#autoload


local sep pref npref i tmp2 group expl menu pre suf opre osuf orig cpre
local opts sopts matcher imm
typeset -U tmp1 matches


zparseopts -D -a sopts \
    'J+:=group' 'V+:=group' 'X+:=expl' 'P:=opts' 'F:=opts' \
    S: r: R: q 1 2 n f 'M+:=matcher' 'i=imm'

sopts=( "$sopts[@]" "$opts[@]" )
if (( $#matcher )); then
  matcher="${matcher[2]}"
else
  matcher=
fi


sep="$1"
if [[ "${2[1]}" = '(' ]]; then
  tmp1=( ${=2[2,-2]} )
else
  tmp1=( "${(@P)2}" )
fi


pre="$PREFIX"
suf="$SUFFIX"
opre="$PREFIX"
osuf="$SUFFIX"
orig="$PREFIX$SUFFIX"


[[ $compstate[insert] = (*menu|[0-9]*) || -n "$_comp_correct" ||
   ( $#compstate[pattern_match] -ne 0 &&
     "$orig" != "${orig:q}" ) ]] && menu=yes


pref=''


compadd -O matches -M "r:|${sep}=* r:|=* $matcher" -a tmp1

(( $#matches )) || matches=( "$tmp1[@]" )

while true; do


  if [[ "$pre" = *${sep}* ]]; then
    PREFIX="${pre%%${sep}*}"
    SUFFIX=""
  else
    PREFIX="${pre}"
    SUFFIX="${suf%%${sep}*}"
  fi


  if [[ -n "$PREFIX$SUFFIX" || "$pre" = ${sep}* ]]; then
    tmp1=( "${(@M)matches:#${PREFIX}${SUFFIX}${sep}*}" )
  else
    tmp1=()
  fi

  if (( $#tmp1 )); then
    npref="${PREFIX}${SUFFIX}${sep}"
  else

    builtin compadd -O tmp1 -M "r:|${sep}=* r:|=* $matcher" - "${(@)${(@)matches%%${sep}*}:#}"

    [[ $#tmp1 -eq 0 && -n "$_comp_correct" ]] &&
      compadd -O tmp1 -M "r:|${sep}=* r:|=* $matcher" - "${(@)${(@)matches%%${sep}*}:#}"

    if [[ $#tmp1 -eq 1 ]]; then


      if [[ "$pre$suf" = *${sep}* ]]; then
        npref="${tmp1[1]}${sep}"
      else
        matches=( "${(@M)matches:#${tmp1[1]}*}" )

	PREFIX="${cpre}${pre}"
	SUFFIX="$suf"

	if [[ $#imm -ne 0 && $#matches -eq 1 ]] ||
           zstyle -t ":completion:${curcontext}:" expand suffix; then
	  compadd "$group[@]" "$expl[@]" "$sopts[@]" \
                  -M "r:|${sep}=* r:|=* $matcher" - $pref$matches
        else
	  if (( $matches[(I)${tmp1[1]}${sep}*] )); then
	    compadd "$group[@]" "$expl[@]" -p "$pref" -r "$sep" -S "$sep" "$opts[@]" \
                    -M "r:|${sep}=* r:|=* $matcher" - "$tmp1[1]"
          else
	    compadd "$group[@]" "$expl[@]" -p "$pref" "$sopts[@]" \
                    -M "r:|${sep}=* r:|=* $matcher" - "$tmp1[1]"
          fi
        fi
	return
      fi
    elif (( $#tmp1 )); then
      local ret=1 tt
      local -a mm


      PREFIX="$pre"
      SUFFIX="$suf"
      compadd -O matches -M "r:|${sep}=* r:|=* $matcher" -a matches

      if [[ "$pre" = *${sep}* ]]; then
 	PREFIX="${cpre}${pre%%${sep}*}"
	SUFFIX="${sep}${pre#*${sep}}${suf}"
      else
        PREFIX="${cpre}${pre}"
	SUFFIX="$suf"
      fi

      for tt in $tmp1
      do
        mm+=( "${(@M)matches:#$tt*}" )
      done
      matches=( $mm )

      if ! zstyle -t ":completion:${curcontext}:" expand suffix ||
         [[ -n "$menu" || -z "$compstate[insert]" ]]; then


        tmp2="$pre$suf"
        if [[ "$tmp2" = *${sep}* ]]; then
          tmp2=(-s "${sep}${tmp2#*${sep}}")
        else
	  tmp2=()
        fi


        compadd "$group[@]" "$expl[@]" -r "$sep" -S "$sep" "$opts[@]" \
	        -p "$pref" "$tmp2[@]" -M "r:|${sep}=* r:|=* $matcher" - \
                "${(@)${(@)${(@M)matches:#*${sep}}%%${sep}*}:#}" && ret=0
        (( $matches[(I)${sep}*] )) &&
            compadd "$group[@]" "$expl[@]" -S '' "$opts[@]" \
	            -p "$pref" \
                    -M "r:|${sep}=* r:|=* $matcher" - "$sep" && ret=0
        compadd "$group[@]" "$expl[@]" -r "$sep" -S "$sep" "$opts[@]" \
                -p "$pref" "$tmp2[@]" -M "r:|${sep}=* r:|=* $matcher" - \
                "${(@)${(@)${(@M)matches:#*?${sep}?*}%%${sep}*}:#}" && ret=0
        compadd "$group[@]" "$expl[@]" -S '' "$opts[@]" -p "$pref" "$tmp2[@]" \
                -M "r:|${sep}=* r:|=* $matcher" - \
                "${(@)matches:#*${sep}*}" && ret=0
      else
	# the unmatched part as a suffix. This will insert the longest
	# unambiguous string for all matching strings.

        compadd "$group[@]" "$expl[@]" "$opts[@]" \
	        -p "$pref" -s "${i#*${sep}}" \
                -M "r:|${sep}=* r:|=* $matcher" - \
                "${(@)${(@)${(@M)matches:#*${sep}*}%%${sep}*}:#}" && ret=0
        compadd "$group[@]" "$expl[@]" -S '' "$opts[@]" -p "$pref" \
                -M "r:|${sep}=* r:|=* $matcher" - \
                "${(@)matches:#*${sep}*}" && ret=0
      fi
      return ret
    else

      { ! zstyle -t ":completion:${curcontext}:" expand prefix ||
        [[ "$orig" = "$pref$pre$suf" ]] } && return 1

      PREFIX="${cpre}${pre}"
      SUFFIX="$suf"

      if [[ -n "$suf" ]]; then
        compadd "$group[@]" "$expl[@]" -s "$suf" "$sopts[@]" \
                -M "r:|${sep}=* r:|=* $matcher" - "$pref$pre"
      else
        compadd "$group[@]" "$expl[@]" -S '' "$opts[@]" \
                -M "r:|${sep}=* r:|=* $matcher" - "$pref$pre"
      fi
      return
    fi
  fi


  matches=( "${(@)${(@)${(@M)matches:#${npref}*}#*${sep}}:#}" )
  pref="$pref$npref"


  if [[ "$pre" = *${sep}* ]]; then
    cpre="${cpre}${pre%%${sep}*}${sep}"
    pre="${pre#*${sep}}"
  elif [[ "$suf" = *${sep}* ]]; then
    cpre="${cpre}${pre}${suf%%${sep}*}${sep}"
    pre="${suf#*${sep}}"
    suf=""
  else

    PREFIX="${opre}${osuf}"
    SUFFIX=""

    if [[ -n "$pref" && "$orig" != "$pref" ]]; then
      if [[ "$pref" = *${sep}*${sep} ]]; then
        compadd "$group[@]" "$expl[@]" "$opts[@]" \
                -p "${pref%${sep}*${sep}}${sep}" -S "$sep" \
                -M "r:|${sep}=* r:|=* $matcher" - "${${pref%${sep}}##*${sep}}"

      elif [[ "$pref" = *${sep}* ]]; then
        compadd "$group[@]" "$expl[@]" -S '' "$opts[@]" \
                -p "${pref%${sep}*}${sep}" \
                -M "r:|${sep}=* r:|=* $matcher" - "${pref##*${sep}}"
      else
        compadd "$group[@]" "$expl[@]" -S '' "$opts[@]" \
                -M "r:|${sep}=* r:|=* $matcher" - "$pref"
      fi
    fi
    return
  fi
done
