Allow unbinding keys.

This commit is contained in:
Florian Bruhin 2014-09-09 23:12:55 +02:00
parent 4fde56a942
commit c0e8352c95

View File

@ -123,11 +123,6 @@ class KeyConfigParser(QObject):
def bind(self, key, command, mode=None):
"""Bind a key to a command.
//
FIXME: We should use the KeyMode enum here, and some argparser type for
a comma-separated list of enums.
Args:
key: The keychain or special key (inside <...>) to bind.
command: The command to execute.
@ -136,6 +131,7 @@ class KeyConfigParser(QObject):
"""
if mode is None:
mode = 'normal'
mode = self._normalize_sectname(mode)
for m in mode.split(','):
if m not in configdata.KEY_DATA:
raise cmdexc.CommandError("Invalid mode {}!".format(m))
@ -148,6 +144,35 @@ class KeyConfigParser(QObject):
for m in mode.split(','):
self.changed.emit(m)
@cmdutils.register(instance='keyconfig')
def unbind(self, key, mode=None):
"""Unbind a keychain.
Args:
key: The keychain or special key (inside <...>) to bind.
mode: A comma-separated list of modes to unbind the key in
(default: normal mode).
"""
if mode is None:
mode = 'normal'
mode = self._normalize_sectname(mode)
for m in mode.split(','):
if m not in configdata.KEY_DATA:
raise cmdexc.CommandError("Invalid mode {}!".format(m))
try:
sect = self.keybindings[mode]
except KeyError as e:
raise cmdexc.CommandError("Can't find mode section '{}'!".format(
sect))
try:
del sect[key]
except KeyError as e:
raise cmdexc.CommandError("Can't find binding '{}' in section "
"'{}'!".format(key, mode))
else:
for m in mode.split(','):
self.changed.emit(m)
def _normalize_sectname(self, s):
"""Normalize a section string like 'foo, bar,baz' to 'bar,baz,foo'."""
return ','.join(sorted(s.split(',')))