26763: fix problem on failed cd -s to relative path
[zsh.git] / Completion / Unix / Type / _path_commands
blob393c0c67e5cb07395c030936f8fec7559780468e
1 #autoload
3 (( $+functions[_path_commands_caching_policy] )) ||
4 _path_commands_caching_policy() {
6 local file
7 local -a oldp dbfiles
9 # rebuild if cache is more than a week old
10 oldp=( "$1"(Nmw+1) )
11 (( $#oldp )) && return 0
13 dbfiles=(/usr/share/man/index.(bt|db|dir|pag)(N) \
14   /usr/man/index.(bt|db|dir|pag)(N) \
15   /var/cache/man/index.(bt|db|dir|pag)(N) \
16   /var/catman/index.(bt|db|dir|pag)(N) \
17   /usr/share/man/*/whatis(N))
19 for file in $dbfiles; do
20   [[ $file -nt $1 ]] && return 0
21 done
23 return 1
26 _call_whatis() { 
27   case "$(whatis --version)" in
28   (whatis from *)
29     local -A args
30     zparseopts -D -A args s: r:
31     apropos "${args[-r]:-"$@"}" | fgrep "($args[-s]"
32     ;;
33   (*) whatis "$@";;
34   esac
37 _path_commands() {
38 local need_desc expl ret=1
40 if zstyle -t ":completion:${curcontext}:" extra-verbose; then
41   local update_policy first
42   if [[ $+_command_descriptions -eq 0 ]]; then
43     first=yes
44     typeset -A -g _command_descriptions
45   fi
46   zstyle -s ":completion:${curcontext}:" cache-policy update_policy
47   [[ -z "$update_policy" ]] && zstyle ":completion:${curcontext}:" \
48     cache-policy _path_commands_caching_policy
49   if ( [[ -n $first ]] || _cache_invalid command-descriptions ) && \
50     ! _retrieve_cache command-descriptions; then
51     local line
52     for line in "${(f)$(_call_program command-descriptions _call_whatis -s 1 -r .\\\*\; _call_whatis -s 6 -r .\\\* 2>/dev/null)}"; do
53       [[ -n ${line:#(#b)([^ ]#) #\([^ ]#\)( #\[[^ ]#\]|)[ -]#(*)} ]] && continue;
54       [[ -z $match[1] || -z $match[3] || -z ${${match[1]}:#*:*} ]] && continue;
55       _command_descriptions[$match[1]]=$match[3]
56     done
57     _store_cache command-descriptions _command_descriptions
58   fi
60   (( $#_command_descriptions )) && need_desc=yes
63 if [[ -n $need_desc ]]; then
64   typeset -a dcmds descs cmds matches
65   local desc cmd sep
66   compadd "$@" -O matches -k commands
67   for cmd in $matches; do
68     desc=$_command_descriptions[$cmd]
69     if [[ -z $desc ]]; then
70       cmds+=$cmd
71     else
72       dcmds+=$cmd
73       descs+="$cmd:$desc"
74     fi
75   done
76   zstyle -s ":completion:${curcontext}:" list-separator sep || sep=--
77   zformat -a descs " $sep " $descs
78   descs=("${(@r:COLUMNS-1:)descs}")
79   _wanted commands expl 'external command' \
80     compadd "$@" -ld descs -a dcmds && ret=0
81   _wanted commands expl 'external command' compadd "$@" -a cmds && ret=0
82 else
83   _wanted commands expl 'external command' compadd "$@" -k commands && ret=0
86 return $ret
89 _path_commands "$@"