vim7: feature request - sub-replace-special extension

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

vim7: feature request - sub-replace-special extension

Mikołaj Machowski
Hello,

Please extend sub-replace-special for items:

\%d
\%o
\%x
\%u
\%U

For replacing characters with their numerical value.

Of course there can be workarounds with custom nr2char(Utf2Nr())
functions but they are awkward.

m.


Reply | Threaded
Open this post in threaded view
|

Re: vim7: feature request - sub-replace-special extension

Bram Moolenaar

Mikolaj Machowski wrote:

> Please extend sub-replace-special for items:
>
> \%d
> \%o
> \%x
> \%u
> \%U
>
> For replacing characters with their numerical value.
>
> Of course there can be workarounds with custom nr2char(Utf2Nr())
> functions but they are awkward.

I suppose you mean that \%d would convert the whole matched text to a
number and then back to a string.  This is quite limiting, since you
can't handle submatches and only do it once.  And how about "%04d" to
padd with zeroes?

I was planning to add a printf() function, now that we have an internal
printf function and can check the arguments for the correct type.  Real
soon now...

--
ARTHUR:    Be quiet!  I order you to shut up.
OLD WOMAN: Order, eh -- who does he think he is?
ARTHUR:    I am your king!
OLD WOMAN: Well, I didn't vote for you.
                 "Monty Python and the Holy Grail" PYTHON (MONTY) PICTURES LTD

 /// Bram Moolenaar -- [hidden email] -- http://www.Moolenaar.net   \\\
///        Sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\              Project leader for A-A-P -- http://www.A-A-P.org        ///
 \\\     Buy LOTR 3 and help AIDS victims -- http://ICCF.nl/lotr.html   ///
Reply | Threaded
Open this post in threaded view
|

Re: vim7: feature request - sub-replace-special extension

Mikołaj Machowski
Dnia pi?tek 29 lipiec 2005 21:15, Bram Moolenaar napisa?:

> Mikolaj Machowski wrote:
> > Please extend sub-replace-special for items:
> >
> > \%d
> > \%o
> > \%x
> > \%u
> > \%U
> >
> > For replacing characters with their numerical value.
> >
> > Of course there can be workarounds with custom nr2char(Utf2Nr())
> > functions but they are awkward.
>
> I suppose you mean that \%d would convert the whole matched text to a
> number and then back to a string.  This is quite limiting, since you
> can't handle submatches and only do it once.  And how about "%04d" to
> padd with zeroes?

Sorry, not clear enough. I'd like to work it on character base:
\%d123
\%o103
\%x1a
\%u12ab


Example
s/\(\d\+\)E\>/\1\%u20ac/g

Should change 200E into 200[euro sign]

m.

Reply | Threaded
Open this post in threaded view
|

Re: vim7: feature request - sub-replace-special extension

A.J.Mechelynck
----- Original Message -----
From: "Mikolaj Machowski" <[hidden email]>
To: <[hidden email]>
Sent: Saturday, July 30, 2005 12:49 AM
Subject: Re: vim7: feature request - sub-replace-special extension


> Dnia pi?tek 29 lipiec 2005 21:15, Bram Moolenaar napisa?:
>> Mikolaj Machowski wrote:
>> > Please extend sub-replace-special for items:
>> >
>> > \%d
>> > \%o
>> > \%x
>> > \%u
>> > \%U
>> >
>> > For replacing characters with their numerical value.
>> >
>> > Of course there can be workarounds with custom nr2char(Utf2Nr())
>> > functions but they are awkward.
>>
>> I suppose you mean that \%d would convert the whole matched text to a
>> number and then back to a string.  This is quite limiting, since you
>> can't handle submatches and only do it once.  And how about "%04d" to
>> padd with zeroes?
>
> Sorry, not clear enough. I'd like to work it on character base:
> \%d123
> \%o103
> \%x1a
> \%u12ab
>
>
> Example
> s/\(\d\+\)E\>/\1\%u20ac/g
>
> Should change 200E into 200[euro sign]
>
> m.

You can use Ctrl-V u 20AC (without the spaces) in a substitute, see ":help
i_CTRL-V_digit" which is not only for insert mode but also on the
command-line. You can also enter the Euro sign by means of a digraph, in
this case Ctrl-K = e (without the spaces again; see "help digraph.txt"). So
instead of \%u20ac you could hit ^Vu20ac (where ^V means "hit Ctrl-V") and
see the euro sign appear in your substitute replacement text. (And if you
use Ctrl-V to paste, then use Ctrl-Q instead.)

You could also use an expression, as in \="\<Char-0x20AC>" but that would be
more cumbersome than the ^V or ^K methods above.

HTH,
Tony.


Reply | Threaded
Open this post in threaded view
|

Re: vim7: feature request - sub-replace-special extension

Bram Moolenaar
In reply to this post by Mikołaj Machowski

Mikolaj Machowski wrote:

> > > Please extend sub-replace-special for items:
> > >
> > > \%d
> > > \%o
> > > \%x
> > > \%u
> > > \%U
> > >
> > > For replacing characters with their numerical value.
> > >
> > > Of course there can be workarounds with custom nr2char(Utf2Nr())
> > > functions but they are awkward.
> >
> > I suppose you mean that \%d would convert the whole matched text to a
> > number and then back to a string.  This is quite limiting, since you
> > can't handle submatches and only do it once.  And how about "%04d" to
> > padd with zeroes?
>
> Sorry, not clear enough. I'd like to work it on character base:
> \%d123
> \%o103
> \%x1a
> \%u12ab
>
>
> Example
> s/\(\d\+\)E\>/\1\%u20ac/g
>
> Should change 200E into 200[euro sign]

For that kind of thing you can use the expression mechanism:

        s/\(\d\+\)E\>/\=submatch(1) . "\u20ac"/g

--
BLACK KNIGHT: I'm invincible!
ARTHUR:       You're a looney.
                 "Monty Python and the Holy Grail" PYTHON (MONTY) PICTURES LTD

 /// Bram Moolenaar -- [hidden email] -- http://www.Moolenaar.net   \\\
///        Sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\              Project leader for A-A-P -- http://www.A-A-P.org        ///
 \\\     Buy LOTR 3 and help AIDS victims -- http://ICCF.nl/lotr.html   ///
Reply | Threaded
Open this post in threaded view
|

Re: vim7: feature request - sub-replace-special extension

Mikołaj Machowski
In reply to this post by A.J.Mechelynck
Dnia sobota 30 lipiec 2005 02:06, Tony Mechelynck napisa?:
> You can use Ctrl-V u 20AC (without the spaces) in a substitute, see
> ":help i_CTRL-V_digit" which is not only for insert mode but also on the
> command-line. You can also enter the Euro sign by means of a digraph, in
> this case Ctrl-K = e (without the spaces again; see "help digraph.txt").
> So instead of \%u20ac you could hit ^Vu20ac (where ^V means "hit
> Ctrl-V") and see the euro sign appear in your substitute replacement
> text. (And if you use Ctrl-V to paste, then use Ctrl-Q instead.)

Doesn't work when you have make generic solution. In scripts
s/E/<c-v>u20ac/ (in all variants) doesn't work.
Tried it.
>
> You could also use an expression, as in \="\<Char-0x20AC>" but that
> would be more cumbersome than the ^V or ^K methods above.
>
Thanks - that is it. Looks really ugly but works. And as I need this for
script it doesn't really matter.

Bram - scrap feature wish but this tip could be added to
sub-replace-special/expression help. Like:

Expression can be used to insert character by its numerical value.
Example:

        s/E/\="\<Char-0x20ac>"/g

Will replace all E with euro sign. Read more in |<Char->|.

m.

Reply | Threaded
Open this post in threaded view
|

Re: vim7: feature request - sub-replace-special extension

A.J.Mechelynck
----- Original Message -----
From: "Mikolaj Machowski" <[hidden email]>
To: <[hidden email]>
Sent: Saturday, July 30, 2005 2:40 PM
Subject: Re: vim7: feature request - sub-replace-special extension


> Dnia sobota 30 lipiec 2005 02:06, Tony Mechelynck napisa?:
>> You can use Ctrl-V u 20AC (without the spaces) in a substitute, see
>> ":help i_CTRL-V_digit" which is not only for insert mode but also on the
>> command-line. You can also enter the Euro sign by means of a digraph, in
>> this case Ctrl-K = e (without the spaces again; see "help digraph.txt").
>> So instead of \%u20ac you could hit ^Vu20ac (where ^V means "hit
>> Ctrl-V") and see the euro sign appear in your substitute replacement
>> text. (And if you use Ctrl-V to paste, then use Ctrl-Q instead.)
>
> Doesn't work when you have make generic solution. In scripts
> s/E/<c-v>u20ac/ (in all variants) doesn't work.
> Tried it.

Did you enter less-than, c, dash, v, greater-than or did you hit the Ctrl-V
(or Ctrl-Q) key? The latter should work, but since it is used to insert a
non-ASCII character into your script, you would need a "scriptencoding
UTF-8" statement somewhere above it in the script (see ":help
:scriptencoding"), and you would need to have set 'encoding' and
'fileencoding' to UTF-8 when editing the script. _Then_ you should see

    s/E/?

(that's s, slash, E, slash, Euro) in your script, and it should work.

>>
>> You could also use an expression, as in \="\<Char-0x20AC>" but that
>> would be more cumbersome than the ^V or ^K methods above.
>>
> Thanks - that is it. Looks really ugly but works. And as I need this for
> script it doesn't really matter.
>
> Bram - scrap feature wish but this tip could be added to
> sub-replace-special/expression help. Like:
>
> Expression can be used to insert character by its numerical value.
> Example:
>
> s/E/\="\<Char-0x20ac>"/g
>
> Will replace all E with euro sign. Read more in |<Char->|.
>
> m.
>
>
>


Reply | Threaded
Open this post in threaded view
|

Re: vim7: feature request - sub-replace-special extension

Mikołaj Machowski
Dnia sobota 30 lipiec 2005 15:53, Tony Mechelynck napisa?:

> ----- Original Message -----
> From: "Mikolaj Machowski" <[hidden email]>
> To: <[hidden email]>
> Sent: Saturday, July 30, 2005 2:40 PM
> Subject: Re: vim7: feature request - sub-replace-special extension
>
> > Dnia sobota 30 lipiec 2005 02:06, Tony Mechelynck napisa?:
> >> You can use Ctrl-V u 20AC (without the spaces) in a substitute, see
> >> ":help i_CTRL-V_digit" which is not only for insert mode but also on
> >> the command-line. You can also enter the Euro sign by means of a
> >> digraph, in this case Ctrl-K = e (without the spaces again; see "help
> >> digraph.txt"). So instead of \%u20ac you could hit ^Vu20ac (where ^V
> >> means "hit Ctrl-V") and see the euro sign appear in your substitute
> >> replacement text. (And if you use Ctrl-V to paste, then use Ctrl-Q
> >> instead.)
> >
> > Doesn't work when you have make generic solution. In scripts
> > s/E/<c-v>u20ac/ (in all variants) doesn't work.
> > Tried it.
>
> Did you enter less-than, c, dash, v, greater-than or did you hit the
> Ctrl-V (or Ctrl-Q) key? The latter should work, but since it is used to
> insert a non-ASCII character into your script, you would need a
> "scriptencoding UTF-8" statement somewhere above it in the script (see
> ":help
>
> :scriptencoding"), and you would need to have set 'encoding' and
>
> 'fileencoding' to UTF-8 when editing the script. _Then_ you should see
>
>     s/E/?
>
> (that's s, slash, E, slash, Euro) in your script, and it should work.

Doesn't work literal version of
s/E/<c-v>u20ac/
echo substitute('e', 'e', "\<c-v>u20ac", '')

You will see in command line ^Vu20ac.

>
> >> You could also use an expression, as in \="\<Char-0x20AC>" but that
> >> would be more cumbersome than the ^V or ^K methods above.
> >
> > Thanks - that is it. Looks really ugly but works. And as I need this
> > for script it doesn't really matter.
> >
> > Bram - scrap feature wish but this tip could be added to
> > sub-replace-special/expression help. Like:
> >
> > Expression can be used to insert character by its numerical value.
> > Example:
> >
> > s/E/\="\<Char-0x20ac>"/g
> >

Bah. This version also don't work to full extend (of course works as
advertised in docs, but...). Example:

echo substitute("asdf U+2211", "U+\([a-fA-F0-9]\)",
"\='\<Char-0x'.submatch(1).'>'", '')

Doesn't work because \= resolves only first level of expression (replace
submatch with 2211) but will not insert Char.

m.