From ec3ad8a9698f6c936a0f63281ea34ae16101f03e Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Tue, 27 Feb 2018 10:55:16 +0100 Subject: Get rid of _warn_on_keychains and _supports_chains --- qutebrowser/keyinput/modeman.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'qutebrowser/keyinput/modeman.py') diff --git a/qutebrowser/keyinput/modeman.py b/qutebrowser/keyinput/modeman.py index e32830f50..75e3af367 100644 --- a/qutebrowser/keyinput/modeman.py +++ b/qutebrowser/keyinput/modeman.py @@ -71,8 +71,7 @@ def init(win_id, parent): KM.passthrough: keyparser.PassthroughKeyParser(win_id, 'passthrough', modeman), KM.command: keyparser.PassthroughKeyParser(win_id, 'command', modeman), - KM.prompt: keyparser.PassthroughKeyParser(win_id, 'prompt', modeman, - warn=False), + KM.prompt: keyparser.PassthroughKeyParser(win_id, 'prompt', modeman), KM.yesno: modeparsers.PromptKeyParser(win_id, modeman), KM.caret: modeparsers.CaretKeyParser(win_id, modeman), KM.set_mark: modeparsers.RegisterKeyParser(win_id, KM.set_mark, -- cgit v1.2.3-54-g00ecf From b85fe8f678a39deabbd4498ce579b15f88f58803 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Tue, 27 Feb 2018 14:07:20 +0100 Subject: Merge BaseKeyParser._handle_key into .handle --- qutebrowser/keyinput/basekeyparser.py | 72 ++++++++++++++--------------------- qutebrowser/keyinput/modeman.py | 8 ++-- qutebrowser/keyinput/modeparsers.py | 45 +++++++++------------- 3 files changed, 51 insertions(+), 74 deletions(-) (limited to 'qutebrowser/keyinput/modeman.py') diff --git a/qutebrowser/keyinput/basekeyparser.py b/qutebrowser/keyinput/basekeyparser.py index ec1004316..27d760d9a 100644 --- a/qutebrowser/keyinput/basekeyparser.py +++ b/qutebrowser/keyinput/basekeyparser.py @@ -88,7 +88,30 @@ class BaseKeyParser(QObject): if self.do_log: log.keyboard.debug(message) - def _handle_key(self, e): + def _match_key(self, sequence): + """Try to match a given keystring with any bound keychain. + + Args: + sequence: The command string to find. + + Return: + A tuple (matchtype, binding). + matchtype: Match.definitive, Match.partial or Match.none. + binding: - None with Match.partial/Match.none. + - The found binding with Match.definitive. + """ + assert sequence + assert not isinstance(sequence, str) + + for seq, cmd in self.bindings.items(): + assert not isinstance(seq, str), seq + match = sequence.matches(seq) + if match != QKeySequence.NoMatch: + return (match, cmd) + + return (QKeySequence.NoMatch, None) + + def handle(self, e): """Handle a new keypress. Separate the keypress into count/command, then check if it matches @@ -99,7 +122,7 @@ class BaseKeyParser(QObject): e: the KeyPressEvent from Qt. Return: - A QKeySequence match or None. + A QKeySequence match. """ key = e.key() txt = keyutils.keyevent_to_string(e) @@ -123,7 +146,7 @@ class BaseKeyParser(QObject): (not self._count and txt == '0')): assert len(txt) == 1, txt self._count += txt - return None + return QKeySequence.ExactMatch sequence = self._sequence.append_event(e) match, binding = self._match_key(sequence) @@ -134,6 +157,7 @@ class BaseKeyParser(QObject): match, binding = self._match_key(mapped) self._sequence = self._sequence.append_event(e) + if match == QKeySequence.ExactMatch: self._debug_log("Definitive match for '{}'.".format( self._sequence)) @@ -143,53 +167,15 @@ class BaseKeyParser(QObject): elif match == QKeySequence.PartialMatch: self._debug_log("No match for '{}' (added {})".format( self._sequence, txt)) + self.keystring_updated.emit(self._count + str(self._sequence)) elif match == QKeySequence.NoMatch: self._debug_log("Giving up with '{}', no matches".format( self._sequence)) self.clear_keystring() else: raise utils.Unreachable("Invalid match value {!r}".format(match)) - return match - def _match_key(self, sequence): - """Try to match a given keystring with any bound keychain. - - Args: - sequence: The command string to find. - - Return: - A tuple (matchtype, binding). - matchtype: Match.definitive, Match.partial or Match.none. - binding: - None with Match.partial/Match.none. - - The found binding with Match.definitive. - """ - assert sequence - assert not isinstance(sequence, str) - - for seq, cmd in self.bindings.items(): - assert not isinstance(seq, str), seq - match = sequence.matches(seq) - if match != QKeySequence.NoMatch: - return (match, cmd) - - return (QKeySequence.NoMatch, None) - - def handle(self, e): - """Handle a new keypress and call the respective handlers. - - Args: - e: the KeyPressEvent from Qt - - Return: - True if the event was handled, False otherwise. - """ - match = self._handle_key(e) - - # don't emit twice if the keystring was cleared in self.clear_keystring - if self._sequence: - self.keystring_updated.emit(self._count + str(self._sequence)) - - return match != QKeySequence.NoMatch + return match @config.change_filter('bindings') def _on_config_changed(self): diff --git a/qutebrowser/keyinput/modeman.py b/qutebrowser/keyinput/modeman.py index 75e3af367..94d76832d 100644 --- a/qutebrowser/keyinput/modeman.py +++ b/qutebrowser/keyinput/modeman.py @@ -157,7 +157,7 @@ class ModeManager(QObject): if curmode != usertypes.KeyMode.insert: log.modes.debug("got keypress in mode {} - delegating to " "{}".format(curmode, utils.qualname(parser))) - handled = parser.handle(event) + match = parser.handle(event) is_non_alnum = ( event.modifiers() not in [Qt.NoModifier, Qt.ShiftModifier] or @@ -165,7 +165,7 @@ class ModeManager(QObject): forward_unbound_keys = config.val.input.forward_unbound_keys - if handled: + if match: filter_this = True elif (parser.passthrough or forward_unbound_keys == 'all' or (forward_unbound_keys == 'auto' and is_non_alnum)): @@ -178,10 +178,10 @@ class ModeManager(QObject): if curmode != usertypes.KeyMode.insert: focus_widget = QApplication.instance().focusWidget() - log.modes.debug("handled: {}, forward_unbound_keys: {}, " + log.modes.debug("match: {}, forward_unbound_keys: {}, " "passthrough: {}, is_non_alnum: {} --> " "filter: {} (focused: {!r})".format( - handled, forward_unbound_keys, + match, forward_unbound_keys, parser.passthrough, is_non_alnum, filter_this, focus_widget)) return filter_this diff --git a/qutebrowser/keyinput/modeparsers.py b/qutebrowser/keyinput/modeparsers.py index 9c44e4818..f397b9fd5 100644 --- a/qutebrowser/keyinput/modeparsers.py +++ b/qutebrowser/keyinput/modeparsers.py @@ -153,8 +153,8 @@ class HintKeyParser(keyparser.CommandKeyParser): self._read_config('hint') self.keystring_updated.connect(self.on_keystring_updated) - def _handle_special_key(self, e): - """Override _handle_special_key to handle string filtering. + def _handle_filter_key(self, e): + """Handle keys for string filtering. Return True if the keypress has been handled, and False if not. @@ -162,10 +162,8 @@ class HintKeyParser(keyparser.CommandKeyParser): e: the KeyPressEvent from Qt. Return: - True if event has been handled, False otherwise. + A QKeySequence match. """ - # FIXME rewrite this - # FIXME should backspacing be a more generic hint feature? log.keyboard.debug("Got special key 0x{:x} text {}".format( e.key(), e.text())) hintmanager = objreg.get('hintmanager', scope='tab', @@ -178,7 +176,7 @@ class HintKeyParser(keyparser.CommandKeyParser): if self._last_press == LastPress.filtertext and self._filtertext: self._filtertext = self._filtertext[:-1] hintmanager.filter_hints(self._filtertext) - return True + return QKeySequence.ExactMatch elif self._last_press == LastPress.keystring and self._sequence: self._sequence = self._sequence[:-1] self.keystring_updated.emit(str(self._sequence)) @@ -187,18 +185,18 @@ class HintKeyParser(keyparser.CommandKeyParser): # in numeric mode after the number has been deleted). hintmanager.filter_hints(self._filtertext) self._last_press = LastPress.filtertext - return True + return QKeySequence.ExactMatch else: - return False + return QKeySequence.NoMatch elif hintmanager.current_mode() != 'number': - return False + return QKeySequence.NoMatch elif not e.text(): - return False + return QKeySequence.NoMatch else: self._filtertext += e.text() hintmanager.filter_hints(self._filtertext) self._last_press = LastPress.filtertext - return True + return QKeySequence.ExactMatch def handle(self, e): """Handle a new keypress and call the respective handlers. @@ -209,25 +207,18 @@ class HintKeyParser(keyparser.CommandKeyParser): Returns: True if the match has been handled, False otherwise. """ - # FIXME rewrite this - match = self._handle_key(e) + match = super().handle(e) if match == QKeySequence.PartialMatch: - # FIXME do we need to check self._sequence here? - self.keystring_updated.emit(str(self._sequence)) self._last_press = LastPress.keystring - return True elif match == QKeySequence.ExactMatch: self._last_press = LastPress.none - return True - elif match is None: # FIXME - return None elif match == QKeySequence.NoMatch: # We couldn't find a keychain so we check if it's a special key. - return self._handle_special_key(e) + return self._handle_filter_key(e) else: raise ValueError("Got invalid match type {}!".format(match)) - return match != QKeySequence.NoMatch + return match def update_bindings(self, strings, preserve_filter=False): """Update bindings when the hint strings changed. @@ -285,15 +276,16 @@ class RegisterKeyParser(keyparser.CommandKeyParser): Return: True if event has been handled, False otherwise. """ - # FIXME rewrite this - if super().handle(e): - return True + match = super().handle(e) + if match: + return match key = e.text() if key == '' or keyutils.keyevent_to_string(e) is None: # this is not a proper register key, let it pass and keep going - return False + # FIXME can we simplify this when we refactor keyutils.py? + return QKeySequence.NoMatch tabbed_browser = objreg.get('tabbed-browser', scope='window', window=self._win_id) @@ -315,5 +307,4 @@ class RegisterKeyParser(keyparser.CommandKeyParser): message.error(str(err), stack=traceback.format_exc()) self.request_leave.emit(self._mode, "valid register key", True) - - return True + return QKeySequence.ExactMatch -- cgit v1.2.3-54-g00ecf From e01db79ce9e28da1bfb5c8a09fe3a686932c5760 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Mon, 5 Mar 2018 06:32:54 +0100 Subject: Filter out ShortcutOverride events properly Fixes #3419 --- doc/changelog.asciidoc | 2 ++ qutebrowser/app.py | 1 + qutebrowser/keyinput/basekeyparser.py | 33 +++++++++++++++++++++------------ qutebrowser/keyinput/modeman.py | 17 ++++++++++------- qutebrowser/keyinput/modeparsers.py | 25 +++++++++++++++++-------- 5 files changed, 51 insertions(+), 27 deletions(-) (limited to 'qutebrowser/keyinput/modeman.py') diff --git a/doc/changelog.asciidoc b/doc/changelog.asciidoc index 218a1d105..b38db88fa 100644 --- a/doc/changelog.asciidoc +++ b/doc/changelog.asciidoc @@ -99,6 +99,8 @@ Fixed - QtWebEngine: `:follow-selected` should now work in more cases with Qt > 5.10. - QtWebEngine: Incremental search now flickers less and doesn't move to the second result when pressing Enter. +- QtWebEngine: Keys like `Ctrl-V` or `Shift-Insert` are now correctly + handled/filtered with Qt 5.10. - QtWebKit: `:view-source` now displays a valid URL. - URLs containing ampersands and other special chars are now shown correctly when filtering them in the completion. diff --git a/qutebrowser/app.py b/qutebrowser/app.py index c755c2f41..2794d36e2 100644 --- a/qutebrowser/app.py +++ b/qutebrowser/app.py @@ -882,6 +882,7 @@ class EventFilter(QObject): self._handlers = { QEvent.KeyPress: self._handle_key_event, QEvent.KeyRelease: self._handle_key_event, + QEvent.ShortcutOverride: self._handle_key_event, } def _handle_key_event(self, event): diff --git a/qutebrowser/keyinput/basekeyparser.py b/qutebrowser/keyinput/basekeyparser.py index 901e96b55..beb31a52c 100644 --- a/qutebrowser/keyinput/basekeyparser.py +++ b/qutebrowser/keyinput/basekeyparser.py @@ -114,7 +114,7 @@ class BaseKeyParser(QObject): return (result, None) - def handle(self, e): + def handle(self, e, *, dry_run=False): """Handle a new keypress. Separate the keypress into count/command, then check if it matches @@ -123,13 +123,16 @@ class BaseKeyParser(QObject): Args: e: the KeyPressEvent from Qt. + dry_run: Don't actually execute anything, only check whether there + would be a match. Return: A QKeySequence match. """ key = e.key() txt = str(keyutils.KeyInfo.from_event(e)) - self._debug_log("Got key: 0x{:x} / text: '{}'".format(key, txt)) + self._debug_log("Got key: 0x{:x} / text: '{}' / dry_run {}".format( + key, txt, dry_run)) if keyutils.is_modifier_key(key): self._debug_log("Ignoring, only modifier") @@ -138,33 +141,39 @@ class BaseKeyParser(QObject): if (txt.isdigit() and self._supports_count and not (not self._count and txt == '0')): assert len(txt) == 1, txt - self._count += txt + if not dry_run: + self._count += txt return QKeySequence.ExactMatch - self._sequence = self._sequence.append_event(e) - match, binding = self._match_key(self._sequence) + sequence = self._sequence.append_event(e) + match, binding = self._match_key(sequence) if match == QKeySequence.NoMatch: mappings = config.val.bindings.key_mappings - mapped = mappings.get(self._sequence, None) + mapped = mappings.get(sequence, None) if mapped is not None: self._debug_log("Mapped {} -> {}".format( - self._sequence, mapped)) + sequence, mapped)) match, binding = self._match_key(mapped) - self._sequence = mapped + sequence = mapped + + if dry_run: + return match + + self._sequence = sequence if match == QKeySequence.ExactMatch: self._debug_log("Definitive match for '{}'.".format( - self._sequence)) + sequence)) count = int(self._count) if self._count else None self.clear_keystring() self.execute(binding, count) elif match == QKeySequence.PartialMatch: self._debug_log("No match for '{}' (added {})".format( - self._sequence, txt)) - self.keystring_updated.emit(self._count + str(self._sequence)) + sequence, txt)) + self.keystring_updated.emit(self._count + str(sequence)) elif match == QKeySequence.NoMatch: self._debug_log("Giving up with '{}', no matches".format( - self._sequence)) + sequence)) self.clear_keystring() else: raise utils.Unreachable("Invalid match value {!r}".format(match)) diff --git a/qutebrowser/keyinput/modeman.py b/qutebrowser/keyinput/modeman.py index 94d76832d..8d2830cc1 100644 --- a/qutebrowser/keyinput/modeman.py +++ b/qutebrowser/keyinput/modeman.py @@ -143,11 +143,12 @@ class ModeManager(QObject): def __repr__(self): return utils.get_repr(self, mode=self.mode) - def _eventFilter_keypress(self, event): + def _eventFilter_keypress(self, event, *, dry_run=False): """Handle filtering of KeyPress events. Args: event: The KeyPress to examine. + dry_run: Don't actually handle the key, only filter it. Return: True if event should be filtered, False otherwise. @@ -157,7 +158,7 @@ class ModeManager(QObject): if curmode != usertypes.KeyMode.insert: log.modes.debug("got keypress in mode {} - delegating to " "{}".format(curmode, utils.qualname(parser))) - match = parser.handle(event) + match = parser.handle(event, dry_run=dry_run) is_non_alnum = ( event.modifiers() not in [Qt.NoModifier, Qt.ShiftModifier] or @@ -173,17 +174,17 @@ class ModeManager(QObject): else: filter_this = True - if not filter_this: + if not filter_this and not dry_run: self._releaseevents_to_pass.add(KeyEvent.from_event(event)) if curmode != usertypes.KeyMode.insert: focus_widget = QApplication.instance().focusWidget() log.modes.debug("match: {}, forward_unbound_keys: {}, " - "passthrough: {}, is_non_alnum: {} --> " - "filter: {} (focused: {!r})".format( + "passthrough: {}, is_non_alnum: {}, dry_run: {} " + "--> filter: {} (focused: {!r})".format( match, forward_unbound_keys, - parser.passthrough, is_non_alnum, filter_this, - focus_widget)) + parser.passthrough, is_non_alnum, dry_run, + filter_this, focus_widget)) return filter_this def _eventFilter_keyrelease(self, event): @@ -320,6 +321,8 @@ class ModeManager(QObject): handlers = { QEvent.KeyPress: self._eventFilter_keypress, QEvent.KeyRelease: self._eventFilter_keyrelease, + QEvent.ShortcutOverride: + functools.partial(self._eventFilter_keypress, dry_run=True), } handler = handlers[event.type()] return handler(event) diff --git a/qutebrowser/keyinput/modeparsers.py b/qutebrowser/keyinput/modeparsers.py index 169232e01..ce4cb71fa 100644 --- a/qutebrowser/keyinput/modeparsers.py +++ b/qutebrowser/keyinput/modeparsers.py @@ -59,11 +59,13 @@ class NormalKeyParser(keyparser.CommandKeyParser): def __repr__(self): return utils.get_repr(self) - def handle(self, e): + def handle(self, e, *, dry_run=False): """Override to abort if the key is a startchar. Args: e: the KeyPressEvent from Qt. + dry_run: Don't actually execute anything, only check whether there + would be a match. Return: A self.Match member. @@ -74,9 +76,9 @@ class NormalKeyParser(keyparser.CommandKeyParser): "currently inhibited.".format(txt)) return QKeySequence.NoMatch - match = super().handle(e) + match = super().handle(e, dry_run=dry_run) - if match == QKeySequence.PartialMatch: + if match == QKeySequence.PartialMatch and not dry_run: timeout = config.val.input.partial_timeout if timeout != 0: self._partial_timer.setInterval(timeout) @@ -198,16 +200,21 @@ class HintKeyParser(keyparser.CommandKeyParser): self._last_press = LastPress.filtertext return QKeySequence.ExactMatch - def handle(self, e): + def handle(self, e, *, dry_run=False): """Handle a new keypress and call the respective handlers. Args: e: the KeyPressEvent from Qt + dry_run: Don't actually execute anything, only check whether there + would be a match. Returns: True if the match has been handled, False otherwise. """ - match = super().handle(e) + match = super().handle(e, dry_run=dry_run) + if dry_run: + return match + if match == QKeySequence.PartialMatch: self._last_press = LastPress.keystring elif match == QKeySequence.ExactMatch: @@ -267,17 +274,19 @@ class RegisterKeyParser(keyparser.CommandKeyParser): self._mode = mode self._read_config('register') - def handle(self, e): + def handle(self, e, *, dry_run=False): """Override handle to always match the next key and use the register. Args: e: the KeyPressEvent from Qt. + dry_run: Don't actually execute anything, only check whether there + would be a match. Return: True if event has been handled, False otherwise. """ - match = super().handle(e) - if match: + match = super().handle(e, dry_run=dry_run) + if match or dry_run: return match if not keyutils.is_printable(e.key()): -- cgit v1.2.3-54-g00ecf From 274f2a9d199684120ec0c66e805410a3a920fb46 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Mon, 5 Mar 2018 06:36:01 +0100 Subject: Rename eventFilter methods in modeman --- qutebrowser/app.py | 2 +- qutebrowser/keyinput/modeman.py | 12 ++++++------ tests/unit/keyinput/test_modeman.py | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) (limited to 'qutebrowser/keyinput/modeman.py') diff --git a/qutebrowser/app.py b/qutebrowser/app.py index 2794d36e2..73b5557cc 100644 --- a/qutebrowser/app.py +++ b/qutebrowser/app.py @@ -900,7 +900,7 @@ class EventFilter(QObject): return False try: man = objreg.get('mode-manager', scope='window', window='current') - return man.eventFilter(event) + return man.handle_event(event) except objreg.RegistryUnavailableError: # No window available yet, or not a MainWindow return False diff --git a/qutebrowser/keyinput/modeman.py b/qutebrowser/keyinput/modeman.py index 8d2830cc1..168efde5b 100644 --- a/qutebrowser/keyinput/modeman.py +++ b/qutebrowser/keyinput/modeman.py @@ -143,7 +143,7 @@ class ModeManager(QObject): def __repr__(self): return utils.get_repr(self, mode=self.mode) - def _eventFilter_keypress(self, event, *, dry_run=False): + def _handle_keypress(self, event, *, dry_run=False): """Handle filtering of KeyPress events. Args: @@ -187,7 +187,7 @@ class ModeManager(QObject): filter_this, focus_widget)) return filter_this - def _eventFilter_keyrelease(self, event): + def _handle_keyrelease(self, event): """Handle filtering of KeyRelease events. Args: @@ -303,7 +303,7 @@ class ModeManager(QObject): raise ValueError("Can't leave normal mode!") self.leave(self.mode, 'leave current') - def eventFilter(self, event): + def handle_event(self, event): """Filter all events based on the currently set mode. Also calls the real keypress handler. @@ -319,10 +319,10 @@ class ModeManager(QObject): return False handlers = { - QEvent.KeyPress: self._eventFilter_keypress, - QEvent.KeyRelease: self._eventFilter_keyrelease, + QEvent.KeyPress: self._handle_keypress, + QEvent.KeyRelease: self._handle_keyrelease, QEvent.ShortcutOverride: - functools.partial(self._eventFilter_keypress, dry_run=True), + functools.partial(self._handle_keypress, dry_run=True), } handler = handlers[event.type()] return handler(event) diff --git a/tests/unit/keyinput/test_modeman.py b/tests/unit/keyinput/test_modeman.py index de9671961..d94d7d38b 100644 --- a/tests/unit/keyinput/test_modeman.py +++ b/tests/unit/keyinput/test_modeman.py @@ -54,4 +54,4 @@ def modeman(mode_manager): def test_non_alphanumeric(key, modifiers, filtered, fake_keyevent, modeman): """Make sure non-alphanumeric keys are passed through correctly.""" evt = fake_keyevent(key=key, modifiers=modifiers) - assert modeman.eventFilter(evt) == filtered + assert modeman.handle_event(evt) == filtered -- cgit v1.2.3-54-g00ecf From 0cd73af691ff08d76eb1dd33f5bb45e9be2c250a Mon Sep 17 00:00:00 2001 From: jakanakae-envangel Date: Thu, 8 Mar 2018 18:06:54 +0100 Subject: keyinput: Merge keyparser into modeparsers --- qutebrowser/keyinput/keyparser.py | 74 ------------------------------------- qutebrowser/keyinput/modeman.py | 10 ++--- qutebrowser/keyinput/modeparsers.py | 62 +++++++++++++++++++++++++++---- 3 files changed, 60 insertions(+), 86 deletions(-) delete mode 100644 qutebrowser/keyinput/keyparser.py (limited to 'qutebrowser/keyinput/modeman.py') diff --git a/qutebrowser/keyinput/keyparser.py b/qutebrowser/keyinput/keyparser.py deleted file mode 100644 index 0ce123bfc..000000000 --- a/qutebrowser/keyinput/keyparser.py +++ /dev/null @@ -1,74 +0,0 @@ -# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et: - -# Copyright 2014-2018 Florian Bruhin (The Compiler) -# -# This file is part of qutebrowser. -# -# qutebrowser is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# qutebrowser is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with qutebrowser. If not, see . - -"""Advanced keyparsers.""" - -import traceback - -from qutebrowser.keyinput.basekeyparser import BaseKeyParser -from qutebrowser.utils import message, utils -from qutebrowser.commands import runners, cmdexc - - -class CommandKeyParser(BaseKeyParser): - - """KeyChainParser for command bindings. - - Attributes: - _commandrunner: CommandRunner instance. - """ - - def __init__(self, win_id, parent=None, supports_count=None): - super().__init__(win_id, parent, supports_count) - self._commandrunner = runners.CommandRunner(win_id) - - def execute(self, cmdstr, count=None): - try: - self._commandrunner.run(cmdstr, count) - except cmdexc.Error as e: - message.error(str(e), stack=traceback.format_exc()) - - -class PassthroughKeyParser(CommandKeyParser): - - """KeyChainParser which passes through normal keys. - - Used for insert/passthrough modes. - - Attributes: - _mode: The mode this keyparser is for. - """ - - do_log = False - passthrough = True - - def __init__(self, win_id, mode, parent=None): - """Constructor. - - Args: - mode: The mode this keyparser is for. - parent: Qt parent. - warn: Whether to warn if an ignored key was bound. - """ - super().__init__(win_id, parent) - self._read_config(mode) - self._mode = mode - - def __repr__(self): - return utils.get_repr(self, mode=self._mode) diff --git a/qutebrowser/keyinput/modeman.py b/qutebrowser/keyinput/modeman.py index 168efde5b..49a0bf68f 100644 --- a/qutebrowser/keyinput/modeman.py +++ b/qutebrowser/keyinput/modeman.py @@ -25,7 +25,7 @@ import attr from PyQt5.QtCore import pyqtSlot, pyqtSignal, Qt, QObject, QEvent from PyQt5.QtWidgets import QApplication -from qutebrowser.keyinput import modeparsers, keyparser +from qutebrowser.keyinput import modeparsers from qutebrowser.config import config from qutebrowser.commands import cmdexc, cmdutils from qutebrowser.utils import usertypes, log, objreg, utils @@ -67,11 +67,11 @@ def init(win_id, parent): keyparsers = { KM.normal: modeparsers.NormalKeyParser(win_id, modeman), KM.hint: modeparsers.HintKeyParser(win_id, modeman), - KM.insert: keyparser.PassthroughKeyParser(win_id, 'insert', modeman), - KM.passthrough: keyparser.PassthroughKeyParser(win_id, 'passthrough', + KM.insert: modeparsers.PassthroughKeyParser(win_id, 'insert', modeman), + KM.passthrough: modeparsers.PassthroughKeyParser(win_id, 'passthrough', modeman), - KM.command: keyparser.PassthroughKeyParser(win_id, 'command', modeman), - KM.prompt: keyparser.PassthroughKeyParser(win_id, 'prompt', modeman), + KM.command: modeparsers.PassthroughKeyParser(win_id, 'command', modeman), + KM.prompt: modeparsers.PassthroughKeyParser(win_id, 'prompt', modeman), KM.yesno: modeparsers.PromptKeyParser(win_id, modeman), KM.caret: modeparsers.CaretKeyParser(win_id, modeman), KM.set_mark: modeparsers.RegisterKeyParser(win_id, KM.set_mark, diff --git a/qutebrowser/keyinput/modeparsers.py b/qutebrowser/keyinput/modeparsers.py index ce4cb71fa..1f76138e0 100644 --- a/qutebrowser/keyinput/modeparsers.py +++ b/qutebrowser/keyinput/modeparsers.py @@ -29,9 +29,9 @@ import enum from PyQt5.QtCore import pyqtSlot, Qt from PyQt5.QtGui import QKeySequence -from qutebrowser.commands import cmdexc +from qutebrowser.commands import runners, cmdexc from qutebrowser.config import config -from qutebrowser.keyinput import keyparser, keyutils +from qutebrowser.keyinput import basekeyparser, keyutils from qutebrowser.utils import usertypes, log, message, objreg, utils @@ -39,7 +39,26 @@ STARTCHARS = ":/?" LastPress = enum.Enum('LastPress', ['none', 'filtertext', 'keystring']) -class NormalKeyParser(keyparser.CommandKeyParser): +class CommandKeyParser(basekeyparser.BaseKeyParser): + + """KeyChainParser for command bindings. + + Attributes: + _commandrunner: CommandRunner instance. + """ + + def __init__(self, win_id, parent=None, supports_count=None): + super().__init__(win_id, parent, supports_count) + self._commandrunner = runners.CommandRunner(win_id) + + def execute(self, cmdstr, count=None): + try: + self._commandrunner.run(cmdstr, count) + except cmdexc.Error as e: + message.error(str(e), stack=traceback.format_exc()) + + +class NormalKeyParser(CommandKeyParser): """KeyParser for normal mode with added STARTCHARS detection and more. @@ -127,7 +146,36 @@ class NormalKeyParser(keyparser.CommandKeyParser): pass -class PromptKeyParser(keyparser.CommandKeyParser): +class PassthroughKeyParser(CommandKeyParser): + + """KeyChainParser which passes through normal keys. + + Used for insert/passthrough modes. + + Attributes: + _mode: The mode this keyparser is for. + """ + + do_log = False + passthrough = True + + def __init__(self, win_id, mode, parent=None): + """Constructor. + + Args: + mode: The mode this keyparser is for. + parent: Qt parent. + warn: Whether to warn if an ignored key was bound. + """ + super().__init__(win_id, parent) + self._read_config(mode) + self._mode = mode + + def __repr__(self): + return utils.get_repr(self, mode=self._mode) + + +class PromptKeyParser(CommandKeyParser): """KeyParser for yes/no prompts.""" @@ -139,7 +187,7 @@ class PromptKeyParser(keyparser.CommandKeyParser): return utils.get_repr(self) -class HintKeyParser(keyparser.CommandKeyParser): +class HintKeyParser(CommandKeyParser): """KeyChainParser for hints. @@ -249,7 +297,7 @@ class HintKeyParser(keyparser.CommandKeyParser): hintmanager.handle_partial_key(keystr) -class CaretKeyParser(keyparser.CommandKeyParser): +class CaretKeyParser(CommandKeyParser): """KeyParser for caret mode.""" @@ -260,7 +308,7 @@ class CaretKeyParser(keyparser.CommandKeyParser): self._read_config('caret') -class RegisterKeyParser(keyparser.CommandKeyParser): +class RegisterKeyParser(CommandKeyParser): """KeyParser for modes that record a register key. -- cgit v1.2.3-54-g00ecf From b789e436b8634951d46684517fb762df5f95cd29 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Fri, 9 Mar 2018 07:07:04 +0100 Subject: Fix lint --- qutebrowser/keyinput/modeman.py | 41 ++++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) (limited to 'qutebrowser/keyinput/modeman.py') diff --git a/qutebrowser/keyinput/modeman.py b/qutebrowser/keyinput/modeman.py index 49a0bf68f..6e04ebe51 100644 --- a/qutebrowser/keyinput/modeman.py +++ b/qutebrowser/keyinput/modeman.py @@ -65,23 +65,30 @@ def init(win_id, parent): modeman = ModeManager(win_id, parent) objreg.register('mode-manager', modeman, scope='window', window=win_id) keyparsers = { - KM.normal: modeparsers.NormalKeyParser(win_id, modeman), - KM.hint: modeparsers.HintKeyParser(win_id, modeman), - KM.insert: modeparsers.PassthroughKeyParser(win_id, 'insert', modeman), - KM.passthrough: modeparsers.PassthroughKeyParser(win_id, 'passthrough', - modeman), - KM.command: modeparsers.PassthroughKeyParser(win_id, 'command', modeman), - KM.prompt: modeparsers.PassthroughKeyParser(win_id, 'prompt', modeman), - KM.yesno: modeparsers.PromptKeyParser(win_id, modeman), - KM.caret: modeparsers.CaretKeyParser(win_id, modeman), - KM.set_mark: modeparsers.RegisterKeyParser(win_id, KM.set_mark, - modeman), - KM.jump_mark: modeparsers.RegisterKeyParser(win_id, KM.jump_mark, - modeman), - KM.record_macro: modeparsers.RegisterKeyParser(win_id, KM.record_macro, - modeman), - KM.run_macro: modeparsers.RegisterKeyParser(win_id, KM.run_macro, - modeman), + KM.normal: + modeparsers.NormalKeyParser(win_id, modeman), + KM.hint: + modeparsers.HintKeyParser(win_id, modeman), + KM.insert: + modeparsers.PassthroughKeyParser(win_id, 'insert', modeman), + KM.passthrough: + modeparsers.PassthroughKeyParser(win_id, 'passthrough', modeman), + KM.command: + modeparsers.PassthroughKeyParser(win_id, 'command', modeman), + KM.prompt: + modeparsers.PassthroughKeyParser(win_id, 'prompt', modeman), + KM.yesno: + modeparsers.PromptKeyParser(win_id, modeman), + KM.caret: + modeparsers.CaretKeyParser(win_id, modeman), + KM.set_mark: + modeparsers.RegisterKeyParser(win_id, KM.set_mark, modeman), + KM.jump_mark: + modeparsers.RegisterKeyParser(win_id, KM.jump_mark, modeman), + KM.record_macro: + modeparsers.RegisterKeyParser(win_id, KM.record_macro, modeman), + KM.run_macro: + modeparsers.RegisterKeyParser(win_id, KM.run_macro, modeman), } objreg.register('keyparsers', keyparsers, scope='window', window=win_id) modeman.destroyed.connect( -- cgit v1.2.3-54-g00ecf