]> code.delx.au - gnu-emacs-elpa/blob - packages/rainbow-mode/rainbow-mode.el
Add cl-letf and cl-labels.
[gnu-emacs-elpa] / packages / rainbow-mode / rainbow-mode.el
1 ;;; rainbow-mode.el --- Colorize color names in buffers
2
3 ;; Copyright (C) 2010-2012 Free Software Foundation, Inc
4
5 ;; Author: Julien Danjou <julien@danjou.info>
6 ;; Keywords: faces
7 ;; Version: 0.5
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 (require 'color)
38
39 (defgroup rainbow nil
40 "Show color strings with a background color."
41 :tag "Rainbow"
42 :group 'help)
43
44 ;; Hexadecimal colors
45 (defvar rainbow-hexadecimal-colors-font-lock-keywords
46 '(("[^&]\\(#\\(?:[0-9a-fA-F]\\{3\\}\\)+\\{1,4\\}\\)"
47 (1 (rainbow-colorize-itself 1)))
48 ("^\\(#\\(?:[0-9a-fA-F]\\{3\\}\\)+\\{1,4\\}\\)"
49 (0 (rainbow-colorize-itself)))
50 ("[Rr][Gg][Bb]:[0-9a-fA-F]\\{1,4\\}/[0-9a-fA-F]\\{1,4\\}/[0-9a-fA-F]\\{1,4\\}"
51 (0 (rainbow-colorize-itself)))
52 ("[Rr][Gg][Bb][Ii]:[0-9.]+/[0-9.]+/[0-9.]+"
53 (0 (rainbow-colorize-itself)))
54 ("\\(?:[Cc][Ii][Ee]\\(?:[Xx][Yy][Zz]\\|[Uu][Vv][Yy]\\|[Xx][Yy][Yy]\\|[Ll][Aa][Bb]\\|[Ll][Uu][Vv]\\)\\|[Tt][Ee][Kk][Hh][Vv][Cc]\\):[+-]?[0-9.]+\\(?:[Ee][+-]?[0-9]+\\)?/[+-]?[0-9.]+\\(?:[Ee][+-]?[0-9]+\\)?/[+-]?[0-9.]+\\(?:[Ee][+-]?[0-9]+\\)?"
55 (0 (rainbow-colorize-itself))))
56 "Font-lock keywords to add for hexadecimal colors.")
57
58 ;; rgb() colors
59 (defvar rainbow-html-rgb-colors-font-lock-keywords
60 '(("rgb(\s*\\([0-9]\\{1,3\\}\\(?:\s*%\\)?\\)\s*,\s*\\([0-9]\\{1,3\\}\\(?:\s*%\\)?\\)\s*,\s*\\([0-9]\\{1,3\\}\\(?:\s*%\\)?\\)\s*)"
61 (0 (rainbow-colorize-rgb)))
62 ("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]*\.?[0-9]+\s*%?\s*)"
63 (0 (rainbow-colorize-rgb)))
64 ("hsl(\s*\\([0-9]\\{1,3\\}\\)\s*,\s*\\([0-9]\\{1,3\\}\\)\s*%\s*,\s*\\([0-9]\\{1,3\\}\\)\s*%\s*)"
65 (0 (rainbow-colorize-hsl)))
66 ("hsla(\s*\\([0-9]\\{1,3\\}\\)\s*,\s*\\([0-9]\\{1,3\\}\\)\s*%\s*,\s*\\([0-9]\\{1,3\\}\\)\s*%\s*,\s*[0-9]*\.?[0-9]+\s*%?\s*)"
67 (0 (rainbow-colorize-hsl))))
68 "Font-lock keywords to add for RGB colors.")
69
70 ;; HTML colors name
71 (defvar rainbow-html-colors-font-lock-keywords nil
72 "Font-lock keywords to add for HTML colors.")
73 (make-variable-buffer-local 'rainbow-html-colors-font-lock-keywords)
74
75 (defcustom rainbow-html-colors-alist
76 '(("AliceBlue" . "#F0F8FF")
77 ("AntiqueWhite" . "#FAEBD7")
78 ("Aqua" . "#00FFFF")
79 ("Aquamarine" . "#7FFFD4")
80 ("Azure" . "#F0FFFF")
81 ("Beige" . "#F5F5DC")
82 ("Bisque" . "#FFE4C4")
83 ("Black" . "#000000")
84 ("BlanchedAlmond" . "#FFEBCD")
85 ("Blue" . "#0000FF")
86 ("BlueViolet" . "#8A2BE2")
87 ("Brown" . "#A52A2A")
88 ("BurlyWood" . "#DEB887")
89 ("CadetBlue" . "#5F9EA0")
90 ("Chartreuse" . "#7FFF00")
91 ("Chocolate" . "#D2691E")
92 ("Coral" . "#FF7F50")
93 ("CornflowerBlue" . "#6495ED")
94 ("Cornsilk" . "#FFF8DC")
95 ("Crimson" . "#DC143C")
96 ("Cyan" . "#00FFFF")
97 ("DarkBlue" . "#00008B")
98 ("DarkCyan" . "#008B8B")
99 ("DarkGoldenRod" . "#B8860B")
100 ("DarkGray" . "#A9A9A9")
101 ("DarkGrey" . "#A9A9A9")
102 ("DarkGreen" . "#006400")
103 ("DarkKhaki" . "#BDB76B")
104 ("DarkMagenta" . "#8B008B")
105 ("DarkOliveGreen" . "#556B2F")
106 ("Darkorange" . "#FF8C00")
107 ("DarkOrchid" . "#9932CC")
108 ("DarkRed" . "#8B0000")
109 ("DarkSalmon" . "#E9967A")
110 ("DarkSeaGreen" . "#8FBC8F")
111 ("DarkSlateBlue" . "#483D8B")
112 ("DarkSlateGray" . "#2F4F4F")
113 ("DarkSlateGrey" . "#2F4F4F")
114 ("DarkTurquoise" . "#00CED1")
115 ("DarkViolet" . "#9400D3")
116 ("DeepPink" . "#FF1493")
117 ("DeepSkyBlue" . "#00BFFF")
118 ("DimGray" . "#696969")
119 ("DimGrey" . "#696969")
120 ("DodgerBlue" . "#1E90FF")
121 ("FireBrick" . "#B22222")
122 ("FloralWhite" . "#FFFAF0")
123 ("ForestGreen" . "#228B22")
124 ("Fuchsia" . "#FF00FF")
125 ("Gainsboro" . "#DCDCDC")
126 ("GhostWhite" . "#F8F8FF")
127 ("Gold" . "#FFD700")
128 ("GoldenRod" . "#DAA520")
129 ("Gray" . "#808080")
130 ("Grey" . "#808080")
131 ("Green" . "#008000")
132 ("GreenYellow" . "#ADFF2F")
133 ("HoneyDew" . "#F0FFF0")
134 ("HotPink" . "#FF69B4")
135 ("IndianRed" . "#CD5C5C")
136 ("Indigo" . "#4B0082")
137 ("Ivory" . "#FFFFF0")
138 ("Khaki" . "#F0E68C")
139 ("Lavender" . "#E6E6FA")
140 ("LavenderBlush" . "#FFF0F5")
141 ("LawnGreen" . "#7CFC00")
142 ("LemonChiffon" . "#FFFACD")
143 ("LightBlue" . "#ADD8E6")
144 ("LightCoral" . "#F08080")
145 ("LightCyan" . "#E0FFFF")
146 ("LightGoldenRodYellow" . "#FAFAD2")
147 ("LightGray" . "#D3D3D3")
148 ("LightGrey" . "#D3D3D3")
149 ("LightGreen" . "#90EE90")
150 ("LightPink" . "#FFB6C1")
151 ("LightSalmon" . "#FFA07A")
152 ("LightSeaGreen" . "#20B2AA")
153 ("LightSkyBlue" . "#87CEFA")
154 ("LightSlateGray" . "#778899")
155 ("LightSlateGrey" . "#778899")
156 ("LightSteelBlue" . "#B0C4DE")
157 ("LightYellow" . "#FFFFE0")
158 ("Lime" . "#00FF00")
159 ("LimeGreen" . "#32CD32")
160 ("Linen" . "#FAF0E6")
161 ("Magenta" . "#FF00FF")
162 ("Maroon" . "#800000")
163 ("MediumAquaMarine" . "#66CDAA")
164 ("MediumBlue" . "#0000CD")
165 ("MediumOrchid" . "#BA55D3")
166 ("MediumPurple" . "#9370D8")
167 ("MediumSeaGreen" . "#3CB371")
168 ("MediumSlateBlue" . "#7B68EE")
169 ("MediumSpringGreen" . "#00FA9A")
170 ("MediumTurquoise" . "#48D1CC")
171 ("MediumVioletRed" . "#C71585")
172 ("MidnightBlue" . "#191970")
173 ("MintCream" . "#F5FFFA")
174 ("MistyRose" . "#FFE4E1")
175 ("Moccasin" . "#FFE4B5")
176 ("NavajoWhite" . "#FFDEAD")
177 ("Navy" . "#000080")
178 ("OldLace" . "#FDF5E6")
179 ("Olive" . "#808000")
180 ("OliveDrab" . "#6B8E23")
181 ("Orange" . "#FFA500")
182 ("OrangeRed" . "#FF4500")
183 ("Orchid" . "#DA70D6")
184 ("PaleGoldenRod" . "#EEE8AA")
185 ("PaleGreen" . "#98FB98")
186 ("PaleTurquoise" . "#AFEEEE")
187 ("PaleVioletRed" . "#D87093")
188 ("PapayaWhip" . "#FFEFD5")
189 ("PeachPuff" . "#FFDAB9")
190 ("Peru" . "#CD853F")
191 ("Pink" . "#FFC0CB")
192 ("Plum" . "#DDA0DD")
193 ("PowderBlue" . "#B0E0E6")
194 ("Purple" . "#800080")
195 ("Red" . "#FF0000")
196 ("RosyBrown" . "#BC8F8F")
197 ("RoyalBlue" . "#4169E1")
198 ("SaddleBrown" . "#8B4513")
199 ("Salmon" . "#FA8072")
200 ("SandyBrown" . "#F4A460")
201 ("SeaGreen" . "#2E8B57")
202 ("SeaShell" . "#FFF5EE")
203 ("Sienna" . "#A0522D")
204 ("Silver" . "#C0C0C0")
205 ("SkyBlue" . "#87CEEB")
206 ("SlateBlue" . "#6A5ACD")
207 ("SlateGray" . "#708090")
208 ("SlateGrey" . "#708090")
209 ("Snow" . "#FFFAFA")
210 ("SpringGreen" . "#00FF7F")
211 ("SteelBlue" . "#4682B4")
212 ("Tan" . "#D2B48C")
213 ("Teal" . "#008080")
214 ("Thistle" . "#D8BFD8")
215 ("Tomato" . "#FF6347")
216 ("Turquoise" . "#40E0D0")
217 ("Violet" . "#EE82EE")
218 ("Wheat" . "#F5DEB3")
219 ("White" . "#FFFFFF")
220 ("WhiteSmoke" . "#F5F5F5")
221 ("Yellow" . "#FFFF00")
222 ("YellowGreen" . "#9ACD32"))
223 "Alist of HTML colors.
224 Each entry should have the form (COLOR-NAME . HEXADECIMAL-COLOR)."
225 :group 'rainbow)
226
227 (defcustom rainbow-html-colors-major-mode-list
228 '(html-mode css-mode php-mode nxml-mode xml-mode)
229 "List of major mode where HTML colors are enabled when
230 `rainbow-html-colors' is set to auto."
231 :group 'rainbow)
232
233 (defcustom rainbow-html-colors 'auto
234 "When to enable HTML colors.
235 If set to t, the HTML colors will be enabled. If set to nil, the
236 HTML colors will not be enabled. If set to auto, the HTML colors
237 will be enabled if a major mode has been detected from the
238 `rainbow-html-colors-major-mode-list'."
239 :group 'rainbow)
240
241 ;; X colors
242 (defvar rainbow-x-colors-font-lock-keywords
243 `((,(regexp-opt (x-defined-colors) 'words)
244 (0 (rainbow-colorize-itself))))
245 "Font-lock keywords to add for X colors.")
246
247 (defcustom rainbow-x-colors-major-mode-list
248 '(emacs-lisp-mode lisp-interaction-mode c-mode c++-mode java-mode)
249 "List of major mode where X colors are enabled when
250 `rainbow-x-colors' is set to auto."
251 :group 'rainbow)
252
253 (defcustom rainbow-x-colors 'auto
254 "When to enable X colors.
255 If set to t, the X colors will be enabled. If set to nil, the
256 X colors will not be enabled. If set to auto, the X colors
257 will be enabled if a major mode has been detected from the
258 `rainbow-x-colors-major-mode-list'."
259 :group 'rainbow)
260
261 ;; LaTeX colors
262 (defvar rainbow-latex-rgb-colors-font-lock-keywords
263 '(("{rgb}{\\([0-9.]+\\),\\([0-9.]+\\),\\([0-9.]+\\)}"
264 (0 (rainbow-colorize-rgb-float)))
265 ("{RGB}{\\([0-9]\\{1,3\\}\\),\\([0-9]\\{1,3\\}\\),\\([0-9]\\{1,3\\}\\)}"
266 (0 (rainbow-colorize-rgb)))
267 ("{HTML}{\\([0-9A-Fa-f]\\{6\\}\\)}"
268 (0 (rainbow-colorize-hexadecimal-without-sharp))))
269 "Font-lock keywords to add for X colors.")
270
271 (defcustom rainbow-latex-colors-major-mode-list
272 '(latex-mode)
273 "List of major mode where X colors are enabled when
274 `rainbow-x-colors' is set to auto."
275 :group 'rainbow)
276
277 (defcustom rainbow-latex-colors 'auto
278 "When to enable LaTeX colors.
279 If set to t, the LaTeX colors will be enabled. If set to nil, the
280 X colors will not be enabled. If set to auto, the LaTeX colors
281 will be enabled if a major mode has been detected from the
282 `rainbow-latex-colors-major-mode-list'."
283 :group 'rainbow)
284
285 ;; Functions
286 (defun rainbow-colorize-match (color &optional match)
287 "Return a matched string propertized with a face whose
288 background is COLOR. The foreground is computed using
289 `rainbow-color-luminance', and is either white or black."
290 (let ((match (or match 0)))
291 (put-text-property
292 (match-beginning match) (match-end match)
293 'face `((:foreground ,(if (> 0.5 (rainbow-x-color-luminance color))
294 "white" "black"))
295 (:background ,color)))))
296
297 (defun rainbow-colorize-itself (&optional match)
298 "Colorize a match with itself."
299 (rainbow-colorize-match (match-string-no-properties (or match 0)) match))
300
301 (defun rainbow-colorize-hexadecimal-without-sharp ()
302 "Colorize an hexadecimal colors and prepend # to it."
303 (rainbow-colorize-match (concat "#" (match-string-no-properties 1))))
304
305 (defun rainbow-colorize-by-assoc (assoc-list)
306 "Colorize a match with its association from ASSOC-LIST."
307 (rainbow-colorize-match (cdr (assoc-string (match-string-no-properties 0)
308 assoc-list t))))
309
310 (defun rainbow-rgb-relative-to-absolute (number)
311 "Convert a relative NUMBER to absolute. If NUMBER is absolute, return NUMBER.
312 This will convert \"80 %\" to 204, \"100 %\" to 255 but \"123\" to \"123\"."
313 (let ((string-length (- (length number) 1)))
314 ;; Is this a number with %?
315 (if (eq (elt number string-length) ?%)
316 (/ (* (string-to-number (substring number 0 string-length)) 255) 100)
317 (string-to-number number))))
318
319 (defun rainbow-colorize-hsl ()
320 "Colorize a match with itself."
321 (let ((h (/ (string-to-number (match-string-no-properties 1)) 360.0))
322 (s (/ (string-to-number (match-string-no-properties 2)) 100.0))
323 (l (/ (string-to-number (match-string-no-properties 3)) 100.0)))
324 (rainbow-colorize-match
325 (multiple-value-bind (r g b)
326 (color-hsl-to-rgb h s l)
327 (format "#%02X%02X%02X" (* r 255) (* g 255) (* b 255))))))
328
329 (defun rainbow-colorize-rgb ()
330 "Colorize a match with itself."
331 (let ((r (rainbow-rgb-relative-to-absolute (match-string-no-properties 1)))
332 (g (rainbow-rgb-relative-to-absolute (match-string-no-properties 2)))
333 (b (rainbow-rgb-relative-to-absolute (match-string-no-properties 3))))
334 (rainbow-colorize-match (format "#%02X%02X%02X" r g b))))
335
336 (defun rainbow-colorize-rgb-float ()
337 "Colorize a match with itself, with relative value."
338 (let ((r (* (string-to-number (match-string-no-properties 1)) 255.0))
339 (g (* (string-to-number (match-string-no-properties 2)) 255.0))
340 (b (* (string-to-number (match-string-no-properties 3)) 255.0)))
341 (rainbow-colorize-match (format "#%02X%02X%02X" r g b))))
342
343 (defun rainbow-color-luminance (red green blue)
344 "Calculate the luminance of color composed of RED, BLUE and GREEN.
345 Return a value between 0 and 1."
346 (/ (+ (* .2126 red) (* .7152 green) (* .0722 blue)) 256))
347
348 (defun rainbow-x-color-luminance (color)
349 "Calculate the luminance of a color string (e.g. \"#ffaa00\", \"blue\").
350 Return a value between 0 and 1."
351 (let* ((values (x-color-values color))
352 (r (/ (car values) 256.0))
353 (g (/ (cadr values) 256.0))
354 (b (/ (caddr values) 256.0)))
355 (rainbow-color-luminance r g b)))
356
357 (defun rainbow-turn-on ()
358 "Turn on raibow-mode."
359 (font-lock-add-keywords nil
360 rainbow-hexadecimal-colors-font-lock-keywords)
361 ;; Activate X colors?
362 (when (or (eq rainbow-x-colors t)
363 (and (eq rainbow-x-colors 'auto)
364 (memq major-mode rainbow-x-colors-major-mode-list)))
365 (font-lock-add-keywords nil
366 rainbow-x-colors-font-lock-keywords))
367 ;; Activate LaTeX colors?
368 (when (or (eq rainbow-latex-colors t)
369 (and (eq rainbow-latex-colors 'auto)
370 (memq major-mode rainbow-latex-colors-major-mode-list)))
371 (font-lock-add-keywords nil
372 rainbow-latex-rgb-colors-font-lock-keywords))
373 ;; Activate HTML colors?
374 (when (or (eq rainbow-html-colors t)
375 (and (eq rainbow-html-colors 'auto)
376 (memq major-mode rainbow-html-colors-major-mode-list)))
377 (setq rainbow-html-colors-font-lock-keywords
378 `((,(regexp-opt (mapcar 'car rainbow-html-colors-alist) 'words)
379 (0 (rainbow-colorize-by-assoc rainbow-html-colors-alist)))))
380 (font-lock-add-keywords nil
381 `(,@rainbow-html-colors-font-lock-keywords
382 ,@rainbow-html-rgb-colors-font-lock-keywords))))
383
384 (defun rainbow-turn-off ()
385 "Turn off rainbow-mode."
386 (font-lock-remove-keywords
387 nil
388 `(,@rainbow-hexadecimal-colors-font-lock-keywords
389 ,@rainbow-x-colors-font-lock-keywords
390 ,@rainbow-latex-rgb-colors-font-lock-keywords
391 ,@rainbow-html-colors-font-lock-keywords
392 ,@rainbow-html-rgb-colors-font-lock-keywords)))
393
394 ;;;###autoload
395 (define-minor-mode rainbow-mode
396 "Colorize strings that represent colors.
397 This will fontify with colors the string like \"#aabbcc\" or \"blue\"."
398 :lighter " Rbow"
399 (progn
400 (if rainbow-mode
401 (rainbow-turn-on)
402 (rainbow-turn-off))
403 ;; Turn on font lock
404 (font-lock-mode 1)))
405
406 (provide 'rainbow-mode)
407
408 ;;; rainbow-mode.el ends here