From 0233dccd1e00719252be01f9de55ac7b0077c720 Mon Sep 17 00:00:00 2001 From: DarkWiiPlayer Date: Tue, 6 Jul 2021 15:39:25 +0200 Subject: [PATCH] Add DeferBuffer vim command --- vim/plugin/defer.vim | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/vim/plugin/defer.vim b/vim/plugin/defer.vim index e4a9a7b..947cd60 100644 --- a/vim/plugin/defer.vim +++ b/vim/plugin/defer.vim @@ -18,22 +18,26 @@ endfun function Defer(command, ...) let l:start = strftime("%s") let l:buffer = [] - let l:callbacks = a:000 + let l:callbacks = a:000 if has("nvim") call jobstart(a:command, { - \ "out_io": "pipe", - \ "out_cb": { pipe, text -> add(l:buffer, text) }, - \ "on_exit": { id, code -> exit(code, l:buffer, l:start, strftime("%s"), l:callbacks) } - \ }) + \ "out_io": "pipe", + \ "on_stdout": { pipe, text -> extend(l:buffer, text) }, + \ "on_exit": { id, code -> exit(code, l:buffer, l:start, strftime("%s"), l:callbacks) } + \}) else call job_start(a:command, { - \ "out_io": "pipe", - \ "out_cb": { pipe, text -> add(l:buffer, text) }, - \ "close_cb": { pipe -> exit(0, l:buffer, l:start, strftime("%s"), a:000) } - \ }) + \ "out_io": "pipe", + \ "out_cb": { pipe, text -> add(l:buffer, text) }, + \ "close_cb": { pipe -> exit(0, l:buffer, l:start, strftime("%s"), a:000) } + \}) end 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) return substitute(a:string, '%[:a-z]*', '\=expand(submatch(0))', 'g') endfun @@ -55,3 +59,4 @@ endfun comm -complete=shellcmd -nargs=* Defer call Defer(s:expand()) comm -complete=shellcmd -nargs=* DeferEcho call Defer(s:expand(), { result -> echo("Deferred job completed (".(result['tend']-result['tstart'])."s): ".s:expand()) }, { result -> echo("Deferred job errored with ".result['code']." (".(result['tend']-result['tstart'])."s): ".s:expand(), 'WarningMsg') }) comm -complete=shellcmd -nargs=* DeferNotify call Defer(s:expand(), { result -> notify("Deferred job completed (".(result['tend']-result['tstart'])."s):\n$ ".s:expand()) }, { result -> notify("Deferred job errored with ".result['code']." (".(result['tend']-result['tstart'])."s):\n$ ".s:expand()) }) +comm -complete=shellcmd -count=0 -nargs=* DeferBuffer call Defer(s:expand(), { result -> replace(, result['output']) })