(DEFINE-FILE-INFO PACKAGE "INTERLISP" READTABLE "XCL" BASE 10)
(filecreated "30-Dec-87 11:38:37" |{POGO:AISNORTH:XEROX}<CUTTING>TMAX>TMAX-ENDNOTE.;2| 22100  

      |previous| |date:| "11-Nov-87 11:49:07" |{POGO:AISNORTH:XEROX}<CUTTING>TMAX>TMAX-ENDNOTE.;1|)


; Copyright (c) 1987 by Xerox Corporation.  All rights reserved.

(prettycomprint tmax-endnotecoms)

(rpaqq tmax-endnotecoms ((* |Developed| |under| |support| |from| nih |grant| rr-00785.)
                         (* |Written| |by| |Frank| |Gilmurray| |and| |Sami| |Shaio.|)
                         (fns add.endnote insert.endnotes insert.endnotes.text delete.endnotes 
                              notesregionp set.endnote.style map.endnote.looks get.endnote.fonts)
                         (fns endnotep note.putfn note.getfn note.buttoneventinfn note.whenselectedfn
                              )
                         (vars endnote.notag.items endnote.tag.items)
                         (records endnotefonts)
                         (* * |Allow| |user| |to| |edit| |Endnote| |text| |in| |another| |TEdit| 
                            |window.|)
                         (fns aux.tedit aux.tedit.afterquitfn aux.tedit.titlemenufn)
                         (* * |Delimit| |text| |between| |two| |markers| |known| |as| region markers.
                            )
                         (fns regmarkobj regmarkobjp regmark.displayfn regmark.imageboxfn 
                              regmark.putfn regmark.getfn regmark.copyfn regmark.buttoneventinfn)
                         (records regmarkobj)))



(* |Developed| |under| |support| |from| nih |grant| rr-00785.)




(* |Written| |by| |Frank| |Gilmurray| |and| |Sami| |Shaio.|)

(defineq

(add.endnote
  (lambda (stream window)                                    (* |fsg| "13-Jul-87 10:44")
          (* * |Insert| |an| endnote |ImageObject| |as| \a |superscript.|
          |Displayed| |as| \a |number| |when| |updated.|)

    (let ((noteobj (numberobj 'note)))
         (tedit.insert.object noteobj stream)
         (|replace| (numberobj text.after#) |of| (|fetch| objectdatum |of| noteobj)
            |with| (or (tedit.getinput stream "Endnote text:")
                       ""))
         (tedit.promptprint stream "" t)
         (and (update? window)
              (update.numberobjs window stream 'endnotep)))))

(insert.endnotes
  (lambda (stream window)                                    (* |fsg| "25-Sep-87 10:23")
          (* * |Inserts| |text| |of| |endnotes| |at| |the| |end| |of| |the| |TEdit| 
          |document.| |The| |text| |is| |inserted| |between| |two| |Region| |marking| 
          |imageobjs.|)

    (let ((textobj (textobj stream))
          list.of.endnotes)
         (and (setq list.of.endnotes (tsp.list.of.objects textobj 'endnotep))
              (let ((caretposition (|fetch| ch# |of| (tedit.getsel stream))))
                   (tedit.promptprint stream (concat (cond
                                                        ((delete.endnotes stream)
                                                         "Rei")
                                                        (t "I"))
                                                    "nserting Endnotes...")
                          t)
                   (tedit.insert.object (regmarkobj 'endnotes '|Endnotes-START|)
                          stream
                          (add1 (|fetch| textlen |of| textobj)))
                   (tedit.looks stream '(protected on) (|fetch| textlen |of| textobj)
                          1)
                   (tedit.insert stream (concat (character (charcode eol))
                                               "Notes"
                                               (character (charcode eol)))
                          (add1 (|fetch| textlen |of| textobj))
                          (|fetch| (endnotefonts title.font) |of| (get.endnote.fonts window))
                          t)
                   (insert.endnotes.text stream window textobj list.of.endnotes)
                   (tedit.insert.object (regmarkobj 'endnotes '|Endnotes-END|)
                          stream
                          (add1 (|fetch| textlen |of| textobj)))
                   (tedit.looks stream '(protected on) (|fetch| textlen |of| textobj)
                          1)
                   (tedit.promptprint stream "done")
                   (tedit.normalizecaret textobj (tedit.setsel stream caretposition 1)))))))

(insert.endnotes.text
  (lambda (stream window textobj list.of.endnotes)           (* |fsg| "18-Jun-87 13:17")
          (* * |Here| |to| |print| |the| |text| |of| |each| |endnote.|)

    (let ((textlooks (|fetch| (endnotefonts text.font) |of| (get.endnote.fonts window)))
          (numblooks (|fetch| (endnotefonts number.font) |of| (get.endnote.fonts window))))
         (|for| endnoteobj |in| list.of.endnotes
            |do| (let ((numstring (mkstring (|fetch| (numberobj numstring) |of| (|fetch| objectdatum
                                                                                   |of| (car 
                                                                                           endnoteobj
                                                                                             )))))
                       (text (|fetch| (numberobj text.after#) |of| (|fetch| objectdatum
                                                                      |of| (car endnoteobj)))))
                      (tedit.insert stream numstring (add1 (|fetch| textlen |of| textobj))
                             numblooks t)
                      (tedit.insert stream (concat "  " text (character (charcode eol)))
                             (add1 (|fetch| textlen |of| textobj))
                             textlooks t))))))

(delete.endnotes
  (lambda (stream)                                           (* |fsg| "25-Sep-87 10:12")
          (* * |Delete| |the| |Endnotes,| |i.e.| |delete| |the| |start/end| regmark 
          |ImageObjects| |and| |all| |the| |text| |between| |them.|)

    (let* ((textobj (textobj stream))
           (notemarker.list (tsp.list.of.objects textobj 'notesregionp))
           (notes.start (cadar notemarker.list))
           (notes.end (cadadr notemarker.list)))
          (and notes.start notes.end (progn (tedit.promptprint stream "Deleting Endnotes..." t)
                                            (tedit.delete stream notes.start (idifference
                                                                              (add1 notes.end)
                                                                              notes.start))
                                            (tedit.promptprint stream "done")
                                            t)))))

(notesregionp
  (lambda (imobj)                                            (* |ss:| "27-Jun-87 15:29")
    (and (regmarkobjp imobj)
         (eq (|fetch| region.use |of| (|fetch| objectdatum |of| imobj))
             'endnotes))))

(set.endnote.style
  (lambda (stream window)                                    (* |fsg| "18-Aug-87 14:13")
          (* * |Set| |the| |font| |of| |the| endnote |number,| |title,| |or| |text.|)

    (let ((note.fonts (get.endnote.fonts window))
          (note.type (menu (|create| menu
                                  title ← "Endnote Fonts"
                                  centerflg ← t
                                  items ← '(|Number| |Title| |Text|))))
          old.font new.font)
         (and note.type (progn (setq old.font (selectq note.type
                                                  (|Number| (|fetch| (endnotefonts number.font)
                                                               |of| note.fonts))
                                                  (|Title| (|fetch| (endnotefonts title.font)
                                                              |of| note.fonts))
                                                  (|Text| (|fetch| (endnotefonts text.font)
                                                             |of| note.fonts))
                                                  nil))
                               (tedit.promptprint stream (concat "Change Endnote " note.type " font "
                                                                (abbreviate.font old.font)
                                                                " to...")
                                      t)
                               (setq new.font (fontcreate (get.tsp.font window old.font)))
                               (cond
                                  ((neq old.font new.font)
                                   (selectq note.type
                                       (|Number| (|replace| (endnotefonts number.font) |of| 
                                                                                           note.fonts
                                                    |with| new.font))
                                       (|Title| (|replace| (endnotefonts title.font) |of| note.fonts
                                                   |with| new.font))
                                       (|Text| (|replace| (endnotefonts text.font) |of| note.fonts
                                                  |with| new.font))
                                       nil)
                                   (and (eq note.type '|Number|)
                                        (map.endnote.looks stream new.font)))
                                  (t nil))
                               (tedit.promptprint stream "" t))))))

(map.endnote.looks
  (lambda (stream numberfont)                                (* |ss:| "27-Jun-87 15:26")
          (* * |Here| |to| |update| |the| endnote |looks.|
          |Only| |the| endnote |superscript| |numbers| |are| |updated.|)

    (let ((list.of.notes (tsp.list.of.objects (textobj stream)
                                'endnotep)))
         (and list.of.notes (progn (tedit.promptprint stream "Updating ENDNOTE Number looks..." t)
                                   (|for| note/ch# |in| list.of.notes
                                      |do| (tedit.looks stream numberfont (cadr note/ch#)
                                                  1))
                                   (tedit.promptprint stream "done"))))))

(get.endnote.fonts
  (lambda (window)                                           (* |ss:| "27-Jun-87 15:24")
          (* * |Setup| |the| |default| endnote |fonts| |for| |number,| |title,| |and| 
          |text.|)

    (or (windowprop window 'endnote.fonts)
        (progn (windowprop window 'endnote.fonts
                      (|create| endnotefonts
                             number.font ← |GP.DefaultFont|
                             title.font ← |GP.DefaultFont|
                             text.font ← |GP.DefaultFont|))
               (windowprop window 'endnote.fonts)))))
)
(defineq

(endnotep
  (lambda (imobj)                                            (* |ss:| "27-Jun-87 15:23")
          (* * |Like| numberobjp |but| |also| |checks| |for| note |ImageObject.|)

    (and (numberobjp imobj)
         (eq (|fetch| (numberobj use) |of| (|fetch| objectdatum |of| imobj))
             'note))))

(note.putfn
  (lambda (numberobj stream window)                          (* |fsg| "11-Aug-87 10:04")
          (* * |Used| |to| |put| \a |numberobj| |that| |is| |functioning| |as| |an| 
          |endnote.|)

    (|with| numberobj (|fetch| objectdatum |of| numberobj)
           (setq font (|for| notefont |in| (get.endnote.fonts window) |collect| (list.font.props
                                                                                 notefont))))
    (prin4 (list '|Endnote| nil (imageobjprop numberobj 'tag)
                 (|fetch| objectdatum |of| numberobj))
           stream)))

(note.getfn
  (lambda (newobj note.datum window)                         (* |fsg| "16-Jul-87 10:49")
          (* * |Used| |to| |get| \a |numberobj| |that| |is| |functioning| |as| |an| 
          |endnote.|)

    (windowprop window 'endnote.fonts (|for| notefont |in| (|fetch| (numberobj font) |of| note.datum)
                                         |collect| (fontcreate notefont)))
    (|replace| (numberobj font) |of| note.datum |with| nil)
    (|replace| objectdatum |of| newobj |with| note.datum)
    newobj))

(note.buttoneventinfn
  (lambda (obj stream window)                                (* |fsg| " 5-Aug-87 09:31")
          (* * |Allow| |user| |to| |edit| |Endnote| |text,| |specify| \a tag\, |delete| 
          |the| tag\, |or| |change| |the| tag.)

    (let* ((tag (imageobjprop obj 'tag))
           (nmenu (|create| menu
                         title ← '|Endnote Menu|
                         items ← (cond
                                    (tag endnote.tag.items)
                                    (t endnote.notag.items))
                         centerflg ← t
                         whenselectedfn ← 'note.whenselectedfn)))
          (putmenuprop nmenu 'note.obj obj)
          (putmenuprop nmenu 'note.window window)
          (menu nmenu))))

(note.whenselectedfn
  (lambda (item menu mb)                                     (* |fsg| "10-Aug-87 13:48")
    (let* ((window (getmenuprop menu 'note.window))
           (obj (getmenuprop menu 'note.obj))
           (tstream (textstream window)))
          (selectq (cadr item)
              ((|Change Tag| |Define Tag|) 
                   (let ((old.tag (imageobjprop obj 'tag))
                         (new.tag (tsp.get.incode tstream)))
                        (and new.tag (neq new.tag old.tag)
                             (progn (number.delete.tag window obj)
                                    (tsp.putcode new.tag obj window)
                                    (imageobjprop obj 'tag new.tag)))))
              (|Delete Tag| (number.delete.tag window obj))
              (|Show Tag| (tedit.promptprint tstream (concat "EndNote Tag=\"" (imageobjprop
                                                                               obj
                                                                               'tag)
                                                            "\"")
                                 t))
              (|Edit Text| (aux.tedit obj (concat "Endnote #" (|fetch| numstring
                                                                 |of| (|fetch| objectdatum
                                                                         |of| obj)))
                                  tstream))
              (error "Undefined EndNote menu item" item))
          nil)))
)

(rpaqq endnote.notag.items ((|Edit Text| |Edit Text| "Edit the text associated with this EndNote.")
                            (|Define Tag| |Define Tag| "Define a TAG for this EndNote.")))

(rpaqq endnote.tag.items ((|Edit Text| |Edit Text| "Edit the text associated with this EndNote.")
                          (|Change Tag| |Change Tag| "Change this EndNote's TAG.")
                          (|Delete Tag| |Delete Tag| "Delete this EndNote's TAG.")
                          (|Show Tag| |Show Tag| "Show this EndNote's TAG.")))
(declare\: eval@compile

(record endnotefonts (number.font title.font text.font))
)
(* * |Allow| |user| |to| |edit| |Endnote| |text| |in| |another| |TEdit| |window.|)

(defineq

(aux.tedit
  (lambda (imobj title stream)                               (* |ss:| "27-Jun-87 15:22")
          (* * |Open| \a |TEdit| |window| |where| |the| |user| |can| |view/edit| |the| 
          |text| |of| |the| |selected| |Endnote.|)

    (let* ((mainwindow (\\tedit.mainw stream))
           (auxwindow (createw (windowprop mainwindow 'auxw.region)
                             title)))
          (windowprop auxwindow 'main.window mainwindow)
          (windowprop auxwindow 'note.imageobj imobj)
          (tedit nil auxwindow nil '(afterquitfn aux.tedit.afterquitfn titlemenufn 
                                           aux.tedit.titlemenufn))
          (tedit.insert (textstream auxwindow)
                 (mkstring (|fetch| (numberobj text.after#) |of| (|fetch| objectdatum |of| imobj)))
                 nil
                 (|fetch| (endnotefonts text.font) |of| (get.endnote.fonts mainwindow))))))

(aux.tedit.afterquitfn
  (lambda (auxwindow)                                        (* |ss:| "27-Jun-87 15:22")
          (* * |Here| after |user| |finished| |with| |Endnote| |TEdit| |process.|)

    (let ((mainwindow (windowprop auxwindow 'main.window)))
         (windowprop mainwindow 'auxw.region (windowprop auxwindow 'region))
         (give.tty.process mainwindow)
         (tedit.normalizecaret (textobj mainwindow)))))

(aux.tedit.titlemenufn
  (lambda (auxwindow)                                        (* |ss:| "27-Jun-87 15:23")
          (* * |Here| |when| |left| |or| |middle| |button| |hit| |in| |title| |bar.|)

    (let ((item (menu (|create| menu
                             centerflg ← t
                             items ← '(|Save Changes| |Abort Changes|)))))
         (and item (progn (selectq item
                              (|Save Changes| 
                                   (|replace| (numberobj text.after#)
                                      |of| (|fetch| objectdatum |of| (windowprop auxwindow
                                                                            'note.imageobj))
                                      |with| (coercetextobj (textstream auxwindow)
                                                    'stringp)))
                              nil)
                          (tedit.quit (textstream auxwindow)))))))
)
(* * |Delimit| |text| |between| |two| |markers| |known| |as| region markers.)

(defineq

(regmarkobj
  (lambda (use marking)                                      (* |fsg| "10-Jul-87 15:58")
    (let ((newobj (imageobjcreate (|create| regmarkobj
                                         region.use ← use
                                         marking ← marking)
                         \\regmarkobj.imagefns)))
         (imageobjprop newobj 'type 'regmarkobj)
         newobj)))

(regmarkobjp
  (lambda (imobj)                                            (* |ss:| "27-Jun-87 15:31")
    (and imobj (eq (imageobjprop imobj 'type)
                   'regmarkobj))))

(regmark.displayfn
  (lambda (obj stream)                                       (* |fsg| "18-Feb-87 09:18")
          (* * regmark |is| |just| \a |marker,| |it| |doesn't| |actually| |display| 
          |anything.|)

    nil))

(regmark.imageboxfn
  (lambda (obj stream currentx rightmargin)                  (* |fsg| "17-Feb-87 10:22")
          (* * regmark |is| |just| \a |marker,| |it| |doesn't| |actually| |display| 
          |anything.|)

    (|create| imagebox
           xsize ← 0
           ysize ← 0
           ydesc ← 0
           xkern ← 0)))

(regmark.putfn
  (lambda (markobj stream)                                   (* |fsg| "23-Jul-87 14:02")
    (prin2 (list '|Region| (|fetch| region.use |of| (|fetch| objectdatum |of| markobj))
                 (|fetch| marking |of| (|fetch| objectdatum |of| markobj)))
           stream)))

(regmark.getfn
  (lambda (stream copy.object)                               (* |fsg| "20-Aug-87 14:58")
    (let ((window (|with| textobj textobj (car \\window))))
         (tsp.setup.fmmenu window))
    (apply (function regmarkobj)
           (or copy.object (cdr (read stream))))))

(regmark.copyfn
  (lambda (image.obj source.stream target.stream)            (* |fsg| "23-Jul-87 14:09")
          (* * |Here| |to| copy \a |RegMark| |Image| |Object.|)

    (selectq (imagestreamtype target.stream)
        (text (let ((textobj (textobj target.stream)))
                   (apply* (imageobjprop image.obj 'getfn)
                          target.stream
                          (|fetch| objectdatum |of| image.obj))))
        (error "Unknown TARGET stream type" (imagestreamtype target.stream)))))

(regmark.buttoneventinfn
  (lambda (markobj stream)                                   (* |fsg| "18-Feb-87 10:07")
          (* * |This| |function| |is| |never| |called| |because| |the| regmark 
          |ImageObjects| |are| |protected| |after| |they| |are| |inserted| |and| 
          |anything| |protected| |can't| |be| |selected.|)

    (and (mousestate middle)
         (let ((markdatum (|fetch| objectdatum |of| markobj)))
              (tedit.promptprint stream (concat "Region used for " (|fetch| region.use |of| markdatum
                                                                          )
                                               (cond
                                                  ((|fetch| marking |of| markdatum)
                                                   (concat ", Marker is " (|fetch| marking
                                                                             |of| markdatum)))
                                                  (t "")))
                     t)))))
)
(declare\: eval@compile

(record regmarkobj (region.use marking))
)
(putprops tmax-endnote copyright ("Xerox Corporation" 1987))
(declare\: dontcopy
  (filemap (nil (1726 11353 (add.endnote 1736 . 2404) (insert.endnotes 2406 . 4609) (
insert.endnotes.text 4611 . 6040) (delete.endnotes 6042 . 7033) (notesregionp 7035 . 7297) (
set.endnote.style 7299 . 9972) (map.endnote.looks 9974 . 10741) (get.endnote.fonts 10743 . 11351)) (
11354 15269 (endnotep 11364 . 11705) (note.putfn 11707 . 12359) (note.getfn 12361 . 12941) (
note.buttoneventinfn 12943 . 13723) (note.whenselectedfn 13725 . 15267)) (15991 18395 (aux.tedit 16001
 . 16963) (aux.tedit.afterquitfn 16965 . 17408) (aux.tedit.titlemenufn 17410 . 18393)) (18480 21944 (
regmarkobj 18490 . 18897) (regmarkobjp 18899 . 19093) (regmark.displayfn 19095 . 19341) (
regmark.imageboxfn 19343 . 19694) (regmark.putfn 19696 . 20028) (regmark.getfn 20030 . 20329) (
regmark.copyfn 20331 . 20869) (regmark.buttoneventinfn 20871 . 21942)))))
stop