frosch03.de/posts/2013-02-10-ZSH-Compeltion
giving frogblog zsh completion
I always was annoyed, that working with the frogblog was a pain. One
has to type the correct subject name into the command line. Therefore
you must first get a list of all the subjects by frogblog list.
I wondered always, how to enable completion inside my console. Because i use zsh as my shell, i show here how to enable completion for frogblog in zsh.
Completion Folders
The folder where zsh looks for completions are listed in the $fpath
variable. You can add the completion file in one of the existing
folders. I created ~/.zsh/complete and added the path to the $fpath.
Here are the additional lines for .zshrc:
{% highlight bash %} fpath=(~/.zsh/complete $fpath) {% endhighlight %}
Also make sure that the compinit is autoloaded (what ever that means
;)) and that the right completion menu is known.
{% highlight bash %} autoload -U compinit compinit
zstyle ':completion:*' menu select=2
{% endhighlight %}
Completion file for frogblog
Completion files are named with a starting '\_'. Here is the content of
~/.zsh/complete/_frogblog
{% highlight bash %} #compdef frogblog
_frogblog() {
    local curcontext="\$curcontext" state line
    typeset -A opt_args
    _arguments \
        '1: :->action'\
        '*: :->subject'
    case \$state in
    action)
        _arguments '1:actions:(list publish delete)'
    ;;
    *)
        case \$words[2] in
        list)
        ;;
        publish)
            compadd "\$@" `ls *.hlog`
        ;;
        delete)
            compadd "\$@" `frogblog list | xargs`
        ;;
        *)
            _files
        esac
    esac
}
_frogblog "$@"
{% endhighlight %}
The _arguments need to be first an action, which can be of the list
(list publish delete). For every one of these actions there are
different argument to be submitted.
- list- is the one that has no more arguments
- publish- offers all files in the current directory end with- .hlog
- delete- offers the list that- frogblog listwould return
If you want to know more how this mechanism actually works, have a look at the page i found the most useful information about zsh completion: ZSH – Writing own completion functions.