Make new key config work (readonly)

This commit is contained in:
Florian Bruhin 2014-09-09 07:43:27 +02:00
parent 64183b5a26
commit 6f03f08111
4 changed files with 45 additions and 35 deletions

View File

@ -37,7 +37,7 @@ from PyQt5.QtCore import (pyqtSlot, QTimer, QEventLoop, Qt, QStandardPaths,
import qutebrowser import qutebrowser
from qutebrowser.commands import userscripts, runners, cmdutils from qutebrowser.commands import userscripts, runners, cmdutils
from qutebrowser.config import (style, config, websettings, iniparsers, from qutebrowser.config import (style, config, websettings, iniparsers,
lineparser, configtypes) lineparser, configtypes, keyconfparser)
from qutebrowser.network import qutescheme, proxy from qutebrowser.network import qutescheme, proxy
from qutebrowser.browser import quickmarks, cookies, downloads, cache from qutebrowser.browser import quickmarks, cookies, downloads, cache
from qutebrowser.widgets import mainwindow, console, crash from qutebrowser.widgets import mainwindow, console, crash
@ -190,6 +190,19 @@ class Application(QApplication):
msgbox.exec_() msgbox.exec_()
# We didn't really initialize much so far, so we just quit hard. # We didn't really initialize much so far, so we just quit hard.
sys.exit(1) sys.exit(1)
try:
self.keyconfig = keyconfparser.KeyConfigParser(confdir, 'keys')
except keyconfparser.KeyConfigError as e:
log.init.exception(e)
errstr = "Error while reading key config:\n"
if hasattr(e, 'lineno'):
errstr += "In line {}: ".format(e.lineno)
errstr += str(e)
msgbox = QMessageBox(QMessageBox.Critical,
"Error while reading key config!", errstr)
msgbox.exec_()
# We didn't really initialize much so far, so we just quit hard.
sys.exit(1)
self.stateconfig = iniparsers.ReadWriteConfigParser(confdir, 'state') self.stateconfig = iniparsers.ReadWriteConfigParser(confdir, 'state')
self.cmd_history = lineparser.LineConfigParser( self.cmd_history = lineparser.LineConfigParser(
confdir, 'cmd_history', ('completion', 'history-length')) confdir, 'cmd_history', ('completion', 'history-length'))
@ -202,14 +215,13 @@ class Application(QApplication):
utypes.KeyMode.hint: utypes.KeyMode.hint:
modeparsers.HintKeyParser(self), modeparsers.HintKeyParser(self),
utypes.KeyMode.insert: utypes.KeyMode.insert:
keyparser.PassthroughKeyParser('keybind.insert', self), keyparser.PassthroughKeyParser('insert', self),
utypes.KeyMode.passthrough: utypes.KeyMode.passthrough:
keyparser.PassthroughKeyParser('keybind.passthrough', self), keyparser.PassthroughKeyParser('passthrough', self),
utypes.KeyMode.command: utypes.KeyMode.command:
keyparser.PassthroughKeyParser('keybind.command', self), keyparser.PassthroughKeyParser('command', self),
utypes.KeyMode.prompt: utypes.KeyMode.prompt:
keyparser.PassthroughKeyParser('keybind.prompt', self, keyparser.PassthroughKeyParser('prompt', self, warn=False),
warn=False),
utypes.KeyMode.yesno: utypes.KeyMode.yesno:
modeparsers.PromptKeyParser(self), modeparsers.PromptKeyParser(self),
} }

View File

@ -23,7 +23,7 @@ import re
import string import string
import functools import functools
from PyQt5.QtCore import pyqtSignal, pyqtSlot, Qt, QObject from PyQt5.QtCore import pyqtSignal, pyqtSlot, Qt, QObject, QCoreApplication
from qutebrowser.config import config from qutebrowser.config import config
from qutebrowser.utils import usertypes, log, utils from qutebrowser.utils import usertypes, log, utils
@ -57,7 +57,7 @@ class BaseKeyParser(QObject):
keychains in a section which does not support them. keychains in a section which does not support them.
_keystring: The currently entered key sequence _keystring: The currently entered key sequence
_timer: Timer for delayed execution. _timer: Timer for delayed execution.
_confsectname: The name of the configsection. _modename: The name of the input mode associated with this keyparser.
_supports_count: Whether count is supported _supports_count: Whether count is supported
_supports_chains: Whether keychains are supported _supports_chains: Whether keychains are supported
@ -77,7 +77,7 @@ class BaseKeyParser(QObject):
supports_chains=False): supports_chains=False):
super().__init__(parent) super().__init__(parent)
self._timer = None self._timer = None
self._confsectname = None self._modename = None
self._keystring = '' self._keystring = ''
if supports_count is None: if supports_count is None:
supports_count = supports_chains supports_count = supports_chains
@ -303,28 +303,26 @@ class BaseKeyParser(QObject):
self.keystring_updated.emit(self._keystring) self.keystring_updated.emit(self._keystring)
return handled return handled
def read_config(self, sectname=None): def read_config(self, modename=None):
"""Read the configuration. """Read the configuration.
Config format: key = command, e.g.: Config format: key = command, e.g.:
<Ctrl+Q> = quit <Ctrl+Q> = quit
Args: Args:
sectname: Name of the section to read. modename: Name of the mode to use.
""" """
if sectname is None: if modename is None:
if self._confsectname is None: if self._modename is None:
raise ValueError("read_config called with no section, but " raise ValueError("read_config called with no mode given, but "
"None defined so far!") "None defined so far!")
sectname = self._confsectname modename = self._modename
else: else:
self._confsectname = sectname self._modename = modename
self.bindings = {} self.bindings = {}
self.special_bindings = {} self.special_bindings = {}
sect = config.section(sectname) keyconfparser = QCoreApplication.instance().keyconfig
if not sect.items(): for (key, cmd) in keyconfparser.get_bindings_for(modename).items():
log.keyboard.warning("No keybindings defined!")
for (key, cmd) in sect.items():
if not cmd: if not cmd:
continue continue
elif key.startswith('<') and key.endswith('>'): elif key.startswith('<') and key.endswith('>'):
@ -334,8 +332,8 @@ class BaseKeyParser(QObject):
self.bindings[key] = cmd self.bindings[key] = cmd
elif self.warn_on_keychains: elif self.warn_on_keychains:
log.keyboard.warning( log.keyboard.warning(
"Ignoring keychain '{}' in section '{}' because " "Ignoring keychain '{}' in mode '{}' because "
"keychains are not supported there.".format(key, sectname)) "keychains are not supported there.".format(key, modename))
def execute(self, cmdstr, keytype, count=None): def execute(self, cmdstr, keytype, count=None):
"""Handle a completed keychain. """Handle a completed keychain.
@ -350,8 +348,8 @@ class BaseKeyParser(QObject):
@pyqtSlot(str, str) @pyqtSlot(str, str)
def on_config_changed(self, section, _option): def on_config_changed(self, section, _option):
"""Re-read the config if a keybinding was changed.""" """Re-read the config if a keybinding was changed."""
if self._confsectname is None: if self._modename is None:
raise AttributeError("on_config_changed called but no section " raise AttributeError("on_config_changed called but no section "
"defined!") "defined!")
if section == self._confsectname: if section == self._modename:
self.read_config() self.read_config()

View File

@ -51,25 +51,25 @@ class PassthroughKeyParser(CommandKeyParser):
Used for insert/passthrough modes. Used for insert/passthrough modes.
Attributes: Attributes:
_confsect: The config section to use. _mode: The mode this keyparser is for.
""" """
do_log = False do_log = False
def __init__(self, confsect, parent=None, warn=True): def __init__(self, mode, parent=None, warn=True):
"""Constructor. """Constructor.
Args: Args:
confsect: The config section to use. mode: The mode this keyparser is for.
parent: Qt parent. parent: Qt parent.
warn: Whether to warn if an ignored key was bound. warn: Whether to warn if an ignored key was bound.
""" """
super().__init__(parent, supports_chains=False) super().__init__(parent, supports_chains=False)
self.log = False self.log = False
self.warn_on_keychains = warn self.warn_on_keychains = warn
self.read_config(confsect) self.read_config(mode)
self._confsect = confsect self._mode = mode
def __repr__(self): def __repr__(self):
return '<{} confsect={}, warn={})'.format( return '<{} mode={}, warn={})'.format(
self.__class__.__name__, self._confsect, self.warn_on_keychains) self.__class__.__name__, self._mode, self.warn_on_keychains)

View File

@ -41,7 +41,7 @@ class NormalKeyParser(keyparser.CommandKeyParser):
def __init__(self, parent=None): def __init__(self, parent=None):
super().__init__(parent, supports_count=True, supports_chains=True) super().__init__(parent, supports_count=True, supports_chains=True)
self.read_config('keybind') self.read_config('normal')
def __repr__(self): def __repr__(self):
return '<{}>'.format(self.__class__.__name__) return '<{}>'.format(self.__class__.__name__)
@ -69,8 +69,8 @@ class PromptKeyParser(keyparser.CommandKeyParser):
def __init__(self, parent=None): def __init__(self, parent=None):
super().__init__(parent, supports_count=False, supports_chains=True) super().__init__(parent, supports_count=False, supports_chains=True)
# We don't want an extra section for this in the config, so we just # We don't want an extra section for this in the config, so we just
# abuse the keybind.prompt section. # abuse the prompt section.
self.read_config('keybind.prompt') self.read_config('prompt')
def __repr__(self): def __repr__(self):
return '<{}>'.format(self.__class__.__name__) return '<{}>'.format(self.__class__.__name__)
@ -98,7 +98,7 @@ class HintKeyParser(keyparser.CommandKeyParser):
super().__init__(parent, supports_count=False, supports_chains=True) super().__init__(parent, supports_count=False, supports_chains=True)
self._filtertext = '' self._filtertext = ''
self._last_press = LastPress.none self._last_press = LastPress.none
self.read_config('keybind.hint') self.read_config('hint')
def _handle_special_key(self, e): def _handle_special_key(self, e):
"""Override _handle_special_key to handle string filtering. """Override _handle_special_key to handle string filtering.