···11MIT License
2233-Copyright (c) 2024 Patrick
33+Copyright (c) 2024 Patrick Dewey
4455Permission is hereby granted, free of charge, to any person obtaining a copy
66of this software and associated documentation files (the "Software"), to deal
+7-2
lua/yankbank/clipboard.lua
···44-- Function to add yanked text to table
55function M.add_yank(yanks, text, max_entries)
66 -- avoid adding empty strings
77- if text ~= "" then
77+ if 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
814 table.insert(yanks, 1, text)
99-1015 if #yanks > max_entries then
1116 table.remove(yanks)
1217 end
+12-7
lua/yankbank/helpers.lua
···31313232-- customized paste function that functions more like 'p'
3333function M.smart_paste(text)
3434+ -- convert text string to string list
3535+ local lines = {}
3636+ for line in text:gmatch("([^\n]*)\n?") do
3737+ table.insert(lines, line)
3838+ end
3939+3440 -- determine if the text should be treated as line-wise based on its ending
3535- if text:sub(-1) == '\n' then
3636- -- line-wise
3737- vim.cmd("normal! o")
3838- vim.api.nvim_paste(text, false, -1)
3939- else
4040- -- character-wise
4141- vim.api.nvim_paste(text, false, -1)
4141+ local type = "c"
4242+ if #lines > 1 then
4343+ type = "l"
4444+ -- remove last newline character to replicate base put behavior
4545+ table.remove(lines)
4246 end
4747+ vim.api.nvim_put(lines, type, true, true)
4348end
44494550return M
+4
lua/yankbank/init.lua
···2828 local sep_opt = opts.sep or sep
29293030 local bufnr, display_lines, line_yank_map = menu.create_and_fill_buffer(yanks, max_entries_opt, sep_opt)
3131+ -- handle empty bank case
3232+ if not bufnr then
3333+ return
3434+ end
3135 local win_id = menu.open_window(bufnr, display_lines)
3236 menu.set_keymaps(win_id, bufnr, yanks, line_yank_map)
3337end
+23-8
lua/yankbank/menu.lua
···27272828 -- set buffer type same as current window for syntax highlighting
2929 local current_filetype = vim.bo.filetype
3030- vim.api.nvim_buf_set_option(bufnr, 'filetype', current_filetype)
3030+ vim.api.nvim_set_option_value("filetype", current_filetype, { buf = bufnr })
31313232 local display_lines, line_yank_map = data.get_display_lines(yanks, sep)
3333···4141function M.open_window(bufnr, display_lines)
4242 -- set maximum window width based on number of lines
4343 local max_width = 0
4444- for _, line in ipairs(display_lines) do
4545- max_width = math.max(max_width, #line)
4444+ if display_lines and #display_lines > 0 then
4545+ for _, line in ipairs(display_lines) do
4646+ max_width = math.max(max_width, #line)
4747+ end
4848+ else
4949+ max_width = vim.api.nvim_get_option_value("columns", {})
4650 end
47514852 -- define buffer window width and height based on number of columns
4949- local width = math.min(max_width + 4, vim.api.nvim_get_option("columns"))
5050- local height = math.min(#display_lines, vim.api.nvim_get_option("lines"))
5353+ local width = math.min(max_width + 4, vim.api.nvim_get_option_value("columns", {}))
5454+ local height = math.min(display_lines and #display_lines or 1, vim.api.nvim_get_option_value("lines", {}))
51555256 -- open window
5357 local win_id = vim.api.nvim_open_win(bufnr, true, {
5458 relative = "editor",
5559 width = width,
5660 height = height,
5757- col = math.floor((vim.api.nvim_get_option("columns") - width) / 2),
5858- row = math.floor((vim.api.nvim_get_option("lines") - height) / 2),
6161+ col = math.floor((vim.api.nvim_get_option_value("columns", {}) - width) / 2),
6262+ row = math.floor((vim.api.nvim_get_option_value("lines", {}) - height) / 2),
5963 border = "rounded",
6064 style = "minimal",
6165 })
62666367 -- Highlight current line
6464- vim.api.nvim_win_set_option(win_id, 'cursorline', true)
6868+ vim.api.nvim_set_option_value('cursorline', true, { win = win_id })
65696670 return win_id
6771end
···96100 helpers.smart_paste(text)
97101 else
98102 print("Error: Invalid selection")
103103+ end
104104+ end, { buffer = bufnr })
105105+106106+ -- bind yank behavior to y
107107+ vim.keymap.set('n', 'yy', function()
108108+ local cursor = vim.api.nvim_win_get_cursor(0)[1]
109109+ local yankIndex = line_yank_map[cursor]
110110+ if yankIndex then
111111+ local text = yanks[yankIndex]
112112+ vim.fn.setreg('"', text)
113113+ vim.api.nvim_win_close(win_id, true)
99114 end
100115 end, { buffer = bufnr })
101116