diff --git a/completion/bash/kitty.sh b/completion/bash/kitty.sh deleted file mode 100644 index 47a8594fe..000000000 --- a/completion/bash/kitty.sh +++ /dev/null @@ -1,14 +0,0 @@ -#!/bin/bash - -kitty_completions() { - local src - local limit - # Send all words up to the word the cursor is currently on - let limit=1+$COMP_CWORD - src=$(printf "%s\n" "${COMP_WORDS[@]: 0:$limit}" | kitty +complete bash) - if [[ $? == 0 ]]; then - eval ${src} - fi -} - -complete -F kitty_completions kitty diff --git a/completion/zsh/_kitty b/completion/zsh/_kitty deleted file mode 100644 index 488ecb220..000000000 --- a/completion/zsh/_kitty +++ /dev/null @@ -1,10 +0,0 @@ -#compdef kitty - -_kitty() { - local src - # Send all words upto the word the cursor is currently on - src=$(printf "%s\n" "${(@)words[1,$CURRENT]}" | kitty +complete zsh) - if [[ $? == 0 ]]; then - eval ${src} - fi -} diff --git a/docs/changelog.rst b/docs/changelog.rst index 087ce40d7..3f983fee0 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -14,6 +14,9 @@ Changelog using standard keyboards) via `IBus `_ (:iss:`469`) +- Implement completion for the kitty command in bash and zsh. See + :ref:`completion`. + - Render the text under the cursor in a fixed color, configurable via the option :opt:`cursor_text_color` (:iss:`126`) diff --git a/docs/index.rst b/docs/index.rst index e6069c997..7384f4b66 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -350,6 +350,39 @@ Frequently Asked Questions The list of Frequently Asked Questions (*FAQ*) is :doc:`available here `. +.. _completion: + +Completion for kitty +--------------------------------- + +|kitty| comes with completion for the ``kitty`` command for popular shells. + + +bash +~~~~~~~~ + +Add the following to your :file:`~/.bashrc` + +.. code-block:: sh + + source <(kitty + complete setup bash) + + +zsh +~~~~~~~~~ + +Add the following to your :file:`~/.zshrc` + +.. code-block:: sh + + autoload -Uz compinit + compinit + # Completion for kitty + kitty + complete setup zsh | source /dev/stdin + +The important thing above is to make sure the call to |kitty| to load the zsh +completions happens after the call to :file:`compinit`. + Changelog ------------------ diff --git a/kitty/complete.py b/kitty/complete.py index fd27e010e..ceec92f3e 100644 --- a/kitty/complete.py +++ b/kitty/complete.py @@ -26,7 +26,37 @@ class Completions: self.no_space_groups = set() -# I/O {{{ +# Shell specific code {{{ + + +completion_scripts = { + 'zsh': ''' +_kitty() { + local src + # Send all words upto the word the cursor is currently on + src=$(printf "%s\n" "${(@)words[1,$CURRENT]}" | kitty +complete zsh) + if [[ $? == 0 ]]; then + eval ${src} + fi +} +compdef _kitty kitty +''', + 'bash': ''' +kitty_completions() { + local src + local limit + # Send all words up to the word the cursor is currently on + let limit=1+$COMP_CWORD + src=$(printf "%s\n" "${COMP_WORDS[@]: 0:$limit}" | kitty +complete bash) + if [[ $? == 0 ]]; then + eval ${src} + fi +} + +complete -F kitty_completions kitty +''', +} + def input_parser(func): name = func.__name__.split('_')[0] @@ -209,10 +239,16 @@ def find_completions(words, new_word, entry_points, namespaced_entry_points): return ans +def setup(cstyle): + print(completion_scripts[cstyle]) + + def main(args, entry_points, namespaced_entry_points): if not args: raise SystemExit('Must specify completion style') cstyle = args[0] + if cstyle == 'setup': + return setup(args[1]) data = sys.stdin.read() try: parser = parsers[cstyle]