/*
 * An attempt to fix TekNap's broken tabkey support
 *
 * Author: Brian Weiss <brian@got.net> - 2000
 */

assign MAX_MSG_BUFFER_SIZE 10

bind ^I parse_command getlastmsg

alias getlastmsg (void)
{
	^local itm,msgcmd,spc
	@ itm = numitems(tabkey) - 1
	@ msgcmd = CMDCHARS ## [msg]
	@ spc = [ ]
	if (numitems(tabkey))
	{
		if (hashish >= 0)
		{
			type ^U^A$msgcmd $getitem(tabkey $hashish)$spc
			@ hashish = hashish - 1
		}{
			type ^U^A$msgcmd $getitem(tabkey $itm)$spc
			@ hashish = itm - 1
		}
	}{
		@ hashish = [-1]
	}
}

on #-msg 421 "*"
{
	if (#getmatches(tabkey $0))
	{
		fe ($getmatches(tabkey $0)) nickitm
		{
			@ delitem(tabkey $nickitm)
		}
	}
	if (numitems(tabkey) == MAX_MSG_BUFFER_SIZE)
	{
		@ delitem(tabkey 0)
	}
	@ setitem(tabkey $numitems(tabkey) $0)
	@ hashish = [-1]
}

on #-send_msg 421 "*"
{
	if (#getmatches(tabkey $0))
	{
		fe ($getmatches(tabkey $0)) nickitm
		{
			@ delitem(tabkey $nickitm)
		}
	}
	if (numitems(tabkey) == MAX_MSG_BUFFER_SIZE)
	{
		@ delitem(tabkey 0)
	}
	@ setitem(tabkey $numitems(tabkey) $0)
	@ hashish = [-1]
}