(DEFINE-FILE-INFO PACKAGE "IL" READTABLE "INTERLISP" BASE 10)
(FILECREATED "12-Jan-88 18:34:54" {QV}<NOTECARDS>1.3LNEXT>RGTEDITPATCH.;4 32436  

      changes to%:  (VARS RGTEDITPATCHCOMS)
                    (PROPS (RGTEDITPATCH MAKEFILE-ENVIRONMENT))
                    (FNS \TEDIT.WAITFORSYSBUFP)

      previous date%: "12-Jan-88 18:27:48" {QV}<NOTECARDS>1.3LNEXT>RGTEDITPATCH.;2)


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

(PRETTYCOMPRINT RGTEDITPATCHCOMS)

(RPAQQ RGTEDITPATCHCOMS ((PROP MAKEFILE-ENVIRONMENT RGTEDITPATCH)
                         (FNS TEDIT \TEDIT.COMMAND.LOOP TEDIT.QUIT \TEDIT.WAITFORSYSBUFP)))

(PUTPROPS RGTEDITPATCH MAKEFILE-ENVIRONMENT (:PACKAGE "IL" :READTABLE "INTERLISP" :BASE 10))
(DEFINEQ

(TEDIT
  [LAMBDA (TEXT WINDOW DONTSPAWN PROPS)               (* ; "Edited 11-Jan-88 16:22 by Randy.Gobbel")
                                                             (* ; 
                    "User entry to the text editor.  Takes an optional window to be used for editing")
                                                             (* ; 
                                      "DONTSPAWN => Don't try to create a new process for this edit.")

    (PROG (PROC TEDITCREATEDWINDOW)                          (* ; 
                                                        "Inlcude the default properties in the list.")

          [COND
             ((AND TEXT (ATOM TEXT))                         (* ; 
                                        "Make sure the file exists before trying to open the window.")

              (SETQ TEXT (OPENFILE TEXT 'INPUT 'OLD]
          (RESETLST [RESETSAVE NIL `(AND ,WINDOW (WINDOWPROP ,WINDOW 'TEXTOBJ NIL]
                 (WITH.MONITOR TEDIT.STARTUP.MONITORLOCK (COND
                                                            ((NOT WINDOW)
                                                             (SETQ TEDITCREATEDWINDOW T)
                                                             (SETQ WINDOW
                                                              (COND
                                                                 [(OR (NOT TEDIT.DEFAULT.WINDOW)
                                                                      (\TEDIT.ACTIVE.WINDOWP 
                                                                             TEDIT.DEFAULT.WINDOW))
                                                                  (TEDIT.CREATEW
                                                                   (COND
                                                                      ((AND TEXT (ATOM TEXT))
                                                                       (CONCAT 
                                                              "Please specify an editing window for " 
                                                                              TEXT))
                                                                      (T 
                                                    "Please specify a region for the editing window."
                                                                         ))
                                                                   TEXT
                                                                   (APPEND PROPS (COPY 
                                                                                  TEDIT.DEFAULT.PROPS
                                                                                       ]
                                                                 (T (\TEDIT.CREATEW.FROM.REGION
                                                                     (WINDOWPROP TEDIT.DEFAULT.WINDOW
                                                                            'REGION)
                                                                     TEXT
                                                                     (APPEND PROPS (COPY 
                                                                                  TEDIT.DEFAULT.PROPS
                                                                                         )))
                                                             (* ; "Replace the old title")

                                                                    TEDIT.DEFAULT.WINDOW)))
                                                             (WINDOWPROP WINDOW 'TEXTOBJ T)
                                                             (* ; 
                 "Mark the newly-created window reserved until the OPENTEXTSTREAM has done its work.")

                                                             ))
                                                             (* ; 
                      "mark that we created the window so that we know we can update the title, etc.")

                        ))
          (SETQ TEXT (OPENTEXTSTREAM TEXT WINDOW NIL NIL PROPS))
          (TEXTPROP TEXT 'QUIT.LOCK (CREATE.MONITORLOCK "TEDIT.QUIT.LOCK"))
                                                             (* ; "Connect the editor to the window")

          [COND
             (TEDITCREATEDWINDOW (TEXTPROP TEXT 'TEDITCREATEDWINDOW 'T]
          (COND
             (DONTSPAWN                                      (* ; 
                                     "Either no processes running, or specifically not to spawn one.")

                    (RETURN (\TEDIT2 TEXT WINDOW T)))
             (T                                              (* ; "Spawn a process to do the edit.")

                [SETQ PROC (ADD.PROCESS (LIST '\TEDIT2 (KWOTE TEXT)
                                              WINDOW NIL)
                                  'NAME
                                  'TEdit
                                  'RESTARTABLE
                                  'HARDRESET
                                  'RESTARTFORM
                                  (LIST '\TEDIT.RESTARTFN (KWOTE TEXT)
                                        WINDOW
                                        (KWOTE PROPS]
                (PROCESSPROP PROC 'WINDOW WINDOW)
                (COND
                   ((NOT (LISTGET (APPEND PROPS (COPY TEDIT.DEFAULT.PROPS))
                                'LEAVETTY))                  (* ; 
                              "Unless he asked us to leave the tty where it is, TEdit should get it.")

                    (TTY.PROCESS PROC)))
                (RETURN PROC])

(\TEDIT.COMMAND.LOOP
  [LAMBDA (STREAM RTBL)                               (* ; "Edited 12-Jan-88 13:39 by Randy.Gobbel")
                                                             (* ; 
            "Main command loop for the TEDIT editor.  Includes keyboard polling and command dispatch")

    (PROG ((TEXTOBJ (COND
                       ((type? STREAM STREAM)
                        (fetch (TEXTSTREAM TEXTOBJ) of STREAM))
                       (T STREAM)))
           (ISCRSTRING (ALLOCSTRING \SCRATCHLEN " "))
           SEL WINDOW LINES IPASSSTRING TTYWINDOW)
          (SETQ SEL (fetch SEL of TEXTOBJ))
          (SETQ WINDOW (fetch \WINDOW of TEXTOBJ))
          (SETQ LINES (fetch LINES of TEXTOBJ))
          (SETQ IPASSSTRING (SUBSTRING ISCRSTRING 1))        (* ; "Used inside \INSERT\TTY\BUFFER")

          (SETQ RTBL (OR RTBL (fetch TXTRTBL of TEXTOBJ)
                         TEDIT.READTABLE))                   (* ; 
                                                     "Used to derive command characters from type-in")

          (for WW inside WINDOW do (WINDOWPROP WW 'PROCESS (THIS.PROCESS)))
                                                             (* ; "And the window to this process")

          (while (NOT (TTY.PROCESSP)) do                     (* ; 
                                               "Wait until we really have the TTY before proceeding.")

                                         (DISMISS 250))
          (RESETLST
           (RESETSAVE (\TEDIT.COMMAND.RESET.SETUP (LIST TEXTOBJ WINDOW)
                             T))
           (PROG (CH FN TCH (DIRTY NIL)
                     (BLANKSEEN NIL)
                     INSCH#
                     (CRSEEN NIL)
                     TLEN CHNO (READSA (fetch READSA of %#CURRENTRDTBL#))
                     (TERMSA (OR (fetch TXTTERMSA of TEXTOBJ)
                                 \PRIMTERMSA))
                     (QUIT.LOCK (TEXTPROP STREAM 'QUIT.LOCK))
                     (TEDITSA (fetch READSA of RTBL))
                     (TEDITFNHASH (fetch READMACRODEFS of RTBL))
                     (LOOPFN (TEXTPROP TEXTOBJ 'LOOPFN))
                     (CHARFN (TEXTPROP TEXTOBJ 'CHARFN))
                     COMMANDFN)
                 (while (NOT (fetch EDITFINISHEDFLG of TEXTOBJ))
                    do
                    (BLOCK)
                    [WITH.MONITOR
                     QUIT.LOCK
                     (if (NOT (fetch EDITFINISHEDFLG of TEXTOBJ))
                         then
                         (ERSETQ (PROGN (\TEDIT.WAITFORSYSBUFP 25)
                                                             (* ; "Await type-in or mouse action")

                                        (if (TTY.PROCESSP)
                                            then (while (OR TEDIT.SELPENDING (fetch EDITOPACTIVE
                                                                                of TEXTOBJ))
                                                    do       (* ; 
                       "Don't do anything while he's selecting or one of the lock-out ops is active.")

                                                       [COND
                                                          ((EQ TEDIT.SELPENDING TEXTOBJ)
                                                             (* ; 
                        "(OR (EQ TEDIT.SELPENDING TEXTOBJ) (fetch TCUP of (fetch CARET of TEXTOBJ)))")
                                                             (* ; 
           "If this TEdit is the one being selected in, or the caret is explicitly visible, flash it")

                                                           (TEDIT.FLASHCARET (fetch CARET
                                                                                of TEXTOBJ]
                                                       (BLOCK))
                                                 (COND
                                                    ((fetch TXTNEEDSUPDATE of TEXTOBJ)
                                                             (* ; 
                     "We got here somehow with the window not in sync with the text.  Run an update.")

                                                     (\SHOWSEL SEL NIL NIL)
                                                     (TEDIT.UPDATE.SCREEN TEXTOBJ NIL T)
                                                     (\FIXSEL SEL TEXTOBJ)
                                                     (\SHOWSEL SEL NIL T)))
                                                 (TEDIT.FLASHCARET (fetch CARET of TEXTOBJ)) 
                                                             (* ; 
                  "Flash the caret periodically (BUT not while we're here only to cleanup and quit.)")

                                                 (replace EDITOPACTIVE of TEXTOBJ with T) 
                                                             (* ; 
                                          "Before starting to work, note that we're doing something.")

                                                 (AND LOOPFN (ERSETQ (APPLY* LOOPFN STREAM))) 
                                                             (* ; 
                                          "If the guy wants control during the loop, give it to him.")
                                                             (* ; "Process any pending selections")

                                                 [COND
                                                    (TEDIT.COPY.PENDING 
                                                             (* ; 
                                                             "Have to copy the shifted SEL to caret.")

                                                           (SETQ TEDIT.COPY.PENDING NIL)
                                                           (\COPYSEL TEDIT.SHIFTEDSELECTION
                                                                  (fetch SHIFTEDSEL of TEXTOBJ))
                                                           (ERSETQ (TEDIT.COPY (fetch SHIFTEDSEL
                                                                                  of TEXTOBJ)
                                                                          (fetch SEL of TEXTOBJ)))
                                                           (replace SET of TEDIT.SHIFTEDSELECTION
                                                              with NIL)
                                                           (replace L1 of TEDIT.SHIFTEDSELECTION
                                                              with NIL)
                                                           (replace LN of TEDIT.SHIFTEDSELECTION
                                                              with NIL)
                                                           (\COPYSEL TEDIT.SHIFTEDSELECTION
                                                                  (fetch SHIFTEDSEL of TEXTOBJ)))
                                                    (TEDIT.COPYLOOKS.PENDING
                                                             (* ; 
                                                             "Have to copy the shifted SEL to caret.")

                                                     (SETQ TEDIT.COPYLOOKS.PENDING NIL)
                                                     (\COPYSEL TEDIT.COPYLOOKSSELECTION
                                                            (fetch SHIFTEDSEL of TEXTOBJ))
                                                     [ERSETQ (COND
                                                                ((EQ 'PARA
                                                                     (fetch SELKIND
                                                                        of (fetch SHIFTEDSEL
                                                                              of TEXTOBJ)))
                                                             (* ; 
                            "copy the paragraph looks, since the source selection type was paragraph")

                                                                 (TEDIT.COPY.PARALOOKS TEXTOBJ
                                                                        (fetch SHIFTEDSEL
                                                                           of TEXTOBJ)
                                                                        (fetch SEL of TEXTOBJ)))
                                                                (T 
                                                             (* ; "copy the character looks")

                                                                   (TEDIT.COPY.LOOKS TEXTOBJ
                                                                          (fetch SHIFTEDSEL
                                                                             of TEXTOBJ)
                                                                          (fetch SEL of TEXTOBJ]
                                                     (\SHOWSEL (fetch SHIFTEDSEL of TEXTOBJ)
                                                            NIL NIL)
                                                     (replace SET of TEDIT.COPYLOOKSSELECTION
                                                        with NIL)
                                                     (replace L1 of TEDIT.COPYLOOKSSELECTION
                                                        with NIL)
                                                     (replace LN of TEDIT.COPYLOOKSSELECTION
                                                        with NIL)
                                                     (\COPYSEL TEDIT.COPYLOOKSSELECTION
                                                            (fetch SHIFTEDSEL of TEXTOBJ)))
                                                    (TEDIT.MOVE.PENDING 
                                                             (* ; 
                                                          "Have to move the ctrl-shift SEL to caret.")

                                                           (SETQ TEDIT.MOVE.PENDING NIL)
                                                           (\COPYSEL TEDIT.MOVESELECTION
                                                                  (fetch MOVESEL of TEXTOBJ))
                                                           (TEDIT.DO.BLUEPENDINGDELETE SEL TEXTOBJ)
                                                           (ERSETQ (TEDIT.MOVE (fetch MOVESEL
                                                                                  of TEXTOBJ)
                                                                          (fetch SEL of TEXTOBJ)))
                                                           (replace SET of TEDIT.MOVESELECTION
                                                              with NIL)
                                                           (replace L1 of TEDIT.MOVESELECTION
                                                              with NIL)
                                                           (replace LN of TEDIT.MOVESELECTION
                                                              with NIL)
                                                           (\COPYSEL TEDIT.MOVESELECTION
                                                                  (fetch MOVESEL of TEXTOBJ)))
                                                    (TEDIT.DEL.PENDING
                                                             (* ; "Delete the current selection.")

                                                     (SETQ TEDIT.DEL.PENDING NIL)
                                                             (* ; 
                                                             "Above all, reset the demand flag first")

                                                     (ERSETQ (COND
                                                                ((fetch SET of TEDIT.DELETESELECTION)
                                                             (* ; 
                                              "Only try the deletion if he really set the selection.")

                                                                 (\SHOWSEL (fetch DELETESEL
                                                                              of TEXTOBJ)
                                                                        NIL NIL)
                                                             (* ; "Turn off the selection highlights")

                                                                 (\SHOWSEL (fetch SEL of TEXTOBJ)
                                                                        NIL NIL)
                                                                 (replace SET
                                                                    of (fetch DELETESEL of TEXTOBJ)
                                                                    with NIL)
                                                                 (\COPYSEL TEDIT.DELETESELECTION
                                                                        (fetch SEL of TEXTOBJ))
                                                                 (\TEDIT.SET.SEL.LOOKS (fetch SEL
                                                                                          of TEXTOBJ)
                                                                        'NORMAL)
                                                             (* ; "Grab the selection we're to use")

                                                                 (\TEDIT.DELETE (fetch SEL
                                                                                   of TEXTOBJ)
                                                                        (fetch \TEXTOBJ
                                                                           of (fetch SEL of TEXTOBJ))
                                                                        NIL)
                                                                 (replace L1 of TEDIT.DELETESELECTION
                                                                    with NIL)
                                                                 (replace LN of TEDIT.DELETESELECTION
                                                                    with NIL]
                                                 (UNINTERRUPTABLY
                                                     (replace (STRINGP OFFST) of ISCRSTRING
                                                        with 0)
                                                     (replace (STRINGP LENGTH) of ISCRSTRING
                                                        with \SCRATCHLEN)))
                                        [while (\SYSBUFP)
                                           do                (* ; "Handle user type-in")

                                              (SETQ CH (\GETKEY))
                                              (COND
                                                 (CHARFN     (* ; 
                                                "Give the OEM user control for each character typed.")

                                                        (SETQ TCH (APPLY* CHARFN STREAM CH))
                                                        (OR (EQ TCH T)
                                                            (SETQ CH TCH))
                                                             (* ; 
      "And let him return one of NIL for 'ignore this char' , T for 'leave it be' or a new charcode.")

                                                        ))
                                              (SELECTC (AND CH (\SYNCODE TEDITSA CH))
                                                  (CHARDELETE.TTC 
                                                             (* ; 
                                       "Backspace handler: Remove the character just before SEL:CH#.")

                                                       (\TEDIT.CHARDELETE TEXTOBJ ISCRSTRING SEL)
                                                       (TEDIT.RESET.EXTEND.PENDING.DELETE SEL))
                                                  (WORDDELETE.TTC 
                                                       (\TEDIT.WORDDELETE TEXTOBJ)
                                                       (TEDIT.RESET.EXTEND.PENDING.DELETE SEL))
                                                  (DELETE.TTC 
                                                             (* ; 
                                                    "DEL Key handler: Delete the selected characters")

                                                              (\TEDIT.DELETE SEL TEXTOBJ)
                                                              (TEDIT.RESET.EXTEND.PENDING.DELETE
                                                               SEL))
                                                  (UNDO.TTC  (* ; 
                                                        "He hit the CANCEL key, so go UNDO something")

                                                            (TEDIT.UNDO TEXTOBJ)
                                                            (TEDIT.RESET.EXTEND.PENDING.DELETE SEL))
                                                  (REDO.TTC  (* ; 
                                                          "He hit the REDO key, so go REDO something")

                                                            (TEDIT.REDO TEXTOBJ)
                                                            (TEDIT.RESET.EXTEND.PENDING.DELETE SEL))
                                                  (FUNCTIONCALL.TTC 
                                                             (* ; 
                                                 "This is a special character -- it calls a function")

                                                       (COND
                                                          ((SETQ FN (GETHASH CH TEDITFNHASH))
                                                             (* ; 
                                                          "There IS a command function to be called.")

                                                           (APPLY* FN (fetch STREAMHINT of TEXTOBJ)
                                                                  TEXTOBJ SEL)
                                                             (* ; "do it")

                                                           (\SHOWSEL SEL NIL NIL)
                                                           (TEDIT.RESET.EXTEND.PENDING.DELETE SEL)
                                                             (* ; 
                                                 "After a user function, no more blue-pending-delete")

                                                           (\SHOWSEL SEL NIL T)
                                                             (* ; "And forget any pending deletion.")

                                                           )))
                                                  (NEXT.TTC  (* ; 
                       "Move to the next blank to fill in.  For now, blanks are delimited by >>...<<")

                                                            (TEDIT.NEXT TEXTOBJ))
                                                  (EXPAND.TTC 
                                                             (* ; "EXPAND AN ABBREVIATION")

                                                              (\TEDIT.ABBREV.EXPAND (fetch STREAMHINT
                                                                                       of TEXTOBJ)))
                                                  (SELECTC (AND TERMSA CH (fetch TERMCLASS
                                                                             of (\SYNCODE TERMSA CH))
                                                                )
                                                      (CHARDELETE.TC 
                                                             (* ; 
                                       "Backspace handler: Remove the character just before SEL:CH#.")

                                                                     (\TEDIT.CHARDELETE TEXTOBJ 
                                                                            ISCRSTRING SEL)
                                                                     (
                                                                    TEDIT.RESET.EXTEND.PENDING.DELETE
                                                                      SEL))
                                                      (WORDDELETE.TC 
                                                             (* ; "Back-WORD handler")

                                                                     (\TEDIT.WORDDELETE TEXTOBJ)
                                                                     (
                                                                    TEDIT.RESET.EXTEND.PENDING.DELETE
                                                                      SEL))
                                                      (LINEDELETE.TC 
                                                             (* ; 
                                                    "DEL Key handler: Delete the selected characters")

                                                                     (\TEDIT.DELETE SEL TEXTOBJ)
                                                                     (
                                                                    TEDIT.RESET.EXTEND.PENDING.DELETE
                                                                      SEL))
                                                      (COND
                                                         (CH (* ; 
                                                  "Any other key was hit: Just insert the character.")

                                                             (TEDIT.DO.BLUEPENDINGDELETE SEL TEXTOBJ)
                                                             (* ; 
                                                       "Handle blue pending delete, if there is one.")

                                                             (TEDIT.\INSERT CH SEL TEXTOBJ BLANKSEEN 
                                                                    CRSEEN]
                                        (replace EDITOPACTIVE of TEXTOBJ with NIL]
                    (replace EDITOPACTIVE of TEXTOBJ with NIL])

(TEDIT.QUIT
  [LAMBDA (STREAM VALUE)                              (* ; "Edited 11-Jan-88 16:22 by Randy.Gobbel")
          
          (* Force the edit session supported by STREAM to terminate, and to return VALUE)

    (COND
       ((type? STREAM STREAM)                                (* If he gave us a textofd, get the 
                                                             textobj)
        (SETQ STREAM (fetch (TEXTSTREAM TEXTOBJ) of STREAM)))
       ((type? TEXTOBJ STREAM)                               (* A Textobj is just fine)
        )
       (T                                                    (* Anything else is ungood, 
                                                             double-plus)
          (\ILLEGAL.ARG STREAM)))
    (WITH.MONITOR (TEXTPROP STREAM 'QUIT.LOCK)
           (replace EDITFINISHEDFLG of STREAM with (OR VALUE T)))
                                                             (* tell the command loop to stop next 
                                                             time through)
    (PROG (MAINW)
          (COND
             ([AND (fetch \WINDOW of STREAM)
                   (NEQ (SETQ MAINW (\TEDIT.PRIMARYW STREAM))
                        (PROCESSPROP (TTY.PROCESS)
                               'WINDOW]
          
          (* there is a main window of the stream, and it is not the window of the tty 
          process, so give it the tty)

              (TTY.PROCESS (WINDOWPROP MAINW 'PROCESS))
              (AND (NEQ (TTY.PROCESS)
                        (THIS.PROCESS))
                   (until [OR (NOT (WINDOWPROP MAINW 'PROCESS))
                              (PROCESS.FINISHEDP (WINDOWPROP MAINW 'PROCESS] do 
          
          (* Wait until the Edit process has had a chance to go away before continuing 
          here.)

                                                                                (DISMISS])

(\TEDIT.WAITFORSYSBUFP
  [LAMBDA (N)                                         (* ; "Edited 12-Jan-88 13:45 by Randy.Gobbel")

    (COND
       [(FIXP N)
        (GLOBALRESOURCE (\DISMISSTIMER)
               (PROG ((NOW (\CLOCK0 \DISMISSTIMER)))
                     (COND
                        ((NOT (TTY.PROCESSP))
                         (AWAIT.EVENT \TTY.PROCESS.EVENT N)))
                 LP                                          (* ; "")

                     (COND
                        ((AND (\SYSBUFP)
                              (TTY.PROCESSP))
                         (RETURN T))
                        ((\CLOCKGREATERP NOW N)              (* ; "Time's up, return with no input")

                         (RETURN NIL))
                        (T (BLOCK)))
                     (GO LP]
       (T (until (\SYSBUFP) do (BLOCK)
                               (AWAIT.EVENT \TTY.PROCESS.EVENT])
)
(PUTPROPS RGTEDITPATCH COPYRIGHT ("Xerox Corporation" 1988))
(DECLARE%: DONTCOPY
  (FILEMAP (NIL (764 32352 (TEDIT 774 . 6433) (\TEDIT.COMMAND.LOOP 6435 . 29477) (TEDIT.QUIT 29479 . 
31424) (\TEDIT.WAITFORSYSBUFP 31426 . 32350)))))
STOP