]> code.delx.au - gnu-emacs-elpa/blob - rainbow-mode-0.1.el
Initial repository contents
[gnu-emacs-elpa] / rainbow-mode-0.1.el
1 ;;; rainbow-mode.el --- Colorize color names in buffers
2
3 ;; Copyright (C) 2010 Free Software Foundation, Inc
4
5 ;; Author: Julien Danjou <julien@danjou.info>
6 ;; Keywords: faces
7 ;; Version: 0.1
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25 ;;
26 ;; This minor mode sets background color to strings that match color
27 ;; names, e.g. #0000ff is displayed in white with a blue background.
28 ;;
29
30 ;;; Code:
31
32 (eval-when-compile
33 (require 'cl))
34
35 (require 'regexp-opt)
36 (require 'faces)
37
38 (defgroup rainbow nil
39 "Show color strings with a background color."
40 :tag "Rainbow"
41 :group 'help)
42
43 ;; Hexadecimal colors
44 (defvar rainbow-hexadecimal-colors-font-lock-keywords
45 '(("#[0-9a-fA-F]\\{3\\}[0-9a-fA-F]\\{3\\}?"
46 (0 (rainbow-colorize-itself))))
47 "Font-lock keywords to add for hexadecimal colors.")
48
49 ;; rgb() colors
50 (defvar rainbow-html-rgb-colors-font-lock-keywords
51 '(("rgb(\s*\\([0-9]\\{1,3\\}\\(?:\s*%\\)?\\)\s*,\s*\\([0-9]\\{1,3\\}\\(?:\s*%\\)?\\)\s*,\s*\\([0-9]\\{1,3\\}\\(?:\s*%\\)?\\)\s*)"
52 (0 (rainbow-colorize-rgb)))
53 ("rgba(\s*\\([0-9]\\{1,3\\}\\(?:\s*%\\)?\\)\s*,\s*\\([0-9]\\{1,3\\}\\(?:\s*%\\)?\\)\s*,\s*\\([0-9]\\{1,3\\}\\(?:\s*%\\)?\\)\s*,\s*[0-9]\\{1,3\\}\s*%?\s*)"
54 (0 (rainbow-colorize-rgb))))
55 "Font-lock keywords to add for RGB colors.")
56
57 ;; HTML colors name
58 (defvar rainbow-html-colors-font-lock-keywords nil
59 "Font-lock keywords to add for HTML colors.")
60 (make-variable-buffer-local 'rainbow-html-colors-font-lock-keywords)
61
62 (defcustom rainbow-html-colors-alist
63 '(("black" . "#000000")
64 ("silver" . "#C0C0C0")
65 ("gray" . "#808080")
66 ("white" . "#FFFFFF")
67 ("maroon" . "#800000")
68 ("red" . "#FF0000")
69 ("purple" . "#800080")
70 ("fuchsia" . "#FF00FF")
71 ("green" . "#008000")
72 ("lime" . "#00FF00")
73 ("olive" . "#808000")
74 ("yellow" . "#FFFF00")
75 ("navy" . "#000080")
76 ("blue" . "#0000FF")
77 ("teal" . "#008080")
78 ("aqua" . "#00FFFF"))
79 "Alist of HTML colors.
80 Each entry should have the form (COLOR-NAME . HEXADECIMAL-COLOR)."
81 :group 'rainbow)
82
83 (defcustom rainbow-html-colors-major-mode-list
84 '(html-mode css-mode php-mode nxml-mode xml-mode)
85 "List of major mode where HTML colors are enabled when
86 `rainbow-html-colors' is set to auto."
87 :group 'rainbow)
88
89 (defcustom rainbow-html-colors 'auto
90 "When to enable HTML colors.
91 If set to t, the HTML colors will be enabled. If set to nil, the
92 HTML colors will not be enabled. If set to auto, the HTML colors
93 will be enabled if a major mode has been detected from the
94 `rainbow-html-colors-major-mode-list'."
95 :group 'rainbow)
96
97 ;; X colors
98 (defvar rainbow-x-colors-font-lock-keywords
99 `((,(regexp-opt (x-defined-colors) 'words)
100 (0 (rainbow-colorize-itself))))
101 "Font-lock keywords to add for X colors.")
102
103 (defcustom rainbow-x-colors-major-mode-list
104 '(emacs-lisp-mode lisp-interaction-mode c-mode c++-mode java-mode)
105 "List of major mode where X colors are enabled when
106 `rainbow-x-colors' is set to auto."
107 :group 'rainbow)
108
109 (defcustom rainbow-x-colors 'auto
110 "When to enable X colors.
111 If set to t, the X colors will be enabled. If set to nil, the
112 X colors will not be enabled. If set to auto, the X colors
113 will be enabled if a major mode has been detected from the
114 `rainbow-x-colors-major-mode-list'."
115 :group 'rainbow)
116
117 ;; LaTeX colors
118 (defvar rainbow-latex-rgb-colors-font-lock-keywords
119 '(("{rgb}{\\([0-9.]+\\),\\([0-9.]+\\),\\([0-9.]+\\)}"
120 (0 (rainbow-colorize-rgb-float)))
121 ("{RGB}{\\([0-9]\\{1,3\\}\\),\\([0-9]\\{1,3\\}\\),\\([0-9]\\{1,3\\}\\)}"
122 (0 (rainbow-colorize-rgb)))
123 ("{HTML}{\\([0-9A-Fa-f]\\{6\\}\\)}"
124 (0 (rainbow-colorize-hexadecimal-without-sharp))))
125 "Font-lock keywords to add for X colors.")
126
127 (defcustom rainbow-latex-colors-major-mode-list
128 '(latex-mode)
129 "List of major mode where X colors are enabled when
130 `rainbow-x-colors' is set to auto."
131 :group 'rainbow)
132
133 (defcustom rainbow-latex-colors 'auto
134 "When to enable LaTeX colors.
135 If set to t, the LaTeX colors will be enabled. If set to nil, the
136 X colors will not be enabled. If set to auto, the LaTeX colors
137 will be enabled if a major mode has been detected from the
138 `rainbow-latex-colors-major-mode-list'."
139 :group 'rainbow)
140
141 ;; Functions
142 (defun rainbow-colorize-match (color)
143 "Return a matched string propertized with a face whose
144 background is COLOR. The foreground is computed using
145 `rainbow-color-luminance', and is either white or black."
146 (put-text-property
147 (match-beginning 0) (match-end 0)
148 'face `((:foreground ,(if (> 128.0 (rainbow-x-color-luminance color))
149 "white" "black"))
150 (:background ,color))))
151
152 (defun rainbow-colorize-itself ()
153 "Colorize a match with itself."
154 (rainbow-colorize-match (match-string-no-properties 0)))
155
156 (defun rainbow-colorize-hexadecimal-without-sharp ()
157 "Colorize an hexadecimal colors and prepend # to it."
158 (rainbow-colorize-match (concat "#" (match-string-no-properties 1))))
159
160 (defun rainbow-colorize-by-assoc (assoc-list)
161 "Colorize a match with its association from ASSOC-LIST."
162 (rainbow-colorize-match (cdr (assoc (match-string-no-properties 0) assoc-list))))
163
164 (defun rainbow-rgb-relative-to-absolute (number)
165 "Convert a relative NUMBER to absolute. If NUMBER is absolute, return NUMBER.
166 This will convert \"80 %\" to 204, \"100 %\" to 255 but \"123\" to \"123\"."
167 (let ((string-length (- (length number) 1)))
168 ;; Is this a number with %?
169 (if (eq (elt number string-length) ?%)
170 (/ (* (string-to-number (substring number 0 string-length)) 255) 100)
171 (string-to-number number))))
172
173 (defun rainbow-colorize-rgb ()
174 "Colorize a match with itself."
175 (let ((r (rainbow-rgb-relative-to-absolute (match-string-no-properties 1)))
176 (g (rainbow-rgb-relative-to-absolute (match-string-no-properties 2)))
177 (b (rainbow-rgb-relative-to-absolute (match-string-no-properties 3))))
178 (rainbow-colorize-match (format "#%02X%02X%02X" r g b))))
179
180 (defun rainbow-colorize-rgb-float ()
181 "Colorize a match with itself, with relative value."
182 (let ((r (* (string-to-number (match-string-no-properties 1)) 255.0))
183 (g (* (string-to-number (match-string-no-properties 2)) 255.0))
184 (b (* (string-to-number (match-string-no-properties 3)) 255.0)))
185 (rainbow-colorize-match (format "#%02X%02X%02X" r g b))))
186
187 (defun rainbow-color-luminance (red green blue)
188 "Calculate the luminance of color composed of RED, BLUE and GREEN."
189 (floor (+ (* .2126 red) (* .7152 green) (* .0722 blue)) 256))
190
191 (defun rainbow-x-color-luminance (color)
192 "Calculate the luminance of a color string (e.g. \"#ffaa00\", \"blue\")."
193 (let* ((values (x-color-values color))
194 (r (car values))
195 (g (cadr values))
196 (b (caddr values)))
197 (rainbow-color-luminance r g b)))
198
199 (defun rainbow-turn-on ()
200 "Turn on raibow-mode."
201 (font-lock-add-keywords nil
202 rainbow-hexadecimal-colors-font-lock-keywords)
203 ;; Activate X colors?
204 (when (or (eq rainbow-x-colors t)
205 (and (eq rainbow-x-colors 'auto)
206 (memq major-mode rainbow-x-colors-major-mode-list)))
207 (font-lock-add-keywords nil
208 rainbow-x-colors-font-lock-keywords))
209 ;; Activate LaTeX colors?
210 (when (or (eq rainbow-latex-colors t)
211 (and (eq rainbow-latex-colors 'auto)
212 (memq major-mode rainbow-latex-colors-major-mode-list)))
213 (font-lock-add-keywords nil
214 rainbow-latex-rgb-colors-font-lock-keywords))
215 ;; Activate HTML colors?
216 (when (or (eq rainbow-html-colors t)
217 (and (eq rainbow-html-colors 'auto)
218 (memq major-mode rainbow-html-colors-major-mode-list)))
219 (setq rainbow-html-colors-font-lock-keywords
220 `((,(regexp-opt (mapcar 'car rainbow-html-colors-alist) 'words)
221 (0 (rainbow-colorize-by-assoc rainbow-html-colors-alist)))))
222 (font-lock-add-keywords nil
223 `(,@rainbow-html-colors-font-lock-keywords
224 ,@rainbow-html-rgb-colors-font-lock-keywords))))
225
226 (defun rainbow-turn-off ()
227 "Turn off rainbow-mode."
228 (font-lock-remove-keywords
229 nil
230 `(,@rainbow-hexadecimal-colors-font-lock-keywords
231 ,@rainbow-x-colors-font-lock-keywords
232 ,@rainbow-latex-rgb-colors-font-lock-keywords
233 ,@rainbow-html-colors-font-lock-keywords
234 ,@rainbow-html-rgb-colors-font-lock-keywords)))
235
236 ;;;###autoload
237 (define-minor-mode rainbow-mode
238 "Colorize strings that represent colors.
239 This will fontify with colors the string like \"#aabbcc\" or \"blue\"."
240 :lighter " Rbow"
241 (progn
242 (if rainbow-mode
243 (rainbow-turn-on)
244 (rainbow-turn-off))
245 ;; Turn on font lock
246 (font-lock-mode 1)))
247
248 (provide 'rainbow-mode)
249
250 ;;; rainbow-mode.el ends here