feat(shortcuts): add a leader key tree and palette remapping

Expose the multi-key mappings starting at the configured leader key in a
which-key panel, so that continuations are discoverable as soon as the prefix
is pressed. Both the panel and the command palette read the same structured
mapping data, and the palette can now write a new mapping for the selected
action.
This commit is contained in:
BlackAngelTV committed 2026-11-13 08:03:03 +01:00
1 parent dc13716431
commit bfc97997a1
10 files changed
+418 -13

No files matched your search

+32 -1
View File
@@ -23,7 +23,7 @@ from .fast_data_types import (
set_ignore_os_keyboard_processing,
)
from .options.types import Options
from .options.utils import KeyboardMode, KeyDefinition, KeyFallbackType, KeyMap, KeyMapOptions
from .options.utils import KeyboardMode, KeyDefinition, KeyFallbackType, KeyMap, KeyMapOptions, parse_shortcut
from .typing_compat import ScreenType
if TYPE_CHECKING:
@@ -40,6 +40,14 @@ def keyboard_mode_name(screen: ScreenType) -> str:
return 'application' if screen.cursor_key_mode else 'normal'
def leader_key_from_options(opts: Options) -> SingleKey | None:
"""The shortcut that opens the leader tree, or None when disabled."""
raw = (getattr(opts, 'leader', '') or '').strip()
if not raw or raw.lower() in ('none', 'no', 'off'):
return None
return parse_shortcut(raw)
def get_shortcut(keymap: KeyMap, ev: KeyEvent) -> list[KeyDefinition] | None:
mods = ev.mods & mod_mask
ans = keymap.get(SingleKey(mods, False, ev.key))
@@ -111,6 +119,24 @@ class Mappings:
self.keyboard_modes[''].keymap = km = km.copy()
for sc in self.global_shortcuts.values():
km.pop(sc, None)
self.leader_key: SingleKey | None = getattr(self.get_options(), 'leader_key', None)
if self.leader_key is None:
try:
self.leader_key = leader_key_from_options(self.get_options())
except Exception:
# An invalid leader is reported when the config is loaded.
self.leader_key = None
def leader_continuations(self, mode: KeyboardMode) -> list[KeyDefinition]:
"""Multi-key mappings whose first key is the leader key."""
ans: list[KeyDefinition] = []
if self.leader_key is None:
return ans
for defns in mode.keymap.values():
for d in defns:
if d.is_sequence and d.trigger == self.leader_key:
ans.append(d)
return ans
def clear_keyboard_modes(self) -> None:
had_mode = bool(self.keyboard_mode_stack)
@@ -212,6 +238,11 @@ class Mappings:
# Handles shortcuts, return True if the key was consumed
is_root_mode = not self.keyboard_mode_stack
mode = self.keyboard_modes[''] if is_root_mode else self.keyboard_mode_stack[-1]
if is_root_mode and self.leader_key is not None and shortcut_matches(self.leader_key, ev):
continuations = self.matching_key_actions(self.leader_continuations(mode))
if continuations:
get_boss().show_leader_keymap(self.leader_key, continuations)
return True
key_action = get_shortcut(mode.keymap, ev)
if key_action is None and self.global_shortcuts_map and (global_key_action := get_shortcut(self.global_shortcuts_map, ev)) is not None:
if grab_keyboard(None):