Skip to content

Instantly share code, notes, and snippets.

@confluencepoint
Forked from amirrajan/init.lua
Last active December 13, 2021 13:59
Show Gist options
  • Save confluencepoint/f158c1fa83b452c33801d69c3cbe66dc to your computer and use it in GitHub Desktop.
Save confluencepoint/f158c1fa83b452c33801d69c3cbe66dc to your computer and use it in GitHub Desktop.
Full Hammerspoon File
require("hs.ipc")
function shell(command)
hs.execute(command .. ' &', true)
end
--- ==========================================
--- Terminal <> Browser ctrl+t
--- ==========================================
-- I spend all my time in chrome and terminal, ctrl+t swaps between the two
hs.hotkey.bind({'ctrl'}, "t", function()
app_name = hs.application.frontmostApplication():name()
if app_name == 'Alacritty' then
hs.osascript.applescript('tell application "Google Chrome" to activate')
else
hs.osascript.applescript('tell application "Alacritty" to activate')
end
end)
--- ==========================================
--- Reload Hammerspoon
--- ==========================================
function reloadConfig(files)
hs.alert('Reloading Hammerspoon, please wait...')
shouldReload = false
for _,file in pairs(files) do
if file:sub(-4) == ".lua" then
shouldReload = true
end
end
if shouldReload then
hs.timer.delayed.new(0.1, doReload):start()
end
end
function doReload(evt)
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()
-- ==========================================
-- ESC
-- ==========================================
g_mod_keys = true
send_escape = false
last_mods = {}
control_key_handler = function()
send_escape = false
end
control_key_timer = hs.timer.delayed.new(0.10, control_key_handler)
control_handler = function(evt)
if not g_mod_keys then
return
end
local new_mods = evt:getFlags()
if last_mods["ctrl"] == new_mods["ctrl"] then
return false
end
if not last_mods["ctrl"] then
last_mods = new_mods
send_escape = true
control_key_timer:start()
else
if send_escape then
if not g_hs_mode then
hs.eventtap.event.newKeyEvent({}, 'escape', true):post()
hs.eventtap.event.newKeyEvent({}, 'escape', false):post()
end
end
last_mods = new_mods
control_key_timer:stop()
end
return false
end
control_tap = hs.eventtap.new({hs.eventtap.event.types.flagsChanged}, control_handler)
control_tap:start()
other_handler = function(evt)
send_escape = false
return false
end
other_tap = hs.eventtap.new({hs.eventtap.event.types.keyDown}, other_handler)
other_tap:start()
-- ==========================================
-- Hammerspoon Mode
-- ==========================================
-- OS level Hammerspoon Leader Key is ctrl+m
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)
-- when in Hammerspoon mode, process keyboard input and execute a command
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 -- i takes you back to insert mode
was_processed = true
hs.alert('hs mode: off')
elseif g_hs_mode_command == 'go' then -- go takes you to google
shell('open http://google.com')
was_processed = true
elseif g_hs_mode_command == 'gm' then -- gm takes you to gmail
shell('open http://gmail.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()
hs.alert.show("Config loaded.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment