def to_py(self, value): self._basic_py_validation(value, str) if not value: return None if utils.is_special_key(value): value = '<{}>'.format(utils.normalize_keystr(value[1:-1])) return value
def read_config(self, modename=None): """Read the configuration. Config format: key = command, e.g.: <Ctrl+Q> = quit Args: modename: Name of the mode to use. """ if modename is None: if self._modename is None: raise ValueError("read_config called with no mode given, but " "None defined so far!") modename = self._modename else: self._modename = modename self.bindings = {} self.special_bindings = {} keyconfparser = objreg.get('key-config') for (key, cmd) in keyconfparser.get_bindings_for(modename).items(): if not cmd: continue elif key.startswith('<') and key.endswith('>'): keystr = utils.normalize_keystr(key[1:-1]) self.special_bindings[keystr] = cmd elif self._supports_chains: self.bindings[key] = cmd elif self._warn_on_keychains: log.keyboard.warning( "Ignoring keychain '{}' in mode '{}' because " "keychains are not supported there.".format(key, modename))
def bind(self, key, command=None, *, mode='normal'): """Bind a key to a command. Args: key: The keychain or special key (inside `<...>`) to bind. command: The command to execute, with optional args, or None to print the current binding. mode: A comma-separated list of modes to bind the key in (default: `normal`). See `:help bindings.commands` for the available modes. """ if command is None: if utils.is_special_key(key): # self._keyconfig.get_command does this, but we also need it # normalized for the output below key = utils.normalize_keystr(key) cmd = self._keyconfig.get_command(key, mode) if cmd is None: message.info("{} is unbound in {} mode".format(key, mode)) else: message.info("{} is bound to '{}' in {} mode".format( key, cmd, mode)) return try: self._keyconfig.bind(key, command, mode=mode, save_yaml=True) except configexc.KeybindingError as e: raise cmdexc.CommandError("bind: {}".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 _parse_key_command(self, modename, key, cmd): """Parse the keys and their command and store them in the object.""" if utils.is_special_key(key): keystr = utils.normalize_keystr(key[1:-1]) self.special_bindings[keystr] = cmd elif self._supports_chains: self.bindings[key] = cmd elif self._warn_on_keychains: log.keyboard.warning("Ignoring keychain '{}' in mode '{}' because " "keychains are not supported there." .format(key, modename))
def _parse_key_command(self, modename, key, cmd): """Parse the keys and their command and store them in the object.""" if utils.is_special_key(key): keystr = utils.normalize_keystr(key[1:-1]) self.special_bindings[keystr] = cmd elif self._supports_chains: self.bindings[key] = cmd elif self._warn_on_keychains: log.keyboard.warning("Ignoring keychain '{}' in mode '{}' because " "keychains are not supported there.".format( key, modename))
def bind(self, win_id, key=None, command=None, *, mode='normal', default=False): """Bind a key to a command. If no command is given, show the current binding for the given key. Using :bind without any arguments opens a page showing all keybindings. Args: key: The keychain or special key (inside `<...>`) to bind. command: The command to execute, with optional args. mode: A comma-separated list of modes to bind the key in (default: `normal`). See `:help bindings.commands` for the available modes. default: If given, restore a default binding. """ if key is None: tabbed_browser = objreg.get('tabbed-browser', scope='window', window=win_id) tabbed_browser.openurl(QUrl('qute://bindings'), newtab=True) return if command is None: if default: # :bind --default: Restore default with self._handle_config_error(): self._keyconfig.bind_default(key, mode=mode, save_yaml=True) return # No --default -> print binding if utils.is_special_key(key): # self._keyconfig.get_command does this, but we also need it # normalized for the output below key = utils.normalize_keystr(key) with self._handle_config_error(): cmd = self._keyconfig.get_command(key, mode) if cmd is None: message.info("{} is unbound in {} mode".format(key, mode)) else: message.info("{} is bound to '{}' in {} mode".format( key, cmd, mode)) return with self._handle_config_error(): self._keyconfig.bind(key, command, mode=mode, save_yaml=True)
def test_normalize(self): """Test normalize with some strings.""" strings = ( ('Control+x', 'ctrl+x'), ('Windows+x', 'meta+x'), ('Mod1+x', 'alt+x'), ('Mod4+x', 'meta+x'), ('Control--', 'ctrl+-'), ('Windows++', 'meta++'), ) for orig, repl in strings: with self.subTest(orig=orig): self.assertEqual(utils.normalize_keystr(orig), repl)
def bind(self, key, command=None, *, mode='normal', default=False): """Bind a key to a command. Args: key: The keychain or special key (inside `<...>`) to bind. command: The command to execute, with optional args, or None to print the current binding. mode: A comma-separated list of modes to bind the key in (default: `normal`). See `:help bindings.commands` for the available modes. default: If given, restore a default binding. """ if command is None: if default: # :bind --default: Restore default with self._handle_config_error(): self._keyconfig.bind_default(key, mode=mode, save_yaml=True) return # No --default -> print binding if utils.is_special_key(key): # self._keyconfig.get_command does this, but we also need it # normalized for the output below key = utils.normalize_keystr(key) with self._handle_config_error(): cmd = self._keyconfig.get_command(key, mode) if cmd is None: message.info("{} is unbound in {} mode".format(key, mode)) else: message.info("{} is bound to '{}' in {} mode".format( key, cmd, mode)) return with self._handle_config_error(): self._keyconfig.bind(key, command, mode=mode, save_yaml=True)
def test_normalize(self, orig, repl): """Test normalize with some strings.""" assert utils.normalize_keystr(orig) == repl