#compdef timeout

autoload -U is-at-least

_timeout() {
    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[@]}" : \
'-k+[timeout-help-kill-after]: :_default' \
'--kill-after=[timeout-help-kill-after]: :_default' \
'-s+[timeout-help-signal]:SIGNAL:_default' \
'--signal=[timeout-help-signal]:SIGNAL:_default' \
'-f[timeout-help-foreground]' \
'--foreground[timeout-help-foreground]' \
'-p[timeout-help-preserve-status]' \
'--preserve-status[timeout-help-preserve-status]' \
'-v[timeout-help-verbose]' \
'--verbose[timeout-help-verbose]' \
'-h[Print help]' \
'--help[Print help]' \
'-V[Print version]' \
'--version[Print version]' \
':duration:_default' \
':command:_command_names -e' \
&& ret=0
}

(( $+functions[_timeout_commands] )) ||
_timeout_commands() {
    local commands; commands=()
    _describe -t commands 'timeout commands' commands "$@"
}

if [ "$funcstack[1]" = "_timeout" ]; then
    _timeout "$@"
else
    compdef _timeout timeout
fi
