Extract hammerspoon into new repo

This commit is contained in:
Henrik Lissner 2016-03-22 18:19:57 -04:00
parent f2e5290bb2
commit ff0422d1d8
7 changed files with 0 additions and 585 deletions

View file

@ -1,5 +0,0 @@
App.new("Finder")
.onActivate(function(self, app)
app:selectMenuItem({"Window", "Bring All to Front"})
end)

View file

@ -1,62 +0,0 @@
local tmux_prefix = {{'ctrl'}, 'c'}
-- Helpers
local function isTmuxWindow()
local win = hs.window.focusedWindow()
if win then
return win:title():find("tmux")
end
return false
end
local function tmuxPressKey(mod, key)
if isTmuxWindow() then
hs.eventtap.keyStroke({'ctrl'}, 'c')
hs.eventtap.keyStroke(mod, key)
return true
end
return false
end
-- App settings
local iterm2
App.new("iTerm2")
.onLaunched(function (self, app)
-- cmd+N for switching tmux windows
for i = 1,9 do
i = tostring(i)
self.bind({'cmd'}, i, function()
if isTmuxWindow() then
os.execute("/usr/local/bin/tmux select-window -t " .. i)
end
end)
end
-- new tmux window
self.bind({'cmd'}, 'n', function()
if not tmuxPressKey({}, 'c') then
app:selectMenuItem({"Shell", "New Window"})
end
end)
-- new window
self.bind({'cmd', 'shift'}, 'n', function()
app:selectMenuItem({"Shell", "New Window"})
end)
-- close pane/window
self.bind({'cmd'}, 'w', function()
if not tmuxPressKey({}, 'x') then
app:selectMenuItem({"Shell", "Close"})
end
end)
-- detach tmux before quitting iterm
self.bind({'cmd'}, 'q', function()
if not tmuxPressKey({}, 'd') then
app:kill()
end
end)
end)
.bind({'cmd', 'shift'}, 'w', util.call(hs.window, 'close')) -- close pane/window

View file

@ -1,32 +0,0 @@
--
commandMode = hs.hotkey.modal.new('cmd', 'escape')
-- Screen overlay
local f = hs.screen.primaryScreen():frame()
local overlay = hs.drawing.rectangle(hs.geometry(0, 0, f.w, f.h + 30))
overlay:setAlpha(0.5)
overlay:setFillColor(hs.drawing.color.black)
overlay:bringToFront()
--
function commandMode:entered() overlay:show() end
function commandMode:exited() overlay:hide() end
-- Command mode keybindings
hs.fnutils.each({
{ '', 'escape', util.call(commandMode, 'exit') },
{ '', 'r', util.reload },
{ 'shift', 'r', function() hs.openConsole(); commandMode:exit() end },
}, function (item)
commandMode:bind(item[1], item[2], item[3])
end)
-- Global keybindings
hs.fnutils.each({
-- { {'cmd', 'ctrl'}, 'up', function() print("UP"); modes.position:enter() end },
-- { {'cmd', 'ctrl'}, 'left', function() print("LEFT"); resizeBRMode:enter() end },
-- { {'cmd', 'ctrl'}, 'right', function() print("RIGHT"); resizeTLMode:enter() end },
}, function (item)
hs.hotkey.bind(item[1], item[2], item[3])
end)

View file

@ -1,19 +0,0 @@
-- Core
util = require('util')
-- Settings
hs.window.animationDuration = 0.1
hs.urlevent.bind("reload", util.reload)
-- Modules
-- require('urls')
-- require('locations')
--
require('modules/apps')
require('modules/windows')
util.autoimport("./apps")
require('bindings')

View file

@ -1,131 +0,0 @@
local apps = {}
-- App-specific code, runs when you launch or switch to another app
App = {}
App.new = function(name)
local self = {}
self.name = name
self.bindings = {}
self.onActivateFn = nil
self.onDeactivateFn = nil
self.onLaunchedFn = nil
self.onTerminatedFn = nil
self.bind = function(mods, key, action, predicate)
table.insert(self.bindings, {key = hs.hotkey.new(mods, key, action),
predicate = predicate})
return self
end
self.onActivate = function(fn)
self.onActivateFn = fn
return self
end
self.onDeactivate = function(fn)
self.onDeactivateFn = fn
return self
end
self.onLaunched = function(fn)
self.onLaunchedFn = fn
local app = hs.application.find(self.name)
if app then
self.launch(app)
if app:isFrontmost() then
self.activate(app)
end
end
return self
end
self.onTerminated = function(fn)
self.onTerminatedFn = fn
return self
end
self.activate = function(appObject)
if type(self.onActivateFn) == 'function' then
self:onActivateFn(appObject)
end
self:enableBinds()
end
self.deactivate = function(appObject)
if type(self.onDeactivateFn) == 'function' then
self:onDeactivateFn(appObject)
end
self:disableBinds()
end
self.launch = function(appObject)
if type(self.onLaunchedFn) == 'function' then
self:onLaunchedFn(appObject)
end
end
self.terminate = function(appObject)
if type(self.onTerminatedFn) == 'function' then
self:onTerminatedFn(appObject)
end
self:clearBinds()
end
self.enableBinds = function(self)
hs.fnutils.each(self.bindings, function(b)
if b.predicate and not b.predicate() then
b.key:disable()
else
b.key:enable()
end
end)
end
self.disableBinds = function(self)
hs.fnutils.each(self.bindings, function(b)
if b and b.key then
b.key:disable()
end
end)
end
self.clearBinds = function(self)
self:disableBinds()
self.bindings = {}
end
apps[name] = self
return self
end
function invoke(keys, predicate)
return function ()
if predicate and not predicate() then
return
end
if type(keys) == 'table' then
hs.fnutils.each(keys, function (key)
if type(key) ~= 'table' then
key = {{}, key}
end
hs.eventtap.keyStroke(key[1], key[2])
end)
elseif type(keys) == 'string' then
os.execute(keys)
elseif type(keys) == 'function' then
return keys()
end
end
end
--
function appGlobalWatcher(appName, eventType, appObject)
if apps[appName] then
if (eventType == hs.application.watcher.activated) then
apps[appName].activate(appObject)
elseif (eventType == hs.application.watcher.deactivated) then
apps[appName].deactivate(appObject)
elseif (eventType == hs.application.watcher.launched) then
apps[appName].launch(appObject)
elseif (eventType == hs.application.watcher.terminated) then
apps[appName].terminate(appObject)
end
end
end
hs.application.watcher.new(appGlobalWatcher):start()

View file

@ -1,302 +0,0 @@
-- TODO: Window management library
local function currentFrame()
return hs.window.focusedWindow():frame()
end
local function currentScreenFrame()
return hs.window.focusedWindow():screen():frame()
end
----------------------------------------
window = {
direction = {
up = 1,
down = 2,
left = 4,
right = 8
},
history = {},
historyLimit = 20,
historyIndex = 1
}
function window:move (f, window, remember)
if window == nil then
window = hs.window.focusedWindow()
end
local oldFrame = window:frame()
if oldFrame ~= f then
window:move(hs.geometry(f.x, f.y, f.w, f.h))
if remember ~= false then
self:remember(oldFrame)
end
end
end
function window:center ()
local s = currentScreenFrame()
local f = currentFrame()
self:move({x = (s.w / 2) - (f.w / 2), y = (s.h / 2) - (f.h / 2), w = f.w, h = f.h})
end
function window:maximize ()
local f = currentFrame()
self:move(currentScreenFrame())
end
function window:slide (direction, amount)
local f = currentFrame()
local s = currentScreenFrame()
if direction == self.direction.up then
f.y = amount == true and s.y or f.y - amount
elseif direction == self.direction.down then
if amount == true then
f.y = s.y + (s.h - f.h)
else
f.y = f.y + amount
end
elseif direction == self.direction.left then
if amount == true then
if f.x == s.x then
hs.window.focusedWindow():moveOneScreenWest()
end
s = currentScreenFrame()
f.x = s.x
else
f.x = f.x - amount
end
elseif direction == self.direction.right then
if amount == true then
if f.x == (s.x + (s.w - f.w)) then
hs.window.focusedWindow():moveOneScreenEast()
end
s = currentScreenFrame()
f.x = s.x + (s.w - f.w)
else
f.x = f.x + amount
end
end
self:move(f)
end
-- TODO Multiple monitor support
function window:resizeIn (direction, amount)
local f = currentFrame()
local s = currentScreenFrame()
if direction == self.direction.up then
if amount == true then
else
f.h = f.h - amount
end
elseif direction == self.direction.down then
-- f.h = amount == true and 10 or f.h - amount
if amount == true then
else
f.h = f.h - amount
f.y = f.y + amount
end
elseif direction == self.direction.left then
if amount == true then
else
f.w = f.w - amount
end
elseif direction == self.direction.right then
if amount == true then
else
f.w = f.w - amount
f.x = f.x + amount
end
end
self:move(f)
end
-- TODO Improve multiple monitor support
function window:resizeOut (direction, amount)
local f = currentFrame()
local s = currentScreenFrame()
if direction == self.direction.up then
if amount == true then
f.h = f.h + f.y
f.y = 0
else
f.h = f.h + amount
f.y = f.y - amount
end
elseif direction == self.direction.down then
if amount == true then
f.h = s.h - f.y
else
f.h = f.h + amount
end
elseif direction == self.direction.left then
if amount == true then
f.w = f.w + f.x
f.x = 0
else
f.w = f.w + amount
f.x = f.x - amount
end
elseif direction == self.direction.right then
-- FIXME If on other monitor, extends past boundaries
if amount == true then
f.w = s.w - f.x
else
f.w = f.w + amount
end
end
self:move(f)
end
function window:undo ()
if #self.history > 0 and self.historyIndex > 0 then
self.historyIndex = self.historyIndex - 1
local win = self.history[self.historyIndex]
win[2]:move(win[1])
else
hs.alert("Can't undo further")
end
end
function window:redo ()
if self.historyIndex < #self.history then
self.historyIndex = self.historyIndex + 1
local win = self.history[self.historyIndex]
win[2]:move(win[1])
else
hs.alert("Can't redo further")
end
end
function window:remember(frame)
if frame ~= self.history[#self.history] then
if self.historyIndex < #self.history then
for i = #self.history, self.historyIndex do
table.remove(self.history, i)
end
end
table.insert(self.history, {frame, hs.window.focusedWindow()})
self.historyIndex = self.historyIndex + 1
if #self.history > self.historyLimit then
table.remove(self.history, 1)
end
end
end
--
local globalMode = hs.hotkey.modal.new()
local modes = {
position = hs.hotkey.modal.new('cmd-ctrl', 'up'),
resizeTL = hs.hotkey.modal.new('cmd-ctrl', 'left'),
resizeBR = hs.hotkey.modal.new('cmd-ctrl', 'right'),
}
local currentMode = nil
-- Screen overlay
local function initOverlay(width, height)
local f = hs.screen.primaryScreen():frame()
return hs.drawing.rectangle(hs.geometry(
(f.w / 2) - (width / 2),
(f.h / 2) - (height / 2),
width, height))
:setAlpha(0.65)
:setFillColor(hs.drawing.color.black)
:bringToFront()
end
local overlay = initOverlay(300, 400)
--
for key, modal in pairs(modes) do
modal:bind('', 'escape', function() modal:exit() end)
modal.entered = function (self)
for k, m in pairs(modes) do
if k ~= key then m:exit() end
end
currentMode = key
globalMode:enter()
overlay:show()
end
modal.exited = function (self)
if currentMode == key then
currentMode = nil
globalMode:exit()
overlay:hide()
end
end
end
local step = 10
local bigstep = 100
globalMode
:bind('', '=', function() window:center() end)
:bind('shift', '=', function() window:maximize() end)
:bind('cmd', 'z', function() window:undo() end)
:bind('cmd-shift', 'z', function() window:redo() end)
modes.position
:bind('', 'up', function() window:slide(window.direction.up, 1) end)
:bind('', 'down', function() window:slide(window.direction.down, 1) end)
:bind('', 'left', function() window:slide(window.direction.left, 1) end)
:bind('', 'right', function() window:slide(window.direction.right, 1) end)
:bind('cmd', 'up', function() window:slide(window.direction.up, true) end)
:bind('cmd', 'down', function() window:slide(window.direction.down, true) end)
:bind('cmd', 'left', function() window:slide(window.direction.left, true) end)
:bind('cmd', 'right', function() window:slide(window.direction.right, true) end)
:bind('shift', 'up', function() window:slide(window.direction.up, step) end)
:bind('shift', 'down', function() window:slide(window.direction.down, step) end)
:bind('shift', 'left', function() window:slide(window.direction.left, step) end)
:bind('shift', 'right', function() window:slide(window.direction.right, step) end)
:bind('alt', 'up', function() window:slide(window.direction.up, bigstep) end)
:bind('alt', 'down', function() window:slide(window.direction.down, bigstep) end)
:bind('alt', 'left', function() window:slide(window.direction.left, bigstep) end)
:bind('alt', 'right', function() window:slide(window.direction.right, bigstep) end)
modes.resizeTL
:bind('', 'up', function() window:resizeOut(window.direction.up, 1) end)
:bind('', 'down', function() window:resizeIn(window.direction.down, 1) end)
:bind('', 'left', function() window:resizeOut(window.direction.left, 1) end)
:bind('', 'right', function() window:resizeIn(window.direction.right, 1) end)
:bind('cmd', 'up', function() window:resizeOut(window.direction.up, true) end)
:bind('cmd', 'down', function() window:resizeIn(window.direction.down, true) end)
:bind('cmd', 'left', function() window:resizeOut(window.direction.left, true) end)
:bind('cmd', 'right', function() window:resizeIn(window.direction.right, true) end)
:bind('shift', 'up', function() window:resizeOut(window.direction.up, step) end)
:bind('shift', 'down', function() window:resizeIn(window.direction.down, step) end)
:bind('shift', 'left', function() window:resizeOut(window.direction.left, step) end)
:bind('shift', 'right', function() window:resizeIn(window.direction.right, step) end)
:bind('alt', 'up', function() window:resizeOut(window.direction.up, bigstep) end)
:bind('alt', 'down', function() window:resizeIn(window.direction.down, bigstep) end)
:bind('alt', 'left', function() window:resizeOut(window.direction.left, bigstep) end)
:bind('alt', 'right', function() window:resizeIn(window.direction.right, bigstep) end)
modes.resizeBR
:bind('', 'up', function() window:resizeIn(window.direction.up, 1) end)
:bind('', 'down', function() window:resizeOut(window.direction.down, 1) end)
:bind('', 'left', function() window:resizeIn(window.direction.left, 1) end)
:bind('', 'right', function() window:resizeOut(window.direction.right, 1) end)
:bind('cmd', 'up', function() window:resizeIn(window.direction.up, true) end)
:bind('cmd', 'down', function() window:resizeOut(window.direction.down, true) end)
:bind('cmd', 'left', function() window:resizeIn(window.direction.left, true) end)
:bind('cmd', 'right', function() window:resizeOut(window.direction.right, true) end)
:bind('shift', 'up', function() window:resizeIn(window.direction.up, step) end)
:bind('shift', 'down', function() window:resizeOut(window.direction.down, step) end)
:bind('shift', 'left', function() window:resizeIn(window.direction.left, step) end)
:bind('shift', 'right', function() window:resizeOut(window.direction.right, step) end)
:bind('alt', 'up', function() window:resizeIn(window.direction.up, bigstep) end)
:bind('alt', 'down', function() window:resizeOut(window.direction.down, bigstep) end)
:bind('alt', 'left', function() window:resizeIn(window.direction.left, bigstep) end)
:bind('alt', 'right', function() window:resizeOut(window.direction.right, bigstep) end)
return window

View file

@ -1,34 +0,0 @@
return {
apps = {},
bindings = {},
ls = function(directory)
local i, t, popen = 0, {}, io.popen
for filename in popen('ls -a "'..directory..'"'):lines() do
i = i + 1
t[i] = filename
end
return t
end,
reload = function()
hs.reload()
hs.alert("Config reloaded!")
end,
call = function(obj, fn)
return function () return obj[fn](obj) end
end,
autoimport = function(dir)
for _, file in pairs(util.ls(dir)) do
if file:find(".+%.lua") then
local a = string.gsub(file, '%.lua', '')
print(a .. " loaded")
require(dir .. "/" .. a)
end
end
end
}