-
Notifications
You must be signed in to change notification settings - Fork 5
/
init.lua
165 lines (135 loc) · 4.48 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
local application = require "hs.application"
local fnutils = require "hs.fnutils"
local grid = require "hs.grid"
local hotkey = require "hs.hotkey"
local mjomatic = require "hs.mjomatic"
local window = require "hs.window"
grid.MARGINX = 0
grid.MARGINY = 0
grid.GRIDHEIGHT = 13
grid.GRIDWIDTH = 13
local mash = {"cmd", "alt", "ctrl"}
local mashshift = {"cmd", "alt", "ctrl", "shift"}
--
-- replace caffeine
--
local caffeine = hs.menubar.new()
function setCaffeineDisplay(state)
local result
if state then
result = caffeine:setIcon("caffeine-on.pdf")
else
result = caffeine:setIcon("caffeine-off.pdf")
end
end
function caffeineClicked()
setCaffeineDisplay(hs.caffeinate.toggle("displayIdle"))
end
if caffeine then
caffeine:setClickCallback(caffeineClicked)
setCaffeineDisplay(hs.caffeinate.get("displayIdle"))
end
hs.hotkey.bind(mash, "/", function() caffeineClicked() end)
--
-- /replace caffeine
--
--
-- toggle push window to edge and restore to screen
--
-- somewhere to store the original position of moved windows
local origWindowPos = {}
-- cleanup the original position when window restored or closed
local function cleanupWindowPos(_,_,_,id)
origWindowPos[id] = nil
end
-- function to move a window to edge or back
local function movewin(direction)
local win = hs.window.focusedWindow()
local res = hs.screen.mainScreen():frame()
local id = win:id()
if not origWindowPos[id] then
-- move the window to edge if no original position is stored in
-- origWindowPos for this window id
local f = win:frame()
origWindowPos[id] = win:frame()
-- add a watcher so we can clean the origWindowPos if window is closed
local watcher = win:newWatcher(cleanupWindowPos, id)
watcher:start({hs.uielement.watcher.elementDestroyed})
if direction == "left" then f.x = (res.w - (res.w * 2)) + 10 end
if direction == "right" then f.x = (res.w + res.w) - 10 end
if direction == "down" then f.y = (res.h + res.h) - 10 end
win:setFrame(f)
else
-- restore the window if there is a value for origWindowPos
win:setFrame(origWindowPos[id])
-- and clear the origWindowPos value
cleanupWindowPos(_,_,_,id)
end
end
hs.hotkey.bind(mash, "A", function() movewin("left") end)
hs.hotkey.bind(mash, "D", function() movewin("right") end)
hs.hotkey.bind(mash, "S", function() movewin("down") end)
--
-- /toggle push window to edge and restore to screen
--
--
-- Open Applications
--
local function openchrome()
application.launchOrFocus("Google Chrome")
end
local function openff()
application.launchOrFocus("FirefoxDeveloperEdition")
end
local function openmail()
application.launchOrFocus("Airmail Beta")
end
hotkey.bind(mash, 'F', openff)
hotkey.bind(mash, 'C', openchrome)
hotkey.bind(mash, 'M', openmail)
--
-- /Open Applications
--
--
-- Window management
--
--Alter gridsize
hotkey.bind(mashshift, '=', function() grid.adjustHeight( 1) end)
hotkey.bind(mashshift, '-', function() grid.adjustHeight(-1) end)
hotkey.bind(mash, '=', function() grid.adjustWidth( 1) end)
hotkey.bind(mash, '-', function() grid.adjustWidth(-1) end)
--Snap windows
hotkey.bind(mash, ';', function() grid.snap(window.focusedWindow()) end)
hotkey.bind(mash, "'", function() fnutils.map(window.visibleWindows(), grid.snap) end)
-- hotkey.bind(mashshift, 'H', function() window.focusedWindow():focusWindowWest() end)
-- hotkey.bind(mashshift, 'L', function() window.focusedWindow():focusWindowEast() end)
-- hotkey.bind(mashshift, 'K', function() window.focusedWindow():focusWindowNorth() end)
-- hotkey.bind(mashshift, 'J', function() window.focusedWindow():focusWindowSouth() end)
--Move windows
hotkey.bind(mash, 'DOWN', grid.pushWindowDown)
hotkey.bind(mash, 'UP', grid.pushWindowUp)
hotkey.bind(mash, 'LEFT', grid.pushWindowLeft)
hotkey.bind(mash, 'RIGHT', grid.pushWindowRight)
--resize windows
hotkey.bind(mashshift, 'UP', grid.resizeWindowShorter)
hotkey.bind(mashshift, 'DOWN', grid.resizeWindowTaller)
hotkey.bind(mashshift, 'RIGHT', grid.resizeWindowWider)
hotkey.bind(mashshift, 'LEFT', grid.resizeWindowThinner)
hotkey.bind(mash, 'N', grid.pushWindowNextScreen)
hotkey.bind(mash, 'P', grid.pushWindowPrevScreen)
-- hotkey.bind(mashshift, 'M', grid.maximizeWindow)
--
-- /Window management
--
--
-- Monitor and reload config when required
--
function reload_config(files)
caffeine:delete()
hs.reload()
end
hs.pathwatcher.new(os.getenv("HOME") .. "/.hammerspoon/", reload_config):start()
hs.alert.show("Config loaded")
--
-- /Monitor and reload config when required
--