diff options
author | Josh Rahm <joshuarahm@gmail.com> | 2025-04-10 00:20:28 -0600 |
---|---|---|
committer | Josh Rahm <joshuarahm@gmail.com> | 2025-04-10 00:20:28 -0600 |
commit | d2325e040e2a4c2ea9d0d8c57af57bc13f7aeae7 (patch) | |
tree | 42cdc551faf3b3abbeb998166f00f937747fa6b1 /lua/vim/userregs/impl/visualselection.lua | |
parent | b3b09ff479510abc51ffb88743bb353bcc15e165 (diff) | |
download | rneovim-userregs-d2325e040e2a4c2ea9d0d8c57af57bc13f7aeae7.tar.gz rneovim-userregs-d2325e040e2a4c2ea9d0d8c57af57bc13f7aeae7.tar.bz2 rneovim-userregs-d2325e040e2a4c2ea9d0d8c57af57bc13f7aeae7.zip |
"| - file contents register
" - visual selection register
" - timestamp register
" - uuid register
" - dirname register
Diffstat (limited to 'lua/vim/userregs/impl/visualselection.lua')
-rw-r--r-- | lua/vim/userregs/impl/visualselection.lua | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/lua/vim/userregs/impl/visualselection.lua b/lua/vim/userregs/impl/visualselection.lua new file mode 100644 index 0000000..93cb370 --- /dev/null +++ b/lua/vim/userregs/impl/visualselection.lua @@ -0,0 +1,51 @@ +local userregs = require("vim.userregs") + +-- ASCII-safe register name +local regname = "\22" + +-- Return the contents of the last visual selection +local function read_last_visual() + local bufnr = vim.api.nvim_get_current_buf() + + local start_pos = vim.fn.getpos("'<") + local end_pos = vim.fn.getpos("'>") + + if not start_pos or not end_pos then + vim.notify("[userregs] No visual selection", vim.log.levels.WARN) + return { lines = {}, type = "V" } + end + + local start_lnum = start_pos[2] - 1 -- 0-indexed + local end_lnum = end_pos[2] -- end line is exclusive + + local lines = vim.api.nvim_buf_get_lines(bufnr, start_lnum, end_lnum, false) + return { + lines = lines, + type = "V", -- Assume linewise for now + additional_data = {}, + } +end + +-- Replace the last visual selection with new content +local function write_last_visual(_, content) + local bufnr = vim.api.nvim_get_current_buf() + + local start_pos = vim.fn.getpos("'<") + local end_pos = vim.fn.getpos("'>") + + if not start_pos or not end_pos then + vim.notify("[userregs] No visual selection to write into", vim.log.levels.ERROR) + return + end + + local start_lnum = start_pos[2] - 1 + local end_lnum = end_pos[2] + + -- Replace the range with the new content + vim.api.nvim_buf_set_lines(bufnr, start_lnum, end_lnum, false, content.lines or {}) +end + +userregs.register_handler(regname, { + put = function() return read_last_visual() end, + yank = write_last_visual, +}) |