# @(#) $eterna: tabkey,v 1.10 2011/03/01 02:55:15 mrg Exp $
#------------------------------------------------------------------------------#
# updated for 2.2.2
# This keeps track of the nicks of people you MSG.  You can then
# just hit the tab key ^I to flip through the list of people
# you sent MSGs to.  
# Assign tk.msgmax to the number of nicknames you wish to store
# Modified by Chetnik (s902211@yallara.cs.rmit.oz.au)
#         and Daemon  (frechett@spot.colorado.edu)
# It will now keep unique nicknames in the list ordered from the most recent
# message received or sent to the oldest message received or sent.
# Ctrl R will flip thru the list backwards (Reverse).
# Crtl-X Ctrl-X deletes the currently displayed or current nick from the list.
# /addnick <nickname list>	will add the nicknames to the list.
# /nicklist 			will show the current list of names
# If a nickname which has 'never' existed is messaged, it will not be added to
# the list.

# searches thru list forwards (tab) or backwards (ctrl R)
bind ^I parse_command ^tk.getmsg 1 $tk.msglist
bind ^R parse_command ^tk.getmsg -1 $tk.msglist
# Delete current nickname or currently displayed nickname from list
# XXX - mrg: disabled due to conflicts with 'screen'.
#bind ^X^X parse_command tk.delnick
# shows all the current nicknames in the list.
alias nicklist echo *** Nickname List: $tk.msglist
# Adds nicknames to the list.. 
alias addnick if ([$1]) { addnick $1- };tk.addmsg $0 $tk.msglist
# Set this to the max number of nickname you want on the list at a time
@ tk.msgmax = 10

# From here down are internal aliases and 'ON's.
# This script uses SERIAL NUMBER  #55
# keeps list of unique nicks from newest message to oldest message.
alias tk.addmsg {
	@ tk.matched = rmatch($0 $^\1-)
	if (tk.matched)
	{
		@ tk.msglist = [$(0-${tk.matched-1}) $(${tk.matched+1}-)]
	}
	#else
	{	@ tk.msglist = [$(0-${tk.msgmax-1})] }
	@ tk.msgcnt = 0
	^assign -tk.matched
}
# searches thru list forwards or backwards. ($0==1==forward),($0==-1==back)
alias tk.getmsg {
	@ tk.msgcnt = tk.msgcnt + [$0]
	if ( #tk.msglist < tk.msgcnt ) {@ tk.msgcnt = 1}
	if (tk.msgcnt <= 0) {@ tk.msgcnt =  #tk.msglist}
	@ tk.junk = K ## [msg]
	type ^U$tk.junk $^\^^{[$($tk.msgcnt)]} 
}
# some initialisation.  You can comment these out if you want to.
# ^on #-401 55 * if ([$AUTO_WHOWAS] != [ON]) { ^whowas $1 }
# ^on ^406 * {
#	^assign tk.msglist $notword($rmatch($1 $tk.msglist) $tk.msglist)
#	if ([$AUTO_WHOWAS] == [ON]) { echo *** $1- }
#}
	

# Adds nick to list when message is sent or received. Doesn't effect output.
on #-send_msg 55 * ^tk.addmsg $0 $tk.msglist
on #-msg 55 * ^tk.addmsg $0 $tk.msglist
on #-send_dcc_chat 55 * ^tk.addmsg \=$0 $tk.msglist
on #-dcc_chat 55 * ^tk.addmsg \=$0 $tk.msglist

# follow nick name changes
on #-nick 55 * tk.followadd $0 $1 $tk.msglist
alias tk.followadd {
        # if the nick is in the list, add the new nick
        @ tk.matched = rmatch($0 $^\2-)
        if (tk.matched)
        {
                addnick $1
        }
        ^assign -tk.matched
}

# deletes current nick from list
alias tk.delnick {
	if (tk.msgcnt == 0)
	{
		echo *** Nickname: $word(0 $tk.msglist) removed.
		@ tk.msglist = [$notword(1 $tk.msglist)]
	}
	{
		echo *** Nickname: $word(${tk.msgcnt-1} $tk.msglist) removed.
		@ tk.msglist = [$notword($tk.msgcnt $tk.msglist)]
	}
	type ^U
}
# The $notword() function.  
alias notword {
    if ([$0] > 0)
    {
	if (([$0] > 1) && ([$0] < rmatch($~ $1-)))
		{ @ nw.sep = [ ] }
		{ @ nw.sep = [] }
		
	@ function_return = [$(1-${[$0]-1})] ## [$nw.sep] ## [$(${[$0]+1}-)]
    }
    {
        @ function_return = [$1-]
    }
}
#------------------------------------------------------------------------------#
