From 5ca6cf55f9c772f5c691b08dc49581f27f88e8f9 Mon Sep 17 00:00:00 2001 From: glacambre Date: Sat, 11 Feb 2023 13:51:33 +0100 Subject: fix(helpers): restore channel id after a call to WITH_SCRIPT_CONTEXT In https://github.com/neovim/neovim/pull/22214, init_default_autocmds has been turned into a lua function call to nvim_create_augroup and nvim_create_autocmd. This introduced a strange regression: a test in vim_spec.lua started failing with its last_set_chan value switching from 0 to -9223372036854775808. It turns out that -9223372036854775808 is the value of LUA_INTERNAL_CALL and would be inherited as last_set_chan by options set from the command line due to the WITH_SCRIPT_CONTEXT macro not restoring the channel id (WITH_SCRIPT_CONTEXT is used by nvim_create_augroup). --- src/nvim/api/private/helpers.h | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src') diff --git a/src/nvim/api/private/helpers.h b/src/nvim/api/private/helpers.h index ec97ba9ec6..b70452d7cb 100644 --- a/src/nvim/api/private/helpers.h +++ b/src/nvim/api/private/helpers.h @@ -175,11 +175,13 @@ typedef struct { #define WITH_SCRIPT_CONTEXT(channel_id, code) \ do { \ const sctx_T save_current_sctx = current_sctx; \ + const uint64_t save_channel_id = current_channel_id; \ current_sctx.sc_sid = \ (channel_id) == LUA_INTERNAL_CALL ? SID_LUA : SID_API_CLIENT; \ current_sctx.sc_lnum = 0; \ current_channel_id = channel_id; \ code; \ + current_channel_id = save_channel_id; \ current_sctx = save_current_sctx; \ } while (0); -- cgit From c5b34fa55483d84d1de32937ffff0b7cf1aeba78 Mon Sep 17 00:00:00 2001 From: glacambre Date: Sat, 11 Feb 2023 09:45:11 +0100 Subject: refactor: move init_default_autocmds to lua The original motivation for this change came from developping https://github.com/neovim/neovim/pull/22159, which will require adding more autocommand creation to Neovim's startup sequence. This change requires lightly editing a test that expected no autocommand to have been created from lua. --- src/nvim/autocmd.c | 29 ----------------------------- src/nvim/main.c | 9 +++------ 2 files changed, 3 insertions(+), 35 deletions(-) (limited to 'src') diff --git a/src/nvim/autocmd.c b/src/nvim/autocmd.c index 0485fbcdb0..4cc75fa9a6 100644 --- a/src/nvim/autocmd.c +++ b/src/nvim/autocmd.c @@ -2759,32 +2759,3 @@ void do_autocmd_focusgained(bool gained) recursive = false; } - -static void define_autocmd(event_T event, char *pat, char *group, bool once, bool nested, char *cmd) -{ - AucmdExecutable exec = AUCMD_EXECUTABLE_INIT; - exec.type = CALLABLE_EX; - exec.callable.cmd = cmd; // autocmd_register() makes a copy - int group_id = augroup_add(group); - autocmd_register(0, event, pat, (int)strlen(pat), group_id, once, nested, NULL, exec); -} - -/// initialization of default autocmds -void init_default_autocmds(void) -{ - // open terminals when opening files that start with term:// -#define PROTO "term://" - define_autocmd(EVENT_BUFREADCMD, PROTO "*", "nvim_terminal", false, true, - "if !exists('b:term_title')|call termopen(" - // Capture the command string - "matchstr(expand(\"\"), " - "'\\c\\m" PROTO "\\%(.\\{-}//\\%(\\d\\+:\\)\\?\\)\\?\\zs.*'), " - // capture the working directory - "{'cwd': expand(get(matchlist(expand(\"\"), " - "'\\c\\m" PROTO "\\(.\\{-}\\)//'), 1, ''))})" - "|endif"); -#undef PROTO - // limit syntax synchronization in the command window - define_autocmd(EVENT_CMDWINENTER, "[:>]", "nvim_cmdwin", false, false, - "syntax sync minlines=1 maxlines=1"); -} diff --git a/src/nvim/main.c b/src/nvim/main.c index 2bbe70784d..1f155aa343 100644 --- a/src/nvim/main.c +++ b/src/nvim/main.c @@ -404,19 +404,16 @@ int main(int argc, char **argv) open_script_files(¶ms); - // Default mappings (incl. menus) + // Default mappings (incl. menus) & autocommands Error err = ERROR_INIT; - Object o = NLUA_EXEC_STATIC("return vim._init_default_mappings()", + Object o = NLUA_EXEC_STATIC("return vim._init_defaults()", (Array)ARRAY_DICT_INIT, &err); assert(!ERROR_SET(&err)); api_clear_error(&err); assert(o.type == kObjectTypeNil); api_free_object(o); - TIME_MSG("init default mappings"); - - init_default_autocmds(); - TIME_MSG("init default autocommands"); + TIME_MSG("init default mappings & autocommands"); bool vimrc_none = strequal(params.use_vimrc, "NONE"); -- cgit