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

View File

@ -135,9 +135,11 @@ class KeyConfig:
def _prepare(self, key, mode): def _prepare(self, key, mode):
"""Make sure the given mode exists and normalize the key.""" """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: if mode not in configdata.DATA['bindings.default'].default:
raise configexc.KeybindingError("Invalid mode {}!".format(mode)) raise configexc.KeybindingError("Invalid mode {}!".format(mode))
return str(keyutils.KeySequence.parse(key)) return key
def get_bindings_for(self, mode): def get_bindings_for(self, mode):
"""Get the combined bindings for the given mode.""" """Get the combined bindings for the given mode."""
@ -188,7 +190,7 @@ class KeyConfig:
bindings = self._config.get_obj('bindings.commands') bindings = self._config.get_obj('bindings.commands')
if mode not in bindings: if mode not in bindings:
bindings[mode] = {} bindings[mode] = {}
bindings[mode][key] = command bindings[mode][str(key)] = command
self._config.update_mutables(save_yaml=save_yaml) self._config.update_mutables(save_yaml=save_yaml)
def bind_default(self, key, *, mode='normal', save_yaml=False): def bind_default(self, key, *, mode='normal', save_yaml=False):
@ -197,7 +199,7 @@ class KeyConfig:
bindings_commands = self._config.get_obj('bindings.commands') bindings_commands = self._config.get_obj('bindings.commands')
try: try:
del bindings_commands[mode][key] del bindings_commands[mode][str(key)]
except KeyError: except KeyError:
raise configexc.KeybindingError( raise configexc.KeybindingError(
"Can't find binding '{}' in {} mode".format(key, mode)) "Can't find binding '{}' in {} mode".format(key, mode))
@ -209,14 +211,14 @@ class KeyConfig:
bindings_commands = self._config.get_obj('bindings.commands') 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 # In custom bindings -> remove it
del bindings_commands[mode][key] del bindings_commands[mode][str(key)]
elif key in val.bindings.default[mode]: elif key in val.bindings.default[mode]:
# In default bindings -> shadow it with None # In default bindings -> shadow it with None
if mode not in bindings_commands: if mode not in bindings_commands:
bindings_commands[mode] = {} bindings_commands[mode] = {}
bindings_commands[mode][key] = None bindings_commands[mode][str(key)] = None
else: else:
raise configexc.KeybindingError( raise configexc.KeybindingError(
"Can't find binding '{}' in {} mode".format(key, mode)) "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.utils import objreg, utils, message, standarddir
from qutebrowser.config import configtypes, configexc, configfiles, configdata from qutebrowser.config import configtypes, configexc, configfiles, configdata
from qutebrowser.misc import editor from qutebrowser.misc import editor
from qutebrowser.keyinput import keyutils
class ConfigCommands: class ConfigCommands:
@ -108,11 +109,12 @@ class ConfigCommands:
available modes. available modes.
default: If given, restore a default binding. default: If given, restore a default binding.
""" """
seq = keyutils.KeySequence(key)
if command is None: if command is None:
if default: if default:
# :bind --default: Restore default # :bind --default: Restore default
with self._handle_config_error(): with self._handle_config_error():
self._keyconfig.bind_default(key, mode=mode, self._keyconfig.bind_default(seq, mode=mode,
save_yaml=True) save_yaml=True)
return return
@ -122,7 +124,7 @@ class ConfigCommands:
# # normalized for the output below # # normalized for the output below
# key = utils.normalize_keystr(key) # key = utils.normalize_keystr(key)
with self._handle_config_error(): with self._handle_config_error():
cmd = self._keyconfig.get_command(key, mode) cmd = self._keyconfig.get_command(seq, mode)
if cmd is None: if cmd is None:
message.info("{} is unbound in {} mode".format(key, mode)) message.info("{} is unbound in {} mode".format(key, mode))
else: else:
@ -131,7 +133,7 @@ class ConfigCommands:
return return
with self._handle_config_error(): 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') @cmdutils.register(instance='config-commands')
def unbind(self, key, *, mode='normal'): def unbind(self, key, *, mode='normal'):
@ -143,7 +145,8 @@ class ConfigCommands:
See `:help bindings.commands` for the available modes. See `:help bindings.commands` for the available modes.
""" """
with self._handle_config_error(): 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.register(instance='config-commands', star_args_optional=True)
@cmdutils.argument('option', completion=configmodel.option) @cmdutils.argument('option', completion=configmodel.option)