(Fpos_visible_in_window_p, Fset_window_hscroll)
[emacs.git] / lisp / facemenu.el
blobe9af58fc73db9285ea44d2346b280d37d8196114
1 ;;; facemenu.el --- create a face menu for interactively adding fonts to text
3 ;; Copyright (c) 1994, 1995, 1996, 2001, 2002 Free Software Foundation, Inc.
5 ;; Author: Boris Goldowsky <boris@gnu.org>
6 ;; Keywords: faces
8 ;; This file is part of GNU Emacs.
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.
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.
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.
25 ;;; Commentary:
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.
31 ;; The menu also contains submenus for indentation and justification-changing
32 ;; commands.
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.
41 ;; Faces can be selected from the keyboard as well.
42 ;; The standard keybindings are M-g (or ESC g) + letter:
43 ;; M-g i = "set italic", M-g b = "set bold", etc.
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
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'.
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.
77 ;; There is at present no way to display what the faces look like in
78 ;; the menu itself.
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.
85 ;; Please send me any other problems, comments or ideas.
87 ;;; Code:
89 (eval-when-compile
90 (require 'help)
91 (require 'button))
93 ;;; Provide some binding for startup:
94 ;;;###autoload (define-key global-map "\M-g" 'facemenu-keymap)
95 ;;;###autoload (autoload 'facemenu-keymap "facemenu" "Keymap for face-changing commands." t 'keymap)
97 ;; Global bindings:
98 (define-key global-map [C-down-mouse-2] 'facemenu-menu)
99 (define-key global-map "\M-g" 'facemenu-keymap)
101 (defgroup facemenu nil
102 "Create a face menu for interactively adding fonts to text"
103 :group 'faces
104 :prefix "facemenu-")
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'.
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.
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)
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)
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.
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.
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)
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)
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)
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)
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)
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)
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)
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)
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)
274 (defcustom facemenu-add-face-function nil
275 "Function called at beginning of text to change or nil.
276 This function is passed the FACE to set and END of text to change, and must
277 return a string which is inserted. It may set `facemenu-end-add-face'."
278 :type '(choice (const :tag "None" nil)
279 function)
280 :group 'facemenu)
282 (defcustom facemenu-end-add-face nil
283 "String to insert or function called at end of text to change or nil.
284 This function is passed the FACE to set, and must return a string which is
285 inserted."
286 :type '(choice (const :tag "None" nil)
287 string
288 function)
289 :group 'facemenu)
291 (defcustom facemenu-remove-face-function nil
292 "When non-nil, this is a function called to remove faces.
293 This function is passed the START and END of text to change.
294 May also be t meaning to use `facemenu-add-face-function'."
295 :type '(choice (const :tag "None" nil)
296 (const :tag "Use add-face" t)
297 function)
298 :group 'facemenu)
300 ;;; Internal Variables
302 (defvar facemenu-color-alist nil
303 ;; 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.")
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)
313 ;; Add each defined face to the menu.
314 (facemenu-iterate 'facemenu-add-new-face
315 (facemenu-complete-face-list facemenu-keybindings)))
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.
323 Interactively, reads the face name with the minibuffer.
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.
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))
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.
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.
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))
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.
371 If the region is active (normally true except in Transient Mark mode)
372 and there is no prefix argument, this command sets the region to the
373 requested face.
375 Otherwise, this command specifies the face for the next character
376 inserted. Moving point or switching buffers before
377 typing a character to insert cancels the specification."
378 (interactive (list (progn
379 (barf-if-buffer-read-only)
380 (facemenu-read-color "Background color: "))
381 (if (and mark-active (not current-prefix-arg))
382 (region-beginning))
383 (if (and mark-active (not current-prefix-arg))
384 (region-end))))
385 (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))
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.
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.
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)))
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)))
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)))
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)))
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))))
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)))
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))))
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)
471 col)))
473 ;;;###autoload
474 (defun list-colors-display (&optional list)
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
478 of colors that the current display can handle."
479 (interactive)
480 (when (and (null list) (> (display-color-cells) 0))
481 (setq list (defined-colors))
482 ;; Delete duplicate colors.
484 ;; Identify duplicate colors by the name rather than the color
485 ;; value. For example, on MS-Windows, logical colors are added to
486 ;; the list that might have the same value but have different
487 ;; names and meanings. For example, `SystemMenuText' (the color
488 ;; w32 uses for the text in menu entries) and `SystemWindowText'
489 ;; (the default color w32 uses for the text in windows and
490 ;; dialogs) may be the same display color and be adjacent in the
491 ;; list. Detecting duplicates by name insures that both of these
492 ;; colors remain despite identical color values.
493 (let ((l list))
494 (while (cdr l)
495 (if (facemenu-color-name-equal (car l) (car (cdr l)))
496 (setcdr l (cdr (cdr l)))
497 (setq l (cdr l)))))
498 (when (memq (display-visual-class) '(gray-scale pseudo-color direct-color))
499 ;; Don't show more than what the display can handle.
500 (let ((lc (nthcdr (1- (display-color-cells)) list)))
501 (if lc
502 (setcdr lc nil)))))
503 (with-output-to-temp-buffer "*Colors*"
504 (save-excursion
505 (set-buffer standard-output)
506 (let (s)
507 (while list
508 (setq s (point))
509 (insert (car list))
510 (indent-to 20)
511 (put-text-property s (point) 'face
512 (cons 'background-color (car list)))
513 (setq s (point))
514 (insert " " (car list) "\n")
515 (put-text-property s (point) 'face
516 (cons 'foreground-color (car list)))
517 (setq list (cdr list)))))))
519 (defun facemenu-color-equal (a b)
520 "Return t if colors A and B are the same color.
521 A and B should be strings naming colors.
522 This function queries the display system to find out what the color
523 names mean. It returns nil if the colors differ or if it can't
524 determine the correct answer."
525 (cond ((equal a b) t)
526 ((equal (color-values a) (color-values b)))))
528 (defun facemenu-color-name-equal (a b)
529 "Return t if colors A and B are the same color.
530 A and B should be strings naming colors. These names are
531 downcased, stripped of spaces and the string `grey' is turned
532 into `gray'. This accommodates alternative spellings of colors
533 found commonly in the list. It returns nil if the colors differ."
534 (progn
535 (setq a (replace-regexp-in-string "grey" "gray"
536 (replace-regexp-in-string " " ""
537 (downcase a)))
538 b (replace-regexp-in-string "grey" "gray"
539 (replace-regexp-in-string " " ""
540 (downcase b))))
542 (equal a b)))
544 (defun facemenu-add-face (face &optional start end)
545 "Add FACE to text between START and END.
546 If START is nil or START to END is empty, add FACE to next typed character
547 instead. For each section of that region that has a different face property,
548 FACE will be consed onto it, and other faces that are completely hidden by
549 that will be removed from the list.
550 If `facemenu-add-face-function' and maybe `facemenu-end-add-face' are non-nil,
551 they are used to set the face information.
553 As a special case, if FACE is `default', then the region is left with NO face
554 text property. Otherwise, selecting the default face would not have any
555 effect. See `facemenu-remove-face-function'."
556 (interactive "*xFace: \nr")
557 (if (and (eq face 'default)
558 (not (eq facemenu-remove-face-function t)))
559 (if facemenu-remove-face-function
560 (funcall facemenu-remove-face-function start end)
561 (if (and start (< start end))
562 (remove-text-properties start end '(face default))
563 (setq self-insert-face 'default
564 self-insert-face-command this-command)))
565 (if facemenu-add-face-function
566 (save-excursion
567 (if end (goto-char end))
568 (save-excursion
569 (if start (goto-char start))
570 (insert-before-markers
571 (funcall facemenu-add-face-function face end)))
572 (if facemenu-end-add-face
573 (insert (if (stringp facemenu-end-add-face)
574 facemenu-end-add-face
575 (funcall facemenu-end-add-face face)))))
576 (if (and start (< start end))
577 (let ((part-start start) part-end)
578 (while (not (= part-start end))
579 (setq part-end (next-single-property-change part-start 'face
580 nil end))
581 (let ((prev (get-text-property part-start 'face)))
582 (put-text-property part-start part-end 'face
583 (if (null prev)
584 face
585 (facemenu-active-faces
586 (cons face
587 (if (listp prev)
588 prev
589 (list prev)))))))
590 (setq part-start part-end)))
591 (setq self-insert-face (if (eq last-command self-insert-face-command)
592 (cons face (if (listp self-insert-face)
593 self-insert-face
594 (list self-insert-face)))
595 face)
596 self-insert-face-command this-command)))))
598 (defun facemenu-active-faces (face-list &optional frame)
599 "Return from FACE-LIST those faces that would be used for display.
600 This means each face attribute is not specified in a face earlier in FACE-LIST
601 and such a face is therefore active when used to display text.
602 If the optional argument FRAME is given, use the faces in that frame; otherwise
603 use the selected frame. If t, then the global, non-frame faces are used."
604 (let* ((mask-atts (copy-sequence
605 (if (consp (car face-list))
606 (face-attributes-as-vector (car face-list))
607 (or (internal-lisp-face-p (car face-list) frame)
608 (check-face (car face-list))))))
609 (active-list (list (car face-list)))
610 (face-list (cdr face-list))
611 (mask-len (length mask-atts)))
612 (while face-list
613 (if (let ((face-atts
614 (if (consp (car face-list))
615 (face-attributes-as-vector (car face-list))
616 (or (internal-lisp-face-p (car face-list) frame)
617 (check-face (car face-list)))))
618 (i mask-len)
619 (useful nil))
620 (while (> (setq i (1- i)) 1)
621 (and (not (memq (aref face-atts i) '(nil unspecified)))
622 (memq (aref mask-atts i) '(nil unspecified))
623 (aset mask-atts i (setq useful t))))
624 useful)
625 (setq active-list (cons (car face-list) active-list)))
626 (setq face-list (cdr face-list)))
627 (nreverse active-list)))
629 (defun facemenu-get-face (symbol)
630 "Make sure FACE exists.
631 If not, create it and add it to the appropriate menu. Return the SYMBOL."
632 (let ((name (symbol-name symbol)))
633 (cond ((facep symbol))
634 (t (make-face symbol))))
635 symbol)
637 (defun facemenu-add-new-face (face)
638 "Add FACE (a face) to the Face menu.
640 This is called whenever you create a new face."
641 (let* (name
642 symbol
643 menu docstring
644 (key (cdr (assoc face facemenu-keybindings)))
645 function menu-val)
646 (if (symbolp face)
647 (setq name (symbol-name face)
648 symbol face)
649 (setq name face
650 symbol (intern name)))
651 (setq menu 'facemenu-face-menu)
652 (setq docstring
653 (format "Select face `%s' for subsequent insertion."
654 name))
655 (cond ((eq t facemenu-unlisted-faces))
656 ((memq symbol facemenu-unlisted-faces))
657 ;; test against regexps in facemenu-unlisted-faces
658 ((let ((unlisted facemenu-unlisted-faces)
659 (matched nil))
660 (while (and unlisted (not matched))
661 (if (and (stringp (car unlisted))
662 (string-match (car unlisted) name))
663 (setq matched t)
664 (setq unlisted (cdr unlisted))))
665 matched))
666 (key ; has a keyboard equivalent. These go at the front.
667 (setq function (intern (concat "facemenu-set-" name)))
668 (fset function
669 `(lambda ()
670 ,docstring
671 (interactive)
672 (facemenu-set-face
673 (quote ,symbol)
674 (if (and mark-active (not current-prefix-arg))
675 (region-beginning))
676 (if (and mark-active (not current-prefix-arg))
677 (region-end)))))
678 (define-key 'facemenu-keymap key (cons name function))
679 (define-key menu key (cons name function)))
680 ((facemenu-iterate ; check if equivalent face is already in the menu
681 (lambda (m) (and (listp m)
682 (symbolp (car m))
683 (face-equal (car m) symbol)))
684 (cdr (symbol-function menu))))
685 (t ; No keyboard equivalent. Figure out where to put it:
686 (setq key (vector symbol)
687 function 'facemenu-set-face-from-menu
688 menu-val (symbol-function menu))
689 (if (and facemenu-new-faces-at-end
690 (> (length menu-val) 3))
691 (define-key-after menu-val key (cons name function)
692 (car (nth (- (length menu-val) 3) menu-val)))
693 (define-key menu key (cons name function))))))
694 nil) ; Return nil for facemenu-iterate
696 (defun facemenu-add-new-color (color &optional menu)
697 "Add COLOR (a color name string) to the appropriate Face menu.
698 MENU should be `facemenu-foreground-menu' or
699 `facemenu-background-menu'.
701 This is called whenever you use a new color."
702 (let* (name
703 symbol
704 docstring
705 function menu-val key
706 (color-p (memq menu '(facemenu-foreground-menu
707 facemenu-background-menu))))
708 (unless (stringp color)
709 (error "%s is not a color" color))
710 (setq name color
711 symbol (intern name))
713 (cond ((eq menu 'facemenu-foreground-menu)
714 (setq docstring
715 (format "Select foreground color %s for subsequent insertion."
716 name)))
717 ((eq menu 'facemenu-background-menu)
718 (setq docstring
719 (format "Select background color %s for subsequent insertion."
720 name))))
721 (cond ((facemenu-iterate ; check if equivalent face is already in the menu
722 (lambda (m) (and (listp m)
723 (symbolp (car m))
724 (stringp (cadr m))
725 (string-equal (cadr m) color)))
726 (cdr (symbol-function menu))))
727 (t ; No keyboard equivalent. Figure out where to put it:
728 (setq key (vector symbol)
729 function 'facemenu-set-face-from-menu
730 menu-val (symbol-function menu))
731 (if (and facemenu-new-faces-at-end
732 (> (length menu-val) 3))
733 (define-key-after menu-val key (cons name function)
734 (car (nth (- (length menu-val) 3) menu-val)))
735 (define-key menu key (cons name function))))))
736 nil) ; Return nil for facemenu-iterate
738 (defun facemenu-complete-face-list (&optional oldlist)
739 "Return list of all faces that look different.
740 Starts with given ALIST of faces, and adds elements only if they display
741 differently from any face already on the list.
742 The faces on ALIST will end up at the end of the returned list, in reverse
743 order."
744 (let ((list (nreverse (mapcar 'car oldlist))))
745 (facemenu-iterate
746 (lambda (new-face)
747 (if (not (memq new-face list))
748 (setq list (cons new-face list)))
749 nil)
750 (nreverse (face-list)))
751 list))
753 (defun facemenu-iterate (func list)
754 "Apply FUNC to each element of LIST until one returns non-nil.
755 Returns the non-nil value it found, or nil if all were nil."
756 (while (and list (not (funcall func (car list))))
757 (setq list (cdr list)))
758 (car list))
760 (facemenu-update)
762 (provide 'facemenu)
764 ;;; arch-tag: 85f6d02b-9085-420e-b651-0678f0e9c7eb
765 ;;; facemenu.el ends here