-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.lua
391 lines (350 loc) · 11.1 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
391
hs.application.enableSpotlightForNameSearches(true)
hs.hotkey.bind({"alt"}, "d", function() hs.reload() end)
hs.hotkey.bind({"alt","shift"}, "h", function() hs.hints.windowHints() end)
hs.hints.style = "vimperator"
function frame(fn)
return function()
local win = hs.window.focusedWindow()
local f = win:frame()
local screen = win:screen()
local s = screen:frame()
fn(f, s)
win:setFrame(f)
end
end
for i = 1,9 do
hs.hotkey.bind(
{ "alt" }, tostring(i),
frame(
function(f, s)
f.x = s.x + s.w * (10 - i) / 10
f.w = s.w * i / 10
f.y = s.y
f.h = s.h
end
)
)
hs.hotkey.bind(
{ "alt", "shift" }, tostring(i),
frame(
function(f, s)
f.x = s.x
f.w = s.w * i / 10
f.y = s.y
f.h = s.h
end
)
)
hs.hotkey.bind(
{ "alt", "ctrl" }, tostring(i),
frame(
function(f, s)
f.x = s.x
f.w = s.w
f.y = s.y
f.h = s.h * i / 10
end
)
)
hs.hotkey.bind(
{ "alt", "ctrl", "shift" }, tostring(i),
frame(
function(f, s)
f.x = s.x
f.w = s.w
f.y = s.y + s.h * (10 - i) / 10
f.h = s.h * i / 10
end
)
)
end
hs.hotkey.bind(
{'alt'}, '0',
frame(
function(f, s)
f.x = s.x
f.w = s.w
f.y = s.y
f.h = s.h
end
)
)
M = 10
N = 10
k = hs.hotkey.modal.new('alt', 'a')
function k:entered() hs.alert 'Resize mode' end
function k:exited() hs.alert 'Exited resize mode' end
k:bind('alt', 'a', function() k:exit() end)
k:bind('', 'escape', function() k:exit() end)
k:bind('', 'J', 'Pressed J',function() print 'let the record show that J was pressed' end)
max = math.max
min = math.min
function bind(mods, keys, fn)
for i,key in ipairs(keys) do
k:bind(mods, key, frame(fn))
end
end
function resizeBindings(base, far, k, sk, S)
function extendBase(f, s)
local cur = f[k]
f[k] = max(s[k], f[k] - s[sk] / S)
local increase = cur - f[k]
f[sk] = f[sk] + increase
end
function shrinkBase(f, s)
local cur = f[k]
f[k] = min(s[k] + s[sk], f[k] + s[sk] / S)
local decrease = f[k] - cur
f[sk] = f[sk] - decrease
end
function throwBase(f, s)
local cur = f[k]
f[k] = s[k]
local increase = cur - f[k]
f[sk] = f[sk] + increase
end
function extendFar(f, s) f[sk] = min(s[sk] - (f[k] - s[k]), f[sk] + s[sk] / S) end
function shrinkFar(f, s) f[sk] = max(0, f[sk] - s[sk] / S) end
function throwFar(f, s) f[sk] = s[sk] - (f[k] - s[k]) end
function nudgeBase(f, s) f[k] = max(s[k], f[k] - s[sk] / S) end
function flushBase(f, s) f[k] = s[k] end
function nudgeFar(f, s) f[k] = min(s[sk] - f[sk] + s[k], f[k] + s[sk] / S) end
function flushFar(f, s) f[k] = s[sk] - f[sk] + s[k] end
-- For the "base" (left or top) edge:
-- - default: extend
-- - shift: contract
-- - ctrl: extend to edge
bind('', base, extendBase)
bind('shift', base, shrinkBase)
bind('ctrl', base, throwBase)
-- Same three bindings, but for the "far" edge (right or bottom)
bind('', far, extendFar)
bind('shift', far, shrinkFar)
bind('ctrl', far, throwFar)
-- cmd: "nudge" in the "base" direction
-- cmd+ctrl: move all the way to "base" edge
bind("cmd", base, nudgeBase)
bind({"cmd", "ctrl"}, base, flushBase)
-- Same two "move" bindings, but toward the "far" edge
bind("cmd", far, nudgeFar)
bind({"cmd", "ctrl"}, far, flushFar)
end
resizeBindings({ 'h', 'left' }, { 'l', 'right' }, 'x', 'w', M)
resizeBindings({ 'k', 'up' }, { 'j', 'down' }, 'y', 'h', N)
function map(tbl, f)
local t = {}
for k,v in pairs(tbl) do
t[k] = f(v)
end
return t
end
function mapToArr(tbl, f)
local t = {}
local i = 1
for k,v in pairs(tbl) do
t[i] = f(k, v)
i = i + 1
end
return t
end
function reduce(tbl, f)
local t = nil
for k,v in pairs(tbl) do
if t == nil then
t = v
else
t = f(t, v)
end
end
return t
end
function foldLeft(tbl, z, f)
local t = z
for k,v in pairs(tbl) do
t = f(t, v)
end
return t
end
function str(o)
if type(o) == 'table' then
return '{ '..table.concat(mapToArr(o, function(k, v) return k..': '..str(v) end), ', ')..' }'
else
return tostring(o)
end
end
function bigsmall(big, sml)
return {
[ "Google Chrome" ] = { screen = big, shape = { x=0.2, y=0, w=0.6, h=1 } },
[ "Superhuman" ] = { screen = big, shape = { x=0.2, y=0, w=0.6, h=1 } },
[ "iTerm2" ] = { screen = verticalDell or big, shape = { x=0 , y=0, w=1 , h=1 } },
[ "IntelliJ IDEA" ] = { screen = big, shape = { x=0.4, y=0, w=0.6, h=1 } },
[ "Code" ] = { screen = big, shape = { x=0.4, y=0, w=0.6, h=1 } },
[ "Slack" ] = { screen = big, shape = { x=0.4, y=0, w=0.6, h=1 } },
[ "Microsoft Teams" ] = { screen = big, shape = { x=0.4, y=0, w=0.6, h=1 } },
[ "Gitter" ] = { screen = big, shape = { x=0.4, y=0, w=0.6, h=1 } },
[ "Mail" ] = { screen = big, shape = { x=0.4, y=0, w=0.6, h=1 } },
[ "Safari" ] = { screen = big, shape = { x=0.4, y=0, w=0.6, h=1 } },
[ "Calendar" ] = { screen = big, shape = { x=0.4, y=0, w=0.6, h=1 } },
[ "Signal" ] = { screen = sml, shape = { x=0 , y=0, w=1 , h=1 } },
[ "WhatsApp" ] = { screen = sml, shape = { x=0 , y=0, w=1 , h=1 } },
}
end
function laptopOnly(laptop)
local right = { x=0.1, y=0, w=0.9, h=1 }
local full = { x=0 , y=0, w=1 , h=1 }
return {
[ "Google Chrome" ] = { screen = laptop, shape = right },
[ "Superhuman" ] = { screen = laptop, shape = right },
[ "iTerm2" ] = { screen = laptop, shape = full },
[ "IntelliJ IDEA" ] = { screen = laptop, shape = right },
[ "Code" ] = { screen = laptop, shape = right },
[ "Slack" ] = { screen = laptop, shape = right },
[ "Microsoft Teams" ] = { screen = laptop, shape = right },
[ "Gitter" ] = { screen = laptop, shape = right },
[ "Mail" ] = { screen = laptop, shape = right },
[ "Safari" ] = { screen = laptop, shape = right },
[ "Calendar" ] = { screen = laptop, shape = right },
[ "Signal" ] = { screen = laptop, shape = full },
[ "WhatsApp" ] = { screen = laptop, shape = full },
}
end
function layout(map)
hs.layout.apply(
mapToArr(
map,
function(k, v)
return { k, nil, v.screen, v.shape, nil, nil }
end
)
)
end
function screenstrs()
local screens = hs.screen.allScreens()
local names = map(screens, function(s) return s:name() end)
print("screens: "..str(names))
return table.concat(names, ',')
end
function msg(m)
print(m)
hs.alert(m)
end
function findscreen(...)
if type(...) == 'string' then
local screen = hs.screen.find(...)
if screen ~= nil then
return screen
end
elseif type(...) == 'array' then
for s in ... do
local screen = hs.screen.find(name)
if screen ~= nil then
return screen
end
end
else
for _, name in pairs(...) do
print('\tchecking name: '..name)
local screen = hs.screen.find(name:lower())
if screen ~= nil then
print('\t\tfound screen: '..name)
return screen
end
end
end
msg("Couldn't find screens: "..str(...).." ("..type(...).."; available: "..screenstrs()..")")
end
hs.hotkey.bind(
{ 'cmd', 'ctrl' }, 'm',
function()
local laptop = findscreen({'Color LCD', 'Built%-in Retina Display'})
-- local lg = findscreen({'LG ULTRAWIDE', 'DELL U2715H'})
local lg = findscreen({'LG ULTRAWIDE', 'LG HDR WFHD'})
if laptop == nil or lg == nil then
hs.alert('missing a screen; laptop: '..(laptop and laptop:name() or '??')..', LG: '..(lg and lg:name() or '??'))
return nil
end
layout(bigsmall(lg, laptop))
end
)
hs.hotkey.bind(
{ 'cmd', 'ctrl' }, 'l',
function()
local laptop = findscreen('Color LCD') or findscreen('Built-in Retina Display')
if laptop == nil then return nil end
layout(laptopOnly(laptop))
end
)
function focusApp(name)
local app = hs.application.find(name)
if app == nil then
hs.alert('Opening '..name)
hs.application.open(name, 0, true)
return
end
local focused = app:focusedWindow()
if focused == nil then
hs.alert("No focused window for "..name)
local windows = app:allWindows()
if #windows > 0 then
windows[1]:focus()
else
hs.alert("No windows found for "..name)
return
end
else
focused:focus()
end
end
function bindFocusApp(name, modifiers, hotkey)
hs.hotkey.bind(modifiers, hotkey, function() focusApp(name) end)
end
appShortcuts = {
[ "Activity Monitor" ] = { modifiers = { 'alt', 'cmd' }, key = 'a' },
[ "Google Chrome" ] = { modifiers = { 'alt', 'cmd' }, key = 'c' },
[ "Safari" ] = { modifiers = { 'alt', 'cmd' }, key = 's' },
[ "iTerm" ] = { modifiers = 'alt' , key = 't' },
[ "Chat GPT" ] = { modifiers = 'alt' , key = 'g' },
[ "Superhuman" ] = { modifiers = 'alt' , key = 'h' },
[ "com.jetbrains.intellij" ] = { modifiers = 'alt' , key = 'i' },
[ "IntelliJ IDEA" ] = { modifiers = { 'alt', 'shift' }, key = 'i' },
[ "VIP Access" ] = { modifiers = 'alt' , key = 'j' },
[ "Slack" ] = { modifiers = 'alt' , key = 'k' },
[ "CLion" ] = { modifiers = 'alt' , key = 'l' },
[ "Cursor" ] = { modifiers = 'alt' , key = 'o' },
[ "Microsoft Teams" ] = { modifiers = 'alt' , key = 'm' },
[ "Signal" ] = { modifiers = 'alt' , key = 'n' },
[ "Final Cut Pro" ] = { modifiers = 'alt' , key = 'p' },
[ "Quicktime Player" ] = { modifiers = 'alt' , key = 'q' },
[ "RStudio" ] = { modifiers = 'alt' , key = 'r' },
[ "System Preferences" ] = { modifiers = 'alt' , key = 's' },
[ "Preview" ] = { modifiers = 'alt' , key = 'v' },
[ "WhatsApp" ] = { modifiers = 'alt' , key = 'w' },
[ "zoom.us" ] = { modifiers = 'alt' , key = 'z' },
}
-- Handle Finder specially; open a new window if none exist
hs.hotkey.bind({ 'cmd', 'alt' }, 'f',
function()
local finder = hs.application.find('Finder')
local window = finder:focusedWindow()
if window == nil then
print 'applescript…'
hs.applescript([[
tell application "System Events"
tell process "Finder"
set frontmost to true
click menu item "New Finder Window" of menu "File" of menu bar 1
end tell
end tell
]])
else
window:focus()
end
end
)
mapToArr(
appShortcuts,
function(name, key)
bindFocusApp(name, key.modifiers, key.key)
end
)
hs.alert.show("Config loaded: "..hs.screen.mainScreen():name())