blob: f112119dc63f0169ce606c20c3a8dd195c3f0f43 [file] [log] [blame]
Bram Moolenaar46eea442022-03-30 10:51:39 +01001*channel.txt* For Vim version 8.2. Last change: 2022 Mar 26
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
122{address} has the form "hostname:port". E.g., "localhost:8765".
123
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200124When using an IPv6 address, enclose it within square brackets. E.g.,
125"[2001:db8::1]:8765".
126
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100127{options} is a dictionary with optional entries: *channel-open-options*
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100128
129"mode" can be: *channel-mode*
130 "json" - Use JSON, see below; most convenient way. Default.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100131 "js" - Use JS (JavaScript) encoding, more efficient than JSON.
Bram Moolenaar38a55632016-02-15 22:07:32 +0100132 "nl" - Use messages that end in a NL character
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100133 "raw" - Use raw messages
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +0100134 "lsp" - Use language server protocol encoding
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100135 *channel-callback* *E921*
Bram Moolenaar38a55632016-02-15 22:07:32 +0100136"callback" A function that is called when a message is received that is
Bram Moolenaar47003982021-12-05 21:54:04 +0000137 not handled otherwise (e.g. a JSON message with ID zero). It
138 gets two arguments: the channel and the received message.
139 Example: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100140 func Handle(channel, msg)
Bram Moolenaarc51cf032022-02-26 12:25:45 +0000141 echo 'Received: ' .. a:msg
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100142 endfunc
Bram Moolenaar38a55632016-02-15 22:07:32 +0100143 let channel = ch_open("localhost:8765", {"callback": "Handle"})
144<
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +0100145 When "mode" is "json" or "js" or "lsp" the "msg" argument is
146 the body of the received message, converted to Vim types.
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100147 When "mode" is "nl" the "msg" argument is one message,
148 excluding the NL.
149 When "mode" is "raw" the "msg" argument is the whole message
150 as a string.
Bram Moolenaare18c0b32016-03-20 21:08:34 +0100151
152 For all callbacks: Use |function()| to bind it to arguments
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100153 and/or a Dictionary. Or use the form "dict.function" to bind
154 the Dictionary.
Bram Moolenaar06d2d382016-05-20 17:24:11 +0200155
156 Callbacks are only called at a "safe" moment, usually when Vim
157 is waiting for the user to type a character. Vim does not use
158 multi-threading.
159
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100160 *close_cb*
161"close_cb" A function that is called when the channel gets closed, other
Bram Moolenaar38a55632016-02-15 22:07:32 +0100162 than by calling ch_close(). It should be defined like this: >
163 func MyCloseHandler(channel)
Bram Moolenaar06d2d382016-05-20 17:24:11 +0200164< Vim will invoke callbacks that handle data before invoking
165 close_cb, thus when this function is called no more data will
Bram Moolenaar68e65602019-05-26 21:33:31 +0200166 be passed to the callbacks. However, if a callback causes Vim
167 to check for messages, the close_cb may be invoked while still
168 in the callback. The plugin must handle this somehow, it can
169 be useful to know that no more data is coming.
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +0100170 If it is not known if there is a message to be read, use a
171 try/catch block: >
172 try
173 let msg = ch_readraw(a:channel)
174 catch
175 let msg = 'no message'
176 endtry
177 try
178 let err = ch_readraw(a:channel, #{part: 'err'})
179 catch
180 let err = 'no error'
181 endtry
182< *channel-drop*
Bram Moolenaar958dc692016-12-01 15:34:12 +0100183"drop" Specifies when to drop messages:
184 "auto" When there is no callback to handle a message.
185 The "close_cb" is also considered for this.
186 "never" All messages will be kept.
187
Bram Moolenaar0b146882018-09-06 16:27:24 +0200188 *channel-noblock*
189"noblock" Same effect as |job-noblock|. Only matters for writing.
190
Bram Moolenaar06d2d382016-05-20 17:24:11 +0200191 *waittime*
Bram Moolenaar38a55632016-02-15 22:07:32 +0100192"waittime" The time to wait for the connection to be made in
Bram Moolenaarf3913272016-02-25 00:00:01 +0100193 milliseconds. A negative number waits forever.
194
195 The default is zero, don't wait, which is useful if a local
196 server is supposed to be running already. On Unix Vim
197 actually uses a 1 msec timeout, that is required on many
198 systems. Use a larger value for a remote server, e.g. 10
199 msec at least.
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100200 *channel-timeout*
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100201"timeout" The time to wait for a request when blocking, E.g. when using
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100202 ch_evalexpr(). In milliseconds. The default is 2000 (2
Bram Moolenaar38a55632016-02-15 22:07:32 +0100203 seconds).
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100204
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100205When "mode" is "json" or "js" the "callback" is optional. When omitted it is
206only possible to receive a message after sending one.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100207
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100208To change the channel options after opening it use |ch_setoptions()|. The
209arguments are similar to what is passed to |ch_open()|, but "waittime" cannot
210be given, since that only applies to opening the channel.
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100211
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100212For example, the handler can be added or changed: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100213 call ch_setoptions(channel, {'callback': callback})
214When "callback" is empty (zero or an empty string) the handler is removed.
215
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100216After a callback has been invoked Vim will update the screen and put the
217cursor back where it belongs. Thus the callback should not need to do
218`:redraw`.
219
Bram Moolenaar38a55632016-02-15 22:07:32 +0100220The timeout can be changed: >
221 call ch_setoptions(channel, {'timeout': msec})
222<
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100223 *channel-close* *E906*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100224Once done with the channel, disconnect it like this: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100225 call ch_close(channel)
226When a socket is used this will close the socket for both directions. When
227pipes are used (stdin/stdout/stderr) they are all closed. This might not be
228what you want! Stopping the job with job_stop() might be better.
Bram Moolenaar187db502016-02-27 14:44:26 +0100229All readahead is discarded, callbacks will no longer be invoked.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100230
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100231Note that a channel is closed in three stages:
232 - The I/O ends, log message: "Closing channel". There can still be queued
233 messages to read or callbacks to invoke.
234 - The readahead is cleared, log message: "Clearing channel". Some variables
235 may still reference the channel.
236 - The channel is freed, log message: "Freeing channel".
237
Bram Moolenaarcbebd482016-02-07 23:02:56 +0100238When the channel can't be opened you will get an error message. There is a
239difference between MS-Windows and Unix: On Unix when the port doesn't exist
240ch_open() fails quickly. On MS-Windows "waittime" applies.
Bram Moolenaaraa3b15d2016-04-21 08:53:19 +0200241*E898* *E901* *E902*
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100242
243If there is an error reading or writing a channel it will be closed.
Bram Moolenaar664f3cf2019-12-07 16:03:51 +0100244*E630* *E631*
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100245
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100246==============================================================================
Bram Moolenaar38a55632016-02-15 22:07:32 +01002474. Using a JSON or JS channel *channel-use*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100248
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100249If mode is JSON then a message can be sent synchronously like this: >
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100250 let response = ch_evalexpr(channel, {expr})
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100251This awaits a response from the other side.
252
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100253When mode is JS this works the same, except that the messages use
Bram Moolenaar38a55632016-02-15 22:07:32 +0100254JavaScript encoding. See |js_encode()| for the difference.
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100255
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100256To send a message, without handling a response or letting the channel callback
257handle the response: >
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100258 call ch_sendexpr(channel, {expr})
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100259
260To send a message and letting the response handled by a specific function,
261asynchronously: >
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100262 call ch_sendexpr(channel, {expr}, {'callback': Handler})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100263
264Vim will match the response with the request using the message ID. Once the
265response is received the callback will be invoked. Further responses with the
266same ID will be ignored. If your server sends back multiple responses you
267need to send them with ID zero, they will be passed to the channel callback.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100268
269The {expr} is converted to JSON and wrapped in an array. An example of the
270message that the receiver will get when {expr} is the string "hello":
271 [12,"hello"] ~
272
273The format of the JSON sent is:
274 [{number},{expr}]
275
276In which {number} is different every time. It must be used in the response
277(if any):
278
279 [{number},{response}]
280
281This way Vim knows which sent message matches with which received message and
282can call the right handler. Also when the messages arrive out of order.
283
Bram Moolenaarf1f07922016-08-26 17:58:53 +0200284A newline character is terminating the JSON text. This can be used to
285separate the read text. For example, in Python:
286 splitidx = read_text.find('\n')
287 message = read_text[:splitidx]
288 rest = read_text[splitidx + 1:]
289
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100290The sender must always send valid JSON to Vim. Vim can check for the end of
291the message by parsing the JSON. It will only accept the message if the end
Bram Moolenaarf1f07922016-08-26 17:58:53 +0200292was received. A newline after the message is optional.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100293
294When the process wants to send a message to Vim without first receiving a
295message, it must use the number zero:
296 [0,{response}]
297
298Then channel handler will then get {response} converted to Vim types. If the
299channel does not have a handler the message is dropped.
300
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100301It is also possible to use ch_sendraw() and ch_evalraw() on a JSON or JS
302channel. The caller is then completely responsible for correct encoding and
303decoding.
Bram Moolenaarcbebd482016-02-07 23:02:56 +0100304
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100305==============================================================================
Bram Moolenaar38a55632016-02-15 22:07:32 +01003065. Channel commands *channel-commands*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100307
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100308With a JSON channel the process can send commands to Vim that will be
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100309handled by Vim internally, it does not require a handler for the channel.
310
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100311Possible commands are: *E903* *E904* *E905*
Bram Moolenaar220adb12016-09-12 12:17:26 +0200312 ["redraw", {forced}]
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100313 ["ex", {Ex command}]
314 ["normal", {Normal mode command}]
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100315 ["expr", {expression}, {number}]
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100316 ["expr", {expression}]
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100317 ["call", {func name}, {argument list}, {number}]
318 ["call", {func name}, {argument list}]
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100319
320With all of these: Be careful what these commands do! You can easily
321interfere with what the user is doing. To avoid trouble use |mode()| to check
322that the editor is in the expected state. E.g., to send keys that must be
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100323inserted as text, not executed as a command:
324 ["ex","if mode() == 'i' | call feedkeys('ClassName') | endif"] ~
325
326Errors in these commands are normally not reported to avoid them messing up
327the display. If you do want to see them, set the 'verbose' option to 3 or
328higher.
329
330
331Command "redraw" ~
332
Bram Moolenaar63b74a82019-03-24 15:09:13 +0100333The other commands do not explicitly update the screen, so that you can send a
334sequence of commands without the cursor moving around. A redraw can happen as
335a side effect of some commands. You must end with the "redraw" command to
336show any changed text and show the cursor where it belongs.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100337
338The argument is normally an empty string:
339 ["redraw", ""] ~
340To first clear the screen pass "force":
341 ["redraw", "force"] ~
342
343
344Command "ex" ~
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100345
346The "ex" command is executed as any Ex command. There is no response for
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100347completion or error. You could use functions in an |autoload| script:
348 ["ex","call myscript#MyFunc(arg)"]
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100349
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100350You can also use "call |feedkeys()|" to insert any key sequence.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100351
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100352When there is an error a message is written to the channel log, if it exists,
353and v:errmsg is set to the error.
354
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100355
356Command "normal" ~
357
Bram Moolenaar681baaf2016-02-04 20:57:07 +0100358The "normal" command is executed like with ":normal!", commands are not
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100359mapped. Example to open the folds under the cursor:
360 ["normal" "zO"]
361
362
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100363Command "expr" with response ~
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100364
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100365The "expr" command can be used to get the result of an expression. For
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100366example, to get the number of lines in the current buffer:
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100367 ["expr","line('$')", -2] ~
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100368
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100369It will send back the result of the expression:
Bram Moolenaare0fa3742016-02-20 15:47:01 +0100370 [-2, "last line"] ~
371The format is:
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100372 [{number}, {result}]
Bram Moolenaar187db502016-02-27 14:44:26 +0100373
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100374Here {number} is the same as what was in the request. Use a negative number
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100375to avoid confusion with message that Vim sends. Use a different number on
376every request to be able to match the request with the response.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100377
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100378{result} is the result of the evaluation and is JSON encoded. If the
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100379evaluation fails or the result can't be encoded in JSON it is the string
380"ERROR".
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100381
382
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100383Command "expr" without a response ~
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100384
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100385This command is similar to "expr" above, but does not send back any response.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100386Example:
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100387 ["expr","setline('$', ['one', 'two', 'three'])"] ~
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100388There is no third argument in the request.
389
390
391Command "call" ~
392
393This is similar to "expr", but instead of passing the whole expression as a
394string this passes the name of a function and a list of arguments. This
395avoids the conversion of the arguments to a string and escaping and
396concatenating them. Example:
397 ["call", "line", ["$"], -2] ~
398
399Leave out the fourth argument if no response is to be sent:
400 ["call", "setline", ["$", ["one", "two", "three"]]] ~
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100401
402==============================================================================
Bram Moolenaar38a55632016-02-15 22:07:32 +01004036. Using a RAW or NL channel *channel-raw*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100404
Bram Moolenaarc0514bf2016-11-17 14:50:09 +0100405If mode is RAW or NL then a message can be sent like this: >
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100406 let response = ch_evalraw(channel, {string})
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100407
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100408The {string} is sent as-is. The response will be what can be read from the
409channel right away. Since Vim doesn't know how to recognize the end of the
Bram Moolenaar38a55632016-02-15 22:07:32 +0100410message you need to take care of it yourself. The timeout applies for reading
411the first byte, after that it will not wait for anything more.
412
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100413If mode is "nl" you can send a message in a similar way. You are expected
Bram Moolenaar38a55632016-02-15 22:07:32 +0100414to put in the NL after each message. Thus you can also send several messages
415ending in a NL at once. The response will be the text up to and including the
416first NL. This can also be just the NL for an empty response.
417If no NL was read before the channel timeout an empty string is returned.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100418
419To send a message, without expecting a response: >
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100420 call ch_sendraw(channel, {string})
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100421The process can send back a response, the channel handler will be called with
422it.
423
424To send a message and letting the response handled by a specific function,
425asynchronously: >
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100426 call ch_sendraw(channel, {string}, {'callback': 'MyHandler'})
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100427
Bram Moolenaar38a55632016-02-15 22:07:32 +0100428This {string} can also be JSON, use |json_encode()| to create it and
429|json_decode()| to handle a received JSON message.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100430
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100431It is not possible to use |ch_evalexpr()| or |ch_sendexpr()| on a raw channel.
Bram Moolenaarcbebd482016-02-07 23:02:56 +0100432
Bram Moolenaar818078d2016-08-27 21:58:42 +0200433A String in Vim cannot contain NUL bytes. To send or receive NUL bytes read
434or write from a buffer. See |in_io-buffer| and |out_io-buffer|.
435
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100436==============================================================================
Bram Moolenaar38a55632016-02-15 22:07:32 +01004377. More channel functions *channel-more*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100438
Bram Moolenaar38a55632016-02-15 22:07:32 +0100439To obtain the status of a channel: ch_status(channel). The possible results
440are:
441 "fail" Failed to open the channel.
442 "open" The channel can be used.
Bram Moolenaar06481422016-04-30 15:13:38 +0200443 "buffered" The channel was closed but there is data to read.
Bram Moolenaar38a55632016-02-15 22:07:32 +0100444 "closed" The channel was closed.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100445
Bram Moolenaar187db502016-02-27 14:44:26 +0100446To obtain the job associated with a channel: ch_getjob(channel)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100447
Bram Moolenaar38a55632016-02-15 22:07:32 +0100448To read one message from a channel: >
449 let output = ch_read(channel)
450This uses the channel timeout. To read without a timeout, just get any
451message that is available: >
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100452 let output = ch_read(channel, {'timeout': 0})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100453When no message was available then the result is v:none for a JSON or JS mode
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100454channels, an empty string for a RAW or NL channel. You can use |ch_canread()|
455to check if there is something to read.
456
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200457Note that when there is no callback, messages are dropped. To avoid that add
458a close callback to the channel.
Bram Moolenaar38a55632016-02-15 22:07:32 +0100459
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +0100460To read all normal output from a RAW channel that is available: >
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100461 let output = ch_readraw(channel)
Bram Moolenaar38a55632016-02-15 22:07:32 +0100462To read the error output: >
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100463 let output = ch_readraw(channel, {"part": "err"})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100464
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100465ch_read() and ch_readraw() use the channel timeout. When there is nothing to
466read within that time an empty string is returned. To specify a different
467timeout in msec use the "timeout" option:
468 {"timeout": 123} ~
469To read from the error output use the "part" option:
470 {"part": "err"} ~
471To read a message with a specific ID, on a JS or JSON channel:
472 {"id": 99} ~
473When no ID is specified or the ID is -1, the first message is returned. This
474overrules any callback waiting for this message.
475
476For a RAW channel this returns whatever is available, since Vim does not know
477where a message ends.
478For a NL channel this returns one message.
479For a JS or JSON channel this returns one decoded message.
480This includes any sequence number.
481
Bram Moolenaar38a55632016-02-15 22:07:32 +0100482==============================================================================
Bram Moolenaar54775062019-07-31 21:07:14 +02004838. Channel functions details *channel-functions-details*
Bram Moolenaared997ad2019-07-21 16:42:00 +0200484
485ch_canread({handle}) *ch_canread()*
486 Return non-zero when there is something to read from {handle}.
487 {handle} can be a Channel or a Job that has a Channel.
488
489 This is useful to read from a channel at a convenient time,
490 e.g. from a timer.
491
492 Note that messages are dropped when the channel does not have
493 a callback. Add a close callback to avoid that.
494
Bram Moolenaar570497a2019-08-22 22:55:13 +0200495 Can also be used as a |method|: >
496 GetChannel()->ch_canread()
Bram Moolenaared997ad2019-07-21 16:42:00 +0200497
498ch_close({handle}) *ch_close()*
499 Close {handle}. See |channel-close|.
500 {handle} can be a Channel or a Job that has a Channel.
501 A close callback is not invoked.
502
Bram Moolenaar570497a2019-08-22 22:55:13 +0200503 Can also be used as a |method|: >
504 GetChannel()->ch_close()
Bram Moolenaared997ad2019-07-21 16:42:00 +0200505
506ch_close_in({handle}) *ch_close_in()*
507 Close the "in" part of {handle}. See |channel-close-in|.
508 {handle} can be a Channel or a Job that has a Channel.
509 A close callback is not invoked.
510
Bram Moolenaar570497a2019-08-22 22:55:13 +0200511 Can also be used as a |method|: >
512 GetChannel()->ch_close_in()
513
Bram Moolenaared997ad2019-07-21 16:42:00 +0200514
515ch_evalexpr({handle}, {expr} [, {options}]) *ch_evalexpr()*
516 Send {expr} over {handle}. The {expr} is encoded
517 according to the type of channel. The function cannot be used
518 with a raw channel. See |channel-use|.
519 {handle} can be a Channel or a Job that has a Channel.
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +0100520 When using the "lsp" channel mode, {expr} must be a |Dict|.
Bram Moolenaared997ad2019-07-21 16:42:00 +0200521 *E917*
522 {options} must be a Dictionary. It must not have a "callback"
523 entry. It can have a "timeout" entry to specify the timeout
524 for this specific request.
525
526 ch_evalexpr() waits for a response and returns the decoded
527 expression. When there is an error or timeout it returns an
528 empty string.
529
Bram Moolenaar8fe10002019-09-11 22:56:44 +0200530 Note that while waiting for the response, Vim handles other
531 messages. You need to make sure this doesn't cause trouble.
532
Bram Moolenaar570497a2019-08-22 22:55:13 +0200533 Can also be used as a |method|: >
534 GetChannel()->ch_evalexpr(expr)
535
Bram Moolenaared997ad2019-07-21 16:42:00 +0200536
537ch_evalraw({handle}, {string} [, {options}]) *ch_evalraw()*
538 Send {string} over {handle}.
539 {handle} can be a Channel or a Job that has a Channel.
540
541 Works like |ch_evalexpr()|, but does not encode the request or
542 decode the response. The caller is responsible for the
543 correct contents. Also does not add a newline for a channel
544 in NL mode, the caller must do that. The NL in the response
545 is removed.
546 Note that Vim does not know when the text received on a raw
547 channel is complete, it may only return the first part and you
548 need to use |ch_readraw()| to fetch the rest.
549 See |channel-use|.
550
Bram Moolenaar570497a2019-08-22 22:55:13 +0200551 Can also be used as a |method|: >
552 GetChannel()->ch_evalraw(rawstring)
Bram Moolenaared997ad2019-07-21 16:42:00 +0200553
554ch_getbufnr({handle}, {what}) *ch_getbufnr()*
Bram Moolenaar6aa57292021-08-14 21:25:52 +0200555 Get the buffer number that {handle} is using for String {what}.
Bram Moolenaared997ad2019-07-21 16:42:00 +0200556 {handle} can be a Channel or a Job that has a Channel.
557 {what} can be "err" for stderr, "out" for stdout or empty for
558 socket output.
559 Returns -1 when there is no buffer.
560
Bram Moolenaar570497a2019-08-22 22:55:13 +0200561 Can also be used as a |method|: >
562 GetChannel()->ch_getbufnr(what)
Bram Moolenaared997ad2019-07-21 16:42:00 +0200563
564ch_getjob({channel}) *ch_getjob()*
565 Get the Job associated with {channel}.
566 If there is no job calling |job_status()| on the returned Job
567 will result in "fail".
568
Bram Moolenaar570497a2019-08-22 22:55:13 +0200569 Can also be used as a |method|: >
570 GetChannel()->ch_getjob()
571
Bram Moolenaared997ad2019-07-21 16:42:00 +0200572
573ch_info({handle}) *ch_info()*
574 Returns a Dictionary with information about {handle}. The
575 items are:
576 "id" number of the channel
577 "status" "open", "buffered" or "closed", like
578 ch_status()
579 When opened with ch_open():
580 "hostname" the hostname of the address
581 "port" the port of the address
582 "sock_status" "open" or "closed"
583 "sock_mode" "NL", "RAW", "JSON" or "JS"
584 "sock_io" "socket"
585 "sock_timeout" timeout in msec
586 When opened with job_start():
587 "out_status" "open", "buffered" or "closed"
588 "out_mode" "NL", "RAW", "JSON" or "JS"
589 "out_io" "null", "pipe", "file" or "buffer"
590 "out_timeout" timeout in msec
591 "err_status" "open", "buffered" or "closed"
592 "err_mode" "NL", "RAW", "JSON" or "JS"
593 "err_io" "out", "null", "pipe", "file" or "buffer"
594 "err_timeout" timeout in msec
595 "in_status" "open" or "closed"
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +0100596 "in_mode" "NL", "RAW", "JSON", "JS" or "LSP"
Bram Moolenaared997ad2019-07-21 16:42:00 +0200597 "in_io" "null", "pipe", "file" or "buffer"
598 "in_timeout" timeout in msec
599
Bram Moolenaar570497a2019-08-22 22:55:13 +0200600 Can also be used as a |method|: >
601 GetChannel()->ch_info()
602
Bram Moolenaared997ad2019-07-21 16:42:00 +0200603
604ch_log({msg} [, {handle}]) *ch_log()*
Bram Moolenaar6aa57292021-08-14 21:25:52 +0200605 Write String {msg} in the channel log file, if it was opened
606 with |ch_logfile()|.
Bram Moolenaared997ad2019-07-21 16:42:00 +0200607 When {handle} is passed the channel number is used for the
608 message.
609 {handle} can be a Channel or a Job that has a Channel. The
610 Channel must be open for the channel number to be used.
611
Bram Moolenaar570497a2019-08-22 22:55:13 +0200612 Can also be used as a |method|: >
613 'did something'->ch_log()
614
Bram Moolenaared997ad2019-07-21 16:42:00 +0200615
616ch_logfile({fname} [, {mode}]) *ch_logfile()*
617 Start logging channel activity to {fname}.
618 When {fname} is an empty string: stop logging.
619
620 When {mode} is omitted or "a" append to the file.
621 When {mode} is "w" start with an empty file.
622
623 Use |ch_log()| to write log messages. The file is flushed
624 after every message, on Unix you can use "tail -f" to see what
625 is going on in real time.
626
Bram Moolenaar077cc7a2020-09-04 16:35:35 +0200627 To enable the log very early, to see what is received from a
628 terminal during startup, use |--cmd|: >
629 vim --cmd "call ch_logfile('logfile', 'w')"
630<
Bram Moolenaared997ad2019-07-21 16:42:00 +0200631 This function is not available in the |sandbox|.
632 NOTE: the channel communication is stored in the file, be
633 aware that this may contain confidential and privacy sensitive
634 information, e.g. a password you type in a terminal window.
635
Bram Moolenaar570497a2019-08-22 22:55:13 +0200636 Can also be used as a |method|: >
637 'logfile'->ch_logfile('w')
638
Bram Moolenaared997ad2019-07-21 16:42:00 +0200639
640ch_open({address} [, {options}]) *ch_open()*
641 Open a channel to {address}. See |channel|.
642 Returns a Channel. Use |ch_status()| to check for failure.
643
Bram Moolenaar6aa57292021-08-14 21:25:52 +0200644 {address} is a String and has the form "hostname:port", e.g.,
Bram Moolenaared997ad2019-07-21 16:42:00 +0200645 "localhost:8765".
646
Bram Moolenaarbfe13cc2020-04-12 17:53:12 +0200647 When using an IPv6 address, enclose it within square brackets.
648 E.g., "[2001:db8::1]:8765".
649
Bram Moolenaared997ad2019-07-21 16:42:00 +0200650 If {options} is given it must be a |Dictionary|.
651 See |channel-open-options|.
652
Bram Moolenaar570497a2019-08-22 22:55:13 +0200653 Can also be used as a |method|: >
654 GetAddress()->ch_open()
655
Bram Moolenaared997ad2019-07-21 16:42:00 +0200656
657ch_read({handle} [, {options}]) *ch_read()*
658 Read from {handle} and return the received message.
659 {handle} can be a Channel or a Job that has a Channel.
660 For a NL channel this waits for a NL to arrive, except when
661 there is nothing more to read (channel was closed).
662 See |channel-more|.
663
Bram Moolenaar570497a2019-08-22 22:55:13 +0200664 Can also be used as a |method|: >
665 GetChannel()->ch_read()
666
Bram Moolenaared997ad2019-07-21 16:42:00 +0200667
668ch_readblob({handle} [, {options}]) *ch_readblob()*
669 Like ch_read() but reads binary data and returns a |Blob|.
670 See |channel-more|.
671
Bram Moolenaar570497a2019-08-22 22:55:13 +0200672 Can also be used as a |method|: >
673 GetChannel()->ch_readblob()
674
Bram Moolenaared997ad2019-07-21 16:42:00 +0200675
676ch_readraw({handle} [, {options}]) *ch_readraw()*
677 Like ch_read() but for a JS and JSON channel does not decode
678 the message. For a NL channel it does not block waiting for
679 the NL to arrive, but otherwise works like ch_read().
680 See |channel-more|.
681
Bram Moolenaar570497a2019-08-22 22:55:13 +0200682 Can also be used as a |method|: >
683 GetChannel()->ch_readraw()
684
Bram Moolenaared997ad2019-07-21 16:42:00 +0200685
686ch_sendexpr({handle}, {expr} [, {options}]) *ch_sendexpr()*
687 Send {expr} over {handle}. The {expr} is encoded
688 according to the type of channel. The function cannot be used
689 with a raw channel.
690 See |channel-use|. *E912*
691 {handle} can be a Channel or a Job that has a Channel.
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +0100692 When using the "lsp" channel mode, {expr} must be a |Dict|.
Bram Moolenaared997ad2019-07-21 16:42:00 +0200693
Bram Moolenaar570497a2019-08-22 22:55:13 +0200694 Can also be used as a |method|: >
695 GetChannel()->ch_sendexpr(expr)
696
Bram Moolenaared997ad2019-07-21 16:42:00 +0200697
698ch_sendraw({handle}, {expr} [, {options}]) *ch_sendraw()*
699 Send |String| or |Blob| {expr} over {handle}.
700 Works like |ch_sendexpr()|, but does not encode the request or
701 decode the response. The caller is responsible for the
702 correct contents. Also does not add a newline for a channel
703 in NL mode, the caller must do that. The NL in the response
704 is removed.
705 See |channel-use|.
706
Bram Moolenaar570497a2019-08-22 22:55:13 +0200707 Can also be used as a |method|: >
708 GetChannel()->ch_sendraw(rawexpr)
709
Bram Moolenaared997ad2019-07-21 16:42:00 +0200710
711ch_setoptions({handle}, {options}) *ch_setoptions()*
712 Set options on {handle}:
713 "callback" the channel callback
714 "timeout" default read timeout in msec
715 "mode" mode for the whole channel
716 See |ch_open()| for more explanation.
717 {handle} can be a Channel or a Job that has a Channel.
718
719 Note that changing the mode may cause queued messages to be
720 lost.
721
722 These options cannot be changed:
723 "waittime" only applies to |ch_open()|
724
Bram Moolenaar570497a2019-08-22 22:55:13 +0200725 Can also be used as a |method|: >
726 GetChannel()->ch_setoptions(options)
727
Bram Moolenaared997ad2019-07-21 16:42:00 +0200728
729ch_status({handle} [, {options}]) *ch_status()*
730 Return the status of {handle}:
731 "fail" failed to open the channel
732 "open" channel can be used
733 "buffered" channel can be read, not written to
734 "closed" channel can not be used
735 {handle} can be a Channel or a Job that has a Channel.
736 "buffered" is used when the channel was closed but there is
737 still data that can be obtained with |ch_read()|.
738
739 If {options} is given it can contain a "part" entry to specify
740 the part of the channel to return the status for: "out" or
741 "err". For example, to get the error status: >
742 ch_status(job, {"part": "err"})
743<
Bram Moolenaar570497a2019-08-22 22:55:13 +0200744 Can also be used as a |method|: >
745 GetChannel()->ch_status()
Bram Moolenaared997ad2019-07-21 16:42:00 +0200746
747==============================================================================
7489. Starting a job with a channel *job-start* *job*
Bram Moolenaar38a55632016-02-15 22:07:32 +0100749
750To start a job and open a channel for stdin/stdout/stderr: >
751 let job = job_start(command, {options})
752
753You can get the channel with: >
754 let channel = job_getchannel(job)
755
756The channel will use NL mode. If you want another mode it's best to specify
757this in {options}. When changing the mode later some text may have already
758been received and not parsed correctly.
759
760If the command produces a line of output that you want to deal with, specify
761a handler for stdout: >
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100762 let job = job_start(command, {"out_cb": "MyHandler"})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100763The function will be called with the channel and a message. You would define
764it like this: >
765 func MyHandler(channel, msg)
766
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100767Without the handler you need to read the output with |ch_read()| or
Bram Moolenaar06481422016-04-30 15:13:38 +0200768|ch_readraw()|. You can do this in the close callback, see |read-in-close-cb|.
Bram Moolenaar38a55632016-02-15 22:07:32 +0100769
Bram Moolenaar1ccd8ff2017-08-11 19:50:37 +0200770Note that if the job exits before you read the output, the output may be lost.
771This depends on the system (on Unix this happens because closing the write end
772of a pipe causes the read end to get EOF). To avoid this make the job sleep
773for a short while before it exits.
774
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100775The handler defined for "out_cb" will not receive stderr. If you want to
776handle that separately, add an "err_cb" handler: >
777 let job = job_start(command, {"out_cb": "MyHandler",
778 \ "err_cb": "ErrHandler"})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100779
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100780If you want to handle both stderr and stdout with one handler use the
781"callback" option: >
Bram Moolenaar664f3cf2019-12-07 16:03:51 +0100782 let job = job_start(command, {"callback": "MyHandler"})
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100783
Bram Moolenaar3ec574f2017-06-13 18:12:01 +0200784Depending on the system, starting a job can put Vim in the background, the
785started job gets the focus. To avoid that, use the `foreground()` function.
786This might not always work when called early, put in the callback handler or
787use a timer to call it after the job has started.
788
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100789You can send a message to the command with ch_evalraw(). If the channel is in
790JSON or JS mode you can use ch_evalexpr().
Bram Moolenaar38a55632016-02-15 22:07:32 +0100791
792There are several options you can use, see |job-options|.
Bram Moolenaar187db502016-02-27 14:44:26 +0100793For example, to start a job and write its output in buffer "dummy": >
794 let logjob = job_start("tail -f /tmp/log",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100795 \ {'out_io': 'buffer', 'out_name': 'dummy'})
Bram Moolenaar187db502016-02-27 14:44:26 +0100796 sbuf dummy
Bram Moolenaar38a55632016-02-15 22:07:32 +0100797
Bram Moolenaar5f148ec2016-03-07 22:59:26 +0100798
799Job input from a buffer ~
Bram Moolenaar818078d2016-08-27 21:58:42 +0200800 *in_io-buffer*
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100801To run a job that reads from a buffer: >
802 let job = job_start({command},
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100803 \ {'in_io': 'buffer', 'in_name': 'mybuffer'})
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100804<
805 *E915* *E918*
806The buffer is found by name, similar to |bufnr()|. The buffer must exist and
807be loaded when job_start() is called.
808
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100809By default this reads the whole buffer. This can be changed with the "in_top"
810and "in_bot" options.
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100811
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100812A special mode is when "in_top" is set to zero and "in_bot" is not set: Every
Bram Moolenaar74675a62017-07-15 13:53:23 +0200813time a line is added to the buffer, the last-but-one line will be sent to the
Bram Moolenaar5f148ec2016-03-07 22:59:26 +0100814job stdin. This allows for editing the last line and sending it when pressing
815Enter.
Bram Moolenaar0874a832016-09-01 15:11:51 +0200816 *channel-close-in*
817When not using the special mode the pipe or socket will be closed after the
818last line has been written. This signals the reading end that the input
819finished. You can also use |ch_close_in()| to close it sooner.
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100820
Bram Moolenaar063b9d12016-07-09 20:21:48 +0200821NUL bytes in the text will be passed to the job (internally Vim stores these
822as NL bytes).
823
Bram Moolenaar06481422016-04-30 15:13:38 +0200824
825Reading job output in the close callback ~
826 *read-in-close-cb*
827If the job can take some time and you don't need intermediate results, you can
828add a close callback and read the output there: >
829
830 func! CloseHandler(channel)
Bram Moolenaar2ec618c2016-10-01 14:47:05 +0200831 while ch_status(a:channel, {'part': 'out'}) == 'buffered'
Bram Moolenaar06481422016-04-30 15:13:38 +0200832 echomsg ch_read(a:channel)
833 endwhile
834 endfunc
835 let job = job_start(command, {'close_cb': 'CloseHandler'})
836
837You will want to do something more useful than "echomsg".
838
Bram Moolenaar38a55632016-02-15 22:07:32 +0100839==============================================================================
Bram Moolenaared997ad2019-07-21 16:42:00 +020084010. Starting a job without a channel *job-start-nochannel*
Bram Moolenaar38a55632016-02-15 22:07:32 +0100841
842To start another process without creating a channel: >
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100843 let job = job_start(command,
Bram Moolenaar51628222016-12-01 23:03:28 +0100844 \ {"in_io": "null", "out_io": "null", "err_io": "null"})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100845
846This starts {command} in the background, Vim does not wait for it to finish.
847
Bram Moolenaar38a55632016-02-15 22:07:32 +0100848When Vim sees that neither stdin, stdout or stderr are connected, no channel
849will be created. Often you will want to include redirection in the command to
850avoid it getting stuck.
851
852There are several options you can use, see |job-options|.
853
Bram Moolenaar77cdfd12016-03-12 12:57:59 +0100854 *job-start-if-needed*
855To start a job only when connecting to an address does not work, do something
856like this: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100857 let channel = ch_open(address, {"waittime": 0})
858 if ch_status(channel) == "fail"
859 let job = job_start(command)
860 let channel = ch_open(address, {"waittime": 1000})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100861 endif
Bram Moolenaar77cdfd12016-03-12 12:57:59 +0100862
863Note that the waittime for ch_open() gives the job one second to make the port
864available.
Bram Moolenaar38a55632016-02-15 22:07:32 +0100865
866==============================================================================
Bram Moolenaared997ad2019-07-21 16:42:00 +020086711. Job functions *job-functions-details*
868
869job_getchannel({job}) *job_getchannel()*
870 Get the channel handle that {job} is using.
871 To check if the job has no channel: >
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +0200872 if string(job_getchannel(job)) == 'channel fail'
Bram Moolenaared997ad2019-07-21 16:42:00 +0200873<
Bram Moolenaar570497a2019-08-22 22:55:13 +0200874 Can also be used as a |method|: >
875 GetJob()->job_getchannel()
Bram Moolenaared997ad2019-07-21 16:42:00 +0200876
877job_info([{job}]) *job_info()*
878 Returns a Dictionary with information about {job}:
879 "status" what |job_status()| returns
880 "channel" what |job_getchannel()| returns
881 "cmd" List of command arguments used to start the job
882 "process" process ID
883 "tty_in" terminal input name, empty when none
884 "tty_out" terminal output name, empty when none
885 "exitval" only valid when "status" is "dead"
886 "exit_cb" function to be called on exit
887 "stoponexit" |job-stoponexit|
888
889 Only in Unix:
890 "termsig" the signal which terminated the process
891 (See |job_stop()| for the values)
892 only valid when "status" is "dead"
893
894 Only in MS-Windows:
895 "tty_type" Type of virtual console in use.
896 Values are "winpty" or "conpty".
897 See 'termwintype'.
898
899 Without any arguments, returns a List with all Job objects.
900
Bram Moolenaar570497a2019-08-22 22:55:13 +0200901 Can also be used as a |method|: >
902 GetJob()->job_info()
903
Bram Moolenaared997ad2019-07-21 16:42:00 +0200904
905job_setoptions({job}, {options}) *job_setoptions()*
906 Change options for {job}. Supported are:
907 "stoponexit" |job-stoponexit|
908 "exit_cb" |job-exit_cb|
909
Bram Moolenaar570497a2019-08-22 22:55:13 +0200910 Can also be used as a |method|: >
911 GetJob()->job_setoptions(options)
912
Bram Moolenaared997ad2019-07-21 16:42:00 +0200913
914job_start({command} [, {options}]) *job_start()*
915 Start a job and return a Job object. Unlike |system()| and
916 |:!cmd| this does not wait for the job to finish.
917 To start a job in a terminal window see |term_start()|.
918
919 If the job fails to start then |job_status()| on the returned
920 Job object results in "fail" and none of the callbacks will be
921 invoked.
922
923 {command} can be a String. This works best on MS-Windows. On
924 Unix it is split up in white-separated parts to be passed to
925 execvp(). Arguments in double quotes can contain white space.
926
927 {command} can be a List, where the first item is the executable
928 and further items are the arguments. All items are converted
929 to String. This works best on Unix.
930
931 On MS-Windows, job_start() makes a GUI application hidden. If
932 want to show it, Use |:!start| instead.
933
934 The command is executed directly, not through a shell, the
935 'shell' option is not used. To use the shell: >
936 let job = job_start(["/bin/sh", "-c", "echo hello"])
937< Or: >
938 let job = job_start('/bin/sh -c "echo hello"')
939< Note that this will start two processes, the shell and the
940 command it executes. If you don't want this use the "exec"
941 shell command.
942
943 On Unix $PATH is used to search for the executable only when
944 the command does not contain a slash.
945
946 The job will use the same terminal as Vim. If it reads from
947 stdin the job and Vim will be fighting over input, that
948 doesn't work. Redirect stdin and stdout to avoid problems: >
949 let job = job_start(['sh', '-c', "myserver </dev/null >/dev/null"])
950<
951 The returned Job object can be used to get the status with
952 |job_status()| and stop the job with |job_stop()|.
953
954 Note that the job object will be deleted if there are no
955 references to it. This closes the stdin and stderr, which may
956 cause the job to fail with an error. To avoid this keep a
957 reference to the job. Thus instead of: >
958 call job_start('my-command')
959< use: >
960 let myjob = job_start('my-command')
961< and unlet "myjob" once the job is not needed or is past the
962 point where it would fail (e.g. when it prints a message on
963 startup). Keep in mind that variables local to a function
964 will cease to exist if the function returns. Use a
965 script-local variable if needed: >
966 let s:myjob = job_start('my-command')
967<
968 {options} must be a Dictionary. It can contain many optional
969 items, see |job-options|.
970
Bram Moolenaar570497a2019-08-22 22:55:13 +0200971 Can also be used as a |method|: >
972 BuildCommand()->job_start()
973
Bram Moolenaared997ad2019-07-21 16:42:00 +0200974
975job_status({job}) *job_status()* *E916*
976 Returns a String with the status of {job}:
977 "run" job is running
978 "fail" job failed to start
979 "dead" job died or was stopped after running
980
981 On Unix a non-existing command results in "dead" instead of
982 "fail", because a fork happens before the failure can be
983 detected.
984
Bram Moolenaarcb80aa22020-10-26 21:12:46 +0100985 If in Vim9 script a variable is declared with type "job" but
986 never assigned to, passing that variable to job_status()
987 returns "fail".
988
Bram Moolenaared997ad2019-07-21 16:42:00 +0200989 If an exit callback was set with the "exit_cb" option and the
990 job is now detected to be "dead" the callback will be invoked.
991
992 For more information see |job_info()|.
993
Bram Moolenaar570497a2019-08-22 22:55:13 +0200994 Can also be used as a |method|: >
995 GetJob()->job_status()
996
Bram Moolenaared997ad2019-07-21 16:42:00 +0200997
998job_stop({job} [, {how}]) *job_stop()*
999 Stop the {job}. This can also be used to signal the job.
1000
1001 When {how} is omitted or is "term" the job will be terminated.
1002 For Unix SIGTERM is sent. On MS-Windows the job will be
1003 terminated forcedly (there is no "gentle" way).
1004 This goes to the process group, thus children may also be
1005 affected.
1006
1007 Effect for Unix:
1008 "term" SIGTERM (default)
1009 "hup" SIGHUP
1010 "quit" SIGQUIT
1011 "int" SIGINT
1012 "kill" SIGKILL (strongest way to stop)
1013 number signal with that number
1014
1015 Effect for MS-Windows:
1016 "term" terminate process forcedly (default)
1017 "hup" CTRL_BREAK
1018 "quit" CTRL_BREAK
1019 "int" CTRL_C
1020 "kill" terminate process forcedly
1021 Others CTRL_BREAK
1022
1023 On Unix the signal is sent to the process group. This means
1024 that when the job is "sh -c command" it affects both the shell
1025 and the command.
1026
1027 The result is a Number: 1 if the operation could be executed,
1028 0 if "how" is not supported on the system.
1029 Note that even when the operation was executed, whether the
1030 job was actually stopped needs to be checked with
1031 |job_status()|.
1032
1033 If the status of the job is "dead", the signal will not be
1034 sent. This is to avoid to stop the wrong job (esp. on Unix,
1035 where process numbers are recycled).
1036
1037 When using "kill" Vim will assume the job will die and close
1038 the channel.
1039
Bram Moolenaar570497a2019-08-22 22:55:13 +02001040 Can also be used as a |method|: >
1041 GetJob()->job_stop()
1042
Bram Moolenaared997ad2019-07-21 16:42:00 +02001043
1044==============================================================================
104512. Job options *job-options*
Bram Moolenaar38a55632016-02-15 22:07:32 +01001046
1047The {options} argument in job_start() is a dictionary. All entries are
Bram Moolenaar02e83b42016-02-21 20:10:26 +01001048optional. Some options can be used after the job has started, using
1049job_setoptions(job, {options}). Many options can be used with the channel
1050related to the job, using ch_setoptions(channel, {options}).
1051See |job_setoptions()| and |ch_setoptions()|.
Bram Moolenaar38a55632016-02-15 22:07:32 +01001052
Bram Moolenaar4f3f6682016-03-26 23:01:59 +01001053 *in_mode* *out_mode* *err_mode*
1054"in_mode" mode specifically for stdin, only when using pipes
1055"out_mode" mode specifically for stdout, only when using pipes
1056"err_mode" mode specifically for stderr, only when using pipes
1057 See |channel-mode| for the values.
1058
1059 Note: when setting "mode" the part specific mode is
1060 overwritten. Therefore set "mode" first and the part
1061 specific mode later.
1062
1063 Note: when writing to a file or buffer and when
1064 reading from a buffer NL mode is used by default.
1065
Bram Moolenaar0b146882018-09-06 16:27:24 +02001066 *job-noblock*
1067"noblock": 1 When writing use a non-blocking write call. This
1068 avoids getting stuck if Vim should handle other
1069 messages in between, e.g. when a job sends back data
1070 to Vim. It implies that when `ch_sendraw()` returns
1071 not all data may have been written yet.
1072 This option was added in patch 8.1.0350, test with: >
1073 if has("patch-8.1.350")
1074 let options['noblock'] = 1
1075 endif
1076<
Bram Moolenaardecb14d2016-02-20 23:32:02 +01001077 *job-callback*
1078"callback": handler Callback for something to read on any part of the
1079 channel.
Bram Moolenaar4f3f6682016-03-26 23:01:59 +01001080 *job-out_cb* *out_cb*
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001081"out_cb": handler Callback for when there is something to read on
Bram Moolenaar4f3f6682016-03-26 23:01:59 +01001082 stdout. Only for when the channel uses pipes. When
1083 "out_cb" wasn't set the channel callback is used.
Bram Moolenaar269f5952016-07-15 22:54:41 +02001084 The two arguments are the channel and the message.
Bram Moolenaar4f3f6682016-03-26 23:01:59 +01001085
1086 *job-err_cb* *err_cb*
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001087"err_cb": handler Callback for when there is something to read on
Bram Moolenaar4f3f6682016-03-26 23:01:59 +01001088 stderr. Only for when the channel uses pipes. When
1089 "err_cb" wasn't set the channel callback is used.
Bram Moolenaar269f5952016-07-15 22:54:41 +02001090 The two arguments are the channel and the message.
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001091 *job-close_cb*
1092"close_cb": handler Callback for when the channel is closed. Same as
Bram Moolenaar82af8712016-06-04 20:20:29 +02001093 "close_cb" on |ch_open()|, see |close_cb|.
Bram Moolenaarbc2eada2017-01-02 21:27:47 +01001094 *job-drop*
Bram Moolenaarb6e0ec62017-07-23 22:12:20 +02001095"drop": when Specifies when to drop messages. Same as "drop" on
Bram Moolenaar51628222016-12-01 23:03:28 +01001096 |ch_open()|, see |channel-drop|. For "auto" the
1097 exit_cb is not considered.
Bram Moolenaarbc2eada2017-01-02 21:27:47 +01001098 *job-exit_cb*
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001099"exit_cb": handler Callback for when the job ends. The arguments are the
Bram Moolenaar38a55632016-02-15 22:07:32 +01001100 job and the exit status.
Bram Moolenaarb4ada792016-10-30 21:55:26 +01001101 Vim checks up to 10 times per second for jobs that
1102 ended. The check can also be triggered by calling
1103 |job_status()|, which may then invoke the exit_cb
1104 handler.
Bram Moolenaar06d2d382016-05-20 17:24:11 +02001105 Note that data can be buffered, callbacks may still be
1106 called after the process ends.
Bram Moolenaar4f3f6682016-03-26 23:01:59 +01001107 *job-timeout*
Bram Moolenaarb6e0ec62017-07-23 22:12:20 +02001108"timeout": time The time to wait for a request when blocking, E.g.
Bram Moolenaar4f3f6682016-03-26 23:01:59 +01001109 when using ch_evalexpr(). In milliseconds. The
1110 default is 2000 (2 seconds).
1111 *out_timeout* *err_timeout*
Bram Moolenaarb6e0ec62017-07-23 22:12:20 +02001112"out_timeout": time Timeout for stdout. Only when using pipes.
1113"err_timeout": time Timeout for stderr. Only when using pipes.
Bram Moolenaar4f3f6682016-03-26 23:01:59 +01001114 Note: when setting "timeout" the part specific mode is
1115 overwritten. Therefore set "timeout" first and the
1116 part specific mode later.
1117
Bram Moolenaar02e83b42016-02-21 20:10:26 +01001118 *job-stoponexit*
1119"stoponexit": {signal} Send {signal} to the job when Vim exits. See
1120 |job_stop()| for possible values.
1121"stoponexit": "" Do not stop the job when Vim exits.
1122 The default is "term".
1123
Bram Moolenaar77cdfd12016-03-12 12:57:59 +01001124 *job-term*
Bram Moolenaarb6e0ec62017-07-23 22:12:20 +02001125"term": "open" Start a terminal in a new window and connect the job
1126 stdin/stdout/stderr to it. Similar to using
1127 `:terminal`.
Bram Moolenaar77cdfd12016-03-12 12:57:59 +01001128 NOTE: Not implemented yet!
Bram Moolenaar38a55632016-02-15 22:07:32 +01001129
Bram Moolenaar77cdfd12016-03-12 12:57:59 +01001130"channel": {channel} Use an existing channel instead of creating a new one.
1131 The parts of the channel that get used for the new job
1132 will be disconnected from what they were used before.
Bram Moolenaar51628222016-12-01 23:03:28 +01001133 If the channel was still used by another job this may
Bram Moolenaar77cdfd12016-03-12 12:57:59 +01001134 cause I/O errors.
1135 Existing callbacks and other settings remain.
1136
Bram Moolenaarb6e0ec62017-07-23 22:12:20 +02001137"pty": 1 Use a pty (pseudo-tty) instead of a pipe when
1138 possible. This is most useful in combination with a
1139 terminal window, see |terminal|.
1140 {only on Unix and Unix-like systems}
1141
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001142 *job-in_io* *in_top* *in_bot* *in_name* *in_buf*
1143"in_io": "null" disconnect stdin (read from /dev/null)
1144"in_io": "pipe" stdin is connected to the channel (default)
1145"in_io": "file" stdin reads from a file
1146"in_io": "buffer" stdin reads from a buffer
1147"in_top": number when using "buffer": first line to send (default: 1)
1148"in_bot": number when using "buffer": last line to send (default: last)
1149"in_name": "/path/file" the name of the file or buffer to read from
1150"in_buf": number the number of the buffer to read from
Bram Moolenaar38a55632016-02-15 22:07:32 +01001151
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001152 *job-out_io* *out_name* *out_buf*
1153"out_io": "null" disconnect stdout (goes to /dev/null)
1154"out_io": "pipe" stdout is connected to the channel (default)
1155"out_io": "file" stdout writes to a file
Bram Moolenaar51628222016-12-01 23:03:28 +01001156"out_io": "buffer" stdout appends to a buffer (see below)
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001157"out_name": "/path/file" the name of the file or buffer to write to
1158"out_buf": number the number of the buffer to write to
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001159"out_modifiable": 0 when writing to a buffer, 'modifiable' will be off
1160 (see below)
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001161"out_msg": 0 when writing to a new buffer, the first line will be
1162 set to "Reading from channel output..."
Bram Moolenaar38a55632016-02-15 22:07:32 +01001163
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001164 *job-err_io* *err_name* *err_buf*
1165"err_io": "out" stderr messages to go to stdout
1166"err_io": "null" disconnect stderr (goes to /dev/null)
1167"err_io": "pipe" stderr is connected to the channel (default)
1168"err_io": "file" stderr writes to a file
Bram Moolenaar51628222016-12-01 23:03:28 +01001169"err_io": "buffer" stderr appends to a buffer (see below)
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001170"err_name": "/path/file" the name of the file or buffer to write to
1171"err_buf": number the number of the buffer to write to
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001172"err_modifiable": 0 when writing to a buffer, 'modifiable' will be off
1173 (see below)
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001174"err_msg": 0 when writing to a new buffer, the first line will be
1175 set to "Reading from channel error..."
Bram Moolenaar77cdfd12016-03-12 12:57:59 +01001176
Bram Moolenaar7db8f6f2016-03-29 23:12:46 +02001177"block_write": number only for testing: pretend every other write to stdin
1178 will block
1179
Bram Moolenaar05aafed2017-08-11 19:12:11 +02001180"env": dict environment variables for the new process
1181"cwd": "/path/to/dir" current working directory for the new process;
1182 if the directory does not exist an error is given
1183
Bram Moolenaar77cdfd12016-03-12 12:57:59 +01001184
1185Writing to a buffer ~
Bram Moolenaar818078d2016-08-27 21:58:42 +02001186 *out_io-buffer*
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001187When the out_io or err_io mode is "buffer" and there is a callback, the text
Bram Moolenaar5f148ec2016-03-07 22:59:26 +01001188is appended to the buffer before invoking the callback.
1189
1190When a buffer is used both for input and output, the output lines are put
1191above the last line, since the last line is what is written to the channel
Bram Moolenaar77cdfd12016-03-12 12:57:59 +01001192input. Otherwise lines are appended below the last line.
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001193
Bram Moolenaar328da0d2016-03-04 22:22:32 +01001194When using JS or JSON mode with "buffer", only messages with zero or negative
1195ID will be added to the buffer, after decoding + encoding. Messages with a
1196positive number will be handled by a callback, commands are handled as usual.
1197
Bram Moolenaar82af8712016-06-04 20:20:29 +02001198The name of the buffer from "out_name" or "err_name" is compared the full name
1199of existing buffers, also after expanding the name for the current directory.
1200E.g., when a buffer was created with ":edit somename" and the buffer name is
1201"somename" it will use that buffer.
1202
1203If there is no matching buffer a new buffer is created. Use an empty name to
1204always create a new buffer. |ch_getbufnr()| can then be used to get the
1205buffer number.
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01001206
1207For a new buffer 'buftype' is set to "nofile" and 'bufhidden' to "hide". If
1208you prefer other settings, create the buffer first and pass the buffer number.
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001209 *out_modifiable* *err_modifiable*
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001210The "out_modifiable" and "err_modifiable" options can be used to set the
1211'modifiable' option off, or write to a buffer that has 'modifiable' off. That
1212means that lines will be appended to the buffer, but the user can't easily
1213change the buffer.
Bram Moolenaar169ebb02016-09-07 23:32:23 +02001214 *out_msg* *err_msg*
1215The "out_msg" option can be used to specify whether a new buffer will have the
1216first line set to "Reading from channel output...". The default is to add the
1217message. "err_msg" does the same for channel error.
1218
Bram Moolenaar9f5842e2016-05-29 16:17:08 +02001219When an existing buffer is to be written where 'modifiable' is off and the
1220"out_modifiable" or "err_modifiable" options is not zero, an error is given
1221and the buffer will not be written to.
1222
Bram Moolenaar187db502016-02-27 14:44:26 +01001223When the buffer written to is displayed in a window and the cursor is in the
1224first column of the last line, the cursor will be moved to the newly added
1225line and the window is scrolled up to show the cursor if needed.
1226
Bram Moolenaar063b9d12016-07-09 20:21:48 +02001227Undo is synced for every added line. NUL bytes are accepted (internally Vim
1228stores these as NL bytes).
Bram Moolenaar38a55632016-02-15 22:07:32 +01001229
Bram Moolenaar77cdfd12016-03-12 12:57:59 +01001230
1231Writing to a file ~
Bram Moolenaard6c2f052016-03-14 23:22:59 +01001232 *E920*
Bram Moolenaar77cdfd12016-03-12 12:57:59 +01001233The file is created with permissions 600 (read-write for the user, not
1234accessible for others). Use |setfperm()| to change this.
1235
1236If the file already exists it is truncated.
1237
Bram Moolenaar38a55632016-02-15 22:07:32 +01001238==============================================================================
Bram Moolenaared997ad2019-07-21 16:42:00 +0200123913. Controlling a job *job-control*
Bram Moolenaar38a55632016-02-15 22:07:32 +01001240
1241To get the status of a job: >
1242 echo job_status(job)
1243
1244To make a job stop running: >
1245 job_stop(job)
1246
1247This is the normal way to end a job. On Unix it sends a SIGTERM to the job.
1248It is possible to use other ways to stop the job, or even send arbitrary
1249signals. E.g. to force a job to stop, "kill it": >
1250 job_stop(job, "kill")
1251
1252For more options see |job_stop()|.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001253
Bram Moolenaarf2732452018-06-03 14:47:35 +02001254==============================================================================
Bram Moolenaared997ad2019-07-21 16:42:00 +0200125514. Using a prompt buffer *prompt-buffer*
Bram Moolenaarf2732452018-06-03 14:47:35 +02001256
1257If you want to type input for the job in a Vim window you have a few options:
1258- Use a normal buffer and handle all possible commands yourself.
1259 This will be complicated, since there are so many possible commands.
1260- Use a terminal window. This works well if what you type goes directly to
1261 the job and the job output is directly displayed in the window.
1262 See |terminal-window|.
Bram Moolenaaracc22402020-06-07 21:07:18 +02001263- Use a window with a prompt buffer. This works well when entering a line for
1264 the job in Vim while displaying (possibly filtered) output from the job.
Bram Moolenaarf2732452018-06-03 14:47:35 +02001265
1266A prompt buffer is created by setting 'buftype' to "prompt". You would
1267normally only do that in a newly created buffer.
1268
1269The user can edit and enter one line of text at the very last line of the
1270buffer. When pressing Enter in the prompt line the callback set with
1271|prompt_setcallback()| is invoked. It would normally send the line to a job.
1272Another callback would receive the output from the job and display it in the
1273buffer, below the prompt (and above the next prompt).
1274
1275Only the text in the last line, after the prompt, is editable. The rest of the
1276buffer is not modifiable with Normal mode commands. It can be modified by
1277calling functions, such as |append()|. Using other commands may mess up the
1278buffer.
1279
1280After setting 'buftype' to "prompt" Vim does not automatically start Insert
1281mode, use `:startinsert` if you want to enter Insert mode, so that the user
1282can start typing a line.
1283
Bram Moolenaar077cc7a2020-09-04 16:35:35 +02001284The text of the prompt can be set with the |prompt_setprompt()| function. If
1285no prompt is set with |prompt_setprompt()|, "% " is used. You can get the
1286effective prompt text for a buffer, with |prompt_getprompt()|.
Bram Moolenaarf2732452018-06-03 14:47:35 +02001287
1288The user can go to Normal mode and navigate through the buffer. This can be
Bram Moolenaar7ff78462020-07-10 22:00:53 +02001289useful to see older output or copy text.
Bram Moolenaarf2732452018-06-03 14:47:35 +02001290
Bram Moolenaard2f3a8b2018-06-19 14:35:59 +02001291The CTRL-W key can be used to start a window command, such as CTRL-W w to
1292switch to the next window. This also works in Insert mode (use Shift-CTRL-W
1293to delete a word). When leaving the window Insert mode will be stopped. When
1294coming back to the prompt window Insert mode will be restored.
1295
Bram Moolenaarf2732452018-06-03 14:47:35 +02001296Any command that starts Insert mode, such as "a", "i", "A" and "I", will move
Bram Moolenaard2f3a8b2018-06-19 14:35:59 +02001297the cursor to the last line. "A" will move to the end of the line, "I" to the
1298start of the line.
Bram Moolenaarf2732452018-06-03 14:47:35 +02001299
Bram Moolenaaracc22402020-06-07 21:07:18 +02001300Here is an example for Unix. It starts a shell in the background and prompts
1301for the next shell command. Output from the shell is displayed above the
1302prompt. >
1303
1304 " Create a channel log so we can see what happens.
1305 call ch_logfile('logfile', 'w')
1306
Bram Moolenaar1588bc82022-03-08 21:35:07 +00001307 " Function handling a line of text that has been typed.
Bram Moolenaaracc22402020-06-07 21:07:18 +02001308 func TextEntered(text)
1309 " Send the text to a shell with Enter appended.
1310 call ch_sendraw(g:shell_job, a:text .. "\n")
1311 endfunc
Bram Moolenaarcb80aa22020-10-26 21:12:46 +01001312
Bram Moolenaar1588bc82022-03-08 21:35:07 +00001313 " Function handling output from the shell: Add it above the prompt.
Bram Moolenaaracc22402020-06-07 21:07:18 +02001314 func GotOutput(channel, msg)
Bram Moolenaarc51cf032022-02-26 12:25:45 +00001315 call append(line("$") - 1, "- " .. a:msg)
Bram Moolenaaracc22402020-06-07 21:07:18 +02001316 endfunc
1317
Bram Moolenaar1588bc82022-03-08 21:35:07 +00001318 " Function handling the shell exits: close the window.
Bram Moolenaaracc22402020-06-07 21:07:18 +02001319 func JobExit(job, status)
1320 quit!
1321 endfunc
1322
1323 " Start a shell in the background.
1324 let shell_job = job_start(["/bin/sh"], #{
1325 \ out_cb: function('GotOutput'),
1326 \ err_cb: function('GotOutput'),
1327 \ exit_cb: function('JobExit'),
1328 \ })
Bram Moolenaaracc22402020-06-07 21:07:18 +02001329
1330 new
1331 set buftype=prompt
1332 let buf = bufnr('')
1333 call prompt_setcallback(buf, function("TextEntered"))
1334 eval prompt_setprompt(buf, "shell command: ")
1335
1336 " start accepting shell commands
1337 startinsert
1338<
Bram Moolenaar1588bc82022-03-08 21:35:07 +00001339The same in |Vim9| script: >
1340
1341 vim9script
1342
1343 # Create a channel log so we can see what happens.
1344 ch_logfile('logfile', 'w')
1345
1346 var shell_job: job
1347
1348 # Function handling a line of text that has been typed.
1349 def TextEntered(text: string)
1350 # Send the text to a shell with Enter appended.
1351 ch_sendraw(shell_job, text .. "\n")
1352 enddef
1353
1354 # Function handling output from the shell: Add it above the prompt.
1355 def GotOutput(channel: channel, msg: string)
1356 append(line("$") - 1, "- " .. msg)
1357 enddef
1358
1359 # Function handling the shell exits: close the window.
1360 def JobExit(job: job, status: number)
1361 quit!
1362 enddef
1363
1364 # Start a shell in the background.
1365 shell_job = job_start(["/bin/sh"], {
1366 out_cb: GotOutput,
1367 err_cb: GotOutput,
1368 exit_cb: JobExit,
1369 })
1370
1371 new
1372 set buftype=prompt
1373 var buf = bufnr('')
1374 prompt_setcallback(buf, TextEntered)
1375 prompt_setprompt(buf, "shell command: ")
1376
1377 # start accepting shell commands
1378 startinsert
Bram Moolenaaracc22402020-06-07 21:07:18 +02001379
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +01001380==============================================================================
138114. Language Server Protocol *language-server-protocol*
1382
1383The language server protocol specification is available at:
1384
1385 https://microsoft.github.io/language-server-protocol/specification
1386
1387Each LSP protocol message starts with a simple HTTP header followed by the
1388payload encoded in JSON-RPC format. This is described in:
1389
1390 https://www.jsonrpc.org/specification
1391
1392For messages received on a channel with mode set to "lsp", Vim will process
1393the HTTP header and decode the payload into a Vim |Dict| type and call the
1394channel callback or the specified callback function. When sending messages on
1395a channel using |ch_evalexpr()| or |ch_sendexpr()|, Vim will add the HTTP
1396header and encode the Vim expression into JSON-RPC.
1397
1398To open a channel using the 'lsp' mode, set the 'mode' item in the |ch_open()|
1399{options} argument to 'lsp'. Example: >
1400
1401 let ch = ch_open(..., #{mode: 'lsp'})
1402
1403To open a channel using the 'lsp' mode with a job, set the 'in_mode' and
1404'out_mode' items in the |job_start()| {options} argument to 'lsp'. Example: >
1405
1406 let job = job_start(...., #{in_mode: 'lsp', out_mode: 'lsp'})
1407
1408To synchronously send a JSON-RPC request to the server, use the |ch_evalexpr()|
1409function. This function will return the response from the server. You can use
1410the 'timeout' field in the {options} argument to control the response wait
1411time. Example: >
1412
1413 let req = {}
1414 let req.method = 'textDocument/definition'
1415 let req.params = {}
1416 let req.params.textDocument = #{uri: 'a.c'}
1417 let req.params.position = #{line: 10, character: 3}
1418 let resp = ch_evalexpr(ch, req, #{timeout: 100})
1419
1420Note that in the request message the 'id' field should not be specified. If it
1421is specified, then Vim will overwrite the value with an internally generated
1422identifier. Vim currently supports only a number type for the 'id' field.
1423
1424To send a JSON-RPC request to the server and asynchronously process the
1425response, use the |ch_sendexpr()| function and supply a callback function.
1426Example: >
1427
1428 let req = {}
1429 let req.method = 'textDocument/hover'
1430 let req.params = {}
1431 let req.params.textDocument = #{uri: 'a.c'}
1432 let req.params.position = #{line: 10, character: 3}
1433 let resp = ch_sendexpr(ch, req, #{callback: 'MyFn'})
1434
1435To send a JSON-RPC notification message to the server, use the |ch_sendexpr()|
1436function. Example: >
1437
1438 call ch_sendexpr(ch, #{method: 'initialized'})
1439
1440To respond to a JSON-RPC request message from the server, use the
1441|ch_sendexpr()| function. In the response message, copy the 'id' field value
1442from the server request message. Example: >
1443
1444 let resp = {}
1445 let resp.id = req.id
1446 let resp.result = 1
1447 call ch_sendexpr(ch, resp)
1448
1449The JSON-RPC notification messages from the server are delivered through the
1450|channel-callback| function.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01001451
Bram Moolenaar91f84f62018-07-29 15:07:52 +02001452 vim:tw=78:ts=8:noet:ft=help:norl: