Skip to content

Instantly share code, notes, and snippets.

@chesster
Created January 1, 2018 20:45
Show Gist options
  • Save chesster/e0c4130125441cddfcafcad696264d65 to your computer and use it in GitHub Desktop.
Save chesster/e0c4130125441cddfcafcad696264d65 to your computer and use it in GitHub Desktop.
Hammerspoon config
-- Misc setup
hs.window.animationDuration = 0
local vw = hs.inspect.inspect
local configFileWatcher = nil
local appWatcher = nil
-- Keyboard modifiers, Capslock bound to cmd+alt+ctrl+shift via Seil and Karabiner
local modNone = {""}
local mAlt = {"⌥"}
local modCmd = {"⌘"}
local modShift = {"⇧"}
local modHyper = {"⌘", "⌥", "⌃"}
-- Cycle args for function when called repeatedly: cycleCalls( fn, { {args1...}, ... } )
function cycleCalls( fn, args )
local argIndex = 0
return function()
argIndex = argIndex + 1
if (argIndex > #args) then
argIndex = 1;
end
fn( args[ argIndex ] );
end
end
-- This method used to place a window to a position and size on the screen by using
-- four floats instead of pixel sizes. Returns the window instance. Examples:
-- windowToGrid( window, 0, 0, 0.25, 0.5 ); -- top-left, width: 25%, height: 50%
-- windowToGrid( someWindow, 0.3, 0.2, 0.5, 0.35 ); -- top: 30%, left: 20%, width: 50%, height: 35%
function windowToGrid( window, rect )
if not window then
return window
end
local screen = hs.screen.mainScreen():fullFrame()
window:setFrame( {
x = math.floor( rect[1] * screen.w + .5 ) + screen.x,
y = math.floor( rect[2] * screen.h + .5 ) + screen.y,
w = math.floor( rect[3] * screen.w + .5 ),
h = math.floor( rect[4] * screen.h + .5 )
} )
return window
end
function toGrid( x, y, w, h )
windowToGrid( hs.window.focusedWindow(), x, y, w, h );
end
modal = hs.hotkey.modal.new(modHyper,'w')
function modal:entered()
hs.alert.closeAll();
hs.alert.show( "Window manager active", 999999 )
end
function modal:exited()
hs.alert.closeAll()
end
modal:bind('','escape', function() modal:exit() end)
modal:bind('','return', function() modal:exit() end)
-- Modal keys
-- Centre window
modal:bind('','c',cycleCalls( toGrid, {{0.2, 0.15, 0.6, 0.75}, {0.1, 0.05, 0.8, 0.90}, {0, 0, 1, 1}} ) )
-- Size/position to one side of the screen
modal:bind('','left', cycleCalls( toGrid,{{0, 0, 0.5, 1}, {0, 0, 0.6, 1},{0, 0, 0.4, 1}}));
modal:bind('','right', cycleCalls( toGrid,{{0.5, 0, 0.5, 1}, {0.4, 0, 0.6, 1},{0.6, 0, 0.4, 1}}));
modal:bind('','up', cycleCalls( toGrid,{{0, 0, 1, 0.3}, {0, 0, 1, 0.7}}));
modal:bind('','down', cycleCalls( toGrid,{{0, 0.7, 1, 0.3}, {0, 0.3, 1, 0.7}}));
-- Reload config automatically
function reloadConfig()
configFileWatcher:stop()
configFileWatcher = nil
appWatcher:stop()
appWatcher = nil
hs.reload()
end
configFileWatcher = hs.pathwatcher.new(os.getenv("HOME") .. "/.hammerspoon/", reloadConfig)
configFileWatcher:start()
-- Callback function for application events
function applicationWatcher(appName, eventType, appObject)
if (eventType == hs.application.watcher.activated) then
if (appName == "Finder") then
-- Bring all Finder windows forward when one gets activated
appObject:selectMenuItem({"Window", "Bring All to Front"})
end
end
end
appWatcher = hs.application.watcher.new(applicationWatcher)
appWatcher:start()
switcher = require "hs.window.switcher"
filter = require "hs.window.filter"
switcher = switcher.new(filter.new():setDefaultFilter{}, {
showSelectedThumbnail = false,
thumbnailSize = 128,
showTitles = true,
textSize = 15,
textColor = { 1.0, 1.0, 1.0, 0.75 },
backgroundColor = { 0.3, 0.3, 0.3, 0.75 },
highlightColor = { 0.8, 0.5, 0.0, 0.80 },
titleBackgroundColor = { 0.0, 0.0, 0.0, 0.75 },
})
hs.hotkey.bind('alt', 'tab', function() switcher:next() end)
hs.hotkey.bind('alt-shift', 'tab', function() switcher:previous() end)
-- Finally, show a notification that we finished loading the config
hs.notify.new( {title='Hammerspoon', subTitle='Configuration loaded'} ):send()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment