fix: close floating windows when calling win_close()

This commit is contained in:
Rom Grk
2021-04-17 17:33:59 -04:00
committed by Lewis Russell
parent a73360a09a
commit 85ae04dbfd
10 changed files with 80 additions and 31 deletions

View File

@@ -395,7 +395,7 @@ void nvim_win_hide(Window window, Error *err)
TryState tstate; TryState tstate;
try_enter(&tstate); try_enter(&tstate);
if (tabpage == curtab) { if (tabpage == curtab) {
win_close(win, false); win_close(win, false, false);
} else { } else {
win_close_othertab(win, false, tabpage); win_close_othertab(win, false, tabpage);
} }

View File

@@ -846,7 +846,7 @@ void goto_buffer(exarg_T *eap, int start, int dir, int count)
enter_cleanup(&cs); enter_cleanup(&cs);
// Quitting means closing the split window, nothing else. // Quitting means closing the split window, nothing else.
win_close(curwin, true); win_close(curwin, true, false);
swap_exists_action = SEA_NONE; swap_exists_action = SEA_NONE;
swap_exists_did_quit = true; swap_exists_did_quit = true;
@@ -1237,7 +1237,7 @@ int do_buffer(int action, int start, int dir, int count, int forceit)
while (buf == curbuf while (buf == curbuf
&& !(curwin->w_closing || curwin->w_buffer->b_locked > 0) && !(curwin->w_closing || curwin->w_buffer->b_locked > 0)
&& (!ONE_WINDOW || first_tabpage->tp_next != NULL)) { && (!ONE_WINDOW || first_tabpage->tp_next != NULL)) {
if (win_close(curwin, false) == FAIL) { if (win_close(curwin, false, false) == FAIL) {
break; break;
} }
} }
@@ -4822,7 +4822,7 @@ void do_arg_all(int count, int forceit, int keep_tabs)
&& (first_tabpage->tp_next == NULL || !had_tab)) { && (first_tabpage->tp_next == NULL || !had_tab)) {
use_firstwin = true; use_firstwin = true;
} else { } else {
win_close(wp, !buf_hide(buf) && !bufIsChanged(buf)); win_close(wp, !buf_hide(buf) && !bufIsChanged(buf), false);
// check if autocommands removed the next window // check if autocommands removed the next window
if (!win_valid(wpnext)) { if (!win_valid(wpnext)) {
// start all over... // start all over...
@@ -5013,7 +5013,7 @@ void ex_buffer_all(exarg_T *eap)
&& !ONE_WINDOW && !ONE_WINDOW
&& !(wp->w_closing && !(wp->w_closing
|| wp->w_buffer->b_locked > 0)) { || wp->w_buffer->b_locked > 0)) {
win_close(wp, false); win_close(wp, false, false);
wpnext = firstwin; // just in case an autocommand does wpnext = firstwin; // just in case an autocommand does
// something strange with windows // something strange with windows
tpnext = first_tabpage; // start all over... tpnext = first_tabpage; // start all over...
@@ -5094,7 +5094,7 @@ void ex_buffer_all(exarg_T *eap)
enter_cleanup(&cs); enter_cleanup(&cs);
// User selected Quit at ATTENTION prompt; close this window. // User selected Quit at ATTENTION prompt; close this window.
win_close(curwin, true); win_close(curwin, true, false);
open_wins--; open_wins--;
swap_exists_action = SEA_NONE; swap_exists_action = SEA_NONE;
swap_exists_did_quit = true; swap_exists_did_quit = true;
@@ -5136,7 +5136,7 @@ void ex_buffer_all(exarg_T *eap)
// BufWrite Autocommands made the window invalid, start over // BufWrite Autocommands made the window invalid, start over
wp = lastwin; wp = lastwin;
} else if (r) { } else if (r) {
win_close(wp, !buf_hide(wp->w_buffer)); win_close(wp, !buf_hide(wp->w_buffer), false);
open_wins--; open_wins--;
wp = lastwin; wp = lastwin;
} else { } else {

View File

@@ -5856,7 +5856,7 @@ void ex_helpclose(exarg_T *eap)
{ {
FOR_ALL_WINDOWS_IN_TAB(win, curtab) { FOR_ALL_WINDOWS_IN_TAB(win, curtab) {
if (bt_help(win->w_buffer)) { if (bt_help(win->w_buffer)) {
win_close(win, false); win_close(win, false, eap->forceit);
return; return;
} }
} }

View File

@@ -6619,7 +6619,7 @@ static void ex_quit(exarg_T *eap)
} }
not_exiting(); not_exiting();
// close window; may free buffer // close window; may free buffer
win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit); win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit, eap->forceit);
} }
} }
@@ -6734,7 +6734,7 @@ void ex_win_close(int forceit, win_T *win, tabpage_T *tp)
// free buffer when not hiding it or when it's a scratch buffer // free buffer when not hiding it or when it's a scratch buffer
if (tp == NULL) { if (tp == NULL) {
win_close(win, !need_hide && !buf_hide(buf)); win_close(win, !need_hide && !buf_hide(buf), forceit);
} else { } else {
win_close_othertab(win, !need_hide && !buf_hide(buf), tp); win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
} }
@@ -6898,7 +6898,7 @@ static void ex_hide(exarg_T *eap)
// ":hide" or ":hide | cmd": hide current window // ":hide" or ":hide | cmd": hide current window
if (!eap->skip) { if (!eap->skip) {
if (eap->addr_count == 0) { if (eap->addr_count == 0) {
win_close(curwin, false); // don't free buffer win_close(curwin, false, eap->forceit); // don't free buffer
} else { } else {
int winnr = 0; int winnr = 0;
win_T *win = NULL; win_T *win = NULL;
@@ -6913,7 +6913,7 @@ static void ex_hide(exarg_T *eap)
if (win == NULL) { if (win == NULL) {
win = lastwin; win = lastwin;
} }
win_close(win, false); win_close(win, false, eap->forceit);
} }
} }
} }
@@ -6971,7 +6971,7 @@ static void ex_exit(exarg_T *eap)
} }
not_exiting(); not_exiting();
// Quit current window, may free the buffer. // Quit current window, may free the buffer.
win_close(curwin, !buf_hide(curwin->w_buffer)); win_close(curwin, !buf_hide(curwin->w_buffer), eap->forceit);
} }
} }
@@ -7572,7 +7572,7 @@ void do_exedit(exarg_T *eap, win_T *old_curwin)
// Reset the error/interrupt/exception state here so that // Reset the error/interrupt/exception state here so that
// aborting() returns FALSE when closing a window. // aborting() returns FALSE when closing a window.
enter_cleanup(&cs); enter_cleanup(&cs);
win_close(curwin, !need_hide && !buf_hide(curbuf)); win_close(curwin, !need_hide && !buf_hide(curbuf), false);
// Restore the error/interrupt/exception state if not // Restore the error/interrupt/exception state if not
// discarded by a new aborting error, interrupt, or // discarded by a new aborting error, interrupt, or

View File

@@ -6563,7 +6563,7 @@ static int open_cmdwin(void)
set_bufref(&bufref, curbuf); set_bufref(&bufref, curbuf);
win_goto(old_curwin); win_goto(old_curwin);
if (win_valid(wp) && wp != curwin) { if (win_valid(wp) && wp != curwin) {
win_close(wp, true); win_close(wp, true, false);
} }
// win_close() may have already wiped the buffer when 'bh' is // win_close() may have already wiped the buffer when 'bh' is

View File

@@ -1560,7 +1560,7 @@ static void edit_buffers(mparm_T *parmp, char_u *cwd)
// When w_arg_idx is -1 remove the window (see create_windows()). // When w_arg_idx is -1 remove the window (see create_windows()).
if (curwin->w_arg_idx == -1) { if (curwin->w_arg_idx == -1) {
win_close(curwin, true); win_close(curwin, true, false);
advance = false; advance = false;
} }
@@ -1572,7 +1572,7 @@ static void edit_buffers(mparm_T *parmp, char_u *cwd)
// When w_arg_idx is -1 remove the window (see create_windows()). // When w_arg_idx is -1 remove the window (see create_windows()).
if (curwin->w_arg_idx == -1) { if (curwin->w_arg_idx == -1) {
arg_idx++; arg_idx++;
win_close(curwin, true); win_close(curwin, true, false);
advance = false; advance = false;
continue; continue;
} }
@@ -1619,7 +1619,7 @@ static void edit_buffers(mparm_T *parmp, char_u *cwd)
did_emsg = FALSE; // avoid hit-enter prompt did_emsg = FALSE; // avoid hit-enter prompt
getout(1); getout(1);
} }
win_close(curwin, true); win_close(curwin, true, false);
advance = false; advance = false;
} }
if (arg_idx == GARGCOUNT - 1) { if (arg_idx == GARGCOUNT - 1) {

View File

@@ -3022,7 +3022,7 @@ static void qf_jump_newwin(qf_info_T *qi, int dir, int errornr, int forceit, boo
if (retval != OK) { if (retval != OK) {
if (opened_window) { if (opened_window) {
win_close(curwin, true); // Close opened window win_close(curwin, true, false); // Close opened window
} }
if (qf_ptr != NULL && qf_ptr->qf_fnum != 0) { if (qf_ptr != NULL && qf_ptr->qf_fnum != 0) {
// Couldn't open file, so put index back where it was. This could // Couldn't open file, so put index back where it was. This could
@@ -3577,7 +3577,7 @@ void ex_cclose(exarg_T *eap)
// Find existing quickfix window and close it. // Find existing quickfix window and close it.
win = qf_find_win(qi); win = qf_find_win(qi);
if (win != NULL) { if (win != NULL) {
win_close(win, false); win_close(win, false, false);
} }
} }
@@ -3651,7 +3651,7 @@ static int qf_open_new_cwindow(qf_info_T *qi, int height)
// win_split, so add a check to ensure that the win is still here // win_split, so add a check to ensure that the win is still here
if (IS_LL_STACK(qi) && !win_valid(win)) { if (IS_LL_STACK(qi) && !win_valid(win)) {
// close the window that was supposed to be for the loclist // close the window that was supposed to be for the loclist
win_close(curwin, false); win_close(curwin, false, false);
return FAIL; return FAIL;
} }
@@ -5767,7 +5767,7 @@ static void wipe_dummy_buffer(buf_T *buf, char_u *dirname_start)
if (firstwin->w_next != NULL) { if (firstwin->w_next != NULL) {
for (win_T *wp = firstwin; wp != NULL; wp = wp->w_next) { for (win_T *wp = firstwin; wp != NULL; wp = wp->w_next) {
if (wp->w_buffer == buf) { if (wp->w_buffer == buf) {
if (win_close(wp, false) == OK) { if (win_close(wp, false, false) == OK) {
did_one = true; did_one = true;
} }
break; break;

View File

@@ -2960,7 +2960,7 @@ static int jumpto_tag(const char_u *lbuf_arg, int forceit, int keep_help)
} else { } else {
RedrawingDisabled--; RedrawingDisabled--;
if (postponed_split) { // close the window if (postponed_split) { // close the window
win_close(curwin, false); win_close(curwin, false, false);
postponed_split = 0; postponed_split = 0;
} }
} }

View File

@@ -304,7 +304,7 @@ newwindow:
newtab = curtab; newtab = curtab;
goto_tabpage_tp(oldtab, true, true); goto_tabpage_tp(oldtab, true, true);
if (curwin == wp) { if (curwin == wp) {
win_close(curwin, false); win_close(curwin, false, false);
} }
if (valid_tabpage(newtab)) { if (valid_tabpage(newtab)) {
goto_tabpage_tp(newtab, true, true); goto_tabpage_tp(newtab, true, true);
@@ -449,7 +449,7 @@ wingotofile:
RESET_BINDING(curwin); RESET_BINDING(curwin);
if (do_ecmd(0, ptr, NULL, NULL, ECMD_LASTL, ECMD_HIDE, NULL) == FAIL) { if (do_ecmd(0, ptr, NULL, NULL, ECMD_LASTL, ECMD_HIDE, NULL) == FAIL) {
// Failed to open the file, close the window opened for it. // Failed to open the file, close the window opened for it.
win_close(curwin, false); win_close(curwin, false, false);
goto_tabpage_win(oldtab, oldwin); goto_tabpage_win(oldtab, oldwin);
} else if (nchar == 'F' && lnum >= 0) { } else if (nchar == 'F' && lnum >= 0) {
curwin->w_cursor.lnum = lnum; curwin->w_cursor.lnum = lnum;
@@ -2290,7 +2290,7 @@ void close_windows(buf_T *buf, int keep_curwin)
for (win_T *wp = firstwin; wp != NULL && !ONE_WINDOW;) { for (win_T *wp = firstwin; wp != NULL && !ONE_WINDOW;) {
if (wp->w_buffer == buf && (!keep_curwin || wp != curwin) if (wp->w_buffer == buf && (!keep_curwin || wp != curwin)
&& !(wp->w_closing || wp->w_buffer->b_locked > 0)) { && !(wp->w_closing || wp->w_buffer->b_locked > 0)) {
if (win_close(wp, false) == FAIL) { if (win_close(wp, false, false) == FAIL) {
// If closing the window fails give up, to avoid looping forever. // If closing the window fails give up, to avoid looping forever.
break; break;
} }
@@ -2368,6 +2368,22 @@ bool last_nonfloat(win_T *wp) FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT
return wp != NULL && firstwin == wp && !(wp->w_next && !wp->w_floating); return wp != NULL && firstwin == wp && !(wp->w_next && !wp->w_floating);
} }
/// Check if floating windows can be closed.
///
/// @return true if all floating windows can be closed
static bool can_close_floating_windows(tabpage_T *tab)
{
FOR_ALL_WINDOWS_IN_TAB(wp, tab) {
buf_T *buf = wp->w_buffer;
int need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
if (need_hide && !buf_hide(buf)) {
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
@@ -2432,7 +2448,7 @@ static bool close_last_window_tabpage(win_T *win, bool free_buf, tabpage_T *prev
// //
// Called by :quit, :close, :xit, :wq and findtag(). // Called by :quit, :close, :xit, :wq and findtag().
// Returns FAIL when the window was not closed. // Returns FAIL when the window was not closed.
int win_close(win_T *win, bool free_buf) int win_close(win_T *win, bool free_buf, bool force)
{ {
win_T *wp; win_T *wp;
bool other_buffer = false; bool other_buffer = false;
@@ -2462,9 +2478,18 @@ int win_close(win_T *win, bool free_buf)
} }
if ((firstwin == win && lastwin_nofloating() == win) if ((firstwin == win && lastwin_nofloating() == win)
&& lastwin->w_floating) { && lastwin->w_floating) {
// TODO(bfredl): we might close the float also instead if (force || can_close_floating_windows(curtab)) {
emsg(e_floatonly); win_T *nextwp;
return FAIL; for (win_T *wpp = firstwin; wpp != NULL; wpp = nextwp) {
nextwp = wpp->w_next;
if (wpp->w_floating) {
win_close(wpp, free_buf, force);
}
}
} else {
emsg(e_floatonly);
return FAIL;
}
} }
// When closing the last window in a tab page first go to another tab page // When closing the last window in a tab page first go to another tab page
@@ -3611,7 +3636,9 @@ void close_others(int message, int forceit)
continue; continue;
} }
} }
win_close(wp, !buf_hide(wp->w_buffer) && !bufIsChanged(wp->w_buffer)); win_close(wp,
!buf_hide(wp->w_buffer) && !bufIsChanged(wp->w_buffer),
false);
} }
if (message && !ONE_WINDOW) { if (message && !ONE_WINDOW) {

View File

@@ -417,6 +417,28 @@ describe('float window', function()
eq(winids, eval('winids')) eq(winids, eval('winids'))
end) end)
it('closed when the last non-float window is closed', function()
local tabpage = exec_lua([[
vim.cmd('edit ./src/nvim/main.c')
vim.cmd('tabedit %')
local buf = vim.api.nvim_create_buf(false, true)
local win = vim.api.nvim_open_win(buf, false, {
relative = 'win',
row = 1,
col = 1,
width = 10,
height = 2
})
vim.cmd('quit')
return vim.api.nvim_get_current_tabpage()
]])
eq(1, tabpage)
end)
local function with_ext_multigrid(multigrid) local function with_ext_multigrid(multigrid)
local screen local screen
before_each(function() before_each(function()