ap schrieb:
>
>
> On Jan 9, 6:06 pm, Andy Wokula <[EMAIL PROTECTED]> wrote:
>> I tried to write a function that collects all the matches for a given
>> pattern in the text.
>>
>> For that purpose, it would be very nice if the following worked:
>>     :%s/{pattern}/\=MyAddMatch(submatch(0))/gn
>
>
> I agree, this could be easier.
>
> Doing it with search() seems a bit tricky.
>
> -it needs 2 calls to search()
> -the startposition needs special care
>   ( there could be a match in the first column of
>     the first line one wants to search )
>
> ----------%<------------------
> "Could be improved by handling the startpos differently
> "and avoid the 3rd search.
> func! CollectWords( pat, first_line, last_line )
>   let result = []
>   call cursor(a:first_line,1)
>   while search(a:pat,'cW',a:last_line)
>     normal! ms
>     call search(a:pat,'ceW')
>     normal! me
>     normal! `sv`ey
>     call add(result,@")
>     call search('.','W')
>   endwhile
>   return result
> endfun
> ----------%<------------------
>
>
> Maybe someone has a better idea, apart from source modification.
>
> -ap

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

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_dev" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Raspunde prin e-mail lui