blob: 2f67b973e89b095da6801fa9020d4e73cbb69908 [file] [log] [blame]
Bram Moolenaar91c49372016-05-08 09:50:29 +02001*channel.txt* For Vim version 7.4. Last change: 2016 Apr 30
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002
3
4 VIM REFERENCE MANUAL by Bram Moolenaar
5
6
7 Inter-process communication *channel*
8
9DRAFT DRAFT DRAFT DRAFT DRAFT DRAFT DRAFT DRAFT DRAFT DRAFT
10
11Vim uses channels to communicate with other processes.
Bram Moolenaar38a55632016-02-15 22:07:32 +010012A channel uses a socket or pipes *socket-interface*
13Jobs can be used to start processes and communicate with them.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010014
Bram Moolenaar681baaf2016-02-04 20:57:07 +010015Vim current supports up to 10 simultaneous channels.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010016The Netbeans interface also uses a channel. |netbeans|
17
Bram Moolenaar38a55632016-02-15 22:07:32 +0100181. Overview |job-channel-overview|
192. Channel demo |channel-demo|
203. Opening a channel |channel-open|
214. Using a JSON or JS channel |channel-use|
225. Channel commands |channel-commands|
236. Using a RAW or NL channel |channel-raw|
247. More channel functions |channel-more|
258. Starting a job with a channel |job-start|
269. Starting a job without a channel |job-start-nochannel|
2710. Job options |job-options|
2811. Controlling a job |job-control|
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010029
30{Vi does not have any of these features}
Bram Moolenaar38a55632016-02-15 22:07:32 +010031{only when compiled with the |+channel| feature for channel stuff}
32{only when compiled with the |+job| feature for job stuff}
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:
381. A deamon, serving several Vim instances.
39 Vim connects to it with a socket.
402. One job working with one Vim instance, asynchronously.
41 Uses a socket or pipes.
423. A job performing some work for a short time, asynchronously.
43 Uses a socket or pipes.
444. Running a filter, synchronously.
45 Uses pipes.
46
Bram Moolenaar77cdfd12016-03-12 12:57:59 +010047For when using sockets See |job-start|, |job-start-nochannel| and
48|channel-open|. For 2 and 3, one or more jobs using pipes, see |job-start|.
Bram Moolenaar38a55632016-02-15 22:07:32 +010049For 4 use the ":{range}!cmd" command, see |filter|.
50
51Over the socket and pipes these protocols are available:
52RAW nothing known, Vim cannot tell where a message ends
53NL every message ends in a NL (newline) character
54JSON JSON encoding |json_encode()|
55JS JavaScript style JSON-like encoding |js_encode()|
56
57Common combination are:
58- Using a job connected through pipes in NL mode. E.g., to run a style
59 checker and receive errors and warnings.
60- Using a deamon, connecting over a socket in JSON mode. E.g. to lookup
61 crosss-refrences in a database.
62
63==============================================================================
642. Channel demo *channel-demo*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010065
66This requires Python. The demo program can be found in
67$VIMRUNTIME/tools/demoserver.py
68Run it in one terminal. We will call this T1.
69
70Run Vim in another terminal. Connect to the demo server with: >
Bram Moolenaar38a55632016-02-15 22:07:32 +010071 let channel = ch_open('localhost:8765')
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010072
73In T1 you should see:
74 === socket opened === ~
75
76You can now send a message to the server: >
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010077 echo ch_evalexpr(channel, 'hello!')
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010078
79The message is received in T1 and a response is sent back to Vim.
80You can see the raw messages in T1. What Vim sends is:
81 [1,"hello!"] ~
82And the response is:
83 [1,"got it"] ~
84The number will increase every time you send a message.
85
86The server can send a command to Vim. Type this on T1 (literally, including
Bram Moolenaarfb1f6262016-01-31 20:24:32 +010087the quotes):
88 ["ex","echo 'hi there'"] ~
89And you should see the message in Vim. You can move the cursor a word forward:
90 ["normal","w"] ~
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010091
92To handle asynchronous communication a callback needs to be used: >
Bram Moolenaar38a55632016-02-15 22:07:32 +010093 func MyHandler(channel, msg)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010094 echo "from the handler: " . a:msg
95 endfunc
Bram Moolenaar02e83b42016-02-21 20:10:26 +010096 call ch_sendexpr(channel, 'hello!', {'callback': "MyHandler"})
Bram Moolenaar38a55632016-02-15 22:07:32 +010097Vim will not wait for a response. Now the server can send the response later
98and MyHandler will be invoked.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010099
100Instead of giving a callback with every send call, it can also be specified
101when opening the channel: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100102 call ch_close(channel)
103 let channel = ch_open('localhost:8765', {'callback': "MyHandler"})
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100104 call ch_sendexpr(channel, 'hello!')
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100105
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100106When trying out channels it's useful to see what is going on. You can tell
107Vim to write lines in log file: >
108 call ch_logfile('channellog', 'w')
109See |ch_logfile()|.
110
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100111==============================================================================
Bram Moolenaar38a55632016-02-15 22:07:32 +01001123. Opening a channel *channel-open*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100113
Bram Moolenaar681baaf2016-02-04 20:57:07 +0100114To open a channel: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100115 let channel = ch_open({address} [, {options}])
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100116 if ch_status(channel) == "open"
117 " use the channel
Bram Moolenaar38a55632016-02-15 22:07:32 +0100118
119Use |ch_status()| to see if the channel could be opened.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100120
121{address} has the form "hostname:port". E.g., "localhost:8765".
122
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100123{options} is a dictionary with optional entries: *channel-open-options*
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100124
125"mode" can be: *channel-mode*
126 "json" - Use JSON, see below; most convenient way. Default.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100127 "js" - Use JS (JavaScript) encoding, more efficient than JSON.
Bram Moolenaar38a55632016-02-15 22:07:32 +0100128 "nl" - Use messages that end in a NL character
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100129 "raw" - Use raw messages
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100130 *channel-callback* *E921*
Bram Moolenaar38a55632016-02-15 22:07:32 +0100131"callback" A function that is called when a message is received that is
132 not handled otherwise. It gets two arguments: the channel
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100133 and the received message. Example: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100134 func Handle(channel, msg)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100135 echo 'Received: ' . a:msg
136 endfunc
Bram Moolenaar38a55632016-02-15 22:07:32 +0100137 let channel = ch_open("localhost:8765", {"callback": "Handle"})
138<
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100139 When "mode" is "json" or "js" the "msg" argument is the body
140 of the received message, converted to Vim types.
141 When "mode" is "nl" the "msg" argument is one message,
142 excluding the NL.
143 When "mode" is "raw" the "msg" argument is the whole message
144 as a string.
Bram Moolenaare18c0b32016-03-20 21:08:34 +0100145
146 For all callbacks: Use |function()| to bind it to arguments
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100147 and/or a Dictionary. Or use the form "dict.function" to bind
148 the Dictionary.
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100149 *close_cb*
150"close_cb" A function that is called when the channel gets closed, other
Bram Moolenaar38a55632016-02-15 22:07:32 +0100151 than by calling ch_close(). It should be defined like this: >
152 func MyCloseHandler(channel)
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100153< *waittime*
Bram Moolenaar38a55632016-02-15 22:07:32 +0100154"waittime" The time to wait for the connection to be made in
Bram Moolenaarf3913272016-02-25 00:00:01 +0100155 milliseconds. A negative number waits forever.
156
157 The default is zero, don't wait, which is useful if a local
158 server is supposed to be running already. On Unix Vim
159 actually uses a 1 msec timeout, that is required on many
160 systems. Use a larger value for a remote server, e.g. 10
161 msec at least.
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100162 *channel-timeout*
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100163"timeout" The time to wait for a request when blocking, E.g. when using
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100164 ch_evalexpr(). In milliseconds. The default is 2000 (2
Bram Moolenaar38a55632016-02-15 22:07:32 +0100165 seconds).
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100166
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100167When "mode" is "json" or "js" the "callback" is optional. When omitted it is
168only possible to receive a message after sending one.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100169
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100170To change the channel options after opening it use |ch_setoptions()|. The
171arguments are similar to what is passed to |ch_open()|, but "waittime" cannot
172be given, since that only applies to opening the channel.
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100173
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100174For example, the handler can be added or changed: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100175 call ch_setoptions(channel, {'callback': callback})
176When "callback" is empty (zero or an empty string) the handler is removed.
177
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100178After a callback has been invoked Vim will update the screen and put the
179cursor back where it belongs. Thus the callback should not need to do
180`:redraw`.
181
Bram Moolenaar38a55632016-02-15 22:07:32 +0100182The timeout can be changed: >
183 call ch_setoptions(channel, {'timeout': msec})
184<
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100185 *channel-close* *E906*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100186Once done with the channel, disconnect it like this: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100187 call ch_close(channel)
188When a socket is used this will close the socket for both directions. When
189pipes are used (stdin/stdout/stderr) they are all closed. This might not be
190what you want! Stopping the job with job_stop() might be better.
Bram Moolenaar187db502016-02-27 14:44:26 +0100191All readahead is discarded, callbacks will no longer be invoked.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100192
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100193Note that a channel is closed in three stages:
194 - The I/O ends, log message: "Closing channel". There can still be queued
195 messages to read or callbacks to invoke.
196 - The readahead is cleared, log message: "Clearing channel". Some variables
197 may still reference the channel.
198 - The channel is freed, log message: "Freeing channel".
199
Bram Moolenaarcbebd482016-02-07 23:02:56 +0100200When the channel can't be opened you will get an error message. There is a
201difference between MS-Windows and Unix: On Unix when the port doesn't exist
202ch_open() fails quickly. On MS-Windows "waittime" applies.
Bram Moolenaaraa3b15d2016-04-21 08:53:19 +0200203*E898* *E901* *E902*
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100204
205If there is an error reading or writing a channel it will be closed.
Bram Moolenaaraa3b15d2016-04-21 08:53:19 +0200206*E630* *E631*
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100207
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100208==============================================================================
Bram Moolenaar38a55632016-02-15 22:07:32 +01002094. Using a JSON or JS channel *channel-use*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100210
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100211If mode is JSON then a message can be sent synchronously like this: >
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100212 let response = ch_evalexpr(channel, {expr})
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100213This awaits a response from the other side.
214
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100215When mode is JS this works the same, except that the messages use
Bram Moolenaar38a55632016-02-15 22:07:32 +0100216JavaScript encoding. See |js_encode()| for the difference.
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100217
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100218To send a message, without handling a response or letting the channel callback
219handle the response: >
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100220 call ch_sendexpr(channel, {expr})
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100221
222To send a message and letting the response handled by a specific function,
223asynchronously: >
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100224 call ch_sendexpr(channel, {expr}, {'callback': Handler})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100225
226Vim will match the response with the request using the message ID. Once the
227response is received the callback will be invoked. Further responses with the
228same ID will be ignored. If your server sends back multiple responses you
229need to send them with ID zero, they will be passed to the channel callback.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100230
231The {expr} is converted to JSON and wrapped in an array. An example of the
232message that the receiver will get when {expr} is the string "hello":
233 [12,"hello"] ~
234
235The format of the JSON sent is:
236 [{number},{expr}]
237
238In which {number} is different every time. It must be used in the response
239(if any):
240
241 [{number},{response}]
242
243This way Vim knows which sent message matches with which received message and
244can call the right handler. Also when the messages arrive out of order.
245
246The sender must always send valid JSON to Vim. Vim can check for the end of
247the message by parsing the JSON. It will only accept the message if the end
248was received.
249
250When the process wants to send a message to Vim without first receiving a
251message, it must use the number zero:
252 [0,{response}]
253
254Then channel handler will then get {response} converted to Vim types. If the
255channel does not have a handler the message is dropped.
256
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100257It is also possible to use ch_sendraw() and ch_evalraw() on a JSON or JS
258channel. The caller is then completely responsible for correct encoding and
259decoding.
Bram Moolenaarcbebd482016-02-07 23:02:56 +0100260
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100261==============================================================================
Bram Moolenaar38a55632016-02-15 22:07:32 +01002625. Channel commands *channel-commands*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100263
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100264With a JSON channel the process can send commands to Vim that will be
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100265handled by Vim internally, it does not require a handler for the channel.
266
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100267Possible commands are: *E903* *E904* *E905*
268 ["redraw" {forced}]
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100269 ["ex", {Ex command}]
270 ["normal", {Normal mode command}]
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100271 ["expr", {expression}, {number}]
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100272 ["expr", {expression}]
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100273 ["call", {func name}, {argument list}, {number}]
274 ["call", {func name}, {argument list}]
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100275
276With all of these: Be careful what these commands do! You can easily
277interfere with what the user is doing. To avoid trouble use |mode()| to check
278that the editor is in the expected state. E.g., to send keys that must be
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100279inserted as text, not executed as a command:
280 ["ex","if mode() == 'i' | call feedkeys('ClassName') | endif"] ~
281
282Errors in these commands are normally not reported to avoid them messing up
283the display. If you do want to see them, set the 'verbose' option to 3 or
284higher.
285
286
287Command "redraw" ~
288
289The other commands do not update the screen, so that you can send a sequence
290of commands without the cursor moving around. You must end with the "redraw"
291command to show any changed text and show the cursor where it belongs.
292
293The argument is normally an empty string:
294 ["redraw", ""] ~
295To first clear the screen pass "force":
296 ["redraw", "force"] ~
297
298
299Command "ex" ~
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100300
301The "ex" command is executed as any Ex command. There is no response for
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100302completion or error. You could use functions in an |autoload| script:
303 ["ex","call myscript#MyFunc(arg)"]
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100304
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100305You can also use "call |feedkeys()|" to insert any key sequence.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100306
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100307When there is an error a message is written to the channel log, if it exists,
308and v:errmsg is set to the error.
309
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100310
311Command "normal" ~
312
Bram Moolenaar681baaf2016-02-04 20:57:07 +0100313The "normal" command is executed like with ":normal!", commands are not
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100314mapped. Example to open the folds under the cursor:
315 ["normal" "zO"]
316
317
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100318Command "expr" with response ~
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100319
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100320The "expr" command can be used to get the result of an expression. For
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100321example, to get the number of lines in the current buffer:
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100322 ["expr","line('$')", -2] ~
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100323
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100324It will send back the result of the expression:
Bram Moolenaare0fa3742016-02-20 15:47:01 +0100325 [-2, "last line"] ~
326The format is:
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100327 [{number}, {result}]
Bram Moolenaar187db502016-02-27 14:44:26 +0100328
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100329Here {number} is the same as what was in the request. Use a negative number
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100330to avoid confusion with message that Vim sends. Use a different number on
331every request to be able to match the request with the response.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100332
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100333{result} is the result of the evaluation and is JSON encoded. If the
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100334evaluation fails or the result can't be encoded in JSON it is the string
335"ERROR".
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100336
337
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100338Command "expr" without a response ~
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100339
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100340This command is similar to "expr" above, but does not send back any response.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100341Example:
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100342 ["expr","setline('$', ['one', 'two', 'three'])"] ~
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100343There is no third argument in the request.
344
345
346Command "call" ~
347
348This is similar to "expr", but instead of passing the whole expression as a
349string this passes the name of a function and a list of arguments. This
350avoids the conversion of the arguments to a string and escaping and
351concatenating them. Example:
352 ["call", "line", ["$"], -2] ~
353
354Leave out the fourth argument if no response is to be sent:
355 ["call", "setline", ["$", ["one", "two", "three"]]] ~
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100356
357==============================================================================
Bram Moolenaar38a55632016-02-15 22:07:32 +01003586. Using a RAW or NL channel *channel-raw*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100359
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100360If mode is RAW or NL then a message can be send like this: >
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100361 let response = ch_evalraw(channel, {string})
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100362
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100363The {string} is sent as-is. The response will be what can be read from the
364channel right away. Since Vim doesn't know how to recognize the end of the
Bram Moolenaar38a55632016-02-15 22:07:32 +0100365message you need to take care of it yourself. The timeout applies for reading
366the first byte, after that it will not wait for anything more.
367
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100368If mode is "nl" you can send a message in a similar way. You are expected
Bram Moolenaar38a55632016-02-15 22:07:32 +0100369to put in the NL after each message. Thus you can also send several messages
370ending in a NL at once. The response will be the text up to and including the
371first NL. This can also be just the NL for an empty response.
372If no NL was read before the channel timeout an empty string is returned.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100373
374To send a message, without expecting a response: >
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100375 call ch_sendraw(channel, {string})
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100376The process can send back a response, the channel handler will be called with
377it.
378
379To send a message and letting the response handled by a specific function,
380asynchronously: >
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100381 call ch_sendraw(channel, {string}, {'callback': 'MyHandler'})
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100382
Bram Moolenaar38a55632016-02-15 22:07:32 +0100383This {string} can also be JSON, use |json_encode()| to create it and
384|json_decode()| to handle a received JSON message.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100385
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100386It is not possible to use |ch_evalexpr()| or |ch_sendexpr()| on a raw channel.
Bram Moolenaarcbebd482016-02-07 23:02:56 +0100387
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100388==============================================================================
Bram Moolenaar38a55632016-02-15 22:07:32 +01003897. More channel functions *channel-more*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100390
Bram Moolenaar38a55632016-02-15 22:07:32 +0100391To obtain the status of a channel: ch_status(channel). The possible results
392are:
393 "fail" Failed to open the channel.
394 "open" The channel can be used.
Bram Moolenaar06481422016-04-30 15:13:38 +0200395 "buffered" The channel was closed but there is data to read.
Bram Moolenaar38a55632016-02-15 22:07:32 +0100396 "closed" The channel was closed.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100397
Bram Moolenaar187db502016-02-27 14:44:26 +0100398To obtain the job associated with a channel: ch_getjob(channel)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100399
Bram Moolenaar38a55632016-02-15 22:07:32 +0100400To read one message from a channel: >
401 let output = ch_read(channel)
402This uses the channel timeout. To read without a timeout, just get any
403message that is available: >
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100404 let output = ch_read(channel, {'timeout': 0})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100405When no message was available then the result is v:none for a JSON or JS mode
406channels, an empty string for a RAW or NL channel.
407
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100408To read all output from a RAW channel that is available: >
409 let output = ch_readraw(channel)
Bram Moolenaar38a55632016-02-15 22:07:32 +0100410To read the error output: >
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100411 let output = ch_readraw(channel, {"part": "err"})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100412
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100413ch_read() and ch_readraw() use the channel timeout. When there is nothing to
414read within that time an empty string is returned. To specify a different
415timeout in msec use the "timeout" option:
416 {"timeout": 123} ~
417To read from the error output use the "part" option:
418 {"part": "err"} ~
419To read a message with a specific ID, on a JS or JSON channel:
420 {"id": 99} ~
421When no ID is specified or the ID is -1, the first message is returned. This
422overrules any callback waiting for this message.
423
424For a RAW channel this returns whatever is available, since Vim does not know
425where a message ends.
426For a NL channel this returns one message.
427For a JS or JSON channel this returns one decoded message.
428This includes any sequence number.
429
Bram Moolenaar38a55632016-02-15 22:07:32 +0100430==============================================================================
4318. Starting a job with a channel *job-start* *job*
432
433To start a job and open a channel for stdin/stdout/stderr: >
434 let job = job_start(command, {options})
435
436You can get the channel with: >
437 let channel = job_getchannel(job)
438
439The channel will use NL mode. If you want another mode it's best to specify
440this in {options}. When changing the mode later some text may have already
441been received and not parsed correctly.
442
443If the command produces a line of output that you want to deal with, specify
444a handler for stdout: >
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100445 let job = job_start(command, {"out_cb": "MyHandler"})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100446The function will be called with the channel and a message. You would define
447it like this: >
448 func MyHandler(channel, msg)
449
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100450Without the handler you need to read the output with |ch_read()| or
Bram Moolenaar06481422016-04-30 15:13:38 +0200451|ch_readraw()|. You can do this in the close callback, see |read-in-close-cb|.
Bram Moolenaar38a55632016-02-15 22:07:32 +0100452
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100453The handler defined for "out_cb" will not receive stderr. If you want to
454handle that separately, add an "err_cb" handler: >
455 let job = job_start(command, {"out_cb": "MyHandler",
456 \ "err_cb": "ErrHandler"})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100457
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100458If you want to handle both stderr and stdout with one handler use the
459"callback" option: >
460 let job = job_start(command, {"callback": "MyHandler"})
461
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100462You can send a message to the command with ch_evalraw(). If the channel is in
463JSON or JS mode you can use ch_evalexpr().
Bram Moolenaar38a55632016-02-15 22:07:32 +0100464
465There are several options you can use, see |job-options|.
Bram Moolenaar187db502016-02-27 14:44:26 +0100466For example, to start a job and write its output in buffer "dummy": >
467 let logjob = job_start("tail -f /tmp/log",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100468 \ {'out_io': 'buffer', 'out_name': 'dummy'})
Bram Moolenaar187db502016-02-27 14:44:26 +0100469 sbuf dummy
Bram Moolenaar38a55632016-02-15 22:07:32 +0100470
Bram Moolenaar5f148ec2016-03-07 22:59:26 +0100471
472Job input from a buffer ~
473
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100474To run a job that reads from a buffer: >
475 let job = job_start({command},
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100476 \ {'in_io': 'buffer', 'in_name': 'mybuffer'})
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100477<
478 *E915* *E918*
479The buffer is found by name, similar to |bufnr()|. The buffer must exist and
480be loaded when job_start() is called.
481
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100482By default this reads the whole buffer. This can be changed with the "in_top"
483and "in_bot" options.
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100484
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100485A special mode is when "in_top" is set to zero and "in_bot" is not set: Every
Bram Moolenaar5f148ec2016-03-07 22:59:26 +0100486time a line is added to the buffer, the last-but-one line will be send to the
487job stdin. This allows for editing the last line and sending it when pressing
488Enter.
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100489
Bram Moolenaar06481422016-04-30 15:13:38 +0200490
491Reading job output in the close callback ~
492 *read-in-close-cb*
493If the job can take some time and you don't need intermediate results, you can
494add a close callback and read the output there: >
495
496 func! CloseHandler(channel)
497 while ch_status(a:channel) == 'buffered'
498 echomsg ch_read(a:channel)
499 endwhile
500 endfunc
501 let job = job_start(command, {'close_cb': 'CloseHandler'})
502
503You will want to do something more useful than "echomsg".
504
Bram Moolenaar38a55632016-02-15 22:07:32 +0100505==============================================================================
5069. Starting a job without a channel *job-start-nochannel*
507
508To start another process without creating a channel: >
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100509 let job = job_start(command,
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100510 \ {"in_io": "null", "out_io": "null", "err_io": "null"})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100511
512This starts {command} in the background, Vim does not wait for it to finish.
513
Bram Moolenaar38a55632016-02-15 22:07:32 +0100514When Vim sees that neither stdin, stdout or stderr are connected, no channel
515will be created. Often you will want to include redirection in the command to
516avoid it getting stuck.
517
518There are several options you can use, see |job-options|.
519
Bram Moolenaar77cdfd12016-03-12 12:57:59 +0100520 *job-start-if-needed*
521To start a job only when connecting to an address does not work, do something
522like this: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100523 let channel = ch_open(address, {"waittime": 0})
524 if ch_status(channel) == "fail"
525 let job = job_start(command)
526 let channel = ch_open(address, {"waittime": 1000})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100527 endif
Bram Moolenaar77cdfd12016-03-12 12:57:59 +0100528
529Note that the waittime for ch_open() gives the job one second to make the port
530available.
Bram Moolenaar38a55632016-02-15 22:07:32 +0100531
532==============================================================================
53310. Job options *job-options*
534
535The {options} argument in job_start() is a dictionary. All entries are
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100536optional. Some options can be used after the job has started, using
537job_setoptions(job, {options}). Many options can be used with the channel
538related to the job, using ch_setoptions(channel, {options}).
539See |job_setoptions()| and |ch_setoptions()|.
Bram Moolenaar38a55632016-02-15 22:07:32 +0100540
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100541 *in_mode* *out_mode* *err_mode*
542"in_mode" mode specifically for stdin, only when using pipes
543"out_mode" mode specifically for stdout, only when using pipes
544"err_mode" mode specifically for stderr, only when using pipes
545 See |channel-mode| for the values.
546
547 Note: when setting "mode" the part specific mode is
548 overwritten. Therefore set "mode" first and the part
549 specific mode later.
550
551 Note: when writing to a file or buffer and when
552 reading from a buffer NL mode is used by default.
553
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100554 *job-callback*
555"callback": handler Callback for something to read on any part of the
556 channel.
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100557 *job-out_cb* *out_cb*
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100558"out_cb": handler Callback for when there is something to read on
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100559 stdout. Only for when the channel uses pipes. When
560 "out_cb" wasn't set the channel callback is used.
561
562 *job-err_cb* *err_cb*
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100563"err_cb": handler Callback for when there is something to read on
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100564 stderr. Only for when the channel uses pipes. When
565 "err_cb" wasn't set the channel callback is used.
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100566 *job-close_cb*
567"close_cb": handler Callback for when the channel is closed. Same as
568 "close_cb" on ch_open().
569 *job-exit_cb*
570"exit_cb": handler Callback for when the job ends. The arguments are the
Bram Moolenaar38a55632016-02-15 22:07:32 +0100571 job and the exit status.
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100572 Vim checks about every 10 seconds for jobs that ended.
573 The callback can also be triggered by calling
574 |job_status()|.
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100575 *job-timeout*
576"timeout" The time to wait for a request when blocking, E.g.
577 when using ch_evalexpr(). In milliseconds. The
578 default is 2000 (2 seconds).
579 *out_timeout* *err_timeout*
580"out_timeout" Timeout for stdout. Only when using pipes.
581"err_timeout" Timeout for stderr. Only when using pipes.
582 Note: when setting "timeout" the part specific mode is
583 overwritten. Therefore set "timeout" first and the
584 part specific mode later.
585
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100586 *job-stoponexit*
587"stoponexit": {signal} Send {signal} to the job when Vim exits. See
588 |job_stop()| for possible values.
589"stoponexit": "" Do not stop the job when Vim exits.
590 The default is "term".
591
Bram Moolenaar77cdfd12016-03-12 12:57:59 +0100592 *job-term*
Bram Moolenaar38a55632016-02-15 22:07:32 +0100593"term": "open" Start a terminal and connect the job
594 stdin/stdout/stderr to it.
Bram Moolenaar77cdfd12016-03-12 12:57:59 +0100595 NOTE: Not implemented yet!
Bram Moolenaar38a55632016-02-15 22:07:32 +0100596
Bram Moolenaar77cdfd12016-03-12 12:57:59 +0100597"channel": {channel} Use an existing channel instead of creating a new one.
598 The parts of the channel that get used for the new job
599 will be disconnected from what they were used before.
600 If the channel was still use by another job this may
601 cause I/O errors.
602 Existing callbacks and other settings remain.
603
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100604 *job-in_io* *in_top* *in_bot* *in_name* *in_buf*
605"in_io": "null" disconnect stdin (read from /dev/null)
606"in_io": "pipe" stdin is connected to the channel (default)
607"in_io": "file" stdin reads from a file
608"in_io": "buffer" stdin reads from a buffer
609"in_top": number when using "buffer": first line to send (default: 1)
610"in_bot": number when using "buffer": last line to send (default: last)
611"in_name": "/path/file" the name of the file or buffer to read from
612"in_buf": number the number of the buffer to read from
Bram Moolenaar38a55632016-02-15 22:07:32 +0100613
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100614 *job-out_io* *out_name* *out_buf*
615"out_io": "null" disconnect stdout (goes to /dev/null)
616"out_io": "pipe" stdout is connected to the channel (default)
617"out_io": "file" stdout writes to a file
618"out_io": "buffer" stdout appends to a buffer
619"out_name": "/path/file" the name of the file or buffer to write to
620"out_buf": number the number of the buffer to write to
Bram Moolenaar38a55632016-02-15 22:07:32 +0100621
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100622 *job-err_io* *err_name* *err_buf*
623"err_io": "out" stderr messages to go to stdout
624"err_io": "null" disconnect stderr (goes to /dev/null)
625"err_io": "pipe" stderr is connected to the channel (default)
626"err_io": "file" stderr writes to a file
627"err_io": "buffer" stderr appends to a buffer
628"err_name": "/path/file" the name of the file or buffer to write to
629"err_buf": number the number of the buffer to write to
Bram Moolenaar77cdfd12016-03-12 12:57:59 +0100630
Bram Moolenaar7db8f6f2016-03-29 23:12:46 +0200631"block_write": number only for testing: pretend every other write to stdin
632 will block
633
Bram Moolenaar77cdfd12016-03-12 12:57:59 +0100634
635Writing to a buffer ~
Bram Moolenaar187db502016-02-27 14:44:26 +0100636
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100637When the out_io or err_io mode is "buffer" and there is a callback, the text
Bram Moolenaar5f148ec2016-03-07 22:59:26 +0100638is appended to the buffer before invoking the callback.
639
640When a buffer is used both for input and output, the output lines are put
641above the last line, since the last line is what is written to the channel
Bram Moolenaar77cdfd12016-03-12 12:57:59 +0100642input. Otherwise lines are appended below the last line.
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100643
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100644When using JS or JSON mode with "buffer", only messages with zero or negative
645ID will be added to the buffer, after decoding + encoding. Messages with a
646positive number will be handled by a callback, commands are handled as usual.
647
Bram Moolenaar187db502016-02-27 14:44:26 +0100648The name of the buffer is compared the full name of existing buffers. If
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100649there is a match that buffer is used. Otherwise a new buffer is created.
650Use an empty name to always create a new buffer. |ch_getbufnr()| can then be
651used to get the buffer number.
652
653For a new buffer 'buftype' is set to "nofile" and 'bufhidden' to "hide". If
654you prefer other settings, create the buffer first and pass the buffer number.
Bram Moolenaar187db502016-02-27 14:44:26 +0100655
656When the buffer written to is displayed in a window and the cursor is in the
657first column of the last line, the cursor will be moved to the newly added
658line and the window is scrolled up to show the cursor if needed.
659
660Undo is synced for every added line.
Bram Moolenaar38a55632016-02-15 22:07:32 +0100661
Bram Moolenaar77cdfd12016-03-12 12:57:59 +0100662
663Writing to a file ~
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100664 *E920*
Bram Moolenaar77cdfd12016-03-12 12:57:59 +0100665The file is created with permissions 600 (read-write for the user, not
666accessible for others). Use |setfperm()| to change this.
667
668If the file already exists it is truncated.
669
Bram Moolenaar38a55632016-02-15 22:07:32 +0100670==============================================================================
67111. Controlling a job *job-control*
672
673To get the status of a job: >
674 echo job_status(job)
675
676To make a job stop running: >
677 job_stop(job)
678
679This is the normal way to end a job. On Unix it sends a SIGTERM to the job.
680It is possible to use other ways to stop the job, or even send arbitrary
681signals. E.g. to force a job to stop, "kill it": >
682 job_stop(job, "kill")
683
684For more options see |job_stop()|.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100685
686
687 vim:tw=78:ts=8:ft=help:norl: