Saturday, March 5, 2011

Re: in sub-replace-expression('\=')

> Could anyone tell me why<NL>(0x0a) should be replaced by<CR>(0x0d)
> in sub-replace-expression('\=')?
>
> See the following code snippet;
>
> function! s:nl()
> return "\n"
> endfunction
> echomsg substitute('a', '.', "\n", "") "(1)
> echomsg substitute('a', '.', '\=s:nl()', "") "(2)
>
> (1) returns<NL>(0x0a), (2) returns<CR>(0x0d).
>
> Why such design choice was done?

I think you are doing something you're not really supposed to do. The
bug is probably that it doesn't trigger an error, not that it works
unexpectedly.

I think there are a bunch of different things going on here.

For (1), I don't get <NL> returned, but ^@ (<Nul>). This is somewhat
expected, given :help keycodes, but still a little mysterious, as that
help topic is about bytes being represented in a buffer, not a string.
Strings usually behave a bit differently and more normally.

But I get different results with :echo to what I get with :echomsg, too.
:echo behaves in a more expected manner. So I suspect :echomsg is not
really printing a Vim string properly, treating it more like raw text
out of a buffer, which is slightly different, and :echo is doing more
the right thing. There are a lot of funny little behaviours with things
like this. Doing Ctrl-R = in insert mode and then using strings like
"\<NL>", "\<CR>", "\<Nul>", "\<Up>", etc. all do different and strange
things, too. Likewise trying them with :echo and :echomsg. Lots of
strangeness.

Regarding (2), note this comment in the help (:help
sub-replace-expression):

"This does not work recursively: a substitute() function inside the
expression cannot use "\=" for the substitute string."

This suggests you can't really expect \= to work as the substitute
string in a substitute() function call.

However, the help text at that place, as well as at :help
sub-replace-special sheds some light on the matter, and perhaps explains
why things are happening the way they are. Of course, the differences
between :echo and :echomsg make it even more confusing.

I'm not sure how much, if any, of this could be regarded as bugs, and
how much, if any, would be fixed, given backwards-compatibility issues.

Ben.

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments: