2017-03-21 09:08:19 -07:00
|
|
|
*job_control.txt* Nvim
|
2014-09-13 12:21:15 -07:00
|
|
|
|
|
|
|
|
|
|
|
NVIM REFERENCE MANUAL by Thiago de Arruda
|
|
|
|
|
|
|
|
|
2019-08-25 16:00:52 -07:00
|
|
|
Nvim job control *job* *job-control*
|
2017-11-05 17:23:32 -07:00
|
|
|
|
|
|
|
Job control is a way to perform multitasking in Nvim, so scripts can spawn and
|
|
|
|
control multiple processes without blocking the current Nvim instance.
|
2014-09-13 12:21:15 -07:00
|
|
|
|
2017-10-20 17:33:58 -07:00
|
|
|
Type |gO| to see the table of contents.
|
2014-09-13 12:21:15 -07:00
|
|
|
|
|
|
|
==============================================================================
|
2017-11-05 17:23:32 -07:00
|
|
|
Concepts
|
2014-09-13 12:21:15 -07:00
|
|
|
|
2017-11-05 17:23:32 -07:00
|
|
|
Job Id *job-id*
|
2014-09-13 12:21:15 -07:00
|
|
|
|
2018-08-04 18:17:08 -07:00
|
|
|
Each job is identified by an integer id, unique for the life of the current
|
|
|
|
Nvim session. Each job-id is a valid |channel-id|: they share the same "key
|
|
|
|
space". Functions like |jobstart()| return job ids; functions like
|
2017-11-05 17:23:32 -07:00
|
|
|
|jobsend()|, |jobstop()|, |rpcnotify()|, and |rpcrequest()| take job ids.
|
2014-09-13 12:21:15 -07:00
|
|
|
|
2018-08-04 18:17:08 -07:00
|
|
|
Job stdio streams form a |channel| which can send and receive raw bytes or
|
|
|
|
|msgpack-rpc| messages.
|
|
|
|
|
2017-11-05 17:23:32 -07:00
|
|
|
==============================================================================
|
|
|
|
Usage *job-control-usage*
|
2014-09-13 12:21:15 -07:00
|
|
|
|
2017-11-05 17:23:32 -07:00
|
|
|
To control jobs, use the "job…" family of functions: |jobstart()|,
|
|
|
|
|jobsend()|, |jobstop()|.
|
2014-09-13 12:21:15 -07:00
|
|
|
|
2017-11-05 17:23:32 -07:00
|
|
|
Example: >
|
2014-09-13 12:21:15 -07:00
|
|
|
|
2017-12-01 05:40:03 -07:00
|
|
|
function! s:OnEvent(job_id, data, event) dict
|
2015-03-25 19:11:05 -07:00
|
|
|
if a:event == 'stdout'
|
|
|
|
let str = self.shell.' stdout: '.join(a:data)
|
|
|
|
elseif a:event == 'stderr'
|
|
|
|
let str = self.shell.' stderr: '.join(a:data)
|
2014-09-13 12:21:15 -07:00
|
|
|
else
|
2015-03-25 19:11:05 -07:00
|
|
|
let str = self.shell.' exited'
|
2014-09-13 12:21:15 -07:00
|
|
|
endif
|
2014-12-08 17:00:05 -07:00
|
|
|
|
2014-09-13 12:21:15 -07:00
|
|
|
call append(line('$'), str)
|
|
|
|
endfunction
|
2015-03-25 19:11:05 -07:00
|
|
|
let s:callbacks = {
|
2017-06-10 00:55:06 -07:00
|
|
|
\ 'on_stdout': function('s:OnEvent'),
|
|
|
|
\ 'on_stderr': function('s:OnEvent'),
|
|
|
|
\ 'on_exit': function('s:OnEvent')
|
2015-03-25 19:11:05 -07:00
|
|
|
\ }
|
|
|
|
let job1 = jobstart(['bash'], extend({'shell': 'shell 1'}, s:callbacks))
|
|
|
|
let job2 = jobstart(['bash', '-c', 'for i in {1..10}; do echo hello $i!; sleep 1; done'], extend({'shell': 'shell 2'}, s:callbacks))
|
2014-12-08 17:00:05 -07:00
|
|
|
|
2017-11-05 17:23:32 -07:00
|
|
|
To test the above script, copy it to a file ~/foo.vim and run it: >
|
|
|
|
nvim -u ~/foo.vim
|
2014-09-13 12:21:15 -07:00
|
|
|
<
|
2017-11-05 17:23:32 -07:00
|
|
|
Description of what happens:
|
|
|
|
- Two bash shells are spawned by |jobstart()| with their stdin/stdout/stderr
|
|
|
|
streams connected to nvim.
|
|
|
|
- The first shell is idle, waiting to read commands from its stdin.
|
|
|
|
- The second shell is started with -c which executes the command (a for-loop
|
|
|
|
printing 0 through 9) and then exits.
|
2017-12-03 12:49:01 -07:00
|
|
|
- `OnEvent()` callback is passed to |jobstart()| to handle various job
|
2017-11-05 17:23:32 -07:00
|
|
|
events. It displays stdout/stderr data received from the shells.
|
|
|
|
|
2017-06-10 00:55:06 -07:00
|
|
|
For |on_stdout| and |on_stderr| see |channel-callback|.
|
2017-11-05 17:23:32 -07:00
|
|
|
*on_exit*
|
|
|
|
Arguments passed to on_exit callback:
|
|
|
|
0: |job-id|
|
2019-08-09 06:34:06 -07:00
|
|
|
1: Exit-code of the process, or 128+SIGNUM if by signal (e.g. 143 on SIGTERM).
|
2017-11-05 17:23:32 -07:00
|
|
|
2: Event type: "exit"
|
2015-03-25 19:11:05 -07:00
|
|
|
|
2017-06-10 00:55:06 -07:00
|
|
|
|
2016-06-26 15:26:10 -07:00
|
|
|
Note: Buffered stdout/stderr data which has not been flushed by the sender
|
2017-11-05 17:23:32 -07:00
|
|
|
will not trigger the on_stdout/on_stderr callback (but if the process
|
|
|
|
ends, the on_exit callback will be invoked).
|
2016-06-26 15:26:10 -07:00
|
|
|
For example, "ruby -e" buffers output, so small strings will be
|
|
|
|
buffered unless "auto-flushing" ($stdout.sync=true) is enabled. >
|
|
|
|
function! Receive(job_id, data, event)
|
|
|
|
echom printf('%s: %s',a:event,string(a:data))
|
|
|
|
endfunction
|
|
|
|
call jobstart(['ruby', '-e',
|
|
|
|
\ '$stdout.sync = true; 5.times do sleep 1 and puts "Hello Ruby!" end'],
|
|
|
|
\ {'on_stdout': 'Receive'})
|
|
|
|
< https://github.com/neovim/neovim/issues/1592
|
|
|
|
|
2017-11-05 17:23:32 -07:00
|
|
|
Note 2:
|
|
|
|
Job event handlers may receive partial (incomplete) lines. For a given
|
|
|
|
invocation of on_stdout/on_stderr, `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']`.
|
|
|
|
Easy way to deal with this: initialize a list as `['']`, then append
|
|
|
|
to it as follows: >
|
|
|
|
let s:chunks = ['']
|
|
|
|
func! s:on_stdout(job_id, data, event) dict
|
|
|
|
let s:chunks[-1] .= a:data[0]
|
|
|
|
call extend(s:chunks, a:data[1:])
|
|
|
|
endf
|
|
|
|
<
|
|
|
|
|
|
|
|
The |jobstart-options| dictionary is passed as |self| to the callback.
|
|
|
|
The above example could be written in this "object-oriented" style: >
|
|
|
|
|
2015-03-25 19:11:05 -07:00
|
|
|
let Shell = {}
|
2015-05-19 07:11:32 -07:00
|
|
|
|
2017-07-12 05:21:24 -07:00
|
|
|
function Shell.on_stdout(_job_id, data, event)
|
|
|
|
call append(line('$'),
|
|
|
|
\ printf('[%s] %s: %s', a:event, self.name, join(a:data[:-2])))
|
2015-03-25 19:11:05 -07:00
|
|
|
endfunction
|
2015-05-19 07:11:32 -07:00
|
|
|
|
2017-07-12 05:21:24 -07:00
|
|
|
let Shell.on_stderr = function(Shell.on_stdout)
|
2015-05-19 07:11:32 -07:00
|
|
|
|
2017-07-12 05:21:24 -07:00
|
|
|
function Shell.on_exit(job_id, _data, event)
|
|
|
|
let msg = printf('job %d ("%s") finished', a:job_id, self.name)
|
|
|
|
call append(line('$'), printf('[%s] BOOM!', a:event))
|
|
|
|
call append(line('$'), printf('[%s] %s!', a:event, msg))
|
2015-03-25 19:11:05 -07:00
|
|
|
endfunction
|
2015-05-19 07:11:32 -07:00
|
|
|
|
2017-07-12 05:21:24 -07:00
|
|
|
function Shell.new(name, cmd)
|
|
|
|
let object = extend(copy(g:Shell), {'name': a:name})
|
|
|
|
let object.cmd = ['sh', '-c', a:cmd]
|
|
|
|
let object.id = jobstart(object.cmd, object)
|
|
|
|
$
|
|
|
|
return object
|
2015-03-25 19:11:05 -07:00
|
|
|
endfunction
|
2015-05-19 07:11:32 -07:00
|
|
|
|
2017-07-12 05:21:24 -07:00
|
|
|
let instance = Shell.new('bomb',
|
|
|
|
\ 'for i in $(seq 9 -1 1); do echo $i 1>&$((i % 2 + 1)); sleep 1; done')
|
|
|
|
<
|
2017-06-10 00:55:06 -07:00
|
|
|
To send data to the job's stdin, use |chansend()|: >
|
|
|
|
:call chansend(job1, "ls\n")
|
|
|
|
:call chansend(job1, "invalid-command\n")
|
|
|
|
:call chansend(job1, "exit\n")
|
2014-09-13 12:21:15 -07:00
|
|
|
<
|
2017-11-05 17:23:32 -07:00
|
|
|
A job may be killed with |jobstop()|: >
|
2014-09-13 12:21:15 -07:00
|
|
|
:call jobstop(job1)
|
|
|
|
<
|
2017-06-10 00:55:06 -07:00
|
|
|
A job may be killed at any time with the |jobstop()| function:
|
|
|
|
>
|
|
|
|
:call jobstop(job1)
|
|
|
|
<
|
|
|
|
Individual streams can be closed without killing the job, see |chanclose()|.
|
|
|
|
|
2014-09-13 12:21:15 -07:00
|
|
|
==============================================================================
|
|
|
|
vim:tw=78:ts=8:noet:ft=help:norl:
|