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 #15 from ptdewey/fix-14

fix: fixed error on yank/delete with no specified opts

authored by

Patrick Dewey and committed by
GitHub
8b36fcb4 6bd9ca9a

+65 -44
+44 -31
lua/yankbank/clipboard.lua
··· 4 4 -- Function to add yanked text to table 5 5 function M.add_yank(yanks, reg_types, text, reg_type, max_entries) 6 6 -- avoid adding empty strings 7 - if text ~= "" and text ~= " " and text ~= "\n" then 8 - -- do not update with duplicate values 9 - for _, entry in ipairs(yanks) do 10 - if entry == text then 11 - return 12 - end 13 - end 14 - table.insert(yanks, 1, text) 15 - table.insert(reg_types, 1, reg_type) 16 - if #yanks > max_entries then 17 - table.remove(yanks) 18 - table.remove(reg_types) 7 + if text == "" and text == " " and text == "\n" then 8 + return 9 + end 10 + 11 + -- do not update with duplicate values 12 + for _, entry in ipairs(yanks) do 13 + if entry == text then 14 + return 19 15 end 20 16 end 17 + 18 + table.insert(yanks, 1, text) 19 + table.insert(reg_types, 1, reg_type) 20 + 21 + if #yanks > max_entries then 22 + table.remove(yanks) 23 + table.remove(reg_types) 24 + end 21 25 end 22 26 23 27 -- set up plugin autocommands ··· 26 30 -- autocommand to listen for yank events 27 31 vim.api.nvim_create_autocmd("TextYankPost", { 28 32 callback = function() 29 - -- TODO: this function can be expanded to incorporate other registers 30 - -- - use vim.v.event.regname and an allowlist 31 - -- local reg_type = vim.v.event.operator 32 - -- if reg_type == "y" or reg_type == "d" then 33 - -- local yanked_text = vim.fn.getreg('"') 34 - 35 33 -- get register information 36 34 local rn = vim.v.event.regname 37 - local reg_type = vim.fn.getregtype('"') 38 35 39 36 -- check if changes were made to default register 40 - if vim.v.event.regname == "" then 41 - local yank = vim.fn.getreg(rn) 37 + if rn == "" or rn == "+" then 38 + local reg_type = vim.fn.getregtype(rn) 39 + local yank_text = vim.fn.getreg(rn) 42 40 43 - -- NOTE: this only blocks adding to list if something else is in plus register 44 - if string.len(yank) <= 1 then 41 + if not yank_text or type(yank_text) ~= "string" then 45 42 return 46 43 end 47 44 48 - M.add_yank(yanks, reg_types, yank, reg_type, opts.max_entries) 45 + if #yank_text <= 1 then 46 + return 47 + end 48 + M.add_yank( 49 + yanks, 50 + reg_types, 51 + yank_text, 52 + reg_type, 53 + opts.max_entries 54 + ) 49 55 end 50 56 end, 51 57 }) 52 58 53 59 -- poll registers when vim is focused (check for new clipboard activity) 54 - if opts.focus_gain_poll and opts.focus_gain_poll == true then 60 + if opts.focus_gain_poll == true then 55 61 vim.api.nvim_create_autocmd("FocusGained", { 56 62 callback = function() 57 63 -- get register information 58 64 local reg_type = vim.fn.getregtype("+") 59 - local yank = vim.fn.getreg("+") 65 + local yank_text = vim.fn.getreg("+") 66 + 67 + if not yank_text or type(yank_text) ~= "string" then 68 + return 69 + end 60 70 61 - -- NOTE: do not add yanks of 1 or 0 characters 62 - if string.len(yank) <= 1 then 71 + if string.len(yank_text) <= 1 then 63 72 return 64 73 end 65 74 66 - M.add_yank(yanks, reg_types, yank, reg_type, opts.max_entries) 75 + M.add_yank( 76 + yanks, 77 + reg_types, 78 + yank_text, 79 + reg_type, 80 + opts.max_entries 81 + ) 67 82 end, 68 83 }) 69 84 end 70 - 71 - -- TODO: focus lost hook? 72 85 end 73 86 74 87 return M
+21 -13
lua/yankbank/init.lua
··· 8 8 -- initialize yanks tables 9 9 local yanks = {} 10 10 local reg_types = {} 11 - local max_entries = 10 12 - local sep = "-----" 11 + local default_opts = { 12 + max_entries = 10, 13 + sep = "-----", 14 + focus_gain_poll = false, 15 + num_behavior = "prefix", 16 + registers = { 17 + yank_register = "+", 18 + }, 19 + keymaps = {}, 20 + } 13 21 14 22 -- wrapper function for main plugin functionality 15 23 local function show_yank_bank(opts) 16 24 -- Parse command arguments directly if args are provided as a string 17 - opts = opts or {} 25 + opts = opts or default_opts 18 26 19 - -- Fallback to defaults if necessary 20 - local max_entries_opt = opts.max_entries or max_entries 21 - local sep_opt = opts.sep or sep 22 - opts.keymaps = opts.keymaps or {} 27 + -- initialize buffer and populate bank 28 + local bufnr, display_lines, line_yank_map = menu.create_and_fill_buffer( 29 + yanks, 30 + reg_types, 31 + opts.max_entries, 32 + opts.sep 33 + ) 23 34 24 - local bufnr, display_lines, line_yank_map = 25 - menu.create_and_fill_buffer(yanks, reg_types, max_entries_opt, sep_opt) 26 35 -- handle empty bank case 27 36 if not bufnr then 28 37 return 29 38 end 39 + 40 + -- open window and set keybinds 30 41 local win_id = menu.open_window(bufnr, display_lines) 31 42 menu.set_keymaps(win_id, bufnr, yanks, reg_types, line_yank_map, opts) 32 43 end 33 44 34 45 -- plugin setup 35 46 function M.setup(opts) 36 - opts = opts or {} 37 - 38 - -- parse opts 39 - max_entries = opts.max_entries or max_entries 47 + opts = opts or default_opts 40 48 41 49 -- create clipboard autocmds 42 50 clipboard.setup_yank_autocmd(yanks, reg_types, opts)