add find buffers

This commit is contained in:
Kien N 2011-09-05 18:05:04 +07:00
parent b6f15390b8
commit c51b7ecbfc
4 changed files with 57 additions and 35 deletions

View File

@ -111,39 +111,23 @@ func! s:GetDataFile(file)
endfunc
func! s:CacheDir()
let cache_dir = exists('*mkdir') ? s:cache_dir.s:lash().'.ctrlp_cache' : s:cache_dir
retu cache_dir
retu exists('*mkdir') ? s:cache_dir.s:lash().'.ctrlp_cache' : s:cache_dir
endfunc
func! s:CacheFile()
let cache_dir = s:CacheDir()
let cache_file = substitute(getcwd(), '\([\/]\|^\a\zs:\)', '%', 'g').'.txt'
retu cache_dir.s:lash().cache_file
retu s:CacheDir().s:lash().substitute(getcwd(), '\([\/]\|^\a\zs:\)', '%', 'g').'.txt'
endfunc
func! s:WriteCache(lines)
let cache_dir = s:CacheDir()
" create directory if not existed
if exists('*mkdir') && !isdirectory(cache_dir)
try
cal mkdir(cache_dir)
catch
echoh Error
ec 'Can''t create caching directory'
echoh None
endtry
sil! cal mkdir(cache_dir)
endif
" write cache
if isdirectory(cache_dir)
try
cal writefile(a:lines, s:CacheFile())
catch
echoh Error
ec 'Can''t write cache file'
echoh None
finally
let g:ctrlp_newcache = 0
endtry
sil! cal writefile(a:lines, s:CacheFile())
let g:ctrlp_newcache = 0
endif
endfunc
@ -203,6 +187,20 @@ func! s:ListAllFiles(path) "{{{
retu allfiles
endfunc "}}}
func! s:ListAllBuffers() "{{{
let nbufs = bufnr('$')
let allbufs = []
for each in range(1, nbufs)
if getbufvar(each, '&bl')
let bufname = bufname(each)
if strlen(bufname) && getbufvar(each, '&ma') && bufname != 'ControlP'
cal add(allbufs, fnamemodify(bufname, ':p'))
endif
endif
endfor
retu allbufs
endfunc "}}}
func! s:SplitPattern(str,...) "{{{
" Split into a list, ignoring spaces
if s:igspace
@ -532,6 +530,7 @@ func! s:MapKeys(...)
for each in range(32,126)
sil! exe "nn \<buffer> \<silent> \<char-".each."> :cal \<SID>".func."(\"".escape(nr2char(each), '"|\')."\"".sjbyfname.")\<cr>"
endfor
if exists('a:2') | retu | endif
" Special keystrokes
if exists('a:1') && a:1 == 0
sil! cal s:MapSpecs('unmap')
@ -555,6 +554,7 @@ func! s:MapSpecs(...)
\ 'ToggleFocus()': ['<tab>'],
\ 'ToggleRegex()': ['<c-r>'],
\ 'ToggleByFname()': ['<c-d>'],
\ 'ToggleType()': ['<c-f>'],
\ 'PrtCurStart()': ['<c-a>'],
\ 'PrtCurEnd()': ['<c-e>'],
\ 'PrtCurLeft()': ['<c-h>', '<left>'],
@ -598,7 +598,7 @@ func! s:ToggleFocus()
endfunc
"}}}
"Todo: Cycle through matches. /high
"Mightdo: Cycle through matches. /high
func! s:SelectJump(char,...) "{{{
let lines = map(b:matched, 'substitute(v:val, "^> ", "", "g")')
if exists('a:1')
@ -617,6 +617,13 @@ endfunc "}}}
func! s:ToggleByFname() "{{{
let s:byfname = s:byfname ? 0 : 1
cal s:MapKeys(s:Focus(), 1)
cal s:BuildPrompt(s:Focus())
endfunc "}}}
func! s:ToggleType() "{{{
let s:itemtype = s:itemtype ? 0 : 1
cal s:SetLines(s:itemtype)
cal s:BuildPrompt(s:Focus())
endfunc "}}}
@ -673,7 +680,8 @@ endfunc
func! s:AcceptSelection(mode) "{{{
let md = a:mode
let line = getline('.')
let filepath = getcwd().s:lash().matchstr(line, '^> \zs.\+\ze\t*$')
let matchstr = matchstr(line, '^> \zs.\+\ze\t*$')
let filepath = s:itemtype ? matchstr : getcwd().s:lash().matchstr
let filename = split(filepath, s:lash())[-1]
" Remove the prompt and match window
sil! cal s:BufOpen('ControlP', 'del')
@ -711,8 +719,7 @@ func! s:compare(s1, s2)
endfunc
func! s:matchsubstr(item, pat)
let tail = split(a:item, '[\/]\ze[^\/]\+$')[-1]
retu match(tail, a:pat)
retu match(split(a:item, '[\/]\ze[^\/]\+$')[-1], a:pat)
endfunc
func! s:matchlists(item, lst)
@ -734,16 +741,23 @@ func! s:Syntax()
endfunc
"}}}
func! ctrlp#init() "{{{
" Initialization {{{
func! s:SetLines(type)
let s:itemtype = a:type
let s:lines = s:itemtype ? s:ListAllBuffers() : s:ListAllFiles(getcwd())
endfunc
func! ctrlp#init(type)
cal ctrlp#SetWorkingPath()
let s:lines = s:ListAllFiles(getcwd())
cal s:SetLines(a:type)
cal s:BufOpen('ControlP')
cal s:SetupBlank('ctrlp')
cal s:MapKeys()
cal s:Renderer(s:lines)
cal s:BuildPrompt()
cal s:Syntax()
endfunc "}}}
endfunc
"}}}
aug CtrlPAug "{{{
au!

View File

@ -107,13 +107,13 @@ Set the parent directory for the '.ctrlp_cache' directory: >
<
*'g:ctrlp_use_caching'*
Set this to 1 to always enable caching. Caching is also temporarily enabled if
the working directory has more than 4000 files: >
Set this to 1 to always enable caching (flat files). Cachings also temporarily
enabled if the working directory has more than 4000 files: >
let g:ctrlp_use_caching = 0
<
*'g:ctrlp_by_filename'*
Set this to 1 to set search by-file-name as the default: >
Set this to 1 to set search by file-name (not full path) as the default: >
let g:ctrlp_by_filename = 0
<
@ -134,6 +134,7 @@ only need to keep the lines that youve changed the values (inside []): >
\ 'ToggleFocus()': ['<tab>'],
\ 'ToggleRegex()': ['<c-r>'],
\ 'ToggleByFname()': ['<c-d>'],
\ 'ToggleType()': ['<c-f>'],
\ 'PrtCurStart()': ['<c-a>'],
\ 'PrtCurEnd()': ['<c-e>'],
\ 'PrtCurLeft()': ['<c-h>', '<left>'],
@ -147,7 +148,10 @@ only need to keep the lines that youve changed the values (inside []): >
*:CtrlP*
:CtrlP
Open the |CtrlP| prompt.
Open the |CtrlP| prompt in find file mode.
*:CtrlPBuffer*
:CtrlPBuffer
Open the |CtrlP| prompt in find buffer mode.
*:ClearCtrlPCache*
:ClearCtrlPCache
@ -177,6 +181,9 @@ Once inside the prompt:
Toggle between full path search and file name only search.
(note: in file name mode, the prompts base is '>d>' instead of '>>>')
<c-f>
Toggle between searching files and searching buffers.
<c-n>,
<c-j>,
<down>
@ -261,8 +268,7 @@ liked the way Command-T and LustyExplorer deal with users input, so I
wrote a pure Vimscript version of their prompt window, intended to use it for
the aforementioned plugin.
Git repository: https://github.com/kien/ctrlp.vim
Mercurial repository: https://bitbucket.org/kien/ctrlp.vim
Git repository: https://github.com/kien/ctrlp.vim
===============================================================================
vim:nofen:et:ts=2:sw=2:sts=2

View File

@ -14,7 +14,8 @@ if !exists('g:ctrlp_map')
let g:ctrlp_map = '<c-p>'
endif
com! CtrlP cal ctrlp#init()
com! CtrlP cal ctrlp#init(0)
com! CtrlPBuffer cal ctrlp#init(1)
com! ClearCtrlPCache cal ctrlp#clearcache()
com! ClearAllCtrlPCaches cal ctrlp#clearallcaches()

View File

@ -16,6 +16,7 @@ back to full path search.
line in the selected file.
e.g. `abc:45` to open the file matched the pattern `abc` and jump to
line 45.
* Press `<c-f>` to switch to find buffer mode. Or run `:CtrlPBuffer`.
## Basic Options