aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lisp/ansi-color.el180
1 files changed, 180 insertions, 0 deletions
diff --git a/lisp/ansi-color.el b/lisp/ansi-color.el
new file mode 100644
index 00000000000..bab5c468b0d
--- /dev/null
+++ b/lisp/ansi-color.el
@@ -0,0 +1,180 @@
1;;; ansi-color.el -- translate ANSI into text-properties
2
3;; Copyright (C) 1999 Free Software Foundation, Inc.
4
5;; Author: Alex Schroeder <a.schroeder@bsiag.ch>
6;; Maintainer: Alex Schroeder <a.schroeder@bsiag.ch>
7;; Version: 1.1.1
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 "bold and blue, bold and blue!!")
70;; (ansi-color-to-text-properties line)
71;; (insert line))
72
73;;; Bugs:
74
75;; Only supports the ANSI sequences that the MUSH I'm on uses (the MUSH
76;; is Elendor, see http://www.elendor.net). To see the list of codes
77;; supported I did a `help ansi()'. Based on this information, I used
78;; TinyTalk.el (without ANSI color support), gave myself the ANSI color
79;; flags using `@set me=ANSI' and `@set me=COLOR', and noted the ANSI
80;; escape sequences produced by the MUSH using `think ansi(r,red)' for
81;; example.
82;;
83;; The code is spaghetti-code, I hate it.
84
85
86
87;;; Code:
88
89(defvar ansi-color-faces-vector
90 [default bold default default underline bold default modeline]
91 "Faces used for ANSI control sequences determining a face.
92
93Those are sequences like this one: , where 1 could be one of the
94following numbers: 0 (default), 1 (hilight, rendered as bold), 4
95(underline), 5 (flashing, rendered as bold), 7 (inverse, rendered the
96same as the modeline)")
97
98(defvar ansi-color-names-vector
99 ["black" "red" "green" "yellow" "blue" "magenta" "cyan" "white"]
100 "Array of colors.
101
102Used for sequences like this one: , where 1 could be an index to a
103foreground color (red, in this case), or , where 1 could be an
104index to a background color.
105
106The default colors are: black, red, green, yellow, blue, magenta,
107cyan, and white.
108
109On a light background, I prefer: black, red, dark green, orange, blue,
110magenta, turquoise, snow4")
111
112;; The main function
113
114(defun ansi-color-to-text-properties (str)
115 "Translates ANSI color control sequences into text-properties.
116
117The ANSI control sequences are made invisible. The text-properties are
118added to the string given in the parameter STR."
119 ;; ANSI code for highlighting, example: boringINTERESTINGboring
120 ;; state: start with 0, "" -> 1, "[" -> 2, "[013457]" -> 3,
121 ;; "[013457]" -> 4, "m" -> back to 0!
122 ;; param: stored when state is 3 (in the above example: 1)
123 (let ((str-length (length str))
124 (face '(default))
125 (i 0) (char) (state 0) (param1) (param2))
126 (while (< i str-length)
127 (setq char (aref str i))
128 (cond
129 ;; Eeither just finished an ANSI control squence (state 4) or
130 ;; wrote normal chars (state 0).
131 ((and (or (= state 0) (= state 4)) (= char ?))
132 (setq state 1)); saw escape
133 ((and (= state 1) (= char ?\[)); seen escape
134 (setq state 2
135 param1 nil
136 param2 nil)); saw [, prepare for param1 and param2!
137 ((and (or (= state 2) (= state 3)); reading first or second digit
138 (string-match "[01234567]" (substring str i (1+ i))))
139 (if (= state 2); reading first digit
140 ;;  (hilight)
141 (setq param1 (string-to-number (substring str i (1+ i)))
142 state 3); prepare to read a second digit or quit.
143 ;; if reading second digit
144 ;; such as  (green foreground)
145 (setq param2 (string-to-number (substring str i (1+ i)))
146 state 4))); read second digit, prepare to quit
147 ((and (or (= state 3) (= state 4)) (= char ?m)); reading last char: m
148 (setq state 4); state 4: m will be invisible. Now reset face
149 ;; according to param1 and param2.
150 (if (null param2); only param1 set: no color changes!
151 ;; : default face
152 (if (= param1 0)
153 (setq face '(default))
154 ;; : hilight, : inverse, : underline, etc.
155 (add-to-list 'face (aref ansi-color-faces-vector param1)))
156 ;; If param2 is set, we are changing back- or foreground color.
157 (if (= param1 3); first digit told us to change foreground
158 ;; : red foreground
159 (add-to-list 'face (cons 'foreground-color
160 (aref ansi-color-names-vector param2)))
161 ;; : green background
162 (add-to-list 'face (cons 'background-color
163 (aref ansi-color-names-vector param2))))))
164 (t (setq state 0))); all other cases, state is 0.
165
166 ;; Set text-property for every char.
167 (if (> state 0); if reading ANSI codes, state > 0: make them
168 ; invisible.
169 (put-text-property i (1+ i) 'invisible t str)
170 ;; if reading normal chars, state is 0, put them in the
171 ;; current face.
172 (put-text-property i (1+ i) 'face face str))
173 ;; next char
174 (setq i (1+ i)))))
175
176(provide 'ansi-color)
177
178;;; ansi-colors.el ends here
179
180