(bibtex-parse-buffers-stealthily):
[bpt/emacs.git] / lisp / facemenu.el
CommitLineData
be010748 1;;; facemenu.el --- create a face menu for interactively adding fonts to text
b578f267 2
1d792b18 3;; Copyright (c) 1994, 1995, 1996, 2001 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
29;; keybindings, which are shown in the menu. Faces with names beginning with
88d690a9 30;; "fg:" or "bg:", as in "fg:red", are treated specially.
bf7d4561
BG
31;; Such faces are assumed to consist only of a foreground (if "fg:") or
32;; background (if "bg:") color. They are thus put into the color submenus
88d690a9
RS
33;; rather than the general Face submenu. These faces can also be
34;; automatically created by selecting the "Other..." menu items in the
35;; "Foreground" and "Background" submenus.
36;;
37;; The menu also contains submenus for indentation and justification-changing
38;; commands.
4e8aa578 39
4e8aa578 40;;; Usage:
bf7d4561
BG
41;; Selecting a face from the menu or typing the keyboard equivalent will
42;; change the region to use that face. If you use transient-mark-mode and the
43;; region is not active, the face will be remembered and used for the next
44;; insertion. It will be forgotten if you move point or make other
45;; modifications before inserting or typing anything.
4e8aa578
RS
46;;
47;; Faces can be selected from the keyboard as well.
88d690a9
RS
48;; The standard keybindings are M-g (or ESC g) + letter:
49;; M-g i = "set italic", M-g b = "set bold", etc.
4e8aa578
RS
50
51;;; Customization:
52;; An alternative set of keybindings that may be easier to type can be set up
88d690a9
RS
53;; using "Alt" or "Hyper" keys. This requires that you either have or create
54;; an Alt or Hyper key on your keyboard. On my keyboard, there is a key
55;; labeled "Alt", but to make it act as an Alt key I have to put this command
56;; into my .xinitrc:
57;; xmodmap -e "add Mod3 = Alt_L"
58;; Or, I can make it into a Hyper key with this:
4e8aa578 59;; xmodmap -e "keysym Alt_L = Hyper_L" -e "add Mod2 = Hyper_L"
88d690a9
RS
60;; Check with local X-perts for how to do it on your system.
61;; Then you can define your keybindings with code like this in your .emacs:
4e8aa578
RS
62;; (setq facemenu-keybindings
63;; '((default . [?\H-d])
64;; (bold . [?\H-b])
65;; (italic . [?\H-i])
88d690a9 66;; (bold-italic . [?\H-l])
4e8aa578 67;; (underline . [?\H-u])))
9086c730 68;; (facemenu-update)
4e8aa578 69;; (setq facemenu-keymap global-map)
88d690a9
RS
70;; (define-key global-map [?\H-c] 'facemenu-set-foreground) ; set fg color
71;; (define-key global-map [?\H-C] 'facemenu-set-background) ; set bg color
4e8aa578 72;;
88d690a9
RS
73;; The order of the faces that appear in the menu and their keybindings can be
74;; controlled by setting the variables `facemenu-keybindings' and
75;; `facemenu-new-faces-at-end'. List faces that you don't use in documents
76;; (eg, `region') in `facemenu-unlisted-faces'.
4e8aa578
RS
77
78;;; Known Problems:
88d690a9
RS
79;; Bold and Italic do not combine to create bold-italic if you select them
80;; both, although most other combinations (eg bold + underline + some color)
81;; do the intuitive thing.
82;;
4e8aa578
RS
83;; There is at present no way to display what the faces look like in
84;; the menu itself.
85;;
86;; `list-faces-display' shows the faces in a different order than
87;; this menu, which could be confusing. I do /not/ sort the list
88;; alphabetically, because I like the default order: it puts the most
89;; basic, common fonts first.
90;;
91;; Please send me any other problems, comments or ideas.
92
93;;; Code:
94
95(provide 'facemenu)
96
9dc90430
BG
97;;; Provide some binding for startup:
98;;;###autoload (define-key global-map "\M-g" 'facemenu-keymap)
99;;;###autoload (autoload 'facemenu-keymap "facemenu" "Keymap for face-changing commands." t 'keymap)
9086c730
RS
100
101;; Global bindings:
102(define-key global-map [C-down-mouse-2] 'facemenu-menu)
103(define-key global-map "\M-g" 'facemenu-keymap)
4e8aa578 104
487e6fcb
RS
105(defgroup facemenu nil
106 "Create a face menu for interactively adding fonts to text"
107 :group 'faces
108 :prefix "facemenu-")
109
110(defcustom facemenu-keybindings
4e8aa578
RS
111 '((default . "d")
112 (bold . "b")
113 (italic . "i")
88d690a9 114 (bold-italic . "l") ; {bold} intersect {italic} = {l}
4e8aa578 115 (underline . "u"))
220c969f 116 "Alist of interesting faces and keybindings.
4e8aa578
RS
117Each element is itself a list: the car is the name of the face,
118the next element is the key to use as a keyboard equivalent of the menu item;
9086c730 119the binding is made in `facemenu-keymap'.
4e8aa578
RS
120
121The faces specifically mentioned in this list are put at the top of
122the menu, in the order specified. All other faces which are defined,
123except for those in `facemenu-unlisted-faces', are listed after them,
124but get no keyboard equivalents.
125
126If you change this variable after loading facemenu.el, you will need to call
487e6fcb
RS
127`facemenu-update' to make it take effect."
128 :type '(repeat (cons face string))
129 :group 'facemenu)
4e8aa578 130
487e6fcb 131(defcustom facemenu-new-faces-at-end t
9086c730 132 "*Where in the menu to insert newly-created faces.
88d690a9 133This should be nil to put them at the top of the menu, or t to put them
487e6fcb
RS
134just before \"Other\" at the end."
135 :type 'boolean
136 :group 'facemenu)
88d690a9 137
487e6fcb 138(defcustom facemenu-unlisted-faces
74a723de
DL
139 `(modeline region secondary-selection highlight scratch-face
140 ,(purecopy "^font-lock-") ,(purecopy "^gnus-") ,(purecopy "^message-")
141 ,(purecopy "^ediff-") ,(purecopy "^term-") ,(purecopy "^vc-")
142 ,(purecopy "^widget-") ,(purecopy "^custom-") ,(purecopy "^vm-"))
9086c730 143 "*List of faces not to include in the Face menu.
7dc30d5b
RS
144Each element may be either a symbol, which is the name of a face, or a string,
145which is a regular expression to be matched against face names. Matching
146faces will not be added to the menu.
147
5a79ed26
KH
148You can set this list before loading facemenu.el, or add a face to it before
149creating that face if you do not want it to be listed. If you change the
150variable so as to eliminate faces that have already been added to the menu,
151call `facemenu-update' to recalculate the menu contents.
4e8aa578 152
88d690a9
RS
153If this variable is t, no faces will be added to the menu. This is useful for
154temporarily turning off the feature that automatically adds faces to the menu
487e6fcb 155when they are created."
7d8177cf
RS
156 :type '(choice (const :tag "Don't add faces" t)
157 (const :tag "None (do add any face)" nil)
7dc30d5b 158 (repeat (choice symbol regexp)))
487e6fcb 159 :group 'facemenu)
88d690a9 160
9dc90430 161;;;###autoload
88d690a9 162(defvar facemenu-face-menu
bf7d4561 163 (let ((map (make-sparse-keymap "Face")))
88d690a9 164 (define-key map "o" (cons "Other..." 'facemenu-set-face))
bf7d4561
BG
165 map)
166 "Menu keymap for faces.")
9dc90430 167;;;###autoload
88d690a9 168(defalias 'facemenu-face-menu facemenu-face-menu)
bf7d4561 169
9dc90430 170;;;###autoload
bf7d4561
BG
171(defvar facemenu-foreground-menu
172 (let ((map (make-sparse-keymap "Foreground Color")))
f34eaa2c 173 (define-key map "o" (cons "Other..." 'facemenu-set-foreground))
bf7d4561
BG
174 map)
175 "Menu keymap for foreground colors.")
9dc90430 176;;;###autoload
88d690a9 177(defalias 'facemenu-foreground-menu facemenu-foreground-menu)
bf7d4561 178
9dc90430 179;;;###autoload
bf7d4561
BG
180(defvar facemenu-background-menu
181 (let ((map (make-sparse-keymap "Background Color")))
f34eaa2c 182 (define-key map "o" (cons "Other..." 'facemenu-set-background))
bf7d4561 183 map)
7e6cb513 184 "Menu keymap for background colors.")
9dc90430 185;;;###autoload
88d690a9 186(defalias 'facemenu-background-menu facemenu-background-menu)
bf7d4561 187
9dc90430 188;;;###autoload
bf7d4561
BG
189(defvar facemenu-special-menu
190 (let ((map (make-sparse-keymap "Special")))
2d07ff84
DL
191 (define-key map [?s] (cons (purecopy "Remove Special")
192 'facemenu-remove-special))
193 (define-key map [?t] (cons (purecopy "Intangible")
194 'facemenu-set-intangible))
195 (define-key map [?v] (cons (purecopy "Invisible")
196 'facemenu-set-invisible))
197 (define-key map [?r] (cons (purecopy "Read-Only")
198 'facemenu-set-read-only))
bf7d4561
BG
199 map)
200 "Menu keymap for non-face text-properties.")
9dc90430 201;;;###autoload
88d690a9
RS
202(defalias 'facemenu-special-menu facemenu-special-menu)
203
9dc90430 204;;;###autoload
88d690a9
RS
205(defvar facemenu-justification-menu
206 (let ((map (make-sparse-keymap "Justification")))
2d07ff84
DL
207 (define-key map [?c] (cons (purecopy "Center") 'set-justification-center))
208 (define-key map [?b] (cons (purecopy "Full") 'set-justification-full))
209 (define-key map [?r] (cons (purecopy "Right") 'set-justification-right))
210 (define-key map [?l] (cons (purecopy "Left") 'set-justification-left))
211 (define-key map [?u] (cons (purecopy "Unfilled") 'set-justification-none))
88d690a9
RS
212 map)
213 "Submenu for text justification commands.")
9dc90430 214;;;###autoload
88d690a9
RS
215(defalias 'facemenu-justification-menu facemenu-justification-menu)
216
9dc90430 217;;;###autoload
88d690a9
RS
218(defvar facemenu-indentation-menu
219 (let ((map (make-sparse-keymap "Indentation")))
f34eaa2c 220 (define-key map [decrease-right-margin]
2d07ff84 221 (cons (purecopy "Indent Right Less") 'decrease-right-margin))
f34eaa2c 222 (define-key map [increase-right-margin]
2d07ff84 223 (cons (purecopy "Indent Right More") 'increase-right-margin))
f34eaa2c 224 (define-key map [decrease-left-margin]
2d07ff84 225 (cons (purecopy "Indent Less") 'decrease-left-margin))
f34eaa2c 226 (define-key map [increase-left-margin]
2d07ff84 227 (cons (purecopy "Indent More") 'increase-left-margin))
88d690a9
RS
228 map)
229 "Submenu for indentation commands.")
9dc90430 230;;;###autoload
88d690a9 231(defalias 'facemenu-indentation-menu facemenu-indentation-menu)
bf7d4561 232
f34eaa2c 233;; This is split up to avoid an overlong line in loaddefs.el.
9dc90430 234;;;###autoload
f34eaa2c 235(defvar facemenu-menu nil
535d2617 236 "Facemenu top-level menu keymap.")
9dc90430 237;;;###autoload
f34eaa2c
KH
238(setq facemenu-menu (make-sparse-keymap "Text Properties"))
239;;;###autoload
240(let ((map facemenu-menu))
2d07ff84
DL
241 (define-key map [dc] (cons (purecopy "Display Colors") 'list-colors-display))
242 (define-key map [df] (cons (purecopy "Display Faces") 'list-faces-display))
243 (define-key map [dp] (cons (purecopy "List Properties")
244 'list-text-properties-at))
245 (define-key map [ra] (cons (purecopy "Remove Text Properties")
246 'facemenu-remove-all))
247 (define-key map [rm] (cons (purecopy "Remove Face Properties")
248 'facemenu-remove-face-props))
249 (define-key map [s1] (list (purecopy "--"))))
f34eaa2c
KH
250;;;###autoload
251(let ((map facemenu-menu))
2d07ff84
DL
252 (define-key map [in] (cons (purecopy "Indentation")
253 'facemenu-indentation-menu))
254 (define-key map [ju] (cons (purecopy "Justification")
255 'facemenu-justification-menu))
256 (define-key map [s2] (list (purecopy "--")))
257 (define-key map [sp] (cons (purecopy "Special Properties")
258 'facemenu-special-menu))
259 (define-key map [bg] (cons (purecopy "Background Color")
260 'facemenu-background-menu))
261 (define-key map [fg] (cons (purecopy "Foreground Color")
262 'facemenu-foreground-menu))
263 (define-key map [fc] (cons (purecopy "Face")
264 'facemenu-face-menu)))
f34eaa2c 265;;;###autoload
88d690a9 266(defalias 'facemenu-menu facemenu-menu)
bf7d4561 267
88d690a9
RS
268(defvar facemenu-keymap
269 (let ((map (make-sparse-keymap "Set face")))
2d07ff84 270 (define-key map "o" (cons (purecopy "Other...") 'facemenu-set-face))
88d690a9 271 map)
9dc90430 272 "Keymap for face-changing commands.
bf7d4561 273`Facemenu-update' fills in the keymap according to the bindings
535d2617 274requested in `facemenu-keybindings'.")
88d690a9 275(defalias 'facemenu-keymap facemenu-keymap)
bf7d4561 276
cb5bec6e 277
487e6fcb 278(defcustom facemenu-add-face-function nil
7e6cb513 279 "Function called at beginning of text to change or nil.
cb5bec6e 280This function is passed the FACE to set and END of text to change, and must
487e6fcb
RS
281return a string which is inserted. It may set `facemenu-end-add-face'."
282 :type '(choice (const :tag "None" nil)
283 function)
284 :group 'facemenu)
cb5bec6e 285
487e6fcb 286(defcustom facemenu-end-add-face nil
7e6cb513 287 "String to insert or function called at end of text to change or nil.
cb5bec6e 288This function is passed the FACE to set, and must return a string which is
487e6fcb
RS
289inserted."
290 :type '(choice (const :tag "None" nil)
291 string
292 function)
293 :group 'facemenu)
cb5bec6e 294
487e6fcb 295(defcustom facemenu-remove-face-function nil
9086c730 296 "When non-nil, this is a function called to remove faces.
cb5bec6e 297This function is passed the START and END of text to change.
7e6cb513 298May also be t meaning to use `facemenu-add-face-function'."
487e6fcb
RS
299 :type '(choice (const :tag "None" nil)
300 (const :tag "Use add-face" t)
301 function)
302 :group 'facemenu)
cb5bec6e 303
bf7d4561
BG
304;;; Internal Variables
305
306(defvar facemenu-color-alist nil
307 ;; Don't initialize here; that doesn't work if preloaded.
308 "Alist of colors, used for completion.
309If null, `facemenu-read-color' will set it.")
4a24b314 310
4e8aa578 311(defun facemenu-update ()
bf7d4561
BG
312 "Add or update the \"Face\" menu in the menu bar.
313You can call this to update things if you change any of the menu configuration
314variables."
4e8aa578 315 (interactive)
4e8aa578 316
bf7d4561
BG
317 ;; Add each defined face to the menu.
318 (facemenu-iterate 'facemenu-add-new-face
319 (facemenu-complete-face-list facemenu-keybindings)))
4a24b314 320
4e8aa578
RS
321;;;###autoload
322(defun facemenu-set-face (face &optional start end)
4a24b314 323 "Add FACE to the region or next character typed.
7d8177cf 324This adds FACE to the top of the face list; any faces lower on the list that
4a24b314
RS
325will not show through at all will be removed.
326
7d8177cf 327Interactively, reads the face name with the minibuffer.
f34eaa2c 328
7d8177cf
RS
329If the region is active (normally true except in Transient Mark mode)
330and there is no prefix argument, this command sets the region to the
331requested face.
f34eaa2c
KH
332
333Otherwise, this command specifies the face for the next character
334inserted. Moving point or switching buffers before
335typing a character to insert cancels the specification."
7d8177cf
RS
336 (interactive (list (progn
337 (barf-if-buffer-read-only)
338 (read-face-name "Use face"))
339 (if (and mark-active (not current-prefix-arg))
340 (region-beginning))
341 (if (and mark-active (not current-prefix-arg))
342 (region-end))))
88d690a9 343 (facemenu-add-new-face face)
7d8177cf 344 (facemenu-add-face face start end))
4a24b314 345
bf7d4561 346;;;###autoload
4a24b314 347(defun facemenu-set-foreground (color &optional start end)
7e6cb513 348 "Set the foreground COLOR of the region or next character typed.
4a24b314 349The color is prompted for. A face named `fg:color' is used \(or created).
7d8177cf
RS
350
351If the region is active (normally true except in Transient Mark mode)
352and there is no prefix argument, this command sets the region to the
353requested face.
354
355Otherwise, this command specifies the face for the next character
356inserted. Moving point or switching buffers before
357typing 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 (unless (color-defined-p color)
366 (message "Color `%s' undefined" color))
367 (facemenu-add-new-face color 'facemenu-foreground-menu)
368 (facemenu-add-face (list (list :foreground color)) start end))
4a24b314 369
bf7d4561 370;;;###autoload
4a24b314 371(defun facemenu-set-background (color &optional start end)
7e6cb513 372 "Set the background COLOR of the region or next character typed.
7d8177cf
RS
373Reads the color in the minibuffer.
374
375If the region is active (normally true except in Transient Mark mode)
376and there is no prefix argument, this command sets the region to the
377requested face.
378
379Otherwise, this command specifies the face for the next character
380inserted. Moving point or switching buffers before
381typing a character to insert cancels the specification."
382 (interactive (list (progn
383 (barf-if-buffer-read-only)
384 (facemenu-read-color "Background color: "))
385 (if (and mark-active (not current-prefix-arg))
386 (region-beginning))
387 (if (and mark-active (not current-prefix-arg))
388 (region-end))))
389 (unless (color-defined-p color)
390 (message "Color `%s' undefined" color))
391 (facemenu-add-new-face color 'facemenu-background-menu)
392 (facemenu-add-face (list (list :background color)) start end))
4e8aa578 393
9dc90430 394;;;###autoload
4e8aa578 395(defun facemenu-set-face-from-menu (face start end)
7e6cb513 396 "Set the FACE of the region or next character typed.
4e8aa578
RS
397This function is designed to be called from a menu; the face to use
398is the menu item's name.
f34eaa2c 399
7d8177cf
RS
400If the region is active (normally true except in Transient Mark mode)
401and there is no prefix argument, this command sets the region to the
402requested face.
f34eaa2c
KH
403
404Otherwise, this command specifies the face for the next character
405inserted. Moving point or switching buffers before
406typing a character to insert cancels the specification."
4a24b314 407 (interactive (list last-command-event
f34eaa2c
KH
408 (if (and mark-active (not current-prefix-arg))
409 (region-beginning))
410 (if (and mark-active (not current-prefix-arg))
411 (region-end))))
88d690a9 412 (barf-if-buffer-read-only)
4a24b314 413 (facemenu-get-face face)
4e8aa578 414 (if start
4a24b314 415 (facemenu-add-face face start end)
cb5bec6e 416 (facemenu-add-face face)))
4e8aa578 417
9dc90430 418;;;###autoload
4e8aa578
RS
419(defun facemenu-set-invisible (start end)
420 "Make the region invisible.
421This sets the `invisible' text property; it can be undone with
f34eaa2c 422`facemenu-remove-special'."
4e8aa578 423 (interactive "r")
0e3edd7b 424 (add-text-properties start end '(invisible t)))
4e8aa578 425
9dc90430 426;;;###autoload
4e8aa578
RS
427(defun facemenu-set-intangible (start end)
428 "Make the region intangible: disallow moving into it.
429This sets the `intangible' text property; it can be undone with
f34eaa2c 430`facemenu-remove-special'."
4e8aa578 431 (interactive "r")
0e3edd7b 432 (add-text-properties start end '(intangible t)))
4e8aa578 433
9dc90430 434;;;###autoload
4e8aa578
RS
435(defun facemenu-set-read-only (start end)
436 "Make the region unmodifiable.
437This sets the `read-only' text property; it can be undone with
f34eaa2c 438`facemenu-remove-special'."
4e8aa578 439 (interactive "r")
0e3edd7b 440 (add-text-properties start end '(read-only t)))
4e8aa578 441
9dc90430 442;;;###autoload
a32d7856
KH
443(defun facemenu-remove-face-props (start end)
444 "Remove `face' and `mouse-face' text properties."
4e8aa578
RS
445 (interactive "*r") ; error if buffer is read-only despite the next line.
446 (let ((inhibit-read-only t))
447 (remove-text-properties
a32d7856 448 start end '(face nil mouse-face nil))))
4e8aa578 449
f34eaa2c
KH
450;;;###autoload
451(defun facemenu-remove-all (start end)
452 "Remove all text properties from the region."
453 (interactive "*r") ; error if buffer is read-only despite the next line.
454 (let ((inhibit-read-only t))
455 (set-text-properties start end nil)))
456
457;;;###autoload
458(defun facemenu-remove-special (start end)
459 "Remove all the \"special\" text properties from the region.
460These special properties include `invisible', `intangible' and `read-only'."
461 (interactive "*r") ; error if buffer is read-only despite the next line.
462 (let ((inhibit-read-only t))
463 (remove-text-properties
464 start end '(invisible nil intangible nil read-only nil))))
465
c0a7db84
BG
466;;;###autoload
467(defun list-text-properties-at (p)
468 "Pop up a buffer listing text-properties at LOCATION."
469 (interactive "d")
cb5bec6e 470 (let ((props (text-properties-at p))
25a4509f 471 category
cb5bec6e 472 str)
c0a7db84
BG
473 (if (null props)
474 (message "None")
cb5bec6e 475 (if (and (not (cdr (cdr props)))
25a4509f 476 (not (eq (car props) 'category))
cb5bec6e
RS
477 (< (length (setq str (format "Text property at %d: %s %S"
478 p (car props) (car (cdr props)))))
479 (frame-width)))
f2b7756c 480 (message "%s" str)
cb5bec6e
RS
481 (with-output-to-temp-buffer "*Text Properties*"
482 (princ (format "Text properties at %d:\n\n" p))
37d5af8a 483 (setq help-xref-stack nil)
cb5bec6e 484 (while props
25a4509f
RS
485 (if (eq (car props) 'category)
486 (setq category (car (cdr props))))
cb5bec6e
RS
487 (princ (format "%-20s %S\n"
488 (car props) (car (cdr props))))
25a4509f
RS
489 (setq props (cdr (cdr props))))
490 (if category
491 (progn
492 (setq props (symbol-plist category))
493 (princ (format "\nCategory %s:\n\n" category))
494 (while props
495 (princ (format "%-20s %S\n"
496 (car props) (car (cdr props))))
497 (if (eq (car props) 'category)
498 (setq category (car (cdr props))))
499 (setq props (cdr (cdr props)))))))))))
c0a7db84 500
bf7d4561 501;;;###autoload
da627a71 502(defun facemenu-read-color (&optional prompt)
bf7d4561 503 "Read a color using the minibuffer."
da627a71 504 (let ((col (completing-read (or prompt "Color: ")
bf7d4561 505 (or facemenu-color-alist
f795f633 506 (mapcar 'list (defined-colors)))
bf7d4561
BG
507 nil t)))
508 (if (equal "" col)
509 nil
510 col)))
4e8aa578 511
88d690a9
RS
512;;;###autoload
513(defun list-colors-display (&optional list)
7c49006b
RS
514 "Display names of defined colors, and show what they look like.
515If the optional argument LIST is non-nil, it should be a list of
516colors to display. Otherwise, this command computes a list
517of colors that the current display can handle."
88d690a9 518 (interactive)
6062889d 519 (when (and (null list) (> (display-color-cells) 0))
f795f633 520 (setq list (defined-colors))
16b6c966
DL
521 ;; Delete duplicate colors.
522 (let ((l list))
523 (while (cdr l)
524 (if (facemenu-color-equal (car l) (car (cdr l)))
525 (setcdr l (cdr (cdr l)))
6062889d
EZ
526 (setq l (cdr l)))))
527 ;; Don't show more than what the display can handle.
528 (let ((lc (nthcdr (1- (display-color-cells)) list)))
529 (if lc
530 (setcdr lc nil))))
88d690a9
RS
531 (with-output-to-temp-buffer "*Colors*"
532 (save-excursion
533 (set-buffer standard-output)
7dc30d5b 534 (let (s)
88d690a9
RS
535 (while list
536 (setq s (point))
537 (insert (car list))
538 (indent-to 20)
539 (put-text-property s (point) 'face
7dc30d5b 540 (cons 'background-color (car list)))
88d690a9
RS
541 (setq s (point))
542 (insert " " (car list) "\n")
543 (put-text-property s (point) 'face
7dc30d5b 544 (cons 'foreground-color (car list)))
88d690a9
RS
545 (setq list (cdr list)))))))
546
547(defun facemenu-color-equal (a b)
548 "Return t if colors A and B are the same color.
7c49006b 549A and B should be strings naming colors.
f795f633
EZ
550This function queries the display system to find out what the color
551names mean. It returns nil if the colors differ or if it can't
7c49006b 552determine the correct answer."
88d690a9 553 (cond ((equal a b) t)
f795f633 554 ((equal (color-values a) (color-values b)))))
88d690a9 555
cb5bec6e 556(defun facemenu-add-face (face &optional start end)
4a24b314 557 "Add FACE to text between START and END.
7e6cb513 558If START is nil or START to END is empty, add FACE to next typed character
cb5bec6e
RS
559instead. For each section of that region that has a different face property,
560FACE will be consed onto it, and other faces that are completely hidden by
561that will be removed from the list.
562If `facemenu-add-face-function' and maybe `facemenu-end-add-face' are non-`nil'
563they are used to set the face information.
bf7d4561
BG
564
565As a special case, if FACE is `default', then the region is left with NO face
566text property. Otherwise, selecting the default face would not have any
cb5bec6e
RS
567effect. See `facemenu-remove-face-function'."
568 (interactive "*xFace: \nr")
569 (if (and (eq face 'default)
570 (not (eq facemenu-remove-face-function t)))
571 (if facemenu-remove-face-function
572 (funcall facemenu-remove-face-function start end)
682e437e
RS
573 (if (and start (< start end))
574 (remove-text-properties start end '(face default))
575 (setq self-insert-face 'default
576 self-insert-face-command this-command)))
cb5bec6e
RS
577 (if facemenu-add-face-function
578 (save-excursion
579 (if end (goto-char end))
580 (save-excursion
581 (if start (goto-char start))
582 (insert-before-markers
583 (funcall facemenu-add-face-function face end)))
584 (if facemenu-end-add-face
585 (insert (if (stringp facemenu-end-add-face)
586 facemenu-end-add-face
587 (funcall facemenu-end-add-face face)))))
588 (if (and start (< start end))
589 (let ((part-start start) part-end)
590 (while (not (= part-start end))
591 (setq part-end (next-single-property-change part-start 'face
592 nil end))
593 (let ((prev (get-text-property part-start 'face)))
594 (put-text-property part-start part-end 'face
595 (if (null prev)
596 face
597 (facemenu-active-faces
598 (cons face
599 (if (listp prev)
600 prev
601 (list prev)))))))
602 (setq part-start part-end)))
603 (setq self-insert-face (if (eq last-command self-insert-face-command)
604 (cons face (if (listp self-insert-face)
605 self-insert-face
606 (list self-insert-face)))
607 face)
608 self-insert-face-command this-command)))))
4a24b314 609
5a79ed26
KH
610(defun facemenu-active-faces (face-list &optional frame)
611 "Return from FACE-LIST those faces that would be used for display.
612This means each face attribute is not specified in a face earlier in FACE-LIST
613and such a face is therefore active when used to display text.
614If the optional argument FRAME is given, use the faces in that frame; otherwise
615use the selected frame. If t, then the global, non-frame faces are used."
7d8177cf
RS
616 (let* ((mask-atts (copy-sequence
617 (if (consp (car face-list))
f790dddf 618 (face-attributes-as-vector (car face-list))
7d8177cf
RS
619 (or (internal-lisp-face-p (car face-list) frame)
620 (check-face (car face-list))))))
5a79ed26
KH
621 (active-list (list (car face-list)))
622 (face-list (cdr face-list))
623 (mask-len (length mask-atts)))
624 (while face-list
7d8177cf
RS
625 (if (let ((face-atts
626 (if (consp (car face-list))
f790dddf 627 (face-attributes-as-vector (car face-list))
7d8177cf
RS
628 (or (internal-lisp-face-p (car face-list) frame)
629 (check-face (car face-list)))))
630 (i mask-len)
631 (useful nil))
5a79ed26 632 (while (> (setq i (1- i)) 1)
7d8177cf
RS
633 (and (not (memq (aref face-atts i) '(nil unspecified)))
634 (memq (aref mask-atts i) '(nil unspecified))
5a79ed26
KH
635 (aset mask-atts i (setq useful t))))
636 useful)
637 (setq active-list (cons (car face-list) active-list)))
638 (setq face-list (cdr face-list)))
639 (nreverse active-list)))
4a24b314 640
bf7d4561
BG
641(defun facemenu-get-face (symbol)
642 "Make sure FACE exists.
7d8177cf 643If not, create it and add it to the appropriate menu. Return the SYMBOL."
0351bce7
RS
644 (let ((name (symbol-name symbol))
645 foreground)
7e6cb513 646 (cond ((facep symbol))
0351bce7
RS
647 (t (make-face symbol))))
648 symbol)
bf7d4561 649
7d8177cf
RS
650(defun facemenu-add-new-face (face-or-color &optional menu)
651 "Add FACE-OR-COLOR (a face or a color) to the appropriate Face menu.
652If MENU is nil, then FACE-OR-COLOR is a face to be added
653to `facemenu-face-menu'. If MENU is `facemenu-foreground-menu'
654or `facemenu-background-menu', FACE-OR-COLOR is a color
655to be added to the specified menu.
656
657This is called whenever you create a new face."
658 (let* (name
659 symbol
660 docstring
1d792b18 661 (key (cdr (assoc face-or-color facemenu-keybindings)))
88d690a9 662 function menu-val)
7d8177cf
RS
663 (if (symbolp face-or-color)
664 (setq name (symbol-name face-or-color)
665 symbol face-or-color)
666 (setq name face-or-color
1d792b18 667 symbol (intern name)))
7d8177cf 668 (cond ((eq menu 'facemenu-foreground-menu)
536f1a10
RS
669 (setq docstring
670 (format "Select foreground color %s for subsequent insertion."
7d8177cf
RS
671 name)))
672 ((eq menu 'facemenu-background-menu)
536f1a10
RS
673 (setq docstring
674 (format "Select background color %s for subsequent insertion."
7d8177cf 675 name)))
536f1a10 676 (t
7d8177cf 677 (setq menu 'facemenu-face-menu)
536f1a10
RS
678 (setq docstring
679 (format "Select face `%s' for subsequent insertion."
7d8177cf 680 name))))
88d690a9 681 (cond ((eq t facemenu-unlisted-faces))
7d8177cf 682 ((memq symbol facemenu-unlisted-faces))
7dc30d5b
RS
683 ;; test against regexps in facemenu-unlisted-faces
684 ((let ((unlisted facemenu-unlisted-faces)
685 (matched nil))
686 (while (and unlisted (not matched))
687 (if (and (stringp (car unlisted))
688 (string-match (car unlisted) name))
689 (setq matched t)
690 (setq unlisted (cdr unlisted))))
691 matched))
88d690a9
RS
692 (key ; has a keyboard equivalent. These go at the front.
693 (setq function (intern (concat "facemenu-set-" name)))
694 (fset function
536f1a10
RS
695 `(lambda ()
696 ,docstring
697 (interactive)
7d8177cf 698 (facemenu-set-face (quote ,symbol))))
88d690a9
RS
699 (define-key 'facemenu-keymap key (cons name function))
700 (define-key menu key (cons name function)))
701 ((facemenu-iterate ; check if equivalent face is already in the menu
702 (lambda (m) (and (listp m)
703 (symbolp (car m))
7d8177cf 704 (face-equal (car m) symbol)))
88d690a9
RS
705 (cdr (symbol-function menu))))
706 (t ; No keyboard equivalent. Figure out where to put it:
7d8177cf 707 (setq key (vector symbol)
88d690a9
RS
708 function 'facemenu-set-face-from-menu
709 menu-val (symbol-function menu))
710 (if (and facemenu-new-faces-at-end
711 (> (length menu-val) 3))
712 (define-key-after menu-val key (cons name function)
713 (car (nth (- (length menu-val) 3) menu-val)))
714 (define-key menu key (cons name function))))))
715 nil) ; Return nil for facemenu-iterate
bf7d4561 716
bf7d4561 717(defun facemenu-complete-face-list (&optional oldlist)
7cd49450 718 "Return list of all faces that look different.
bf7d4561
BG
719Starts with given ALIST of faces, and adds elements only if they display
720differently from any face already on the list.
721The faces on ALIST will end up at the end of the returned list, in reverse
722order."
723 (let ((list (nreverse (mapcar 'car oldlist))))
724 (facemenu-iterate
725 (lambda (new-face)
726 (if (not (memq new-face list))
727 (setq list (cons new-face list)))
728 nil)
729 (nreverse (face-list)))
730 list))
731
7e6cb513 732(defun facemenu-iterate (func list)
4e8aa578
RS
733 "Apply FUNC to each element of LIST until one returns non-nil.
734Returns the non-nil value it found, or nil if all were nil."
7e6cb513
SM
735 (while (and list (not (funcall func (car list))))
736 (setq list (cdr list)))
737 (car list))
4e8aa578
RS
738
739(facemenu-update)
4e8aa578
RS
740
741;;; facemenu.el ends here