Function: term-handle-ansi-escape

term-handle-ansi-escape is a byte-compiled function defined in term.el.gz.

Signature

(term-handle-ansi-escape PROC PARAMS CHAR &optional PRIVATE)

Source Code

;; Defined in /usr/src/emacs/lisp/term.el.gz
;; Handle a character assuming (eq terminal-state 2) -
;; i.e. we have previously seen Escape followed by ?[.

(defun term-handle-ansi-escape (proc params char &optional private)
  (cond
   ((and private (not (memq char '(?h ?l))))
    ;; Recognize private capabilities only for mode entry and exit
    nil)
   ((or (eq char ?H)  ;; cursor motion (terminfo: cup,home)
	;; (eq char ?f) ;; xterm seems to handle this sequence too, not
	;; needed for now
	)
    (term-goto
     (1- (max 1 (min (or (nth 0 params) 0) term-height)))
     (1- (max 1 (min (or (nth 1 params) 0) term-width)))))
   ;; \E[A - cursor up (terminfo: cuu, cuu1)
   ((eq char ?A)
    (term-handle-deferred-scroll)
    (let ((tcr (term-current-row))
          (scroll-amount (car params)))
      (term-down
       (if (< (- tcr scroll-amount) term-scroll-start)
	   ;; If the amount to move is before scroll start, move
	   ;; to scroll start.
	   (- term-scroll-start tcr)
         (if (>= scroll-amount tcr)
	     (- tcr)
           (- (max 1 scroll-amount))))
       t)))
   ;; \E[B - cursor down (terminfo: cud)
   ((eq char ?B)
    (let ((tcr (term-current-row))
          (scroll-amount (car params)))
      (unless (>= tcr term-scroll-end)
	(term-down
         (min (- term-scroll-end tcr) (max 1 scroll-amount))
         t))))
   ;; \E[C - cursor right (terminfo: cuf, cuf1)
   ((eq char ?C)
    (term-move-columns
     (max 1
          (if (>= (+ (car params) (term-current-column)) term-width)
	      (- term-width (term-current-column)  1)
            (car params)))))
   ;; \E[D - cursor left (terminfo: cub)
   ((eq char ?D)
    (term-move-columns (- (max 1 (car params)))))
   ;; \E[G - cursor motion to absolute column (terminfo: hpa)
   ((eq char ?G)
    (term-move-columns (- (max 0 (min term-width (car params)))
                          (term-current-column))))
   ;; \E[J - clear to end of screen (terminfo: ed, clear)
   ((eq char ?J)
    (term-erase-in-display (car params)))
   ;; \E[K - clear to end of line (terminfo: el, el1)
   ((eq char ?K)
    (term-erase-in-line (car params)))
   ;; \E[L - insert lines (terminfo: il, il1)
   ((eq char ?L)
    (term-insert-lines (max 1 (car params))))
   ;; \E[M - delete lines (terminfo: dl, dl1)
   ((eq char ?M)
    (term-delete-lines (max 1 (car params))))
   ;; \E[P - delete chars (terminfo: dch, dch1)
   ((eq char ?P)
    (term-delete-chars (max 1 (car params))))
   ;; \E[@ - insert spaces (terminfo: ich)
   ((eq char ?@)
    (term-insert-spaces (max 1 (car params))))
   ;; \E[?h - DEC Private Mode Set

   ;; N.B. we previously had a bug in which we'd decode \e[?<NR>h or
   ;; \e[?<NR>l as a command with zero in the params field and so
   ;; didn't recognize DEC private escape sequences.  However, the
   ;; termcap and terminfo files had the non-? (question mark means DEC
   ;; private) versions, so things kind of worked anyway.  To preserve
   ;; compatibility, we recognize both private- and non-private
   ;; messages for capabilities we added before we fixed the bug but
   ;; require the private flag for capabilities we added after.
   ((eq char ?h)
    (cond ((eq (car params) 4) ;; (terminfo: smir)
           (setq term-insert-mode t))
          ((and private (eq (car params) 7)) ;; (terminfo: smam)
           (setq term-auto-margins t))
          ((eq (car params) 47) ;; (terminfo: smcup)
           (term-switch-to-alternate-sub-buffer t))))
   ;; \E[?l - DEC Private Mode Reset
   ((eq char ?l)
    (cond ((eq (car params) 4) ;; (terminfo: rmir)
           (setq term-insert-mode nil))
          ((and private (eq (car params) 7)) ;; (terminfo: rmam)
           (setq term-auto-margins nil))
          ((eq (car params) 47) ;; (terminfo: rmcup)
           (term-switch-to-alternate-sub-buffer nil))))

   ;; Modified to allow ansi coloring -mm
   ;; \E[m - Set/reset modes, set bg/fg
   ;;(terminfo: smso,rmso,smul,rmul,rev,bold,dim,sitm,ritm,blink,sgr0,invis,op,setab,setaf)
   ((eq char ?m)
    (term--handle-colors-list params))

   ;; \E[6n - Report cursor position (terminfo: u7)
   ((eq char ?n)
    (term-handle-deferred-scroll)
    (process-send-string proc
			 ;; (terminfo: u6)
			 (format "\e[%s;%sR"
				 (1+ (term-current-row))
				 (1+ (term-horizontal-column)))))
   ;; \E[r - Set scrolling region (terminfo: csr)
   ((eq char ?r)
    (term-set-scroll-region
     (1- (or (nth 0 params) 0))
     (1- (or (nth 1 params) 0))))
   (t)))