mirror of
https://github.com/neovim/neovim.git
synced 2026-03-30 20:32:08 +00:00
fix(startup): wait for bg detection before user config #37075
Problem: Automatic background detection sets the background option too late, which loads colorschemes twice and causes problems when the user's terminal background doesn't match the default (#32109, #36211, #36416). Solution: Use a DA1 query to determine whether the TTY supports OSC 11. Wait for background detection and setting to complete before processing user config. Note: To preserve the existing behavior as much as possible, this triggers OptionSet manually on VimEnter (since it won't trigger automatically if we set bg during startup). However, I'm unsure if this behavior is truly desired given that the documentation says OptionSet is triggered "After setting an option (except during |startup|)." Also fixes flickering issue #28667. To check for flickering: nvim --clean --cmd "set termguicolors" --cmd "echo \"foo\"" --cmd "sleep 10" On master, this gives me a black screen for 10 seconds, but on this branch, the background is dark or light depending on the terminal background (since the option is now set during startup rather than after VimEnter).
This commit is contained in:
@@ -814,13 +814,25 @@ do
|
||||
-- an OSC 11 response from the terminal emulator. If the user has set
|
||||
-- 'background' explicitly then we will delete this autocommand,
|
||||
-- effectively disabling automatic background setting.
|
||||
local force = false
|
||||
local did_dsr_response = false
|
||||
local id = vim.api.nvim_create_autocmd('TermResponse', {
|
||||
group = group,
|
||||
nested = true,
|
||||
desc = "Update the value of 'background' automatically based on the terminal emulator's background color",
|
||||
callback = function(args)
|
||||
local resp = args.data.sequence ---@type string
|
||||
|
||||
-- DSR response that should come after the OSC 11 response if the
|
||||
-- terminal supports it.
|
||||
if string.match(resp, '^\027%[0n$') then
|
||||
did_dsr_response = true
|
||||
-- Don't delete the autocmd because the bg response may come
|
||||
-- after the DSR response if the terminal handles requests out
|
||||
-- of sequence. In that case, the background will simply be set
|
||||
-- later in the startup sequence.
|
||||
return false
|
||||
end
|
||||
|
||||
local r, g, b = parseosc11(resp)
|
||||
if r and g and b then
|
||||
local rr = parsecolor(r)
|
||||
@@ -830,15 +842,20 @@ do
|
||||
if rr and gg and bb then
|
||||
local luminance = (0.299 * rr) + (0.587 * gg) + (0.114 * bb)
|
||||
local bg = luminance < 0.5 and 'dark' or 'light'
|
||||
setoption('background', bg, force)
|
||||
vim.api.nvim_set_option_value('background', bg, {})
|
||||
|
||||
-- On the first query response, don't force setting the option in
|
||||
-- case the user has already set it manually. If they have, then
|
||||
-- this autocommand will be deleted. If they haven't, then we do
|
||||
-- want to force setting the option to override the value set by
|
||||
-- this autocommand.
|
||||
if not force then
|
||||
force = true
|
||||
-- Ensure OptionSet still triggers when we set the background during startup
|
||||
if vim.v.vim_did_enter == 0 then
|
||||
vim.api.nvim_create_autocmd('VimEnter', {
|
||||
group = group,
|
||||
once = true,
|
||||
nested = true,
|
||||
callback = function()
|
||||
vim.api.nvim_exec_autocmds('OptionSet', {
|
||||
pattern = 'background',
|
||||
})
|
||||
end,
|
||||
})
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -850,13 +867,37 @@ do
|
||||
nested = true,
|
||||
once = true,
|
||||
callback = function()
|
||||
if vim.api.nvim_get_option_info2('background', {}).was_set then
|
||||
local optinfo = vim.api.nvim_get_option_info2('background', {})
|
||||
local sid_lua = -8
|
||||
if
|
||||
optinfo.was_set
|
||||
and optinfo.last_set_sid ~= sid_lua
|
||||
and next(vim.api.nvim_get_autocmds({ id = id })) ~= nil
|
||||
then
|
||||
vim.api.nvim_del_autocmd(id)
|
||||
end
|
||||
end,
|
||||
})
|
||||
|
||||
vim.api.nvim_ui_send('\027]11;?\007')
|
||||
-- Send OSC 11 query along with DSR sequence to determine whether
|
||||
-- terminal supports the query. If the DSR response comes first,
|
||||
-- the terminal most likely doesn't support the bg color query,
|
||||
-- and we don't have to keep waiting for a bg color response.
|
||||
-- #32109
|
||||
local osc11 = '\027]11;?\007'
|
||||
local dsr = '\027[5n'
|
||||
vim.api.nvim_ui_send(osc11 .. dsr)
|
||||
|
||||
-- Wait until detection of OSC 11 capabilities is complete to
|
||||
-- ensure background is automatically set before user config.
|
||||
if not vim.wait(100, function()
|
||||
return did_dsr_response
|
||||
end, 1) then
|
||||
vim.notify(
|
||||
'defaults.lua: Did not detect DSR response from terminal. This results in a slower startup time.',
|
||||
vim.log.levels.WARN
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
--- If the TUI (term_has_truecolor) was able to determine that the host
|
||||
|
||||
Reference in New Issue
Block a user