robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1--- @brief 2--- 3--- WARNING: This is an experimental feature intended to replace the builtin message + cmdline 4--- presentation layer. 5--- 6--- To enable this feature (default opts shown, see also 'messagesopt'): 7--- ```lua 8--- require('vim._core.ui2').enable({ 9--- enable = true, -- Whether to enable or disable the UI. 10--- msg = { -- Options related to the message module. 11--- ---@type string|table<string, 'cmd'|'msg'|'pager'> Default message target 12--- ---or table mapping |ui-messages| kinds, triggers and IDs to a target. 13--- ---Table keys are matched as a Lua pattern to the message ID. 'default' 14--- ---mapping applies to any omitted kind: { default = 'cmd', progress = 'msg' }. 15--- targets = 'cmd', 16--- dialog = { -- Options related to dialog window. 17--- height = 0.5, -- Maximum height. 18--- }, 19--- msg = { -- Options related to msg window. 20--- height = 0.5, -- Maximum height. 21--- }, 22--- pager = { -- Options related to message window. 23--- height = 0.999, -- Maximum height. 24--- }, 25--- }, 26--- }) 27--- ``` 28--- 29--- ui2 behavior is also configured by these 'messagesopt' items: "pager", "timeout". 30--- Example: 31--- ```vim 32--- set messagesopt+=maxheight:50,pager:<CR>,timeout:4000 33--- ``` 34--- 35--- There are four special windows/buffers for presenting messages and cmdline: 36--- - "cmd": Cmdline. Also used by default for messages, 'showcmd', 'showmode', 'ruler'. 37--- - "msg": Message window. Shows ephemeral messages; useful if 'cmdheight' is 0. 38--- - "pager": Pager window. Shows |:messages|, allows navigating all messages. 39--- - "dialog": Dialog window. Shows modal prompts that wait for user input. 40--- 41--- In each of the special buffers, 'filetype' is set as listed above ("cmd", "msg", …). Handle the 42--- [FileType] event to configure any local options for these windows/buffers. 43--- 44--- Unlike the legacy [hit-enter] prompt, messages exceeding 'cmdheight' are collapsed, followed by 45--- a `[+x]` "spill" indicator, where `x` is number of overflow lines. To see all messages, do either: 46--- - |g<| at any time. 47--- - Type the "pager" char (if configured in 'messagesopt'), immediately after an interactive [:] command. 48 49local api =vim . api 50local nvim_on =require ( 'vim._core.util' ). nvim_on 51local M ={ 52ns =api . nvim_create_namespace ( 'nvim.ui2' ), 53augroup =api . nvim_create_augroup ( 'nvim.ui2' ), 54cmdheight =vim . o . cmdheight , -- 'cmdheight' option value set by user. 55redrawing =false , -- True when redrawing to display UI event. 56wins ={ cmd =- 1 , dialog =- 1 , msg =- 1 , pager =- 1 } , 57bufs ={ cmd =- 1 , dialog =- 1 , msg =- 1 , pager =- 1 } , 58cfg ={ 59enable =true , 60msg ={ -- Options related to the message module. 61---@type table<string, 'cmd'|'msg'|'pager'> Kind specific message targets. 62targets ={ default ='cmd' } , 63dialog ={ -- Options related to dialog window. 64height =0.5 , -- Maximum height. 65} , 66msg ={ -- Options related to msg window. 67height =0.5 , -- Maximum height. 68} , 69pager ={ -- Options related to message window. 70height =0.999 , -- Maximum height. 71} , 72} , 73} , 74} 75 76---Ensure target buffers and windows are still valid. 77function M . check_targets () 78local curtab =api . nvim_get_current_tabpage () 79for i , type in ipairs ( { 'cmd' , 'dialog' , 'msg' , 'pager' } ) do 80local buf =api . nvim_buf_is_valid ( M . bufs [ type ]) and M . bufs [ type ] 81local win =api . nvim_win_is_valid ( M . wins [ type ]) and M . wins [ type ] 82local floating =win and api . nvim_win_get_config ( win ). zindex 83local setopt =not buf or not win or not floating 84M . bufs [ type ] =buf or api . nvim_create_buf ( false , false ) 85 86if not win or not floating then 87-- Open a new window when closed or no longer floating (e.g. wincmd J). 88local cfg ={ col =0 , row =1 , width =10000 , height =1 , noautocmd =true } 89cfg . focusable =false 90cfg . style ='minimal' 91cfg . relative ='laststatus' 92cfg . anchor =type ~= 'cmd' and 'SE' or nil 93cfg . mouse =type == 'pager' or nil 94cfg . border =type ~= 'msg' and 'none' or nil 95cfg . _cmdline_offset =type == 'cmd' and 0 or nil 96cfg . hide =type ~= 'cmd' or M . cmdheight == 0 or nil 97-- kZIndexMessages < cmd zindex < kZIndexCmdlinePopupMenu (grid_defs.h), pager below others. 98cfg . zindex =201 - i 99-- Open the window without fileinfo notifications 100vim . _with ( { silent =true } , function () 101M . wins [ type ] =api . nvim_open_win ( M . bufs [ type ], false , cfg ) 102end ) 103elseif api . nvim_win_get_tabpage ( M . wins [ type ]) ~= curtab then 104api . nvim_win_set_config ( M . wins [ type ], { win =api . nvim_tabpage_get_win ( curtab ) } ) 105end 106if win and floating and api . nvim_win_get_buf ( win ) ~= M . bufs [ type ] then 107api . nvim_win_set_buf ( win , M . bufs [ type ]) 108setopt =true 109end 110 111if setopt then 112-- Set options without firing OptionSet and BufFilePost. 113vim . _with ( { win =M . wins [ type ], noautocmd =true } , function () 114api . nvim_set_option_value ( 'eventignorewin' , 'all,-FileType' , { scope ='local' } ) 115api . nvim_set_option_value ( 'wrap' , true , { scope ='local' } ) 116api . nvim_set_option_value ( 'linebreak' , false , { scope ='local' } ) 117api . nvim_set_option_value ( 'smoothscroll' , true , { scope ='local' } ) 118api . nvim_set_option_value ( 'breakindent' , false , { scope ='local' } ) 119api . nvim_set_option_value ( 'foldenable' , false , { scope ='local' } ) 120api . nvim_set_option_value ( 'showbreak' , '' , { scope ='local' } ) 121api . nvim_set_option_value ( 'spell' , false , { scope ='local' } ) 122api . nvim_set_option_value ( 'swapfile' , false , { scope ='local' } ) 123api . nvim_set_option_value ( 'modeline' , false , { scope ='local' } ) 124api . nvim_set_option_value ( 'modifiable' , true , { scope ='local' } ) 125api . nvim_set_option_value ( 'bufhidden' , 'hide' , { scope ='local' } ) 126api . nvim_set_option_value ( 'buftype' , 'nofile' , { scope ='local' } ) 127-- Use MsgArea except in the msg window. Hide Search highlighting except in the pager. 128local search_hide ='Search:,CurSearch:,IncSearch:' 129local hl ='Normal:MsgArea,' .. search_hide 130if type == 'pager' then 131hl ='Normal:MsgArea' 132elseif type == 'msg' then 133hl =search_hide 134elseif type == 'cmd' then 135api . nvim_set_option_value ( 'winpinned' , true , { scope ='local' } ) 136end 137api . nvim_set_option_value ( 'winhighlight' , hl , { scope ='local' } ) 138end ) 139api . nvim_buf_set_name ( M . bufs [ type ], ( '[%s]' ): format ( type : sub ( 1 , 1 ): upper () .. type : sub ( 2 ))) 140-- Fire FileType with window context to let the user reconfigure local options. 141vim . _with ( { win =M . wins [ type ] } , function () 142api . nvim_set_option_value ( 'filetype' , type , { scope ='local' } ) 143end ) 144 145if type == 'pager' then 146-- Close pager with `q`, same as `checkhealth` 147api . nvim_buf_set_keymap ( M . bufs . pager , 'n' , 'q' , '<Cmd>wincmd c<CR>' ) 148elseif M . msg [ type ] then 149M . msg [ type ]. prev_msg ='' -- Will no longer be visible. 150end 151end 152end 153end 154 155--- @param redraw_msg boolean 156--- @param event string 157--- @param ... any 158local function ui_callback ( redraw_msg , event , ... ) 159local handler =M . msg [ event ] or M . cmd [ event ] --[[@as function]] 160M . check_targets () 161handler ( ... ) 162-- Cmdline mode, non-fast message and non-empty showcmd require an immediate redraw. 163if 164( M . cmd [ event ] or redraw_msg or ( event == 'msg_showcmd' and select ( 1 , ... )[ 1 ])) 165and event ~= 'cmdline_hide' 166then 167M . redrawing =true 168api . nvim__redraw ( { 169flush =handler ~= M . cmd . cmdline_hide or nil , 170cursor =handler == M . cmd [ event ] and true or nil , 171win =handler == M . cmd [ event ] and M . wins . cmd or nil , 172} ) 173M . redrawing =false 174end 175end 176local scheduled_ui_callback =vim . schedule_wrap ( ui_callback ) 177 178--- @param name string 179--- @param value any 180--- @param new_name string 181local function validate_old_cfg ( name , value , new_name ) 182if value ~= nil then 183error ( 184( [[ui2: config key %q was removed, set %q in the 'messagesopt' option instead]] ): format ( 185name , 186new_name 187) 188) 189end 190end 191 192---@nodoc 193---@class (private) vim._core.ui2.Opts 194---@field enable? boolean 195---@field pager_char? string 196---@field msg? { 197--- targets?: 'cmd'|'msg'|'pager'|table<string,'cmd'|'msg'|'pager'>, 198--- dialog?: { height?: number }, 199--- msg?: { height?: number, timeout?: integer }, 200--- pager?: { height?: number }, 201--- cmd?: { height?: integer }, 202--- } 203---@param opts? vim._core.ui2.Opts 204function M . enable ( opts ) 205opts =opts or {} 206vim . validate ( 'opts' , opts , 'table' , true ) 207local msg =opts . msg or {} 208validate_old_cfg ( 'pager_char' , opts . pager_char , 'pager' ) 209validate_old_cfg ( 'msg.msg.timeout' , ( msg . msg or {} ). timeout , 'timeout' ) 210validate_old_cfg ( 'msg.cmd.height' , ( msg . cmd or {} ). height , 'maxheight' ) 211-- The deep merge preserves the required defaults in M.cfg. 212---@diagnostic disable-next-line: assign-type-mismatch 213M . cfg =vim . tbl_deep_extend ( 'keep' , opts , M . cfg ) 214M . cfg . msg . targets =type ( M . cfg . msg . targets ) == 'table' and M . cfg . msg . targets 215or { default =M . cfg . msg . targets } 216M . cfg . msg . targets . default =M . cfg . msg . targets . default or 'cmd' 217if # api . nvim_list_uis () == 0 then 218return -- Don't prevent stdout messaging when no UIs are attached. 219end 220 221if M . cfg . enable == false then 222-- Detach and cleanup windows, buffers and autocommands. 223for _ , win in pairs ( M . wins ) do 224pcall ( api . nvim_win_close , win , true ) 225end 226for _ , buf in pairs ( M . bufs ) do 227pcall ( api . nvim_buf_delete , buf , {} ) 228end 229api . nvim_clear_autocmds ( { group =M . augroup } ) 230vim . ui_detach ( M . ns ) 231return 232end 233 234M . cmd =require ( 'vim._core.ui2.cmdline' ) 235M . msg =require ( 'vim._core.ui2.messages' ) 236M . msg . on_option_changed () 237vim . ui_attach ( M . ns , { ext_messages =true , set_cmdheight =false } , function ( event , ... ) 238if not ( M . msg [ event ] or M . cmd [ event ]) then 239return 240end 241-- Ensure cmdline is placed after a scheduled message in block mode. 242if vim . in_fast_event () or ( event == 'cmdline_show' and M . cmd . srow > 0 ) then 243scheduled_ui_callback ( false , event , ... ) 244else 245ui_callback ( event == 'msg_show' , event , ... ) 246end 247return true 248end ) 249 250-- The visibility and appearance of the cmdline and message window is 251-- dependent on some option values. Reconfigure windows when option value 252-- has changed and after VimEnter when the user configured value is known. 253-- TODO: Reconsider what is needed when this module is enabled by default early in startup. 254--- @param value integer 255local function check_cmdheight ( value ) 256M . check_targets () 257-- 'cmdheight' set; (un)hide cmdline window and set its height. 258local cfg ={ height =math . max ( value , 1 ), hide =value == 0 } 259api . nvim_win_set_config ( M . wins . cmd , cfg ) 260M . cmdheight =value 261end 262 263if vim . v . vim_did_enter == 0 then 264vim . schedule ( function () 265check_cmdheight ( vim . o . cmdheight ) 266M . msg . on_option_changed () 267end ) 268end 269 270nvim_on ( 'OptionSet' , M . augroup , { 271pattern ={ 'cmdheight' , 'fillchars' , 'laststatus' , 'messagesopt' } , 272desc ='Handle message/pager option changes.' , 273} , function ( ev ) 274if ev . match == 'cmdheight' then 275check_cmdheight ( vim . v . option_new ) 276else 277M . msg . on_option_changed () 278end 279M . msg . set_pos () 280end ) 281 282nvim_on ( { 'VimResized' , 'TabEnter' } , M . augroup , { 283desc ='Set cmdline and message window dimensions after shell resize or tabpage change.' , 284} , function ( ev ) 285M . msg . on_option_changed () -- 'messagesopt':maxheight is relative to 'lines'. 286M . check_targets () 287-- After a tabpage was closed unhide the msg window on the current tabpage. 288if ev . event == 'TabEnter' and next ( M . msg . msg . ids ) ~= nil then 289api . nvim_win_set_config ( M . wins . msg , { hide =false , width =M . msg . msg . width } ) 290end 291M . msg . set_pos () 292end ) 293end 294 295return M