# basde on
#  incremental completion for zsh
#  by y.fujii <y-fujii at mimosa-pudica.net>, public domain

zle -N self-insert self-insert-incr
# zle -N vi-backward-delete-char-incr

# bindkey -M viins '^h' vi-backward-delete-char-incr
# bindkey -M viins '^?' vi-backward-delete-char-incr

# to increase the incr-0.2 max matches
export INCR_MAX_MATCHES=60

# function limit-completion () {
#    if ((compstate[nmatches] <= 1)); then
#       zle -M ""
#    elif ((compstate[list_lines] > ${INCR_MAX_MATCHES:-20})); then
#       compstate[list]=""
#       zle -M "too many matches."
#    fi
# }
function limit-completion () {
   # got the line comparing with LINES from
   #  /usr/share/zsh/5.0.2/functions/Zle/incremental-complete-word
   if ((compstate[list_lines] > ${INCR_MAX_MATCHES:-20} \
        || compstate[list_lines]+BUFFERLINES+2 > LINES))
   then
      compstate[list]=''
      zle -M "too many matches."
   fi
}

function self-insert-incr () {
   if zle .self-insert; then
      show-choices
      # complete-word-incr
   fi
}

function vi-backward-delete-char-incr () {
   if zle vi-backward-delete-char; then
      show-choices
      # complete-word-incr
   fi
}

function show-choices () {
   # local cursor_org
   # local buffer_org
   # local cursor_now
   # local buffer_now
   # cursor_org="$CURSOR"
   # buffer_org="$BUFFER"
   # if [[ "$BUFFER[1]" != "!" ]]
   # echo $widgets[list-choices]
   #  user:_zsh_highlight_widget_list-choices
   # mv 91-history-substring-search.zsh \
   #     91-history-substring-search.zsh.disable
   # rm 90-syntax.zsh
   # echo $widgets[list-choices]
   #  completion:.list-choices:_main_complete
   # compinit is redirecting list-choices to _main_complete
   #  and the _normal called by _main_complete is doing a
   #  bang expansion as soon as it sees a ! in the string
   #  Hence, do not call list-choices if there is a ! in that line
   #  or, instead of filtering $BUFFER on !, unsetopt BANG_HIST
   #  as list-choices is checking on BANG_HIST before expanding
   #  the !.
   # if [[ "$BUFFER" != *\!* && \
   unsetopt localoptions BANG_HIST
   # do not list-choices if it is a paste, pending > 0 in such cases
   # do not list-choices if editing in the middle of a word
   if [[ "$PENDING" -eq 0  && \
         ( -z "$RBUFFER" || "$RBUFFER[1]" == ' ' )
         ]]
   then
      comppostfuncs=(limit-completion)
      zle list-choices
   fi
   # cursor_now="$CURSOR"
   # buffer_now="$BUFFER"
}

function complete-word-incr () {
   local cursor_org
   local buffer_org
   local cursor_now
   local buffer_now
   local lbuffer_now
   cursor_org="$CURSOR"
   buffer_org="$BUFFER"
   comppostfuncs=(limit-completion)
   zle complete-word
   cursor_now="$CURSOR"
   buffer_now="$BUFFER"
   lbuffer_now="$LBUFFER"
   # if [ "$cursor_now" -gt "$cursor_org" \
   #    -a "$buffer_org[1,cursor_org]" == "$buffer_now[1,cursor_org]" \
   #    -a "$lbuffer_now[-1]" == " " ]
   # then
   #    CURSOR="$cursor_org"
   # fi
}

# vim: set filetype=zsh shiftwidth=3 tabstop=3 expandtab fileformat=unix
