feat(cmdwin): implement cmdwin as a normal buf+win

Problem:

cmdwin (the `:q` cmdline buffer) has various limitations which require
special-casing all over the codebase.

Besides complicating the code, it also breaks async plugins if they try
to create buffers/windows after some work is done, if the user happens
to open cmdwin at the wrong the moment:

    Lua callback: …/guh.nvim/lua/guh/util.lua:531:
    E11: Invalid in command-line window; <CR> executes, CTRL-C quits
    stack traceback:
        [C]: in function 'nvim_buf_delete'
        …/guh.nvim/lua/guh/util.lua:531: in function <…/guh.nvim/lua/guh/util.lua:526>

Solution:

Just say no to "inception". Reimplement cmdwin as a normal buffer+window.

All of the cmdwin contortions (in both core, and innocent plugins) exist
literally only to support "inception": recursive
cmdwin-in-cmdline-things, like `<c-r>=`, `/`, search-during-substitute,
`:input()`, etc. So we just won't support that (though I have
a potential plan for that later, which I call "modal parking lot").

The benefit is that plugins, and core, no longer have to care about
cmdwin.

BONUS:
- mouse-drag on vertical separators works (it only worked for
  horizontal/statusline before)
- inccommand-in-cmdwin now works correctly, for free (thus don't need
  #40077).

POTENTIAL FOLLOWUPS
- Drop `CHECK_CMDWIN` ("E11: Invalid in command-line window"), allow chaos.
- Unify `BUFLOCK_OK` / `LOCK_OK` ?

DESIGN:
- Eliminate lots of C globals, `EX_CMDWIN`, etc.
- `text_locked()` no longer reports true for cmdwin.
- cmdwin = a normal window with 'winfixbuf', 'bufhidden=wipe',
  'buftype=nofile'. Invariants come from those options rather than
  special cases throughout the codebase.
- `nv_record` for q:/q//q? calls Lua
  `nlua_call_vimfn("vim._core.cmdwin", …)`. No `K_CMDWIN`
  / cmdline-reader detour.
- `cedit_key` (`c_CTRL-F`) schedules a deferred event that calls
  `vim._core.cmdwin.open(type, content, pos)` and returns `Ctrl_C` so
  the in-flight cmdline cancels. Reader state is not serialized; instead
  the captured `(type, line, col)` is replayed via
  `nvim_feedkeys(type..line.."<CR>", "nt", …)` after user confirms.
- On confirm/cancel: `<CR>` / `<C-C>` calls into Lua which closes the
  window and re-feeds the cmdline.

BREAKING CHANGES:
- Expression-register cmdline (`<C-R>=` from insert-mode) no longer
  supports cmdwin. Same applies to `input()` / `inputlist()` (already
  covered by `text_locked`).
- Usage of cmdwin in macros/mappings will probably break (assuming they
  ever worked).
This commit is contained in:
Justin M. Keyes
2026-06-19 12:56:38 +02:00
parent 122be61ed2
commit b2bf7bcfb1
67 changed files with 1005 additions and 1645 deletions

View File

@@ -0,0 +1,115 @@
-- Tests for non-blocking cmdwin. #40312
local t = require('test.testutil')
local n = require('test.functional.testnvim')()
local feed = n.feed
local api = n.api
local fn = n.fn
local exec_lua = n.exec_lua
local eq = t.eq
describe('cmdwin', function()
before_each(n.clear)
it('q: opens cmdwin', function()
feed('q:')
eq(':', fn.getcmdwintype())
eq('[Command Line]', vim.fs.basename(api.nvim_buf_get_name(0)))
-- cmdwin-char is shown in window-local 'statuscolumn'.
eq('%#NonText#: ', api.nvim_get_option_value('statuscolumn', { win = 0 }))
eq('nofile', api.nvim_get_option_value('buftype', { buf = 0 }))
eq('wipe', api.nvim_get_option_value('bufhidden', { buf = 0 }))
eq(false, api.nvim_get_option_value('swapfile', { buf = 0 }))
eq(true, api.nvim_get_option_value('winfixbuf', { win = 0 }))
-- <CR> executes the cmdline
feed('ilet g:cmdwin_result = 42<Esc>')
feed('<CR>')
eq(42, api.nvim_get_var('cmdwin_result'))
eq('', fn.getcmdwintype())
end)
it('q/ opens cmdwin with search history', function()
feed('q/')
eq('/', fn.getcmdwintype())
end)
it('c_CTRL-F opens cmdwin pre-filled with current cmdline', function()
feed(':echo "hi"<C-F>')
n.poke_eventloop()
eq(':', fn.getcmdwintype())
eq('echo "hi"', api.nvim_get_current_line())
end)
it('<C-C> in normal mode cancels without executing', function()
feed('q:')
feed('iecho "nope"<Esc>')
feed('<C-C>')
eq('', fn.getcmdwintype())
-- v:errmsg should not contain a successful echo from the cancelled line.
end)
it('async API calls work while cmdwin is open #40312', function()
feed('q:')
eq(':', fn.getcmdwintype())
local ok, err = pcall(function()
local b = api.nvim_create_buf(true, false)
api.nvim_buf_delete(b, { force = true })
end)
eq(true, ok, 'buf_delete unexpectedly failed: ' .. tostring(err))
end)
it('textlock does not include cmdwin #40312', function()
feed('q:')
-- Setting an option from API used to fail with E11 in cmdwin.
api.nvim_set_option_value('wildignore', '*.tmp', {})
end)
it('E1292 cannot nest cmdwin', function()
feed('q:')
eq(':', fn.getcmdwintype())
feed('q:')
-- Still just one cmdwin.
eq(':', fn.getcmdwintype())
local nwin = 0
for _, w in ipairs(api.nvim_list_wins()) do
if api.nvim_get_option_value('winfixbuf', { win = w }) then
nwin = nwin + 1
end
end
eq(1, nwin)
end)
it(':quit closes cmdwin', function()
feed('q:')
eq(':', fn.getcmdwintype())
feed(':q<CR>')
eq('', fn.getcmdwintype())
end)
it(':messages from inside cmdwin works #40312', function()
-- Before #40312, this raised E11 because text_locked() returned true for cmdwin.
exec_lua([[vim.api.nvim_echo({{'hello from history'}}, true, {})]])
feed('q:')
eq(':', fn.getcmdwintype())
n.command('messages') -- Previously E11; now actually runs.
end)
it('CmdwinEnter/CmdwinLeave events', function()
exec_lua([[
_G.events = {}
vim.api.nvim_create_autocmd('CmdwinEnter', {
callback = function(a) table.insert(_G.events, 'enter:'..a.match) end,
})
vim.api.nvim_create_autocmd('CmdwinLeave', {
callback = function(a) table.insert(_G.events, 'leave:'..a.match) end,
})
]])
feed('q:')
n.poke_eventloop() -- Ensure q: is processed before the <C-C> mapping fires.
feed('<C-C>')
eq({ 'enter::', 'leave::' }, exec_lua('return _G.events'))
end)
end)