Merge from emacs-23 branch, up to 2010-05-20T21:33:58Z!juri@jurta.org.
[emacs.git] / lisp / ansi-color.el
blob8321edb21b1e83ab29ee83e44adfee866e043e20
1 ;;; ansi-color.el --- translate ANSI escape sequences into faces
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
6 ;; Author: Alex Schroeder <alex@gnu.org>
7 ;; Maintainer: Alex Schroeder <alex@gnu.org>
8 ;; Version: 3.4.2
9 ;; Keywords: comm processes terminals services
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;;; Commentary:
28 ;; This file provides a function that takes a string or a region
29 ;; containing Select Graphic Rendition (SGR) control sequences (formerly
30 ;; known as ANSI escape sequences) and tries to translate these into
31 ;; faces.
33 ;; This allows you to run ls --color=yes in shell-mode. It is now
34 ;; enabled by default; to disable it, set ansi-color-for-comint-mode
35 ;; to nil.
37 ;; Note that starting your shell from within Emacs might set the TERM
38 ;; environment variable. The new setting might disable the output of
39 ;; SGR control sequences. Using ls --color=yes forces ls to produce
40 ;; these.
42 ;; SGR control sequences are defined in section 3.8.117 of the ECMA-48
43 ;; standard (identical to ISO/IEC 6429), which is freely available as a
44 ;; PDF file <URL:http://www.ecma.ch/ecma1/STAND/ECMA-048.HTM>. The
45 ;; "Graphic Rendition Combination Mode (GRCM)" implemented is
46 ;; "cumulative mode" as defined in section 7.2.8. Cumulative mode means
47 ;; that whenever possible, SGR control sequences are combined (ie. blue
48 ;; and bold).
50 ;; The basic functions are:
52 ;; `ansi-color-apply' to colorize a string containing SGR control
53 ;; sequences.
55 ;; `ansi-color-filter-apply' to filter SGR control sequences from a
56 ;; string.
58 ;; `ansi-color-apply-on-region' to colorize a region containing SGR
59 ;; control sequences.
61 ;; `ansi-color-filter-region' to filter SGR control sequences from a
62 ;; region.
64 ;;; Thanks
66 ;; Georges Brun-Cottan <gbruncot@emc.com> for improving ansi-color.el
67 ;; substantially by adding the code needed to cope with arbitrary chunks
68 ;; of output and the filter functions.
70 ;; Markus Kuhn <Markus.Kuhn@cl.cam.ac.uk> for pointing me to ECMA-48.
72 ;; Stefan Monnier <foo@acm.com> explaing obscure font-lock stuff and
73 ;; code suggestions.
77 ;;; Code:
79 (defvar comint-last-output-start)
81 ;; Customization
83 (defgroup ansi-colors nil
84 "Translating SGR control sequences to faces.
85 This translation effectively colorizes strings and regions based upon
86 SGR control sequences embedded in the text. SGR (Select Graphic
87 Rendition) control sequences are defined in section 3.8.117 of the
88 ECMA-48 standard \(identical to ISO/IEC 6429), which is freely available
89 as a PDF file <URL:http://www.ecma.ch/ecma1/STAND/ECMA-048.HTM>."
90 :version "21.1"
91 :group 'processes)
93 (defcustom ansi-color-faces-vector
94 [default bold default italic underline bold bold-italic modeline]
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 bold
106 6 rapidly blinking bold-italic
107 7 negative image modeline
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" "red" "green" "yellow" "blue" "magenta" "cyan" "white"]
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'."
137 :type '(vector string string string string string string string string)
138 :set 'ansi-color-map-update
139 :initialize 'custom-initialize-default
140 :group 'ansi-colors)
142 (defconst ansi-color-regexp "\033\\[\\([0-9;]*m\\)"
143 "Regexp that matches SGR control sequences.")
145 (defconst ansi-color-drop-regexp
146 "\033\\[\\([ABCDsuK]\\|2J\\|=[0-9]+[hI]\\|[0-9;]*[Hf]\\)"
147 "Regexp that matches ANSI control sequences to silently drop.")
149 (defconst ansi-color-parameter-regexp "\\([0-9]*\\)[m;]"
150 "Regexp that matches SGR control sequence parameters.")
153 ;; Convenience functions for comint modes (eg. shell-mode)
156 (defcustom ansi-color-for-comint-mode t
157 "Determines what to do with comint output.
158 If nil, do nothing.
159 If the symbol `filter', then filter all SGR control sequences.
160 If anything else (such as t), then translate SGR control sequences
161 into text properties.
163 In order for this to have any effect, `ansi-color-process-output' must
164 be in `comint-output-filter-functions'.
166 This can be used to enable colorized ls --color=yes output
167 in shell buffers. You set this variable by calling one of:
168 \\[ansi-color-for-comint-mode-on]
169 \\[ansi-color-for-comint-mode-off]
170 \\[ansi-color-for-comint-mode-filter]"
171 :type '(choice (const :tag "Do nothing" nil)
172 (const :tag "Filter" filter)
173 (const :tag "Translate" t))
174 :group 'ansi-colors
175 :version "23.2")
177 ;;;###autoload
178 (defun ansi-color-for-comint-mode-on ()
179 "Set `ansi-color-for-comint-mode' to t."
180 (interactive)
181 (setq ansi-color-for-comint-mode t))
183 (defun ansi-color-for-comint-mode-off ()
184 "Set `ansi-color-for-comint-mode' to nil."
185 (interactive)
186 (setq ansi-color-for-comint-mode nil))
188 (defun ansi-color-for-comint-mode-filter ()
189 "Set `ansi-color-for-comint-mode' to symbol `filter'."
190 (interactive)
191 (setq ansi-color-for-comint-mode 'filter))
193 ;;;###autoload
194 (defun ansi-color-process-output (ignored)
195 "Maybe translate SGR control sequences of comint output into text properties.
197 Depending on variable `ansi-color-for-comint-mode' the comint output is
198 either not processed, SGR control sequences are filtered using
199 `ansi-color-filter-region', or SGR control sequences are translated into
200 text properties using `ansi-color-apply-on-region'.
202 The comint output is assumed to lie between the marker
203 `comint-last-output-start' and the process-mark.
205 This is a good function to put in `comint-output-filter-functions'."
206 (let ((start-marker (or comint-last-output-start
207 (point-min-marker)))
208 (end-marker (process-mark (get-buffer-process (current-buffer)))))
209 (cond ((eq ansi-color-for-comint-mode nil))
210 ((eq ansi-color-for-comint-mode 'filter)
211 (ansi-color-filter-region start-marker end-marker))
213 (ansi-color-apply-on-region start-marker end-marker)))))
215 (add-hook 'comint-output-filter-functions
216 'ansi-color-process-output)
218 (defalias 'ansi-color-unfontify-region 'font-lock-default-unfontify-region)
219 (make-obsolete 'ansi-color-unfontify-region "not needed any more" "24.1")
221 ;; Working with strings
222 (defvar ansi-color-context nil
223 "Context saved between two calls to `ansi-color-apply'.
224 This is a list of the form (FACES FRAGMENT) or nil. FACES is a list of
225 faces the last call to `ansi-color-apply' ended with, and FRAGMENT is a
226 string starting with an escape sequence, possibly the start of a new
227 escape sequence.")
228 (make-variable-buffer-local 'ansi-color-context)
230 (defun ansi-color-filter-apply (string)
231 "Filter out all ANSI control sequences from STRING.
233 Every call to this function will set and use the buffer-local variable
234 `ansi-color-context' to save partial escape sequences. This information
235 will be used for the next call to `ansi-color-apply'. Set
236 `ansi-color-context' to nil if you don't want this.
238 This function can be added to `comint-preoutput-filter-functions'."
239 (let ((start 0) end result)
240 ;; if context was saved and is a string, prepend it
241 (if (cadr ansi-color-context)
242 (setq string (concat (cadr ansi-color-context) string)
243 ansi-color-context nil))
244 ;; find the next escape sequence
245 (while (setq end (string-match ansi-color-regexp string start))
246 (setq result (concat result (substring string start end))
247 start (match-end 0)))
248 ;; save context, add the remainder of the string to the result
249 (let (fragment)
250 (if (string-match "\033" string start)
251 (let ((pos (match-beginning 0)))
252 (setq fragment (substring string pos)
253 result (concat result (substring string start pos))))
254 (setq result (concat result (substring string start))))
255 (setq ansi-color-context (if fragment (list nil fragment))))
256 result))
258 (defun ansi-color-apply (string)
259 "Translates SGR control sequences into text properties.
260 Delete all other control sequences without processing them.
262 Applies SGR control sequences setting foreground and background colors
263 to STRING using text properties and returns the result. The colors used
264 are given in `ansi-color-faces-vector' and `ansi-color-names-vector'.
265 See function `ansi-color-apply-sequence' for details.
267 Every call to this function will set and use the buffer-local variable
268 `ansi-color-context' to save partial escape sequences and current face.
269 This information will be used for the next call to `ansi-color-apply'.
270 Set `ansi-color-context' to nil if you don't want this.
272 This function can be added to `comint-preoutput-filter-functions'."
273 (let ((face (car ansi-color-context))
274 (start 0) end escape-sequence result
275 colorized-substring)
276 ;; If context was saved and is a string, prepend it.
277 (if (cadr ansi-color-context)
278 (setq string (concat (cadr ansi-color-context) string)
279 ansi-color-context nil))
280 ;; Find the next escape sequence.
281 (while (setq end (string-match ansi-color-regexp string start))
282 (setq escape-sequence (match-string 1 string))
283 ;; Colorize the old block from start to end using old face.
284 (when face
285 (put-text-property start end 'font-lock-face face string))
286 (setq colorized-substring (substring string start end)
287 start (match-end 0))
288 ;; Eliminate unrecognized ANSI sequences.
289 (while (string-match ansi-color-drop-regexp colorized-substring)
290 (setq colorized-substring
291 (replace-match "" nil nil colorized-substring)))
292 (push colorized-substring result)
293 ;; Create new face, by applying escape sequence parameters.
294 (setq face (ansi-color-apply-sequence escape-sequence face)))
295 ;; if the rest of the string should have a face, put it there
296 (when face
297 (put-text-property start (length string) 'font-lock-face face string))
298 ;; save context, add the remainder of the string to the result
299 (let (fragment)
300 (if (string-match "\033" string start)
301 (let ((pos (match-beginning 0)))
302 (setq fragment (substring string pos))
303 (push (substring string start pos) result))
304 (push (substring string start) result))
305 (setq ansi-color-context (if (or face fragment) (list face fragment))))
306 (apply 'concat (nreverse result))))
308 ;; Working with regions
310 (defvar ansi-color-context-region nil
311 "Context saved between two calls to `ansi-color-apply-on-region'.
312 This is a list of the form (FACES MARKER) or nil. FACES is a list of
313 faces the last call to `ansi-color-apply-on-region' ended with, and
314 MARKER is a buffer position within an escape sequence or the last
315 position processed.")
316 (make-variable-buffer-local 'ansi-color-context-region)
318 (defun ansi-color-filter-region (begin end)
319 "Filter out all ANSI control sequences from region BEGIN to END.
321 Every call to this function will set and use the buffer-local variable
322 `ansi-color-context-region' to save position. This information will be
323 used for the next call to `ansi-color-apply-on-region'. Specifically,
324 it will override BEGIN, the start of the region. Set
325 `ansi-color-context-region' to nil if you don't want this."
326 (let ((end-marker (copy-marker end))
327 (start (or (cadr ansi-color-context-region) begin)))
328 (save-excursion
329 (goto-char start)
330 ;; Delete unrecognized escape sequences.
331 (while (re-search-forward ansi-color-drop-regexp end-marker t)
332 (replace-match ""))
333 (goto-char start)
334 ;; Delete SGR escape sequences.
335 (while (re-search-forward ansi-color-regexp end-marker t)
336 (replace-match ""))
337 ;; save context, add the remainder of the string to the result
338 (if (re-search-forward "\033" end-marker t)
339 (setq ansi-color-context-region (list nil (match-beginning 0)))
340 (setq ansi-color-context-region nil)))))
342 (defun ansi-color-apply-on-region (begin end)
343 "Translates SGR control sequences into overlays or extents.
344 Delete all other control sequences without processing them.
346 SGR control sequences are applied by setting foreground and
347 background colors to the text between BEGIN and END using
348 overlays. The colors used are given in `ansi-color-faces-vector'
349 and `ansi-color-names-vector'. See `ansi-color-apply-sequence'
350 for details.
352 Every call to this function will set and use the buffer-local variable
353 `ansi-color-context-region' to save position and current face. This
354 information will be used for the next call to
355 `ansi-color-apply-on-region'. Specifically, it will override BEGIN, the
356 start of the region and set the face with which to start. Set
357 `ansi-color-context-region' to nil if you don't want this."
358 (let ((face (car ansi-color-context-region))
359 (start-marker (or (cadr ansi-color-context-region)
360 (copy-marker begin)))
361 (end-marker (copy-marker end))
362 escape-sequence)
363 ;; First, eliminate unrecognized ANSI control sequences.
364 (save-excursion
365 (goto-char start-marker)
366 (while (re-search-forward ansi-color-drop-regexp end-marker t)
367 (replace-match "")))
368 (save-excursion
369 (goto-char start-marker)
370 ;; Find the next SGR sequence.
371 (while (re-search-forward ansi-color-regexp end-marker t)
372 ;; Colorize the old block from start to end using old face.
373 (when face
374 (ansi-color-set-extent-face
375 (ansi-color-make-extent start-marker (match-beginning 0))
376 face))
377 ;; store escape sequence and new start position
378 (setq escape-sequence (match-string 1)
379 start-marker (copy-marker (match-end 0)))
380 ;; delete the escape sequence
381 (replace-match "")
382 ;; create new face by applying all the parameters in the escape
383 ;; sequence
384 (setq face (ansi-color-apply-sequence escape-sequence face)))
385 ;; search for the possible start of a new escape sequence
386 (if (re-search-forward "\033" end-marker t)
387 (progn
388 ;; if the rest of the region should have a face, put it there
389 (when face
390 (ansi-color-set-extent-face
391 (ansi-color-make-extent start-marker (point))
392 face))
393 ;; save face and point
394 (setq ansi-color-context-region
395 (list face (copy-marker (match-beginning 0)))))
396 ;; if the rest of the region should have a face, put it there
397 (if face
398 (progn
399 (ansi-color-set-extent-face
400 (ansi-color-make-extent start-marker end-marker)
401 face)
402 (setq ansi-color-context-region (list face)))
403 ;; reset context
404 (setq ansi-color-context-region nil))))))
406 ;; This function helps you look for overlapping overlays. This is
407 ;; usefull in comint-buffers. Overlapping overlays should not happen!
408 ;; A possible cause for bugs are the markers. If you create an overlay
409 ;; up to the end of the region, then that end might coincide with the
410 ;; process-mark. As text is added BEFORE the process-mark, the overlay
411 ;; will keep growing. Therefore, as more overlays are created later on,
412 ;; there will be TWO OR MORE overlays covering the buffer at that point.
413 ;; This function helps you check your buffer for these situations.
414 ; (defun ansi-color-debug-overlays ()
415 ; (interactive)
416 ; (let ((pos (point-min)))
417 ; (while (< pos (point-max))
418 ; (if (<= 2 (length (overlays-at pos)))
419 ; (progn
420 ; (goto-char pos)
421 ; (error "%d overlays at %d" (length (overlays-at pos)) pos))
422 ; (let (message-log-max)
423 ; (message "Reached %d." pos)))
424 ; (setq pos (next-overlay-change pos)))))
426 ;; Emacs/XEmacs compatibility layer
428 (defun ansi-color-make-face (property color)
429 "Return a face with PROPERTY set to COLOR.
430 PROPERTY can be either symbol `foreground' or symbol `background'.
432 For Emacs, we just return the cons cell \(PROPERTY . COLOR).
433 For XEmacs, we create a temporary face and return it."
434 (if (featurep 'xemacs)
435 (let ((face (make-face (intern (concat color "-" (symbol-name property)))
436 "Temporary face created by ansi-color."
437 t)))
438 (set-face-property face property color)
439 face)
440 (cond ((eq property 'foreground)
441 (cons 'foreground-color color))
442 ((eq property 'background)
443 (cons 'background-color color))
445 (cons property color)))))
447 (defun ansi-color-make-extent (from to &optional object)
448 "Make an extent for the range [FROM, TO) in OBJECT.
450 OBJECT defaults to the current buffer. XEmacs uses `make-extent', Emacs
451 uses `make-overlay'. XEmacs can use a buffer or a string for OBJECT,
452 Emacs requires OBJECT to be a buffer."
453 (if (fboundp 'make-extent)
454 (make-extent from to object)
455 ;; In Emacs, the overlay might end at the process-mark in comint
456 ;; buffers. In that case, new text will be inserted before the
457 ;; process-mark, ie. inside the overlay (using insert-before-marks).
458 ;; In order to avoid this, we use the `insert-behind-hooks' overlay
459 ;; property to make sure it works.
460 (let ((overlay (make-overlay from to object)))
461 (overlay-put overlay 'modification-hooks '(ansi-color-freeze-overlay))
462 overlay)))
464 (defun ansi-color-freeze-overlay (overlay is-after begin end &optional len)
465 "Prevent OVERLAY from being extended.
466 This function can be used for the `modification-hooks' overlay
467 property."
468 ;; if stuff was inserted at the end of the overlay
469 (when (and is-after
470 (= 0 len)
471 (= end (overlay-end overlay)))
472 ;; reset the end of the overlay
473 (move-overlay overlay (overlay-start overlay) begin)))
475 (defun ansi-color-set-extent-face (extent face)
476 "Set the `face' property of EXTENT to FACE.
477 XEmacs uses `set-extent-face', Emacs uses `overlay-put'."
478 (if (featurep 'xemacs)
479 (set-extent-face extent face)
480 (overlay-put extent 'face face)))
482 ;; Helper functions
484 (defun ansi-color-apply-sequence (escape-sequence faces)
485 "Apply ESCAPE-SEQ to FACES and return the new list of faces.
487 ESCAPE-SEQ is an escape sequences parsed by `ansi-color-get-face'.
489 If the new faces start with the symbol `default', then the new
490 faces are returned. If the faces start with something else,
491 they are appended to the front of the FACES list, and the new
492 list of faces is returned.
494 If `ansi-color-get-face' returns nil, then we either got a
495 null-sequence, or we stumbled upon some garbage. In either
496 case we return nil."
497 (let ((new-faces (ansi-color-get-face escape-sequence)))
498 (cond ((null new-faces)
499 nil)
500 ((eq (car new-faces) 'default)
501 (cdr new-faces))
503 ;; Like (append NEW-FACES FACES)
504 ;; but delete duplicates in FACES.
505 (let ((modified-faces (copy-sequence faces)))
506 (dolist (face (nreverse new-faces))
507 (setq modified-faces (delete face modified-faces))
508 (push face modified-faces))
509 modified-faces)))))
511 (defun ansi-color-make-color-map ()
512 "Creates a vector of face definitions and returns it.
514 The index into the vector is an ANSI code. See the documentation of
515 `ansi-color-map' for an example.
517 The face definitions are based upon the variables
518 `ansi-color-faces-vector' and `ansi-color-names-vector'."
519 (let ((ansi-color-map (make-vector 50 nil))
520 (index 0))
521 ;; miscellaneous attributes
522 (mapc
523 (function (lambda (e)
524 (aset ansi-color-map index e)
525 (setq index (1+ index)) ))
526 ansi-color-faces-vector)
527 ;; foreground attributes
528 (setq index 30)
529 (mapc
530 (function (lambda (e)
531 (aset ansi-color-map index
532 (ansi-color-make-face 'foreground e))
533 (setq index (1+ index)) ))
534 ansi-color-names-vector)
535 ;; background attributes
536 (setq index 40)
537 (mapc
538 (function (lambda (e)
539 (aset ansi-color-map index
540 (ansi-color-make-face 'background e))
541 (setq index (1+ index)) ))
542 ansi-color-names-vector)
543 ansi-color-map))
545 (defvar ansi-color-map (ansi-color-make-color-map)
546 "A brand new color map suitable for `ansi-color-get-face'.
548 The value of this variable is usually constructed by
549 `ansi-color-make-color-map'. The values in the array are such that the
550 numbers included in an SGR control sequences point to the correct
551 foreground or background colors.
553 Example: The sequence \033[34m specifies a blue foreground. Therefore:
554 (aref ansi-color-map 34)
555 => \(foreground-color . \"blue\")")
557 (defun ansi-color-map-update (symbol value)
558 "Update `ansi-color-map'.
560 Whenever the vectors used to construct `ansi-color-map' are changed,
561 this function is called. Therefore this function is listed as the :set
562 property of `ansi-color-faces-vector' and `ansi-color-names-vector'."
563 (set-default symbol value)
564 (setq ansi-color-map (ansi-color-make-color-map)))
566 (defun ansi-color-get-face-1 (ansi-code)
567 "Get face definition from `ansi-color-map'.
568 ANSI-CODE is used as an index into the vector."
569 (condition-case nil
570 (aref ansi-color-map ansi-code)
571 (args-out-of-range nil)))
573 (defun ansi-color-get-face (escape-seq)
574 "Create a new face by applying all the parameters in ESCAPE-SEQ.
576 Should any of the parameters result in the default face (usually this is
577 the parameter 0), then the effect of all previous parameters is cancelled.
579 ESCAPE-SEQ is a SGR control sequences such as \\033[34m. The parameter
580 34 is used by `ansi-color-get-face-1' to return a face definition."
581 (let ((i 0)
582 f val)
583 (while (string-match ansi-color-parameter-regexp escape-seq i)
584 (setq i (match-end 0)
585 val (ansi-color-get-face-1
586 (string-to-number (match-string 1 escape-seq) 10)))
587 (cond ((not val))
588 ((eq val 'default)
589 (setq f (list val)))
591 (unless (member val f)
592 (push val f)))))
595 (provide 'ansi-color)
597 ;; arch-tag: 00726118-9432-44fd-b72d-d2af7591c99c
598 ;;; ansi-color.el ends here