fix: tweaked so it actually works on my Arch netbook
This commit is contained in:
1 file changed
+58
-51
@@ -1,18 +1,17 @@
|
|||||||
-- lua/universal-clipboard/init.lua
|
-- lua/universal-clipboard/init.lua
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
-- 1) Default options
|
-- Our default options
|
||||||
local default_opts = {
|
local default_opts = {
|
||||||
verbose = false,
|
verbose = false,
|
||||||
}
|
}
|
||||||
|
|
||||||
-- 2) Check if Neovim has '+clipboard'
|
-- Pick copy/paste commands for Wayland or X11 if '+clipboard' is missing
|
||||||
local function has_builtin_clipboard()
|
local function pick_copy_paste_commands(opts)
|
||||||
return vim.fn.has("clipboard") == 1
|
if opts.verbose then
|
||||||
end
|
vim.notify("[universal-clipboard.nvim] looking for wl-copy/xclip/xsel ...")
|
||||||
|
end
|
||||||
|
|
||||||
-- 3) Pick copy/paste commands for Wayland or X11 if '+clipboard' is missing
|
|
||||||
local function pick_copy_paste_commands()
|
|
||||||
-- Check Wayland + wl-copy/wl-paste
|
-- Check Wayland + wl-copy/wl-paste
|
||||||
local wayland_display = os.getenv("WAYLAND_DISPLAY")
|
local wayland_display = os.getenv("WAYLAND_DISPLAY")
|
||||||
local wayland_runtime = os.getenv("XDG_RUNTIME_DIR")
|
local wayland_runtime = os.getenv("XDG_RUNTIME_DIR")
|
||||||
@@ -23,14 +22,20 @@ local function pick_copy_paste_commands()
|
|||||||
local have_wayland = wayland_display and wayland_display ~= "" and (vim.fn.isdirectory(wayland_socket_path) == 1)
|
local have_wayland = wayland_display and wayland_display ~= "" and (vim.fn.isdirectory(wayland_socket_path) == 1)
|
||||||
|
|
||||||
if has_wlcopy and has_wlpaste and have_wayland then
|
if has_wlcopy and has_wlpaste and have_wayland then
|
||||||
|
if opts.verbose then
|
||||||
|
vim.notify("[universal-clipboard.nvim] found wl-copy/wl-paste + Wayland", vim.log.levels.INFO)
|
||||||
|
end
|
||||||
return {
|
return {
|
||||||
copy = "wl-copy",
|
copy = "wl-copy",
|
||||||
paste = "wl-paste --no-newline",
|
paste = "wl-paste --no-newline",
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Otherwise, try xclip
|
-- Next, try xclip
|
||||||
if vim.fn.executable("xclip") == 1 then
|
if vim.fn.executable("xclip") == 1 then
|
||||||
|
if opts.verbose then
|
||||||
|
vim.notify("[universal-clipboard.nvim] found xclip", vim.log.levels.INFO)
|
||||||
|
end
|
||||||
return {
|
return {
|
||||||
copy = "xclip -selection clipboard",
|
copy = "xclip -selection clipboard",
|
||||||
paste = "xclip -selection clipboard -o",
|
paste = "xclip -selection clipboard -o",
|
||||||
@@ -39,6 +44,9 @@ local function pick_copy_paste_commands()
|
|||||||
|
|
||||||
-- Otherwise, try xsel
|
-- Otherwise, try xsel
|
||||||
if vim.fn.executable("xsel") == 1 then
|
if vim.fn.executable("xsel") == 1 then
|
||||||
|
if opts.verbose then
|
||||||
|
vim.notify("[universal-clipboard.nvim] found xsel", vim.log.levels.INFO)
|
||||||
|
end
|
||||||
return {
|
return {
|
||||||
copy = "xsel --clipboard --input",
|
copy = "xsel --clipboard --input",
|
||||||
paste = "xsel --clipboard --output",
|
paste = "xsel --clipboard --output",
|
||||||
@@ -46,73 +54,72 @@ local function pick_copy_paste_commands()
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- No suitable tool found
|
-- No suitable tool found
|
||||||
|
if opts.verbose then
|
||||||
|
vim.notify("[universal-clipboard.nvim] none found.")
|
||||||
|
end
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
|
|
||||||
-- 4) Actually configure Neovim's clipboard usage
|
-- 4) Actually configure Neovim's clipboard usage
|
||||||
local function configure_clipboard()
|
local function configure_clipboard(opts)
|
||||||
if has_builtin_clipboard() then
|
-- Unify normal yanks/pastes to systepm clipboard
|
||||||
-- Neovim has native +clipboard
|
vim.opt.clipboard = "unnamedplus"
|
||||||
vim.opt.clipboard = "unnamedplus"
|
|
||||||
return "builtin"
|
-- Search for custom clipboard provider
|
||||||
|
local commands = pick_copy_paste_commands(opts)
|
||||||
|
if commands then
|
||||||
|
vim.g.clipboard = {
|
||||||
|
name = "UniversalClipboard",
|
||||||
|
copy = {
|
||||||
|
["+"] = commands.copy,
|
||||||
|
["*"] = commands.copy,
|
||||||
|
},
|
||||||
|
paste = {
|
||||||
|
["+"] = commands.paste,
|
||||||
|
["*"] = commands.paste,
|
||||||
|
},
|
||||||
|
cache_enabled = 0,
|
||||||
|
}
|
||||||
|
return "wl-copy/xclip/xsel"
|
||||||
else
|
else
|
||||||
-- Fall back to custom clipboard provider
|
-- No tool found
|
||||||
local commands = pick_copy_paste_commands()
|
vim.notify(
|
||||||
if commands then
|
"[universal-clipboard.nvim] No suitable clipboard tool found (wl-copy, xclip, xsel).",
|
||||||
vim.g.clipboard = {
|
vim.log.levels.WARN
|
||||||
name = "UniversalClipboard",
|
)
|
||||||
copy = {
|
return "system"
|
||||||
["+"] = commands.copy,
|
|
||||||
["*"] = commands.copy,
|
|
||||||
},
|
|
||||||
paste = {
|
|
||||||
["+"] = commands.paste,
|
|
||||||
["*"] = commands.paste,
|
|
||||||
},
|
|
||||||
cache_enabled = 0,
|
|
||||||
}
|
|
||||||
-- Also unify normal yanks/pastes to system clipboard
|
|
||||||
vim.opt.clipboard = "unnamedplus"
|
|
||||||
return "fallback"
|
|
||||||
else
|
|
||||||
-- No tool found
|
|
||||||
vim.notify(
|
|
||||||
"[universal-clipboard.nvim] No suitable clipboard tool found (wl-copy, xclip, xsel).",
|
|
||||||
vim.log.levels.WARN
|
|
||||||
)
|
|
||||||
return "none"
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- 5) Setup function (entry point)
|
-- Setup function (entry point)
|
||||||
function M.setup(user_opts)
|
function M.setup(user_opts)
|
||||||
-- Merge defaults with user_opts
|
-- Merge defaults with user_opts
|
||||||
local opts = vim.tbl_deep_extend("force", default_opts, user_opts or {})
|
local opts = vim.tbl_deep_extend("force", default_opts, user_opts or {})
|
||||||
|
|
||||||
local mode = configure_clipboard()
|
-- Set the clipboard up
|
||||||
|
local mode = configure_clipboard(opts)
|
||||||
if opts.verbose then
|
if opts.verbose then
|
||||||
vim.notify(("[universal-clipboard.nvim] Clipboard mode: %s"):format(mode), vim.log.levels.INFO)
|
vim.notify(("[universal-clipboard.nvim] Clipboard mode: %s"):format(mode), vim.log.levels.INFO)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Create user commands for debugging / re-init
|
-- Create user commands for debugging / re-init
|
||||||
vim.api.nvim_create_user_command("UniversalClipboardCheck", function()
|
vim.api.nvim_create_user_command("UniversalClipboardCheck", function()
|
||||||
if has_builtin_clipboard() then
|
print("=== UniversalClipboardCheck ===")
|
||||||
print("Neovim has +clipboard. Current setting:")
|
|
||||||
print(vim.inspect(vim.opt.clipboard:get()))
|
local opt_clipboard = vim.opt.clipboard:get()
|
||||||
|
print("vim.opt.clipboard = ", vim.inspect(opt_clipboard))
|
||||||
|
|
||||||
|
if vim.g.clipboard then
|
||||||
|
print("vim.g.clipboard = ", vim.inspect(vim.g.clipboard))
|
||||||
else
|
else
|
||||||
print("Neovim lacks +clipboard. Using fallback if available.")
|
print("vim.g.clipboard is not set.")
|
||||||
if vim.g.clipboard then
|
|
||||||
print("Fallback config: " .. vim.inspect(vim.g.clipboard))
|
|
||||||
else
|
|
||||||
print("No fallback is set (no suitable tool found?).")
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end, {})
|
end, {})
|
||||||
|
|
||||||
vim.api.nvim_create_user_command("UniversalClipboardReinit", function()
|
vim.api.nvim_create_user_command("UniversalClipboardReinit", function()
|
||||||
local new_mode = configure_clipboard()
|
local new_mode = configure_clipboard({ verbose = true })
|
||||||
print("Reinitialized clipboard mode: " .. new_mode)
|
print("Reinitialized clipboard mode: " .. new_mode)
|
||||||
|
vim.cmd("UniversalClipboardCheck")
|
||||||
end, {})
|
end, {})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user