From c51b7ecbfc4b8f945a113bb1d25a93868b0390dd Mon Sep 17 00:00:00 2001 From: Kien N Date: Mon, 5 Sep 2011 18:05:04 +0700 Subject: [PATCH] add find buffers --- autoload/ctrlp.vim | 70 +++++++++++++++++++++++++++------------------- doc/ctrlp.txt | 18 ++++++++---- plugin/ctrlp.vim | 3 +- readme.md | 1 + 4 files changed, 57 insertions(+), 35 deletions(-) diff --git a/autoload/ctrlp.vim b/autoload/ctrlp.vim index 2c03a4f..4fe1686 100644 --- a/autoload/ctrlp.vim +++ b/autoload/ctrlp.vim @@ -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 \ \ \ :cal \".func."(\"".escape(nr2char(each), '"|\')."\"".sjbyfname.")\" 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()': [''], \ 'ToggleRegex()': [''], \ 'ToggleByFname()': [''], + \ 'ToggleType()': [''], \ 'PrtCurStart()': [''], \ 'PrtCurEnd()': [''], \ 'PrtCurLeft()': ['', ''], @@ -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! diff --git a/doc/ctrlp.txt b/doc/ctrlp.txt index 74f049d..c66c877 100644 --- a/doc/ctrlp.txt +++ b/doc/ctrlp.txt @@ -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). Caching’s 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 you’ve changed the values (inside []): > \ 'ToggleFocus()': [''], \ 'ToggleRegex()': [''], \ 'ToggleByFname()': [''], + \ 'ToggleType()': [''], \ 'PrtCurStart()': [''], \ 'PrtCurEnd()': [''], \ 'PrtCurLeft()': ['', ''], @@ -147,7 +148,10 @@ only need to keep the lines that you’ve 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 prompt’s base is '>d>' instead of '>>>') + + Toggle between searching files and searching buffers. + , , @@ -261,8 +268,7 @@ liked the way Command-T and LustyExplorer deal with user’s 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 diff --git a/plugin/ctrlp.vim b/plugin/ctrlp.vim index cacbc7a..d4d51f2 100644 --- a/plugin/ctrlp.vim +++ b/plugin/ctrlp.vim @@ -14,7 +14,8 @@ if !exists('g:ctrlp_map') let g:ctrlp_map = '' 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() diff --git a/readme.md b/readme.md index ab12b10..daf09f7 100644 --- a/readme.md +++ b/readme.md @@ -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 `` to switch to find buffer mode. Or run `:CtrlPBuffer`. ## Basic Options