mirror of
https://github.com/neovim/neovim.git
synced 2026-07-10 11:29:39 +00:00
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:
168
runtime/lua/vim/_core/cmdwin.lua
Normal file
168
runtime/lua/vim/_core/cmdwin.lua
Normal file
@@ -0,0 +1,168 @@
|
||||
--- @brief Command-line window (q:, q/, q?, c_CTRL-F).
|
||||
---
|
||||
--- Implements cmdwin as a normal window+buffer, instead of the legacy nested `state_enter` loop.
|
||||
--- - On confirm, curline is fed back through the cmdline via |nvim_feedkeys()|.
|
||||
--- - On cancel, curline is pre-filled in the ":" cmdline.
|
||||
|
||||
local M = {}
|
||||
|
||||
--- @class vim._core.cmdwin.State
|
||||
--- @field type string ':', '/', '?'
|
||||
--- @field win integer cmdwin window id
|
||||
--- @field buf integer cmdwin buffer id
|
||||
--- @field caller_win integer Window to return-to on close
|
||||
|
||||
--- @type vim._core.cmdwin.State?
|
||||
local state = nil
|
||||
|
||||
local cmdwin_types = { [':'] = true, ['/'] = true, ['?'] = true }
|
||||
|
||||
--- Fills the cmdwin buffer with the cmdline history.
|
||||
local function fill_history(buf, type)
|
||||
local histname = type == ':' and 'cmd' or (type == '/' or type == '?') and 'search' or nil
|
||||
assert(histname, 'cmdwin: unknown type: ' .. tostring(type))
|
||||
local n = vim.fn.histnr(histname)
|
||||
if n <= 0 then -- May be -1 if history is empty.
|
||||
return
|
||||
end
|
||||
local lines = {} --- @type string[]
|
||||
for i = 1, n do
|
||||
local h = vim.fn.histget(histname, i)
|
||||
if h ~= '' then
|
||||
-- One cmdwin line = one cmdline. Entry may have embedded newlines (e.g. via feedkeys or :execute).
|
||||
lines[#lines + 1] = (h:gsub('\n', '\0'))
|
||||
end
|
||||
end
|
||||
if #lines > 0 then
|
||||
vim.api.nvim_buf_set_lines(buf, 0, -1, false, lines)
|
||||
end
|
||||
end
|
||||
|
||||
--- Open the command-line window.
|
||||
---
|
||||
--- @param type? string ':', '/', '?'. Default ':'.
|
||||
--- @param init_line? string Pre-fill the last line (the "live" cmdline).
|
||||
--- @param init_col? integer 1-based cursor column in the last line.
|
||||
function M.open(type, init_line, init_col)
|
||||
type = type or ':'
|
||||
assert(cmdwin_types[type], 'cmdwin: unknown type: ' .. tostring(type))
|
||||
if state ~= nil then
|
||||
vim.api.nvim_echo(
|
||||
{ { 'E1292: Command-line window is already open', 'ErrorMsg' } },
|
||||
true,
|
||||
{ err = true }
|
||||
)
|
||||
return
|
||||
end
|
||||
|
||||
local caller = vim.api.nvim_get_current_win()
|
||||
|
||||
-- Split a horizontal window at the bottom, sized by 'cmdwinheight'.
|
||||
local ok, err = pcall(function()
|
||||
vim.cmd(('botright %dnew'):format(vim.o.cmdwinheight))
|
||||
end)
|
||||
if not ok then
|
||||
vim.api.nvim_echo({ { tostring(err), 'ErrorMsg' } }, true, { err = true })
|
||||
return
|
||||
end
|
||||
local win = vim.api.nvim_get_current_win()
|
||||
local buf = vim.api.nvim_get_current_buf()
|
||||
|
||||
vim.bo[buf].buftype = 'nofile'
|
||||
vim.bo[buf].bufhidden = 'wipe'
|
||||
vim.bo[buf].swapfile = false
|
||||
vim.bo[buf].buflisted = false
|
||||
vim.wo[win][0].winfixbuf = true
|
||||
vim.wo[win][0].foldenable = false
|
||||
-- Show cmdwin-char via 'statuscolumn'.
|
||||
vim.wo[win][0].statuscolumn = '%#NonText#' .. type .. ' '
|
||||
|
||||
fill_history(buf, type)
|
||||
|
||||
-- Append the in-flight cmdline (or empty placeholder) as the last line.
|
||||
vim.api.nvim_buf_set_lines(buf, -1, -1, false, { init_line or '' })
|
||||
local last = vim.api.nvim_buf_line_count(buf)
|
||||
vim.api.nvim_win_set_cursor(win, { last, math.max(0, (init_col or 1) - 1) })
|
||||
|
||||
pcall(vim.api.nvim_buf_set_name, buf, '[Command Line]')
|
||||
if type == ':' then
|
||||
vim.bo[buf].filetype = 'vim'
|
||||
end
|
||||
|
||||
vim.api.nvim__cmdwin_set(type, buf) -- Update the C-side globals.
|
||||
|
||||
state = {
|
||||
type = type,
|
||||
win = win,
|
||||
buf = buf,
|
||||
caller_win = caller,
|
||||
}
|
||||
|
||||
-- Buffer-local mappings.
|
||||
vim.keymap.set(
|
||||
{ 'n', 'i' },
|
||||
'<CR>',
|
||||
[[<C-\><C-N><Cmd>lua require'vim._core.cmdwin'.confirm()<CR>]],
|
||||
{ buffer = buf, silent = true, desc = 'cmdwin: execute' }
|
||||
)
|
||||
vim.keymap.set(
|
||||
{ 'n', 'i', 'x' },
|
||||
'<C-C>',
|
||||
[[<C-\><C-N><Cmd>lua require'vim._core.cmdwin'.cancel()<CR>]],
|
||||
{ buffer = buf, silent = true, desc = 'cmdwin: cancel' }
|
||||
)
|
||||
|
||||
-- Clean up if the window is closed by other means (`:q`, `:close`, etc.).
|
||||
vim.api.nvim_create_autocmd({ 'WinClosed' }, {
|
||||
buffer = buf,
|
||||
once = true,
|
||||
callback = function()
|
||||
if state ~= nil then
|
||||
M._cleanup()
|
||||
end
|
||||
end,
|
||||
})
|
||||
|
||||
vim.api.nvim_exec_autocmds('CmdwinEnter', { pattern = type, modeline = false })
|
||||
end
|
||||
|
||||
--- @private
|
||||
function M._cleanup()
|
||||
if state == nil then
|
||||
return
|
||||
end
|
||||
local s = state
|
||||
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)
|
||||
end
|
||||
if vim.api.nvim_win_is_valid(s.caller_win) then
|
||||
pcall(vim.api.nvim_set_current_win, s.caller_win)
|
||||
end
|
||||
end
|
||||
|
||||
local function _confirm()
|
||||
if state == nil then
|
||||
return
|
||||
end
|
||||
local line = vim.api.nvim_get_current_line()
|
||||
local type = state.type
|
||||
M._cleanup()
|
||||
return line, type
|
||||
end
|
||||
|
||||
--- Confirm and execute the current line as a cmdline.
|
||||
function M.confirm()
|
||||
local line, type = _confirm()
|
||||
vim.api.nvim_feedkeys(type .. line .. vim.keycode('<CR>'), 'nt', false)
|
||||
end
|
||||
|
||||
--- Cancel: close the cmdwin and re-enter cmdline mode with the line pre-filled (no execute).
|
||||
function M.cancel()
|
||||
local line, type = _confirm()
|
||||
vim.api.nvim_feedkeys(type .. line, 'nt', false)
|
||||
end
|
||||
|
||||
return M
|
||||
10
runtime/lua/vim/_meta/api.gen.lua
generated
10
runtime/lua/vim/_meta/api.gen.lua
generated
@@ -24,6 +24,14 @@ function vim.api.nvim__buf_debug_extmarks(buf, keys, dot) end
|
||||
--- @return table<string,any>
|
||||
function vim.api.nvim__buf_stats(buf) end
|
||||
|
||||
--- WARNING: This feature is experimental/unstable.
|
||||
---
|
||||
--- Records the cmdwin scratchbuf and type, or clears both when type="" / buf=0. Internal use only.
|
||||
---
|
||||
--- @param type string ':', '/', '?' (first char only); empty to clear.
|
||||
--- @param buf integer cmdwin buffer id, or 0 to clear.
|
||||
function vim.api.nvim__cmdwin_set(type, buf) end
|
||||
|
||||
--- WARNING: This feature is experimental/unstable.
|
||||
---
|
||||
--- Sets info for the completion item at the given index. If the info text was shown in a window,
|
||||
@@ -967,12 +975,10 @@ function vim.api.nvim_create_autocmd(event, opts) end
|
||||
|
||||
--- Creates a new, empty, unnamed buffer.
|
||||
---
|
||||
--- @see buf_open_scratch
|
||||
--- @param listed boolean Sets 'buflisted'
|
||||
--- @param scratch boolean Creates a "throwaway" `scratch-buffer` for temporary work
|
||||
--- (always 'nomodified'). Also sets 'nomodeline' on the buffer.
|
||||
--- @return integer # Buffer id, or 0 on error
|
||||
---
|
||||
function vim.api.nvim_create_buf(listed, scratch) end
|
||||
|
||||
--- Creates a new namespace or gets an existing one. [namespace]()
|
||||
|
||||
Reference in New Issue
Block a user