Skip to content

Instantly share code, notes, and snippets.

@dalejung
Created May 26, 2016 14:34
Show Gist options
  • Save dalejung/2497fd7179ff645177ea40a986901627 to your computer and use it in GitHub Desktop.
Save dalejung/2497fd7179ff645177ea40a986901627 to your computer and use it in GitHub Desktop.
ma hammerspoon
local hyper = {"cmd", "shift",}
function handles_focus(win)
local app = win:application()
local app_name = app:title()
if app_name ~= 'iTerm2' then
return false
end
if string.match(win:title(), "(tmux)") then
return true
end
end
local focus_map = {
["D"] = 'focusWindowSouth',
["U"] = 'focusWindowNorth',
["L"] = 'focusWindowWest',
["R"] = 'focusWindowEast',
}
function at_edge(direction)
local command = "export PATH=/usr/local/bin:$PATH; ~/.tmux/plugins/tmux-select-pane-no-wrap/scripts/at_edge.sh " .. direction
local handle = io.popen(command)
local result = handle:read("*a")
handle:close()
if tonumber(result) == 1 then
return true
end
end
-- The tmux bindings should use different keys than the hammerspoon
-- bindings. https://groups.google.com/forum/#!topic/hammerspoon/yp4AvJr5v7Q
-- Instead of having a delay, i just used an alternate hotkey set just for
-- hammerspoon -> tmux integration.
local tmux_focus_map = {
["U"] = '[',
["D"] = ']',
["L"] = '\\',
["R"] = 'X',
}
function focus_mover(direction)
local win = hs.window.focusedWindow()
if handles_focus(win) then
if not at_edge(direction) then
local tmux_hotkey = tmux_focus_map[direction]
hs.eventtap.keyStroke({'ctrl'}, 'a')
hs.eventtap.keyStroke({'ctrl'}, tmux_hotkey)
return
end
end
local meth = focus_map[direction]
hs.window[meth](win)
end
hs.hotkey.bind(hyper, 'k', function()
focus_mover('U')
end)
hs.hotkey.bind(hyper, 'j', function()
focus_mover('D')
end)
hs.hotkey.bind(hyper, 'l', function()
focus_mover('R')
end)
hs.hotkey.bind(hyper, 'h', function()
focus_mover('L')
end)
-- Hyper i to show window hints
-----------------------------------------------
hs.hotkey.bind(hyper, "i", function()
hs.hints.windowHints()
end)
-----------------------------------------------
-- Reload config on write
-----------------------------------------------
function reload_config(files)
hs.reload()
end
hs.pathwatcher.new(os.getenv("HOME") .. "/.hammerspoon/", reload_config):start()
hs.alert.show("Config loaded")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment