diff options
author | Gregory Anders <greg@gpanders.com> | 2025-03-12 08:11:19 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-03-12 08:11:19 -0500 |
commit | 04181070746a51d2f11ce5fc96d2696ea267ff70 (patch) | |
tree | e271d9a3868b445a5b147267937a7411b224daed /runtime/plugin/osc52.lua | |
parent | 69a19295f8fe90356011eff2b7fa67b0593fffcc (diff) | |
download | rneovim-04181070746a51d2f11ce5fc96d2696ea267ff70.tar.gz rneovim-04181070746a51d2f11ce5fc96d2696ea267ff70.tar.bz2 rneovim-04181070746a51d2f11ce5fc96d2696ea267ff70.zip |
fix: update osc52 termfeatures flag on UIEnter/UILeave (#32756)
Problem:
Nvim tries to use OSC 52 even when no TUIs are attached.
Solution:
On each UIEnter/UILeave event, check that there is a TUI client connected to Nvim's stdout.
Diffstat (limited to 'runtime/plugin/osc52.lua')
-rw-r--r-- | runtime/plugin/osc52.lua | 109 |
1 files changed, 72 insertions, 37 deletions
diff --git a/runtime/plugin/osc52.lua b/runtime/plugin/osc52.lua index c7f1cbe2e3..e9173f6a1b 100644 --- a/runtime/plugin/osc52.lua +++ b/runtime/plugin/osc52.lua @@ -1,37 +1,72 @@ -local tty = false -for _, ui in ipairs(vim.api.nvim_list_uis()) do - if ui.chan == 1 and ui.stdout_tty then - tty = true - break - end -end - --- Do not query when any of the following is true: --- * TUI is not attached --- * OSC 52 support is explicitly disabled via g:termfeatures --- * Using a badly behaved terminal -if - not tty - or (vim.g.termfeatures ~= nil and vim.g.termfeatures.osc52 == false) - or vim.env.TERM_PROGRAM == 'Apple_Terminal' -then - return -end - -require('vim.termcap').query('Ms', function(cap, found, seq) - if not found then - return - end - - assert(cap == 'Ms') - - -- If the terminal reports a sequence other than OSC 52 for the Ms capability - -- then ignore it. We only support OSC 52 (for now) - if not seq or not seq:match('^\027%]52') then - return - end - - local termfeatures = vim.g.termfeatures or {} - termfeatures.osc52 = true - vim.g.termfeatures = termfeatures -end) +--- @class (private) TermFeatures +--- @field osc52 boolean? + +local id = vim.api.nvim_create_augroup('nvim.osc52', { clear = true }) +vim.api.nvim_create_autocmd('UIEnter', { + group = id, + desc = 'Enable OSC 52 feature flag if a supporting TUI is attached', + callback = function() + -- If OSC 52 is explicitly disabled by the user then don't do anything + if vim.g.termfeatures ~= nil and vim.g.termfeatures.osc52 == false then + return + end + + local tty = false + for _, ui in ipairs(vim.api.nvim_list_uis()) do + if ui.stdout_tty then + tty = true + break + end + end + + -- Do not query when any of the following is true: + -- * No TUI is attached + -- * Using a badly behaved terminal + if not tty or vim.env.TERM_PROGRAM == 'Apple_Terminal' then + local termfeatures = vim.g.termfeatures or {} ---@type TermFeatures + termfeatures.osc52 = nil + vim.g.termfeatures = termfeatures + return + end + + require('vim.termcap').query('Ms', function(cap, found, seq) + if not found then + return + end + + assert(cap == 'Ms') + + -- If the terminal reports a sequence other than OSC 52 for the Ms capability + -- then ignore it. We only support OSC 52 (for now) + if not seq or not seq:match('^\027%]52') then + return + end + + local termfeatures = vim.g.termfeatures or {} ---@type TermFeatures + termfeatures.osc52 = true + vim.g.termfeatures = termfeatures + end) + end, +}) + +vim.api.nvim_create_autocmd('UILeave', { + group = id, + desc = 'Reset OSC 52 feature flag if no TUIs are attached', + callback = function() + -- If OSC 52 is explicitly disabled by the user then don't do anything + if vim.g.termfeatures ~= nil and vim.g.termfeatures.osc52 == false then + return + end + + -- If no TUI is connected to Nvim's stdout then reset the OSC 52 term features flag + for _, ui in ipairs(vim.api.nvim_list_uis()) do + if ui.stdout_tty then + return + end + end + + local termfeatures = vim.g.termfeatures or {} ---@type TermFeatures + termfeatures.osc52 = nil + vim.g.termfeatures = termfeatures + end, +}) |