From 47cca10b3f820f7ab6761d0ff2b1c738e38ee30f Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Fri, 18 Mar 2016 14:40:47 +0900 Subject: [PATCH 01/12] Customizable core types --- autoload/ctrlp.vim | 108 +++++++++++++++++++++++++++------------------ plugin/ctrlp.vim | 21 +++++---- 2 files changed, 76 insertions(+), 53 deletions(-) diff --git a/autoload/ctrlp.vim b/autoload/ctrlp.vim index 98369b2d..a30fdbc8 100644 --- a/autoload/ctrlp.vim +++ b/autoload/ctrlp.vim @@ -434,9 +434,9 @@ fu! s:UserCmd(lscmd) let path = exists('*shellescape') ? shellescape(path) : path if has('patch-7.4-597') && !(has('win32') || has('win64')) let g:ctrlp_allfiles = systemlist(printf(lscmd, path)) - else + el let g:ctrlp_allfiles = split(system(printf(lscmd, path)), "\n") - end + en if exists('+ssl') && exists('ssl') let &ssl = ssl cal map(g:ctrlp_allfiles, 'tr(v:val, "\\", "/")') @@ -527,7 +527,7 @@ fu! s:MatchIt(items, pat, limit, exc) for item in a:items let id += 1 try - if (s:matchcrfile || !( s:ispath && item == a:exc )) && + if (s:matchcrfile || !( s:ispath && item == a:exc )) && \call(s:mfunc, [item, pat]) >= 0 cal add(lines, item) en @@ -714,7 +714,7 @@ fu! s:PrtBS() if empty(s:prompt[0]) && s:brfprt != 0 cal s:PrtExit() retu - endif + en unl! s:hstgot let [s:prompt[0], s:matches] = [substitute(s:prompt[0], '.$', '', ''), 1] cal s:BuildPrompt(1) @@ -872,12 +872,13 @@ fu! s:PrtFocusMap(char) endf fu! s:PrtClearCache() - if s:itemtype == 0 + let ct = s:curtype() + if ct == 'fil' cal ctrlp#clr() - elsei s:itemtype > 2 + elsei s:itemtype >= len(s:coretypes) cal ctrlp#clr(s:statypes[s:itemtype][1]) en - if s:itemtype == 2 + if ct == 'mru' let g:ctrlp_lines = ctrlp#mrufiles#refresh() el cal ctrlp#setlines() @@ -888,9 +889,10 @@ fu! s:PrtClearCache() endf fu! s:PrtDeleteEnt() - if s:itemtype == 2 + let ct = s:curtype() + if ct == 'mru' cal s:PrtDeleteMRU() - elsei s:itemtype == 1 + elsei ct == 'buf' cal s:delbuf() elsei type(s:getextvar('wipe')) == 1 cal s:delent(s:getextvar('wipe')) @@ -898,7 +900,7 @@ fu! s:PrtDeleteEnt() endf fu! s:PrtDeleteMRU() - if s:itemtype == 2 + if s:curtype() == 'mru' cal s:delent('ctrlp#mrufiles#remove') en endf @@ -1005,7 +1007,7 @@ fu! s:ToggleByFname() endf fu! s:ToggleType(dir) - let max = len(g:ctrlp_ext_vars) + 2 + let max = len(g:ctrlp_ext_vars) + len(s:coretypes) - 1 let next = s:walker(max, s:itemtype, a:dir) cal ctrlp#setlines(next) cal ctrlp#syntax() @@ -1145,7 +1147,7 @@ fu! s:AcceptSelection(action) if subm | if s:SpecInputs(str) | retu | en | en " Get the selected line let line = ctrlp#getcline() - if !subm && !s:itemtype && line == '' && line('.') > s:offset + if !subm && s:curtype() != 'fil' && line == '' && line('.') > s:offset \ && str !~ '\v^(\.\.([\/]\.\.)*[\/]?[.\/]*|/|\\|\?|\@.+)$' cal s:CreateNewFile(md) | retu en @@ -1155,7 +1157,7 @@ fu! s:AcceptSelection(action) let actfunc = s:openfunc[s:ctype] let type = has_key(s:openfunc, 'arg_type') ? s:openfunc['arg_type'] : 'list' el - if s:itemtype < 3 + if s:itemtype < len(s:coretypes) let [actfunc, type] = ['ctrlp#acceptfile', 'dict'] el let [actfunc, exttype] = [s:getextvar('accept'), s:getextvar('act_farg')] @@ -1200,15 +1202,16 @@ fu! s:CreateNewFile(...) endf " * OpenMulti() {{{1 fu! s:MarkToOpen() + let ct = s:curtype() if s:bufnr <= 0 || s:opmul == '0' - \ || ( s:itemtype > 2 && s:getextvar('opmul') != 1 ) + \ || ( s:itemtype >= len(s:coretypes) && s:getextvar('opmul') != 1 ) retu en let line = ctrlp#getcline() " Do not allow to mark modified or current buffer let bufnr = s:bufnrfilpath(line)[0] - if (s:itemtype == 1 && s:delbufcond(bufnr)) + if (ct == 'buf' && s:delbufcond(bufnr)) retu en @@ -1241,7 +1244,7 @@ endf fu! s:OpenMulti(...) let has_marked = exists('s:marked') if ( !has_marked && a:0 ) || s:opmul == '0' || !s:ispath - \ || ( s:itemtype > 2 && s:getextvar('opmul') != 1 ) + \ || ( s:itemtype >= len(s:coretypes) && s:getextvar('opmul') != 1 ) retu -1 en " Get the options @@ -1415,7 +1418,8 @@ fu! s:shortest(lens) endf fu! s:mixedsort(...) - if s:itemtype == 1 + let ct = s:curtype() + if ct == 'buf' let pat = '[\/]\?\[\d\+\*No Name\]$' if a:1 =~# pat && a:2 =~# pat | retu 0 elsei a:1 =~# pat | retu 1 @@ -1426,10 +1430,10 @@ fu! s:mixedsort(...) let ms = [] if s:res_count < 21 let ms += [s:compfnlen(a:1, a:2)] - if s:itemtype !~ '^[12]$' | let ms += [s:comptime(a:1, a:2)] | en + if ct !~ '^\(buf\|mru\)$' | let ms += [s:comptime(a:1, a:2)] | en if !s:itemtype | let ms += [s:comparent(a:1, a:2)] | en en - if s:itemtype =~ '^[12]$' + if ct =~ '^\(buf\|mru\)$' let ms += [s:compmref(a:1, a:2)] let cln = cml ? cln : 0 en @@ -1454,6 +1458,7 @@ endf " Statusline {{{2 fu! ctrlp#statusline() if !exists('s:statypes') + let s:coretypes = filter(s:coretypes, 'index(g:ctrlp_types, v:val[1])!=-1') let s:statypes = copy(s:coretypes) if !empty(g:ctrlp_ext_vars) cal map(copy(g:ctrlp_ext_vars), @@ -1513,7 +1518,8 @@ endf " Line formatting {{{3 fu! s:formatline(str) let str = a:str - if s:itemtype == 1 + let ct = s:curtype() + if ct == 'buf' let bufnr = s:bufnrfilpath(str)[0] let parts = s:bufparts(bufnr) let str = printf('%'.s:bufnr_width.'s', bufnr) @@ -1533,7 +1539,7 @@ fu! s:formatline(str) en en en - let cond = s:itemtype != 1 &&s:ispath && ( s:winw - 4 ) < s:strwidth(str) + let cond = ct != 'buf' &&s:ispath && ( s:winw - 4 ) < s:strwidth(str) retu s:lineprefix.( cond ? s:pathshorten(str) : str ) endf @@ -1588,7 +1594,7 @@ fu! s:lash(...) endf fu! s:ispathitem() - retu s:itemtype < 3 || ( s:itemtype > 2 && s:getextvar('type') == 'path' ) + retu s:itemtype < len(s:coretypes) || s:getextvar('type') == 'path' endf fu! ctrlp#igncwd(cwd) @@ -1622,11 +1628,11 @@ fu! ctrlp#dirnfile(entries) endf fu! s:usrign(item, type) - if s:igntype == 1 | retu a:item =~ s:usrign | end + if s:igntype == 1 | retu a:item =~ s:usrign | en if s:igntype == 2 if call(s:usrign, [a:item, a:type]) retu 1 - end + en elsei s:igntype == 4 if has_key(s:usrign, a:type) && s:usrign[a:type] != '' \ && a:item =~ s:usrign[a:type] @@ -1634,8 +1640,8 @@ fu! s:usrign(item, type) elsei has_key(s:usrign, 'func') && s:usrign['func'] != '' \ && call(s:usrign['func'], [a:item, a:type]) retu 1 - end - end + en + en retu 0 endf @@ -1736,7 +1742,7 @@ fu! ctrlp#syntax() sy match CtrlPLinePre '^>' en - if s:itemtype == 1 && s:has_conceal + if s:curtype() == 'buf' && s:has_conceal sy region CtrlPBufferNr matchgroup=CtrlPLinePre start='^>\s\+' end='\s' sy region CtrlPBufferInd concealends matchgroup=Ignore start='' end='' sy region CtrlPBufferRegion concealends matchgroup=Ignore start='' end='' @@ -1774,7 +1780,7 @@ fu! s:highlight(pat, grp) if s:byfname() " Make sure there are no slashes in our match let beginning = beginning.'\([^\/]*$\)\@=' - end + en for i in range(len(chars)) " Surround our current target letter with \zs and \ze so it only @@ -1786,14 +1792,14 @@ fu! s:highlight(pat, grp) if i == 0 let charcopy[i] = '\zs'.charcopy[i].'\ze' let middle = join(charcopy, '.\{-}') - else + el let before = join(charcopy[0:i-1], '.\{-}') let after = join(charcopy[i+1:-1], '.\{-}') let c = charcopy[i] " for abc, match either ab.\{-}c or a.*b.\{-}c in that order let cpat = '\(\zs'.c.'\|'.'.*\zs'.c.'\)\ze.*' let middle = before.cpat.after - endif + en " Now we matchadd for each letter, the basic form being: " ^.*\zsx\ze.*$, but with our pattern we built above for the letter, @@ -2090,12 +2096,13 @@ fu! s:modevar() endf fu! s:nosort() - retu s:matcher != {} || s:nolim == 1 || ( s:itemtype == 2 && s:mrudef ) - \ || ( s:itemtype =~ '\v^(1|2)$' && s:prompt == ['', '', ''] ) || !s:dosort + let ct = s:curtype() + retu s:matcher != {} || s:nolim == 1 || ( ct == 'mru' && s:mrudef ) + \ || ( ct =~ '^\(buf\|mru\)$' && s:prompt == ['', '', ''] ) || !s:dosort endf fu! s:byfname() - retu s:itemtype != 1 && s:ispath && s:byfname + retu s:curtype() != 'buf' && s:ispath && s:byfname endf fu! s:narrowable() @@ -2317,13 +2324,17 @@ fu! s:buildpat(lst) retu pat endf +fu! s:curtype() + return s:CurTypeName()[1] +endf + fu! s:mfunc() let mfunc = 'match' if s:byfname() let mfunc = 's:matchfname' - elsei s:itemtype == 1 + elsei s:curtype() == 'buf' let mfunc = 's:matchbuf' - elsei s:itemtype > 2 + elsei s:itemtype >= len(s:coretypes) let matchtypes = { 'tabs': 's:matchtabs', 'tabe': 's:matchtabe' } if has_key(matchtypes, s:matchtype) let mfunc = matchtypes[s:matchtype] @@ -2382,7 +2393,7 @@ fu! s:insertcache(str) endf " Extensions {{{2 fu! s:mtype() - retu s:itemtype > 2 ? s:getextvar('type') : 'path' + retu s:itemtype >= len(s:coretypes) ? s:getextvar('type') : 'path' endf fu! s:execextvar(key) @@ -2393,8 +2404,8 @@ fu! s:execextvar(key) endf fu! s:getextvar(key) - if s:itemtype > 2 - let vars = g:ctrlp_ext_vars[s:itemtype - 3] + if s:itemtype >= len(s:coretypes) && len(g:ctrlp_ext_vars) + let vars = g:ctrlp_ext_vars[s:itemtype - len(s:coretypes)] retu has_key(vars, a:key) ? vars[a:key] : -1 en retu get(g:, 'ctrlp_' . s:matchtype . '_' . a:key, -1) @@ -2457,11 +2468,11 @@ endf " Returns [lname, sname] fu! s:CurTypeName() - if s:itemtype < 3 + if s:itemtype < len(s:coretypes) return s:coretypes[s:itemtype] - else + el return [s:getextvar("lname"), s:getextvar('sname')] - endif + en endfu fu! s:ExitIfSingleCandidate() @@ -2469,7 +2480,7 @@ fu! s:ExitIfSingleCandidate() call s:AcceptSelection('e') call ctrlp#exit() return 1 - endif + en return 0 endfu @@ -2482,14 +2493,23 @@ fu! ctrlp#init(type, ...) cal s:SetWD(a:0 ? a:1 : {}) cal s:MapNorms() cal s:MapSpecs() - cal ctrlp#setlines(s:settype(a:type)) + if type(a:type) == 0 + let type = a:type + el + let type = index(g:ctrlp_types, a:type) + if type == -1 + call ctrlp#exit() + retu + en + en + cal ctrlp#setlines(s:settype(type)) cal ctrlp#syntax() cal s:SetDefTxt() let curName = s:CurTypeName() let shouldExitSingle = index(s:opensingle, curName[0])>=0 || index(s:opensingle, curName[1])>=0 if shouldExitSingle && s:ExitIfSingleCandidate() return 0 - endif + en cal s:BuildPrompt(1) if s:keyloop | cal s:KeyLoop() | en return 1 diff --git a/plugin/ctrlp.vim b/plugin/ctrlp.vim index 5f5303f1..77acc8f0 100644 --- a/plugin/ctrlp.vim +++ b/plugin/ctrlp.vim @@ -10,17 +10,20 @@ if ( exists('g:loaded_ctrlp') && g:loaded_ctrlp ) || v:version < 700 || &cp en let g:loaded_ctrlp = 1 +if !exists('g:ctrlp_types') + let g:ctrlp_types = ['fil', 'buf', 'mru'] +en let [g:ctrlp_lines, g:ctrlp_allfiles, g:ctrlp_alltags, g:ctrlp_alldirs, \ g:ctrlp_allmixes, g:ctrlp_buftags, g:ctrlp_ext_vars, g:ctrlp_builtins] - \ = [[], [], [], [], {}, {}, [], 2] + \ = [[], [], [], [], {}, {}, [], len(g:ctrlp_types)] if !exists('g:ctrlp_map') | let g:ctrlp_map = '' | en if !exists('g:ctrlp_cmd') | let g:ctrlp_cmd = 'CtrlP' | en -com! -n=? -com=dir CtrlP cal ctrlp#init(0, { 'dir': }) -com! -n=? -com=dir CtrlPMRUFiles cal ctrlp#init(2, { 'dir': }) +com! -n=? -com=dir CtrlP cal ctrlp#init('fil', { 'dir': }) +com! -n=? -com=dir CtrlPMRUFiles cal ctrlp#init('mru', { 'dir': }) -com! -bar CtrlPBuffer cal ctrlp#init(1) +com! -bar CtrlPBuffer cal ctrlp#init('buf') com! -n=? CtrlPLastMode cal ctrlp#init(-1, { 'args': }) com! -bar CtrlPClearCache cal ctrlp#clr() @@ -29,9 +32,9 @@ com! -bar CtrlPClearAllCaches cal ctrlp#clra() com! -bar ClearCtrlPCache cal ctrlp#clr() com! -bar ClearAllCtrlPCaches cal ctrlp#clra() -com! -bar CtrlPCurWD cal ctrlp#init(0, { 'mode': '' }) -com! -bar CtrlPCurFile cal ctrlp#init(0, { 'mode': 'c' }) -com! -bar CtrlPRoot cal ctrlp#init(0, { 'mode': 'r' }) +com! -bar CtrlPCurWD cal ctrlp#init('fil', { 'mode': '' }) +com! -bar CtrlPCurFile cal ctrlp#init('fil', { 'mode': 'c' }) +com! -bar CtrlPRoot cal ctrlp#init('fil', { 'mode': 'r' }) exe 'nn (ctrlp) :'.g:ctrlp_cmd.'' @@ -55,10 +58,10 @@ com! -bar CtrlPRTS cal ctrlp#init(ctrlp#rtscript#id()) com! -bar CtrlPUndo cal ctrlp#init(ctrlp#undo#id()) com! -n=? -com=buffer CtrlPLine - \ cal ctrlp#init(ctrlp#line#cmd(1, )) + \ cal ctrlp#init(ctrlp#line#cmd('buf', )) com! -n=? -com=buffer CtrlPChange - \ cal ctrlp#init(ctrlp#changes#cmd(0, )) + \ cal ctrlp#init(ctrlp#changes#cmd('fil', )) com! -bar CtrlPChangeAll cal ctrlp#init(ctrlp#changes#cmd(1)) com! -bar CtrlPMixed cal ctrlp#init(ctrlp#mixed#id()) From 2b973b45bf0cbed2ffe75e412fb74dc68938f4f6 Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Fri, 18 Mar 2016 14:46:27 +0900 Subject: [PATCH 02/12] Document for g:ctrlp_types --- doc/ctrlp.txt | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/doc/ctrlp.txt b/doc/ctrlp.txt index bd4fc095..4cc6017d 100644 --- a/doc/ctrlp.txt +++ b/doc/ctrlp.txt @@ -68,6 +68,7 @@ Overview:~ |ctrlp_open_single_match|.....Automatically accept when only one candidate. |ctrlp_brief_prompt|..........Exit CtrlP on empty prompt by . |ctrlp_match_current_file|....Include current file in match entries. + |ctrlp_types|.................Names of builtin types. MRU mode: |ctrlp_mruf_max|..............Max MRU entries to remember. @@ -454,13 +455,19 @@ This option works well together with |g:ctrlp_open_single_match| *'g:ctrlp_match_current_file'* -Includes the current file in the match entries: +Includes the current file in the match entries: > let g:ctrlp_match_current_file = 1 By default, the current file is excluded from the list. Note: does not apply when |g:ctrlp_match_func| is used. + *'g:ctrlp_types'* +Set this to list of names to customize core types: > + let g:ctrlp_types = ['mru', 'fil'] + +By default, the types are: > + let g:ctrlp_types = ['fil', 'buf', 'mru']. *'g:ctrlp_abbrev'* Define input abbreviations that can be expanded (either internally or visibly) From 1873663698d537a27a1c341a9eb1db14f7a27a59 Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Fri, 18 Mar 2016 16:29:44 +0900 Subject: [PATCH 03/12] Do filter before start --- autoload/ctrlp.vim | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/autoload/ctrlp.vim b/autoload/ctrlp.vim index a30fdbc8..54e986f3 100644 --- a/autoload/ctrlp.vim +++ b/autoload/ctrlp.vim @@ -201,11 +201,11 @@ let s:hlgrps = { \ } " lname, sname of the basic(non-extension) modes -let s:coretypes = [ +let s:coretypes = filter([ \ ['files', 'fil'], \ ['buffers', 'buf'], \ ['mru files', 'mru'], -\ ] +\ ], 'index(g:ctrlp_types, v:val[1])!=-1') " Get the options {{{2 fu! s:opts(...) @@ -1458,7 +1458,6 @@ endf " Statusline {{{2 fu! ctrlp#statusline() if !exists('s:statypes') - let s:coretypes = filter(s:coretypes, 'index(g:ctrlp_types, v:val[1])!=-1') let s:statypes = copy(s:coretypes) if !empty(g:ctrlp_ext_vars) cal map(copy(g:ctrlp_ext_vars), @@ -2404,7 +2403,7 @@ fu! s:execextvar(key) endf fu! s:getextvar(key) - if s:itemtype >= len(s:coretypes) && len(g:ctrlp_ext_vars) + if s:itemtype >= len(s:coretypes) && len(g:ctrlp_ext_vars) > 0 let vars = g:ctrlp_ext_vars[s:itemtype - len(s:coretypes)] retu has_key(vars, a:key) ? vars[a:key] : -1 en @@ -2459,7 +2458,8 @@ endf fu! ctrlp#setlines(...) if a:0 | let s:itemtype = a:1 | en cal s:modevar() - let types = ['ctrlp#files()', 'ctrlp#buffers()', 'ctrlp#mrufiles#list()'] + let inits = {'fil': 'ctrlp#files()', 'buf': 'ctrlp#buffers()', 'mru': 'ctrlp#mrufiles#list()'} + let types = map(copy(g:ctrlp_types), 'inits[v:val]') if !empty(g:ctrlp_ext_vars) cal map(copy(g:ctrlp_ext_vars), 'add(types, v:val["init"])') en From 35a9a06c89760fe05f8b3cd11e8cfea216fa3e75 Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Fri, 18 Mar 2016 16:30:38 +0900 Subject: [PATCH 04/12] Should be max count of builtins --- plugin/ctrlp.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/ctrlp.vim b/plugin/ctrlp.vim index 77acc8f0..fd9b4a00 100644 --- a/plugin/ctrlp.vim +++ b/plugin/ctrlp.vim @@ -15,7 +15,7 @@ if !exists('g:ctrlp_types') en let [g:ctrlp_lines, g:ctrlp_allfiles, g:ctrlp_alltags, g:ctrlp_alldirs, \ g:ctrlp_allmixes, g:ctrlp_buftags, g:ctrlp_ext_vars, g:ctrlp_builtins] - \ = [[], [], [], [], {}, {}, [], len(g:ctrlp_types)] + \ = [[], [], [], [], {}, {}, [], len(g:ctrlp_types)-1] if !exists('g:ctrlp_map') | let g:ctrlp_map = '' | en if !exists('g:ctrlp_cmd') | let g:ctrlp_cmd = 'CtrlP' | en From da3a72a8159828b318086c1b549e5d63a6cf88ac Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Fri, 18 Mar 2016 16:46:23 +0900 Subject: [PATCH 05/12] Validate g:ctrlp_types --- plugin/ctrlp.vim | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/plugin/ctrlp.vim b/plugin/ctrlp.vim index fd9b4a00..9f2bb147 100644 --- a/plugin/ctrlp.vim +++ b/plugin/ctrlp.vim @@ -10,8 +10,11 @@ if ( exists('g:loaded_ctrlp') && g:loaded_ctrlp ) || v:version < 700 || &cp en let g:loaded_ctrlp = 1 +let s:types = ['fil', 'buf', 'mru'] if !exists('g:ctrlp_types') - let g:ctrlp_types = ['fil', 'buf', 'mru'] + let g:ctrlp_types = s:types +el + call filter(g:ctrlp_types, "index(['fil', 'buf', 'mru'], v:val)!=-1") en let [g:ctrlp_lines, g:ctrlp_allfiles, g:ctrlp_alltags, g:ctrlp_alldirs, \ g:ctrlp_allmixes, g:ctrlp_buftags, g:ctrlp_ext_vars, g:ctrlp_builtins] From 70ef32ec55459a81bdee46b2d29b3cad15960d86 Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Sat, 19 Mar 2016 10:39:21 +0900 Subject: [PATCH 06/12] Fix search the type --- autoload/ctrlp.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autoload/ctrlp.vim b/autoload/ctrlp.vim index 54e986f3..2f5950e4 100644 --- a/autoload/ctrlp.vim +++ b/autoload/ctrlp.vim @@ -2469,7 +2469,7 @@ endf " Returns [lname, sname] fu! s:CurTypeName() if s:itemtype < len(s:coretypes) - return s:coretypes[s:itemtype] + return filter(copy(s:coretypes), 'v:val[1]==g:ctrlp_types[s:itemtype]')[0][1] el return [s:getextvar("lname"), s:getextvar('sname')] en From 9075bc1dd89847ab1c9975d1f97f1d9f9ae8f1d8 Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Fri, 18 Mar 2016 14:40:47 +0900 Subject: [PATCH 07/12] Customizable core types --- autoload/ctrlp.vim | 106 +++++++++++++++++++++++++++------------------ plugin/ctrlp.vim | 21 +++++---- 2 files changed, 75 insertions(+), 52 deletions(-) diff --git a/autoload/ctrlp.vim b/autoload/ctrlp.vim index 603e4d95..0e501730 100644 --- a/autoload/ctrlp.vim +++ b/autoload/ctrlp.vim @@ -437,9 +437,9 @@ fu! s:UserCmd(lscmd) en if has('patch-7.4-597') && !(has('win32') || has('win64')) let g:ctrlp_allfiles = systemlist(printf(lscmd, path)) - else + el let g:ctrlp_allfiles = split(system(printf(lscmd, path)), "\n") - end + en if exists('+ssl') && exists('ssl') let &ssl = ssl cal map(g:ctrlp_allfiles, 'tr(v:val, "\\", "/")') @@ -717,7 +717,7 @@ fu! s:PrtBS() if empty(s:prompt[0]) && s:brfprt != 0 cal s:PrtExit() retu - endif + en unl! s:hstgot let [s:prompt[0], s:matches] = [substitute(s:prompt[0], '.$', '', ''), 1] cal s:BuildPrompt(1) @@ -875,12 +875,13 @@ fu! s:PrtFocusMap(char) endf fu! s:PrtClearCache() - if s:itemtype == 0 + let ct = s:curtype() + if ct == 'fil' cal ctrlp#clr() - elsei s:itemtype > 2 + elsei s:itemtype >= len(s:coretypes) cal ctrlp#clr(s:statypes[s:itemtype][1]) en - if s:itemtype == 2 + if ct == 'mru' let g:ctrlp_lines = ctrlp#mrufiles#refresh() el cal ctrlp#setlines() @@ -891,9 +892,10 @@ fu! s:PrtClearCache() endf fu! s:PrtDeleteEnt() - if s:itemtype == 2 + let ct = s:curtype() + if ct == 'mru' cal s:PrtDeleteMRU() - elsei s:itemtype == 1 + elsei ct == 'buf' cal s:delbuf() elsei type(s:getextvar('wipe')) == 1 cal s:delent(s:getextvar('wipe')) @@ -901,7 +903,7 @@ fu! s:PrtDeleteEnt() endf fu! s:PrtDeleteMRU() - if s:itemtype == 2 + if s:curtype() == 'mru' cal s:delent('ctrlp#mrufiles#remove') en endf @@ -1008,7 +1010,7 @@ fu! s:ToggleByFname() endf fu! s:ToggleType(dir) - let max = len(g:ctrlp_ext_vars) + 2 + let max = len(g:ctrlp_ext_vars) + len(s:coretypes) - 1 let next = s:walker(max, s:itemtype, a:dir) cal ctrlp#setlines(next) cal ctrlp#syntax() @@ -1148,7 +1150,7 @@ fu! s:AcceptSelection(action) if subm | if s:SpecInputs(str) | retu | en | en " Get the selected line let line = ctrlp#getcline() - if !subm && !s:itemtype && line == '' && line('.') > s:offset + if !subm && s:curtype() != 'fil' && line == '' && line('.') > s:offset \ && str !~ '\v^(\.\.([\/]\.\.)*[\/]?[.\/]*|/|\\|\?|\@.+)$' cal s:CreateNewFile(md) | retu en @@ -1158,7 +1160,7 @@ fu! s:AcceptSelection(action) let actfunc = s:openfunc[s:ctype] let type = has_key(s:openfunc, 'arg_type') ? s:openfunc['arg_type'] : 'list' el - if s:itemtype < 3 + if s:itemtype < len(s:coretypes) let [actfunc, type] = ['ctrlp#acceptfile', 'dict'] el let [actfunc, exttype] = [s:getextvar('accept'), s:getextvar('act_farg')] @@ -1203,15 +1205,16 @@ fu! s:CreateNewFile(...) endf " * OpenMulti() {{{1 fu! s:MarkToOpen() + let ct = s:curtype() if s:bufnr <= 0 || s:opmul == '0' - \ || ( s:itemtype > 2 && s:getextvar('opmul') != 1 ) + \ || ( s:itemtype >= len(s:coretypes) && s:getextvar('opmul') != 1 ) retu en let line = ctrlp#getcline() " Do not allow to mark modified or current buffer let bufnr = s:bufnrfilpath(line)[0] - if (s:itemtype == 1 && s:delbufcond(bufnr)) + if (ct == 'buf' && s:delbufcond(bufnr)) retu en @@ -1244,7 +1247,7 @@ endf fu! s:OpenMulti(...) let has_marked = exists('s:marked') if ( !has_marked && a:0 ) || s:opmul == '0' || !s:ispath - \ || ( s:itemtype > 2 && s:getextvar('opmul') != 1 ) + \ || ( s:itemtype >= len(s:coretypes) && s:getextvar('opmul') != 1 ) retu -1 en " Get the options @@ -1418,7 +1421,8 @@ fu! s:shortest(lens) endf fu! s:mixedsort(...) - if s:itemtype == 1 + let ct = s:curtype() + if ct == 'buf' let pat = '[\/]\?\[\d\+\*No Name\]$' if a:1 =~# pat && a:2 =~# pat | retu 0 elsei a:1 =~# pat | retu 1 @@ -1429,10 +1433,10 @@ fu! s:mixedsort(...) let ms = [] if s:res_count < 21 let ms += [s:compfnlen(a:1, a:2)] - if s:itemtype !~ '^[12]$' | let ms += [s:comptime(a:1, a:2)] | en + if ct !~ '^\(buf\|mru\)$' | let ms += [s:comptime(a:1, a:2)] | en if !s:itemtype | let ms += [s:comparent(a:1, a:2)] | en en - if s:itemtype =~ '^[12]$' + if ct =~ '^\(buf\|mru\)$' let ms += [s:compmref(a:1, a:2)] let cln = cml ? cln : 0 en @@ -1457,6 +1461,7 @@ endf " Statusline {{{2 fu! ctrlp#statusline() if !exists('s:statypes') + let s:coretypes = filter(s:coretypes, 'index(g:ctrlp_types, v:val[1])!=-1') let s:statypes = copy(s:coretypes) if !empty(g:ctrlp_ext_vars) cal map(copy(g:ctrlp_ext_vars), @@ -1516,7 +1521,8 @@ endf " Line formatting {{{3 fu! s:formatline(str) let str = a:str - if s:itemtype == 1 + let ct = s:curtype() + if ct == 'buf' let bufnr = s:bufnrfilpath(str)[0] let parts = s:bufparts(bufnr) let str = printf('%'.s:bufnr_width.'s', bufnr) @@ -1536,7 +1542,7 @@ fu! s:formatline(str) en en en - let cond = s:itemtype != 1 &&s:ispath && ( s:winw - 4 ) < s:strwidth(str) + let cond = ct != 'buf' &&s:ispath && ( s:winw - 4 ) < s:strwidth(str) retu s:lineprefix.( cond ? s:pathshorten(str) : str ) endf @@ -1591,7 +1597,7 @@ fu! s:lash(...) endf fu! s:ispathitem() - retu s:itemtype < 3 || ( s:itemtype > 2 && s:getextvar('type') == 'path' ) + retu s:itemtype < len(s:coretypes) || s:getextvar('type') == 'path' endf fu! ctrlp#igncwd(cwd) @@ -1625,11 +1631,11 @@ fu! ctrlp#dirnfile(entries) endf fu! s:usrign(item, type) - if s:igntype == 1 | retu a:item =~ s:usrign | end + if s:igntype == 1 | retu a:item =~ s:usrign | en if s:igntype == 2 if call(s:usrign, [a:item, a:type]) retu 1 - end + en elsei s:igntype == 4 if has_key(s:usrign, a:type) && s:usrign[a:type] != '' \ && a:item =~ s:usrign[a:type] @@ -1637,8 +1643,8 @@ fu! s:usrign(item, type) elsei has_key(s:usrign, 'func') && s:usrign['func'] != '' \ && call(s:usrign['func'], [a:item, a:type]) retu 1 - end - end + en + en retu 0 endf @@ -1740,7 +1746,7 @@ fu! ctrlp#syntax() sy match CtrlPLinePre '^>' en - if s:itemtype == 1 && s:has_conceal + if s:curtype() == 'buf' && s:has_conceal sy region CtrlPBufferNr matchgroup=CtrlPLinePre start='^>\s\+' end='\s' sy region CtrlPBufferInd concealends matchgroup=Ignore start='' end='' sy region CtrlPBufferRegion concealends matchgroup=Ignore start='' end='' @@ -1778,7 +1784,7 @@ fu! s:highlight(pat, grp) if s:byfname() " Make sure there are no slashes in our match let beginning = beginning.'\([^\/]*$\)\@=' - end + en for i in range(len(chars)) " Surround our current target letter with \zs and \ze so it only @@ -1790,14 +1796,14 @@ fu! s:highlight(pat, grp) if i == 0 let charcopy[i] = '\zs'.charcopy[i].'\ze' let middle = join(charcopy, '.\{-}') - else + el let before = join(charcopy[0:i-1], '.\{-}') let after = join(charcopy[i+1:-1], '.\{-}') let c = charcopy[i] " for abc, match either ab.\{-}c or a.*b.\{-}c in that order let cpat = '\(\zs'.c.'\|'.'.*\zs'.c.'\)\ze.*' let middle = before.cpat.after - endif + en " Now we matchadd for each letter, the basic form being: " ^.*\zsx\ze.*$, but with our pattern we built above for the letter, @@ -2106,12 +2112,13 @@ fu! s:modevar() endf fu! s:nosort() - retu s:matcher != {} || s:nolim == 1 || ( s:itemtype == 2 && s:mrudef ) - \ || ( s:itemtype =~ '\v^(1|2)$' && s:prompt == ['', '', ''] ) || !s:dosort + let ct = s:curtype() + retu s:matcher != {} || s:nolim == 1 || ( ct == 'mru' && s:mrudef ) + \ || ( ct =~ '^\(buf\|mru\)$' && s:prompt == ['', '', ''] ) || !s:dosort endf fu! s:byfname() - retu s:itemtype != 1 && s:ispath && s:byfname + retu s:curtype() != 'buf' && s:ispath && s:byfname endf fu! s:narrowable() @@ -2333,13 +2340,17 @@ fu! s:buildpat(lst) retu pat endf +fu! s:curtype() + return s:CurTypeName()[1] +endf + fu! s:mfunc() let mfunc = 'match' if s:byfname() let mfunc = 's:matchfname' - elsei s:itemtype == 1 + elsei s:curtype() == 'buf' let mfunc = 's:matchbuf' - elsei s:itemtype > 2 + elsei s:itemtype >= len(s:coretypes) let matchtypes = { 'tabs': 's:matchtabs', 'tabe': 's:matchtabe' } if has_key(matchtypes, s:matchtype) let mfunc = matchtypes[s:matchtype] @@ -2398,7 +2409,7 @@ fu! s:insertcache(str) endf " Extensions {{{2 fu! s:mtype() - retu s:itemtype > 2 ? s:getextvar('type') : 'path' + retu s:itemtype >= len(s:coretypes) ? s:getextvar('type') : 'path' endf fu! s:execextvar(key) @@ -2409,8 +2420,8 @@ fu! s:execextvar(key) endf fu! s:getextvar(key) - if s:itemtype > 2 - let vars = g:ctrlp_ext_vars[s:itemtype - 3] + if s:itemtype >= len(s:coretypes) && len(g:ctrlp_ext_vars) + let vars = g:ctrlp_ext_vars[s:itemtype - len(s:coretypes)] retu has_key(vars, a:key) ? vars[a:key] : -1 en retu get(g:, 'ctrlp_' . s:matchtype . '_' . a:key, -1) @@ -2473,11 +2484,11 @@ endf " Returns [lname, sname] fu! s:CurTypeName() - if s:itemtype < 3 + if s:itemtype < len(s:coretypes) return s:coretypes[s:itemtype] - else + el return [s:getextvar("lname"), s:getextvar('sname')] - endif + en endfu fu! s:ExitIfSingleCandidate() @@ -2485,7 +2496,7 @@ fu! s:ExitIfSingleCandidate() call s:AcceptSelection('e') call ctrlp#exit() return 1 - endif + en return 0 endfu @@ -2498,14 +2509,23 @@ fu! ctrlp#init(type, ...) cal s:SetWD(a:0 ? a:1 : {}) cal s:MapNorms() cal s:MapSpecs() - cal ctrlp#setlines(s:settype(a:type)) + if type(a:type) == 0 + let type = a:type + el + let type = index(g:ctrlp_types, a:type) + if type == -1 + call ctrlp#exit() + retu + en + en + cal ctrlp#setlines(s:settype(type)) cal ctrlp#syntax() cal s:SetDefTxt() let curName = s:CurTypeName() let shouldExitSingle = index(s:opensingle, curName[0])>=0 || index(s:opensingle, curName[1])>=0 if shouldExitSingle && s:ExitIfSingleCandidate() return 0 - endif + en cal s:BuildPrompt(1) if s:keyloop | cal s:KeyLoop() | en return 1 diff --git a/plugin/ctrlp.vim b/plugin/ctrlp.vim index 5f5303f1..77acc8f0 100644 --- a/plugin/ctrlp.vim +++ b/plugin/ctrlp.vim @@ -10,17 +10,20 @@ if ( exists('g:loaded_ctrlp') && g:loaded_ctrlp ) || v:version < 700 || &cp en let g:loaded_ctrlp = 1 +if !exists('g:ctrlp_types') + let g:ctrlp_types = ['fil', 'buf', 'mru'] +en let [g:ctrlp_lines, g:ctrlp_allfiles, g:ctrlp_alltags, g:ctrlp_alldirs, \ g:ctrlp_allmixes, g:ctrlp_buftags, g:ctrlp_ext_vars, g:ctrlp_builtins] - \ = [[], [], [], [], {}, {}, [], 2] + \ = [[], [], [], [], {}, {}, [], len(g:ctrlp_types)] if !exists('g:ctrlp_map') | let g:ctrlp_map = '' | en if !exists('g:ctrlp_cmd') | let g:ctrlp_cmd = 'CtrlP' | en -com! -n=? -com=dir CtrlP cal ctrlp#init(0, { 'dir': }) -com! -n=? -com=dir CtrlPMRUFiles cal ctrlp#init(2, { 'dir': }) +com! -n=? -com=dir CtrlP cal ctrlp#init('fil', { 'dir': }) +com! -n=? -com=dir CtrlPMRUFiles cal ctrlp#init('mru', { 'dir': }) -com! -bar CtrlPBuffer cal ctrlp#init(1) +com! -bar CtrlPBuffer cal ctrlp#init('buf') com! -n=? CtrlPLastMode cal ctrlp#init(-1, { 'args': }) com! -bar CtrlPClearCache cal ctrlp#clr() @@ -29,9 +32,9 @@ com! -bar CtrlPClearAllCaches cal ctrlp#clra() com! -bar ClearCtrlPCache cal ctrlp#clr() com! -bar ClearAllCtrlPCaches cal ctrlp#clra() -com! -bar CtrlPCurWD cal ctrlp#init(0, { 'mode': '' }) -com! -bar CtrlPCurFile cal ctrlp#init(0, { 'mode': 'c' }) -com! -bar CtrlPRoot cal ctrlp#init(0, { 'mode': 'r' }) +com! -bar CtrlPCurWD cal ctrlp#init('fil', { 'mode': '' }) +com! -bar CtrlPCurFile cal ctrlp#init('fil', { 'mode': 'c' }) +com! -bar CtrlPRoot cal ctrlp#init('fil', { 'mode': 'r' }) exe 'nn (ctrlp) :'.g:ctrlp_cmd.'' @@ -55,10 +58,10 @@ com! -bar CtrlPRTS cal ctrlp#init(ctrlp#rtscript#id()) com! -bar CtrlPUndo cal ctrlp#init(ctrlp#undo#id()) com! -n=? -com=buffer CtrlPLine - \ cal ctrlp#init(ctrlp#line#cmd(1, )) + \ cal ctrlp#init(ctrlp#line#cmd('buf', )) com! -n=? -com=buffer CtrlPChange - \ cal ctrlp#init(ctrlp#changes#cmd(0, )) + \ cal ctrlp#init(ctrlp#changes#cmd('fil', )) com! -bar CtrlPChangeAll cal ctrlp#init(ctrlp#changes#cmd(1)) com! -bar CtrlPMixed cal ctrlp#init(ctrlp#mixed#id()) From 6867046a05b6fad36ff758d3cbc70765cedce958 Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Fri, 18 Mar 2016 14:46:27 +0900 Subject: [PATCH 08/12] Document for g:ctrlp_types --- doc/ctrlp.txt | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/doc/ctrlp.txt b/doc/ctrlp.txt index bd4fc095..4cc6017d 100644 --- a/doc/ctrlp.txt +++ b/doc/ctrlp.txt @@ -68,6 +68,7 @@ Overview:~ |ctrlp_open_single_match|.....Automatically accept when only one candidate. |ctrlp_brief_prompt|..........Exit CtrlP on empty prompt by . |ctrlp_match_current_file|....Include current file in match entries. + |ctrlp_types|.................Names of builtin types. MRU mode: |ctrlp_mruf_max|..............Max MRU entries to remember. @@ -454,13 +455,19 @@ This option works well together with |g:ctrlp_open_single_match| *'g:ctrlp_match_current_file'* -Includes the current file in the match entries: +Includes the current file in the match entries: > let g:ctrlp_match_current_file = 1 By default, the current file is excluded from the list. Note: does not apply when |g:ctrlp_match_func| is used. + *'g:ctrlp_types'* +Set this to list of names to customize core types: > + let g:ctrlp_types = ['mru', 'fil'] + +By default, the types are: > + let g:ctrlp_types = ['fil', 'buf', 'mru']. *'g:ctrlp_abbrev'* Define input abbreviations that can be expanded (either internally or visibly) From d83bddc2f10cdc63a7e93dcfd0adab048ba21759 Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Fri, 18 Mar 2016 16:29:44 +0900 Subject: [PATCH 09/12] Do filter before start --- autoload/ctrlp.vim | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/autoload/ctrlp.vim b/autoload/ctrlp.vim index 0e501730..a8ddff04 100644 --- a/autoload/ctrlp.vim +++ b/autoload/ctrlp.vim @@ -201,11 +201,11 @@ let s:hlgrps = { \ } " lname, sname of the basic(non-extension) modes -let s:coretypes = [ +let s:coretypes = filter([ \ ['files', 'fil'], \ ['buffers', 'buf'], \ ['mru files', 'mru'], -\ ] +\ ], 'index(g:ctrlp_types, v:val[1])!=-1') " Get the options {{{2 fu! s:opts(...) @@ -1461,7 +1461,6 @@ endf " Statusline {{{2 fu! ctrlp#statusline() if !exists('s:statypes') - let s:coretypes = filter(s:coretypes, 'index(g:ctrlp_types, v:val[1])!=-1') let s:statypes = copy(s:coretypes) if !empty(g:ctrlp_ext_vars) cal map(copy(g:ctrlp_ext_vars), @@ -2420,7 +2419,7 @@ fu! s:execextvar(key) endf fu! s:getextvar(key) - if s:itemtype >= len(s:coretypes) && len(g:ctrlp_ext_vars) + if s:itemtype >= len(s:coretypes) && len(g:ctrlp_ext_vars) > 0 let vars = g:ctrlp_ext_vars[s:itemtype - len(s:coretypes)] retu has_key(vars, a:key) ? vars[a:key] : -1 en @@ -2475,7 +2474,8 @@ endf fu! ctrlp#setlines(...) if a:0 | let s:itemtype = a:1 | en cal s:modevar() - let types = ['ctrlp#files()', 'ctrlp#buffers()', 'ctrlp#mrufiles#list()'] + let inits = {'fil': 'ctrlp#files()', 'buf': 'ctrlp#buffers()', 'mru': 'ctrlp#mrufiles#list()'} + let types = map(copy(g:ctrlp_types), 'inits[v:val]') if !empty(g:ctrlp_ext_vars) cal map(copy(g:ctrlp_ext_vars), 'add(types, v:val["init"])') en From ac8b3b6a1cf1772b44d39f9e4fe5001fb0e8a678 Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Fri, 18 Mar 2016 16:30:38 +0900 Subject: [PATCH 10/12] Should be max count of builtins --- plugin/ctrlp.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/ctrlp.vim b/plugin/ctrlp.vim index 77acc8f0..fd9b4a00 100644 --- a/plugin/ctrlp.vim +++ b/plugin/ctrlp.vim @@ -15,7 +15,7 @@ if !exists('g:ctrlp_types') en let [g:ctrlp_lines, g:ctrlp_allfiles, g:ctrlp_alltags, g:ctrlp_alldirs, \ g:ctrlp_allmixes, g:ctrlp_buftags, g:ctrlp_ext_vars, g:ctrlp_builtins] - \ = [[], [], [], [], {}, {}, [], len(g:ctrlp_types)] + \ = [[], [], [], [], {}, {}, [], len(g:ctrlp_types)-1] if !exists('g:ctrlp_map') | let g:ctrlp_map = '' | en if !exists('g:ctrlp_cmd') | let g:ctrlp_cmd = 'CtrlP' | en From e6291bc022438e72f571e8cdac3001c97372b596 Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Fri, 18 Mar 2016 16:46:23 +0900 Subject: [PATCH 11/12] Validate g:ctrlp_types --- plugin/ctrlp.vim | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/plugin/ctrlp.vim b/plugin/ctrlp.vim index fd9b4a00..9f2bb147 100644 --- a/plugin/ctrlp.vim +++ b/plugin/ctrlp.vim @@ -10,8 +10,11 @@ if ( exists('g:loaded_ctrlp') && g:loaded_ctrlp ) || v:version < 700 || &cp en let g:loaded_ctrlp = 1 +let s:types = ['fil', 'buf', 'mru'] if !exists('g:ctrlp_types') - let g:ctrlp_types = ['fil', 'buf', 'mru'] + let g:ctrlp_types = s:types +el + call filter(g:ctrlp_types, "index(['fil', 'buf', 'mru'], v:val)!=-1") en let [g:ctrlp_lines, g:ctrlp_allfiles, g:ctrlp_alltags, g:ctrlp_alldirs, \ g:ctrlp_allmixes, g:ctrlp_buftags, g:ctrlp_ext_vars, g:ctrlp_builtins] From 3294f1a77ef9f24de8eb9a9b28427a9557736909 Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Sat, 19 Mar 2016 10:39:21 +0900 Subject: [PATCH 12/12] Fix search the type --- autoload/ctrlp.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autoload/ctrlp.vim b/autoload/ctrlp.vim index a8ddff04..5371df52 100644 --- a/autoload/ctrlp.vim +++ b/autoload/ctrlp.vim @@ -2485,7 +2485,7 @@ endf " Returns [lname, sname] fu! s:CurTypeName() if s:itemtype < len(s:coretypes) - return s:coretypes[s:itemtype] + return filter(copy(s:coretypes), 'v:val[1]==g:ctrlp_types[s:itemtype]')[0][1] el return [s:getextvar("lname"), s:getextvar('sname')] en