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