From bbccbbcd20d78e88dc629c32014f367e7a90f6c1 Mon Sep 17 00:00:00 2001 From: cinuor Date: Wed, 13 Apr 2022 17:31:15 +0800 Subject: [PATCH 1/3] feat: add system clipboard support --- README.md | 4 ++++ lua/neoclip/handlers.lua | 30 ++++++++++++++++++++++++++++++ lua/neoclip/settings.lua | 1 + lua/neoclip/telescope.lua | 2 ++ 4 files changed, 37 insertions(+) diff --git a/README.md b/README.md index 5e84816..0757dde 100644 --- a/README.md +++ b/README.md @@ -150,6 +150,10 @@ use { With `fzf-lua` only insert mode is supported and `fzf`-style key-syntax needs to be used. You can also use the `custom` entry to specify custom actions to take on certain key-presses, see [below](#custom-actions) for more details. NOTE: these are only set in the `telescope` buffer and you need to setup your own keybindings to for example open `telescope`. +* `enable_system_clipboard`: if set to `true`, the content in register `*` and + `+` will be stored. + NOTE: clipboard provider need to set up correctly in neovim, see `:help clipboard` for + more details See screenshot section below for how the settings above might affect the looks. diff --git a/lua/neoclip/handlers.lua b/lua/neoclip/handlers.lua index 2a9c279..026cf5f 100644 --- a/lua/neoclip/handlers.lua +++ b/lua/neoclip/handlers.lua @@ -27,6 +27,11 @@ local function get_regtype(regtype) end end +local function get_register_event(register) + local event = vim.fn.getreginfo(register) + return event +end + M.handle_yank_post = function() if neoclip.stopped then return @@ -92,6 +97,31 @@ end M.delete = function(typ, entry) storage.delete(typ, entry) + if settings.enable_system_clipboard then + for _, register in ipairs({'+', '*'}) do + vim.fn.setreg(register, {}, 'c') + end + end end +M.handle_system_yank = function () + if neoclip.stopped then + return + end + if not settings.enable_system_clipboard then + return + end + for _, register in ipairs({'+', '*'}) do + local event = get_register_event(register) + if #event.regcontents ~= 0 then + if should_add(event) then + storage.insert({ + regtype = get_regtype(event.regtype), + contents = event.regcontents, + filetype = vim.bo.filetype, + }, 'yanks') + end + end + end +end return M diff --git a/lua/neoclip/settings.lua b/lua/neoclip/settings.lua index 8013ca7..bbba114 100644 --- a/lua/neoclip/settings.lua +++ b/lua/neoclip/settings.lua @@ -4,6 +4,7 @@ local settings = { history = 1000, enable_persistent_history = false, continuous_sync = false, + enable_system_clipboard = false, db_path = vim.fn.stdpath("data") .. "/databases/neoclip.sqlite3", filter = nil, preview = true, diff --git a/lua/neoclip/telescope.lua b/lua/neoclip/telescope.lua index d40028d..c841e22 100644 --- a/lua/neoclip/telescope.lua +++ b/lua/neoclip/telescope.lua @@ -162,6 +162,8 @@ local function get_export(register_names, typ) if opts ~= nil and opts.extra ~= nil then register_names = utils.join(register_names, parse_extra(opts.extra)) end + -- NOTE: use system clipboard + handlers.handle_system_yank() local results = storage.get({reversed = true})[typ] pickers.new(opts, { prompt_title = picker_utils.make_prompt_title(register_names), From 106b1edc739e24267cc9bafc365a20f800c0044c Mon Sep 17 00:00:00 2001 From: cinuor Date: Wed, 13 Apr 2022 17:44:52 +0800 Subject: [PATCH 2/3] fix: 4 space for tab --- lua/neoclip/handlers.lua | 42 +++++++++++++++++++-------------------- lua/neoclip/telescope.lua | 2 +- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/lua/neoclip/handlers.lua b/lua/neoclip/handlers.lua index 026cf5f..b051558 100644 --- a/lua/neoclip/handlers.lua +++ b/lua/neoclip/handlers.lua @@ -29,7 +29,7 @@ end local function get_register_event(register) local event = vim.fn.getreginfo(register) - return event + return event end M.handle_yank_post = function() @@ -97,31 +97,31 @@ end M.delete = function(typ, entry) storage.delete(typ, entry) - if settings.enable_system_clipboard then - for _, register in ipairs({'+', '*'}) do - vim.fn.setreg(register, {}, 'c') - end - end + if settings.enable_system_clipboard then + for _, register in ipairs({'+', '*'}) do + vim.fn.setreg(register, {}, 'c') + end + end end M.handle_system_yank = function () if neoclip.stopped then return end - if not settings.enable_system_clipboard then - return - end - for _, register in ipairs({'+', '*'}) do - local event = get_register_event(register) - if #event.regcontents ~= 0 then - if should_add(event) then - storage.insert({ - regtype = get_regtype(event.regtype), - contents = event.regcontents, - filetype = vim.bo.filetype, - }, 'yanks') - end - end - end + if not settings.enable_system_clipboard then + return + end + for _, register in ipairs({'+', '*'}) do + local event = get_register_event(register) + if #event.regcontents ~= 0 then + if should_add(event) then + storage.insert({ + regtype = get_regtype(event.regtype), + contents = event.regcontents, + filetype = vim.bo.filetype, + }, 'yanks') + end + end + end end return M diff --git a/lua/neoclip/telescope.lua b/lua/neoclip/telescope.lua index c841e22..907b79b 100644 --- a/lua/neoclip/telescope.lua +++ b/lua/neoclip/telescope.lua @@ -162,7 +162,7 @@ local function get_export(register_names, typ) if opts ~= nil and opts.extra ~= nil then register_names = utils.join(register_names, parse_extra(opts.extra)) end - -- NOTE: use system clipboard + -- NOTE: use system clipboard handlers.handle_system_yank() local results = storage.get({reversed = true})[typ] pickers.new(opts, { From df69f470a034034a3f2e5a767093395edf659360 Mon Sep 17 00:00:00 2001 From: cinuor Date: Thu, 14 Apr 2022 17:59:16 +0800 Subject: [PATCH 3/3] fix: not delete system clipboard and remove system_yank in telescope picker --- lua/neoclip/handlers.lua | 5 ----- lua/neoclip/telescope.lua | 2 -- 2 files changed, 7 deletions(-) diff --git a/lua/neoclip/handlers.lua b/lua/neoclip/handlers.lua index b051558..ba6258a 100644 --- a/lua/neoclip/handlers.lua +++ b/lua/neoclip/handlers.lua @@ -97,11 +97,6 @@ end M.delete = function(typ, entry) storage.delete(typ, entry) - if settings.enable_system_clipboard then - for _, register in ipairs({'+', '*'}) do - vim.fn.setreg(register, {}, 'c') - end - end end M.handle_system_yank = function () diff --git a/lua/neoclip/telescope.lua b/lua/neoclip/telescope.lua index 907b79b..d40028d 100644 --- a/lua/neoclip/telescope.lua +++ b/lua/neoclip/telescope.lua @@ -162,8 +162,6 @@ local function get_export(register_names, typ) if opts ~= nil and opts.extra ~= nil then register_names = utils.join(register_names, parse_extra(opts.extra)) end - -- NOTE: use system clipboard - handlers.handle_system_yank() local results = storage.get({reversed = true})[typ] pickers.new(opts, { prompt_title = picker_utils.make_prompt_title(register_names),