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>
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-g (or ESC g) + letter:
43 ;; M-g i = "set italic", M-g 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.
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)
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"
108 (defcustom facemenu-keybindings
112 (bold-italic .
"l") ; {bold} intersect {italic} = {l}
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
))
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."
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
)))
160 (defvar facemenu-face-menu
161 (let ((map (make-sparse-keymap "Face")))
162 (define-key map
"o" (cons "Other..." 'facemenu-set-face
))
164 "Menu keymap for faces.")
166 (defalias 'facemenu-face-menu facemenu-face-menu
)
169 (defvar facemenu-foreground-menu
170 (let ((map (make-sparse-keymap "Foreground Color")))
171 (define-key map
"o" (cons "Other..." 'facemenu-set-foreground
))
173 "Menu keymap for foreground colors.")
175 (defalias 'facemenu-foreground-menu facemenu-foreground-menu
)
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
)
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
))
198 "Menu keymap for non-face text-properties.")
200 (defalias 'facemenu-special-menu facemenu-special-menu
)
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
))
211 "Submenu for text justification commands.")
213 (defalias 'facemenu-justification-menu facemenu-justification-menu
)
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
))
227 "Submenu for indentation commands.")
229 (defalias 'facemenu-indentation-menu facemenu-indentation-menu
)
231 ;; This is split up to avoid an overlong line in loaddefs.el.
233 (defvar facemenu-menu nil
234 "Facemenu top-level menu keymap.")
236 (setq facemenu-menu
(make-sparse-keymap "Text Properties"))
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 Text")
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 "--"))))
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)))
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))
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)
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
288 :type '(choice (const :tag "None" nil)
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)
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
315 ;; Add each defined face to the menu.
316 (facemenu-iterate 'facemenu-add-new-face
317 (facemenu-complete-face-list facemenu-keybindings)))
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
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))
339 (if (and mark-active (not current-prefix-arg))
341 (facemenu-add-new-face face)
342 (facemenu-add-face face start end))
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
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))
361 (if (and mark-active (not current-prefix-arg))
363 (unless (color-defined-p color)
364 (message "Color `%s' undefined" color))
365 (facemenu-add-new-face color 'facemenu-foreground-menu)
366 (facemenu-add-face (list (list :foreground color)) start end))
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
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))
385 (if (and mark-active (not current-prefix-arg))
387 (unless (color-defined-p color)
388 (message "Color `%s' undefined" color))
389 (facemenu-add-new-face color 'facemenu-background-menu)
390 (facemenu-add-face (list (list :background color)) start end))
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
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))
408 (if (and mark-active (not current-prefix-arg))
410 (barf-if-buffer-read-only)
411 (facemenu-get-face face)
413 (facemenu-add-face face start end)
414 (facemenu-add-face face)))
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'."
422 (add-text-properties start end '(invisible t)))
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'."
430 (add-text-properties start end '(intangible t)))
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'."
438 (add-text-properties start end '(read-only t)))
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))))
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)))
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 ;;; Describe-Text Mode.
466 (defun describe-text-done ()
467 "Delete the current window or bury the current buffer."
469 (if (> (count-windows) 1)
473 (defvar describe-text-mode-map
474 (let ((map (make-sparse-keymap)))
475 (set-keymap-parent map widget-keymap)
477 "Keymap for `describe-text-mode'.")
479 (defcustom describe-text-mode-hook nil
480 "List of hook functions ran by `describe-text-mode'."
483 (defun describe-text-mode ()
484 "Major mode for buffers created by `describe-text-at'.
486 \\{describe-text-mode-map}
487 Entry to this mode calls the value of `describe-text-mode-hook'
488 if that value is non-nil."
489 (kill-all-local-variables)
490 (setq major-mode 'describe-text-mode
491 mode-name "Describe-Text")
492 (use-local-map describe-text-mode-map)
494 (run-hooks 'describe-text-mode-hook))
496 ;;; Describe-Text Utilities.
498 (defun describe-text-widget (widget)
499 "Insert text to describe WIDGET in the current buffer."
501 :notify `(lambda (&rest ignore)
502 (widget-browse ',widget))
503 (format "%S" (if (symbolp widget)
507 (widget-create 'info-link :tag "widget" "(widget)Top"))
509 (defun describe-text-sexp (sexp)
510 "Insert a short description of SEXP in the current buffer."
511 (let ((pp (condition-case signal
513 (error (prin1-to-string signal)))))
514 (when (string-match "\n\\'" pp)
515 (setq pp (substring pp 0 (1- (length pp)))))
516 (if (cond ((string-match "\n" pp)
518 ((> (length pp) (- (window-width) (current-column)))
522 (widget-create 'push-button
524 :action (lambda (widget &optional event)
525 (with-output-to-temp-buffer
527 (princ (widget-get widget :value))))
531 (defun describe-text-properties (properties)
532 "Insert a description of PROPERTIES in the current buffer.
533 PROPERTIES should be a list of overlay or text properties.
534 The `category' property is made into a widget button that call
535 `describe-text-category' when pushed."
537 (widget-insert (format " %-20s " (car properties)))
538 (let ((key (nth 0 properties))
539 (value (nth 1 properties)))
540 (cond ((eq key 'category)
542 :notify `(lambda (&rest ignore)
543 (describe-text-category ',value))
544 (format "%S" value)))
546 (describe-text-widget value))
548 (describe-text-sexp value))))
550 (setq properties (cdr (cdr properties)))))
552 ;;; Describe-Text Commands.
554 (defun describe-text-category (category)
555 "Describe a text property category."
557 (when (get-buffer "*Text Category*")
558 (kill-buffer "*Text Category*"))
560 (with-output-to-temp-buffer "*Text Category*"
561 (set-buffer "*Text Category*")
562 (widget-insert "Category " (format "%S" category) ":\n\n")
563 (describe-text-properties (symbol-plist category))
565 (goto-char (point-min)))))
568 (defun describe-text-at (pos)
569 "Describe widgets, buttons, overlays and text properties at POS."
571 (when (eq (current-buffer) (get-buffer "*Text Description*"))
572 (error "Can't do self inspection"))
573 (let* ((properties (text-properties-at pos))
574 (overlays (overlays-at pos))
576 (wid-field (get-char-property pos 'field))
577 (wid-button (get-char-property pos 'button))
578 (wid-doc (get-char-property pos 'widget-doc))
579 ;; If button.el is not loaded, we have no buttons in the text.
580 (button (and (fboundp 'button-at) (button-at pos)))
581 (button-type (and button (button-type button)))
582 (button-label (and button (button-label button)))
583 (widget (or wid-field wid-button wid-doc)))
584 (if (not (or properties overlays))
585 (message "This is plain text.")
586 (when (get-buffer "*Text Description*")
587 (kill-buffer "*Text Description*"))
589 (with-output-to-temp-buffer "*Text Description*"
590 (set-buffer "*Text Description*")
591 (widget-insert "Text content at position " (format "%d" pos) ":\n\n")
593 (when (widgetp widget)
594 (widget-insert (cond (wid-field "This is an editable text area")
595 (wid-button "This is an active area")
596 (wid-doc "This is documentation text")))
597 (widget-insert " of a ")
598 (describe-text-widget widget)
599 (widget-insert ".\n\n"))
601 (when (and button (not (widgetp wid-button)))
602 (widget-insert "Here is a " (format "%S" button-type)
603 " button labeled `" button-label "'.\n\n"))
606 (if (eq (length overlays) 1)
607 (widget-insert "There is an overlay here:\n")
608 (widget-insert "There are " (format "%d" (length overlays))
609 " overlays here:\n"))
610 (dolist (overlay overlays)
611 (widget-insert " From " (format "%d" (overlay-start overlay))
612 " to " (format "%d" (overlay-end overlay)) "\n")
613 (describe-text-properties (overlay-properties overlay)))
614 (widget-insert "\n"))
617 (widget-insert "There are text properties here:\n")
618 (describe-text-properties properties))
620 (goto-char (point-min)))))))
622 ;;; List Text Properties
625 (defun list-text-properties-at (p)
626 "Pop up a buffer listing text-properties at LOCATION."
628 (let ((props (text-properties-at p))
633 (if (and (not (cdr (cdr props)))
634 (not (eq (car props) 'category))
635 (< (length (setq str (format "Text property at %d: %s %S"
636 p (car props) (car (cdr props)))))
639 (with-output-to-temp-buffer "*Text Properties*"
640 (princ (format "Text properties at %d:\n\n" p))
641 (setq help-xref-stack nil)
643 (if (eq (car props) 'category)
644 (setq category (car (cdr props))))
645 (princ (format "%-20s %S\n"
646 (car props) (car (cdr props))))
647 (setq props (cdr (cdr props))))
650 (setq props (symbol-plist category))
651 (princ (format "\nCategory %s:\n\n" category))
653 (princ (format "%-20s %S\n"
654 (car props) (car (cdr props))))
655 (if (eq (car props) 'category)
656 (setq category (car (cdr props))))
657 (setq props (cdr (cdr props)))))))))))
660 (defun facemenu-read-color (&optional prompt)
661 "Read a color using the minibuffer."
662 (let ((col (completing-read (or prompt "Color: ")
663 (or facemenu-color-alist
664 (mapcar 'list (defined-colors)))
671 (defun list-colors-display (&optional list)
672 "Display names of defined colors, and show what they look like.
673 If the optional argument LIST is non-nil, it should be a list of
674 colors to display. Otherwise, this command computes a list
675 of colors that the current display can handle."
677 (when (and (null list) (> (display-color-cells) 0))
678 (setq list (defined-colors))
679 ;; Delete duplicate colors.
682 (if (facemenu-color-equal (car l) (car (cdr l)))
683 (setcdr l (cdr (cdr l)))
685 ;; Don't show more than what the display can handle.
686 (let ((lc (nthcdr (1- (display-color-cells)) list)))
689 (with-output-to-temp-buffer "*Colors*"
691 (set-buffer standard-output)
697 (put-text-property s (point) 'face
698 (cons 'background-color (car list)))
700 (insert " " (car list) "\n")
701 (put-text-property s (point) 'face
702 (cons 'foreground-color (car list)))
703 (setq list (cdr list)))))))
705 (defun facemenu-color-equal (a b)
706 "Return t if colors A and B are the same color.
707 A and B should be strings naming colors.
708 This function queries the display system to find out what the color
709 names mean. It returns nil if the colors differ or if it can't
710 determine the correct answer."
711 (cond ((equal a b) t)
712 ((equal (color-values a) (color-values b)))))
714 (defun facemenu-add-face (face &optional start end)
715 "Add FACE to text between START and END.
716 If START is nil or START to END is empty, add FACE to next typed character
717 instead. For each section of that region that has a different face property,
718 FACE will be consed onto it, and other faces that are completely hidden by
719 that will be removed from the list.
720 If `facemenu-add-face-function' and maybe `facemenu-end-add-face' are non-nil,
721 they are used to set the face information.
723 As a special case, if FACE is `default', then the region is left with NO face
724 text property. Otherwise, selecting the default face would not have any
725 effect. See `facemenu-remove-face-function'."
726 (interactive "*xFace: \nr")
727 (if (and (eq face 'default)
728 (not (eq facemenu-remove-face-function t)))
729 (if facemenu-remove-face-function
730 (funcall facemenu-remove-face-function start end)
731 (if (and start (< start end))
732 (remove-text-properties start end '(face default))
733 (setq self-insert-face 'default
734 self-insert-face-command this-command)))
735 (if facemenu-add-face-function
737 (if end (goto-char end))
739 (if start (goto-char start))
740 (insert-before-markers
741 (funcall facemenu-add-face-function face end)))
742 (if facemenu-end-add-face
743 (insert (if (stringp facemenu-end-add-face)
744 facemenu-end-add-face
745 (funcall facemenu-end-add-face face)))))
746 (if (and start (< start end))
747 (let ((part-start start) part-end)
748 (while (not (= part-start end))
749 (setq part-end (next-single-property-change part-start 'face
751 (let ((prev (get-text-property part-start 'face)))
752 (put-text-property part-start part-end 'face
755 (facemenu-active-faces
760 (setq part-start part-end)))
761 (setq self-insert-face (if (eq last-command self-insert-face-command)
762 (cons face (if (listp self-insert-face)
764 (list self-insert-face)))
766 self-insert-face-command this-command)))))
768 (defun facemenu-active-faces (face-list &optional frame)
769 "Return from FACE-LIST those faces that would be used for display.
770 This means each face attribute is not specified in a face earlier in FACE-LIST
771 and such a face is therefore active when used to display text.
772 If the optional argument FRAME is given, use the faces in that frame; otherwise
773 use the selected frame. If t, then the global, non-frame faces are used."
774 (let* ((mask-atts (copy-sequence
775 (if (consp (car face-list))
776 (face-attributes-as-vector (car face-list))
777 (or (internal-lisp-face-p (car face-list) frame)
778 (check-face (car face-list))))))
779 (active-list (list (car face-list)))
780 (face-list (cdr face-list))
781 (mask-len (length mask-atts)))
784 (if (consp (car face-list))
785 (face-attributes-as-vector (car face-list))
786 (or (internal-lisp-face-p (car face-list) frame)
787 (check-face (car face-list)))))
790 (while (> (setq i (1- i)) 1)
791 (and (not (memq (aref face-atts i) '(nil unspecified)))
792 (memq (aref mask-atts i) '(nil unspecified))
793 (aset mask-atts i (setq useful t))))
795 (setq active-list (cons (car face-list) active-list)))
796 (setq face-list (cdr face-list)))
797 (nreverse active-list)))
799 (defun facemenu-get-face (symbol)
800 "Make sure FACE exists.
801 If not, create it and add it to the appropriate menu. Return the SYMBOL."
802 (let ((name (symbol-name symbol)))
803 (cond ((facep symbol))
804 (t (make-face symbol))))
807 (defun facemenu-add-new-face (face-or-color &optional menu)
808 "Add FACE-OR-COLOR (a face or a color) to the appropriate Face menu.
809 If MENU is nil, then FACE-OR-COLOR is a face to be added
810 to `facemenu-face-menu'. If MENU is `facemenu-foreground-menu'
811 or `facemenu-background-menu', FACE-OR-COLOR is a color
812 to be added to the specified menu.
814 This is called whenever you create a new face."
818 (key (cdr (assoc face-or-color facemenu-keybindings)))
820 (if (symbolp face-or-color)
821 (setq name (symbol-name face-or-color)
822 symbol face-or-color)
823 (setq name face-or-color
824 symbol (intern name)))
825 (cond ((eq menu 'facemenu-foreground-menu)
827 (format "Select foreground color %s for subsequent insertion."
829 ((eq menu 'facemenu-background-menu)
831 (format "Select background color %s for subsequent insertion."
834 (setq menu 'facemenu-face-menu)
836 (format "Select face `%s' for subsequent insertion."
838 (cond ((eq t facemenu-unlisted-faces))
839 ((memq symbol facemenu-unlisted-faces))
840 ;; test against regexps in facemenu-unlisted-faces
841 ((let ((unlisted facemenu-unlisted-faces)
843 (while (and unlisted (not matched))
844 (if (and (stringp (car unlisted))
845 (string-match (car unlisted) name))
847 (setq unlisted (cdr unlisted))))
849 (key ; has a keyboard equivalent. These go at the front.
850 (setq function (intern (concat "facemenu-set-" name)))
857 (if (and mark-active (not current-prefix-arg))
859 (if (and mark-active (not current-prefix-arg))
861 (define-key 'facemenu-keymap key (cons name function))
862 (define-key menu key (cons name function)))
863 ((facemenu-iterate ; check if equivalent face is already in the menu
864 (lambda (m) (and (listp m)
866 (face-equal (car m) symbol)))
867 (cdr (symbol-function menu))))
868 (t ; No keyboard equivalent. Figure out where to put it:
869 (setq key (vector symbol)
870 function 'facemenu-set-face-from-menu
871 menu-val (symbol-function menu))
872 (if (and facemenu-new-faces-at-end
873 (> (length menu-val) 3))
874 (define-key-after menu-val key (cons name function)
875 (car (nth (- (length menu-val) 3) menu-val)))
876 (define-key menu key (cons name function))))))
877 nil) ; Return nil for facemenu-iterate
879 (defun facemenu-complete-face-list (&optional oldlist)
880 "Return list of all faces that look different.
881 Starts with given ALIST of faces, and adds elements only if they display
882 differently from any face already on the list.
883 The faces on ALIST will end up at the end of the returned list, in reverse
885 (let ((list (nreverse (mapcar 'car oldlist))))
888 (if (not (memq new-face list))
889 (setq list (cons new-face list)))
891 (nreverse (face-list)))
894 (defun facemenu-iterate (func list)
895 "Apply FUNC to each element of LIST until one returns non-nil.
896 Returns the non-nil value it found, or nil if all were nil."
897 (while (and list (not (funcall func (car list))))
898 (setq list (cdr list)))
903 ;;; facemenu.el ends here