John Aldridge | 1 Dec 2003 07:22
Favicon

RE: Calling a function from a :s command (weird results)

At 23:42 11-30-2003, Klaus Bosau wrote:

>On Sun, 30 Nov 2003, David Fishburn wrote:
>
> >>> I get this kind of thing once in a while; and if I remember  correctly,
> >>> it's a quoting issue.  From your results, it looks as if the \= isn't
> >>> being taken for its intended meaning.  Without remembering  what really
> >>> fixes this, I'd say go ahead and throw an extra backslash in there.
> >>
> >> Just a guess.. Flag 'C' in 'cpo'?
> >
> > Nope.
> >   cpoptions=aABceFs
>
>Second try..
>
>   function! InvertString(str)
>       " Courtesy of Antony Scriven
>       " This will invert/reverse a string
>       " This will work on arbitrary length strings, too. The /.*/ should
>   be
>   ^
>       " quick, which might make it up for using a regex rather than using
>       " numerous commands in a :while loop.
>       "
>       " This can be used in a substitute command as follows:
>       " :%s/AUTHORIZATION/\=InvertString(submatch(0))
>       let inverted = substitute(a:str, '.\(.*\)\ <at> =',
>                   \ '\=a:str[strlen(submatch(1))]', 'g')
>
>       return inverted
>   endfunction
>
>Klaus

I ran a couple of tests and the results are interesting.

Could it be that submatching can't be nested?

     function! TestSubSub(str)
         if 1
             return substitute(a:str, 'h\(i\)', '\=strlen(submatch(1))', 'g')
             " Produces:
             " T=strlen(submatch(1))s is some sample text.
         else
             return substitute(a:str, 'h\(i\)', strlen(submatch(1)), 'g')
             " Produces:
             " T2s is some sample text.
             " Interesting:
             " The 2 is the submatch strlen from outside this call!!!
         endif
     endfunction

     " This is some sample text.
     " Try: %s/Th\(is\)/\=TestSubSub(submatch(0))/

~
~
~
"John R. Aldridge, Jr." 

Gmane