1# This is a function to dump the definitions for new-style
2# completion defined by 'compinit' in the same directory.  The output
3# should be directed into the "compinit.dump" in the same directory as
4# compinit. If you rename init, just stick .dump onto the end of whatever
5# you have called it and put it in the same directory.  This is handled
6# automatically if you invoke compinit with the option -d.
7#
8# You will need to update the dump every time you add a new completion.
9# To do this, simply remove the .dump file, start a new shell, and
10# create the .dump file as before.  Again, compinit -d handles this
11# automatically.
12
13# Print the number of files used for completion. This is used in compinit
14# to see if auto-dump should re-dump the dump-file.
15
16emulate -L zsh
17setopt extendedglob noshglob
18
19typeset _d_file _d_f _d_bks _d_line _d_als _d_files _d_name _d_tmp
20
21_d_file=${_comp_dumpfile-${0:h}/compinit.dump}.$HOST.$$
22[[ $_d_file = //* ]] && _d_file=${_d_file[2,-1]}
23
24[[ -w ${_d_file:h} ]] || return 1
25
26_d_files=( ${^~fpath:/.}/^([^_]*|*~|*.zwc)(N) )
27
28if [[ -n "$_comp_secure" ]]; then
29  _d_wdirs=( ${^fpath}(Nf:g+w:,f:o+w:,^u0u${EUID}) )
30  _d_wfiles=( ${^~fpath:/.}/^([^_]*|*~|*.zwc)(N^u0u${EUID}) )
31
32  (( $#_d_wfiles )) && _d_files=( "${(@)_d_files:#(${(j:|:)_d_wfiles})}"  )
33  (( $#_d_wdirs ))  && _d_files=( "${(@)_d_files:#(${(j:|:)_d_wdirs})/*}" )
34fi
35
36print "#files: $#_d_files\tversion: $ZSH_VERSION" > $_d_file
37
38# Dump the arrays _comps, _services and _patcomps.  The quoting
39# hieroglyphics ensure that a single quote inside a variable is itself
40# correctly quoted.
41
42print "\n_comps=(" >> $_d_file
43for _d_f in ${(ok)_comps}; do
44  print -r - "${(qq)_d_f}" "${(qq)_comps[$_d_f]}"
45done >> $_d_file
46print ")" >> $_d_file
47
48print "\n_services=(" >> $_d_file
49for _d_f in ${(ok)_services}; do
50  print -r - "${(qq)_d_f}" "${(qq)_services[$_d_f]}"
51done >> $_d_file
52print ")" >> $_d_file
53
54print "\n_patcomps=(" >> $_d_file
55for _d_f in ${(ok)_patcomps}; do
56  print -r - "${(qq)_d_f}" "${(qq)_patcomps[$_d_f]}"
57done >> $_d_file
58print ")" >> $_d_file
59
60_d_tmp="_postpatcomps"
61print "\n_postpatcomps=(" >> $_d_file
62for _d_f in ${(ok)_postpatcomps}; do
63  print -r - "${(qq)_d_f}" "${(qq)_postpatcomps[$_d_f]}"
64done >> $_d_file
65print ")" >> $_d_file
66
67print "\n_compautos=(" >> $_d_file
68for _d_f in "${(ok@)_compautos}"; do
69  print -r - "${(qq)_d_f}" "${(qq)_compautos[$_d_f]}"
70done >> $_d_file
71print ")" >> $_d_file
72
73print >> $_d_file
74
75# Now dump the key bindings. We dump all bindings for zle widgets
76# whose names start with a underscore.
77# We need both the zle -C's and the bindkey's to recreate.
78# We can ignore any zle -C which rebinds a standard widget (second
79# argument to zle does not begin with a `_').
80
81_d_bks=()
82typeset _d_complist=
83zle -lL |
84  while read -rA _d_line; do
85    if [[ ${_d_line[3]} = _* && ${_d_line[5]} = _* ]]; then
86      if [[ -z "$_d_complist" && ${_d_line[4]} = .menu-select ]]; then
87        print 'zmodload -i zsh/complist'
88	_d_complist=yes
89      fi
90      print -r - ${_d_line}
91      _d_bks+=(${_d_line[3]})
92    fi
93  done >> $_d_file
94bindkey |
95  while read -rA _d_line; do
96    if [[ ${_d_line[2]} = (${(j.|.)~_d_bks}) ]]; then
97      print -r "bindkey '${_d_line[1][2,-2]}' ${_d_line[2]}"
98    fi
99  done >> $_d_file
100
101print >> $_d_file
102
103
104# Autoloads: look for all functions beginning with `_'.
105
106_d_als=(${(o)$(typeset +fm '_*')})
107
108# print them out:  about five to a line looks neat
109
110integer _i=5
111print -n autoload -Uz >> $_d_file
112while (( $#_d_als )); do
113  if (( ! $+_compautos[$_d_als[1]] )); then
114    print -n " $_d_als[1]"
115    if (( ! --_i && $#_d_als > 1 )); then
116      _i=5
117      print -n ' \\\n           '
118    fi
119  fi
120  shift _d_als
121done >> $_d_file
122
123print >> $_d_file
124
125local _c
126for _c in "${(ok@)_compautos}"; do
127  print "autoload -Uz $_compautos[$_c] $_c" >> $_d_file
128done
129
130print >> $_d_file
131
132print "typeset -gUa _comp_assocs" >> $_d_file
133print "_comp_assocs=( ${(qq)_comp_assocs} )" >> $_d_file
134
135mv -f $_d_file ${_d_file%.$HOST.$$}
136
137unfunction compdump
138autoload -Uz compdump
139