-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinit.lua
More file actions
465 lines (384 loc) · 12.6 KB
/
init.lua
File metadata and controls
465 lines (384 loc) · 12.6 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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
local VK_JIS_YEN = 0x5d
local VK_JIS_UNDERSCORE = 0x5e
local VK_JIS_LEFT_BRACKET = 0x1E
local VK_JIS_RIGHT_BRACKET = 0x2A
local VK_LEFT_COMMAND = 0x37
local VK_RIGHT_COMMAND = 0x36
local VK_EISUU = 0x66
local VK_KANA = 0x68
local showInfo = false
local function info(message)
if showInfo then
hs.alert.show(message)
end
end
local function warn(message)
hs.alert.show(message)
end
hs.hotkey.bind({'cmd', 'shift', 'ctrl'}, 'D', function() showInfo = not(showInfo) end)
--
-- to switch eisuu/kana with single command press.
--
switchInputMethodPrevKey = 0xFF
-- invalidate previous key
switchInputMethodInvalidate = hs.eventtap.new({hs.eventtap.event.types.keyDown},
function(e)
switchInputMethodPrevKey = 0xFF
end
)
switchInputMethodInvalidate:start()
repeatedCountOfEisuu = 0
reverseEisuuKana = false
switchInputMethod = hs.eventtap.new({hs.eventtap.event.types.flagsChanged},
function(e)
local keyCode = e:getKeyCode()
local isCmd = e:getFlags()['cmd']
info("flagsChanged code:"..tostring(keyCode))
info("flagsChanged flags:"..tostring(isCmd))
local eisuuTrigger = VK_LEFT_COMMAND
local kanaTrigger = VK_RIGHT_COMMAND
info("is reverse"..tostring(reverseEisuuKana))
if reverseEisuuKana then
eisuuTrigger = VK_RIGHT_COMMAND
kanaTrigger = VK_LEFT_COMMAND
end
if isCmd then
switchInputMethodPrevKey = keyCode
else
if switchInputMethodPrevKey == eisuuTrigger then
hs.eventtap.keyStroke({}, VK_EISUU)
repeatedCountOfEisuu = 0
elseif switchInputMethodPrevKey == kanaTrigger then
hs.eventtap.keyStroke({}, VK_KANA)
repeatedCountOfEisuu = repeatedCountOfEisuu + 1
if repeatedCountOfEisuu >= 5 then
repeatedCountOfEisuu = 0
reverseEisuuKana = not reverseEisuuKana
end
end
end
end
)
switchInputMethod:start()
--
-- to emacs like keybindins in Xcode
--
local function getTableLength(t)
local count = 0
for _ in pairs(t) do count = count + 1 end
return count
end
local function isEqualTable(t1, t2)
if getTableLength(t1) ~= getTableLength(t2) then return false end
for k, v in pairs(t1) do
if t2[k] ~= v then
return false
end
end
return true
end
local function disableAll(keySet)
for k, v in pairs(keySet) do v:disable() end
end
local function enableAll(keySet)
for k, v in pairs(keySet) do v:enable() end
end
local function pressKey(modifiers, key)
modifiers = modifiers or {}
hs.eventtap.keyStroke(modifiers, key, 20 * 1000)
end
local function pressKeyFunc(modifiers, key)
return function() pressKey(modifiers, key) end
end
local function createKeyRemap(srcModifiers, srcKey, dstModifiers, dstKey)
dstFunc = function() pressKey(dstModifiers, dstKey) end
return hs.hotkey.new(srcModifiers, srcKey, dstFunc, nil, dstFunc)
end
SubMode = {}
SubMode.new = function(name, commandTable, othersFunc)
local obj = {}
obj.name = name
obj.commandTable = commandTable
obj.othersFunc = othersFunc
obj.commandWatcher = {}
obj.enable = function(self)
info(self.name.." start")
self.commandWatcher:start()
end
obj.disable = function(self)
info(self.name.." end")
self.commandWatcher:stop()
end
obj.commandWatcher = hs.eventtap.new( {hs.eventtap.event.types.keyDown},
function(tapEvent)
for k,v in pairs(obj.commandTable) do
if v.key == hs.keycodes.map[tapEvent:getKeyCode()] and isEqualTable(v.modifiers, tapEvent:getFlags()) then
info(obj.name.." end")
obj.commandWatcher:stop()
if v.func then
v.func()
end
return true
end
end
if obj.othersFunc then
return othersFunc(tapEvent)
end
end)
return obj
end
markMode = SubMode.new(
"Mark Mode",
{
{modifiers = {ctrl = true}, key = 'space'}, -- only disables mark mode
{modifiers = {ctrl = true}, key = 'g'}, -- only disables mark mode
{modifiers = {ctrl = true}, key = 'w', func = pressKeyFunc({'cmd'}, 'x')},
{modifiers = {alt = true}, key = 'w', func = pressKeyFunc({'cmd'}, 'c')}
},
function(tapEvent) -- force shift on
flags = tapEvent:getFlags()
flags.shift = true
tapEvent:setFlags(flags)
return false
end
)
commandMode = SubMode.new(
"Command Mode",
{
{modifiers = {ctrl = true}, key = 'g'}, -- only disables command mode
{modifiers = {ctrl = true}, key = 'f', func = pressKeyFunc({'cmd', 'shift'}, 'o')},
{modifiers = {ctrl = true}, key = 's', func = pressKeyFunc({'cmd'}, 's')},
-- next tab
{modifiers = {}, key = 'n', func = pressKeyFunc({'cmd', 'shift'}, VK_JIS_RIGHT_BRACKET)},
-- previous tab
{modifiers = {}, key = 'p', func = pressKeyFunc({'cmd', 'shift'}, VK_JIS_LEFT_BRACKET)},
-- close tab
{modifiers = {}, key = 'k', func = pressKeyFunc({'cmd'}, 'w')},
}
)
xcodeBindings = {
-- mark mode
hs.hotkey.new({'ctrl'}, 'space', function() markMode:enable() end),
-- command mode
hs.hotkey.new({'ctrl'}, 'x', function() commandMode:enable() end),
-- etc
-- jump to beginning/end of document
createKeyRemap({'alt', 'shift'}, ',', {'cmd'}, 'up'),
createKeyRemap({'alt', 'shift'}, '.', {'cmd'}, 'down'),
-- move to up/down of line. for popup window
createKeyRemap({'ctrl'}, 'p', {}, 'up'),
createKeyRemap({'ctrl'}, 'n', {}, 'down'),
-- undo
createKeyRemap({'ctrl'}, '/', {'cmd'}, 'z'),
-- search
createKeyRemap({'ctrl'}, 's', {'cmd'}, 'f'),
-- paste
createKeyRemap({'ctrl'}, 'y', {'cmd'}, 'v'),
-- kill line
hs.hotkey.new({'ctrl'}, 'k', function()
markMode:enable()
pressKey({'ctrl'}, 'e')
pressKey({'ctrl'}, 'w')
end),
}
hs.window.filter.new()
:subscribe(hs.window.filter.windowFocused,function()
local focused = hs.window.focusedWindow()
if focused:application():name() == 'Xcode' then
-- warn("Focused XCode")
enableAll(xcodeBindings)
else
-- warn("Unfocused XCode")
disableAll(xcodeBindings)
markMode:disable()
commandMode:disable()
end
if focused:application():name() == 'TradingView' then
enableAll(tradingViewBindings)
else
disableAll(tradingViewBindings)
markMode:disable()
commandMode:disable()
end
end)
--
-- to integrate message send with Cmd + Enter in messenger apps.
--
slackSendBindings = hs.eventtap.new( {hs.eventtap.event.types.keyDown},
function(e)
if hs.keycodes.map[e:getKeyCode()] == 'return' and isEqualTable(e:getFlags(), {cmd = true}) then
info("Cmd return -> return")
e:setFlags({cmd = false})
elseif hs.keycodes.map[e:getKeyCode()] == 'return' and isEqualTable(e:getFlags(), {}) then
info("return -> shift + return")
e:setFlags({shift = true})
end
end)
hs.window.filter.new({'Skype', 'Discord'})
:subscribe(hs.window.filter.windowFocused,function() slackSendBindings:start() end)
:subscribe(hs.window.filter.windowUnfocused,function() slackSendBindings:stop() end)
--
-- to input backslash in JetBrains IDE.
--
function flagsMatches(flags, modifiers)
local set = {}
for _, k in ipairs(modifiers) do set[string.lower(k)] = true end
for _, k in ipairs({'fn', 'cmd', 'ctrl', 'alt', 'shift'}) do
if set[k] ~= flags[k] then return false end
end
return true
end
hs.hotkey.bind({"option"}, "f", function()
local app = hs.appfinder.appFromName("Finder")
if app ~= nil and app:isFrontmost() then
app:hide()
else
hs.application.launchOrFocus("Finder")
end
end)
function getTargetText()
local elem = hs.uielement.focusedElement()
if elem then
local sel = elem:selectedText()
if (sel == nil or sel == "") then
sel = hs.pasteboard.getContents()
if (sel == nil or sel == "") then
return
end
end
return sel
else
return
end
end
--
-- to show hex of selected text.
--
function showHexDescription()
local targetText = getTargetText()
if targetText == nil then
return
end
local targetNum = tonumber(targetText)
if targetNum == nil then
return
end
message = string.format("Base10: %d\nBase16: 0x%X", targetNum, targetNum)
local showSeconds = 5
hs.alert.show(message, showSeconds)
end
hs.hotkey.bind({'shift', 'ctrl'}, 'h', function() showHexDescription() end)
--
-- to show date of selected timestamp.
--
function showTimeDescription()
local targetText = getTargetText()
if targetText == nil then
return
end
local targetNum = tonumber(targetText)
if targetNum == nil then
return
end
local formattedTime
if targetNum > 10000000000 then
formattedTime = os.date("%Y-%m-%d %H:%M:%S", math.floor(targetNum / 1000))
local millis = targetNum % 1000
formattedTime = string.format("%s.%03d", formattedTime, millis)
else
formattedTime = os.date("%Y-%m-%d %H:%M:%S", targetNum)
end
message = string.format("Timestamp: %d\nDate: %s", targetNum, formattedTime)
local showSeconds = 5
hs.alert.show(message, showSeconds)
end
hs.hotkey.bind({'shift', 'ctrl'}, 't', function() showTimeDescription() end)
local function parseHexColor(targetText)
targetText = targetText:match("^#?(.*)$")
local a, r, g, b
if targetText:len() > 6 then
a, r, g, b = targetText:match("(%x%x)(%x%x)(%x%x)(%x%x)")
else
a = nil
r, g, b = targetText:match("(%x%x)(%x%x)(%x%x)")
end
if r and g and b then
if a then
return tonumber(a, 16), tonumber(r, 16), tonumber(g, 16), tonumber(b, 16)
else
return nil, tonumber(r, 16), tonumber(g, 16), tonumber(b, 16)
end
else
return nil, nil, nil, nil
end
end
--
-- to show color of selected hexstring.
--
function showColorDescription()
local targetText = getTargetText()
if targetText == nil then
return
end
local a, r, g, b = parseHexColor(targetText)
if r and g and b then
if a == nil then
a = 255
end
message = string.format("A: %d, R: %d, G: %d, B: %d", a, r, g, b)
hs.alert.show(message, {fillColor = {alpha = a / 255, red = r / 255, green = g / 255, blue = b / 255}}, 5)
end
end
hs.hotkey.bind({'shift', 'ctrl'}, 'c', function() showColorDescription() end)
displayUpdate = hs.eventtap.new( {hs.eventtap.event.types.keyDown},
function(e)
if hs.keycodes.map[e:getKeyCode()] == 'f2' and isEqualTable(e:getFlags(), {cmd = true, fn = true}) then
local event = hs.eventtap.event.newSystemKeyEvent("BRIGHTNESS_UP", true)
event:setFlags({cmd = true})
event:post()
end
end)
displayUpdate:start()
-- for trading view
tradingViewBindings = {
-- mark mode
hs.hotkey.new({'ctrl'}, 'space', function() markMode:enable() end),
-- command mode
hs.hotkey.new({'ctrl'}, 'x', function() commandMode:enable() end),
-- move to up/down of line. for popup window
createKeyRemap({'ctrl'}, 'p', {}, 'up'),
createKeyRemap({'ctrl'}, 'n', {}, 'down'),
-- undo
-- createKeyRemap({'ctrl'}, '/', {'cmd'}, 'z'),
-- search
-- createKeyRemap({'ctrl'}, 's', {'cmd'}, 'f'),
-- paste
createKeyRemap({'ctrl'}, 'y', {'cmd'}, 'v'),
-- kill line
hs.hotkey.new({'ctrl'}, 'k', function()
markMode:enable()
pressKey({'ctrl'}, 'e')
pressKey({'ctrl'}, 'w')
end),
}
--
-- for debug
--
local function showKeyPress(tapEvent)
local code = tapEvent:getKeyCode()
local charactor = hs.keycodes.map[tapEvent:getKeyCode()]
hs.alert.show(tostring(code)..":"..charactor, 1.5)
end
local keyTap = hs.eventtap.new( {hs.eventtap.event.types.keyDown}, showKeyPress)
k = hs.hotkey.modal.new({"cmd", "shift", "ctrl"}, 'P')
function k:entered()
hs.alert.show("Enabling Keypress Show Mode", 1.5)
keyTap:start()
end
function k:exited()
hs.alert.show("Disabling Keypress Show Mode", 1.5)
end
k:bind({"cmd", "shift", "ctrl"}, 'P', function()
keyTap:stop()
k:exit()
end)