aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGregory Anders <8965202+gpanders@users.noreply.github.com>2023-11-29 09:43:11 -0600
committerGitHub <noreply@github.com>2023-11-29 09:43:11 -0600
commit9b4b23493d6976613fc3e8b561c34bb4b808399c (patch)
treec0e6f61666b07749d6413c80f7afac03e90c33ec
parent86cc791debba09c8ed1aa0d863be844108866a38 (diff)
downloadrneovim-9b4b23493d6976613fc3e8b561c34bb4b808399c.tar.gz
rneovim-9b4b23493d6976613fc3e8b561c34bb4b808399c.tar.bz2
rneovim-9b4b23493d6976613fc3e8b561c34bb4b808399c.zip
fix(defaults): wait until VimEnter to set background (#26284)
The OptionSet autocommand does not fire until Vim has finished starting, so setting 'background' before the VimEnter event would not fire the OptionSet event. The prior implementation also waited until VimEnter to set 'background', so this was a regression introduced when moving background detection into Lua.
-rw-r--r--runtime/lua/vim/_defaults.lua33
1 files changed, 25 insertions, 8 deletions
diff --git a/runtime/lua/vim/_defaults.lua b/runtime/lua/vim/_defaults.lua
index 09d6d43e7a..cc872dea83 100644
--- a/runtime/lua/vim/_defaults.lua
+++ b/runtime/lua/vim/_defaults.lua
@@ -252,14 +252,33 @@ do
if tty then
local timer = assert(vim.uv.new_timer())
+ ---@param bg string New value of the 'background' option
+ local function setbg(bg)
+ if vim.api.nvim_get_option_info2('background', {}).was_set then
+ -- Don't do anything if 'background' is already set
+ return
+ end
+
+ -- Wait until Nvim is finished starting to set 'background' to ensure the
+ -- OptionSet event fires.
+ if vim.v.vim_did_enter == 1 then
+ if vim.o.background ~= bg then
+ vim.o.background = bg
+ end
+ else
+ vim.api.nvim_create_autocmd('VimEnter', {
+ once = true,
+ nested = true,
+ callback = function()
+ setbg(bg)
+ end,
+ })
+ end
+ end
+
local id = vim.api.nvim_create_autocmd('TermResponse', {
nested = true,
callback = function(args)
- if vim.api.nvim_get_option_info2('background', {}).was_set then
- -- Don't do anything if 'background' is already set
- return true
- end
-
local resp = args.data ---@type string
local r, g, b = parseosc11(resp)
if r and g and b then
@@ -270,9 +289,7 @@ 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'
- if bg ~= vim.o.background then
- vim.o.background = bg
- end
+ setbg(bg)
end
return true