Bram Moolenaar | b1c9198 | 2018-05-17 17:04:55 +0200 | [diff] [blame] | 1 | *channel.txt* For Vim version 8.1. Last change: 2018 Apr 18 |
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| |
| 21 | 8. Starting a job with a channel |job-start| |
| 22 | 9. Starting a job without a channel |job-start-nochannel| |
| 23 | 10. Job options |job-options| |
| 24 | 11. Controlling a job |job-control| |
Bram Moolenaar | f273245 | 2018-06-03 14:47:35 +0200 | [diff] [blame] | 25 | 12. Using a prompt buffer |prompt-buffer| |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 26 | |
| 27 | {Vi does not have any of these features} |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 28 | {only when compiled with the |+channel| feature for channel stuff} |
Bram Moolenaar | f37506f | 2016-08-31 22:22:10 +0200 | [diff] [blame] | 29 | You can check this with: `has('channel')` |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 30 | {only when compiled with the |+job| feature for job stuff} |
Bram Moolenaar | f37506f | 2016-08-31 22:22:10 +0200 | [diff] [blame] | 31 | You can check this with: `has('job')` |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 32 | |
| 33 | ============================================================================== |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 34 | 1. Overview *job-channel-overview* |
| 35 | |
| 36 | There are four main types of jobs: |
Bram Moolenaar | 50ba526 | 2016-09-22 22:33:02 +0200 | [diff] [blame] | 37 | 1. A daemon, serving several Vim instances. |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 38 | Vim connects to it with a socket. |
| 39 | 2. One job working with one Vim instance, asynchronously. |
| 40 | Uses a socket or pipes. |
| 41 | 3. A job performing some work for a short time, asynchronously. |
| 42 | Uses a socket or pipes. |
| 43 | 4. Running a filter, synchronously. |
| 44 | Uses pipes. |
| 45 | |
Bram Moolenaar | 77cdfd1 | 2016-03-12 12:57:59 +0100 | [diff] [blame] | 46 | For 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 Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 48 | For 4 use the ":{range}!cmd" command, see |filter|. |
| 49 | |
| 50 | Over the socket and pipes these protocols are available: |
| 51 | RAW nothing known, Vim cannot tell where a message ends |
| 52 | NL every message ends in a NL (newline) character |
| 53 | JSON JSON encoding |json_encode()| |
| 54 | JS JavaScript style JSON-like encoding |js_encode()| |
| 55 | |
| 56 | Common 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 Moolenaar | 7dda86f | 2018-04-20 22:36:41 +0200 | [diff] [blame] | 59 | - 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] | 60 | cross-references in a database. |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 61 | |
| 62 | ============================================================================== |
Bram Moolenaar | 2685212 | 2016-05-24 20:02:38 +0200 | [diff] [blame] | 63 | 2. Channel demo *channel-demo* *demoserver.py* |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 64 | |
| 65 | This requires Python. The demo program can be found in |
| 66 | $VIMRUNTIME/tools/demoserver.py |
| 67 | Run it in one terminal. We will call this T1. |
| 68 | |
| 69 | Run Vim in another terminal. Connect to the demo server with: > |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 70 | let channel = ch_open('localhost:8765') |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 71 | |
| 72 | In T1 you should see: |
| 73 | === socket opened === ~ |
| 74 | |
| 75 | You can now send a message to the server: > |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 76 | echo ch_evalexpr(channel, 'hello!') |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 77 | |
| 78 | The message is received in T1 and a response is sent back to Vim. |
| 79 | You can see the raw messages in T1. What Vim sends is: |
| 80 | [1,"hello!"] ~ |
| 81 | And the response is: |
| 82 | [1,"got it"] ~ |
| 83 | The number will increase every time you send a message. |
| 84 | |
| 85 | 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] | 86 | the quotes): |
| 87 | ["ex","echo 'hi there'"] ~ |
| 88 | And you should see the message in Vim. You can move the cursor a word forward: |
| 89 | ["normal","w"] ~ |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 90 | |
| 91 | To handle asynchronous communication a callback needs to be used: > |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 92 | func MyHandler(channel, msg) |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 93 | echo "from the handler: " . a:msg |
| 94 | endfunc |
Bram Moolenaar | 02e83b4 | 2016-02-21 20:10:26 +0100 | [diff] [blame] | 95 | call ch_sendexpr(channel, 'hello!', {'callback': "MyHandler"}) |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 96 | Vim will not wait for a response. Now the server can send the response later |
| 97 | and MyHandler will be invoked. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 98 | |
| 99 | Instead of giving a callback with every send call, it can also be specified |
| 100 | when opening the channel: > |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 101 | call ch_close(channel) |
| 102 | let channel = ch_open('localhost:8765', {'callback': "MyHandler"}) |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 103 | call ch_sendexpr(channel, 'hello!') |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 104 | |
Bram Moolenaar | 328da0d | 2016-03-04 22:22:32 +0100 | [diff] [blame] | 105 | When trying out channels it's useful to see what is going on. You can tell |
| 106 | Vim to write lines in log file: > |
| 107 | call ch_logfile('channellog', 'w') |
| 108 | See |ch_logfile()|. |
| 109 | |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 110 | ============================================================================== |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 111 | 3. Opening a channel *channel-open* |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 112 | |
Bram Moolenaar | 681baaf | 2016-02-04 20:57:07 +0100 | [diff] [blame] | 113 | To open a channel: > |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 114 | let channel = ch_open({address} [, {options}]) |
Bram Moolenaar | 02e83b4 | 2016-02-21 20:10:26 +0100 | [diff] [blame] | 115 | if ch_status(channel) == "open" |
| 116 | " use the channel |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 117 | |
| 118 | Use |ch_status()| to see if the channel could be opened. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 119 | |
| 120 | {address} has the form "hostname:port". E.g., "localhost:8765". |
| 121 | |
Bram Moolenaar | 4f3f668 | 2016-03-26 23:01:59 +0100 | [diff] [blame] | 122 | {options} is a dictionary with optional entries: *channel-open-options* |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 123 | |
| 124 | "mode" can be: *channel-mode* |
| 125 | "json" - Use JSON, see below; most convenient way. Default. |
Bram Moolenaar | 910b8aa | 2016-02-16 21:03:07 +0100 | [diff] [blame] | 126 | "js" - Use JS (JavaScript) encoding, more efficient than JSON. |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 127 | "nl" - Use messages that end in a NL character |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 128 | "raw" - Use raw messages |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 129 | *channel-callback* *E921* |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 130 | "callback" A function that is called when a message is received that is |
| 131 | not handled otherwise. It gets two arguments: the channel |
Bram Moolenaar | 02e83b4 | 2016-02-21 20:10:26 +0100 | [diff] [blame] | 132 | and the received message. Example: > |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 133 | func Handle(channel, msg) |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 134 | echo 'Received: ' . a:msg |
| 135 | endfunc |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 136 | let channel = ch_open("localhost:8765", {"callback": "Handle"}) |
| 137 | < |
Bram Moolenaar | 02e83b4 | 2016-02-21 20:10:26 +0100 | [diff] [blame] | 138 | 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 Moolenaar | e18c0b3 | 2016-03-20 21:08:34 +0100 | [diff] [blame] | 144 | |
| 145 | For all callbacks: Use |function()| to bind it to arguments |
Bram Moolenaar | 4f3f668 | 2016-03-26 23:01:59 +0100 | [diff] [blame] | 146 | and/or a Dictionary. Or use the form "dict.function" to bind |
| 147 | the Dictionary. |
Bram Moolenaar | 06d2d38 | 2016-05-20 17:24:11 +0200 | [diff] [blame] | 148 | |
| 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 Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 153 | *close_cb* |
| 154 | "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] | 155 | than by calling ch_close(). It should be defined like this: > |
| 156 | func MyCloseHandler(channel) |
Bram Moolenaar | 06d2d38 | 2016-05-20 17:24:11 +0200 | [diff] [blame] | 157 | < Vim will invoke callbacks that handle data before invoking |
| 158 | close_cb, thus when this function is called no more data will |
Bram Moolenaar | 958dc69 | 2016-12-01 15:34:12 +0100 | [diff] [blame] | 159 | 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 Moolenaar | 06d2d38 | 2016-05-20 17:24:11 +0200 | [diff] [blame] | 166 | *waittime* |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 167 | "waittime" The time to wait for the connection to be made in |
Bram Moolenaar | f391327 | 2016-02-25 00:00:01 +0100 | [diff] [blame] | 168 | milliseconds. A negative number waits forever. |
| 169 | |
| 170 | The default is zero, don't wait, which is useful if a local |
| 171 | server is supposed to be running already. On Unix Vim |
| 172 | actually uses a 1 msec timeout, that is required on many |
| 173 | systems. Use a larger value for a remote server, e.g. 10 |
| 174 | msec at least. |
Bram Moolenaar | 4f3f668 | 2016-03-26 23:01:59 +0100 | [diff] [blame] | 175 | *channel-timeout* |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 176 | "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] | 177 | ch_evalexpr(). In milliseconds. The default is 2000 (2 |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 178 | seconds). |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 179 | |
Bram Moolenaar | 595e64e | 2016-02-07 19:19:53 +0100 | [diff] [blame] | 180 | When "mode" is "json" or "js" the "callback" is optional. When omitted it is |
| 181 | only possible to receive a message after sending one. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 182 | |
Bram Moolenaar | 02e83b4 | 2016-02-21 20:10:26 +0100 | [diff] [blame] | 183 | To change the channel options after opening it use |ch_setoptions()|. The |
| 184 | arguments are similar to what is passed to |ch_open()|, but "waittime" cannot |
| 185 | be given, since that only applies to opening the channel. |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 186 | |
Bram Moolenaar | 02e83b4 | 2016-02-21 20:10:26 +0100 | [diff] [blame] | 187 | For example, the handler can be added or changed: > |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 188 | call ch_setoptions(channel, {'callback': callback}) |
| 189 | When "callback" is empty (zero or an empty string) the handler is removed. |
| 190 | |
Bram Moolenaar | 328da0d | 2016-03-04 22:22:32 +0100 | [diff] [blame] | 191 | After a callback has been invoked Vim will update the screen and put the |
| 192 | cursor back where it belongs. Thus the callback should not need to do |
| 193 | `:redraw`. |
| 194 | |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 195 | The timeout can be changed: > |
| 196 | call ch_setoptions(channel, {'timeout': msec}) |
| 197 | < |
Bram Moolenaar | 02e83b4 | 2016-02-21 20:10:26 +0100 | [diff] [blame] | 198 | *channel-close* *E906* |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 199 | Once done with the channel, disconnect it like this: > |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 200 | call ch_close(channel) |
| 201 | When a socket is used this will close the socket for both directions. When |
| 202 | pipes are used (stdin/stdout/stderr) they are all closed. This might not be |
| 203 | what you want! Stopping the job with job_stop() might be better. |
Bram Moolenaar | 187db50 | 2016-02-27 14:44:26 +0100 | [diff] [blame] | 204 | All readahead is discarded, callbacks will no longer be invoked. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 205 | |
Bram Moolenaar | 4f3f668 | 2016-03-26 23:01:59 +0100 | [diff] [blame] | 206 | Note that a channel is closed in three stages: |
| 207 | - The I/O ends, log message: "Closing channel". There can still be queued |
| 208 | messages to read or callbacks to invoke. |
| 209 | - The readahead is cleared, log message: "Clearing channel". Some variables |
| 210 | may still reference the channel. |
| 211 | - The channel is freed, log message: "Freeing channel". |
| 212 | |
Bram Moolenaar | cbebd48 | 2016-02-07 23:02:56 +0100 | [diff] [blame] | 213 | When the channel can't be opened you will get an error message. There is a |
| 214 | difference between MS-Windows and Unix: On Unix when the port doesn't exist |
| 215 | ch_open() fails quickly. On MS-Windows "waittime" applies. |
Bram Moolenaar | aa3b15d | 2016-04-21 08:53:19 +0200 | [diff] [blame] | 216 | *E898* *E901* *E902* |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 217 | |
| 218 | If there is an error reading or writing a channel it will be closed. |
Bram Moolenaar | aa3b15d | 2016-04-21 08:53:19 +0200 | [diff] [blame] | 219 | *E630* *E631* |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 220 | |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 221 | ============================================================================== |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 222 | 4. Using a JSON or JS channel *channel-use* |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 223 | |
Bram Moolenaar | 910b8aa | 2016-02-16 21:03:07 +0100 | [diff] [blame] | 224 | 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] | 225 | let response = ch_evalexpr(channel, {expr}) |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 226 | This awaits a response from the other side. |
| 227 | |
Bram Moolenaar | 910b8aa | 2016-02-16 21:03:07 +0100 | [diff] [blame] | 228 | 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] | 229 | JavaScript encoding. See |js_encode()| for the difference. |
Bram Moolenaar | 595e64e | 2016-02-07 19:19:53 +0100 | [diff] [blame] | 230 | |
Bram Moolenaar | 02e83b4 | 2016-02-21 20:10:26 +0100 | [diff] [blame] | 231 | To send a message, without handling a response or letting the channel callback |
| 232 | handle the response: > |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 233 | call ch_sendexpr(channel, {expr}) |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 234 | |
| 235 | To send a message and letting the response handled by a specific function, |
| 236 | asynchronously: > |
Bram Moolenaar | 02e83b4 | 2016-02-21 20:10:26 +0100 | [diff] [blame] | 237 | call ch_sendexpr(channel, {expr}, {'callback': Handler}) |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 238 | |
| 239 | Vim will match the response with the request using the message ID. Once the |
| 240 | response is received the callback will be invoked. Further responses with the |
| 241 | same ID will be ignored. If your server sends back multiple responses you |
| 242 | 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] | 243 | |
| 244 | The {expr} is converted to JSON and wrapped in an array. An example of the |
| 245 | message that the receiver will get when {expr} is the string "hello": |
| 246 | [12,"hello"] ~ |
| 247 | |
| 248 | The format of the JSON sent is: |
| 249 | [{number},{expr}] |
| 250 | |
| 251 | In which {number} is different every time. It must be used in the response |
| 252 | (if any): |
| 253 | |
| 254 | [{number},{response}] |
| 255 | |
| 256 | This way Vim knows which sent message matches with which received message and |
| 257 | can call the right handler. Also when the messages arrive out of order. |
| 258 | |
Bram Moolenaar | f1f0792 | 2016-08-26 17:58:53 +0200 | [diff] [blame] | 259 | A newline character is terminating the JSON text. This can be used to |
| 260 | separate the read text. For example, in Python: |
| 261 | splitidx = read_text.find('\n') |
| 262 | message = read_text[:splitidx] |
| 263 | rest = read_text[splitidx + 1:] |
| 264 | |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 265 | The sender must always send valid JSON to Vim. Vim can check for the end of |
| 266 | 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] | 267 | was received. A newline after the message is optional. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 268 | |
| 269 | When the process wants to send a message to Vim without first receiving a |
| 270 | message, it must use the number zero: |
| 271 | [0,{response}] |
| 272 | |
| 273 | Then channel handler will then get {response} converted to Vim types. If the |
| 274 | channel does not have a handler the message is dropped. |
| 275 | |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 276 | It is also possible to use ch_sendraw() and ch_evalraw() on a JSON or JS |
| 277 | channel. The caller is then completely responsible for correct encoding and |
| 278 | decoding. |
Bram Moolenaar | cbebd48 | 2016-02-07 23:02:56 +0100 | [diff] [blame] | 279 | |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 280 | ============================================================================== |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 281 | 5. Channel commands *channel-commands* |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 282 | |
Bram Moolenaar | 910b8aa | 2016-02-16 21:03:07 +0100 | [diff] [blame] | 283 | 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] | 284 | handled by Vim internally, it does not require a handler for the channel. |
| 285 | |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 286 | Possible commands are: *E903* *E904* *E905* |
Bram Moolenaar | 220adb1 | 2016-09-12 12:17:26 +0200 | [diff] [blame] | 287 | ["redraw", {forced}] |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 288 | ["ex", {Ex command}] |
| 289 | ["normal", {Normal mode command}] |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 290 | ["expr", {expression}, {number}] |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 291 | ["expr", {expression}] |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 292 | ["call", {func name}, {argument list}, {number}] |
| 293 | ["call", {func name}, {argument list}] |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 294 | |
| 295 | With all of these: Be careful what these commands do! You can easily |
| 296 | interfere with what the user is doing. To avoid trouble use |mode()| to check |
| 297 | 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] | 298 | inserted as text, not executed as a command: |
| 299 | ["ex","if mode() == 'i' | call feedkeys('ClassName') | endif"] ~ |
| 300 | |
| 301 | Errors in these commands are normally not reported to avoid them messing up |
| 302 | the display. If you do want to see them, set the 'verbose' option to 3 or |
| 303 | higher. |
| 304 | |
| 305 | |
| 306 | Command "redraw" ~ |
| 307 | |
| 308 | The other commands do not update the screen, so that you can send a sequence |
| 309 | of commands without the cursor moving around. You must end with the "redraw" |
| 310 | command to show any changed text and show the cursor where it belongs. |
| 311 | |
| 312 | The argument is normally an empty string: |
| 313 | ["redraw", ""] ~ |
| 314 | To first clear the screen pass "force": |
| 315 | ["redraw", "force"] ~ |
| 316 | |
| 317 | |
| 318 | Command "ex" ~ |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 319 | |
| 320 | 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] | 321 | completion or error. You could use functions in an |autoload| script: |
| 322 | ["ex","call myscript#MyFunc(arg)"] |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 323 | |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 324 | You can also use "call |feedkeys()|" to insert any key sequence. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 325 | |
Bram Moolenaar | 4f3f668 | 2016-03-26 23:01:59 +0100 | [diff] [blame] | 326 | When there is an error a message is written to the channel log, if it exists, |
| 327 | and v:errmsg is set to the error. |
| 328 | |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 329 | |
| 330 | Command "normal" ~ |
| 331 | |
Bram Moolenaar | 681baaf | 2016-02-04 20:57:07 +0100 | [diff] [blame] | 332 | The "normal" command is executed like with ":normal!", commands are not |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 333 | mapped. Example to open the folds under the cursor: |
| 334 | ["normal" "zO"] |
| 335 | |
| 336 | |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 337 | Command "expr" with response ~ |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 338 | |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 339 | 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] | 340 | example, to get the number of lines in the current buffer: |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 341 | ["expr","line('$')", -2] ~ |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 342 | |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 343 | It will send back the result of the expression: |
Bram Moolenaar | e0fa374 | 2016-02-20 15:47:01 +0100 | [diff] [blame] | 344 | [-2, "last line"] ~ |
| 345 | The format is: |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 346 | [{number}, {result}] |
Bram Moolenaar | 187db50 | 2016-02-27 14:44:26 +0100 | [diff] [blame] | 347 | |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 348 | 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] | 349 | to avoid confusion with message that Vim sends. Use a different number on |
| 350 | every request to be able to match the request with the response. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 351 | |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 352 | {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] | 353 | evaluation fails or the result can't be encoded in JSON it is the string |
| 354 | "ERROR". |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 355 | |
| 356 | |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 357 | Command "expr" without a response ~ |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 358 | |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 359 | 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] | 360 | Example: |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 361 | ["expr","setline('$', ['one', 'two', 'three'])"] ~ |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 362 | There is no third argument in the request. |
| 363 | |
| 364 | |
| 365 | Command "call" ~ |
| 366 | |
| 367 | This is similar to "expr", but instead of passing the whole expression as a |
| 368 | string this passes the name of a function and a list of arguments. This |
| 369 | avoids the conversion of the arguments to a string and escaping and |
| 370 | concatenating them. Example: |
| 371 | ["call", "line", ["$"], -2] ~ |
| 372 | |
| 373 | Leave out the fourth argument if no response is to be sent: |
| 374 | ["call", "setline", ["$", ["one", "two", "three"]]] ~ |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 375 | |
| 376 | ============================================================================== |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 377 | 6. Using a RAW or NL channel *channel-raw* |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 378 | |
Bram Moolenaar | c0514bf | 2016-11-17 14:50:09 +0100 | [diff] [blame] | 379 | 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] | 380 | let response = ch_evalraw(channel, {string}) |
Bram Moolenaar | 910b8aa | 2016-02-16 21:03:07 +0100 | [diff] [blame] | 381 | |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 382 | The {string} is sent as-is. The response will be what can be read from the |
| 383 | 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] | 384 | message you need to take care of it yourself. The timeout applies for reading |
| 385 | the first byte, after that it will not wait for anything more. |
| 386 | |
Bram Moolenaar | 910b8aa | 2016-02-16 21:03:07 +0100 | [diff] [blame] | 387 | 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] | 388 | to put in the NL after each message. Thus you can also send several messages |
| 389 | ending in a NL at once. The response will be the text up to and including the |
| 390 | first NL. This can also be just the NL for an empty response. |
| 391 | 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] | 392 | |
| 393 | To send a message, without expecting a response: > |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 394 | call ch_sendraw(channel, {string}) |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 395 | The process can send back a response, the channel handler will be called with |
| 396 | it. |
| 397 | |
| 398 | To send a message and letting the response handled by a specific function, |
| 399 | asynchronously: > |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 400 | call ch_sendraw(channel, {string}, {'callback': 'MyHandler'}) |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 401 | |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 402 | This {string} can also be JSON, use |json_encode()| to create it and |
| 403 | |json_decode()| to handle a received JSON message. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 404 | |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 405 | 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] | 406 | |
Bram Moolenaar | 818078d | 2016-08-27 21:58:42 +0200 | [diff] [blame] | 407 | A String in Vim cannot contain NUL bytes. To send or receive NUL bytes read |
| 408 | or write from a buffer. See |in_io-buffer| and |out_io-buffer|. |
| 409 | |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 410 | ============================================================================== |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 411 | 7. More channel functions *channel-more* |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 412 | |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 413 | To obtain the status of a channel: ch_status(channel). The possible results |
| 414 | are: |
| 415 | "fail" Failed to open the channel. |
| 416 | "open" The channel can be used. |
Bram Moolenaar | 0648142 | 2016-04-30 15:13:38 +0200 | [diff] [blame] | 417 | "buffered" The channel was closed but there is data to read. |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 418 | "closed" The channel was closed. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 419 | |
Bram Moolenaar | 187db50 | 2016-02-27 14:44:26 +0100 | [diff] [blame] | 420 | To obtain the job associated with a channel: ch_getjob(channel) |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 421 | |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 422 | To read one message from a channel: > |
| 423 | let output = ch_read(channel) |
| 424 | This uses the channel timeout. To read without a timeout, just get any |
| 425 | message that is available: > |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 426 | let output = ch_read(channel, {'timeout': 0}) |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 427 | 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] | 428 | channels, an empty string for a RAW or NL channel. You can use |ch_canread()| |
| 429 | to check if there is something to read. |
| 430 | |
Bram Moolenaar | 05aafed | 2017-08-11 19:12:11 +0200 | [diff] [blame] | 431 | Note that when there is no callback, messages are dropped. To avoid that add |
| 432 | a close callback to the channel. |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 433 | |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 434 | To read all output from a RAW channel that is available: > |
| 435 | let output = ch_readraw(channel) |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 436 | To read the error output: > |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 437 | let output = ch_readraw(channel, {"part": "err"}) |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 438 | |
Bram Moolenaar | 4f3f668 | 2016-03-26 23:01:59 +0100 | [diff] [blame] | 439 | ch_read() and ch_readraw() use the channel timeout. When there is nothing to |
| 440 | read within that time an empty string is returned. To specify a different |
| 441 | timeout in msec use the "timeout" option: |
| 442 | {"timeout": 123} ~ |
| 443 | To read from the error output use the "part" option: |
| 444 | {"part": "err"} ~ |
| 445 | To read a message with a specific ID, on a JS or JSON channel: |
| 446 | {"id": 99} ~ |
| 447 | When no ID is specified or the ID is -1, the first message is returned. This |
| 448 | overrules any callback waiting for this message. |
| 449 | |
| 450 | For a RAW channel this returns whatever is available, since Vim does not know |
| 451 | where a message ends. |
| 452 | For a NL channel this returns one message. |
| 453 | For a JS or JSON channel this returns one decoded message. |
| 454 | This includes any sequence number. |
| 455 | |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 456 | ============================================================================== |
| 457 | 8. Starting a job with a channel *job-start* *job* |
| 458 | |
| 459 | To start a job and open a channel for stdin/stdout/stderr: > |
| 460 | let job = job_start(command, {options}) |
| 461 | |
| 462 | You can get the channel with: > |
| 463 | let channel = job_getchannel(job) |
| 464 | |
| 465 | The channel will use NL mode. If you want another mode it's best to specify |
| 466 | this in {options}. When changing the mode later some text may have already |
| 467 | been received and not parsed correctly. |
| 468 | |
| 469 | If the command produces a line of output that you want to deal with, specify |
| 470 | a handler for stdout: > |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 471 | let job = job_start(command, {"out_cb": "MyHandler"}) |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 472 | The function will be called with the channel and a message. You would define |
| 473 | it like this: > |
| 474 | func MyHandler(channel, msg) |
| 475 | |
Bram Moolenaar | 02e83b4 | 2016-02-21 20:10:26 +0100 | [diff] [blame] | 476 | 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] | 477 | |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] | 478 | |
Bram Moolenaar | 1ccd8ff | 2017-08-11 19:50:37 +0200 | [diff] [blame] | 479 | Note that if the job exits before you read the output, the output may be lost. |
| 480 | This depends on the system (on Unix this happens because closing the write end |
| 481 | of a pipe causes the read end to get EOF). To avoid this make the job sleep |
| 482 | for a short while before it exits. |
| 483 | |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 484 | The handler defined for "out_cb" will not receive stderr. If you want to |
| 485 | handle that separately, add an "err_cb" handler: > |
| 486 | let job = job_start(command, {"out_cb": "MyHandler", |
| 487 | \ "err_cb": "ErrHandler"}) |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 488 | |
Bram Moolenaar | 02e83b4 | 2016-02-21 20:10:26 +0100 | [diff] [blame] | 489 | If you want to handle both stderr and stdout with one handler use the |
| 490 | "callback" option: > |
| 491 | let job = job_start(command, {"callback": "MyHandler"}) |
| 492 | |
Bram Moolenaar | 3ec574f | 2017-06-13 18:12:01 +0200 | [diff] [blame] | 493 | Depending on the system, starting a job can put Vim in the background, the |
| 494 | started job gets the focus. To avoid that, use the `foreground()` function. |
| 495 | This might not always work when called early, put in the callback handler or |
| 496 | use a timer to call it after the job has started. |
| 497 | |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 498 | You can send a message to the command with ch_evalraw(). If the channel is in |
| 499 | JSON or JS mode you can use ch_evalexpr(). |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 500 | |
| 501 | There are several options you can use, see |job-options|. |
Bram Moolenaar | 187db50 | 2016-02-27 14:44:26 +0100 | [diff] [blame] | 502 | For example, to start a job and write its output in buffer "dummy": > |
| 503 | let logjob = job_start("tail -f /tmp/log", |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 504 | \ {'out_io': 'buffer', 'out_name': 'dummy'}) |
Bram Moolenaar | 187db50 | 2016-02-27 14:44:26 +0100 | [diff] [blame] | 505 | sbuf dummy |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 506 | |
Bram Moolenaar | 5f148ec | 2016-03-07 22:59:26 +0100 | [diff] [blame] | 507 | |
| 508 | Job input from a buffer ~ |
Bram Moolenaar | 818078d | 2016-08-27 21:58:42 +0200 | [diff] [blame] | 509 | *in_io-buffer* |
Bram Moolenaar | 328da0d | 2016-03-04 22:22:32 +0100 | [diff] [blame] | 510 | To run a job that reads from a buffer: > |
| 511 | let job = job_start({command}, |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 512 | \ {'in_io': 'buffer', 'in_name': 'mybuffer'}) |
Bram Moolenaar | 328da0d | 2016-03-04 22:22:32 +0100 | [diff] [blame] | 513 | < |
| 514 | *E915* *E918* |
| 515 | The buffer is found by name, similar to |bufnr()|. The buffer must exist and |
| 516 | be loaded when job_start() is called. |
| 517 | |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 518 | By default this reads the whole buffer. This can be changed with the "in_top" |
| 519 | and "in_bot" options. |
Bram Moolenaar | 328da0d | 2016-03-04 22:22:32 +0100 | [diff] [blame] | 520 | |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 521 | 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] | 522 | 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] | 523 | job stdin. This allows for editing the last line and sending it when pressing |
| 524 | Enter. |
Bram Moolenaar | 0874a83 | 2016-09-01 15:11:51 +0200 | [diff] [blame] | 525 | *channel-close-in* |
| 526 | When not using the special mode the pipe or socket will be closed after the |
| 527 | last line has been written. This signals the reading end that the input |
| 528 | finished. You can also use |ch_close_in()| to close it sooner. |
Bram Moolenaar | 328da0d | 2016-03-04 22:22:32 +0100 | [diff] [blame] | 529 | |
Bram Moolenaar | 063b9d1 | 2016-07-09 20:21:48 +0200 | [diff] [blame] | 530 | NUL bytes in the text will be passed to the job (internally Vim stores these |
| 531 | as NL bytes). |
| 532 | |
Bram Moolenaar | 0648142 | 2016-04-30 15:13:38 +0200 | [diff] [blame] | 533 | |
| 534 | Reading job output in the close callback ~ |
| 535 | *read-in-close-cb* |
| 536 | If the job can take some time and you don't need intermediate results, you can |
| 537 | add a close callback and read the output there: > |
| 538 | |
| 539 | func! CloseHandler(channel) |
Bram Moolenaar | 2ec618c | 2016-10-01 14:47:05 +0200 | [diff] [blame] | 540 | while ch_status(a:channel, {'part': 'out'}) == 'buffered' |
Bram Moolenaar | 0648142 | 2016-04-30 15:13:38 +0200 | [diff] [blame] | 541 | echomsg ch_read(a:channel) |
| 542 | endwhile |
| 543 | endfunc |
| 544 | let job = job_start(command, {'close_cb': 'CloseHandler'}) |
| 545 | |
| 546 | You will want to do something more useful than "echomsg". |
| 547 | |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 548 | ============================================================================== |
| 549 | 9. Starting a job without a channel *job-start-nochannel* |
| 550 | |
| 551 | To start another process without creating a channel: > |
Bram Moolenaar | 328da0d | 2016-03-04 22:22:32 +0100 | [diff] [blame] | 552 | let job = job_start(command, |
Bram Moolenaar | 5162822 | 2016-12-01 23:03:28 +0100 | [diff] [blame] | 553 | \ {"in_io": "null", "out_io": "null", "err_io": "null"}) |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 554 | |
| 555 | This starts {command} in the background, Vim does not wait for it to finish. |
| 556 | |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 557 | When Vim sees that neither stdin, stdout or stderr are connected, no channel |
| 558 | will be created. Often you will want to include redirection in the command to |
| 559 | avoid it getting stuck. |
| 560 | |
| 561 | There are several options you can use, see |job-options|. |
| 562 | |
Bram Moolenaar | 77cdfd1 | 2016-03-12 12:57:59 +0100 | [diff] [blame] | 563 | *job-start-if-needed* |
| 564 | To start a job only when connecting to an address does not work, do something |
| 565 | like this: > |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 566 | let channel = ch_open(address, {"waittime": 0}) |
| 567 | if ch_status(channel) == "fail" |
| 568 | let job = job_start(command) |
| 569 | let channel = ch_open(address, {"waittime": 1000}) |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 570 | endif |
Bram Moolenaar | 77cdfd1 | 2016-03-12 12:57:59 +0100 | [diff] [blame] | 571 | |
| 572 | Note that the waittime for ch_open() gives the job one second to make the port |
| 573 | available. |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 574 | |
| 575 | ============================================================================== |
| 576 | 10. Job options *job-options* |
| 577 | |
| 578 | The {options} argument in job_start() is a dictionary. All entries are |
Bram Moolenaar | 02e83b4 | 2016-02-21 20:10:26 +0100 | [diff] [blame] | 579 | optional. Some options can be used after the job has started, using |
| 580 | job_setoptions(job, {options}). Many options can be used with the channel |
| 581 | related to the job, using ch_setoptions(channel, {options}). |
| 582 | See |job_setoptions()| and |ch_setoptions()|. |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 583 | |
Bram Moolenaar | 4f3f668 | 2016-03-26 23:01:59 +0100 | [diff] [blame] | 584 | *in_mode* *out_mode* *err_mode* |
| 585 | "in_mode" mode specifically for stdin, only when using pipes |
| 586 | "out_mode" mode specifically for stdout, only when using pipes |
| 587 | "err_mode" mode specifically for stderr, only when using pipes |
| 588 | See |channel-mode| for the values. |
| 589 | |
| 590 | Note: when setting "mode" the part specific mode is |
| 591 | overwritten. Therefore set "mode" first and the part |
| 592 | specific mode later. |
| 593 | |
| 594 | Note: when writing to a file or buffer and when |
| 595 | reading from a buffer NL mode is used by default. |
| 596 | |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 597 | *job-callback* |
| 598 | "callback": handler Callback for something to read on any part of the |
| 599 | channel. |
Bram Moolenaar | 4f3f668 | 2016-03-26 23:01:59 +0100 | [diff] [blame] | 600 | *job-out_cb* *out_cb* |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 601 | "out_cb": handler Callback for when there is something to read on |
Bram Moolenaar | 4f3f668 | 2016-03-26 23:01:59 +0100 | [diff] [blame] | 602 | stdout. Only for when the channel uses pipes. When |
| 603 | "out_cb" wasn't set the channel callback is used. |
Bram Moolenaar | 269f595 | 2016-07-15 22:54:41 +0200 | [diff] [blame] | 604 | The two arguments are the channel and the message. |
Bram Moolenaar | 4f3f668 | 2016-03-26 23:01:59 +0100 | [diff] [blame] | 605 | |
| 606 | *job-err_cb* *err_cb* |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 607 | "err_cb": handler Callback for when there is something to read on |
Bram Moolenaar | 4f3f668 | 2016-03-26 23:01:59 +0100 | [diff] [blame] | 608 | stderr. Only for when the channel uses pipes. When |
| 609 | "err_cb" wasn't set the channel callback is used. |
Bram Moolenaar | 269f595 | 2016-07-15 22:54:41 +0200 | [diff] [blame] | 610 | The two arguments are the channel and the message. |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 611 | *job-close_cb* |
| 612 | "close_cb": handler Callback for when the channel is closed. Same as |
Bram Moolenaar | 82af871 | 2016-06-04 20:20:29 +0200 | [diff] [blame] | 613 | "close_cb" on |ch_open()|, see |close_cb|. |
Bram Moolenaar | bc2eada | 2017-01-02 21:27:47 +0100 | [diff] [blame] | 614 | *job-drop* |
Bram Moolenaar | b6e0ec6 | 2017-07-23 22:12:20 +0200 | [diff] [blame] | 615 | "drop": when Specifies when to drop messages. Same as "drop" on |
Bram Moolenaar | 5162822 | 2016-12-01 23:03:28 +0100 | [diff] [blame] | 616 | |ch_open()|, see |channel-drop|. For "auto" the |
| 617 | exit_cb is not considered. |
Bram Moolenaar | bc2eada | 2017-01-02 21:27:47 +0100 | [diff] [blame] | 618 | *job-exit_cb* |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 619 | "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] | 620 | job and the exit status. |
Bram Moolenaar | b4ada79 | 2016-10-30 21:55:26 +0100 | [diff] [blame] | 621 | Vim checks up to 10 times per second for jobs that |
| 622 | ended. The check can also be triggered by calling |
| 623 | |job_status()|, which may then invoke the exit_cb |
| 624 | handler. |
Bram Moolenaar | 06d2d38 | 2016-05-20 17:24:11 +0200 | [diff] [blame] | 625 | Note that data can be buffered, callbacks may still be |
| 626 | called after the process ends. |
Bram Moolenaar | 4f3f668 | 2016-03-26 23:01:59 +0100 | [diff] [blame] | 627 | *job-timeout* |
Bram Moolenaar | b6e0ec6 | 2017-07-23 22:12:20 +0200 | [diff] [blame] | 628 | "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] | 629 | when using ch_evalexpr(). In milliseconds. The |
| 630 | default is 2000 (2 seconds). |
| 631 | *out_timeout* *err_timeout* |
Bram Moolenaar | b6e0ec6 | 2017-07-23 22:12:20 +0200 | [diff] [blame] | 632 | "out_timeout": time Timeout for stdout. Only when using pipes. |
| 633 | "err_timeout": time Timeout for stderr. Only when using pipes. |
Bram Moolenaar | 4f3f668 | 2016-03-26 23:01:59 +0100 | [diff] [blame] | 634 | Note: when setting "timeout" the part specific mode is |
| 635 | overwritten. Therefore set "timeout" first and the |
| 636 | part specific mode later. |
| 637 | |
Bram Moolenaar | 02e83b4 | 2016-02-21 20:10:26 +0100 | [diff] [blame] | 638 | *job-stoponexit* |
| 639 | "stoponexit": {signal} Send {signal} to the job when Vim exits. See |
| 640 | |job_stop()| for possible values. |
| 641 | "stoponexit": "" Do not stop the job when Vim exits. |
| 642 | The default is "term". |
| 643 | |
Bram Moolenaar | 77cdfd1 | 2016-03-12 12:57:59 +0100 | [diff] [blame] | 644 | *job-term* |
Bram Moolenaar | b6e0ec6 | 2017-07-23 22:12:20 +0200 | [diff] [blame] | 645 | "term": "open" Start a terminal in a new window and connect the job |
| 646 | stdin/stdout/stderr to it. Similar to using |
| 647 | `:terminal`. |
Bram Moolenaar | 77cdfd1 | 2016-03-12 12:57:59 +0100 | [diff] [blame] | 648 | NOTE: Not implemented yet! |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 649 | |
Bram Moolenaar | 77cdfd1 | 2016-03-12 12:57:59 +0100 | [diff] [blame] | 650 | "channel": {channel} Use an existing channel instead of creating a new one. |
| 651 | The parts of the channel that get used for the new job |
| 652 | will be disconnected from what they were used before. |
Bram Moolenaar | 5162822 | 2016-12-01 23:03:28 +0100 | [diff] [blame] | 653 | If the channel was still used by another job this may |
Bram Moolenaar | 77cdfd1 | 2016-03-12 12:57:59 +0100 | [diff] [blame] | 654 | cause I/O errors. |
| 655 | Existing callbacks and other settings remain. |
| 656 | |
Bram Moolenaar | b6e0ec6 | 2017-07-23 22:12:20 +0200 | [diff] [blame] | 657 | "pty": 1 Use a pty (pseudo-tty) instead of a pipe when |
| 658 | possible. This is most useful in combination with a |
| 659 | terminal window, see |terminal|. |
| 660 | {only on Unix and Unix-like systems} |
| 661 | |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 662 | *job-in_io* *in_top* *in_bot* *in_name* *in_buf* |
| 663 | "in_io": "null" disconnect stdin (read from /dev/null) |
| 664 | "in_io": "pipe" stdin is connected to the channel (default) |
| 665 | "in_io": "file" stdin reads from a file |
| 666 | "in_io": "buffer" stdin reads from a buffer |
| 667 | "in_top": number when using "buffer": first line to send (default: 1) |
| 668 | "in_bot": number when using "buffer": last line to send (default: last) |
| 669 | "in_name": "/path/file" the name of the file or buffer to read from |
| 670 | "in_buf": number the number of the buffer to read from |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 671 | |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 672 | *job-out_io* *out_name* *out_buf* |
| 673 | "out_io": "null" disconnect stdout (goes to /dev/null) |
| 674 | "out_io": "pipe" stdout is connected to the channel (default) |
| 675 | "out_io": "file" stdout writes to a file |
Bram Moolenaar | 5162822 | 2016-12-01 23:03:28 +0100 | [diff] [blame] | 676 | "out_io": "buffer" stdout appends to a buffer (see below) |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 677 | "out_name": "/path/file" the name of the file or buffer to write to |
| 678 | "out_buf": number the number of the buffer to write to |
Bram Moolenaar | 9f5842e | 2016-05-29 16:17:08 +0200 | [diff] [blame] | 679 | "out_modifiable": 0 when writing to a buffer, 'modifiable' will be off |
| 680 | (see below) |
Bram Moolenaar | 169ebb0 | 2016-09-07 23:32:23 +0200 | [diff] [blame] | 681 | "out_msg": 0 when writing to a new buffer, the first line will be |
| 682 | set to "Reading from channel output..." |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 683 | |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 684 | *job-err_io* *err_name* *err_buf* |
| 685 | "err_io": "out" stderr messages to go to stdout |
| 686 | "err_io": "null" disconnect stderr (goes to /dev/null) |
| 687 | "err_io": "pipe" stderr is connected to the channel (default) |
| 688 | "err_io": "file" stderr writes to a file |
Bram Moolenaar | 5162822 | 2016-12-01 23:03:28 +0100 | [diff] [blame] | 689 | "err_io": "buffer" stderr appends to a buffer (see below) |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 690 | "err_name": "/path/file" the name of the file or buffer to write to |
| 691 | "err_buf": number the number of the buffer to write to |
Bram Moolenaar | 9f5842e | 2016-05-29 16:17:08 +0200 | [diff] [blame] | 692 | "err_modifiable": 0 when writing to a buffer, 'modifiable' will be off |
| 693 | (see below) |
Bram Moolenaar | 169ebb0 | 2016-09-07 23:32:23 +0200 | [diff] [blame] | 694 | "err_msg": 0 when writing to a new buffer, the first line will be |
| 695 | set to "Reading from channel error..." |
Bram Moolenaar | 77cdfd1 | 2016-03-12 12:57:59 +0100 | [diff] [blame] | 696 | |
Bram Moolenaar | 7db8f6f | 2016-03-29 23:12:46 +0200 | [diff] [blame] | 697 | "block_write": number only for testing: pretend every other write to stdin |
| 698 | will block |
| 699 | |
Bram Moolenaar | 05aafed | 2017-08-11 19:12:11 +0200 | [diff] [blame] | 700 | "env": dict environment variables for the new process |
| 701 | "cwd": "/path/to/dir" current working directory for the new process; |
| 702 | if the directory does not exist an error is given |
| 703 | |
Bram Moolenaar | 77cdfd1 | 2016-03-12 12:57:59 +0100 | [diff] [blame] | 704 | |
| 705 | Writing to a buffer ~ |
Bram Moolenaar | 818078d | 2016-08-27 21:58:42 +0200 | [diff] [blame] | 706 | *out_io-buffer* |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 707 | 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] | 708 | is appended to the buffer before invoking the callback. |
| 709 | |
| 710 | When a buffer is used both for input and output, the output lines are put |
| 711 | 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] | 712 | input. Otherwise lines are appended below the last line. |
Bram Moolenaar | c7f0ebc | 2016-02-27 21:10:09 +0100 | [diff] [blame] | 713 | |
Bram Moolenaar | 328da0d | 2016-03-04 22:22:32 +0100 | [diff] [blame] | 714 | When using JS or JSON mode with "buffer", only messages with zero or negative |
| 715 | ID will be added to the buffer, after decoding + encoding. Messages with a |
| 716 | positive number will be handled by a callback, commands are handled as usual. |
| 717 | |
Bram Moolenaar | 82af871 | 2016-06-04 20:20:29 +0200 | [diff] [blame] | 718 | The name of the buffer from "out_name" or "err_name" is compared the full name |
| 719 | of existing buffers, also after expanding the name for the current directory. |
| 720 | E.g., when a buffer was created with ":edit somename" and the buffer name is |
| 721 | "somename" it will use that buffer. |
| 722 | |
| 723 | If there is no matching buffer a new buffer is created. Use an empty name to |
| 724 | always create a new buffer. |ch_getbufnr()| can then be used to get the |
| 725 | buffer number. |
Bram Moolenaar | c7f0ebc | 2016-02-27 21:10:09 +0100 | [diff] [blame] | 726 | |
| 727 | For a new buffer 'buftype' is set to "nofile" and 'bufhidden' to "hide". If |
| 728 | 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] | 729 | *out_modifiable* *err_modifiable* |
Bram Moolenaar | 9f5842e | 2016-05-29 16:17:08 +0200 | [diff] [blame] | 730 | The "out_modifiable" and "err_modifiable" options can be used to set the |
| 731 | 'modifiable' option off, or write to a buffer that has 'modifiable' off. That |
| 732 | means that lines will be appended to the buffer, but the user can't easily |
| 733 | change the buffer. |
Bram Moolenaar | 169ebb0 | 2016-09-07 23:32:23 +0200 | [diff] [blame] | 734 | *out_msg* *err_msg* |
| 735 | The "out_msg" option can be used to specify whether a new buffer will have the |
| 736 | first line set to "Reading from channel output...". The default is to add the |
| 737 | message. "err_msg" does the same for channel error. |
| 738 | |
Bram Moolenaar | 9f5842e | 2016-05-29 16:17:08 +0200 | [diff] [blame] | 739 | When an existing buffer is to be written where 'modifiable' is off and the |
| 740 | "out_modifiable" or "err_modifiable" options is not zero, an error is given |
| 741 | and the buffer will not be written to. |
| 742 | |
Bram Moolenaar | 187db50 | 2016-02-27 14:44:26 +0100 | [diff] [blame] | 743 | When the buffer written to is displayed in a window and the cursor is in the |
| 744 | first column of the last line, the cursor will be moved to the newly added |
| 745 | line and the window is scrolled up to show the cursor if needed. |
| 746 | |
Bram Moolenaar | 063b9d1 | 2016-07-09 20:21:48 +0200 | [diff] [blame] | 747 | Undo is synced for every added line. NUL bytes are accepted (internally Vim |
| 748 | stores these as NL bytes). |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 749 | |
Bram Moolenaar | 77cdfd1 | 2016-03-12 12:57:59 +0100 | [diff] [blame] | 750 | |
| 751 | Writing to a file ~ |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 752 | *E920* |
Bram Moolenaar | 77cdfd1 | 2016-03-12 12:57:59 +0100 | [diff] [blame] | 753 | The file is created with permissions 600 (read-write for the user, not |
| 754 | accessible for others). Use |setfperm()| to change this. |
| 755 | |
| 756 | If the file already exists it is truncated. |
| 757 | |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 758 | ============================================================================== |
| 759 | 11. Controlling a job *job-control* |
| 760 | |
| 761 | To get the status of a job: > |
| 762 | echo job_status(job) |
| 763 | |
| 764 | To make a job stop running: > |
| 765 | job_stop(job) |
| 766 | |
| 767 | This is the normal way to end a job. On Unix it sends a SIGTERM to the job. |
| 768 | It is possible to use other ways to stop the job, or even send arbitrary |
| 769 | signals. E.g. to force a job to stop, "kill it": > |
| 770 | job_stop(job, "kill") |
| 771 | |
| 772 | For more options see |job_stop()|. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 773 | |
Bram Moolenaar | f273245 | 2018-06-03 14:47:35 +0200 | [diff] [blame] | 774 | ============================================================================== |
| 775 | 12. Using a prompt buffer *prompt-buffer* |
| 776 | |
| 777 | If you want to type input for the job in a Vim window you have a few options: |
| 778 | - Use a normal buffer and handle all possible commands yourself. |
| 779 | This will be complicated, since there are so many possible commands. |
| 780 | - Use a terminal window. This works well if what you type goes directly to |
| 781 | the job and the job output is directly displayed in the window. |
| 782 | See |terminal-window|. |
| 783 | - Use a prompt window. This works well when entering a line for the job in Vim |
| 784 | while displaying (possibly filtered) output from the job. |
| 785 | |
| 786 | A prompt buffer is created by setting 'buftype' to "prompt". You would |
| 787 | normally only do that in a newly created buffer. |
| 788 | |
| 789 | The user can edit and enter one line of text at the very last line of the |
| 790 | buffer. When pressing Enter in the prompt line the callback set with |
| 791 | |prompt_setcallback()| is invoked. It would normally send the line to a job. |
| 792 | Another callback would receive the output from the job and display it in the |
| 793 | buffer, below the prompt (and above the next prompt). |
| 794 | |
| 795 | Only the text in the last line, after the prompt, is editable. The rest of the |
| 796 | buffer is not modifiable with Normal mode commands. It can be modified by |
| 797 | calling functions, such as |append()|. Using other commands may mess up the |
| 798 | buffer. |
| 799 | |
| 800 | After setting 'buftype' to "prompt" Vim does not automatically start Insert |
| 801 | mode, use `:startinsert` if you want to enter Insert mode, so that the user |
| 802 | can start typing a line. |
| 803 | |
| 804 | The text of the prompt can be set with the |prompt_setprompt()| function. |
| 805 | |
| 806 | The user can go to Normal mode and navigate through the buffer. This can be |
| 807 | useful see older output or copy text. |
| 808 | |
| 809 | Any command that starts Insert mode, such as "a", "i", "A" and "I", will move |
| 810 | the cursor to the last line, after the prompt. |
| 811 | |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 812 | |
| 813 | vim:tw=78:ts=8:ft=help:norl: |