Merge #40325 from justinmk/cmdwin

This commit is contained in:
Justin M. Keyes
2026-06-25 13:06:01 -04:00
committed by GitHub
68 changed files with 1017 additions and 1647 deletions

View 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

View File

@@ -624,13 +624,16 @@ local function win_row_height_border(tgt, min)
local cfgmin = ui.cfg.msg[tgt].height --[[@as number]]
min = math.min(min, cfgmin < 1 and math.ceil(o.lines * cfgmin) or cfgmin)
if tgt ~= 'pager' then
return (tgt == 'msg' and 0 or 1) - ui.cmd.wmnumode, min, min < o.lines - ui.cmdheight
return (tgt == 'msg' and 0 or 1) - ui.cmd.wmnumode,
math.max(1, min),
min < o.lines - ui.cmdheight
end
local cmdwin = fn.getcmdwintype() ~= was_cmdwin and api.nvim_win_get_height(0) or 0
local global_stl = (cmdwin > 0 or o.laststatus == 3) and 1 or 0
local row = 1 - cmdwin - global_stl
local top = min < o.lines - ui.cmdheight - global_stl - cmdwin
return row, math.min(min, o.lines - (top and 1 or 0) - ui.cmdheight - global_stl - cmdwin), top
local height = math.min(min, o.lines - (top and 1 or 0) - ui.cmdheight - global_stl - cmdwin)
return row, math.max(1, height), top
end
local function enter_pager()

View File

@@ -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]()