Re: escape() and '

2006-10-25 Thread Nikolai Weibull

On 10/25/06, Hari Krishna Dara [EMAIL PROTECTED] wrote:


'\V'.escape(substitute(regex, ', '', 'g'), '\')


Uh, when did Vim's strings become objects?  (Would be really nice if
they were, mind you.)

 nikolai


Re: escape() and '

2006-10-25 Thread Nikolai Weibull

On 10/25/06, Nikolai Weibull [EMAIL PROTECTED] wrote:

On 10/25/06, Hari Krishna Dara [EMAIL PROTECTED] wrote:

 '\V'.escape(substitute(regex, ', '', 'g'), '\')

Uh, when did Vim's strings become objects?  (Would be really nice if
they were, mind you.)


Ahahaha, OK.  Sorry.  Goddam I hate the concatenation operator.  It's
virtually impossible to spot.  '\V' . escape(...), now that I can sort
of semi-read.

 nikolai


Re: escape() and '

2006-10-24 Thread Mikolaj Machowski
Dnia poniedziałek, 23 października 2006 16:25, Nikolai Weibull napisał:
 On 10/23/06, Mikolaj Machowski [EMAIL PROTECTED] wrote:
  Hello,
 
  I understand that escape() was primarily designed to escape strings
  when passing to system functions, but personally I never used that and
  in didn't noticed such use in various scripts but very often it is
  used to escape various charaters in Vim's own regexp matching or
  passing one string to some other Vim command.
 
  Hence is the problem: when escaping ' with escape(), character is
  prepended with \ which doesn't make sense when passing it to other Vim
  command because proper way to escape it in Vim is doubling it with
 
  another '. Example::
  :echo escape('as''df', )

 There should really be a third, optional, parameter to escape() where
 you can specify what character to use for escaping.

That wouldn't be real solution because to escape ' you still (in most
situations) would need two escape() calls. One for escape ' with ' and
second for rest of characters with \. The best solution is providing
info about context.

Yakov: why so much contexts? for single and double quotes there is only
difference in number of backslashes. Escaping in command line is rather
problem of which character should be escaped (most notably space), not
system of escaping.

I'd like to see only one flag additional and one default:

s - [default - for backward compatibility] system escaping with \
v - Vim escaping, ' for ' and possibly some other (future?) differences

m.



Re: escape() and '

2006-10-24 Thread Nikolai Weibull

On 10/23/06, Mikolaj Machowski [EMAIL PROTECTED] wrote:


Dnia poniedziałek, 23 października 2006 16:25, Nikolai Weibull napisał:


I'll take your word for it that that means that I wrote something at
some time or other.


 There should really be a third, optional, parameter to escape() where
 you can specify what character to use for escaping.



That wouldn't be real solution because to escape ' you still (in most
situations) would need two escape() calls. One for escape ' with ' and
second for rest of characters with \. The best solution is providing
info about context.


I don't follow.  When would you need to escape both ' with '' and
other characters with \?  The only active character in a
single-quoted string is the single-quote itself.

Oh, I see.  You're thinking of creating a string for passing to
substitute() inside an :execute, or something like that.  Ah, true,
then you'd need to escape the single-quotes for the string, and, e.g.,
. with \..  Escaping is a lot more difficult than one often
thinks, I suppose.

 nikolai


Re: escape() and '

2006-10-24 Thread Mikolaj Machowski
Dnia wtorek, 24 października 2006 11:10, Nikolai Weibull napisał:
 Oh, I see.  You're thinking of creating a string for passing to
 substitute() inside an :execute, or something like that.  Ah, true,
 then you'd need to escape the single-quotes for the string, and, e.g.,
 . with \..  Escaping is a lot more difficult than one often
 thinks, I suppose.

Plus eg. matching string with string()'ed list.

m.



Re: escape() and '

2006-10-24 Thread Hari Krishna Dara

On Mon, 23 Oct 2006 at 12:51pm, Mikolaj Machowski wrote:

 Hello,

 I understand that escape() was primarily designed to escape strings when
 passing to system functions, but personally I never used that and in
 didn't noticed such use in various scripts but very often it is used to
 escape various charaters in Vim's own regexp matching or passing one
 string to some other Vim command.

 Hence is the problem: when escaping ' with escape(), character is
 prepended with \ which doesn't make sense when passing it to other Vim
 command because proper way to escape it in Vim is doubling it with
 another '. Example::

 :echo escape('as''df', )

I didn't even know that you can escape a single-quote inside
single-quotes like this, where is this information burried in Vim help?
It seems to work though, so just checking if it is documented.

-- 
Thanks,
Hari

__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 


Re: escape() and '

2006-10-24 Thread Nikolai Weibull

On 10/25/06, Hari Krishna Dara [EMAIL PROTECTED] wrote:


 :echo escape('as''df', )

I didn't even know that you can escape a single-quote inside
single-quotes like this, where is this information burried in Vim help?
It seems to work though, so just checking if it is documented.


It's new for Vim 7 I believe.

:help literal-string

 nikolai


Re: escape() and '

2006-10-24 Thread Hari Krishna Dara

On Tue, 24 Oct 2006 at 11:10am, Nikolai Weibull wrote:
   There should really be a third, optional, parameter to escape() where
   you can specify what character to use for escaping.

  That wouldn't be real solution because to escape ' you still (in most
  situations) would need two escape() calls. One for escape ' with ' and
  second for rest of characters with \. The best solution is providing
  info about context.

 I don't follow.  When would you need to escape both ' with '' and
 other characters with \?  The only active character in a
 single-quoted string is the single-quote itself.

 Oh, I see.  You're thinking of creating a string for passing to
 substitute() inside an :execute, or something like that.  Ah, true,
 then you'd need to escape the single-quotes for the string, and, e.g.,
 . with \..  Escaping is a lot more difficult than one often
 thinks, I suppose.

I don't this this is a valid case either. The problem is only when you
need to use such strings with the :exec command (as that involves
concatenating strings), but if you are just passing on the string to
Vim functions, you don't have to worry about escaping single quotes.
Even when you need to concatenate and use a regex, you still need to
only escape the single-quotes and backslashes and use it in the \V mode,
something like:

'\V'.escape(substitute(regex, ', '', 'g'), '\')

-- 
Hari

__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 


Re: escape() and '

2006-10-23 Thread A.J.Mechelynck

Mikolaj Machowski wrote:

Hello,

I understand that escape() was primarily designed to escape strings when
passing to system functions, but personally I never used that and in
didn't noticed such use in various scripts but very often it is used to
escape various charaters in Vim's own regexp matching or passing one
string to some other Vim command.

Hence is the problem: when escaping ' with escape(), character is
prepended with \ which doesn't make sense when passing it to other Vim
command because proper way to escape it in Vim is doubling it with
another '. Example::

:echo escape('as''df', )

m.





escape() is for a double-quoted string, or for the unquoted strings accepted 
by some commands. For a single-quoted string you need something else, such as


substitute(substitute(string,','',g),'^.*$','''\0''')


Best regards,
Tony.


Re: escape() and '

2006-10-23 Thread Nikolai Weibull

On 10/23/06, A.J.Mechelynck [EMAIL PROTECTED] wrote:

Mikolaj Machowski wrote:
 Hello,

 I understand that escape() was primarily designed to escape strings when
 passing to system functions, but personally I never used that and in
 didn't noticed such use in various scripts but very often it is used to
 escape various charaters in Vim's own regexp matching or passing one
 string to some other Vim command.

 Hence is the problem: when escaping ' with escape(), character is
 prepended with \ which doesn't make sense when passing it to other Vim
 command because proper way to escape it in Vim is doubling it with
 another '. Example::

 :echo escape('as''df', )



escape() is for a double-quoted string, or for the unquoted strings accepted
by some commands. For a single-quoted string you need something else, such as

substitute(substitute(string,','',g),'^.*$','''\0''')


I realize that you want to provide a solution to a problem and that's
fine, but you don't seem to have understood Mikolaj's
statement/question.  He begins with I understand that escape() was
primarily designed to escape strings when passing to system
functions, so I think he can figure out the solution well enough, and
he's instead asking why escape() hasn't been adopted to other areas of
use, such as escaping for regexes, which is a very common operation,
or passing a string to another Vim command.

Also, why would you ever write substitute(x, '^.*$', '''\0''') instead
of ' . x . '?

 nikolai


Re: escape() and '

2006-10-23 Thread Yakov Lerner

On 10/23/06, Nikolai Weibull [EMAIL PROTECTED] wrote:

On 10/23/06, Mikolaj Machowski [EMAIL PROTECTED] wrote:
 Hello,

 I understand that escape() was primarily designed to escape strings when
 passing to system functions, but personally I never used that and in
 didn't noticed such use in various scripts but very often it is used to
 escape various charaters in Vim's own regexp matching or passing one
 string to some other Vim command.

 Hence is the problem: when escaping ' with escape(), character is
 prepended with \ which doesn't make sense when passing it to other Vim
 command because proper way to escape it in Vim is doubling it with
 another '. Example::

 :echo escape('as''df', )

There should really be a third, optional, parameter to escape() where
you can specify what character to use for escaping.


I'd suggest that 3rd arg for escape(), if it's neeed (well you
always can brute-force escape things using substitute()), it would
be code of the context (1-5) for which escaping is destined. Because
Vim has at least 4-5 different escaping rules in different contexts:

1) escaping rules for double-quoted strings
2) escaping rules for single-quoted strings
3) escaping rules for :-command-line
4) escaping rules for regexes
5) escaping rules rules for rhs of the mapping.

Escaping would be different for each of those contexts.

Yakov