Add g:ctrlp_match_func

This commit is contained in:
Kien N 2012-03-12 08:58:01 +07:00
parent 5ed0cba954
commit 01910d3bb6
2 changed files with 97 additions and 11 deletions

View File

@ -21,6 +21,7 @@ fu! s:opts()
\ 'g:ctrlp_highlight_match': ['s:mathi', [1, 'CtrlPMatch']], \ 'g:ctrlp_highlight_match': ['s:mathi', [1, 'CtrlPMatch']],
\ 'g:ctrlp_jump_to_buffer': ['s:jmptobuf', 2], \ 'g:ctrlp_jump_to_buffer': ['s:jmptobuf', 2],
\ 'g:ctrlp_lazy_update': ['s:lazy', 0], \ 'g:ctrlp_lazy_update': ['s:lazy', 0],
\ 'g:ctrlp_match_func': ['s:matcher', {}],
\ 'g:ctrlp_match_window_bottom': ['s:mwbottom', 1], \ 'g:ctrlp_match_window_bottom': ['s:mwbottom', 1],
\ 'g:ctrlp_match_window_reversed': ['s:mwreverse', 1], \ 'g:ctrlp_match_window_reversed': ['s:mwreverse', 1],
\ 'g:ctrlp_max_depth': ['s:maxdepth', 40], \ 'g:ctrlp_max_depth': ['s:maxdepth', 40],
@ -314,10 +315,10 @@ fu! ctrlp#buffers()
\ 'fnamemodify(bufname(v:val), ":.")') \ 'fnamemodify(bufname(v:val), ":.")')
endf endf
" * MatchedItems() {{{1 " * MatchedItems() {{{1
fu! s:MatchIt(items, pat, limit, mfunc, ipt) fu! s:MatchIt(items, pat, limit, mfunc, ipt, exc)
let [newitems, crfile] = [[], exists('s:crfilerel') ? s:crfilerel : ''] let newitems = []
for item in a:items for item in a:items
try | if !( a:ipt && item == crfile ) && call(a:mfunc, [item, a:pat]) >= 0 try | if !( a:ipt && item == a:exc ) && call(a:mfunc, [item, a:pat]) >= 0
cal add(newitems, item) cal add(newitems, item)
en | cat | brea | endt en | cat | brea | endt
if a:limit > 0 && len(newitems) >= a:limit | brea | en if a:limit > 0 && len(newitems) >= a:limit | brea | en
@ -325,7 +326,7 @@ fu! s:MatchIt(items, pat, limit, mfunc, ipt)
retu newitems retu newitems
endf endf
fu! s:MatchedItems(items, pat, limit, ipt) fu! s:MatchedItems(items, str, pat, limit, ipt)
let [type, mfunc] = [s:type(1), 'match'] let [type, mfunc] = [s:type(1), 'match']
if s:byfname && a:ipt if s:byfname && a:ipt
let mfunc = 's:matchfname' let mfunc = 's:matchfname'
@ -333,7 +334,14 @@ fu! s:MatchedItems(items, pat, limit, ipt)
let types = { 'tabs': 's:matchtabs', 'tabe': 's:matchtabe' } let types = { 'tabs': 's:matchtabs', 'tabe': 's:matchtabe' }
if has_key(types, type) | let mfunc = types[type] | en if has_key(types, type) | let mfunc = types[type] | en
en en
let newitems = s:MatchIt(a:items, a:pat, a:limit, mfunc, a:ipt) let exc = exists('s:crfilerel') ? s:crfilerel : ''
let matfunc = 's:MatchIt'
let argms = [a:items, a:pat, a:limit, mfunc, a:ipt, exc]
if s:matcher != {} && has_key(s:matcher, 'match')
let [matfunc, argms[1], argms[3]] = s:matargs(mfunc, a:str)
let argms += [s:regexp]
en
let newitems = call(matfunc, argms)
let s:matches = len(newitems) let s:matches = len(newitems)
retu newitems retu newitems
endf endf
@ -412,7 +420,7 @@ fu! s:Update(str)
if str == oldstr && !empty(str) && !exists('s:force') | retu | en if str == oldstr && !empty(str) && !exists('s:force') | retu | en
let [pat, ipt] = [s:SplitPattern(str), s:ispathitem()] let [pat, ipt] = [s:SplitPattern(str), s:ispathitem()]
let lines = exists('g:ctrlp_nolimit') && empty(str) ? copy(g:ctrlp_lines) let lines = exists('g:ctrlp_nolimit') && empty(str) ? copy(g:ctrlp_lines)
\ : s:MatchedItems(g:ctrlp_lines, pat, s:winh, ipt) \ : s:MatchedItems(g:ctrlp_lines, str, pat, s:winh, ipt)
cal s:Render(lines, pat, ipt) cal s:Render(lines, pat, ipt)
endf endf
@ -1001,7 +1009,7 @@ fu! ctrlp#statusline()
let byfname = s:byfname ? 'file' : 'path' let byfname = s:byfname ? 'file' : 'path'
let marked = s:opmul != '0' ? let marked = s:opmul != '0' ?
\ exists('s:marked') ? ' <'.s:dismrk().'>' : ' <->' : '' \ exists('s:marked') ? ' <'.s:dismrk().'>' : ' <->' : ''
if has_key(s:status, 'main') if s:status != {} && has_key(s:status, 'main')
let args = [focus, byfname, s:regexp, prv, item, nxt, marked] let args = [focus, byfname, s:regexp, prv, item, nxt, marked]
let &l:stl = call(s:status['main'], args) let &l:stl = call(s:status['main'], args)
el el
@ -1022,7 +1030,8 @@ endf
fu! ctrlp#progress(enum) fu! ctrlp#progress(enum)
if has('macunix') || has('mac') | sl 1m | en if has('macunix') || has('mac') | sl 1m | en
let &l:stl = has_key(s:status, 'prog') ? call(s:status['prog'], [a:enum]) let &l:stl = s:status != {} && has_key(s:status, 'prog')
\ ? call(s:status['prog'], [a:enum])
\ : '%#CtrlPStats# '.a:enum.' %* %=%<%#CtrlPMode2# '.getcwd().' %*' \ : '%#CtrlPStats# '.a:enum.' %* %=%<%#CtrlPMode2# '.getcwd().' %*'
redraws redraws
endf endf
@ -1180,6 +1189,7 @@ fu! ctrlp#syntax()
endf endf
fu! s:highlight(pat, grp, ipt) fu! s:highlight(pat, grp, ipt)
if s:matcher != {} | retu | en
cal clearmatches() cal clearmatches()
if !empty(a:pat) && a:ipt if !empty(a:pat) && a:ipt
let pat = s:regexp ? substitute(a:pat, '\\\@<!\^', '^> \\zs', 'g') : a:pat let pat = s:regexp ? substitute(a:pat, '\\\@<!\^', '^> \\zs', 'g') : a:pat
@ -1342,6 +1352,16 @@ fu! s:argmaps(md, ...)
retu a:md retu a:md
endf endf
" Misc {{{2 " Misc {{{2
fu! s:matargs(mfunc, str)
let match_type = {
\ 'match': 'full-line',
\ 's:matchfname': 'filename-only',
\ 's:matchtabs': 'first-non-tab',
\ 's:matchtabe': 'until-last-tab',
\ }
retu [s:matcher['match'], a:str, match_type[a:mfunc]]
endf
fu! s:log(m) fu! s:log(m)
if exists('g:ctrlp_log') && g:ctrlp_log | if a:m if exists('g:ctrlp_log') && g:ctrlp_log | if a:m
let cadir = ctrlp#utils#cachedir() let cadir = ctrlp#utils#cachedir()

View File

@ -178,9 +178,7 @@ Note: In some terminals, its not possible to remap <c-h> without also changin
<bs> (|key-codes|). So if pressing <bs> moves the cursor to the left instead of <bs> (|key-codes|). So if pressing <bs> moves the cursor to the left instead of
deleting a char for you, add this to your |vimrc| to disable the default <c-h> deleting a char for you, add this to your |vimrc| to disable the default <c-h>
mapping: > mapping: >
let g:ctrlp_prompt_mappings = { let g:ctrlp_prompt_mappings = { 'PrtCurLeft()': ['<left>', '<c-^>'] }
\ 'PrtCurLeft()': ['<left>', '<c-^>']
\ }
< <
*'g:ctrlp_mruf_max'* *'g:ctrlp_mruf_max'*
@ -386,8 +384,75 @@ Example: >
\ 'prog': 'Function_Name_2', \ 'prog': 'Function_Name_2',
\ } \ }
< <
Structure of the functions: >
function! Function_Name_1(focus, byfname, regex, prev, item, next, marked)
" a:focus a:byfname a:regex a:prev a:item a:next a:marked
" | | | | | | |
" | | | | | | +- Number of marked files.
" | | | | | |
" | | | | | +- The next search mode.
" | | | | |
" | | | | +- The current search mode.
" | | | |
" | | | +- The previous search mode.
" | | |
" | | +- In regex mode: 1 or 0.
" | |
" | +- In filename mode or in full path mode: "file" or "path".
" |
" +- The focus of the prompt: "prt" or "win".
return full_statusline
endfunction
function! Function_Name_2(str)
" a:str
" |
" + Either a number of files scanned so far, or a string indicating the
" directory is being scanned with a user_command.
return full_statusline
endfunction
<
See https://gist.github.com/1610859 for a working example. See https://gist.github.com/1610859 for a working example.
*'g:ctrlp_match_func'*
Set an external fuzzy matching function for |CtrlP| to use: >
let g:ctrlp_match_func = {}
<
Example: >
let g:ctrlp_match_func = { 'match': 'Function_Name' }
<
Structure of the function: >
function! Function_Name(items, str, limit, mmode, ispath, crfile, regex)
" a:items a:str a:limit a:mmode a:ispath a:crfile a:regex
" | | | | | | |
" | | | | | | +- In regex mode: 1 or 0.
" | | | | | |
" | | | | | +- The file in the current window
" | | | | | Should be excluded from the
" | | | | | results when a:ispath == 1.
" | | | | |
" | | | | +- Is 1 when searching in file, buffer, mru
" | | | | dir, rtscript... modes. Is 0 otherwise.
" | | | |
" | | | +- The match mode. Can be one of these strings:
" | | | + "full-line": match the entire line.
" | | | + "filename-only": match only the filename.
" | | | + "first-non-tab": match until the first tab char.
" | | | + "until-last-tab": match until the last tab char.
" | | |
" | | +- The max height of the match window. Can be used to limit
" | | the number of items to return.
" | |
" | +- The string entered by the user.
" |
" +- The full list of items to search in (e.g. all files in a dir tree).
return list_of_matched_items
endfunction
<
=============================================================================== ===============================================================================
COMMANDS *ctrlp-commands* COMMANDS *ctrlp-commands*
@ -789,6 +854,7 @@ Special thanks:~
=============================================================================== ===============================================================================
CHANGELOG *ctrlp-changelog* CHANGELOG *ctrlp-changelog*
+ New option: |g:ctrlp_match_func|, allow using a custom fuzzy matcher.
+ Rename: + Rename:
*ClearCtrlPCache* -> |CtrlPClearCache| *ClearCtrlPCache* -> |CtrlPClearCache|
*ClearAllCtrlPCaches* -> |CtrlPClearAllCaches| *ClearAllCtrlPCaches* -> |CtrlPClearAllCaches|