anchor.diff:     (input :initarg :input)
      (queued-bytes :initarg :queued-bytes)
      (interrupt-character :initarg :interrupt-character :accessor interrupt-character)
  -   (state :initarg :state :accessor terminal-state)
  -   (current-number)
  -   (parameters)
  -   (escape-sequence)
  +   (state :initform 'xterm-initial :accessor terminal-state)
  +   (current-number :initform nil)
  +   (parameters :initform '())
  +   (escape-sequence :initform '())
      (width :reader terminal-width)
      (height :reader terminal-height)
      (x-pos :initform 0 :accessor x-pos)
  
positive.code:                           #xFF000000 (terminal-framebuffer term)
                            (+ (* (y-pos term) 16) (y-offset term)) (+ (* left 8) (x-offset term)))))
  
  (defmethod sys.int::stream-write-byte (byte (stream xterm-terminal))
    (ecase (terminal-state stream)
      ((nil)
       (case (code-char byte)
         (#\Escape
          (setf (terminal-state stream) :saw-escape
                (slot-value stream 'current-number) nil
                (slot-value stream 'parameters) '()
                (slot-value stream 'escape-sequence) '()))
         (#\Cr
          (setf (x-pos stream) 0))
         (#\Lf
          (incf (y-pos stream)))
         (#\Bs
          (decf (x-pos stream)))
         (t (write-terminal stream (code-char byte)))))
      (:saw-escape
       (push byte (slot-value stream 'escape-sequence))
       ;; Saw escape byte.
       (case (code-char byte)
         (#\[ (setf (terminal-state stream) :saw-bracket))
         (#\( (setf (terminal-state stream) :saw-paren))
         (#\> (finish-parsing-escape stream))
         (#\= (finish-parsing-escape stream))
         (#\M (finish-parsing-escape stream)) ; Reverse index?
         (t (give-up-parsing-escape stream))))
      (:saw-bracket ;; Saw "\e[" and maybe some digits and semicolons.
       (push byte (slot-value stream 'escape-sequence))
       (cond ((eql (code-char byte) #\;)
              (cond ((slot-value stream 'current-number)
                     (push (slot-value stream 'current-number) (slot-value stream 'parameters))
                     (setf (slot-value stream 'current-number) nil))
                    (t (push 0 (slot-value stream 'parameters)))))
             ((digit-char-p (code-char byte))
              (unless (slot-value stream 'current-number)
                (setf (slot-value stream 'current-number) 0))
              (setf (slot-value stream 'current-number) (+ (* (slot-value stream 'current-number) 10)
                                                           (- byte (char-code #\0)))))
             (t (when (slot-value stream 'current-number)
                  (push (slot-value stream 'current-number) (slot-value stream 'parameters))
                  (setf (slot-value stream 'current-number) nil))
                (case (code-char byte)
                  (#\A
                   (decf (y-pos stream) (or (first (slot-value stream 'parameters)) 1))
                   (finish-parsing-escape stream))
                  (#\B
                   (incf (y-pos stream) (or (first (slot-value stream 'parameters)) 1))
                   (finish-parsing-escape stream))
                  (#\C
                   (incf (x-pos stream) (or (first (slot-value stream 'parameters)) 1))
                   (finish-parsing-escape stream))
                  (#\D
                   (decf (x-pos stream) (or (first (slot-value stream 'parameters)) 1))
                   (finish-parsing-escape stream))
                  (#\H
                   (let* ((params (nreverse (slot-value stream 'parameters)))
                          (row (or (first params) 1))
                          (column (or (second params) 1)))
                     (setf (y-pos stream) (1- row)
                           (x-pos stream) (1- column)))
                   (finish-parsing-escape stream))
                  (#\G
                   (setf (x-pos stream) (1- (or (first (slot-value stream 'parameters)) 1)))
                   (finish-parsing-escape stream))
                  (#\J
                   (case (first (slot-value stream 'parameters))
                     ((0 nil) (clear stream 0 (y-pos stream))) ; clear above
                     (1 (clear stream (y-pos stream) (terminal-height stream))) ; clear below
                     (2 (clear stream 0 (terminal-height stream)))
                     (t (give-up-parsing-escape stream)))
                   (finish-parsing-escape stream))
                  (#\K
                   (case (first (slot-value stream 'parameters))
                     ((0 nil) (erase stream (x-pos stream) (terminal-width stream))) ; erase to right
                     (1 (erase stream 0 (x-pos stream))) ; erase to left
                     (2 (erase stream 0 (terminal-width stream))) ; erase all
                     (t (give-up-parsing-escape stream)))
                   (finish-parsing-escape stream))
                  (#\d
                   (setf (y-pos stream) (1- (or (first (slot-value stream 'parameters)) 1)))
                   (finish-parsing-escape stream))
                  (#\m (finish-parsing-escape stream)) ; character attributes **
                  (#\r (finish-parsing-escape stream)) ; set scroll region. **
                  (#\h (finish-parsing-escape stream)) ; set mode **
                  (#\l (finish-parsing-escape stream)) ; clear mode **
                  (#\?
                   (if (or (slot-value stream 'parameters)
                           (slot-value stream 'current-number))
                       (give-up-parsing-escape stream)
                       (setf (terminal-state stream) :saw-bracket-question)))
                  (t (give-up-parsing-escape stream))))))
      (:saw-bracket-question
       ;; Saw "\e[?"
       (push byte (slot-value stream 'escape-sequence))
       (cond ((eql (code-char byte) #\;)
              (cond ((slot-value stream 'current-number)
                     (push (slot-value stream 'current-number) (slot-value stream 'parameters))
                     (setf (slot-value stream 'current-number) nil))
                    (t (push 0 (slot-value stream 'parameters)))))
             ((digit-char-p (code-char byte))
              (unless (slot-value stream 'current-number)
                (setf (slot-value stream 'current-number) 0))
              (setf (slot-value stream 'current-number) (+ (* (slot-value stream 'current-number) 10)
                                                           (- byte (char-code #\0)))))
             (t (when (slot-value stream 'current-number)
                  (push (slot-value stream 'current-number) (slot-value stream 'parameters))
                  (setf (slot-value stream 'current-number) nil))
                (case (code-char byte)
                  (#\h (finish-parsing-escape stream)) ; DEC private mode set **
                  (#\l (finish-parsing-escape stream)) ; DEC private mode clear **
                  (t (give-up-parsing-escape stream))))))
      (:saw-paren
       ;; Saw "\e(", expecting a character set identifier. **
       (push byte (slot-value stream 'escape-sequence))
       (finish-parsing-escape stream))))
  
  (defvar *xterm-translations*
    (list (list (name-char "Up-Arrow")    '(#\Esc #\[ #\A))
  
negative.code:                           #xFF000000 fb
                            (y-offset term) (x-offset term))))
  
  (defun report-unknown-escape (term)
    (format t "Failed to parse escape sequence ~S in state ~S.~%"
            (mapcar 'code-char (nreverse (slot-value term 'escape-sequence)))
            (terminal-state term)))
  
  (defun write-terminal (term char)
    (let ((x (x-pos term)) (y (y-pos term)))
  