Improve Open Multiple Files

Try not to open a new tab when using <c-o> to open marked files.
Option to move the match window to top of screen.
This commit is contained in:
Kien N 2011-10-09 09:30:16 +07:00
parent 000742dc0f
commit 6ee4410612
4 changed files with 48 additions and 47 deletions

View File

@ -3,7 +3,7 @@
" Description: Full path fuzzy file, buffer and MRU file finder for Vim. " Description: Full path fuzzy file, buffer and MRU file finder for Vim.
" Author: Kien Nguyen <github.com/kien> " Author: Kien Nguyen <github.com/kien>
" License: MIT " License: MIT
" Version: 1.5.1 " Version: 1.5.2
" ============================================================================= " =============================================================================
if v:version < '700' "{{{ if v:version < '700' "{{{
@ -14,6 +14,7 @@ endif "}}}
func! s:opts() func! s:opts()
let opts = { let opts = {
\ 'g:ctrlp_match_window_reversed' : ['s:mwreverse', 1], \ 'g:ctrlp_match_window_reversed' : ['s:mwreverse', 1],
\ 'g:ctrlp_match_window_bottom' : ['s:mwbottom', 1],
\ 'g:ctrlp_split_window' : ['s:splitwin', 0], \ 'g:ctrlp_split_window' : ['s:splitwin', 0],
\ 'g:ctrlp_working_path_mode' : ['s:pathmode', 1], \ 'g:ctrlp_working_path_mode' : ['s:pathmode', 1],
\ 'g:ctrlp_root_markers' : ['s:rmarkers', []], \ 'g:ctrlp_root_markers' : ['s:rmarkers', []],
@ -61,9 +62,7 @@ cal s:opts()
let s:lash = ctrlp#utils#lash() let s:lash = ctrlp#utils#lash()
" Limiters " Limiters
let s:compare_lim = 3000 let [s:compare_lim, s:nocache_lim, s:mltipats_lim] = [3000, 4000, 2000]
let s:nocache_lim = 4000
let s:mltipats_lim = 2000
"}}} "}}}
" * Clear caches {{{ " * Clear caches {{{
@ -112,11 +111,13 @@ func! s:GlobPath(dirs, allfiles, depth)
endfunc endfunc
func! s:UserCommand(path, lscmd) func! s:UserCommand(path, lscmd)
let path = a:path
if exists('+ssl') && &ssl if exists('+ssl') && &ssl
let ssl = &ssl let ssl = &ssl
let &ssl = 0 let &ssl = 0
let path = substitute(path, '/', '\', 'g')
endif endif
let g:ctrlp_allfiles = split(system(printf(a:lscmd, shellescape(a:path))), '\n') let g:ctrlp_allfiles = split(system(printf(a:lscmd, shellescape(path))), '\n')
if exists('+ssl') && exists('ssl') if exists('+ssl') && exists('ssl')
let &ssl = ssl let &ssl = ssl
cal map(g:ctrlp_allfiles, 'substitute(v:val, "\\", "/", "g")') cal map(g:ctrlp_allfiles, 'substitute(v:val, "\\", "/", "g")')
@ -216,9 +217,7 @@ endfunc "}}}
" * GetMatchedItems {{{ " * GetMatchedItems {{{
func! s:MatchIt(items, pat, limit) func! s:MatchIt(items, pat, limit)
let items = a:items let [items, pat, limit] = [a:items, a:pat, a:limit]
let pat = a:pat
let limit = a:limit
let newitems = [] let newitems = []
for item in items for item in items
if s:byfname if s:byfname
@ -233,9 +232,7 @@ func! s:MatchIt(items, pat, limit)
endfunc endfunc
func! s:GetMatchedItems(items, pats, limit) func! s:GetMatchedItems(items, pats, limit)
let items = a:items let [items, pats, limit] = [a:items, a:pats, a:limit]
let pats = a:pats
let limit = a:limit
" If items is longer than s:mltipats_lim, use only the last pattern " If items is longer than s:mltipats_lim, use only the last pattern
if len(items) >= s:mltipats_lim if len(items) >= s:mltipats_lim
let pats = [pats[-1]] let pats = [pats[-1]]
@ -293,9 +290,10 @@ func! s:BufOpen(...) "{{{
cal s:recordhist(prt[0] . prt[1] . prt[2]) cal s:recordhist(prt[0] . prt[1] . prt[2])
ec ec
else else
let s:currwin = winnr()
" Open new buffer " Open new buffer
sil! exe 'bo 1new' a:1 let pos = s:mwbottom ? 'bo' : 'to'
sil! exe pos '1new' a:1
let s:currwin = s:mwbottom ? winnr('#') : winnr('#') + 1
abc <buffer> abc <buffer>
let s:winnr = bufwinnr('%') let s:winnr = bufwinnr('%')
let s:bufnr = bufnr('%') let s:bufnr = bufnr('%')
@ -431,7 +429,7 @@ func! s:CreateNewFile() "{{{
let arr = split(str, '[\/]') let arr = split(str, '[\/]')
cal map(arr, 'escape(v:val, "%#")') cal map(arr, 'escape(v:val, "%#")')
let fname = remove(arr, -1) let fname = remove(arr, -1)
clo! exe s:currwin.'winc w'
if s:newfop == 1 " In new tab if s:newfop == 1 " In new tab
tabnew tabnew
let cmd = 'e' let cmd = 'e'
@ -489,17 +487,15 @@ func! s:OpenMulti()
retu retu
endif endif
let marked = deepcopy(s:marked) let marked = deepcopy(s:marked)
let prt = g:CtrlP_prompt
let str = prt[0] . prt[1] . prt[2]
if !has('autocmd') | cal s:BufOpen('ControlP', 'del') | endif if !has('autocmd') | cal s:BufOpen('ControlP', 'del') | endif
exe s:currwin.'winc w' exe s:currwin.'winc w'
" Try not to open in new tab " Try not to open in new tab
let ntab = 0 let ntab = 0
let norbufs = s:normbuf() let norwins = s:normbuf()
if empty(norbufs) if empty(norwins)
let ntab = 1 let ntab = 1
else else
for each in norbufs for each in norwins
let bufnr = winbufnr(each) let bufnr = winbufnr(each)
if !empty(bufname(bufnr)) && !empty(getbufvar(bufnr, '&ft')) if !empty(bufname(bufnr)) && !empty(getbufvar(bufnr, '&ft'))
\ && bufname(bufnr) != 'ControlP' \ && bufname(bufnr) != 'ControlP'
@ -507,7 +503,7 @@ func! s:OpenMulti()
endif endif
endfor endfor
if !ntab if !ntab
let wnr = min(norbufs) let wnr = min(norwins)
endif endif
endif endif
if ntab | tabnew | endif if ntab | tabnew | endif
@ -653,7 +649,7 @@ func! s:PrtClearCache()
endfunc endfunc
func! s:PrtExit() func! s:PrtExit()
if has('autocmd') && s:currwin if has('autocmd')
exe s:currwin.'winc w' exe s:currwin.'winc w'
else else
cal s:BufOpen('ControlP', 'del') cal s:BufOpen('ControlP', 'del')
@ -810,9 +806,7 @@ func! s:FindRoot(curr, mark, depth, ...)
endif endif
else else
let parent = substitute(a:curr, '[\/]\zs[^\/]\+[\/]\?$', '', '') let parent = substitute(a:curr, '[\/]\zs[^\/]\+[\/]\?$', '', '')
if parent != a:curr if parent != a:curr | cal s:FindRoot(parent, a:mark, depth, a:1) | endif
cal s:FindRoot(parent, a:mark, depth, a:1)
endif
endif endif
endfunc endfunc
@ -836,10 +830,10 @@ func! ctrlp#SetWorkingPath(...)
if s:pathmode == 1 || l:pathmode == 1 | retu | endif if s:pathmode == 1 || l:pathmode == 1 | retu | endif
let markers = [ let markers = [
\ 'root.dir', \ 'root.dir',
\ '.vimprojects',
\ '.git/', \ '.git/',
\ '_darcs/',
\ '.hg/', \ '.hg/',
\ '.vimprojects',
\ '_darcs/',
\ '.bzr/', \ '.bzr/',
\ ] \ ]
if exists('s:rmarkers') && type(s:rmarkers) == 3 && !empty(s:rmarkers) if exists('s:rmarkers') && type(s:rmarkers) == 3 && !empty(s:rmarkers)
@ -857,8 +851,8 @@ func! s:AcceptSelection(mode,...) "{{{
let prt = g:CtrlP_prompt let prt = g:CtrlP_prompt
let str = prt[0] . prt[1] . prt[2] let str = prt[0] . prt[1] . prt[2]
if md == 'e' && !s:itemtype if md == 'e' && !s:itemtype
" Walk backward the dir tree
if str == '..' if str == '..'
" Walk backward the dir tree
cal s:parentdir(getcwd()) cal s:parentdir(getcwd())
cal s:SetLines(s:itemtype) cal s:SetLines(s:itemtype)
cal s:PrtClear() cal s:PrtClear()
@ -879,11 +873,11 @@ func! s:AcceptSelection(mode,...) "{{{
if exists('a:1') && a:1 | retu filpath | endif if exists('a:1') && a:1 | retu filpath | endif
" Manually remove the prompt and match window " Manually remove the prompt and match window
if !has('autocmd') | cal s:BufOpen('ControlP', 'del') | endif if !has('autocmd') | cal s:BufOpen('ControlP', 'del') | endif
exe s:currwin.'winc w'
let bufnum = bufnr(filpath) let bufnum = bufnr(filpath)
let bufwinnr = bufwinnr(bufnum) let bufwinnr = bufwinnr(bufnum)
let norbufs = s:normbuf() let norwins = s:normbuf()
let norbuf = empty(norbufs) ? 0 : norbufs[0] let norwin = empty(norwins) ? 0 : norwins[0]
exe s:currwin.'winc w'
" Check if the file's already opened in a tab " Check if the file's already opened in a tab
for nr in range(1, tabpagenr('$')) for nr in range(1, tabpagenr('$'))
" Get a list of the buffers in the nr tab " Get a list of the buffers in the nr tab
@ -927,11 +921,11 @@ func! s:AcceptSelection(mode,...) "{{{
elseif md == 'e' || !s:splitwin " In current window elseif md == 'e' || !s:splitwin " In current window
let cmd = 'e' let cmd = 'e'
" If there's at least 1 normal buffer " If there's at least 1 normal buffer
if norbuf if norwin
" But not the current one " But not the current one
if !&l:bl || !empty(&l:bt) || !&l:ma if !&l:bl || !empty(&l:bt) || !&l:ma
" Go to the first normal one " Go to the first normal one
exe norbuf.'winc w' exe norwin.'winc w'
endif endif
else else
" No normal buffers " No normal buffers
@ -951,8 +945,7 @@ endfunc "}}}
" Sorting {{{ " Sorting {{{
func! s:complen(s1, s2) func! s:complen(s1, s2)
" By length " By length
let len1 = strlen(a:s1) let [len1, len2] = [strlen(a:s1), strlen(a:s2)]
let len2 = strlen(a:s2)
retu len1 == len2 ? 0 : len1 > len2 ? 1 : -1 retu len1 == len2 ? 0 : len1 > len2 ? 1 : -1
endfunc endfunc
@ -972,8 +965,7 @@ endfunc
func! s:comptime(s1, s2) func! s:comptime(s1, s2)
" By last modified time " By last modified time
let time1 = getftime(a:s1) let [time1, time2] = [getftime(a:s1), getftime(a:s2)]
let time2 = getftime(a:s2)
retu time1 == time2 ? 0 : time1 < time2 ? 1 : -1 retu time1 == time2 ? 0 : time1 < time2 ? 1 : -1
endfunc endfunc
@ -1049,8 +1041,7 @@ func! s:statusline(...)
endfunc endfunc
func! s:progress(len) func! s:progress(len)
let cnt = '%#Function# '.a:len.' %*' let [cnt, dir] = ['%#Function# '.a:len.' %*', ' %=%<%#LineNr# '.getcwd().' %*']
let dir = ' %=%<%#LineNr# '.getcwd().' %*'
let &l:stl = cnt.dir let &l:stl = cnt.dir
redr redr
endfunc endfunc
@ -1205,15 +1196,15 @@ endfunc
" Buffers {{{ " Buffers {{{
func! s:normbuf() func! s:normbuf()
let bufs = [] let winnrs = []
for each in range(1, winnr('$')) for each in range(1, winnr('$'))
let bufnr = winbufnr(each) let bufnr = winbufnr(each)
if getbufvar(bufnr, '&bl') && empty(getbufvar(bufnr, '&bt')) if getbufvar(bufnr, '&bl') && empty(getbufvar(bufnr, '&bt'))
\ && getbufvar(bufnr, '&ma') \ && getbufvar(bufnr, '&ma')
cal add(bufs, each) cal add(winnrs, each)
endif endif
endfor endfor
retu bufs retu winnrs
endfunc endfunc
func! s:setupblank() func! s:setupblank()
@ -1327,8 +1318,7 @@ endfunc
func! ctrlp#init(type, ...) func! ctrlp#init(type, ...)
if exists('s:init') | retu | endif if exists('s:init') | retu | endif
let s:matches = 1 let [s:matches, s:init] = [1, 1]
let s:init = 1
let a1 = exists('a:1') ? a:1 : '' let a1 = exists('a:1') ? a:1 : ''
cal ctrlp#SetWorkingPath(a1) cal ctrlp#SetWorkingPath(a1)
cal s:BufOpen('ControlP') cal s:BufOpen('ControlP')

View File

@ -1,4 +1,4 @@
*ctrlp.txt* Full path fuzzy file, buffer and MRU file finder. v1.5.1 *ctrlp.txt* Full path fuzzy file, buffer and MRU file finder. v1.5.2
*CtrlP* *ControlP* *'ctrlp'* *'ctrl-p'* *CtrlP* *ControlP* *'ctrlp'* *'ctrl-p'*
=============================================================================== ===============================================================================
# # # #
@ -55,6 +55,11 @@ Set this to 1 to set full |regexp| search as the default mode: >
let g:ctrlp_regexp_search = 0 let g:ctrlp_regexp_search = 0
< <
*'g:ctrlp_match_window_bottom'*
Set this to 0 to show the match window at the top of the screen: >
let g:ctrlp_match_window_bottom = 1
<
*'g:ctrlp_match_window_reversed'* *'g:ctrlp_match_window_reversed'*
Reverse the sort order of the matched files in the match window. The default Reverse the sort order of the matched files in the match window. The default
setting is from bottom to top: > setting is from bottom to top: >

View File

@ -31,5 +31,4 @@ exe 'nn <silent>' g:ctrlp_map ':<c-u>CtrlP<cr>'
if g:ctrlp_mru_files | cal ctrlp#mrufiles#init() | endif if g:ctrlp_mru_files | cal ctrlp#mrufiles#init() | endif
let g:ctrlp_lines = [] let [g:ctrlp_lines, g:ctrlp_allfiles] = [[], []]
let g:ctrlp_allfiles = []

View File

@ -21,7 +21,7 @@ Once CtrlP is open:
e.g. `abc:45` will open the file matched the pattern and jump to line 45. e.g. `abc:45` will open the file matched the pattern and jump to line 45.
* Submit two dots `..` as the input string to go backward the directory tree by 1 level. * Submit two dots `..` as the input string to go backward the directory tree by 1 level.
* Use `<c-y>` to create a new file and its parent dirs. * Use `<c-y>` to create a new file and its parent dirs.
* Use `<c-z>` to mark multiple files and `<c-o>` to open them. * Use `<c-z>` to mark/unmark files and `<c-o>` to open them.
## Basic Options ## Basic Options
* Change the mapping to invoke CtrlP: * Change the mapping to invoke CtrlP:
@ -69,6 +69,13 @@ e.g. Just have something like this in your vimrc:
set wildignore+=.git\*,.hg\*,.svn\* " for Windows set wildignore+=.git\*,.hg\*,.svn\* " for Windows
``` ```
* Use a custom file listing command with:
```vim
let g:ctrlp_user_command = 'find %s -type f' " MacOSX/Linux
let g:ctrlp_user_command = 'dir %s /-n /b /s /a-d' " Windows
```
_Check [the docs][2] for more mappings, commands and options._ _Check [the docs][2] for more mappings, commands and options._
[1]: http://i.imgur.com/3rtLt.png [1]: http://i.imgur.com/3rtLt.png