Make `checkout-model' apply to filesets.
[emacs.git] / lisp / facemenu.el
blob760f53a2ed846a3c62231005648d42f6b4ee8acf
1 ;;; facemenu.el --- create a face menu for interactively adding fonts to text
3 ;; Copyright (C) 1994, 1995, 1996, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: Boris Goldowsky <boris@gnu.org>
7 ;; Keywords: faces
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 3, or (at your option)
14 ;; any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
26 ;;; Commentary:
28 ;; This file defines a menu of faces (bold, italic, etc) which allows you to
29 ;; set the face used for a region of the buffer. Some faces also have
30 ;; keybindings, which are shown in the menu.
32 ;; The menu also contains submenus for indentation and justification-changing
33 ;; commands.
35 ;;; Usage:
36 ;; Selecting a face from the menu or typing the keyboard equivalent will
37 ;; change the region to use that face. If you use transient-mark-mode and the
38 ;; region is not active, the face will be remembered and used for the next
39 ;; insertion. It will be forgotten if you move point or make other
40 ;; modifications before inserting or typing anything.
42 ;; Faces can be selected from the keyboard as well.
43 ;; The standard keybindings are M-o (or ESC o) + letter:
44 ;; M-o i = "set italic", M-o b = "set bold", etc.
46 ;;; Customization:
47 ;; An alternative set of keybindings that may be easier to type can be set up
48 ;; using "Alt" or "Hyper" keys. This requires that you either have or create
49 ;; an Alt or Hyper key on your keyboard. On my keyboard, there is a key
50 ;; labeled "Alt", but to make it act as an Alt key I have to put this command
51 ;; into my .xinitrc:
52 ;; xmodmap -e "add Mod3 = Alt_L"
53 ;; Or, I can make it into a Hyper key with this:
54 ;; xmodmap -e "keysym Alt_L = Hyper_L" -e "add Mod2 = Hyper_L"
55 ;; Check with local X-perts for how to do it on your system.
56 ;; Then you can define your keybindings with code like this in your .emacs:
57 ;; (setq facemenu-keybindings
58 ;; '((default . [?\H-d])
59 ;; (bold . [?\H-b])
60 ;; (italic . [?\H-i])
61 ;; (bold-italic . [?\H-l])
62 ;; (underline . [?\H-u])))
63 ;; (facemenu-update)
64 ;; (setq facemenu-keymap global-map)
65 ;; (define-key global-map [?\H-c] 'facemenu-set-foreground) ; set fg color
66 ;; (define-key global-map [?\H-C] 'facemenu-set-background) ; set bg color
68 ;; The order of the faces that appear in the menu and their keybindings can be
69 ;; controlled by setting the variables `facemenu-keybindings' and
70 ;; `facemenu-new-faces-at-end'. List faces that you want to use in documents
71 ;; in `facemenu-listed-faces'.
73 ;;; Known Problems:
74 ;; Bold and Italic do not combine to create bold-italic if you select them
75 ;; both, although most other combinations (eg bold + underline + some color)
76 ;; do the intuitive thing.
78 ;; There is at present no way to display what the faces look like in
79 ;; the menu itself.
81 ;; `list-faces-display' shows the faces in a different order than
82 ;; this menu, which could be confusing. I do /not/ sort the list
83 ;; alphabetically, because I like the default order: it puts the most
84 ;; basic, common fonts first.
86 ;; Please send me any other problems, comments or ideas.
88 ;;; Code:
90 (eval-when-compile
91 (require 'help)
92 (require 'button))
94 ;; Global bindings:
95 (define-key global-map [C-down-mouse-2] 'facemenu-menu)
96 (define-key global-map "\M-o" 'facemenu-keymap)
98 (defgroup facemenu nil
99 "Create a face menu for interactively adding fonts to text."
100 :group 'faces
101 :prefix "facemenu-")
103 (defcustom facemenu-keybindings
104 '((default . "d")
105 (bold . "b")
106 (italic . "i")
107 (bold-italic . "l") ; {bold} intersect {italic} = {l}
108 (underline . "u"))
109 "Alist of interesting faces and keybindings.
110 Each element is itself a list: the car is the name of the face,
111 the next element is the key to use as a keyboard equivalent of the menu item;
112 the binding is made in `facemenu-keymap'.
114 The faces specifically mentioned in this list are put at the top of
115 the menu, in the order specified. All other faces which are defined
116 in `facemenu-listed-faces' are listed after them, but get no
117 keyboard equivalents.
119 If you change this variable after loading facemenu.el, you will need to call
120 `facemenu-update' to make it take effect."
121 :type '(repeat (cons face string))
122 :group 'facemenu)
124 (defcustom facemenu-new-faces-at-end t
125 "*Where in the menu to insert newly-created faces.
126 This should be nil to put them at the top of the menu, or t to put them
127 just before \"Other\" at the end."
128 :type 'boolean
129 :group 'facemenu)
131 (defvar facemenu-unlisted-faces
132 `(modeline region secondary-selection highlight scratch-face
133 ,(purecopy "^font-lock-") ,(purecopy "^gnus-") ,(purecopy "^message-")
134 ,(purecopy "^ediff-") ,(purecopy "^term-") ,(purecopy "^vc-")
135 ,(purecopy "^widget-") ,(purecopy "^custom-") ,(purecopy "^vm-"))
136 "*List of faces that are of no interest to the user.")
137 (make-obsolete-variable 'facemenu-unlisted-faces 'facemenu-listed-faces
138 "22.1,\n and has no effect on the Face menu")
140 (defcustom facemenu-listed-faces nil
141 "*List of faces to include in the Face menu.
142 Each element should be a symbol, the name of a face.
143 The \"basic \" faces in `facemenu-keybindings' are automatically
144 added to the Face menu, and need not be in this list.
146 This value takes effect when you load facemenu.el. If the
147 list includes symbols which are not defined as faces, they
148 are ignored; however, subsequently defining or creating
149 those faces adds them to the menu then. You can call
150 `facemenu-update' to recalculate the menu contents, such as
151 if you change the value of this variable,
153 If this variable is t, all faces that you apply to text
154 using the face menu commands (even by name), and all faces
155 that you define or create, are added to the menu. You may
156 find it useful to set this variable to t temporarily while
157 you define some faces, so that they will be added. However,
158 if the value is no longer t and you call `facemenu-update',
159 it will remove any faces not explicitly in the list."
160 :type '(choice (const :tag "List all faces" t)
161 (const :tag "None" nil)
162 (repeat symbol))
163 :group 'facemenu
164 :version "22.1")
166 (defvar facemenu-face-menu
167 (let ((map (make-sparse-keymap "Face")))
168 (define-key map "o" (cons "Other..." 'facemenu-set-face))
169 map)
170 "Menu keymap for faces.")
171 (defalias 'facemenu-face-menu facemenu-face-menu)
172 (put 'facemenu-face-menu 'menu-enable '(facemenu-enable-faces-p))
174 (defvar facemenu-foreground-menu
175 (let ((map (make-sparse-keymap "Foreground Color")))
176 (define-key map "o" (cons "Other..." 'facemenu-set-foreground))
177 map)
178 "Menu keymap for foreground colors.")
179 (defalias 'facemenu-foreground-menu facemenu-foreground-menu)
180 (put 'facemenu-foreground-menu 'menu-enable '(facemenu-enable-faces-p))
182 (defvar facemenu-background-menu
183 (let ((map (make-sparse-keymap "Background Color")))
184 (define-key map "o" (cons "Other..." 'facemenu-set-background))
185 map)
186 "Menu keymap for background colors.")
187 (defalias 'facemenu-background-menu facemenu-background-menu)
188 (put 'facemenu-background-menu 'menu-enable '(facemenu-enable-faces-p))
190 ;;; Condition for enabling menu items that set faces.
191 (defun facemenu-enable-faces-p ()
192 (not (and font-lock-mode font-lock-defaults)))
194 (defvar facemenu-special-menu
195 (let ((map (make-sparse-keymap "Special")))
196 (define-key map [?s] (cons (purecopy "Remove Special")
197 'facemenu-remove-special))
198 (define-key map [?t] (cons (purecopy "Intangible")
199 'facemenu-set-intangible))
200 (define-key map [?v] (cons (purecopy "Invisible")
201 'facemenu-set-invisible))
202 (define-key map [?r] (cons (purecopy "Read-Only")
203 'facemenu-set-read-only))
204 map)
205 "Menu keymap for non-face text-properties.")
206 (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))
215 map)
216 "Submenu for text justification commands.")
217 (defalias 'facemenu-justification-menu facemenu-justification-menu)
219 (defvar facemenu-indentation-menu
220 (let ((map (make-sparse-keymap "Indentation")))
221 (define-key map [decrease-right-margin]
222 (cons (purecopy "Indent Right Less") 'decrease-right-margin))
223 (define-key map [increase-right-margin]
224 (cons (purecopy "Indent Right More") 'increase-right-margin))
225 (define-key map [decrease-left-margin]
226 (cons (purecopy "Indent Less") 'decrease-left-margin))
227 (define-key map [increase-left-margin]
228 (cons (purecopy "Indent More") 'increase-left-margin))
229 map)
230 "Submenu for indentation commands.")
231 (defalias 'facemenu-indentation-menu facemenu-indentation-menu)
233 ;; This is split up to avoid an overlong line in loaddefs.el.
234 (defvar facemenu-menu nil
235 "Facemenu top-level menu keymap.")
236 (setq facemenu-menu (make-sparse-keymap "Text Properties"))
237 (let ((map facemenu-menu))
238 (define-key map [dc] (cons (purecopy "Display Colors") 'list-colors-display))
239 (define-key map [df] (cons (purecopy "Display Faces") 'list-faces-display))
240 (define-key map [dp] (cons (purecopy "Describe Properties")
241 'describe-text-properties))
242 (define-key map [ra] (cons (purecopy "Remove Text Properties")
243 'facemenu-remove-all))
244 (define-key map [rm] (cons (purecopy "Remove Face Properties")
245 'facemenu-remove-face-props))
246 (define-key map [s1] (list (purecopy "--"))))
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 (defalias 'facemenu-menu facemenu-menu)
263 (defvar facemenu-keymap
264 (let ((map (make-sparse-keymap "Set face")))
265 (define-key map "o" (cons (purecopy "Other...") 'facemenu-set-face))
266 (define-key map "\M-o" 'font-lock-fontify-block)
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 "Alist of colors, used for completion.
304 If this is nil, then the value of (defined-colors) is used.")
306 (defun facemenu-update ()
307 "Add or update the \"Face\" menu in the menu bar.
308 You can call this to update things if you change any of the menu configuration
309 variables."
310 (interactive)
312 ;; Add each defined face to the menu.
313 (facemenu-iterate 'facemenu-add-new-face
314 (facemenu-complete-face-list facemenu-keybindings)))
316 (defun facemenu-set-face (face &optional start end)
317 "Apply FACE to the region or next character typed.
319 If the region is active (normally true except in Transient
320 Mark mode) and nonempty, and there is no prefix argument,
321 this command applies FACE to the region. Otherwise, it applies FACE
322 to the faces to use for the next character
323 inserted. (Moving point or switching buffers before typing
324 a character to insert cancels the specification.)
326 If FACE is `default', to \"apply\" it means clearing
327 the list of faces to be used. For any other value of FACE,
328 to \"apply\" it means putting FACE at the front of the list
329 of faces to be used, and removing any faces further
330 along in the list that would be completely overridden by
331 preceding faces (including FACE).
333 This command can also add FACE to the menu of faces,
334 if `facemenu-listed-faces' says to do that."
335 (interactive (list (progn
336 (barf-if-buffer-read-only)
337 (read-face-name "Use face"))
338 (if (and mark-active (not current-prefix-arg))
339 (region-beginning))
340 (if (and mark-active (not current-prefix-arg))
341 (region-end))))
342 (facemenu-add-new-face face)
343 (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
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 (facemenu-set-face-from-menu
364 (facemenu-add-new-color color 'facemenu-foreground-menu)
365 start end))
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 (facemenu-set-face-from-menu
386 (facemenu-add-new-color color 'facemenu-background-menu)
387 start end))
389 (defun facemenu-set-face-from-menu (face start end)
390 "Set the FACE of the region or next character typed.
391 This function is designed to be called from a menu; FACE is determined
392 using the event type of the menu entry. If FACE is a symbol whose
393 name starts with \"fg:\" or \"bg:\", then this functions sets the
394 foreground or background to the color specified by the rest of the
395 symbol's name. Any other symbol is considered the name of a face.
397 If the region is active (normally true except in Transient Mark mode)
398 and there is no prefix argument, this command sets the region to the
399 requested face.
401 Otherwise, this command specifies the face for the next character
402 inserted. Moving point or switching buffers before typing a character
403 to insert cancels the specification."
404 (interactive (list last-command-event
405 (if (and mark-active (not current-prefix-arg))
406 (region-beginning))
407 (if (and mark-active (not current-prefix-arg))
408 (region-end))))
409 (barf-if-buffer-read-only)
410 (facemenu-add-face
411 (let ((fn (symbol-name face)))
412 (if (string-match "\\`\\([fb]\\)g:\\(.+\\)" fn)
413 (list (list (if (string= (match-string 1 fn) "f")
414 :foreground
415 :background)
416 (match-string 2 fn)))
417 face))
418 start end))
420 (defun facemenu-set-invisible (start end)
421 "Make the region invisible.
422 This sets the `invisible' text property; it can be undone with
423 `facemenu-remove-special'."
424 (interactive "r")
425 (add-text-properties start end '(invisible t)))
427 (defun facemenu-set-intangible (start end)
428 "Make the region intangible: disallow moving into it.
429 This sets the `intangible' text property; it can be undone with
430 `facemenu-remove-special'."
431 (interactive "r")
432 (add-text-properties start end '(intangible t)))
434 (defun facemenu-set-read-only (start end)
435 "Make the region unmodifiable.
436 This sets the `read-only' text property; it can be undone with
437 `facemenu-remove-special'."
438 (interactive "r")
439 (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))))
448 (defun facemenu-remove-all (start end)
449 "Remove all text properties from the region."
450 (interactive "*r") ; error if buffer is read-only despite the next line.
451 (let ((inhibit-read-only t))
452 (set-text-properties start end nil)))
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 (defun facemenu-read-color (&optional prompt)
463 "Read a color using the minibuffer."
464 (let* ((completion-ignore-case t)
465 (col (completing-read (or prompt "Color: ")
466 (or facemenu-color-alist
467 (defined-colors))
468 nil t)))
469 (if (equal "" col)
471 col)))
473 (defun list-colors-display (&optional list buffer-name)
474 "Display names of defined colors, and show what they look like.
475 If the optional argument LIST is non-nil, it should be a list of
476 colors to display. Otherwise, this command computes a list of
477 colors that the current display can handle. If the optional
478 argument BUFFER-NAME is nil, it defaults to *Colors*."
479 (interactive)
480 (when (and (null list) (> (display-color-cells) 0))
481 (setq list (list-colors-duplicates (defined-colors)))
482 (when (memq (display-visual-class) '(gray-scale pseudo-color direct-color))
483 ;; Don't show more than what the display can handle.
484 (let ((lc (nthcdr (1- (display-color-cells)) list)))
485 (if lc
486 (setcdr lc nil)))))
487 (with-help-window (or buffer-name "*Colors*")
488 (save-excursion
489 (set-buffer standard-output)
490 (setq truncate-lines t)
491 (if temp-buffer-show-function
492 (list-colors-print list)
493 ;; Call list-colors-print from temp-buffer-show-hook
494 ;; to get the right value of window-width in list-colors-print
495 ;; after the buffer is displayed.
496 (add-hook 'temp-buffer-show-hook
497 (lambda () (list-colors-print list)) nil t)))))
499 (defun list-colors-print (list)
500 (dolist (color list)
501 (if (consp color)
502 (if (cdr color)
503 (setq color (sort color (lambda (a b)
504 (string< (downcase a)
505 (downcase b))))))
506 (setq color (list color)))
507 (put-text-property
508 (prog1 (point)
509 (insert (car color))
510 (indent-to 22))
511 (point)
512 'face (list ':background (car color)))
513 (put-text-property
514 (prog1 (point)
515 (insert " " (if (cdr color)
516 (mapconcat 'identity (cdr color) ", ")
517 (car color))))
518 (point)
519 'face (list ':foreground (car color)))
520 (indent-to (max (- (window-width) 8) 44))
521 (insert (apply 'format "#%02x%02x%02x"
522 (mapcar (lambda (c) (lsh c -8))
523 (color-values (car color)))))
525 (insert "\n"))
526 (goto-char (point-min)))
528 (defun list-colors-duplicates (&optional list)
529 "Return a list of colors with grouped duplicate colors.
530 If a color has no duplicates, then the element of the returned list
531 has the form '(COLOR-NAME). The element of the returned list with
532 duplicate colors has the form '(COLOR-NAME DUPLICATE-COLOR-NAME ...).
533 This function uses the predicate `facemenu-color-equal' to compare
534 color names. If the optional argument LIST is non-nil, it should
535 be a list of colors to display. Otherwise, this function uses
536 a list of colors that the current display can handle."
537 (let* ((list (mapcar 'list (or list (defined-colors))))
538 (l list))
539 (while (cdr l)
540 (if (and (facemenu-color-equal (car (car l)) (car (car (cdr l))))
541 (not (if (boundp 'w32-default-color-map)
542 (not (assoc (car (car l)) w32-default-color-map)))))
543 (progn
544 (setcdr (car l) (cons (car (car (cdr l))) (cdr (car l))))
545 (setcdr l (cdr (cdr l))))
546 (setq l (cdr l))))
547 list))
549 (defun facemenu-color-equal (a b)
550 "Return t if colors A and B are the same color.
551 A and B should be strings naming colors.
552 This function queries the display system to find out what the color
553 names mean. It returns nil if the colors differ or if it can't
554 determine the correct answer."
555 (cond ((equal a b) t)
556 ((equal (color-values a) (color-values b)))))
558 (defun facemenu-add-face (face &optional start end)
559 "Add FACE to text between START and END.
560 If START is nil or START to END is empty, add FACE to next typed character
561 instead. For each section of that region that has a different face property,
562 FACE will be consed onto it, and other faces that are completely hidden by
563 that will be removed from the list.
564 If `facemenu-add-face-function' and maybe `facemenu-end-add-face' are non-nil,
565 they are used to set the face information.
567 As a special case, if FACE is `default', then the region is left with NO face
568 text property. Otherwise, selecting the default face would not have any
569 effect. See `facemenu-remove-face-function'."
570 (interactive "*xFace: \nr")
571 (if (and (eq face 'default)
572 (not (eq facemenu-remove-face-function t)))
573 (if facemenu-remove-face-function
574 (funcall facemenu-remove-face-function start end)
575 (if (and start (< start end))
576 (remove-text-properties start end '(face default))
577 (setq self-insert-face 'default
578 self-insert-face-command this-command)))
579 (if facemenu-add-face-function
580 (save-excursion
581 (if end (goto-char end))
582 (save-excursion
583 (if start (goto-char start))
584 (insert-before-markers
585 (funcall facemenu-add-face-function face end)))
586 (if facemenu-end-add-face
587 (insert (if (stringp facemenu-end-add-face)
588 facemenu-end-add-face
589 (funcall facemenu-end-add-face face)))))
590 (if (and start (< start end))
591 (let ((part-start start) part-end)
592 (while (not (= part-start end))
593 (setq part-end (next-single-property-change part-start 'face
594 nil end))
595 (let ((prev (get-text-property part-start 'face)))
596 (put-text-property part-start part-end 'face
597 (if (null prev)
598 face
599 (facemenu-active-faces
600 (cons face
601 (if (listp prev)
602 prev
603 (list prev)))
604 ;; Specify the selected frame
605 ;; because nil would mean to use
606 ;; the new-frame default settings,
607 ;; and those are usually nil.
608 (selected-frame)))))
609 (setq part-start part-end)))
610 (setq self-insert-face (if (eq last-command self-insert-face-command)
611 (cons face (if (listp self-insert-face)
612 self-insert-face
613 (list self-insert-face)))
614 face)
615 self-insert-face-command this-command))))
616 (unless (facemenu-enable-faces-p)
617 (message "Font-lock mode will override any faces you set in this buffer")))
619 (defun facemenu-active-faces (face-list &optional frame)
620 "Return from FACE-LIST those faces that would be used for display.
621 This means each face attribute is not specified in a face earlier in FACE-LIST
622 and such a face is therefore active when used to display text.
623 If the optional argument FRAME is given, use the faces in that frame; otherwise
624 use the selected frame. If t, then the global, non-frame faces are used."
625 (let* ((mask-atts (copy-sequence
626 (if (consp (car face-list))
627 (face-attributes-as-vector (car face-list))
628 (or (internal-lisp-face-p (car face-list) frame)
629 (check-face (car face-list))))))
630 (active-list (list (car face-list)))
631 (face-list (cdr face-list))
632 (mask-len (length mask-atts)))
633 (while face-list
634 (if (let ((face-atts
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 (i mask-len)
640 (useful nil))
641 (while (>= (setq i (1- i)) 0)
642 (and (not (memq (aref face-atts i) '(nil unspecified)))
643 (memq (aref mask-atts i) '(nil unspecified))
644 (aset mask-atts i (setq useful t))))
645 useful)
646 (setq active-list (cons (car face-list) active-list)))
647 (setq face-list (cdr face-list)))
648 (nreverse active-list)))
650 (defun facemenu-add-new-face (face)
651 "Add FACE (a face) to the Face menu if `facemenu-listed-faces' says so.
652 This is called whenever you create a new face, and at other times."
653 (let* (name
654 symbol
655 menu docstring
656 (key (cdr (assoc face facemenu-keybindings)))
657 function menu-val)
658 (if (symbolp face)
659 (setq name (symbol-name face)
660 symbol face)
661 (setq name face
662 symbol (intern name)))
663 (setq menu 'facemenu-face-menu)
664 (setq docstring
665 (format "Select face `%s' for subsequent insertion.
666 If the mark is active and there is no prefix argument,
667 apply face `%s' to the region instead.
668 This command was defined by `facemenu-add-new-face'."
669 name name))
670 (cond ((facemenu-iterate ; check if equivalent face is already in the menu
671 (lambda (m) (and (listp m)
672 (symbolp (car m))
673 ;; Avoid error in face-equal
674 ;; when a non-face is erroneously present.
675 (facep (car m))
676 (face-equal (car m) symbol)))
677 (cdr (symbol-function menu))))
678 ;; Faces with a keyboard equivalent. These go at the front.
679 (key
680 (setq function (intern (concat "facemenu-set-" name)))
681 (fset function
682 `(lambda ()
683 ,docstring
684 (interactive)
685 (facemenu-set-face
686 (quote ,symbol)
687 (if (and mark-active (not current-prefix-arg))
688 (region-beginning))
689 (if (and mark-active (not current-prefix-arg))
690 (region-end)))))
691 (define-key 'facemenu-keymap key (cons name function))
692 (define-key menu key (cons name function)))
693 ;; Faces with no keyboard equivalent. Figure out where to put it:
694 ((or (eq t facemenu-listed-faces)
695 (memq symbol facemenu-listed-faces))
696 (setq key (vector symbol)
697 function 'facemenu-set-face-from-menu
698 menu-val (symbol-function menu))
699 (if (and facemenu-new-faces-at-end
700 (> (length menu-val) 3))
701 (define-key-after menu-val key (cons name function)
702 (car (nth (- (length menu-val) 3) menu-val)))
703 (define-key menu key (cons name function))))))
704 nil) ; Return nil for facemenu-iterate
706 (defun facemenu-add-new-color (color menu)
707 "Add COLOR (a color name string) to the appropriate Face menu.
708 MENU should be `facemenu-foreground-menu' or `facemenu-background-menu'.
709 Return the event type (a symbol) of the added menu entry.
711 This is called whenever you use a new color."
712 (let (symbol docstring)
713 (unless (color-defined-p color)
714 (error "Color `%s' undefined" color))
715 (cond ((eq menu 'facemenu-foreground-menu)
716 (setq docstring
717 (format "Select foreground color %s for subsequent insertion."
718 color)
719 symbol (intern (concat "fg:" color))))
720 ((eq menu 'facemenu-background-menu)
721 (setq docstring
722 (format "Select background color %s for subsequent insertion."
723 color)
724 symbol (intern (concat "bg:" color))))
725 (t (error "MENU should be `facemenu-foreground-menu' or `facemenu-background-menu'")))
726 (unless (facemenu-iterate ; Check if color is already in the menu.
727 (lambda (m) (and (listp m)
728 (eq (car m) symbol)))
729 (cdr (symbol-function menu)))
730 ;; Color is not in the menu. Figure out where to put it.
731 (let ((key (vector symbol))
732 (function 'facemenu-set-face-from-menu)
733 (menu-val (symbol-function menu)))
734 (if (and facemenu-new-faces-at-end
735 (> (length menu-val) 3))
736 (define-key-after menu-val key (cons color function)
737 (car (nth (- (length menu-val) 3) menu-val)))
738 (define-key menu key (cons color function)))))
739 symbol))
741 (defun facemenu-complete-face-list (&optional oldlist)
742 "Return list of all faces that look different.
743 Starts with given ALIST of faces, and adds elements only if they display
744 differently from any face already on the list.
745 The faces on ALIST will end up at the end of the returned list, in reverse
746 order."
747 (let ((list (nreverse (mapcar 'car oldlist))))
748 (facemenu-iterate
749 (lambda (new-face)
750 (if (not (memq new-face list))
751 (setq list (cons new-face list)))
752 nil)
753 (nreverse (face-list)))
754 list))
756 (defun facemenu-iterate (func list)
757 "Apply FUNC to each element of LIST until one returns non-nil.
758 Returns the non-nil value it found, or nil if all were nil."
759 (while (and list (not (funcall func (car list))))
760 (setq list (cdr list)))
761 (car list))
763 (facemenu-update)
765 (provide 'facemenu)
767 ;; arch-tag: 85f6d02b-9085-420e-b651-0678f0e9c7eb
768 ;;; facemenu.el ends here