On Fr, 24 Nov 2017, Ingo Karkat wrote: > On 24-Nov-2017 15:13 +0100, Christian Brabandt wrote: > > > > On Fr, 24 Nov 2017, Marius Gedminas wrote: > > > >> On Fri, Nov 24, 2017 at 11:30:03AM +0100, Christian Brabandt > >> wrote: > >>> Hi, while writing the test for checking the URLs I stumbled on > >>> this difference between submatch(1) and \1: #v+ let a = ' > >>> https://www.vim.org ' let g:pat = '.\{-}\(http[^ ]*\).\{-}$' > >>> echo substitute(a, g:pat, submatch(1), '') echo substitute(a, > >>> g:pat, '\1', '') #v- > >>> > >>> If you execute this piece of script, the first map() returns an > >>> empty list, while the second map() returns the URL stripped by > >>> whitespace (which would be my expected behaviour for both map() > >>> calls). > >>> > >>> Is this expected? > >> > >> You're calling submatch(1) and then passing the returned value to > >> substitute(). Try > >> > >> echo substitute(a, g:pat, '\=submatch(1)', '') > >> > >> or > >> > >> echo substitute(a, g:pat, {m->submatch(1)}, '') > >> > >> or > >> > >> echo substitute(a, g:pat, {m->m[1]}, '') > > > > It is only valid in a expression context of the replace part in the > > substitute() function? That is not how I read the help. That > > should be stressed some more (or generate an error, if this is > > generally invalid perhaps?) > > :help submatch() mentions right at the beginning: > ,---- > | Only for an expression in a |:substitute| command or > | substitute() function. > `---- > > When you invoke substitute(a, g:pat, submatch(1), ''), you _first_ > evaluate submatch() as an argument to be passed to the substitute() > function, so it's clearly outside of the substitution context. You > have to use a String expression or lambda, as Marius has shown. I'm > not sure whether an error is called for here; the programming error > should be pretty obvious when testing, anyway. > > Have you been working in a different programming language recently? I > find these things (variable interpolation and :execute is another such > area) natural as long as I'm working in Vimscript; but then they > suddenly appear odd when coming from a different language.
Thanks! Christian -- Wie man sein Kind nicht nennen sollte: Udo Pieh -- -- You received this message from the "vim_dev" 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 --- You received this message because you are subscribed to the Google Groups "vim_dev" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.
