1# Some misc functions..  for 2.2 and beyond.
2# by: Ian Frechette  (frechett@spot.colorado.edu)
3# Wed Aug 18 00:26:53 MDT 1993
4@ RCS.functions = [$$Header: /home/cvs/ircii/script/functions,v 1.3 2001/08/12 15:44:17 mrg Exp $$]
5
6
7# ircII scripting note.  @ var = <expression> is used a lot. One case
8# where it is not feasible to use  the @ var = othervar or @ var = funct()
9# format is when the $(stuff) construct is needed.  In that case you
10# must surround it with [].  e.g.   @ blah = [$($0)]   will assign
11# to 'blah' the contents of the variable named in arg 0.
12
13#
14# reverse a string fed to it.  The first argument is the width of the
15# field in which to right justify the reversed text
16# $reverse(65 one two three) returns..
17#                                                          eerht owt eno
18# if 0 is given as the width it's reversed and output left justified
19alias reverse {
20	^assign -rev.result
21	@ rev.ind = 0
22	@ rev.str = [$1-]
23	while (mid($rev.ind 1 $rev.str)!=[])
24	{
25		@ rev.result = mid($rev.ind 1 $rev.str) ## rev.result
26		@ rev.ind = rev.ind + 1
27	}
28	# This right justifies to a field with of $0.
29	# strip out the format() function if you don't want it formatted.
30	@ function_return = format($0 $rev.result)
31}
32
33# These have been updated to handle multiple words.
34# format and lformat differ from $[-num]var and $[num]var in that
35# They don't chop off the string if it is too long.
36alias format {
37	@ IRCII.word = [$1-]
38	if (@IRCII.word < [$0])
39		{ @ function_return = [$([-$0]IRCII.word)] }
40		{ @ function_return = IRCII.word }
41}
42
43alias lformat {
44	@ IRCII.word = [$1-]
45	if (@IRCII.word < [$0])
46		{ @ function_return = [$([$0]IRCII.word)] }
47		{ @ function_return = IRCII.word }
48}
49
50# Center text within a given width.   center(width text)
51# "$center(10 one)"   returns "   one"
52# this might not make sense at first, but it saves alot of cursor travel
53# not writing all the spaces on the right side.
54alias center {
55	@ IRCII.word = [$1-]
56	@ IRCII.wordl = @IRCII.word
57	@ IRCII.width = [$0]
58	if (IRCII.wordl > IRCII.width)
59		{ @ function_return = ircII.word }
60		{ @ function_return = [$([${(IRCII.width - ircII.wordl)/2}])] ## IRCII.word }
61}
62
63# This is the huge beastly CPU expensive search and replace funnction
64# written entirely in ircII script language.
65# $sandr(search pat/replace pat/words)
66# the search and replace patterns can contain spaces or any other chars
67# with the exeption of '/'.
68alias sandr {
69	@ sr.src = left($index(/ $*) $*)
70	@ sr.rep = mid(${index(/ $*) +1} ${rindex(/ $*) - index(/ $*) +1} $*)
71	@ sr.word = mid(${rindex(/ $*) + 1} 512 $*)
72	@ sr.srcl = @sr.src
73	@ sr.wordl = @sr.word
74	@ sr.cnt1 = 0
75	@ sr.cnt2 = 0
76	@ sr.lmark = 0
77	@ sr.gotit = 0
78	^assign -sr.fstring
79	while (sr.cnt1 < sr.wordl)
80	{
81		@ sr.scnt1 = sr.cnt1
82		while ((mid($sr.cnt1 1 $sr.word) == mid($sr.cnt2 1 $sr.src)) && (sr.cnt2 < sr.srcl))
83		{
84			if (sr.cnt2 == sr.srcl - 1)
85			{
86				@ sr.gotit = 1
87			}
88			@ sr.cnt1 = sr.cnt1 + 1
89			@ sr.cnt2 = sr.cnt2 + 1
90		}
91		@ sr.cnt2 = 0
92		if (sr.gotit)
93		{
94			@ sr.fstring = sr.fstring ## mid($sr.lmark ${sr.scnt1 - sr.lmark} $sr.word) ## sr.rep
95			@ sr.gotit = 0
96			@ sr.lmark = sr.cnt1
97		}
98		{
99			@ sr.cnt1 = sr.cnt1 +1
100		}
101	}
102	@ sr.fstring = sr.fstring ## mid($sr.lmark 512 $sr.word)
103	@ function_return = sr.fstring
104}
105
106# The perfect complement to the $word() function.
107# $notword(index words)  returns words minus the indexed word.
108# the special handling of nw.sep is to deal with the cases when
109# the index points to the first or last word.
110alias notword {
111    if ([$0] > 0)
112    {
113	if (([$0] > 1) && ([$0] < rmatch($~ $1-)))
114		{ @ nw.sep = [ ] }
115		{ @ nw.sep = [] }
116
117	@ function_return = [$(1-${[$0]-1})] ## nw.sep ## [$(${[$0]+1}-)]
118    }
119    {
120        @ function_return = [$1-]
121    }
122    ^assign -nw.sep
123}
124
125# If you want to look an array.. Type /show <arrayname>
126# Lists keys and contents
127^alias show {
128  if ( [$($0)] )
129  {
130    echo $0 $($0)
131  }
132  foreach $0 ii
133  {
134    show $0.$ii
135  }
136  ^assign -ii
137}
138
139
140# push an item onto the head of a list
141# this only takes the name of the list instead of the variable itself.
142# examples.
143# /push list Item
144# or     if (push(list Item)) { echo push sucessful } { echo push failed }
145# echo $list returns 'Item'
146alias push {
147	if (![$1])
148	{ @function_return = 0 }
149	{ eval @ $0 = [$1- $($0)];@function_return = 1}
150}
151
152# pop an item off a list.  Specified with $pop(listname)
153# note there is no $ in front of listname.
154# examples.
155# /eval echo $pop(list)         returns 'Item' and the list is shortened
156# push List2 $pop(List1)        moves first item from List1 to List2
157alias pop {
158	@function_return = word(0 $($0))
159	eval @ $0 = notword(1 $($0))
160}
161
162# pluck a word from a list.
163# eg. $blah == "one two three four five"
164# $pluck(blah three)     returns "one two four five"
165alias pluck {
166	@ function_return = notword($match($1 $($0)) $($0))
167}
168
169alias remove {
170	@ rem.tmp = [$($0)]
171	while (rmatch($1 ${rem.tmp = pluck(rem.tmp $1)})) {#}
172	@ function_return = rem.tmp
173}
174
175
176# This alias sorts flat lists case insenstive
177# IT can be easily changed to sort case sensitive by removing the
178# $toupper() call.
179# operation..    $sort(list or words here)   will return a string with
180# the list of words sorted in ascending order.
181# to sort in reverse order
182alias sort {
183	@ sort.tmp = [$*]
184	while (sort.word = pop(sort.tmp)) {
185		eval @ sort.str.$encode($toupper($sort.word)) = sort.word
186	}
187	@ sort.sep = []
188	foreach sort.str ii {
189		# sort ascending
190		@ sort.tmp = sort.tmp ## sort.sep ## sort.str[$ii]
191		# sort decending
192		# push sort.tmp sort.str[$ii]
193		^assign -sort.str[$ii]
194		@ sort.sep = [ ]
195	}
196	@ function_return = sort.tmp
197	^assign -sort.sep
198	^assign -sort.tmp
199}
200