diff options
| author | Gregory Anders <greg@gpanders.com> | 2025-07-17 18:47:33 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-07-17 18:47:33 -0500 |
| commit | e0d179561dc813b813703cc1a092be23ff8866b5 (patch) | |
| tree | 6dd51bf6e420a765cd3a052278be41ff2928853a /runtime/plugin | |
| parent | 9d1333a385fc9e00716215c14c73a4254e048a39 (diff) | |
| parent | 112092271be5993e8050665b066a0e924463b2a7 (diff) | |
Merge pull request #34860 from gpanders/push-lorwmnmtysnt
feat(tui): use DA1 response to determine OSC 52 support
Diffstat (limited to 'runtime/plugin')
| -rw-r--r-- | runtime/plugin/osc52.lua | 82 |
1 files changed, 63 insertions, 19 deletions
diff --git a/runtime/plugin/osc52.lua b/runtime/plugin/osc52.lua index e9173f6a1b..3ca7c5e3c0 100644 --- a/runtime/plugin/osc52.lua +++ b/runtime/plugin/osc52.lua @@ -19,33 +19,77 @@ vim.api.nvim_create_autocmd('UIEnter', { 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 + -- Do not query when no TUI is attached + if not tty then + return + end + + -- Clear existing OSC 52 value, since this is a new UI we might be attached to a different + -- terminal + do 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 + -- Check DA1 first + vim.api.nvim_create_autocmd('TermResponse', { + group = id, + nested = true, + callback = function(args) + local resp = args.data.sequence ---@type string + local params = resp:match('^\027%[%?([%d;]+)c$') + if params then + -- Check termfeatures again, it may have changed between the query and response. + if vim.g.termfeatures ~= nil and vim.g.termfeatures.osc52 ~= nil then + return true + end - assert(cap == 'Ms') + for param in string.gmatch(params, '%d+') do + if param == '52' then + local termfeatures = vim.g.termfeatures or {} ---@type TermFeatures + termfeatures.osc52 = true + vim.g.termfeatures = termfeatures + return true + end + end - -- 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 + -- Do not use XTGETTCAP on terminals that echo unknown sequences + if vim.env.TERM_PROGRAM == 'Apple_Terminal' then + return true + end - local termfeatures = vim.g.termfeatures or {} ---@type TermFeatures - termfeatures.osc52 = true - vim.g.termfeatures = termfeatures - end) + -- Fallback to XTGETTCAP + require('vim.termcap').query('Ms', function(cap, found, seq) + if not found then + return + end + + -- Check termfeatures again, it may have changed between the query and response. + if vim.g.termfeatures ~= nil and vim.g.termfeatures.osc52 ~= nil 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) + + return true + end + end, + }) + + -- Write DA1 request + io.stdout:write('\027[c') end, }) |
