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