Function: ansi-color-apply-sequence
ansi-color-apply-sequence is a byte-compiled function defined in
ansi-color.el.gz.
Signature
(ansi-color-apply-sequence ESCAPE-SEQUENCE CODES)
Documentation
Apply ESCAPE-SEQUENCE to CODES and return the new list of codes.
ESCAPE-SEQUENCE is an escape sequence parsed by
ansi-color-parse-sequence.
For each new code, the following happens: if it is 1-7, add it to the list of codes; if it is 21-25 or 27, delete appropriate parameters from the list of codes; if it is 30-37 (or 90-97) resp. 39, the foreground color code is replaced or added resp. deleted; if it is 40-47 (or 100-107) resp. 49, the background color code is replaced or added resp. deleted; any other code is discarded together with the old codes. Finally, the so changed list of codes is returned.
Source Code
;; Defined in /usr/src/emacs/lisp/ansi-color.el.gz
(defun ansi-color-apply-sequence (escape-sequence codes)
"Apply ESCAPE-SEQUENCE to CODES and return the new list of codes.
ESCAPE-SEQUENCE is an escape sequence parsed by
`ansi-color-parse-sequence'.
For each new code, the following happens: if it is 1-7, add it to
the list of codes; if it is 21-25 or 27, delete appropriate
parameters from the list of codes; if it is 30-37 (or 90-97) resp. 39,
the foreground color code is replaced or added resp. deleted; if it
is 40-47 (or 100-107) resp. 49, the background color code is replaced
or added resp. deleted; any other code is discarded together with the
old codes. Finally, the so changed list of codes is returned."
(let ((new-codes (ansi-color-parse-sequence escape-sequence)))
(while new-codes
(let* ((new (pop new-codes))
(q (/ new 10)))
(setq codes
(pcase q
(0 (unless (memq new '(0 8 9))
(cons new (remq new codes))))
(2 (unless (memq new '(20 26 28 29))
;; The standard says `21 doubly underlined' while
;; https://en.wikipedia.org/wiki/ANSI_escape_code claims
;; `21 Bright/Bold: off or Underline: Double'.
(remq (- new 20) (pcase new
(22 (remq 1 codes))
(25 (remq 6 codes))
(_ codes)))))
((or 3 4 9 10) (let ((r (mod new 10)))
(unless (= r 8)
(let (beg)
(while (and codes (/= q (/ (car codes) 10)))
(push (pop codes) beg))
(setq codes (nconc (nreverse beg) (cdr codes)))
(if (= r 9)
codes
(cons new codes))))))
(_ nil)))))
codes))