Repository navigation
Expand file tree
/
Copy pathhammerspoon_init.lua
More file actions
179 lines (153 loc) · 3.73 KB
/
Copy pathhammerspoon_init.lua
File metadata and controls
179 lines (153 loc) · 3.73 KB
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
local laptopScreen = "Built-in Retina Display"
hs.window.animationDuration = 0
local function getSelectedText(app)
local appElement = hs.axuielement.applicationElement(app)
local focusedElement = appElement:attributeValue("AXFocusedUIElement")
if focusedElement then
return focusedElement:attributeValue("AXSelectedText")
end
return nil
end
hs.hotkey.bind({"cmd", "alt"}, "P", function()
local win = hs.window.focusedWindow()
local title = win:title()
local app = win:application()
local appname = app:name()
local text = getSelectedText(app)
if appname == "Terminal" then
hs.alert.show(win:title())
else
hs.alert.show(appname)
end
-- Test by highlighting one of the below lines and hitting cmd-alt-p.
-- The url selected should be opened in Safari.
-- http://somethingdoneright.net
-- http://google.com
if text then
local url = string.find(text, "https?://")
if url then
hs.urlevent.openURLWithBundle(text, "com.apple.Safari")
return
end
end
end)
hs.hotkey.bind({"cmd", "alt"}, "F", function()
local win = hs.window.focusedWindow()
win:setFrame(win:screen():frame())
end)
hs.hotkey.bind({"cmd", "alt"}, "H", function()
local win = hs.window.focusedWindow()
send("left", win)
end)
hs.hotkey.bind({"cmd", "alt"}, "L", function()
local win = hs.window.focusedWindow()
send("right", win)
end)
hs.hotkey.bind({"cmd", "ctrl"}, "[", function()
local win = hs.window.focusedWindow()
sendToStack("left", win)
end)
hs.hotkey.bind({"cmd", "ctrl"}, "]", function()
local win = hs.window.focusedWindow()
sendToStack("right", win)
end)
-- "left"|"right" -> index -> win
stacks = {left={}, right={}}
function renderStacks()
for name, s in pairs(stacks) do
renderStack(s, name)
end
end
function send(name, win)
local screen = win:screen()
local max = screen:frame()
-- if a window is in the other stack, remove it.
_, other = selectStack(name)
if contains(other, win) then
table.remove(other, find(win, other))
renderStacks()
end
local f = win:frame()
local leftPad = 0
if name == "right" then
leftPad = (max.w / 2)
end
f.x = max.x + leftPad
f.y = max.y
f.w = max.w / 2
f.h = max.h
win:setFrame(f)
end
function sendToStack(name, win)
stack, other = selectStack(name)
-- if a window is in the other stack, remove it.
if contains(other, win) then
table.remove(other, find(win, other))
end
-- if the window is already in this stack, cycle it.
if contains(stack, win) then
shift(stack)
else
-- otherwise, add it to the stack.
table.insert(stack, win)
end
renderStacks()
end
function renderStack(stack, name)
local count = #stack
for i, win in pairs(stack) do
local f = win:frame()
local screen = win:screen()
local max = screen:frame()
local leftPad = 0
if name == "right" then
leftPad = (max.w / 2)
end
local h = max.h / count
f.x = max.x + leftPad
f.y = max.y + (h * (i-1))
f.w = max.w / 2
f.h = h
win:setFrame(f)
end
end
-- returns stack, other
function selectStack(name)
local stacks = shallowcopy(stacks)
local stack = remove(stacks, name)
local other = stacks[next(stacks)]
return stack, other
end
function contains(stack, win)
for i, v in pairs(stack) do
if v == win then
return true
end
end
return false
end
function find(win, stack)
for i, w in pairs(stack) do
if w == win then
return i
end
end
return -1
end
function shift(stack)
first = table.remove(stack, 1)
table.insert(stack, first)
return stack
end
function shallowcopy(table)
local result = {}
for k, v in pairs(table) do
result[k] = v
end
return result
end
function remove(table, key)
v = table[key]
table[key] = nil
return v
end