symbol plist accessor
[bpt/emacs.git] / lisp / ansi-color.el
CommitLineData
a5c581d8 1;;; ansi-color.el --- translate ANSI escape sequences into faces -*- lexical-binding: t -*-
618206ea 2
ba318903 3;; Copyright (C) 1999-2014 Free Software Foundation, Inc.
618206ea 4
8737bb5a
GM
5;; Author: Alex Schroeder <alex@gnu.org>
6;; Maintainer: Alex Schroeder <alex@gnu.org>
b3287acf
GM
7;; Version: 3.4.2
8;; Keywords: comm processes terminals services
618206ea
RS
9
10;; This file is part of GNU Emacs.
11
eb3fa2cf
GM
12;; GNU Emacs is free software: you can redistribute it and/or modify
13;; it under the terms of the GNU General Public License as published by
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
16
17;; GNU Emacs is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
618206ea 22;; You should have received a copy of the GNU General Public License
eb3fa2cf 23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
618206ea
RS
24
25;;; Commentary:
26
0e3c1e3e
GM
27;; This file provides a function that takes a string or a region
28;; containing Select Graphic Rendition (SGR) control sequences (formerly
29;; known as ANSI escape sequences) and tries to translate these into
30;; faces.
618206ea 31;;
89601c7b
CY
32;; This allows you to run ls --color=yes in shell-mode. It is now
33;; enabled by default; to disable it, set ansi-color-for-comint-mode
34;; to nil.
0e3c1e3e
GM
35;;
36;; Note that starting your shell from within Emacs might set the TERM
37;; environment variable. The new setting might disable the output of
38;; SGR control sequences. Using ls --color=yes forces ls to produce
39;; these.
40;;
986b7dee
GM
41;; SGR control sequences are defined in section 3.8.117 of the ECMA-48
42;; standard (identical to ISO/IEC 6429), which is freely available as a
73600831
XF
43;; PDF file <URL:http://www.ecma-international.org/publications/standards/Ecma-048.htm>.
44;; The "Graphic Rendition Combination Mode (GRCM)" implemented is
45;; "cumulative mode" as defined in section 7.2.8. Cumulative mode
46;; means that whenever possible, SGR control sequences are combined
47;; (ie. blue and bold).
618206ea 48
986b7dee
GM
49;; The basic functions are:
50;;
51;; `ansi-color-apply' to colorize a string containing SGR control
52;; sequences.
53;;
54;; `ansi-color-filter-apply' to filter SGR control sequences from a
55;; string.
618206ea 56;;
986b7dee
GM
57;; `ansi-color-apply-on-region' to colorize a region containing SGR
58;; control sequences.
59;;
60;; `ansi-color-filter-region' to filter SGR control sequences from a
61;; region.
62
986b7dee 63;;; Thanks
618206ea 64
986b7dee
GM
65;; Georges Brun-Cottan <gbruncot@emc.com> for improving ansi-color.el
66;; substantially by adding the code needed to cope with arbitrary chunks
67;; of output and the filter functions.
618206ea 68;;
986b7dee 69;; Markus Kuhn <Markus.Kuhn@cl.cam.ac.uk> for pointing me to ECMA-48.
0e3c1e3e 70;;
4c36be58 71;; Stefan Monnier <foo@acm.com> for explaining obscure font-lock stuff and for
0e3c1e3e 72;; code suggestions.
618206ea
RS
73
74\f
75
76;;; Code:
77
cd91462f
JB
78(defvar comint-last-output-start)
79
8737bb5a
GM
80;; Customization
81
986b7dee 82(defgroup ansi-colors nil
0e3c1e3e 83 "Translating SGR control sequences to faces.
986b7dee
GM
84This translation effectively colorizes strings and regions based upon
85SGR control sequences embedded in the text. SGR (Select Graphic
2f29c200 86Rendition) control sequences are defined in section 8.3.117 of the
a5c581d8
JB
87ECMA-48 standard (identical to ISO/IEC 6429), which is freely available
88at <URL:http://www.ecma-international.org/publications/standards/Ecma-048.htm>
89as a PDF file."
7c6c3d8e 90 :version "21.1"
986b7dee
GM
91 :group 'processes)
92
93(defcustom ansi-color-faces-vector
37269466 94 [default bold default italic underline success warning error]
986b7dee
GM
95 "Faces used for SGR control sequences determining a face.
96This vector holds the faces used for SGR control sequence parameters 0
97to 7.
618206ea 98
986b7dee
GM
99Parameter Description Face used by default
100 0 default default
101 1 bold bold
102 2 faint default
103 3 italic italic
104 4 underlined underline
37269466
CY
105 5 slowly blinking success
106 6 rapidly blinking warning
107 7 negative image error
618206ea 108
0e3c1e3e
GM
109Note that the symbol `default' is special: It will not be combined
110with the current face.
111
986b7dee
GM
112This vector is used by `ansi-color-make-color-map' to create a color
113map. This color map is stored in the variable `ansi-color-map'."
114 :type '(vector face face face face face face face face)
115 :set 'ansi-color-map-update
116 :initialize 'custom-initialize-default
117 :group 'ansi-colors)
118
119(defcustom ansi-color-names-vector
eb67db41 120 ["black" "red3" "green3" "yellow3" "blue2" "magenta3" "cyan3" "gray90"]
986b7dee
GM
121 "Colors used for SGR control sequences determining a color.
122This vector holds the colors used for SGR control sequences parameters
a5c581d8 12330 to 37 (foreground colors) and 40 to 47 (background colors).
986b7dee
GM
124
125Parameter Color
126 30 40 black
127 31 41 red
128 32 42 green
129 33 43 yellow
130 34 44 blue
131 35 45 magenta
132 36 46 cyan
133 37 47 white
134
135This vector is used by `ansi-color-make-color-map' to create a color
7a097943
LL
136map. This color map is stored in the variable `ansi-color-map'.
137
138Each element may also be a cons cell where the car and cdr specify the
139foreground and background colors, respectively."
140 :type '(vector (choice color (cons color color))
141 (choice color (cons color color))
142 (choice color (cons color color))
143 (choice color (cons color color))
144 (choice color (cons color color))
145 (choice color (cons color color))
146 (choice color (cons color color))
147 (choice color (cons color color)))
986b7dee
GM
148 :set 'ansi-color-map-update
149 :initialize 'custom-initialize-default
eb67db41 150 :version "24.4" ; default colors copied from `xterm-standard-colors'
986b7dee
GM
151 :group 'ansi-colors)
152
c21d4d1a 153(defconst ansi-color-regexp "\033\\[\\([0-9;]*m\\)"
986b7dee
GM
154 "Regexp that matches SGR control sequences.")
155
bc8d33d5 156(defconst ansi-color-drop-regexp
0283d6b0 157 "\033\\[\\([ABCDsuK]\\|[12][JK]\\|=[0-9]+[hI]\\|[0-9;]*[Hf]\\)"
bc8d33d5
CY
158 "Regexp that matches ANSI control sequences to silently drop.")
159
986b7dee
GM
160(defconst ansi-color-parameter-regexp "\\([0-9]*\\)[m;]"
161 "Regexp that matches SGR control sequence parameters.")
162
163
0e3c1e3e 164;; Convenience functions for comint modes (eg. shell-mode)
986b7dee
GM
165
166
925f8c70 167(defcustom ansi-color-for-comint-mode t
0e3c1e3e
GM
168 "Determines what to do with comint output.
169If nil, do nothing.
170If the symbol `filter', then filter all SGR control sequences.
171If anything else (such as t), then translate SGR control sequences
bc8d33d5 172into text properties.
986b7dee 173
0e3c1e3e
GM
174In order for this to have any effect, `ansi-color-process-output' must
175be in `comint-output-filter-functions'.
986b7dee 176
0e3c1e3e
GM
177This can be used to enable colorized ls --color=yes output
178in shell buffers. You set this variable by calling one of:
179\\[ansi-color-for-comint-mode-on]
180\\[ansi-color-for-comint-mode-off]
181\\[ansi-color-for-comint-mode-filter]"
0e3c1e3e
GM
182 :type '(choice (const :tag "Do nothing" nil)
183 (const :tag "Filter" filter)
184 (const :tag "Translate" t))
c2dae51b
CY
185 :group 'ansi-colors
186 :version "23.2")
986b7dee 187
0fd40f89
CY
188(defvar ansi-color-apply-face-function 'ansi-color-apply-overlay-face
189 "Function for applying an Ansi Color face to text in a buffer.
190This function should accept three arguments: BEG, END, and FACE,
191and it should apply face FACE to the text between BEG and END.")
192
58622cc5 193;;;###autoload
0e3c1e3e
GM
194(defun ansi-color-for-comint-mode-on ()
195 "Set `ansi-color-for-comint-mode' to t."
196 (interactive)
197 (setq ansi-color-for-comint-mode t))
198
199(defun ansi-color-for-comint-mode-off ()
200 "Set `ansi-color-for-comint-mode' to nil."
201 (interactive)
202 (setq ansi-color-for-comint-mode nil))
203
204(defun ansi-color-for-comint-mode-filter ()
205 "Set `ansi-color-for-comint-mode' to symbol `filter'."
206 (interactive)
207 (setq ansi-color-for-comint-mode 'filter))
208
58622cc5 209;;;###autoload
1b25a579 210(defun ansi-color-process-output (ignored)
bc8d33d5 211 "Maybe translate SGR control sequences of comint output into text properties.
0e3c1e3e
GM
212
213Depending on variable `ansi-color-for-comint-mode' the comint output is
214either not processed, SGR control sequences are filtered using
215`ansi-color-filter-region', or SGR control sequences are translated into
bc8d33d5 216text properties using `ansi-color-apply-on-region'.
0e3c1e3e
GM
217
218The comint output is assumed to lie between the marker
219`comint-last-output-start' and the process-mark.
220
221This is a good function to put in `comint-output-filter-functions'."
18f00515
CY
222 (let ((start-marker (if (and (markerp comint-last-output-start)
223 (eq (marker-buffer comint-last-output-start)
224 (current-buffer))
225 (marker-position comint-last-output-start))
226 comint-last-output-start
227 (point-min-marker)))
0e3c1e3e
GM
228 (end-marker (process-mark (get-buffer-process (current-buffer)))))
229 (cond ((eq ansi-color-for-comint-mode nil))
230 ((eq ansi-color-for-comint-mode 'filter)
231 (ansi-color-filter-region start-marker end-marker))
232 (t
233 (ansi-color-apply-on-region start-marker end-marker)))))
234
59f7af81
CY
235(define-obsolete-function-alias 'ansi-color-unfontify-region
236 'font-lock-default-unfontify-region "24.1")
0e3c1e3e
GM
237
238;; Working with strings
a5c581d8 239(defvar-local ansi-color-context nil
0e3c1e3e 240 "Context saved between two calls to `ansi-color-apply'.
2f29c200
WJ
241This is a list of the form (CODES FRAGMENT) or nil. CODES
242represents the state the last call to `ansi-color-apply' ended
243with, currently a list of ansi codes, and FRAGMENT is a string
244starting with an escape sequence, possibly the start of a new
0e3c1e3e 245escape sequence.")
0e3c1e3e
GM
246
247(defun ansi-color-filter-apply (string)
bc8d33d5 248 "Filter out all ANSI control sequences from STRING.
0e3c1e3e
GM
249
250Every call to this function will set and use the buffer-local variable
251`ansi-color-context' to save partial escape sequences. This information
252will be used for the next call to `ansi-color-apply'. Set
253`ansi-color-context' to nil if you don't want this.
618206ea 254
0e3c1e3e
GM
255This function can be added to `comint-preoutput-filter-functions'."
256 (let ((start 0) end result)
257 ;; if context was saved and is a string, prepend it
258 (if (cadr ansi-color-context)
259 (setq string (concat (cadr ansi-color-context) string)
260 ansi-color-context nil))
261 ;; find the next escape sequence
262 (while (setq end (string-match ansi-color-regexp string start))
263 (setq result (concat result (substring string start end))
264 start (match-end 0)))
265 ;; save context, add the remainder of the string to the result
266 (let (fragment)
267 (if (string-match "\033" string start)
268 (let ((pos (match-beginning 0)))
269 (setq fragment (substring string pos)
270 result (concat result (substring string start pos))))
271 (setq result (concat result (substring string start))))
ce327e48 272 (setq ansi-color-context (if fragment (list nil fragment))))
0e3c1e3e 273 result))
8737bb5a 274
2f29c200
WJ
275(defun ansi-color--find-face (codes)
276 "Return the face corresponding to CODES."
277 (let (faces)
278 (while codes
279 (let ((face (ansi-color-get-face-1 (pop codes))))
280 ;; In the (default underline) face, say, the value of the
281 ;; "underline" attribute of the `default' face wins.
282 (unless (eq face 'default)
283 (push face faces))))
284 ;; Avoid some long-lived conses in the common case.
285 (if (cdr faces)
286 (nreverse faces)
287 (car faces))))
288
8737bb5a 289(defun ansi-color-apply (string)
bc8d33d5
CY
290 "Translates SGR control sequences into text properties.
291Delete all other control sequences without processing them.
8737bb5a 292
986b7dee 293Applies SGR control sequences setting foreground and background colors
bc8d33d5 294to STRING using text properties and returns the result. The colors used
0e3c1e3e
GM
295are given in `ansi-color-faces-vector' and `ansi-color-names-vector'.
296See function `ansi-color-apply-sequence' for details.
297
298Every call to this function will set and use the buffer-local variable
2f29c200 299`ansi-color-context' to save partial escape sequences and current ansi codes.
0e3c1e3e
GM
300This information will be used for the next call to `ansi-color-apply'.
301Set `ansi-color-context' to nil if you don't want this.
302
ce327e48 303This function can be added to `comint-preoutput-filter-functions'."
2f29c200 304 (let ((codes (car ansi-color-context))
bc8d33d5
CY
305 (start 0) end escape-sequence result
306 colorized-substring)
307 ;; If context was saved and is a string, prepend it.
0e3c1e3e
GM
308 (if (cadr ansi-color-context)
309 (setq string (concat (cadr ansi-color-context) string)
310 ansi-color-context nil))
bc8d33d5 311 ;; Find the next escape sequence.
986b7dee 312 (while (setq end (string-match ansi-color-regexp string start))
0e3c1e3e 313 (setq escape-sequence (match-string 1 string))
bc8d33d5 314 ;; Colorize the old block from start to end using old face.
2f29c200
WJ
315 (when codes
316 (put-text-property start end 'font-lock-face (ansi-color--find-face codes) string))
bc8d33d5 317 (setq colorized-substring (substring string start end)
986b7dee 318 start (match-end 0))
bc8d33d5
CY
319 ;; Eliminate unrecognized ANSI sequences.
320 (while (string-match ansi-color-drop-regexp colorized-substring)
321 (setq colorized-substring
322 (replace-match "" nil nil colorized-substring)))
323 (push colorized-substring result)
324 ;; Create new face, by applying escape sequence parameters.
2f29c200 325 (setq codes (ansi-color-apply-sequence escape-sequence codes)))
0e3c1e3e 326 ;; if the rest of the string should have a face, put it there
2f29c200 327 (when codes
a5c581d8
JB
328 (put-text-property start (length string)
329 'font-lock-face (ansi-color--find-face codes) string))
0e3c1e3e
GM
330 ;; save context, add the remainder of the string to the result
331 (let (fragment)
332 (if (string-match "\033" string start)
333 (let ((pos (match-beginning 0)))
bc8d33d5
CY
334 (setq fragment (substring string pos))
335 (push (substring string start pos) result))
336 (push (substring string start) result))
2f29c200 337 (setq ansi-color-context (if (or codes fragment) (list codes fragment))))
bc8d33d5 338 (apply 'concat (nreverse result))))
0e3c1e3e
GM
339
340;; Working with regions
341
a5c581d8 342(defvar-local ansi-color-context-region nil
0e3c1e3e 343 "Context saved between two calls to `ansi-color-apply-on-region'.
2f29c200
WJ
344This is a list of the form (CODES MARKER) or nil. CODES
345represents the state the last call to `ansi-color-apply-on-region'
346ended with, currently a list of ansi codes, and MARKER is a
347buffer position within an escape sequence or the last position
348processed.")
8737bb5a 349
0e3c1e3e 350(defun ansi-color-filter-region (begin end)
bc8d33d5 351 "Filter out all ANSI control sequences from region BEGIN to END.
0e3c1e3e
GM
352
353Every call to this function will set and use the buffer-local variable
354`ansi-color-context-region' to save position. This information will be
355used for the next call to `ansi-color-apply-on-region'. Specifically,
356it will override BEGIN, the start of the region. Set
357`ansi-color-context-region' to nil if you don't want this."
358 (let ((end-marker (copy-marker end))
359 (start (or (cadr ansi-color-context-region) begin)))
360 (save-excursion
361 (goto-char start)
bc8d33d5
CY
362 ;; Delete unrecognized escape sequences.
363 (while (re-search-forward ansi-color-drop-regexp end-marker t)
364 (replace-match ""))
365 (goto-char start)
366 ;; Delete SGR escape sequences.
0e3c1e3e 367 (while (re-search-forward ansi-color-regexp end-marker t)
0e3c1e3e 368 (replace-match ""))
bc8d33d5
CY
369 ;; save context, add the remainder of the string to the result
370 (if (re-search-forward "\033" end-marker t)
371 (setq ansi-color-context-region (list nil (match-beginning 0)))
372 (setq ansi-color-context-region nil)))))
986b7dee 373
0e3c1e3e
GM
374(defun ansi-color-apply-on-region (begin end)
375 "Translates SGR control sequences into overlays or extents.
bc8d33d5 376Delete all other control sequences without processing them.
986b7dee 377
0e9e6c6a
CY
378SGR control sequences are applied by calling the function
379specified by `ansi-color-apply-face-function'. The default
380function sets foreground and background colors to the text
381between BEGIN and END, using overlays. The colors used are given
382in `ansi-color-faces-vector' and `ansi-color-names-vector'. See
383`ansi-color-apply-sequence' for details.
0e3c1e3e 384
2f29c200
WJ
385Every call to this function will set and use the buffer-local
386variable `ansi-color-context-region' to save position and current
387ansi codes. This information will be used for the next call to
388`ansi-color-apply-on-region'. Specifically, it will override
389BEGIN, the start of the region and set the face with which to
390start. Set `ansi-color-context-region' to nil if you don't want
391this."
392 (let ((codes (car ansi-color-context-region))
71296446 393 (start-marker (or (cadr ansi-color-context-region)
0e3c1e3e
GM
394 (copy-marker begin)))
395 (end-marker (copy-marker end))
396 escape-sequence)
bc8d33d5
CY
397 ;; First, eliminate unrecognized ANSI control sequences.
398 (save-excursion
399 (goto-char start-marker)
400 (while (re-search-forward ansi-color-drop-regexp end-marker t)
401 (replace-match "")))
986b7dee 402 (save-excursion
0e3c1e3e 403 (goto-char start-marker)
bc8d33d5 404 ;; Find the next SGR sequence.
0e3c1e3e 405 (while (re-search-forward ansi-color-regexp end-marker t)
bc8d33d5 406 ;; Colorize the old block from start to end using old face.
0fd40f89
CY
407 (funcall ansi-color-apply-face-function
408 start-marker (match-beginning 0)
2f29c200 409 (ansi-color--find-face codes))
0e3c1e3e
GM
410 ;; store escape sequence and new start position
411 (setq escape-sequence (match-string 1)
412 start-marker (copy-marker (match-end 0)))
413 ;; delete the escape sequence
414 (replace-match "")
2f29c200
WJ
415 ;; Update the list of ansi codes.
416 (setq codes (ansi-color-apply-sequence escape-sequence codes)))
0e3c1e3e
GM
417 ;; search for the possible start of a new escape sequence
418 (if (re-search-forward "\033" end-marker t)
419 (progn
420 ;; if the rest of the region should have a face, put it there
0fd40f89 421 (funcall ansi-color-apply-face-function
2f29c200
WJ
422 start-marker (point) (ansi-color--find-face codes))
423 ;; save codes and point
0e3c1e3e 424 (setq ansi-color-context-region
2f29c200 425 (list codes (copy-marker (match-beginning 0)))))
0e3c1e3e 426 ;; if the rest of the region should have a face, put it there
0fd40f89 427 (funcall ansi-color-apply-face-function
2f29c200
WJ
428 start-marker end-marker (ansi-color--find-face codes))
429 (setq ansi-color-context-region (if codes (list codes)))))))
0fd40f89
CY
430
431(defun ansi-color-apply-overlay-face (beg end face)
432 "Make an overlay from BEG to END, and apply face FACE.
433If FACE is nil, do nothing."
434 (when face
435 (ansi-color-set-extent-face
436 (ansi-color-make-extent beg end)
437 face)))
0e3c1e3e
GM
438
439;; This function helps you look for overlapping overlays. This is
fa463103 440;; useful in comint-buffers. Overlapping overlays should not happen!
0e3c1e3e
GM
441;; A possible cause for bugs are the markers. If you create an overlay
442;; up to the end of the region, then that end might coincide with the
443;; process-mark. As text is added BEFORE the process-mark, the overlay
444;; will keep growing. Therefore, as more overlays are created later on,
445;; there will be TWO OR MORE overlays covering the buffer at that point.
446;; This function helps you check your buffer for these situations.
447; (defun ansi-color-debug-overlays ()
448; (interactive)
449; (let ((pos (point-min)))
450; (while (< pos (point-max))
451; (if (<= 2 (length (overlays-at pos)))
452; (progn
453; (goto-char pos)
454; (error "%d overlays at %d" (length (overlays-at pos)) pos))
455; (let (message-log-max)
456; (message "Reached %d." pos)))
457; (setq pos (next-overlay-change pos)))))
458
459;; Emacs/XEmacs compatibility layer
460
461(defun ansi-color-make-face (property color)
462 "Return a face with PROPERTY set to COLOR.
71296446 463PROPERTY can be either symbol `foreground' or symbol `background'.
0e3c1e3e 464
a5c581d8 465For Emacs, we just return the cons cell (PROPERTY . COLOR).
0e3c1e3e
GM
466For XEmacs, we create a temporary face and return it."
467 (if (featurep 'xemacs)
468 (let ((face (make-face (intern (concat color "-" (symbol-name property)))
469 "Temporary face created by ansi-color."
470 t)))
471 (set-face-property face property color)
472 face)
473 (cond ((eq property 'foreground)
474 (cons 'foreground-color color))
475 ((eq property 'background)
476 (cons 'background-color color))
477 (t
478 (cons property color)))))
479
480(defun ansi-color-make-extent (from to &optional object)
481 "Make an extent for the range [FROM, TO) in OBJECT.
482
483OBJECT defaults to the current buffer. XEmacs uses `make-extent', Emacs
484uses `make-overlay'. XEmacs can use a buffer or a string for OBJECT,
485Emacs requires OBJECT to be a buffer."
c8f0dac9 486 (if (fboundp 'make-extent)
0e3c1e3e
GM
487 (make-extent from to object)
488 ;; In Emacs, the overlay might end at the process-mark in comint
489 ;; buffers. In that case, new text will be inserted before the
490 ;; process-mark, ie. inside the overlay (using insert-before-marks).
491 ;; In order to avoid this, we use the `insert-behind-hooks' overlay
492 ;; property to make sure it works.
493 (let ((overlay (make-overlay from to object)))
494 (overlay-put overlay 'modification-hooks '(ansi-color-freeze-overlay))
495 overlay)))
496
497(defun ansi-color-freeze-overlay (overlay is-after begin end &optional len)
498 "Prevent OVERLAY from being extended.
499This function can be used for the `modification-hooks' overlay
500property."
501 ;; if stuff was inserted at the end of the overlay
502 (when (and is-after
503 (= 0 len)
504 (= end (overlay-end overlay)))
505 ;; reset the end of the overlay
506 (move-overlay overlay (overlay-start overlay) begin)))
507
508(defun ansi-color-set-extent-face (extent face)
509 "Set the `face' property of EXTENT to FACE.
510XEmacs uses `set-extent-face', Emacs uses `overlay-put'."
a445370f 511 (if (featurep 'xemacs)
0e3c1e3e
GM
512 (set-extent-face extent face)
513 (overlay-put extent 'face face)))
986b7dee 514
8737bb5a
GM
515;; Helper functions
516
2f29c200
WJ
517(defsubst ansi-color-parse-sequence (escape-seq)
518 "Return the list of all the parameters in ESCAPE-SEQ.
0e3c1e3e 519
2f29c200
WJ
520ESCAPE-SEQ is a SGR control sequences such as \\033[34m. The parameter
52134 is used by `ansi-color-get-face-1' to return a face definition.
0e3c1e3e 522
2f29c200
WJ
523Returns nil only if there's no match for `ansi-color-parameter-regexp'."
524 (let ((i 0)
525 codes val)
526 (while (string-match ansi-color-parameter-regexp escape-seq i)
527 (setq i (match-end 0)
528 val (string-to-number (match-string 1 escape-seq) 10))
529 ;; It so happens that (string-to-number "") => 0.
530 (push val codes))
531 (nreverse codes)))
532
533(defun ansi-color-apply-sequence (escape-sequence codes)
a5c581d8 534 "Apply ESCAPE-SEQUENCE to CODES and return the new list of codes.
2f29c200 535
a5c581d8
JB
536ESCAPE-SEQUENCE is an escape sequence parsed by
537`ansi-color-parse-sequence'.
2f29c200 538
9db94175
WJ
539For each new code, the following happens: if it is 1-7, add it to
540the list of codes; if it is 21-25 or 27, delete appropriate
541parameters from the list of codes; if it is 30-37 resp. 39, the
542foreground color code is replaced or added resp. deleted; if it
543is 40-47 resp. 49, the background color code is replaced or added
544resp. deleted; any other code is discarded together with the old
545codes. Finally, the so changed list of codes is returned."
2f29c200
WJ
546 (let ((new-codes (ansi-color-parse-sequence escape-sequence)))
547 (while new-codes
9db94175
WJ
548 (let* ((new (pop new-codes))
549 (q (/ new 10)))
550 (setq codes
551 (pcase q
552 (0 (unless (memq new '(0 8 9))
553 (cons new (remq new codes))))
554 (2 (unless (memq new '(20 26 28 29))
555 ;; The standard says `21 doubly underlined' while
556 ;; http://en.wikipedia.org/wiki/ANSI_escape_code claims
557 ;; `21 Bright/Bold: off or Underline: Double'.
558 (remq (- new 20) (pcase new
559 (22 (remq 1 codes))
560 (25 (remq 6 codes))
561 (_ codes)))))
562 ((or 3 4) (let ((r (mod new 10)))
563 (unless (= r 8)
564 (let (beg)
565 (while (and codes (/= q (/ (car codes) 10)))
566 (push (pop codes) beg))
567 (setq codes (nconc (nreverse beg) (cdr codes)))
568 (if (= r 9)
569 codes
570 (cons new codes))))))
571 (_ nil)))))
2f29c200 572 codes))
0e3c1e3e 573
986b7dee
GM
574(defun ansi-color-make-color-map ()
575 "Creates a vector of face definitions and returns it.
576
577The index into the vector is an ANSI code. See the documentation of
578`ansi-color-map' for an example.
579
580The face definitions are based upon the variables
581`ansi-color-faces-vector' and `ansi-color-names-vector'."
a5c581d8 582 (let ((map (make-vector 50 nil))
986b7dee
GM
583 (index 0))
584 ;; miscellaneous attributes
c04c01ca 585 (mapc
986b7dee 586 (function (lambda (e)
a5c581d8 587 (aset map index e)
986b7dee
GM
588 (setq index (1+ index)) ))
589 ansi-color-faces-vector)
986b7dee
GM
590 ;; foreground attributes
591 (setq index 30)
c04c01ca 592 (mapc
986b7dee 593 (function (lambda (e)
a5c581d8 594 (aset map index
7a097943
LL
595 (ansi-color-make-face 'foreground
596 (if (consp e) (car e) e)))
986b7dee
GM
597 (setq index (1+ index)) ))
598 ansi-color-names-vector)
986b7dee
GM
599 ;; background attributes
600 (setq index 40)
c04c01ca 601 (mapc
986b7dee 602 (function (lambda (e)
a5c581d8 603 (aset map index
7a097943
LL
604 (ansi-color-make-face 'background
605 (if (consp e) (cdr e) e)))
986b7dee
GM
606 (setq index (1+ index)) ))
607 ansi-color-names-vector)
a5c581d8 608 map))
986b7dee
GM
609
610(defvar ansi-color-map (ansi-color-make-color-map)
0e3c1e3e 611 "A brand new color map suitable for `ansi-color-get-face'.
986b7dee
GM
612
613The value of this variable is usually constructed by
614`ansi-color-make-color-map'. The values in the array are such that the
615numbers included in an SGR control sequences point to the correct
616foreground or background colors.
617
a5c581d8 618Example: The sequence \\033[34m specifies a blue foreground. Therefore:
986b7dee 619 (aref ansi-color-map 34)
a5c581d8 620 => (foreground-color . \"blue\")")
986b7dee
GM
621
622(defun ansi-color-map-update (symbol value)
623 "Update `ansi-color-map'.
624
625Whenever the vectors used to construct `ansi-color-map' are changed,
626this function is called. Therefore this function is listed as the :set
627property of `ansi-color-faces-vector' and `ansi-color-names-vector'."
628 (set-default symbol value)
629 (setq ansi-color-map (ansi-color-make-color-map)))
630
631(defun ansi-color-get-face-1 (ansi-code)
632 "Get face definition from `ansi-color-map'.
633ANSI-CODE is used as an index into the vector."
634 (condition-case nil
635 (aref ansi-color-map ansi-code)
74f24ba7 636 (args-out-of-range nil)))
986b7dee 637
618206ea
RS
638(provide 'ansi-color)
639
8737bb5a 640;;; ansi-color.el ends here