#autoload

#
#
#
#
#

local opt expl max slice hmax=$#historywords beg=2

if zstyle -t ":completion:${curcontext}:" remove-all-dups; then
  opt=-
else
  opt=-1
fi

if zstyle -t ":completion:${curcontext}:" sort; then
  opt="${opt}J"
else
  opt="${opt}V"
fi

if zstyle -s ":completion:${curcontext}:" range max; then
  if [[ $max = *:* ]]; then
    slice=${max#*:}
    max=${max%:*}
  else
    slice=$max
  fi
  [[ max -gt hmax ]] && max=$hmax
else
  max=$hmax
  slice=$max
fi

PREFIX="$IPREFIX$PREFIX"
IPREFIX=
SUFFIX="$SUFFIX$ISUFFIX"
ISUFFIX=


local -a hslice
while [[ $compstate[nmatches] -eq 0 && beg -lt max ]]; do
  if [[ -n $compstate[quote] ]]
  then hslice=( ${(Q)historywords[beg,beg+slice]} )
  else hslice=( ${historywords[beg,beg+slice]} )
  fi
  _wanted "$opt" history-words expl 'history word' \
      compadd -Q -a hslice
  (( beg+=slice ))
done

(( $compstate[nmatches] ))
