(Qcenter): New variable.
[bpt/emacs.git] / lisp / ansi-color.el
CommitLineData
618206ea
RS
1;;; ansi-color.el -- translate ANSI into text-properties
2
3;; Copyright (C) 1999 Free Software Foundation, Inc.
4
8737bb5a
GM
5;; Author: Alex Schroeder <alex@gnu.org>
6;; Maintainer: Alex Schroeder <alex@gnu.org>
4fefa382 7;; Version: 2.1.2
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
618206ea
RS
39;;; Testing:
40
41;; If you want to test the setup, evaluate the following fragment in a
42;; buffer without font-lock-mode. This doesn't work in buffers that
43;; have font-lock-mode!
44;;
8737bb5a 45;; (insert (ansi-color-apply "\033[1mbold\033[0m and \033[34mblue\033[0m, \033[1m\033[34mbold and blue\033[0m!!"))
618206ea 46
8737bb5a 47;; Usage with TinyMush.el:
618206ea 48
8737bb5a
GM
49;; In order to install this with TinyMush.el, add the following to your
50;; .emacs file:
618206ea 51;;
8737bb5a
GM
52;; (setq tinymud-filter-line-hook 'my-ansi-color-filter)
53;; (autoload 'ansi-color-apply "ansi-color"
54;; "Translates ANSI color control sequences into text-properties." t)
55;; (defun my-ansi-color-filter (conn line)
56;; "Call `ansi-color-apply' and then processes things like `filter-line'."
57;; (setq line (ansi-color-apply line))
58;; (if (not (get-value conn 'trigger-disable))
59;; (progn
60;; (check-triggers conn line
61;; (get-value conn 'triggers))
62;; (check-triggers conn line
63;; (get-value (get-value conn 'world) 'triggers))
64;; (check-triggers conn line
65;; tinymud-global-triggers)))
66;; (display-line conn line)
67;; t)
68
69;; Usage with shell-mode:
70
71;; In order to enjoy the marvels of "ls --color=tty" you will have to
72;; enter shell-mode using M-x shell, possibly disable font-lock-mode
73;; using M-: (font-lock-mode 0), and add ansi-color-apply to
74;; comint-preoutput-filter-functions using M-: (add-hook
75;; 'comint-preoutput-filter-functions 'ansi-color-apply).
618206ea
RS
76
77\f
78
79;;; Code:
80
8737bb5a
GM
81;; Customization
82
618206ea
RS
83(defvar ansi-color-faces-vector
84 [default bold default default underline bold default modeline]
85 "Faces used for ANSI control sequences determining a face.
86
8737bb5a 87Those are sequences like this one: \033[1m, where 1 could be one of the
618206ea
RS
88following numbers: 0 (default), 1 (hilight, rendered as bold), 4
89(underline), 5 (flashing, rendered as bold), 7 (inverse, rendered the
90same as the modeline)")
91
92(defvar ansi-color-names-vector
93 ["black" "red" "green" "yellow" "blue" "magenta" "cyan" "white"]
94 "Array of colors.
95
8737bb5a
GM
96Used for sequences like this one: \033[31m, where 1 could be an index to a
97foreground color (red, in this case), or \033[41m, where 1 could be an
618206ea
RS
98index to a background color.
99
100The default colors are: black, red, green, yellow, blue, magenta,
101cyan, and white.
102
103On a light background, I prefer: black, red, dark green, orange, blue,
104magenta, turquoise, snow4")
105
8737bb5a
GM
106;; Main function
107
108(defun ansi-color-apply (string)
109 "Translates ANSI color control sequences into text-properties.
110
111Applies ANSI control sequences setting foreground and background colors
112to STRING and returns the result. The colors used are given in
113`ansi-color-faces-vector' and `ansi-color-names-vector'.
114
115This function can be added to `comint-preoutput-filter-functions'."
116 (let ((face)
117 (start 0) (end) (escape)
118 (result)
119 (params))
120 ;; find the next escape sequence
4fefa382 121 (while (setq end (string-match "\033\\[\\([013457][01234567]?;\\)*[013457][01234567]?m" string start))
8737bb5a
GM
122 ;; store escape sequence
123 (setq escape (match-string 0 string))
124 ;; colorize the old block from start to end using old face
125 (if face
126 (put-text-property start end 'face face string))
127 (setq result (concat result (substring string start end)))
128 ;; create new face by applying all the parameters in the escape sequence
129 (let ((i 0))
4fefa382 130 (while (setq i (string-match "[013457][01234567]?[;m]" escape i))
8737bb5a
GM
131 (setq face (ansi-color-make-face face
132 (aref escape i)
133 (aref escape (1+ i))))
134 (setq i (match-end 0))))
135 (setq start (+ end (length escape))))
136 (concat result (substring string start))))
137
138;; Helper functions
139
140(defun ansi-color-make-face (face param1 param2)
141 "Return a face based on FACE and characters PARAM1 and PARAM2.
142
143The face can be used in a call to `add-text-properties'. The PARAM1 and
144PARAM2 characters are the two numeric characters in ANSI control
145sequences between ?[ and ?m. Unless the ANSI control sequence specifies
146a return to default face using PARAM1 ?0 and PARAM2 ?m (ie. \"\033[0m\"), the
147properties specified by PARAM1 and PARAM2 are added to face."
148 (cond ((= param1 ?0)
149 nil)
150 ((= param2 ?m)
151 (add-to-list 'face (aref ansi-color-faces-vector
152 (string-to-number (char-to-string param1)))))
153 ((= param1 ?3)
154 (add-to-list 'face (cons 'foreground-color
155 (aref ansi-color-names-vector
156 (string-to-number (char-to-string param2))))))
157 ((= param1 ?4)
158 (add-to-list 'face (cons 'background-color
159 (aref ansi-color-names-vector
160 (string-to-number (char-to-string param2))))))
161 (t (add-to-list 'face (aref ansi-color-faces-vector
162 (string-to-number (char-to-string param1)))))))
618206ea
RS
163
164(provide 'ansi-color)
165
8737bb5a 166;;; ansi-color.el ends here