Bram Moolenaar | eb49041 | 2022-06-28 13:44:46 +0100 | [diff] [blame] | 1 | *channel.txt* For Vim version 9.0. Last change: 2022 Jun 23 |
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 | |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 9 | Vim uses channels to communicate with other processes. |
Bram Moolenaar | 269f595 | 2016-07-15 22:54:41 +0200 | [diff] [blame] | 10 | A channel uses a socket or pipes. *socket-interface* |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 11 | Jobs can be used to start processes and communicate with them. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 12 | The Netbeans interface also uses a channel. |netbeans| |
| 13 | |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 14 | 1. Overview |job-channel-overview| |
| 15 | 2. Channel demo |channel-demo| |
| 16 | 3. Opening a channel |channel-open| |
| 17 | 4. Using a JSON or JS channel |channel-use| |
| 18 | 5. Channel commands |channel-commands| |
| 19 | 6. Using a RAW or NL channel |channel-raw| |
| 20 | 7. More channel functions |channel-more| |
Bram Moolenaar | 5477506 | 2019-07-31 21:07:14 +0200 | [diff] [blame] | 21 | 8. Channel functions details |channel-functions-details| |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 22 | 9. Starting a job with a channel |job-start| |
| 23 | 10. Starting a job without a channel |job-start-nochannel| |
| 24 | 11. Job functions |job-functions-details| |
| 25 | 12. Job options |job-options| |
| 26 | 13. Controlling a job |job-control| |
| 27 | 14. Using a prompt buffer |prompt-buffer| |
Yegappan Lakshmanan | 3b470ae | 2022-04-16 10:41:27 +0100 | [diff] [blame] | 28 | 15. Language Server Protocol |language-server-protocol| |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 29 | |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 30 | {only when compiled with the |+channel| feature for channel stuff} |
Bram Moolenaar | f37506f | 2016-08-31 22:22:10 +0200 | [diff] [blame] | 31 | You can check this with: `has('channel')` |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 32 | {only when compiled with the |+job| feature for job stuff} |
Bram Moolenaar | f37506f | 2016-08-31 22:22:10 +0200 | [diff] [blame] | 33 | You can check this with: `has('job')` |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 34 | |
| 35 | ============================================================================== |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 36 | 1. Overview *job-channel-overview* |
| 37 | |
| 38 | There are four main types of jobs: |
Bram Moolenaar | 50ba526 | 2016-09-22 22:33:02 +0200 | [diff] [blame] | 39 | 1. A daemon, serving several Vim instances. |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 40 | Vim connects to it with a socket. |
| 41 | 2. One job working with one Vim instance, asynchronously. |
| 42 | Uses a socket or pipes. |
| 43 | 3. A job performing some work for a short time, asynchronously. |
| 44 | Uses a socket or pipes. |
| 45 | 4. Running a filter, synchronously. |
| 46 | Uses pipes. |
| 47 | |
Bram Moolenaar | 77cdfd1 | 2016-03-12 12:57:59 +0100 | [diff] [blame] | 48 | For when using sockets See |job-start|, |job-start-nochannel| and |
| 49 | |channel-open|. For 2 and 3, one or more jobs using pipes, see |job-start|. |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 50 | For 4 use the ":{range}!cmd" command, see |filter|. |
| 51 | |
| 52 | Over the socket and pipes these protocols are available: |
| 53 | RAW nothing known, Vim cannot tell where a message ends |
| 54 | NL every message ends in a NL (newline) character |
| 55 | JSON JSON encoding |json_encode()| |
| 56 | JS JavaScript style JSON-like encoding |js_encode()| |
Yegappan Lakshmanan | 9247a22 | 2022-03-30 10:16:05 +0100 | [diff] [blame] | 57 | LSP Language Server Protocol encoding |language-server-protocol| |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 58 | |
| 59 | Common combination are: |
| 60 | - Using a job connected through pipes in NL mode. E.g., to run a style |
| 61 | checker and receive errors and warnings. |
Bram Moolenaar | 7dda86f | 2018-04-20 22:36:41 +0200 | [diff] [blame] | 62 | - Using a daemon, connecting over a socket in JSON mode. E.g. to lookup |
Bram Moolenaar | 0952131 | 2016-08-12 22:54:35 +0200 | [diff] [blame] | 63 | cross-references in a database. |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 64 | |
| 65 | ============================================================================== |
Bram Moolenaar | 2685212 | 2016-05-24 20:02:38 +0200 | [diff] [blame] | 66 | 2. Channel demo *channel-demo* *demoserver.py* |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 67 | |
| 68 | This requires Python. The demo program can be found in |
| 69 | $VIMRUNTIME/tools/demoserver.py |
| 70 | Run it in one terminal. We will call this T1. |
| 71 | |
| 72 | Run Vim in another terminal. Connect to the demo server with: > |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 73 | let channel = ch_open('localhost:8765') |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 74 | |
| 75 | In T1 you should see: |
| 76 | === socket opened === ~ |
| 77 | |
| 78 | You can now send a message to the server: > |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 79 | echo ch_evalexpr(channel, 'hello!') |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 80 | |
| 81 | The message is received in T1 and a response is sent back to Vim. |
| 82 | You can see the raw messages in T1. What Vim sends is: |
| 83 | [1,"hello!"] ~ |
| 84 | And the response is: |
| 85 | [1,"got it"] ~ |
| 86 | The number will increase every time you send a message. |
| 87 | |
| 88 | 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] | 89 | the quotes): |
| 90 | ["ex","echo 'hi there'"] ~ |
| 91 | And you should see the message in Vim. You can move the cursor a word forward: |
| 92 | ["normal","w"] ~ |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 93 | |
| 94 | To handle asynchronous communication a callback needs to be used: > |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 95 | func MyHandler(channel, msg) |
Bram Moolenaar | c51cf03 | 2022-02-26 12:25:45 +0000 | [diff] [blame] | 96 | echo "from the handler: " .. a:msg |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 97 | endfunc |
Bram Moolenaar | 02e83b4 | 2016-02-21 20:10:26 +0100 | [diff] [blame] | 98 | call ch_sendexpr(channel, 'hello!', {'callback': "MyHandler"}) |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 99 | Vim will not wait for a response. Now the server can send the response later |
| 100 | and MyHandler will be invoked. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 101 | |
| 102 | Instead of giving a callback with every send call, it can also be specified |
| 103 | when opening the channel: > |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 104 | call ch_close(channel) |
| 105 | let channel = ch_open('localhost:8765', {'callback': "MyHandler"}) |
Bram Moolenaar | 4700398 | 2021-12-05 21:54:04 +0000 | [diff] [blame] | 106 | call ch_sendexpr(channel, 'hello channel!') |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 107 | |
Bram Moolenaar | 328da0d | 2016-03-04 22:22:32 +0100 | [diff] [blame] | 108 | When trying out channels it's useful to see what is going on. You can tell |
| 109 | Vim to write lines in log file: > |
| 110 | call ch_logfile('channellog', 'w') |
| 111 | See |ch_logfile()|. |
| 112 | |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 113 | ============================================================================== |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 114 | 3. Opening a channel *channel-open* |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 115 | |
Bram Moolenaar | 681baaf | 2016-02-04 20:57:07 +0100 | [diff] [blame] | 116 | To open a channel: > |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 117 | let channel = ch_open({address} [, {options}]) |
Bram Moolenaar | 02e83b4 | 2016-02-21 20:10:26 +0100 | [diff] [blame] | 118 | if ch_status(channel) == "open" |
| 119 | " use the channel |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 120 | |
| 121 | Use |ch_status()| to see if the channel could be opened. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 122 | |
LemonBoy | cc766a8 | 2022-04-04 15:46:58 +0100 | [diff] [blame] | 123 | *channel-address* |
| 124 | {address} can be a domain name or an IP address, followed by a port number, or |
| 125 | a Unix-domain socket path prefixed by "unix:". E.g. > |
| 126 | www.example.com:80 " domain + port |
| 127 | 127.0.0.1:1234 " IPv4 + port |
| 128 | [2001:db8::1]:8765 " IPv6 + port |
| 129 | unix:/tmp/my-socket " Unix-domain socket path |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 130 | |
Bram Moolenaar | 4f3f668 | 2016-03-26 23:01:59 +0100 | [diff] [blame] | 131 | {options} is a dictionary with optional entries: *channel-open-options* |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 132 | |
| 133 | "mode" can be: *channel-mode* |
| 134 | "json" - Use JSON, see below; most convenient way. Default. |
Bram Moolenaar | 910b8aa | 2016-02-16 21:03:07 +0100 | [diff] [blame] | 135 | "js" - Use JS (JavaScript) encoding, more efficient than JSON. |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 136 | "nl" - Use messages that end in a NL character |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 137 | "raw" - Use raw messages |
Yegappan Lakshmanan | 9247a22 | 2022-03-30 10:16:05 +0100 | [diff] [blame] | 138 | "lsp" - Use language server protocol encoding |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 139 | *channel-callback* *E921* |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 140 | "callback" A function that is called when a message is received that is |
Bram Moolenaar | 4700398 | 2021-12-05 21:54:04 +0000 | [diff] [blame] | 141 | not handled otherwise (e.g. a JSON message with ID zero). It |
| 142 | gets two arguments: the channel and the received message. |
| 143 | Example: > |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 144 | func Handle(channel, msg) |
Bram Moolenaar | c51cf03 | 2022-02-26 12:25:45 +0000 | [diff] [blame] | 145 | echo 'Received: ' .. a:msg |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 146 | endfunc |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 147 | let channel = ch_open("localhost:8765", {"callback": "Handle"}) |
| 148 | < |
Yegappan Lakshmanan | 9247a22 | 2022-03-30 10:16:05 +0100 | [diff] [blame] | 149 | When "mode" is "json" or "js" or "lsp" the "msg" argument is |
| 150 | the body of the received message, converted to Vim types. |
Bram Moolenaar | 02e83b4 | 2016-02-21 20:10:26 +0100 | [diff] [blame] | 151 | When "mode" is "nl" the "msg" argument is one message, |
| 152 | excluding the NL. |
| 153 | When "mode" is "raw" the "msg" argument is the whole message |
| 154 | as a string. |
Bram Moolenaar | e18c0b3 | 2016-03-20 21:08:34 +0100 | [diff] [blame] | 155 | |
| 156 | For all callbacks: Use |function()| to bind it to arguments |
Bram Moolenaar | 4f3f668 | 2016-03-26 23:01:59 +0100 | [diff] [blame] | 157 | and/or a Dictionary. Or use the form "dict.function" to bind |
| 158 | the Dictionary. |
Bram Moolenaar | 06d2d38 | 2016-05-20 17:24:11 +0200 | [diff] [blame] | 159 | |
| 160 | Callbacks are only called at a "safe" moment, usually when Vim |
| 161 | is waiting for the user to type a character. Vim does not use |
| 162 | multi-threading. |
| 163 | |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 164 | *close_cb* |
| 165 | "close_cb" A function that is called when the channel gets closed, other |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 166 | than by calling ch_close(). It should be defined like this: > |
| 167 | func MyCloseHandler(channel) |
Bram Moolenaar | 06d2d38 | 2016-05-20 17:24:11 +0200 | [diff] [blame] | 168 | < Vim will invoke callbacks that handle data before invoking |
| 169 | close_cb, thus when this function is called no more data will |
Bram Moolenaar | 68e6560 | 2019-05-26 21:33:31 +0200 | [diff] [blame] | 170 | be passed to the callbacks. However, if a callback causes Vim |
| 171 | to check for messages, the close_cb may be invoked while still |
| 172 | in the callback. The plugin must handle this somehow, it can |
| 173 | be useful to know that no more data is coming. |
Yegappan Lakshmanan | 9247a22 | 2022-03-30 10:16:05 +0100 | [diff] [blame] | 174 | If it is not known if there is a message to be read, use a |
| 175 | try/catch block: > |
| 176 | try |
| 177 | let msg = ch_readraw(a:channel) |
| 178 | catch |
| 179 | let msg = 'no message' |
| 180 | endtry |
| 181 | try |
| 182 | let err = ch_readraw(a:channel, #{part: 'err'}) |
| 183 | catch |
| 184 | let err = 'no error' |
| 185 | endtry |
| 186 | < *channel-drop* |
Bram Moolenaar | 958dc69 | 2016-12-01 15:34:12 +0100 | [diff] [blame] | 187 | "drop" Specifies when to drop messages: |
| 188 | "auto" When there is no callback to handle a message. |
| 189 | The "close_cb" is also considered for this. |
| 190 | "never" All messages will be kept. |
| 191 | |
Bram Moolenaar | 0b14688 | 2018-09-06 16:27:24 +0200 | [diff] [blame] | 192 | *channel-noblock* |
| 193 | "noblock" Same effect as |job-noblock|. Only matters for writing. |
| 194 | |
Bram Moolenaar | 06d2d38 | 2016-05-20 17:24:11 +0200 | [diff] [blame] | 195 | *waittime* |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 196 | "waittime" The time to wait for the connection to be made in |
Bram Moolenaar | f391327 | 2016-02-25 00:00:01 +0100 | [diff] [blame] | 197 | milliseconds. A negative number waits forever. |
| 198 | |
| 199 | The default is zero, don't wait, which is useful if a local |
| 200 | server is supposed to be running already. On Unix Vim |
| 201 | actually uses a 1 msec timeout, that is required on many |
| 202 | systems. Use a larger value for a remote server, e.g. 10 |
| 203 | msec at least. |
Bram Moolenaar | 4f3f668 | 2016-03-26 23:01:59 +0100 | [diff] [blame] | 204 | *channel-timeout* |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 205 | "timeout" The time to wait for a request when blocking, E.g. when using |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 206 | ch_evalexpr(). In milliseconds. The default is 2000 (2 |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 207 | seconds). |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 208 | |
Bram Moolenaar | 595e64e | 2016-02-07 19:19:53 +0100 | [diff] [blame] | 209 | When "mode" is "json" or "js" the "callback" is optional. When omitted it is |
| 210 | only possible to receive a message after sending one. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 211 | |
Bram Moolenaar | 02e83b4 | 2016-02-21 20:10:26 +0100 | [diff] [blame] | 212 | To change the channel options after opening it use |ch_setoptions()|. The |
| 213 | arguments are similar to what is passed to |ch_open()|, but "waittime" cannot |
| 214 | be given, since that only applies to opening the channel. |
Bram Moolenaar | 4d919d7 | 2016-02-05 22:36:41 +0100 | [diff] [blame] | 215 | |
Bram Moolenaar | 02e83b4 | 2016-02-21 20:10:26 +0100 | [diff] [blame] | 216 | For example, the handler can be added or changed: > |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 217 | call ch_setoptions(channel, {'callback': callback}) |
| 218 | When "callback" is empty (zero or an empty string) the handler is removed. |
| 219 | |
Bram Moolenaar | 328da0d | 2016-03-04 22:22:32 +0100 | [diff] [blame] | 220 | After a callback has been invoked Vim will update the screen and put the |
| 221 | cursor back where it belongs. Thus the callback should not need to do |
| 222 | `:redraw`. |
| 223 | |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 224 | The timeout can be changed: > |
| 225 | call ch_setoptions(channel, {'timeout': msec}) |
| 226 | < |
Bram Moolenaar | 02e83b4 | 2016-02-21 20:10:26 +0100 | [diff] [blame] | 227 | *channel-close* *E906* |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 228 | Once done with the channel, disconnect it like this: > |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 229 | call ch_close(channel) |
| 230 | When a socket is used this will close the socket for both directions. When |
| 231 | pipes are used (stdin/stdout/stderr) they are all closed. This might not be |
| 232 | what you want! Stopping the job with job_stop() might be better. |
Bram Moolenaar | 187db50 | 2016-02-27 14:44:26 +0100 | [diff] [blame] | 233 | All readahead is discarded, callbacks will no longer be invoked. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 234 | |
Bram Moolenaar | 4f3f668 | 2016-03-26 23:01:59 +0100 | [diff] [blame] | 235 | Note that a channel is closed in three stages: |
| 236 | - The I/O ends, log message: "Closing channel". There can still be queued |
| 237 | messages to read or callbacks to invoke. |
| 238 | - The readahead is cleared, log message: "Clearing channel". Some variables |
| 239 | may still reference the channel. |
| 240 | - The channel is freed, log message: "Freeing channel". |
| 241 | |
Bram Moolenaar | cbebd48 | 2016-02-07 23:02:56 +0100 | [diff] [blame] | 242 | When the channel can't be opened you will get an error message. There is a |
| 243 | difference between MS-Windows and Unix: On Unix when the port doesn't exist |
| 244 | ch_open() fails quickly. On MS-Windows "waittime" applies. |
Bram Moolenaar | aa3b15d | 2016-04-21 08:53:19 +0200 | [diff] [blame] | 245 | *E898* *E901* *E902* |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 246 | |
| 247 | If there is an error reading or writing a channel it will be closed. |
Bram Moolenaar | 664f3cf | 2019-12-07 16:03:51 +0100 | [diff] [blame] | 248 | *E630* *E631* |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 249 | |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 250 | ============================================================================== |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 251 | 4. Using a JSON or JS channel *channel-use* |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 252 | |
Bram Moolenaar | 910b8aa | 2016-02-16 21:03:07 +0100 | [diff] [blame] | 253 | If mode is JSON then a message can be sent synchronously like this: > |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 254 | let response = ch_evalexpr(channel, {expr}) |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 255 | This awaits a response from the other side. |
| 256 | |
Bram Moolenaar | 910b8aa | 2016-02-16 21:03:07 +0100 | [diff] [blame] | 257 | 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] | 258 | JavaScript encoding. See |js_encode()| for the difference. |
Bram Moolenaar | 595e64e | 2016-02-07 19:19:53 +0100 | [diff] [blame] | 259 | |
Bram Moolenaar | 02e83b4 | 2016-02-21 20:10:26 +0100 | [diff] [blame] | 260 | To send a message, without handling a response or letting the channel callback |
| 261 | handle the response: > |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 262 | call ch_sendexpr(channel, {expr}) |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 263 | |
| 264 | To send a message and letting the response handled by a specific function, |
| 265 | asynchronously: > |
Bram Moolenaar | 02e83b4 | 2016-02-21 20:10:26 +0100 | [diff] [blame] | 266 | call ch_sendexpr(channel, {expr}, {'callback': Handler}) |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 267 | |
| 268 | Vim will match the response with the request using the message ID. Once the |
| 269 | response is received the callback will be invoked. Further responses with the |
| 270 | same ID will be ignored. If your server sends back multiple responses you |
| 271 | 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] | 272 | |
| 273 | The {expr} is converted to JSON and wrapped in an array. An example of the |
| 274 | message that the receiver will get when {expr} is the string "hello": |
| 275 | [12,"hello"] ~ |
| 276 | |
| 277 | The format of the JSON sent is: |
| 278 | [{number},{expr}] |
| 279 | |
| 280 | In which {number} is different every time. It must be used in the response |
| 281 | (if any): |
| 282 | |
| 283 | [{number},{response}] |
| 284 | |
| 285 | This way Vim knows which sent message matches with which received message and |
| 286 | can call the right handler. Also when the messages arrive out of order. |
| 287 | |
Bram Moolenaar | f1f0792 | 2016-08-26 17:58:53 +0200 | [diff] [blame] | 288 | A newline character is terminating the JSON text. This can be used to |
| 289 | separate the read text. For example, in Python: |
| 290 | splitidx = read_text.find('\n') |
| 291 | message = read_text[:splitidx] |
| 292 | rest = read_text[splitidx + 1:] |
| 293 | |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 294 | The sender must always send valid JSON to Vim. Vim can check for the end of |
| 295 | the message by parsing the JSON. It will only accept the message if the end |
Bram Moolenaar | f1f0792 | 2016-08-26 17:58:53 +0200 | [diff] [blame] | 296 | was received. A newline after the message is optional. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 297 | |
| 298 | When the process wants to send a message to Vim without first receiving a |
| 299 | message, it must use the number zero: |
| 300 | [0,{response}] |
| 301 | |
| 302 | Then channel handler will then get {response} converted to Vim types. If the |
| 303 | channel does not have a handler the message is dropped. |
| 304 | |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 305 | It is also possible to use ch_sendraw() and ch_evalraw() on a JSON or JS |
| 306 | channel. The caller is then completely responsible for correct encoding and |
| 307 | decoding. |
Bram Moolenaar | cbebd48 | 2016-02-07 23:02:56 +0100 | [diff] [blame] | 308 | |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 309 | ============================================================================== |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 310 | 5. Channel commands *channel-commands* |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 311 | |
Bram Moolenaar | 910b8aa | 2016-02-16 21:03:07 +0100 | [diff] [blame] | 312 | 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] | 313 | handled by Vim internally, it does not require a handler for the channel. |
| 314 | |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 315 | Possible commands are: *E903* *E904* *E905* |
Bram Moolenaar | 220adb1 | 2016-09-12 12:17:26 +0200 | [diff] [blame] | 316 | ["redraw", {forced}] |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 317 | ["ex", {Ex command}] |
| 318 | ["normal", {Normal mode command}] |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 319 | ["expr", {expression}, {number}] |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 320 | ["expr", {expression}] |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 321 | ["call", {func name}, {argument list}, {number}] |
| 322 | ["call", {func name}, {argument list}] |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 323 | |
| 324 | With all of these: Be careful what these commands do! You can easily |
| 325 | interfere with what the user is doing. To avoid trouble use |mode()| to check |
| 326 | 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] | 327 | inserted as text, not executed as a command: |
| 328 | ["ex","if mode() == 'i' | call feedkeys('ClassName') | endif"] ~ |
| 329 | |
| 330 | Errors in these commands are normally not reported to avoid them messing up |
| 331 | the display. If you do want to see them, set the 'verbose' option to 3 or |
| 332 | higher. |
| 333 | |
| 334 | |
| 335 | Command "redraw" ~ |
| 336 | |
Bram Moolenaar | 63b74a8 | 2019-03-24 15:09:13 +0100 | [diff] [blame] | 337 | The other commands do not explicitly update the screen, so that you can send a |
| 338 | sequence of commands without the cursor moving around. A redraw can happen as |
| 339 | a side effect of some commands. You must end with the "redraw" command to |
| 340 | show any changed text and show the cursor where it belongs. |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 341 | |
| 342 | The argument is normally an empty string: |
| 343 | ["redraw", ""] ~ |
| 344 | To first clear the screen pass "force": |
| 345 | ["redraw", "force"] ~ |
| 346 | |
| 347 | |
| 348 | Command "ex" ~ |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 349 | |
| 350 | 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] | 351 | completion or error. You could use functions in an |autoload| script: |
| 352 | ["ex","call myscript#MyFunc(arg)"] |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 353 | |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 354 | You can also use "call |feedkeys()|" to insert any key sequence. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 355 | |
Bram Moolenaar | 4f3f668 | 2016-03-26 23:01:59 +0100 | [diff] [blame] | 356 | When there is an error a message is written to the channel log, if it exists, |
| 357 | and v:errmsg is set to the error. |
| 358 | |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 359 | |
| 360 | Command "normal" ~ |
| 361 | |
Bram Moolenaar | 681baaf | 2016-02-04 20:57:07 +0100 | [diff] [blame] | 362 | The "normal" command is executed like with ":normal!", commands are not |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 363 | mapped. Example to open the folds under the cursor: |
| 364 | ["normal" "zO"] |
| 365 | |
| 366 | |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 367 | Command "expr" with response ~ |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 368 | |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 369 | 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] | 370 | example, to get the number of lines in the current buffer: |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 371 | ["expr","line('$')", -2] ~ |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 372 | |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 373 | It will send back the result of the expression: |
Bram Moolenaar | e0fa374 | 2016-02-20 15:47:01 +0100 | [diff] [blame] | 374 | [-2, "last line"] ~ |
| 375 | The format is: |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 376 | [{number}, {result}] |
Bram Moolenaar | 187db50 | 2016-02-27 14:44:26 +0100 | [diff] [blame] | 377 | |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 378 | 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] | 379 | to avoid confusion with message that Vim sends. Use a different number on |
| 380 | every request to be able to match the request with the response. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 381 | |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 382 | {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] | 383 | evaluation fails or the result can't be encoded in JSON it is the string |
| 384 | "ERROR". |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 385 | |
| 386 | |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 387 | Command "expr" without a response ~ |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 388 | |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 389 | 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] | 390 | Example: |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 391 | ["expr","setline('$', ['one', 'two', 'three'])"] ~ |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 392 | There is no third argument in the request. |
| 393 | |
| 394 | |
| 395 | Command "call" ~ |
| 396 | |
| 397 | This is similar to "expr", but instead of passing the whole expression as a |
| 398 | string this passes the name of a function and a list of arguments. This |
| 399 | avoids the conversion of the arguments to a string and escaping and |
| 400 | concatenating them. Example: |
| 401 | ["call", "line", ["$"], -2] ~ |
| 402 | |
| 403 | Leave out the fourth argument if no response is to be sent: |
| 404 | ["call", "setline", ["$", ["one", "two", "three"]]] ~ |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 405 | |
| 406 | ============================================================================== |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 407 | 6. Using a RAW or NL channel *channel-raw* |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 408 | |
Bram Moolenaar | c0514bf | 2016-11-17 14:50:09 +0100 | [diff] [blame] | 409 | If mode is RAW or NL then a message can be sent like this: > |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 410 | let response = ch_evalraw(channel, {string}) |
Bram Moolenaar | 910b8aa | 2016-02-16 21:03:07 +0100 | [diff] [blame] | 411 | |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 412 | The {string} is sent as-is. The response will be what can be read from the |
| 413 | 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] | 414 | message you need to take care of it yourself. The timeout applies for reading |
| 415 | the first byte, after that it will not wait for anything more. |
| 416 | |
Bram Moolenaar | 910b8aa | 2016-02-16 21:03:07 +0100 | [diff] [blame] | 417 | 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] | 418 | to put in the NL after each message. Thus you can also send several messages |
| 419 | ending in a NL at once. The response will be the text up to and including the |
| 420 | first NL. This can also be just the NL for an empty response. |
| 421 | 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] | 422 | |
| 423 | To send a message, without expecting a response: > |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 424 | call ch_sendraw(channel, {string}) |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 425 | The process can send back a response, the channel handler will be called with |
| 426 | it. |
| 427 | |
Yegappan Lakshmanan | 3b470ae | 2022-04-16 10:41:27 +0100 | [diff] [blame] | 428 | *channel-onetime-callback* |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 429 | To send a message and letting the response handled by a specific function, |
| 430 | asynchronously: > |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 431 | call ch_sendraw(channel, {string}, {'callback': 'MyHandler'}) |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 432 | |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 433 | This {string} can also be JSON, use |json_encode()| to create it and |
| 434 | |json_decode()| to handle a received JSON message. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 435 | |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 436 | It is not possible to use |ch_evalexpr()| or |ch_sendexpr()| on a raw channel. |
Bram Moolenaar | cbebd48 | 2016-02-07 23:02:56 +0100 | [diff] [blame] | 437 | |
Bram Moolenaar | 818078d | 2016-08-27 21:58:42 +0200 | [diff] [blame] | 438 | A String in Vim cannot contain NUL bytes. To send or receive NUL bytes read |
| 439 | or write from a buffer. See |in_io-buffer| and |out_io-buffer|. |
| 440 | |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 441 | ============================================================================== |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 442 | 7. More channel functions *channel-more* |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 443 | |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 444 | To obtain the status of a channel: ch_status(channel). The possible results |
| 445 | are: |
| 446 | "fail" Failed to open the channel. |
| 447 | "open" The channel can be used. |
Bram Moolenaar | 0648142 | 2016-04-30 15:13:38 +0200 | [diff] [blame] | 448 | "buffered" The channel was closed but there is data to read. |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 449 | "closed" The channel was closed. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 450 | |
Bram Moolenaar | 187db50 | 2016-02-27 14:44:26 +0100 | [diff] [blame] | 451 | To obtain the job associated with a channel: ch_getjob(channel) |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 452 | |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 453 | To read one message from a channel: > |
| 454 | let output = ch_read(channel) |
| 455 | This uses the channel timeout. To read without a timeout, just get any |
| 456 | message that is available: > |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 457 | let output = ch_read(channel, {'timeout': 0}) |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 458 | When no message was available then the result is v:none for a JSON or JS mode |
Bram Moolenaar | 4b785f6 | 2016-11-29 21:54:44 +0100 | [diff] [blame] | 459 | channels, an empty string for a RAW or NL channel. You can use |ch_canread()| |
| 460 | to check if there is something to read. |
| 461 | |
Bram Moolenaar | 05aafed | 2017-08-11 19:12:11 +0200 | [diff] [blame] | 462 | Note that when there is no callback, messages are dropped. To avoid that add |
| 463 | a close callback to the channel. |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 464 | |
Yegappan Lakshmanan | 9247a22 | 2022-03-30 10:16:05 +0100 | [diff] [blame] | 465 | To read all normal output from a RAW channel that is available: > |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 466 | let output = ch_readraw(channel) |
Bram Moolenaar | a57b553 | 2022-06-24 11:48:03 +0100 | [diff] [blame] | 467 | To read all error output from a RAW channel that is available:: > |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 468 | let output = ch_readraw(channel, {"part": "err"}) |
Bram Moolenaar | a57b553 | 2022-06-24 11:48:03 +0100 | [diff] [blame] | 469 | Note that if the channel is in NL mode, ch_readraw() will only return one line |
| 470 | for each call. |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 471 | |
Bram Moolenaar | 4f3f668 | 2016-03-26 23:01:59 +0100 | [diff] [blame] | 472 | ch_read() and ch_readraw() use the channel timeout. When there is nothing to |
| 473 | read within that time an empty string is returned. To specify a different |
| 474 | timeout in msec use the "timeout" option: |
| 475 | {"timeout": 123} ~ |
| 476 | To read from the error output use the "part" option: |
| 477 | {"part": "err"} ~ |
| 478 | To read a message with a specific ID, on a JS or JSON channel: |
| 479 | {"id": 99} ~ |
| 480 | When no ID is specified or the ID is -1, the first message is returned. This |
| 481 | overrules any callback waiting for this message. |
| 482 | |
| 483 | For a RAW channel this returns whatever is available, since Vim does not know |
| 484 | where a message ends. |
| 485 | For a NL channel this returns one message. |
| 486 | For a JS or JSON channel this returns one decoded message. |
| 487 | This includes any sequence number. |
| 488 | |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 489 | ============================================================================== |
Bram Moolenaar | 5477506 | 2019-07-31 21:07:14 +0200 | [diff] [blame] | 490 | 8. Channel functions details *channel-functions-details* |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 491 | |
| 492 | ch_canread({handle}) *ch_canread()* |
| 493 | Return non-zero when there is something to read from {handle}. |
| 494 | {handle} can be a Channel or a Job that has a Channel. |
| 495 | |
| 496 | This is useful to read from a channel at a convenient time, |
| 497 | e.g. from a timer. |
| 498 | |
| 499 | Note that messages are dropped when the channel does not have |
| 500 | a callback. Add a close callback to avoid that. |
| 501 | |
Bram Moolenaar | 570497a | 2019-08-22 22:55:13 +0200 | [diff] [blame] | 502 | Can also be used as a |method|: > |
| 503 | GetChannel()->ch_canread() |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 504 | |
| 505 | ch_close({handle}) *ch_close()* |
| 506 | Close {handle}. See |channel-close|. |
| 507 | {handle} can be a Channel or a Job that has a Channel. |
| 508 | A close callback is not invoked. |
| 509 | |
Bram Moolenaar | 570497a | 2019-08-22 22:55:13 +0200 | [diff] [blame] | 510 | Can also be used as a |method|: > |
| 511 | GetChannel()->ch_close() |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 512 | |
| 513 | ch_close_in({handle}) *ch_close_in()* |
| 514 | Close the "in" part of {handle}. See |channel-close-in|. |
| 515 | {handle} can be a Channel or a Job that has a Channel. |
| 516 | A close callback is not invoked. |
| 517 | |
Bram Moolenaar | 570497a | 2019-08-22 22:55:13 +0200 | [diff] [blame] | 518 | Can also be used as a |method|: > |
| 519 | GetChannel()->ch_close_in() |
| 520 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 521 | |
| 522 | ch_evalexpr({handle}, {expr} [, {options}]) *ch_evalexpr()* |
| 523 | Send {expr} over {handle}. The {expr} is encoded |
| 524 | according to the type of channel. The function cannot be used |
| 525 | with a raw channel. See |channel-use|. |
| 526 | {handle} can be a Channel or a Job that has a Channel. |
Yegappan Lakshmanan | 9247a22 | 2022-03-30 10:16:05 +0100 | [diff] [blame] | 527 | When using the "lsp" channel mode, {expr} must be a |Dict|. |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 528 | *E917* |
| 529 | {options} must be a Dictionary. It must not have a "callback" |
| 530 | entry. It can have a "timeout" entry to specify the timeout |
| 531 | for this specific request. |
| 532 | |
| 533 | ch_evalexpr() waits for a response and returns the decoded |
| 534 | expression. When there is an error or timeout it returns an |
Yegappan Lakshmanan | e0805b8 | 2022-04-16 15:18:23 +0100 | [diff] [blame] | 535 | empty |String| or, when using the "lsp" channel mode, returns an |
| 536 | empty |Dict|. |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 537 | |
Bram Moolenaar | 8fe1000 | 2019-09-11 22:56:44 +0200 | [diff] [blame] | 538 | Note that while waiting for the response, Vim handles other |
| 539 | messages. You need to make sure this doesn't cause trouble. |
| 540 | |
Bram Moolenaar | 570497a | 2019-08-22 22:55:13 +0200 | [diff] [blame] | 541 | Can also be used as a |method|: > |
| 542 | GetChannel()->ch_evalexpr(expr) |
| 543 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 544 | |
| 545 | ch_evalraw({handle}, {string} [, {options}]) *ch_evalraw()* |
| 546 | Send {string} over {handle}. |
| 547 | {handle} can be a Channel or a Job that has a Channel. |
| 548 | |
| 549 | Works like |ch_evalexpr()|, but does not encode the request or |
| 550 | decode the response. The caller is responsible for the |
| 551 | correct contents. Also does not add a newline for a channel |
| 552 | in NL mode, the caller must do that. The NL in the response |
| 553 | is removed. |
| 554 | Note that Vim does not know when the text received on a raw |
| 555 | channel is complete, it may only return the first part and you |
| 556 | need to use |ch_readraw()| to fetch the rest. |
| 557 | See |channel-use|. |
| 558 | |
Bram Moolenaar | 570497a | 2019-08-22 22:55:13 +0200 | [diff] [blame] | 559 | Can also be used as a |method|: > |
| 560 | GetChannel()->ch_evalraw(rawstring) |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 561 | |
| 562 | ch_getbufnr({handle}, {what}) *ch_getbufnr()* |
Bram Moolenaar | 6aa5729 | 2021-08-14 21:25:52 +0200 | [diff] [blame] | 563 | Get the buffer number that {handle} is using for String {what}. |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 564 | {handle} can be a Channel or a Job that has a Channel. |
| 565 | {what} can be "err" for stderr, "out" for stdout or empty for |
| 566 | socket output. |
| 567 | Returns -1 when there is no buffer. |
| 568 | |
Bram Moolenaar | 570497a | 2019-08-22 22:55:13 +0200 | [diff] [blame] | 569 | Can also be used as a |method|: > |
| 570 | GetChannel()->ch_getbufnr(what) |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 571 | |
| 572 | ch_getjob({channel}) *ch_getjob()* |
| 573 | Get the Job associated with {channel}. |
| 574 | If there is no job calling |job_status()| on the returned Job |
| 575 | will result in "fail". |
| 576 | |
Bram Moolenaar | 570497a | 2019-08-22 22:55:13 +0200 | [diff] [blame] | 577 | Can also be used as a |method|: > |
| 578 | GetChannel()->ch_getjob() |
| 579 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 580 | |
| 581 | ch_info({handle}) *ch_info()* |
| 582 | Returns a Dictionary with information about {handle}. The |
| 583 | items are: |
| 584 | "id" number of the channel |
| 585 | "status" "open", "buffered" or "closed", like |
| 586 | ch_status() |
| 587 | When opened with ch_open(): |
| 588 | "hostname" the hostname of the address |
| 589 | "port" the port of the address |
LemonBoy | cc766a8 | 2022-04-04 15:46:58 +0100 | [diff] [blame] | 590 | "path" the path of the Unix-domain socket |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 591 | "sock_status" "open" or "closed" |
| 592 | "sock_mode" "NL", "RAW", "JSON" or "JS" |
| 593 | "sock_io" "socket" |
| 594 | "sock_timeout" timeout in msec |
LemonBoy | cc766a8 | 2022-04-04 15:46:58 +0100 | [diff] [blame] | 595 | |
Bram Moolenaar | cbaff5e | 2022-04-08 17:45:08 +0100 | [diff] [blame] | 596 | Note that "path" is only present for Unix-domain sockets, for |
LemonBoy | cc766a8 | 2022-04-04 15:46:58 +0100 | [diff] [blame] | 597 | regular ones "hostname" and "port" are present instead. |
| 598 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 599 | When opened with job_start(): |
| 600 | "out_status" "open", "buffered" or "closed" |
| 601 | "out_mode" "NL", "RAW", "JSON" or "JS" |
| 602 | "out_io" "null", "pipe", "file" or "buffer" |
| 603 | "out_timeout" timeout in msec |
| 604 | "err_status" "open", "buffered" or "closed" |
| 605 | "err_mode" "NL", "RAW", "JSON" or "JS" |
| 606 | "err_io" "out", "null", "pipe", "file" or "buffer" |
| 607 | "err_timeout" timeout in msec |
| 608 | "in_status" "open" or "closed" |
Yegappan Lakshmanan | 9247a22 | 2022-03-30 10:16:05 +0100 | [diff] [blame] | 609 | "in_mode" "NL", "RAW", "JSON", "JS" or "LSP" |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 610 | "in_io" "null", "pipe", "file" or "buffer" |
| 611 | "in_timeout" timeout in msec |
| 612 | |
Bram Moolenaar | 570497a | 2019-08-22 22:55:13 +0200 | [diff] [blame] | 613 | Can also be used as a |method|: > |
| 614 | GetChannel()->ch_info() |
| 615 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 616 | |
| 617 | ch_log({msg} [, {handle}]) *ch_log()* |
Bram Moolenaar | 6aa5729 | 2021-08-14 21:25:52 +0200 | [diff] [blame] | 618 | Write String {msg} in the channel log file, if it was opened |
| 619 | with |ch_logfile()|. |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 620 | When {handle} is passed the channel number is used for the |
| 621 | message. |
| 622 | {handle} can be a Channel or a Job that has a Channel. The |
| 623 | Channel must be open for the channel number to be used. |
| 624 | |
Bram Moolenaar | 570497a | 2019-08-22 22:55:13 +0200 | [diff] [blame] | 625 | Can also be used as a |method|: > |
| 626 | 'did something'->ch_log() |
| 627 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 628 | |
| 629 | ch_logfile({fname} [, {mode}]) *ch_logfile()* |
| 630 | Start logging channel activity to {fname}. |
| 631 | When {fname} is an empty string: stop logging. |
| 632 | |
Bram Moolenaar | 1d97db3 | 2022-06-04 22:15:54 +0100 | [diff] [blame] | 633 | When {mode} is omitted or contains "a" or is "o" then append |
| 634 | to the file. |
| 635 | When {mode} contains "w" and not "a" start with an empty file. |
| 636 | When {mode} contains "o" then log all terminal output. |
| 637 | Otherwise only some interesting terminal output is logged. |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 638 | |
| 639 | Use |ch_log()| to write log messages. The file is flushed |
| 640 | after every message, on Unix you can use "tail -f" to see what |
| 641 | is going on in real time. |
| 642 | |
Bram Moolenaar | 077cc7a | 2020-09-04 16:35:35 +0200 | [diff] [blame] | 643 | To enable the log very early, to see what is received from a |
Bram Moolenaar | 1d97db3 | 2022-06-04 22:15:54 +0100 | [diff] [blame] | 644 | terminal during startup, use |--log| (this uses mode "ao"): > |
Bram Moolenaar | c9a9a0a | 2022-04-12 15:09:23 +0100 | [diff] [blame] | 645 | vim --log logfile |
Bram Moolenaar | 077cc7a | 2020-09-04 16:35:35 +0200 | [diff] [blame] | 646 | < |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 647 | This function is not available in the |sandbox|. |
| 648 | NOTE: the channel communication is stored in the file, be |
| 649 | aware that this may contain confidential and privacy sensitive |
| 650 | information, e.g. a password you type in a terminal window. |
| 651 | |
Bram Moolenaar | 570497a | 2019-08-22 22:55:13 +0200 | [diff] [blame] | 652 | Can also be used as a |method|: > |
| 653 | 'logfile'->ch_logfile('w') |
| 654 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 655 | |
| 656 | ch_open({address} [, {options}]) *ch_open()* |
| 657 | Open a channel to {address}. See |channel|. |
| 658 | Returns a Channel. Use |ch_status()| to check for failure. |
| 659 | |
LemonBoy | cc766a8 | 2022-04-04 15:46:58 +0100 | [diff] [blame] | 660 | {address} is a String, see |channel-address| for the possible |
| 661 | accepted forms. |
Bram Moolenaar | bfe13cc | 2020-04-12 17:53:12 +0200 | [diff] [blame] | 662 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 663 | If {options} is given it must be a |Dictionary|. |
| 664 | See |channel-open-options|. |
| 665 | |
Bram Moolenaar | 570497a | 2019-08-22 22:55:13 +0200 | [diff] [blame] | 666 | Can also be used as a |method|: > |
| 667 | GetAddress()->ch_open() |
| 668 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 669 | |
| 670 | ch_read({handle} [, {options}]) *ch_read()* |
| 671 | Read from {handle} and return the received message. |
| 672 | {handle} can be a Channel or a Job that has a Channel. |
| 673 | For a NL channel this waits for a NL to arrive, except when |
| 674 | there is nothing more to read (channel was closed). |
| 675 | See |channel-more|. |
| 676 | |
Bram Moolenaar | 570497a | 2019-08-22 22:55:13 +0200 | [diff] [blame] | 677 | Can also be used as a |method|: > |
| 678 | GetChannel()->ch_read() |
| 679 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 680 | |
| 681 | ch_readblob({handle} [, {options}]) *ch_readblob()* |
| 682 | Like ch_read() but reads binary data and returns a |Blob|. |
| 683 | See |channel-more|. |
| 684 | |
Bram Moolenaar | 570497a | 2019-08-22 22:55:13 +0200 | [diff] [blame] | 685 | Can also be used as a |method|: > |
| 686 | GetChannel()->ch_readblob() |
| 687 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 688 | |
| 689 | ch_readraw({handle} [, {options}]) *ch_readraw()* |
| 690 | Like ch_read() but for a JS and JSON channel does not decode |
| 691 | the message. For a NL channel it does not block waiting for |
| 692 | the NL to arrive, but otherwise works like ch_read(). |
| 693 | See |channel-more|. |
| 694 | |
Bram Moolenaar | 570497a | 2019-08-22 22:55:13 +0200 | [diff] [blame] | 695 | Can also be used as a |method|: > |
| 696 | GetChannel()->ch_readraw() |
| 697 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 698 | |
| 699 | ch_sendexpr({handle}, {expr} [, {options}]) *ch_sendexpr()* |
| 700 | Send {expr} over {handle}. The {expr} is encoded |
| 701 | according to the type of channel. The function cannot be used |
| 702 | with a raw channel. |
| 703 | See |channel-use|. *E912* |
| 704 | {handle} can be a Channel or a Job that has a Channel. |
Yegappan Lakshmanan | 9247a22 | 2022-03-30 10:16:05 +0100 | [diff] [blame] | 705 | When using the "lsp" channel mode, {expr} must be a |Dict|. |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 706 | |
Yegappan Lakshmanan | 3b470ae | 2022-04-16 10:41:27 +0100 | [diff] [blame] | 707 | If the channel mode is "lsp", then returns a Dict. Otherwise |
| 708 | returns an empty String. If the "callback" item is present in |
| 709 | {options}, then the returned Dict contains the ID of the |
| 710 | request message. The ID can be used to send a cancellation |
Yegappan Lakshmanan | e0805b8 | 2022-04-16 15:18:23 +0100 | [diff] [blame] | 711 | request to the LSP server (if needed). Returns an empty Dict |
| 712 | on error. |
Yegappan Lakshmanan | 3b470ae | 2022-04-16 10:41:27 +0100 | [diff] [blame] | 713 | |
| 714 | If a response message is not expected for {expr}, then don't |
| 715 | specify the "callback" item in {options}. |
| 716 | |
Bram Moolenaar | 570497a | 2019-08-22 22:55:13 +0200 | [diff] [blame] | 717 | Can also be used as a |method|: > |
| 718 | GetChannel()->ch_sendexpr(expr) |
| 719 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 720 | |
| 721 | ch_sendraw({handle}, {expr} [, {options}]) *ch_sendraw()* |
| 722 | Send |String| or |Blob| {expr} over {handle}. |
| 723 | Works like |ch_sendexpr()|, but does not encode the request or |
| 724 | decode the response. The caller is responsible for the |
| 725 | correct contents. Also does not add a newline for a channel |
| 726 | in NL mode, the caller must do that. The NL in the response |
| 727 | is removed. |
| 728 | See |channel-use|. |
| 729 | |
Bram Moolenaar | 570497a | 2019-08-22 22:55:13 +0200 | [diff] [blame] | 730 | Can also be used as a |method|: > |
| 731 | GetChannel()->ch_sendraw(rawexpr) |
| 732 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 733 | |
| 734 | ch_setoptions({handle}, {options}) *ch_setoptions()* |
| 735 | Set options on {handle}: |
| 736 | "callback" the channel callback |
| 737 | "timeout" default read timeout in msec |
| 738 | "mode" mode for the whole channel |
| 739 | See |ch_open()| for more explanation. |
| 740 | {handle} can be a Channel or a Job that has a Channel. |
| 741 | |
| 742 | Note that changing the mode may cause queued messages to be |
| 743 | lost. |
| 744 | |
| 745 | These options cannot be changed: |
| 746 | "waittime" only applies to |ch_open()| |
| 747 | |
Bram Moolenaar | 570497a | 2019-08-22 22:55:13 +0200 | [diff] [blame] | 748 | Can also be used as a |method|: > |
| 749 | GetChannel()->ch_setoptions(options) |
| 750 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 751 | |
| 752 | ch_status({handle} [, {options}]) *ch_status()* |
| 753 | Return the status of {handle}: |
| 754 | "fail" failed to open the channel |
| 755 | "open" channel can be used |
| 756 | "buffered" channel can be read, not written to |
| 757 | "closed" channel can not be used |
| 758 | {handle} can be a Channel or a Job that has a Channel. |
| 759 | "buffered" is used when the channel was closed but there is |
| 760 | still data that can be obtained with |ch_read()|. |
| 761 | |
| 762 | If {options} is given it can contain a "part" entry to specify |
| 763 | the part of the channel to return the status for: "out" or |
| 764 | "err". For example, to get the error status: > |
| 765 | ch_status(job, {"part": "err"}) |
| 766 | < |
Bram Moolenaar | 570497a | 2019-08-22 22:55:13 +0200 | [diff] [blame] | 767 | Can also be used as a |method|: > |
| 768 | GetChannel()->ch_status() |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 769 | |
| 770 | ============================================================================== |
| 771 | 9. Starting a job with a channel *job-start* *job* |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 772 | |
| 773 | To start a job and open a channel for stdin/stdout/stderr: > |
| 774 | let job = job_start(command, {options}) |
| 775 | |
| 776 | You can get the channel with: > |
| 777 | let channel = job_getchannel(job) |
| 778 | |
| 779 | The channel will use NL mode. If you want another mode it's best to specify |
| 780 | this in {options}. When changing the mode later some text may have already |
| 781 | been received and not parsed correctly. |
| 782 | |
| 783 | If the command produces a line of output that you want to deal with, specify |
| 784 | a handler for stdout: > |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 785 | let job = job_start(command, {"out_cb": "MyHandler"}) |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 786 | The function will be called with the channel and a message. You would define |
| 787 | it like this: > |
| 788 | func MyHandler(channel, msg) |
| 789 | |
Bram Moolenaar | 02e83b4 | 2016-02-21 20:10:26 +0100 | [diff] [blame] | 790 | Without the handler you need to read the output with |ch_read()| or |
Bram Moolenaar | 0648142 | 2016-04-30 15:13:38 +0200 | [diff] [blame] | 791 | |ch_readraw()|. You can do this in the close callback, see |read-in-close-cb|. |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 792 | |
Bram Moolenaar | 1ccd8ff | 2017-08-11 19:50:37 +0200 | [diff] [blame] | 793 | Note that if the job exits before you read the output, the output may be lost. |
| 794 | This depends on the system (on Unix this happens because closing the write end |
| 795 | of a pipe causes the read end to get EOF). To avoid this make the job sleep |
| 796 | for a short while before it exits. |
| 797 | |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 798 | The handler defined for "out_cb" will not receive stderr. If you want to |
| 799 | handle that separately, add an "err_cb" handler: > |
| 800 | let job = job_start(command, {"out_cb": "MyHandler", |
| 801 | \ "err_cb": "ErrHandler"}) |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 802 | |
Bram Moolenaar | 02e83b4 | 2016-02-21 20:10:26 +0100 | [diff] [blame] | 803 | If you want to handle both stderr and stdout with one handler use the |
| 804 | "callback" option: > |
Bram Moolenaar | 664f3cf | 2019-12-07 16:03:51 +0100 | [diff] [blame] | 805 | let job = job_start(command, {"callback": "MyHandler"}) |
Bram Moolenaar | 02e83b4 | 2016-02-21 20:10:26 +0100 | [diff] [blame] | 806 | |
Bram Moolenaar | 3ec574f | 2017-06-13 18:12:01 +0200 | [diff] [blame] | 807 | Depending on the system, starting a job can put Vim in the background, the |
| 808 | started job gets the focus. To avoid that, use the `foreground()` function. |
| 809 | This might not always work when called early, put in the callback handler or |
| 810 | use a timer to call it after the job has started. |
| 811 | |
Bram Moolenaar | 8b1862a | 2016-02-27 19:21:24 +0100 | [diff] [blame] | 812 | You can send a message to the command with ch_evalraw(). If the channel is in |
| 813 | JSON or JS mode you can use ch_evalexpr(). |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 814 | |
| 815 | There are several options you can use, see |job-options|. |
Bram Moolenaar | 187db50 | 2016-02-27 14:44:26 +0100 | [diff] [blame] | 816 | For example, to start a job and write its output in buffer "dummy": > |
| 817 | let logjob = job_start("tail -f /tmp/log", |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 818 | \ {'out_io': 'buffer', 'out_name': 'dummy'}) |
Bram Moolenaar | 187db50 | 2016-02-27 14:44:26 +0100 | [diff] [blame] | 819 | sbuf dummy |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 820 | |
Bram Moolenaar | 5f148ec | 2016-03-07 22:59:26 +0100 | [diff] [blame] | 821 | |
| 822 | Job input from a buffer ~ |
Bram Moolenaar | 818078d | 2016-08-27 21:58:42 +0200 | [diff] [blame] | 823 | *in_io-buffer* |
Bram Moolenaar | 328da0d | 2016-03-04 22:22:32 +0100 | [diff] [blame] | 824 | To run a job that reads from a buffer: > |
| 825 | let job = job_start({command}, |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 826 | \ {'in_io': 'buffer', 'in_name': 'mybuffer'}) |
Bram Moolenaar | 328da0d | 2016-03-04 22:22:32 +0100 | [diff] [blame] | 827 | < |
| 828 | *E915* *E918* |
| 829 | The buffer is found by name, similar to |bufnr()|. The buffer must exist and |
| 830 | be loaded when job_start() is called. |
| 831 | |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 832 | By default this reads the whole buffer. This can be changed with the "in_top" |
| 833 | and "in_bot" options. |
Bram Moolenaar | 328da0d | 2016-03-04 22:22:32 +0100 | [diff] [blame] | 834 | |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 835 | A special mode is when "in_top" is set to zero and "in_bot" is not set: Every |
Bram Moolenaar | 74675a6 | 2017-07-15 13:53:23 +0200 | [diff] [blame] | 836 | time a line is added to the buffer, the last-but-one line will be sent to the |
Bram Moolenaar | 5f148ec | 2016-03-07 22:59:26 +0100 | [diff] [blame] | 837 | job stdin. This allows for editing the last line and sending it when pressing |
| 838 | Enter. |
Bram Moolenaar | 0874a83 | 2016-09-01 15:11:51 +0200 | [diff] [blame] | 839 | *channel-close-in* |
| 840 | When not using the special mode the pipe or socket will be closed after the |
| 841 | last line has been written. This signals the reading end that the input |
| 842 | finished. You can also use |ch_close_in()| to close it sooner. |
Bram Moolenaar | 328da0d | 2016-03-04 22:22:32 +0100 | [diff] [blame] | 843 | |
Bram Moolenaar | 063b9d1 | 2016-07-09 20:21:48 +0200 | [diff] [blame] | 844 | NUL bytes in the text will be passed to the job (internally Vim stores these |
| 845 | as NL bytes). |
| 846 | |
Bram Moolenaar | 0648142 | 2016-04-30 15:13:38 +0200 | [diff] [blame] | 847 | |
| 848 | Reading job output in the close callback ~ |
| 849 | *read-in-close-cb* |
| 850 | If the job can take some time and you don't need intermediate results, you can |
| 851 | add a close callback and read the output there: > |
| 852 | |
| 853 | func! CloseHandler(channel) |
Bram Moolenaar | 2ec618c | 2016-10-01 14:47:05 +0200 | [diff] [blame] | 854 | while ch_status(a:channel, {'part': 'out'}) == 'buffered' |
Bram Moolenaar | 0648142 | 2016-04-30 15:13:38 +0200 | [diff] [blame] | 855 | echomsg ch_read(a:channel) |
| 856 | endwhile |
| 857 | endfunc |
| 858 | let job = job_start(command, {'close_cb': 'CloseHandler'}) |
| 859 | |
| 860 | You will want to do something more useful than "echomsg". |
| 861 | |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 862 | ============================================================================== |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 863 | 10. Starting a job without a channel *job-start-nochannel* |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 864 | |
| 865 | To start another process without creating a channel: > |
Bram Moolenaar | 328da0d | 2016-03-04 22:22:32 +0100 | [diff] [blame] | 866 | let job = job_start(command, |
Bram Moolenaar | 5162822 | 2016-12-01 23:03:28 +0100 | [diff] [blame] | 867 | \ {"in_io": "null", "out_io": "null", "err_io": "null"}) |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 868 | |
| 869 | This starts {command} in the background, Vim does not wait for it to finish. |
| 870 | |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 871 | When Vim sees that neither stdin, stdout or stderr are connected, no channel |
| 872 | will be created. Often you will want to include redirection in the command to |
| 873 | avoid it getting stuck. |
| 874 | |
| 875 | There are several options you can use, see |job-options|. |
| 876 | |
Bram Moolenaar | 77cdfd1 | 2016-03-12 12:57:59 +0100 | [diff] [blame] | 877 | *job-start-if-needed* |
| 878 | To start a job only when connecting to an address does not work, do something |
| 879 | like this: > |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 880 | let channel = ch_open(address, {"waittime": 0}) |
| 881 | if ch_status(channel) == "fail" |
| 882 | let job = job_start(command) |
| 883 | let channel = ch_open(address, {"waittime": 1000}) |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 884 | endif |
Bram Moolenaar | 77cdfd1 | 2016-03-12 12:57:59 +0100 | [diff] [blame] | 885 | |
| 886 | Note that the waittime for ch_open() gives the job one second to make the port |
| 887 | available. |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 888 | |
| 889 | ============================================================================== |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 890 | 11. Job functions *job-functions-details* |
| 891 | |
| 892 | job_getchannel({job}) *job_getchannel()* |
| 893 | Get the channel handle that {job} is using. |
| 894 | To check if the job has no channel: > |
Yegappan Lakshmanan | 1a71d31 | 2021-07-15 12:49:58 +0200 | [diff] [blame] | 895 | if string(job_getchannel(job)) == 'channel fail' |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 896 | < |
Bram Moolenaar | 570497a | 2019-08-22 22:55:13 +0200 | [diff] [blame] | 897 | Can also be used as a |method|: > |
| 898 | GetJob()->job_getchannel() |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 899 | |
| 900 | job_info([{job}]) *job_info()* |
| 901 | Returns a Dictionary with information about {job}: |
| 902 | "status" what |job_status()| returns |
| 903 | "channel" what |job_getchannel()| returns |
| 904 | "cmd" List of command arguments used to start the job |
| 905 | "process" process ID |
| 906 | "tty_in" terminal input name, empty when none |
| 907 | "tty_out" terminal output name, empty when none |
| 908 | "exitval" only valid when "status" is "dead" |
| 909 | "exit_cb" function to be called on exit |
| 910 | "stoponexit" |job-stoponexit| |
| 911 | |
| 912 | Only in Unix: |
| 913 | "termsig" the signal which terminated the process |
| 914 | (See |job_stop()| for the values) |
| 915 | only valid when "status" is "dead" |
| 916 | |
| 917 | Only in MS-Windows: |
| 918 | "tty_type" Type of virtual console in use. |
| 919 | Values are "winpty" or "conpty". |
| 920 | See 'termwintype'. |
| 921 | |
| 922 | Without any arguments, returns a List with all Job objects. |
| 923 | |
Bram Moolenaar | 570497a | 2019-08-22 22:55:13 +0200 | [diff] [blame] | 924 | Can also be used as a |method|: > |
| 925 | GetJob()->job_info() |
| 926 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 927 | |
| 928 | job_setoptions({job}, {options}) *job_setoptions()* |
| 929 | Change options for {job}. Supported are: |
| 930 | "stoponexit" |job-stoponexit| |
| 931 | "exit_cb" |job-exit_cb| |
| 932 | |
Bram Moolenaar | 570497a | 2019-08-22 22:55:13 +0200 | [diff] [blame] | 933 | Can also be used as a |method|: > |
| 934 | GetJob()->job_setoptions(options) |
| 935 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 936 | |
| 937 | job_start({command} [, {options}]) *job_start()* |
| 938 | Start a job and return a Job object. Unlike |system()| and |
| 939 | |:!cmd| this does not wait for the job to finish. |
| 940 | To start a job in a terminal window see |term_start()|. |
| 941 | |
| 942 | If the job fails to start then |job_status()| on the returned |
| 943 | Job object results in "fail" and none of the callbacks will be |
| 944 | invoked. |
| 945 | |
| 946 | {command} can be a String. This works best on MS-Windows. On |
| 947 | Unix it is split up in white-separated parts to be passed to |
| 948 | execvp(). Arguments in double quotes can contain white space. |
| 949 | |
| 950 | {command} can be a List, where the first item is the executable |
| 951 | and further items are the arguments. All items are converted |
| 952 | to String. This works best on Unix. |
| 953 | |
| 954 | On MS-Windows, job_start() makes a GUI application hidden. If |
| 955 | want to show it, Use |:!start| instead. |
| 956 | |
| 957 | The command is executed directly, not through a shell, the |
| 958 | 'shell' option is not used. To use the shell: > |
| 959 | let job = job_start(["/bin/sh", "-c", "echo hello"]) |
| 960 | < Or: > |
| 961 | let job = job_start('/bin/sh -c "echo hello"') |
| 962 | < Note that this will start two processes, the shell and the |
| 963 | command it executes. If you don't want this use the "exec" |
| 964 | shell command. |
| 965 | |
| 966 | On Unix $PATH is used to search for the executable only when |
| 967 | the command does not contain a slash. |
| 968 | |
| 969 | The job will use the same terminal as Vim. If it reads from |
| 970 | stdin the job and Vim will be fighting over input, that |
| 971 | doesn't work. Redirect stdin and stdout to avoid problems: > |
| 972 | let job = job_start(['sh', '-c', "myserver </dev/null >/dev/null"]) |
| 973 | < |
| 974 | The returned Job object can be used to get the status with |
| 975 | |job_status()| and stop the job with |job_stop()|. |
| 976 | |
| 977 | Note that the job object will be deleted if there are no |
| 978 | references to it. This closes the stdin and stderr, which may |
| 979 | cause the job to fail with an error. To avoid this keep a |
| 980 | reference to the job. Thus instead of: > |
| 981 | call job_start('my-command') |
| 982 | < use: > |
| 983 | let myjob = job_start('my-command') |
| 984 | < and unlet "myjob" once the job is not needed or is past the |
| 985 | point where it would fail (e.g. when it prints a message on |
| 986 | startup). Keep in mind that variables local to a function |
| 987 | will cease to exist if the function returns. Use a |
| 988 | script-local variable if needed: > |
| 989 | let s:myjob = job_start('my-command') |
| 990 | < |
| 991 | {options} must be a Dictionary. It can contain many optional |
| 992 | items, see |job-options|. |
| 993 | |
Bram Moolenaar | 570497a | 2019-08-22 22:55:13 +0200 | [diff] [blame] | 994 | Can also be used as a |method|: > |
| 995 | BuildCommand()->job_start() |
| 996 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 997 | |
| 998 | job_status({job}) *job_status()* *E916* |
| 999 | Returns a String with the status of {job}: |
| 1000 | "run" job is running |
| 1001 | "fail" job failed to start |
| 1002 | "dead" job died or was stopped after running |
| 1003 | |
| 1004 | On Unix a non-existing command results in "dead" instead of |
| 1005 | "fail", because a fork happens before the failure can be |
| 1006 | detected. |
| 1007 | |
Bram Moolenaar | cb80aa2 | 2020-10-26 21:12:46 +0100 | [diff] [blame] | 1008 | If in Vim9 script a variable is declared with type "job" but |
| 1009 | never assigned to, passing that variable to job_status() |
| 1010 | returns "fail". |
| 1011 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 1012 | If an exit callback was set with the "exit_cb" option and the |
| 1013 | job is now detected to be "dead" the callback will be invoked. |
| 1014 | |
| 1015 | For more information see |job_info()|. |
| 1016 | |
Bram Moolenaar | 570497a | 2019-08-22 22:55:13 +0200 | [diff] [blame] | 1017 | Can also be used as a |method|: > |
| 1018 | GetJob()->job_status() |
| 1019 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 1020 | |
| 1021 | job_stop({job} [, {how}]) *job_stop()* |
| 1022 | Stop the {job}. This can also be used to signal the job. |
| 1023 | |
| 1024 | When {how} is omitted or is "term" the job will be terminated. |
| 1025 | For Unix SIGTERM is sent. On MS-Windows the job will be |
| 1026 | terminated forcedly (there is no "gentle" way). |
| 1027 | This goes to the process group, thus children may also be |
| 1028 | affected. |
| 1029 | |
| 1030 | Effect for Unix: |
| 1031 | "term" SIGTERM (default) |
| 1032 | "hup" SIGHUP |
| 1033 | "quit" SIGQUIT |
| 1034 | "int" SIGINT |
| 1035 | "kill" SIGKILL (strongest way to stop) |
| 1036 | number signal with that number |
| 1037 | |
| 1038 | Effect for MS-Windows: |
| 1039 | "term" terminate process forcedly (default) |
| 1040 | "hup" CTRL_BREAK |
| 1041 | "quit" CTRL_BREAK |
| 1042 | "int" CTRL_C |
| 1043 | "kill" terminate process forcedly |
| 1044 | Others CTRL_BREAK |
| 1045 | |
| 1046 | On Unix the signal is sent to the process group. This means |
| 1047 | that when the job is "sh -c command" it affects both the shell |
| 1048 | and the command. |
| 1049 | |
| 1050 | The result is a Number: 1 if the operation could be executed, |
| 1051 | 0 if "how" is not supported on the system. |
| 1052 | Note that even when the operation was executed, whether the |
| 1053 | job was actually stopped needs to be checked with |
| 1054 | |job_status()|. |
| 1055 | |
| 1056 | If the status of the job is "dead", the signal will not be |
| 1057 | sent. This is to avoid to stop the wrong job (esp. on Unix, |
| 1058 | where process numbers are recycled). |
| 1059 | |
| 1060 | When using "kill" Vim will assume the job will die and close |
| 1061 | the channel. |
| 1062 | |
Bram Moolenaar | 570497a | 2019-08-22 22:55:13 +0200 | [diff] [blame] | 1063 | Can also be used as a |method|: > |
| 1064 | GetJob()->job_stop() |
| 1065 | |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 1066 | |
| 1067 | ============================================================================== |
| 1068 | 12. Job options *job-options* |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 1069 | |
| 1070 | The {options} argument in job_start() is a dictionary. All entries are |
Bram Moolenaar | 02e83b4 | 2016-02-21 20:10:26 +0100 | [diff] [blame] | 1071 | optional. Some options can be used after the job has started, using |
| 1072 | job_setoptions(job, {options}). Many options can be used with the channel |
| 1073 | related to the job, using ch_setoptions(channel, {options}). |
| 1074 | See |job_setoptions()| and |ch_setoptions()|. |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 1075 | |
Bram Moolenaar | 4f3f668 | 2016-03-26 23:01:59 +0100 | [diff] [blame] | 1076 | *in_mode* *out_mode* *err_mode* |
| 1077 | "in_mode" mode specifically for stdin, only when using pipes |
| 1078 | "out_mode" mode specifically for stdout, only when using pipes |
| 1079 | "err_mode" mode specifically for stderr, only when using pipes |
| 1080 | See |channel-mode| for the values. |
| 1081 | |
| 1082 | Note: when setting "mode" the part specific mode is |
| 1083 | overwritten. Therefore set "mode" first and the part |
| 1084 | specific mode later. |
| 1085 | |
| 1086 | Note: when writing to a file or buffer and when |
| 1087 | reading from a buffer NL mode is used by default. |
| 1088 | |
Bram Moolenaar | 0b14688 | 2018-09-06 16:27:24 +0200 | [diff] [blame] | 1089 | *job-noblock* |
| 1090 | "noblock": 1 When writing use a non-blocking write call. This |
| 1091 | avoids getting stuck if Vim should handle other |
| 1092 | messages in between, e.g. when a job sends back data |
| 1093 | to Vim. It implies that when `ch_sendraw()` returns |
| 1094 | not all data may have been written yet. |
| 1095 | This option was added in patch 8.1.0350, test with: > |
| 1096 | if has("patch-8.1.350") |
| 1097 | let options['noblock'] = 1 |
| 1098 | endif |
| 1099 | < |
Bram Moolenaar | decb14d | 2016-02-20 23:32:02 +0100 | [diff] [blame] | 1100 | *job-callback* |
| 1101 | "callback": handler Callback for something to read on any part of the |
| 1102 | channel. |
Bram Moolenaar | 4f3f668 | 2016-03-26 23:01:59 +0100 | [diff] [blame] | 1103 | *job-out_cb* *out_cb* |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 1104 | "out_cb": handler Callback for when there is something to read on |
Bram Moolenaar | 4f3f668 | 2016-03-26 23:01:59 +0100 | [diff] [blame] | 1105 | stdout. Only for when the channel uses pipes. When |
| 1106 | "out_cb" wasn't set the channel callback is used. |
Bram Moolenaar | 269f595 | 2016-07-15 22:54:41 +0200 | [diff] [blame] | 1107 | The two arguments are the channel and the message. |
Bram Moolenaar | 4f3f668 | 2016-03-26 23:01:59 +0100 | [diff] [blame] | 1108 | |
| 1109 | *job-err_cb* *err_cb* |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 1110 | "err_cb": handler Callback for when there is something to read on |
Bram Moolenaar | 4f3f668 | 2016-03-26 23:01:59 +0100 | [diff] [blame] | 1111 | stderr. Only for when the channel uses pipes. When |
| 1112 | "err_cb" wasn't set the channel callback is used. |
Bram Moolenaar | 269f595 | 2016-07-15 22:54:41 +0200 | [diff] [blame] | 1113 | The two arguments are the channel and the message. |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 1114 | *job-close_cb* |
| 1115 | "close_cb": handler Callback for when the channel is closed. Same as |
Bram Moolenaar | 82af871 | 2016-06-04 20:20:29 +0200 | [diff] [blame] | 1116 | "close_cb" on |ch_open()|, see |close_cb|. |
Bram Moolenaar | bc2eada | 2017-01-02 21:27:47 +0100 | [diff] [blame] | 1117 | *job-drop* |
Bram Moolenaar | b6e0ec6 | 2017-07-23 22:12:20 +0200 | [diff] [blame] | 1118 | "drop": when Specifies when to drop messages. Same as "drop" on |
Bram Moolenaar | 5162822 | 2016-12-01 23:03:28 +0100 | [diff] [blame] | 1119 | |ch_open()|, see |channel-drop|. For "auto" the |
| 1120 | exit_cb is not considered. |
Bram Moolenaar | bc2eada | 2017-01-02 21:27:47 +0100 | [diff] [blame] | 1121 | *job-exit_cb* |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 1122 | "exit_cb": handler Callback for when the job ends. The arguments are the |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 1123 | job and the exit status. |
Bram Moolenaar | b4ada79 | 2016-10-30 21:55:26 +0100 | [diff] [blame] | 1124 | Vim checks up to 10 times per second for jobs that |
| 1125 | ended. The check can also be triggered by calling |
| 1126 | |job_status()|, which may then invoke the exit_cb |
| 1127 | handler. |
Bram Moolenaar | 06d2d38 | 2016-05-20 17:24:11 +0200 | [diff] [blame] | 1128 | Note that data can be buffered, callbacks may still be |
| 1129 | called after the process ends. |
Bram Moolenaar | 4f3f668 | 2016-03-26 23:01:59 +0100 | [diff] [blame] | 1130 | *job-timeout* |
Bram Moolenaar | b6e0ec6 | 2017-07-23 22:12:20 +0200 | [diff] [blame] | 1131 | "timeout": time The time to wait for a request when blocking, E.g. |
Bram Moolenaar | 4f3f668 | 2016-03-26 23:01:59 +0100 | [diff] [blame] | 1132 | when using ch_evalexpr(). In milliseconds. The |
| 1133 | default is 2000 (2 seconds). |
| 1134 | *out_timeout* *err_timeout* |
Bram Moolenaar | b6e0ec6 | 2017-07-23 22:12:20 +0200 | [diff] [blame] | 1135 | "out_timeout": time Timeout for stdout. Only when using pipes. |
| 1136 | "err_timeout": time Timeout for stderr. Only when using pipes. |
Bram Moolenaar | 4f3f668 | 2016-03-26 23:01:59 +0100 | [diff] [blame] | 1137 | Note: when setting "timeout" the part specific mode is |
| 1138 | overwritten. Therefore set "timeout" first and the |
| 1139 | part specific mode later. |
| 1140 | |
Bram Moolenaar | 02e83b4 | 2016-02-21 20:10:26 +0100 | [diff] [blame] | 1141 | *job-stoponexit* |
| 1142 | "stoponexit": {signal} Send {signal} to the job when Vim exits. See |
| 1143 | |job_stop()| for possible values. |
| 1144 | "stoponexit": "" Do not stop the job when Vim exits. |
| 1145 | The default is "term". |
| 1146 | |
Bram Moolenaar | 77cdfd1 | 2016-03-12 12:57:59 +0100 | [diff] [blame] | 1147 | *job-term* |
Bram Moolenaar | b6e0ec6 | 2017-07-23 22:12:20 +0200 | [diff] [blame] | 1148 | "term": "open" Start a terminal in a new window and connect the job |
| 1149 | stdin/stdout/stderr to it. Similar to using |
| 1150 | `:terminal`. |
Bram Moolenaar | 77cdfd1 | 2016-03-12 12:57:59 +0100 | [diff] [blame] | 1151 | NOTE: Not implemented yet! |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 1152 | |
Bram Moolenaar | 77cdfd1 | 2016-03-12 12:57:59 +0100 | [diff] [blame] | 1153 | "channel": {channel} Use an existing channel instead of creating a new one. |
| 1154 | The parts of the channel that get used for the new job |
| 1155 | will be disconnected from what they were used before. |
Bram Moolenaar | 5162822 | 2016-12-01 23:03:28 +0100 | [diff] [blame] | 1156 | If the channel was still used by another job this may |
Bram Moolenaar | 77cdfd1 | 2016-03-12 12:57:59 +0100 | [diff] [blame] | 1157 | cause I/O errors. |
| 1158 | Existing callbacks and other settings remain. |
| 1159 | |
Bram Moolenaar | b6e0ec6 | 2017-07-23 22:12:20 +0200 | [diff] [blame] | 1160 | "pty": 1 Use a pty (pseudo-tty) instead of a pipe when |
| 1161 | possible. This is most useful in combination with a |
| 1162 | terminal window, see |terminal|. |
| 1163 | {only on Unix and Unix-like systems} |
| 1164 | |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 1165 | *job-in_io* *in_top* *in_bot* *in_name* *in_buf* |
| 1166 | "in_io": "null" disconnect stdin (read from /dev/null) |
| 1167 | "in_io": "pipe" stdin is connected to the channel (default) |
| 1168 | "in_io": "file" stdin reads from a file |
| 1169 | "in_io": "buffer" stdin reads from a buffer |
| 1170 | "in_top": number when using "buffer": first line to send (default: 1) |
| 1171 | "in_bot": number when using "buffer": last line to send (default: last) |
| 1172 | "in_name": "/path/file" the name of the file or buffer to read from |
| 1173 | "in_buf": number the number of the buffer to read from |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 1174 | |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 1175 | *job-out_io* *out_name* *out_buf* |
| 1176 | "out_io": "null" disconnect stdout (goes to /dev/null) |
| 1177 | "out_io": "pipe" stdout is connected to the channel (default) |
| 1178 | "out_io": "file" stdout writes to a file |
Bram Moolenaar | 5162822 | 2016-12-01 23:03:28 +0100 | [diff] [blame] | 1179 | "out_io": "buffer" stdout appends to a buffer (see below) |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 1180 | "out_name": "/path/file" the name of the file or buffer to write to |
| 1181 | "out_buf": number the number of the buffer to write to |
Bram Moolenaar | 9f5842e | 2016-05-29 16:17:08 +0200 | [diff] [blame] | 1182 | "out_modifiable": 0 when writing to a buffer, 'modifiable' will be off |
| 1183 | (see below) |
Bram Moolenaar | 169ebb0 | 2016-09-07 23:32:23 +0200 | [diff] [blame] | 1184 | "out_msg": 0 when writing to a new buffer, the first line will be |
| 1185 | set to "Reading from channel output..." |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 1186 | |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 1187 | *job-err_io* *err_name* *err_buf* |
| 1188 | "err_io": "out" stderr messages to go to stdout |
| 1189 | "err_io": "null" disconnect stderr (goes to /dev/null) |
| 1190 | "err_io": "pipe" stderr is connected to the channel (default) |
| 1191 | "err_io": "file" stderr writes to a file |
Bram Moolenaar | 5162822 | 2016-12-01 23:03:28 +0100 | [diff] [blame] | 1192 | "err_io": "buffer" stderr appends to a buffer (see below) |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 1193 | "err_name": "/path/file" the name of the file or buffer to write to |
| 1194 | "err_buf": number the number of the buffer to write to |
Bram Moolenaar | 9f5842e | 2016-05-29 16:17:08 +0200 | [diff] [blame] | 1195 | "err_modifiable": 0 when writing to a buffer, 'modifiable' will be off |
| 1196 | (see below) |
Bram Moolenaar | 169ebb0 | 2016-09-07 23:32:23 +0200 | [diff] [blame] | 1197 | "err_msg": 0 when writing to a new buffer, the first line will be |
| 1198 | set to "Reading from channel error..." |
Bram Moolenaar | 77cdfd1 | 2016-03-12 12:57:59 +0100 | [diff] [blame] | 1199 | |
Bram Moolenaar | 7db8f6f | 2016-03-29 23:12:46 +0200 | [diff] [blame] | 1200 | "block_write": number only for testing: pretend every other write to stdin |
| 1201 | will block |
| 1202 | |
Bram Moolenaar | 05aafed | 2017-08-11 19:12:11 +0200 | [diff] [blame] | 1203 | "env": dict environment variables for the new process |
| 1204 | "cwd": "/path/to/dir" current working directory for the new process; |
| 1205 | if the directory does not exist an error is given |
| 1206 | |
Bram Moolenaar | 77cdfd1 | 2016-03-12 12:57:59 +0100 | [diff] [blame] | 1207 | |
| 1208 | Writing to a buffer ~ |
Bram Moolenaar | 818078d | 2016-08-27 21:58:42 +0200 | [diff] [blame] | 1209 | *out_io-buffer* |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 1210 | When the out_io or err_io mode is "buffer" and there is a callback, the text |
Bram Moolenaar | 5f148ec | 2016-03-07 22:59:26 +0100 | [diff] [blame] | 1211 | is appended to the buffer before invoking the callback. |
| 1212 | |
| 1213 | When a buffer is used both for input and output, the output lines are put |
| 1214 | above the last line, since the last line is what is written to the channel |
Bram Moolenaar | 77cdfd1 | 2016-03-12 12:57:59 +0100 | [diff] [blame] | 1215 | input. Otherwise lines are appended below the last line. |
Bram Moolenaar | c7f0ebc | 2016-02-27 21:10:09 +0100 | [diff] [blame] | 1216 | |
Bram Moolenaar | 328da0d | 2016-03-04 22:22:32 +0100 | [diff] [blame] | 1217 | When using JS or JSON mode with "buffer", only messages with zero or negative |
| 1218 | ID will be added to the buffer, after decoding + encoding. Messages with a |
| 1219 | positive number will be handled by a callback, commands are handled as usual. |
| 1220 | |
Bram Moolenaar | 82af871 | 2016-06-04 20:20:29 +0200 | [diff] [blame] | 1221 | The name of the buffer from "out_name" or "err_name" is compared the full name |
| 1222 | of existing buffers, also after expanding the name for the current directory. |
| 1223 | E.g., when a buffer was created with ":edit somename" and the buffer name is |
| 1224 | "somename" it will use that buffer. |
| 1225 | |
| 1226 | If there is no matching buffer a new buffer is created. Use an empty name to |
| 1227 | always create a new buffer. |ch_getbufnr()| can then be used to get the |
| 1228 | buffer number. |
Bram Moolenaar | c7f0ebc | 2016-02-27 21:10:09 +0100 | [diff] [blame] | 1229 | |
| 1230 | For a new buffer 'buftype' is set to "nofile" and 'bufhidden' to "hide". If |
| 1231 | you prefer other settings, create the buffer first and pass the buffer number. |
Bram Moolenaar | 169ebb0 | 2016-09-07 23:32:23 +0200 | [diff] [blame] | 1232 | *out_modifiable* *err_modifiable* |
Bram Moolenaar | 9f5842e | 2016-05-29 16:17:08 +0200 | [diff] [blame] | 1233 | The "out_modifiable" and "err_modifiable" options can be used to set the |
| 1234 | 'modifiable' option off, or write to a buffer that has 'modifiable' off. That |
| 1235 | means that lines will be appended to the buffer, but the user can't easily |
| 1236 | change the buffer. |
Bram Moolenaar | 169ebb0 | 2016-09-07 23:32:23 +0200 | [diff] [blame] | 1237 | *out_msg* *err_msg* |
| 1238 | The "out_msg" option can be used to specify whether a new buffer will have the |
| 1239 | first line set to "Reading from channel output...". The default is to add the |
| 1240 | message. "err_msg" does the same for channel error. |
| 1241 | |
Bram Moolenaar | 9f5842e | 2016-05-29 16:17:08 +0200 | [diff] [blame] | 1242 | When an existing buffer is to be written where 'modifiable' is off and the |
| 1243 | "out_modifiable" or "err_modifiable" options is not zero, an error is given |
| 1244 | and the buffer will not be written to. |
| 1245 | |
Bram Moolenaar | 187db50 | 2016-02-27 14:44:26 +0100 | [diff] [blame] | 1246 | When the buffer written to is displayed in a window and the cursor is in the |
| 1247 | first column of the last line, the cursor will be moved to the newly added |
| 1248 | line and the window is scrolled up to show the cursor if needed. |
| 1249 | |
Bram Moolenaar | 063b9d1 | 2016-07-09 20:21:48 +0200 | [diff] [blame] | 1250 | Undo is synced for every added line. NUL bytes are accepted (internally Vim |
| 1251 | stores these as NL bytes). |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 1252 | |
Bram Moolenaar | 77cdfd1 | 2016-03-12 12:57:59 +0100 | [diff] [blame] | 1253 | |
| 1254 | Writing to a file ~ |
Bram Moolenaar | d6c2f05 | 2016-03-14 23:22:59 +0100 | [diff] [blame] | 1255 | *E920* |
Bram Moolenaar | 77cdfd1 | 2016-03-12 12:57:59 +0100 | [diff] [blame] | 1256 | The file is created with permissions 600 (read-write for the user, not |
| 1257 | accessible for others). Use |setfperm()| to change this. |
| 1258 | |
| 1259 | If the file already exists it is truncated. |
| 1260 | |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 1261 | ============================================================================== |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 1262 | 13. Controlling a job *job-control* |
Bram Moolenaar | 38a5563 | 2016-02-15 22:07:32 +0100 | [diff] [blame] | 1263 | |
| 1264 | To get the status of a job: > |
| 1265 | echo job_status(job) |
| 1266 | |
| 1267 | To make a job stop running: > |
| 1268 | job_stop(job) |
| 1269 | |
| 1270 | This is the normal way to end a job. On Unix it sends a SIGTERM to the job. |
| 1271 | It is possible to use other ways to stop the job, or even send arbitrary |
| 1272 | signals. E.g. to force a job to stop, "kill it": > |
| 1273 | job_stop(job, "kill") |
| 1274 | |
| 1275 | For more options see |job_stop()|. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 1276 | |
Bram Moolenaar | f273245 | 2018-06-03 14:47:35 +0200 | [diff] [blame] | 1277 | ============================================================================== |
Bram Moolenaar | ed997ad | 2019-07-21 16:42:00 +0200 | [diff] [blame] | 1278 | 14. Using a prompt buffer *prompt-buffer* |
Bram Moolenaar | f273245 | 2018-06-03 14:47:35 +0200 | [diff] [blame] | 1279 | |
| 1280 | If you want to type input for the job in a Vim window you have a few options: |
| 1281 | - Use a normal buffer and handle all possible commands yourself. |
| 1282 | This will be complicated, since there are so many possible commands. |
| 1283 | - Use a terminal window. This works well if what you type goes directly to |
| 1284 | the job and the job output is directly displayed in the window. |
| 1285 | See |terminal-window|. |
Bram Moolenaar | acc2240 | 2020-06-07 21:07:18 +0200 | [diff] [blame] | 1286 | - Use a window with a prompt buffer. This works well when entering a line for |
| 1287 | the job in Vim while displaying (possibly filtered) output from the job. |
Bram Moolenaar | f273245 | 2018-06-03 14:47:35 +0200 | [diff] [blame] | 1288 | |
| 1289 | A prompt buffer is created by setting 'buftype' to "prompt". You would |
| 1290 | normally only do that in a newly created buffer. |
| 1291 | |
| 1292 | The user can edit and enter one line of text at the very last line of the |
| 1293 | buffer. When pressing Enter in the prompt line the callback set with |
| 1294 | |prompt_setcallback()| is invoked. It would normally send the line to a job. |
| 1295 | Another callback would receive the output from the job and display it in the |
| 1296 | buffer, below the prompt (and above the next prompt). |
| 1297 | |
| 1298 | Only the text in the last line, after the prompt, is editable. The rest of the |
| 1299 | buffer is not modifiable with Normal mode commands. It can be modified by |
| 1300 | calling functions, such as |append()|. Using other commands may mess up the |
| 1301 | buffer. |
| 1302 | |
| 1303 | After setting 'buftype' to "prompt" Vim does not automatically start Insert |
| 1304 | mode, use `:startinsert` if you want to enter Insert mode, so that the user |
| 1305 | can start typing a line. |
| 1306 | |
Bram Moolenaar | 077cc7a | 2020-09-04 16:35:35 +0200 | [diff] [blame] | 1307 | The text of the prompt can be set with the |prompt_setprompt()| function. If |
| 1308 | no prompt is set with |prompt_setprompt()|, "% " is used. You can get the |
| 1309 | effective prompt text for a buffer, with |prompt_getprompt()|. |
Bram Moolenaar | f273245 | 2018-06-03 14:47:35 +0200 | [diff] [blame] | 1310 | |
| 1311 | The user can go to Normal mode and navigate through the buffer. This can be |
Bram Moolenaar | 7ff7846 | 2020-07-10 22:00:53 +0200 | [diff] [blame] | 1312 | useful to see older output or copy text. |
Bram Moolenaar | f273245 | 2018-06-03 14:47:35 +0200 | [diff] [blame] | 1313 | |
Bram Moolenaar | d2f3a8b | 2018-06-19 14:35:59 +0200 | [diff] [blame] | 1314 | The CTRL-W key can be used to start a window command, such as CTRL-W w to |
| 1315 | switch to the next window. This also works in Insert mode (use Shift-CTRL-W |
| 1316 | to delete a word). When leaving the window Insert mode will be stopped. When |
| 1317 | coming back to the prompt window Insert mode will be restored. |
| 1318 | |
Bram Moolenaar | f273245 | 2018-06-03 14:47:35 +0200 | [diff] [blame] | 1319 | Any command that starts Insert mode, such as "a", "i", "A" and "I", will move |
Bram Moolenaar | d2f3a8b | 2018-06-19 14:35:59 +0200 | [diff] [blame] | 1320 | the cursor to the last line. "A" will move to the end of the line, "I" to the |
| 1321 | start of the line. |
Bram Moolenaar | f273245 | 2018-06-03 14:47:35 +0200 | [diff] [blame] | 1322 | |
Bram Moolenaar | acc2240 | 2020-06-07 21:07:18 +0200 | [diff] [blame] | 1323 | Here is an example for Unix. It starts a shell in the background and prompts |
| 1324 | for the next shell command. Output from the shell is displayed above the |
| 1325 | prompt. > |
| 1326 | |
| 1327 | " Create a channel log so we can see what happens. |
| 1328 | call ch_logfile('logfile', 'w') |
| 1329 | |
Bram Moolenaar | 1588bc8 | 2022-03-08 21:35:07 +0000 | [diff] [blame] | 1330 | " Function handling a line of text that has been typed. |
Bram Moolenaar | acc2240 | 2020-06-07 21:07:18 +0200 | [diff] [blame] | 1331 | func TextEntered(text) |
| 1332 | " Send the text to a shell with Enter appended. |
| 1333 | call ch_sendraw(g:shell_job, a:text .. "\n") |
| 1334 | endfunc |
Bram Moolenaar | cb80aa2 | 2020-10-26 21:12:46 +0100 | [diff] [blame] | 1335 | |
Bram Moolenaar | 1588bc8 | 2022-03-08 21:35:07 +0000 | [diff] [blame] | 1336 | " Function handling output from the shell: Add it above the prompt. |
Bram Moolenaar | acc2240 | 2020-06-07 21:07:18 +0200 | [diff] [blame] | 1337 | func GotOutput(channel, msg) |
Bram Moolenaar | c51cf03 | 2022-02-26 12:25:45 +0000 | [diff] [blame] | 1338 | call append(line("$") - 1, "- " .. a:msg) |
Bram Moolenaar | acc2240 | 2020-06-07 21:07:18 +0200 | [diff] [blame] | 1339 | endfunc |
| 1340 | |
Bram Moolenaar | 1588bc8 | 2022-03-08 21:35:07 +0000 | [diff] [blame] | 1341 | " Function handling the shell exits: close the window. |
Bram Moolenaar | acc2240 | 2020-06-07 21:07:18 +0200 | [diff] [blame] | 1342 | func JobExit(job, status) |
| 1343 | quit! |
| 1344 | endfunc |
| 1345 | |
| 1346 | " Start a shell in the background. |
| 1347 | let shell_job = job_start(["/bin/sh"], #{ |
| 1348 | \ out_cb: function('GotOutput'), |
| 1349 | \ err_cb: function('GotOutput'), |
| 1350 | \ exit_cb: function('JobExit'), |
| 1351 | \ }) |
Bram Moolenaar | acc2240 | 2020-06-07 21:07:18 +0200 | [diff] [blame] | 1352 | |
| 1353 | new |
| 1354 | set buftype=prompt |
| 1355 | let buf = bufnr('') |
| 1356 | call prompt_setcallback(buf, function("TextEntered")) |
| 1357 | eval prompt_setprompt(buf, "shell command: ") |
| 1358 | |
| 1359 | " start accepting shell commands |
| 1360 | startinsert |
| 1361 | < |
Bram Moolenaar | 1588bc8 | 2022-03-08 21:35:07 +0000 | [diff] [blame] | 1362 | The same in |Vim9| script: > |
| 1363 | |
| 1364 | vim9script |
| 1365 | |
| 1366 | # Create a channel log so we can see what happens. |
| 1367 | ch_logfile('logfile', 'w') |
| 1368 | |
| 1369 | var shell_job: job |
| 1370 | |
| 1371 | # Function handling a line of text that has been typed. |
| 1372 | def TextEntered(text: string) |
| 1373 | # Send the text to a shell with Enter appended. |
| 1374 | ch_sendraw(shell_job, text .. "\n") |
| 1375 | enddef |
| 1376 | |
| 1377 | # Function handling output from the shell: Add it above the prompt. |
| 1378 | def GotOutput(channel: channel, msg: string) |
| 1379 | append(line("$") - 1, "- " .. msg) |
| 1380 | enddef |
| 1381 | |
| 1382 | # Function handling the shell exits: close the window. |
| 1383 | def JobExit(job: job, status: number) |
| 1384 | quit! |
| 1385 | enddef |
| 1386 | |
| 1387 | # Start a shell in the background. |
| 1388 | shell_job = job_start(["/bin/sh"], { |
| 1389 | out_cb: GotOutput, |
| 1390 | err_cb: GotOutput, |
| 1391 | exit_cb: JobExit, |
| 1392 | }) |
| 1393 | |
| 1394 | new |
| 1395 | set buftype=prompt |
| 1396 | var buf = bufnr('') |
| 1397 | prompt_setcallback(buf, TextEntered) |
| 1398 | prompt_setprompt(buf, "shell command: ") |
| 1399 | |
| 1400 | # start accepting shell commands |
| 1401 | startinsert |
Bram Moolenaar | acc2240 | 2020-06-07 21:07:18 +0200 | [diff] [blame] | 1402 | |
Yegappan Lakshmanan | 9247a22 | 2022-03-30 10:16:05 +0100 | [diff] [blame] | 1403 | ============================================================================== |
Yegappan Lakshmanan | 3b470ae | 2022-04-16 10:41:27 +0100 | [diff] [blame] | 1404 | 15. Language Server Protocol *language-server-protocol* |
Yegappan Lakshmanan | 9247a22 | 2022-03-30 10:16:05 +0100 | [diff] [blame] | 1405 | |
| 1406 | The language server protocol specification is available at: |
| 1407 | |
| 1408 | https://microsoft.github.io/language-server-protocol/specification |
| 1409 | |
| 1410 | Each LSP protocol message starts with a simple HTTP header followed by the |
| 1411 | payload encoded in JSON-RPC format. This is described in: |
| 1412 | |
| 1413 | https://www.jsonrpc.org/specification |
| 1414 | |
Yegappan Lakshmanan | e0805b8 | 2022-04-16 15:18:23 +0100 | [diff] [blame] | 1415 | To encode and send a LSP request/notification message in a Vim |Dict| into a |
| 1416 | LSP JSON-RPC message and to receive and decode a LSP JSON-RPC |
| 1417 | response/notification message into a Vim |Dict|, connect to the LSP server |
| 1418 | with the |channel-mode| set to "lsp". |
| 1419 | |
| 1420 | For messages received on a channel with |channel-mode| set to "lsp", Vim will |
| 1421 | process the HTTP header and decode the JSON-RPC payload into a Vim |Dict| type |
| 1422 | and call the |channel-callback| function or the specified |
| 1423 | |channel-onetime-callback| function. When sending messages on a channel using |
| 1424 | the |ch_evalexpr()| or |ch_sendexpr()| functions, Vim will add the HTTP header |
| 1425 | and encode the Vim expression into JSON. Refer to |json_encode()| and |
| 1426 | |json_decode()| for more information about how Vim encodes and decodes the |
| 1427 | builtin types into JSON. |
Yegappan Lakshmanan | 9247a22 | 2022-03-30 10:16:05 +0100 | [diff] [blame] | 1428 | |
| 1429 | To open a channel using the 'lsp' mode, set the 'mode' item in the |ch_open()| |
| 1430 | {options} argument to 'lsp'. Example: > |
| 1431 | |
| 1432 | let ch = ch_open(..., #{mode: 'lsp'}) |
| 1433 | |
| 1434 | To open a channel using the 'lsp' mode with a job, set the 'in_mode' and |
| 1435 | 'out_mode' items in the |job_start()| {options} argument to 'lsp'. Example: > |
| 1436 | |
Yegappan Lakshmanan | e0805b8 | 2022-04-16 15:18:23 +0100 | [diff] [blame] | 1437 | let cmd = ['clangd', '--background-index', '--clang-tidy'] |
| 1438 | let opts = {} |
| 1439 | let opts.in_mode = 'lsp' |
| 1440 | let opts.out_mode = 'lsp' |
Yegappan Lakshmanan | 03cca29 | 2022-04-18 14:07:46 +0100 | [diff] [blame] | 1441 | let opts.err_mode = 'nl' |
Yegappan Lakshmanan | e0805b8 | 2022-04-16 15:18:23 +0100 | [diff] [blame] | 1442 | let opts.out_cb = function('LspOutCallback') |
| 1443 | let opts.err_cb = function('LspErrCallback') |
| 1444 | let opts.exit_cb = function('LspExitCallback') |
| 1445 | let job = job_start(cmd, opts) |
Yegappan Lakshmanan | 9247a22 | 2022-03-30 10:16:05 +0100 | [diff] [blame] | 1446 | |
Yegappan Lakshmanan | 03cca29 | 2022-04-18 14:07:46 +0100 | [diff] [blame] | 1447 | Note that if a job outputs LSP messages on stdout and non-LSP messages on |
| 1448 | stderr, then the channel-callback function should handle both the message |
| 1449 | formats appropriately or you should use a separate callback function for |
| 1450 | "out_cb" and "err_cb" to handle them as shown above. |
| 1451 | |
Yegappan Lakshmanan | 3b470ae | 2022-04-16 10:41:27 +0100 | [diff] [blame] | 1452 | To synchronously send a JSON-RPC request to the server, use the |
| 1453 | |ch_evalexpr()| function. This function will wait and return the decoded |
| 1454 | response message from the server. You can use either the |channel-timeout| or |
Yegappan Lakshmanan | 9247a22 | 2022-03-30 10:16:05 +0100 | [diff] [blame] | 1455 | the 'timeout' field in the {options} argument to control the response wait |
Yegappan Lakshmanan | e0805b8 | 2022-04-16 15:18:23 +0100 | [diff] [blame] | 1456 | time. If the request times out, then an empty |Dict| is returned. Example: > |
Yegappan Lakshmanan | 9247a22 | 2022-03-30 10:16:05 +0100 | [diff] [blame] | 1457 | |
| 1458 | let req = {} |
| 1459 | let req.method = 'textDocument/definition' |
| 1460 | let req.params = {} |
| 1461 | let req.params.textDocument = #{uri: 'a.c'} |
| 1462 | let req.params.position = #{line: 10, character: 3} |
Yegappan Lakshmanan | e0805b8 | 2022-04-16 15:18:23 +0100 | [diff] [blame] | 1463 | let defs = ch_evalexpr(ch, req, #{timeout: 100}) |
| 1464 | if defs->empty() |
| 1465 | ... <handle failure> |
| 1466 | endif |
Yegappan Lakshmanan | 9247a22 | 2022-03-30 10:16:05 +0100 | [diff] [blame] | 1467 | |
| 1468 | Note that in the request message the 'id' field should not be specified. If it |
| 1469 | is specified, then Vim will overwrite the value with an internally generated |
| 1470 | identifier. Vim currently supports only a number type for the 'id' field. |
Yegappan Lakshmanan | 3b470ae | 2022-04-16 10:41:27 +0100 | [diff] [blame] | 1471 | The callback function will be invoked for both a successful and a failed RPC |
Yegappan Lakshmanan | e0805b8 | 2022-04-16 15:18:23 +0100 | [diff] [blame] | 1472 | request. |
Yegappan Lakshmanan | 9247a22 | 2022-03-30 10:16:05 +0100 | [diff] [blame] | 1473 | |
| 1474 | To send a JSON-RPC request to the server and asynchronously process the |
Yegappan Lakshmanan | e0805b8 | 2022-04-16 15:18:23 +0100 | [diff] [blame] | 1475 | response, use the |ch_sendexpr()| function and supply a callback function. If |
| 1476 | the "id" field is present in the request message, then Vim will overwrite it |
| 1477 | with an internally generated number. This function returns a Dict with the |
| 1478 | identifier used for the message. This can be used to send cancellation |
| 1479 | request to the LSP server (if needed). Example: > |
Yegappan Lakshmanan | 9247a22 | 2022-03-30 10:16:05 +0100 | [diff] [blame] | 1480 | |
| 1481 | let req = {} |
| 1482 | let req.method = 'textDocument/hover' |
Yegappan Lakshmanan | 3b470ae | 2022-04-16 10:41:27 +0100 | [diff] [blame] | 1483 | let req.id = 200 |
Yegappan Lakshmanan | 9247a22 | 2022-03-30 10:16:05 +0100 | [diff] [blame] | 1484 | let req.params = {} |
| 1485 | let req.params.textDocument = #{uri: 'a.c'} |
| 1486 | let req.params.position = #{line: 10, character: 3} |
Yegappan Lakshmanan | e0805b8 | 2022-04-16 15:18:23 +0100 | [diff] [blame] | 1487 | let resp = ch_sendexpr(ch, req, #{callback: 'HoverFunc'}) |
Yegappan Lakshmanan | 9247a22 | 2022-03-30 10:16:05 +0100 | [diff] [blame] | 1488 | |
Yegappan Lakshmanan | e0805b8 | 2022-04-16 15:18:23 +0100 | [diff] [blame] | 1489 | To cancel an outstanding asynchronous LSP request sent to the server using the |
Bram Moolenaar | d592deb | 2022-06-17 15:42:40 +0100 | [diff] [blame] | 1490 | |ch_sendexpr()| function, send a cancellation message to the server using the |
Yegappan Lakshmanan | e0805b8 | 2022-04-16 15:18:23 +0100 | [diff] [blame] | 1491 | |ch_sendexpr()| function with the ID returned by the |ch_sendexpr()| function |
| 1492 | for the request. Example: > |
Yegappan Lakshmanan | 3b470ae | 2022-04-16 10:41:27 +0100 | [diff] [blame] | 1493 | |
| 1494 | " send a completion request |
| 1495 | let req = {} |
| 1496 | let req.method = 'textDocument/completion' |
| 1497 | let req.params = {} |
| 1498 | let req.params.textDocument = #{uri: 'a.c'} |
| 1499 | let req.params.position = #{line: 10, character: 3} |
Yegappan Lakshmanan | e0805b8 | 2022-04-16 15:18:23 +0100 | [diff] [blame] | 1500 | let reqstatus = ch_sendexpr(ch, req, #{callback: 'LspComplete'}) |
Yegappan Lakshmanan | 3b470ae | 2022-04-16 10:41:27 +0100 | [diff] [blame] | 1501 | " send a cancellation notification |
| 1502 | let notif = {} |
| 1503 | let notif.method = '$/cancelRequest' |
| 1504 | let notif.id = reqstatus.id |
| 1505 | call ch_sendexpr(ch, notif) |
| 1506 | |
Yegappan Lakshmanan | 9247a22 | 2022-03-30 10:16:05 +0100 | [diff] [blame] | 1507 | To send a JSON-RPC notification message to the server, use the |ch_sendexpr()| |
Yegappan Lakshmanan | 3b470ae | 2022-04-16 10:41:27 +0100 | [diff] [blame] | 1508 | function. As the server will not send a response message to the notification, |
| 1509 | don't specify the "callback" item. Example: > |
Yegappan Lakshmanan | 9247a22 | 2022-03-30 10:16:05 +0100 | [diff] [blame] | 1510 | |
| 1511 | call ch_sendexpr(ch, #{method: 'initialized'}) |
| 1512 | |
| 1513 | To respond to a JSON-RPC request message from the server, use the |
| 1514 | |ch_sendexpr()| function. In the response message, copy the 'id' field value |
| 1515 | from the server request message. Example: > |
| 1516 | |
| 1517 | let resp = {} |
| 1518 | let resp.id = req.id |
| 1519 | let resp.result = 1 |
| 1520 | call ch_sendexpr(ch, resp) |
| 1521 | |
| 1522 | The JSON-RPC notification messages from the server are delivered through the |
| 1523 | |channel-callback| function. |
Bram Moolenaar | 3b5f929 | 2016-01-28 22:37:01 +0100 | [diff] [blame] | 1524 | |
Yegappan Lakshmanan | 3b470ae | 2022-04-16 10:41:27 +0100 | [diff] [blame] | 1525 | Depending on the use case, you can use the ch_evalexpr(), ch_sendexpr() and |
| 1526 | ch_sendraw() functions on the same channel. |
| 1527 | |
| 1528 | A LSP request message has the following format (expressed as a Vim Dict). The |
| 1529 | "params" field is optional: > |
| 1530 | |
| 1531 | { |
| 1532 | "jsonrpc": "2.0", |
| 1533 | "id": <number>, |
| 1534 | "method": <string>, |
| 1535 | "params": <list|dict> |
| 1536 | } |
| 1537 | |
Bram Moolenaar | d592deb | 2022-06-17 15:42:40 +0100 | [diff] [blame] | 1538 | A LSP response message has the following format (expressed as a Vim Dict). The |
Yegappan Lakshmanan | 3b470ae | 2022-04-16 10:41:27 +0100 | [diff] [blame] | 1539 | "result" and "error" fields are optional: > |
| 1540 | |
| 1541 | { |
| 1542 | "jsonrpc": "2.0", |
| 1543 | "id": <number>, |
| 1544 | "result": <vim type> |
| 1545 | "error": <dict> |
| 1546 | } |
| 1547 | |
| 1548 | A LSP notification message has the following format (expressed as a Vim Dict). |
| 1549 | The "params" field is optional: > |
| 1550 | |
| 1551 | { |
| 1552 | "jsonrpc": "2.0", |
| 1553 | "method": <string>, |
| 1554 | "params": <list|dict> |
| 1555 | } |
| 1556 | |
Bram Moolenaar | 8a3b805 | 2022-06-26 12:21:15 +0100 | [diff] [blame] | 1557 | < |
Bram Moolenaar | 91f84f6 | 2018-07-29 15:07:52 +0200 | [diff] [blame] | 1558 | vim:tw=78:ts=8:noet:ft=help:norl: |