/*
$Id: total.the,v 1.2 2004/08/07 04:28:33 mark Exp $
*/
/***********************************************************************/
/* Description: REXX macro to sum numbers in a marked block.           */
/* Syntax:      total                                                  */
/* Notes:       This macro will sum the numbers in the marked block    */
/*              and insert a new line with the total at the end of the */
/*              block.                                                 */
/***********************************************************************/
Trace o
Numeric Digits 20    /* to make really sure that we don't get overflow */
'EXTRACT /BLOCK/FILENAME/FPATH/'
If block.0 = 1 Then Do
   'EMSG No Marked block'
   Return
   End
If block.1 \= 'BOX' & block.1 \= 'COLUMN' Then Do
   'EMSG No Marked BOX or COLUMN block'
   Return
   End
current_file = fpath.1||filename.1
If current_file \= block.6 Then Do
   'EMSG Marked block not in current file'
   Return
   End
Do i = block.2 To block.4
   'NOMSG :'||i
   If rc = 0 Then Leave
End
tot. = 0
Do Forever
   'EXTRACT /CURLINE/LINE/EOF'
   If line.1 > block.4 Then Leave
   If eof.1 = 'YES' Then Leave
   num = Substr(curline.3,block.3,block.5-block.3+1)
   Do i = 1 To Words(num)
      If Datatype(Word(num,i),'NUM') Then tot.i = tot.i + Word(num,i)
   End
   if Words(num) > tot.0 Then tot.0 = Words(num)
   'N'
End
'U'
line = ''
Do i = 1 To tot.0
   line = line tot.i
End
/*
 * Determine correct right alignment
 */
len = Length(line)
spaces = block.5-len
If spaces > 0 Then line = Copies(' ',spaces)line
'i' line
'N'
Return
