robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
d9ed4c8566
master
1--- @class (private) TermFeatures 2--- @field osc52 boolean? 3 4local id =vim . api . nvim_create_augroup ( 'nvim.osc52' , { clear =true } ) 5vim . api . nvim_create_autocmd ( 'UIEnter' , { 6group =id , 7desc ='Enable OSC 52 feature flag if a supporting TUI is attached' , 8callback =function () 9-- If OSC 52 is explicitly disabled by the user then don't do anything 10if vim . g . termfeatures ~= nil and vim . g . termfeatures . osc52 == false then 11return 12end 13 14local tty =false 15for _ , ui in ipairs ( vim . api . nvim_list_uis ()) do 16if ui . stdout_tty then 17tty =true 18break 19end 20end 21 22-- Do not query when no TUI is attached 23if not tty then 24return 25end 26 27-- Clear existing OSC 52 value, since this is a new UI we might be attached to a different 28-- terminal 29do 30local termfeatures =vim . g . termfeatures or {} ---@type TermFeatures 31termfeatures . osc52 =nil 32vim . g . termfeatures =termfeatures 33end 34 35-- Check DA1 first 36vim . api . nvim_create_autocmd ( 'TermResponse' , { 37group =id , 38nested =true , 39callback =function ( ev ) 40local resp =ev . data . sequence ---@type string 41local params =resp : match ( '^\027%[%?([%d;]+)c$' ) 42if params then 43-- Check termfeatures again, it may have changed between the query and response. 44if vim . g . termfeatures ~= nil and vim . g . termfeatures . osc52 ~= nil then 45return true 46end 47 48for param in string . gmatch ( params , '%d+' ) do 49if param == '52' then 50local termfeatures =vim . g . termfeatures or {} ---@type TermFeatures 51termfeatures . osc52 =true 52vim . g . termfeatures =termfeatures 53return true 54end 55end 56 57-- Do not use XTGETTCAP on terminals that echo unknown sequences 58if vim . env . TERM_PROGRAM == 'Apple_Terminal' then 59return true 60end 61 62-- Fallback to XTGETTCAP 63vim . tty . query ( 'Ms' , function ( cap , found , seq ) 64if not found then 65return 66end 67 68-- Check termfeatures again, it may have changed between the query and response. 69if vim . g . termfeatures ~= nil and vim . g . termfeatures . osc52 ~= nil then 70return 71end 72 73assert ( cap == 'Ms' ) 74 75-- If the terminal reports a sequence other than OSC 52 for the Ms capability 76-- then ignore it. We only support OSC 52 (for now) 77if not seq or not seq : match ( '^\027%]52' ) then 78return 79end 80 81local termfeatures =vim . g . termfeatures or {} ---@type TermFeatures 82termfeatures . osc52 =true 83vim . g . termfeatures =termfeatures 84end ) 85 86return true 87end 88end , 89} ) 90 91-- Write DA1 request 92vim . api . nvim_ui_send ( '\027[c' ) 93end , 94} ) 95 96vim . api . nvim_create_autocmd ( 'UILeave' , { 97group =id , 98desc ='Reset OSC 52 feature flag if no TUIs are attached' , 99callback =function () 100-- If OSC 52 is explicitly disabled by the user then don't do anything 101if vim . g . termfeatures ~= nil and vim . g . termfeatures . osc52 == false then 102return 103end 104 105-- If no TUI is connected to Nvim's stdout then reset the OSC 52 term features flag 106for _ , ui in ipairs ( vim . api . nvim_list_uis ()) do 107if ui . stdout_tty then 108return 109end 110end 111 112local termfeatures =vim . g . termfeatures or {} ---@type TermFeatures 113termfeatures . osc52 =nil 114vim . g . termfeatures =termfeatures 115end , 116} )