#!/usr/bin/zsh

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

# got the below alignment with
#  or, below
#  :11,12s/\s\+/ /g
#  '<,'>Tabularize /\s/l0c0
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
   local list_lines
   list_lines=$compstate[list_lines]
   {
   setopt localoptions xtrace verbose
   echo "started limit-completion"
   echo "compstate[list_lines]: $compstate[list_lines]"
   echo "INCR_MAX_MATCHES: ${INCR_MAX_MATCHES:-20}"
   echo "BUFFERLINES: $BUFFERLINES"
   echo "list_lines: $list_lines"
   echo "expr list_lines: $(expr $list_lines + $BUFFERLINES + 2)"
   echo "----- no idea why these below lines do not work"
   echo "4: $(expr ${compstate[list_lines]})"
   echo "5: $(expr $compstate[list_lines])"
   echo "6: $(expr $compstate[list_lines] + $BUFFERLINES)"
   echo "7: $(expr $compstate[list_lines] + $BUFFERLINES + 2)"
   echo "----- no idea why the above lines do not work"
   echo "LINES: $LINES"
   echo "ended limit-completion"
   # if ((compstate[list_lines] > ${INCR_MAX_MATCHES:-20} \
   #      || compstate[list_lines]+BUFFERLINES+2 > LINES))
   if [[ "$list_lines" -gt "${INCR_MAX_MATCHES:-20}" \
         || $(expr $list_lines + $BUFFERLINES + 2) -gt "$LINES" ]]
   then
      compstate[list]=''
      zle -M "too many matches."
   fi
   }  2>>| /tmp/zsh-limit-completion.log 1>&2
}

function self-insert-incr () {
   # echo "started 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" != *\!* && \
     setopt localoptions
   unsetopt BANG_HIST
   # setopt xtrace verbose
   # do not list-choices if there is a ! in that word
   #  but, as I could not figure out the word vs line,
   #  just skipping on any line with a !
   # 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 [[ "$BUFFER" != *\!* \
         && "$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
