blob: 4571258a418fd50b61c525b1078fdf3d5453e900 [file] [log] [blame]
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01001*channel.txt* For Vim version 7.4. Last change: 2016 Feb 27
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 Moolenaar8b1862a2016-02-27 19:21:24 +010077 echo ch_evalexpr(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 Moolenaar8b1862a2016-02-27 19:21:24 +0100104 call ch_sendexpr(channel, 'hello!')
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 Moolenaar187db502016-02-27 14:44:26 +0100133 Note: when writing to a file or buffer NL mode is always used.
134
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100135 *channel-callback*
Bram Moolenaar38a55632016-02-15 22:07:32 +0100136"callback" A function that is called when a message is received that is
137 not handled otherwise. It gets two arguments: the channel
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100138 and the received message. Example: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100139 func Handle(channel, msg)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100140 echo 'Received: ' . a:msg
141 endfunc
Bram Moolenaar38a55632016-02-15 22:07:32 +0100142 let channel = ch_open("localhost:8765", {"callback": "Handle"})
143<
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100144 When "mode" is "json" or "js" the "msg" argument is the body
145 of the received message, converted to Vim types.
146 When "mode" is "nl" the "msg" argument is one message,
147 excluding the NL.
148 When "mode" is "raw" the "msg" argument is the whole message
149 as a string.
150 *out-cb*
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100151"out-cb" A function like "callback" but used for stdout. Only for when
152 the channel uses pipes. When "out-cb" wasn't set the channel
153 callback is used.
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100154 *err-cb*
Bram Moolenaar38a55632016-02-15 22:07:32 +0100155"err-cb" A function like "callback" but used for stderr. Only for when
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100156 the channel uses pipes. When "err-cb" wasn't set the channel
157 callback is used.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100158
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100159 TODO: *close-cb*
Bram Moolenaar38a55632016-02-15 22:07:32 +0100160"close-cb" A function that is called when the channel gets closed, other
161 than by calling ch_close(). It should be defined like this: >
162 func MyCloseHandler(channel)
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100163< *waittime*
Bram Moolenaar38a55632016-02-15 22:07:32 +0100164"waittime" The time to wait for the connection to be made in
Bram Moolenaarf3913272016-02-25 00:00:01 +0100165 milliseconds. A negative number waits forever.
166
167 The default is zero, don't wait, which is useful if a local
168 server is supposed to be running already. On Unix Vim
169 actually uses a 1 msec timeout, that is required on many
170 systems. Use a larger value for a remote server, e.g. 10
171 msec at least.
Bram Moolenaar38a55632016-02-15 22:07:32 +0100172
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100173"timeout" The time to wait for a request when blocking, E.g. when using
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100174 ch_evalexpr(). In milliseconds. The default is 2000 (2
Bram Moolenaar38a55632016-02-15 22:07:32 +0100175 seconds).
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100176 *out-timeout* *err-timeout*
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100177"out-timeout" Timeout for stdout. Only when using pipes.
178"err-timeout" Timeout for stderr. Only when using pipes.
179 Note: when setting "timeout" the part specific mode is
180 overwritten. Therefore set "timeout" first and the part
181 specific mode later.
182
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100183When "mode" is "json" or "js" the "callback" is optional. When omitted it is
184only possible to receive a message after sending one.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100185
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100186To change the channel options after opening it use |ch_setoptions()|. The
187arguments are similar to what is passed to |ch_open()|, but "waittime" cannot
188be given, since that only applies to opening the channel.
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100189
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100190For example, the handler can be added or changed: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100191 call ch_setoptions(channel, {'callback': callback})
192When "callback" is empty (zero or an empty string) the handler is removed.
193
194The timeout can be changed: >
195 call ch_setoptions(channel, {'timeout': msec})
196<
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100197 *channel-close* *E906*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100198Once done with the channel, disconnect it like this: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100199 call ch_close(channel)
200When a socket is used this will close the socket for both directions. When
201pipes are used (stdin/stdout/stderr) they are all closed. This might not be
202what you want! Stopping the job with job_stop() might be better.
Bram Moolenaar187db502016-02-27 14:44:26 +0100203All readahead is discarded, callbacks will no longer be invoked.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100204
Bram Moolenaarcbebd482016-02-07 23:02:56 +0100205When the channel can't be opened you will get an error message. There is a
206difference between MS-Windows and Unix: On Unix when the port doesn't exist
207ch_open() fails quickly. On MS-Windows "waittime" applies.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100208*E898* *E899* *E900* *E901* *E902*
209
210If there is an error reading or writing a channel it will be closed.
211*E896* *E630* *E631*
212
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100213==============================================================================
Bram Moolenaar38a55632016-02-15 22:07:32 +01002144. Using a JSON or JS channel *channel-use*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100215
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100216If mode is JSON then a message can be sent synchronously like this: >
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100217 let response = ch_evalexpr(channel, {expr})
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100218This awaits a response from the other side.
219
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100220When mode is JS this works the same, except that the messages use
Bram Moolenaar38a55632016-02-15 22:07:32 +0100221JavaScript encoding. See |js_encode()| for the difference.
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100222
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100223To send a message, without handling a response or letting the channel callback
224handle the response: >
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100225 call ch_sendexpr(channel, {expr})
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100226
227To send a message and letting the response handled by a specific function,
228asynchronously: >
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100229 call ch_sendexpr(channel, {expr}, {'callback': Handler})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100230
231Vim will match the response with the request using the message ID. Once the
232response is received the callback will be invoked. Further responses with the
233same ID will be ignored. If your server sends back multiple responses you
234need to send them with ID zero, they will be passed to the channel callback.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100235
236The {expr} is converted to JSON and wrapped in an array. An example of the
237message that the receiver will get when {expr} is the string "hello":
238 [12,"hello"] ~
239
240The format of the JSON sent is:
241 [{number},{expr}]
242
243In which {number} is different every time. It must be used in the response
244(if any):
245
246 [{number},{response}]
247
248This way Vim knows which sent message matches with which received message and
249can call the right handler. Also when the messages arrive out of order.
250
251The sender must always send valid JSON to Vim. Vim can check for the end of
252the message by parsing the JSON. It will only accept the message if the end
253was received.
254
255When the process wants to send a message to Vim without first receiving a
256message, it must use the number zero:
257 [0,{response}]
258
259Then channel handler will then get {response} converted to Vim types. If the
260channel does not have a handler the message is dropped.
261
Bram Moolenaare0fa3742016-02-20 15:47:01 +0100262On read error or ch_close(), when using a socket, the string "DETACH" is sent,
Bram Moolenaarf3913272016-02-25 00:00:01 +0100263if still possible. The channel will then be inactive. For a JSON and JS mode
264channel quotes are used around DETACH, otherwise there are no quotes.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100265
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100266It is also possible to use ch_sendraw() and ch_evalraw() on a JSON or JS
267channel. The caller is then completely responsible for correct encoding and
268decoding.
Bram Moolenaarcbebd482016-02-07 23:02:56 +0100269
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100270==============================================================================
Bram Moolenaar38a55632016-02-15 22:07:32 +01002715. Channel commands *channel-commands*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100272
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100273With a JSON channel the process can send commands to Vim that will be
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100274handled by Vim internally, it does not require a handler for the channel.
275
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100276Possible commands are: *E903* *E904* *E905*
277 ["redraw" {forced}]
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100278 ["ex", {Ex command}]
279 ["normal", {Normal mode command}]
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100280 ["expr", {expression}, {number}]
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100281 ["expr", {expression}]
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100282 ["call", {func name}, {argument list}, {number}]
283 ["call", {func name}, {argument list}]
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100284
285With all of these: Be careful what these commands do! You can easily
286interfere with what the user is doing. To avoid trouble use |mode()| to check
287that the editor is in the expected state. E.g., to send keys that must be
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100288inserted as text, not executed as a command:
289 ["ex","if mode() == 'i' | call feedkeys('ClassName') | endif"] ~
290
291Errors in these commands are normally not reported to avoid them messing up
292the display. If you do want to see them, set the 'verbose' option to 3 or
293higher.
294
295
296Command "redraw" ~
297
298The other commands do not update the screen, so that you can send a sequence
299of commands without the cursor moving around. You must end with the "redraw"
300command to show any changed text and show the cursor where it belongs.
301
302The argument is normally an empty string:
303 ["redraw", ""] ~
304To first clear the screen pass "force":
305 ["redraw", "force"] ~
306
307
308Command "ex" ~
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100309
310The "ex" command is executed as any Ex command. There is no response for
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100311completion or error. You could use functions in an |autoload| script:
312 ["ex","call myscript#MyFunc(arg)"]
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100313
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100314You can also use "call |feedkeys()|" to insert any key sequence.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100315
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100316
317Command "normal" ~
318
Bram Moolenaar681baaf2016-02-04 20:57:07 +0100319The "normal" command is executed like with ":normal!", commands are not
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100320mapped. Example to open the folds under the cursor:
321 ["normal" "zO"]
322
323
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100324Command "expr" with response ~
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100325
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100326The "expr" command can be used to get the result of an expression. For
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100327example, to get the number of lines in the current buffer:
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100328 ["expr","line('$')", -2] ~
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100329
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100330It will send back the result of the expression:
Bram Moolenaare0fa3742016-02-20 15:47:01 +0100331 [-2, "last line"] ~
332The format is:
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100333 [{number}, {result}]
Bram Moolenaar187db502016-02-27 14:44:26 +0100334
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100335Here {number} is the same as what was in the request. Use a negative number
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100336to avoid confusion with message that Vim sends. Use a different number on
337every request to be able to match the request with the response.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100338
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100339{result} is the result of the evaluation and is JSON encoded. If the
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100340evaluation fails or the result can't be encoded in JSON it is the string
341"ERROR".
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100342
343
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100344Command "expr" without a response ~
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100345
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100346This command is similar to "expr" above, but does not send back any response.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100347Example:
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100348 ["expr","setline('$', ['one', 'two', 'three'])"] ~
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100349There is no third argument in the request.
350
351
352Command "call" ~
353
354This is similar to "expr", but instead of passing the whole expression as a
355string this passes the name of a function and a list of arguments. This
356avoids the conversion of the arguments to a string and escaping and
357concatenating them. Example:
358 ["call", "line", ["$"], -2] ~
359
360Leave out the fourth argument if no response is to be sent:
361 ["call", "setline", ["$", ["one", "two", "three"]]] ~
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100362
363==============================================================================
Bram Moolenaar38a55632016-02-15 22:07:32 +01003646. Using a RAW or NL channel *channel-raw*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100365
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100366If mode is RAW or NL then a message can be send like this: >
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100367 let response = ch_evalraw(channel, {string})
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100368
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100369The {string} is sent as-is. The response will be what can be read from the
370channel right away. Since Vim doesn't know how to recognize the end of the
Bram Moolenaar38a55632016-02-15 22:07:32 +0100371message you need to take care of it yourself. The timeout applies for reading
372the first byte, after that it will not wait for anything more.
373
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100374If mode is "nl" you can send a message in a similar way. You are expected
Bram Moolenaar38a55632016-02-15 22:07:32 +0100375to put in the NL after each message. Thus you can also send several messages
376ending in a NL at once. The response will be the text up to and including the
377first NL. This can also be just the NL for an empty response.
378If no NL was read before the channel timeout an empty string is returned.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100379
380To send a message, without expecting a response: >
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100381 call ch_sendraw(channel, {string})
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100382The process can send back a response, the channel handler will be called with
383it.
384
385To send a message and letting the response handled by a specific function,
386asynchronously: >
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100387 call ch_sendraw(channel, {string}, {'callback': 'MyHandler'})
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100388
Bram Moolenaar38a55632016-02-15 22:07:32 +0100389This {string} can also be JSON, use |json_encode()| to create it and
390|json_decode()| to handle a received JSON message.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100391
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100392It is not possible to use |ch_evalexpr()| or |ch_sendexpr()| on a raw channel.
Bram Moolenaarcbebd482016-02-07 23:02:56 +0100393
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100394==============================================================================
Bram Moolenaar38a55632016-02-15 22:07:32 +01003957. More channel functions *channel-more*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100396
Bram Moolenaar38a55632016-02-15 22:07:32 +0100397To obtain the status of a channel: ch_status(channel). The possible results
398are:
399 "fail" Failed to open the channel.
400 "open" The channel can be used.
401 "closed" The channel was closed.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100402
403TODO:
Bram Moolenaar187db502016-02-27 14:44:26 +0100404To obtain the job associated with a channel: ch_getjob(channel)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100405
Bram Moolenaar38a55632016-02-15 22:07:32 +0100406To read one message from a channel: >
407 let output = ch_read(channel)
408This uses the channel timeout. To read without a timeout, just get any
409message that is available: >
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100410 let output = ch_read(channel, {'timeout': 0})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100411When no message was available then the result is v:none for a JSON or JS mode
412channels, an empty string for a RAW or NL channel.
413
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100414To read all output from a RAW channel that is available: >
415 let output = ch_readraw(channel)
Bram Moolenaar38a55632016-02-15 22:07:32 +0100416To read the error output: >
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100417 let output = ch_readraw(channel, {"part": "err"})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100418
419==============================================================================
4208. Starting a job with a channel *job-start* *job*
421
422To start a job and open a channel for stdin/stdout/stderr: >
423 let job = job_start(command, {options})
424
425You can get the channel with: >
426 let channel = job_getchannel(job)
427
428The channel will use NL mode. If you want another mode it's best to specify
429this in {options}. When changing the mode later some text may have already
430been received and not parsed correctly.
431
432If the command produces a line of output that you want to deal with, specify
433a handler for stdout: >
434 let job = job_start(command, {"out-cb": "MyHandler"})
435The function will be called with the channel and a message. You would define
436it like this: >
437 func MyHandler(channel, msg)
438
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100439Without the handler you need to read the output with |ch_read()| or
440|ch_readraw()|.
Bram Moolenaar38a55632016-02-15 22:07:32 +0100441
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100442The handler defined for "out-cb" will not receive stderr. If you want to
Bram Moolenaar38a55632016-02-15 22:07:32 +0100443handle that separately, add an "err-cb" handler: >
444 let job = job_start(command, {"out-cb": "MyHandler",
445 \ "err-cb": "ErrHandler"})
446
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100447If you want to handle both stderr and stdout with one handler use the
448"callback" option: >
449 let job = job_start(command, {"callback": "MyHandler"})
450
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100451You can send a message to the command with ch_evalraw(). If the channel is in
452JSON or JS mode you can use ch_evalexpr().
Bram Moolenaar38a55632016-02-15 22:07:32 +0100453
454There are several options you can use, see |job-options|.
Bram Moolenaar187db502016-02-27 14:44:26 +0100455For example, to start a job and write its output in buffer "dummy": >
456 let logjob = job_start("tail -f /tmp/log",
457 \ {'out-io': 'buffer', 'out-name': 'dummy'})
458 sbuf dummy
Bram Moolenaar38a55632016-02-15 22:07:32 +0100459
460TODO:
461To run a job and read its output once it is done: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100462 let job = job_start({command}, {'exit-cb': 'MyHandler'})
463 func MyHandler(job, status)
464 let channel = job_getchannel()
465 let output = ch_readall(channel)
466 " parse output
467 endfunc
468
469==============================================================================
4709. Starting a job without a channel *job-start-nochannel*
471
472To start another process without creating a channel: >
473 let job = job_start(command, {"in-io": "null", "out-io": "null"})
474
475This starts {command} in the background, Vim does not wait for it to finish.
476
477TODO:
478When Vim sees that neither stdin, stdout or stderr are connected, no channel
479will be created. Often you will want to include redirection in the command to
480avoid it getting stuck.
481
482There are several options you can use, see |job-options|.
483
484TODO: *job-may-start*
485To start a job only when connecting to an address does not work use
486job_maystart('command', {address}, {options}), For Example: >
487 let job = job_maystart(command, address, {"waittime": 1000})
488 let channel = job_gethandle(job)
489
490This comes down to: >
491 let channel = ch_open(address, {"waittime": 0})
492 if ch_status(channel) == "fail"
493 let job = job_start(command)
494 let channel = ch_open(address, {"waittime": 1000})
495 call job_sethandle(channel)
496 endif
497Note that the specified waittime applies to when the job has been started.
498This gives the job some time to make the port available.
499
500==============================================================================
50110. Job options *job-options*
502
503The {options} argument in job_start() is a dictionary. All entries are
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100504optional. Some options can be used after the job has started, using
505job_setoptions(job, {options}). Many options can be used with the channel
506related to the job, using ch_setoptions(channel, {options}).
507See |job_setoptions()| and |ch_setoptions()|.
Bram Moolenaar38a55632016-02-15 22:07:32 +0100508
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100509 *job-callback*
510"callback": handler Callback for something to read on any part of the
511 channel.
512 *job-out-cb*
Bram Moolenaar38a55632016-02-15 22:07:32 +0100513"out-cb": handler Callback for when there is something to read on
514 stdout.
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100515 *job-err-cb*
Bram Moolenaar38a55632016-02-15 22:07:32 +0100516"err-cb": handler Callback for when there is something to read on
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100517 stderr.
Bram Moolenaar187db502016-02-27 14:44:26 +0100518 *job-close-cb*
Bram Moolenaar38a55632016-02-15 22:07:32 +0100519"close-cb": handler Callback for when the channel is closed. Same as
520 "close-cb" on ch_open().
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100521 *job-exit-cb*
Bram Moolenaar38a55632016-02-15 22:07:32 +0100522"exit-cb": handler Callback for when the job ends. The arguments are the
523 job and the exit status.
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100524 Vim checks about every 10 seconds for jobs that ended.
525 The callback can also be triggered by calling
526 |job_status()|.
527 *job-stoponexit*
528"stoponexit": {signal} Send {signal} to the job when Vim exits. See
529 |job_stop()| for possible values.
530"stoponexit": "" Do not stop the job when Vim exits.
531 The default is "term".
532
Bram Moolenaar38a55632016-02-15 22:07:32 +0100533TODO: *job-term*
534"term": "open" Start a terminal and connect the job
535 stdin/stdout/stderr to it.
536
Bram Moolenaar187db502016-02-27 14:44:26 +0100537 *job-in-io*
538"in-io": "null" disconnect stdin TODO
Bram Moolenaar38a55632016-02-15 22:07:32 +0100539"in-io": "pipe" stdin is connected to the channel (default)
Bram Moolenaar187db502016-02-27 14:44:26 +0100540"in-io": "file" stdin reads from a file TODO
541"in-io": "buffer" stdin reads from a buffer TODO
542"in-name": "/path/file" the name of he file or buffer to read from
543"in-buf": number the number of the buffer to read from TODO
Bram Moolenaar38a55632016-02-15 22:07:32 +0100544
Bram Moolenaar187db502016-02-27 14:44:26 +0100545 *job-out-io*
546"out-io": "null" disconnect stdout TODO
Bram Moolenaar38a55632016-02-15 22:07:32 +0100547"out-io": "pipe" stdout is connected to the channel (default)
Bram Moolenaar187db502016-02-27 14:44:26 +0100548"out-io": "file" stdout writes to a file TODO
Bram Moolenaar38a55632016-02-15 22:07:32 +0100549"out-io": "buffer" stdout appends to a buffer
Bram Moolenaar187db502016-02-27 14:44:26 +0100550"out-name": "/path/file" the name of the file or buffer to write to
551"out-buf": number the number of the buffer to write to TODO
Bram Moolenaar38a55632016-02-15 22:07:32 +0100552
Bram Moolenaar187db502016-02-27 14:44:26 +0100553 *job-err-io*
554"err-io": "out" same as stdout TODO
555"err-io": "null" disconnect stderr TODO
556"err-io": "pipe" stderr is connected to the channel (default)
557"err-io": "file" stderr writes to a file TODO
558"err-io": "buffer" stderr appends to a buffer TODO
559"err-name": "/path/file" the name of the file or buffer to write to
560"err-buf": number the number of the buffer to write to TODO
561
562When the IO mode is "buffer" and there is a callback, the text is appended to
563the buffer before invoking the callback.
564 *E915*
565The name of the buffer is compared the full name of existing buffers. If
566there is a match that buffer is used. Otherwise a new buffer is created,
567where 'buftype' is set to "nofile" and 'bufhidden' to "hide". If you prefer
568other settings, create the buffer first and pass the buffer number.
569
570When the buffer written to is displayed in a window and the cursor is in the
571first column of the last line, the cursor will be moved to the newly added
572line and the window is scrolled up to show the cursor if needed.
573
574Undo is synced for every added line.
Bram Moolenaar38a55632016-02-15 22:07:32 +0100575
Bram Moolenaar38a55632016-02-15 22:07:32 +0100576==============================================================================
57711. Controlling a job *job-control*
578
579To get the status of a job: >
580 echo job_status(job)
581
582To make a job stop running: >
583 job_stop(job)
584
585This is the normal way to end a job. On Unix it sends a SIGTERM to the job.
586It is possible to use other ways to stop the job, or even send arbitrary
587signals. E.g. to force a job to stop, "kill it": >
588 job_stop(job, "kill")
589
590For more options see |job_stop()|.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100591
592
593 vim:tw=78:ts=8:ft=help:norl: