keyinput: Merge keyparser into modeparsers

This commit is contained in:
jakanakae-envangel 2018-03-08 18:06:54 +01:00
parent 63d23ca9df
commit 0cd73af691
3 changed files with 60 additions and 86 deletions

View File

@ -1,74 +0,0 @@
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
# Copyright 2014-2018 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
#
# 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 <http://www.gnu.org/licenses/>.
"""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)

View File

@ -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,

View File

@ -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.