From b73a829837bbc05840ae00cbe514fb1786695614 Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Tue, 14 Nov 2023 05:15:45 -0800 Subject: refactor: vim.ui.clipboard #26040 Problem: Platform-specific UI providers should live in `vim.ui.*`. #24164 Solution: - Move `vim.clipboard.osc52` module to `vim.ui.clipboard.osc52`. - TODO: move all of `clipboard.vim` to `vim.ui.clipboard`. ref #25872 --- runtime/lua/vim/ui/clipboard/osc52.lua | 60 ++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 runtime/lua/vim/ui/clipboard/osc52.lua (limited to 'runtime/lua/vim/ui/clipboard/osc52.lua') diff --git a/runtime/lua/vim/ui/clipboard/osc52.lua b/runtime/lua/vim/ui/clipboard/osc52.lua new file mode 100644 index 0000000000..035a6abb86 --- /dev/null +++ b/runtime/lua/vim/ui/clipboard/osc52.lua @@ -0,0 +1,60 @@ +local M = {} + +function M.copy(lines) + local s = table.concat(lines, '\n') + io.stdout:write(string.format('\027]52;;%s\027\\', vim.base64.encode(s))) +end + +function M.paste() + local contents = nil + local id = vim.api.nvim_create_autocmd('TermResponse', { + callback = function(args) + local resp = args.data ---@type string + local encoded = resp:match('\027%]52;%w?;([A-Za-z0-9+/=]*)') + if encoded then + contents = vim.base64.decode(encoded) + return true + end + end, + }) + + io.stdout:write('\027]52;;?\027\\') + + local ok, res + + -- Wait 1s first for terminals that respond quickly + ok, res = vim.wait(1000, function() + return contents ~= nil + end) + + if res == -1 then + -- If no response was received after 1s, print a message and keep waiting + vim.api.nvim_echo( + { { 'Waiting for OSC 52 response from the terminal. Press Ctrl-C to interrupt...' } }, + false, + {} + ) + ok, res = vim.wait(9000, function() + return contents ~= nil + end) + end + + if not ok then + vim.api.nvim_del_autocmd(id) + if res == -1 then + vim.notify( + 'Timed out waiting for a clipboard response from the terminal', + vim.log.levels.WARN + ) + elseif res == -2 then + -- Clear message area + vim.api.nvim_echo({ { '' } }, false, {}) + end + return 0 + end + + -- If we get here, contents should be non-nil + return vim.split(assert(contents), '\n') +end + +return M -- cgit 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/ui/clipboard/osc52.lua | 107 +++++++++++++++++++-------------- 1 file changed, 61 insertions(+), 46 deletions(-) (limited to 'runtime/lua/vim/ui/clipboard/osc52.lua') diff --git a/runtime/lua/vim/ui/clipboard/osc52.lua b/runtime/lua/vim/ui/clipboard/osc52.lua index 035a6abb86..f1d454010f 100644 --- a/runtime/lua/vim/ui/clipboard/osc52.lua +++ b/runtime/lua/vim/ui/clipboard/osc52.lua @@ -1,60 +1,75 @@ local M = {} -function M.copy(lines) - local s = table.concat(lines, '\n') - io.stdout:write(string.format('\027]52;;%s\027\\', vim.base64.encode(s))) +--- Return the OSC 52 escape sequence +--- +--- @param clipboard string The clipboard to read from or write to +--- @param contents string The Base64 encoded contents to write to the clipboard, or '?' to read +--- from the clipboard +local function osc52(clipboard, contents) + return string.format('\027]52;%s;%s\027\\', clipboard, contents) end -function M.paste() - local contents = nil - local id = vim.api.nvim_create_autocmd('TermResponse', { - callback = function(args) - local resp = args.data ---@type string - local encoded = resp:match('\027%]52;%w?;([A-Za-z0-9+/=]*)') - if encoded then - contents = vim.base64.decode(encoded) - return true - end - end, - }) - - io.stdout:write('\027]52;;?\027\\') - - local ok, res - - -- Wait 1s first for terminals that respond quickly - ok, res = vim.wait(1000, function() - return contents ~= nil - end) - - if res == -1 then - -- If no response was received after 1s, print a message and keep waiting - vim.api.nvim_echo( - { { 'Waiting for OSC 52 response from the terminal. Press Ctrl-C to interrupt...' } }, - false, - {} - ) - ok, res = vim.wait(9000, function() +function M.copy(reg) + local clipboard = reg == '+' and 'c' or 's' + return function(lines) + local s = table.concat(lines, '\n') + io.stdout:write(osc52(clipboard, vim.base64.encode(s))) + end +end + +function M.paste(reg) + local clipboard = reg == '+' and 'c' or 's' + return function() + local contents = nil + local id = vim.api.nvim_create_autocmd('TermResponse', { + callback = function(args) + local resp = args.data ---@type string + local encoded = resp:match('\027%]52;%w?;([A-Za-z0-9+/=]*)') + if encoded then + contents = vim.base64.decode(encoded) + return true + end + end, + }) + + io.stdout:write(osc52(clipboard, '?')) + + local ok, res + + -- Wait 1s first for terminals that respond quickly + ok, res = vim.wait(1000, function() return contents ~= nil end) - end - if not ok then - vim.api.nvim_del_autocmd(id) if res == -1 then - vim.notify( - 'Timed out waiting for a clipboard response from the terminal', - vim.log.levels.WARN + -- If no response was received after 1s, print a message and keep waiting + vim.api.nvim_echo( + { { 'Waiting for OSC 52 response from the terminal. Press Ctrl-C to interrupt...' } }, + false, + {} ) - elseif res == -2 then - -- Clear message area - vim.api.nvim_echo({ { '' } }, false, {}) + ok, res = vim.wait(9000, function() + return contents ~= nil + end) + end + + if not ok then + vim.api.nvim_del_autocmd(id) + if res == -1 then + vim.notify( + 'Timed out waiting for a clipboard response from the terminal', + vim.log.levels.WARN + ) + elseif res == -2 then + -- Clear message area + vim.api.nvim_echo({ { '' } }, false, {}) + end + return 0 end - return 0 - end - -- If we get here, contents should be non-nil - return vim.split(assert(contents), '\n') + -- If we get here, contents should be non-nil + return vim.split(assert(contents), '\n') + end end return M -- cgit From 86c2213b5e451b0c80c2d7adc356c7cebe4bb7f8 Mon Sep 17 00:00:00 2001 From: Tomasz N Date: Fri, 17 Nov 2023 02:52:22 +0100 Subject: fix(osc52): use `p` for primary selection instead of `s` (#26076) Co-authored-by: Gregory Anders --- runtime/lua/vim/ui/clipboard/osc52.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'runtime/lua/vim/ui/clipboard/osc52.lua') diff --git a/runtime/lua/vim/ui/clipboard/osc52.lua b/runtime/lua/vim/ui/clipboard/osc52.lua index f1d454010f..6483f0387d 100644 --- a/runtime/lua/vim/ui/clipboard/osc52.lua +++ b/runtime/lua/vim/ui/clipboard/osc52.lua @@ -10,7 +10,7 @@ local function osc52(clipboard, contents) end function M.copy(reg) - local clipboard = reg == '+' and 'c' or 's' + local clipboard = reg == '+' and 'c' or 'p' return function(lines) local s = table.concat(lines, '\n') io.stdout:write(osc52(clipboard, vim.base64.encode(s))) @@ -18,7 +18,7 @@ function M.copy(reg) end function M.paste(reg) - local clipboard = reg == '+' and 'c' or 's' + local clipboard = reg == '+' and 'c' or 'p' return function() local contents = nil local id = vim.api.nvim_create_autocmd('TermResponse', { -- cgit