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