2
0
mirror of https://github.com/webgefrickel/dotfiles synced 2024-11-11 07:10:40 +00:00
steffen-dotfiles/hammerspoon/init.lua

161 lines
6.6 KiB
Lua
Raw Normal View History

local vimouse = require('vimouse')
local app = require('hs.application')
local eventtap = require('hs.eventtap')
local geometry = require('hs.geometry')
local hotkey = require('hs.hotkey')
local layout = require('hs.layout')
local screen = require('hs.screen')
local win = require('hs.window')
2021-10-16 16:01:33 +00:00
-- Custom variables
--------------------
2017-06-12 10:06:46 +00:00
local hyper = { 'cmd', 'alt', 'shift', 'ctrl' }
local alt = { 'alt' }
local altShift = { 'alt', 'shift' }
local laptopMonitor = "Built-in Retina Display"
2021-06-16 13:51:24 +00:00
local mainMonitor = "DELL U3415W"
2017-06-12 10:06:46 +00:00
2021-10-16 16:01:33 +00:00
-- Custom positions and layouts where which apps can be on the screen
--------------------
local screenPositions = {
left = geometry.rect(0.01, 0.01, 0.485, 0.98),
leftTop = { x = 0.01, y = 0.01, w = 0.485, h = 0.485 },
leftBottom = { x= 0.01, y = 0.505, w = 0.485, h = 0.485 },
right = geometry.rect(0.505, 0.01, 0.485, 0.98),
rightTop = { x = 0.505, y = 0.01, w = 0.485, h = 0.485 },
rightBottom = { x= 0.505, y = 0.505, w = 0.485, h = 0.485 },
top = geometry.rect(0.01, 0.01, 0.98, 0.485),
bottom = geometry.rect(0.01, 0.505, 0.98, 0.485),
center = geometry.rect(0.15, 0.15, 0.7, 0.7),
max = geometry.rect(0.01, 0.01, 0.98, 0.98),
full = geometry.rect(0, 0, 1, 1),
}
2017-06-12 10:06:46 +00:00
local layoutDouble = {
2021-10-16 16:01:33 +00:00
{ "Calendar", nil, laptopMonitor, screenPositions.full, nil, nil },
{ "Firefox", nil, mainMonitor, screenPositions.left, nil, nil },
2021-10-16 16:01:33 +00:00
{ "ForkLift", nil, laptopMonitor, screenPositions.full, nil, nil },
{ "Messages", nil, mainMonitor, screenPositions.rightTop, nil, nil },
2021-11-01 19:00:27 +00:00
{ "Microsoft Teams", nil, mainMonitor, screenPositions.rightBottom, nil, nil },
{ "Reminders", nil, laptopMonitor, screenPositions.full, nil, nil },
{ "Signal", nil, mainMonitor, screenPositions.rightBottom, nil, nil },
2021-11-01 19:00:27 +00:00
{ "Spotify", nil, mainMonitor, screenPositions.left, nil, nil },
{ "Telegram", nil, mainMonitor, screenPositions.rightTop, nil, nil },
2021-11-01 19:00:27 +00:00
{ "iTerm2", nil, mainMonitor, screenPositions.right, nil, nil },
}
local layoutSingle = {
2021-10-16 16:01:33 +00:00
{ "Calendar", nil, laptopMonitor, screenPositions.full, nil, nil },
{ "Firefox", nil, laptopMonitor, screenPositions.full, nil, nil },
{ "ForkLift", nil, laptopMonitor, screenPositions.full, nil, nil },
{ "Messages", nil, laptopMonitor, screenPositions.full, nil, nil },
2021-11-01 19:00:27 +00:00
{ "Microsoft Teams", nil, laptopMonitor, screenPositions.full, nil, nil },
{ "Reminders", nil, laptopMonitor, screenPositions.full, nil, nil },
2021-10-16 16:01:33 +00:00
{ "Signal", nil, laptopMonitor, screenPositions.full, nil, nil },
2021-11-01 19:00:27 +00:00
{ "Spotify", nil, laptopMonitor, screenPositions.full, nil, nil },
2021-10-16 16:01:33 +00:00
{ "Telegram", nil, laptopMonitor, screenPositions.full, nil, nil },
2021-11-01 19:00:27 +00:00
{ "iTerm2", nil, laptopMonitor, screenPositions.full, nil, nil },
}
2017-06-12 18:39:54 +00:00
local appNames = {
"Calendar",
"Firefox",
"ForkLift",
"Messages",
2021-11-01 19:00:27 +00:00
"Microsoft Teams",
"Reminders",
"Signal",
2021-11-01 19:00:27 +00:00
"Spotify",
"Telegram",
2021-11-01 19:00:27 +00:00
"iTerm",
}
2017-06-12 18:39:54 +00:00
2021-10-16 16:01:33 +00:00
-- Local helper functions
--------------------
local function launchApps()
for i, appName in ipairs(appNames) do
app.launchOrFocus(appName)
end
2017-06-12 18:39:54 +00:00
end
local function moveMouse()
local pt = hs.geometry.rectMidPoint(win.focusedWindow():frame())
hs.mouse.absolutePosition(pt)
end
local function applyPosition(pos)
2021-06-15 11:21:42 +00:00
local w = win.focusedWindow()
local screenName = w:screen():name()
local tempPos = screenPositions[pos]
local tempLayout = {
{ app.frontmostApplication(), w, screenName, tempPos, nil, nil },
}
layout.apply(tempLayout)
end
2021-10-16 16:01:33 +00:00
-- Window management and general config
--------------------
win.animationDuration = 0
2021-10-16 16:01:33 +00:00
-- Keybindings
--------------------
-- Applying main two layouts
hotkey.bind(hyper, 'q', function() layout.apply(layoutSingle) end)
hotkey.bind(hyper, 'w', function() layout.apply(layoutDouble) end)
2021-01-27 16:08:05 +00:00
2021-10-16 16:01:33 +00:00
-- Direct app navigation
-- hotkey D is set in Dash itself
hotkey.bind(hyper, 'a', function() app.launchOrFocus('iTerm') end)
hotkey.bind(hyper, 's', function() app.launchOrFocus('Firefox') end)
hotkey.bind(hyper, 'f', function() app.launchOrFocus('ForkLift') end)
hotkey.bind(hyper, 'g', function() launchApps() end)
2021-10-16 16:01:33 +00:00
-- Moving windows around / navigating windows
hotkey.bind(hyper, '[', function() win.focusedWindow():moveOneScreenNorth(); moveMouse() end)
hotkey.bind(hyper, ']', function() win.focusedWindow():moveOneScreenSouth(); moveMouse() end)
hotkey.bind(hyper, 'z', function() applyPosition('full'); moveMouse() end)
hotkey.bind(hyper, 'x', function() applyPosition('max'); moveMouse() end)
hotkey.bind(hyper, 'c', function() applyPosition('center'); moveMouse() end)
hotkey.bind(hyper, 'h', function() applyPosition('left'); moveMouse() end)
hotkey.bind(hyper, 'u', function() applyPosition('leftTop'); moveMouse() end)
hotkey.bind(hyper, 'n', function() applyPosition('leftBottom'); moveMouse() end)
hotkey.bind(hyper, 'l', function() applyPosition('right'); moveMouse() end)
hotkey.bind(hyper, 'i', function() applyPosition('rightTop'); moveMouse() end)
hotkey.bind(hyper, 'm', function() applyPosition('rightBottom'); moveMouse() end)
hotkey.bind(hyper, 'k', function() applyPosition('top'); moveMouse() end)
hotkey.bind(hyper, 'j', function() applyPosition('bottom'); moveMouse() end)
2017-06-12 18:39:54 +00:00
2017-06-17 11:52:33 +00:00
-- map hyper + number to the corresponding fn-key, since the touchbar
-- kinda sucks, and karabiner-elements is breaking fn-function to show keys
hotkey.bind(hyper, '1', function() eventtap.keyStroke({}, 'F1') end)
hotkey.bind(hyper, '2', function() eventtap.keyStroke({}, 'F2') end)
hotkey.bind(hyper, '3', function() eventtap.keyStroke({}, 'F3') end)
hotkey.bind(hyper, '4', function() eventtap.keyStroke({}, 'F4') end)
hotkey.bind(hyper, '5', function() eventtap.keyStroke({}, 'F5') end)
hotkey.bind(hyper, '6', function() eventtap.keyStroke({}, 'F6') end)
hotkey.bind(hyper, '7', function() eventtap.keyStroke({}, 'F7') end)
hotkey.bind(hyper, '8', function() eventtap.keyStroke({}, 'F8') end)
hotkey.bind(hyper, '9', function() eventtap.keyStroke({}, 'F9') end)
hotkey.bind(hyper, '0', function() eventtap.keyStroke({}, 'F10') end)
hotkey.bind(hyper, '-', function() eventtap.keyStroke({}, 'F11') end)
hotkey.bind(hyper, '=', function() eventtap.keyStroke({}, 'F12') end)
2017-06-17 11:52:33 +00:00
-- macos keyboard input source switching is still broken AF, so we map
-- umlauts and others manually with hammerspoon, instead of using US-german-layout
-- dunno why I had to add a space beföre ´äÄ, but this way it works
hotkey.bind(alt, 'a', function() eventtap.keyStrokes(' ä') end)
hotkey.bind(alt, 'o', function() eventtap.keyStrokes('ö') end)
hotkey.bind(alt, 's', function() eventtap.keyStrokes('ß') end)
hotkey.bind(alt, 'u', function() eventtap.keyStrokes('ü') end)
hotkey.bind(altShift, 'a', function() eventtap.keyStrokes(' Ä') end)
hotkey.bind(altShift, 'o', function() eventtap.keyStrokes('Ö') end)
hotkey.bind(altShift, 'u', function() eventtap.keyStrokes('Ü') end)
2021-10-16 16:01:33 +00:00
vimouse(hyper, ',')