Add DeferBuffer vim command

This commit is contained in:
Talia 2021-07-06 15:39:25 +02:00
parent 1e5c9162f9
commit 0233dccd1e

View file

@ -18,22 +18,26 @@ endfun
function Defer(command, ...) function Defer(command, ...)
let l:start = strftime("%s") let l:start = strftime("%s")
let l:buffer = [] let l:buffer = []
let l:callbacks = a:000 let l:callbacks = a:000
if has("nvim") if has("nvim")
call jobstart(a:command, { call jobstart(a:command, {
\ "out_io": "pipe", \ "out_io": "pipe",
\ "out_cb": { pipe, text -> add(l:buffer, text) }, \ "on_stdout": { pipe, text -> extend(l:buffer, text) },
\ "on_exit": { id, code -> <SID>exit(code, l:buffer, l:start, strftime("%s"), l:callbacks) } \ "on_exit": { id, code -> <SID>exit(code, l:buffer, l:start, strftime("%s"), l:callbacks) }
\ }) \})
else else
call job_start(a:command, { call job_start(a:command, {
\ "out_io": "pipe", \ "out_io": "pipe",
\ "out_cb": { pipe, text -> add(l:buffer, text) }, \ "out_cb": { pipe, text -> add(l:buffer, text) },
\ "close_cb": { pipe -> <SID>exit(0, l:buffer, l:start, strftime("%s"), a:000) } \ "close_cb": { pipe -> <SID>exit(0, l:buffer, l:start, strftime("%s"), a:000) }
\ }) \})
end end
endfun endfun
function s:replace(buf, text)
call nvim_buf_set_lines(a:buf, 0, nvim_buf_line_count(a:buf), 0, a:text)
endfun
function s:expand(string) function s:expand(string)
return substitute(a:string, '%[:a-z]*', '\=expand(submatch(0))', 'g') return substitute(a:string, '%[:a-z]*', '\=expand(submatch(0))', 'g')
endfun endfun
@ -55,3 +59,4 @@ endfun
comm -complete=shellcmd -nargs=* Defer call Defer(s:expand(<q-args>)) comm -complete=shellcmd -nargs=* Defer call Defer(s:expand(<q-args>))
comm -complete=shellcmd -nargs=* DeferEcho call Defer(s:expand(<q-args>), { result -> <SID>echo("Deferred job completed (".(result['tend']-result['tstart'])."s): ".s:expand(<q-args>)) }, { result -> <SID>echo("Deferred job errored with ".result['code']." (".(result['tend']-result['tstart'])."s): ".s:expand(<q-args>), 'WarningMsg') }) comm -complete=shellcmd -nargs=* DeferEcho call Defer(s:expand(<q-args>), { result -> <SID>echo("Deferred job completed (".(result['tend']-result['tstart'])."s): ".s:expand(<q-args>)) }, { result -> <SID>echo("Deferred job errored with ".result['code']." (".(result['tend']-result['tstart'])."s): ".s:expand(<q-args>), 'WarningMsg') })
comm -complete=shellcmd -nargs=* DeferNotify call Defer(s:expand(<q-args>), { result -> <SID>notify("Deferred job completed (".(result['tend']-result['tstart'])."s):\n$ ".s:expand(<q-args>)) }, { result -> <SID>notify("Deferred job errored with ".result['code']." (".(result['tend']-result['tstart'])."s):\n$ ".s:expand(<q-args>)) }) comm -complete=shellcmd -nargs=* DeferNotify call Defer(s:expand(<q-args>), { result -> <SID>notify("Deferred job completed (".(result['tend']-result['tstart'])."s):\n$ ".s:expand(<q-args>)) }, { result -> <SID>notify("Deferred job errored with ".result['code']." (".(result['tend']-result['tstart'])."s):\n$ ".s:expand(<q-args>)) })
comm -complete=shellcmd -count=0 -nargs=* DeferBuffer call Defer(s:expand(<q-args>), { result -> <SID>replace(<count>, result['output']) })