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