feat(api): allow win_hide to close cmdwin or non-previous windows

This aligns its behaviour better with `nvim_win_close`.

Note that `:hide` is actually incapable of closing the cmdwin, unlike `:close`
and `:quit`, so this is a bit of a difference in behaviour.
This commit is contained in:
Sean Dewar
2023-07-24 14:19:01 +01:00
parent 5d921e28c1
commit 472271199e
5 changed files with 41 additions and 15 deletions

View File

@ -2897,7 +2897,7 @@ nvim_win_hide({window}) *nvim_win_hide()*
or |nvim_win_close()|, which will close the buffer. or |nvim_win_close()|, which will close the buffer.
Attributes: ~ Attributes: ~
not allowed when |textlock| is active or in the |cmdwin| not allowed when |textlock| is active
Parameters: ~ Parameters: ~
• {window} Window handle, or 0 for current window • {window} Window handle, or 0 for current window

View File

@ -362,10 +362,10 @@ Boolean nvim_win_is_valid(Window window)
/// @param[out] err Error details, if any /// @param[out] err Error details, if any
void nvim_win_hide(Window window, Error *err) void nvim_win_hide(Window window, Error *err)
FUNC_API_SINCE(7) FUNC_API_SINCE(7)
FUNC_API_TEXTLOCK FUNC_API_TEXTLOCK_ALLOW_CMDWIN
{ {
win_T *win = find_window_by_handle(window, err); win_T *win = find_window_by_handle(window, err);
if (!win) { if (!win || !can_close_in_cmdwin(win, err)) {
return; return;
} }
@ -397,20 +397,10 @@ void nvim_win_close(Window window, Boolean force, Error *err)
FUNC_API_TEXTLOCK_ALLOW_CMDWIN FUNC_API_TEXTLOCK_ALLOW_CMDWIN
{ {
win_T *win = find_window_by_handle(window, err); win_T *win = find_window_by_handle(window, err);
if (!win) { if (!win || !can_close_in_cmdwin(win, err)) {
return; return;
} }
if (cmdwin_type != 0) {
if (win == curwin) {
cmdwin_result = Ctrl_C;
return;
} else if (win == cmdwin_old_curwin) {
api_set_error(err, kErrorTypeException, "%s", e_cmdwin);
return;
}
}
tabpage_T *tabpage = win_find_tabpage(win); tabpage_T *tabpage = win_find_tabpage(win);
TryState tstate; TryState tstate;
try_enter(&tstate); try_enter(&tstate);

View File

@ -2670,6 +2670,23 @@ static bool can_close_floating_windows(void)
return true; return true;
} }
/// @return true if, considering the cmdwin, `win` is safe to close.
/// If false and `win` is the cmdwin, it is closed; otherwise, `err` is set.
bool can_close_in_cmdwin(win_T *win, Error *err)
FUNC_ATTR_NONNULL_ALL
{
if (cmdwin_type != 0) {
if (win == curwin) {
cmdwin_result = Ctrl_C;
return false;
} else if (win == cmdwin_old_curwin) {
api_set_error(err, kErrorTypeException, "%s", e_cmdwin);
return false;
}
}
return true;
}
/// Close the possibly last window in a tab page. /// Close the possibly last window in a tab page.
/// ///
/// @param win window to close /// @param win window to close

View File

@ -615,6 +615,24 @@ describe('API/win', function()
eq({oldwin}, meths.list_wins()) eq({oldwin}, meths.list_wins())
eq({oldbuf}, meths.list_bufs()) eq({oldbuf}, meths.list_bufs())
end) end)
it('in the cmdwin', function()
feed('q:')
-- Can close the cmdwin.
meths.win_hide(0)
eq('', funcs.getcmdwintype())
local old_win = meths.get_current_win()
local other_win = meths.open_win(0, false, {
relative='win', row=3, col=3, width=12, height=3
})
feed('q:')
-- Cannot close the previous window.
eq('E11: Invalid in command-line window; <CR> executes, CTRL-C quits',
pcall_err(meths.win_hide, old_win))
-- Can close other windows.
meths.win_hide(other_win)
eq(false, meths.win_is_valid(other_win))
end)
end) end)
describe('text_height', function() describe('text_height', function()

View File

@ -73,9 +73,10 @@ describe('eval-API', function()
-- Some functions checking textlock (usually those that may change the current window or buffer) -- Some functions checking textlock (usually those that may change the current window or buffer)
-- also ought to not be usable in the cmdwin. -- also ought to not be usable in the cmdwin.
local old_win = meths.get_current_win()
feed("q:") feed("q:")
eq('E11: Invalid in command-line window; <CR> executes, CTRL-C quits', eq('E11: Invalid in command-line window; <CR> executes, CTRL-C quits',
pcall_err(meths.win_hide, 0)) pcall_err(meths.set_current_win, old_win))
-- But others, like nvim_buf_set_lines(), which just changes text, is OK. -- But others, like nvim_buf_set_lines(), which just changes text, is OK.
curbufmeths.set_lines(0, -1, 1, {"wow!"}) curbufmeths.set_lines(0, -1, 1, {"wow!"})