Bram Moolenaar | 4700398 | 2021-12-05 21:54:04 +0000 | [diff] [blame] | 1 | *channel.txt* For Vim version 8.2. Last change: 2021 Nov 28 |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 2 | |
| 3 | |
| 4 | VIM REFERENCE MANUAL by Bram Moolenaar |
| 5 | |
| 6 | |
| 7 | Inter-process communication *channel* |
| 8 | |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 9 | Vim uses channels to communicate with other processes. |
Bram Moolenaar | 269f595 | 2016-07-15 22:54:41 +0200 | [diff] [blame] | 10 | A channel uses a socket or pipes. *socket-interface* |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 11 | Jobs can be used to start processes and communicate with them. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 12 | The Netbeans interface also uses a channel. |netbeans| |
| 13 | |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 14 | 1. Overview |job-channel-overview| |
| 15 | 2. Channel demo |channel-demo| |
| 16 | 3. Opening a channel |channel-open| |
| 17 | 4. Using a JSON or JS channel |channel-use| |
| 18 | 5. Channel commands |channel-commands| |
| 19 | 6. Using a RAW or NL channel |channel-raw| |
| 20 | 7. More channel functions |channel-more| |
Bram Moolenaar | 5477506 | 2019-07-31 21:07:14 +0200 | [diff] [blame] | 21 | 8. Channel functions details |channel-functions-details| |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 22 | 9. Starting a job with a channel |job-start| |
| 23 | 10. Starting a job without a channel |job-start-nochannel| |
| 24 | 11. Job functions |job-functions-details| |
| 25 | 12. Job options |job-options| |
| 26 | 13. Controlling a job |job-control| |
| 27 | 14. Using a prompt buffer |prompt-buffer| |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 28 | |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 29 | {only when compiled with the |+channel| feature for channel stuff} |
Bram Moolenaar | f37506f | 2016-08-31 22:22:10 +0200 | [diff] [blame] | 30 | You can check this with: `has('channel')` |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 31 | {only when compiled with the |+job| feature for job stuff} |
Bram Moolenaar | f37506f | 2016-08-31 22:22:10 +0200 | [diff] [blame] | 32 | You can check this with: `has('job')` |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 33 | |
| 34 | ============================================================================== |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 35 | 1. Overview *job-channel-overview* |
| 36 | |
| 37 | There are four main types of jobs: |
Bram Moolenaar | 50ba526 | 2016-09-22 22:33:02 +0200 | [diff] [blame] | 38 | 1. A daemon, serving several Vim instances. |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 39 | Vim connects to it with a socket. |
| 40 | 2. One job working with one Vim instance, asynchronously. |
| 41 | Uses a socket or pipes. |
| 42 | 3. A job performing some work for a short time, asynchronously. |
| 43 | Uses a socket or pipes. |
| 44 | 4. Running a filter, synchronously. |
| 45 | Uses pipes. |
| 46 | |
Bram Moolenaar | 77cdfd1 | 2016-03-12 12:57:59 +0100 | [diff] [blame] | 47 | For 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 Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 49 | For 4 use the ":{range}!cmd" command, see |filter|. |
| 50 | |
| 51 | Over the socket and pipes these protocols are available: |
| 52 | RAW nothing known, Vim cannot tell where a message ends |
| 53 | NL every message ends in a NL (newline) character |
| 54 | JSON JSON encoding |json_encode()| |
| 55 | JS JavaScript style JSON-like encoding |js_encode()| |
| 56 | |
| 57 | Common 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. |
Bram Moolenaar | 7dda86f | 2018-04-20 22:36:41 +0200 | [diff] [blame] | 60 | - Using a daemon, connecting over a socket in JSON mode. E.g. to lookup |
Bram Moolenaar | 0952131 | 2016-08-12 22:54:35 +0200 | [diff] [blame] | 61 | cross-references in a database. |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 62 | |
| 63 | ============================================================================== |
Bram Moolenaar | 2685212 | 2016-05-24 20:02:38 +0200 | [diff] [blame] | 64 | 2. Channel demo *channel-demo* *demoserver.py* |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 65 | |
| 66 | This requires Python. The demo program can be found in |
| 67 | $VIMRUNTIME/tools/demoserver.py |
| 68 | Run it in one terminal. We will call this T1. |
| 69 | |
| 70 | Run Vim in another terminal. Connect to the demo server with: > |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 71 | let channel = ch_open('localhost:8765') |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 72 | |
| 73 | In T1 you should see: |
| 74 | === socket opened === ~ |
| 75 | |
| 76 | You can now send a message to the server: > |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 77 | echo ch_evalexpr(channel, 'hello!') |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 78 | |
| 79 | The message is received in T1 and a response is sent back to Vim. |
| 80 | You can see the raw messages in T1. What Vim sends is: |
| 81 | [1,"hello!"] ~ |
| 82 | And the response is: |
| 83 | [1,"got it"] ~ |
| 84 | The number will increase every time you send a message. |
| 85 | |
| 86 | The server can send a command to Vim. Type this on T1 (literally, including |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 87 | the quotes): |
| 88 | ["ex","echo 'hi there'"] ~ |
| 89 | And you should see the message in Vim. You can move the cursor a word forward: |
| 90 | ["normal","w"] ~ |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 91 | |
| 92 | To handle asynchronous communication a callback needs to be used: > |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 93 | func MyHandler(channel, msg) |
Bram Moolenaar | c51cf03 | 2022-02-26 12:25:45 +0000 | [diff] [blame] | 94 | echo "from the handler: " .. a:msg |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 95 | endfunc |
Bram Moolenaar | 02e83b4 | 2016-02-21 20:10:26 +0100 | [diff] [blame] | 96 | call ch_sendexpr(channel, 'hello!', {'callback': "MyHandler"}) |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 97 | Vim will not wait for a response. Now the server can send the response later |
| 98 | and MyHandler will be invoked. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 99 | |
| 100 | Instead of giving a callback with every send call, it can also be specified |
| 101 | when opening the channel: > |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 102 | call ch_close(channel) |
| 103 | let channel = ch_open('localhost:8765', {'callback': "MyHandler"}) |
Bram Moolenaar | 4700398 | 2021-12-05 21:54:04 +0000 | [diff] [blame] | 104 | call ch_sendexpr(channel, 'hello channel!') |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 105 | |
Bram Moolenaar | 328da0d | 2016-03-04 22:22:32 +0100 | [diff] [blame] | 106 | When trying out channels it's useful to see what is going on. You can tell |
| 107 | Vim to write lines in log file: > |
| 108 | call ch_logfile('channellog', 'w') |
| 109 | See |ch_logfile()|. |
| 110 | |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 111 | ============================================================================== |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 112 | 3. Opening a channel *channel-open* |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 113 | |
Bram Moolenaar | 681baaf | 2016-02-04 20:57:07 +0100 | [diff] [blame] | 114 | To open a channel: > |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 115 | let channel = ch_open({address} [, {options}]) |
Bram Moolenaar | 02e83b4 | 2016-02-21 20:10:26 +0100 | [diff] [blame] | 116 | if ch_status(channel) == "open" |
| 117 | " use the channel |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 118 | |
| 119 | Use |ch_status()| to see if the channel could be opened. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 120 | |
| 121 | {address} has the form "hostname:port". E.g., "localhost:8765". |
| 122 | |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 123 | When using an IPv6 address, enclose it within square brackets. E.g., |
| 124 | "[2001:db8::1]:8765". |
| 125 | |
Bram Moolenaar | 4f3f668 | 2016-03-26 23:01:59 +0100 | [diff] [blame] | 126 | {options} is a dictionary with optional entries: *channel-open-options* |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 127 | |
| 128 | "mode" can be: *channel-mode* |
| 129 | "json" - Use JSON, see below; most convenient way. Default. |
Bram Moolenaar | 910b8aa | 2016-02-16 21:03:07 +0100 | [diff] [blame] | 130 | "js" - Use JS (JavaScript) encoding, more efficient than JSON. |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 131 | "nl" - Use messages that end in a NL character |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 132 | "raw" - Use raw messages |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 133 | *channel-callback* *E921* |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 134 | "callback" A function that is called when a message is received that is |
Bram Moolenaar | 4700398 | 2021-12-05 21:54:04 +0000 | [diff] [blame] | 135 | not handled otherwise (e.g. a JSON message with ID zero). It |
| 136 | gets two arguments: the channel and the received message. |
| 137 | Example: > |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 138 | func Handle(channel, msg) |
Bram Moolenaar | c51cf03 | 2022-02-26 12:25:45 +0000 | [diff] [blame] | 139 | echo 'Received: ' .. a:msg |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 140 | endfunc |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 141 | let channel = ch_open("localhost:8765", {"callback": "Handle"}) |
| 142 | < |
Bram Moolenaar | 02e83b4 | 2016-02-21 20:10:26 +0100 | [diff] [blame] | 143 | When "mode" is "json" or "js" the "msg" argument is the body |
| 144 | of the received message, converted to Vim types. |
| 145 | When "mode" is "nl" the "msg" argument is one message, |
| 146 | excluding the NL. |
| 147 | When "mode" is "raw" the "msg" argument is the whole message |
| 148 | as a string. |
Bram Moolenaar | e18c0b3 | 2016-03-20 21:08:34 +0100 | [diff] [blame] | 149 | |
| 150 | For all callbacks: Use |function()| to bind it to arguments |
Bram Moolenaar | 4f3f668 | 2016-03-26 23:01:59 +0100 | [diff] [blame] | 151 | and/or a Dictionary. Or use the form "dict.function" to bind |
| 152 | the Dictionary. |
Bram Moolenaar | 06d2d38 | 2016-05-20 17:24:11 +0200 | [diff] [blame] | 153 | |
| 154 | Callbacks are only called at a "safe" moment, usually when Vim |
| 155 | is waiting for the user to type a character. Vim does not use |
| 156 | multi-threading. |
| 157 | |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 158 | *close_cb* |
| 159 | "close_cb" A function that is called when the channel gets closed, other |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 160 | than by calling ch_close(). It should be defined like this: > |
| 161 | func MyCloseHandler(channel) |
Bram Moolenaar | 06d2d38 | 2016-05-20 17:24:11 +0200 | [diff] [blame] | 162 | < Vim will invoke callbacks that handle data before invoking |
| 163 | close_cb, thus when this function is called no more data will |
Bram Moolenaar | 68e6560 | 2019-05-26 21:33:31 +0200 | [diff] [blame] | 164 | be passed to the callbacks. However, if a callback causes Vim |
| 165 | to check for messages, the close_cb may be invoked while still |
| 166 | in the callback. The plugin must handle this somehow, it can |
| 167 | be useful to know that no more data is coming. |
Bram Moolenaar | 958dc69 | 2016-12-01 15:34:12 +0100 | [diff] [blame] | 168 | *channel-drop* |
| 169 | "drop" Specifies when to drop messages: |
| 170 | "auto" When there is no callback to handle a message. |
| 171 | The "close_cb" is also considered for this. |
| 172 | "never" All messages will be kept. |
| 173 | |
Bram Moolenaar | 0b14688 | 2018-09-06 16:27:24 +0200 | [diff] [blame] | 174 | *channel-noblock* |
| 175 | "noblock" Same effect as |job-noblock|. Only matters for writing. |
| 176 | |
Bram Moolenaar | 06d2d38 | 2016-05-20 17:24:11 +0200 | [diff] [blame] | 177 | *waittime* |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 178 | "waittime" The time to wait for the connection to be made in |
Bram Moolenaar | f391327 | 2016-02-25 00:00:01 +0100 | [diff] [blame] | 179 | milliseconds. A negative number waits forever. |
| 180 | |
| 181 | The default is zero, don't wait, which is useful if a local |
| 182 | server is supposed to be running already. On Unix Vim |
| 183 | actually uses a 1 msec timeout, that is required on many |
| 184 | systems. Use a larger value for a remote server, e.g. 10 |
| 185 | msec at least. |
Bram Moolenaar | 4f3f668 | 2016-03-26 23:01:59 +0100 | [diff] [blame] | 186 | *channel-timeout* |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 187 | "timeout" The time to wait for a request when blocking, E.g. when using |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 188 | ch_evalexpr(). In milliseconds. The default is 2000 (2 |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 189 | seconds). |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 190 | |
Bram Moolenaar | 595e64e | 2016-02-07 19:19:53 +0100 | [diff] [blame] | 191 | When "mode" is "json" or "js" the "callback" is optional. When omitted it is |
| 192 | only possible to receive a message after sending one. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 193 | |
Bram Moolenaar | 02e83b4 | 2016-02-21 20:10:26 +0100 | [diff] [blame] | 194 | To change the channel options after opening it use |ch_setoptions()|. The |
| 195 | arguments are similar to what is passed to |ch_open()|, but "waittime" cannot |
| 196 | be given, since that only applies to opening the channel. |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 197 | |
Bram Moolenaar | 02e83b4 | 2016-02-21 20:10:26 +0100 | [diff] [blame] | 198 | For example, the handler can be added or changed: > |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 199 | call ch_setoptions(channel, {'callback': callback}) |
| 200 | When "callback" is empty (zero or an empty string) the handler is removed. |
| 201 | |
Bram Moolenaar | 328da0d | 2016-03-04 22:22:32 +0100 | [diff] [blame] | 202 | After a callback has been invoked Vim will update the screen and put the |
| 203 | cursor back where it belongs. Thus the callback should not need to do |
| 204 | `:redraw`. |
| 205 | |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 206 | The timeout can be changed: > |
| 207 | call ch_setoptions(channel, {'timeout': msec}) |
| 208 | < |
Bram Moolenaar | 02e83b4 | 2016-02-21 20:10:26 +0100 | [diff] [blame] | 209 | *channel-close* *E906* |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 210 | Once done with the channel, disconnect it like this: > |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 211 | call ch_close(channel) |
| 212 | When a socket is used this will close the socket for both directions. When |
| 213 | pipes are used (stdin/stdout/stderr) they are all closed. This might not be |
| 214 | what you want! Stopping the job with job_stop() might be better. |
Bram Moolenaar | 187db50 | 2016-02-27 14:44:26 +0100 | [diff] [blame] | 215 | All readahead is discarded, callbacks will no longer be invoked. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 216 | |
Bram Moolenaar | 4f3f668 | 2016-03-26 23:01:59 +0100 | [diff] [blame] | 217 | Note that a channel is closed in three stages: |
| 218 | - The I/O ends, log message: "Closing channel". There can still be queued |
| 219 | messages to read or callbacks to invoke. |
| 220 | - The readahead is cleared, log message: "Clearing channel". Some variables |
| 221 | may still reference the channel. |
| 222 | - The channel is freed, log message: "Freeing channel". |
| 223 | |
Bram Moolenaar | cbebd48 | 2016-02-07 23:02:56 +0100 | [diff] [blame] | 224 | When the channel can't be opened you will get an error message. There is a |
| 225 | difference between MS-Windows and Unix: On Unix when the port doesn't exist |
| 226 | ch_open() fails quickly. On MS-Windows "waittime" applies. |
Bram Moolenaar | aa3b15d | 2016-04-21 08:53:19 +0200 | [diff] [blame] | 227 | *E898* *E901* *E902* |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 228 | |
| 229 | If there is an error reading or writing a channel it will be closed. |
Bram Moolenaar | 664f3cf | 2019-12-07 16:03:51 +0100 | [diff] [blame] | 230 | *E630* *E631* |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 231 | |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 232 | ============================================================================== |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 233 | 4. Using a JSON or JS channel *channel-use* |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 234 | |
Bram Moolenaar | 910b8aa | 2016-02-16 21:03:07 +0100 | [diff] [blame] | 235 | If mode is JSON then a message can be sent synchronously like this: > |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 236 | let response = ch_evalexpr(channel, {expr}) |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 237 | This awaits a response from the other side. |
| 238 | |
Bram Moolenaar | 910b8aa | 2016-02-16 21:03:07 +0100 | [diff] [blame] | 239 | When mode is JS this works the same, except that the messages use |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 240 | JavaScript encoding. See |js_encode()| for the difference. |
Bram Moolenaar | 595e64e | 2016-02-07 19:19:53 +0100 | [diff] [blame] | 241 | |
Bram Moolenaar | 02e83b4 | 2016-02-21 20:10:26 +0100 | [diff] [blame] | 242 | To send a message, without handling a response or letting the channel callback |
| 243 | handle the response: > |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 244 | call ch_sendexpr(channel, {expr}) |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 245 | |
| 246 | To send a message and letting the response handled by a specific function, |
| 247 | asynchronously: > |
Bram Moolenaar | 02e83b4 | 2016-02-21 20:10:26 +0100 | [diff] [blame] | 248 | call ch_sendexpr(channel, {expr}, {'callback': Handler}) |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 249 | |
| 250 | Vim will match the response with the request using the message ID. Once the |
| 251 | response is received the callback will be invoked. Further responses with the |
| 252 | same ID will be ignored. If your server sends back multiple responses you |
| 253 | need to send them with ID zero, they will be passed to the channel callback. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 254 | |
| 255 | The {expr} is converted to JSON and wrapped in an array. An example of the |
| 256 | message that the receiver will get when {expr} is the string "hello": |
| 257 | [12,"hello"] ~ |
| 258 | |
| 259 | The format of the JSON sent is: |
| 260 | [{number},{expr}] |
| 261 | |
| 262 | In which {number} is different every time. It must be used in the response |
| 263 | (if any): |
| 264 | |
| 265 | [{number},{response}] |
| 266 | |
| 267 | This way Vim knows which sent message matches with which received message and |
| 268 | can call the right handler. Also when the messages arrive out of order. |
| 269 | |
Bram Moolenaar | f1f0792 | 2016-08-26 17:58:53 +0200 | [diff] [blame] | 270 | A newline character is terminating the JSON text. This can be used to |
| 271 | separate the read text. For example, in Python: |
| 272 | splitidx = read_text.find('\n') |
| 273 | message = read_text[:splitidx] |
| 274 | rest = read_text[splitidx + 1:] |
| 275 | |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 276 | The sender must always send valid JSON to Vim. Vim can check for the end of |
| 277 | the message by parsing the JSON. It will only accept the message if the end |
Bram Moolenaar | f1f0792 | 2016-08-26 17:58:53 +0200 | [diff] [blame] | 278 | was received. A newline after the message is optional. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 279 | |
| 280 | When the process wants to send a message to Vim without first receiving a |
| 281 | message, it must use the number zero: |
| 282 | [0,{response}] |
| 283 | |
| 284 | Then channel handler will then get {response} converted to Vim types. If the |
| 285 | channel does not have a handler the message is dropped. |
| 286 | |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 287 | It is also possible to use ch_sendraw() and ch_evalraw() on a JSON or JS |
| 288 | channel. The caller is then completely responsible for correct encoding and |
| 289 | decoding. |
Bram Moolenaar | cbebd48 | 2016-02-07 23:02:56 +0100 | [diff] [blame] | 290 | |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 291 | ============================================================================== |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 292 | 5. Channel commands *channel-commands* |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 293 | |
Bram Moolenaar | 910b8aa | 2016-02-16 21:03:07 +0100 | [diff] [blame] | 294 | With a JSON channel the process can send commands to Vim that will be |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 295 | handled by Vim internally, it does not require a handler for the channel. |
| 296 | |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 297 | Possible commands are: *E903* *E904* *E905* |
Bram Moolenaar | 220adb1 | 2016-09-12 12:17:26 +0200 | [diff] [blame] | 298 | ["redraw", {forced}] |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 299 | ["ex", {Ex command}] |
| 300 | ["normal", {Normal mode command}] |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 301 | ["expr", {expression}, {number}] |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 302 | ["expr", {expression}] |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 303 | ["call", {func name}, {argument list}, {number}] |
| 304 | ["call", {func name}, {argument list}] |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 305 | |
| 306 | With all of these: Be careful what these commands do! You can easily |
| 307 | interfere with what the user is doing. To avoid trouble use |mode()| to check |
| 308 | that the editor is in the expected state. E.g., to send keys that must be |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 309 | inserted as text, not executed as a command: |
| 310 | ["ex","if mode() == 'i' | call feedkeys('ClassName') | endif"] ~ |
| 311 | |
| 312 | Errors in these commands are normally not reported to avoid them messing up |
| 313 | the display. If you do want to see them, set the 'verbose' option to 3 or |
| 314 | higher. |
| 315 | |
| 316 | |
| 317 | Command "redraw" ~ |
| 318 | |
Bram Moolenaar | 63b74a8 | 2019-03-24 15:09:13 +0100 | [diff] [blame] | 319 | The other commands do not explicitly update the screen, so that you can send a |
| 320 | sequence of commands without the cursor moving around. A redraw can happen as |
| 321 | a side effect of some commands. You must end with the "redraw" command to |
| 322 | show any changed text and show the cursor where it belongs. |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 323 | |
| 324 | The argument is normally an empty string: |
| 325 | ["redraw", ""] ~ |
| 326 | To first clear the screen pass "force": |
| 327 | ["redraw", "force"] ~ |
| 328 | |
| 329 | |
| 330 | Command "ex" ~ |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 331 | |
| 332 | The "ex" command is executed as any Ex command. There is no response for |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 333 | completion or error. You could use functions in an |autoload| script: |
| 334 | ["ex","call myscript#MyFunc(arg)"] |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 335 | |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 336 | You can also use "call |feedkeys()|" to insert any key sequence. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 337 | |
Bram Moolenaar | 4f3f668 | 2016-03-26 23:01:59 +0100 | [diff] [blame] | 338 | When there is an error a message is written to the channel log, if it exists, |
| 339 | and v:errmsg is set to the error. |
| 340 | |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 341 | |
| 342 | Command "normal" ~ |
| 343 | |
Bram Moolenaar | 681baaf | 2016-02-04 20:57:07 +0100 | [diff] [blame] | 344 | The "normal" command is executed like with ":normal!", commands are not |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 345 | mapped. Example to open the folds under the cursor: |
| 346 | ["normal" "zO"] |
| 347 | |
| 348 | |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 349 | Command "expr" with response ~ |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 350 | |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 351 | The "expr" command can be used to get the result of an expression. For |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 352 | example, to get the number of lines in the current buffer: |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 353 | ["expr","line('$')", -2] ~ |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 354 | |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 355 | It will send back the result of the expression: |
Bram Moolenaar | e0fa374 | 2016-02-20 15:47:01 +0100 | [diff] [blame] | 356 | [-2, "last line"] ~ |
| 357 | The format is: |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 358 | [{number}, {result}] |
Bram Moolenaar | 187db50 | 2016-02-27 14:44:26 +0100 | [diff] [blame] | 359 | |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 360 | Here {number} is the same as what was in the request. Use a negative number |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 361 | to avoid confusion with message that Vim sends. Use a different number on |
| 362 | every request to be able to match the request with the response. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 363 | |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 364 | {result} is the result of the evaluation and is JSON encoded. If the |
Bram Moolenaar | 595e64e | 2016-02-07 19:19:53 +0100 | [diff] [blame] | 365 | evaluation fails or the result can't be encoded in JSON it is the string |
| 366 | "ERROR". |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 367 | |
| 368 | |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 369 | Command "expr" without a response ~ |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 370 | |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 371 | This command is similar to "expr" above, but does not send back any response. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 372 | Example: |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 373 | ["expr","setline('$', ['one', 'two', 'three'])"] ~ |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 374 | There is no third argument in the request. |
| 375 | |
| 376 | |
| 377 | Command "call" ~ |
| 378 | |
| 379 | This is similar to "expr", but instead of passing the whole expression as a |
| 380 | string this passes the name of a function and a list of arguments. This |
| 381 | avoids the conversion of the arguments to a string and escaping and |
| 382 | concatenating them. Example: |
| 383 | ["call", "line", ["$"], -2] ~ |
| 384 | |
| 385 | Leave out the fourth argument if no response is to be sent: |
| 386 | ["call", "setline", ["$", ["one", "two", "three"]]] ~ |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 387 | |
| 388 | ============================================================================== |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 389 | 6. Using a RAW or NL channel *channel-raw* |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 390 | |
Bram Moolenaar | c0514bf | 2016-11-17 14:50:09 +0100 | [diff] [blame] | 391 | If mode is RAW or NL then a message can be sent like this: > |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 392 | let response = ch_evalraw(channel, {string}) |
Bram Moolenaar | 910b8aa | 2016-02-16 21:03:07 +0100 | [diff] [blame] | 393 | |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 394 | The {string} is sent as-is. The response will be what can be read from the |
| 395 | channel right away. Since Vim doesn't know how to recognize the end of the |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 396 | message you need to take care of it yourself. The timeout applies for reading |
| 397 | the first byte, after that it will not wait for anything more. |
| 398 | |
Bram Moolenaar | 910b8aa | 2016-02-16 21:03:07 +0100 | [diff] [blame] | 399 | If mode is "nl" you can send a message in a similar way. You are expected |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 400 | to put in the NL after each message. Thus you can also send several messages |
| 401 | ending in a NL at once. The response will be the text up to and including the |
| 402 | first NL. This can also be just the NL for an empty response. |
| 403 | If no NL was read before the channel timeout an empty string is returned. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 404 | |
| 405 | To send a message, without expecting a response: > |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 406 | call ch_sendraw(channel, {string}) |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 407 | The process can send back a response, the channel handler will be called with |
| 408 | it. |
| 409 | |
| 410 | To send a message and letting the response handled by a specific function, |
| 411 | asynchronously: > |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 412 | call ch_sendraw(channel, {string}, {'callback': 'MyHandler'}) |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 413 | |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 414 | This {string} can also be JSON, use |json_encode()| to create it and |
| 415 | |json_decode()| to handle a received JSON message. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 416 | |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 417 | It is not possible to use |ch_evalexpr()| or |ch_sendexpr()| on a raw channel. |
Bram Moolenaar | cbebd48 | 2016-02-07 23:02:56 +0100 | [diff] [blame] | 418 | |
Bram Moolenaar | 818078d | 2016-08-27 21:58:42 +0200 | [diff] [blame] | 419 | A String in Vim cannot contain NUL bytes. To send or receive NUL bytes read |
| 420 | or write from a buffer. See |in_io-buffer| and |out_io-buffer|. |
| 421 | |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 422 | ============================================================================== |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 423 | 7. More channel functions *channel-more* |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 424 | |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 425 | To obtain the status of a channel: ch_status(channel). The possible results |
| 426 | are: |
| 427 | "fail" Failed to open the channel. |
| 428 | "open" The channel can be used. |
Bram Moolenaar | 0648142 | 2016-04-30 15:13:38 +0200 | [diff] [blame] | 429 | "buffered" The channel was closed but there is data to read. |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 430 | "closed" The channel was closed. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 431 | |
Bram Moolenaar | 187db50 | 2016-02-27 14:44:26 +0100 | [diff] [blame] | 432 | To obtain the job associated with a channel: ch_getjob(channel) |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 433 | |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 434 | To read one message from a channel: > |
| 435 | let output = ch_read(channel) |
| 436 | This uses the channel timeout. To read without a timeout, just get any |
| 437 | message that is available: > |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 438 | let output = ch_read(channel, {'timeout': 0}) |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 439 | When no message was available then the result is v:none for a JSON or JS mode |
Bram Moolenaar | 4b785f6 | 2016-11-29 21:54:44 +0100 | [diff] [blame] | 440 | channels, an empty string for a RAW or NL channel. You can use |ch_canread()| |
| 441 | to check if there is something to read. |
| 442 | |
Bram Moolenaar | 05aafed | 2017-08-11 19:12:11 +0200 | [diff] [blame] | 443 | Note that when there is no callback, messages are dropped. To avoid that add |
| 444 | a close callback to the channel. |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 445 | |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 446 | To read all output from a RAW channel that is available: > |
| 447 | let output = ch_readraw(channel) |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 448 | To read the error output: > |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 449 | let output = ch_readraw(channel, {"part": "err"}) |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 450 | |
Bram Moolenaar | 4f3f668 | 2016-03-26 23:01:59 +0100 | [diff] [blame] | 451 | ch_read() and ch_readraw() use the channel timeout. When there is nothing to |
| 452 | read within that time an empty string is returned. To specify a different |
| 453 | timeout in msec use the "timeout" option: |
| 454 | {"timeout": 123} ~ |
| 455 | To read from the error output use the "part" option: |
| 456 | {"part": "err"} ~ |
| 457 | To read a message with a specific ID, on a JS or JSON channel: |
| 458 | {"id": 99} ~ |
| 459 | When no ID is specified or the ID is -1, the first message is returned. This |
| 460 | overrules any callback waiting for this message. |
| 461 | |
| 462 | For a RAW channel this returns whatever is available, since Vim does not know |
| 463 | where a message ends. |
| 464 | For a NL channel this returns one message. |
| 465 | For a JS or JSON channel this returns one decoded message. |
| 466 | This includes any sequence number. |
| 467 | |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 468 | ============================================================================== |
Bram Moolenaar | 5477506 | 2019-07-31 21:07:14 +0200 | [diff] [blame] | 469 | 8. Channel functions details *channel-functions-details* |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 470 | |
| 471 | ch_canread({handle}) *ch_canread()* |
| 472 | Return non-zero when there is something to read from {handle}. |
| 473 | {handle} can be a Channel or a Job that has a Channel. |
| 474 | |
| 475 | This is useful to read from a channel at a convenient time, |
| 476 | e.g. from a timer. |
| 477 | |
| 478 | Note that messages are dropped when the channel does not have |
| 479 | a callback. Add a close callback to avoid that. |
| 480 | |
Bram Moolenaar | 570497a | 2019-08-22 22:55:13 +0200 | [diff] [blame] | 481 | Can also be used as a |method|: > |
| 482 | GetChannel()->ch_canread() |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 483 | |
| 484 | ch_close({handle}) *ch_close()* |
| 485 | Close {handle}. See |channel-close|. |
| 486 | {handle} can be a Channel or a Job that has a Channel. |
| 487 | A close callback is not invoked. |
| 488 | |
Bram Moolenaar | 570497a | 2019-08-22 22:55:13 +0200 | [diff] [blame] | 489 | Can also be used as a |method|: > |
| 490 | GetChannel()->ch_close() |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 491 | |
| 492 | ch_close_in({handle}) *ch_close_in()* |
| 493 | Close the "in" part of {handle}. See |channel-close-in|. |
| 494 | {handle} can be a Channel or a Job that has a Channel. |
| 495 | A close callback is not invoked. |
| 496 | |
Bram Moolenaar | 570497a | 2019-08-22 22:55:13 +0200 | [diff] [blame] | 497 | Can also be used as a |method|: > |
| 498 | GetChannel()->ch_close_in() |
| 499 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 500 | |
| 501 | ch_evalexpr({handle}, {expr} [, {options}]) *ch_evalexpr()* |
| 502 | Send {expr} over {handle}. The {expr} is encoded |
| 503 | according to the type of channel. The function cannot be used |
| 504 | with a raw channel. See |channel-use|. |
| 505 | {handle} can be a Channel or a Job that has a Channel. |
| 506 | *E917* |
| 507 | {options} must be a Dictionary. It must not have a "callback" |
| 508 | entry. It can have a "timeout" entry to specify the timeout |
| 509 | for this specific request. |
| 510 | |
| 511 | ch_evalexpr() waits for a response and returns the decoded |
| 512 | expression. When there is an error or timeout it returns an |
| 513 | empty string. |
| 514 | |
Bram Moolenaar | 8fe1000 | 2019-09-11 22:56:44 +0200 | [diff] [blame] | 515 | Note that while waiting for the response, Vim handles other |
| 516 | messages. You need to make sure this doesn't cause trouble. |
| 517 | |
Bram Moolenaar | 570497a | 2019-08-22 22:55:13 +0200 | [diff] [blame] | 518 | Can also be used as a |method|: > |
| 519 | GetChannel()->ch_evalexpr(expr) |
| 520 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 521 | |
| 522 | ch_evalraw({handle}, {string} [, {options}]) *ch_evalraw()* |
| 523 | Send {string} over {handle}. |
| 524 | {handle} can be a Channel or a Job that has a Channel. |
| 525 | |
| 526 | Works like |ch_evalexpr()|, but does not encode the request or |
| 527 | decode the response. The caller is responsible for the |
| 528 | correct contents. Also does not add a newline for a channel |
| 529 | in NL mode, the caller must do that. The NL in the response |
| 530 | is removed. |
| 531 | Note that Vim does not know when the text received on a raw |
| 532 | channel is complete, it may only return the first part and you |
| 533 | need to use |ch_readraw()| to fetch the rest. |
| 534 | See |channel-use|. |
| 535 | |
Bram Moolenaar | 570497a | 2019-08-22 22:55:13 +0200 | [diff] [blame] | 536 | Can also be used as a |method|: > |
| 537 | GetChannel()->ch_evalraw(rawstring) |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 538 | |
| 539 | ch_getbufnr({handle}, {what}) *ch_getbufnr()* |
Bram Moolenaar | 6aa5729 | 2021-08-14 21:25:52 +0200 | [diff] [blame] | 540 | Get the buffer number that {handle} is using for String {what}. |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 541 | {handle} can be a Channel or a Job that has a Channel. |
| 542 | {what} can be "err" for stderr, "out" for stdout or empty for |
| 543 | socket output. |
| 544 | Returns -1 when there is no buffer. |
| 545 | |
Bram Moolenaar | 570497a | 2019-08-22 22:55:13 +0200 | [diff] [blame] | 546 | Can also be used as a |method|: > |
| 547 | GetChannel()->ch_getbufnr(what) |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 548 | |
| 549 | ch_getjob({channel}) *ch_getjob()* |
| 550 | Get the Job associated with {channel}. |
| 551 | If there is no job calling |job_status()| on the returned Job |
| 552 | will result in "fail". |
| 553 | |
Bram Moolenaar | 570497a | 2019-08-22 22:55:13 +0200 | [diff] [blame] | 554 | Can also be used as a |method|: > |
| 555 | GetChannel()->ch_getjob() |
| 556 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 557 | |
| 558 | ch_info({handle}) *ch_info()* |
| 559 | Returns a Dictionary with information about {handle}. The |
| 560 | items are: |
| 561 | "id" number of the channel |
| 562 | "status" "open", "buffered" or "closed", like |
| 563 | ch_status() |
| 564 | When opened with ch_open(): |
| 565 | "hostname" the hostname of the address |
| 566 | "port" the port of the address |
| 567 | "sock_status" "open" or "closed" |
| 568 | "sock_mode" "NL", "RAW", "JSON" or "JS" |
| 569 | "sock_io" "socket" |
| 570 | "sock_timeout" timeout in msec |
| 571 | When opened with job_start(): |
| 572 | "out_status" "open", "buffered" or "closed" |
| 573 | "out_mode" "NL", "RAW", "JSON" or "JS" |
| 574 | "out_io" "null", "pipe", "file" or "buffer" |
| 575 | "out_timeout" timeout in msec |
| 576 | "err_status" "open", "buffered" or "closed" |
| 577 | "err_mode" "NL", "RAW", "JSON" or "JS" |
| 578 | "err_io" "out", "null", "pipe", "file" or "buffer" |
| 579 | "err_timeout" timeout in msec |
| 580 | "in_status" "open" or "closed" |
| 581 | "in_mode" "NL", "RAW", "JSON" or "JS" |
| 582 | "in_io" "null", "pipe", "file" or "buffer" |
| 583 | "in_timeout" timeout in msec |
| 584 | |
Bram Moolenaar | 570497a | 2019-08-22 22:55:13 +0200 | [diff] [blame] | 585 | Can also be used as a |method|: > |
| 586 | GetChannel()->ch_info() |
| 587 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 588 | |
| 589 | ch_log({msg} [, {handle}]) *ch_log()* |
Bram Moolenaar | 6aa5729 | 2021-08-14 21:25:52 +0200 | [diff] [blame] | 590 | Write String {msg} in the channel log file, if it was opened |
| 591 | with |ch_logfile()|. |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 592 | When {handle} is passed the channel number is used for the |
| 593 | message. |
| 594 | {handle} can be a Channel or a Job that has a Channel. The |
| 595 | Channel must be open for the channel number to be used. |
| 596 | |
Bram Moolenaar | 570497a | 2019-08-22 22:55:13 +0200 | [diff] [blame] | 597 | Can also be used as a |method|: > |
| 598 | 'did something'->ch_log() |
| 599 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 600 | |
| 601 | ch_logfile({fname} [, {mode}]) *ch_logfile()* |
| 602 | Start logging channel activity to {fname}. |
| 603 | When {fname} is an empty string: stop logging. |
| 604 | |
| 605 | When {mode} is omitted or "a" append to the file. |
| 606 | When {mode} is "w" start with an empty file. |
| 607 | |
| 608 | Use |ch_log()| to write log messages. The file is flushed |
| 609 | after every message, on Unix you can use "tail -f" to see what |
| 610 | is going on in real time. |
| 611 | |
Bram Moolenaar | 077cc7a | 2020-09-04 16:35:35 +0200 | [diff] [blame] | 612 | To enable the log very early, to see what is received from a |
| 613 | terminal during startup, use |--cmd|: > |
| 614 | vim --cmd "call ch_logfile('logfile', 'w')" |
| 615 | < |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 616 | This function is not available in the |sandbox|. |
| 617 | NOTE: the channel communication is stored in the file, be |
| 618 | aware that this may contain confidential and privacy sensitive |
| 619 | information, e.g. a password you type in a terminal window. |
| 620 | |
Bram Moolenaar | 570497a | 2019-08-22 22:55:13 +0200 | [diff] [blame] | 621 | Can also be used as a |method|: > |
| 622 | 'logfile'->ch_logfile('w') |
| 623 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 624 | |
| 625 | ch_open({address} [, {options}]) *ch_open()* |
| 626 | Open a channel to {address}. See |channel|. |
| 627 | Returns a Channel. Use |ch_status()| to check for failure. |
| 628 | |
Bram Moolenaar | 6aa5729 | 2021-08-14 21:25:52 +0200 | [diff] [blame] | 629 | {address} is a String and has the form "hostname:port", e.g., |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 630 | "localhost:8765". |
| 631 | |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 632 | When using an IPv6 address, enclose it within square brackets. |
| 633 | E.g., "[2001:db8::1]:8765". |
| 634 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 635 | If {options} is given it must be a |Dictionary|. |
| 636 | See |channel-open-options|. |
| 637 | |
Bram Moolenaar | 570497a | 2019-08-22 22:55:13 +0200 | [diff] [blame] | 638 | Can also be used as a |method|: > |
| 639 | GetAddress()->ch_open() |
| 640 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 641 | |
| 642 | ch_read({handle} [, {options}]) *ch_read()* |
| 643 | Read from {handle} and return the received message. |
| 644 | {handle} can be a Channel or a Job that has a Channel. |
| 645 | For a NL channel this waits for a NL to arrive, except when |
| 646 | there is nothing more to read (channel was closed). |
| 647 | See |channel-more|. |
| 648 | |
Bram Moolenaar | 570497a | 2019-08-22 22:55:13 +0200 | [diff] [blame] | 649 | Can also be used as a |method|: > |
| 650 | GetChannel()->ch_read() |
| 651 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 652 | |
| 653 | ch_readblob({handle} [, {options}]) *ch_readblob()* |
| 654 | Like ch_read() but reads binary data and returns a |Blob|. |
| 655 | See |channel-more|. |
| 656 | |
Bram Moolenaar | 570497a | 2019-08-22 22:55:13 +0200 | [diff] [blame] | 657 | Can also be used as a |method|: > |
| 658 | GetChannel()->ch_readblob() |
| 659 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 660 | |
| 661 | ch_readraw({handle} [, {options}]) *ch_readraw()* |
| 662 | Like ch_read() but for a JS and JSON channel does not decode |
| 663 | the message. For a NL channel it does not block waiting for |
| 664 | the NL to arrive, but otherwise works like ch_read(). |
| 665 | See |channel-more|. |
| 666 | |
Bram Moolenaar | 570497a | 2019-08-22 22:55:13 +0200 | [diff] [blame] | 667 | Can also be used as a |method|: > |
| 668 | GetChannel()->ch_readraw() |
| 669 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 670 | |
| 671 | ch_sendexpr({handle}, {expr} [, {options}]) *ch_sendexpr()* |
| 672 | Send {expr} over {handle}. The {expr} is encoded |
| 673 | according to the type of channel. The function cannot be used |
| 674 | with a raw channel. |
| 675 | See |channel-use|. *E912* |
| 676 | {handle} can be a Channel or a Job that has a Channel. |
| 677 | |
Bram Moolenaar | 570497a | 2019-08-22 22:55:13 +0200 | [diff] [blame] | 678 | Can also be used as a |method|: > |
| 679 | GetChannel()->ch_sendexpr(expr) |
| 680 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 681 | |
| 682 | ch_sendraw({handle}, {expr} [, {options}]) *ch_sendraw()* |
| 683 | Send |String| or |Blob| {expr} over {handle}. |
| 684 | Works like |ch_sendexpr()|, but does not encode the request or |
| 685 | decode the response. The caller is responsible for the |
| 686 | correct contents. Also does not add a newline for a channel |
| 687 | in NL mode, the caller must do that. The NL in the response |
| 688 | is removed. |
| 689 | See |channel-use|. |
| 690 | |
Bram Moolenaar | 570497a | 2019-08-22 22:55:13 +0200 | [diff] [blame] | 691 | Can also be used as a |method|: > |
| 692 | GetChannel()->ch_sendraw(rawexpr) |
| 693 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 694 | |
| 695 | ch_setoptions({handle}, {options}) *ch_setoptions()* |
| 696 | Set options on {handle}: |
| 697 | "callback" the channel callback |
| 698 | "timeout" default read timeout in msec |
| 699 | "mode" mode for the whole channel |
| 700 | See |ch_open()| for more explanation. |
| 701 | {handle} can be a Channel or a Job that has a Channel. |
| 702 | |
| 703 | Note that changing the mode may cause queued messages to be |
| 704 | lost. |
| 705 | |
| 706 | These options cannot be changed: |
| 707 | "waittime" only applies to |ch_open()| |
| 708 | |
Bram Moolenaar | 570497a | 2019-08-22 22:55:13 +0200 | [diff] [blame] | 709 | Can also be used as a |method|: > |
| 710 | GetChannel()->ch_setoptions(options) |
| 711 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 712 | |
| 713 | ch_status({handle} [, {options}]) *ch_status()* |
| 714 | Return the status of {handle}: |
| 715 | "fail" failed to open the channel |
| 716 | "open" channel can be used |
| 717 | "buffered" channel can be read, not written to |
| 718 | "closed" channel can not be used |
| 719 | {handle} can be a Channel or a Job that has a Channel. |
| 720 | "buffered" is used when the channel was closed but there is |
| 721 | still data that can be obtained with |ch_read()|. |
| 722 | |
| 723 | If {options} is given it can contain a "part" entry to specify |
| 724 | the part of the channel to return the status for: "out" or |
| 725 | "err". For example, to get the error status: > |
| 726 | ch_status(job, {"part": "err"}) |
| 727 | < |
Bram Moolenaar | 570497a | 2019-08-22 22:55:13 +0200 | [diff] [blame] | 728 | Can also be used as a |method|: > |
| 729 | GetChannel()->ch_status() |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 730 | |
| 731 | ============================================================================== |
| 732 | 9. Starting a job with a channel *job-start* *job* |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 733 | |
| 734 | To start a job and open a channel for stdin/stdout/stderr: > |
| 735 | let job = job_start(command, {options}) |
| 736 | |
| 737 | You can get the channel with: > |
| 738 | let channel = job_getchannel(job) |
| 739 | |
| 740 | The channel will use NL mode. If you want another mode it's best to specify |
| 741 | this in {options}. When changing the mode later some text may have already |
| 742 | been received and not parsed correctly. |
| 743 | |
| 744 | If the command produces a line of output that you want to deal with, specify |
| 745 | a handler for stdout: > |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 746 | let job = job_start(command, {"out_cb": "MyHandler"}) |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 747 | The function will be called with the channel and a message. You would define |
| 748 | it like this: > |
| 749 | func MyHandler(channel, msg) |
| 750 | |
Bram Moolenaar | 02e83b4 | 2016-02-21 20:10:26 +0100 | [diff] [blame] | 751 | Without the handler you need to read the output with |ch_read()| or |
Bram Moolenaar | 0648142 | 2016-04-30 15:13:38 +0200 | [diff] [blame] | 752 | |ch_readraw()|. You can do this in the close callback, see |read-in-close-cb|. |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 753 | |
Bram Moolenaar | 1ccd8ff | 2017-08-11 19:50:37 +0200 | [diff] [blame] | 754 | Note that if the job exits before you read the output, the output may be lost. |
| 755 | This depends on the system (on Unix this happens because closing the write end |
| 756 | of a pipe causes the read end to get EOF). To avoid this make the job sleep |
| 757 | for a short while before it exits. |
| 758 | |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 759 | The handler defined for "out_cb" will not receive stderr. If you want to |
| 760 | handle that separately, add an "err_cb" handler: > |
| 761 | let job = job_start(command, {"out_cb": "MyHandler", |
| 762 | \ "err_cb": "ErrHandler"}) |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 763 | |
Bram Moolenaar | 02e83b4 | 2016-02-21 20:10:26 +0100 | [diff] [blame] | 764 | If you want to handle both stderr and stdout with one handler use the |
| 765 | "callback" option: > |
Bram Moolenaar | 664f3cf | 2019-12-07 16:03:51 +0100 | [diff] [blame] | 766 | let job = job_start(command, {"callback": "MyHandler"}) |
Bram Moolenaar | 02e83b4 | 2016-02-21 20:10:26 +0100 | [diff] [blame] | 767 | |
Bram Moolenaar | 3ec574f | 2017-06-13 18:12:01 +0200 | [diff] [blame] | 768 | Depending on the system, starting a job can put Vim in the background, the |
| 769 | started job gets the focus. To avoid that, use the `foreground()` function. |
| 770 | This might not always work when called early, put in the callback handler or |
| 771 | use a timer to call it after the job has started. |
| 772 | |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 773 | You can send a message to the command with ch_evalraw(). If the channel is in |
| 774 | JSON or JS mode you can use ch_evalexpr(). |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 775 | |
| 776 | There are several options you can use, see |job-options|. |
Bram Moolenaar | 187db50 | 2016-02-27 14:44:26 +0100 | [diff] [blame] | 777 | For example, to start a job and write its output in buffer "dummy": > |
| 778 | let logjob = job_start("tail -f /tmp/log", |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 779 | \ {'out_io': 'buffer', 'out_name': 'dummy'}) |
Bram Moolenaar | 187db50 | 2016-02-27 14:44:26 +0100 | [diff] [blame] | 780 | sbuf dummy |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 781 | |
Bram Moolenaar | 5f148ec | 2016-03-07 22:59:26 +0100 | [diff] [blame] | 782 | |
| 783 | Job input from a buffer ~ |
Bram Moolenaar | 818078d | 2016-08-27 21:58:42 +0200 | [diff] [blame] | 784 | *in_io-buffer* |
Bram Moolenaar | 328da0d | 2016-03-04 22:22:32 +0100 | [diff] [blame] | 785 | To run a job that reads from a buffer: > |
| 786 | let job = job_start({command}, |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 787 | \ {'in_io': 'buffer', 'in_name': 'mybuffer'}) |
Bram Moolenaar | 328da0d | 2016-03-04 22:22:32 +0100 | [diff] [blame] | 788 | < |
| 789 | *E915* *E918* |
| 790 | The buffer is found by name, similar to |bufnr()|. The buffer must exist and |
| 791 | be loaded when job_start() is called. |
| 792 | |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 793 | By default this reads the whole buffer. This can be changed with the "in_top" |
| 794 | and "in_bot" options. |
Bram Moolenaar | 328da0d | 2016-03-04 22:22:32 +0100 | [diff] [blame] | 795 | |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 796 | A special mode is when "in_top" is set to zero and "in_bot" is not set: Every |
Bram Moolenaar | 74675a6 | 2017-07-15 13:53:23 +0200 | [diff] [blame] | 797 | time a line is added to the buffer, the last-but-one line will be sent to the |
Bram Moolenaar | 5f148ec | 2016-03-07 22:59:26 +0100 | [diff] [blame] | 798 | job stdin. This allows for editing the last line and sending it when pressing |
| 799 | Enter. |
Bram Moolenaar | 0874a83 | 2016-09-01 15:11:51 +0200 | [diff] [blame] | 800 | *channel-close-in* |
| 801 | When not using the special mode the pipe or socket will be closed after the |
| 802 | last line has been written. This signals the reading end that the input |
| 803 | finished. You can also use |ch_close_in()| to close it sooner. |
Bram Moolenaar | 328da0d | 2016-03-04 22:22:32 +0100 | [diff] [blame] | 804 | |
Bram Moolenaar | 063b9d1 | 2016-07-09 20:21:48 +0200 | [diff] [blame] | 805 | NUL bytes in the text will be passed to the job (internally Vim stores these |
| 806 | as NL bytes). |
| 807 | |
Bram Moolenaar | 0648142 | 2016-04-30 15:13:38 +0200 | [diff] [blame] | 808 | |
| 809 | Reading job output in the close callback ~ |
| 810 | *read-in-close-cb* |
| 811 | If the job can take some time and you don't need intermediate results, you can |
| 812 | add a close callback and read the output there: > |
| 813 | |
| 814 | func! CloseHandler(channel) |
Bram Moolenaar | 2ec618c | 2016-10-01 14:47:05 +0200 | [diff] [blame] | 815 | while ch_status(a:channel, {'part': 'out'}) == 'buffered' |
Bram Moolenaar | 0648142 | 2016-04-30 15:13:38 +0200 | [diff] [blame] | 816 | echomsg ch_read(a:channel) |
| 817 | endwhile |
| 818 | endfunc |
| 819 | let job = job_start(command, {'close_cb': 'CloseHandler'}) |
| 820 | |
| 821 | You will want to do something more useful than "echomsg". |
| 822 | |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 823 | ============================================================================== |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 824 | 10. Starting a job without a channel *job-start-nochannel* |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 825 | |
| 826 | To start another process without creating a channel: > |
Bram Moolenaar | 328da0d | 2016-03-04 22:22:32 +0100 | [diff] [blame] | 827 | let job = job_start(command, |
Bram Moolenaar | 5162822 | 2016-12-01 23:03:28 +0100 | [diff] [blame] | 828 | \ {"in_io": "null", "out_io": "null", "err_io": "null"}) |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 829 | |
| 830 | This starts {command} in the background, Vim does not wait for it to finish. |
| 831 | |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 832 | When Vim sees that neither stdin, stdout or stderr are connected, no channel |
| 833 | will be created. Often you will want to include redirection in the command to |
| 834 | avoid it getting stuck. |
| 835 | |
| 836 | There are several options you can use, see |job-options|. |
| 837 | |
Bram Moolenaar | 77cdfd1 | 2016-03-12 12:57:59 +0100 | [diff] [blame] | 838 | *job-start-if-needed* |
| 839 | To start a job only when connecting to an address does not work, do something |
| 840 | like this: > |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 841 | let channel = ch_open(address, {"waittime": 0}) |
| 842 | if ch_status(channel) == "fail" |
| 843 | let job = job_start(command) |
| 844 | let channel = ch_open(address, {"waittime": 1000}) |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 845 | endif |
Bram Moolenaar | 77cdfd1 | 2016-03-12 12:57:59 +0100 | [diff] [blame] | 846 | |
| 847 | Note that the waittime for ch_open() gives the job one second to make the port |
| 848 | available. |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 849 | |
| 850 | ============================================================================== |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 851 | 11. Job functions *job-functions-details* |
| 852 | |
| 853 | job_getchannel({job}) *job_getchannel()* |
| 854 | Get the channel handle that {job} is using. |
| 855 | To check if the job has no channel: > |
Yegappan Lakshmanan | 1a71d31 | 2021-07-15 12:49:58 +0200 | [diff] [blame] | 856 | if string(job_getchannel(job)) == 'channel fail' |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 857 | < |
Bram Moolenaar | 570497a | 2019-08-22 22:55:13 +0200 | [diff] [blame] | 858 | Can also be used as a |method|: > |
| 859 | GetJob()->job_getchannel() |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 860 | |
| 861 | job_info([{job}]) *job_info()* |
| 862 | Returns a Dictionary with information about {job}: |
| 863 | "status" what |job_status()| returns |
| 864 | "channel" what |job_getchannel()| returns |
| 865 | "cmd" List of command arguments used to start the job |
| 866 | "process" process ID |
| 867 | "tty_in" terminal input name, empty when none |
| 868 | "tty_out" terminal output name, empty when none |
| 869 | "exitval" only valid when "status" is "dead" |
| 870 | "exit_cb" function to be called on exit |
| 871 | "stoponexit" |job-stoponexit| |
| 872 | |
| 873 | Only in Unix: |
| 874 | "termsig" the signal which terminated the process |
| 875 | (See |job_stop()| for the values) |
| 876 | only valid when "status" is "dead" |
| 877 | |
| 878 | Only in MS-Windows: |
| 879 | "tty_type" Type of virtual console in use. |
| 880 | Values are "winpty" or "conpty". |
| 881 | See 'termwintype'. |
| 882 | |
| 883 | Without any arguments, returns a List with all Job objects. |
| 884 | |
Bram Moolenaar | 570497a | 2019-08-22 22:55:13 +0200 | [diff] [blame] | 885 | Can also be used as a |method|: > |
| 886 | GetJob()->job_info() |
| 887 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 888 | |
| 889 | job_setoptions({job}, {options}) *job_setoptions()* |
| 890 | Change options for {job}. Supported are: |
| 891 | "stoponexit" |job-stoponexit| |
| 892 | "exit_cb" |job-exit_cb| |
| 893 | |
Bram Moolenaar | 570497a | 2019-08-22 22:55:13 +0200 | [diff] [blame] | 894 | Can also be used as a |method|: > |
| 895 | GetJob()->job_setoptions(options) |
| 896 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 897 | |
| 898 | job_start({command} [, {options}]) *job_start()* |
| 899 | Start a job and return a Job object. Unlike |system()| and |
| 900 | |:!cmd| this does not wait for the job to finish. |
| 901 | To start a job in a terminal window see |term_start()|. |
| 902 | |
| 903 | If the job fails to start then |job_status()| on the returned |
| 904 | Job object results in "fail" and none of the callbacks will be |
| 905 | invoked. |
| 906 | |
| 907 | {command} can be a String. This works best on MS-Windows. On |
| 908 | Unix it is split up in white-separated parts to be passed to |
| 909 | execvp(). Arguments in double quotes can contain white space. |
| 910 | |
| 911 | {command} can be a List, where the first item is the executable |
| 912 | and further items are the arguments. All items are converted |
| 913 | to String. This works best on Unix. |
| 914 | |
| 915 | On MS-Windows, job_start() makes a GUI application hidden. If |
| 916 | want to show it, Use |:!start| instead. |
| 917 | |
| 918 | The command is executed directly, not through a shell, the |
| 919 | 'shell' option is not used. To use the shell: > |
| 920 | let job = job_start(["/bin/sh", "-c", "echo hello"]) |
| 921 | < Or: > |
| 922 | let job = job_start('/bin/sh -c "echo hello"') |
| 923 | < Note that this will start two processes, the shell and the |
| 924 | command it executes. If you don't want this use the "exec" |
| 925 | shell command. |
| 926 | |
| 927 | On Unix $PATH is used to search for the executable only when |
| 928 | the command does not contain a slash. |
| 929 | |
| 930 | The job will use the same terminal as Vim. If it reads from |
| 931 | stdin the job and Vim will be fighting over input, that |
| 932 | doesn't work. Redirect stdin and stdout to avoid problems: > |
| 933 | let job = job_start(['sh', '-c', "myserver </dev/null >/dev/null"]) |
| 934 | < |
| 935 | The returned Job object can be used to get the status with |
| 936 | |job_status()| and stop the job with |job_stop()|. |
| 937 | |
| 938 | Note that the job object will be deleted if there are no |
| 939 | references to it. This closes the stdin and stderr, which may |
| 940 | cause the job to fail with an error. To avoid this keep a |
| 941 | reference to the job. Thus instead of: > |
| 942 | call job_start('my-command') |
| 943 | < use: > |
| 944 | let myjob = job_start('my-command') |
| 945 | < and unlet "myjob" once the job is not needed or is past the |
| 946 | point where it would fail (e.g. when it prints a message on |
| 947 | startup). Keep in mind that variables local to a function |
| 948 | will cease to exist if the function returns. Use a |
| 949 | script-local variable if needed: > |
| 950 | let s:myjob = job_start('my-command') |
| 951 | < |
| 952 | {options} must be a Dictionary. It can contain many optional |
| 953 | items, see |job-options|. |
| 954 | |
Bram Moolenaar | 570497a | 2019-08-22 22:55:13 +0200 | [diff] [blame] | 955 | Can also be used as a |method|: > |
| 956 | BuildCommand()->job_start() |
| 957 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 958 | |
| 959 | job_status({job}) *job_status()* *E916* |
| 960 | Returns a String with the status of {job}: |
| 961 | "run" job is running |
| 962 | "fail" job failed to start |
| 963 | "dead" job died or was stopped after running |
| 964 | |
| 965 | On Unix a non-existing command results in "dead" instead of |
| 966 | "fail", because a fork happens before the failure can be |
| 967 | detected. |
| 968 | |
Bram Moolenaar | cb80aa2 | 2020-10-26 21:12:46 +0100 | [diff] [blame] | 969 | If in Vim9 script a variable is declared with type "job" but |
| 970 | never assigned to, passing that variable to job_status() |
| 971 | returns "fail". |
| 972 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 973 | If an exit callback was set with the "exit_cb" option and the |
| 974 | job is now detected to be "dead" the callback will be invoked. |
| 975 | |
| 976 | For more information see |job_info()|. |
| 977 | |
Bram Moolenaar | 570497a | 2019-08-22 22:55:13 +0200 | [diff] [blame] | 978 | Can also be used as a |method|: > |
| 979 | GetJob()->job_status() |
| 980 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 981 | |
| 982 | job_stop({job} [, {how}]) *job_stop()* |
| 983 | Stop the {job}. This can also be used to signal the job. |
| 984 | |
| 985 | When {how} is omitted or is "term" the job will be terminated. |
| 986 | For Unix SIGTERM is sent. On MS-Windows the job will be |
| 987 | terminated forcedly (there is no "gentle" way). |
| 988 | This goes to the process group, thus children may also be |
| 989 | affected. |
| 990 | |
| 991 | Effect for Unix: |
| 992 | "term" SIGTERM (default) |
| 993 | "hup" SIGHUP |
| 994 | "quit" SIGQUIT |
| 995 | "int" SIGINT |
| 996 | "kill" SIGKILL (strongest way to stop) |
| 997 | number signal with that number |
| 998 | |
| 999 | Effect for MS-Windows: |
| 1000 | "term" terminate process forcedly (default) |
| 1001 | "hup" CTRL_BREAK |
| 1002 | "quit" CTRL_BREAK |
| 1003 | "int" CTRL_C |
| 1004 | "kill" terminate process forcedly |
| 1005 | Others CTRL_BREAK |
| 1006 | |
| 1007 | On Unix the signal is sent to the process group. This means |
| 1008 | that when the job is "sh -c command" it affects both the shell |
| 1009 | and the command. |
| 1010 | |
| 1011 | The result is a Number: 1 if the operation could be executed, |
| 1012 | 0 if "how" is not supported on the system. |
| 1013 | Note that even when the operation was executed, whether the |
| 1014 | job was actually stopped needs to be checked with |
| 1015 | |job_status()|. |
| 1016 | |
| 1017 | If the status of the job is "dead", the signal will not be |
| 1018 | sent. This is to avoid to stop the wrong job (esp. on Unix, |
| 1019 | where process numbers are recycled). |
| 1020 | |
| 1021 | When using "kill" Vim will assume the job will die and close |
| 1022 | the channel. |
| 1023 | |
Bram Moolenaar | 570497a | 2019-08-22 22:55:13 +0200 | [diff] [blame] | 1024 | Can also be used as a |method|: > |
| 1025 | GetJob()->job_stop() |
| 1026 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 1027 | |
| 1028 | ============================================================================== |
| 1029 | 12. Job options *job-options* |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 1030 | |
| 1031 | The {options} argument in job_start() is a dictionary. All entries are |
Bram Moolenaar | 02e83b4 | 2016-02-21 20:10:26 +0100 | [diff] [blame] | 1032 | optional. Some options can be used after the job has started, using |
| 1033 | job_setoptions(job, {options}). Many options can be used with the channel |
| 1034 | related to the job, using ch_setoptions(channel, {options}). |
| 1035 | See |job_setoptions()| and |ch_setoptions()|. |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 1036 | |
Bram Moolenaar | 4f3f668 | 2016-03-26 23:01:59 +0100 | [diff] [blame] | 1037 | *in_mode* *out_mode* *err_mode* |
| 1038 | "in_mode" mode specifically for stdin, only when using pipes |
| 1039 | "out_mode" mode specifically for stdout, only when using pipes |
| 1040 | "err_mode" mode specifically for stderr, only when using pipes |
| 1041 | See |channel-mode| for the values. |
| 1042 | |
| 1043 | Note: when setting "mode" the part specific mode is |
| 1044 | overwritten. Therefore set "mode" first and the part |
| 1045 | specific mode later. |
| 1046 | |
| 1047 | Note: when writing to a file or buffer and when |
| 1048 | reading from a buffer NL mode is used by default. |
| 1049 | |
Bram Moolenaar | 0b14688 | 2018-09-06 16:27:24 +0200 | [diff] [blame] | 1050 | *job-noblock* |
| 1051 | "noblock": 1 When writing use a non-blocking write call. This |
| 1052 | avoids getting stuck if Vim should handle other |
| 1053 | messages in between, e.g. when a job sends back data |
| 1054 | to Vim. It implies that when `ch_sendraw()` returns |
| 1055 | not all data may have been written yet. |
| 1056 | This option was added in patch 8.1.0350, test with: > |
| 1057 | if has("patch-8.1.350") |
| 1058 | let options['noblock'] = 1 |
| 1059 | endif |
| 1060 | < |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 1061 | *job-callback* |
| 1062 | "callback": handler Callback for something to read on any part of the |
| 1063 | channel. |
Bram Moolenaar | 4f3f668 | 2016-03-26 23:01:59 +0100 | [diff] [blame] | 1064 | *job-out_cb* *out_cb* |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 1065 | "out_cb": handler Callback for when there is something to read on |
Bram Moolenaar | 4f3f668 | 2016-03-26 23:01:59 +0100 | [diff] [blame] | 1066 | stdout. Only for when the channel uses pipes. When |
| 1067 | "out_cb" wasn't set the channel callback is used. |
Bram Moolenaar | 269f595 | 2016-07-15 22:54:41 +0200 | [diff] [blame] | 1068 | The two arguments are the channel and the message. |
Bram Moolenaar | 4f3f668 | 2016-03-26 23:01:59 +0100 | [diff] [blame] | 1069 | |
| 1070 | *job-err_cb* *err_cb* |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 1071 | "err_cb": handler Callback for when there is something to read on |
Bram Moolenaar | 4f3f668 | 2016-03-26 23:01:59 +0100 | [diff] [blame] | 1072 | stderr. Only for when the channel uses pipes. When |
| 1073 | "err_cb" wasn't set the channel callback is used. |
Bram Moolenaar | 269f595 | 2016-07-15 22:54:41 +0200 | [diff] [blame] | 1074 | The two arguments are the channel and the message. |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 1075 | *job-close_cb* |
| 1076 | "close_cb": handler Callback for when the channel is closed. Same as |
Bram Moolenaar | 82af871 | 2016-06-04 20:20:29 +0200 | [diff] [blame] | 1077 | "close_cb" on |ch_open()|, see |close_cb|. |
Bram Moolenaar | bc2eada | 2017-01-02 21:27:47 +0100 | [diff] [blame] | 1078 | *job-drop* |
Bram Moolenaar | b6e0ec6 | 2017-07-23 22:12:20 +0200 | [diff] [blame] | 1079 | "drop": when Specifies when to drop messages. Same as "drop" on |
Bram Moolenaar | 5162822 | 2016-12-01 23:03:28 +0100 | [diff] [blame] | 1080 | |ch_open()|, see |channel-drop|. For "auto" the |
| 1081 | exit_cb is not considered. |
Bram Moolenaar | bc2eada | 2017-01-02 21:27:47 +0100 | [diff] [blame] | 1082 | *job-exit_cb* |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 1083 | "exit_cb": handler Callback for when the job ends. The arguments are the |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 1084 | job and the exit status. |
Bram Moolenaar | b4ada79 | 2016-10-30 21:55:26 +0100 | [diff] [blame] | 1085 | Vim checks up to 10 times per second for jobs that |
| 1086 | ended. The check can also be triggered by calling |
| 1087 | |job_status()|, which may then invoke the exit_cb |
| 1088 | handler. |
Bram Moolenaar | 06d2d38 | 2016-05-20 17:24:11 +0200 | [diff] [blame] | 1089 | Note that data can be buffered, callbacks may still be |
| 1090 | called after the process ends. |
Bram Moolenaar | 4f3f668 | 2016-03-26 23:01:59 +0100 | [diff] [blame] | 1091 | *job-timeout* |
Bram Moolenaar | b6e0ec6 | 2017-07-23 22:12:20 +0200 | [diff] [blame] | 1092 | "timeout": time The time to wait for a request when blocking, E.g. |
Bram Moolenaar | 4f3f668 | 2016-03-26 23:01:59 +0100 | [diff] [blame] | 1093 | when using ch_evalexpr(). In milliseconds. The |
| 1094 | default is 2000 (2 seconds). |
| 1095 | *out_timeout* *err_timeout* |
Bram Moolenaar | b6e0ec6 | 2017-07-23 22:12:20 +0200 | [diff] [blame] | 1096 | "out_timeout": time Timeout for stdout. Only when using pipes. |
| 1097 | "err_timeout": time Timeout for stderr. Only when using pipes. |
Bram Moolenaar | 4f3f668 | 2016-03-26 23:01:59 +0100 | [diff] [blame] | 1098 | Note: when setting "timeout" the part specific mode is |
| 1099 | overwritten. Therefore set "timeout" first and the |
| 1100 | part specific mode later. |
| 1101 | |
Bram Moolenaar | 02e83b4 | 2016-02-21 20:10:26 +0100 | [diff] [blame] | 1102 | *job-stoponexit* |
| 1103 | "stoponexit": {signal} Send {signal} to the job when Vim exits. See |
| 1104 | |job_stop()| for possible values. |
| 1105 | "stoponexit": "" Do not stop the job when Vim exits. |
| 1106 | The default is "term". |
| 1107 | |
Bram Moolenaar | 77cdfd1 | 2016-03-12 12:57:59 +0100 | [diff] [blame] | 1108 | *job-term* |
Bram Moolenaar | b6e0ec6 | 2017-07-23 22:12:20 +0200 | [diff] [blame] | 1109 | "term": "open" Start a terminal in a new window and connect the job |
| 1110 | stdin/stdout/stderr to it. Similar to using |
| 1111 | `:terminal`. |
Bram Moolenaar | 77cdfd1 | 2016-03-12 12:57:59 +0100 | [diff] [blame] | 1112 | NOTE: Not implemented yet! |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 1113 | |
Bram Moolenaar | 77cdfd1 | 2016-03-12 12:57:59 +0100 | [diff] [blame] | 1114 | "channel": {channel} Use an existing channel instead of creating a new one. |
| 1115 | The parts of the channel that get used for the new job |
| 1116 | will be disconnected from what they were used before. |
Bram Moolenaar | 5162822 | 2016-12-01 23:03:28 +0100 | [diff] [blame] | 1117 | If the channel was still used by another job this may |
Bram Moolenaar | 77cdfd1 | 2016-03-12 12:57:59 +0100 | [diff] [blame] | 1118 | cause I/O errors. |
| 1119 | Existing callbacks and other settings remain. |
| 1120 | |
Bram Moolenaar | b6e0ec6 | 2017-07-23 22:12:20 +0200 | [diff] [blame] | 1121 | "pty": 1 Use a pty (pseudo-tty) instead of a pipe when |
| 1122 | possible. This is most useful in combination with a |
| 1123 | terminal window, see |terminal|. |
| 1124 | {only on Unix and Unix-like systems} |
| 1125 | |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 1126 | *job-in_io* *in_top* *in_bot* *in_name* *in_buf* |
| 1127 | "in_io": "null" disconnect stdin (read from /dev/null) |
| 1128 | "in_io": "pipe" stdin is connected to the channel (default) |
| 1129 | "in_io": "file" stdin reads from a file |
| 1130 | "in_io": "buffer" stdin reads from a buffer |
| 1131 | "in_top": number when using "buffer": first line to send (default: 1) |
| 1132 | "in_bot": number when using "buffer": last line to send (default: last) |
| 1133 | "in_name": "/path/file" the name of the file or buffer to read from |
| 1134 | "in_buf": number the number of the buffer to read from |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 1135 | |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 1136 | *job-out_io* *out_name* *out_buf* |
| 1137 | "out_io": "null" disconnect stdout (goes to /dev/null) |
| 1138 | "out_io": "pipe" stdout is connected to the channel (default) |
| 1139 | "out_io": "file" stdout writes to a file |
Bram Moolenaar | 5162822 | 2016-12-01 23:03:28 +0100 | [diff] [blame] | 1140 | "out_io": "buffer" stdout appends to a buffer (see below) |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 1141 | "out_name": "/path/file" the name of the file or buffer to write to |
| 1142 | "out_buf": number the number of the buffer to write to |
Bram Moolenaar | 9f5842e | 2016-05-29 16:17:08 +0200 | [diff] [blame] | 1143 | "out_modifiable": 0 when writing to a buffer, 'modifiable' will be off |
| 1144 | (see below) |
Bram Moolenaar | 169ebb0 | 2016-09-07 23:32:23 +0200 | [diff] [blame] | 1145 | "out_msg": 0 when writing to a new buffer, the first line will be |
| 1146 | set to "Reading from channel output..." |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 1147 | |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 1148 | *job-err_io* *err_name* *err_buf* |
| 1149 | "err_io": "out" stderr messages to go to stdout |
| 1150 | "err_io": "null" disconnect stderr (goes to /dev/null) |
| 1151 | "err_io": "pipe" stderr is connected to the channel (default) |
| 1152 | "err_io": "file" stderr writes to a file |
Bram Moolenaar | 5162822 | 2016-12-01 23:03:28 +0100 | [diff] [blame] | 1153 | "err_io": "buffer" stderr appends to a buffer (see below) |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 1154 | "err_name": "/path/file" the name of the file or buffer to write to |
| 1155 | "err_buf": number the number of the buffer to write to |
Bram Moolenaar | 9f5842e | 2016-05-29 16:17:08 +0200 | [diff] [blame] | 1156 | "err_modifiable": 0 when writing to a buffer, 'modifiable' will be off |
| 1157 | (see below) |
Bram Moolenaar | 169ebb0 | 2016-09-07 23:32:23 +0200 | [diff] [blame] | 1158 | "err_msg": 0 when writing to a new buffer, the first line will be |
| 1159 | set to "Reading from channel error..." |
Bram Moolenaar | 77cdfd1 | 2016-03-12 12:57:59 +0100 | [diff] [blame] | 1160 | |
Bram Moolenaar | 7db8f6f | 2016-03-29 23:12:46 +0200 | [diff] [blame] | 1161 | "block_write": number only for testing: pretend every other write to stdin |
| 1162 | will block |
| 1163 | |
Bram Moolenaar | 05aafed | 2017-08-11 19:12:11 +0200 | [diff] [blame] | 1164 | "env": dict environment variables for the new process |
| 1165 | "cwd": "/path/to/dir" current working directory for the new process; |
| 1166 | if the directory does not exist an error is given |
| 1167 | |
Bram Moolenaar | 77cdfd1 | 2016-03-12 12:57:59 +0100 | [diff] [blame] | 1168 | |
| 1169 | Writing to a buffer ~ |
Bram Moolenaar | 818078d | 2016-08-27 21:58:42 +0200 | [diff] [blame] | 1170 | *out_io-buffer* |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 1171 | When the out_io or err_io mode is "buffer" and there is a callback, the text |
Bram Moolenaar | 5f148ec | 2016-03-07 22:59:26 +0100 | [diff] [blame] | 1172 | is appended to the buffer before invoking the callback. |
| 1173 | |
| 1174 | When a buffer is used both for input and output, the output lines are put |
| 1175 | above the last line, since the last line is what is written to the channel |
Bram Moolenaar | 77cdfd1 | 2016-03-12 12:57:59 +0100 | [diff] [blame] | 1176 | input. Otherwise lines are appended below the last line. |
Bram Moolenaar | c7f0ebc | 2016-02-27 21:10:09 +0100 | [diff] [blame] | 1177 | |
Bram Moolenaar | 328da0d | 2016-03-04 22:22:32 +0100 | [diff] [blame] | 1178 | When using JS or JSON mode with "buffer", only messages with zero or negative |
| 1179 | ID will be added to the buffer, after decoding + encoding. Messages with a |
| 1180 | positive number will be handled by a callback, commands are handled as usual. |
| 1181 | |
Bram Moolenaar | 82af871 | 2016-06-04 20:20:29 +0200 | [diff] [blame] | 1182 | The name of the buffer from "out_name" or "err_name" is compared the full name |
| 1183 | of existing buffers, also after expanding the name for the current directory. |
| 1184 | E.g., when a buffer was created with ":edit somename" and the buffer name is |
| 1185 | "somename" it will use that buffer. |
| 1186 | |
| 1187 | If there is no matching buffer a new buffer is created. Use an empty name to |
| 1188 | always create a new buffer. |ch_getbufnr()| can then be used to get the |
| 1189 | buffer number. |
Bram Moolenaar | c7f0ebc | 2016-02-27 21:10:09 +0100 | [diff] [blame] | 1190 | |
| 1191 | For a new buffer 'buftype' is set to "nofile" and 'bufhidden' to "hide". If |
| 1192 | you prefer other settings, create the buffer first and pass the buffer number. |
Bram Moolenaar | 169ebb0 | 2016-09-07 23:32:23 +0200 | [diff] [blame] | 1193 | *out_modifiable* *err_modifiable* |
Bram Moolenaar | 9f5842e | 2016-05-29 16:17:08 +0200 | [diff] [blame] | 1194 | The "out_modifiable" and "err_modifiable" options can be used to set the |
| 1195 | 'modifiable' option off, or write to a buffer that has 'modifiable' off. That |
| 1196 | means that lines will be appended to the buffer, but the user can't easily |
| 1197 | change the buffer. |
Bram Moolenaar | 169ebb0 | 2016-09-07 23:32:23 +0200 | [diff] [blame] | 1198 | *out_msg* *err_msg* |
| 1199 | The "out_msg" option can be used to specify whether a new buffer will have the |
| 1200 | first line set to "Reading from channel output...". The default is to add the |
| 1201 | message. "err_msg" does the same for channel error. |
| 1202 | |
Bram Moolenaar | 9f5842e | 2016-05-29 16:17:08 +0200 | [diff] [blame] | 1203 | When an existing buffer is to be written where 'modifiable' is off and the |
| 1204 | "out_modifiable" or "err_modifiable" options is not zero, an error is given |
| 1205 | and the buffer will not be written to. |
| 1206 | |
Bram Moolenaar | 187db50 | 2016-02-27 14:44:26 +0100 | [diff] [blame] | 1207 | When the buffer written to is displayed in a window and the cursor is in the |
| 1208 | first column of the last line, the cursor will be moved to the newly added |
| 1209 | line and the window is scrolled up to show the cursor if needed. |
| 1210 | |
Bram Moolenaar | 063b9d1 | 2016-07-09 20:21:48 +0200 | [diff] [blame] | 1211 | Undo is synced for every added line. NUL bytes are accepted (internally Vim |
| 1212 | stores these as NL bytes). |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 1213 | |
Bram Moolenaar | 77cdfd1 | 2016-03-12 12:57:59 +0100 | [diff] [blame] | 1214 | |
| 1215 | Writing to a file ~ |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 1216 | *E920* |
Bram Moolenaar | 77cdfd1 | 2016-03-12 12:57:59 +0100 | [diff] [blame] | 1217 | The file is created with permissions 600 (read-write for the user, not |
| 1218 | accessible for others). Use |setfperm()| to change this. |
| 1219 | |
| 1220 | If the file already exists it is truncated. |
| 1221 | |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 1222 | ============================================================================== |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 1223 | 13. Controlling a job *job-control* |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 1224 | |
| 1225 | To get the status of a job: > |
| 1226 | echo job_status(job) |
| 1227 | |
| 1228 | To make a job stop running: > |
| 1229 | job_stop(job) |
| 1230 | |
| 1231 | This is the normal way to end a job. On Unix it sends a SIGTERM to the job. |
| 1232 | It is possible to use other ways to stop the job, or even send arbitrary |
| 1233 | signals. E.g. to force a job to stop, "kill it": > |
| 1234 | job_stop(job, "kill") |
| 1235 | |
| 1236 | For more options see |job_stop()|. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 1237 | |
Bram Moolenaar | f273245 | 2018-06-03 14:47:35 +0200 | [diff] [blame] | 1238 | ============================================================================== |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 1239 | 14. Using a prompt buffer *prompt-buffer* |
Bram Moolenaar | f273245 | 2018-06-03 14:47:35 +0200 | [diff] [blame] | 1240 | |
| 1241 | If you want to type input for the job in a Vim window you have a few options: |
| 1242 | - Use a normal buffer and handle all possible commands yourself. |
| 1243 | This will be complicated, since there are so many possible commands. |
| 1244 | - Use a terminal window. This works well if what you type goes directly to |
| 1245 | the job and the job output is directly displayed in the window. |
| 1246 | See |terminal-window|. |
Bram Moolenaar | acc2240 | 2020-06-07 21:07:18 +0200 | [diff] [blame] | 1247 | - Use a window with a prompt buffer. This works well when entering a line for |
| 1248 | the job in Vim while displaying (possibly filtered) output from the job. |
Bram Moolenaar | f273245 | 2018-06-03 14:47:35 +0200 | [diff] [blame] | 1249 | |
| 1250 | A prompt buffer is created by setting 'buftype' to "prompt". You would |
| 1251 | normally only do that in a newly created buffer. |
| 1252 | |
| 1253 | The user can edit and enter one line of text at the very last line of the |
| 1254 | buffer. When pressing Enter in the prompt line the callback set with |
| 1255 | |prompt_setcallback()| is invoked. It would normally send the line to a job. |
| 1256 | Another callback would receive the output from the job and display it in the |
| 1257 | buffer, below the prompt (and above the next prompt). |
| 1258 | |
| 1259 | Only the text in the last line, after the prompt, is editable. The rest of the |
| 1260 | buffer is not modifiable with Normal mode commands. It can be modified by |
| 1261 | calling functions, such as |append()|. Using other commands may mess up the |
| 1262 | buffer. |
| 1263 | |
| 1264 | After setting 'buftype' to "prompt" Vim does not automatically start Insert |
| 1265 | mode, use `:startinsert` if you want to enter Insert mode, so that the user |
| 1266 | can start typing a line. |
| 1267 | |
Bram Moolenaar | 077cc7a | 2020-09-04 16:35:35 +0200 | [diff] [blame] | 1268 | The text of the prompt can be set with the |prompt_setprompt()| function. If |
| 1269 | no prompt is set with |prompt_setprompt()|, "% " is used. You can get the |
| 1270 | effective prompt text for a buffer, with |prompt_getprompt()|. |
Bram Moolenaar | f273245 | 2018-06-03 14:47:35 +0200 | [diff] [blame] | 1271 | |
| 1272 | The user can go to Normal mode and navigate through the buffer. This can be |
Bram Moolenaar | 7ff7846 | 2020-07-10 22:00:53 +0200 | [diff] [blame] | 1273 | useful to see older output or copy text. |
Bram Moolenaar | f273245 | 2018-06-03 14:47:35 +0200 | [diff] [blame] | 1274 | |
Bram Moolenaar | d2f3a8b | 2018-06-19 14:35:59 +0200 | [diff] [blame] | 1275 | The CTRL-W key can be used to start a window command, such as CTRL-W w to |
| 1276 | switch to the next window. This also works in Insert mode (use Shift-CTRL-W |
| 1277 | to delete a word). When leaving the window Insert mode will be stopped. When |
| 1278 | coming back to the prompt window Insert mode will be restored. |
| 1279 | |
Bram Moolenaar | f273245 | 2018-06-03 14:47:35 +0200 | [diff] [blame] | 1280 | Any command that starts Insert mode, such as "a", "i", "A" and "I", will move |
Bram Moolenaar | d2f3a8b | 2018-06-19 14:35:59 +0200 | [diff] [blame] | 1281 | the cursor to the last line. "A" will move to the end of the line, "I" to the |
| 1282 | start of the line. |
Bram Moolenaar | f273245 | 2018-06-03 14:47:35 +0200 | [diff] [blame] | 1283 | |
Bram Moolenaar | acc2240 | 2020-06-07 21:07:18 +0200 | [diff] [blame] | 1284 | Here is an example for Unix. It starts a shell in the background and prompts |
| 1285 | for the next shell command. Output from the shell is displayed above the |
| 1286 | prompt. > |
| 1287 | |
| 1288 | " Create a channel log so we can see what happens. |
| 1289 | call ch_logfile('logfile', 'w') |
| 1290 | |
| 1291 | " Function handling a line of text has been typed. |
| 1292 | func TextEntered(text) |
| 1293 | " Send the text to a shell with Enter appended. |
| 1294 | call ch_sendraw(g:shell_job, a:text .. "\n") |
| 1295 | endfunc |
Bram Moolenaar | cb80aa2 | 2020-10-26 21:12:46 +0100 | [diff] [blame] | 1296 | |
Bram Moolenaar | acc2240 | 2020-06-07 21:07:18 +0200 | [diff] [blame] | 1297 | " Function handling output from the shell: Added above the prompt. |
| 1298 | func GotOutput(channel, msg) |
Bram Moolenaar | c51cf03 | 2022-02-26 12:25:45 +0000 | [diff] [blame] | 1299 | call append(line("$") - 1, "- " .. a:msg) |
Bram Moolenaar | acc2240 | 2020-06-07 21:07:18 +0200 | [diff] [blame] | 1300 | endfunc |
| 1301 | |
| 1302 | " Function handling the shell exist: close the window. |
| 1303 | func JobExit(job, status) |
| 1304 | quit! |
| 1305 | endfunc |
| 1306 | |
| 1307 | " Start a shell in the background. |
| 1308 | let shell_job = job_start(["/bin/sh"], #{ |
| 1309 | \ out_cb: function('GotOutput'), |
| 1310 | \ err_cb: function('GotOutput'), |
| 1311 | \ exit_cb: function('JobExit'), |
| 1312 | \ }) |
| 1313 | let shell_ch = job_getchannel(shell_job) |
| 1314 | |
| 1315 | new |
| 1316 | set buftype=prompt |
| 1317 | let buf = bufnr('') |
| 1318 | call prompt_setcallback(buf, function("TextEntered")) |
| 1319 | eval prompt_setprompt(buf, "shell command: ") |
| 1320 | |
| 1321 | " start accepting shell commands |
| 1322 | startinsert |
| 1323 | < |
| 1324 | |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 1325 | |
Bram Moolenaar | 91f84f6 | 2018-07-29 15:07:52 +0200 | [diff] [blame] | 1326 | vim:tw=78:ts=8:noet:ft=help:norl: |