diff options
author | TJ DeVries <devries.timothyj@gmail.com> | 2022-03-30 11:59:36 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-30 17:59:36 +0200 |
commit | 7fb2310edb8f2fae1ddd175ef4bd6508ca3ce7e3 (patch) | |
tree | c7057721653d9b13356d304a366316ac38dae85e | |
parent | 1217694f21cff2953e6c56be2157365daf7078eb (diff) | |
download | rneovim-7fb2310edb8f2fae1ddd175ef4bd6508ca3ce7e3.tar.gz rneovim-7fb2310edb8f2fae1ddd175ef4bd6508ca3ce7e3.tar.bz2 rneovim-7fb2310edb8f2fae1ddd175ef4bd6508ca3ce7e3.zip |
fix: set nested before executing callback (#17801)
-rw-r--r-- | src/nvim/autocmd.c | 7 | ||||
-rw-r--r-- | test/functional/api/autocmd_spec.lua | 27 |
2 files changed, 32 insertions, 2 deletions
diff --git a/src/nvim/autocmd.c b/src/nvim/autocmd.c index a36f2c97b5..d4af05b961 100644 --- a/src/nvim/autocmd.c +++ b/src/nvim/autocmd.c @@ -2022,6 +2022,11 @@ char_u *getnextac(int c, void *cookie, int indent, bool do_concat) verbose_leave_scroll(); } + // Make sure to set autocmd_nested before executing + // lua code, so that it works properly + autocmd_nested = ac->nested; + current_sctx = ac->script_ctx; + if (ac->exec.type == CALLABLE_CB) { typval_T argsin = TV_INITIAL_VALUE; typval_T rettv = TV_INITIAL_VALUE; @@ -2052,8 +2057,6 @@ char_u *getnextac(int c, void *cookie, int indent, bool do_concat) if (oneshot) { aucmd_del(ac); } - autocmd_nested = ac->nested; - current_sctx = ac->script_ctx; if (ac->last) { acp->nextcmd = NULL; } else { diff --git a/test/functional/api/autocmd_spec.lua b/test/functional/api/autocmd_spec.lua index 3b14ae9bf7..b8fbd4f9a5 100644 --- a/test/functional/api/autocmd_spec.lua +++ b/test/functional/api/autocmd_spec.lua @@ -334,6 +334,33 @@ describe('autocmd api', function() local aus2 = meths.get_autocmds { group = auname, event = "InsertEnter" } eq(0, #aus2) end) + + it('should respect nested', function() + local bufs = exec_lua [[ + local count = 0 + vim.api.nvim_create_autocmd("BufNew", { + once = false, + nested = true, + callback = function() + count = count + 1 + if count > 5 then + return true + end + + vim.cmd(string.format("new README_%s.md", count)) + end + }) + + vim.cmd "new First.md" + + return vim.api.nvim_list_bufs() + ]] + + -- 1 for the first buffer + -- 2 for First.md + -- 3-7 for the 5 we make in the autocmd + eq({1, 2, 3, 4, 5, 6, 7}, bufs) + end) end) describe('groups', function() |