blob: 856149a627a0212385f08577d4ec78ba6a078c32 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
-- 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 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 json = require('JSON')
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();
local json = require('JSON')
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)
|