* lisp/facemenu.el (list-colors-display): Doc fix (minor rephrasing).
[bpt/emacs.git] / lisp / facemenu.el
CommitLineData
be010748 1;;; facemenu.el --- create a face menu for interactively adding fonts to text
b578f267 2
acaf905b 3;; Copyright (C) 1994-1996, 2001-2012 Free Software Foundation, Inc.
4e8aa578 4
5762abec 5;; Author: Boris Goldowsky <boris@gnu.org>
4e8aa578 6;; Keywords: faces
bd78fa1d 7;; Package: emacs
4e8aa578
RS
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
6bdad9ae 102 (mapcar 'purecopy
4e8aa578
RS
103 '((default . "d")
104 (bold . "b")
105 (italic . "i")
88d690a9 106 (bold-italic . "l") ; {bold} intersect {italic} = {l}
6bdad9ae 107 (underline . "u")))
220c969f 108 "Alist of interesting faces and keybindings.
4e8aa578
RS
109Each element is itself a list: the car is the name of the face,
110the next element is the key to use as a keyboard equivalent of the menu item;
9086c730 111the binding is made in `facemenu-keymap'.
4e8aa578
RS
112
113The faces specifically mentioned in this list are put at the top of
b6a67507
CY
114the menu, in the order specified. All other faces which are defined
115in `facemenu-listed-faces' are listed after them, but get no
116keyboard equivalents.
4e8aa578
RS
117
118If you change this variable after loading facemenu.el, you will need to call
487e6fcb
RS
119`facemenu-update' to make it take effect."
120 :type '(repeat (cons face string))
121 :group 'facemenu)
4e8aa578 122
487e6fcb 123(defcustom facemenu-new-faces-at-end t
9201cc28 124 "Where in the menu to insert newly-created faces.
88d690a9 125This should be nil to put them at the top of the menu, or t to put them
487e6fcb
RS
126just before \"Other\" at the end."
127 :type 'boolean
128 :group 'facemenu)
88d690a9 129
ed50f0d2
CY
130(defvar facemenu-unlisted-faces
131 `(modeline region secondary-selection highlight scratch-face
132 ,(purecopy "^font-lock-") ,(purecopy "^gnus-") ,(purecopy "^message-")
133 ,(purecopy "^ediff-") ,(purecopy "^term-") ,(purecopy "^vc-")
134 ,(purecopy "^widget-") ,(purecopy "^custom-") ,(purecopy "^vm-"))
d9f2959e
JB
135 "*List of faces that are of no interest to the user.")
136(make-obsolete-variable 'facemenu-unlisted-faces 'facemenu-listed-faces
b8a2c14a 137 "22.1,\n and has no effect on the Face menu")
ed50f0d2 138
b6a67507 139(defcustom facemenu-listed-faces nil
9201cc28 140 "List of faces to include in the Face menu.
d7beaf53 141Each element should be a symbol, the name of a face.
b6a67507 142The \"basic \" faces in `facemenu-keybindings' are automatically
d7beaf53
RS
143added to the Face menu, and need not be in this list.
144
145This value takes effect when you load facemenu.el. If the
146list includes symbols which are not defined as faces, they
147are ignored; however, subsequently defining or creating
148those faces adds them to the menu then. You can call
149`facemenu-update' to recalculate the menu contents, such as
150if you change the value of this variable,
151
152If this variable is t, all faces that you apply to text
153using the face menu commands (even by name), and all faces
154that you define or create, are added to the menu. You may
155find it useful to set this variable to t temporarily while
156you define some faces, so that they will be added. However,
157if the value is no longer t and you call `facemenu-update',
158it will remove any faces not explicitly in the list."
b6a67507
CY
159 :type '(choice (const :tag "List all faces" t)
160 (const :tag "None" nil)
161 (repeat symbol))
162 :group 'facemenu
163 :version "22.1")
88d690a9
RS
164
165(defvar facemenu-face-menu
bf7d4561 166 (let ((map (make-sparse-keymap "Face")))
1e8780b1 167 (define-key map "o" (cons (purecopy "Other...") 'facemenu-set-face))
bf7d4561
BG
168 map)
169 "Menu keymap for faces.")
88d690a9 170(defalias 'facemenu-face-menu facemenu-face-menu)
6c763f36 171(put 'facemenu-face-menu 'menu-enable '(facemenu-enable-faces-p))
bf7d4561 172
71296446 173(defvar facemenu-foreground-menu
bf7d4561 174 (let ((map (make-sparse-keymap "Foreground Color")))
1e8780b1 175 (define-key map "o" (cons (purecopy "Other...") 'facemenu-set-foreground))
bf7d4561
BG
176 map)
177 "Menu keymap for foreground colors.")
88d690a9 178(defalias 'facemenu-foreground-menu facemenu-foreground-menu)
6c763f36 179(put 'facemenu-foreground-menu 'menu-enable '(facemenu-enable-faces-p))
bf7d4561
BG
180
181(defvar facemenu-background-menu
182 (let ((map (make-sparse-keymap "Background Color")))
1e8780b1 183 (define-key map "o" (cons (purecopy "Other...") 'facemenu-set-background))
bf7d4561 184 map)
7e6cb513 185 "Menu keymap for background colors.")
88d690a9 186(defalias 'facemenu-background-menu facemenu-background-menu)
6c763f36
RS
187(put 'facemenu-background-menu 'menu-enable '(facemenu-enable-faces-p))
188
189;;; Condition for enabling menu items that set faces.
190(defun facemenu-enable-faces-p ()
673c1168
CY
191 ;; Enable the facemenu if facemenu-add-face-function is defined
192 ;; (e.g. in Tex-mode and SGML mode), or if font-lock is off.
193 (or (not (and font-lock-mode font-lock-defaults))
194 facemenu-add-face-function))
bf7d4561 195
71296446 196(defvar facemenu-special-menu
bf7d4561 197 (let ((map (make-sparse-keymap "Special")))
2d07ff84
DL
198 (define-key map [?s] (cons (purecopy "Remove Special")
199 'facemenu-remove-special))
200 (define-key map [?t] (cons (purecopy "Intangible")
201 'facemenu-set-intangible))
202 (define-key map [?v] (cons (purecopy "Invisible")
203 'facemenu-set-invisible))
204 (define-key map [?r] (cons (purecopy "Read-Only")
205 'facemenu-set-read-only))
bf7d4561
BG
206 map)
207 "Menu keymap for non-face text-properties.")
88d690a9
RS
208(defalias 'facemenu-special-menu facemenu-special-menu)
209
210(defvar facemenu-justification-menu
211 (let ((map (make-sparse-keymap "Justification")))
2d07ff84
DL
212 (define-key map [?c] (cons (purecopy "Center") 'set-justification-center))
213 (define-key map [?b] (cons (purecopy "Full") 'set-justification-full))
214 (define-key map [?r] (cons (purecopy "Right") 'set-justification-right))
215 (define-key map [?l] (cons (purecopy "Left") 'set-justification-left))
216 (define-key map [?u] (cons (purecopy "Unfilled") 'set-justification-none))
88d690a9
RS
217 map)
218 "Submenu for text justification commands.")
219(defalias 'facemenu-justification-menu facemenu-justification-menu)
220
221(defvar facemenu-indentation-menu
222 (let ((map (make-sparse-keymap "Indentation")))
71296446 223 (define-key map [decrease-right-margin]
2d07ff84 224 (cons (purecopy "Indent Right Less") 'decrease-right-margin))
f34eaa2c 225 (define-key map [increase-right-margin]
2d07ff84 226 (cons (purecopy "Indent Right More") 'increase-right-margin))
f34eaa2c 227 (define-key map [decrease-left-margin]
2d07ff84 228 (cons (purecopy "Indent Less") 'decrease-left-margin))
f34eaa2c 229 (define-key map [increase-left-margin]
2d07ff84 230 (cons (purecopy "Indent More") 'increase-left-margin))
88d690a9
RS
231 map)
232 "Submenu for indentation commands.")
233(defalias 'facemenu-indentation-menu facemenu-indentation-menu)
bf7d4561 234
f34eaa2c 235;; This is split up to avoid an overlong line in loaddefs.el.
f34eaa2c 236(defvar facemenu-menu nil
535d2617 237 "Facemenu top-level menu keymap.")
f34eaa2c 238(setq facemenu-menu (make-sparse-keymap "Text Properties"))
f34eaa2c 239(let ((map facemenu-menu))
2d07ff84
DL
240 (define-key map [dc] (cons (purecopy "Display Colors") 'list-colors-display))
241 (define-key map [df] (cons (purecopy "Display Faces") 'list-faces-display))
cea5ec30
RS
242 (define-key map [dp] (cons (purecopy "Describe Properties")
243 'describe-text-properties))
b322f63a
LMI
244 (define-key map [ra] (list 'menu-item (purecopy "Remove Text Properties")
245 'facemenu-remove-all
246 :enable 'mark-active))
247 (define-key map [rm] (list 'menu-item (purecopy "Remove Face Properties")
248 'facemenu-remove-face-props
249 :enable 'mark-active))
2d07ff84 250 (define-key map [s1] (list (purecopy "--"))))
f34eaa2c 251(let ((map facemenu-menu))
71296446 252 (define-key map [in] (cons (purecopy "Indentation")
2d07ff84
DL
253 'facemenu-indentation-menu))
254 (define-key map [ju] (cons (purecopy "Justification")
255 'facemenu-justification-menu))
256 (define-key map [s2] (list (purecopy "--")))
71296446 257 (define-key map [sp] (cons (purecopy "Special Properties")
2d07ff84 258 'facemenu-special-menu))
71296446 259 (define-key map [bg] (cons (purecopy "Background Color")
2d07ff84 260 'facemenu-background-menu))
71296446 261 (define-key map [fg] (cons (purecopy "Foreground Color")
2d07ff84 262 'facemenu-foreground-menu))
71296446 263 (define-key map [fc] (cons (purecopy "Face")
2d07ff84 264 'facemenu-face-menu)))
88d690a9 265(defalias 'facemenu-menu facemenu-menu)
bf7d4561 266
71296446 267(defvar facemenu-keymap
88d690a9 268 (let ((map (make-sparse-keymap "Set face")))
2d07ff84 269 (define-key map "o" (cons (purecopy "Other...") 'facemenu-set-face))
ef49d20f 270 (define-key map "\M-o" 'font-lock-fontify-block)
88d690a9 271 map)
9dc90430 272 "Keymap for face-changing commands.
bf7d4561 273`Facemenu-update' fills in the keymap according to the bindings
535d2617 274requested in `facemenu-keybindings'.")
88d690a9 275(defalias 'facemenu-keymap facemenu-keymap)
bf7d4561 276
cb5bec6e 277
487e6fcb 278(defcustom facemenu-add-face-function nil
7e6cb513 279 "Function called at beginning of text to change or nil.
cb5bec6e 280This function is passed the FACE to set and END of text to change, and must
487e6fcb
RS
281return a string which is inserted. It may set `facemenu-end-add-face'."
282 :type '(choice (const :tag "None" nil)
283 function)
284 :group 'facemenu)
cb5bec6e 285
487e6fcb 286(defcustom facemenu-end-add-face nil
7e6cb513 287 "String to insert or function called at end of text to change or nil.
cb5bec6e 288This function is passed the FACE to set, and must return a string which is
487e6fcb
RS
289inserted."
290 :type '(choice (const :tag "None" nil)
291 string
292 function)
293 :group 'facemenu)
cb5bec6e 294
487e6fcb 295(defcustom facemenu-remove-face-function nil
9086c730 296 "When non-nil, this is a function called to remove faces.
cb5bec6e 297This function is passed the START and END of text to change.
7e6cb513 298May also be t meaning to use `facemenu-add-face-function'."
487e6fcb
RS
299 :type '(choice (const :tag "None" nil)
300 (const :tag "Use add-face" t)
301 function)
302 :group 'facemenu)
cb5bec6e 303
bf7d4561
BG
304;;; Internal Variables
305
306(defvar facemenu-color-alist nil
bf7d4561 307 "Alist of colors, used for completion.
a926c9ce 308If this is nil, then the value of (defined-colors) is used.")
4a24b314 309
4e8aa578 310(defun facemenu-update ()
bf7d4561
BG
311 "Add or update the \"Face\" menu in the menu bar.
312You can call this to update things if you change any of the menu configuration
313variables."
4e8aa578 314 (interactive)
4e8aa578 315
bf7d4561
BG
316 ;; Add each defined face to the menu.
317 (facemenu-iterate 'facemenu-add-new-face
318 (facemenu-complete-face-list facemenu-keybindings)))
4a24b314 319
4e8aa578 320(defun facemenu-set-face (face &optional start end)
cd7890bd
RS
321 "Apply FACE to the region or next character typed.
322
323If the region is active (normally true except in Transient
324Mark mode) and nonempty, and there is no prefix argument,
325this command applies FACE to the region. Otherwise, it applies FACE
326to the faces to use for the next character
327inserted. (Moving point or switching buffers before typing
328a character to insert cancels the specification.)
329
330If FACE is `default', to \"apply\" it means clearing
331the list of faces to be used. For any other value of FACE,
332to \"apply\" it means putting FACE at the front of the list
333of faces to be used, and removing any faces further
334along in the list that would be completely overridden by
335preceding faces (including FACE).
336
337This command can also add FACE to the menu of faces,
338if `facemenu-listed-faces' says to do that."
7d8177cf
RS
339 (interactive (list (progn
340 (barf-if-buffer-read-only)
341 (read-face-name "Use face"))
342 (if (and mark-active (not current-prefix-arg))
343 (region-beginning))
344 (if (and mark-active (not current-prefix-arg))
345 (region-end))))
88d690a9 346 (facemenu-add-new-face face)
7d8177cf 347 (facemenu-add-face face start end))
4a24b314
RS
348
349(defun facemenu-set-foreground (color &optional start end)
7e6cb513 350 "Set the foreground COLOR of the region or next character typed.
af1eab21 351This command reads the color in the minibuffer.
7d8177cf
RS
352
353If the region is active (normally true except in Transient Mark mode)
354and there is no prefix argument, this command sets the region to the
355requested face.
356
357Otherwise, this command specifies the face for the next character
358inserted. Moving point or switching buffers before
71296446 359typing a character to insert cancels the specification."
7d8177cf
RS
360 (interactive (list (progn
361 (barf-if-buffer-read-only)
9317e499 362 (read-color "Foreground color: "))
7d8177cf
RS
363 (if (and mark-active (not current-prefix-arg))
364 (region-beginning))
365 (if (and mark-active (not current-prefix-arg))
366 (region-end))))
b97c98ad
LK
367 (facemenu-set-face-from-menu
368 (facemenu-add-new-color color 'facemenu-foreground-menu)
369 start end))
4a24b314
RS
370
371(defun facemenu-set-background (color &optional start end)
7e6cb513 372 "Set the background COLOR of the region or next character typed.
af1eab21 373This command reads the color in the minibuffer.
7d8177cf
RS
374
375If the region is active (normally true except in Transient Mark mode)
376and there is no prefix argument, this command sets the region to the
377requested face.
378
379Otherwise, this command specifies the face for the next character
380inserted. Moving point or switching buffers before
71296446 381typing a character to insert cancels the specification."
7d8177cf
RS
382 (interactive (list (progn
383 (barf-if-buffer-read-only)
9317e499 384 (read-color "Background color: "))
7d8177cf
RS
385 (if (and mark-active (not current-prefix-arg))
386 (region-beginning))
387 (if (and mark-active (not current-prefix-arg))
388 (region-end))))
b97c98ad
LK
389 (facemenu-set-face-from-menu
390 (facemenu-add-new-color color 'facemenu-background-menu)
391 start end))
4e8aa578
RS
392
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
RS
423
424(defun facemenu-set-invisible (start end)
425 "Make the region invisible.
426This sets the `invisible' text property; it can be undone with
f34eaa2c 427`facemenu-remove-special'."
4e8aa578 428 (interactive "r")
0e3edd7b 429 (add-text-properties start end '(invisible t)))
4e8aa578
RS
430
431(defun facemenu-set-intangible (start end)
432 "Make the region intangible: disallow moving into it.
433This sets the `intangible' text property; it can be undone with
f34eaa2c 434`facemenu-remove-special'."
4e8aa578 435 (interactive "r")
0e3edd7b 436 (add-text-properties start end '(intangible t)))
4e8aa578
RS
437
438(defun facemenu-set-read-only (start end)
439 "Make the region unmodifiable.
440This sets the `read-only' text property; it can be undone with
f34eaa2c 441`facemenu-remove-special'."
4e8aa578 442 (interactive "r")
0e3edd7b 443 (add-text-properties start end '(read-only t)))
4e8aa578 444
a32d7856
KH
445(defun facemenu-remove-face-props (start end)
446 "Remove `face' and `mouse-face' text properties."
4e8aa578
RS
447 (interactive "*r") ; error if buffer is read-only despite the next line.
448 (let ((inhibit-read-only t))
71296446 449 (remove-text-properties
a32d7856 450 start end '(face nil mouse-face nil))))
4e8aa578 451
f34eaa2c
KH
452(defun facemenu-remove-all (start end)
453 "Remove all text properties from the region."
454 (interactive "*r") ; error if buffer is read-only despite the next line.
455 (let ((inhibit-read-only t))
456 (set-text-properties start end nil)))
457
f34eaa2c
KH
458(defun facemenu-remove-special (start end)
459 "Remove all the \"special\" text properties from the region.
460These special properties include `invisible', `intangible' and `read-only'."
461 (interactive "*r") ; error if buffer is read-only despite the next line.
462 (let ((inhibit-read-only t))
71296446 463 (remove-text-properties
f34eaa2c 464 start end '(invisible nil intangible nil read-only nil))))
0af1db42 465\f
9317e499 466(defalias 'facemenu-read-color 'read-color)
4e8aa578 467
f0bf7c8e
JL
468(defcustom list-colors-sort nil
469 "Color sort order for `list-colors-display'.
470`nil' means default implementation-dependent order (defined in `x-colors').
471`name' sorts by color name.
472`rgb' sorts by red, green, blue components.
8fd02581 473`(rgb-dist . COLOR)' sorts by the RGB distance to the specified color.
f0bf7c8e 474`hsv' sorts by hue, saturation, value.
8fd02581 475`(hsv-dist . COLOR)' sorts by the HSV distance to the specified color
f0bf7c8e
JL
476and excludes grayscale colors."
477 :type '(choice (const :tag "Unsorted" nil)
478 (const :tag "Color Name" name)
479 (const :tag "Red-Green-Blue" rgb)
480 (cons :tag "Distance on RGB cube"
481 (const :tag "Distance from Color" rgb-dist)
482 (color :tag "Source Color Name"))
483 (const :tag "Hue-Saturation-Value" hsv)
484 (cons :tag "Distance on HSV cylinder"
485 (const :tag "Distance from Color" hsv-dist)
486 (color :tag "Source Color Name")))
487 :group 'facemenu
488 :version "24.1")
489
490(defun list-colors-sort-key (color)
491 "Return a list of keys for sorting colors depending on `list-colors-sort'.
492COLOR is the name of the color. When return value is nil,
493filter out the color from the output."
6d713256 494 (require 'color)
f0bf7c8e
JL
495 (cond
496 ((null list-colors-sort) color)
497 ((eq list-colors-sort 'name)
498 (downcase color))
499 ((eq list-colors-sort 'rgb)
500 (color-values color))
501 ((eq (car-safe list-colors-sort) 'rgb-dist)
502 (color-distance color (cdr list-colors-sort)))
503 ((eq list-colors-sort 'hsv)
6d713256 504 (apply 'color-rgb-to-hsv (color-name-to-rgb color)))
f0bf7c8e 505 ((eq (car-safe list-colors-sort) 'hsv-dist)
6d713256 506 (let* ((c-rgb (color-name-to-rgb color))
f0bf7c8e
JL
507 (c-hsv (apply 'color-rgb-to-hsv c-rgb))
508 (o-hsv (apply 'color-rgb-to-hsv
6d713256 509 (color-name-to-rgb (cdr list-colors-sort)))))
f0bf7c8e
JL
510 (unless (and (eq (nth 0 c-rgb) (nth 1 c-rgb)) ; exclude grayscale
511 (eq (nth 1 c-rgb) (nth 2 c-rgb)))
512 ;; 3D Euclidean distance (sqrt is not needed for sorting)
513 (+ (expt (- 180 (abs (- 180 (abs (- (nth 0 c-hsv) ; wrap hue
514 (nth 0 o-hsv)))))) 2)
515 (expt (- (nth 1 c-hsv) (nth 1 o-hsv)) 2)
516 (expt (- (nth 2 c-hsv) (nth 2 o-hsv)) 2)))))))
6f320937
CY
517
518(defun list-colors-display (&optional list buffer-name callback)
7c49006b
RS
519 "Display names of defined colors, and show what they look like.
520If the optional argument LIST is non-nil, it should be a list of
066a23af 521colors to display. Otherwise, this command computes a list of
ea32ef46
GM
522colors that the current display can handle. Customize
523`list-colors-sort' to change the order in which colors are shown.
6f320937 524
ea32ef46 525If the optional argument BUFFER-NAME is nil, it defaults to *Colors*.
6f320937
CY
526
527If the optional argument CALLBACK is non-nil, it should be a
528function to call each time the user types RET or clicks on a
ea32ef46 529color. The function should accept a single argument, the color name."
88d690a9 530 (interactive)
6062889d 531 (when (and (null list) (> (display-color-cells) 0))
066a23af 532 (setq list (list-colors-duplicates (defined-colors)))
f0bf7c8e
JL
533 (when list-colors-sort
534 ;; Schwartzian transform with `(color key1 key2 key3 ...)'.
535 (setq list (mapcar
536 'car
537 (sort (delq nil (mapcar
538 (lambda (c)
539 (let ((key (list-colors-sort-key
540 (car c))))
541 (when key
542 (cons c (if (consp key) key
543 (list key))))))
544 list))
545 (lambda (a b)
546 (let* ((a-keys (cdr a))
547 (b-keys (cdr b))
548 (a-key (car a-keys))
549 (b-key (car b-keys)))
550 ;; Skip common keys at the beginning of key lists.
551 (while (and a-key b-key (equal a-key b-key))
552 (setq a-keys (cdr a-keys) a-key (car a-keys)
553 b-keys (cdr b-keys) b-key (car b-keys)))
554 (cond
555 ((and (numberp a-key) (numberp b-key))
556 (< a-key b-key))
557 ((and (stringp a-key) (stringp b-key))
558 (string< a-key b-key)))))))))
d2596700
MB
559 (when (memq (display-visual-class) '(gray-scale pseudo-color direct-color))
560 ;; Don't show more than what the display can handle.
561 (let ((lc (nthcdr (1- (display-color-cells)) list)))
562 (if lc
563 (setcdr lc nil)))))
df7f0d0b
CY
564 (unless buffer-name
565 (setq buffer-name "*Colors*"))
566 (with-help-window buffer-name
567 (with-current-buffer standard-output
6f320937 568 (erase-buffer)
fe0fb33e
CY
569 (list-colors-print list callback)
570 (set-buffer-modified-p nil)
df7f0d0b 571 (setq truncate-lines t)))
fe0fb33e
CY
572 (when callback
573 (pop-to-buffer buffer-name)
574 (message "Click on a color to select it.")))
6f320937
CY
575
576(defun list-colors-print (list &optional callback)
577 (let ((callback-fn
578 (if callback
579 `(lambda (button)
580 (funcall ,callback (button-get button 'color-name))))))
581 (dolist (color list)
582 (if (consp color)
583 (if (cdr color)
584 (setq color (sort color (lambda (a b)
585 (string< (downcase a)
586 (downcase b))))))
587 (setq color (list color)))
588 (let* ((opoint (point))
589 (color-values (color-values (car color)))
590 (light-p (>= (apply 'max color-values)
fe0fb33e 591 (* (car (color-values "white")) .5))))
6f320937
CY
592 (insert (car color))
593 (indent-to 22)
594 (put-text-property opoint (point) 'face `(:background ,(car color)))
595 (put-text-property
596 (prog1 (point)
89877f5f 597 (insert " ")
fe0fb33e
CY
598 ;; Insert all color names.
599 (insert (mapconcat 'identity color ",")))
6f320937
CY
600 (point)
601 'face (list :foreground (car color)))
fe0fb33e
CY
602 (insert (propertize " " 'display '(space :align-to (- right 9))))
603 (insert " ")
f0bf7c8e
JL
604 (insert (propertize
605 (apply 'format "#%02x%02x%02x"
606 (mapcar (lambda (c) (lsh c -8))
607 color-values))
608 'mouse-face 'highlight
609 'help-echo
610 (let ((hsv (apply 'color-rgb-to-hsv
6d713256 611 (color-name-to-rgb (car color)))))
f0bf7c8e
JL
612 (format "H:%d S:%d V:%d"
613 (nth 0 hsv) (nth 1 hsv) (nth 2 hsv)))))
6f320937
CY
614 (when callback
615 (make-text-button
616 opoint (point)
617 'follow-link t
618 'mouse-face (list :background (car color)
619 :foreground (if light-p "black" "white"))
620 'color-name (car color)
621 'action callback-fn)))
622 (insert "\n"))
623 (goto-char (point-min))))
624
066a23af
JL
625
626(defun list-colors-duplicates (&optional list)
627 "Return a list of colors with grouped duplicate colors.
628If a color has no duplicates, then the element of the returned list
629has the form '(COLOR-NAME). The element of the returned list with
630duplicate colors has the form '(COLOR-NAME DUPLICATE-COLOR-NAME ...).
631This function uses the predicate `facemenu-color-equal' to compare
632color names. If the optional argument LIST is non-nil, it should
633be a list of colors to display. Otherwise, this function uses
634a list of colors that the current display can handle."
635 (let* ((list (mapcar 'list (or list (defined-colors))))
636 (l list))
637 (while (cdr l)
638 (if (and (facemenu-color-equal (car (car l)) (car (car (cdr l))))
343a34ff
JB
639 ;; On MS-Windows, there are logical colors that might have
640 ;; the same value but different names and meanings. For
641 ;; example, `SystemMenuText' (the color w32 uses for the
642 ;; text in menu entries) and `SystemWindowText' (the default
643 ;; color w32 uses for the text in windows and dialogs) may
644 ;; be the same display color and be adjacent in the list.
958ad486
JB
645 ;; These system colors all have names prefixed with "System",
646 ;; which is hardcoded in w32fns.c (SYSTEM_COLOR_PREFIX).
343a34ff
JB
647 ;; This makes them different to any other color. Bug#9722
648 (not (and (eq system-type 'windows-nt)
649 (string-match-p "^System" (car (car l))))))
066a23af
JL
650 (progn
651 (setcdr (car l) (cons (car (car (cdr l))) (cdr (car l))))
652 (setcdr l (cdr (cdr l))))
653 (setq l (cdr l))))
654 list))
88d690a9
RS
655
656(defun facemenu-color-equal (a b)
657 "Return t if colors A and B are the same color.
7c49006b 658A and B should be strings naming colors.
f795f633
EZ
659This function queries the display system to find out what the color
660names mean. It returns nil if the colors differ or if it can't
7c49006b 661determine the correct answer."
88d690a9 662 (cond ((equal a b) t)
f795f633 663 ((equal (color-values a) (color-values b)))))
88d690a9 664
a2e5caf7
SM
665
666(defvar facemenu-self-insert-data nil)
667
668(defun facemenu-post-self-insert-function ()
669 (when (and (car facemenu-self-insert-data)
670 (eq last-command (cdr facemenu-self-insert-data)))
671 (put-text-property (1- (point)) (point)
672 'face (car facemenu-self-insert-data))
673 (setq facemenu-self-insert-data nil))
674 (remove-hook 'post-self-insert-hook 'facemenu-post-self-insert-function))
675
676(defun facemenu-set-self-insert-face (face)
677 "Arrange for the next self-inserted char to have face `face'."
678 (setq facemenu-self-insert-data (cons face this-command))
679 (add-hook 'post-self-insert-hook 'facemenu-post-self-insert-function))
680
cb5bec6e 681(defun facemenu-add-face (face &optional start end)
4a24b314 682 "Add FACE to text between START and END.
7e6cb513 683If START is nil or START to END is empty, add FACE to next typed character
cb5bec6e
RS
684instead. For each section of that region that has a different face property,
685FACE will be consed onto it, and other faces that are completely hidden by
686that will be removed from the list.
af1eab21 687If `facemenu-add-face-function' and maybe `facemenu-end-add-face' are non-nil,
cb5bec6e 688they are used to set the face information.
bf7d4561
BG
689
690As a special case, if FACE is `default', then the region is left with NO face
691text property. Otherwise, selecting the default face would not have any
cb5bec6e
RS
692effect. See `facemenu-remove-face-function'."
693 (interactive "*xFace: \nr")
a2e5caf7
SM
694 (cond
695 ((and (eq face 'default)
696 (not (eq facemenu-remove-face-function t)))
697 (if facemenu-remove-face-function
698 (funcall facemenu-remove-face-function start end)
cb5bec6e 699 (if (and start (< start end))
a2e5caf7
SM
700 (remove-text-properties start end '(face default))
701 (facemenu-set-self-insert-face 'default))))
702 (facemenu-add-face-function
703 (save-excursion
704 (if end (goto-char end))
705 (save-excursion
706 (if start (goto-char start))
707 (insert-before-markers
708 (funcall facemenu-add-face-function face end)))
709 (if facemenu-end-add-face
710 (insert (if (stringp facemenu-end-add-face)
711 facemenu-end-add-face
712 (funcall facemenu-end-add-face face))))))
713 ((and start (< start end))
714 (let ((part-start start) part-end)
715 (while (not (= part-start end))
716 (setq part-end (next-single-property-change part-start 'face
717 nil end))
718 (let ((prev (get-text-property part-start 'face)))
719 (put-text-property part-start part-end 'face
720 (if (null prev)
721 face
722 (facemenu-active-faces
723 (cons face
724 (if (listp prev)
725 prev
726 (list prev)))
727 ;; Specify the selected frame
728 ;; because nil would mean to use
729 ;; the new-frame default settings,
730 ;; and those are usually nil.
731 (selected-frame)))))
732 (setq part-start part-end))))
733 (t
734 (facemenu-set-self-insert-face
735 (if (eq last-command (cdr facemenu-self-insert-data))
736 (cons face (if (listp (car facemenu-self-insert-data))
737 (car facemenu-self-insert-data)
738 (list (car facemenu-self-insert-data))))
739 face))))
97a7aa7b
RS
740 (unless (facemenu-enable-faces-p)
741 (message "Font-lock mode will override any faces you set in this buffer")))
4a24b314 742
5a79ed26
KH
743(defun facemenu-active-faces (face-list &optional frame)
744 "Return from FACE-LIST those faces that would be used for display.
745This means each face attribute is not specified in a face earlier in FACE-LIST
746and such a face is therefore active when used to display text.
747If the optional argument FRAME is given, use the faces in that frame; otherwise
748use the selected frame. If t, then the global, non-frame faces are used."
7d8177cf
RS
749 (let* ((mask-atts (copy-sequence
750 (if (consp (car face-list))
f790dddf 751 (face-attributes-as-vector (car face-list))
7d8177cf
RS
752 (or (internal-lisp-face-p (car face-list) frame)
753 (check-face (car face-list))))))
5a79ed26
KH
754 (active-list (list (car face-list)))
755 (face-list (cdr face-list))
756 (mask-len (length mask-atts)))
757 (while face-list
7d8177cf
RS
758 (if (let ((face-atts
759 (if (consp (car face-list))
f790dddf 760 (face-attributes-as-vector (car face-list))
7d8177cf
RS
761 (or (internal-lisp-face-p (car face-list) frame)
762 (check-face (car face-list)))))
763 (i mask-len)
764 (useful nil))
240c0c90 765 (while (>= (setq i (1- i)) 0)
7d8177cf
RS
766 (and (not (memq (aref face-atts i) '(nil unspecified)))
767 (memq (aref mask-atts i) '(nil unspecified))
5a79ed26
KH
768 (aset mask-atts i (setq useful t))))
769 useful)
770 (setq active-list (cons (car face-list) active-list)))
771 (setq face-list (cdr face-list)))
772 (nreverse active-list)))
4a24b314 773
9bf4c4e5 774(defun facemenu-add-new-face (face)
cd7890bd
RS
775 "Add FACE (a face) to the Face menu if `facemenu-listed-faces' says so.
776This is called whenever you create a new face, and at other times."
7d8177cf
RS
777 (let* (name
778 symbol
9bf4c4e5
RS
779 menu docstring
780 (key (cdr (assoc face facemenu-keybindings)))
88d690a9 781 function menu-val)
9bf4c4e5
RS
782 (if (symbolp face)
783 (setq name (symbol-name face)
784 symbol face)
785 (setq name face
1d792b18 786 symbol (intern name)))
9bf4c4e5
RS
787 (setq menu 'facemenu-face-menu)
788 (setq docstring
1e8780b1 789 (purecopy (format "Select face `%s' for subsequent insertion.
3187841b
RS
790If the mark is active and there is no prefix argument,
791apply face `%s' to the region instead.
792This command was defined by `facemenu-add-new-face'."
1e8780b1 793 name name)))
b6a67507
CY
794 (cond ((facemenu-iterate ; check if equivalent face is already in the menu
795 (lambda (m) (and (listp m)
796 (symbolp (car m))
f3359de1
RS
797 ;; Avoid error in face-equal
798 ;; when a non-face is erroneously present.
799 (facep (car m))
b6a67507
CY
800 (face-equal (car m) symbol)))
801 (cdr (symbol-function menu))))
802 ;; Faces with a keyboard equivalent. These go at the front.
803 (key
88d690a9
RS
804 (setq function (intern (concat "facemenu-set-" name)))
805 (fset function
536f1a10
RS
806 `(lambda ()
807 ,docstring
808 (interactive)
af1eab21
RS
809 (facemenu-set-face
810 (quote ,symbol)
811 (if (and mark-active (not current-prefix-arg))
812 (region-beginning))
813 (if (and mark-active (not current-prefix-arg))
814 (region-end)))))
88d690a9
RS
815 (define-key 'facemenu-keymap key (cons name function))
816 (define-key menu key (cons name function)))
b6a67507
CY
817 ;; Faces with no keyboard equivalent. Figure out where to put it:
818 ((or (eq t facemenu-listed-faces)
819 (memq symbol facemenu-listed-faces))
7d8177cf 820 (setq key (vector symbol)
88d690a9
RS
821 function 'facemenu-set-face-from-menu
822 menu-val (symbol-function menu))
823 (if (and facemenu-new-faces-at-end
b6a67507 824 (> (length menu-val) 3))
88d690a9 825 (define-key-after menu-val key (cons name function)
9bf4c4e5
RS
826 (car (nth (- (length menu-val) 3) menu-val)))
827 (define-key menu key (cons name function))))))
828 nil) ; Return nil for facemenu-iterate
829
019b1899 830(defun facemenu-add-new-color (color menu)
9bf4c4e5 831 "Add COLOR (a color name string) to the appropriate Face menu.
019b1899 832MENU should be `facemenu-foreground-menu' or `facemenu-background-menu'.
b97c98ad 833Return the event type (a symbol) of the added menu entry.
9bf4c4e5
RS
834
835This is called whenever you use a new color."
06b60517 836 (let (symbol)
019b1899
LK
837 (unless (color-defined-p color)
838 (error "Color `%s' undefined" color))
9bf4c4e5 839 (cond ((eq menu 'facemenu-foreground-menu)
06b60517 840 (setq symbol (intern (concat "fg:" color))))
9bf4c4e5 841 ((eq menu 'facemenu-background-menu)
06b60517 842 (setq symbol (intern (concat "bg:" color))))
019b1899 843 (t (error "MENU should be `facemenu-foreground-menu' or `facemenu-background-menu'")))
b97c98ad
LK
844 (unless (facemenu-iterate ; Check if color is already in the menu.
845 (lambda (m) (and (listp m)
846 (eq (car m) symbol)))
847 (cdr (symbol-function menu)))
848 ;; Color is not in the menu. Figure out where to put it.
849 (let ((key (vector symbol))
850 (function 'facemenu-set-face-from-menu)
851 (menu-val (symbol-function menu)))
852 (if (and facemenu-new-faces-at-end
853 (> (length menu-val) 3))
854 (define-key-after menu-val key (cons color function)
855 (car (nth (- (length menu-val) 3) menu-val)))
856 (define-key menu key (cons color function)))))
019b1899 857 symbol))
bf7d4561 858
bf7d4561 859(defun facemenu-complete-face-list (&optional oldlist)
7cd49450 860 "Return list of all faces that look different.
71296446 861Starts with given ALIST of faces, and adds elements only if they display
bf7d4561 862differently from any face already on the list.
71296446 863The faces on ALIST will end up at the end of the returned list, in reverse
bf7d4561
BG
864order."
865 (let ((list (nreverse (mapcar 'car oldlist))))
71296446
JB
866 (facemenu-iterate
867 (lambda (new-face)
bf7d4561
BG
868 (if (not (memq new-face list))
869 (setq list (cons new-face list)))
870 nil)
871 (nreverse (face-list)))
872 list))
873
7e6cb513 874(defun facemenu-iterate (func list)
4e8aa578
RS
875 "Apply FUNC to each element of LIST until one returns non-nil.
876Returns the non-nil value it found, or nil if all were nil."
7e6cb513
SM
877 (while (and list (not (funcall func (car list))))
878 (setq list (cdr list)))
879 (car list))
4e8aa578
RS
880
881(facemenu-update)
4e8aa578 882
1ff4ace5 883(provide 'facemenu)
ab5796a9 884
4e8aa578 885;;; facemenu.el ends here