Tuesday, April 27, 2010

Re: How to highligh multiple matches?

Am 27.04.2010 16:21, schrieb Peter Princz:
> "foo" in the last question, sorry.
>
> /p.
>
> On 27 April 2010 16:19, Peter Princz<princzp@gmail.com> wrote:
>
>> Dear vim community,
>>
>> I have a small issue, it really just bugs me. Could not find it in the
>> documentation, maybe the collective wisdom of this list will solve it.
>>
>> A search pattern of: .*\zsfoo\ze\&.*\zsbar\ze will find all instances where
>> "foo" and "bar" are at the same line, in arbitrary order.
>> This is OK, but I want them to be highlighted as matches too, both of them.
>>
>>
>> Now, only "bar" will be highlighted, not "foo".
>>
>> Do yo have any idea, how to highlight "bar" as well?
>> Any help very much appreciated.
>>
>> Have a nice day,
>> Peter

This is old code from/after a similar thread.
Hint: use :SearchTwo with bang to get both items matched.


" Created: 2008 Aug 07
" Last Change: 2009 Jun 05
" Rev Days: 3

" Search for 'abc\|def', but match only if the alternative word matches in
" the same line. I.e. if 'abc' matches, require a match for 'def' in the
" same line (and vice versa with [!]). This is unlike the LogiPat plugin,
" which always matches whole lines.
"
" /\%(def.*\)\@<=abc\|abc\%(.*def\)\@=\|\%(abc.*\)\@<=def\|def\%(.*abc\)\@=

com! -bang -nargs=* SearchTwo call feedkeys('/'.SearchTwo(<bang>0, <f-args>))

" lskdfjlsdkf abc lksdjflsadkf abc
" lsk def dfjlsdkf abc lksdjflsadkf abc laskdjflsd abc
" sldkfj lsdkf def lsdkflsfa sdef lsdkfjsdaf
" lsdfjsldk abc lsadkfsd def
" ;sldkf def lsdfksd abc lsdkfj
" all sdkf def lsdkfjsd
" alsdkfj sad abc lsadkfj
"abcdef
"defabc

func! SearchTwo(bang, word1, word2)
let wp1 = a:word1 " anwolib#MagicEscape(a:word1)
let wp2 = a:word2 " anwolib#MagicEscape(a:word2)
let beffmt = '\%%(%s.*\)\@<=%s'
let aftfmt = '%s\%%(.*%s\)\@='
let patfmt = beffmt.'\|'.aftfmt
let pat1 = printf(patfmt, wp2, wp1, wp1, wp2)
let pat2 = printf(patfmt, wp1, wp2, wp2, wp1)
if a:bang
return pat1.'\|'.pat2
else
return pat1
endif
endfunc

" match abc with a match for def before it
" match abc with a match for def after it
" match def with a match for abc before it
" match def with a match for abc after it
"
" (def) abc
" abc (def)
" (abc) def
" def (abc)

--
Andy

--
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

Subscription settings: http://groups.google.com/group/vim_use/subscribe?hl=en

No comments:

Post a Comment