emacs-bzr-get-version tweak
[bpt/emacs.git] / lisp / ansi-color.el
CommitLineData
0e3c1e3e 1;;; ansi-color.el --- translate ANSI escape sequences into faces
618206ea 2
acaf905b 3;; Copyright (C) 1999-2012 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
43;; PDF file <URL:http://www.ecma.ch/ecma1/STAND/ECMA-048.HTM>. The
44;; "Graphic Rendition Combination Mode (GRCM)" implemented is
45;; "cumulative mode" as defined in section 7.2.8. Cumulative mode means
46;; that whenever possible, SGR control sequences are combined (ie. blue
47;; 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
86Rendition) control sequences are defined in section 3.8.117 of the
87ECMA-48 standard \(identical to ISO/IEC 6429), which is freely available
88as a PDF file <URL:http://www.ecma.ch/ecma1/STAND/ECMA-048.HTM>."
7c6c3d8e 89 :version "21.1"
986b7dee
GM
90 :group 'processes)
91
92(defcustom ansi-color-faces-vector
93 [default bold default italic underline bold bold-italic modeline]
94 "Faces used for SGR control sequences determining a face.
95This vector holds the faces used for SGR control sequence parameters 0
96to 7.
618206ea 97
986b7dee
GM
98Parameter Description Face used by default
99 0 default default
100 1 bold bold
101 2 faint default
102 3 italic italic
103 4 underlined underline
104 5 slowly blinking bold
105 6 rapidly blinking bold-italic
106 7 negative image modeline
618206ea 107
0e3c1e3e
GM
108Note that the symbol `default' is special: It will not be combined
109with the current face.
110
986b7dee
GM
111This vector is used by `ansi-color-make-color-map' to create a color
112map. This color map is stored in the variable `ansi-color-map'."
113 :type '(vector face face face face face face face face)
114 :set 'ansi-color-map-update
115 :initialize 'custom-initialize-default
116 :group 'ansi-colors)
117
118(defcustom ansi-color-names-vector
618206ea 119 ["black" "red" "green" "yellow" "blue" "magenta" "cyan" "white"]
986b7dee
GM
120 "Colors used for SGR control sequences determining a color.
121This vector holds the colors used for SGR control sequences parameters
12230 to 37 \(foreground colors) and 40 to 47 (background colors).
123
124Parameter Color
125 30 40 black
126 31 41 red
127 32 42 green
128 33 43 yellow
129 34 44 blue
130 35 45 magenta
131 36 46 cyan
132 37 47 white
133
134This vector is used by `ansi-color-make-color-map' to create a color
7a097943
LL
135map. This color map is stored in the variable `ansi-color-map'.
136
137Each element may also be a cons cell where the car and cdr specify the
138foreground and background colors, respectively."
139 :type '(vector (choice color (cons color color))
140 (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)))
986b7dee
GM
147 :set 'ansi-color-map-update
148 :initialize 'custom-initialize-default
149 :group 'ansi-colors)
150
c21d4d1a 151(defconst ansi-color-regexp "\033\\[\\([0-9;]*m\\)"
986b7dee
GM
152 "Regexp that matches SGR control sequences.")
153
bc8d33d5
CY
154(defconst ansi-color-drop-regexp
155 "\033\\[\\([ABCDsuK]\\|2J\\|=[0-9]+[hI]\\|[0-9;]*[Hf]\\)"
156 "Regexp that matches ANSI control sequences to silently drop.")
157
986b7dee
GM
158(defconst ansi-color-parameter-regexp "\\([0-9]*\\)[m;]"
159 "Regexp that matches SGR control sequence parameters.")
160
161
0e3c1e3e 162;; Convenience functions for comint modes (eg. shell-mode)
986b7dee
GM
163
164
925f8c70 165(defcustom ansi-color-for-comint-mode t
0e3c1e3e
GM
166 "Determines what to do with comint output.
167If nil, do nothing.
168If the symbol `filter', then filter all SGR control sequences.
169If anything else (such as t), then translate SGR control sequences
bc8d33d5 170into text properties.
986b7dee 171
0e3c1e3e
GM
172In order for this to have any effect, `ansi-color-process-output' must
173be in `comint-output-filter-functions'.
986b7dee 174
0e3c1e3e
GM
175This can be used to enable colorized ls --color=yes output
176in shell buffers. You set this variable by calling one of:
177\\[ansi-color-for-comint-mode-on]
178\\[ansi-color-for-comint-mode-off]
179\\[ansi-color-for-comint-mode-filter]"
0e3c1e3e
GM
180 :type '(choice (const :tag "Do nothing" nil)
181 (const :tag "Filter" filter)
182 (const :tag "Translate" t))
c2dae51b
CY
183 :group 'ansi-colors
184 :version "23.2")
986b7dee 185
0fd40f89
CY
186(defvar ansi-color-apply-face-function 'ansi-color-apply-overlay-face
187 "Function for applying an Ansi Color face to text in a buffer.
188This function should accept three arguments: BEG, END, and FACE,
189and it should apply face FACE to the text between BEG and END.")
190
58622cc5 191;;;###autoload
0e3c1e3e
GM
192(defun ansi-color-for-comint-mode-on ()
193 "Set `ansi-color-for-comint-mode' to t."
194 (interactive)
195 (setq ansi-color-for-comint-mode t))
196
197(defun ansi-color-for-comint-mode-off ()
198 "Set `ansi-color-for-comint-mode' to nil."
199 (interactive)
200 (setq ansi-color-for-comint-mode nil))
201
202(defun ansi-color-for-comint-mode-filter ()
203 "Set `ansi-color-for-comint-mode' to symbol `filter'."
204 (interactive)
205 (setq ansi-color-for-comint-mode 'filter))
206
58622cc5 207;;;###autoload
1b25a579 208(defun ansi-color-process-output (ignored)
bc8d33d5 209 "Maybe translate SGR control sequences of comint output into text properties.
0e3c1e3e
GM
210
211Depending on variable `ansi-color-for-comint-mode' the comint output is
212either not processed, SGR control sequences are filtered using
213`ansi-color-filter-region', or SGR control sequences are translated into
bc8d33d5 214text properties using `ansi-color-apply-on-region'.
0e3c1e3e
GM
215
216The comint output is assumed to lie between the marker
217`comint-last-output-start' and the process-mark.
218
219This is a good function to put in `comint-output-filter-functions'."
220 (let ((start-marker (or comint-last-output-start
221 (point-min-marker)))
222 (end-marker (process-mark (get-buffer-process (current-buffer)))))
223 (cond ((eq ansi-color-for-comint-mode nil))
224 ((eq ansi-color-for-comint-mode 'filter)
225 (ansi-color-filter-region start-marker end-marker))
226 (t
227 (ansi-color-apply-on-region start-marker end-marker)))))
228
ce327e48
SM
229(defalias 'ansi-color-unfontify-region 'font-lock-default-unfontify-region)
230(make-obsolete 'ansi-color-unfontify-region "not needed any more" "24.1")
0e3c1e3e
GM
231
232;; Working with strings
0e3c1e3e
GM
233(defvar ansi-color-context nil
234 "Context saved between two calls to `ansi-color-apply'.
235This is a list of the form (FACES FRAGMENT) or nil. FACES is a list of
236faces the last call to `ansi-color-apply' ended with, and FRAGMENT is a
237string starting with an escape sequence, possibly the start of a new
238escape sequence.")
239(make-variable-buffer-local 'ansi-color-context)
240
241(defun ansi-color-filter-apply (string)
bc8d33d5 242 "Filter out all ANSI control sequences from STRING.
0e3c1e3e
GM
243
244Every call to this function will set and use the buffer-local variable
245`ansi-color-context' to save partial escape sequences. This information
246will be used for the next call to `ansi-color-apply'. Set
247`ansi-color-context' to nil if you don't want this.
618206ea 248
0e3c1e3e
GM
249This function can be added to `comint-preoutput-filter-functions'."
250 (let ((start 0) end result)
251 ;; if context was saved and is a string, prepend it
252 (if (cadr ansi-color-context)
253 (setq string (concat (cadr ansi-color-context) string)
254 ansi-color-context nil))
255 ;; find the next escape sequence
256 (while (setq end (string-match ansi-color-regexp string start))
257 (setq result (concat result (substring string start end))
258 start (match-end 0)))
259 ;; save context, add the remainder of the string to the result
260 (let (fragment)
261 (if (string-match "\033" string start)
262 (let ((pos (match-beginning 0)))
263 (setq fragment (substring string pos)
264 result (concat result (substring string start pos))))
265 (setq result (concat result (substring string start))))
ce327e48 266 (setq ansi-color-context (if fragment (list nil fragment))))
0e3c1e3e 267 result))
8737bb5a
GM
268
269(defun ansi-color-apply (string)
bc8d33d5
CY
270 "Translates SGR control sequences into text properties.
271Delete all other control sequences without processing them.
8737bb5a 272
986b7dee 273Applies SGR control sequences setting foreground and background colors
bc8d33d5 274to STRING using text properties and returns the result. The colors used
0e3c1e3e
GM
275are given in `ansi-color-faces-vector' and `ansi-color-names-vector'.
276See function `ansi-color-apply-sequence' for details.
277
278Every call to this function will set and use the buffer-local variable
279`ansi-color-context' to save partial escape sequences and current face.
280This information will be used for the next call to `ansi-color-apply'.
281Set `ansi-color-context' to nil if you don't want this.
282
ce327e48 283This function can be added to `comint-preoutput-filter-functions'."
0e3c1e3e 284 (let ((face (car ansi-color-context))
bc8d33d5
CY
285 (start 0) end escape-sequence result
286 colorized-substring)
287 ;; If context was saved and is a string, prepend it.
0e3c1e3e
GM
288 (if (cadr ansi-color-context)
289 (setq string (concat (cadr ansi-color-context) string)
290 ansi-color-context nil))
bc8d33d5 291 ;; Find the next escape sequence.
986b7dee 292 (while (setq end (string-match ansi-color-regexp string start))
0e3c1e3e 293 (setq escape-sequence (match-string 1 string))
bc8d33d5 294 ;; Colorize the old block from start to end using old face.
0e3c1e3e 295 (when face
ce327e48 296 (put-text-property start end 'font-lock-face face string))
bc8d33d5 297 (setq colorized-substring (substring string start end)
986b7dee 298 start (match-end 0))
bc8d33d5
CY
299 ;; Eliminate unrecognized ANSI sequences.
300 (while (string-match ansi-color-drop-regexp colorized-substring)
301 (setq colorized-substring
302 (replace-match "" nil nil colorized-substring)))
303 (push colorized-substring result)
304 ;; Create new face, by applying escape sequence parameters.
0e3c1e3e
GM
305 (setq face (ansi-color-apply-sequence escape-sequence face)))
306 ;; if the rest of the string should have a face, put it there
307 (when face
ce327e48 308 (put-text-property start (length string) 'font-lock-face face string))
0e3c1e3e
GM
309 ;; save context, add the remainder of the string to the result
310 (let (fragment)
311 (if (string-match "\033" string start)
312 (let ((pos (match-beginning 0)))
bc8d33d5
CY
313 (setq fragment (substring string pos))
314 (push (substring string start pos) result))
315 (push (substring string start) result))
ce327e48 316 (setq ansi-color-context (if (or face fragment) (list face fragment))))
bc8d33d5 317 (apply 'concat (nreverse result))))
0e3c1e3e
GM
318
319;; Working with regions
320
321(defvar ansi-color-context-region nil
322 "Context saved between two calls to `ansi-color-apply-on-region'.
323This is a list of the form (FACES MARKER) or nil. FACES is a list of
324faces the last call to `ansi-color-apply-on-region' ended with, and
325MARKER is a buffer position within an escape sequence or the last
326position processed.")
327(make-variable-buffer-local 'ansi-color-context-region)
8737bb5a 328
0e3c1e3e 329(defun ansi-color-filter-region (begin end)
bc8d33d5 330 "Filter out all ANSI control sequences from region BEGIN to END.
0e3c1e3e
GM
331
332Every call to this function will set and use the buffer-local variable
333`ansi-color-context-region' to save position. This information will be
334used for the next call to `ansi-color-apply-on-region'. Specifically,
335it will override BEGIN, the start of the region. Set
336`ansi-color-context-region' to nil if you don't want this."
337 (let ((end-marker (copy-marker end))
338 (start (or (cadr ansi-color-context-region) begin)))
339 (save-excursion
340 (goto-char start)
bc8d33d5
CY
341 ;; Delete unrecognized escape sequences.
342 (while (re-search-forward ansi-color-drop-regexp end-marker t)
343 (replace-match ""))
344 (goto-char start)
345 ;; Delete SGR escape sequences.
0e3c1e3e 346 (while (re-search-forward ansi-color-regexp end-marker t)
0e3c1e3e 347 (replace-match ""))
bc8d33d5
CY
348 ;; save context, add the remainder of the string to the result
349 (if (re-search-forward "\033" end-marker t)
350 (setq ansi-color-context-region (list nil (match-beginning 0)))
351 (setq ansi-color-context-region nil)))))
986b7dee 352
0e3c1e3e
GM
353(defun ansi-color-apply-on-region (begin end)
354 "Translates SGR control sequences into overlays or extents.
bc8d33d5 355Delete all other control sequences without processing them.
986b7dee 356
bc8d33d5
CY
357SGR control sequences are applied by setting foreground and
358background colors to the text between BEGIN and END using
359overlays. The colors used are given in `ansi-color-faces-vector'
360and `ansi-color-names-vector'. See `ansi-color-apply-sequence'
361for details.
0e3c1e3e
GM
362
363Every call to this function will set and use the buffer-local variable
364`ansi-color-context-region' to save position and current face. This
365information will be used for the next call to
366`ansi-color-apply-on-region'. Specifically, it will override BEGIN, the
367start of the region and set the face with which to start. Set
368`ansi-color-context-region' to nil if you don't want this."
369 (let ((face (car ansi-color-context-region))
71296446 370 (start-marker (or (cadr ansi-color-context-region)
0e3c1e3e
GM
371 (copy-marker begin)))
372 (end-marker (copy-marker end))
373 escape-sequence)
bc8d33d5
CY
374 ;; First, eliminate unrecognized ANSI control sequences.
375 (save-excursion
376 (goto-char start-marker)
377 (while (re-search-forward ansi-color-drop-regexp end-marker t)
378 (replace-match "")))
986b7dee 379 (save-excursion
0e3c1e3e 380 (goto-char start-marker)
bc8d33d5 381 ;; Find the next SGR sequence.
0e3c1e3e 382 (while (re-search-forward ansi-color-regexp end-marker t)
bc8d33d5 383 ;; Colorize the old block from start to end using old face.
0fd40f89
CY
384 (funcall ansi-color-apply-face-function
385 start-marker (match-beginning 0)
386 face)
0e3c1e3e
GM
387 ;; store escape sequence and new start position
388 (setq escape-sequence (match-string 1)
389 start-marker (copy-marker (match-end 0)))
390 ;; delete the escape sequence
391 (replace-match "")
392 ;; create new face by applying all the parameters in the escape
393 ;; sequence
394 (setq face (ansi-color-apply-sequence escape-sequence face)))
395 ;; search for the possible start of a new escape sequence
396 (if (re-search-forward "\033" end-marker t)
397 (progn
398 ;; if the rest of the region should have a face, put it there
0fd40f89
CY
399 (funcall ansi-color-apply-face-function
400 start-marker (point) face)
0e3c1e3e
GM
401 ;; save face and point
402 (setq ansi-color-context-region
403 (list face (copy-marker (match-beginning 0)))))
404 ;; if the rest of the region should have a face, put it there
0fd40f89
CY
405 (funcall ansi-color-apply-face-function
406 start-marker end-marker face)
407 (setq ansi-color-context-region (if face (list face)))))))
408
409(defun ansi-color-apply-overlay-face (beg end face)
410 "Make an overlay from BEG to END, and apply face FACE.
411If FACE is nil, do nothing."
412 (when face
413 (ansi-color-set-extent-face
414 (ansi-color-make-extent beg end)
415 face)))
0e3c1e3e
GM
416
417;; This function helps you look for overlapping overlays. This is
fa463103 418;; useful in comint-buffers. Overlapping overlays should not happen!
0e3c1e3e
GM
419;; A possible cause for bugs are the markers. If you create an overlay
420;; up to the end of the region, then that end might coincide with the
421;; process-mark. As text is added BEFORE the process-mark, the overlay
422;; will keep growing. Therefore, as more overlays are created later on,
423;; there will be TWO OR MORE overlays covering the buffer at that point.
424;; This function helps you check your buffer for these situations.
425; (defun ansi-color-debug-overlays ()
426; (interactive)
427; (let ((pos (point-min)))
428; (while (< pos (point-max))
429; (if (<= 2 (length (overlays-at pos)))
430; (progn
431; (goto-char pos)
432; (error "%d overlays at %d" (length (overlays-at pos)) pos))
433; (let (message-log-max)
434; (message "Reached %d." pos)))
435; (setq pos (next-overlay-change pos)))))
436
437;; Emacs/XEmacs compatibility layer
438
439(defun ansi-color-make-face (property color)
440 "Return a face with PROPERTY set to COLOR.
71296446 441PROPERTY can be either symbol `foreground' or symbol `background'.
0e3c1e3e
GM
442
443For Emacs, we just return the cons cell \(PROPERTY . COLOR).
444For XEmacs, we create a temporary face and return it."
445 (if (featurep 'xemacs)
446 (let ((face (make-face (intern (concat color "-" (symbol-name property)))
447 "Temporary face created by ansi-color."
448 t)))
449 (set-face-property face property color)
450 face)
451 (cond ((eq property 'foreground)
452 (cons 'foreground-color color))
453 ((eq property 'background)
454 (cons 'background-color color))
455 (t
456 (cons property color)))))
457
458(defun ansi-color-make-extent (from to &optional object)
459 "Make an extent for the range [FROM, TO) in OBJECT.
460
461OBJECT defaults to the current buffer. XEmacs uses `make-extent', Emacs
462uses `make-overlay'. XEmacs can use a buffer or a string for OBJECT,
463Emacs requires OBJECT to be a buffer."
c8f0dac9 464 (if (fboundp 'make-extent)
0e3c1e3e
GM
465 (make-extent from to object)
466 ;; In Emacs, the overlay might end at the process-mark in comint
467 ;; buffers. In that case, new text will be inserted before the
468 ;; process-mark, ie. inside the overlay (using insert-before-marks).
469 ;; In order to avoid this, we use the `insert-behind-hooks' overlay
470 ;; property to make sure it works.
471 (let ((overlay (make-overlay from to object)))
472 (overlay-put overlay 'modification-hooks '(ansi-color-freeze-overlay))
473 overlay)))
474
475(defun ansi-color-freeze-overlay (overlay is-after begin end &optional len)
476 "Prevent OVERLAY from being extended.
477This function can be used for the `modification-hooks' overlay
478property."
479 ;; if stuff was inserted at the end of the overlay
480 (when (and is-after
481 (= 0 len)
482 (= end (overlay-end overlay)))
483 ;; reset the end of the overlay
484 (move-overlay overlay (overlay-start overlay) begin)))
485
486(defun ansi-color-set-extent-face (extent face)
487 "Set the `face' property of EXTENT to FACE.
488XEmacs uses `set-extent-face', Emacs uses `overlay-put'."
a445370f 489 (if (featurep 'xemacs)
0e3c1e3e
GM
490 (set-extent-face extent face)
491 (overlay-put extent 'face face)))
986b7dee 492
8737bb5a
GM
493;; Helper functions
494
0e3c1e3e
GM
495(defun ansi-color-apply-sequence (escape-sequence faces)
496 "Apply ESCAPE-SEQ to FACES and return the new list of faces.
497
498ESCAPE-SEQ is an escape sequences parsed by `ansi-color-get-face'.
499
500If the new faces start with the symbol `default', then the new
501faces are returned. If the faces start with something else,
502they are appended to the front of the FACES list, and the new
503list of faces is returned.
504
505If `ansi-color-get-face' returns nil, then we either got a
506null-sequence, or we stumbled upon some garbage. In either
507case we return nil."
508 (let ((new-faces (ansi-color-get-face escape-sequence)))
509 (cond ((null new-faces)
510 nil)
511 ((eq (car new-faces) 'default)
512 (cdr new-faces))
513 (t
7f0986e3
RS
514 ;; Like (append NEW-FACES FACES)
515 ;; but delete duplicates in FACES.
516 (let ((modified-faces (copy-sequence faces)))
517 (dolist (face (nreverse new-faces))
518 (setq modified-faces (delete face modified-faces))
519 (push face modified-faces))
520 modified-faces)))))
0e3c1e3e 521
986b7dee
GM
522(defun ansi-color-make-color-map ()
523 "Creates a vector of face definitions and returns it.
524
525The index into the vector is an ANSI code. See the documentation of
526`ansi-color-map' for an example.
527
528The face definitions are based upon the variables
529`ansi-color-faces-vector' and `ansi-color-names-vector'."
530 (let ((ansi-color-map (make-vector 50 nil))
531 (index 0))
532 ;; miscellaneous attributes
c04c01ca 533 (mapc
986b7dee
GM
534 (function (lambda (e)
535 (aset ansi-color-map index e)
536 (setq index (1+ index)) ))
537 ansi-color-faces-vector)
986b7dee
GM
538 ;; foreground attributes
539 (setq index 30)
c04c01ca 540 (mapc
986b7dee
GM
541 (function (lambda (e)
542 (aset ansi-color-map index
7a097943
LL
543 (ansi-color-make-face 'foreground
544 (if (consp e) (car e) e)))
986b7dee
GM
545 (setq index (1+ index)) ))
546 ansi-color-names-vector)
986b7dee
GM
547 ;; background attributes
548 (setq index 40)
c04c01ca 549 (mapc
986b7dee
GM
550 (function (lambda (e)
551 (aset ansi-color-map index
7a097943
LL
552 (ansi-color-make-face 'background
553 (if (consp e) (cdr e) e)))
986b7dee
GM
554 (setq index (1+ index)) ))
555 ansi-color-names-vector)
556 ansi-color-map))
557
558(defvar ansi-color-map (ansi-color-make-color-map)
0e3c1e3e 559 "A brand new color map suitable for `ansi-color-get-face'.
986b7dee
GM
560
561The value of this variable is usually constructed by
562`ansi-color-make-color-map'. The values in the array are such that the
563numbers included in an SGR control sequences point to the correct
564foreground or background colors.
565
566Example: The sequence \033[34m specifies a blue foreground. Therefore:
567 (aref ansi-color-map 34)
568 => \(foreground-color . \"blue\")")
569
570(defun ansi-color-map-update (symbol value)
571 "Update `ansi-color-map'.
572
573Whenever the vectors used to construct `ansi-color-map' are changed,
574this function is called. Therefore this function is listed as the :set
575property of `ansi-color-faces-vector' and `ansi-color-names-vector'."
576 (set-default symbol value)
577 (setq ansi-color-map (ansi-color-make-color-map)))
578
579(defun ansi-color-get-face-1 (ansi-code)
580 "Get face definition from `ansi-color-map'.
581ANSI-CODE is used as an index into the vector."
582 (condition-case nil
583 (aref ansi-color-map ansi-code)
74f24ba7 584 (args-out-of-range nil)))
986b7dee
GM
585
586(defun ansi-color-get-face (escape-seq)
587 "Create a new face by applying all the parameters in ESCAPE-SEQ.
588
0e3c1e3e 589Should any of the parameters result in the default face (usually this is
301b181a 590the parameter 0), then the effect of all previous parameters is canceled.
0e3c1e3e
GM
591
592ESCAPE-SEQ is a SGR control sequences such as \\033[34m. The parameter
986b7dee 59334 is used by `ansi-color-get-face-1' to return a face definition."
c21d4d1a 594 (let ((i 0)
0e3c1e3e 595 f val)
c21d4d1a 596 (while (string-match ansi-color-parameter-regexp escape-seq i)
0e3c1e3e
GM
597 (setq i (match-end 0)
598 val (ansi-color-get-face-1
c21d4d1a 599 (string-to-number (match-string 1 escape-seq) 10)))
0e3c1e3e
GM
600 (cond ((not val))
601 ((eq val 'default)
602 (setq f (list val)))
603 (t
7f0986e3
RS
604 (unless (member val f)
605 (push val f)))))
986b7dee 606 f))
618206ea
RS
607
608(provide 'ansi-color)
609
8737bb5a 610;;; ansi-color.el ends here