-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.lua
390 lines (337 loc) · 10 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
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
-- General Hammerspoon configurations
hs.window.animationDuration = 0.1
hs.application.enableSpotlightForNameSearches(true)
hs.hints.hintChars = { "J", "K", "L", ";", "A", "S", "D", "F", "H", "G" }
-- Window size and position history
local windowHistory = {}
function windowHistory:save()
local window = hs.window.focusedWindow()
if not self[window:id()] then
self[window:id()] = {}
end
table.insert(self[window:id()], window:frame())
end
function windowHistory:restore()
local window = hs.window.focusedWindow()
if not self[window:id()] then
return
end
if #self[window:id()] > 0 then
window:setFrame(table.remove(self[window:id()]))
end
end
-- Remove window history of no longer existing windows
function windowHistory:clean()
for id in pairs(self) do
if type(id) == "number" and hs.window.get(id) == nil then
self[id] = nil
end
end
end
-- Focus History
-- Remembers when I switch apps/windows so I can back/forward between them
local FOCUS_HISTORY_MAX = 20
local focusHistory = {} -- First element is oldest, last newest
local focusPosition = 1
local filterSub = nil
function focusHistoryPush(window)
local id = window:id()
local idx = hs.fnutils.indexOf(focusHistory, id)
-- Handle when we select a window that exists in history
if idx then
if focusPosition ~= #focusHistory then
-- If we moved back in history and selected a window move the current window to be
-- the most recent one
local currentWindowID = table.remove(focusHistory, focusPosition)
table.insert(focusHistory, currentWindowID)
focusPosition = #focusHistory
end
-- Move the existing window to be to most recent one
table.remove(focusHistory, idx)
table.insert(focusHistory, id)
return
end
if #focusHistory > FOCUS_HISTORY_MAX then
table.remove(focusHistory, 1)
end
table.insert(focusHistory, id)
focusPosition = #focusHistory
end
function focusHistoryBackward()
if focusPosition == 1 then
return
end
focusPosition = focusPosition - 1
local win = hs.window.get(focusHistory[focusPosition])
if not win then
table.remove(focusHistory, focusPosition)
focusHistoryBackward()
return
end
filterSub:pause()
win:focus()
-- Resume after a delay so that the filter won't trigger focusHistory:push
hs.timer.doAfter(1, function() filterSub:resume() end)
end
function focusHistoryForward()
if focusPosition + 1 > #focusHistory then
return
end
focusPosition = focusPosition + 1
local win = hs.window.get(focusHistory[focusPosition])
if not win then
table.remove(focusHistory, focusPosition)
focusHistoryForward()
return
end
filterSub:pause()
hs.window.get(focusHistory[focusPosition]):focus()
-- Resume after a delay so that the filter won't trigger focusHistory:push
hs.timer.doAfter(1, function() filterSub:resume() end)
end
filterSub = hs.window.filter.default:subscribe(hs.window.filter.windowFocused, focusHistoryPush)
-- Clean the window history every 5 minutes to avoid memory leaks
hs.timer.doEvery(hs.timer.minutes(5), function() windowHistory:clean() end)
function isExternal()
return not hs.window.focusedWindow():screen():name():lower():find("built-in", 1, true)
end
-- setFocusedWindow set the current window frame according to the return value of
-- `layoutfn`, which should be a function that takes three arguments:
-- `screen`: The screen's frame
-- `window`: The current window's frame
-- `isExternal`: Whether it's not on the built-in display or not
function setFocusedWindow(layoutfn)
-- Return a function so we could use it easily with `bind()`
return function()
local window = hs.window.focusedWindow()
local screen = window:screen()
-- Fix an issue with FireFox movement
local axApp = hs.axuielement.applicationElement(window:application())
local wasEnhanced = axApp.AXEnhancedUserInterface
axApp.AXEnhancedUserInterface = false
-- Set window
windowHistory:save()
window:setFrame(hs.geometry(layoutfn(screen:frame(), window:frame(), isExternal())))
-- Restore enhancement
hs.timer.doAfter(hs.window.animationDuration * 2, function() axApp.AXEnhancedUserInterface = wasEnhanced end)
end
end
-- Focus an application, toggling between its windows
function focus(appName)
-- Return a function so we could use it easily with `bind()`
return function()
local app = hs.application.get(appName)
if app then
if not app:isFrontmost() then
app:mainWindow():focus()
else
-- Filter windows without title (workaround a Chrome issue)
local windows = hs.fnutils.filter(app:allWindows(), function(w) return w:title() ~= "" end)
if #windows > 1 and app:mainWindow() == windows[1] then
windows[2]:focus()
else
windows[1]:focus()
end
end
end
end
end
-- Bind a key to the hyper key
function bind(key, fn)
hs.hotkey.bind({"ctrl", "alt", "cmd", "shift"}, key, fn)
end
-- Focus History
bind("[", focusHistoryBackward)
bind("]", focusHistoryForward)
-- Hammersppon hints
bind("h", hs.hints.windowHints)
-- Focus specific applications
bind("j", focus("Firefox"))
bind("k", focus("Emacs"))
bind("l", focus("Terminal"))
bind("u", focus("Dash"))
bind("i", focus("Zulip"))
-- Focus a bindable application
local bindableFocus = nil
-- Set the application to focus
bind("8", function() bindableFocus = hs.window.focusedWindow() end)
-- Focus the application
bind("o", function()
if bindableFocus then
bindableFocus:focus()
end
end)
-- Start screensave
hs.hotkey.bind({}, "f20", hs.caffeinate.startScreensaver)
-- Sending windows to different monitors
bind("1", function() hs.window.focusedWindow():centerOnScreen(hs.screen.allScreens()[1], true) end)
bind("2", function() hs.window.focusedWindow():centerOnScreen(hs.screen.allScreens()[2], true) end)
-- Laptop layout ratios
local rightRatio = 0.4443
local leftRatio = 0.5556
-- Restore layout history
bind("z", function() windowHistory:restore() end)
-- Left window
bind("a", setFocusedWindow(function(screen, window, isExternal)
if isExternal then
return {
x = screen.x,
y = screen.y,
w = 1400,
h = screen.h,
}
else
return {
x = screen.x,
y = screen.y,
w = screen.w * leftRatio,
h = screen.h,
}
end
end))
-- Middle window
bind("s", setFocusedWindow(function(screen, window, isExternal)
if isExternal then
return {
x = screen.x + 720,
y = screen.y,
w = 2000,
h = screen.h,
}
else
return {
x = screen.x + 20,
y = screen.y,
w = screen.w - 40,
h = screen.h,
}
end
end))
-- Right window
bind("d", setFocusedWindow(function(screen, window, isExternal)
local large = 2040
local small = 770
if isExternal then
if window.w == large or window.w == large - 2 then -- (- 2) for terminal, it has weird sizing
return {
x = screen.x + screen.w - small,
y = screen.y,
w = small,
h = screen.h
}
else
return {
x = screen.x + screen.w - large,
y = screen.y,
w = large,
h = screen.h,
}
end
else
return {
x = screen.x + screen.w * leftRatio,
y = screen.y,
w = screen.w * rightRatio,
h = screen.h,
}
end
end))
-- Center window
bind("c", setFocusedWindow(function(screen, window)
return {
x = screen.x + (screen.w - window.w) / 2,
y = screen.y + (screen.h - window.h) / 2,
w = window.w,
h = window.h,
}
end))
-- Upper center window
bind("v", setFocusedWindow(function(screen, window)
return {
x = screen.x + (screen.w - window.w) / 2,
y = screen.y + 40,
w = window.w,
h = window.h,
}
end))
-- Finder sized window
bind("f", setFocusedWindow(function(screen, window)
return {
x = window.x,
y = window.y,
h = 640,
w = 600,
}
end))
-- Tall window
bind("w", setFocusedWindow(function(screen, window)
return {
x = window.x,
y = screen.y,
w = window.w,
h = screen.h,
}
end))
-- Kagi Summerizer better position
hs.window.filter.new("Firefox"):subscribe(
hs.window.filter.windowCreated,
function(window, name)
if window:title() == "about:blank -" or window:title() == "Extension: (Kagi Search for Firefox) -" then
-- Most probably Kagi Summerizer
local screen = window:screen():frame()
window:setFrame(hs.geometry({
x=1415,
y=(screen.h - 1060) / 2,
w=600,
h=1060,
}))
end
end
)
-- Application specific configuration
-- A helper function that converts an event to key code: { modifiers.., characters... }
function toKey(event)
local key = {}
for modifier, _ in pairs(event:getFlags()) do
table.insert(key, modifier)
end
table.insert(key, hs.keycodes.map[event:getKeyCode()])
return key
end
-- Swap meta and alt keys in Terminal.app
local swapMeta = hs.eventtap.new({ hs.eventtap.event.types.keyDown }, function(event)
-- Ignore those keys when swaping the meta
local ignoredKeys = hs.fnutils.map({
{ "cmd", "tab" },
{ "cmd", "space" },
{ "alt", "space" },
{ "cmd", "q" },
{ "cmd", "c" },
{ "cmd", "alt", "c" },
{ "cmd", "shift", "c" },
{ "cmd", "," },
{ "cmd", "shift", "4" },
{ "cmd", "shift", "3" },
{ "cmd", "ctrl", "\\" },
{ "ctrl", "cmd", "\\" },
}, table.concat)
local modifiers = event:getFlags()
-- Ignore hyper key presses
if modifiers.cmd and modifiers.alt and modifiers.ctrl and modifiers.shift then
return false, {}
end
-- Ignore keys that are in ignoredKeys
if hs.fnutils.contains(ignoredKeys, table.concat(toKey(event))) then
return false, {}
end
if modifiers.alt then
return true, { event:setFlags({ cmd = true, alt = nil }) }
end
if modifiers.cmd then
return true, { event:setFlags({ cmd = nil, alt = true }) }
end
end)
-- Attach the eventtap to the Terminal app only when it's focused
hs.window.filter.new("Terminal")
:subscribe(hs.window.filter.windowFocused, function() swapMeta:start() end)
:subscribe(hs.window.filter.windowUnfocused, function() swapMeta:stop() end)