Mercurial > emacs
comparison lisp/ansi-color.el @ 35160:75a8ca0bdd1d
(ansi-color-process-output): Use markers instead
of positions for start and end of region.
(ansi-color-apply-on-region): Rewrote code to make it more robust.
Previously, occasional mistakes happend when fontifying many
chunks of output (eg. ls --color=yes /dev). This happened
whenever an overlay was created up to the end of the region, which
coincided with the process-mark. New text would then be added
within that overlay instead of after it.
(ansi-color-make-extent): Overlays are created with the property
`modification-hooks' set to '(ansi-color-freeze-overlay).
(ansi-color-freeze-overlay): New function. When inserting text at
the end of the overlay, the overlay will resize.
(ansi-color-process-output): Doc change.
(ansi-color-unfontify-region): Doc change. No longer installed
automatically in font-lock-unfontify-region-function.
(ansi-color-apply): Doc change.
(ansi-color-apply-on-region): Use extents or overlays instead of
text-properties.
(ansi-color-make-extent): New function.
(ansi-color-set-extent-face): New function.
(ansi-color-process): Removed, Emacs and XEmacs
both use ansi-color-process-output, now.
(ansi-color-process-output): Doesn't return string anymore. It is
installed in comint-output-filter-functions for both Emacs and
XEmacs, now.
(ansi-color-unfontify-region): Simplified code removing variables
pos and start-ansi.
(ansi-color-apply): Put text-property ansi-color before putting
text-property face because ansi-color-unfontify-region is called
immediately after the call to put-text-property.
(ansi-color-context-region): Doc change.
(ansi-color-filter-region): Simplified code.
(ansi-color-apply-on-region): Changed start to start-marker, using
a marker explicitly. Put text-property ansi-color before putting
text-property face because ansi-color-unfontify-region is called
immediately after the call to put-text-property.
(ansi-color-faces-vector): Doc change.
(ansi-color-for-comint-mode): Changed :type property to choice.
(ansi-color-last-context): Removed.
(ansi-color-process-output): Don't use ansi-color-last-context, as
the main functions will store their context now.
(ansi-color-context): Doc change.
(ansi-color-filter-apply): Rewrote it based on ansi-color-apply.
Uses ansi-color-context such that repeated calls will strip
partial escape sequences, too.
(ansi-color-apply): Simplified code. Colorize end of string if
face is not null. Store context in new (FACE STRING) format, such
that repeated calls will strip partial escape sequences, too.
Append faces to face property using ansi-color-apply-sequence such
that cumulative mode actually works.
(ansi-color-context-region): New variable.
(ansi-color-filter-region): Rewrote it based on
ansi-color-apply-on-region. Uses ansi-color-context-region such
that repeated calls will strip partial escape sequences, too.
(ansi-color-apply-on-region): Simplified code. Colorize end of
region if face is not null. Store context in new (FACE POS)
format, such that repeated calls will strip partial escape
sequences, too. Append faces to face property using
ansi-color-apply-sequence such that cumulative mode actually
works.
(ansi-color-apply-sequence): New function.
(ansi-color-get-face): When the default face is added to the list
of faces, all previous settings are discarded and the list of
faces is set to '(default).
(ansi-color-faces-vector): Use nil for the default
face, such that ansi-color-apply and ansi-color-apply-on-region
will do the right thing.
(ansi-color-apply): Do the right thing, ie. if ansi-color-get-face
returns nil, set the list of faces back to nil instead of
appending the result of ansi-color-get-face to the front of the
list.
(ansi-color-for-comint-mode): Doc change.
(ansi-color-process): Doc change.
(ansi-color-last-context): New buffer-local variable.
(ansi-color-process-output): New function. It is automatically
added to comint-output-filter-functions if this is XEmacs.
(ansi-color-unfontify-region): New optional parameter for XEmacs
compatibility. Check wether font-lock-syntactic-keywords is
boundp before removing the syntax table text property, as XEmacs
doesn't have it.
(ansi-color-filter-region): Doc change.
(ansi-color-apply-on-region): Doc change.
(ansi-color-make-face): New function. Compatibility layer for
XEmacs. Return temporary faces instead of cons cells for XEmacs.
(ansi-color-make-color-map): Use ansi-color-make-face.
(ansi-color-get-face): Avoid face text property '(nil) as results
in an errow for XEmacs.
(ansi-color-unfontify-region): New function. Uses
text-property ansi-color in order to preserve fontification by
ansi-color. When the package is loaded, a lambda expression is
put onto font-lock-mode-hook. This lambda expression will check
font-lock-unfontify-region-function and replace
font-lock-default-unfontify-region with
ansi-color-unfontify-region.
(ansi-color-apply): Add text-property ansi-color in addition to
text-property face.
(ansi-color-apply-on-region): Add text-property ansi-color in
addition to text-property face.
(save-buffer-state): Copy of the macro that is also used by
lazy-lock and font-lock.
(ansi-color-for-comint-mode): New option.
(ansi-color-for-comint-mode-on): Set ansi-color-for-comint-mode.
(ansi-color-for-comint-mode-off): Ditto.
(ansi-color-for-comint-mode-filter): Ditto.
(ansi-color-process): New function. Uses
ansi-color-for-comint-mode to decide what to do. This function is
added to comint-preoutput-filter-functions when the package is
loaded.
(ansi-color-for-shell-mode-set): Removed.
(ansi-color-for-shell-mode): Removed.
(ansi-color-for-shell-mode-set): New function with
the lambda expression from the ansi-color-for-shell-mode :set
property. Additionally, modify shell-mode-hook to enable or
disable font-lock-mode for future shell buffers.
(ansi-color-for-shell-mode): The :set property calls
ansi-color-for-shell-mode-set instead of a lambda expression.
(ansi-color-for-shell-mode): Doc change.
(ansi-color-context): New variable.
(ansi-color-apply): Save context between calls.
author | Gerd Moellmann <gerd@gnu.org> |
---|---|
date | Tue, 09 Jan 2001 11:38:28 +0000 |
parents | 5ad18c4ebe5c |
children | 16c26d4faf83 |
comparison
equal
deleted
inserted
replaced
35159:9276c6d67ee4 | 35160:75a8ca0bdd1d |
---|---|
1 ;;; ansi-color.el --- translate ANSI into text-properties | 1 ;;; ansi-color.el --- translate ANSI escape sequences into faces |
2 | 2 |
3 ;; Copyright (C) 1999, 2000 Free Software Foundation, Inc. | 3 ;; Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc. |
4 | 4 |
5 ;; Author: Alex Schroeder <alex@gnu.org> | 5 ;; Author: Alex Schroeder <alex@gnu.org> |
6 ;; Maintainer: Alex Schroeder <alex@gnu.org> | 6 ;; Maintainer: Alex Schroeder <alex@gnu.org> |
7 ;; Version: 2.4.0 | 7 ;; Version: 3.4.0 |
8 ;; Keywords: comm processes | 8 ;; Keywords: comm processes |
9 | 9 |
10 ;; This file is part of GNU Emacs. | 10 ;; This file is part of GNU Emacs. |
11 | 11 |
12 ;; GNU Emacs is free software; you can redistribute it and/or modify it | 12 ;; GNU Emacs is free software; you can redistribute it and/or modify it |
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | 24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
25 ;; Boston, MA 02111-1307, USA. | 25 ;; Boston, MA 02111-1307, USA. |
26 | 26 |
27 ;;; Commentary: | 27 ;;; Commentary: |
28 | 28 |
29 ;; This file provides a function that takes a string containing Select | 29 ;; This file provides a function that takes a string or a region |
30 ;; Graphic Rendition (SGR) control sequences (formerly known as ANSI | 30 ;; containing Select Graphic Rendition (SGR) control sequences (formerly |
31 ;; escape sequences) and tries to replace these with text-properties. | 31 ;; known as ANSI escape sequences) and tries to translate these into |
32 ;; | 32 ;; faces. |
33 ;; This allows you to run ls --color=yes in shell-mode: If | 33 ;; |
34 ;; `ansi-color-for-shell-mode' is non-nil, the SGR control sequences are | 34 ;; This allows you to run ls --color=yes in shell-mode. In order to |
35 ;; translated into text-properties, colorizing the ls output. If | 35 ;; test this, proceed as follows: |
36 ;; `ansi-color-for-shell-mode' is nil, the SGR control sequences are | 36 ;; |
37 ;; stripped, making the ls output legible. | 37 ;; 1. start a shell: M-x shell |
38 ;; 2. load this file: M-x load-library RET ansi-color RET | |
39 ;; 3. activate ansi-color: M-x ansi-color-for-comint-mode-on | |
40 ;; 4. test ls --color=yes in the *shell* buffer | |
41 ;; | |
42 ;; Note that starting your shell from within Emacs might set the TERM | |
43 ;; environment variable. The new setting might disable the output of | |
44 ;; SGR control sequences. Using ls --color=yes forces ls to produce | |
45 ;; these. | |
46 ;; | |
47 ;; If you decide you like this, add the following to your .emacs file: | |
48 ;; | |
49 ;; (autoload 'ansi-color-for-comint-mode-on "ansi-color" nil t) | |
50 ;; (add-hook 'shell-mode-hook 'ansi-color-for-comint-mode-on) | |
38 ;; | 51 ;; |
39 ;; SGR control sequences are defined in section 3.8.117 of the ECMA-48 | 52 ;; SGR control sequences are defined in section 3.8.117 of the ECMA-48 |
40 ;; standard (identical to ISO/IEC 6429), which is freely available as a | 53 ;; standard (identical to ISO/IEC 6429), which is freely available as a |
41 ;; PDF file <URL:http://www.ecma.ch/ecma1/STAND/ECMA-048.HTM>. The | 54 ;; PDF file <URL:http://www.ecma.ch/ecma1/STAND/ECMA-048.HTM>. The |
42 ;; "Graphic Rendition Combination Mode (GRCM)" implemented is | 55 ;; "Graphic Rendition Combination Mode (GRCM)" implemented is |
56 ;; control sequences. | 69 ;; control sequences. |
57 ;; | 70 ;; |
58 ;; `ansi-color-filter-region' to filter SGR control sequences from a | 71 ;; `ansi-color-filter-region' to filter SGR control sequences from a |
59 ;; region. | 72 ;; region. |
60 | 73 |
61 ;; Instead of defining lots of new faces, this package uses | |
62 ;; text-properties as described in the elisp manual | |
63 ;; *Note (elisp)Special Properties::. | |
64 | |
65 ;;; Thanks | 74 ;;; Thanks |
66 | 75 |
67 ;; Georges Brun-Cottan <gbruncot@emc.com> for improving ansi-color.el | 76 ;; Georges Brun-Cottan <gbruncot@emc.com> for improving ansi-color.el |
68 ;; substantially by adding the code needed to cope with arbitrary chunks | 77 ;; substantially by adding the code needed to cope with arbitrary chunks |
69 ;; of output and the filter functions. | 78 ;; of output and the filter functions. |
70 ;; | 79 ;; |
71 ;; Markus Kuhn <Markus.Kuhn@cl.cam.ac.uk> for pointing me to ECMA-48. | 80 ;; Markus Kuhn <Markus.Kuhn@cl.cam.ac.uk> for pointing me to ECMA-48. |
81 ;; | |
82 ;; Stefan Monnier <foo@acm.com> explaing obscure font-lock stuff and | |
83 ;; code suggestions. | |
72 | 84 |
73 | 85 |
74 | 86 |
75 ;;; Code: | 87 ;;; Code: |
76 | 88 |
77 ;; Customization | 89 ;; Customization |
78 | 90 |
79 (defgroup ansi-colors nil | 91 (defgroup ansi-colors nil |
80 "Translating SGR control sequences to text-properties. | 92 "Translating SGR control sequences to faces. |
81 This translation effectively colorizes strings and regions based upon | 93 This translation effectively colorizes strings and regions based upon |
82 SGR control sequences embedded in the text. SGR (Select Graphic | 94 SGR control sequences embedded in the text. SGR (Select Graphic |
83 Rendition) control sequences are defined in section 3.8.117 of the | 95 Rendition) control sequences are defined in section 3.8.117 of the |
84 ECMA-48 standard \(identical to ISO/IEC 6429), which is freely available | 96 ECMA-48 standard \(identical to ISO/IEC 6429), which is freely available |
85 as a PDF file <URL:http://www.ecma.ch/ecma1/STAND/ECMA-048.HTM>." | 97 as a PDF file <URL:http://www.ecma.ch/ecma1/STAND/ECMA-048.HTM>." |
100 4 underlined underline | 112 4 underlined underline |
101 5 slowly blinking bold | 113 5 slowly blinking bold |
102 6 rapidly blinking bold-italic | 114 6 rapidly blinking bold-italic |
103 7 negative image modeline | 115 7 negative image modeline |
104 | 116 |
117 Note that the symbol `default' is special: It will not be combined | |
118 with the current face. | |
119 | |
105 This vector is used by `ansi-color-make-color-map' to create a color | 120 This vector is used by `ansi-color-make-color-map' to create a color |
106 map. This color map is stored in the variable `ansi-color-map'." | 121 map. This color map is stored in the variable `ansi-color-map'." |
107 :type '(vector face face face face face face face face) | 122 :type '(vector face face face face face face face face) |
108 :set 'ansi-color-map-update | 123 :set 'ansi-color-map-update |
109 :initialize 'custom-initialize-default | 124 :initialize 'custom-initialize-default |
130 :type '(vector string string string string string string string string) | 145 :type '(vector string string string string string string string string) |
131 :set 'ansi-color-map-update | 146 :set 'ansi-color-map-update |
132 :initialize 'custom-initialize-default | 147 :initialize 'custom-initialize-default |
133 :group 'ansi-colors) | 148 :group 'ansi-colors) |
134 | 149 |
135 (defcustom ansi-color-for-shell-mode nil | |
136 "Determine wether font-lock or ansi-color get to fontify shell buffers. | |
137 | |
138 If non-nil and `global-font-lock-mode' is non-nil, ansi-color will be | |
139 used. This adds `ansi-color-apply' to | |
140 `comint-preoutput-filter-functions' and removes | |
141 `ansi-color-filter-apply' for all shell-mode buffers. | |
142 | |
143 If non-nil and global-font-lock-mode is nil, both `ansi-color-apply' and | |
144 `ansi-color-filter-apply' will be removed from | |
145 `comint-preoutput-filter-functions' for all shell-mode buffers. | |
146 | |
147 If nil, font-lock will be used (if it is enabled). This adds | |
148 `ansi-color-filter-apply' to `comint-preoutput-filter-functions' and | |
149 removes `ansi-color-apply' for all shell-mode buffers." | |
150 :version "20.8" | |
151 :type 'boolean | |
152 :set (function (lambda (symbol value) | |
153 (set-default symbol value) | |
154 (save-excursion | |
155 (let ((buffers (buffer-list)) | |
156 buffer) | |
157 (while buffers | |
158 (setq buffer (car buffers) | |
159 buffers (cdr buffers)) | |
160 (set-buffer buffer) | |
161 (when (eq major-mode 'shell-mode) | |
162 (if value | |
163 (if global-font-lock-mode | |
164 (progn | |
165 (font-lock-mode 0) | |
166 (remove-hook 'comint-preoutput-filter-functions | |
167 'ansi-color-filter-apply) | |
168 (add-hook 'comint-preoutput-filter-functions | |
169 'ansi-color-apply)) | |
170 (remove-hook 'comint-preoutput-filter-functions | |
171 'ansi-color-filter-apply) | |
172 (remove-hook 'comint-preoutput-filter-functions | |
173 'ansi-color-apply)) | |
174 (if global-font-lock-mode | |
175 (font-lock-mode 1)) | |
176 (remove-hook 'comint-preoutput-filter-functions | |
177 'ansi-color-apply) | |
178 (add-hook 'comint-preoutput-filter-functions | |
179 'ansi-color-filter-apply)))))))) | |
180 :initialize 'custom-initialize-reset | |
181 :group 'ansi-colors) | |
182 | |
183 (defconst ansi-color-regexp "\033\\[\\([0-9;]*\\)m" | 150 (defconst ansi-color-regexp "\033\\[\\([0-9;]*\\)m" |
184 "Regexp that matches SGR control sequences.") | 151 "Regexp that matches SGR control sequences.") |
185 | 152 |
186 (defconst ansi-color-parameter-regexp "\\([0-9]*\\)[m;]" | 153 (defconst ansi-color-parameter-regexp "\\([0-9]*\\)[m;]" |
187 "Regexp that matches SGR control sequence parameters.") | 154 "Regexp that matches SGR control sequence parameters.") |
188 | 155 |
189 | 156 |
190 ;; Main functions | 157 ;; Convenience functions for comint modes (eg. shell-mode) |
191 | 158 |
192 | 159 |
193 (defun ansi-color-filter-apply (s) | 160 (defcustom ansi-color-for-comint-mode nil |
194 "Filter out all SGR control sequences from S. | 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. | |
166 | |
167 In order for this to have any effect, `ansi-color-process-output' must | |
168 be in `comint-output-filter-functions'. | |
169 | |
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]" | |
175 :version "20.8" | |
176 :type '(choice (const :tag "Do nothing" nil) | |
177 (const :tag "Filter" filter) | |
178 (const :tag "Translate" t)) | |
179 :group 'ansi-colors) | |
180 | |
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 | |
196 (defun ansi-color-process-output (string) | |
197 "Maybe translate SGR control sequences of comint output into text-properties. | |
198 | |
199 Depending on variable `ansi-color-for-comint-mode' the comint output is | |
200 either not processed, SGR control sequences are filtered using | |
201 `ansi-color-filter-region', or SGR control sequences are translated into | |
202 text-properties using `ansi-color-apply-on-region'. | |
203 | |
204 The comint output is assumed to lie between the marker | |
205 `comint-last-output-start' and the process-mark. | |
206 | |
207 This is a good function to put in `comint-output-filter-functions'." | |
208 (let ((start-marker (or comint-last-output-start | |
209 (point-min-marker))) | |
210 (end-marker (process-mark (get-buffer-process (current-buffer))))) | |
211 (cond ((eq ansi-color-for-comint-mode nil)) | |
212 ((eq ansi-color-for-comint-mode 'filter) | |
213 (ansi-color-filter-region start-marker end-marker)) | |
214 (t | |
215 (ansi-color-apply-on-region start-marker end-marker))))) | |
216 | |
217 (add-hook 'comint-output-filter-functions | |
218 'ansi-color-process-output) | |
219 | |
220 | |
221 ;; Alternative font-lock-unfontify-region-function | |
222 | |
223 | |
224 (eval-when-compile | |
225 ;; We use this to preserve or protect things when modifying text | |
226 ;; properties. Stolen from lazy-lock and font-lock. Ugly!!! | |
227 ;; Probably most of this is not needed? | |
228 (defmacro save-buffer-state (varlist &rest body) | |
229 "Bind variables according to VARLIST and eval BODY restoring buffer state." | |
230 (` (let* ((,@ (append varlist | |
231 '((modified (buffer-modified-p)) (buffer-undo-list t) | |
232 (inhibit-read-only t) (inhibit-point-motion-hooks t) | |
233 before-change-functions after-change-functions | |
234 deactivate-mark buffer-file-name buffer-file-truename)))) | |
235 (,@ body) | |
236 (when (and (not modified) (buffer-modified-p)) | |
237 (set-buffer-modified-p nil))))) | |
238 (put 'save-buffer-state 'lisp-indent-function 1)) | |
239 | |
240 (defun ansi-color-unfontify-region (beg end &rest xemacs-stuff) | |
241 "Replacement function for `font-lock-default-unfontify-region'. | |
242 When font-lock is active in a buffer, you cannot simply add face | |
243 text-properties to the buffer. Font-lock will remove the face | |
244 text-property using `font-lock-unfontify-region-function'. If you want | |
245 to insert the strings returned by `ansi-color-apply' into such buffers, | |
246 you must set `font-lock-unfontify-region-function' to | |
247 `ansi-color-unfontify-region'. This function will not remove all face | |
248 text-properties unconditionally. It will keep the face text-properties | |
249 if the property `ansi-color' is set. | |
250 | |
251 The region from BEG to END is unfontified. XEMACS-STUFF is ignored. | |
252 | |
253 A possible way to install this would be: | |
254 | |
255 \(add-hook 'font-lock-mode-hook | |
256 \(function (lambda () | |
257 \(setq font-lock-unfontify-region-function | |
258 'ansi-color-unfontify-region))))" | |
259 ;; save-buffer-state is a macro in font-lock.el! | |
260 (save-buffer-state nil | |
261 (when (boundp 'font-lock-syntactic-keywords) | |
262 (remove-text-properties beg end '(syntax-table nil))) | |
263 ;; instead of just using (remove-text-properties beg end '(face | |
264 ;; nil)), we find regions with a non-nil face test-property, skip | |
265 ;; positions with the ansi-color property set, and remove the | |
266 ;; remaining face test-properties. | |
267 (while (setq beg (text-property-not-all beg end 'face nil)) | |
268 (setq beg (or (text-property-not-all beg end 'ansi-color t) end)) | |
269 (when (get-text-property beg 'face) | |
270 (let ((end-face (or (text-property-any beg end 'face nil) | |
271 end))) | |
272 (remove-text-properties beg end-face '(face nil)) | |
273 (setq beg end-face)))))) | |
274 | |
275 ;; Working with strings | |
276 | |
277 (defvar ansi-color-context nil | |
278 "Context saved between two calls to `ansi-color-apply'. | |
279 This is a list of the form (FACES FRAGMENT) or nil. FACES is a list of | |
280 faces the last call to `ansi-color-apply' ended with, and FRAGMENT is a | |
281 string starting with an escape sequence, possibly the start of a new | |
282 escape sequence.") | |
283 (make-variable-buffer-local 'ansi-color-context) | |
284 | |
285 (defun ansi-color-filter-apply (string) | |
286 "Filter out all SGR control sequences from STRING. | |
287 | |
288 Every call to this function will set and use the buffer-local variable | |
289 `ansi-color-context' to save partial escape sequences. This information | |
290 will be used for the next call to `ansi-color-apply'. Set | |
291 `ansi-color-context' to nil if you don't want this. | |
195 | 292 |
196 This function can be added to `comint-preoutput-filter-functions'." | 293 This function can be added to `comint-preoutput-filter-functions'." |
197 (while (string-match ansi-color-regexp s) | 294 (let ((start 0) end result) |
198 (setq s (replace-match "" t t s))) | 295 ;; if context was saved and is a string, prepend it |
199 s) | 296 (if (cadr ansi-color-context) |
200 | 297 (setq string (concat (cadr ansi-color-context) string) |
201 | 298 ansi-color-context nil)) |
202 (defun ansi-color-filter-region (begin end) | 299 ;; find the next escape sequence |
203 "Filter out all SGR control sequences from region START END. | 300 (while (setq end (string-match ansi-color-regexp string start)) |
204 | 301 (setq result (concat result (substring string start end)) |
205 Returns the first point it is safe to start with. Used to speedup | 302 start (match-end 0))) |
206 further processing. | 303 ;; save context, add the remainder of the string to the result |
207 | 304 (let (fragment) |
208 Design to cope with arbitrary chunk of output such as the ones get by | 305 (if (string-match "\033" string start) |
209 comint-output-filter-functions, e.g.: | 306 (let ((pos (match-beginning 0))) |
210 | 307 (setq fragment (substring string pos) |
211 \(defvar last-context nil) | 308 result (concat result (substring string start pos)))) |
212 \(make-variable-buffer-local 'last-context) | 309 (setq result (concat result (substring string start)))) |
213 | 310 (if fragment |
214 \(defun filter-out-color-in-buffer (s) | 311 (setq ansi-color-context (list nil fragment)) |
215 \(setq last-context | 312 (setq ansi-color-context nil))) |
216 \(ansi-color-filter-region | 313 result)) |
217 \(if last-context | |
218 last-context | |
219 \(if (marker-position comint-last-output-start) | |
220 \(marker-position comint-last-output-start) | |
221 1)) | |
222 \(marker-position (process-mark (get-buffer-process (current-buffer)))) )) | |
223 s) | |
224 | |
225 \(add-hook 'comint-output-filter-functions 'filter-out-color-in-buffer) | |
226 " | |
227 (let ((endm (copy-marker end))) | |
228 (save-excursion | |
229 (goto-char begin) | |
230 (while (re-search-forward ansi-color-regexp endm t) | |
231 (replace-match "")) | |
232 (if (re-search-forward "\033" endm t) | |
233 (match-beginning 0) | |
234 (marker-position endm))))) | |
235 | |
236 | 314 |
237 (defun ansi-color-apply (string) | 315 (defun ansi-color-apply (string) |
238 "Translates SGR control sequences into text-properties. | 316 "Translates SGR control sequences into text-properties. |
239 | 317 |
240 Applies SGR control sequences setting foreground and background colors | 318 Applies SGR control sequences setting foreground and background colors |
241 to STRING and returns the result. The colors used are given in | 319 to STRING using text-properties and returns the result. The colors used |
242 `ansi-color-faces-vector' and `ansi-color-names-vector'. | 320 are given in `ansi-color-faces-vector' and `ansi-color-names-vector'. |
243 | 321 See function `ansi-color-apply-sequence' for details. |
244 This function can be added to `comint-preoutput-filter-functions'." | 322 |
245 (let (face (start 0) end escape-sequence null-sequence result) | 323 Every call to this function will set and use the buffer-local variable |
324 `ansi-color-context' to save partial escape sequences and current face. | |
325 This information will be used for the next call to `ansi-color-apply'. | |
326 Set `ansi-color-context' to nil if you don't want this. | |
327 | |
328 This function can be added to `comint-preoutput-filter-functions'. | |
329 | |
330 You cannot insert the strings returned into buffers using font-lock. | |
331 See `ansi-color-unfontify-region' for a way around this." | |
332 (let ((face (car ansi-color-context)) | |
333 (start 0) end escape-sequence result) | |
334 ;; if context was saved and is a string, prepend it | |
335 (if (cadr ansi-color-context) | |
336 (setq string (concat (cadr ansi-color-context) string) | |
337 ansi-color-context nil)) | |
246 ;; find the next escape sequence | 338 ;; find the next escape sequence |
247 (while (setq end (string-match ansi-color-regexp string start)) | 339 (while (setq end (string-match ansi-color-regexp string start)) |
248 ;; store escape sequence | 340 ;; store escape sequence |
249 (setq escape-sequence (match-string 1 string) | 341 (setq escape-sequence (match-string 1 string)) |
250 null-sequence (string-equal escape-sequence "")) | |
251 ;; colorize the old block from start to end using old face | 342 ;; colorize the old block from start to end using old face |
252 (if face | 343 (when face |
253 (put-text-property start end 'face face string)) | 344 (put-text-property start end 'ansi-color t string) |
345 (put-text-property start end 'face face string)) | |
254 (setq result (concat result (substring string start end)) | 346 (setq result (concat result (substring string start end)) |
255 start (match-end 0)) | 347 start (match-end 0)) |
256 ;; create new face by applying all the parameters in the escape sequence | 348 ;; create new face by applying all the parameters in the escape |
257 (if null-sequence | 349 ;; sequence |
258 (setq face nil) | 350 (setq face (ansi-color-apply-sequence escape-sequence face))) |
259 (setq face (ansi-color-get-face escape-sequence)))) | 351 ;; if the rest of the string should have a face, put it there |
260 (concat result (substring string start)))) | 352 (when face |
261 | 353 (put-text-property start (length string) 'ansi-color t string) |
262 | 354 (put-text-property start (length string) 'face face string)) |
263 (defun ansi-color-apply-on-region (begin end &optional context) | 355 ;; save context, add the remainder of the string to the result |
264 "Translates SGR control sequences into text-properties. | 356 (let (fragment) |
357 (if (string-match "\033" string start) | |
358 (let ((pos (match-beginning 0))) | |
359 (setq fragment (substring string pos) | |
360 result (concat result (substring string start pos)))) | |
361 (setq result (concat result (substring string start)))) | |
362 (if (or face fragment) | |
363 (setq ansi-color-context (list face fragment)) | |
364 (setq ansi-color-context nil))) | |
365 result)) | |
366 | |
367 ;; Working with regions | |
368 | |
369 (defvar ansi-color-context-region nil | |
370 "Context saved between two calls to `ansi-color-apply-on-region'. | |
371 This is a list of the form (FACES MARKER) or nil. FACES is a list of | |
372 faces the last call to `ansi-color-apply-on-region' ended with, and | |
373 MARKER is a buffer position within an escape sequence or the last | |
374 position processed.") | |
375 (make-variable-buffer-local 'ansi-color-context-region) | |
376 | |
377 (defun ansi-color-filter-region (begin end) | |
378 "Filter out all SGR control sequences from region BEGIN to END. | |
379 | |
380 Every call to this function will set and use the buffer-local variable | |
381 `ansi-color-context-region' to save position. This information will be | |
382 used for the next call to `ansi-color-apply-on-region'. Specifically, | |
383 it will override BEGIN, the start of the region. Set | |
384 `ansi-color-context-region' to nil if you don't want this." | |
385 (let ((end-marker (copy-marker end)) | |
386 (start (or (cadr ansi-color-context-region) begin))) | |
387 (save-excursion | |
388 (goto-char start) | |
389 ;; find the next escape sequence | |
390 (while (re-search-forward ansi-color-regexp end-marker t) | |
391 ;; delete the escape sequence | |
392 (replace-match "")) | |
393 ;; save context, add the remainder of the string to the result | |
394 (if (re-search-forward "\033" end-marker t) | |
395 (setq ansi-color-context-region (list nil (match-beginning 0))) | |
396 (setq ansi-color-context-region nil))))) | |
397 | |
398 (defun ansi-color-apply-on-region (begin end) | |
399 "Translates SGR control sequences into overlays or extents. | |
265 | 400 |
266 Applies SGR control sequences setting foreground and background colors | 401 Applies SGR control sequences setting foreground and background colors |
267 to text in region. The colors used are given in | 402 to text in region between BEGIN and END using extents or overlays. |
268 `ansi-color-faces-vector' and `ansi-color-names-vector'. | 403 Emacs will use overlays, XEmacs will use extents. The colors used are |
269 Returns a context than can be used to speedup further processing. | 404 given in `ansi-color-faces-vector' and `ansi-color-names-vector'. See |
270 Context is a (begin (start . face)) list. | 405 function `ansi-color-apply-sequence' for details. |
271 | 406 |
272 Design to cope with arbitrary chunk of output such as the ones get by | 407 Every call to this function will set and use the buffer-local variable |
273 comint-output-filter-functions, e.g.: | 408 `ansi-color-context-region' to save position and current face. This |
274 | 409 information will be used for the next call to |
275 \(defvar last-context nil) | 410 `ansi-color-apply-on-region'. Specifically, it will override BEGIN, the |
276 \(make-variable-buffer-local 'last-context) | 411 start of the region and set the face with which to start. Set |
277 | 412 `ansi-color-context-region' to nil if you don't want this." |
278 \(defun ansi-output-filter (s) | 413 (let ((face (car ansi-color-context-region)) |
279 \(setq last-context | 414 (start-marker (or (cadr ansi-color-context-region) |
280 \(ansi-color-apply-on-region | 415 (copy-marker begin))) |
281 \(if last-context | 416 (end-marker (copy-marker end)) |
282 \(car last-context) | 417 escape-sequence) |
283 \(if (marker-position comint-last-output-start) | |
284 \(marker-position comint-last-output-start) | |
285 1)) | |
286 \(process-mark (get-buffer-process (current-buffer))) | |
287 last-context )) | |
288 s) | |
289 | |
290 \(add-hook 'comint-output-filter-functions 'ansi-output-filter) | |
291 " | |
292 (let ((endm (copy-marker end)) | |
293 (face (if (and context (cdr context)) | |
294 (cdr (cdr context)))) | |
295 (face-start (if (and context (cdr context)) | |
296 (car (cdr context)))) | |
297 (next-safe-start begin) | |
298 escape-sequence | |
299 null-sequence | |
300 stop ) | |
301 (save-excursion | 418 (save-excursion |
302 (goto-char begin) | 419 (goto-char start-marker) |
303 ;; find the next escape sequence | 420 ;; find the next escape sequence |
304 (while (setq stop (re-search-forward ansi-color-regexp endm t)) | 421 (while (re-search-forward ansi-color-regexp end-marker t) |
305 ;; store escape sequence | 422 ;; colorize the old block from start to end using old face |
306 (setq escape-sequence (match-string 1)) | 423 (when face |
307 (setq null-sequence (string-equal (match-string 1) "")) | 424 (ansi-color-set-extent-face |
308 (setq next-safe-start (match-beginning 0)) | 425 (ansi-color-make-extent start-marker (match-beginning 0)) |
309 (if face | 426 face)) |
310 (put-text-property face-start next-safe-start 'face face)) ; colorize | 427 ;; store escape sequence and new start position |
311 (replace-match "") ; delete the ANSI sequence | 428 (setq escape-sequence (match-string 1) |
312 (if null-sequence | 429 start-marker (copy-marker (match-end 0))) |
313 (setq face nil) | 430 ;; delete the escape sequence |
314 (setq face-start next-safe-start) | 431 (replace-match "") |
315 (setq face (ansi-color-get-face escape-sequence)))) | 432 ;; create new face by applying all the parameters in the escape |
316 (setq next-safe-start | 433 ;; sequence |
317 (if (re-search-forward "\033" endm t) | 434 (setq face (ansi-color-apply-sequence escape-sequence face))) |
318 (match-beginning 0) | 435 ;; search for the possible start of a new escape sequence |
319 (marker-position endm)))) | 436 (if (re-search-forward "\033" end-marker t) |
320 (cons next-safe-start | 437 (progn |
321 (if face | 438 ;; if the rest of the region should have a face, put it there |
322 (cons face-start face))) )) | 439 (when face |
440 (ansi-color-set-extent-face | |
441 (ansi-color-make-extent start-marker (point)) | |
442 face)) | |
443 ;; save face and point | |
444 (setq ansi-color-context-region | |
445 (list face (copy-marker (match-beginning 0))))) | |
446 ;; if the rest of the region should have a face, put it there | |
447 (if face | |
448 (progn | |
449 (ansi-color-set-extent-face | |
450 (ansi-color-make-extent start-marker end-marker) | |
451 face) | |
452 (setq ansi-color-context-region (list face))) | |
453 ;; reset context | |
454 (setq ansi-color-context-region nil)))))) | |
455 | |
456 ;; This function helps you look for overlapping overlays. This is | |
457 ;; usefull in comint-buffers. Overlapping overlays should not happen! | |
458 ;; A possible cause for bugs are the markers. If you create an overlay | |
459 ;; up to the end of the region, then that end might coincide with the | |
460 ;; process-mark. As text is added BEFORE the process-mark, the overlay | |
461 ;; will keep growing. Therefore, as more overlays are created later on, | |
462 ;; there will be TWO OR MORE overlays covering the buffer at that point. | |
463 ;; This function helps you check your buffer for these situations. | |
464 ; (defun ansi-color-debug-overlays () | |
465 ; (interactive) | |
466 ; (let ((pos (point-min))) | |
467 ; (while (< pos (point-max)) | |
468 ; (if (<= 2 (length (overlays-at pos))) | |
469 ; (progn | |
470 ; (goto-char pos) | |
471 ; (error "%d overlays at %d" (length (overlays-at pos)) pos)) | |
472 ; (let (message-log-max) | |
473 ; (message "Reached %d." pos))) | |
474 ; (setq pos (next-overlay-change pos))))) | |
475 | |
476 ;; Emacs/XEmacs compatibility layer | |
477 | |
478 (defun ansi-color-make-face (property color) | |
479 "Return a face with PROPERTY set to COLOR. | |
480 PROPERTY can be either symbol `foreground' or symbol `background'. | |
481 | |
482 For Emacs, we just return the cons cell \(PROPERTY . COLOR). | |
483 For XEmacs, we create a temporary face and return it." | |
484 (if (featurep 'xemacs) | |
485 (let ((face (make-face (intern (concat color "-" (symbol-name property))) | |
486 "Temporary face created by ansi-color." | |
487 t))) | |
488 (set-face-property face property color) | |
489 face) | |
490 (cond ((eq property 'foreground) | |
491 (cons 'foreground-color color)) | |
492 ((eq property 'background) | |
493 (cons 'background-color color)) | |
494 (t | |
495 (cons property color))))) | |
496 | |
497 (defun ansi-color-make-extent (from to &optional object) | |
498 "Make an extent for the range [FROM, TO) in OBJECT. | |
499 | |
500 OBJECT defaults to the current buffer. XEmacs uses `make-extent', Emacs | |
501 uses `make-overlay'. XEmacs can use a buffer or a string for OBJECT, | |
502 Emacs requires OBJECT to be a buffer." | |
503 (if (functionp 'make-extent) | |
504 (make-extent from to object) | |
505 ;; In Emacs, the overlay might end at the process-mark in comint | |
506 ;; buffers. In that case, new text will be inserted before the | |
507 ;; process-mark, ie. inside the overlay (using insert-before-marks). | |
508 ;; In order to avoid this, we use the `insert-behind-hooks' overlay | |
509 ;; property to make sure it works. | |
510 (let ((overlay (make-overlay from to object))) | |
511 (overlay-put overlay 'modification-hooks '(ansi-color-freeze-overlay)) | |
512 overlay))) | |
513 | |
514 (defun ansi-color-freeze-overlay (overlay is-after begin end &optional len) | |
515 "Prevent OVERLAY from being extended. | |
516 This function can be used for the `modification-hooks' overlay | |
517 property." | |
518 ;; if stuff was inserted at the end of the overlay | |
519 (when (and is-after | |
520 (= 0 len) | |
521 (= end (overlay-end overlay))) | |
522 ;; reset the end of the overlay | |
523 (move-overlay overlay (overlay-start overlay) begin))) | |
524 | |
525 (defun ansi-color-set-extent-face (extent face) | |
526 "Set the `face' property of EXTENT to FACE. | |
527 XEmacs uses `set-extent-face', Emacs uses `overlay-put'." | |
528 (if (functionp 'set-extent-face) | |
529 (set-extent-face extent face) | |
530 (overlay-put extent 'face face))) | |
323 | 531 |
324 ;; Helper functions | 532 ;; Helper functions |
533 | |
534 (defun ansi-color-apply-sequence (escape-sequence faces) | |
535 "Apply ESCAPE-SEQ to FACES and return the new list of faces. | |
536 | |
537 ESCAPE-SEQ is an escape sequences parsed by `ansi-color-get-face'. | |
538 | |
539 If the new faces start with the symbol `default', then the new | |
540 faces are returned. If the faces start with something else, | |
541 they are appended to the front of the FACES list, and the new | |
542 list of faces is returned. | |
543 | |
544 If `ansi-color-get-face' returns nil, then we either got a | |
545 null-sequence, or we stumbled upon some garbage. In either | |
546 case we return nil." | |
547 (let ((new-faces (ansi-color-get-face escape-sequence))) | |
548 (cond ((null new-faces) | |
549 nil) | |
550 ((eq (car new-faces) 'default) | |
551 (cdr new-faces)) | |
552 (t | |
553 (append new-faces face))))) | |
325 | 554 |
326 (defun ansi-color-make-color-map () | 555 (defun ansi-color-make-color-map () |
327 "Creates a vector of face definitions and returns it. | 556 "Creates a vector of face definitions and returns it. |
328 | 557 |
329 The index into the vector is an ANSI code. See the documentation of | 558 The index into the vector is an ANSI code. See the documentation of |
337 (mapcar | 566 (mapcar |
338 (function (lambda (e) | 567 (function (lambda (e) |
339 (aset ansi-color-map index e) | 568 (aset ansi-color-map index e) |
340 (setq index (1+ index)) )) | 569 (setq index (1+ index)) )) |
341 ansi-color-faces-vector) | 570 ansi-color-faces-vector) |
342 | |
343 ;; foreground attributes | 571 ;; foreground attributes |
344 (setq index 30) | 572 (setq index 30) |
345 (mapcar | 573 (mapcar |
346 (function (lambda (e) | 574 (function (lambda (e) |
347 (aset ansi-color-map index | 575 (aset ansi-color-map index |
348 (cons 'foreground-color e)) | 576 (ansi-color-make-face 'foreground e)) |
349 (setq index (1+ index)) )) | 577 (setq index (1+ index)) )) |
350 ansi-color-names-vector) | 578 ansi-color-names-vector) |
351 | |
352 ;; background attributes | 579 ;; background attributes |
353 (setq index 40) | 580 (setq index 40) |
354 (mapcar | 581 (mapcar |
355 (function (lambda (e) | 582 (function (lambda (e) |
356 (aset ansi-color-map index | 583 (aset ansi-color-map index |
357 (cons 'background-color e)) | 584 (ansi-color-make-face 'background e)) |
358 (setq index (1+ index)) )) | 585 (setq index (1+ index)) )) |
359 ansi-color-names-vector) | 586 ansi-color-names-vector) |
360 ansi-color-map)) | 587 ansi-color-map)) |
361 | 588 |
362 (defvar ansi-color-map (ansi-color-make-color-map) | 589 (defvar ansi-color-map (ansi-color-make-color-map) |
363 "A brand new color map suitable for ansi-color-get-face. | 590 "A brand new color map suitable for `ansi-color-get-face'. |
364 | 591 |
365 The value of this variable is usually constructed by | 592 The value of this variable is usually constructed by |
366 `ansi-color-make-color-map'. The values in the array are such that the | 593 `ansi-color-make-color-map'. The values in the array are such that the |
367 numbers included in an SGR control sequences point to the correct | 594 numbers included in an SGR control sequences point to the correct |
368 foreground or background colors. | 595 foreground or background colors. |
388 ('args-out-of-range nil))) | 615 ('args-out-of-range nil))) |
389 | 616 |
390 (defun ansi-color-get-face (escape-seq) | 617 (defun ansi-color-get-face (escape-seq) |
391 "Create a new face by applying all the parameters in ESCAPE-SEQ. | 618 "Create a new face by applying all the parameters in ESCAPE-SEQ. |
392 | 619 |
393 ESCAPE-SEQ is a SGR control sequences such as \033[34m. The parameter | 620 Should any of the parameters result in the default face (usually this is |
621 the parameter 0), then the effect of all previous parameters is cancelled. | |
622 | |
623 ESCAPE-SEQ is a SGR control sequences such as \\033[34m. The parameter | |
394 34 is used by `ansi-color-get-face-1' to return a face definition." | 624 34 is used by `ansi-color-get-face-1' to return a face definition." |
395 (let ((ansi-color-r "[0-9][0-9]?") | 625 (let ((ansi-color-r "[0-9][0-9]?") |
396 (i 0) | 626 (i 0) |
397 f) | 627 f val) |
398 (while (string-match ansi-color-r escape-seq i) | 628 (while (string-match ansi-color-r escape-seq i) |
399 (setq i (match-end 0)) | 629 (setq i (match-end 0) |
400 (add-to-list 'f | 630 val (ansi-color-get-face-1 |
401 (ansi-color-get-face-1 | 631 (string-to-int (match-string 0 escape-seq) 10))) |
402 (string-to-int (match-string 0 escape-seq) 10)))) | 632 (cond ((not val)) |
633 ((eq val 'default) | |
634 (setq f (list val))) | |
635 (t | |
636 (add-to-list 'f val)))) | |
403 f)) | 637 f)) |
404 | 638 |
405 (provide 'ansi-color) | 639 (provide 'ansi-color) |
406 | 640 |
407 ;;; ansi-color.el ends here | 641 ;;; ansi-color.el ends here |