blob: aa31816fcb778071279c3b87b9230bd2947698a4 [file] [log] [blame]
Bram Moolenaar02e83b42016-02-21 20:10:26 +01001*channel.txt* For Vim version 7.4. Last change: 2016 Feb 21
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002
3
4 VIM REFERENCE MANUAL by Bram Moolenaar
5
6
7 Inter-process communication *channel*
8
9DRAFT DRAFT DRAFT DRAFT DRAFT DRAFT DRAFT DRAFT DRAFT DRAFT
10
11Vim uses channels to communicate with other processes.
Bram Moolenaar38a55632016-02-15 22:07:32 +010012A channel uses a socket or pipes *socket-interface*
13Jobs can be used to start processes and communicate with them.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010014
Bram Moolenaar681baaf2016-02-04 20:57:07 +010015Vim current supports up to 10 simultaneous channels.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010016The Netbeans interface also uses a channel. |netbeans|
17
Bram Moolenaar38a55632016-02-15 22:07:32 +0100181. Overview |job-channel-overview|
192. Channel demo |channel-demo|
203. Opening a channel |channel-open|
214. Using a JSON or JS channel |channel-use|
225. Channel commands |channel-commands|
236. Using a RAW or NL channel |channel-raw|
247. More channel functions |channel-more|
258. Starting a job with a channel |job-start|
269. Starting a job without a channel |job-start-nochannel|
2710. Job options |job-options|
2811. Controlling a job |job-control|
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010029
30{Vi does not have any of these features}
Bram Moolenaar38a55632016-02-15 22:07:32 +010031{only when compiled with the |+channel| feature for channel stuff}
32{only when compiled with the |+job| feature for job stuff}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010033
34==============================================================================
Bram Moolenaar38a55632016-02-15 22:07:32 +0100351. Overview *job-channel-overview*
36
37There are four main types of jobs:
381. A deamon, serving several Vim instances.
39 Vim connects to it with a socket.
402. One job working with one Vim instance, asynchronously.
41 Uses a socket or pipes.
423. A job performing some work for a short time, asynchronously.
43 Uses a socket or pipes.
444. Running a filter, synchronously.
45 Uses pipes.
46
47For when using sockets See |job-start|, |job-may-start| and |channel-open|.
48For 2 and 3, one or more jobs using pipes, see |job-start|.
49For 4 use the ":{range}!cmd" command, see |filter|.
50
51Over the socket and pipes these protocols are available:
52RAW nothing known, Vim cannot tell where a message ends
53NL every message ends in a NL (newline) character
54JSON JSON encoding |json_encode()|
55JS JavaScript style JSON-like encoding |js_encode()|
56
57Common combination are:
58- Using a job connected through pipes in NL mode. E.g., to run a style
59 checker and receive errors and warnings.
60- Using a deamon, connecting over a socket in JSON mode. E.g. to lookup
61 crosss-refrences in a database.
62
63==============================================================================
642. Channel demo *channel-demo*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010065
66This requires Python. The demo program can be found in
67$VIMRUNTIME/tools/demoserver.py
68Run it in one terminal. We will call this T1.
69
70Run Vim in another terminal. Connect to the demo server with: >
Bram Moolenaar38a55632016-02-15 22:07:32 +010071 let channel = ch_open('localhost:8765')
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010072
73In T1 you should see:
74 === socket opened === ~
75
76You can now send a message to the server: >
Bram Moolenaar38a55632016-02-15 22:07:32 +010077 echo ch_sendexpr(channel, 'hello!')
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010078
79The message is received in T1 and a response is sent back to Vim.
80You can see the raw messages in T1. What Vim sends is:
81 [1,"hello!"] ~
82And the response is:
83 [1,"got it"] ~
84The number will increase every time you send a message.
85
86The server can send a command to Vim. Type this on T1 (literally, including
Bram Moolenaarfb1f6262016-01-31 20:24:32 +010087the quotes):
88 ["ex","echo 'hi there'"] ~
89And you should see the message in Vim. You can move the cursor a word forward:
90 ["normal","w"] ~
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010091
92To handle asynchronous communication a callback needs to be used: >
Bram Moolenaar38a55632016-02-15 22:07:32 +010093 func MyHandler(channel, msg)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010094 echo "from the handler: " . a:msg
95 endfunc
Bram Moolenaar02e83b42016-02-21 20:10:26 +010096 call ch_sendexpr(channel, 'hello!', {'callback': "MyHandler"})
Bram Moolenaar38a55632016-02-15 22:07:32 +010097Vim will not wait for a response. Now the server can send the response later
98and MyHandler will be invoked.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010099
100Instead of giving a callback with every send call, it can also be specified
101when opening the channel: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100102 call ch_close(channel)
103 let channel = ch_open('localhost:8765', {'callback': "MyHandler"})
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100104 call ch_sendexpr(channel, 'hello!', {'callback': 0})
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100105
106==============================================================================
Bram Moolenaar38a55632016-02-15 22:07:32 +01001073. Opening a channel *channel-open*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100108
Bram Moolenaar681baaf2016-02-04 20:57:07 +0100109To open a channel: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100110 let channel = ch_open({address} [, {options}])
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100111 if ch_status(channel) == "open"
112 " use the channel
Bram Moolenaar38a55632016-02-15 22:07:32 +0100113
114Use |ch_status()| to see if the channel could be opened.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100115
116{address} has the form "hostname:port". E.g., "localhost:8765".
117
Bram Moolenaar38a55632016-02-15 22:07:32 +0100118{options} is a dictionary with optional entries:
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100119
120"mode" can be: *channel-mode*
121 "json" - Use JSON, see below; most convenient way. Default.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100122 "js" - Use JS (JavaScript) encoding, more efficient than JSON.
Bram Moolenaar38a55632016-02-15 22:07:32 +0100123 "nl" - Use messages that end in a NL character
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100124 "raw" - Use raw messages
125
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100126"in-mode" mode specifically for stdin, only when using pipes
127"out-mode" mode specifically for stdout, only when using pipes
128"err-mode" mode specifically for stderr, only when using pipes
129 Note: when setting "mode" the part specific mode is
130 overwritten. Therefore set "mode" first and the part specific
131 mode later.
132
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100133 *channel-callback*
Bram Moolenaar38a55632016-02-15 22:07:32 +0100134"callback" A function that is called when a message is received that is
135 not handled otherwise. It gets two arguments: the channel
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100136 and the received message. Example: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100137 func Handle(channel, msg)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100138 echo 'Received: ' . a:msg
139 endfunc
Bram Moolenaar38a55632016-02-15 22:07:32 +0100140 let channel = ch_open("localhost:8765", {"callback": "Handle"})
141<
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100142 When "mode" is "json" or "js" the "msg" argument is the body
143 of the received message, converted to Vim types.
144 When "mode" is "nl" the "msg" argument is one message,
145 excluding the NL.
146 When "mode" is "raw" the "msg" argument is the whole message
147 as a string.
148 *out-cb*
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100149"out-cb" A function like "callback" but used for stdout. Only for when
150 the channel uses pipes. When "out-cb" wasn't set the channel
151 callback is used.
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100152 *err-cb*
Bram Moolenaar38a55632016-02-15 22:07:32 +0100153"err-cb" A function like "callback" but used for stderr. Only for when
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100154 the channel uses pipes. When "err-cb" wasn't set the channel
155 callback is used.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100156
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100157 TODO: *close-cb*
Bram Moolenaar38a55632016-02-15 22:07:32 +0100158"close-cb" A function that is called when the channel gets closed, other
159 than by calling ch_close(). It should be defined like this: >
160 func MyCloseHandler(channel)
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100161< *waittime*
Bram Moolenaar38a55632016-02-15 22:07:32 +0100162"waittime" The time to wait for the connection to be made in
163 milliseconds. The default is zero, don't wait, which is
164 useful if the server is supposed to be running already. A
165 negative number waits forever.
166
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100167"timeout" The time to wait for a request when blocking, E.g. when using
168 ch_sendexpr(). In milliseconds. The default is 2000 (2
Bram Moolenaar38a55632016-02-15 22:07:32 +0100169 seconds).
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100170 *out-timeout* *err-timeout*
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100171"out-timeout" Timeout for stdout. Only when using pipes.
172"err-timeout" Timeout for stderr. Only when using pipes.
173 Note: when setting "timeout" the part specific mode is
174 overwritten. Therefore set "timeout" first and the part
175 specific mode later.
176
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100177When "mode" is "json" or "js" the "callback" is optional. When omitted it is
178only possible to receive a message after sending one.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100179
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100180To change the channel options after opening it use |ch_setoptions()|. The
181arguments are similar to what is passed to |ch_open()|, but "waittime" cannot
182be given, since that only applies to opening the channel.
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100183
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100184For example, the handler can be added or changed: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100185 call ch_setoptions(channel, {'callback': callback})
186When "callback" is empty (zero or an empty string) the handler is removed.
187
188The timeout can be changed: >
189 call ch_setoptions(channel, {'timeout': msec})
190<
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100191 *channel-close* *E906*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100192Once done with the channel, disconnect it like this: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100193 call ch_close(channel)
194When a socket is used this will close the socket for both directions. When
195pipes are used (stdin/stdout/stderr) they are all closed. This might not be
196what you want! Stopping the job with job_stop() might be better.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100197
Bram Moolenaarcbebd482016-02-07 23:02:56 +0100198When the channel can't be opened you will get an error message. There is a
199difference between MS-Windows and Unix: On Unix when the port doesn't exist
200ch_open() fails quickly. On MS-Windows "waittime" applies.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100201*E898* *E899* *E900* *E901* *E902*
202
203If there is an error reading or writing a channel it will be closed.
204*E896* *E630* *E631*
205
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100206==============================================================================
Bram Moolenaar38a55632016-02-15 22:07:32 +01002074. Using a JSON or JS channel *channel-use*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100208
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100209If mode is JSON then a message can be sent synchronously like this: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100210 let response = ch_sendexpr(channel, {expr})
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100211This awaits a response from the other side.
212
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100213When mode is JS this works the same, except that the messages use
Bram Moolenaar38a55632016-02-15 22:07:32 +0100214JavaScript encoding. See |js_encode()| for the difference.
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100215
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100216To send a message, without handling a response or letting the channel callback
217handle the response: >
218 call ch_sendexpr(channel, {expr}, {'callback': 0})
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100219
220To send a message and letting the response handled by a specific function,
221asynchronously: >
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100222 call ch_sendexpr(channel, {expr}, {'callback': Handler})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100223
224Vim will match the response with the request using the message ID. Once the
225response is received the callback will be invoked. Further responses with the
226same ID will be ignored. If your server sends back multiple responses you
227need to send them with ID zero, they will be passed to the channel callback.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100228
229The {expr} is converted to JSON and wrapped in an array. An example of the
230message that the receiver will get when {expr} is the string "hello":
231 [12,"hello"] ~
232
233The format of the JSON sent is:
234 [{number},{expr}]
235
236In which {number} is different every time. It must be used in the response
237(if any):
238
239 [{number},{response}]
240
241This way Vim knows which sent message matches with which received message and
242can call the right handler. Also when the messages arrive out of order.
243
244The sender must always send valid JSON to Vim. Vim can check for the end of
245the message by parsing the JSON. It will only accept the message if the end
246was received.
247
248When the process wants to send a message to Vim without first receiving a
249message, it must use the number zero:
250 [0,{response}]
251
252Then channel handler will then get {response} converted to Vim types. If the
253channel does not have a handler the message is dropped.
254
Bram Moolenaare0fa3742016-02-20 15:47:01 +0100255On read error or ch_close(), when using a socket, the string "DETACH" is sent,
256if still possible. The channel will then be inactive.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100257
Bram Moolenaarcbebd482016-02-07 23:02:56 +0100258It is also possible to use ch_sendraw() on a JSON or JS channel. The caller
259is then completely responsible for correct encoding and decoding.
260
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100261==============================================================================
Bram Moolenaar38a55632016-02-15 22:07:32 +01002625. Channel commands *channel-commands*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100263
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100264With a JSON channel the process can send commands to Vim that will be
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100265handled by Vim internally, it does not require a handler for the channel.
266
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100267Possible commands are: *E903* *E904* *E905*
268 ["redraw" {forced}]
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100269 ["ex", {Ex command}]
270 ["normal", {Normal mode command}]
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100271 ["expr", {expression}, {number}]
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100272 ["expr", {expression}]
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100273 ["call", {func name}, {argument list}, {number}]
274 ["call", {func name}, {argument list}]
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100275
276With all of these: Be careful what these commands do! You can easily
277interfere with what the user is doing. To avoid trouble use |mode()| to check
278that the editor is in the expected state. E.g., to send keys that must be
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100279inserted as text, not executed as a command:
280 ["ex","if mode() == 'i' | call feedkeys('ClassName') | endif"] ~
281
282Errors in these commands are normally not reported to avoid them messing up
283the display. If you do want to see them, set the 'verbose' option to 3 or
284higher.
285
286
287Command "redraw" ~
288
289The other commands do not update the screen, so that you can send a sequence
290of commands without the cursor moving around. You must end with the "redraw"
291command to show any changed text and show the cursor where it belongs.
292
293The argument is normally an empty string:
294 ["redraw", ""] ~
295To first clear the screen pass "force":
296 ["redraw", "force"] ~
297
298
299Command "ex" ~
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100300
301The "ex" command is executed as any Ex command. There is no response for
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100302completion or error. You could use functions in an |autoload| script:
303 ["ex","call myscript#MyFunc(arg)"]
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100304
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100305You can also use "call |feedkeys()|" to insert any key sequence.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100306
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100307
308Command "normal" ~
309
Bram Moolenaar681baaf2016-02-04 20:57:07 +0100310The "normal" command is executed like with ":normal!", commands are not
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100311mapped. Example to open the folds under the cursor:
312 ["normal" "zO"]
313
314
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100315Command "expr" with response ~
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100316
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100317The "expr" command can be used to get the result of an expression. For
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100318example, to get the number of lines in the current buffer:
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100319 ["expr","line('$')", -2] ~
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100320
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100321It will send back the result of the expression:
Bram Moolenaare0fa3742016-02-20 15:47:01 +0100322 [-2, "last line"] ~
323The format is:
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100324 [{number}, {result}]
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100325 *E915*
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100326Here {number} is the same as what was in the request. Use a negative number
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100327to avoid confusion with message that Vim sends. Use a different number on
328every request to be able to match the request with the response.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100329
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100330{result} is the result of the evaluation and is JSON encoded. If the
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100331evaluation fails or the result can't be encoded in JSON it is the string
332"ERROR".
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100333
334
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100335Command "expr" without a response ~
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100336
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100337This command is similar to "expr" above, but does not send back any response.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100338Example:
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100339 ["expr","setline('$', ['one', 'two', 'three'])"] ~
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100340There is no third argument in the request.
341
342
343Command "call" ~
344
345This is similar to "expr", but instead of passing the whole expression as a
346string this passes the name of a function and a list of arguments. This
347avoids the conversion of the arguments to a string and escaping and
348concatenating them. Example:
349 ["call", "line", ["$"], -2] ~
350
351Leave out the fourth argument if no response is to be sent:
352 ["call", "setline", ["$", ["one", "two", "three"]]] ~
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100353
354==============================================================================
Bram Moolenaar38a55632016-02-15 22:07:32 +01003556. Using a RAW or NL channel *channel-raw*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100356
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100357If mode is RAW or NL then a message can be send like this: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100358 let response = ch_sendraw(channel, {string})
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100359
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100360The {string} is sent as-is. The response will be what can be read from the
361channel right away. Since Vim doesn't know how to recognize the end of the
Bram Moolenaar38a55632016-02-15 22:07:32 +0100362message you need to take care of it yourself. The timeout applies for reading
363the first byte, after that it will not wait for anything more.
364
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100365If mode is "nl" you can send a message in a similar way. You are expected
Bram Moolenaar38a55632016-02-15 22:07:32 +0100366to put in the NL after each message. Thus you can also send several messages
367ending in a NL at once. The response will be the text up to and including the
368first NL. This can also be just the NL for an empty response.
369If no NL was read before the channel timeout an empty string is returned.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100370
371To send a message, without expecting a response: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100372 call ch_sendraw(channel, {string}, 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100373The process can send back a response, the channel handler will be called with
374it.
375
376To send a message and letting the response handled by a specific function,
377asynchronously: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100378 call ch_sendraw(channel, {string}, {callback})
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100379
Bram Moolenaar38a55632016-02-15 22:07:32 +0100380This {string} can also be JSON, use |json_encode()| to create it and
381|json_decode()| to handle a received JSON message.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100382
Bram Moolenaarcbebd482016-02-07 23:02:56 +0100383It is not possible to use |ch_sendexpr()| on a raw channel.
384
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100385==============================================================================
Bram Moolenaar38a55632016-02-15 22:07:32 +01003867. More channel functions *channel-more*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100387
Bram Moolenaar38a55632016-02-15 22:07:32 +0100388To obtain the status of a channel: ch_status(channel). The possible results
389are:
390 "fail" Failed to open the channel.
391 "open" The channel can be used.
392 "closed" The channel was closed.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100393
394TODO:
Bram Moolenaar38a55632016-02-15 22:07:32 +0100395To objain the job associated with a channel: ch_getjob(channel)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100396
Bram Moolenaar38a55632016-02-15 22:07:32 +0100397To read one message from a channel: >
398 let output = ch_read(channel)
399This uses the channel timeout. To read without a timeout, just get any
400message that is available: >
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100401 let output = ch_read(channel, {'timeout': 0})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100402When no message was available then the result is v:none for a JSON or JS mode
403channels, an empty string for a RAW or NL channel.
404
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100405To read all output from a RAW channel that is available: >
406 let output = ch_readraw(channel)
Bram Moolenaar38a55632016-02-15 22:07:32 +0100407To read the error output: >
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100408 let output = ch_readraw(channel, {"part": "err"})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100409
410==============================================================================
4118. Starting a job with a channel *job-start* *job*
412
413To start a job and open a channel for stdin/stdout/stderr: >
414 let job = job_start(command, {options})
415
416You can get the channel with: >
417 let channel = job_getchannel(job)
418
419The channel will use NL mode. If you want another mode it's best to specify
420this in {options}. When changing the mode later some text may have already
421been received and not parsed correctly.
422
423If the command produces a line of output that you want to deal with, specify
424a handler for stdout: >
425 let job = job_start(command, {"out-cb": "MyHandler"})
426The function will be called with the channel and a message. You would define
427it like this: >
428 func MyHandler(channel, msg)
429
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100430Without the handler you need to read the output with |ch_read()| or
431|ch_readraw()|.
Bram Moolenaar38a55632016-02-15 22:07:32 +0100432
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100433The handler defined for "out-cb" will not receive stderr. If you want to
Bram Moolenaar38a55632016-02-15 22:07:32 +0100434handle that separately, add an "err-cb" handler: >
435 let job = job_start(command, {"out-cb": "MyHandler",
436 \ "err-cb": "ErrHandler"})
437
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100438If you want to handle both stderr and stdout with one handler use the
439"callback" option: >
440 let job = job_start(command, {"callback": "MyHandler"})
441
Bram Moolenaar38a55632016-02-15 22:07:32 +0100442You can send a message to the command with ch_sendraw(). If the channel is in
443JSON or JS mode you can use ch_sendexpr().
444
445There are several options you can use, see |job-options|.
446
447TODO:
448To run a job and read its output once it is done: >
449
450 let job = job_start({command}, {'exit-cb': 'MyHandler'})
451 func MyHandler(job, status)
452 let channel = job_getchannel()
453 let output = ch_readall(channel)
454 " parse output
455 endfunc
456
457==============================================================================
4589. Starting a job without a channel *job-start-nochannel*
459
460To start another process without creating a channel: >
461 let job = job_start(command, {"in-io": "null", "out-io": "null"})
462
463This starts {command} in the background, Vim does not wait for it to finish.
464
465TODO:
466When Vim sees that neither stdin, stdout or stderr are connected, no channel
467will be created. Often you will want to include redirection in the command to
468avoid it getting stuck.
469
470There are several options you can use, see |job-options|.
471
472TODO: *job-may-start*
473To start a job only when connecting to an address does not work use
474job_maystart('command', {address}, {options}), For Example: >
475 let job = job_maystart(command, address, {"waittime": 1000})
476 let channel = job_gethandle(job)
477
478This comes down to: >
479 let channel = ch_open(address, {"waittime": 0})
480 if ch_status(channel) == "fail"
481 let job = job_start(command)
482 let channel = ch_open(address, {"waittime": 1000})
483 call job_sethandle(channel)
484 endif
485Note that the specified waittime applies to when the job has been started.
486This gives the job some time to make the port available.
487
488==============================================================================
48910. Job options *job-options*
490
491The {options} argument in job_start() is a dictionary. All entries are
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100492optional. Some options can be used after the job has started, using
493job_setoptions(job, {options}). Many options can be used with the channel
494related to the job, using ch_setoptions(channel, {options}).
495See |job_setoptions()| and |ch_setoptions()|.
Bram Moolenaar38a55632016-02-15 22:07:32 +0100496
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100497 *job-callback*
498"callback": handler Callback for something to read on any part of the
499 channel.
500 *job-out-cb*
Bram Moolenaar38a55632016-02-15 22:07:32 +0100501"out-cb": handler Callback for when there is something to read on
502 stdout.
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100503 *job-err-cb*
Bram Moolenaar38a55632016-02-15 22:07:32 +0100504"err-cb": handler Callback for when there is something to read on
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100505 stderr.
Bram Moolenaar38a55632016-02-15 22:07:32 +0100506TODO: *job-close-cb*
507"close-cb": handler Callback for when the channel is closed. Same as
508 "close-cb" on ch_open().
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100509 *job-exit-cb*
Bram Moolenaar38a55632016-02-15 22:07:32 +0100510"exit-cb": handler Callback for when the job ends. The arguments are the
511 job and the exit status.
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100512 Vim checks about every 10 seconds for jobs that ended.
513 The callback can also be triggered by calling
514 |job_status()|.
515 *job-stoponexit*
516"stoponexit": {signal} Send {signal} to the job when Vim exits. See
517 |job_stop()| for possible values.
518"stoponexit": "" Do not stop the job when Vim exits.
519 The default is "term".
520
Bram Moolenaar38a55632016-02-15 22:07:32 +0100521TODO: *job-term*
522"term": "open" Start a terminal and connect the job
523 stdin/stdout/stderr to it.
524
525TODO: *job-in-io*
526"in-io": "null" disconnect stdin
527"in-io": "pipe" stdin is connected to the channel (default)
528"in-io": "file" stdin reads from a file
529"in-file": "/path/file" the file to read from
530
531TODO: *job-out-io*
532"out-io": "null" disconnect stdout
533"out-io": "pipe" stdout is connected to the channel (default)
534"out-io": "file" stdout writes to a file
535"out-file": "/path/file" the file to write to
536"out-io": "buffer" stdout appends to a buffer
537"out-buffer": "name" buffer to append to
538
539TODO: *job-err-io*
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100540"err-io": "out" same type as stdout (default)
Bram Moolenaar38a55632016-02-15 22:07:32 +0100541"err-io": "null" disconnect stderr
542"err-io": "pipe" stderr is connected to the channel
543"err-io": "file" stderr writes to a file
544"err-file": "/path/file" the file to write to
545"err-io": "buffer" stderr appends to a buffer
546"err-buffer": "name" buffer to append to
547
Bram Moolenaar38a55632016-02-15 22:07:32 +0100548==============================================================================
54911. Controlling a job *job-control*
550
551To get the status of a job: >
552 echo job_status(job)
553
554To make a job stop running: >
555 job_stop(job)
556
557This is the normal way to end a job. On Unix it sends a SIGTERM to the job.
558It is possible to use other ways to stop the job, or even send arbitrary
559signals. E.g. to force a job to stop, "kill it": >
560 job_stop(job, "kill")
561
562For more options see |job_stop()|.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100563
564
565 vim:tw=78:ts=8:ft=help:norl: