Skip to content

Commit e4b0a4f

Browse files
authored
Merge pull request #220 from ctrlpvim/core-types
Customizable core types
2 parents b9fa920 + 7607f82 commit e4b0a4f

3 files changed

Lines changed: 90 additions & 57 deletions

File tree

autoload/ctrlp.vim

Lines changed: 67 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -203,11 +203,11 @@ let s:hlgrps = {
203203
\ }
204204

205205
" lname, sname of the basic(non-extension) modes
206-
let s:coretypes = [
206+
let s:coretypes = filter([
207207
\ ['files', 'fil'],
208208
\ ['buffers', 'buf'],
209209
\ ['mru files', 'mru'],
210-
\ ]
210+
\ ], 'index(g:ctrlp_types, v:val[1])!=-1')
211211

212212
" Get the options {{{2
213213
fu! s:opts(...)
@@ -439,9 +439,9 @@ fu! s:UserCmd(lscmd)
439439
en
440440
if has('patch-7.4-597') && !(has('win32') || has('win64'))
441441
let g:ctrlp_allfiles = systemlist(printf(lscmd, path))
442-
else
442+
el
443443
let g:ctrlp_allfiles = split(system(printf(lscmd, path)), "\n")
444-
end
444+
en
445445
if exists('+ssl') && exists('ssl')
446446
let &ssl = ssl
447447
cal map(g:ctrlp_allfiles, 'tr(v:val, "\\", "/")')
@@ -719,7 +719,7 @@ fu! s:PrtBS()
719719
if empty(s:prompt[0]) && s:brfprt != 0
720720
cal s:PrtExit()
721721
retu
722-
endif
722+
en
723723
unl! s:hstgot
724724
let [s:prompt[0], s:matches] = [substitute(s:prompt[0], '.$', '', ''), 1]
725725
cal s:BuildPrompt(1)
@@ -877,12 +877,13 @@ fu! s:PrtFocusMap(char)
877877
endf
878878

879879
fu! s:PrtClearCache()
880-
if s:itemtype == 0
880+
let ct = s:curtype()
881+
if ct == 'fil'
881882
cal ctrlp#clr()
882-
elsei s:itemtype > 2
883+
elsei s:itemtype >= len(s:coretypes)
883884
cal ctrlp#clr(s:statypes[s:itemtype][1])
884885
en
885-
if s:itemtype == 2
886+
if ct == 'mru'
886887
let g:ctrlp_lines = ctrlp#mrufiles#refresh()
887888
el
888889
cal ctrlp#setlines()
@@ -893,17 +894,18 @@ fu! s:PrtClearCache()
893894
endf
894895

895896
fu! s:PrtDeleteEnt()
896-
if s:itemtype == 2
897+
let ct = s:curtype()
898+
if ct == 'mru'
897899
cal s:PrtDeleteMRU()
898-
elsei s:itemtype == 1
900+
elsei ct == 'buf'
899901
cal s:delbuf()
900902
elsei type(s:getextvar('wipe')) == 1
901903
cal s:delent(s:getextvar('wipe'))
902904
en
903905
endf
904906

905907
fu! s:PrtDeleteMRU()
906-
if s:itemtype == 2
908+
if s:curtype() == 'mru'
907909
cal s:delent('ctrlp#mrufiles#remove')
908910
en
909911
endf
@@ -1011,7 +1013,7 @@ fu! s:ToggleByFname()
10111013
endf
10121014

10131015
fu! s:ToggleType(dir)
1014-
let max = len(g:ctrlp_ext_vars) + 2
1016+
let max = len(g:ctrlp_ext_vars) + len(s:coretypes) - 1
10151017
let next = s:walker(max, s:itemtype, a:dir)
10161018
cal ctrlp#setlines(next)
10171019
cal ctrlp#syntax()
@@ -1151,7 +1153,7 @@ fu! s:AcceptSelection(action)
11511153
if subm | if s:SpecInputs(str) | retu | en | en
11521154
" Get the selected line
11531155
let line = ctrlp#getcline()
1154-
if !subm && !s:itemtype && line == '' && line('.') > s:offset
1156+
if !subm && s:curtype() != 'fil' && line == '' && line('.') > s:offset
11551157
\ && str !~ '\v^(\.\.([\/]\.\.)*[\/]?[.\/]*|/|\\|\?|\@.+)$'
11561158
cal s:CreateNewFile(md) | retu
11571159
en
@@ -1161,7 +1163,7 @@ fu! s:AcceptSelection(action)
11611163
let actfunc = s:openfunc[s:ctype]
11621164
let type = has_key(s:openfunc, 'arg_type') ? s:openfunc['arg_type'] : 'list'
11631165
el
1164-
if s:itemtype < 3
1166+
if s:itemtype < len(s:coretypes)
11651167
let [actfunc, type] = ['ctrlp#acceptfile', 'dict']
11661168
el
11671169
let [actfunc, exttype] = [s:getextvar('accept'), s:getextvar('act_farg')]
@@ -1206,15 +1208,16 @@ fu! s:CreateNewFile(...)
12061208
endf
12071209
" * OpenMulti() {{{1
12081210
fu! s:MarkToOpen()
1211+
let ct = s:curtype()
12091212
if s:bufnr <= 0 || s:opmul == '0'
1210-
\ || ( s:itemtype > 2 && s:getextvar('opmul') != 1 )
1213+
\ || ( s:itemtype >= len(s:coretypes) && s:getextvar('opmul') != 1 )
12111214
retu
12121215
en
12131216
let line = ctrlp#getcline()
12141217

12151218
" Do not allow to mark modified or current buffer
12161219
let bufnr = s:bufnrfilpath(line)[0]
1217-
if (s:itemtype == 1 && s:delbufcond(bufnr))
1220+
if (ct == 'buf' && s:delbufcond(bufnr))
12181221
retu
12191222
en
12201223

@@ -1247,7 +1250,7 @@ endf
12471250
fu! s:OpenMulti(...)
12481251
let has_marked = exists('s:marked')
12491252
if ( !has_marked && a:0 ) || s:opmul == '0' || !s:ispath
1250-
\ || ( s:itemtype > 2 && s:getextvar('opmul') != 1 )
1253+
\ || ( s:itemtype >= len(s:coretypes) && s:getextvar('opmul') != 1 )
12511254
retu -1
12521255
en
12531256
" Get the options
@@ -1442,7 +1445,8 @@ fu! s:shortest(lens)
14421445
endf
14431446

14441447
fu! s:mixedsort(...)
1445-
if s:itemtype == 1
1448+
let ct = s:curtype()
1449+
if ct == 'buf'
14461450
let pat = '[\/]\?\[\d\+\*No Name\]$'
14471451
if a:1 =~# pat && a:2 =~# pat | retu 0
14481452
elsei a:1 =~# pat | retu 1
@@ -1453,10 +1457,10 @@ fu! s:mixedsort(...)
14531457
let ms = []
14541458
if s:res_count < 21
14551459
let ms += [s:compfnlen(a:1, a:2)]
1456-
if s:itemtype !~ '^[12]$' | let ms += [s:comptime(a:1, a:2)] | en
1460+
if ct !~ '^\(buf\|mru\)$' | let ms += [s:comptime(a:1, a:2)] | en
14571461
if !s:itemtype | let ms += [s:comparent(a:1, a:2)] | en
14581462
en
1459-
if s:itemtype =~ '^[12]$'
1463+
if ct =~ '^\(buf\|mru\)$'
14601464
let ms += [s:compmref(a:1, a:2)]
14611465
let cln = cml ? cln : 0
14621466
en
@@ -1540,7 +1544,8 @@ endf
15401544
" Line formatting {{{3
15411545
fu! s:formatline(str)
15421546
let str = a:str
1543-
if s:itemtype == 1
1547+
let ct = s:curtype()
1548+
if ct == 'buf'
15441549
let bufnr = s:bufnrfilpath(str)[0]
15451550
let parts = s:bufparts(bufnr)
15461551
let str = printf('%'.s:bufnr_width.'s', bufnr)
@@ -1560,7 +1565,7 @@ fu! s:formatline(str)
15601565
en
15611566
en
15621567
en
1563-
let cond = s:itemtype != 1 &&s:ispath && ( s:winw - 4 ) < s:strwidth(str)
1568+
let cond = ct != 'buf' &&s:ispath && ( s:winw - 4 ) < s:strwidth(str)
15641569
retu s:lineprefix.( cond ? s:pathshorten(str) : str )
15651570
endf
15661571

@@ -1615,7 +1620,7 @@ fu! s:lash(...)
16151620
endf
16161621

16171622
fu! s:ispathitem()
1618-
retu s:itemtype < 3 || ( s:itemtype > 2 && s:getextvar('type') == 'path' )
1623+
retu s:itemtype < len(s:coretypes) || s:getextvar('type') == 'path'
16191624
endf
16201625

16211626
fu! ctrlp#igncwd(cwd)
@@ -1649,20 +1654,20 @@ fu! ctrlp#dirnfile(entries)
16491654
endf
16501655

16511656
fu! s:usrign(item, type)
1652-
if s:igntype == 1 | retu a:item =~ s:usrign | end
1657+
if s:igntype == 1 | retu a:item =~ s:usrign | en
16531658
if s:igntype == 2
16541659
if call(s:usrign, [a:item, a:type])
16551660
retu 1
1656-
end
1661+
en
16571662
elsei s:igntype == 4
16581663
if has_key(s:usrign, a:type) && s:usrign[a:type] != ''
16591664
\ && a:item =~ s:usrign[a:type]
16601665
retu 1
16611666
elsei has_key(s:usrign, 'func') && s:usrign['func'] != ''
16621667
\ && call(s:usrign['func'], [a:item, a:type])
16631668
retu 1
1664-
end
1665-
end
1669+
en
1670+
en
16661671
retu 0
16671672
endf
16681673

@@ -1772,7 +1777,7 @@ fu! ctrlp#syntax()
17721777
exe "sy match CtrlPLinePre '^".escape(get(g:, 'ctrlp_line_prefix', '>'),'^$.*~\')."'"
17731778
en
17741779

1775-
if s:itemtype == 1 && s:has_conceal
1780+
if s:curtype() == 'buf' && s:has_conceal
17761781
sy region CtrlPBufferNr matchgroup=CtrlPLinePre start='^>\s\+' end='\s'
17771782
sy region CtrlPBufferInd concealends matchgroup=Ignore start='<bi>' end='</bi>'
17781783
sy region CtrlPBufferRegion concealends matchgroup=Ignore start='<bn>' end='</bn>'
@@ -1810,7 +1815,7 @@ fu! s:highlight(pat, grp)
18101815
if s:byfname()
18111816
" Make sure there are no slashes in our match
18121817
let beginning = beginning.'\([^\/]*$\)\@='
1813-
end
1818+
en
18141819

18151820
for i in range(len(chars))
18161821
" Surround our current target letter with \zs and \ze so it only
@@ -1822,14 +1827,14 @@ fu! s:highlight(pat, grp)
18221827
if i == 0
18231828
let charcopy[i] = '\zs'.charcopy[i].'\ze'
18241829
let middle = join(charcopy, '.\{-}')
1825-
else
1830+
el
18261831
let before = join(charcopy[0:i-1], '.\{-}')
18271832
let after = join(charcopy[i+1:-1], '.\{-}')
18281833
let c = charcopy[i]
18291834
" for abc, match either ab.\{-}c or a.*b.\{-}c in that order
18301835
let cpat = '\(\zs'.c.'\|'.'.*\zs'.c.'\)\ze.*'
18311836
let middle = before.cpat.after
1832-
endif
1837+
en
18331838

18341839
" Now we matchadd for each letter, the basic form being:
18351840
" ^.*\zsx\ze.*$, but with our pattern we built above for the letter,
@@ -2140,12 +2145,13 @@ fu! s:modevar()
21402145
endf
21412146

21422147
fu! s:nosort()
2143-
retu s:matcher != {} || s:nolim == 1 || ( s:itemtype == 2 && s:mrudef )
2144-
\ || ( s:itemtype =~ '\v^(1|2)$' && s:prompt == ['', '', ''] ) || !s:dosort
2148+
let ct = s:curtype()
2149+
retu s:matcher != {} || s:nolim == 1 || ( ct == 'mru' && s:mrudef )
2150+
\ || ( ct =~ '^\(buf\|mru\)$' && s:prompt == ['', '', ''] ) || !s:dosort
21452151
endf
21462152

21472153
fu! s:byfname()
2148-
retu s:itemtype != 1 && s:ispath && s:byfname
2154+
retu s:curtype() != 'buf' && s:ispath && s:byfname
21492155
endf
21502156

21512157
fu! s:narrowable()
@@ -2374,13 +2380,17 @@ fu! s:buildpat(lst)
23742380
retu pat
23752381
endf
23762382

2383+
fu! s:curtype()
2384+
return s:CurTypeName()[1]
2385+
endf
2386+
23772387
fu! s:mfunc()
23782388
let mfunc = 'match'
23792389
if s:byfname()
23802390
let mfunc = 's:matchfname'
2381-
elsei s:itemtype == 1
2391+
elsei s:curtype() == 'buf'
23822392
let mfunc = 's:matchbuf'
2383-
elsei s:itemtype > 2
2393+
elsei s:itemtype >= len(s:coretypes)
23842394
let matchtypes = { 'tabs': 's:matchtabs', 'tabe': 's:matchtabe' }
23852395
if has_key(matchtypes, s:matchtype)
23862396
let mfunc = matchtypes[s:matchtype]
@@ -2439,7 +2449,7 @@ fu! s:insertcache(str)
24392449
endf
24402450
" Extensions {{{2
24412451
fu! s:mtype()
2442-
retu s:itemtype > 2 ? s:getextvar('type') : 'path'
2452+
retu s:itemtype >= len(s:coretypes) ? s:getextvar('type') : 'path'
24432453
endf
24442454

24452455
fu! s:execextvar(key)
@@ -2450,8 +2460,8 @@ fu! s:execextvar(key)
24502460
endf
24512461

24522462
fu! s:getextvar(key)
2453-
if s:itemtype > 2
2454-
let vars = g:ctrlp_ext_vars[s:itemtype - 3]
2463+
if s:itemtype >= len(s:coretypes) && len(g:ctrlp_ext_vars) > 0
2464+
let vars = g:ctrlp_ext_vars[s:itemtype - len(s:coretypes)]
24552465
retu has_key(vars, a:key) ? vars[a:key] : -1
24562466
en
24572467
retu get(g:, 'ctrlp_' . s:matchtype . '_' . a:key, -1)
@@ -2505,7 +2515,8 @@ endf
25052515
fu! ctrlp#setlines(...)
25062516
if a:0 | let s:itemtype = a:1 | en
25072517
cal s:modevar()
2508-
let types = ['ctrlp#files()', 'ctrlp#buffers()', 'ctrlp#mrufiles#list()']
2518+
let inits = {'fil': 'ctrlp#files()', 'buf': 'ctrlp#buffers()', 'mru': 'ctrlp#mrufiles#list()'}
2519+
let types = map(copy(g:ctrlp_types), 'inits[v:val]')
25092520
if !empty(g:ctrlp_ext_vars)
25102521
cal map(copy(g:ctrlp_ext_vars), 'add(types, v:val["init"])')
25112522
en
@@ -2514,19 +2525,19 @@ endf
25142525

25152526
" Returns [lname, sname]
25162527
fu! s:CurTypeName()
2517-
if s:itemtype < 3
2518-
return s:coretypes[s:itemtype]
2519-
else
2528+
if s:itemtype < len(s:coretypes)
2529+
return filter(copy(s:coretypes), 'v:val[1]==g:ctrlp_types[s:itemtype]')[0][1]
2530+
el
25202531
return [s:getextvar("lname"), s:getextvar('sname')]
2521-
endif
2532+
en
25222533
endfu
25232534

25242535
fu! s:ExitIfSingleCandidate()
25252536
if len(s:Update(s:prompt[0])) == 1
25262537
call s:AcceptSelection('e')
25272538
call ctrlp#exit()
25282539
return 1
2529-
endif
2540+
en
25302541
return 0
25312542
endfu
25322543

@@ -2539,14 +2550,23 @@ fu! ctrlp#init(type, ...)
25392550
cal s:SetWD(a:0 ? a:1 : {})
25402551
cal s:MapNorms()
25412552
cal s:MapSpecs()
2542-
cal ctrlp#setlines(s:settype(a:type))
2553+
if type(a:type) == 0
2554+
let type = a:type
2555+
el
2556+
let type = index(g:ctrlp_types, a:type)
2557+
if type == -1
2558+
call ctrlp#exit()
2559+
retu
2560+
en
2561+
en
2562+
cal ctrlp#setlines(s:settype(type))
25432563
cal ctrlp#syntax()
25442564
cal s:SetDefTxt()
25452565
let curName = s:CurTypeName()
25462566
let shouldExitSingle = index(s:opensingle, curName[0])>=0 || index(s:opensingle, curName[1])>=0
25472567
if shouldExitSingle && s:ExitIfSingleCandidate()
25482568
return 0
2549-
endif
2569+
en
25502570
cal s:BuildPrompt(1)
25512571
if s:keyloop | cal s:KeyLoop() | en
25522572
return 1

doc/ctrlp.txt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ Overview:~
6868
|ctrlp_open_single_match|.....Automatically accept when only one candidate.
6969
|ctrlp_brief_prompt|..........Exit CtrlP on empty prompt by <bs>.
7070
|ctrlp_match_current_file|....Include current file in match entries.
71+
|ctrlp_types|.................Names of builtin types.
7172

7273
MRU mode:
7374
|ctrlp_mruf_max|..............Max MRU entries to remember.
@@ -454,13 +455,19 @@ This option works well together with |g:ctrlp_open_single_match|
454455

455456

456457
*'g:ctrlp_match_current_file'*
457-
Includes the current file in the match entries:
458+
Includes the current file in the match entries: >
458459
let g:ctrlp_match_current_file = 1
459460
460461
By default, the current file is excluded from the list.
461462

462463
Note: does not apply when |g:ctrlp_match_func| is used.
463464

465+
*'g:ctrlp_types'*
466+
Set this to list of names to customize core types: >
467+
let g:ctrlp_types = ['mru', 'fil']
468+
469+
By default, the types are: >
470+
let g:ctrlp_types = ['fil', 'buf', 'mru'].
464471
465472
*'g:ctrlp_abbrev'*
466473
Define input abbreviations that can be expanded (either internally or visibly)

0 commit comments

Comments
 (0)