home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / thesrc15.zip / append.the next >
Text File  |  1993-12-08  |  6KB  |  125 lines

  1. /*
  2. $Header: C:\THE\RCS\append.the 1.4 1993/09/01 16:27:20 MH Interim MH $
  3. */
  4. /***********************************************************************/
  5. /* Description: REXX macro to append a string to a line.               */
  6. /* Syntax:      append target string                                   */
  7. /* Notes:       This macro appends the supplied string to the lines    */
  8. /*              specified in the target.                               */
  9. /*              Full XEDIT/KEDIT/THE targets are supported.            */
  10. /***********************************************************************/
  11. trace o
  12. args = Arg(1)
  13. argtype.0 = 2
  14. argtype.1 = 'TARGET'
  15. argtype.2 = 'STRING'
  16. noargs = parseargs(args)
  17. If noargs < 2 Then Do                               /* no args - error */
  18.    'CMSG append' args                             /* redisplay command */
  19.    'EMSG' errstr                                   /* say its an error */
  20.    Exit                                              /* go back to THE */
  21. End
  22. forward = 1                  /* assume direction is forward by defualt */
  23. 'EXTRACT /LINE/TOF/EOF/SIZE/STAY/FTYPE/FNAME/'    /* get various stuff */
  24. current_line = line.1                   /* save current line for later */
  25. If tof.1 = 'ON' Then 'N'       /* if on 'top of file' move down 1 line */
  26. If eof.1 = 'ON' Then 'U'    /* if on 'bottom of file' move down 1 line */
  27. nolines = valid_target(arg.1)              /* validate supplied target */
  28. If nolines = 0 Then Do            /* invalid target or target no found */
  29.    'CMSG append' args                             /* redisplay command */
  30.    'EMSG Invalid target specified:' arg.1          /* say its an error */
  31.    ':'||current_line                           /* restore current line */
  32.    Exit                                              /* go back to THE */
  33. End
  34. If nolines = 'ALL' Then Do                         /* if target is ALL */
  35.    ':1'                                              /* move to line 1 */
  36.    nolines = size.1                  /* nolines to act on - whole file */
  37. End
  38. If nolines < 0 Then Do                /* if target before current line */
  39.    forward = 0                    /* indicate direction to be backward */
  40.    nolines = nolines * -1                     /* make nolines positive */
  41. End
  42. totlines = 0                             /* reset changed line counter */
  43. Do nolines                              /* for each line to target ... */
  44.    'EXTRACT/CURLINE/'                     /* get current line contents */
  45.    'REPLACE' curline.3||arg.2
  46.    totlines = totlines + 1
  47.    If forward = 1 Then 'N'         /* if going forward, get next line */
  48.    Else 'U'                  /* if going backwards, get previous line */
  49.    If rc \= 0 Then Leave                        /* shouldn't get here */
  50. End
  51. 'EMSG' "'"||arg.2||"'" 'appended to' totlines 'lines' /* say how many lines changed */
  52. If stay.1 = 'ON' Then ':'||current_line 
  53. Return                                              /* go back to THE */
  54.  
  55. /***********************************************************************/
  56. /* Parses a string into types of arguments.                            */
  57. /* Allowable types of arguments are:                                   */
  58. /*     STRING   - any string                                           */
  59. /*     TARGET   - any valid THE target                                 */
  60. /* Returns number of arguments or -1 if an error                       */
  61. /* Usage:                                                              */
  62. /*       Set argtype.i with the type of argument expected              */
  63. /*       Set argtype.0 with the number of arguments expected           */
  64. /* Return:                                                             */
  65. /*       Sets arg.i to each argument                                   */
  66. /***********************************************************************/
  67. parseargs: Procedure Expose arg. argtype. errstr
  68. Parse Arg args
  69. noargs = 0
  70. Do i = 1 To argtype.0
  71.    Select
  72.      When argtype.i = 'STRING' Then Do
  73.           arg.i = args
  74.           noargs = noargs + 1
  75.           End
  76.      When argtype.i = 'TARGET' Then Do
  77.           args = Strip(args,'L')
  78.           word1 = Word(args,1)
  79.           Select
  80.             When Datatype(word1,'NUM') Then Do
  81.                  arg.i = word1
  82.                  args = Substr(args,Length(arg.i)+2)
  83.                  noargs = noargs + 1
  84.                  End
  85.             When Substr(args,1,1) = ':' | Substr(args,1,1) = ';' Then Do
  86.                  If Datatype(Substr(word1,2),'NUM') Then Do
  87.                     arg.i = word1
  88.                     args = Substr(args,Length(arg.i)+2)
  89.                     noargs = noargs + 1
  90.                     End
  91.                  Else Do
  92.                     errstr = 'Invalid target specified' word1
  93.                     Return -1
  94.                     End
  95.                  End
  96.             When Translate(word1) = 'ALL' | word1 = '*' | word1 = '-*' Then Do
  97.                  arg.i = word1
  98.                  args = Substr(args,Length(word1)+2)
  99.                  noargs = noargs + 1
  100.                  End
  101.             Otherwise Do
  102.                  delim = Substr(args,1,1)
  103.                  If delim \= '/' & delim \= '\' & delim \= '@' Then Do
  104.                     errstr = 'Invalid string target delimiter specified' delim
  105.                     Return -1
  106.                     End
  107.                  pos = Pos(delim,Substr(args,2))
  108.                  If pos = 0 Then Do
  109.                     errstr = 'No terminating string target delimiter specified' delim
  110.                     Return -1
  111.                     End
  112.                  arg.i = Substr(args,1,pos)
  113.                  args = Substr(args,pos+3)
  114.                  noargs = noargs + 1
  115.                  End
  116.           End
  117.      End
  118.      Otherwise Do
  119.         errstr = 'invalid argument type' argtype.i
  120.         Return -1
  121.         End
  122.    End
  123. End
  124. Return noargs
  125.