From aa4e47f704c53ab1d825260d2bf34e2872e3ca89 Mon Sep 17 00:00:00 2001 From: Sean Dewar Date: Fri, 23 Jun 2023 22:32:07 +0100 Subject: fix(api): disallow some more functions during textlock Problem: nvim_buf_set_text(), nvim_open_term() and termopen() all change buffer text, which is forbidden during textlock. Additionally, nvim_open_term() and termopen() may be used to convert the cmdwin buffer into a terminal buffer, which is weird. Solution: Allow nvim_buf_set_text() and nvim_open_term() in the cmdwin, but disallow nvim_open_term() from converting the cmdwin buffer into a terminal buffer. termopen() is not allowed in the cmdwin (as it always operates on curbuf), so just check text_locked(). Also happens to improve the error in #21055: nvim_buf_set_text() was callable during textlock, but happened to check textlock indirectly via u_save(); however, this caused the error to be overwritten by an unhelpful "Failed to save undo information" message when msg_list == NULL (e.g: an `` mapping invoked outside of do_cmdline()). --- test/functional/vimscript/api_functions_spec.lua | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'test/functional/vimscript/api_functions_spec.lua') diff --git a/test/functional/vimscript/api_functions_spec.lua b/test/functional/vimscript/api_functions_spec.lua index 683ae2446c..6548548a9e 100644 --- a/test/functional/vimscript/api_functions_spec.lua +++ b/test/functional/vimscript/api_functions_spec.lua @@ -54,11 +54,23 @@ describe('eval-API', function() matches('Vim%(call%):E5555: API call: E565: Not allowed to change text or change window$', pcall_err(command, "normal! yy")) + command("autocmd TextYankPost ++once call nvim_open_term(0, {})") + matches('Vim%(call%):E5555: API call: E565: Not allowed to change text or change window$', + pcall_err(command, "normal! yy")) + -- Functions checking textlock should also not be usable from mappings. command("inoremap nvim_win_close(0, 1)") eq('Vim(normal):E5555: API call: E565: Not allowed to change text or change window', pcall_err(command, [[execute "normal i\"]])) + -- Text-changing functions gave a "Failed to save undo information" error when called from an + -- mapping outside do_cmdline() (msg_list == NULL), so use feed() to test this. + command("inoremap nvim_buf_set_text(0, 0, 0, 0, 0, ['hi'])") + meths.set_vvar('errmsg', '') + feed("i") + eq('E5555: API call: E565: Not allowed to change text or change window', + meths.get_vvar('errmsg')) + -- Some functions checking textlock (usually those that may change the current window or buffer) -- also ought to not be usable in the cmdwin. feed("q:") @@ -68,6 +80,15 @@ describe('eval-API', function() -- But others, like nvim_buf_set_lines(), which just changes text, is OK. curbufmeths.set_lines(0, -1, 1, {"wow!"}) eq({'wow!'}, curbufmeths.get_lines(0, -1, 1)) + + -- Turning the cmdwin buffer into a terminal buffer would be pretty weird. + eq('E11: Invalid in command-line window; executes, CTRL-C quits', + pcall_err(meths.open_term, 0, {})) + + -- But turning a different buffer into a terminal from the cmdwin is OK. + local term_buf = meths.create_buf(false, true) + meths.open_term(term_buf, {}) + eq('terminal', meths.get_option_value("buftype", {buf = term_buf})) end) it("use buffer numbers and windows ids as handles", function() -- cgit