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