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 #6 from ptdewey/fix-issue-5

fix(#5 character-wise paste behavior)

authored by

Patrick Dewey and committed by
GitHub
6a8fb9c8 91af9a50

+20 -18
+6 -3
lua/yankbank/clipboard.lua
··· 2 2 local M = {} 3 3 4 4 -- Function to add yanked text to table 5 - function M.add_yank(yanks, text, max_entries) 5 + function M.add_yank(yanks, reg_types, text, reg_type, max_entries) 6 6 -- avoid adding empty strings 7 7 if text ~= "" and text ~= " " and text ~= "\n" then 8 8 -- do not update with duplicate values ··· 12 12 end 13 13 end 14 14 table.insert(yanks, 1, text) 15 + table.insert(reg_types, 1, reg_type) 15 16 if #yanks > max_entries then 16 17 table.remove(yanks) 18 + table.remove(reg_types) 17 19 end 18 20 end 19 21 end 20 22 21 23 -- autocommand to listen for yank events 22 - function M.setup_yank_autocmd(yanks, max_entries) 24 + function M.setup_yank_autocmd(yanks, reg_types, max_entries) 23 25 vim.api.nvim_create_autocmd("TextYankPost", { 24 26 callback = function() 25 27 -- TODO: this function can be expanded to incorporate other registers ··· 30 32 31 33 -- get register information 32 34 local rn = vim.v.event.regname 35 + local reg_type = vim.fn.getregtype('"') 33 36 34 37 -- check changes wwere made to default register 35 38 if vim.v.event.regname == "" then ··· 40 43 return 41 44 end 42 45 43 - M.add_yank(yanks, yanked_text, max_entries) 46 + M.add_yank(yanks, reg_types, yanked_text, reg_type, max_entries) 44 47 end 45 48 end, 46 49 })
+4 -7
lua/yankbank/helpers.lua
··· 42 42 vim.api.nvim_win_set_cursor(0, { 1, 0 }) 43 43 end 44 44 45 - -- customized paste function that functions more like 'p' 46 - function M.smart_paste(text) 45 + -- customized paste function that functions like 'p' 46 + function M.smart_paste(text, reg_type) 47 47 -- convert text string to string list 48 48 local lines = {} 49 49 for line in text:gmatch("([^\n]*)\n?") do 50 50 table.insert(lines, line) 51 51 end 52 52 53 - -- determine if the text should be treated as line-wise based on its ending 54 - local type = "c" 53 + -- remove last newline character to replicate base put behavior 55 54 if #lines > 1 then 56 - type = "l" 57 - -- remove last newline character to replicate base put behavior 58 55 table.remove(lines) 59 56 end 60 - vim.api.nvim_put(lines, type, true, true) 57 + vim.api.nvim_put(lines, reg_type, true, true) 61 58 end 62 59 63 60 return M
+4 -4
lua/yankbank/init.lua
··· 7 7 8 8 -- initialize yanks tables 9 9 local yanks = {} 10 + local reg_types = {} 10 11 local max_entries = 10 11 12 local sep = "-----" 12 13 ··· 21 22 opts.keymaps = opts.keymaps or {} 22 23 23 24 local bufnr, display_lines, line_yank_map = 24 - menu.create_and_fill_buffer(yanks, max_entries_opt, sep_opt) 25 + menu.create_and_fill_buffer(yanks, reg_types, max_entries_opt, sep_opt) 25 26 -- handle empty bank case 26 27 if not bufnr then 27 28 return 28 29 end 29 30 local win_id = menu.open_window(bufnr, display_lines) 30 - menu.set_keymaps(win_id, bufnr, yanks, line_yank_map, opts) 31 + menu.set_keymaps(win_id, bufnr, yanks, reg_types, line_yank_map, opts) 31 32 end 32 33 33 34 -- plugin setup ··· 36 37 37 38 -- parse opts 38 39 max_entries = opts.max_entries or max_entries 39 - -- sep = opts.sep or sep 40 40 41 41 -- create clipboard autocmds 42 - clipboard.setup_yank_autocmd(yanks, max_entries) 42 + clipboard.setup_yank_autocmd(yanks, reg_types, max_entries) 43 43 44 44 -- Create user command 45 45 vim.api.nvim_create_user_command("YankBank", function()
+6 -4
lua/yankbank/menu.lua
··· 7 7 local helpers = require("yankbank.helpers") 8 8 9 9 -- create new buffer and reformat yank table for ui 10 - function M.create_and_fill_buffer(yanks, max_entries, sep) 10 + function M.create_and_fill_buffer(yanks, reg_types, max_entries, sep) 11 11 -- check the content of the system clipboard register 12 12 -- TODO: this could be replaced with some sort of polling of the + register 13 13 local text = vim.fn.getreg("+") 14 14 local most_recent_yank = yanks[1] or "" 15 15 if text ~= most_recent_yank then 16 - clipboard.add_yank(yanks, text, max_entries) 16 + local reg_type = vim.fn.getregtype("+") 17 + clipboard.add_yank(yanks, reg_types, text, reg_type, max_entries) 17 18 end 18 19 19 20 -- stop if yank table is empty ··· 81 82 end 82 83 83 84 -- Set key mappings for the popup window 84 - function M.set_keymaps(win_id, bufnr, yanks, line_yank_map, opts) 85 + function M.set_keymaps(win_id, bufnr, yanks, reg_types, line_yank_map, opts) 85 86 -- Key mappings for selection and closing the popup 86 87 local map_opts = { noremap = true, silent = true, buffer = bufnr } 87 88 ··· 158 159 159 160 -- close window upon selection 160 161 vim.api.nvim_win_close(win_id, true) 161 - helpers.smart_paste(text) 162 + helpers.smart_paste(text, reg_types[yankIndex]) 162 163 else 163 164 print("Error: Invalid selection") 164 165 end ··· 171 172 if yankIndex then 172 173 local text = yanks[yankIndex] 173 174 -- NOTE: possibly change this to '"' if not using system clipboard 175 + -- - make this an option 174 176 vim.fn.setreg("+", text) 175 177 vim.api.nvim_win_close(win_id, true) 176 178 end