dired-mark-extension: Unmark if called with C-u prefix
[emacs.git] / lisp / ansi-color.el
blob788a7bde4cad80c5260adff95cfb6adb4ce9bebb
1 ;;; ansi-color.el --- translate ANSI escape sequences into faces -*- lexical-binding: t -*-
3 ;; Copyright (C) 1999-2016 Free Software Foundation, Inc.
5 ;; Author: Alex Schroeder <alex@gnu.org>
6 ;; Maintainer: Alex Schroeder <alex@gnu.org>
7 ;; Version: 3.4.2
8 ;; Keywords: comm processes terminals services
10 ;; This file is part of GNU Emacs.
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.
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.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
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.
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.
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.
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-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).
49 ;; The basic functions are:
51 ;; `ansi-color-apply' to colorize a string containing SGR control
52 ;; sequences.
54 ;; `ansi-color-filter-apply' to filter SGR control sequences from a
55 ;; string.
57 ;; `ansi-color-apply-on-region' to colorize a region containing SGR
58 ;; control sequences.
60 ;; `ansi-color-filter-region' to filter SGR control sequences from a
61 ;; region.
63 ;;; Thanks
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.
69 ;; Markus Kuhn <Markus.Kuhn@cl.cam.ac.uk> for pointing me to ECMA-48.
71 ;; Stefan Monnier <foo@acm.com> for explaining obscure font-lock stuff and for
72 ;; code suggestions.
76 ;;; Code:
78 (defvar comint-last-output-start)
80 ;; Customization
82 (defgroup ansi-colors nil
83 "Translating SGR control sequences to faces.
84 This translation effectively colorizes strings and regions based upon
85 SGR control sequences embedded in the text. SGR (Select Graphic
86 Rendition) control sequences are defined in section 8.3.117 of the
87 ECMA-48 standard (identical to ISO/IEC 6429), which is freely available
88 at <URL:http://www.ecma-international.org/publications/standards/Ecma-048.htm>
89 as a PDF file."
90 :version "21.1"
91 :group 'processes)
93 (defcustom ansi-color-faces-vector
94 [default bold default italic underline success warning error]
95 "Faces used for SGR control sequences determining a face.
96 This vector holds the faces used for SGR control sequence parameters 0
97 to 7.
99 Parameter 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
105 5 slowly blinking success
106 6 rapidly blinking warning
107 7 negative image error
109 Note that the symbol `default' is special: It will not be combined
110 with the current face.
112 This vector is used by `ansi-color-make-color-map' to create a color
113 map. 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)
119 (defcustom ansi-color-names-vector
120 ["black" "red3" "green3" "yellow3" "blue2" "magenta3" "cyan3" "gray90"]
121 "Colors used for SGR control sequences determining a color.
122 This vector holds the colors used for SGR control sequences parameters
123 30 to 37 (foreground colors) and 40 to 47 (background colors).
125 Parameter 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
135 This vector is used by `ansi-color-make-color-map' to create a color
136 map. This color map is stored in the variable `ansi-color-map'.
138 Each element may also be a cons cell where the car and cdr specify the
139 foreground 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)))
148 :set 'ansi-color-map-update
149 :initialize 'custom-initialize-default
150 :version "24.4" ; default colors copied from `xterm-standard-colors'
151 :group 'ansi-colors)
153 (defconst ansi-color-regexp "\033\\[\\([0-9;]*m\\)"
154 "Regexp that matches SGR control sequences.")
156 (defconst ansi-color-drop-regexp
157 "\033\\[\\([ABCDsuK]\\|[12][JK]\\|=[0-9]+[hI]\\|[0-9;]*[Hf]\\|\\?[0-9]+[hl]\\)"
158 "Regexp that matches ANSI control sequences to silently drop.")
160 (defconst ansi-color-parameter-regexp "\\([0-9]*\\)[m;]"
161 "Regexp that matches SGR control sequence parameters.")
164 ;; Convenience functions for comint modes (eg. shell-mode)
167 (defcustom ansi-color-for-comint-mode t
168 "Determines what to do with comint output.
169 If nil, do nothing.
170 If the symbol `filter', then filter all SGR control sequences.
171 If anything else (such as t), then translate SGR control sequences
172 into text properties.
174 In order for this to have any effect, `ansi-color-process-output' must
175 be in `comint-output-filter-functions'.
177 This can be used to enable colorized ls --color=yes output
178 in 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]"
182 :type '(choice (const :tag "Do nothing" nil)
183 (const :tag "Filter" filter)
184 (const :tag "Translate" t))
185 :group 'ansi-colors
186 :version "23.2")
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.
190 This function should accept three arguments: BEG, END, and FACE,
191 and it should apply face FACE to the text between BEG and END.")
193 ;;;###autoload
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))
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))
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))
209 ;;;###autoload
210 (defun ansi-color-process-output (ignored)
211 "Maybe translate SGR control sequences of comint output into text properties.
213 Depending on variable `ansi-color-for-comint-mode' the comint output is
214 either not processed, SGR control sequences are filtered using
215 `ansi-color-filter-region', or SGR control sequences are translated into
216 text properties using `ansi-color-apply-on-region'.
218 The comint output is assumed to lie between the marker
219 `comint-last-output-start' and the process-mark.
221 This is a good function to put in `comint-output-filter-functions'."
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)))
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))
233 (ansi-color-apply-on-region start-marker end-marker)))))
235 (define-obsolete-function-alias 'ansi-color-unfontify-region
236 'font-lock-default-unfontify-region "24.1")
238 ;; Working with strings
239 (defvar-local ansi-color-context nil
240 "Context saved between two calls to `ansi-color-apply'.
241 This is a list of the form (CODES FRAGMENT) or nil. CODES
242 represents the state the last call to `ansi-color-apply' ended
243 with, currently a list of ansi codes, and FRAGMENT is a string
244 starting with an escape sequence, possibly the start of a new
245 escape sequence.")
247 (defun ansi-color-filter-apply (string)
248 "Filter out all ANSI control sequences from STRING.
250 Every call to this function will set and use the buffer-local variable
251 `ansi-color-context' to save partial escape sequences. This information
252 will be used for the next call to `ansi-color-apply'. Set
253 `ansi-color-context' to nil if you don't want this.
255 This 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 ;; eliminate unrecognized escape sequences
266 (while (string-match ansi-color-drop-regexp string)
267 (setq string
268 (replace-match "" nil nil string)))
269 ;; save context, add the remainder of the string to the result
270 (let (fragment)
271 (if (string-match "\033" string start)
272 (let ((pos (match-beginning 0)))
273 (setq fragment (substring string pos)
274 result (concat result (substring string start pos))))
275 (setq result (concat result (substring string start))))
276 (setq ansi-color-context (if fragment (list nil fragment))))
277 result))
279 (defun ansi-color--find-face (codes)
280 "Return the face corresponding to CODES."
281 (let (faces)
282 (while codes
283 (let ((face (ansi-color-get-face-1 (pop codes))))
284 ;; In the (default underline) face, say, the value of the
285 ;; "underline" attribute of the `default' face wins.
286 (unless (eq face 'default)
287 (push face faces))))
288 ;; Avoid some long-lived conses in the common case.
289 (if (cdr faces)
290 (nreverse faces)
291 (car faces))))
293 (defun ansi-color-apply (string)
294 "Translates SGR control sequences into text properties.
295 Delete all other control sequences without processing them.
297 Applies SGR control sequences setting foreground and background colors
298 to STRING using text properties and returns the result. The colors used
299 are given in `ansi-color-faces-vector' and `ansi-color-names-vector'.
300 See function `ansi-color-apply-sequence' for details.
302 Every call to this function will set and use the buffer-local variable
303 `ansi-color-context' to save partial escape sequences and current ansi codes.
304 This information will be used for the next call to `ansi-color-apply'.
305 Set `ansi-color-context' to nil if you don't want this.
307 This function can be added to `comint-preoutput-filter-functions'."
308 (let ((codes (car ansi-color-context))
309 (start 0) end escape-sequence result
310 colorized-substring)
311 ;; If context was saved and is a string, prepend it.
312 (if (cadr ansi-color-context)
313 (setq string (concat (cadr ansi-color-context) string)
314 ansi-color-context nil))
315 ;; Find the next escape sequence.
316 (while (setq end (string-match ansi-color-regexp string start))
317 (setq escape-sequence (match-string 1 string))
318 ;; Colorize the old block from start to end using old face.
319 (when codes
320 (put-text-property start end 'font-lock-face (ansi-color--find-face codes) string))
321 (setq colorized-substring (substring string start end)
322 start (match-end 0))
323 ;; Eliminate unrecognized ANSI sequences.
324 (while (string-match ansi-color-drop-regexp colorized-substring)
325 (setq colorized-substring
326 (replace-match "" nil nil colorized-substring)))
327 (push colorized-substring result)
328 ;; Create new face, by applying escape sequence parameters.
329 (setq codes (ansi-color-apply-sequence escape-sequence codes)))
330 ;; if the rest of the string should have a face, put it there
331 (when codes
332 (put-text-property start (length string)
333 'font-lock-face (ansi-color--find-face codes) string))
334 ;; eliminate unrecognized escape sequences
335 (while (string-match ansi-color-drop-regexp string)
336 (setq string
337 (replace-match "" nil nil string)))
338 ;; save context, add the remainder of the string to the result
339 (let (fragment)
340 (if (string-match "\033" string start)
341 (let ((pos (match-beginning 0)))
342 (setq fragment (substring string pos))
343 (push (substring string start pos) result))
344 (push (substring string start) result))
345 (setq ansi-color-context (if (or codes fragment) (list codes fragment))))
346 (apply 'concat (nreverse result))))
348 ;; Working with regions
350 (defvar-local ansi-color-context-region nil
351 "Context saved between two calls to `ansi-color-apply-on-region'.
352 This is a list of the form (CODES MARKER) or nil. CODES
353 represents the state the last call to `ansi-color-apply-on-region'
354 ended with, currently a list of ansi codes, and MARKER is a
355 buffer position within an escape sequence or the last position
356 processed.")
358 (defun ansi-color-filter-region (begin end)
359 "Filter out all ANSI control sequences from region BEGIN to END.
361 Every call to this function will set and use the buffer-local variable
362 `ansi-color-context-region' to save position. This information will be
363 used for the next call to `ansi-color-apply-on-region'. Specifically,
364 it will override BEGIN, the start of the region. Set
365 `ansi-color-context-region' to nil if you don't want this."
366 (let ((end-marker (copy-marker end))
367 (start (or (cadr ansi-color-context-region) begin)))
368 (save-excursion
369 (goto-char start)
370 ;; Delete unrecognized escape sequences.
371 (while (re-search-forward ansi-color-drop-regexp end-marker t)
372 (replace-match ""))
373 (goto-char start)
374 ;; Delete SGR escape sequences.
375 (while (re-search-forward ansi-color-regexp end-marker t)
376 (replace-match ""))
377 ;; save context, add the remainder of the string to the result
378 (if (re-search-forward "\033" end-marker t)
379 (setq ansi-color-context-region (list nil (match-beginning 0)))
380 (setq ansi-color-context-region nil)))))
382 (defun ansi-color-apply-on-region (begin end)
383 "Translates SGR control sequences into overlays or extents.
384 Delete all other control sequences without processing them.
386 SGR control sequences are applied by calling the function
387 specified by `ansi-color-apply-face-function'. The default
388 function sets foreground and background colors to the text
389 between BEGIN and END, using overlays. The colors used are given
390 in `ansi-color-faces-vector' and `ansi-color-names-vector'. See
391 `ansi-color-apply-sequence' for details.
393 Every call to this function will set and use the buffer-local
394 variable `ansi-color-context-region' to save position and current
395 ansi codes. This information will be used for the next call to
396 `ansi-color-apply-on-region'. Specifically, it will override
397 BEGIN, the start of the region and set the face with which to
398 start. Set `ansi-color-context-region' to nil if you don't want
399 this."
400 (let ((codes (car ansi-color-context-region))
401 (start-marker (or (cadr ansi-color-context-region)
402 (copy-marker begin)))
403 (end-marker (copy-marker end))
404 escape-sequence)
405 ;; First, eliminate unrecognized ANSI control sequences.
406 (save-excursion
407 (goto-char start-marker)
408 (while (re-search-forward ansi-color-drop-regexp end-marker t)
409 (replace-match "")))
410 (save-excursion
411 (goto-char start-marker)
412 ;; Find the next SGR sequence.
413 (while (re-search-forward ansi-color-regexp end-marker t)
414 ;; Colorize the old block from start to end using old face.
415 (funcall ansi-color-apply-face-function
416 start-marker (match-beginning 0)
417 (ansi-color--find-face codes))
418 ;; store escape sequence and new start position
419 (setq escape-sequence (match-string 1)
420 start-marker (copy-marker (match-end 0)))
421 ;; delete the escape sequence
422 (replace-match "")
423 ;; Update the list of ansi codes.
424 (setq codes (ansi-color-apply-sequence escape-sequence codes)))
425 ;; search for the possible start of a new escape sequence
426 (if (re-search-forward "\033" end-marker t)
427 (progn
428 ;; if the rest of the region should have a face, put it there
429 (funcall ansi-color-apply-face-function
430 start-marker (point) (ansi-color--find-face codes))
431 ;; save codes and point
432 (setq ansi-color-context-region
433 (list codes (copy-marker (match-beginning 0)))))
434 ;; if the rest of the region should have a face, put it there
435 (funcall ansi-color-apply-face-function
436 start-marker end-marker (ansi-color--find-face codes))
437 (setq ansi-color-context-region (if codes (list codes)))))))
439 (defun ansi-color-apply-overlay-face (beg end face)
440 "Make an overlay from BEG to END, and apply face FACE.
441 If FACE is nil, do nothing."
442 (when face
443 (ansi-color-set-extent-face
444 (ansi-color-make-extent beg end)
445 face)))
447 ;; This function helps you look for overlapping overlays. This is
448 ;; useful in comint-buffers. Overlapping overlays should not happen!
449 ;; A possible cause for bugs are the markers. If you create an overlay
450 ;; up to the end of the region, then that end might coincide with the
451 ;; process-mark. As text is added BEFORE the process-mark, the overlay
452 ;; will keep growing. Therefore, as more overlays are created later on,
453 ;; there will be TWO OR MORE overlays covering the buffer at that point.
454 ;; This function helps you check your buffer for these situations.
455 ; (defun ansi-color-debug-overlays ()
456 ; (interactive)
457 ; (let ((pos (point-min)))
458 ; (while (< pos (point-max))
459 ; (if (<= 2 (length (overlays-at pos)))
460 ; (progn
461 ; (goto-char pos)
462 ; (error "%d overlays at %d" (length (overlays-at pos)) pos))
463 ; (let (message-log-max)
464 ; (message "Reached %d." pos)))
465 ; (setq pos (next-overlay-change pos)))))
467 ;; Emacs/XEmacs compatibility layer
469 (defun ansi-color-make-face (property color)
470 "Return a face with PROPERTY set to COLOR.
471 PROPERTY can be either symbol `foreground' or symbol `background'.
473 For Emacs, we just return the cons cell (PROPERTY . COLOR).
474 For XEmacs, we create a temporary face and return it."
475 (if (featurep 'xemacs)
476 (let ((face (make-face (intern (concat color "-" (symbol-name property)))
477 "Temporary face created by ansi-color."
478 t)))
479 (set-face-property face property color)
480 face)
481 (cond ((eq property 'foreground)
482 (cons 'foreground-color color))
483 ((eq property 'background)
484 (cons 'background-color color))
486 (cons property color)))))
488 (defun ansi-color-make-extent (from to &optional object)
489 "Make an extent for the range [FROM, TO) in OBJECT.
491 OBJECT defaults to the current buffer. XEmacs uses `make-extent', Emacs
492 uses `make-overlay'. XEmacs can use a buffer or a string for OBJECT,
493 Emacs requires OBJECT to be a buffer."
494 (if (fboundp 'make-extent)
495 (make-extent from to object)
496 ;; In Emacs, the overlay might end at the process-mark in comint
497 ;; buffers. In that case, new text will be inserted before the
498 ;; process-mark, ie. inside the overlay (using insert-before-marks).
499 ;; In order to avoid this, we use the `insert-behind-hooks' overlay
500 ;; property to make sure it works.
501 (let ((overlay (make-overlay from to object)))
502 (overlay-put overlay 'modification-hooks '(ansi-color-freeze-overlay))
503 overlay)))
505 (defun ansi-color-freeze-overlay (overlay is-after begin end &optional len)
506 "Prevent OVERLAY from being extended.
507 This function can be used for the `modification-hooks' overlay
508 property."
509 ;; if stuff was inserted at the end of the overlay
510 (when (and is-after
511 (= 0 len)
512 (= end (overlay-end overlay)))
513 ;; reset the end of the overlay
514 (move-overlay overlay (overlay-start overlay) begin)))
516 (defun ansi-color-set-extent-face (extent face)
517 "Set the `face' property of EXTENT to FACE.
518 XEmacs uses `set-extent-face', Emacs uses `overlay-put'."
519 (if (featurep 'xemacs)
520 (set-extent-face extent face)
521 (overlay-put extent 'face face)))
523 ;; Helper functions
525 (defsubst ansi-color-parse-sequence (escape-seq)
526 "Return the list of all the parameters in ESCAPE-SEQ.
528 ESCAPE-SEQ is a SGR control sequences such as \\033[34m. The parameter
529 34 is used by `ansi-color-get-face-1' to return a face definition.
531 Returns nil only if there's no match for `ansi-color-parameter-regexp'."
532 (let ((i 0)
533 codes val)
534 (while (string-match ansi-color-parameter-regexp escape-seq i)
535 (setq i (match-end 0)
536 val (string-to-number (match-string 1 escape-seq) 10))
537 ;; It so happens that (string-to-number "") => 0.
538 (push val codes))
539 (nreverse codes)))
541 (defun ansi-color-apply-sequence (escape-sequence codes)
542 "Apply ESCAPE-SEQUENCE to CODES and return the new list of codes.
544 ESCAPE-SEQUENCE is an escape sequence parsed by
545 `ansi-color-parse-sequence'.
547 For each new code, the following happens: if it is 1-7, add it to
548 the list of codes; if it is 21-25 or 27, delete appropriate
549 parameters from the list of codes; if it is 30-37 resp. 39, the
550 foreground color code is replaced or added resp. deleted; if it
551 is 40-47 resp. 49, the background color code is replaced or added
552 resp. deleted; any other code is discarded together with the old
553 codes. Finally, the so changed list of codes is returned."
554 (let ((new-codes (ansi-color-parse-sequence escape-sequence)))
555 (while new-codes
556 (let* ((new (pop new-codes))
557 (q (/ new 10)))
558 (setq codes
559 (pcase q
560 (0 (unless (memq new '(0 8 9))
561 (cons new (remq new codes))))
562 (2 (unless (memq new '(20 26 28 29))
563 ;; The standard says `21 doubly underlined' while
564 ;; http://en.wikipedia.org/wiki/ANSI_escape_code claims
565 ;; `21 Bright/Bold: off or Underline: Double'.
566 (remq (- new 20) (pcase new
567 (22 (remq 1 codes))
568 (25 (remq 6 codes))
569 (_ codes)))))
570 ((or 3 4) (let ((r (mod new 10)))
571 (unless (= r 8)
572 (let (beg)
573 (while (and codes (/= q (/ (car codes) 10)))
574 (push (pop codes) beg))
575 (setq codes (nconc (nreverse beg) (cdr codes)))
576 (if (= r 9)
577 codes
578 (cons new codes))))))
579 (_ nil)))))
580 codes))
582 (defun ansi-color-make-color-map ()
583 "Creates a vector of face definitions and returns it.
585 The index into the vector is an ANSI code. See the documentation of
586 `ansi-color-map' for an example.
588 The face definitions are based upon the variables
589 `ansi-color-faces-vector' and `ansi-color-names-vector'."
590 (let ((map (make-vector 50 nil))
591 (index 0))
592 ;; miscellaneous attributes
593 (mapc
594 (function (lambda (e)
595 (aset map index e)
596 (setq index (1+ index)) ))
597 ansi-color-faces-vector)
598 ;; foreground attributes
599 (setq index 30)
600 (mapc
601 (function (lambda (e)
602 (aset map index
603 (ansi-color-make-face 'foreground
604 (if (consp e) (car e) e)))
605 (setq index (1+ index)) ))
606 ansi-color-names-vector)
607 ;; background attributes
608 (setq index 40)
609 (mapc
610 (function (lambda (e)
611 (aset map index
612 (ansi-color-make-face 'background
613 (if (consp e) (cdr e) e)))
614 (setq index (1+ index)) ))
615 ansi-color-names-vector)
616 map))
618 (defvar ansi-color-map (ansi-color-make-color-map)
619 "A brand new color map suitable for `ansi-color-get-face'.
621 The value of this variable is usually constructed by
622 `ansi-color-make-color-map'. The values in the array are such that the
623 numbers included in an SGR control sequences point to the correct
624 foreground or background colors.
626 Example: The sequence \\033[34m specifies a blue foreground. Therefore:
627 (aref ansi-color-map 34)
628 => (foreground-color . \"blue\")")
630 (defun ansi-color-map-update (symbol value)
631 "Update `ansi-color-map'.
633 Whenever the vectors used to construct `ansi-color-map' are changed,
634 this function is called. Therefore this function is listed as the :set
635 property of `ansi-color-faces-vector' and `ansi-color-names-vector'."
636 (set-default symbol value)
637 (setq ansi-color-map (ansi-color-make-color-map)))
639 (defun ansi-color-get-face-1 (ansi-code)
640 "Get face definition from `ansi-color-map'.
641 ANSI-CODE is used as an index into the vector."
642 (condition-case nil
643 (aref ansi-color-map ansi-code)
644 (args-out-of-range nil)))
646 (provide 'ansi-color)
648 ;;; ansi-color.el ends here