(DEFINE-FILE-INFO READTABLE "INTERLISP" PACKAGE "INTERLISP" BASE 10)
(FILECREATED "27-Sep-87 18:43:27" {DSK}<OST>ANSICHATCOPY.;24 52005  

      changes to%:  (FNS ANSI-Chat-Expand-Selection)
                    (FILES CHATDECLS)

      previous date%: "26-Sep-87 15:20:27" {DSK}<OST>ANSICHATCOPY.;23)


(PRETTYCOMPRINT ANSICHATCOPYCOMS)

(RPAQQ ANSICHATCOPYCOMS ((PROP MAKEFILE-ENVIRONMENT ANSICHATCOPY)
                         (PROP FILETYPE ANSICHATCOPY)
                         (FNS ANSI-Chat-Copy-Complete ANSI-Chat-Copy-Extend ANSI-Chat-Copy-Get-String 
                              ANSI-Chat-Copy-Invert-Highlight ANSI-Chat-Copy-Invert-Highlight-Lines 
                              ANSI-Chat-Copy-Remove-Highlight ANSI-Chat-Copy-Select 
                              ANSI-Chat-Copy-Top ANSI-Chat-CopyButtonEventFn 
                              ANSI-Chat-Expand-Selection ANSI-Chat-Growable-Character 
                              ANSI-Chat-Update-Extended-Selection ANSI-Chat-Update-Selection)
                         (DECLARE%: EVAL@COMPILE DONTCOPY (FILES (SOURCE)
                                                                 CHATDECLS ANSICHATDECLS))))

(PUTPROPS ANSICHATCOPY MAKEFILE-ENVIRONMENT (:READTABLE "INTERLISP" :PACKAGE "INTERLISP" :BASE 10))

(PUTPROPS ANSICHATCOPY FILETYPE TCOMPL)
(DEFINEQ

(ANSI-Chat-Copy-Complete
  [LAMBDA (Chat.State ANSI-State)                         (* ; "Edited 22-Sep-87 14:25 by R.Beeman")

    (with CHAT.STATE Chat.State                              (* ; "Chat State")

          (with ANSI-STATE ANSI-State                        (* ; "ANSI State")

                (CURSOR T)
                (SETQ LineCursor NIL)                        (* ; "LineCursor")

                (ANSI-Chat-Copy-Remove-Highlight Chat.State ANSI-State)
                (COND
                   ((NOT (ZEROP SelectionLine))              (* ; "Only if there is a selection")

                    (BKSYSBUF (COND
                                 [(ZEROP SelectionStartColumn)
                                                             (* ; "Line Selection")

                                  (COND
                                     ((ZEROP ExtendedSelectionLine)
                                                             (* ; "No Extension")

                                      (ANSI-Chat-Copy-Get-String Chat.State ANSI-State SelectionLine 
                                             1 0))
                                     (T                      (* ; "There is an Extension")

                                        (CONCATLIST (for Line from (IMIN SelectionLine 
                                                                         ExtendedSelectionLine)
                                                       to (IMAX SelectionLine ExtendedSelectionLine)
                                                       collect (COND
                                                                  ((ANSI-Chat-Copy-Top Chat.State 
                                                                          ANSI-State Line)
                                                                   "")
                                                                  (T (ANSI-Chat-Copy-Get-String
                                                                      Chat.State ANSI-State Line 1 0]
                                 ((ZEROP ExtendedSelectionLine)
                                                             (* ; "No Extension")

                                  (ANSI-Chat-Copy-Get-String Chat.State ANSI-State SelectionLine 
                                         SelectionStartColumn SelectionEndColumn))
                                 ((= SelectionLine ExtendedSelectionLine)
                                                             (* ; "All on Same Line")

                                  (ANSI-Chat-Copy-Get-String Chat.State ANSI-State SelectionLine
                                         (IMIN SelectionStartColumn ExtendedSelectionStartColumn)
                                         (IMAX SelectionEndColumn ExtendedSelectionEndColumn)))
                                 ((< SelectionLine ExtendedSelectionLine)
                                                             (* ; "Selection before Extension")

                                  (CONCAT (ANSI-Chat-Copy-Get-String Chat.State ANSI-State 
                                                 SelectionLine SelectionStartColumn 0)
                                         (COND
                                            [(< SelectionLine (SUB1 ExtendedSelectionLine))
                                             (CONCATLIST (for Line from (ADD1 SelectionLine)
                                                            to (SUB1 ExtendedSelectionLine)
                                                            collect (COND
                                                                       ((ANSI-Chat-Copy-Top 
                                                                               Chat.State ANSI-State 
                                                                               Line)
                                                                        "")
                                                                       (T (ANSI-Chat-Copy-Get-String
                                                                           Chat.State ANSI-State Line 
                                                                           1 0]
                                            (T ""))
                                         (ANSI-Chat-Copy-Get-String Chat.State ANSI-State 
                                                ExtendedSelectionLine 1 ExtendedSelectionEndColumn)))
                                 (T (CONCAT (ANSI-Chat-Copy-Get-String Chat.State ANSI-State 
                                                   ExtendedSelectionLine ExtendedSelectionStartColumn 
                                                   0)
                                           (COND
                                              [(< ExtendedSelectionLine (SUB1 SelectionLine))
                                               (CONCATLIST (for Line from (ADD1 ExtendedSelectionLine
                                                                                )
                                                              to (SUB1 SelectionLine)
                                                              collect (COND
                                                                         ((ANSI-Chat-Copy-Top 
                                                                                 Chat.State 
                                                                                 ANSI-State Line)
                                                                          "")
                                                                         (T (
                                                                            ANSI-Chat-Copy-Get-String
                                                                             Chat.State ANSI-State 
                                                                             Line 1 0]
                                              (T ""))
                                           (ANSI-Chat-Copy-Get-String Chat.State ANSI-State 
                                                  SelectionLine 1 SelectionEndColumn])

(ANSI-Chat-Copy-Extend
  [LAMBDA (Chat.State ANSI-State Line Column)             (* ; "Edited 21-Sep-87 12:00 by R.Beeman")

    (with CHAT.STATE Chat.State                              (* ; "Chat State")

          (with ANSI-STATE ANSI-State                        (* ; "ANSI State")

                (COND
                   ((NOT (ZEROP SelectionLine))              (* ; 
                                                             "Can't Extend without Initial Selection")

                    [COND
                       ((ZEROP SelectionStartColumn)
                        (SETQ Column 0))
                       (T (COND
                             ((ZEROP Column)
                              (SETQ Column 1]
                    (COND
                       ([COND
                           [(ZEROP ExtendedSelectionLine)    (* ; 
                                                             "Unless line selection on this line")

                            (NOT (AND (= Line SelectionLine)
                                      (ZEROP SelectionStartColumn]
                           (T                                (* ; "Extend")

                              (NOT (AND (= Line ExtendedSelectionLine)
                                        (<= ExtendedSelectionStartColumn Column 
                                         ExtendedSelectionEndColumn]
          
          (* ;; "Something Changed Recalculate")

                        (ANSI-Chat-Update-Extended-Selection Chat.State ANSI-State Line
                               (ANSI-Chat-Expand-Selection Chat.State ANSI-State Line Column
                                      'Left)
                               (ANSI-Chat-Expand-Selection Chat.State ANSI-State Line Column
                                      'Right))
                        (COND
                           ((AND (ZEROP SelectionStartColumn)
                                 (= SelectionLine ExtendedSelectionLine))
                            (SETQ ExtendedSelectionLine 0])

(ANSI-Chat-Copy-Get-String
  [LAMBDA (Chat.State ANSI-State Line Start End)          (* ; "Edited 22-Sep-87 11:29 by R.Beeman")

    (LET* ((CharacterLine (ELT (fetch (ANSI-STATE Characters) of ANSI-State)
                               Line))
           (LineWidth (ELT CharacterLine 0)))
          [COND
             ((ZEROP End)
              (SETQ End (ADD1 LineWidth]
          (CONCAT (COND
                     [(< Start End)
                      (CONCATLIST (for Index from Start to (SUB1 End)
                                     collect (CHARACTER (ELT CharacterLine Index]
                     (T ""))
                 (MKSTRING (CHARACTER (COND
                                         ((> End LineWidth)
                                          (CHARCODE CR))
                                         (T (ELT CharacterLine End])

(ANSI-Chat-Copy-Invert-Highlight
  [LAMBDA (Chat.State ANSI-State Line Start End NoExtend)
    (DECLARE (GLOBALVARS ANSI-Chat-Dashing))              (* ; "Edited 22-Sep-87 14:18 by R.Beeman")

    (with CHAT.STATE Chat.State                              (* ; "Chat State")

          (with ANSI-STATE ANSI-State                        (* ; "ANSI State")

                (LET* ((LineWidth (ELT (ELT Characters Line)
                                       0))
                       [CharacterWidth (COND
                                          ((= (ELT LineAttributes Line)
                                              5)
                                           FONTWIDTH)
                                          (T (RSH FONTWIDTH -1]
                       (Left (+ (ITIMES (SUB1 Start)
                                       CharacterWidth)
                                FONTWIDTH)))
                      [COND
                         ((AND (NOT NoExtend)
                               (ZEROP End))
                          (SETQ End (ADD1 LineWidth]
                      (BITBLT ANSI-Chat-Dashing (IMOD Left 3)
                             0 DSP Left (ITIMES (- BottomLine Line)
                                               FONTHEIGHT)
                             (+ (ITIMES (- End Start)
                                       CharacterWidth)
                                (COND
                                   ((> End LineWidth)
                                    FONTWIDTH)
                                   (T CharacterWidth)))
                             1
                             'SOURCE
                             'INVERT])

(ANSI-Chat-Copy-Invert-Highlight-Lines
  [LAMBDA (Chat.State ANSI-State Start End)               (* ; "Edited 21-Sep-87 16:22 by R.Beeman")

    (for Line from Start to End unless (ANSI-Chat-Copy-Top Chat.State ANSI-State Line)
       do (ANSI-Chat-Copy-Invert-Highlight Chat.State ANSI-State Line 1 0])

(ANSI-Chat-Copy-Remove-Highlight
  [LAMBDA (Chat.State ANSI-State)                         (* ; "Edited 22-Sep-87 11:32 by R.Beeman")

    (with CHAT.STATE Chat.State                              (* ; "Chat State")

          (with ANSI-STATE ANSI-State                        (* ; "ANSI State")

                (COND
                   ((ZEROP SelectionLine)                    (* ; "No Selection -- do nothing")

                    NIL)
                   [(ZEROP SelectionStartColumn)             (* ; "Line Selection")

                    (COND
                       ((ZEROP ExtendedSelectionLine)        (* ; "No Extension")

                        (ANSI-Chat-Copy-Invert-Highlight Chat.State ANSI-State SelectionLine 1 0))
                       (T                                    (* ; "There is an Extension")

                          (for Line from (IMIN SelectionLine ExtendedSelectionLine)
                             to (IMAX SelectionLine ExtendedSelectionLine)
                             do (COND
                                   ((NOT (ANSI-Chat-Copy-Top Chat.State ANSI-State Line))
                                    (ANSI-Chat-Copy-Invert-Highlight Chat.State ANSI-State Line 1 0]
                   ((ZEROP ExtendedSelectionLine)            (* ; "No Extension")

                    (ANSI-Chat-Copy-Invert-Highlight Chat.State ANSI-State SelectionLine 
                           SelectionStartColumn SelectionEndColumn))
                   ((= SelectionLine ExtendedSelectionLine)  (* ; "All on Same Line")

                    (ANSI-Chat-Copy-Invert-Highlight Chat.State ANSI-State SelectionLine
                           (IMIN SelectionStartColumn ExtendedSelectionStartColumn)
                           (IMAX SelectionEndColumn ExtendedSelectionEndColumn)))
                   ((< SelectionLine ExtendedSelectionLine)  (* ; "Selection before Extension")

                    (ANSI-Chat-Copy-Invert-Highlight Chat.State ANSI-State SelectionLine 
                           SelectionStartColumn 0)
                    [COND
                       ((< SelectionLine (SUB1 ExtendedSelectionLine))
                        (for Line from (ADD1 SelectionLine) to (SUB1 ExtendedSelectionLine)
                           do (COND
                                 ((NOT (ANSI-Chat-Copy-Top Chat.State ANSI-State Line))
                                  (ANSI-Chat-Copy-Invert-Highlight Chat.State ANSI-State Line 1 0]
                    (ANSI-Chat-Copy-Invert-Highlight Chat.State ANSI-State ExtendedSelectionLine 1 
                           ExtendedSelectionEndColumn))
                   (T (ANSI-Chat-Copy-Invert-Highlight Chat.State ANSI-State ExtendedSelectionLine 
                             ExtendedSelectionStartColumn 0)
                      [COND
                         ((< ExtendedSelectionLine (SUB1 SelectionLine))
                          (for Line from (ADD1 ExtendedSelectionLine) to (SUB1 SelectionLine)
                             do (COND
                                   ((NOT (ANSI-Chat-Copy-Top Chat.State ANSI-State Line))
                                    (ANSI-Chat-Copy-Invert-Highlight Chat.State ANSI-State Line 1 0]
                      (ANSI-Chat-Copy-Invert-Highlight Chat.State ANSI-State SelectionLine 1 
                             SelectionEndColumn])

(ANSI-Chat-Copy-Select
  [LAMBDA (Chat.State ANSI-State Line Column Button)      (* ; "Edited 22-Sep-87 13:11 by R.Beeman")

    (with CHAT.STATE Chat.State                              (* ; "Chat State")

          (with ANSI-STATE ANSI-State                        (* ; "ANSI State")

                (COND
                   ([OR (ZEROP SelectionLine)
                        (NOT (AND (OR (EQ SelectionType Button)
                                      (ZEROP SelectionStartColumn))
                                  (ZEROP ExtendedSelectionLine)
                                  (= Line SelectionLine)
                                  (<= SelectionStartColumn Column SelectionEndColumn]
          
          (* ;; "Something Changed Recalculate")

                    (SETQ SelectionType Button)
                    (ANSI-Chat-Update-Selection Chat.State ANSI-State Line (
                                                                           ANSI-Chat-Expand-Selection
                                                                            Chat.State ANSI-State 
                                                                            Line Column 'Left)
                           (ANSI-Chat-Expand-Selection Chat.State ANSI-State Line Column 'Right])

(ANSI-Chat-Copy-Top
  [LAMBDA (Chat.State ANSI-State Line)                    (* ; "Edited 21-Sep-87 11:37 by R.Beeman")

    (with CHAT.STATE Chat.State                              (* ; "Chat State")

          (with ANSI-STATE ANSI-State                        (* ; "ANSI State")

                (AND (= (ELT LineAttributes Line)
                        3)
                     (< Line BottomLine)
                     (= (ELT LineAttributes (ADD1 Line))
                        4)
                     (= (ELT (ELT Characters Line)
                             0)
                        (ELT (ELT Characters (ADD1 Line))
                             0])

(ANSI-Chat-CopyButtonEventFn
  [LAMBDA (Window)
    (DECLARE (GLOBALVARS ANSI-Chat-LineCursor))           (* ; "Edited 22-Sep-87 11:43 by R.Beeman")

    (LET* [(Chat.State (WINDOWPROP Window 'CHATSTATE))
           (Chat.Process (AND Chat.State (type? CHAT.STATE Chat.State)
                              (fetch (CHAT.STATE TYPEOUTPROC) of Chat.State)))
           (ANSI-State (AND Chat.State (type? CHAT.STATE Chat.State)
                            (fetch (CHAT.STATE TERM.STATE) of Chat.State]
          (COND
             ((AND (PROCESSP Chat.Process)
                   (NOT (RELPROCESSP Chat.Process)))
              (with CHAT.STATE Chat.State                    (* ; "Chat State")

                    (with ANSI-STATE ANSI-State              (* ; "ANSI State")

                          (SETQ SelectionLine 0)             (* ; "Indicate No Selection Yet")

                          (while (OR (KEYDOWNP 'LSHIFT)
                                     (KEYDOWNP 'RSHIFT)
                                     (KEYDOWNP 'COPY))
                             do [LET [(Column (SUB1 (IQUOTIENT (+ (LASTMOUSEX DSP)
                                                                  FONTWIDTH)
                                                           FONTWIDTH)))
                                      (Line (- BottomLine (SUB1 (IQUOTIENT (+ (LASTMOUSEY DSP)
                                                                              FONTHEIGHT)
                                                                       FONTHEIGHT]
                                     [COND
                                        [(AND (ZEROP Column)
                                              (<= 1 Line BottomLine))
                                         (COND
                                            ((NOT LineCursor)(* ; "LineCursor")

                                             (SETQ LineCursor T)
                                             (CURSOR (OR (AND (BOUNDP 'ANSI-Chat-LineCursor)
                                                              (type? CURSOR ANSI-Chat-LineCursor)
                                                              ANSI-Chat-LineCursor)
                                                         (SETQ ANSI-Chat-LineCursor
                                                          (CURSORCREATE 
                            #*(16 16)@@@A@@@C@@@G@@@O@@AO@@CO@@GO@@@O@@AK@@AI@@C@@@C@@@F@@@F@@@L@@@L@ 
                                                                 15 15]
                                        (T (COND
                                              (LineCursor (SETQ LineCursor NIL)
                                                     (CURSOR T]
                                     (COND
                                        ((NOT (MOUSESTATE UP))
                                         (COND
                                            ((AND (<= 0 Column (ADD1 RightMargin))
                                                  (<= 1 Line BottomLine))
                                             [COND
                                                ((ANSI-Chat-Copy-Top Chat.State ANSI-State Line)
                                                 (SETQ Line (ADD1 Line]
                                             [COND
                                                ((NOT (= (ELT LineAttributes Line)
                                                         5))
                                                 (SETQ Column (RSH (ADD1 Column)
                                                                   1]
                                             (SETQ Column (IMIN (ADD1 (ELT (ELT Characters Line)
                                                                           0))
                                                                Column))
                                             (SELECTQ LASTMOUSEBUTTONS
                                                 ((1 4) 
                                                      (ANSI-Chat-Copy-Select Chat.State ANSI-State 
                                                             Line Column LASTMOUSEBUTTONS))
                                                 (2 (ANSI-Chat-Copy-Extend Chat.State ANSI-State Line 
                                                           Column))
                                                 NIL))
                                            (T (SELECTQ LASTMOUSEBUTTONS
                                                   ((1 4)    (* ; "Cancel Selection")

                                                        (ANSI-Chat-Copy-Remove-Highlight Chat.State 
                                                               ANSI-State)
                                                        (SETQ SelectionLine 0))
                                                   NIL]
                                (BLOCK) finally (ANSI-Chat-Copy-Complete Chat.State ANSI-State])

(ANSI-Chat-Expand-Selection
  [LAMBDA (Chat.State ANSI-State Line Column Direction)   (* ; "Edited 27-Sep-87 18:42 by R.Beeman")

    (with
     CHAT.STATE Chat.State                                   (* ; "Chat State")

     (with ANSI-STATE ANSI-State                             (* ; "ANSI State")

           (COND
              ((OR (ZEROP Column)
                   (= SelectionType 4))
               Column)
              (T (LET* [(CharacterLine (ELT Characters Line))
                        (LastColumn (ELT CharacterLine 0))
                        (SeedType (COND
                                     ((> Column LastColumn)
                                      'WhiteSpace)
                                     (T (ANSI-Chat-Growable-Character (ELT CharacterLine Column]
                       (SELECTQ Direction
                           (Left                             (* ; "Grow Left")

                                 (while (AND (> Column 1)
                                             (EQ (ANSI-Chat-Growable-Character (ELT CharacterLine
                                                                                    (SUB1 Column)))
                                                 SeedType)) do (SETQ Column (SUB1 Column))
                                    finally (RETURN Column)))
                           (PROGN                            (* ; "Grow Right")

                                  (while [OR (AND (< Column LastColumn)
                                                  (EQ (ANSI-Chat-Growable-Character
                                                       (ELT CharacterLine (ADD1 Column)))
                                                      SeedType))
                                             (AND (= Column LastColumn)
                                                  (EQ SeedType 'WhiteSpace]
                                     do (SETQ Column (ADD1 Column)) finally (RETURN Column])

(ANSI-Chat-Growable-Character
  [LAMBDA (CharacterCode)                                 (* ; "Edited 23-Sep-87 15:24 by R.Beeman")

    (COND
       ((= (CHARCODE SPACE)
           CharacterCode)
        'WhiteSpace)
       ((OR (<= (CHARCODE 0)
             CharacterCode
             (CHARCODE 9))
            (<= (CHARCODE A)
             CharacterCode
             (CHARCODE Z))
            (<= (CHARCODE a)
             CharacterCode
             (CHARCODE z)))
        'Text)
       (T 'Punctuation])

(ANSI-Chat-Update-Extended-Selection
  [LAMBDA (Chat.State ANSI-State NewSelectionLine NewSelectionStartColumn NewSelectionEndColumn)
                                                          (* ; "Edited 22-Sep-87 17:29 by R.Beeman")
          
          (* ;; "To update the current Highlighted Text, there may be additions and/or deletions")
          
          (* ;; "The updates are organized into eight ordering cases where S -- Initial Selection, E -- Current Extension, N -- New Extension")
          
          (* ;; "Case 0 [..NS...], Case 1 [...SN..] -- No Current Extension")
          
          (* ;; 
     "Case 2 [NE.S...], Case 3 [...SNE.], Case 4 [..NS.E.] -- New Extension before Current Extension")
          
          (* ;; 
      "Case 5 [...S.EN], Case 6 [.ENS...], Case 7 [.E.SN..] -- New Extension after Current Extension")

    (with CHAT.STATE Chat.State                              (* ; "Chat State")

          (with ANSI-STATE ANSI-State                        (* ; "ANSI State")

                (COND
                   [(ZEROP SelectionStartColumn)             (* ; "Handle Line Selections")

                    (COND
                       [(ZEROP ExtendedSelectionLine)        (* ; "No Current Extension")

                        (COND
                           ((< NewSelectionLine SelectionLine)
                                                             (* ; "Case 0")

                            (ANSI-Chat-Copy-Invert-Highlight-Lines Chat.State ANSI-State 
                                   NewSelectionLine (SUB1 SelectionLine)))
                           (T                                (* ; "Case 1")

                              (ANSI-Chat-Copy-Invert-Highlight-Lines Chat.State ANSI-State
                                     (ADD1 SelectionLine)
                                     NewSelectionLine]
                       [(< NewSelectionLine ExtendedSelectionLine)
                                                             (* ; 
                                                             "New Extension before Current Extension")

                        (COND
                           ((< ExtendedSelectionLine SelectionLine)
                                                             (* ; "Case 2")

                            (ANSI-Chat-Copy-Invert-Highlight-Lines Chat.State ANSI-State 
                                   NewSelectionLine (SUB1 ExtendedSelectionLine)))
                           ((< SelectionLine NewSelectionLine)
                                                             (* ; "Case 3")

                            (ANSI-Chat-Copy-Invert-Highlight-Lines Chat.State ANSI-State (ADD1 
                                                                                     NewSelectionLine
                                                                                               )
                                   ExtendedSelectionLine))
                           (T                                (* ; "Case 4")

                              (ANSI-Chat-Copy-Invert-Highlight-Lines Chat.State ANSI-State
                                     (ADD1 SelectionLine)
                                     ExtendedSelectionLine)
                              (ANSI-Chat-Copy-Invert-Highlight-Lines Chat.State ANSI-State 
                                     NewSelectionLine (SUB1 SelectionLine]
                       ((< SelectionLine ExtendedSelectionLine)
                                                             (* ; "Case 5")

                        (ANSI-Chat-Copy-Invert-Highlight-Lines Chat.State ANSI-State (ADD1 
                                                                                ExtendedSelectionLine
                                                                                           )
                               NewSelectionLine))
                       ((< NewSelectionLine SelectionLine)   (* ; "Case 6")

                        (ANSI-Chat-Copy-Invert-Highlight-Lines Chat.State ANSI-State 
                               ExtendedSelectionLine (SUB1 NewSelectionLine)))
                       (T                                    (* ; "Case 7")

                          (ANSI-Chat-Copy-Invert-Highlight-Lines Chat.State ANSI-State 
                                 ExtendedSelectionLine (SUB1 SelectionLine))
                          (ANSI-Chat-Copy-Invert-Highlight-Lines Chat.State ANSI-State (ADD1 
                                                                                        SelectionLine
                                                                                             )
                                 NewSelectionLine]
                   [(ZEROP ExtendedSelectionLine)            (* ; "No Current Extension")

                    (COND
                       [(= SelectionLine NewSelectionLine)   (* ; "S and N on Same Line")

                        (COND
                           ((< NewSelectionStartColumn SelectionStartColumn)
                                                             (* ; "Case 0")

                            (ANSI-Chat-Copy-Invert-Highlight Chat.State ANSI-State NewSelectionLine 
                                   NewSelectionStartColumn (SUB1 SelectionStartColumn)
                                   T))
                           (T                                (* ; "Case 1")

                              (ANSI-Chat-Copy-Invert-Highlight Chat.State ANSI-State NewSelectionLine
                                     (ADD1 SelectionEndColumn)
                                     NewSelectionEndColumn]
                       ((< NewSelectionLine SelectionLine)   (* ; "Case 0")

                        (ANSI-Chat-Copy-Invert-Highlight Chat.State ANSI-State NewSelectionLine 
                               NewSelectionStartColumn 0)
                        (ANSI-Chat-Copy-Invert-Highlight-Lines Chat.State ANSI-State (ADD1 
                                                                                     NewSelectionLine
                                                                                           )
                               (SUB1 SelectionLine))
                        (ANSI-Chat-Copy-Invert-Highlight Chat.State ANSI-State SelectionLine 1
                               (SUB1 SelectionStartColumn)
                               T))
                       (T                                    (* ; "Case 1")

                          (ANSI-Chat-Copy-Invert-Highlight Chat.State ANSI-State SelectionLine
                                 (ADD1 SelectionEndColumn)
                                 0)
                          (ANSI-Chat-Copy-Invert-Highlight-Lines Chat.State ANSI-State (ADD1 
                                                                                        SelectionLine
                                                                                             )
                                 (SUB1 NewSelectionLine))
                          (ANSI-Chat-Copy-Invert-Highlight Chat.State ANSI-State NewSelectionLine 1 
                                 NewSelectionEndColumn]
                   [(= NewSelectionLine ExtendedSelectionLine)
                                                             (* ; "N and E on Same Line")

                    (COND
                       [(< NewSelectionStartColumn ExtendedSelectionStartColumn)
                                                             (* ; 
                                                             "New Extension before Current Extension")

                        (COND
                           [(= NewSelectionLine SelectionLine)
                                                             (* ; "All on Same Line")

                            (COND
                               ((< ExtendedSelectionStartColumn SelectionStartColumn)
                                                             (* ; "Case 2")

                                (ANSI-Chat-Copy-Invert-Highlight Chat.State ANSI-State 
                                       NewSelectionLine NewSelectionStartColumn (SUB1 
                                                                         ExtendedSelectionStartColumn
                                                                                      )
                                       T))
                               ((< SelectionStartColumn NewSelectionStartColumn)
                                                             (* ; "Case 3")

                                (ANSI-Chat-Copy-Invert-Highlight Chat.State ANSI-State 
                                       NewSelectionLine (ADD1 NewSelectionEndColumn)
                                       ExtendedSelectionEndColumn))
                               (T                            (* ; "Case 4")

                                  (ANSI-Chat-Copy-Invert-Highlight Chat.State ANSI-State 
                                         NewSelectionLine (ADD1 SelectionEndColumn)
                                         ExtendedSelectionEndColumn)
                                  (ANSI-Chat-Copy-Invert-Highlight Chat.State ANSI-State 
                                         NewSelectionLine NewSelectionStartColumn (SUB1 
                                                                                 SelectionStartColumn
                                                                                        )
                                         T]
                           ((< NewSelectionLine SelectionLine)
                                                             (* ; "Case 2")

                            (ANSI-Chat-Copy-Invert-Highlight Chat.State ANSI-State NewSelectionLine 
                                   NewSelectionStartColumn (SUB1 ExtendedSelectionStartColumn)
                                   T))
                           (T                                (* ; "Case 3 -- (Case 4 not possible)")

                              (ANSI-Chat-Copy-Invert-Highlight Chat.State ANSI-State NewSelectionLine
                                     (ADD1 NewSelectionEndColumn)
                                     ExtendedSelectionEndColumn]
                       [(= NewSelectionLine SelectionLine)   (* ; "All on Same Line")

                        (COND
                           ((< SelectionStartColumn ExtendedSelectionStartColumn)
                                                             (* ; "Case 5")

                            (ANSI-Chat-Copy-Invert-Highlight Chat.State ANSI-State NewSelectionLine
                                   (ADD1 ExtendedSelectionEndColumn)
                                   NewSelectionEndColumn))
                           ((< NewSelectionStartColumn SelectionStartColumn)
                                                             (* ; "Case 6")

                            (ANSI-Chat-Copy-Invert-Highlight Chat.State ANSI-State NewSelectionLine 
                                   ExtendedSelectionStartColumn (SUB1 NewSelectionStartColumn)
                                   T))
                           (T                                (* ; "Case 7")

                              (ANSI-Chat-Copy-Invert-Highlight Chat.State ANSI-State NewSelectionLine 
                                     ExtendedSelectionStartColumn (SUB1 SelectionStartColumn)
                                     T)
                              (ANSI-Chat-Copy-Invert-Highlight Chat.State ANSI-State NewSelectionLine
                                     (ADD1 SelectionEndColumn)
                                     NewSelectionEndColumn]
                       ((< SelectionLine NewSelectionLine)   (* ; "Case 5")

                        (ANSI-Chat-Copy-Invert-Highlight Chat.State ANSI-State NewSelectionLine
                               (ADD1 ExtendedSelectionEndColumn)
                               NewSelectionEndColumn))
                       (T                                    (* ; "Case 6 -- (Case 7 not possible)")

                          (ANSI-Chat-Copy-Invert-Highlight Chat.State ANSI-State NewSelectionLine 
                                 ExtendedSelectionStartColumn (SUB1 NewSelectionStartColumn)
                                 T]
                   [(< NewSelectionLine ExtendedSelectionLine)
                                                             (* ; 
                                                             "New Extension before Current Extension")

                    (COND
                       [(= ExtendedSelectionLine SelectionLine)
                                                             (* ; "E and S on Same Line")

                        (ANSI-Chat-Copy-Invert-Highlight Chat.State ANSI-State NewSelectionLine 
                               NewSelectionStartColumn 0)
                        (ANSI-Chat-Copy-Invert-Highlight-Lines Chat.State ANSI-State (ADD1 
                                                                                     NewSelectionLine
                                                                                           )
                               (SUB1 ExtendedSelectionLine))
                        (COND
                           ((< ExtendedSelectionStartColumn SelectionStartColumn)
                                                             (* ; "Case 2")

                            (ANSI-Chat-Copy-Invert-Highlight Chat.State ANSI-State 
                                   ExtendedSelectionLine 1 (SUB1 ExtendedSelectionStartColumn)
                                   T))
                           (T                                (* ; "Case 4 -- (Case 3 not possible)")

                              (ANSI-Chat-Copy-Invert-Highlight Chat.State ANSI-State SelectionLine
                                     (ADD1 SelectionEndColumn)
                                     ExtendedSelectionEndColumn)
                              (ANSI-Chat-Copy-Invert-Highlight Chat.State ANSI-State SelectionLine 1
                                     (SUB1 SelectionStartColumn)
                                     T]
                       [(= NewSelectionLine SelectionLine)   (* ; "N and S on Same Line")

                        (ANSI-Chat-Copy-Invert-Highlight-Lines Chat.State ANSI-State (ADD1 
                                                                                     NewSelectionLine
                                                                                           )
                               (SUB1 ExtendedSelectionLine))
                        (ANSI-Chat-Copy-Invert-Highlight Chat.State ANSI-State ExtendedSelectionLine 
                               1 ExtendedSelectionEndColumn)
                        (COND
                           ((< SelectionStartColumn NewSelectionStartColumn)
                                                             (* ; "Case 3")

                            (ANSI-Chat-Copy-Invert-Highlight Chat.State ANSI-State NewSelectionLine
                                   (ADD1 NewSelectionEndColumn)
                                   0))
                           (T                                (* ; "Case 4 -- (Case 2 not possible)")

                              (ANSI-Chat-Copy-Invert-Highlight Chat.State ANSI-State NewSelectionLine
                                     (ADD1 SelectionEndColumn)
                                     0)
                              (ANSI-Chat-Copy-Invert-Highlight Chat.State ANSI-State NewSelectionLine 
                                     NewSelectionStartColumn (SUB1 SelectionStartColumn)
                                     T]
                       ((< ExtendedSelectionLine SelectionLine)
                                                             (* ; "Case 2")

                        (ANSI-Chat-Copy-Invert-Highlight Chat.State ANSI-State NewSelectionLine 
                               NewSelectionStartColumn 0)
                        (ANSI-Chat-Copy-Invert-Highlight-Lines Chat.State ANSI-State (ADD1 
                                                                                     NewSelectionLine
                                                                                           )
                               (SUB1 ExtendedSelectionLine))
                        (ANSI-Chat-Copy-Invert-Highlight Chat.State ANSI-State ExtendedSelectionLine 
                               1 (SUB1 ExtendedSelectionStartColumn)
                               T))
                       ((< SelectionLine NewSelectionLine)   (* ; "Case 3")

                        (ANSI-Chat-Copy-Invert-Highlight Chat.State ANSI-State NewSelectionLine
                               (ADD1 NewSelectionEndColumn)
                               0)
                        (ANSI-Chat-Copy-Invert-Highlight-Lines Chat.State ANSI-State (ADD1 
                                                                                     NewSelectionLine
                                                                                           )
                               (SUB1 ExtendedSelectionLine))
                        (ANSI-Chat-Copy-Invert-Highlight Chat.State ANSI-State ExtendedSelectionLine 
                               1 ExtendedSelectionEndColumn))
                       (T                                    (* ; "Case 4")

                          (ANSI-Chat-Copy-Invert-Highlight Chat.State ANSI-State SelectionLine
                                 (ADD1 SelectionEndColumn)
                                 0)
                          (ANSI-Chat-Copy-Invert-Highlight-Lines Chat.State ANSI-State (ADD1 
                                                                                        SelectionLine
                                                                                             )
                                 (SUB1 ExtendedSelectionLine))
                          (ANSI-Chat-Copy-Invert-Highlight Chat.State ANSI-State 
                                 ExtendedSelectionLine 1 ExtendedSelectionEndColumn)
                          (ANSI-Chat-Copy-Invert-Highlight Chat.State ANSI-State NewSelectionLine 
                                 NewSelectionStartColumn 0)
                          (ANSI-Chat-Copy-Invert-Highlight-Lines Chat.State ANSI-State (ADD1 
                                                                                     NewSelectionLine
                                                                                             )
                                 (SUB1 SelectionLine))
                          (ANSI-Chat-Copy-Invert-Highlight Chat.State ANSI-State SelectionLine 1
                                 (SUB1 SelectionStartColumn)
                                 T]
                   [(= ExtendedSelectionLine SelectionLine)  (* ; "E and S on Same Line")

                    (ANSI-Chat-Copy-Invert-Highlight-Lines Chat.State ANSI-State (ADD1 SelectionLine)
                           (SUB1 NewSelectionLine))
                    (ANSI-Chat-Copy-Invert-Highlight Chat.State ANSI-State NewSelectionLine 1 
                           NewSelectionEndColumn)
                    (COND
                       ((< SelectionStartColumn ExtendedSelectionStartColumn)
                                                             (* ; "Case 5")

                        (ANSI-Chat-Copy-Invert-Highlight Chat.State ANSI-State ExtendedSelectionLine
                               (ADD1 ExtendedSelectionEndColumn)
                               0))
                       (T                                    (* ; "Case 7 -- (Case 6 not possible)")

                          (ANSI-Chat-Copy-Invert-Highlight Chat.State ANSI-State SelectionLine 
                                 ExtendedSelectionStartColumn (SUB1 SelectionStartColumn)
                                 T)
                          (ANSI-Chat-Copy-Invert-Highlight Chat.State ANSI-State SelectionLine
                                 (ADD1 SelectionEndColumn)
                                 0]
                   [(= NewSelectionLine SelectionLine)       (* ; "N and S on Same Line")

                    (ANSI-Chat-Copy-Invert-Highlight Chat.State ANSI-State ExtendedSelectionLine 
                           ExtendedSelectionStartColumn 0)
                    (ANSI-Chat-Copy-Invert-Highlight-Lines Chat.State ANSI-State (ADD1 
                                                                                ExtendedSelectionLine
                                                                                       )
                           (SUB1 NewSelectionLine))
                    (COND
                       ((< NewSelectionStartColumn SelectionStartColumn)
                                                             (* ; "Case 6")

                        (ANSI-Chat-Copy-Invert-Highlight Chat.State ANSI-State NewSelectionLine 1
                               (SUB1 NewSelectionStartColumn)
                               T))
                       (T                                    (* ; "Case 7 -- (Case 5 not possible)")

                          (ANSI-Chat-Copy-Invert-Highlight Chat.State ANSI-State NewSelectionLine 1
                                 (SUB1 SelectionStartColumn)
                                 T)
                          (ANSI-Chat-Copy-Invert-Highlight Chat.State ANSI-State NewSelectionLine
                                 (ADD1 SelectionEndColumn)
                                 NewSelectionEndColumn]
                   ((< SelectionLine ExtendedSelectionLine)  (* ; "Case 5")

                    (ANSI-Chat-Copy-Invert-Highlight Chat.State ANSI-State ExtendedSelectionLine
                           (ADD1 ExtendedSelectionEndColumn)
                           0)
                    (ANSI-Chat-Copy-Invert-Highlight-Lines Chat.State ANSI-State (ADD1 
                                                                                ExtendedSelectionLine
                                                                                       )
                           (SUB1 NewSelectionLine))
                    (ANSI-Chat-Copy-Invert-Highlight Chat.State ANSI-State NewSelectionLine 1 
                           NewSelectionEndColumn))
                   ((< NewSelectionLine SelectionLine)       (* ; "Case 6")

                    (ANSI-Chat-Copy-Invert-Highlight Chat.State ANSI-State ExtendedSelectionLine 
                           ExtendedSelectionStartColumn 0)
                    (ANSI-Chat-Copy-Invert-Highlight-Lines Chat.State ANSI-State (ADD1 
                                                                                ExtendedSelectionLine
                                                                                       )
                           (SUB1 NewSelectionLine))
                    (ANSI-Chat-Copy-Invert-Highlight Chat.State ANSI-State NewSelectionLine 1
                           (SUB1 NewSelectionStartColumn)
                           T))
                   (T                                        (* ; "Case 7")

                      (ANSI-Chat-Copy-Invert-Highlight Chat.State ANSI-State ExtendedSelectionLine 
                             ExtendedSelectionStartColumn 0)
                      (ANSI-Chat-Copy-Invert-Highlight-Lines Chat.State ANSI-State (ADD1 
                                                                                ExtendedSelectionLine
                                                                                         )
                             (SUB1 SelectionLine))
                      (ANSI-Chat-Copy-Invert-Highlight Chat.State ANSI-State SelectionLine 1
                             (SUB1 SelectionStartColumn)
                             T)
                      (ANSI-Chat-Copy-Invert-Highlight Chat.State ANSI-State SelectionLine
                             (ADD1 SelectionEndColumn)
                             0)
                      (ANSI-Chat-Copy-Invert-Highlight-Lines Chat.State ANSI-State (ADD1 
                                                                                        SelectionLine
                                                                                         )
                             (SUB1 NewSelectionLine))
                      (ANSI-Chat-Copy-Invert-Highlight Chat.State ANSI-State NewSelectionLine 1 
                             NewSelectionEndColumn)))
                (SETQ ExtendedSelectionLine NewSelectionLine)
                (SETQ ExtendedSelectionStartColumn NewSelectionStartColumn)
                (SETQ ExtendedSelectionEndColumn NewSelectionEndColumn])

(ANSI-Chat-Update-Selection
  [LAMBDA (Chat.State ANSI-State NewSelectionLine NewSelectionStartColumn NewSelectionEndColumn)
                                                          (* ; "Edited 22-Sep-87 11:01 by R.Beeman")

    (with CHAT.STATE Chat.State                              (* ; "Chat State")

          (with ANSI-STATE ANSI-State                        (* ; "ANSI State")

                (ANSI-Chat-Copy-Remove-Highlight Chat.State ANSI-State)
                (ANSI-Chat-Copy-Invert-Highlight Chat.State ANSI-State NewSelectionLine
                       (IMAX 1 NewSelectionStartColumn)
                       NewSelectionEndColumn)
                (SETQ SelectionLine NewSelectionLine)
                (SETQ SelectionStartColumn NewSelectionStartColumn)
                (SETQ SelectionEndColumn NewSelectionEndColumn)
                (SETQ ExtendedSelectionLine 0])
)
(DECLARE%: EVAL@COMPILE DONTCOPY 
(FILESLOAD (SOURCE)
       CHATDECLS ANSICHATDECLS)
)
(DECLARE%: DONTCOPY
  (FILEMAP (NIL (1337 51894 (ANSI-Chat-Copy-Complete 1347 . 7655) (ANSI-Chat-Copy-Extend 7657 . 9745) (
ANSI-Chat-Copy-Get-String 9747 . 10626) (ANSI-Chat-Copy-Invert-Highlight 10628 . 12326) (
ANSI-Chat-Copy-Invert-Highlight-Lines 12328 . 12671) (ANSI-Chat-Copy-Remove-Highlight 12673 . 16161) (
ANSI-Chat-Copy-Select 16163 . 17484) (ANSI-Chat-Copy-Top 17486 . 18173) (ANSI-Chat-CopyButtonEventFn 
18175 . 23220) (ANSI-Chat-Expand-Selection 23222 . 25242) (ANSI-Chat-Growable-Character 25244 . 25762)
 (ANSI-Chat-Update-Extended-Selection 25764 . 50970) (ANSI-Chat-Update-Selection 50972 . 51892)))))
STOP