Skip to content

Instantly share code, notes, and snippets.

@jakanaka-evan
Created March 13, 2018 20:36
Show Gist options
  • Save jakanaka-evan/4973c4a4e46e8e3bdae57b17dba0e26c to your computer and use it in GitHub Desktop.
Save jakanaka-evan/4973c4a4e46e8e3bdae57b17dba0e26c to your computer and use it in GitHub Desktop.
Get original sequence directly from `KeySequence` object
diff --git a/qutebrowser/keyinput/keyutils.py b/qutebrowser/keyinput/keyutils.py
index d0a17eca8..d23ac25c2 100644
--- a/qutebrowser/keyinput/keyutils.py
+++ b/qutebrowser/keyinput/keyutils.py
@@ -348,7 +348,8 @@ class KeySequence:
not be mutated in order to ensure consistent hashing.
Attributes:
- _sequences: A list of QKeySequence
+ _sequences: A list of QKeySequence.
+ _orig_sequence: KeySequence with no key_mappings applied.
Class attributes:
_MAX_LEN: The maximum amount of keys in a QKeySequence.
@@ -357,6 +358,7 @@ class KeySequence:
_MAX_LEN = 4
def __init__(self, *keys):
+ self._orig_sequence = None
self._sequences = []
for sub in utils.chunk(keys, self._MAX_LEN):
sequence = QKeySequence(*sub)
@@ -526,7 +528,15 @@ class KeySequence:
assert len(new_seq) == 1
key = new_seq[0].to_int()
keys.append(key)
- return self.__class__(*keys)
+ ret = self.__class__(*keys)
+ ret._orig_sequence = self.without_mappings()
+ return ret
+
+ def without_mappings(self):
+ """Get the KeySequence before any mappings were applied."""
+ if self._orig_sequence:
+ return self._orig_sequence
+ return self
@classmethod
def parse(cls, keystr):
diff --git a/qutebrowser/keyinput/modeparsers.py b/qutebrowser/keyinput/modeparsers.py
index 867cb2ba3..ec493772e 100644
--- a/qutebrowser/keyinput/modeparsers.py
+++ b/qutebrowser/keyinput/modeparsers.py
@@ -155,7 +155,6 @@ class PassthroughKeyParser(CommandKeyParser):
Attributes:
_mode: The mode this keyparser is for.
- _orig_sequence: Current sequence with no key_mappings applied.
"""
do_log = False
@@ -171,7 +170,6 @@ class PassthroughKeyParser(CommandKeyParser):
"""
super().__init__(win_id, parent)
self._read_config(mode)
- self._orig_sequence = keyutils.KeySequence()
self._mode = mode
def __repr__(self):
@@ -192,13 +190,10 @@ class PassthroughKeyParser(CommandKeyParser):
getattr(e, "ignore_event", False)):
return QKeySequence.NoMatch
- orig_sequence = self._orig_sequence.append_event(e)
+ sequence = self._sequence
match = super().handle(e, dry_run=dry_run)
- if not dry_run and match == QKeySequence.PartialMatch:
- self._orig_sequence = orig_sequence
-
- if dry_run or len(orig_sequence) == 1 or match != QKeySequence.NoMatch:
+ if dry_run or len(sequence) == 0 or match != QKeySequence.NoMatch:
return match
window = QApplication.focusWindow()
@@ -206,7 +201,7 @@ class PassthroughKeyParser(CommandKeyParser):
return match
first = True
- for keyinfo in orig_sequence:
+ for keyinfo in sequence.without_mappings().append_event(e):
press_event = keyinfo.to_event(QEvent.KeyPress)
if first:
press_event.ignore_event = True
@@ -217,11 +212,6 @@ class PassthroughKeyParser(CommandKeyParser):
return QKeySequence.ExactMatch
- def clear_keystring(self):
- """Override to also clear the original sequence."""
- super().clear_keystring()
- self._orig_sequence = keyutils.KeySequence()
-
class PromptKeyParser(CommandKeyParser):
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment