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 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
if not tty then
return
end
do
local termfeatures = vim.g.termfeatures or {}
termfeatures.osc52 = nil
vim.g.termfeatures = termfeatures
end
vim.api.nvim_create_autocmd('TermResponse', {
group = id,
nested = true,
callback = function(args)
local resp = args.data.sequence
local params = resp:match('^\027%[%?([%d;]+)c$')
if params then
if vim.g.termfeatures ~= nil and vim.g.termfeatures.osc52 ~= nil then
return true
end
for param in string.gmatch(params, '%d+') do
if param == '52' then
local termfeatures = vim.g.termfeatures or {}
termfeatures.osc52 = true
vim.g.termfeatures = termfeatures
return true
end
end
if vim.env.TERM_PROGRAM == 'Apple_Terminal' then
return true
end
require('vim.termcap').query('Ms', function(cap, found, seq)
if not found then
return
end
if vim.g.termfeatures ~= nil and vim.g.termfeatures.osc52 ~= nil then
return
end
assert(cap == 'Ms')
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)
return true
end
end,
})
vim.api.nvim_ui_send('\027[c')
end,
})
vim.api.nvim_create_autocmd('UILeave', {
group = id,
desc = 'Reset OSC 52 feature flag if no TUIs are attached',
callback = function()
if vim.g.termfeatures ~= nil and vim.g.termfeatures.osc52 == false then
return
end
for _, ui in ipairs(vim.api.nvim_list_uis()) do
if ui.stdout_tty then
return
end
end
local termfeatures = vim.g.termfeatures or {}
termfeatures.osc52 = nil
vim.g.termfeatures = termfeatures
end,
})