(diff-default-read-only): Change default.
[emacs.git] / lisp / facemenu.el
blob3893e320655e32a48c41f33f1c7c5516653a3972
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 (eval-when-compile
90 (require 'help)
91 (require 'button))
93 ;;; Provide some binding for startup:
94 ;;;###autoload (define-key global-map "\M-g" 'facemenu-keymap)
95 ;;;###autoload (autoload 'facemenu-keymap "facemenu" "Keymap for face-changing commands." t 'keymap)
97 ;; Global bindings:
98 (define-key global-map [C-down-mouse-2] 'facemenu-menu)
99 (define-key global-map "\M-g" 'facemenu-keymap)
101 (defgroup facemenu nil
102 "Create a face menu for interactively adding fonts to text"
103 :group 'faces
104 :prefix "facemenu-")
106 (defcustom facemenu-keybindings
107 '((default . "d")
108 (bold . "b")
109 (italic . "i")
110 (bold-italic . "l") ; {bold} intersect {italic} = {l}
111 (underline . "u"))
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))
125 :group 'facemenu)
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."
131 :type 'boolean
132 :group 'facemenu)
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)))
155 :group 'facemenu)
157 ;;;###autoload
158 (defvar facemenu-face-menu
159 (let ((map (make-sparse-keymap "Face")))
160 (define-key map "o" (cons "Other..." 'facemenu-set-face))
161 map)
162 "Menu keymap for faces.")
163 ;;;###autoload
164 (defalias 'facemenu-face-menu facemenu-face-menu)
166 ;;;###autoload
167 (defvar facemenu-foreground-menu
168 (let ((map (make-sparse-keymap "Foreground Color")))
169 (define-key map "o" (cons "Other..." 'facemenu-set-foreground))
170 map)
171 "Menu keymap for foreground colors.")
172 ;;;###autoload
173 (defalias 'facemenu-foreground-menu facemenu-foreground-menu)
175 ;;;###autoload
176 (defvar facemenu-background-menu
177 (let ((map (make-sparse-keymap "Background Color")))
178 (define-key map "o" (cons "Other..." 'facemenu-set-background))
179 map)
180 "Menu keymap for background colors.")
181 ;;;###autoload
182 (defalias 'facemenu-background-menu facemenu-background-menu)
184 ;;;###autoload
185 (defvar facemenu-special-menu
186 (let ((map (make-sparse-keymap "Special")))
187 (define-key map [?s] (cons (purecopy "Remove Special")
188 'facemenu-remove-special))
189 (define-key map [?t] (cons (purecopy "Intangible")
190 'facemenu-set-intangible))
191 (define-key map [?v] (cons (purecopy "Invisible")
192 'facemenu-set-invisible))
193 (define-key map [?r] (cons (purecopy "Read-Only")
194 'facemenu-set-read-only))
195 map)
196 "Menu keymap for non-face text-properties.")
197 ;;;###autoload
198 (defalias 'facemenu-special-menu facemenu-special-menu)
200 ;;;###autoload
201 (defvar facemenu-justification-menu
202 (let ((map (make-sparse-keymap "Justification")))
203 (define-key map [?c] (cons (purecopy "Center") 'set-justification-center))
204 (define-key map [?b] (cons (purecopy "Full") 'set-justification-full))
205 (define-key map [?r] (cons (purecopy "Right") 'set-justification-right))
206 (define-key map [?l] (cons (purecopy "Left") 'set-justification-left))
207 (define-key map [?u] (cons (purecopy "Unfilled") 'set-justification-none))
208 map)
209 "Submenu for text justification commands.")
210 ;;;###autoload
211 (defalias 'facemenu-justification-menu facemenu-justification-menu)
213 ;;;###autoload
214 (defvar facemenu-indentation-menu
215 (let ((map (make-sparse-keymap "Indentation")))
216 (define-key map [decrease-right-margin]
217 (cons (purecopy "Indent Right Less") 'decrease-right-margin))
218 (define-key map [increase-right-margin]
219 (cons (purecopy "Indent Right More") 'increase-right-margin))
220 (define-key map [decrease-left-margin]
221 (cons (purecopy "Indent Less") 'decrease-left-margin))
222 (define-key map [increase-left-margin]
223 (cons (purecopy "Indent More") 'increase-left-margin))
224 map)
225 "Submenu for indentation commands.")
226 ;;;###autoload
227 (defalias 'facemenu-indentation-menu facemenu-indentation-menu)
229 ;; This is split up to avoid an overlong line in loaddefs.el.
230 ;;;###autoload
231 (defvar facemenu-menu nil
232 "Facemenu top-level menu keymap.")
233 ;;;###autoload
234 (setq facemenu-menu (make-sparse-keymap "Text Properties"))
235 ;;;###autoload
236 (let ((map facemenu-menu))
237 (define-key map [dc] (cons (purecopy "Display Colors") 'list-colors-display))
238 (define-key map [df] (cons (purecopy "Display Faces") 'list-faces-display))
239 (define-key map [dp] (cons (purecopy "Describe Properties")
240 'describe-text-properties))
241 (define-key map [ra] (cons (purecopy "Remove Text Properties")
242 'facemenu-remove-all))
243 (define-key map [rm] (cons (purecopy "Remove Face Properties")
244 'facemenu-remove-face-props))
245 (define-key map [s1] (list (purecopy "--"))))
246 ;;;###autoload
247 (let ((map facemenu-menu))
248 (define-key map [in] (cons (purecopy "Indentation")
249 'facemenu-indentation-menu))
250 (define-key map [ju] (cons (purecopy "Justification")
251 'facemenu-justification-menu))
252 (define-key map [s2] (list (purecopy "--")))
253 (define-key map [sp] (cons (purecopy "Special Properties")
254 'facemenu-special-menu))
255 (define-key map [bg] (cons (purecopy "Background Color")
256 'facemenu-background-menu))
257 (define-key map [fg] (cons (purecopy "Foreground Color")
258 'facemenu-foreground-menu))
259 (define-key map [fc] (cons (purecopy "Face")
260 'facemenu-face-menu)))
261 ;;;###autoload
262 (defalias 'facemenu-menu facemenu-menu)
264 (defvar facemenu-keymap
265 (let ((map (make-sparse-keymap "Set face")))
266 (define-key map "o" (cons (purecopy "Other...") 'facemenu-set-face))
267 map)
268 "Keymap for face-changing commands.
269 `Facemenu-update' fills in the keymap according to the bindings
270 requested in `facemenu-keybindings'.")
271 (defalias 'facemenu-keymap facemenu-keymap)
274 (defcustom facemenu-add-face-function nil
275 "Function called at beginning of text to change or nil.
276 This function is passed the FACE to set and END of text to change, and must
277 return a string which is inserted. It may set `facemenu-end-add-face'."
278 :type '(choice (const :tag "None" nil)
279 function)
280 :group 'facemenu)
282 (defcustom facemenu-end-add-face nil
283 "String to insert or function called at end of text to change or nil.
284 This function is passed the FACE to set, and must return a string which is
285 inserted."
286 :type '(choice (const :tag "None" nil)
287 string
288 function)
289 :group 'facemenu)
291 (defcustom facemenu-remove-face-function nil
292 "When non-nil, this is a function called to remove faces.
293 This function is passed the START and END of text to change.
294 May also be t meaning to use `facemenu-add-face-function'."
295 :type '(choice (const :tag "None" nil)
296 (const :tag "Use add-face" t)
297 function)
298 :group 'facemenu)
300 ;;; Internal Variables
302 (defvar facemenu-color-alist nil
303 ;; Don't initialize here; that doesn't work if preloaded.
304 "Alist of colors, used for completion.
305 If null, `facemenu-read-color' will set it.")
307 (defun facemenu-update ()
308 "Add or update the \"Face\" menu in the menu bar.
309 You can call this to update things if you change any of the menu configuration
310 variables."
311 (interactive)
313 ;; Add each defined face to the menu.
314 (facemenu-iterate 'facemenu-add-new-face
315 (facemenu-complete-face-list facemenu-keybindings)))
317 ;;;###autoload
318 (defun facemenu-set-face (face &optional start end)
319 "Add FACE to the region or next character typed.
320 This adds FACE to the top of the face list; any faces lower on the list that
321 will not show through at all will be removed.
323 Interactively, reads the face name with the minibuffer.
325 If the region is active (normally true except in Transient Mark mode)
326 and there is no prefix argument, this command sets the region to the
327 requested face.
329 Otherwise, this command specifies the face for the next character
330 inserted. Moving point or switching buffers before
331 typing a character to insert cancels the specification."
332 (interactive (list (progn
333 (barf-if-buffer-read-only)
334 (read-face-name "Use face"))
335 (if (and mark-active (not current-prefix-arg))
336 (region-beginning))
337 (if (and mark-active (not current-prefix-arg))
338 (region-end))))
339 (facemenu-add-new-face face)
340 (facemenu-add-face face start end))
342 ;;;###autoload
343 (defun facemenu-set-foreground (color &optional start end)
344 "Set the foreground COLOR of the region or next character typed.
345 This command reads the color in the minibuffer.
347 If the region is active (normally true except in Transient Mark mode)
348 and there is no prefix argument, this command sets the region to the
349 requested face.
351 Otherwise, this command specifies the face for the next character
352 inserted. Moving point or switching buffers before
353 typing a character to insert cancels the specification."
354 (interactive (list (progn
355 (barf-if-buffer-read-only)
356 (facemenu-read-color "Foreground color: "))
357 (if (and mark-active (not current-prefix-arg))
358 (region-beginning))
359 (if (and mark-active (not current-prefix-arg))
360 (region-end))))
361 (unless (color-defined-p color)
362 (message "Color `%s' undefined" color))
363 (facemenu-add-new-color color 'facemenu-foreground-menu)
364 (facemenu-add-face (list (list :foreground color)) start end))
366 ;;;###autoload
367 (defun facemenu-set-background (color &optional start end)
368 "Set the background COLOR of the region or next character typed.
369 This command reads the color in the minibuffer.
371 If the region is active (normally true except in Transient Mark mode)
372 and there is no prefix argument, this command sets the region to the
373 requested face.
375 Otherwise, this command specifies the face for the next character
376 inserted. Moving point or switching buffers before
377 typing a character to insert cancels the specification."
378 (interactive (list (progn
379 (barf-if-buffer-read-only)
380 (facemenu-read-color "Background color: "))
381 (if (and mark-active (not current-prefix-arg))
382 (region-beginning))
383 (if (and mark-active (not current-prefix-arg))
384 (region-end))))
385 (unless (color-defined-p color)
386 (message "Color `%s' undefined" color))
387 (facemenu-add-new-color color 'facemenu-background-menu)
388 (facemenu-add-face (list (list :background color)) start end))
390 ;;;###autoload
391 (defun facemenu-set-face-from-menu (face start end)
392 "Set the FACE of the region or next character typed.
393 This function is designed to be called from a menu; the face to use
394 is the menu item's name.
396 If the region is active (normally true except in Transient Mark mode)
397 and there is no prefix argument, this command sets the region to the
398 requested face.
400 Otherwise, this command specifies the face for the next character
401 inserted. Moving point or switching buffers before
402 typing a character to insert cancels the specification."
403 (interactive (list last-command-event
404 (if (and mark-active (not current-prefix-arg))
405 (region-beginning))
406 (if (and mark-active (not current-prefix-arg))
407 (region-end))))
408 (barf-if-buffer-read-only)
409 (facemenu-get-face face)
410 (if start
411 (facemenu-add-face face start end)
412 (facemenu-add-face face)))
414 ;;;###autoload
415 (defun facemenu-set-invisible (start end)
416 "Make the region invisible.
417 This sets the `invisible' text property; it can be undone with
418 `facemenu-remove-special'."
419 (interactive "r")
420 (add-text-properties start end '(invisible t)))
422 ;;;###autoload
423 (defun facemenu-set-intangible (start end)
424 "Make the region intangible: disallow moving into it.
425 This sets the `intangible' text property; it can be undone with
426 `facemenu-remove-special'."
427 (interactive "r")
428 (add-text-properties start end '(intangible t)))
430 ;;;###autoload
431 (defun facemenu-set-read-only (start end)
432 "Make the region unmodifiable.
433 This sets the `read-only' text property; it can be undone with
434 `facemenu-remove-special'."
435 (interactive "r")
436 (add-text-properties start end '(read-only t)))
438 ;;;###autoload
439 (defun facemenu-remove-face-props (start end)
440 "Remove `face' and `mouse-face' text properties."
441 (interactive "*r") ; error if buffer is read-only despite the next line.
442 (let ((inhibit-read-only t))
443 (remove-text-properties
444 start end '(face nil mouse-face nil))))
446 ;;;###autoload
447 (defun facemenu-remove-all (start end)
448 "Remove all text properties from the region."
449 (interactive "*r") ; error if buffer is read-only despite the next line.
450 (let ((inhibit-read-only t))
451 (set-text-properties start end nil)))
453 ;;;###autoload
454 (defun facemenu-remove-special (start end)
455 "Remove all the \"special\" text properties from the region.
456 These special properties include `invisible', `intangible' and `read-only'."
457 (interactive "*r") ; error if buffer is read-only despite the next line.
458 (let ((inhibit-read-only t))
459 (remove-text-properties
460 start end '(invisible nil intangible nil read-only nil))))
462 ;;;###autoload
463 (defun facemenu-read-color (&optional prompt)
464 "Read a color using the minibuffer."
465 (let ((col (completing-read (or prompt "Color: ")
466 (or facemenu-color-alist
467 (defined-colors))
468 nil t)))
469 (if (equal "" col)
471 col)))
473 ;;;###autoload
474 (defun list-colors-display (&optional list)
475 "Display names of defined colors, and show what they look like.
476 If the optional argument LIST is non-nil, it should be a list of
477 colors to display. Otherwise, this command computes a list
478 of colors that the current display can handle."
479 (interactive)
480 (when (and (null list) (> (display-color-cells) 0))
481 (setq list (defined-colors))
482 ;; Delete duplicate colors.
483 (let ((l list))
484 (while (cdr l)
485 (if (facemenu-color-equal (car l) (car (cdr l)))
486 (setcdr l (cdr (cdr l)))
487 (setq l (cdr l)))))
488 (when (memq (display-visual-class) '(gray-scale pseudo-color direct-color))
489 ;; Don't show more than what the display can handle.
490 (let ((lc (nthcdr (1- (display-color-cells)) list)))
491 (if lc
492 (setcdr lc nil)))))
493 (with-output-to-temp-buffer "*Colors*"
494 (save-excursion
495 (set-buffer standard-output)
496 (let (s)
497 (while list
498 (setq s (point))
499 (insert (car list))
500 (indent-to 20)
501 (put-text-property s (point) 'face
502 (cons 'background-color (car list)))
503 (setq s (point))
504 (insert " " (car list) "\n")
505 (put-text-property s (point) 'face
506 (cons 'foreground-color (car list)))
507 (setq list (cdr list)))))))
509 (defun facemenu-color-equal (a b)
510 "Return t if colors A and B are the same color.
511 A and B should be strings naming colors.
512 This function queries the display system to find out what the color
513 names mean. It returns nil if the colors differ or if it can't
514 determine the correct answer."
515 (cond ((equal a b) t)
516 ((equal (color-values a) (color-values b)))))
518 (defun facemenu-add-face (face &optional start end)
519 "Add FACE to text between START and END.
520 If START is nil or START to END is empty, add FACE to next typed character
521 instead. For each section of that region that has a different face property,
522 FACE will be consed onto it, and other faces that are completely hidden by
523 that will be removed from the list.
524 If `facemenu-add-face-function' and maybe `facemenu-end-add-face' are non-nil,
525 they are used to set the face information.
527 As a special case, if FACE is `default', then the region is left with NO face
528 text property. Otherwise, selecting the default face would not have any
529 effect. See `facemenu-remove-face-function'."
530 (interactive "*xFace: \nr")
531 (if (and (eq face 'default)
532 (not (eq facemenu-remove-face-function t)))
533 (if facemenu-remove-face-function
534 (funcall facemenu-remove-face-function start end)
535 (if (and start (< start end))
536 (remove-text-properties start end '(face default))
537 (setq self-insert-face 'default
538 self-insert-face-command this-command)))
539 (if facemenu-add-face-function
540 (save-excursion
541 (if end (goto-char end))
542 (save-excursion
543 (if start (goto-char start))
544 (insert-before-markers
545 (funcall facemenu-add-face-function face end)))
546 (if facemenu-end-add-face
547 (insert (if (stringp facemenu-end-add-face)
548 facemenu-end-add-face
549 (funcall facemenu-end-add-face face)))))
550 (if (and start (< start end))
551 (let ((part-start start) part-end)
552 (while (not (= part-start end))
553 (setq part-end (next-single-property-change part-start 'face
554 nil end))
555 (let ((prev (get-text-property part-start 'face)))
556 (put-text-property part-start part-end 'face
557 (if (null prev)
558 face
559 (facemenu-active-faces
560 (cons face
561 (if (listp prev)
562 prev
563 (list prev)))))))
564 (setq part-start part-end)))
565 (setq self-insert-face (if (eq last-command self-insert-face-command)
566 (cons face (if (listp self-insert-face)
567 self-insert-face
568 (list self-insert-face)))
569 face)
570 self-insert-face-command this-command)))))
572 (defun facemenu-active-faces (face-list &optional frame)
573 "Return from FACE-LIST those faces that would be used for display.
574 This means each face attribute is not specified in a face earlier in FACE-LIST
575 and such a face is therefore active when used to display text.
576 If the optional argument FRAME is given, use the faces in that frame; otherwise
577 use the selected frame. If t, then the global, non-frame faces are used."
578 (let* ((mask-atts (copy-sequence
579 (if (consp (car face-list))
580 (face-attributes-as-vector (car face-list))
581 (or (internal-lisp-face-p (car face-list) frame)
582 (check-face (car face-list))))))
583 (active-list (list (car face-list)))
584 (face-list (cdr face-list))
585 (mask-len (length mask-atts)))
586 (while face-list
587 (if (let ((face-atts
588 (if (consp (car face-list))
589 (face-attributes-as-vector (car face-list))
590 (or (internal-lisp-face-p (car face-list) frame)
591 (check-face (car face-list)))))
592 (i mask-len)
593 (useful nil))
594 (while (> (setq i (1- i)) 1)
595 (and (not (memq (aref face-atts i) '(nil unspecified)))
596 (memq (aref mask-atts i) '(nil unspecified))
597 (aset mask-atts i (setq useful t))))
598 useful)
599 (setq active-list (cons (car face-list) active-list)))
600 (setq face-list (cdr face-list)))
601 (nreverse active-list)))
603 (defun facemenu-get-face (symbol)
604 "Make sure FACE exists.
605 If not, create it and add it to the appropriate menu. Return the SYMBOL."
606 (let ((name (symbol-name symbol)))
607 (cond ((facep symbol))
608 (t (make-face symbol))))
609 symbol)
611 (defun facemenu-add-new-face (face)
612 "Add FACE (a face) to the Face menu.
614 This is called whenever you create a new face."
615 (let* (name
616 symbol
617 menu docstring
618 (key (cdr (assoc face facemenu-keybindings)))
619 function menu-val)
620 (if (symbolp face)
621 (setq name (symbol-name face)
622 symbol face)
623 (setq name face
624 symbol (intern name)))
625 (setq menu 'facemenu-face-menu)
626 (setq docstring
627 (format "Select face `%s' for subsequent insertion."
628 name))
629 (cond ((eq t facemenu-unlisted-faces))
630 ((memq symbol facemenu-unlisted-faces))
631 ;; test against regexps in facemenu-unlisted-faces
632 ((let ((unlisted facemenu-unlisted-faces)
633 (matched nil))
634 (while (and unlisted (not matched))
635 (if (and (stringp (car unlisted))
636 (string-match (car unlisted) name))
637 (setq matched t)
638 (setq unlisted (cdr unlisted))))
639 matched))
640 (key ; has a keyboard equivalent. These go at the front.
641 (setq function (intern (concat "facemenu-set-" name)))
642 (fset function
643 `(lambda ()
644 ,docstring
645 (interactive)
646 (facemenu-set-face
647 (quote ,symbol)
648 (if (and mark-active (not current-prefix-arg))
649 (region-beginning))
650 (if (and mark-active (not current-prefix-arg))
651 (region-end)))))
652 (define-key 'facemenu-keymap key (cons name function))
653 (define-key menu key (cons name function)))
654 ((facemenu-iterate ; check if equivalent face is already in the menu
655 (lambda (m) (and (listp m)
656 (symbolp (car m))
657 (face-equal (car m) symbol)))
658 (cdr (symbol-function menu))))
659 (t ; No keyboard equivalent. Figure out where to put it:
660 (setq key (vector symbol)
661 function 'facemenu-set-face-from-menu
662 menu-val (symbol-function menu))
663 (if (and facemenu-new-faces-at-end
664 (> (length menu-val) 3))
665 (define-key-after menu-val key (cons name function)
666 (car (nth (- (length menu-val) 3) menu-val)))
667 (define-key menu key (cons name function))))))
668 nil) ; Return nil for facemenu-iterate
670 (defun facemenu-add-new-color (color &optional menu)
671 "Add COLOR (a color name string) to the appropriate Face menu.
672 MENU should be `facemenu-foreground-menu' or
673 `facemenu-background-menu'.
675 This is called whenever you use a new color."
676 (let* (name
677 symbol
678 docstring
679 function menu-val key
680 (color-p (memq menu '(facemenu-foreground-menu
681 facemenu-background-menu))))
682 (unless (stringp color)
683 (error "%s is not a color" color))
684 (setq name color
685 symbol (intern name))
687 (cond ((eq menu 'facemenu-foreground-menu)
688 (setq docstring
689 (format "Select foreground color %s for subsequent insertion."
690 name)))
691 ((eq menu 'facemenu-background-menu)
692 (setq docstring
693 (format "Select background color %s for subsequent insertion."
694 name))))
695 (cond ((facemenu-iterate ; check if equivalent face is already in the menu
696 (lambda (m) (and (listp m)
697 (symbolp (car m))
698 (stringp (cadr m))
699 (string-equal (cadr m) color)))
700 (cdr (symbol-function menu))))
701 (t ; No keyboard equivalent. Figure out where to put it:
702 (setq key (vector symbol)
703 function 'facemenu-set-face-from-menu
704 menu-val (symbol-function menu))
705 (if (and facemenu-new-faces-at-end
706 (> (length menu-val) 3))
707 (define-key-after menu-val key (cons name function)
708 (car (nth (- (length menu-val) 3) menu-val)))
709 (define-key menu key (cons name function))))))
710 nil) ; Return nil for facemenu-iterate
712 (defun facemenu-complete-face-list (&optional oldlist)
713 "Return list of all faces that look different.
714 Starts with given ALIST of faces, and adds elements only if they display
715 differently from any face already on the list.
716 The faces on ALIST will end up at the end of the returned list, in reverse
717 order."
718 (let ((list (nreverse (mapcar 'car oldlist))))
719 (facemenu-iterate
720 (lambda (new-face)
721 (if (not (memq new-face list))
722 (setq list (cons new-face list)))
723 nil)
724 (nreverse (face-list)))
725 list))
727 (defun facemenu-iterate (func list)
728 "Apply FUNC to each element of LIST until one returns non-nil.
729 Returns the non-nil value it found, or nil if all were nil."
730 (while (and list (not (funcall func (car list))))
731 (setq list (cdr list)))
732 (car list))
734 (facemenu-update)
736 (provide 'facemenu)
738 ;;; arch-tag: 85f6d02b-9085-420e-b651-0678f0e9c7eb
739 ;;; facemenu.el ends here