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:
10 files changed
+418
-13
No files matched your search
@@ -80,6 +80,45 @@ def prepare_config_file_for_editing() -> str:
|
||||
return defconf
|
||||
|
||||
|
||||
def finalize_leader(opts: Options) -> None:
|
||||
"""Validate the leader shortcut once so that key handling can use it directly."""
|
||||
from .keys import leader_key_from_options
|
||||
|
||||
try:
|
||||
opts.leader_key = leader_key_from_options(opts)
|
||||
except Exception as err:
|
||||
opts.leader_key = None
|
||||
log_error(f'Ignoring invalid leader shortcut: {opts.leader}. Error: {err}')
|
||||
|
||||
|
||||
def append_map_to_config(key: str, definition: str, conf_path: str = '') -> str:
|
||||
"""Append a map directive to the user config, creating it when needed.
|
||||
|
||||
The path is returned so that callers can report it. Only the mapping is
|
||||
written, existing configuration is never rewritten.
|
||||
"""
|
||||
from .constants import config_dir
|
||||
|
||||
path = conf_path or os.path.join(config_dir, 'kitty.conf')
|
||||
parent = os.path.dirname(path)
|
||||
if parent:
|
||||
with suppress(FileExistsError):
|
||||
os.makedirs(parent)
|
||||
needs_newline = False
|
||||
try:
|
||||
if os.path.getsize(path) > 0:
|
||||
with open(path, 'rb') as f:
|
||||
f.seek(-1, os.SEEK_END)
|
||||
needs_newline = f.read(1) != b'\n'
|
||||
except FileNotFoundError:
|
||||
pass
|
||||
with open(path, 'ab') as f:
|
||||
if needs_newline:
|
||||
f.write(b'\n')
|
||||
f.write(f'map {key} {definition}\n'.encode('utf-8'))
|
||||
return path
|
||||
|
||||
|
||||
def finalize_keys(opts: Options, accumulate_bad_lines: list[BadLine] | None = None) -> None:
|
||||
defns: list[KeyDefinition] = []
|
||||
for d in opts.map:
|
||||
@@ -118,6 +157,7 @@ def finalize_keys(opts: Options, accumulate_bad_lines: list[BadLine] | None = No
|
||||
items = m.keymap[defn.trigger] = [kd for kd in items if defn.rest != kd.rest or defn.options.when_focus_on != kd.options.when_focus_on]
|
||||
items.append(defn)
|
||||
opts.keyboard_modes = modes
|
||||
finalize_leader(opts)
|
||||
|
||||
|
||||
def finalize_mouse_mappings(opts: Options, accumulate_bad_lines: list[BadLine] | None = None) -> None:
|
||||
|
||||
Reference in new issue
Block a user