#autoload

local i fds expl list link sep

fds=( /dev/fd/<0-9>(N:t) )

if zstyle -T ":completion:${curcontext}:" verbose && [[ -h /proc/$$/fd/$fds[1] ]]; then
  zstyle -s ":completion:${curcontext}:" list-separator sep || sep=--

  if zmodload -F zsh/stat b:zstat; then
    for i in "${fds[@]}"; do
      zstat +link -A link /proc/$$/fd/$i
      list+=( "$i $sep ${link[1]}" )
    done
  elif (( $+commands[readlink] )); then
    for i in "${fds[@]}"; list+=( "$i $sep $(readlink /proc/$$/fd/$i)" )
  else
    for i in "${fds[@]}"; do
      list+=( "$i $sep $(ls -l /proc/$$/fd/$i|sed 's/.*-> //' )" )
    done
  fi

  if (( $list[(I)* $sep ?*] )); then
    _wanted file-descriptors expl 'file descriptor' compadd "$@" -d list -a - fds
    return
  fi
fi

_wanted file-descriptors expl 'file descriptor' compadd -a "$@" - fds
