*** empty log message ***
[emacs.git] / lisp / ansi-color.el
blob746038e32dc43b32b8a47f9ef46f951dbb4ec9a8
1 ;;; ansi-color.el -- translate ANSI into text-properties
3 ;; Copyright (C) 1999 Free Software Foundation, Inc.
5 ;; Author: Alex Schroeder <alex@gnu.org>
6 ;; Maintainer: Alex Schroeder <alex@gnu.org>
7 ;; Version: 2.1.2
8 ;; Keywords: comm processes
10 ;; This file is part of GNU Emacs.
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.
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.
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.
27 ;;; Commentary:
29 ;; You can get the latest version of this file from my homepage
30 ;; <URL:http://www.geocities.com/TimesSquare/6120/emacs.html>.
32 ;; This file provides a function that takes a string containing ANSI
33 ;; control sequences and tries to replace these with text-properties.
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.
39 ;;; Testing:
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!
45 ;; (insert (ansi-color-apply "\033[1mbold\033[0m and \033[34mblue\033[0m, \033[1m\033[34mbold and blue\033[0m!!"))
47 ;; Usage with TinyMush.el:
49 ;; In order to install this with TinyMush.el, add the following to your
50 ;; .emacs file:
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)
69 ;; Usage with shell-mode:
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).
79 ;;; Code:
81 ;; Customization
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.
87 Those are sequences like this one: \033[1m, where 1 could be one of the
88 following numbers: 0 (default), 1 (hilight, rendered as bold), 4
89 (underline), 5 (flashing, rendered as bold), 7 (inverse, rendered the
90 same as the modeline)")
92 (defvar ansi-color-names-vector
93 ["black" "red" "green" "yellow" "blue" "magenta" "cyan" "white"]
94 "Array of colors.
96 Used for sequences like this one: \033[31m, where 1 could be an index to a
97 foreground color (red, in this case), or \033[41m, where 1 could be an
98 index to a background color.
100 The default colors are: black, red, green, yellow, blue, magenta,
101 cyan, and white.
103 On a light background, I prefer: black, red, dark green, orange, blue,
104 magenta, turquoise, snow4")
106 ;; Main function
108 (defun ansi-color-apply (string)
109 "Translates ANSI color control sequences into text-properties.
111 Applies ANSI control sequences setting foreground and background colors
112 to STRING and returns the result. The colors used are given in
113 `ansi-color-faces-vector' and `ansi-color-names-vector'.
115 This 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
121 (while (setq end (string-match "\033\\[\\([013457][01234567]?;\\)*[013457][01234567]?m" string start))
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))
130 (while (setq i (string-match "[013457][01234567]?[;m]" escape i))
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))))
138 ;; Helper functions
140 (defun ansi-color-make-face (face param1 param2)
141 "Return a face based on FACE and characters PARAM1 and PARAM2.
143 The face can be used in a call to `add-text-properties'. The PARAM1 and
144 PARAM2 characters are the two numeric characters in ANSI control
145 sequences between ?[ and ?m. Unless the ANSI control sequence specifies
146 a return to default face using PARAM1 ?0 and PARAM2 ?m (ie. \"\033[0m\"), the
147 properties 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)))))))
164 (provide 'ansi-color)
166 ;;; ansi-color.el ends here