Mercurial > emacs
annotate lisp/ansi-color.el @ 28592:e579af0ac592
Don't define autoconfiscated
BROKEN_MKTIME.
author | Dave Love <fx@gnu.org> |
---|---|
date | Fri, 14 Apr 2000 17:51:55 +0000 |
parents | 5ebcf240e1c6 |
children | 5ad18c4ebe5c |
rev | line source |
---|---|
25171 | 1 ;;; ansi-color.el -- translate ANSI into text-properties |
2 | |
3 ;; Copyright (C) 1999 Free Software Foundation, Inc. | |
4 | |
26092 | 5 ;; Author: Alex Schroeder <alex@gnu.org> |
6 ;; Maintainer: Alex Schroeder <alex@gnu.org> | |
26439
5ebcf240e1c6
(ansi-color-apply): Updated regexps to include
Gerd Moellmann <gerd@gnu.org>
parents:
26092
diff
changeset
|
7 ;; Version: 2.1.2 |
25171 | 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 ;;; 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 ;; | |
26092 | 45 ;; (insert (ansi-color-apply "\033[1mbold\033[0m and \033[34mblue\033[0m, \033[1m\033[34mbold and blue\033[0m!!")) |
25171 | 46 |
26092 | 47 ;; Usage with TinyMush.el: |
25171 | 48 |
26092 | 49 ;; In order to install this with TinyMush.el, add the following to your |
50 ;; .emacs file: | |
25171 | 51 ;; |
26092 | 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). | |
25171 | 76 |
77 | |
78 | |
79 ;;; Code: | |
80 | |
26092 | 81 ;; Customization |
82 | |
25171 | 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 | |
26092 | 87 Those are sequences like this one: \033[1m, where 1 could be one of the |
25171 | 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)") | |
91 | |
92 (defvar ansi-color-names-vector | |
93 ["black" "red" "green" "yellow" "blue" "magenta" "cyan" "white"] | |
94 "Array of colors. | |
95 | |
26092 | 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 | |
25171 | 98 index to a background color. |
99 | |
100 The default colors are: black, red, green, yellow, blue, magenta, | |
101 cyan, and white. | |
102 | |
103 On a light background, I prefer: black, red, dark green, orange, blue, | |
104 magenta, turquoise, snow4") | |
105 | |
26092 | 106 ;; Main function |
25171 | 107 |
26092 | 108 (defun ansi-color-apply (string) |
109 "Translates ANSI color control sequences into text-properties. | |
110 | |
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'. | |
25171 | 114 |
26092 | 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 | |
26439
5ebcf240e1c6
(ansi-color-apply): Updated regexps to include
Gerd Moellmann <gerd@gnu.org>
parents:
26092
diff
changeset
|
121 (while (setq end (string-match "\033\\[\\([013457][01234567]?;\\)*[013457][01234567]?m" string start)) |
26092 | 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)) | |
26439
5ebcf240e1c6
(ansi-color-apply): Updated regexps to include
Gerd Moellmann <gerd@gnu.org>
parents:
26092
diff
changeset
|
130 (while (setq i (string-match "[013457][01234567]?[;m]" escape i)) |
26092 | 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)))) | |
25171 | 137 |
26092 | 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. | |
25225
38f98813a83d
(ansi-color-to-text-properties): Added New state 5
Karl Heuer <kwzh@gnu.org>
parents:
25171
diff
changeset
|
142 |
26092 | 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))))))) | |
25171 | 163 |
164 (provide 'ansi-color) | |
165 | |
26092 | 166 ;;; ansi-color.el ends here |