Skip to content

Commit

Permalink
Smarter OpenMulti() action
Browse files Browse the repository at this point in the history
  • Loading branch information
kien committed Oct 8, 2011
1 parent c8d02c5 commit b7d87e1
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 34 deletions.
86 changes: 52 additions & 34 deletions autoload/ctrlp.vim
Expand Up @@ -255,11 +255,7 @@ endfunc
func! s:BufOpen(...) "{{{
if exists('a:2')
" Closing
try
bun!
catch
winc c
endtry
try | bun! | catch | clo! | endtry
exe s:currwin.'winc w'
" Restore the changed global options
let &magic = s:CtrlP_magic
Expand Down Expand Up @@ -427,7 +423,7 @@ func! s:CreateNewFile() "{{{
let arr = split(str, '[\/]')
cal map(arr, 'escape(v:val, "%#")')
let fname = remove(arr, -1)
winc c
clo!
if s:newfop == 1 " In new tab
tabnew
let cmd = 'e'
Expand Down Expand Up @@ -490,34 +486,36 @@ func! s:OpenMulti()
if !has('autocmd') | cal s:BufOpen('ControlP', 'del') | endif
exe s:currwin.'winc w'
" Try not to open in new tab
let bufs = []
for winnr in range(1, winnr('$'))
cal add(bufs, winbufnr(winnr))
endfor
let ntab = 1
" Check if the other window only has a blank buffer
if len(bufs) == 1
for each in bufs
if getbufvar(each, '&bl') && empty(bufname(each))
\ && empty(getbufvar(each, '&bt')) && empty(getbufvar(each, '&ft'))
\ && getbufvar(each, '&ma') && bufname(each) != 'ControlP'
" If it does, don't open new tab
let ntab = 0
let ntab = 0
let norbufs = s:normbuf()
if empty(norbufs)
let ntab = 1
else
for each in norbufs
let bufnr = winbufnr(each)
if !empty(bufname(bufnr)) && !empty(getbufvar(bufnr, '&ft'))
\ && bufname(bufnr) != 'ControlP'
let ntab = 1
endif
endfor
if !ntab
let wnr = min(norbufs)
endif
endif
if ntab | tabnew | endif
let ic = 1
let wnr = exists('wnr') ? wnr : 1
exe wnr.'winc w'
for key in keys(marked)
let filpath = marked[key]
sil! exe 'bo vne' filpath
let cmd = ic == 1 ? 'e ' : 'vne '
sil! exe cmd.filpath
if s:opmul > 1 && s:opmul < ic
winc c
clo!
else
let ic += 1
endif
endfor
1winc w | winc c
ec
endfunc
"}}}
Expand Down Expand Up @@ -844,12 +842,20 @@ func! s:AcceptSelection(mode,...) "{{{
let md = a:mode
let prt = g:CtrlP_prompt
let str = prt[0] . prt[1] . prt[2]
" Walk backward the dir tree
if md == 'e' && !s:itemtype && str == '..'
cal s:parentdir(getcwd())
cal s:SetLines(s:itemtype)
cal s:PrtClear()
retu
if md == 'e' && !s:itemtype
" Walk backward the dir tree
if str == '..'
cal s:parentdir(getcwd())
cal s:SetLines(s:itemtype)
cal s:PrtClear()
retu
elseif str == '?'
" Use ? for help
exe s:currwin.'winc w'
let hlpwin = &columns > 159 ? '| vert res 80' : ''
exe 'bo vert h ctrlp-mappings' hlpwin '| norm! 0'
retu
endif
endif
" Get the full path
let matchstr = matchstr(getline('.'), '^> \zs.\+\ze\t*$')
Expand All @@ -859,9 +865,10 @@ func! s:AcceptSelection(mode,...) "{{{
if exists('a:1') && a:1 | retu filpath | endif
" Manually remove the prompt and match window
if !has('autocmd') | cal s:BufOpen('ControlP', 'del') | endif
let bufnum = bufnr(filpath)
let bufnum = bufnr(filpath)
let bufwinnr = bufwinnr(bufnum)
let norbuf = s:normbuf()
let norbufs = s:normbuf()
let norbuf = empty(norbufs) ? 0 : norbufs[0]
exe s:currwin.'winc w'
" Check if the file's already opened in a tab
for nr in range(1, tabpagenr('$'))
Expand Down Expand Up @@ -949,6 +956,13 @@ func! s:compword(s1, s2)
retu wrd1 == wrd2 ? 0 : wrd1 > wrd2 ? 1 : -1
endfunc

func! s:comptime(s1, s2)
" By last modified time
let time1 = getftime(a:s1)
let time2 = getftime(a:s2)
retu time1 == time2 ? 0 : time1 < time2 ? 1 : -1
endfunc

func! s:matchlens(str, pat, ...)
if empty(a:pat) || index(['^','$'], a:pat) >= 0
retu {}
Expand Down Expand Up @@ -986,7 +1000,11 @@ func! s:wordonly(lens)
endfunc

func! s:mixedsort(s1, s2)
retu 3 * s:compmatlen(a:s1, a:s2) + 2 * s:complen(a:s1, a:s2) + s:compword(a:s1, a:s2)
let cmatlen = s:compmatlen(a:s1, a:s2)
let ctime = s:comptime(a:s1, a:s2)
let clen = s:complen(a:s1, a:s2)
let cword = s:compword(a:s1, a:s2)
retu 3 * cmatlen + 3 * ctime + 2 * clen + cword
endfunc
"}}}

Expand Down Expand Up @@ -1173,15 +1191,15 @@ endfunc

" Buffers {{{
func! s:normbuf()
if &l:bl && empty(&l:bt) && &l:ma | retu winnr() | endif
let bufs = []
for each in range(1, winnr('$'))
let bufnr = winbufnr(each)
if getbufvar(bufnr, '&bl') && empty(getbufvar(bufnr, '&bt'))
\ && getbufvar(bufnr, '&ma')
retu each
cal add(bufs, each)
endif
endfor
retu 0
retu bufs
endfunc

func! s:setupblank()
Expand Down
7 changes: 7 additions & 0 deletions doc/ctrlp.txt
Expand Up @@ -221,6 +221,11 @@ Use %s in place of the target directory: >
Examples: >
let g:ctrlp_user_command = 'find %s -type f' " MacOSX/Linux
let g:ctrlp_user_command = 'dir %s /-n /b /s /a-d' " Windows

" Use version control commands. Remember to set |g:ctrlp_working_path_mode|
" to 2 and |g:ctrlp_root_markers| to ['.git/'] or ['.hg/']
let g:ctrlp_user_command = 'cd %s && git ls-files'
let g:ctrlp_user_command = 'hg --cwd %s locate -I .'
<
You can also use 'grep', 'findstr' or something else to filter the results.
Examples: >
Expand Down Expand Up @@ -441,6 +446,8 @@ e) Type the name of a non-existed file and press <c-y> to create it.
e.g. 'parentdir/file.txt' will create a directory named 'parentdir' as well
as 'file.txt'. Use '\' in place of '/' on Windows (if |'ssl'| is not set).

f) Submit ? to open this help file.

===============================================================================
6. Credits *ctrlp-credits*

Expand Down
1 change: 1 addition & 0 deletions readme.md
Expand Up @@ -21,6 +21,7 @@ Once CtrlP is open:
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.
* 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.

## Basic Options
* Change the mapping to invoke CtrlP:
Expand Down

0 comments on commit b7d87e1

Please sign in to comment.