(Fdefvar): Don't try to set constant symbols.
[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 40;;
71296446 41;; Faces can be selected from the keyboard as well.
6be7d8db
RS
42;; The standard keybindings are M-o (or ESC o) + letter:
43;; M-o i = "set italic", M-o 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
71296446 89(eval-when-compile
0e520006
PA
90 (require 'help)
91 (require 'button))
92
9dc90430 93;;; Provide some binding for startup:
6be7d8db 94;;;###autoload (define-key global-map "\M-o" 'facemenu-keymap)
9dc90430 95;;;###autoload (autoload 'facemenu-keymap "facemenu" "Keymap for face-changing commands." t 'keymap)
71296446 96
9086c730
RS
97;; Global bindings:
98(define-key global-map [C-down-mouse-2] 'facemenu-menu)
6be7d8db 99(define-key global-map "\M-o" '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,
71296446 119except for those in `facemenu-unlisted-faces', are listed after them,
4e8aa578
RS
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)
6c763f36 165(put 'facemenu-face-menu 'menu-enable '(facemenu-enable-faces-p))
bf7d4561 166
9dc90430 167;;;###autoload
71296446 168(defvar facemenu-foreground-menu
bf7d4561 169 (let ((map (make-sparse-keymap "Foreground Color")))
f34eaa2c 170 (define-key map "o" (cons "Other..." 'facemenu-set-foreground))
bf7d4561
BG
171 map)
172 "Menu keymap for foreground colors.")
9dc90430 173;;;###autoload
88d690a9 174(defalias 'facemenu-foreground-menu facemenu-foreground-menu)
6c763f36 175(put 'facemenu-foreground-menu 'menu-enable '(facemenu-enable-faces-p))
bf7d4561 176
9dc90430 177;;;###autoload
bf7d4561
BG
178(defvar facemenu-background-menu
179 (let ((map (make-sparse-keymap "Background Color")))
f34eaa2c 180 (define-key map "o" (cons "Other..." 'facemenu-set-background))
bf7d4561 181 map)
7e6cb513 182 "Menu keymap for background colors.")
9dc90430 183;;;###autoload
88d690a9 184(defalias 'facemenu-background-menu facemenu-background-menu)
6c763f36
RS
185(put 'facemenu-background-menu 'menu-enable '(facemenu-enable-faces-p))
186
187;;; Condition for enabling menu items that set faces.
188(defun facemenu-enable-faces-p ()
189 (not (and font-lock-mode font-lock-defaults)))
bf7d4561 190
9dc90430 191;;;###autoload
71296446 192(defvar facemenu-special-menu
bf7d4561 193 (let ((map (make-sparse-keymap "Special")))
2d07ff84
DL
194 (define-key map [?s] (cons (purecopy "Remove Special")
195 'facemenu-remove-special))
196 (define-key map [?t] (cons (purecopy "Intangible")
197 'facemenu-set-intangible))
198 (define-key map [?v] (cons (purecopy "Invisible")
199 'facemenu-set-invisible))
200 (define-key map [?r] (cons (purecopy "Read-Only")
201 'facemenu-set-read-only))
bf7d4561
BG
202 map)
203 "Menu keymap for non-face text-properties.")
9dc90430 204;;;###autoload
88d690a9
RS
205(defalias 'facemenu-special-menu facemenu-special-menu)
206
9dc90430 207;;;###autoload
88d690a9
RS
208(defvar facemenu-justification-menu
209 (let ((map (make-sparse-keymap "Justification")))
2d07ff84
DL
210 (define-key map [?c] (cons (purecopy "Center") 'set-justification-center))
211 (define-key map [?b] (cons (purecopy "Full") 'set-justification-full))
212 (define-key map [?r] (cons (purecopy "Right") 'set-justification-right))
213 (define-key map [?l] (cons (purecopy "Left") 'set-justification-left))
214 (define-key map [?u] (cons (purecopy "Unfilled") 'set-justification-none))
88d690a9
RS
215 map)
216 "Submenu for text justification commands.")
9dc90430 217;;;###autoload
88d690a9
RS
218(defalias 'facemenu-justification-menu facemenu-justification-menu)
219
9dc90430 220;;;###autoload
88d690a9
RS
221(defvar facemenu-indentation-menu
222 (let ((map (make-sparse-keymap "Indentation")))
71296446 223 (define-key map [decrease-right-margin]
2d07ff84 224 (cons (purecopy "Indent Right Less") 'decrease-right-margin))
f34eaa2c 225 (define-key map [increase-right-margin]
2d07ff84 226 (cons (purecopy "Indent Right More") 'increase-right-margin))
f34eaa2c 227 (define-key map [decrease-left-margin]
2d07ff84 228 (cons (purecopy "Indent Less") 'decrease-left-margin))
f34eaa2c 229 (define-key map [increase-left-margin]
2d07ff84 230 (cons (purecopy "Indent More") 'increase-left-margin))
88d690a9
RS
231 map)
232 "Submenu for indentation commands.")
9dc90430 233;;;###autoload
88d690a9 234(defalias 'facemenu-indentation-menu facemenu-indentation-menu)
bf7d4561 235
f34eaa2c 236;; This is split up to avoid an overlong line in loaddefs.el.
9dc90430 237;;;###autoload
f34eaa2c 238(defvar facemenu-menu nil
535d2617 239 "Facemenu top-level menu keymap.")
9dc90430 240;;;###autoload
f34eaa2c
KH
241(setq facemenu-menu (make-sparse-keymap "Text Properties"))
242;;;###autoload
243(let ((map facemenu-menu))
2d07ff84
DL
244 (define-key map [dc] (cons (purecopy "Display Colors") 'list-colors-display))
245 (define-key map [df] (cons (purecopy "Display Faces") 'list-faces-display))
cea5ec30
RS
246 (define-key map [dp] (cons (purecopy "Describe Properties")
247 'describe-text-properties))
2d07ff84
DL
248 (define-key map [ra] (cons (purecopy "Remove Text Properties")
249 'facemenu-remove-all))
250 (define-key map [rm] (cons (purecopy "Remove Face Properties")
251 'facemenu-remove-face-props))
252 (define-key map [s1] (list (purecopy "--"))))
f34eaa2c
KH
253;;;###autoload
254(let ((map facemenu-menu))
71296446 255 (define-key map [in] (cons (purecopy "Indentation")
2d07ff84
DL
256 'facemenu-indentation-menu))
257 (define-key map [ju] (cons (purecopy "Justification")
258 'facemenu-justification-menu))
259 (define-key map [s2] (list (purecopy "--")))
71296446 260 (define-key map [sp] (cons (purecopy "Special Properties")
2d07ff84 261 'facemenu-special-menu))
71296446 262 (define-key map [bg] (cons (purecopy "Background Color")
2d07ff84 263 'facemenu-background-menu))
71296446 264 (define-key map [fg] (cons (purecopy "Foreground Color")
2d07ff84 265 'facemenu-foreground-menu))
71296446 266 (define-key map [fc] (cons (purecopy "Face")
2d07ff84 267 'facemenu-face-menu)))
f34eaa2c 268;;;###autoload
88d690a9 269(defalias 'facemenu-menu facemenu-menu)
bf7d4561 270
71296446 271(defvar facemenu-keymap
88d690a9 272 (let ((map (make-sparse-keymap "Set face")))
2d07ff84 273 (define-key map "o" (cons (purecopy "Other...") 'facemenu-set-face))
88d690a9 274 map)
9dc90430 275 "Keymap for face-changing commands.
bf7d4561 276`Facemenu-update' fills in the keymap according to the bindings
535d2617 277requested in `facemenu-keybindings'.")
88d690a9 278(defalias 'facemenu-keymap facemenu-keymap)
bf7d4561 279
cb5bec6e 280
487e6fcb 281(defcustom facemenu-add-face-function nil
7e6cb513 282 "Function called at beginning of text to change or nil.
cb5bec6e 283This function is passed the FACE to set and END of text to change, and must
487e6fcb
RS
284return a string which is inserted. It may set `facemenu-end-add-face'."
285 :type '(choice (const :tag "None" nil)
286 function)
287 :group 'facemenu)
cb5bec6e 288
487e6fcb 289(defcustom facemenu-end-add-face nil
7e6cb513 290 "String to insert or function called at end of text to change or nil.
cb5bec6e 291This function is passed the FACE to set, and must return a string which is
487e6fcb
RS
292inserted."
293 :type '(choice (const :tag "None" nil)
294 string
295 function)
296 :group 'facemenu)
cb5bec6e 297
487e6fcb 298(defcustom facemenu-remove-face-function nil
9086c730 299 "When non-nil, this is a function called to remove faces.
cb5bec6e 300This function is passed the START and END of text to change.
7e6cb513 301May also be t meaning to use `facemenu-add-face-function'."
487e6fcb
RS
302 :type '(choice (const :tag "None" nil)
303 (const :tag "Use add-face" t)
304 function)
305 :group 'facemenu)
cb5bec6e 306
bf7d4561
BG
307;;; Internal Variables
308
309(defvar facemenu-color-alist nil
310 ;; Don't initialize here; that doesn't work if preloaded.
311 "Alist of colors, used for completion.
312If null, `facemenu-read-color' will set it.")
4a24b314 313
4e8aa578 314(defun facemenu-update ()
bf7d4561
BG
315 "Add or update the \"Face\" menu in the menu bar.
316You can call this to update things if you change any of the menu configuration
317variables."
4e8aa578 318 (interactive)
4e8aa578 319
bf7d4561
BG
320 ;; Add each defined face to the menu.
321 (facemenu-iterate 'facemenu-add-new-face
322 (facemenu-complete-face-list facemenu-keybindings)))
4a24b314 323
4e8aa578
RS
324;;;###autoload
325(defun facemenu-set-face (face &optional start end)
4a24b314 326 "Add FACE to the region or next character typed.
7d8177cf 327This adds FACE to the top of the face list; any faces lower on the list that
4a24b314
RS
328will not show through at all will be removed.
329
7d8177cf 330Interactively, reads the face name with the minibuffer.
f34eaa2c 331
7d8177cf
RS
332If the region is active (normally true except in Transient Mark mode)
333and there is no prefix argument, this command sets the region to the
334requested face.
f34eaa2c
KH
335
336Otherwise, this command specifies the face for the next character
337inserted. Moving point or switching buffers before
71296446 338typing a character to insert cancels the specification."
7d8177cf
RS
339 (interactive (list (progn
340 (barf-if-buffer-read-only)
341 (read-face-name "Use face"))
342 (if (and mark-active (not current-prefix-arg))
343 (region-beginning))
344 (if (and mark-active (not current-prefix-arg))
345 (region-end))))
88d690a9 346 (facemenu-add-new-face face)
7d8177cf 347 (facemenu-add-face face start end))
4a24b314 348
bf7d4561 349;;;###autoload
4a24b314 350(defun facemenu-set-foreground (color &optional start end)
7e6cb513 351 "Set the foreground COLOR of the region or next character typed.
af1eab21 352This command reads the color in the minibuffer.
7d8177cf
RS
353
354If the region is active (normally true except in Transient Mark mode)
355and there is no prefix argument, this command sets the region to the
356requested face.
357
358Otherwise, this command specifies the face for the next character
359inserted. Moving point or switching buffers before
71296446 360typing a character to insert cancels the specification."
7d8177cf
RS
361 (interactive (list (progn
362 (barf-if-buffer-read-only)
363 (facemenu-read-color "Foreground color: "))
364 (if (and mark-active (not current-prefix-arg))
365 (region-beginning))
366 (if (and mark-active (not current-prefix-arg))
367 (region-end))))
368 (unless (color-defined-p color)
369 (message "Color `%s' undefined" color))
9bf4c4e5 370 (facemenu-add-new-color color 'facemenu-foreground-menu)
7d8177cf 371 (facemenu-add-face (list (list :foreground color)) start end))
4a24b314 372
bf7d4561 373;;;###autoload
4a24b314 374(defun facemenu-set-background (color &optional start end)
7e6cb513 375 "Set the background COLOR of the region or next character typed.
af1eab21 376This command reads the color in the minibuffer.
7d8177cf
RS
377
378If the region is active (normally true except in Transient Mark mode)
379and there is no prefix argument, this command sets the region to the
380requested face.
381
382Otherwise, this command specifies the face for the next character
383inserted. Moving point or switching buffers before
71296446 384typing a character to insert cancels the specification."
7d8177cf
RS
385 (interactive (list (progn
386 (barf-if-buffer-read-only)
387 (facemenu-read-color "Background color: "))
388 (if (and mark-active (not current-prefix-arg))
389 (region-beginning))
390 (if (and mark-active (not current-prefix-arg))
391 (region-end))))
392 (unless (color-defined-p color)
393 (message "Color `%s' undefined" color))
9bf4c4e5 394 (facemenu-add-new-color color 'facemenu-background-menu)
7d8177cf 395 (facemenu-add-face (list (list :background color)) start end))
4e8aa578 396
9dc90430 397;;;###autoload
4e8aa578 398(defun facemenu-set-face-from-menu (face start end)
7e6cb513 399 "Set the FACE of the region or next character typed.
4e8aa578
RS
400This function is designed to be called from a menu; the face to use
401is the menu item's name.
f34eaa2c 402
7d8177cf
RS
403If the region is active (normally true except in Transient Mark mode)
404and there is no prefix argument, this command sets the region to the
405requested face.
f34eaa2c
KH
406
407Otherwise, this command specifies the face for the next character
408inserted. Moving point or switching buffers before
71296446 409typing a character to insert cancels the specification."
4a24b314 410 (interactive (list last-command-event
f34eaa2c
KH
411 (if (and mark-active (not current-prefix-arg))
412 (region-beginning))
413 (if (and mark-active (not current-prefix-arg))
414 (region-end))))
88d690a9 415 (barf-if-buffer-read-only)
4a24b314 416 (facemenu-get-face face)
71296446 417 (if start
4a24b314 418 (facemenu-add-face face start end)
cb5bec6e 419 (facemenu-add-face face)))
4e8aa578 420
9dc90430 421;;;###autoload
4e8aa578
RS
422(defun facemenu-set-invisible (start end)
423 "Make the region invisible.
424This sets the `invisible' text property; it can be undone with
f34eaa2c 425`facemenu-remove-special'."
4e8aa578 426 (interactive "r")
0e3edd7b 427 (add-text-properties start end '(invisible t)))
4e8aa578 428
9dc90430 429;;;###autoload
4e8aa578
RS
430(defun facemenu-set-intangible (start end)
431 "Make the region intangible: disallow moving into it.
432This sets the `intangible' text property; it can be undone with
f34eaa2c 433`facemenu-remove-special'."
4e8aa578 434 (interactive "r")
0e3edd7b 435 (add-text-properties start end '(intangible t)))
4e8aa578 436
9dc90430 437;;;###autoload
4e8aa578
RS
438(defun facemenu-set-read-only (start end)
439 "Make the region unmodifiable.
440This sets the `read-only' text property; it can be undone with
f34eaa2c 441`facemenu-remove-special'."
4e8aa578 442 (interactive "r")
0e3edd7b 443 (add-text-properties start end '(read-only t)))
4e8aa578 444
9dc90430 445;;;###autoload
a32d7856
KH
446(defun facemenu-remove-face-props (start end)
447 "Remove `face' and `mouse-face' text properties."
4e8aa578
RS
448 (interactive "*r") ; error if buffer is read-only despite the next line.
449 (let ((inhibit-read-only t))
71296446 450 (remove-text-properties
a32d7856 451 start end '(face nil mouse-face nil))))
4e8aa578 452
f34eaa2c
KH
453;;;###autoload
454(defun facemenu-remove-all (start end)
455 "Remove all text properties from the region."
456 (interactive "*r") ; error if buffer is read-only despite the next line.
457 (let ((inhibit-read-only t))
458 (set-text-properties start end nil)))
459
460;;;###autoload
461(defun facemenu-remove-special (start end)
462 "Remove all the \"special\" text properties from the region.
463These special properties include `invisible', `intangible' and `read-only'."
464 (interactive "*r") ; error if buffer is read-only despite the next line.
465 (let ((inhibit-read-only t))
71296446 466 (remove-text-properties
f34eaa2c 467 start end '(invisible nil intangible nil read-only nil))))
0af1db42 468\f
bf7d4561 469;;;###autoload
da627a71 470(defun facemenu-read-color (&optional prompt)
bf7d4561 471 "Read a color using the minibuffer."
71296446 472 (let ((col (completing-read (or prompt "Color: ")
bf7d4561 473 (or facemenu-color-alist
1ff4ace5 474 (defined-colors))
bf7d4561
BG
475 nil t)))
476 (if (equal "" col)
477 nil
478 col)))
4e8aa578 479
88d690a9 480;;;###autoload
066a23af 481(defun list-colors-display (&optional list buffer-name)
7c49006b
RS
482 "Display names of defined colors, and show what they look like.
483If the optional argument LIST is non-nil, it should be a list of
066a23af
JL
484colors to display. Otherwise, this command computes a list of
485colors that the current display can handle. If the optional
486argument BUFFER-NAME is nil, it defaults to *Colors*."
88d690a9 487 (interactive)
6062889d 488 (when (and (null list) (> (display-color-cells) 0))
066a23af 489 (setq list (list-colors-duplicates (defined-colors)))
d2596700
MB
490 (when (memq (display-visual-class) '(gray-scale pseudo-color direct-color))
491 ;; Don't show more than what the display can handle.
492 (let ((lc (nthcdr (1- (display-color-cells)) list)))
493 (if lc
494 (setcdr lc nil)))))
066a23af 495 (with-output-to-temp-buffer (or buffer-name "*Colors*")
88d690a9
RS
496 (save-excursion
497 (set-buffer standard-output)
066a23af 498 (setq truncate-lines t)
987d1819
JL
499 (if temp-buffer-show-function
500 (list-colors-print list)
501 ;; Call list-colors-print from temp-buffer-show-hook
502 ;; to get the right value of window-width in list-colors-print
503 ;; after the buffer is displayed.
504 (add-hook 'temp-buffer-show-hook
505 (lambda () (list-colors-print list)) nil t)))))
506
507(defun list-colors-print (list)
508 (dolist (color list)
509 (if (consp color)
510 (if (cdr color)
511 (setq color (sort color (lambda (a b)
512 (string< (downcase a)
513 (downcase b))))))
514 (setq color (list color)))
515 (put-text-property
516 (prog1 (point)
517 (insert (car color))
518 (indent-to 22))
519 (point)
520 'face (cons 'background-color (car color)))
521 (put-text-property
522 (prog1 (point)
51d23bd0
JL
523 (insert " " (if (cdr color)
524 (mapconcat 'identity (cdr color) ", ")
525 (car color))))
987d1819 526 (point)
51d23bd0
JL
527 'face (cons 'foreground-color (car color)))
528 (indent-to (max (- (window-width) 8) 44))
529 (insert (apply 'format "#%02x%02x%02x"
530 (mapcar (lambda (c) (lsh c -8))
531 (color-values (car color)))))
532
533 (insert "\n"))
987d1819 534 (goto-char (point-min)))
066a23af
JL
535
536(defun list-colors-duplicates (&optional list)
537 "Return a list of colors with grouped duplicate colors.
538If a color has no duplicates, then the element of the returned list
539has the form '(COLOR-NAME). The element of the returned list with
540duplicate colors has the form '(COLOR-NAME DUPLICATE-COLOR-NAME ...).
541This function uses the predicate `facemenu-color-equal' to compare
542color names. If the optional argument LIST is non-nil, it should
543be a list of colors to display. Otherwise, this function uses
544a list of colors that the current display can handle."
545 (let* ((list (mapcar 'list (or list (defined-colors))))
546 (l list))
547 (while (cdr l)
548 (if (and (facemenu-color-equal (car (car l)) (car (car (cdr l))))
51d23bd0
JL
549 (not (if (boundp 'w32-default-color-map)
550 (not (assoc (car (car l)) w32-default-color-map)))))
066a23af
JL
551 (progn
552 (setcdr (car l) (cons (car (car (cdr l))) (cdr (car l))))
553 (setcdr l (cdr (cdr l))))
554 (setq l (cdr l))))
555 list))
88d690a9
RS
556
557(defun facemenu-color-equal (a b)
558 "Return t if colors A and B are the same color.
7c49006b 559A and B should be strings naming colors.
f795f633
EZ
560This function queries the display system to find out what the color
561names mean. It returns nil if the colors differ or if it can't
7c49006b 562determine the correct answer."
88d690a9 563 (cond ((equal a b) t)
f795f633 564 ((equal (color-values a) (color-values b)))))
88d690a9 565
cb5bec6e 566(defun facemenu-add-face (face &optional start end)
4a24b314 567 "Add FACE to text between START and END.
7e6cb513 568If START is nil or START to END is empty, add FACE to next typed character
cb5bec6e
RS
569instead. For each section of that region that has a different face property,
570FACE will be consed onto it, and other faces that are completely hidden by
571that will be removed from the list.
af1eab21 572If `facemenu-add-face-function' and maybe `facemenu-end-add-face' are non-nil,
cb5bec6e 573they are used to set the face information.
bf7d4561
BG
574
575As a special case, if FACE is `default', then the region is left with NO face
576text property. Otherwise, selecting the default face would not have any
cb5bec6e
RS
577effect. See `facemenu-remove-face-function'."
578 (interactive "*xFace: \nr")
579 (if (and (eq face 'default)
580 (not (eq facemenu-remove-face-function t)))
581 (if facemenu-remove-face-function
582 (funcall facemenu-remove-face-function start end)
682e437e
RS
583 (if (and start (< start end))
584 (remove-text-properties start end '(face default))
585 (setq self-insert-face 'default
586 self-insert-face-command this-command)))
cb5bec6e
RS
587 (if facemenu-add-face-function
588 (save-excursion
589 (if end (goto-char end))
590 (save-excursion
591 (if start (goto-char start))
592 (insert-before-markers
593 (funcall facemenu-add-face-function face end)))
594 (if facemenu-end-add-face
595 (insert (if (stringp facemenu-end-add-face)
596 facemenu-end-add-face
597 (funcall facemenu-end-add-face face)))))
598 (if (and start (< start end))
599 (let ((part-start start) part-end)
600 (while (not (= part-start end))
601 (setq part-end (next-single-property-change part-start 'face
602 nil end))
603 (let ((prev (get-text-property part-start 'face)))
604 (put-text-property part-start part-end 'face
605 (if (null prev)
606 face
607 (facemenu-active-faces
608 (cons face
609 (if (listp prev)
610 prev
611 (list prev)))))))
612 (setq part-start part-end)))
613 (setq self-insert-face (if (eq last-command self-insert-face-command)
614 (cons face (if (listp self-insert-face)
615 self-insert-face
616 (list self-insert-face)))
617 face)
618 self-insert-face-command this-command)))))
4a24b314 619
5a79ed26
KH
620(defun facemenu-active-faces (face-list &optional frame)
621 "Return from FACE-LIST those faces that would be used for display.
622This means each face attribute is not specified in a face earlier in FACE-LIST
623and such a face is therefore active when used to display text.
624If the optional argument FRAME is given, use the faces in that frame; otherwise
625use the selected frame. If t, then the global, non-frame faces are used."
7d8177cf
RS
626 (let* ((mask-atts (copy-sequence
627 (if (consp (car face-list))
f790dddf 628 (face-attributes-as-vector (car face-list))
7d8177cf
RS
629 (or (internal-lisp-face-p (car face-list) frame)
630 (check-face (car face-list))))))
5a79ed26
KH
631 (active-list (list (car face-list)))
632 (face-list (cdr face-list))
633 (mask-len (length mask-atts)))
634 (while face-list
7d8177cf
RS
635 (if (let ((face-atts
636 (if (consp (car face-list))
f790dddf 637 (face-attributes-as-vector (car face-list))
7d8177cf
RS
638 (or (internal-lisp-face-p (car face-list) frame)
639 (check-face (car face-list)))))
640 (i mask-len)
641 (useful nil))
240c0c90 642 (while (>= (setq i (1- i)) 0)
7d8177cf
RS
643 (and (not (memq (aref face-atts i) '(nil unspecified)))
644 (memq (aref mask-atts i) '(nil unspecified))
5a79ed26
KH
645 (aset mask-atts i (setq useful t))))
646 useful)
647 (setq active-list (cons (car face-list) active-list)))
648 (setq face-list (cdr face-list)))
649 (nreverse active-list)))
4a24b314 650
bf7d4561
BG
651(defun facemenu-get-face (symbol)
652 "Make sure FACE exists.
7d8177cf 653If not, create it and add it to the appropriate menu. Return the SYMBOL."
c7bce5f2 654 (let ((name (symbol-name symbol)))
7e6cb513 655 (cond ((facep symbol))
0351bce7
RS
656 (t (make-face symbol))))
657 symbol)
bf7d4561 658
9bf4c4e5
RS
659(defun facemenu-add-new-face (face)
660 "Add FACE (a face) to the Face menu.
7d8177cf
RS
661
662This is called whenever you create a new face."
663 (let* (name
664 symbol
9bf4c4e5
RS
665 menu docstring
666 (key (cdr (assoc face facemenu-keybindings)))
88d690a9 667 function menu-val)
9bf4c4e5
RS
668 (if (symbolp face)
669 (setq name (symbol-name face)
670 symbol face)
671 (setq name face
1d792b18 672 symbol (intern name)))
9bf4c4e5
RS
673 (setq menu 'facemenu-face-menu)
674 (setq docstring
675 (format "Select face `%s' for subsequent insertion."
676 name))
88d690a9 677 (cond ((eq t facemenu-unlisted-faces))
7d8177cf 678 ((memq symbol facemenu-unlisted-faces))
7dc30d5b
RS
679 ;; test against regexps in facemenu-unlisted-faces
680 ((let ((unlisted facemenu-unlisted-faces)
681 (matched nil))
682 (while (and unlisted (not matched))
683 (if (and (stringp (car unlisted))
684 (string-match (car unlisted) name))
685 (setq matched t)
686 (setq unlisted (cdr unlisted))))
687 matched))
88d690a9
RS
688 (key ; has a keyboard equivalent. These go at the front.
689 (setq function (intern (concat "facemenu-set-" name)))
690 (fset function
536f1a10
RS
691 `(lambda ()
692 ,docstring
693 (interactive)
af1eab21
RS
694 (facemenu-set-face
695 (quote ,symbol)
696 (if (and mark-active (not current-prefix-arg))
697 (region-beginning))
698 (if (and mark-active (not current-prefix-arg))
699 (region-end)))))
88d690a9
RS
700 (define-key 'facemenu-keymap key (cons name function))
701 (define-key menu key (cons name function)))
702 ((facemenu-iterate ; check if equivalent face is already in the menu
71296446 703 (lambda (m) (and (listp m)
88d690a9 704 (symbolp (car m))
7d8177cf 705 (face-equal (car m) symbol)))
88d690a9
RS
706 (cdr (symbol-function menu))))
707 (t ; No keyboard equivalent. Figure out where to put it:
7d8177cf 708 (setq key (vector symbol)
88d690a9
RS
709 function 'facemenu-set-face-from-menu
710 menu-val (symbol-function menu))
711 (if (and facemenu-new-faces-at-end
712 (> (length menu-val) 3))
713 (define-key-after menu-val key (cons name function)
9bf4c4e5
RS
714 (car (nth (- (length menu-val) 3) menu-val)))
715 (define-key menu key (cons name function))))))
716 nil) ; Return nil for facemenu-iterate
717
718(defun facemenu-add-new-color (color &optional menu)
719 "Add COLOR (a color name string) to the appropriate Face menu.
720MENU should be `facemenu-foreground-menu' or
721`facemenu-background-menu'.
722
723This is called whenever you use a new color."
724 (let* (name
725 symbol
726 docstring
727 function menu-val key
728 (color-p (memq menu '(facemenu-foreground-menu
729 facemenu-background-menu))))
730 (unless (stringp color)
731 (error "%s is not a color" color))
732 (setq name color
733 symbol (intern name))
734
735 (cond ((eq menu 'facemenu-foreground-menu)
736 (setq docstring
737 (format "Select foreground color %s for subsequent insertion."
738 name)))
739 ((eq menu 'facemenu-background-menu)
740 (setq docstring
741 (format "Select background color %s for subsequent insertion."
742 name))))
743 (cond ((facemenu-iterate ; check if equivalent face is already in the menu
71296446 744 (lambda (m) (and (listp m)
9bf4c4e5
RS
745 (symbolp (car m))
746 (stringp (cadr m))
747 (string-equal (cadr m) color)))
748 (cdr (symbol-function menu))))
749 (t ; No keyboard equivalent. Figure out where to put it:
750 (setq key (vector symbol)
751 function 'facemenu-set-face-from-menu
752 menu-val (symbol-function menu))
753 (if (and facemenu-new-faces-at-end
754 (> (length menu-val) 3))
755 (define-key-after menu-val key (cons name function)
88d690a9
RS
756 (car (nth (- (length menu-val) 3) menu-val)))
757 (define-key menu key (cons name function))))))
758 nil) ; Return nil for facemenu-iterate
bf7d4561 759
bf7d4561 760(defun facemenu-complete-face-list (&optional oldlist)
7cd49450 761 "Return list of all faces that look different.
71296446 762Starts with given ALIST of faces, and adds elements only if they display
bf7d4561 763differently from any face already on the list.
71296446 764The faces on ALIST will end up at the end of the returned list, in reverse
bf7d4561
BG
765order."
766 (let ((list (nreverse (mapcar 'car oldlist))))
71296446
JB
767 (facemenu-iterate
768 (lambda (new-face)
bf7d4561
BG
769 (if (not (memq new-face list))
770 (setq list (cons new-face list)))
771 nil)
772 (nreverse (face-list)))
773 list))
774
7e6cb513 775(defun facemenu-iterate (func list)
4e8aa578
RS
776 "Apply FUNC to each element of LIST until one returns non-nil.
777Returns the non-nil value it found, or nil if all were nil."
7e6cb513
SM
778 (while (and list (not (funcall func (car list))))
779 (setq list (cdr list)))
780 (car list))
4e8aa578
RS
781
782(facemenu-update)
4e8aa578 783
1ff4ace5 784(provide 'facemenu)
ab5796a9
MB
785
786;;; arch-tag: 85f6d02b-9085-420e-b651-0678f0e9c7eb
4e8aa578 787;;; facemenu.el ends here