#
# These are my tabkey routines. Pressing the tabkey with nothing in your buffer
# will cycle through people you are talking to or if you type part of a
# nickname and then tab it will complete the nickname. Enjoy.
#
# - whitefang (thall@epicsol.org)
#


bind ^i parse_command proc.tabkey

alias proc.tabkey
{
        ^local text,word,comp,comm,nick,temp

        @ text = L, word = rightw(1 $text), comp = pattern($^\word* $chanusers())
        unless (text)
        {
                type /msg
                @ text = L
        }
        @ comm = word(0 $text), nick = word(1 $text)

        /* msg buffer routine */
        if (comm == [/msg])
        {
                if (finditem(TK $nick) >= 0)
                {
                        parsekey erase_line
                        @ temp = getitem(TK ${finditem(TK $nick)-1})
                        xtype -l /msg ${temp?temp:getitem(TK ${numitems(TK)-1})}$chr(32)
                } {
                        parsekey erase_line
                        xtype -l /msg $getitem(TK ${numitems(TK)-1})$chr(32)
                }
                return
        }

        /* nick completion routine */
        if (!word || !numwords($comp) || right(1 $word) == [:])
        {
                return
        }
        if (numwords($comp) > 1)
        {
                xecho -b Matches:
                echo $comp
                return
        }
        if (numwords($text) == 1)
        {
                parsekey erase_line
                xtype -l $comp:$chr(32)
        } {
                pop text
                parsekey erase_line
                xtype -l $text $comp
        }
}

alias proc.add_tabkey (nick)
{
        if (nick)
        {
                if (finditem(TK $nick) >= 0) {@ delitem(TK $finditem(TK $nick))}
                @ setitem(TK $numitems(TK) $nick)
                if (numitems(TK) == 10) {@ delitem(TK 0)}
        }
}

on #-dcc_chat -420 * proc.add_tabkey =$0
on #-dcc_connect -420 "% CHAT *" proc.add_tabkey =$0
on #-msg -420 * proc.add_tabkey $0
on #-send_dcc_chat -420 * proc.add_tabkey =$0
on #-send_msg -420 * proc.add_tabkey $0

alias m msg
alias msg
{
        //msg $*
        if (@)
        {
                proc.add_tabkey $0
        }
}

# tjbh/2000
