Skip to content

Commit

Permalink
feat(api): enable nvim_exec_autocmds to pass arbitrary data (neovim#1…
Browse files Browse the repository at this point in the history
…8613)

Add a "data" key to nvim_exec_autocmds that passes arbitrary data (API
objects) to autocommand callbacks.
  • Loading branch information
gpanders authored and kraftwerk28 committed Jun 1, 2022
1 parent 07e1074 commit 268330a
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 6 deletions.
5 changes: 5 additions & 0 deletions runtime/doc/api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3469,6 +3469,8 @@ nvim_create_autocmd({event}, {*opts}) *nvim_create_autocmd()*
• buf: (number) the expanded value of |<abuf>|
• file: (string) the expanded value of
|<afile>|
• data: (any) arbitrary data passed to
|nvim_exec_autocmds()|

• command (string) optional: Vim command to
execute on event. Cannot be used with
Expand Down Expand Up @@ -3544,6 +3546,9 @@ nvim_exec_autocmds({event}, {*opts}) *nvim_exec_autocmds()*
• modeline (bool) optional: defaults to true.
Process the modeline after the autocommands
|<nomodeline>|.
• data (any): arbitrary data to send to the
autocommand callback. See
|nvim_create_autocmd()| for details.

See also: ~
|:doautocmd|
Expand Down
10 changes: 9 additions & 1 deletion src/nvim/api/autocmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@ Array nvim_get_autocmds(Dict(get_autocmds) *opts, Error *err)
/// - match: (string) the expanded value of |<amatch>|
/// - buf: (number) the expanded value of |<abuf>|
/// - file: (string) the expanded value of |<afile>|
/// - data: (any) arbitrary data passed to |nvim_exec_autocmds()|
/// - command (string) optional: Vim command to execute on event. Cannot be used with
/// {callback}
/// - once (boolean) optional: defaults to false. Run the autocommand
Expand Down Expand Up @@ -749,6 +750,8 @@ void nvim_del_augroup_by_name(String name, Error *err)
/// {pattern}.
/// - modeline (bool) optional: defaults to true. Process the
/// modeline after the autocommands |<nomodeline>|.
/// - data (any): arbitrary data to send to the autocommand callback. See
/// |nvim_create_autocmd()| for details.
/// @see |:doautocmd|
void nvim_exec_autocmds(Object event, Dict(exec_autocmds) *opts, Error *err)
FUNC_API_SINCE(9)
Expand All @@ -760,6 +763,7 @@ void nvim_exec_autocmds(Object event, Dict(exec_autocmds) *opts, Error *err)
bool set_buf = false;

char *pattern = NULL;
Object *data = NULL;
bool set_pattern = false;

Array event_array = ARRAY_DICT_INIT;
Expand Down Expand Up @@ -818,6 +822,10 @@ void nvim_exec_autocmds(Object event, Dict(exec_autocmds) *opts, Error *err)
set_pattern = true;
}

if (opts->data.type != kObjectTypeNil) {
data = &opts->data;
}

modeline = api_object_to_bool(opts->modeline, "modeline", true, err);

if (set_pattern && set_buf) {
Expand All @@ -829,7 +837,7 @@ void nvim_exec_autocmds(Object event, Dict(exec_autocmds) *opts, Error *err)
FOREACH_ITEM(event_array, event_str, {
GET_ONE_EVENT(event_nr, event_str, cleanup)

did_aucmd |= apply_autocmds_group(event_nr, pattern, NULL, true, au_group, buf, NULL);
did_aucmd |= apply_autocmds_group(event_nr, pattern, NULL, true, au_group, buf, NULL, data);
})

if (did_aucmd && modeline) {
Expand Down
1 change: 1 addition & 0 deletions src/nvim/api/keysets.lua
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ return {
"group";
"modeline";
"pattern";
"data";
};
get_autocmds = {
"event";
Expand Down
17 changes: 12 additions & 5 deletions src/nvim/autocmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -1220,7 +1220,7 @@ int do_doautocmd(char *arg_start, bool do_msg, bool *did_something)
// Loop over the events.
while (*arg && !ends_excmd(*arg) && !ascii_iswhite(*arg)) {
if (apply_autocmds_group(event_name2nr(arg, &arg), fname, NULL, true, group,
curbuf, NULL)) {
curbuf, NULL, NULL)) {
nothing_done = false;
}
}
Expand Down Expand Up @@ -1505,7 +1505,7 @@ void aucmd_restbuf(aco_save_T *aco)
/// @return true if some commands were executed.
bool apply_autocmds(event_T event, char *fname, char *fname_io, bool force, buf_T *buf)
{
return apply_autocmds_group(event, fname, fname_io, force, AUGROUP_ALL, buf, NULL);
return apply_autocmds_group(event, fname, fname_io, force, AUGROUP_ALL, buf, NULL, NULL);
}

/// Like apply_autocmds(), but with extra "eap" argument. This takes care of
Expand All @@ -1522,7 +1522,7 @@ bool apply_autocmds(event_T event, char *fname, char *fname_io, bool force, buf_
bool apply_autocmds_exarg(event_T event, char *fname, char *fname_io, bool force, buf_T *buf,
exarg_T *eap)
{
return apply_autocmds_group(event, fname, fname_io, force, AUGROUP_ALL, buf, eap);
return apply_autocmds_group(event, fname, fname_io, force, AUGROUP_ALL, buf, eap, NULL);
}

/// Like apply_autocmds(), but handles the caller's retval. If the script
Expand All @@ -1546,7 +1546,7 @@ bool apply_autocmds_retval(event_T event, char *fname, char *fname_io, bool forc
}

bool did_cmd = apply_autocmds_group(event, fname, fname_io, force,
AUGROUP_ALL, buf, NULL);
AUGROUP_ALL, buf, NULL, NULL);
if (did_cmd && aborting()) {
*retval = FAIL;
}
Expand Down Expand Up @@ -1595,7 +1595,7 @@ bool trigger_cursorhold(void) FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT
///
/// @return true if some commands were executed.
bool apply_autocmds_group(event_T event, char *fname, char *fname_io, bool force, int group,
buf_T *buf, exarg_T *eap)
buf_T *buf, exarg_T *eap, Object *data)
{
char *sfname = NULL; // short file name
bool retval = false;
Expand Down Expand Up @@ -1811,6 +1811,9 @@ bool apply_autocmds_group(event_T event, char *fname, char *fname_io, bool force
patcmd.next = active_apc_list;
active_apc_list = &patcmd;

// Attach data to command
patcmd.data = data;

// set v:cmdarg (only when there is a matching pattern)
save_cmdbang = (long)get_vim_var_nr(VV_CMDBANG);
if (eap != NULL) {
Expand Down Expand Up @@ -2026,6 +2029,10 @@ static bool call_autocmd_callback(const AutoCmd *ac, const AutoPatCmd *apc)
PUT(data, "file", CSTR_TO_OBJ((char *)autocmd_fname));
PUT(data, "buf", INTEGER_OBJ(autocmd_bufnr));

if (apc->data) {
PUT(data, "data", copy_object(*apc->data));
}

int group = apc->curpat->group;
switch (group) {
case AUGROUP_ERROR:
Expand Down
1 change: 1 addition & 0 deletions src/nvim/autocmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ typedef struct AutoPatCmd {
char *tail; // tail of fname
event_T event; // current event
int arg_bufnr; // initially equal to <abuf>, set to zero when buf is deleted
Object *data; // arbitrary data
struct AutoPatCmd *next; // chain of active apc-s for auto-invalidation
} AutoPatCmd;

Expand Down
28 changes: 28 additions & 0 deletions test/functional/api/autocmd_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,34 @@ describe('autocmd api', function()
}, meths.get_var("autocmd_args"))

end)

it('can receive arbitrary data', function()
local function test(data)
eq(data, exec_lua([[
local input = ...
local output
vim.api.nvim_create_autocmd("User", {
pattern = "Test",
callback = function(args)
output = args.data
end,
})
vim.api.nvim_exec_autocmds("User", {
pattern = "Test",
data = input,
})
return output
]], data))
end

test("Hello")
test(42)
test(true)
test({ "list" })
test({ foo = "bar" })
end)
end)

describe('nvim_get_autocmds', function()
Expand Down

0 comments on commit 268330a

Please sign in to comment.