Skip to content

Commit

Permalink
Add visual paste
Browse files Browse the repository at this point in the history
Closes #128
  • Loading branch information
peterfication committed Jul 11, 2024
1 parent 709c97f commit fe02b72
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 7 deletions.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ require('neoclip').setup({
select = '<cr>',
paste = '<c-p>',
paste_behind = '<c-k>',
paste_visual = '<c-v>',
replay = '<c-q>', -- replay a macro
delete = '<c-d>', -- delete an entry
edit = '<c-e>', -- edit an entry
Expand All @@ -167,6 +168,7 @@ require('neoclip').setup({
--- It is possible to map to more than one key.
-- paste = { 'p', '<c-p>' },
paste_behind = 'P',
paste_visual = 'v',
replay = 'q',
delete = 'd',
edit = 'e',
Expand All @@ -177,6 +179,7 @@ require('neoclip').setup({
select = 'default',
paste = 'ctrl-p',
paste_behind = 'ctrl-k',
paste_visual = 'ctrl-v',
custom = {},
},
},
Expand Down Expand Up @@ -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 for visual mode:

```vim
<ESC>:Telescope neoclip<CR>
```

The `<ESC>` is necessary to "save" the visual selection before opening a floating 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 `<CMD>Telescope neoclip<CR>`, 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
Expand Down Expand Up @@ -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)
Expand Down
15 changes: 9 additions & 6 deletions doc/neoclip.txt
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ The following are the defaults and the keys are explained below:
select = '<cr>',
paste = '<c-p>',
paste_behind = '<c-k>',
paste_visual = '<c-v>',
replay = '<c-q>', -- replay a macro
delete = '<c-d>', -- delete an entry
edit = '<c-e>', -- edit an entry
Expand All @@ -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', '<c-p>' },
paste_behind = 'P',
paste_visual = 'v',
replay = 'q',
delete = 'd',
edit = 'e',
Expand All @@ -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 = {},
},
},
Expand Down Expand Up @@ -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
Expand All @@ -433,7 +436,7 @@ TIPS *neoclip-nvim-neoclip.lua-tips*
end
return true
end

require('neoclip').setup{
...
filter = function(data)
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lua/neoclip/fzf.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions lua/neoclip/handlers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions lua/neoclip/settings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ local settings = {
select = '<cr>',
paste = '<c-p>',
paste_behind = '<c-k>',
paste_visual = '<c-v>',
replay = '<c-q>',
delete = '<c-d>',
edit = '<c-e>',
Expand All @@ -45,6 +46,7 @@ local settings = {
select = '<cr>',
paste = 'p',
paste_behind = 'P',
paste_visual = 'v',
replay = 'q',
delete = 'd',
edit = 'e',
Expand All @@ -55,6 +57,7 @@ local settings = {
select = 'default',
paste = 'ctrl-p',
paste_behind = 'ctrl-k',
paste_visual = 'ctrl-v',
custom = {},
},
},
Expand Down
1 change: 1 addition & 0 deletions lua/neoclip/telescope.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
3 changes: 3 additions & 0 deletions setup.lua.example
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ return {
select = "<cr>",
paste = "<c-p>",
paste_behind = "<c-k>",
paste_visual = "<c-v>",
replay = "<c-q>", -- replay a macro
delete = "<c-d>", -- delete an entry
edit = "<c-e>", -- edit an entry
Expand All @@ -32,6 +33,7 @@ return {
--- It is possible to map to more than one key.
-- paste = { 'p', '<c-p>' },
paste_behind = "P",
paste_visual = "v",
replay = "q",
delete = "d",
edit = "e",
Expand All @@ -42,6 +44,7 @@ return {
select = "default",
paste = "ctrl-p",
paste_behind = "ctrl-k",
paste_visual = "ctrl-v",
custom = {},
},
},
Expand Down
3 changes: 3 additions & 0 deletions tests/plenary/neoclip_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,7 @@ some line]],
select = '<c-a>',
paste = '<c-b>',
paste_behind = '<c-c>',
paste_visual = '<c-v>',
replay = '<c-d>',
delete = '<c-e>',
edit = '<c-e>',
Expand All @@ -613,6 +614,7 @@ some line]],
select = 'a',
paste = 'b',
paste_behind = 'c',
paste_visual = 'v',
replay = 'd',
delete = 'e',
edit = 'e',
Expand All @@ -627,6 +629,7 @@ some line]],
select = '<c-a>',
paste = '<c-b>',
paste_behind = '<c-c>',
paste_visual = '<c-v>',
custom = {
['<c-e>'] = function(opts)
return opts
Expand Down

0 comments on commit fe02b72

Please sign in to comment.