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