From b32b5b2711ed338f4944c983a247f80c3163f87c Mon Sep 17 00:00:00 2001 From: Gregory Anders <8965202+gpanders@users.noreply.github.com> Date: Thu, 30 Nov 2023 16:17:38 -0600 Subject: fix: use BEL to terminate OSC 11 request (#26335) --- runtime/lua/vim/_defaults.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'runtime/lua/vim/_defaults.lua') diff --git a/runtime/lua/vim/_defaults.lua b/runtime/lua/vim/_defaults.lua index cc872dea83..c3bb36fc36 100644 --- a/runtime/lua/vim/_defaults.lua +++ b/runtime/lua/vim/_defaults.lua @@ -297,7 +297,7 @@ do end, }) - io.stdout:write('\027]11;?\027\\') + io.stdout:write('\027]11;?\007') timer:start(1000, 0, function() -- No response received. Delete the autocommand -- cgit From 2613ba5000d4c0d9b15e2eec2d2b97615575925e Mon Sep 17 00:00:00 2001 From: Gregory Anders Date: Tue, 5 Dec 2023 10:01:32 -0800 Subject: feat(defaults): enable 'termguicolors' by default when supported by terminal Enable 'termguicolors' automatically when Nvim can detect that truecolor is supported by the host terminal. If $COLORTERM is set to "truecolor" or "24bit", or the terminal's terminfo entry contains capabilities for Tc, RGB, or setrgbf and setrgbb, then we assume that the terminal supports truecolor. Otherwise, the terminal is queried (using both XTGETTCAP and SGR + DECRQSS). If the terminal's response to these queries (if any) indicates that it supports truecolor, then 'termguicolors' is enabled. --- runtime/lua/vim/_defaults.lua | 255 +++++++++++++++++++++++++++++------------- 1 file changed, 180 insertions(+), 75 deletions(-) (limited to 'runtime/lua/vim/_defaults.lua') diff --git a/runtime/lua/vim/_defaults.lua b/runtime/lua/vim/_defaults.lua index c3bb36fc36..b73681be04 100644 --- a/runtime/lua/vim/_defaults.lua +++ b/runtime/lua/vim/_defaults.lua @@ -165,91 +165,92 @@ do }) end ---- Guess value of 'background' based on terminal color. ---- ---- We write Operating System Command (OSC) 11 to the terminal to request the ---- terminal's background color. We then wait for a response. If the response ---- matches `rgba:RRRR/GGGG/BBBB/AAAA` where R, G, B, and A are hex digits, then ---- compute the luminance[1] of the RGB color and classify it as light/dark ---- accordingly. Note that the color components may have anywhere from one to ---- four hex digits, and require scaling accordingly as values out of 4, 8, 12, ---- or 16 bits. Also note the A(lpha) component is optional, and is parsed but ---- ignored in the calculations. ---- ---- [1] https://en.wikipedia.org/wiki/Luma_%28video%29 -do - --- Parse a string of hex characters as a color. - --- - --- The string can contain 1 to 4 hex characters. The returned value is - --- between 0.0 and 1.0 (inclusive) representing the intensity of the color. - --- - --- For instance, if only a single hex char "a" is used, then this function - --- returns 0.625 (10 / 16), while a value of "aa" would return 0.664 (170 / - --- 256). - --- - --- @param c string Color as a string of hex chars - --- @return number? Intensity of the color - local function parsecolor(c) - if #c == 0 or #c > 4 then - return nil - end - - local val = tonumber(c, 16) - if not val then - return nil - end - - local max = tonumber(string.rep('f', #c), 16) - return val / max +-- Only do the following when the TUI is attached +local tty = nil +for _, ui in ipairs(vim.api.nvim_list_uis()) do + if ui.chan == 1 and ui.stdout_tty then + tty = ui + break end +end - --- Parse an OSC 11 response - --- - --- Either of the two formats below are accepted: - --- - --- OSC 11 ; rgb:// - --- - --- or - --- - --- OSC 11 ; rgba:/// +if tty then + --- Guess value of 'background' based on terminal color. --- - --- where + --- We write Operating System Command (OSC) 11 to the terminal to request the + --- terminal's background color. We then wait for a response. If the response + --- matches `rgba:RRRR/GGGG/BBBB/AAAA` where R, G, B, and A are hex digits, then + --- compute the luminance[1] of the RGB color and classify it as light/dark + --- accordingly. Note that the color components may have anywhere from one to + --- four hex digits, and require scaling accordingly as values out of 4, 8, 12, + --- or 16 bits. Also note the A(lpha) component is optional, and is parsed but + --- ignored in the calculations. --- - --- , , , := h | hh | hhh | hhhh - --- - --- The alpha component is ignored, if present. - --- - --- @param resp string OSC 11 response - --- @return string? Red component - --- @return string? Green component - --- @return string? Blue component - local function parseosc11(resp) - local r, g, b - r, g, b = resp:match('^\027%]11;rgb:(%x+)/(%x+)/(%x+)$') - if not r and not g and not b then - local a - r, g, b, a = resp:match('^\027%]11;rgba:(%x+)/(%x+)/(%x+)/(%x+)$') - if not a or #a > 4 then - return nil, nil, nil + --- [1] https://en.wikipedia.org/wiki/Luma_%28video%29 + do + --- Parse a string of hex characters as a color. + --- + --- The string can contain 1 to 4 hex characters. The returned value is + --- between 0.0 and 1.0 (inclusive) representing the intensity of the color. + --- + --- For instance, if only a single hex char "a" is used, then this function + --- returns 0.625 (10 / 16), while a value of "aa" would return 0.664 (170 / + --- 256). + --- + --- @param c string Color as a string of hex chars + --- @return number? Intensity of the color + local function parsecolor(c) + if #c == 0 or #c > 4 then + return nil end - end - if r and g and b and #r <= 4 and #g <= 4 and #b <= 4 then - return r, g, b + local val = tonumber(c, 16) + if not val then + return nil + end + + local max = tonumber(string.rep('f', #c), 16) + return val / max end - return nil, nil, nil - end + --- Parse an OSC 11 response + --- + --- Either of the two formats below are accepted: + --- + --- OSC 11 ; rgb:// + --- + --- or + --- + --- OSC 11 ; rgba:/// + --- + --- where + --- + --- , , , := h | hh | hhh | hhhh + --- + --- The alpha component is ignored, if present. + --- + --- @param resp string OSC 11 response + --- @return string? Red component + --- @return string? Green component + --- @return string? Blue component + local function parseosc11(resp) + local r, g, b + r, g, b = resp:match('^\027%]11;rgb:(%x+)/(%x+)/(%x+)$') + if not r and not g and not b then + local a + r, g, b, a = resp:match('^\027%]11;rgba:(%x+)/(%x+)/(%x+)/(%x+)$') + if not a or #a > 4 then + return nil, nil, nil + end + end + + if r and g and b and #r <= 4 and #g <= 4 and #b <= 4 then + return r, g, b + end - local tty = false - for _, ui in ipairs(vim.api.nvim_list_uis()) do - if ui.chan == 1 and ui.stdout_tty then - tty = true - break + return nil, nil, nil end - end - if tty then local timer = assert(vim.uv.new_timer()) ---@param bg string New value of the 'background' option @@ -300,7 +301,7 @@ do io.stdout:write('\027]11;?\007') timer:start(1000, 0, function() - -- No response received. Delete the autocommand + -- Delete the autocommand if no response was received vim.schedule(function() -- Suppress error if autocommand has already been deleted pcall(vim.api.nvim_del_autocmd, id) @@ -311,4 +312,108 @@ do end end) end + + --- If the TUI (term_has_truecolor) was able to determine that the host + --- terminal supports truecolor, enable 'termguicolors'. Otherwise, query the + --- terminal (using both XTGETTCAP and SGR + DECRQSS). If the terminal's + --- response indicates that it does support truecolor enable 'termguicolors', + --- but only if the user has not already disabled it. + do + if tty.rgb then + -- The TUI was able to determine truecolor support + vim.o.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 + return + end + + caps[cap] = true + if caps.Tc or caps.RGB or (caps.setrgbf and caps.setrgbb) then + settgc() + end + end) + + local timer = assert(vim.uv.new_timer()) + + -- Arbitrary colors to set in the SGR sequence + local r = 1 + local g = 2 + local b = 3 + + local id = vim.api.nvim_create_autocmd('TermResponse', { + nested = true, + callback = function(args) + local resp = args.data ---@type string + local decrqss = resp:match('^\027P1%$r([%d;:]+)m$') + + if decrqss then + -- The DECRQSS SGR response first contains attributes separated by + -- semicolons, followed by the SGR itself with parameters separated + -- by colons. Some terminals include "0" in the attribute list + -- unconditionally; others do not. Our SGR sequence did not set any + -- attributes, so there should be no attributes in the list. + local attrs = vim.split(decrqss, ';') + if #attrs ~= 1 and (#attrs ~= 2 or attrs[1] ~= '0') then + return true + end + + -- The returned SGR sequence should begin with 48:2 + local sgr = attrs[#attrs]:match('^48:2:([%d:]+)$') + if not sgr then + return true + end + + -- The remaining elements of the SGR sequence should be the 3 colors + -- we set. Some terminals also include an additional parameter + -- (which can even be empty!), so handle those cases as well + local params = vim.split(sgr, ':') + if #params ~= 3 and (#params ~= 4 or (params[1] ~= '' and params[1] ~= '1')) then + return true + end + + if + tonumber(params[#params - 2]) == r + and tonumber(params[#params - 1]) == g + and tonumber(params[#params]) == b + then + settgc() + end + + return true + end + end, + }) + + -- Write SGR followed by DECRQSS. This sets the background color then + -- immediately asks the terminal what the background color is. If the + -- terminal responds to the DECRQSS with the same SGR sequence that we + -- sent then the terminal supports truecolor. + local decrqss = '\027P$qm\027\\' + if os.getenv('TMUX') then + decrqss = string.format('\027Ptmux;%s\027\\', decrqss:gsub('\027', '\027\027')) + end + io.stdout:write(string.format('\027[48;2;%d;%d;%dm%s', r, g, b, decrqss)) + + timer:start(1000, 0, function() + -- Delete the autocommand if no response was received + vim.schedule(function() + -- Suppress error if autocommand has already been deleted + pcall(vim.api.nvim_del_autocmd, id) + end) + + if not timer:is_closing() then + timer:close() + end + end) + end + end end -- cgit From 5aa1ba3efe0597a5f508b8220961c75c3359ccdb Mon Sep 17 00:00:00 2001 From: Jaehwang Jung Date: Thu, 14 Dec 2023 07:16:21 +0900 Subject: fix(defaults): background detection in tmux (#26557) Wrap the query with passthrough sequence --- runtime/lua/vim/_defaults.lua | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'runtime/lua/vim/_defaults.lua') diff --git a/runtime/lua/vim/_defaults.lua b/runtime/lua/vim/_defaults.lua index b73681be04..2db82a04a1 100644 --- a/runtime/lua/vim/_defaults.lua +++ b/runtime/lua/vim/_defaults.lua @@ -298,7 +298,11 @@ if tty then end, }) - io.stdout:write('\027]11;?\007') + local query = '\027]11;?\007' + 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() -- Delete the autocommand if no response was received -- cgit From ddcbc5c78a492b55951cd60ba91d3d4cbf35a5c5 Mon Sep 17 00:00:00 2001 From: Gregory Anders <8965202+gpanders@users.noreply.github.com> Date: Thu, 14 Dec 2023 08:38:26 -0600 Subject: docs: add comment for OSC 11 tmux passthrough (#26566) --- runtime/lua/vim/_defaults.lua | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'runtime/lua/vim/_defaults.lua') diff --git a/runtime/lua/vim/_defaults.lua b/runtime/lua/vim/_defaults.lua index 2db82a04a1..b75a3dc69e 100644 --- a/runtime/lua/vim/_defaults.lua +++ b/runtime/lua/vim/_defaults.lua @@ -299,9 +299,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() -- cgit From b0e2643cb222989354a4c66c639206c84389a519 Mon Sep 17 00:00:00 2001 From: Gregory Anders Date: Thu, 14 Dec 2023 11:11:46 -0600 Subject: 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(-) (limited to 'runtime/lua/vim/_defaults.lua') 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 -- cgit From f4f7e294695abe7837af62b68564114de7abd0b4 Mon Sep 17 00:00:00 2001 From: Gregory Anders <8965202+gpanders@users.noreply.github.com> Date: Fri, 15 Dec 2023 16:35:55 -0600 Subject: refactor(defaults): always set options, even if value hasn't changed (#26595) Comparing against the old value before setting matched the original C implementation, but there is no reason to use this restriction. In particular, this inhibits using OptionSet to determine when the option was set. If users need to handle a case where the option _changed_, it is easy to do so in an OptionSet autocommand using v:option_new and v:option_old (and friends). --- runtime/lua/vim/_defaults.lua | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'runtime/lua/vim/_defaults.lua') diff --git a/runtime/lua/vim/_defaults.lua b/runtime/lua/vim/_defaults.lua index 041a8cd669..2627cbcd0d 100644 --- a/runtime/lua/vim/_defaults.lua +++ b/runtime/lua/vim/_defaults.lua @@ -189,9 +189,7 @@ if tty then -- 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 + vim.o[option] = value else vim.api.nvim_create_autocmd('VimEnter', { once = true, -- cgit From c26dc1f77c792fe5cbefd578dc8d1e23c80d3688 Mon Sep 17 00:00:00 2001 From: Nacho Nieva <83428506+NachoNievaG@users.noreply.github.com> Date: Tue, 26 Dec 2023 20:26:18 -0300 Subject: feat(defaults): map Q and @x to repeat in Visual mode (#26495) --- runtime/lua/vim/_defaults.lua | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'runtime/lua/vim/_defaults.lua') diff --git a/runtime/lua/vim/_defaults.lua b/runtime/lua/vim/_defaults.lua index 2627cbcd0d..0d756b8701 100644 --- a/runtime/lua/vim/_defaults.lua +++ b/runtime/lua/vim/_defaults.lua @@ -67,6 +67,21 @@ do --- See |&-default| vim.keymap.set('n', '&', ':&&', { desc = ':help &-default' }) + --- Use Q in visual mode to execute a macro on each line of the selection. #21422 + --- + --- Applies to @x and includes @@ too. + vim.keymap.set( + 'x', + 'Q', + ':normal! @=reg_recorded()', + { silent = true, desc = ':help v_Q-default' } + ) + vim.keymap.set( + 'x', + '@', + "':normal! @'.getcharstr().''", + { silent = true, expr = true, desc = ':help v_@-default' } + ) --- Map |gx| to call |vim.ui.open| on the identifier under the cursor do -- TODO: use vim.region() when it lands... #13896 #16843 -- cgit From 714b075197c02d27d5c05133759ae97bed279b50 Mon Sep 17 00:00:00 2001 From: dundargoc <33953936+dundargoc@users.noreply.github.com> Date: Thu, 28 Dec 2023 03:28:48 +0100 Subject: docs: small fixes (#26651) Co-authored-by: Gregory Anders Co-authored-by: WillLillis --- runtime/lua/vim/_defaults.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'runtime/lua/vim/_defaults.lua') diff --git a/runtime/lua/vim/_defaults.lua b/runtime/lua/vim/_defaults.lua index 0d756b8701..af962eea5d 100644 --- a/runtime/lua/vim/_defaults.lua +++ b/runtime/lua/vim/_defaults.lua @@ -194,7 +194,7 @@ if tty then --- already set by the user. --- --- @param option string Option name - --- @param value string Option value + --- @param value any 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 -- cgit From 8df37423781493f58de060e1c9219cd1c3768130 Mon Sep 17 00:00:00 2001 From: Gregory Anders <8965202+gpanders@users.noreply.github.com> Date: Sat, 6 Jan 2024 18:08:29 -0600 Subject: fix(defaults): use augroup for default autocommands (#26933) --- runtime/lua/vim/_defaults.lua | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'runtime/lua/vim/_defaults.lua') diff --git a/runtime/lua/vim/_defaults.lua b/runtime/lua/vim/_defaults.lua index af962eea5d..64eb638fd7 100644 --- a/runtime/lua/vim/_defaults.lua +++ b/runtime/lua/vim/_defaults.lua @@ -190,6 +190,8 @@ for _, ui in ipairs(vim.api.nvim_list_uis()) do end if tty then + local group = vim.api.nvim_create_augroup('nvim_tty', {}) + --- Set an option after startup (so that OptionSet is fired), but only if not --- already set by the user. --- @@ -207,6 +209,7 @@ if tty then vim.o[option] = value else vim.api.nvim_create_autocmd('VimEnter', { + group = group, once = true, nested = true, callback = function() @@ -295,6 +298,7 @@ if tty then local timer = assert(vim.uv.new_timer()) local id = vim.api.nvim_create_autocmd('TermResponse', { + group = group, nested = true, callback = function(args) local resp = args.data ---@type string @@ -370,6 +374,7 @@ if tty then local b = 3 local id = vim.api.nvim_create_autocmd('TermResponse', { + group = group, nested = true, callback = function(args) local resp = args.data ---@type string -- cgit From 7589336120a258cf75134a5243b2f6b1926ac85b Mon Sep 17 00:00:00 2001 From: Daniel Steinberg Date: Mon, 15 Jan 2024 11:12:07 -0500 Subject: feat(terminal): respond to OSC background and foreground request (#17197) The motivation for this update is Issue #15365, where background=light is not properly set for Nvim running from an Nvim :terminal. This can be encountered when e.g., opening a terminal to make git commits, which opens EDITOR=nvim in the nested terminal. Under the implementation of this commit, the OSC response always indicates a black or white foreground/background. While this may not reflect the actual foreground/background color, it permits 'background' to be retained for a nested Nvim instance running in the terminal emulator. The behaviour matches Vim. --- runtime/lua/vim/_defaults.lua | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'runtime/lua/vim/_defaults.lua') diff --git a/runtime/lua/vim/_defaults.lua b/runtime/lua/vim/_defaults.lua index 64eb638fd7..07850a5a47 100644 --- a/runtime/lua/vim/_defaults.lua +++ b/runtime/lua/vim/_defaults.lua @@ -155,6 +155,30 @@ do end, }) + vim.api.nvim_create_autocmd('TermRequest', { + group = nvim_terminal_augroup, + desc = 'Respond to OSC foreground/background color requests', + callback = function(args) + local fg_request = args.data == '\027]10;?' + local bg_request = args.data == '\027]11;?' + if fg_request or bg_request then + -- WARN: This does not return the actual foreground/background color, + -- but rather returns: + -- - fg=white/bg=black when Nvim option 'background' is 'dark' + -- - fg=black/bg=white when Nvim option 'background' is 'light' + local red, green, blue = 0, 0, 0 + local bg_option_dark = vim.o.background == 'dark' + if (fg_request and bg_option_dark) or (bg_request and not bg_option_dark) then + red, green, blue = 65535, 65535, 65535 + end + local command = fg_request and 10 or 11 + local data = string.format('\027]%d;rgb:%04x/%04x/%04x\007', command, red, green, blue) + local channel = vim.bo[args.buf].channel + vim.api.nvim_chan_send(channel, data) + end + end, + }) + vim.api.nvim_create_autocmd('CmdwinEnter', { pattern = '[:>]', desc = 'Limit syntax sync to maxlines=1 in the command window', -- cgit From eef2aedff6313d85b852463c78614ace57bb6f56 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Thu, 22 Feb 2024 21:15:56 +0800 Subject: fix(defaults): remove tmux background detection passthrough (#27571) There is now a new tmux 3.4 release that queries background color from the parent terminal if background is not set in tmux, so removing the passthrough still works when background is not set in tmux, and fixes the incorrect detection when background is set in tmux. --- runtime/lua/vim/_defaults.lua | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) (limited to 'runtime/lua/vim/_defaults.lua') diff --git a/runtime/lua/vim/_defaults.lua b/runtime/lua/vim/_defaults.lua index 07850a5a47..f453264c76 100644 --- a/runtime/lua/vim/_defaults.lua +++ b/runtime/lua/vim/_defaults.lua @@ -343,17 +343,7 @@ if tty then end, }) - 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) + io.stdout:write('\027]11;?\007') timer:start(1000, 0, function() -- Delete the autocommand if no response was received -- cgit From 85cb0b0ddc59cd1b3a911e5b4a358e5404c1d0d8 Mon Sep 17 00:00:00 2001 From: Evgeni Chasnovski Date: Thu, 22 Feb 2024 21:54:21 +0200 Subject: fix(defaults): make terminal autoclose not block other events (#27581) Problem: When terminal is autocloses, it blocks other events, like `BufEnter`. Solution: Use `nested = true`. --- runtime/lua/vim/_defaults.lua | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'runtime/lua/vim/_defaults.lua') diff --git a/runtime/lua/vim/_defaults.lua b/runtime/lua/vim/_defaults.lua index f453264c76..6afb1d6b1c 100644 --- a/runtime/lua/vim/_defaults.lua +++ b/runtime/lua/vim/_defaults.lua @@ -143,14 +143,16 @@ do vim.api.nvim_create_autocmd({ 'TermClose' }, { group = nvim_terminal_augroup, + nested = true, desc = 'Automatically close terminal buffers when started with no arguments and exiting without an error', callback = function(args) - if vim.v.event.status == 0 then - local info = vim.api.nvim_get_chan_info(vim.bo[args.buf].channel) - local argv = info.argv or {} - if #argv == 1 and argv[1] == vim.o.shell then - vim.cmd({ cmd = 'bdelete', args = { args.buf }, bang = true }) - end + if vim.v.event.status ~= 0 then + return + end + local info = vim.api.nvim_get_chan_info(vim.bo[args.buf].channel) + local argv = info.argv or {} + if #argv == 1 and argv[1] == vim.o.shell then + vim.api.nvim_buf_delete(args.buf, { force = true }) end end, }) -- cgit From eb4783fb6c8c16d3a9a10e5ef36312737fc9bc40 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Fri, 23 Feb 2024 08:12:46 +0800 Subject: refactor(defaults): use getregion() for default * and # mappings --- runtime/lua/vim/_defaults.lua | 20 +------------------- 1 file changed, 1 insertion(+), 19 deletions(-) (limited to 'runtime/lua/vim/_defaults.lua') diff --git a/runtime/lua/vim/_defaults.lua b/runtime/lua/vim/_defaults.lua index 6afb1d6b1c..32534a89b4 100644 --- a/runtime/lua/vim/_defaults.lua +++ b/runtime/lua/vim/_defaults.lua @@ -4,27 +4,9 @@ do --- --- See |v_star-default| and |v_#-default| do - local function region_chunks(region) - local chunks = {} - local maxcol = vim.v.maxcol - for line, cols in vim.spairs(region) do - local endcol = cols[2] == maxcol and -1 or cols[2] - local chunk = vim.api.nvim_buf_get_text(0, line, cols[1], line, endcol, {})[1] - table.insert(chunks, chunk) - end - return chunks - end - local function _visual_search(cmd) assert(cmd == '/' or cmd == '?') - local region = vim.region( - 0, - '.', - 'v', - vim.api.nvim_get_mode().mode:sub(1, 1), - vim.o.selection == 'inclusive' - ) - local chunks = region_chunks(region) + local chunks = vim.fn.getregion('.', 'v', vim.fn.mode()) local esc_chunks = vim .iter(chunks) :map(function(v) -- cgit From a8e4ee2f2b4392e959b31c1d079166e245a401ba Mon Sep 17 00:00:00 2001 From: Gregory Anders <8965202+gpanders@users.noreply.github.com> Date: Mon, 26 Feb 2024 11:33:16 -0600 Subject: fix(defaults): validate 'channel' before responding to OSC request (#27594) Validate the channel number before responding to an OSC 10/11 request. When used with nvim_open_term, the channel number is unset (since there is no process on the other side of the PTY). --- runtime/lua/vim/_defaults.lua | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'runtime/lua/vim/_defaults.lua') diff --git a/runtime/lua/vim/_defaults.lua b/runtime/lua/vim/_defaults.lua index 32534a89b4..7cece41ed3 100644 --- a/runtime/lua/vim/_defaults.lua +++ b/runtime/lua/vim/_defaults.lua @@ -143,6 +143,10 @@ do group = nvim_terminal_augroup, desc = 'Respond to OSC foreground/background color requests', callback = function(args) + local channel = vim.bo[args.buf].channel + if channel == 0 then + return + end local fg_request = args.data == '\027]10;?' local bg_request = args.data == '\027]11;?' if fg_request or bg_request then @@ -157,7 +161,6 @@ do end local command = fg_request and 10 or 11 local data = string.format('\027]%d;rgb:%04x/%04x/%04x\007', command, red, green, blue) - local channel = vim.bo[args.buf].channel vim.api.nvim_chan_send(channel, data) end end, -- cgit From ce7c51a1a3b2b38cdba730aeb53840d0ace32173 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Thu, 29 Feb 2024 07:19:26 +0800 Subject: vim-patch:9.1.0142: getregion() can be improved (#27662) Problem: getregion() can be improved (after v9.1.120) Solution: change getregion() implementation to use pos as lists and one optional {opt} dictionary (Shougo Matsushita) Note: The following is a breaking change! Currently, the getregion() function (included as of patch v9.1.120) takes 3 arguments: the first 2 arguments are strings, describing a position, arg3 is the type string. However, that is slightly inflexible, there is no way to specify additional arguments. So let's instead change the function signature to: getregion(pos1, pos2 [, {Dict}]) where both pos1 and pos2 are lists. This is slightly cleaner, and gives us the flexibility to specify additional arguments as key/value pairs to the optional Dict arg. Now it supports the "type" key to specify the selection type (characterwise, blockwise or linewise) and now in addition one can also define the selection type, independently of what the 'selection' option actually is. Technically, this is a breaking change, but since the getregion() Vimscript function is still quite new, this should be fine. closes: vim/vim#14090 https://github.com/vim/vim/commit/19b718828d8d5fab52d94c6cdba694641879ab38 Co-authored-by: Shougo Matsushita --- runtime/lua/vim/_defaults.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'runtime/lua/vim/_defaults.lua') diff --git a/runtime/lua/vim/_defaults.lua b/runtime/lua/vim/_defaults.lua index 7cece41ed3..c8131f8eb4 100644 --- a/runtime/lua/vim/_defaults.lua +++ b/runtime/lua/vim/_defaults.lua @@ -6,7 +6,8 @@ do do local function _visual_search(cmd) assert(cmd == '/' or cmd == '?') - local chunks = vim.fn.getregion('.', 'v', vim.fn.mode()) + local chunks = + vim.fn.getregion(vim.fn.getpos('.'), vim.fn.getpos('v'), { type = vim.fn.mode() }) local esc_chunks = vim .iter(chunks) :map(function(v) -- cgit From f9e7c4c9c4fb89e6114a37387f3738d98cb4ba6a Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Thu, 29 Feb 2024 08:21:13 +0800 Subject: refactor(defaults): use getregion() for Visual mode gx (#27663) Also make it work better on a multiline selection. --- runtime/lua/vim/_defaults.lua | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) (limited to 'runtime/lua/vim/_defaults.lua') diff --git a/runtime/lua/vim/_defaults.lua b/runtime/lua/vim/_defaults.lua index c8131f8eb4..91baee1a1e 100644 --- a/runtime/lua/vim/_defaults.lua +++ b/runtime/lua/vim/_defaults.lua @@ -67,15 +67,6 @@ do ) --- Map |gx| to call |vim.ui.open| on the identifier under the cursor do - -- TODO: use vim.region() when it lands... #13896 #16843 - local function get_visual_selection() - local save_a = vim.fn.getreginfo('a') - vim.cmd([[norm! "ay]]) - local selection = vim.fn.getreg('a', 1) - vim.fn.setreg('a', save_a) - return selection - end - local function do_open(uri) local _, err = vim.ui.open(uri) if err then @@ -89,7 +80,10 @@ do do_open(vim.fn.expand('')) end, { desc = gx_desc }) vim.keymap.set({ 'x' }, 'gx', function() - do_open(get_visual_selection()) + local lines = + vim.fn.getregion(vim.fn.getpos('.'), vim.fn.getpos('v'), { type = vim.fn.mode() }) + -- Trim whitespace on each line and concatenate. + do_open(table.concat(vim.iter(lines):map(vim.trim):totable())) end, { desc = gx_desc }) end end -- cgit