Skip to content

Commit

Permalink
update matcher to match that of ctrlp-cmatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronjensen committed Aug 8, 2014
1 parent 3559e2b commit 417ab86
Showing 1 changed file with 41 additions and 19 deletions.
60 changes: 41 additions & 19 deletions autoload/ctrlp.vim
Expand Up @@ -1628,25 +1628,47 @@ fu! s:highlight(pat, grp)
el
let pat = a:pat

" calculate how many letters are here
let lettercount = len(split(pat, '\\{-}'))
for i in range(lettercount)
" surround the letter we care about with \zs and \ze so only it is
" highlighted in this go. Prefer the letter right next to the previous
" one, otherwise scan out to the last one
let letterpat = substitute(pat,
\ '^\%(\\\?.\zs\[\^\\\?.\]\\{-}\)\{'.i.'}\(\\\?.\)\%(\[\^\\\?.\]\\{-}\)\?\ze.*$',
\ '\\(\\zs\1\\|.*\\zs\1\\)\\ze.\\{-}', '')

if s:byfname
" replace [^x] with [^/x] to make sure no slashes between letters
let letterpat = substitute(letterpat, '\[\^\(.\{-}\)\]\\{-}', '[^\\/\1]\\{-}', 'g')
" replace the end to make sure no slashes follow the pattern
let letterpat = substitute(letterpat, '\$\@<!$', '[^\\/]*$', 'g')
en

cal matchadd(a:grp, ( s:martcs == '' ? '\c' : '\C' ).letterpat)
endfo
" get original characters so we can rebuild pat
let chars = split(pat, '\[\^\\\?.\]\\{-}')

" Build a pattern like /a.*b.*c/ from abc (but with .\{-} non-greedy
" matchers instead)
let pat = join(chars, '.\{-}')
" Ensure we match the last version of our pattern
let ending = '\(.*'.pat.'\)\@!'
" Case sensitive?
let beginning = ( s:martcs == '' ? '\c' : '\C' ).'^.*'
if s:byfname
" Make sure there are no slashes in our match
let beginning = beginning.'\([^\/]*$\)\@='
end

for i in range(len(chars))
" Surround our current target letter with \zs and \ze so it only
" actually matches that one letter, but has all preceding and trailing
" letters as well.
" \zsa.*b.*c
" a\(\zsb\|.*\zsb)\ze.*c
let charcopy = copy(chars)
if i == 0
let charcopy[i] = '\zs'.charcopy[i].'\ze'
let middle = join(charcopy, '.\{-}')
else
let before = join(charcopy[0:i-1], '.\{-}')
let after = join(charcopy[i+1:-1], '.\{-}')
let c = charcopy[i]
" for abc, match either ab.\{-}c or a.*b.\{-}c in that order
let cpat = '\(\zs'.c.'\|'.'.*\zs'.c.'\)\ze.*'
let middle = before.cpat.after
endif

" Now we matchadd for each letter, the basic form being:
" ^.*\zsx\ze.*$, but with our pattern we built above for the letter,
" and a negative lookahead ensuring that we only highlight the last
" occurrence of our letters. We also ensure that our matcher is case
" insensitive or sensitive depending.
cal matchadd(a:grp, beginning.middle.ending)
endfor
en

cal matchadd('CtrlPLinePre', '^>')
Expand Down

0 comments on commit 417ab86

Please sign in to comment.