Skip to content

Commit 2affb83

Browse files
committed
docs: api events
1 parent 9c3099f commit 2affb83

File tree

7 files changed

+133
-70
lines changed

7 files changed

+133
-70
lines changed

runtime/doc/api.txt

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -254,19 +254,6 @@ The idea is "versionless evolution", in the words of Rich Hickey:
254254
- Relaxing a requirement should be a compatible change.
255255
- Strengthening a promise should be a compatible change.
256256

257-
==============================================================================
258-
Global events *api-global-events*
259-
260-
When a client invokes an API request as an async notification, it is not
261-
possible for Nvim to send an error response. Instead, in case of error, the
262-
following notification will be sent to the client:
263-
264-
*nvim_error_event*
265-
nvim_error_event[{type}, {message}]
266-
267-
{type} is a numeric id as defined by `api_info().error_types`, and {message} is
268-
a string with the error message.
269-
270257
==============================================================================
271258
Buffer update events *api-buffer-updates*
272259

@@ -553,6 +540,38 @@ Extmark positions changed by an edit will be restored on undo/redo. Creating
553540
and deleting extmarks is not a buffer change, thus new undo states are not
554541
created for extmark changes.
555542

543+
==============================================================================
544+
Global Events *api-events*
545+
546+
nvim_error_event({type}, {msg}) *nvim_error_event*
547+
Emitted on the client channel if an async API request responds with an
548+
error.
549+
550+
Attributes: ~
551+
|RPC| only
552+
553+
Parameters: ~
554+
{type} (`integer`) Error type id as defined by
555+
`api_info().error_types`.
556+
{msg} (`string`) Error message.
557+
558+
nvim_ui_term_event({event}, {value}) *nvim_ui_term_event*
559+
Emitted by the TUI client to signal when a host-terminal event occurred.
560+
561+
Supports these events:
562+
• "termresponse": The host-terminal sent a DA1, OSC, DCS, or APC response
563+
sequence to Nvim. The payload is the received response. Sets
564+
|v:termresponse| and fires |TermResponse|.
565+
566+
Attributes: ~
567+
|RPC| only
568+
Since: 0.10.0
569+
570+
Parameters: ~
571+
{event} (`string`) Event name
572+
{value} (`any`) Event payload
573+
574+
556575
==============================================================================
557576
Global Functions *api-global*
558577

@@ -3660,22 +3679,6 @@ nvim_ui_set_option({name}, {value}) *nvim_ui_set_option()*
36603679
{name} (`string`)
36613680
{value} (`any`)
36623681

3663-
nvim_ui_term_event({event}, {value}) *nvim_ui_term_event()*
3664-
Tells Nvim when a host-terminal event occurred.
3665-
3666-
Supports these events:
3667-
• "termresponse": The host-terminal sent a DA1, OSC, DCS, or APC response
3668-
sequence to Nvim. The payload is the received response. Sets
3669-
|v:termresponse| and fires |TermResponse|.
3670-
3671-
Attributes: ~
3672-
|RPC| only
3673-
Since: 0.10.0
3674-
3675-
Parameters: ~
3676-
{event} (`string`) Event name
3677-
{value} (`any`) Event payload
3678-
36793682
nvim_ui_try_resize({width}, {height}) *nvim_ui_try_resize()*
36803683

36813684
Attributes: ~

src/gen/gen_api_dispatch.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,7 @@ output:write([[
395395
#include "nvim/api/buffer.h"
396396
#include "nvim/api/command.h"
397397
#include "nvim/api/deprecated.h"
398+
#include "nvim/api/events.h"
398399
#include "nvim/api/extmark.h"
399400
#include "nvim/api/options.h"
400401
#include "nvim/api/tabpage.h"

src/gen/gen_vimdoc.lua

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#!/usr/bin/env -S nvim -l
2-
--- Generates Nvim :help docs from Lua/C docstrings
2+
--- Generates Nvim :help docs from Lua/C docstrings.
3+
---
4+
--- Usage:
5+
--- make doc
36
---
47
--- The generated :help text for each function is formatted as follows:
58
--- - Max width of 78 columns (`TEXT_WIDTH`).
@@ -106,6 +109,7 @@ local config = {
106109
filename = 'api.txt',
107110
section_order = {
108111
-- Sections at the top, in a specific order:
112+
'events.c',
109113
'vim.c',
110114
'vimscript.c',
111115

@@ -126,11 +130,22 @@ local config = {
126130
['vim.c'] = 'Global',
127131
},
128132
section_fmt = function(name)
133+
if name == 'Events' then
134+
return 'Global Events'
135+
end
136+
129137
return name .. ' Functions'
130138
end,
131139
helptag_fmt = function(name)
132140
return fmt('api-%s', name:lower())
133141
end,
142+
fn_helptag_fmt = function(fun)
143+
local name = fun.name
144+
if vim.endswith(name, '_event') then
145+
return name
146+
end
147+
return fn_helptag_fmt_common(fun)
148+
end,
134149
},
135150
lua = {
136151
filename = 'lua.txt',

src/nvim/api/events.c

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#include <assert.h>
2+
#include <inttypes.h>
3+
#include <stdbool.h>
4+
#include <stddef.h>
5+
#include <stdint.h>
6+
#include <stdlib.h>
7+
#include <string.h>
8+
9+
#include "klib/kvec.h"
10+
#include "nvim/api/events.h"
11+
#include "nvim/api/private/converter.h"
12+
#include "nvim/api/private/defs.h"
13+
#include "nvim/api/private/helpers.h"
14+
#include "nvim/api/private/validate.h"
15+
#include "nvim/api/ui.h"
16+
#include "nvim/assert_defs.h"
17+
#include "nvim/autocmd.h"
18+
#include "nvim/autocmd_defs.h"
19+
#include "nvim/channel.h"
20+
#include "nvim/channel_defs.h"
21+
#include "nvim/eval.h"
22+
#include "nvim/globals.h"
23+
#include "nvim/main.h"
24+
#include "nvim/map_defs.h"
25+
#include "nvim/memory.h"
26+
#include "nvim/memory_defs.h"
27+
#include "nvim/msgpack_rpc/channel.h"
28+
#include "nvim/msgpack_rpc/channel_defs.h"
29+
#include "nvim/msgpack_rpc/packer.h"
30+
#include "nvim/msgpack_rpc/packer_defs.h"
31+
#include "nvim/types_defs.h"
32+
#include "nvim/ui.h"
33+
34+
/// Emitted on the client channel if an async API request responds with an error.
35+
///
36+
/// @param channel_id
37+
/// @param type Error type id as defined by `api_info().error_types`.
38+
/// @param msg Error message.
39+
void nvim_error_event(uint64_t channel_id, Integer type, String msg)
40+
FUNC_API_REMOTE_ONLY
41+
{
42+
// TODO(bfredl): consider printing message to user, as will be relevant
43+
// if we fork nvim processes as async workers
44+
ELOG("async error on channel %" PRId64 ": %s", channel_id, msg.size ? msg.data : "");
45+
}
46+
47+
/// Emitted by the TUI client to signal when a host-terminal event occurred.
48+
///
49+
/// Supports these events:
50+
///
51+
/// - "termresponse": The host-terminal sent a DA1, OSC, DCS, or APC response sequence to Nvim.
52+
/// The payload is the received response. Sets |v:termresponse| and fires
53+
/// |TermResponse|.
54+
///
55+
/// @param channel_id
56+
/// @param event Event name
57+
/// @param value Event payload
58+
/// @param[out] err Error details, if any.
59+
void nvim_ui_term_event(uint64_t channel_id, String event, Object value, Error *err)
60+
FUNC_API_SINCE(12) FUNC_API_REMOTE_ONLY
61+
{
62+
if (strequal("termresponse", event.data)) {
63+
if (value.type != kObjectTypeString) {
64+
api_set_error(err, kErrorTypeValidation, "termresponse must be a string");
65+
return;
66+
}
67+
68+
const String termresponse = value.data.string;
69+
set_vim_var_string(VV_TERMRESPONSE, termresponse.data, (ptrdiff_t)termresponse.size);
70+
71+
MAXSIZE_TEMP_DICT(data, 1);
72+
PUT_C(data, "sequence", value);
73+
apply_autocmds_group(EVENT_TERMRESPONSE, NULL, NULL, true, AUGROUP_ALL, NULL, NULL,
74+
&DICT_OBJ(data));
75+
}
76+
}

src/nvim/api/events.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#pragma once
2+
3+
#include <stdint.h> // IWYU pragma: keep
4+
5+
#include "nvim/api/keysets_defs.h" // IWYU pragma: keep
6+
#include "nvim/api/private/defs.h" // IWYU pragma: keep
7+
8+
#include "api/events.h.generated.h"

src/nvim/api/ui.c

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -567,37 +567,6 @@ void nvim_ui_pum_set_bounds(uint64_t channel_id, Float width, Float height, Floa
567567
ui->pum_pos = true;
568568
}
569569

570-
/// Tells Nvim when a host-terminal event occurred.
571-
///
572-
/// Supports these events:
573-
///
574-
/// - "termresponse": The host-terminal sent a DA1, OSC, DCS, or APC response sequence to Nvim.
575-
/// The payload is the received response. Sets |v:termresponse| and fires
576-
/// |TermResponse|.
577-
///
578-
/// @param channel_id
579-
/// @param event Event name
580-
/// @param value Event payload
581-
/// @param[out] err Error details, if any.
582-
void nvim_ui_term_event(uint64_t channel_id, String event, Object value, Error *err)
583-
FUNC_API_SINCE(12) FUNC_API_REMOTE_ONLY
584-
{
585-
if (strequal("termresponse", event.data)) {
586-
if (value.type != kObjectTypeString) {
587-
api_set_error(err, kErrorTypeValidation, "termresponse must be a string");
588-
return;
589-
}
590-
591-
const String termresponse = value.data.string;
592-
set_vim_var_string(VV_TERMRESPONSE, termresponse.data, (ptrdiff_t)termresponse.size);
593-
594-
MAXSIZE_TEMP_DICT(data, 1);
595-
PUT_C(data, "sequence", value);
596-
apply_autocmds_group(EVENT_TERMRESPONSE, NULL, NULL, true, AUGROUP_ALL, NULL, NULL,
597-
&DICT_OBJ(data));
598-
}
599-
}
600-
601570
static void flush_event(RemoteUI *ui)
602571
{
603572
if (ui->cur_event) {

src/nvim/api/vim.c

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2222,15 +2222,6 @@ DictAs(eval_statusline_ret) nvim_eval_statusline(String str, Dict(eval_statuslin
22222222
return result;
22232223
}
22242224

2225-
/// @nodoc
2226-
void nvim_error_event(uint64_t channel_id, Integer lvl, String data)
2227-
FUNC_API_REMOTE_ONLY
2228-
{
2229-
// TODO(bfredl): consider printing message to user, as will be relevant
2230-
// if we fork nvim processes as async workers
2231-
ELOG("async error on channel %" PRId64 ": %s", channel_id, data.size ? data.data : "");
2232-
}
2233-
22342225
/// EXPERIMENTAL: this API may change in the future.
22352226
///
22362227
/// Sets info for the completion item at the given index. If the info text was shown in a window,

0 commit comments

Comments
 (0)