Neovim plugin improving access to clipboard history (mirror)

feat(added yank keybind to popup and improved put handling)

+47 -18
+1 -1
LICENSE
··· 1 1 MIT License 2 2 3 - Copyright (c) 2024 Patrick 3 + Copyright (c) 2024 Patrick Dewey 4 4 5 5 Permission is hereby granted, free of charge, to any person obtaining a copy 6 6 of this software and associated documentation files (the "Software"), to deal
+7 -2
lua/yankbank/clipboard.lua
··· 4 4 -- Function to add yanked text to table 5 5 function M.add_yank(yanks, text, max_entries) 6 6 -- avoid adding empty strings 7 - if text ~= "" then 7 + if 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 8 14 table.insert(yanks, 1, text) 9 - 10 15 if #yanks > max_entries then 11 16 table.remove(yanks) 12 17 end
+12 -7
lua/yankbank/helpers.lua
··· 31 31 32 32 -- customized paste function that functions more like 'p' 33 33 function M.smart_paste(text) 34 + -- convert text string to string list 35 + local lines = {} 36 + for line in text:gmatch("([^\n]*)\n?") do 37 + table.insert(lines, line) 38 + end 39 + 34 40 -- determine if the text should be treated as line-wise based on its ending 35 - if text:sub(-1) == '\n' then 36 - -- line-wise 37 - vim.cmd("normal! o") 38 - vim.api.nvim_paste(text, false, -1) 39 - else 40 - -- character-wise 41 - vim.api.nvim_paste(text, false, -1) 41 + local type = "c" 42 + if #lines > 1 then 43 + type = "l" 44 + -- remove last newline character to replicate base put behavior 45 + table.remove(lines) 42 46 end 47 + vim.api.nvim_put(lines, type, true, true) 43 48 end 44 49 45 50 return M
+4
lua/yankbank/init.lua
··· 28 28 local sep_opt = opts.sep or sep 29 29 30 30 local bufnr, display_lines, line_yank_map = menu.create_and_fill_buffer(yanks, max_entries_opt, sep_opt) 31 + -- handle empty bank case 32 + if not bufnr then 33 + return 34 + end 31 35 local win_id = menu.open_window(bufnr, display_lines) 32 36 menu.set_keymaps(win_id, bufnr, yanks, line_yank_map) 33 37 end
+23 -8
lua/yankbank/menu.lua
··· 27 27 28 28 -- set buffer type same as current window for syntax highlighting 29 29 local current_filetype = vim.bo.filetype 30 - vim.api.nvim_buf_set_option(bufnr, 'filetype', current_filetype) 30 + vim.api.nvim_set_option_value("filetype", current_filetype, { buf = bufnr }) 31 31 32 32 local display_lines, line_yank_map = data.get_display_lines(yanks, sep) 33 33 ··· 41 41 function M.open_window(bufnr, display_lines) 42 42 -- set maximum window width based on number of lines 43 43 local max_width = 0 44 - for _, line in ipairs(display_lines) do 45 - max_width = math.max(max_width, #line) 44 + if display_lines and #display_lines > 0 then 45 + for _, line in ipairs(display_lines) do 46 + max_width = math.max(max_width, #line) 47 + end 48 + else 49 + max_width = vim.api.nvim_get_option_value("columns", {}) 46 50 end 47 51 48 52 -- define buffer window width and height based on number of columns 49 - local width = math.min(max_width + 4, vim.api.nvim_get_option("columns")) 50 - local height = math.min(#display_lines, vim.api.nvim_get_option("lines")) 53 + local width = math.min(max_width + 4, vim.api.nvim_get_option_value("columns", {})) 54 + local height = math.min(display_lines and #display_lines or 1, vim.api.nvim_get_option_value("lines", {})) 51 55 52 56 -- open window 53 57 local win_id = vim.api.nvim_open_win(bufnr, true, { 54 58 relative = "editor", 55 59 width = width, 56 60 height = height, 57 - col = math.floor((vim.api.nvim_get_option("columns") - width) / 2), 58 - row = math.floor((vim.api.nvim_get_option("lines") - height) / 2), 61 + col = math.floor((vim.api.nvim_get_option_value("columns", {}) - width) / 2), 62 + row = math.floor((vim.api.nvim_get_option_value("lines", {}) - height) / 2), 59 63 border = "rounded", 60 64 style = "minimal", 61 65 }) 62 66 63 67 -- Highlight current line 64 - vim.api.nvim_win_set_option(win_id, 'cursorline', true) 68 + vim.api.nvim_set_option_value('cursorline', true, { win = win_id }) 65 69 66 70 return win_id 67 71 end ··· 96 100 helpers.smart_paste(text) 97 101 else 98 102 print("Error: Invalid selection") 103 + end 104 + end, { buffer = bufnr }) 105 + 106 + -- bind yank behavior to y 107 + vim.keymap.set('n', 'yy', function() 108 + local cursor = vim.api.nvim_win_get_cursor(0)[1] 109 + local yankIndex = line_yank_map[cursor] 110 + if yankIndex then 111 + local text = yanks[yankIndex] 112 + vim.fn.setreg('"', text) 113 + vim.api.nvim_win_close(win_id, true) 99 114 end 100 115 end, { buffer = bufnr }) 101 116