Bram Moolenaar | f391327 | 2016-02-25 00:00:01 +0100 | [diff] [blame] | 1 | *channel.txt* For Vim version 7.4. Last change: 2016 Feb 23 |
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 | |
| 47 | For when using sockets See |job-start|, |job-may-start| and |channel-open|. |
| 48 | For 2 and 3, one or more jobs using pipes, see |job-start|. |
| 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 | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 77 | echo ch_sendexpr(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 | 02e83b4 | 2016-02-21 20:10:26 +0100 | [diff] [blame] | 104 | call ch_sendexpr(channel, 'hello!', {'callback': 0}) |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 105 | |
| 106 | ============================================================================== |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 107 | 3. Opening a channel *channel-open* |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 108 | |
Bram Moolenaar | 681baaf | 2016-02-04 20:57:07 +0100 | [diff] [blame] | 109 | To open a channel: > |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 110 | let channel = ch_open({address} [, {options}]) |
Bram Moolenaar | 02e83b4 | 2016-02-21 20:10:26 +0100 | [diff] [blame] | 111 | if ch_status(channel) == "open" |
| 112 | " use the channel |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 113 | |
| 114 | Use |ch_status()| to see if the channel could be opened. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 115 | |
| 116 | {address} has the form "hostname:port". E.g., "localhost:8765". |
| 117 | |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 118 | {options} is a dictionary with optional entries: |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 119 | |
| 120 | "mode" can be: *channel-mode* |
| 121 | "json" - Use JSON, see below; most convenient way. Default. |
Bram Moolenaar | 910b8aa | 2016-02-16 21:03:07 +0100 | [diff] [blame] | 122 | "js" - Use JS (JavaScript) encoding, more efficient than JSON. |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 123 | "nl" - Use messages that end in a NL character |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 124 | "raw" - Use raw messages |
| 125 | |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 126 | "in-mode" mode specifically for stdin, only when using pipes |
| 127 | "out-mode" mode specifically for stdout, only when using pipes |
| 128 | "err-mode" mode specifically for stderr, only when using pipes |
| 129 | Note: when setting "mode" the part specific mode is |
| 130 | overwritten. Therefore set "mode" first and the part specific |
| 131 | mode later. |
| 132 | |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 133 | *channel-callback* |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 134 | "callback" A function that is called when a message is received that is |
| 135 | not handled otherwise. It gets two arguments: the channel |
Bram Moolenaar | 02e83b4 | 2016-02-21 20:10:26 +0100 | [diff] [blame] | 136 | and the received message. Example: > |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 137 | func Handle(channel, msg) |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 138 | echo 'Received: ' . a:msg |
| 139 | endfunc |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 140 | let channel = ch_open("localhost:8765", {"callback": "Handle"}) |
| 141 | < |
Bram Moolenaar | 02e83b4 | 2016-02-21 20:10:26 +0100 | [diff] [blame] | 142 | When "mode" is "json" or "js" the "msg" argument is the body |
| 143 | of the received message, converted to Vim types. |
| 144 | When "mode" is "nl" the "msg" argument is one message, |
| 145 | excluding the NL. |
| 146 | When "mode" is "raw" the "msg" argument is the whole message |
| 147 | as a string. |
| 148 | *out-cb* |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 149 | "out-cb" A function like "callback" but used for stdout. Only for when |
| 150 | the channel uses pipes. When "out-cb" wasn't set the channel |
| 151 | callback is used. |
Bram Moolenaar | 02e83b4 | 2016-02-21 20:10:26 +0100 | [diff] [blame] | 152 | *err-cb* |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 153 | "err-cb" A function like "callback" but used for stderr. Only for when |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 154 | the channel uses pipes. When "err-cb" wasn't set the channel |
| 155 | callback is used. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 156 | |
Bram Moolenaar | 02e83b4 | 2016-02-21 20:10:26 +0100 | [diff] [blame] | 157 | TODO: *close-cb* |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 158 | "close-cb" A function that is called when the channel gets closed, other |
| 159 | than by calling ch_close(). It should be defined like this: > |
| 160 | func MyCloseHandler(channel) |
Bram Moolenaar | 02e83b4 | 2016-02-21 20:10:26 +0100 | [diff] [blame] | 161 | < *waittime* |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 162 | "waittime" The time to wait for the connection to be made in |
Bram Moolenaar | f391327 | 2016-02-25 00:00:01 +0100 | [diff] [blame] | 163 | milliseconds. A negative number waits forever. |
| 164 | |
| 165 | The default is zero, don't wait, which is useful if a local |
| 166 | server is supposed to be running already. On Unix Vim |
| 167 | actually uses a 1 msec timeout, that is required on many |
| 168 | systems. Use a larger value for a remote server, e.g. 10 |
| 169 | msec at least. |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 170 | |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 171 | "timeout" The time to wait for a request when blocking, E.g. when using |
| 172 | ch_sendexpr(). In milliseconds. The default is 2000 (2 |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 173 | seconds). |
Bram Moolenaar | 02e83b4 | 2016-02-21 20:10:26 +0100 | [diff] [blame] | 174 | *out-timeout* *err-timeout* |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 175 | "out-timeout" Timeout for stdout. Only when using pipes. |
| 176 | "err-timeout" Timeout for stderr. Only when using pipes. |
| 177 | Note: when setting "timeout" the part specific mode is |
| 178 | overwritten. Therefore set "timeout" first and the part |
| 179 | specific mode later. |
| 180 | |
Bram Moolenaar | 595e64e | 2016-02-07 19:19:53 +0100 | [diff] [blame] | 181 | When "mode" is "json" or "js" the "callback" is optional. When omitted it is |
| 182 | only possible to receive a message after sending one. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 183 | |
Bram Moolenaar | 02e83b4 | 2016-02-21 20:10:26 +0100 | [diff] [blame] | 184 | To change the channel options after opening it use |ch_setoptions()|. The |
| 185 | arguments are similar to what is passed to |ch_open()|, but "waittime" cannot |
| 186 | be given, since that only applies to opening the channel. |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 187 | |
Bram Moolenaar | 02e83b4 | 2016-02-21 20:10:26 +0100 | [diff] [blame] | 188 | For example, the handler can be added or changed: > |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 189 | call ch_setoptions(channel, {'callback': callback}) |
| 190 | When "callback" is empty (zero or an empty string) the handler is removed. |
| 191 | |
| 192 | The timeout can be changed: > |
| 193 | call ch_setoptions(channel, {'timeout': msec}) |
| 194 | < |
Bram Moolenaar | 02e83b4 | 2016-02-21 20:10:26 +0100 | [diff] [blame] | 195 | *channel-close* *E906* |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 196 | Once done with the channel, disconnect it like this: > |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 197 | call ch_close(channel) |
| 198 | When a socket is used this will close the socket for both directions. When |
| 199 | pipes are used (stdin/stdout/stderr) they are all closed. This might not be |
| 200 | what you want! Stopping the job with job_stop() might be better. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 201 | |
Bram Moolenaar | cbebd48 | 2016-02-07 23:02:56 +0100 | [diff] [blame] | 202 | When the channel can't be opened you will get an error message. There is a |
| 203 | difference between MS-Windows and Unix: On Unix when the port doesn't exist |
| 204 | ch_open() fails quickly. On MS-Windows "waittime" applies. |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 205 | *E898* *E899* *E900* *E901* *E902* |
| 206 | |
| 207 | If there is an error reading or writing a channel it will be closed. |
| 208 | *E896* *E630* *E631* |
| 209 | |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 210 | ============================================================================== |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 211 | 4. Using a JSON or JS channel *channel-use* |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 212 | |
Bram Moolenaar | 910b8aa | 2016-02-16 21:03:07 +0100 | [diff] [blame] | 213 | If mode is JSON then a message can be sent synchronously like this: > |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 214 | let response = ch_sendexpr(channel, {expr}) |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 215 | This awaits a response from the other side. |
| 216 | |
Bram Moolenaar | 910b8aa | 2016-02-16 21:03:07 +0100 | [diff] [blame] | 217 | 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] | 218 | JavaScript encoding. See |js_encode()| for the difference. |
Bram Moolenaar | 595e64e | 2016-02-07 19:19:53 +0100 | [diff] [blame] | 219 | |
Bram Moolenaar | 02e83b4 | 2016-02-21 20:10:26 +0100 | [diff] [blame] | 220 | To send a message, without handling a response or letting the channel callback |
| 221 | handle the response: > |
| 222 | call ch_sendexpr(channel, {expr}, {'callback': 0}) |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 223 | |
| 224 | To send a message and letting the response handled by a specific function, |
| 225 | asynchronously: > |
Bram Moolenaar | 02e83b4 | 2016-02-21 20:10:26 +0100 | [diff] [blame] | 226 | call ch_sendexpr(channel, {expr}, {'callback': Handler}) |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 227 | |
| 228 | Vim will match the response with the request using the message ID. Once the |
| 229 | response is received the callback will be invoked. Further responses with the |
| 230 | same ID will be ignored. If your server sends back multiple responses you |
| 231 | 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] | 232 | |
| 233 | The {expr} is converted to JSON and wrapped in an array. An example of the |
| 234 | message that the receiver will get when {expr} is the string "hello": |
| 235 | [12,"hello"] ~ |
| 236 | |
| 237 | The format of the JSON sent is: |
| 238 | [{number},{expr}] |
| 239 | |
| 240 | In which {number} is different every time. It must be used in the response |
| 241 | (if any): |
| 242 | |
| 243 | [{number},{response}] |
| 244 | |
| 245 | This way Vim knows which sent message matches with which received message and |
| 246 | can call the right handler. Also when the messages arrive out of order. |
| 247 | |
| 248 | The sender must always send valid JSON to Vim. Vim can check for the end of |
| 249 | the message by parsing the JSON. It will only accept the message if the end |
| 250 | was received. |
| 251 | |
| 252 | When the process wants to send a message to Vim without first receiving a |
| 253 | message, it must use the number zero: |
| 254 | [0,{response}] |
| 255 | |
| 256 | Then channel handler will then get {response} converted to Vim types. If the |
| 257 | channel does not have a handler the message is dropped. |
| 258 | |
Bram Moolenaar | e0fa374 | 2016-02-20 15:47:01 +0100 | [diff] [blame] | 259 | On read error or ch_close(), when using a socket, the string "DETACH" is sent, |
Bram Moolenaar | f391327 | 2016-02-25 00:00:01 +0100 | [diff] [blame] | 260 | if still possible. The channel will then be inactive. For a JSON and JS mode |
| 261 | channel quotes are used around DETACH, otherwise there are no quotes. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 262 | |
Bram Moolenaar | cbebd48 | 2016-02-07 23:02:56 +0100 | [diff] [blame] | 263 | It is also possible to use ch_sendraw() on a JSON or JS channel. The caller |
| 264 | is then completely responsible for correct encoding and decoding. |
| 265 | |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 266 | ============================================================================== |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 267 | 5. Channel commands *channel-commands* |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 268 | |
Bram Moolenaar | 910b8aa | 2016-02-16 21:03:07 +0100 | [diff] [blame] | 269 | 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] | 270 | handled by Vim internally, it does not require a handler for the channel. |
| 271 | |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 272 | Possible commands are: *E903* *E904* *E905* |
| 273 | ["redraw" {forced}] |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 274 | ["ex", {Ex command}] |
| 275 | ["normal", {Normal mode command}] |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 276 | ["expr", {expression}, {number}] |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 277 | ["expr", {expression}] |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 278 | ["call", {func name}, {argument list}, {number}] |
| 279 | ["call", {func name}, {argument list}] |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 280 | |
| 281 | With all of these: Be careful what these commands do! You can easily |
| 282 | interfere with what the user is doing. To avoid trouble use |mode()| to check |
| 283 | 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] | 284 | inserted as text, not executed as a command: |
| 285 | ["ex","if mode() == 'i' | call feedkeys('ClassName') | endif"] ~ |
| 286 | |
| 287 | Errors in these commands are normally not reported to avoid them messing up |
| 288 | the display. If you do want to see them, set the 'verbose' option to 3 or |
| 289 | higher. |
| 290 | |
| 291 | |
| 292 | Command "redraw" ~ |
| 293 | |
| 294 | The other commands do not update the screen, so that you can send a sequence |
| 295 | of commands without the cursor moving around. You must end with the "redraw" |
| 296 | command to show any changed text and show the cursor where it belongs. |
| 297 | |
| 298 | The argument is normally an empty string: |
| 299 | ["redraw", ""] ~ |
| 300 | To first clear the screen pass "force": |
| 301 | ["redraw", "force"] ~ |
| 302 | |
| 303 | |
| 304 | Command "ex" ~ |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 305 | |
| 306 | 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] | 307 | completion or error. You could use functions in an |autoload| script: |
| 308 | ["ex","call myscript#MyFunc(arg)"] |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 309 | |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 310 | You can also use "call |feedkeys()|" to insert any key sequence. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 311 | |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 312 | |
| 313 | Command "normal" ~ |
| 314 | |
Bram Moolenaar | 681baaf | 2016-02-04 20:57:07 +0100 | [diff] [blame] | 315 | The "normal" command is executed like with ":normal!", commands are not |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 316 | mapped. Example to open the folds under the cursor: |
| 317 | ["normal" "zO"] |
| 318 | |
| 319 | |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 320 | Command "expr" with response ~ |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 321 | |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 322 | 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] | 323 | example, to get the number of lines in the current buffer: |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 324 | ["expr","line('$')", -2] ~ |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 325 | |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 326 | It will send back the result of the expression: |
Bram Moolenaar | e0fa374 | 2016-02-20 15:47:01 +0100 | [diff] [blame] | 327 | [-2, "last line"] ~ |
| 328 | The format is: |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 329 | [{number}, {result}] |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 330 | *E915* |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 331 | 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] | 332 | to avoid confusion with message that Vim sends. Use a different number on |
| 333 | every request to be able to match the request with the response. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 334 | |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 335 | {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] | 336 | evaluation fails or the result can't be encoded in JSON it is the string |
| 337 | "ERROR". |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 338 | |
| 339 | |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 340 | Command "expr" without a response ~ |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 341 | |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 342 | 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] | 343 | Example: |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 344 | ["expr","setline('$', ['one', 'two', 'three'])"] ~ |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 345 | There is no third argument in the request. |
| 346 | |
| 347 | |
| 348 | Command "call" ~ |
| 349 | |
| 350 | This is similar to "expr", but instead of passing the whole expression as a |
| 351 | string this passes the name of a function and a list of arguments. This |
| 352 | avoids the conversion of the arguments to a string and escaping and |
| 353 | concatenating them. Example: |
| 354 | ["call", "line", ["$"], -2] ~ |
| 355 | |
| 356 | Leave out the fourth argument if no response is to be sent: |
| 357 | ["call", "setline", ["$", ["one", "two", "three"]]] ~ |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 358 | |
| 359 | ============================================================================== |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 360 | 6. Using a RAW or NL channel *channel-raw* |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 361 | |
Bram Moolenaar | 910b8aa | 2016-02-16 21:03:07 +0100 | [diff] [blame] | 362 | If mode is RAW or NL then a message can be send like this: > |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 363 | let response = ch_sendraw(channel, {string}) |
Bram Moolenaar | 910b8aa | 2016-02-16 21:03:07 +0100 | [diff] [blame] | 364 | |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 365 | The {string} is sent as-is. The response will be what can be read from the |
| 366 | 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] | 367 | message you need to take care of it yourself. The timeout applies for reading |
| 368 | the first byte, after that it will not wait for anything more. |
| 369 | |
Bram Moolenaar | 910b8aa | 2016-02-16 21:03:07 +0100 | [diff] [blame] | 370 | 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] | 371 | to put in the NL after each message. Thus you can also send several messages |
| 372 | ending in a NL at once. The response will be the text up to and including the |
| 373 | first NL. This can also be just the NL for an empty response. |
| 374 | 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] | 375 | |
| 376 | To send a message, without expecting a response: > |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 377 | call ch_sendraw(channel, {string}, 0) |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 378 | The process can send back a response, the channel handler will be called with |
| 379 | it. |
| 380 | |
| 381 | To send a message and letting the response handled by a specific function, |
| 382 | asynchronously: > |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 383 | call ch_sendraw(channel, {string}, {callback}) |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 384 | |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 385 | This {string} can also be JSON, use |json_encode()| to create it and |
| 386 | |json_decode()| to handle a received JSON message. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 387 | |
Bram Moolenaar | cbebd48 | 2016-02-07 23:02:56 +0100 | [diff] [blame] | 388 | It is not possible to use |ch_sendexpr()| on a raw channel. |
| 389 | |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 390 | ============================================================================== |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 391 | 7. More channel functions *channel-more* |
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 | To obtain the status of a channel: ch_status(channel). The possible results |
| 394 | are: |
| 395 | "fail" Failed to open the channel. |
| 396 | "open" The channel can be used. |
| 397 | "closed" The channel was closed. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 398 | |
| 399 | TODO: |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 400 | To objain the job associated with a channel: ch_getjob(channel) |
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 | To read one message from a channel: > |
| 403 | let output = ch_read(channel) |
| 404 | This uses the channel timeout. To read without a timeout, just get any |
| 405 | message that is available: > |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 406 | let output = ch_read(channel, {'timeout': 0}) |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 407 | When no message was available then the result is v:none for a JSON or JS mode |
| 408 | channels, an empty string for a RAW or NL channel. |
| 409 | |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 410 | To read all output from a RAW channel that is available: > |
| 411 | let output = ch_readraw(channel) |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 412 | To read the error output: > |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 413 | let output = ch_readraw(channel, {"part": "err"}) |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 414 | |
| 415 | ============================================================================== |
| 416 | 8. Starting a job with a channel *job-start* *job* |
| 417 | |
| 418 | To start a job and open a channel for stdin/stdout/stderr: > |
| 419 | let job = job_start(command, {options}) |
| 420 | |
| 421 | You can get the channel with: > |
| 422 | let channel = job_getchannel(job) |
| 423 | |
| 424 | The channel will use NL mode. If you want another mode it's best to specify |
| 425 | this in {options}. When changing the mode later some text may have already |
| 426 | been received and not parsed correctly. |
| 427 | |
| 428 | If the command produces a line of output that you want to deal with, specify |
| 429 | a handler for stdout: > |
| 430 | let job = job_start(command, {"out-cb": "MyHandler"}) |
| 431 | The function will be called with the channel and a message. You would define |
| 432 | it like this: > |
| 433 | func MyHandler(channel, msg) |
| 434 | |
Bram Moolenaar | 02e83b4 | 2016-02-21 20:10:26 +0100 | [diff] [blame] | 435 | Without the handler you need to read the output with |ch_read()| or |
| 436 | |ch_readraw()|. |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 437 | |
Bram Moolenaar | 02e83b4 | 2016-02-21 20:10:26 +0100 | [diff] [blame] | 438 | The handler defined for "out-cb" will not receive stderr. If you want to |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 439 | handle that separately, add an "err-cb" handler: > |
| 440 | let job = job_start(command, {"out-cb": "MyHandler", |
| 441 | \ "err-cb": "ErrHandler"}) |
| 442 | |
Bram Moolenaar | 02e83b4 | 2016-02-21 20:10:26 +0100 | [diff] [blame] | 443 | If you want to handle both stderr and stdout with one handler use the |
| 444 | "callback" option: > |
| 445 | let job = job_start(command, {"callback": "MyHandler"}) |
| 446 | |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 447 | You can send a message to the command with ch_sendraw(). If the channel is in |
| 448 | JSON or JS mode you can use ch_sendexpr(). |
| 449 | |
| 450 | There are several options you can use, see |job-options|. |
| 451 | |
| 452 | TODO: |
| 453 | To run a job and read its output once it is done: > |
| 454 | |
| 455 | let job = job_start({command}, {'exit-cb': 'MyHandler'}) |
| 456 | func MyHandler(job, status) |
| 457 | let channel = job_getchannel() |
| 458 | let output = ch_readall(channel) |
| 459 | " parse output |
| 460 | endfunc |
| 461 | |
| 462 | ============================================================================== |
| 463 | 9. Starting a job without a channel *job-start-nochannel* |
| 464 | |
| 465 | To start another process without creating a channel: > |
| 466 | let job = job_start(command, {"in-io": "null", "out-io": "null"}) |
| 467 | |
| 468 | This starts {command} in the background, Vim does not wait for it to finish. |
| 469 | |
| 470 | TODO: |
| 471 | When Vim sees that neither stdin, stdout or stderr are connected, no channel |
| 472 | will be created. Often you will want to include redirection in the command to |
| 473 | avoid it getting stuck. |
| 474 | |
| 475 | There are several options you can use, see |job-options|. |
| 476 | |
| 477 | TODO: *job-may-start* |
| 478 | To start a job only when connecting to an address does not work use |
| 479 | job_maystart('command', {address}, {options}), For Example: > |
| 480 | let job = job_maystart(command, address, {"waittime": 1000}) |
| 481 | let channel = job_gethandle(job) |
| 482 | |
| 483 | This comes down to: > |
| 484 | let channel = ch_open(address, {"waittime": 0}) |
| 485 | if ch_status(channel) == "fail" |
| 486 | let job = job_start(command) |
| 487 | let channel = ch_open(address, {"waittime": 1000}) |
| 488 | call job_sethandle(channel) |
| 489 | endif |
| 490 | Note that the specified waittime applies to when the job has been started. |
| 491 | This gives the job some time to make the port available. |
| 492 | |
| 493 | ============================================================================== |
| 494 | 10. Job options *job-options* |
| 495 | |
| 496 | The {options} argument in job_start() is a dictionary. All entries are |
Bram Moolenaar | 02e83b4 | 2016-02-21 20:10:26 +0100 | [diff] [blame] | 497 | optional. Some options can be used after the job has started, using |
| 498 | job_setoptions(job, {options}). Many options can be used with the channel |
| 499 | related to the job, using ch_setoptions(channel, {options}). |
| 500 | See |job_setoptions()| and |ch_setoptions()|. |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 501 | |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 502 | *job-callback* |
| 503 | "callback": handler Callback for something to read on any part of the |
| 504 | channel. |
| 505 | *job-out-cb* |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 506 | "out-cb": handler Callback for when there is something to read on |
| 507 | stdout. |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 508 | *job-err-cb* |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 509 | "err-cb": handler Callback for when there is something to read on |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 510 | stderr. |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 511 | TODO: *job-close-cb* |
| 512 | "close-cb": handler Callback for when the channel is closed. Same as |
| 513 | "close-cb" on ch_open(). |
Bram Moolenaar | 02e83b4 | 2016-02-21 20:10:26 +0100 | [diff] [blame] | 514 | *job-exit-cb* |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 515 | "exit-cb": handler Callback for when the job ends. The arguments are the |
| 516 | job and the exit status. |
Bram Moolenaar | 02e83b4 | 2016-02-21 20:10:26 +0100 | [diff] [blame] | 517 | Vim checks about every 10 seconds for jobs that ended. |
| 518 | The callback can also be triggered by calling |
| 519 | |job_status()|. |
| 520 | *job-stoponexit* |
| 521 | "stoponexit": {signal} Send {signal} to the job when Vim exits. See |
| 522 | |job_stop()| for possible values. |
| 523 | "stoponexit": "" Do not stop the job when Vim exits. |
| 524 | The default is "term". |
| 525 | |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 526 | TODO: *job-term* |
| 527 | "term": "open" Start a terminal and connect the job |
| 528 | stdin/stdout/stderr to it. |
| 529 | |
| 530 | TODO: *job-in-io* |
| 531 | "in-io": "null" disconnect stdin |
| 532 | "in-io": "pipe" stdin is connected to the channel (default) |
| 533 | "in-io": "file" stdin reads from a file |
| 534 | "in-file": "/path/file" the file to read from |
| 535 | |
| 536 | TODO: *job-out-io* |
| 537 | "out-io": "null" disconnect stdout |
| 538 | "out-io": "pipe" stdout is connected to the channel (default) |
| 539 | "out-io": "file" stdout writes to a file |
| 540 | "out-file": "/path/file" the file to write to |
| 541 | "out-io": "buffer" stdout appends to a buffer |
| 542 | "out-buffer": "name" buffer to append to |
| 543 | |
| 544 | TODO: *job-err-io* |
Bram Moolenaar | 910b8aa | 2016-02-16 21:03:07 +0100 | [diff] [blame] | 545 | "err-io": "out" same type as stdout (default) |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 546 | "err-io": "null" disconnect stderr |
| 547 | "err-io": "pipe" stderr is connected to the channel |
| 548 | "err-io": "file" stderr writes to a file |
| 549 | "err-file": "/path/file" the file to write to |
| 550 | "err-io": "buffer" stderr appends to a buffer |
| 551 | "err-buffer": "name" buffer to append to |
| 552 | |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 553 | ============================================================================== |
| 554 | 11. Controlling a job *job-control* |
| 555 | |
| 556 | To get the status of a job: > |
| 557 | echo job_status(job) |
| 558 | |
| 559 | To make a job stop running: > |
| 560 | job_stop(job) |
| 561 | |
| 562 | This is the normal way to end a job. On Unix it sends a SIGTERM to the job. |
| 563 | It is possible to use other ways to stop the job, or even send arbitrary |
| 564 | signals. E.g. to force a job to stop, "kill it": > |
| 565 | job_stop(job, "kill") |
| 566 | |
| 567 | For more options see |job_stop()|. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 568 | |
| 569 | |
| 570 | vim:tw=78:ts=8:ft=help:norl: |