Use KeySequences in config.py

This commit is contained in:
Florian Bruhin 2017-12-29 15:58:20 +01:00
parent 7b3cb14e6e
commit caa05df16d
3 changed files with 19 additions and 12 deletions

View File

@ -22,6 +22,7 @@
from qutebrowser.config import configdata, configexc
from qutebrowser.completion.models import completionmodel, listcategory, util
from qutebrowser.commands import runners, cmdexc
from qutebrowser.keyinput import keyutils
def option(*, info):
@ -79,8 +80,9 @@ def bind(key, *, info):
"""
model = completionmodel.CompletionModel(column_widths=(20, 60, 20))
data = []
seq = keyutils.KeySequence.parse(key)
cmd_text = info.keyconf.get_command(key, 'normal')
cmd_text = info.keyconf.get_command(seq, 'normal')
if cmd_text:
parser = runners.CommandParser()
try:
@ -90,7 +92,7 @@ def bind(key, *, info):
else:
data.append((cmd_text, '(Current) {}'.format(cmd.desc), key))
cmd_text = info.keyconf.get_command(key, 'normal', default=True)
cmd_text = info.keyconf.get_command(seq, 'normal', default=True)
if cmd_text:
parser = runners.CommandParser()
cmd = parser.parse(cmd_text).cmd

View File

@ -135,9 +135,11 @@ class KeyConfig:
def _prepare(self, key, mode):
"""Make sure the given mode exists and normalize the key."""
# Catch old usage of this code
assert isinstance(key, keyutils.KeySequence), key
if mode not in configdata.DATA['bindings.default'].default:
raise configexc.KeybindingError("Invalid mode {}!".format(mode))
return str(keyutils.KeySequence.parse(key))
return key
def get_bindings_for(self, mode):
"""Get the combined bindings for the given mode."""
@ -188,7 +190,7 @@ class KeyConfig:
bindings = self._config.get_obj('bindings.commands')
if mode not in bindings:
bindings[mode] = {}
bindings[mode][key] = command
bindings[mode][str(key)] = command
self._config.update_mutables(save_yaml=save_yaml)
def bind_default(self, key, *, mode='normal', save_yaml=False):
@ -197,7 +199,7 @@ class KeyConfig:
bindings_commands = self._config.get_obj('bindings.commands')
try:
del bindings_commands[mode][key]
del bindings_commands[mode][str(key)]
except KeyError:
raise configexc.KeybindingError(
"Can't find binding '{}' in {} mode".format(key, mode))
@ -209,14 +211,14 @@ class KeyConfig:
bindings_commands = self._config.get_obj('bindings.commands')
if val.bindings.commands[mode].get(key, None) is not None:
if str(key) in bindings_commands.get(mode, {}):
# In custom bindings -> remove it
del bindings_commands[mode][key]
del bindings_commands[mode][str(key)]
elif key in val.bindings.default[mode]:
# In default bindings -> shadow it with None
if mode not in bindings_commands:
bindings_commands[mode] = {}
bindings_commands[mode][key] = None
bindings_commands[mode][str(key)] = None
else:
raise configexc.KeybindingError(
"Can't find binding '{}' in {} mode".format(key, mode))

View File

@ -29,6 +29,7 @@ from qutebrowser.completion.models import configmodel
from qutebrowser.utils import objreg, utils, message, standarddir
from qutebrowser.config import configtypes, configexc, configfiles, configdata
from qutebrowser.misc import editor
from qutebrowser.keyinput import keyutils
class ConfigCommands:
@ -108,11 +109,12 @@ class ConfigCommands:
available modes.
default: If given, restore a default binding.
"""
seq = keyutils.KeySequence(key)
if command is None:
if default:
# :bind --default: Restore default
with self._handle_config_error():
self._keyconfig.bind_default(key, mode=mode,
self._keyconfig.bind_default(seq, mode=mode,
save_yaml=True)
return
@ -122,7 +124,7 @@ class ConfigCommands:
# # normalized for the output below
# key = utils.normalize_keystr(key)
with self._handle_config_error():
cmd = self._keyconfig.get_command(key, mode)
cmd = self._keyconfig.get_command(seq, mode)
if cmd is None:
message.info("{} is unbound in {} mode".format(key, mode))
else:
@ -131,7 +133,7 @@ class ConfigCommands:
return
with self._handle_config_error():
self._keyconfig.bind(key, command, mode=mode, save_yaml=True)
self._keyconfig.bind(seq, command, mode=mode, save_yaml=True)
@cmdutils.register(instance='config-commands')
def unbind(self, key, *, mode='normal'):
@ -143,7 +145,8 @@ class ConfigCommands:
See `:help bindings.commands` for the available modes.
"""
with self._handle_config_error():
self._keyconfig.unbind(key, mode=mode, save_yaml=True)
self._keyconfig.unbind(keyutils.KeySequence.parse(key), mode=mode,
save_yaml=True)
@cmdutils.register(instance='config-commands', star_args_optional=True)
@cmdutils.argument('option', completion=configmodel.option)