Handle unknown keys with :bind/:unbind

This commit is contained in:
Florian Bruhin 2018-02-27 12:59:23 +01:00
parent 88b5007457
commit 244590f49d
2 changed files with 15 additions and 2 deletions

View File

@ -59,6 +59,13 @@ class ConfigCommands:
raise cmdexc.CommandError("Error while parsing {}: {}" raise cmdexc.CommandError("Error while parsing {}: {}"
.format(pattern, str(e))) .format(pattern, str(e)))
def _parse_key(self, key):
"""Parse a key argument."""
try:
return keyutils.KeySequence.parse(key)
except keyutils.KeyParseError as e:
raise cmdexc.CommandError(str(e))
def _print_value(self, option, pattern): def _print_value(self, option, pattern):
"""Print the value of the given option.""" """Print the value of the given option."""
with self._handle_config_error(): with self._handle_config_error():
@ -143,7 +150,7 @@ class ConfigCommands:
tabbed_browser.openurl(QUrl('qute://bindings'), newtab=True) tabbed_browser.openurl(QUrl('qute://bindings'), newtab=True)
return return
seq = keyutils.KeySequence.parse(key) seq = self._parse_key(key)
if command is None: if command is None:
if default: if default:
@ -176,7 +183,7 @@ 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(keyutils.KeySequence.parse(key), mode=mode, self._keyconfig.unbind(self._parse_key(key), mode=mode,
save_yaml=True) save_yaml=True)
@cmdutils.register(instance='config-commands', star_args_optional=True) @cmdutils.register(instance='config-commands', star_args_optional=True)

View File

@ -557,12 +557,18 @@ class TestBind:
# :bind --default foobar # :bind --default foobar
('bind', ['foobar'], {'default': True}, ('bind', ['foobar'], {'default': True},
"Can't find binding 'foobar' in normal mode"), "Can't find binding 'foobar' in normal mode"),
# :bind <blub> nop
('bind', ['<blub>', 'nop'], {},
"Could not parse '<blub>': Got unknown key!"),
# :unbind foobar # :unbind foobar
('unbind', ['foobar'], {}, ('unbind', ['foobar'], {},
"Can't find binding 'foobar' in normal mode"), "Can't find binding 'foobar' in normal mode"),
# :unbind --mode=wrongmode x # :unbind --mode=wrongmode x
('unbind', ['x'], {'mode': 'wrongmode'}, ('unbind', ['x'], {'mode': 'wrongmode'},
'Invalid mode wrongmode!'), 'Invalid mode wrongmode!'),
# :unbind <blub>
('unbind', ['<blub>'], {},
"Could not parse '<blub>': Got unknown key!"),
]) ])
def test_bind_invalid(self, commands, def test_bind_invalid(self, commands,
command, args, kwargs, expected): command, args, kwargs, expected):