Skip to content

Commit

Permalink
use kitty conditional mappings
Browse files Browse the repository at this point in the history
  • Loading branch information
oddnugget committed Apr 7, 2024
1 parent 53f6a64 commit 65ad6e8
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 21 deletions.
28 changes: 14 additions & 14 deletions doc/smart-splits.txt
Original file line number Diff line number Diff line change
Expand Up @@ -484,9 +484,6 @@ KITTY *smart-splits-kitty*
[!NOTE] The `config.at_edge = 'wrap'` option is not supoprted in Kitty
terminal multiplexer due to inability to determine pane layout from CLI.

[!NOTE] This won't work if the pane is connected over SSH, as the pane will
not properly report the foreground process name.

Add the following snippet to `~/.config/kitty/kitty.conf`, adjusting the
keymaps and resize amount as desired.

Expand All @@ -502,17 +499,20 @@ keymaps and resize amount as desired.
map alt+h kitten pass_keys.py relative_resize left 3 alt+h
map alt+l kitten pass_keys.py relative_resize right 3 alt+l
<
By default, it matches against the name of the current foreground process to
detect if `vim`/`nvim` is running. If that doesn't work for you, or you want
to include other CLI/TUI programs in the exclusion, you can provide an
additional regex argument:
By default the nvim plugin sets a kitty user-var `in_editor` via autocmd when
the plugin loads. You can take advantage of this together with kittys
[conditional mappings feature](https://sw.kovidgoyal.net/kitty/mapping/#conditional-mappings-depending-on-the-state-of-the-focused-window)

map ctrl+j neighboring_window down
map ctrl+k neighboring_window up
map ctrl+h neighboring_window left
map ctrl+l neighboring_window right

map --when-focus-on var:in_editor ctrl+j
map --when-focus-on var:in_editor ctrl+k
map --when-focus-on var:in_editor ctrl+h
map --when-focus-on var:in_editor ctrl+l

>
map ctrl+j kitten pass_keys.py neighboring_window bottom ctrl+j "^.* - nvim$"
map ctrl+k kitten pass_keys.py neighboring_window top ctrl+k "^.* - nvim$"
map ctrl+h kitten pass_keys.py neighboring_window left ctrl+h "^.* - nvim$"
map ctrl+l kitten pass_keys.py neighboring_window right ctrl+l "^.* - nvim$"
# the 3 here is the resize amount, adjust as needed
map alt+j kitten pass_keys.py relative_resize down 3 alt+j "^.* - nvim$"
map alt+k kitten pass_keys.py relative_resize up 3 alt+k "^.* - nvim$"
Expand Down Expand Up @@ -578,4 +578,4 @@ currently in use. The API offers the following methods:
---@field type 'tmux'|'wezterm'|'kitty'
<

vim:tw=78:ts=8:ft=help:norl:
vim:tw=78:ts=8:ft=help:norl:
7 changes: 1 addition & 6 deletions kitty/pass_keys.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import re

from kittens.tui.handler import result_handler
from kitty.key_encoding import KeyEvent, parse_shortcut


def is_window_vim(boss, window):
vars = boss.call_remote_control(window, ('set-user-vars', f'--match=id:{window.id}'))
for var in vars.split('\n'):
if var.startswith('IS_NVIM'):
if var.startswith('in_editor'):
return True
else:
return False


def encode_key_mapping(window, key_mapping):
mods, key = parse_shortcut(key_mapping)
event = KeyEvent(
Expand Down Expand Up @@ -102,7 +99,5 @@ def handle_result(args, result, target_window_id, boss):
for keymap in key_mapping.split(">"):
encoded = encode_key_mapping(window, keymap)
window.write_to_child(encoded)
elif action == 'neighboring_window':
boss.active_tab.neighboring_window(direction)
elif action == 'relative_resize':
relative_resize_window(direction, amount, target_window_id, boss)
14 changes: 13 additions & 1 deletion lua/smart-splits/mux/kitty.lua
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,19 @@ function M.resize_pane(direction, amount)
end

function M.on_init()
os.execute('kitten @ set-user-vars IS_NVIM=true')
vim.api.nvim_create_autocmd('VimEnter', {
group = vim.api.nvim_create_augroup('KittySetVarVimEnter', { clear = true }),
callback = function()
io.stdout:write('\x1b]1337;SetUserVar=in_editor=MQo\007')
end,
})

vim.api.nvim_create_autocmd('VimLeave', {
group = vim.api.nvim_create_augroup('KittyUnsetVarVimLeave', { clear = true }),
callback = function()
io.stdout:write('\x1b]1337;SetUserVar=in_editor\007')
end,
})
end

function M.split_pane(_, _)
Expand Down

0 comments on commit 65ad6e8

Please sign in to comment.