2021-06-28 11:43:07 +00:00
|
|
|
function s:exit(code, buffer, start, end, callbacks)
|
|
|
|
if len(a:callbacks)>1
|
|
|
|
let [l:Success, l:Error] = a:callbacks
|
|
|
|
elseif len(a:callbacks)>0
|
|
|
|
let l:Success = a:callbacks[0]
|
|
|
|
let l:Error = a:callbacks[0]
|
2020-09-29 11:19:24 +00:00
|
|
|
else
|
2021-06-28 11:43:07 +00:00
|
|
|
let l:Success = { a -> a }
|
|
|
|
let l:Error = { a -> a }
|
2020-09-29 11:19:24 +00:00
|
|
|
end
|
2021-06-28 11:43:07 +00:00
|
|
|
if (a:code == 0)
|
|
|
|
return l:Success({"output": a:buffer, "tstart": a:start, "tend": a:end, "code": a:code})
|
|
|
|
else
|
|
|
|
return l:Error({"output": a:buffer, "tstart": a:start, "tend": a:end, "code": a:code})
|
|
|
|
endif
|
2020-09-23 11:52:03 +00:00
|
|
|
endfun
|
|
|
|
|
2021-06-28 11:43:07 +00:00
|
|
|
function Defer(command, ...)
|
2020-09-29 11:19:24 +00:00
|
|
|
let l:start = strftime("%s")
|
2020-08-19 15:32:32 +00:00
|
|
|
let l:buffer = []
|
2021-07-06 13:39:25 +00:00
|
|
|
let l:callbacks = a:000
|
2021-06-28 11:43:07 +00:00
|
|
|
if has("nvim")
|
|
|
|
call jobstart(a:command, {
|
2021-07-06 13:39:25 +00:00
|
|
|
\ "out_io": "pipe",
|
|
|
|
\ "on_stdout": { pipe, text -> extend(l:buffer, text) },
|
2023-07-17 15:28:50 +00:00
|
|
|
\ "on_stderr": { pipe, text -> extend(l:buffer, text) },
|
2021-07-06 13:39:25 +00:00
|
|
|
\ "on_exit": { id, code -> <SID>exit(code, l:buffer, l:start, strftime("%s"), l:callbacks) }
|
|
|
|
\})
|
2021-06-28 11:43:07 +00:00
|
|
|
else
|
|
|
|
call job_start(a:command, {
|
2021-07-06 13:39:25 +00:00
|
|
|
\ "out_io": "pipe",
|
|
|
|
\ "out_cb": { pipe, text -> add(l:buffer, text) },
|
|
|
|
\ "close_cb": { pipe -> <SID>exit(0, l:buffer, l:start, strftime("%s"), a:000) }
|
|
|
|
\})
|
2021-06-28 11:43:07 +00:00
|
|
|
end
|
2020-09-29 11:19:24 +00:00
|
|
|
endfun
|
|
|
|
|
2021-07-06 13:39:25 +00:00
|
|
|
function s:replace(buf, text)
|
|
|
|
call nvim_buf_set_lines(a:buf, 0, nvim_buf_line_count(a:buf), 0, a:text)
|
|
|
|
endfun
|
|
|
|
|
2020-09-29 11:19:24 +00:00
|
|
|
function s:expand(string)
|
|
|
|
return substitute(a:string, '%[:a-z]*', '\=expand(submatch(0))', 'g')
|
2020-08-19 12:23:39 +00:00
|
|
|
endfun
|
|
|
|
|
2021-06-28 11:43:07 +00:00
|
|
|
function s:echo(message, ...)
|
|
|
|
if (a:0 > 0)
|
|
|
|
exec "echohl ".a:1
|
|
|
|
end
|
2020-09-29 11:19:24 +00:00
|
|
|
echom a:message
|
2021-06-28 11:43:07 +00:00
|
|
|
if (a:0 > 0)
|
|
|
|
echohl None
|
|
|
|
end
|
2020-08-21 11:00:07 +00:00
|
|
|
endfun
|
|
|
|
|
2023-07-17 15:28:50 +00:00
|
|
|
function s:scratch(result)
|
|
|
|
new
|
|
|
|
call nvim_buf_set_lines(0, 0, nvim_buf_line_count(0), 0, a:result["output"])
|
|
|
|
endfun
|
|
|
|
|
2020-08-21 11:00:07 +00:00
|
|
|
function s:notify(message)
|
2020-09-29 11:19:24 +00:00
|
|
|
call Defer('notify-send "Vim" "'.a:message.'"', { b -> 0 })
|
2020-08-21 11:00:07 +00:00
|
|
|
endfun
|
|
|
|
|
2021-06-28 11:43:07 +00:00
|
|
|
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=* 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>)) })
|
2023-07-17 15:28:50 +00:00
|
|
|
comm -complete=shellcmd -nargs=* DeferScratch call Defer(s:expand(<q-args>), function("<SID>scratch"))
|
2021-07-06 13:39:25 +00:00
|
|
|
comm -complete=shellcmd -count=0 -nargs=* DeferBuffer call Defer(s:expand(<q-args>), { result -> <SID>replace(<count>, result['output']) })
|