Mercurial > emacs
annotate lisp/ansi-color.el @ 25506:ba0ad349e792
(SPLIT_COMPOSITE_SEQ): New macro.
(SPLIT_CHARACTER_SEQ): New macro.
(SPLIT_MULTIBYTE_SEQ): New macro.
(CHAR_COMPONENT_VALID_P): New macro.
(non_ascii_char_to_string): Generate a multibyte sequence as far
as possible.
(string_to_non_ascii_char): The 4th arg exclude_tail_garbage is
deleted. Caller changed. Use the macro SPLIT_MULTIBYTE_SEQ.
(split_non_ascii_string): Likewise.
(multibyte_form_length): Use the macro PARSE_MULTIBYTE_SEQ.
(char_printable_p): New function.
(translate_char): Check character by NATNUMP instead of INTEGERP.
(unibyte_char_to_multibyte): Call char_valid_p instead of
VALID_MULTIBYTE_CHAR_P.
(Fmake_char_internal): Check the arguments more rigidly.
(Fcharset_after): Use the macro SPLIT_MULTIBYTE_SEQ.
(char_valid_p): Check the validity by CHAR_COMPONENT_VALID_P.
(Fmultibyte_char_to_unibyte): Check the validity of character by
CHAR_VALID_P.
(chars_in_text): Call multibyte_chars_in_text.
(multibyte_chars_in_text): Use the macro PARSE_MULTIBYTE_SEQ.
(Fcompose_string): Use the macro STRING_CHAR_AND_LENGTH instead of
STRING_CHAR_AND_CHAR_LENGTH (which is obsolete now).
author | Kenichi Handa <handa@m17n.org> |
---|---|
date | Fri, 03 Sep 1999 01:28:42 +0000 |
parents | 38f98813a83d |
children | 8080bafbae4b |
rev | line source |
---|---|
25171 | 1 ;;; ansi-color.el -- translate ANSI into text-properties |
2 | |
3 ;; Copyright (C) 1999 Free Software Foundation, Inc. | |
4 | |
25225
38f98813a83d
(ansi-color-to-text-properties): Added New state 5
Karl Heuer <kwzh@gnu.org>
parents:
25171
diff
changeset
|
5 ;; Author: Alex Schroeder <alex@gnu.ch> |
38f98813a83d
(ansi-color-to-text-properties): Added New state 5
Karl Heuer <kwzh@gnu.org>
parents:
25171
diff
changeset
|
6 ;; Maintainer: Alex Schroeder <alex@gnu.ch> |
38f98813a83d
(ansi-color-to-text-properties): Added New state 5
Karl Heuer <kwzh@gnu.org>
parents:
25171
diff
changeset
|
7 ;; Version: 1.2.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 | |
29 ;; You can get the latest version of this file from my homepage | |
30 ;; <URL:http://www.geocities.com/TimesSquare/6120/emacs.html>. | |
31 ;; | |
32 ;; This file provides a function that takes a string containing ANSI | |
33 ;; control sequences and tries to replace these with text-properties. | |
34 ;; | |
35 ;; I was unable to extract this functionality from term.el for another | |
36 ;; program I wanted to extend (the MUSH client TinyTalk.el), so I had to | |
37 ;; rewrite this. | |
38 | |
39 ;; In order to install this with TinyMush.el, add the following to your | |
40 ;; .emacs file: | |
41 ;; | |
42 ;; (setq tinymud-filter-line-hook 'my-tinymud-add-ansi-text-properties) | |
43 ;; (autoload 'ansi-color-to-text-properties "ansi-color" | |
44 ;; "Translates ANSI color control sequences into text-properties." t) | |
45 ;; (defun my-tinymud-add-ansi-text-properties (conn line) | |
46 ;; "Call `ansi-color-to-text-properties' for LINE. | |
47 ;; Ignores CONN and returns nil, so that `tinymud-filter-line' continues to | |
48 ;; process triggers and everything else." | |
49 ;; (ansi-color-to-text-properties line) | |
50 ;; nil) | |
51 | |
52 ;; If the ANSI sequences assume that you have a black background, you'll | |
53 ;; have to display the stuff in a frame with a black background. You | |
54 ;; can create such a frame like this (it still looks ugly!): | |
55 ;; | |
56 ;; (defun my-black-frame () | |
57 ;; "Create a frame with black background." | |
58 ;; (interactive) | |
59 ;; (make-frame '((foreground-color . "white") | |
60 ;; (background-color . "black")))) | |
61 | |
62 ;;; Testing: | |
63 | |
64 ;; If you want to test the setup, evaluate the following fragment in a | |
65 ;; buffer without font-lock-mode. This doesn't work in buffers that | |
66 ;; have font-lock-mode! | |
67 ;; | |
68 ;; (progn | |
69 ;; (setq line "[1mbold[0m and [34mblue[0m, [1m[34mbold and blue[0m!!") | |
70 ;; (ansi-color-to-text-properties line) | |
71 ;; (insert line)) | |
25225
38f98813a83d
(ansi-color-to-text-properties): Added New state 5
Karl Heuer <kwzh@gnu.org>
parents:
25171
diff
changeset
|
72 ;; |
38f98813a83d
(ansi-color-to-text-properties): Added New state 5
Karl Heuer <kwzh@gnu.org>
parents:
25171
diff
changeset
|
73 ;; Other test strings: (m-eating-bug) "[1mmold[0m should be mold" |
25171 | 74 |
75 ;;; Bugs: | |
76 | |
25225
38f98813a83d
(ansi-color-to-text-properties): Added New state 5
Karl Heuer <kwzh@gnu.org>
parents:
25171
diff
changeset
|
77 ;; 1. Only supports the ANSI sequences that the MUSH I'm on uses (the |
38f98813a83d
(ansi-color-to-text-properties): Added New state 5
Karl Heuer <kwzh@gnu.org>
parents:
25171
diff
changeset
|
78 ;; MUSH is Elendor, see http://www.elendor.net). To see the list of |
38f98813a83d
(ansi-color-to-text-properties): Added New state 5
Karl Heuer <kwzh@gnu.org>
parents:
25171
diff
changeset
|
79 ;; codes supported I did a `help ansi()'. Based on this information, |
38f98813a83d
(ansi-color-to-text-properties): Added New state 5
Karl Heuer <kwzh@gnu.org>
parents:
25171
diff
changeset
|
80 ;; I used TinyTalk.el (without ANSI color support), gave myself the |
38f98813a83d
(ansi-color-to-text-properties): Added New state 5
Karl Heuer <kwzh@gnu.org>
parents:
25171
diff
changeset
|
81 ;; ANSI color flags using `@set me=ANSI' and `@set me=COLOR', and |
38f98813a83d
(ansi-color-to-text-properties): Added New state 5
Karl Heuer <kwzh@gnu.org>
parents:
25171
diff
changeset
|
82 ;; noted the ANSI escape sequences produced by the MUSH using `think |
38f98813a83d
(ansi-color-to-text-properties): Added New state 5
Karl Heuer <kwzh@gnu.org>
parents:
25171
diff
changeset
|
83 ;; ansi(r,red)' for example. |
25171 | 84 ;; |
25225
38f98813a83d
(ansi-color-to-text-properties): Added New state 5
Karl Heuer <kwzh@gnu.org>
parents:
25171
diff
changeset
|
85 ;; 2. The code is spaghetti-code, I hate it. |
38f98813a83d
(ansi-color-to-text-properties): Added New state 5
Karl Heuer <kwzh@gnu.org>
parents:
25171
diff
changeset
|
86 ;; |
38f98813a83d
(ansi-color-to-text-properties): Added New state 5
Karl Heuer <kwzh@gnu.org>
parents:
25171
diff
changeset
|
87 ;; 3. If a squence of chars looks like the start of an ANSI sequence, |
38f98813a83d
(ansi-color-to-text-properties): Added New state 5
Karl Heuer <kwzh@gnu.org>
parents:
25171
diff
changeset
|
88 ;; the chars will be set invisible. If the squence of chars turns |
38f98813a83d
(ansi-color-to-text-properties): Added New state 5
Karl Heuer <kwzh@gnu.org>
parents:
25171
diff
changeset
|
89 ;; out not to be an ANSI sequence, this is not undone. Here is a |
38f98813a83d
(ansi-color-to-text-properties): Added New state 5
Karl Heuer <kwzh@gnu.org>
parents:
25171
diff
changeset
|
90 ;; teststring: "Is '[3' visible as ^[[3?" This could be solved by |
38f98813a83d
(ansi-color-to-text-properties): Added New state 5
Karl Heuer <kwzh@gnu.org>
parents:
25171
diff
changeset
|
91 ;; using `state': it shows most of the time how many characters have |
38f98813a83d
(ansi-color-to-text-properties): Added New state 5
Karl Heuer <kwzh@gnu.org>
parents:
25171
diff
changeset
|
92 ;; been set invisible. |
25171 | 93 |
94 | |
95 | |
96 ;;; Code: | |
97 | |
98 (defvar ansi-color-faces-vector | |
99 [default bold default default underline bold default modeline] | |
100 "Faces used for ANSI control sequences determining a face. | |
101 | |
102 Those are sequences like this one: [1m, where 1 could be one of the | |
103 following numbers: 0 (default), 1 (hilight, rendered as bold), 4 | |
104 (underline), 5 (flashing, rendered as bold), 7 (inverse, rendered the | |
105 same as the modeline)") | |
106 | |
107 (defvar ansi-color-names-vector | |
108 ["black" "red" "green" "yellow" "blue" "magenta" "cyan" "white"] | |
109 "Array of colors. | |
110 | |
111 Used for sequences like this one: [31m, where 1 could be an index to a | |
112 foreground color (red, in this case), or [41m, where 1 could be an | |
113 index to a background color. | |
114 | |
115 The default colors are: black, red, green, yellow, blue, magenta, | |
116 cyan, and white. | |
117 | |
118 On a light background, I prefer: black, red, dark green, orange, blue, | |
119 magenta, turquoise, snow4") | |
120 | |
121 ;; The main function | |
122 | |
123 (defun ansi-color-to-text-properties (str) | |
124 "Translates ANSI color control sequences into text-properties. | |
125 | |
126 The ANSI control sequences are made invisible. The text-properties are | |
127 added to the string given in the parameter STR." | |
128 ;; ANSI code for highlighting, example: boring[1mINTERESTING[0mboring | |
129 ;; state: start with 0, "" -> 1, "[" -> 2, "[013457]" -> 3, | |
130 ;; "[013457]" -> 4, "m" -> back to 0! | |
131 ;; param: stored when state is 3 (in the above example: 1) | |
132 (let ((str-length (length str)) | |
133 (face '(default)) | |
134 (i 0) (char) (state 0) (param1) (param2)) | |
135 (while (< i str-length) | |
136 (setq char (aref str i)) | |
137 (cond | |
25225
38f98813a83d
(ansi-color-to-text-properties): Added New state 5
Karl Heuer <kwzh@gnu.org>
parents:
25171
diff
changeset
|
138 ;; When writing normal chars (state 0) and happening upon an ANSI sequence. |
38f98813a83d
(ansi-color-to-text-properties): Added New state 5
Karl Heuer <kwzh@gnu.org>
parents:
25171
diff
changeset
|
139 ((and (= state 0) (= char ?)) |
25171 | 140 (setq state 1)); saw escape |
141 ((and (= state 1) (= char ?\[)); seen escape | |
142 (setq state 2 | |
143 param1 nil | |
144 param2 nil)); saw [, prepare for param1 and param2! | |
145 ((and (or (= state 2) (= state 3)); reading first or second digit | |
146 (string-match "[01234567]" (substring str i (1+ i)))) | |
147 (if (= state 2); reading first digit | |
148 ;; [1m (hilight) | |
149 (setq param1 (string-to-number (substring str i (1+ i))) | |
150 state 3); prepare to read a second digit or quit. | |
151 ;; if reading second digit | |
152 ;; such as [32m (green foreground) | |
153 (setq param2 (string-to-number (substring str i (1+ i))) | |
154 state 4))); read second digit, prepare to quit | |
155 ((and (or (= state 3) (= state 4)) (= char ?m)); reading last char: m | |
25225
38f98813a83d
(ansi-color-to-text-properties): Added New state 5
Karl Heuer <kwzh@gnu.org>
parents:
25171
diff
changeset
|
156 (setq state 5); state 5: m will be last invisible char. Now |
38f98813a83d
(ansi-color-to-text-properties): Added New state 5
Karl Heuer <kwzh@gnu.org>
parents:
25171
diff
changeset
|
157 ;; reset face according to param1 and param2. |
25171 | 158 (if (null param2); only param1 set: no color changes! |
159 ;; [0m: default face | |
160 (if (= param1 0) | |
161 (setq face '(default)) | |
162 ;; [1m: hilight, [7m: inverse, [4m: underline, etc. | |
163 (add-to-list 'face (aref ansi-color-faces-vector param1))) | |
164 ;; If param2 is set, we are changing back- or foreground color. | |
165 (if (= param1 3); first digit told us to change foreground | |
166 ;; [31m: red foreground | |
167 (add-to-list 'face (cons 'foreground-color | |
168 (aref ansi-color-names-vector param2))) | |
169 ;; [42m: green background | |
170 (add-to-list 'face (cons 'background-color | |
171 (aref ansi-color-names-vector param2)))))) | |
172 (t (setq state 0))); all other cases, state is 0. | |
173 | |
174 ;; Set text-property for every char. | |
175 (if (> state 0); if reading ANSI codes, state > 0: make them | |
176 ; invisible. | |
177 (put-text-property i (1+ i) 'invisible t str) | |
178 ;; if reading normal chars, state is 0, put them in the | |
179 ;; current face. | |
180 (put-text-property i (1+ i) 'face face str)) | |
25225
38f98813a83d
(ansi-color-to-text-properties): Added New state 5
Karl Heuer <kwzh@gnu.org>
parents:
25171
diff
changeset
|
181 |
38f98813a83d
(ansi-color-to-text-properties): Added New state 5
Karl Heuer <kwzh@gnu.org>
parents:
25171
diff
changeset
|
182 ;; Debug: (message "%c: %d" char state) |
38f98813a83d
(ansi-color-to-text-properties): Added New state 5
Karl Heuer <kwzh@gnu.org>
parents:
25171
diff
changeset
|
183 |
38f98813a83d
(ansi-color-to-text-properties): Added New state 5
Karl Heuer <kwzh@gnu.org>
parents:
25171
diff
changeset
|
184 ;; If we just finished reading an ANSI sequence (state 5), reset |
38f98813a83d
(ansi-color-to-text-properties): Added New state 5
Karl Heuer <kwzh@gnu.org>
parents:
25171
diff
changeset
|
185 ;; state (state 0). |
38f98813a83d
(ansi-color-to-text-properties): Added New state 5
Karl Heuer <kwzh@gnu.org>
parents:
25171
diff
changeset
|
186 (if (> state 4) (setq state 0)) |
38f98813a83d
(ansi-color-to-text-properties): Added New state 5
Karl Heuer <kwzh@gnu.org>
parents:
25171
diff
changeset
|
187 ;; Next char |
25171 | 188 (setq i (1+ i))))) |
189 | |
190 (provide 'ansi-color) | |
191 | |
192 ;;; ansi-colors.el ends here | |
193 | |
194 |