# Some misc functions..  for 2.2 and beyond.
# by: Ian Frechette  (frechett@spot.colorado.edu)
# Wed Aug 18 00:26:53 MDT 1993
@ RCS.functions = [$$Header: /local/cvs/ircii/script/functions,v 1.3 2001/08/12 15:44:17 mrg Exp $$]


# ircII scripting note.  @ var = <expression> is used a lot. One case
# where it is not feasible to use  the @ var = othervar or @ var = funct()
# format is when the $(stuff) construct is needed.  In that case you
# must surround it with [].  e.g.   @ blah = [$($0)]   will assign
# to 'blah' the contents of the variable named in arg 0.

# 
# reverse a string fed to it.  The first argument is the width of the
# field in which to right justify the reversed text
# $reverse(65 one two three) returns.. 
#                                                          eerht owt eno
# if 0 is given as the width it's reversed and output left justified
alias reverse {
	^assign -rev.result
	@ rev.ind = 0
	@ rev.str = [$1-]
	while (mid($rev.ind 1 $rev.str)!=[])
	{
		@ rev.result = mid($rev.ind 1 $rev.str) ## rev.result
		@ rev.ind = rev.ind + 1
	}
	# This right justifies to a field with of $0.  
	# strip out the format() function if you don't want it formatted.
	@ function_return = format($0 $rev.result)
}

# These have been updated to handle multiple words.
# format and lformat differ from $[-num]var and $[num]var in that
# They don't chop off the string if it is too long.
alias format {
	@ IRCII.word = [$1-]
	if (@IRCII.word < [$0]) 
		{ @ function_return = [$([-$0]IRCII.word)] } 
		{ @ function_return = IRCII.word } 
}

alias lformat {
	@ IRCII.word = [$1-]
	if (@IRCII.word < [$0]) 
		{ @ function_return = [$([$0]IRCII.word)] } 
		{ @ function_return = IRCII.word } 
}

# Center text within a given width.   center(width text)
# "$center(10 one)"   returns "   one"
# this might not make sense at first, but it saves alot of cursor travel
# not writing all the spaces on the right side.
alias center {
	@ IRCII.word = [$1-]
	@ IRCII.wordl = @IRCII.word
	@ IRCII.width = [$0]
	if (IRCII.wordl > IRCII.width)
		{ @ function_return = ircII.word }
		{ @ function_return = [$([${(IRCII.width - ircII.wordl)/2}])] ## IRCII.word }
}

# This is the huge beastly CPU expensive search and replace funnction 
# written entirely in ircII script language.
# $sandr(search pat/replace pat/words)
# the search and replace patterns can contain spaces or any other chars
# with the exeption of '/'.
alias sandr {
	@ sr.src = left($index(/ $*) $*)
	@ sr.rep = mid(${index(/ $*) +1} ${rindex(/ $*) - index(/ $*) +1} $*)
	@ sr.word = mid(${rindex(/ $*) + 1} 512 $*)
	@ sr.srcl = @sr.src
	@ sr.wordl = @sr.word
	@ sr.cnt1 = 0
	@ sr.cnt2 = 0
	@ sr.lmark = 0
	@ sr.gotit = 0
	^assign -sr.fstring
	while (sr.cnt1 < sr.wordl)
	{
		@ sr.scnt1 = sr.cnt1
		while ((mid($sr.cnt1 1 $sr.word) == mid($sr.cnt2 1 $sr.src)) && (sr.cnt2 < sr.srcl))
		{
			if (sr.cnt2 == sr.srcl - 1)
			{
				@ sr.gotit = 1
			}
			@ sr.cnt1 = sr.cnt1 + 1
			@ sr.cnt2 = sr.cnt2 + 1
		}
		@ sr.cnt2 = 0
		if (sr.gotit)
		{
			@ sr.fstring = sr.fstring ## mid($sr.lmark ${sr.scnt1 - sr.lmark} $sr.word) ## sr.rep
			@ sr.gotit = 0
			@ sr.lmark = sr.cnt1
		}
		{
			@ sr.cnt1 = sr.cnt1 +1
		}
	}
	@ sr.fstring = sr.fstring ## mid($sr.lmark 512 $sr.word)
	@ function_return = sr.fstring
}

# The perfect complement to the $word() function.  
# $notword(index words)  returns words minus the indexed word. 
# the special handling of nw.sep is to deal with the cases when 
# the index points to the first or last word.
alias notword {
    if ([$0] > 0)
    {
	if (([$0] > 1) && ([$0] < rmatch($~ $1-)))
		{ @ nw.sep = [ ] }
		{ @ nw.sep = [] }
		
	@ function_return = [$(1-${[$0]-1})] ## nw.sep ## [$(${[$0]+1}-)]
    }d meta2-3 parse fk.key 3
bind meta2-4 parse fk.key 4
bind meta2-5 parse fk.key 5
bind meta2-6 parse fk.key 6
bind meta2-7 parse fk.key 7
bind meta2-8 parse fk.key 8
bind meta2-9 parse fk.key 9
bind meta2-0 parse fk.key 0
bind meta2-; parse fk.key .
bind meta2-A parse fk.key A;parsekey meta2_character;type ~
bind meta2-B parse fk.key B;parsekey meta2_character;type ~
bind meta2-C parse fk.key C;parsekey meta2_character;type ~
bind meta2-D parse fk.key D;parsekey meta2_character;type ~
bind meta2-E parse fk.key E;parsekey meta2_character;type ~
bind meta2-F parse fk.key F;parsekey meta2_character;type ~
bind meta2-G parse fk.key G;parsekey meta2_character;type ~
bind meta2-H parse fk.key H;parsekey meta2_character;type ~
bind meta2-I parse fk.key I;parsekey meta2_character;type ~
bind meta2-J parse fk.key J;parsekey meta2_character;type ~
bind meta2-K parse fk.key K;parsekey meta2_character;type ~
bind meta2-L parse fk.key L;parsekey meta2_character;type ~
bind meta2-M parse fk.key M;parsekey meta2_character;type ~
bind meta2-N parse fk.key N;parsekey meta2_character;type ~
bind meta2-O parse fk.key O;parsekey meta2_character;type ~
bind meta2-P parse fk.key P;parsekey meta2_character;type ~
bind meta2-Q parse fk.key Q;parsekey meta2_character;type ~
bind meta2-R parse fk.key R;parsekey meta2_character;type ~
bind meta2-S parse fk.key S;parsekey meta2_character;type ~
bind meta2-T parse fk.key T;parsekey meta2_character;type ~
bind meta2-U parse fk.key U;parsekey meta2_character;type ~
bind meta2-V parse fk.key V;parsekey meta2_character;type ~
bind meta2-W parse fk.key W;parsekey meta2_character;type ~
bind meta2-X parse fk.key X;parsekey meta2_character;type ~
bind meta2-Y parse fk.key Y;parsekey meta2_character;type ~
bind meta2-Z parse fk.key Z;parsekey meta2_character;type ~
bind meta2-~ nothing

alias fk.key {
	^assign fk.key $(fk.key)$0
	if ([$(fk.$(fk.key))]) {
	    ^bind meta2-~ $(fk.$(fk.key))
	}{
	    ^bind me