summaryrefslogtreecommitdiff
path: root/plugin/fileregister.lua
diff options
context:
space:
mode:
authorJosh Rahm <joshuarahm@gmail.com>2025-04-09 22:01:12 -0600
committerJosh Rahm <joshuarahm@gmail.com>2025-04-09 22:01:54 -0600
commitb3b09ff479510abc51ffb88743bb353bcc15e165 (patch)
treea2b7486a7146dfe4074faf9f1856fc25f461b466 /plugin/fileregister.lua
parent8495168e18c5d9f0dc1a7f5b8fcd23d2d0ab4652 (diff)
downloadrneovim-userregs-b3b09ff479510abc51ffb88743bb353bcc15e165.tar.gz
rneovim-userregs-b3b09ff479510abc51ffb88743bb353bcc15e165.tar.bz2
rneovim-userregs-b3b09ff479510abc51ffb88743bb353bcc15e165.zip
Update rneovim userregs to be compatible with userregs 2.0.
Diffstat (limited to 'plugin/fileregister.lua')
-rw-r--r--plugin/fileregister.lua98
1 files changed, 55 insertions, 43 deletions
diff --git a/plugin/fileregister.lua b/plugin/fileregister.lua
index 856149a..08bdc5a 100644
--- a/plugin/fileregister.lua
+++ b/plugin/fileregister.lua
@@ -1,59 +1,71 @@
--- Implementation for the "fileregister" ("&). The fileregister is a user
--- register that reads the register data from a specific file
--- (g:fileregister_filename).
---
--- If the data is encoded in JSON, it is read as 'content' (see :help urf),
--- otherwise the file is interpreted as lines (useful when writing to the file
--- from an external program).
---
--- requires 'json-lua'
-
-local userreg = require("vim.userreg")
+--- Fileregister: maps the '&' register to an external file.
+---
+--- This register reads from or writes to a file defined by `g:fileregister_filename`
+--- or defaults to `${RUN_DIRECTORY}/fileregister`.
+---
+--- On yank, the content is saved as JSON.
+--- On put, the content is decoded from JSON (if valid) or interpreted as raw lines.
+
+local userregs = require("vim.userregs")
local api = vim.api
local handler = {}
--- Retruns the filename to read/write to. Defaults to
--- /tmp/.vim.$USER.fileregister, but uses g:fileregister_filename if set.
-function handler.get_file_name()
- return vim.g.fileregister_filename or
- '/tmp/.vim.' .. os.getenv("USER") .. ".fileregister"
+--- Get the filename used for this register.
+--- Defaults to /tmp/.vim.$USER.fileregister
+---@return string
+local function get_file_name()
+ return vim.g.fileregister_filename
+ or (vim.fn.stdpath("run") .. "/fileregister")
end
--- Stores the yanked text into the fileregister as JSON.
-function handler.do_yank(self, content)
- local json = require('JSON')
- local file = self.get_file_name();
- string = json:encode(content)
+--- Write the yanked content into the fileregister as JSON.
+---@param _ string register name (ignored)
+---@param content table content from 'userregfunc'
+function handler.yank(_, content)
+ local filename = get_file_name()
+ local ok, encoded = pcall(vim.json.encode, content)
+ if not ok then
+ vim.notify("[fileregister] Failed to encode content to JSON", vim.log.levels.ERROR)
+ return
+ end
- file = io.open(file, "w")
- file:write(string)
- file:close()
-end
+ local f = io.open(filename, "w")
+ if not f then
+ vim.notify("[fileregister] Could not open file for writing: " .. filename, vim.log.levels.ERROR)
+ return
+ end
--- Reads the text from the file register and returns it.
-function handler.do_put(self)
- local file = self.get_file_name();
- local json = require('JSON')
+ f:write(encoded)
+ f:close()
+end
- file = io.open(file, "r")
+--- Read from the file. If it's valid JSON, decode as register content.
+--- Otherwise, interpret it as a list of lines.
+---@param _ string register name (ignored)
+---@return table|string
+function handler.put(_)
+ local filename = get_file_name()
+ local f = io.open(filename, "r")
- if not file then
+ if not f then
return {}
end
- content = file:read "*a"
- file:close();
-
- if content:sub(1, 1) == '{' then
- return json:decode(content)
- else
- ret = {}
- for s in content:gmatch("[^\r\n]+") do
- table.insert(ret, s)
- end
- return ret
+ local content = f:read("*a")
+ f:close()
+
+ if content:sub(1, 1) == "{" then
+ local ok, decoded = pcall(vim.json.decode, content)
+ if ok then return decoded end
+ end
+
+ local lines = {}
+ for line in content:gmatch("[^\r\n]+") do
+ table.insert(lines, line)
end
+ return lines
end
-userreg.register_handler('&', handler)
+-- Register the '&' fileregister
+userregs.register_handler('&', handler)