From 08dd3adffb60ae6c031f9b8310febd65bb14604e Mon Sep 17 00:00:00 2001 From: glepnir Date: Thu, 10 Sep 2026 03:42:20 +0800 Subject: feat(api): add ev.win to autocmd callback args #39826 Problem: Autocmd callbacks currently have to rely on curwin, which may already have changed by the time they run. This is especially noticeable for WinNew, WinClosed, WinResized and WinScrolled. Solution: Track the window in the autocmd context and expose it as ev.win. Pass it explicitly for the window events that need it. --- src/nvim/api/autocmd.c | 11 ++++++----- src/nvim/api/win_config.c | 2 +- 2 files changed, 7 insertions(+), 6 deletions(-) (limited to 'src/nvim/api') diff --git a/src/nvim/api/autocmd.c b/src/nvim/api/autocmd.c index f3b5aa780a..62fb5da9e4 100644 --- a/src/nvim/api/autocmd.c +++ b/src/nvim/api/autocmd.c @@ -373,13 +373,14 @@ cleanup: /// - callback (`function|string?`) Lua function (or Vimscript function name, if string) /// called when the event(s) is triggered. Lua callback can return |lua-truthy| to delete /// the autocommand. Callback receives one argument, a table with keys: [event-args]() -/// - buf: (`number`) [] +/// - buf: (`integer`) [] /// - data: (`any`) Arbitrary data passed from [nvim_exec_autocmds()] [event-data]() /// - event: (`vim.api.keyset.events`) Name of the triggered event |autocmd-events| /// - file: (`string`) [] (not expanded to a full path) -/// - group: (`number?`) Group id, if any -/// - id: (`number`) Autocommand id +/// - group: (`integer?`) Group id, if any +/// - id: (`integer`) Autocommand id /// - match: (`string`) [] (expanded to a full path) +/// - win: (`integer`) id of the window for the event, see |window-ID| /// - command (string?) Vim command executed on event. Not allowed with {callback}. /// - desc (`string?`) Description (for documentation and troubleshooting). /// - group (`string|integer?`) Group name or id to match against. @@ -764,8 +765,8 @@ void nvim_exec_autocmds(Object event, Dict(exec_autocmds) *opts, Arena *arena, E FOREACH_ITEM(patterns, pat, { char *fname = !has_buf ? pat.data.string.data : NULL; - did_aucmd |= apply_autocmds_group(event_nr, fname, NULL, true, au_group, b, NULL, data, - has_buf); + did_aucmd |= apply_autocmds_group(event_nr, fname, NULL, true, au_group, b, curwin, NULL, + data, has_buf); }) }) diff --git a/src/nvim/api/win_config.c b/src/nvim/api/win_config.c index c7f3047a64..12ee0ccb61 100644 --- a/src/nvim/api/win_config.c +++ b/src/nvim/api/win_config.c @@ -299,7 +299,7 @@ Window nvim_open_win(Buffer buf, Boolean enter, Dict(win_config) *config, Error const bool result = ctx_switch(&switchwin, wp, tp, NULL, kCtxNoDisplay); assert(result); (void)result; - if (apply_autocmds(EVENT_WINNEW, NULL, NULL, false, curbuf)) { + if (apply_autocmds_win(EVENT_WINNEW, NULL, NULL, false, curbuf, wp)) { tp = win_find_tabpage(wp); } ctx_restore(&switchwin); -- cgit