#compdef fmt

autoload -U is-at-least

_fmt() {
    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[@]}" : \
'-p+[Reformat only lines beginning with PREFIX, reattaching PREFIX to reformatted lines. Unless -x is specified, leading whitespace will be ignored when matching PREFIX.]:PREFIX:_default' \
'--prefix=[Reformat only lines beginning with PREFIX, reattaching PREFIX to reformatted lines. Unless -x is specified, leading whitespace will be ignored when matching PREFIX.]:PREFIX:_default' \
'-P+[Do not reformat lines beginning with PSKIP. Unless -X is specified, leading whitespace will be ignored when matching PSKIP]:PSKIP:_default' \
'--skip-prefix=[Do not reformat lines beginning with PSKIP. Unless -X is specified, leading whitespace will be ignored when matching PSKIP]:PSKIP:_default' \
'-w+[Fill output lines up to a maximum of WIDTH columns, default 75. This can be specified as a negative number in the first argument.]:WIDTH:_default' \
'--width=[Fill output lines up to a maximum of WIDTH columns, default 75. This can be specified as a negative number in the first argument.]:WIDTH:_default' \
'-g+[Goal width, default of 93% of WIDTH. Must be less than or equal to WIDTH.]:GOAL:_default' \
'--goal=[Goal width, default of 93% of WIDTH. Must be less than or equal to WIDTH.]:GOAL:_default' \
'-T+[Treat tabs as TABWIDTH spaces for determining line length, default 8. Note that this is used only for calculating line lengths; tabs are preserved in the output.]:TABWIDTH:_default' \
'--tab-width=[Treat tabs as TABWIDTH spaces for determining line length, default 8. Note that this is used only for calculating line lengths; tabs are preserved in the output.]:TABWIDTH:_default' \
'-c[First and second line of paragraph may have different indentations, in which case the first line'\''s indentation is preserved, and each subsequent line'\''s indentation matches the second line.]' \
'--crown-margin[First and second line of paragraph may have different indentations, in which case the first line'\''s indentation is preserved, and each subsequent line'\''s indentation matches the second line.]' \
'-t[Like -c, except that the first and second line of a paragraph *must* have different indentation or they are treated as separate paragraphs.]' \
'--tagged-paragraph[Like -c, except that the first and second line of a paragraph *must* have different indentation or they are treated as separate paragraphs.]' \
'-m[Attempt to detect and preserve mail headers in the input. Be careful when combining this flag with -p.]' \
'--preserve-headers[Attempt to detect and preserve mail headers in the input. Be careful when combining this flag with -p.]' \
'-s[Split lines only, do not reflow.]' \
'--split-only[Split lines only, do not reflow.]' \
'-u[Insert exactly one space between words, and two between sentences. Sentence breaks in the input are detected as \[?!.\] followed by two spaces or a newline; other punctuation is not interpreted as a sentence break.]' \
'--uniform-spacing[Insert exactly one space between words, and two between sentences. Sentence breaks in the input are detected as \[?!.\] followed by two spaces or a newline; other punctuation is not interpreted as a sentence break.]' \
'-x[PREFIX must match at the beginning of the line with no preceding whitespace.]' \
'--exact-prefix[PREFIX must match at the beginning of the line with no preceding whitespace.]' \
'-X[PSKIP must match at the beginning of the line with no preceding whitespace.]' \
'--exact-skip-prefix[PSKIP must match at the beginning of the line with no preceding whitespace.]' \
'-q[Break lines more quickly at the expense of a potentially more ragged appearance.]' \
'--quick[Break lines more quickly at the expense of a potentially more ragged appearance.]' \
'-h[Print help]' \
'--help[Print help]' \
'-V[Print version]' \
'--version[Print version]' \
'::files:_files' \
&& ret=0
}

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

if [ "$funcstack[1]" = "_fmt" ]; then
    _fmt "$@"
else
    compdef _fmt fmt
fi
