]> code.delx.au - gnu-emacs/blob - lisp/facemenu.el
(compose-string, encode-composition-rule, compose-last-chars):
[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, 2005 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., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, 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 (put 'facemenu-face-menu 'menu-enable '(facemenu-enable-faces-p))
166
167 ;;;###autoload
168 (defvar facemenu-foreground-menu
169 (let ((map (make-sparse-keymap "Foreground Color")))
170 (define-key map "o" (cons "Other..." 'facemenu-set-foreground))
171 map)
172 "Menu keymap for foreground colors.")
173 ;;;###autoload
174 (defalias 'facemenu-foreground-menu facemenu-foreground-menu)
175 (put 'facemenu-foreground-menu 'menu-enable '(facemenu-enable-faces-p))
176
177 ;;;###autoload
178 (defvar facemenu-background-menu
179 (let ((map (make-sparse-keymap "Background Color")))
180 (define-key map "o" (cons "Other..." 'facemenu-set-background))
181 map)
182 "Menu keymap for background colors.")
183 ;;;###autoload
184 (defalias 'facemenu-background-menu facemenu-background-menu)
185 (put 'facemenu-background-menu 'menu-enable '(facemenu-enable-faces-p))
186
187 ;;; Condition for enabling menu items that set faces.
188 (defun facemenu-enable-faces-p ()
189 (not (and font-lock-mode font-lock-defaults)))
190
191 ;;;###autoload
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 ;;;###autoload
205 (defalias 'facemenu-special-menu facemenu-special-menu)
206
207 ;;;###autoload
208 (defvar facemenu-justification-menu
209 (let ((map (make-sparse-keymap "Justification")))
210 (define-key map [?c] (cons (purecopy "Center") 'set-justification-center))
211 (define-key map [?b] (cons (purecopy "Full") 'set-justification-full))
212 (define-key map [?r] (cons (purecopy "Right") 'set-justification-right))
213 (define-key map [?l] (cons (purecopy "Left") 'set-justification-left))
214 (define-key map [?u] (cons (purecopy "Unfilled") 'set-justification-none))
215 map)
216 "Submenu for text justification commands.")
217 ;;;###autoload
218 (defalias 'facemenu-justification-menu facemenu-justification-menu)
219
220 ;;;###autoload
221 (defvar facemenu-indentation-menu
222 (let ((map (make-sparse-keymap "Indentation")))
223 (define-key map [decrease-right-margin]
224 (cons (purecopy "Indent Right Less") 'decrease-right-margin))
225 (define-key map [increase-right-margin]
226 (cons (purecopy "Indent Right More") 'increase-right-margin))
227 (define-key map [decrease-left-margin]
228 (cons (purecopy "Indent Less") 'decrease-left-margin))
229 (define-key map [increase-left-margin]
230 (cons (purecopy "Indent More") 'increase-left-margin))
231 map)
232 "Submenu for indentation commands.")
233 ;;;###autoload
234 (defalias 'facemenu-indentation-menu facemenu-indentation-menu)
235
236 ;; This is split up to avoid an overlong line in loaddefs.el.
237 ;;;###autoload
238 (defvar facemenu-menu nil
239 "Facemenu top-level menu keymap.")
240 ;;;###autoload
241 (setq facemenu-menu (make-sparse-keymap "Text Properties"))
242 ;;;###autoload
243 (let ((map facemenu-menu))
244 (define-key map [dc] (cons (purecopy "Display Colors") 'list-colors-display))
245 (define-key map [df] (cons (purecopy "Display Faces") 'list-faces-display))
246 (define-key map [dp] (cons (purecopy "Describe Properties")
247 'describe-text-properties))
248 (define-key map [ra] (cons (purecopy "Remove Text Properties")
249 'facemenu-remove-all))
250 (define-key map [rm] (cons (purecopy "Remove Face Properties")
251 'facemenu-remove-face-props))
252 (define-key map [s1] (list (purecopy "--"))))
253 ;;;###autoload
254 (let ((map facemenu-menu))
255 (define-key map [in] (cons (purecopy "Indentation")
256 'facemenu-indentation-menu))
257 (define-key map [ju] (cons (purecopy "Justification")
258 'facemenu-justification-menu))
259 (define-key map [s2] (list (purecopy "--")))
260 (define-key map [sp] (cons (purecopy "Special Properties")
261 'facemenu-special-menu))
262 (define-key map [bg] (cons (purecopy "Background Color")
263 'facemenu-background-menu))
264 (define-key map [fg] (cons (purecopy "Foreground Color")
265 'facemenu-foreground-menu))
266 (define-key map [fc] (cons (purecopy "Face")
267 'facemenu-face-menu)))
268 ;;;###autoload
269 (defalias 'facemenu-menu facemenu-menu)
270
271 (defvar facemenu-keymap
272 (let ((map (make-sparse-keymap "Set face")))
273 (define-key map "o" (cons (purecopy "Other...") 'facemenu-set-face))
274 map)
275 "Keymap for face-changing commands.
276 `Facemenu-update' fills in the keymap according to the bindings
277 requested in `facemenu-keybindings'.")
278 (defalias 'facemenu-keymap facemenu-keymap)
279
280
281 (defcustom facemenu-add-face-function nil
282 "Function called at beginning of text to change or nil.
283 This function is passed the FACE to set and END of text to change, and must
284 return a string which is inserted. It may set `facemenu-end-add-face'."
285 :type '(choice (const :tag "None" nil)
286 function)
287 :group 'facemenu)
288
289 (defcustom facemenu-end-add-face nil
290 "String to insert or function called at end of text to change or nil.
291 This function is passed the FACE to set, and must return a string which is
292 inserted."
293 :type '(choice (const :tag "None" nil)
294 string
295 function)
296 :group 'facemenu)
297
298 (defcustom facemenu-remove-face-function nil
299 "When non-nil, this is a function called to remove faces.
300 This function is passed the START and END of text to change.
301 May also be t meaning to use `facemenu-add-face-function'."
302 :type '(choice (const :tag "None" nil)
303 (const :tag "Use add-face" t)
304 function)
305 :group 'facemenu)
306
307 ;;; Internal Variables
308
309 (defvar facemenu-color-alist nil
310 ;; Don't initialize here; that doesn't work if preloaded.
311 "Alist of colors, used for completion.
312 If null, `facemenu-read-color' will set it.")
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