From ffeb31c2f962b895bbc7111dd095e4b5ba27e88d Mon Sep 17 00:00:00 2001 From: Gregory Anders Date: Thu, 14 Dec 2023 11:37:45 -0600 Subject: [PATCH 1/2] fix(termcap): set 'nested' on TermResponse autocommand --- runtime/lua/vim/termcap.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/runtime/lua/vim/termcap.lua b/runtime/lua/vim/termcap.lua index e48657b3c2..ec29acca48 100644 --- a/runtime/lua/vim/termcap.lua +++ b/runtime/lua/vim/termcap.lua @@ -34,6 +34,7 @@ function M.query(caps, cb) local timer = assert(vim.uv.new_timer()) local id = vim.api.nvim_create_autocmd('TermResponse', { + nested = true, callback = function(args) local resp = args.data ---@type string local k, rest = resp:match('^\027P1%+r(%x+)(.*)$') From b0e2643cb222989354a4c66c639206c84389a519 Mon Sep 17 00:00:00 2001 From: Gregory Anders Date: Thu, 14 Dec 2023 11:11:46 -0600 Subject: [PATCH 2/2] refactor(defaults): defer setting 'termguicolors' until after VimEnter This ensures that any OptionSet autocommands will fire when the value is changed. --- runtime/lua/vim/_defaults.lua | 67 +++++++++++++++++------------------ 1 file changed, 32 insertions(+), 35 deletions(-) diff --git a/runtime/lua/vim/_defaults.lua b/runtime/lua/vim/_defaults.lua index b75a3dc69e..041a8cd669 100644 --- a/runtime/lua/vim/_defaults.lua +++ b/runtime/lua/vim/_defaults.lua @@ -175,6 +175,34 @@ for _, ui in ipairs(vim.api.nvim_list_uis()) do end if tty then + --- Set an option after startup (so that OptionSet is fired), but only if not + --- already set by the user. + --- + --- @param option string Option name + --- @param value string Option value + local function setoption(option, value) + if vim.api.nvim_get_option_info2(option, {}).was_set then + -- Don't do anything if option is already set + return + end + + -- Wait until Nvim is finished starting to set the option to ensure the + -- OptionSet event fires. + if vim.v.vim_did_enter == 1 then + if vim.o[option] ~= value then + vim.o[option] = value + end + else + vim.api.nvim_create_autocmd('VimEnter', { + once = true, + nested = true, + callback = function() + setoption(option, value) + end, + }) + end + end + --- Guess value of 'background' based on terminal color. --- --- We write Operating System Command (OSC) 11 to the terminal to request the @@ -253,30 +281,6 @@ 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) @@ -290,7 +294,7 @@ if tty then 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' - setbg(bg) + setoption('background', bg) end return true @@ -331,15 +335,8 @@ if tty then do if tty.rgb then -- The TUI was able to determine truecolor support - vim.o.termguicolors = true + setoption('termguicolors', true) else - --- Enable 'termguicolors', but only if it was not already set by the user. - local function settgc() - if not vim.api.nvim_get_option_info2('termguicolors', {}).was_set then - vim.o.termguicolors = true - end - end - local caps = {} ---@type table require('vim.termcap').query({ 'Tc', 'RGB', 'setrgbf', 'setrgbb' }, function(cap, found) if not found then @@ -348,7 +345,7 @@ if tty then caps[cap] = true if caps.Tc or caps.RGB or (caps.setrgbf and caps.setrgbb) then - settgc() + setoption('termguicolors', true) end end) @@ -395,7 +392,7 @@ if tty then and tonumber(params[#params - 1]) == g and tonumber(params[#params]) == b then - settgc() + setoption('termguicolors', true) end return true