Created
July 10, 2017 05:16
-
-
Save thekoc/49dfeed6b4a4b617c4b06eda1a25af18 to your computer and use it in GitHub Desktop.
Hammerspoon configuration file backup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local function hasValue(value, list) | |
for i, e in ipairs(list) do | |
if value == e then | |
return true | |
end | |
end | |
return false | |
end | |
local function applicationWatcher(appName, eventType, appObject) | |
print("trigger watcher") | |
if (eventType == hs.application.watcher.activated) then | |
print("active", appName) | |
if (hasValue(appName, emacs_env_exclude_apps)) then | |
emacs_env:exit() | |
else | |
emacs_env:enter() | |
end | |
end | |
end | |
local function bindEmacsKeys(env) | |
env:bind('alt', 'F', function() hs.eventtap.keyStroke({"alt"}, "right") end) | |
env:bind('alt', 'B', function() hs.eventtap.keyStroke({"alt"}, "left") end) | |
env:bind('alt', 'D', function() hs.eventtap.keyStroke({"alt"}, "forwarddelete") end) | |
env:bind('ctrl', 'N', function() hs.eventtap.keyStroke({}, 'down') end) | |
env:bind('ctrl', 'P', function() hs.eventtap.keyStroke({}, 'up') end) | |
env:bind('ctrl', 'F', function() hs.eventtap.keyStroke({}, 'right') end) | |
env:bind('ctrl', 'B', function() hs.eventtap.keyStroke({}, 'left') end) | |
env:bind('ctrl', 'U', function () hs.eventtap.keyStroke({'cmd'}, 'delete') end) | |
env:bind('ctrl', 'W', function () hs.eventtap.keyStroke({'alt'}, 'delete') end) | |
end | |
hs.hotkey.bind({"ctrl"}, "[", function() hs.eventtap.keyStroke({}, 'escape') end) | |
local appWatcher = hs.application.watcher.new(applicationWatcher) | |
appWatcher:start() | |
emacs_env_exclude_apps = {'iTerm2', 'MacVim', 'Atom'} | |
emacs_env = hs.hotkey.modal.new('', nil) | |
bindEmacsKeys(emacs_env) | |
alfred_env = hs.hotkey.modal.new('', nil) | |
bindEmacsKeys(alfred_env) | |
local current_window = hs.window.focusedWindow() | |
if (not current_window == nil) then | |
if (not hasValue(current_window:application():name(), emacs_env_exclude_apps)) then | |
emacs_env:enter() | |
else | |
emacs_env:exit() | |
end | |
end | |
local function alfredWatcher() | |
while true do | |
local current_window = hs.window.focusedWindow() | |
print('checking') | |
if (current_window == nil or current_window:application():name() ~= "Alfred 3") then | |
alfred_env:exit() | |
print('exiting alfred') | |
break | |
end | |
end | |
end | |
local function callAlfred() | |
local current_window = hs.window.focusedWindow() | |
if (current_window == nil or current_window:application():name() ~= "Alfred 3") then | |
hs.osascript.applescript('tell application "Alfred 3" to search') | |
hs.keycodes.setLayout("ABC") | |
else | |
hs.eventtap.keyStroke({}, "escape") | |
end | |
end | |
hs.hotkey.bind({"alt"}, "space", callAlfred) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment