/* ================================================= */
/* Glossary.  Opens a document and prompts for pairs */
/* of values, the first being the search value and   */
/* the second being the replace value.  Sequentially */
/* finds and replaces every value pair as a batch.   */
/*                                                   */
/* FinalWriter macro                                 */
/* written by Merrill Callaway                       */
/* $VER: Glossary 1.0 copyright 1994 by              */
/* Merrill Callaway                                  */
/* ================================================= */
OPTIONS RESULTS

/* Get selected strings in a loop. */
/* If none, prompt for one */

Status PortName
oldport=RESULT

request=GETCLIP('MakeGlossaryFlagRequest')

IF request~=1 THEN DO
   /* make glossary or use existing one? */
   ShowMessage 1 1 '"Do you want to make a glossary ",
       "or load/use an existing one?" "",
       "Make it" "Load it" "Cancel"'
   IF RESULT=2 THEN CALL LoadGlossary
   IF RESULT=3 THEN EXIT 0
   END


/* default is to make glossary */
/* Make a Glossary from selected or typed text. */

MakeGlossary:
OPTIONS RESULTS
CALL SETCLIP('MakeGlossaryFlagRequest',1)
/* Establish count of how many strings have been selected. */
n=GETCLIP('FWstrcount')
IF DATATYPE(n,W) THEN n=n+1;ELSE n=1
CALL SETCLIP('FWstrcount',n)

COPY
IF RC=10 THEN DO
   ShowMessage 1 1 '"Find string not selected.",
   "Do you want to enter one?" "",
   "OK" "Make Glossary" "Cancel"'
   SELECT

   WHEN RESULT=1 THEN DO
      RequestText '"Find String" "Type in FIND string." ""'
      findstring=RESULT
      CALL SETCLIP('FWfindstr.'n,findstring)
      RequestText '"Replace String",
      "Type in REPLACE string."' findstring
      repstring=RESULT
      CALL SETCLIP('FWrepstr.'n,repstring)
      EXIT 0
      END

   WHEN RESULT=2 THEN DO
      newportname=NEWDoc.rexx
      ADDRESS VALUE newportname

      DO k=1 TO n-1
         findstr.k='FINDSTRING 'k':'GETCLIP('FWfindstr.'k)||,
         'a'x||'REPLSTRING 'k':'GETCLIP('FWrepstr.'k)||'a'x
         type findstr.k
         END k

      /* do find and replace or edit glossary here */
      ShowMessage 1 1 '"Glossary List.",
         "Do you want to batch replace?" "",
         "OK" "Edit Glossary" "Cancel"'

      SELECT
      WHEN RESULT=1 THEN CALL SearchAndReplace

      WHEN RESULT=2 THEN CALL EditGlossary

      OTHERWISE NOP /* cancel go to cleanup and exit */
      END /* select */

      /* clean up clip list and close doc */
      CALL GlossaryCleanUp
      CALL CloseDown 1
      EXIT 0
      END /* OUTER result=2 */

   OTHERWISE DO
      CALL GlossaryCleanUp
      CALL CloseDown 0
      EXIT 0
      END

   END /* select block */

   END /* if block */

/* If you have selected a string */
Extract
findstring=RESULT
findstring=STRIP(findstring,T,'a'x)
CALL SETCLIP('FWfindstr.'n,findstring)
RequestText '"SELECTED FIND String",
   "Type in REPLACE string."' findstring
repstring=RESULT
CALL SETCLIP('FWrepstr.'n,repstring)

EXIT 0 /* MakeGlossary */


/* search and replace findstring with replstring in orig. doc. */
SearchAndReplace:
OPTIONS RESULTS
ADDRESS VALUE oldport
WinToFront
DO r=1 TO n-1
   /* move to top of document */
   CALL MoveSOF
   findstring=GETCLIP('FWfindstr.'r)
   replstring=GETCLIP('FWrepstr.'r)

   IF r=1 THEN Find
   Find findstring

   DO FOREVER
      Cut
      Type replstring
      FindNext
      IF RC~=0 THEN LEAVE
      END /* do forever */

   END r
RETURN


/* edit the Glossary */
EditGlossary:
OPTIONS RESULTS

text1="Find/Replace String"
text2="Type Find/Repl No. 1 - "n-1".  0=Done."

text3="Find String"
text4="Edit Find String"

text5="Replace String"
text6="Edit Replace String"

/* watch the quotes! */
DO FOREVER
   RequestText '"'text1'"' '"'text2'"' 1
   frnum=RESULT

   /* error/input control */
   IF frnum=0 THEN LEAVE
   IF ~DATATYPE(frnum,W) THEN SIGNAL EditGlossary
   IF frnum>n-1 THEN SIGNAL EditGlossary

   oldfind=GETCLIP('FWfindstr.'frnum)
   RequestText '"'text3'"' '"'text4'"' oldfind
   newfind=RESULT
   CALL SETCLIP('FWfindstr.'frnum,newfind)
   CALL MoveSOF
   Find 'FINDSTRING 'frnum
   Find oldfind
   Cut
   Type newfind


   oldrepl=GETCLIP('FWrepstr.'frnum)
   RequestText '"'text5'"' '"'text6'"' oldrepl
   newrep=RESULT
   CALL SETCLIP('FWrepstr.'frnum,newrep)
   CALL MoveSOF
   Find 'REPLSTRING 'frnum
   Find oldrepl
   Cut
   Type newrep

   END /* do forever */

ShowMessage 1 1 '"Batch replace?" "" "",
   "OK" "Edit Glossary" "Abort"'
IF RESULT=1 THEN CALL SearchAndReplace
IF RESULT=2 THEN SIGNAL EditGlossary
/* Abort just return */
RETURN


/* clean up all clip list variables used */
GlossaryCleanUp:
OPTIONS RESULTS
count=GETCLIP('FWstrcount')
DO k=1 TO count
CALL SETCLIP('FWfindstr.'k)
CALL SETCLIP('FWrepstr.'k)
END
CALL SETCLIP('FWstrcount')
RETURN


/* close down glossary document */
CloseDown:
OPTIONS RESULTS
ARG file
IF file THEN DO
   ADDRESS VALUE newportname
   WinToFront
   Close
   END
ADDRESS VALUE oldport
WinToFront
CALL MoveSOF
CALL SETCLIP('MakeGlossaryFlagRequest')
RETURN

/* movesof */
MoveSOF:
CtrlDown
AltDown
Cursor UP
CtrlUp
AltUp
RETURN

/* Load a Glossary from a file */
LoadGlossary:

OPTIONS RESULTS
CALL SETCLIP('MakeGlossaryFlagRequest',1)
newportname=OPENDoc.rexx
ADDRESS VALUE newportname
WinToFront
SelectAll
Extract
glosstext=RESULT

IF LEFT(glosstext,13)~=='FINDSTRING 1:' THEN DO
   ShowMessage 1 1 '"File is not in",
      "glossary format." "" "OK" "" ""'
   IF RESULT=1 THEN DO
      CALL CloseDown 1
      EXIT 0
      END
   END


g=1
DO WHILE glosstext ~= ''
   pattern1='FINDSTRING 'g':'
   pattern2='REPLSTRING 'g':'
   cr='a'x
   PARSE VAR glosstext,
       (pattern1) findstr.g (cr),
       (pattern2) replstr.g (cr),
       glosstext
   g=g+1
   END
g=g-2


/* put results into clip list */
DO h=1 TO g
   CALL SETCLIP('FWfindstr.'h, findstr.h)
   CALL SETCLIP('FWrepstr.'h, replstr.h)
   END

n=g+1
CALL SETCLIP('FWstrcount',n)
ShowMessage 1 1 '"OK to Find",
    "and Replace all strings?" "",
    "OK" "Abort" ""'
IF RESULT=1 THEN CALL SearchAndReplace
CALL GlossaryCleanUp
CALL CloseDown 1
EXIT 0

