blob: 5cc214caf1f88a7a9ed4e50833ea32b006425f98 [file] [log] [blame]
Bram Moolenaar818078d2016-08-27 21:58:42 +02001*channel.txt* For Vim version 7.4. Last change: 2016 Aug 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
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01009Vim uses channels to communicate with other processes.
Bram Moolenaar269f5952016-07-15 22:54:41 +020010A channel uses a socket or pipes. *socket-interface*
Bram Moolenaar38a55632016-02-15 22:07:32 +010011Jobs can be used to start processes and communicate with them.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010012The Netbeans interface also uses a channel. |netbeans|
13
Bram Moolenaar38a55632016-02-15 22:07:32 +0100141. Overview |job-channel-overview|
152. Channel demo |channel-demo|
163. Opening a channel |channel-open|
174. Using a JSON or JS channel |channel-use|
185. Channel commands |channel-commands|
196. Using a RAW or NL channel |channel-raw|
207. More channel functions |channel-more|
218. Starting a job with a channel |job-start|
229. Starting a job without a channel |job-start-nochannel|
2310. Job options |job-options|
2411. Controlling a job |job-control|
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010025
26{Vi does not have any of these features}
Bram Moolenaar38a55632016-02-15 22:07:32 +010027{only when compiled with the |+channel| feature for channel stuff}
28{only when compiled with the |+job| feature for job stuff}
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010029
30==============================================================================
Bram Moolenaar38a55632016-02-15 22:07:32 +0100311. Overview *job-channel-overview*
32
33There are four main types of jobs:
341. A deamon, serving several Vim instances.
35 Vim connects to it with a socket.
362. One job working with one Vim instance, asynchronously.
37 Uses a socket or pipes.
383. A job performing some work for a short time, asynchronously.
39 Uses a socket or pipes.
404. Running a filter, synchronously.
41 Uses pipes.
42
Bram Moolenaar77cdfd12016-03-12 12:57:59 +010043For when using sockets See |job-start|, |job-start-nochannel| and
44|channel-open|. For 2 and 3, one or more jobs using pipes, see |job-start|.
Bram Moolenaar38a55632016-02-15 22:07:32 +010045For 4 use the ":{range}!cmd" command, see |filter|.
46
47Over the socket and pipes these protocols are available:
48RAW nothing known, Vim cannot tell where a message ends
49NL every message ends in a NL (newline) character
50JSON JSON encoding |json_encode()|
51JS JavaScript style JSON-like encoding |js_encode()|
52
53Common combination are:
54- Using a job connected through pipes in NL mode. E.g., to run a style
55 checker and receive errors and warnings.
56- Using a deamon, connecting over a socket in JSON mode. E.g. to lookup
Bram Moolenaar09521312016-08-12 22:54:35 +020057 cross-references in a database.
Bram Moolenaar38a55632016-02-15 22:07:32 +010058
59==============================================================================
Bram Moolenaar26852122016-05-24 20:02:38 +0200602. Channel demo *channel-demo* *demoserver.py*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010061
62This requires Python. The demo program can be found in
63$VIMRUNTIME/tools/demoserver.py
64Run it in one terminal. We will call this T1.
65
66Run Vim in another terminal. Connect to the demo server with: >
Bram Moolenaar38a55632016-02-15 22:07:32 +010067 let channel = ch_open('localhost:8765')
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010068
69In T1 you should see:
70 === socket opened === ~
71
72You can now send a message to the server: >
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010073 echo ch_evalexpr(channel, 'hello!')
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010074
75The message is received in T1 and a response is sent back to Vim.
76You can see the raw messages in T1. What Vim sends is:
77 [1,"hello!"] ~
78And the response is:
79 [1,"got it"] ~
80The number will increase every time you send a message.
81
82The server can send a command to Vim. Type this on T1 (literally, including
Bram Moolenaarfb1f6262016-01-31 20:24:32 +010083the quotes):
84 ["ex","echo 'hi there'"] ~
85And you should see the message in Vim. You can move the cursor a word forward:
86 ["normal","w"] ~
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010087
88To handle asynchronous communication a callback needs to be used: >
Bram Moolenaar38a55632016-02-15 22:07:32 +010089 func MyHandler(channel, msg)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010090 echo "from the handler: " . a:msg
91 endfunc
Bram Moolenaar02e83b42016-02-21 20:10:26 +010092 call ch_sendexpr(channel, 'hello!', {'callback': "MyHandler"})
Bram Moolenaar38a55632016-02-15 22:07:32 +010093Vim will not wait for a response. Now the server can send the response later
94and MyHandler will be invoked.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010095
96Instead of giving a callback with every send call, it can also be specified
97when opening the channel: >
Bram Moolenaar38a55632016-02-15 22:07:32 +010098 call ch_close(channel)
99 let channel = ch_open('localhost:8765', {'callback': "MyHandler"})
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100100 call ch_sendexpr(channel, 'hello!')
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100101
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100102When trying out channels it's useful to see what is going on. You can tell
103Vim to write lines in log file: >
104 call ch_logfile('channellog', 'w')
105See |ch_logfile()|.
106
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100107==============================================================================
Bram Moolenaar38a55632016-02-15 22:07:32 +01001083. Opening a channel *channel-open*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100109
Bram Moolenaar681baaf2016-02-04 20:57:07 +0100110To open a channel: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100111 let channel = ch_open({address} [, {options}])
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100112 if ch_status(channel) == "open"
113 " use the channel
Bram Moolenaar38a55632016-02-15 22:07:32 +0100114
115Use |ch_status()| to see if the channel could be opened.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100116
117{address} has the form "hostname:port". E.g., "localhost:8765".
118
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100119{options} is a dictionary with optional entries: *channel-open-options*
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100120
121"mode" can be: *channel-mode*
122 "json" - Use JSON, see below; most convenient way. Default.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100123 "js" - Use JS (JavaScript) encoding, more efficient than JSON.
Bram Moolenaar38a55632016-02-15 22:07:32 +0100124 "nl" - Use messages that end in a NL character
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100125 "raw" - Use raw messages
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100126 *channel-callback* *E921*
Bram Moolenaar38a55632016-02-15 22:07:32 +0100127"callback" A function that is called when a message is received that is
128 not handled otherwise. It gets two arguments: the channel
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100129 and the received message. Example: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100130 func Handle(channel, msg)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100131 echo 'Received: ' . a:msg
132 endfunc
Bram Moolenaar38a55632016-02-15 22:07:32 +0100133 let channel = ch_open("localhost:8765", {"callback": "Handle"})
134<
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100135 When "mode" is "json" or "js" the "msg" argument is the body
136 of the received message, converted to Vim types.
137 When "mode" is "nl" the "msg" argument is one message,
138 excluding the NL.
139 When "mode" is "raw" the "msg" argument is the whole message
140 as a string.
Bram Moolenaare18c0b32016-03-20 21:08:34 +0100141
142 For all callbacks: Use |function()| to bind it to arguments
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100143 and/or a Dictionary. Or use the form "dict.function" to bind
144 the Dictionary.
Bram Moolenaar06d2d382016-05-20 17:24:11 +0200145
146 Callbacks are only called at a "safe" moment, usually when Vim
147 is waiting for the user to type a character. Vim does not use
148 multi-threading.
149
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100150 *close_cb*
151"close_cb" A function that is called when the channel gets closed, other
Bram Moolenaar38a55632016-02-15 22:07:32 +0100152 than by calling ch_close(). It should be defined like this: >
153 func MyCloseHandler(channel)
Bram Moolenaar06d2d382016-05-20 17:24:11 +0200154< Vim will invoke callbacks that handle data before invoking
155 close_cb, thus when this function is called no more data will
156 be received.
157 *waittime*
Bram Moolenaar38a55632016-02-15 22:07:32 +0100158"waittime" The time to wait for the connection to be made in
Bram Moolenaarf3913272016-02-25 00:00:01 +0100159 milliseconds. A negative number waits forever.
160
161 The default is zero, don't wait, which is useful if a local
162 server is supposed to be running already. On Unix Vim
163 actually uses a 1 msec timeout, that is required on many
164 systems. Use a larger value for a remote server, e.g. 10
165 msec at least.
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100166 *channel-timeout*
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100167"timeout" The time to wait for a request when blocking, E.g. when using
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100168 ch_evalexpr(). In milliseconds. The default is 2000 (2
Bram Moolenaar38a55632016-02-15 22:07:32 +0100169 seconds).
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100170
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100171When "mode" is "json" or "js" the "callback" is optional. When omitted it is
172only possible to receive a message after sending one.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100173
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100174To change the channel options after opening it use |ch_setoptions()|. The
175arguments are similar to what is passed to |ch_open()|, but "waittime" cannot
176be given, since that only applies to opening the channel.
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100177
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100178For example, the handler can be added or changed: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100179 call ch_setoptions(channel, {'callback': callback})
180When "callback" is empty (zero or an empty string) the handler is removed.
181
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100182After a callback has been invoked Vim will update the screen and put the
183cursor back where it belongs. Thus the callback should not need to do
184`:redraw`.
185
Bram Moolenaar38a55632016-02-15 22:07:32 +0100186The timeout can be changed: >
187 call ch_setoptions(channel, {'timeout': msec})
188<
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100189 *channel-close* *E906*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100190Once done with the channel, disconnect it like this: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100191 call ch_close(channel)
192When a socket is used this will close the socket for both directions. When
193pipes are used (stdin/stdout/stderr) they are all closed. This might not be
194what you want! Stopping the job with job_stop() might be better.
Bram Moolenaar187db502016-02-27 14:44:26 +0100195All readahead is discarded, callbacks will no longer be invoked.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100196
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100197Note that a channel is closed in three stages:
198 - The I/O ends, log message: "Closing channel". There can still be queued
199 messages to read or callbacks to invoke.
200 - The readahead is cleared, log message: "Clearing channel". Some variables
201 may still reference the channel.
202 - The channel is freed, log message: "Freeing channel".
203
Bram Moolenaarcbebd482016-02-07 23:02:56 +0100204When the channel can't be opened you will get an error message. There is a
205difference between MS-Windows and Unix: On Unix when the port doesn't exist
206ch_open() fails quickly. On MS-Windows "waittime" applies.
Bram Moolenaaraa3b15d2016-04-21 08:53:19 +0200207*E898* *E901* *E902*
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100208
209If there is an error reading or writing a channel it will be closed.
Bram Moolenaaraa3b15d2016-04-21 08:53:19 +0200210*E630* *E631*
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100211
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100212==============================================================================
Bram Moolenaar38a55632016-02-15 22:07:32 +01002134. Using a JSON or JS channel *channel-use*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100214
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100215If mode is JSON then a message can be sent synchronously like this: >
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100216 let response = ch_evalexpr(channel, {expr})
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100217This awaits a response from the other side.
218
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100219When mode is JS this works the same, except that the messages use
Bram Moolenaar38a55632016-02-15 22:07:32 +0100220JavaScript encoding. See |js_encode()| for the difference.
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100221
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100222To send a message, without handling a response or letting the channel callback
223handle the response: >
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100224 call ch_sendexpr(channel, {expr})
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100225
226To send a message and letting the response handled by a specific function,
227asynchronously: >
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100228 call ch_sendexpr(channel, {expr}, {'callback': Handler})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100229
230Vim will match the response with the request using the message ID. Once the
231response is received the callback will be invoked. Further responses with the
232same ID will be ignored. If your server sends back multiple responses you
233need to send them with ID zero, they will be passed to the channel callback.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100234
235The {expr} is converted to JSON and wrapped in an array. An example of the
236message that the receiver will get when {expr} is the string "hello":
237 [12,"hello"] ~
238
239The format of the JSON sent is:
240 [{number},{expr}]
241
242In which {number} is different every time. It must be used in the response
243(if any):
244
245 [{number},{response}]
246
247This way Vim knows which sent message matches with which received message and
248can call the right handler. Also when the messages arrive out of order.
249
Bram Moolenaarf1f07922016-08-26 17:58:53 +0200250A newline character is terminating the JSON text. This can be used to
251separate the read text. For example, in Python:
252 splitidx = read_text.find('\n')
253 message = read_text[:splitidx]
254 rest = read_text[splitidx + 1:]
255
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100256The sender must always send valid JSON to Vim. Vim can check for the end of
257the message by parsing the JSON. It will only accept the message if the end
Bram Moolenaarf1f07922016-08-26 17:58:53 +0200258was received. A newline after the message is optional.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100259
260When the process wants to send a message to Vim without first receiving a
261message, it must use the number zero:
262 [0,{response}]
263
264Then channel handler will then get {response} converted to Vim types. If the
265channel does not have a handler the message is dropped.
266
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100267It is also possible to use ch_sendraw() and ch_evalraw() on a JSON or JS
268channel. The caller is then completely responsible for correct encoding and
269decoding.
Bram Moolenaarcbebd482016-02-07 23:02:56 +0100270
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100271==============================================================================
Bram Moolenaar38a55632016-02-15 22:07:32 +01002725. Channel commands *channel-commands*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100273
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100274With a JSON channel the process can send commands to Vim that will be
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100275handled by Vim internally, it does not require a handler for the channel.
276
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100277Possible commands are: *E903* *E904* *E905*
278 ["redraw" {forced}]
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100279 ["ex", {Ex command}]
280 ["normal", {Normal mode command}]
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100281 ["expr", {expression}, {number}]
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100282 ["expr", {expression}]
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100283 ["call", {func name}, {argument list}, {number}]
284 ["call", {func name}, {argument list}]
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100285
286With all of these: Be careful what these commands do! You can easily
287interfere with what the user is doing. To avoid trouble use |mode()| to check
288that the editor is in the expected state. E.g., to send keys that must be
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100289inserted as text, not executed as a command:
290 ["ex","if mode() == 'i' | call feedkeys('ClassName') | endif"] ~
291
292Errors in these commands are normally not reported to avoid them messing up
293the display. If you do want to see them, set the 'verbose' option to 3 or
294higher.
295
296
297Command "redraw" ~
298
299The other commands do not update the screen, so that you can send a sequence
300of commands without the cursor moving around. You must end with the "redraw"
301command to show any changed text and show the cursor where it belongs.
302
303The argument is normally an empty string:
304 ["redraw", ""] ~
305To first clear the screen pass "force":
306 ["redraw", "force"] ~
307
308
309Command "ex" ~
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100310
311The "ex" command is executed as any Ex command. There is no response for
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100312completion or error. You could use functions in an |autoload| script:
313 ["ex","call myscript#MyFunc(arg)"]
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100314
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100315You can also use "call |feedkeys()|" to insert any key sequence.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100316
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100317When there is an error a message is written to the channel log, if it exists,
318and v:errmsg is set to the error.
319
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100320
321Command "normal" ~
322
Bram Moolenaar681baaf2016-02-04 20:57:07 +0100323The "normal" command is executed like with ":normal!", commands are not
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100324mapped. Example to open the folds under the cursor:
325 ["normal" "zO"]
326
327
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100328Command "expr" with response ~
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100329
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100330The "expr" command can be used to get the result of an expression. For
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100331example, to get the number of lines in the current buffer:
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100332 ["expr","line('$')", -2] ~
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100333
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100334It will send back the result of the expression:
Bram Moolenaare0fa3742016-02-20 15:47:01 +0100335 [-2, "last line"] ~
336The format is:
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100337 [{number}, {result}]
Bram Moolenaar187db502016-02-27 14:44:26 +0100338
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100339Here {number} is the same as what was in the request. Use a negative number
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100340to avoid confusion with message that Vim sends. Use a different number on
341every request to be able to match the request with the response.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100342
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100343{result} is the result of the evaluation and is JSON encoded. If the
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100344evaluation fails or the result can't be encoded in JSON it is the string
345"ERROR".
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100346
347
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100348Command "expr" without a response ~
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100349
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100350This command is similar to "expr" above, but does not send back any response.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100351Example:
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100352 ["expr","setline('$', ['one', 'two', 'three'])"] ~
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100353There is no third argument in the request.
354
355
356Command "call" ~
357
358This is similar to "expr", but instead of passing the whole expression as a
359string this passes the name of a function and a list of arguments. This
360avoids the conversion of the arguments to a string and escaping and
361concatenating them. Example:
362 ["call", "line", ["$"], -2] ~
363
364Leave out the fourth argument if no response is to be sent:
365 ["call", "setline", ["$", ["one", "two", "three"]]] ~
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100366
367==============================================================================
Bram Moolenaar38a55632016-02-15 22:07:32 +01003686. Using a RAW or NL channel *channel-raw*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100369
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100370If mode is RAW or NL then a message can be send like this: >
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100371 let response = ch_evalraw(channel, {string})
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100372
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100373The {string} is sent as-is. The response will be what can be read from the
374channel right away. Since Vim doesn't know how to recognize the end of the
Bram Moolenaar38a55632016-02-15 22:07:32 +0100375message you need to take care of it yourself. The timeout applies for reading
376the first byte, after that it will not wait for anything more.
377
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100378If mode is "nl" you can send a message in a similar way. You are expected
Bram Moolenaar38a55632016-02-15 22:07:32 +0100379to put in the NL after each message. Thus you can also send several messages
380ending in a NL at once. The response will be the text up to and including the
381first NL. This can also be just the NL for an empty response.
382If no NL was read before the channel timeout an empty string is returned.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100383
384To send a message, without expecting a response: >
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100385 call ch_sendraw(channel, {string})
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100386The process can send back a response, the channel handler will be called with
387it.
388
389To send a message and letting the response handled by a specific function,
390asynchronously: >
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100391 call ch_sendraw(channel, {string}, {'callback': 'MyHandler'})
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100392
Bram Moolenaar38a55632016-02-15 22:07:32 +0100393This {string} can also be JSON, use |json_encode()| to create it and
394|json_decode()| to handle a received JSON message.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100395
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100396It is not possible to use |ch_evalexpr()| or |ch_sendexpr()| on a raw channel.
Bram Moolenaarcbebd482016-02-07 23:02:56 +0100397
Bram Moolenaar818078d2016-08-27 21:58:42 +0200398A String in Vim cannot contain NUL bytes. To send or receive NUL bytes read
399or write from a buffer. See |in_io-buffer| and |out_io-buffer|.
400
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100401==============================================================================
Bram Moolenaar38a55632016-02-15 22:07:32 +01004027. More channel functions *channel-more*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100403
Bram Moolenaar38a55632016-02-15 22:07:32 +0100404To obtain the status of a channel: ch_status(channel). The possible results
405are:
406 "fail" Failed to open the channel.
407 "open" The channel can be used.
Bram Moolenaar06481422016-04-30 15:13:38 +0200408 "buffered" The channel was closed but there is data to read.
Bram Moolenaar38a55632016-02-15 22:07:32 +0100409 "closed" The channel was closed.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100410
Bram Moolenaar187db502016-02-27 14:44:26 +0100411To obtain the job associated with a channel: ch_getjob(channel)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100412
Bram Moolenaar38a55632016-02-15 22:07:32 +0100413To read one message from a channel: >
414 let output = ch_read(channel)
415This uses the channel timeout. To read without a timeout, just get any
416message that is available: >
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100417 let output = ch_read(channel, {'timeout': 0})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100418When no message was available then the result is v:none for a JSON or JS mode
419channels, an empty string for a RAW or NL channel.
420
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100421To read all output from a RAW channel that is available: >
422 let output = ch_readraw(channel)
Bram Moolenaar38a55632016-02-15 22:07:32 +0100423To read the error output: >
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100424 let output = ch_readraw(channel, {"part": "err"})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100425
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100426ch_read() and ch_readraw() use the channel timeout. When there is nothing to
427read within that time an empty string is returned. To specify a different
428timeout in msec use the "timeout" option:
429 {"timeout": 123} ~
430To read from the error output use the "part" option:
431 {"part": "err"} ~
432To read a message with a specific ID, on a JS or JSON channel:
433 {"id": 99} ~
434When no ID is specified or the ID is -1, the first message is returned. This
435overrules any callback waiting for this message.
436
437For a RAW channel this returns whatever is available, since Vim does not know
438where a message ends.
439For a NL channel this returns one message.
440For a JS or JSON channel this returns one decoded message.
441This includes any sequence number.
442
Bram Moolenaar38a55632016-02-15 22:07:32 +0100443==============================================================================
4448. Starting a job with a channel *job-start* *job*
445
446To start a job and open a channel for stdin/stdout/stderr: >
447 let job = job_start(command, {options})
448
449You can get the channel with: >
450 let channel = job_getchannel(job)
451
452The channel will use NL mode. If you want another mode it's best to specify
453this in {options}. When changing the mode later some text may have already
454been received and not parsed correctly.
455
456If the command produces a line of output that you want to deal with, specify
457a handler for stdout: >
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100458 let job = job_start(command, {"out_cb": "MyHandler"})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100459The function will be called with the channel and a message. You would define
460it like this: >
461 func MyHandler(channel, msg)
462
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100463Without the handler you need to read the output with |ch_read()| or
Bram Moolenaar06481422016-04-30 15:13:38 +0200464|ch_readraw()|. You can do this in the close callback, see |read-in-close-cb|.
Bram Moolenaar38a55632016-02-15 22:07:32 +0100465
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100466The handler defined for "out_cb" will not receive stderr. If you want to
467handle that separately, add an "err_cb" handler: >
468 let job = job_start(command, {"out_cb": "MyHandler",
469 \ "err_cb": "ErrHandler"})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100470
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100471If you want to handle both stderr and stdout with one handler use the
472"callback" option: >
473 let job = job_start(command, {"callback": "MyHandler"})
474
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100475You can send a message to the command with ch_evalraw(). If the channel is in
476JSON or JS mode you can use ch_evalexpr().
Bram Moolenaar38a55632016-02-15 22:07:32 +0100477
478There are several options you can use, see |job-options|.
Bram Moolenaar187db502016-02-27 14:44:26 +0100479For example, to start a job and write its output in buffer "dummy": >
480 let logjob = job_start("tail -f /tmp/log",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100481 \ {'out_io': 'buffer', 'out_name': 'dummy'})
Bram Moolenaar187db502016-02-27 14:44:26 +0100482 sbuf dummy
Bram Moolenaar38a55632016-02-15 22:07:32 +0100483
Bram Moolenaar5f148ec2016-03-07 22:59:26 +0100484
485Job input from a buffer ~
Bram Moolenaar818078d2016-08-27 21:58:42 +0200486 *in_io-buffer*
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100487To run a job that reads from a buffer: >
488 let job = job_start({command},
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100489 \ {'in_io': 'buffer', 'in_name': 'mybuffer'})
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100490<
491 *E915* *E918*
492The buffer is found by name, similar to |bufnr()|. The buffer must exist and
493be loaded when job_start() is called.
494
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100495By default this reads the whole buffer. This can be changed with the "in_top"
496and "in_bot" options.
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100497
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100498A special mode is when "in_top" is set to zero and "in_bot" is not set: Every
Bram Moolenaar5f148ec2016-03-07 22:59:26 +0100499time a line is added to the buffer, the last-but-one line will be send to the
500job stdin. This allows for editing the last line and sending it when pressing
501Enter.
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100502
Bram Moolenaar063b9d12016-07-09 20:21:48 +0200503NUL bytes in the text will be passed to the job (internally Vim stores these
504as NL bytes).
505
Bram Moolenaar06481422016-04-30 15:13:38 +0200506
507Reading job output in the close callback ~
508 *read-in-close-cb*
509If the job can take some time and you don't need intermediate results, you can
510add a close callback and read the output there: >
511
512 func! CloseHandler(channel)
513 while ch_status(a:channel) == 'buffered'
514 echomsg ch_read(a:channel)
515 endwhile
516 endfunc
517 let job = job_start(command, {'close_cb': 'CloseHandler'})
518
519You will want to do something more useful than "echomsg".
520
Bram Moolenaar38a55632016-02-15 22:07:32 +0100521==============================================================================
5229. Starting a job without a channel *job-start-nochannel*
523
524To start another process without creating a channel: >
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100525 let job = job_start(command,
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100526 \ {"in_io": "null", "out_io": "null", "err_io": "null"})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100527
528This starts {command} in the background, Vim does not wait for it to finish.
529
Bram Moolenaar38a55632016-02-15 22:07:32 +0100530When Vim sees that neither stdin, stdout or stderr are connected, no channel
531will be created. Often you will want to include redirection in the command to
532avoid it getting stuck.
533
534There are several options you can use, see |job-options|.
535
Bram Moolenaar77cdfd12016-03-12 12:57:59 +0100536 *job-start-if-needed*
537To start a job only when connecting to an address does not work, do something
538like this: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100539 let channel = ch_open(address, {"waittime": 0})
540 if ch_status(channel) == "fail"
541 let job = job_start(command)
542 let channel = ch_open(address, {"waittime": 1000})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100543 endif
Bram Moolenaar77cdfd12016-03-12 12:57:59 +0100544
545Note that the waittime for ch_open() gives the job one second to make the port
546available.
Bram Moolenaar38a55632016-02-15 22:07:32 +0100547
548==============================================================================
54910. Job options *job-options*
550
551The {options} argument in job_start() is a dictionary. All entries are
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100552optional. Some options can be used after the job has started, using
553job_setoptions(job, {options}). Many options can be used with the channel
554related to the job, using ch_setoptions(channel, {options}).
555See |job_setoptions()| and |ch_setoptions()|.
Bram Moolenaar38a55632016-02-15 22:07:32 +0100556
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100557 *in_mode* *out_mode* *err_mode*
558"in_mode" mode specifically for stdin, only when using pipes
559"out_mode" mode specifically for stdout, only when using pipes
560"err_mode" mode specifically for stderr, only when using pipes
561 See |channel-mode| for the values.
562
563 Note: when setting "mode" the part specific mode is
564 overwritten. Therefore set "mode" first and the part
565 specific mode later.
566
567 Note: when writing to a file or buffer and when
568 reading from a buffer NL mode is used by default.
569
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100570 *job-callback*
571"callback": handler Callback for something to read on any part of the
572 channel.
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100573 *job-out_cb* *out_cb*
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100574"out_cb": handler Callback for when there is something to read on
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100575 stdout. Only for when the channel uses pipes. When
576 "out_cb" wasn't set the channel callback is used.
Bram Moolenaar269f5952016-07-15 22:54:41 +0200577 The two arguments are the channel and the message.
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100578
579 *job-err_cb* *err_cb*
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100580"err_cb": handler Callback for when there is something to read on
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100581 stderr. Only for when the channel uses pipes. When
582 "err_cb" wasn't set the channel callback is used.
Bram Moolenaar269f5952016-07-15 22:54:41 +0200583 The two arguments are the channel and the message.
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100584 *job-close_cb*
585"close_cb": handler Callback for when the channel is closed. Same as
Bram Moolenaar82af8712016-06-04 20:20:29 +0200586 "close_cb" on |ch_open()|, see |close_cb|.
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100587 *job-exit_cb*
588"exit_cb": handler Callback for when the job ends. The arguments are the
Bram Moolenaar38a55632016-02-15 22:07:32 +0100589 job and the exit status.
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100590 Vim checks about every 10 seconds for jobs that ended.
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200591 The check also be triggered by calling |job_status()|,
592 which may then invoke the exit_cb handler.
Bram Moolenaar06d2d382016-05-20 17:24:11 +0200593 Note that data can be buffered, callbacks may still be
594 called after the process ends.
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100595 *job-timeout*
596"timeout" The time to wait for a request when blocking, E.g.
597 when using ch_evalexpr(). In milliseconds. The
598 default is 2000 (2 seconds).
599 *out_timeout* *err_timeout*
600"out_timeout" Timeout for stdout. Only when using pipes.
601"err_timeout" Timeout for stderr. Only when using pipes.
602 Note: when setting "timeout" the part specific mode is
603 overwritten. Therefore set "timeout" first and the
604 part specific mode later.
605
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100606 *job-stoponexit*
607"stoponexit": {signal} Send {signal} to the job when Vim exits. See
608 |job_stop()| for possible values.
609"stoponexit": "" Do not stop the job when Vim exits.
610 The default is "term".
611
Bram Moolenaar77cdfd12016-03-12 12:57:59 +0100612 *job-term*
Bram Moolenaar38a55632016-02-15 22:07:32 +0100613"term": "open" Start a terminal and connect the job
614 stdin/stdout/stderr to it.
Bram Moolenaar77cdfd12016-03-12 12:57:59 +0100615 NOTE: Not implemented yet!
Bram Moolenaar38a55632016-02-15 22:07:32 +0100616
Bram Moolenaar77cdfd12016-03-12 12:57:59 +0100617"channel": {channel} Use an existing channel instead of creating a new one.
618 The parts of the channel that get used for the new job
619 will be disconnected from what they were used before.
620 If the channel was still use by another job this may
621 cause I/O errors.
622 Existing callbacks and other settings remain.
623
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100624 *job-in_io* *in_top* *in_bot* *in_name* *in_buf*
625"in_io": "null" disconnect stdin (read from /dev/null)
626"in_io": "pipe" stdin is connected to the channel (default)
627"in_io": "file" stdin reads from a file
628"in_io": "buffer" stdin reads from a buffer
629"in_top": number when using "buffer": first line to send (default: 1)
630"in_bot": number when using "buffer": last line to send (default: last)
631"in_name": "/path/file" the name of the file or buffer to read from
632"in_buf": number the number of the buffer to read from
Bram Moolenaar38a55632016-02-15 22:07:32 +0100633
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100634 *job-out_io* *out_name* *out_buf*
635"out_io": "null" disconnect stdout (goes to /dev/null)
636"out_io": "pipe" stdout is connected to the channel (default)
637"out_io": "file" stdout writes to a file
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200638"out_io": "buffer" stdout appends to a buffer (see below)
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100639"out_name": "/path/file" the name of the file or buffer to write to
640"out_buf": number the number of the buffer to write to
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200641"out_modifiable": 0 when writing to a buffer, 'modifiable' will be off
642 (see below)
Bram Moolenaar38a55632016-02-15 22:07:32 +0100643
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100644 *job-err_io* *err_name* *err_buf*
645"err_io": "out" stderr messages to go to stdout
646"err_io": "null" disconnect stderr (goes to /dev/null)
647"err_io": "pipe" stderr is connected to the channel (default)
648"err_io": "file" stderr writes to a file
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200649"err_io": "buffer" stderr appends to a buffer (see below)
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100650"err_name": "/path/file" the name of the file or buffer to write to
651"err_buf": number the number of the buffer to write to
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200652"err_modifiable": 0 when writing to a buffer, 'modifiable' will be off
653 (see below)
Bram Moolenaar77cdfd12016-03-12 12:57:59 +0100654
Bram Moolenaar7db8f6f2016-03-29 23:12:46 +0200655"block_write": number only for testing: pretend every other write to stdin
656 will block
657
Bram Moolenaar77cdfd12016-03-12 12:57:59 +0100658
659Writing to a buffer ~
Bram Moolenaar818078d2016-08-27 21:58:42 +0200660 *out_io-buffer*
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100661When the out_io or err_io mode is "buffer" and there is a callback, the text
Bram Moolenaar5f148ec2016-03-07 22:59:26 +0100662is appended to the buffer before invoking the callback.
663
664When a buffer is used both for input and output, the output lines are put
665above the last line, since the last line is what is written to the channel
Bram Moolenaar77cdfd12016-03-12 12:57:59 +0100666input. Otherwise lines are appended below the last line.
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100667
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100668When using JS or JSON mode with "buffer", only messages with zero or negative
669ID will be added to the buffer, after decoding + encoding. Messages with a
670positive number will be handled by a callback, commands are handled as usual.
671
Bram Moolenaar82af8712016-06-04 20:20:29 +0200672The name of the buffer from "out_name" or "err_name" is compared the full name
673of existing buffers, also after expanding the name for the current directory.
674E.g., when a buffer was created with ":edit somename" and the buffer name is
675"somename" it will use that buffer.
676
677If there is no matching buffer a new buffer is created. Use an empty name to
678always create a new buffer. |ch_getbufnr()| can then be used to get the
679buffer number.
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100680
681For a new buffer 'buftype' is set to "nofile" and 'bufhidden' to "hide". If
682you prefer other settings, create the buffer first and pass the buffer number.
Bram Moolenaar187db502016-02-27 14:44:26 +0100683
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200684The "out_modifiable" and "err_modifiable" options can be used to set the
685'modifiable' option off, or write to a buffer that has 'modifiable' off. That
686means that lines will be appended to the buffer, but the user can't easily
687change the buffer.
688
689When an existing buffer is to be written where 'modifiable' is off and the
690"out_modifiable" or "err_modifiable" options is not zero, an error is given
691and the buffer will not be written to.
692
Bram Moolenaar187db502016-02-27 14:44:26 +0100693When the buffer written to is displayed in a window and the cursor is in the
694first column of the last line, the cursor will be moved to the newly added
695line and the window is scrolled up to show the cursor if needed.
696
Bram Moolenaar063b9d12016-07-09 20:21:48 +0200697Undo is synced for every added line. NUL bytes are accepted (internally Vim
698stores these as NL bytes).
Bram Moolenaar38a55632016-02-15 22:07:32 +0100699
Bram Moolenaar77cdfd12016-03-12 12:57:59 +0100700
701Writing to a file ~
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100702 *E920*
Bram Moolenaar77cdfd12016-03-12 12:57:59 +0100703The file is created with permissions 600 (read-write for the user, not
704accessible for others). Use |setfperm()| to change this.
705
706If the file already exists it is truncated.
707
Bram Moolenaar38a55632016-02-15 22:07:32 +0100708==============================================================================
70911. Controlling a job *job-control*
710
711To get the status of a job: >
712 echo job_status(job)
713
714To make a job stop running: >
715 job_stop(job)
716
717This is the normal way to end a job. On Unix it sends a SIGTERM to the job.
718It is possible to use other ways to stop the job, or even send arbitrary
719signals. E.g. to force a job to stop, "kill it": >
720 job_stop(job, "kill")
721
722For more options see |job_stop()|.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100723
724
725 vim:tw=78:ts=8:ft=help:norl: