Skip to content

Commit

Permalink
fix(aucmd_win): always make aucmd_win the last window
Browse files Browse the repository at this point in the history
  • Loading branch information
zeertzjq committed Mar 23, 2022
1 parent 7735163 commit 89712dc
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/nvim/api/win_config.c
Expand Up @@ -150,7 +150,7 @@ Window nvim_open_win(Buffer buffer, Boolean enter, Dict(float_config) *config, E
if (!parse_float_config(config, &fconfig, false, true, err)) {
return 0;
}
win_T *wp = win_new_float(NULL, fconfig, err);
win_T *wp = win_new_float(NULL, false, fconfig, err);
if (!wp) {
return 0;
}
Expand Down Expand Up @@ -200,7 +200,7 @@ void nvim_win_set_config(Window window, Dict(float_config) *config, Error *err)
return;
}
if (new_float) {
if (!win_new_float(win, fconfig, err)) {
if (!win_new_float(win, false, fconfig, err)) {
return;
}
redraw_later(win, NOT_VALID);
Expand Down
20 changes: 11 additions & 9 deletions src/nvim/window.c
Expand Up @@ -560,7 +560,7 @@ void do_window(int nchar, long Prenum, int xchar)
config.height = curwin->w_height;
config.external = true;
Error err = ERROR_INIT;
if (!win_new_float(curwin, config, &err)) {
if (!win_new_float(curwin, false, config, &err)) {
emsg(err.msg);
api_clear_error(&err);
beep_flush();
Expand Down Expand Up @@ -629,16 +629,18 @@ void win_set_buf(Window window, Buffer buffer, bool noautocmd, Error *err)

/// Create a new float.
///
/// if wp == NULL allocate a new window, otherwise turn existing window into a
/// float. It must then already belong to the current tabpage!
///
/// config must already have been validated!
win_T *win_new_float(win_T *wp, FloatConfig fconfig, Error *err)
/// @param wp if NULL, allocate a new window, otherwise turn existing window into a float.
/// It must then already belong to the current tabpage!
/// @param last make the window the last one in the window list.
/// Only used when allocating the autocommand window.
/// @param config must already have been validated!
win_T *win_new_float(win_T *wp, bool last, FloatConfig fconfig, Error *err)
{
if (wp == NULL) {
wp = win_alloc(lastwin_nofloating(), false);
wp = win_alloc(last ? lastwin : lastwin_nofloating(), false);
win_init(wp, curwin, 0);
} else {
assert(!last);
assert(!wp->w_floating);
if (firstwin == wp && lastwin_nofloating() == wp) {
// last non-float
Expand Down Expand Up @@ -2543,7 +2545,7 @@ int win_close(win_T *win, bool free_buf, bool force)
emsg(_(e_autocmd_close));
return FAIL;
}
if ((firstwin == aucmd_win || lastwin == aucmd_win) && one_window()) {
if (lastwin == aucmd_win && one_window()) {
emsg(_("E814: Cannot close window, only autocmd window would remain"));
return FAIL;
}
Expand Down Expand Up @@ -3844,7 +3846,7 @@ void win_alloc_aucmd_win(void)
fconfig.width = Columns;
fconfig.height = 5;
fconfig.focusable = false;
aucmd_win = win_new_float(NULL, fconfig, &err);
aucmd_win = win_new_float(NULL, true, fconfig, &err);
aucmd_win->w_buffer->b_nwindows--;
RESET_BINDING(aucmd_win);
}
Expand Down
19 changes: 19 additions & 0 deletions test/functional/autocmd/autocmd_spec.lua
Expand Up @@ -398,6 +398,25 @@ describe('autocmd', function()
]])
end)

describe('closing last non-floating window in tab from `aucmd_win`', function()
before_each(function()
command('edit Xa.txt')
command('tabnew Xb.txt')
command('autocmd BufAdd Xa.txt 1close')
end)

it('gives E814 when there are no other floating windows', function()
eq('Vim(close):E814: Cannot close window, only autocmd window would remain',
pcall_err(command, 'doautoall BufAdd'))
end)

it('gives E814 when there are other floating windows', function()
meths.open_win(0, true, {width = 10, height = 10, relative = 'editor', row = 10, col = 10})
eq('Vim(close):E814: Cannot close window, only autocmd window would remain',
pcall_err(command, 'doautoall BufAdd'))
end)
end)

it(':doautocmd does not warn "No matching autocommands" #10689', function()
local screen = Screen.new(32, 3)
screen:attach()
Expand Down

0 comments on commit 89712dc

Please sign in to comment.