]> git.rkrishnan.org Git - .emacs.d.git/blob - emacs/rainbow-mode.el
8207abcd1d772c24ecaa62ff27070cf9c4b07383
[.emacs.d.git] / emacs / rainbow-mode.el
1 ;;; rainbow-mode.el --- prints color strings with colored background
2
3 ;; Copyright (C) 2010 Julien Danjou
4
5 ;; Author: Julien Danjou <julien@danjou.info>
6 ;; Keywords: strings, faces
7
8 ;; This file is NOT part of GNU Emacs.
9
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24 ;; This minor mode will add background to strings that matches color names.
25 ;; i.e.
26 ;;     #0000ff
27 ;; Will be printed 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 ;; Functions
118 (defun rainbow-colorize-match (color)
119   "Return a matched string propertized with a face whose
120 background is COLOR. The foreground is computed using
121 `rainbow-color-luminance', and is either white or black."
122   (put-text-property
123    (match-beginning 0) (match-end 0)
124    'face `((:foreground ,(if (> 128.0 (rainbow-x-color-luminance color))
125                              "white" "black"))
126            (:background ,color))))
127
128 (defun rainbow-colorize-itself ()
129   "Colorize a match with itself."
130   (rainbow-colorize-match (match-string-no-properties 0)))
131
132 (defun rainbow-colorize-by-assoc (assoc-list)
133   "Colorize a match with its association from ASSOC-LIST."
134   (rainbow-colorize-match (cdr (assoc (match-string-no-properties 0) assoc-list))))
135
136 (defun rainbow-rgb-relative-to-absolute (number)
137   "Convert a relative NUMBER to absolute. If NUMBER is absolute, return NUMBER.
138 This will convert \"80 %\" to 204, \"100 %\" to 255 but \"123\" to \"123\""
139   (let ((string-length (- (length number) 1)))
140     ;; Is this a number with %?
141     (if (eq (elt number string-length) ?%)
142         (/ (* (string-to-number (substring number 0 string-length)) 255) 100)
143       (string-to-number number))))
144
145 (defun rainbow-colorize-rgb ()
146   "Colorize a match with itself."
147   (let ((r (rainbow-rgb-relative-to-absolute (match-string-no-properties 1)))
148         (g (rainbow-rgb-relative-to-absolute (match-string-no-properties 2)))
149         (b (rainbow-rgb-relative-to-absolute (match-string-no-properties 3))))
150     (rainbow-colorize-match (format "#%02X%02X%02X" r g b))))
151
152 (defun rainbow-color-luminance (red green blue)
153   "Calculate the luminance of color composed of RED, BLUE and GREEN."
154   (floor (+ (* .2126 red) (* .7152 green) (* .0722 blue)) 256))
155
156 (defun rainbow-x-color-luminance (color)
157   "Calculate the luminance of a color string (e.g. \"#ffaa00\", \"blue\")."
158   (let* ((values (x-color-values color))
159          (r (car values))
160          (g (cadr values))
161          (b (caddr values)))
162     (rainbow-color-luminance r g b)))
163
164 (defun rainbow-turn-on ()
165   "Turn on raibow-mode."
166   (font-lock-add-keywords nil
167                           (list rainbow-hexadecimal-colors-font-lock-keywords))
168   ;; Activate X colors?
169   (when (or (eq rainbow-x-colors t)
170             (and (eq rainbow-x-colors 'auto)
171                  (memq major-mode rainbow-x-colors-major-mode-list)))
172     (font-lock-add-keywords nil
173                             (list rainbow-x-colors-font-lock-keywords)))
174   ;; Activate HTML colors?
175   (when (or (eq rainbow-html-colors t)
176             (and (eq rainbow-html-colors 'auto)
177                  (memq major-mode rainbow-html-colors-major-mode-list)))
178     (setq rainbow-html-colors-font-lock-keywords
179           `(,(regexp-opt (mapcar 'car rainbow-html-colors-alist) 'words)
180             (0 (rainbow-colorize-by-assoc rainbow-html-colors-alist))))
181     (font-lock-add-keywords nil
182                             `(,rainbow-html-colors-font-lock-keywords
183                               ,@rainbow-html-rgb-colors-font-lock-keywords))))
184
185 (defun rainbow-turn-off ()
186   "Turn off rainbow-mode."
187   (font-lock-remove-keywords
188    nil
189    (list
190     rainbow-hexadecimal-colors-font-lock-keywords
191     rainbow-html-colors-font-lock-keywords
192     rainbow-x-colors-font-lock-keywords
193     rainbow-html-rgb-colors-font-lock-keywords)))
194
195 ;;;###autoload
196 (define-minor-mode rainbow-mode
197   "Colorize strings that represent colors.
198 This will fontify with colors the string like \"#aabbcc\" or \"blue\""
199   :lighter " Rbow"
200   (progn
201     (if rainbow-mode
202         (rainbow-turn-on)
203       (rainbow-turn-off))
204     ;; Turn on font lock
205     (font-lock-mode 1)))
206
207 (provide 'rainbow-mode)