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