diff options
author | zeertzjq <zeertzjq@outlook.com> | 2023-01-24 18:31:07 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-01-24 18:31:07 +0800 |
commit | c6ab8dfc15e0f6f1a805ce2145e2b4f0072b33d1 (patch) | |
tree | 0e39f9d3c235f63aa7ee57dc7f4ce237b7d3c9f7 /src/nvim/window.c | |
parent | 39630265c476e64b2a544155e52b7a133222a551 (diff) | |
download | rneovim-c6ab8dfc15e0f6f1a805ce2145e2b4f0072b33d1.tar.gz rneovim-c6ab8dfc15e0f6f1a805ce2145e2b4f0072b33d1.tar.bz2 rneovim-c6ab8dfc15e0f6f1a805ce2145e2b4f0072b33d1.zip |
revert: "refactor(win_close): remove "force", don't pass on "free_buf" (#21921)" (#21979)
This reverts commit 0371d0f7afa5e01dd2ac8bbd3abcf0f7454872b3.
> 'bufhidden' option exists. I don't think we should assume autoclosing
windows are fine just because 'hidden' is set.
Diffstat (limited to 'src/nvim/window.c')
-rw-r--r-- | src/nvim/window.c | 46 |
1 files changed, 34 insertions, 12 deletions
diff --git a/src/nvim/window.c b/src/nvim/window.c index f4e84d4e81..05f84b5a91 100644 --- a/src/nvim/window.c +++ b/src/nvim/window.c @@ -368,7 +368,7 @@ newwindow: newtab = curtab; goto_tabpage_tp(oldtab, true, true); if (curwin == wp) { - win_close(curwin, false); + win_close(curwin, false, false); } if (valid_tabpage(newtab)) { goto_tabpage_tp(newtab, true, true); @@ -517,7 +517,7 @@ wingotofile: RESET_BINDING(curwin); if (do_ecmd(0, ptr, NULL, NULL, ECMD_LASTL, ECMD_HIDE, NULL) == FAIL) { // 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); } else if (nchar == 'F' && lnum >= 0) { curwin->w_cursor.lnum = lnum; @@ -2491,7 +2491,7 @@ void close_windows(buf_T *buf, bool keep_curwin) for (win_T *wp = lastwin; wp != NULL && (is_aucmd_win(lastwin) || !one_window(wp));) { if (wp->w_buffer == buf && (!keep_curwin || wp != curwin) && !(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. break; } @@ -2567,6 +2567,24 @@ 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); } +/// Check if floating windows in the current tab can be closed. +/// Do not call this when the autocommand window is in use! +/// +/// @return true if all floating windows can be closed +static bool can_close_floating_windows(void) +{ + assert(!is_aucmd_win(lastwin)); + for (win_T *wp = lastwin; wp->w_floating; wp = wp->w_prev) { + 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. /// /// @param win window to close @@ -2658,7 +2676,7 @@ static void win_close_buffer(win_T *win, bool free_buf, bool abort_if_last) // // Called by :quit, :close, :xit, :wq and findtag(). // 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) { tabpage_T *prev_curtab = curtab; frame_T *win_frame = win->w_floating ? NULL : win->w_frame->fr_parent; @@ -2682,14 +2700,18 @@ int win_close(win_T *win, bool free_buf) emsg(_("E814: Cannot close window, only autocmd window would remain")); return FAIL; } - // close the last window until the there are no floating windows - while (lastwin->w_floating) { - buf_T *buf = lastwin->w_buffer; - bool need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1); - if (win_close(lastwin, !need_hide && !buf_hide(buf)) == FAIL) { - // If closing the window fails give up, to avoid looping forever. - return FAIL; + if (force || can_close_floating_windows()) { + // close the last window until the there are no floating windows + while (lastwin->w_floating) { + // `force` flag isn't actually used when closing a floating window. + if (win_close(lastwin, free_buf, true) == FAIL) { + // If closing the window fails give up, to avoid looping forever. + return FAIL; + } } + } else { + emsg(e_floatonly); + return FAIL; } } @@ -3879,7 +3901,7 @@ void close_others(int message, int forceit) 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) { |