diff options
Diffstat (limited to 'runtime/lua/vim/_defaults.lua')
-rw-r--r-- | runtime/lua/vim/_defaults.lua | 71 |
1 files changed, 36 insertions, 35 deletions
diff --git a/runtime/lua/vim/_defaults.lua b/runtime/lua/vim/_defaults.lua index 2db82a04a1..2627cbcd0d 100644 --- a/runtime/lua/vim/_defaults.lua +++ b/runtime/lua/vim/_defaults.lua @@ -175,6 +175,32 @@ 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 + vim.o[option] = value + 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 +279,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 +292,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 @@ -299,9 +301,15 @@ if tty then }) local query = '\027]11;?\007' + + -- tmux 3.3a and earlier do not query the parent terminal for background color. As of the + -- writing of this comment, 3.3a is the latest release, so a passthrough sequence is necessary. + -- The passthrough should be removed as soon as a tmux version later than 3.3a is released. + -- See: https://github.com/neovim/neovim/pull/26557 if os.getenv('TMUX') then query = string.format('\027Ptmux;%s\027\\', query:gsub('\027', '\027\027')) end + io.stdout:write(query) timer:start(1000, 0, function() @@ -325,15 +333,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 @@ -342,7 +343,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) @@ -389,7 +390,7 @@ if tty then and tonumber(params[#params - 1]) == g and tonumber(params[#params]) == b then - settgc() + setoption('termguicolors', true) end return true |