Commit | Line | Data |
---|---|---|
be010748 | 1 | ;;; facemenu.el --- create a face menu for interactively adding fonts to text |
b578f267 | 2 | |
0d30b337 | 3 | ;; Copyright (C) 1994, 1995, 1996, 2001, 2002, 2003, 2004, |
114f9c96 | 4 | ;; 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc. |
4e8aa578 | 5 | |
5762abec | 6 | ;; Author: Boris Goldowsky <boris@gnu.org> |
4e8aa578 RS |
7 | ;; Keywords: faces |
8 | ||
9 | ;; This file is part of GNU Emacs. | |
10 | ||
eb3fa2cf | 11 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
4e8aa578 | 12 | ;; it under the terms of the GNU General Public License as published by |
eb3fa2cf GM |
13 | ;; the Free Software Foundation, either version 3 of the License, or |
14 | ;; (at your option) any later version. | |
4e8aa578 RS |
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 | |
eb3fa2cf | 22 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
4e8aa578 RS |
23 | |
24 | ;;; Commentary: | |
b578f267 | 25 | |
bf7d4561 BG |
26 | ;; This file defines a menu of faces (bold, italic, etc) which allows you to |
27 | ;; set the face used for a region of the buffer. Some faces also have | |
af1eab21 | 28 | ;; keybindings, which are shown in the menu. |
88d690a9 RS |
29 | ;; |
30 | ;; The menu also contains submenus for indentation and justification-changing | |
31 | ;; commands. | |
4e8aa578 | 32 | |
4e8aa578 | 33 | ;;; Usage: |
bf7d4561 BG |
34 | ;; Selecting a face from the menu or typing the keyboard equivalent will |
35 | ;; change the region to use that face. If you use transient-mark-mode and the | |
36 | ;; region is not active, the face will be remembered and used for the next | |
37 | ;; insertion. It will be forgotten if you move point or make other | |
38 | ;; modifications before inserting or typing anything. | |
4e8aa578 | 39 | ;; |
71296446 | 40 | ;; Faces can be selected from the keyboard as well. |
6be7d8db RS |
41 | ;; The standard keybindings are M-o (or ESC o) + letter: |
42 | ;; M-o i = "set italic", M-o b = "set bold", etc. | |
4e8aa578 RS |
43 | |
44 | ;;; Customization: | |
45 | ;; An alternative set of keybindings that may be easier to type can be set up | |
88d690a9 RS |
46 | ;; using "Alt" or "Hyper" keys. This requires that you either have or create |
47 | ;; an Alt or Hyper key on your keyboard. On my keyboard, there is a key | |
48 | ;; labeled "Alt", but to make it act as an Alt key I have to put this command | |
49 | ;; into my .xinitrc: | |
50 | ;; xmodmap -e "add Mod3 = Alt_L" | |
51 | ;; Or, I can make it into a Hyper key with this: | |
4e8aa578 | 52 | ;; xmodmap -e "keysym Alt_L = Hyper_L" -e "add Mod2 = Hyper_L" |
88d690a9 RS |
53 | ;; Check with local X-perts for how to do it on your system. |
54 | ;; Then you can define your keybindings with code like this in your .emacs: | |
4e8aa578 RS |
55 | ;; (setq facemenu-keybindings |
56 | ;; '((default . [?\H-d]) | |
57 | ;; (bold . [?\H-b]) | |
58 | ;; (italic . [?\H-i]) | |
88d690a9 | 59 | ;; (bold-italic . [?\H-l]) |
4e8aa578 | 60 | ;; (underline . [?\H-u]))) |
9086c730 | 61 | ;; (facemenu-update) |
4e8aa578 | 62 | ;; (setq facemenu-keymap global-map) |
88d690a9 RS |
63 | ;; (define-key global-map [?\H-c] 'facemenu-set-foreground) ; set fg color |
64 | ;; (define-key global-map [?\H-C] 'facemenu-set-background) ; set bg color | |
4e8aa578 | 65 | ;; |
88d690a9 RS |
66 | ;; The order of the faces that appear in the menu and their keybindings can be |
67 | ;; controlled by setting the variables `facemenu-keybindings' and | |
b6a67507 CY |
68 | ;; `facemenu-new-faces-at-end'. List faces that you want to use in documents |
69 | ;; in `facemenu-listed-faces'. | |
4e8aa578 RS |
70 | |
71 | ;;; Known Problems: | |
88d690a9 RS |
72 | ;; Bold and Italic do not combine to create bold-italic if you select them |
73 | ;; both, although most other combinations (eg bold + underline + some color) | |
74 | ;; do the intuitive thing. | |
75 | ;; | |
4e8aa578 RS |
76 | ;; There is at present no way to display what the faces look like in |
77 | ;; the menu itself. | |
78 | ;; | |
79 | ;; `list-faces-display' shows the faces in a different order than | |
80 | ;; this menu, which could be confusing. I do /not/ sort the list | |
81 | ;; alphabetically, because I like the default order: it puts the most | |
82 | ;; basic, common fonts first. | |
83 | ;; | |
84 | ;; Please send me any other problems, comments or ideas. | |
85 | ||
86 | ;;; Code: | |
87 | ||
71296446 | 88 | (eval-when-compile |
0e520006 PA |
89 | (require 'help) |
90 | (require 'button)) | |
91 | ||
9086c730 RS |
92 | ;; Global bindings: |
93 | (define-key global-map [C-down-mouse-2] 'facemenu-menu) | |
6be7d8db | 94 | (define-key global-map "\M-o" 'facemenu-keymap) |
4e8aa578 | 95 | |
487e6fcb | 96 | (defgroup facemenu nil |
8e51619c | 97 | "Create a face menu for interactively adding fonts to text." |
487e6fcb RS |
98 | :group 'faces |
99 | :prefix "facemenu-") | |
100 | ||
101 | (defcustom facemenu-keybindings | |
6bdad9ae | 102 | (mapcar 'purecopy |
4e8aa578 RS |
103 | '((default . "d") |
104 | (bold . "b") | |
105 | (italic . "i") | |
88d690a9 | 106 | (bold-italic . "l") ; {bold} intersect {italic} = {l} |
6bdad9ae | 107 | (underline . "u"))) |
220c969f | 108 | "Alist of interesting faces and keybindings. |
4e8aa578 RS |
109 | Each element is itself a list: the car is the name of the face, |
110 | the next element is the key to use as a keyboard equivalent of the menu item; | |
9086c730 | 111 | the binding is made in `facemenu-keymap'. |
4e8aa578 RS |
112 | |
113 | The faces specifically mentioned in this list are put at the top of | |
b6a67507 CY |
114 | the menu, in the order specified. All other faces which are defined |
115 | in `facemenu-listed-faces' are listed after them, but get no | |
116 | keyboard equivalents. | |
4e8aa578 RS |
117 | |
118 | If you change this variable after loading facemenu.el, you will need to call | |
487e6fcb RS |
119 | `facemenu-update' to make it take effect." |
120 | :type '(repeat (cons face string)) | |
121 | :group 'facemenu) | |
4e8aa578 | 122 | |
487e6fcb | 123 | (defcustom facemenu-new-faces-at-end t |
9201cc28 | 124 | "Where in the menu to insert newly-created faces. |
88d690a9 | 125 | This should be nil to put them at the top of the menu, or t to put them |
487e6fcb RS |
126 | just before \"Other\" at the end." |
127 | :type 'boolean | |
128 | :group 'facemenu) | |
88d690a9 | 129 | |
ed50f0d2 CY |
130 | (defvar facemenu-unlisted-faces |
131 | `(modeline region secondary-selection highlight scratch-face | |
132 | ,(purecopy "^font-lock-") ,(purecopy "^gnus-") ,(purecopy "^message-") | |
133 | ,(purecopy "^ediff-") ,(purecopy "^term-") ,(purecopy "^vc-") | |
134 | ,(purecopy "^widget-") ,(purecopy "^custom-") ,(purecopy "^vm-")) | |
d9f2959e JB |
135 | "*List of faces that are of no interest to the user.") |
136 | (make-obsolete-variable 'facemenu-unlisted-faces 'facemenu-listed-faces | |
b8a2c14a | 137 | "22.1,\n and has no effect on the Face menu") |
ed50f0d2 | 138 | |
b6a67507 | 139 | (defcustom facemenu-listed-faces nil |
9201cc28 | 140 | "List of faces to include in the Face menu. |
d7beaf53 | 141 | Each element should be a symbol, the name of a face. |
b6a67507 | 142 | The \"basic \" faces in `facemenu-keybindings' are automatically |
d7beaf53 RS |
143 | added to the Face menu, and need not be in this list. |
144 | ||
145 | This value takes effect when you load facemenu.el. If the | |
146 | list includes symbols which are not defined as faces, they | |
147 | are ignored; however, subsequently defining or creating | |
148 | those faces adds them to the menu then. You can call | |
149 | `facemenu-update' to recalculate the menu contents, such as | |
150 | if you change the value of this variable, | |
151 | ||
152 | If this variable is t, all faces that you apply to text | |
153 | using the face menu commands (even by name), and all faces | |
154 | that you define or create, are added to the menu. You may | |
155 | find it useful to set this variable to t temporarily while | |
156 | you define some faces, so that they will be added. However, | |
157 | if the value is no longer t and you call `facemenu-update', | |
158 | it will remove any faces not explicitly in the list." | |
b6a67507 CY |
159 | :type '(choice (const :tag "List all faces" t) |
160 | (const :tag "None" nil) | |
161 | (repeat symbol)) | |
162 | :group 'facemenu | |
163 | :version "22.1") | |
88d690a9 RS |
164 | |
165 | (defvar facemenu-face-menu | |
bf7d4561 | 166 | (let ((map (make-sparse-keymap "Face"))) |
1e8780b1 | 167 | (define-key map "o" (cons (purecopy "Other...") 'facemenu-set-face)) |
bf7d4561 BG |
168 | map) |
169 | "Menu keymap for faces.") | |
88d690a9 | 170 | (defalias 'facemenu-face-menu facemenu-face-menu) |
6c763f36 | 171 | (put 'facemenu-face-menu 'menu-enable '(facemenu-enable-faces-p)) |
bf7d4561 | 172 | |
71296446 | 173 | (defvar facemenu-foreground-menu |
bf7d4561 | 174 | (let ((map (make-sparse-keymap "Foreground Color"))) |
1e8780b1 | 175 | (define-key map "o" (cons (purecopy "Other...") 'facemenu-set-foreground)) |
bf7d4561 BG |
176 | map) |
177 | "Menu keymap for foreground colors.") | |
88d690a9 | 178 | (defalias 'facemenu-foreground-menu facemenu-foreground-menu) |
6c763f36 | 179 | (put 'facemenu-foreground-menu 'menu-enable '(facemenu-enable-faces-p)) |
bf7d4561 BG |
180 | |
181 | (defvar facemenu-background-menu | |
182 | (let ((map (make-sparse-keymap "Background Color"))) | |
1e8780b1 | 183 | (define-key map "o" (cons (purecopy "Other...") 'facemenu-set-background)) |
bf7d4561 | 184 | map) |
7e6cb513 | 185 | "Menu keymap for background colors.") |
88d690a9 | 186 | (defalias 'facemenu-background-menu facemenu-background-menu) |
6c763f36 RS |
187 | (put 'facemenu-background-menu 'menu-enable '(facemenu-enable-faces-p)) |
188 | ||
189 | ;;; Condition for enabling menu items that set faces. | |
190 | (defun facemenu-enable-faces-p () | |
673c1168 CY |
191 | ;; Enable the facemenu if facemenu-add-face-function is defined |
192 | ;; (e.g. in Tex-mode and SGML mode), or if font-lock is off. | |
193 | (or (not (and font-lock-mode font-lock-defaults)) | |
194 | facemenu-add-face-function)) | |
bf7d4561 | 195 | |
71296446 | 196 | (defvar facemenu-special-menu |
bf7d4561 | 197 | (let ((map (make-sparse-keymap "Special"))) |
2d07ff84 DL |
198 | (define-key map [?s] (cons (purecopy "Remove Special") |
199 | 'facemenu-remove-special)) | |
200 | (define-key map [?t] (cons (purecopy "Intangible") | |
201 | 'facemenu-set-intangible)) | |
202 | (define-key map [?v] (cons (purecopy "Invisible") | |
203 | 'facemenu-set-invisible)) | |
204 | (define-key map [?r] (cons (purecopy "Read-Only") | |
205 | 'facemenu-set-read-only)) | |
bf7d4561 BG |
206 | map) |
207 | "Menu keymap for non-face text-properties.") | |
88d690a9 RS |
208 | (defalias 'facemenu-special-menu facemenu-special-menu) |
209 | ||
210 | (defvar facemenu-justification-menu | |
211 | (let ((map (make-sparse-keymap "Justification"))) | |
2d07ff84 DL |
212 | (define-key map [?c] (cons (purecopy "Center") 'set-justification-center)) |
213 | (define-key map [?b] (cons (purecopy "Full") 'set-justification-full)) | |
214 | (define-key map [?r] (cons (purecopy "Right") 'set-justification-right)) | |
215 | (define-key map [?l] (cons (purecopy "Left") 'set-justification-left)) | |
216 | (define-key map [?u] (cons (purecopy "Unfilled") 'set-justification-none)) | |
88d690a9 RS |
217 | map) |
218 | "Submenu for text justification commands.") | |
219 | (defalias 'facemenu-justification-menu facemenu-justification-menu) | |
220 | ||
221 | (defvar facemenu-indentation-menu | |
222 | (let ((map (make-sparse-keymap "Indentation"))) | |
71296446 | 223 | (define-key map [decrease-right-margin] |
2d07ff84 | 224 | (cons (purecopy "Indent Right Less") 'decrease-right-margin)) |
f34eaa2c | 225 | (define-key map [increase-right-margin] |
2d07ff84 | 226 | (cons (purecopy "Indent Right More") 'increase-right-margin)) |
f34eaa2c | 227 | (define-key map [decrease-left-margin] |
2d07ff84 | 228 | (cons (purecopy "Indent Less") 'decrease-left-margin)) |
f34eaa2c | 229 | (define-key map [increase-left-margin] |
2d07ff84 | 230 | (cons (purecopy "Indent More") 'increase-left-margin)) |
88d690a9 RS |
231 | map) |
232 | "Submenu for indentation commands.") | |
233 | (defalias 'facemenu-indentation-menu facemenu-indentation-menu) | |
bf7d4561 | 234 | |
f34eaa2c | 235 | ;; This is split up to avoid an overlong line in loaddefs.el. |
f34eaa2c | 236 | (defvar facemenu-menu nil |
535d2617 | 237 | "Facemenu top-level menu keymap.") |
f34eaa2c | 238 | (setq facemenu-menu (make-sparse-keymap "Text Properties")) |
f34eaa2c | 239 | (let ((map facemenu-menu)) |
2d07ff84 DL |
240 | (define-key map [dc] (cons (purecopy "Display Colors") 'list-colors-display)) |
241 | (define-key map [df] (cons (purecopy "Display Faces") 'list-faces-display)) | |
cea5ec30 RS |
242 | (define-key map [dp] (cons (purecopy "Describe Properties") |
243 | 'describe-text-properties)) | |
2d07ff84 DL |
244 | (define-key map [ra] (cons (purecopy "Remove Text Properties") |
245 | 'facemenu-remove-all)) | |
246 | (define-key map [rm] (cons (purecopy "Remove Face Properties") | |
247 | 'facemenu-remove-face-props)) | |
248 | (define-key map [s1] (list (purecopy "--")))) | |
f34eaa2c | 249 | (let ((map facemenu-menu)) |
71296446 | 250 | (define-key map [in] (cons (purecopy "Indentation") |
2d07ff84 DL |
251 | 'facemenu-indentation-menu)) |
252 | (define-key map [ju] (cons (purecopy "Justification") | |
253 | 'facemenu-justification-menu)) | |
254 | (define-key map [s2] (list (purecopy "--"))) | |
71296446 | 255 | (define-key map [sp] (cons (purecopy "Special Properties") |
2d07ff84 | 256 | 'facemenu-special-menu)) |
71296446 | 257 | (define-key map [bg] (cons (purecopy "Background Color") |
2d07ff84 | 258 | 'facemenu-background-menu)) |
71296446 | 259 | (define-key map [fg] (cons (purecopy "Foreground Color") |
2d07ff84 | 260 | 'facemenu-foreground-menu)) |
71296446 | 261 | (define-key map [fc] (cons (purecopy "Face") |
2d07ff84 | 262 | 'facemenu-face-menu))) |
88d690a9 | 263 | (defalias 'facemenu-menu facemenu-menu) |
bf7d4561 | 264 | |
71296446 | 265 | (defvar facemenu-keymap |
88d690a9 | 266 | (let ((map (make-sparse-keymap "Set face"))) |
2d07ff84 | 267 | (define-key map "o" (cons (purecopy "Other...") 'facemenu-set-face)) |
ef49d20f | 268 | (define-key map "\M-o" 'font-lock-fontify-block) |
88d690a9 | 269 | map) |
9dc90430 | 270 | "Keymap for face-changing commands. |
bf7d4561 | 271 | `Facemenu-update' fills in the keymap according to the bindings |
535d2617 | 272 | requested in `facemenu-keybindings'.") |
88d690a9 | 273 | (defalias 'facemenu-keymap facemenu-keymap) |
bf7d4561 | 274 | |
cb5bec6e | 275 | |
487e6fcb | 276 | (defcustom facemenu-add-face-function nil |
7e6cb513 | 277 | "Function called at beginning of text to change or nil. |
cb5bec6e | 278 | This function is passed the FACE to set and END of text to change, and must |
487e6fcb RS |
279 | return a string which is inserted. It may set `facemenu-end-add-face'." |
280 | :type '(choice (const :tag "None" nil) | |
281 | function) | |
282 | :group 'facemenu) | |
cb5bec6e | 283 | |
487e6fcb | 284 | (defcustom facemenu-end-add-face nil |
7e6cb513 | 285 | "String to insert or function called at end of text to change or nil. |
cb5bec6e | 286 | This function is passed the FACE to set, and must return a string which is |
487e6fcb RS |
287 | inserted." |
288 | :type '(choice (const :tag "None" nil) | |
289 | string | |
290 | function) | |
291 | :group 'facemenu) | |
cb5bec6e | 292 | |
487e6fcb | 293 | (defcustom facemenu-remove-face-function nil |
9086c730 | 294 | "When non-nil, this is a function called to remove faces. |
cb5bec6e | 295 | This function is passed the START and END of text to change. |
7e6cb513 | 296 | May also be t meaning to use `facemenu-add-face-function'." |
487e6fcb RS |
297 | :type '(choice (const :tag "None" nil) |
298 | (const :tag "Use add-face" t) | |
299 | function) | |
300 | :group 'facemenu) | |
cb5bec6e | 301 | |
bf7d4561 BG |
302 | ;;; Internal Variables |
303 | ||
304 | (defvar facemenu-color-alist nil | |
bf7d4561 | 305 | "Alist of colors, used for completion. |
a926c9ce | 306 | If this is nil, then the value of (defined-colors) is used.") |
4a24b314 | 307 | |
4e8aa578 | 308 | (defun facemenu-update () |
bf7d4561 BG |
309 | "Add or update the \"Face\" menu in the menu bar. |
310 | You can call this to update things if you change any of the menu configuration | |
311 | variables." | |
4e8aa578 | 312 | (interactive) |
4e8aa578 | 313 | |
bf7d4561 BG |
314 | ;; Add each defined face to the menu. |
315 | (facemenu-iterate 'facemenu-add-new-face | |
316 | (facemenu-complete-face-list facemenu-keybindings))) | |
4a24b314 | 317 | |
4e8aa578 | 318 | (defun facemenu-set-face (face &optional start end) |
cd7890bd RS |
319 | "Apply FACE to the region or next character typed. |
320 | ||
321 | If the region is active (normally true except in Transient | |
322 | Mark mode) and nonempty, and there is no prefix argument, | |
323 | this command applies FACE to the region. Otherwise, it applies FACE | |
324 | to the faces to use for the next character | |
325 | inserted. (Moving point or switching buffers before typing | |
326 | a character to insert cancels the specification.) | |
327 | ||
328 | If FACE is `default', to \"apply\" it means clearing | |
329 | the list of faces to be used. For any other value of FACE, | |
330 | to \"apply\" it means putting FACE at the front of the list | |
331 | of faces to be used, and removing any faces further | |
332 | along in the list that would be completely overridden by | |
333 | preceding faces (including FACE). | |
334 | ||
335 | This command can also add FACE to the menu of faces, | |
336 | if `facemenu-listed-faces' says to do that." | |
7d8177cf RS |
337 | (interactive (list (progn |
338 | (barf-if-buffer-read-only) | |
339 | (read-face-name "Use face")) | |
340 | (if (and mark-active (not current-prefix-arg)) | |
341 | (region-beginning)) | |
342 | (if (and mark-active (not current-prefix-arg)) | |
343 | (region-end)))) | |
88d690a9 | 344 | (facemenu-add-new-face face) |
7d8177cf | 345 | (facemenu-add-face face start end)) |
4a24b314 RS |
346 | |
347 | (defun facemenu-set-foreground (color &optional start end) | |
7e6cb513 | 348 | "Set the foreground COLOR of the region or next character typed. |
af1eab21 | 349 | This command reads the color in the minibuffer. |
7d8177cf RS |
350 | |
351 | If the region is active (normally true except in Transient Mark mode) | |
352 | and there is no prefix argument, this command sets the region to the | |
353 | requested face. | |
354 | ||
355 | Otherwise, this command specifies the face for the next character | |
356 | inserted. Moving point or switching buffers before | |
71296446 | 357 | typing a character to insert cancels the specification." |
7d8177cf RS |
358 | (interactive (list (progn |
359 | (barf-if-buffer-read-only) | |
360 | (facemenu-read-color "Foreground color: ")) | |
361 | (if (and mark-active (not current-prefix-arg)) | |
362 | (region-beginning)) | |
363 | (if (and mark-active (not current-prefix-arg)) | |
364 | (region-end)))) | |
b97c98ad LK |
365 | (facemenu-set-face-from-menu |
366 | (facemenu-add-new-color color 'facemenu-foreground-menu) | |
367 | start end)) | |
4a24b314 RS |
368 | |
369 | (defun facemenu-set-background (color &optional start end) | |
7e6cb513 | 370 | "Set the background COLOR of the region or next character typed. |
af1eab21 | 371 | This command reads the color in the minibuffer. |
7d8177cf RS |
372 | |
373 | If the region is active (normally true except in Transient Mark mode) | |
374 | and there is no prefix argument, this command sets the region to the | |
375 | requested face. | |
376 | ||
377 | Otherwise, this command specifies the face for the next character | |
378 | inserted. Moving point or switching buffers before | |
71296446 | 379 | typing a character to insert cancels the specification." |
7d8177cf RS |
380 | (interactive (list (progn |
381 | (barf-if-buffer-read-only) | |
382 | (facemenu-read-color "Background color: ")) | |
383 | (if (and mark-active (not current-prefix-arg)) | |
384 | (region-beginning)) | |
385 | (if (and mark-active (not current-prefix-arg)) | |
386 | (region-end)))) | |
b97c98ad LK |
387 | (facemenu-set-face-from-menu |
388 | (facemenu-add-new-color color 'facemenu-background-menu) | |
389 | start end)) | |
4e8aa578 RS |
390 | |
391 | (defun facemenu-set-face-from-menu (face start end) | |
7e6cb513 | 392 | "Set the FACE of the region or next character typed. |
b97c98ad LK |
393 | This function is designed to be called from a menu; FACE is determined |
394 | using the event type of the menu entry. If FACE is a symbol whose | |
395 | name starts with \"fg:\" or \"bg:\", then this functions sets the | |
396 | foreground or background to the color specified by the rest of the | |
397 | symbol's name. Any other symbol is considered the name of a face. | |
f34eaa2c | 398 | |
7d8177cf RS |
399 | If the region is active (normally true except in Transient Mark mode) |
400 | and there is no prefix argument, this command sets the region to the | |
401 | requested face. | |
f34eaa2c KH |
402 | |
403 | Otherwise, this command specifies the face for the next character | |
b97c98ad LK |
404 | inserted. Moving point or switching buffers before typing a character |
405 | to insert cancels the specification." | |
4a24b314 | 406 | (interactive (list last-command-event |
f34eaa2c KH |
407 | (if (and mark-active (not current-prefix-arg)) |
408 | (region-beginning)) | |
409 | (if (and mark-active (not current-prefix-arg)) | |
410 | (region-end)))) | |
88d690a9 | 411 | (barf-if-buffer-read-only) |
b97c98ad LK |
412 | (facemenu-add-face |
413 | (let ((fn (symbol-name face))) | |
414 | (if (string-match "\\`\\([fb]\\)g:\\(.+\\)" fn) | |
415 | (list (list (if (string= (match-string 1 fn) "f") | |
416 | :foreground | |
417 | :background) | |
418 | (match-string 2 fn))) | |
419 | face)) | |
420 | start end)) | |
4e8aa578 RS |
421 | |
422 | (defun facemenu-set-invisible (start end) | |
423 | "Make the region invisible. | |
424 | This sets the `invisible' text property; it can be undone with | |
f34eaa2c | 425 | `facemenu-remove-special'." |
4e8aa578 | 426 | (interactive "r") |
0e3edd7b | 427 | (add-text-properties start end '(invisible t))) |
4e8aa578 RS |
428 | |
429 | (defun facemenu-set-intangible (start end) | |
430 | "Make the region intangible: disallow moving into it. | |
431 | This sets the `intangible' text property; it can be undone with | |
f34eaa2c | 432 | `facemenu-remove-special'." |
4e8aa578 | 433 | (interactive "r") |
0e3edd7b | 434 | (add-text-properties start end '(intangible t))) |
4e8aa578 RS |
435 | |
436 | (defun facemenu-set-read-only (start end) | |
437 | "Make the region unmodifiable. | |
438 | This sets the `read-only' text property; it can be undone with | |
f34eaa2c | 439 | `facemenu-remove-special'." |
4e8aa578 | 440 | (interactive "r") |
0e3edd7b | 441 | (add-text-properties start end '(read-only t))) |
4e8aa578 | 442 | |
a32d7856 KH |
443 | (defun facemenu-remove-face-props (start end) |
444 | "Remove `face' and `mouse-face' text properties." | |
4e8aa578 RS |
445 | (interactive "*r") ; error if buffer is read-only despite the next line. |
446 | (let ((inhibit-read-only t)) | |
71296446 | 447 | (remove-text-properties |
a32d7856 | 448 | start end '(face nil mouse-face nil)))) |
4e8aa578 | 449 | |
f34eaa2c KH |
450 | (defun facemenu-remove-all (start end) |
451 | "Remove all text properties from the region." | |
452 | (interactive "*r") ; error if buffer is read-only despite the next line. | |
453 | (let ((inhibit-read-only t)) | |
454 | (set-text-properties start end nil))) | |
455 | ||
f34eaa2c KH |
456 | (defun facemenu-remove-special (start end) |
457 | "Remove all the \"special\" text properties from the region. | |
458 | These special properties include `invisible', `intangible' and `read-only'." | |
459 | (interactive "*r") ; error if buffer is read-only despite the next line. | |
460 | (let ((inhibit-read-only t)) | |
71296446 | 461 | (remove-text-properties |
f34eaa2c | 462 | start end '(invisible nil intangible nil read-only nil)))) |
0af1db42 | 463 | \f |
da627a71 | 464 | (defun facemenu-read-color (&optional prompt) |
bf7d4561 | 465 | "Read a color using the minibuffer." |
5bcc074b | 466 | (let* ((completion-ignore-case t) |
daad00fc CY |
467 | (color-list (or facemenu-color-alist (defined-colors))) |
468 | (completer | |
469 | (lambda (string pred all-completions) | |
470 | (if all-completions | |
471 | (or (all-completions string color-list pred) | |
472 | (if (color-defined-p string) | |
473 | (list string))) | |
474 | (or (try-completion string color-list pred) | |
475 | (if (color-defined-p string) | |
476 | string))))) | |
477 | (col (completing-read (or prompt "Color: ") completer nil t))) | |
bf7d4561 BG |
478 | (if (equal "" col) |
479 | nil | |
480 | col))) | |
4e8aa578 | 481 | |
f0bf7c8e JL |
482 | (defun color-rgb-to-hsv (r g b) |
483 | "For R, G, B color components return a list of hue, saturation, value. | |
484 | R, G, B input values should be in [0..65535] range. | |
485 | Output values for hue are integers in [0..360] range. | |
486 | Output values for saturation and value are integers in [0..100] range." | |
487 | (let* ((r (/ r 65535.0)) | |
488 | (g (/ g 65535.0)) | |
489 | (b (/ b 65535.0)) | |
490 | (max (max r g b)) | |
491 | (min (min r g b)) | |
492 | (h (cond ((= max min) 0) | |
493 | ((= max r) (mod (+ (* 60 (/ (- g b) (- max min))) 360) 360)) | |
494 | ((= max g) (+ (* 60 (/ (- b r) (- max min))) 120)) | |
495 | ((= max b) (+ (* 60 (/ (- r g) (- max min))) 240)))) | |
496 | (s (cond ((= max 0) 0) | |
497 | (t (- 1 (/ min max))))) | |
498 | (v max)) | |
499 | (list (round h) (round s 0.01) (round v 0.01)))) | |
500 | ||
501 | (defcustom list-colors-sort nil | |
502 | "Color sort order for `list-colors-display'. | |
503 | `nil' means default implementation-dependent order (defined in `x-colors'). | |
504 | `name' sorts by color name. | |
505 | `rgb' sorts by red, green, blue components. | |
8fd02581 | 506 | `(rgb-dist . COLOR)' sorts by the RGB distance to the specified color. |
f0bf7c8e | 507 | `hsv' sorts by hue, saturation, value. |
8fd02581 | 508 | `(hsv-dist . COLOR)' sorts by the HSV distance to the specified color |
f0bf7c8e JL |
509 | and excludes grayscale colors." |
510 | :type '(choice (const :tag "Unsorted" nil) | |
511 | (const :tag "Color Name" name) | |
512 | (const :tag "Red-Green-Blue" rgb) | |
513 | (cons :tag "Distance on RGB cube" | |
514 | (const :tag "Distance from Color" rgb-dist) | |
515 | (color :tag "Source Color Name")) | |
516 | (const :tag "Hue-Saturation-Value" hsv) | |
517 | (cons :tag "Distance on HSV cylinder" | |
518 | (const :tag "Distance from Color" hsv-dist) | |
519 | (color :tag "Source Color Name"))) | |
520 | :group 'facemenu | |
521 | :version "24.1") | |
522 | ||
523 | (defun list-colors-sort-key (color) | |
524 | "Return a list of keys for sorting colors depending on `list-colors-sort'. | |
525 | COLOR is the name of the color. When return value is nil, | |
526 | filter out the color from the output." | |
527 | (cond | |
528 | ((null list-colors-sort) color) | |
529 | ((eq list-colors-sort 'name) | |
530 | (downcase color)) | |
531 | ((eq list-colors-sort 'rgb) | |
532 | (color-values color)) | |
533 | ((eq (car-safe list-colors-sort) 'rgb-dist) | |
534 | (color-distance color (cdr list-colors-sort))) | |
535 | ((eq list-colors-sort 'hsv) | |
536 | (apply 'color-rgb-to-hsv (color-values color))) | |
537 | ((eq (car-safe list-colors-sort) 'hsv-dist) | |
538 | (let* ((c-rgb (color-values color)) | |
539 | (c-hsv (apply 'color-rgb-to-hsv c-rgb)) | |
540 | (o-hsv (apply 'color-rgb-to-hsv | |
541 | (color-values (cdr list-colors-sort))))) | |
542 | (unless (and (eq (nth 0 c-rgb) (nth 1 c-rgb)) ; exclude grayscale | |
543 | (eq (nth 1 c-rgb) (nth 2 c-rgb))) | |
544 | ;; 3D Euclidean distance (sqrt is not needed for sorting) | |
545 | (+ (expt (- 180 (abs (- 180 (abs (- (nth 0 c-hsv) ; wrap hue | |
546 | (nth 0 o-hsv)))))) 2) | |
547 | (expt (- (nth 1 c-hsv) (nth 1 o-hsv)) 2) | |
548 | (expt (- (nth 2 c-hsv) (nth 2 o-hsv)) 2))))))) | |
6f320937 CY |
549 | |
550 | (defun list-colors-display (&optional list buffer-name callback) | |
7c49006b RS |
551 | "Display names of defined colors, and show what they look like. |
552 | If the optional argument LIST is non-nil, it should be a list of | |
066a23af | 553 | colors to display. Otherwise, this command computes a list of |
6f320937 CY |
554 | colors that the current display can handle. |
555 | ||
556 | If the optional argument BUFFER-NAME is nil, it defaults to | |
557 | *Colors*. | |
558 | ||
559 | If the optional argument CALLBACK is non-nil, it should be a | |
560 | function to call each time the user types RET or clicks on a | |
561 | color. The function should accept a single argument, the color | |
f0bf7c8e JL |
562 | name. |
563 | ||
564 | You can change the color sort order by customizing `list-colors-sort'." | |
88d690a9 | 565 | (interactive) |
6062889d | 566 | (when (and (null list) (> (display-color-cells) 0)) |
066a23af | 567 | (setq list (list-colors-duplicates (defined-colors))) |
f0bf7c8e JL |
568 | (when list-colors-sort |
569 | ;; Schwartzian transform with `(color key1 key2 key3 ...)'. | |
570 | (setq list (mapcar | |
571 | 'car | |
572 | (sort (delq nil (mapcar | |
573 | (lambda (c) | |
574 | (let ((key (list-colors-sort-key | |
575 | (car c)))) | |
576 | (when key | |
577 | (cons c (if (consp key) key | |
578 | (list key)))))) | |
579 | list)) | |
580 | (lambda (a b) | |
581 | (let* ((a-keys (cdr a)) | |
582 | (b-keys (cdr b)) | |
583 | (a-key (car a-keys)) | |
584 | (b-key (car b-keys))) | |
585 | ;; Skip common keys at the beginning of key lists. | |
586 | (while (and a-key b-key (equal a-key b-key)) | |
587 | (setq a-keys (cdr a-keys) a-key (car a-keys) | |
588 | b-keys (cdr b-keys) b-key (car b-keys))) | |
589 | (cond | |
590 | ((and (numberp a-key) (numberp b-key)) | |
591 | (< a-key b-key)) | |
592 | ((and (stringp a-key) (stringp b-key)) | |
593 | (string< a-key b-key))))))))) | |
d2596700 MB |
594 | (when (memq (display-visual-class) '(gray-scale pseudo-color direct-color)) |
595 | ;; Don't show more than what the display can handle. | |
596 | (let ((lc (nthcdr (1- (display-color-cells)) list))) | |
597 | (if lc | |
598 | (setcdr lc nil))))) | |
6f320937 CY |
599 | (let ((buf (get-buffer-create "*Colors*"))) |
600 | (with-current-buffer buf | |
601 | (erase-buffer) | |
066a23af | 602 | (setq truncate-lines t) |
e020fb59 JL |
603 | ;; Display buffer before generating content to allow |
604 | ;; `list-colors-print' to get the right window-width. | |
605 | (pop-to-buffer buf) | |
6f320937 | 606 | (list-colors-print list callback) |
e020fb59 | 607 | (set-buffer-modified-p nil))) |
6f320937 CY |
608 | (if callback |
609 | (message "Click on a color to select it."))) | |
610 | ||
611 | (defun list-colors-print (list &optional callback) | |
612 | (let ((callback-fn | |
613 | (if callback | |
614 | `(lambda (button) | |
615 | (funcall ,callback (button-get button 'color-name)))))) | |
616 | (dolist (color list) | |
617 | (if (consp color) | |
618 | (if (cdr color) | |
619 | (setq color (sort color (lambda (a b) | |
620 | (string< (downcase a) | |
621 | (downcase b)))))) | |
622 | (setq color (list color))) | |
623 | (let* ((opoint (point)) | |
624 | (color-values (color-values (car color))) | |
625 | (light-p (>= (apply 'max color-values) | |
89877f5f CY |
626 | (* (car (color-values "white")) .5))) |
627 | (max-len (max (- (window-width) 33) 20))) | |
6f320937 CY |
628 | (insert (car color)) |
629 | (indent-to 22) | |
630 | (put-text-property opoint (point) 'face `(:background ,(car color))) | |
631 | (put-text-property | |
632 | (prog1 (point) | |
89877f5f CY |
633 | (insert " ") |
634 | (if (cdr color) | |
635 | ;; Insert as many color names as possible, fitting max-len. | |
636 | (let ((names (list (car color))) | |
637 | (others (cdr color)) | |
638 | (len (length (car color))) | |
639 | newlen) | |
640 | (while (and others | |
641 | (< (setq newlen (+ len 2 (length (car others)))) | |
642 | max-len)) | |
643 | (setq len newlen) | |
644 | (push (pop others) names)) | |
645 | (insert (mapconcat 'identity (nreverse names) ", "))) | |
646 | (insert (car color)))) | |
6f320937 CY |
647 | (point) |
648 | 'face (list :foreground (car color))) | |
649 | (indent-to (max (- (window-width) 8) 44)) | |
f0bf7c8e JL |
650 | (insert (propertize |
651 | (apply 'format "#%02x%02x%02x" | |
652 | (mapcar (lambda (c) (lsh c -8)) | |
653 | color-values)) | |
654 | 'mouse-face 'highlight | |
655 | 'help-echo | |
656 | (let ((hsv (apply 'color-rgb-to-hsv | |
657 | (color-values (car color))))) | |
658 | (format "H:%d S:%d V:%d" | |
659 | (nth 0 hsv) (nth 1 hsv) (nth 2 hsv))))) | |
6f320937 CY |
660 | (when callback |
661 | (make-text-button | |
662 | opoint (point) | |
663 | 'follow-link t | |
664 | 'mouse-face (list :background (car color) | |
665 | :foreground (if light-p "black" "white")) | |
666 | 'color-name (car color) | |
667 | 'action callback-fn))) | |
668 | (insert "\n")) | |
669 | (goto-char (point-min)))) | |
670 | ||
066a23af JL |
671 | |
672 | (defun list-colors-duplicates (&optional list) | |
673 | "Return a list of colors with grouped duplicate colors. | |
674 | If a color has no duplicates, then the element of the returned list | |
675 | has the form '(COLOR-NAME). The element of the returned list with | |
676 | duplicate colors has the form '(COLOR-NAME DUPLICATE-COLOR-NAME ...). | |
677 | This function uses the predicate `facemenu-color-equal' to compare | |
678 | color names. If the optional argument LIST is non-nil, it should | |
679 | be a list of colors to display. Otherwise, this function uses | |
680 | a list of colors that the current display can handle." | |
681 | (let* ((list (mapcar 'list (or list (defined-colors)))) | |
682 | (l list)) | |
683 | (while (cdr l) | |
684 | (if (and (facemenu-color-equal (car (car l)) (car (car (cdr l)))) | |
c3f9cd46 JR |
685 | (not (if (fboundp 'w32-default-color-map) |
686 | (not (assoc (car (car l)) (w32-default-color-map)))))) | |
066a23af JL |
687 | (progn |
688 | (setcdr (car l) (cons (car (car (cdr l))) (cdr (car l)))) | |
689 | (setcdr l (cdr (cdr l)))) | |
690 | (setq l (cdr l)))) | |
691 | list)) | |
88d690a9 RS |
692 | |
693 | (defun facemenu-color-equal (a b) | |
694 | "Return t if colors A and B are the same color. | |
7c49006b | 695 | A and B should be strings naming colors. |
f795f633 EZ |
696 | This function queries the display system to find out what the color |
697 | names mean. It returns nil if the colors differ or if it can't | |
7c49006b | 698 | determine the correct answer." |
88d690a9 | 699 | (cond ((equal a b) t) |
f795f633 | 700 | ((equal (color-values a) (color-values b))))) |
88d690a9 | 701 | |
a2e5caf7 SM |
702 | |
703 | (defvar facemenu-self-insert-data nil) | |
704 | ||
705 | (defun facemenu-post-self-insert-function () | |
706 | (when (and (car facemenu-self-insert-data) | |
707 | (eq last-command (cdr facemenu-self-insert-data))) | |
708 | (put-text-property (1- (point)) (point) | |
709 | 'face (car facemenu-self-insert-data)) | |
710 | (setq facemenu-self-insert-data nil)) | |
711 | (remove-hook 'post-self-insert-hook 'facemenu-post-self-insert-function)) | |
712 | ||
713 | (defun facemenu-set-self-insert-face (face) | |
714 | "Arrange for the next self-inserted char to have face `face'." | |
715 | (setq facemenu-self-insert-data (cons face this-command)) | |
716 | (add-hook 'post-self-insert-hook 'facemenu-post-self-insert-function)) | |
717 | ||
cb5bec6e | 718 | (defun facemenu-add-face (face &optional start end) |
4a24b314 | 719 | "Add FACE to text between START and END. |
7e6cb513 | 720 | If START is nil or START to END is empty, add FACE to next typed character |
cb5bec6e RS |
721 | instead. For each section of that region that has a different face property, |
722 | FACE will be consed onto it, and other faces that are completely hidden by | |
723 | that will be removed from the list. | |
af1eab21 | 724 | If `facemenu-add-face-function' and maybe `facemenu-end-add-face' are non-nil, |
cb5bec6e | 725 | they are used to set the face information. |
bf7d4561 BG |
726 | |
727 | As a special case, if FACE is `default', then the region is left with NO face | |
728 | text property. Otherwise, selecting the default face would not have any | |
cb5bec6e RS |
729 | effect. See `facemenu-remove-face-function'." |
730 | (interactive "*xFace: \nr") | |
a2e5caf7 SM |
731 | (cond |
732 | ((and (eq face 'default) | |
733 | (not (eq facemenu-remove-face-function t))) | |
734 | (if facemenu-remove-face-function | |
735 | (funcall facemenu-remove-face-function start end) | |
cb5bec6e | 736 | (if (and start (< start end)) |
a2e5caf7 SM |
737 | (remove-text-properties start end '(face default)) |
738 | (facemenu-set-self-insert-face 'default)))) | |
739 | (facemenu-add-face-function | |
740 | (save-excursion | |
741 | (if end (goto-char end)) | |
742 | (save-excursion | |
743 | (if start (goto-char start)) | |
744 | (insert-before-markers | |
745 | (funcall facemenu-add-face-function face end))) | |
746 | (if facemenu-end-add-face | |
747 | (insert (if (stringp facemenu-end-add-face) | |
748 | facemenu-end-add-face | |
749 | (funcall facemenu-end-add-face face)))))) | |
750 | ((and start (< start end)) | |
751 | (let ((part-start start) part-end) | |
752 | (while (not (= part-start end)) | |
753 | (setq part-end (next-single-property-change part-start 'face | |
754 | nil end)) | |
755 | (let ((prev (get-text-property part-start 'face))) | |
756 | (put-text-property part-start part-end 'face | |
757 | (if (null prev) | |
758 | face | |
759 | (facemenu-active-faces | |
760 | (cons face | |
761 | (if (listp prev) | |
762 | prev | |
763 | (list prev))) | |
764 | ;; Specify the selected frame | |
765 | ;; because nil would mean to use | |
766 | ;; the new-frame default settings, | |
767 | ;; and those are usually nil. | |
768 | (selected-frame))))) | |
769 | (setq part-start part-end)))) | |
770 | (t | |
771 | (facemenu-set-self-insert-face | |
772 | (if (eq last-command (cdr facemenu-self-insert-data)) | |
773 | (cons face (if (listp (car facemenu-self-insert-data)) | |
774 | (car facemenu-self-insert-data) | |
775 | (list (car facemenu-self-insert-data)))) | |
776 | face)))) | |
97a7aa7b RS |
777 | (unless (facemenu-enable-faces-p) |
778 | (message "Font-lock mode will override any faces you set in this buffer"))) | |
4a24b314 | 779 | |
5a79ed26 KH |
780 | (defun facemenu-active-faces (face-list &optional frame) |
781 | "Return from FACE-LIST those faces that would be used for display. | |
782 | This means each face attribute is not specified in a face earlier in FACE-LIST | |
783 | and such a face is therefore active when used to display text. | |
784 | If the optional argument FRAME is given, use the faces in that frame; otherwise | |
785 | use the selected frame. If t, then the global, non-frame faces are used." | |
7d8177cf RS |
786 | (let* ((mask-atts (copy-sequence |
787 | (if (consp (car face-list)) | |
f790dddf | 788 | (face-attributes-as-vector (car face-list)) |
7d8177cf RS |
789 | (or (internal-lisp-face-p (car face-list) frame) |
790 | (check-face (car face-list)))))) | |
5a79ed26 KH |
791 | (active-list (list (car face-list))) |
792 | (face-list (cdr face-list)) | |
793 | (mask-len (length mask-atts))) | |
794 | (while face-list | |
7d8177cf RS |
795 | (if (let ((face-atts |
796 | (if (consp (car face-list)) | |
f790dddf | 797 | (face-attributes-as-vector (car face-list)) |
7d8177cf RS |
798 | (or (internal-lisp-face-p (car face-list) frame) |
799 | (check-face (car face-list))))) | |
800 | (i mask-len) | |
801 | (useful nil)) | |
240c0c90 | 802 | (while (>= (setq i (1- i)) 0) |
7d8177cf RS |
803 | (and (not (memq (aref face-atts i) '(nil unspecified))) |
804 | (memq (aref mask-atts i) '(nil unspecified)) | |
5a79ed26 KH |
805 | (aset mask-atts i (setq useful t)))) |
806 | useful) | |
807 | (setq active-list (cons (car face-list) active-list))) | |
808 | (setq face-list (cdr face-list))) | |
809 | (nreverse active-list))) | |
4a24b314 | 810 | |
9bf4c4e5 | 811 | (defun facemenu-add-new-face (face) |
cd7890bd RS |
812 | "Add FACE (a face) to the Face menu if `facemenu-listed-faces' says so. |
813 | This is called whenever you create a new face, and at other times." | |
7d8177cf RS |
814 | (let* (name |
815 | symbol | |
9bf4c4e5 RS |
816 | menu docstring |
817 | (key (cdr (assoc face facemenu-keybindings))) | |
88d690a9 | 818 | function menu-val) |
9bf4c4e5 RS |
819 | (if (symbolp face) |
820 | (setq name (symbol-name face) | |
821 | symbol face) | |
822 | (setq name face | |
1d792b18 | 823 | symbol (intern name))) |
9bf4c4e5 RS |
824 | (setq menu 'facemenu-face-menu) |
825 | (setq docstring | |
1e8780b1 | 826 | (purecopy (format "Select face `%s' for subsequent insertion. |
3187841b RS |
827 | If the mark is active and there is no prefix argument, |
828 | apply face `%s' to the region instead. | |
829 | This command was defined by `facemenu-add-new-face'." | |
1e8780b1 | 830 | name name))) |
b6a67507 CY |
831 | (cond ((facemenu-iterate ; check if equivalent face is already in the menu |
832 | (lambda (m) (and (listp m) | |
833 | (symbolp (car m)) | |
f3359de1 RS |
834 | ;; Avoid error in face-equal |
835 | ;; when a non-face is erroneously present. | |
836 | (facep (car m)) | |
b6a67507 CY |
837 | (face-equal (car m) symbol))) |
838 | (cdr (symbol-function menu)))) | |
839 | ;; Faces with a keyboard equivalent. These go at the front. | |
840 | (key | |
88d690a9 RS |
841 | (setq function (intern (concat "facemenu-set-" name))) |
842 | (fset function | |
536f1a10 RS |
843 | `(lambda () |
844 | ,docstring | |
845 | (interactive) | |
af1eab21 RS |
846 | (facemenu-set-face |
847 | (quote ,symbol) | |
848 | (if (and mark-active (not current-prefix-arg)) | |
849 | (region-beginning)) | |
850 | (if (and mark-active (not current-prefix-arg)) | |
851 | (region-end))))) | |
88d690a9 RS |
852 | (define-key 'facemenu-keymap key (cons name function)) |
853 | (define-key menu key (cons name function))) | |
b6a67507 CY |
854 | ;; Faces with no keyboard equivalent. Figure out where to put it: |
855 | ((or (eq t facemenu-listed-faces) | |
856 | (memq symbol facemenu-listed-faces)) | |
7d8177cf | 857 | (setq key (vector symbol) |
88d690a9 RS |
858 | function 'facemenu-set-face-from-menu |
859 | menu-val (symbol-function menu)) | |
860 | (if (and facemenu-new-faces-at-end | |
b6a67507 | 861 | (> (length menu-val) 3)) |
88d690a9 | 862 | (define-key-after menu-val key (cons name function) |
9bf4c4e5 RS |
863 | (car (nth (- (length menu-val) 3) menu-val))) |
864 | (define-key menu key (cons name function)))))) | |
865 | nil) ; Return nil for facemenu-iterate | |
866 | ||
019b1899 | 867 | (defun facemenu-add-new-color (color menu) |
9bf4c4e5 | 868 | "Add COLOR (a color name string) to the appropriate Face menu. |
019b1899 | 869 | MENU should be `facemenu-foreground-menu' or `facemenu-background-menu'. |
b97c98ad | 870 | Return the event type (a symbol) of the added menu entry. |
9bf4c4e5 RS |
871 | |
872 | This is called whenever you use a new color." | |
019b1899 LK |
873 | (let (symbol docstring) |
874 | (unless (color-defined-p color) | |
875 | (error "Color `%s' undefined" color)) | |
9bf4c4e5 RS |
876 | (cond ((eq menu 'facemenu-foreground-menu) |
877 | (setq docstring | |
878 | (format "Select foreground color %s for subsequent insertion." | |
019b1899 | 879 | color) |
b97c98ad | 880 | symbol (intern (concat "fg:" color)))) |
9bf4c4e5 RS |
881 | ((eq menu 'facemenu-background-menu) |
882 | (setq docstring | |
883 | (format "Select background color %s for subsequent insertion." | |
019b1899 | 884 | color) |
b97c98ad | 885 | symbol (intern (concat "bg:" color)))) |
019b1899 | 886 | (t (error "MENU should be `facemenu-foreground-menu' or `facemenu-background-menu'"))) |
b97c98ad LK |
887 | (unless (facemenu-iterate ; Check if color is already in the menu. |
888 | (lambda (m) (and (listp m) | |
889 | (eq (car m) symbol))) | |
890 | (cdr (symbol-function menu))) | |
891 | ;; Color is not in the menu. Figure out where to put it. | |
892 | (let ((key (vector symbol)) | |
893 | (function 'facemenu-set-face-from-menu) | |
894 | (menu-val (symbol-function menu))) | |
895 | (if (and facemenu-new-faces-at-end | |
896 | (> (length menu-val) 3)) | |
897 | (define-key-after menu-val key (cons color function) | |
898 | (car (nth (- (length menu-val) 3) menu-val))) | |
899 | (define-key menu key (cons color function))))) | |
019b1899 | 900 | symbol)) |
bf7d4561 | 901 | |
bf7d4561 | 902 | (defun facemenu-complete-face-list (&optional oldlist) |
7cd49450 | 903 | "Return list of all faces that look different. |
71296446 | 904 | Starts with given ALIST of faces, and adds elements only if they display |
bf7d4561 | 905 | differently from any face already on the list. |
71296446 | 906 | The faces on ALIST will end up at the end of the returned list, in reverse |
bf7d4561 BG |
907 | order." |
908 | (let ((list (nreverse (mapcar 'car oldlist)))) | |
71296446 JB |
909 | (facemenu-iterate |
910 | (lambda (new-face) | |
bf7d4561 BG |
911 | (if (not (memq new-face list)) |
912 | (setq list (cons new-face list))) | |
913 | nil) | |
914 | (nreverse (face-list))) | |
915 | list)) | |
916 | ||
7e6cb513 | 917 | (defun facemenu-iterate (func list) |
4e8aa578 RS |
918 | "Apply FUNC to each element of LIST until one returns non-nil. |
919 | Returns the non-nil value it found, or nil if all were nil." | |
7e6cb513 SM |
920 | (while (and list (not (funcall func (car list)))) |
921 | (setq list (cdr list))) | |
922 | (car list)) | |
4e8aa578 RS |
923 | |
924 | (facemenu-update) | |
4e8aa578 | 925 | |
1ff4ace5 | 926 | (provide 'facemenu) |
ab5796a9 | 927 | |
cbee283d | 928 | ;; arch-tag: 85f6d02b-9085-420e-b651-0678f0e9c7eb |
4e8aa578 | 929 | ;;; facemenu.el ends here |