From 417ec78a1d4e12dfb1d21fe2714cd1d24fc050d1 Mon Sep 17 00:00:00 2001 From: Henrik Lissner Date: Thu, 24 Dec 2015 00:21:18 -0500 Subject: [PATCH] Add initial hammerspoon config --- hammerspoon/apps.lua | 19 +++++++++++ hammerspoon/core.lua | 56 +++++++++++++++++++++++++++++++++ hammerspoon/init.lua | 14 +++++++++ hammerspoon/keybinds.lua | 17 ++++++++++ hammerspoon/locations.lua | 0 hammerspoon/modules/windows.lua | 2 ++ hammerspoon/urls.lua | 4 +++ 7 files changed, 112 insertions(+) create mode 100644 hammerspoon/apps.lua create mode 100644 hammerspoon/core.lua create mode 100644 hammerspoon/init.lua create mode 100644 hammerspoon/keybinds.lua create mode 100644 hammerspoon/locations.lua create mode 100644 hammerspoon/modules/windows.lua create mode 100644 hammerspoon/urls.lua diff --git a/hammerspoon/apps.lua b/hammerspoon/apps.lua new file mode 100644 index 00000000..c5579f1f --- /dev/null +++ b/hammerspoon/apps.lua @@ -0,0 +1,19 @@ + +forApp("Finder", function (appName, eventType, appObject) + -- Bring all Finder windows forward when one gets activated + appObject:selectMenuItem({"Window", "Bring All to Front"}) +end) + +forApp("iTerm2", function (appName, _, appObject) + -- cmd+N switch-to-window shortcuts in Tmux + for i = 1,9 do + i = tostring(i) + bind(appName, 'cmd', i, function () + if string.find(appObject:focusedWindow():title(), ".*tmux") then + -- os.execute("/usr/local/bin/tmux select-window -t " .. i) + hs.eventtap.keyStroke({'ctrl'}, 'c') + hs.eventtap.keyStroke({}, i) + end + end) + end +end) diff --git a/hammerspoon/core.lua b/hammerspoon/core.lua new file mode 100644 index 00000000..cc4aa265 --- /dev/null +++ b/hammerspoon/core.lua @@ -0,0 +1,56 @@ +-- TODO: Turn into App class + +core = {} +core.__index = core + +function core.reload() + hs.reload() + hs.alert("Config reloaded!") +end + +-- App-specific code, runs when you launch or switch to another app +apps = {} +function forApp(name, func) + if type(name) == 'table' then + for i = 1, #name do + apps[name[i]] = func + end + else + apps[name] = func + end +end + +-- App-specific keybinds +lastApp = "" +binds = {} +function bind(app, mods, key, pressedfn, releasedfn, repeatfn) + if not binds[app] then + binds[app] = {} + end + if not binds[app][mods] then + binds[app][mods] = {} + end + if binds[app][mods][key] then + binds[app][mods][key]:enable() + else + binds[app][mods][key] = hs.hotkey.bind(mods, key, nil, pressedfn, releasedfn, repeatfn) + end +end + +function appWatcher(appName, eventType, appObject) + if (eventType == hs.application.watcher.activated) then + if (lastApp ~= appName) and (binds[lastApp]) then + for _, i in pairs(binds[lastApp]) do + for _, j in pairs(i) do + j:disable() + end + end + end + if apps[appName] then + apps[appName](appName, eventType, appObject) + lastApp = appName + end + end +end +local appWatcher = hs.application.watcher.new(appWatcher) +appWatcher:start() diff --git a/hammerspoon/init.lua b/hammerspoon/init.lua new file mode 100644 index 00000000..315888a1 --- /dev/null +++ b/hammerspoon/init.lua @@ -0,0 +1,14 @@ +require('core') + +-- +require('modules/windows') + +-- Globals +screen = hs.screen.primaryScreen() + +-- Modules +require('keybinds') +require('apps') +require('urls') +require('locations') + diff --git a/hammerspoon/keybinds.lua b/hammerspoon/keybinds.lua new file mode 100644 index 00000000..013597c8 --- /dev/null +++ b/hammerspoon/keybinds.lua @@ -0,0 +1,17 @@ +k = hs.hotkey.modal.new('cmd', 'escape') + + +-- Screen overlay +local f = screen:frame() +local r = hs.drawing.rectangle(hs.geometry(0, 0, f.w, f.h + 30)) +r:setAlpha(0.5) +r:setFillColor(hs.drawing.color.black) +r:bringToFront() + +function k:entered() hs.alert("Command mode!"); r:show() end +function k:exited() r:hide(); hs.alert("Bye bye") end + + +-- Bindings +k:bind('', 'escape', function() k:exit() end) +k:bind('', 'r', function() core.reload() end) diff --git a/hammerspoon/locations.lua b/hammerspoon/locations.lua new file mode 100644 index 00000000..e69de29b diff --git a/hammerspoon/modules/windows.lua b/hammerspoon/modules/windows.lua new file mode 100644 index 00000000..0141167e --- /dev/null +++ b/hammerspoon/modules/windows.lua @@ -0,0 +1,2 @@ +-- TODO: Window management library + diff --git a/hammerspoon/urls.lua b/hammerspoon/urls.lua new file mode 100644 index 00000000..d4d7e513 --- /dev/null +++ b/hammerspoon/urls.lua @@ -0,0 +1,4 @@ +-- hammerspoon://reload +hs.urlevent.bind("reload", core.reload) + +-- TODO: hammerspoon://org?search=keywords