blob: cb6f7818048141e45576cf73725db4742f6294da [file] [log] [blame]
Bram Moolenaar63b74a82019-03-24 15:09:13 +01001*channel.txt* For Vim version 8.1. Last change: 2019 Mar 21
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|
218. Starting a job with a channel |job-start|
229. Starting a job without a channel |job-start-nochannel|
2310. Job options |job-options|
2411. Controlling a job |job-control|
Bram Moolenaarf2732452018-06-03 14:47:35 +02002512. Using a prompt buffer |prompt-buffer|
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010026
27{Vi does not have any of these features}
Bram Moolenaar38a55632016-02-15 22:07:32 +010028{only when compiled with the |+channel| feature for channel stuff}
Bram Moolenaarf37506f2016-08-31 22:22:10 +020029 You can check this with: `has('channel')`
Bram Moolenaar38a55632016-02-15 22:07:32 +010030{only when compiled with the |+job| feature for job stuff}
Bram Moolenaarf37506f2016-08-31 22:22:10 +020031 You can check this with: `has('job')`
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010032
33==============================================================================
Bram Moolenaar38a55632016-02-15 22:07:32 +0100341. Overview *job-channel-overview*
35
36There are four main types of jobs:
Bram Moolenaar50ba5262016-09-22 22:33:02 +0200371. A daemon, serving several Vim instances.
Bram Moolenaar38a55632016-02-15 22:07:32 +010038 Vim connects to it with a socket.
392. One job working with one Vim instance, asynchronously.
40 Uses a socket or pipes.
413. A job performing some work for a short time, asynchronously.
42 Uses a socket or pipes.
434. Running a filter, synchronously.
44 Uses pipes.
45
Bram Moolenaar77cdfd12016-03-12 12:57:59 +010046For when using sockets See |job-start|, |job-start-nochannel| and
47|channel-open|. For 2 and 3, one or more jobs using pipes, see |job-start|.
Bram Moolenaar38a55632016-02-15 22:07:32 +010048For 4 use the ":{range}!cmd" command, see |filter|.
49
50Over the socket and pipes these protocols are available:
51RAW nothing known, Vim cannot tell where a message ends
52NL every message ends in a NL (newline) character
53JSON JSON encoding |json_encode()|
54JS JavaScript style JSON-like encoding |js_encode()|
55
56Common combination are:
57- Using a job connected through pipes in NL mode. E.g., to run a style
58 checker and receive errors and warnings.
Bram Moolenaar7dda86f2018-04-20 22:36:41 +020059- Using a daemon, connecting over a socket in JSON mode. E.g. to lookup
Bram Moolenaar09521312016-08-12 22:54:35 +020060 cross-references in a database.
Bram Moolenaar38a55632016-02-15 22:07:32 +010061
62==============================================================================
Bram Moolenaar26852122016-05-24 20:02:38 +0200632. Channel demo *channel-demo* *demoserver.py*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010064
65This requires Python. The demo program can be found in
66$VIMRUNTIME/tools/demoserver.py
67Run it in one terminal. We will call this T1.
68
69Run Vim in another terminal. Connect to the demo server with: >
Bram Moolenaar38a55632016-02-15 22:07:32 +010070 let channel = ch_open('localhost:8765')
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010071
72In T1 you should see:
73 === socket opened === ~
74
75You can now send a message to the server: >
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010076 echo ch_evalexpr(channel, 'hello!')
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010077
78The message is received in T1 and a response is sent back to Vim.
79You can see the raw messages in T1. What Vim sends is:
80 [1,"hello!"] ~
81And the response is:
82 [1,"got it"] ~
83The number will increase every time you send a message.
84
85The server can send a command to Vim. Type this on T1 (literally, including
Bram Moolenaarfb1f6262016-01-31 20:24:32 +010086the quotes):
87 ["ex","echo 'hi there'"] ~
88And you should see the message in Vim. You can move the cursor a word forward:
89 ["normal","w"] ~
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010090
91To handle asynchronous communication a callback needs to be used: >
Bram Moolenaar38a55632016-02-15 22:07:32 +010092 func MyHandler(channel, msg)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010093 echo "from the handler: " . a:msg
94 endfunc
Bram Moolenaar02e83b42016-02-21 20:10:26 +010095 call ch_sendexpr(channel, 'hello!', {'callback': "MyHandler"})
Bram Moolenaar38a55632016-02-15 22:07:32 +010096Vim will not wait for a response. Now the server can send the response later
97and MyHandler will be invoked.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010098
99Instead of giving a callback with every send call, it can also be specified
100when opening the channel: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100101 call ch_close(channel)
102 let channel = ch_open('localhost:8765', {'callback': "MyHandler"})
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100103 call ch_sendexpr(channel, 'hello!')
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100104
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100105When trying out channels it's useful to see what is going on. You can tell
106Vim to write lines in log file: >
107 call ch_logfile('channellog', 'w')
108See |ch_logfile()|.
109
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100110==============================================================================
Bram Moolenaar38a55632016-02-15 22:07:32 +01001113. Opening a channel *channel-open*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100112
Bram Moolenaar681baaf2016-02-04 20:57:07 +0100113To open a channel: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100114 let channel = ch_open({address} [, {options}])
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100115 if ch_status(channel) == "open"
116 " use the channel
Bram Moolenaar38a55632016-02-15 22:07:32 +0100117
118Use |ch_status()| to see if the channel could be opened.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100119
120{address} has the form "hostname:port". E.g., "localhost:8765".
121
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100122{options} is a dictionary with optional entries: *channel-open-options*
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100123
124"mode" can be: *channel-mode*
125 "json" - Use JSON, see below; most convenient way. Default.
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100126 "js" - Use JS (JavaScript) encoding, more efficient than JSON.
Bram Moolenaar38a55632016-02-15 22:07:32 +0100127 "nl" - Use messages that end in a NL character
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100128 "raw" - Use raw messages
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100129 *channel-callback* *E921*
Bram Moolenaar38a55632016-02-15 22:07:32 +0100130"callback" A function that is called when a message is received that is
131 not handled otherwise. It gets two arguments: the channel
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100132 and the received message. Example: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100133 func Handle(channel, msg)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100134 echo 'Received: ' . a:msg
135 endfunc
Bram Moolenaar38a55632016-02-15 22:07:32 +0100136 let channel = ch_open("localhost:8765", {"callback": "Handle"})
137<
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100138 When "mode" is "json" or "js" the "msg" argument is the body
139 of the received message, converted to Vim types.
140 When "mode" is "nl" the "msg" argument is one message,
141 excluding the NL.
142 When "mode" is "raw" the "msg" argument is the whole message
143 as a string.
Bram Moolenaare18c0b32016-03-20 21:08:34 +0100144
145 For all callbacks: Use |function()| to bind it to arguments
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100146 and/or a Dictionary. Or use the form "dict.function" to bind
147 the Dictionary.
Bram Moolenaar06d2d382016-05-20 17:24:11 +0200148
149 Callbacks are only called at a "safe" moment, usually when Vim
150 is waiting for the user to type a character. Vim does not use
151 multi-threading.
152
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100153 *close_cb*
154"close_cb" A function that is called when the channel gets closed, other
Bram Moolenaar38a55632016-02-15 22:07:32 +0100155 than by calling ch_close(). It should be defined like this: >
156 func MyCloseHandler(channel)
Bram Moolenaar06d2d382016-05-20 17:24:11 +0200157< Vim will invoke callbacks that handle data before invoking
158 close_cb, thus when this function is called no more data will
Bram Moolenaar958dc692016-12-01 15:34:12 +0100159 be passed to the callbacks.
160 *channel-drop*
161"drop" Specifies when to drop messages:
162 "auto" When there is no callback to handle a message.
163 The "close_cb" is also considered for this.
164 "never" All messages will be kept.
165
Bram Moolenaar0b146882018-09-06 16:27:24 +0200166 *channel-noblock*
167"noblock" Same effect as |job-noblock|. Only matters for writing.
168
Bram Moolenaar06d2d382016-05-20 17:24:11 +0200169 *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 Moolenaar4f3f6682016-03-26 23:01:59 +0100178 *channel-timeout*
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 Moolenaardecb14d2016-02-20 23:32:02 +0100182
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100183When "mode" is "json" or "js" the "callback" is optional. When omitted it is
184only possible to receive a message after sending one.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100185
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100186To change the channel options after opening it use |ch_setoptions()|. The
187arguments are similar to what is passed to |ch_open()|, but "waittime" cannot
188be given, since that only applies to opening the channel.
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100189
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100190For example, the handler can be added or changed: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100191 call ch_setoptions(channel, {'callback': callback})
192When "callback" is empty (zero or an empty string) the handler is removed.
193
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100194After a callback has been invoked Vim will update the screen and put the
195cursor back where it belongs. Thus the callback should not need to do
196`:redraw`.
197
Bram Moolenaar38a55632016-02-15 22:07:32 +0100198The timeout can be changed: >
199 call ch_setoptions(channel, {'timeout': msec})
200<
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100201 *channel-close* *E906*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100202Once done with the channel, disconnect it like this: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100203 call ch_close(channel)
204When a socket is used this will close the socket for both directions. When
205pipes are used (stdin/stdout/stderr) they are all closed. This might not be
206what you want! Stopping the job with job_stop() might be better.
Bram Moolenaar187db502016-02-27 14:44:26 +0100207All readahead is discarded, callbacks will no longer be invoked.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100208
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100209Note that a channel is closed in three stages:
210 - The I/O ends, log message: "Closing channel". There can still be queued
211 messages to read or callbacks to invoke.
212 - The readahead is cleared, log message: "Clearing channel". Some variables
213 may still reference the channel.
214 - The channel is freed, log message: "Freeing channel".
215
Bram Moolenaarcbebd482016-02-07 23:02:56 +0100216When the channel can't be opened you will get an error message. There is a
217difference between MS-Windows and Unix: On Unix when the port doesn't exist
218ch_open() fails quickly. On MS-Windows "waittime" applies.
Bram Moolenaaraa3b15d2016-04-21 08:53:19 +0200219*E898* *E901* *E902*
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100220
221If there is an error reading or writing a channel it will be closed.
Bram Moolenaaraa3b15d2016-04-21 08:53:19 +0200222*E630* *E631*
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100223
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100224==============================================================================
Bram Moolenaar38a55632016-02-15 22:07:32 +01002254. Using a JSON or JS channel *channel-use*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100226
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100227If mode is JSON then a message can be sent synchronously like this: >
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100228 let response = ch_evalexpr(channel, {expr})
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100229This awaits a response from the other side.
230
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100231When mode is JS this works the same, except that the messages use
Bram Moolenaar38a55632016-02-15 22:07:32 +0100232JavaScript encoding. See |js_encode()| for the difference.
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100233
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100234To send a message, without handling a response or letting the channel callback
235handle the response: >
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100236 call ch_sendexpr(channel, {expr})
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100237
238To send a message and letting the response handled by a specific function,
239asynchronously: >
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100240 call ch_sendexpr(channel, {expr}, {'callback': Handler})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100241
242Vim will match the response with the request using the message ID. Once the
243response is received the callback will be invoked. Further responses with the
244same ID will be ignored. If your server sends back multiple responses you
245need to send them with ID zero, they will be passed to the channel callback.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100246
247The {expr} is converted to JSON and wrapped in an array. An example of the
248message that the receiver will get when {expr} is the string "hello":
249 [12,"hello"] ~
250
251The format of the JSON sent is:
252 [{number},{expr}]
253
254In which {number} is different every time. It must be used in the response
255(if any):
256
257 [{number},{response}]
258
259This way Vim knows which sent message matches with which received message and
260can call the right handler. Also when the messages arrive out of order.
261
Bram Moolenaarf1f07922016-08-26 17:58:53 +0200262A newline character is terminating the JSON text. This can be used to
263separate the read text. For example, in Python:
264 splitidx = read_text.find('\n')
265 message = read_text[:splitidx]
266 rest = read_text[splitidx + 1:]
267
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100268The sender must always send valid JSON to Vim. Vim can check for the end of
269the message by parsing the JSON. It will only accept the message if the end
Bram Moolenaarf1f07922016-08-26 17:58:53 +0200270was received. A newline after the message is optional.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100271
272When the process wants to send a message to Vim without first receiving a
273message, it must use the number zero:
274 [0,{response}]
275
276Then channel handler will then get {response} converted to Vim types. If the
277channel does not have a handler the message is dropped.
278
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100279It is also possible to use ch_sendraw() and ch_evalraw() on a JSON or JS
280channel. The caller is then completely responsible for correct encoding and
281decoding.
Bram Moolenaarcbebd482016-02-07 23:02:56 +0100282
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100283==============================================================================
Bram Moolenaar38a55632016-02-15 22:07:32 +01002845. Channel commands *channel-commands*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100285
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100286With a JSON channel the process can send commands to Vim that will be
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100287handled by Vim internally, it does not require a handler for the channel.
288
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100289Possible commands are: *E903* *E904* *E905*
Bram Moolenaar220adb12016-09-12 12:17:26 +0200290 ["redraw", {forced}]
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100291 ["ex", {Ex command}]
292 ["normal", {Normal mode command}]
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100293 ["expr", {expression}, {number}]
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100294 ["expr", {expression}]
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100295 ["call", {func name}, {argument list}, {number}]
296 ["call", {func name}, {argument list}]
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100297
298With all of these: Be careful what these commands do! You can easily
299interfere with what the user is doing. To avoid trouble use |mode()| to check
300that the editor is in the expected state. E.g., to send keys that must be
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100301inserted as text, not executed as a command:
302 ["ex","if mode() == 'i' | call feedkeys('ClassName') | endif"] ~
303
304Errors in these commands are normally not reported to avoid them messing up
305the display. If you do want to see them, set the 'verbose' option to 3 or
306higher.
307
308
309Command "redraw" ~
310
Bram Moolenaar63b74a82019-03-24 15:09:13 +0100311The other commands do not explicitly update the screen, so that you can send a
312sequence of commands without the cursor moving around. A redraw can happen as
313a side effect of some commands. You must end with the "redraw" command to
314show any changed text and show the cursor where it belongs.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100315
316The argument is normally an empty string:
317 ["redraw", ""] ~
318To first clear the screen pass "force":
319 ["redraw", "force"] ~
320
321
322Command "ex" ~
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100323
324The "ex" command is executed as any Ex command. There is no response for
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100325completion or error. You could use functions in an |autoload| script:
326 ["ex","call myscript#MyFunc(arg)"]
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100327
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100328You can also use "call |feedkeys()|" to insert any key sequence.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100329
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100330When there is an error a message is written to the channel log, if it exists,
331and v:errmsg is set to the error.
332
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100333
334Command "normal" ~
335
Bram Moolenaar681baaf2016-02-04 20:57:07 +0100336The "normal" command is executed like with ":normal!", commands are not
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100337mapped. Example to open the folds under the cursor:
338 ["normal" "zO"]
339
340
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100341Command "expr" with response ~
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100342
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100343The "expr" command can be used to get the result of an expression. For
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100344example, to get the number of lines in the current buffer:
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100345 ["expr","line('$')", -2] ~
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100346
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100347It will send back the result of the expression:
Bram Moolenaare0fa3742016-02-20 15:47:01 +0100348 [-2, "last line"] ~
349The format is:
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100350 [{number}, {result}]
Bram Moolenaar187db502016-02-27 14:44:26 +0100351
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100352Here {number} is the same as what was in the request. Use a negative number
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100353to avoid confusion with message that Vim sends. Use a different number on
354every request to be able to match the request with the response.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100355
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100356{result} is the result of the evaluation and is JSON encoded. If the
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100357evaluation fails or the result can't be encoded in JSON it is the string
358"ERROR".
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100359
360
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100361Command "expr" without a response ~
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100362
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100363This command is similar to "expr" above, but does not send back any response.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100364Example:
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100365 ["expr","setline('$', ['one', 'two', 'three'])"] ~
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100366There is no third argument in the request.
367
368
369Command "call" ~
370
371This is similar to "expr", but instead of passing the whole expression as a
372string this passes the name of a function and a list of arguments. This
373avoids the conversion of the arguments to a string and escaping and
374concatenating them. Example:
375 ["call", "line", ["$"], -2] ~
376
377Leave out the fourth argument if no response is to be sent:
378 ["call", "setline", ["$", ["one", "two", "three"]]] ~
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100379
380==============================================================================
Bram Moolenaar38a55632016-02-15 22:07:32 +01003816. Using a RAW or NL channel *channel-raw*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100382
Bram Moolenaarc0514bf2016-11-17 14:50:09 +0100383If mode is RAW or NL then a message can be sent like this: >
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100384 let response = ch_evalraw(channel, {string})
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100385
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100386The {string} is sent as-is. The response will be what can be read from the
387channel right away. Since Vim doesn't know how to recognize the end of the
Bram Moolenaar38a55632016-02-15 22:07:32 +0100388message you need to take care of it yourself. The timeout applies for reading
389the first byte, after that it will not wait for anything more.
390
Bram Moolenaar910b8aa2016-02-16 21:03:07 +0100391If mode is "nl" you can send a message in a similar way. You are expected
Bram Moolenaar38a55632016-02-15 22:07:32 +0100392to put in the NL after each message. Thus you can also send several messages
393ending in a NL at once. The response will be the text up to and including the
394first NL. This can also be just the NL for an empty response.
395If no NL was read before the channel timeout an empty string is returned.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100396
397To send a message, without expecting a response: >
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100398 call ch_sendraw(channel, {string})
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100399The process can send back a response, the channel handler will be called with
400it.
401
402To send a message and letting the response handled by a specific function,
403asynchronously: >
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100404 call ch_sendraw(channel, {string}, {'callback': 'MyHandler'})
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100405
Bram Moolenaar38a55632016-02-15 22:07:32 +0100406This {string} can also be JSON, use |json_encode()| to create it and
407|json_decode()| to handle a received JSON message.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100408
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100409It is not possible to use |ch_evalexpr()| or |ch_sendexpr()| on a raw channel.
Bram Moolenaarcbebd482016-02-07 23:02:56 +0100410
Bram Moolenaar818078d2016-08-27 21:58:42 +0200411A String in Vim cannot contain NUL bytes. To send or receive NUL bytes read
412or write from a buffer. See |in_io-buffer| and |out_io-buffer|.
413
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100414==============================================================================
Bram Moolenaar38a55632016-02-15 22:07:32 +01004157. More channel functions *channel-more*
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100416
Bram Moolenaar38a55632016-02-15 22:07:32 +0100417To obtain the status of a channel: ch_status(channel). The possible results
418are:
419 "fail" Failed to open the channel.
420 "open" The channel can be used.
Bram Moolenaar06481422016-04-30 15:13:38 +0200421 "buffered" The channel was closed but there is data to read.
Bram Moolenaar38a55632016-02-15 22:07:32 +0100422 "closed" The channel was closed.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100423
Bram Moolenaar187db502016-02-27 14:44:26 +0100424To obtain the job associated with a channel: ch_getjob(channel)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100425
Bram Moolenaar38a55632016-02-15 22:07:32 +0100426To read one message from a channel: >
427 let output = ch_read(channel)
428This uses the channel timeout. To read without a timeout, just get any
429message that is available: >
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100430 let output = ch_read(channel, {'timeout': 0})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100431When no message was available then the result is v:none for a JSON or JS mode
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100432channels, an empty string for a RAW or NL channel. You can use |ch_canread()|
433to check if there is something to read.
434
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200435Note that when there is no callback, messages are dropped. To avoid that add
436a close callback to the channel.
Bram Moolenaar38a55632016-02-15 22:07:32 +0100437
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100438To read all output from a RAW channel that is available: >
439 let output = ch_readraw(channel)
Bram Moolenaar38a55632016-02-15 22:07:32 +0100440To read the error output: >
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100441 let output = ch_readraw(channel, {"part": "err"})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100442
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100443ch_read() and ch_readraw() use the channel timeout. When there is nothing to
444read within that time an empty string is returned. To specify a different
445timeout in msec use the "timeout" option:
446 {"timeout": 123} ~
447To read from the error output use the "part" option:
448 {"part": "err"} ~
449To read a message with a specific ID, on a JS or JSON channel:
450 {"id": 99} ~
451When no ID is specified or the ID is -1, the first message is returned. This
452overrules any callback waiting for this message.
453
454For a RAW channel this returns whatever is available, since Vim does not know
455where a message ends.
456For a NL channel this returns one message.
457For a JS or JSON channel this returns one decoded message.
458This includes any sequence number.
459
Bram Moolenaar38a55632016-02-15 22:07:32 +0100460==============================================================================
4618. Starting a job with a channel *job-start* *job*
462
463To start a job and open a channel for stdin/stdout/stderr: >
464 let job = job_start(command, {options})
465
466You can get the channel with: >
467 let channel = job_getchannel(job)
468
469The channel will use NL mode. If you want another mode it's best to specify
470this in {options}. When changing the mode later some text may have already
471been received and not parsed correctly.
472
473If the command produces a line of output that you want to deal with, specify
474a handler for stdout: >
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100475 let job = job_start(command, {"out_cb": "MyHandler"})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100476The function will be called with the channel and a message. You would define
477it like this: >
478 func MyHandler(channel, msg)
479
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100480Without the handler you need to read the output with |ch_read()| or
Bram Moolenaar06481422016-04-30 15:13:38 +0200481|ch_readraw()|. You can do this in the close callback, see |read-in-close-cb|.
Bram Moolenaar38a55632016-02-15 22:07:32 +0100482
Bram Moolenaar1ccd8ff2017-08-11 19:50:37 +0200483Note that if the job exits before you read the output, the output may be lost.
484This depends on the system (on Unix this happens because closing the write end
485of a pipe causes the read end to get EOF). To avoid this make the job sleep
486for a short while before it exits.
487
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100488The handler defined for "out_cb" will not receive stderr. If you want to
489handle that separately, add an "err_cb" handler: >
490 let job = job_start(command, {"out_cb": "MyHandler",
491 \ "err_cb": "ErrHandler"})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100492
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100493If you want to handle both stderr and stdout with one handler use the
494"callback" option: >
495 let job = job_start(command, {"callback": "MyHandler"})
496
Bram Moolenaar3ec574f2017-06-13 18:12:01 +0200497Depending on the system, starting a job can put Vim in the background, the
498started job gets the focus. To avoid that, use the `foreground()` function.
499This might not always work when called early, put in the callback handler or
500use a timer to call it after the job has started.
501
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100502You can send a message to the command with ch_evalraw(). If the channel is in
503JSON or JS mode you can use ch_evalexpr().
Bram Moolenaar38a55632016-02-15 22:07:32 +0100504
505There are several options you can use, see |job-options|.
Bram Moolenaar187db502016-02-27 14:44:26 +0100506For example, to start a job and write its output in buffer "dummy": >
507 let logjob = job_start("tail -f /tmp/log",
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100508 \ {'out_io': 'buffer', 'out_name': 'dummy'})
Bram Moolenaar187db502016-02-27 14:44:26 +0100509 sbuf dummy
Bram Moolenaar38a55632016-02-15 22:07:32 +0100510
Bram Moolenaar5f148ec2016-03-07 22:59:26 +0100511
512Job input from a buffer ~
Bram Moolenaar818078d2016-08-27 21:58:42 +0200513 *in_io-buffer*
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100514To run a job that reads from a buffer: >
515 let job = job_start({command},
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100516 \ {'in_io': 'buffer', 'in_name': 'mybuffer'})
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100517<
518 *E915* *E918*
519The buffer is found by name, similar to |bufnr()|. The buffer must exist and
520be loaded when job_start() is called.
521
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100522By default this reads the whole buffer. This can be changed with the "in_top"
523and "in_bot" options.
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100524
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100525A special mode is when "in_top" is set to zero and "in_bot" is not set: Every
Bram Moolenaar74675a62017-07-15 13:53:23 +0200526time a line is added to the buffer, the last-but-one line will be sent to the
Bram Moolenaar5f148ec2016-03-07 22:59:26 +0100527job stdin. This allows for editing the last line and sending it when pressing
528Enter.
Bram Moolenaar0874a832016-09-01 15:11:51 +0200529 *channel-close-in*
530When not using the special mode the pipe or socket will be closed after the
531last line has been written. This signals the reading end that the input
532finished. You can also use |ch_close_in()| to close it sooner.
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100533
Bram Moolenaar063b9d12016-07-09 20:21:48 +0200534NUL bytes in the text will be passed to the job (internally Vim stores these
535as NL bytes).
536
Bram Moolenaar06481422016-04-30 15:13:38 +0200537
538Reading job output in the close callback ~
539 *read-in-close-cb*
540If the job can take some time and you don't need intermediate results, you can
541add a close callback and read the output there: >
542
543 func! CloseHandler(channel)
Bram Moolenaar2ec618c2016-10-01 14:47:05 +0200544 while ch_status(a:channel, {'part': 'out'}) == 'buffered'
Bram Moolenaar06481422016-04-30 15:13:38 +0200545 echomsg ch_read(a:channel)
546 endwhile
547 endfunc
548 let job = job_start(command, {'close_cb': 'CloseHandler'})
549
550You will want to do something more useful than "echomsg".
551
Bram Moolenaar38a55632016-02-15 22:07:32 +0100552==============================================================================
5539. Starting a job without a channel *job-start-nochannel*
554
555To start another process without creating a channel: >
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100556 let job = job_start(command,
Bram Moolenaar51628222016-12-01 23:03:28 +0100557 \ {"in_io": "null", "out_io": "null", "err_io": "null"})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100558
559This starts {command} in the background, Vim does not wait for it to finish.
560
Bram Moolenaar38a55632016-02-15 22:07:32 +0100561When Vim sees that neither stdin, stdout or stderr are connected, no channel
562will be created. Often you will want to include redirection in the command to
563avoid it getting stuck.
564
565There are several options you can use, see |job-options|.
566
Bram Moolenaar77cdfd12016-03-12 12:57:59 +0100567 *job-start-if-needed*
568To start a job only when connecting to an address does not work, do something
569like this: >
Bram Moolenaar38a55632016-02-15 22:07:32 +0100570 let channel = ch_open(address, {"waittime": 0})
571 if ch_status(channel) == "fail"
572 let job = job_start(command)
573 let channel = ch_open(address, {"waittime": 1000})
Bram Moolenaar38a55632016-02-15 22:07:32 +0100574 endif
Bram Moolenaar77cdfd12016-03-12 12:57:59 +0100575
576Note that the waittime for ch_open() gives the job one second to make the port
577available.
Bram Moolenaar38a55632016-02-15 22:07:32 +0100578
579==============================================================================
58010. Job options *job-options*
581
582The {options} argument in job_start() is a dictionary. All entries are
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100583optional. Some options can be used after the job has started, using
584job_setoptions(job, {options}). Many options can be used with the channel
585related to the job, using ch_setoptions(channel, {options}).
586See |job_setoptions()| and |ch_setoptions()|.
Bram Moolenaar38a55632016-02-15 22:07:32 +0100587
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100588 *in_mode* *out_mode* *err_mode*
589"in_mode" mode specifically for stdin, only when using pipes
590"out_mode" mode specifically for stdout, only when using pipes
591"err_mode" mode specifically for stderr, only when using pipes
592 See |channel-mode| for the values.
593
594 Note: when setting "mode" the part specific mode is
595 overwritten. Therefore set "mode" first and the part
596 specific mode later.
597
598 Note: when writing to a file or buffer and when
599 reading from a buffer NL mode is used by default.
600
Bram Moolenaar0b146882018-09-06 16:27:24 +0200601 *job-noblock*
602"noblock": 1 When writing use a non-blocking write call. This
603 avoids getting stuck if Vim should handle other
604 messages in between, e.g. when a job sends back data
605 to Vim. It implies that when `ch_sendraw()` returns
606 not all data may have been written yet.
607 This option was added in patch 8.1.0350, test with: >
608 if has("patch-8.1.350")
609 let options['noblock'] = 1
610 endif
611<
Bram Moolenaardecb14d2016-02-20 23:32:02 +0100612 *job-callback*
613"callback": handler Callback for something to read on any part of the
614 channel.
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100615 *job-out_cb* *out_cb*
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100616"out_cb": handler Callback for when there is something to read on
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100617 stdout. Only for when the channel uses pipes. When
618 "out_cb" wasn't set the channel callback is used.
Bram Moolenaar269f5952016-07-15 22:54:41 +0200619 The two arguments are the channel and the message.
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100620
621 *job-err_cb* *err_cb*
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100622"err_cb": handler Callback for when there is something to read on
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100623 stderr. Only for when the channel uses pipes. When
624 "err_cb" wasn't set the channel callback is used.
Bram Moolenaar269f5952016-07-15 22:54:41 +0200625 The two arguments are the channel and the message.
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100626 *job-close_cb*
627"close_cb": handler Callback for when the channel is closed. Same as
Bram Moolenaar82af8712016-06-04 20:20:29 +0200628 "close_cb" on |ch_open()|, see |close_cb|.
Bram Moolenaarbc2eada2017-01-02 21:27:47 +0100629 *job-drop*
Bram Moolenaarb6e0ec62017-07-23 22:12:20 +0200630"drop": when Specifies when to drop messages. Same as "drop" on
Bram Moolenaar51628222016-12-01 23:03:28 +0100631 |ch_open()|, see |channel-drop|. For "auto" the
632 exit_cb is not considered.
Bram Moolenaarbc2eada2017-01-02 21:27:47 +0100633 *job-exit_cb*
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100634"exit_cb": handler Callback for when the job ends. The arguments are the
Bram Moolenaar38a55632016-02-15 22:07:32 +0100635 job and the exit status.
Bram Moolenaarb4ada792016-10-30 21:55:26 +0100636 Vim checks up to 10 times per second for jobs that
637 ended. The check can also be triggered by calling
638 |job_status()|, which may then invoke the exit_cb
639 handler.
Bram Moolenaar06d2d382016-05-20 17:24:11 +0200640 Note that data can be buffered, callbacks may still be
641 called after the process ends.
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100642 *job-timeout*
Bram Moolenaarb6e0ec62017-07-23 22:12:20 +0200643"timeout": time The time to wait for a request when blocking, E.g.
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100644 when using ch_evalexpr(). In milliseconds. The
645 default is 2000 (2 seconds).
646 *out_timeout* *err_timeout*
Bram Moolenaarb6e0ec62017-07-23 22:12:20 +0200647"out_timeout": time Timeout for stdout. Only when using pipes.
648"err_timeout": time Timeout for stderr. Only when using pipes.
Bram Moolenaar4f3f6682016-03-26 23:01:59 +0100649 Note: when setting "timeout" the part specific mode is
650 overwritten. Therefore set "timeout" first and the
651 part specific mode later.
652
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100653 *job-stoponexit*
654"stoponexit": {signal} Send {signal} to the job when Vim exits. See
655 |job_stop()| for possible values.
656"stoponexit": "" Do not stop the job when Vim exits.
657 The default is "term".
658
Bram Moolenaar77cdfd12016-03-12 12:57:59 +0100659 *job-term*
Bram Moolenaarb6e0ec62017-07-23 22:12:20 +0200660"term": "open" Start a terminal in a new window and connect the job
661 stdin/stdout/stderr to it. Similar to using
662 `:terminal`.
Bram Moolenaar77cdfd12016-03-12 12:57:59 +0100663 NOTE: Not implemented yet!
Bram Moolenaar38a55632016-02-15 22:07:32 +0100664
Bram Moolenaar77cdfd12016-03-12 12:57:59 +0100665"channel": {channel} Use an existing channel instead of creating a new one.
666 The parts of the channel that get used for the new job
667 will be disconnected from what they were used before.
Bram Moolenaar51628222016-12-01 23:03:28 +0100668 If the channel was still used by another job this may
Bram Moolenaar77cdfd12016-03-12 12:57:59 +0100669 cause I/O errors.
670 Existing callbacks and other settings remain.
671
Bram Moolenaarb6e0ec62017-07-23 22:12:20 +0200672"pty": 1 Use a pty (pseudo-tty) instead of a pipe when
673 possible. This is most useful in combination with a
674 terminal window, see |terminal|.
675 {only on Unix and Unix-like systems}
676
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100677 *job-in_io* *in_top* *in_bot* *in_name* *in_buf*
678"in_io": "null" disconnect stdin (read from /dev/null)
679"in_io": "pipe" stdin is connected to the channel (default)
680"in_io": "file" stdin reads from a file
681"in_io": "buffer" stdin reads from a buffer
682"in_top": number when using "buffer": first line to send (default: 1)
683"in_bot": number when using "buffer": last line to send (default: last)
684"in_name": "/path/file" the name of the file or buffer to read from
685"in_buf": number the number of the buffer to read from
Bram Moolenaar38a55632016-02-15 22:07:32 +0100686
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100687 *job-out_io* *out_name* *out_buf*
688"out_io": "null" disconnect stdout (goes to /dev/null)
689"out_io": "pipe" stdout is connected to the channel (default)
690"out_io": "file" stdout writes to a file
Bram Moolenaar51628222016-12-01 23:03:28 +0100691"out_io": "buffer" stdout appends to a buffer (see below)
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100692"out_name": "/path/file" the name of the file or buffer to write to
693"out_buf": number the number of the buffer to write to
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200694"out_modifiable": 0 when writing to a buffer, 'modifiable' will be off
695 (see below)
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200696"out_msg": 0 when writing to a new buffer, the first line will be
697 set to "Reading from channel output..."
Bram Moolenaar38a55632016-02-15 22:07:32 +0100698
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100699 *job-err_io* *err_name* *err_buf*
700"err_io": "out" stderr messages to go to stdout
701"err_io": "null" disconnect stderr (goes to /dev/null)
702"err_io": "pipe" stderr is connected to the channel (default)
703"err_io": "file" stderr writes to a file
Bram Moolenaar51628222016-12-01 23:03:28 +0100704"err_io": "buffer" stderr appends to a buffer (see below)
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100705"err_name": "/path/file" the name of the file or buffer to write to
706"err_buf": number the number of the buffer to write to
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200707"err_modifiable": 0 when writing to a buffer, 'modifiable' will be off
708 (see below)
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200709"err_msg": 0 when writing to a new buffer, the first line will be
710 set to "Reading from channel error..."
Bram Moolenaar77cdfd12016-03-12 12:57:59 +0100711
Bram Moolenaar7db8f6f2016-03-29 23:12:46 +0200712"block_write": number only for testing: pretend every other write to stdin
713 will block
714
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200715"env": dict environment variables for the new process
716"cwd": "/path/to/dir" current working directory for the new process;
717 if the directory does not exist an error is given
718
Bram Moolenaar77cdfd12016-03-12 12:57:59 +0100719
720Writing to a buffer ~
Bram Moolenaar818078d2016-08-27 21:58:42 +0200721 *out_io-buffer*
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100722When the out_io or err_io mode is "buffer" and there is a callback, the text
Bram Moolenaar5f148ec2016-03-07 22:59:26 +0100723is appended to the buffer before invoking the callback.
724
725When a buffer is used both for input and output, the output lines are put
726above the last line, since the last line is what is written to the channel
Bram Moolenaar77cdfd12016-03-12 12:57:59 +0100727input. Otherwise lines are appended below the last line.
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100728
Bram Moolenaar328da0d2016-03-04 22:22:32 +0100729When using JS or JSON mode with "buffer", only messages with zero or negative
730ID will be added to the buffer, after decoding + encoding. Messages with a
731positive number will be handled by a callback, commands are handled as usual.
732
Bram Moolenaar82af8712016-06-04 20:20:29 +0200733The name of the buffer from "out_name" or "err_name" is compared the full name
734of existing buffers, also after expanding the name for the current directory.
735E.g., when a buffer was created with ":edit somename" and the buffer name is
736"somename" it will use that buffer.
737
738If there is no matching buffer a new buffer is created. Use an empty name to
739always create a new buffer. |ch_getbufnr()| can then be used to get the
740buffer number.
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100741
742For a new buffer 'buftype' is set to "nofile" and 'bufhidden' to "hide". If
743you prefer other settings, create the buffer first and pass the buffer number.
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200744 *out_modifiable* *err_modifiable*
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200745The "out_modifiable" and "err_modifiable" options can be used to set the
746'modifiable' option off, or write to a buffer that has 'modifiable' off. That
747means that lines will be appended to the buffer, but the user can't easily
748change the buffer.
Bram Moolenaar169ebb02016-09-07 23:32:23 +0200749 *out_msg* *err_msg*
750The "out_msg" option can be used to specify whether a new buffer will have the
751first line set to "Reading from channel output...". The default is to add the
752message. "err_msg" does the same for channel error.
753
Bram Moolenaar9f5842e2016-05-29 16:17:08 +0200754When an existing buffer is to be written where 'modifiable' is off and the
755"out_modifiable" or "err_modifiable" options is not zero, an error is given
756and the buffer will not be written to.
757
Bram Moolenaar187db502016-02-27 14:44:26 +0100758When the buffer written to is displayed in a window and the cursor is in the
759first column of the last line, the cursor will be moved to the newly added
760line and the window is scrolled up to show the cursor if needed.
761
Bram Moolenaar063b9d12016-07-09 20:21:48 +0200762Undo is synced for every added line. NUL bytes are accepted (internally Vim
763stores these as NL bytes).
Bram Moolenaar38a55632016-02-15 22:07:32 +0100764
Bram Moolenaar77cdfd12016-03-12 12:57:59 +0100765
766Writing to a file ~
Bram Moolenaard6c2f052016-03-14 23:22:59 +0100767 *E920*
Bram Moolenaar77cdfd12016-03-12 12:57:59 +0100768The file is created with permissions 600 (read-write for the user, not
769accessible for others). Use |setfperm()| to change this.
770
771If the file already exists it is truncated.
772
Bram Moolenaar38a55632016-02-15 22:07:32 +0100773==============================================================================
77411. Controlling a job *job-control*
775
776To get the status of a job: >
777 echo job_status(job)
778
779To make a job stop running: >
780 job_stop(job)
781
782This is the normal way to end a job. On Unix it sends a SIGTERM to the job.
783It is possible to use other ways to stop the job, or even send arbitrary
784signals. E.g. to force a job to stop, "kill it": >
785 job_stop(job, "kill")
786
787For more options see |job_stop()|.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100788
Bram Moolenaarf2732452018-06-03 14:47:35 +0200789==============================================================================
79012. Using a prompt buffer *prompt-buffer*
791
792If you want to type input for the job in a Vim window you have a few options:
793- Use a normal buffer and handle all possible commands yourself.
794 This will be complicated, since there are so many possible commands.
795- Use a terminal window. This works well if what you type goes directly to
796 the job and the job output is directly displayed in the window.
797 See |terminal-window|.
798- Use a prompt window. This works well when entering a line for the job in Vim
799 while displaying (possibly filtered) output from the job.
800
801A prompt buffer is created by setting 'buftype' to "prompt". You would
802normally only do that in a newly created buffer.
803
804The user can edit and enter one line of text at the very last line of the
805buffer. When pressing Enter in the prompt line the callback set with
806|prompt_setcallback()| is invoked. It would normally send the line to a job.
807Another callback would receive the output from the job and display it in the
808buffer, below the prompt (and above the next prompt).
809
810Only the text in the last line, after the prompt, is editable. The rest of the
811buffer is not modifiable with Normal mode commands. It can be modified by
812calling functions, such as |append()|. Using other commands may mess up the
813buffer.
814
815After setting 'buftype' to "prompt" Vim does not automatically start Insert
816mode, use `:startinsert` if you want to enter Insert mode, so that the user
817can start typing a line.
818
819The text of the prompt can be set with the |prompt_setprompt()| function.
820
821The user can go to Normal mode and navigate through the buffer. This can be
822useful see older output or copy text.
823
Bram Moolenaard2f3a8b2018-06-19 14:35:59 +0200824The CTRL-W key can be used to start a window command, such as CTRL-W w to
825switch to the next window. This also works in Insert mode (use Shift-CTRL-W
826to delete a word). When leaving the window Insert mode will be stopped. When
827coming back to the prompt window Insert mode will be restored.
828
Bram Moolenaarf2732452018-06-03 14:47:35 +0200829Any command that starts Insert mode, such as "a", "i", "A" and "I", will move
Bram Moolenaard2f3a8b2018-06-19 14:35:59 +0200830the cursor to the last line. "A" will move to the end of the line, "I" to the
831start of the line.
Bram Moolenaarf2732452018-06-03 14:47:35 +0200832
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100833
Bram Moolenaar91f84f62018-07-29 15:07:52 +0200834 vim:tw=78:ts=8:noet:ft=help:norl: