diff --git a/README.md b/README.md index b9183be..a1616bb 100644 --- a/README.md +++ b/README.md @@ -156,6 +156,7 @@ require('neoclip').setup({ select = '', paste = '', paste_behind = '', + paste_visual = '', replay = '', -- replay a macro delete = '', -- delete an entry edit = '', -- edit an entry @@ -167,6 +168,7 @@ require('neoclip').setup({ --- It is possible to map to more than one key. -- paste = { 'p', '' }, paste_behind = 'P', + paste_visual = 'v', replay = 'q', delete = 'd', edit = 'e', @@ -177,6 +179,7 @@ require('neoclip').setup({ select = 'default', paste = 'ctrl-p', paste_behind = 'ctrl-k', + paste_visual = 'ctrl-v', custom = {}, }, }, @@ -332,6 +335,16 @@ if using `telescope` or ``` if using `fzf-lua`. +### Visual mode + +If you want to select some text and replace it with a Neoclip selection, you need the following keymap: + +```vim +:Telescope neoclip +``` + +The `` is necessary to "save" the visual selection before opening a foating buffer. If not, the visual selection is lost. If we don't do this, there will be an error `E481: No range allowed` because Telescope does not support ranges (it seems). If the mapping were `Telescope neoclip`, the visual range would be dropped before opening Telescope without being saved so the visual paste later on would be wrong. + ### Macros If `enable_macro_history` is set to `true` (default) in the [`setup`](#configuration) then any recorded macro will be stored and can later be accessed using: ```vim @@ -381,7 +394,7 @@ You can edit the contents of an entry using the keybinds for `edit`. It'll open end return true end - + require('neoclip').setup{ ... filter = function(data) diff --git a/doc/neoclip.txt b/doc/neoclip.txt index 6275a2e..0114bdb 100644 --- a/doc/neoclip.txt +++ b/doc/neoclip.txt @@ -140,6 +140,7 @@ The following are the defaults and the keys are explained below: select = '', paste = '', paste_behind = '', + paste_visual = '', replay = '', -- replay a macro delete = '', -- delete an entry edit = '', -- edit an entry @@ -151,6 +152,7 @@ The following are the defaults and the keys are explained below: --- It is possible to map to more than one key. -- paste = { 'p', '' }, paste_behind = 'P', + paste_visual = 'v', replay = 'q', delete = 'd', edit = 'e', @@ -161,6 +163,7 @@ The following are the defaults and the keys are explained below: select = 'default', paste = 'ctrl-p', paste_behind = 'ctrl-k', + paste_visual = 'ctrl-v', custom = {}, }, }, @@ -424,7 +427,7 @@ TIPS *neoclip-nvim-neoclip.lua-tips* local function is_whitespace(line) return vim.fn.match(line, [[^\s*$]]) ~= -1 end - + local function all(tbl, check) for _, entry in ipairs(tbl) do if not check(entry) then @@ -433,7 +436,7 @@ TIPS *neoclip-nvim-neoclip.lua-tips* end return true end - + require('neoclip').setup{ ... filter = function(data) @@ -484,10 +487,10 @@ PREVIEW = FALSE AND CONTENT_SPEC_COLUMN = FALSE ~ 2. Links *neoclip-links* 1. *neoclip*: https://user-images.githubusercontent.com/23341710/140090515-83a08f0f-85f9-4278-bcbe-48e4d8442ace.png -2. *@cdown*: -3. *@fdschmidt93*: -4. *@ibhagwan*: -5. *@kkharji*: +2. *@cdown*: +3. *@fdschmidt93*: +4. *@ibhagwan*: +5. *@kkharji*: 6. *preview*: https://user-images.githubusercontent.com/23341710/140090515-83a08f0f-85f9-4278-bcbe-48e4d8442ace.png 7. *content_spec_column*: https://user-images.githubusercontent.com/23341710/140090472-3271affa-7efd-40bd-9d20-562b2074b261.png 8. *clean*: https://user-images.githubusercontent.com/23341710/140090327-30bfff28-83ff-4695-82b8-8d4abfd68546.png diff --git a/lua/neoclip/fzf.lua b/lua/neoclip/fzf.lua index 80781df..bdb9c0c 100644 --- a/lua/neoclip/fzf.lua +++ b/lua/neoclip/fzf.lua @@ -59,6 +59,7 @@ local function make_actions(register_names) [keys.select] = get_set_register_handler(register_names), [keys.paste] = get_paste_handler(register_names, 'p'), [keys.paste_behind] = get_paste_handler(register_names, 'P'), + [keys.paste_visual] = get_paste_handler(register_names, 'v'), } if keys.custom ~= nil then for key, action in pairs(keys.custom) do diff --git a/lua/neoclip/handlers.lua b/lua/neoclip/handlers.lua index 1fadb82..303dbb3 100644 --- a/lua/neoclip/handlers.lua +++ b/lua/neoclip/handlers.lua @@ -91,11 +91,24 @@ end -- TODO can this be done without setting the register? M.paste = function(entry, op) + if op == "v" then + return M.paste_visual(entry) + end + temporary_reg_usage(entry, function(register_name) vim.cmd(string.format('normal! "%s%s', register_name, op)) end) end +M.paste_visual = function(entry) + temporary_reg_usage(entry, function(register_name) + -- `gv` is needed to reselect the last visual selection. + -- NOTE: This only works if the last visual selection was saved by + -- returing back to normal mode before opening neoclip. + vim.api.nvim_feedkeys('gv"'..register_name..'p', "n", false) + end) +end + -- TODO can this be done without setting the register? M.replay = function(entry) temporary_reg_usage(entry, function(register_name) diff --git a/lua/neoclip/settings.lua b/lua/neoclip/settings.lua index 7d73fdb..debb87c 100644 --- a/lua/neoclip/settings.lua +++ b/lua/neoclip/settings.lua @@ -36,6 +36,7 @@ local settings = { select = '', paste = '', paste_behind = '', + paste_visual = '', replay = '', delete = '', edit = '', @@ -45,6 +46,7 @@ local settings = { select = '', paste = 'p', paste_behind = 'P', + paste_visual = 'v', replay = 'q', delete = 'd', edit = 'e', @@ -55,6 +57,7 @@ local settings = { select = 'default', paste = 'ctrl-p', paste_behind = 'ctrl-k', + paste_visual = 'ctrl-v', custom = {}, }, }, diff --git a/lua/neoclip/telescope.lua b/lua/neoclip/telescope.lua index 429dbd1..998d9a3 100644 --- a/lua/neoclip/telescope.lua +++ b/lua/neoclip/telescope.lua @@ -299,6 +299,7 @@ local function get_export(register_names, typ) map_if_set(map, mode, keys.select, 'select', get_set_register_handler(register_names, typ)) map_if_set(map, mode, keys.paste, 'paste', get_paste_handler(register_names, typ, 'p', current_buffer)) map_if_set(map, mode, keys.paste_behind, 'paste_behind', get_paste_handler(register_names, typ, 'P', current_buffer)) + map_if_set(map, mode, keys.paste_visual, 'paste_visual', get_paste_handler(register_names, typ, 'v', current_buffer)) map_if_set(map, mode, keys.replay, 'replay', get_replay_recording_handler(register_names, typ, current_buffer)) map_if_set(map, mode, keys.delete, 'delete', get_delete_handler(typ)) map_if_set(map, mode, keys.edit, 'edit', get_edit_handler(typ, opts)) diff --git a/setup.lua.example b/setup.lua.example index fea2d32..80115df 100644 --- a/setup.lua.example +++ b/setup.lua.example @@ -21,6 +21,7 @@ return { select = "", paste = "", paste_behind = "", + paste_visual = "", replay = "", -- replay a macro delete = "", -- delete an entry edit = "", -- edit an entry @@ -32,6 +33,7 @@ return { --- It is possible to map to more than one key. -- paste = { 'p', '' }, paste_behind = "P", + paste_visual = "v", replay = "q", delete = "d", edit = "e", @@ -42,6 +44,7 @@ return { select = "default", paste = "ctrl-p", paste_behind = "ctrl-k", + paste_visual = "ctrl-v", custom = {}, }, }, diff --git a/tests/plenary/neoclip_spec.lua b/tests/plenary/neoclip_spec.lua index db51eef..7250049 100644 --- a/tests/plenary/neoclip_spec.lua +++ b/tests/plenary/neoclip_spec.lua @@ -600,6 +600,7 @@ some line]], select = '', paste = '', paste_behind = '', + paste_visual = '', replay = '', delete = '', edit = '', @@ -613,6 +614,7 @@ some line]], select = 'a', paste = 'b', paste_behind = 'c', + paste_visual = 'v', replay = 'd', delete = 'e', edit = 'e', @@ -627,6 +629,7 @@ some line]], select = '', paste = '', paste_behind = '', + paste_visual = '', custom = { [''] = function(opts) return opts