diff --git a/runtime/lua/vim/_core/defaults.lua b/runtime/lua/vim/_core/defaults.lua index 4414608cbf..20b21c3082 100644 --- a/runtime/lua/vim/_core/defaults.lua +++ b/runtime/lua/vim/_core/defaults.lua @@ -964,8 +964,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' - -- Use :noautocmd to suppress OptionSet event; OSC11 response may arrive after VimEnter. - vim.cmd('noautocmd set background=' .. bg) + vim.o.background = bg end end end diff --git a/test/functional/terminal/tui_spec.lua b/test/functional/terminal/tui_spec.lua index 3087268e94..1e216fb7c5 100644 --- a/test/functional/terminal/tui_spec.lua +++ b/test/functional/terminal/tui_spec.lua @@ -4642,30 +4642,6 @@ describe('TUI bg color', function() end) end) - it('does not trigger OptionSet from automatic background processing', function() - command('set background=light') - local child_server = new_pipename() - local screen = tt.setup_child_nvim({ - '--clean', - '--listen', - child_server, - '--cmd', - 'colorscheme vim', - '--cmd', - 'set noswapfile', - '-c', - [[let g:background_optionset = 0]], - '-c', - [[autocmd OptionSet background let g:background_optionset += 1]], - }) - screen:expect({ any = '%[No Name%]' }) - local child_session = n.connect(child_server) - retry(nil, nil, function() - eq({ true, 'light' }, { child_session:request('nvim_eval', '&background') }) - end) - eq({ true, 0 }, { child_session:request('nvim_eval', 'g:background_optionset') }) - end) - it('sends theme update notifications when background changes #31652', function() command('set background=dark') -- set outer Nvim background local child_server = new_pipename() @@ -4677,6 +4653,10 @@ describe('TUI bg color', function() 'colorscheme vim', '--cmd', 'set noswapfile', + '-c', + [[autocmd OptionSet background let g:background_option_old = v:option_old]], + '-c', + [[autocmd OptionSet background let g:background_option_new = v:option_new]], }) screen:expect({ any = '%[No Name%]' }) local child_session = n.connect(child_server) @@ -4687,6 +4667,9 @@ describe('TUI bg color', function() retry(nil, nil, function() eq({ true, 'light' }, { child_session:request('nvim_eval', '&background') }) end) + -- Make sure it triggered OptionSet with v:option_* values #38551 #41146 + eq({ true, 'dark' }, { child_session:request('nvim_eval', 'g:background_option_old') }) + eq({ true, 'light' }, { child_session:request('nvim_eval', 'g:background_option_new') }) end) -- Start a headless server (the outer Nvim is its OSC-11-answering terminal),