#compdef ranger

autoload -U is-at-least

_ranger() {
    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[@]}" \
'-e[Enables experimental features.]' \
'--experimental[Enables experimental features.]' \
'-h[Print help]' \
'--help[Print help]' \
'-V[Print version]' \
'--version[Print version]' \
":: :_ranger_commands" \
"*::: :->ranger" \
&& ret=0
    case $state in
    (ranger)
        words=($line[1] "${words[@]}")
        (( CURRENT += 1 ))
        curcontext="${curcontext%:*:*}:ranger-command-$line[1]:"
        case $line[1] in
            (man)
_arguments "${_arguments_options[@]}" \
'-o+[]: : ' \
'--out=[]: : ' \
'-f+[]: :(manpages markdown)' \
'--format=[]: :(manpages markdown)' \
'-h[Print help]' \
'--help[Print help]' \
'-V[Print version]' \
'--version[Print version]' \
&& ret=0
;;
(autocomplete)
_arguments "${_arguments_options[@]}" \
'-o+[]: : ' \
'--out=[]: : ' \
'-s+[]: :(bash zsh fish elvish powershell)' \
'--shell=[]: :(bash zsh fish elvish powershell)' \
'-h[Print help]' \
'--help[Print help]' \
'-V[Print version]' \
'--version[Print version]' \
&& ret=0
;;
(help)
_arguments "${_arguments_options[@]}" \
":: :_ranger__help_commands" \
"*::: :->help" \
&& ret=0

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

(( $+functions[_ranger_commands] )) ||
_ranger_commands() {
    local commands; commands=(
'man:Renders the manual.' \
'autocomplete:Renders shell completion scripts.' \
'help:Print this message or the help of the given subcommand(s)' \
    )
    _describe -t commands 'ranger commands' commands "$@"
}
(( $+functions[_ranger__autocomplete_commands] )) ||
_ranger__autocomplete_commands() {
    local commands; commands=()
    _describe -t commands 'ranger autocomplete commands' commands "$@"
}
(( $+functions[_ranger__help__autocomplete_commands] )) ||
_ranger__help__autocomplete_commands() {
    local commands; commands=()
    _describe -t commands 'ranger help autocomplete commands' commands "$@"
}
(( $+functions[_ranger__help_commands] )) ||
_ranger__help_commands() {
    local commands; commands=(
'man:Renders the manual.' \
'autocomplete:Renders shell completion scripts.' \
'help:Print this message or the help of the given subcommand(s)' \
    )
    _describe -t commands 'ranger help commands' commands "$@"
}
(( $+functions[_ranger__help__help_commands] )) ||
_ranger__help__help_commands() {
    local commands; commands=()
    _describe -t commands 'ranger help help commands' commands "$@"
}
(( $+functions[_ranger__help__man_commands] )) ||
_ranger__help__man_commands() {
    local commands; commands=()
    _describe -t commands 'ranger help man commands' commands "$@"
}
(( $+functions[_ranger__man_commands] )) ||
_ranger__man_commands() {
    local commands; commands=()
    _describe -t commands 'ranger man commands' commands "$@"
}

if [ "$funcstack[1]" = "_ranger" ]; then
    _ranger "$@"
else
    compdef _ranger ranger
fi
