Simplify osc52 code.

This commit is contained in:
Gabriel Ebner 2021-10-31 11:38:30 +01:00
parent f5ec5806ed
commit f59879acbe
2 changed files with 11 additions and 25 deletions

View File

@ -5,31 +5,19 @@ local M = {}
local base64 = {}
local max_osc52_sequence = 100000
function M.get_OSC52(str, tgt)
local function osc52_copy(str, tgt)
return "\x1b]52;" .. tgt .. ";" .. base64.encode(str) .. "\x07"
end
-- This is for `tmux` sessions which filters OSC 52 locally.
function M.get_OSC52_tmux(str, tgt)
return "\x1bPtmux;\x1b\x1b]52;" .. tgt .. ";" .. base64.encode(str) .. "\x07\x1b\\"
local function tmux_wrap(cmd)
return "\x1bPtmux;\x1b" .. cmd .. "\x1b\\"
end
-- base64s the entire source, wraps it in a single OSC52, and then
-- breaks the result in small chunks which are each wrapped in a DCS sequence.
--
-- This is appropriate when running on `screen`. Screen doesn't support OSC 52,
-- but will pass the contents of a DCS sequence to the outer terminal unmolested.
-- It imposes a small max length to DCS sequences, so we send in chunks.
function M.get_OSC52_DCS(str, tgt)
local b64 = base64.encode(str)
local function screen_wrap(cmd)
-- Insert <end-dcs><start-dcs> pair every 76 characters.
-- TODO: insert "\x1b\\\x1bP" every 76 characters
-- Now wrap the whole thing in <start-dcs><start-osc52>...<end-osc52><end-dcs>.
b64 = "\x1bP\x1b]52;" .. tgt .. ";" .. b64 .. "\x07\x1b\x5c"
return b64
return "\x1bP" .. cmd .. "\x1b\x5c"
end
M.terminal = vim.api.nvim_eval'$TERM'
@ -39,13 +27,11 @@ function M.copy(str, tgt)
if #str > max_osc52_sequence then error'string to long to copy via OSC52' end
local osc52
local osc52 = osc52_copy(str, tgt)
if M.terminal:match'^tmux' ~= nil then
osc52 = M.get_OSC52_tmux(str, tgt)
osc52 = tmux_wrap(osc52)
elseif M.terminal:match'^screen' ~= nil then
osc52 = M.get_OSC52_DCS(str, tgt)
else
osc52 = M.get_OSC52(str, tgt)
osc52 = screen_wrap(osc52)
end
io.stdout:write(osc52)
@ -64,8 +50,8 @@ function M.enable()
\ '*': { lines, regtype -> v:lua.osc52_clipboard_handler(lines, regtype, '*') },
\ },
\ 'paste': {
\ '+': { -> '' },
\ '*': { -> '' },
\ '+': { -> [] },
\ '*': { -> [] },
\ },
\}
]]

2
vimrc
View File

@ -152,7 +152,7 @@ set backupcopy=yes
se inccommand=nosplit
set mouse=a
set mouse=nv
set title
se scl=yes:1 " say no to wobbly side column