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