-
Notifications
You must be signed in to change notification settings - Fork 0
/
modmap.lua
76 lines (67 loc) · 1.52 KB
/
modmap.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
-- the mod map keeps track of all root modifier states
local M = {}
local dump = require("modalisa.lib.vim").inspect
local function set(self, key, mods, overwrite_only)
for _, mod in pairs(mods) do
if not overwrite_only or not (self.map[mod] == nil) then
self.map[mod] = true
end
end
local converted_key = self.conversion[key]
-- key is actually a mod (e.g. Super_L -> Mod4)
if converted_key then
if not overwrite_only or not (self.map[converted_key] == nil) then
self.map[converted_key] = true
end
end
end
function M:new(key, mods, modmap)
local inst = {}
inst.map = {}
inst.conversion = modmap
set(inst, key, mods, false)
return setmetatable(inst, {
__index = M,
})
end
function M:press(key, mods)
-- do not set pressed for mods that we don't care about
return set(self, key, mods, true)
end
function M:release(key)
local converted_key = self.conversion[key]
-- key is actually a mod (e.g. Super_L -> Mod4)
if converted_key then
if not (self.map[converted_key] == nil) then
self.map[converted_key] = false
end
end
end
function M:has_pressed_mods()
for _, m in pairs(self.map) do
if m then
return true
end
end
return false
end
function M:get_pressed_mods()
local pressed_mods = {}
for mod, is_pressed in pairs(self.map or {}) do
if is_pressed then
table.insert(pressed_mods, mod)
end
end
return pressed_mods
end
return setmetatable(M, {
__call = function(self, ...)
return self:new(...)
end,
__tostring = function()
if M.mod then
return dump(M.mod)
end
return "nil"
end,
})