diff options
author | Josh Rahm <rahm@google.com> | 2022-08-18 18:39:14 -0600 |
---|---|---|
committer | Josh Rahm <rahm@google.com> | 2022-08-18 18:39:14 -0600 |
commit | 852bd19b8148023db10aad2d9d1e0c44bf38cac6 (patch) | |
tree | ab27f3d89da365d503ab9109dca107be2b7996e9 | |
download | rneovim-userregs-852bd19b8148023db10aad2d9d1e0c44bf38cac6.tar.gz rneovim-userregs-852bd19b8148023db10aad2d9d1e0c44bf38cac6.tar.bz2 rneovim-userregs-852bd19b8148023db10aad2d9d1e0c44bf38cac6.zip |
Initial commit - fileregister
-rw-r--r-- | plugin/fileregister.lua | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/plugin/fileregister.lua b/plugin/fileregister.lua new file mode 100644 index 0000000..a2b5621 --- /dev/null +++ b/plugin/fileregister.lua @@ -0,0 +1,58 @@ +-- 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") +local json = require('JSON') +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" +end + +-- Stores the yanked text into the fileregister as JSON. +function handler.do_yank(self, content) + local file = self.get_file_name(); + string = json:encode(content) + + file = io.open(file, "w") + file:write(string) + file:close() +end + +-- Reads the text from the file register and returns it. +function handler.do_put(self) + local file = self.get_file_name(); + + file = io.open(file, "r") + + if not file 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 + end +end + +userreg.register_handler('&', handler) |