robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1local M ={} 2 3--- Send `payload` to the host terminal and listen for `TermResponse`, calling `on_response` for 4--- each response. Cleans up after `opts.timeout` ms if the callback never returns `true`. 5--- 6--- The autocommand is removed when: 7--- - `on_response()` returns `true` 8--- - the timeout fires (and `opts.on_timeout` is called, if given) 9--- - the caller explicitly deletes the returned autocmd id 10--- 11---@param payload string Sequence to send via nvim_ui_send(). Use empty string ('') to just register 12--- a listener (no sending). 13---@param opts? { timeout?: integer, on_timeout?: fun(), group?: integer|string, chan?: integer } # 14--- - `timeout` (default: 1000) ms to wait before giving up, or 0 for never (caller must remove the autocmd). 15--- - `on_timeout` optional fn called when the timeout fires. 16--- - `group`: augroup for the TermResponse autocmd. 17--- - `chan`: only handle responses from this channel. 18---@param on_response fun(resp:string):boolean? Called for each TermResponse. Return `true` to stop listening. 19---@return integer # autocmd id of the TermResponse handler. 20function M . request ( payload , opts , on_response ) 21vim . validate ( 'payload' , payload , 'string' ) 22vim . validate ( 'opts' , opts , 'table' , true ) 23vim . validate ( 'on_response' , on_response , 'function' ) 24 25opts =opts or {} 26local timeout =opts . timeout or 1000 27local timer ---@type uv.uv_timer_t? 28if timeout > 0 then 29timer =assert ( vim . uv . new_timer ()) 30end 31 32local id =require ( 'vim._core.util' ). nvim_on ( 33'TermResponse' , 34opts . group , 35{ nested =true } , 36---@param ev {data: vim.event.termresponse.data} 37function ( ev ) 38if opts . chan and ev . data . chan ~= opts . chan then 39return 40end 41local stop =on_response ( ev . data . sequence ) 42-- If on_response is done, cancel the timeout so on_timeout doesn't fire spuriously. 43if stop and timer and not timer : is_closing () then 44timer : close () 45end 46return stop 47end 48) 49 50if payload ~= '' then 51vim . api . nvim_ui_send ( payload ) 52end 53 54if timer then 55timer : start ( timeout , 0 , function () 56vim . schedule ( function () 57pcall ( vim . api . nvim_del_autocmd , id ) 58if opts . on_timeout then 59opts . on_timeout () 60end 61end ) 62if not timer : is_closing () then 63timer : close () 64end 65end ) 66end 67 68return id 69end 70 71--- Query the host terminal emulator for terminfo capabilities. 72--- 73--- This function sends the XTGETTCAP DCS sequence to the host terminal emulator asking the terminal 74--- to send us its terminal capabilities. These are strings that are normally taken from a terminfo 75--- file, however an up to date terminfo database is not always available (particularly on remote 76--- machines), and many terminals continue to misidentify themselves or do not provide their own 77--- terminfo file, making the terminfo database unreliable. 78--- 79--- Querying the terminal guarantees that we get a truthful answer, but only if the host terminal 80--- emulator supports the XTGETTCAP sequence. 81--- 82--- @param caps string|table A terminal capability or list of capabilities to query 83--- @param opts? { timeout?: integer, on_timeout?: fun(), group?: integer|string, chan?: integer } 84--- @param on_response fun(cap:string, found:boolean, seq:string?) Called for each capability in 85--- `caps`. `found` is true if the capability was found, else false. `seq` is the control 86--- sequence if found, or nil for boolean capabilities. 87---@overload fun(caps:string|table, on_response:fun(cap:string, found:boolean, seq:string?)) 88function M . query ( caps , opts , on_response ) 89if type ( opts ) == 'function' then 90on_response =opts 91opts ={} 92end 93 94vim . validate ( 'caps' , caps , { 'string' , 'table' } ) 95vim . validate ( 'opts' , opts , 'table' , true ) 96vim . validate ( 'on_response' , on_response , 'function' ) 97 98opts =opts or {} 99 100if type ( caps ) ~= 'table' then 101caps ={ caps } 102end 103 104local pending ={} ---@type table<string, boolean> 105for _ , v in ipairs ( caps ) do 106pending [ v ] =true 107end 108 109local encoded ={} ---@type string[] 110for i =1 , # caps do 111encoded [ i ] =vim . text . hexencode ( caps [ i ]) 112end 113local payload =( '\027P+q%s\027\\' ): format ( table . concat ( encoded , ';' )) 114 115M . request ( payload , { 116timeout =opts . timeout , 117group =opts . group , 118chan =opts . chan , 119on_timeout =function () 120-- Call the callback for all capabilities that were not found. 121for k in pairs ( pending ) do 122on_response ( k , false , nil ) 123end 124if opts . on_timeout then 125opts . on_timeout () 126end 127end , 128} , function ( resp ) 129local k , rest =resp : match ( '^\027P1%+r(%x+)(.*)$' ) 130if not k or not rest then 131return 132end 133local cap =vim . text . hexdecode ( k ) 134if not cap or not pending [ cap ] then 135-- Received a response for a capability we didn't request. This can happen if there are 136-- multiple concurrent XTGETTCAP requests 137return 138end 139 140local seq ---@type string? 141if rest : match ( '^=%x+$' ) then 142seq =vim . text . hexdecode ( rest : sub ( 2 )) 143if not seq then 144return -- Ignore malformed responses and leave the capability pending. 145end 146seq =seq : gsub ( '\\E' , '\027' ): gsub ( '%%p%d' , '' ): gsub ( '\\(%d+)' , string . char ) 147end 148 149on_response ( cap , true , seq ) 150pending [ cap ] =nil 151 152return next ( pending ) == nil 153end ) 154end 155 156--- Send an APC sequence to the terminal and call `on_response` for each APC response received. 157--- Cleans up after {timeout} milliseconds if no response is received. 158--- 159--- `on_response` receives the full APC sequence including the `\027_` prefix. 160--- Return `true` from `on_response` to stop listening. 161--- 162---@param payload string APC sequence to send (full escape sequence including prefix/suffix) 163---@param opts {timeout?:integer, chan?:integer} Options table (timeout in milliseconds, default 1000) 164---@param on_response fun(resp:string):boolean? Callback invoked for each APC TermResponse 165---@overload fun(payload:string, on_response:fun(resp:string):boolean?) 166function M . query_apc ( payload , opts , on_response ) 167if type ( opts ) == 'function' then 168on_response =opts 169opts ={} 170end 171 172vim . validate ( 'payload' , payload , 'string' ) 173vim . validate ( 'opts' , opts , 'table' ) 174vim . validate ( 'on_response' , on_response , 'function' ) 175 176M . request ( payload , opts , function ( resp ) 177if resp : match ( '^\027_' ) then 178return on_response ( resp ) 179end 180end ) 181end 182 183--- Displays error even when this code runs in the context of the client 184--- @param msg string 185local function broadcast_error ( msg ) 186vim . rpcnotify ( 0 , 'nvim_echo' , { 187{ 188msg , 189} , 190} , true , { err =true } ) 191end 192 193--- Get user overrides for terminfo entries as a table. See |$NVIM_TERMDEFS| 194function M . _get_termdefs () 195local termdefs_raw =os . getenv ( 'NVIM_TERMDEFS' ) 196if termdefs_raw ~= nil then 197local ok , termdefs_or_err =pcall ( vim . json . decode , termdefs_raw ) 198if not ok then 199broadcast_error ( 'E557: Failed to parse $NVIM_TERMDEFS: ' .. vim . inspect ( termdefs_or_err )) 200return 201elseif type ( termdefs_or_err ) ~= 'table' or vim . isarray ( termdefs_or_err ) then 202broadcast_error ( 'E557: expected $NVIM_TERMDEFS to be table. :help $NVIM_TERMDEFS' ) 203return 204end 205return termdefs_or_err 206end 207end 208 209return M