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