Fix maintainer address.
[emacs.git] / lisp / facemenu.el
blob5ab3ccb76150e24dfe5cae20f62da4a47500542f
1 ;;; facemenu.el --- create a face menu for interactively adding fonts to text
3 ;; Copyright (c) 1994, 1995, 1996 Free Software Foundation, Inc.
5 ;; Author: Boris Goldowsky <boris@gnu.org>
6 ;; Keywords: faces
8 ;; This file is part of GNU Emacs.
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.
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.
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.
25 ;;; Commentary:
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.
37 ;; The menu also contains submenus for indentation and justification-changing
38 ;; commands.
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.
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.
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
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'.
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.
83 ;; There is at present no way to display what the faces look like in
84 ;; the menu itself.
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.
91 ;; Please send me any other problems, comments or ideas.
93 ;;; Code:
95 (provide 'facemenu)
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)
101 ;; Global bindings:
102 (define-key global-map [C-down-mouse-2] 'facemenu-menu)
103 (define-key global-map "\M-g" 'facemenu-keymap)
105 (defgroup facemenu nil
106 "Create a face menu for interactively adding fonts to text"
107 :group 'faces
108 :prefix "facemenu-")
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'.
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.
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)
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)
138 (defcustom facemenu-unlisted-faces
139 '(modeline region secondary-selection highlight scratch-face
140 "^font-lock-" "^gnus-" "^message-" "^ediff-" "^term-" "^vc-"
141 "^widget-" "^custom-" "^vm-")
142 "*List of faces not to include in the Face menu.
143 Each element may be either a symbol, which is the name of a face, or a string,
144 which is a regular expression to be matched against face names. Matching
145 faces will not be added to the menu.
147 You can set this list before loading facemenu.el, or add a face to it before
148 creating that face if you do not want it to be listed. If you change the
149 variable so as to eliminate faces that have already been added to the menu,
150 call `facemenu-update' to recalculate the menu contents.
152 If this variable is t, no faces will be added to the menu. This is useful for
153 temporarily turning off the feature that automatically adds faces to the menu
154 when they are created."
155 :type '(choice (const :tag "Don't add" t)
156 (const :tag "None" nil)
157 (repeat (choice symbol regexp)))
158 :group 'facemenu)
160 ;;;###autoload
161 (defvar facemenu-face-menu
162 (let ((map (make-sparse-keymap "Face")))
163 (define-key map "o" (cons "Other..." 'facemenu-set-face))
164 map)
165 "Menu keymap for faces.")
166 ;;;###autoload
167 (defalias 'facemenu-face-menu facemenu-face-menu)
169 ;;;###autoload
170 (defvar facemenu-foreground-menu
171 (let ((map (make-sparse-keymap "Foreground Color")))
172 (define-key map "o" (cons "Other..." 'facemenu-set-foreground))
173 map)
174 "Menu keymap for foreground colors.")
175 ;;;###autoload
176 (defalias 'facemenu-foreground-menu facemenu-foreground-menu)
178 ;;;###autoload
179 (defvar facemenu-background-menu
180 (let ((map (make-sparse-keymap "Background Color")))
181 (define-key map "o" (cons "Other..." 'facemenu-set-background))
182 map)
183 "Menu keymap for background colors")
184 ;;;###autoload
185 (defalias 'facemenu-background-menu facemenu-background-menu)
187 ;;;###autoload
188 (defvar facemenu-special-menu
189 (let ((map (make-sparse-keymap "Special")))
190 (define-key map [?s] (cons "Remove Special" 'facemenu-remove-special))
191 (define-key map [?t] (cons "Intangible" 'facemenu-set-intangible))
192 (define-key map [?v] (cons "Invisible" 'facemenu-set-invisible))
193 (define-key map [?r] (cons "Read-Only" 'facemenu-set-read-only))
194 map)
195 "Menu keymap for non-face text-properties.")
196 ;;;###autoload
197 (defalias 'facemenu-special-menu facemenu-special-menu)
199 ;;;###autoload
200 (defvar facemenu-justification-menu
201 (let ((map (make-sparse-keymap "Justification")))
202 (define-key map [?c] (cons "Center" 'set-justification-center))
203 (define-key map [?b] (cons "Full" 'set-justification-full))
204 (define-key map [?r] (cons "Right" 'set-justification-right))
205 (define-key map [?l] (cons "Left" 'set-justification-left))
206 (define-key map [?u] (cons "Unfilled" 'set-justification-none))
207 map)
208 "Submenu for text justification commands.")
209 ;;;###autoload
210 (defalias 'facemenu-justification-menu facemenu-justification-menu)
212 ;;;###autoload
213 (defvar facemenu-indentation-menu
214 (let ((map (make-sparse-keymap "Indentation")))
215 (define-key map [decrease-right-margin]
216 (cons "Indent Right Less" 'decrease-right-margin))
217 (define-key map [increase-right-margin]
218 (cons "Indent Right More" 'increase-right-margin))
219 (define-key map [decrease-left-margin]
220 (cons "Indent Less" 'decrease-left-margin))
221 (define-key map [increase-left-margin]
222 (cons "Indent More" 'increase-left-margin))
223 map)
224 "Submenu for indentation commands.")
225 ;;;###autoload
226 (defalias 'facemenu-indentation-menu facemenu-indentation-menu)
228 ;; This is split up to avoid an overlong line in loaddefs.el.
229 ;;;###autoload
230 (defvar facemenu-menu nil
231 "Facemenu top-level menu keymap.")
232 ;;;###autoload
233 (setq facemenu-menu (make-sparse-keymap "Text Properties"))
234 ;;;###autoload
235 (let ((map facemenu-menu))
236 (define-key map [dc] (cons "Display Colors" 'list-colors-display))
237 (define-key map [df] (cons "Display Faces" 'list-faces-display))
238 (define-key map [dp] (cons "List Properties" 'list-text-properties-at))
239 (define-key map [ra] (cons "Remove Text Properties" 'facemenu-remove-all))
240 (define-key map [rm] (cons "Remove Face Properties" 'facemenu-remove-face-props))
241 (define-key map [s1] (list "-----------------")))
242 ;;;###autoload
243 (let ((map facemenu-menu))
244 (define-key map [in] (cons "Indentation" 'facemenu-indentation-menu))
245 (define-key map [ju] (cons "Justification" 'facemenu-justification-menu))
246 (define-key map [s2] (list "-----------------"))
247 (define-key map [sp] (cons "Special Properties" 'facemenu-special-menu))
248 (define-key map [bg] (cons "Background Color" 'facemenu-background-menu))
249 (define-key map [fg] (cons "Foreground Color" 'facemenu-foreground-menu))
250 (define-key map [fc] (cons "Face" 'facemenu-face-menu)))
251 ;;;###autoload
252 (defalias 'facemenu-menu facemenu-menu)
254 (defvar facemenu-keymap
255 (let ((map (make-sparse-keymap "Set face")))
256 (define-key map "o" (cons "Other..." 'facemenu-set-face))
257 map)
258 "Keymap for face-changing commands.
259 `Facemenu-update' fills in the keymap according to the bindings
260 requested in `facemenu-keybindings'.")
261 (defalias 'facemenu-keymap facemenu-keymap)
264 (defcustom facemenu-add-face-function nil
265 "Function called at beginning of text to change or `nil'.
266 This function is passed the FACE to set and END of text to change, and must
267 return a string which is inserted. It may set `facemenu-end-add-face'."
268 :type '(choice (const :tag "None" nil)
269 function)
270 :group 'facemenu)
272 (defcustom facemenu-end-add-face nil
273 "String to insert or function called at end of text to change or `nil'.
274 This function is passed the FACE to set, and must return a string which is
275 inserted."
276 :type '(choice (const :tag "None" nil)
277 string
278 function)
279 :group 'facemenu)
281 (defcustom facemenu-remove-face-function nil
282 "When non-nil, this is a function called to remove faces.
283 This function is passed the START and END of text to change.
284 May also be `t' meaning to use `facemenu-add-face-function'."
285 :type '(choice (const :tag "None" nil)
286 (const :tag "Use add-face" t)
287 function)
288 :group 'facemenu)
290 ;;; Internal Variables
292 (defvar facemenu-color-alist nil
293 ;; Don't initialize here; that doesn't work if preloaded.
294 "Alist of colors, used for completion.
295 If null, `facemenu-read-color' will set it.")
297 (defun facemenu-update ()
298 "Add or update the \"Face\" menu in the menu bar.
299 You can call this to update things if you change any of the menu configuration
300 variables."
301 (interactive)
303 ;; Add each defined face to the menu.
304 (facemenu-iterate 'facemenu-add-new-face
305 (facemenu-complete-face-list facemenu-keybindings)))
307 ;;;###autoload
308 (defun facemenu-set-face (face &optional start end)
309 "Add FACE to the region or next character typed.
310 It will be added to the top of the face list; any faces lower on the list that
311 will not show through at all will be removed.
313 Interactively, the face to be used is read with the minibuffer.
315 If the region is active and there is no prefix argument,
316 this command sets the region to the requested face.
318 Otherwise, this command specifies the face for the next character
319 inserted. Moving point or switching buffers before
320 typing a character to insert cancels the specification."
321 (interactive (list (read-face-name "Use face: ")))
322 (barf-if-buffer-read-only)
323 (facemenu-add-new-face face)
324 (if (and mark-active (not current-prefix-arg))
325 (let ((start (or start (region-beginning)))
326 (end (or end (region-end))))
327 (facemenu-add-face face start end))
328 (facemenu-add-face face)))
330 ;;;###autoload
331 (defun facemenu-set-foreground (color &optional start end)
332 "Set the foreground color of the region or next character typed.
333 The color is prompted for. A face named `fg:color' is used \(or created).
334 If the region is active, it will be set to the requested face. If
335 it is inactive \(even if mark-even-if-inactive is set) the next
336 character that is typed \(via `self-insert-command') will be set to
337 the selected face. Moving point or switching buffers before
338 typing a character cancels the request."
339 (interactive (list (facemenu-read-color "Foreground color: ")))
340 (let ((face (intern (concat "fg:" color))))
341 (or (facemenu-get-face face)
342 (error "Unknown color: %s" color))
343 (facemenu-set-face face start end)))
345 ;;;###autoload
346 (defun facemenu-set-background (color &optional start end)
347 "Set the background color of the region or next character typed.
348 The color is prompted for. A face named `bg:color' is used \(or created).
349 If the region is active, it will be set to the requested face. If
350 it is inactive \(even if mark-even-if-inactive is set) the next
351 character that is typed \(via `self-insert-command') will be set to
352 the selected face. Moving point or switching buffers before
353 typing a character cancels the request."
354 (interactive (list (facemenu-read-color "Background color: ")))
355 (let ((face (intern (concat "bg:" color))))
356 (or (facemenu-get-face face)
357 (error "Unknown color: %s" color))
358 (facemenu-set-face face start end)))
360 ;;;###autoload
361 (defun facemenu-set-face-from-menu (face start end)
362 "Set the face of the region or next character typed.
363 This function is designed to be called from a menu; the face to use
364 is the menu item's name.
366 If the region is active and there is no prefix argument,
367 this command sets the region to the requested face.
369 Otherwise, this command specifies the face for the next character
370 inserted. Moving point or switching buffers before
371 typing a character to insert cancels the specification."
372 (interactive (list last-command-event
373 (if (and mark-active (not current-prefix-arg))
374 (region-beginning))
375 (if (and mark-active (not current-prefix-arg))
376 (region-end))))
377 (barf-if-buffer-read-only)
378 (facemenu-get-face face)
379 (if start
380 (facemenu-add-face face start end)
381 (facemenu-add-face face)))
383 ;;;###autoload
384 (defun facemenu-set-invisible (start end)
385 "Make the region invisible.
386 This sets the `invisible' text property; it can be undone with
387 `facemenu-remove-special'."
388 (interactive "r")
389 (add-text-properties start end '(invisible t)))
391 ;;;###autoload
392 (defun facemenu-set-intangible (start end)
393 "Make the region intangible: disallow moving into it.
394 This sets the `intangible' text property; it can be undone with
395 `facemenu-remove-special'."
396 (interactive "r")
397 (add-text-properties start end '(intangible t)))
399 ;;;###autoload
400 (defun facemenu-set-read-only (start end)
401 "Make the region unmodifiable.
402 This sets the `read-only' text property; it can be undone with
403 `facemenu-remove-special'."
404 (interactive "r")
405 (add-text-properties start end '(read-only t)))
407 ;;;###autoload
408 (defun facemenu-remove-face-props (start end)
409 "Remove `face' and `mouse-face' text properties."
410 (interactive "*r") ; error if buffer is read-only despite the next line.
411 (let ((inhibit-read-only t))
412 (remove-text-properties
413 start end '(face nil mouse-face nil))))
415 ;;;###autoload
416 (defun facemenu-remove-all (start end)
417 "Remove all text properties from the region."
418 (interactive "*r") ; error if buffer is read-only despite the next line.
419 (let ((inhibit-read-only t))
420 (set-text-properties start end nil)))
422 ;;;###autoload
423 (defun facemenu-remove-special (start end)
424 "Remove all the \"special\" text properties from the region.
425 These special properties include `invisible', `intangible' and `read-only'."
426 (interactive "*r") ; error if buffer is read-only despite the next line.
427 (let ((inhibit-read-only t))
428 (remove-text-properties
429 start end '(invisible nil intangible nil read-only nil))))
431 ;;;###autoload
432 (defun list-text-properties-at (p)
433 "Pop up a buffer listing text-properties at LOCATION."
434 (interactive "d")
435 (let ((props (text-properties-at p))
436 category
437 str)
438 (if (null props)
439 (message "None")
440 (if (and (not (cdr (cdr props)))
441 (not (eq (car props) 'category))
442 (< (length (setq str (format "Text property at %d: %s %S"
443 p (car props) (car (cdr props)))))
444 (frame-width)))
445 (message "%s" str)
446 (with-output-to-temp-buffer "*Text Properties*"
447 (princ (format "Text properties at %d:\n\n" p))
448 (while props
449 (if (eq (car props) 'category)
450 (setq category (car (cdr props))))
451 (princ (format "%-20s %S\n"
452 (car props) (car (cdr props))))
453 (setq props (cdr (cdr props))))
454 (if category
455 (progn
456 (setq props (symbol-plist category))
457 (princ (format "\nCategory %s:\n\n" category))
458 (while props
459 (princ (format "%-20s %S\n"
460 (car props) (car (cdr props))))
461 (if (eq (car props) 'category)
462 (setq category (car (cdr props))))
463 (setq props (cdr (cdr props)))))))))))
465 ;;;###autoload
466 (defun facemenu-read-color (&optional prompt)
467 "Read a color using the minibuffer."
468 (let ((col (completing-read (or prompt "Color: ")
469 (or facemenu-color-alist
470 (if window-system
471 (mapcar 'list (x-defined-colors))
472 (mapcar 'list (tty-defined-colors))))
473 nil t)))
474 (if (equal "" col)
476 col)))
478 ;;;###autoload
479 (defun list-colors-display (&optional list)
480 "Display names of defined colors, and show what they look like.
481 If the optional argument LIST is non-nil, it should be a list of
482 colors to display. Otherwise, this command computes a list
483 of colors that the current display can handle."
484 (interactive)
485 (when (null list)
486 (setq list (if window-system
487 (x-defined-colors)
488 (tty-defined-colors)))
489 ;; Delete duplicate colors.
490 (let ((l list))
491 (while (cdr l)
492 (if (facemenu-color-equal (car l) (car (cdr l)))
493 (setcdr l (cdr (cdr l)))
494 (setq l (cdr l))))))
495 (with-output-to-temp-buffer "*Colors*"
496 (save-excursion
497 (set-buffer standard-output)
498 (let (s)
499 (while list
500 (setq s (point))
501 (insert (car list))
502 (indent-to 20)
503 (put-text-property s (point) 'face
504 (cons 'background-color (car list)))
505 (setq s (point))
506 (insert " " (car list) "\n")
507 (put-text-property s (point) 'face
508 (cons 'foreground-color (car list)))
509 (setq list (cdr list)))))))
511 (defun facemenu-color-equal (a b)
512 "Return t if colors A and B are the same color.
513 A and B should be strings naming colors.
514 This function queries the window-system server to find out what the
515 color names mean. It returns nil if the colors differ or if it can't
516 determine the correct answer."
517 (cond ((equal a b) t)
518 ((and (memq window-system '(x w32))
519 (equal (x-color-values a) (x-color-values b))))
520 ((eq window-system 'pc)
521 (and (x-color-defined-p a) (x-color-defined-p b)
522 (eq (msdos-color-translate a) (msdos-color-translate b))))))
524 (defun facemenu-add-face (face &optional start end)
525 "Add FACE to text between START and END.
526 If START is `nil' or START to END is empty, add FACE to next typed character
527 instead. For each section of that region that has a different face property,
528 FACE will be consed onto it, and other faces that are completely hidden by
529 that will be removed from the list.
530 If `facemenu-add-face-function' and maybe `facemenu-end-add-face' are non-`nil'
531 they are used to set the face information.
533 As a special case, if FACE is `default', then the region is left with NO face
534 text property. Otherwise, selecting the default face would not have any
535 effect. See `facemenu-remove-face-function'."
536 (interactive "*xFace: \nr")
537 (if (and (eq face 'default)
538 (not (eq facemenu-remove-face-function t)))
539 (if facemenu-remove-face-function
540 (funcall facemenu-remove-face-function start end)
541 (if (and start (< start end))
542 (remove-text-properties start end '(face default))
543 (setq self-insert-face 'default
544 self-insert-face-command this-command)))
545 (if facemenu-add-face-function
546 (save-excursion
547 (if end (goto-char end))
548 (save-excursion
549 (if start (goto-char start))
550 (insert-before-markers
551 (funcall facemenu-add-face-function face end)))
552 (if facemenu-end-add-face
553 (insert (if (stringp facemenu-end-add-face)
554 facemenu-end-add-face
555 (funcall facemenu-end-add-face face)))))
556 (if (and start (< start end))
557 (let ((part-start start) part-end)
558 (while (not (= part-start end))
559 (setq part-end (next-single-property-change part-start 'face
560 nil end))
561 (let ((prev (get-text-property part-start 'face)))
562 (put-text-property part-start part-end 'face
563 (if (null prev)
564 face
565 (facemenu-active-faces
566 (cons face
567 (if (listp prev)
568 prev
569 (list prev)))))))
570 (setq part-start part-end)))
571 (setq self-insert-face (if (eq last-command self-insert-face-command)
572 (cons face (if (listp self-insert-face)
573 self-insert-face
574 (list self-insert-face)))
575 face)
576 self-insert-face-command this-command)))))
578 (defun facemenu-active-faces (face-list &optional frame)
579 "Return from FACE-LIST those faces that would be used for display.
580 This means each face attribute is not specified in a face earlier in FACE-LIST
581 and such a face is therefore active when used to display text.
582 If the optional argument FRAME is given, use the faces in that frame; otherwise
583 use the selected frame. If t, then the global, non-frame faces are used."
584 (let* ((mask-atts (copy-sequence (internal-get-face (car face-list) frame)))
585 (active-list (list (car face-list)))
586 (face-list (cdr face-list))
587 (mask-len (length mask-atts)))
588 (while face-list
589 (if (let ((face-atts (internal-get-face (car face-list) frame))
590 (i mask-len) (useful nil))
591 (while (> (setq i (1- i)) 1)
592 (and (aref face-atts i) (not (aref mask-atts i))
593 (aset mask-atts i (setq useful t))))
594 useful)
595 (setq active-list (cons (car face-list) active-list)))
596 (setq face-list (cdr face-list)))
597 (nreverse active-list)))
599 (defun facemenu-get-face (symbol)
600 "Make sure FACE exists.
601 If not, create it and add it to the appropriate menu. Return the symbol.
603 If a window system is in use, and this function creates a face named
604 `fg:color', then it sets the foreground to that color. Likewise, `bg:color'
605 means to set the background. In either case, if the color is undefined,
606 no color is set and a warning is issued."
607 (let ((name (symbol-name symbol))
608 foreground)
609 (cond ((internal-find-face symbol))
610 ((and window-system
611 (or (setq foreground (string-match "^fg:" name))
612 (string-match "^bg:" name)))
613 (let ((face (make-face symbol))
614 (color (substring name 3)))
615 (if (x-color-defined-p color)
616 (if foreground
617 (set-face-foreground face color)
618 (set-face-background face color))
619 (message "Color \"%s\" undefined" color))))
620 (t (make-face symbol))))
621 symbol)
623 (defun facemenu-add-new-face (face)
624 "Add a FACE to the appropriate Face menu.
625 Automatically called when a new face is created."
626 (let* ((name (symbol-name face))
627 menu docstring
628 (key (cdr (assoc face facemenu-keybindings)))
629 function menu-val)
630 (cond ((string-match "^fg:" name)
631 (setq name (substring name 3))
632 (setq docstring
633 (format "Select foreground color %s for subsequent insertion."
634 name))
635 (setq menu 'facemenu-foreground-menu))
636 ((string-match "^bg:" name)
637 (setq name (substring name 3))
638 (setq docstring
639 (format "Select background color %s for subsequent insertion."
640 name))
641 (setq menu 'facemenu-background-menu))
643 (setq docstring
644 (format "Select face `%s' for subsequent insertion."
645 name))
646 (setq menu 'facemenu-face-menu)))
647 (cond ((eq t facemenu-unlisted-faces))
648 ((memq face facemenu-unlisted-faces))
649 ;; test against regexps in facemenu-unlisted-faces
650 ((let ((unlisted facemenu-unlisted-faces)
651 (matched nil))
652 (while (and unlisted (not matched))
653 (if (and (stringp (car unlisted))
654 (string-match (car unlisted) name))
655 (setq matched t)
656 (setq unlisted (cdr unlisted))))
657 matched))
658 (key ; has a keyboard equivalent. These go at the front.
659 (setq function (intern (concat "facemenu-set-" name)))
660 (fset function
661 `(lambda ()
662 ,docstring
663 (interactive)
664 (facemenu-set-face (quote ,face))))
665 (define-key 'facemenu-keymap key (cons name function))
666 (define-key menu key (cons name function)))
667 ((facemenu-iterate ; check if equivalent face is already in the menu
668 (lambda (m) (and (listp m)
669 (symbolp (car m))
670 (face-equal (car m) face)))
671 (cdr (symbol-function menu))))
672 (t ; No keyboard equivalent. Figure out where to put it:
673 (setq key (vector face)
674 function 'facemenu-set-face-from-menu
675 menu-val (symbol-function menu))
676 (if (and facemenu-new-faces-at-end
677 (> (length menu-val) 3))
678 (define-key-after menu-val key (cons name function)
679 (car (nth (- (length menu-val) 3) menu-val)))
680 (define-key menu key (cons name function))))))
681 nil) ; Return nil for facemenu-iterate
683 (defun facemenu-complete-face-list (&optional oldlist)
684 "Return list of all faces that look different.
685 Starts with given ALIST of faces, and adds elements only if they display
686 differently from any face already on the list.
687 The faces on ALIST will end up at the end of the returned list, in reverse
688 order."
689 (let ((list (nreverse (mapcar 'car oldlist))))
690 (facemenu-iterate
691 (lambda (new-face)
692 (if (not (memq new-face list))
693 (setq list (cons new-face list)))
694 nil)
695 (nreverse (face-list)))
696 list))
698 (defun facemenu-iterate (func iterate-list)
699 "Apply FUNC to each element of LIST until one returns non-nil.
700 Returns the non-nil value it found, or nil if all were nil."
701 (while (and iterate-list (not (funcall func (car iterate-list))))
702 (setq iterate-list (cdr iterate-list)))
703 (car iterate-list))
705 (facemenu-update)
707 ;;; facemenu.el ends here