From 3ef86c924b750180a8b6ad01382236134fe927c9 Mon Sep 17 00:00:00 2001 From: andy941 <49410235+andy941@users.noreply.github.com> Date: Wed, 17 Apr 2024 08:16:16 +0100 Subject: [PATCH] feat: Add option to choose clipboard program (#322) * Refactor and add option to choose clipboard program * Warn user early when clipboard program specified is not executable closes #320 --- doc/telekasten.txt | 2 ++ lua/telekasten.lua | 90 ++++++++++++++++++++++++++++------------------ 2 files changed, 57 insertions(+), 35 deletions(-) diff --git a/doc/telekasten.txt b/doc/telekasten.txt index a6017eb..019a798 100644 --- a/doc/telekasten.txt +++ b/doc/telekasten.txt @@ -181,6 +181,8 @@ telekasten.setup({opts}) } }, + -- Specify a clipboard program to use + clipboard_program = "", -- xsel, xclip, wl-paste, osascript } < diff --git a/lua/telekasten.lua b/lua/telekasten.lua index 3a07f47..1daa3fb 100644 --- a/lua/telekasten.lua +++ b/lua/telekasten.lua @@ -163,6 +163,9 @@ local function defaultConfig(home) follow_url_fallback = nil, -- Enable creation new notes with Ctrl-n when finding notes enable_create_new = true, + + -- Specify a clipboard program to use + clipboard_program = "", -- xsel, xclip, wl-paste, osascript } M.Cfg = cfg M.note_type_templates = { @@ -371,45 +374,62 @@ local function imgFromClipboard() return end + local paste_command = {} + paste_command["xsel"] = function(dir, filename) + local _image_path = vim.fn.system("xsel --clipboard --output") + local image_path = _image_path:gsub("file://", "") + if + vim.fn + .system("file --mime-type -b " .. image_path) + :gsub("%s+", "") + == "image/png" + then + return "cp " .. image_path .. " " .. dir .. "/" .. filename + else + return "" + end + end + paste_command["xclip"] = function(dir, filename) + return "xclip -selection clipboard -t image/png -o > " + .. dir + .. "/" + .. filename + end + paste_command["osascript"] = function(dir, filename) + return string.format( + 'osascript -e "tell application \\"System Events\\" to write (the clipboard as «class PNGf») to ' + .. '(make new file at folder \\"%s\\" with properties {name:\\"%s\\"})"', + dir, + filename + ) + end + + paste_command["wl-paste"] = function(dir, filename) + return "wl-paste -n -t image/png > " .. dir .. "/" .. filename + end + local get_paste_command - if vim.fn.executable("xsel") == 1 then - get_paste_command = function(dir, filename) - local _image_path = vim.fn.system("xsel --clipboard --output") - local image_path = _image_path:gsub("file://", "") - if - vim.fn - .system("file --mime-type -b " .. image_path) - :gsub("%s+", "") - == "image/png" - then - return "cp " .. image_path .. " " .. dir .. "/" .. filename - else - return "" - end - end - elseif vim.fn.executable("xclip") == 1 then - get_paste_command = function(dir, filename) - return "xclip -selection clipboard -t image/png -o > " - .. dir - .. "/" - .. filename - end - elseif vim.fn.executable("wl-paste") == 1 then - get_paste_command = function(dir, filename) - return "wl-paste -n -t image/png > " .. dir .. "/" .. filename - end - elseif vim.fn.executable("osascript") == 1 then - get_paste_command = function(dir, filename) - return string.format( - 'osascript -e "tell application \\"System Events\\" to write (the clipboard as «class PNGf») to ' - .. '(make new file at folder \\"%s\\" with properties {name:\\"%s\\"})"', - dir, - filename + if paste_command[M.Cfg.clipboard_program] ~= nil then + if vim.fn.executable(M.Cfg.clipboard_program) ~= 1 then + vim.api.nvim_err_write( + "The clipboard program specified [`" + .. M.cfg.clipboard_program + .. "`] is not executable or not in your $PATH\n" ) end + get_paste_command = paste_command[M.Cfg.clipboard_program] + elseif vim.fn.executable("xsel") == 1 then + get_paste_command = paste_command["xsel"] + elseif vim.fn.executable("xclip") == 1 then + get_paste_command = paste_command["xclip"] + elseif vim.fn.executable("wl-paste") == 1 then + get_paste_command = paste_command["wl-paste"] + elseif vim.fn.executable("osascript") == 1 then + get_paste_command = paste_command["osascript"] else - vim.api.nvim_err_write("No xclip installed!\n") - return + vim.api.nvim_err_write( + "No clipboard programs found!\nChecked executables: xsel, xclip, wl-paste, osascript\n" + ) end -- TODO: check `xclip -selection clipboard -t TARGETS -o` for the occurrence of `image/png`