···44-- Function to add yanked text to table
55function M.add_yank(yanks, reg_types, text, reg_type, max_entries)
66 -- avoid adding empty strings
77- if text ~= "" and text ~= " " and text ~= "\n" then
88- -- do not update with duplicate values
99- for _, entry in ipairs(yanks) do
1010- if entry == text then
1111- return
1212- end
1313- end
1414- table.insert(yanks, 1, text)
1515- table.insert(reg_types, 1, reg_type)
1616- if #yanks > max_entries then
1717- table.remove(yanks)
1818- table.remove(reg_types)
77+ if text == "" and text == " " and text == "\n" then
88+ return
99+ end
1010+1111+ -- do not update with duplicate values
1212+ for _, entry in ipairs(yanks) do
1313+ if entry == text then
1414+ return
1915 end
2016 end
1717+1818+ table.insert(yanks, 1, text)
1919+ table.insert(reg_types, 1, reg_type)
2020+2121+ if #yanks > max_entries then
2222+ table.remove(yanks)
2323+ table.remove(reg_types)
2424+ end
2125end
22262327-- set up plugin autocommands
···2630 -- autocommand to listen for yank events
2731 vim.api.nvim_create_autocmd("TextYankPost", {
2832 callback = function()
2929- -- TODO: this function can be expanded to incorporate other registers
3030- -- - use vim.v.event.regname and an allowlist
3131- -- local reg_type = vim.v.event.operator
3232- -- if reg_type == "y" or reg_type == "d" then
3333- -- local yanked_text = vim.fn.getreg('"')
3434-3533 -- get register information
3634 local rn = vim.v.event.regname
3737- local reg_type = vim.fn.getregtype('"')
38353936 -- check if changes were made to default register
4040- if vim.v.event.regname == "" then
4141- local yank = vim.fn.getreg(rn)
3737+ if rn == "" or rn == "+" then
3838+ local reg_type = vim.fn.getregtype(rn)
3939+ local yank_text = vim.fn.getreg(rn)
42404343- -- NOTE: this only blocks adding to list if something else is in plus register
4444- if string.len(yank) <= 1 then
4141+ if not yank_text or type(yank_text) ~= "string" then
4542 return
4643 end
47444848- M.add_yank(yanks, reg_types, yank, reg_type, opts.max_entries)
4545+ if #yank_text <= 1 then
4646+ return
4747+ end
4848+ M.add_yank(
4949+ yanks,
5050+ reg_types,
5151+ yank_text,
5252+ reg_type,
5353+ opts.max_entries
5454+ )
4955 end
5056 end,
5157 })
52585359 -- poll registers when vim is focused (check for new clipboard activity)
5454- if opts.focus_gain_poll and opts.focus_gain_poll == true then
6060+ if opts.focus_gain_poll == true then
5561 vim.api.nvim_create_autocmd("FocusGained", {
5662 callback = function()
5763 -- get register information
5864 local reg_type = vim.fn.getregtype("+")
5959- local yank = vim.fn.getreg("+")
6565+ local yank_text = vim.fn.getreg("+")
6666+6767+ if not yank_text or type(yank_text) ~= "string" then
6868+ return
6969+ end
60706161- -- NOTE: do not add yanks of 1 or 0 characters
6262- if string.len(yank) <= 1 then
7171+ if string.len(yank_text) <= 1 then
6372 return
6473 end
65746666- M.add_yank(yanks, reg_types, yank, reg_type, opts.max_entries)
7575+ M.add_yank(
7676+ yanks,
7777+ reg_types,
7878+ yank_text,
7979+ reg_type,
8080+ opts.max_entries
8181+ )
6782 end,
6883 })
6984 end
7070-7171- -- TODO: focus lost hook?
7285end
73867487return M
+21-13
lua/yankbank/init.lua
···88-- initialize yanks tables
99local yanks = {}
1010local reg_types = {}
1111-local max_entries = 10
1212-local sep = "-----"
1111+local default_opts = {
1212+ max_entries = 10,
1313+ sep = "-----",
1414+ focus_gain_poll = false,
1515+ num_behavior = "prefix",
1616+ registers = {
1717+ yank_register = "+",
1818+ },
1919+ keymaps = {},
2020+}
13211422-- wrapper function for main plugin functionality
1523local function show_yank_bank(opts)
1624 -- Parse command arguments directly if args are provided as a string
1717- opts = opts or {}
2525+ opts = opts or default_opts
18261919- -- Fallback to defaults if necessary
2020- local max_entries_opt = opts.max_entries or max_entries
2121- local sep_opt = opts.sep or sep
2222- opts.keymaps = opts.keymaps or {}
2727+ -- initialize buffer and populate bank
2828+ local bufnr, display_lines, line_yank_map = menu.create_and_fill_buffer(
2929+ yanks,
3030+ reg_types,
3131+ opts.max_entries,
3232+ opts.sep
3333+ )
23342424- local bufnr, display_lines, line_yank_map =
2525- menu.create_and_fill_buffer(yanks, reg_types, max_entries_opt, sep_opt)
2635 -- handle empty bank case
2736 if not bufnr then
2837 return
2938 end
3939+4040+ -- open window and set keybinds
3041 local win_id = menu.open_window(bufnr, display_lines)
3142 menu.set_keymaps(win_id, bufnr, yanks, reg_types, line_yank_map, opts)
3243end
33443445-- plugin setup
3546function M.setup(opts)
3636- opts = opts or {}
3737-3838- -- parse opts
3939- max_entries = opts.max_entries or max_entries
4747+ opts = opts or default_opts
40484149 -- create clipboard autocmds
4250 clipboard.setup_yank_autocmd(yanks, reg_types, opts)