(DEFINE-FILE-INFO PACKAGE "INTERLISP" READTABLE "INTERLISP")
(FILECREATED "10-Feb-88 11:27:19" {QV}<NOTECARDS>1.3LNEXT>RHTPATCH309.;1 37878  

      changes to%:  (VARS RHTPATCH309COMS)
                    (FNS NC.MakeDocument NC.DumpExportableCardToDoc NC.DumpNoteCardToDoc 
                         NC.AskCrossFileLinkMode))


(* "
Copyright (c) 1988 by Xerox Corporation.  All rights reserved.
")

(PRETTYCOMPRINT RHTPATCH309COMS)

(RPAQQ RHTPATCH309COMS ((DECLARE%: DONTCOPY (PROPS (RHTPATCH309 MAKEFILE-ENVIRONMENT)
                                                   (RHTPATCH309 FILETYPE)))
                        [DECLARE%: FIRST (P (NC.LoadFileFromDirectories 'NCDOCUMENTCARD]
                        
          
          (* ;; "Fix for a bug in document cards caused by encountering into cross-file links.  Now only asks question about two-way crossfilelinks once per document card computation.  Also kept the prompt window for the doc card from closing after the questions about foreign notefile.")

                        
          
          (* ;; "Changes to NCDOCUMENTCARD")

                        (FNS NC.MakeDocument NC.DumpCardToDoc NC.DumpExportableCardToDoc 
                             NC.DumpFileBoxToDoc NC.DumpNoteCardToDoc)
                        
          
          (* ;; "Change to NCCROSSFILELINKS")

                        (FNS NC.AskCrossFileLinkMode)))
(DECLARE%: DONTCOPY 

(PUTPROPS RHTPATCH309 MAKEFILE-ENVIRONMENT (:PACKAGE "INTERLISP" :READTABLE "INTERLISP"))

(PUTPROPS RHTPATCH309 FILETYPE :TCOMPL)
)
(DECLARE%: FIRST 
(NC.LoadFileFromDirectories 'NCDOCUMENTCARD)
)



(* ;; 
"Fix for a bug in document cards caused by encountering into cross-file links.  Now only asks question about two-way crossfilelinks once per document card computation.  Also kept the prompt window for the doc card from closing after the questions about foreign notefile."
)




(* ;; "Changes to NCDOCUMENTCARD")

(DEFINEQ

(NC.MakeDocument
  [LAMBDA (Card Title NoDisplayFlg CardIdentifier)           (* ; "Edited 10-Feb-88 11:21 by Trigg")

(* ;;; "Called from a filebox's title bar.  Makes a document by smashing all the descendant cards's text together.  Ask user if wants numbered section headings and titles.  The former are made from FileBox titles, the latter from notecard titles.  Delete embedded links at the end if the user desires.")
          
          (* ;; "rht 10/22/84: Hacked to be callable from Programmer's interface.")
          
          (* ;; 
          "rht 11/17/84: Checks for cancel when choosing rootID and also when setting parameters.")
          
          (* ;; "rht 8/25/85: Now dumps sketch and graph cards as well as text cards.")
          
          (* ;; "rht 9/16/85: Now handles cr's around titles using para leading.")
          
          (* ;; "fgh 11/178/85 Updated to handle Card and NoteFile objects.")
          
          (* ;; "kirk 27Jun86 Moved NC.RetrieveTitle call so does not break when user Cancels")
          
          (* ;; "rht 7/31/86: Now checks for card types having ExportSubstanceFn prop.")
          
          (* ;; "kirk 8/22/86 Fix of free use of NoteFile var")
          
          (* ;; "rht 10/15/86: Integrated markM's changes and fixed box numbering.")
          
          (* ;; "rht 11/17/86: Now calls NC.ApplySupersFn rather than NC.MakeNoteCard.")
          
          (* ;; "pmi 12/5/86: Modified message to NC.SelectNoteCards to mention SHIFT-selection.")
          
          (* ;; 
          "pmi 12/12/86: Removed obsolete ReturnLinksFlg argument in call to NC.SelectNoteCards.")
          
          (* ;; "rg 3/16/87 NC.DeleteNoteCards -> NC.DeleteNoteCard")
          
          (* ;; 
   "rg 3/18/87 added NC.CardSelectionOperation wrapper.  Still needs ProtectedCardOperation wrapper!")
          
          (* ;; "rg 4/2/87 changed NC.CardSelectionOperation to NCP.WithLockedCards")
          
          (* ;; "rht 4/17/87: No longer bugs user for confirm of delete when cancel'ing.")
          
          (* ;; "rht 6/4/87: Now stashes on doc card a prop list of destination notefiles of cross-file links encountered during creation of contents.")
          
          (* ;; "rht 2/10/88: Changed print message slightly.")

    (NCP.WithLockedCards (PROG (RootCard RootTitle DocWindow DocCard DocWindowOrCard DocStream 
                                      HeadingsFromFileboxes TitlesFromNoteCards BuildBackLinks 
                                      CopyEmbeddedLinks ExpandEmbeddedLinks InspectWin 
                                      RootSubstanceType)
                               (OR NoDisplayFlg (SPAWN.MOUSE))
                               (SETQ DocWindowOrCard (NC.ApplySupersFn MakeFn Card "Document" 
                                                            NoDisplayFlg))
                               (if NoDisplayFlg
                                   then (SETQ DocWindow NIL)
                                        (SETQ DocCard DocWindowOrCard)
                                 else (SETQ DocWindow DocWindowOrCard)
                                      (SETQ DocCard (NC.CoerceToCard DocWindow)))
                                                             (* ; 
                     "NC.MakeNoteCard either returned an Card or a window depending on NoDisplayFlg.")

                               (SETQ RootCard (OR (NC.CoerceToCard CardIdentifier)
                                                  (NC.SelectNoteCards T NIL NC.SelectingCardMenu 
                                                         DocWindow 
                      "Please shift-select the Note Card or File Box the document should start from."
                                                         )))
                               (if (NOT RootCard)
                                   then (NC.DeleteNoteCard Card NIL T)
                                        (RETURN NIL))
                               (SETQ RootTitle (NC.RetrieveTitle RootCard))
                               (NC.SetTitle DocCard (CONCAT "Document from %"" RootTitle "%""))
                               (AND DocWindow (WINDOWPROP DocWindow 'TITLE (NC.RetrieveTitle DocCard)
                                                     ))
                               (SETQ DocStream (NC.FetchSubstance DocCard))

(* ;;; "Get MakeDocument parameters from user via inspector window.")

                               (if (NOT NoDisplayFlg)
                                   then (SETQ InspectWin (NC.BuildMakeDocInspector DocWindow))
                                        (TOTOPW InspectWin)
                                        (for while (OPENWP InspectWin) do (BLOCK)))
                               (if (EQ (GETPROP 'NC.MakeDocParameters '--DONE--)
                                       'QUIT)
                                   then (PUTPROP 'NC.MakeDocParameters '--DONE-- '--CANCEL--)
                                        (NC.DeleteNoteCard Card NIL T)
                                        (RETURN NIL))
                               (SETQ HeadingsFromFileboxes (GETPROP 'NC.MakeDocParameters
                                                                  'HeadingsFromFileboxes))
                               (SETQ TitlesFromNoteCards (GETPROP 'NC.MakeDocParameters '
                                                                TitlesFromNoteCards))
                               (SETQ BuildBackLinks (GETPROP 'NC.MakeDocParameters 'BuildBackLinks))
                               (SETQ CopyEmbeddedLinks (GETPROP 'NC.MakeDocParameters '
                                                              CopyEmbeddedLinks))
                               (SETQ ExpandEmbeddedLinks (GETPROP 'NC.MakeDocParameters '
                                                                ExpandEmbeddedLinks))

(* ;;; "Call recursive routine to dump filebox.")

                               (RESETLST (RESETSAVE (CURSOR WAITINGCURSOR))
                                      (NC.PrintMsg DocWindow NIL 
                                             "Collecting contents from descendant cards ... ")

(* ;;; "Clean up the SeenBefore markers placed on the cards and boxes just copied.")

                                      [RESETSAVE NIL '(PROGN (for Card
                                                                in (NC.FetchUserDataProp DocCard
                                                                          'SeenCards)
                                                                do (NC.SetUserDataProp Card
                                                                          'SeenBefore NIL))
                                                             (NC.SetUserDataProp DocCard 'SeenCards 
                                                                    NIL]
                                                             (* ; 
                                       "Clean up the CrossFileLinkModePropList hung off the Doccard.")

                                      [RESETSAVE NIL '(PROGN (NC.SetUserDataProp DocCard '
                                                                    CrossFileLinkModePropList NIL]

(* ;;; "Unbelievably kludgy hack to get around Intermezzo TEdit bug.  Just insert and delete a CR.")

                                      (TEDIT.INSERT DocStream NC.CRString 1)
                                      (TEDIT.DELETE DocStream 1 1)
                                      (NC.SetUserDataProp DocCard 'CrossFileLinkModePropList
                                             (LIST (fetch (Card NoteFile) of DocCard)
                                                   NIL))
                                      (NC.DumpCardToDoc RootCard DocCard DocStream 0 0 
                                             HeadingsFromFileboxes TitlesFromNoteCards BuildBackLinks 
                                             CopyEmbeddedLinks ExpandEmbeddedLinks)
                                      (NC.PrintMsg DocWindow NIL "Done!"))
                               (COND
                                  ((NOT NoDisplayFlg)
                                   (BLOCK 250)
                                   (NC.ClearMsg DocWindow T)))
                               (RETURN DocWindowOrCard])

(NC.DumpCardToDoc
  [LAMBDA (Card DocCard DocStream CurSection SubSectionNum HeadingsFromFileboxes TitlesFromNoteCards 
                BuildBackLinks CopyEmbeddedLinks ExpandEmbeddedLinks)
                                                             (* ; "Edited  8-Feb-88 17:18 by Trigg")
          
          (* ;; "rht 10/15/86: Integrated markM's changes and fixed box numbering.")
          
          (* ;; "rht 6/4/87: Now handles cross file links.")
          
          (* ;; "rht 2/8/88: Now returns SubSectionNum in case that can't follow cross-file link.")

    (LET ((Title (NC.RetrieveTitle Card)))
         (if [AND (NC.CrossFileLinkCardP Card)
                  (NOT (SETQ Card (NC.GetCrossFileLinkDestCard Card (NC.CoerceToInterestedWindow
                                                                     DocCard]
             then (NC.PrintMsg NIL NIL "Couldn't follow cross-file link to " Title (CHARACTER 13))
                  SubSectionNum
           else (LET ((Type (NC.RetrieveType Card)))
                     (COND
                        ((OR (NCP.SketchBasedP Card)
                             (NCP.GraphBasedP Card)
                             (GETPROP Type 'ExportSubstanceFn))
                         (NC.ActivateCardAndDo Card
                                (NC.DumpExportableCardToDoc Card DocCard DocStream CurSection 
                                       SubSectionNum HeadingsFromFileboxes TitlesFromNoteCards 
                                       BuildBackLinks CopyEmbeddedLinks ExpandEmbeddedLinks Type)))
                        ((NCP.FileBoxP Card)
                         (NC.ActivateCardAndDo Card
                                (NC.DumpFileBoxToDoc Card DocCard DocStream CurSection SubSectionNum 
                                       HeadingsFromFileboxes TitlesFromNoteCards BuildBackLinks 
                                       CopyEmbeddedLinks ExpandEmbeddedLinks)))
                        ((NCP.TextBasedP Card)
                         (NC.ActivateCardAndDo Card
                                (NC.DumpNoteCardToDoc Card DocCard DocStream CurSection SubSectionNum 
                                       HeadingsFromFileboxes TitlesFromNoteCards BuildBackLinks 
                                       CopyEmbeddedLinks ExpandEmbeddedLinks)))
                        (T (NC.PrintMsg NIL NIL "Can't make document from non-exportable card "
                                  (NC.RetrieveTitle Card)
                                  (CHARACTER 13))
                           SubSectionNum])

(NC.DumpExportableCardToDoc
  [LAMBDA (Card DocCard DocStream CurSection SubSectionNum HeadingsFromFileboxes TitlesFromNoteCards 
                BuildBackLinks CopyEmbeddedLinks ExpandEmbeddedLinks CardType)
                                                             (* ; "Edited 10-Feb-88 11:26 by Trigg")

(* ;;; "Dump the CardID sketch or graph card to the document card DocStream.")
          
          (* ;; "rht 9/16/85: Now handles cr's around titles using para leading.")
          
          (* ;; "fgh 11/17/85 Updated to handle card objects and removal of SubstanceTypes.")
          
          (* ;; "rht 7/31/86: Now can handle other card types than Graph or Sketch based ones, but they must have an appropriate fn on the ExportSubstanceFn property of the atom.  That fn should return either an imageobj or a textstream.")
          
          (* ;; "rht 8/11/86: Added ShrunkenFlg so that Card is reshrunk afterwards if necessary.")
          
          (* ;; "rht 10/15/86: Integrated markM's changes and fixed box numbering.")
          
          (* ;; "rht 10/31/86: Now uses TEDIT.COPY rather than TEDIT.INCLUDE because the latter loses formatting info.")
          
          (* ;; "rht 3/26/87: Fixed bug whereby links in sketch cards weren't getting smashed to 'external' link icons.  Now calls NC.MakeExternalSketchCopy.")
          
          (* ;; "rht 4/22/87: Now expects NC.MakeExternalSketchCopy to return an imageobj.")
          
          (* ;; "rht 6/4/87: Now calls NC.MakeLink rather than NCP.LocalGlobalLink and handles computes proper cross-file links mode.")
          
          (* ;; 
   "pmi 8/7/87: Now 'externalizes' graphs so that they won't break when edited once in the document.")
          
          (* ;; "rht 2/10/88: Added a check for link to copy being crossfilelink.")

    (DECLARE (GLOBALVARS NC.DocBackPtrLinkLabel))
    (LET ((CardStream (NC.FetchSubstance Card))
          (DocObj (TEXTOBJ DocStream))
          (InterestedWindow (NC.CoerceToInterestedWindow DocCard))
          ShrunkenFlg OldLoc ThingToInsert)
         (NC.AddCRIfNeeded DocStream)
         (SETQ OldLoc (fetch (TEXTOBJ TEXTLEN) of DocObj))
         [AND (NEQ TitlesFromNoteCards 'NONE)
              (NC.AppendStringToStream DocStream (CONCAT (NC.RetrieveTitle Card))
                     (EQ TitlesFromNoteCards 'Bold]
         (AND (FMEMB BuildBackLinks '(ToCards ToCardsBoxes))
              (NC.AddLinkToCard DocCard (NC.MakeLink NIL NC.DocBackPtrLinkLabel Card DocCard
                                               (create LINKDISPLAYMODE
                                                      ATTACHBITMAPFLG ← T)
                                               NIL NIL NIL NIL (NC.ComputeCrossFileLinkMode
                                                                Card
                                                                (NC.FetchUserDataProp DocCard
                                                                       'CrossFileLinkModePropList)
                                                                InterestedWindow))
                     Card))
         (if (GREATERP (fetch (TEXTOBJ TEXTLEN) of DocObj)
                    OldLoc)
             then (NC.AddCRIfNeeded DocStream)
                  (NC.ChangeParaLeading DocStream))
         (if (NOT (NC.FetchUserDataProp Card 'SeenBefore))
             then (SETQ ShrunkenFlg (NC.GetShrunkenWin Card))
                  [NC.SetUserDataProp DocCard 'SeenCards (CONS Card (NC.FetchUserDataProp
                                                                     DocCard
                                                                     'SeenCards]
                  (NC.SetUserDataProp Card 'SeenBefore T) 

(* ;;; "Stick an imageobj made from the card into the document.  Also might be a textstream computed by the card type's ExportSubstanceFn.")

                  [SETQ ThingToInsert (if (NCP.GraphBasedP CardType)
                                          then (GRAPHEROBJ (NC.MakeExternalGraphCopy CardStream))
                                        elseif (NCP.SketchBasedP CardType)
                                          then (NC.MakeExternalSketchCopy (OR (NC.FetchWindow Card)
                                                                              CardStream))
                                        elseif (LET [(ExportSubstanceFn (GETPROP CardType
                                                                               'ExportSubstanceFn]
                                                    (AND ExportSubstanceFn (APPLY* ExportSubstanceFn 
                                                                                  CardStream]
                  [AND CardStream (if (IMAGEOBJP ThingToInsert)
                                      then (TEDIT.INSERT.OBJECT ThingToInsert DocStream)
                                    elseif (TEXTSTREAMP ThingToInsert)
                                      then (TEDIT.COPY (TEDIT.SETSEL ThingToInsert 1
                                                              (fetch TEXTLEN of (TEXTOBJ 
                                                                                       ThingToInsert)
                                                                     ))
                                                  (TEDIT.SETSEL DocStream (fetch TEXTLEN of DocObj)
                                                         1
                                                         'RIGHT] 

(* ;;; "Step through list of notecard imageobjs in the card we're working on and either expand or copy or ignore each according to values of ExpandEmbeddedLinks and CopyEmbeddedLinks.")

                  (for Link in (CAR (NC.CollectReferences Card NIL NIL NIL))
                     bind LinkLabel ToCard ToCardType ActiveFlg ExpandFlg CopyFlg AlreadyExpanded
                     eachtime (BLOCK)
                     do (SETQ LinkLabel (fetch (Link Label) of Link))
                        [SETQ CopyFlg (OR (EQ CopyEmbeddedLinks 'ALL)
                                          (AND (LISTP CopyEmbeddedLinks)
                                               (FMEMB LinkLabel CopyEmbeddedLinks]
                        [SETQ ExpandFlg (OR (EQ ExpandEmbeddedLinks 'ALL)
                                            (AND (LISTP ExpandEmbeddedLinks)
                                                 (FMEMB LinkLabel ExpandEmbeddedLinks]
                        (SETQ ToCard (fetch (Link DestinationCard) of Link))
                        (if (AND (NC.CrossFileLinkCardP ToCard)
                                 (OR ExpandFlg CopyFlg))
                            then (SETQ ToCard (NC.GetCrossFileLinkDestCard ToCard InterestedWindow)))
                        [if (NC.ValidCardP ToCard)
                            then (if (AND (SETQ AlreadyExpanded (NC.FetchUserDataProp ToCard
                                                                       'SeenBefore))
                                          ExpandFlg)
                                     then (NC.PrintMsg NIL NIL (NC.RetrieveTitle ToCard)
                                                 " only expanded once in this cycle."
                                                 (CHARACTER 13)))
                                 (if (OR CopyFlg (AND ExpandFlg AlreadyExpanded))
                                     then                    (* ; "Copy this link.")

                                          (NC.AddLinkToCard DocCard
                                                 (NC.MakeLink NIL LinkLabel ToCard DocCard NIL NIL 
                                                        NIL NIL NIL (NC.ComputeCrossFileLinkMode
                                                                     ToCard
                                                                     (NC.FetchUserDataProp
                                                                      DocCard
                                                                      'CrossFileLinkModePropList)
                                                                     InterestedWindow))
                                                 ToCard))
                                 (if (AND ExpandFlg (NOT AlreadyExpanded))
                                     then                    (* ; 
                                             "Expand this link.  Check type and make recursive call.")

                                          (SETQ SubSectionNum
                                           (NC.DumpCardToDoc ToCard DocCard DocStream CurSection 
                                                  SubSectionNum HeadingsFromFileboxes 
                                                  TitlesFromNoteCards BuildBackLinks 
                                                  CopyEmbeddedLinks ExpandEmbeddedLinks]
                     finally (TEDIT.SETSEL DocStream (ADD1 (fetch TEXTLEN of DocObj))
                                    0
                                    'RIGHT))
                  (NC.SetUserDataProp Card 'SeenBefore NIL)
                  (AND ShrunkenFlg (SHRINKW (NC.FetchWindow Card)))
           else (NC.PrintMsg NIL NIL (NC.RetrieveTitle Card)
                       " only expanded once in this cycle."
                       (CHARACTER 13)))
         SubSectionNum])

(NC.DumpFileBoxToDoc
  [LAMBDA (FileBoxCard DocCard DocStream CurSection SubSectionNum HeadingsFromFileboxes 
                 TitlesFromNoteCards BuildBackLinks CopyEmbeddedLinks ExpandEmbeddedLinks)
                                                             (* rht%: " 4-Jun-87 12:28")
          
          (* * Dump the contents of FileboxID to DocStream by recursively dumping all of 
          its children. Keep track of the section numbers in case HeadingsFromFileboxes = 
          Numbered. Insert back pointers to the source cards and/or boxes depending on 
          value of BuildBackpointers. Embedded links
          (or some subset of them) may be copied or expanded depending on the values of 
          CopyEmbeddedLinks and ExpandEmbeddedLinks.)
          
          (* * rht 8/25/85%: Fixed to handle sketch and graph cards.)
          
          (* * rht 9/16/85%: Now handles cr's around titles using para leading.)
          
          (* * fgh |11/178/85| Updated to handle Card and NoteFile objects.)
          
          (* * rht 7/31/86%: Now checks for card types having ExportSubstanceFn prop.)
          
          (* * rht 8/11/86%: Added ShrunkenFlg so that FileBoxCard is reshrunk afterwards 
          if necessary.)
          
          (* * rht 10/15/86%: Integrated markM's changes and fixed box numbering.)
          
          (* * rht 6/4/87%: Now calls NC.MakeLink rather than NCP.LocalGlobalLink and 
          handles computes proper cross-file links mode.)

    (LET ((DocObject (TEXTOBJ DocStream))
          (InterestedWindow (NC.CoerceToInterestedWindow DocCard))
          ShrunkenFlg OldLoc)
         (NC.AddCRIfNeeded DocStream)
         (SETQ OldLoc (fetch (TEXTOBJ TEXTLEN) of DocObject))
         (if (AND (EQ HeadingsFromFileboxes 'NumberedBold)
                  (NOT (ZEROP SubSectionNum)))
             then (SETQ CurSection (if (ZEROP CurSection)
                                       then SubSectionNum
                                     else (CONCAT CurSection "." SubSectionNum)))
                  (NC.AppendStringToStream DocStream (CONCAT CurSection " ")
                         T))
         (AND (NEQ HeadingsFromFileboxes 'NONE)
              (NC.AppendStringToStream DocStream (CONCAT (NC.RetrieveTitle FileBoxCard))
                     T))
         (AND (FMEMB BuildBackLinks '(ToBoxes ToCardsBoxes))
              (NC.AddLinkToCard DocCard (NC.MakeLink NIL NC.DocBackPtrLinkLabel FileBoxCard DocCard
                                               (create LINKDISPLAYMODE
                                                      ATTACHBITMAPFLG ← T)
                                               NIL NIL NIL NIL (NC.ComputeCrossFileLinkMode
                                                                FileBoxCard
                                                                (NC.FetchUserDataProp DocCard
                                                                       'CrossFileLinkModePropList)
                                                                InterestedWindow))
                     FileBoxCard))
         (if (GREATERP (fetch (TEXTOBJ TEXTLEN) of DocObject)
                    OldLoc)
             then (NC.AddCRIfNeeded DocStream)
                  (NC.ChangeParaLeading DocStream))
         (if (NOT (NC.FetchUserDataProp FileBoxCard 'SeenBefore))
             then (SETQ ShrunkenFlg (NC.GetShrunkenWin FileBoxCard))
                  [NC.SetUserDataProp DocCard 'SeenCards (CONS FileBoxCard (NC.FetchUserDataProp
                                                                            DocCard
                                                                            'SeenCards]
                  (NC.SetUserDataProp FileBoxCard 'SeenBefore T)
                  [for Link in (NC.FetchToLinksInOrder FileBoxCard) bind (SubSectionCounter ← 1)
                     eachtime (BLOCK) when (FMEMB (fetch (Link Label) of Link)
                                                  (LIST NC.FiledCardLinkLabel NC.SubBoxLinkLabel))
                     do (LET ((ChildCard (fetch (Link DestinationCard) of Link)))
                             (SETQ SubSectionCounter
                              (NC.DumpCardToDoc ChildCard DocCard DocStream CurSection 
                                     SubSectionCounter HeadingsFromFileboxes TitlesFromNoteCards 
                                     BuildBackLinks CopyEmbeddedLinks ExpandEmbeddedLinks]
                  (NC.SetUserDataProp FileBoxCard 'SeenBefore NIL)
                  (AND ShrunkenFlg (SHRINKW (NC.FetchWindow FileBoxCard)))
                  (ADD1 SubSectionNum)
           else (NC.PrintMsg NIL NIL (NC.RetrieveTitle FileBoxCard)
                       " only expanded once in this cycle.")
                SubSectionNum])

(NC.DumpNoteCardToDoc
  [LAMBDA (Card DocCard DocStream CurSection SubSectionNum HeadingsFromFileboxes TitlesFromNoteCards 
                BuildBackLinks CopyEmbeddedLinks ExpandEmbeddedLinks)
                                                             (* ; "Edited 10-Feb-88 11:26 by Trigg")

(* ;;; "Dump the CardID notecard to the document card DocStream.")
          
          (* ;; "rht 8/25/85: Fixed to handle sketch and graph cards.")
          
          (* ;; "rht 9/16/85: Now handles cr's around titles using para leading.")
          
          (* ;; "fgh 11/178/85 Updated to handle Card and NoteFile objects.")
          
          (* ;; "rht 7/31/86: Now checks for card types having ExportSubstanceFn prop.")
          
          (* ;; "rht 8/11/86: Added ShrunkenFlg so that Card is reshrunk afterwards if necessary.")
          
          (* ;; "rht 10/15/86: Integrated markM's changes and fixed box numbering.")
          
          (* ;; "rht 6/4/87: Now calls NC.MakeLink rather than NCP.LocalGlobalLink and handles computes proper cross-file links mode.")
          
          (* ;; "rht 2/10/88: Added a check for link to copy being crossfilelink.")

    (LET
     ((CardStream (NC.FetchSubstance Card))
      (DocObj (TEXTOBJ DocStream))
      (InterestedWindow (NC.CoerceToInterestedWindow DocCard))
      ShrunkenFlg CardObj OldLoc)
     (SETQ CardObj (TEXTOBJ CardStream))
     (NC.AddCRIfNeeded DocStream)
     (SETQ OldLoc (fetch (TEXTOBJ TEXTLEN) of DocObj))
     [AND (NEQ TitlesFromNoteCards 'NONE)
          (NC.AppendStringToStream DocStream (CONCAT (NC.RetrieveTitle Card))
                 (EQ TitlesFromNoteCards 'Bold]
     (AND (FMEMB BuildBackLinks '(ToCards ToCardsBoxes))
          (NC.AddLinkToCard DocCard (NC.MakeLink NIL NC.DocBackPtrLinkLabel Card DocCard
                                           (create LINKDISPLAYMODE
                                                  ATTACHBITMAPFLG ← T)
                                           NIL NIL NIL NIL (NC.ComputeCrossFileLinkMode
                                                            Card
                                                            (NC.FetchUserDataProp DocCard
                                                                   'CrossFileLinkModePropList)
                                                            InterestedWindow))
                 Card))
     (if (GREATERP (fetch (TEXTOBJ TEXTLEN) of DocObj)
                OldLoc)
         then (NC.AddCRIfNeeded DocStream)
              (NC.ChangeParaLeading DocStream))
     (if (NOT (NC.FetchUserDataProp Card 'SeenBefore))
         then (SETQ ShrunkenFlg (NC.GetShrunkenWin Card))
              [NC.SetUserDataProp DocCard 'SeenCards (CONS Card (NC.FetchUserDataProp DocCard
                                                                       'SeenCards]
              (NC.SetUserDataProp Card 'SeenBefore T) 
          
          (* ;; "Step through list of notecard imageobjs in the card we're working on and either expand or copy or ignore each according to values of ExpandEmbeddedLinks and CopyEmbeddedLinks.")

              (for Object in (TEDIT.LIST.OF.OBJECTS CardObj (FUNCTION NC.LinkIconImageObjP))
                 bind LinkSpec LinkLabel ToCard ToCardType (LastLoc ← 1)
                      (CurLoc ← 0)
                      ActiveP ExpandP CopyP AlreadyExpanded eachtime (BLOCK)
                 do [(SETQ LinkSpec (NC.FetchLinkFromLinkIcon (CAR Object)))
                     (SETQ LinkLabel (fetch (Link Label) of LinkSpec))
                     (SETQ CurLoc (CADR Object))             (* ; 
                                                  "Copy over any text between this obj and the last.")

                     [if (ILESSP LastLoc CurLoc)
                         then (TEDIT.COPY (TEDIT.SETSEL CardStream LastLoc (IDIFFERENCE CurLoc 
                                                                                  LastLoc))
                                     (TEDIT.SETSEL DocStream (fetch TEXTLEN of DocObj)
                                            1
                                            'RIGHT]
                     (SETQ LastLoc (ADD1 CurLoc))
                     [SETQ CopyP (OR (EQ CopyEmbeddedLinks 'ALL)
                                     (AND (LISTP CopyEmbeddedLinks)
                                          (FMEMB LinkLabel CopyEmbeddedLinks]
                     [SETQ ExpandP (OR (EQ ExpandEmbeddedLinks 'ALL)
                                       (AND (LISTP ExpandEmbeddedLinks)
                                            (FMEMB LinkLabel ExpandEmbeddedLinks]
                     (SETQ ToCard (fetch (Link DestinationCard) of LinkSpec))
                     (if (AND (NC.CrossFileLinkCardP ToCard)
                              (OR ExpandP CopyP))
                         then (SETQ ToCard (NC.GetCrossFileLinkDestCard ToCard InterestedWindow)))
                     (if (NC.ValidCardP ToCard)
                         then (if (AND (SETQ AlreadyExpanded (NC.FetchUserDataProp ToCard
                                                                    'SeenBefore))
                                       ExpandP)
                                  then (NC.PrintMsg NIL NIL (NC.RetrieveTitle ToCard)
                                              " only expanded once in this cycle."
                                              (CHARACTER 13)))
                              (if (OR CopyP (AND ExpandP AlreadyExpanded))
                                  then                       (* ; "Copy this link.")

                                       (NC.AddLinkToCard DocCard (NC.MakeLink
                                                                  NIL LinkLabel ToCard DocCard
                                                                  (fetch (Link DisplayMode)
                                                                     of LinkSpec)
                                                                  NIL NIL NIL NIL
                                                                  (NC.ComputeCrossFileLinkMode
                                                                   ToCard
                                                                   (NC.FetchUserDataProp DocCard
                                                                          'CrossFileLinkModePropList)
                                                                   InterestedWindow))
                                              ToCard)        (* ; "(TEDIT.COPY (TEDIT.SETSEL CardStream CurLoc 1) (TEDIT.SETSEL DocStream (ADD1 (fetch TEXTLEN of DocObj)) 0 'RIGHT))")
)
                              (if (AND ExpandP (NOT AlreadyExpanded))
                                  then                       (* ; 
                                             "Expand this link.  Check type and make recursive call.")

                                       (SETQ SubSectionNum
                                        (NC.DumpCardToDoc ToCard DocCard DocStream CurSection 
                                               SubSectionNum HeadingsFromFileboxes 
                                               TitlesFromNoteCards BuildBackLinks CopyEmbeddedLinks 
                                               ExpandEmbeddedLinks]
                 finally [COND
                            ((ILESSP CurLoc (fetch TEXTLEN of CardObj))
                             (TEDIT.COPY (TEDIT.SETSEL CardStream LastLoc
                                                (IDIFFERENCE (fetch TEXTLEN of CardObj)
                                                       CurLoc))
                                    (TEDIT.SETSEL DocStream (ADD1 (fetch TEXTLEN of DocObj))
                                           0
                                           'RIGHT]
                       (TEDIT.SETSEL DocStream (ADD1 (fetch TEXTLEN of DocObj))
                              0
                              'RIGHT))
              (NC.SetUserDataProp Card 'SeenBefore NIL)
              (AND ShrunkenFlg (SHRINKW (NC.FetchWindow Card)))
       else (NC.PrintMsg NIL NIL (NC.RetrieveTitle Card)
                   " only expanded once in this cycle."
                   (CHARACTER 13)))
     SubSectionNum])
)



(* ;; "Change to NCCROSSFILELINKS")

(DEFINEQ

(NC.AskCrossFileLinkMode
  [LAMBDA (DestCard InterestedWindow)                        (* ; "Edited 10-Feb-88 11:25 by Trigg")

(* ;;; "Ask the user whether to make the link be twoway or oneway.  Return T if user says two-way.  If dest notefile is open read-only, then only choice is one-way so don't ask.")
          
          (* ;; 
          "rht 2/10/88: Now checks whether prompt window was already open and if so, preserves that.")

    (LET* ((NoteFile (fetch (Card NoteFile) of DestCard))
           (FullFileName (fetch (NoteFile FullFileName) of NoteFile))
           (WasOpenPromptWindowFlg (NC.PromptWindowOpenP InterestedWindow)))
          (if (NC.ReadOnlyNoteFileP NoteFile)
              then NIL
            else (NC.AskYesOrNo (CONCAT "Okay to make cross-file link to '" (NC.RetrieveTitle 
                                                                                   DestCard)
                                       "' in '"
                                       (FILENAMEFIELD FullFileName 'NAME)
                                       ";"
                                       (FILENAMEFIELD FullFileName 'VERSION)
                                       "' be TWOWAY?"
                                       (CHARACTER 13)
                                       
                                      "(otherwise leaves no record of link in destination notefile) "
                                       )
                        " -- " "Yes" T InterestedWindow WasOpenPromptWindowFlg])
)
(PUTPROPS RHTPATCH309 COPYRIGHT ("Xerox Corporation" 1988))
(DECLARE%: DONTCOPY
  (FILEMAP (NIL (1978 36172 (NC.MakeDocument 1988 . 10507) (NC.DumpCardToDoc 10509 . 13117) (
NC.DumpExportableCardToDoc 13119 . 22712) (NC.DumpFileBoxToDoc 22714 . 27676) (NC.DumpNoteCardToDoc 
27678 . 36170)) (36217 37795 (NC.AskCrossFileLinkMode 36227 . 37793)))))
STOP