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