#compdef -subscript-

if [[ "$PREFIX" = :* ]]; then
  compadd -p: -S ':]' alnum alpha blank cntrl digit graph lower print punct \
      space upper xdigit
elif [[ ${(Pt)${compstate[parameter]}} = assoc* ]]; then
  if [[ "$RBUFFER" = \]* ]]; then
    compadd -S '' - "${(@kP)${compstate[parameter]}}"
  else
    compadd -S ']' - "${(@kP)${compstate[parameter]}}"
  fi
elif [[ ${(Pt)${compstate[parameter]}} = array* ]]; then
  local list i j

  ind=( {1..${#${(P)${compstate[parameter]}}}} )
  list=()
  for i in "$ind[@]"; do
    [[ "$i" = ${PREFIX}*${SUFFIX} ]] &&
        list=( "$list[@]" 
	  "${(r:4:: ::):)i} $(print -D ${(P)${compstate[parameter]}[$i]})" )
  done

  if [[ "$RBUFFER" = \]* ]]; then
    compadd -S '' -V default -y list - "$ind[@]"
  else
    compadd -S ']' -V default -y list - "$ind[@]"
  fi
else
  _compalso -math-
fi
