/*
$Id: append.the,v 1.2 2013/11/08 06:47:09 mark Exp $
*/
/***********************************************************************/
/* Description: REXX macro to append a string to a line.               */
/* Syntax:      append target string                                   */
/* Notes:       This macro appends the supplied string to the lines    */
/*              specified in the target.                               */
/*              Full XEDIT/KEDIT/THE targets are supported.            */
/*              A single character delimits the beginning of the string*/
/*              to be appended.                                        */
/*              e.g. append /fred/ | /bob/ xyz                         */
/*                                       ^ 1 space                     */
/*                  will append 'xyz' to the end of each line          */
/*              e.g. append /fred/ | /bob/  xyz                        */
/*                                       ^^ 2 spaces                   */
/*                  will append ' xyz' to the end of each line         */
/*              e.g. append /fred/ | /bob/xyz                          */
/*                                      ^ no spaces                    */
/*                  will append 'yz' to the end of each line           */
/***********************************************************************/
Trace o
arg1 = Arg(1)
noargs = Arg()
If noargs = 0 Then arg1 = '1'               /* no args - assume 1 line */
forward = 1                  /* assume direction is forward by default */
'EXTRACT /LINE/SIZE/STAY/FTYPE/FNAME/'            /* get various stuff */
current_line = line.1                   /* save current line for later */
reply = valid_target(arg1,spare)           /* validate supplied target */
If reply = 'ERROR' Then
   Do
     'EMSG Error: 17 Invalid target' arg1
     Exit
   End
If reply = 'NOTFOUND' Then
   Do
     'EMSG Error: 17 Target not found' arg1
     Exit
   End
'preserve'
start_line = Word(reply,1)                        /* get starting line */
nolines = Word(reply,2)                         /* get number of lines */
start_string = Wordindex(reply,2) + Wordlength(reply,2) + 2
string = Substr(reply,start_string) /* rest of argument is append string */
If nolines < 0 Then Do                /* if target before current line */
   forward = 0                    /* indicate direction to be backward */
   nolines = nolines * -1                     /* make nolines positive */
End
':'||start_line                                    /* go to first line */
totlines = 0                             /* reset changed line counter */
Do nolines                              /* for each line to target ... */
   'EXTRACT /CURLINE/TOF/EOF/'       /* get current line contents, etc.*/
   If tof.1 = 'ON',                    /* ignore line if on TOF or EOF */
   |  eof.1 = 'ON' Then nop
   Else
     Do
       'REPLACE' curline.3||string
       totlines = totlines + 1
     End
   If forward = 1 Then 'N'          /* if going forward, get next line */
   Else 'U'                   /* if going backwards, get previous line */
   If rc \= 0 Then Leave                         /* shouldn't get here */
End
'EMSG' "'"||string||"'" 'appended to' totlines 'lines' /* say how many lines changed */
If stay.1 = 'ON' Then ':'||current_line
'restore'
Return                                               /* go back to THE */
