#compdef sd

autoload -U is-at-least

_sd() {
    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[@]}" \
'-n+[Limit the number of replacements that can occur per file. 0 indicates unlimited replacements]:LIMIT: ' \
'--max-replacements=[Limit the number of replacements that can occur per file. 0 indicates unlimited replacements]:LIMIT: ' \
'-f+[Regex flags. May be combined (like \`-f mc\`).]:FLAGS: ' \
'--flags=[Regex flags. May be combined (like \`-f mc\`).]:FLAGS: ' \
'-p[Display changes in a human reviewable format (the specifics of the format are likely to change in the future)]' \
'--preview[Display changes in a human reviewable format (the specifics of the format are likely to change in the future)]' \
'-F[Treat FIND and REPLACE_WITH args as literal strings]' \
'--fixed-strings[Treat FIND and REPLACE_WITH args as literal strings]' \
'-h[Print help (see more with '\''--help'\'')]' \
'--help[Print help (see more with '\''--help'\'')]' \
'-V[Print version]' \
'--version[Print version]' \
':find -- The regexp or string (if using `-F`) to search for:' \
':replace_with -- What to replace each match with. Unless in string mode, you may use captured values like $1, $2, etc:' \
'*::files -- The path to file(s). This is optional - sd can also read from STDIN:_files' \
&& ret=0
}

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

if [ "$funcstack[1]" = "_sd" ]; then
    _sd "$@"
else
    compdef _sd sd
fi
