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/plugin/osc52.lua | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 runtime/plugin/osc52.lua (limited to 'runtime/plugin/osc52.lua') diff --git a/runtime/plugin/osc52.lua b/runtime/plugin/osc52.lua new file mode 100644 index 0000000000..78b21863ad --- /dev/null +++ b/runtime/plugin/osc52.lua @@ -0,0 +1,37 @@ +local tty = vim.iter(vim.api.nvim_list_uis()):any(function(ui) + return ui.chan == 1 and ui.stdout_tty +end) + +if + not tty + or vim.g.clipboard ~= nil + or vim.o.clipboard ~= '' + or not os.getenv('SSH_TTY') + or os.getenv('TMUX') +then + return +end + +require('vim.termcap').query('Ms', function(cap, seq) + assert(cap == 'Ms') + + -- If the terminal reports a sequence other than OSC 52 for the Ms capability + -- then ignore it. We only support OSC 52 (for now) + if not seq:match('^\027%]52') then + return + end + + local osc52 = require('vim.ui.clipboard.osc52') + + vim.g.clipboard = { + name = 'OSC 52', + copy = { + ['+'] = osc52.copy('+'), + ['*'] = osc52.copy('*'), + }, + paste = { + ['+'] = osc52.paste('+'), + ['*'] = osc52.paste('*'), + }, + } +end) -- cgit From 5d75d9aef2b69036328501820df770b29d57a308 Mon Sep 17 00:00:00 2001 From: Gregory Anders <8965202+gpanders@users.noreply.github.com> Date: Thu, 16 Nov 2023 13:56:05 -0600 Subject: fix(osc52): enable OSC 52 by default in tmux sessions (#26072) tmux has a set-clipboard option which, when set to 'on', allows applications to set the system clipboard using the usual OSC 52 escape sequence. --- runtime/plugin/osc52.lua | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) (limited to 'runtime/plugin/osc52.lua') diff --git a/runtime/plugin/osc52.lua b/runtime/plugin/osc52.lua index 78b21863ad..374b70066f 100644 --- a/runtime/plugin/osc52.lua +++ b/runtime/plugin/osc52.lua @@ -2,19 +2,18 @@ local tty = vim.iter(vim.api.nvim_list_uis()):any(function(ui) return ui.chan == 1 and ui.stdout_tty end) -if - not tty - or vim.g.clipboard ~= nil - or vim.o.clipboard ~= '' - or not os.getenv('SSH_TTY') - or os.getenv('TMUX') -then +if not tty or vim.g.clipboard ~= nil or vim.o.clipboard ~= '' or not os.getenv('SSH_TTY') then return end require('vim.termcap').query('Ms', function(cap, seq) assert(cap == 'Ms') + -- Check 'clipboard' and g:clipboard again to avoid a race condition + if vim.o.clipboard ~= '' or vim.g.clipboard ~= nil then + return + end + -- If the terminal reports a sequence other than OSC 52 for the Ms capability -- then ignore it. We only support OSC 52 (for now) if not seq:match('^\027%]52') then -- cgit