-
Notifications
You must be signed in to change notification settings - Fork 0
/
ratspoon.lua
230 lines (192 loc) · 5.16 KB
/
ratspoon.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
prefix = {
mods={"ctrl"},
key="t",
}
appNameToPreferredNumber = {
Emacs=0,
Safari=1,
Terminal=2,
Spotify=9,
}
windows = {}
prevwindow = nil
squareDrawing = nil
function notify(msg)
hs.alert.show(msg)
end
function log(msg)
print(msg)
end
function deleteSquarePointer()
squareDrawing:delete()
squareDrawing = nil
end
function drawSquarePointer()
if squareDrawing then
deleteSquarePointer()
end
-- Get the current co-ordinates of the mouse pointer
mousepoint = hs.mouse.absolutePosition()
-- Prepare a big red circle around the mouse pointer
squareDrawing = hs.drawing.rectangle(hs.geometry.rect(mousepoint.x-10, mousepoint.y-10, 20, 20))
squareDrawing:setStrokeColor({["red"]=0.478431,["green"]=0.780392,["blue"]=0.960784,["alpha"]=1})
squareDrawing:setFill(false)
squareDrawing:setStrokeWidth(5)
squareDrawing:show()
end
function reloadConfig()
hs.reload()
notify("Configuration was reloaded")
keymapselect:exit()
end
function showWindows()
s = ""
for i = 0, 9 do
window = windows[i]
if window ~= nil then
if s ~= "" then
s = s .. "\n"
end
s = s .. tostring(i) .. " - " .. window:application():name() .. " - " .. window:title()
end
end
notify(s)
keymapselect:exit()
end
function findWindow(window)
local MAXWINDOWS <const> = 9
for i = 0, MAXWINDOWS do
if windows[i] == window then
return i
end
end
return -1
end
function firstAvailableNumber()
i = 0
while windows[i] do
i = i + 1
end
return i
end
function rebind(n)
window = hs.window.focusedWindow()
-- unbind if it's already bound
i = findWindow(window)
if i ~= -1 then
windows[i] = nil
end
-- move window already at this number
if windows[n] then
windows[firstAvailableNumber()] = windows[n]
end
windows[n] = window
notify("Bound " .. window:title() .. " to " .. n)
keymaprebind:exit()
end
function selectwin(w)
focused = hs.window.focusedWindow()
if focused ~= w then
prevwindow = focused
end
w:focus()
end
function selectWinNumber(n)
if windows[n] == nil then
notify(tostring(n) .. " is not bound")
else
selectwin(windows[n])
end
keymapselect:exit()
end
function other()
if prevwindow == nil then
notify("No prev window")
else
selectwin(prevwindow)
end
keymapselect:exit()
end
function prefixAction()
keymapselect:enter()
end
function literal()
-- exit first, otherwise the keyStroke is caught by the keymap again
keymapselect:exit()
hs.hotkey.deleteAll(prefix["mods"], prefix["key"])
hs.eventtap.keyStroke(prefix["mods"], prefix["key"])
hs.hotkey.bind(prefix["mods"], prefix["key"], prefixAction)
keymapselect:bind(prefix["mods"], prefix["key"], other)
end
function enterRebind()
keymapselect:exit()
keymaprebind:enter()
end
function windowCreatedCallback(window, appName, event)
i = firstAvailableNumber()
log("created window for " .. appName .. " is number " .. i)
windows[i] = window
end
function windowDestroyedCallback(window, appName, event)
i = findWindow(window)
if i ~= -1 then
windows[i] = nil
end
log("destroyed window for " .. appName .. " was number " .. i)
end
hs.window.filter.new():subscribe({
windowCreated=windowCreatedCallback,
windowDestroyed=windowDestroyedCallback
})
function bindRunningApps()
windows = {}
for _, app in ipairs(hs.application.runningApplications()) do
for _, window in ipairs(app:allWindows()) do
appName = window:application():name()
-- Skip auto-numbering Finder window that does not exist
if not (appName == "Finder" and window:title() == "") then
number = appNameToPreferredNumber[appName]
if not number then
number = firstAvailableNumber()
end
windows[number] = window
end
end
end
keymapselect:exit()
end
-- Don't bind directly in here because it will trigger on keyup, not
-- keydown. This makes it feel laggy.
keymapselect = hs.hotkey.modal.new()
keymaprebind = hs.hotkey.modal.new()
hs.hotkey.bind(prefix["mods"], prefix["key"], prefixAction)
keymapselect:bind("", "t", literal)
keymapselect:bind(prefix["mods"], prefix["key"], other)
keymapselect:bind("", "w", showWindows)
keymapselect:bind("ctrl", "w", showWindows)
keymapselect:bind("", "r", reloadConfig)
keymapselect:bind("shift", "d", bindRunningApps)
keymapselect:bind("", "escape", function() keymapselect:exit() end)
for i = 0, 9 do
keymapselect:bind("", tostring(i), function() selectWinNumber(i) end)
keymapselect:bind("ctrl", tostring(i), function() selectWinNumber(i) end)
end
keymapselect:bind("", "n", enterRebind)
keymapselect:bind("ctrl", "n", enterRebind)
keymaprebind:bind("", "escape", function() keymaprebind:exit() end)
for i = 0, 9 do
keymaprebind:bind("", tostring(i), function() rebind(i) end)
end
bindRunningApps()
function keymapselect:entered()
drawSquarePointer()
end
function keymapselect:exited()
deleteSquarePointer()
end
function keymaprebind:entered()
drawSquarePointer()
end
function keymaprebind:exited()
deleteSquarePointer()
end