From 0e3c1e3ea6a27e20d0252661336afe9fc84b21f5 Mon Sep 17 00:00:00 2001 From: Gerd Moellmann Date: Tue, 9 Jan 2001 11:38:28 +0000 Subject: (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. --- lisp/ansi-color.el | 604 +++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 419 insertions(+), 185 deletions(-) (limited to 'lisp/ansi-color.el') diff --git a/lisp/ansi-color.el b/lisp/ansi-color.el index ca07b0f8ea5..a3e3fb5e44c 100644 --- a/lisp/ansi-color.el +++ b/lisp/ansi-color.el @@ -1,10 +1,10 @@ -;;; ansi-color.el --- translate ANSI into text-properties +;;; ansi-color.el --- translate ANSI escape sequences into faces -;; Copyright (C) 1999, 2000 Free Software Foundation, Inc. +;; Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc. ;; Author: Alex Schroeder ;; Maintainer: Alex Schroeder -;; Version: 2.4.0 +;; Version: 3.4.0 ;; Keywords: comm processes ;; This file is part of GNU Emacs. @@ -26,15 +26,28 @@ ;;; Commentary: -;; This file provides a function that takes a string containing Select -;; Graphic Rendition (SGR) control sequences (formerly known as ANSI -;; escape sequences) and tries to replace these with text-properties. +;; This file provides a function that takes a string or a region +;; containing Select Graphic Rendition (SGR) control sequences (formerly +;; known as ANSI escape sequences) and tries to translate these into +;; faces. ;; -;; This allows you to run ls --color=yes in shell-mode: If -;; `ansi-color-for-shell-mode' is non-nil, the SGR control sequences are -;; translated into text-properties, colorizing the ls output. If -;; `ansi-color-for-shell-mode' is nil, the SGR control sequences are -;; stripped, making the ls output legible. +;; This allows you to run ls --color=yes in shell-mode. In order to +;; test this, proceed as follows: +;; +;; 1. start a shell: M-x shell +;; 2. load this file: M-x load-library RET ansi-color RET +;; 3. activate ansi-color: M-x ansi-color-for-comint-mode-on +;; 4. test ls --color=yes in the *shell* buffer +;; +;; Note that starting your shell from within Emacs might set the TERM +;; environment variable. The new setting might disable the output of +;; SGR control sequences. Using ls --color=yes forces ls to produce +;; these. +;; +;; If you decide you like this, add the following to your .emacs file: +;; +;; (autoload 'ansi-color-for-comint-mode-on "ansi-color" nil t) +;; (add-hook 'shell-mode-hook 'ansi-color-for-comint-mode-on) ;; ;; SGR control sequences are defined in section 3.8.117 of the ECMA-48 ;; standard (identical to ISO/IEC 6429), which is freely available as a @@ -58,10 +71,6 @@ ;; `ansi-color-filter-region' to filter SGR control sequences from a ;; region. -;; Instead of defining lots of new faces, this package uses -;; text-properties as described in the elisp manual -;; *Note (elisp)Special Properties::. - ;;; Thanks ;; Georges Brun-Cottan for improving ansi-color.el @@ -69,6 +78,9 @@ ;; of output and the filter functions. ;; ;; Markus Kuhn for pointing me to ECMA-48. +;; +;; Stefan Monnier explaing obscure font-lock stuff and +;; code suggestions. @@ -77,7 +89,7 @@ ;; Customization (defgroup ansi-colors nil - "Translating SGR control sequences to text-properties. + "Translating SGR control sequences to faces. This translation effectively colorizes strings and regions based upon SGR control sequences embedded in the text. SGR (Select Graphic Rendition) control sequences are defined in section 3.8.117 of the @@ -102,6 +114,9 @@ Parameter Description Face used by default 6 rapidly blinking bold-italic 7 negative image modeline +Note that the symbol `default' is special: It will not be combined +with the current face. + This vector is used by `ansi-color-make-color-map' to create a color map. This color map is stored in the variable `ansi-color-map'." :type '(vector face face face face face face face face) @@ -132,54 +147,6 @@ map. This color map is stored in the variable `ansi-color-map'." :initialize 'custom-initialize-default :group 'ansi-colors) -(defcustom ansi-color-for-shell-mode nil - "Determine wether font-lock or ansi-color get to fontify shell buffers. - -If non-nil and `global-font-lock-mode' is non-nil, ansi-color will be -used. This adds `ansi-color-apply' to -`comint-preoutput-filter-functions' and removes -`ansi-color-filter-apply' for all shell-mode buffers. - -If non-nil and global-font-lock-mode is nil, both `ansi-color-apply' and -`ansi-color-filter-apply' will be removed from -`comint-preoutput-filter-functions' for all shell-mode buffers. - -If nil, font-lock will be used (if it is enabled). This adds -`ansi-color-filter-apply' to `comint-preoutput-filter-functions' and -removes `ansi-color-apply' for all shell-mode buffers." - :version "20.8" - :type 'boolean - :set (function (lambda (symbol value) - (set-default symbol value) - (save-excursion - (let ((buffers (buffer-list)) - buffer) - (while buffers - (setq buffer (car buffers) - buffers (cdr buffers)) - (set-buffer buffer) - (when (eq major-mode 'shell-mode) - (if value - (if global-font-lock-mode - (progn - (font-lock-mode 0) - (remove-hook 'comint-preoutput-filter-functions - 'ansi-color-filter-apply) - (add-hook 'comint-preoutput-filter-functions - 'ansi-color-apply)) - (remove-hook 'comint-preoutput-filter-functions - 'ansi-color-filter-apply) - (remove-hook 'comint-preoutput-filter-functions - 'ansi-color-apply)) - (if global-font-lock-mode - (font-lock-mode 1)) - (remove-hook 'comint-preoutput-filter-functions - 'ansi-color-apply) - (add-hook 'comint-preoutput-filter-functions - 'ansi-color-filter-apply)))))))) - :initialize 'custom-initialize-reset - :group 'ansi-colors) - (defconst ansi-color-regexp "\033\\[\\([0-9;]*\\)m" "Regexp that matches SGR control sequences.") @@ -187,142 +154,404 @@ removes `ansi-color-apply' for all shell-mode buffers." "Regexp that matches SGR control sequence parameters.") -;; Main functions +;; Convenience functions for comint modes (eg. shell-mode) -(defun ansi-color-filter-apply (s) - "Filter out all SGR control sequences from S. +(defcustom ansi-color-for-comint-mode nil + "Determines what to do with comint output. +If nil, do nothing. +If the symbol `filter', then filter all SGR control sequences. +If anything else (such as t), then translate SGR control sequences +into text-properties. -This function can be added to `comint-preoutput-filter-functions'." - (while (string-match ansi-color-regexp s) - (setq s (replace-match "" t t s))) - s) +In order for this to have any effect, `ansi-color-process-output' must +be in `comint-output-filter-functions'. +This can be used to enable colorized ls --color=yes output +in shell buffers. You set this variable by calling one of: +\\[ansi-color-for-comint-mode-on] +\\[ansi-color-for-comint-mode-off] +\\[ansi-color-for-comint-mode-filter]" + :version "20.8" + :type '(choice (const :tag "Do nothing" nil) + (const :tag "Filter" filter) + (const :tag "Translate" t)) + :group 'ansi-colors) -(defun ansi-color-filter-region (begin end) - "Filter out all SGR control sequences from region START END. - -Returns the first point it is safe to start with. Used to speedup -further processing. - -Design to cope with arbitrary chunk of output such as the ones get by -comint-output-filter-functions, e.g.: - -\(defvar last-context nil) -\(make-variable-buffer-local 'last-context) - -\(defun filter-out-color-in-buffer (s) - \(setq last-context - \(ansi-color-filter-region - \(if last-context - last-context - \(if (marker-position comint-last-output-start) - \(marker-position comint-last-output-start) - 1)) - \(marker-position (process-mark (get-buffer-process (current-buffer)))) )) - s) - -\(add-hook 'comint-output-filter-functions 'filter-out-color-in-buffer) -" - (let ((endm (copy-marker end))) - (save-excursion - (goto-char begin) - (while (re-search-forward ansi-color-regexp endm t) - (replace-match "")) - (if (re-search-forward "\033" endm t) - (match-beginning 0) - (marker-position endm))))) +(defun ansi-color-for-comint-mode-on () + "Set `ansi-color-for-comint-mode' to t." + (interactive) + (setq ansi-color-for-comint-mode t)) + +(defun ansi-color-for-comint-mode-off () + "Set `ansi-color-for-comint-mode' to nil." + (interactive) + (setq ansi-color-for-comint-mode nil)) + +(defun ansi-color-for-comint-mode-filter () + "Set `ansi-color-for-comint-mode' to symbol `filter'." + (interactive) + (setq ansi-color-for-comint-mode 'filter)) + +(defun ansi-color-process-output (string) + "Maybe translate SGR control sequences of comint output into text-properties. + +Depending on variable `ansi-color-for-comint-mode' the comint output is +either not processed, SGR control sequences are filtered using +`ansi-color-filter-region', or SGR control sequences are translated into +text-properties using `ansi-color-apply-on-region'. + +The comint output is assumed to lie between the marker +`comint-last-output-start' and the process-mark. + +This is a good function to put in `comint-output-filter-functions'." + (let ((start-marker (or comint-last-output-start + (point-min-marker))) + (end-marker (process-mark (get-buffer-process (current-buffer))))) + (cond ((eq ansi-color-for-comint-mode nil)) + ((eq ansi-color-for-comint-mode 'filter) + (ansi-color-filter-region start-marker end-marker)) + (t + (ansi-color-apply-on-region start-marker end-marker))))) + +(add-hook 'comint-output-filter-functions + 'ansi-color-process-output) + + +;; Alternative font-lock-unfontify-region-function + + +(eval-when-compile + ;; We use this to preserve or protect things when modifying text + ;; properties. Stolen from lazy-lock and font-lock. Ugly!!! + ;; Probably most of this is not needed? + (defmacro save-buffer-state (varlist &rest body) + "Bind variables according to VARLIST and eval BODY restoring buffer state." + (` (let* ((,@ (append varlist + '((modified (buffer-modified-p)) (buffer-undo-list t) + (inhibit-read-only t) (inhibit-point-motion-hooks t) + before-change-functions after-change-functions + deactivate-mark buffer-file-name buffer-file-truename)))) + (,@ body) + (when (and (not modified) (buffer-modified-p)) + (set-buffer-modified-p nil))))) + (put 'save-buffer-state 'lisp-indent-function 1)) + +(defun ansi-color-unfontify-region (beg end &rest xemacs-stuff) + "Replacement function for `font-lock-default-unfontify-region'. +When font-lock is active in a buffer, you cannot simply add face +text-properties to the buffer. Font-lock will remove the face +text-property using `font-lock-unfontify-region-function'. If you want +to insert the strings returned by `ansi-color-apply' into such buffers, +you must set `font-lock-unfontify-region-function' to +`ansi-color-unfontify-region'. This function will not remove all face +text-properties unconditionally. It will keep the face text-properties +if the property `ansi-color' is set. + +The region from BEG to END is unfontified. XEMACS-STUFF is ignored. + +A possible way to install this would be: + +\(add-hook 'font-lock-mode-hook + \(function (lambda () + \(setq font-lock-unfontify-region-function + 'ansi-color-unfontify-region))))" + ;; save-buffer-state is a macro in font-lock.el! + (save-buffer-state nil + (when (boundp 'font-lock-syntactic-keywords) + (remove-text-properties beg end '(syntax-table nil))) + ;; instead of just using (remove-text-properties beg end '(face + ;; nil)), we find regions with a non-nil face test-property, skip + ;; positions with the ansi-color property set, and remove the + ;; remaining face test-properties. + (while (setq beg (text-property-not-all beg end 'face nil)) + (setq beg (or (text-property-not-all beg end 'ansi-color t) end)) + (when (get-text-property beg 'face) + (let ((end-face (or (text-property-any beg end 'face nil) + end))) + (remove-text-properties beg end-face '(face nil)) + (setq beg end-face)))))) + +;; Working with strings + +(defvar ansi-color-context nil + "Context saved between two calls to `ansi-color-apply'. +This is a list of the form (FACES FRAGMENT) or nil. FACES is a list of +faces the last call to `ansi-color-apply' ended with, and FRAGMENT is a +string starting with an escape sequence, possibly the start of a new +escape sequence.") +(make-variable-buffer-local 'ansi-color-context) + +(defun ansi-color-filter-apply (string) + "Filter out all SGR control sequences from STRING. + +Every call to this function will set and use the buffer-local variable +`ansi-color-context' to save partial escape sequences. This information +will be used for the next call to `ansi-color-apply'. Set +`ansi-color-context' to nil if you don't want this. +This function can be added to `comint-preoutput-filter-functions'." + (let ((start 0) end result) + ;; if context was saved and is a string, prepend it + (if (cadr ansi-color-context) + (setq string (concat (cadr ansi-color-context) string) + ansi-color-context nil)) + ;; find the next escape sequence + (while (setq end (string-match ansi-color-regexp string start)) + (setq result (concat result (substring string start end)) + start (match-end 0))) + ;; save context, add the remainder of the string to the result + (let (fragment) + (if (string-match "\033" string start) + (let ((pos (match-beginning 0))) + (setq fragment (substring string pos) + result (concat result (substring string start pos)))) + (setq result (concat result (substring string start)))) + (if fragment + (setq ansi-color-context (list nil fragment)) + (setq ansi-color-context nil))) + result)) (defun ansi-color-apply (string) "Translates SGR control sequences into text-properties. Applies SGR control sequences setting foreground and background colors -to STRING and returns the result. The colors used are given in -`ansi-color-faces-vector' and `ansi-color-names-vector'. - -This function can be added to `comint-preoutput-filter-functions'." - (let (face (start 0) end escape-sequence null-sequence result) +to STRING using text-properties and returns the result. The colors used +are given in `ansi-color-faces-vector' and `ansi-color-names-vector'. +See function `ansi-color-apply-sequence' for details. + +Every call to this function will set and use the buffer-local variable +`ansi-color-context' to save partial escape sequences and current face. +This information will be used for the next call to `ansi-color-apply'. +Set `ansi-color-context' to nil if you don't want this. + +This function can be added to `comint-preoutput-filter-functions'. + +You cannot insert the strings returned into buffers using font-lock. +See `ansi-color-unfontify-region' for a way around this." + (let ((face (car ansi-color-context)) + (start 0) end escape-sequence result) + ;; if context was saved and is a string, prepend it + (if (cadr ansi-color-context) + (setq string (concat (cadr ansi-color-context) string) + ansi-color-context nil)) ;; find the next escape sequence (while (setq end (string-match ansi-color-regexp string start)) ;; store escape sequence - (setq escape-sequence (match-string 1 string) - null-sequence (string-equal escape-sequence "")) + (setq escape-sequence (match-string 1 string)) ;; colorize the old block from start to end using old face - (if face - (put-text-property start end 'face face string)) + (when face + (put-text-property start end 'ansi-color t string) + (put-text-property start end 'face face string)) (setq result (concat result (substring string start end)) start (match-end 0)) - ;; create new face by applying all the parameters in the escape sequence - (if null-sequence - (setq face nil) - (setq face (ansi-color-get-face escape-sequence)))) - (concat result (substring string start)))) + ;; create new face by applying all the parameters in the escape + ;; sequence + (setq face (ansi-color-apply-sequence escape-sequence face))) + ;; if the rest of the string should have a face, put it there + (when face + (put-text-property start (length string) 'ansi-color t string) + (put-text-property start (length string) 'face face string)) + ;; save context, add the remainder of the string to the result + (let (fragment) + (if (string-match "\033" string start) + (let ((pos (match-beginning 0))) + (setq fragment (substring string pos) + result (concat result (substring string start pos)))) + (setq result (concat result (substring string start)))) + (if (or face fragment) + (setq ansi-color-context (list face fragment)) + (setq ansi-color-context nil))) + result)) + +;; Working with regions + +(defvar ansi-color-context-region nil + "Context saved between two calls to `ansi-color-apply-on-region'. +This is a list of the form (FACES MARKER) or nil. FACES is a list of +faces the last call to `ansi-color-apply-on-region' ended with, and +MARKER is a buffer position within an escape sequence or the last +position processed.") +(make-variable-buffer-local 'ansi-color-context-region) +(defun ansi-color-filter-region (begin end) + "Filter out all SGR control sequences from region BEGIN to END. + +Every call to this function will set and use the buffer-local variable +`ansi-color-context-region' to save position. This information will be +used for the next call to `ansi-color-apply-on-region'. Specifically, +it will override BEGIN, the start of the region. Set +`ansi-color-context-region' to nil if you don't want this." + (let ((end-marker (copy-marker end)) + (start (or (cadr ansi-color-context-region) begin))) + (save-excursion + (goto-char start) + ;; find the next escape sequence + (while (re-search-forward ansi-color-regexp end-marker t) + ;; delete the escape sequence + (replace-match "")) + ;; save context, add the remainder of the string to the result + (if (re-search-forward "\033" end-marker t) + (setq ansi-color-context-region (list nil (match-beginning 0))) + (setq ansi-color-context-region nil))))) -(defun ansi-color-apply-on-region (begin end &optional context) - "Translates SGR control sequences into text-properties. +(defun ansi-color-apply-on-region (begin end) + "Translates SGR control sequences into overlays or extents. Applies SGR control sequences setting foreground and background colors -to text in region. The colors used are given in -`ansi-color-faces-vector' and `ansi-color-names-vector'. -Returns a context than can be used to speedup further processing. -Context is a (begin (start . face)) list. - -Design to cope with arbitrary chunk of output such as the ones get by -comint-output-filter-functions, e.g.: - -\(defvar last-context nil) -\(make-variable-buffer-local 'last-context) - -\(defun ansi-output-filter (s) - \(setq last-context - \(ansi-color-apply-on-region - \(if last-context - \(car last-context) - \(if (marker-position comint-last-output-start) - \(marker-position comint-last-output-start) - 1)) - \(process-mark (get-buffer-process (current-buffer))) - last-context )) - s) - -\(add-hook 'comint-output-filter-functions 'ansi-output-filter) -" - (let ((endm (copy-marker end)) - (face (if (and context (cdr context)) - (cdr (cdr context)))) - (face-start (if (and context (cdr context)) - (car (cdr context)))) - (next-safe-start begin) - escape-sequence - null-sequence - stop ) +to text in region between BEGIN and END using extents or overlays. +Emacs will use overlays, XEmacs will use extents. The colors used are +given in `ansi-color-faces-vector' and `ansi-color-names-vector'. See +function `ansi-color-apply-sequence' for details. + +Every call to this function will set and use the buffer-local variable +`ansi-color-context-region' to save position and current face. This +information will be used for the next call to +`ansi-color-apply-on-region'. Specifically, it will override BEGIN, the +start of the region and set the face with which to start. Set +`ansi-color-context-region' to nil if you don't want this." + (let ((face (car ansi-color-context-region)) + (start-marker (or (cadr ansi-color-context-region) + (copy-marker begin))) + (end-marker (copy-marker end)) + escape-sequence) (save-excursion - (goto-char begin) + (goto-char start-marker) ;; find the next escape sequence - (while (setq stop (re-search-forward ansi-color-regexp endm t)) - ;; store escape sequence - (setq escape-sequence (match-string 1)) - (setq null-sequence (string-equal (match-string 1) "")) - (setq next-safe-start (match-beginning 0)) - (if face - (put-text-property face-start next-safe-start 'face face)) ; colorize - (replace-match "") ; delete the ANSI sequence - (if null-sequence - (setq face nil) - (setq face-start next-safe-start) - (setq face (ansi-color-get-face escape-sequence)))) - (setq next-safe-start - (if (re-search-forward "\033" endm t) - (match-beginning 0) - (marker-position endm)))) - (cons next-safe-start - (if face - (cons face-start face))) )) + (while (re-search-forward ansi-color-regexp end-marker t) + ;; colorize the old block from start to end using old face + (when face + (ansi-color-set-extent-face + (ansi-color-make-extent start-marker (match-beginning 0)) + face)) + ;; store escape sequence and new start position + (setq escape-sequence (match-string 1) + start-marker (copy-marker (match-end 0))) + ;; delete the escape sequence + (replace-match "") + ;; create new face by applying all the parameters in the escape + ;; sequence + (setq face (ansi-color-apply-sequence escape-sequence face))) + ;; search for the possible start of a new escape sequence + (if (re-search-forward "\033" end-marker t) + (progn + ;; if the rest of the region should have a face, put it there + (when face + (ansi-color-set-extent-face + (ansi-color-make-extent start-marker (point)) + face)) + ;; save face and point + (setq ansi-color-context-region + (list face (copy-marker (match-beginning 0))))) + ;; if the rest of the region should have a face, put it there + (if face + (progn + (ansi-color-set-extent-face + (ansi-color-make-extent start-marker end-marker) + face) + (setq ansi-color-context-region (list face))) + ;; reset context + (setq ansi-color-context-region nil)))))) + +;; This function helps you look for overlapping overlays. This is +;; usefull in comint-buffers. Overlapping overlays should not happen! +;; A possible cause for bugs are the markers. If you create an overlay +;; up to the end of the region, then that end might coincide with the +;; process-mark. As text is added BEFORE the process-mark, the overlay +;; will keep growing. Therefore, as more overlays are created later on, +;; there will be TWO OR MORE overlays covering the buffer at that point. +;; This function helps you check your buffer for these situations. +; (defun ansi-color-debug-overlays () +; (interactive) +; (let ((pos (point-min))) +; (while (< pos (point-max)) +; (if (<= 2 (length (overlays-at pos))) +; (progn +; (goto-char pos) +; (error "%d overlays at %d" (length (overlays-at pos)) pos)) +; (let (message-log-max) +; (message "Reached %d." pos))) +; (setq pos (next-overlay-change pos))))) + +;; Emacs/XEmacs compatibility layer + +(defun ansi-color-make-face (property color) + "Return a face with PROPERTY set to COLOR. +PROPERTY can be either symbol `foreground' or symbol `background'. + +For Emacs, we just return the cons cell \(PROPERTY . COLOR). +For XEmacs, we create a temporary face and return it." + (if (featurep 'xemacs) + (let ((face (make-face (intern (concat color "-" (symbol-name property))) + "Temporary face created by ansi-color." + t))) + (set-face-property face property color) + face) + (cond ((eq property 'foreground) + (cons 'foreground-color color)) + ((eq property 'background) + (cons 'background-color color)) + (t + (cons property color))))) + +(defun ansi-color-make-extent (from to &optional object) + "Make an extent for the range [FROM, TO) in OBJECT. + +OBJECT defaults to the current buffer. XEmacs uses `make-extent', Emacs +uses `make-overlay'. XEmacs can use a buffer or a string for OBJECT, +Emacs requires OBJECT to be a buffer." + (if (functionp 'make-extent) + (make-extent from to object) + ;; In Emacs, the overlay might end at the process-mark in comint + ;; buffers. In that case, new text will be inserted before the + ;; process-mark, ie. inside the overlay (using insert-before-marks). + ;; In order to avoid this, we use the `insert-behind-hooks' overlay + ;; property to make sure it works. + (let ((overlay (make-overlay from to object))) + (overlay-put overlay 'modification-hooks '(ansi-color-freeze-overlay)) + overlay))) + +(defun ansi-color-freeze-overlay (overlay is-after begin end &optional len) + "Prevent OVERLAY from being extended. +This function can be used for the `modification-hooks' overlay +property." + ;; if stuff was inserted at the end of the overlay + (when (and is-after + (= 0 len) + (= end (overlay-end overlay))) + ;; reset the end of the overlay + (move-overlay overlay (overlay-start overlay) begin))) + +(defun ansi-color-set-extent-face (extent face) + "Set the `face' property of EXTENT to FACE. +XEmacs uses `set-extent-face', Emacs uses `overlay-put'." + (if (functionp 'set-extent-face) + (set-extent-face extent face) + (overlay-put extent 'face face))) ;; Helper functions +(defun ansi-color-apply-sequence (escape-sequence faces) + "Apply ESCAPE-SEQ to FACES and return the new list of faces. + +ESCAPE-SEQ is an escape sequences parsed by `ansi-color-get-face'. + +If the new faces start with the symbol `default', then the new +faces are returned. If the faces start with something else, +they are appended to the front of the FACES list, and the new +list of faces is returned. + +If `ansi-color-get-face' returns nil, then we either got a +null-sequence, or we stumbled upon some garbage. In either +case we return nil." + (let ((new-faces (ansi-color-get-face escape-sequence))) + (cond ((null new-faces) + nil) + ((eq (car new-faces) 'default) + (cdr new-faces)) + (t + (append new-faces face))))) + (defun ansi-color-make-color-map () "Creates a vector of face definitions and returns it. @@ -339,28 +568,26 @@ The face definitions are based upon the variables (aset ansi-color-map index e) (setq index (1+ index)) )) ansi-color-faces-vector) - ;; foreground attributes (setq index 30) (mapcar (function (lambda (e) (aset ansi-color-map index - (cons 'foreground-color e)) + (ansi-color-make-face 'foreground e)) (setq index (1+ index)) )) ansi-color-names-vector) - ;; background attributes (setq index 40) (mapcar (function (lambda (e) (aset ansi-color-map index - (cons 'background-color e)) + (ansi-color-make-face 'background e)) (setq index (1+ index)) )) ansi-color-names-vector) ansi-color-map)) (defvar ansi-color-map (ansi-color-make-color-map) - "A brand new color map suitable for ansi-color-get-face. + "A brand new color map suitable for `ansi-color-get-face'. The value of this variable is usually constructed by `ansi-color-make-color-map'. The values in the array are such that the @@ -390,16 +617,23 @@ ANSI-CODE is used as an index into the vector." (defun ansi-color-get-face (escape-seq) "Create a new face by applying all the parameters in ESCAPE-SEQ. -ESCAPE-SEQ is a SGR control sequences such as \033[34m. The parameter +Should any of the parameters result in the default face (usually this is +the parameter 0), then the effect of all previous parameters is cancelled. + +ESCAPE-SEQ is a SGR control sequences such as \\033[34m. The parameter 34 is used by `ansi-color-get-face-1' to return a face definition." (let ((ansi-color-r "[0-9][0-9]?") (i 0) - f) + f val) (while (string-match ansi-color-r escape-seq i) - (setq i (match-end 0)) - (add-to-list 'f - (ansi-color-get-face-1 - (string-to-int (match-string 0 escape-seq) 10)))) + (setq i (match-end 0) + val (ansi-color-get-face-1 + (string-to-int (match-string 0 escape-seq) 10))) + (cond ((not val)) + ((eq val 'default) + (setq f (list val))) + (t + (add-to-list 'f val)))) f)) (provide 'ansi-color) -- cgit v1.2.1