-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcutlass.lua
90 lines (76 loc) · 2.71 KB
/
cutlass.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
local cutlass = {}
local function with_defaults(options)
local flip = function(t)
local flipped = {}
for _, value in pairs(t) do
flipped[value] = true
end
return flipped
end
return {
cut_key = options.cut_key or nil,
override_del = options.override_del or nil,
exclude = options.exclude and flip(options.exclude) or {},
registers = vim.tbl_extend("force", {
select = "_",
delete = "_",
change = "_",
}, options.registers or {}),
}
end
local keymap_opts = { noremap = true, silent = true }
local map = vim.keymap.set
function cutlass.setup(options)
cutlass.options = with_defaults(options or {})
cutlass.override_delete_and_change_bindings()
cutlass.override_select_bindings()
cutlass.create_cut_bindings()
end
function cutlass.override_delete_and_change_bindings()
for _, mode in pairs({ "x", "n" }) do
for _, lhs in pairs({ "c", "C", "s", "S" }) do
if not cutlass.options.exclude[mode .. lhs] and vim.fn.maparg(lhs, mode) == "" then
map(mode, lhs, string.format('"%s%s', cutlass.options.registers.change, lhs), keymap_opts)
end
end
for _, lhs in pairs({ "d", "D", "x", "X" }) do
if not cutlass.options.exclude[mode .. lhs] and vim.fn.maparg(lhs, mode) == "" then
map(mode, lhs, string.format('"%s%s', cutlass.options.registers.delete, lhs), keymap_opts)
end
end
end
if cutlass.options.override_del == true then
map("n", "<Del>", string.format('"%sx', cutlass.options.registers.delete), keymap_opts)
map("x", "<Del>", string.format('"%sx', cutlass.options.registers.delete), keymap_opts)
end
end
function cutlass.override_select_bindings()
-- Add a map for every printable character to copy to black hole register
for char_nr = 33, 126 do
local char = vim.fn.nr2char(char_nr)
if not cutlass.options.exclude["s" .. char] then
map(
"s",
char,
string.format('<c-o>"%sc%s', cutlass.options.registers.select, char == "\\" and "\\\\" or char),
keymap_opts
)
end
end
if not cutlass.options.exclude["s<bs>"] then
map("s", "<bs>", string.format('<c-o>"%sc', cutlass.options.registers.select), keymap_opts)
end
if not cutlass.options.exclude["s<space>"] then
map("s", "<space>", string.format('<c-o>"%sc<space>', cutlass.options.registers.select), keymap_opts)
end
end
function cutlass.create_cut_bindings()
if nil == cutlass.options.cut_key then
return
end
map("n", cutlass.options.cut_key, "d", keymap_opts)
map("x", cutlass.options.cut_key, "d", keymap_opts)
map("n", cutlass.options.cut_key .. cutlass.options.cut_key, "dd", keymap_opts)
map("n", string.upper(cutlass.options.cut_key), "D", keymap_opts)
end
return cutlass