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