(rmail-output): If preserving MIME-version, preserve Content-type too.
[bpt/emacs.git] / lisp / facemenu.el
CommitLineData
be010748 1;;; facemenu.el --- create a face menu for interactively adding fonts to text
b578f267 2
0e520006 3;; Copyright (c) 1994, 1995, 1996, 2001, 2002 Free Software Foundation, Inc.
4e8aa578 4
5762abec 5;; Author: Boris Goldowsky <boris@gnu.org>
4e8aa578
RS
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
b578f267
EN
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.
4e8aa578
RS
24
25;;; Commentary:
b578f267 26
bf7d4561
BG
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
af1eab21 29;; keybindings, which are shown in the menu.
88d690a9
RS
30;;
31;; The menu also contains submenus for indentation and justification-changing
32;; commands.
4e8aa578 33
4e8aa578 34;;; Usage:
bf7d4561
BG
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.
4e8aa578
RS
40;;
41;; Faces can be selected from the keyboard as well.
88d690a9
RS
42;; The standard keybindings are M-g (or ESC g) + letter:
43;; M-g i = "set italic", M-g b = "set bold", etc.
4e8aa578
RS
44
45;;; Customization:
46;; An alternative set of keybindings that may be easier to type can be set up
88d690a9
RS
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:
4e8aa578 53;; xmodmap -e "keysym Alt_L = Hyper_L" -e "add Mod2 = Hyper_L"
88d690a9
RS
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:
4e8aa578
RS
56;; (setq facemenu-keybindings
57;; '((default . [?\H-d])
58;; (bold . [?\H-b])
59;; (italic . [?\H-i])
88d690a9 60;; (bold-italic . [?\H-l])
4e8aa578 61;; (underline . [?\H-u])))
9086c730 62;; (facemenu-update)
4e8aa578 63;; (setq facemenu-keymap global-map)
88d690a9
RS
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
4e8aa578 66;;
88d690a9
RS
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'.
4e8aa578
RS
71
72;;; Known Problems:
88d690a9
RS
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;;
4e8aa578
RS
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
0e520006
PA
89(eval-when-compile
90 (require 'help)
91 (require 'button))
92
9dc90430
BG
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)
9086c730
RS
96
97;; Global bindings:
98(define-key global-map [C-down-mouse-2] 'facemenu-menu)
99(define-key global-map "\M-g" 'facemenu-keymap)
4e8aa578 100
487e6fcb
RS
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
4e8aa578
RS
107 '((default . "d")
108 (bold . "b")
109 (italic . "i")
88d690a9 110 (bold-italic . "l") ; {bold} intersect {italic} = {l}
4e8aa578 111 (underline . "u"))
220c969f 112 "Alist of interesting faces and keybindings.
4e8aa578
RS
113Each element is itself a list: the car is the name of the face,
114the next element is the key to use as a keyboard equivalent of the menu item;
9086c730 115the binding is made in `facemenu-keymap'.
4e8aa578
RS
116
117The faces specifically mentioned in this list are put at the top of
118the menu, in the order specified. All other faces which are defined,
119except for those in `facemenu-unlisted-faces', are listed after them,
120but get no keyboard equivalents.
121
122If you change this variable after loading facemenu.el, you will need to call
487e6fcb
RS
123`facemenu-update' to make it take effect."
124 :type '(repeat (cons face string))
125 :group 'facemenu)
4e8aa578 126
487e6fcb 127(defcustom facemenu-new-faces-at-end t
9086c730 128 "*Where in the menu to insert newly-created faces.
88d690a9 129This should be nil to put them at the top of the menu, or t to put them
487e6fcb
RS
130just before \"Other\" at the end."
131 :type 'boolean
132 :group 'facemenu)
88d690a9 133
487e6fcb 134(defcustom facemenu-unlisted-faces
74a723de
DL
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-"))
9086c730 139 "*List of faces not to include in the Face menu.
7dc30d5b
RS
140Each element may be either a symbol, which is the name of a face, or a string,
141which is a regular expression to be matched against face names. Matching
142faces will not be added to the menu.
143
5a79ed26
KH
144You can set this list before loading facemenu.el, or add a face to it before
145creating that face if you do not want it to be listed. If you change the
146variable so as to eliminate faces that have already been added to the menu,
147call `facemenu-update' to recalculate the menu contents.
4e8aa578 148
88d690a9
RS
149If this variable is t, no faces will be added to the menu. This is useful for
150temporarily turning off the feature that automatically adds faces to the menu
487e6fcb 151when they are created."
7d8177cf
RS
152 :type '(choice (const :tag "Don't add faces" t)
153 (const :tag "None (do add any face)" nil)
7dc30d5b 154 (repeat (choice symbol regexp)))
487e6fcb 155 :group 'facemenu)
88d690a9 156
9dc90430 157;;;###autoload
88d690a9 158(defvar facemenu-face-menu
bf7d4561 159 (let ((map (make-sparse-keymap "Face")))
88d690a9 160 (define-key map "o" (cons "Other..." 'facemenu-set-face))
bf7d4561
BG
161 map)
162 "Menu keymap for faces.")
9dc90430 163;;;###autoload
88d690a9 164(defalias 'facemenu-face-menu facemenu-face-menu)
bf7d4561 165
9dc90430 166;;;###autoload
bf7d4561
BG
167(defvar facemenu-foreground-menu
168 (let ((map (make-sparse-keymap "Foreground Color")))
f34eaa2c 169 (define-key map "o" (cons "Other..." 'facemenu-set-foreground))
bf7d4561
BG
170 map)
171 "Menu keymap for foreground colors.")
9dc90430 172;;;###autoload
88d690a9 173(defalias 'facemenu-foreground-menu facemenu-foreground-menu)
bf7d4561 174
9dc90430 175;;;###autoload
bf7d4561
BG
176(defvar facemenu-background-menu
177 (let ((map (make-sparse-keymap "Background Color")))
f34eaa2c 178 (define-key map "o" (cons "Other..." 'facemenu-set-background))
bf7d4561 179 map)
7e6cb513 180 "Menu keymap for background colors.")
9dc90430 181;;;###autoload
88d690a9 182(defalias 'facemenu-background-menu facemenu-background-menu)
bf7d4561 183
9dc90430 184;;;###autoload
bf7d4561
BG
185(defvar facemenu-special-menu
186 (let ((map (make-sparse-keymap "Special")))
2d07ff84
DL
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))
bf7d4561
BG
195 map)
196 "Menu keymap for non-face text-properties.")
9dc90430 197;;;###autoload
88d690a9
RS
198(defalias 'facemenu-special-menu facemenu-special-menu)
199
9dc90430 200;;;###autoload
88d690a9
RS
201(defvar facemenu-justification-menu
202 (let ((map (make-sparse-keymap "Justification")))
2d07ff84
DL
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))
88d690a9
RS
208 map)
209 "Submenu for text justification commands.")
9dc90430 210;;;###autoload
88d690a9
RS
211(defalias 'facemenu-justification-menu facemenu-justification-menu)
212
9dc90430 213;;;###autoload
88d690a9
RS
214(defvar facemenu-indentation-menu
215 (let ((map (make-sparse-keymap "Indentation")))
f34eaa2c 216 (define-key map [decrease-right-margin]
2d07ff84 217 (cons (purecopy "Indent Right Less") 'decrease-right-margin))
f34eaa2c 218 (define-key map [increase-right-margin]
2d07ff84 219 (cons (purecopy "Indent Right More") 'increase-right-margin))
f34eaa2c 220 (define-key map [decrease-left-margin]
2d07ff84 221 (cons (purecopy "Indent Less") 'decrease-left-margin))
f34eaa2c 222 (define-key map [increase-left-margin]
2d07ff84 223 (cons (purecopy "Indent More") 'increase-left-margin))
88d690a9
RS
224 map)
225 "Submenu for indentation commands.")
9dc90430 226;;;###autoload
88d690a9 227(defalias 'facemenu-indentation-menu facemenu-indentation-menu)
bf7d4561 228
f34eaa2c 229;; This is split up to avoid an overlong line in loaddefs.el.
9dc90430 230;;;###autoload
f34eaa2c 231(defvar facemenu-menu nil
535d2617 232 "Facemenu top-level menu keymap.")
9dc90430 233;;;###autoload
f34eaa2c
KH
234(setq facemenu-menu (make-sparse-keymap "Text Properties"))
235;;;###autoload
236(let ((map facemenu-menu))
2d07ff84
DL
237 (define-key map [dc] (cons (purecopy "Display Colors") 'list-colors-display))
238 (define-key map [df] (cons (purecopy "Display Faces") 'list-faces-display))
cea5ec30
RS
239 (define-key map [dp] (cons (purecopy "Describe Properties")
240 'describe-text-properties))
2d07ff84
DL
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 "--"))))
f34eaa2c
KH
246;;;###autoload
247(let ((map facemenu-menu))
2d07ff84
DL
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)))
f34eaa2c 261;;;###autoload
88d690a9 262(defalias 'facemenu-menu facemenu-menu)
bf7d4561 263
88d690a9
RS
264(defvar facemenu-keymap
265 (let ((map (make-sparse-keymap "Set face")))
2d07ff84 266 (define-key map "o" (cons (purecopy "Other...") 'facemenu-set-face))
88d690a9 267 map)
9dc90430 268 "Keymap for face-changing commands.
bf7d4561 269`Facemenu-update' fills in the keymap according to the bindings
535d2617 270requested in `facemenu-keybindings'.")
88d690a9 271(defalias 'facemenu-keymap facemenu-keymap)
bf7d4561 272
cb5bec6e 273
487e6fcb 274(defcustom facemenu-add-face-function nil
7e6cb513 275 "Function called at beginning of text to change or nil.
cb5bec6e 276This function is passed the FACE to set and END of text to change, and must
487e6fcb
RS
277return a string which is inserted. It may set `facemenu-end-add-face'."
278 :type '(choice (const :tag "None" nil)
279 function)
280 :group 'facemenu)
cb5bec6e 281
487e6fcb 282(defcustom facemenu-end-add-face nil
7e6cb513 283 "String to insert or function called at end of text to change or nil.
cb5bec6e 284This function is passed the FACE to set, and must return a string which is
487e6fcb
RS
285inserted."
286 :type '(choice (const :tag "None" nil)
287 string
288 function)
289 :group 'facemenu)
cb5bec6e 290
487e6fcb 291(defcustom facemenu-remove-face-function nil
9086c730 292 "When non-nil, this is a function called to remove faces.
cb5bec6e 293This function is passed the START and END of text to change.
7e6cb513 294May also be t meaning to use `facemenu-add-face-function'."
487e6fcb
RS
295 :type '(choice (const :tag "None" nil)
296 (const :tag "Use add-face" t)
297 function)
298 :group 'facemenu)
cb5bec6e 299
bf7d4561
BG
300;;; Internal Variables
301
302(defvar facemenu-color-alist nil
303 ;; Don't initialize here; that doesn't work if preloaded.
304 "Alist of colors, used for completion.
305If null, `facemenu-read-color' will set it.")
4a24b314 306
4e8aa578 307(defun facemenu-update ()
bf7d4561
BG
308 "Add or update the \"Face\" menu in the menu bar.
309You can call this to update things if you change any of the menu configuration
310variables."
4e8aa578 311 (interactive)
4e8aa578 312
bf7d4561
BG
313 ;; Add each defined face to the menu.
314 (facemenu-iterate 'facemenu-add-new-face
315 (facemenu-complete-face-list facemenu-keybindings)))
4a24b314 316
4e8aa578
RS
317;;;###autoload
318(defun facemenu-set-face (face &optional start end)
4a24b314 319 "Add FACE to the region or next character typed.
7d8177cf 320This adds FACE to the top of the face list; any faces lower on the list that
4a24b314
RS
321will not show through at all will be removed.
322
7d8177cf 323Interactively, reads the face name with the minibuffer.
f34eaa2c 324
7d8177cf
RS
325If the region is active (normally true except in Transient Mark mode)
326and there is no prefix argument, this command sets the region to the
327requested face.
f34eaa2c
KH
328
329Otherwise, this command specifies the face for the next character
330inserted. Moving point or switching buffers before
331typing a character to insert cancels the specification."
7d8177cf
RS
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))))
88d690a9 339 (facemenu-add-new-face face)
7d8177cf 340 (facemenu-add-face face start end))
4a24b314 341
bf7d4561 342;;;###autoload
4a24b314 343(defun facemenu-set-foreground (color &optional start end)
7e6cb513 344 "Set the foreground COLOR of the region or next character typed.
af1eab21 345This command reads the color in the minibuffer.
7d8177cf
RS
346
347If the region is active (normally true except in Transient Mark mode)
348and there is no prefix argument, this command sets the region to the
349requested face.
350
351Otherwise, this command specifies the face for the next character
352inserted. Moving point or switching buffers before
353typing 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))
9bf4c4e5 363 (facemenu-add-new-color color 'facemenu-foreground-menu)
7d8177cf 364 (facemenu-add-face (list (list :foreground color)) start end))
4a24b314 365
bf7d4561 366;;;###autoload
4a24b314 367(defun facemenu-set-background (color &optional start end)
7e6cb513 368 "Set the background COLOR of the region or next character typed.
af1eab21 369This command reads the color in the minibuffer.
7d8177cf
RS
370
371If the region is active (normally true except in Transient Mark mode)
372and there is no prefix argument, this command sets the region to the
373requested face.
374
375Otherwise, this command specifies the face for the next character
376inserted. Moving point or switching buffers before
377typing 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))
9bf4c4e5 387 (facemenu-add-new-color color 'facemenu-background-menu)
7d8177cf 388 (facemenu-add-face (list (list :background color)) start end))
4e8aa578 389
9dc90430 390;;;###autoload
4e8aa578 391(defun facemenu-set-face-from-menu (face start end)
7e6cb513 392 "Set the FACE of the region or next character typed.
4e8aa578
RS
393This function is designed to be called from a menu; the face to use
394is the menu item's name.
f34eaa2c 395
7d8177cf
RS
396If the region is active (normally true except in Transient Mark mode)
397and there is no prefix argument, this command sets the region to the
398requested face.
f34eaa2c
KH
399
400Otherwise, this command specifies the face for the next character
401inserted. Moving point or switching buffers before
402typing a character to insert cancels the specification."
4a24b314 403 (interactive (list last-command-event
f34eaa2c
KH
404 (if (and mark-active (not current-prefix-arg))
405 (region-beginning))
406 (if (and mark-active (not current-prefix-arg))
407 (region-end))))
88d690a9 408 (barf-if-buffer-read-only)
4a24b314 409 (facemenu-get-face face)
4e8aa578 410 (if start
4a24b314 411 (facemenu-add-face face start end)
cb5bec6e 412 (facemenu-add-face face)))
4e8aa578 413
9dc90430 414;;;###autoload
4e8aa578
RS
415(defun facemenu-set-invisible (start end)
416 "Make the region invisible.
417This sets the `invisible' text property; it can be undone with
f34eaa2c 418`facemenu-remove-special'."
4e8aa578 419 (interactive "r")
0e3edd7b 420 (add-text-properties start end '(invisible t)))
4e8aa578 421
9dc90430 422;;;###autoload
4e8aa578
RS
423(defun facemenu-set-intangible (start end)
424 "Make the region intangible: disallow moving into it.
425This sets the `intangible' text property; it can be undone with
f34eaa2c 426`facemenu-remove-special'."
4e8aa578 427 (interactive "r")
0e3edd7b 428 (add-text-properties start end '(intangible t)))
4e8aa578 429
9dc90430 430;;;###autoload
4e8aa578
RS
431(defun facemenu-set-read-only (start end)
432 "Make the region unmodifiable.
433This sets the `read-only' text property; it can be undone with
f34eaa2c 434`facemenu-remove-special'."
4e8aa578 435 (interactive "r")
0e3edd7b 436 (add-text-properties start end '(read-only t)))
4e8aa578 437
9dc90430 438;;;###autoload
a32d7856
KH
439(defun facemenu-remove-face-props (start end)
440 "Remove `face' and `mouse-face' text properties."
4e8aa578
RS
441 (interactive "*r") ; error if buffer is read-only despite the next line.
442 (let ((inhibit-read-only t))
443 (remove-text-properties
a32d7856 444 start end '(face nil mouse-face nil))))
4e8aa578 445
f34eaa2c
KH
446;;;###autoload
447(defun facemenu-remove-all (start end)
448 "Remove all text properties from the region."
449 (interactive "*r") ; error if buffer is read-only despite the next line.
450 (let ((inhibit-read-only t))
451 (set-text-properties start end nil)))
452
453;;;###autoload
454(defun facemenu-remove-special (start end)
455 "Remove all the \"special\" text properties from the region.
456These 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))))
0af1db42 461\f
bf7d4561 462;;;###autoload
da627a71 463(defun facemenu-read-color (&optional prompt)
bf7d4561 464 "Read a color using the minibuffer."
da627a71 465 (let ((col (completing-read (or prompt "Color: ")
bf7d4561 466 (or facemenu-color-alist
1ff4ace5 467 (defined-colors))
bf7d4561
BG
468 nil t)))
469 (if (equal "" col)
470 nil
471 col)))
4e8aa578 472
88d690a9
RS
473;;;###autoload
474(defun list-colors-display (&optional list)
7c49006b
RS
475 "Display names of defined colors, and show what they look like.
476If the optional argument LIST is non-nil, it should be a list of
477colors to display. Otherwise, this command computes a list
478of colors that the current display can handle."
88d690a9 479 (interactive)
6062889d 480 (when (and (null list) (> (display-color-cells) 0))
f795f633 481 (setq list (defined-colors))
16b6c966
DL
482 ;; Delete duplicate colors.
483 (let ((l list))
484 (while (cdr l)
485 (if (facemenu-color-equal (car l) (car (cdr l)))
486 (setcdr l (cdr (cdr l)))
6062889d 487 (setq l (cdr l)))))
d2596700
MB
488 (when (memq (display-visual-class) '(gray-scale pseudo-color direct-color))
489 ;; Don't show more than what the display can handle.
490 (let ((lc (nthcdr (1- (display-color-cells)) list)))
491 (if lc
492 (setcdr lc nil)))))
88d690a9
RS
493 (with-output-to-temp-buffer "*Colors*"
494 (save-excursion
495 (set-buffer standard-output)
7dc30d5b 496 (let (s)
88d690a9
RS
497 (while list
498 (setq s (point))
499 (insert (car list))
500 (indent-to 20)
501 (put-text-property s (point) 'face
7dc30d5b 502 (cons 'background-color (car list)))
88d690a9
RS
503 (setq s (point))
504 (insert " " (car list) "\n")
505 (put-text-property s (point) 'face
7dc30d5b 506 (cons 'foreground-color (car list)))
88d690a9
RS
507 (setq list (cdr list)))))))
508
509(defun facemenu-color-equal (a b)
510 "Return t if colors A and B are the same color.
7c49006b 511A and B should be strings naming colors.
f795f633
EZ
512This function queries the display system to find out what the color
513names mean. It returns nil if the colors differ or if it can't
7c49006b 514determine the correct answer."
88d690a9 515 (cond ((equal a b) t)
f795f633 516 ((equal (color-values a) (color-values b)))))
88d690a9 517
cb5bec6e 518(defun facemenu-add-face (face &optional start end)
4a24b314 519 "Add FACE to text between START and END.
7e6cb513 520If START is nil or START to END is empty, add FACE to next typed character
cb5bec6e
RS
521instead. For each section of that region that has a different face property,
522FACE will be consed onto it, and other faces that are completely hidden by
523that will be removed from the list.
af1eab21 524If `facemenu-add-face-function' and maybe `facemenu-end-add-face' are non-nil,
cb5bec6e 525they are used to set the face information.
bf7d4561
BG
526
527As a special case, if FACE is `default', then the region is left with NO face
528text property. Otherwise, selecting the default face would not have any
cb5bec6e
RS
529effect. See `facemenu-remove-face-function'."
530 (interactive "*xFace: \nr")
531 (if (and (eq face 'default)
532 (not (eq facemenu-remove-face-function t)))
533 (if facemenu-remove-face-function
534 (funcall facemenu-remove-face-function start end)
682e437e
RS
535 (if (and start (< start end))
536 (remove-text-properties start end '(face default))
537 (setq self-insert-face 'default
538 self-insert-face-command this-command)))
cb5bec6e
RS
539 (if facemenu-add-face-function
540 (save-excursion
541 (if end (goto-char end))
542 (save-excursion
543 (if start (goto-char start))
544 (insert-before-markers
545 (funcall facemenu-add-face-function face end)))
546 (if facemenu-end-add-face
547 (insert (if (stringp facemenu-end-add-face)
548 facemenu-end-add-face
549 (funcall facemenu-end-add-face face)))))
550 (if (and start (< start end))
551 (let ((part-start start) part-end)
552 (while (not (= part-start end))
553 (setq part-end (next-single-property-change part-start 'face
554 nil end))
555 (let ((prev (get-text-property part-start 'face)))
556 (put-text-property part-start part-end 'face
557 (if (null prev)
558 face
559 (facemenu-active-faces
560 (cons face
561 (if (listp prev)
562 prev
563 (list prev)))))))
564 (setq part-start part-end)))
565 (setq self-insert-face (if (eq last-command self-insert-face-command)
566 (cons face (if (listp self-insert-face)
567 self-insert-face
568 (list self-insert-face)))
569 face)
570 self-insert-face-command this-command)))))
4a24b314 571
5a79ed26
KH
572(defun facemenu-active-faces (face-list &optional frame)
573 "Return from FACE-LIST those faces that would be used for display.
574This means each face attribute is not specified in a face earlier in FACE-LIST
575and such a face is therefore active when used to display text.
576If the optional argument FRAME is given, use the faces in that frame; otherwise
577use the selected frame. If t, then the global, non-frame faces are used."
7d8177cf
RS
578 (let* ((mask-atts (copy-sequence
579 (if (consp (car face-list))
f790dddf 580 (face-attributes-as-vector (car face-list))
7d8177cf
RS
581 (or (internal-lisp-face-p (car face-list) frame)
582 (check-face (car face-list))))))
5a79ed26
KH
583 (active-list (list (car face-list)))
584 (face-list (cdr face-list))
585 (mask-len (length mask-atts)))
586 (while face-list
7d8177cf
RS
587 (if (let ((face-atts
588 (if (consp (car face-list))
f790dddf 589 (face-attributes-as-vector (car face-list))
7d8177cf
RS
590 (or (internal-lisp-face-p (car face-list) frame)
591 (check-face (car face-list)))))
592 (i mask-len)
593 (useful nil))
5a79ed26 594 (while (> (setq i (1- i)) 1)
7d8177cf
RS
595 (and (not (memq (aref face-atts i) '(nil unspecified)))
596 (memq (aref mask-atts i) '(nil unspecified))
5a79ed26
KH
597 (aset mask-atts i (setq useful t))))
598 useful)
599 (setq active-list (cons (car face-list) active-list)))
600 (setq face-list (cdr face-list)))
601 (nreverse active-list)))
4a24b314 602
bf7d4561
BG
603(defun facemenu-get-face (symbol)
604 "Make sure FACE exists.
7d8177cf 605If not, create it and add it to the appropriate menu. Return the SYMBOL."
c7bce5f2 606 (let ((name (symbol-name symbol)))
7e6cb513 607 (cond ((facep symbol))
0351bce7
RS
608 (t (make-face symbol))))
609 symbol)
bf7d4561 610
9bf4c4e5
RS
611(defun facemenu-add-new-face (face)
612 "Add FACE (a face) to the Face menu.
7d8177cf
RS
613
614This is called whenever you create a new face."
615 (let* (name
616 symbol
9bf4c4e5
RS
617 menu docstring
618 (key (cdr (assoc face facemenu-keybindings)))
88d690a9 619 function menu-val)
9bf4c4e5
RS
620 (if (symbolp face)
621 (setq name (symbol-name face)
622 symbol face)
623 (setq name face
1d792b18 624 symbol (intern name)))
9bf4c4e5
RS
625 (setq menu 'facemenu-face-menu)
626 (setq docstring
627 (format "Select face `%s' for subsequent insertion."
628 name))
88d690a9 629 (cond ((eq t facemenu-unlisted-faces))
7d8177cf 630 ((memq symbol facemenu-unlisted-faces))
7dc30d5b
RS
631 ;; test against regexps in facemenu-unlisted-faces
632 ((let ((unlisted facemenu-unlisted-faces)
633 (matched nil))
634 (while (and unlisted (not matched))
635 (if (and (stringp (car unlisted))
636 (string-match (car unlisted) name))
637 (setq matched t)
638 (setq unlisted (cdr unlisted))))
639 matched))
88d690a9
RS
640 (key ; has a keyboard equivalent. These go at the front.
641 (setq function (intern (concat "facemenu-set-" name)))
642 (fset function
536f1a10
RS
643 `(lambda ()
644 ,docstring
645 (interactive)
af1eab21
RS
646 (facemenu-set-face
647 (quote ,symbol)
648 (if (and mark-active (not current-prefix-arg))
649 (region-beginning))
650 (if (and mark-active (not current-prefix-arg))
651 (region-end)))))
88d690a9
RS
652 (define-key 'facemenu-keymap key (cons name function))
653 (define-key menu key (cons name function)))
654 ((facemenu-iterate ; check if equivalent face is already in the menu
655 (lambda (m) (and (listp m)
656 (symbolp (car m))
7d8177cf 657 (face-equal (car m) symbol)))
88d690a9
RS
658 (cdr (symbol-function menu))))
659 (t ; No keyboard equivalent. Figure out where to put it:
7d8177cf 660 (setq key (vector symbol)
88d690a9
RS
661 function 'facemenu-set-face-from-menu
662 menu-val (symbol-function menu))
663 (if (and facemenu-new-faces-at-end
664 (> (length menu-val) 3))
665 (define-key-after menu-val key (cons name function)
9bf4c4e5
RS
666 (car (nth (- (length menu-val) 3) menu-val)))
667 (define-key menu key (cons name function))))))
668 nil) ; Return nil for facemenu-iterate
669
670(defun facemenu-add-new-color (color &optional menu)
671 "Add COLOR (a color name string) to the appropriate Face menu.
672MENU should be `facemenu-foreground-menu' or
673`facemenu-background-menu'.
674
675This is called whenever you use a new color."
676 (let* (name
677 symbol
678 docstring
679 function menu-val key
680 (color-p (memq menu '(facemenu-foreground-menu
681 facemenu-background-menu))))
682 (unless (stringp color)
683 (error "%s is not a color" color))
684 (setq name color
685 symbol (intern name))
686
687 (cond ((eq menu 'facemenu-foreground-menu)
688 (setq docstring
689 (format "Select foreground color %s for subsequent insertion."
690 name)))
691 ((eq menu 'facemenu-background-menu)
692 (setq docstring
693 (format "Select background color %s for subsequent insertion."
694 name))))
695 (cond ((facemenu-iterate ; check if equivalent face is already in the menu
696 (lambda (m) (and (listp m)
697 (symbolp (car m))
698 (stringp (cadr m))
699 (string-equal (cadr m) color)))
700 (cdr (symbol-function menu))))
701 (t ; No keyboard equivalent. Figure out where to put it:
702 (setq key (vector symbol)
703 function 'facemenu-set-face-from-menu
704 menu-val (symbol-function menu))
705 (if (and facemenu-new-faces-at-end
706 (> (length menu-val) 3))
707 (define-key-after menu-val key (cons name function)
88d690a9
RS
708 (car (nth (- (length menu-val) 3) menu-val)))
709 (define-key menu key (cons name function))))))
710 nil) ; Return nil for facemenu-iterate
bf7d4561 711
bf7d4561 712(defun facemenu-complete-face-list (&optional oldlist)
7cd49450 713 "Return list of all faces that look different.
bf7d4561
BG
714Starts with given ALIST of faces, and adds elements only if they display
715differently from any face already on the list.
716The faces on ALIST will end up at the end of the returned list, in reverse
717order."
718 (let ((list (nreverse (mapcar 'car oldlist))))
719 (facemenu-iterate
720 (lambda (new-face)
721 (if (not (memq new-face list))
722 (setq list (cons new-face list)))
723 nil)
724 (nreverse (face-list)))
725 list))
726
7e6cb513 727(defun facemenu-iterate (func list)
4e8aa578
RS
728 "Apply FUNC to each element of LIST until one returns non-nil.
729Returns the non-nil value it found, or nil if all were nil."
7e6cb513
SM
730 (while (and list (not (funcall func (car list))))
731 (setq list (cdr list)))
732 (car list))
4e8aa578
RS
733
734(facemenu-update)
4e8aa578 735
1ff4ace5 736(provide 'facemenu)
4e8aa578 737;;; facemenu.el ends here