-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.lua
364 lines (306 loc) · 9.92 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
hs.loadSpoon("WinWin")
hs.loadSpoon("WindowGrid")
hs.loadSpoon("WindowHalfsAndThirds")
hs.loadSpoon("KSheet")
-- hs.loadSpoon("Seal")
hs.loadSpoon("SpoonInstall")
hs.loadSpoon("AClock")
-- hs.loadSpoon("HCalendar")
hs.loadSpoon("CountDown")
hs.loadSpoon("HSKeybindings")
-- hs.loadSpoon("TimeFlow")
Install=spoon.SpoonInstall
local hyper = {'ctrl', 'cmd'}
local alert = require 'hs.alert'
local application = require 'hs.application'
local geometry = require 'hs.geometry'
local grid = require 'hs.grid'
local hints = require 'hs.hints'
local hotkey = require 'hs.hotkey'
local layout = require 'hs.layout'
local window = require 'hs.window'
local speech = require 'hs.speech'
-- Init speaker.
speaker = speech.new()
-- Init.
hs.window.animationDuration = 0 -- don't waste time on animation when resize window
-- Key to launch application.
local key2App = {
j = {'/Applications/Firefox Developer Edition.app', 'English'},
h = {'/Applications/Visual Studio Code.app', 'English'},
t = {'/System/Applications/Utilities/Terminal.app', 'English'},
l = {'/Applications/PhpStorm.app','English'},
w = {'/Applications/WeChat.app', 'Chinese'},
e = {'/Applications/Safari.app', 'English'},
s = {'/Applications/Sublime Text.app', 'English'},
k = {'/Applications/Sourcetree.app', 'English'},
}
-- Show launch application's keystroke.
local showAppKeystrokeAlertId = ""
local function showAppKeystroke()
if showAppKeystrokeAlertId == "" then
-- Show application keystroke if alert id is empty.
local keystroke = ""
local keystrokeString = ""
for key, app in pairs(key2App) do
keystrokeString = string.format("%-10s%s", key:upper(), app[1]:match("^.+/(.+)$"):gsub(".app", ""))
if keystroke == "" then
keystroke = keystrokeString
else
keystroke = keystroke .. "\n" .. keystrokeString
end
end
showAppKeystrokeAlertId = hs.alert.show(keystroke, hs.alert.defaultStyle, hs.screen.mainScreen(), 10)
else
-- Otherwise hide keystroke alert.
hs.alert.closeSpecific(showAppKeystrokeAlertId)
showAppKeystrokeAlertId = ""
end
end
hs.hotkey.bind(hyper, "z", showAppKeystroke)
-- Build better app switcher.
switcher = hs.window.switcher.new(
hs.window.filter.new()
:setAppFilter('Emacs', {allowRoles = '*', allowTitles = 1}), -- make emacs window show in switcher list
{
showTitles = false, -- don't show window title
thumbnailSize = 200, -- window thumbnail size
showSelectedThumbnail = false, -- don't show bigger thumbnail
backgroundColor = {0, 0, 0, 0.8}, -- background color
highlightColor = {0.3, 0.3, 0.3, 0.8}, -- selected color
}
)
hs.hotkey.bind("alt", "tab", function()
switcher:next()
updateFocusAppInputMethod()
end)
hs.hotkey.bind("alt-shift", "tab", function()
switcher:previous()
updateFocusAppInputMethod()
end)
function updateFocusAppInputMethod()
end
-- Handle cursor focus and application's screen manage.
startAppPath = ""
function applicationWatcher(appName, eventType, appObject)
-- Move cursor to center of application when application activated.
-- Then don't need move cursor between screens.
if (eventType == hs.application.watcher.activated) then
-- Just adjust cursor postion if app open by user keyboard.
if appObject:path() == startAppPath then
spoon.WinWin:centerCursor()
startAppPath = ""
end
end
end
appWatcher = hs.application.watcher.new(applicationWatcher)
appWatcher:start()
function findApplication(appPath)
local apps = application.runningApplications()
for i = 1, #apps do
local app = apps[i]
if app:path() == appPath then
return app
end
end
return nil
end
function launchApp(appPath)
-- We need use Chrome's remote debug protocol that debug JavaScript code in Emacs.
-- So we need launch chrome with --remote-debugging-port argument instead application.launchOrFocus.
if appPath == "/Applications/Google Chrome.app" then
hs.execute("open -a 'Google Chrome' --args '--remote-debugging-port=9222'")
else
application.launchOrFocus(appPath)
end
end
-- Toggle an application between being the frontmost app, and being hidden
function toggleApplication(app)
local appPath = app[1]
local inputMethod = app[2]
-- Tag app path use for `applicationWatcher'.
startAppPath = appPath
local app = findApplication(appPath)
local setInputMethod = true
if not app then
-- Application not running, launch app
launchApp(appPath)
else
-- Application running, toggle hide/unhide
local mainwin = app:mainWindow()
if mainwin then
if app:isFrontmost() then
-- Show mouse circle if has focus on target application.
drawMouseCircle()
mainwin.application():hide()
setInputMethod = false
else
-- Focus target application if it not at frontmost.
mainwin:application():activate(true)
mainwin:application():unhide()
mainwin:focus()
end
else
-- Start application if application is hide.
if app:hide() then
launchApp(appPath)
end
end
end
end
local mouseCircle = nil
local mouseCircleTimer = nil
function drawMouseCircle()
-- Kill previous circle if it still live.
circle = mouseCircle
timer = mouseCircleTimer
if circle then
circle:delete()
if timer then
timer:stop()
end
end
-- Get mouse point.
mousepoint = hs.mouse.getAbsolutePosition()
-- Init circle color and raius.
local color = {
["red"]= 92.0 / 255.0,
["blue"]= 245.0 / 255.0,
["green"]= 201.0 / 255.0,
["alpha"]= 0.8}
raius = 30
-- Draw mouse circle.
circle = hs.drawing.circle(hs.geometry.rect(mousepoint.x - raius / 2, mousepoint.y - raius / 2, raius, raius))
circle:setStroke(false)
circle:setFillColor(color)
circle:bringToFront(true)
circle:show()
-- Save circle in local variable.
mouseCircle = circle
-- Hide mouse circle after 0.5 second.
mouseCircleTimer = hs.timer.doAfter(
0.5,
function()
circle:hide(0.5)
hs.timer.doAfter(0.6, function() circle:delete() end)
end)
end
moveToScreen = function(win, n)
local screens = hs.screen.allScreens()
if n > #screens then
hs.alert.show("No enough screens " .. #screens)
else
local toWin = hs.screen.allScreens()[n]:name()
hs.alert.show("Move " .. win:application():name() .. " to " .. toWin)
hs.layout.apply({{nil, win:title(), toWin, hs.layout.maximized, nil, nil}})
end
end
function resizeToFullWindow()
local win = hs.window.focusedWindow()
local f = win:frame()
local screen = win:screen()
local max = screen:frame()
local winScale = 0.9
f.x = max.x
f.y = max.y
f.w = max.w
f.h = max.h
win:setFrame(f)
end
hs.hotkey.bind(hyper, 'N', resizeToFullWindow)
function resizeToCenter()
local win = hs.window.focusedWindow()
local f = win:frame()
local screen = win:screen()
local max = screen:frame()
local winScale = 0.9
f.x = max.x + (max.w * (1 - winScale) / 2)
f.y = max.y + (max.h * (1 - winScale) / 2)
f.w = max.w * winScale
f.h = max.h * winScale
win:setFrame(f)
end
-- Window operations.
hs.hotkey.bind(hyper, 'U', resizeToCenter)
hs.hotkey.bind(
hyper, "Y",
function()
window.focusedWindow():moveToUnit(layout.left50)
end)
hs.hotkey.bind(
hyper, "R",
function()
window.focusedWindow():moveToUnit(layout.right50)
end)
hs.hotkey.bind(
hyper, ".",
function()
hs.alert.show(string.format("App path: %s\nApp name: %s\nIM source id: %s",
window.focusedWindow():application():path(),
window.focusedWindow():application():name(),
hs.keycodes.currentSourceID()))
end)
hotkey.bind(
hyper, '/',
function()
hints.windowHints()
end)
-- Start or focus application.
for key, app in pairs(key2App) do
hotkey.bind(
hyper, key,
function()
toggleApplication(app)
end)
end
-- Binding key to start plugin.
Install:andUse(
"WindowHalfsAndThirds",
{
config = {use_frame_correctness = true},
hotkeys = {max_toggle = {hyper, "I"}}
})
Install:andUse(
"WindowGrid",
{
config = {gridGeometries = {{"6x4"}}},
hotkeys = {show_grid = {hyper, ","}},
start = true
})
-- Show application keystroke window.
local ksheetIsShow = false
local ksheetAppPath = ""
hs.hotkey.bind(
hyper, "M",
function ()
local currentAppPath = window.focusedWindow():application():path()
-- Toggle ksheet window if cache path equal current app path.
if ksheetAppPath == currentAppPath then
if ksheetIsShow then
spoon.KSheet:hide()
ksheetIsShow = false
else
spoon.KSheet:show()
ksheetIsShow = true
end
-- Show app's keystroke if cache path not equal current app path.
else
spoon.KSheet:show()
ksheetIsShow = true
ksheetAppPath = currentAppPath
end
end)
-- Reload config.
hs.hotkey.bind(
hyper, "'", function ()
speaker:speak("Cover me, reloading...")
hs.reload()
end)
-- We put reload notify at end of config, notify popup mean no error in config.
hs.notify.new({title="NEST", informativeText="Boss, I am online!"}):send()
-- Speak something after configuration success.
speaker:speak("Boss, I am online!")
hs.hotkey.bind(
hyper, "C",function()
spoon.AClock:toggleShow()
end)