Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 1 | *channel.txt* For Vim version 7.4. Last change: 2016 Feb 05 |
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. |
| 12 | A channel uses a socket. *socket-interface* |
| 13 | |
Bram Moolenaar | 681baaf | 2016-02-04 20:57:07 +0100 | [diff] [blame] | 14 | Vim current supports up to 10 simultaneous channels. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 15 | The Netbeans interface also uses a channel. |netbeans| |
| 16 | |
| 17 | 1. Demo |channel-demo| |
| 18 | 2. Opening a channel |channel-open| |
| 19 | 3. Using a JSON channel |channel-use| |
| 20 | 4. Vim commands |channel-commands| |
| 21 | 5. Using a raw channel |channel-use| |
| 22 | 6. Job control |job-control| |
| 23 | |
| 24 | {Vi does not have any of these features} |
| 25 | {only available when compiled with the |+channel| feature} |
| 26 | |
| 27 | ============================================================================== |
| 28 | 1. Demo *channel-demo* |
| 29 | |
| 30 | This requires Python. The demo program can be found in |
| 31 | $VIMRUNTIME/tools/demoserver.py |
| 32 | Run it in one terminal. We will call this T1. |
| 33 | |
| 34 | Run Vim in another terminal. Connect to the demo server with: > |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 35 | let handle = ch_open('localhost:8765') |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 36 | |
| 37 | In T1 you should see: |
| 38 | === socket opened === ~ |
| 39 | |
| 40 | You can now send a message to the server: > |
Bram Moolenaar | 681baaf | 2016-02-04 20:57:07 +0100 | [diff] [blame] | 41 | echo ch_sendexpr(handle, 'hello!') |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 42 | |
| 43 | The message is received in T1 and a response is sent back to Vim. |
| 44 | You can see the raw messages in T1. What Vim sends is: |
| 45 | [1,"hello!"] ~ |
| 46 | And the response is: |
| 47 | [1,"got it"] ~ |
| 48 | The number will increase every time you send a message. |
| 49 | |
| 50 | 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] | 51 | the quotes): |
| 52 | ["ex","echo 'hi there'"] ~ |
| 53 | And you should see the message in Vim. You can move the cursor a word forward: |
| 54 | ["normal","w"] ~ |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 55 | |
| 56 | To handle asynchronous communication a callback needs to be used: > |
| 57 | func MyHandler(handle, msg) |
| 58 | echo "from the handler: " . a:msg |
| 59 | endfunc |
Bram Moolenaar | 681baaf | 2016-02-04 20:57:07 +0100 | [diff] [blame] | 60 | call ch_sendexpr(handle, 'hello!', "MyHandler") |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 61 | |
| 62 | Instead of giving a callback with every send call, it can also be specified |
| 63 | when opening the channel: > |
Bram Moolenaar | 681baaf | 2016-02-04 20:57:07 +0100 | [diff] [blame] | 64 | call ch_close(handle) |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 65 | let handle = ch_open('localhost:8765', {'callback': "MyHandler"}) |
Bram Moolenaar | 681baaf | 2016-02-04 20:57:07 +0100 | [diff] [blame] | 66 | call ch_sendexpr(handle, 'hello!', 0) |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 67 | |
| 68 | ============================================================================== |
| 69 | 2. Opening a channel *channel-open* |
| 70 | |
Bram Moolenaar | 681baaf | 2016-02-04 20:57:07 +0100 | [diff] [blame] | 71 | To open a channel: > |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 72 | let handle = ch_open({address} [, {argdict}]) |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 73 | |
| 74 | {address} has the form "hostname:port". E.g., "localhost:8765". |
| 75 | |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 76 | {argdict} is a dictionary with optional entries: |
| 77 | |
| 78 | "mode" can be: *channel-mode* |
| 79 | "json" - Use JSON, see below; most convenient way. Default. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 80 | "raw" - Use raw messages |
| 81 | |
| 82 | *channel-callback* |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 83 | "callback" is a function that is called when a message is received that is not |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 84 | handled otherwise. It gets two arguments: the channel handle and the received |
| 85 | message. Example: > |
| 86 | func Handle(handle, msg) |
| 87 | echo 'Received: ' . a:msg |
| 88 | endfunc |
Bram Moolenaar | 681baaf | 2016-02-04 20:57:07 +0100 | [diff] [blame] | 89 | let handle = ch_open("localhost:8765", 'json', "Handle") |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 90 | |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 91 | "waittime" is the time to wait for the connection to be made in milliseconds. |
| 92 | The default is zero, don't wait, which is useful if the server is supposed to |
| 93 | be running already. A negative number waits forever. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 94 | |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 95 | "timeout" is the time to wait for a request when blocking, using |
| 96 | ch_sendexpr(). Again in millisecons. The default si 2000 (2 seconds). |
| 97 | |
| 98 | When "mode" is "json" the "msg" argument is the body of the received message, |
| 99 | converted to Vim types. |
| 100 | When "mode" is "raw" the "msg" argument is the whole message as a string. |
| 101 | |
| 102 | When "mode" is "json" the "callback" is optional. When omitted it is only |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 103 | possible to receive a message after sending one. |
| 104 | |
| 105 | The handler can be added or changed later: > |
Bram Moolenaar | 681baaf | 2016-02-04 20:57:07 +0100 | [diff] [blame] | 106 | call ch_setcallback(handle, {callback}) |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 107 | When "callback is empty (zero or an empty string) the handler is removed. |
| 108 | NOT IMPLEMENTED YET |
| 109 | |
| 110 | The timeout can be changed later: > |
| 111 | call ch_settimeout(handle, {msec}) |
| 112 | NOT IMPLEMENTED YET |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 113 | |
| 114 | Once done with the channel, disconnect it like this: > |
Bram Moolenaar | 681baaf | 2016-02-04 20:57:07 +0100 | [diff] [blame] | 115 | call ch_close(handle) |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 116 | |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 117 | Currently up to 10 channels can be in use at the same time. *E897* |
| 118 | |
| 119 | When the channel can't be opened you will get an error message. |
| 120 | *E898* *E899* *E900* *E901* *E902* |
| 121 | |
| 122 | If there is an error reading or writing a channel it will be closed. |
| 123 | *E896* *E630* *E631* |
| 124 | |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 125 | ============================================================================== |
| 126 | 3. Using a JSON channel *channel-use* |
| 127 | |
| 128 | If {mode} is "json" then a message can be sent synchronously like this: > |
Bram Moolenaar | 681baaf | 2016-02-04 20:57:07 +0100 | [diff] [blame] | 129 | let response = ch_sendexpr(handle, {expr}) |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 130 | This awaits a response from the other side. |
| 131 | |
| 132 | To send a message, without handling a response: > |
Bram Moolenaar | 681baaf | 2016-02-04 20:57:07 +0100 | [diff] [blame] | 133 | call ch_sendexpr(handle, {expr}, 0) |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 134 | |
| 135 | To send a message and letting the response handled by a specific function, |
| 136 | asynchronously: > |
Bram Moolenaar | 681baaf | 2016-02-04 20:57:07 +0100 | [diff] [blame] | 137 | call ch_sendexpr(handle, {expr}, {callback}) |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 138 | |
| 139 | The {expr} is converted to JSON and wrapped in an array. An example of the |
| 140 | message that the receiver will get when {expr} is the string "hello": |
| 141 | [12,"hello"] ~ |
| 142 | |
| 143 | The format of the JSON sent is: |
| 144 | [{number},{expr}] |
| 145 | |
| 146 | In which {number} is different every time. It must be used in the response |
| 147 | (if any): |
| 148 | |
| 149 | [{number},{response}] |
| 150 | |
| 151 | This way Vim knows which sent message matches with which received message and |
| 152 | can call the right handler. Also when the messages arrive out of order. |
| 153 | |
| 154 | The sender must always send valid JSON to Vim. Vim can check for the end of |
| 155 | the message by parsing the JSON. It will only accept the message if the end |
| 156 | was received. |
| 157 | |
| 158 | When the process wants to send a message to Vim without first receiving a |
| 159 | message, it must use the number zero: |
| 160 | [0,{response}] |
| 161 | |
| 162 | Then channel handler will then get {response} converted to Vim types. If the |
| 163 | channel does not have a handler the message is dropped. |
| 164 | |
Bram Moolenaar | 681baaf | 2016-02-04 20:57:07 +0100 | [diff] [blame] | 165 | On read error or ch_close() the string "DETACH" is sent, if still possible. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 166 | The channel will then be inactive. |
| 167 | |
| 168 | ============================================================================== |
| 169 | 4. Vim commands *channel-commands* |
| 170 | |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 171 | PARTLY IMPLEMENTED: only "ex" and "normal" work |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 172 | |
| 173 | With a "json" channel the process can send commands to Vim that will be |
| 174 | handled by Vim internally, it does not require a handler for the channel. |
| 175 | |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 176 | Possible commands are: *E903* *E904* *E905* |
| 177 | ["redraw" {forced}] |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 178 | ["ex", {Ex command}] |
| 179 | ["normal", {Normal mode command}] |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 180 | ["eval", {expression}, {number}] |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 181 | ["expr", {expression}] |
| 182 | |
| 183 | With all of these: Be careful what these commands do! You can easily |
| 184 | interfere with what the user is doing. To avoid trouble use |mode()| to check |
| 185 | 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] | 186 | inserted as text, not executed as a command: |
| 187 | ["ex","if mode() == 'i' | call feedkeys('ClassName') | endif"] ~ |
| 188 | |
| 189 | Errors in these commands are normally not reported to avoid them messing up |
| 190 | the display. If you do want to see them, set the 'verbose' option to 3 or |
| 191 | higher. |
| 192 | |
| 193 | |
| 194 | Command "redraw" ~ |
| 195 | |
| 196 | The other commands do not update the screen, so that you can send a sequence |
| 197 | of commands without the cursor moving around. You must end with the "redraw" |
| 198 | command to show any changed text and show the cursor where it belongs. |
| 199 | |
| 200 | The argument is normally an empty string: |
| 201 | ["redraw", ""] ~ |
| 202 | To first clear the screen pass "force": |
| 203 | ["redraw", "force"] ~ |
| 204 | |
| 205 | |
| 206 | Command "ex" ~ |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 207 | |
| 208 | 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] | 209 | completion or error. You could use functions in an |autoload| script: |
| 210 | ["ex","call myscript#MyFunc(arg)"] |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 211 | |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 212 | You can also use "call |feedkeys()|" to insert any key sequence. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 213 | |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 214 | |
| 215 | Command "normal" ~ |
| 216 | |
Bram Moolenaar | 681baaf | 2016-02-04 20:57:07 +0100 | [diff] [blame] | 217 | The "normal" command is executed like with ":normal!", commands are not |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 218 | mapped. Example to open the folds under the cursor: |
| 219 | ["normal" "zO"] |
| 220 | |
| 221 | |
| 222 | Command "eval" ~ |
| 223 | |
| 224 | The "eval" command an be used to get the result of an expression. For |
| 225 | example, to get the number of lines in the current buffer: |
| 226 | ["eval","line('$')"] ~ |
| 227 | |
| 228 | it will send back the result of the expression: |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 229 | [{number}, {result}] |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 230 | Here {number} is the same as what was in the request. Use a negative number |
| 231 | to avoid confusion with message that Vim sends. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 232 | |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 233 | {result} is the result of the evaluation and is JSON encoded. If the |
| 234 | evaluation fails it is the string "ERROR". |
| 235 | |
| 236 | |
| 237 | Command "expr" ~ |
| 238 | |
| 239 | 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] | 240 | Example: |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 241 | ["expr","setline('$', ['one', 'two', 'three'])"] ~ |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 242 | |
| 243 | ============================================================================== |
| 244 | 5. Using a raw channel *channel-raw* |
| 245 | |
| 246 | If {mode} is "raw" then a message can be send like this: > |
Bram Moolenaar | 681baaf | 2016-02-04 20:57:07 +0100 | [diff] [blame] | 247 | let response = ch_sendraw(handle, {string}) |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 248 | The {string} is sent as-is. The response will be what can be read from the |
| 249 | channel right away. Since Vim doesn't know how to recognize the end of the |
| 250 | message you need to take care of it yourself. |
| 251 | |
| 252 | To send a message, without expecting a response: > |
Bram Moolenaar | 681baaf | 2016-02-04 20:57:07 +0100 | [diff] [blame] | 253 | call ch_sendraw(handle, {string}, 0) |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 254 | The process can send back a response, the channel handler will be called with |
| 255 | it. |
| 256 | |
| 257 | To send a message and letting the response handled by a specific function, |
| 258 | asynchronously: > |
Bram Moolenaar | 681baaf | 2016-02-04 20:57:07 +0100 | [diff] [blame] | 259 | call ch_sendraw(handle, {string}, {callback}) |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 260 | |
| 261 | This {string} can also be JSON, use |jsonencode()| to create it and |
| 262 | |jsondecode()| to handle a received JSON message. |
| 263 | |
| 264 | ============================================================================== |
| 265 | 6. Job control *job-control* |
| 266 | |
| 267 | NOT IMPLEMENTED YET |
| 268 | |
| 269 | To start another process: > |
| 270 | call startjob({command}) |
| 271 | |
| 272 | This does not wait for {command} to exit. |
| 273 | |
| 274 | TODO: |
| 275 | |
| 276 | let handle = startjob({command}, 's') # uses stdin/stdout |
| 277 | let handle = startjob({command}, '', {address}) # uses socket |
| 278 | let handle = startjob({command}, 'd', {address}) # start if connect fails |
| 279 | |
| 280 | |
| 281 | vim:tw=78:ts=8:ft=help:norl: |