vim7: bug - NL, substitute() and submatch()

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

vim7: bug - NL, substitute() and submatch()

Mikołaj Machowski
Hello,

Cannot place new line character into string using substitute() and
submatch() combination.

When working on string
asdf1234asdf
s/\d*/\=substitute(submatch(0), '.', '\n', 'g')/

works as intended returning
asdf



asdf

But when doing
let a = substitute('asdf1234asdf', '\d*', '\=substitute(submatch(0), ".",
"\n", "g")')
echo a
asdfasdf

For all regular characters it works. Number of \ or \r or ^M doesn't
help.

m.

--
LaTeX + Vim = http://vim-latex.sourceforge.net/
Vim-list(s) Users Map: (last change 15 May)
 http://skawina.eu.org/mikolaj/vimlist
CLEWN - http://clewn.sf.net

Reply | Threaded
Open this post in threaded view
|

Re: vim7: bug - NL, substitute() and submatch()

A.J.Mechelynck
Mikolaj Machowski wrote:

> Hello,
>
> Cannot place new line character into string using substitute() and
> submatch() combination.
>
> When working on string
> asdf1234asdf
> s/\d*/\=substitute(submatch(0), '.', '\n', 'g')/
>
> works as intended returning
> asdf
>
>
>
> asdf
>
> But when doing
> let a = substitute('asdf1234asdf', '\d*', '\=substitute(submatch(0), ".",
> "\n", "g")')
> echo a
> asdfasdf
>
> For all regular characters it works. Number of \ or \r or ^M doesn't
> help.
>
> m.
>

To break a line using :substitute, you need to insert "\r", not "\n".
One of the implications of this is that (IIUC)

        :1,$s/\n/\r/g

actually counts line breaks, replacing them by themselves (but it's not
the most economical way to do that). That is one of the quirks of Vim; I
believe that altering it would break too many existing scripts.

Best regards,
Tony.

Reply | Threaded
Open this post in threaded view
|

Re: vim7: bug - NL, substitute() and submatch()

Mikołaj Machowski
Dnia pi?tek 03 czerwiec 2005 20:52, A. J. Mechelynck napisa?:
> > But when doing
> > let a = substitute('asdf1234asdf', '\d*', '\=substitute(submatch(0),
> > ".", "\n", "g")')
> > echo a
> > asdfasdf
> >
> > For all regular characters it works. Number of \ or \r or ^M doesn't
                                                        ^^^
> > help.
>
> To break a line using :substitute, you need to insert "\r", not "\n".

Did you read what I wrote?

Besides, this is about substitute() (where \n is accepted), not
:substitute.

m.

Reply | Threaded
Open this post in threaded view
|

Re: vim7: bug - NL, substitute() and submatch()

A.J.Mechelynck
Mikolaj Machowski wrote:

> Dnia pi?tek 03 czerwiec 2005 20:52, A. J. Mechelynck napisa?:
>
>>>But when doing
>>>let a = substitute('asdf1234asdf', '\d*', '\=substitute(submatch(0),
>>>".", "\n", "g")')
>>>echo a
>>>asdfasdf
>>>
>>>For all regular characters it works. Number of \ or \r or ^M doesn't
>
>                                                         ^^^
>
>>>help.
>>
>>To break a line using :substitute, you need to insert "\r", not "\n".
>
>
> Did you read what I wrote?
>
> Besides, this is about substitute() (where \n is accepted), not
> :substitute.
>
> m.
>
>
>
>

Here's what I get with gvim 7.00aa snapshot 0078 on WinXP:

        :echo
substitute('asdf1234asdf','\d*','\=substitute(submatch(0),".","\n","g")')
E119: Not enough arguments for function: substitute
E15: Invalid expression:
substitute('asdf1234asdf','\d*','\=substitute(submatch(0),".","\n","g")')

        :echo
substitute('asdf1234asdf','\d*','\=substitute(submatch(0),".","\n","g")','g')
asdf

        :echo substitute('asdf1234asdf','\d*','\=substitute(submatch(0),".",' .
"'" . '\n' . "'" . ',"g")','g')
asdf
asdf

In other words: it works for me, when I make sure that only single
quotes are used around the \n


Best regards,
Tony.





Best regards,
Tony.

Reply | Threaded
Open this post in threaded view
|

Re: vim7: bug - NL, substitute() and submatch()

Mikołaj Machowski
Dnia sobota 04 czerwiec 2005 19:41, A. J. Mechelynck napisa?:
>
> Here's what I get with gvim 7.00aa snapshot 0078 on WinXP:
> :echo
>
> substitute('asdf1234asdf','\d*','\=substitute(submatch(0),".","\n","g")'
>) E119: Not enough arguments for function: substitute

That is expected. substitute() requires flag element, even if empty.
> :echo
>
> substitute('asdf1234asdf','\d*','\=substitute(submatch(0),".","\n","g")'
>,'g') asdf

That is my problem.
>
> :echo substitute('asdf1234asdf','\d*','\=substitute(submatch(0),".",' .
>
> "'" . '\n' . "'" . ',"g")','g')
> asdf
> asdf
>
Doesn't work for me. Today CVS, Linux, Mandriva 10.2.

Note that when replacing echo with echomsg result is:
asdf^M^M^M^Masdf

So, replacement was successful (even with Tony's second example equal to
my try) just interpretation of character is borked.

m.

Reply | Threaded
Open this post in threaded view
|

Re: vim7: bug - NL, substitute() and submatch()

A.J.Mechelynck
Mikolaj Machowski wrote:

> Dnia sobota 04 czerwiec 2005 19:41, A. J. Mechelynck napisa?:
>
>>Here's what I get with gvim 7.00aa snapshot 0078 on WinXP:
>> :echo
>>
>>substitute('asdf1234asdf','\d*','\=substitute(submatch(0),".","\n","g")'
>>) E119: Not enough arguments for function: substitute
>
>
> That is expected. substitute() requires flag element, even if empty.
>
>> :echo
>>
>>substitute('asdf1234asdf','\d*','\=substitute(submatch(0),".","\n","g")'
>>,'g') asdf
>
>
> That is my problem.
>
>> :echo substitute('asdf1234asdf','\d*','\=substitute(submatch(0),".",' .
>>
>>"'" . '\n' . "'" . ',"g")','g')
>>asdf
>>asdf
>>
>
> Doesn't work for me. Today CVS, Linux, Mandriva 10.2.
>
> Note that when replacing echo with echomsg result is:
> asdf^M^M^M^Masdf
[...]

Replacing echo with echomsg in either my second or my third example
gives me asdf^M^M^M^Masdf too. With

        :let @* = substitute(

etc., followed by

        :reg

I also get asdf^M^M^M^Masdf -- apparently the problem is with ":echo",
not with substitute().


Best regards,
Tony.

Reply | Threaded
Open this post in threaded view
|

Re: vim7: bug - NL, substitute() and submatch()

Mikołaj Machowski
Dnia niedziela 05 czerwiec 2005 00:33, A. J. Mechelynck napisa??:

> >
> > Doesn't work for me. Today CVS, Linux, Mandriva 10.2.
> >
> > Note that when replacing echo with echomsg result is:
> > asdf^M^M^M^Masdf
>
> [...]
>
> Replacing echo with echomsg in either my second or my third example
> gives me asdf^M^M^M^Masdf too. With
>
> :let @* = substitute(
>
> etc., followed by
>
> :reg
>
> I also get asdf^M^M^M^Masdf -- apparently the problem is with ":echo",
> not with substitute().

No. echo and echomsg behaviour is correct. echo tries to interpret
special characters and remove them when cannot do this. While echomsg
show everything without interpretation.

Try
echo "asdf\nasdf"
echomsg "asdf\nasdf"

What I posted is dumbed down example of real behaviour with assigning
variables and so on. Everywhere else \n was inserted properly as
newline. Here is passed as ^M on Unix in DOS-ended files. I think here
is solution of mystery. On MS-Windows ^M is treated as newline (part of
it?), while on Unix (Linux in this case) it is left as is. Somewhere
should be put test on 'fformat'.

m.