Skip to content

Commit e927848

Browse files
committed
Allow x_func function arguments to be passed in a Dictionary
1 parent 693f4d6 commit e927848

File tree

2 files changed

+90
-22
lines changed

2 files changed

+90
-22
lines changed

autoload/ctrlp.vim

+69-22
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,16 @@ fu! s:MatchedItems(items, pat, limit)
472472
let exc = exists('s:crfilerel') ? s:crfilerel : ''
473473
let items = s:narrowable() ? s:matched + s:mdata[3] : a:items
474474
if s:matcher != {}
475-
let argms = [items, a:pat, a:limit, s:mmode(), s:ispath, exc, s:regexp]
475+
let argms =
476+
\ has_key(s:matcher, 'arg_type') && s:matcher['arg_type'] == 'dict' ? [{
477+
\ 'items': items,
478+
\ 'str': a:pat,
479+
\ 'limit': a:limit,
480+
\ 'mmode': s:mmode(),
481+
\ 'ispath': s:ispath,
482+
\ 'crfile': exc,
483+
\ 'regex': s:regexp,
484+
\ }] : [items, a:pat, a:limit, s:mmode(), s:ispath, exc, s:regexp]
476485
let lines = call(s:matcher['match'], argms, s:matcher)
477486
el
478487
let lines = s:MatchIt(items, a:pat, a:limit, exc)
@@ -961,22 +970,29 @@ fu! s:SetWD(args)
961970
en
962971
endf
963972
" * AcceptSelection() {{{1
964-
fu! ctrlp#acceptfile(mode, line, ...)
965-
let [md, useb] = [a:mode, 0]
966-
if !type(a:line)
967-
let [filpath, bufnr, useb] = [a:line, a:line, 1]
973+
fu! ctrlp#acceptfile(...)
974+
let useb = 0
975+
if a:0 == 1 && type(a:1) == 4
976+
let [md, line] = [a:1['action'], a:1['line']]
977+
let atl = has_key(a:1, 'tail') ? a:1['tail'] : ''
968978
el
969-
let filpath = fnamemodify(a:line, ':p')
970-
if s:nonamecond(a:line, filpath)
971-
let bufnr = str2nr(matchstr(a:line, '[\/]\?\[\zs\d\+\ze\*No Name\]$'))
979+
let [md, line] = [a:1, a:2]
980+
let atl = a:0 > 2 ? a:3 : ''
981+
en
982+
if !type(line)
983+
let [filpath, bufnr, useb] = [line, line, 1]
984+
el
985+
let filpath = fnamemodify(line, ':p')
986+
if s:nonamecond(line, filpath)
987+
let bufnr = str2nr(matchstr(line, '[\/]\?\[\zs\d\+\ze\*No Name\]$'))
972988
let [filpath, useb] = [bufnr, 1]
973989
el
974990
let bufnr = bufnr('^'.filpath.'$')
975991
en
976992
en
977993
cal s:PrtExit()
978994
let tail = s:tail()
979-
let j2l = a:0 ? a:1 : matchstr(tail, '^ +\zs\d\+$')
995+
let j2l = atl != '' ? atl : matchstr(tail, '^ +\zs\d\+$')
980996
if ( s:jmptobuf =~ md || ( s:jmptobuf && md =~ '[et]' ) ) && bufnr > 0
981997
\ && !( md == 'e' && bufnr == bufnr('%') )
982998
let [jmpb, bufwinnr] = [1, bufwinnr(bufnr)]
@@ -1004,7 +1020,7 @@ fu! ctrlp#acceptfile(mode, line, ...)
10041020
" Reset &switchbuf option
10051021
let [swb, &swb] = [&swb, '']
10061022
" Open new window/buffer
1007-
let [fid, tail] = [( useb ? bufnr : filpath ), ( a:0 ? ' +'.a:1 : tail )]
1023+
let [fid, tail] = [( useb ? bufnr : filpath ), ( atl != '' ? ' +'.atl : tail )]
10081024
let args = [cmd, fid, tail, 1, [useb, j2l]]
10091025
cal call('s:openfile', args)
10101026
let &swb = swb
@@ -1035,24 +1051,34 @@ fu! s:SpecInputs(str)
10351051
retu 0
10361052
endf
10371053

1038-
fu! s:AcceptSelection(mode)
1039-
if a:mode != 'e' && s:OpenMulti(a:mode) != -1 | retu | en
1054+
fu! s:AcceptSelection(action)
1055+
let [md, icr] = [a:action[0], match(a:action, 'r') >= 0]
1056+
let subm = icr || ( !icr && md == 'e' )
1057+
if !subm && s:OpenMulti(md) != -1 | retu | en
10401058
let str = s:getinput()
1041-
if a:mode == 'e' | if s:SpecInputs(str) | retu | en | en
1059+
if subm | if s:SpecInputs(str) | retu | en | en
10421060
" Get the selected line
10431061
let line = ctrlp#getcline()
1044-
if a:mode != 'e' && !s:itemtype && line == ''
1062+
if !subm && !s:itemtype && line == '' && line('.') > s:offset
10451063
\ && str !~ '\v^(\.\.([\/]\.\.)*[\/]?[.\/]*|/|\\|\?|\@.+)$'
1046-
cal s:CreateNewFile(a:mode) | retu
1064+
cal s:CreateNewFile(md) | retu
10471065
en
10481066
if empty(line) | retu | en
10491067
" Do something with it
10501068
if s:openfunc != {} && has_key(s:openfunc, s:ctype)
10511069
let actfunc = s:openfunc[s:ctype]
1070+
let type = has_key(s:openfunc, 'arg_type') ? s:openfunc['arg_type'] : 'list'
10521071
el
1053-
let actfunc = s:itemtype < 3 ? 'ctrlp#acceptfile' : s:getextvar('accept')
1072+
if s:itemtype < 3
1073+
let [actfunc, type] = ['ctrlp#acceptfile', 'dict']
1074+
el
1075+
let [actfunc, exttype] = [s:getextvar('accept'), s:getextvar('act_farg')]
1076+
let type = exttype == 'dict' ? exttype : 'list'
1077+
en
10541078
en
1055-
cal call(actfunc, [a:mode, line])
1079+
let actargs = type == 'dict' ? [{ 'action': md, 'line': line, 'icr': icr }]
1080+
\ : [md, line]
1081+
cal call(actfunc, actargs)
10561082
endf
10571083
" - CreateNewFile() {{{1
10581084
fu! s:CreateNewFile(...)
@@ -1211,7 +1237,10 @@ fu! s:OpenNoMarks(md, line)
12111237
cal s:remarksigns()
12121238
cal s:BuildPrompt(0)
12131239
elsei a:md == 'x'
1214-
cal call(s:openfunc[s:ctype], [a:md, a:line], s:openfunc)
1240+
let type = has_key(s:openfunc, 'arg_type') ? s:openfunc['arg_type'] : 'dict'
1241+
let argms = type == 'dict' ? [{ 'action': a:md, 'line': a:line }]
1242+
\ : [a:md, a:line]
1243+
cal call(s:openfunc[s:ctype], argms, s:openfunc)
12151244
elsei a:md == 'd'
12161245
let dir = fnamemodify(a:line, ':h')
12171246
if isdirectory(dir)
@@ -1352,8 +1381,17 @@ fu! ctrlp#statusline()
13521381
let marked = s:opmul != '0' ?
13531382
\ exists('s:marked') ? ' <'.s:dismrk().'>' : ' <->' : ''
13541383
if s:status != {}
1355-
let args = [focus, byfname, s:regexp, prv, s:ctype, nxt, marked]
1356-
let &l:stl = call(s:status['main'], args, s:status)
1384+
let argms =
1385+
\ has_key(s:status, 'arg_type') && s:status['arg_type'] == 'dict' ? [{
1386+
\ 'focus': focus,
1387+
\ 'byfname': byfname,
1388+
\ 'regex': s:regexp,
1389+
\ 'prev': prv,
1390+
\ 'item': s:ctype,
1391+
\ 'next': nxt,
1392+
\ 'marked': marked,
1393+
\ }] : [focus, byfname, s:regexp, prv, s:ctype, nxt, marked]
1394+
let &l:stl = call(s:status['main'], argms, s:status)
13571395
el
13581396
let item = '%#CtrlPMode1# '.s:ctype.' %*'
13591397
let focus = '%#CtrlPMode2# '.focus.' %*'
@@ -1373,8 +1411,13 @@ endf
13731411
fu! ctrlp#progress(enum, ...)
13741412
if has('macunix') || has('mac') | sl 1m | en
13751413
let txt = a:0 ? '(press ctrl-c to abort)' : ''
1376-
let &l:stl = s:status != {} ? call(s:status['prog'], [a:enum], s:status)
1377-
\ : '%#CtrlPStats# '.a:enum.' %* '.txt.'%=%<%#CtrlPMode2# %{getcwd()} %*'
1414+
if s:status != {}
1415+
let argms = has_key(s:status, 'arg_type') && s:status['arg_type'] == 'dict'
1416+
\ ? [{ 'str': a:enum }] : [a:enum]
1417+
let &l:stl = call(s:status['prog'], argms, s:status)
1418+
el
1419+
let &l:stl = '%#CtrlPStats# '.a:enum.' %* '.txt.'%=%<%#CtrlPMode2# %{getcwd()} %*'
1420+
en
13781421
redraws
13791422
endf
13801423
" *** Paths {{{2
@@ -2178,6 +2221,10 @@ endf
21782221
fu! ctrlp#call(func, ...)
21792222
retu call(a:func, a:000)
21802223
endf
2224+
2225+
fu! ctrlp#getvar(var)
2226+
retu {a:var}
2227+
endf
21812228
"}}}1
21822229
" * Initialization {{{1
21832230
fu! ctrlp#setlines(...)

doc/ctrlp.txt

+21
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,27 @@ Structure of the function: >
743743
endfunction
744744
<
745745

746+
Note: you can extend any of the above options with { 'arg_type': 'dict' } to
747+
enable passing all the function arguments in a single Dictionary argument. Use
748+
the existing argument names as keys in this Dictionary.
749+
750+
Example: >
751+
let g:ctrlp_status_func = {
752+
\ 'arg_type' : 'dict',
753+
\ 'enter': 'Function_Name_1',
754+
\ 'exit': 'Function_Name_2',
755+
\ }
756+
757+
function! Function_Name_1(dict)
758+
" where dict == {
759+
" \ 'focus': value,
760+
" \ 'byfname': value,
761+
" \ 'regex': value,
762+
" \ ...
763+
" }
764+
endfunction
765+
<
766+
746767
===============================================================================
747768
COMMANDS *ctrlp-commands*
748769

0 commit comments

Comments
 (0)