-
Notifications
You must be signed in to change notification settings - Fork 0
/
keydoc.lua
152 lines (135 loc) · 3.79 KB
/
keydoc.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
-- Document key bindings
-- source at http://awesome.naquadah.org/wiki/Document_keybindings
local awful = require("awful")
local table = table
local ipairs = ipairs
local pairs = pairs
local math = math
local string = string
local type = type
local modkey = "Mod4"
local beautiful = require("beautiful")
local naughty = require("naughty")
local capi = {
root = root,
client = client
}
module("keydoc")
local doc = { }
local currentgroup = "Misc"
local orig = awful.key.new
-- Replacement for awful.key.new
local function new(mod, key, press, release, docstring)
-- Usually, there is no use of release, let's just use it for doc
-- if it's a string.
if press and release and not docstring and type(release) == "string" then
docstring = release
release = nil
end
local k = orig(mod, key, press, release)
-- Remember documentation for this key (we take the first one)
if k and #k > 0 and docstring then
doc[k[1]] = { help = docstring,
group = currentgroup }
end
return k
end
awful.key.new = new -- monkey patch
-- Turn a key to a string
local function key2str(key)
local sym = key.key or key.keysym
local translate = {
["#14"] = "#",
[" "] = "Space",
}
sym = translate[sym] or sym
if not key.modifiers or #key.modifiers == 0 then return sym end
local result = ""
local translate = {
[modkey] = "⊞",
Shift = "⇧",
Control = "Ctrl",
}
for _, mod in pairs(key.modifiers) do
mod = translate[mod] or mod
result = result .. mod .. " + "
end
return result .. sym
end
-- Unicode "aware" length function (well, UTF8 aware)
-- See: http://lua-users.org/wiki/LuaUnicode
local function unilen(str)
local _, count = string.gsub(str, "[^\128-\193]", "")
return count
end
-- Start a new group
function group(name)
currentgroup = name
return {}
end
local function markup(keys)
local result = {}
-- Compute longest key combination
local longest = 0
for _, key in ipairs(keys) do
if doc[key] then
longest = math.max(longest, unilen(key2str(key)))
end
end
local curgroup = nil
for _, key in ipairs(keys) do
if doc[key] then
local help, group = doc[key].help, doc[key].group
local skey = key2str(key)
result[group] = (result[group] or "") ..
'<span color="' .. beautiful.fg_widget_clock .. '"> '
.. skey
.. string.format("%" .. (longest - unilen(skey)) .. "s ", "") ..
'</span> <span color="' .. beautiful.fg_widget_value .. '">'
.. help ..
'</span>\n'
end
end
return result
end
-- Customize version of standard function pairs that sort keys
-- (from Michal Kottman on Stackoverflow)
function spairs(t, order)
-- collect the keys
local keys = {}
for k in pairs(t) do keys[#keys+1] = k end
-- if order function given, sort by it by passing the table and keys a, b,
-- otherwise just sort the keys
if order then
table.sort(keys, function(a,b) return order(t, a, b) end)
else
table.sort(keys)
end
-- return the iterator function
local i = 0
return function()
i = i + 1
if keys[i] then
return keys[i], t[keys[i]]
end
end
end
-- Display help in a naughty notification
local nid = nil
function display()
local strings = markup(awful.util.table.join(
capi.root.keys(),
capi.client.focus and capi.client.focus:keys() or {}))
local result = ""
for group, res in spairs(strings) do
if #result > 0 then result = result .. "\n" end
result = result ..
'<span weight="bold" color="'
.. beautiful.fg_widget_value_important ..
'">' .. group .. "</span>\n" .. res
end
nid = naughty.notify({ text = result,
replaces_id = nid,
hover_timeout = 0.1,
timeout = 30 }).id
end