Add a :opt:kitten_alias option that can be used to alias kitten invocation for brevity and to change kitten option defaults globally

See #1879
This commit is contained in:
Kovid Goyal 2019-08-02 10:40:11 +05:30
parent bdf7d98a36
commit d0ecdfb330
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 37 additions and 1 deletions

View File

@ -13,6 +13,9 @@ To update |kitty|, :doc:`follow the instructions <binary>`.
- hints kitten: Allow specifying :option:`kitty +kitten hints --program` - hints kitten: Allow specifying :option:`kitty +kitten hints --program`
multiple times to run multiple programs (:iss:`1879`) multiple times to run multiple programs (:iss:`1879`)
- Add a :opt:`kitten_alias` option that can be used to alias kitten invocation
for brevity and to change kitten option defaults globally (:iss:`1879`)
- Dont fail to start if running the shell to read the EDITOR env var fails - Dont fail to start if running the shell to read the EDITOR env var fails
(:iss:`1869`) (:iss:`1869`)

View File

@ -263,6 +263,21 @@ class KeyDefinition:
self.trigger = defines.resolve_key_mods(kitty_mod, self.trigger[0]), self.trigger[1], self.trigger[2] self.trigger = defines.resolve_key_mods(kitty_mod, self.trigger[0]), self.trigger[1], self.trigger[2]
self.rest = tuple((defines.resolve_key_mods(kitty_mod, mods), is_native, key) for mods, is_native, key in self.rest) self.rest = tuple((defines.resolve_key_mods(kitty_mod, mods), is_native, key) for mods, is_native, key in self.rest)
def resolve_kitten_aliases(self, aliases):
if not self.action.args:
return
kitten = self.action.args[0]
rest = self.action.args[1] if len(self.action.args) > 1 else ''
changed = False
for key, expanded in aliases.items():
if key == kitten:
changed = True
kitten = expanded[0]
if len(expanded) > 1:
rest = expanded[1] + ' ' + rest
if changed:
self.action = self.action._replace(args=[kitten + (' ' + rest).rstrip()])
def parse_key(val, key_definitions): def parse_key(val, key_definitions):
parts = val.split(maxsplit=1) parts = val.split(maxsplit=1)
@ -385,6 +400,13 @@ def handle_symbol_map(key, val, ans):
ans['symbol_map'].update(parse_symbol_map(val)) ans['symbol_map'].update(parse_symbol_map(val))
@special_handler
def handle_kitten_alias(key, val, ans):
parts = val.split(maxsplit=2)
if len(parts) >= 2:
ans['kitten_aliases'][parts[0]] = parts[1:]
@special_handler @special_handler
def handle_send_text(key, val, ans): def handle_send_text(key, val, ans):
# For legacy compatibility # For legacy compatibility
@ -444,7 +466,7 @@ def option_names_for_completion():
def parse_config(lines, check_keys=True, accumulate_bad_lines=None): def parse_config(lines, check_keys=True, accumulate_bad_lines=None):
ans = {'symbol_map': {}, 'keymap': {}, 'sequence_map': {}, 'key_definitions': [], 'env': {}} ans = {'symbol_map': {}, 'keymap': {}, 'sequence_map': {}, 'key_definitions': [], 'env': {}, 'kitten_aliases': {}}
parse_config_base( parse_config_base(
lines, lines,
defaults, defaults,
@ -594,8 +616,11 @@ def finalize_keys(opts):
defns = [] defns = []
else: else:
defns.append(d) defns.append(d)
kitten_aliases = opts.kitten_aliases
for d in defns: for d in defns:
d.resolve(opts.kitty_mod) d.resolve(opts.kitty_mod)
if kitten_aliases and d.action.func == 'kitten':
d.resolve_kitten_aliases(kitten_aliases)
keymap = {} keymap = {}
sequence_map = {} sequence_map = {}

View File

@ -992,6 +992,14 @@ o('clear_all_shortcuts', False, long_text=_('''
You can have kitty remove all shortcut definition seen up to this point. Useful, for You can have kitty remove all shortcut definition seen up to this point. Useful, for
instance, to remove the default shortcuts.''')) instance, to remove the default shortcuts.'''))
o('kitten_alias', 'hints hints --hints-offset=0', add_to_default=False, long_text=_('''
You can create aliases for kitten names, this allows overriding the defaults
for kitten options and can also be used to shorten repeated mappings of the same
kitten with a specific group of options. For example, the above alias
changes the default value of :option:`kitty +kitten hints --hints-offset`
to zero for all mappings, including the builtin ones.
'''))
g('shortcuts.clipboard') # {{{ g('shortcuts.clipboard') # {{{
k('copy_to_clipboard', 'kitty_mod+c', 'copy_to_clipboard', _('Copy to clipboard'), long_text=_(''' k('copy_to_clipboard', 'kitty_mod+c', 'copy_to_clipboard', _('Copy to clipboard'), long_text=_('''
There is also a :code:`copy_or_interrupt` action that can be optionally mapped to :kbd:`Ctrl+c`. There is also a :code:`copy_or_interrupt` action that can be optionally mapped to :kbd:`Ctrl+c`.