Skip to content

Add extension 'BufFilter' #589

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions autoload/ctrlp.vim
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ let [s:lcmap, s:prtmaps] = ['nn <buffer> <silent>', {
\ 'AcceptSelection("t")': ['<c-t>'],
\ 'AcceptSelection("v")': ['<c-v>', '<RightMouse>'],
\ 'ToggleFocus()': ['<s-tab>'],
\ 'PrtInputItem()': ['<c-i>'],
\ 'ToggleRegex()': ['<c-r>'],
\ 'ToggleByFname()': ['<c-d>'],
\ 'ToggleType(1)': ['<c-f>', '<c-up>'],
Expand Down Expand Up @@ -1137,6 +1138,14 @@ fu! s:ToggleFocus()
let s:focus = !s:focus
cal s:BuildPrompt(0)
endf
function! s:PrtInputItem()
" I don't know how to remeber old mappings
cmap <silent><c-space> <cr>
let oldText = s:prompt[0]
let str = input(oldText . ' ')
call s:PrtAdd((len(oldText) ? ' ' : '') . str)
cunmap <c-space>
endfunction

fu! s:ToggleRegex()
let s:regexp = !s:regexp
Expand Down Expand Up @@ -1988,9 +1997,11 @@ fu! ctrlp#syntax()
endf

fu! s:highlight(pat, grp)
if s:matcher != {} | retu | en
if s:matcher != {} && !has_key(s:matcher, 'highlight')| retu | en
cal clearmatches()
if !empty(a:pat) && s:ispath
if !empty(a:pat) && s:matcher != {} " user-defined highlights
call call(s:matcher['highlight'], [a:pat, a:grp])
elseif !empty(a:pat) && s:ispath
if s:regexp
let pat = substitute(a:pat, '\\\@<!\^', '^> \\zs', 'g')
cal matchadd(a:grp, ( s:martcs == '' ? '\c' : '\C' ).pat)
Expand Down
104 changes: 104 additions & 0 deletions autoload/ctrlp/buffilter.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
" =============================================================================
" File: autoload/ctrlp/buffilter.vim
" Description: BufFilter extension
" modified based on CtrlPLine
" Author: Troy Daniel <github.com/TroyDanielFZ>
" =============================================================================

" Init {{{1
if exists('g:loaded_ctrlp_buffilter') && g:loaded_ctrlp_buffilter
fini
en
let g:loaded_ctrlp_buffilter = 1

cal add(g:ctrlp_ext_vars, {
\ 'init': 'ctrlp#buffilter#init(s:crbufnr)',
\ 'accept': 'ctrlp#buffilter#accept',
\ 'exit': 'ctrlp#buffilter#exit()',
\ 'lname': 'lines',
\ 'sname': 'lns',
\ 'sort': 0,
\ 'type': 'tabe',
\ })

let s:id = g:ctrlp_builtins + len(g:ctrlp_ext_vars)
" Utilities {{{1
fu! s:syntax()
if !ctrlp#nosy()
cal ctrlp#hicheck('CtrlPBufName', 'Directory')
cal ctrlp#hicheck('CtrlPTabExtra', 'Comment')
sy match CtrlPBufName '\t|\zs[^|]\+\ze|\d\+:\d\+|$'
sy match CtrlPTabExtra '\zs\t.*\ze$' contains=CtrlPBufName
highlight link CtrlPMatch DiffChange
en
endf
" Public {{{1
" use buffilter only in buffilter mode
fu! ctrlp#buffilter#exit()
unlet g:ctrlp_match_func
endf

fu! ctrlp#buffilter#init(bufnr)
let [lines, bufnr] = [[], a:bufnr]
let bufs = exists('s:lnmode') && s:lnmode ? ctrlp#buffers('id') : [bufnr]
" Load only the current buffer
let [lfb, bufn] = [getbufline(bufnr, 1, '$'), bufname(bufnr)]
if lfb == [] && bufn != ''
let lfb = ctrlp#utils#readfile(fnamemodify(bufn, ':p'))
en
cal map(lfb, 'tr(v:val, '' '', '' '')')
let [linenr, len_lfb] = [1, len(lfb)]
let buft = bufn == '' ? '[No Name]' : fnamemodify(bufn, ':t')
wh linenr <= len_lfb
let lfb[linenr - 1] .= ' |'.buft.'|'.bufnr.':'.linenr.'|'
let linenr += 1
endw
cal extend(lines, filter(lfb, 'v:val !~ ''^\s*\t|[^|]\+|\d\+:\d\+|$'''))
cal s:syntax()
retu lines
endf

fu! ctrlp#buffilter#accept(mode, str)
let info = matchlist(a:str, '\t|[^|]\+|\(\d\+\):\(\d\+\)|$')
let bufnr = str2nr(get(info, 1))
if bufnr
cal ctrlp#acceptfile('Et', bufnr, get(info, 2)) " jump to buffer,
" cal ctrlp#acceptfile(a:mode, bufnr, get(info, 2))
en
endf

fu! ctrlp#buffilter#cmd(mode, ...)
let s:lnmode = a:mode
if a:0 && !empty(a:1)
let s:lnmode = 0
let bname = a:1 =~# '^%$\|^#\d*$' ? expand(a:1) : a:1
let s:bufnr = bufnr('^'.fnamemodify(bname, ':p').'$')
en

let g:ctrlp_match_func = { 'match': "ctrlp#buffilter#matcher",
\ "highlight": 'ctrlp#buffilter#patterner'}
" let g:ctrlp_pattern_func = [1, 'ctrlp#buffilter#patterner']
retu s:id
endf
"}}}

function! ctrlp#buffilter#patterner(str, grp)
echom "ctrlp#buffilter#patterner: " . a:str
let patterns=split(tolower(a:str), '\s\+', 0)
let searchPattern = join(map(patterns, 'SearchEscape(v:val)'), '\|')
cal matchadd(a:grp, searchPattern)
" return searchPattern
endfunction

function! ctrlp#buffilter#matcher(items, str, limit, mmode, ispath, crfile, regex)
echo "ctrlp#buffilter#matcher"
let items=copy(a:items)
let patterns=split(tolower(a:str), '\s\+', 0)
for p in patterns
call filter(items, 'stridx(tolower(v:val), p) >=0 ')
endfor
echo []+items
return []+items
endfunction

" vim:fen:fdm=marker:fmr={{{,}}}:fdl=0:fdc=1:ts=2:sw=2:sts=2
12 changes: 12 additions & 0 deletions doc/ctrlp.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1213,6 +1213,18 @@ Available extensions:~
- Name: 'line'
- Command: ":CtrlPLine [buffer]"
- Search for a line in all listed buffers or in the specified [buffer].

*:CtrlPBufFilter*
* BufFilter mode: ~
- Name: 'buffilter'
- Command: ":CtrlPBufFilter"
- This mode is quite different to the Line mode. In this mode, you can type
a few keywords splitted by space, then only the lines contains all the
keywords will show up. No fuzzy search.
In addition, you can press <c-i>, and then you'll be prompt for
keywods, after type the keywords, you can type <c-i> again (or <cr>
of coursr)to return to the filter mode. This mode is speically useful for
those who need non-ascii input, or paste from keyboard.

*:CtrlPChange*
*:CtrlPChangeAll*
Expand Down
2 changes: 2 additions & 0 deletions plugin/ctrlp.vim
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ com! -bar CtrlPUndo cal ctrlp#init(ctrlp#undo#id())
com! -n=? -com=buffer CtrlPLine
\ cal ctrlp#init(ctrlp#line#cmd('buf', <q-args>))

com! -n=? -com=buffer CtrlPBufFilter
\ cal ctrlp#init(ctrlp#buffilter#cmd(1, <q-args>))
com! -n=? -com=buffer CtrlPChange
\ cal ctrlp#init(ctrlp#changes#cmd('fil', <q-args>))

Expand Down