From 7b15a3a4b4fb4dc6be5d53a22c20d68f8181d68b Mon Sep 17 00:00:00 2001 From: Josh Rahm Date: Sun, 18 Oct 2026 15:49:48 -0600 Subject: [extregs] initial commit. added a bunch of useful extregs --- lua/extregs/storage.lua | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 lua/extregs/storage.lua (limited to 'lua/extregs/storage.lua') diff --git a/lua/extregs/storage.lua b/lua/extregs/storage.lua new file mode 100644 index 0000000..c532485 --- /dev/null +++ b/lua/extregs/storage.lua @@ -0,0 +1,24 @@ +local M = {} +M.cookie = 'NVIM_EXTREGS_JSON_V1' + +local function validate(reg) + if type(reg) ~= 'table' or type(reg.lines) ~= 'table' or type(reg.type) ~= 'string' then + error('invalid yankreg: expected { lines = {...}, type = "v"|"V"|blockwise }') + end + return reg +end + +function M.encode(reg) + return M.cookie .. '\n' .. vim.json.encode(validate(reg)) .. '\n' +end + +function M.decode(data) + if data:sub(1, #M.cookie + 1) == M.cookie .. '\n' then + return validate(vim.json.decode(data:sub(#M.cookie + 2))) + end + local lines = vim.split(data, '\n', { plain = true }) + if #lines > 1 and lines[#lines] == '' then table.remove(lines) end + return { lines = lines, type = #lines > 1 and 'V' or 'v' } +end + +return M -- cgit