Skip to content

Commit

Permalink
[hammerspoon] window focus management setup
Browse files Browse the repository at this point in the history
  • Loading branch information
antonk52 committed Jan 17, 2025
1 parent 41d8edc commit 0e40be9
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 21 deletions.
15 changes: 15 additions & 0 deletions hammerspoon/.luarc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"$schema": "https://raw.githubusercontent.com/LuaLS/vscode-lua/master/setting/schema.json",
"workspace.library": ["/Applications/Hammerspoon.app/Contents/Resources/extensions/hs"],
"diagnostics": {
"globals": ["hs"],
"disable": ["undefined-global"]
},
"runtime": {
"version": "Lua 5.4",
"special": {
"hs.hotkey.bind": "function",
"hs.timer.doAfter": "function"
}
}
}
90 changes: 90 additions & 0 deletions hammerspoon/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
-- Setup

hs.console.clearConsole()
hs.alert.show('Hammerspoon Loaded <tab>+R to reload')
hs.hotkey.bind({ 'ctrl', 'alt', 'shift' }, 'R', hs.reload)

-- ===================================================
-- Focus between windows in the current workspace
-- ===================================================
local function next_idx(table, value)
for i, v in ipairs(table) do
if v == value then
if i == #table then
return 1
else
return i + 1
end
end
end
end

local function prev_idx(table, value)
for i, v in ipairs(table) do
if v == value then
if i == 1 then
return #table
else
return i - 1
end
end
end
end

local STATE = {
currentSpaceWindows = hs.window.filter.new():setCurrentSpace(true):getWindows(),
}
-- on window close or open update the list of windows

local function update_current_space_windows()
STATE.currentSpaceWindows = hs.window.filter.new():setCurrentSpace(true):getWindows()
end

local f = hs.window.filter.new():setCurrentSpace(true):subscribe({
hs.window.filter.windowsChanged,
}, update_current_space_windows)

-- print(hs.inspect(f))
local function focusNextWindowInScreen()
local currentWindow = hs.window.frontmostWindow()
if not currentWindow then
return
end

if #STATE.currentSpaceWindows < 2 then
hs.alert.show('Nothing to focus')
return
end

local next_i = next_idx(STATE.currentSpaceWindows, currentWindow)

local next_win = STATE.currentSpaceWindows[next_i]

if next_win then
next_win:focus()
end
end

local function focusPreviousWindowInScreen()
local currentWindow = hs.window.frontmostWindow()
if not currentWindow then
return
end

if #STATE.currentSpaceWindows < 2 then
hs.alert.show('Nothing to focus')
return
end

local prev_i = prev_idx(STATE.currentSpaceWindows, currentWindow)

local prev_win = STATE.currentSpaceWindows[prev_i]

if prev_win then
prev_win:focus()
end
end

-- Bind keys to move focus
hs.hotkey.bind({ 'ctrl', 'option', 'shift' }, 'j', focusNextWindowInScreen)
hs.hotkey.bind({ 'ctrl', 'option', 'shift' }, 'k', focusPreviousWindowInScreen)
18 changes: 0 additions & 18 deletions karabiner/karabiner.json
Original file line number Diff line number Diff line change
Expand Up @@ -562,24 +562,6 @@
}
]
},
{
"description": "TAB + j = move focus to next window",
"manipulators": [
{
"from": {
"key_code": "j",
"modifiers": { "mandatory": ["left_control", "left_option", "left_shift"] }
},
"to": [
{
"key_code": "f4",
"modifiers": ["left_control"]
}
],
"type": "basic"
}
]
},
{
"description": "TAB + [] to navigate workspaces",
"manipulators": [
Expand Down
2 changes: 2 additions & 0 deletions neovim.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
base: lua51

globals:
hs:
any: true
vim:
any: true
assert:
Expand Down
6 changes: 3 additions & 3 deletions nvim/lazy-lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
"diffview.nvim": { "branch": "main", "commit": "4516612fe98ff56ae0415a259ff6361a89419b0a" },
"lazy.nvim": { "branch": "main", "commit": "d8f26efd456190241afd1b0f5235fe6fdba13d4a" },
"markdowny.nvim": { "branch": "main", "commit": "8ff18e8677cf39821913f387697378fcf3e141a0" },
"mini.nvim": { "branch": "main", "commit": "ecde5ffd9adfd590d5c2d5d35a7f0152def894fc" },
"mini.nvim": { "branch": "main", "commit": "44a3ab2bc6b40edeb7a76359826c9779d5c70cb5" },
"none-ls.nvim": { "branch": "main", "commit": "3291afdff94e5083c7d6a4e9e661c3682c4b9b2a" },
"npm_scripts.nvim": { "branch": "main", "commit": "2dfa5d3b09564042c65ef8975eb3948b00f95422" },
"nvim-lspconfig": { "branch": "master", "commit": "339ccc81e08793c3af9b83882a6ebd90c9cc0d3b" },
"nvim-treesitter": { "branch": "master", "commit": "5da195ac3dfafd08d8b10756d975f0e01e1d563a" },
"plenary.nvim": { "branch": "master", "commit": "3707cdb1e43f5cea73afb6037e6494e7ce847a66" },
"schemastore.nvim": { "branch": "main", "commit": "a0dd8a31e30a34755e51abdb76d9c82e08b8ab38" },
"snacks.nvim": { "branch": "main", "commit": "ee697ad55d11b1f5c5332852a36bdfd7b4b84096" },
"schemastore.nvim": { "branch": "main", "commit": "b62efcb71077bfeb407dcd05fb0584e422131150" },
"snacks.nvim": { "branch": "main", "commit": "706b1abc1697ca050314dc667e0900d53cad8aa4" },
"telescope.nvim": { "branch": "master", "commit": "415af52339215926d705cccc08145f3782c4d132" },
"ts-comments.nvim": { "branch": "main", "commit": "872dcfa0418f4a33b7437fb4d9f4e89f2f000d74" },
"vim-dirvish": { "branch": "master", "commit": "c87a8e103f2e8b44b411a38dda9bbe1229547952" },
Expand Down
1 change: 1 addition & 0 deletions scripts/prepare-xdg.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ DOTS="$HOME"/dot-files
[ ! -L "$XDG_CONFIG_HOME"/karabiner ] && ln -s "$DOTS"/karabiner "$XDG_CONFIG_HOME"/karabiner

[ ! -L "$HOME"/.zshrc ] && ln -s "$DOTS"/.zshrc "$HOME"/.zshrc
[ ! -L "$HOME"/.hammerspoon ] && ln -s "$DOTS"/hammerspoon "$HOME"/.hammerspoon

echo 'symlinks created'

Expand Down

0 comments on commit 0e40be9

Please sign in to comment.