From 514138aad23851955503a5e912dc45f278796bda Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Wed, 7 Mar 2018 22:37:10 +0100 Subject: Allow to bind numbers in keybindings This mostly reverts 4ef5db1bc4b5205812714a57d29daa59224afe8b for #1966, but fixes #3684 by allowing numbers to be bound again. If the user wants to bind numbers instead of using them for a count, why not let them. --- qutebrowser/config/configtypes.py | 9 +-------- qutebrowser/keyinput/basekeyparser.py | 20 +++++++++++++------- tests/unit/config/test_configtypes.py | 4 +++- tests/unit/keyinput/conftest.py | 3 ++- tests/unit/keyinput/test_basekeyparser.py | 8 +++++--- 5 files changed, 24 insertions(+), 20 deletions(-) diff --git a/qutebrowser/config/configtypes.py b/qutebrowser/config/configtypes.py index 217e94505..14855bf03 100644 --- a/qutebrowser/config/configtypes.py +++ b/qutebrowser/config/configtypes.py @@ -1654,13 +1654,6 @@ class Key(BaseType): return None try: - seq = keyutils.KeySequence.parse(value) + return keyutils.KeySequence.parse(value) except keyutils.KeyParseError as e: raise configexc.ValidationError(value, str(e)) - - for info in seq: - if Qt.Key_1 <= info.key <= Qt.Key_9 and not info.modifiers: - raise configexc.ValidationError( - value, "Numbers are reserved for counts!") - - return seq diff --git a/qutebrowser/keyinput/basekeyparser.py b/qutebrowser/keyinput/basekeyparser.py index 6ebb0b464..c527b3cde 100644 --- a/qutebrowser/keyinput/basekeyparser.py +++ b/qutebrowser/keyinput/basekeyparser.py @@ -139,13 +139,6 @@ class BaseKeyParser(QObject): self._debug_log("Ignoring, only modifier") return QKeySequence.NoMatch - if (txt.isdigit() and self._supports_count and not - (not self._count and txt == '0')): - assert len(txt) == 1, txt - if not dry_run: - self._count += txt - return QKeySequence.ExactMatch - try: sequence = self._sequence.append_event(e) except keyutils.KeyParseError as ex: @@ -153,7 +146,10 @@ class BaseKeyParser(QObject): self.clear_keystring() return QKeySequence.NoMatch + # First, try a straightforward match match, binding = self._match_key(sequence) + + # If that doesn't match, try a key_mapping if match == QKeySequence.NoMatch: mapped = sequence.with_mappings(config.val.bindings.key_mappings) if sequence != mapped: @@ -162,6 +158,16 @@ class BaseKeyParser(QObject): match, binding = self._match_key(mapped) sequence = mapped + # If that doesn't match either, try treating it as count. + if (match == QKeySequence.NoMatch and + txt.isdigit() and + self._supports_count and + not (not self._count and txt == '0')): + assert len(txt) == 1, txt + if not dry_run: + self._count += txt + return QKeySequence.ExactMatch + if dry_run: return match diff --git a/tests/unit/config/test_configtypes.py b/tests/unit/config/test_configtypes.py index 074a54f23..4dd7837fb 100644 --- a/tests/unit/config/test_configtypes.py +++ b/tests/unit/config/test_configtypes.py @@ -2063,11 +2063,13 @@ class TestKey: ('', keyutils.KeySequence.parse('')), ('', keyutils.KeySequence.parse('')), ('0', keyutils.KeySequence.parse('0')), + ('1', keyutils.KeySequence.parse('1')), + ('a1', keyutils.KeySequence.parse('a1')), ]) def test_to_py_valid(self, klass, val, expected): assert klass().to_py(val) == expected - @pytest.mark.parametrize('val', ['\U00010000', '', '1', 'a1']) + @pytest.mark.parametrize('val', ['\U00010000', '']) def test_to_py_invalid(self, klass, val): with pytest.raises(configexc.ValidationError): klass().to_py(val) diff --git a/tests/unit/keyinput/conftest.py b/tests/unit/keyinput/conftest.py index 96d07f744..045b62336 100644 --- a/tests/unit/keyinput/conftest.py +++ b/tests/unit/keyinput/conftest.py @@ -33,7 +33,8 @@ BINDINGS = {'prompt': {'': 'message-info ctrla', 'ax': 'message-info ax', 'ccc': 'message-info ccc', 'yY': 'yank -s', - '0': 'message-info 0'}, + '0': 'message-info 0', + '1': 'message-info 1'}, 'command': {'foo': 'message-info bar', '': 'message-info ctrlx'}, 'normal': {'a': 'message-info a', 'ba': 'message-info ba'}} diff --git a/tests/unit/keyinput/test_basekeyparser.py b/tests/unit/keyinput/test_basekeyparser.py index 581d0b676..947c50592 100644 --- a/tests/unit/keyinput/test_basekeyparser.py +++ b/tests/unit/keyinput/test_basekeyparser.py @@ -192,9 +192,11 @@ class TestHandle: keyparser.execute.assert_called_with('message-info ba', None) assert not keyparser._sequence - def test_0_press(self, handle_text, keyparser): - handle_text(Qt.Key_0) - keyparser.execute.assert_called_once_with('message-info 0', None) + @pytest.mark.parametrize('key, number', [(Qt.Key_0, 0), (Qt.Key_1, 1)]) + def test_number_press(self, handle_text, keyparser, key, number): + handle_text(key) + command = 'message-info {}'.format(number) + keyparser.execute.assert_called_once_with(command, None) assert not keyparser._sequence def test_umlauts(self, handle_text, keyparser, config_stub): -- cgit v1.2.3-54-g00ecf