#compdef khal
# vim: ft=zsh sw=2 ts=2
# install by copying to file where your zsh looks for completion scripts
# e.g. add this to your zshrc:
# fpath=(~/.zsh/completion $fpath) 

_calendars() {
  local accounts
  accounts=( ${${(f)"$(command khal printcalendars 2> /dev/null)"}/[$'\n']##/} )
  compadd "$a" ${(kv)=accounts}
}


local ret=1 state

local -a common_ops
common_ops=(
  {-h,--help}"[show help]"
  {-v,--verbose}"[give more output]"
  {-V,--version}"[show version and exit]"
  {-q,--quiet}"[give less output]"
  {-c,--config=}"[config file]:filename:_files"
)

_directories () {
  _wanted directories expl directory _path_files -/ "$@" -
}

typeset -A opt_args
_arguments \
  ':subcommand:->subcommand' \
  $common_ops \
  '*::options:->options' && ret=0

case $state in
  subcommand)
    local -a subcommands
    subcommands=(
      "calendar:show calendar"
      "agenda:show agenda"
      "interactive:open the interactive calendar"
      "new:add a new event"
      "printcalendars:print all configured calendars"
    )

    _describe -t subcommands 'khal subcommands' subcommands && ret=0
  ;;

  options)
    local -a args
    args=(
      $common_ops
    )

    case $words[1] in
      calendar | agenda | interactive)
        args+=(
          "-a=[use this calendar]:calendars:_calendars"
          "-b=[do not use this calendar]:calendars:_calendars"
        )
      ;;

    esac

    _arguments $args && ret=0
  ;;
esac

return ret

# vim: ft=zsh
