-
Notifications
You must be signed in to change notification settings - Fork 167
Expand file tree
/
Copy pathinit.lua
More file actions
305 lines (279 loc) · 8.31 KB
/
init.lua
File metadata and controls
305 lines (279 loc) · 8.31 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
--- === RecursiveBinder ===
---
--- A spoon that let you bind sequential bindings.
--- It also (optionally) shows a bar about current keys bindings.
---
--- [Click to download](https://github.com/Hammerspoon/Spoons/raw/master/Spoons/RecursiveBinder.spoon.zip)
local obj={}
obj.__index = obj
-- Metadata
obj.name = "RecursiveBinder"
obj.version = "0.7"
obj.author = "Yuan Fu <casouri@gmail.com>"
obj.homepage = "https://github.com/Hammerspoon/Spoons"
obj.license = "MIT - https://opensource.org/licenses/MIT"
--- RecursiveBinder.escapeKey
--- Variable
--- key to abort, default to {keyNone, 'escape'}
obj.escapeKey = {keyNone, 'escape'}
--- RecursiveBinder.helperEntryEachLine
--- Variable
--- Number of entries each line of helper. Default to 5.
obj.helperEntryEachLine = 5
--- RecursiveBinder.helperEntryLengthInChar
--- Variable
--- Length of each entry in char. Default to 20.
obj.helperEntryLengthInChar = 20
--- RecursiveBinder.helperFormat
--- Variable
--- format of helper, the helper is just a hs.alert
---
--- Notes:
--- * default to {atScreenEdge=2,
--- strokeColor={ white = 0, alpha = 2 },
--- textFont='SF Mono'
--- textSize=20}
obj.helperFormat = {atScreenEdge=2,
strokeColor={ white = 0, alpha = 2 },
textFont='Courier',
textSize=20}
--- RecursiveBinder.showBindHelper()
--- Variable
--- whether to show helper, can be true of false
obj.showBindHelper = true
--- RecursiveBinder.helperModifierMapping()
--- Variable
--- The mapping used to display modifiers on helper.
---
--- Notes:
--- * Default to {
--- command = '⌘',
--- control = '⌃',
--- option = '⌥',
--- shift = '⇧',
--- }
obj.helperModifierMapping = {
command = '⌘',
control = '⌃',
option = '⌥',
shift = '⇧',
}
-- used by next model to close previous helper
local previousHelperID = nil
-- this function is used by helper to display
-- appropriate 'shift + key' bindings
-- it turns a lower key to the corresponding
-- upper key on keyboard
local function keyboardUpper(key)
local upperTable = {
a='A',
b='B',
c='C',
d='D',
e='E',
f='F',
g='G',
h='H',
i='I',
j='J',
k='K',
l='L',
m='M',
n='N',
o='O',
p='P',
q='Q',
r='R',
s='S',
t='T',
u='U',
v='V',
w='W',
x='X',
y='Y',
z='Z',
['`']='~',
['1']='!',
['2']='@',
['3']='#',
['4']='$',
['5']='%',
['6']='^',
['7']='&',
['8']='*',
['9']='(',
['0']=')',
['-']='_',
['=']='+',
['[']='}',
[']']='}',
['\\']='|',
[';']=':',
['\'']='"',
[',']='<',
['.']='>',
['/']='?'
}
uppperKey = upperTable[key]
if uppperKey then
return uppperKey
else
return key
end
end
--- RecursiveBinder.singleKey(key, name)
--- Method
--- this function simply return a table with empty modifiers also it translates capital letters to normal letter with shift modifer
---
--- Parameters:
--- * key - a letter
--- * name - the description to pass to the keys binding function
---
--- Returns:
--- * a table of modifiers and keys and names, ready to be used in keymap
--- to pass to RecursiveBinder.recursiveBind()
function obj.singleKey(key, name)
local mod = {}
if key == keyboardUpper(key) and string.len(key) == 1 then
mod = {'shift'}
key = string.lower(key)
end
if name then
return {mod, key, name}
else
return {mod, key, 'no name'}
end
end
-- generate a string representation of a key spec
-- {{'shift', 'command'}, 'a} -> 'shift+command+a'
local function createKeyName(key)
-- key is in the form {{modifers}, key, (optional) name}
-- create proper key name for helper
local modifierTable = key[1]
local keyString = key[2]
-- add a little mapping for space
if keyString == 'space' then keyString = 'SPC' end
if #modifierTable == 1 and modifierTable[1] == 'shift' and string.len(keyString) == 1 then
-- shift + key map to Uppercase key
-- shift + d --> D
-- if key is not on letter(space), don't do it.
return keyboardUpper(keyString)
else
-- append each modifiers together
local keyName = ''
if #modifierTable >= 1 then
for count = 1, #modifierTable do
local modifier = modifierTable[count]
if count == 1 then
keyName = obj.helperModifierMapping[modifier]..' + '
else
keyName = keyName..obj.helperModifierMapping[modifier]..' + '
end
end
end
-- finally append key, e.g. 'f', after modifers
return keyName..keyString
end
end
-- show helper of available keys of current layer
local function showHelper(keyFuncNameTable)
-- keyFuncNameTable is a table that key is key name and value is description
local helper = ''
local separator = '' -- first loop doesn't need to add a separator, because it is in the very front.
local lastLine = ''
local count = 0
local keysSorted = {}
for k, v in pairs(keyFuncNameTable) do
table.insert(keysSorted, {key = k, value = v})
end
table.sort(keysSorted, function(a, b) return a.value < b.value end)
for i = 1, #keysSorted do
local keyName = keysSorted[i].key
local funcName = keysSorted[i].value
local newEntry = keyName..' → '..funcName
-- make sure each entry is of the same length
if string.len(newEntry) > obj.helperEntryLengthInChar then
newEntry = string.sub(newEntry, 1, obj.helperEntryLengthInChar - 2)..'..'
elseif string.len(newEntry) < obj.helperEntryLengthInChar then
newEntry = newEntry..string.rep(' ', obj.helperEntryLengthInChar - string.len(newEntry))
end
-- create new line for every helperEntryEachLine entries
if count == 0 then
separator = ''
elseif count % obj.helperEntryEachLine == 0 then
separator = '\n'
else
separator = ' '
end
helper = helper..separator..newEntry
count = count + 1
end
previousHelperID = hs.alert.show(helper, obj.helperFormat, true)
end
local function killHelper()
hs.alert.closeSpecific(previousHelperID)
end
--- RecursiveBinder.recursiveBind(keymap)
--- Method
--- Bind sequential keys by a nested keymap.
---
--- Parameters:
--- * keymap - A table that specifies the mapping.
---
--- Returns:
--- * A function to start. Bind it to a initial key binding.
---
--- Notes:
--- * Spec of keymap:
--- * Every key is of format {{modifers}, key, (optional) description}
--- * The first two element is what you usually pass into a hs.hotkey.bind() function.
--- * Each value of key can be in two form:
--- 1. A function. Then pressing the key invokes the function
--- 2. A table. Then pressing the key bring to another layer of keybindings.
--- And the table have the same format of top table: keys to keys, value to table or function
-- the actual binding function
function obj.recursiveBind(keymap, modals)
if not modals then modals = {} end
if type(keymap) == 'function' then
-- in this case "keymap" is actuall a function
return keymap
end
local modal = hs.hotkey.modal.new()
table.insert(modals, modal)
local keyFuncNameTable = {}
for key, map in pairs(keymap) do
local func = obj.recursiveBind(map, modals)
-- key[1] is modifiers, i.e. {'shift'}, key[2] is key, i.e. 'f'
modal:bind(key[1], key[2], function() modal:exit() killHelper() func() end)
modal:bind(obj.escapeKey[1], obj.escapeKey[2], function() modal:exit() killHelper() end)
if #key >= 3 then
keyFuncNameTable[createKeyName(key)] = key[3]
end
end
return function()
-- exit all modals, accounts for pressing the trigger key while
-- a modal is already open
for _, modal in pairs(modals) do
modal:exit()
end
modal:enter()
killHelper()
if obj.showBindHelper then
showHelper(keyFuncNameTable)
end
end
end
-- function testrecursiveModal(keymap)
-- print(keymap)
-- if type(keymap) == 'number' then
-- return keymap
-- end
-- print('make new modal')
-- for key, map in pairs(keymap) do
-- print('key', key, 'map', testrecursiveModal(map))
-- end
-- return 0
-- end
-- mymap = {f = { r = 1, m = 2}, s = {r = 3, m = 4}, m = 5}
-- testrecursiveModal(mymap)
return obj