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