(cvs-vc-command-advice): Handle the case where args
[emacs.git] / lisp / facemenu.el
blobca81ebec67d5cc9580896cfa14c3c559a43d1b63
1 ;;; facemenu.el --- create a face menu for interactively adding fonts to text
3 ;; Copyright (c) 1994, 1995, 1996, 2001, 2002 Free Software Foundation, Inc.
5 ;; Author: Boris Goldowsky <boris@gnu.org>
6 ;; Keywords: faces
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
25 ;;; Commentary:
27 ;; This file defines a menu of faces (bold, italic, etc) which allows you to
28 ;; set the face used for a region of the buffer. Some faces also have
29 ;; keybindings, which are shown in the menu.
31 ;; The menu also contains submenus for indentation and justification-changing
32 ;; commands.
34 ;;; Usage:
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-g (or ESC g) + letter:
43 ;; M-g i = "set italic", M-g b = "set bold", etc.
45 ;;; Customization:
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
50 ;; into my .xinitrc:
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])
58 ;; (bold . [?\H-b])
59 ;; (italic . [?\H-i])
60 ;; (bold-italic . [?\H-l])
61 ;; (underline . [?\H-u])))
62 ;; (facemenu-update)
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'.
72 ;;; Known Problems:
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
78 ;; the menu itself.
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.
87 ;;; Code:
89 (provide 'facemenu)
91 (eval-when-compile
92 (require 'help)
93 (require 'button))
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)
99 ;; Global bindings:
100 (define-key global-map [C-down-mouse-2] 'facemenu-menu)
101 (define-key global-map "\M-g" 'facemenu-keymap)
103 (defgroup facemenu nil
104 "Create a face menu for interactively adding fonts to text"
105 :group 'faces
106 :prefix "facemenu-")
108 (defcustom facemenu-keybindings
109 '((default . "d")
110 (bold . "b")
111 (italic . "i")
112 (bold-italic . "l") ; {bold} intersect {italic} = {l}
113 (underline . "u"))
114 "Alist of interesting faces and keybindings.
115 Each element is itself a list: the car is the name of the face,
116 the next element is the key to use as a keyboard equivalent of the menu item;
117 the binding is made in `facemenu-keymap'.
119 The faces specifically mentioned in this list are put at the top of
120 the menu, in the order specified. All other faces which are defined,
121 except for those in `facemenu-unlisted-faces', are listed after them,
122 but get no keyboard equivalents.
124 If you change this variable after loading facemenu.el, you will need to call
125 `facemenu-update' to make it take effect."
126 :type '(repeat (cons face string))
127 :group 'facemenu)
129 (defcustom facemenu-new-faces-at-end t
130 "*Where in the menu to insert newly-created faces.
131 This should be nil to put them at the top of the menu, or t to put them
132 just before \"Other\" at the end."
133 :type 'boolean
134 :group 'facemenu)
136 (defcustom facemenu-unlisted-faces
137 `(modeline region secondary-selection highlight scratch-face
138 ,(purecopy "^font-lock-") ,(purecopy "^gnus-") ,(purecopy "^message-")
139 ,(purecopy "^ediff-") ,(purecopy "^term-") ,(purecopy "^vc-")
140 ,(purecopy "^widget-") ,(purecopy "^custom-") ,(purecopy "^vm-"))
141 "*List of faces not to include in the Face menu.
142 Each element may be either a symbol, which is the name of a face, or a string,
143 which is a regular expression to be matched against face names. Matching
144 faces will not be added to the menu.
146 You can set this list before loading facemenu.el, or add a face to it before
147 creating that face if you do not want it to be listed. If you change the
148 variable so as to eliminate faces that have already been added to the menu,
149 call `facemenu-update' to recalculate the menu contents.
151 If this variable is t, no faces will be added to the menu. This is useful for
152 temporarily turning off the feature that automatically adds faces to the menu
153 when they are created."
154 :type '(choice (const :tag "Don't add faces" t)
155 (const :tag "None (do add any face)" nil)
156 (repeat (choice symbol regexp)))
157 :group 'facemenu)
159 ;;;###autoload
160 (defvar facemenu-face-menu
161 (let ((map (make-sparse-keymap "Face")))
162 (define-key map "o" (cons "Other..." 'facemenu-set-face))
163 map)
164 "Menu keymap for faces.")
165 ;;;###autoload
166 (defalias 'facemenu-face-menu facemenu-face-menu)
168 ;;;###autoload
169 (defvar facemenu-foreground-menu
170 (let ((map (make-sparse-keymap "Foreground Color")))
171 (define-key map "o" (cons "Other..." 'facemenu-set-foreground))
172 map)
173 "Menu keymap for foreground colors.")
174 ;;;###autoload
175 (defalias 'facemenu-foreground-menu facemenu-foreground-menu)
177 ;;;###autoload
178 (defvar facemenu-background-menu
179 (let ((map (make-sparse-keymap "Background Color")))
180 (define-key map "o" (cons "Other..." 'facemenu-set-background))
181 map)
182 "Menu keymap for background colors.")
183 ;;;###autoload
184 (defalias 'facemenu-background-menu facemenu-background-menu)
186 ;;;###autoload
187 (defvar facemenu-special-menu
188 (let ((map (make-sparse-keymap "Special")))
189 (define-key map [?s] (cons (purecopy "Remove Special")
190 'facemenu-remove-special))
191 (define-key map [?t] (cons (purecopy "Intangible")
192 'facemenu-set-intangible))
193 (define-key map [?v] (cons (purecopy "Invisible")
194 'facemenu-set-invisible))
195 (define-key map [?r] (cons (purecopy "Read-Only")
196 'facemenu-set-read-only))
197 map)
198 "Menu keymap for non-face text-properties.")
199 ;;;###autoload
200 (defalias 'facemenu-special-menu facemenu-special-menu)
202 ;;;###autoload
203 (defvar facemenu-justification-menu
204 (let ((map (make-sparse-keymap "Justification")))
205 (define-key map [?c] (cons (purecopy "Center") 'set-justification-center))
206 (define-key map [?b] (cons (purecopy "Full") 'set-justification-full))
207 (define-key map [?r] (cons (purecopy "Right") 'set-justification-right))
208 (define-key map [?l] (cons (purecopy "Left") 'set-justification-left))
209 (define-key map [?u] (cons (purecopy "Unfilled") 'set-justification-none))
210 map)
211 "Submenu for text justification commands.")
212 ;;;###autoload
213 (defalias 'facemenu-justification-menu facemenu-justification-menu)
215 ;;;###autoload
216 (defvar facemenu-indentation-menu
217 (let ((map (make-sparse-keymap "Indentation")))
218 (define-key map [decrease-right-margin]
219 (cons (purecopy "Indent Right Less") 'decrease-right-margin))
220 (define-key map [increase-right-margin]
221 (cons (purecopy "Indent Right More") 'increase-right-margin))
222 (define-key map [decrease-left-margin]
223 (cons (purecopy "Indent Less") 'decrease-left-margin))
224 (define-key map [increase-left-margin]
225 (cons (purecopy "Indent More") 'increase-left-margin))
226 map)
227 "Submenu for indentation commands.")
228 ;;;###autoload
229 (defalias 'facemenu-indentation-menu facemenu-indentation-menu)
231 ;; This is split up to avoid an overlong line in loaddefs.el.
232 ;;;###autoload
233 (defvar facemenu-menu nil
234 "Facemenu top-level menu keymap.")
235 ;;;###autoload
236 (setq facemenu-menu (make-sparse-keymap "Text Properties"))
237 ;;;###autoload
238 (let ((map facemenu-menu))
239 (define-key map [dc] (cons (purecopy "Display Colors") 'list-colors-display))
240 (define-key map [df] (cons (purecopy "Display Faces") 'list-faces-display))
241 (define-key map [dp] (cons (purecopy "Describe Properties")
242 'describe-text-properties))
243 (define-key map [ra] (cons (purecopy "Remove Text Properties")
244 'facemenu-remove-all))
245 (define-key map [rm] (cons (purecopy "Remove Face Properties")
246 'facemenu-remove-face-props))
247 (define-key map [s1] (list (purecopy "--"))))
248 ;;;###autoload
249 (let ((map facemenu-menu))
250 (define-key map [in] (cons (purecopy "Indentation")
251 'facemenu-indentation-menu))
252 (define-key map [ju] (cons (purecopy "Justification")
253 'facemenu-justification-menu))
254 (define-key map [s2] (list (purecopy "--")))
255 (define-key map [sp] (cons (purecopy "Special Properties")
256 'facemenu-special-menu))
257 (define-key map [bg] (cons (purecopy "Background Color")
258 'facemenu-background-menu))
259 (define-key map [fg] (cons (purecopy "Foreground Color")
260 'facemenu-foreground-menu))
261 (define-key map [fc] (cons (purecopy "Face")
262 'facemenu-face-menu)))
263 ;;;###autoload
264 (defalias 'facemenu-menu facemenu-menu)
266 (defvar facemenu-keymap
267 (let ((map (make-sparse-keymap "Set face")))
268 (define-key map "o" (cons (purecopy "Other...") 'facemenu-set-face))
269 map)
270 "Keymap for face-changing commands.
271 `Facemenu-update' fills in the keymap according to the bindings
272 requested in `facemenu-keybindings'.")
273 (defalias 'facemenu-keymap facemenu-keymap)
276 (defcustom facemenu-add-face-function nil
277 "Function called at beginning of text to change or nil.
278 This function is passed the FACE to set and END of text to change, and must
279 return a string which is inserted. It may set `facemenu-end-add-face'."
280 :type '(choice (const :tag "None" nil)
281 function)
282 :group 'facemenu)
284 (defcustom facemenu-end-add-face nil
285 "String to insert or function called at end of text to change or nil.
286 This function is passed the FACE to set, and must return a string which is
287 inserted."
288 :type '(choice (const :tag "None" nil)
289 string
290 function)
291 :group 'facemenu)
293 (defcustom facemenu-remove-face-function nil
294 "When non-nil, this is a function called to remove faces.
295 This function is passed the START and END of text to change.
296 May also be t meaning to use `facemenu-add-face-function'."
297 :type '(choice (const :tag "None" nil)
298 (const :tag "Use add-face" t)
299 function)
300 :group 'facemenu)
302 ;;; Internal Variables
304 (defvar facemenu-color-alist nil
305 ;; Don't initialize here; that doesn't work if preloaded.
306 "Alist of colors, used for completion.
307 If null, `facemenu-read-color' will set it.")
309 (defun facemenu-update ()
310 "Add or update the \"Face\" menu in the menu bar.
311 You can call this to update things if you change any of the menu configuration
312 variables."
313 (interactive)
315 ;; Add each defined face to the menu.
316 (facemenu-iterate 'facemenu-add-new-face
317 (facemenu-complete-face-list facemenu-keybindings)))
319 ;;;###autoload
320 (defun facemenu-set-face (face &optional start end)
321 "Add FACE to the region or next character typed.
322 This adds FACE to the top of the face list; any faces lower on the list that
323 will not show through at all will be removed.
325 Interactively, reads the face name with the minibuffer.
327 If the region is active (normally true except in Transient Mark mode)
328 and there is no prefix argument, this command sets the region to the
329 requested face.
331 Otherwise, this command specifies the face for the next character
332 inserted. Moving point or switching buffers before
333 typing a character to insert cancels the specification."
334 (interactive (list (progn
335 (barf-if-buffer-read-only)
336 (read-face-name "Use face"))
337 (if (and mark-active (not current-prefix-arg))
338 (region-beginning))
339 (if (and mark-active (not current-prefix-arg))
340 (region-end))))
341 (facemenu-add-new-face face)
342 (facemenu-add-face face start end))
344 ;;;###autoload
345 (defun facemenu-set-foreground (color &optional start end)
346 "Set the foreground COLOR of the region or next character typed.
347 This command reads the color in the minibuffer.
349 If the region is active (normally true except in Transient Mark mode)
350 and there is no prefix argument, this command sets the region to the
351 requested face.
353 Otherwise, this command specifies the face for the next character
354 inserted. Moving point or switching buffers before
355 typing a character to insert cancels the specification."
356 (interactive (list (progn
357 (barf-if-buffer-read-only)
358 (facemenu-read-color "Foreground color: "))
359 (if (and mark-active (not current-prefix-arg))
360 (region-beginning))
361 (if (and mark-active (not current-prefix-arg))
362 (region-end))))
363 (unless (color-defined-p color)
364 (message "Color `%s' undefined" color))
365 (facemenu-add-new-color color 'facemenu-foreground-menu)
366 (facemenu-add-face (list (list :foreground color)) start end))
368 ;;;###autoload
369 (defun facemenu-set-background (color &optional start end)
370 "Set the background COLOR of the region or next character typed.
371 This command reads the color in the minibuffer.
373 If the region is active (normally true except in Transient Mark mode)
374 and there is no prefix argument, this command sets the region to the
375 requested face.
377 Otherwise, this command specifies the face for the next character
378 inserted. Moving point or switching buffers before
379 typing a character to insert cancels the specification."
380 (interactive (list (progn
381 (barf-if-buffer-read-only)
382 (facemenu-read-color "Background color: "))
383 (if (and mark-active (not current-prefix-arg))
384 (region-beginning))
385 (if (and mark-active (not current-prefix-arg))
386 (region-end))))
387 (unless (color-defined-p color)
388 (message "Color `%s' undefined" color))
389 (facemenu-add-new-color color 'facemenu-background-menu)
390 (facemenu-add-face (list (list :background color)) start end))
392 ;;;###autoload
393 (defun facemenu-set-face-from-menu (face start end)
394 "Set the FACE of the region or next character typed.
395 This function is designed to be called from a menu; the face to use
396 is the menu item's name.
398 If the region is active (normally true except in Transient Mark mode)
399 and there is no prefix argument, this command sets the region to the
400 requested face.
402 Otherwise, this command specifies the face for the next character
403 inserted. Moving point or switching buffers before
404 typing a character to insert cancels the specification."
405 (interactive (list last-command-event
406 (if (and mark-active (not current-prefix-arg))
407 (region-beginning))
408 (if (and mark-active (not current-prefix-arg))
409 (region-end))))
410 (barf-if-buffer-read-only)
411 (facemenu-get-face face)
412 (if start
413 (facemenu-add-face face start end)
414 (facemenu-add-face face)))
416 ;;;###autoload
417 (defun facemenu-set-invisible (start end)
418 "Make the region invisible.
419 This sets the `invisible' text property; it can be undone with
420 `facemenu-remove-special'."
421 (interactive "r")
422 (add-text-properties start end '(invisible t)))
424 ;;;###autoload
425 (defun facemenu-set-intangible (start end)
426 "Make the region intangible: disallow moving into it.
427 This sets the `intangible' text property; it can be undone with
428 `facemenu-remove-special'."
429 (interactive "r")
430 (add-text-properties start end '(intangible t)))
432 ;;;###autoload
433 (defun facemenu-set-read-only (start end)
434 "Make the region unmodifiable.
435 This sets the `read-only' text property; it can be undone with
436 `facemenu-remove-special'."
437 (interactive "r")
438 (add-text-properties start end '(read-only t)))
440 ;;;###autoload
441 (defun facemenu-remove-face-props (start end)
442 "Remove `face' and `mouse-face' text properties."
443 (interactive "*r") ; error if buffer is read-only despite the next line.
444 (let ((inhibit-read-only t))
445 (remove-text-properties
446 start end '(face nil mouse-face nil))))
448 ;;;###autoload
449 (defun facemenu-remove-all (start end)
450 "Remove all text properties from the region."
451 (interactive "*r") ; error if buffer is read-only despite the next line.
452 (let ((inhibit-read-only t))
453 (set-text-properties start end nil)))
455 ;;;###autoload
456 (defun facemenu-remove-special (start end)
457 "Remove all the \"special\" text properties from the region.
458 These special properties include `invisible', `intangible' and `read-only'."
459 (interactive "*r") ; error if buffer is read-only despite the next line.
460 (let ((inhibit-read-only t))
461 (remove-text-properties
462 start end '(invisible nil intangible nil read-only nil))))
464 ;;;###autoload
465 (defun facemenu-read-color (&optional prompt)
466 "Read a color using the minibuffer."
467 (let ((col (completing-read (or prompt "Color: ")
468 (or facemenu-color-alist
469 (mapcar 'list (defined-colors)))
470 nil t)))
471 (if (equal "" col)
473 col)))
475 ;;;###autoload
476 (defun list-colors-display (&optional list)
477 "Display names of defined colors, and show what they look like.
478 If the optional argument LIST is non-nil, it should be a list of
479 colors to display. Otherwise, this command computes a list
480 of colors that the current display can handle."
481 (interactive)
482 (when (and (null list) (> (display-color-cells) 0))
483 (setq list (defined-colors))
484 ;; Delete duplicate colors.
485 (let ((l list))
486 (while (cdr l)
487 (if (facemenu-color-equal (car l) (car (cdr l)))
488 (setcdr l (cdr (cdr l)))
489 (setq l (cdr l)))))
490 (when (memq (display-visual-class) '(gray-scale pseudo-color direct-color))
491 ;; Don't show more than what the display can handle.
492 (let ((lc (nthcdr (1- (display-color-cells)) list)))
493 (if lc
494 (setcdr lc nil)))))
495 (with-output-to-temp-buffer "*Colors*"
496 (save-excursion
497 (set-buffer standard-output)
498 (let (s)
499 (while list
500 (setq s (point))
501 (insert (car list))
502 (indent-to 20)
503 (put-text-property s (point) 'face
504 (cons 'background-color (car list)))
505 (setq s (point))
506 (insert " " (car list) "\n")
507 (put-text-property s (point) 'face
508 (cons 'foreground-color (car list)))
509 (setq list (cdr list)))))))
511 (defun facemenu-color-equal (a b)
512 "Return t if colors A and B are the same color.
513 A and B should be strings naming colors.
514 This function queries the display system to find out what the color
515 names mean. It returns nil if the colors differ or if it can't
516 determine the correct answer."
517 (cond ((equal a b) t)
518 ((equal (color-values a) (color-values b)))))
520 (defun facemenu-add-face (face &optional start end)
521 "Add FACE to text between START and END.
522 If START is nil or START to END is empty, add FACE to next typed character
523 instead. For each section of that region that has a different face property,
524 FACE will be consed onto it, and other faces that are completely hidden by
525 that will be removed from the list.
526 If `facemenu-add-face-function' and maybe `facemenu-end-add-face' are non-nil,
527 they are used to set the face information.
529 As a special case, if FACE is `default', then the region is left with NO face
530 text property. Otherwise, selecting the default face would not have any
531 effect. See `facemenu-remove-face-function'."
532 (interactive "*xFace: \nr")
533 (if (and (eq face 'default)
534 (not (eq facemenu-remove-face-function t)))
535 (if facemenu-remove-face-function
536 (funcall facemenu-remove-face-function start end)
537 (if (and start (< start end))
538 (remove-text-properties start end '(face default))
539 (setq self-insert-face 'default
540 self-insert-face-command this-command)))
541 (if facemenu-add-face-function
542 (save-excursion
543 (if end (goto-char end))
544 (save-excursion
545 (if start (goto-char start))
546 (insert-before-markers
547 (funcall facemenu-add-face-function face end)))
548 (if facemenu-end-add-face
549 (insert (if (stringp facemenu-end-add-face)
550 facemenu-end-add-face
551 (funcall facemenu-end-add-face face)))))
552 (if (and start (< start end))
553 (let ((part-start start) part-end)
554 (while (not (= part-start end))
555 (setq part-end (next-single-property-change part-start 'face
556 nil end))
557 (let ((prev (get-text-property part-start 'face)))
558 (put-text-property part-start part-end 'face
559 (if (null prev)
560 face
561 (facemenu-active-faces
562 (cons face
563 (if (listp prev)
564 prev
565 (list prev)))))))
566 (setq part-start part-end)))
567 (setq self-insert-face (if (eq last-command self-insert-face-command)
568 (cons face (if (listp self-insert-face)
569 self-insert-face
570 (list self-insert-face)))
571 face)
572 self-insert-face-command this-command)))))
574 (defun facemenu-active-faces (face-list &optional frame)
575 "Return from FACE-LIST those faces that would be used for display.
576 This means each face attribute is not specified in a face earlier in FACE-LIST
577 and such a face is therefore active when used to display text.
578 If the optional argument FRAME is given, use the faces in that frame; otherwise
579 use the selected frame. If t, then the global, non-frame faces are used."
580 (let* ((mask-atts (copy-sequence
581 (if (consp (car face-list))
582 (face-attributes-as-vector (car face-list))
583 (or (internal-lisp-face-p (car face-list) frame)
584 (check-face (car face-list))))))
585 (active-list (list (car face-list)))
586 (face-list (cdr face-list))
587 (mask-len (length mask-atts)))
588 (while face-list
589 (if (let ((face-atts
590 (if (consp (car face-list))
591 (face-attributes-as-vector (car face-list))
592 (or (internal-lisp-face-p (car face-list) frame)
593 (check-face (car face-list)))))
594 (i mask-len)
595 (useful nil))
596 (while (> (setq i (1- i)) 1)
597 (and (not (memq (aref face-atts i) '(nil unspecified)))
598 (memq (aref mask-atts i) '(nil unspecified))
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)))
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 (let ((name (symbol-name symbol)))
609 (cond ((facep symbol))
610 (t (make-face symbol))))
611 symbol)
613 (defun facemenu-add-new-face (face)
614 "Add FACE (a face) to the Face menu.
616 This is called whenever you create a new face."
617 (let* (name
618 symbol
619 menu docstring
620 (key (cdr (assoc face facemenu-keybindings)))
621 function menu-val)
622 (if (symbolp face)
623 (setq name (symbol-name face)
624 symbol face)
625 (setq name face
626 symbol (intern name)))
627 (setq menu 'facemenu-face-menu)
628 (setq docstring
629 (format "Select face `%s' for subsequent insertion."
630 name))
631 (cond ((eq t facemenu-unlisted-faces))
632 ((memq symbol facemenu-unlisted-faces))
633 ;; test against regexps in facemenu-unlisted-faces
634 ((let ((unlisted facemenu-unlisted-faces)
635 (matched nil))
636 (while (and unlisted (not matched))
637 (if (and (stringp (car unlisted))
638 (string-match (car unlisted) name))
639 (setq matched t)
640 (setq unlisted (cdr unlisted))))
641 matched))
642 (key ; has a keyboard equivalent. These go at the front.
643 (setq function (intern (concat "facemenu-set-" name)))
644 (fset function
645 `(lambda ()
646 ,docstring
647 (interactive)
648 (facemenu-set-face
649 (quote ,symbol)
650 (if (and mark-active (not current-prefix-arg))
651 (region-beginning))
652 (if (and mark-active (not current-prefix-arg))
653 (region-end)))))
654 (define-key 'facemenu-keymap key (cons name function))
655 (define-key menu key (cons name function)))
656 ((facemenu-iterate ; check if equivalent face is already in the menu
657 (lambda (m) (and (listp m)
658 (symbolp (car m))
659 (face-equal (car m) symbol)))
660 (cdr (symbol-function menu))))
661 (t ; No keyboard equivalent. Figure out where to put it:
662 (setq key (vector symbol)
663 function 'facemenu-set-face-from-menu
664 menu-val (symbol-function menu))
665 (if (and facemenu-new-faces-at-end
666 (> (length menu-val) 3))
667 (define-key-after menu-val key (cons name function)
668 (car (nth (- (length menu-val) 3) menu-val)))
669 (define-key menu key (cons name function))))))
670 nil) ; Return nil for facemenu-iterate
672 (defun facemenu-add-new-color (color &optional menu)
673 "Add COLOR (a color name string) to the appropriate Face menu.
674 MENU should be `facemenu-foreground-menu' or
675 `facemenu-background-menu'.
677 This is called whenever you use a new color."
678 (let* (name
679 symbol
680 docstring
681 function menu-val key
682 (color-p (memq menu '(facemenu-foreground-menu
683 facemenu-background-menu))))
684 (unless (stringp color)
685 (error "%s is not a color" color))
686 (setq name color
687 symbol (intern name))
689 (cond ((eq menu 'facemenu-foreground-menu)
690 (setq docstring
691 (format "Select foreground color %s for subsequent insertion."
692 name)))
693 ((eq menu 'facemenu-background-menu)
694 (setq docstring
695 (format "Select background color %s for subsequent insertion."
696 name))))
697 (cond ((facemenu-iterate ; check if equivalent face is already in the menu
698 (lambda (m) (and (listp m)
699 (symbolp (car m))
700 (stringp (cadr m))
701 (string-equal (cadr m) color)))
702 (cdr (symbol-function menu))))
703 (t ; No keyboard equivalent. Figure out where to put it:
704 (setq key (vector symbol)
705 function 'facemenu-set-face-from-menu
706 menu-val (symbol-function menu))
707 (if (and facemenu-new-faces-at-end
708 (> (length menu-val) 3))
709 (define-key-after menu-val key (cons name function)
710 (car (nth (- (length menu-val) 3) menu-val)))
711 (define-key menu key (cons name function))))))
712 nil) ; Return nil for facemenu-iterate
714 (defun facemenu-complete-face-list (&optional oldlist)
715 "Return list of all faces that look different.
716 Starts with given ALIST of faces, and adds elements only if they display
717 differently from any face already on the list.
718 The faces on ALIST will end up at the end of the returned list, in reverse
719 order."
720 (let ((list (nreverse (mapcar 'car oldlist))))
721 (facemenu-iterate
722 (lambda (new-face)
723 (if (not (memq new-face list))
724 (setq list (cons new-face list)))
725 nil)
726 (nreverse (face-list)))
727 list))
729 (defun facemenu-iterate (func list)
730 "Apply FUNC to each element of LIST until one returns non-nil.
731 Returns the non-nil value it found, or nil if all were nil."
732 (while (and list (not (funcall func (car list))))
733 (setq list (cdr list)))
734 (car list))
736 (facemenu-update)
738 ;;; facemenu.el ends here