Commit | Line | Data |
---|---|---|
618206ea RS |
1 | ;;; ansi-color.el -- translate ANSI into text-properties |
2 | ||
3 | ;; Copyright (C) 1999 Free Software Foundation, Inc. | |
4 | ||
18e260d4 KH |
5 | ;; Author: Alex Schroeder <alex@gnu.ch> |
6 | ;; Maintainer: Alex Schroeder <alex@gnu.ch> | |
7 | ;; Version: 1.2.0 | |
618206ea RS |
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 "\e[1mbold\e[0m and \e[34mblue\e[0m, \e[1m\e[34mbold and blue\e[0m!!") | |
70 | ;; (ansi-color-to-text-properties line) | |
71 | ;; (insert line)) | |
18e260d4 KH |
72 | ;; |
73 | ;; Other test strings: (m-eating-bug) "\e[1mmold\e[0m should be mold" | |
618206ea RS |
74 | |
75 | ;;; Bugs: | |
76 | ||
18e260d4 KH |
77 | ;; 1. Only supports the ANSI sequences that the MUSH I'm on uses (the |
78 | ;; MUSH is Elendor, see http://www.elendor.net). To see the list of | |
79 | ;; codes supported I did a `help ansi()'. Based on this information, | |
80 | ;; I used TinyTalk.el (without ANSI color support), gave myself the | |
81 | ;; ANSI color flags using `@set me=ANSI' and `@set me=COLOR', and | |
82 | ;; noted the ANSI escape sequences produced by the MUSH using `think | |
83 | ;; ansi(r,red)' for example. | |
84 | ;; | |
85 | ;; 2. The code is spaghetti-code, I hate it. | |
618206ea | 86 | ;; |
18e260d4 KH |
87 | ;; 3. If a squence of chars looks like the start of an ANSI sequence, |
88 | ;; the chars will be set invisible. If the squence of chars turns | |
89 | ;; out not to be an ANSI sequence, this is not undone. Here is a | |
90 | ;; teststring: "Is '\e[3' visible as ^[[3?" This could be solved by | |
91 | ;; using `state': it shows most of the time how many characters have | |
92 | ;; been set invisible. | |
618206ea RS |
93 | |
94 | \f | |
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: \e[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: \e[31m, where 1 could be an index to a | |
112 | foreground color (red, in this case), or \e[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\e[1mINTERESTING\e[0mboring | |
129 | ;; state: start with 0, "\e" -> 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 | |
18e260d4 KH |
138 | ;; When writing normal chars (state 0) and happening upon an ANSI sequence. |
139 | ((and (= state 0) (= char ?\e)) | |
618206ea RS |
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 | ;; \e[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 \e[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 | |
18e260d4 KH |
156 | (setq state 5); state 5: m will be last invisible char. Now |
157 | ;; reset face according to param1 and param2. | |
618206ea RS |
158 | (if (null param2); only param1 set: no color changes! |
159 | ;; \e[0m: default face | |
160 | (if (= param1 0) | |
161 | (setq face '(default)) | |
162 | ;; \e[1m: hilight, \e[7m: inverse, \e[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 | ;; \e[31m: red foreground | |
167 | (add-to-list 'face (cons 'foreground-color | |
168 | (aref ansi-color-names-vector param2))) | |
169 | ;; \e[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)) | |
18e260d4 KH |
181 | |
182 | ;; Debug: (message "%c: %d" char state) | |
183 | ||
184 | ;; If we just finished reading an ANSI sequence (state 5), reset | |
185 | ;; state (state 0). | |
186 | (if (> state 4) (setq state 0)) | |
187 | ;; Next char | |
618206ea RS |
188 | (setq i (1+ i))))) |
189 | ||
190 | (provide 'ansi-color) | |
191 | ||
192 | ;;; ansi-colors.el ends here | |
193 | ||
194 |