diff --git a/rtv/objects.py b/rtv/objects.py index b6a42a1..48f0074 100644 --- a/rtv/objects.py +++ b/rtv/objects.py @@ -643,14 +643,16 @@ class KeyMap(object): 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 = {} + 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: + self._keymap = new_keymap + else: + 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) \ No newline at end of file + 'valid key' % key) diff --git a/tests/test_config.py b/tests/test_config.py index 51c40b6..060a6e4 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -136,9 +136,10 @@ def test_config_from_file(): fargs, fbindings = Config.get_file(filename=fp.name) config = Config(**fargs) + default_keymap = config.keymap._keymap.copy() config.keymap.set_bindings(fbindings) assert config.config == {} - assert config.keymap._keymap == {} + assert config.keymap._keymap == default_keymap # [rtv] rows = ['{0}={1}'.format(key, val) for key, val in args.items()] @@ -224,4 +225,4 @@ def test_config_history(): config.delete_history() assert len(config.history) == 0 - assert not os.path.exists(fp.name) \ No newline at end of file + assert not os.path.exists(fp.name) diff --git a/tests/test_objects.py b/tests/test_objects.py index 1ac11ca..e3d8213 100644 --- a/tests/test_objects.py +++ b/tests/test_objects.py @@ -370,13 +370,17 @@ def test_objects_keymap(): keymap.get('downvote') assert 'DOWNVOTE' in six.text_type(e) - # Updating the bindings wipes out the old ones bindings = {'refresh': ['a', 0x12, '', '']} + bindings2 = {'upvote': ['b', 0x13, '']} + keymap._keymap = {} keymap.set_bindings(bindings) assert keymap.get('refresh') with pytest.raises(exceptions.ConfigError) as e: keymap.get('upvote') assert 'UPVOTE' in six.text_type(e) + keymap.set_bindings(bindings2) + assert keymap.get('refresh') + assert keymap.get('upvote') # Strings should be parsed correctly into keys assert KeyMap.parse('a') == 97 @@ -589,4 +593,4 @@ def test_objects_navigator_flip(): nav.flip(3) assert nav.page_index == 2 assert nav.cursor_index == 3 - assert not nav.inverted \ No newline at end of file + assert not nav.inverted