1- *channel.txt* For Vim version 7.4. Last change: 2016 Feb 20
1+ *channel.txt* For Vim version 7.4. Last change: 2016 Feb 21
22
33
44 VIM REFERENCE MANUAL by Bram Moolenaar
@@ -93,21 +93,23 @@ To handle asynchronous communication a callback needs to be used: >
9393 func MyHandler(channel, msg)
9494 echo "from the handler: " . a:msg
9595 endfunc
96- call ch_sendexpr(channel, 'hello!', "MyHandler")
96+ call ch_sendexpr(channel, 'hello!', {'callback': "MyHandler"} )
9797 Vim will not wait for a response. Now the server can send the response later
9898and MyHandler will be invoked.
9999
100100Instead of giving a callback with every send call, it can also be specified
101101when opening the channel: >
102102 call ch_close(channel)
103103 let channel = ch_open('localhost:8765', {'callback': "MyHandler"})
104- call ch_sendexpr(channel, 'hello!', 0 )
104+ call ch_sendexpr(channel, 'hello!', {'callback': 0} )
105105
106106==============================================================================
1071073. Opening a channel *channel-open*
108108
109109To open a channel: >
110110 let channel = ch_open({address} [, {options}])
111+ if ch_status(channel) == "open"
112+ " use the channel
111113
112114 Use | ch_status() | to see if the channel could be opened.
113115
@@ -131,25 +133,32 @@ Use |ch_status()| to see if the channel could be opened.
131133 *channel-callback*
132134"callback" A function that is called when a message is received that is
133135 not handled otherwise. It gets two arguments: the channel
134- handle and the received message. Example: >
136+ and the received message. Example: >
135137 func Handle(channel, msg)
136138 echo 'Received: ' . a:msg
137139 endfunc
138140 let channel = ch_open("localhost:8765", {"callback": "Handle"})
139141<
142+ 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*
140149"out-cb" A function like "callback" but used for stdout. Only for when
141150 the channel uses pipes. When "out-cb" wasn't set the channel
142151 callback is used.
143-
152+ *err-cb*
144153"err-cb" A function like "callback" but used for stderr. Only for when
145154 the channel uses pipes. When "err-cb" wasn't set the channel
146155 callback is used.
147156
148- TODO:
157+ TODO: *close-cb*
149158"close-cb" A function that is called when the channel gets closed, other
150159 than by calling ch_close(). It should be defined like this: >
151160 func MyCloseHandler(channel)
152-
161+ < *waittime*
153162"waittime" The time to wait for the connection to be made in
154163 milliseconds. The default is zero, don't wait, which is
155164 useful if the server is supposed to be running already. A
@@ -158,41 +167,34 @@ Use |ch_status()| to see if the channel could be opened.
158167"timeout" The time to wait for a request when blocking, E.g. when using
159168 ch_sendexpr(). In milliseconds. The default is 2000 (2
160169 seconds).
161-
170+ *out-timeout* *err-timeout*
162171"out-timeout" Timeout for stdout. Only when using pipes.
163172"err-timeout" Timeout for stderr. Only when using pipes.
164173 Note: when setting "timeout" the part specific mode is
165174 overwritten. Therefore set "timeout" first and the part
166175 specific mode later.
167176
168- When "mode" is "json" or "js" the "msg" argument is the body of the received
169- message, converted to Vim types.
170- When "mode" is "raw" the "msg" argument is the whole message as a string.
171-
172177When "mode" is "json" or "js" the "callback" is optional. When omitted it is
173178only possible to receive a message after sending one.
174179
175- To change the channel options after opening it use ch_setoptions(). The
176- arguments are similar to what is passed to ch_open(), but "waittime" cannot be
177- given, since that only applies to opening the channel.
180+ To change the channel options after opening it use | ch_setoptions() | . The
181+ arguments are similar to what is passed to | ch_open() | , but "waittime" cannot
182+ be given, since that only applies to opening the channel.
178183
179- The handler can be added or changed: >
184+ For example, the handler can be added or changed: >
180185 call ch_setoptions(channel, {'callback': callback})
181186 When "callback" is empty (zero or an empty string) the handler is removed.
182187
183188The timeout can be changed: >
184189 call ch_setoptions(channel, {'timeout': msec})
185190<
186- *E906*
191+ *channel-close* * E906*
187192Once done with the channel, disconnect it like this: >
188193 call ch_close(channel)
189194 When a socket is used this will close the socket for both directions. When
190195pipes are used (stdin/stdout/stderr) they are all closed. This might not be
191196what you want! Stopping the job with job_stop() might be better.
192197
193- TODO:
194- Currently up to 10 channels can be in use at the same time. *E897*
195-
196198When the channel can't be opened you will get an error message. There is a
197199difference between MS-Windows and Unix: On Unix when the port doesn't exist
198200ch_open() fails quickly. On MS-Windows "waittime" applies.
@@ -211,12 +213,13 @@ This awaits a response from the other side.
211213When mode is JS this works the same, except that the messages use
212214JavaScript encoding. See | js_encode() | for the difference.
213215
214- To send a message, without handling a response: >
215- call ch_sendexpr(channel, {expr}, 0)
216+ To send a message, without handling a response or letting the channel callback
217+ handle the response: >
218+ call ch_sendexpr(channel, {expr}, {'callback': 0})
216219
217220 To send a message and letting the response handled by a specific function,
218221asynchronously: >
219- call ch_sendexpr(channel, {expr}, {callback})
222+ call ch_sendexpr(channel, {expr}, {' callback': Handler })
220223
221224 Vim will match the response with the request using the message ID. Once the
222225response is received the callback will be invoked. Further responses with the
@@ -424,13 +427,18 @@ The function will be called with the channel and a message. You would define
424427it like this: >
425428 func MyHandler(channel, msg)
426429
427- Without the handler you need to read the output with ch_read().
430+ Without the handler you need to read the output with | ch_read() | or
431+ | ch_readraw() | .
428432
429- The handler defined for "out-cb" will also receive stderr. If you want to
433+ The handler defined for "out-cb" will not receive stderr. If you want to
430434handle that separately, add an "err-cb" handler: >
431435 let job = job_start(command, {"out-cb": "MyHandler",
432436 \ "err-cb": "ErrHandler"})
433437
438+ If 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+
434442 You can send a message to the command with ch_sendraw(). If the channel is in
435443JSON or JS mode you can use ch_sendexpr().
436444
@@ -481,7 +489,10 @@ This gives the job some time to make the port available.
48148910. Job options *job-options*
482490
483491The {options} argument in job_start() is a dictionary. All entries are
484- optional. The same options can be used with job_setoptions(job, {options} ).
492+ optional. Some options can be used after the job has started, using
493+ job_setoptions(job, {options} ). Many options can be used with the channel
494+ related to the job, using ch_setoptions(channel, {options} ).
495+ See | job_setoptions() | and | ch_setoptions() | .
485496
486497 *job-callback*
487498"callback": handler Callback for something to read on any part of the
@@ -495,13 +506,18 @@ optional. The same options can be used with job_setoptions(job, {options}).
495506TODO: *job-close-cb*
496507"close-cb": handler Callback for when the channel is closed. Same as
497508 "close-cb" on ch_open().
498- TODO: *job-exit-cb*
509+ *job-exit-cb*
499510"exit-cb": handler Callback for when the job ends. The arguments are the
500511 job and the exit status.
501- TODO: *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.
512+ 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+
505521TODO: *job-term*
506522"term": "open" Start a terminal and connect the job
507523 stdin/stdout/stderr to it.
@@ -529,9 +545,6 @@ TODO: *job-err-io*
529545"err-io": "buffer" stderr appends to a buffer
530546"err-buffer": "name" buffer to append to
531547
532- TODO: more options
533-
534-
535548==============================================================================
53654911. Controlling a job *job-control*
537550
0 commit comments