Keep remote clipboard paste fast and cover provider transports

This commit is contained in:
David Heinemeier Hansson committed 2026-09-13 17:46:10 +02:00
1 parent db5f3791c5
commit c095aa4ccc
3 files changed
+177 -19

No files matched your search

@@ -1,9 +1,8 @@
-- Clipboard for sessions whose yanks may need to reach another machine:
-- every copy is emitted as OSC 52 (inside tmux this becomes a tmux buffer,
-- rebroadcast to every attached client, local or SSH). Paste prefers the
-- local Wayland clipboard when one is available, so content copied in other
-- apps remains pasteable; without a display, paste is an OSC 52 query that
-- tmux (or the terminal) answers.
-- Copies reach the local Wayland clipboard and attached terminals. Reads use
-- Wayland or tmux's buffer, falling back to this Neovim's last copy. Never query
-- the terminal with OSC 52: support for writes does not imply permission to read,
-- and an unanswered query blocks ordinary paste for ten seconds. To insert new
-- text from the host when no readable clipboard exists, use terminal paste.
local M = {}
local function proc_lines(pid, file)
@@ -54,10 +53,15 @@ function M.setup()
and vim.fn.executable("wl-copy") == 1
and vim.fn.executable("wl-paste") == 1
local has_tmux = in_tmux and vim.fn.executable("tmux") == 1
local last_copy = {}
local function copy(register)
local emit = osc52.copy(register)
return function(lines)
return function(lines, regtype)
last_copy[register] = { vim.deepcopy(lines), regtype }
if has_wayland then
local cmd = { "wl-copy", "--sensitive", "--type", "text/plain" }
if register == "*" then
@@ -67,36 +71,60 @@ function M.setup()
end
if vim.g.omarchy_remote_clipboard_osc52 ~= false then
if has_tmux and register == "+" then
-- Let tmux emit OSC 52, avoiding its input parser's payload size limit.
vim.fn.system({ "tmux", "load-buffer", "-w", "-" }, lines)
if vim.v.shell_error == 0 then
return
end
end
emit(lines)
end
end
end
local function paste(register)
if not has_wayland then
return osc52.paste(register)
end
return function()
local cmd = { "wl-paste", "--no-newline" }
if register == "*" then
cmd[#cmd + 1] = "--primary"
local cmd
-- tmux's buffer is shared between Neovim instances in the session. Over
-- SSH, prefer it to the remote machine's unrelated graphical clipboard.
if has_tmux and register == "+" and (in_ssh or not has_wayland)
and vim.g.omarchy_remote_clipboard_osc52 ~= false then
cmd = { "tmux", "save-buffer", "-" }
elseif has_wayland then
cmd = { "wl-paste", "--no-newline" }
if register == "*" then
cmd[#cmd + 1] = "--primary"
end
end
local lines = vim.fn.systemlist(cmd, "", 1)
return vim.v.shell_error == 0 and lines or {}
if cmd then
local lines = vim.fn.systemlist(cmd, "", 1)
if vim.v.shell_error == 0 then
-- An empty clipboard is valid; only failed reads use the fallback.
if last_copy[register] and vim.deep_equal(lines, last_copy[register][1]) then
return vim.deepcopy(last_copy[register])
end
return lines
end
end
return vim.deepcopy(last_copy[register] or { {}, "v" })
end
end
-- LazyVim disables clipboard syncing over SSH; our provider supports it.
vim.opt.clipboard = "unnamedplus"
vim.g.clipboard = {
name = "OmarchyRemoteClipboard",
copy = { ["+"] = copy("+"), ["*"] = copy("*") },
paste = { ["+"] = paste("+"), ["*"] = paste("*") },
cache_enabled = 0,
}
-- LazyVim clears this over SSH. Users can opt out before setup or override
-- the option afterward in config/options.lua.
if vim.g.omarchy_remote_clipboard_sync ~= false then
vim.opt.clipboard:append("unnamedplus")
end
end
return M