home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / ircii30.zip / script / functions < prev    next >
Text File  |  1994-07-30  |  6KB  |  200 lines

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