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