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