Skip to content

Instantly share code, notes, and snippets.

@amirrajan
Last active August 16, 2020 16:33
Show Gist options
  • Save amirrajan/e2ec90b4727de0febcdf4c2684257d46 to your computer and use it in GitHub Desktop.
Save amirrajan/e2ec90b4727de0febcdf4c2684257d46 to your computer and use it in GitHub Desktop.
Hammerspoon Mode
-- hotload config
function reloadConfig(files)
doReload = false
for _,file in pairs(files) do
if file:sub(-4) == ".lua" then
doReload = true
end
end
if doReload then
result, success = hs.execute("luac -p ~/.hammerspoon/init.lua 2>&1 >/dev/null", true)
if not success then
hs.alert(result)
return
end
result, success = hs.execute("luac -p ~/.hammerspoon/Spoons/KeyFu.spoon/init.lua 2>&1 >/dev/null", true)
if not success then
hs.alert(result)
return
end
hs.reload()
end
end
hs_config_watcher = hs.pathwatcher.new(os.getenv("HOME") .. "/.hammerspoon/", reloadConfig):start()
hs.alert.show("Config loaded.")
-- toggle between chrome and iterm using ctrl t
hs.hotkey.bind({'ctrl'}, "t", function()
app_name = hs.application.frontmostApplication():name()
if app_name == 'iTerm2' then
hs.osascript.applescript('tell application "Chrome" to activate')
else
hs.osascript.applescript('tell application "iTerm" to activate')
end
end)
-- global command mode for hammersppon because I'm insane
-- ctrl m to go into hammerspoon mode
g_mod_keys = true
g_hs_mode = false
g_hs_mode_command = ''
hs.hotkey.bind({'ctrl'}, "m", function()
if not g_hs_mode then
hs.alert('hs mode: on')
g_hs_mode = true
end
end)
hs_mode_handler = function(evt)
local new_mods = evt:getFlags()
if not g_hs_mode then
return
end
if evt:getCharacters(true) then
g_hs_mode_command = g_hs_mode_command .. evt:getCharacters(true)
end
local was_processed = false
if evt:getCharacters(true) == 'i' then --exit hammerspoon mode
was_processed = true
hs.alert('hs mode: off')
elseif g_hs_mode_command == 'go' then -- open google
shell('open http://google.com')
was_processed = true
end
if was_processed then
g_hs_mode = false
g_hs_mode_command = ''
return true
end
if string.len(g_hs_mode_command) > 2 then
hs.alert("exiting (unknown command): " .. g_hs_mode_command)
g_hs_mode = false
g_hs_mode_command = ''
end
return true
end
hs_mode = hs.eventtap.new({hs.eventtap.event.types.keyDown}, hs_mode_handler)
hs_mode:start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment