26763: fix problem on failed cd -s to relative path
[zsh.git] / Completion / compdump
blob068383c22ba66269a330e388527fd4f4dc9a74e1
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.
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.
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.
16 emulate -L zsh
17 setopt extendedglob noshglob
19 typeset _d_file _d_f _d_bks _d_line _d_als _d_files _d_name _d_tmp
21 _d_file=${_comp_dumpfile-${0:h}/compinit.dump}.$HOST.$$
22 [[ $_d_file = //* ]] && _d_file=${_d_file[2,-1]}
24 [[ -w ${_d_file:h} ]] || return 1
26 _d_files=( ${^~fpath:/.}/^([^_]*|*~|*.zwc)(N) )
28 if [[ -n "$_comp_secure" ]]; then
29   _d_wdirs=( ${^fpath}(Nf:g+w:,f:o+w:,^u0u${EUID}) )
30   _d_wfiles=( ${^~fpath:/.}/^([^_]*|*~|*.zwc)(N^u0u${EUID}) )
32   (( $#_d_wfiles )) && _d_files=( "${(@)_d_files:#(${(j:|:)_d_wfiles})}"  )
33   (( $#_d_wdirs ))  && _d_files=( "${(@)_d_files:#(${(j:|:)_d_wdirs})/*}" )
36 print "#files: $#_d_files\tversion: $ZSH_VERSION" > $_d_file
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.
42 print "\n_comps=(" >> $_d_file
43 for _d_f in ${(ok)_comps}; do
44   print -r - "${(qq)_d_f}" "${(qq)_comps[$_d_f]}"
45 done >> $_d_file
46 print ")" >> $_d_file
48 print "\n_services=(" >> $_d_file
49 for _d_f in ${(ok)_services}; do
50   print -r - "${(qq)_d_f}" "${(qq)_services[$_d_f]}"
51 done >> $_d_file
52 print ")" >> $_d_file
54 print "\n_patcomps=(" >> $_d_file
55 for _d_f in ${(ok)_patcomps}; do
56   print -r - "${(qq)_d_f}" "${(qq)_patcomps[$_d_f]}"
57 done >> $_d_file
58 print ")" >> $_d_file
60 _d_tmp="_postpatcomps"
61 print "\n_postpatcomps=(" >> $_d_file
62 for _d_f in ${(ok)_postpatcomps}; do
63   print -r - "${(qq)_d_f}" "${(qq)_postpatcomps[$_d_f]}"
64 done >> $_d_file
65 print ")" >> $_d_file
67 print "\n_compautos=(" >> $_d_file
68 for _d_f in "${(ok@)_compautos}"; do
69   print -r - "${(qq)_d_f}" "${(qq)_compautos[$_d_f]}"
70 done >> $_d_file
71 print ")" >> $_d_file
73 print >> $_d_file
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 `_').
81 _d_bks=()
82 typeset _d_complist=
83 zle -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
94 bindkey |
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
101 print >> $_d_file
104 # Autoloads: look for all functions beginning with `_'.
106 _d_als=(${(o)$(typeset +fm '_*')})
108 # print them out:  about five to a line looks neat
110 integer _i=5
111 print -n autoload -Uz >> $_d_file
112 while (( $#_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
121 done >> $_d_file
123 print >> $_d_file
125 local _c
126 for _c in "${(ok@)_compautos}"; do
127   print "autoload -Uz $_compautos[$_c] $_c" >> $_d_file
128 done
130 print >> $_d_file
132 print "typeset -gUa _comp_assocs" >> $_d_file
133 print "_comp_assocs=( ${(qq)_comp_assocs} )" >> $_d_file
135 mv -f $_d_file ${_d_file%.$HOST.$$}
137 unfunction compdump
138 autoload -Uz compdump