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