Use VCS's list commands

:help g:ctrlp_user_command
This commit is contained in:
Kien N 2011-10-09 02:26:36 +07:00
parent 1ebf85d96c
commit 000742dc0f
2 changed files with 57 additions and 21 deletions

View File

@ -92,7 +92,7 @@ endfunc
"}}} "}}}
" * ListAllFiles {{{ " * ListAllFiles {{{
func! s:List(dirs, allfiles, depth) func! s:GlobPath(dirs, allfiles, depth)
" Note: wildignore is ignored when using ** " Note: wildignore is ignored when using **
let glob = s:dotfiles ? '.*\|*' : '*' let glob = s:dotfiles ? '.*\|*' : '*'
let entries = split(globpath(a:dirs, glob), '\n') let entries = split(globpath(a:dirs, glob), '\n')
@ -107,28 +107,36 @@ func! s:List(dirs, allfiles, depth)
else else
let dirs = join(alldirs, ',') let dirs = join(alldirs, ',')
sil! cal s:progress(len(g:ctrlp_allfiles)) sil! cal s:progress(len(g:ctrlp_allfiles))
cal s:List(dirs, g:ctrlp_allfiles, depth) cal s:GlobPath(dirs, g:ctrlp_allfiles, depth)
endif
endfunc
func! s:UserCommand(path, lscmd)
if exists('+ssl') && &ssl
let ssl = &ssl
let &ssl = 0
endif
let g:ctrlp_allfiles = split(system(printf(a:lscmd, shellescape(a:path))), '\n')
if exists('+ssl') && exists('ssl')
let &ssl = ssl
cal map(g:ctrlp_allfiles, 'substitute(v:val, "\\", "/", "g")')
endif
if exists('s:vcscmd') && s:vcscmd
cal map(g:ctrlp_allfiles, 'substitute(v:val, "/", "\\", "g")')
endif endif
endfunc endfunc
func! s:ListAllFiles(path) func! s:ListAllFiles(path)
let cache_file = ctrlp#utils#cachefile() let cache_file = ctrlp#utils#cachefile()
if g:ctrlp_newcache || !filereadable(cache_file) || !s:caching if g:ctrlp_newcache || !filereadable(cache_file) || !s:caching
let lscmd = s:lscommand()
" Get the list of files " Get the list of files
if empty(g:ctrlp_user_command) if empty(lscmd)
cal s:List(a:path, [], 0) cal s:GlobPath(a:path, [], 0)
else else
sil! cal s:progress(escape('Waiting...', ' ')) sil! cal s:progress(escape('Waiting...', ' '))
try try
if exists('+ssl') && &ssl cal s:UserCommand(a:path, lscmd)
let ssl = &ssl
let &ssl = 0
endif
let g:ctrlp_allfiles = split(system(printf(g:ctrlp_user_command, shellescape(a:path))), '\n')
if exists('+ssl') && exists('ssl')
let &ssl = ssl
cal map(g:ctrlp_allfiles, 'substitute(v:val, "\\", "/", "g")')
endif
catch catch
retu [] retu []
endtry endtry
@ -792,13 +800,19 @@ endfunc
"}}} "}}}
" * SetWorkingPath {{{ " * SetWorkingPath {{{
func! s:FindRoot(curr, mark, depth) func! s:FindRoot(curr, mark, depth, ...)
let depth = a:depth + 1 let depth = a:depth + 1
if !empty(globpath(a:curr, a:mark)) || depth > s:maxdepth if !empty(globpath(a:curr, a:mark)) || depth > s:maxdepth
if exists('a:1') && !empty(a:1)
let s:vcsroot = depth <= s:maxdepth ? a:curr : ''
else
sil! exe 'chd!' a:curr sil! exe 'chd!' a:curr
endif
else else
let parent = substitute(a:curr, '[\/]\zs[^\/]\+[\/]\?$', '', '') let parent = substitute(a:curr, '[\/]\zs[^\/]\+[\/]\?$', '', '')
if parent != a:curr | cal s:FindRoot(parent, a:mark, depth) | endif if parent != a:curr
cal s:FindRoot(parent, a:mark, depth, a:1)
endif
endif endif
endfunc endfunc
@ -1269,6 +1283,25 @@ func! s:insertcache(str)
cal ctrlp#utils#writecache(data) cal ctrlp#utils#writecache(data)
endif endif
endfunc endfunc
func! s:lscommand()
let usercmd = g:ctrlp_user_command
if type(usercmd) == 1
retu usercmd
elseif type(usercmd) == 3 && len(usercmd) >= 2
\ && !empty(usercmd[0]) && !empty(usercmd[1])
let rmarker = usercmd[0]
" Find a repo root if existed
cal s:FindRoot(getcwd(), rmarker, 0, 1)
if !exists('s:vcsroot') || ( exists('s:vcsroot') && empty(s:vcsroot) )
" Try the secondary_command if defined
retu len(usercmd) == 3 && !empty(usercmd[2]) ? usercmd[2] : ''
else
let s:vcscmd = s:lash == '\' ? 1 : 0
retu usercmd[1]
endif
endif
endfunc
"}}} "}}}
"}}} "}}}

View File

@ -221,17 +221,20 @@ Use %s in place of the target directory: >
Examples: > Examples: >
let g:ctrlp_user_command = 'find %s -type f' " MacOSX/Linux let g:ctrlp_user_command = 'find %s -type f' " MacOSX/Linux
let g:ctrlp_user_command = 'dir %s /-n /b /s /a-d' " Windows 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. You can also use 'grep', 'findstr' or something else to filter the results.
Examples: > Examples: >
let g:ctrlp_user_command = 'find %s -type f | grep (?!tmp/.*)' let g:ctrlp_user_command = 'find %s -type f | grep (?!tmp/.*)'
let g:ctrlp_user_command = 'dir %s /-n /b /s /a-d | findstr .*\.py$' let g:ctrlp_user_command = 'dir %s /-n /b /s /a-d | findstr .*\.py$'
< <
Use version control listing commands: >
let g:ctrlp_user_command = [marker, command, secondary_command]
<
If the secondary_command is empty or not defined, globpath() will be used.
Examples: >
let g:ctrlp_user_command = ['.git/', 'cd %s && git ls-files']
let g:ctrlp_user_command = ['.hg/', 'hg --cwd %s locate --fullpath -I .']
<
*'g:ctrlp_open_new_file'* *'g:ctrlp_open_new_file'*
Use this option to specify how the newly created file is to be opened when Use this option to specify how the newly created file is to be opened when