From 27c9e50208b6bb272d81424020e785af26b88b69 Mon Sep 17 00:00:00 2001 From: Albert Lee Date: Thu, 6 Mar 2014 11:38:52 -0500 Subject: [PATCH] Improve g:ctrlp_working_path_mode Support ordered processing of multiple g:ctrlp_working_path_mode specifications Clarify g:ctrlp_working_path_mode semantics in readme.md --- autoload/ctrlp.vim | 44 +++++++++++++++++++++++++++++--------------- readme.md | 15 ++++++++------- 2 files changed, 37 insertions(+), 22 deletions(-) diff --git a/autoload/ctrlp.vim b/autoload/ctrlp.vim index 19ac1463..8f5d1c0b 100644 --- a/autoload/ctrlp.vim +++ b/autoload/ctrlp.vim @@ -959,23 +959,20 @@ fu! s:SetWD(args) if has_key(a:args, 'dir') && a:args['dir'] != '' cal ctrlp#setdir(a:args['dir']) | retu en - let pmode = has_key(a:args, 'mode') ? a:args['mode'] : s:pathmode + let pmodes = has_key(a:args, 'mode') ? a:args['mode'] : s:pathmode let [s:crfilerel, s:dyncwd] = [fnamemodify(s:crfile, ':.'), getcwd()] if s:crfile =~ '^.\+://' | retu | en - if pmode =~ 'c' || ( pmode =~ 'a' && stridx(s:crfpath, s:cwd) < 0 ) - \ || ( !type(pmode) && pmode ) - if exists('+acd') | let [s:glb_acd, &acd] = [&acd, 0] | en - cal ctrlp#setdir(s:crfpath) - en - if pmode =~ 'r' || pmode == 2 - let markers = ['.git', '.hg', '.svn', '.bzr', '_darcs'] - let spath = pmode =~ 'd' ? s:dyncwd : pmode =~ 'w' ? s:cwd : s:crfpath - if type(s:rmarkers) == 3 && !empty(s:rmarkers) - if s:findroot(spath, s:rmarkers, 0, 0) != [] | retu | en - cal filter(markers, 'index(s:rmarkers, v:val) < 0') - en - cal s:findroot(spath, markers, 0, 0) - en + if (!type(pmodes)) + let pmodes = + \ pmodes == 0 ? '' : + \ pmodes == 1 ? 'a' : + \ pmodes == 2 ? 'r' : + \ 'c' + en + let spath = pmodes =~ 'd' ? s:dyncwd : pmodes =~ 'w' ? s:cwd : s:crfpath + for pmode in split(pmodes, '\zs') + if ctrlp#setpathmode(pmode, spath) | retu | en + endfo endf " * AcceptSelection() {{{1 fu! ctrlp#acceptfile(...) @@ -1586,6 +1583,23 @@ fu! s:findroot(curr, mark, depth, type) retu [] endf +fu! ctrlp#setpathmode(pmode, ...) + if a:pmode == 'c' || ( a:pmode == 'a' && stridx(s:crfpath, s:cwd) < 0 ) + if exists('+acd') | let [s:glb_acd, &acd] = [&acd, 0] | en + cal ctrlp#setdir(s:crfpath) + retu 1 + elsei a:pmode == 'r' + let spath = a:0 ? a:1 : s:crfpath + let markers = ['.git', '.hg', '.svn', '.bzr', '_darcs'] + if type(s:rmarkers) == 3 && !empty(s:rmarkers) + if s:findroot(spath, s:rmarkers, 0, 0) != [] | retu 1 | en + cal filter(markers, 'index(s:rmarkers, v:val) < 0') + en + if s:findroot(spath, markers, 0, 0) != [] | retu 1 | en + en + retu 0 +endf + fu! ctrlp#setdir(path, ...) let cmd = a:0 ? a:1 : 'lc!' sil! exe cmd s:fnesc(a:path, 'c') diff --git a/readme.md b/readme.md index bcd07c7f..f2bbe3da 100644 --- a/readme.md +++ b/readme.md @@ -32,8 +32,8 @@ Check `:help ctrlp-commands` and `:help ctrlp-extensions` for other commands. Run `:help ctrlp-mappings` or submit `?` in CtrlP for more mapping help. * Submit two or more dots `..` to go up the directory tree by one or multiple levels. -* End the input string with a colon `:` followed by a command to execute it on the opening file(s): -Use `:25` to jump to line 25. +* End the input string with a colon `:` followed by a command to execute it on the opening file(s): +Use `:25` to jump to line 25. Use `:diffthis` when opening multiple files to run `:diffthis` on the first 4 files. ## Basic Options @@ -44,17 +44,18 @@ Use `:diffthis` when opening multiple files to run `:diffthis` on the first 4 fi let g:ctrlp_cmd = 'CtrlP' ``` -* When invoked, unless a starting directory is specified, CtrlP will set its local working directory according to this variable: +* When invoked without an explicit starting directory, CtrlP will set its local working directory according to this variable: ```vim let g:ctrlp_working_path_mode = 'ra' ``` - `'c'` - the directory of the current file. - `'r'` - the nearest ancestor that contains one of these directories or files: `.git` `.hg` `.svn` `.bzr` `_darcs` - `'a'` - like c, but only if the current working directory outside of CtrlP is not a direct ancestor of the directory of the current file. - `0` or `''` (empty string) - disable this feature. + `'c'` - use the parent directory of the current file. + `'a'` - use the parent directory of the current file if it is not a descendant of the current working directory outside of CtrlP. + `'r'` - use the nearest ancestor of the current file that contains one of these markers: `.git` `.hg` `.svn` `.bzr` `_darcs` + `0` or `''` (empty string) - use the current working directory outside of CtrlP. + If more than one mode is specified, they will be tried in order until a directory is located. Define additional root markers with the `g:ctrlp_root_markers` option. * Exclude files and directories using Vim's `wildignore` and CtrlP's own `g:ctrlp_custom_ignore`: