blob: a89bdb787526e8c6938b936a90086167b40a5b02 [file] [log] [blame]
Bram Moolenaar5f148ec2016-03-07 22:59:26 +01001*channel.txt* For Vim version 7.4. Last change: 2016 Mar 06
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
47For when using sockets See |job-start|, |job-may-start| and |channel-open|.
48For 2 and 3, one or more jobs using pipes, see |job-start|.
49For 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 Moolenaar38a55632016-02-15 22:07:32 +0100123{options} is a dictionary with optional entries:
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
130
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100131"in-mode" mode specifically for stdin, only when using pipes
132"out-mode" mode specifically for stdout, only when using pipes
133"err-mode" mode specifically for stderr, only when using pipes
134 Note: when setting "mode" the part specific mode is
135 overwritten. Therefore set "mode" first and the part specific
136 mode later.
137
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100138 Note: when writing to a file or buffer and when reading from a
139 buffer NL mode is used by default.
Bram Moolenaar187db502016-02-27 14:44:26 +0100140
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100141 *channel-callback*
Bram Moolenaar38a55632016-02-15 22:07:32 +0100142"callback" A function that is called when a message is received that is
143 not handled otherwise. It gets two arguments: the channel
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100144 and the received message. Example: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100145 func Handle(channel, msg)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100146 echo 'Received: ' . a:msg
147 endfunc
Bram Moolenaar38a55632016-02-15 22:07:32 +0100148 let channel = ch_open("localhost:8765", {"callback": "Handle"})
149<
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100150 When "mode" is "json" or "js" the "msg" argument is the body
151 of the received message, converted to Vim types.
152 When "mode" is "nl" the "msg" argument is one message,
153 excluding the NL.
154 When "mode" is "raw" the "msg" argument is the whole message
155 as a string.
156 *out-cb*
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100157"out-cb" A function like "callback" but used for stdout. Only for when
158 the channel uses pipes. When "out-cb" wasn't set the channel
159 callback is used.
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100160 *err-cb*
Bram Moolenaar38a55632016-02-15 22:07:32 +0100161"err-cb" A function like "callback" but used for stderr. Only for when
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100162 the channel uses pipes. When "err-cb" wasn't set the channel
163 callback is used.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100164
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100165 TODO: *close-cb*
Bram Moolenaar38a55632016-02-15 22:07:32 +0100166"close-cb" A function that is called when the channel gets closed, other
167 than by calling ch_close(). It should be defined like this: >
168 func MyCloseHandler(channel)
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100169< *waittime*
Bram Moolenaar38a55632016-02-15 22:07:32 +0100170"waittime" The time to wait for the connection to be made in
Bram Moolenaarf3913272016-02-25 00:00:01 +0100171 milliseconds. A negative number waits forever.
172
173 The default is zero, don't wait, which is useful if a local
174 server is supposed to be running already. On Unix Vim
175 actually uses a 1 msec timeout, that is required on many
176 systems. Use a larger value for a remote server, e.g. 10
177 msec at least.
Bram Moolenaar38a55632016-02-15 22:07:32 +0100178
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100179"timeout" The time to wait for a request when blocking, E.g. when using
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100180 ch_evalexpr(). In milliseconds. The default is 2000 (2
Bram Moolenaar38a55632016-02-15 22:07:32 +0100181 seconds).
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100182 *out-timeout* *err-timeout*
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100183"out-timeout" Timeout for stdout. Only when using pipes.
184"err-timeout" Timeout for stderr. Only when using pipes.
185 Note: when setting "timeout" the part specific mode is
186 overwritten. Therefore set "timeout" first and the part
187 specific mode later.
188
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100189When "mode" is "json" or "js" the "callback" is optional. When omitted it is
190only possible to receive a message after sending one.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100191
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100192To change the channel options after opening it use |ch_setoptions()|. The
193arguments are similar to what is passed to |ch_open()|, but "waittime" cannot
194be given, since that only applies to opening the channel.
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100195
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100196For example, the handler can be added or changed: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100197 call ch_setoptions(channel, {'callback': callback})
198When "callback" is empty (zero or an empty string) the handler is removed.
199
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100200After a callback has been invoked Vim will update the screen and put the
201cursor back where it belongs. Thus the callback should not need to do
202`:redraw`.
203
Bram Moolenaar38a55632016-02-15 22:07:32 +0100204The timeout can be changed: >
205 call ch_setoptions(channel, {'timeout': msec})
206<
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100207 *channel-close* *E906*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100208Once done with the channel, disconnect it like this: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100209 call ch_close(channel)
210When a socket is used this will close the socket for both directions. When
211pipes are used (stdin/stdout/stderr) they are all closed. This might not be
212what you want! Stopping the job with job_stop() might be better.
Bram Moolenaar187db502016-02-27 14:44:26 +0100213All readahead is discarded, callbacks will no longer be invoked.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100214
Bram Moolenaarcbebd482016-02-07 23:02:56 +0100215When the channel can't be opened you will get an error message. There is a
216difference between MS-Windows and Unix: On Unix when the port doesn't exist
217ch_open() fails quickly. On MS-Windows "waittime" applies.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100218*E898* *E899* *E900* *E901* *E902*
219
220If there is an error reading or writing a channel it will be closed.
221*E896* *E630* *E631*
222
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100223==============================================================================
Bram Moolenaar38a55632016-02-15 22:07:32 +01002244. Using a JSON or JS channel *channel-use*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100225
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100226If mode is JSON then a message can be sent synchronously like this: >
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100227 let response = ch_evalexpr(channel, {expr})
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100228This awaits a response from the other side.
229
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100230When mode is JS this works the same, except that the messages use
Bram Moolenaar38a55632016-02-15 22:07:32 +0100231JavaScript encoding. See |js_encode()| for the difference.
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100232
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100233To send a message, without handling a response or letting the channel callback
234handle the response: >
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100235 call ch_sendexpr(channel, {expr})
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100236
237To send a message and letting the response handled by a specific function,
238asynchronously: >
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100239 call ch_sendexpr(channel, {expr}, {'callback': Handler})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100240
241Vim will match the response with the request using the message ID. Once the
242response is received the callback will be invoked. Further responses with the
243same ID will be ignored. If your server sends back multiple responses you
244need to send them with ID zero, they will be passed to the channel callback.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100245
246The {expr} is converted to JSON and wrapped in an array. An example of the
247message that the receiver will get when {expr} is the string "hello":
248 [12,"hello"] ~
249
250The format of the JSON sent is:
251 [{number},{expr}]
252
253In which {number} is different every time. It must be used in the response
254(if any):
255
256 [{number},{response}]
257
258This way Vim knows which sent message matches with which received message and
259can call the right handler. Also when the messages arrive out of order.
260
261The sender must always send valid JSON to Vim. Vim can check for the end of
262the message by parsing the JSON. It will only accept the message if the end
263was received.
264
265When the process wants to send a message to Vim without first receiving a
266message, it must use the number zero:
267 [0,{response}]
268
269Then channel handler will then get {response} converted to Vim types. If the
270channel does not have a handler the message is dropped.
271
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100272On read error or ch_close(), when using a socket with RAW or NL mode, the
273string "DETACH\n" is sent, if still possible. The channel will then be
274inactive.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100275
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100276It is also possible to use ch_sendraw() and ch_evalraw() on a JSON or JS
277channel. The caller is then completely responsible for correct encoding and
278decoding.
Bram Moolenaarcbebd482016-02-07 23:02:56 +0100279
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100280==============================================================================
Bram Moolenaar38a55632016-02-15 22:07:32 +01002815. Channel commands *channel-commands*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100282
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100283With a JSON channel the process can send commands to Vim that will be
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100284handled by Vim internally, it does not require a handler for the channel.
285
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100286Possible commands are: *E903* *E904* *E905*
287 ["redraw" {forced}]
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100288 ["ex", {Ex command}]
289 ["normal", {Normal mode command}]
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100290 ["expr", {expression}, {number}]
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100291 ["expr", {expression}]
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100292 ["call", {func name}, {argument list}, {number}]
293 ["call", {func name}, {argument list}]
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100294
295With all of these: Be careful what these commands do! You can easily
296interfere with what the user is doing. To avoid trouble use |mode()| to check
297that the editor is in the expected state. E.g., to send keys that must be
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100298inserted as text, not executed as a command:
299 ["ex","if mode() == 'i' | call feedkeys('ClassName') | endif"] ~
300
301Errors in these commands are normally not reported to avoid them messing up
302the display. If you do want to see them, set the 'verbose' option to 3 or
303higher.
304
305
306Command "redraw" ~
307
308The other commands do not update the screen, so that you can send a sequence
309of commands without the cursor moving around. You must end with the "redraw"
310command to show any changed text and show the cursor where it belongs.
311
312The argument is normally an empty string:
313 ["redraw", ""] ~
314To first clear the screen pass "force":
315 ["redraw", "force"] ~
316
317
318Command "ex" ~
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100319
320The "ex" command is executed as any Ex command. There is no response for
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100321completion or error. You could use functions in an |autoload| script:
322 ["ex","call myscript#MyFunc(arg)"]
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100323
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100324You can also use "call |feedkeys()|" to insert any key sequence.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100325
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100326
327Command "normal" ~
328
Bram Moolenaar681baaf2016-02-04 20:57:07 +0100329The "normal" command is executed like with ":normal!", commands are not
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100330mapped. Example to open the folds under the cursor:
331 ["normal" "zO"]
332
333
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100334Command "expr" with response ~
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100335
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100336The "expr" command can be used to get the result of an expression. For
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100337example, to get the number of lines in the current buffer:
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100338 ["expr","line('$')", -2] ~
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100339
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100340It will send back the result of the expression:
Bram Moolenaare0fa3742016-02-20 15:47:01 +0100341 [-2, "last line"] ~
342The format is:
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100343 [{number}, {result}]
Bram Moolenaar187db502016-02-27 14:44:26 +0100344
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100345Here {number} is the same as what was in the request. Use a negative number
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100346to avoid confusion with message that Vim sends. Use a different number on
347every request to be able to match the request with the response.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100348
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100349{result} is the result of the evaluation and is JSON encoded. If the
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100350evaluation fails or the result can't be encoded in JSON it is the string
351"ERROR".
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100352
353
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100354Command "expr" without a response ~
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100355
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100356This command is similar to "expr" above, but does not send back any response.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100357Example:
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100358 ["expr","setline('$', ['one', 'two', 'three'])"] ~
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100359There is no third argument in the request.
360
361
362Command "call" ~
363
364This is similar to "expr", but instead of passing the whole expression as a
365string this passes the name of a function and a list of arguments. This
366avoids the conversion of the arguments to a string and escaping and
367concatenating them. Example:
368 ["call", "line", ["$"], -2] ~
369
370Leave out the fourth argument if no response is to be sent:
371 ["call", "setline", ["$", ["one", "two", "three"]]] ~
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100372
373==============================================================================
Bram Moolenaar38a55632016-02-15 22:07:32 +01003746. Using a RAW or NL channel *channel-raw*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100375
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100376If mode is RAW or NL then a message can be send like this: >
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100377 let response = ch_evalraw(channel, {string})
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100378
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100379The {string} is sent as-is. The response will be what can be read from the
380channel right away. Since Vim doesn't know how to recognize the end of the
Bram Moolenaar38a55632016-02-15 22:07:32 +0100381message you need to take care of it yourself. The timeout applies for reading
382the first byte, after that it will not wait for anything more.
383
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100384If mode is "nl" you can send a message in a similar way. You are expected
Bram Moolenaar38a55632016-02-15 22:07:32 +0100385to put in the NL after each message. Thus you can also send several messages
386ending in a NL at once. The response will be the text up to and including the
387first NL. This can also be just the NL for an empty response.
388If no NL was read before the channel timeout an empty string is returned.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100389
390To send a message, without expecting a response: >
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100391 call ch_sendraw(channel, {string})
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100392The process can send back a response, the channel handler will be called with
393it.
394
395To send a message and letting the response handled by a specific function,
396asynchronously: >
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100397 call ch_sendraw(channel, {string}, {'callback': 'MyHandler'})
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100398
Bram Moolenaar38a55632016-02-15 22:07:32 +0100399This {string} can also be JSON, use |json_encode()| to create it and
400|json_decode()| to handle a received JSON message.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100401
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100402It is not possible to use |ch_evalexpr()| or |ch_sendexpr()| on a raw channel.
Bram Moolenaarcbebd482016-02-07 23:02:56 +0100403
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100404==============================================================================
Bram Moolenaar38a55632016-02-15 22:07:32 +01004057. More channel functions *channel-more*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100406
Bram Moolenaar38a55632016-02-15 22:07:32 +0100407To obtain the status of a channel: ch_status(channel). The possible results
408are:
409 "fail" Failed to open the channel.
410 "open" The channel can be used.
411 "closed" The channel was closed.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100412
413TODO:
Bram Moolenaar187db502016-02-27 14:44:26 +0100414To obtain the job associated with a channel: ch_getjob(channel)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100415
Bram Moolenaar38a55632016-02-15 22:07:32 +0100416To read one message from a channel: >
417 let output = ch_read(channel)
418This uses the channel timeout. To read without a timeout, just get any
419message that is available: >
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100420 let output = ch_read(channel, {'timeout': 0})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100421When no message was available then the result is v:none for a JSON or JS mode
422channels, an empty string for a RAW or NL channel.
423
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100424To read all output from a RAW channel that is available: >
425 let output = ch_readraw(channel)
Bram Moolenaar38a55632016-02-15 22:07:32 +0100426To read the error output: >
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100427 let output = ch_readraw(channel, {"part": "err"})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100428
429==============================================================================
4308. Starting a job with a channel *job-start* *job*
431
432To start a job and open a channel for stdin/stdout/stderr: >
433 let job = job_start(command, {options})
434
435You can get the channel with: >
436 let channel = job_getchannel(job)
437
438The channel will use NL mode. If you want another mode it's best to specify
439this in {options}. When changing the mode later some text may have already
440been received and not parsed correctly.
441
442If the command produces a line of output that you want to deal with, specify
443a handler for stdout: >
444 let job = job_start(command, {"out-cb": "MyHandler"})
445The function will be called with the channel and a message. You would define
446it like this: >
447 func MyHandler(channel, msg)
448
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100449Without the handler you need to read the output with |ch_read()| or
450|ch_readraw()|.
Bram Moolenaar38a55632016-02-15 22:07:32 +0100451
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100452The handler defined for "out-cb" will not receive stderr. If you want to
Bram Moolenaar38a55632016-02-15 22:07:32 +0100453handle that separately, add an "err-cb" handler: >
454 let job = job_start(command, {"out-cb": "MyHandler",
455 \ "err-cb": "ErrHandler"})
456
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100457If you want to handle both stderr and stdout with one handler use the
458"callback" option: >
459 let job = job_start(command, {"callback": "MyHandler"})
460
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100461You can send a message to the command with ch_evalraw(). If the channel is in
462JSON or JS mode you can use ch_evalexpr().
Bram Moolenaar38a55632016-02-15 22:07:32 +0100463
464There are several options you can use, see |job-options|.
Bram Moolenaar187db502016-02-27 14:44:26 +0100465For example, to start a job and write its output in buffer "dummy": >
466 let logjob = job_start("tail -f /tmp/log",
467 \ {'out-io': 'buffer', 'out-name': 'dummy'})
468 sbuf dummy
Bram Moolenaar38a55632016-02-15 22:07:32 +0100469
Bram Moolenaar5f148ec2016-03-07 22:59:26 +0100470
471Job input from a buffer ~
472
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100473To run a job that reads from a buffer: >
474 let job = job_start({command},
475 \ {'in-io': 'buffer', 'in-name': 'mybuffer'})
476<
477 *E915* *E918*
478The buffer is found by name, similar to |bufnr()|. The buffer must exist and
479be loaded when job_start() is called.
480
481By default this reads the whole buffer. This can be changed with the "in-top"
482and "in-bot" options.
483
Bram Moolenaar5f148ec2016-03-07 22:59:26 +0100484A special mode is when "in-top" is set to zero and "in-bot" is not set: Every
485time a line is added to the buffer, the last-but-one line will be send to the
486job stdin. This allows for editing the last line and sending it when pressing
487Enter.
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100488
Bram Moolenaar38a55632016-02-15 22:07:32 +0100489TODO:
490To run a job and read its output once it is done: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100491 let job = job_start({command}, {'exit-cb': 'MyHandler'})
492 func MyHandler(job, status)
493 let channel = job_getchannel()
494 let output = ch_readall(channel)
495 " parse output
496 endfunc
497
498==============================================================================
4999. Starting a job without a channel *job-start-nochannel*
500
501To start another process without creating a channel: >
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100502 let job = job_start(command,
503 \ {"in-io": "null", "out-io": "null", "err-io": "null"})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100504
505This starts {command} in the background, Vim does not wait for it to finish.
506
507TODO:
508When Vim sees that neither stdin, stdout or stderr are connected, no channel
509will be created. Often you will want to include redirection in the command to
510avoid it getting stuck.
511
512There are several options you can use, see |job-options|.
513
514TODO: *job-may-start*
515To start a job only when connecting to an address does not work use
516job_maystart('command', {address}, {options}), For Example: >
517 let job = job_maystart(command, address, {"waittime": 1000})
518 let channel = job_gethandle(job)
519
520This comes down to: >
521 let channel = ch_open(address, {"waittime": 0})
522 if ch_status(channel) == "fail"
523 let job = job_start(command)
524 let channel = ch_open(address, {"waittime": 1000})
525 call job_sethandle(channel)
526 endif
527Note that the specified waittime applies to when the job has been started.
528This gives the job some time to make the port available.
529
530==============================================================================
53110. Job options *job-options*
532
533The {options} argument in job_start() is a dictionary. All entries are
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100534optional. Some options can be used after the job has started, using
535job_setoptions(job, {options}). Many options can be used with the channel
536related to the job, using ch_setoptions(channel, {options}).
537See |job_setoptions()| and |ch_setoptions()|.
Bram Moolenaar38a55632016-02-15 22:07:32 +0100538
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100539 *job-callback*
540"callback": handler Callback for something to read on any part of the
541 channel.
542 *job-out-cb*
Bram Moolenaar38a55632016-02-15 22:07:32 +0100543"out-cb": handler Callback for when there is something to read on
544 stdout.
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100545 *job-err-cb*
Bram Moolenaar38a55632016-02-15 22:07:32 +0100546"err-cb": handler Callback for when there is something to read on
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100547 stderr.
Bram Moolenaar187db502016-02-27 14:44:26 +0100548 *job-close-cb*
Bram Moolenaar38a55632016-02-15 22:07:32 +0100549"close-cb": handler Callback for when the channel is closed. Same as
550 "close-cb" on ch_open().
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100551 *job-exit-cb*
Bram Moolenaar38a55632016-02-15 22:07:32 +0100552"exit-cb": handler Callback for when the job ends. The arguments are the
553 job and the exit status.
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100554 Vim checks about every 10 seconds for jobs that ended.
555 The callback can also be triggered by calling
556 |job_status()|.
557 *job-stoponexit*
558"stoponexit": {signal} Send {signal} to the job when Vim exits. See
559 |job_stop()| for possible values.
560"stoponexit": "" Do not stop the job when Vim exits.
561 The default is "term".
562
Bram Moolenaar38a55632016-02-15 22:07:32 +0100563TODO: *job-term*
564"term": "open" Start a terminal and connect the job
565 stdin/stdout/stderr to it.
566
Bram Moolenaar187db502016-02-27 14:44:26 +0100567 *job-in-io*
568"in-io": "null" disconnect stdin TODO
Bram Moolenaar38a55632016-02-15 22:07:32 +0100569"in-io": "pipe" stdin is connected to the channel (default)
Bram Moolenaar187db502016-02-27 14:44:26 +0100570"in-io": "file" stdin reads from a file TODO
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100571"in-io": "buffer" stdin reads from a buffer
572"in-top": number when using "buffer": first line to send (default: 1)
573"in-bot": number when using "buffer": last line to send (default: last)
Bram Moolenaar5f148ec2016-03-07 22:59:26 +0100574"in-name": "/path/file" the name of the file or buffer to read from
Bram Moolenaar187db502016-02-27 14:44:26 +0100575"in-buf": number the number of the buffer to read from TODO
Bram Moolenaar38a55632016-02-15 22:07:32 +0100576
Bram Moolenaar187db502016-02-27 14:44:26 +0100577 *job-out-io*
578"out-io": "null" disconnect stdout TODO
Bram Moolenaar38a55632016-02-15 22:07:32 +0100579"out-io": "pipe" stdout is connected to the channel (default)
Bram Moolenaar187db502016-02-27 14:44:26 +0100580"out-io": "file" stdout writes to a file TODO
Bram Moolenaar38a55632016-02-15 22:07:32 +0100581"out-io": "buffer" stdout appends to a buffer
Bram Moolenaar187db502016-02-27 14:44:26 +0100582"out-name": "/path/file" the name of the file or buffer to write to
583"out-buf": number the number of the buffer to write to TODO
Bram Moolenaar38a55632016-02-15 22:07:32 +0100584
Bram Moolenaar187db502016-02-27 14:44:26 +0100585 *job-err-io*
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100586"err-io": "out" stderr messages to go to stdout
Bram Moolenaar187db502016-02-27 14:44:26 +0100587"err-io": "null" disconnect stderr TODO
588"err-io": "pipe" stderr is connected to the channel (default)
589"err-io": "file" stderr writes to a file TODO
590"err-io": "buffer" stderr appends to a buffer TODO
591"err-name": "/path/file" the name of the file or buffer to write to
592"err-buf": number the number of the buffer to write to TODO
593
Bram Moolenaar5f148ec2016-03-07 22:59:26 +0100594When the out-io or err-io mode is "buffer" and there is a callback, the text
595is appended to the buffer before invoking the callback.
596
597When a buffer is used both for input and output, the output lines are put
598above the last line, since the last line is what is written to the channel
599input. Otherwise lines are appened below the last line.
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100600
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100601When using JS or JSON mode with "buffer", only messages with zero or negative
602ID will be added to the buffer, after decoding + encoding. Messages with a
603positive number will be handled by a callback, commands are handled as usual.
604
Bram Moolenaar187db502016-02-27 14:44:26 +0100605The name of the buffer is compared the full name of existing buffers. If
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100606there is a match that buffer is used. Otherwise a new buffer is created.
607Use an empty name to always create a new buffer. |ch_getbufnr()| can then be
608used to get the buffer number.
609
610For a new buffer 'buftype' is set to "nofile" and 'bufhidden' to "hide". If
611you prefer other settings, create the buffer first and pass the buffer number.
Bram Moolenaar187db502016-02-27 14:44:26 +0100612
613When the buffer written to is displayed in a window and the cursor is in the
614first column of the last line, the cursor will be moved to the newly added
615line and the window is scrolled up to show the cursor if needed.
616
617Undo is synced for every added line.
Bram Moolenaar38a55632016-02-15 22:07:32 +0100618
Bram Moolenaar38a55632016-02-15 22:07:32 +0100619==============================================================================
62011. Controlling a job *job-control*
621
622To get the status of a job: >
623 echo job_status(job)
624
625To make a job stop running: >
626 job_stop(job)
627
628This is the normal way to end a job. On Unix it sends a SIGTERM to the job.
629It is possible to use other ways to stop the job, or even send arbitrary
630signals. E.g. to force a job to stop, "kill it": >
631 job_stop(job, "kill")
632
633For more options see |job_stop()|.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100634
635
636 vim:tw=78:ts=8:ft=help:norl: