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