Skip to content

Commit b94d8b4

Browse files
triskmattn
authored andcommitted
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
1 parent a07f71c commit b94d8b4

2 files changed

Lines changed: 37 additions & 23 deletions

File tree

autoload/ctrlp.vim

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -957,23 +957,19 @@ fu! s:SetWD(args)
957957
if has_key(a:args, 'dir') && a:args['dir'] != ''
958958
cal ctrlp#setdir(a:args['dir']) | retu
959959
en
960-
let pmode = has_key(a:args, 'mode') ? a:args['mode'] : s:pathmode
960+
let pmodes = has_key(a:args, 'mode') ? a:args['mode'] : s:pathmode
961961
let [s:crfilerel, s:dyncwd] = [fnamemodify(s:crfile, ':.'), getcwd()]
962-
if s:crfile =~ '^.\+://' | retu | en
963-
if pmode =~ 'c' || ( pmode =~ 'a' && stridx(s:crfpath, s:cwd) < 0 )
964-
\ || ( !type(pmode) && pmode )
965-
if exists('+acd') | let [s:glb_acd, &acd] = [&acd, 0] | en
966-
cal ctrlp#setdir(s:crfpath)
967-
en
968-
if pmode =~ 'r' || pmode == 2
969-
let markers = ['.git', '.hg', '.svn', '.bzr', '_darcs'] + s:custom_markers
970-
let spath = pmode =~ 'd' ? s:dyncwd : pmode =~ 'w' ? s:cwd : s:crfpath
971-
if type(s:rmarkers) == 3 && !empty(s:rmarkers)
972-
if s:findroot(spath, s:rmarkers, 0, 0) != [] | retu | en
973-
cal filter(markers, 'index(s:rmarkers, v:val) < 0')
974-
en
975-
cal s:findroot(spath, markers, 0, 0)
976-
en
962+
if (!type(pmodes))
963+
let pmodes =
964+
\ pmodes == 0 ? '' :
965+
\ pmodes == 1 ? 'a' :
966+
\ pmodes == 2 ? 'r' :
967+
\ 'c'
968+
en
969+
let spath = pmodes =~ 'd' ? s:dyncwd : pmodes =~ 'w' ? s:cwd : s:crfpath
970+
for pmode in split(pmodes, '\zs')
971+
if ctrlp#setpathmode(pmode, spath) | retu | en
972+
endfo
977973
endf
978974
" * AcceptSelection() {{{1
979975
fu! ctrlp#acceptfile(...)
@@ -1584,6 +1580,23 @@ fu! s:findroot(curr, mark, depth, type)
15841580
retu []
15851581
endf
15861582

1583+
fu! ctrlp#setpathmode(pmode, ...)
1584+
if a:pmode == 'c' || ( a:pmode == 'a' && stridx(s:crfpath, s:cwd) < 0 )
1585+
if exists('+acd') | let [s:glb_acd, &acd] = [&acd, 0] | en
1586+
cal ctrlp#setdir(s:crfpath)
1587+
retu 1
1588+
elsei a:pmode == 'r'
1589+
let spath = a:0 ? a:1 : s:crfpath
1590+
let markers = ['.git', '.hg', '.svn', '.bzr', '_darcs']
1591+
if type(s:rmarkers) == 3 && !empty(s:rmarkers)
1592+
if s:findroot(spath, s:rmarkers, 0, 0) != [] | retu 1 | en
1593+
cal filter(markers, 'index(s:rmarkers, v:val) < 0')
1594+
en
1595+
if s:findroot(spath, markers, 0, 0) != [] | retu 1 | en
1596+
en
1597+
retu 0
1598+
endf
1599+
15871600
fu! ctrlp#setdir(path, ...)
15881601
let cmd = a:0 ? a:1 : 'lc!'
15891602
sil! exe cmd s:fnesc(a:path, 'c')

readme.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ Check `:help ctrlp-commands` and `:help ctrlp-extensions` for other commands.
3232
Run `:help ctrlp-mappings` or submit `?` in CtrlP for more mapping help.
3333

3434
* Submit two or more dots `..` to go up the directory tree by one or multiple levels.
35-
* End the input string with a colon `:` followed by a command to execute it on the opening file(s):
36-
Use `:25` to jump to line 25.
35+
* End the input string with a colon `:` followed by a command to execute it on the opening file(s):
36+
Use `:25` to jump to line 25.
3737
Use `:diffthis` when opening multiple files to run `:diffthis` on the first 4 files.
3838

3939
## Basic Options
@@ -44,17 +44,18 @@ Use `:diffthis` when opening multiple files to run `:diffthis` on the first 4 fi
4444
let g:ctrlp_cmd = 'CtrlP'
4545
```
4646
47-
* When invoked, unless a starting directory is specified, CtrlP will set its local working directory according to this variable:
47+
* When invoked without an explicit starting directory, CtrlP will set its local working directory according to this variable:
4848
4949
```vim
5050
let g:ctrlp_working_path_mode = 'ra'
5151
```
5252
53-
`'c'` - the directory of the current file.
54-
`'r'` - the nearest ancestor that contains one of these directories or files: `.git` `.hg` `.svn` `.bzr` `_darcs`
55-
`'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.
56-
`0` or `''` (empty string) - disable this feature.
53+
`'c'` - use the parent directory of the current file.
54+
`'a'` - use the parent directory of the current file if it is not a descendant of the current working directory outside of CtrlP.
55+
`'r'` - use the nearest ancestor of the current file that contains one of these markers: `.git` `.hg` `.svn` `.bzr` `_darcs`
56+
`0` or `''` (empty string) - use the current working directory outside of CtrlP.
5757
58+
If more than one mode is specified, they will be tried in order until a directory is located.
5859
Define additional root markers with the `g:ctrlp_root_markers` option.
5960
6061
* Exclude files and directories using Vim's `wildignore` and CtrlP's own `g:ctrlp_custom_ignore`:

0 commit comments

Comments
 (0)