(replace_buffer_in_all_windows):
[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 "^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.
146
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.
151
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)
159
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)
168
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)
177
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)
186
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)
198
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)
211
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)
227
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 All" 'facemenu-remove-all))
240 (define-key map [rm] (cons "Remove Properties" 'facemenu-remove-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)
253
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)
262
263
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)
271
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)
280
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)
289
290 ;;; Internal Variables
291
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.")
296
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)
302
303 ;; Add each defined face to the menu.
304 (facemenu-iterate 'facemenu-add-new-face
305 (facemenu-complete-face-list facemenu-keybindings)))
306
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.
312
313 Interactively, the face to be used is read with the minibuffer.
314
315 If the region is active and there is no prefix argument,
316 this command sets the region to the requested face.
317
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)))
329
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)))
344
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)))
359
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.
365
366 If the region is active and there is no prefix argument,
367 this command sets the region to the requested face.
368
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)))
382
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)))
390
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)))
398
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)))
406
407 ;;;###autoload
408 (defun facemenu-remove-props (start end)
409 "Remove all text properties that facemenu added to region."
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 invisible nil intangible nil
414 read-only nil category nil))))
415
416 ;;;###autoload
417 (defun facemenu-remove-all (start end)
418 "Remove all text properties from the region."
419 (interactive "*r") ; error if buffer is read-only despite the next line.
420 (let ((inhibit-read-only t))
421 (set-text-properties start end nil)))
422
423 ;;;###autoload
424 (defun facemenu-remove-special (start end)
425 "Remove all the \"special\" text properties from the region.
426 These special properties include `invisible', `intangible' and `read-only'."
427 (interactive "*r") ; error if buffer is read-only despite the next line.
428 (let ((inhibit-read-only t))
429 (remove-text-properties
430 start end '(invisible nil intangible nil read-only nil))))
431
432 ;;;###autoload
433 (defun list-text-properties-at (p)
434 "Pop up a buffer listing text-properties at LOCATION."
435 (interactive "d")
436 (let ((props (text-properties-at p))
437 category
438 str)
439 (if (null props)
440 (message "None")
441 (if (and (not (cdr (cdr props)))
442 (not (eq (car props) 'category))
443 (< (length (setq str (format "Text property at %d: %s %S"
444 p (car props) (car (cdr props)))))
445 (frame-width)))
446 (message "%s" str)
447 (with-output-to-temp-buffer "*Text Properties*"
448 (princ (format "Text properties at %d:\n\n" p))
449 (while props
450 (if (eq (car props) 'category)
451 (setq category (car (cdr props))))
452 (princ (format "%-20s %S\n"
453 (car props) (car (cdr props))))
454 (setq props (cdr (cdr props))))
455 (if category
456 (progn
457 (setq props (symbol-plist category))
458 (princ (format "\nCategory %s:\n\n" category))
459 (while props
460 (princ (format "%-20s %S\n"
461 (car props) (car (cdr props))))
462 (if (eq (car props) 'category)
463 (setq category (car (cdr props))))
464 (setq props (cdr (cdr props)))))))))))
465
466 ;;;###autoload
467 (defun facemenu-read-color (&optional prompt)
468 "Read a color using the minibuffer."
469 (let ((col (completing-read (or prompt "Color: ")
470 (or facemenu-color-alist
471 (if window-system
472 (mapcar 'list (x-defined-colors))))
473 nil t)))
474 (if (equal "" col)
475 nil
476 col)))
477
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 (if (and (null list) window-system)
486 (progn
487 (setq list (x-defined-colors))
488 ;; Delete duplicate colors.
489 (let ((l list))
490 (while (cdr l)
491 (if (facemenu-color-equal (car l) (car (cdr l)))
492 (setcdr l (cdr (cdr l)))
493 (setq l (cdr l)))))))
494 (with-output-to-temp-buffer "*Colors*"
495 (save-excursion
496 (set-buffer standard-output)
497 (let (s)
498 (while list
499 (setq s (point))
500 (insert (car list))
501 (indent-to 20)
502 (put-text-property s (point) 'face
503 (cons 'background-color (car list)))
504 (setq s (point))
505 (insert " " (car list) "\n")
506 (put-text-property s (point) 'face
507 (cons 'foreground-color (car list)))
508 (setq list (cdr list)))))))
509
510 (defun facemenu-color-equal (a b)
511 "Return t if colors A and B are the same color.
512 A and B should be strings naming colors.
513 This function queries the window-system server to find out what the
514 color names mean. It returns nil if the colors differ or if it can't
515 determine the correct answer."
516 (cond ((equal a b) t)
517 ((and (memq window-system '(x w32))
518 (equal (x-color-values a) (x-color-values b))))
519 ((eq window-system 'pc)
520 (and (x-color-defined-p a) (x-color-defined-p b)
521 (eq (msdos-color-translate a) (msdos-color-translate b))))))
522
523 (defun facemenu-add-face (face &optional start end)
524 "Add FACE to text between START and END.
525 If START is `nil' or START to END is empty, add FACE to next typed character
526 instead. For each section of that region that has a different face property,
527 FACE will be consed onto it, and other faces that are completely hidden by
528 that will be removed from the list.
529 If `facemenu-add-face-function' and maybe `facemenu-end-add-face' are non-`nil'
530 they are used to set the face information.
531
532 As a special case, if FACE is `default', then the region is left with NO face
533 text property. Otherwise, selecting the default face would not have any
534 effect. See `facemenu-remove-face-function'."
535 (interactive "*xFace: \nr")
536 (if (and (eq face 'default)
537 (not (eq facemenu-remove-face-function t)))
538 (if facemenu-remove-face-function
539 (funcall facemenu-remove-face-function start end)
540 (if (and start (< start end))
541 (remove-text-properties start end '(face default))
542 (setq self-insert-face 'default
543 self-insert-face-command this-command)))
544 (if facemenu-add-face-function
545 (save-excursion
546 (if end (goto-char end))
547 (save-excursion
548 (if start (goto-char start))
549 (insert-before-markers
550 (funcall facemenu-add-face-function face end)))
551 (if facemenu-end-add-face
552 (insert (if (stringp facemenu-end-add-face)
553 facemenu-end-add-face
554 (funcall facemenu-end-add-face face)))))
555 (if (and start (< start end))
556 (let ((part-start start) part-end)
557 (while (not (= part-start end))
558 (setq part-end (next-single-property-change part-start 'face
559 nil end))
560 (let ((prev (get-text-property part-start 'face)))
561 (put-text-property part-start part-end 'face
562 (if (null prev)
563 face
564 (facemenu-active-faces
565 (cons face
566 (if (listp prev)
567 prev
568 (list prev)))))))
569 (setq part-start part-end)))
570 (setq self-insert-face (if (eq last-command self-insert-face-command)
571 (cons face (if (listp self-insert-face)
572 self-insert-face
573 (list self-insert-face)))
574 face)
575 self-insert-face-command this-command)))))
576
577 (defun facemenu-active-faces (face-list &optional frame)
578 "Return from FACE-LIST those faces that would be used for display.
579 This means each face attribute is not specified in a face earlier in FACE-LIST
580 and such a face is therefore active when used to display text.
581 If the optional argument FRAME is given, use the faces in that frame; otherwise
582 use the selected frame. If t, then the global, non-frame faces are used."
583 (let* ((mask-atts (copy-sequence (internal-get-face (car face-list) frame)))
584 (active-list (list (car face-list)))
585 (face-list (cdr face-list))
586 (mask-len (length mask-atts)))
587 (while face-list
588 (if (let ((face-atts (internal-get-face (car face-list) frame))
589 (i mask-len) (useful nil))
590 (while (> (setq i (1- i)) 1)
591 (and (aref face-atts i) (not (aref mask-atts i))
592 (aset mask-atts i (setq useful t))))
593 useful)
594 (setq active-list (cons (car face-list) active-list)))
595 (setq face-list (cdr face-list)))
596 (nreverse active-list)))
597
598 (defun facemenu-get-face (symbol)
599 "Make sure FACE exists.
600 If not, create it and add it to the appropriate menu. Return the symbol.
601
602 If a window system is in use, and this function creates a face named
603 `fg:color', then it sets the foreground to that color. Likewise, `bg:color'
604 means to set the background. In either case, if the color is undefined,
605 no color is set and a warning is issued."
606 (let ((name (symbol-name symbol))
607 foreground)
608 (cond ((internal-find-face symbol))
609 ((and window-system
610 (or (setq foreground (string-match "^fg:" name))
611 (string-match "^bg:" name)))
612 (let ((face (make-face symbol))
613 (color (substring name 3)))
614 (if (x-color-defined-p color)
615 (if foreground
616 (set-face-foreground face color)
617 (set-face-background face color))
618 (message "Color \"%s\" undefined" color))))
619 (t (make-face symbol))))
620 symbol)
621
622 (defun facemenu-add-new-face (face)
623 "Add a FACE to the appropriate Face menu.
624 Automatically called when a new face is created."
625 (let* ((name (symbol-name face))
626 menu docstring
627 (key (cdr (assoc face facemenu-keybindings)))
628 function menu-val)
629 (cond ((string-match "^fg:" name)
630 (setq name (substring name 3))
631 (setq docstring
632 (format "Select foreground color %s for subsequent insertion."
633 name))
634 (setq menu 'facemenu-foreground-menu))
635 ((string-match "^bg:" name)
636 (setq name (substring name 3))
637 (setq docstring
638 (format "Select background color %s for subsequent insertion."
639 name))
640 (setq menu 'facemenu-background-menu))
641 (t
642 (setq docstring
643 (format "Select face `%s' for subsequent insertion."
644 name))
645 (setq menu 'facemenu-face-menu)))
646 (cond ((eq t facemenu-unlisted-faces))
647 ((memq face facemenu-unlisted-faces))
648 ;; test against regexps in facemenu-unlisted-faces
649 ((let ((unlisted facemenu-unlisted-faces)
650 (matched nil))
651 (while (and unlisted (not matched))
652 (if (and (stringp (car unlisted))
653 (string-match (car unlisted) name))
654 (setq matched t)
655 (setq unlisted (cdr unlisted))))
656 matched))
657 (key ; has a keyboard equivalent. These go at the front.
658 (setq function (intern (concat "facemenu-set-" name)))
659 (fset function
660 `(lambda ()
661 ,docstring
662 (interactive)
663 (facemenu-set-face (quote ,face))))
664 (define-key 'facemenu-keymap key (cons name function))
665 (define-key menu key (cons name function)))
666 ((facemenu-iterate ; check if equivalent face is already in the menu
667 (lambda (m) (and (listp m)
668 (symbolp (car m))
669 (face-equal (car m) face)))
670 (cdr (symbol-function menu))))
671 (t ; No keyboard equivalent. Figure out where to put it:
672 (setq key (vector face)
673 function 'facemenu-set-face-from-menu
674 menu-val (symbol-function menu))
675 (if (and facemenu-new-faces-at-end
676 (> (length menu-val) 3))
677 (define-key-after menu-val key (cons name function)
678 (car (nth (- (length menu-val) 3) menu-val)))
679 (define-key menu key (cons name function))))))
680 nil) ; Return nil for facemenu-iterate
681
682 (defun facemenu-complete-face-list (&optional oldlist)
683 "Return list of all faces that look different.
684 Starts with given ALIST of faces, and adds elements only if they display
685 differently from any face already on the list.
686 The faces on ALIST will end up at the end of the returned list, in reverse
687 order."
688 (let ((list (nreverse (mapcar 'car oldlist))))
689 (facemenu-iterate
690 (lambda (new-face)
691 (if (not (memq new-face list))
692 (setq list (cons new-face list)))
693 nil)
694 (nreverse (face-list)))
695 list))
696
697 (defun facemenu-iterate (func iterate-list)
698 "Apply FUNC to each element of LIST until one returns non-nil.
699 Returns the non-nil value it found, or nil if all were nil."
700 (while (and iterate-list (not (funcall func (car iterate-list))))
701 (setq iterate-list (cdr iterate-list)))
702 (car iterate-list))
703
704 (facemenu-update)
705
706 ;;; facemenu.el ends here