blob: a306abb4888c24ed4834dc26a2c5bf34566e1e15 [file] [log] [blame]
Bram Moolenaarcbaff5e2022-04-08 17:45:08 +01001*channel.txt* For Vim version 8.2. Last change: 2022 Apr 05
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()|
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +010056LSP Language Server Protocol encoding |language-server-protocol|
Bram Moolenaar38a55632016-02-15 22:07:32 +010057
58Common combination are:
59- Using a job connected through pipes in NL mode. E.g., to run a style
60 checker and receive errors and warnings.
Bram Moolenaar7dda86f2018-04-20 22:36:41 +020061- Using a daemon, connecting over a socket in JSON mode. E.g. to lookup
Bram Moolenaar09521312016-08-12 22:54:35 +020062 cross-references in a database.
Bram Moolenaar38a55632016-02-15 22:07:32 +010063
64==============================================================================
Bram Moolenaar26852122016-05-24 20:02:38 +0200652. Channel demo *channel-demo* *demoserver.py*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010066
67This requires Python. The demo program can be found in
68$VIMRUNTIME/tools/demoserver.py
69Run it in one terminal. We will call this T1.
70
71Run Vim in another terminal. Connect to the demo server with: >
Bram Moolenaar38a55632016-02-15 22:07:32 +010072 let channel = ch_open('localhost:8765')
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010073
74In T1 you should see:
75 === socket opened === ~
76
77You can now send a message to the server: >
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010078 echo ch_evalexpr(channel, 'hello!')
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010079
80The message is received in T1 and a response is sent back to Vim.
81You can see the raw messages in T1. What Vim sends is:
82 [1,"hello!"] ~
83And the response is:
84 [1,"got it"] ~
85The number will increase every time you send a message.
86
87The server can send a command to Vim. Type this on T1 (literally, including
Bram Moolenaarfb1f6262016-01-31 20:24:32 +010088the quotes):
89 ["ex","echo 'hi there'"] ~
90And you should see the message in Vim. You can move the cursor a word forward:
91 ["normal","w"] ~
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010092
93To handle asynchronous communication a callback needs to be used: >
Bram Moolenaar38a55632016-02-15 22:07:32 +010094 func MyHandler(channel, msg)
Bram Moolenaarc51cf032022-02-26 12:25:45 +000095 echo "from the handler: " .. a:msg
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010096 endfunc
Bram Moolenaar02e83b42016-02-21 20:10:26 +010097 call ch_sendexpr(channel, 'hello!', {'callback': "MyHandler"})
Bram Moolenaar38a55632016-02-15 22:07:32 +010098Vim will not wait for a response. Now the server can send the response later
99and MyHandler will be invoked.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100100
101Instead of giving a callback with every send call, it can also be specified
102when opening the channel: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100103 call ch_close(channel)
104 let channel = ch_open('localhost:8765', {'callback': "MyHandler"})
Bram Moolenaar47003982021-12-05 21:54:04 +0000105 call ch_sendexpr(channel, 'hello channel!')
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100106
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100107When trying out channels it's useful to see what is going on. You can tell
108Vim to write lines in log file: >
109 call ch_logfile('channellog', 'w')
110See |ch_logfile()|.
111
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100112==============================================================================
Bram Moolenaar38a55632016-02-15 22:07:32 +01001133. Opening a channel *channel-open*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100114
Bram Moolenaar681baaf2016-02-04 20:57:07 +0100115To open a channel: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100116 let channel = ch_open({address} [, {options}])
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100117 if ch_status(channel) == "open"
118 " use the channel
Bram Moolenaar38a55632016-02-15 22:07:32 +0100119
120Use |ch_status()| to see if the channel could be opened.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100121
LemonBoycc766a82022-04-04 15:46:58 +0100122 *channel-address*
123{address} can be a domain name or an IP address, followed by a port number, or
124a Unix-domain socket path prefixed by "unix:". E.g. >
125 www.example.com:80 " domain + port
126 127.0.0.1:1234 " IPv4 + port
127 [2001:db8::1]:8765 " IPv6 + port
128 unix:/tmp/my-socket " Unix-domain socket path
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200129
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100130{options} is a dictionary with optional entries: *channel-open-options*
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100131
132"mode" can be: *channel-mode*
133 "json" - Use JSON, see below; most convenient way. Default.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100134 "js" - Use JS (JavaScript) encoding, more efficient than JSON.
Bram Moolenaar38a55632016-02-15 22:07:32 +0100135 "nl" - Use messages that end in a NL character
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100136 "raw" - Use raw messages
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +0100137 "lsp" - Use language server protocol encoding
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100138 *channel-callback* *E921*
Bram Moolenaar38a55632016-02-15 22:07:32 +0100139"callback" A function that is called when a message is received that is
Bram Moolenaar47003982021-12-05 21:54:04 +0000140 not handled otherwise (e.g. a JSON message with ID zero). It
141 gets two arguments: the channel and the received message.
142 Example: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100143 func Handle(channel, msg)
Bram Moolenaarc51cf032022-02-26 12:25:45 +0000144 echo 'Received: ' .. a:msg
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100145 endfunc
Bram Moolenaar38a55632016-02-15 22:07:32 +0100146 let channel = ch_open("localhost:8765", {"callback": "Handle"})
147<
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +0100148 When "mode" is "json" or "js" or "lsp" the "msg" argument is
149 the body of the received message, converted to Vim types.
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100150 When "mode" is "nl" the "msg" argument is one message,
151 excluding the NL.
152 When "mode" is "raw" the "msg" argument is the whole message
153 as a string.
Bram Moolenaare18c0b32016-03-20 21:08:34 +0100154
155 For all callbacks: Use |function()| to bind it to arguments
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100156 and/or a Dictionary. Or use the form "dict.function" to bind
157 the Dictionary.
Bram Moolenaar06d2d382016-05-20 17:24:11 +0200158
159 Callbacks are only called at a "safe" moment, usually when Vim
160 is waiting for the user to type a character. Vim does not use
161 multi-threading.
162
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100163 *close_cb*
164"close_cb" A function that is called when the channel gets closed, other
Bram Moolenaar38a55632016-02-15 22:07:32 +0100165 than by calling ch_close(). It should be defined like this: >
166 func MyCloseHandler(channel)
Bram Moolenaar06d2d382016-05-20 17:24:11 +0200167< Vim will invoke callbacks that handle data before invoking
168 close_cb, thus when this function is called no more data will
Bram Moolenaar68e65602019-05-26 21:33:31 +0200169 be passed to the callbacks. However, if a callback causes Vim
170 to check for messages, the close_cb may be invoked while still
171 in the callback. The plugin must handle this somehow, it can
172 be useful to know that no more data is coming.
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +0100173 If it is not known if there is a message to be read, use a
174 try/catch block: >
175 try
176 let msg = ch_readraw(a:channel)
177 catch
178 let msg = 'no message'
179 endtry
180 try
181 let err = ch_readraw(a:channel, #{part: 'err'})
182 catch
183 let err = 'no error'
184 endtry
185< *channel-drop*
Bram Moolenaar958dc692016-12-01 15:34:12 +0100186"drop" Specifies when to drop messages:
187 "auto" When there is no callback to handle a message.
188 The "close_cb" is also considered for this.
189 "never" All messages will be kept.
190
Bram Moolenaar0b146882018-09-06 16:27:24 +0200191 *channel-noblock*
192"noblock" Same effect as |job-noblock|. Only matters for writing.
193
Bram Moolenaar06d2d382016-05-20 17:24:11 +0200194 *waittime*
Bram Moolenaar38a55632016-02-15 22:07:32 +0100195"waittime" The time to wait for the connection to be made in
Bram Moolenaarf3913272016-02-25 00:00:01 +0100196 milliseconds. A negative number waits forever.
197
198 The default is zero, don't wait, which is useful if a local
199 server is supposed to be running already. On Unix Vim
200 actually uses a 1 msec timeout, that is required on many
201 systems. Use a larger value for a remote server, e.g. 10
202 msec at least.
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100203 *channel-timeout*
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100204"timeout" The time to wait for a request when blocking, E.g. when using
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100205 ch_evalexpr(). In milliseconds. The default is 2000 (2
Bram Moolenaar38a55632016-02-15 22:07:32 +0100206 seconds).
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100207
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100208When "mode" is "json" or "js" the "callback" is optional. When omitted it is
209only possible to receive a message after sending one.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100210
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100211To change the channel options after opening it use |ch_setoptions()|. The
212arguments are similar to what is passed to |ch_open()|, but "waittime" cannot
213be given, since that only applies to opening the channel.
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100214
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100215For example, the handler can be added or changed: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100216 call ch_setoptions(channel, {'callback': callback})
217When "callback" is empty (zero or an empty string) the handler is removed.
218
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100219After a callback has been invoked Vim will update the screen and put the
220cursor back where it belongs. Thus the callback should not need to do
221`:redraw`.
222
Bram Moolenaar38a55632016-02-15 22:07:32 +0100223The timeout can be changed: >
224 call ch_setoptions(channel, {'timeout': msec})
225<
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100226 *channel-close* *E906*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100227Once done with the channel, disconnect it like this: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100228 call ch_close(channel)
229When a socket is used this will close the socket for both directions. When
230pipes are used (stdin/stdout/stderr) they are all closed. This might not be
231what you want! Stopping the job with job_stop() might be better.
Bram Moolenaar187db502016-02-27 14:44:26 +0100232All readahead is discarded, callbacks will no longer be invoked.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100233
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100234Note that a channel is closed in three stages:
235 - The I/O ends, log message: "Closing channel". There can still be queued
236 messages to read or callbacks to invoke.
237 - The readahead is cleared, log message: "Clearing channel". Some variables
238 may still reference the channel.
239 - The channel is freed, log message: "Freeing channel".
240
Bram Moolenaarcbebd482016-02-07 23:02:56 +0100241When the channel can't be opened you will get an error message. There is a
242difference between MS-Windows and Unix: On Unix when the port doesn't exist
243ch_open() fails quickly. On MS-Windows "waittime" applies.
Bram Moolenaaraa3b15d2016-04-21 08:53:19 +0200244*E898* *E901* *E902*
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100245
246If there is an error reading or writing a channel it will be closed.
Bram Moolenaar664f3cf2019-12-07 16:03:51 +0100247*E630* *E631*
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100248
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100249==============================================================================
Bram Moolenaar38a55632016-02-15 22:07:32 +01002504. Using a JSON or JS channel *channel-use*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100251
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100252If mode is JSON then a message can be sent synchronously like this: >
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100253 let response = ch_evalexpr(channel, {expr})
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100254This awaits a response from the other side.
255
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100256When mode is JS this works the same, except that the messages use
Bram Moolenaar38a55632016-02-15 22:07:32 +0100257JavaScript encoding. See |js_encode()| for the difference.
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100258
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100259To send a message, without handling a response or letting the channel callback
260handle the response: >
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100261 call ch_sendexpr(channel, {expr})
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100262
263To send a message and letting the response handled by a specific function,
264asynchronously: >
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100265 call ch_sendexpr(channel, {expr}, {'callback': Handler})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100266
267Vim will match the response with the request using the message ID. Once the
268response is received the callback will be invoked. Further responses with the
269same ID will be ignored. If your server sends back multiple responses you
270need to send them with ID zero, they will be passed to the channel callback.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100271
272The {expr} is converted to JSON and wrapped in an array. An example of the
273message that the receiver will get when {expr} is the string "hello":
274 [12,"hello"] ~
275
276The format of the JSON sent is:
277 [{number},{expr}]
278
279In which {number} is different every time. It must be used in the response
280(if any):
281
282 [{number},{response}]
283
284This way Vim knows which sent message matches with which received message and
285can call the right handler. Also when the messages arrive out of order.
286
Bram Moolenaarf1f07922016-08-26 17:58:53 +0200287A newline character is terminating the JSON text. This can be used to
288separate the read text. For example, in Python:
289 splitidx = read_text.find('\n')
290 message = read_text[:splitidx]
291 rest = read_text[splitidx + 1:]
292
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100293The sender must always send valid JSON to Vim. Vim can check for the end of
294the message by parsing the JSON. It will only accept the message if the end
Bram Moolenaarf1f07922016-08-26 17:58:53 +0200295was received. A newline after the message is optional.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100296
297When the process wants to send a message to Vim without first receiving a
298message, it must use the number zero:
299 [0,{response}]
300
301Then channel handler will then get {response} converted to Vim types. If the
302channel does not have a handler the message is dropped.
303
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100304It is also possible to use ch_sendraw() and ch_evalraw() on a JSON or JS
305channel. The caller is then completely responsible for correct encoding and
306decoding.
Bram Moolenaarcbebd482016-02-07 23:02:56 +0100307
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100308==============================================================================
Bram Moolenaar38a55632016-02-15 22:07:32 +01003095. Channel commands *channel-commands*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100310
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100311With a JSON channel the process can send commands to Vim that will be
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100312handled by Vim internally, it does not require a handler for the channel.
313
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100314Possible commands are: *E903* *E904* *E905*
Bram Moolenaar220adb12016-09-12 12:17:26 +0200315 ["redraw", {forced}]
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100316 ["ex", {Ex command}]
317 ["normal", {Normal mode command}]
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100318 ["expr", {expression}, {number}]
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100319 ["expr", {expression}]
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100320 ["call", {func name}, {argument list}, {number}]
321 ["call", {func name}, {argument list}]
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100322
323With all of these: Be careful what these commands do! You can easily
324interfere with what the user is doing. To avoid trouble use |mode()| to check
325that the editor is in the expected state. E.g., to send keys that must be
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100326inserted as text, not executed as a command:
327 ["ex","if mode() == 'i' | call feedkeys('ClassName') | endif"] ~
328
329Errors in these commands are normally not reported to avoid them messing up
330the display. If you do want to see them, set the 'verbose' option to 3 or
331higher.
332
333
334Command "redraw" ~
335
Bram Moolenaar63b74a82019-03-24 15:09:13 +0100336The other commands do not explicitly update the screen, so that you can send a
337sequence of commands without the cursor moving around. A redraw can happen as
338a side effect of some commands. You must end with the "redraw" command to
339show any changed text and show the cursor where it belongs.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100340
341The argument is normally an empty string:
342 ["redraw", ""] ~
343To first clear the screen pass "force":
344 ["redraw", "force"] ~
345
346
347Command "ex" ~
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100348
349The "ex" command is executed as any Ex command. There is no response for
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100350completion or error. You could use functions in an |autoload| script:
351 ["ex","call myscript#MyFunc(arg)"]
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100352
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100353You can also use "call |feedkeys()|" to insert any key sequence.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100354
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100355When there is an error a message is written to the channel log, if it exists,
356and v:errmsg is set to the error.
357
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100358
359Command "normal" ~
360
Bram Moolenaar681baaf2016-02-04 20:57:07 +0100361The "normal" command is executed like with ":normal!", commands are not
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100362mapped. Example to open the folds under the cursor:
363 ["normal" "zO"]
364
365
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100366Command "expr" with response ~
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100367
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100368The "expr" command can be used to get the result of an expression. For
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100369example, to get the number of lines in the current buffer:
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100370 ["expr","line('$')", -2] ~
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100371
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100372It will send back the result of the expression:
Bram Moolenaare0fa3742016-02-20 15:47:01 +0100373 [-2, "last line"] ~
374The format is:
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100375 [{number}, {result}]
Bram Moolenaar187db502016-02-27 14:44:26 +0100376
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100377Here {number} is the same as what was in the request. Use a negative number
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100378to avoid confusion with message that Vim sends. Use a different number on
379every request to be able to match the request with the response.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100380
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100381{result} is the result of the evaluation and is JSON encoded. If the
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100382evaluation fails or the result can't be encoded in JSON it is the string
383"ERROR".
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100384
385
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100386Command "expr" without a response ~
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100387
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100388This command is similar to "expr" above, but does not send back any response.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100389Example:
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100390 ["expr","setline('$', ['one', 'two', 'three'])"] ~
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100391There is no third argument in the request.
392
393
394Command "call" ~
395
396This is similar to "expr", but instead of passing the whole expression as a
397string this passes the name of a function and a list of arguments. This
398avoids the conversion of the arguments to a string and escaping and
399concatenating them. Example:
400 ["call", "line", ["$"], -2] ~
401
402Leave out the fourth argument if no response is to be sent:
403 ["call", "setline", ["$", ["one", "two", "three"]]] ~
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100404
405==============================================================================
Bram Moolenaar38a55632016-02-15 22:07:32 +01004066. Using a RAW or NL channel *channel-raw*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100407
Bram Moolenaarc0514bf2016-11-17 14:50:09 +0100408If mode is RAW or NL then a message can be sent like this: >
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100409 let response = ch_evalraw(channel, {string})
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100410
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100411The {string} is sent as-is. The response will be what can be read from the
412channel right away. Since Vim doesn't know how to recognize the end of the
Bram Moolenaar38a55632016-02-15 22:07:32 +0100413message you need to take care of it yourself. The timeout applies for reading
414the first byte, after that it will not wait for anything more.
415
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100416If mode is "nl" you can send a message in a similar way. You are expected
Bram Moolenaar38a55632016-02-15 22:07:32 +0100417to put in the NL after each message. Thus you can also send several messages
418ending in a NL at once. The response will be the text up to and including the
419first NL. This can also be just the NL for an empty response.
420If no NL was read before the channel timeout an empty string is returned.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100421
422To send a message, without expecting a response: >
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100423 call ch_sendraw(channel, {string})
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100424The process can send back a response, the channel handler will be called with
425it.
426
427To send a message and letting the response handled by a specific function,
428asynchronously: >
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100429 call ch_sendraw(channel, {string}, {'callback': 'MyHandler'})
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100430
Bram Moolenaar38a55632016-02-15 22:07:32 +0100431This {string} can also be JSON, use |json_encode()| to create it and
432|json_decode()| to handle a received JSON message.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100433
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100434It is not possible to use |ch_evalexpr()| or |ch_sendexpr()| on a raw channel.
Bram Moolenaarcbebd482016-02-07 23:02:56 +0100435
Bram Moolenaar818078d2016-08-27 21:58:42 +0200436A String in Vim cannot contain NUL bytes. To send or receive NUL bytes read
437or write from a buffer. See |in_io-buffer| and |out_io-buffer|.
438
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100439==============================================================================
Bram Moolenaar38a55632016-02-15 22:07:32 +01004407. More channel functions *channel-more*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100441
Bram Moolenaar38a55632016-02-15 22:07:32 +0100442To obtain the status of a channel: ch_status(channel). The possible results
443are:
444 "fail" Failed to open the channel.
445 "open" The channel can be used.
Bram Moolenaar06481422016-04-30 15:13:38 +0200446 "buffered" The channel was closed but there is data to read.
Bram Moolenaar38a55632016-02-15 22:07:32 +0100447 "closed" The channel was closed.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100448
Bram Moolenaar187db502016-02-27 14:44:26 +0100449To obtain the job associated with a channel: ch_getjob(channel)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100450
Bram Moolenaar38a55632016-02-15 22:07:32 +0100451To read one message from a channel: >
452 let output = ch_read(channel)
453This uses the channel timeout. To read without a timeout, just get any
454message that is available: >
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100455 let output = ch_read(channel, {'timeout': 0})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100456When no message was available then the result is v:none for a JSON or JS mode
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100457channels, an empty string for a RAW or NL channel. You can use |ch_canread()|
458to check if there is something to read.
459
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200460Note that when there is no callback, messages are dropped. To avoid that add
461a close callback to the channel.
Bram Moolenaar38a55632016-02-15 22:07:32 +0100462
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +0100463To read all normal output from a RAW channel that is available: >
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100464 let output = ch_readraw(channel)
Bram Moolenaar38a55632016-02-15 22:07:32 +0100465To read the error output: >
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100466 let output = ch_readraw(channel, {"part": "err"})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100467
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100468ch_read() and ch_readraw() use the channel timeout. When there is nothing to
469read within that time an empty string is returned. To specify a different
470timeout in msec use the "timeout" option:
471 {"timeout": 123} ~
472To read from the error output use the "part" option:
473 {"part": "err"} ~
474To read a message with a specific ID, on a JS or JSON channel:
475 {"id": 99} ~
476When no ID is specified or the ID is -1, the first message is returned. This
477overrules any callback waiting for this message.
478
479For a RAW channel this returns whatever is available, since Vim does not know
480where a message ends.
481For a NL channel this returns one message.
482For a JS or JSON channel this returns one decoded message.
483This includes any sequence number.
484
Bram Moolenaar38a55632016-02-15 22:07:32 +0100485==============================================================================
Bram Moolenaar54775062019-07-31 21:07:14 +02004868. Channel functions details *channel-functions-details*
Bram Moolenaared997ad2019-07-21 16:42:00 +0200487
488ch_canread({handle}) *ch_canread()*
489 Return non-zero when there is something to read from {handle}.
490 {handle} can be a Channel or a Job that has a Channel.
491
492 This is useful to read from a channel at a convenient time,
493 e.g. from a timer.
494
495 Note that messages are dropped when the channel does not have
496 a callback. Add a close callback to avoid that.
497
Bram Moolenaar570497a2019-08-22 22:55:13 +0200498 Can also be used as a |method|: >
499 GetChannel()->ch_canread()
Bram Moolenaared997ad2019-07-21 16:42:00 +0200500
501ch_close({handle}) *ch_close()*
502 Close {handle}. See |channel-close|.
503 {handle} can be a Channel or a Job that has a Channel.
504 A close callback is not invoked.
505
Bram Moolenaar570497a2019-08-22 22:55:13 +0200506 Can also be used as a |method|: >
507 GetChannel()->ch_close()
Bram Moolenaared997ad2019-07-21 16:42:00 +0200508
509ch_close_in({handle}) *ch_close_in()*
510 Close the "in" part of {handle}. See |channel-close-in|.
511 {handle} can be a Channel or a Job that has a Channel.
512 A close callback is not invoked.
513
Bram Moolenaar570497a2019-08-22 22:55:13 +0200514 Can also be used as a |method|: >
515 GetChannel()->ch_close_in()
516
Bram Moolenaared997ad2019-07-21 16:42:00 +0200517
518ch_evalexpr({handle}, {expr} [, {options}]) *ch_evalexpr()*
519 Send {expr} over {handle}. The {expr} is encoded
520 according to the type of channel. The function cannot be used
521 with a raw channel. See |channel-use|.
522 {handle} can be a Channel or a Job that has a Channel.
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +0100523 When using the "lsp" channel mode, {expr} must be a |Dict|.
Bram Moolenaared997ad2019-07-21 16:42:00 +0200524 *E917*
525 {options} must be a Dictionary. It must not have a "callback"
526 entry. It can have a "timeout" entry to specify the timeout
527 for this specific request.
528
529 ch_evalexpr() waits for a response and returns the decoded
530 expression. When there is an error or timeout it returns an
531 empty string.
532
Bram Moolenaar8fe10002019-09-11 22:56:44 +0200533 Note that while waiting for the response, Vim handles other
534 messages. You need to make sure this doesn't cause trouble.
535
Bram Moolenaar570497a2019-08-22 22:55:13 +0200536 Can also be used as a |method|: >
537 GetChannel()->ch_evalexpr(expr)
538
Bram Moolenaared997ad2019-07-21 16:42:00 +0200539
540ch_evalraw({handle}, {string} [, {options}]) *ch_evalraw()*
541 Send {string} over {handle}.
542 {handle} can be a Channel or a Job that has a Channel.
543
544 Works like |ch_evalexpr()|, but does not encode the request or
545 decode the response. The caller is responsible for the
546 correct contents. Also does not add a newline for a channel
547 in NL mode, the caller must do that. The NL in the response
548 is removed.
549 Note that Vim does not know when the text received on a raw
550 channel is complete, it may only return the first part and you
551 need to use |ch_readraw()| to fetch the rest.
552 See |channel-use|.
553
Bram Moolenaar570497a2019-08-22 22:55:13 +0200554 Can also be used as a |method|: >
555 GetChannel()->ch_evalraw(rawstring)
Bram Moolenaared997ad2019-07-21 16:42:00 +0200556
557ch_getbufnr({handle}, {what}) *ch_getbufnr()*
Bram Moolenaar6aa57292021-08-14 21:25:52 +0200558 Get the buffer number that {handle} is using for String {what}.
Bram Moolenaared997ad2019-07-21 16:42:00 +0200559 {handle} can be a Channel or a Job that has a Channel.
560 {what} can be "err" for stderr, "out" for stdout or empty for
561 socket output.
562 Returns -1 when there is no buffer.
563
Bram Moolenaar570497a2019-08-22 22:55:13 +0200564 Can also be used as a |method|: >
565 GetChannel()->ch_getbufnr(what)
Bram Moolenaared997ad2019-07-21 16:42:00 +0200566
567ch_getjob({channel}) *ch_getjob()*
568 Get the Job associated with {channel}.
569 If there is no job calling |job_status()| on the returned Job
570 will result in "fail".
571
Bram Moolenaar570497a2019-08-22 22:55:13 +0200572 Can also be used as a |method|: >
573 GetChannel()->ch_getjob()
574
Bram Moolenaared997ad2019-07-21 16:42:00 +0200575
576ch_info({handle}) *ch_info()*
577 Returns a Dictionary with information about {handle}. The
578 items are:
579 "id" number of the channel
580 "status" "open", "buffered" or "closed", like
581 ch_status()
582 When opened with ch_open():
583 "hostname" the hostname of the address
584 "port" the port of the address
LemonBoycc766a82022-04-04 15:46:58 +0100585 "path" the path of the Unix-domain socket
Bram Moolenaared997ad2019-07-21 16:42:00 +0200586 "sock_status" "open" or "closed"
587 "sock_mode" "NL", "RAW", "JSON" or "JS"
588 "sock_io" "socket"
589 "sock_timeout" timeout in msec
LemonBoycc766a82022-04-04 15:46:58 +0100590
Bram Moolenaarcbaff5e2022-04-08 17:45:08 +0100591 Note that "path" is only present for Unix-domain sockets, for
LemonBoycc766a82022-04-04 15:46:58 +0100592 regular ones "hostname" and "port" are present instead.
593
Bram Moolenaared997ad2019-07-21 16:42:00 +0200594 When opened with job_start():
595 "out_status" "open", "buffered" or "closed"
596 "out_mode" "NL", "RAW", "JSON" or "JS"
597 "out_io" "null", "pipe", "file" or "buffer"
598 "out_timeout" timeout in msec
599 "err_status" "open", "buffered" or "closed"
600 "err_mode" "NL", "RAW", "JSON" or "JS"
601 "err_io" "out", "null", "pipe", "file" or "buffer"
602 "err_timeout" timeout in msec
603 "in_status" "open" or "closed"
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +0100604 "in_mode" "NL", "RAW", "JSON", "JS" or "LSP"
Bram Moolenaared997ad2019-07-21 16:42:00 +0200605 "in_io" "null", "pipe", "file" or "buffer"
606 "in_timeout" timeout in msec
607
Bram Moolenaar570497a2019-08-22 22:55:13 +0200608 Can also be used as a |method|: >
609 GetChannel()->ch_info()
610
Bram Moolenaared997ad2019-07-21 16:42:00 +0200611
612ch_log({msg} [, {handle}]) *ch_log()*
Bram Moolenaar6aa57292021-08-14 21:25:52 +0200613 Write String {msg} in the channel log file, if it was opened
614 with |ch_logfile()|.
Bram Moolenaared997ad2019-07-21 16:42:00 +0200615 When {handle} is passed the channel number is used for the
616 message.
617 {handle} can be a Channel or a Job that has a Channel. The
618 Channel must be open for the channel number to be used.
619
Bram Moolenaar570497a2019-08-22 22:55:13 +0200620 Can also be used as a |method|: >
621 'did something'->ch_log()
622
Bram Moolenaared997ad2019-07-21 16:42:00 +0200623
624ch_logfile({fname} [, {mode}]) *ch_logfile()*
625 Start logging channel activity to {fname}.
626 When {fname} is an empty string: stop logging.
627
628 When {mode} is omitted or "a" append to the file.
629 When {mode} is "w" start with an empty file.
630
631 Use |ch_log()| to write log messages. The file is flushed
632 after every message, on Unix you can use "tail -f" to see what
633 is going on in real time.
634
Bram Moolenaar077cc7a2020-09-04 16:35:35 +0200635 To enable the log very early, to see what is received from a
636 terminal during startup, use |--cmd|: >
637 vim --cmd "call ch_logfile('logfile', 'w')"
638<
Bram Moolenaared997ad2019-07-21 16:42:00 +0200639 This function is not available in the |sandbox|.
640 NOTE: the channel communication is stored in the file, be
641 aware that this may contain confidential and privacy sensitive
642 information, e.g. a password you type in a terminal window.
643
Bram Moolenaar570497a2019-08-22 22:55:13 +0200644 Can also be used as a |method|: >
645 'logfile'->ch_logfile('w')
646
Bram Moolenaared997ad2019-07-21 16:42:00 +0200647
648ch_open({address} [, {options}]) *ch_open()*
649 Open a channel to {address}. See |channel|.
650 Returns a Channel. Use |ch_status()| to check for failure.
651
LemonBoycc766a82022-04-04 15:46:58 +0100652 {address} is a String, see |channel-address| for the possible
653 accepted forms.
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200654
Bram Moolenaared997ad2019-07-21 16:42:00 +0200655 If {options} is given it must be a |Dictionary|.
656 See |channel-open-options|.
657
Bram Moolenaar570497a2019-08-22 22:55:13 +0200658 Can also be used as a |method|: >
659 GetAddress()->ch_open()
660
Bram Moolenaared997ad2019-07-21 16:42:00 +0200661
662ch_read({handle} [, {options}]) *ch_read()*
663 Read from {handle} and return the received message.
664 {handle} can be a Channel or a Job that has a Channel.
665 For a NL channel this waits for a NL to arrive, except when
666 there is nothing more to read (channel was closed).
667 See |channel-more|.
668
Bram Moolenaar570497a2019-08-22 22:55:13 +0200669 Can also be used as a |method|: >
670 GetChannel()->ch_read()
671
Bram Moolenaared997ad2019-07-21 16:42:00 +0200672
673ch_readblob({handle} [, {options}]) *ch_readblob()*
674 Like ch_read() but reads binary data and returns a |Blob|.
675 See |channel-more|.
676
Bram Moolenaar570497a2019-08-22 22:55:13 +0200677 Can also be used as a |method|: >
678 GetChannel()->ch_readblob()
679
Bram Moolenaared997ad2019-07-21 16:42:00 +0200680
681ch_readraw({handle} [, {options}]) *ch_readraw()*
682 Like ch_read() but for a JS and JSON channel does not decode
683 the message. For a NL channel it does not block waiting for
684 the NL to arrive, but otherwise works like ch_read().
685 See |channel-more|.
686
Bram Moolenaar570497a2019-08-22 22:55:13 +0200687 Can also be used as a |method|: >
688 GetChannel()->ch_readraw()
689
Bram Moolenaared997ad2019-07-21 16:42:00 +0200690
691ch_sendexpr({handle}, {expr} [, {options}]) *ch_sendexpr()*
692 Send {expr} over {handle}. The {expr} is encoded
693 according to the type of channel. The function cannot be used
694 with a raw channel.
695 See |channel-use|. *E912*
696 {handle} can be a Channel or a Job that has a Channel.
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +0100697 When using the "lsp" channel mode, {expr} must be a |Dict|.
Bram Moolenaared997ad2019-07-21 16:42:00 +0200698
Bram Moolenaar570497a2019-08-22 22:55:13 +0200699 Can also be used as a |method|: >
700 GetChannel()->ch_sendexpr(expr)
701
Bram Moolenaared997ad2019-07-21 16:42:00 +0200702
703ch_sendraw({handle}, {expr} [, {options}]) *ch_sendraw()*
704 Send |String| or |Blob| {expr} over {handle}.
705 Works like |ch_sendexpr()|, but does not encode the request or
706 decode the response. The caller is responsible for the
707 correct contents. Also does not add a newline for a channel
708 in NL mode, the caller must do that. The NL in the response
709 is removed.
710 See |channel-use|.
711
Bram Moolenaar570497a2019-08-22 22:55:13 +0200712 Can also be used as a |method|: >
713 GetChannel()->ch_sendraw(rawexpr)
714
Bram Moolenaared997ad2019-07-21 16:42:00 +0200715
716ch_setoptions({handle}, {options}) *ch_setoptions()*
717 Set options on {handle}:
718 "callback" the channel callback
719 "timeout" default read timeout in msec
720 "mode" mode for the whole channel
721 See |ch_open()| for more explanation.
722 {handle} can be a Channel or a Job that has a Channel.
723
724 Note that changing the mode may cause queued messages to be
725 lost.
726
727 These options cannot be changed:
728 "waittime" only applies to |ch_open()|
729
Bram Moolenaar570497a2019-08-22 22:55:13 +0200730 Can also be used as a |method|: >
731 GetChannel()->ch_setoptions(options)
732
Bram Moolenaared997ad2019-07-21 16:42:00 +0200733
734ch_status({handle} [, {options}]) *ch_status()*
735 Return the status of {handle}:
736 "fail" failed to open the channel
737 "open" channel can be used
738 "buffered" channel can be read, not written to
739 "closed" channel can not be used
740 {handle} can be a Channel or a Job that has a Channel.
741 "buffered" is used when the channel was closed but there is
742 still data that can be obtained with |ch_read()|.
743
744 If {options} is given it can contain a "part" entry to specify
745 the part of the channel to return the status for: "out" or
746 "err". For example, to get the error status: >
747 ch_status(job, {"part": "err"})
748<
Bram Moolenaar570497a2019-08-22 22:55:13 +0200749 Can also be used as a |method|: >
750 GetChannel()->ch_status()
Bram Moolenaared997ad2019-07-21 16:42:00 +0200751
752==============================================================================
7539. Starting a job with a channel *job-start* *job*
Bram Moolenaar38a55632016-02-15 22:07:32 +0100754
755To start a job and open a channel for stdin/stdout/stderr: >
756 let job = job_start(command, {options})
757
758You can get the channel with: >
759 let channel = job_getchannel(job)
760
761The channel will use NL mode. If you want another mode it's best to specify
762this in {options}. When changing the mode later some text may have already
763been received and not parsed correctly.
764
765If the command produces a line of output that you want to deal with, specify
766a handler for stdout: >
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100767 let job = job_start(command, {"out_cb": "MyHandler"})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100768The function will be called with the channel and a message. You would define
769it like this: >
770 func MyHandler(channel, msg)
771
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100772Without the handler you need to read the output with |ch_read()| or
Bram Moolenaar06481422016-04-30 15:13:38 +0200773|ch_readraw()|. You can do this in the close callback, see |read-in-close-cb|.
Bram Moolenaar38a55632016-02-15 22:07:32 +0100774
Bram Moolenaar1ccd8ff2017-08-11 19:50:37 +0200775Note that if the job exits before you read the output, the output may be lost.
776This depends on the system (on Unix this happens because closing the write end
777of a pipe causes the read end to get EOF). To avoid this make the job sleep
778for a short while before it exits.
779
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100780The handler defined for "out_cb" will not receive stderr. If you want to
781handle that separately, add an "err_cb" handler: >
782 let job = job_start(command, {"out_cb": "MyHandler",
783 \ "err_cb": "ErrHandler"})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100784
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100785If you want to handle both stderr and stdout with one handler use the
786"callback" option: >
Bram Moolenaar664f3cf2019-12-07 16:03:51 +0100787 let job = job_start(command, {"callback": "MyHandler"})
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100788
Bram Moolenaar3ec574f2017-06-13 18:12:01 +0200789Depending on the system, starting a job can put Vim in the background, the
790started job gets the focus. To avoid that, use the `foreground()` function.
791This might not always work when called early, put in the callback handler or
792use a timer to call it after the job has started.
793
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100794You can send a message to the command with ch_evalraw(). If the channel is in
795JSON or JS mode you can use ch_evalexpr().
Bram Moolenaar38a55632016-02-15 22:07:32 +0100796
797There are several options you can use, see |job-options|.
Bram Moolenaar187db502016-02-27 14:44:26 +0100798For example, to start a job and write its output in buffer "dummy": >
799 let logjob = job_start("tail -f /tmp/log",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100800 \ {'out_io': 'buffer', 'out_name': 'dummy'})
Bram Moolenaar187db502016-02-27 14:44:26 +0100801 sbuf dummy
Bram Moolenaar38a55632016-02-15 22:07:32 +0100802
Bram Moolenaar5f148ec2016-03-07 22:59:26 +0100803
804Job input from a buffer ~
Bram Moolenaar818078d2016-08-27 21:58:42 +0200805 *in_io-buffer*
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100806To run a job that reads from a buffer: >
807 let job = job_start({command},
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100808 \ {'in_io': 'buffer', 'in_name': 'mybuffer'})
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100809<
810 *E915* *E918*
811The buffer is found by name, similar to |bufnr()|. The buffer must exist and
812be loaded when job_start() is called.
813
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100814By default this reads the whole buffer. This can be changed with the "in_top"
815and "in_bot" options.
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100816
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100817A special mode is when "in_top" is set to zero and "in_bot" is not set: Every
Bram Moolenaar74675a62017-07-15 13:53:23 +0200818time a line is added to the buffer, the last-but-one line will be sent to the
Bram Moolenaar5f148ec2016-03-07 22:59:26 +0100819job stdin. This allows for editing the last line and sending it when pressing
820Enter.
Bram Moolenaar0874a832016-09-01 15:11:51 +0200821 *channel-close-in*
822When not using the special mode the pipe or socket will be closed after the
823last line has been written. This signals the reading end that the input
824finished. You can also use |ch_close_in()| to close it sooner.
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100825
Bram Moolenaar063b9d12016-07-09 20:21:48 +0200826NUL bytes in the text will be passed to the job (internally Vim stores these
827as NL bytes).
828
Bram Moolenaar06481422016-04-30 15:13:38 +0200829
830Reading job output in the close callback ~
831 *read-in-close-cb*
832If the job can take some time and you don't need intermediate results, you can
833add a close callback and read the output there: >
834
835 func! CloseHandler(channel)
Bram Moolenaar2ec618c2016-10-01 14:47:05 +0200836 while ch_status(a:channel, {'part': 'out'}) == 'buffered'
Bram Moolenaar06481422016-04-30 15:13:38 +0200837 echomsg ch_read(a:channel)
838 endwhile
839 endfunc
840 let job = job_start(command, {'close_cb': 'CloseHandler'})
841
842You will want to do something more useful than "echomsg".
843
Bram Moolenaar38a55632016-02-15 22:07:32 +0100844==============================================================================
Bram Moolenaared997ad2019-07-21 16:42:00 +020084510. Starting a job without a channel *job-start-nochannel*
Bram Moolenaar38a55632016-02-15 22:07:32 +0100846
847To start another process without creating a channel: >
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100848 let job = job_start(command,
Bram Moolenaar51628222016-12-01 23:03:28 +0100849 \ {"in_io": "null", "out_io": "null", "err_io": "null"})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100850
851This starts {command} in the background, Vim does not wait for it to finish.
852
Bram Moolenaar38a55632016-02-15 22:07:32 +0100853When Vim sees that neither stdin, stdout or stderr are connected, no channel
854will be created. Often you will want to include redirection in the command to
855avoid it getting stuck.
856
857There are several options you can use, see |job-options|.
858
Bram Moolenaar77cdfd12016-03-12 12:57:59 +0100859 *job-start-if-needed*
860To start a job only when connecting to an address does not work, do something
861like this: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100862 let channel = ch_open(address, {"waittime": 0})
863 if ch_status(channel) == "fail"
864 let job = job_start(command)
865 let channel = ch_open(address, {"waittime": 1000})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100866 endif
Bram Moolenaar77cdfd12016-03-12 12:57:59 +0100867
868Note that the waittime for ch_open() gives the job one second to make the port
869available.
Bram Moolenaar38a55632016-02-15 22:07:32 +0100870
871==============================================================================
Bram Moolenaared997ad2019-07-21 16:42:00 +020087211. Job functions *job-functions-details*
873
874job_getchannel({job}) *job_getchannel()*
875 Get the channel handle that {job} is using.
876 To check if the job has no channel: >
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +0200877 if string(job_getchannel(job)) == 'channel fail'
Bram Moolenaared997ad2019-07-21 16:42:00 +0200878<
Bram Moolenaar570497a2019-08-22 22:55:13 +0200879 Can also be used as a |method|: >
880 GetJob()->job_getchannel()
Bram Moolenaared997ad2019-07-21 16:42:00 +0200881
882job_info([{job}]) *job_info()*
883 Returns a Dictionary with information about {job}:
884 "status" what |job_status()| returns
885 "channel" what |job_getchannel()| returns
886 "cmd" List of command arguments used to start the job
887 "process" process ID
888 "tty_in" terminal input name, empty when none
889 "tty_out" terminal output name, empty when none
890 "exitval" only valid when "status" is "dead"
891 "exit_cb" function to be called on exit
892 "stoponexit" |job-stoponexit|
893
894 Only in Unix:
895 "termsig" the signal which terminated the process
896 (See |job_stop()| for the values)
897 only valid when "status" is "dead"
898
899 Only in MS-Windows:
900 "tty_type" Type of virtual console in use.
901 Values are "winpty" or "conpty".
902 See 'termwintype'.
903
904 Without any arguments, returns a List with all Job objects.
905
Bram Moolenaar570497a2019-08-22 22:55:13 +0200906 Can also be used as a |method|: >
907 GetJob()->job_info()
908
Bram Moolenaared997ad2019-07-21 16:42:00 +0200909
910job_setoptions({job}, {options}) *job_setoptions()*
911 Change options for {job}. Supported are:
912 "stoponexit" |job-stoponexit|
913 "exit_cb" |job-exit_cb|
914
Bram Moolenaar570497a2019-08-22 22:55:13 +0200915 Can also be used as a |method|: >
916 GetJob()->job_setoptions(options)
917
Bram Moolenaared997ad2019-07-21 16:42:00 +0200918
919job_start({command} [, {options}]) *job_start()*
920 Start a job and return a Job object. Unlike |system()| and
921 |:!cmd| this does not wait for the job to finish.
922 To start a job in a terminal window see |term_start()|.
923
924 If the job fails to start then |job_status()| on the returned
925 Job object results in "fail" and none of the callbacks will be
926 invoked.
927
928 {command} can be a String. This works best on MS-Windows. On
929 Unix it is split up in white-separated parts to be passed to
930 execvp(). Arguments in double quotes can contain white space.
931
932 {command} can be a List, where the first item is the executable
933 and further items are the arguments. All items are converted
934 to String. This works best on Unix.
935
936 On MS-Windows, job_start() makes a GUI application hidden. If
937 want to show it, Use |:!start| instead.
938
939 The command is executed directly, not through a shell, the
940 'shell' option is not used. To use the shell: >
941 let job = job_start(["/bin/sh", "-c", "echo hello"])
942< Or: >
943 let job = job_start('/bin/sh -c "echo hello"')
944< Note that this will start two processes, the shell and the
945 command it executes. If you don't want this use the "exec"
946 shell command.
947
948 On Unix $PATH is used to search for the executable only when
949 the command does not contain a slash.
950
951 The job will use the same terminal as Vim. If it reads from
952 stdin the job and Vim will be fighting over input, that
953 doesn't work. Redirect stdin and stdout to avoid problems: >
954 let job = job_start(['sh', '-c', "myserver </dev/null >/dev/null"])
955<
956 The returned Job object can be used to get the status with
957 |job_status()| and stop the job with |job_stop()|.
958
959 Note that the job object will be deleted if there are no
960 references to it. This closes the stdin and stderr, which may
961 cause the job to fail with an error. To avoid this keep a
962 reference to the job. Thus instead of: >
963 call job_start('my-command')
964< use: >
965 let myjob = job_start('my-command')
966< and unlet "myjob" once the job is not needed or is past the
967 point where it would fail (e.g. when it prints a message on
968 startup). Keep in mind that variables local to a function
969 will cease to exist if the function returns. Use a
970 script-local variable if needed: >
971 let s:myjob = job_start('my-command')
972<
973 {options} must be a Dictionary. It can contain many optional
974 items, see |job-options|.
975
Bram Moolenaar570497a2019-08-22 22:55:13 +0200976 Can also be used as a |method|: >
977 BuildCommand()->job_start()
978
Bram Moolenaared997ad2019-07-21 16:42:00 +0200979
980job_status({job}) *job_status()* *E916*
981 Returns a String with the status of {job}:
982 "run" job is running
983 "fail" job failed to start
984 "dead" job died or was stopped after running
985
986 On Unix a non-existing command results in "dead" instead of
987 "fail", because a fork happens before the failure can be
988 detected.
989
Bram Moolenaarcb80aa22020-10-26 21:12:46 +0100990 If in Vim9 script a variable is declared with type "job" but
991 never assigned to, passing that variable to job_status()
992 returns "fail".
993
Bram Moolenaared997ad2019-07-21 16:42:00 +0200994 If an exit callback was set with the "exit_cb" option and the
995 job is now detected to be "dead" the callback will be invoked.
996
997 For more information see |job_info()|.
998
Bram Moolenaar570497a2019-08-22 22:55:13 +0200999 Can also be used as a |method|: >
1000 GetJob()->job_status()
1001
Bram Moolenaared997ad2019-07-21 16:42:00 +02001002
1003job_stop({job} [, {how}]) *job_stop()*
1004 Stop the {job}. This can also be used to signal the job.
1005
1006 When {how} is omitted or is "term" the job will be terminated.
1007 For Unix SIGTERM is sent. On MS-Windows the job will be
1008 terminated forcedly (there is no "gentle" way).
1009 This goes to the process group, thus children may also be
1010 affected.
1011
1012 Effect for Unix:
1013 "term" SIGTERM (default)
1014 "hup" SIGHUP
1015 "quit" SIGQUIT
1016 "int" SIGINT
1017 "kill" SIGKILL (strongest way to stop)
1018 number signal with that number
1019
1020 Effect for MS-Windows:
1021 "term" terminate process forcedly (default)
1022 "hup" CTRL_BREAK
1023 "quit" CTRL_BREAK
1024 "int" CTRL_C
1025 "kill" terminate process forcedly
1026 Others CTRL_BREAK
1027
1028 On Unix the signal is sent to the process group. This means
1029 that when the job is "sh -c command" it affects both the shell
1030 and the command.
1031
1032 The result is a Number: 1 if the operation could be executed,
1033 0 if "how" is not supported on the system.
1034 Note that even when the operation was executed, whether the
1035 job was actually stopped needs to be checked with
1036 |job_status()|.
1037
1038 If the status of the job is "dead", the signal will not be
1039 sent. This is to avoid to stop the wrong job (esp. on Unix,
1040 where process numbers are recycled).
1041
1042 When using "kill" Vim will assume the job will die and close
1043 the channel.
1044
Bram Moolenaar570497a2019-08-22 22:55:13 +02001045 Can also be used as a |method|: >
1046 GetJob()->job_stop()
1047
Bram Moolenaared997ad2019-07-21 16:42:00 +02001048
1049==============================================================================
105012. Job options *job-options*
Bram Moolenaar38a55632016-02-15 22:07:32 +01001051
1052The {options} argument in job_start() is a dictionary. All entries are
Bram Moolenaar02e83b42016-02-21 20:10:26 +01001053optional. Some options can be used after the job has started, using
1054job_setoptions(job, {options}). Many options can be used with the channel
1055related to the job, using ch_setoptions(channel, {options}).
1056See |job_setoptions()| and |ch_setoptions()|.
Bram Moolenaar38a55632016-02-15 22:07:32 +01001057
Bram Moolenaar4f3f6682016-03-26 23:01:59 +01001058 *in_mode* *out_mode* *err_mode*
1059"in_mode" mode specifically for stdin, only when using pipes
1060"out_mode" mode specifically for stdout, only when using pipes
1061"err_mode" mode specifically for stderr, only when using pipes
1062 See |channel-mode| for the values.
1063
1064 Note: when setting "mode" the part specific mode is
1065 overwritten. Therefore set "mode" first and the part
1066 specific mode later.
1067
1068 Note: when writing to a file or buffer and when
1069 reading from a buffer NL mode is used by default.
1070
Bram Moolenaar0b146882018-09-06 16:27:24 +02001071 *job-noblock*
1072"noblock": 1 When writing use a non-blocking write call. This
1073 avoids getting stuck if Vim should handle other
1074 messages in between, e.g. when a job sends back data
1075 to Vim. It implies that when `ch_sendraw()` returns
1076 not all data may have been written yet.
1077 This option was added in patch 8.1.0350, test with: >
1078 if has("patch-8.1.350")
1079 let options['noblock'] = 1
1080 endif
1081<
Bram Moolenaardecb14d2016-02-20 23:32:02 +01001082 *job-callback*
1083"callback": handler Callback for something to read on any part of the
1084 channel.
Bram Moolenaar4f3f6682016-03-26 23:01:59 +01001085 *job-out_cb* *out_cb*
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001086"out_cb": handler Callback for when there is something to read on
Bram Moolenaar4f3f6682016-03-26 23:01:59 +01001087 stdout. Only for when the channel uses pipes. When
1088 "out_cb" wasn't set the channel callback is used.
Bram Moolenaar269f5952016-07-15 22:54:41 +02001089 The two arguments are the channel and the message.
Bram Moolenaar4f3f6682016-03-26 23:01:59 +01001090
1091 *job-err_cb* *err_cb*
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001092"err_cb": handler Callback for when there is something to read on
Bram Moolenaar4f3f6682016-03-26 23:01:59 +01001093 stderr. Only for when the channel uses pipes. When
1094 "err_cb" wasn't set the channel callback is used.
Bram Moolenaar269f5952016-07-15 22:54:41 +02001095 The two arguments are the channel and the message.
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001096 *job-close_cb*
1097"close_cb": handler Callback for when the channel is closed. Same as
Bram Moolenaar82af8712016-06-04 20:20:29 +02001098 "close_cb" on |ch_open()|, see |close_cb|.
Bram Moolenaarbc2eada2017-01-02 21:27:47 +01001099 *job-drop*
Bram Moolenaarb6e0ec62017-07-23 22:12:20 +02001100"drop": when Specifies when to drop messages. Same as "drop" on
Bram Moolenaar51628222016-12-01 23:03:28 +01001101 |ch_open()|, see |channel-drop|. For "auto" the
1102 exit_cb is not considered.
Bram Moolenaarbc2eada2017-01-02 21:27:47 +01001103 *job-exit_cb*
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001104"exit_cb": handler Callback for when the job ends. The arguments are the
Bram Moolenaar38a55632016-02-15 22:07:32 +01001105 job and the exit status.
Bram Moolenaarb4ada792016-10-30 21:55:26 +01001106 Vim checks up to 10 times per second for jobs that
1107 ended. The check can also be triggered by calling
1108 |job_status()|, which may then invoke the exit_cb
1109 handler.
Bram Moolenaar06d2d382016-05-20 17:24:11 +02001110 Note that data can be buffered, callbacks may still be
1111 called after the process ends.
Bram Moolenaar4f3f6682016-03-26 23:01:59 +01001112 *job-timeout*
Bram Moolenaarb6e0ec62017-07-23 22:12:20 +02001113"timeout": time The time to wait for a request when blocking, E.g.
Bram Moolenaar4f3f6682016-03-26 23:01:59 +01001114 when using ch_evalexpr(). In milliseconds. The
1115 default is 2000 (2 seconds).
1116 *out_timeout* *err_timeout*
Bram Moolenaarb6e0ec62017-07-23 22:12:20 +02001117"out_timeout": time Timeout for stdout. Only when using pipes.
1118"err_timeout": time Timeout for stderr. Only when using pipes.
Bram Moolenaar4f3f6682016-03-26 23:01:59 +01001119 Note: when setting "timeout" the part specific mode is
1120 overwritten. Therefore set "timeout" first and the
1121 part specific mode later.
1122
Bram Moolenaar02e83b42016-02-21 20:10:26 +01001123 *job-stoponexit*
1124"stoponexit": {signal} Send {signal} to the job when Vim exits. See
1125 |job_stop()| for possible values.
1126"stoponexit": "" Do not stop the job when Vim exits.
1127 The default is "term".
1128
Bram Moolenaar77cdfd12016-03-12 12:57:59 +01001129 *job-term*
Bram Moolenaarb6e0ec62017-07-23 22:12:20 +02001130"term": "open" Start a terminal in a new window and connect the job
1131 stdin/stdout/stderr to it. Similar to using
1132 `:terminal`.
Bram Moolenaar77cdfd12016-03-12 12:57:59 +01001133 NOTE: Not implemented yet!
Bram Moolenaar38a55632016-02-15 22:07:32 +01001134
Bram Moolenaar77cdfd12016-03-12 12:57:59 +01001135"channel": {channel} Use an existing channel instead of creating a new one.
1136 The parts of the channel that get used for the new job
1137 will be disconnected from what they were used before.
Bram Moolenaar51628222016-12-01 23:03:28 +01001138 If the channel was still used by another job this may
Bram Moolenaar77cdfd12016-03-12 12:57:59 +01001139 cause I/O errors.
1140 Existing callbacks and other settings remain.
1141
Bram Moolenaarb6e0ec62017-07-23 22:12:20 +02001142"pty": 1 Use a pty (pseudo-tty) instead of a pipe when
1143 possible. This is most useful in combination with a
1144 terminal window, see |terminal|.
1145 {only on Unix and Unix-like systems}
1146
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001147 *job-in_io* *in_top* *in_bot* *in_name* *in_buf*
1148"in_io": "null" disconnect stdin (read from /dev/null)
1149"in_io": "pipe" stdin is connected to the channel (default)
1150"in_io": "file" stdin reads from a file
1151"in_io": "buffer" stdin reads from a buffer
1152"in_top": number when using "buffer": first line to send (default: 1)
1153"in_bot": number when using "buffer": last line to send (default: last)
1154"in_name": "/path/file" the name of the file or buffer to read from
1155"in_buf": number the number of the buffer to read from
Bram Moolenaar38a55632016-02-15 22:07:32 +01001156
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001157 *job-out_io* *out_name* *out_buf*
1158"out_io": "null" disconnect stdout (goes to /dev/null)
1159"out_io": "pipe" stdout is connected to the channel (default)
1160"out_io": "file" stdout writes to a file
Bram Moolenaar51628222016-12-01 23:03:28 +01001161"out_io": "buffer" stdout appends to a buffer (see below)
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001162"out_name": "/path/file" the name of the file or buffer to write to
1163"out_buf": number the number of the buffer to write to
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001164"out_modifiable": 0 when writing to a buffer, 'modifiable' will be off
1165 (see below)
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001166"out_msg": 0 when writing to a new buffer, the first line will be
1167 set to "Reading from channel output..."
Bram Moolenaar38a55632016-02-15 22:07:32 +01001168
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001169 *job-err_io* *err_name* *err_buf*
1170"err_io": "out" stderr messages to go to stdout
1171"err_io": "null" disconnect stderr (goes to /dev/null)
1172"err_io": "pipe" stderr is connected to the channel (default)
1173"err_io": "file" stderr writes to a file
Bram Moolenaar51628222016-12-01 23:03:28 +01001174"err_io": "buffer" stderr appends to a buffer (see below)
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001175"err_name": "/path/file" the name of the file or buffer to write to
1176"err_buf": number the number of the buffer to write to
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001177"err_modifiable": 0 when writing to a buffer, 'modifiable' will be off
1178 (see below)
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001179"err_msg": 0 when writing to a new buffer, the first line will be
1180 set to "Reading from channel error..."
Bram Moolenaar77cdfd12016-03-12 12:57:59 +01001181
Bram Moolenaar7db8f6f2016-03-29 23:12:46 +02001182"block_write": number only for testing: pretend every other write to stdin
1183 will block
1184
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001185"env": dict environment variables for the new process
1186"cwd": "/path/to/dir" current working directory for the new process;
1187 if the directory does not exist an error is given
1188
Bram Moolenaar77cdfd12016-03-12 12:57:59 +01001189
1190Writing to a buffer ~
Bram Moolenaar818078d2016-08-27 21:58:42 +02001191 *out_io-buffer*
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001192When the out_io or err_io mode is "buffer" and there is a callback, the text
Bram Moolenaar5f148ec2016-03-07 22:59:26 +01001193is appended to the buffer before invoking the callback.
1194
1195When a buffer is used both for input and output, the output lines are put
1196above the last line, since the last line is what is written to the channel
Bram Moolenaar77cdfd12016-03-12 12:57:59 +01001197input. Otherwise lines are appended below the last line.
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001198
Bram Moolenaar328da0d2016-03-04 22:22:32 +01001199When using JS or JSON mode with "buffer", only messages with zero or negative
1200ID will be added to the buffer, after decoding + encoding. Messages with a
1201positive number will be handled by a callback, commands are handled as usual.
1202
Bram Moolenaar82af8712016-06-04 20:20:29 +02001203The name of the buffer from "out_name" or "err_name" is compared the full name
1204of existing buffers, also after expanding the name for the current directory.
1205E.g., when a buffer was created with ":edit somename" and the buffer name is
1206"somename" it will use that buffer.
1207
1208If there is no matching buffer a new buffer is created. Use an empty name to
1209always create a new buffer. |ch_getbufnr()| can then be used to get the
1210buffer number.
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001211
1212For a new buffer 'buftype' is set to "nofile" and 'bufhidden' to "hide". If
1213you prefer other settings, create the buffer first and pass the buffer number.
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001214 *out_modifiable* *err_modifiable*
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001215The "out_modifiable" and "err_modifiable" options can be used to set the
1216'modifiable' option off, or write to a buffer that has 'modifiable' off. That
1217means that lines will be appended to the buffer, but the user can't easily
1218change the buffer.
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001219 *out_msg* *err_msg*
1220The "out_msg" option can be used to specify whether a new buffer will have the
1221first line set to "Reading from channel output...". The default is to add the
1222message. "err_msg" does the same for channel error.
1223
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001224When an existing buffer is to be written where 'modifiable' is off and the
1225"out_modifiable" or "err_modifiable" options is not zero, an error is given
1226and the buffer will not be written to.
1227
Bram Moolenaar187db502016-02-27 14:44:26 +01001228When the buffer written to is displayed in a window and the cursor is in the
1229first column of the last line, the cursor will be moved to the newly added
1230line and the window is scrolled up to show the cursor if needed.
1231
Bram Moolenaar063b9d12016-07-09 20:21:48 +02001232Undo is synced for every added line. NUL bytes are accepted (internally Vim
1233stores these as NL bytes).
Bram Moolenaar38a55632016-02-15 22:07:32 +01001234
Bram Moolenaar77cdfd12016-03-12 12:57:59 +01001235
1236Writing to a file ~
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001237 *E920*
Bram Moolenaar77cdfd12016-03-12 12:57:59 +01001238The file is created with permissions 600 (read-write for the user, not
1239accessible for others). Use |setfperm()| to change this.
1240
1241If the file already exists it is truncated.
1242
Bram Moolenaar38a55632016-02-15 22:07:32 +01001243==============================================================================
Bram Moolenaared997ad2019-07-21 16:42:00 +0200124413. Controlling a job *job-control*
Bram Moolenaar38a55632016-02-15 22:07:32 +01001245
1246To get the status of a job: >
1247 echo job_status(job)
1248
1249To make a job stop running: >
1250 job_stop(job)
1251
1252This is the normal way to end a job. On Unix it sends a SIGTERM to the job.
1253It is possible to use other ways to stop the job, or even send arbitrary
1254signals. E.g. to force a job to stop, "kill it": >
1255 job_stop(job, "kill")
1256
1257For more options see |job_stop()|.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001258
Bram Moolenaarf2732452018-06-03 14:47:35 +02001259==============================================================================
Bram Moolenaared997ad2019-07-21 16:42:00 +0200126014. Using a prompt buffer *prompt-buffer*
Bram Moolenaarf2732452018-06-03 14:47:35 +02001261
1262If you want to type input for the job in a Vim window you have a few options:
1263- Use a normal buffer and handle all possible commands yourself.
1264 This will be complicated, since there are so many possible commands.
1265- Use a terminal window. This works well if what you type goes directly to
1266 the job and the job output is directly displayed in the window.
1267 See |terminal-window|.
Bram Moolenaaracc22402020-06-07 21:07:18 +02001268- Use a window with a prompt buffer. This works well when entering a line for
1269 the job in Vim while displaying (possibly filtered) output from the job.
Bram Moolenaarf2732452018-06-03 14:47:35 +02001270
1271A prompt buffer is created by setting 'buftype' to "prompt". You would
1272normally only do that in a newly created buffer.
1273
1274The user can edit and enter one line of text at the very last line of the
1275buffer. When pressing Enter in the prompt line the callback set with
1276|prompt_setcallback()| is invoked. It would normally send the line to a job.
1277Another callback would receive the output from the job and display it in the
1278buffer, below the prompt (and above the next prompt).
1279
1280Only the text in the last line, after the prompt, is editable. The rest of the
1281buffer is not modifiable with Normal mode commands. It can be modified by
1282calling functions, such as |append()|. Using other commands may mess up the
1283buffer.
1284
1285After setting 'buftype' to "prompt" Vim does not automatically start Insert
1286mode, use `:startinsert` if you want to enter Insert mode, so that the user
1287can start typing a line.
1288
Bram Moolenaar077cc7a2020-09-04 16:35:35 +02001289The text of the prompt can be set with the |prompt_setprompt()| function. If
1290no prompt is set with |prompt_setprompt()|, "% " is used. You can get the
1291effective prompt text for a buffer, with |prompt_getprompt()|.
Bram Moolenaarf2732452018-06-03 14:47:35 +02001292
1293The user can go to Normal mode and navigate through the buffer. This can be
Bram Moolenaar7ff78462020-07-10 22:00:53 +02001294useful to see older output or copy text.
Bram Moolenaarf2732452018-06-03 14:47:35 +02001295
Bram Moolenaard2f3a8b2018-06-19 14:35:59 +02001296The CTRL-W key can be used to start a window command, such as CTRL-W w to
1297switch to the next window. This also works in Insert mode (use Shift-CTRL-W
1298to delete a word). When leaving the window Insert mode will be stopped. When
1299coming back to the prompt window Insert mode will be restored.
1300
Bram Moolenaarf2732452018-06-03 14:47:35 +02001301Any command that starts Insert mode, such as "a", "i", "A" and "I", will move
Bram Moolenaard2f3a8b2018-06-19 14:35:59 +02001302the cursor to the last line. "A" will move to the end of the line, "I" to the
1303start of the line.
Bram Moolenaarf2732452018-06-03 14:47:35 +02001304
Bram Moolenaaracc22402020-06-07 21:07:18 +02001305Here is an example for Unix. It starts a shell in the background and prompts
1306for the next shell command. Output from the shell is displayed above the
1307prompt. >
1308
1309 " Create a channel log so we can see what happens.
1310 call ch_logfile('logfile', 'w')
1311
Bram Moolenaar1588bc82022-03-08 21:35:07 +00001312 " Function handling a line of text that has been typed.
Bram Moolenaaracc22402020-06-07 21:07:18 +02001313 func TextEntered(text)
1314 " Send the text to a shell with Enter appended.
1315 call ch_sendraw(g:shell_job, a:text .. "\n")
1316 endfunc
Bram Moolenaarcb80aa22020-10-26 21:12:46 +01001317
Bram Moolenaar1588bc82022-03-08 21:35:07 +00001318 " Function handling output from the shell: Add it above the prompt.
Bram Moolenaaracc22402020-06-07 21:07:18 +02001319 func GotOutput(channel, msg)
Bram Moolenaarc51cf032022-02-26 12:25:45 +00001320 call append(line("$") - 1, "- " .. a:msg)
Bram Moolenaaracc22402020-06-07 21:07:18 +02001321 endfunc
1322
Bram Moolenaar1588bc82022-03-08 21:35:07 +00001323 " Function handling the shell exits: close the window.
Bram Moolenaaracc22402020-06-07 21:07:18 +02001324 func JobExit(job, status)
1325 quit!
1326 endfunc
1327
1328 " Start a shell in the background.
1329 let shell_job = job_start(["/bin/sh"], #{
1330 \ out_cb: function('GotOutput'),
1331 \ err_cb: function('GotOutput'),
1332 \ exit_cb: function('JobExit'),
1333 \ })
Bram Moolenaaracc22402020-06-07 21:07:18 +02001334
1335 new
1336 set buftype=prompt
1337 let buf = bufnr('')
1338 call prompt_setcallback(buf, function("TextEntered"))
1339 eval prompt_setprompt(buf, "shell command: ")
1340
1341 " start accepting shell commands
1342 startinsert
1343<
Bram Moolenaar1588bc82022-03-08 21:35:07 +00001344The same in |Vim9| script: >
1345
1346 vim9script
1347
1348 # Create a channel log so we can see what happens.
1349 ch_logfile('logfile', 'w')
1350
1351 var shell_job: job
1352
1353 # Function handling a line of text that has been typed.
1354 def TextEntered(text: string)
1355 # Send the text to a shell with Enter appended.
1356 ch_sendraw(shell_job, text .. "\n")
1357 enddef
1358
1359 # Function handling output from the shell: Add it above the prompt.
1360 def GotOutput(channel: channel, msg: string)
1361 append(line("$") - 1, "- " .. msg)
1362 enddef
1363
1364 # Function handling the shell exits: close the window.
1365 def JobExit(job: job, status: number)
1366 quit!
1367 enddef
1368
1369 # Start a shell in the background.
1370 shell_job = job_start(["/bin/sh"], {
1371 out_cb: GotOutput,
1372 err_cb: GotOutput,
1373 exit_cb: JobExit,
1374 })
1375
1376 new
1377 set buftype=prompt
1378 var buf = bufnr('')
1379 prompt_setcallback(buf, TextEntered)
1380 prompt_setprompt(buf, "shell command: ")
1381
1382 # start accepting shell commands
1383 startinsert
Bram Moolenaaracc22402020-06-07 21:07:18 +02001384
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01001385==============================================================================
138614. Language Server Protocol *language-server-protocol*
1387
1388The language server protocol specification is available at:
1389
1390 https://microsoft.github.io/language-server-protocol/specification
1391
1392Each LSP protocol message starts with a simple HTTP header followed by the
1393payload encoded in JSON-RPC format. This is described in:
1394
1395 https://www.jsonrpc.org/specification
1396
1397For messages received on a channel with mode set to "lsp", Vim will process
1398the HTTP header and decode the payload into a Vim |Dict| type and call the
1399channel callback or the specified callback function. When sending messages on
1400a channel using |ch_evalexpr()| or |ch_sendexpr()|, Vim will add the HTTP
1401header and encode the Vim expression into JSON-RPC.
1402
1403To open a channel using the 'lsp' mode, set the 'mode' item in the |ch_open()|
1404{options} argument to 'lsp'. Example: >
1405
1406 let ch = ch_open(..., #{mode: 'lsp'})
1407
1408To open a channel using the 'lsp' mode with a job, set the 'in_mode' and
1409'out_mode' items in the |job_start()| {options} argument to 'lsp'. Example: >
1410
1411 let job = job_start(...., #{in_mode: 'lsp', out_mode: 'lsp'})
1412
1413To synchronously send a JSON-RPC request to the server, use the |ch_evalexpr()|
1414function. This function will return the response from the server. You can use
1415the 'timeout' field in the {options} argument to control the response wait
1416time. Example: >
1417
1418 let req = {}
1419 let req.method = 'textDocument/definition'
1420 let req.params = {}
1421 let req.params.textDocument = #{uri: 'a.c'}
1422 let req.params.position = #{line: 10, character: 3}
1423 let resp = ch_evalexpr(ch, req, #{timeout: 100})
1424
1425Note that in the request message the 'id' field should not be specified. If it
1426is specified, then Vim will overwrite the value with an internally generated
1427identifier. Vim currently supports only a number type for the 'id' field.
1428
1429To send a JSON-RPC request to the server and asynchronously process the
1430response, use the |ch_sendexpr()| function and supply a callback function.
1431Example: >
1432
1433 let req = {}
1434 let req.method = 'textDocument/hover'
1435 let req.params = {}
1436 let req.params.textDocument = #{uri: 'a.c'}
1437 let req.params.position = #{line: 10, character: 3}
1438 let resp = ch_sendexpr(ch, req, #{callback: 'MyFn'})
1439
1440To send a JSON-RPC notification message to the server, use the |ch_sendexpr()|
1441function. Example: >
1442
1443 call ch_sendexpr(ch, #{method: 'initialized'})
1444
1445To respond to a JSON-RPC request message from the server, use the
1446|ch_sendexpr()| function. In the response message, copy the 'id' field value
1447from the server request message. Example: >
1448
1449 let resp = {}
1450 let resp.id = req.id
1451 let resp.result = 1
1452 call ch_sendexpr(ch, resp)
1453
1454The JSON-RPC notification messages from the server are delivered through the
1455|channel-callback| function.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001456
Bram Moolenaar91f84f62018-07-29 15:07:52 +02001457 vim:tw=78:ts=8:noet:ft=help:norl: