#autoload

local -a match mbegin mend

#
if _have_glob_qual $PREFIX; then
  local ret=1
  compset -p ${#match[1]}
  if [[ $_comp_caller_options[extendedglob] == on ]] && compset -P '\#'; then
    _globflags && ret=0
  else
    if [[ $_comp_caller_options[extendedglob] == on ]]; then
      local -a flags
      flags=(
      '#:introduce glob flag'
      )
      _describe -t globflags "glob flag" flags -Q -S '' && ret=0
    fi
    _globquals && ret=0
  fi
  return ret
fi


local linepath realpath donepath prepath testpath exppath skips skipped
local tmp1 tmp2 tmp3 tmp4 i orig eorig pre suf tpre tsuf opre osuf cpre
local pats haspats ignore pfx pfxsfx sopt gopt opt sdirs ignpar cfopt listsfx
local nm=$compstate[nmatches] menu matcher mopts sort mid accex fake
local listfiles listopts tmpdisp origtmp1 Uopt
local accept_exact_dirs path_completion
integer npathcheck
local -a Mopts

typeset -U prepaths exppaths

exppaths=()


zparseopts -a mopts \
    'P:=pfx' 'S:=pfxsfx' 'q=pfxsfx' 'r:=pfxsfx' 'R:=pfxsfx' \
    'W:=prepaths' 'F:=ignore' 'M+:=matcher' \
    J+: V+: X+: 1 2 n 'f=tmp1' '/=tmp1' 'g+:-=tmp1'

sopt="-${(@j::M)${(@)tmp1#-}#?}"
(( $tmp1[(I)-[/g]*] )) && haspats=yes
(( $tmp1[(I)-g*] )) && gopt=yes
if (( $tmp1[(I)-/] )); then
  pats="${(@)${(@M)tmp1:#-g*}#-g}"
  pats=( '*(-/)' ${${(z):-x $pats}[2,-1]} )
else
  pats="${(@)${(@M)tmp1:#-g*}#-g}"
  pats=( ${${(z):-x $pats}[2,-1]} )
fi
pats=( "${(@)pats:# #}" )

if (( $#pfx )); then
  compset -P "$pfx[2]" || pfxsfx=( "$pfx[@]" "$pfxsfx[@]" )
fi

if (( $#prepaths )); then
  tmp1="${prepaths[2]}"
  if [[ "$tmp1[1]" = '(' ]]; then
    prepaths=( ${^=tmp1[2,-2]%/}/ )
  elif [[ "$tmp1[1]" = '/' ]]; then
    prepaths=( "${tmp1%/}/" )
  else
    prepaths=( ${(P)^tmp1%/}/ )
    (( ! $#prepaths )) && prepaths=( ${tmp1%/}/ )
  fi
  (( ! $#prepaths )) && prepaths=( '' )
else
  prepaths=( '' )
fi

if (( $#ignore )); then
  if [[ "${ignore[2]}" = \(* ]]; then
    ignore=( ${=ignore[2][2,-2]} )
  else
    ignore=( ${(P)ignore[2]} )
  fi
fi


if [[ "$sopt" = -(f|) ]]; then
  if [[ -z "$gopt" ]]; then
    sopt='-f'
    pats=('*')
  else
    unset sopt
  fi
fi

if (( ! $mopts[(I)-[JVX]] )); then
  local expl

  if [[ -z "$gopt" && "$sopt" = -/ ]]; then
    _description directories expl directory
  else
    _description files expl file
  fi
  tmp1=$expl[(I)-M*]
  if (( tmp1 )); then
    if (( $#matcher )); then
      matcher[2]="$matcher[2] $expl[1+tmp1]"
    else
      matcher=(-M "$expl[1+tmp1]")
    fi
  fi
  mopts=( "$mopts[@]" "$expl[@]" )
fi


[[ -z "$_comp_no_ignore" && $#ignore -eq 0 &&
   ( -z $gopt || "$pats" = \ #\*\ # ) && -n $FIGNORE ]] && 
    ignore=( "?*${^fignore[@]}" )

if (( $#ignore )); then
  _comp_ignore=( "$_comp_ignore[@]" "$ignore[@]" )
  (( $mopts[(I)-F] )) || mopts=( "$mopts[@]" -F _comp_ignore )
fi

if [[ $#matcher -eq 0 && -o nocaseglob ]]; then
  matcher=( -M 'm:{a-zA-Z}={A-Za-z}' )
fi

if (( $#matcher )); then
  mopts=( "$mopts[@]" "$matcher[@]" )
fi

if zstyle -s ":completion:${curcontext}:" file-sort tmp1; then
  case "$tmp1" in
  *size*)             sort=oL;;
  *links*)            sort=ol;;
  *(time|date|modi)*) sort=om;;
  *access*)           sort=oa;;
  *(inode|change)*)   sort=oc;;
  *)                  sort=on;;
  esac
  [[ "$tmp1" = *rev* ]] && sort[1]=O
  [[ "$tmp1" = *follow* ]] && sort="-${sort}-"

  if [[ "$sort" = on ]]; then
    sort=
  else
    mopts=( "${(@)mopts/#-J/-V}" )

    tmp2=()
    for tmp1 in "$pats[@]"; do
      if _have_glob_qual "$tmp1" complete; then
	# unbalanced parenthesis is correct: match[1] contains the start,
	# match[5] doesn't contain the end.
	tmp2+=( "${match[1]}#q${sort})(${match[5]})" )
      else
        tmp2+=( "${tmp1}(${sort})" )
      fi
    done
    pats=( "$tmp2[@]" )
  fi
fi


if zstyle -t ":completion:${curcontext}:paths" squeeze-slashes; then
  skips='((.|..|)/)##'
else
  skips='((.|..)/)##'
fi

zstyle -s ":completion:${curcontext}:paths" special-dirs sdirs
zstyle -t ":completion:${curcontext}:paths" list-suffixes &&
    listsfx=yes

[[ "$pats" = ((|*[[:blank:]])\*(|[[:blank:]]*|\([^[:blank:]]##\))|*\([^[:blank:]]#/[^[:blank:]]#\)*) ]] &&
    sopt=$sopt/

zstyle -a ":completion:${curcontext}:paths" accept-exact accex
zstyle -a ":completion:${curcontext}:" fake-files fake

zstyle -s ":completion:${curcontext}:" ignore-parents ignpar

zstyle -t ":completion:${curcontext}:paths" accept-exact-dirs &&
  accept_exact_dirs=1
zstyle -T ":completion:${curcontext}:paths" path-completion &&
  path_completion=1

if [[ -n "$compstate[pattern_match]" ]]; then
  if { [[ -z "$SUFFIX" ]] && _have_glob_qual "$PREFIX" complete; } ||
    _have_glob_qual "$SUFFIX" complete; then
    tmp3=${match[5]}
    if [[ -n "$SUFFIX" ]]; then
      SUFFIX=${match[2]}
    else
      PREFIX=${match[2]}
    fi
    tmp2=()
    for tmp1 in "$pats[@]"; do
      if _have_glob_qual "$tmp1" complete; then
	# unbalanced parenthesis is correct: match[1] contains the start,
	# match[5] doesn't contain the end.
	tmp2+=( "${match[1]}${tmp3}${match[5]})")
      else
	tmp2+=( "${tmp1}(${tmp3})" )
      fi
    done
    pats=( "$tmp2[@]" )
  fi
fi


pre="$PREFIX"
suf="$SUFFIX"
opre="$PREFIX"
osuf="$SUFFIX"
orig="${PREFIX}${SUFFIX}"
eorig="$orig"

[[ $compstate[insert] = (*menu|[0-9]*) || -n "$_comp_correct" ||
   ( -n "$compstate[pattern_match]" &&
     "${orig#\~}" != (|*[^\\])[][*?#~^\|\<\>]* ) ]] && menu=yes
if [[ -n "$_comp_correct" ]]; then
    cfopt=-
    Uopt=-U
else
    Mopts=(-M "r:|/=* r:|=*")
fi


if [[ "$pre" = [^][*?#^\|\<\>\\]#(\`[^\`]#\`|\$)*/* && "$compstate[quote]" != \' ]]; then


  linepath="${(M)pre##*\$[^/]##/}"
  function {
    setopt localoptions nounset
    eval 'realpath=${(e)~linepath}' 2>/dev/null
  }
  [[ -z "$realpath" || "$realpath" = "$linepath" ]] && return 1
  pre="${pre#${linepath}}"
  i='[^/]'
  i="${#linepath//$i}"
  orig="${orig[1,(in:i:)/][1,-2]}"
  donepath=
  prepaths=( '' )
elif [[ "$pre[1]" = \~ && -z "$compstate[quote]" ]]; then


  linepath="${pre[2,-1]%%/*}"
  if [[ -z "$linepath" ]]; then
    realpath="${HOME%/}/"
  elif [[ "$linepath" = ([-+]|)[0-9]## ]]; then
    if [[ "$linepath" != [-+]* ]]; then
      tmp1="$linepath"
    else
      if [[ "$linepath" = -* ]]; then
        tmp1=$(( $#dirstack $linepath ))
      else
        tmp1=$linepath[2,-1]
      fi
      [[ -o pushdminus ]] && tmp1=$(( $#dirstack - $tmp1 ))
    fi
    if (( ! tmp1 )); then
      realpath=$PWD/
    elif [[ tmp1 -le $#dirstack ]]; then
      realpath=$dirstack[tmp1]/
    else
      _message 'not enough directory stack entries'
      return 1
    fi
  elif [[ "$linepath" = [-+] ]]; then
    realpath=${~:-\~$linepath}/
  else
    eval "realpath=~${linepath}/" 2>/dev/null
    if [[ -z "$realpath" ]]; then
      _message "unknown user \`$linepath'"
      return 1
    fi
  fi
  linepath="~${linepath}/"
  [[ "$realpath" = "$linepath" ]] && return 1
  pre="${pre#*/}"
  orig="${orig#*/}"
  donepath=
  prepaths=( '' )
else

  linepath=
  realpath=

  if zstyle -s ":completion:${curcontext}:" preserve-prefix tmp1 &&
     [[ -n "$tmp1" && "$pre" = (#b)(${~tmp1})* ]]; then

    pre="$pre[${#match[1]}+1,-1]"
    orig="$orig[${#match[1]}+1,-1]"
    donepath="$match[1]"
    prepaths=( '' )

  elif [[ "$pre[1]" = / ]]; then

    pre="$pre[2,-1]"
    orig="$orig[2,-1]"
    donepath='/'
    prepaths=( '' )
  else
    
    [[ "$pre" = (.|..)/* ]] && prepaths=( '' )
    donepath=
  fi
fi


for prepath in "$prepaths[@]"; do


  skipped=
  cpre=

  if [[ ( -n $accept_exact_dirs || -z $path_completion ) && \
        ${pre} = (#b)(*)/([^/]#) ]]; then
    #
    tmp1=${match[1]}
    tpre=${match[2]}
    tmp2=${(Q)tmp1}
    tmp1=${tmp1//(#b)\\(?)/$match[1]}
    tpre=${tpre//(#b)\\([^\\\]\[\^\~\(\)\#\*\?])/$match[1]}
    tmp3=${donepath//(#b)\\(?)/$match[1]}
    while true; do
      if [[ -z $path_completion || -d $prepath$realpath$tmp3$tmp2 ]]; then
	tmp3=$tmp3$tmp1/
	# Now put donepath back the way it should be.  (I think.)
	donepath=${tmp3//(#b)([\\\]\[\^\~\(\)\#\*\?])/\\$match[1]}
	pre=$tpre
	break
      elif [[ $tmp1 = (#b)(*)/([^/]#) ]]; then
	tmp1=$match[1]
	tpre=$match[2]/$tpre
      else
	break
      fi
    done
  fi

  tpre="$pre"
  tsuf="$suf"
  #
  testpath="${donepath//(#b)\\([\\\]\[\^\~\(\)\#\*\?])/$match[1]}"

  tmp2="${(M)tpre##${~skips}}"
  tpre="${tpre#$tmp2}"

  tmp1=( "$prepath$realpath$donepath$tmp2" )

  (( npathcheck = 0 ))
  while true; do

    origtmp1=("${tmp1[@]}")

    if [[ "$tpre" = */* ]]; then
      PREFIX="${tpre%%/*}"
      SUFFIX=
    else
      PREFIX="${tpre}"
      SUFFIX="${tsuf%%/*}"
    fi




    tmp2=( "$tmp1[@]" )

    if [[ "$tpre$tsuf" = (#b)*/(*) ]]; then


      if [[ -n "$fake${match[1]}" ]]; then
        compfiles -P$cfopt tmp1 accex "$skipped" "$_matcher $matcher[2]" "$sdirs" fake
      else
        compfiles -P$cfopt tmp1 accex "$skipped" "$_matcher $matcher[2]" '' fake
      fi
    elif [[ "$sopt" = *[/f]* ]]; then
      compfiles -p$cfopt tmp1 accex "$skipped" "$_matcher $matcher[2]" "$sdirs" fake "$pats[@]"
    else
      compfiles -p$cfopt tmp1 accex "$skipped" "$_matcher $matcher[2]" '' fake "$pats[@]"
    fi
    tmp1=( $~tmp1 ) 2> /dev/null

    if [[ -n "$PREFIX$SUFFIX" ]]; then

      #
      if (( ! $#tmp1 && npathcheck == 0 )); then
	(( npathcheck = 1 ))
	for tmp3 in "$tmp2[@]"; do
	  if [[ -n $tmp3 && $tmp3 != */ ]]; then
	    tmp3+=/
	  fi
	  if [[ -e "$tmp3${(Q)PREFIX}${(Q)SUFFIX}" ]] then
	    (( npathcheck = 2 ))
	  fi
	done
	if (( npathcheck == 2 )); then
	  # repeat loop with same arguments
	  tmp1=("$origtmp1[@]")
	  continue
	fi
      fi

      if (( ! $#tmp1 )); then
        tmp2=( ${^${tmp2:#/}}/$PREFIX$SUFFIX )
      elif [[ "$tmp1[1]" = */* ]]; then
        if [[ -n "$_comp_correct" ]]; then
          tmp2=( "$tmp1[@]" )
          builtin compadd -D tmp1 "$matcher[@]" - "${(@)tmp1:t}"

          if [[ $#tmp1 -eq 0 ]]; then
            tmp1=( "$tmp2[@]" )
	    compadd -D tmp1 "$matcher[@]" - "${(@)tmp2:t}"
          fi
        else
          tmp2=( "$tmp1[@]" )
          compadd -D tmp1 "$matcher[@]" - "${(@)tmp1:t}"
        fi
      else
        tmp2=( '' )
        compadd -D tmp1 "$matcher[@]" -a tmp1
      fi


      if (( ! $#tmp1 )); then
 	if [[ "$tmp2[1]" = */* ]]; then
	  tmp2=( "${(@)tmp2#${prepath}${realpath}}" )
	  if [[ "$tmp2[1]" = */* ]]; then
	    tmp2=( "${(@)tmp2:h}" )
	    compquote tmp2
	    if [[ "$tmp2" = */ ]]; then
	      exppaths=( "$exppaths[@]" ${^tmp2}${tpre}${tsuf} )
	    else
	      exppaths=( "$exppaths[@]" ${^tmp2}/${tpre}${tsuf} )
	    fi
          elif [[ ${tpre}${tsuf} = */* ]]; then
	    exppaths=( "$exppaths[@]" ${tpre}${tsuf} )

	    ### this once was in an `else' (not `elif')
	  fi
        fi
        continue 2
      fi
    elif (( ! $#tmp1 )); then

      if [[ -z "$tpre$tsuf" && -n "$pre$suf" ]]; then
	pfxsfx=(-S '' "$pfxsfx[@]")
	### Don't remember what the break was good for. We explicitly
	### execute this only when there are no matches in the directory,
	### so why continue?
	###
        ### tmp1=( "$tmp2[@]" )
	### break
      elif [[ -n "$haspats" && -z "$tpre$tsuf$suf" && "$pre" = */ ]]; then
	PREFIX="${opre}"
	SUFFIX="${osuf}"
        compadd -nQS '' - "$linepath$donepath$orig"
        tmp4=-
      fi
      continue 2
    fi

    if [[ -n "$ignpar" && -z "$_comp_no_ignore" &&
          "$tpre$tsuf" != */* && $#tmp1 -ne 0 &&
          ( "$ignpar" != *dir* || "$pats" = '*(-/)' ) &&
          ( "$ignpar" != *..* || "$tmp1[1]" = *../* ) ]]; then

      compfiles -i tmp1 ignore "$ignpar" "$prepath$realpath$donepath"
      _comp_ignore+=( ${(@)ignore#$prepath$realpath$donepath} )

      (( $#_comp_ignore && ! $mopts[(I)-F] )) &&
          mopts=( "$mopts[@]" -F _comp_ignore )
    fi


    if [[ "$tpre" = */* ]]; then
      tpre="${tpre#*/}"
    elif [[ "$tsuf" = */* ]]; then
      tpre="${tsuf#*/}"
      tsuf=
    else
      break
    fi


    tmp1=( ${tmp1//(#b)([][()|*?^#~<>\\=])/\\${match[1]}} )
    tmp2="${(M)tpre##${~skips}}"
    if [[ -n "$tmp2" ]]; then
      skipped="/$tmp2"
      tpre="${tpre#$tmp2}"
    else
      skipped=/
    fi
    (( npathcheck = 0 ))
  done


  tmp3="$pre$suf"
  tpre="$pre"
  tsuf="$suf"
  if [[ -n "${prepath}${realpath}${testpath}" ]]
  then
    if [[ -o nocaseglob ]]
    then
      tmp1=( "${(@)tmp1#(#i)${prepath}${realpath}${testpath}}" )
    else
      tmp1=( "${(@)tmp1#${prepath}${realpath}${testpath}}" )
    fi
  fi

  while true; do


    compfiles -r tmp1 "${(Q)tmp3}"
    tmp4=$?

    if [[ "$tpre" = */* ]]; then
      tmp2="${cpre}${tpre%%/*}"
      PREFIX="${linepath}${donepath}${tmp2}"
      SUFFIX="/${tpre#*/}${tsuf#*/}"
    else
      tmp2="${cpre}${tpre}"
      PREFIX="${linepath}${donepath}${tmp2}"
      SUFFIX="${tsuf}"
    fi


    if (( tmp4 )); then

      tmp2="$testpath"
      if [[ -n "$linepath" ]]; then
        compquote -p tmp2 tmp1
      elif [[ -n "$tmp2" ]]; then
        compquote -p tmp1
        compquote tmp2
      else
        compquote tmp1 tmp2
      fi

      if [[ -z "$_comp_correct" &&
            "$compstate[pattern_match]" = \*  && -n "$listsfx" &&
            "$tmp2" = (|*[^\\])[][*?#~^\|\<\>]* ]]; then
        PREFIX="$opre"
        SUFFIX="$osuf"
      fi


      if [[ -z "$compstate[insert]" ]] ||
         { ! zstyle -t ":completion:${curcontext}:paths" expand suffix &&
           [[ -z "$listsfx" &&
              ( -n "$_comp_correct" ||
                -z "$compstate[pattern_match]" || "$SUFFIX" != */* ||
                "${SUFFIX#*/}" = (|*[^\\])[][*?#~^\|\<\>]* ) ]] }; then
	# We have not been told to insert the match, so we are
	# listing, or something.
        (( tmp4 )) && zstyle -t ":completion:${curcontext}:paths" ambiguous &&
            compstate[to_end]=
        if [[ "$tmp3" = */* ]]; then
	  if [[ -z "$listsfx" || "$tmp3" != */?* ]]; then
	    # I think this means we are expanding some directory
	    # back up the path.
	    tmp1=("${(@)tmp1%%/*}")
	    _list_files tmp1 "$prepath$realpath$testpath"
	    compadd $Uopt -Qf "$mopts[@]" \
                    -p "${Uopt:+$IPREFIX}$linepath$tmp2" \
	            -s "/${tmp3#*/}${Uopt:+$ISUFFIX}" \
	            -W "$prepath$realpath$testpath" \
		    "$pfxsfx[@]" $Mopts \
		    $listopts \
	            -a tmp1
          else
	    # Same with a non-empty suffix
	    tmp1=("${(@)^tmp1%%/*}/${tmp3#*/}")
	    _list_files tmp1 "$prepath$realpath$testpath"
	    compadd $Uopt -Qf "$mopts[@]" \
                    -p "${Uopt:+$IPREFIX}$linepath$tmp2" \
	            -s "${Uopt:+$ISUFFIX}" \
	            -W "$prepath$realpath$testpath" \
		    "$pfxsfx[@]" $Mopts \
	            $listopts \
		    -a tmp1
          fi
	else
	  _list_files tmp1 "$prepath$realpath$testpath"
	  compadd $Uopt -Qf "$mopts[@]" -p "${Uopt:+$IPREFIX}$linepath$tmp2" \
	          -s "${Uopt:+$ISUFFIX}" \
	          -W "$prepath$realpath$testpath" \
		   "$pfxsfx[@]" $Mopts \
	           $listopts \
		   -a tmp1
	fi
      else
	# We are inserting the match into the command line.
        if [[ "$tmp3" = */* ]]; then
	  tmp4=( $Uopt -Qf "$mopts[@]" -p "${Uopt:+$IPREFIX}$linepath$tmp2"
	         -W "$prepath$realpath$testpath"
	         "$pfxsfx[@]" $Mopts )
	  if [[ -z "$listsfx" ]]; then
            for i in "$tmp1[@]"; do
	      tmpdisp=("$i")
	      _list_files tmpdisp "$prepath$realpath$testpath"
	      compadd "$tmp4[@]" -s "${Uopt:+$ISUFFIX}" $listopts - "$tmpdisp"
	    done
          else
            [[ -n "$compstate[pattern_match]" ]] && SUFFIX="${SUFFIX:s./.*/}*"

            for i in "$tmp1[@]"; do
	      _list_files i "$prepath$realpath$testpath"
	      compadd "$tmp4[@]" $listopts - "$i"
	    done
          fi
        else
	  _list_files tmp1 "$prepath$realpath$testpath"
	  compadd $Uopt -Qf "$mopts[@]" -p "${Uopt:+$IPREFIX}$linepath$tmp2" \
	          -s "${Uopt:+$ISUFFIX}" \
                  -W "$prepath$realpath$testpath" \
		  "$pfxsfx[@]" $Mopts \
                  $listopts \
		  -a tmp1
        fi
      fi
      tmp4=-
      break
    fi


    if [[ "$tmp3" != */* ]]; then
      tmp4=
      break
    fi


    testpath="${testpath}${tmp1[1]%%/*}/"

    tmp3="${tmp3#*/}"

    if [[ "$tpre" = */* ]]; then
      if [[ -z "$_comp_correct" && -n "$compstate[pattern_match]" &&
            "$tmp2" = (|*[^\\])[][*?#~^\|\<\>]* ]]; then
        cpre="${cpre}${tmp1[1]%%/*}/"
      else
        cpre="${cpre}${tpre%%/*}/"
      fi
      tpre="${tpre#*/}"
    elif [[ "$tsuf" = */* ]]; then
      [[ "$tsuf" != /* ]] && mid="$testpath"
      if [[ -z "$_comp_correct" && -n "$compstate[pattern_match]" &&
            "$tmp2" = (|*[^\\])[][*?#~^\|\<\>]* ]]; then
        cpre="${cpre}${tmp1[1]%%/*}/"
      else
        cpre="${cpre}${tpre}/"
      fi
      tpre="${tsuf#*/}"
      tsuf=
    else
      tpre=
      tsuf=
    fi

    tmp1=( "${(@)tmp1#*/}" )
  done

  if [[ -z "$tmp4" ]]; then
    if [[ "$mid" = */ ]]; then
      PREFIX="${opre}"
      SUFFIX="${osuf}"

      tmp4="${testpath#${mid}}"
      if [[ $mid = */*/* ]]; then
	# Multiple levels of directory involved.
	tmp3="${mid%/*/}"
	tmp2="${${mid%/}##*/}"
	if [[ -n "$linepath" ]]; then
          compquote -p tmp3
	else
          compquote tmp3
	fi
	compquote tmp4 tmp2 tmp1
	for i in "$tmp1[@]"; do
	  _list_files tmp2 "$prepath$realpath${mid%/*/}"
          compadd $Uopt -Qf "$mopts[@]" -p "${Uopt:+$IPREFIX}$linepath$tmp3/" \
	    -s "/$tmp4$i${Uopt:+$ISUFFIX}" \
            -W "$prepath$realpath${mid%/*/}/" \
	    "$pfxsfx[@]" $Mopts $listopts - "$tmp2"
	done
      else
	# Simpler case with fewer directories: avoid double counting.
	tmp2="${${mid%/}##*/}"
	compquote tmp4 tmp2 tmp1
	for i in "$tmp1[@]"; do
	  _list_files tmp2 "$prepath$realpath${mid%/*/}"
          compadd $Uopt -Qf "$mopts[@]" -p "${Uopt:+$IPREFIX}$linepath" \
	    -s "/$tmp4$i${Uopt:+$ISUFFIX}" \
            -W "$prepath$realpath" \
	    "$pfxsfx[@]" $Mopts $listopts - "$tmp2"
	done
      fi
    else
      if [[ "$osuf" = */* ]]; then
        PREFIX="${opre}${osuf}"
        SUFFIX=
      else
        PREFIX="${opre}"
        SUFFIX="${osuf}"
      fi
      tmp4="$testpath"
      if [[ -n "$linepath" ]]; then
        compquote -p tmp4 tmp1
      elif [[ -n "$tmp4" ]]; then
        compquote -p tmp1
        compquote tmp4
      else
        compquote tmp4 tmp1
      fi
      if [[ -z "$_comp_correct" && -n "$compstate[pattern_match]" &&
            "${PREFIX#\~}$SUFFIX" = (|*[^\\])[][*?#~^\|\<\>]* ]]; then
	# Pattern match, we need to be clever with matchers.
	tmp1=("$linepath$tmp4${(@)^tmp1}")
	_list_files tmp1 "$prepath$realpath"
        compadd -Qf -W "$prepath$realpath" "$pfxsfx[@]" "$mopts[@]" \
                -M "r:|/=* r:|=*" $listopts -a tmp1
      else
	# Not a pattern match
	_list_files tmp1 "$prepath$realpath$testpath"
        compadd $Uopt -Qf -p "${Uopt:+$IPREFIX}$linepath$tmp4" \
	        -s "${Uopt:+$ISUFFIX}" \
	        -W "$prepath$realpath$testpath" \
	        "$pfxsfx[@]" "$mopts[@]" $Mopts $listopts -a tmp1
      fi
    fi
  fi
done


if [[ _matcher_num -eq ${#_matchers} ]] &&
   zstyle -t ":completion:${curcontext}:paths" expand prefix &&
   [[ nm -eq compstate[nmatches] && $#exppaths -ne 0 &&
      "$linepath$exppaths" != "$eorig" ]]; then
  PREFIX="${opre}"
  SUFFIX="${osuf}"
  compadd -Q "$mopts[@]" -S '' -M "r:|/=* r:|=*" -p "$linepath" -a exppaths
fi

[[ nm -ne compstate[nmatches] ]]
