···55---@return table
66function M.get_entry(i)
77 return {
88- yank_text = YANKS[i],
99- reg_type = REG_TYPES[i],
88+ yank_text = YB_YANKS[i],
99+ reg_type = YB_REG_TYPES[i],
1010 }
1111end
1212···1414---@return table
1515function M.get_all()
1616 local out = {}
1717- for i, v in ipairs(YANKS) do
1717+ for i, v in ipairs(YB_YANKS) do
1818 table.insert(out, {
1919 yank_text = v,
2020- reg_type = REG_TYPES[i],
2020+ reg_type = YB_REG_TYPES[i],
2121 })
2222 end
2323 return out
···3333--- remove entry from yankbank by index
3434---@param i integer index to remove
3535function M.remove_entry(i)
3636- local yank_text = table.remove(YANKS, i)
3737- table.remove(REG_TYPES, i)
3838- if OPTS.persist_type == "sqlite" then
3636+ local yank_text = table.remove(YB_YANKS, i)
3737+ table.remove(YB_REG_TYPES, i)
3838+ if YB_OPTS.persist_type == "sqlite" then
3939 require("yankbank.persistence.sql").data().remove_match(yank_text)
4040 end
4141end
+9-9
lua/yankbank/clipboard.lua
···1313 end
14141515 -- check for duplicate values already inserted
1616- for i, entry in ipairs(YANKS) do
1616+ for i, entry in ipairs(YB_YANKS) do
1717 if entry == text then
1818 -- remove matched entry so it can be inserted at 1st position
1919- table.remove(YANKS, i)
2020- table.remove(REG_TYPES, i)
1919+ table.remove(YB_YANKS, i)
2020+ table.remove(YB_REG_TYPES, i)
2121 break
2222 end
2323 end
24242525 -- add entry to bank
2626- table.insert(YANKS, 1, text)
2727- table.insert(REG_TYPES, 1, reg_type)
2626+ table.insert(YB_YANKS, 1, text)
2727+ table.insert(YB_REG_TYPES, 1, reg_type)
28282929 -- trim table size if necessary
3030- if #YANKS > OPTS.max_entries then
3131- table.remove(YANKS)
3232- table.remove(REG_TYPES)
3030+ if #YB_YANKS > YB_OPTS.max_entries then
3131+ table.remove(YB_YANKS)
3232+ table.remove(YB_REG_TYPES)
3333 end
34343535 -- add entry to persistent store
···6262 })
63636464 -- poll registers when vim is focused (check for new clipboard activity)
6565- if OPTS.focus_gain_poll == true then
6565+ if YB_OPTS.focus_gain_poll == true then
6666 vim.api.nvim_create_autocmd("FocusGained", {
6767 callback = function()
6868 -- get register information
+5-5
lua/yankbank/data.lua
···88 local yank_num = 0
991010 -- calculate the maximum width needed for the yank numbers
1111- local max_digits = #tostring(#YANKS)
1111+ local max_digits = #tostring(#YB_YANKS)
12121313 -- assumes yanks is table of strings
1414- for i, yank in ipairs(YANKS) do
1414+ for i, yank in ipairs(YB_YANKS) do
1515 yank_num = yank_num + 1
16161717 local yank_lines = yank
···4848 table.insert(line_yank_map, i)
4949 end
50505151- if i < #YANKS then
5151+ if i < #YB_YANKS then
5252 -- Add a visual separator between yanks, aligned with the yank content
5353- if OPTS.sep ~= "" then
5353+ if YB_OPTS.sep ~= "" then
5454 table.insert(
5555 display_lines,
5656- string.rep(" ", max_digits + 2) .. OPTS.sep
5656+ string.rep(" ", max_digits + 2) .. YB_OPTS.sep
5757 )
5858 end
5959 table.insert(line_yank_map, false)
+4-3
lua/yankbank/helpers.lua
···4343 vim.api.nvim_win_set_cursor(0, { 1, 0 })
4444end
45454646---- customized paste function that functions like 'p'
4646+--- customized paste function that functions like 'p' or 'P'
4747---@param text string|table
4848---@param reg_type string
4949-function M.smart_paste(text, reg_type)
4949+---@param after boolean define if text should be pasted after 'p' or before 'P'
5050+function M.smart_paste(text, reg_type, after)
5051 local lines = {}
5152 if type(text) == "string" then
5253 -- convert text string to string list
···6162 lines = text
6263 end
63646464- vim.api.nvim_put(lines, reg_type, true, true)
6565+ vim.api.nvim_put(lines, reg_type, after, true)
6566end
66676768return M
+8-7
lua/yankbank/init.lua
···11local M = {}
2233+-- define global variables
44+YB_YANKS = {}
55+YB_REG_TYPES = {}
66+YB_OPTS = {}
77+38-- local imports
49local menu = require("yankbank.menu")
510local clipboard = require("yankbank.clipboard")
611local persistence = require("yankbank.persistence")
71288-YANKS = {}
99-REG_TYPES = {}
1010-OPTS = {}
1111-1213-- default plugin options
1314local default_opts = {
1415 max_entries = 10,
···24252526--- wrapper function for main plugin functionality
2627local function show_yank_bank()
2727- YANKS = persistence.get_yanks() or YANKS
2828+ YB_YANKS = persistence.get_yanks() or YB_YANKS
28292930 -- initialize buffer and populate bank
3031 local buf_data = menu.create_and_fill_buffer()
···4344---@param opts? table
4445function M.setup(opts)
4546 -- merge opts with default options table
4646- OPTS = vim.tbl_deep_extend("keep", opts or {}, default_opts)
4747+ YB_OPTS = vim.tbl_deep_extend("keep", opts or {}, default_opts)
47484849 -- enable persistence based on opts (needs to be called before autocmd setup)
4949- YANKS, REG_TYPES = persistence.setup()
5050+ YB_YANKS, YB_REG_TYPES = persistence.setup()
50515152 -- create clipboard autocmds
5253 clipboard.setup_yank_autocmd()
+44-26
lua/yankbank/menu.lua
···88 navigation_next = "j",
99 navigation_prev = "k",
1010 paste = "<CR>",
1111+ paste_back = "P",
1112 yank = "yy",
1213 close = { "<Esc>", "<C-c>", "q" },
1314}
···1718 yank_register = "+",
1819}
19202121+-- merge default and options keymap tables
2222+local k = vim.tbl_deep_extend("force", default_keymaps, YB_OPTS.keymaps or {})
2323+2424+-- merge default and options register tables
2525+YB_OPTS.registers =
2626+ vim.tbl_deep_extend("force", default_registers, YB_OPTS.registers or {})
2727+2828+-- check table for number behavior option (prefix or jump, default to prefix)
2929+YB_OPTS.num_behavior = YB_OPTS.num_behavior or "prefix"
3030+2031--- Container class for YankBank buffer related variables
2132---@class YankBankBufData
2233---@field bufnr integer
···2839---@return YankBankBufData?
2940function M.create_and_fill_buffer()
3041 -- stop if yanks or register types table is empty
3131- if #YANKS == 0 or #REG_TYPES == 0 then
4242+ if #YB_YANKS == 0 or #YB_REG_TYPES == 0 then
3243 print("No yanks to show.")
3344 return nil
3445 end
···105116 -- key mappings for selection and closing the popup
106117 local map_opts = { noremap = true, silent = true, buffer = b.bufnr }
107118108108- -- merge default and options keymap tables
109109- local k = vim.tbl_deep_extend("force", default_keymaps, OPTS.keymaps or {})
110110-111111- -- merge default and options keymap tables
112112- OPTS.registers =
113113- vim.tbl_deep_extend("force", default_registers, OPTS.registers or {})
114114-115115- -- check table for number behavior option (prefix or jump, default to prefix)
116116- OPTS.num_behavior = OPTS.num_behavior or "prefix"
117117-118119 -- popup buffer navigation binds
119119- if OPTS.num_behavior == "prefix" then
120120+ if YB_OPTS.num_behavior == "prefix" then
120121 vim.keymap.set("n", k.navigation_next, function()
121122 local count = vim.v.count1 > 0 and vim.v.count1 or 1
122123 helpers.next_numbered_item(count)
···126127 local count = vim.v.count1 > 0 and vim.v.count1 or 1
127128 helpers.prev_numbered_item(count)
128129 return ""
129129- end, { noremap = true, silent = true, buffer = b.bufnr })
130130+ end, map_opts)
130131 else
131132 vim.keymap.set(
132133 "n",
133134 k.navigation_next,
134135 helpers.next_numbered_item,
135135- { noremap = true, silent = true, buffer = b.bufnr }
136136+ map_opts
136137 )
137138 vim.keymap.set(
138139 "n",
139140 k.navigation_prev,
140141 helpers.prev_numbered_item,
141141- { noremap = true, silent = true, buffer = b.bufnr }
142142+ map_opts
142143 )
143144 end
144145145145- -- Map number keys to jump to entry if num_behavior is 'jump'
146146- if OPTS.num_behavior == "jump" then
147147- for i = 1, OPTS.max_entries do
146146+ -- map number keys to jump to entry if num_behavior is 'jump'
147147+ if YB_OPTS.num_behavior == "jump" then
148148+ for i = 1, YB_OPTS.max_entries do
148149 vim.keymap.set("n", tostring(i), function()
149150 local target_line = nil
150151 for line_num, yank_num in pairs(b.line_yank_map) do
···166167 -- use the mapping to find the original yank
167168 local yankIndex = b.line_yank_map[cursor]
168169 if yankIndex then
169169- -- retrieve the full yank, including all lines
170170- local text = YANKS[yankIndex]
171171-170170+ -- close window upon selection
171171+ vim.api.nvim_win_close(b.win_id, true)
172172+ helpers.smart_paste(
173173+ YB_YANKS[yankIndex],
174174+ YB_REG_TYPES[yankIndex],
175175+ true
176176+ )
177177+ else
178178+ print("Error: Invalid selection")
179179+ end
180180+ end, map_opts)
181181+ -- paste backwards
182182+ vim.keymap.set("n", k.paste_back, function()
183183+ local cursor = vim.api.nvim_win_get_cursor(b.win_id)[1]
184184+ -- use the mapping to find the original yank
185185+ local yankIndex = b.line_yank_map[cursor]
186186+ if yankIndex then
172187 -- close window upon selection
173188 vim.api.nvim_win_close(b.win_id, true)
174174- helpers.smart_paste(text, REG_TYPES[yankIndex])
189189+ helpers.smart_paste(
190190+ YB_YANKS[yankIndex],
191191+ YB_REG_TYPES[yankIndex],
192192+ false
193193+ )
175194 else
176195 print("Error: Invalid selection")
177196 end
178178- end, { buffer = b.bufnr })
197197+ end, map_opts)
179198180199 -- bind yank behavior
181200 vim.keymap.set("n", k.yank, function()
182201 local cursor = vim.api.nvim_win_get_cursor(b.win_id)[1]
183202 local yankIndex = b.line_yank_map[cursor]
184203 if yankIndex then
185185- local text = YANKS[yankIndex]
186186- vim.fn.setreg(OPTS.registers.yank_register, text)
204204+ vim.fn.setreg(YB_OPTS.registers.yank_register, YB_YANKS[yankIndex])
187205 vim.api.nvim_win_close(b.win_id, true)
188206 end
189189- end, { buffer = b.bufnr })
207207+ end, map_opts)
190208191209 -- close popup keybinds
192210 -- REFACTOR: check if close keybind is string, handle differently
+4-4
lua/yankbank/persistence.lua
···66---@param entry string
77---@param reg_type string
88function M.add_entry(entry, reg_type)
99- if OPTS.persist_type == "sqlite" then
99+ if YB_OPTS.persist_type == "sqlite" then
1010 persistence:insert_yank(entry, reg_type)
1111 end
1212end
13131414--- get current state of yanks in persistent storage
1515function M.get_yanks()
1616- if OPTS.persist_type == "sqlite" then
1616+ if YB_OPTS.persist_type == "sqlite" then
1717 return persistence:get_bank()
1818 end
1919end
···2222---@return table
2323---@return table
2424function M.setup()
2525- if not OPTS.persist_type then
2525+ if not YB_OPTS.persist_type then
2626 return {}, {}
2727- elseif OPTS.persist_type == "sqlite" then
2727+ elseif YB_OPTS.persist_type == "sqlite" then
2828 persistence = require("yankbank.persistence.sql").setup()
2929 return persistence:get_bank()
3030 else
+4-4
lua/yankbank/persistence/sql.lua
···102102--- set up database persistence
103103---@return sqlite_tbl data
104104function M.setup()
105105- max_entries = OPTS.max_entries
105105+ max_entries = YB_OPTS.max_entries
106106107107 vim.api.nvim_create_user_command("YankBankClearDB", function()
108108 data:remove()
109109- YANKS = {}
110110- REG_TYPES = {}
109109+ YB_YANKS = {}
110110+ YB_REG_TYPES = {}
111111 end, {})
112112113113- if OPTS.debug == true then
113113+ if YB_OPTS.debug == true then
114114 vim.api.nvim_create_user_command("YankBankViewDB", function()
115115 print(vim.inspect(data:get()))
116116 end, {})