robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1--- @brief Command-line window (q:, q/, q?, c_CTRL-F). 2--- 3--- Implements cmdwin as a normal window+buffer, instead of the legacy nested `state_enter` loop. 4--- - On confirm, curline is fed back through the cmdline via |nvim_feedkeys()|. 5--- - On cancel, curline is pre-filled in the ":" cmdline. 6 7local M ={} 8 9--- @class vim._core.cmdwin.State 10--- @field type ':'|'/'|'?' 11--- @field win integer cmdwin window id 12--- @field buf integer cmdwin buffer id 13--- @field caller_win integer Window to return-to on close 14 15--- @type vim._core.cmdwin.State? 16local state =nil 17 18--- @type table<string, boolean> 19local cmdwin_types ={ [ ':' ] =true , [ '/' ] =true , [ '?' ] =true } 20 21--- Fills the cmdwin buffer with the cmdline history. 22--- @return boolean filled Whether any lines were written. 23--- @param buf integer 24--- @param type ':'|'/'|'?' 25local function fill_history ( buf , type ) 26local histname =type == ':' and 'cmd' or 'search' 27local n =vim . fn . histnr ( histname ) 28if n <= 0 then -- May be -1 if history is empty. 29return false 30end 31local lines ={} --- @type string[] 32for i =1 , n do 33local h =vim . fn . histget ( histname , i ) 34if h ~= '' then 35-- One cmdwin line = one cmdline. Entry may have embedded newlines (e.g. via feedkeys or :execute). 36lines [ # lines + 1 ] =( h : gsub ( '\n' , '\0' )) 37end 38end 39if # lines == 0 then 40return false 41end 42vim . api . nvim_buf_set_lines ( buf , 0 , - 1 , false , lines ) 43return true 44end 45 46--- Open the command-line window. 47--- 48--- @param type? ':'|'/'|'?' Default ':'. 49--- @param init_line? string Pre-fill the last line (the "live" cmdline). 50--- @param init_col? integer 1-based cursor column in the last line. 51function M . open ( type , init_line , init_col ) 52type =type or ':' 53assert ( cmdwin_types [ type ], 'cmdwin: unknown type: ' .. tostring ( type )) 54if state ~= nil then 55vim . api . nvim_echo ( 56{ { 'E1292: Command-line window is already open' , 'ErrorMsg' } } , 57true , 58{ err =true } 59) 60return 61end 62 63local caller =vim . api . nvim_get_current_win () 64 65-- Split a horizontal window at the bottom, sized by 'cmdwinheight'. 66local ok , err =pcall ( function () 67vim . cmd (( 'botright %dnew' ): format ( vim . o . cmdwinheight )) 68end ) 69if not ok then 70vim . api . nvim_echo ( { { tostring ( err ), 'ErrorMsg' } } , true , { err =true } ) 71return 72end 73local win =vim . api . nvim_get_current_win () 74local buf =vim . api . nvim_get_current_buf () 75 76vim . bo [ buf ]. buftype ='nofile' 77vim . bo [ buf ]. bufhidden ='wipe' 78vim . bo [ buf ]. swapfile =false 79vim . bo [ buf ]. buflisted =true -- #40431 80vim . wo [ win ][ 0 ]. foldenable =false 81vim . wo [ win ][ 0 ]. scrollbind =false 82-- Show cmdwin-char via 'statuscolumn'. 83vim . wo [ win ][ 0 ]. statuscolumn ='%#NonText#' .. type 84 85local filled =fill_history ( buf , type ) 86init_line =init_line and init_line : gsub ( '\n' , '\0' ) or '' 87 88-- Append the in-flight cmdline as the last line (or only line if history is empty). 89vim . api . nvim_buf_set_lines ( buf , filled and - 1 or 0 , - 1 , false , { init_line } ) 90local last =vim . api . nvim_buf_line_count ( buf ) 91vim . api . nvim_win_set_cursor ( win , { last , math . max ( 0 , ( init_col or 1 ) - 1 ) } ) 92 93pcall ( vim . api . nvim_buf_set_name , buf , '[Command Line]' ) 94if type == ':' then 95vim . bo [ buf ]. filetype ='vim' 96end 97 98if vim . o . wildchar == ( '\t' ): byte () then 99vim . keymap . set ( 'i' , '<Tab>' , '<C-X><C-V>' , { buf =buf } ) 100vim . keymap . set ( 'n' , '<Tab>' , 'a<C-X><C-V>' , { buf =buf } ) 101end 102 103vim . api . nvim__cmdwin_set ( type , buf ) -- Update the C-side globals. 104 105state ={ 106type =type , 107win =win , 108buf =buf , 109caller_win =caller , 110} 111 112-- Clean up when the (last-visible) cmdwin is closed by other means (`:q`, `:close`, etc.). 113vim . api . nvim_create_autocmd ( { 'WinClosed' } , { 114buffer =buf , 115nested =true , 116callback =function ( ev ) 117if state == nil then 118return 119end 120local closing =tonumber ( ev . match ) 121for _ , w in ipairs ( vim . fn . win_findbuf ( buf )) do 122if w ~= closing then 123return -- Still visible elsewhere; keep cmdwin (and this autocmd) active. 124end 125end 126M . _cleanup () 127return true -- Last cmdwin window gone; delete this autocmd. 128end , 129} ) 130 131-- Clean up when the cmdwin is swapped to another buffer 132vim . api . nvim_create_autocmd ( { 'BufWinLeave' } , { 133buffer =buf , 134nested =true , 135callback =function () 136if state == nil then 137return 138end 139-- We're the last cmdwin buffer (see BufWinLeave), so cleanup. 140-- But don't delete the buffer, as the BufWinLeave code handles it. 141M . _cleanup { delbuf =false } 142return true 143end , 144} ) 145 146vim . api . nvim_exec_autocmds ( 'CmdwinEnter' , { pattern =type , modeline =false } ) 147end 148 149--- @private 150--- @param opts? {delbuf?: boolean} 151function M . _cleanup ( opts ) 152if state == nil then 153return 154end 155local s =state 156state =nil 157pcall ( vim . api . nvim__cmdwin_set , '' , 0 ) -- Clear the C-side globals. 158pcall ( vim . api . nvim_exec_autocmds , 'CmdwinLeave' , { pattern =s . type , modeline =false } ) 159if ( opts == nil or opts . delbuf ~= false ) and vim . api . nvim_buf_is_valid ( s . buf ) then 160pcall ( vim . api . nvim_buf_delete , s . buf , { force =true } ) 161end 162-- Only return to caller_win for the current tabpage; avoid changing the current tab during an autocmd. 163if 164vim . api . nvim_win_is_valid ( s . caller_win ) 165and vim . api . nvim_win_get_tabpage ( s . caller_win ) == vim . api . nvim_get_current_tabpage () 166then 167pcall ( vim . api . nvim_set_current_win , s . caller_win ) 168end 169end 170 171--- cmdwin window, or nil if not open. 172--- @return integer? 173function M . win () 174return state and vim . api . nvim_win_is_valid ( state . win ) and state . win or nil 175end 176 177--- Closes the cmdwin and returns its current line and type. 178--- @return string line, ':'|'/'|'?' type 179local function _close () 180local line =vim . api . nvim_get_current_line () 181local type =assert ( state ). type 182M . _cleanup () 183return line , type 184end 185 186--- Confirm and execute the current line as a cmdline. 187function M . confirm () 188if state == nil then -- Not in cmdwin (closed already?). 189return 190end 191local line , type =_close () 192line =line : gsub ( '%z' , '\n' ): gsub ( '(%c)' , '\022%1' ) -- Escape control characters. 193vim . api . nvim_feedkeys ( type .. line .. '\r' , 'nt' , true ) 194end 195 196--- Cancel: close the cmdwin and re-enter cmdline mode with the line pre-filled (no execute). 197function M . cancel () 198if state == nil then -- Not in cmdwin (closed already?). 199return 200end 201local line , type =_close () 202line =line : gsub ( '%z' , '\n' ): gsub ( '(%c)' , '\022%1' ) -- Escape control characters. 203vim . api . nvim_feedkeys ( type .. line , 'nt' , true ) 204end 205 206return M