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