26763: fix problem on failed cd -s to relative path
[zsh.git] / Completion / Unix / Type / _list_files
blobe04392d1dc99ebf4fade1e12527c09e1ebda75e4
1 #autoload
3 # Helper function for _path_files to handle the file-list style.
5 # arguments:
6 #  name of parameter containing file matches
7 #  directory prefix
8 # Sets array listfiles to the display strings and the array
9 # listopts appropriately to be added to the compadd command line.
11 local stat f elt what dir
12 local -a stylevals
13 integer ok
15 listfiles=()
16 listopts=()
18 zstyle -a ":completion:${curcontext}:" file-list stylevals || return 1
20 # TODO: more flexible way of handling the following?  e.g. use $compstate?
21 case $WIDGETSTYLE in
22   (*complete*)
23   what=insert
24   ;;
26   (*)
27   what=list
28   ;;
29 esac
31 for elt in $stylevals; do
32   case $elt in
33     (*($what|all|true|1|yes)*=<->)
34     # use long format if no more than the given number of matches
35     (( ${(P)#1} <= ${elt##*=} )) && (( ok = 1 ))
36     break
37     ;;
39     (*($what|all|true|1|yes)[^=]#)
40     # always use long format
41     (( ok = 1 ))
42     break
43     ;;
44   esac
45 done
47 (( ok )) || return 1
49 zmodload -F zsh/stat b:zstat 2>/dev/null || return 1
51 dir=${2:+$2/}
52 dir=${(Q)dir}
54 for f in ${(PQ)1}; do
55   if [[ ! -e "$dir$f" ]]; then
56     listfiles+=("$dir$f")
57     continue
58   fi
60   # Borrowed from Functions/Example/zls
61   zstat -s -H stat -F "%b %e %H:%M" - "$dir$f" >/dev/null 2>&1
63   listfiles+=("$stat[mode] ${(l:3:)stat[nlink]} ${(r:8:)stat[uid]} \
64  ${(r:8:)stat[gid]} ${(l:8:)stat[size]} $stat[mtime] $f")
65 done
67 (( ${#listfiles} )) && listopts=(-d listfiles -l -o)
69 return 0