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