(FILECREATED "11-Jun-87 16:46:26" {QV}<NOTECARDS>1.3K>NEXT>NCLINKS.;75 172988 

      changes to:  (FNS NC.CheckDisplayModeFormat NC.DelReferencesToCard NC.DeleteLink 
			NC.DeletedLinkGetFn)

      previous date: " 8-Jun-87 18:08:49" {QV}<NOTECARDS>1.3K>NEXT>NCLINKS.;74)


(* Copyright (c) 1984, 1985, 1986, 1987 by Xerox Corporation. All rights reserved.)

(PRETTYCOMPRINT NCLINKSCOMS)

(RPAQQ NCLINKSCOMS ((* * Internal variables)
	(GLOBALVARS NC.LinkIconSpaceInterLine NC.FileBoxLinkSpacer NC.NoDeleteImageFns 
		    NC.SystemLinkLabels NC.InitialLinkLabels NC.SubBoxLinkLabel NC.FiledCardLinkLabel 
		    NC.BrowserContentsLinkLabel NC.ListContentsLinkLabel NC.UnspecifiedLinkLabel 
		    NC.UCASESystemLinkLabels NC.DocBackPtrLinkLabel NC.LinkIndexBackPtrLinkLabel 
		    NC.LinkIconImageFns NC.LinkIconMiddleButtonMenu NC.LinkDisplayModesStylesheet 
		    NC.LinkIconBorderWidth NC.LinkIconSpaceOuterY NC.LinkIconSpaceInnerY 
		    NC.LinkIconSpaceOuterX NC.LinkIconSpaceInnerX NC.OrphanID 
		    MINIMUM.VISIBLE.SCALE.FACTOR DEFAULT.VISIBLE.SCALE.FACTOR TEDIT.DEFAULT.FONT)
	(VARS (NC.SubBoxLinkLabel (QUOTE SubBox))
	      (NC.FiledCardLinkLabel (QUOTE FiledCard))
	      (NC.BrowserContentsLinkLabel (QUOTE BrowserContents))
	      (NC.ListContentsLinkLabel (QUOTE ListContents))
	      (NC.UnspecifiedLinkLabel (QUOTE Unspecified))
	      (NC.DocBackPtrLinkLabel (QUOTE DocBackPtr))
	      (NC.LinkIndexBackPtrLinkLabel (QUOTE LinkIndexBackPtr))
	      (NC.SystemLinkLabels (LIST NC.ListContentsLinkLabel NC.BrowserContentsLinkLabel 
					 NC.FiledCardLinkLabel NC.SubBoxLinkLabel 
					 NC.DocBackPtrLinkLabel NC.LinkIndexBackPtrLinkLabel))
	      (NC.UCASESystemLinkLabels (FOR Label in NC.SystemLinkLabels collect (U-CASE Label)))
	      (NC.InitialLinkLabels (APPEND (QUOTE (Comment See Unspecified))
					    NC.SystemLinkLabels)))
	(INITVARS (NC.LinkIconSpaceInterLine 0)
		  (NC.LinkIconBorderWidth 1)
		  (NC.LinkIconSpaceOuterY 2)
		  (NC.LinkIconSpaceInnerY 1)
		  (NC.LinkIconSpaceOuterX 2)
		  (NC.LinkIconSpaceInnerX 3)
		  (NC.FileBoxLinkSpacer (CHARACTER 13)))
	(* * Links mechanism)
	(RECORDS Link LINKDISPLAYMODE)
	(FNS NC.GlobalLinkP NC.ChildLinkP NC.ContentsLinkP NC.SubContentsLinkP NC.ReverseLinkLabelP 
	     NC.MakeGlobalLink NC.AddFromLink NC.AddLinkToGraphCard NC.AddLinkToSketchCard 
	     NC.InsertLinkInSketch NC.InsertLinkInText NC.AddToLink NC.CachedAddToLink NC.DelFromLink 
	     NC.CachedDelFromLink NC.DelToLink NC.DelReferencesToCard 
	     NC.DelReferencesToCardFromGlobalList NC.DeleteLink NC.DeleteToLink NC.DeleteFromLink 
	     NC.DeletedLinkDisplayFn NC.DeletedLinkGetFn NC.DeletedLinkImageBoxFn NC.DeletedLinkPutFn 
	     NC.DeletedLinkImageObjP NC.CheckForOrphanDelete NC.FileInOrphanBox NC.HookToOrphanCard 
	     NC.InsertLinkBeforeMarker NC.LinkLabelP NC.IDAlphOrder NC.MakeLink 
	     NC.CachedMakeFilingLink NC.MakeFilingLink NC.MakeFilingLinks NC.RelabelLink 
	     NC.SystemLinkLabelP NC.ValidLinkP NC.MakeChildLink NC.EnsureNoCycles NC.SameLinkP 
	     NC.SameLinksP NC.AddGlobalLinkToCard NC.AddGlobalLinksToCard 
	     NC.ChangeCardTitleFromLinkIcon NC.UncacheLinks NC.TraverseLink NC.CreateLinkIconStrings)
	(FNS NC.AddLinkToCard NC.AddLinksToCard)
	(FNS NC.DelReferencesToCardFromShowLinks)
	(* * New Display Mode format)
	(FNS NC.CheckDisplayModeFormat NC.InsureLinkDisplayMode NC.MakeNewDisplayMode 
	     NC.DisplayModeFromStylesheetSelections NC.StylesheetSelectionsFromDisplayMode)
	(GLOBALVARS NC.MenuItems/DisplayModeValues)
	[VARS (NC.MenuItems/DisplayModeValues (QUOTE (Yes T No NIL Float FLOAT Yes T No NIL Float 
							  FLOAT Yes T No NIL Float FLOAT]
	(* * New fns to support No-Link cards/substances)
	(FNS NC.LinksSupportedP)
	(* * This stuff supports the "push-copy" method of copying links in NC)
	(FNS)
	(* * Link display icons)
	(FNS NC.FetchLinkIconForLink NC.MakeLinkIcon NC.LinkIconButtonEventInFn NC.LinkIconDisplayFn 
	     NC.LinkIconImageBoxFn NC.LinkIconImageObjP NC.LinkIconPutFn NC.LinkIconGetFn 
	     NC.LinkIconMiddleButtonFn NC.LinkIconWhenCopiedFn NC.LinkIconWhenDeletedFn 
	     NC.LinkIconWhenInsertedFn NC.LinkIconWhenMovedFn NC.FetchLinkFromLinkIcon 
	     NC.ChangeLinkDisplayMode NC.UpdateLinkImages NC.LinkIconCopyFn NC.LinkAtCharPos 
	     NC.LinkIconLeftButtonFn)
	(FNS NC.FillInLinkIcon NC.InvisibleLinkImageBoxFn NC.InvisibleLinkPutFn NC.InvisibleLinkGetFn)
	(* * New stuff for allowing put to work from TEdit.)
	(GLOBALVARS NC.ExternalPutLinkIconImageFns)
	[VARS (NC.ExternalPutLinkIconImageFns (IMAGEFNSCREATE (FUNCTION 
								  NC.ExternalPutLinkIconDisplayFn)
							      (FUNCTION 
								 NC.ExternalPutLinkIconImageBoxFn)
							      (FUNCTION NC.ExternalPutLinkIconPutFn)
							      (FUNCTION NC.ExternalPutLinkIconGetFn)
							      NIL
							      (FUNCTION NILL]
	(FNS NC.ExternalPutLinkIconDisplayFn NC.ExternalPutLinkIconImageBoxFn 
	     NC.ExternalPutLinkIconPutFn NC.ExternalPutLinkIconGetFn NC.CoerceToExternalPutLinkIcon)
	(* * Stuff to register our image object for intermezzo.)
	(FNS NC.RegisterPlaceMarkerImageObj)
	(P (NC.RegisterPlaceMarkerImageObj))
	(* * Stuff for handling link icons in sketches.)
	(FNS NC.LinkIconSketchElementP NC.DeleteLinkIconSketchElement)
	(* * Icons representing notecards types.)
	(GLOBALVARS NC.FileBoxIcon NC.GraphCardIcon NC.SketchCardIcon NC.TextCardIcon NC.TypelessIcon)
	(* * DELETEME imageobjs)
	(GLOBALVARS NC.DELETEMEImageObjDatum NC.RemoveDELETEMEImageObjsFromCardFlg)
	(INITVARS (NC.DELETEMEImageObjDatum "[[DELETE ME]]")
		  (NC.RemoveDELETEMEImageObjsFromCardFlg NIL))
	(FNS NC.DELETEMEImageObjP)))
(* * Internal variables)

(DECLARE: DOEVAL@COMPILE DONTCOPY

(GLOBALVARS NC.LinkIconSpaceInterLine NC.FileBoxLinkSpacer NC.NoDeleteImageFns NC.SystemLinkLabels 
	    NC.InitialLinkLabels NC.SubBoxLinkLabel NC.FiledCardLinkLabel NC.BrowserContentsLinkLabel 
	    NC.ListContentsLinkLabel NC.UnspecifiedLinkLabel NC.UCASESystemLinkLabels 
	    NC.DocBackPtrLinkLabel NC.LinkIndexBackPtrLinkLabel NC.LinkIconImageFns 
	    NC.LinkIconMiddleButtonMenu NC.LinkDisplayModesStylesheet NC.LinkIconBorderWidth 
	    NC.LinkIconSpaceOuterY NC.LinkIconSpaceInnerY NC.LinkIconSpaceOuterX 
	    NC.LinkIconSpaceInnerX NC.OrphanID MINIMUM.VISIBLE.SCALE.FACTOR 
	    DEFAULT.VISIBLE.SCALE.FACTOR TEDIT.DEFAULT.FONT)
)

(RPAQQ NC.SubBoxLinkLabel SubBox)

(RPAQQ NC.FiledCardLinkLabel FiledCard)

(RPAQQ NC.BrowserContentsLinkLabel BrowserContents)

(RPAQQ NC.ListContentsLinkLabel ListContents)

(RPAQQ NC.UnspecifiedLinkLabel Unspecified)

(RPAQQ NC.DocBackPtrLinkLabel DocBackPtr)

(RPAQQ NC.LinkIndexBackPtrLinkLabel LinkIndexBackPtr)

(RPAQ NC.SystemLinkLabels (LIST NC.ListContentsLinkLabel NC.BrowserContentsLinkLabel 
				  NC.FiledCardLinkLabel NC.SubBoxLinkLabel NC.DocBackPtrLinkLabel 
				  NC.LinkIndexBackPtrLinkLabel))

(RPAQ NC.UCASESystemLinkLabels (FOR Label in NC.SystemLinkLabels collect (U-CASE Label)))

(RPAQ NC.InitialLinkLabels (APPEND (QUOTE (Comment See Unspecified))
				     NC.SystemLinkLabels))

(RPAQ? NC.LinkIconSpaceInterLine 0)

(RPAQ? NC.LinkIconBorderWidth 1)

(RPAQ? NC.LinkIconSpaceOuterY 2)

(RPAQ? NC.LinkIconSpaceInnerY 1)

(RPAQ? NC.LinkIconSpaceOuterX 2)

(RPAQ? NC.LinkIconSpaceInnerX 3)

(RPAQ? NC.FileBoxLinkSpacer (CHARACTER 13))
(* * Links mechanism)

[DECLARE: EVAL@COMPILE 

(DATATYPE Link (UID SourceCard DestinationCard AnchorMode Label DisplayMode UserData))

(TYPERECORD LINKDISPLAYMODE (SHOWTITLEFLG SHOWLINKTYPEFLG ATTACHBITMAPFLG)
			      (TYPE? (AND (EQ (LENGTH DATUM)
						    4)
					      (EQ (CAR DATUM)
						    (QUOTE LINKDISPLAYMODE))
					      (FMEMB (CADR DATUM)
						       (QUOTE (T NIL FLOAT)))
					      (FMEMB (CADDR DATUM)
						       (QUOTE (T NIL FLOAT)))
					      (FMEMB (CADDDR DATUM)
						       (QUOTE (T NIL FLOAT)))
					      T)))
]
(/DECLAREDATATYPE (QUOTE Link)
		  (QUOTE (POINTER POINTER POINTER POINTER POINTER POINTER POINTER))
		  (QUOTE ((Link 0 POINTER)
			  (Link 2 POINTER)
			  (Link 4 POINTER)
			  (Link 6 POINTER)
			  (Link 8 POINTER)
			  (Link 10 POINTER)
			  (Link 12 POINTER)))
		  (QUOTE 14))
(DEFINEQ

(NC.GlobalLinkP
  (LAMBDA (Link)                                             (* kirk: "15-Nov-85 03:36")

          (* * Return T if Link is a Global to Global link)


    (EQ (QUOTE GlobalGlobal)
	  (fetch (Link AnchorMode) of Link))))

(NC.ChildLinkP
  (LAMBDA (Link)                                             (* rht: " 1-Aug-84 09:34")

          (* * Is link either to a filed card or sub filebox?)


    (OR (NC.ContentsLinkP Link)
	  (NC.SubContentsLinkP Link))))

(NC.ContentsLinkP
  (LAMBDA (Link)                                             (* kirk: "15-Nov-85 01:42")
    (COND
      ((EQ NC.FiledCardLinkLabel (fetch (Link Label) of Link))
	T)
      (T NIL))))

(NC.SubContentsLinkP
  (LAMBDA (Link)                                             (* kirk: "15-Nov-85 01:42")
    (COND
      ((EQ NC.SubBoxLinkLabel (fetch (Link Label) of Link))
	T)
      (T NIL))))

(NC.ReverseLinkLabelP
  (LAMBDA (Link ListOfLabels)                                (* kirk: "15-Nov-85 01:43")
                                                             (* Is the reverse of Link, i.e. with "←" prefix 
							     attached, present on ListOfLabels?)

          (* * rht 9/4/84: Modified so as to return the linklabel found or nil.)


    (PROG ((Label (fetch (Link Label) of Link)))
	    (RETURN (COND
			((FMEMB (PACK* (QUOTE ←)
					   Label)
				  (OR (LISTP ListOfLabels)
					(LIST ListOfLabels)))
			  Label))))))

(NC.MakeGlobalLink
  (LAMBDA (Window LinkLabel DestinationCard SourceCard DisplayMode)
                                                             (* rht: "15-Nov-85 15:58")

          (* * Make a new global link from DestinationCard to SourceCard with label LinkLabel and display mode DisplayMode.
	  Window is optional if SourceCard is specified, otherwise it is required.)



          (* * rht 11/9/84: Now checks result of NC.MakeLink before building link.)


    (LET ((Link (NC.MakeLink Window LinkLabel DestinationCard SourceCard DisplayMode (QUOTE
				 GlobalGlobal)
			       NIL NIL (QUOTE ATEND))))
         (AND Link (NC.SetGlobalLinks SourceCard (CONS Link (NC.FetchGlobalLinks SourceCard)))
		)
     Link)))

(NC.AddFromLink
  [LAMBDA (Link)                                             (* Randy.Gobbel "15-May-87 09:00")

          (* * Add Link to the FromLinks of the appropraite card on DatbaseStream)



          (* * kirk: 14Nov85: deleted use of of DatabaseStream)



          (* * rht 2/14/86: Now calls NC.UncacheLinks instead of two calls to NC.SetToLinks and NC.SetFromLinks.)



          (* * kef 7/22/86: Now only calls NC.PutFromLinks instead of NC.PutLinks because we can't be certain of owning the 
	  write locks on all of the Link parts.)



          (* * rg 5/15/87 now calls NC.LinksCachedP instead of NC.ActiveCardP)


    (LET ((Card (fetch (Link DestinationCard) of Link)))
         [COND
	   ((NC.LinksCachedP Card)
	     (NC.SetFromLinks Card (CONS Link (NC.FetchFromLinks Card)))
	     (NC.SetLinksDirtyFlg Card T))
	   (T (WITH.MONITOR (NC.FetchMonitor (fetch (Card NoteFile) of Card))
			    (NC.GetLinks Card)
			    (NC.SetFromLinks Card (CONS Link (NC.FetchFromLinks Card)))
			    (NC.SetLinksDirtyFlg Card T)
			    (NC.PutFromLinks Card)
			    (NC.UncacheLinks Card]
     Link])

(NC.AddLinkToGraphCard
  [LAMBDA (Window LinkOrLinkLabel DestinationCard)           (* rht: " 2-Nov-86 00:41")
                                                             (* Add a NOTECARD link to a graph card.
							     Pass the fact that this is a link-add to 
							     NC.GraphAddNodeFn via the NoteCardInsertingLink window
							     property)

          (* * rht 4/7/86: Now can handle insertion of multiple links by accepting label and/or destination card args.
	  Either or both can be nil.)



          (* * rht 11/2/86: Now checks result of NC.MakeLink before proceeding.)


    (LET [(Link (if (type? Link LinkOrLinkLabel)
		    then LinkOrLinkLabel
		  else (NC.MakeLink Window LinkOrLinkLabel DestinationCard]
         (if Link
	     then (WINDOWPROP Window (QUOTE NoteCardInsertingLink)
				  Link)
		    (EDITADDNODE Window])

(NC.AddLinkToSketchCard
  (LAMBDA (SketchWindow LinkOrLinkLabel DestinationCard)     (* rht: " 7-Apr-86 17:24")
                                                             (* Called from a SKETCH window to add a new NoteCard 
							     Link Icon to the sketch)

          (* * rht 11/9/84: Now checks result of NC.MakeLink before building link.)



          (* * rht 4/7/86: Now accepts LinkOrLinkLabel and DestinationCard args. Either or both can be nil.)


    (if (NOT (type? Link LinkOrLinkLabel))
	then (SETQ LinkOrLinkLabel (NC.MakeLink SketchWindow
						      (OR LinkOrLinkLabel
							    (NC.AskLinkLabel SketchWindow NIL NIL T 
									       T))
						      DestinationCard)))
    (NC.InsertLinkInSketch SketchWindow LinkOrLinkLabel)))

(NC.InsertLinkInSketch
  (LAMBDA (SketchWindow Link SketchWindowPosition)           (* kirk: " 4-Sep-86 18:42")

          (* * rht 8/20/85: Rewritten to use Richard's sketch programmer's interface. Eliminates references to sketch 
	  records.)



          (* * rht 10/1/85: Now checks that chosen position is actually within sketch, otherwise aborts by deleting link.)



          (* * fgh 2/6/86 Added Card argument.)



          (* * kirk 11Mar86 Deleted Card argument.)



          (* * rht 8/1/86: Now doesn't break if NULL Link.)



          (* * kirk 9/4/86 changed to use VIEWER.SCALE instead of old name)


    (AND Link (LET ((LinkIcon (NC.MakeLinkIcon Link))
		      (Scale (VIEWER.SCALE SketchWindow))
		      SketchIcon Size Position)
		     (SETQ Size (IMAGEBOXSIZE LinkIcon))
		     (SETQ Position (OR (POSITIONP SketchWindowPosition)
					    (GETBOXPOSITION (fetch (IMAGEBOX XSIZE)
								 of Size)
							      (fetch (IMAGEBOX YSIZE)
								 of Size)
							      NIL NIL SketchWindow)))
		     (if (AND (INSIDEP (DSPCLIPPINGREGION NIL SketchWindow)
					     Position)
				  (SETQ SketchIcon (SKETCH.IMAGE.OBJECT.ELEMENT LinkIcon Scale 
										    Position)))
			 then (SKETCH.ADD.ELEMENT SketchIcon SketchWindow)
				SketchIcon
		       else (NCP.DeleteLinks Link)
			      NIL)))))

(NC.InsertLinkInText
  (LAMBDA (TextStream LinkOrLinkLabel DestinationCard SourceCard DisplayMode CharacterPosition)
                                                             (* kirk: "15-Nov-85 11:53")

          (* * Add a link image object (and hence a link) to the text specified by TextStream. Link has LinkLabel, goes to 
	  DestinationCard and has DisplayMode. Returns the Link inserted.)



          (* * rht 11/9/84: Now checks result of NC.MakeLink before building link.)



          (* * rht 3/26/85: Now uses insert position to calculate the link after which to insert new link and passes this to 
	  NC.MakeLink.)


    (PROG ((Window (WINDOW.FROM.TEDIT.THING TextStream))
	     (TextObject (TEXTOBJ TextStream))
	     Link InsertPos)
	    (RETURN (COND
			((SETQ InsertPos (OR (if (AND (FIXP CharacterPosition)
							      (ILEQ CharacterPosition
								      (fetch (TEXTOBJ TEXTLEN)
									 of TextObject)))
						     then CharacterPosition)
						 (NC.CharPosFromTextObject TextObject)))
			  (if (SETQ Link (COND
				    ((type? Link LinkOrLinkLabel)
				      LinkOrLinkLabel)
				    (T (NC.MakeLink Window LinkOrLinkLabel DestinationCard 
						      SourceCard DisplayMode NIL NIL NIL
						      (NC.LinkAtCharPos InsertPos TextStream)))))
			      then (TEDIT.SETSEL TextObject InsertPos 0 (QUOTE LEFT))
				     (TEDIT.INSERT.OBJECT (NC.MakeLinkIcon Link)
							    (TEXTSTREAM TextObject)
							    InsertPos))
			  Link)
			(T (NC.ReportError "NC.InsertLinkInText" 
					     "No selection set when inserting link image object.")
			   NIL))))))

(NC.AddToLink
  (LAMBDA (Link LinkToInsertAfter BeingMovedFlg)             (* rht: "11-Aug-86 21:39")

          (* * Add Link to the ToLinks of the source card)



          (* * rht 3/26/85: Added LinkToInsertAfter arg which is either NIL, ATEND, or a link after which to insert new link.
	  If NIL, then insert at front of list. If ATEND then insert at end.)



          (* * kirk: 13Nov85: deleted second parameter (DatabaseSTream) and changed to call NC.CachedAddToLink.)



          (* * rht 2/14/86: Now calls NC.UncacheLinks instead of two calls to NC.SetToLinks and NC.SetFromLinks.)



          (* * rht 8/11/86: Added BeingMovedFlg. If non-nil, then delete instance of Link from ToLinks before inserting it in
	  new spot.)


    (LET ((SourceCard (fetch (Link SourceCard) of Link)))
         (COND
	   ((NC.ActiveCardP SourceCard)
	     (NC.CachedAddToLink Link SourceCard LinkToInsertAfter BeingMovedFlg))
	   (T (WITH.MONITOR (NC.FetchMonitor (fetch (Card NoteFile) of SourceCard))
			    (NC.GetLinks SourceCard)
			    (NC.CachedAddToLink Link SourceCard LinkToInsertAfter BeingMovedFlg)
			    (NC.PutLinks SourceCard)
			    (NC.UncacheLinks SourceCard)))))
    Link))

(NC.CachedAddToLink
  (LAMBDA (Link SourceCard LinkToInsertAfter BeingMovedFlg)
                                                             (* rht: "11-Aug-86 21:39")

          (* * Add Link to the cached ToLinks of the source card)



          (* * rht 3/26/85: Added LinkToInsertAfter arg which is either NIL, ATEND, or a link after which to insert new link.
	  If NIL, then insert at front of list. If ATEND then insert at end.)



          (* * kirk: 13Nov85: changed to use NoteFile instead of DatabaseStream)



          (* * kirk 15Nov85: deleted use of LinkID)



          (* * rht 8/11/86: Added BeingMovedFlg. If non-nil, then delete instance of Link from ToLinks before inserting it in
	  new spot.)


    (LET ((ToLinks (NC.FetchToLinks SourceCard)))
         (if BeingMovedFlg
	     then                                          (* Look for and delete old instance of the link.)
		    (if (NC.SameLinkP Link (CAR ToLinks))
			then (NC.SetToLinks SourceCard (SETQ ToLinks (CDR ToLinks)))
		      else (for RestOfToLinks on ToLinks thereis (NC.SameLinkP
									   Link
									   (CADR RestOfToLinks))
				finally (RPLACD RestOfToLinks (CDDR RestOfToLinks)))))
         (COND
	   ((AND ToLinks (type? Link LinkToInsertAfter))
                                                             (* Destructively insert after a given link.)
	     (for RestOfToLinks on ToLinks thereis (NC.SameLinkP LinkToInsertAfter
									 (CAR RestOfToLinks))
		finally (COND
			    (RestOfToLinks (RPLACD RestOfToLinks (CONS Link (CDR RestOfToLinks))
						     ))
			    (T (NC.SetToLinks SourceCard (NCONC1 ToLinks Link))))))
	   ((EQ LinkToInsertAfter (QUOTE ATEND))         (* Destructively insert at the end.)
	     (NC.SetToLinks SourceCard (NCONC1 ToLinks Link)))
	   (T                                                (* Stick it at the front.)
	      (NC.SetToLinks SourceCard (CONS Link ToLinks))))
         (NC.SetLinksDirtyFlg SourceCard T)
     Link)))

(NC.DelFromLink
  [LAMBDA (Link NoOrphanHookFlg)                             (* Randy.Gobbel " 8-May-87 12:42")

          (* * Delete a FromLink from its destination card Hook card to orphan if this is the last link.)



          (* * rht 11/15/84: Changed decision as to when to orphanize a card. Now must have deleted its last link 
	  (not just last subbox or filedcard link). Also checks that link doesn't point from card to itself.)



          (* * rht 12/1/84: Now doesn't do any work unless ID is valid, i.e. not DELETED or FREE.)



          (* * kirk: 13Nov85: deleted of use of DatabaseStream and changed to call CachedDelFromLink.)



          (* * kirk 29Jan86 Changed to a direct recursive call eliminating CachedDelFromLink. Added NC.FileInOrphanBox.)



          (* * rht 2/14/86: Now calls NC.UncacheLinks instead of two calls to NC.SetToLinks and NC.SetFromLinks.)



          (* * kef 7/30/86: Modified so that it only puts the FROMLINKS in the case of an inactiveP card.)



          (* * rg 5/8/87 check for active card should have been check for links cached ; fixed)


    (LET ((DestinationCard (fetch (Link DestinationCard) of Link)))
         (COND
	   ((NC.ValidCardP DestinationCard)
	     (COND
	       ((NC.LinksCachedP DestinationCard)
		 (NC.CachedDelFromLink Link DestinationCard)
		 (OR NoOrphanHookFlg (NC.FileInOrphanBox Link DestinationCard)))
	       (T (WITH.MONITOR (NC.FetchMonitor (fetch (Card NoteFile) of DestinationCard))
				(NC.GetLinks DestinationCard)
				(NC.CachedDelFromLink Link DestinationCard)
				(NC.PutFromLinks DestinationCard)
				(OR NoOrphanHookFlg (NC.FileInOrphanBox Link DestinationCard))
				(NC.UncacheLinks DestinationCard))
		  Link])

(NC.CachedDelFromLink
  (LAMBDA (Link DestinationCard)                             (* kirk: "29-Jan-86 17:07")

          (* * Delete a FromLink from its destination card Hook card to orphan if this is the last link.)



          (* * rht 11/15/84: Changed decision as to when to orphanize a card. Now must have deleted its last link 
	  (not just last subbox or filedcard link). Also checks that link doesn't point from card to itself.)



          (* * rht 12/1/84: Now doesn't do any work unless DestinationCard is valid, i.e. not DELETED or FREE.)



          (* * kirk: 13Nov85: copied out of NC.DelFromLink and changed to use NoteFile instead of DatabaseStream)



          (* * kirk 14Nov85: deleted use of LinkID)



          (* * rht 12/9/85: Changed Global var NC.TopLevelCards to call on the NC.FetchTopLevelCards fn.)



          (* * kirk 29Jan86 moved all the orphans stuff to NC.FileInOrphanBox called by NC.DelFromLink)


    (NC.SetFromLinks DestinationCard (DREMOVE (for OldLink in (NC.FetchFromLinks 
										  DestinationCard)
						     thereis (AND (NC.SameLinkP Link OldLink)
								      OldLink))
						  (NC.FetchFromLinks DestinationCard)))
    (NC.SetLinksDirtyFlg DestinationCard T)
    Link))

(NC.DelToLink
  [LAMBDA (Link)                                             (* Randy.Gobbel "15-May-87 16:54")

          (* * Delete ToLink spoecified by Link from cards on DatabasseStream)



          (* * rht 12/1/84: Now doesn't do any work unless SourceCard is valid, i.e. not DELETED or FREE.)



          (* * kirk: 13Nov85: changed to use NoteFile from card from link instead of DatabaseStream)



          (* * rht 2/14/86: Now calls NC.UncacheLinks instead of two calls to NC.SetToLinks and NC.SetFromLinks.)



          (* * rg 5/15/87 now calls NC.LinksCachedP instead of NC.ActiveCardP)


    (PROG ((SourceCard (fetch (Link SourceCard) of Link)))
	    (COND
	      ((NOT (NC.ValidCardP SourceCard)))
	      ((NC.LinksCachedP SourceCard)
		(NC.SetToLinks SourceCard (DREMOVE (for OldLink in (NC.FetchToLinks 
										       SourceCard)
							  thereis (AND (NC.SameLinkP Link 
											  OldLink)
									   OldLink))
						       (NC.FetchToLinks SourceCard)))
		(NC.SetLinksDirtyFlg SourceCard T))
	      (T (WITH.MONITOR (NC.FetchMonitor (fetch (Card NoteFile) of SourceCard)
						  "NC.AddFromLink")
			       (NC.GetLinks SourceCard)
			       (NC.SetToLinks SourceCard (DREMOVE (for OldLink
									 in (NC.FetchToLinks
										SourceCard)
									 thereis
									  (AND (NC.SameLinkP
										   Link OldLink)
										 OldLink))
								      (NC.FetchToLinks SourceCard)))
			       (NC.SetLinksDirtyFlg SourceCard T)
			       (NC.PutLinks SourceCard))
		 (NC.UncacheLinks SourceCard)))
	    (RETURN Link])

(NC.DelReferencesToCard
  [LAMBDA (SourceCard LinkOrDestinationCard Don'tCreateDeletedImageObjFlg)
                                                             (* rht: " 9-Jun-87 18:16")
                                                             (* The card specified by DestinationID is being 
							     deleted. Remove all references to it from the card 
							     specified by SourceCard)

          (* * kirk: 13Nov85: deleted use of DatabaseStream)



          (* * fgh 2/5/86 Added call to NC.ApplyFn)



          (* * kef 7/31/86: Added wrapping of write lock grabbing so that deactivating the card preserves the nesting of 
	  write lock ownership.)



          (* * kef 8/5/86: Added putting of Main Card data in ActiveP case. This is so that if subsequent other people bring 
	  up the card, they will see the fact that link card was deleted.)



          (* * kef 8/7/86: Now requires that the card is being edited rather than just Active to thwart direct write through 
	  to NoteFile.)



          (* * rht 8/11/86 Added ShrunkenFlg so that SourceCard is reshrunk afterwards if necessary.)



          (* * fgh 8/30/86 Changed APPLY* to NC.ApplyFn. Appears there might be trouble here since we don't appear to wait if
	  we can't get all the write permission locks. I am assuming for now that this is being taken care of at a higher 
	  level.)



          (* * rht 9/19/86: It was testing NC.BeingEditedP to decide whether had to obtain write permission, etc. I think 
	  NC.ActiveCardP is enough. NC.BeingEditedP is bogus anyway as it doesn't work on card's whose editors don't have 
	  processes like graph cards.)



          (* * rht 11/4/86: Now takes Don'tCreateDeletedImageObjFlg arg.)



          (* * rht 3/19/87: Undo Ken's server-related fix of 8/5/86, i.e. ripped out call to NC.PutMainCardData when card is 
	  active.)



          (* * rht 6/9/87: Now marks card dirty after calling DeleteLinksFn.)


    (LET ((ShrunkenFlg (NC.GetShrunkenWin SourceCard))
	  NoteCardType Substance)
         [COND
	   ((NC.ActiveCardP SourceCard)
	     (SETQ NoteCardType (NC.RetrieveType SourceCard))
	     (SETQ Substance (NC.FetchSubstance SourceCard))
	     (NC.ApplyFn DeleteLinksFn SourceCard LinkOrDestinationCard Don'tCreateDeletedImageObjFlg)
	     (NC.MarkCardDirty SourceCard))
	   (T (WITH.MONITOR (NC.FetchMonitor (fetch (Card NoteFile) of SourceCard))
			    (if (NC.ValidCardP (NC.GetNoteCard SourceCard))
				then (RESETLST (RESETSAVE (for CardPart
								   in (QUOTE (SUBSTANCE TOLINKS 
										    GLOBALTOLINKS 
											 PROPLIST))
								   do (NC.ApplyFn 
									  ObtainWritePermissionFn 
										    SourceCard 
										    CardPart))
								(BQUOTE (NC.DeactivateCard , 
										       SourceCard)))
						   (SETQ NoteCardType (NC.RetrieveType SourceCard)
						     )
						   (SETQ Substance (NC.FetchSubstance SourceCard))
						   (NC.ApplyFn DeleteLinksFn SourceCard 
							       LinkOrDestinationCard 
							       Don'tCreateDeletedImageObjFlg)
						   (NC.PutMainCardData SourceCard]
         (NC.DelReferencesToCardFromGlobalList SourceCard LinkOrDestinationCard)
         (AND ShrunkenFlg (SHRINKW (NC.FetchWindow SourceCard])

(NC.DelReferencesToCardFromGlobalList
  [LAMBDA (SourceCard LinkOrDestinationCard)                 (* rht: " 5-Jun-87 14:56")

          (* * LinkOrDestinationCard is being deleted. Remove all references to it from the global links list of SourceCard)



          (* * rht 11/19/84: Fixed a naming typo changing ID to SourceCard everywhere. Also fixed DestinationID arg to be 
	  LinkOrDestinationID.)



          (* * kirk: 14Nov85: deleted use of of DatabaseStream and LinkID and changed EQs to NC.Same*Ps)



          (* * rg 5/15/87 now calls NC.LinksCachedP instead of NC.ActiveCardP)



          (* * rht&pmi 6/5/87: Added call to NC.UncacheLinks.)


    (COND
      ((NC.LinksCachedP SourceCard)
	(NC.SetGlobalLinks SourceCard (for Link in (NC.FetchGlobalLinks SourceCard)
					   when (if (NC.CardP LinkOrDestinationCard)
						      then (NOT (NC.SameCardP
								      LinkOrDestinationCard
								      (fetch (Link DestinationCard)
									 of Link)))
						    else (NOT (NC.SameLinkP 
									    LinkOrDestinationCard 
										  Link)))
					   collect Link)))
      (T (WITH.MONITOR (NC.FetchMonitor (fetch (Card NoteFile) of SourceCard))
		       (NC.GetLinks SourceCard)
		       (NC.SetGlobalLinks SourceCard
					    (for Link in (NC.FetchGlobalLinks SourceCard)
					       when (if (NC.CardP LinkOrDestinationCard)
							  then (NOT (NC.SameCardP
									  LinkOrDestinationCard
									  (fetch (Link 
										  DestinationCard)
									     of Link)))
							else (NOT (NC.SameLinkP 
									    LinkOrDestinationCard 
										      Link)))
					       collect Link))
		       (NC.PutLinks SourceCard)
		       (NC.UncacheLinks SourceCard])

(NC.DeleteLink
  [LAMBDA (Link NoOrphanHookFlg Don'tDelLinkIconFlg)         (* rht: " 9-Jun-87 18:27")

          (* * Delete a link with the option of not putting an orphan hook in case of last filing link.
	  Also option of not deleting link icon from the source card's substance.)



          (* * fgh 5/2/86 Included calls to NC.DelReferencesToCardFromShowLinks in order to clean up ShowLinks windows when 
	  links are deleted.)



          (* * kef 7/17/86: Added the requirement that the write permissions be obtained for the TOLINKS of the Source Card 
	  and the FROMLINKS of the Destination Card.)



          (* * kef 7/22/86: Puts the links for the Destination Card now right away while we are still holding onto the write 
	  lock for the FROMLINKS.)



          (* * kef 7/30/86: Modified to check for Client's concept of whether he owns the write lock or not, thus deciding 
	  whether or not to setup the release of the write lock afterwards.)



          (* * fgh 8/30/86 Adpated to use NC.IfCardPartNotBusy. Note use of ERROR!. Unfortunately, it appears that the only 
	  way to keep TEDIT from deleting the Link's Image Object is to bail out but good. The control structure here works 
	  because NC.IfCardPartNotBusy returns NIL if the CardPart is busy. The Ts at the end as the last SExpr in each call 
	  to NC.IfCardPartNotBusy insure that NC.IfCardPartNotBusy returns non-NIL otherwise.)



          (* * rht 9/29/86: Removed the call to NC.PutFromLinks. Looks to me like it'll get called anyway by NC.DelFromLink 
	  if necessary.)



          (* * rht 10/6/86: Delete crossfilelink cards when their links are deleted.)



          (* * rht 11/4/86: Now takes Don'tCreateDeletedImageObjFlg arg.)



          (* * rht 11/13/86: Undid my change of 11/4/86.)



          (* * rg 3/11/87 changed call of NC.DeleteNoteCard to NC.DeleteNoteCardInternal)



          (* * rht 3/13/87: Broke out code into the NC.DeleteFromLink and NC.DeleteToLink functions.)



          (* * rht 5/26/87: Changed to match reduced functionality of NC.ValidLinkP, now checks that DestinationCard and 
	  SourceCard are valid cards.)



          (* * rht 6/9/87: Slight modification of previous fix; moved check for valid source and destination card to be 
	  around the relevant calls to NC.DeleteFromLink and NC.DeleteToLink.)


    (AND (NC.ValidLinkP Link)
	   (LET ((SourceCard (fetch (Link SourceCard) of Link))
		 (DestinationCard (fetch (Link DestinationCard) of Link)))
	        (WITH.MONITOR (NC.FetchMonitor (fetch (Card NoteFile) of SourceCard))
			      (OR (NC.IfCardPartNotBusy DestinationCard (QUOTE FROMLINKS)
							  (OR (NC.IfCardPartNotBusy
								  DestinationCard
								  (QUOTE TOLINKS)
								  (AND (NC.ValidCardP 
										  DestinationCard)
									 (NC.DeleteFromLink Link 
										  NoOrphanHookFlg))
								  (AND (NC.ValidCardP SourceCard)
									 (NC.DeleteToLink Link 
									      Don'tDelLinkIconFlg))
								  (replace (Link UID) of Link
								     with -1)
								  T)
								(ERROR!))
							  T)
				    (ERROR!])

(NC.DeleteToLink
  [LAMBDA (Link Don'tDelLinkIconFlg)                         (* rht: "26-Mar-87 19:15")

          (* * This is part of what used to be the innards of NC.DeleteLink.)



          (* * rht 3/26/87: Changed to call NC.DeleteNoteCardInternal with QuietFlg on.)


    (AND (NC.ValidLinkP Link)
	   (LET ((SourceCard (fetch (Link SourceCard) of Link)))
	        (WITH.MONITOR (NC.FetchMonitor (fetch (Card NoteFile) of SourceCard))
			      (OR Don'tDelLinkIconFlg (NC.DelReferencesToCard SourceCard Link))
			      (NC.DelToLink Link)
			      (NC.DelReferencesToCardFromShowLinks SourceCard Link)

          (* * Delete cross file link cards when their links are deleted.)


			      (if (NC.CrossFileLinkCardP SourceCard)
				  then (NC.DeleteNoteCardInternal SourceCard T])

(NC.DeleteFromLink
  [LAMBDA (Link NoOrphanHookFlg)                             (* rht: "26-Mar-87 18:43")

          (* * This is part of what used to be the innards of NC.DeleteLink.)



          (* * rht 3/26/87: Changed to call NC.DeleteNoteCardInternal with QuietFlg on.)


    (AND (NC.ValidLinkP Link)
	   (LET ((DestinationCard (fetch (Link DestinationCard) of Link)))
	        (WITH.MONITOR (NC.FetchMonitor (fetch (Card NoteFile) of DestinationCard))
			      (NC.DelFromLink Link NoOrphanHookFlg)
			      (NC.DelReferencesToCardFromShowLinks DestinationCard Link)

          (* * Delete cross file link cards when their links are deleted.)


			      (if (NC.CrossFileLinkCardP DestinationCard)
				  then (NC.DeleteNoteCardInternal DestinationCard T])

(NC.DeletedLinkDisplayFn
  (LAMBDA (ImageObj Stream)                                  (* fgh: "14-May-85 13:04")
    (SELECTQ (IMAGESTREAMTYPE Stream)
	       (DISPLAY                                      (* This is being displayed on the screen)
			(PROG ((ImageBox (NC.DeletedLinkImageBoxFn ImageObj Stream))
				 Bottom XSize YSize Left)
			        (DSPXPOSITION (IPLUS 3 (SETQ Left (DSPXPOSITION NIL Stream)))
						Stream)
			        (SETQ XSize (fetch (IMAGEBOX XSIZE) of ImageBox))
			        (SETQ YSize (fetch (IMAGEBOX YSIZE) of ImageBox))
			        (SETQ Bottom (IDIFFERENCE (DSPYPOSITION NIL Stream)
							      (fetch (IMAGEBOX YDESC)
								 of ImageBox)))
                                                             (* don't bother to errorset this)
			        (DSPFONT (PROG1 (DSPFONT NC.LinkIconFont Stream)
						    (PRIN1 "Deleted" Stream))
					   Stream)
			        (BITBLT NIL NIL NIL Stream (ADD1 Left)
					  (IPLUS Bottom 2)
					  (IDIFFERENCE XSize 2)
					  (IDIFFERENCE YSize 4)
					  (QUOTE TEXTURE)
					  (COND
					    ((EQ (DSPOPERATION NIL Stream)
						   (QUOTE ERASE))
					      (QUOTE ERASE))
					    (T (QUOTE INVERT)))
					  BLACKSHADE)))
	       ((PRESS INTERPRESS)                           (* It's being displayed on a press file)
		 NIL)
	       NIL)))

(NC.DeletedLinkGetFn
  [LAMBDA (InputStream TextStream)                           (* rht: "10-Jun-87 14:56")

          (* * rht 6/10/87: Now skips carriage return if any.)


    (if (EQ (\PEEKBIN InputStream)
		13)
	then (BIN InputStream))
    (BIN InputStream)
    NC.DeletedLinkImageObject])

(NC.DeletedLinkImageBoxFn
  (LAMBDA (ImageObj Stream)                                  (* fgh: " 1-Apr-85 18:07")
    (PROG ((Font NC.DefaultFont))
	    (RETURN (create IMAGEBOX
				XSIZE ← (IPLUS 6 (STRINGWIDTH "Deleted" Font))
				YSIZE ← (IPLUS 4 (FONTPROP Font (QUOTE HEIGHT)))
				YDESC ← (IPLUS 2 (FONTPROP Font (QUOTE DESCENT)))
				XKERN ← 0)))))

(NC.DeletedLinkPutFn
  (LAMBDA (ImageObj OutputStream)                            (* fgh: "26-Feb-84 01:04")
    (BOUT OutputStream 1)))

(NC.DeletedLinkImageObjP
  (LAMBDA (ImageObj)                                         (* rht: "12-Jun-85 10:07")

          (* * Returns non-nil if ImageObj is a deleted link icon.)


    (AND (IMAGEOBJP ImageObj)
	   (EQ (FUNCTION NC.DeletedLinkDisplayFn)
		 (IMAGEOBJPROP ImageObj (QUOTE DISPLAYFN))))))

(NC.CheckForOrphanDelete
  [LAMBDA (SourceCard DestinationCard SourceWindow)          (* Randy.Gobbel "15-May-87 16:40")

          (* Check to make sure we are not deleteing the last reference to a note card from the orphan card.
	  Last references are okay to delete it since they will be put in the orphan card. But deleting last reference from 
	  the orphan card is very, very bad. Warn the user.)



          (* * kirk: 14Nov85: changed EQs to NC.Same*Ps)



          (* * rht 7/4/86: Brought up to date with 1.3k. E.g. took out mention of NC.OrphansID)



          (* * rg 5/15/87 now calls NC.LinksCachedP instead of NC.ActiveCardP)


    (LET [(FilingLinks (LIST NC.SubBoxLinkLabel NC.FiledCardLinkLabel))
	  (OrphansCard (fetch (NoteFile OrphansCard) of (fetch (Card NoteFile) of SourceCard]
         (AND (NC.SameCardP SourceCard OrphansCard)
		(for Link in (COND
				   ((NC.LinksCachedP DestinationCard)
				     (NC.FetchFromLinks DestinationCard))
				   (T (NC.GetLinks DestinationCard)
				      (NC.FetchFromLinks DestinationCard)))
		   when (FMEMB (fetch (Link Label) of Link)
				   FilingLinks)
		   unless (NC.SameCardP (fetch (Link SourceCard) of Link)
					    OrphansCard)
		   never Link)
		(NC.PrintMsg (NC.FetchWindow SourceCard)
			       T "You have just deleted the last filing link to " (NC.RetrieveTitle
				 DestinationCard)
			       (CHARACTER 13)
			       "The Search operation can be used to find it."])

(NC.FileInOrphanBox
  [LAMBDA (Link DestinationCard)                             (* rht: "20-Apr-87 14:25")

          (* * Hook card to orphan if this is the last link.)



          (* * rht 7/4/86: Changed to call NC.FetchSpecialCards so that RegistryCard, etc. won't have to be filed somewhere.)



          (* * rg 11/18/86 Added globalvars declaration)



          (* * rht 4/20/87: Changed to work on new cards unless they're displayed on screen. Also removed reference to 
	  long-demised NC.OrphansID.)


    (LET ((SourceCard (fetch (Link SourceCard) of Link)))
         [AND [NOT (FMEMB DestinationCard (NC.FetchSpecialCards (fetch (Card NoteFile)
									   of DestinationCard]
		(NOT (NC.SameCardP SourceCard DestinationCard))
		(NOT (AND (NC.FetchNewCardFlg DestinationCard)
			      (NC.FetchWindow DestinationCard)))
		[NOT (NC.SameCardP SourceCard (fetch (NoteFile OrphansCard)
						     of (fetch (Card NoteFile) of SourceCard]
		(COND
		  ((OR (NULL (NC.FetchFromLinks DestinationCard))
			 (for FromLink in (NC.FetchFromLinks DestinationCard)
			    always (NC.SameCardP (fetch (Link SourceCard) of FromLink)
						     DestinationCard)))
		    (NC.PrintMsg NIL T "You have just removed the last link to " (NC.RetrieveTitle
				     DestinationCard)
				   "."
				   (CHARACTER 13)
				   "It is being filed in the Orphan FileBox.")
		    (NC.HookToOrphanCard DestinationCard))
		  ([for FromLink in (NC.FetchFromLinks DestinationCard)
		      never (FMEMB (fetch (Link Label) of FromLink)
				       (QUOTE (SubBox FiledCard]
		    (NC.PrintMsg NIL T "You have just unfiled " (NC.RetrieveTitle DestinationCard)
				   " from its last filebox."
				   (CHARACTER 13)
				   "It is being filed in the Orphan FileBox.")
		    (NC.HookToOrphanCard DestinationCard]
     Link])

(NC.HookToOrphanCard
  [LAMBDA (Card OrphansCard)                                 (* Randy.Gobbel "19-Feb-87 16:57")
                                                             (* The last reference to the card has just been 
							     deleted. Hook this card to the orphan card so it 
							     doesn't get lost forever)

          (* * kirk: 14Nov85: deleted use of DatabaseStream and changed EQs to NC.Same*Ps)



          (* * kef 7/29/86: Took out the calls to NC.PutMainCardData, NC.PutLinks, and NC.DeactivateCard after 
	  NC.MakeFilingLink. The reason is that this is done within NC.InsertLinkBeforeMarker or NC.InsertLinkInOrdering, 
	  which are called by NC.MakeFilingLink. Therefore, don't do this twice.)



          (* * rg 2/19/87 Return parent as value)


    (DECLARE (GLOBALVARS NC.FiledCardLinkLabel NC.SubBoxLinkLabel))
    (LET ((NoteFile (fetch (Card NoteFile) of Card)))
         (OR OrphansCard (SETQ OrphansCard (fetch (NoteFile OrphansCard) of NoteFile)))
         (COND
	   ((NC.ActiveCardP OrphansCard)
	     (COND
	       ((NC.MakeFilingLink OrphansCard Card (if (NC.FileBoxP Card T)
							  then NC.SubBoxLinkLabel
							else NC.FiledCardLinkLabel))
		 OrphansCard)))
	   (T (WITH.MONITOR (NC.FetchMonitor NoteFile)
			    (NC.GetNoteCard OrphansCard)   (* These no longer needed expressions used to go after
							     the NC.MakeFilingLink below: 
							     (NC.PutMainCardData OrphansCard) 
							     (NC.PutLinks OrphansCard) (NC.DeactivateCard 
							     OrphansCard))
			    (COND
			      ((NC.MakeFilingLink OrphansCard Card (if (NC.FileBoxP Card T)
									 then NC.SubBoxLinkLabel
								       else NC.FiledCardLinkLabel))
				OrphansCard])

(NC.InsertLinkBeforeMarker
  (LAMBDA (SourceCard DestinationCard LinkLabel DisplayMode Marker# NoSpacerFlg)
                                                             (* fgh: " 1-Sep-86 13:21")
                                                             (* Insert a link to DestinationCard in SourceCard just
							     before the Marker#'th place marker object)

          (* * rht 12/7/84: Now returns the newly created link.)



          (* * rht 9/13/85: When card is inactive, there won't be a current cursor position. So if no marker# is specified, 
	  then insert at end.)



          (* * kirk: 14Nov85: deleted use of of DatabaseStream)



          (* * kef 7/28/86: Added call to obtain write locks on SUBSTANCE and all LINKS in the case when the card is not 
	  NC.ActiveCardP.)



          (* * kef 7/31/86: Added "balancing" of write lock ownership with NC.DeactivateCard.)



          (* * kef 8/7/86: Now cached changes only occur if the card is currently being edited.)



          (* * fgh 8/30/86 Adpated to NC.IfCardPartsNotBusy)



          (* * fgh 9/1/86 Rescinded kef's change of 8/7/86. This causes active but nit visible cards to be written over even 
	  if they are dirty.)


    (LET (Objects TextStream TextObject (Spacer (CONCAT (CHARACTER 13)))
		  Link BusyPart)
         (COND
	   ((NC.ActiveCardP SourceCard)
	     (NC.IfMultipleCardPartsNotBusy SourceCard (SUBSTANCE TOLINKS FROMLINKS GLOBALTOLINKS)
					    (SETQ TextObject (TEXTOBJ (SETQ TextStream
									    (NC.FetchSubstance
									      SourceCard))))
					    (COND
					      ((AND (FIXP Marker#)
						      (SETQ Objects (TEDIT.LIST.OF.OBJECTS
							  TextObject
							  (FUNCTION NC.PlaceMarkerP))))
						(COND
						  ((EQ Marker# 0)
						    (TEDIT.SETSEL TextStream 1 0 (QUOTE LEFT)))
						  ((IGREATERP Marker# (FLENGTH Objects))
						    (TEDIT.SETSEL TextStream
								    (ADD1 (fetch (TEXTOBJ
										       TEXTLEN)
									       of TextObject))
								    0
								    (QUOTE RIGHT)))
						  (T (TEDIT.SETSEL TextStream
								     (CADAR (FNTH Objects Marker#)
									      )
								     0
								     (QUOTE LEFT)))))
					      (NC.MarkersInFileBoxesFlg (TEDIT.SETSEL TextStream
											(GETEOFPTR
											  TextStream)
											0
											(QUOTE
											  RIGHT))))
					    (SETQ Link (NC.InsertLinkInText TextStream LinkLabel 
										DestinationCard 
										SourceCard 
										DisplayMode))
					    (COND
					      ((NULL NoSpacerFlg)
						(TEDIT.INSERT TextStream Spacer)))))
	   (T (WITH.MONITOR (NC.FetchMonitor (fetch (Card NoteFile) of SourceCard))
			    (NC.IfMultipleCardPartsNotBusy
			      SourceCard
			      (SUBSTANCE TOLINKS FROMLINKS GLOBALTOLINKS)
			      (COND
				((NC.CardP (NC.GetNoteCard SourceCard))

          (* * This next set of obtain write locks is only to make sure that we come out even when NC.DeactivateCard releases
	  the write locks. Note that the SourceCard will be deactivated upon exit of the RESETLST, as set up by the 
	  RESETSAVE.)


				  (RESETSAVE (for CardPart in (QUOTE (SUBSTANCE TOLINKS 
										    GLOBALTOLINKS 
											PROPLIST))
						  do (APPLY* (fetch (Card 
									  ObtainWritePermissionFn)
								    of SourceCard)
								 SourceCard CardPart))
					       (BQUOTE (NC.DeactivateCard , SourceCard)))
				  (SETQ TextObject (TEXTOBJ (SETQ TextStream (
								    NC.FetchSubstance SourceCard))))
				  (COND
				    ((AND (FIXP Marker#)
					    (SETQ Objects (TEDIT.LIST.OF.OBJECTS
						TextObject
						(FUNCTION NC.PlaceMarkerP))))
				      (COND
					((EQ Marker# 0)
					  (TEDIT.SETSEL TextStream 1 0 (QUOTE LEFT)))
					((IGREATERP Marker# (FLENGTH Objects))
					  (TEDIT.SETSEL TextStream (ADD1 (fetch (TEXTOBJ
											TEXTLEN)
										of TextObject))
							  0
							  (QUOTE RIGHT)))
					(T (TEDIT.SETSEL TextStream (CADAR (FNTH Objects 
										       Marker#))
							   0
							   (QUOTE LEFT)))))
				    (T                       (* Cursor has no position in closed Tedit win, so just
							     set selection to end.)
				       (TEDIT.SETSEL TextStream (ADD1 (fetch (TEXTOBJ TEXTLEN)
									     of TextObject))
						       0
						       (QUOTE RIGHT))))
				  (SETQ Link (NC.InsertLinkInText TextStream LinkLabel 
								      DestinationCard SourceCard 
								      DisplayMode))
				  (COND
				    ((NULL NoSpacerFlg)
				      (TEDIT.INSERT TextStream Spacer)))
				  (NC.PutMainCardData SourceCard)
				  (NC.PutLinks SourceCard)))))))
     Link)))

(NC.LinkLabelP
  (LAMBDA (Link ListOfLabels)                                (* kirk: "15-Nov-85 01:43")

          (* * rht 9/4/84: Modified so as to return the linklabel if found or nil.)


    (CAR (FMEMB (fetch (Link Label) of Link)
		    (OR (LISTP ListOfLabels)
			  (LIST ListOfLabels))))))

(NC.IDAlphOrder
  (LAMBDA (LinkOrCard1 LinkOrCard2)                          (* rht: "18-Jul-86 15:20")

          (* * Return T if Card1's title is alphabetically before Card2's)



          (* * rht 7/18/86: Now can accept either links or cards as arg.)


    (ALPHORDER (NCP.CardTitle (if (NC.ValidLinkP LinkOrCard1)
				      then (fetch (Link DestinationCard) of LinkOrCard1)
				    else LinkOrCard1))
		 (NCP.CardTitle (if (NC.ValidLinkP LinkOrCard2)
				      then (fetch (Link DestinationCard) of LinkOrCard2)
				    else LinkOrCard2)))))

(NC.MakeLink
  [LAMBDA (Window LinkLabel DestinationCard SourceCard DisplayMode AnchorMode Message NoDisplayFlg 
		  LinkToInsertAfter CrossFileLinksMode)      (* Randy.Gobbel " 2-Apr-87 15:38")

          (* * Make a link from (OR Window SourceCard) to DestinationCard with linklabel of LinkLabel)



          (* * rht 1/12/85: If need to create a new card, then now shows card type menu near window of SourceID.)



          (* * rht 1/13/85: Added extra args Message and NoDisplayFlg.)



          (* * rht 3/26/85: Added LinkToInsertAfter arg which should be NIL or a link to insert the new To link after.
	  If NIL, then insert at front of ToLinks.)



          (* * kirk 9/23/85: took out GETPROMPTWINDOW call for asknotecardtype)



          (* * kirk: 14Nov85: changed NC.CoerceToID to to NC.CoerceToCard)



          (* * fgh 11/16/85 Changed from PROG to LET and used COND to contyrol returnmed value.)



          (* * fgh 2/5/86 Changed call DefaultLinkDisplayMode to FetchLinkDisplayMode)



          (* * fgh 6/5/86 Now calls AskLinkLabel if LinkLabel arg is NIL)



          (* * rht 7/4/86: Added check for readonly card.)



          (* * kef 7/17/86: Added calls to grab the write permission on the appropriate card parts.)



          (* * kef 7/22/86: Saves the links on the Destination Card now right away, while still holding onto the FROMLINKS 
	  write lock.)



          (* * fgh 8/30/86 Adpated to use NC.IfCardPartNotBusy.)



          (* * rht 9/29/86: Tossed Ken's call to NC.PutFromLinks; It was the cause of too many nasty breaks.
	  Also made syntactic fixes.)



          (* * rht 10/4/86: Now handles cross file links. New arg CrossFileLinksMode determines whether cross-file link will 
	  be two-way, i.e. will destination card know it's being linked to.)



          (* * rht 11/10/86: Now creates new crossfile link if Destination card is a CrossFileLink card that we didn't just 
	  create.)



          (* * rht 11/14/86: Now checks if non-nil DestinationCard before trying to do cross-filelink stuff.)



          (* * pmi 12/5/86: Modified message to NC.SelectNoteCards to mention SHIFT-selection.)



          (* * rht 12/9/86: Throws out JustCreatedFlg marker stuff.)



          (* * pmi 12/12/86: Removed obsolete ReturnLinksFlg argument in call to NC.SelectNoteCards.)



          (* * rht 12/16/86: Fixed bug whereby electing not to open notefile containing crossfilelink dest card caused 
	  break.)



          (* * rht 12/16/86: Now passes Window down to NC.GetCrossFileLinkDestCard.)



          (* * rg 3/18/87 added NCP.WithLockedCards wrapper)


    (DECLARE (GLOBALVARS NC.SelectingSingleCardMenu NC.NewCrossFileLinksTwoWayFlg))
    (OR SourceCard (SETQ SourceCard (NC.CoerceToCard Window)))
    (AND (NC.CheckForNotReadOnly SourceCard Window "Can't make links in ")
	   (NCP.WithLockedCards
	     (LET (Link Type)
	          (OR Window (SETQ Window (NC.FetchWindow SourceCard)))
	          (OR Message (SETQ Message 
			  "Please shift-select the Card or Box to be linked to."))
	          (OR LinkLabel (SETQ LinkLabel (NC.AskLinkLabel Window NIL NIL T NIL)))
	          (OR DestinationCard (SETQ DestinationCard
			  (NC.SelectNoteCards T [FUNCTION (LAMBDA (Card)
						    (COND
						      ((NOT (NC.SameCardP Card SourceCard))
							T)
						      (T (NC.PrintMsg Window T 
							     "A Card/Box cannot link to itself. "
									(CHARACTER 13)
									"Selection ignored."
									(CHARACTER 13))
							 NIL]
						NC.SelectingSingleCardMenu SourceCard Message)))
	          [if (EQ DestinationCard (QUOTE *New% Card*))
		      then (SETQ DestinationCard (AND (SETQ Type (NC.AskNoteCardType
								(WINDOWREGION Window)))
							    (NC.CoerceToCard
							      (NC.MakeNoteCard Type
										 (fetch
										   (Card NoteFile)
										    of SourceCard)
										 NIL NoDisplayFlg]

          (* * If we're trying to link to a CrossFileLink card, then check whether card was just created.
	  If so, then it's the first link, otherwise we make a new CrossFileLink.)


	          (AND DestinationCard (NC.CrossFileLinkCardP DestinationCard)
			 (SETQ DestinationCard (NC.GetCrossFileLinkDestCard DestinationCard 
										Window))
			 (NC.SetUserDataProp DestinationCard (QUOTE JustCreatedFlg)
					       NIL))
	          (if DestinationCard
		      then (NC.IfCardPartNotBusy
			       DestinationCard
			       (QUOTE FROMLINKS)
			       (NC.IfCardPartNotBusy
				 SourceCard
				 (QUOTE TOLINKS)

          (* * If have cross-file link, then make two new crossfilelink cards, one per notefile. Make global link over there 
	  from crossfilelink card to DestinationCard and local link here from SourceCard to crossfilelink card.)


				 [if (NOT (NC.SameNoteFileP (fetch (Card NoteFile)
								     of SourceCard)
								  (fetch (Card NoteFile)
								     of DestinationCard)))
				     then (LET [(CrossFileLinksTwoWayFlg
						    (OR (EQ CrossFileLinksMode (QUOTE TWOWAY))
							  (AND (NULL CrossFileLinksMode)
								 NC.NewCrossFileLinksTwoWayFlg]
					         (if CrossFileLinksTwoWayFlg
						     then (NC.MakeGlobalLink Window LinkLabel 
										 DestinationCard
										 (
								       NC.CreateCrossFileLinkCard
										   DestinationCard 
										   SourceCard T)
										 DisplayMode))
					         (SETQ DestinationCard (NC.CreateCrossFileLinkCard
						     SourceCard DestinationCard 
						     CrossFileLinksTwoWayFlg]
				 [SETQ Link (create Link
							UID ← (NC.MakeUID)
							SourceCard ← SourceCard
							DestinationCard ← DestinationCard
							AnchorMode ← AnchorMode
							Label ← LinkLabel
							DisplayMode ← (OR DisplayMode (
									  NC.FetchLinkDisplayMode
									      SourceCard]
				 (NC.AddToLink Link LinkToInsertAfter)
				 (NC.AddFromLink Link)
				 Link))
		    else NIL])

(NC.CachedMakeFilingLink
  [LAMBDA (SourceCard DestinationCard LinkType DisplayMode IgnoreMarkersFlg)
                                                             (* rht: "13-Oct-86 12:31")

          (* * Assumes SourceCard is cached. Inserts a link to DestinationCard into SourceCard. Unless IgnoreMarkersFlg is 
	  non-nil or there's no marker for type of DestinationCard, then will insert somewhere between appropriate marker and
	  either the next marker or the end of SourceCard substance using SourceCard's OrderingFn to determine where to 
	  insert. If there's no OrderingFn, but there is an appropriate marker, then stick at end of group.
	  If no marker and no OrderingFn, then stick wherever cursor is.)


    (LET ((OrderingFn (NC.GetProp SourceCard (QUOTE OrderingFn)))
	  (TextStream (NC.FetchSubstance SourceCard))
	  StartLoc EndLoc TextObject FoundMarkerFlg Link)
         (SETQ TextObject (TEXTOBJ TextStream))
         [OR IgnoreMarkersFlg (SETQ FoundMarkerFlg (for MarkerPairs
							  on (TEDIT.LIST.OF.OBJECTS
								 TextObject
								 (FUNCTION NC.PlaceMarkerP))
							  eachtime (BLOCK)
							  do (if (NC.MarkerMatchesCardP
								       (CAAR MarkerPairs)
								       DestinationCard)
								   then
								    (SETQ StartLoc (CADAR 
										      MarkerPairs))
								    [SETQ EndLoc
								      (OR (CADADR MarkerPairs)
									    (ADD1 (GETEOFPTR
										      TextStream]
								    (RETURN T))
							  finally (SETQ StartLoc 1)
								    (SETQ EndLoc
								      (ADD1 (GETEOFPTR TextStream]
         [COND
	   [OrderingFn (for ObjectPair in (TEDIT.LIST.OF.OBJECTS TextObject
								       (FUNCTION 
									 NC.LinkIconImageObjP))
			  eachtime (BLOCK) until (GREATERP (CADR ObjectPair)
								   EndLoc)
			  when [AND (LEQ StartLoc (CADR ObjectPair))
					(APPLY* OrderingFn DestinationCard
						  (fetch (Link DestinationCard)
						     of (NC.FetchLinkFromLinkIcon (CAR 
										       ObjectPair]
			  do (TEDIT.SETSEL TextStream (CADR ObjectPair)
					       0
					       (QUOTE LEFT))
			       (RETURN)
			  finally (TEDIT.SETSEL TextStream EndLoc 0 (QUOTE RIGHT]
	   (FoundMarkerFlg (TEDIT.SETSEL TextStream EndLoc 0 (QUOTE RIGHT]
         (PROG1 (NC.InsertLinkInText TextStream LinkType DestinationCard SourceCard DisplayMode)
		  (TEDIT.INSERT TextStream NC.FileBoxLinkSpacer])

(NC.MakeFilingLink
  [LAMBDA (SourceCard DestinationCard LinkType)              (* rht: "13-Oct-86 12:09")

          (* Make a link from FileBox to DestinationCard If card is a regular card, insert link at end of contents card.
	  If card is a supercontents of contents card then insert just before the subcontents marker 
	  (i.e., at end of super contents list). If card is a subcontents card then insert just before note cards marker 
	  (i.e., at end of subcontents list))



          (* * rht 10/25/85: Now checks if card's window is shrunken. If so, shrink it back at the end.)



          (* * kirk: 14Nov85: deleted use of of DatabaseStream)



          (* * fgh 11/15/85 Adapted from and intended to replace NC.MakeAContentsHook)



          (* * kirk 11Apr86 took out hard coded link display mode)



          (* * rht 10/13/86: Totally overhauled. Now calls NC.CachedMakeFilingLink which does both inserting before markers 
	  and inserting in ordering.)


    (if (NC.ActiveCardP SourceCard)
	then [NC.IfMultipleCardPartsNotBusy SourceCard (SUBSTANCE TOLINKS FROMLINKS GLOBALTOLINKS)
					      (LET ((ShrunkenFlg (NC.GetShrunkenWin SourceCard)))
					           (PROG1 (NC.CachedMakeFilingLink SourceCard 
										  DestinationCard 
										       LinkType)
							    (AND ShrunkenFlg (SHRINKW
								     (NC.FetchWindow SourceCard]
      else (WITH.MONITOR (NC.FetchMonitor (fetch (Card NoteFile) of SourceCard))
			   (NC.IfMultipleCardPartsNotBusy
			     SourceCard
			     (SUBSTANCE TOLINKS FROMLINKS GLOBALTOLINKS)
			     (if (NC.CardP (NC.GetNoteCard SourceCard))
				 then 

          (* * This next set of obtain write locks is only to make sure that we come out even when NC.DeactivateCard releases
	  the write locks. Note that the SourceCard will be deactivated upon exit of the RESETLST, as set up by the 
	  RESETSAVE.)


					(RESETSAVE (for CardPart
							in (QUOTE (SUBSTANCE TOLINKS 
										 GLOBALTOLINKS 
										 PROPLIST))
							do (APPLY* (fetch (Card 
									  ObtainWritePermissionFn)
									  of SourceCard)
								       SourceCard CardPart))
						     (BQUOTE (NC.DeactivateCard , SourceCard)))
					(PROG1 (NC.CachedMakeFilingLink SourceCard 
									    DestinationCard LinkType)
						 (NC.PutMainCardData SourceCard)
						 (NC.PutLinks SourceCard])

(NC.MakeFilingLinks
  [LAMBDA (Card Msg InterestedWindow)                        (* Randy.Gobbel " 2-Apr-87 15:38")
                                                             (* Hooks card specified by Card to all of the current 
							     contents cards by a Contents link)

          (* * rht 8/1/84: Changed the NC.PrintMsg2 call for "No FileBox has been specified." to use NIL as second arg rather
	  than T. This prevents erasure of previous error messages.)



          (* * rht 12/8/84: Massive rewrite. Now calls NC.MakeChildLink. And always orphanizes if no parent specified.
	  This is because it's currently called only by NC.InsureProperFiling.)



          (* * rht 6/25/85: Now returns NewParents and checks if CANCELLED comes back from NC.SelectNoteCards so can abort 
	  the calling operation.)



          (* * Fgh 11/15/85 Adapted from and intended to replace NC.MakeContentsHooks)



          (* * fgh 8/6/86 Updated to use NC.AttachPromptWindow and added Msg arg)



          (* * 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 2/19/87 Make sure NewParents always has the real value)



          (* * rg 3/18/87 added NC.CardSelectionOperation wrapper)



          (* * rht 3/23/87: Changed so that message will fit on one less line.)



          (* * rht 3/23/87: Now takes InterestedWindow arg.)



          (* * Rht 3/24/87: Now calls NC.CoerceToInterestedWindow)



          (* * rht 3/26/87: No longer allows selection of boxes in foreign notefiles.)



          (* * rg 4/1/87 changed CANCELLED to DON'T)


    (DECLARE (GLOBALVARS NC.SelectingContentsMenu))
    (NCP.WithLockedCards (PROG ((NoteFile (fetch (Card NoteFile) of Card))
				  OneHook NewParents)
			         (OR InterestedWindow (SETQ InterestedWindow (
					   NC.CoerceToInterestedWindow Card)))
			         (SETQ NewParents (NC.SelectNoteCards
				     NIL
				     [FUNCTION (LAMBDA (SelectedCard)
					 (AND (NC.FileBoxP SelectedCard)
						(NC.SameNoteFileP NoteFile (fetch (Card NoteFile)
										of SelectedCard]
				     NC.SelectingContentsMenu Card (CONCAT (OR Msg "")
									     
						"('Done' with no selections files in ToBeFiled.)")
				     T))
			         [COND
				   ([NOT (OR (EQ NewParents (QUOTE DON'T))
						 (AND NewParents
							(for ParentCard in NewParents
							   bind OneHook
							   when (NC.MakeChildLink Card ParentCard 
										 InterestedWindow)
							   do (SETQ OneHook T)
							   finally (RETURN OneHook]
				     (NC.PrintMsg InterestedWindow NIL 
						    "No FileBox has been specified."
						    (CHARACTER 13)
						    "This card will be filed in the ToBeFiled Box."
						    (CHARACTER 13))
				     (SETQ NewParents (NC.HookToOrphanCard Card
									       (fetch (NoteFile
											  
										    ToBeFiledCard)
										  of NoteFile)))
				     (COND
				       ((NC.AttachPromptWindow InterestedWindow NIL NIL NIL T)
					 (SPAWN.MOUSE)
					 (DISMISS 2000)
					 (NC.ClearMsg InterestedWindow T]
			         (RETURN NewParents])

(NC.RelabelLink
  [LAMBDA (LinkOrLinkIcon Window NewLinkLabel ForceRedisplayFlg)
                                                             (* Randy.Gobbel "15-May-87 17:03" pp pp)
                                                             (* Relabel a NoteCard link.
							     Ask user for new label. Update all the proper 
							     references to this link.)

          (* * rht 11/19/84: Fixed so that Card is defined before first use.)



          (* * kirk 14Nov85: deleted use of and LinkID)



          (* * kef 8/8/86: Added obtaining write lock and NC.PutFromLinks.)



          (* * fgh 8/30/86 Adapted to NC.IfCardPartNotBusy)



          (* * rht 9/29/86: Changed Ken's call to NC.CardBeingEditedP to NC.ActiveCardP and other minor mod's.)



          (* * rg 5/15/87 now calls NC.LinksCachedP instead of NC.ActiveCardP)


    (LET (Link LinkIcon Card DestinationCard OldLabel NoteCardType GlobalLinkFlg)
         [if (NC.LinkIconImageObjP LinkOrLinkIcon)
	     then (SETQ Link (NC.FetchLinkFromLinkIcon LinkOrLinkIcon))
		    (SETQ LinkIcon LinkOrLinkIcon)
	   else (SETQ Link LinkOrLinkIcon)
		  (OR (SETQ GlobalLinkFlg (NC.GlobalLinkP Link))
			(SETQ LinkIcon (NC.FetchLinkIconForLink Link]
         (SETQ Card (fetch (Link SourceCard) of Link))
         [COND
	   ((WINDOWP Window))
	   ((NC.ActiveCardP Card)
	     (SETQ Window (NC.FetchWindow Card]
         (SETQ DestinationCard (fetch (Link DestinationCard) of Link))
         (NC.IfCardPartNotBusy DestinationCard (QUOTE FROMLINKS)
			       (COND
				 ((NC.SystemLinkLabelP (fetch (Link Label) of Link))
				   (NC.PrintMsg Window T "This is a system maintained pointer."
						  (CHARACTER 13)
						  "You cannot change its label."
						  (CHARACTER 13))
				   (SPAWN.MOUSE)
				   (DISMISS 1500)
				   (NC.ClearMsg Window T))
				 ((SETQ NewLinkLabel (OR NewLinkLabel
							     (NC.AskLinkLabel Window NIL NIL T T)))
				   (SETQ NoteCardType (NC.RetrieveType Card))

          (* * Put new label in Link Icon or global links list)


				   (if GlobalLinkFlg
				       then (for GlobalLink in (NC.FetchGlobalLinks Card)
						 when (NC.SameLinkP GlobalLink Link)
						 do (replace (Link Label) of GlobalLink
							 with NewLinkLabel)
						      (NC.SetLinksDirtyFlg Card T)
						      (RETURN))
				     else (replace (Link Label) of (NC.FetchLinkFromLinkIcon
									   LinkIcon)
					       with NewLinkLabel)
					    (NC.MarkCardDirty Card))

          (* * Update ToLink list of the SourceCard card)


				   (for ToLink in (NC.FetchToLinks Card)
				      when (NC.SameLinkP ToLink Link)
				      do (replace (Link Label) of ToLink with NewLinkLabel)
					   (NC.SetLinksDirtyFlg Card T)
					   (RETURN))

          (* * Update FromLink list of DestinationCard card)


				   (if (NC.LinksCachedP DestinationCard)
				       then (for FromLink in (NC.FetchFromLinks 
										  DestinationCard)
						 when (NC.SameLinkP FromLink Link)
						 do (replace (Link Label) of FromLink
							 with NewLinkLabel)
						      (NC.SetLinksDirtyFlg DestinationCard T)
						      (RETURN))
				     else (WITH.MONITOR (NC.FetchMonitor (fetch (Card NoteFile)
										of DestinationCard))
							  (NC.GetLinks DestinationCard)
							  (for FromLink in (NC.FetchFromLinks
										 DestinationCard)
							     when (NC.SameLinkP FromLink Link)
							     do (replace (Link Label)
								     of FromLink with 
										     NewLinkLabel)
								  (NC.SetLinksDirtyFlg 
										  DestinationCard T)
								  (RETURN))
							  (NC.PutFromLinks DestinationCard)
							  (NC.UncacheLinks DestinationCard)))

          (* * Update images in SourceCard window)


				   (COND
				     ((AND (NULL ForceRedisplayFlg)
					     (NC.TEditBasedP NoteCardType))
				       (QUOTE CHANGED))
				     ((WINDOWP Window)
				       (NC.UpdateLinkImages Window (fetch (Link DestinationCard)
									of Link))
				       NIL)
				     (T NIL])

(NC.SystemLinkLabelP
  (LAMBDA (LinkLabel)                                        (* fgh: "23-Apr-84 23:07")
                                                             (* Return T in LinkLabel is a label for a system 
							     maintained link. NIL otherwise.
							     System maintained links determined by hardwire list 
							     for now.)
    (FMEMB LinkLabel NC.SystemLinkLabels)))

(NC.ValidLinkP
  (LAMBDA (Link)                                             (* rht: "18-Jul-86 16:49")
                                                             (* Check Link to see if it is a valid NOTECARDLINK, 
							     incvluding check that the NoteCard specified by 
							     DESTINATIONID is an active card.)

          (* * kirk 15Nov85: deleted use of DatabaseStream)



          (* * rht 7/18/86: Was actually reading in the destination card from notefile! No more.)


    (LET (DestinationCard)
         (AND (type? Link Link)
		(NEQ (fetch (Link UID) of Link)
		       -1)
		(NC.ValidCardP (fetch (Link DestinationCard) of Link))
		(NC.ValidCardP (fetch (Link SourceCard) of Link))))))

(NC.MakeChildLink
  [LAMBDA (Child Parent Window)                              (* Randy.Gobbel "18-Dec-86 18:26")

          (* * Tries to add Child as a child of Parent using either FiledCard or SubBox link as appropriate.
	  Won't allow if would cause a cycle. Returns new link if successful, else NIL.)



          (* * kirk 14Nov85: deleted use of PSA.Database)



          (* * rg 12/18/86: Cycle-checking code moved to NC.EnsureNoCycles)


    (DECLARE (GLOBALVARS NC.SubBoxLinkLabel NC.FiledCardLinkLabel))
    (AND (NC.EnsureNoCycles Parent Child (FUNCTION NC.ChildLinkP)
				Window)
	   (NC.MakeFilingLink Parent Child (if (NC.FileBoxP Child T)
						 then NC.SubBoxLinkLabel
					       else NC.FiledCardLinkLabel])

(NC.EnsureNoCycles
  [LAMBDA (Parent Child LinkPredicate InterestedWindow)      (* Randy.Gobbel "18-Dec-86 18:14")

          (* * Return T if no cycles via specified link predicate, else NIL.)


    (COND
      ([for Link in (NC.FetchToLinks Parent) thereis (AND (APPLY* LinkPredicate Link)
								    (NC.SameCardP
								      Child
								      (fetch (Link DestinationCard)
									 of Link]
	(NC.PrintMsg InterestedWindow NIL (NC.RetrieveTitle Child)
		       " is already a child of "
		       (NC.RetrieveTitle Parent)
		       (CHARACTER 13))
	NIL)
      ((AND (NEQ Parent Child)
	      (NC.NotDaughterP Child Parent LinkPredicate))
	T)
      (T (NC.PrintMsg InterestedWindow T (NC.RetrieveTitle Child)
			" is an ancestor of "
			(NC.RetrieveTitle Parent)
			(CHARACTER 13))
	 NIL])

(NC.SameLinkP
  (LAMBDA (Link1 Link2)                                      (* rht: "15-Nov-85 15:45")
                                                             (* Compare two link UIDs)
    (NC.SameUIDP (fetch (Link UID) of Link1)
		   (fetch (Link UID) of Link2))))

(NC.SameLinksP
  (LAMBDA (LinkListA LinkListB)                              (* rht: "15-Nov-85 15:39")

          (* * ARe these two lists of links the same?)


    (for LinkA in LinkListA as LinkB in LinkListB always (NC.SameLinkP LinkA LinkB))))

(NC.AddGlobalLinkToCard
  [LAMBDA (WindowOrTextStream)                               (* rht: "15-Feb-85 18:54")

          (* * Called from card's menu to add one global link.)


    (NC.AddGlobalLinksToCard WindowOrTextStream T])

(NC.AddGlobalLinksToCard
  [LAMBDA (CardIdentifier SingleLinkFlg)                     (* pmi: "21-May-87 15:42")

          (* * Called from card's menu to add some global links.)



          (* * rht 11/24/85: Fixed bugs. It was calling NC.CoerceToID.)



          (* * fgh 6/9/86 Added chack for other interaction operations already in progress.)



          (* * 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/4/87 rewritten for new version of NC.ProtectedCardOperation)



          (* * rg 3/18/87 added NC.CardSelectionOperation wrapper)



          (* * rg 3/31/87 fixed bug in NC.ProtectedCardOperation wrapper)



          (* * pmi 5/21/87: Changed NC.CardSelectionOperation to NCP.WithLockedCards)


    (NC.ProtectedCardOperation (NC.CoerceToCard CardIdentifier)
			       "Add Global Link(s)" NIL
			       (NCP.WithLockedCards
				 (LET (Window SourceCard DestinationCards Label)
				      (SETQ SourceCard (NC.CoerceToCard CardIdentifier))
				      (SETQ Window (NC.FetchWindow SourceCard))
				      (SETQ Label (NC.AskLinkLabel Window NIL NIL T T))
				      (AND Label
					     (if SingleLinkFlg
						 then (NC.MakeGlobalLink Window Label NIL 
									     SourceCard)
							(NC.ClearMsg Window T)
					       else (SETQ DestinationCards
							(NC.SelectNoteCards
							  NIL
							  [FUNCTION (LAMBDA (Card)
							      (COND
								((NEQ Card SourceCard)
								  T)
								(T (NC.PrintMsg Window T 
							    "A Card/Box cannot point to itself. "
										  (CHARACTER 13)
										  
									     "Selection ignored."
										  (CHARACTER 13))
								   NIL]
							  NC.SelectingMultipleCardsMenu SourceCard 
				       "Please shift-select the Cards or Boxes to be pointed to."))
						      (NC.ClearMsg Window T)
						      (for DestinationCard in DestinationCards
							 do (NC.MakeGlobalLink Window Label 
										  DestinationCard 
										   SourceCard])

(NC.ChangeCardTitleFromLinkIcon
  [LAMBDA (LinkIcon Window)                                  (* rht: "18-Feb-87 11:34")

          (* * From a link icon sitting in Window, change the title of the link's destination card.)



          (* * kirk 14Nov85: deleted use of PSA.Database)



          (* * fgh 6/4/86 Fixed so that moves card back to original location if moved to be on screen.
	  This fixes a bug in TEdit that'll mess up the screen if a window is mived under a ImageObj ButtonEventInFn.)



          (* * kef 7/16/86: Added mechanism for grabbing the write permission on the TITLE of the destination card.)



          (* * kef 7/30/86: Modified to check for Client's concept of whether he owns the write lock or not, thus deciding 
	  whether or not to setup the release of the write lock afterwards.)



          (* * fgh 8/30/86 Adpated to use NC.IfCardPartNotBusy.)



          (* * rht 11/13/86: Now checks for crossfile links and bails out.)



          (* * rht 2/18/87: Added check for readonly notefile to fix bug %#376.0)


    (LET (Link Card DestinationCard OldTitle NewTitle)
         (RESETLST [RESETSAVE NIL (BQUOTE (MOVEW , Window , (LOWERLEFT (WINDOWPROP
										   Window
										   (QUOTE REGION]
		     (if (NC.LinkIconImageObjP LinkIcon)
			 then
			  (SETQ Link (NC.FetchLinkFromLinkIcon LinkIcon))
			  (SETQ Card (fetch (Link SourceCard) of Link))
			  (SETQ DestinationCard (fetch (Link DestinationCard) of Link))
			  (if (NC.CheckForNotReadOnly Card Window 
							  "Can't change titles for cards in ")
			      then
			       (if (NC.CrossFileLinkCardP DestinationCard)
				   then (NC.PrintMsg Window T 
					    "Can't change card title from a cross notefile link.")
					  (DISMISS 1500)
					  (NC.ClearMsg Window T)
					  NIL
				 else (NC.IfCardPartNotBusy
					  DestinationCard
					  (QUOTE TITLE)
					  (if (SETQ NewTitle
						  (NC.AskUser (CONCAT 
							  "Enter new title for card with title '"
									  (NC.RetrieveTitle 
										  DestinationCard)
									  "'"
									  (CHARACTER 13))
								"-->  "
								(if (AND (STREQUAL
									       (SETQ OldTitle
										 (NC.RetrieveTitle
										   DestinationCard))
									       "Untitled")
									     (NC.FetchNewCardFlg
									       DestinationCard))
								    then NIL
								  else OldTitle)
								T Window))
					      then (NC.AssignTitle DestinationCard NIL NewTitle)
						     (QUOTE CHANGED])

(NC.UncacheLinks
  (LAMBDA (Card)                                             (* rht: "14-Feb-86 15:06")

          (* * Throw away the links cache for this card.)


    (replace (Card Links) of Card with NIL)))

(NC.TraverseLink
  [LAMBDA (LinkIcon Window)                                  (* rht: "18-Nov-86 18:47")

          (* * Follow the link to the card at the other end. For now, amounts to editing the destination card.)


    (LET ((Card (fetch (Link DestinationCard) of (NC.FetchLinkFromLinkIcon LinkIcon)))
	  OldTitle)
         (SETQ OldTitle (NC.RetrieveTitle Card))
         (NC.EditNoteCard Card)

          (* * We're under TEdit, so if title was changed by EditFn, then be sure link icon gets redisplayed.)


         (if (STREQUAL OldTitle (NC.RetrieveTitle Card))
	     then (QUOTE DON'T)
	   else (QUOTE CHANGED])

(NC.CreateLinkIconStrings
  [LAMBDA (Link LinkIconString Icon ImageStream)             (* pmi: "13-Feb-87 12:18")

          (* * pmi 2/9/87: First written to parse the text for a link icon into multiple lines)


    (DECLARE (GLOBALVARS NC.LinkIconMaxWidth NC.LinkIconMultiLineMode NC.LinkIconBorderWidth 
			     NC.LinkIconSpaceInnerX))
    (PROG ((Scale (DSPSCALE NIL ImageStream))
	     (LinkIconLines NIL)
	     (ThisString "")
	     RemainingLineWidth Font ScaledBorderWidth ScaledSpaceInnerX TotalEdgeSpaceX LineWidth 
	     StringLength CharsPtr NumberOfLines StringList String StringWidth Char CharWidth 
	     CurrentWidth EndPtr)
	    (SETQ Font (DSPFONT NIL ImageStream))

          (* * Make temporaries of scaled vars.)


	    (SETQ ScaledBorderWidth (TIMES Scale NC.LinkIconBorderWidth))
	    (SETQ ScaledSpaceInnerX (TIMES Scale NC.LinkIconSpaceInnerX))
	    (SETQ TotalEdgeSpaceX (PLUS ScaledBorderWidth ScaledBorderWidth ScaledSpaceInnerX 
					    ScaledSpaceInnerX))

          (* * Width of text in link icon)


	    (SETQ LineWidth (DIFFERENCE (TIMES Scale NC.LinkIconMaxWidth)
					    TotalEdgeSpaceX))

          (* * If there isn't even enough room for the bitmap, give up.)


	    (if Icon
		then (SETQ RemainingLineWidth (PLUS (DIFFERENCE LineWidth
									(TIMES Scale (BITMAPWIDTH
										   Icon)))
							  ScaledBorderWidth))
	      else (SETQ RemainingLineWidth LineWidth))

          (* * Turn the text into a string list of words and spaces)


	    [if NC.LinkIconMultiLineMode
		then 

          (* * Calculate multiple lines of text)


		       (SETQ StringList (NC.ParseString LinkIconString))
		       (SETQ String (CAR StringList))
		       (SETQ NumberOfLines 0) 

          (* * Determine the line breaks)


		       [while StringList
			  do (if (AND (STREQUAL (SUBSTRING String 1 1)
							(QUOTE " "))
					    (STREQUAL ThisString ""))
				   then 

          (* * The "word" is a space or spaces and we are at the beginning of the line. In this case we want to throw away 
	  the spaces.)


					  (SETQ StringList (CDR StringList))
					  (if StringList
					      then (SETQ String (CAR StringList)))
				 else (SETQ StringWidth (STRINGWIDTH String Font))
					(if (LESSP StringWidth RemainingLineWidth)
					    then 

          (* * The word or spaces will fit on the remainder of the line)


						   (SETQ ThisString (CONCAT ThisString String))
						   (SETQ RemainingLineWidth (DIFFERENCE 
									       RemainingLineWidth 
										      StringWidth))
						   (SETQ StringList (CDR StringList))
						   (if StringList
						       then (SETQ String (CAR StringList))
						     else (SETQ LinkIconLines (NCONC1 
										    LinkIconLines 
										       ThisString)))
					  elseif (EQ StringWidth RemainingLineWidth)
					    then 

          (* * The word or spaces will exactly fit on the remainder of the line)


						   (SETQ ThisString (CONCAT ThisString String))
						   (SETQ LinkIconLines (NCONC1 LinkIconLines 
										   ThisString))
						   (SETQ ThisString "")
						   (SETQ RemainingLineWidth LineWidth)
						   (SETQ NumberOfLines (ADD1 NumberOfLines))
						   (SETQ StringList (CDR StringList))
						   (if StringList
						       then (SETQ String (CAR StringList)))
					  elseif (STREQUAL ThisString "")
					    then 

          (* * We have a word that is too long for the line, and we are at the beginning of the line)


						   (SETQ CurrentWidth 0)
						   [SETQ EndPtr (for I from 1
								     to (NCHARS String)
								     do (SETQ Char
									    (SUBSTRING String I I))
									  (SETQ CurrentWidth
									    (PLUS CurrentWidth
										    (STRINGWIDTH
										      Char Font)))
									  (if (GREATERP 
										     CurrentWidth 
									       RemainingLineWidth)
									      then
									       (RETURN
										 (SUB1 I]
						   [if (EQ EndPtr 0)
						       then 

          (* * This character won't fit on this line, so give up)


							      (OR LinkIconLines (SETQ 
								      LinkIconLines (LIST "")))
							      (RETURN)
						     else (SETQ ThisString
							      (CONCAT ThisString
									(SUBSTRING String 1 EndPtr]
						   (SETQ LinkIconLines (NCONC1 LinkIconLines 
										   ThisString))
						   (SETQ String (SUBSTRING String (ADD1 EndPtr))
						     )
						   (SETQ ThisString "")
						   (SETQ RemainingLineWidth LineWidth)
						   (SETQ NumberOfLines (ADD1 NumberOfLines))
					  elseif (STREQUAL (SUBSTRING String 1 1)
							       (QUOTE " "))
					    then 

          (* * We have spaces at the end of the line which can be thrown away)


						   (SETQ LinkIconLines (NCONC1 LinkIconLines 
										   ThisString))
						   (SETQ ThisString "")
						   (SETQ RemainingLineWidth LineWidth)
						   (SETQ NumberOfLines (ADD1 NumberOfLines))
						   (SETQ StringList (CDR StringList))
						   (if StringList
						       then (SETQ String (CAR StringList)))
					  else 

          (* * We have a word that won't fit on the remainder of the line, so we must start it on the next line)


						 (SETQ LinkIconLines (NCONC1 LinkIconLines 
										 ThisString))
						 (SETQ ThisString "")
						 (SETQ RemainingLineWidth LineWidth)
						 (SETQ NumberOfLines (ADD1 NumberOfLines]
	      elseif (LESSP (STRINGWIDTH LinkIconString Font)
				RemainingLineWidth)
		then 

          (* * Calculate single line of text. The string will fit as is)


		       (SETQ LinkIconLines (LIST LinkIconString))
	      else 

          (* * Calculate single line of text. We have to chop the string down to fit in one line)


		     (SETQ StringLength (NCHARS LinkIconString))
		     (SETQ CharsPtr 1)
		     (SETQ LinkIconLines (LIST (while (ILEQ CharsPtr StringLength)
						      do [if (LESSP (SETQ CharWidth
									    (STRINGWIDTH
									      (NTHCHAR 
										   LinkIconString 
											 CharsPtr)
									      Font))
									  RemainingLineWidth)
							       then (SETQ RemainingLineWidth
									(DIFFERENCE 
									       RemainingLineWidth 
										      CharWidth))
								      (SETQ CharsPtr (ADD1 
											 CharsPtr))
							     elseif (EQ CharsPtr 1)
							       then (RETURN "")
							     else (RETURN (SUBSTRING
										LinkIconString 1
										(SUB1 CharsPtr]
						      finally (RETURN (SUBSTRING LinkIconString 
										       1
										       (SUB1 
											 CharsPtr]
	    (RETURN LinkIconLines])
)
(DEFINEQ

(NC.AddLinkToCard
  [LAMBDA (CardIdentifier LinkOrLinkLabel DestinationCard)   (* Randy.Gobbel " 4-Mar-87 14:15")

          (* * Add a link to card designated by CardIdentifier. Rest of args can be NIL. Currently, this dispatches on card 
	  type. Should eventually call something like an InsertLinkFn.)



          (* * fgh 6/9/86 Added code to check to make sure that another operation is not in progress on this card when this 
	  fn is called.)



          (* * rg 3/4/87 rewritten for new version of NC.ProtectedCardOperation)


    (LET ((Card (NC.CoerceToCard CardIdentifier))
	  CardType Window)
         (NC.ProtectedCardOperation Card "Insert Link" NIL (SETQ Window (NC.FetchWindow Card))
				    (SETQ CardType (NC.FetchType Card))
				    (OR LinkOrLinkLabel (SETQ LinkOrLinkLabel
					    (NC.AskLinkLabel Window NIL NIL T T)))
				    (AND LinkOrLinkLabel (COND
					     ((NC.IsSubTypeOfP CardType (QUOTE Text))
					       (NC.InsertLinkInText (NC.FetchSubstance Card)
								      LinkOrLinkLabel DestinationCard 
								      Card))
					     ((NC.IsSubTypeOfP CardType (QUOTE Graph))
					       (NC.AddLinkToGraphCard Window LinkOrLinkLabel 
									DestinationCard))
					     ((NC.IsSubTypeOfP CardType (QUOTE Sketch))
					       (NC.AddLinkToSketchCard Window LinkOrLinkLabel 
									 DestinationCard])

(NC.AddLinksToCard
  [LAMBDA (CardIdentifier LinkLabel DestinationCards)        (* Randy.Gobbel " 2-Apr-87 15:38")

          (* * Adds multiple links to card designated by CardIdentifier. Rest of args can be NIL.)



          (* * fgh 6/9/86 Added Checks to make sure other interaction operation not in porgress.)



          (* * rht 9/23/86: Fixed bug in function passed to NC.SelectNoteCards.)



          (* * 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/4/87 rewritten for new version of NC.ProtectedCardOperation)



          (* * rg 3/18/87 added NC.CardSelectionOperation wrapper)



          (* * RG 4/2/87 changed NC.CardSelectionOperation to NCP.WithLockedCards)


    (LET ((SourceCard (NC.CoerceToCard CardIdentifier))
	  CardType Window OperationInProgress)
         (NC.ProtectedCardOperation SourceCard "Insert Links" NIL
				    (NCP.WithLockedCards
				      (SETQ Window (NC.FetchWindow SourceCard))
				      (SETQ CardType (NC.FetchType SourceCard))
				      (OR LinkLabel (SETQ LinkLabel
					      (NC.AskLinkLabel Window NIL NIL T T)))
				      (if LinkLabel
					  then (OR DestinationCards
						       (SETQ DestinationCards
							 (NC.SelectNoteCards
							   NIL
							   [FUNCTION (LAMBDA (SelectedCard)
							       (COND
								 ((NOT (NC.SameCardP SelectedCard 
										       SourceCard))
								   T)
								 (T (NC.PrintMsg (NC.FetchWindow
										     SourceCard)
										   T 
							    "A Card/Box cannot point to itself. "
										   (CHARACTER
										     13)
										   
									     "Selection ignored."
										   (CHARACTER
										     13))
								    NIL]
							   NC.SelectingMultipleCardsMenu SourceCard 
				       "Please shift-select the Cards or Boxes to be pointed to.")))
						 (NC.ClearMsg Window T)
						 (for DestinationCard in DestinationCards
						    do (NC.AddLinkToCard CardIdentifier LinkLabel 
									     DestinationCard])
)
(DEFINEQ

(NC.DelReferencesToCardFromShowLinks
  (LAMBDA (Card Link)                                        (* fgh: " 2-May-86 16:07")

          (* * Delete any references to link from the show links editor above Card, if any)



          (* * fgh 5/2/86 First created.)


    (LET (Window Substance OldSel)
         (if (SETQ Window (NC.FetchWindow Card))
	     then (for AttachedWindow in (ALLATTACHEDWINDOWS Window)
		       when (WINDOWPROP AttachedWindow (QUOTE ShowLinks))
		       do (for LinkIconPair in (TEDIT.LIST.OF.OBJECTS (TEXTOBJ
										(SETQ Substance
										  (TEXTSTREAM
										    AttachedWindow)))
									      (FUNCTION 
									     NC.LinkIconImageObjP))
			       when (NC.SameLinkP Link (NC.FetchLinkFromLinkIcon (CAR 
										     LinkIconPair)))
			       do (replace (IMAGEOBJ IMAGEOBJFNS) of (CAR LinkIconPair)
				       with NC.NoDeleteImageFns)
                                                             (* WHENDELETEDFN taken care of earlier in delete 
							     cycle)
				    (SETQ OldSel (TEDIT.GETSEL Substance))
				    (TEDIT.DELETE Substance (CADR LinkIconPair)
						    1)
				    (TEDIT.INSERT.OBJECT NC.DeletedLinkImageObject Substance
							   (CADR LinkIconPair))
				    (TEDIT.SETSEL Substance OldSel)))))))
)
(* * New Display Mode format)

(DEFINEQ

(NC.CheckDisplayModeFormat
  [LAMBDA (Link)                                             (* fgh: "16-Nov-85 17:56")

          (* * If Link's display mode is an atom, then change to the new record format.)



          (* * rht 3/28/85: Now does nothing if link is not in current format. In that case, we should be inside of a 
	  ConvertVersion0ToVersion1 so that a later repair should fix the links.)



          (* * rht 7/17/85: Now turns into a typed record if wasn't already.)



          (* * fgh 11/16/85 Removed conversion to typed record, since links are now datatypes.)


    (LET (DisplayMode)
         [COND
	   ((type? Link Link)
	     (COND
	       ((ATOM (SETQ DisplayMode (fetch (Link DisplayMode) of Link)))
		 (replace (Link DisplayMode) of Link with (NC.MakeNewDisplayMode DisplayMode))
		 )
	       ((NEQ (CAR DisplayMode)
		       (QUOTE LINKDISPLAYMODE))
		 (replace (Link DisplayMode) of Link with (CONS (QUOTE LINKDISPLAYMODE)
									DisplayMode]
     Link])

(NC.InsureLinkDisplayMode
  [LAMBDA (LinkDisplayMode)                                  (* rht: " 6-Oct-86 23:40")

          (* * Return the recordized form of the LinkDisplayMode argument.)



          (* * fgh 5/2/86 First created)



          (* * rht 10/6/86: Now uses FLOAT for ATTACHBITMAPFLG in most cases where LinkDisplayMode is atomic.)


    (COND
      ((type? LINKDISPLAYMODE LinkDisplayMode)
	LinkDisplayMode)
      (T (SELECTQ LinkDisplayMode
		    (Title (create LINKDISPLAYMODE
				     SHOWTITLEFLG ← T
				     ATTACHBITMAPFLG ← (QUOTE FLOAT)))
		    (Icon (create LINKDISPLAYMODE
				    ATTACHBITMAPFLG ← T))
		    (Label (create LINKDISPLAYMODE
				     SHOWLINKTYPEFLG ← T
				     ATTACHBITMAPFLG ← (QUOTE FLOAT)))
		    (Both (create LINKDISPLAYMODE
				    SHOWTITLEFLG ← T
				    SHOWLINKTYPEFLG ← T
				    ATTACHBITMAPFLG ← (QUOTE FLOAT)))
		    (create LINKDISPLAYMODE
			      ATTACHBITMAPFLG ← T])

(NC.MakeNewDisplayMode
  [LAMBDA (ModeAtom)                                         (* rht: " 9-Sep-85 22:12")

          (* * Makes a new display mode record structure using info in ModeAtom.)


    (create LINKDISPLAYMODE
	      SHOWTITLEFLG ← (AND (FMEMB ModeAtom (QUOTE (Title Both)))
				    T)
	      SHOWLINKTYPEFLG ← (AND (FMEMB ModeAtom (QUOTE (Label Both)))
				       T)
	      ATTACHBITMAPFLG ← (if (OR (NULL ModeAtom)
					    (EQ ModeAtom (QUOTE Icon)))
				    then T
				  else (QUOTE FLOAT])

(NC.DisplayModeFromStylesheetSelections
  (LAMBDA (Selections)                                       (* rht: "18-Jul-85 14:35")

          (* * Return the display mode corresponding to the user's selection from the changeDisplayMode stylesheet.)


    (AND Selections (create LINKDISPLAYMODE
				SHOWTITLEFLG ← (LISTGET NC.MenuItems/DisplayModeValues
							    (CAR Selections))
				SHOWLINKTYPEFLG ← (LISTGET NC.MenuItems/DisplayModeValues
							       (CADR Selections))
				ATTACHBITMAPFLG ← (LISTGET NC.MenuItems/DisplayModeValues
							       (CADDR Selections))))))

(NC.StylesheetSelectionsFromDisplayMode
  (LAMBDA (DisplayMode)                                      (* rht: "16-Mar-85 16:28")

          (* * Return the list of ChangeDisplayMode stylesheet selections corresponding to the given DisplayMode.)


    (LIST (SELECTQ (fetch (LINKDISPLAYMODE SHOWTITLEFLG) of DisplayMode)
		       (NIL (QUOTE No))
		       (FLOAT (QUOTE Float))
		       (QUOTE Yes))
	    (SELECTQ (fetch (LINKDISPLAYMODE SHOWLINKTYPEFLG) of DisplayMode)
		       (NIL (QUOTE No))
		       (FLOAT (QUOTE Float))
		       (QUOTE Yes))
	    (SELECTQ (fetch (LINKDISPLAYMODE ATTACHBITMAPFLG) of DisplayMode)
		       (NIL (QUOTE No))
		       (FLOAT (QUOTE Float))
		       (QUOTE Yes)))))
)
(DECLARE: DOEVAL@COMPILE DONTCOPY

(GLOBALVARS NC.MenuItems/DisplayModeValues)
)

(RPAQQ NC.MenuItems/DisplayModeValues (Yes T No NIL Float FLOAT Yes T No NIL Float FLOAT Yes T No 
					     NIL Float FLOAT))
(* * New fns to support No-Link cards/substances)

(DEFINEQ

(NC.LinksSupportedP
  (LAMBDA (Card AnchorModesList)                             (* fgh: " 5-Feb-86 14:08")

          (* * Are all of the anchor modes supporterd by the card Card)



          (* * fgh 2/5/86 Changed call to LinkAnchorModesFromType to NC.FetchLinkAnchorModesSupported)


    (LET ((SupportedAnchorModes (NC.FetchLinkAnchorModesSupported Card)))
         (COND
	   ((EQ SupportedAnchorModes T))
	   ((NULL SupportedAnchorModes)
	     NIL)
	   (T (SETQ SupportedAnchorModes (MKLIST SupportedAnchorModes))
	      (for AnchorMode in AnchorModesList always (FMEMB AnchorMode 
								       SupportedAnchorModes)))))))
)
(* * This stuff supports the "push-copy" method of copying links in NC)

(* * Link display icons)

(DEFINEQ

(NC.FetchLinkIconForLink
  (LAMBDA (Link)                                             (* fgh: " 5-Feb-86 19:52")

          (* * Find the Link Icon corresponding to Link)



          (* * kirk 14Nov85: deleted use of LinkID and PSA.Database)



          (* * fgh 2/5/86 Added call to NC.ApplyFn)


    (LET ((SourceCard (fetch (Link SourceCard) of Link)))
         (for LinkIcon in (CAR (NC.ApplyFn CollectLinksFn SourceCard NIL T))
	    thereis (if (NC.SameLinkP Link (NC.FetchLinkFromLinkIcon LinkIcon))
			  then LinkIcon)))))

(NC.MakeLinkIcon
  (LAMBDA (Link)                                             (* fgh: " 1-Oct-84 20:48")

          (* * Create a Link Icon image object from the lionk info in Link)


    (IMAGEOBJCREATE Link NC.LinkIconImageFns)))

(NC.LinkIconButtonEventInFn
  [LAMBDA (ImageObject Window)                               (* rht: "18-Nov-86 18:48")
                                                             (* If the guy buttons inside a link image object)

          (* * fgh 6/3/86 Removed hot region, removed push copy link icon.)



          (* * fgh 6/29/86 Put back in a hot region.)



          (* * rht 11/18/86: Now calls NC.LinkIconLeftButtonFn rather than hardwiring in the call to NC.EditNoteCard.)


    (LET ((Window (COND
		    ((WINDOWP Window)
		      Window)
		    ((DISPLAYSTREAMP Window)
		      (WFROMDS Window))
		    ((TEXTSTREAMP Window)
		      (WINDOW.FROM.TEDIT.THING Window))
		    (T NIL)))
	  (ClippingRegion (DSPCLIPPINGREGION NIL Window))
	  (Link (NC.FetchLinkFromLinkIcon ImageObject))
	  HotRegion MouseButtonState TTYWindow ClippingRegionWidth)

          (* * Figure out the hot region -- basically the clipping region minus a few pts at right and left to allow for 
	  TEdit selection of icon.)


         [SETQ HotRegion (if (GREATERP (SETQ ClippingRegionWidth (fetch (REGION WIDTH)
									    of ClippingRegion))
					     24)
			       then                        (* There's at least 10 pts between the two 7pt cold 
							     regions at left and right.)
				      (CREATEREGION (PLUS (fetch (REGION LEFT) of 
										   ClippingRegion)
							      7)
						      (fetch (REGION BOTTOM) of ClippingRegion)
						      (DIFFERENCE (fetch (REGION WIDTH)
								       of ClippingRegion)
								    14)
						      (fetch (REGION HEIGHT) of ClippingRegion))
			     elseif (AND (LESSP ClippingRegionWidth 9))
			       then                        (* if link icon width less than 9 then no cold 
							     regions)
				      ClippingRegion
			     else                          (* Divide into 4ths and give 1/4 at each edge as cold 
							     regions.)
				    (CREATEREGION (PLUS (fetch (REGION LEFT) of 
										   ClippingRegion)
							    (TIMES .25 ClippingRegionWidth))
						    (fetch (REGION BOTTOM) of ClippingRegion)
						    (QUOTIENT ClippingRegionWidth 2)
						    (fetch (REGION HEIGHT) of ClippingRegion]
         (COND
	   ((NOT (INSIDEP HotRegion (LASTMOUSEX Window)
			      (LASTMOUSEY Window)))

          (* * If in the link icon, but not in the hot region -- want to select the link icon.)


	     NIL)
	   (T 

          (* * Otherwise, invert the window and wait until the mouse is up or the cursor moves out of the link icon.)


	      (RESETLST (RESETSAVE (INVERTW Window))
			  (until [OR (MOUSESTATE UP)
					 (NOT (INSIDEP HotRegion (LASTMOUSEX Window)
							   (LASTMOUSEY Window]
			     do (SETQ MouseButtonState LASTMOUSEBUTTONS)))

          (* * If user let up inside the link icon, then call the appropriate function.)


	      (COND
		((AND (INSIDEP HotRegion (LASTMOUSEX Window)
				   (LASTMOUSEY Window))
			MouseButtonState)

          (* * User let up inside the link icon.)


		  (COND
		    ((OR (KEYDOWNP (QUOTE LSHIFT))
			   (KEYDOWNP (QUOTE RSHIFT))
			   (KEYDOWNP (QUOTE CTRL))
			   (KEYDOWNP (QUOTE COPY))
			   (KEYDOWNP (QUOTE DELETE))
			   (KEYDOWNP (QUOTE MOVE)))      (* Copy or move or delete key is down, just do 
							     selection)
		      NIL)
		    ((ZEROP (LOGXOR MouseButtonState 4))
                                                             (* The left button was down, call the left button fn.)
		      (NC.LinkIconLeftButtonFn ImageObject Window))
		    ((OR (ZEROP (LOGXOR MouseButtonState 1))
			   (ZEROP (LOGXOR MouseButtonState 6)))
                                                             (* The middle button was down, call the middle button 
							     fn.)
		      (NC.LinkIconMiddleButtonFn ImageObject Window))
		    ((ZEROP (LOGXOR MouseButtonState 2))
                                                             (* The right button was down, allow selection)
		      NIL)
		    (T                                       (* Hmmm, don't know know, allow selectin)
		       NIL)))
		([AND (INSIDEP ClippingRegion (LASTMOUSEX Window)
				   (LASTMOUSEY Window))
			(NOT (INSIDEP HotRegion (LASTMOUSEX Window)
					  (LASTMOUSEY Window]

          (* * User let up outside the hot region but inside the link icon -- select the link icon)


		  NIL)
		(T 

          (* * User let up outside the link icon -- don't select the link icon)


		   (QUOTE DON'T])

(NC.LinkIconDisplayFn
  [LAMBDA (ImageObj ImageStream StreamType TextStream Scale)
                                                             (* pmi: " 1-May-87 18:02")

          (* * Display a link icon)



          (* * rht 11/13/84: Made width of box lines also scale dependent.)



          (* * rht 12/4/84: Hacked so type-dependent icons come out optionally to left of text.)



          (* * rht 2/9/85: Changed to use new display mode format.)



          (* * fgh 2/5/86 Added call to NC.FetchLinkIconAttachedBitMap)



          (* * rht & fgh 5/9/86: Massive hacking to display coords, etc.)



          (* * fgh 5/9/86 Added DSPFONT kludge after TypeIcon BITBLT to get around bug in PRESS BITBLT.
	  Bug reported as AR %#5630.0)



          (* * rht 5/10/86: Rearranged order of expressions a bit and added arg to call to NC.FetchLinkIconAttachedBitMap in 
	  order to get a bitmap with correct height for the box we're drawing.)



          (* * rht 8/7/86: Now passes Scale argument to NC.FetchLinkIconAttachedBitMap. Also uses the Title and Label args if
	  non-nil. If not, then recomputes them more sensibly.)



          (* * rht 8/25/86: Fixed improperly placed comment.)



          (* * pmi 2/11/87: Overhauled to accommodate multi-line link icons)



          (* * pmi 2/27/87: Fixed problems with only displaying the attached bitmap and with displaying null titles)



          (* * pmi 5/1/87: added missing local vars declarations.)


    (DECLARE (GLOBALVARS NC.LinkIconMultiLineMode NC.LinkIconMaxWidth NC.LinkIconSpaceInnerX 
			     NC.LinkIconSpaceOuterX NC.LinkIconAttachBitmapFlg NC.LinkIconSpaceInnerY 
			     NC.LinkIconSpaceOuterY NC.LinkIconSpaceInterLine NC.LinkIconBorderWidth 
			     NC.LinkIconShowLinkTypeFlg NC.LinkIconShowTitleFlg))
    (RESETLST (RESETSAVE NIL (BQUOTE (DSPFONT , (DSPFONT NC.LinkIconFont ImageStream)
						      , ImageStream)))
		(PROG ((Scale (DSPSCALE NIL ImageStream))
			 (Link (NC.FetchLinkFromLinkIcon ImageObj))
			 Font FontHeight FontAscent FontDescent Left Bottom Top ShowTitleFlg 
			 LinkDisplayMode AttachBitmapFlg DisplayType Window Card Title Label 
			 LinkIconString LinkIconStrings ApproxBoxWidth ApproxBoxHeight BoxWidth 
			 BoxHeight ImageBox Icon ScaledIconHeight ScaledIconWidth XPosition 
			 ScaledLinkIconMaxWidth ScaledBorderWidth ScaledSpaceInnerX ScaledSpaceOuterX 
			 ScaledSpaceInnerY ScaledSpaceOuterY ScaledSpaceInterLine 
			 HalfScaledSpaceInterLine BottomOfLine)
		        (SETQ Font (DSPFONT NIL ImageStream))
		        (SETQ FontHeight (FONTHEIGHT Font))

          (* * Determine what type of Display to do)


		        (SETQ DisplayType (fetch (Link DisplayMode) of Link))
		        (SETQ ShowTitleFlg (fetch (LINKDISPLAYMODE SHOWTITLEFLG) of DisplayType)
			  )
		        (SETQ Card (if (EQ ShowTitleFlg (QUOTE SOURCE))
					 then (fetch (Link SourceCard) of Link)
				       else (fetch (Link DestinationCard) of Link)))
		        (SETQ LinkDisplayMode (fetch (LINKDISPLAYMODE SHOWLINKTYPEFLG)
						   of DisplayType))
		        (SETQ AttachBitmapFlg (fetch (LINKDISPLAYMODE ATTACHBITMAPFLG)
						   of DisplayType))
		        (SETQ AttachBitmapFlg (if (EQ AttachBitmapFlg (QUOTE FLOAT))
						    then NC.LinkIconAttachBitmapFlg
						  else AttachBitmapFlg))

          (* * Construct the text for the link icon)


		        (SETQ Title (if (AND ShowTitleFlg (OR (NEQ ShowTitleFlg
									     (QUOTE FLOAT))
								      NC.LinkIconShowTitleFlg))
					  then (NC.RetrieveTitle Card)
					else NIL))
		        (SETQ Label (AND (COND
					       ((EQ LinkDisplayMode (QUOTE FLOAT))
						 NC.LinkIconShowLinkTypeFlg)
					       (T LinkDisplayMode))
					     (fetch (Link Label) of Link)))
		        (SETQ LinkIconString (CONCAT (COND
							   (Label (CONCAT "<" Label ">"))
							   (T ""))
							 (COND
							   ((AND Label Title)
							     " ")
							   (T ""))
							 (OR Title "")))

          (* * Make temporaries of scaled variables)


		        (SETQ ScaledLinkIconMaxWidth (TIMES Scale NC.LinkIconMaxWidth))
		        (SETQ ScaledBorderWidth (TIMES Scale NC.LinkIconBorderWidth))
		        (SETQ ScaledSpaceInnerX (TIMES Scale NC.LinkIconSpaceInnerX))
		        (SETQ ScaledSpaceOuterX (TIMES Scale NC.LinkIconSpaceOuterX))
		        (SETQ ScaledSpaceInnerY (TIMES Scale NC.LinkIconSpaceInnerY))
		        (SETQ ScaledSpaceOuterY (TIMES Scale NC.LinkIconSpaceOuterY))
		        (SETQ ScaledSpaceInterLine (TIMES Scale NC.LinkIconSpaceInterLine))
		        (SETQ HalfScaledSpaceInterLine (IQUOTIENT ScaledSpaceInterLine 2))

          (* * Set up the icon, if displayed)


		        (SETQ ImageBox (IMAGEOBJPROP ImageObj (QUOTE BOUNDBOX)))
		        (if AttachBitmapFlg
			    then 

          (* * Attached icon)



          (* * Use an estimate of the width and height to tell if the box contains more than one line of text)


				   [if NC.LinkIconMultiLineMode
				       then 

          (* * Multi-line link icons are enabled)


					      (SETQ ApproxBoxWidth (PLUS ScaledSpaceOuterX 
									     ScaledBorderWidth 
									     ScaledSpaceInnerX
									     (STRINGWIDTH 
										   LinkIconString 
											    Font)
									     ScaledSpaceInnerX 
									     ScaledBorderWidth 
									     ScaledSpaceOuterX))
					      (SETQ ApproxBoxHeight
						(PLUS ScaledSpaceOuterY ScaledBorderWidth 
							ScaledSpaceInnerY FontHeight 
							ScaledSpaceInnerY ScaledBorderWidth 
							ScaledSpaceOuterY))
					      [if ImageBox
						  then 

          (* * The image box has already been calculated. Use the box height to determine if Multi-line or Single line.
	  Calculate the correct bitmap.)


							 (SETQ BoxHeight (fetch (IMAGEBOX
										      YSIZE)
									      of ImageBox))
							 (if (GREATERP BoxHeight ApproxBoxHeight)
							     then 
                                                             (* Calculate height for Multi-line icon)
								    (SETQ ScaledIconHeight
								      (PLUS ScaledBorderWidth 
									      ScaledSpaceInnerY 
									      FontHeight 
									 HalfScaledSpaceInterLine))
							   else 
                                                             (* Calculate height for Single line icon)
								  (SETQ ScaledIconHeight
								    (PLUS ScaledBorderWidth 
									    ScaledSpaceInnerY 
									    FontHeight 
									    ScaledSpaceInnerY 
									    ScaledBorderWidth)))
							 (SETQ Icon (
							     NC.FetchLinkIconAttachedBitMap Card 
										 ScaledIconHeight 
											    Scale))
							 (SETQ ScaledIconHeight
							   (TIMES Scale (BITMAPHEIGHT Icon)))
							 (SETQ ScaledIconWidth (TIMES
							     Scale
							     (BITMAPWIDTH Icon)))
						else 

          (* * The image box must be calculated)


						       (if (GREATERP ApproxBoxWidth 
									 ScaledLinkIconMaxWidth)
							   then 
                                                             (* Calculate Multi-line icon)
								  (SETQ ScaledIconHeight
								    (PLUS ScaledBorderWidth 
									    ScaledSpaceInnerY 
									    FontHeight 
									 HalfScaledSpaceInterLine))
								  (SETQ Icon (
								   NC.FetchLinkIconAttachedBitMap
								      Card ScaledIconHeight Scale))
								  (SETQ ScaledIconHeight
								    (TIMES Scale (BITMAPHEIGHT
									       Icon)))
								  (SETQ ScaledIconWidth
								    (TIMES Scale (BITMAPWIDTH
									       Icon)))
							 else 
                                                             (* Calculate Single line icon)
								(SETQ ScaledIconHeight
								  (PLUS ScaledBorderWidth 
									  ScaledSpaceInnerY 
									  FontHeight 
									  ScaledSpaceInnerY 
									  ScaledBorderWidth))
								(SETQ Icon (
								   NC.FetchLinkIconAttachedBitMap
								    Card ScaledIconHeight Scale))
								(SETQ ScaledIconHeight
								  (TIMES Scale (BITMAPHEIGHT
									     Icon)))
								(SETQ ScaledIconWidth
								  (TIMES Scale (BITMAPWIDTH
									     Icon)))

          (* * Now see if total width, including the icon, will still fit if this is a Single line)


								(if (AND (NOT (STREQUAL
										      LinkIconString 
										      ""))
									     (GREATERP
									       (PLUS ApproxBoxWidth 
										  ScaledIconWidth)
									       ScaledLinkIconMaxWidth)
									     )
								    then 
                                                             (* Now it doesn't fit. Calculate Multi-line icon)
									   (SETQ ScaledIconHeight
									     (PLUS 
										ScaledBorderWidth 
										ScaledSpaceInnerY 
										     FontHeight 
									 HalfScaledSpaceInterLine))
									   (SETQ Icon
									     (
								   NC.FetchLinkIconAttachedBitMap
									       Card ScaledIconHeight 
									       Scale))
									   (SETQ ScaledIconHeight
									     (TIMES Scale
										      (BITMAPHEIGHT
											Icon)))
									   (SETQ ScaledIconWidth
									     (TIMES Scale
										      (BITMAPWIDTH
											Icon]
				     else 

          (* * Multi-line link icons are disabled)


					    (SETQ ScaledIconHeight (PLUS ScaledBorderWidth 
									     ScaledSpaceInnerY 
									     FontHeight 
									     ScaledSpaceInnerY 
									     ScaledBorderWidth))
					    (SETQ Icon (NC.FetchLinkIconAttachedBitMap Card 
										 ScaledIconHeight 
											   Scale))
					    (SETQ ScaledIconHeight (TIMES Scale (BITMAPHEIGHT
										Icon)))
					    (SETQ ScaledIconWidth (TIMES Scale (BITMAPWIDTH
									       Icon]
			  else 

          (* * No attached icon)


				 (SETQ Icon NIL)
				 (SETQ ScaledIconHeight 0)
				 (SETQ ScaledIconWidth 0))

          (* * Now determine the text to be printed)


		        (if (AND Icon (GREATERP (PLUS ScaledIconWidth ScaledSpaceOuterX 
							      ScaledSpaceOuterX)
						      ScaledLinkIconMaxWidth))
			    then 

          (* If the width of the icon plus the outer x's is greater than the ScaledLinkIconMaxWidth set by the user, set the 
	  icon and text to NIL)


				   (SETQ Icon NIL)
				   (SETQ ScaledIconHeight 0)
				   (SETQ ScaledIconWidth 0)
				   (SETQ LinkIconStrings (LIST ""))
			  elseif (STREQUAL LinkIconString "")
			    then                           (* There is no text to print, so set it to the null 
							     string)
				   (SETQ LinkIconStrings (LIST ""))
			  else 

          (* * Have the text parsed into separate lines)


				 (SETQ LinkIconStrings (NC.CreateLinkIconStrings Link 
										   LinkIconString 
										     Icon ImageStream)
				   ))

          (* * Get the image box info for this icon)


		        (SETQ ImageBox
			  (OR ImageBox
				(NC.LinkIconImageBoxFn ImageObj ImageStream NIL NIL NIL DisplayType 
							 Title Label LinkIconString Icon 
							 LinkIconStrings)))

          (* * Compute all the size values.)


		        (SETQ Bottom (PLUS (DIFFERENCE (DSPYPOSITION NIL ImageStream)
							     (fetch (IMAGEBOX YDESC)
								of ImageBox))
					       ScaledSpaceOuterY))
		        (SETQ BoxHeight (DIFFERENCE (fetch (IMAGEBOX YSIZE) of ImageBox)
							(PLUS ScaledSpaceOuterY ScaledSpaceOuterY)))
		        (SETQ Top (PLUS Bottom BoxHeight))
		        (SETQ Left (PLUS (DSPXPOSITION NIL ImageStream)
					     ScaledSpaceOuterX))
		        (SETQ BoxWidth (DIFFERENCE (fetch (IMAGEBOX XSIZE) of ImageBox)
						       (PLUS ScaledSpaceOuterX ScaledSpaceOuterX)))

          (* * Display the icon, if possible)


		        (if (AND Icon (LEQ (PLUS ScaledIconWidth ScaledSpaceOuterX 
							 ScaledSpaceOuterX)
						 ScaledLinkIconMaxWidth))
			    then 

          (* Put out the icon bitmap, but only if the width of the icon plus the outer x's does not exceed the 
	  LinkIconMaxWidth set by the user.)


				   (BITBLT Icon 0 0 ImageStream Left (DIFFERENCE Top 
										 ScaledIconHeight))

          (* DSPFONT is a kludge to get around bug in PRESS BITBLT which sets the width of a space char to NIL.
	  Bug reported as AR %#5630.0)


				   (DSPFONT Font ImageStream)
				   (if (NOT (OR Label Title))
				       then                (* Quit because just a typeicon)
					      (RETURN))
			  else                             (* There isn't enough space for the icon, so set it to
							     NIL)
				 (SETQ Icon NIL)
				 (SETQ ScaledIconWidth 0))

          (* * Enter the appropriate text.)


		        (SETQ XPosition (PLUS Left (if Icon
							   then ScaledIconWidth
							 else ScaledBorderWidth)
						  ScaledSpaceInnerX))
		        (DSPXPOSITION XPosition ImageStream)
		        (SETQ FontDescent (FONTDESCENT Font))
		        (if LinkIconStrings
			    then (if (GREATERP (LENGTH LinkIconStrings)
						     1)
				       then (SETQ FontAscent (FONTASCENT Font))
					      (SETQ BottomOfLine (DIFFERENCE Top
										 (PLUS 
										ScaledBorderWidth 
										ScaledSpaceInnerY 
										       FontAscent)))
					      (SETQ XPosition (PLUS Left ScaledSpaceInnerX 
									ScaledBorderWidth))
					      (for String in LinkIconStrings
						 do (DSPYPOSITION BottomOfLine ImageStream)
						      (PRIN1 String ImageStream)
						      (SETQ BottomOfLine
							(DIFFERENCE BottomOfLine
								      (PLUS FontDescent 
									     ScaledSpaceInterLine 
									      FontAscent)))
						      (DSPXPOSITION XPosition ImageStream))

          (* * Draw the box.)


					      (NC.DrawInnerBox Left Bottom BoxWidth BoxHeight 
								 ScaledBorderWidth NIL ImageStream 
								 Icon ScaledIconWidth 
								 ScaledIconHeight)
				     else (DSPYPOSITION (PLUS Bottom ScaledBorderWidth 
								    ScaledSpaceInnerY FontDescent)
							    ImageStream)
					    (PRIN1 (CAR LinkIconStrings)
						     ImageStream)

          (* * Draw the box.)


					    (NC.DrawInnerBox (PLUS Left ScaledIconWidth)
							       Bottom
							       (DIFFERENCE BoxWidth ScaledIconWidth)
							       BoxHeight ScaledBorderWidth NIL 
							       ImageStream Icon])

(NC.LinkIconImageBoxFn
  [LAMBDA (ImageObj ImageStream CurrentX RightMargin DummyArg DisplayType Title Label LinkIconString 
		    Icon LinkIconStrings)                    (* pmi: " 1-May-87 18:00")

          (* * rht 9/20/84: Now scales result before returning by proper amount depending on stream type.
	  e.g. for PRESS and INTERPRESS.)



          (* * rht 11/13/84: In computation of XSIZE, extra width is figured using characters in the font, "nn", rather than 
	  absolute pixel count.)



          (* * rht 2/9/85: Now uses new displaymode format.)



          (* * rht 5/9/86: Note that RightMargin non-nil signals a TEdit stream.)



          (* * rht & fgh 5/9/86: Massive hacking to display coords, etc.)



          (* * rht 5/10/86: Rearranged order of expressions a bit and added arg to call to NC.FetchLinkIconAttachedBitMap in 
	  order to get a bitmap with correct height for the box we're drawing.)



          (* * rht 8/7/86: Now passes Scale argument to NC.FetchLinkIconAttachedBitMap. Also uses the Title and Label args if
	  non-nil. If not, then recomputes them more sensibly.)



          (* * rht 8/25/85: Fixed improperly placed comment.)



          (* * rg 11/18/86: Added TotalEdgeSpaceY to local vars declaration)



          (* * pmi 2/6/87: Overhauled to accommodate multi-line link icons)



          (* * pmi 2/27/87: Fixed problems with only displaying the attached bitmap and with displaying null titles)



          (* * pmi 5/1/87: added missing local vars declarations.)


    (DECLARE (GLOBALVARS NC.LinkIconMultiLineMode NC.LinkIconMaxWidth NC.LinkIconSpaceInnerX 
			     NC.LinkIconSpaceOuterX NC.LinkIconAttachBitmapFlg NC.LinkIconSpaceInnerY 
			     NC.LinkIconSpaceOuterY NC.LinkIconSpaceInterLine NC.LinkIconBorderWidth 
			     NC.LinkIconShowLinkTypeFlg NC.LinkIconShowTitleFlg))
    (RESETLST (RESETSAVE NIL (BQUOTE (DSPFONT , (DSPFONT NC.LinkIconFont ImageStream)
						      , ImageStream)))
		(LET ((Link (NC.FetchLinkFromLinkIcon ImageObj))
		      (Scale (DSPSCALE NIL ImageStream))
		      (StringWidth 0)
		      Card Font FontHeight ShowTitleFlg AttachBitmapFlg LinkDisplayMode 
		      ApproxBoxWidth ScaledIconHeight ScaledIconWidth NumberOfLines XSize YSize 
		      ScaledLinkIconMaxWidth ScaledBorderWidth ScaledSpaceInnerX ScaledSpaceOuterX 
		      ScaledSpaceInnerY ScaledSpaceOuterY ScaledSpaceInterLine 
		      HalfScaledSpaceInterLine TotalEdgeSpaceY)
		     (SETQ Font (DSPFONT NIL ImageStream))
		     (SETQ FontHeight (FONTHEIGHT Font))

          (* * Determine what type of Display to do)


		     (OR DisplayType (SETQ DisplayType (fetch (Link DisplayMode) of Link)))
		     (SETQ ShowTitleFlg (fetch (LINKDISPLAYMODE SHOWTITLEFLG) of DisplayType))
		     (SETQ Card (if (EQ ShowTitleFlg (QUOTE SOURCE))
				      then (fetch (Link SourceCard) of Link)
				    else (fetch (Link DestinationCard) of Link)))
		     (SETQ LinkDisplayMode (fetch (LINKDISPLAYMODE SHOWLINKTYPEFLG) of 
										      DisplayType))
		     (SETQ AttachBitmapFlg (fetch (LINKDISPLAYMODE ATTACHBITMAPFLG) of 
										      DisplayType))
		     (SETQ AttachBitmapFlg (if (EQ AttachBitmapFlg (QUOTE FLOAT))
						 then NC.LinkIconAttachBitmapFlg
					       else AttachBitmapFlg))

          (* * Construct the text for the link icon)


		     [if (NOT LinkIconString)
			 then (OR Title (SETQ Title (if (AND ShowTitleFlg
								       (OR (NEQ ShowTitleFlg
										    (QUOTE FLOAT))
									     NC.LinkIconShowTitleFlg))
							      then (NC.RetrieveTitle Card)
							    else NIL)))
				[OR Label (SETQ Label (AND (if (EQ LinkDisplayMode
									     (QUOTE FLOAT))
								     then 
								       NC.LinkIconShowLinkTypeFlg
								   else LinkDisplayMode)
								 (fetch (Link Label) of Link]
				(SETQ LinkIconString (CONCAT (COND
								   (Label (CONCAT "<" Label ">"))
								   (T ""))
								 (COND
								   ((AND Label Title)
								     " ")
								   (T ""))
								 (OR Title ""]

          (* * Make temporaries of scaled vars.)


		     (SETQ ScaledLinkIconMaxWidth (TIMES Scale NC.LinkIconMaxWidth))
		     (SETQ ScaledBorderWidth (TIMES Scale NC.LinkIconBorderWidth))
		     (SETQ ScaledSpaceInnerX (TIMES Scale NC.LinkIconSpaceInnerX))
		     (SETQ ScaledSpaceOuterX (TIMES Scale NC.LinkIconSpaceOuterX))
		     (SETQ ScaledSpaceInnerY (TIMES Scale NC.LinkIconSpaceInnerY))
		     (SETQ ScaledSpaceOuterY (TIMES Scale NC.LinkIconSpaceOuterY))
		     (SETQ ScaledSpaceInterLine (TIMES Scale NC.LinkIconSpaceInterLine))
		     (SETQ HalfScaledSpaceInterLine (IQUOTIENT ScaledSpaceInterLine 2))

          (* *)


		     (SETQ TotalEdgeSpaceY (PLUS ScaledBorderWidth ScaledSpaceOuterY 
						     ScaledSpaceInnerY))
		     (SETQ YSize (PLUS TotalEdgeSpaceY TotalEdgeSpaceY FontHeight))

          (* * Set up the icon, if displayed)


		     (if AttachBitmapFlg
			 then 

          (* * Attached icon)



          (* * Use an estimate of the width to tell if the box contains more than one line of text)


				[if NC.LinkIconMultiLineMode
				    then 

          (* * Multi-line link icons are enabled)


					   (SETQ ApproxBoxWidth (PLUS ScaledSpaceOuterX 
									  ScaledBorderWidth 
									  ScaledSpaceInnerX
									  (STRINGWIDTH 
										   LinkIconString 
											 Font)
									  ScaledSpaceInnerX 
									  ScaledBorderWidth 
									  ScaledSpaceOuterX))

          (* * The image box must be calculated)


					   [if (GREATERP ApproxBoxWidth ScaledLinkIconMaxWidth)
					       then        (* Calculate Multi-line icon)
						      (SETQ ScaledIconHeight
							(PLUS ScaledBorderWidth ScaledSpaceInnerY 
								FontHeight HalfScaledSpaceInterLine))
						      (SETQ Icon (NC.FetchLinkIconAttachedBitMap
							  Card ScaledIconHeight Scale))
						      (SETQ ScaledIconHeight (TIMES Scale
											(
										     BITMAPHEIGHT
											  Icon)))
						      (SETQ ScaledIconWidth (TIMES Scale
										       (BITMAPWIDTH
											 Icon)))
					     else          (* Calculate Single line icon)
						    (SETQ ScaledIconHeight
						      (PLUS ScaledBorderWidth ScaledSpaceInnerY 
							      FontHeight ScaledSpaceInnerY 
							      ScaledBorderWidth))
						    (SETQ Icon (NC.FetchLinkIconAttachedBitMap
							Card ScaledIconHeight Scale))
						    (SETQ ScaledIconHeight (TIMES Scale
										      (BITMAPHEIGHT
											Icon)))
						    (SETQ ScaledIconWidth (TIMES Scale
										     (BITMAPWIDTH
										       Icon)))

          (* * Now see if total width, including the icon, will still fit if this is a Single line)


						    (if (AND (NOT (STREQUAL LinkIconString "")
									)
								 (GREATERP (PLUS ApproxBoxWidth 
										  ScaledIconWidth)
									     ScaledLinkIconMaxWidth))
							then 
                                                             (* Now it doesn't fit. Calculate Multi-line icon)
							       (SETQ ScaledIconHeight
								 (PLUS ScaledBorderWidth 
									 ScaledSpaceInnerY FontHeight 
									 HalfScaledSpaceInterLine))
							       (SETQ Icon (
								   NC.FetchLinkIconAttachedBitMap
								   Card ScaledIconHeight Scale))
							       (SETQ ScaledIconHeight
								 (TIMES Scale (BITMAPHEIGHT
									    Icon)))
							       (SETQ ScaledIconWidth
								 (TIMES Scale (BITMAPWIDTH Icon]
				  else 

          (* * Multi-line link icons are disabled)


					 (SETQ ScaledIconHeight (PLUS ScaledBorderWidth 
									  ScaledSpaceInnerY 
									  FontHeight 
									  ScaledSpaceInnerY 
									  ScaledBorderWidth))
					 (SETQ Icon (NC.FetchLinkIconAttachedBitMap Card 
										 ScaledIconHeight 
											Scale))
					 (SETQ ScaledIconHeight (TIMES Scale (BITMAPHEIGHT
									     Icon)))
					 (SETQ ScaledIconWidth (TIMES Scale (BITMAPWIDTH Icon]
		       else 

          (* * No attached icon)


			      (SETQ Icon NIL)
			      (SETQ ScaledIconHeight 0)
			      (SETQ ScaledIconWidth 0))

          (* * Now determine the actual size of the image box)


		     [if (AND Icon (GREATERP (PLUS ScaledIconWidth ScaledSpaceOuterX 
							   ScaledSpaceOuterX)
						   ScaledLinkIconMaxWidth))
			 then 

          (* If the width of the icon plus the outer x's is greater than the ScaledLinkIconMaxWidth set by the user, set the 
	  box width to ScaledLinkIconMaxWidth)


				(SETQ XSize ScaledLinkIconMaxWidth)
		       elseif (STREQUAL LinkIconString "")
			 then (if Icon
				    then                   (* There is no text to print, so the box width is just
							     the icon width plus the outer x on each side)
					   (SETQ XSize (PLUS ScaledIconWidth ScaledSpaceOuterX 
								 ScaledSpaceOuterX))
				  else 

          (* There is no text or Icon to print, so the box width is just the inner x, border width, and outer x on each side)


					 (SETQ XSize (PLUS ScaledSpaceInnerX ScaledSpaceInnerX 
							       ScaledBorderWidth ScaledBorderWidth 
							       ScaledSpaceOuterX ScaledSpaceOuterX)))
		       else                                (* Have the text parsed into separate lines)
			      (SETQ LinkIconStrings (NC.CreateLinkIconStrings Link LinkIconString 
										  Icon ImageStream))
			      (if LinkIconStrings
				  then (SETQ NumberOfLines (LENGTH LinkIconStrings))
					 (if (GREATERP NumberOfLines 1)
					     then (SETQ StringWidth (PLUS (STRINGWIDTH
										  (CAR 
										  LinkIconStrings)
										  Font)
										ScaledIconWidth))
                                                             (* Find the longest string for the width of the box)
						    (for String in (CDR LinkIconStrings)
						       bind PartStringWidth
						       when (GREATERP (SETQ PartStringWidth
									    (STRINGWIDTH String 
											   Font))
									  StringWidth)
						       do (SETQ StringWidth PartStringWidth))
						    (SETQ YSize (PLUS TotalEdgeSpaceY 
									  TotalEdgeSpaceY
									  (TIMES NumberOfLines 
										   FontHeight)
									  (TIMES (SUB1 
										    NumberOfLines)
										   
									     ScaledSpaceInterLine)))
					   else (SETQ StringWidth (PLUS (STRINGWIDTH
										(CAR 
										  LinkIconStrings)
										Font)
									      ScaledIconWidth)))
					 (SETQ XSize (MIN (PLUS StringWidth ScaledSpaceOuterX 
								      ScaledSpaceOuterX 
								      ScaledSpaceInnerX 
								      ScaledSpaceInnerX
								      (if Icon
									  then ScaledBorderWidth
									else (PLUS 
										ScaledBorderWidth 
										ScaledBorderWidth)))
							      (PLUS (TIMES Scale 
									   ScaledLinkIconMaxWidth)
								      ScaledSpaceOuterX 
								      ScaledSpaceOuterX]
		     (create IMAGEBOX
			       XSIZE ← XSize
			       YSIZE ← YSize
			       YDESC ← (COND
				 (RightMargin                (* This is in a TEdittextstream)
					      (PLUS (FONTDESCENT Font)
						      TotalEdgeSpaceY))
				 (T 0))
			       XKERN ← 0])

(NC.LinkIconImageObjP
  (LAMBDA (IMAGEOBJ)                                         (* fgh: " 1-Oct-84 20:04")

          (* * Is IMAGEOBJ a Link Icon?)


    (AND (IMAGEOBJP IMAGEOBJ)
	   (EQ (FUNCTION NC.LinkIconGetFn)
		 (IMAGEOBJPROP IMAGEOBJ (QUOTE GETFN))))))

(NC.LinkIconPutFn
  [LAMBDA (ImageObject Stream)                               (* rht: "15-Jul-86 14:52")

          (* * Writes the Link information in the link icon specified by ImageObject onto the file specified by Stream)



          (* * fgh 11/16/85 Updated to handle Link DATATYPE)



          (* * rht 7/15/86: Now checks for old style links. If so, then put down a placeholder imageobj.)


    (LET ((Link (NC.FetchLinkFromLinkIcon ImageObject)))
         (if (type? Link Link)
	     then (NC.WriteLink Link Stream)
	   else (IMAGEOBJPROP ImageObject (QUOTE OBJECTDATUM)
				  "[[Old style link.]]")
		  (replace (IMAGEOBJ IMAGEOBJFNS) of ImageObject with 
								   NC.ExternalPutLinkIconImageFns)
		  (NC.ExternalPutLinkIconPutFn ImageObject Stream])

(NC.LinkIconGetFn
  (LAMBDA (Stream)                                           (* fgh: "16-Nov-85 20:40")

          (* * Reads the link information from Stream and returns a link icon image object for this link info.
	  If the value on the stream is an atom in the old style, convert to a proper record.)


    (NC.MakeLinkIcon (NC.ReadLink Stream))))

(NC.LinkIconMiddleButtonFn
  [LAMBDA (LinkIcon Window)                                  (* pmi: "25-Mar-87 12:43")
                                                             (* Middle button has been pressed in the Notecard 
							     pointer. Bring up menu of choices for manipulating 
							     this pointer.)

          (* * rht 11/18/86: Now calls NC.TraverseLink rather than NC.EditNoteCard.)



          (* * rht 2/4/87: Now checks the ExtraLinkIconMenuItems property of the destination's card type atom.
	  If non-nil, then include those items in link icon menu.)



          (* * pmi 3/25/87: Added NC.MenuFont to all menus)


    (DECLARE (GLOBALVARS NC.LinkIconMiddleButtonMenu NC.MenuFont))
    (LET ((Card (fetch (Link DestinationCard) of (NC.FetchLinkFromLinkIcon LinkIcon)))
	  ExtraItems Selection Menu)
         (SETQ ExtraItems (GETPROP (NC.RetrieveType Card)
				       (QUOTE ExtraLinkIconMenuItems)))
         [SETQ Menu (COND
	     ((AND (BOUNDP (QUOTE NC.LinkIconMiddleButtonMenu))
		     (type? MENU NC.LinkIconMiddleButtonMenu)
		     (NULL ExtraItems))
	       NC.LinkIconMiddleButtonMenu)
	     (T (create MENU
			  ITEMS ← (APPEND (QUOTE ((Bring% Up% Card/Box (QUOTE Edit)
									   
					     "Bring up the note card pointed to by this pointer.")
						       (Change% Link% Type (QUOTE Relabel)
									   
							      "Change the label of this pointer.")
						       (Change% Card% Title (QUOTE Retitle)
									    
							   "Change the destination card's title.")
						       (Change% Display% Mode (QUOTE Display)
									      
				      "Change the type of information displayed in this pointer.")))
					    ExtraItems)
			  CENTERFLG ← T
			  TITLE ← "Link Ops"
			  MENUFONT ← NC.MenuFont
			  ITEMHEIGHT ← (IPLUS (FONTPROP NC.MenuFont (QUOTE HEIGHT))
						1]
         (if (NULL ExtraItems)
	     then (SETQ NC.LinkIconMiddleButtonMenu Menu))
         (if (NC.CardP (NC.CoerceToCard Window))
	     then (SELECTQ (SETQ Selection (MENU Menu))
			       (Edit (NC.TraverseLink LinkIcon Window))
			       (Display (NC.ChangeLinkDisplayMode LinkIcon Window))
			       (Relabel (NC.RelabelLink LinkIcon Window))
			       (Retitle (NC.ChangeCardTitleFromLinkIcon LinkIcon Window))
			       (NIL NIL)
			       (APPLY* Selection Card))
	   else NIL])

(NC.LinkIconWhenCopiedFn
  [LAMBDA (ImageObject ToWindowStream FromTextStream ToTextStream)
                                                             (* rht: "25-Jan-87 20:10")

          (* * Called when copying a link icon from FromTextStream to ToWindowStream. Sets the necessary link information up 
	  for card corresponding to ToWindowStream.)



          (* * rht 11/18/84: Major hacking. Now checks for all sorts of illegal cases. Either goes ahead with copy, converts 
	  link type to "Unspecified", or deletes the new "invisible" link. The latter will change when imageobj fns can 
	  return DON'T.)



          (* * rht 12/12/84: now does RETFROM instead of adding processes to delete imageobj's. This is cleaner, but still 
	  ugly.)



          (* * rht 3/25/85: Fixed to handle copies within a sketch card. That is indicated by nullity of the two textstream 
	  args.)



          (* * rht 3/24/86: Changed call to NC.CoerceToID to NC.CoerceToCard)



          (* * fgh 6/30/86 Now ERROR! out of attempt to copy between NoteFiles)



          (* * rht 7/14/86: Reversed order of tests for WINDOWP and TEXTSTREAMP in computation of NewSourceCard.)



          (* * rht 8/11/86: Now only resets NewSourceCard if it was NIL so that copies to a sketch will work.)



          (* * rht 10/5/86: Undid change fgh 6/30/86: now allows copies of links across notefiles. Also handles illegal 
	  copies differently. Rather than RETFROM'ing or ERROR!'ing, it puts in a DELETE ME imageobj.)



          (* * rht 12/16/86: Now passes Window to NC.GetCrossFileLinkDestCard.)



          (* * rht 1/21/87: Now handles attempt to copy to non-NoteCards stream differently than other illegalities.)



          (* * rht 1/25/87: Now stashes new link created on source card's user data props for use by NC.LinkAtCharPos in case
	  this is a copy of multiple links.)


    (DECLARE (GLOBALVARS NC.CopyCrossFileLinksMode NC.DELETEMEImageObjDatum NC.SystemLinkLabels 
			     NC.UnspecifiedLinkLabel NC.NewCrossFileLinksTwoWayFlg))
    (LET ((Link (NC.FetchLinkFromLinkIcon ImageObject))
	  (ImageObjectCopy (IMAGEOBJPROP ImageObject (QUOTE ImageObjectCopy)))
	  (InTEditCopyFlg (COND
			    ((STKPOS (QUOTE TEDIT.COPY))
			      T)))
	  (Window (AND ToWindowStream (WFROMDS ToWindowStream T)))
	  SourceCard DestinationCard NewSourceCard NewLabel NewDisplayMode OldDisplayMode Label 
	  InsertPos IllegalCopyFlg ExternalCopyFlg)
         (SETQ Label (fetch (Link Label) of Link))
         (SETQ SourceCard (fetch (Link SourceCard) of Link))
         (SETQ DestinationCard (fetch (Link DestinationCard) of Link))
         (SETQ OldDisplayMode (fetch (Link DisplayMode) of Link))
         [SETQ NewSourceCard (COND
	     ((TEXTSTREAMP ToTextStream)
	       (NC.CoerceToCard ToTextStream))
	     ((WINDOWP Window)
	       (NC.CoerceToCard Window]
         [COND
	   ((AND (NULL NewSourceCard)
		   (NULL FromTextStream)
		   (NULL ToTextStream))                    (* We must be moving within a sketch.)
	     (SETQ NewSourceCard SourceCard))
	   ((TEXTSTREAMP ToTextStream)                     (* If it's a text copy, then compute position to 
							     insert link at.)
	     (SETQ InsertPos (NC.CharPosFromTextObject (TEXTOBJ ToTextStream]
         [COND
	   ((NULL NewSourceCard)                           (* Trying to copy to a non NoteCard stream)
	     (NC.PrintMsg Window NIL (CONCAT "Tried to copy a NoteCards link icon" 
						 " to a non-NoteCards stream."
						 (CHARACTER 13)))
	     (SETQ ExternalCopyFlg T))
	   ([NULL (NC.LinksSupportedP NewSourceCard (QUOTE (Local]
	     (NC.PrintMsg Window NIL (CONCAT "Tried to copy a NoteCards link icon" 
						 " to a NoteCard that"
						 " does not support links!!."
						 (CHARACTER 13)))
	     (SETQ IllegalCopyFlg T))
	   ((AND (FMEMB Label (QUOTE (FiledCard SubBox)))
		   (NEQ (NC.RetrieveType NewSourceCard)
			  (QUOTE FileBox)))                (* Copy from filebox to non-filebox.)
	     (NC.PrintMsg Window NIL (CONCAT 
				       "Tried to copy filedcard or subbox link to a non-filebox."
						 (CHARACTER 13)
						 "Link type of copy set to 'Unspecified'."
						 (CHARACTER 13)))
	     (SETQ NewLabel NC.UnspecifiedLinkLabel)
	     (SETQ NewDisplayMode (create LINKDISPLAYMODE
				       copying OldDisplayMode SHOWTITLEFLG ← T SHOWLINKTYPEFLG ← T))
	     )
	   ((AND (NEQ (NC.RetrieveType SourceCard)
			  (QUOTE FileBox))
		   (EQ (NC.RetrieveType NewSourceCard)
			 (QUOTE FileBox)))                 (* Copy from non-filebox to filebox.)
	     (NC.PrintMsg Window NIL (CONCAT "Can't copy links from non-filebox to filebox."
						 (CHARACTER 13)
						 "Try using 'PutCardsHere'."
						 (CHARACTER 13)))
	     (SETQ IllegalCopyFlg T))
	   ((AND (FMEMB Label (QUOTE (FiledCard SubBox)))
		   (EQ NewSourceCard SourceCard))          (* Copy within same filebox.)
	     (NC.PrintMsg Window NIL (CONCAT 
					  "Can't copy links within a FileBox.  Try move instead."
						 (CHARACTER 13)))
	     (SETQ IllegalCopyFlg T))
	   ([AND (EQ (NC.RetrieveType NewSourceCard)
			 (QUOTE FileBox))
		   (for Link1 in (NC.FetchToLinks NewSourceCard)
		      thereis (AND (NC.ChildLinkP Link1)
				       (EQ DestinationCard (fetch (Link DestinationCard)
								of Link1]
                                                             (* Copy to a filebox already containing this child.)
	     (NC.PrintMsg Window NIL (CONCAT (NC.RetrieveTitle DestinationCard)
						 " not copied: already appears as a child of "
						 (NC.RetrieveTitle NewSourceCard)
						 (CHARACTER 13)))
	     (SETQ IllegalCopyFlg T))
	   ([AND (EQ Label (QUOTE SubBox))
		   (OR (EQ NewSourceCard DestinationCard)
			 (NOT (NC.NotDaughterP DestinationCard NewSourceCard
						   (FUNCTION NC.ChildLinkP]
                                                             (* Copy to a filebox causes a cycle.)
	     (NC.PrintMsg Window NIL (CONCAT "Couldn't copy " Link " because of subbox cycle."
						 (CHARACTER 13)))
	     (SETQ IllegalCopyFlg T))
	   ((AND (FMEMB Label NC.SystemLinkLabels)
		   [NOT (FMEMB Label (QUOTE (FiledCard SubBox]
		   (NEQ NewSourceCard SourceCard))         (* Copy of system link outside of own card.)
	     (NC.PrintMsg Window NIL (CONCAT "Tried to copy system link." (CHARACTER 13)
						 "Link type of copy set to 'Unspecified'."
						 (CHARACTER 13)))
	     (SETQ NewLabel NC.UnspecifiedLinkLabel)
	     (SETQ NewDisplayMode (create LINKDISPLAYMODE
				       copying OldDisplayMode SHOWTITLEFLG ← T SHOWLINKTYPEFLG ← T))
	     )
	   ((NC.CrossFileLinkCardP DestinationCard)        (* Copy of a cross-file link.)
	     (if (NOT (NC.ActiveCardP DestinationCard))
		 then (NC.GetNoteCard DestinationCard))
                                                             (* If it's to be two-way, then make a fresh 
							     CrossFileLinkCard.)
	     (if (OR (EQ NC.CopyCrossFileLinksMode (QUOTE TWOWAY))
			 (AND (NULL NC.CopyCrossFileLinksMode)
				NC.NewCrossFileLinksTwoWayFlg))
		 then (SETQ DestinationCard (NC.GetCrossFileLinkDestCard DestinationCard Window)
			  ))
	     (OR DestinationCard (SETQ IllegalCopyFlg T]
         [COND
	   (IllegalCopyFlg (IMAGEOBJPROP ImageObjectCopy (QUOTE OBJECTDATUM)
					   NC.DELETEMEImageObjDatum)
			   (NC.SetUserDataProp SourceCard (QUOTE NeedsCleaningFlg)
						 T))
	   (ExternalCopyFlg (NC.CoerceToExternalPutLinkIcon ImageObjectCopy SourceCard 
							      DestinationCard (OR NewLabel Label)))
	   (T (NC.SetUserDataProp NewSourceCard (QUOTE LastLinkCopiedOrMoved)
				    (NC.FillInLinkIcon ImageObjectCopy (OR NewLabel Label)
							 DestinationCard NewSourceCard
							 (OR NewDisplayMode OldDisplayMode)
							 (NC.LinkAtCharPos InsertPos ToTextStream)
							 NC.CopyCrossFileLinksMode]
         (IMAGEOBJPROP ImageObject (QUOTE ImageObjectCopy)
			 NIL])

(NC.LinkIconWhenDeletedFn
  [LAMBDA (ImageObject Stream)                               (* rht: "26-Mar-87 19:10")

          (* * When deleting a link icon image object from a card, make sure the link information on the source and 
	  destinmation cards get updated. When deleting a link image object from a prop list editor, just inform the user 
	  that this is a useless operation.)



          (* * rht 11/19/84: Now handles the case when a move is in progress. True if the old link is cached on the ImageObj.
	  Also changed so that SourceID is computed from ImageObject rather than from Stream. Similarly for the window.)



          (* * kirk 14Nov85: deleted use of PSA.Database)



          (* * fgh 5/2/86 Added code to handle deleting of links from ShowLinks editors.)



          (* * fgh 6/4/86 Added KLUDGE to take care of case when this is called under TEDIT.UNDO.MOVE)



          (* * rht 7/4/86: Now checks for readonly cards. Also passes checks for deleting from orphan card when calling 
	  NC.DeleteLink.)



          (* * fgh 7/14/86 Now deletes link icon directly, rather than depending on DeleteLink to do it.
	  Sets the Don'tDeleteLinkIconFlg in the call to DeleteLink to do so.)



          (* * rht 8/11/86: Now computes InsideShowLinksP and lets that determine value of Don'tDelLinkIcon flg in call to 
	  NC.DeleteLink.)



          (* * rht&pmi 12/8/86: Now checks whether we're called by TEDIT.MOVE. If so, don't cause orphan hook under 
	  NC.DelFromLink.)



          (* * rht 3/26/87: Now passes non-nil NoOrphanHookFlg to NC.DeleteLink in case we're deleting a cross-file link.)


    (DECLARE (GLOBALVARS NC.DeletedLinkImageObject))
    (LET ((Link (NC.FetchLinkFromLinkIcon ImageObject))
	  LinkType OldLink StkPtr SourceCard DestinationCard InsideShowLinksP)
         (COND
	   [(NC.CheckForNotReadOnly (fetch (Link SourceCard) of Link)
				      NIL "Can't delete links in ")
	     (COND
	       ((IMAGEOBJPROP ImageObject (QUOTE LinkBeingMovedWithinCard))
                                                             (* TEdit is trying to delete the old link imageobj 
							     after moving within a card.)
		 (IMAGEOBJPROP ImageObject (QUOTE LinkBeingMovedWithinCard)
				 NIL))
	       ((SETQ OldLink (IMAGEOBJPROP ImageObject (QUOTE LinkBeingMoved)))
                                                             (* Pass non-nil NoOrphanHookFlg arg to NC.DelFromLink 
							     if we are under a TEDIT.MOVE call.)
		 [NC.DelFromLink OldLink (RELSTK (STKPOS (QUOTE TEDIT.MOVE]
		 (NC.DelToLink OldLink)
		 (replace (Link UID) of OldLink with -1)
		 (IMAGEOBJPROP ImageObject (QUOTE LinkBeingMoved)
				 NIL))
	       ((SETQ StkPtr (STKPOS (QUOTE TEDIT.UNDO.MOVE)))

          (* * Called from TEDIT.UNDO.MOVE, don't relly want to delete the links. KLUDGE because TEDIT will not call the 
	  LinkIconInsertFn later on.)


		 (RELSTK StkPtr)
		 NIL)
	       (T 

          (* * if this is reversed source/dest {i.e., a From link} inside the show links editor, then return it to its 
	  original direction)


		  [COND
		    ((AND (SETQ InsideShowLinksP (IMAGEOBJPROP ImageObject (QUOTE 
									     InsidePropListEditor)))
			    (LISTGET (fetch (Link UserData) of Link)
				       (QUOTE Reversed)))
		      (SETQ Link (create Link using Link SourceCard ← (fetch (Link 
										  DestinationCard)
									       of Link)
							  DestinationCard ← (fetch (Link SourceCard)
									       of Link]

          (* * Then just delete the link)


		  (NC.CheckForOrphanDelete (SETQ SourceCard (fetch (Link SourceCard)
								   of Link))
					     (SETQ DestinationCard (fetch (Link DestinationCard)
									of Link)))
		  (NC.DeleteLink Link (OR (NC.SameCardP SourceCard (fetch (NoteFile 
										      OrphansCard)
									    of (fetch
										   (Card NoteFile)
										    of SourceCard)))
					      (NC.CrossFileLinkCardP DestinationCard)
					      (NC.CrossFileLinkCardP SourceCard))
				   (NOT InsideShowLinksP))
		  (create IMAGEOBJ smashing ImageObject OBJECTDATUM ← (fetch (IMAGEOBJ 
										      OBJECTDATUM)
									     of 
									NC.DeletedLinkImageObject)
						IMAGEOBJPLIST ← (fetch (IMAGEOBJ IMAGEOBJPLIST)
								   of NC.DeletedLinkImageObject)
						IMAGEOBJFNS ← (fetch (IMAGEOBJ IMAGEOBJFNS)
								 of NC.DeletedLinkImageObject))
		  (replace (Link UID) of Link with -1]
	   (T (ERROR!])

(NC.LinkIconWhenInsertedFn
  (LAMBDA (ImageObj WindowStream)                            (* rht: "15-Nov-85 16:06")
                                                             (* An Notecard link is being inserted.
							     If this is a deleteundo, then have to re-establish the
							     link information)

          (* * kirk 15Nov85: deleted use of PSA.Database)


    (LET ((StackPointer (STKPOS (QUOTE TEDIT.UNDO.DELETION)))
	  SourceCard Link)
         (COND
	   ((NULL StackPointer)
	     (RELSTK StackPointer))
	   (T (RELSTK StackPointer)
	      (SETQ SourceCard (NC.CoerceToCard WindowStream))
	      (COND
		((NC.CardP SourceCard)
		  (SETQ Link (NC.FetchLinkFromLinkIcon ImageObj))
		  (NC.AddFromLink Link)
		  (NC.AddToLink Link))))))))

(NC.LinkIconWhenMovedFn
  [LAMBDA (ImageObject ToWindowStream FromTextStream ToTextStream)
                                                             (* rht: "25-Jan-87 20:10")

          (* * Called when moving a link icon from FromTextStream to ToWindowStream. Sets the necessary link information up 
	  for card corresponding to ToWindowStream.)



          (* * rht 11/18/84: Major hacking. Now checks for all sorts of illegal cases. Either goes ahead with move, converts 
	  link type to "Unspecified", or deletes the new "invisible" link. The code is very similar to 
	  NC.LinkIconWhenCopiedFn except that within-filebox moves are allowed. Also when aborting a move, we must insert a 
	  copy of the link back to take the place of the deleted original. This will all change when imageobj fns can return 
	  DON'T.)



          (* * rht 12/12/84: Now just RETFROM's rather than doing the addprocess stuff. Should be cleaner, but still ugly.)



          (* * kirk 15Nov85: deleted use of PSA.Database)



          (* * rht 3/24/86: Changed call to NC.CoerceToID to NC.CoerceToCard)



          (* * kirk 24Mar86: added Dave Newman patch to avoid duplicate links in sketch cards)



          (* * rht 7/14/86: Reversed order of tests for WINDOWP and TEXTSTREAMP in computation of NewSourceCard.)



          (* * rht 8/11/86: Now only resets NewSourceCard if it was NIL so that moves to a sketch will work.
	  Now checks if SourceCard = NewSourceCard in which case we're moving within a card. Don't make a new link in that 
	  case.)



          (* * rht 10/5/86: Undid change fgh 6/30/86: now allows copies of links across notefiles.)



          (* * rht 11/13/86: Removed "tsk, tsk" messages.)



          (* * rht 12/16/86: Fixed calls to NC.PrintMsg to use Window arg.)



          (* * rht 1/25/87: Now stashes new link created on source card's user data props for use by NC.LinkAtCharPos in case
	  this is a copy of multiple links.)


    (DECLARE (GLOBALVARS NC.SystemLinkLabels NC.UnspecifiedLinkLabel))
    (LET (Label (Link (NC.FetchLinkFromLinkIcon ImageObject))
		SourceCard DestinationCard NewSourceCard NewLabel NewDisplayMode OldDisplayMode
		(Window (AND ToWindowStream (WFROMDS ToWindowStream T)))
		InsertPos)
         (SETQ Label (fetch (Link Label) of Link))
         [SETQ NewSourceCard (COND
	     ((TEXTSTREAMP ToTextStream)
	       (NC.CoerceToCard ToTextStream))
	     ((WINDOWP Window)
	       (NC.CoerceToCard Window]
         (SETQ SourceCard (fetch (Link SourceCard) of Link))
         (SETQ DestinationCard (fetch (Link DestinationCard) of Link))
         (SETQ OldDisplayMode (fetch (Link DisplayMode) of Link))
         [COND
	   ((AND (NULL NewSourceCard)
		   (NULL FromTextStream)
		   (NULL ToTextStream))                    (* We must be moving within a sketch.)
	     (SETQ NewSourceCard SourceCard))
	   ((TEXTSTREAMP ToTextStream)                     (* If it's a text copy, then compute position to 
							     insert link at.)
	     (SETQ InsertPos (NC.CharPosFromTextObject (TEXTOBJ ToTextStream]
         [COND
	   ((NULL NewSourceCard)                           (* Trying to copy to a non NoteCard stream)
	     (NC.PrintMsg Window NIL (CONCAT "Tried to move a NoteCards link icon" 
						 " to a non-NoteCards stream!!."
						 (CHARACTER 13)))
	     (RETFROM (QUOTE TEDIT.MOVE)
			NIL T))
	   ([NULL (NC.LinksSupportedP NewSourceCard (QUOTE (Local]
	     (NC.PrintMsg Window NIL (CONCAT "Tried to move a NoteCards link icon" 
						 " to a NoteCard that"
						 " does not support links!!."
						 (CHARACTER 13)))
	     (RETFROM (QUOTE TEDIT.MOVE)
			NIL T))
	   ((AND (FMEMB Label (QUOTE (FiledCard SubBox)))
		   (NEQ (NC.RetrieveType NewSourceCard)
			  (QUOTE FileBox)))                (* Move from filebox to non-filebox.)
	     (NC.PrintMsg Window NIL (CONCAT 
				       "Tried to move filedcard or subbox link to a non-filebox."
						 (CHARACTER 13)
						 "Link type of copy set to 'Unspecified'."
						 (CHARACTER 13)))
	     (SETQ NewLabel NC.UnspecifiedLinkLabel)
	     (SETQ NewDisplayMode (create LINKDISPLAYMODE
				       copying OldDisplayMode SHOWTITLEFLG ← T SHOWLINKTYPEFLG ← T))
	     )
	   ((AND (NEQ (NC.RetrieveType SourceCard)
			  (QUOTE FileBox))
		   (EQ (NC.RetrieveType NewSourceCard)
			 (QUOTE FileBox)))                 (* Move from non-filebox to filebox.)
	     (NC.PrintMsg Window NIL (CONCAT "Can't move links from non-filebox to filebox."
						 (CHARACTER 13)
						 "Try using PutCardsHere."
						 (CHARACTER 13)))
	     (RETFROM (QUOTE TEDIT.MOVE)
			NIL T))
	   ([AND (EQ (NC.RetrieveType NewSourceCard)
			 (QUOTE FileBox))
		   (NEQ NewSourceCard SourceCard)
		   (for Link1 in (NC.FetchToLinks NewSourceCard)
		      thereis (AND (NC.ChildLinkP Link1)
				       (EQ DestinationCard (fetch (Link DestinationCard)
								of Link1]
                                                             (* Move to a filebox already containing this child.)
	     (NC.PrintMsg Window NIL (CONCAT (NC.RetrieveTitle DestinationCard)
						 " not moved: already appears as a child of "
						 (NC.RetrieveTitle NewSourceCard)
						 (CHARACTER 13)))
	     (RETFROM (QUOTE TEDIT.MOVE)
			NIL T))
	   ([AND (EQ Label (QUOTE SubBox))
		   (NEQ NewSourceCard SourceCard)
		   (OR (EQ NewSourceCard DestinationCard)
			 (NOT (NC.NotDaughterP DestinationCard NewSourceCard
						   (FUNCTION NC.ChildLinkP]
                                                             (* Move to a filebox causes a cycle.)
	     (NC.PrintMsg Window NIL (CONCAT "Couldn't move " Link " because of subbox cycle."
						 (CHARACTER 13)))
	     (RETFROM (QUOTE TEDIT.MOVE)
			NIL T))
	   ((AND (FMEMB Label NC.SystemLinkLabels)
		   [NOT (FMEMB Label (QUOTE (FiledCard SubBox]
		   (NEQ NewSourceCard SourceCard))         (* Move of system link outside of own card.)
	     (NC.PrintMsg Window NIL (CONCAT "Tried to copy system link." (CHARACTER 13)
						 "Link type of copy set to 'Unspecified'."
						 (CHARACTER 13)))
	     (SETQ NewLabel NC.UnspecifiedLinkLabel)
	     (SETQ NewDisplayMode (create LINKDISPLAYMODE
				       copying OldDisplayMode SHOWTITLEFLG ← T SHOWLINKTYPEFLG ← T]
         (if (EQ SourceCard NewSourceCard)
	     then                                          (* Moving within a card, so just reposition Link in 
							     ToLinks list.)
		    (AND (NC.TEditBasedP SourceCard)
			   (IMAGEOBJPROP ImageObject (QUOTE LinkBeingMovedWithinCard)
					   Link))
		    (NC.AddToLink Link (NC.LinkAtCharPos InsertPos ToTextStream)
				    T)
	   else (IMAGEOBJPROP ImageObject (QUOTE LinkBeingMoved)
				  Link)
		  (NC.SetUserDataProp NewSourceCard (QUOTE LastLinkCopiedOrMoved)
					(NC.FillInLinkIcon ImageObject (OR NewLabel Label)
							     DestinationCard NewSourceCard
							     (OR NewDisplayMode OldDisplayMode)
							     (NC.LinkAtCharPos InsertPos 
										 ToTextStream])

(NC.FetchLinkFromLinkIcon
  (LAMBDA (LinkIcon)                                         (* fgh: "16-Nov-85 17:57")

          (* * Return the link information from the LinkIcon image object)



          (* * rht 7/18/85: Changed to convert to typed links.)



          (* * fgh 11/16/85 Removed code to make link into aTYPEDRECORD, since links are now datatypes.)


    (NC.CheckDisplayModeFormat (IMAGEOBJPROP LinkIcon (QUOTE OBJECTDATUM)))))

(NC.ChangeLinkDisplayMode
  [LAMBDA (LinkOrLinkIcon Window NewDisplayMode)             (* Randy.Gobbel "15-May-87 16:39")

          (* * Change the display mode of the NOTECARDLINK defined by LinkOrLinkIcon. Have the user choose the new display 
	  mode.)



          (* * NOTE: assumes that the SourceID card of the link is active, i.e., the substance and links are cached for this 
	  card.)



          (* * rht 2/1/85: Now doesn't do all the work if new display mode is same as the old one. Or if user doesn't make a 
	  selection from the menu.)



          (* * kef 8/8/86: Now does the write through to the NoteFile even if the card is active, but not being edited.
	  Also, in the case of doing the write through, puts only the from links.)



          (* * fgh 8/30/86 Adapted to call NC.IfCardPartNotBusy.)



          (* * rht 9/29/86: Changed Ken's call to NC.CardBeingEditedP to NC.ActiveCardP and other minor changes.)



          (* * rht 2/18/87: Added check for readonly notefile to fix bug %#376.0)



          (* * rg 5/15/87 now calls NC.LinksCachedP instead of NC.ActiveCardP when changing FromLinks)


    (LET (Card DestinationCard Link LinkIcon GlobalLinkFlg OldDisplayMode)
         [if (NC.LinkIconImageObjP LinkOrLinkIcon)
	     then (SETQ Link (NC.FetchLinkFromLinkIcon LinkOrLinkIcon))
		    (SETQ LinkIcon LinkOrLinkIcon)
	   else (SETQ Link LinkOrLinkIcon)
		  (OR (SETQ GlobalLinkFlg (NC.GlobalLinkP Link))
			(SETQ LinkIcon (NC.FetchLinkIconForLink Link]
         (SETQ Card (fetch (Link SourceCard) of Link))
         (SETQ OldDisplayMode (fetch (Link DisplayMode) of Link))
         (if (NC.CheckForNotReadOnly Card Window "Can't change display mode for links in ")
	     then [COND
		      ((WINDOWP Window))
		      ((NC.ActiveCardP Card)
			(SETQ Window (NC.FetchWindow Card]

          (* * Get new display mode from user)


		    (SETQ DestinationCard (fetch (Link DestinationCard) of Link))
		    (NC.IfCardPartNotBusy
		      DestinationCard
		      (QUOTE FROMLINKS)
		      [if (NOT NewDisplayMode)
			  then (SETQ NewDisplayMode
				   (NC.DisplayModeFromStylesheetSelections
				     (STYLESHEET (CREATE.STYLE (QUOTE ITEMS)
								   [LIST
								     (create MENU
									       ITEMS ←
									       (QUOTE (Yes No Float)
											))
								     (create MENU
									       ITEMS ←
									       (QUOTE (Yes No Float)
											))
								     (create MENU
									       ITEMS ←
									       (QUOTE (Yes No Float]
								   (QUOTE ITEM.TITLES)
								   (QUOTE (Title?%  LinkType?%  
										    AttachBitmap?))
								   (QUOTE ITEM.TITLE.FONT)
								   (FONTCOPY MENUFONT (QUOTE
										 WEIGHT)
									       (QUOTE BOLD))
								   (QUOTE SELECTIONS)
								   (
							   NC.StylesheetSelectionsFromDisplayMode
								     OldDisplayMode)
								   (QUOTE NEED.NOT.FILL.IN)
								   NIL
								   (QUOTE TITLE)
								   (QUOTE Display% Mode?)
								   (QUOTE POSITION)
								   (create POSITION
									     XCOORD ← LASTMOUSEX
									     YCOORD ← LASTMOUSEY]
		      (if (OR (NULL NewDisplayMode)
				  (EQUAL NewDisplayMode OldDisplayMode))
			  then 

          (* * If new display mode is same as old, then bail out.)


				 NIL
			else 

          (* * Update link in LinkIcon or in global links list)


			       (if GlobalLinkFlg
				   then (for GlobalLink in (NC.FetchGlobalLinks Card)
					     when (NC.SameLinkP Link GlobalLink)
					     do (replace (Link DisplayMode) of GlobalLink
						     with NewDisplayMode))
					  (NC.SetLinksDirtyFlg Card T)
				 else (replace (Link DisplayMode) of (NC.FetchLinkFromLinkIcon
									     LinkIcon)
					   with NewDisplayMode)
					(NC.MarkCardDirty Card))

          (* * Update link in ToLinks List of Source card)


			       (for ToLink in (NC.FetchToLinks Card) when (NC.SameLinkP
										    Link ToLink)
				  do (replace (Link DisplayMode) of ToLink with 
										   NewDisplayMode))
			       (NC.SetLinksDirtyFlg Card T) 

          (* * Update Link in FromLinks list of Destination card)


			       (if (NC.LinksCachedP DestinationCard)
				   then (for FromLink in (NC.FetchFromLinks DestinationCard)
					     when (NC.SameLinkP Link FromLink)
					     do (replace (Link DisplayMode) of FromLink
						     with NewDisplayMode))
					  (NC.SetLinksDirtyFlg DestinationCard T)
				 else (WITH.MONITOR (NC.FetchMonitor (fetch (Card NoteFile)
									    of DestinationCard))
						      (NC.GetLinks DestinationCard)
						      (for FromLink in (NC.FetchFromLinks
									     DestinationCard)
							 when (NC.SameLinkP Link FromLink)
							 do (replace (Link DisplayMode)
								 of FromLink with NewDisplayMode))
						      (NC.PutFromLinks DestinationCard)
						      (NC.UncacheLinks DestinationCard)))

          (* * Update the image in any active window for Card)


			       (if [AND (WINDOWP Window)
					    (NOT (NC.TEditBasedP (NC.RetrieveType Card]
				   then (NC.UpdateLinkImages Window (fetch (Link 
										  DestinationCard)
									   of Link)))
                                                             (* This causes tedit to redisplay the screen.)
			       (QUOTE CHANGED])

(NC.UpdateLinkImages
  (LAMBDA (SourceWindowOrCard DestinationCard)               (* fgh: " 5-Feb-86 20:01")

          (* * Update the Link Image Objects in SourceCard that point to DestinationID)



          (* * rht 10/2/85: Now checks if card's window was shrunk. If so, unshrink, modify and reshrink.)



          (* * fgh 11/17/85 Updated to handle card objects.)



          (* * fgh 2/5/86 Addeed call to NC.ApplyFn)


    (LET (SourceWindow SourceCard NoteCardType ShrunkenWin)
         (COND
	   ((WINDOWP SourceWindowOrCard)
	     (SETQ SourceWindow SourceWindowOrCard)
	     (SETQ SourceCard (NC.CoerceToCard SourceWindowOrCard)))
	   (T (SETQ SourceWindow (NC.FetchWindow SourceWindowOrCard))
	      (SETQ SourceCard SourceWindowOrCard)))
         (COND
	   ((SETQ ShrunkenWin (NC.GetShrunkenWin SourceWindow))
	     (EXPANDW ShrunkenWin)))
         (SETQ NoteCardType (NC.RetrieveType SourceCard))
         (NC.ApplyFn UpdateLinkIconsFn SourceCard DestinationCard)
         (COND
	   (ShrunkenWin (SHRINKW SourceWindow)))
     NIL)))

(NC.LinkIconCopyFn
  (LAMBDA (ImageObject)                                      (* fgh: "29-Jan-86 15:58")

          (* * Return a copy of image object. IF it is a system link label, then make its label unspecified.)



          (* * rht 11/18/84: No longer worries about system link labels. All that is handled by WhenCopiedFn and WhenMovedFn.
	  Caches the new copy on the prop list of the original imageobj.)



          (* * rht 8/22/85: Added call to NC.CoerceToExternalPutLinkIcon to make the imageobj look like a external link icon.
	  If we're in a normal imageobj copy then the whencopiedfn will get called and replace the contents of 
	  ImageObjectCopy anyway. However, if only the CopyFn gets called, then this will stick.)



          (* * fgh 1/29/86 Removed the "copying ImageObj" clause in the create of ImageObjectCopy to fix bug where the 
	  copying was causing a stack overflow thru infinite recursive COPYALL calls. Copying clause was unnecessary.)


    (LET ((ImageObjectCopy (create IMAGEOBJ))
	  (Link (NC.FetchLinkFromLinkIcon ImageObject)))
         (NC.CoerceToExternalPutLinkIcon ImageObjectCopy (fetch (Link SourceCard) of Link)
					   (fetch (Link DestinationCard) of Link)
					   (fetch (Link Label) of Link))
         (IMAGEOBJPROP ImageObject (QUOTE ImageObjectCopy)
			 ImageObjectCopy)
     ImageObjectCopy)))

(NC.LinkAtCharPos
  [LAMBDA (CharPos TextStream)                               (* rht: " 1-Feb-87 15:45")

          (* * Returns the link right before CharPos in TextStream. Return NIL if there is no link icon located before 
	  CharPos.)



          (* * rht 1/25/87: Now takes special kludgy action if we're under a copy of multiple links. If it's past the first 
	  link in the copy then the value returned is the link just copied previously.)



          (* * rht 2/1/87: Fix to above kludgy action so that STKARG call is safe.)


    (if (TEXTSTREAMP TextStream)
	then (if [LET [(StkPos (STKPOS (QUOTE TEDIT.SELECTED.PIECES]
		          (AND StkPos (PROG1 (STKARG (QUOTE NPC)
							   StkPos)
						 (RELSTK StkPos]
		   then (NC.FetchUserDataProp (NC.CoerceToCard TextStream)
						  (QUOTE LastLinkCopiedOrMoved))
		 else (for LinkIconPair in (TEDIT.LIST.OF.OBJECTS (TEXTOBJ TextStream)
									  (FUNCTION 
									    NC.LinkIconImageObjP))
			   bind OldLinkIconPair while (LESSP (CADR LinkIconPair)
								   CharPos)
			   do (SETQ OldLinkIconPair LinkIconPair)
			   finally (RETURN (AND OldLinkIconPair (NC.FetchLinkFromLinkIcon
							(CAR OldLinkIconPair])

(NC.LinkIconLeftButtonFn
  [LAMBDA (LinkIcon Window)                                  (* rht: "18-Nov-86 18:47")

          (* * Left button has been pressed in the link icon. For now, just traverse the link.)


    (NC.TraverseLink LinkIcon Window])
)
(DEFINEQ

(NC.FillInLinkIcon
  [LAMBDA (ImageObj Label DestinationCard SourceCard DisplayMode LinkToInsertAfter CrossFileLinksMode)
                                                             (* rht: "25-Jan-87 20:01")

          (* * Called by WhenCopiedFn and WhenMovedFn to build the new link and stick it in the ImageObj.)



          (* * rht 1/25/87: Now returns the new link.)


    (DECLARE (GLOBALVARS NC.LinkIconImageFns))
    (LET ((Link (NC.MakeLink NIL Label DestinationCard SourceCard DisplayMode NIL NIL NIL 
			       LinkToInsertAfter CrossFileLinksMode)))
         (IMAGEOBJPROP ImageObj (QUOTE OBJECTDATUM)
			 Link)
         (replace (IMAGEOBJ IMAGEOBJFNS) of ImageObj with NC.LinkIconImageFns)
     Link])

(NC.InvisibleLinkImageBoxFn
  (LAMBDA (ImageObj Stream)                                  (* rht: "20-Nov-84 10:57")

          (* * Returns a zero-size image box.)


    (create IMAGEBOX
	      XSIZE ← 0
	      YSIZE ← 0
	      YDESC ← 0
	      XKERN ← 0)))

(NC.InvisibleLinkPutFn
  (LAMBDA (ImageObj OutputStream)                            (* fgh: "26-Feb-84 01:04")
    (BOUT OutputStream 1)))

(NC.InvisibleLinkGetFn
  (LAMBDA (InputStream TextStream)                           (* rht: "21-Nov-84 12:10")
    (BIN InputStream)
    NC.InvisibleLinkImageObject))
)
(* * New stuff for allowing put to work from TEdit.)

(DECLARE: DOEVAL@COMPILE DONTCOPY

(GLOBALVARS NC.ExternalPutLinkIconImageFns)
)

(RPAQ NC.ExternalPutLinkIconImageFns (IMAGEFNSCREATE (FUNCTION NC.ExternalPutLinkIconDisplayFn)
						       (FUNCTION NC.ExternalPutLinkIconImageBoxFn)
						       (FUNCTION NC.ExternalPutLinkIconPutFn)
						       (FUNCTION NC.ExternalPutLinkIconGetFn)
						       NIL
						       (FUNCTION NILL)))
(DEFINEQ

(NC.ExternalPutLinkIconDisplayFn
  (LAMBDA (ImageObj ImageStream STREAMTYPE TEXTSTREAM SCALE)
                                                             (* fgh: "30-May-85 23:09")

          (* * Display a link icon)


    (PROG ((Font (FONTCREATE (QUOTE HELVETICA)
				 10
				 (QUOTE BOLD))))
	    (DSPFONT (PROG1 (DSPFONT Font ImageStream)
				(PRIN1 (IMAGEOBJPROP ImageObj (QUOTE OBJECTDATUM))
					 ImageStream))
		       ImageStream))))

(NC.ExternalPutLinkIconImageBoxFn
  (LAMBDA (ImageObj ImageStream CurrentX RightMargin)        (* fgh: "30-May-85 16:52")
    (PROG ((FONT (FONTCREATE (QUOTE HELVETICA)
				 10
				 (QUOTE BOLD)))
	     (Scale (DSPSCALE NIL ImageStream)))
	    (RETURN (create IMAGEBOX
				XSIZE ← (TIMES Scale (STRINGWIDTH (IMAGEOBJPROP ImageObj
											(QUOTE
											  OBJECTDATUM)
											)
									FONT))
				YSIZE ← (TIMES Scale (FONTPROP FONT (QUOTE HEIGHT)))
				YDESC ← (COND
				  (RightMargin               (* This is in a TEdittextstream)
					       (FONTPROP FONT (QUOTE DESCENT)))
				  (T 0))
				XKERN ← 0)))))

(NC.ExternalPutLinkIconPutFn
  (LAMBDA (ImageObject Stream)                               (* kirk: "15-Nov-85 03:38")

          (* * Writes the Link information in the link icon specified by ImageObject onto the file specified by Stream)


    (LET (String Link)
         (COND
	   ((SETQ String (STRINGP (IMAGEOBJPROP ImageObject (QUOTE OBJECTDATUM))))
	     (PRIN2 String Stream))
	   (T (SETQ Link (NC.FetchLinkFromLinkIcon ImageObject))
	      (PRIN2 (CONCAT "***[[" (fetch (Link Label) of Link)
				 " link to "
				 (NC.RetrieveTitle (fetch (Link DestinationCard) of Link))
				 "]]***")
		       Stream))))))

(NC.ExternalPutLinkIconGetFn
  (LAMBDA (Stream)                                           (* fgh: "30-May-85 16:43")

          (* * Reads the link information from Stream and returns a link icon image object for this link info.)


    (IMAGEOBJCREATE (READ Stream)
		      NC.ExternalPutLinkIconImageFns)))

(NC.CoerceToExternalPutLinkIcon
  (LAMBDA (ImageObject SourceCard DestinationCard LinkLabel)
                                                             (* rht: "15-Nov-85 15:59")

          (* * Make image object into an external put link icon with the given data)



          (* * kirk 15Nov85: deleted use of LinkID)


    (IMAGEOBJPROP ImageObject (QUOTE OBJECTDATUM)
		    (CONCAT "[[" LinkLabel " link to " (NC.RetrieveTitle DestinationCard)
			      "]]"))
    (replace (IMAGEOBJ IMAGEOBJFNS) of ImageObject with NC.ExternalPutLinkIconImageFns)))
)
(* * Stuff to register our image object for intermezzo.)

(DEFINEQ

(NC.RegisterPlaceMarkerImageObj
  (LAMBDA NIL                                                (* rht: "31-May-85 11:40")

          (* * Cause the PlaceMarker Image Obj to be registered by creating the make place marker function once)


    (NC.MakePlaceMarker)))
)
(NC.RegisterPlaceMarkerImageObj)
(* * Stuff for handling link icons in sketches.)

(DEFINEQ

(NC.LinkIconSketchElementP
  (LAMBDA (SketchElement)                                    (* rht: "20-Aug-85 16:23")

          (* * Return non-nil if SketchElement is a link icon imageobj.)


    (NC.LinkIconImageObjP (SKETCH.IMAGEOBJ.OF.ELEMENT SketchElement))))

(NC.DeleteLinkIconSketchElement
  [LAMBDA (SketchElement CardID Don'tCreateDeletedImageObjFlg)
                                                             (* rht: " 4-Nov-86 21:01")

          (* * This replaces the link icon for SketchElement from SketchWin with deleted link icon.)



          (* * rht 4/30/86: Changed to simply delete the link icon if NC.UseDeletedLinkIconIndicators is NIL)



          (* * rht 11/4/86: Now takes Don'tCreateDeletedImageObjFlg arg.)


    (DECLARE (GLOBALVARS NC.DeletedLinkImageFns NC.UseDeletedLinkIconIndicatorsFlg 
			     NC.DeletedLinkImageObject))
    (LET ((Position (SKETCH.POSITION.OF.ELEMENT SketchElement))
	  (SketchSubstance (NC.FetchSubstance CardID))
	  (LinkIcon (SKETCH.IMAGEOBJ.OF.ELEMENT SketchElement))
	  (Window (NC.FetchWindow CardID))
	  Scale)
         (if (AND NC.UseDeletedLinkIconIndicatorsFlg (NOT Don'tCreateDeletedImageObjFlg))
	     then (SETQ Scale (if Window
				      then (VIEWER.SCALE Window)
				    else (NC.FetchScale CardID)))
		    (create IMAGEOBJ smashing LinkIcon OBJECTDATUM ← (IMAGEOBJPROP
						    NC.DeletedLinkImageObject
						    (QUOTE OBJECTDATUM))
						  IMAGEOBJFNS ← NC.DeletedLinkImageFns)
		    (AND Window (SKETCH.ELEMENT.CHANGED SketchSubstance SketchElement Window))
	   else (SKETCH.DELETE.ELEMENT SketchElement SketchSubstance T])
)
(* * Icons representing notecards types.)

(DECLARE: DOEVAL@COMPILE DONTCOPY

(GLOBALVARS NC.FileBoxIcon NC.GraphCardIcon NC.SketchCardIcon NC.TextCardIcon NC.TypelessIcon)
)
(* * DELETEME imageobjs)

(DECLARE: DOEVAL@COMPILE DONTCOPY

(GLOBALVARS NC.DELETEMEImageObjDatum NC.RemoveDELETEMEImageObjsFromCardFlg)
)

(RPAQ? NC.DELETEMEImageObjDatum "[[DELETE ME]]")

(RPAQ? NC.RemoveDELETEMEImageObjsFromCardFlg NIL)
(DEFINEQ

(NC.DELETEMEImageObjP
  [LAMBDA (ImageObj)                                         (* rht: " 7-Oct-86 16:26")

          (* * Returns non-nil if ImageObj is a DELETEME imageobj.)


    (DECLARE (GLOBALVARS NC.DELETEMEImageObjDatum))
    (AND (IMAGEOBJP ImageObj)
	   (EQUAL (IMAGEOBJPROP ImageObj (QUOTE OBJECTDATUM))
		    NC.DELETEMEImageObjDatum])
)
(PUTPROPS NCLINKS COPYRIGHT ("Xerox Corporation" 1984 1985 1986 1987))
(DECLARE: DONTCOPY
  (FILEMAP (NIL (8240 85647 (NC.GlobalLinkP 8250 . 8514) (NC.ChildLinkP 8516 . 8773) (NC.ContentsLinkP 
8775 . 8999) (NC.SubContentsLinkP 9001 . 9225) (NC.ReverseLinkLabelP 9227 . 9833) (NC.MakeGlobalLink 
9835 . 10606) (NC.AddFromLink 10608 . 11819) (NC.AddLinkToGraphCard 11821 . 12744) (
NC.AddLinkToSketchCard 12746 . 13559) (NC.InsertLinkInSketch 13561 . 15010) (NC.InsertLinkInText 15012
 . 16740) (NC.AddToLink 16742 . 18015) (NC.CachedAddToLink 18017 . 20197) (NC.DelFromLink 20199 . 
22026) (NC.CachedDelFromLink 22028 . 23339) (NC.DelToLink 23341 . 25052) (NC.DelReferencesToCard 25054
 . 28488) (NC.DelReferencesToCardFromGlobalList 28490 . 30365) (NC.DeleteLink 30367 . 33654) (
NC.DeleteToLink 33656 . 34526) (NC.DeleteFromLink 34528 . 35365) (NC.DeletedLinkDisplayFn 35367 . 
36845) (NC.DeletedLinkGetFn 36847 . 37177) (NC.DeletedLinkImageBoxFn 37179 . 37601) (
NC.DeletedLinkPutFn 37603 . 37751) (NC.DeletedLinkImageObjP 37753 . 38094) (NC.CheckForOrphanDelete 
38096 . 39682) (NC.FileInOrphanBox 39684 . 41698) (NC.HookToOrphanCard 41700 . 43556) (
NC.InsertLinkBeforeMarker 43558 . 48550) (NC.LinkLabelP 48552 . 48888) (NC.IDAlphOrder 48890 . 49515) 
(NC.MakeLink 49517 . 55808) (NC.CachedMakeFilingLink 55810 . 58447) (NC.MakeFilingLink 58449 . 60956) 
(NC.MakeFilingLinks 60958 . 64399) (NC.RelabelLink 64401 . 68843) (NC.SystemLinkLabelP 68845 . 69267) 
(NC.ValidLinkP 69269 . 70047) (NC.MakeChildLink 70049 . 70840) (NC.EnsureNoCycles 70842 . 71734) (
NC.SameLinkP 71736 . 72037) (NC.SameLinksP 72039 . 72322) (NC.AddGlobalLinkToCard 72324 . 72570) (
NC.AddGlobalLinksToCard 72572 . 74795) (NC.ChangeCardTitleFromLinkIcon 74797 . 77477) (NC.UncacheLinks
 77479 . 77714) (NC.TraverseLink 77716 . 78406) (NC.CreateLinkIconStrings 78408 . 85645)) (85648 89341
 (NC.AddLinkToCard 85658 . 87100) (NC.AddLinksToCard 87102 . 89339)) (89342 90779 (
NC.DelReferencesToCardFromShowLinks 89352 . 90777)) (90816 94932 (NC.CheckDisplayModeFormat 90826 . 
91916) (NC.InsureLinkDisplayMode 91918 . 92914) (NC.MakeNewDisplayMode 92916 . 93496) (
NC.DisplayModeFromStylesheetSelections 93498 . 94128) (NC.StylesheetSelectionsFromDisplayMode 94130 . 
94930)) (95202 95901 (NC.LinksSupportedP 95212 . 95899)) (96011 165166 (NC.FetchLinkIconForLink 96021
 . 96612) (NC.MakeLinkIcon 96614 . 96861) (NC.LinkIconButtonEventInFn 96863 . 101729) (
NC.LinkIconDisplayFn 101731 . 116939) (NC.LinkIconImageBoxFn 116941 . 128867) (NC.LinkIconImageObjP 
128869 . 129169) (NC.LinkIconPutFn 129171 . 130004) (NC.LinkIconGetFn 130006 . 130382) (
NC.LinkIconMiddleButtonFn 130384 . 132894) (NC.LinkIconWhenCopiedFn 132896 . 141468) (
NC.LinkIconWhenDeletedFn 141470 . 146266) (NC.LinkIconWhenInsertedFn 146268 . 147103) (
NC.LinkIconWhenMovedFn 147105 . 154691) (NC.FetchLinkFromLinkIcon 154693 . 155169) (
NC.ChangeLinkDisplayMode 155171 . 160982) (NC.UpdateLinkImages 160984 . 162122) (NC.LinkIconCopyFn 
162124 . 163559) (NC.LinkAtCharPos 163561 . 164896) (NC.LinkIconLeftButtonFn 164898 . 165164)) (165167
 166568 (NC.FillInLinkIcon 165177 . 165943) (NC.InvisibleLinkImageBoxFn 165945 . 166234) (
NC.InvisibleLinkPutFn 166236 . 166386) (NC.InvisibleLinkGetFn 166388 . 166566)) (167019 169865 (
NC.ExternalPutLinkIconDisplayFn 167029 . 167525) (NC.ExternalPutLinkIconImageBoxFn 167527 . 168243) (
NC.ExternalPutLinkIconPutFn 168245 . 168933) (NC.ExternalPutLinkIconGetFn 168935 . 169262) (
NC.CoerceToExternalPutLinkIcon 169264 . 169863)) (169929 170219 (NC.RegisterPlaceMarkerImageObj 169939
 . 170217)) (170308 172059 (NC.LinkIconSketchElementP 170318 . 170600) (NC.DeleteLinkIconSketchElement
 170602 . 172057)) (172493 172895 (NC.DELETEMEImageObjP 172503 . 172893)))))
STOP