fix(cmdwin): handle split/tabpage

Problem:
- If cmdwin window is split, ENTER in one does not close the others.
- If cmdwin is put into a different tabpage via <c-w>T, it stops working
  (ENTER does not execute the cmd).

Solution:
- Close the buffer instead of the window.
- In the WinClosed handler, skip `M._cleanup()` unless this is the last
  cmdwin window.
This commit is contained in:
Justin M. Keyes
2026-06-29 23:19:52 +02:00
parent 2a4bb3eb37
commit 684371ba5d
3 changed files with 50 additions and 14 deletions

View File

@@ -101,15 +101,22 @@ function M.open(type, init_line, init_col)
caller_win = caller,
}
-- Clean up if the window is closed by other means (`:q`, `:close`, etc.).
-- Clean up when the (last-visible) cmdwin is closed by other means (`:q`, `:close`, etc.).
vim.api.nvim_create_autocmd({ 'WinClosed' }, {
buffer = buf,
once = true,
nested = true,
callback = function()
if state ~= nil then
M._cleanup()
callback = function(ev)
if state == nil then
return
end
local closing = tonumber(ev.match)
for _, w in ipairs(vim.fn.win_findbuf(buf)) do
if w ~= closing then
return -- Still visible elsewhere; keep cmdwin (and this autocmd) active.
end
end
M._cleanup()
return true -- Last cmdwin window gone; delete this autocmd.
end,
})
@@ -125,8 +132,8 @@ function M._cleanup()
state = nil
pcall(vim.api.nvim__cmdwin_set, '', 0) -- Clear the C-side globals.
pcall(vim.api.nvim_exec_autocmds, 'CmdwinLeave', { pattern = s.type, modeline = false })
if vim.api.nvim_win_is_valid(s.win) then
pcall(vim.api.nvim_win_close, s.win, true)
if vim.api.nvim_buf_is_valid(s.buf) then
pcall(vim.api.nvim_buf_delete, s.buf, { force = true })
end
if vim.api.nvim_win_is_valid(s.caller_win) then
pcall(vim.api.nvim_set_current_win, s.caller_win)