2018-07-19 08:52:53 -04:00
|
|
|
" Spawn starts an asynchronous job. See the description of go#job#Options to
|
|
|
|
" understand the args parameter.
|
|
|
|
"
|
|
|
|
" Spawn returns a job.
|
|
|
|
function! go#job#Spawn(cmd, args)
|
|
|
|
let l:options = go#job#Options(a:args)
|
|
|
|
return go#job#Start(a:cmd, l:options)
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
" Options returns callbacks to be used with job_start. It is abstracted to be
|
2018-02-04 06:35:08 -05:00
|
|
|
" used with various go commands, such as build, test, install, etc.. This
|
|
|
|
" allows us to avoid writing the same callback over and over for some
|
2018-07-19 08:52:53 -04:00
|
|
|
" commands. It's fully customizable so each command can change it to its own
|
2018-02-04 06:35:08 -05:00
|
|
|
" logic.
|
|
|
|
"
|
|
|
|
" args is a dictionary with the these keys:
|
|
|
|
" 'bang':
|
|
|
|
" Set to 0 to jump to the first error in the error list.
|
|
|
|
" Defaults to 0.
|
2018-07-19 08:52:53 -04:00
|
|
|
" 'statustype':
|
|
|
|
" The status type to use when updating the status.
|
|
|
|
" See statusline.vim.
|
2018-02-04 06:35:08 -05:00
|
|
|
" 'for':
|
|
|
|
" The g:go_list_type_command key to use to get the error list type to use.
|
2018-08-25 12:13:42 -04:00
|
|
|
" Errors will not be handled when the value is '_'.
|
2018-02-04 06:35:08 -05:00
|
|
|
" Defaults to '_job'
|
2018-07-19 08:52:53 -04:00
|
|
|
" 'errorformat':
|
|
|
|
" The errorformat string to use when parsing errors. Defaults to
|
|
|
|
" &errorformat.
|
|
|
|
" See :help 'errorformat'.
|
2018-02-04 06:35:08 -05:00
|
|
|
" 'complete':
|
|
|
|
" A function to call after the job exits and the channel is closed. The
|
|
|
|
" function will be passed three arguments: the job, its exit code, and the
|
2018-08-25 12:13:42 -04:00
|
|
|
" list of messages received from the channel. The default is a no-op. A
|
|
|
|
" custom value can modify the messages before they are processed by the
|
2018-09-24 20:40:17 -04:00
|
|
|
" returned exit_cb and close_cb callbacks. When the function is called,
|
|
|
|
" the current window will be the window that was hosting the buffer when
|
|
|
|
" the job was started. After it returns, the current window will be
|
|
|
|
" restored to what it was before the function was called.
|
2018-02-04 06:35:08 -05:00
|
|
|
|
|
|
|
" The return value is a dictionary with these keys:
|
|
|
|
" 'callback':
|
|
|
|
" A function suitable to be passed as a job callback handler. See
|
|
|
|
" job-callback.
|
|
|
|
" 'exit_cb':
|
|
|
|
" A function suitable to be passed as a job exit_cb handler. See
|
|
|
|
" job-exit_cb.
|
|
|
|
" 'close_cb':
|
|
|
|
" A function suitable to be passed as a job close_cb handler. See
|
|
|
|
" job-close_cb.
|
2018-07-19 08:52:53 -04:00
|
|
|
" 'cwd':
|
2018-08-25 12:13:42 -04:00
|
|
|
" The path to the directory which contains the current buffer. The
|
|
|
|
" callbacks are configured to expect this directory is the working
|
|
|
|
" directory for the job; it should not be modified by callers.
|
2018-07-19 08:52:53 -04:00
|
|
|
function! go#job#Options(args)
|
2018-02-04 06:35:08 -05:00
|
|
|
let cbs = {}
|
2018-03-31 10:56:26 -04:00
|
|
|
let state = {
|
2018-06-14 06:31:12 -04:00
|
|
|
\ 'winid': win_getid(winnr()),
|
2018-03-31 10:56:26 -04:00
|
|
|
\ 'dir': getcwd(),
|
|
|
|
\ 'jobdir': fnameescape(expand("%:p:h")),
|
|
|
|
\ 'messages': [],
|
|
|
|
\ 'bang': 0,
|
|
|
|
\ 'for': "_job",
|
|
|
|
\ 'exited': 0,
|
|
|
|
\ 'exit_status': 0,
|
|
|
|
\ 'closed': 0,
|
2018-07-19 08:52:53 -04:00
|
|
|
\ 'errorformat': &errorformat,
|
|
|
|
\ 'statustype' : ''
|
2018-03-31 10:56:26 -04:00
|
|
|
\ }
|
2016-12-27 09:46:49 -05:00
|
|
|
|
2018-07-19 08:52:53 -04:00
|
|
|
if has("patch-8.0.0902") || has('nvim')
|
|
|
|
let cbs.cwd = state.jobdir
|
|
|
|
endif
|
|
|
|
|
2016-12-27 09:46:49 -05:00
|
|
|
if has_key(a:args, 'bang')
|
2018-03-31 10:56:26 -04:00
|
|
|
let state.bang = a:args.bang
|
2016-12-27 09:46:49 -05:00
|
|
|
endif
|
|
|
|
|
2017-11-24 08:54:40 -05:00
|
|
|
if has_key(a:args, 'for')
|
2018-03-31 10:56:26 -04:00
|
|
|
let state.for = a:args.for
|
2017-11-24 08:54:40 -05:00
|
|
|
endif
|
|
|
|
|
2018-07-19 08:52:53 -04:00
|
|
|
if has_key(a:args, 'statustype')
|
|
|
|
let state.statustype = a:args.statustype
|
|
|
|
endif
|
|
|
|
|
|
|
|
if has_key(a:args, 'errorformat')
|
|
|
|
let state.errorformat = a:args.errorformat
|
|
|
|
endif
|
|
|
|
|
2018-03-31 10:56:26 -04:00
|
|
|
" do nothing in state.complete by default.
|
|
|
|
function state.complete(job, exit_status, data)
|
2018-09-24 20:40:17 -04:00
|
|
|
if has_key(self, 'custom_complete')
|
|
|
|
let l:winid = win_getid(winnr())
|
|
|
|
" Always set the active window to the window that was active when the job
|
|
|
|
" was started. Among other things, this makes sure that the correct
|
|
|
|
" window's location list will be populated when the list type is
|
|
|
|
" 'location' and the user has moved windows since starting the job.
|
|
|
|
call win_gotoid(self.winid)
|
|
|
|
call self.custom_complete(a:job, a:exit_status, a:data)
|
|
|
|
call win_gotoid(l:winid)
|
|
|
|
endif
|
|
|
|
|
|
|
|
call self.show_errors(a:job, a:exit_status, a:data)
|
2018-02-04 06:35:08 -05:00
|
|
|
endfunction
|
|
|
|
|
2018-07-19 08:52:53 -04:00
|
|
|
function state.show_status(job, exit_status) dict
|
2018-09-24 20:40:17 -04:00
|
|
|
if self.statustype == ''
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
2018-07-19 08:52:53 -04:00
|
|
|
if go#config#EchoCommandInfo()
|
2018-09-24 20:40:17 -04:00
|
|
|
let prefix = '[' . self.statustype . '] '
|
2018-07-19 08:52:53 -04:00
|
|
|
if a:exit_status == 0
|
|
|
|
call go#util#EchoSuccess(prefix . "SUCCESS")
|
|
|
|
else
|
|
|
|
call go#util#EchoError(prefix . "FAIL")
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
|
|
|
|
let status = {
|
|
|
|
\ 'desc': 'last status',
|
|
|
|
\ 'type': self.statustype,
|
|
|
|
\ 'state': "success",
|
|
|
|
\ }
|
|
|
|
|
|
|
|
if a:exit_status
|
|
|
|
let status.state = "failed"
|
|
|
|
endif
|
|
|
|
|
|
|
|
if has_key(self, 'started_at')
|
|
|
|
let elapsed_time = reltimestr(reltime(self.started_at))
|
|
|
|
" strip whitespace
|
|
|
|
let elapsed_time = substitute(elapsed_time, '^\s*\(.\{-}\)\s*$', '\1', '')
|
|
|
|
let status.state .= printf(" (%ss)", elapsed_time)
|
|
|
|
endif
|
|
|
|
|
|
|
|
call go#statusline#Update(self.jobdir, status)
|
|
|
|
endfunction
|
|
|
|
|
2018-02-04 06:35:08 -05:00
|
|
|
if has_key(a:args, 'complete')
|
2018-09-24 20:40:17 -04:00
|
|
|
let state.custom_complete = a:args.complete
|
2016-12-27 09:46:49 -05:00
|
|
|
endif
|
|
|
|
|
2018-07-19 08:52:53 -04:00
|
|
|
function! s:start(args) dict
|
2018-09-24 20:40:17 -04:00
|
|
|
if go#config#EchoCommandInfo() && self.statustype != ""
|
|
|
|
let prefix = '[' . self.statustype . '] '
|
|
|
|
call go#util#EchoSuccess(prefix . "dispatched")
|
|
|
|
endif
|
|
|
|
|
2018-07-19 08:52:53 -04:00
|
|
|
if self.statustype != ''
|
|
|
|
let status = {
|
|
|
|
\ 'desc': 'current status',
|
|
|
|
\ 'type': self.statustype,
|
|
|
|
\ 'state': "started",
|
|
|
|
\ }
|
|
|
|
|
|
|
|
call go#statusline#Update(self.jobdir, status)
|
|
|
|
endif
|
|
|
|
let self.started_at = reltime()
|
|
|
|
endfunction
|
|
|
|
" explicitly bind _start to state so that within it, self will
|
|
|
|
" always refer to state. See :help Partial for more information.
|
|
|
|
"
|
|
|
|
" _start is intended only for internal use and should not be referenced
|
|
|
|
" outside of this file.
|
|
|
|
let cbs._start = function('s:start', [''], state)
|
|
|
|
|
2018-03-31 10:56:26 -04:00
|
|
|
function! s:callback(chan, msg) dict
|
|
|
|
call add(self.messages, a:msg)
|
2016-12-27 09:46:49 -05:00
|
|
|
endfunction
|
2018-03-31 10:56:26 -04:00
|
|
|
" explicitly bind callback to state so that within it, self will
|
|
|
|
" always refer to state. See :help Partial for more information.
|
|
|
|
let cbs.callback = function('s:callback', [], state)
|
2016-12-27 09:46:49 -05:00
|
|
|
|
2018-03-31 10:56:26 -04:00
|
|
|
function! s:exit_cb(job, exitval) dict
|
|
|
|
let self.exit_status = a:exitval
|
|
|
|
let self.exited = 1
|
2016-12-27 09:46:49 -05:00
|
|
|
|
2018-07-19 08:52:53 -04:00
|
|
|
call self.show_status(a:job, a:exitval)
|
2016-12-27 09:46:49 -05:00
|
|
|
|
2018-07-19 08:52:53 -04:00
|
|
|
if self.closed || has('nvim')
|
2018-03-31 10:56:26 -04:00
|
|
|
call self.complete(a:job, self.exit_status, self.messages)
|
2017-09-02 06:43:18 -04:00
|
|
|
endif
|
2018-02-04 06:35:08 -05:00
|
|
|
endfunction
|
2018-03-31 10:56:26 -04:00
|
|
|
" explicitly bind exit_cb to state so that within it, self will always refer
|
|
|
|
" to state. See :help Partial for more information.
|
|
|
|
let cbs.exit_cb = function('s:exit_cb', [], state)
|
2018-02-04 06:35:08 -05:00
|
|
|
|
2018-03-31 10:56:26 -04:00
|
|
|
function! s:close_cb(ch) dict
|
|
|
|
let self.closed = 1
|
2018-02-04 06:35:08 -05:00
|
|
|
|
2018-03-31 10:56:26 -04:00
|
|
|
if self.exited
|
2018-02-04 06:35:08 -05:00
|
|
|
let job = ch_getjob(a:ch)
|
2018-03-31 10:56:26 -04:00
|
|
|
call self.complete(job, self.exit_status, self.messages)
|
2018-02-04 06:35:08 -05:00
|
|
|
endif
|
|
|
|
endfunction
|
2018-03-31 10:56:26 -04:00
|
|
|
" explicitly bind close_cb to state so that within it, self will
|
|
|
|
" always refer to state. See :help Partial for more information.
|
|
|
|
let cbs.close_cb = function('s:close_cb', [], state)
|
2017-09-02 06:43:18 -04:00
|
|
|
|
2018-03-31 10:56:26 -04:00
|
|
|
function state.show_errors(job, exit_status, data)
|
2018-08-25 12:13:42 -04:00
|
|
|
if self.for == '_'
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
2018-06-14 06:31:12 -04:00
|
|
|
let l:winid = win_getid(winnr())
|
2018-08-25 12:13:42 -04:00
|
|
|
" Always set the active window to the window that was active when the job
|
|
|
|
" was started. Among other things, this makes sure that the correct
|
|
|
|
" window's location list will be populated when the list type is
|
|
|
|
" 'location' and the user has moved windows since starting the job.
|
2018-06-14 06:31:12 -04:00
|
|
|
call win_gotoid(self.winid)
|
|
|
|
|
2018-03-31 10:56:26 -04:00
|
|
|
let l:listtype = go#list#Type(self.for)
|
2018-02-04 06:35:08 -05:00
|
|
|
if a:exit_status == 0
|
2016-12-27 09:46:49 -05:00
|
|
|
call go#list#Clean(l:listtype)
|
2018-06-14 06:31:12 -04:00
|
|
|
call win_gotoid(l:winid)
|
2016-12-27 09:46:49 -05:00
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
2018-03-31 10:56:26 -04:00
|
|
|
let l:listtype = go#list#Type(self.for)
|
2018-02-04 06:35:08 -05:00
|
|
|
if len(a:data) == 0
|
|
|
|
call go#list#Clean(l:listtype)
|
2018-06-14 06:31:12 -04:00
|
|
|
call win_gotoid(l:winid)
|
2018-02-04 06:35:08 -05:00
|
|
|
return
|
|
|
|
endif
|
2016-12-27 09:46:49 -05:00
|
|
|
|
2018-03-31 10:56:26 -04:00
|
|
|
let out = join(self.messages, "\n")
|
|
|
|
|
2018-07-19 08:52:53 -04:00
|
|
|
let l:cd = exists('*haslocaldir') && haslocaldir() ? 'lcd' : 'cd'
|
2016-12-27 09:46:49 -05:00
|
|
|
try
|
2018-03-31 10:56:26 -04:00
|
|
|
" parse the errors relative to self.jobdir
|
2018-07-19 08:52:53 -04:00
|
|
|
execute l:cd self.jobdir
|
2018-03-31 10:56:26 -04:00
|
|
|
call go#list#ParseFormat(l:listtype, self.errorformat, out, self.for)
|
|
|
|
let errors = go#list#Get(l:listtype)
|
2016-12-27 09:46:49 -05:00
|
|
|
finally
|
2018-07-19 08:52:53 -04:00
|
|
|
execute l:cd fnameescape(self.dir)
|
2016-12-27 09:46:49 -05:00
|
|
|
endtry
|
|
|
|
|
2018-03-31 10:56:26 -04:00
|
|
|
|
2018-02-04 06:35:08 -05:00
|
|
|
if empty(errors)
|
2016-12-27 09:46:49 -05:00
|
|
|
" failed to parse errors, output the original content
|
2018-09-24 20:40:17 -04:00
|
|
|
call go#util#EchoError([self.dir] + self.messages)
|
2018-06-14 06:31:12 -04:00
|
|
|
call win_gotoid(l:winid)
|
2016-12-27 09:46:49 -05:00
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
2018-08-25 12:13:42 -04:00
|
|
|
" only open the error window if user was still in the window from which
|
|
|
|
" the job was started.
|
2018-06-14 06:31:12 -04:00
|
|
|
if self.winid == l:winid
|
2018-02-04 06:35:08 -05:00
|
|
|
call go#list#Window(l:listtype, len(errors))
|
2018-03-31 10:56:26 -04:00
|
|
|
if !self.bang
|
2018-02-04 06:35:08 -05:00
|
|
|
call go#list#JumpToFirst(l:listtype)
|
2016-12-27 09:46:49 -05:00
|
|
|
endif
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
return cbs
|
|
|
|
endfunction
|
2018-02-04 06:35:08 -05:00
|
|
|
|
2018-08-25 12:13:42 -04:00
|
|
|
" go#job#Start runs a job. The options are expected to be the options
|
|
|
|
" suitable for Vim8 jobs. When called from Neovim, Vim8 options will be
|
|
|
|
" transformed to their Neovim equivalents.
|
2018-07-19 08:52:53 -04:00
|
|
|
function! go#job#Start(cmd, options)
|
|
|
|
let l:cd = exists('*haslocaldir') && haslocaldir() ? 'lcd' : 'cd'
|
|
|
|
let l:options = copy(a:options)
|
|
|
|
|
2018-08-25 12:13:42 -04:00
|
|
|
if has('nvim')
|
|
|
|
let l:options = s:neooptions(l:options)
|
|
|
|
endif
|
|
|
|
|
2018-09-24 20:40:17 -04:00
|
|
|
" Verify that the working directory for the job actually exists. Return
|
|
|
|
" early if the directory does not exist. This helps avoid errors when
|
|
|
|
" working with plugins that use virtual files that don't actually exist on
|
|
|
|
" the file system.
|
|
|
|
let filedir = expand("%:p:h")
|
|
|
|
if has_key(l:options, 'cwd') && !isdirectory(l:options.cwd)
|
|
|
|
return
|
|
|
|
elseif !isdirectory(filedir)
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
2018-07-19 08:52:53 -04:00
|
|
|
if !has_key(l:options, 'cwd')
|
|
|
|
" pre start
|
|
|
|
let dir = getcwd()
|
2018-09-24 20:40:17 -04:00
|
|
|
execute l:cd fnameescape(filedir)
|
2018-07-19 08:52:53 -04:00
|
|
|
endif
|
|
|
|
|
|
|
|
if has_key(l:options, '_start')
|
|
|
|
call l:options._start()
|
|
|
|
" remove _start to play nicely with vim (when vim encounters an unexpected
|
2018-08-25 12:13:42 -04:00
|
|
|
" job option it reports an "E475: invalid argument" error).
|
2018-07-19 08:52:53 -04:00
|
|
|
unlet l:options._start
|
|
|
|
endif
|
|
|
|
|
|
|
|
if has('nvim')
|
|
|
|
let l:input = []
|
2018-09-24 20:40:17 -04:00
|
|
|
if has_key(a:options, 'in_io') && a:options.in_io ==# 'file' && !empty(a:options.in_name)
|
|
|
|
let l:input = readfile(a:options.in_name, "b")
|
2018-07-19 08:52:53 -04:00
|
|
|
endif
|
|
|
|
|
|
|
|
let job = jobstart(a:cmd, l:options)
|
|
|
|
|
|
|
|
if len(l:input) > 0
|
|
|
|
call chansend(job, l:input)
|
|
|
|
" close stdin to signal that no more bytes will be sent.
|
|
|
|
call chanclose(job, 'stdin')
|
|
|
|
endif
|
|
|
|
else
|
|
|
|
let job = job_start(a:cmd, l:options)
|
|
|
|
endif
|
|
|
|
|
|
|
|
if !has_key(l:options, 'cwd')
|
|
|
|
" post start
|
|
|
|
execute l:cd fnameescape(dir)
|
|
|
|
endif
|
|
|
|
|
|
|
|
return job
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
" s:neooptions returns a dictionary of job options suitable for use by Neovim
|
|
|
|
" based on a dictionary of job options suitable for Vim8.
|
|
|
|
function! s:neooptions(options)
|
|
|
|
let l:options = {}
|
|
|
|
let l:options['stdout_buf'] = ''
|
|
|
|
let l:options['stderr_buf'] = ''
|
|
|
|
|
|
|
|
for key in keys(a:options)
|
2018-08-25 12:13:42 -04:00
|
|
|
if key == 'cwd'
|
|
|
|
let l:options['cwd'] = a:options['cwd']
|
|
|
|
continue
|
|
|
|
endif
|
|
|
|
|
2018-09-24 20:40:17 -04:00
|
|
|
" dealing with the channel lines of Neovim sucks. The docs (:help
|
|
|
|
" channel-lines) say:
|
|
|
|
" stream event handlers may receive partial (incomplete) lines. For a
|
|
|
|
" given invocation of on_stdout etc, `a:data` is not guaranteed to end
|
|
|
|
" with a newline.
|
|
|
|
" - `abcdefg` may arrive as `['abc']`, `['defg']`.
|
|
|
|
" - `abc\nefg` may arrive as `['abc', '']`, `['efg']` or `['abc']`,
|
|
|
|
" `['','efg']`, or even `['ab']`, `['c','efg']`.
|
2018-07-19 08:52:53 -04:00
|
|
|
if key == 'callback'
|
|
|
|
let l:options['callback'] = a:options['callback']
|
|
|
|
|
|
|
|
if !has_key(a:options, 'out_cb')
|
|
|
|
function! s:callback2on_stdout(ch, data, event) dict
|
2018-09-24 20:40:17 -04:00
|
|
|
" a single empty string means EOF was reached.
|
|
|
|
if len(a:data) == 1 && a:data[0] == ''
|
|
|
|
" when there's nothing buffered, return early so that an
|
|
|
|
" erroneous message will not be added.
|
|
|
|
if self.stdout_buf == ''
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
|
|
|
let l:data = [self.stdout_buf]
|
|
|
|
let self.stdout_buf = ''
|
|
|
|
else
|
|
|
|
let l:data = copy(a:data)
|
|
|
|
let l:data[0] = self.stdout_buf . l:data[0]
|
|
|
|
|
|
|
|
" The last element may be a partial line; save it for next time.
|
2018-07-19 08:52:53 -04:00
|
|
|
let self.stdout_buf = l:data[-1]
|
|
|
|
|
2018-09-24 20:40:17 -04:00
|
|
|
let l:data = l:data[:-2]
|
|
|
|
|
|
|
|
if len(l:data) == 0
|
|
|
|
return
|
|
|
|
endif
|
2018-07-19 08:52:53 -04:00
|
|
|
endif
|
|
|
|
|
2018-09-24 20:40:17 -04:00
|
|
|
for l:msg in l:data
|
|
|
|
call self.callback(a:ch, l:msg)
|
|
|
|
endfor
|
2018-07-19 08:52:53 -04:00
|
|
|
endfunction
|
|
|
|
let l:options['on_stdout'] = function('s:callback2on_stdout', [], l:options)
|
|
|
|
endif
|
|
|
|
|
|
|
|
if !has_key(a:options, 'err_cb')
|
|
|
|
function! s:callback2on_stderr(ch, data, event) dict
|
2018-09-24 20:40:17 -04:00
|
|
|
" a single empty string means EOF was reached.
|
|
|
|
if len(a:data) == 1 && a:data[0] == ''
|
|
|
|
" when there's nothing buffered, return early so that an
|
|
|
|
" erroneous message will not be added.
|
|
|
|
if self.stderr_buf == ''
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
let l:data = [self.stderr_buf]
|
|
|
|
let self.stderr_buf = ''
|
|
|
|
else
|
|
|
|
let l:data = copy(a:data)
|
|
|
|
let l:data[0] = self.stderr_buf . l:data[0]
|
|
|
|
|
|
|
|
" The last element may be a partial line; save it for next time.
|
2018-07-19 08:52:53 -04:00
|
|
|
let self.stderr_buf = l:data[-1]
|
|
|
|
|
2018-09-24 20:40:17 -04:00
|
|
|
let l:data = l:data[:-2]
|
|
|
|
if len(l:data) == 0
|
|
|
|
return
|
|
|
|
endif
|
2018-07-19 08:52:53 -04:00
|
|
|
endif
|
|
|
|
|
2018-09-24 20:40:17 -04:00
|
|
|
for l:msg in l:data
|
|
|
|
call self.callback(a:ch, l:msg)
|
|
|
|
endfor
|
2018-07-19 08:52:53 -04:00
|
|
|
endfunction
|
|
|
|
let l:options['on_stderr'] = function('s:callback2on_stderr', [], l:options)
|
|
|
|
endif
|
|
|
|
|
|
|
|
continue
|
|
|
|
endif
|
|
|
|
|
|
|
|
if key == 'out_cb'
|
|
|
|
let l:options['out_cb'] = a:options['out_cb']
|
|
|
|
function! s:on_stdout(ch, data, event) dict
|
2018-09-24 20:40:17 -04:00
|
|
|
" a single empty string means EOF was reached.
|
|
|
|
if len(a:data) == 1 && a:data[0] == ''
|
|
|
|
" when there's nothing buffered, return early so that an
|
|
|
|
" erroneous message will not be added.
|
|
|
|
if self.stdout_buf == ''
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
let l:data = [self.stdout_buf]
|
|
|
|
let self.stdout_buf = ''
|
|
|
|
else
|
|
|
|
let l:data = copy(a:data)
|
|
|
|
let l:data[0] = self.stdout_buf . l:data[0]
|
2018-07-19 08:52:53 -04:00
|
|
|
|
2018-09-24 20:40:17 -04:00
|
|
|
" The last element may be a partial line; save it for next time.
|
2018-07-19 08:52:53 -04:00
|
|
|
let self.stdout_buf = l:data[-1]
|
|
|
|
|
2018-09-24 20:40:17 -04:00
|
|
|
let l:data = l:data[:-2]
|
|
|
|
if len(l:data) == 0
|
|
|
|
return
|
|
|
|
endif
|
2018-07-19 08:52:53 -04:00
|
|
|
endif
|
|
|
|
|
2018-09-24 20:40:17 -04:00
|
|
|
for l:msg in l:data
|
|
|
|
call self.out_cb(a:ch, l:msg)
|
|
|
|
endfor
|
2018-07-19 08:52:53 -04:00
|
|
|
endfunction
|
|
|
|
let l:options['on_stdout'] = function('s:on_stdout', [], l:options)
|
|
|
|
|
|
|
|
continue
|
|
|
|
endif
|
|
|
|
|
|
|
|
if key == 'err_cb'
|
|
|
|
let l:options['err_cb'] = a:options['err_cb']
|
|
|
|
function! s:on_stderr(ch, data, event) dict
|
2018-09-24 20:40:17 -04:00
|
|
|
" a single empty string means EOF was reached.
|
|
|
|
if len(a:data) == 1 && a:data[0] == ''
|
|
|
|
" when there's nothing buffered, return early so that an
|
|
|
|
" erroneous message will not be added.
|
|
|
|
if self.stderr_buf == ''
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
let l:data = [self.stderr_buf]
|
|
|
|
let self.stderr_buf = ''
|
|
|
|
else
|
|
|
|
let l:data = copy(a:data)
|
|
|
|
let l:data[0] = self.stderr_buf . l:data[0]
|
2018-07-19 08:52:53 -04:00
|
|
|
|
2018-09-24 20:40:17 -04:00
|
|
|
" The last element may be a partial line; save it for next time.
|
2018-07-19 08:52:53 -04:00
|
|
|
let self.stderr_buf = l:data[-1]
|
|
|
|
|
2018-09-24 20:40:17 -04:00
|
|
|
let l:data = l:data[:-2]
|
|
|
|
if len(l:data) == 0
|
|
|
|
return
|
|
|
|
endif
|
2018-07-19 08:52:53 -04:00
|
|
|
endif
|
|
|
|
|
2018-09-24 20:40:17 -04:00
|
|
|
for l:msg in l:data
|
|
|
|
call self.err_cb(a:ch, l:msg)
|
|
|
|
endfor
|
2018-07-19 08:52:53 -04:00
|
|
|
endfunction
|
|
|
|
let l:options['on_stderr'] = function('s:on_stderr', [], l:options)
|
|
|
|
|
|
|
|
continue
|
|
|
|
endif
|
|
|
|
|
|
|
|
if key == 'exit_cb'
|
|
|
|
let l:options['exit_cb'] = a:options['exit_cb']
|
|
|
|
function! s:on_exit(jobid, exitval, event) dict
|
|
|
|
call self.exit_cb(a:jobid, a:exitval)
|
|
|
|
endfunction
|
|
|
|
let l:options['on_exit'] = function('s:on_exit', [], l:options)
|
|
|
|
|
|
|
|
continue
|
|
|
|
endif
|
|
|
|
|
|
|
|
if key == 'close_cb'
|
|
|
|
continue
|
|
|
|
endif
|
|
|
|
|
2018-09-24 20:40:17 -04:00
|
|
|
if key == 'stoponexit'
|
|
|
|
if a:options['stoponexit'] == ''
|
|
|
|
let l:options['detach'] = 1
|
|
|
|
endif
|
|
|
|
continue
|
|
|
|
endif
|
2018-07-19 08:52:53 -04:00
|
|
|
endfor
|
|
|
|
return l:options
|
|
|
|
endfunction
|
|
|
|
|
2016-12-27 09:46:49 -05:00
|
|
|
" vim: sw=2 ts=2 et
|