1 ;;; facemenu.el --- create a face menu for interactively adding fonts to text
3 ;; Copyright (c) 1994, 1995, 1996, 2001, 2002, 2005 Free Software Foundation, Inc.
5 ;; Author: Boris Goldowsky <boris@gnu.org>
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)
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.
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.
31 ;; The menu also contains submenus for indentation and justification-changing
35 ;; Selecting a face from the menu or typing the keyboard equivalent will
36 ;; change the region to use that face. If you use transient-mark-mode and the
37 ;; region is not active, the face will be remembered and used for the next
38 ;; insertion. It will be forgotten if you move point or make other
39 ;; modifications before inserting or typing anything.
41 ;; Faces can be selected from the keyboard as well.
42 ;; The standard keybindings are M-o (or ESC o) + letter:
43 ;; M-o i = "set italic", M-o b = "set bold", etc.
46 ;; An alternative set of keybindings that may be easier to type can be set up
47 ;; using "Alt" or "Hyper" keys. This requires that you either have or create
48 ;; an Alt or Hyper key on your keyboard. On my keyboard, there is a key
49 ;; labeled "Alt", but to make it act as an Alt key I have to put this command
51 ;; xmodmap -e "add Mod3 = Alt_L"
52 ;; Or, I can make it into a Hyper key with this:
53 ;; xmodmap -e "keysym Alt_L = Hyper_L" -e "add Mod2 = Hyper_L"
54 ;; Check with local X-perts for how to do it on your system.
55 ;; Then you can define your keybindings with code like this in your .emacs:
56 ;; (setq facemenu-keybindings
57 ;; '((default . [?\H-d])
60 ;; (bold-italic . [?\H-l])
61 ;; (underline . [?\H-u])))
63 ;; (setq facemenu-keymap global-map)
64 ;; (define-key global-map [?\H-c] 'facemenu-set-foreground) ; set fg color
65 ;; (define-key global-map [?\H-C] 'facemenu-set-background) ; set bg color
67 ;; The order of the faces that appear in the menu and their keybindings can be
68 ;; controlled by setting the variables `facemenu-keybindings' and
69 ;; `facemenu-new-faces-at-end'. List faces that you don't use in documents
70 ;; (eg, `region') in `facemenu-unlisted-faces'.
73 ;; Bold and Italic do not combine to create bold-italic if you select them
74 ;; both, although most other combinations (eg bold + underline + some color)
75 ;; do the intuitive thing.
77 ;; There is at present no way to display what the faces look like in
80 ;; `list-faces-display' shows the faces in a different order than
81 ;; this menu, which could be confusing. I do /not/ sort the list
82 ;; alphabetically, because I like the default order: it puts the most
83 ;; basic, common fonts first.
85 ;; Please send me any other problems, comments or ideas.
93 ;;; Provide some binding for startup:
94 ;;;###autoload (define-key global-map "\M-o" 'facemenu-keymap)
95 ;;;###autoload (autoload 'facemenu-keymap "facemenu" "Keymap for face-changing commands." t 'keymap)
98 (define-key global-map
[C-down-mouse-2
] 'facemenu-menu
)
99 (define-key global-map
"\M-o" 'facemenu-keymap
)
101 (defgroup facemenu nil
102 "Create a face menu for interactively adding fonts to text."
106 (defcustom facemenu-keybindings
110 (bold-italic .
"l") ; {bold} intersect {italic} = {l}
112 "Alist of interesting faces and keybindings.
113 Each element is itself a list: the car is the name of the face,
114 the next element is the key to use as a keyboard equivalent of the menu item;
115 the binding is made in `facemenu-keymap'.
117 The faces specifically mentioned in this list are put at the top of
118 the menu, in the order specified. All other faces which are defined,
119 except for those in `facemenu-unlisted-faces', are listed after them,
120 but get no keyboard equivalents.
122 If you change this variable after loading facemenu.el, you will need to call
123 `facemenu-update' to make it take effect."
124 :type
'(repeat (cons face string
))
127 (defcustom facemenu-new-faces-at-end t
128 "*Where in the menu to insert newly-created faces.
129 This should be nil to put them at the top of the menu, or t to put them
130 just before \"Other\" at the end."
134 (defcustom facemenu-unlisted-faces
135 `(modeline region secondary-selection highlight scratch-face
136 ,(purecopy "^font-lock-") ,(purecopy "^gnus-") ,(purecopy "^message-")
137 ,(purecopy "^ediff-") ,(purecopy "^term-") ,(purecopy "^vc-")
138 ,(purecopy "^widget-") ,(purecopy "^custom-") ,(purecopy "^vm-"))
139 "*List of faces not to include in the Face menu.
140 Each element may be either a symbol, which is the name of a face, or a string,
141 which is a regular expression to be matched against face names. Matching
142 faces will not be added to the menu.
144 You can set this list before loading facemenu.el, or add a face to it before
145 creating that face if you do not want it to be listed. If you change the
146 variable so as to eliminate faces that have already been added to the menu,
147 call `facemenu-update' to recalculate the menu contents.
149 If this variable is t, no faces will be added to the menu. This is useful for
150 temporarily turning off the feature that automatically adds faces to the menu
151 when they are created."
152 :type
'(choice (const :tag
"Don't add faces" t
)
153 (const :tag
"None (do add any face)" nil
)
154 (repeat (choice symbol regexp
)))
158 (defvar facemenu-face-menu
159 (let ((map (make-sparse-keymap "Face")))
160 (define-key map
"o" (cons "Other..." 'facemenu-set-face
))
162 "Menu keymap for faces.")
164 (defalias 'facemenu-face-menu facemenu-face-menu
)
165 (put 'facemenu-face-menu
'menu-enable
'(facemenu-enable-faces-p))
168 (defvar facemenu-foreground-menu
169 (let ((map (make-sparse-keymap "Foreground Color")))
170 (define-key map
"o" (cons "Other..." 'facemenu-set-foreground
))
172 "Menu keymap for foreground colors.")
174 (defalias 'facemenu-foreground-menu facemenu-foreground-menu
)
175 (put 'facemenu-foreground-menu
'menu-enable
'(facemenu-enable-faces-p))
178 (defvar facemenu-background-menu
179 (let ((map (make-sparse-keymap "Background Color")))
180 (define-key map
"o" (cons "Other..." 'facemenu-set-background
))
182 "Menu keymap for background colors.")
184 (defalias 'facemenu-background-menu facemenu-background-menu
)
185 (put 'facemenu-background-menu
'menu-enable
'(facemenu-enable-faces-p))
187 ;;; Condition for enabling menu items that set faces.
188 (defun facemenu-enable-faces-p ()
189 (not (and font-lock-mode font-lock-defaults
)))
192 (defvar facemenu-special-menu
193 (let ((map (make-sparse-keymap "Special")))
194 (define-key map
[?s
] (cons (purecopy "Remove Special")
195 'facemenu-remove-special
))
196 (define-key map
[?t
] (cons (purecopy "Intangible")
197 'facemenu-set-intangible
))
198 (define-key map
[?v
] (cons (purecopy "Invisible")
199 'facemenu-set-invisible
))
200 (define-key map
[?r
] (cons (purecopy "Read-Only")
201 'facemenu-set-read-only
))
203 "Menu keymap for non-face text-properties.")
205 (defalias 'facemenu-special-menu facemenu-special-menu
)
208 (defvar facemenu-justification-menu
209 (let ((map (make-sparse-keymap "Justification")))
210 (define-key map
[?c
] (cons (purecopy "Center") 'set-justification-center
))
211 (define-key map
[?b
] (cons (purecopy "Full") 'set-justification-full
))
212 (define-key map
[?r
] (cons (purecopy "Right") 'set-justification-right
))
213 (define-key map
[?l
] (cons (purecopy "Left") 'set-justification-left
))
214 (define-key map
[?u
] (cons (purecopy "Unfilled") 'set-justification-none
))
216 "Submenu for text justification commands.")
218 (defalias 'facemenu-justification-menu facemenu-justification-menu
)
221 (defvar facemenu-indentation-menu
222 (let ((map (make-sparse-keymap "Indentation")))
223 (define-key map
[decrease-right-margin
]
224 (cons (purecopy "Indent Right Less") 'decrease-right-margin
))
225 (define-key map
[increase-right-margin
]
226 (cons (purecopy "Indent Right More") 'increase-right-margin
))
227 (define-key map
[decrease-left-margin
]
228 (cons (purecopy "Indent Less") 'decrease-left-margin
))
229 (define-key map
[increase-left-margin
]
230 (cons (purecopy "Indent More") 'increase-left-margin
))
232 "Submenu for indentation commands.")
234 (defalias 'facemenu-indentation-menu facemenu-indentation-menu
)
236 ;; This is split up to avoid an overlong line in loaddefs.el.
238 (defvar facemenu-menu nil
239 "Facemenu top-level menu keymap.")
241 (setq facemenu-menu
(make-sparse-keymap "Text Properties"))
243 (let ((map facemenu-menu
))
244 (define-key map
[dc] (cons (purecopy "Display Colors") 'list-colors-display))
245 (define-key map [df] (cons (purecopy "Display Faces") 'list-faces-display))
246 (define-key map [dp] (cons (purecopy "Describe Properties")
247 'describe-text-properties))
248 (define-key map [ra] (cons (purecopy "Remove Text Properties")
249 'facemenu-remove-all))
250 (define-key map [rm] (cons (purecopy "Remove Face Properties")
251 'facemenu-remove-face-props))
252 (define-key map [s1] (list (purecopy "--"))))
254 (let ((map facemenu-menu))
255 (define-key map [in] (cons (purecopy "Indentation")
256 'facemenu-indentation-menu))
257 (define-key map [ju] (cons (purecopy "Justification")
258 'facemenu-justification-menu))
259 (define-key map [s2] (list (purecopy "--")))
260 (define-key map [sp] (cons (purecopy "Special Properties")
261 'facemenu-special-menu))
262 (define-key map [bg] (cons (purecopy "Background Color")
263 'facemenu-background-menu))
264 (define-key map [fg] (cons (purecopy "Foreground Color")
265 'facemenu-foreground-menu))
266 (define-key map [fc] (cons (purecopy "Face")
267 'facemenu-face-menu)))
269 (defalias 'facemenu-menu facemenu-menu)
271 (defvar facemenu-keymap
272 (let ((map (make-sparse-keymap "Set face")))
273 (define-key map "o" (cons (purecopy "Other...") 'facemenu-set-face))
275 "Keymap for face-changing commands.
276 `Facemenu-update' fills in the keymap according to the bindings
277 requested in `facemenu-keybindings'.")
278 (defalias 'facemenu-keymap facemenu-keymap)
281 (defcustom facemenu-add-face-function nil
282 "Function called at beginning of text to change or nil.
283 This function is passed the FACE to set and END of text to change, and must
284 return a string which is inserted. It may set `facemenu-end-add-face'."
285 :type '(choice (const :tag "None" nil)
289 (defcustom facemenu-end-add-face nil
290 "String to insert or function called at end of text to change or nil.
291 This function is passed the FACE to set, and must return a string which is
293 :type '(choice (const :tag "None" nil)
298 (defcustom facemenu-remove-face-function nil
299 "When non-nil, this is a function called to remove faces.
300 This function is passed the START and END of text to change.
301 May also be t meaning to use `facemenu-add-face-function'."
302 :type '(choice (const :tag "None" nil)
303 (const :tag "Use add-face" t)
307 ;;; Internal Variables
309 (defvar facemenu-color-alist nil
310 ;; Don't initialize here; that doesn't work if preloaded.
311 "Alist of colors, used for completion.
312 If null, `facemenu-read-color' will set it.")
314 (defun facemenu-update ()
315 "Add or update the \"Face\" menu in the menu bar.
316 You can call this to update things if you change any of the menu configuration
320 ;; Add each defined face to the menu.
321 (facemenu-iterate 'facemenu-add-new-face
322 (facemenu-complete-face-list facemenu-keybindings)))
325 (defun facemenu-set-face (face &optional start end)
326 "Add FACE to the region or next character typed.
327 This adds FACE to the top of the face list; any faces lower on the list that
328 will not show through at all will be removed.
330 Interactively, reads the face name with the minibuffer.
332 If the region is active (normally true except in Transient Mark mode)
333 and there is no prefix argument, this command sets the region to the
336 Otherwise, this command specifies the face for the next character
337 inserted. Moving point or switching buffers before
338 typing a character to insert cancels the specification."
339 (interactive (list (progn
340 (barf-if-buffer-read-only)
341 (read-face-name "Use face"))
342 (if (and mark-active (not current-prefix-arg))
344 (if (and mark-active (not current-prefix-arg))
346 (facemenu-add-new-face face)
347 (facemenu-add-face face start end))
350 (defun facemenu-set-foreground (color &optional start end)
351 "Set the foreground COLOR of the region or next character typed.
352 This command reads the color in the minibuffer.
354 If the region is active (normally true except in Transient Mark mode)
355 and there is no prefix argument, this command sets the region to the
358 Otherwise, this command specifies the face for the next character
359 inserted. Moving point or switching buffers before
360 typing a character to insert cancels the specification."
361 (interactive (list (progn
362 (barf-if-buffer-read-only)
363 (facemenu-read-color "Foreground color: "))
364 (if (and mark-active (not current-prefix-arg))
366 (if (and mark-active (not current-prefix-arg))
368 (facemenu-set-face-from-menu
369 (facemenu-add-new-color color 'facemenu-foreground-menu)
373 (defun facemenu-set-background (color &optional start end)
374 "Set the background COLOR of the region or next character typed.
375 This command reads the color in the minibuffer.
377 If the region is active (normally true except in Transient Mark mode)
378 and there is no prefix argument, this command sets the region to the
381 Otherwise, this command specifies the face for the next character
382 inserted. Moving point or switching buffers before
383 typing a character to insert cancels the specification."
384 (interactive (list (progn
385 (barf-if-buffer-read-only)
386 (facemenu-read-color "Background color: "))
387 (if (and mark-active (not current-prefix-arg))
389 (if (and mark-active (not current-prefix-arg))
391 (facemenu-set-face-from-menu
392 (facemenu-add-new-color color 'facemenu-background-menu)
396 (defun facemenu-set-face-from-menu (face start end)
397 "Set the FACE of the region or next character typed.
398 This function is designed to be called from a menu; FACE is determined
399 using the event type of the menu entry. If FACE is a symbol whose
400 name starts with \"fg:\" or \"bg:\", then this functions sets the
401 foreground or background to the color specified by the rest of the
402 symbol's name. Any other symbol is considered the name of a face.
404 If the region is active (normally true except in Transient Mark mode)
405 and there is no prefix argument, this command sets the region to the
408 Otherwise, this command specifies the face for the next character
409 inserted. Moving point or switching buffers before typing a character
410 to insert cancels the specification."
411 (interactive (list last-command-event
412 (if (and mark-active (not current-prefix-arg))
414 (if (and mark-active (not current-prefix-arg))
416 (barf-if-buffer-read-only)
418 (let ((fn (symbol-name face)))
419 (if (string-match "\\`\\([fb]\\)g:\\(.+\\)" fn)
420 (list (list (if (string= (match-string 1 fn) "f")
423 (match-string 2 fn)))
428 (defun facemenu-set-invisible (start end)
429 "Make the region invisible.
430 This sets the `invisible' text property; it can be undone with
431 `facemenu-remove-special'."
433 (add-text-properties start end '(invisible t)))
436 (defun facemenu-set-intangible (start end)
437 "Make the region intangible: disallow moving into it.
438 This sets the `intangible' text property; it can be undone with
439 `facemenu-remove-special'."
441 (add-text-properties start end '(intangible t)))
444 (defun facemenu-set-read-only (start end)
445 "Make the region unmodifiable.
446 This sets the `read-only' text property; it can be undone with
447 `facemenu-remove-special'."
449 (add-text-properties start end '(read-only t)))
452 (defun facemenu-remove-face-props (start end)
453 "Remove `face' and `mouse-face' text properties."
454 (interactive "*r") ; error if buffer is read-only despite the next line.
455 (let ((inhibit-read-only t))
456 (remove-text-properties
457 start end '(face nil mouse-face nil))))
460 (defun facemenu-remove-all (start end)
461 "Remove all text properties from the region."
462 (interactive "*r") ; error if buffer is read-only despite the next line.
463 (let ((inhibit-read-only t))
464 (set-text-properties start end nil)))
467 (defun facemenu-remove-special (start end)
468 "Remove all the \"special\" text properties from the region.
469 These special properties include `invisible', `intangible' and `read-only'."
470 (interactive "*r") ; error if buffer is read-only despite the next line.
471 (let ((inhibit-read-only t))
472 (remove-text-properties
473 start end '(invisible nil intangible nil read-only nil))))
476 (defun facemenu-read-color (&optional prompt)
477 "Read a color using the minibuffer."
478 (let ((col (completing-read (or prompt "Color: ")
479 (or facemenu-color-alist
487 (defun list-colors-display (&optional list buffer-name)
488 "Display names of defined colors, and show what they look like.
489 If the optional argument LIST is non-nil, it should be a list of
490 colors to display. Otherwise, this command computes a list of
491 colors that the current display can handle. If the optional
492 argument BUFFER-NAME is nil, it defaults to *Colors*."
494 (when (and (null list) (> (display-color-cells) 0))
495 (setq list (list-colors-duplicates (defined-colors)))
496 (when (memq (display-visual-class) '(gray-scale pseudo-color direct-color))
497 ;; Don't show more than what the display can handle.
498 (let ((lc (nthcdr (1- (display-color-cells)) list)))
501 (with-output-to-temp-buffer (or buffer-name "*Colors*")
503 (set-buffer standard-output)
504 (setq truncate-lines t)
505 (if temp-buffer-show-function
506 (list-colors-print list)
507 ;; Call list-colors-print from temp-buffer-show-hook
508 ;; to get the right value of window-width in list-colors-print
509 ;; after the buffer is displayed.
510 (add-hook 'temp-buffer-show-hook
511 (lambda () (list-colors-print list)) nil t)))))
513 (defun list-colors-print (list)
517 (setq color (sort color (lambda (a b)
518 (string< (downcase a)
520 (setq color (list color)))
526 'face (cons 'background-color (car color)))
529 (insert " " (if (cdr color)
530 (mapconcat 'identity (cdr color) ", ")
533 'face (cons 'foreground-color (car color)))
534 (indent-to (max (- (window-width) 8) 44))
535 (insert (apply 'format "#%02x%02x%02x"
536 (mapcar (lambda (c) (lsh c -8))
537 (color-values (car color)))))
540 (goto-char (point-min)))
542 (defun list-colors-duplicates (&optional list)
543 "Return a list of colors with grouped duplicate colors.
544 If a color has no duplicates, then the element of the returned list
545 has the form '(COLOR-NAME). The element of the returned list with
546 duplicate colors has the form '(COLOR-NAME DUPLICATE-COLOR-NAME ...).
547 This function uses the predicate `facemenu-color-equal' to compare
548 color names. If the optional argument LIST is non-nil, it should
549 be a list of colors to display. Otherwise, this function uses
550 a list of colors that the current display can handle."
551 (let* ((list (mapcar 'list (or list (defined-colors))))
554 (if (and (facemenu-color-equal (car (car l)) (car (car (cdr l))))
555 (not (if (boundp 'w32-default-color-map)
556 (not (assoc (car (car l)) w32-default-color-map)))))
558 (setcdr (car l) (cons (car (car (cdr l))) (cdr (car l))))
559 (setcdr l (cdr (cdr l))))
563 (defun facemenu-color-equal (a b)
564 "Return t if colors A and B are the same color.
565 A and B should be strings naming colors.
566 This function queries the display system to find out what the color
567 names mean. It returns nil if the colors differ or if it can't
568 determine the correct answer."
569 (cond ((equal a b) t)
570 ((equal (color-values a) (color-values b)))))
572 (defun facemenu-add-face (face &optional start end)
573 "Add FACE to text between START and END.
574 If START is nil or START to END is empty, add FACE to next typed character
575 instead. For each section of that region that has a different face property,
576 FACE will be consed onto it, and other faces that are completely hidden by
577 that will be removed from the list.
578 If `facemenu-add-face-function' and maybe `facemenu-end-add-face' are non-nil,
579 they are used to set the face information.
581 As a special case, if FACE is `default', then the region is left with NO face
582 text property. Otherwise, selecting the default face would not have any
583 effect. See `facemenu-remove-face-function'."
584 (interactive "*xFace: \nr")
585 (if (and (eq face 'default)
586 (not (eq facemenu-remove-face-function t)))
587 (if facemenu-remove-face-function
588 (funcall facemenu-remove-face-function start end)
589 (if (and start (< start end))
590 (remove-text-properties start end '(face default))
591 (setq self-insert-face 'default
592 self-insert-face-command this-command)))
593 (if facemenu-add-face-function
595 (if end (goto-char end))
597 (if start (goto-char start))
598 (insert-before-markers
599 (funcall facemenu-add-face-function face end)))
600 (if facemenu-end-add-face
601 (insert (if (stringp facemenu-end-add-face)
602 facemenu-end-add-face
603 (funcall facemenu-end-add-face face)))))
604 (if (and start (< start end))
605 (let ((part-start start) part-end)
606 (while (not (= part-start end))
607 (setq part-end (next-single-property-change part-start 'face
609 (let ((prev (get-text-property part-start 'face)))
610 (put-text-property part-start part-end 'face
613 (facemenu-active-faces
618 (setq part-start part-end)))
619 (setq self-insert-face (if (eq last-command self-insert-face-command)
620 (cons face (if (listp self-insert-face)
622 (list self-insert-face)))
624 self-insert-face-command this-command))))
625 (unless (facemenu-enable-faces-p)
626 (message "Font-lock mode will override any faces you set in this buffer")))
628 (defun facemenu-active-faces (face-list &optional frame)
629 "Return from FACE-LIST those faces that would be used for display.
630 This means each face attribute is not specified in a face earlier in FACE-LIST
631 and such a face is therefore active when used to display text.
632 If the optional argument FRAME is given, use the faces in that frame; otherwise
633 use the selected frame. If t, then the global, non-frame faces are used."
634 (let* ((mask-atts (copy-sequence
635 (if (consp (car face-list))
636 (face-attributes-as-vector (car face-list))
637 (or (internal-lisp-face-p (car face-list) frame)
638 (check-face (car face-list))))))
639 (active-list (list (car face-list)))
640 (face-list (cdr face-list))
641 (mask-len (length mask-atts)))
644 (if (consp (car face-list))
645 (face-attributes-as-vector (car face-list))
646 (or (internal-lisp-face-p (car face-list) frame)
647 (check-face (car face-list)))))
650 (while (>= (setq i (1- i)) 0)
651 (and (not (memq (aref face-atts i) '(nil unspecified)))
652 (memq (aref mask-atts i) '(nil unspecified))
653 (aset mask-atts i (setq useful t))))
655 (setq active-list (cons (car face-list) active-list)))
656 (setq face-list (cdr face-list)))
657 (nreverse active-list)))
659 (defun facemenu-add-new-face (face)
660 "Add FACE (a face) to the Face menu.
662 This is called whenever you create a new face."
666 (key (cdr (assoc face facemenu-keybindings)))
669 (setq name (symbol-name face)
672 symbol (intern name)))
673 (setq menu 'facemenu-face-menu)
675 (format "Select face `%s' for subsequent insertion."
677 (cond ((eq t facemenu-unlisted-faces))
678 ((memq symbol facemenu-unlisted-faces))
679 ;; test against regexps in facemenu-unlisted-faces
680 ((let ((unlisted facemenu-unlisted-faces)
682 (while (and unlisted (not matched))
683 (if (and (stringp (car unlisted))
684 (string-match (car unlisted) name))
686 (setq unlisted (cdr unlisted))))
688 (key ; has a keyboard equivalent. These go at the front.
689 (setq function (intern (concat "facemenu-set-" name)))
696 (if (and mark-active (not current-prefix-arg))
698 (if (and mark-active (not current-prefix-arg))
700 (define-key 'facemenu-keymap key (cons name function))
701 (define-key menu key (cons name function)))
702 ((facemenu-iterate ; check if equivalent face is already in the menu
703 (lambda (m) (and (listp m)
705 (face-equal (car m) symbol)))
706 (cdr (symbol-function menu))))
707 (t ; No keyboard equivalent. Figure out where to put it:
708 (setq key (vector symbol)
709 function 'facemenu-set-face-from-menu
710 menu-val (symbol-function menu))
711 (if (and facemenu-new-faces-at-end
712 (> (length menu-val) 3))
713 (define-key-after menu-val key (cons name function)
714 (car (nth (- (length menu-val) 3) menu-val)))
715 (define-key menu key (cons name function))))))
716 nil) ; Return nil for facemenu-iterate
718 (defun facemenu-add-new-color (color menu)
719 "Add COLOR (a color name string) to the appropriate Face menu.
720 MENU should be `facemenu-foreground-menu' or `facemenu-background-menu'.
721 Return the event type (a symbol) of the added menu entry.
723 This is called whenever you use a new color."
724 (let (symbol docstring)
725 (unless (color-defined-p color)
726 (error "Color `%s' undefined" color))
727 (cond ((eq menu 'facemenu-foreground-menu)
729 (format "Select foreground color %s for subsequent insertion."
731 symbol (intern (concat "fg:" color))))
732 ((eq menu 'facemenu-background-menu)
734 (format "Select background color %s for subsequent insertion."
736 symbol (intern (concat "bg:" color))))
737 (t (error "MENU should be `facemenu-foreground-menu' or `facemenu-background-menu'")))
738 (unless (facemenu-iterate ; Check if color is already in the menu.
739 (lambda (m) (and (listp m)
740 (eq (car m) symbol)))
741 (cdr (symbol-function menu)))
742 ;; Color is not in the menu. Figure out where to put it.
743 (let ((key (vector symbol))
744 (function 'facemenu-set-face-from-menu)
745 (menu-val (symbol-function menu)))
746 (if (and facemenu-new-faces-at-end
747 (> (length menu-val) 3))
748 (define-key-after menu-val key (cons color function)
749 (car (nth (- (length menu-val) 3) menu-val)))
750 (define-key menu key (cons color function)))))
753 (defun facemenu-complete-face-list (&optional oldlist)
754 "Return list of all faces that look different.
755 Starts with given ALIST of faces, and adds elements only if they display
756 differently from any face already on the list.
757 The faces on ALIST will end up at the end of the returned list, in reverse
759 (let ((list (nreverse (mapcar 'car oldlist))))
762 (if (not (memq new-face list))
763 (setq list (cons new-face list)))
765 (nreverse (face-list)))
768 (defun facemenu-iterate (func list)
769 "Apply FUNC to each element of LIST until one returns non-nil.
770 Returns the non-nil value it found, or nil if all were nil."
771 (while (and list (not (funcall func (car list))))
772 (setq list (cdr list)))
779 ;;; arch-tag: 85f6d02b-9085-420e-b651-0678f0e9c7eb
780 ;;; facemenu.el ends here