/* nag.rexx - translates and acts upon single words sent from NAGPLUS    */

/*      copyright 1989 Richard Lee Stockton and Gramma Software.         */
/*    This code is freely distributable as long as this copyright        */
/*   notice remains, unchanged, at the start of the code. Thank you.     */

/*      To activate from NAGPLUS, release RMB over the TEXT area         */
/*      of the event. The selected word of the event text will be        */
/*      used as the 'link' word.                                         */

cli       = 'NewCLI'
dir       = 'c:List'         /* 'list' or your favorite DIR type program */
dirfile   = 'ram:linkdir.temp'

OPTIONS RESULTS

IF(~EXISTS("libs:rexxsupport.library")|~EXISTS("libs:rexxarplib.library")) THEN
 DO
    EXIT "Couldn't find needed library (rexxsupport and/or rexxarplib)."
 END
CALL ADDLIB('rexxsupport.library',0,-30,0)
CALL ADDLIB('rexxarplib.library',0,-30,0)

CALL SCREENTOFRONT()    /* need arplib and rexxarplib for this */

return_text = ""

/* Editor argument from NAGPLUS. "editor notedir link portname configfile" */
PARSE ARG nagedarg

editor  = WORD(nagedarg,1)  /* 'hardwire' this if your editor needs args */
notedir = WORD(nagedarg,2)  /*  Example:  editor = "Util:dme -g -m437"   */
link    = WORD(nagedarg,3)

test    = UPPER(link)
rxprg   = test
IF RIGHT(rxprg,5)~='.REXX' THEN rxprg = rxprg'.rexx'

IF test=='CLI' THEN
  DO
    ADDRESS COMMAND cli    /* open a CLI/SHELL/WHATEVER */
  END

ELSE IF WORD(STATEF(link),1)=='DIR' THEN   /* if it's a DIR, show it */
   DO
      ADDRESS COMMAND dir '>'dirfile link  /* redirect output to file */
      CALL OPEN w,"CON:0/0/640/200/ ...Press 'RETURN' To Exit... "
      CALL OPEN f,dirfile,'R'
      dirstring = READLN(f)
      DO WHILE dirstring~=""
         CALL WRITELN(w,dirstring)
         dirstring = READLN(f)
      END
      CALL CLOSE(f)
      CALL READLN(w)         /* Wait for <RETURN> */
      CALL CLOSE(w)
      IF SHOW('L',"rexxarplib.library") THEN
        CALL SCREENTOBACK()    /* need arplib and rexxarplib for this */
      ADDRESS COMMAND 'c:delete' dirfile
   END

                                          /* do REXX program */
            /* use 'INTERPRET' so we can see any returned RESULTs */

ELSE IF WORD(STATEF('REXX:'rxprg),1)=="FILE" THEN
       DO
         INTERPRET('return_text='rxprg'()')
         IF SHOW('L',"rexxarplib.library") THEN
           CALL SCREENTOBACK()    /* need arplib and rexxarplib for this */
       END

ELSE  ADDRESS COMMAND editor notedir || link

EXIT return_text
