From db57df04b6af03ad9dd0447ffc8e881c97a39732 Mon Sep 17 00:00:00 2001 From: Gregory Anders <8965202+gpanders@users.noreply.github.com> Date: Thu, 16 Nov 2023 12:21:24 -0600 Subject: feat(clipboard): enable OSC 52 clipboard provider by default (#26064) Use the XTGETTCAP sequence to determine if the host terminal supports the OSC 52 sequence and, if it does, enable the OSC 52 clipboard provider by default. This is only done automatically when all of the following are true: 1. Nvim is running in the TUI 2. 'clipboard' is not set to unnamed or unnamedplus 3. g:clipboard is unset 4. Nvim is running in an SSH connection ($SSH_TTY is set) 5. Nvim is not running inside tmux ($TMUX is unset) --- runtime/lua/vim/termcap.lua | 60 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 runtime/lua/vim/termcap.lua (limited to 'runtime/lua/vim/termcap.lua') diff --git a/runtime/lua/vim/termcap.lua b/runtime/lua/vim/termcap.lua new file mode 100644 index 0000000000..0eefc5eee4 --- /dev/null +++ b/runtime/lua/vim/termcap.lua @@ -0,0 +1,60 @@ +local M = {} + +--- Query the host terminal emulator for terminfo capabilities. +--- +--- This function sends the XTGETTCAP DCS sequence to the host terminal emulator asking the terminal +--- to send us its terminal capabilities. These are strings that are normally taken from a terminfo +--- file, however an up to date terminfo database is not always available (particularly on remote +--- machines), and many terminals continue to misidentify themselves or do not provide their own +--- terminfo file, making the terminfo database unreliable. +--- +--- Querying the terminal guarantees that we get a truthful answer, but only if the host terminal +--- emulator supports the XTGETTCAP sequence. +--- +--- @param caps string|table A terminal capability or list of capabilities to query +--- @param cb function(cap:string, seq:string) Function to call when a response is received +function M.query(caps, cb) + vim.validate({ + caps = { caps, { 'string', 'table' } }, + cb = { cb, 'f' }, + }) + + if type(caps) ~= 'table' then + caps = { caps } + end + + local count = #caps + + vim.api.nvim_create_autocmd('TermResponse', { + callback = function(args) + local resp = args.data ---@type string + local k, v = resp:match('^\027P1%+r(%x+)=(%x+)$') + if k and v then + local cap = vim.text.hexdecode(k) + local seq = + vim.text.hexdecode(v):gsub('\\E', '\027'):gsub('%%p%d', ''):gsub('\\(%d+)', string.char) + + -- TODO: When libtermkey is patched to accept BEL as an OSC terminator, this workaround can + -- be removed + seq = seq:gsub('\007$', '\027\\') + + cb(cap, seq) + + count = count - 1 + if count == 0 then + return true + end + end + end, + }) + + local encoded = {} ---@type string[] + for i = 1, #caps do + encoded[i] = vim.text.hexencode(caps[i]) + end + + local query = string.format('\027P+q%s\027\\', table.concat(encoded, ';')) + io.stdout:write(query) +end + +return M -- cgit From b7831c7f996b769544d2bdc536ef601eb46b4918 Mon Sep 17 00:00:00 2001 From: Gregory Anders <8965202+gpanders@users.noreply.github.com> Date: Tue, 28 Nov 2023 16:34:18 -0600 Subject: fix(termcap): use tmux passthrough sequence when running in tmux (#26281) tmux intercepts and ignores XTGETTCAP so wrap the query in the tmux passthrough sequence to make sure the query arrives at the "host" terminal. Users must still set the 'allow-passthrough' option in their tmux.conf. --- runtime/lua/vim/termcap.lua | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'runtime/lua/vim/termcap.lua') diff --git a/runtime/lua/vim/termcap.lua b/runtime/lua/vim/termcap.lua index 0eefc5eee4..2117f287cb 100644 --- a/runtime/lua/vim/termcap.lua +++ b/runtime/lua/vim/termcap.lua @@ -34,10 +34,6 @@ function M.query(caps, cb) local seq = vim.text.hexdecode(v):gsub('\\E', '\027'):gsub('%%p%d', ''):gsub('\\(%d+)', string.char) - -- TODO: When libtermkey is patched to accept BEL as an OSC terminator, this workaround can - -- be removed - seq = seq:gsub('\007$', '\027\\') - cb(cap, seq) count = count - 1 @@ -54,6 +50,12 @@ function M.query(caps, cb) end local query = string.format('\027P+q%s\027\\', table.concat(encoded, ';')) + + -- If running in tmux, wrap with the passthrough sequence + if os.getenv('TMUX') then + query = string.format('\027Ptmux;\027%s\027\\', query) + end + io.stdout:write(query) end -- cgit From 400b7842a98355bc3fcf2ecfaeda8ccf259d917a Mon Sep 17 00:00:00 2001 From: Gregory Anders <8965202+gpanders@users.noreply.github.com> Date: Wed, 29 Nov 2023 09:49:44 -0600 Subject: fix(termcap): escape escapes in passthrough sequence (#26301) When using the tmux passthrough sequence any escape characters in the inner sequence must be escaped by adding another escape character. --- runtime/lua/vim/termcap.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'runtime/lua/vim/termcap.lua') diff --git a/runtime/lua/vim/termcap.lua b/runtime/lua/vim/termcap.lua index 2117f287cb..862cc52149 100644 --- a/runtime/lua/vim/termcap.lua +++ b/runtime/lua/vim/termcap.lua @@ -53,7 +53,7 @@ function M.query(caps, cb) -- If running in tmux, wrap with the passthrough sequence if os.getenv('TMUX') then - query = string.format('\027Ptmux;\027%s\027\\', query) + query = string.format('\027Ptmux;%s\027\\', query:gsub('\027', '\027\027')) end io.stdout:write(query) -- cgit