Skip to content

Commit 6bca877

Browse files
authored
Merge pull request #516 from ishitaku5522/master
Re-Implement g:ctrlp_user_command_async
2 parents 2e773fd + 0628989 commit 6bca877

File tree

3 files changed

+84
-10
lines changed

3 files changed

+84
-10
lines changed

autoload/ctrlp.vim

Lines changed: 82 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ fu! s:Open()
330330
endf
331331

332332
fu! s:Close()
333+
cal s:async_glob_abort()
333334
cal s:buffunc(0)
334335
if winnr('$') == 1
335336
bw!
@@ -434,9 +435,62 @@ fu! s:GlobPath(dirs, depth)
434435
en
435436
endf
436437

437-
fu! ctrlp#addfile(ch, file)
438-
call add(g:ctrlp_allfiles, a:file)
439-
cal s:BuildPrompt(1)
438+
fu! s:async_glob_update_progress(timer)
439+
let s:must_wait = 0
440+
if exists('s:focus') && get(s:, 'setlines_post_ended', 0)
441+
cal s:ForceUpdate()
442+
en
443+
if exists('s:timer')
444+
sil! cal ctrlp#statusline()
445+
endif
446+
447+
if !exists('s:job')
448+
call s:stop_timer_if_exists()
449+
endif
450+
endf
451+
452+
fu! s:async_glob_on_stdout(job, data, ...)
453+
if type(a:data) ==# type([])
454+
call extend(g:ctrlp_allfiles, filter(a:data, 'v:val !=# ""'))
455+
else
456+
call add(g:ctrlp_allfiles, a:data)
457+
endif
458+
endf
459+
460+
fu! s:async_glob_on_exit(...)
461+
let s:must_wait = 0
462+
if exists('s:job')
463+
unlet s:job
464+
endif
465+
cal s:stop_timer_if_exists()
466+
if exists('s:focus') && get(s:, 'setlines_post_ended', 0)
467+
sil! cal ctrlp#statusline()
468+
cal s:ForceUpdate()
469+
en
470+
endf
471+
472+
fu! s:async_glob_abort()
473+
cal s:stop_job_if_exists()
474+
cal s:stop_timer_if_exists()
475+
cal s:ForceUpdate()
476+
endf
477+
478+
fu! s:stop_timer_if_exists()
479+
if exists('s:timer')
480+
call timer_stop(s:timer)
481+
unlet s:timer
482+
en
483+
endf
484+
485+
fu! s:stop_job_if_exists()
486+
if exists('s:job')
487+
if !has('nvim')
488+
cal job_stop(s:job)
489+
else
490+
cal jobstop(s:job)
491+
endif
492+
unlet s:job
493+
en
440494
endf
441495

442496
fu! s:safe_printf(format, ...)
@@ -462,12 +516,26 @@ fu! s:UserCmd(lscmd)
462516
if (has('win32') || has('win64')) && match(&shell, 'sh') != -1
463517
let path = tr(path, '\', '/')
464518
en
465-
if s:usrcmdasync && v:version >= 800 && exists('*job_start')
466-
if exists('s:job')
467-
call job_stop(s:job)
468-
en
519+
if s:usrcmdasync && (v:version >= 800 || has('nvim')) && (exists('*job_start') || exists('*jobstart'))
520+
cal s:stop_job_if_exists()
469521
let g:ctrlp_allfiles = []
470-
let s:job = job_start([&shell, &shellcmdflag, printf(lscmd, path)], {'callback': 'ctrlp#addfile'})
522+
let s:must_wait = 1
523+
let argv = [&shell, &shellcmdflag, printf(lscmd, path)]
524+
if !has('nvim')
525+
let s:job = job_start(argv, {
526+
\ 'out_cb': function('s:async_glob_on_stdout'),
527+
\ 'exit_cb': function('s:async_glob_on_exit')
528+
\ })
529+
else
530+
let s:job = jobstart(argv, {
531+
\ 'on_stdout': function('s:async_glob_on_stdout'),
532+
\ 'on_exit': function('s:async_glob_on_exit')
533+
\ })
534+
endif
535+
let s:timer = timer_start(250, function('s:async_glob_update_progress'), {'repeat': -1})
536+
while s:must_wait
537+
sleep 50m
538+
endwhile
471539
elsei has('patch-7.4-597') && !(has('win32') || has('win64'))
472540
let g:ctrlp_allfiles = systemlist(s:safe_printf(lscmd, path))
473541
el
@@ -1054,6 +1122,7 @@ fu! s:ToggleByFname()
10541122
endf
10551123

10561124
fu! s:ToggleType(dir)
1125+
cal s:async_glob_abort()
10571126
let max = len(g:ctrlp_ext_vars) + len(s:coretypes) - 1
10581127
let next = s:walker(max, s:itemtype, a:dir)
10591128
cal ctrlp#setlines(next)
@@ -1563,6 +1632,9 @@ fu! ctrlp#statusline()
15631632
let slider = ' <'.prv.'>={'.item.'}=<'.nxt.'>'
15641633
let dir = ' %=%<%#CtrlPMode2# %{getcwd()} %*'
15651634
let &l:stl = focus.byfname.regex.slider.marked.dir
1635+
if exists('s:timer')
1636+
let &l:stl = '%#CtrlPStats# '.len(g:ctrlp_allfiles).' '.&l:stl
1637+
en
15661638
en
15671639
endf
15681640

@@ -2571,6 +2643,7 @@ endf
25712643
fu! s:setlines_pre(...)
25722644
if a:0 | let s:itemtype = a:1 | en
25732645
cal s:modevar()
2646+
let s:setlines_post_ended = 0
25742647
let g:ctrlp_lines = []
25752648
endf
25762649

@@ -2581,6 +2654,7 @@ fu! s:setlines_post()
25812654
cal map(copy(g:ctrlp_ext_vars), 'add(types, v:val["init"])')
25822655
en
25832656
let g:ctrlp_lines = eval(types[s:itemtype])
2657+
let s:setlines_post_ended = 1
25842658
endf
25852659

25862660
fu! ctrlp#setlines(...)

doc/ctrlp.cnx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1439,7 +1439,7 @@ Before 2016/11/28~
14391439
+ 新命令: |YankLine()| 来复制整个文件。
14401440
+ 新选项: |g:ctrlp_types| 来选择內建类型。
14411441
+ 新特性: 异步在新线程中调用 |g:ctrlp_user_command| 。 设置
1442-
|g:user_command_async| 为1来启用。
1442+
|g:ctrlp_user_command_async| 为1来启用。
14431443
+ 为 delphi, rust 和 golang提供buffertag支持。
14441444
+ 新选项: |g:ctrlp_brief_prompt|,
14451445
|g:match_current_file|,

doc/ctrlp.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1505,7 +1505,7 @@ Before 2016/11/28~
15051505
+ New command: |YankLine()| to yank current line.
15061506
+ New option: |g:ctrlp_types| to select builtin modes.
15071507
+ New feature: asynchronized spawn of |g:ctrlp_user_command|. This enable
1508-
with set |g:user_command_async| to 1.
1508+
with set |g:ctrlp_user_command_async| to 1.
15091509
+ Support buffertag for delphi, rust and golang.
15101510
+ New option: |g:ctrlp_brief_prompt|,
15111511
|g:match_current_file|,

0 commit comments

Comments
 (0)