blob: aca6b087757ed8e2bcd9fc1124ff7162b5eb5145 [file] [log] [blame]
Bram Moolenaare0fa3742016-02-20 15:47:01 +01001*channel.txt* For Vim version 7.4. Last change: 2016 Feb 20
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 Moolenaar38a55632016-02-15 22:07:32 +010096 call ch_sendexpr(channel, 'hello!', "MyHandler")
97Vim 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"})
104 call ch_sendexpr(channel, 'hello!', 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}])
111
112Use |ch_status()| to see if the channel could be opened.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100113
114{address} has the form "hostname:port". E.g., "localhost:8765".
115
Bram Moolenaar38a55632016-02-15 22:07:32 +0100116{options} is a dictionary with optional entries:
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100117
118"mode" can be: *channel-mode*
119 "json" - Use JSON, see below; most convenient way. Default.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100120 "js" - Use JS (JavaScript) encoding, more efficient than JSON.
Bram Moolenaar38a55632016-02-15 22:07:32 +0100121 "nl" - Use messages that end in a NL character
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100122 "raw" - Use raw messages
123
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100124"in-mode" mode specifically for stdin, only when using pipes
125"out-mode" mode specifically for stdout, only when using pipes
126"err-mode" mode specifically for stderr, only when using pipes
127 Note: when setting "mode" the part specific mode is
128 overwritten. Therefore set "mode" first and the part specific
129 mode later.
130
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100131 *channel-callback*
Bram Moolenaar38a55632016-02-15 22:07:32 +0100132"callback" A function that is called when a message is received that is
133 not handled otherwise. It gets two arguments: the channel
134 handle and the received message. Example: >
135 func Handle(channel, msg)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100136 echo 'Received: ' . a:msg
137 endfunc
Bram Moolenaar38a55632016-02-15 22:07:32 +0100138 let channel = ch_open("localhost:8765", {"callback": "Handle"})
139<
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100140"out-cb" A function like "callback" but used for stdout. Only for when
141 the channel uses pipes. When "out-cb" wasn't set the channel
142 callback is used.
143
Bram Moolenaar38a55632016-02-15 22:07:32 +0100144"err-cb" A function like "callback" but used for stderr. Only for when
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100145 the channel uses pipes. When "err-cb" wasn't set the channel
146 callback is used.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100147
Bram Moolenaar38a55632016-02-15 22:07:32 +0100148 TODO:
149"close-cb" A function that is called when the channel gets closed, other
150 than by calling ch_close(). It should be defined like this: >
151 func MyCloseHandler(channel)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100152
Bram Moolenaar38a55632016-02-15 22:07:32 +0100153"waittime" The time to wait for the connection to be made in
154 milliseconds. The default is zero, don't wait, which is
155 useful if the server is supposed to be running already. A
156 negative number waits forever.
157
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100158"timeout" The time to wait for a request when blocking, E.g. when using
159 ch_sendexpr(). In milliseconds. The default is 2000 (2
Bram Moolenaar38a55632016-02-15 22:07:32 +0100160 seconds).
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100161
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100162"out-timeout" Timeout for stdout. Only when using pipes.
163"err-timeout" Timeout for stderr. Only when using pipes.
164 Note: when setting "timeout" the part specific mode is
165 overwritten. Therefore set "timeout" first and the part
166 specific mode later.
167
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100168When "mode" is "json" or "js" the "msg" argument is the body of the received
169message, converted to Vim types.
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100170When "mode" is "raw" the "msg" argument is the whole message as a string.
171
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100172When "mode" is "json" or "js" the "callback" is optional. When omitted it is
173only possible to receive a message after sending one.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100174
Bram Moolenaar38a55632016-02-15 22:07:32 +0100175To change the channel options after opening it use ch_setoptions(). The
176arguments are similar to what is passed to ch_open(), but "waittime" cannot be
177given, since that only applies to opening the channel.
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100178
Bram Moolenaar38a55632016-02-15 22:07:32 +0100179The handler can be added or changed: >
180 call ch_setoptions(channel, {'callback': callback})
181When "callback" is empty (zero or an empty string) the handler is removed.
182
183The timeout can be changed: >
184 call ch_setoptions(channel, {'timeout': msec})
185<
Bram Moolenaar835dc632016-02-07 14:27:38 +0100186 *E906*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100187Once done with the channel, disconnect it like this: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100188 call ch_close(channel)
189When a socket is used this will close the socket for both directions. When
190pipes are used (stdin/stdout/stderr) they are all closed. This might not be
191what you want! Stopping the job with job_stop() might be better.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100192
Bram Moolenaar38a55632016-02-15 22:07:32 +0100193TODO:
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100194Currently up to 10 channels can be in use at the same time. *E897*
195
Bram Moolenaarcbebd482016-02-07 23:02:56 +0100196When the channel can't be opened you will get an error message. There is a
197difference between MS-Windows and Unix: On Unix when the port doesn't exist
198ch_open() fails quickly. On MS-Windows "waittime" applies.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100199*E898* *E899* *E900* *E901* *E902*
200
201If there is an error reading or writing a channel it will be closed.
202*E896* *E630* *E631*
203
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100204==============================================================================
Bram Moolenaar38a55632016-02-15 22:07:32 +01002054. Using a JSON or JS channel *channel-use*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100206
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100207If mode is JSON then a message can be sent synchronously like this: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100208 let response = ch_sendexpr(channel, {expr})
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100209This awaits a response from the other side.
210
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100211When mode is JS this works the same, except that the messages use
Bram Moolenaar38a55632016-02-15 22:07:32 +0100212JavaScript encoding. See |js_encode()| for the difference.
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100213
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100214To send a message, without handling a response: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100215 call ch_sendexpr(channel, {expr}, 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100216
217To send a message and letting the response handled by a specific function,
218asynchronously: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100219 call ch_sendexpr(channel, {expr}, {callback})
220
221Vim will match the response with the request using the message ID. Once the
222response is received the callback will be invoked. Further responses with the
223same ID will be ignored. If your server sends back multiple responses you
224need to send them with ID zero, they will be passed to the channel callback.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100225
226The {expr} is converted to JSON and wrapped in an array. An example of the
227message that the receiver will get when {expr} is the string "hello":
228 [12,"hello"] ~
229
230The format of the JSON sent is:
231 [{number},{expr}]
232
233In which {number} is different every time. It must be used in the response
234(if any):
235
236 [{number},{response}]
237
238This way Vim knows which sent message matches with which received message and
239can call the right handler. Also when the messages arrive out of order.
240
241The sender must always send valid JSON to Vim. Vim can check for the end of
242the message by parsing the JSON. It will only accept the message if the end
243was received.
244
245When the process wants to send a message to Vim without first receiving a
246message, it must use the number zero:
247 [0,{response}]
248
249Then channel handler will then get {response} converted to Vim types. If the
250channel does not have a handler the message is dropped.
251
Bram Moolenaare0fa3742016-02-20 15:47:01 +0100252On read error or ch_close(), when using a socket, the string "DETACH" is sent,
253if still possible. The channel will then be inactive.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100254
Bram Moolenaarcbebd482016-02-07 23:02:56 +0100255It is also possible to use ch_sendraw() on a JSON or JS channel. The caller
256is then completely responsible for correct encoding and decoding.
257
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100258==============================================================================
Bram Moolenaar38a55632016-02-15 22:07:32 +01002595. Channel commands *channel-commands*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100260
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100261With a JSON channel the process can send commands to Vim that will be
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100262handled by Vim internally, it does not require a handler for the channel.
263
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100264Possible commands are: *E903* *E904* *E905*
265 ["redraw" {forced}]
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100266 ["ex", {Ex command}]
267 ["normal", {Normal mode command}]
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100268 ["expr", {expression}, {number}]
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100269 ["expr", {expression}]
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100270 ["call", {func name}, {argument list}, {number}]
271 ["call", {func name}, {argument list}]
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100272
273With all of these: Be careful what these commands do! You can easily
274interfere with what the user is doing. To avoid trouble use |mode()| to check
275that the editor is in the expected state. E.g., to send keys that must be
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100276inserted as text, not executed as a command:
277 ["ex","if mode() == 'i' | call feedkeys('ClassName') | endif"] ~
278
279Errors in these commands are normally not reported to avoid them messing up
280the display. If you do want to see them, set the 'verbose' option to 3 or
281higher.
282
283
284Command "redraw" ~
285
286The other commands do not update the screen, so that you can send a sequence
287of commands without the cursor moving around. You must end with the "redraw"
288command to show any changed text and show the cursor where it belongs.
289
290The argument is normally an empty string:
291 ["redraw", ""] ~
292To first clear the screen pass "force":
293 ["redraw", "force"] ~
294
295
296Command "ex" ~
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100297
298The "ex" command is executed as any Ex command. There is no response for
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100299completion or error. You could use functions in an |autoload| script:
300 ["ex","call myscript#MyFunc(arg)"]
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100301
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100302You can also use "call |feedkeys()|" to insert any key sequence.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100303
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100304
305Command "normal" ~
306
Bram Moolenaar681baaf2016-02-04 20:57:07 +0100307The "normal" command is executed like with ":normal!", commands are not
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100308mapped. Example to open the folds under the cursor:
309 ["normal" "zO"]
310
311
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100312Command "expr" with response ~
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100313
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100314The "expr" command can be used to get the result of an expression. For
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100315example, to get the number of lines in the current buffer:
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100316 ["expr","line('$')", -2] ~
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100317
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100318It will send back the result of the expression:
Bram Moolenaare0fa3742016-02-20 15:47:01 +0100319 [-2, "last line"] ~
320The format is:
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100321 [{number}, {result}]
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100322 *E915*
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100323Here {number} is the same as what was in the request. Use a negative number
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100324to avoid confusion with message that Vim sends. Use a different number on
325every request to be able to match the request with the response.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100326
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100327{result} is the result of the evaluation and is JSON encoded. If the
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100328evaluation fails or the result can't be encoded in JSON it is the string
329"ERROR".
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100330
331
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100332Command "expr" without a response ~
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100333
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100334This command is similar to "expr" above, but does not send back any response.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100335Example:
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100336 ["expr","setline('$', ['one', 'two', 'three'])"] ~
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100337There is no third argument in the request.
338
339
340Command "call" ~
341
342This is similar to "expr", but instead of passing the whole expression as a
343string this passes the name of a function and a list of arguments. This
344avoids the conversion of the arguments to a string and escaping and
345concatenating them. Example:
346 ["call", "line", ["$"], -2] ~
347
348Leave out the fourth argument if no response is to be sent:
349 ["call", "setline", ["$", ["one", "two", "three"]]] ~
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100350
351==============================================================================
Bram Moolenaar38a55632016-02-15 22:07:32 +01003526. Using a RAW or NL channel *channel-raw*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100353
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100354If mode is RAW or NL then a message can be send like this: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100355 let response = ch_sendraw(channel, {string})
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100356
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100357The {string} is sent as-is. The response will be what can be read from the
358channel right away. Since Vim doesn't know how to recognize the end of the
Bram Moolenaar38a55632016-02-15 22:07:32 +0100359message you need to take care of it yourself. The timeout applies for reading
360the first byte, after that it will not wait for anything more.
361
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100362If mode is "nl" you can send a message in a similar way. You are expected
Bram Moolenaar38a55632016-02-15 22:07:32 +0100363to put in the NL after each message. Thus you can also send several messages
364ending in a NL at once. The response will be the text up to and including the
365first NL. This can also be just the NL for an empty response.
366If no NL was read before the channel timeout an empty string is returned.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100367
368To send a message, without expecting a response: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100369 call ch_sendraw(channel, {string}, 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100370The process can send back a response, the channel handler will be called with
371it.
372
373To send a message and letting the response handled by a specific function,
374asynchronously: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100375 call ch_sendraw(channel, {string}, {callback})
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100376
Bram Moolenaar38a55632016-02-15 22:07:32 +0100377This {string} can also be JSON, use |json_encode()| to create it and
378|json_decode()| to handle a received JSON message.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100379
Bram Moolenaarcbebd482016-02-07 23:02:56 +0100380It is not possible to use |ch_sendexpr()| on a raw channel.
381
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100382==============================================================================
Bram Moolenaar38a55632016-02-15 22:07:32 +01003837. More channel functions *channel-more*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100384
Bram Moolenaar38a55632016-02-15 22:07:32 +0100385To obtain the status of a channel: ch_status(channel). The possible results
386are:
387 "fail" Failed to open the channel.
388 "open" The channel can be used.
389 "closed" The channel was closed.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100390
391TODO:
Bram Moolenaar38a55632016-02-15 22:07:32 +0100392To objain the job associated with a channel: ch_getjob(channel)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100393
Bram Moolenaar38a55632016-02-15 22:07:32 +0100394To read one message from a channel: >
395 let output = ch_read(channel)
396This uses the channel timeout. To read without a timeout, just get any
397message that is available: >
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100398 let output = ch_read(channel, {'timeout': 0})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100399When no message was available then the result is v:none for a JSON or JS mode
400channels, an empty string for a RAW or NL channel.
401
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100402To read all output from a RAW channel that is available: >
403 let output = ch_readraw(channel)
Bram Moolenaar38a55632016-02-15 22:07:32 +0100404To read the error output: >
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100405 let output = ch_readraw(channel, {"part": "err"})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100406
407==============================================================================
4088. Starting a job with a channel *job-start* *job*
409
410To start a job and open a channel for stdin/stdout/stderr: >
411 let job = job_start(command, {options})
412
413You can get the channel with: >
414 let channel = job_getchannel(job)
415
416The channel will use NL mode. If you want another mode it's best to specify
417this in {options}. When changing the mode later some text may have already
418been received and not parsed correctly.
419
420If the command produces a line of output that you want to deal with, specify
421a handler for stdout: >
422 let job = job_start(command, {"out-cb": "MyHandler"})
423The function will be called with the channel and a message. You would define
424it like this: >
425 func MyHandler(channel, msg)
426
427Without the handler you need to read the output with ch_read().
428
429The handler defined for "out-cb" will also receive stderr. If you want to
430handle that separately, add an "err-cb" handler: >
431 let job = job_start(command, {"out-cb": "MyHandler",
432 \ "err-cb": "ErrHandler"})
433
434You can send a message to the command with ch_sendraw(). If the channel is in
435JSON or JS mode you can use ch_sendexpr().
436
437There are several options you can use, see |job-options|.
438
439TODO:
440To run a job and read its output once it is done: >
441
442 let job = job_start({command}, {'exit-cb': 'MyHandler'})
443 func MyHandler(job, status)
444 let channel = job_getchannel()
445 let output = ch_readall(channel)
446 " parse output
447 endfunc
448
449==============================================================================
4509. Starting a job without a channel *job-start-nochannel*
451
452To start another process without creating a channel: >
453 let job = job_start(command, {"in-io": "null", "out-io": "null"})
454
455This starts {command} in the background, Vim does not wait for it to finish.
456
457TODO:
458When Vim sees that neither stdin, stdout or stderr are connected, no channel
459will be created. Often you will want to include redirection in the command to
460avoid it getting stuck.
461
462There are several options you can use, see |job-options|.
463
464TODO: *job-may-start*
465To start a job only when connecting to an address does not work use
466job_maystart('command', {address}, {options}), For Example: >
467 let job = job_maystart(command, address, {"waittime": 1000})
468 let channel = job_gethandle(job)
469
470This comes down to: >
471 let channel = ch_open(address, {"waittime": 0})
472 if ch_status(channel) == "fail"
473 let job = job_start(command)
474 let channel = ch_open(address, {"waittime": 1000})
475 call job_sethandle(channel)
476 endif
477Note that the specified waittime applies to when the job has been started.
478This gives the job some time to make the port available.
479
480==============================================================================
48110. Job options *job-options*
482
483The {options} argument in job_start() is a dictionary. All entries are
484optional. The same options can be used with job_setoptions(job, {options}).
485
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100486 *job-callback*
487"callback": handler Callback for something to read on any part of the
488 channel.
489 *job-out-cb*
Bram Moolenaar38a55632016-02-15 22:07:32 +0100490"out-cb": handler Callback for when there is something to read on
491 stdout.
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100492 *job-err-cb*
Bram Moolenaar38a55632016-02-15 22:07:32 +0100493"err-cb": handler Callback for when there is something to read on
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100494 stderr.
Bram Moolenaar38a55632016-02-15 22:07:32 +0100495TODO: *job-close-cb*
496"close-cb": handler Callback for when the channel is closed. Same as
497 "close-cb" on ch_open().
498TODO: *job-exit-cb*
499"exit-cb": handler Callback for when the job ends. The arguments are the
500 job and the exit status.
501TODO: *job-killonexit*
502"killonexit": 1 Stop the job when Vim exits.
503"killonexit": 0 Do not stop the job when Vim exits.
504 The default is 1.
505TODO: *job-term*
506"term": "open" Start a terminal and connect the job
507 stdin/stdout/stderr to it.
508
509TODO: *job-in-io*
510"in-io": "null" disconnect stdin
511"in-io": "pipe" stdin is connected to the channel (default)
512"in-io": "file" stdin reads from a file
513"in-file": "/path/file" the file to read from
514
515TODO: *job-out-io*
516"out-io": "null" disconnect stdout
517"out-io": "pipe" stdout is connected to the channel (default)
518"out-io": "file" stdout writes to a file
519"out-file": "/path/file" the file to write to
520"out-io": "buffer" stdout appends to a buffer
521"out-buffer": "name" buffer to append to
522
523TODO: *job-err-io*
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100524"err-io": "out" same type as stdout (default)
Bram Moolenaar38a55632016-02-15 22:07:32 +0100525"err-io": "null" disconnect stderr
526"err-io": "pipe" stderr is connected to the channel
527"err-io": "file" stderr writes to a file
528"err-file": "/path/file" the file to write to
529"err-io": "buffer" stderr appends to a buffer
530"err-buffer": "name" buffer to append to
531
532TODO: more options
533
534
535==============================================================================
53611. Controlling a job *job-control*
537
538To get the status of a job: >
539 echo job_status(job)
540
541To make a job stop running: >
542 job_stop(job)
543
544This is the normal way to end a job. On Unix it sends a SIGTERM to the job.
545It is possible to use other ways to stop the job, or even send arbitrary
546signals. E.g. to force a job to stop, "kill it": >
547 job_stop(job, "kill")
548
549For more options see |job_stop()|.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100550
551
552 vim:tw=78:ts=8:ft=help:norl: