Skip to content

Commit

Permalink
Merge pull request #13 from ptdewey/feat-12-register-polling
Browse files Browse the repository at this point in the history
feat: added '+' register polling on focus gain
  • Loading branch information
ptdewey authored Jun 12, 2024
2 parents 4379fbe + 6408965 commit dfb53bc
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 16 deletions.
File renamed without changes.
31 changes: 31 additions & 0 deletions .github/workflows/pr_check.yaml
Original file line number Diff line number Diff line change
@@ -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
22 changes: 14 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -54,31 +52,41 @@ The setup function also supports taking in a table of options:
| keymaps.yank | string | `"yy"` |
| keymaps.close | table of strings | `{ "<Esc>", "<C-c>", "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.
Expand All @@ -94,10 +102,8 @@ vim.keymap.set("n", "<leader>y", "<cmd>YankBank<CR>", { 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

Expand Down
34 changes: 28 additions & 6 deletions lua/yankbank/clipboard.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -34,19 +36,39 @@ 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
2 changes: 1 addition & 1 deletion lua/yankbank/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
3 changes: 2 additions & 1 deletion lua/yankbank/menu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit dfb53bc

Please sign in to comment.