!       filename FOOTSTRT      11/06/93
!       The following variables are used in the macros and are declared here.
VAR placeMarker
VAR tagPageNumber
VAR framePageNumber
VAR count
VAR message
VAR currentFrameName
VAR preserveStylePalette 
VAR holdFrameName
VAR oops
VAR oopsMessage
VAR addedFootnotes
VAR previousFootnoteCount
VAR currentFootnoteCount
VAR charAtCursor
VAR preserveStatus
VAR adjustMessage
VAR upDownMessage
VAR misplacedMessage
VAR problemCount
VAR lors
VAR loopCount
VAR endMessage
VAR spacingIncrement
VAR leadingIncrement
VAR spacePoints
VAR leadPoints
VAR newFootnote
VAR newFootnoteMessage
VAR forceFootnoteRedo
VAR footnoteNumber
VAR readCharAtCursor
VAR startingFrameName
VAR nothingToDo
VAR describeSubdir
VAR macroSubdir
VAR footnotePath
PROCEDURE QuitMacro
        IF preserveStylePalette = "not visible" THEN                   ! tests to see if the Style Palette was originally turned off 
                StylePaletteVisible                                               ! if it was, this COMMAND toggles it back to OFF
        ELSE
        END IF
        IF preserveStatus = "not visble" THEN                            ! tests to see if the status line was originally turned off
                SET DocPrefShowStatus TO FALSE                ! if it was, it turns it back off.
        ELSE
        END IF
        SET SelectedFrame TO currentFrameName
        SET CursorPosition TO placeMarker
        SET StatusMessage TO "All footnotes have been created"
        IF c = "manual end" THEN
                SET endMessage TO "REMINDER: Number of misplaced footnotes = "
                CONVERT problemCount TOSTRING problemCount
                SET endMessage TO endMessage + problemCount
                MSG endMessage
        ELSE
                IF nothingToDo = "on" THEN
                        MSG "There was nothing to do, all footnote frames have been removed"
                ELSE
                        IF loopCount = 1 THEN
                                MSG "The footnotes were created successfully the first time"
                        ELSE
                                CONVERT loopCount TOSTRING loopCount
                                SET endMessage  TO "The footnotes were created in " + loopCount
                                SET endMessage TO endMessage + " tries"
                                IF c = "manual end" THEN
                                ELSE
                                        MSG endMessage
                                END IF
                        END IF
                END IF
        END IF
QUIT
END PROCEDURE

PROCEDURE DeleteFootnoteFrames
        SET holdFrameName TO FrameName
        SET SelectedFrame TO "footnote data"
        CursorHome
        REPEAT
                DefineToWordEnd
                CopyToClipboard
                SET x TO PastedClipboard
                CancelDefinedBlock
                IF x = "0" THEN
                        EXIT 
                ELSE
                        IF x = "01" THEN
                                EXIT
                        ELSE
                                SET y TO "footnote" + x
                                GoToLinkTag y
                                DeleteFrame
                                SET SelectedFrame TO "footnote data"
                                TableCellTab
                                TableCellTab
                                DefineToWordEnd
                                CopyToClipboard
                                SET x TO PastedClipboard
                                CancelDefinedBlock
                                DeleteLinkTag x
                                TableCellTab
                                TableCellTab
                                TableCellTab
                        END IF
                END IF
        END REPEAT
        CancelDefinedBlock
        SET SelectedFrame TO "footnote data"
        DeleteFrame
        DeleteLinkTag "footnote data"
        SET SelectedFrame TO holdFrameName
        SET forceFootnoteRedo TO "off"
END PROCEDURE

PROCEDURE CheckIfOnSamePage
        SET Env1 TO "null"
        SET x TO 1
        SET misplacedMessage TO ""
        SET problemCount TO 0
        SET StatusMessage TO "Checking if footnotes are on the correct page........"
        SET holdFrameName TO FrameName
        SET SelectedFrame TO "footnote data"
        CursorHome
        REPEAT
                SET holdFrameName TO FrameName
                SET SelectedFrame TO "footnote data"
                DefineToWordEnd
                CopyToClipboard
                SET z TO PastedClipboard
                CONVERT z TOSTRING z
                CancelDefinedBlock
                TableCellTab
                TableCellTab
                DefineToWordEnd
                CopyToClipboard
                CancelDefinedBlock
                TableCellTab
                TableCellTab
                TableCellTab
                SET x TO PastedClipboard
                CONVERT x TOSTRING x
                SET LinkTagName TO x
                SET SelectedFrame TO holdFrameName
                IF LinkTagExists THEN
                        GoToLinkTag x
                        SET tagPageNumber TO CursorPageNumber
                        SET SelectedFrame TO x
                        SET framePageNumber TO CursorPageNumber
                        CONVERT x TOVALUE x
                        IF framePageNumber = tagPageNumber THEN
                        ELSE
                                SET problemCount TO problemCount + 1
                                SET misplacedMessage To misplacedMessage + " "
                                SET misplacedMessage TO misplacedMessage + z
                                SET Env1 TO "retry"                                             
                                        IF count = 1 THEN
                                                SET StatusMessage TO "This footnote is on the wrong page. This can be corrected."
                                                PAUSE 1
                                        ELSE
                                        END IF
                        END IF
                ELSE
                END IF
                CONVERT x TOVALUE x
                EXIT WHEN x = 2100
                EXIT WHEN x = 0
                SET x TO x + 1
        END REPEAT
        IF problemCount = 1 THEN
                SET misplacedMessage TO "Footnote"  + misplacedMessage
                SET misplacedMessage TO misplacedMessage + " is on the wrong page."
        ELSE
                SET misplacedMessage TO "Footnotes" + misplacedMessage
                SET misplacedmessage TO misplacedMessage + " are on the wrong pages."
        END IF
        SET SelectedFrame TO "footnote data"
        CursorHome
END PROCEDURE

PROCEDURE CheckIfOnSamePageInitial
        SET oops TO "off"
        SET x TO 1
        SET holdFrameName TO FrameName
        SET SelectedFrame TO "footnote data"
        CursorHome
        SET SelectedFrame TO holdFrameName
        REPEAT
                SET StatusMessage TO "Checking if footnotes are on the correct page........"
                SET holdFrameName TO FrameName
                SET SelectedFrame TO "footnote data"
                DefineToWordEnd
                CopyToClipboard
                CancelDefinedBlock
                SET m TO PastedClipboard
                TableCellTab
                TableCellTab
                DefineToWordEnd
                CopyToClipboard
                CancelDefinedBlock
                TableCellTab
                TableCellTab
                TableCellTab
                SET x TO PastedClipboard
                EXIT WHEN x = "0"
                CONVERT x TOSTRING x
                SET LinkTagName TO x
                SET SelectedFrame TO holdFrameName
                IF LinkTagExists THEN
                        GoToLinkTag x
                        SET tagPageNumber TO CursorPageNumber
                        SET SelectedFrame TO x
                        SET framePageNumber TO CursorPageNumber
                        CONVERT x TOVALUE x
                        IF framePageNumber < tagPageNumber THEN
                                SET m TO "Footnote " + m
                                SET m TO m + " on Page "
                                CONVERT framePageNumber TOSTRING framePageNumber
                                SET m TO m + framePageNumber
                                SET m TO m + " is before its reference number on Page "
                                CONVERT tagPageNumber TOSTRING tagPageNumber
                                SET m TO m + tagPageNumber
                                SET m TO m + ".        This can be corrected."
                                SET oops TO "on"
                                SET StatusMessage TO "This footnote is on the page preceding its reference. This can be corrected."
                                MSG m
                        ELSE
                                IF framePageNumber > tagPageNumber THEN
                                        SET m TO "Footnote " + m
                                        SET m TO m + " on Page "
                                        CONVERT framePageNumber TOSTRING framePageNumber
                                        SET m TO m + framePageNumber
                                        SET m TO m + " is after its reference number on Page "
                                        CONVERT tagPageNumber TOSTRING tagPageNumber
                                        SET m TO m + tagPageNumber
                                        SET m TO m + ".        This can be corrected."
                                        SET oops TO "on"
                                        SET StatusMessage TO "This footnote is on the page preceding its reference. This can be corrected."
                                        MSG m
                                ELSE
                                END IF
                        END IF
                ELSE
                        EXIT
                END IF
                CONVERT x TOVALUE x
                EXIT WHEN x = 2100
                EXIT WHEN x = 0
                SET x TO x + 1
        END REPEAT
        SET SelectedFrame TO holdFrameName
        IF oops = "on" THEN
        ELSE
                QUERY "All previous footnotes are correctly placed. Click OK if you've added any or want to continue anyway." TO oopsMessage
                IF oopsMessage  THEN
                SET forceFootnoteRedo TO "on"
                ELSE
                        SET SelectedFrame TO currentFrameName
                        SET CursorPosition TO placeMarker
                        CALL QuitMacro
                END IF
        END IF
END PROCEDURE

PROCEDURE CheckPreviousNumberOfFootnotes
        SET currentFrameName TO FrameName
        SET SelectedFrame TO "footnote data"
        CursorHome
        SearchClear
        SET SearchSearch1 TO "0"
        SET SearchWholeWords1 TO TRUE
        SearchForward
        SET x TO TableCurrentRow
        SET previousFootnoteCount TO x
        SET SelectedFrame TO currentFrameName
        SET x TO 1
        REPEAT
                IF AttributeUnbreakable THEN
                        SET x TO x - 1
                ELSE
                END IF
                ParagraphDown
                EXIT WHEN EndOfFrame
                SET x TO x + 1
                SET currentFootnoteCount TO x
        END REPEAT
        SET addedFootnotes TO currentFootnoteCount - previousFootnoteCount
        IF addedFootnotes = 0 THEN
        ELSE
                IF addedFootnotes =1 THEN
                        MSG "You have added one footnote. You should skip the Initial Check phase"
                ELSE
                END IF
                IF addedFootnotes =2 THEN
                        MSG "You have added two footnotes. You should skip the Initial Check phase"
                ELSE
                END IF
                IF addedFootnotes =3 THEN
                        MSG "You have added three footnotes. You should skip the Initial Check phase"
                ELSE
                END IF
                IF addedFootnotes =4 THEN
                        MSG "You have added four footnotes You should skip the Initial Check phase"
                ELSE
                END IF
                IF addedFootnotes =5 THEN
                        MSG "You have added five footnotes. You should skip the Initial Check phase"
                ELSE
                END IF
                IF addedFootnotes >5 THEN
                        MSG "You have added more than five footnotes. You should skip the Initial Check phase"
                ELSE
                END IF
        END IF
        IF addedFootnotes = 0 THEN
        ELSE
                IF addedFootnotes < 0 THEN
                        SET addedFootnotes TO 0 - addedFootnotes
                        IF addedFootnotes = 1 THEN
                                MSG "You have deleted one footnote. You should skip the Initial Check Phase when you 'Create Footnotes'"
                        ELSE
                        END IF
                        IF addedFootnotes = 2 THEN
                                MSG "You have deleted two footnotes. You should skip the Initial Check Phase when you 'Create Footnotes'"
                        ELSE
                        END IF
                        IF addedFootnotes = 3 THEN
                                MSG "You have deleted three footnotes. You should skip the Initial Check Phase when you 'Create Footnotes'"
                        ELSE
                        END IF
                        IF addedFootnotes = 4 THEN
                                MSG "You have deleted four footnotes You should skip the Initial Check Phase when you 'Create Footnotes'"
                        ELSE
                        END IF
                        IF addedFootnotes = 5 THEN
                                MSG "You have deleted five footnotes. You should skip the Initial Check Phase when you 'Create Footnotes'"
                        ELSE
                        END IF
                        IF addedFootnotes > 5 THEN
                                MSG "You have deleted more than five footnotes. You should skip the Initial Check Phase when you 'Create Footnotes'"
                        ELSE
                        END IF
                ELSE
                END IF
        END IF
        QUERY  "Do Initial Check?  (Click YES)   To skip this test choose No " TO message
        IF message  THEN
                IF addedFootnotes > 0 THEN
                        MSG "In the future you should skip this phase when you have added  any footnotes!"
                ELSE
                END IF  
                IF addedFootnotes < 0 THEN
                        MSG "In the future you should skip this phase when you have deleted any footnotes!"
                ELSE
                END IF
                CALL CheckIfOnSamePageInitial
        ELSE
        END IF
END PROCEDURE

PROCEDURE CreateFootnoteData
        SET currentFrameName TO FrameName
        FrameCreateStart
        SET FrameStartingPage TO CursorPageNumber
        SET FrameName TO "footnote data"
        SET FrameRightPageMarginRight TO 2
        SET FrameRightPageMarginTop TO 1
        SET FrameRightPageMarginLeft TO 2
        SET FrameRightPageMarginBottom TO 1
        SET FrameShouldDraw TO FALSE
        SET FrameShouldPrint TO FALSE
        SET FrameOpaque TO FALSE
        FrameProcessChanges
        TableCreateStart
        SET TableRows TO 1
        SET TableColumns TO 5
        DefineToDocumentEnd
        StyleCreateVariation
        AlignCentered
        StyleApplyVariation
        CancelDefinedBlock
        TableProcessChanges
        CursorHome
        CreateLinkTag "footnote data"
        SET SelectedFrame TO currentFrameName
END PROCEDURE

PROCEDURE GetDirections
        SET lors TO " "
        MSG misplacedMessage
        GET "Select Cancel if to fix this manually, or choose to adjust  (s)pacing or adjust (l)eading?" TOSTRING adjustMessage
        IF adjustMessage = "l" THEN
                GET "Increase Leading (+)  Decrease Leading (-)" TOSTRING upDownMessage
                IF upDownMessage = "+" THEN
                        GET "How many points?" TOVALUE leadingIncrement
                        GET " I will increase the leading in each footnote. How many times should I try?" TOVALUE count
                        IF count = 0 THEN
                                SET count TO 2
                        ELSE
                                SET count TO count + 1
                        END IF
                ELSE
                        IF upDownMessage = "-" THEN
                                GET "How many points?" TOVALUE leadingIncrement
                                GET " I will decrease the leading in each footnote. How many times should I try?" TOVALUE count
                                IF count = 0 THEN
                                        SET count TO 2
                                ELSE
                                        SET count TO count + 1
                                END IF
                        ELSE
                                SET StatusMessage TO "Proceed with the manual changes needed.....Good luck"
                                Pause 5
                                        !       MSG "Proceed with the manual changes necessary."
                                        SET c TO "manual end"
                        END IF
                END IF                 
        ELSE
                SET lors TO "l"
        END IF
        IF adjustMessage = "s" THEN
                GET "How many points should the spacing be increased?" TOVALUE spacingIncrement
                GET "I will increase the spacing between each footnote. How many times should I try?" TOVALUE count
                IF count = 0 THEN
                        SET count TO 2
                ELSE
                        SET count TO count + 1
                END IF
        ELSE
                SET lors TO lors + "s"
        END IF
END PROCEDURE


MACRO MasterFootnote
        CancelDefinedBlock        ! This is important, nasty things happen if you happen to have some text selected!
        GoToLinkTag "startpoint"
        CursorUp
        IF AttributeUnderline THEN
        ELSE
                PUT "*"
        END IF
        GoToLinkTag "startpoint"
        QUERY "You are about to convert to Footnotes.  All previous manual tweaking of footnotes will be lost! OK to proceed?" TO a
        IF a  THEN
        ELSE
                MSG "Canceling..... Back to Document"
                QUIT
        END IF
        IF NOT IsCharNumeric THEN
                MSG "Please make sure the cursor is right before the number of the first endnote. Try again!"
                QUIT
        ELSE
        END IF
        SET startingFrameName TO FrameName
        SET placeMarker TO CursorPosition
        SET Env5 TO 4pt
        ParagraphUp
        IF CursorCharacter = "*" THEN
                SET Env9 TO "*"
                DefineToLineEnd
                DefineLeft
                DeleteText
        ELSE
                SET Env6 TO StylePointSize
                SET Env7 TO UnderlineSize
                SET Env8 TO UnderlinePosition
                DefineToParagraphEnd
                CopyToClipboard
                SET Env9 TO PastedClipboard
                CancelDefinedBlock
        END IF
        SET CursorPosition TO placeMarker
        SET Env0 TO 0pt
        SET leadPoints TO 0pt
        SET spacePoints TO 0pt
        SET CursorPosition TO placeMarker
        SET LinkTagName TO "footnote data"
        IF LinkTagExists THEN
                CALL  CheckPreviousNumberOfFootnotes
        ELSE
        END IF
        SET CursorPosition TO placeMarker 
        IF DocPrefShowStatus THEN
                SET preserveStatus TO "visible"
        ELSE
                SET preserveStatus TO "not visible"
                SET DocPrefShowStatus TO TRUE
        END IF        
        IF IsStylePaletteVisible THEN
                SET preserveStylePalette TO "visible"
        ELSE
                SET preserveStylePalette TO "not visible"
                StylePaletteVisible
        END IF
        SET forceFootnoteRedo TO "off"
        SET nothingToDo TO "off"
        SET c TO "automatic repeat"
        SET currentFrameName TO SelectedFrame
        SET count TO 1
        SET Env1 TO "null"
        SET upDownMessage TO " "
        SET adjustMessage TO " "
        SET loopCount TO 0
                        !The following REPEAT is run only once, except it is repeated if you decide to correct  for misplaced footnote frames.
        REPEAT                                          !<<<<<<<<<<<<<<<<<<<<<<<<<<<repeat<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
                                !This is the macro that starts the footnote creation process. First though we have to put the cursor at  the 
                                ! beginning of the  first endnote, right before the number.
                SET loopCount TO loopCount + 1
                SET SelectedFrame TO startingFrameName
                CancelDefinedBlock
                GoToLinkTag "startpoint"
                REPEAT
                        IF AttributeUnbreakable THEN
                                ParagraphDown
                        ELSE
                                EXIT
                        END IF
                END REPEAT
                IF EndOfFrame THEN 
                SET LinkTagName TO "footnote data"
                        IF LinkTagExists THEN
                                CALL DeleteFootnoteFrames
                                SET nothingToDo TO "on"
                                CALL QuitMacro
                        ELSE
                        END IF
                ELSE
                END IF
                REPEAT
                        SET placeMarker TO CursorPosition 
                        CursorWordRight
                        PUT " "
                        CursorWordLeft
                        SET readCharAtCursor TO CursorCharacter
                        REPEAT
                                IF readCharAtCursor = " " THEN
                                        EXIT
                                ELSE
                                        DefineRight
                                        SET readCharAtCursor TO CursorCharacter
                                END IF
                        END REPEAT
                        CopyToClipboard
                        CancelDefinedBlock
                        DeleteText
                        SET footnoteNumber TO PastedClipboard
                        SET CursorPosition TO placeMarker
                        EndNoteJump
                        SET b TO CursorPageNumber
                        CONVERT b TOSTRING b
                        SET b TO footnoteNumber + b
                        SET LinkTagName TO b
                        IF LinkTagExists THEN
                                CALL DeleteFootnoteFrames
                                EXIT
                        ELSE
                                SET CursorPosition TO placeMarker
                                ParagraphDown
                        END IF
                        EXIT WHEN EndOfFrame
                END REPEAT
                IF forceFootnoteRedo = "on" THEN
                        CALL DeleteFootnoteFrames
                ELSE
                END IF
                SET SelectedFrame TO startingFrameName
                SET currentFrameName TO FrameName
                SET CursorPosition TO placeMarker
                SET LinkTagName TO "footnote data"
                IF LinkTagExists THEN
                ELSE
                        CALL  CreateFootnoteData
                END IF
                SET SelectedFrame TO currentFrameName
                GoToLinkTag "startpoint"
                ! This REPEAT is the one that steps through the endnote text block, converting them to footnotes.
                REPEAT                                  !<<<<<<<<<<<<<<<<<<<<repeat<<<<<<<<<<<<<<<<<<<<<<<<<
                ! SaveDocument   ! this is optional, but could be a good thing to do if you run the macros repeatedly
                                                          ! because the Snapshot file can possibly get very large and you can run out of disk
                                                          ! space, causing much grief and swearing and cussing.
                                                          ! MAKE A BACKUP BEFORE DOING FOOTNOTES!
                        REPEAT
                                IF AttributeUnbreakable THEN
                                        ParagraphDown
                                ELSE
                                        EXIT
                                END IF
                        END REPEAT
                        IF EndOfFrame THEN
                                SET StatusMessage TO "Finished processing last footnote....."
                                EXIT
                        ELSE
                                SET StatusMessage TO "Processing footnotes......."
                                SET describeSubdir TO ProgramDirectory
                                SET macroSubdir TO describeSubdir + "macros\"
                                SET footnotePath TO macroSubdir + "footnote"
                                PlayMacro footnotePath
                                CursorWordLeft
                                EndnoteJump
                                ParagraphDown
                                CancelDefinedBlock
                        END IF
                END REPEAT                              !>>>>>>>>>>>>>>>>>>>>>repeat>>>>>>>>>>>>>>>>>>>>>>>>
        SET StatusMessage TO "Done......." 
        SET holdFrameName TO FrameName
        SET SelectedFrame TO "footnote data"
        CancelDefinedBlock
        DefineToDocumentEnd
        DeleteText
        CancelDefinedBlock
        PUT "0"
        TableCellTab
        PUT "0"
        TableCellTab
        PUT "0"
        TableCellTab
        PUT "0"
        TableCellTab
        PUT "0"
        CursorHome
        SET SelectedFrame TO holdFrameName
        CALL CheckIfOnSamePage
        IF Env1 = "retry" THEN
                IF count = 1 THEN
                        CALL GetDirections
                        IF lors = "ls" THEN
                                MSG "You must enter a lower case  's'  or a 'l' in order to proceed with automatic error correction"  
                                CALL GetDirections
                                IF lors = "ls" THEN
                                        SET c TO "manual end"
                                ELSE
                                END IF 
                        ELSE
                        END IF
                ELSE
                END IF
                SET count TO count - 1
                IF adjustMessage = "l" THEN
                        IF upDownMessage = "+" THEN
                                SET v TO leadingIncrement
                                REPEAT
                                        SET leadPoints TO leadPoints + 1pt
                                        SET v TO v - 1
                                        EXIT WHEN v = 0
                                END REPEAT
                                SET Env0 TO Env0 - leadPoints
                        ELSE
                        END IF
                        IF upDownMessage = "-" THEN
                                SET v TO leadingIncrement
                                REPEAT
                                        SET leadPoints TO leadPoints + 1pt
                                        SET v TO v - 1
                                        EXIT WHEN v = 0
                                END REPEAT
                                SET Env0 TO Env0 + leadPoints
                        ELSE
                        END IF
                ELSE
                        IF adjustMessage = "s" THEN
                                SET v TO spacingIncrement
                                REPEAT
                                        SET spacePoints TO spacePoints + 1pt
                                        IF v = 0 THEN
                                        ELSE
                                        SET v TO v - 1
                                        END IF
                                        EXIT WHEN v = 0
                                END REPEAT
                                SET Env5 TO Env5 + spacePoints
                        ELSE
                        END IF
                END IF
        ELSE
        END IF
        EXIT WHEN count <= 0
        EXIT WHEN Env1 = "null"
        IF c = "manual end" THEN
                CALL QuitMacro
        ELSE
        END IF
        END REPEAT                                  !>>>>>>>>>>>>>>>>>>>>>>repeat>>>>>>>>>>>>>>>>>>>>>>>>
        SET StatusMessage TO "All footnotes have been created"
        IF c =  "manual end" THEN
                CALL QuitMacro
        ELSE
        END IF
CALL QuitMacro
END MACRO

