Repository navigation
Expand file tree
/
Copy pathHotkeyBindWhen.lua
More file actions
358 lines (297 loc) · 12.3 KB
/
Copy pathHotkeyBindWhen.lua
File metadata and controls
358 lines (297 loc) · 12.3 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
-- https://github.com/Hammerspoon/hammerspoon/blob/master/SPOONS.md#hotkeys
-- https://stackoverflow.com/questions/63795560/how-can-i-prevent-hammerspoon-hotkeys-from-overriding-hotkeys-in-other-applicati
---@class hs.hotkey.conditions
---@class hs.hotkey.target
---@field app fun(appName: string): HotkeyTarget
---@field appTab fun(appName: string, tabPattern: string): HotkeyTarget
---@class hs.hotkey
---@field conditions hs.hotkey.conditions
---@field target hs.hotkey.target
---@alias HotkeyCondition fun(currentAppName: string, currentTab: string): boolean
---@alias HotkeyTarget { kind: "app"|"appTab", app: string, tab: string|nil, withTab: function|nil }
---@alias HotkeyTargetSpec { app: string, tab: string|nil }
---@alias HotkeyTargetInput HotkeyCondition|HotkeyTarget|HotkeyTargetSpec|string
---@type hs.hotkey.conditions
hs.hotkey.conditions = hs.hotkey.conditions or {}
local hotkeyConditions = hs.hotkey.conditions
---@type hs.hotkey.target
hs.hotkey.target = hs.hotkey.target or {}
local hotkeyTarget = hs.hotkey.target
---@class hs.hotkey.target
---@field app fun(appName: string): HotkeyTarget
---@field appTab fun(appName: string, tabPattern: string): HotkeyTarget
function hotkeyTarget.appTab(appName, tabPattern)
return {
kind = "appTab",
app = appName,
tab = tabPattern
}
end
function hotkeyTarget.app(appName)
return {
kind = "app",
app = appName,
withTab = function(tabPattern)
return hotkeyTarget.appTab(appName, tabPattern)
end
}
end
local function matchesPattern(value, pattern)
return string.match(value, pattern) ~= nil
end
local function assertString(value, name)
if type(value) ~= "string" then
error(name .. " must be a string, got " .. type(value) .. ": " .. tostring(value), 3)
end
end
local function conditionFromTarget(target)
if target.kind == "app" then
assertString(target.app, "target.app")
return function(currentAppName)
return matchesPattern(currentAppName, target.app)
end
end
if target.kind == "appTab" then
assertString(target.app, "target.app")
assertString(target.tab, "target.tab")
return function(currentAppName, currentTab)
return matchesPattern(currentAppName, target.app)
and matchesPattern(currentTab, target.tab)
end
end
error("Unknown hotkey target kind: " .. tostring(target.kind), 3)
end
local function conditionFromOne(conditionOrTarget)
if type(conditionOrTarget) == "function" then
return conditionOrTarget
end
if type(conditionOrTarget) == "string" then
return conditionFromTarget(hotkeyTarget.app(conditionOrTarget))
end
if type(conditionOrTarget) == "table" and conditionOrTarget.kind then
return conditionFromTarget(conditionOrTarget)
end
if type(conditionOrTarget) == "table" and conditionOrTarget.app then
if conditionOrTarget.tab then
return conditionFromTarget(hotkeyTarget.appTab(conditionOrTarget.app, conditionOrTarget.tab))
end
return conditionFromTarget(hotkeyTarget.app(conditionOrTarget.app))
end
error("Expected a hotkey condition or target, got "
.. type(conditionOrTarget)
.. ": "
.. tostring(conditionOrTarget), 3)
end
local function conditionsFrom(...)
local args = { ... }
if #args == 1 and type(args[1]) == "table" and not args[1].kind and not args[1].app then
args = args[1]
end
if #args == 0 then
error("Expected at least one hotkey condition or target", 3)
end
local conditions = {}
for _, conditionOrTarget in ipairs(args) do
table.insert(conditions, conditionFromOne(conditionOrTarget))
end
return conditions
end
local function conditionFrom(...)
local conditions = conditionsFrom(...)
if #conditions == 1 then
return conditions[1]
end
return hotkeyConditions.anyOf(table.unpack(conditions))
end
--- condition combinators
---@class hs.hotkey.conditions
---@field allApps HotkeyCondition
function hotkeyConditions.allApps()
return true
end
---@class hs.hotkey.conditions
---@field anyOf fun(conditionOrTarget: HotkeyTargetInput, ...: HotkeyTargetInput): HotkeyCondition
function hotkeyConditions.anyOf(...)
local conditions = conditionsFrom(...)
return function(currentAppName, currentTab)
for _, cond in ipairs(conditions) do
if cond(currentAppName, currentTab) then
return true
end
end
return false
end
end
---@class hs.hotkey.conditions
---@field noneOf fun(conditionOrTarget: HotkeyTargetInput, ...: HotkeyTargetInput): HotkeyCondition
function hotkeyConditions.noneOf(...)
local conditions = conditionsFrom(...)
return function(currentAppName, currentTab)
for _, cond in ipairs(conditions) do
if cond(currentAppName, currentTab) then
return false
end
end
return true
end
end
-- hs.hotkey.bindWhen(HotkeyTargetInput, modifier, key, message, pressedfn, releasedfn, repeatfn)
-- hs.hotkey.when(HotkeyTargetInput, ...).bind(modifier, key, message, pressedfn, releasedfn, repeatfn)
-- hs.hotkey.when(HotkeyTargetInput, ...).block(modifier, key, message, debugMessage)
-- hs.hotkey.whenNot(HotkeyTargetInput, ...).bind(modifier, key, message, pressedfn, releasedfn, repeatfn)
-- hs.hotkey.whenNot(HotkeyTargetInput, ...).block(modifier, key, message, debugMessage)
-- Blocked press and repeat events are passed through with hs.eventtap.keyStroke.
-- Blocked release events are ignored.
-- local at = hs.hotkey.target
-- local conditions = hs.hotkey.conditions
-- HotkeyTargetInput :=
-- appName
-- { app = appName }
-- { app = appName, tab = tabPattern }
-- at.app(appName)
-- at.app(appName).withTab(tabPattern)
-- at.appTab(appName, tabPattern)
-- condition
-- { targetOrCondition, ... }
-- Condition combinators:
-- conditions.allApps
-- conditions.anyOf(targetOrCondition, ...)
-- conditions.noneOf(targetOrCondition, ...)
-- `appName` := app name pattern, for example "Google Chrome"
-- `tabPattern` := focused window title pattern, for example "essages%)$"
-- `targetOrCondition` := HotkeyTargetInput except nested lists
-- hs.hotkey.when("Google Chrome", "IntelliJ IDEA").bind({"cmd"}, "n", "info", pressedfn)
-- hs.hotkey.when({ "Google Chrome", "IntelliJ IDEA" }).bind({"cmd"}, "n", "info", pressedfn)
-- hs.hotkey.when({ app = "MailMate", tab = "essages%)$" }).bind({"cmd"}, "n", "info", pressedfn)
-- hs.hotkey.when(at.app("MailMate").withTab("essages%)$"), at.app("WhatsApp").withTab("WhatsApp")).bind({"cmd"}, "n", "info", pressedfn)
-- hs.hotkey.when(conditions.anyOf("Google Chrome", "IntelliJ IDEA")).bind({"cmd"}, "n", "info", pressedfn)
-- hs.hotkey.whenNot("Google Chrome").bind({"cmd"}, "n", "info", pressedfn)
-- hs.hotkey.when(conditions.allApps).bind({"cmd"}, "n", nil, pressedfn)
-- hs.hotkey.when("MailMate").block({"cmd"}, "W", "block cmd+w", "blocked cmd+w in MailMate")
---@param shouldHandleHotkey HotkeyTargetInput
---@param modifier table|string
---@param key string|number
---@param message string|nil
---@param pressedfn function|nil
---@param releasedfn function|nil
---@param repeatfn function|nil
---@class hs.hotkey
---@field bindWhen fun(shouldHandleHotkey: HotkeyTargetInput, modifier: table|string, key: string|number, message: string|nil, pressedfn: function|nil, releasedfn: function|nil, repeatfn: function|nil)
function hs.hotkey.bindWhen(shouldHandleHotkey, modifier, key, message, pressedfn, releasedfn, repeatfn)
local shouldHandleCondition = conditionFrom(shouldHandleHotkey)
-- in case someone forget to set the message to nil
if type(message) == "function" then
repeatfn = releasedfn
releasedfn = pressedfn
pressedfn = message
message = nil
end
if not pressedfn and not releasedfn and not repeatfn then
error("At least one of pressedfn, releasedfn or repeatfn must be a function", 2)
end
local hotkeyHandler
local function shouldHandleCurrentContext()
-- guard: frontmostApplication may be nil briefly
local frontmost = hs.application.frontmostApplication()
local currentApp = frontmost and frontmost:name() or ""
debugInfo("currentApp = hs.application.frontmostApplication():name()")
debugInfo("currentApp: ", currentApp)
-- guard: focusedWindow may be nil (e.g., Desktop focus or no windows)
local focusedWindow = hs.window.focusedWindow()
debugInfo("focusedWindow = hs.window.focusedWindow():")
debugInfo("focusedWindow: ", focusedWindow)
local currentTab
if focusedWindow and focusedWindow.title then
currentTab = focusedWindow:title()
else
currentTab = ""
end
debugInfo("currentTab=focusedWindow:title()")
debugInfo("currentTab: ", currentTab)
local shouldHandleCurrentHotkey = shouldHandleCondition(currentApp, currentTab)
debugInfo("shouldHandleCurrentHotkey = shouldHandleCondition(currentApp, currentTab)")
debugInfo("shouldHandleCurrentHotkey: ", shouldHandleCurrentHotkey)
return shouldHandleCurrentHotkey
end
local function passThroughKeystroke()
hotkeyDisableSilent(hotkeyHandler)
hs.eventtap.keyStroke(modifier, key)
hotkeyEnableSilent(hotkeyHandler)
end
local function handleHotkeyEvent(callback, passThroughWhenBlocked)
local shouldHandleCurrentHotkey = shouldHandleCurrentContext()
if shouldHandleCurrentHotkey then
--hs.alert.show("execute on: " .. currentApp)
--logger.i("execute on: " .. currentApp)
if callback then
callback()
end
elseif passThroughWhenBlocked then
passThroughKeystroke()
else
--hs.alert.show("no function at: " .. currentApp)
--logger.i("no function at: " .. currentApp)
end
end
local function optionalHandlerFor(callback, passThroughWhenBlocked)
if not callback then
return nil
end
return function()
handleHotkeyEvent(callback, passThroughWhenBlocked)
end
end
local function requiredHandlerFor(callback, passThroughWhenBlocked)
return function()
handleHotkeyEvent(callback, passThroughWhenBlocked)
end
end
hotkeyHandler = hs.hotkey.bind(
modifier,
key,
message,
requiredHandlerFor(pressedfn, true),
optionalHandlerFor(releasedfn, false),
optionalHandlerFor(repeatfn, true)
)
end
---@class hs.hotkey.whenBuilder
---@field bind fun(modifier: table|string, key: string|number, message: string|nil, pressedfn: function|nil, releasedfn: function|nil, repeatfn: function|nil)
---@field block fun(modifier: table|string, key: string|number, message: string|nil, debugMessage: string|nil)
---@param ... HotkeyTargetInput
---@return hs.hotkey.whenBuilder
---@class hs.hotkey
---@field when fun(...: HotkeyTargetInput): hs.hotkey.whenBuilder
function hs.hotkey.when(...)
local shouldHandleHotkey = conditionFrom(...)
return {
bind = function(modifier, key, message, pressedfn, releasedfn, repeatfn)
hs.hotkey.bindWhen(shouldHandleHotkey, modifier, key, message, pressedfn, releasedfn, repeatfn)
end,
block = function(modifier, key, message, debugMessage)
hs.hotkey.blockWhen(shouldHandleHotkey, modifier, key, message, debugMessage)
end
}
end
---@param ... HotkeyTargetInput
---@return hs.hotkey.whenBuilder
---@class hs.hotkey
---@field whenNot fun(...: HotkeyTargetInput): hs.hotkey.whenBuilder
function hs.hotkey.whenNot(...)
return hs.hotkey.when(hotkeyConditions.noneOf(...))
end
---@param shouldHandleHotkey HotkeyTargetInput
---@param modifier table|string
---@param key string|number
---@param message string|nil
---@param debugMessage string|nil
---@class hs.hotkey
---@field blockWhen fun(shouldHandleHotkey: HotkeyTargetInput, modifier: table|string, key: string|number, message: string|nil, debugMessage: string|nil)
function hs.hotkey.blockWhen(shouldHandleHotkey, modifier, key, message, debugMessage)
local noop = function()
if debugMessage then
debugInfo(debugMessage)
end
end
hs.hotkey.bindWhen(shouldHandleHotkey, modifier, key, message, noop)
end