(c-mode-base-map, c++-mode-map):
[bpt/emacs.git] / lisp / facemenu.el
1 ;;; facemenu.el --- create a face menu for interactively adding fonts to text
2
3 ;; Copyright (c) 1994, 1995, 1996 Free Software Foundation, Inc.
4
5 ;; Author: Boris Goldowsky <boris@gnu.ai.mit.edu>
6 ;; Keywords: faces
7
8 ;; This file is 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 2, or (at your option)
13 ;; 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; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;; This file defines a menu of faces (bold, italic, etc) which allows you to
28 ;; set the face used for a region of the buffer. Some faces also have
29 ;; keybindings, which are shown in the menu. Faces with names beginning with
30 ;; "fg:" or "bg:", as in "fg:red", are treated specially.
31 ;; Such faces are assumed to consist only of a foreground (if "fg:") or
32 ;; background (if "bg:") color. They are thus put into the color submenus
33 ;; rather than the general Face submenu. These faces can also be
34 ;; automatically created by selecting the "Other..." menu items in the
35 ;; "Foreground" and "Background" submenus.
36 ;;
37 ;; The menu also contains submenus for indentation and justification-changing
38 ;; commands.
39
40 ;;; Usage:
41 ;; Selecting a face from the menu or typing the keyboard equivalent will
42 ;; change the region to use that face. If you use transient-mark-mode and the
43 ;; region is not active, the face will be remembered and used for the next
44 ;; insertion. It will be forgotten if you move point or make other
45 ;; modifications before inserting or typing anything.
46 ;;
47 ;; Faces can be selected from the keyboard as well.
48 ;; The standard keybindings are M-g (or ESC g) + letter:
49 ;; M-g i = "set italic", M-g b = "set bold", etc.
50
51 ;;; Customization:
52 ;; An alternative set of keybindings that may be easier to type can be set up
53 ;; using "Alt" or "Hyper" keys. This requires that you either have or create
54 ;; an Alt or Hyper key on your keyboard. On my keyboard, there is a key
55 ;; labeled "Alt", but to make it act as an Alt key I have to put this command
56 ;; into my .xinitrc:
57 ;; xmodmap -e "add Mod3 = Alt_L"
58 ;; Or, I can make it into a Hyper key with this:
59 ;; xmodmap -e "keysym Alt_L = Hyper_L" -e "add Mod2 = Hyper_L"
60 ;; Check with local X-perts for how to do it on your system.
61 ;; Then you can define your keybindings with code like this in your .emacs:
62 ;; (setq facemenu-keybindings
63 ;; '((default . [?\H-d])
64 ;; (bold . [?\H-b])
65 ;; (italic . [?\H-i])
66 ;; (bold-italic . [?\H-l])
67 ;; (underline . [?\H-u])))
68 ;; (facemenu-update)
69 ;; (setq facemenu-keymap global-map)
70 ;; (define-key global-map [?\H-c] 'facemenu-set-foreground) ; set fg color
71 ;; (define-key global-map [?\H-C] 'facemenu-set-background) ; set bg color
72 ;;
73 ;; The order of the faces that appear in the menu and their keybindings can be
74 ;; controlled by setting the variables `facemenu-keybindings' and
75 ;; `facemenu-new-faces-at-end'. List faces that you don't use in documents
76 ;; (eg, `region') in `facemenu-unlisted-faces'.
77
78 ;;; Known Problems:
79 ;; Bold and Italic do not combine to create bold-italic if you select them
80 ;; both, although most other combinations (eg bold + underline + some color)
81 ;; do the intuitive thing.
82 ;;
83 ;; There is at present no way to display what the faces look like in
84 ;; the menu itself.
85 ;;
86 ;; `list-faces-display' shows the faces in a different order than
87 ;; this menu, which could be confusing. I do /not/ sort the list
88 ;; alphabetically, because I like the default order: it puts the most
89 ;; basic, common fonts first.
90 ;;
91 ;; Please send me any other problems, comments or ideas.
92
93 ;;; Code:
94
95 (provide 'facemenu)
96
97 ;;; Provide some binding for startup:
98 ;;;###autoload (define-key global-map "\M-g" 'facemenu-keymap)
99 ;;;###autoload (autoload 'facemenu-keymap "facemenu" "Keymap for face-changing commands." t 'keymap)
100
101 ;; Global bindings:
102 (define-key global-map [C-down-mouse-2] 'facemenu-menu)
103 (define-key global-map "\M-g" 'facemenu-keymap)
104
105 (defgroup facemenu nil
106 "Create a face menu for interactively adding fonts to text"
107 :group 'faces
108 :prefix "facemenu-")
109
110 (defcustom facemenu-keybindings
111 '((default . "d")
112 (bold . "b")
113 (italic . "i")
114 (bold-italic . "l") ; {bold} intersect {italic} = {l}
115 (underline . "u"))
116 "Alist of interesting faces and keybindings.
117 Each element is itself a list: the car is the name of the face,
118 the next element is the key to use as a keyboard equivalent of the menu item;
119 the binding is made in `facemenu-keymap'.
120
121 The faces specifically mentioned in this list are put at the top of
122 the menu, in the order specified. All other faces which are defined,
123 except for those in `facemenu-unlisted-faces', are listed after them,
124 but get no keyboard equivalents.
125
126 If you change this variable after loading facemenu.el, you will need to call
127 `facemenu-update' to make it take effect."
128 :type '(repeat (cons face string))
129 :group 'facemenu)
130
131 (defcustom facemenu-new-faces-at-end t
132 "*Where in the menu to insert newly-created faces.
133 This should be nil to put them at the top of the menu, or t to put them
134 just before \"Other\" at the end."
135 :type 'boolean
136 :group 'facemenu)
137
138 (defcustom facemenu-unlisted-faces
139 '(modeline region secondary-selection highlight scratch-face)
140 "*List of faces not to include in the Face menu.
141 You can set this list before loading facemenu.el, or add a face to it before
142 creating that face if you do not want it to be listed. If you change the
143 variable so as to eliminate faces that have already been added to the menu,
144 call `facemenu-update' to recalculate the menu contents.
145
146 If this variable is t, no faces will be added to the menu. This is useful for
147 temporarily turning off the feature that automatically adds faces to the menu
148 when they are created."
149 :type '(choice (const :tag "Don't add" t)
150 (const :tag "None" nil)
151 (repeat face))
152 :group 'facemenu)
153
154 ;;;###autoload
155 (defvar facemenu-face-menu
156 (let ((map (make-sparse-keymap "Face")))
157 (define-key map "o" (cons "Other..." 'facemenu-set-face))
158 map)
159 "Menu keymap for faces.")
160 ;;;###autoload
161 (defalias 'facemenu-face-menu facemenu-face-menu)
162
163 ;;;###autoload
164 (defvar facemenu-foreground-menu
165 (let ((map (make-sparse-keymap "Foreground Color")))
166 (define-key map "o" (cons "Other..." 'facemenu-set-foreground))
167 map)
168 "Menu keymap for foreground colors.")
169 ;;;###autoload
170 (defalias 'facemenu-foreground-menu facemenu-foreground-menu)
171
172 ;;;###autoload
173 (defvar facemenu-background-menu
174 (let ((map (make-sparse-keymap "Background Color")))
175 (define-key map "o" (cons "Other..." 'facemenu-set-background))
176 map)
177 "Menu keymap for background colors")
178 ;;;###autoload
179 (defalias 'facemenu-background-menu facemenu-background-menu)
180
181 ;;;###autoload
182 (defvar facemenu-special-menu
183 (let ((map (make-sparse-keymap "Special")))
184 (define-key map [?s] (cons "Remove Special" 'facemenu-remove-special))
185 (define-key map [?t] (cons "Intangible" 'facemenu-set-intangible))
186 (define-key map [?v] (cons "Invisible" 'facemenu-set-invisible))
187 (define-key map [?r] (cons "Read-Only" 'facemenu-set-read-only))
188 map)
189 "Menu keymap for non-face text-properties.")
190 ;;;###autoload
191 (defalias 'facemenu-special-menu facemenu-special-menu)
192
193 ;;;###autoload
194 (defvar facemenu-justification-menu
195 (let ((map (make-sparse-keymap "Justification")))
196 (define-key map [?c] (cons "Center" 'set-justification-center))
197 (define-key map [?b] (cons "Full" 'set-justification-full))
198 (define-key map [?r] (cons "Right" 'set-justification-right))
199 (define-key map [?l] (cons "Left" 'set-justification-left))
200 (define-key map [?u] (cons "Unfilled" 'set-justification-none))
201 map)
202 "Submenu for text justification commands.")
203 ;;;###autoload
204 (defalias 'facemenu-justification-menu facemenu-justification-menu)
205
206 ;;;###autoload
207 (defvar facemenu-indentation-menu
208 (let ((map (make-sparse-keymap "Indentation")))
209 (define-key map [decrease-right-margin]
210 (cons "Indent Right Less" 'decrease-right-margin))
211 (define-key map [increase-right-margin]
212 (cons "Indent Right More" 'increase-right-margin))
213 (define-key map [decrease-left-margin]
214 (cons "Indent Less" 'decrease-left-margin))
215 (define-key map [increase-left-margin]
216 (cons "Indent More" 'increase-left-margin))
217 map)
218 "Submenu for indentation commands.")
219 ;;;###autoload
220 (defalias 'facemenu-indentation-menu facemenu-indentation-menu)
221
222 ;; This is split up to avoid an overlong line in loaddefs.el.
223 ;;;###autoload
224 (defvar facemenu-menu nil
225 "Facemenu top-level menu keymap.")
226 ;;;###autoload
227 (setq facemenu-menu (make-sparse-keymap "Text Properties"))
228 ;;;###autoload
229 (let ((map facemenu-menu))
230 (define-key map [dc] (cons "Display Colors" 'list-colors-display))
231 (define-key map [df] (cons "Display Faces" 'list-faces-display))
232 (define-key map [dp] (cons "List Properties" 'list-text-properties-at))
233 (define-key map [ra] (cons "Remove All" 'facemenu-remove-all))
234 (define-key map [rm] (cons "Remove Properties" 'facemenu-remove-props))
235 (define-key map [s1] (list "-----------------")))
236 ;;;###autoload
237 (let ((map facemenu-menu))
238 (define-key map [in] (cons "Indentation" 'facemenu-indentation-menu))
239 (define-key map [ju] (cons "Justification" 'facemenu-justification-menu))
240 (define-key map [s2] (list "-----------------"))
241 (define-key map [sp] (cons "Special Properties" 'facemenu-special-menu))
242 (define-key map [bg] (cons "Background Color" 'facemenu-background-menu))
243 (define-key map [fg] (cons "Foreground Color" 'facemenu-foreground-menu))
244 (define-key map [fc] (cons "Face" 'facemenu-face-menu)))
245 ;;;###autoload
246 (defalias 'facemenu-menu facemenu-menu)
247
248 (defvar facemenu-keymap
249 (let ((map (make-sparse-keymap "Set face")))
250 (define-key map "o" (cons "Other..." 'facemenu-set-face))
251 map)
252 "Keymap for face-changing commands.
253 `Facemenu-update' fills in the keymap according to the bindings
254 requested in `facemenu-keybindings'.")
255 (defalias 'facemenu-keymap facemenu-keymap)
256
257
258 (defcustom facemenu-add-face-function nil
259 "Function called at beginning of text to change or `nil'.
260 This function is passed the FACE to set and END of text to change, and must
261 return a string which is inserted. It may set `facemenu-end-add-face'."
262 :type '(choice (const :tag "None" nil)
263 function)
264 :group 'facemenu)
265
266 (defcustom facemenu-end-add-face nil
267 "String to insert or function called at end of text to change or `nil'.
268 This function is passed the FACE to set, and must return a string which is
269 inserted."
270 :type '(choice (const :tag "None" nil)
271 string
272 function)
273 :group 'facemenu)
274
275 (defcustom facemenu-remove-face-function nil
276 "When non-nil, this is a function called to remove faces.
277 This function is passed the START and END of text to change.
278 May also be `t' meaning to use `facemenu-add-face-function'."
279 :type '(choice (const :tag "None" nil)
280 (const :tag "Use add-face" t)
281 function)
282 :group 'facemenu)
283
284 ;;; Internal Variables
285
286 (defvar facemenu-color-alist nil
287 ;; Don't initialize here; that doesn't work if preloaded.
288 "Alist of colors, used for completion.
289 If null, `facemenu-read-color' will set it.")
290
291 (defun facemenu-update ()
292 "Add or update the \"Face\" menu in the menu bar.
293 You can call this to update things if you change any of the menu configuration
294 variables."
295 (interactive)
296
297 ;; Add each defined face to the menu.
298 (facemenu-iterate 'facemenu-add-new-face
299 (facemenu-complete-face-list facemenu-keybindings)))
300
301 ;;;###autoload
302 (defun facemenu-set-face (face &optional start end)
303 "Add FACE to the region or next character typed.
304 It will be added to the top of the face list; any faces lower on the list that
305 will not show through at all will be removed.
306
307 Interactively, the face to be used is read with the minibuffer.
308
309 If the region is active and there is no prefix argument,
310 this command sets the region to the requested face.
311
312 Otherwise, this command specifies the face for the next character
313 inserted. Moving point or switching buffers before
314 typing a character to insert cancels the specification."
315 (interactive (list (read-face-name "Use face: ")))
316 (barf-if-buffer-read-only)
317 (facemenu-add-new-face face)
318 (if (and mark-active (not current-prefix-arg))
319 (let ((start (or start (region-beginning)))
320 (end (or end (region-end))))
321 (facemenu-add-face face start end))
322 (facemenu-add-face face)))
323
324 ;;;###autoload
325 (defun facemenu-set-foreground (color &optional start end)
326 "Set the foreground color of the region or next character typed.
327 The color is prompted for. A face named `fg:color' is used \(or created).
328 If the region is active, it will be set to the requested face. If
329 it is inactive \(even if mark-even-if-inactive is set) the next
330 character that is typed \(via `self-insert-command') will be set to
331 the selected face. Moving point or switching buffers before
332 typing a character cancels the request."
333 (interactive (list (facemenu-read-color "Foreground color: ")))
334 (let ((face (intern (concat "fg:" color))))
335 (or (facemenu-get-face face)
336 (error "Unknown color: %s" color))
337 (facemenu-set-face face start end)))
338
339 ;;;###autoload
340 (defun facemenu-set-background (color &optional start end)
341 "Set the background color of the region or next character typed.
342 The color is prompted for. A face named `bg:color' is used \(or created).
343 If the region is active, it will be set to the requested face. If
344 it is inactive \(even if mark-even-if-inactive is set) the next
345 character that is typed \(via `self-insert-command') will be set to
346 the selected face. Moving point or switching buffers before
347 typing a character cancels the request."
348 (interactive (list (facemenu-read-color "Background color: ")))
349 (let ((face (intern (concat "bg:" color))))
350 (or (facemenu-get-face face)
351 (error "Unknown color: %s" color))
352 (facemenu-set-face face start end)))
353
354 ;;;###autoload
355 (defun facemenu-set-face-from-menu (face start end)
356 "Set the face of the region or next character typed.
357 This function is designed to be called from a menu; the face to use
358 is the menu item's name.
359
360 If the region is active and there is no prefix argument,
361 this command sets the region to the requested face.
362
363 Otherwise, this command specifies the face for the next character
364 inserted. Moving point or switching buffers before
365 typing a character to insert cancels the specification."
366 (interactive (list last-command-event
367 (if (and mark-active (not current-prefix-arg))
368 (region-beginning))
369 (if (and mark-active (not current-prefix-arg))
370 (region-end))))
371 (barf-if-buffer-read-only)
372 (facemenu-get-face face)
373 (if start
374 (facemenu-add-face face start end)
375 (facemenu-add-face face)))
376
377 ;;;###autoload
378 (defun facemenu-set-invisible (start end)
379 "Make the region invisible.
380 This sets the `invisible' text property; it can be undone with
381 `facemenu-remove-special'."
382 (interactive "r")
383 (add-text-properties start end '(invisible t)))
384
385 ;;;###autoload
386 (defun facemenu-set-intangible (start end)
387 "Make the region intangible: disallow moving into it.
388 This sets the `intangible' text property; it can be undone with
389 `facemenu-remove-special'."
390 (interactive "r")
391 (add-text-properties start end '(intangible t)))
392
393 ;;;###autoload
394 (defun facemenu-set-read-only (start end)
395 "Make the region unmodifiable.
396 This sets the `read-only' text property; it can be undone with
397 `facemenu-remove-special'."
398 (interactive "r")
399 (add-text-properties start end '(read-only t)))
400
401 ;;;###autoload
402 (defun facemenu-remove-props (start end)
403 "Remove all text properties that facemenu added to region."
404 (interactive "*r") ; error if buffer is read-only despite the next line.
405 (let ((inhibit-read-only t))
406 (remove-text-properties
407 start end '(face nil invisible nil intangible nil
408 read-only nil category nil))))
409
410 ;;;###autoload
411 (defun facemenu-remove-all (start end)
412 "Remove all text properties from the region."
413 (interactive "*r") ; error if buffer is read-only despite the next line.
414 (let ((inhibit-read-only t))
415 (set-text-properties start end nil)))
416
417 ;;;###autoload
418 (defun facemenu-remove-special (start end)
419 "Remove all the \"special\" text properties from the region.
420 These special properties include `invisible', `intangible' and `read-only'."
421 (interactive "*r") ; error if buffer is read-only despite the next line.
422 (let ((inhibit-read-only t))
423 (remove-text-properties
424 start end '(invisible nil intangible nil read-only nil))))
425
426 ;;;###autoload
427 (defun list-text-properties-at (p)
428 "Pop up a buffer listing text-properties at LOCATION."
429 (interactive "d")
430 (let ((props (text-properties-at p))
431 category
432 str)
433 (if (null props)
434 (message "None")
435 (if (and (not (cdr (cdr props)))
436 (not (eq (car props) 'category))
437 (< (length (setq str (format "Text property at %d: %s %S"
438 p (car props) (car (cdr props)))))
439 (frame-width)))
440 (message "%s" str)
441 (with-output-to-temp-buffer "*Text Properties*"
442 (princ (format "Text properties at %d:\n\n" p))
443 (while props
444 (if (eq (car props) 'category)
445 (setq category (car (cdr props))))
446 (princ (format "%-20s %S\n"
447 (car props) (car (cdr props))))
448 (setq props (cdr (cdr props))))
449 (if category
450 (progn
451 (setq props (symbol-plist category))
452 (princ (format "\nCategory %s:\n\n" category))
453 (while props
454 (princ (format "%-20s %S\n"
455 (car props) (car (cdr props))))
456 (if (eq (car props) 'category)
457 (setq category (car (cdr props))))
458 (setq props (cdr (cdr props)))))))))))
459
460 ;;;###autoload
461 (defun facemenu-read-color (&optional prompt)
462 "Read a color using the minibuffer."
463 (let ((col (completing-read (or prompt "Color: ")
464 (or facemenu-color-alist
465 (if window-system
466 (mapcar 'list (x-defined-colors))))
467 nil t)))
468 (if (equal "" col)
469 nil
470 col)))
471
472 ;;;###autoload
473 (defun list-colors-display (&optional list)
474 "Display names of defined colors, and show what they look like.
475 If the optional argument LIST is non-nil, it should be a list of
476 colors to display. Otherwise, this command computes a list
477 of colors that the current display can handle."
478 (interactive)
479 (if (and (null list) window-system)
480 (progn
481 (setq list (x-defined-colors))
482 ;; Delete duplicate colors.
483 (let ((l list))
484 (while (cdr l)
485 (if (facemenu-color-equal (car l) (car (cdr l)))
486 (setcdr l (cdr (cdr l)))
487 (setq l (cdr l)))))))
488 (with-output-to-temp-buffer "*Colors*"
489 (save-excursion
490 (set-buffer standard-output)
491 (let ((facemenu-unlisted-faces t)
492 s)
493 (while list
494 (setq s (point))
495 (insert (car list))
496 (indent-to 20)
497 (put-text-property s (point) 'face
498 (facemenu-get-face
499 (intern (concat "bg:" (car list)))))
500 (setq s (point))
501 (insert " " (car list) "\n")
502 (put-text-property s (point) 'face
503 (facemenu-get-face
504 (intern (concat "fg:" (car list)))))
505 (setq list (cdr list)))))))
506
507 (defun facemenu-color-equal (a b)
508 "Return t if colors A and B are the same color.
509 A and B should be strings naming colors.
510 This function queries the window-system server to find out what the
511 color names mean. It returns nil if the colors differ or if it can't
512 determine the correct answer."
513 (cond ((equal a b) t)
514 ((and (memq window-system '(x w32))
515 (equal (x-color-values a) (x-color-values b))))
516 ((eq window-system 'pc)
517 (and (x-color-defined-p a) (x-color-defined-p b)
518 (eq (msdos-color-translate a) (msdos-color-translate b))))))
519
520 (defun facemenu-add-face (face &optional start end)
521 "Add FACE to text between START and END.
522 If START is `nil' or START to END is empty, add FACE to next typed character
523 instead. For each section of that region that has a different face property,
524 FACE will be consed onto it, and other faces that are completely hidden by
525 that will be removed from the list.
526 If `facemenu-add-face-function' and maybe `facemenu-end-add-face' are non-`nil'
527 they are used to set the face information.
528
529 As a special case, if FACE is `default', then the region is left with NO face
530 text property. Otherwise, selecting the default face would not have any
531 effect. See `facemenu-remove-face-function'."
532 (interactive "*xFace: \nr")
533 (if (and (eq face 'default)
534 (not (eq facemenu-remove-face-function t)))
535 (if facemenu-remove-face-function
536 (funcall facemenu-remove-face-function start end)
537 (if (and start (< start end))
538 (remove-text-properties start end '(face default))
539 (setq self-insert-face 'default
540 self-insert-face-command this-command)))
541 (if facemenu-add-face-function
542 (save-excursion
543 (if end (goto-char end))
544 (save-excursion
545 (if start (goto-char start))
546 (insert-before-markers
547 (funcall facemenu-add-face-function face end)))
548 (if facemenu-end-add-face
549 (insert (if (stringp facemenu-end-add-face)
550 facemenu-end-add-face
551 (funcall facemenu-end-add-face face)))))
552 (if (and start (< start end))
553 (let ((part-start start) part-end)
554 (while (not (= part-start end))
555 (setq part-end (next-single-property-change part-start 'face
556 nil end))
557 (let ((prev (get-text-property part-start 'face)))
558 (put-text-property part-start part-end 'face
559 (if (null prev)
560 face
561 (facemenu-active-faces
562 (cons face
563 (if (listp prev)
564 prev
565 (list prev)))))))
566 (setq part-start part-end)))
567 (setq self-insert-face (if (eq last-command self-insert-face-command)
568 (cons face (if (listp self-insert-face)
569 self-insert-face
570 (list self-insert-face)))
571 face)
572 self-insert-face-command this-command)))))
573
574 (defun facemenu-active-faces (face-list &optional frame)
575 "Return from FACE-LIST those faces that would be used for display.
576 This means each face attribute is not specified in a face earlier in FACE-LIST
577 and such a face is therefore active when used to display text.
578 If the optional argument FRAME is given, use the faces in that frame; otherwise
579 use the selected frame. If t, then the global, non-frame faces are used."
580 (let* ((mask-atts (copy-sequence (internal-get-face (car face-list) frame)))
581 (active-list (list (car face-list)))
582 (face-list (cdr face-list))
583 (mask-len (length mask-atts)))
584 (while face-list
585 (if (let ((face-atts (internal-get-face (car face-list) frame))
586 (i mask-len) (useful nil))
587 (while (> (setq i (1- i)) 1)
588 (and (aref face-atts i) (not (aref mask-atts i))
589 (aset mask-atts i (setq useful t))))
590 useful)
591 (setq active-list (cons (car face-list) active-list)))
592 (setq face-list (cdr face-list)))
593 (nreverse active-list)))
594
595 (defun facemenu-get-face (symbol)
596 "Make sure FACE exists.
597 If not, it is created. If it is created and is of the form `fg:color', then
598 set the foreground to that color. If of the form `bg:color', set the
599 background. In any case, add it to the appropriate menu. Returns the face,
600 or nil if given a bad color."
601 (if (or (internal-find-face symbol)
602 (let* ((face (make-face symbol))
603 (name (symbol-name symbol))
604 (color (substring name 3)))
605 (cond ((string-match "^fg:" name)
606 (set-face-foreground face color)
607 (and window-system
608 (x-color-defined-p color)))
609 ((string-match "^bg:" name)
610 (set-face-background face color)
611 (and window-system
612 (x-color-defined-p color)))
613 (t))))
614 symbol))
615
616 (defun facemenu-add-new-face (face)
617 "Add a FACE to the appropriate Face menu.
618 Automatically called when a new face is created."
619 (let* ((name (symbol-name face))
620 menu docstring
621 (key (cdr (assoc face facemenu-keybindings)))
622 function menu-val)
623 (cond ((string-match "^fg:" name)
624 (setq name (substring name 3))
625 (setq docstring
626 (format "Select foreground color %s for subsequent insertion."
627 name))
628 (setq menu 'facemenu-foreground-menu))
629 ((string-match "^bg:" name)
630 (setq name (substring name 3))
631 (setq docstring
632 (format "Select background color %s for subsequent insertion."
633 name))
634 (setq menu 'facemenu-background-menu))
635 (t
636 (setq docstring
637 (format "Select face `%s' for subsequent insertion."
638 name))
639 (setq menu 'facemenu-face-menu)))
640 (cond ((eq t facemenu-unlisted-faces))
641 ((memq face facemenu-unlisted-faces))
642 (key ; has a keyboard equivalent. These go at the front.
643 (setq function (intern (concat "facemenu-set-" name)))
644 (fset function
645 `(lambda ()
646 ,docstring
647 (interactive)
648 (facemenu-set-face (quote ,face))))
649 (define-key 'facemenu-keymap key (cons name function))
650 (define-key menu key (cons name function)))
651 ((facemenu-iterate ; check if equivalent face is already in the menu
652 (lambda (m) (and (listp m)
653 (symbolp (car m))
654 (face-equal (car m) face)))
655 (cdr (symbol-function menu))))
656 (t ; No keyboard equivalent. Figure out where to put it:
657 (setq key (vector face)
658 function 'facemenu-set-face-from-menu
659 menu-val (symbol-function menu))
660 (if (and facemenu-new-faces-at-end
661 (> (length menu-val) 3))
662 (define-key-after menu-val key (cons name function)
663 (car (nth (- (length menu-val) 3) menu-val)))
664 (define-key menu key (cons name function))))))
665 nil) ; Return nil for facemenu-iterate
666
667 (defun facemenu-complete-face-list (&optional oldlist)
668 "Return list of all faces that look different.
669 Starts with given ALIST of faces, and adds elements only if they display
670 differently from any face already on the list.
671 The faces on ALIST will end up at the end of the returned list, in reverse
672 order."
673 (let ((list (nreverse (mapcar 'car oldlist))))
674 (facemenu-iterate
675 (lambda (new-face)
676 (if (not (memq new-face list))
677 (setq list (cons new-face list)))
678 nil)
679 (nreverse (face-list)))
680 list))
681
682 (defun facemenu-iterate (func iterate-list)
683 "Apply FUNC to each element of LIST until one returns non-nil.
684 Returns the non-nil value it found, or nil if all were nil."
685 (while (and iterate-list (not (funcall func (car iterate-list))))
686 (setq iterate-list (cdr iterate-list)))
687 (car iterate-list))
688
689 (facemenu-update)
690
691 ;;; facemenu.el ends here