blob: ece06af5ba7c86a51131666e5b8b3b4473ade48f [file] [log] [blame]
Christian Brabandt1a946042024-06-13 19:13:28 +02001*channel.txt* For Vim version 9.1. Last change: 2024 Jun 13
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|
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +01002815. Language Server Protocol |language-server-protocol|
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010029
Bram Moolenaar2ecbe532022-07-29 21:36:21 +010030 *E1277*
Bram Moolenaar38a55632016-02-15 22:07:32 +010031{only when compiled with the |+channel| feature for channel stuff}
Bram Moolenaarf37506f2016-08-31 22:22:10 +020032 You can check this with: `has('channel')`
Bram Moolenaar38a55632016-02-15 22:07:32 +010033{only when compiled with the |+job| feature for job stuff}
Bram Moolenaarf37506f2016-08-31 22:22:10 +020034 You can check this with: `has('job')`
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010035
36==============================================================================
Bram Moolenaar38a55632016-02-15 22:07:32 +0100371. Overview *job-channel-overview*
38
39There are four main types of jobs:
Bram Moolenaar50ba5262016-09-22 22:33:02 +0200401. A daemon, serving several Vim instances.
Bram Moolenaar38a55632016-02-15 22:07:32 +010041 Vim connects to it with a socket.
422. One job working with one Vim instance, asynchronously.
43 Uses a socket or pipes.
443. A job performing some work for a short time, asynchronously.
45 Uses a socket or pipes.
464. Running a filter, synchronously.
47 Uses pipes.
48
Bram Moolenaar77cdfd12016-03-12 12:57:59 +010049For when using sockets See |job-start|, |job-start-nochannel| and
50|channel-open|. For 2 and 3, one or more jobs using pipes, see |job-start|.
Bram Moolenaar38a55632016-02-15 22:07:32 +010051For 4 use the ":{range}!cmd" command, see |filter|.
52
53Over the socket and pipes these protocols are available:
54RAW nothing known, Vim cannot tell where a message ends
55NL every message ends in a NL (newline) character
56JSON JSON encoding |json_encode()|
57JS JavaScript style JSON-like encoding |js_encode()|
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +010058LSP Language Server Protocol encoding |language-server-protocol|
Bram Moolenaar38a55632016-02-15 22:07:32 +010059
60Common combination are:
61- Using a job connected through pipes in NL mode. E.g., to run a style
62 checker and receive errors and warnings.
Bram Moolenaar7dda86f2018-04-20 22:36:41 +020063- Using a daemon, connecting over a socket in JSON mode. E.g. to lookup
Bram Moolenaar09521312016-08-12 22:54:35 +020064 cross-references in a database.
Bram Moolenaar38a55632016-02-15 22:07:32 +010065
66==============================================================================
Bram Moolenaar26852122016-05-24 20:02:38 +0200672. Channel demo *channel-demo* *demoserver.py*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010068
69This requires Python. The demo program can be found in
70$VIMRUNTIME/tools/demoserver.py
71Run it in one terminal. We will call this T1.
72
73Run Vim in another terminal. Connect to the demo server with: >
Bram Moolenaar38a55632016-02-15 22:07:32 +010074 let channel = ch_open('localhost:8765')
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010075
76In T1 you should see:
77 === socket opened === ~
78
79You can now send a message to the server: >
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010080 echo ch_evalexpr(channel, 'hello!')
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010081
82The message is received in T1 and a response is sent back to Vim.
83You can see the raw messages in T1. What Vim sends is:
84 [1,"hello!"] ~
85And the response is:
86 [1,"got it"] ~
87The number will increase every time you send a message.
88
89The server can send a command to Vim. Type this on T1 (literally, including
Bram Moolenaarfb1f6262016-01-31 20:24:32 +010090the quotes):
91 ["ex","echo 'hi there'"] ~
92And you should see the message in Vim. You can move the cursor a word forward:
93 ["normal","w"] ~
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010094
95To handle asynchronous communication a callback needs to be used: >
Bram Moolenaar38a55632016-02-15 22:07:32 +010096 func MyHandler(channel, msg)
Bram Moolenaarc51cf032022-02-26 12:25:45 +000097 echo "from the handler: " .. a:msg
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010098 endfunc
Bram Moolenaar02e83b42016-02-21 20:10:26 +010099 call ch_sendexpr(channel, 'hello!', {'callback': "MyHandler"})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100100Vim will not wait for a response. Now the server can send the response later
101and MyHandler will be invoked.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100102
103Instead of giving a callback with every send call, it can also be specified
104when opening the channel: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100105 call ch_close(channel)
106 let channel = ch_open('localhost:8765', {'callback': "MyHandler"})
Bram Moolenaar47003982021-12-05 21:54:04 +0000107 call ch_sendexpr(channel, 'hello channel!')
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100108
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100109When trying out channels it's useful to see what is going on. You can tell
110Vim to write lines in log file: >
111 call ch_logfile('channellog', 'w')
112See |ch_logfile()|.
113
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100114==============================================================================
Bram Moolenaar38a55632016-02-15 22:07:32 +01001153. Opening a channel *channel-open*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100116
Bram Moolenaar681baaf2016-02-04 20:57:07 +0100117To open a channel: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100118 let channel = ch_open({address} [, {options}])
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100119 if ch_status(channel) == "open"
120 " use the channel
Bram Moolenaar38a55632016-02-15 22:07:32 +0100121
122Use |ch_status()| to see if the channel could be opened.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100123
LemonBoycc766a82022-04-04 15:46:58 +0100124 *channel-address*
125{address} can be a domain name or an IP address, followed by a port number, or
126a Unix-domain socket path prefixed by "unix:". E.g. >
127 www.example.com:80 " domain + port
128 127.0.0.1:1234 " IPv4 + port
129 [2001:db8::1]:8765 " IPv6 + port
130 unix:/tmp/my-socket " Unix-domain socket path
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200131
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100132{options} is a dictionary with optional entries: *channel-open-options*
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100133
134"mode" can be: *channel-mode*
135 "json" - Use JSON, see below; most convenient way. Default.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100136 "js" - Use JS (JavaScript) encoding, more efficient than JSON.
Bram Moolenaar38a55632016-02-15 22:07:32 +0100137 "nl" - Use messages that end in a NL character
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100138 "raw" - Use raw messages
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +0100139 "lsp" - Use language server protocol encoding
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100140 *channel-callback* *E921*
Bram Moolenaar38a55632016-02-15 22:07:32 +0100141"callback" A function that is called when a message is received that is
Bram Moolenaar47003982021-12-05 21:54:04 +0000142 not handled otherwise (e.g. a JSON message with ID zero). It
143 gets two arguments: the channel and the received message.
144 Example: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100145 func Handle(channel, msg)
Bram Moolenaarc51cf032022-02-26 12:25:45 +0000146 echo 'Received: ' .. a:msg
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100147 endfunc
Bram Moolenaar38a55632016-02-15 22:07:32 +0100148 let channel = ch_open("localhost:8765", {"callback": "Handle"})
149<
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +0100150 When "mode" is "json" or "js" or "lsp" the "msg" argument is
151 the body of the received message, converted to Vim types.
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100152 When "mode" is "nl" the "msg" argument is one message,
153 excluding the NL.
154 When "mode" is "raw" the "msg" argument is the whole message
155 as a string.
Bram Moolenaare18c0b32016-03-20 21:08:34 +0100156
157 For all callbacks: Use |function()| to bind it to arguments
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100158 and/or a Dictionary. Or use the form "dict.function" to bind
159 the Dictionary.
Bram Moolenaar06d2d382016-05-20 17:24:11 +0200160
161 Callbacks are only called at a "safe" moment, usually when Vim
162 is waiting for the user to type a character. Vim does not use
163 multi-threading.
164
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100165 *close_cb*
166"close_cb" A function that is called when the channel gets closed, other
Bram Moolenaar38a55632016-02-15 22:07:32 +0100167 than by calling ch_close(). It should be defined like this: >
168 func MyCloseHandler(channel)
Bram Moolenaar06d2d382016-05-20 17:24:11 +0200169< Vim will invoke callbacks that handle data before invoking
170 close_cb, thus when this function is called no more data will
Bram Moolenaar68e65602019-05-26 21:33:31 +0200171 be passed to the callbacks. However, if a callback causes Vim
172 to check for messages, the close_cb may be invoked while still
173 in the callback. The plugin must handle this somehow, it can
174 be useful to know that no more data is coming.
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +0100175 If it is not known if there is a message to be read, use a
176 try/catch block: >
177 try
178 let msg = ch_readraw(a:channel)
179 catch
180 let msg = 'no message'
181 endtry
182 try
183 let err = ch_readraw(a:channel, #{part: 'err'})
184 catch
185 let err = 'no error'
186 endtry
187< *channel-drop*
Bram Moolenaar958dc692016-12-01 15:34:12 +0100188"drop" Specifies when to drop messages:
189 "auto" When there is no callback to handle a message.
190 The "close_cb" is also considered for this.
191 "never" All messages will be kept.
192
Bram Moolenaar0b146882018-09-06 16:27:24 +0200193 *channel-noblock*
194"noblock" Same effect as |job-noblock|. Only matters for writing.
195
Bram Moolenaar06d2d382016-05-20 17:24:11 +0200196 *waittime*
Bram Moolenaar38a55632016-02-15 22:07:32 +0100197"waittime" The time to wait for the connection to be made in
Bram Moolenaarf3913272016-02-25 00:00:01 +0100198 milliseconds. A negative number waits forever.
199
200 The default is zero, don't wait, which is useful if a local
201 server is supposed to be running already. On Unix Vim
202 actually uses a 1 msec timeout, that is required on many
203 systems. Use a larger value for a remote server, e.g. 10
204 msec at least.
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100205 *channel-timeout*
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100206"timeout" The time to wait for a request when blocking, E.g. when using
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100207 ch_evalexpr(). In milliseconds. The default is 2000 (2
Bram Moolenaar38a55632016-02-15 22:07:32 +0100208 seconds).
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100209
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100210When "mode" is "json" or "js" the "callback" is optional. When omitted it is
211only possible to receive a message after sending one.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100212
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100213To change the channel options after opening it use |ch_setoptions()|. The
214arguments are similar to what is passed to |ch_open()|, but "waittime" cannot
215be given, since that only applies to opening the channel.
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100216
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100217For example, the handler can be added or changed: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100218 call ch_setoptions(channel, {'callback': callback})
219When "callback" is empty (zero or an empty string) the handler is removed.
220
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100221After a callback has been invoked Vim will update the screen and put the
222cursor back where it belongs. Thus the callback should not need to do
223`:redraw`.
224
Bram Moolenaar38a55632016-02-15 22:07:32 +0100225The timeout can be changed: >
226 call ch_setoptions(channel, {'timeout': msec})
227<
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100228 *channel-close* *E906*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100229Once done with the channel, disconnect it like this: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100230 call ch_close(channel)
231When a socket is used this will close the socket for both directions. When
232pipes are used (stdin/stdout/stderr) they are all closed. This might not be
233what you want! Stopping the job with job_stop() might be better.
Bram Moolenaar187db502016-02-27 14:44:26 +0100234All readahead is discarded, callbacks will no longer be invoked.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100235
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100236Note that a channel is closed in three stages:
237 - The I/O ends, log message: "Closing channel". There can still be queued
238 messages to read or callbacks to invoke.
239 - The readahead is cleared, log message: "Clearing channel". Some variables
240 may still reference the channel.
241 - The channel is freed, log message: "Freeing channel".
242
Bram Moolenaarcbebd482016-02-07 23:02:56 +0100243When the channel can't be opened you will get an error message. There is a
244difference between MS-Windows and Unix: On Unix when the port doesn't exist
245ch_open() fails quickly. On MS-Windows "waittime" applies.
Bram Moolenaaraa3b15d2016-04-21 08:53:19 +0200246*E898* *E901* *E902*
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100247
248If there is an error reading or writing a channel it will be closed.
Bram Moolenaar664f3cf2019-12-07 16:03:51 +0100249*E630* *E631*
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100250
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100251==============================================================================
Bram Moolenaar38a55632016-02-15 22:07:32 +01002524. Using a JSON or JS channel *channel-use*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100253
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100254If mode is JSON then a message can be sent synchronously like this: >
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100255 let response = ch_evalexpr(channel, {expr})
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100256This awaits a response from the other side.
257
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100258When mode is JS this works the same, except that the messages use
Bram Moolenaar38a55632016-02-15 22:07:32 +0100259JavaScript encoding. See |js_encode()| for the difference.
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100260
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100261To send a message, without handling a response or letting the channel callback
262handle the response: >
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100263 call ch_sendexpr(channel, {expr})
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100264
265To send a message and letting the response handled by a specific function,
266asynchronously: >
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100267 call ch_sendexpr(channel, {expr}, {'callback': Handler})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100268
269Vim will match the response with the request using the message ID. Once the
270response is received the callback will be invoked. Further responses with the
271same ID will be ignored. If your server sends back multiple responses you
272need to send them with ID zero, they will be passed to the channel callback.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100273
274The {expr} is converted to JSON and wrapped in an array. An example of the
275message that the receiver will get when {expr} is the string "hello":
276 [12,"hello"] ~
277
278The format of the JSON sent is:
279 [{number},{expr}]
280
281In which {number} is different every time. It must be used in the response
282(if any):
283
284 [{number},{response}]
285
286This way Vim knows which sent message matches with which received message and
287can call the right handler. Also when the messages arrive out of order.
288
Bram Moolenaarf1f07922016-08-26 17:58:53 +0200289A newline character is terminating the JSON text. This can be used to
290separate the read text. For example, in Python:
291 splitidx = read_text.find('\n')
292 message = read_text[:splitidx]
293 rest = read_text[splitidx + 1:]
294
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100295The sender must always send valid JSON to Vim. Vim can check for the end of
296the message by parsing the JSON. It will only accept the message if the end
Bram Moolenaarf1f07922016-08-26 17:58:53 +0200297was received. A newline after the message is optional.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100298
299When the process wants to send a message to Vim without first receiving a
300message, it must use the number zero:
301 [0,{response}]
302
303Then channel handler will then get {response} converted to Vim types. If the
304channel does not have a handler the message is dropped.
305
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100306It is also possible to use ch_sendraw() and ch_evalraw() on a JSON or JS
307channel. The caller is then completely responsible for correct encoding and
308decoding.
Bram Moolenaarcbebd482016-02-07 23:02:56 +0100309
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100310==============================================================================
Bram Moolenaar38a55632016-02-15 22:07:32 +01003115. Channel commands *channel-commands*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100312
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100313With a JSON channel the process can send commands to Vim that will be
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100314handled by Vim internally, it does not require a handler for the channel.
315
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100316Possible commands are: *E903* *E904* *E905*
Bram Moolenaar220adb12016-09-12 12:17:26 +0200317 ["redraw", {forced}]
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100318 ["ex", {Ex command}]
319 ["normal", {Normal mode command}]
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100320 ["expr", {expression}, {number}]
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100321 ["expr", {expression}]
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100322 ["call", {func name}, {argument list}, {number}]
323 ["call", {func name}, {argument list}]
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100324
325With all of these: Be careful what these commands do! You can easily
326interfere with what the user is doing. To avoid trouble use |mode()| to check
327that the editor is in the expected state. E.g., to send keys that must be
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100328inserted as text, not executed as a command:
329 ["ex","if mode() == 'i' | call feedkeys('ClassName') | endif"] ~
330
331Errors in these commands are normally not reported to avoid them messing up
332the display. If you do want to see them, set the 'verbose' option to 3 or
333higher.
334
335
336Command "redraw" ~
337
Bram Moolenaar63b74a82019-03-24 15:09:13 +0100338The other commands do not explicitly update the screen, so that you can send a
339sequence of commands without the cursor moving around. A redraw can happen as
340a side effect of some commands. You must end with the "redraw" command to
341show any changed text and show the cursor where it belongs.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100342
343The argument is normally an empty string:
344 ["redraw", ""] ~
345To first clear the screen pass "force":
346 ["redraw", "force"] ~
347
348
349Command "ex" ~
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100350
351The "ex" command is executed as any Ex command. There is no response for
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100352completion or error. You could use functions in an |autoload| script:
353 ["ex","call myscript#MyFunc(arg)"]
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100354
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100355You can also use "call |feedkeys()|" to insert any key sequence.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100356
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100357When there is an error a message is written to the channel log, if it exists,
358and v:errmsg is set to the error.
359
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100360
361Command "normal" ~
362
Bram Moolenaar681baaf2016-02-04 20:57:07 +0100363The "normal" command is executed like with ":normal!", commands are not
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100364mapped. Example to open the folds under the cursor:
365 ["normal" "zO"]
366
367
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100368Command "expr" with response ~
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100369
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100370The "expr" command can be used to get the result of an expression. For
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100371example, to get the number of lines in the current buffer:
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100372 ["expr","line('$')", -2] ~
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100373
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100374It will send back the result of the expression:
Bram Moolenaare0fa3742016-02-20 15:47:01 +0100375 [-2, "last line"] ~
376The format is:
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100377 [{number}, {result}]
Bram Moolenaar187db502016-02-27 14:44:26 +0100378
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100379Here {number} is the same as what was in the request. Use a negative number
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100380to avoid confusion with message that Vim sends. Use a different number on
381every request to be able to match the request with the response.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100382
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100383{result} is the result of the evaluation and is JSON encoded. If the
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100384evaluation fails or the result can't be encoded in JSON it is the string
385"ERROR".
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100386
387
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100388Command "expr" without a response ~
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100389
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100390This command is similar to "expr" above, but does not send back any response.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100391Example:
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100392 ["expr","setline('$', ['one', 'two', 'three'])"] ~
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100393There is no third argument in the request.
394
395
396Command "call" ~
397
398This is similar to "expr", but instead of passing the whole expression as a
399string this passes the name of a function and a list of arguments. This
400avoids the conversion of the arguments to a string and escaping and
401concatenating them. Example:
402 ["call", "line", ["$"], -2] ~
403
404Leave out the fourth argument if no response is to be sent:
405 ["call", "setline", ["$", ["one", "two", "three"]]] ~
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100406
407==============================================================================
Bram Moolenaar38a55632016-02-15 22:07:32 +01004086. Using a RAW or NL channel *channel-raw*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100409
Bram Moolenaarc0514bf2016-11-17 14:50:09 +0100410If mode is RAW or NL then a message can be sent like this: >
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100411 let response = ch_evalraw(channel, {string})
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100412
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100413The {string} is sent as-is. The response will be what can be read from the
414channel right away. Since Vim doesn't know how to recognize the end of the
Bram Moolenaar38a55632016-02-15 22:07:32 +0100415message you need to take care of it yourself. The timeout applies for reading
416the first byte, after that it will not wait for anything more.
417
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100418If mode is "nl" you can send a message in a similar way. You are expected
Bram Moolenaar38a55632016-02-15 22:07:32 +0100419to put in the NL after each message. Thus you can also send several messages
420ending in a NL at once. The response will be the text up to and including the
421first NL. This can also be just the NL for an empty response.
422If no NL was read before the channel timeout an empty string is returned.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100423
424To send a message, without expecting a response: >
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100425 call ch_sendraw(channel, {string})
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100426The process can send back a response, the channel handler will be called with
427it.
428
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +0100429 *channel-onetime-callback*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100430To send a message and letting the response handled by a specific function,
431asynchronously: >
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100432 call ch_sendraw(channel, {string}, {'callback': 'MyHandler'})
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100433
Bram Moolenaar38a55632016-02-15 22:07:32 +0100434This {string} can also be JSON, use |json_encode()| to create it and
435|json_decode()| to handle a received JSON message.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100436
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100437It is not possible to use |ch_evalexpr()| or |ch_sendexpr()| on a raw channel.
Bram Moolenaarcbebd482016-02-07 23:02:56 +0100438
Bram Moolenaar818078d2016-08-27 21:58:42 +0200439A String in Vim cannot contain NUL bytes. To send or receive NUL bytes read
440or write from a buffer. See |in_io-buffer| and |out_io-buffer|.
441
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100442==============================================================================
Bram Moolenaar38a55632016-02-15 22:07:32 +01004437. More channel functions *channel-more*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100444
Bram Moolenaar38a55632016-02-15 22:07:32 +0100445To obtain the status of a channel: ch_status(channel). The possible results
446are:
447 "fail" Failed to open the channel.
448 "open" The channel can be used.
Bram Moolenaar06481422016-04-30 15:13:38 +0200449 "buffered" The channel was closed but there is data to read.
Bram Moolenaar38a55632016-02-15 22:07:32 +0100450 "closed" The channel was closed.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100451
Bram Moolenaar187db502016-02-27 14:44:26 +0100452To obtain the job associated with a channel: ch_getjob(channel)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100453
Bram Moolenaar38a55632016-02-15 22:07:32 +0100454To read one message from a channel: >
455 let output = ch_read(channel)
456This uses the channel timeout. To read without a timeout, just get any
457message that is available: >
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100458 let output = ch_read(channel, {'timeout': 0})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100459When no message was available then the result is v:none for a JSON or JS mode
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100460channels, an empty string for a RAW or NL channel. You can use |ch_canread()|
461to check if there is something to read.
462
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200463Note that when there is no callback, messages are dropped. To avoid that add
464a close callback to the channel.
Bram Moolenaar38a55632016-02-15 22:07:32 +0100465
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +0100466To read all normal output from a RAW channel that is available: >
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100467 let output = ch_readraw(channel)
Bram Moolenaar76db9e02022-11-09 21:21:04 +0000468To read all error output from a RAW channel that is available: >
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100469 let output = ch_readraw(channel, {"part": "err"})
Bram Moolenaara57b5532022-06-24 11:48:03 +0100470Note that if the channel is in NL mode, ch_readraw() will only return one line
471for each call.
Bram Moolenaar38a55632016-02-15 22:07:32 +0100472
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100473ch_read() and ch_readraw() use the channel timeout. When there is nothing to
474read within that time an empty string is returned. To specify a different
475timeout in msec use the "timeout" option:
476 {"timeout": 123} ~
477To read from the error output use the "part" option:
478 {"part": "err"} ~
479To read a message with a specific ID, on a JS or JSON channel:
480 {"id": 99} ~
481When no ID is specified or the ID is -1, the first message is returned. This
482overrules any callback waiting for this message.
483
484For a RAW channel this returns whatever is available, since Vim does not know
485where a message ends.
486For a NL channel this returns one message.
487For a JS or JSON channel this returns one decoded message.
488This includes any sequence number.
489
Bram Moolenaar38a55632016-02-15 22:07:32 +0100490==============================================================================
Bram Moolenaar54775062019-07-31 21:07:14 +02004918. Channel functions details *channel-functions-details*
Bram Moolenaared997ad2019-07-21 16:42:00 +0200492
493ch_canread({handle}) *ch_canread()*
494 Return non-zero when there is something to read from {handle}.
495 {handle} can be a Channel or a Job that has a Channel.
496
497 This is useful to read from a channel at a convenient time,
498 e.g. from a timer.
499
500 Note that messages are dropped when the channel does not have
501 a callback. Add a close callback to avoid that.
502
Bram Moolenaar570497a2019-08-22 22:55:13 +0200503 Can also be used as a |method|: >
504 GetChannel()->ch_canread()
Christian Brabandt5674c9a2024-06-09 00:13:43 +0200505<
506 Return type: |Number|
Bram Moolenaared997ad2019-07-21 16:42:00 +0200507
508ch_close({handle}) *ch_close()*
509 Close {handle}. See |channel-close|.
510 {handle} can be a Channel or a Job that has a Channel.
511 A close callback is not invoked.
512
Bram Moolenaar570497a2019-08-22 22:55:13 +0200513 Can also be used as a |method|: >
514 GetChannel()->ch_close()
Christian Brabandt5674c9a2024-06-09 00:13:43 +0200515<
516 Return type: |Number|
Bram Moolenaared997ad2019-07-21 16:42:00 +0200517
518ch_close_in({handle}) *ch_close_in()*
519 Close the "in" part of {handle}. See |channel-close-in|.
520 {handle} can be a Channel or a Job that has a Channel.
521 A close callback is not invoked.
522
Bram Moolenaar570497a2019-08-22 22:55:13 +0200523 Can also be used as a |method|: >
524 GetChannel()->ch_close_in()
Christian Brabandt5674c9a2024-06-09 00:13:43 +0200525<
526 Return type: |Number|
Bram Moolenaar570497a2019-08-22 22:55:13 +0200527
Bram Moolenaared997ad2019-07-21 16:42:00 +0200528
529ch_evalexpr({handle}, {expr} [, {options}]) *ch_evalexpr()*
530 Send {expr} over {handle}. The {expr} is encoded
531 according to the type of channel. The function cannot be used
532 with a raw channel. See |channel-use|.
533 {handle} can be a Channel or a Job that has a Channel.
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +0100534 When using the "lsp" channel mode, {expr} must be a |Dict|.
Bram Moolenaared997ad2019-07-21 16:42:00 +0200535 *E917*
536 {options} must be a Dictionary. It must not have a "callback"
537 entry. It can have a "timeout" entry to specify the timeout
538 for this specific request.
539
540 ch_evalexpr() waits for a response and returns the decoded
541 expression. When there is an error or timeout it returns an
Yegappan Lakshmanane0805b82022-04-16 15:18:23 +0100542 empty |String| or, when using the "lsp" channel mode, returns an
543 empty |Dict|.
Bram Moolenaared997ad2019-07-21 16:42:00 +0200544
Bram Moolenaar8fe10002019-09-11 22:56:44 +0200545 Note that while waiting for the response, Vim handles other
546 messages. You need to make sure this doesn't cause trouble.
547
Bram Moolenaar570497a2019-08-22 22:55:13 +0200548 Can also be used as a |method|: >
549 GetChannel()->ch_evalexpr(expr)
Christian Brabandt5674c9a2024-06-09 00:13:43 +0200550<
551 Return type: dict<any> or |String|
Bram Moolenaar570497a2019-08-22 22:55:13 +0200552
Bram Moolenaared997ad2019-07-21 16:42:00 +0200553
554ch_evalraw({handle}, {string} [, {options}]) *ch_evalraw()*
555 Send {string} over {handle}.
556 {handle} can be a Channel or a Job that has a Channel.
557
558 Works like |ch_evalexpr()|, but does not encode the request or
559 decode the response. The caller is responsible for the
560 correct contents. Also does not add a newline for a channel
561 in NL mode, the caller must do that. The NL in the response
562 is removed.
563 Note that Vim does not know when the text received on a raw
564 channel is complete, it may only return the first part and you
565 need to use |ch_readraw()| to fetch the rest.
566 See |channel-use|.
567
Bram Moolenaar570497a2019-08-22 22:55:13 +0200568 Can also be used as a |method|: >
569 GetChannel()->ch_evalraw(rawstring)
Christian Brabandt5674c9a2024-06-09 00:13:43 +0200570<
571 Return type: dict<any> or |String|
Bram Moolenaared997ad2019-07-21 16:42:00 +0200572
573ch_getbufnr({handle}, {what}) *ch_getbufnr()*
Bram Moolenaar6aa57292021-08-14 21:25:52 +0200574 Get the buffer number that {handle} is using for String {what}.
Bram Moolenaared997ad2019-07-21 16:42:00 +0200575 {handle} can be a Channel or a Job that has a Channel.
576 {what} can be "err" for stderr, "out" for stdout or empty for
577 socket output.
578 Returns -1 when there is no buffer.
579
Bram Moolenaar570497a2019-08-22 22:55:13 +0200580 Can also be used as a |method|: >
581 GetChannel()->ch_getbufnr(what)
Christian Brabandt5674c9a2024-06-09 00:13:43 +0200582<
583 Return type: |Number|
Bram Moolenaared997ad2019-07-21 16:42:00 +0200584
585ch_getjob({channel}) *ch_getjob()*
586 Get the Job associated with {channel}.
587 If there is no job calling |job_status()| on the returned Job
588 will result in "fail".
589
Bram Moolenaar570497a2019-08-22 22:55:13 +0200590 Can also be used as a |method|: >
591 GetChannel()->ch_getjob()
Christian Brabandt5674c9a2024-06-09 00:13:43 +0200592<
593 Return type: |job| or |String|
Bram Moolenaared997ad2019-07-21 16:42:00 +0200594
595ch_info({handle}) *ch_info()*
596 Returns a Dictionary with information about {handle}. The
597 items are:
598 "id" number of the channel
599 "status" "open", "buffered" or "closed", like
600 ch_status()
601 When opened with ch_open():
602 "hostname" the hostname of the address
603 "port" the port of the address
LemonBoycc766a82022-04-04 15:46:58 +0100604 "path" the path of the Unix-domain socket
Bram Moolenaared997ad2019-07-21 16:42:00 +0200605 "sock_status" "open" or "closed"
606 "sock_mode" "NL", "RAW", "JSON" or "JS"
607 "sock_io" "socket"
608 "sock_timeout" timeout in msec
LemonBoycc766a82022-04-04 15:46:58 +0100609
Bram Moolenaarcbaff5e2022-04-08 17:45:08 +0100610 Note that "path" is only present for Unix-domain sockets, for
LemonBoycc766a82022-04-04 15:46:58 +0100611 regular ones "hostname" and "port" are present instead.
612
Bram Moolenaared997ad2019-07-21 16:42:00 +0200613 When opened with job_start():
614 "out_status" "open", "buffered" or "closed"
615 "out_mode" "NL", "RAW", "JSON" or "JS"
616 "out_io" "null", "pipe", "file" or "buffer"
617 "out_timeout" timeout in msec
618 "err_status" "open", "buffered" or "closed"
619 "err_mode" "NL", "RAW", "JSON" or "JS"
620 "err_io" "out", "null", "pipe", "file" or "buffer"
621 "err_timeout" timeout in msec
622 "in_status" "open" or "closed"
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +0100623 "in_mode" "NL", "RAW", "JSON", "JS" or "LSP"
Bram Moolenaared997ad2019-07-21 16:42:00 +0200624 "in_io" "null", "pipe", "file" or "buffer"
625 "in_timeout" timeout in msec
626
Bram Moolenaar570497a2019-08-22 22:55:13 +0200627 Can also be used as a |method|: >
628 GetChannel()->ch_info()
Christian Brabandt5674c9a2024-06-09 00:13:43 +0200629<
630 Return type: dict<any>
Bram Moolenaared997ad2019-07-21 16:42:00 +0200631
632ch_log({msg} [, {handle}]) *ch_log()*
Bram Moolenaar6aa57292021-08-14 21:25:52 +0200633 Write String {msg} in the channel log file, if it was opened
634 with |ch_logfile()|.
Bram Moolenaar4f501172022-12-01 11:02:23 +0000635 The text "ch_log():" is prepended to the message to make clear
636 it came from this function call and make it easier to find in
637 the log file.
Bram Moolenaared997ad2019-07-21 16:42:00 +0200638 When {handle} is passed the channel number is used for the
639 message.
640 {handle} can be a Channel or a Job that has a Channel. The
641 Channel must be open for the channel number to be used.
642
Bram Moolenaar570497a2019-08-22 22:55:13 +0200643 Can also be used as a |method|: >
644 'did something'->ch_log()
Christian Brabandt5674c9a2024-06-09 00:13:43 +0200645<
646 Return type: dict<any>
Bram Moolenaared997ad2019-07-21 16:42:00 +0200647
648ch_logfile({fname} [, {mode}]) *ch_logfile()*
649 Start logging channel activity to {fname}.
650 When {fname} is an empty string: stop logging.
651
Bram Moolenaar1d97db32022-06-04 22:15:54 +0100652 When {mode} is omitted or contains "a" or is "o" then append
653 to the file.
654 When {mode} contains "w" and not "a" start with an empty file.
655 When {mode} contains "o" then log all terminal output.
656 Otherwise only some interesting terminal output is logged.
Bram Moolenaared997ad2019-07-21 16:42:00 +0200657
658 Use |ch_log()| to write log messages. The file is flushed
659 after every message, on Unix you can use "tail -f" to see what
660 is going on in real time.
661
Bram Moolenaar077cc7a2020-09-04 16:35:35 +0200662 To enable the log very early, to see what is received from a
Bram Moolenaar1d97db32022-06-04 22:15:54 +0100663 terminal during startup, use |--log| (this uses mode "ao"): >
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +0100664 vim --log logfile
Bram Moolenaar077cc7a2020-09-04 16:35:35 +0200665<
Bram Moolenaared997ad2019-07-21 16:42:00 +0200666 This function is not available in the |sandbox|.
667 NOTE: the channel communication is stored in the file, be
668 aware that this may contain confidential and privacy sensitive
669 information, e.g. a password you type in a terminal window.
670
Bram Moolenaar570497a2019-08-22 22:55:13 +0200671 Can also be used as a |method|: >
672 'logfile'->ch_logfile('w')
Christian Brabandt5674c9a2024-06-09 00:13:43 +0200673<
674 Return type: |Number|
Bram Moolenaared997ad2019-07-21 16:42:00 +0200675
676ch_open({address} [, {options}]) *ch_open()*
677 Open a channel to {address}. See |channel|.
678 Returns a Channel. Use |ch_status()| to check for failure.
679
LemonBoycc766a82022-04-04 15:46:58 +0100680 {address} is a String, see |channel-address| for the possible
681 accepted forms.
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200682
Bram Moolenaared997ad2019-07-21 16:42:00 +0200683 If {options} is given it must be a |Dictionary|.
684 See |channel-open-options|.
685
Bram Moolenaar570497a2019-08-22 22:55:13 +0200686 Can also be used as a |method|: >
687 GetAddress()->ch_open()
Christian Brabandt5674c9a2024-06-09 00:13:43 +0200688<
689 Return type: |channel|
Bram Moolenaared997ad2019-07-21 16:42:00 +0200690
691ch_read({handle} [, {options}]) *ch_read()*
692 Read from {handle} and return the received message.
693 {handle} can be a Channel or a Job that has a Channel.
694 For a NL channel this waits for a NL to arrive, except when
695 there is nothing more to read (channel was closed).
696 See |channel-more|.
697
Bram Moolenaar570497a2019-08-22 22:55:13 +0200698 Can also be used as a |method|: >
699 GetChannel()->ch_read()
Christian Brabandt5674c9a2024-06-09 00:13:43 +0200700<
701 Return type: |String|
Bram Moolenaared997ad2019-07-21 16:42:00 +0200702
703ch_readblob({handle} [, {options}]) *ch_readblob()*
704 Like ch_read() but reads binary data and returns a |Blob|.
705 See |channel-more|.
706
Bram Moolenaar570497a2019-08-22 22:55:13 +0200707 Can also be used as a |method|: >
708 GetChannel()->ch_readblob()
Christian Brabandt5674c9a2024-06-09 00:13:43 +0200709<
710 Return type: |Blob|
Bram Moolenaared997ad2019-07-21 16:42:00 +0200711
712ch_readraw({handle} [, {options}]) *ch_readraw()*
713 Like ch_read() but for a JS and JSON channel does not decode
714 the message. For a NL channel it does not block waiting for
715 the NL to arrive, but otherwise works like ch_read().
716 See |channel-more|.
717
Bram Moolenaar570497a2019-08-22 22:55:13 +0200718 Can also be used as a |method|: >
719 GetChannel()->ch_readraw()
Christian Brabandt5674c9a2024-06-09 00:13:43 +0200720<
721 Return type: |String|
Bram Moolenaared997ad2019-07-21 16:42:00 +0200722
723ch_sendexpr({handle}, {expr} [, {options}]) *ch_sendexpr()*
724 Send {expr} over {handle}. The {expr} is encoded
725 according to the type of channel. The function cannot be used
726 with a raw channel.
727 See |channel-use|. *E912*
728 {handle} can be a Channel or a Job that has a Channel.
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +0100729 When using the "lsp" channel mode, {expr} must be a |Dict|.
Bram Moolenaared997ad2019-07-21 16:42:00 +0200730
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +0100731 If the channel mode is "lsp", then returns a Dict. Otherwise
732 returns an empty String. If the "callback" item is present in
733 {options}, then the returned Dict contains the ID of the
734 request message. The ID can be used to send a cancellation
Yegappan Lakshmanane0805b82022-04-16 15:18:23 +0100735 request to the LSP server (if needed). Returns an empty Dict
736 on error.
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +0100737
738 If a response message is not expected for {expr}, then don't
739 specify the "callback" item in {options}.
740
Bram Moolenaar570497a2019-08-22 22:55:13 +0200741 Can also be used as a |method|: >
742 GetChannel()->ch_sendexpr(expr)
Christian Brabandt5674c9a2024-06-09 00:13:43 +0200743<
744 Return type: dict<any> or |String|
Bram Moolenaar570497a2019-08-22 22:55:13 +0200745
Bram Moolenaared997ad2019-07-21 16:42:00 +0200746
747ch_sendraw({handle}, {expr} [, {options}]) *ch_sendraw()*
748 Send |String| or |Blob| {expr} over {handle}.
749 Works like |ch_sendexpr()|, but does not encode the request or
750 decode the response. The caller is responsible for the
751 correct contents. Also does not add a newline for a channel
752 in NL mode, the caller must do that. The NL in the response
753 is removed.
754 See |channel-use|.
755
Bram Moolenaar570497a2019-08-22 22:55:13 +0200756 Can also be used as a |method|: >
757 GetChannel()->ch_sendraw(rawexpr)
Christian Brabandt5674c9a2024-06-09 00:13:43 +0200758<
759 Return type: dict<any> or |String|
Bram Moolenaared997ad2019-07-21 16:42:00 +0200760
761ch_setoptions({handle}, {options}) *ch_setoptions()*
762 Set options on {handle}:
763 "callback" the channel callback
764 "timeout" default read timeout in msec
765 "mode" mode for the whole channel
766 See |ch_open()| for more explanation.
767 {handle} can be a Channel or a Job that has a Channel.
768
769 Note that changing the mode may cause queued messages to be
770 lost.
771
772 These options cannot be changed:
773 "waittime" only applies to |ch_open()|
774
Bram Moolenaar570497a2019-08-22 22:55:13 +0200775 Can also be used as a |method|: >
776 GetChannel()->ch_setoptions(options)
Christian Brabandt5674c9a2024-06-09 00:13:43 +0200777<
778 Return type: |Number|
Bram Moolenaared997ad2019-07-21 16:42:00 +0200779
780ch_status({handle} [, {options}]) *ch_status()*
781 Return the status of {handle}:
782 "fail" failed to open the channel
783 "open" channel can be used
784 "buffered" channel can be read, not written to
785 "closed" channel can not be used
786 {handle} can be a Channel or a Job that has a Channel.
787 "buffered" is used when the channel was closed but there is
788 still data that can be obtained with |ch_read()|.
789
790 If {options} is given it can contain a "part" entry to specify
791 the part of the channel to return the status for: "out" or
792 "err". For example, to get the error status: >
793 ch_status(job, {"part": "err"})
794<
Bram Moolenaar570497a2019-08-22 22:55:13 +0200795 Can also be used as a |method|: >
796 GetChannel()->ch_status()
Christian Brabandt5674c9a2024-06-09 00:13:43 +0200797<
798 Return type: |String|
Bram Moolenaared997ad2019-07-21 16:42:00 +0200799
800==============================================================================
8019. Starting a job with a channel *job-start* *job*
Bram Moolenaar38a55632016-02-15 22:07:32 +0100802
803To start a job and open a channel for stdin/stdout/stderr: >
804 let job = job_start(command, {options})
805
806You can get the channel with: >
807 let channel = job_getchannel(job)
808
809The channel will use NL mode. If you want another mode it's best to specify
810this in {options}. When changing the mode later some text may have already
811been received and not parsed correctly.
812
813If the command produces a line of output that you want to deal with, specify
814a handler for stdout: >
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100815 let job = job_start(command, {"out_cb": "MyHandler"})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100816The function will be called with the channel and a message. You would define
817it like this: >
818 func MyHandler(channel, msg)
819
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100820Without the handler you need to read the output with |ch_read()| or
Bram Moolenaar06481422016-04-30 15:13:38 +0200821|ch_readraw()|. You can do this in the close callback, see |read-in-close-cb|.
Bram Moolenaar38a55632016-02-15 22:07:32 +0100822
Bram Moolenaar1ccd8ff2017-08-11 19:50:37 +0200823Note that if the job exits before you read the output, the output may be lost.
824This depends on the system (on Unix this happens because closing the write end
825of a pipe causes the read end to get EOF). To avoid this make the job sleep
826for a short while before it exits.
827
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100828The handler defined for "out_cb" will not receive stderr. If you want to
829handle that separately, add an "err_cb" handler: >
830 let job = job_start(command, {"out_cb": "MyHandler",
831 \ "err_cb": "ErrHandler"})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100832
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100833If you want to handle both stderr and stdout with one handler use the
834"callback" option: >
Bram Moolenaar664f3cf2019-12-07 16:03:51 +0100835 let job = job_start(command, {"callback": "MyHandler"})
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100836
Bram Moolenaar3ec574f2017-06-13 18:12:01 +0200837Depending on the system, starting a job can put Vim in the background, the
838started job gets the focus. To avoid that, use the `foreground()` function.
839This might not always work when called early, put in the callback handler or
840use a timer to call it after the job has started.
841
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100842You can send a message to the command with ch_evalraw(). If the channel is in
843JSON or JS mode you can use ch_evalexpr().
Bram Moolenaar38a55632016-02-15 22:07:32 +0100844
845There are several options you can use, see |job-options|.
Bram Moolenaar187db502016-02-27 14:44:26 +0100846For example, to start a job and write its output in buffer "dummy": >
847 let logjob = job_start("tail -f /tmp/log",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100848 \ {'out_io': 'buffer', 'out_name': 'dummy'})
Bram Moolenaar187db502016-02-27 14:44:26 +0100849 sbuf dummy
Bram Moolenaar38a55632016-02-15 22:07:32 +0100850
Bram Moolenaar5f148ec2016-03-07 22:59:26 +0100851
852Job input from a buffer ~
Bram Moolenaar818078d2016-08-27 21:58:42 +0200853 *in_io-buffer*
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100854To run a job that reads from a buffer: >
855 let job = job_start({command},
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100856 \ {'in_io': 'buffer', 'in_name': 'mybuffer'})
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100857<
858 *E915* *E918*
859The buffer is found by name, similar to |bufnr()|. The buffer must exist and
860be loaded when job_start() is called.
861
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100862By default this reads the whole buffer. This can be changed with the "in_top"
863and "in_bot" options.
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100864
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100865A special mode is when "in_top" is set to zero and "in_bot" is not set: Every
Bram Moolenaar74675a62017-07-15 13:53:23 +0200866time a line is added to the buffer, the last-but-one line will be sent to the
Bram Moolenaar5f148ec2016-03-07 22:59:26 +0100867job stdin. This allows for editing the last line and sending it when pressing
868Enter.
Bram Moolenaar0874a832016-09-01 15:11:51 +0200869 *channel-close-in*
870When not using the special mode the pipe or socket will be closed after the
871last line has been written. This signals the reading end that the input
872finished. You can also use |ch_close_in()| to close it sooner.
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100873
Bram Moolenaar063b9d12016-07-09 20:21:48 +0200874NUL bytes in the text will be passed to the job (internally Vim stores these
875as NL bytes).
876
Bram Moolenaar06481422016-04-30 15:13:38 +0200877
878Reading job output in the close callback ~
879 *read-in-close-cb*
880If the job can take some time and you don't need intermediate results, you can
881add a close callback and read the output there: >
882
883 func! CloseHandler(channel)
Bram Moolenaar2ec618c2016-10-01 14:47:05 +0200884 while ch_status(a:channel, {'part': 'out'}) == 'buffered'
Bram Moolenaar06481422016-04-30 15:13:38 +0200885 echomsg ch_read(a:channel)
886 endwhile
887 endfunc
888 let job = job_start(command, {'close_cb': 'CloseHandler'})
889
890You will want to do something more useful than "echomsg".
891
Bram Moolenaar38a55632016-02-15 22:07:32 +0100892==============================================================================
Bram Moolenaared997ad2019-07-21 16:42:00 +020089310. Starting a job without a channel *job-start-nochannel*
Bram Moolenaar38a55632016-02-15 22:07:32 +0100894
895To start another process without creating a channel: >
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100896 let job = job_start(command,
Bram Moolenaar51628222016-12-01 23:03:28 +0100897 \ {"in_io": "null", "out_io": "null", "err_io": "null"})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100898
899This starts {command} in the background, Vim does not wait for it to finish.
900
Bram Moolenaar38a55632016-02-15 22:07:32 +0100901When Vim sees that neither stdin, stdout or stderr are connected, no channel
902will be created. Often you will want to include redirection in the command to
903avoid it getting stuck.
904
905There are several options you can use, see |job-options|.
906
Bram Moolenaar77cdfd12016-03-12 12:57:59 +0100907 *job-start-if-needed*
908To start a job only when connecting to an address does not work, do something
909like this: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100910 let channel = ch_open(address, {"waittime": 0})
911 if ch_status(channel) == "fail"
912 let job = job_start(command)
913 let channel = ch_open(address, {"waittime": 1000})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100914 endif
Bram Moolenaar77cdfd12016-03-12 12:57:59 +0100915
916Note that the waittime for ch_open() gives the job one second to make the port
917available.
Bram Moolenaar38a55632016-02-15 22:07:32 +0100918
919==============================================================================
Bram Moolenaared997ad2019-07-21 16:42:00 +020092011. Job functions *job-functions-details*
921
922job_getchannel({job}) *job_getchannel()*
923 Get the channel handle that {job} is using.
924 To check if the job has no channel: >
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +0200925 if string(job_getchannel(job)) == 'channel fail'
Bram Moolenaared997ad2019-07-21 16:42:00 +0200926<
Bram Moolenaar570497a2019-08-22 22:55:13 +0200927 Can also be used as a |method|: >
928 GetJob()->job_getchannel()
Christian Brabandt5674c9a2024-06-09 00:13:43 +0200929<
930 Return type: |channel|
Bram Moolenaared997ad2019-07-21 16:42:00 +0200931
932job_info([{job}]) *job_info()*
933 Returns a Dictionary with information about {job}:
934 "status" what |job_status()| returns
935 "channel" what |job_getchannel()| returns
936 "cmd" List of command arguments used to start the job
937 "process" process ID
938 "tty_in" terminal input name, empty when none
939 "tty_out" terminal output name, empty when none
940 "exitval" only valid when "status" is "dead"
941 "exit_cb" function to be called on exit
942 "stoponexit" |job-stoponexit|
943
944 Only in Unix:
945 "termsig" the signal which terminated the process
946 (See |job_stop()| for the values)
947 only valid when "status" is "dead"
948
949 Only in MS-Windows:
950 "tty_type" Type of virtual console in use.
951 Values are "winpty" or "conpty".
952 See 'termwintype'.
953
954 Without any arguments, returns a List with all Job objects.
955
Bram Moolenaar570497a2019-08-22 22:55:13 +0200956 Can also be used as a |method|: >
957 GetJob()->job_info()
Christian Brabandt5674c9a2024-06-09 00:13:43 +0200958<
959 Return type: dict<any> or list<any> depending on whether {job}
960 was given
Bram Moolenaar570497a2019-08-22 22:55:13 +0200961
Bram Moolenaared997ad2019-07-21 16:42:00 +0200962
963job_setoptions({job}, {options}) *job_setoptions()*
964 Change options for {job}. Supported are:
965 "stoponexit" |job-stoponexit|
966 "exit_cb" |job-exit_cb|
967
Bram Moolenaar570497a2019-08-22 22:55:13 +0200968 Can also be used as a |method|: >
969 GetJob()->job_setoptions(options)
Christian Brabandt5674c9a2024-06-09 00:13:43 +0200970<
971 Return type: |Number|
Bram Moolenaar570497a2019-08-22 22:55:13 +0200972
Bram Moolenaared997ad2019-07-21 16:42:00 +0200973
974job_start({command} [, {options}]) *job_start()*
975 Start a job and return a Job object. Unlike |system()| and
976 |:!cmd| this does not wait for the job to finish.
977 To start a job in a terminal window see |term_start()|.
978
979 If the job fails to start then |job_status()| on the returned
980 Job object results in "fail" and none of the callbacks will be
981 invoked.
982
983 {command} can be a String. This works best on MS-Windows. On
984 Unix it is split up in white-separated parts to be passed to
985 execvp(). Arguments in double quotes can contain white space.
986
987 {command} can be a List, where the first item is the executable
988 and further items are the arguments. All items are converted
989 to String. This works best on Unix.
990
991 On MS-Windows, job_start() makes a GUI application hidden. If
Christian Brabandt596ad662023-08-16 00:11:09 +0200992 you want to show it, use |:!start| instead.
Bram Moolenaared997ad2019-07-21 16:42:00 +0200993
994 The command is executed directly, not through a shell, the
995 'shell' option is not used. To use the shell: >
996 let job = job_start(["/bin/sh", "-c", "echo hello"])
997< Or: >
998 let job = job_start('/bin/sh -c "echo hello"')
999< Note that this will start two processes, the shell and the
1000 command it executes. If you don't want this use the "exec"
1001 shell command.
1002
1003 On Unix $PATH is used to search for the executable only when
1004 the command does not contain a slash.
1005
1006 The job will use the same terminal as Vim. If it reads from
1007 stdin the job and Vim will be fighting over input, that
1008 doesn't work. Redirect stdin and stdout to avoid problems: >
1009 let job = job_start(['sh', '-c', "myserver </dev/null >/dev/null"])
1010<
1011 The returned Job object can be used to get the status with
1012 |job_status()| and stop the job with |job_stop()|.
1013
1014 Note that the job object will be deleted if there are no
1015 references to it. This closes the stdin and stderr, which may
1016 cause the job to fail with an error. To avoid this keep a
1017 reference to the job. Thus instead of: >
1018 call job_start('my-command')
1019< use: >
1020 let myjob = job_start('my-command')
1021< and unlet "myjob" once the job is not needed or is past the
1022 point where it would fail (e.g. when it prints a message on
1023 startup). Keep in mind that variables local to a function
1024 will cease to exist if the function returns. Use a
1025 script-local variable if needed: >
1026 let s:myjob = job_start('my-command')
1027<
1028 {options} must be a Dictionary. It can contain many optional
1029 items, see |job-options|.
1030
Bram Moolenaar570497a2019-08-22 22:55:13 +02001031 Can also be used as a |method|: >
1032 BuildCommand()->job_start()
Christian Brabandt5674c9a2024-06-09 00:13:43 +02001033<
Christian Brabandt1a946042024-06-13 19:13:28 +02001034 Return type: |job|
Bram Moolenaar570497a2019-08-22 22:55:13 +02001035
Bram Moolenaared997ad2019-07-21 16:42:00 +02001036
1037job_status({job}) *job_status()* *E916*
1038 Returns a String with the status of {job}:
1039 "run" job is running
1040 "fail" job failed to start
1041 "dead" job died or was stopped after running
1042
1043 On Unix a non-existing command results in "dead" instead of
1044 "fail", because a fork happens before the failure can be
1045 detected.
1046
Bram Moolenaarcb80aa22020-10-26 21:12:46 +01001047 If in Vim9 script a variable is declared with type "job" but
1048 never assigned to, passing that variable to job_status()
1049 returns "fail".
1050
Bram Moolenaared997ad2019-07-21 16:42:00 +02001051 If an exit callback was set with the "exit_cb" option and the
1052 job is now detected to be "dead" the callback will be invoked.
1053
1054 For more information see |job_info()|.
1055
Bram Moolenaar570497a2019-08-22 22:55:13 +02001056 Can also be used as a |method|: >
1057 GetJob()->job_status()
Christian Brabandt5674c9a2024-06-09 00:13:43 +02001058<
Christian Brabandt1a946042024-06-13 19:13:28 +02001059 Return type: |String|
Bram Moolenaar570497a2019-08-22 22:55:13 +02001060
Bram Moolenaared997ad2019-07-21 16:42:00 +02001061
1062job_stop({job} [, {how}]) *job_stop()*
1063 Stop the {job}. This can also be used to signal the job.
1064
1065 When {how} is omitted or is "term" the job will be terminated.
1066 For Unix SIGTERM is sent. On MS-Windows the job will be
1067 terminated forcedly (there is no "gentle" way).
1068 This goes to the process group, thus children may also be
1069 affected.
1070
1071 Effect for Unix:
1072 "term" SIGTERM (default)
1073 "hup" SIGHUP
1074 "quit" SIGQUIT
1075 "int" SIGINT
1076 "kill" SIGKILL (strongest way to stop)
1077 number signal with that number
1078
1079 Effect for MS-Windows:
1080 "term" terminate process forcedly (default)
1081 "hup" CTRL_BREAK
1082 "quit" CTRL_BREAK
1083 "int" CTRL_C
1084 "kill" terminate process forcedly
1085 Others CTRL_BREAK
1086
1087 On Unix the signal is sent to the process group. This means
1088 that when the job is "sh -c command" it affects both the shell
1089 and the command.
1090
1091 The result is a Number: 1 if the operation could be executed,
1092 0 if "how" is not supported on the system.
1093 Note that even when the operation was executed, whether the
1094 job was actually stopped needs to be checked with
1095 |job_status()|.
1096
1097 If the status of the job is "dead", the signal will not be
1098 sent. This is to avoid to stop the wrong job (esp. on Unix,
1099 where process numbers are recycled).
1100
1101 When using "kill" Vim will assume the job will die and close
1102 the channel.
1103
Bram Moolenaar570497a2019-08-22 22:55:13 +02001104 Can also be used as a |method|: >
1105 GetJob()->job_stop()
Christian Brabandt5674c9a2024-06-09 00:13:43 +02001106<
1107 Return type: |Number|
Bram Moolenaar570497a2019-08-22 22:55:13 +02001108
Bram Moolenaared997ad2019-07-21 16:42:00 +02001109
1110==============================================================================
111112. Job options *job-options*
Bram Moolenaar38a55632016-02-15 22:07:32 +01001112
1113The {options} argument in job_start() is a dictionary. All entries are
Bram Moolenaar02e83b42016-02-21 20:10:26 +01001114optional. Some options can be used after the job has started, using
1115job_setoptions(job, {options}). Many options can be used with the channel
1116related to the job, using ch_setoptions(channel, {options}).
1117See |job_setoptions()| and |ch_setoptions()|.
Bram Moolenaar38a55632016-02-15 22:07:32 +01001118
Bram Moolenaar4f3f6682016-03-26 23:01:59 +01001119 *in_mode* *out_mode* *err_mode*
1120"in_mode" mode specifically for stdin, only when using pipes
1121"out_mode" mode specifically for stdout, only when using pipes
1122"err_mode" mode specifically for stderr, only when using pipes
1123 See |channel-mode| for the values.
1124
1125 Note: when setting "mode" the part specific mode is
1126 overwritten. Therefore set "mode" first and the part
1127 specific mode later.
1128
1129 Note: when writing to a file or buffer and when
1130 reading from a buffer NL mode is used by default.
1131
Bram Moolenaar0b146882018-09-06 16:27:24 +02001132 *job-noblock*
1133"noblock": 1 When writing use a non-blocking write call. This
1134 avoids getting stuck if Vim should handle other
1135 messages in between, e.g. when a job sends back data
1136 to Vim. It implies that when `ch_sendraw()` returns
1137 not all data may have been written yet.
1138 This option was added in patch 8.1.0350, test with: >
1139 if has("patch-8.1.350")
1140 let options['noblock'] = 1
1141 endif
1142<
Bram Moolenaardecb14d2016-02-20 23:32:02 +01001143 *job-callback*
1144"callback": handler Callback for something to read on any part of the
1145 channel.
Bram Moolenaar4f3f6682016-03-26 23:01:59 +01001146 *job-out_cb* *out_cb*
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001147"out_cb": handler Callback for when there is something to read on
Bram Moolenaar4f3f6682016-03-26 23:01:59 +01001148 stdout. Only for when the channel uses pipes. When
1149 "out_cb" wasn't set the channel callback is used.
Bram Moolenaar269f5952016-07-15 22:54:41 +02001150 The two arguments are the channel and the message.
Bram Moolenaar4f3f6682016-03-26 23:01:59 +01001151
1152 *job-err_cb* *err_cb*
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001153"err_cb": handler Callback for when there is something to read on
Bram Moolenaar4f3f6682016-03-26 23:01:59 +01001154 stderr. Only for when the channel uses pipes. When
1155 "err_cb" wasn't set the channel callback is used.
Bram Moolenaar269f5952016-07-15 22:54:41 +02001156 The two arguments are the channel and the message.
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001157 *job-close_cb*
1158"close_cb": handler Callback for when the channel is closed. Same as
Bram Moolenaar82af8712016-06-04 20:20:29 +02001159 "close_cb" on |ch_open()|, see |close_cb|.
Bram Moolenaarbc2eada2017-01-02 21:27:47 +01001160 *job-drop*
Bram Moolenaarb6e0ec62017-07-23 22:12:20 +02001161"drop": when Specifies when to drop messages. Same as "drop" on
Bram Moolenaar51628222016-12-01 23:03:28 +01001162 |ch_open()|, see |channel-drop|. For "auto" the
1163 exit_cb is not considered.
Bram Moolenaarbc2eada2017-01-02 21:27:47 +01001164 *job-exit_cb*
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001165"exit_cb": handler Callback for when the job ends. The arguments are the
Bram Moolenaar38a55632016-02-15 22:07:32 +01001166 job and the exit status.
Bram Moolenaarb4ada792016-10-30 21:55:26 +01001167 Vim checks up to 10 times per second for jobs that
1168 ended. The check can also be triggered by calling
1169 |job_status()|, which may then invoke the exit_cb
1170 handler.
Bram Moolenaar06d2d382016-05-20 17:24:11 +02001171 Note that data can be buffered, callbacks may still be
1172 called after the process ends.
Bram Moolenaar4f3f6682016-03-26 23:01:59 +01001173 *job-timeout*
Bram Moolenaarb6e0ec62017-07-23 22:12:20 +02001174"timeout": time The time to wait for a request when blocking, E.g.
Bram Moolenaar4f3f6682016-03-26 23:01:59 +01001175 when using ch_evalexpr(). In milliseconds. The
1176 default is 2000 (2 seconds).
1177 *out_timeout* *err_timeout*
Bram Moolenaarb6e0ec62017-07-23 22:12:20 +02001178"out_timeout": time Timeout for stdout. Only when using pipes.
1179"err_timeout": time Timeout for stderr. Only when using pipes.
Bram Moolenaar4f3f6682016-03-26 23:01:59 +01001180 Note: when setting "timeout" the part specific mode is
1181 overwritten. Therefore set "timeout" first and the
1182 part specific mode later.
1183
Bram Moolenaar02e83b42016-02-21 20:10:26 +01001184 *job-stoponexit*
1185"stoponexit": {signal} Send {signal} to the job when Vim exits. See
1186 |job_stop()| for possible values.
1187"stoponexit": "" Do not stop the job when Vim exits.
1188 The default is "term".
1189
Bram Moolenaar77cdfd12016-03-12 12:57:59 +01001190 *job-term*
Bram Moolenaarb6e0ec62017-07-23 22:12:20 +02001191"term": "open" Start a terminal in a new window and connect the job
1192 stdin/stdout/stderr to it. Similar to using
1193 `:terminal`.
Bram Moolenaar77cdfd12016-03-12 12:57:59 +01001194 NOTE: Not implemented yet!
Bram Moolenaar38a55632016-02-15 22:07:32 +01001195
Bram Moolenaar77cdfd12016-03-12 12:57:59 +01001196"channel": {channel} Use an existing channel instead of creating a new one.
1197 The parts of the channel that get used for the new job
1198 will be disconnected from what they were used before.
Bram Moolenaar51628222016-12-01 23:03:28 +01001199 If the channel was still used by another job this may
Bram Moolenaar77cdfd12016-03-12 12:57:59 +01001200 cause I/O errors.
1201 Existing callbacks and other settings remain.
1202
Bram Moolenaarb6e0ec62017-07-23 22:12:20 +02001203"pty": 1 Use a pty (pseudo-tty) instead of a pipe when
1204 possible. This is most useful in combination with a
1205 terminal window, see |terminal|.
1206 {only on Unix and Unix-like systems}
1207
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001208 *job-in_io* *in_top* *in_bot* *in_name* *in_buf*
1209"in_io": "null" disconnect stdin (read from /dev/null)
1210"in_io": "pipe" stdin is connected to the channel (default)
1211"in_io": "file" stdin reads from a file
1212"in_io": "buffer" stdin reads from a buffer
1213"in_top": number when using "buffer": first line to send (default: 1)
1214"in_bot": number when using "buffer": last line to send (default: last)
1215"in_name": "/path/file" the name of the file or buffer to read from
1216"in_buf": number the number of the buffer to read from
Bram Moolenaar38a55632016-02-15 22:07:32 +01001217
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001218 *job-out_io* *out_name* *out_buf*
1219"out_io": "null" disconnect stdout (goes to /dev/null)
1220"out_io": "pipe" stdout is connected to the channel (default)
1221"out_io": "file" stdout writes to a file
Bram Moolenaar51628222016-12-01 23:03:28 +01001222"out_io": "buffer" stdout appends to a buffer (see below)
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001223"out_name": "/path/file" the name of the file or buffer to write to
1224"out_buf": number the number of the buffer to write to
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001225"out_modifiable": 0 when writing to a buffer, 'modifiable' will be off
1226 (see below)
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001227"out_msg": 0 when writing to a new buffer, the first line will be
1228 set to "Reading from channel output..."
Bram Moolenaar38a55632016-02-15 22:07:32 +01001229
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001230 *job-err_io* *err_name* *err_buf*
1231"err_io": "out" stderr messages to go to stdout
1232"err_io": "null" disconnect stderr (goes to /dev/null)
1233"err_io": "pipe" stderr is connected to the channel (default)
1234"err_io": "file" stderr writes to a file
Bram Moolenaar51628222016-12-01 23:03:28 +01001235"err_io": "buffer" stderr appends to a buffer (see below)
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001236"err_name": "/path/file" the name of the file or buffer to write to
1237"err_buf": number the number of the buffer to write to
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001238"err_modifiable": 0 when writing to a buffer, 'modifiable' will be off
1239 (see below)
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001240"err_msg": 0 when writing to a new buffer, the first line will be
1241 set to "Reading from channel error..."
Bram Moolenaar77cdfd12016-03-12 12:57:59 +01001242
Bram Moolenaar7db8f6f2016-03-29 23:12:46 +02001243"block_write": number only for testing: pretend every other write to stdin
1244 will block
1245
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001246"env": dict environment variables for the new process
1247"cwd": "/path/to/dir" current working directory for the new process;
1248 if the directory does not exist an error is given
1249
Bram Moolenaar77cdfd12016-03-12 12:57:59 +01001250
1251Writing to a buffer ~
Bram Moolenaar818078d2016-08-27 21:58:42 +02001252 *out_io-buffer*
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001253When the out_io or err_io mode is "buffer" and there is a callback, the text
Bram Moolenaar5f148ec2016-03-07 22:59:26 +01001254is appended to the buffer before invoking the callback.
1255
1256When a buffer is used both for input and output, the output lines are put
1257above the last line, since the last line is what is written to the channel
Bram Moolenaar77cdfd12016-03-12 12:57:59 +01001258input. Otherwise lines are appended below the last line.
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001259
Bram Moolenaar328da0d2016-03-04 22:22:32 +01001260When using JS or JSON mode with "buffer", only messages with zero or negative
1261ID will be added to the buffer, after decoding + encoding. Messages with a
1262positive number will be handled by a callback, commands are handled as usual.
1263
Bram Moolenaar82af8712016-06-04 20:20:29 +02001264The name of the buffer from "out_name" or "err_name" is compared the full name
1265of existing buffers, also after expanding the name for the current directory.
1266E.g., when a buffer was created with ":edit somename" and the buffer name is
1267"somename" it will use that buffer.
1268
1269If there is no matching buffer a new buffer is created. Use an empty name to
1270always create a new buffer. |ch_getbufnr()| can then be used to get the
1271buffer number.
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001272
1273For a new buffer 'buftype' is set to "nofile" and 'bufhidden' to "hide". If
1274you prefer other settings, create the buffer first and pass the buffer number.
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001275 *out_modifiable* *err_modifiable*
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001276The "out_modifiable" and "err_modifiable" options can be used to set the
1277'modifiable' option off, or write to a buffer that has 'modifiable' off. That
1278means that lines will be appended to the buffer, but the user can't easily
1279change the buffer.
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001280 *out_msg* *err_msg*
1281The "out_msg" option can be used to specify whether a new buffer will have the
1282first line set to "Reading from channel output...". The default is to add the
1283message. "err_msg" does the same for channel error.
1284
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001285When an existing buffer is to be written where 'modifiable' is off and the
1286"out_modifiable" or "err_modifiable" options is not zero, an error is given
1287and the buffer will not be written to.
1288
Bram Moolenaar187db502016-02-27 14:44:26 +01001289When the buffer written to is displayed in a window and the cursor is in the
1290first column of the last line, the cursor will be moved to the newly added
1291line and the window is scrolled up to show the cursor if needed.
1292
Bram Moolenaar063b9d12016-07-09 20:21:48 +02001293Undo is synced for every added line. NUL bytes are accepted (internally Vim
1294stores these as NL bytes).
Bram Moolenaar38a55632016-02-15 22:07:32 +01001295
Bram Moolenaar77cdfd12016-03-12 12:57:59 +01001296
1297Writing to a file ~
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001298 *E920*
Bram Moolenaar77cdfd12016-03-12 12:57:59 +01001299The file is created with permissions 600 (read-write for the user, not
1300accessible for others). Use |setfperm()| to change this.
1301
1302If the file already exists it is truncated.
1303
Bram Moolenaar38a55632016-02-15 22:07:32 +01001304==============================================================================
Bram Moolenaared997ad2019-07-21 16:42:00 +0200130513. Controlling a job *job-control*
Bram Moolenaar38a55632016-02-15 22:07:32 +01001306
1307To get the status of a job: >
1308 echo job_status(job)
1309
1310To make a job stop running: >
1311 job_stop(job)
1312
1313This is the normal way to end a job. On Unix it sends a SIGTERM to the job.
1314It is possible to use other ways to stop the job, or even send arbitrary
1315signals. E.g. to force a job to stop, "kill it": >
1316 job_stop(job, "kill")
1317
1318For more options see |job_stop()|.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001319
Bram Moolenaarf2732452018-06-03 14:47:35 +02001320==============================================================================
Bram Moolenaared997ad2019-07-21 16:42:00 +0200132114. Using a prompt buffer *prompt-buffer*
Bram Moolenaarf2732452018-06-03 14:47:35 +02001322
1323If you want to type input for the job in a Vim window you have a few options:
1324- Use a normal buffer and handle all possible commands yourself.
1325 This will be complicated, since there are so many possible commands.
1326- Use a terminal window. This works well if what you type goes directly to
1327 the job and the job output is directly displayed in the window.
1328 See |terminal-window|.
Bram Moolenaaracc22402020-06-07 21:07:18 +02001329- Use a window with a prompt buffer. This works well when entering a line for
1330 the job in Vim while displaying (possibly filtered) output from the job.
Bram Moolenaarf2732452018-06-03 14:47:35 +02001331
1332A prompt buffer is created by setting 'buftype' to "prompt". You would
1333normally only do that in a newly created buffer.
1334
1335The user can edit and enter one line of text at the very last line of the
1336buffer. When pressing Enter in the prompt line the callback set with
1337|prompt_setcallback()| is invoked. It would normally send the line to a job.
1338Another callback would receive the output from the job and display it in the
1339buffer, below the prompt (and above the next prompt).
1340
1341Only the text in the last line, after the prompt, is editable. The rest of the
1342buffer is not modifiable with Normal mode commands. It can be modified by
1343calling functions, such as |append()|. Using other commands may mess up the
1344buffer.
1345
1346After setting 'buftype' to "prompt" Vim does not automatically start Insert
1347mode, use `:startinsert` if you want to enter Insert mode, so that the user
1348can start typing a line.
1349
Bram Moolenaar077cc7a2020-09-04 16:35:35 +02001350The text of the prompt can be set with the |prompt_setprompt()| function. If
1351no prompt is set with |prompt_setprompt()|, "% " is used. You can get the
1352effective prompt text for a buffer, with |prompt_getprompt()|.
Bram Moolenaarf2732452018-06-03 14:47:35 +02001353
1354The user can go to Normal mode and navigate through the buffer. This can be
Bram Moolenaar7ff78462020-07-10 22:00:53 +02001355useful to see older output or copy text.
Bram Moolenaarf2732452018-06-03 14:47:35 +02001356
Bram Moolenaard2f3a8b2018-06-19 14:35:59 +02001357The CTRL-W key can be used to start a window command, such as CTRL-W w to
1358switch to the next window. This also works in Insert mode (use Shift-CTRL-W
1359to delete a word). When leaving the window Insert mode will be stopped. When
1360coming back to the prompt window Insert mode will be restored.
1361
Bram Moolenaarf2732452018-06-03 14:47:35 +02001362Any command that starts Insert mode, such as "a", "i", "A" and "I", will move
Bram Moolenaard2f3a8b2018-06-19 14:35:59 +02001363the cursor to the last line. "A" will move to the end of the line, "I" to the
1364start of the line.
Bram Moolenaarf2732452018-06-03 14:47:35 +02001365
Bram Moolenaaracc22402020-06-07 21:07:18 +02001366Here is an example for Unix. It starts a shell in the background and prompts
1367for the next shell command. Output from the shell is displayed above the
1368prompt. >
1369
1370 " Create a channel log so we can see what happens.
1371 call ch_logfile('logfile', 'w')
1372
Bram Moolenaar1588bc82022-03-08 21:35:07 +00001373 " Function handling a line of text that has been typed.
Bram Moolenaaracc22402020-06-07 21:07:18 +02001374 func TextEntered(text)
1375 " Send the text to a shell with Enter appended.
1376 call ch_sendraw(g:shell_job, a:text .. "\n")
1377 endfunc
Bram Moolenaarcb80aa22020-10-26 21:12:46 +01001378
Bram Moolenaar1588bc82022-03-08 21:35:07 +00001379 " Function handling output from the shell: Add it above the prompt.
Bram Moolenaaracc22402020-06-07 21:07:18 +02001380 func GotOutput(channel, msg)
Bram Moolenaarc51cf032022-02-26 12:25:45 +00001381 call append(line("$") - 1, "- " .. a:msg)
Bram Moolenaaracc22402020-06-07 21:07:18 +02001382 endfunc
1383
Bram Moolenaar1588bc82022-03-08 21:35:07 +00001384 " Function handling the shell exits: close the window.
Bram Moolenaaracc22402020-06-07 21:07:18 +02001385 func JobExit(job, status)
1386 quit!
1387 endfunc
1388
1389 " Start a shell in the background.
1390 let shell_job = job_start(["/bin/sh"], #{
1391 \ out_cb: function('GotOutput'),
1392 \ err_cb: function('GotOutput'),
1393 \ exit_cb: function('JobExit'),
1394 \ })
Bram Moolenaaracc22402020-06-07 21:07:18 +02001395
1396 new
1397 set buftype=prompt
1398 let buf = bufnr('')
1399 call prompt_setcallback(buf, function("TextEntered"))
1400 eval prompt_setprompt(buf, "shell command: ")
1401
1402 " start accepting shell commands
1403 startinsert
1404<
Bram Moolenaar1588bc82022-03-08 21:35:07 +00001405The same in |Vim9| script: >
1406
1407 vim9script
1408
1409 # Create a channel log so we can see what happens.
1410 ch_logfile('logfile', 'w')
1411
1412 var shell_job: job
1413
1414 # Function handling a line of text that has been typed.
1415 def TextEntered(text: string)
1416 # Send the text to a shell with Enter appended.
1417 ch_sendraw(shell_job, text .. "\n")
1418 enddef
1419
1420 # Function handling output from the shell: Add it above the prompt.
1421 def GotOutput(channel: channel, msg: string)
1422 append(line("$") - 1, "- " .. msg)
1423 enddef
1424
1425 # Function handling the shell exits: close the window.
1426 def JobExit(job: job, status: number)
1427 quit!
1428 enddef
1429
1430 # Start a shell in the background.
1431 shell_job = job_start(["/bin/sh"], {
1432 out_cb: GotOutput,
1433 err_cb: GotOutput,
1434 exit_cb: JobExit,
1435 })
1436
1437 new
1438 set buftype=prompt
1439 var buf = bufnr('')
1440 prompt_setcallback(buf, TextEntered)
1441 prompt_setprompt(buf, "shell command: ")
1442
1443 # start accepting shell commands
1444 startinsert
Bram Moolenaaracc22402020-06-07 21:07:18 +02001445
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01001446==============================================================================
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +0100144715. Language Server Protocol *language-server-protocol*
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01001448
1449The language server protocol specification is available at:
1450
1451 https://microsoft.github.io/language-server-protocol/specification
1452
1453Each LSP protocol message starts with a simple HTTP header followed by the
1454payload encoded in JSON-RPC format. This is described in:
1455
1456 https://www.jsonrpc.org/specification
1457
Yegappan Lakshmanane0805b82022-04-16 15:18:23 +01001458To encode and send a LSP request/notification message in a Vim |Dict| into a
1459LSP JSON-RPC message and to receive and decode a LSP JSON-RPC
1460response/notification message into a Vim |Dict|, connect to the LSP server
1461with the |channel-mode| set to "lsp".
1462
1463For messages received on a channel with |channel-mode| set to "lsp", Vim will
1464process the HTTP header and decode the JSON-RPC payload into a Vim |Dict| type
1465and call the |channel-callback| function or the specified
1466|channel-onetime-callback| function. When sending messages on a channel using
1467the |ch_evalexpr()| or |ch_sendexpr()| functions, Vim will add the HTTP header
1468and encode the Vim expression into JSON. Refer to |json_encode()| and
1469|json_decode()| for more information about how Vim encodes and decodes the
1470builtin types into JSON.
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01001471
1472To open a channel using the 'lsp' mode, set the 'mode' item in the |ch_open()|
1473{options} argument to 'lsp'. Example: >
1474
1475 let ch = ch_open(..., #{mode: 'lsp'})
1476
1477To open a channel using the 'lsp' mode with a job, set the 'in_mode' and
1478'out_mode' items in the |job_start()| {options} argument to 'lsp'. Example: >
1479
Yegappan Lakshmanane0805b82022-04-16 15:18:23 +01001480 let cmd = ['clangd', '--background-index', '--clang-tidy']
1481 let opts = {}
1482 let opts.in_mode = 'lsp'
1483 let opts.out_mode = 'lsp'
Yegappan Lakshmanan03cca292022-04-18 14:07:46 +01001484 let opts.err_mode = 'nl'
Yegappan Lakshmanane0805b82022-04-16 15:18:23 +01001485 let opts.out_cb = function('LspOutCallback')
1486 let opts.err_cb = function('LspErrCallback')
1487 let opts.exit_cb = function('LspExitCallback')
1488 let job = job_start(cmd, opts)
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01001489
Yegappan Lakshmanan03cca292022-04-18 14:07:46 +01001490Note that if a job outputs LSP messages on stdout and non-LSP messages on
1491stderr, then the channel-callback function should handle both the message
1492formats appropriately or you should use a separate callback function for
1493"out_cb" and "err_cb" to handle them as shown above.
1494
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +01001495To synchronously send a JSON-RPC request to the server, use the
1496|ch_evalexpr()| function. This function will wait and return the decoded
1497response message from the server. You can use either the |channel-timeout| or
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01001498the 'timeout' field in the {options} argument to control the response wait
Yegappan Lakshmanane0805b82022-04-16 15:18:23 +01001499time. If the request times out, then an empty |Dict| is returned. Example: >
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01001500
1501 let req = {}
1502 let req.method = 'textDocument/definition'
1503 let req.params = {}
1504 let req.params.textDocument = #{uri: 'a.c'}
1505 let req.params.position = #{line: 10, character: 3}
Yegappan Lakshmanane0805b82022-04-16 15:18:23 +01001506 let defs = ch_evalexpr(ch, req, #{timeout: 100})
1507 if defs->empty()
1508 ... <handle failure>
1509 endif
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01001510
1511Note that in the request message the 'id' field should not be specified. If it
1512is specified, then Vim will overwrite the value with an internally generated
1513identifier. Vim currently supports only a number type for the 'id' field.
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +01001514The callback function will be invoked for both a successful and a failed RPC
Yegappan Lakshmanane0805b82022-04-16 15:18:23 +01001515request.
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01001516
1517To send a JSON-RPC request to the server and asynchronously process the
Yegappan Lakshmanane0805b82022-04-16 15:18:23 +01001518response, use the |ch_sendexpr()| function and supply a callback function. If
1519the "id" field is present in the request message, then Vim will overwrite it
1520with an internally generated number. This function returns a Dict with the
1521identifier used for the message. This can be used to send cancellation
1522request to the LSP server (if needed). Example: >
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01001523
1524 let req = {}
1525 let req.method = 'textDocument/hover'
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +01001526 let req.id = 200
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01001527 let req.params = {}
1528 let req.params.textDocument = #{uri: 'a.c'}
1529 let req.params.position = #{line: 10, character: 3}
Yegappan Lakshmanane0805b82022-04-16 15:18:23 +01001530 let resp = ch_sendexpr(ch, req, #{callback: 'HoverFunc'})
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01001531
Yegappan Lakshmanane0805b82022-04-16 15:18:23 +01001532To cancel an outstanding asynchronous LSP request sent to the server using the
Bram Moolenaard592deb2022-06-17 15:42:40 +01001533|ch_sendexpr()| function, send a cancellation message to the server using the
Yegappan Lakshmanane0805b82022-04-16 15:18:23 +01001534|ch_sendexpr()| function with the ID returned by the |ch_sendexpr()| function
1535for the request. Example: >
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +01001536
1537 " send a completion request
1538 let req = {}
1539 let req.method = 'textDocument/completion'
1540 let req.params = {}
1541 let req.params.textDocument = #{uri: 'a.c'}
1542 let req.params.position = #{line: 10, character: 3}
Yegappan Lakshmanane0805b82022-04-16 15:18:23 +01001543 let reqstatus = ch_sendexpr(ch, req, #{callback: 'LspComplete'})
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +01001544 " send a cancellation notification
1545 let notif = {}
1546 let notif.method = '$/cancelRequest'
1547 let notif.id = reqstatus.id
1548 call ch_sendexpr(ch, notif)
1549
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01001550To send a JSON-RPC notification message to the server, use the |ch_sendexpr()|
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +01001551function. As the server will not send a response message to the notification,
1552don't specify the "callback" item. Example: >
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01001553
1554 call ch_sendexpr(ch, #{method: 'initialized'})
1555
1556To respond to a JSON-RPC request message from the server, use the
1557|ch_sendexpr()| function. In the response message, copy the 'id' field value
1558from the server request message. Example: >
1559
1560 let resp = {}
1561 let resp.id = req.id
1562 let resp.result = 1
1563 call ch_sendexpr(ch, resp)
1564
1565The JSON-RPC notification messages from the server are delivered through the
1566|channel-callback| function.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001567
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +01001568Depending on the use case, you can use the ch_evalexpr(), ch_sendexpr() and
1569ch_sendraw() functions on the same channel.
1570
1571A LSP request message has the following format (expressed as a Vim Dict). The
1572"params" field is optional: >
1573
1574 {
1575 "jsonrpc": "2.0",
1576 "id": <number>,
1577 "method": <string>,
1578 "params": <list|dict>
1579 }
1580
Bram Moolenaard592deb2022-06-17 15:42:40 +01001581A LSP response message has the following format (expressed as a Vim Dict). The
Yegappan Lakshmanan3b470ae2022-04-16 10:41:27 +01001582"result" and "error" fields are optional: >
1583
1584 {
1585 "jsonrpc": "2.0",
1586 "id": <number>,
1587 "result": <vim type>
1588 "error": <dict>
1589 }
1590
1591A LSP notification message has the following format (expressed as a Vim Dict).
1592The "params" field is optional: >
1593
1594 {
1595 "jsonrpc": "2.0",
1596 "method": <string>,
1597 "params": <list|dict>
1598 }
1599
Bram Moolenaar8a3b8052022-06-26 12:21:15 +01001600<
Bram Moolenaar91f84f62018-07-29 15:07:52 +02001601 vim:tw=78:ts=8:noet:ft=help:norl: