Bram Moolenaar | 7db8f6f | 2016-03-29 23:12:46 +0200 | [diff] [blame] | 1 | *channel.txt* For Vim version 7.4. Last change: 2016 Mar 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 | |
| 9 | DRAFT DRAFT DRAFT DRAFT DRAFT DRAFT DRAFT DRAFT DRAFT DRAFT |
| 10 | |
| 11 | Vim uses channels to communicate with other processes. |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 12 | A channel uses a socket or pipes *socket-interface* |
| 13 | Jobs can be used to start processes and communicate with them. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 14 | |
Bram Moolenaar | 681baaf | 2016-02-04 20:57:07 +0100 | [diff] [blame] | 15 | Vim current supports up to 10 simultaneous channels. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 16 | The Netbeans interface also uses a channel. |netbeans| |
| 17 | |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 18 | 1. Overview |job-channel-overview| |
| 19 | 2. Channel demo |channel-demo| |
| 20 | 3. Opening a channel |channel-open| |
| 21 | 4. Using a JSON or JS channel |channel-use| |
| 22 | 5. Channel commands |channel-commands| |
| 23 | 6. Using a RAW or NL channel |channel-raw| |
| 24 | 7. More channel functions |channel-more| |
| 25 | 8. Starting a job with a channel |job-start| |
| 26 | 9. Starting a job without a channel |job-start-nochannel| |
| 27 | 10. Job options |job-options| |
| 28 | 11. Controlling a job |job-control| |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 29 | |
| 30 | {Vi does not have any of these features} |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 31 | {only when compiled with the |+channel| feature for channel stuff} |
| 32 | {only when compiled with the |+job| feature for job stuff} |
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: |
| 38 | 1. A deamon, serving several Vim instances. |
| 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. |
| 60 | - Using a deamon, connecting over a socket in JSON mode. E.g. to lookup |
| 61 | crosss-refrences in a database. |
| 62 | |
| 63 | ============================================================================== |
| 64 | 2. Channel demo *channel-demo* |
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 | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 94 | echo "from the handler: " . a:msg |
| 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 | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 104 | call ch_sendexpr(channel, 'hello!') |
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 | 4f3f668 | 2016-03-26 23:01:59 +0100 | [diff] [blame] | 123 | {options} is a dictionary with optional entries: *channel-open-options* |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 124 | |
| 125 | "mode" can be: *channel-mode* |
| 126 | "json" - Use JSON, see below; most convenient way. Default. |
Bram Moolenaar | 910b8aa | 2016-02-16 21:03:07 +0100 | [diff] [blame] | 127 | "js" - Use JS (JavaScript) encoding, more efficient than JSON. |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 128 | "nl" - Use messages that end in a NL character |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 129 | "raw" - Use raw messages |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 130 | *channel-callback* *E921* |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 131 | "callback" A function that is called when a message is received that is |
| 132 | not handled otherwise. It gets two arguments: the channel |
Bram Moolenaar | 02e83b4 | 2016-02-21 20:10:26 +0100 | [diff] [blame] | 133 | and the received message. Example: > |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 134 | func Handle(channel, msg) |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 135 | echo 'Received: ' . a:msg |
| 136 | endfunc |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 137 | let channel = ch_open("localhost:8765", {"callback": "Handle"}) |
| 138 | < |
Bram Moolenaar | 02e83b4 | 2016-02-21 20:10:26 +0100 | [diff] [blame] | 139 | When "mode" is "json" or "js" the "msg" argument is the body |
| 140 | of the received message, converted to Vim types. |
| 141 | When "mode" is "nl" the "msg" argument is one message, |
| 142 | excluding the NL. |
| 143 | When "mode" is "raw" the "msg" argument is the whole message |
| 144 | as a string. |
Bram Moolenaar | e18c0b3 | 2016-03-20 21:08:34 +0100 | [diff] [blame] | 145 | |
| 146 | For all callbacks: Use |function()| to bind it to arguments |
Bram Moolenaar | 4f3f668 | 2016-03-26 23:01:59 +0100 | [diff] [blame] | 147 | and/or a Dictionary. Or use the form "dict.function" to bind |
| 148 | the Dictionary. |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 149 | *close_cb* |
| 150 | "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] | 151 | than by calling ch_close(). It should be defined like this: > |
| 152 | func MyCloseHandler(channel) |
Bram Moolenaar | 02e83b4 | 2016-02-21 20:10:26 +0100 | [diff] [blame] | 153 | < *waittime* |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 154 | "waittime" The time to wait for the connection to be made in |
Bram Moolenaar | f391327 | 2016-02-25 00:00:01 +0100 | [diff] [blame] | 155 | milliseconds. A negative number waits forever. |
| 156 | |
| 157 | The default is zero, don't wait, which is useful if a local |
| 158 | server is supposed to be running already. On Unix Vim |
| 159 | actually uses a 1 msec timeout, that is required on many |
| 160 | systems. Use a larger value for a remote server, e.g. 10 |
| 161 | msec at least. |
Bram Moolenaar | 4f3f668 | 2016-03-26 23:01:59 +0100 | [diff] [blame] | 162 | *channel-timeout* |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 163 | "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] | 164 | ch_evalexpr(). In milliseconds. The default is 2000 (2 |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 165 | seconds). |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 166 | |
Bram Moolenaar | 595e64e | 2016-02-07 19:19:53 +0100 | [diff] [blame] | 167 | When "mode" is "json" or "js" the "callback" is optional. When omitted it is |
| 168 | only possible to receive a message after sending one. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 169 | |
Bram Moolenaar | 02e83b4 | 2016-02-21 20:10:26 +0100 | [diff] [blame] | 170 | To change the channel options after opening it use |ch_setoptions()|. The |
| 171 | arguments are similar to what is passed to |ch_open()|, but "waittime" cannot |
| 172 | be given, since that only applies to opening the channel. |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 173 | |
Bram Moolenaar | 02e83b4 | 2016-02-21 20:10:26 +0100 | [diff] [blame] | 174 | For example, the handler can be added or changed: > |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 175 | call ch_setoptions(channel, {'callback': callback}) |
| 176 | When "callback" is empty (zero or an empty string) the handler is removed. |
| 177 | |
Bram Moolenaar | 328da0d | 2016-03-04 22:22:32 +0100 | [diff] [blame] | 178 | After a callback has been invoked Vim will update the screen and put the |
| 179 | cursor back where it belongs. Thus the callback should not need to do |
| 180 | `:redraw`. |
| 181 | |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 182 | The timeout can be changed: > |
| 183 | call ch_setoptions(channel, {'timeout': msec}) |
| 184 | < |
Bram Moolenaar | 02e83b4 | 2016-02-21 20:10:26 +0100 | [diff] [blame] | 185 | *channel-close* *E906* |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 186 | Once done with the channel, disconnect it like this: > |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 187 | call ch_close(channel) |
| 188 | When a socket is used this will close the socket for both directions. When |
| 189 | pipes are used (stdin/stdout/stderr) they are all closed. This might not be |
| 190 | what you want! Stopping the job with job_stop() might be better. |
Bram Moolenaar | 187db50 | 2016-02-27 14:44:26 +0100 | [diff] [blame] | 191 | All readahead is discarded, callbacks will no longer be invoked. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 192 | |
Bram Moolenaar | 4f3f668 | 2016-03-26 23:01:59 +0100 | [diff] [blame] | 193 | Note that a channel is closed in three stages: |
| 194 | - The I/O ends, log message: "Closing channel". There can still be queued |
| 195 | messages to read or callbacks to invoke. |
| 196 | - The readahead is cleared, log message: "Clearing channel". Some variables |
| 197 | may still reference the channel. |
| 198 | - The channel is freed, log message: "Freeing channel". |
| 199 | |
Bram Moolenaar | cbebd48 | 2016-02-07 23:02:56 +0100 | [diff] [blame] | 200 | When the channel can't be opened you will get an error message. There is a |
| 201 | difference between MS-Windows and Unix: On Unix when the port doesn't exist |
| 202 | ch_open() fails quickly. On MS-Windows "waittime" applies. |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 203 | *E898* *E899* *E900* *E901* *E902* |
| 204 | |
| 205 | If there is an error reading or writing a channel it will be closed. |
| 206 | *E896* *E630* *E631* |
| 207 | |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 208 | ============================================================================== |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 209 | 4. Using a JSON or JS channel *channel-use* |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 210 | |
Bram Moolenaar | 910b8aa | 2016-02-16 21:03:07 +0100 | [diff] [blame] | 211 | 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] | 212 | let response = ch_evalexpr(channel, {expr}) |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 213 | This awaits a response from the other side. |
| 214 | |
Bram Moolenaar | 910b8aa | 2016-02-16 21:03:07 +0100 | [diff] [blame] | 215 | 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] | 216 | JavaScript encoding. See |js_encode()| for the difference. |
Bram Moolenaar | 595e64e | 2016-02-07 19:19:53 +0100 | [diff] [blame] | 217 | |
Bram Moolenaar | 02e83b4 | 2016-02-21 20:10:26 +0100 | [diff] [blame] | 218 | To send a message, without handling a response or letting the channel callback |
| 219 | handle the response: > |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 220 | call ch_sendexpr(channel, {expr}) |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 221 | |
| 222 | To send a message and letting the response handled by a specific function, |
| 223 | asynchronously: > |
Bram Moolenaar | 02e83b4 | 2016-02-21 20:10:26 +0100 | [diff] [blame] | 224 | call ch_sendexpr(channel, {expr}, {'callback': Handler}) |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 225 | |
| 226 | Vim will match the response with the request using the message ID. Once the |
| 227 | response is received the callback will be invoked. Further responses with the |
| 228 | same ID will be ignored. If your server sends back multiple responses you |
| 229 | 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] | 230 | |
| 231 | The {expr} is converted to JSON and wrapped in an array. An example of the |
| 232 | message that the receiver will get when {expr} is the string "hello": |
| 233 | [12,"hello"] ~ |
| 234 | |
| 235 | The format of the JSON sent is: |
| 236 | [{number},{expr}] |
| 237 | |
| 238 | In which {number} is different every time. It must be used in the response |
| 239 | (if any): |
| 240 | |
| 241 | [{number},{response}] |
| 242 | |
| 243 | This way Vim knows which sent message matches with which received message and |
| 244 | can call the right handler. Also when the messages arrive out of order. |
| 245 | |
| 246 | The sender must always send valid JSON to Vim. Vim can check for the end of |
| 247 | the message by parsing the JSON. It will only accept the message if the end |
| 248 | was received. |
| 249 | |
| 250 | When the process wants to send a message to Vim without first receiving a |
| 251 | message, it must use the number zero: |
| 252 | [0,{response}] |
| 253 | |
| 254 | Then channel handler will then get {response} converted to Vim types. If the |
| 255 | channel does not have a handler the message is dropped. |
| 256 | |
Bram Moolenaar | 328da0d | 2016-03-04 22:22:32 +0100 | [diff] [blame] | 257 | On read error or ch_close(), when using a socket with RAW or NL mode, the |
| 258 | string "DETACH\n" is sent, if still possible. The channel will then be |
| 259 | inactive. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 260 | |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 261 | It is also possible to use ch_sendraw() and ch_evalraw() on a JSON or JS |
| 262 | channel. The caller is then completely responsible for correct encoding and |
| 263 | decoding. |
Bram Moolenaar | cbebd48 | 2016-02-07 23:02:56 +0100 | [diff] [blame] | 264 | |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 265 | ============================================================================== |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 266 | 5. Channel commands *channel-commands* |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 267 | |
Bram Moolenaar | 910b8aa | 2016-02-16 21:03:07 +0100 | [diff] [blame] | 268 | 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] | 269 | handled by Vim internally, it does not require a handler for the channel. |
| 270 | |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 271 | Possible commands are: *E903* *E904* *E905* |
| 272 | ["redraw" {forced}] |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 273 | ["ex", {Ex command}] |
| 274 | ["normal", {Normal mode command}] |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 275 | ["expr", {expression}, {number}] |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 276 | ["expr", {expression}] |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 277 | ["call", {func name}, {argument list}, {number}] |
| 278 | ["call", {func name}, {argument list}] |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 279 | |
| 280 | With all of these: Be careful what these commands do! You can easily |
| 281 | interfere with what the user is doing. To avoid trouble use |mode()| to check |
| 282 | 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] | 283 | inserted as text, not executed as a command: |
| 284 | ["ex","if mode() == 'i' | call feedkeys('ClassName') | endif"] ~ |
| 285 | |
| 286 | Errors in these commands are normally not reported to avoid them messing up |
| 287 | the display. If you do want to see them, set the 'verbose' option to 3 or |
| 288 | higher. |
| 289 | |
| 290 | |
| 291 | Command "redraw" ~ |
| 292 | |
| 293 | The other commands do not update the screen, so that you can send a sequence |
| 294 | of commands without the cursor moving around. You must end with the "redraw" |
| 295 | command to show any changed text and show the cursor where it belongs. |
| 296 | |
| 297 | The argument is normally an empty string: |
| 298 | ["redraw", ""] ~ |
| 299 | To first clear the screen pass "force": |
| 300 | ["redraw", "force"] ~ |
| 301 | |
| 302 | |
| 303 | Command "ex" ~ |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 304 | |
| 305 | 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] | 306 | completion or error. You could use functions in an |autoload| script: |
| 307 | ["ex","call myscript#MyFunc(arg)"] |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 308 | |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 309 | You can also use "call |feedkeys()|" to insert any key sequence. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 310 | |
Bram Moolenaar | 4f3f668 | 2016-03-26 23:01:59 +0100 | [diff] [blame] | 311 | When there is an error a message is written to the channel log, if it exists, |
| 312 | and v:errmsg is set to the error. |
| 313 | |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 314 | |
| 315 | Command "normal" ~ |
| 316 | |
Bram Moolenaar | 681baaf | 2016-02-04 20:57:07 +0100 | [diff] [blame] | 317 | The "normal" command is executed like with ":normal!", commands are not |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 318 | mapped. Example to open the folds under the cursor: |
| 319 | ["normal" "zO"] |
| 320 | |
| 321 | |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 322 | Command "expr" with response ~ |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 323 | |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 324 | 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] | 325 | example, to get the number of lines in the current buffer: |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 326 | ["expr","line('$')", -2] ~ |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 327 | |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 328 | It will send back the result of the expression: |
Bram Moolenaar | e0fa374 | 2016-02-20 15:47:01 +0100 | [diff] [blame] | 329 | [-2, "last line"] ~ |
| 330 | The format is: |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 331 | [{number}, {result}] |
Bram Moolenaar | 187db50 | 2016-02-27 14:44:26 +0100 | [diff] [blame] | 332 | |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 333 | 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] | 334 | to avoid confusion with message that Vim sends. Use a different number on |
| 335 | every request to be able to match the request with the response. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 336 | |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 337 | {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] | 338 | evaluation fails or the result can't be encoded in JSON it is the string |
| 339 | "ERROR". |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 340 | |
| 341 | |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 342 | Command "expr" without a response ~ |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 343 | |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 344 | 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] | 345 | Example: |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 346 | ["expr","setline('$', ['one', 'two', 'three'])"] ~ |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 347 | There is no third argument in the request. |
| 348 | |
| 349 | |
| 350 | Command "call" ~ |
| 351 | |
| 352 | This is similar to "expr", but instead of passing the whole expression as a |
| 353 | string this passes the name of a function and a list of arguments. This |
| 354 | avoids the conversion of the arguments to a string and escaping and |
| 355 | concatenating them. Example: |
| 356 | ["call", "line", ["$"], -2] ~ |
| 357 | |
| 358 | Leave out the fourth argument if no response is to be sent: |
| 359 | ["call", "setline", ["$", ["one", "two", "three"]]] ~ |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 360 | |
| 361 | ============================================================================== |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 362 | 6. Using a RAW or NL channel *channel-raw* |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 363 | |
Bram Moolenaar | 910b8aa | 2016-02-16 21:03:07 +0100 | [diff] [blame] | 364 | If mode is RAW or NL then a message can be send like this: > |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 365 | let response = ch_evalraw(channel, {string}) |
Bram Moolenaar | 910b8aa | 2016-02-16 21:03:07 +0100 | [diff] [blame] | 366 | |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 367 | The {string} is sent as-is. The response will be what can be read from the |
| 368 | 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] | 369 | message you need to take care of it yourself. The timeout applies for reading |
| 370 | the first byte, after that it will not wait for anything more. |
| 371 | |
Bram Moolenaar | 910b8aa | 2016-02-16 21:03:07 +0100 | [diff] [blame] | 372 | 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] | 373 | to put in the NL after each message. Thus you can also send several messages |
| 374 | ending in a NL at once. The response will be the text up to and including the |
| 375 | first NL. This can also be just the NL for an empty response. |
| 376 | 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] | 377 | |
| 378 | To send a message, without expecting a response: > |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 379 | call ch_sendraw(channel, {string}) |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 380 | The process can send back a response, the channel handler will be called with |
| 381 | it. |
| 382 | |
| 383 | To send a message and letting the response handled by a specific function, |
| 384 | asynchronously: > |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 385 | call ch_sendraw(channel, {string}, {'callback': 'MyHandler'}) |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 386 | |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 387 | This {string} can also be JSON, use |json_encode()| to create it and |
| 388 | |json_decode()| to handle a received JSON message. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 389 | |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 390 | 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] | 391 | |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 392 | ============================================================================== |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 393 | 7. More channel functions *channel-more* |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 394 | |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 395 | To obtain the status of a channel: ch_status(channel). The possible results |
| 396 | are: |
| 397 | "fail" Failed to open the channel. |
| 398 | "open" The channel can be used. |
| 399 | "closed" The channel was closed. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 400 | |
Bram Moolenaar | 187db50 | 2016-02-27 14:44:26 +0100 | [diff] [blame] | 401 | To obtain the job associated with a channel: ch_getjob(channel) |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 402 | |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 403 | To read one message from a channel: > |
| 404 | let output = ch_read(channel) |
| 405 | This uses the channel timeout. To read without a timeout, just get any |
| 406 | message that is available: > |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 407 | let output = ch_read(channel, {'timeout': 0}) |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 408 | When no message was available then the result is v:none for a JSON or JS mode |
| 409 | channels, an empty string for a RAW or NL channel. |
| 410 | |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 411 | To read all output from a RAW channel that is available: > |
| 412 | let output = ch_readraw(channel) |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 413 | To read the error output: > |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 414 | let output = ch_readraw(channel, {"part": "err"}) |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 415 | |
Bram Moolenaar | 4f3f668 | 2016-03-26 23:01:59 +0100 | [diff] [blame] | 416 | ch_read() and ch_readraw() use the channel timeout. When there is nothing to |
| 417 | read within that time an empty string is returned. To specify a different |
| 418 | timeout in msec use the "timeout" option: |
| 419 | {"timeout": 123} ~ |
| 420 | To read from the error output use the "part" option: |
| 421 | {"part": "err"} ~ |
| 422 | To read a message with a specific ID, on a JS or JSON channel: |
| 423 | {"id": 99} ~ |
| 424 | When no ID is specified or the ID is -1, the first message is returned. This |
| 425 | overrules any callback waiting for this message. |
| 426 | |
| 427 | For a RAW channel this returns whatever is available, since Vim does not know |
| 428 | where a message ends. |
| 429 | For a NL channel this returns one message. |
| 430 | For a JS or JSON channel this returns one decoded message. |
| 431 | This includes any sequence number. |
| 432 | |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 433 | ============================================================================== |
| 434 | 8. Starting a job with a channel *job-start* *job* |
| 435 | |
| 436 | To start a job and open a channel for stdin/stdout/stderr: > |
| 437 | let job = job_start(command, {options}) |
| 438 | |
| 439 | You can get the channel with: > |
| 440 | let channel = job_getchannel(job) |
| 441 | |
| 442 | The channel will use NL mode. If you want another mode it's best to specify |
| 443 | this in {options}. When changing the mode later some text may have already |
| 444 | been received and not parsed correctly. |
| 445 | |
| 446 | If the command produces a line of output that you want to deal with, specify |
| 447 | a handler for stdout: > |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 448 | let job = job_start(command, {"out_cb": "MyHandler"}) |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 449 | The function will be called with the channel and a message. You would define |
| 450 | it like this: > |
| 451 | func MyHandler(channel, msg) |
| 452 | |
Bram Moolenaar | 02e83b4 | 2016-02-21 20:10:26 +0100 | [diff] [blame] | 453 | Without the handler you need to read the output with |ch_read()| or |
| 454 | |ch_readraw()|. |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 455 | |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 456 | The handler defined for "out_cb" will not receive stderr. If you want to |
| 457 | handle that separately, add an "err_cb" handler: > |
| 458 | let job = job_start(command, {"out_cb": "MyHandler", |
| 459 | \ "err_cb": "ErrHandler"}) |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 460 | |
Bram Moolenaar | 02e83b4 | 2016-02-21 20:10:26 +0100 | [diff] [blame] | 461 | If you want to handle both stderr and stdout with one handler use the |
| 462 | "callback" option: > |
| 463 | let job = job_start(command, {"callback": "MyHandler"}) |
| 464 | |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 465 | You can send a message to the command with ch_evalraw(). If the channel is in |
| 466 | JSON or JS mode you can use ch_evalexpr(). |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 467 | |
| 468 | There are several options you can use, see |job-options|. |
Bram Moolenaar | 187db50 | 2016-02-27 14:44:26 +0100 | [diff] [blame] | 469 | For example, to start a job and write its output in buffer "dummy": > |
| 470 | let logjob = job_start("tail -f /tmp/log", |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 471 | \ {'out_io': 'buffer', 'out_name': 'dummy'}) |
Bram Moolenaar | 187db50 | 2016-02-27 14:44:26 +0100 | [diff] [blame] | 472 | sbuf dummy |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 473 | |
Bram Moolenaar | 5f148ec | 2016-03-07 22:59:26 +0100 | [diff] [blame] | 474 | |
| 475 | Job input from a buffer ~ |
| 476 | |
Bram Moolenaar | 328da0d | 2016-03-04 22:22:32 +0100 | [diff] [blame] | 477 | To run a job that reads from a buffer: > |
| 478 | let job = job_start({command}, |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 479 | \ {'in_io': 'buffer', 'in_name': 'mybuffer'}) |
Bram Moolenaar | 328da0d | 2016-03-04 22:22:32 +0100 | [diff] [blame] | 480 | < |
| 481 | *E915* *E918* |
| 482 | The buffer is found by name, similar to |bufnr()|. The buffer must exist and |
| 483 | be loaded when job_start() is called. |
| 484 | |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 485 | By default this reads the whole buffer. This can be changed with the "in_top" |
| 486 | and "in_bot" options. |
Bram Moolenaar | 328da0d | 2016-03-04 22:22:32 +0100 | [diff] [blame] | 487 | |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 488 | A special mode is when "in_top" is set to zero and "in_bot" is not set: Every |
Bram Moolenaar | 5f148ec | 2016-03-07 22:59:26 +0100 | [diff] [blame] | 489 | time a line is added to the buffer, the last-but-one line will be send to the |
| 490 | job stdin. This allows for editing the last line and sending it when pressing |
| 491 | Enter. |
Bram Moolenaar | 328da0d | 2016-03-04 22:22:32 +0100 | [diff] [blame] | 492 | |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 493 | ============================================================================== |
| 494 | 9. Starting a job without a channel *job-start-nochannel* |
| 495 | |
| 496 | To start another process without creating a channel: > |
Bram Moolenaar | 328da0d | 2016-03-04 22:22:32 +0100 | [diff] [blame] | 497 | let job = job_start(command, |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 498 | \ {"in_io": "null", "out_io": "null", "err_io": "null"}) |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 499 | |
| 500 | This starts {command} in the background, Vim does not wait for it to finish. |
| 501 | |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 502 | When Vim sees that neither stdin, stdout or stderr are connected, no channel |
| 503 | will be created. Often you will want to include redirection in the command to |
| 504 | avoid it getting stuck. |
| 505 | |
| 506 | There are several options you can use, see |job-options|. |
| 507 | |
Bram Moolenaar | 77cdfd1 | 2016-03-12 12:57:59 +0100 | [diff] [blame] | 508 | *job-start-if-needed* |
| 509 | To start a job only when connecting to an address does not work, do something |
| 510 | like this: > |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 511 | let channel = ch_open(address, {"waittime": 0}) |
| 512 | if ch_status(channel) == "fail" |
| 513 | let job = job_start(command) |
| 514 | let channel = ch_open(address, {"waittime": 1000}) |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 515 | endif |
Bram Moolenaar | 77cdfd1 | 2016-03-12 12:57:59 +0100 | [diff] [blame] | 516 | |
| 517 | Note that the waittime for ch_open() gives the job one second to make the port |
| 518 | available. |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 519 | |
| 520 | ============================================================================== |
| 521 | 10. Job options *job-options* |
| 522 | |
| 523 | The {options} argument in job_start() is a dictionary. All entries are |
Bram Moolenaar | 02e83b4 | 2016-02-21 20:10:26 +0100 | [diff] [blame] | 524 | optional. Some options can be used after the job has started, using |
| 525 | job_setoptions(job, {options}). Many options can be used with the channel |
| 526 | related to the job, using ch_setoptions(channel, {options}). |
| 527 | See |job_setoptions()| and |ch_setoptions()|. |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 528 | |
Bram Moolenaar | 4f3f668 | 2016-03-26 23:01:59 +0100 | [diff] [blame] | 529 | *in_mode* *out_mode* *err_mode* |
| 530 | "in_mode" mode specifically for stdin, only when using pipes |
| 531 | "out_mode" mode specifically for stdout, only when using pipes |
| 532 | "err_mode" mode specifically for stderr, only when using pipes |
| 533 | See |channel-mode| for the values. |
| 534 | |
| 535 | Note: when setting "mode" the part specific mode is |
| 536 | overwritten. Therefore set "mode" first and the part |
| 537 | specific mode later. |
| 538 | |
| 539 | Note: when writing to a file or buffer and when |
| 540 | reading from a buffer NL mode is used by default. |
| 541 | |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 542 | *job-callback* |
| 543 | "callback": handler Callback for something to read on any part of the |
| 544 | channel. |
Bram Moolenaar | 4f3f668 | 2016-03-26 23:01:59 +0100 | [diff] [blame] | 545 | *job-out_cb* *out_cb* |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 546 | "out_cb": handler Callback for when there is something to read on |
Bram Moolenaar | 4f3f668 | 2016-03-26 23:01:59 +0100 | [diff] [blame] | 547 | stdout. Only for when the channel uses pipes. When |
| 548 | "out_cb" wasn't set the channel callback is used. |
| 549 | |
| 550 | *job-err_cb* *err_cb* |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 551 | "err_cb": handler Callback for when there is something to read on |
Bram Moolenaar | 4f3f668 | 2016-03-26 23:01:59 +0100 | [diff] [blame] | 552 | stderr. Only for when the channel uses pipes. When |
| 553 | "err_cb" wasn't set the channel callback is used. |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 554 | *job-close_cb* |
| 555 | "close_cb": handler Callback for when the channel is closed. Same as |
| 556 | "close_cb" on ch_open(). |
| 557 | *job-exit_cb* |
| 558 | "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] | 559 | job and the exit status. |
Bram Moolenaar | 02e83b4 | 2016-02-21 20:10:26 +0100 | [diff] [blame] | 560 | Vim checks about every 10 seconds for jobs that ended. |
| 561 | The callback can also be triggered by calling |
| 562 | |job_status()|. |
Bram Moolenaar | 4f3f668 | 2016-03-26 23:01:59 +0100 | [diff] [blame] | 563 | *job-timeout* |
| 564 | "timeout" The time to wait for a request when blocking, E.g. |
| 565 | when using ch_evalexpr(). In milliseconds. The |
| 566 | default is 2000 (2 seconds). |
| 567 | *out_timeout* *err_timeout* |
| 568 | "out_timeout" Timeout for stdout. Only when using pipes. |
| 569 | "err_timeout" Timeout for stderr. Only when using pipes. |
| 570 | Note: when setting "timeout" the part specific mode is |
| 571 | overwritten. Therefore set "timeout" first and the |
| 572 | part specific mode later. |
| 573 | |
Bram Moolenaar | 02e83b4 | 2016-02-21 20:10:26 +0100 | [diff] [blame] | 574 | *job-stoponexit* |
| 575 | "stoponexit": {signal} Send {signal} to the job when Vim exits. See |
| 576 | |job_stop()| for possible values. |
| 577 | "stoponexit": "" Do not stop the job when Vim exits. |
| 578 | The default is "term". |
| 579 | |
Bram Moolenaar | 77cdfd1 | 2016-03-12 12:57:59 +0100 | [diff] [blame] | 580 | *job-term* |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 581 | "term": "open" Start a terminal and connect the job |
| 582 | stdin/stdout/stderr to it. |
Bram Moolenaar | 77cdfd1 | 2016-03-12 12:57:59 +0100 | [diff] [blame] | 583 | NOTE: Not implemented yet! |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 584 | |
Bram Moolenaar | 77cdfd1 | 2016-03-12 12:57:59 +0100 | [diff] [blame] | 585 | "channel": {channel} Use an existing channel instead of creating a new one. |
| 586 | The parts of the channel that get used for the new job |
| 587 | will be disconnected from what they were used before. |
| 588 | If the channel was still use by another job this may |
| 589 | cause I/O errors. |
| 590 | Existing callbacks and other settings remain. |
| 591 | |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 592 | *job-in_io* *in_top* *in_bot* *in_name* *in_buf* |
| 593 | "in_io": "null" disconnect stdin (read from /dev/null) |
| 594 | "in_io": "pipe" stdin is connected to the channel (default) |
| 595 | "in_io": "file" stdin reads from a file |
| 596 | "in_io": "buffer" stdin reads from a buffer |
| 597 | "in_top": number when using "buffer": first line to send (default: 1) |
| 598 | "in_bot": number when using "buffer": last line to send (default: last) |
| 599 | "in_name": "/path/file" the name of the file or buffer to read from |
| 600 | "in_buf": number the number of the buffer to read from |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 601 | |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 602 | *job-out_io* *out_name* *out_buf* |
| 603 | "out_io": "null" disconnect stdout (goes to /dev/null) |
| 604 | "out_io": "pipe" stdout is connected to the channel (default) |
| 605 | "out_io": "file" stdout writes to a file |
| 606 | "out_io": "buffer" stdout appends to a buffer |
| 607 | "out_name": "/path/file" the name of the file or buffer to write to |
| 608 | "out_buf": number the number of the buffer to write to |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 609 | |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 610 | *job-err_io* *err_name* *err_buf* |
| 611 | "err_io": "out" stderr messages to go to stdout |
| 612 | "err_io": "null" disconnect stderr (goes to /dev/null) |
| 613 | "err_io": "pipe" stderr is connected to the channel (default) |
| 614 | "err_io": "file" stderr writes to a file |
| 615 | "err_io": "buffer" stderr appends to a buffer |
| 616 | "err_name": "/path/file" the name of the file or buffer to write to |
| 617 | "err_buf": number the number of the buffer to write to |
Bram Moolenaar | 77cdfd1 | 2016-03-12 12:57:59 +0100 | [diff] [blame] | 618 | |
Bram Moolenaar | 7db8f6f | 2016-03-29 23:12:46 +0200 | [diff] [blame] | 619 | "block_write": number only for testing: pretend every other write to stdin |
| 620 | will block |
| 621 | |
Bram Moolenaar | 77cdfd1 | 2016-03-12 12:57:59 +0100 | [diff] [blame] | 622 | |
| 623 | Writing to a buffer ~ |
Bram Moolenaar | 187db50 | 2016-02-27 14:44:26 +0100 | [diff] [blame] | 624 | |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 625 | 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] | 626 | is appended to the buffer before invoking the callback. |
| 627 | |
| 628 | When a buffer is used both for input and output, the output lines are put |
| 629 | 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] | 630 | input. Otherwise lines are appended below the last line. |
Bram Moolenaar | c7f0ebc | 2016-02-27 21:10:09 +0100 | [diff] [blame] | 631 | |
Bram Moolenaar | 328da0d | 2016-03-04 22:22:32 +0100 | [diff] [blame] | 632 | When using JS or JSON mode with "buffer", only messages with zero or negative |
| 633 | ID will be added to the buffer, after decoding + encoding. Messages with a |
| 634 | positive number will be handled by a callback, commands are handled as usual. |
| 635 | |
Bram Moolenaar | 187db50 | 2016-02-27 14:44:26 +0100 | [diff] [blame] | 636 | The name of the buffer is compared the full name of existing buffers. If |
Bram Moolenaar | c7f0ebc | 2016-02-27 21:10:09 +0100 | [diff] [blame] | 637 | there is a match that buffer is used. Otherwise a new buffer is created. |
| 638 | Use an empty name to always create a new buffer. |ch_getbufnr()| can then be |
| 639 | used to get the buffer number. |
| 640 | |
| 641 | For a new buffer 'buftype' is set to "nofile" and 'bufhidden' to "hide". If |
| 642 | you prefer other settings, create the buffer first and pass the buffer number. |
Bram Moolenaar | 187db50 | 2016-02-27 14:44:26 +0100 | [diff] [blame] | 643 | |
| 644 | When the buffer written to is displayed in a window and the cursor is in the |
| 645 | first column of the last line, the cursor will be moved to the newly added |
| 646 | line and the window is scrolled up to show the cursor if needed. |
| 647 | |
| 648 | Undo is synced for every added line. |
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 | |
| 651 | Writing to a file ~ |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 652 | *E920* |
Bram Moolenaar | 77cdfd1 | 2016-03-12 12:57:59 +0100 | [diff] [blame] | 653 | The file is created with permissions 600 (read-write for the user, not |
| 654 | accessible for others). Use |setfperm()| to change this. |
| 655 | |
| 656 | If the file already exists it is truncated. |
| 657 | |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 658 | ============================================================================== |
| 659 | 11. Controlling a job *job-control* |
| 660 | |
| 661 | To get the status of a job: > |
| 662 | echo job_status(job) |
| 663 | |
| 664 | To make a job stop running: > |
| 665 | job_stop(job) |
| 666 | |
| 667 | This is the normal way to end a job. On Unix it sends a SIGTERM to the job. |
| 668 | It is possible to use other ways to stop the job, or even send arbitrary |
| 669 | signals. E.g. to force a job to stop, "kill it": > |
| 670 | job_stop(job, "kill") |
| 671 | |
| 672 | For more options see |job_stop()|. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 673 | |
| 674 | |
| 675 | vim:tw=78:ts=8:ft=help:norl: |