blob: 39cd912ad564c7491b21b1c53752d3a82b625653 [file] [log] [blame]
Bram Moolenaar3ec574f2017-06-13 18:12:01 +02001*channel.txt* For Vim version 8.0. Last change: 2017 Jun 11
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}
Bram Moolenaarf37506f2016-08-31 22:22:10 +020028 You can check this with: `has('channel')`
Bram Moolenaar38a55632016-02-15 22:07:32 +010029{only when compiled with the |+job| feature for job stuff}
Bram Moolenaarf37506f2016-08-31 22:22:10 +020030 You can check this with: `has('job')`
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010031
32==============================================================================
Bram Moolenaar38a55632016-02-15 22:07:32 +0100331. Overview *job-channel-overview*
34
35There are four main types of jobs:
Bram Moolenaar50ba5262016-09-22 22:33:02 +0200361. A daemon, serving several Vim instances.
Bram Moolenaar38a55632016-02-15 22:07:32 +010037 Vim connects to it with a socket.
382. One job working with one Vim instance, asynchronously.
39 Uses a socket or pipes.
403. A job performing some work for a short time, asynchronously.
41 Uses a socket or pipes.
424. Running a filter, synchronously.
43 Uses pipes.
44
Bram Moolenaar77cdfd12016-03-12 12:57:59 +010045For when using sockets See |job-start|, |job-start-nochannel| and
46|channel-open|. For 2 and 3, one or more jobs using pipes, see |job-start|.
Bram Moolenaar38a55632016-02-15 22:07:32 +010047For 4 use the ":{range}!cmd" command, see |filter|.
48
49Over the socket and pipes these protocols are available:
50RAW nothing known, Vim cannot tell where a message ends
51NL every message ends in a NL (newline) character
52JSON JSON encoding |json_encode()|
53JS JavaScript style JSON-like encoding |js_encode()|
54
55Common combination are:
56- Using a job connected through pipes in NL mode. E.g., to run a style
57 checker and receive errors and warnings.
58- Using a deamon, connecting over a socket in JSON mode. E.g. to lookup
Bram Moolenaar09521312016-08-12 22:54:35 +020059 cross-references in a database.
Bram Moolenaar38a55632016-02-15 22:07:32 +010060
61==============================================================================
Bram Moolenaar26852122016-05-24 20:02:38 +0200622. Channel demo *channel-demo* *demoserver.py*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010063
64This requires Python. The demo program can be found in
65$VIMRUNTIME/tools/demoserver.py
66Run it in one terminal. We will call this T1.
67
68Run Vim in another terminal. Connect to the demo server with: >
Bram Moolenaar38a55632016-02-15 22:07:32 +010069 let channel = ch_open('localhost:8765')
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010070
71In T1 you should see:
72 === socket opened === ~
73
74You can now send a message to the server: >
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010075 echo ch_evalexpr(channel, 'hello!')
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010076
77The message is received in T1 and a response is sent back to Vim.
78You can see the raw messages in T1. What Vim sends is:
79 [1,"hello!"] ~
80And the response is:
81 [1,"got it"] ~
82The number will increase every time you send a message.
83
84The server can send a command to Vim. Type this on T1 (literally, including
Bram Moolenaarfb1f6262016-01-31 20:24:32 +010085the quotes):
86 ["ex","echo 'hi there'"] ~
87And you should see the message in Vim. You can move the cursor a word forward:
88 ["normal","w"] ~
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010089
90To handle asynchronous communication a callback needs to be used: >
Bram Moolenaar38a55632016-02-15 22:07:32 +010091 func MyHandler(channel, msg)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010092 echo "from the handler: " . a:msg
93 endfunc
Bram Moolenaar02e83b42016-02-21 20:10:26 +010094 call ch_sendexpr(channel, 'hello!', {'callback': "MyHandler"})
Bram Moolenaar38a55632016-02-15 22:07:32 +010095Vim will not wait for a response. Now the server can send the response later
96and MyHandler will be invoked.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010097
98Instead of giving a callback with every send call, it can also be specified
99when opening the channel: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100100 call ch_close(channel)
101 let channel = ch_open('localhost:8765', {'callback': "MyHandler"})
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100102 call ch_sendexpr(channel, 'hello!')
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100103
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100104When trying out channels it's useful to see what is going on. You can tell
105Vim to write lines in log file: >
106 call ch_logfile('channellog', 'w')
107See |ch_logfile()|.
108
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100109==============================================================================
Bram Moolenaar38a55632016-02-15 22:07:32 +01001103. Opening a channel *channel-open*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100111
Bram Moolenaar681baaf2016-02-04 20:57:07 +0100112To open a channel: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100113 let channel = ch_open({address} [, {options}])
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100114 if ch_status(channel) == "open"
115 " use the channel
Bram Moolenaar38a55632016-02-15 22:07:32 +0100116
117Use |ch_status()| to see if the channel could be opened.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100118
119{address} has the form "hostname:port". E.g., "localhost:8765".
120
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100121{options} is a dictionary with optional entries: *channel-open-options*
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100122
123"mode" can be: *channel-mode*
124 "json" - Use JSON, see below; most convenient way. Default.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100125 "js" - Use JS (JavaScript) encoding, more efficient than JSON.
Bram Moolenaar38a55632016-02-15 22:07:32 +0100126 "nl" - Use messages that end in a NL character
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100127 "raw" - Use raw messages
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100128 *channel-callback* *E921*
Bram Moolenaar38a55632016-02-15 22:07:32 +0100129"callback" A function that is called when a message is received that is
130 not handled otherwise. It gets two arguments: the channel
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100131 and the received message. Example: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100132 func Handle(channel, msg)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100133 echo 'Received: ' . a:msg
134 endfunc
Bram Moolenaar38a55632016-02-15 22:07:32 +0100135 let channel = ch_open("localhost:8765", {"callback": "Handle"})
136<
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100137 When "mode" is "json" or "js" the "msg" argument is the body
138 of the received message, converted to Vim types.
139 When "mode" is "nl" the "msg" argument is one message,
140 excluding the NL.
141 When "mode" is "raw" the "msg" argument is the whole message
142 as a string.
Bram Moolenaare18c0b32016-03-20 21:08:34 +0100143
144 For all callbacks: Use |function()| to bind it to arguments
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100145 and/or a Dictionary. Or use the form "dict.function" to bind
146 the Dictionary.
Bram Moolenaar06d2d382016-05-20 17:24:11 +0200147
148 Callbacks are only called at a "safe" moment, usually when Vim
149 is waiting for the user to type a character. Vim does not use
150 multi-threading.
151
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100152 *close_cb*
153"close_cb" A function that is called when the channel gets closed, other
Bram Moolenaar38a55632016-02-15 22:07:32 +0100154 than by calling ch_close(). It should be defined like this: >
155 func MyCloseHandler(channel)
Bram Moolenaar06d2d382016-05-20 17:24:11 +0200156< Vim will invoke callbacks that handle data before invoking
157 close_cb, thus when this function is called no more data will
Bram Moolenaar958dc692016-12-01 15:34:12 +0100158 be passed to the callbacks.
159 *channel-drop*
160"drop" Specifies when to drop messages:
161 "auto" When there is no callback to handle a message.
162 The "close_cb" is also considered for this.
163 "never" All messages will be kept.
164
Bram Moolenaar06d2d382016-05-20 17:24:11 +0200165 *waittime*
Bram Moolenaar38a55632016-02-15 22:07:32 +0100166"waittime" The time to wait for the connection to be made in
Bram Moolenaarf3913272016-02-25 00:00:01 +0100167 milliseconds. A negative number waits forever.
168
169 The default is zero, don't wait, which is useful if a local
170 server is supposed to be running already. On Unix Vim
171 actually uses a 1 msec timeout, that is required on many
172 systems. Use a larger value for a remote server, e.g. 10
173 msec at least.
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100174 *channel-timeout*
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100175"timeout" The time to wait for a request when blocking, E.g. when using
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100176 ch_evalexpr(). In milliseconds. The default is 2000 (2
Bram Moolenaar38a55632016-02-15 22:07:32 +0100177 seconds).
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100178
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100179When "mode" is "json" or "js" the "callback" is optional. When omitted it is
180only possible to receive a message after sending one.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100181
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100182To change the channel options after opening it use |ch_setoptions()|. The
183arguments are similar to what is passed to |ch_open()|, but "waittime" cannot
184be given, since that only applies to opening the channel.
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100185
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100186For example, the handler can be added or changed: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100187 call ch_setoptions(channel, {'callback': callback})
188When "callback" is empty (zero or an empty string) the handler is removed.
189
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100190After a callback has been invoked Vim will update the screen and put the
191cursor back where it belongs. Thus the callback should not need to do
192`:redraw`.
193
Bram Moolenaar38a55632016-02-15 22:07:32 +0100194The 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 Moolenaar4f3f6682016-03-26 23:01:59 +0100205Note that a channel is closed in three stages:
206 - The I/O ends, log message: "Closing channel". There can still be queued
207 messages to read or callbacks to invoke.
208 - The readahead is cleared, log message: "Clearing channel". Some variables
209 may still reference the channel.
210 - The channel is freed, log message: "Freeing channel".
211
Bram Moolenaarcbebd482016-02-07 23:02:56 +0100212When the channel can't be opened you will get an error message. There is a
213difference between MS-Windows and Unix: On Unix when the port doesn't exist
214ch_open() fails quickly. On MS-Windows "waittime" applies.
Bram Moolenaaraa3b15d2016-04-21 08:53:19 +0200215*E898* *E901* *E902*
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100216
217If there is an error reading or writing a channel it will be closed.
Bram Moolenaaraa3b15d2016-04-21 08:53:19 +0200218*E630* *E631*
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100219
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100220==============================================================================
Bram Moolenaar38a55632016-02-15 22:07:32 +01002214. Using a JSON or JS channel *channel-use*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100222
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100223If mode is JSON then a message can be sent synchronously like this: >
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100224 let response = ch_evalexpr(channel, {expr})
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100225This awaits a response from the other side.
226
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100227When mode is JS this works the same, except that the messages use
Bram Moolenaar38a55632016-02-15 22:07:32 +0100228JavaScript encoding. See |js_encode()| for the difference.
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100229
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100230To send a message, without handling a response or letting the channel callback
231handle the response: >
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100232 call ch_sendexpr(channel, {expr})
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100233
234To send a message and letting the response handled by a specific function,
235asynchronously: >
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100236 call ch_sendexpr(channel, {expr}, {'callback': Handler})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100237
238Vim will match the response with the request using the message ID. Once the
239response is received the callback will be invoked. Further responses with the
240same ID will be ignored. If your server sends back multiple responses you
241need to send them with ID zero, they will be passed to the channel callback.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100242
243The {expr} is converted to JSON and wrapped in an array. An example of the
244message that the receiver will get when {expr} is the string "hello":
245 [12,"hello"] ~
246
247The format of the JSON sent is:
248 [{number},{expr}]
249
250In which {number} is different every time. It must be used in the response
251(if any):
252
253 [{number},{response}]
254
255This way Vim knows which sent message matches with which received message and
256can call the right handler. Also when the messages arrive out of order.
257
Bram Moolenaarf1f07922016-08-26 17:58:53 +0200258A newline character is terminating the JSON text. This can be used to
259separate the read text. For example, in Python:
260 splitidx = read_text.find('\n')
261 message = read_text[:splitidx]
262 rest = read_text[splitidx + 1:]
263
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100264The sender must always send valid JSON to Vim. Vim can check for the end of
265the message by parsing the JSON. It will only accept the message if the end
Bram Moolenaarf1f07922016-08-26 17:58:53 +0200266was received. A newline after the message is optional.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100267
268When the process wants to send a message to Vim without first receiving a
269message, it must use the number zero:
270 [0,{response}]
271
272Then channel handler will then get {response} converted to Vim types. If the
273channel does not have a handler the message is dropped.
274
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100275It is also possible to use ch_sendraw() and ch_evalraw() on a JSON or JS
276channel. The caller is then completely responsible for correct encoding and
277decoding.
Bram Moolenaarcbebd482016-02-07 23:02:56 +0100278
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100279==============================================================================
Bram Moolenaar38a55632016-02-15 22:07:32 +01002805. Channel commands *channel-commands*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100281
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100282With a JSON channel the process can send commands to Vim that will be
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100283handled by Vim internally, it does not require a handler for the channel.
284
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100285Possible commands are: *E903* *E904* *E905*
Bram Moolenaar220adb12016-09-12 12:17:26 +0200286 ["redraw", {forced}]
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100287 ["ex", {Ex command}]
288 ["normal", {Normal mode command}]
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100289 ["expr", {expression}, {number}]
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100290 ["expr", {expression}]
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100291 ["call", {func name}, {argument list}, {number}]
292 ["call", {func name}, {argument list}]
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100293
294With all of these: Be careful what these commands do! You can easily
295interfere with what the user is doing. To avoid trouble use |mode()| to check
296that the editor is in the expected state. E.g., to send keys that must be
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100297inserted as text, not executed as a command:
298 ["ex","if mode() == 'i' | call feedkeys('ClassName') | endif"] ~
299
300Errors in these commands are normally not reported to avoid them messing up
301the display. If you do want to see them, set the 'verbose' option to 3 or
302higher.
303
304
305Command "redraw" ~
306
307The other commands do not update the screen, so that you can send a sequence
308of commands without the cursor moving around. You must end with the "redraw"
309command to show any changed text and show the cursor where it belongs.
310
311The argument is normally an empty string:
312 ["redraw", ""] ~
313To first clear the screen pass "force":
314 ["redraw", "force"] ~
315
316
317Command "ex" ~
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100318
319The "ex" command is executed as any Ex command. There is no response for
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100320completion or error. You could use functions in an |autoload| script:
321 ["ex","call myscript#MyFunc(arg)"]
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100322
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100323You can also use "call |feedkeys()|" to insert any key sequence.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100324
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100325When there is an error a message is written to the channel log, if it exists,
326and v:errmsg is set to the error.
327
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100328
329Command "normal" ~
330
Bram Moolenaar681baaf2016-02-04 20:57:07 +0100331The "normal" command is executed like with ":normal!", commands are not
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100332mapped. Example to open the folds under the cursor:
333 ["normal" "zO"]
334
335
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100336Command "expr" with response ~
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100337
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100338The "expr" command can be used to get the result of an expression. For
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100339example, to get the number of lines in the current buffer:
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100340 ["expr","line('$')", -2] ~
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100341
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100342It will send back the result of the expression:
Bram Moolenaare0fa3742016-02-20 15:47:01 +0100343 [-2, "last line"] ~
344The format is:
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100345 [{number}, {result}]
Bram Moolenaar187db502016-02-27 14:44:26 +0100346
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100347Here {number} is the same as what was in the request. Use a negative number
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100348to avoid confusion with message that Vim sends. Use a different number on
349every request to be able to match the request with the response.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100350
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100351{result} is the result of the evaluation and is JSON encoded. If the
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100352evaluation fails or the result can't be encoded in JSON it is the string
353"ERROR".
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100354
355
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100356Command "expr" without a response ~
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100357
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100358This command is similar to "expr" above, but does not send back any response.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100359Example:
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100360 ["expr","setline('$', ['one', 'two', 'three'])"] ~
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100361There is no third argument in the request.
362
363
364Command "call" ~
365
366This is similar to "expr", but instead of passing the whole expression as a
367string this passes the name of a function and a list of arguments. This
368avoids the conversion of the arguments to a string and escaping and
369concatenating them. Example:
370 ["call", "line", ["$"], -2] ~
371
372Leave out the fourth argument if no response is to be sent:
373 ["call", "setline", ["$", ["one", "two", "three"]]] ~
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100374
375==============================================================================
Bram Moolenaar38a55632016-02-15 22:07:32 +01003766. Using a RAW or NL channel *channel-raw*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100377
Bram Moolenaarc0514bf2016-11-17 14:50:09 +0100378If mode is RAW or NL then a message can be sent like this: >
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100379 let response = ch_evalraw(channel, {string})
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100380
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100381The {string} is sent as-is. The response will be what can be read from the
382channel right away. Since Vim doesn't know how to recognize the end of the
Bram Moolenaar38a55632016-02-15 22:07:32 +0100383message you need to take care of it yourself. The timeout applies for reading
384the first byte, after that it will not wait for anything more.
385
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100386If mode is "nl" you can send a message in a similar way. You are expected
Bram Moolenaar38a55632016-02-15 22:07:32 +0100387to put in the NL after each message. Thus you can also send several messages
388ending in a NL at once. The response will be the text up to and including the
389first NL. This can also be just the NL for an empty response.
390If no NL was read before the channel timeout an empty string is returned.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100391
392To send a message, without expecting a response: >
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100393 call ch_sendraw(channel, {string})
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100394The process can send back a response, the channel handler will be called with
395it.
396
397To send a message and letting the response handled by a specific function,
398asynchronously: >
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100399 call ch_sendraw(channel, {string}, {'callback': 'MyHandler'})
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100400
Bram Moolenaar38a55632016-02-15 22:07:32 +0100401This {string} can also be JSON, use |json_encode()| to create it and
402|json_decode()| to handle a received JSON message.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100403
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100404It is not possible to use |ch_evalexpr()| or |ch_sendexpr()| on a raw channel.
Bram Moolenaarcbebd482016-02-07 23:02:56 +0100405
Bram Moolenaar818078d2016-08-27 21:58:42 +0200406A String in Vim cannot contain NUL bytes. To send or receive NUL bytes read
407or write from a buffer. See |in_io-buffer| and |out_io-buffer|.
408
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100409==============================================================================
Bram Moolenaar38a55632016-02-15 22:07:32 +01004107. More channel functions *channel-more*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100411
Bram Moolenaar38a55632016-02-15 22:07:32 +0100412To obtain the status of a channel: ch_status(channel). The possible results
413are:
414 "fail" Failed to open the channel.
415 "open" The channel can be used.
Bram Moolenaar06481422016-04-30 15:13:38 +0200416 "buffered" The channel was closed but there is data to read.
Bram Moolenaar38a55632016-02-15 22:07:32 +0100417 "closed" The channel was closed.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100418
Bram Moolenaar187db502016-02-27 14:44:26 +0100419To obtain the job associated with a channel: ch_getjob(channel)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100420
Bram Moolenaar38a55632016-02-15 22:07:32 +0100421To read one message from a channel: >
422 let output = ch_read(channel)
423This uses the channel timeout. To read without a timeout, just get any
424message that is available: >
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100425 let output = ch_read(channel, {'timeout': 0})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100426When no message was available then the result is v:none for a JSON or JS mode
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100427channels, an empty string for a RAW or NL channel. You can use |ch_canread()|
428to check if there is something to read.
429
430Note that when there is no callback message are dropped. To avoid that add a
431close callback to the channel.
Bram Moolenaar38a55632016-02-15 22:07:32 +0100432
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100433To read all output from a RAW channel that is available: >
434 let output = ch_readraw(channel)
Bram Moolenaar38a55632016-02-15 22:07:32 +0100435To read the error output: >
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100436 let output = ch_readraw(channel, {"part": "err"})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100437
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100438ch_read() and ch_readraw() use the channel timeout. When there is nothing to
439read within that time an empty string is returned. To specify a different
440timeout in msec use the "timeout" option:
441 {"timeout": 123} ~
442To read from the error output use the "part" option:
443 {"part": "err"} ~
444To read a message with a specific ID, on a JS or JSON channel:
445 {"id": 99} ~
446When no ID is specified or the ID is -1, the first message is returned. This
447overrules any callback waiting for this message.
448
449For a RAW channel this returns whatever is available, since Vim does not know
450where a message ends.
451For a NL channel this returns one message.
452For a JS or JSON channel this returns one decoded message.
453This includes any sequence number.
454
Bram Moolenaar38a55632016-02-15 22:07:32 +0100455==============================================================================
4568. Starting a job with a channel *job-start* *job*
457
458To start a job and open a channel for stdin/stdout/stderr: >
459 let job = job_start(command, {options})
460
461You can get the channel with: >
462 let channel = job_getchannel(job)
463
464The channel will use NL mode. If you want another mode it's best to specify
465this in {options}. When changing the mode later some text may have already
466been received and not parsed correctly.
467
468If the command produces a line of output that you want to deal with, specify
469a handler for stdout: >
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100470 let job = job_start(command, {"out_cb": "MyHandler"})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100471The function will be called with the channel and a message. You would define
472it like this: >
473 func MyHandler(channel, msg)
474
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100475Without the handler you need to read the output with |ch_read()| or
Bram Moolenaar06481422016-04-30 15:13:38 +0200476|ch_readraw()|. You can do this in the close callback, see |read-in-close-cb|.
Bram Moolenaar38a55632016-02-15 22:07:32 +0100477
Bram Moolenaar2ec618c2016-10-01 14:47:05 +0200478Note that if the job exits before you read the output, the output may be lost.
479This depends on the system (on Unix this happens because closing the write end
480of a pipe causes the read end to get EOF). To avoid this make the job sleep
481for a short while before it exits.
482
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100483The handler defined for "out_cb" will not receive stderr. If you want to
484handle that separately, add an "err_cb" handler: >
485 let job = job_start(command, {"out_cb": "MyHandler",
486 \ "err_cb": "ErrHandler"})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100487
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100488If you want to handle both stderr and stdout with one handler use the
489"callback" option: >
490 let job = job_start(command, {"callback": "MyHandler"})
491
Bram Moolenaar3ec574f2017-06-13 18:12:01 +0200492Depending on the system, starting a job can put Vim in the background, the
493started job gets the focus. To avoid that, use the `foreground()` function.
494This might not always work when called early, put in the callback handler or
495use a timer to call it after the job has started.
496
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100497You can send a message to the command with ch_evalraw(). If the channel is in
498JSON or JS mode you can use ch_evalexpr().
Bram Moolenaar38a55632016-02-15 22:07:32 +0100499
500There are several options you can use, see |job-options|.
Bram Moolenaar187db502016-02-27 14:44:26 +0100501For example, to start a job and write its output in buffer "dummy": >
502 let logjob = job_start("tail -f /tmp/log",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100503 \ {'out_io': 'buffer', 'out_name': 'dummy'})
Bram Moolenaar187db502016-02-27 14:44:26 +0100504 sbuf dummy
Bram Moolenaar38a55632016-02-15 22:07:32 +0100505
Bram Moolenaar5f148ec2016-03-07 22:59:26 +0100506
507Job input from a buffer ~
Bram Moolenaar818078d2016-08-27 21:58:42 +0200508 *in_io-buffer*
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100509To run a job that reads from a buffer: >
510 let job = job_start({command},
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100511 \ {'in_io': 'buffer', 'in_name': 'mybuffer'})
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100512<
513 *E915* *E918*
514The buffer is found by name, similar to |bufnr()|. The buffer must exist and
515be loaded when job_start() is called.
516
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100517By default this reads the whole buffer. This can be changed with the "in_top"
518and "in_bot" options.
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100519
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100520A special mode is when "in_top" is set to zero and "in_bot" is not set: Every
Bram Moolenaar5f148ec2016-03-07 22:59:26 +0100521time a line is added to the buffer, the last-but-one line will be send to the
522job stdin. This allows for editing the last line and sending it when pressing
523Enter.
Bram Moolenaar0874a832016-09-01 15:11:51 +0200524 *channel-close-in*
525When not using the special mode the pipe or socket will be closed after the
526last line has been written. This signals the reading end that the input
527finished. You can also use |ch_close_in()| to close it sooner.
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100528
Bram Moolenaar063b9d12016-07-09 20:21:48 +0200529NUL bytes in the text will be passed to the job (internally Vim stores these
530as NL bytes).
531
Bram Moolenaar06481422016-04-30 15:13:38 +0200532
533Reading job output in the close callback ~
534 *read-in-close-cb*
535If the job can take some time and you don't need intermediate results, you can
536add a close callback and read the output there: >
537
538 func! CloseHandler(channel)
Bram Moolenaar2ec618c2016-10-01 14:47:05 +0200539 while ch_status(a:channel, {'part': 'out'}) == 'buffered'
Bram Moolenaar06481422016-04-30 15:13:38 +0200540 echomsg ch_read(a:channel)
541 endwhile
542 endfunc
543 let job = job_start(command, {'close_cb': 'CloseHandler'})
544
545You will want to do something more useful than "echomsg".
546
Bram Moolenaar38a55632016-02-15 22:07:32 +0100547==============================================================================
5489. Starting a job without a channel *job-start-nochannel*
549
550To start another process without creating a channel: >
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100551 let job = job_start(command,
Bram Moolenaar51628222016-12-01 23:03:28 +0100552 \ {"in_io": "null", "out_io": "null", "err_io": "null"})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100553
554This starts {command} in the background, Vim does not wait for it to finish.
555
Bram Moolenaar38a55632016-02-15 22:07:32 +0100556When Vim sees that neither stdin, stdout or stderr are connected, no channel
557will be created. Often you will want to include redirection in the command to
558avoid it getting stuck.
559
560There are several options you can use, see |job-options|.
561
Bram Moolenaar77cdfd12016-03-12 12:57:59 +0100562 *job-start-if-needed*
563To start a job only when connecting to an address does not work, do something
564like this: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100565 let channel = ch_open(address, {"waittime": 0})
566 if ch_status(channel) == "fail"
567 let job = job_start(command)
568 let channel = ch_open(address, {"waittime": 1000})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100569 endif
Bram Moolenaar77cdfd12016-03-12 12:57:59 +0100570
571Note that the waittime for ch_open() gives the job one second to make the port
572available.
Bram Moolenaar38a55632016-02-15 22:07:32 +0100573
574==============================================================================
57510. Job options *job-options*
576
577The {options} argument in job_start() is a dictionary. All entries are
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100578optional. Some options can be used after the job has started, using
579job_setoptions(job, {options}). Many options can be used with the channel
580related to the job, using ch_setoptions(channel, {options}).
581See |job_setoptions()| and |ch_setoptions()|.
Bram Moolenaar38a55632016-02-15 22:07:32 +0100582
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100583 *in_mode* *out_mode* *err_mode*
584"in_mode" mode specifically for stdin, only when using pipes
585"out_mode" mode specifically for stdout, only when using pipes
586"err_mode" mode specifically for stderr, only when using pipes
587 See |channel-mode| for the values.
588
589 Note: when setting "mode" the part specific mode is
590 overwritten. Therefore set "mode" first and the part
591 specific mode later.
592
593 Note: when writing to a file or buffer and when
594 reading from a buffer NL mode is used by default.
595
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100596 *job-callback*
597"callback": handler Callback for something to read on any part of the
598 channel.
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100599 *job-out_cb* *out_cb*
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100600"out_cb": handler Callback for when there is something to read on
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100601 stdout. Only for when the channel uses pipes. When
602 "out_cb" wasn't set the channel callback is used.
Bram Moolenaar269f5952016-07-15 22:54:41 +0200603 The two arguments are the channel and the message.
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100604
605 *job-err_cb* *err_cb*
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100606"err_cb": handler Callback for when there is something to read on
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100607 stderr. Only for when the channel uses pipes. When
608 "err_cb" wasn't set the channel callback is used.
Bram Moolenaar269f5952016-07-15 22:54:41 +0200609 The two arguments are the channel and the message.
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100610 *job-close_cb*
611"close_cb": handler Callback for when the channel is closed. Same as
Bram Moolenaar82af8712016-06-04 20:20:29 +0200612 "close_cb" on |ch_open()|, see |close_cb|.
Bram Moolenaarbc2eada2017-01-02 21:27:47 +0100613 *job-drop*
Bram Moolenaar51628222016-12-01 23:03:28 +0100614"drop" Specifies when to drop messages. Same as "drop" on
615 |ch_open()|, see |channel-drop|. For "auto" the
616 exit_cb is not considered.
Bram Moolenaarbc2eada2017-01-02 21:27:47 +0100617 *job-exit_cb*
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100618"exit_cb": handler Callback for when the job ends. The arguments are the
Bram Moolenaar38a55632016-02-15 22:07:32 +0100619 job and the exit status.
Bram Moolenaarb4ada792016-10-30 21:55:26 +0100620 Vim checks up to 10 times per second for jobs that
621 ended. The check can also be triggered by calling
622 |job_status()|, which may then invoke the exit_cb
623 handler.
Bram Moolenaar06d2d382016-05-20 17:24:11 +0200624 Note that data can be buffered, callbacks may still be
625 called after the process ends.
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100626 *job-timeout*
627"timeout" The time to wait for a request when blocking, E.g.
628 when using ch_evalexpr(). In milliseconds. The
629 default is 2000 (2 seconds).
630 *out_timeout* *err_timeout*
631"out_timeout" Timeout for stdout. Only when using pipes.
632"err_timeout" Timeout for stderr. Only when using pipes.
633 Note: when setting "timeout" the part specific mode is
634 overwritten. Therefore set "timeout" first and the
635 part specific mode later.
636
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100637 *job-stoponexit*
638"stoponexit": {signal} Send {signal} to the job when Vim exits. See
639 |job_stop()| for possible values.
640"stoponexit": "" Do not stop the job when Vim exits.
641 The default is "term".
642
Bram Moolenaar77cdfd12016-03-12 12:57:59 +0100643 *job-term*
Bram Moolenaar38a55632016-02-15 22:07:32 +0100644"term": "open" Start a terminal and connect the job
645 stdin/stdout/stderr to it.
Bram Moolenaar77cdfd12016-03-12 12:57:59 +0100646 NOTE: Not implemented yet!
Bram Moolenaar38a55632016-02-15 22:07:32 +0100647
Bram Moolenaar77cdfd12016-03-12 12:57:59 +0100648"channel": {channel} Use an existing channel instead of creating a new one.
649 The parts of the channel that get used for the new job
650 will be disconnected from what they were used before.
Bram Moolenaar51628222016-12-01 23:03:28 +0100651 If the channel was still used by another job this may
Bram Moolenaar77cdfd12016-03-12 12:57:59 +0100652 cause I/O errors.
653 Existing callbacks and other settings remain.
654
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100655 *job-in_io* *in_top* *in_bot* *in_name* *in_buf*
656"in_io": "null" disconnect stdin (read from /dev/null)
657"in_io": "pipe" stdin is connected to the channel (default)
658"in_io": "file" stdin reads from a file
659"in_io": "buffer" stdin reads from a buffer
660"in_top": number when using "buffer": first line to send (default: 1)
661"in_bot": number when using "buffer": last line to send (default: last)
662"in_name": "/path/file" the name of the file or buffer to read from
663"in_buf": number the number of the buffer to read from
Bram Moolenaar38a55632016-02-15 22:07:32 +0100664
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100665 *job-out_io* *out_name* *out_buf*
666"out_io": "null" disconnect stdout (goes to /dev/null)
667"out_io": "pipe" stdout is connected to the channel (default)
668"out_io": "file" stdout writes to a file
Bram Moolenaar51628222016-12-01 23:03:28 +0100669"out_io": "buffer" stdout appends to a buffer (see below)
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100670"out_name": "/path/file" the name of the file or buffer to write to
671"out_buf": number the number of the buffer to write to
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200672"out_modifiable": 0 when writing to a buffer, 'modifiable' will be off
673 (see below)
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200674"out_msg": 0 when writing to a new buffer, the first line will be
675 set to "Reading from channel output..."
Bram Moolenaar38a55632016-02-15 22:07:32 +0100676
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100677 *job-err_io* *err_name* *err_buf*
678"err_io": "out" stderr messages to go to stdout
679"err_io": "null" disconnect stderr (goes to /dev/null)
680"err_io": "pipe" stderr is connected to the channel (default)
681"err_io": "file" stderr writes to a file
Bram Moolenaar51628222016-12-01 23:03:28 +0100682"err_io": "buffer" stderr appends to a buffer (see below)
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100683"err_name": "/path/file" the name of the file or buffer to write to
684"err_buf": number the number of the buffer to write to
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200685"err_modifiable": 0 when writing to a buffer, 'modifiable' will be off
686 (see below)
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200687"err_msg": 0 when writing to a new buffer, the first line will be
688 set to "Reading from channel error..."
Bram Moolenaar77cdfd12016-03-12 12:57:59 +0100689
Bram Moolenaar7db8f6f2016-03-29 23:12:46 +0200690"block_write": number only for testing: pretend every other write to stdin
691 will block
692
Bram Moolenaar77cdfd12016-03-12 12:57:59 +0100693
694Writing to a buffer ~
Bram Moolenaar818078d2016-08-27 21:58:42 +0200695 *out_io-buffer*
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100696When the out_io or err_io mode is "buffer" and there is a callback, the text
Bram Moolenaar5f148ec2016-03-07 22:59:26 +0100697is appended to the buffer before invoking the callback.
698
699When a buffer is used both for input and output, the output lines are put
700above the last line, since the last line is what is written to the channel
Bram Moolenaar77cdfd12016-03-12 12:57:59 +0100701input. Otherwise lines are appended below the last line.
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100702
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100703When using JS or JSON mode with "buffer", only messages with zero or negative
704ID will be added to the buffer, after decoding + encoding. Messages with a
705positive number will be handled by a callback, commands are handled as usual.
706
Bram Moolenaar82af8712016-06-04 20:20:29 +0200707The name of the buffer from "out_name" or "err_name" is compared the full name
708of existing buffers, also after expanding the name for the current directory.
709E.g., when a buffer was created with ":edit somename" and the buffer name is
710"somename" it will use that buffer.
711
712If there is no matching buffer a new buffer is created. Use an empty name to
713always create a new buffer. |ch_getbufnr()| can then be used to get the
714buffer number.
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100715
716For a new buffer 'buftype' is set to "nofile" and 'bufhidden' to "hide". If
717you prefer other settings, create the buffer first and pass the buffer number.
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200718 *out_modifiable* *err_modifiable*
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200719The "out_modifiable" and "err_modifiable" options can be used to set the
720'modifiable' option off, or write to a buffer that has 'modifiable' off. That
721means that lines will be appended to the buffer, but the user can't easily
722change the buffer.
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200723 *out_msg* *err_msg*
724The "out_msg" option can be used to specify whether a new buffer will have the
725first line set to "Reading from channel output...". The default is to add the
726message. "err_msg" does the same for channel error.
727
728'modifiable' option off, or write to a buffer that has 'modifiable' off. That
729means that lines will be appended to the buffer, but the user can't easily
730change the buffer.
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200731
732When an existing buffer is to be written where 'modifiable' is off and the
733"out_modifiable" or "err_modifiable" options is not zero, an error is given
734and the buffer will not be written to.
735
Bram Moolenaar187db502016-02-27 14:44:26 +0100736When the buffer written to is displayed in a window and the cursor is in the
737first column of the last line, the cursor will be moved to the newly added
738line and the window is scrolled up to show the cursor if needed.
739
Bram Moolenaar063b9d12016-07-09 20:21:48 +0200740Undo is synced for every added line. NUL bytes are accepted (internally Vim
741stores these as NL bytes).
Bram Moolenaar38a55632016-02-15 22:07:32 +0100742
Bram Moolenaar77cdfd12016-03-12 12:57:59 +0100743
744Writing to a file ~
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100745 *E920*
Bram Moolenaar77cdfd12016-03-12 12:57:59 +0100746The file is created with permissions 600 (read-write for the user, not
747accessible for others). Use |setfperm()| to change this.
748
749If the file already exists it is truncated.
750
Bram Moolenaar38a55632016-02-15 22:07:32 +0100751==============================================================================
75211. Controlling a job *job-control*
753
754To get the status of a job: >
755 echo job_status(job)
756
757To make a job stop running: >
758 job_stop(job)
759
760This is the normal way to end a job. On Unix it sends a SIGTERM to the job.
761It is possible to use other ways to stop the job, or even send arbitrary
762signals. E.g. to force a job to stop, "kill it": >
763 job_stop(job, "kill")
764
765For more options see |job_stop()|.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100766
767
768 vim:tw=78:ts=8:ft=help:norl: