-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathhammerspoon-init.lua
315 lines (280 loc) · 12.8 KB
/
hammerspoon-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
-------------------------------------------------------------------
-- Globals
-------------------------------------------------------------------
hs.window.animationDuration = 0
-------------------------------------------------------------------
-- ControlEscape
-------------------------------------------------------------------
hs.loadSpoon('ControlEscape'):start()
-------------------------------------------------------------------
-- Events
--
-- I just use this to explore some possibilities, but have never
-- actually done anything with it, so all it does "for real" is
-- return false
-------------------------------------------------------------------
hs.eventtap.new({ hs.eventtap.event.types.flagsChanged }, function(e)
return false
end):start()
-------------------------------------------------------------------
-- ControlEscape
--
-- Neat little plug to allow me to use the CONTROL key (which is
-- actually my CAPSLOCK key, after mapping it using the standard
-- Mac keyboard preferences) to mimic the ESCAPE key.
-------------------------------------------------------------------
controlescape = hs.loadSpoon('ControlEscape'):start()
-------------------------------------------------------------------
-- Vim Mode
--
-- Not sure I like this one... but I'm still trying it out.
-------------------------------------------------------------------
vim = hs.loadSpoon('VimMode')
hs.hotkey.bind({'ctrl'}, ';', function()
vim:enter()
end)
-------------------------------------------------------------------
-- Window Layouts
-------------------------------------------------------------------
-- These are just convenient names for layouts. We can use numbers
-- between 0 and 1 for defining 'percentages' of screen real estate
-- so 'right30' is the window on the right of the screen where the
-- vertical split (x-axis) starts at 70% of the screen from the
-- left, and is 30% wide.
--
-- And so on...
units = {
right30 = { x = 0.70, y = 0.00, w = 0.30, h = 1.00 },
right70 = { x = 0.30, y = 0.00, w = 0.70, h = 1.00 },
left70 = { x = 0.00, y = 0.00, w = 0.70, h = 1.00 },
left30 = { x = 0.00, y = 0.00, w = 0.30, h = 1.00 },
top50 = { x = 0.00, y = 0.00, w = 1.00, h = 0.50 },
bot50 = { x = 0.00, y = 0.50, w = 1.00, h = 0.50 },
bot80 = { x = 0.00, y = 0.20, w = 1.00, h = 0.80 },
bot87 = { x = 0.00, y = 0.20, w = 1.00, h = 0.87 },
bot90 = { x = 0.00, y = 0.20, w = 1.00, h = 0.90 },
upright30 = { x = 0.70, y = 0.00, w = 0.30, h = 0.50 },
botright30 = { x = 0.70, y = 0.50, w = 0.30, h = 0.50 },
upleft70 = { x = 0.00, y = 0.00, w = 0.70, h = 0.50 },
botleft70 = { x = 0.00, y = 0.50, w = 0.70, h = 0.50 },
right70top80 = { x = 0.70, y = 0.00, w = 0.30, h = 0.80 },
maximum = { x = 0.00, y = 0.00, w = 1.00, h = 1.00 },
center = { x = 0.20, y = 0.10, w = 0.60, h = 0.80 }
}
-- There is a 'layout' plugin but it was more difficult for me to
-- understand than it was for me to just write my own, so this is
-- my definitions for defining the layouts for all of the apps
-- that I tend to use.
layouts = {
alternatecoding = {
{ name = 'Firefox', unit = units.left70 },
{ name = 'VimR', unit = units.left30 },
{ name = 'iTerm2', unit = units.right70 }
},
alternatework = {
{ name = 'Firefox', unit = units.left70, screen = 'Thunderbolt Display' },
{ name = 'VimR', unit = units.left30, screen = 'Thunderbolt Display' },
{ name = 'iTerm2', unit = units.right70, screen = 'Thunderbolt Display' },
{ name = 'Slack', unit = units.bot87, screen = 'Color LCD' },
{ name = 'Microsoft Outlook', unit = units.maximum, screen = 'Color LCD' },
{ name = 'WhatsApp', unit = units.center, screen = 'Color LCD' },
{ name = 'Google Chrome', unit = units.right70, screen = 'Color LCD' }
},
coding = {
{ name = 'Firefox', unit = units.left70 },
{ name = 'VimR', unit = units.left70 },
{ name = 'iTerm2', unit = units.right30 }
},
-- I'll use 'work' as my example. If I want to position the windows of
-- all of these applications, then I simply specify 'layouts.work' and
-- then the layout engine will move all of the windows for these apps to
-- the right monitor and in the right position on that monitor.
work = {
{ name = 'Firefox', unit = units.left70, screen = 'Thunderbolt Display' },
{ name = 'VimR', unit = units.left70, screen = 'Thunderbolt Display' },
{ name = 'iTerm2', unit = units.right30, screen = 'Thunderbolt Display' },
{ name = 'Slack', unit = units.bot87, screen = 'Color LCD' },
{ name = 'Microsoft Outlook', unit = units.maximum, screen = 'Color LCD' },
{ name = 'WhatsApp', unit = units.center, screen = 'Color LCD' },
{ name = 'Google Chrome', unit = units.right70, screen = 'Color LCD' }
},
writing = {
{ name = 'Firefox', unit = units.left70 },
{ name = 'VimR', unit = units.left70 },
{ name = 'iTerm2', unit = units.right30 },
{ name = 'Skim', unit = units.right70top80 }
}
}
-- Tells me whether or not the machine running Hammerspoon is
-- my work machine or not
function isWorkMachine()
local allNames = hs.host.names()
for i=1,#allNames do
if allNames[i]:match('^cawl') then
return true
end
end
return false
end
-- Takes a layout definition (e.g. 'layouts.work') and iterates through
-- each application definition, laying it out as speccified
function runLayout(layout)
for i = 1,#layout do
local t = layout[i]
local win = hs.application.get(t.name):mainWindow()
local screen = nil
if t.screen ~= nil then
screen = hs.screen.find(t.screen)
end
win:move(t.unit, screen, true)
end
end
-- All of the mappings for moving the window of the 'current' application
-- to the right spot. Tries to map 'vim' keys as much as possible, but
-- deviates to a 'visual' representation when that's not possible.
mash = { 'shift', 'ctrl', 'cmd' }
hs.hotkey.bind(mash, 'l', function() hs.window.focusedWindow():move(units.right30, nil, true) end)
hs.hotkey.bind(mash, 'h', function() hs.window.focusedWindow():move(units.left70, nil, true) end)
hs.hotkey.bind(mash, 'k', function() hs.window.focusedWindow():move(units.top50, nil, true) end)
hs.hotkey.bind(mash, 'j', function() hs.window.focusedWindow():move(units.bot50, nil, true) end)
hs.hotkey.bind(mash, ']', function() hs.window.focusedWindow():move(units.upright30, nil, true) end)
hs.hotkey.bind(mash, '[', function() hs.window.focusedWindow():move(units.upleft70, nil, true) end)
hs.hotkey.bind(mash, ';', function() hs.window.focusedWindow():move(units.botleft70, nil, true) end)
hs.hotkey.bind(mash, "'", function() hs.window.focusedWindow():move(units.botright30, nil, true) end)
hs.hotkey.bind(mash, 'm', function() hs.window.focusedWindow():move(units.maximum, nil, true) end)
-- The goal here is to bind mash+0 and mash+9 to the grander layouts
-- but there's a quick difference depending on whether or not I happen
-- to be on my work machine or not
if isWorkMachine() then
hs.hotkey.bind(mash, '0', function() runLayout(layouts.work) end)
hs.hotkey.bind(mash, '9', function() runLayout(layouts.alternatework) end)
else
hs.hotkey.bind(mash, '0', function() runLayout(layouts.coding) end)
hs.hotkey.bind(mash, '9', function() runLayout(layouts.alternatecoding) end)
end
hs.hotkey.bind(mash, '8', function() runLayout(layouts.writing) end)
-------------------------------------------------------------------
-- Key Remapper
--
-- Doesn't work.
-------------------------------------------------------------------
-- local function pressFn(mods, key)
-- if key == nil then
-- key = mods
-- mods = {}
-- end
--
-- return function() hs.eventtap.keyStroke(mods, key, 1000) end
-- end
--
-- local function remap(mods, key, pressFn)
-- hs.hotkey.bind(mods, key, pressFn, nil, pressFn)
-- end
-------------------------------------------------------------------
-- Launcher
--
-- This is the awesome. The other stuff is all cool, but this is the
-- thing I love the most because it reduces the amount of time I
-- spend with the mouse, and is far more deterministic than trying
-- to use cmd+tab.
--
-- The idea here is to have a MODE-BASED app launching and app
-- switching system. Traditional Mac philosophy (and Emacs :D)
-- would have us contort our hands into crazy combinations of keys
-- to manipulate the state of the machine, which is a serious pain
-- in the ass. Using Hammerspoon we can avoid that.
--
-- * ctrl+space gets us into "launch mode"
-- * In "launch mode" the keyboard changes so that each key can now
-- have a new meaning. For example, the 'v' key is now responsible
-- for either launching or switching to VimR
-- * You can then map whatever you like to whatever function you'd
-- like to invoke.
--
-- It's just a big pile of awesome.
-------------------------------------------------------------------
-- We need to store the reference to the alert window
local appLauncherAlertWindow = nil
-- This is the key mode handle
local launchMode = hs.hotkey.modal.new({}, nil, '')
-- Leaves the launch mode, returning the keyboard to its normal
-- state, and closes the alert window, if it's showing
function leaveMode()
if appLauncherAlertWindow ~= nil then
hs.alert.closeSpecific(appLauncherAlertWindow, 0)
appLauncherAlertWindow = nil
end
launchMode:exit()
end
-- So simple, so awesome.
function switchToApp(app)
hs.application.open(app)
leaveMode()
end
-- Enters launch mode. The bulk of this is geared toward
-- showing a big ugly window that can't be ignored; the
-- keyboard is now in launch mode.
hs.hotkey.bind({ 'ctrl' }, 'space', function()
launchMode:enter()
appLauncherAlertWindow = hs.alert.show('App Launcher Mode', {
strokeColor = hs.drawing.color.x11.orangered,
fillColor = hs.drawing.color.x11.cyan,
textColor = hs.drawing.color.x11.black,
strokeWidth = 20,
radius = 30,
textSize = 128,
fadeInDuration = 0,
atScreenEdge = 2
}, 'infinite')
end)
-- When in launch mode, hitting ctrl+space again leaves it
launchMode:bind({ 'ctrl' }, 'space', function() leaveMode() end)
-- Mapped keys
launchMode:bind({}, 'c', function() switchToApp('Google Chrome.app') end)
launchMode:bind({}, 'f', function() switchToApp('Firefox.app') end)
launchMode:bind({}, 'k', function() switchToApp('Skim.app') end)
launchMode:bind({}, 'l', function() switchToApp('VLC.app') end)
launchMode:bind({}, 'o', function() switchToApp('Microsoft Outlook.app') end)
launchMode:bind({}, 's', function() switchToApp('Slack.app') end)
launchMode:bind({}, 't', function() switchToApp('iTerm.app') end)
launchMode:bind({}, 'v', function() switchToApp('VimR.app') end)
launchMode:bind({}, 'w', function() switchToApp('WhatsApp.app') end)
launchMode:bind({}, '`', function() hs.reload(); leaveMode() end)
-- Unmapped keys
launchMode:bind({}, 'a', function() leaveMode() end)
launchMode:bind({}, 'b', function() leaveMode() end)
launchMode:bind({}, 'd', function() leaveMode() end)
launchMode:bind({}, 'e', function() leaveMode() end)
launchMode:bind({}, 'g', function() leaveMode() end)
launchMode:bind({}, 'h', function() leaveMode() end)
launchMode:bind({}, 'i', function() leaveMode() end)
launchMode:bind({}, 'j', function() leaveMode() end)
launchMode:bind({}, 'm', function() leaveMode() end)
launchMode:bind({}, 'n', function() leaveMode() end)
launchMode:bind({}, 'p', function() leaveMode() end)
launchMode:bind({}, 'q', function() leaveMode() end)
launchMode:bind({}, 'r', function() leaveMode() end)
launchMode:bind({}, 'u', function() leaveMode() end)
launchMode:bind({}, 'x', function() leaveMode() end)
launchMode:bind({}, 'y', function() leaveMode() end)
launchMode:bind({}, 'z', function() leaveMode() end)
launchMode:bind({}, '1', function() leaveMode() end)
launchMode:bind({}, '2', function() leaveMode() end)
launchMode:bind({}, '3', function() leaveMode() end)
launchMode:bind({}, '4', function() leaveMode() end)
launchMode:bind({}, '5', function() leaveMode() end)
launchMode:bind({}, '6', function() leaveMode() end)
launchMode:bind({}, '7', function() leaveMode() end)
launchMode:bind({}, '8', function() leaveMode() end)
launchMode:bind({}, '9', function() leaveMode() end)
launchMode:bind({}, '0', function() leaveMode() end)
launchMode:bind({}, '-', function() leaveMode() end)
launchMode:bind({}, '=', function() leaveMode() end)
launchMode:bind({}, '[', function() leaveMode() end)
launchMode:bind({}, ']', function() leaveMode() end)
launchMode:bind({}, '\\', function() leaveMode() end)
launchMode:bind({}, ';', function() leaveMode() end)
launchMode:bind({}, "'", function() leaveMode() end)
launchMode:bind({}, ',', function() leaveMode() end)
launchMode:bind({}, '.', function() leaveMode() end)
launchMode:bind({}, '/', function() leaveMode() end)