Skip to content

Commit b0d818f

Browse files
author
skywind3000
committed
new -once option to buffer output and flush them at once when job is finished (behave like vim-dispatch)
1 parent 6dfa350 commit b0d818f

File tree

1 file changed

+52
-15
lines changed

1 file changed

+52
-15
lines changed

plugin/asyncrun.vim

Lines changed: 52 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
" Maintainer: skywind3000 (at) gmail.com, 2016-2021
44
" Homepage: https://github.com/skywind3000/asyncrun.vim
55
"
6-
" Last Modified: 2021/12/20 03:43
6+
" Last Modified: 2021/12/20 04:44
77
"
88
" Run shell command in background and output to quickfix:
99
" :AsyncRun[!] [options] {cmd} ...
@@ -389,14 +389,15 @@ function! s:AsyncRun_Job_CheckScroll()
389389
endfunc
390390

391391
" invoked on timer or finished
392-
function! s:AsyncRun_Job_Update(count)
392+
function! s:AsyncRun_Job_Update(count, ...)
393393
let l:iconv = (g:asyncrun_encs != "")? 1 : 0
394394
let l:count = 0
395395
let l:total = 0
396396
let l:empty = [{'text':''}]
397397
let l:check = s:AsyncRun_Job_CheckScroll()
398398
let l:efm1 = &g:efm
399399
let l:efm2 = &l:efm
400+
let once = (a:0 < 1)? get(g:, 'asyncrun_once', 0) : (a:1)
400401
if g:asyncrun_encs == &encoding
401402
let l:iconv = 0
402403
endif
@@ -408,6 +409,9 @@ function! s:AsyncRun_Job_Update(count)
408409
if s:async_info.raw == 1
409410
let l:raw = 1
410411
endif
412+
if once != 0
413+
let array = []
414+
endif
411415
while s:async_tail < s:async_head
412416
let l:text = s:async_output[s:async_tail]
413417
if l:iconv != 0
@@ -417,18 +421,26 @@ function! s:AsyncRun_Job_Update(count)
417421
endtry
418422
endif
419423
let l:text = substitute(l:text, '\r$', '', 'g')
420-
if l:text != ''
421-
if l:raw == 0
422-
if and(g:asyncrun_skip, 1) == 0
423-
caddexpr l:text
424+
if once == 0
425+
if l:text != ''
426+
if l:raw == 0
427+
if and(g:asyncrun_skip, 1) == 0
428+
caddexpr l:text
429+
else
430+
noautocmd caddexpr l:text
431+
endif
424432
else
425-
noautocmd caddexpr l:text
433+
call setqflist([{'text':l:text}], 'a')
426434
endif
427-
else
428-
call setqflist([{'text':l:text}], 'a')
435+
elseif g:asyncrun_trim == 0
436+
call setqflist(l:empty, 'a')
437+
endif
438+
else
439+
if l:text != ''
440+
let array += [l:text]
441+
elseif g:asyncrun_trim == 0
442+
let array += [l:text]
429443
endif
430-
elseif g:asyncrun_trim == 0
431-
call setqflist(l:empty, 'a')
432444
endif
433445
let l:total += 1
434446
unlet s:async_output[s:async_tail]
@@ -438,6 +450,23 @@ function! s:AsyncRun_Job_Update(count)
438450
break
439451
endif
440452
endwhile
453+
if once != 0
454+
if l:raw == 0
455+
if and(g:asyncrun_skip, 1) == 0
456+
caddexpr array
457+
else
458+
noautocmd caddexpr array
459+
endif
460+
else
461+
let items = []
462+
for text in array
463+
let items += [{'text': text}]
464+
endfor
465+
call setqflist(items, 'a')
466+
unlet items
467+
endif
468+
unlet array
469+
endif
441470
if g:asyncrun_local != 0
442471
if l:efm1 != &g:efm | let &g:efm = l:efm1 | endif
443472
if l:efm2 != &l:efm | let &l:efm = l:efm2 | endif
@@ -485,8 +514,13 @@ function! g:AsyncRun_Job_OnTimer(id)
485514
call job_status(s:async_job)
486515
endif
487516
endif
488-
call s:AsyncRun_Job_Update(limit)
517+
if s:async_info.once == 0
518+
call s:AsyncRun_Job_Update(limit)
519+
endif
489520
if and(s:async_state, 7) == 7
521+
if s:async_info.once != 0
522+
call s:AsyncRun_Job_Update(-1, 1)
523+
endif
490524
if s:async_head == s:async_tail
491525
call s:AsyncRun_Job_OnFinish()
492526
endif
@@ -504,7 +538,9 @@ function! s:AsyncRun_Job_OnCallback(channel, text)
504538
let s:async_output[s:async_head] = a:text
505539
let s:async_head += 1
506540
if s:async_congest != 0
507-
call s:AsyncRun_Job_Update(-1)
541+
if s:async_info.once == 0
542+
call s:AsyncRun_Job_Update(-1)
543+
endif
508544
endif
509545
endfunc
510546

@@ -522,7 +558,7 @@ function! s:AsyncRun_Job_OnFinish()
522558
call timer_stop(s:async_timer)
523559
unlet s:async_timer
524560
endif
525-
call s:AsyncRun_Job_Update(-1)
561+
call s:AsyncRun_Job_Update(-1, s:async_info.once)
526562
let l:current = localtime()
527563
let l:last = l:current - s:async_start
528564
let l:check = s:AsyncRun_Job_CheckScroll()
@@ -1658,6 +1694,7 @@ function! s:run(opts)
16581694
let s:async_info.range_buf = opts.range_buf
16591695
let s:async_info.strip = opts.strip
16601696
let s:async_info.append = opts.append
1697+
let s:async_info.once = get(opts, 'once', 0)
16611698
if s:AsyncRun_Job_Start(l:command) != 0
16621699
call s:AutoCmd('Error')
16631700
endif
@@ -2002,7 +2039,7 @@ endfunc
20022039
" asyncrun - version
20032040
"----------------------------------------------------------------------
20042041
function! asyncrun#version()
2005-
return '2.9.4'
2042+
return '2.9.5'
20062043
endfunc
20072044

20082045

0 commit comments

Comments
 (0)