blob: 8767d8714b268d23c54a300fdacede60ceafbf21 [file] [log] [blame]
Bram Moolenaar8fe10002019-09-11 22:56:44 +02001*channel.txt* For Vim version 8.1. Last change: 2019 Sep 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|
Bram Moolenaar54775062019-07-31 21:07:14 +0200218. Channel functions details |channel-functions-details|
Bram Moolenaared997ad2019-07-21 16:42:00 +0200229. Starting a job with a channel |job-start|
2310. Starting a job without a channel |job-start-nochannel|
2411. Job functions |job-functions-details|
2512. Job options |job-options|
2613. Controlling a job |job-control|
2714. Using a prompt buffer |prompt-buffer|
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010028
Bram Moolenaar38a55632016-02-15 22:07:32 +010029{only when compiled with the |+channel| feature for channel stuff}
Bram Moolenaarf37506f2016-08-31 22:22:10 +020030 You can check this with: `has('channel')`
Bram Moolenaar38a55632016-02-15 22:07:32 +010031{only when compiled with the |+job| feature for job stuff}
Bram Moolenaarf37506f2016-08-31 22:22:10 +020032 You can check this with: `has('job')`
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010033
34==============================================================================
Bram Moolenaar38a55632016-02-15 22:07:32 +0100351. Overview *job-channel-overview*
36
37There are four main types of jobs:
Bram Moolenaar50ba5262016-09-22 22:33:02 +0200381. A daemon, serving several Vim instances.
Bram Moolenaar38a55632016-02-15 22:07:32 +010039 Vim connects to it with a socket.
402. One job working with one Vim instance, asynchronously.
41 Uses a socket or pipes.
423. A job performing some work for a short time, asynchronously.
43 Uses a socket or pipes.
444. Running a filter, synchronously.
45 Uses pipes.
46
Bram Moolenaar77cdfd12016-03-12 12:57:59 +010047For when using sockets See |job-start|, |job-start-nochannel| and
48|channel-open|. For 2 and 3, one or more jobs using pipes, see |job-start|.
Bram Moolenaar38a55632016-02-15 22:07:32 +010049For 4 use the ":{range}!cmd" command, see |filter|.
50
51Over the socket and pipes these protocols are available:
52RAW nothing known, Vim cannot tell where a message ends
53NL every message ends in a NL (newline) character
54JSON JSON encoding |json_encode()|
55JS JavaScript style JSON-like encoding |js_encode()|
56
57Common combination are:
58- Using a job connected through pipes in NL mode. E.g., to run a style
59 checker and receive errors and warnings.
Bram Moolenaar7dda86f2018-04-20 22:36:41 +020060- Using a daemon, connecting over a socket in JSON mode. E.g. to lookup
Bram Moolenaar09521312016-08-12 22:54:35 +020061 cross-references in a database.
Bram Moolenaar38a55632016-02-15 22:07:32 +010062
63==============================================================================
Bram Moolenaar26852122016-05-24 20:02:38 +0200642. Channel demo *channel-demo* *demoserver.py*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010065
66This requires Python. The demo program can be found in
67$VIMRUNTIME/tools/demoserver.py
68Run it in one terminal. We will call this T1.
69
70Run Vim in another terminal. Connect to the demo server with: >
Bram Moolenaar38a55632016-02-15 22:07:32 +010071 let channel = ch_open('localhost:8765')
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010072
73In T1 you should see:
74 === socket opened === ~
75
76You can now send a message to the server: >
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010077 echo ch_evalexpr(channel, 'hello!')
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010078
79The message is received in T1 and a response is sent back to Vim.
80You can see the raw messages in T1. What Vim sends is:
81 [1,"hello!"] ~
82And the response is:
83 [1,"got it"] ~
84The number will increase every time you send a message.
85
86The server can send a command to Vim. Type this on T1 (literally, including
Bram Moolenaarfb1f6262016-01-31 20:24:32 +010087the quotes):
88 ["ex","echo 'hi there'"] ~
89And you should see the message in Vim. You can move the cursor a word forward:
90 ["normal","w"] ~
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010091
92To handle asynchronous communication a callback needs to be used: >
Bram Moolenaar38a55632016-02-15 22:07:32 +010093 func MyHandler(channel, msg)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010094 echo "from the handler: " . a:msg
95 endfunc
Bram Moolenaar02e83b42016-02-21 20:10:26 +010096 call ch_sendexpr(channel, 'hello!', {'callback': "MyHandler"})
Bram Moolenaar38a55632016-02-15 22:07:32 +010097Vim will not wait for a response. Now the server can send the response later
98and MyHandler will be invoked.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010099
100Instead of giving a callback with every send call, it can also be specified
101when opening the channel: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100102 call ch_close(channel)
103 let channel = ch_open('localhost:8765', {'callback': "MyHandler"})
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100104 call ch_sendexpr(channel, 'hello!')
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100105
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100106When trying out channels it's useful to see what is going on. You can tell
107Vim to write lines in log file: >
108 call ch_logfile('channellog', 'w')
109See |ch_logfile()|.
110
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100111==============================================================================
Bram Moolenaar38a55632016-02-15 22:07:32 +01001123. Opening a channel *channel-open*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100113
Bram Moolenaar681baaf2016-02-04 20:57:07 +0100114To open a channel: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100115 let channel = ch_open({address} [, {options}])
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100116 if ch_status(channel) == "open"
117 " use the channel
Bram Moolenaar38a55632016-02-15 22:07:32 +0100118
119Use |ch_status()| to see if the channel could be opened.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100120
121{address} has the form "hostname:port". E.g., "localhost:8765".
122
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100123{options} is a dictionary with optional entries: *channel-open-options*
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100124
125"mode" can be: *channel-mode*
126 "json" - Use JSON, see below; most convenient way. Default.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100127 "js" - Use JS (JavaScript) encoding, more efficient than JSON.
Bram Moolenaar38a55632016-02-15 22:07:32 +0100128 "nl" - Use messages that end in a NL character
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100129 "raw" - Use raw messages
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100130 *channel-callback* *E921*
Bram Moolenaar38a55632016-02-15 22:07:32 +0100131"callback" A function that is called when a message is received that is
132 not handled otherwise. It gets two arguments: the channel
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100133 and the received message. Example: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100134 func Handle(channel, msg)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100135 echo 'Received: ' . a:msg
136 endfunc
Bram Moolenaar38a55632016-02-15 22:07:32 +0100137 let channel = ch_open("localhost:8765", {"callback": "Handle"})
138<
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100139 When "mode" is "json" or "js" the "msg" argument is the body
140 of the received message, converted to Vim types.
141 When "mode" is "nl" the "msg" argument is one message,
142 excluding the NL.
143 When "mode" is "raw" the "msg" argument is the whole message
144 as a string.
Bram Moolenaare18c0b32016-03-20 21:08:34 +0100145
146 For all callbacks: Use |function()| to bind it to arguments
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100147 and/or a Dictionary. Or use the form "dict.function" to bind
148 the Dictionary.
Bram Moolenaar06d2d382016-05-20 17:24:11 +0200149
150 Callbacks are only called at a "safe" moment, usually when Vim
151 is waiting for the user to type a character. Vim does not use
152 multi-threading.
153
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100154 *close_cb*
155"close_cb" A function that is called when the channel gets closed, other
Bram Moolenaar38a55632016-02-15 22:07:32 +0100156 than by calling ch_close(). It should be defined like this: >
157 func MyCloseHandler(channel)
Bram Moolenaar06d2d382016-05-20 17:24:11 +0200158< Vim will invoke callbacks that handle data before invoking
159 close_cb, thus when this function is called no more data will
Bram Moolenaar68e65602019-05-26 21:33:31 +0200160 be passed to the callbacks. However, if a callback causes Vim
161 to check for messages, the close_cb may be invoked while still
162 in the callback. The plugin must handle this somehow, it can
163 be useful to know that no more data is coming.
Bram Moolenaar958dc692016-12-01 15:34:12 +0100164 *channel-drop*
165"drop" Specifies when to drop messages:
166 "auto" When there is no callback to handle a message.
167 The "close_cb" is also considered for this.
168 "never" All messages will be kept.
169
Bram Moolenaar0b146882018-09-06 16:27:24 +0200170 *channel-noblock*
171"noblock" Same effect as |job-noblock|. Only matters for writing.
172
Bram Moolenaar06d2d382016-05-20 17:24:11 +0200173 *waittime*
Bram Moolenaar38a55632016-02-15 22:07:32 +0100174"waittime" The time to wait for the connection to be made in
Bram Moolenaarf3913272016-02-25 00:00:01 +0100175 milliseconds. A negative number waits forever.
176
177 The default is zero, don't wait, which is useful if a local
178 server is supposed to be running already. On Unix Vim
179 actually uses a 1 msec timeout, that is required on many
180 systems. Use a larger value for a remote server, e.g. 10
181 msec at least.
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100182 *channel-timeout*
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100183"timeout" The time to wait for a request when blocking, E.g. when using
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100184 ch_evalexpr(). In milliseconds. The default is 2000 (2
Bram Moolenaar38a55632016-02-15 22:07:32 +0100185 seconds).
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100186
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100187When "mode" is "json" or "js" the "callback" is optional. When omitted it is
188only possible to receive a message after sending one.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100189
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100190To change the channel options after opening it use |ch_setoptions()|. The
191arguments are similar to what is passed to |ch_open()|, but "waittime" cannot
192be given, since that only applies to opening the channel.
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100193
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100194For example, the handler can be added or changed: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100195 call ch_setoptions(channel, {'callback': callback})
196When "callback" is empty (zero or an empty string) the handler is removed.
197
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100198After a callback has been invoked Vim will update the screen and put the
199cursor back where it belongs. Thus the callback should not need to do
200`:redraw`.
201
Bram Moolenaar38a55632016-02-15 22:07:32 +0100202The timeout can be changed: >
203 call ch_setoptions(channel, {'timeout': msec})
204<
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100205 *channel-close* *E906*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100206Once done with the channel, disconnect it like this: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100207 call ch_close(channel)
208When a socket is used this will close the socket for both directions. When
209pipes are used (stdin/stdout/stderr) they are all closed. This might not be
210what you want! Stopping the job with job_stop() might be better.
Bram Moolenaar187db502016-02-27 14:44:26 +0100211All readahead is discarded, callbacks will no longer be invoked.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100212
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100213Note that a channel is closed in three stages:
214 - The I/O ends, log message: "Closing channel". There can still be queued
215 messages to read or callbacks to invoke.
216 - The readahead is cleared, log message: "Clearing channel". Some variables
217 may still reference the channel.
218 - The channel is freed, log message: "Freeing channel".
219
Bram Moolenaarcbebd482016-02-07 23:02:56 +0100220When the channel can't be opened you will get an error message. There is a
221difference between MS-Windows and Unix: On Unix when the port doesn't exist
222ch_open() fails quickly. On MS-Windows "waittime" applies.
Bram Moolenaaraa3b15d2016-04-21 08:53:19 +0200223*E898* *E901* *E902*
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100224
225If there is an error reading or writing a channel it will be closed.
Bram Moolenaaraa3b15d2016-04-21 08:53:19 +0200226*E630* *E631*
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100227
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100228==============================================================================
Bram Moolenaar38a55632016-02-15 22:07:32 +01002294. Using a JSON or JS channel *channel-use*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100230
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100231If mode is JSON then a message can be sent synchronously like this: >
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100232 let response = ch_evalexpr(channel, {expr})
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100233This awaits a response from the other side.
234
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100235When mode is JS this works the same, except that the messages use
Bram Moolenaar38a55632016-02-15 22:07:32 +0100236JavaScript encoding. See |js_encode()| for the difference.
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100237
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100238To send a message, without handling a response or letting the channel callback
239handle the response: >
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100240 call ch_sendexpr(channel, {expr})
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100241
242To send a message and letting the response handled by a specific function,
243asynchronously: >
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100244 call ch_sendexpr(channel, {expr}, {'callback': Handler})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100245
246Vim will match the response with the request using the message ID. Once the
247response is received the callback will be invoked. Further responses with the
248same ID will be ignored. If your server sends back multiple responses you
249need to send them with ID zero, they will be passed to the channel callback.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100250
251The {expr} is converted to JSON and wrapped in an array. An example of the
252message that the receiver will get when {expr} is the string "hello":
253 [12,"hello"] ~
254
255The format of the JSON sent is:
256 [{number},{expr}]
257
258In which {number} is different every time. It must be used in the response
259(if any):
260
261 [{number},{response}]
262
263This way Vim knows which sent message matches with which received message and
264can call the right handler. Also when the messages arrive out of order.
265
Bram Moolenaarf1f07922016-08-26 17:58:53 +0200266A newline character is terminating the JSON text. This can be used to
267separate the read text. For example, in Python:
268 splitidx = read_text.find('\n')
269 message = read_text[:splitidx]
270 rest = read_text[splitidx + 1:]
271
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100272The sender must always send valid JSON to Vim. Vim can check for the end of
273the message by parsing the JSON. It will only accept the message if the end
Bram Moolenaarf1f07922016-08-26 17:58:53 +0200274was received. A newline after the message is optional.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100275
276When the process wants to send a message to Vim without first receiving a
277message, it must use the number zero:
278 [0,{response}]
279
280Then channel handler will then get {response} converted to Vim types. If the
281channel does not have a handler the message is dropped.
282
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100283It is also possible to use ch_sendraw() and ch_evalraw() on a JSON or JS
284channel. The caller is then completely responsible for correct encoding and
285decoding.
Bram Moolenaarcbebd482016-02-07 23:02:56 +0100286
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100287==============================================================================
Bram Moolenaar38a55632016-02-15 22:07:32 +01002885. Channel commands *channel-commands*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100289
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100290With a JSON channel the process can send commands to Vim that will be
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100291handled by Vim internally, it does not require a handler for the channel.
292
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100293Possible commands are: *E903* *E904* *E905*
Bram Moolenaar220adb12016-09-12 12:17:26 +0200294 ["redraw", {forced}]
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100295 ["ex", {Ex command}]
296 ["normal", {Normal mode command}]
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100297 ["expr", {expression}, {number}]
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100298 ["expr", {expression}]
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100299 ["call", {func name}, {argument list}, {number}]
300 ["call", {func name}, {argument list}]
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100301
302With all of these: Be careful what these commands do! You can easily
303interfere with what the user is doing. To avoid trouble use |mode()| to check
304that the editor is in the expected state. E.g., to send keys that must be
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100305inserted as text, not executed as a command:
306 ["ex","if mode() == 'i' | call feedkeys('ClassName') | endif"] ~
307
308Errors in these commands are normally not reported to avoid them messing up
309the display. If you do want to see them, set the 'verbose' option to 3 or
310higher.
311
312
313Command "redraw" ~
314
Bram Moolenaar63b74a82019-03-24 15:09:13 +0100315The other commands do not explicitly update the screen, so that you can send a
316sequence of commands without the cursor moving around. A redraw can happen as
317a side effect of some commands. You must end with the "redraw" command to
318show any changed text and show the cursor where it belongs.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100319
320The argument is normally an empty string:
321 ["redraw", ""] ~
322To first clear the screen pass "force":
323 ["redraw", "force"] ~
324
325
326Command "ex" ~
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100327
328The "ex" command is executed as any Ex command. There is no response for
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100329completion or error. You could use functions in an |autoload| script:
330 ["ex","call myscript#MyFunc(arg)"]
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100331
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100332You can also use "call |feedkeys()|" to insert any key sequence.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100333
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100334When there is an error a message is written to the channel log, if it exists,
335and v:errmsg is set to the error.
336
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100337
338Command "normal" ~
339
Bram Moolenaar681baaf2016-02-04 20:57:07 +0100340The "normal" command is executed like with ":normal!", commands are not
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100341mapped. Example to open the folds under the cursor:
342 ["normal" "zO"]
343
344
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100345Command "expr" with response ~
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100346
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100347The "expr" command can be used to get the result of an expression. For
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100348example, to get the number of lines in the current buffer:
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100349 ["expr","line('$')", -2] ~
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100350
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100351It will send back the result of the expression:
Bram Moolenaare0fa3742016-02-20 15:47:01 +0100352 [-2, "last line"] ~
353The format is:
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100354 [{number}, {result}]
Bram Moolenaar187db502016-02-27 14:44:26 +0100355
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100356Here {number} is the same as what was in the request. Use a negative number
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100357to avoid confusion with message that Vim sends. Use a different number on
358every request to be able to match the request with the response.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100359
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100360{result} is the result of the evaluation and is JSON encoded. If the
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100361evaluation fails or the result can't be encoded in JSON it is the string
362"ERROR".
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100363
364
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100365Command "expr" without a response ~
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100366
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100367This command is similar to "expr" above, but does not send back any response.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100368Example:
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100369 ["expr","setline('$', ['one', 'two', 'three'])"] ~
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100370There is no third argument in the request.
371
372
373Command "call" ~
374
375This is similar to "expr", but instead of passing the whole expression as a
376string this passes the name of a function and a list of arguments. This
377avoids the conversion of the arguments to a string and escaping and
378concatenating them. Example:
379 ["call", "line", ["$"], -2] ~
380
381Leave out the fourth argument if no response is to be sent:
382 ["call", "setline", ["$", ["one", "two", "three"]]] ~
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100383
384==============================================================================
Bram Moolenaar38a55632016-02-15 22:07:32 +01003856. Using a RAW or NL channel *channel-raw*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100386
Bram Moolenaarc0514bf2016-11-17 14:50:09 +0100387If mode is RAW or NL then a message can be sent like this: >
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100388 let response = ch_evalraw(channel, {string})
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100389
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100390The {string} is sent as-is. The response will be what can be read from the
391channel right away. Since Vim doesn't know how to recognize the end of the
Bram Moolenaar38a55632016-02-15 22:07:32 +0100392message you need to take care of it yourself. The timeout applies for reading
393the first byte, after that it will not wait for anything more.
394
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100395If mode is "nl" you can send a message in a similar way. You are expected
Bram Moolenaar38a55632016-02-15 22:07:32 +0100396to put in the NL after each message. Thus you can also send several messages
397ending in a NL at once. The response will be the text up to and including the
398first NL. This can also be just the NL for an empty response.
399If no NL was read before the channel timeout an empty string is returned.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100400
401To send a message, without expecting a response: >
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100402 call ch_sendraw(channel, {string})
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100403The process can send back a response, the channel handler will be called with
404it.
405
406To send a message and letting the response handled by a specific function,
407asynchronously: >
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100408 call ch_sendraw(channel, {string}, {'callback': 'MyHandler'})
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100409
Bram Moolenaar38a55632016-02-15 22:07:32 +0100410This {string} can also be JSON, use |json_encode()| to create it and
411|json_decode()| to handle a received JSON message.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100412
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100413It is not possible to use |ch_evalexpr()| or |ch_sendexpr()| on a raw channel.
Bram Moolenaarcbebd482016-02-07 23:02:56 +0100414
Bram Moolenaar818078d2016-08-27 21:58:42 +0200415A String in Vim cannot contain NUL bytes. To send or receive NUL bytes read
416or write from a buffer. See |in_io-buffer| and |out_io-buffer|.
417
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100418==============================================================================
Bram Moolenaar38a55632016-02-15 22:07:32 +01004197. More channel functions *channel-more*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100420
Bram Moolenaar38a55632016-02-15 22:07:32 +0100421To obtain the status of a channel: ch_status(channel). The possible results
422are:
423 "fail" Failed to open the channel.
424 "open" The channel can be used.
Bram Moolenaar06481422016-04-30 15:13:38 +0200425 "buffered" The channel was closed but there is data to read.
Bram Moolenaar38a55632016-02-15 22:07:32 +0100426 "closed" The channel was closed.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100427
Bram Moolenaar187db502016-02-27 14:44:26 +0100428To obtain the job associated with a channel: ch_getjob(channel)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100429
Bram Moolenaar38a55632016-02-15 22:07:32 +0100430To read one message from a channel: >
431 let output = ch_read(channel)
432This uses the channel timeout. To read without a timeout, just get any
433message that is available: >
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100434 let output = ch_read(channel, {'timeout': 0})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100435When no message was available then the result is v:none for a JSON or JS mode
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100436channels, an empty string for a RAW or NL channel. You can use |ch_canread()|
437to check if there is something to read.
438
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200439Note that when there is no callback, messages are dropped. To avoid that add
440a close callback to the channel.
Bram Moolenaar38a55632016-02-15 22:07:32 +0100441
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100442To read all output from a RAW channel that is available: >
443 let output = ch_readraw(channel)
Bram Moolenaar38a55632016-02-15 22:07:32 +0100444To read the error output: >
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100445 let output = ch_readraw(channel, {"part": "err"})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100446
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100447ch_read() and ch_readraw() use the channel timeout. When there is nothing to
448read within that time an empty string is returned. To specify a different
449timeout in msec use the "timeout" option:
450 {"timeout": 123} ~
451To read from the error output use the "part" option:
452 {"part": "err"} ~
453To read a message with a specific ID, on a JS or JSON channel:
454 {"id": 99} ~
455When no ID is specified or the ID is -1, the first message is returned. This
456overrules any callback waiting for this message.
457
458For a RAW channel this returns whatever is available, since Vim does not know
459where a message ends.
460For a NL channel this returns one message.
461For a JS or JSON channel this returns one decoded message.
462This includes any sequence number.
463
Bram Moolenaar38a55632016-02-15 22:07:32 +0100464==============================================================================
Bram Moolenaar54775062019-07-31 21:07:14 +02004658. Channel functions details *channel-functions-details*
Bram Moolenaared997ad2019-07-21 16:42:00 +0200466
467ch_canread({handle}) *ch_canread()*
468 Return non-zero when there is something to read from {handle}.
469 {handle} can be a Channel or a Job that has a Channel.
470
471 This is useful to read from a channel at a convenient time,
472 e.g. from a timer.
473
474 Note that messages are dropped when the channel does not have
475 a callback. Add a close callback to avoid that.
476
Bram Moolenaar570497a2019-08-22 22:55:13 +0200477 Can also be used as a |method|: >
478 GetChannel()->ch_canread()
Bram Moolenaared997ad2019-07-21 16:42:00 +0200479
480ch_close({handle}) *ch_close()*
481 Close {handle}. See |channel-close|.
482 {handle} can be a Channel or a Job that has a Channel.
483 A close callback is not invoked.
484
Bram Moolenaar570497a2019-08-22 22:55:13 +0200485 Can also be used as a |method|: >
486 GetChannel()->ch_close()
Bram Moolenaared997ad2019-07-21 16:42:00 +0200487
488ch_close_in({handle}) *ch_close_in()*
489 Close the "in" part of {handle}. See |channel-close-in|.
490 {handle} can be a Channel or a Job that has a Channel.
491 A close callback is not invoked.
492
Bram Moolenaar570497a2019-08-22 22:55:13 +0200493 Can also be used as a |method|: >
494 GetChannel()->ch_close_in()
495
Bram Moolenaared997ad2019-07-21 16:42:00 +0200496
497ch_evalexpr({handle}, {expr} [, {options}]) *ch_evalexpr()*
498 Send {expr} over {handle}. The {expr} is encoded
499 according to the type of channel. The function cannot be used
500 with a raw channel. See |channel-use|.
501 {handle} can be a Channel or a Job that has a Channel.
502 *E917*
503 {options} must be a Dictionary. It must not have a "callback"
504 entry. It can have a "timeout" entry to specify the timeout
505 for this specific request.
506
507 ch_evalexpr() waits for a response and returns the decoded
508 expression. When there is an error or timeout it returns an
509 empty string.
510
Bram Moolenaar8fe10002019-09-11 22:56:44 +0200511 Note that while waiting for the response, Vim handles other
512 messages. You need to make sure this doesn't cause trouble.
513
Bram Moolenaar570497a2019-08-22 22:55:13 +0200514 Can also be used as a |method|: >
515 GetChannel()->ch_evalexpr(expr)
516
Bram Moolenaared997ad2019-07-21 16:42:00 +0200517
518ch_evalraw({handle}, {string} [, {options}]) *ch_evalraw()*
519 Send {string} over {handle}.
520 {handle} can be a Channel or a Job that has a Channel.
521
522 Works like |ch_evalexpr()|, but does not encode the request or
523 decode the response. The caller is responsible for the
524 correct contents. Also does not add a newline for a channel
525 in NL mode, the caller must do that. The NL in the response
526 is removed.
527 Note that Vim does not know when the text received on a raw
528 channel is complete, it may only return the first part and you
529 need to use |ch_readraw()| to fetch the rest.
530 See |channel-use|.
531
Bram Moolenaar570497a2019-08-22 22:55:13 +0200532 Can also be used as a |method|: >
533 GetChannel()->ch_evalraw(rawstring)
Bram Moolenaared997ad2019-07-21 16:42:00 +0200534
535ch_getbufnr({handle}, {what}) *ch_getbufnr()*
536 Get the buffer number that {handle} is using for {what}.
537 {handle} can be a Channel or a Job that has a Channel.
538 {what} can be "err" for stderr, "out" for stdout or empty for
539 socket output.
540 Returns -1 when there is no buffer.
541
Bram Moolenaar570497a2019-08-22 22:55:13 +0200542 Can also be used as a |method|: >
543 GetChannel()->ch_getbufnr(what)
Bram Moolenaared997ad2019-07-21 16:42:00 +0200544
545ch_getjob({channel}) *ch_getjob()*
546 Get the Job associated with {channel}.
547 If there is no job calling |job_status()| on the returned Job
548 will result in "fail".
549
Bram Moolenaar570497a2019-08-22 22:55:13 +0200550 Can also be used as a |method|: >
551 GetChannel()->ch_getjob()
552
Bram Moolenaared997ad2019-07-21 16:42:00 +0200553
554ch_info({handle}) *ch_info()*
555 Returns a Dictionary with information about {handle}. The
556 items are:
557 "id" number of the channel
558 "status" "open", "buffered" or "closed", like
559 ch_status()
560 When opened with ch_open():
561 "hostname" the hostname of the address
562 "port" the port of the address
563 "sock_status" "open" or "closed"
564 "sock_mode" "NL", "RAW", "JSON" or "JS"
565 "sock_io" "socket"
566 "sock_timeout" timeout in msec
567 When opened with job_start():
568 "out_status" "open", "buffered" or "closed"
569 "out_mode" "NL", "RAW", "JSON" or "JS"
570 "out_io" "null", "pipe", "file" or "buffer"
571 "out_timeout" timeout in msec
572 "err_status" "open", "buffered" or "closed"
573 "err_mode" "NL", "RAW", "JSON" or "JS"
574 "err_io" "out", "null", "pipe", "file" or "buffer"
575 "err_timeout" timeout in msec
576 "in_status" "open" or "closed"
577 "in_mode" "NL", "RAW", "JSON" or "JS"
578 "in_io" "null", "pipe", "file" or "buffer"
579 "in_timeout" timeout in msec
580
Bram Moolenaar570497a2019-08-22 22:55:13 +0200581 Can also be used as a |method|: >
582 GetChannel()->ch_info()
583
Bram Moolenaared997ad2019-07-21 16:42:00 +0200584
585ch_log({msg} [, {handle}]) *ch_log()*
586 Write {msg} in the channel log file, if it was opened with
587 |ch_logfile()|.
588 When {handle} is passed the channel number is used for the
589 message.
590 {handle} can be a Channel or a Job that has a Channel. The
591 Channel must be open for the channel number to be used.
592
Bram Moolenaar570497a2019-08-22 22:55:13 +0200593 Can also be used as a |method|: >
594 'did something'->ch_log()
595
Bram Moolenaared997ad2019-07-21 16:42:00 +0200596
597ch_logfile({fname} [, {mode}]) *ch_logfile()*
598 Start logging channel activity to {fname}.
599 When {fname} is an empty string: stop logging.
600
601 When {mode} is omitted or "a" append to the file.
602 When {mode} is "w" start with an empty file.
603
604 Use |ch_log()| to write log messages. The file is flushed
605 after every message, on Unix you can use "tail -f" to see what
606 is going on in real time.
607
608 This function is not available in the |sandbox|.
609 NOTE: the channel communication is stored in the file, be
610 aware that this may contain confidential and privacy sensitive
611 information, e.g. a password you type in a terminal window.
612
Bram Moolenaar570497a2019-08-22 22:55:13 +0200613 Can also be used as a |method|: >
614 'logfile'->ch_logfile('w')
615
Bram Moolenaared997ad2019-07-21 16:42:00 +0200616
617ch_open({address} [, {options}]) *ch_open()*
618 Open a channel to {address}. See |channel|.
619 Returns a Channel. Use |ch_status()| to check for failure.
620
621 {address} has the form "hostname:port", e.g.,
622 "localhost:8765".
623
624 If {options} is given it must be a |Dictionary|.
625 See |channel-open-options|.
626
Bram Moolenaar570497a2019-08-22 22:55:13 +0200627 Can also be used as a |method|: >
628 GetAddress()->ch_open()
629
Bram Moolenaared997ad2019-07-21 16:42:00 +0200630
631ch_read({handle} [, {options}]) *ch_read()*
632 Read from {handle} and return the received message.
633 {handle} can be a Channel or a Job that has a Channel.
634 For a NL channel this waits for a NL to arrive, except when
635 there is nothing more to read (channel was closed).
636 See |channel-more|.
637
Bram Moolenaar570497a2019-08-22 22:55:13 +0200638 Can also be used as a |method|: >
639 GetChannel()->ch_read()
640
Bram Moolenaared997ad2019-07-21 16:42:00 +0200641
642ch_readblob({handle} [, {options}]) *ch_readblob()*
643 Like ch_read() but reads binary data and returns a |Blob|.
644 See |channel-more|.
645
Bram Moolenaar570497a2019-08-22 22:55:13 +0200646 Can also be used as a |method|: >
647 GetChannel()->ch_readblob()
648
Bram Moolenaared997ad2019-07-21 16:42:00 +0200649
650ch_readraw({handle} [, {options}]) *ch_readraw()*
651 Like ch_read() but for a JS and JSON channel does not decode
652 the message. For a NL channel it does not block waiting for
653 the NL to arrive, but otherwise works like ch_read().
654 See |channel-more|.
655
Bram Moolenaar570497a2019-08-22 22:55:13 +0200656 Can also be used as a |method|: >
657 GetChannel()->ch_readraw()
658
Bram Moolenaared997ad2019-07-21 16:42:00 +0200659
660ch_sendexpr({handle}, {expr} [, {options}]) *ch_sendexpr()*
661 Send {expr} over {handle}. The {expr} is encoded
662 according to the type of channel. The function cannot be used
663 with a raw channel.
664 See |channel-use|. *E912*
665 {handle} can be a Channel or a Job that has a Channel.
666
Bram Moolenaar570497a2019-08-22 22:55:13 +0200667 Can also be used as a |method|: >
668 GetChannel()->ch_sendexpr(expr)
669
Bram Moolenaared997ad2019-07-21 16:42:00 +0200670
671ch_sendraw({handle}, {expr} [, {options}]) *ch_sendraw()*
672 Send |String| or |Blob| {expr} over {handle}.
673 Works like |ch_sendexpr()|, but does not encode the request or
674 decode the response. The caller is responsible for the
675 correct contents. Also does not add a newline for a channel
676 in NL mode, the caller must do that. The NL in the response
677 is removed.
678 See |channel-use|.
679
Bram Moolenaar570497a2019-08-22 22:55:13 +0200680 Can also be used as a |method|: >
681 GetChannel()->ch_sendraw(rawexpr)
682
Bram Moolenaared997ad2019-07-21 16:42:00 +0200683
684ch_setoptions({handle}, {options}) *ch_setoptions()*
685 Set options on {handle}:
686 "callback" the channel callback
687 "timeout" default read timeout in msec
688 "mode" mode for the whole channel
689 See |ch_open()| for more explanation.
690 {handle} can be a Channel or a Job that has a Channel.
691
692 Note that changing the mode may cause queued messages to be
693 lost.
694
695 These options cannot be changed:
696 "waittime" only applies to |ch_open()|
697
Bram Moolenaar570497a2019-08-22 22:55:13 +0200698 Can also be used as a |method|: >
699 GetChannel()->ch_setoptions(options)
700
Bram Moolenaared997ad2019-07-21 16:42:00 +0200701
702ch_status({handle} [, {options}]) *ch_status()*
703 Return the status of {handle}:
704 "fail" failed to open the channel
705 "open" channel can be used
706 "buffered" channel can be read, not written to
707 "closed" channel can not be used
708 {handle} can be a Channel or a Job that has a Channel.
709 "buffered" is used when the channel was closed but there is
710 still data that can be obtained with |ch_read()|.
711
712 If {options} is given it can contain a "part" entry to specify
713 the part of the channel to return the status for: "out" or
714 "err". For example, to get the error status: >
715 ch_status(job, {"part": "err"})
716<
Bram Moolenaar570497a2019-08-22 22:55:13 +0200717 Can also be used as a |method|: >
718 GetChannel()->ch_status()
Bram Moolenaared997ad2019-07-21 16:42:00 +0200719
720==============================================================================
7219. Starting a job with a channel *job-start* *job*
Bram Moolenaar38a55632016-02-15 22:07:32 +0100722
723To start a job and open a channel for stdin/stdout/stderr: >
724 let job = job_start(command, {options})
725
726You can get the channel with: >
727 let channel = job_getchannel(job)
728
729The channel will use NL mode. If you want another mode it's best to specify
730this in {options}. When changing the mode later some text may have already
731been received and not parsed correctly.
732
733If the command produces a line of output that you want to deal with, specify
734a handler for stdout: >
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100735 let job = job_start(command, {"out_cb": "MyHandler"})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100736The function will be called with the channel and a message. You would define
737it like this: >
738 func MyHandler(channel, msg)
739
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100740Without the handler you need to read the output with |ch_read()| or
Bram Moolenaar06481422016-04-30 15:13:38 +0200741|ch_readraw()|. You can do this in the close callback, see |read-in-close-cb|.
Bram Moolenaar38a55632016-02-15 22:07:32 +0100742
Bram Moolenaar1ccd8ff2017-08-11 19:50:37 +0200743Note that if the job exits before you read the output, the output may be lost.
744This depends on the system (on Unix this happens because closing the write end
745of a pipe causes the read end to get EOF). To avoid this make the job sleep
746for a short while before it exits.
747
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100748The handler defined for "out_cb" will not receive stderr. If you want to
749handle that separately, add an "err_cb" handler: >
750 let job = job_start(command, {"out_cb": "MyHandler",
751 \ "err_cb": "ErrHandler"})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100752
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100753If you want to handle both stderr and stdout with one handler use the
754"callback" option: >
755 let job = job_start(command, {"callback": "MyHandler"})
756
Bram Moolenaar3ec574f2017-06-13 18:12:01 +0200757Depending on the system, starting a job can put Vim in the background, the
758started job gets the focus. To avoid that, use the `foreground()` function.
759This might not always work when called early, put in the callback handler or
760use a timer to call it after the job has started.
761
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100762You can send a message to the command with ch_evalraw(). If the channel is in
763JSON or JS mode you can use ch_evalexpr().
Bram Moolenaar38a55632016-02-15 22:07:32 +0100764
765There are several options you can use, see |job-options|.
Bram Moolenaar187db502016-02-27 14:44:26 +0100766For example, to start a job and write its output in buffer "dummy": >
767 let logjob = job_start("tail -f /tmp/log",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100768 \ {'out_io': 'buffer', 'out_name': 'dummy'})
Bram Moolenaar187db502016-02-27 14:44:26 +0100769 sbuf dummy
Bram Moolenaar38a55632016-02-15 22:07:32 +0100770
Bram Moolenaar5f148ec2016-03-07 22:59:26 +0100771
772Job input from a buffer ~
Bram Moolenaar818078d2016-08-27 21:58:42 +0200773 *in_io-buffer*
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100774To run a job that reads from a buffer: >
775 let job = job_start({command},
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100776 \ {'in_io': 'buffer', 'in_name': 'mybuffer'})
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100777<
778 *E915* *E918*
779The buffer is found by name, similar to |bufnr()|. The buffer must exist and
780be loaded when job_start() is called.
781
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100782By default this reads the whole buffer. This can be changed with the "in_top"
783and "in_bot" options.
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100784
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100785A special mode is when "in_top" is set to zero and "in_bot" is not set: Every
Bram Moolenaar74675a62017-07-15 13:53:23 +0200786time a line is added to the buffer, the last-but-one line will be sent to the
Bram Moolenaar5f148ec2016-03-07 22:59:26 +0100787job stdin. This allows for editing the last line and sending it when pressing
788Enter.
Bram Moolenaar0874a832016-09-01 15:11:51 +0200789 *channel-close-in*
790When not using the special mode the pipe or socket will be closed after the
791last line has been written. This signals the reading end that the input
792finished. You can also use |ch_close_in()| to close it sooner.
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100793
Bram Moolenaar063b9d12016-07-09 20:21:48 +0200794NUL bytes in the text will be passed to the job (internally Vim stores these
795as NL bytes).
796
Bram Moolenaar06481422016-04-30 15:13:38 +0200797
798Reading job output in the close callback ~
799 *read-in-close-cb*
800If the job can take some time and you don't need intermediate results, you can
801add a close callback and read the output there: >
802
803 func! CloseHandler(channel)
Bram Moolenaar2ec618c2016-10-01 14:47:05 +0200804 while ch_status(a:channel, {'part': 'out'}) == 'buffered'
Bram Moolenaar06481422016-04-30 15:13:38 +0200805 echomsg ch_read(a:channel)
806 endwhile
807 endfunc
808 let job = job_start(command, {'close_cb': 'CloseHandler'})
809
810You will want to do something more useful than "echomsg".
811
Bram Moolenaar38a55632016-02-15 22:07:32 +0100812==============================================================================
Bram Moolenaared997ad2019-07-21 16:42:00 +020081310. Starting a job without a channel *job-start-nochannel*
Bram Moolenaar38a55632016-02-15 22:07:32 +0100814
815To start another process without creating a channel: >
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100816 let job = job_start(command,
Bram Moolenaar51628222016-12-01 23:03:28 +0100817 \ {"in_io": "null", "out_io": "null", "err_io": "null"})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100818
819This starts {command} in the background, Vim does not wait for it to finish.
820
Bram Moolenaar38a55632016-02-15 22:07:32 +0100821When Vim sees that neither stdin, stdout or stderr are connected, no channel
822will be created. Often you will want to include redirection in the command to
823avoid it getting stuck.
824
825There are several options you can use, see |job-options|.
826
Bram Moolenaar77cdfd12016-03-12 12:57:59 +0100827 *job-start-if-needed*
828To start a job only when connecting to an address does not work, do something
829like this: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100830 let channel = ch_open(address, {"waittime": 0})
831 if ch_status(channel) == "fail"
832 let job = job_start(command)
833 let channel = ch_open(address, {"waittime": 1000})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100834 endif
Bram Moolenaar77cdfd12016-03-12 12:57:59 +0100835
836Note that the waittime for ch_open() gives the job one second to make the port
837available.
Bram Moolenaar38a55632016-02-15 22:07:32 +0100838
839==============================================================================
Bram Moolenaared997ad2019-07-21 16:42:00 +020084011. Job functions *job-functions-details*
841
842job_getchannel({job}) *job_getchannel()*
843 Get the channel handle that {job} is using.
844 To check if the job has no channel: >
845 if string(job_getchannel()) == 'channel fail'
846<
Bram Moolenaar570497a2019-08-22 22:55:13 +0200847 Can also be used as a |method|: >
848 GetJob()->job_getchannel()
Bram Moolenaared997ad2019-07-21 16:42:00 +0200849
850job_info([{job}]) *job_info()*
851 Returns a Dictionary with information about {job}:
852 "status" what |job_status()| returns
853 "channel" what |job_getchannel()| returns
854 "cmd" List of command arguments used to start the job
855 "process" process ID
856 "tty_in" terminal input name, empty when none
857 "tty_out" terminal output name, empty when none
858 "exitval" only valid when "status" is "dead"
859 "exit_cb" function to be called on exit
860 "stoponexit" |job-stoponexit|
861
862 Only in Unix:
863 "termsig" the signal which terminated the process
864 (See |job_stop()| for the values)
865 only valid when "status" is "dead"
866
867 Only in MS-Windows:
868 "tty_type" Type of virtual console in use.
869 Values are "winpty" or "conpty".
870 See 'termwintype'.
871
872 Without any arguments, returns a List with all Job objects.
873
Bram Moolenaar570497a2019-08-22 22:55:13 +0200874 Can also be used as a |method|: >
875 GetJob()->job_info()
876
Bram Moolenaared997ad2019-07-21 16:42:00 +0200877
878job_setoptions({job}, {options}) *job_setoptions()*
879 Change options for {job}. Supported are:
880 "stoponexit" |job-stoponexit|
881 "exit_cb" |job-exit_cb|
882
Bram Moolenaar570497a2019-08-22 22:55:13 +0200883 Can also be used as a |method|: >
884 GetJob()->job_setoptions(options)
885
Bram Moolenaared997ad2019-07-21 16:42:00 +0200886
887job_start({command} [, {options}]) *job_start()*
888 Start a job and return a Job object. Unlike |system()| and
889 |:!cmd| this does not wait for the job to finish.
890 To start a job in a terminal window see |term_start()|.
891
892 If the job fails to start then |job_status()| on the returned
893 Job object results in "fail" and none of the callbacks will be
894 invoked.
895
896 {command} can be a String. This works best on MS-Windows. On
897 Unix it is split up in white-separated parts to be passed to
898 execvp(). Arguments in double quotes can contain white space.
899
900 {command} can be a List, where the first item is the executable
901 and further items are the arguments. All items are converted
902 to String. This works best on Unix.
903
904 On MS-Windows, job_start() makes a GUI application hidden. If
905 want to show it, Use |:!start| instead.
906
907 The command is executed directly, not through a shell, the
908 'shell' option is not used. To use the shell: >
909 let job = job_start(["/bin/sh", "-c", "echo hello"])
910< Or: >
911 let job = job_start('/bin/sh -c "echo hello"')
912< Note that this will start two processes, the shell and the
913 command it executes. If you don't want this use the "exec"
914 shell command.
915
916 On Unix $PATH is used to search for the executable only when
917 the command does not contain a slash.
918
919 The job will use the same terminal as Vim. If it reads from
920 stdin the job and Vim will be fighting over input, that
921 doesn't work. Redirect stdin and stdout to avoid problems: >
922 let job = job_start(['sh', '-c', "myserver </dev/null >/dev/null"])
923<
924 The returned Job object can be used to get the status with
925 |job_status()| and stop the job with |job_stop()|.
926
927 Note that the job object will be deleted if there are no
928 references to it. This closes the stdin and stderr, which may
929 cause the job to fail with an error. To avoid this keep a
930 reference to the job. Thus instead of: >
931 call job_start('my-command')
932< use: >
933 let myjob = job_start('my-command')
934< and unlet "myjob" once the job is not needed or is past the
935 point where it would fail (e.g. when it prints a message on
936 startup). Keep in mind that variables local to a function
937 will cease to exist if the function returns. Use a
938 script-local variable if needed: >
939 let s:myjob = job_start('my-command')
940<
941 {options} must be a Dictionary. It can contain many optional
942 items, see |job-options|.
943
Bram Moolenaar570497a2019-08-22 22:55:13 +0200944 Can also be used as a |method|: >
945 BuildCommand()->job_start()
946
Bram Moolenaared997ad2019-07-21 16:42:00 +0200947
948job_status({job}) *job_status()* *E916*
949 Returns a String with the status of {job}:
950 "run" job is running
951 "fail" job failed to start
952 "dead" job died or was stopped after running
953
954 On Unix a non-existing command results in "dead" instead of
955 "fail", because a fork happens before the failure can be
956 detected.
957
958 If an exit callback was set with the "exit_cb" option and the
959 job is now detected to be "dead" the callback will be invoked.
960
961 For more information see |job_info()|.
962
Bram Moolenaar570497a2019-08-22 22:55:13 +0200963 Can also be used as a |method|: >
964 GetJob()->job_status()
965
Bram Moolenaared997ad2019-07-21 16:42:00 +0200966
967job_stop({job} [, {how}]) *job_stop()*
968 Stop the {job}. This can also be used to signal the job.
969
970 When {how} is omitted or is "term" the job will be terminated.
971 For Unix SIGTERM is sent. On MS-Windows the job will be
972 terminated forcedly (there is no "gentle" way).
973 This goes to the process group, thus children may also be
974 affected.
975
976 Effect for Unix:
977 "term" SIGTERM (default)
978 "hup" SIGHUP
979 "quit" SIGQUIT
980 "int" SIGINT
981 "kill" SIGKILL (strongest way to stop)
982 number signal with that number
983
984 Effect for MS-Windows:
985 "term" terminate process forcedly (default)
986 "hup" CTRL_BREAK
987 "quit" CTRL_BREAK
988 "int" CTRL_C
989 "kill" terminate process forcedly
990 Others CTRL_BREAK
991
992 On Unix the signal is sent to the process group. This means
993 that when the job is "sh -c command" it affects both the shell
994 and the command.
995
996 The result is a Number: 1 if the operation could be executed,
997 0 if "how" is not supported on the system.
998 Note that even when the operation was executed, whether the
999 job was actually stopped needs to be checked with
1000 |job_status()|.
1001
1002 If the status of the job is "dead", the signal will not be
1003 sent. This is to avoid to stop the wrong job (esp. on Unix,
1004 where process numbers are recycled).
1005
1006 When using "kill" Vim will assume the job will die and close
1007 the channel.
1008
Bram Moolenaar570497a2019-08-22 22:55:13 +02001009 Can also be used as a |method|: >
1010 GetJob()->job_stop()
1011
Bram Moolenaared997ad2019-07-21 16:42:00 +02001012
1013==============================================================================
101412. Job options *job-options*
Bram Moolenaar38a55632016-02-15 22:07:32 +01001015
1016The {options} argument in job_start() is a dictionary. All entries are
Bram Moolenaar02e83b42016-02-21 20:10:26 +01001017optional. Some options can be used after the job has started, using
1018job_setoptions(job, {options}). Many options can be used with the channel
1019related to the job, using ch_setoptions(channel, {options}).
1020See |job_setoptions()| and |ch_setoptions()|.
Bram Moolenaar38a55632016-02-15 22:07:32 +01001021
Bram Moolenaar4f3f6682016-03-26 23:01:59 +01001022 *in_mode* *out_mode* *err_mode*
1023"in_mode" mode specifically for stdin, only when using pipes
1024"out_mode" mode specifically for stdout, only when using pipes
1025"err_mode" mode specifically for stderr, only when using pipes
1026 See |channel-mode| for the values.
1027
1028 Note: when setting "mode" the part specific mode is
1029 overwritten. Therefore set "mode" first and the part
1030 specific mode later.
1031
1032 Note: when writing to a file or buffer and when
1033 reading from a buffer NL mode is used by default.
1034
Bram Moolenaar0b146882018-09-06 16:27:24 +02001035 *job-noblock*
1036"noblock": 1 When writing use a non-blocking write call. This
1037 avoids getting stuck if Vim should handle other
1038 messages in between, e.g. when a job sends back data
1039 to Vim. It implies that when `ch_sendraw()` returns
1040 not all data may have been written yet.
1041 This option was added in patch 8.1.0350, test with: >
1042 if has("patch-8.1.350")
1043 let options['noblock'] = 1
1044 endif
1045<
Bram Moolenaardecb14d2016-02-20 23:32:02 +01001046 *job-callback*
1047"callback": handler Callback for something to read on any part of the
1048 channel.
Bram Moolenaar4f3f6682016-03-26 23:01:59 +01001049 *job-out_cb* *out_cb*
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001050"out_cb": handler Callback for when there is something to read on
Bram Moolenaar4f3f6682016-03-26 23:01:59 +01001051 stdout. Only for when the channel uses pipes. When
1052 "out_cb" wasn't set the channel callback is used.
Bram Moolenaar269f5952016-07-15 22:54:41 +02001053 The two arguments are the channel and the message.
Bram Moolenaar4f3f6682016-03-26 23:01:59 +01001054
1055 *job-err_cb* *err_cb*
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001056"err_cb": handler Callback for when there is something to read on
Bram Moolenaar4f3f6682016-03-26 23:01:59 +01001057 stderr. Only for when the channel uses pipes. When
1058 "err_cb" wasn't set the channel callback is used.
Bram Moolenaar269f5952016-07-15 22:54:41 +02001059 The two arguments are the channel and the message.
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001060 *job-close_cb*
1061"close_cb": handler Callback for when the channel is closed. Same as
Bram Moolenaar82af8712016-06-04 20:20:29 +02001062 "close_cb" on |ch_open()|, see |close_cb|.
Bram Moolenaarbc2eada2017-01-02 21:27:47 +01001063 *job-drop*
Bram Moolenaarb6e0ec62017-07-23 22:12:20 +02001064"drop": when Specifies when to drop messages. Same as "drop" on
Bram Moolenaar51628222016-12-01 23:03:28 +01001065 |ch_open()|, see |channel-drop|. For "auto" the
1066 exit_cb is not considered.
Bram Moolenaarbc2eada2017-01-02 21:27:47 +01001067 *job-exit_cb*
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001068"exit_cb": handler Callback for when the job ends. The arguments are the
Bram Moolenaar38a55632016-02-15 22:07:32 +01001069 job and the exit status.
Bram Moolenaarb4ada792016-10-30 21:55:26 +01001070 Vim checks up to 10 times per second for jobs that
1071 ended. The check can also be triggered by calling
1072 |job_status()|, which may then invoke the exit_cb
1073 handler.
Bram Moolenaar06d2d382016-05-20 17:24:11 +02001074 Note that data can be buffered, callbacks may still be
1075 called after the process ends.
Bram Moolenaar4f3f6682016-03-26 23:01:59 +01001076 *job-timeout*
Bram Moolenaarb6e0ec62017-07-23 22:12:20 +02001077"timeout": time The time to wait for a request when blocking, E.g.
Bram Moolenaar4f3f6682016-03-26 23:01:59 +01001078 when using ch_evalexpr(). In milliseconds. The
1079 default is 2000 (2 seconds).
1080 *out_timeout* *err_timeout*
Bram Moolenaarb6e0ec62017-07-23 22:12:20 +02001081"out_timeout": time Timeout for stdout. Only when using pipes.
1082"err_timeout": time Timeout for stderr. Only when using pipes.
Bram Moolenaar4f3f6682016-03-26 23:01:59 +01001083 Note: when setting "timeout" the part specific mode is
1084 overwritten. Therefore set "timeout" first and the
1085 part specific mode later.
1086
Bram Moolenaar02e83b42016-02-21 20:10:26 +01001087 *job-stoponexit*
1088"stoponexit": {signal} Send {signal} to the job when Vim exits. See
1089 |job_stop()| for possible values.
1090"stoponexit": "" Do not stop the job when Vim exits.
1091 The default is "term".
1092
Bram Moolenaar77cdfd12016-03-12 12:57:59 +01001093 *job-term*
Bram Moolenaarb6e0ec62017-07-23 22:12:20 +02001094"term": "open" Start a terminal in a new window and connect the job
1095 stdin/stdout/stderr to it. Similar to using
1096 `:terminal`.
Bram Moolenaar77cdfd12016-03-12 12:57:59 +01001097 NOTE: Not implemented yet!
Bram Moolenaar38a55632016-02-15 22:07:32 +01001098
Bram Moolenaar77cdfd12016-03-12 12:57:59 +01001099"channel": {channel} Use an existing channel instead of creating a new one.
1100 The parts of the channel that get used for the new job
1101 will be disconnected from what they were used before.
Bram Moolenaar51628222016-12-01 23:03:28 +01001102 If the channel was still used by another job this may
Bram Moolenaar77cdfd12016-03-12 12:57:59 +01001103 cause I/O errors.
1104 Existing callbacks and other settings remain.
1105
Bram Moolenaarb6e0ec62017-07-23 22:12:20 +02001106"pty": 1 Use a pty (pseudo-tty) instead of a pipe when
1107 possible. This is most useful in combination with a
1108 terminal window, see |terminal|.
1109 {only on Unix and Unix-like systems}
1110
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001111 *job-in_io* *in_top* *in_bot* *in_name* *in_buf*
1112"in_io": "null" disconnect stdin (read from /dev/null)
1113"in_io": "pipe" stdin is connected to the channel (default)
1114"in_io": "file" stdin reads from a file
1115"in_io": "buffer" stdin reads from a buffer
1116"in_top": number when using "buffer": first line to send (default: 1)
1117"in_bot": number when using "buffer": last line to send (default: last)
1118"in_name": "/path/file" the name of the file or buffer to read from
1119"in_buf": number the number of the buffer to read from
Bram Moolenaar38a55632016-02-15 22:07:32 +01001120
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001121 *job-out_io* *out_name* *out_buf*
1122"out_io": "null" disconnect stdout (goes to /dev/null)
1123"out_io": "pipe" stdout is connected to the channel (default)
1124"out_io": "file" stdout writes to a file
Bram Moolenaar51628222016-12-01 23:03:28 +01001125"out_io": "buffer" stdout appends to a buffer (see below)
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001126"out_name": "/path/file" the name of the file or buffer to write to
1127"out_buf": number the number of the buffer to write to
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001128"out_modifiable": 0 when writing to a buffer, 'modifiable' will be off
1129 (see below)
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001130"out_msg": 0 when writing to a new buffer, the first line will be
1131 set to "Reading from channel output..."
Bram Moolenaar38a55632016-02-15 22:07:32 +01001132
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001133 *job-err_io* *err_name* *err_buf*
1134"err_io": "out" stderr messages to go to stdout
1135"err_io": "null" disconnect stderr (goes to /dev/null)
1136"err_io": "pipe" stderr is connected to the channel (default)
1137"err_io": "file" stderr writes to a file
Bram Moolenaar51628222016-12-01 23:03:28 +01001138"err_io": "buffer" stderr appends to a buffer (see below)
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001139"err_name": "/path/file" the name of the file or buffer to write to
1140"err_buf": number the number of the buffer to write to
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001141"err_modifiable": 0 when writing to a buffer, 'modifiable' will be off
1142 (see below)
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001143"err_msg": 0 when writing to a new buffer, the first line will be
1144 set to "Reading from channel error..."
Bram Moolenaar77cdfd12016-03-12 12:57:59 +01001145
Bram Moolenaar7db8f6f2016-03-29 23:12:46 +02001146"block_write": number only for testing: pretend every other write to stdin
1147 will block
1148
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001149"env": dict environment variables for the new process
1150"cwd": "/path/to/dir" current working directory for the new process;
1151 if the directory does not exist an error is given
1152
Bram Moolenaar77cdfd12016-03-12 12:57:59 +01001153
1154Writing to a buffer ~
Bram Moolenaar818078d2016-08-27 21:58:42 +02001155 *out_io-buffer*
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001156When the out_io or err_io mode is "buffer" and there is a callback, the text
Bram Moolenaar5f148ec2016-03-07 22:59:26 +01001157is appended to the buffer before invoking the callback.
1158
1159When a buffer is used both for input and output, the output lines are put
1160above the last line, since the last line is what is written to the channel
Bram Moolenaar77cdfd12016-03-12 12:57:59 +01001161input. Otherwise lines are appended below the last line.
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001162
Bram Moolenaar328da0d2016-03-04 22:22:32 +01001163When using JS or JSON mode with "buffer", only messages with zero or negative
1164ID will be added to the buffer, after decoding + encoding. Messages with a
1165positive number will be handled by a callback, commands are handled as usual.
1166
Bram Moolenaar82af8712016-06-04 20:20:29 +02001167The name of the buffer from "out_name" or "err_name" is compared the full name
1168of existing buffers, also after expanding the name for the current directory.
1169E.g., when a buffer was created with ":edit somename" and the buffer name is
1170"somename" it will use that buffer.
1171
1172If there is no matching buffer a new buffer is created. Use an empty name to
1173always create a new buffer. |ch_getbufnr()| can then be used to get the
1174buffer number.
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001175
1176For a new buffer 'buftype' is set to "nofile" and 'bufhidden' to "hide". If
1177you prefer other settings, create the buffer first and pass the buffer number.
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001178 *out_modifiable* *err_modifiable*
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001179The "out_modifiable" and "err_modifiable" options can be used to set the
1180'modifiable' option off, or write to a buffer that has 'modifiable' off. That
1181means that lines will be appended to the buffer, but the user can't easily
1182change the buffer.
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001183 *out_msg* *err_msg*
1184The "out_msg" option can be used to specify whether a new buffer will have the
1185first line set to "Reading from channel output...". The default is to add the
1186message. "err_msg" does the same for channel error.
1187
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001188When an existing buffer is to be written where 'modifiable' is off and the
1189"out_modifiable" or "err_modifiable" options is not zero, an error is given
1190and the buffer will not be written to.
1191
Bram Moolenaar187db502016-02-27 14:44:26 +01001192When the buffer written to is displayed in a window and the cursor is in the
1193first column of the last line, the cursor will be moved to the newly added
1194line and the window is scrolled up to show the cursor if needed.
1195
Bram Moolenaar063b9d12016-07-09 20:21:48 +02001196Undo is synced for every added line. NUL bytes are accepted (internally Vim
1197stores these as NL bytes).
Bram Moolenaar38a55632016-02-15 22:07:32 +01001198
Bram Moolenaar77cdfd12016-03-12 12:57:59 +01001199
1200Writing to a file ~
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001201 *E920*
Bram Moolenaar77cdfd12016-03-12 12:57:59 +01001202The file is created with permissions 600 (read-write for the user, not
1203accessible for others). Use |setfperm()| to change this.
1204
1205If the file already exists it is truncated.
1206
Bram Moolenaar38a55632016-02-15 22:07:32 +01001207==============================================================================
Bram Moolenaared997ad2019-07-21 16:42:00 +0200120813. Controlling a job *job-control*
Bram Moolenaar38a55632016-02-15 22:07:32 +01001209
1210To get the status of a job: >
1211 echo job_status(job)
1212
1213To make a job stop running: >
1214 job_stop(job)
1215
1216This is the normal way to end a job. On Unix it sends a SIGTERM to the job.
1217It is possible to use other ways to stop the job, or even send arbitrary
1218signals. E.g. to force a job to stop, "kill it": >
1219 job_stop(job, "kill")
1220
1221For more options see |job_stop()|.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001222
Bram Moolenaarf2732452018-06-03 14:47:35 +02001223==============================================================================
Bram Moolenaared997ad2019-07-21 16:42:00 +0200122414. Using a prompt buffer *prompt-buffer*
Bram Moolenaarf2732452018-06-03 14:47:35 +02001225
1226If you want to type input for the job in a Vim window you have a few options:
1227- Use a normal buffer and handle all possible commands yourself.
1228 This will be complicated, since there are so many possible commands.
1229- Use a terminal window. This works well if what you type goes directly to
1230 the job and the job output is directly displayed in the window.
1231 See |terminal-window|.
1232- Use a prompt window. This works well when entering a line for the job in Vim
1233 while displaying (possibly filtered) output from the job.
1234
1235A prompt buffer is created by setting 'buftype' to "prompt". You would
1236normally only do that in a newly created buffer.
1237
1238The user can edit and enter one line of text at the very last line of the
1239buffer. When pressing Enter in the prompt line the callback set with
1240|prompt_setcallback()| is invoked. It would normally send the line to a job.
1241Another callback would receive the output from the job and display it in the
1242buffer, below the prompt (and above the next prompt).
1243
1244Only the text in the last line, after the prompt, is editable. The rest of the
1245buffer is not modifiable with Normal mode commands. It can be modified by
1246calling functions, such as |append()|. Using other commands may mess up the
1247buffer.
1248
1249After setting 'buftype' to "prompt" Vim does not automatically start Insert
1250mode, use `:startinsert` if you want to enter Insert mode, so that the user
1251can start typing a line.
1252
1253The text of the prompt can be set with the |prompt_setprompt()| function.
1254
1255The user can go to Normal mode and navigate through the buffer. This can be
1256useful see older output or copy text.
1257
Bram Moolenaard2f3a8b2018-06-19 14:35:59 +02001258The CTRL-W key can be used to start a window command, such as CTRL-W w to
1259switch to the next window. This also works in Insert mode (use Shift-CTRL-W
1260to delete a word). When leaving the window Insert mode will be stopped. When
1261coming back to the prompt window Insert mode will be restored.
1262
Bram Moolenaarf2732452018-06-03 14:47:35 +02001263Any command that starts Insert mode, such as "a", "i", "A" and "I", will move
Bram Moolenaard2f3a8b2018-06-19 14:35:59 +02001264the cursor to the last line. "A" will move to the end of the line, "I" to the
1265start of the line.
Bram Moolenaarf2732452018-06-03 14:47:35 +02001266
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001267
Bram Moolenaar91f84f62018-07-29 15:07:52 +02001268 vim:tw=78:ts=8:noet:ft=help:norl: