generated from sayanarijit/plugin-template1.xplr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.lua
145 lines (122 loc) · 2.96 KB
/
init.lua
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
---@diagnostic disable
local xplr = xplr
---@diagnostic enable
local registers = {}
local function format(reg)
return "\x1b[1;7m " .. reg .. " \x1b[0m"
end
local function get(reg)
return registers[reg] or {}
end
local function set(reg, paths)
if paths and #paths ~= 0 then
registers[reg] = paths
else
registers[reg] = nil
end
end
local function all()
return registers
end
local function setup(args)
args = args or {}
args.mode = args.mode or "default"
args.key = args.key or '"'
xplr.config.modes.builtin[args.mode].key_bindings.on_key[args.key] = {
help = "registers",
messages = {
"PopMode",
{ SwitchModeCustom = "registers" },
},
}
xplr.config.modes.custom.registers = {
name = "registers",
layout = {
Horizontal = {
config = {
constraints = {
{ Percentage = 50 },
{ Percentage = 50 },
},
},
splits = {
{
CustomContent = {
title = "registers",
body = {
DynamicTable = {
widths = {
{ Length = 10 },
{ MinLessThanLayoutWidth = 10 },
},
render = "custom.registers.render",
},
},
},
},
"Selection",
},
},
},
key_bindings = {
on_key = {
esc = {
help = "cancel",
messages = {
"PopMode",
},
},
["ctrl-c"] = {
messages = {
"Terminate",
},
},
},
on_character = {
help = "swap with register",
messages = {
"UpdateInputBufferFromKey",
{ CallLuaSilently = "custom.registers.swap" },
"PopMode",
},
},
},
}
xplr.fn.custom.registers = {}
xplr.fn.custom.registers.swap = function(app)
local reg = app.input_buffer
local msgs = {
"ClearSelection",
"ResetInputBuffer",
{ LogInfo = string.format("Selection swapped with register %q", reg) },
}
local selection = get(reg)
for _, path in ipairs(selection) do
table.insert(msgs, { SelectPath = path })
end
selection = {}
for _, node in ipairs(app.selection) do
table.insert(selection, node.absolute_path)
end
set(reg, selection)
return msgs
end
xplr.fn.custom.registers.render = function(_)
local view = {
{ "reg", "# selected" },
{ "───", "──────────" },
}
local _registers = {}
for reg, selection in pairs(all()) do
table.insert(_registers, { reg = reg, selection = selection })
end
table.sort(_registers, function(a, b)
return a.reg < b.reg
end)
for _, x in ipairs(_registers) do
table.insert(view, { format(x.reg), tostring(#x.selection) })
end
return view
end
end
return { setup = setup, get = get, set = set, all = all }