1#
2# This file gives examples of possible programmable completions (compctl).
3# You can either put the compctl commands in your .zshrc file, or put them
4# in a separate file (say .zcompctl) and source it from your .zshrc file.
5#
6# These are just examples. Use and modify to personal taste.  Copying
7# this file without thought will needlessly increase zsh's memory usage
8# and startup time.
9#
10# For a detailed description of how these work, check the zshcompctl man
11# page.
12#
13#------------------------------------------------------------------------------
14hosts=( ${(s: :)${(ps:\t:)${${(f)"$(</etc/hosts)"}%%\#*}##[:blank:]#[^[:blank:]]#}} )
15ports=( "${(@)${(@)${(@f)$(</etc/services)}:#\#*}%%[ 	]*}" )
16
17# groups=( $(cut -d: -f1 /etc/group) )
18# groups=( $(ypcat group.byname | cut -d: -f1) ) # if you use NIS
19
20# It can be done without forking, but it used too much memory in old zsh's:
21groups=( ${${(s: :)$(</etc/group)}%%:*} )
22#groups=( ${${(s: :)$(ypcat groups)}%%:*} ) # if you use NIS
23
24# Completion for zsh builtins.
25compctl -z -P '%' bg
26compctl -j -P '%' fg jobs disown
27compctl -j -P '%' + -s '`ps -x | tail +2 | cut -c1-5`' wait
28compctl -A shift
29compctl -c type whence where which
30compctl -m -x 'W[1,-*d*]' -n - 'W[1,-*a*]' -a - 'W[1,-*f*]' -F -- unhash
31compctl -m -q -S '=' -x 'W[1,-*d*] n[1,=]' -/ - \
32	'W[1,-*d*]' -n -q -S '=' - 'n[1,=]' -/g '*(*)' -- hash
33compctl -F functions unfunction
34compctl -k '(al dc dl do le up al bl cd ce cl cr
35	dc dl do ho is le ma nd nl se so up)' echotc
36compctl -a unalias
37compctl -v getln getopts read unset vared
38compctl -v -S '=' -q declare export integer local readonly typeset
39compctl -eB -x 'p[1] s[-]' -k '(a f m r)' - \
40	'C[1,-*a*]' -ea - 'C[1,-*f*]' -eF - 'C[-1,-*r*]' -ew -- disable
41compctl -dB -x 'p[1] s[-]' -k '(a f m r)' - \
42	'C[1,-*a*]' -da - 'C[1,-*f*]' -dF - 'C[-1,-*r*]' -dw -- enable
43compctl -k "(${(j: :)${(f)$(limit)}%% *})" limit unlimit
44compctl -l '' -x 'p[1]' -f -- . source
45# Redirection below makes zsh silent when completing unsetopt xtrace
46compctl -s '$({ unsetopt kshoptionprint; setopt } 2>/dev/null)' + -o + -x 's[no]' -o -- unsetopt
47compctl -s '$({ unsetopt kshoptionprint; unsetopt } 2>/dev/null)' + -o + -x 's[no]' -o -- setopt
48compctl -s '${^fpath}/*(N:t)' autoload
49compctl -b -x 'W[1,-*[DAN]*],C[-1,-*M]' -s '$(bindkey -l)' -- bindkey
50compctl -c -x 'C[-1,-*k]' -A - 'C[-1,-*K]' -F -- compctl
51compctl -x 'C[-1,-*e]' -c - 'C[-1,-[ARWI]##]' -f -- fc
52compctl -x 'p[1]' - 'p[2,-1]' -l '' -- sched
53compctl -x 'C[-1,[+-]o]' -o - 'c[-1,-A]' -A -- set
54compctl -b -x 'w[1,-N] p[3]' -F -- zle
55compctl -s '${^module_path}/*(N:t:r)' -x \
56	'W[1,-*(a*u|u*a)*],W[1,-*a*]p[3,-1]' -B - \
57	'W[1,-*u*]' -s '$(zmodload)' -- zmodload
58
59# Anything after nohup is a command by itself with its own completion
60compctl -l '' nohup noglob exec nice eval - time rusage
61compctl -l '' -x 'p[1]' -eB -- builtin
62compctl -l '' -x 'p[1]' -em -- command
63compctl -x 'p[1]' -c - 'p[2,-1]' -k signals -- trap
64#------------------------------------------------------------------------------
65# kill takes signal names as the first argument after -, but job names after %
66# or PIDs as a last resort
67compctl -j -P '%' + -s '`ps -x | tail +2 | cut -c1-5`' + \
68	-x 's[-] p[1]' -k "($signals[1,-3])" -- kill
69#------------------------------------------------------------------------------
70compctl -s '$(groups)' + -k groups newgrp
71compctl -f -x 'p[1], p[2] C[-1,-*]' -k groups -- chgrp
72compctl -f -x 'p[1] n[-1,.], p[2] C[-1,-*] n[-1,.]' -k groups - \
73	'p[1], p[2] C[-1,-*]' -u -S '.' -q -- chown
74compctl -/g '*.x' rpcgen
75compctl -u -x 's[+] c[-1,-f],s[-f+]' -W ~/Mail -f - \
76	's[-f],c[-1,-f]' -f -- mail elm
77compctl -x 'c[-1,-f]' -W ~/Mail -f -- pine
78#------------------------------------------------------------------------------
79compctl -s "\$(awk '/^[a-zA-Z0-9][^ 	]+:/ {print \$1}' FS=: [mM]akefile)" -x \
80	'c[-1,-f]' -f -- make gmake pmake
81#------------------------------------------------------------------------------
82# tar
83tarnames () {
84	# Completion function for use with tar:
85	# get the names of files in the tar archive to extract.
86	#
87	# The main claim to fame of this particular function is that it
88	# completes directories in the tar-file in a manner very roughly
89	# consistent with `compctl -f'.  There are two bugs:  first, the whole
90	# path prefix up to the present is listed with ^D, not just the new
91	# part to add; second, after a unique completion a space is always
92	# inserted, even if the completion ends with a slash.  These are
93	# currently limitations of zsh.
94	#
95	# This only works for the (fairly common) tar argument style where
96	# the arguments are bunched into the first argument, and the second
97	# argument is the name of the tarfile.  For example,
98	#  tar xvzf zsh-3.1.2.tar.gz ...
99	# You can only use compressed/gzipped files if tar is GNU tar,
100	# although the correct name for the tar programme will be inferred.
101
102	local line list=tf
103	read -cA line
104	# $line[2] is the first argument:  check for possible compression args.
105	# (This is harmless when used with non-GNU tar, but then the file must
106	# be uncompressed to be able to use it with tar anyway.)
107	[[ $line[2] = *[Zz]* ]] && list=tfz
108	# $line[1] is the command name:  something like tar or gnutar.
109	# $line[3] is the name of the tar archive.
110
111	# cache contents for multiple completion: note tar_cache_name
112	# and tar_cache_list are not local.  Assumes all files with the
113	# same name are the same file, even if in different directories:
114	# you can trick it with $PWD/file on the command line.
115	if [[ $line[3] != $tar_cache_name ]]; then
116	  tar_cache_list=($($line[1] $list $line[3]))
117	  tar_cache_name=$line[3]
118	fi
119
120	# Now prune the list to include only appropriate directories.
121	local file new
122	reply=()
123	if [[ $1 = */* ]]; then
124	  local sofar=${1%/*}/
125	  for file in $tar_cache_list; do
126	    if [[ $file = $sofar* ]]; then
127	      new=${file#$sofar}
128	      if [[ $new = */* ]]; then
129		new=$sofar${new%%/*}/
130	      else
131		new=$file
132	      fi
133	      if [[ $1 != */ || $new != $1 ]]; then
134		reply=($reply $new)
135	      fi
136	    fi
137	  done
138	else
139	  for file in $tar_cache_list; do
140	    if [[ $file = */* ]]; then
141	      new=${file%%/*}/
142	    else
143	      new=$file
144	    fi
145	    reply=($reply $new)
146	  done
147	fi
148}
149
150compctl -f \
151	-x 'p[2] C[-1,*(z*f|f*z)*]' -/g '*.(taz|tar.(gz|z|Z)|tgz)' \
152	- 'p[2] C[-1,*(Z*f|f*Z)*]' -/g '*.(tar.Z|taz)' \
153	- 'p[2] C[-1,*f*]' -/g '*.tar' \
154	- 'p[1] N[-1,ctxvzZ]' -k "(v z f)" \
155	- 'p[1] s[]' -k "(c t x)" -S '' \
156	- 'p[3,-1] W[1,*x*]' -K tarnames \
157	-- tar gtar gnutar
158#------------------------------------------------------------------------------
159# rmdir only real directories
160compctl -/g '*(/)' rmdir dircmp
161#------------------------------------------------------------------------------
162# cd/pushd only directories or symbolic links to directories
163compctl -/ cd chdir dirs pushd
164
165# Another possibility for cd/pushd is to use it in conjunction with the
166# cdmatch function (in the Functions subdirectory of zsh distribution).
167compctl -K cdmatch -S '/' -q -x 'p[2]' -Q -K cdmatch2 - \
168	'S[/][~][./][../]' -g '*(-/)' + -g '*(-/D)' - \
169	'n[-1,/]' -K cdmatch -S '/' -q -- cd chdir pushd
170#------------------------------------------------------------------------------
171# If the command is rsh, make the first argument complete to hosts and treat the
172# rest of the line as a command on its own.
173compctl -k hosts -x 'p[2,-1]' -l '' -- rsh
174
175# rlogin takes hosts and users after `-l'
176compctl -k hosts -x 'c[-1,-l]' -u -- rlogin
177
178# rcp: match users, hosts and files initially.  Match files after a :, or hosts
179# after an @.  If one argument contains a : then the other matches files only.
180# Not quite perfect; compctl just isn't up to it yet.
181compctl -u -k hosts -f -x 'n[1,:]' -f - 'n[1,@]' -k hosts -S ':' - \
182	'p[1] W[2,*:*]' -f - 'p[1] W[2,*?*]' -u -k hosts -S ':' - \
183	'p[2] W[1,*:*]' -f - 'p[2] W[1,*?*]' -u -k hosts -S ':' -- rcp
184
185compctl -k hosts host rup rusers ping
186#------------------------------------------------------------------------------
187# strip, profile, and debug only executables.  The compctls for the
188# debuggers could be better, of course.
189compctl -/g '*(*)' strip gprof adb dbx xdbx ups
190compctl -/g '*.[ao]' -/g '*(*)' nm
191#------------------------------------------------------------------------------
192# shells: compctl needs some more enhancement to do -c properly.
193compctl -f -x 'C[-1,-*c]' -c - 'C[-1,[-+]*o]' -o -- bash ksh sh zsh
194#------------------------------------------------------------------------------
195# su takes a username and args for the shell.
196compctl -u -x 'w[1,-]p[3,-1]' -l sh - 'w[1,-]' -u - 'p[2,-1]' -l sh -- su
197#------------------------------------------------------------------------------
198# Run ghostscript on postscript files, but if no postscript file matches what
199# we already typed, complete directories as the postscript file may not be in
200# the current directory.
201compctl -/g '*.(e|E|)(ps|PS)' \
202	gs ghostview nup psps pstops psmulti psnup psselect
203#------------------------------------------------------------------------------
204# Similar things for tex, texinfo and dvi files.
205compctl -/g '*.tex*' {,la,gla,ams{la,},{g,}sli}tex texi2dvi
206compctl -/g '*.dvi' xdvi dvips
207#------------------------------------------------------------------------------
208# For rcs users, co and rlog from the RCS directory.  We don't want to see
209# the RCS and ,v though.
210compctl -g 'RCS/*(:s@RCS/@@:s/,v//)' co rlog rcs rcsdiff
211#------------------------------------------------------------------------------
212# gzip uncompressed files, but gzip -d only gzipped or compressed files
213compctl -x 'R[-*[dt],^*]' -/g '*.(gz|z|Z|t[agp]z|tarZ|tz)' + -f - \
214    's[]' -/g '^*(.(tz|gz|t[agp]z|tarZ|zip|ZIP|jpg|JPG|gif|GIF|[zZ])|[~#])' \
215    + -f -- gzip
216compctl -/g '*.(gz|z|Z|t[agp]z|tarZ|tz)' gunzip gzcat zcat
217compctl -/g '*.Z' uncompress zmore
218compctl -/g '*.F' melt fcat
219#------------------------------------------------------------------------------
220# ftp takes hostnames
221ftphosts=(prep.ai.mit.edu wuarchive.wustl.edu ftp.uu.net ftp.math.gatech.edu)
222compctl -k ftphosts ftp
223
224# Some systems have directories containing indices of ftp servers.
225# For example: we have the directory /home/ftp/index/INDEX containing
226# files of the form `<name>-INDEX.Z', this leads to:
227#compctl -g '/home/ftp/index/INDEX/*-INDEX.Z(:t:r:s/-INDEX//)' ftp tftp
228#------------------------------------------------------------------------------
229# Change default completion (see the multicomp function in the Function
230# subdirectory of the zsh distribution).
231#compctl -D -f + -U -K multicomp
232# If completion of usernames is slow for you, you may want to add something
233# like
234#    -x 'C[0,*/*]' -f - 's[~]' -S/ -k users + -u
235# where `users' contains the names of the users you want to complete often.
236# If you want to use this and to be able to complete named directories after
237# the `~' you should add `+ -n' at the end
238#------------------------------------------------------------------------------
239# This is to complete all directories under /home, even those that are not
240# yet mounted (if you use the automounter).
241
242# This is for NIS+ (e.g. Solaris 2.x)
243#compctl -Tx 's[/home/] C[0,^/home/*/*]'  -S '/' -s '$(niscat auto_home.org_dir | \
244#	awk '\''/export\/[a-zA-Z]*$/ {print $NF}'\'' FS=/)'
245
246# And this is for YP (e.g. SunOS4.x)
247#compctl -Tx 's[/home/] C[0,^/home/*/*]' -S '/' -s '$(ypcat auto.home | \
248#	awk '\''/export\/[a-zA-Z]*$/ {print $NF}'\'' FS=/)'
249#------------------------------------------------------------------------------
250# Find is very system dependent, this one is for GNU find.
251# Note that 'r[-exec,;]' must come first
252if [[ -r /proc/filesystems ]]; then
253    # Linux
254    filesystems='"${(@)${(@f)$(</proc/filesystems)}#*	}"'
255else
256    filesystems='ufs 4.2 4.3 nfs tmp mfs S51K S52K'
257fi
258compctl -x 'r[-exec,;][-ok,;]' -l '' - \
259's[-]' -s 'daystart {max,min,}depth follow noleaf version xdev
260	{a,c,}newer {a,c,m}{min,time} empty false {fs,x,}type gid inum links
261	{i,}{l,}name {no,}{user,group} path perm regex size true uid used
262	exec {f,}print{f,0,} ok prune ls' - \
263	'p[1]' -g '. .. *(-/)' - \
264	'C[-1,-((a|c|)newer|fprint(|0|f))]' -f - \
265	'c[-1,-fstype]' -s $filesystems - \
266	'c[-1,-group]' -k groups - \
267	'c[-1,-user]' -u -- find
268#------------------------------------------------------------------------------
269# Generic completion for C compiler.
270compctl -/g "*.[cCoa]" -x 's[-I]' -/ - \
271	's[-l]' -s '${(s.:.)^LD_LIBRARY_PATH}/lib*.a(:t:r:s/lib//)' -- cc
272#------------------------------------------------------------------------------
273# GCC completion, by Andrew Main
274# completes to filenames (*.c, *.C, *.o, etc.); to miscellaneous options after
275# a -; to various -f options after -f (and similarly -W, -g and -m); and to a
276# couple of other things at different points.
277# The -l completion is nicked from the cc compctl above.
278# The -m completion should be tailored to each system; the one below is i386.
279compctl -/g '*.([cCmisSoak]|cc|cxx|ii|k[ih])' -x \
280	's[-l]' -s '${(s.:.)^LD_LIBRARY_PATH}/lib*.a(:t:r:s/lib//)' - \
281	'c[-1,-x]' -k '(none c objective-c c-header c++ cpp-output
282	assembler assembler-with-cpp)' - \
283	'c[-1,-o]' -f - \
284	'C[-1,-i(nclude|macros)]' -/g '*.h' - \
285	'C[-1,-i(dirafter|prefix)]' -/ - \
286	's[-B][-I][-L]' -/ - \
287	's[-fno-],s[-f]' -k '(all-virtual cond-mismatch dollars-in-identifiers
288	enum-int-equiv external-templates asm builtin strict-prototype
289	signed-bitfields signd-char this-is-variable unsigned-bitfields
290	unsigned-char writable-strings syntax-only pretend-float caller-saves
291	cse-follow-jumps cse-skip-blocks delayed-branch elide-constructors
292	expensive-optimizations fast-math float-store force-addr force-mem
293	inline-functions keep-inline-functions memoize-lookups default-inline
294	defer-pop function-cse inline peephole omit-frame-pointer
295	rerun-cse-after-loop schedule-insns schedule-insns2 strength-reduce
296	thread-jumps unroll-all-loops unroll-loops)' - \
297	's[-g]' -k '(coff xcoff xcoff+ dwarf dwarf+ stabs stabs+ gdb)' - \
298	's[-mno-][-mno][-m]' -k '(486 soft-float fp-ret-in-387)' - \
299	's[-Wno-][-W]' -k '(all aggregate-return cast-align cast-qual
300	char-subscript comment conversion enum-clash error format id-clash-6
301	implicit inline missing-prototypes missing-declarations nested-externs
302	import parentheses pointer-arith redundant-decls return-type shadow
303	strict-prototypes switch template-debugging traditional trigraphs
304	uninitialized unused write-strings)' - \
305	's[-]' -k '(pipe ansi traditional traditional-cpp trigraphs pedantic
306	pedantic-errors nostartfiles nostdlib static shared symbolic include
307	imacros idirafter iprefix iwithprefix nostdinc nostdinc++ undef)' \
308	-X 'Use "-f", "-g", "-m" or "-W" for more options' -- gcc g++
309#------------------------------------------------------------------------------
310# There are (at least) two ways to complete manual pages.  This one is
311# extremely memory expensive if you have lots of man pages
312man_var() {
313    man_pages=( ${^manpath}/man*/*(N:t:r) )
314    compctl -k man_pages -x 'C[-1,-P]' -m - \
315	    'R[-*l*,;]' -/g '*.(man|[0-9](|[a-z]))' -- man
316    reply=( $man_pages )
317}
318compctl -K man_var -x 'C[-1,-P]' -m - \
319	'R[-*l*,;]' -/g '*.(man|[0-9](|[a-z]))' -- man
320
321# This one isn't that expensive but somewhat slower
322man_glob () {
323   local a
324   read -cA a
325   if [[ $a[2] = -s ]] then         # Or [[ $a[2] = [0-9]* ]] for BSD
326     reply=( ${^manpath}/man$a[3]/$1*$2(N:t:r) )
327   else
328     reply=( ${^manpath}/man*/$1*$2(N:t:r) )
329   fi
330}
331#compctl -K man_glob -x 'C[-1,-P]' -m - \
332#	'R[-*l*,;]' -/g '*.(man|[0-9nlpo](|[a-z]))' -- man
333#------------------------------------------------------------------------------
334# xsetroot: gets possible colours, cursors and bitmaps from wherever.
335# Uses two auxiliary functions.  You might need to change the path names.
336Xcolours() {
337  reply=( ${(L)$(awk '{ if (NF = 4) print $4 }' \
338	< /usr/openwin/lib/X11/rgb.txt)} )
339}
340Xcursor() {
341  reply=( $(sed -n 's/^#define[	 ][ 	]*XC_\([^ 	]*\)[ 	].*$/\1/p' \
342	  < /usr/include/X11/cursorfont.h) )
343}
344compctl -k '(-help -def -display -cursor -cursor_name -bitmap -mod -fg -bg
345	-grey -rv -solid -name)' -x \
346	'c[-1,-display]' -s '$DISPLAY' -k hosts -S ':0' - \
347	'c[-1,-cursor]' -f -  'c[-2,-cursor]' -f - \
348	'c[-1,-bitmap]' -g '/usr/include/X11/bitmaps/*' - \
349	'c[-1,-cursor_name]' -K Xcursor - \
350	'C[-1,-(solid|fg|bg)]' -K Xcolours -- xsetroot
351#------------------------------------------------------------------------------
352# dd
353compctl -k '(if of conv ibs obs bs cbs files skip file seek count)' \
354	-S '=' -x 's[if=], s[of=]' -f - 'C[0,conv=*,*] n[-1,,], s[conv=]' \
355	-k '(ascii ebcdic ibm block unblock lcase ucase swap noerror sync)' \
356	-q -S ',' - 'n[-1,=]' -X '<number>'  -- dd
357#------------------------------------------------------------------------------
358# Various MH completions by Peter Stephenson
359# You may need to edit where it says *Edit Me*.
360
361# The following three functions are best autoloaded.
362# mhcomp completes folders (including subfolders),
363# mhfseq completes sequence names and message numbers,
364# mhfile completes files in standard MH locations.
365
366function mhcomp {
367  # Completion function for MH folders.
368  # Works with both + (rel. to top) and @ (rel. to current).
369  local nword args pref char mhpath
370  read -nc nword
371  read -cA args
372
373  pref=$args[$nword]
374  char=$pref[1]
375  pref=$pref[2,-1]
376
377  # The $(...) here accounts for most of the time spent in this function.
378  if [[ $char = + ]]; then
379  #    mhpath=$(mhpath +)
380  # *Edit Me*: use a hard wired value here: it's faster.
381    mhpath=~/Mail
382  elif [[ $char = @ ]]; then
383    mhpath=$(mhpath)
384  fi
385
386  eval "reply=($mhpath/$pref*(N-/))"
387
388  # I'm frankly amazed that this next step works, but it does.
389  reply=(${reply#$mhpath/})
390}
391
392mhfseq() {
393  # Extract MH message names and numbers for completion.  Use of the
394  # correct folder, if it is not the current one, requires that it
395  # should be the previous command line argument.  If the previous
396  # argument is `-draftmessage', a hard wired draft folder name is used.
397
398  local folder foldpath words pos nums
399  read -cA words
400  read -cn pos
401
402  # Look for a folder name.
403  # First try the previous word.
404  if [[ $words[$pos-1] = [@+]* ]]; then
405    folder=$words[$pos-1]
406  # Next look and see if we're looking for a draftmessage
407  elif [[ $words[$pos-1] = -draftmessage ]]; then
408    # *Edit Me*:  shortcut -- hard-wire draftfolder here
409    # Should really look for a +draftfolder argument.
410    folder=+drafts
411  fi
412  # Else use the current folder ($folder empty)
413
414  if [[ $folder = +* ]]; then
415  # *Edit Me*:  use hard-wired path with + for speed.
416    foldpath=~/Mail/$folder[2,-1]
417  else
418    foldpath=$(mhpath $folder)
419  fi
420
421  # Extract all existing message numbers from the folder.
422  nums=($foldpath/<->(N:t))
423  # If that worked, look for marked sequences.
424  # *Edit Me*: if you never use non-standard sequences, comment out
425  # or delete the next three lines.
426  if (( $#nums )); then
427    nums=($nums $(mark $folder | awk -F: '{print $1}'))
428  fi
429
430  # *Edit Me*:  `unseen' is the value of Unseen-Sequence, if it exists;
431  set -A reply next cur prev first last all unseen $nums
432
433}
434
435mhfile () {
436  # Find an MH file; for use with -form arguments and the like.
437  # Use with compctl -K mhfile.
438
439  local mhfpath file
440  # *Edit Me*:  Array containing all the places MH will look for templates etc.
441  mhfpath=(~/Mail /usr/local/lib/MH)
442
443  # Emulate completeinword behaviour as appropriate
444  local wordstr
445  if [[ -o completeinword ]]; then
446    wordstr='$1*$2'
447  else
448    wordstr='$1$2*'
449  fi
450
451  if [[ $1$2 = */* ]]; then
452    # path given: don't search MH locations
453    eval "reply=($wordstr(.N))"
454  else
455    # no path:  only search MH locations.
456    eval "reply=(\$mhfpath/$wordstr(.N:t))"
457  fi
458}
459
460# Note: you must type the initial + or @ of a folder name to get
461# completion, even in places where only folder names are allowed.
462# Abbreviations for options are not recognised.  Hit tab to complete
463# the option name first.
464compctl -K mhfseq -x 's[+][@]' -K mhcomp -S / -q - \
465  's[-]' -k "(all fast nofast header noheader help list nolist \
466  pack nopack pop push recurse norecurse total nototal)" -- folder
467compctl -K mhfseq -x 's[+][@],c[-1,-draftfolder] s[+][@]' \
468  -K mhcomp -S / -q - 'c[-1,-draftmessage]' -K mhfseq - \
469  'C[-1,-(editor|whatnowproc)]' -c - \
470  's[-]' -k "(draftfolder draftmessage nodraftfolder editor noedit \
471  file form use nouse whatnowproc nowhatnowproc help)" - \
472  'c[-1,-form]' -K mhfile -- comp
473compctl -K mhfseq -x 's[+][@]' -K mhcomp -S / -q - \
474  's[-]' -k "(audit noaudit changecur nochangecur form format \
475  file silent nosilent truncate notruncate width help)" - \
476  'C[-1,-(audit|form)]' -K mhfile - 'c[-1,-file]' -f + -- inc
477compctl -K mhfseq -x 's[+][@]' -K mhcomp -S / -q - \
478  's[-]' -k "(sequence add delete list public nopublic zero nozero help)" -- \
479  mark
480compctl -K mhfseq -x 's[+][@]' \
481  -K mhcomp -S / -q - 'c[-1,-file]' -f - 'c[-1,-rmmprov]' -c - \
482  's[-]' -k "(draft link nolink preserve nopreserve src file \
483  rmmproc normmproc help)" -- refile
484compctl -K mhfseq -x 's[+][@]' \
485  -K mhcomp -S / -q - 'c[-1,-draftmessage]'  -K mhfseq -\
486  's[-]' -k "(annotate noannotate cc nocc draftfolder nodraftfolder \
487  draftmessage editor noedit fcc filter form inplace noinplace query \
488  noquery width whatnowproc nowhatnowproc help)" - 'c[-1,(cc|nocc)]' \
489  -k "(all to cc me)" - 'C[-1,-(filter|form)]' -K mhfile - \
490  'C[-1,-(editor|whatnowproc)]' -c -- repl
491compctl -K mhfseq -x 's[+][@]' -K mhcomp -S / -q - \
492  's[-]' -k "(clear noclear form format header noheader reverse noreverse \
493  file help width)" - 'c[-1,-file]' -f - 'c[-1,-form]' -K mhfile -- scan
494compctl -K mhfseq -x 's[+][@]'  -K mhcomp -S / -q - \
495  's[-]' -k "(draft form moreproc nomoreproc header noheader \
496   showproc noshowproc length width help)" - 'C[-1,-(show|more)proc]' -c - \
497   'c[-1,-file]' -f - 'c[-1,-form]' -K mhfile - \
498   'c[-1,-length]' -s '$LINES' - 'c[-1,-width]' -s '$COLUMNS' -- show next prev
499compctl -K mhfseq -x 's[+][@]' -K mhcomp -S / -q - 's[-]' \
500  -k "(help)" -- rmm
501compctl -K mhfseq -x 's[+][@]' -K mhcomp -S / -q - \
502  's[-]' -k "(after before cc date datefield from help list nolist \
503  public nopublic search sequence subject to zero nozero not or and \
504  lbrace rbrace)" -- pick
505compctl -K mhfseq -x 's[+][@]' -K mhcomp -S / -q - 's[-]' \
506  -k "(alias check draft draftfolder draftmessage help nocheck \
507  nodraftfolder)" -- whom
508compctl -K mhfseq -x 's[+][@]' -K mhcomp -S / -q - 's[-]' \
509  -k "(file part type list headers noheaders realsize norealsize nolist \
510  show serialonly noserialonly form pause nopause noshow store auto noauto \
511  nostore cache nocache rcache wcache check nocheck ebcdicsafe noebcdicsafe \
512  rfc934mode norfc934mode verbose noverbose help)" - \
513  'c[-1,-file]' -f - 'c[-1,-form]' -K mhfile - \
514  'C[-1,-[rw]cache]' -k '(public private never ask)' -- mhn
515compctl -K mhfseq -x 's[+][@]' -K mhcomp -S / -q - 's[-]' -k '(help)' -- mhpath
516#------------------------------------------------------------------------------
517# CVS
518#
519cvscmds=(add admin rcs checkout commit diff rdiff export history import log rlog
520         release remove status tag rtag update annotate)
521cvsignore="*~ *# .#* *.o *.a CVS . .."
522
523compctl -k cvscmds \
524    -x "c[-1,-D]" -k '(today yesterday 1\ week\ ago)' \
525    - "c[-1,-m]" -k '(bugfix cosmetic\ fix ... added\ functionality foo)' \
526    - "c[-1,-F]" -f \
527    - "c[-1,-r]" -K cvsrevisions \
528    - "c[-1,-I]" -f \
529    - "R[add,;]" -K cvsaddp \
530    - "R[(admin|rcs),;]" -/K cvstargets \
531    - "R[(checkout|co),;]" -K cvsrepositories \
532    - "R[(commit|ci),;]" -/K cvstargets \
533    - "R[(r|)diff,;]" -/K cvstargets \
534    - "R[export,;]" -f \
535    - "R[history,;]" -/K cvstargets \
536    - "R[history,;] c[-1,-u]" -u \
537    - "R[import,;]" -K cvsrepositories \
538    - "R[(r|)log,;]" -/K cvstargets \
539    - 'R[(r|)log,;] s[-w] n[-1,,],s[-w]' -u -S , -q \
540    - "R[rel(|ease),;]" -f \
541    - "R[(remove|rm),;] R[-f,;]" -/K cvstargets \
542    - "R[(remove|rm),;]" -K cvsremovep \
543    - "R[status,;]" -/K cvstargets \
544    - "R[(r|)tag,;]" -/K cvstargets \
545    - "R[up(|date),;]" -/K cvstargets \
546    - "R[annotate,;]" -/K cvstargets \
547    -- cvs 
548
549compctl -/K cvstargets cvstest 
550
551cvsprefix() {
552    local nword args f
553    read -nc nword; read -Ac args
554    pref=$args[$nword]
555    if [[ -d $pref:h && ! -d $pref ]]; then
556	pref=$pref:h
557    elif [[ $pref != */* ]]; then
558	pref=
559    fi
560    [[ -n "$pref" && "$pref" != */ ]] && pref=$pref/
561}
562
563cvsentries() {
564    setopt localoptions nullglob unset
565    if [[ -f ${pref}CVS/Entries ]]; then
566	reply=( "${pref}${(@)^${(@)${(@)${(f@)$(<${pref}CVS/Entries)}:#D*}#/}%%/*}" )
567    fi
568}
569
570cvstargets() {
571    local pref
572    cvsprefix
573    cvsentries
574}
575
576cvsrevisions() {
577    reply=( "${(@)${(@)${(@M)${(@f)$(cvs -q status -vl .)}:#	*}##[ 	]##}%%[ 	]*}" )
578}
579
580cvsrepositories() {
581    local root=$CVSROOT
582    [[ -f CVS/Root ]] && root=$(<CVS/Root)
583    reply=(
584	$root/^CVSROOT(:t)
585	"${(@)${(@M)${(@f)$(<$root/CVSROOT/modules)}:#[^#]*}%%[ 	]*}"
586    )
587}
588
589cvsremovep() {
590    local pref
591    cvsprefix
592    cvsentries
593    setopt localoptions unset
594    local omit
595    omit=( ${pref}*(D) )
596    eval 'reply=( ${reply:#('${(j:|:)omit}')} )'
597}
598
599cvsaddp() {
600    local pref
601    cvsprefix
602    cvsentries
603    setopt localoptions unset
604    local all omit
605    all=( ${pref}*(D) )
606    omit=( $reply ${pref}${^${=cvsignore}} )
607    [[ -r ~/.cvsignore ]] && omit=( $omit ${pref}${^$(<~/.cvsignore)} )
608    [[ -r ${pref}.cvsignore ]] && omit=( $omit ${pref}${^$(<${pref}.cvsignore)} )
609    eval 'reply=( ${all:#('${(j:|:)omit}')} )' 
610}
611
612#------------------------------------------------------------------------------
613# RedHat Linux rpm utility
614#
615compctl -s '$(rpm -qa)' \
616	-x 's[--]' -s 'oldpackage percent replacefiles replacepkgs noscripts
617		       root excludedocs includedocs test upgrade test clean
618		       short-circuit sign recompile rebuild resign querytags
619		       queryformat version help quiet rcfile force hash' - \
620	's[ftp:]' -P '//' -s '$(</u/zsh/ftphosts)' -S '/' - \
621	'c[-1,--root]' -/ - \
622	'c[-1,--rcfile]' -f - \
623	'p[1] s[-b]' -k '(p l c i b a)' - \
624	'c[-1,--queryformat] N[-1,{]' \
625		-s '"${(@)${(@f)$(rpm --querytags)}#RPMTAG_}"' -S '}' - \
626	'W[1,-q*] C[-1,-([^-]*|)f*]' -f - \
627	'W[1,-i*], W[1,-q*] C[-1,-([^-]*|)p*]' \
628		-/g '*.rpm' + -f -- rpm
629#------------------------------------------------------------------------------
630compctl -u -x 'c[-1,-w]' -f -- ac
631compctl -/g '*.m(.)' mira
632#------------------------------------------------------------------------------
633# talk completion: complete local users, or users at hosts listed via rwho
634compctl -K talkmatch talk ytalk ytalk3
635function talkmatch {
636    local u
637    reply=($(users))
638    for u in "${(@)${(@f)$(rwho 2>/dev/null)}%%:*}"; do
639	reply=($reply ${u%% *}@${u##* })
640    done
641}
642#------------------------------------------------------------------------------
643# Linux mount
644comp_fsmount () {
645    local tmp; if [[ $UID = 0 ]]; then tmp=""; else tmp="user"; fi
646    sed -n -e "s|^[^# 	][^ 	]*[ 	][ 	]*\(/[^ 	]*\)[ 	].*$tmp.*|\1|p" /etc/fstab
647}
648comp_nfsmount () {
649    local cmd args host
650    read -Ac cmd; read -cn where
651    host=${${cmd[$where]}%%:*}
652    reply=("${(@f)$(showmount -e $host | sed -n -e "s|^/\([^ ]*\) .*|$host:/\1|p")}")
653}
654compctl -s '$(mount | \
655	      sed -e "s/^[^ ]* on \\([^ ]*\\) type.*/\\1/"'"$(
656	      if [[ ! $UID = 0 ]]; then
657		  echo ' | egrep "^${(j:|:)$(comp_fsmount)}\$"'
658	      fi)"')' umount
659compctl -s '$(comp_fsmount)' + \
660	-x 'S[/]' -f -- + \
661	-x 'C[0,*:*]' -K comp_nfsmount -- + \
662	-s '$(< /etc/hosts)' \
663	mount
664#------------------------------------------------------------------------------
665# Lynx (web browser)
666compctl -k '(http:// file: ftp:// gopher:// news://)' -S '' \
667        -x 's[ftp://]' -k ftphosts -S/ \
668        - 'n[1,://]' -k hosts -S/ \
669        - 's[file:/]' -/g '*.html' -W/ \
670	- 's[file:]' -s '~+' -S '/' \
671        - 's[-]' -k '(anonymous auth base book buried_news cache case
672		cfg child cookies crawl display dump editor emacskeys
673		enable_scrollback error_file fileversions force_html
674		from ftp get_data head help historical homepage
675		image_links index link localhost locexec mime_header
676		minimal newschunksize newsmaxchunk nobrowse noexec
677		nofilereferer nofilereferer nolist nolog nopause
678		noprint noredir noreferer nosocks nostatus number_links
679		popup post_data print pseudo_inlines raw realm reload
680		restrictions resubmit_posts rlogin selective show_cursor
681		source startfile_ok telnet term trace traversal underscore
682		validate version vikeys)' \
683        -- lynx
684#------------------------------------------------------------------------------
685# ssh (secure shell)
686compctl -k hosts \
687	-x "c[-1,-l]" -u \
688	- "c[-1,-i]" -f \
689	- "c[-1,-e]" -k "(~ none)" \
690	- "c[-1,-c]" -k "(idea des 3des tss arcfour none)" \
691	- "c[-1,-p]" -k ports \
692	- "c[-1,-L] c[-1,-R] c[-1,-o]" -k "()" \
693	-- ssh
694#------------------------------------------------------------------------------
695# network stuff
696compctl -k hosts \
697	-x "s[-class=]" -k "(any in chaos hesiod)" \
698	- "s[-query=]" -k "(a cname hinfo md mx mb mg minfo ns ptr soa txt uinfo wks any)" \
699	- "s[-]" -Q -S '' -k "(query= all\  class= d2\  nod2\  debug\  nodebug\  defname\  nodefname\  domain= ignoretc\  noignoretc\ )" \
700	-- nslookup
701
702compctl -k hosts \
703	-x "C[-1,[^-]*] p[2,-1]" -k ports \
704	-- telnet
705
706compctl -x 'N[-1,@]' -k hosts - 's[]' -u -qS '@' -- finger
707#------------------------------------------------------------------------------
708# gdb
709compctl -/g "*(*)" \
710	-x "s[-]" -k "(help nx q batch cd f b tty exec se core symbols c x d)" \
711	- "C[-1,(-cd|-directory)]" -/ \
712	- "C[-1,(-core|-c)]" -/g 'core*' \
713	- "C[-1,(-se|-exec)]" -f \
714	- "C[-1,(-symbols|-command|-x)]" -f \
715	- "p[2,-1] C[-1,[^-]*]" -/g "core*" \
716	-- gdb
717