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 #13 from ptdewey/feat-12-register-polling

feat: added '+' register polling on focus gain

authored by

Patrick Dewey and committed by
GitHub
dfb53bc9 4379fbe4

+76 -16
.github/workflows/docs.yml .github/workflows/docs.yaml
+31
.github/workflows/pr_check.yaml
··· 1 + name: Run formatter and linter 2 + 3 + on: 4 + pull_request: 5 + 6 + jobs: 7 + stylua: 8 + name: Stylua 9 + runs-on: ubuntu-22.04 10 + steps: 11 + - uses: actions/checkout@v3 12 + - uses: JohnnyMorganz/stylua-action@v3 13 + with: 14 + token: ${{ secrets.GITHUB_TOKEN }} 15 + version: latest 16 + args: --color always --check . 17 + 18 + luacheck: 19 + name: Luacheck 20 + runs-on: ubuntu-22.04 21 + steps: 22 + - uses: actions/checkout@v3 23 + 24 + - name: Prepare 25 + run: | 26 + sudo apt-get update 27 + sudo apt-get install -y luarocks 28 + sudo luarocks install luacheck 29 + 30 + - name: Lint 31 + run: sudo make lint
+14 -8
README.md
··· 5 5 ## What it Does 6 6 7 7 YankBank stores the N recent yanks into the unnamed register ("), then populates a popup window with these recent yanks, allowing for quick access to recent yank history. 8 - Upon opening the popup menu, the current contents of the unnamedplus (+) register are also added to the menu (if they are different than the current contents of the unnamed register). 8 + Upon opening the popup menu, the current contents of the unnamedplus (+) register are also added to the menu (if they are different from the current contents of the unnamed register). 9 9 10 10 Choosing an entry from the menu (by hitting enter) will paste it into the currently open buffer at the cursor position. 11 11 12 12 ### Screenshots 13 - 14 - ![YankBank popup window](assets/screenshot-1.png) 15 13 16 14 ![YankBank popup window zoomed](assets/screenshot-2.png) 17 15 ··· 54 52 | keymaps.yank | string | `"yy"` | 55 53 | keymaps.close | table of strings | `{ "<Esc>", "<C-c>", "q" }` | 56 54 | num_behavior | string defining jump behavior "prefix" or "jump" | `"prefix"` | 55 + | focus_gain_poll | boolean | `nil` | 57 56 | registers | table container for register overrides | `{ }` | 58 57 | registers.yank_register | default register to yank from popup to | `"+"` | 59 58 60 59 61 - If no separator is desired, pass in an empty string for sep: 60 + #### Example Configuration 61 + 62 62 ```lua 63 63 config = function() 64 64 require('yankbank').setup({ 65 - max_entries = 12, 65 + max_entries = 9, 66 66 sep = "", 67 + num_behavior = "prefix", 68 + focus_gain_poll = true, 67 69 keymaps = { 68 70 navigation_next = "j", 69 71 navigation_prev = "k", 70 72 }, 71 - num_behavior = "prefix", 73 + registers = { 74 + yank_register = "+", 75 + }, 72 76 }) 73 77 end, 74 78 ``` 75 79 80 + If no separator is desired, pass in an empty string for `sep` 81 + 76 82 The 'num_behavior' option defines in-popup navigation behavior when hitting number keys. 77 83 - `num_behavior = "prefix"` works similar to traditional vim navigation with '3j' moving down 3 entries in the bank. 78 84 - `num_behavior = "jump"` jumps to entry matching the pressed number key (i.e. '3' jumps to entry 3) 79 85 - Note: If 'max_entries' is a two-digit number, there will be a delay upon pressing numbers that prefix a valid entry. 80 86 87 + 88 + The 'focus_gain_poll' option allows for enabling an additional autocommand that watches for focus gains (refocusing Neovim window), and checks for changes in the unnamedplus ('+') register, adding to yankbank when new contents are found. This allows for automatically adding text copied from other sources (like a browser) to the yankbank without the bank opening trigger. Off by default, but I highly recommend enabling it (`focus_gain_poll = true`) 81 89 82 90 ## Usage 83 91 ··· 94 102 ## Potential Improvements 95 103 96 104 - Persistence between sessions (through either sqlite database or just a file) 97 - - Polling on unnamedplus register to populate bank in more intuitive manner (could be enabled as option) 98 105 - nvim-cmp integration 99 106 - fzf integration 100 - - Setup options configuring which registers are included 101 107 102 108 ## Alternatives 103 109
+28 -6
lua/yankbank/clipboard.lua
··· 20 20 end 21 21 end 22 22 23 - -- autocommand to listen for yank events 24 - function M.setup_yank_autocmd(yanks, reg_types, max_entries) 23 + -- set up plugin autocommands 24 + -- TODO: make augroup 25 + function M.setup_yank_autocmd(yanks, reg_types, opts) 26 + -- autocommand to listen for yank events 25 27 vim.api.nvim_create_autocmd("TextYankPost", { 26 28 callback = function() 27 29 -- TODO: this function can be expanded to incorporate other registers ··· 34 36 local rn = vim.v.event.regname 35 37 local reg_type = vim.fn.getregtype('"') 36 38 37 - -- check changes wwere made to default register 39 + -- check if changes were made to default register 38 40 if vim.v.event.regname == "" then 39 - local yanked_text = vim.fn.getreg(rn) 41 + local yank = vim.fn.getreg(rn) 40 42 41 43 -- NOTE: this only blocks adding to list if something else is in plus register 42 - if string.len(yanked_text) <= 1 then 44 + if string.len(yank) <= 1 then 43 45 return 44 46 end 45 47 46 - M.add_yank(yanks, reg_types, yanked_text, reg_type, max_entries) 48 + M.add_yank(yanks, reg_types, yank, reg_type, opts.max_entries) 47 49 end 48 50 end, 49 51 }) 52 + 53 + -- poll registers when vim is focused (check for new clipboard activity) 54 + if opts.focus_gain_poll and opts.focus_gain_poll == true then 55 + vim.api.nvim_create_autocmd("FocusGained", { 56 + callback = function() 57 + -- get register information 58 + local reg_type = vim.fn.getregtype("+") 59 + local yank = vim.fn.getreg("+") 60 + 61 + -- NOTE: do not add yanks of 1 or 0 characters 62 + if string.len(yank) <= 1 then 63 + return 64 + end 65 + 66 + M.add_yank(yanks, reg_types, yank, reg_type, opts.max_entries) 67 + end, 68 + }) 69 + end 70 + 71 + -- TODO: focus lost hook? 50 72 end 51 73 52 74 return M
+1 -1
lua/yankbank/init.lua
··· 39 39 max_entries = opts.max_entries or max_entries 40 40 41 41 -- create clipboard autocmds 42 - clipboard.setup_yank_autocmd(yanks, reg_types, max_entries) 42 + clipboard.setup_yank_autocmd(yanks, reg_types, opts) 43 43 44 44 -- Create user command 45 45 vim.api.nvim_create_user_command("YankBank", function()
+2 -1
lua/yankbank/menu.lua
··· 104 104 local k = vim.tbl_deep_extend("force", default_keymaps, opts.keymaps or {}) 105 105 106 106 -- merge default and options keymap tables 107 - opts.registers = vim.tbl_deep_extend("force", default_registers, opts.registers or {}) 107 + opts.registers = 108 + vim.tbl_deep_extend("force", default_registers, opts.registers or {}) 108 109 109 110 -- check table for number behavior option (prefix or jump, default to prefix) 110 111 opts.num_behavior = opts.num_behavior or "prefix"