blob: 0bc98f191ed8d5a7616700bd1249d7f5515553b9 [file] [log] [blame]
Bram Moolenaar4d919d72016-02-05 22:36:41 +01001*channel.txt* For Vim version 7.4. Last change: 2016 Feb 05
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01002
3
4 VIM REFERENCE MANUAL by Bram Moolenaar
5
6
7 Inter-process communication *channel*
8
9DRAFT DRAFT DRAFT DRAFT DRAFT DRAFT DRAFT DRAFT DRAFT DRAFT
10
11Vim uses channels to communicate with other processes.
12A channel uses a socket. *socket-interface*
13
Bram Moolenaar681baaf2016-02-04 20:57:07 +010014Vim current supports up to 10 simultaneous channels.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010015The Netbeans interface also uses a channel. |netbeans|
16
171. Demo |channel-demo|
182. Opening a channel |channel-open|
193. Using a JSON channel |channel-use|
204. Vim commands |channel-commands|
215. Using a raw channel |channel-use|
226. 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==============================================================================
281. Demo *channel-demo*
29
30This requires Python. The demo program can be found in
31$VIMRUNTIME/tools/demoserver.py
32Run it in one terminal. We will call this T1.
33
34Run Vim in another terminal. Connect to the demo server with: >
Bram Moolenaar4d919d72016-02-05 22:36:41 +010035 let handle = ch_open('localhost:8765')
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010036
37In T1 you should see:
38 === socket opened === ~
39
40You can now send a message to the server: >
Bram Moolenaar681baaf2016-02-04 20:57:07 +010041 echo ch_sendexpr(handle, 'hello!')
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010042
43The message is received in T1 and a response is sent back to Vim.
44You can see the raw messages in T1. What Vim sends is:
45 [1,"hello!"] ~
46And the response is:
47 [1,"got it"] ~
48The number will increase every time you send a message.
49
50The server can send a command to Vim. Type this on T1 (literally, including
Bram Moolenaarfb1f6262016-01-31 20:24:32 +010051the quotes):
52 ["ex","echo 'hi there'"] ~
53And you should see the message in Vim. You can move the cursor a word forward:
54 ["normal","w"] ~
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010055
56To handle asynchronous communication a callback needs to be used: >
57 func MyHandler(handle, msg)
58 echo "from the handler: " . a:msg
59 endfunc
Bram Moolenaar681baaf2016-02-04 20:57:07 +010060 call ch_sendexpr(handle, 'hello!', "MyHandler")
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010061
62Instead of giving a callback with every send call, it can also be specified
63when opening the channel: >
Bram Moolenaar681baaf2016-02-04 20:57:07 +010064 call ch_close(handle)
Bram Moolenaar4d919d72016-02-05 22:36:41 +010065 let handle = ch_open('localhost:8765', {'callback': "MyHandler"})
Bram Moolenaar681baaf2016-02-04 20:57:07 +010066 call ch_sendexpr(handle, 'hello!', 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010067
68==============================================================================
692. Opening a channel *channel-open*
70
Bram Moolenaar681baaf2016-02-04 20:57:07 +010071To open a channel: >
Bram Moolenaar4d919d72016-02-05 22:36:41 +010072 let handle = ch_open({address} [, {argdict}])
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010073
74{address} has the form "hostname:port". E.g., "localhost:8765".
75
Bram Moolenaar4d919d72016-02-05 22:36:41 +010076{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 Moolenaar3b5f9292016-01-28 22:37:01 +010080 "raw" - Use raw messages
81
82 *channel-callback*
Bram Moolenaar4d919d72016-02-05 22:36:41 +010083"callback" is a function that is called when a message is received that is not
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010084handled otherwise. It gets two arguments: the channel handle and the received
85message. Example: >
86 func Handle(handle, msg)
87 echo 'Received: ' . a:msg
88 endfunc
Bram Moolenaar681baaf2016-02-04 20:57:07 +010089 let handle = ch_open("localhost:8765", 'json', "Handle")
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010090
Bram Moolenaar4d919d72016-02-05 22:36:41 +010091"waittime" is the time to wait for the connection to be made in milliseconds.
92The default is zero, don't wait, which is useful if the server is supposed to
93be running already. A negative number waits forever.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010094
Bram Moolenaar4d919d72016-02-05 22:36:41 +010095"timeout" is the time to wait for a request when blocking, using
96ch_sendexpr(). Again in millisecons. The default si 2000 (2 seconds).
97
98When "mode" is "json" the "msg" argument is the body of the received message,
99converted to Vim types.
100When "mode" is "raw" the "msg" argument is the whole message as a string.
101
102When "mode" is "json" the "callback" is optional. When omitted it is only
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100103possible to receive a message after sending one.
104
105The handler can be added or changed later: >
Bram Moolenaar681baaf2016-02-04 20:57:07 +0100106 call ch_setcallback(handle, {callback})
Bram Moolenaar4d919d72016-02-05 22:36:41 +0100107When "callback is empty (zero or an empty string) the handler is removed.
108NOT IMPLEMENTED YET
109
110The timeout can be changed later: >
111 call ch_settimeout(handle, {msec})
112NOT IMPLEMENTED YET
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100113
114Once done with the channel, disconnect it like this: >
Bram Moolenaar681baaf2016-02-04 20:57:07 +0100115 call ch_close(handle)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100116
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100117Currently up to 10 channels can be in use at the same time. *E897*
118
119When the channel can't be opened you will get an error message.
120*E898* *E899* *E900* *E901* *E902*
121
122If there is an error reading or writing a channel it will be closed.
123*E896* *E630* *E631*
124
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100125==============================================================================
1263. Using a JSON channel *channel-use*
127
128If {mode} is "json" then a message can be sent synchronously like this: >
Bram Moolenaar681baaf2016-02-04 20:57:07 +0100129 let response = ch_sendexpr(handle, {expr})
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100130This awaits a response from the other side.
131
132To send a message, without handling a response: >
Bram Moolenaar681baaf2016-02-04 20:57:07 +0100133 call ch_sendexpr(handle, {expr}, 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100134
135To send a message and letting the response handled by a specific function,
136asynchronously: >
Bram Moolenaar681baaf2016-02-04 20:57:07 +0100137 call ch_sendexpr(handle, {expr}, {callback})
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100138
139The {expr} is converted to JSON and wrapped in an array. An example of the
140message that the receiver will get when {expr} is the string "hello":
141 [12,"hello"] ~
142
143The format of the JSON sent is:
144 [{number},{expr}]
145
146In which {number} is different every time. It must be used in the response
147(if any):
148
149 [{number},{response}]
150
151This way Vim knows which sent message matches with which received message and
152can call the right handler. Also when the messages arrive out of order.
153
154The sender must always send valid JSON to Vim. Vim can check for the end of
155the message by parsing the JSON. It will only accept the message if the end
156was received.
157
158When the process wants to send a message to Vim without first receiving a
159message, it must use the number zero:
160 [0,{response}]
161
162Then channel handler will then get {response} converted to Vim types. If the
163channel does not have a handler the message is dropped.
164
Bram Moolenaar681baaf2016-02-04 20:57:07 +0100165On read error or ch_close() the string "DETACH" is sent, if still possible.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100166The channel will then be inactive.
167
168==============================================================================
1694. Vim commands *channel-commands*
170
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100171PARTLY IMPLEMENTED: only "ex" and "normal" work
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100172
173With a "json" channel the process can send commands to Vim that will be
174handled by Vim internally, it does not require a handler for the channel.
175
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100176Possible commands are: *E903* *E904* *E905*
177 ["redraw" {forced}]
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100178 ["ex", {Ex command}]
179 ["normal", {Normal mode command}]
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100180 ["eval", {expression}, {number}]
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100181 ["expr", {expression}]
182
183With all of these: Be careful what these commands do! You can easily
184interfere with what the user is doing. To avoid trouble use |mode()| to check
185that the editor is in the expected state. E.g., to send keys that must be
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100186inserted as text, not executed as a command:
187 ["ex","if mode() == 'i' | call feedkeys('ClassName') | endif"] ~
188
189Errors in these commands are normally not reported to avoid them messing up
190the display. If you do want to see them, set the 'verbose' option to 3 or
191higher.
192
193
194Command "redraw" ~
195
196The other commands do not update the screen, so that you can send a sequence
197of commands without the cursor moving around. You must end with the "redraw"
198command to show any changed text and show the cursor where it belongs.
199
200The argument is normally an empty string:
201 ["redraw", ""] ~
202To first clear the screen pass "force":
203 ["redraw", "force"] ~
204
205
206Command "ex" ~
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100207
208The "ex" command is executed as any Ex command. There is no response for
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100209completion or error. You could use functions in an |autoload| script:
210 ["ex","call myscript#MyFunc(arg)"]
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100211
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100212You can also use "call |feedkeys()|" to insert any key sequence.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100213
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100214
215Command "normal" ~
216
Bram Moolenaar681baaf2016-02-04 20:57:07 +0100217The "normal" command is executed like with ":normal!", commands are not
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100218mapped. Example to open the folds under the cursor:
219 ["normal" "zO"]
220
221
222Command "eval" ~
223
224The "eval" command an be used to get the result of an expression. For
225example, to get the number of lines in the current buffer:
226 ["eval","line('$')"] ~
227
228it will send back the result of the expression:
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100229 [{number}, {result}]
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100230Here {number} is the same as what was in the request. Use a negative number
231to avoid confusion with message that Vim sends.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100232
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100233{result} is the result of the evaluation and is JSON encoded. If the
234evaluation fails it is the string "ERROR".
235
236
237Command "expr" ~
238
239The "expr" command is similar to "eval", but does not send back any response.
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100240Example:
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100241 ["expr","setline('$', ['one', 'two', 'three'])"] ~
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100242
243==============================================================================
2445. Using a raw channel *channel-raw*
245
246If {mode} is "raw" then a message can be send like this: >
Bram Moolenaar681baaf2016-02-04 20:57:07 +0100247 let response = ch_sendraw(handle, {string})
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100248The {string} is sent as-is. The response will be what can be read from the
249channel right away. Since Vim doesn't know how to recognize the end of the
250message you need to take care of it yourself.
251
252To send a message, without expecting a response: >
Bram Moolenaar681baaf2016-02-04 20:57:07 +0100253 call ch_sendraw(handle, {string}, 0)
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100254The process can send back a response, the channel handler will be called with
255it.
256
257To send a message and letting the response handled by a specific function,
258asynchronously: >
Bram Moolenaar681baaf2016-02-04 20:57:07 +0100259 call ch_sendraw(handle, {string}, {callback})
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100260
261This {string} can also be JSON, use |jsonencode()| to create it and
262|jsondecode()| to handle a received JSON message.
263
264==============================================================================
2656. Job control *job-control*
266
267NOT IMPLEMENTED YET
268
269To start another process: >
270 call startjob({command})
271
272This does not wait for {command} to exit.
273
274TODO:
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: