1#compdef patch
2
3if (( ! $+_patch_args )); then
4  local help="$(LC_ALL=C patch --help 2>&1)"
5  local -A optionmap
6  local arg
7  local comp_p='compadd "$expl[@]" -S0 -r "1-9" - ""'
8
9  [[ "$help" = *--[^h]* || "$help" = *\[-c\|-e\|-n\]* ]] && arg=+
10
11  optionmap=(
12    '*\[-p\[strip-count\]\]*' '(--strip)-p-[number of path prefix components to strip]:number of path prefix components to strip:'"{$comp_p}"
13    '*( -p NUM[, ]|\[-p num\])*' '(--strip)-p+[number of path prefix components to strip]:number of path prefix components to strip:'"{$comp_p}"
14    '*\[--strip\[=strip-count\]\]*' '(-p)--strip=-[number of path prefix components to strip]:number of path prefix components to strip:'"{$comp_p}"
15    '* --strip=NUM[, ]*' '(-p)--strip=[number of path prefix components to strip]:number of path prefix components to strip:'"{$comp_p}"
16
17    '*\[-Fmax-fuzz\]*' '(--fuzz)-F-[specify the maximum fuzz factor]:maximum fuzz factor:'
18    '*(\[-F max-fuzz\]| -F LINES[, ])*' '(--fuzz)-F+[specify the maximum fuzz factor]:maximum fuzz factor:'
19    '*(\[--fuzz=max-fuzz\]| --fuzz( |=)LINES[, ])*' '(-F)--fuzz=[specify the maximum fuzz factor]:maximum fuzz factor:'
20
21    '*(\[-[a-zA-Z]#l[a-zA-Z]#\]| -l[, ])*' '(--ignore-whitespace)-l[ignore white space changes]'
22    '*(\[--ignore-whitespace\]| --ignore-whitespace[, ])*' '(-l)--ignore-whitespace[ignore white space changes]'
23
24    '*(\[-[a-zA-Z]#c[a-zA-Z]#\]| -c[, ]|\[-c\|)*' '(   -e -n -u --context --ed --normal --unified)-c[interpret context diff]'
25    '*(\[-[a-zA-Z]#e[a-zA-Z]#\]| -e[, ]|\|-e\|)*' '(-c    -n -u --context --ed --normal --unified)-e[interpret ed script]'
26    '*(\[-[a-zA-Z]#n[a-zA-Z]#\]| -n[, ]|\|-n\])*' '(-c -e    -u --context --ed --normal --unified)-n[interpret normal diff]'
27    '*(\[-[a-zA-Z]#u[a-zA-Z]#\]| -u[, ])*'        '(-c -e -n    --context --ed --normal --unified)-u[interpret unified diff]'
28    '*(\[--context\]| --context[, ])*'            '(-c -e -n -u           --ed --normal --unified)--context[interpret context diff]'
29    '*(\[--ed\]| --ed[, ])*'                      '(-c -e -n -u --context      --normal --unified)--ed[interpret ed script]'
30    '*(\[--normal\]| --normal[, ])*'              '(-c -e -n -u --context --ed          --unified)--normal[interpret normal diff]'
31    '*(\[--unified\]| --unified[, ])*'            '(-c -e -n -u --context --ed --normal          )--unified[interpret unified diff]'
32
33    '*(\[-[a-zA-Z]#N[a-zA-Z]#\]| -N[, ])*' '(--forward)-N[ignore reverse patches]'
34    '*(\[--forward\]| --forward[, ])*' '(-N)--forward[ignore reverse patches]'
35    '*(\[-[a-zA-Z]#R[a-zA-Z]#\]| -R[, ])*' '(--reverse)-R[reverse mode]'
36    '*(\[--reverse\]| --reverse[, ])*' '(-R)--reverse[reverse mode]'
37
38    '*(\[-i patchfile\]| -i PATCHFILE[, ])*' '(--input)-i+[patch file]:patch file:_files'
39    '* --input=PATCHFILE[, ]*' '(-i)--input=[patch file]:patch file:_files'
40
41    '*(\[-o out-file\]| -o FILE[, ]|\[-o outfile\])*' "(--output)-o${arg}[output file]:output file:_files"
42    '*(\[--output=out-file\]| --output=FILE[, ])*' '(-o)--output=[output file]:output file:_files'
43    '*(\[-r rej-name\]| -r FILE[, ]|\[-r rejectfile\])*' "(--reject-file)-r${arg}[reject file]:reject file:_files"
44    '*(\[--reject-file=rej-name\]| --reject-file=FILE[, ])*' '(-r)--reject-file=[reject file]:reject file:_files'
45
46    '*(\[-D symbol\]| -D NAME[, ]|\[-D define\])*' '(--ifdef)-D+[cpp symbol]:symbol:'
47    '*(\[--ifdef=symbol\]| --ifdef=NAME[, ])*' '(-D)--ifdef=[cpp symbol]:symbol:'
48
49    '*(\[-[a-zA-Z]#E[a-zA-Z]#\]| -E[, ])*' '(--remove-empty-files)-E[remove empty files]'
50    '*(\[--remove-empty-files\]| --remove-empty-files[, ])*' '(-E)--remove-empty-files[remove empty files]'
51
52    '* -Z[, ]*'         '(   -T --set-utc --set-time)-Z[assume timestamp as UTC]'
53    '* -T[, ]*'         '(-Z    --set-utc --set-time)-T[assume timestamp as local time]'
54    '* --set-utc[, ]*'  '(-Z -T           --set-time)--set-utc[assume timestamp as UTC]'
55    '* --set-time[, ]*' '(-Z -T --set-utc           )--set-time[assume timestamp as local time]'
56
57    '* --quoting-style=WORD[, ]*' '--quoting-style=[quoting style]:quoting style:(literal shell shell-always c escape)'
58
59    '*(\[-[a-zA-Z]#b[a-zA-Z]#\]| -b[, ])*' '(--backup)-b[backup original]'
60    '* --backup[, ]*' '(-b)--backup[backup original]'
61    '* --backup-if-mismatch[, ]*' '(--no-backup-if-mismatch)--backup-if-mismatch[back up if not match exactly]'
62    '* --no-backup-if-mismatch[, ]*' '(--backup-if-mismatch)--no-backup-if-mismatch[back up only if otherwise requested]'
63
64    '*(\[-V {numbered,existing,simple}\]| -V STYLE[, ])*' "(--version-control)-V${arg}[backup method]:backup method:(numbered existing simple)"
65    '*(\[--version-control={numbered,existing,simple}\]| --version-control=STYLE[, ])*' '(-V)--version-control=[backup method]:backup method:(numbered existing simple)'
66
67    '*(\[-B backup-prefix\]| -B PREFIX[, ])*' "(--prefix)-B${arg}[specify backup prefix]:backup prefix:"
68    '*(\[--prefix=backup-prefix\]| --prefix=PREFIX[, ])*' '(-B)--prefix=[specify backup prefix]:backup prefix:'
69    '* -Y PREFIX[, ]*' '(--basename-prefix)-Y+[specify backup basename prefix]:backup basename prefix:'
70    '* --basename-prefix=PREFIX[, ]*' '(-Y)--basename-prefix=[specify backup basename prefix]:backup basename prefix:'
71    '*\[-b backup-ext\]*'                              "(   -z --suffix)-b${arg}[specify backup suffix]:backup suffix:(.bak)"
72    '* -z SUFFIX[, ]*'                                 '(-b    --suffix)-z+[specify backup suffix]:backup suffix:(.bak)'
73    '*(\[--suffix=backup-ext\]| --suffix=SUFFIX[, ])*' '(-b -z         )--suffix=[specify backup suffix]:backup suffix:(.bak)'
74
75    '* -g NUM[, ]*' '(--get)-g+[get from RCS etc.]:number:((1\:get -1\:ask))'
76    '* --get=NUM[, ]*' '(-g)--get=[get from RCS etc.]:number:((1\:get -1\:ask))'
77
78    '* -g  --get *' '(-G --get --no-get)-g[get from RCS or SCCS]
79(-g -G --no-get)--get[get from RCS or SCCS]'
80    '* -G  --no-get *' '(-g --get --no-get)-G[don'\''t get from RCS or SCCS]
81(-g -G --get)--no-get[don'\''t get from RCS or SCCS]'
82
83    '*(\[-[a-zA-Z]#f[a-zA-Z]#\]| -f[, ])*' '(   -t --force --batch)-f[force mode]'
84    '*(\[-[a-zA-Z]#t[a-zA-Z]#\]| -t[, ])*' '(-f    --force --batch)-t[batch mode]'
85    '*(\[--force\]| --force[, ])*'         '(-f -t         --batch)--force[force mode]'
86    '*(\[--batch\]| --batch[, ])*'         '(-f -t --force        )--batch[batch mode]'
87
88    '*(\[-[a-zA-Z]#s[a-zA-Z]#\]| -s[, ])*' '(   --quiet --silent)-s[silent mode]'
89    '*(\[--quiet\]| --quiet[, ])*'         '(-s         --silent)--quiet[silent mode]'
90    '*(\[--silent\]|--silent[, ])*'        '(-s --quiet         )--silent[silent mode]'
91
92    '* --verbose[, ]*' '--verbose[verbose mode]'
93    '* --dry-run[, ]*' '--dry-run[don'\''t actually change files]'
94    '* --posix[, ]*' '--posix[POSIX mode]'
95
96    '*(\[-d directory\]| -d DIR[, ]|\[-d dir\])*' '(--directory)-d+[change the working directory]:chdir to:_files -/'
97    '*(\[--directory=directory\]| --directory=DIR[, ])*' '(-d)--directory=[change the working directory]:chdir to:_files -/'
98
99    '* --binary[, ]*' '--binary[binary mode]'
100
101    '*(\[-[a-zA-Z]#v[a-zA-Z]#\]| -v[, ])*' '(--version)-v[print version]'
102    '*(\[--version\]| --version[, ])*' '(-v)--version[print version]'
103    '* --help[, ]*' '--help[print help message]'
104
105    '*\[-[a-zA-Z]#S[a-zA-Z]#\]*' '*-S[ignore this patch]'
106    '*\[--skip\]*' '*--skip[ignore this patch]'
107
108    '*(\[-[a-zA-Z]#C[a-zA-Z]#\])*' '(--check)-C[check only]'
109    '*\[--check\]*' '(-C)--check[check only]'
110
111    '*\[--index-first\]*' '--index-first[take Index: line precedence]'
112
113    '* --use-index-line *' '--use-index-line[deal with Index: line]'
114    '* --patch-root=DIR *' '--patch-root=[restrict paths]:patch root:_files -/'
115
116    '* -U *' '(--unified-reject-files)-U[create unified reject files]'
117    '* --unified-reject-files *' '(-U)--unified-reject-files[create unified reject files]'
118    '* --global-reject-file=file *' '--global-reject-file=[put all rejects into one file]:file:_files'
119  )
120  _patch_args=(
121    ${arg:+"-s"}
122    ${(fF)optionmap[(K)"$help"]}
123  )
124fi
125
126_arguments "${(@)_patch_args}" ':original file:_files' ':patch file:_files'
127