-
Notifications
You must be signed in to change notification settings - Fork 1
/
init.lua
159 lines (145 loc) · 4.61 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
-- This project is licensed under the MIT License (see LICENSE).
--- Create key and button bindings from simple definitions.
--
-- @author James Reed <[email protected]> et al.
-- @copyright 2019-2020 James Reed
-- @module awesome-ez
local awful = require("awful")
local gtable = require("gears.table")
local ez = {}
ez.modifiers = {
["M"] = "Mod4",
["A"] = "Mod1",
["S"] = "Shift",
["C"] = "Control",
}
--- Split a string on a delimiter.
--
-- @param s The string.
-- @param sep The delimiter.
-- @return A table of strings.
local function split(s, sep)
sep = sep or "%s"
local res = {}
for m in string.gmatch(s, string.format("([^%s]+)", sep)) do
table.insert(res, m)
end
return res
end
ez.util = {}
--- Convert a table into a callable function.
--
-- The first element of the table should be a function, followed by arguments
-- to this function.
--
-- @param cb The callback or a table describing the callback.
-- @usage local cb = ez.util.cb_from_table({awful.spawn, "xterm"})
-- @return The callback.
function ez.util.cb_from_table(cb)
if type(cb) == "table" and
not (getmetatable(cb) and getmetatable(cb).__call) then
local tbl = gtable.clone(cb, false)
local func = table.remove(tbl, 1)
cb = function()
return func(unpack(tbl))
end
end
return cb
end
--- Parse a key definition string into modifiers and a key.
--
-- Key definition strings consist of modifier characters and a key separated
-- by hyphens, e.g. "M-S-x" is the combination of Mod4, Shift, and the x key.
-- If the key is surrounded by <>, it is interpreted as a key group, e.g.
-- "M-<numrow> uses the modifier Mod4 and the key group "numrow".
--
-- The modifier key strings are: M = Mod4, A = Mod1, S = Shift, C = Control.
--
-- @param keydef The key definition string.
-- @usage local modkeys, key = ez.util.parse_key("M-Return")
-- @return A table of modifiers and the key.
function ez.util.parse_key(keydef)
local modkeys = {}
for _, key in ipairs(split(keydef, "-")) do
if ez.modifiers[key] ~= nil then
table.insert(modkeys, ez.modifiers[key])
else
local group = string.match(key, "<(%w+)>")
if group then
return modkeys, nil, group
end
return modkeys, key
end
end
end
--- Parse a button definition string into modifiers and a button.
--
-- Button definition strings consist of modifier characters and a key separated
-- by hyphens, e.g. "M-S-1" is the combination of Mod4, Shift, and button 1.
--
-- The modifier key strings are: M = Mod4, A = Mod1, S = Shift, C = Control.
--
-- @param btndef The button definition string.
-- @usage local modkeys, btn = ez.util.parse_button("M-1")
-- @return A table of modifiers and the button.
function ez.util.parse_button(btndef)
if type(btndef) == "number" then
return {}, btndef
end
local modkeys = {}
for _, key in ipairs(split(btndef, "-")) do
if ez.modifiers[key] ~= nil then
table.insert(modkeys, ez.modifiers[key])
else
return modkeys, tonumber(key)
end
end
end
--- Create a key binding from a key definition string and callback.
--
-- @param keydef The key definition string.
-- @param cb The callback or table describing the callback.
-- @return A table with the key objects.
function ez.key(keydef, cb)
local modkeys, key, group = ez.util.parse_key(keydef)
if group then
return awful.key {
keygroup = group,
modifiers = modkeys,
on_press = cb,
}
end
return awful.key(modkeys, key, ez.util.cb_from_table(cb))
end
--- Create a button binding from a button definition string and callback.
--
-- @param btndef The button definition string.
-- @param cb The callback or table describing the callback.
-- @return A table with the button objects.
function ez.btn(btndef, cb)
local modkeys, btn = ez.util.parse_button(btndef)
return awful.button(modkeys, btn, ez.util.cb_from_table(cb))
end
--- Create key bindings for elements of a table.
--
-- @param tbl The table of key bindings.
-- @return A table containing created key objects.
function ez.keytable(tbl)
local res = {}
for keydef, cb in pairs(tbl) do
table.insert(res, ez.key(keydef, cb))
end
return res
end
--- Create button bindings for elements of a table.
--
-- @param tbl The table of button bindings.
-- @return A table containing created button objects.
function ez.btntable(tbl)
local res = {}
for btndef, cb in pairs(tbl) do
table.insert(res, ez.btn(btndef, cb))
end
return res
end
return ez