summaryrefslogtreecommitdiff
path: root/lua/vim/userregs/impl/buffercontents.lua
diff options
context:
space:
mode:
authorJosh Rahm <joshuarahm@gmail.com>2025-04-10 00:20:28 -0600
committerJosh Rahm <joshuarahm@gmail.com>2025-04-10 00:20:28 -0600
commitd2325e040e2a4c2ea9d0d8c57af57bc13f7aeae7 (patch)
tree42cdc551faf3b3abbeb998166f00f937747fa6b1 /lua/vim/userregs/impl/buffercontents.lua
parentb3b09ff479510abc51ffb88743bb353bcc15e165 (diff)
downloadrneovim-userregs-d2325e040e2a4c2ea9d0d8c57af57bc13f7aeae7.tar.gz
rneovim-userregs-d2325e040e2a4c2ea9d0d8c57af57bc13f7aeae7.tar.bz2
rneovim-userregs-d2325e040e2a4c2ea9d0d8c57af57bc13f7aeae7.zip
Add some more user-defined registers:HEADmain
"| - file contents register " - visual selection register " - timestamp register " - uuid register " - dirname register
Diffstat (limited to 'lua/vim/userregs/impl/buffercontents.lua')
-rw-r--r--lua/vim/userregs/impl/buffercontents.lua39
1 files changed, 39 insertions, 0 deletions
diff --git a/lua/vim/userregs/impl/buffercontents.lua b/lua/vim/userregs/impl/buffercontents.lua
new file mode 100644
index 0000000..016b4e5
--- /dev/null
+++ b/lua/vim/userregs/impl/buffercontents.lua
@@ -0,0 +1,39 @@
+local userregs = require("vim.userregs")
+
+-- Register name: "|"
+-- This register represents the full contents of the current buffer.
+-- Yank into it → saves the buffer.
+-- Put from it → replaces the buffer contents.
+
+local regname = "|"
+
+--- Reads the full contents of the current buffer as a list of lines.
+--- This is returned in the format expected by 'userregfunc'.
+--- @return table a 'put' dictionary with `lines`, `type`, and `additional_data`
+local function read_current_buffer()
+ local bufnr = vim.api.nvim_get_current_buf()
+ local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
+
+ return {
+ lines = lines,
+ type = "V", -- linewise register
+ additional_data = {},
+ }
+end
+
+--- Replaces the entire contents of the current buffer with the given content.
+--- Used when yanking into the "|" register.
+--- @param _ string -- register name (ignored here)
+--- @param content table -- dictionary with `lines` from yankreg_T
+local function write_current_buffer(_, content)
+ local bufnr = vim.api.nvim_get_current_buf()
+
+ vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, content.lines or {})
+end
+
+-- Register the "|" register with the userregs framework
+userregs.register_handler(regname, {
+ put = function(_) return read_current_buffer() end,
+ yank = write_current_buffer,
+})
+