Skip to content

Commit

Permalink
Add minimum implementation for multiple buffer support
Browse files Browse the repository at this point in the history
  • Loading branch information
tacahiroy committed Feb 8, 2015
1 parent bd710ba commit bb87f80
Showing 1 changed file with 53 additions and 19 deletions.
72 changes: 53 additions & 19 deletions autoload/ctrlp/funky.vim
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,17 @@ endfunction
" TODO: some functions should be defined under ctrlp#funky#utils namespace
function! s:syntax(filetype)
if !ctrlp#nosy()
call ctrlp#hicheck('CtrlPTabExtra', 'Comment')
syn match CtrlPTabExtra '\t#.*:\d\+:\d\+$'

for [k,v] in items(s:custom_hl_list)
call ctrlp#hicheck(k, v.to_group)
execute printf('syn match %s "%s"', k, v.pat)
endfor

if s:syntax_highlight | let &filetype = a:filetype | endif
if !s:is_multi_buffers && s:syntax_highlight
let &filetype = a:filetype
endif

call ctrlp#hicheck('CtrlPTabExtra', 'Comment')
syn match CtrlPTabExtra '\t#.*:\d\+:\d\+$'
endif
endfunction

Expand All @@ -86,6 +88,15 @@ function! s:error(msg)
let v:errmsg = a:msg
endfunction

function! s:load_buffer_by_name(bufnr)
" if !bufloaded(a:bufnr) | execute 'keepalt buffer ' . bufname(a:bufnr) | endif
execute 'keepalt buffer ' . bufname(a:bufnr)
endfunction

function! s:load_buffer_by_number(bufnr)
execute 'keepalt buffer ' . a:bufnr
endfunction

function! s:filetype(bufnr)
return getbufvar(a:bufnr, '&l:filetype')
endfunction
Expand Down Expand Up @@ -153,34 +164,48 @@ function! s:after_jump()
endfunction
" }}}

" Provide a list of strings to search in
" Provides a list of strings to search in
"
" Return: List
" FIXME: refactoring
function! ctrlp#funky#init(bufnr)
" ControlP buffer is active when this function is invoked
try
" NOTE: To prevent ctrlp error. this is a bug on ctrlp itself, perhaps?
let saved_ei = &eventignore
let &eventignore = 'BufLeave'

let ctrlp_winnr = bufwinnr(bufnr(''))
execute bufwinnr(a:bufnr) . 'wincmd w'
let pos = getpos('.')
let filetype = s:filetype(a:bufnr)

if s:is_multi_buffers
let bufs = map(ctrlp#buffers(), 'bufnr(v:val)')
else
let bufs = [a:bufnr]
endif

let candidates = []
for ft in split(filetype, '\.')
if s:has_filter(ft)
let filters = s:filters_by_filetype(ft, a:bufnr)
let st = reltime()
let candidates += ctrlp#funky#extract(a:bufnr, filters)
call s:fu.debug('Extract: ' . reltimestr(reltime(st)))
if s:has_post_extract_hook(ft)
call ctrlp#funky#ft#{ft}#post_extract_hook(candidates)
for bufnr in bufs
call s:load_buffer_by_name(bufnr)
let filetype = s:filetype(bufnr)
for ft in split(filetype, '\.')
if s:has_filter(ft)
let filters = s:filters_by_filetype(ft, bufnr)
let st = reltime()
let candidates += ctrlp#funky#extract(bufnr, filters)
call s:fu.debug('Extract: ' . reltimestr(reltime(st)))
if s:has_post_extract_hook(ft)
call ctrlp#funky#ft#{ft}#post_extract_hook(candidates)
endif
elseif s:report_filter_error
echoerr printf('%s: filters not exist', ft)
endif
elseif s:report_filter_error
echoerr printf('%s: filters not exist', ft)
endif
endfor
endfor

" activate the former buffer
execute 'buffer ' . bufname(a:bufnr)
call setpos('.', pos)

execute ctrlp_winnr . 'wincmd w'
Expand Down Expand Up @@ -208,7 +233,7 @@ function! ctrlp#funky#funky(word)
endtry
endfunction

" todo: needs to improved. 'if s:sort_by_mru' too much
" TODO: this fat function needs to be improved. 'if s:sort_by_mru' too much
function! ctrlp#funky#extract(bufnr, patterns)
try
let candidates = []
Expand Down Expand Up @@ -237,6 +262,10 @@ function! ctrlp#funky#extract(bufnr, patterns)
return ca
endif

"
" no cache mode is from here
"

execute bufwinnr(a:bufnr) . 'wincmd w'

let mru = []
Expand Down Expand Up @@ -320,6 +349,7 @@ function! ctrlp#funky#accept(mode, str)
" should be current window = former window
let lnum = matchstr(a:str, '\d\+$')
execute 'noautocmd ' . get(s:, 'winnr', 1) . 'wincmd w'
call s:load_buffer_by_number(bufnr)
call cursor(lnum, 1)

call s:after_jump()
Expand Down Expand Up @@ -353,10 +383,13 @@ endfunction
""
" Configuration
"
let g:ctrlp#funky#is_debug = get(g:, 'ctrlp_funky_debug', 0)

let s:errmsg = ''
let s:custom_hl_list = {}

let g:ctrlp#funky#is_debug = get(g:, 'ctrlp_funky_debug', 0)
let s:is_multi_buffers = get(g:, 'ctrlp_funky_multi_buffers', 0)

let s:report_filter_error = get(g:, 'ctrlp_funky_report_filter_error', 0)
let s:sort_by_mru = get(g:, 'ctrlp_funky_sort_by_mru', 0)
" after jump action
Expand Down Expand Up @@ -391,6 +424,7 @@ if s:use_cache
endif
endif
call s:fu.debug('INFO: use_cache? ' . (s:use_cache ? 'TRUE' : 'FALSE'))
call s:fu.debug('INFO: mutli_buffers? ' . (s:is_multi_buffers ? 'TRUE' : 'FALSE'))

" The main variable for this extension.
"
Expand Down

0 comments on commit bb87f80

Please sign in to comment.