aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGregory Anders <8965202+gpanders@users.noreply.github.com>2023-12-15 09:23:12 -0600
committerGitHub <noreply@github.com>2023-12-15 09:23:12 -0600
commit224b2ec20250db8afd7a9232cad9e35b49e253a7 (patch)
tree6bfaa8c869573ec5765d2d8e4dc6e1a3824588fb
parentd82168e41c21a3a107e52c139abc90dbe41f2010 (diff)
parentb0e2643cb222989354a4c66c639206c84389a519 (diff)
downloadrneovim-224b2ec20250db8afd7a9232cad9e35b49e253a7.tar.gz
rneovim-224b2ec20250db8afd7a9232cad9e35b49e253a7.tar.bz2
rneovim-224b2ec20250db8afd7a9232cad9e35b49e253a7.zip
Merge pull request #26579 from gpanders/defer-set-tgc
refactor(defaults): defer setting 'termguicolors' until after VimEnter
-rw-r--r--runtime/lua/vim/_defaults.lua67
-rw-r--r--runtime/lua/vim/termcap.lua1
2 files changed, 33 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<string, boolean>
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
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+)(.*)$')