#compdef settle

autoload -U is-at-least

_settle() {
    typeset -A opt_args
    typeset -a _arguments_options
    local ret=1

    if is-at-least 5.2; then
        _arguments_options=(-s -S -C)
    else
        _arguments_options=(-s -C)
    fi

    local context curcontext="$curcontext" state line
    _arguments "${_arguments_options[@]}" : \
'-h[Print help]' \
'--help[Print help]' \
'-V[Print version]' \
'--version[Print version]' \
":: :_settle_commands" \
"*::: :->settle" \
&& ret=0
    case $state in
    (settle)
        words=($line[1] "${words[@]}")
        (( CURRENT += 1 ))
        curcontext="${curcontext%:*:*}:settle-command-$line[1]:"
        case $line[1] in
            (sync)
_arguments "${_arguments_options[@]}" : \
'(-u --update -n --rename -g --generate)-p+[helper option to --create and --move; specify working project]: :_default' \
'(-u --update -n --rename -g --generate)--project=[helper option to --create and --move; specify working project]: :_default' \
'(-u --update -m --move -n --rename -g --generate)-c+[create a new Zettel]:TITLE:_default' \
'(-u --update -m --move -n --rename -g --generate)--create=[create a new Zettel]:TITLE:_default' \
'(-m --move -n --rename -g --generate)-u+[update a note'\''s metadata, given its path]:PATH:_default' \
'(-m --move -n --rename -g --generate)--update=[update a note'\''s metadata, given its path]:PATH:_default' \
'(-n --rename -g --generate)-m+[move the matching Zettel to a project; requires --project]:REGEX:_default' \
'(-n --rename -g --generate)--move=[move the matching Zettel to a project; requires --project]:REGEX:_default' \
'(-g --generate)-n+[rename a note, preserving project and updating backlinks]:TITLE:_default:TITLE:_default' \
'(-g --generate)--rename=[rename a note, preserving project and updating backlinks]:TITLE:_default:TITLE:_default' \
'-g[(re)generate the database]' \
'--generate[(re)generate the database]' \
'-h[Print help]' \
'--help[Print help]' \
&& ret=0
;;
(query)
_arguments "${_arguments_options[@]}" : \
'-t+[keep Zettel with a matching title]:REGEX:_default' \
'--title=[keep Zettel with a matching title]:REGEX:_default' \
'-p+[keep Zettel that are in the matching projects]:REGEX:_default' \
'--project=[keep Zettel that are in the matching projects]:REGEX:_default' \
'-g+[keep Zettel that have a matching tag name]:REGEX:_default' \
'--tag=[keep Zettel that have a matching tag name]:REGEX:_default' \
'-x+[keep Zettel that contain some text]:REGEX:_default' \
'--text=[keep Zettel that contain some text]:REGEX:_default' \
'-l+[keep Zettel that have links to the matching Zettel]:REGEX:_default' \
'--links=[keep Zettel that have links to the matching Zettel]:REGEX:_default' \
'-b+[keep Zettel that have links from the matching Zettel]:REGEX:_default' \
'--backlinks=[keep Zettel that have links from the matching Zettel]:REGEX:_default' \
'-f+[print formatted]: :_default' \
'--format=[print formatted]: :_default' \
'-s+[specify separator for links and backlinks in formatted output]:SEPARATOR:_default' \
'--link_sep=[specify separator for links and backlinks in formatted output]:SEPARATOR:_default' \
'(-f --format -s --link_sep)--graph=[turn the query results into a graph\: '\''dot'\'', '\''json'\'' or '\''vizk'\'']: :_default' \
'-o[keep Zettel that don'\''t have any links to and fro]' \
'--loners[keep Zettel that don'\''t have any links to and fro]' \
'-e[match everything exactly, disabling regex]' \
'--exact[match everything exactly, disabling regex]' \
'-h[Print help]' \
'--help[Print help]' \
&& ret=0
;;
(ls)
_arguments "${_arguments_options[@]}" : \
'-h[Print help]' \
'--help[Print help]' \
':OBJECT -- object to list (tags, projects, ghosts, path):_default' \
&& ret=0
;;
(compl)
_arguments "${_arguments_options[@]}" : \
'-h[Print help]' \
'--help[Print help]' \
':SHELL:_default' \
&& ret=0
;;
(help)
_arguments "${_arguments_options[@]}" : \
":: :_settle__help_commands" \
"*::: :->help" \
&& ret=0

    case $state in
    (help)
        words=($line[1] "${words[@]}")
        (( CURRENT += 1 ))
        curcontext="${curcontext%:*:*}:settle-help-command-$line[1]:"
        case $line[1] in
            (sync)
_arguments "${_arguments_options[@]}" : \
&& ret=0
;;
(query)
_arguments "${_arguments_options[@]}" : \
&& ret=0
;;
(ls)
_arguments "${_arguments_options[@]}" : \
&& ret=0
;;
(compl)
_arguments "${_arguments_options[@]}" : \
&& ret=0
;;
(help)
_arguments "${_arguments_options[@]}" : \
&& ret=0
;;
        esac
    ;;
esac
;;
        esac
    ;;
esac
}

(( $+functions[_settle_commands] )) ||
_settle_commands() {
    local commands; commands=(
'sync:sync the database' \
'query:query the database' \
'ls:list things not related to notes' \
'compl:generate completion file for a given shell' \
'help:Print this message or the help of the given subcommand(s)' \
    )
    _describe -t commands 'settle commands' commands "$@"
}
(( $+functions[_settle__compl_commands] )) ||
_settle__compl_commands() {
    local commands; commands=()
    _describe -t commands 'settle compl commands' commands "$@"
}
(( $+functions[_settle__help_commands] )) ||
_settle__help_commands() {
    local commands; commands=(
'sync:sync the database' \
'query:query the database' \
'ls:list things not related to notes' \
'compl:generate completion file for a given shell' \
'help:Print this message or the help of the given subcommand(s)' \
    )
    _describe -t commands 'settle help commands' commands "$@"
}
(( $+functions[_settle__help__compl_commands] )) ||
_settle__help__compl_commands() {
    local commands; commands=()
    _describe -t commands 'settle help compl commands' commands "$@"
}
(( $+functions[_settle__help__help_commands] )) ||
_settle__help__help_commands() {
    local commands; commands=()
    _describe -t commands 'settle help help commands' commands "$@"
}
(( $+functions[_settle__help__ls_commands] )) ||
_settle__help__ls_commands() {
    local commands; commands=()
    _describe -t commands 'settle help ls commands' commands "$@"
}
(( $+functions[_settle__help__query_commands] )) ||
_settle__help__query_commands() {
    local commands; commands=()
    _describe -t commands 'settle help query commands' commands "$@"
}
(( $+functions[_settle__help__sync_commands] )) ||
_settle__help__sync_commands() {
    local commands; commands=()
    _describe -t commands 'settle help sync commands' commands "$@"
}
(( $+functions[_settle__ls_commands] )) ||
_settle__ls_commands() {
    local commands; commands=()
    _describe -t commands 'settle ls commands' commands "$@"
}
(( $+functions[_settle__query_commands] )) ||
_settle__query_commands() {
    local commands; commands=()
    _describe -t commands 'settle query commands' commands "$@"
}
(( $+functions[_settle__sync_commands] )) ||
_settle__sync_commands() {
    local commands; commands=()
    _describe -t commands 'settle sync commands' commands "$@"
}

if [ "$funcstack[1]" = "_settle" ]; then
    _settle "$@"
else
    compdef _settle settle
fi
