annotate lisp/ansi-color.el @ 26061:1ca697674a40

*** empty log message ***
author Gerd Moellmann <gerd@gnu.org>
date Sun, 17 Oct 1999 12:57:38 +0000
parents 38f98813a83d
children 8080bafbae4b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
25171
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1 ;;; ansi-color.el -- translate ANSI into text-properties
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
2
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
3 ;; Copyright (C) 1999 Free Software Foundation, Inc.
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
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
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
8 ;; Keywords: comm processes
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
9
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
10 ;; This file is part of GNU Emacs.
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
11
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
12 ;; GNU Emacs is free software; you can redistribute it and/or modify it
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
13 ;; under the terms of the GNU General Public License as published by the
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
14 ;; Free Software Foundation; either version 2, or (at your option) any
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
15 ;; later version.
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
16 ;;
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
17 ;; GNU Emacs is distributed in the hope that it will be useful, but
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
18 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
20 ;; General Public License for more details.
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
21 ;;
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
22 ;; You should have received a copy of the GNU General Public License
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
25 ;; Boston, MA 02111-1307, USA.
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
26
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
27 ;;; Commentary:
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
28
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
29 ;; You can get the latest version of this file from my homepage
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
30 ;; <URL:http://www.geocities.com/TimesSquare/6120/emacs.html>.
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
31 ;;
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
32 ;; This file provides a function that takes a string containing ANSI
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
33 ;; control sequences and tries to replace these with text-properties.
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
34 ;;
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
35 ;; I was unable to extract this functionality from term.el for another
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
36 ;; program I wanted to extend (the MUSH client TinyTalk.el), so I had to
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
37 ;; rewrite this.
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
38
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
39 ;; In order to install this with TinyMush.el, add the following to your
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
40 ;; .emacs file:
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
41 ;;
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
42 ;; (setq tinymud-filter-line-hook 'my-tinymud-add-ansi-text-properties)
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
43 ;; (autoload 'ansi-color-to-text-properties "ansi-color"
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
44 ;; "Translates ANSI color control sequences into text-properties." t)
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
45 ;; (defun my-tinymud-add-ansi-text-properties (conn line)
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
46 ;; "Call `ansi-color-to-text-properties' for LINE.
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
47 ;; Ignores CONN and returns nil, so that `tinymud-filter-line' continues to
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
48 ;; process triggers and everything else."
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
49 ;; (ansi-color-to-text-properties line)
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
50 ;; nil)
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
51
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
52 ;; If the ANSI sequences assume that you have a black background, you'll
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
53 ;; have to display the stuff in a frame with a black background. You
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
54 ;; can create such a frame like this (it still looks ugly!):
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
55 ;;
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
56 ;; (defun my-black-frame ()
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
57 ;; "Create a frame with black background."
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
58 ;; (interactive)
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
59 ;; (make-frame '((foreground-color . "white")
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
60 ;; (background-color . "black"))))
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
61
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
62 ;;; Testing:
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
63
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
64 ;; If you want to test the setup, evaluate the following fragment in a
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
65 ;; buffer without font-lock-mode. This doesn't work in buffers that
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
66 ;; have font-lock-mode!
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
67 ;;
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
68 ;; (progn
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
69 ;; (setq line "bold and blue, bold and blue!!")
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
70 ;; (ansi-color-to-text-properties line)
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
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) "mold should be mold"
25171
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
74
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
75 ;;; Bugs:
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
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
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
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
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
93
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
94
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
95
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
96 ;;; Code:
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
97
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
98 (defvar ansi-color-faces-vector
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
99 [default bold default default underline bold default modeline]
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
100 "Faces used for ANSI control sequences determining a face.
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
101
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
102 Those are sequences like this one: , where 1 could be one of the
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
103 following numbers: 0 (default), 1 (hilight, rendered as bold), 4
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
104 (underline), 5 (flashing, rendered as bold), 7 (inverse, rendered the
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
105 same as the modeline)")
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
106
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
107 (defvar ansi-color-names-vector
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
108 ["black" "red" "green" "yellow" "blue" "magenta" "cyan" "white"]
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
109 "Array of colors.
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
110
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
111 Used for sequences like this one: , where 1 could be an index to a
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
112 foreground color (red, in this case), or , where 1 could be an
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
113 index to a background color.
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
114
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
115 The default colors are: black, red, green, yellow, blue, magenta,
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
116 cyan, and white.
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
117
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
118 On a light background, I prefer: black, red, dark green, orange, blue,
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
119 magenta, turquoise, snow4")
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
120
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
121 ;; The main function
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
122
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
123 (defun ansi-color-to-text-properties (str)
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
124 "Translates ANSI color control sequences into text-properties.
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
125
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
126 The ANSI control sequences are made invisible. The text-properties are
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
127 added to the string given in the parameter STR."
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
128 ;; ANSI code for highlighting, example: boringINTERESTINGboring
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
129 ;; state: start with 0, "" -> 1, "[" -> 2, "[013457]" -> 3,
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
130 ;; "[013457]" -> 4, "m" -> back to 0!
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
131 ;; param: stored when state is 3 (in the above example: 1)
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
132 (let ((str-length (length str))
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
133 (face '(default))
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
134 (i 0) (char) (state 0) (param1) (param2))
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
135 (while (< i str-length)
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
136 (setq char (aref str i))
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
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
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
140 (setq state 1)); saw escape
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
141 ((and (= state 1) (= char ?\[)); seen escape
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
142 (setq state 2
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
143 param1 nil
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
144 param2 nil)); saw [, prepare for param1 and param2!
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
145 ((and (or (= state 2) (= state 3)); reading first or second digit
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
146 (string-match "[01234567]" (substring str i (1+ i))))
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
147 (if (= state 2); reading first digit
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
148 ;;  (hilight)
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
149 (setq param1 (string-to-number (substring str i (1+ i)))
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
150 state 3); prepare to read a second digit or quit.
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
151 ;; if reading second digit
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
152 ;; such as  (green foreground)
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
153 (setq param2 (string-to-number (substring str i (1+ i)))
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
154 state 4))); read second digit, prepare to quit
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
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
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
158 (if (null param2); only param1 set: no color changes!
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
159 ;; : default face
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
160 (if (= param1 0)
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
161 (setq face '(default))
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
162 ;; : hilight, : inverse, : underline, etc.
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
163 (add-to-list 'face (aref ansi-color-faces-vector param1)))
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
164 ;; If param2 is set, we are changing back- or foreground color.
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
165 (if (= param1 3); first digit told us to change foreground
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
166 ;; : red foreground
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
167 (add-to-list 'face (cons 'foreground-color
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
168 (aref ansi-color-names-vector param2)))
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
169 ;; : green background
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
170 (add-to-list 'face (cons 'background-color
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
171 (aref ansi-color-names-vector param2))))))
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
172 (t (setq state 0))); all other cases, state is 0.
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
173
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
174 ;; Set text-property for every char.
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
175 (if (> state 0); if reading ANSI codes, state > 0: make them
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
176 ; invisible.
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
177 (put-text-property i (1+ i) 'invisible t str)
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
178 ;; if reading normal chars, state is 0, put them in the
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
179 ;; current face.
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
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
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
188 (setq i (1+ i)))))
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
189
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
190 (provide 'ansi-color)
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
191
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
192 ;;; ansi-colors.el ends here
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
193
b565998245c5 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
194