set_bindings can now update or replace existing bindings

This commit is contained in:
woorst
2017-07-19 18:24:04 -05:00
parent 47239b9bca
commit f43a4894fb
3 changed files with 18 additions and 13 deletions

View File

@@ -642,15 +642,17 @@ class KeyMap(object):
self._keymap = None
self.set_bindings(bindings)
def set_bindings(self, bindings):
# Clear the keymap before applying the bindings to avoid confusion.
# If a user defines custom bindings in their config file, they must
# explicitly define ALL of the bindings.
self._keymap = {}
def set_bindings(self, bindings, mode='update'):
new_keymap = {}
for command, keys in bindings.items():
if not isinstance(command, Command):
command = Command(command)
self._keymap[command] = keys
new_keymap[command] = keys
if not self._keymap or mode == 'replace':
self._keymap = new_keymap
elif mode == 'update':
self._keymap.update(new_keymap)
def get(self, command):
if not isinstance(command, Command):
@@ -694,4 +696,4 @@ class KeyMap(object):
except (AttributeError, ValueError, TypeError):
raise exceptions.ConfigError('Invalid configuration! "%s" is not a '
'valid key' % key)
'valid key' % key)