Skip to content

Commit

Permalink
Fix a single key mapping not overriding a previously defined multi-ke…
Browse files Browse the repository at this point in the history
…y mapping
  • Loading branch information
kovidgoyal committed Jan 26, 2024
1 parent ae1bf69 commit 8c50632
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
5 changes: 5 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ The :doc:`ssh kitten <kittens/ssh>` is redesigned with powerful new features:
Detailed list of changes
-------------------------------------

0.32.2 [future]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

- Fix a single key mapping not overriding a previously defined multi-key mapping

0.32.1 [2024-01-26]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
28 changes: 17 additions & 11 deletions kitty/keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,28 +103,34 @@ def matching_key_actions(self, candidates: Iterable[KeyDefinition]) -> List[KeyD
matches = []
has_sequence_match = False
for x in candidates:
is_applicable = False
if x.options.when_focus_on:
try:
if w and w in self.match_windows(x.options.when_focus_on):
matches.append(x)
if x.is_sequence:
has_sequence_match = True
is_applicable = True
except Exception:
self.clear_keyboard_modes()
self.show_error(_('Invalid key mapping'), _(
'The match expression {0} is not valid for {1}').format(x.options.when_focus_on, '--when-focus-on'))
return []
else:
is_applicable = True
if is_applicable:
matches.append(x)
if x.is_sequence:
has_sequence_match = True
matches.append(x)
if has_sequence_match:
terminal_matches = [x for x in matches if not x.rest]
if terminal_matches:
matches = [terminal_matches[-1]]
else:
matches = [x for x in matches if x.is_sequence]
q = matches[-1].options.when_focus_on
matches = [x for x in matches if x.options.when_focus_on == q]
last_terminal_idx = -1
for i, x in enumerate(matches):
if not x.rest:
last_terminal_idx = i
if last_terminal_idx > -1:
if last_terminal_idx == len(matches) -1:
matches = matches[last_terminal_idx:]
else:
matches = matches[last_terminal_idx+1:]
q = matches[-1].options.when_focus_on
matches = [x for x in matches if x.options.when_focus_on == q]
else:
matches = [matches[-1]]
return matches
Expand Down
11 changes: 11 additions & 0 deletions kitty_tests/keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,17 @@ def __call__(self, *keys: str):
tm = TM('map kitty_mod+p new_window')
self.ae(tm('ctrl+shift+p', 'f'), [True, False])
self.ae(tm.actions, ['new_window'])
# multi-key mapping overrides previous single key mapping with same prefix
tm = TM('map kitty_mod+s>p new_window')
self.ae(tm('ctrl+shift+s', 'p'), [True, True])
self.ae(tm.actions, ['new_window'])
# mix of single and multi-key mappings with same prefix
tm = TM('map alt+p>1 multi1', 'map alt+p single1', 'map alt+p>2 multi2')
self.ae(tm('alt+p', '2'), [True, True])
self.ae(tm.actions, ['multi2'])
self.ae(tm('alt+p', '1'), [True, False])
af(tm.actions)
self.ae(len(tm.active_window.key_seqs), 1)

# changing a multi key mapping
tm = TM('map kitty_mod+p>f new_window')
Expand Down

0 comments on commit 8c50632

Please sign in to comment.