Neovim plugin improving access to clipboard history (mirror)
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request #3 from ptdewey/dev

feat(added custom keymap support)

authored by

Patrick and committed by
GitHub
3f3199cb 5ec28cb3

+51 -34
+20 -7
README.md
··· 38 38 The setup function also supports taking in a table of options: 39 39 | Option | Type | Default | 40 40 |-------------|--------------------------------------------|----------------| 41 - | max_entries | integer number of entries to show in popup | 10 | 42 - | sep | string separator to show between table entries | "-----" | 41 + | max_entries | integer number of entries to show in popup | `10` | 42 + | sep | string separator to show between table entries | `"-----"` | 43 + | keymaps | table containing keymap overrides | `{}` | 44 + | keymaps.navigation_next | string | `"j"` | 45 + | keymaps.navigation_prev | string | `"k"` | 46 + | keymaps.paste | string | `"<CR>"` | 47 + | keymaps.yank | string | `"yy"` | 48 + | keymaps.close | table of strings | `{ "<Esc>", "<C-c>", "q" }` | 43 49 44 50 45 51 If no separator is desired, pass in an empty string for sep: ··· 48 54 require('yankbank').setup({ 49 55 max_entries = 12, 50 56 sep = "", 57 + keymaps = { 58 + navigation_next = "j", 59 + navigation_prev = "k", 60 + }, 51 61 }) 52 62 end, 53 63 ``` ··· 60 70 I would personally also recommend setting a keybind to open the menu. 61 71 ```lua 62 72 -- map to '<leader>y' 63 - vim.keymap.set("n", "<leader>y", ":YankBank<CR>", { noremap = true }) 73 + vim.keymap.set("n", "<leader>y", "<cmd>YankBank<CR>", { noremap = true }) 64 74 ``` 65 75 66 76 ## Potential Improvements 67 - - Expose popup keybind behavior through setup options 68 - - Access to other registers (number/letter registers?) 77 + - Persistence between sessions (through either sqlite database or just a file) 69 78 - Polling on unnamedplus register to populate bank in more intuitive manner (could be enabled as option) 79 + - Number based navigation of menu 80 + - nvim-cmp integration 81 + - fzf integration 82 + - Setup options configuring which registers are included 70 83 71 84 ## Alternatives 72 - - [neoclip](https://github.com/AckslD/nvim-neoclip.lua) 73 - - [Yanky](https://github.com/gbprod/yanky.nvim) 85 + - [nvim-neoclip](https://github.com/AckslD/nvim-neoclip.lua) 86 + - [yanky.nvim](https://github.com/gbprod/yanky.nvim)
+8 -16
lua/yankbank/init.lua
··· 11 11 local sep = "-----" 12 12 13 13 -- wrapper function for main plugin functionality 14 - local function show_yank_bank(args) 14 + local function show_yank_bank(opts) 15 15 -- Parse command arguments directly if args are provided as a string 16 - local opts = {} 17 - if type(args) == "string" and args ~= "" then 18 - local parts = vim.split(args, "%s+", {true}) 19 - opts.max_entries = tonumber(parts[1]) 20 - opts.sep = parts[2] 21 - elseif type(args) == "table" then 22 - -- If opts is already a table, use it directly (for programmatic calls) 23 - opts = args 24 - end 16 + opts = opts or {} 25 17 26 18 -- Fallback to defaults if necessary 27 19 local max_entries_opt = opts.max_entries or max_entries 28 20 local sep_opt = opts.sep or sep 21 + opts.keymaps = opts.keymaps or {} 29 22 30 23 local bufnr, display_lines, line_yank_map = menu.create_and_fill_buffer(yanks, max_entries_opt, sep_opt) 31 24 -- handle empty bank case ··· 33 26 return 34 27 end 35 28 local win_id = menu.open_window(bufnr, display_lines) 36 - menu.set_keymaps(win_id, bufnr, yanks, line_yank_map) 29 + menu.set_keymaps(win_id, bufnr, yanks, line_yank_map, opts) 37 30 end 38 31 39 32 -- plugin setup ··· 42 35 43 36 -- parse opts 44 37 max_entries = opts.max_entries or max_entries 45 - sep = opts.sep or sep 38 + -- sep = opts.sep or sep 46 39 47 40 -- create clipboard autocmds 48 41 clipboard.setup_yank_autocmd(yanks, max_entries) 49 42 50 43 -- Create user command 51 - -- TODO: allow params (i.e. keymaps/max_entries/separator) 52 - vim.api.nvim_create_user_command("YankBank", function(args) 53 - show_yank_bank(args.args) 54 - end, { desc = "Show Recent Yanks", nargs = "*" }) 44 + vim.api.nvim_create_user_command("YankBank", function() 45 + show_yank_bank(opts) 46 + end, { desc = "Show Recent Yanks" }) 55 47 end 56 48 57 49 return M
+23 -11
lua/yankbank/menu.lua
··· 50 50 end 51 51 52 52 -- define buffer window width and height based on number of columns 53 + -- FIX: long enough entries will cause window to go below end of screen 54 + -- FIX: wrapping long lines will cause entries below to not show in menu (requires scrolling to see) 53 55 local width = math.min(max_width + 4, vim.api.nvim_get_option_value("columns", {})) 54 56 local height = math.min(display_lines and #display_lines or 1, vim.api.nvim_get_option_value("lines", {})) 55 57 ··· 71 73 end 72 74 73 75 -- Set key mappings for the popup window 74 - -- TODO: configurable options (take in inside setup function) 75 - function M.set_keymaps(win_id, bufnr, yanks, line_yank_map) 76 + function M.set_keymaps(win_id, bufnr, yanks, line_yank_map, opts) 76 77 -- Key mappings for selection and closing the popup 77 78 local map_opts = { noremap = true, silent = true, buffer = bufnr } 79 + 80 + -- default plugin keymaps 81 + local default_keymaps = { 82 + navigation_next = "j", 83 + navigation_prev = "k", 84 + paste = "<CR>", 85 + yank = "yy", 86 + close = { "<Esc>", "<C-c>", "q" }, -- TODO: issues might arise passing non-table single value for this 87 + } 88 + 89 + -- merge default and options keymap tables 90 + local k = vim.tbl_deep_extend("force", default_keymaps, opts.keymaps or {}) 78 91 79 92 -- popup buffer navigation binds 80 - vim.keymap.set('n', 'j', helpers.next_numbered_item, 93 + vim.keymap.set("n", k.navigation_next, helpers.next_numbered_item, 81 94 { noremap = true, silent = true, buffer = bufnr }) 82 - vim.keymap.set('n', 'k', helpers.prev_numbered_item, 95 + vim.keymap.set("n", k.navigation_prev, helpers.prev_numbered_item, 83 96 { noremap = true, silent = true, buffer = bufnr }) 84 97 85 - -- bind paste behavior to enter 86 - vim.keymap.set('n', '<CR>', function() 98 + -- bind paste behavior 99 + vim.keymap.set("n", k.paste, function() 87 100 local cursor = vim.api.nvim_win_get_cursor(win_id)[1] 88 101 -- use the mapping to find the original yank 89 102 local yankIndex = line_yank_map[cursor] ··· 99 112 end 100 113 end, { buffer = bufnr }) 101 114 102 - -- bind yank behavior to y 103 - vim.keymap.set('n', 'yy', function() 115 + -- bind yank behavior 116 + vim.keymap.set("n", k.yank, function() 104 117 local cursor = vim.api.nvim_win_get_cursor(win_id)[1] 105 118 local yankIndex = line_yank_map[cursor] 106 119 if yankIndex then ··· 112 125 end, { buffer = bufnr }) 113 126 114 127 -- close popup keybinds 115 - local close_maps = { "<Esc>", "<C-c>", "q" } 116 - for _, map in ipairs(close_maps) do 117 - vim.keymap.set('n', map, function() 128 + for _, map in ipairs(k.close) do 129 + vim.keymap.set("n", map, function() 118 130 vim.api.nvim_win_close(win_id, true) 119 131 end, map_opts) 120 132 end