def bind(self, key, command, *, mode, force=False, save_yaml=False): """Add a new binding from key to command.""" key = self._prepare(key, mode) parser = runners.CommandParser() try: results = parser.parse_all(command) except cmdexc.Error as e: raise configexc.KeybindingError("Invalid command: {}".format(e)) for result in results: # pragma: no branch try: result.cmd.validate_mode(usertypes.KeyMode[mode]) except cmdexc.PrerequisitesError as e: raise configexc.KeybindingError(str(e)) log.keyboard.vdebug("Adding binding {} -> {} in mode {}.".format( key, command, mode)) if key in self.get_bindings_for(mode) and not force: raise configexc.DuplicateKeyError(key) bindings = self._config.get_obj('bindings.commands') if mode not in bindings: bindings[mode] = {} bindings[mode][key] = command self._config.update_mutables(save_yaml=save_yaml)
def _validate(self, key: keyutils.KeySequence, mode: str) -> None: """Validate the given key and mode.""" # Catch old usage of this code if not isinstance(key, keyutils.KeySequence): raise AssertionError(key) if mode not in configdata.DATA['bindings.default'].default: raise configexc.KeybindingError("Invalid mode {}!".format(mode))
def bind(self, key, command, mode='normal', *, force=False): with self._handle_error('binding', key): try: self._keyconfig.bind(key, command, mode=mode, force=force) except configexc.DuplicateKeyError as e: raise configexc.KeybindingError('{} - use force=True to ' 'override!'.format(e))
def _prepare(self, key, mode): """Make sure the given mode exists and normalize the key.""" if mode not in configdata.DATA['bindings.default'].default: raise configexc.KeybindingError("Invalid mode {}!".format(mode)) if utils.is_special_key(key): # <Ctrl-t>, <ctrl-T>, and <ctrl-t> should be considered equivalent return utils.normalize_keystr(key) return key
def bind_default(self, key, *, mode='normal', save_yaml=False): """Restore a default keybinding.""" key = self._prepare(key, mode) bindings_commands = self._config.get_obj('bindings.commands') try: del bindings_commands[mode][key] except KeyError: raise configexc.KeybindingError( "Can't find binding '{}' in {} mode".format(key, mode)) self._config.update_mutables(save_yaml=save_yaml)
def bind_default(self, key: keyutils.KeySequence, *, mode: str = 'normal', save_yaml: bool = False) -> None: """Restore a default keybinding.""" self._validate(key, mode) bindings_commands = self._config.get_mutable_obj('bindings.commands') try: del bindings_commands[mode][str(key)] except KeyError: raise configexc.KeybindingError( "Can't find binding '{}' in {} mode".format(key, mode)) self._config.update_mutables(save_yaml=save_yaml)
def bind(self, key, command, *, mode, save_yaml=False): """Add a new binding from key to command.""" if command is not None and not command.strip(): raise configexc.KeybindingError( "Can't add binding '{}' with empty command in {} " 'mode'.format(key, mode)) key = self._prepare(key, mode) log.keyboard.vdebug("Adding binding {} -> {} in mode {}.".format( key, command, mode)) bindings = self._config.get_obj('bindings.commands') if mode not in bindings: bindings[mode] = {} bindings[mode][key] = command self._config.update_mutables(save_yaml=save_yaml)
def unbind(self, key, *, mode='normal', save_yaml=False): """Unbind the given key in the given mode.""" key = self._prepare(key, mode) bindings_commands = self._config.get_obj('bindings.commands') if val.bindings.commands[mode].get(key, None) is not None: # In custom bindings -> remove it del bindings_commands[mode][key] elif key in val.bindings.default[mode]: # In default bindings -> shadow it with None if mode not in bindings_commands: bindings_commands[mode] = {} bindings_commands[mode][key] = None else: raise configexc.KeybindingError( "Can't find binding '{}' in {} mode".format(key, mode)) self._config.update_mutables(save_yaml=save_yaml)
def bind(self, key: keyutils.KeySequence, command: str, *, mode: str, save_yaml: bool = False) -> None: """Add a new binding from key to command.""" if not command.strip(): raise configexc.KeybindingError( "Can't add binding '{}' with empty command in {} " 'mode'.format(key, mode)) self._validate(key, mode) log.keyboard.vdebug( # type: ignore[attr-defined] "Adding binding {} -> {} in mode {}.".format(key, command, mode)) bindings = self._config.get_mutable_obj('bindings.commands') if mode not in bindings: bindings[mode] = {} bindings[mode][str(key)] = command self._config.update_mutables(save_yaml=save_yaml)
def unbind(self, key: keyutils.KeySequence, *, mode: str = 'normal', save_yaml: bool = False) -> None: """Unbind the given key in the given mode.""" self._validate(key, mode) bindings_commands = self._config.get_mutable_obj('bindings.commands') if val.bindings.commands[mode].get(key, None) is not None: # In custom bindings -> remove it del bindings_commands[mode][str(key)] elif key in val.bindings.default[mode]: # In default bindings -> shadow it with None if mode not in bindings_commands: bindings_commands[mode] = {} bindings_commands[mode][str(key)] = None else: raise configexc.KeybindingError( "Can't find binding '{}' in {} mode".format(key, mode)) self._config.update_mutables(save_yaml=save_yaml)