commit 5d921e28c1cc33eced22bbfa823460ca241e3dc1
parent 6b4970f6e0ac36021b2a8bd0533f5078040d31f7
Author: Sean Dewar <seandewar@users.noreply.github.com>
Date: Sun, 23 Jul 2023 23:10:28 +0100
feat(api): allow win_close in cmdwin to close wins except previous
Disallow closing the previous window from `nvim_win_close`, as this will cause
issues.
Again, no telling how safe this is. It also requires exposing old_curwin. :/
Also note that it's possible for the `&cmdheight` to change if, for example,
there are 2 tabpages and `nvim_win_close` is used to close the last window in
the other tabpage while `&stal` is 1. This is addressed in a later commit.
Diffstat:
4 files changed, 17 insertions(+), 6 deletions(-)
diff --git a/src/nvim/api/window.c b/src/nvim/api/window.c
@@ -404,10 +404,11 @@ void nvim_win_close(Window window, Boolean force, Error *err)
if (cmdwin_type != 0) {
if (win == curwin) {
cmdwin_result = Ctrl_C;
- } else {
+ return;
+ } else if (win == cmdwin_old_curwin) {
api_set_error(err, kErrorTypeException, "%s", e_cmdwin);
+ return;
}
- return;
}
tabpage_T *tabpage = win_find_tabpage(win);
diff --git a/src/nvim/ex_getln.c b/src/nvim/ex_getln.c
@@ -4398,6 +4398,7 @@ static int open_cmdwin(void)
// Set "cmdwin_type" before any autocommands may mess things up.
cmdwin_type = get_cmdline_type();
cmdwin_level = ccline.level;
+ cmdwin_old_curwin = old_curwin;
// Create empty command-line buffer.
if (buf_open_scratch(0, _("[Command Line]")) == FAIL) {
@@ -4405,6 +4406,7 @@ static int open_cmdwin(void)
win_close(curwin, true, false);
ga_clear(&winsizes);
cmdwin_type = 0;
+ cmdwin_old_curwin = NULL;
return Ctrl_C;
}
// Command-line buffer has bufhidden=wipe, unlike a true "scratch" buffer.
@@ -4501,6 +4503,7 @@ static int open_cmdwin(void)
cmdwin_type = 0;
cmdwin_level = 0;
+ cmdwin_old_curwin = NULL;
exmode_active = save_exmode;
diff --git a/src/nvim/globals.h b/src/nvim/globals.h
@@ -831,6 +831,7 @@ EXTERN bool km_startsel INIT(= false);
EXTERN int cmdwin_type INIT(= 0); ///< type of cmdline window or 0
EXTERN int cmdwin_result INIT(= 0); ///< result of cmdline window or 0
EXTERN int cmdwin_level INIT(= 0); ///< cmdline recursion level
+EXTERN win_T *cmdwin_old_curwin INIT(= NULL); ///< curwin before opening cmdline window or NULL
EXTERN char no_lines_msg[] INIT(= N_("--No lines in buffer--"));
diff --git a/test/functional/api/window_spec.lua b/test/functional/api/window_spec.lua
@@ -540,15 +540,21 @@ describe('API/win', function()
command('split')
eq(2, #meths.list_wins())
local oldwin = meths.get_current_win()
+ local otherwin = meths.open_win(0, false, {
+ relative='editor', row=10, col=10, width=10, height=10,
+ })
-- Open cmdline-window.
feed('q:')
- eq(3, #meths.list_wins())
+ eq(4, #meths.list_wins())
eq(':', funcs.getcmdwintype())
- -- Vim: not allowed to close other windows from cmdline-window.
+ -- Not allowed to close previous window from cmdline-window.
eq('E11: Invalid in command-line window; <CR> executes, CTRL-C quits',
- pcall_err(meths.win_close, oldwin, true))
+ pcall_err(meths.win_close, oldwin, true))
+ -- Closing other windows is fine.
+ meths.win_close(otherwin, true)
+ eq(false, meths.win_is_valid(otherwin))
-- Close cmdline-window.
- meths.win_close(0,true)
+ meths.win_close(0, true)
eq(2, #meths.list_wins())
eq('', funcs.getcmdwintype())
end)