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