-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
98 lines (78 loc) · 2.34 KB
/
main.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
local config = require("config")
local keybinds = require("keybinds")
-- https://stackoverflow.com/questions/22831701/lua-read-beginning-of-a-string
function string.starts(String, Start)
return string.sub(String, 1, string.len(Start)) == Start
end
function handle(key)
print("Handling " .. key)
local help = false
if config.helpKey ~= nil then
if string.starts(key, config.helpKey) then
help = true
key = string.gsub(key, config.helpKey, "")
end
end
if ximKeymap[ximMode] == nil then
return
end
if ximKeymap[ximMode][key] == nil then
return
end
local bind = ximKeymap[ximMode][key]
local modeBefore = ximMode
if help == false then
ximKeymap[ximMode][key].call()
print("Executing " .. key)
else
print("Printing help for " .. key)
app.msgbox("Xim Help\n\n[" .. config.map[ximMode].name .. "]: " .. bind.name, { "Ok" })
end
if ximSticky == false and modeBefore == ximMode then
ximMode = config.defaultMode
end
print("ximMode: " .. ximMode .. ", ximSticky: " .. tostring(ximSticky))
end
local function registerCallback(index, bind_name, button)
local funct_name = "funct" .. tostring(index)
-- Create a function callback dynamically
local funct =
funct_name ..
" = function() handle(\"" ..
button ..
"\") end"
local compiled, err = load(funct)
if compiled ~= nil then
compiled()
local block = {
["menu"] = "Xim: " .. bind_name,
["callback"] = funct_name,
["accelerator"] = button,
}
app.registerUi(block)
print("Registered: " .. funct)
else
print("Failed to load .." .. funct_name .. ":\n" .. tostring(err) .. "\n" .. funct)
end
end
local function registerCallbacks(keymap)
local index = 0
for _, mode in pairs(keymap) do
for button, bind in pairs(mode) do
registerCallback(index, bind.name, button)
index = index + 1
registerCallback(index, bind.name, config.helpKey .. button)
index = index + 1
end
end
end
function initUi()
app.registerUi({ ["menu"] = "Xim: Info", ["callback"] = "info", ["accelerator"] = "F1" })
registerCallbacks(ximKeymap)
end
function info()
app.msgbox("[" .. ximMode .. "]:\n" .. tostring(ximSticky), {})
end
ximMode = config.defaultMode
ximKeymap = keybinds.generateKeymap(config.map, config.generateModeKeybinds, config.stickyKey)
ximSticky = false