For that purpose, it would be very nice if the following worked:
:%s/{pattern}/\=MyAddMatch(submatch(0))/gn
but it doesn't, because the "n" flag disables evaluation of the
expression after '\='.
IMHO the "n" flag should only ignore the result of the expression.
--
Andy
Yeah, this could be accepted as a workaround.
but: it doesn't work safely in all situations (e.g. ambigious NUL
characters in the returned string).
Also it would unnecessarily change the buffer (ugly in readonly
buffers) and add useless undo information.
>> but it doesn't, because the "n" flag disables evaluation of the
>> expression after '\='.
>>
>> IMHO the "n" flag should only ignore the result of the expression.
>>
>> --
>> Andy
>
> -ap
--
Andy
Thanks for your try. Right, two search()es are much effort just for
getting the contents of a single match.
I first tried to use
//^M
y//e^M
in a loop, but this fails for matches of length 1.
Your function does it right.
Since in most cases I have a pattern that matches on a single line and
I only want matches for the last search pattern, I'll go with this:
func! CollectMatches()
let matches = []
let patq = substitute(@/, "'", "''", "g")
exec 'sil g//call substitute(getline("."),'''.patq
\.''', "\\=add(matches, submatch(0))", "g")'
return matches
endfunc
--
Andy