From 869c6c64264d4cd1f45e0bbafd7d92238a93886f Mon Sep 17 00:00:00 2001 From: ptdewey Date: Tue, 11 Jun 2024 21:57:04 -0400 Subject: [PATCH 1/2] feat: optional '+' register polling on focus gain --- .github/workflows/{docs.yml => docs.yaml} | 0 .github/workflows/pr_check.yaml | 31 ++++++++++++++++++++ README.md | 22 ++++++++------ lua/yankbank/clipboard.lua | 35 +++++++++++++++++++---- lua/yankbank/init.lua | 2 +- 5 files changed, 75 insertions(+), 15 deletions(-) rename .github/workflows/{docs.yml => docs.yaml} (100%) create mode 100644 .github/workflows/pr_check.yaml diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yaml similarity index 100% rename from .github/workflows/docs.yml rename to .github/workflows/docs.yaml diff --git a/.github/workflows/pr_check.yaml b/.github/workflows/pr_check.yaml new file mode 100644 index 0000000..92ebfd8 --- /dev/null +++ b/.github/workflows/pr_check.yaml @@ -0,0 +1,31 @@ +name: Run formatter and linter + +on: + pull_request: + +jobs: + stylua: + name: Stylua + runs-on: ubuntu-22.04 + steps: + - uses: actions/checkout@v3 + - uses: JohnnyMorganz/stylua-action@v3 + with: + token: ${{ secrets.GITHUB_TOKEN }} + version: latest + args: --color always --check . + + luacheck: + name: Luacheck + runs-on: ubuntu-22.04 + steps: + - uses: actions/checkout@v3 + + - name: Prepare + run: | + sudo apt-get update + sudo apt-get install -y luarocks + sudo luarocks install luacheck + + - name: Lint + run: sudo make lint diff --git a/README.md b/README.md index 3a526d9..212e8ac 100644 --- a/README.md +++ b/README.md @@ -5,14 +5,12 @@ A Neovim plugin for keeping track of more recent yanks and deletions and exposin ## What it Does 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. -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). +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). Choosing an entry from the menu (by hitting enter) will paste it into the currently open buffer at the cursor position. ### Screenshots -![YankBank popup window](assets/screenshot-1.png) - ![YankBank popup window zoomed](assets/screenshot-2.png) The menu is specific to the current session, and will only contain the contents of the current unnamedplus register upon opening in a completely new session. @@ -54,31 +52,41 @@ The setup function also supports taking in a table of options: | keymaps.yank | string | `"yy"` | | keymaps.close | table of strings | `{ "", "", "q" }` | | num_behavior | string defining jump behavior "prefix" or "jump" | `"prefix"` | +| focus_gain_poll | boolean | `nil` | | registers | table container for register overrides | `{ }` | | registers.yank_register | default register to yank from popup to | `"+"` | -If no separator is desired, pass in an empty string for sep: +#### Example Configuration + ```lua config = function() require('yankbank').setup({ - max_entries = 12, + max_entries = 9, sep = "", + num_behavior = "prefix", + focus_gain_poll = true, keymaps = { navigation_next = "j", navigation_prev = "k", }, - num_behavior = "prefix", + registers = { + yank_register = "+", + }, }) end, ``` +If no separator is desired, pass in an empty string for `sep` + The 'num_behavior' option defines in-popup navigation behavior when hitting number keys. - `num_behavior = "prefix"` works similar to traditional vim navigation with '3j' moving down 3 entries in the bank. - `num_behavior = "jump"` jumps to entry matching the pressed number key (i.e. '3' jumps to entry 3) - Note: If 'max_entries' is a two-digit number, there will be a delay upon pressing numbers that prefix a valid entry. +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`) + ## Usage The popup menu can be opened with the command:`:YankBank`, an entry is pasted at the current cursor position by hitting enter, and the menu can be closed by hitting escape, ctrl-c, or q. @@ -94,10 +102,8 @@ vim.keymap.set("n", "y", "YankBank", { noremap = true }) ## Potential Improvements - Persistence between sessions (through either sqlite database or just a file) -- Polling on unnamedplus register to populate bank in more intuitive manner (could be enabled as option) - nvim-cmp integration - fzf integration -- Setup options configuring which registers are included ## Alternatives diff --git a/lua/yankbank/clipboard.lua b/lua/yankbank/clipboard.lua index 9dbd21b..ecf6e87 100644 --- a/lua/yankbank/clipboard.lua +++ b/lua/yankbank/clipboard.lua @@ -20,8 +20,10 @@ function M.add_yank(yanks, reg_types, text, reg_type, max_entries) end end --- autocommand to listen for yank events -function M.setup_yank_autocmd(yanks, reg_types, max_entries) +-- set up plugin autocommands +-- TODO: make augroup +function M.setup_yank_autocmd(yanks, reg_types, opts) + -- autocommand to listen for yank events vim.api.nvim_create_autocmd("TextYankPost", { callback = function() -- TODO: this function can be expanded to incorporate other registers @@ -34,19 +36,40 @@ function M.setup_yank_autocmd(yanks, reg_types, max_entries) local rn = vim.v.event.regname local reg_type = vim.fn.getregtype('"') - -- check changes wwere made to default register + -- check if changes were made to default register if vim.v.event.regname == "" then - local yanked_text = vim.fn.getreg(rn) + local yank = vim.fn.getreg(rn) -- NOTE: this only blocks adding to list if something else is in plus register - if string.len(yanked_text) <= 1 then + if string.len(yank) <= 1 then return end - M.add_yank(yanks, reg_types, yanked_text, reg_type, max_entries) + M.add_yank(yanks, reg_types, yank, reg_type, opts.max_entries) end end, }) + + -- poll registers when vim is focused (check for new clipboard activity) + if opts.focus_gain_poll and opts.focus_gain_poll == true then + vim.api.nvim_create_autocmd("FocusGained", { + callback = function() + + -- get register information + local reg_type = vim.fn.getregtype('+') + local yank = vim.fn.getreg('+') + + -- NOTE: do not add yanks of 1 or 0 characters + if string.len(yank) <= 1 then + return + end + + M.add_yank(yanks, reg_types, yank, reg_type, opts.max_entries) + end + }) + end + + -- TODO: focus lost hook? end return M diff --git a/lua/yankbank/init.lua b/lua/yankbank/init.lua index f97bb21..b5f2812 100644 --- a/lua/yankbank/init.lua +++ b/lua/yankbank/init.lua @@ -39,7 +39,7 @@ function M.setup(opts) max_entries = opts.max_entries or max_entries -- create clipboard autocmds - clipboard.setup_yank_autocmd(yanks, reg_types, max_entries) + clipboard.setup_yank_autocmd(yanks, reg_types, opts) -- Create user command vim.api.nvim_create_user_command("YankBank", function() From 64089657c15268489c83c3294998b5bb78ac01c6 Mon Sep 17 00:00:00 2001 From: ptdewey Date: Tue, 11 Jun 2024 21:58:28 -0400 Subject: [PATCH 2/2] feat: optional '+' register polling on focus gain --- lua/yankbank/clipboard.lua | 7 +++---- lua/yankbank/menu.lua | 3 ++- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lua/yankbank/clipboard.lua b/lua/yankbank/clipboard.lua index ecf6e87..765fc07 100644 --- a/lua/yankbank/clipboard.lua +++ b/lua/yankbank/clipboard.lua @@ -54,10 +54,9 @@ function M.setup_yank_autocmd(yanks, reg_types, opts) if opts.focus_gain_poll and opts.focus_gain_poll == true then vim.api.nvim_create_autocmd("FocusGained", { callback = function() - -- get register information - local reg_type = vim.fn.getregtype('+') - local yank = vim.fn.getreg('+') + local reg_type = vim.fn.getregtype("+") + local yank = vim.fn.getreg("+") -- NOTE: do not add yanks of 1 or 0 characters if string.len(yank) <= 1 then @@ -65,7 +64,7 @@ function M.setup_yank_autocmd(yanks, reg_types, opts) end M.add_yank(yanks, reg_types, yank, reg_type, opts.max_entries) - end + end, }) end diff --git a/lua/yankbank/menu.lua b/lua/yankbank/menu.lua index d3e0324..19316a1 100644 --- a/lua/yankbank/menu.lua +++ b/lua/yankbank/menu.lua @@ -104,7 +104,8 @@ function M.set_keymaps(win_id, bufnr, yanks, reg_types, line_yank_map, opts) local k = vim.tbl_deep_extend("force", default_keymaps, opts.keymaps or {}) -- merge default and options keymap tables - opts.registers = vim.tbl_deep_extend("force", default_registers, opts.registers or {}) + opts.registers = + vim.tbl_deep_extend("force", default_registers, opts.registers or {}) -- check table for number behavior option (prefix or jump, default to prefix) opts.num_behavior = opts.num_behavior or "prefix"