Add "Package:" file headers to denote built-in packages.
[emacs.git] / lisp / facemenu.el
blob5249538d711d61b65097e51142b1d3805f33d96a
1 ;;; facemenu.el --- create a face menu for interactively adding fonts to text
3 ;; Copyright (C) 1994, 1995, 1996, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; Author: Boris Goldowsky <boris@gnu.org>
7 ;; Keywords: faces
8 ;; Package: emacs
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; This file defines a menu of faces (bold, italic, etc) which allows you to
28 ;; set the face used for a region of the buffer. Some faces also have
29 ;; keybindings, which are shown in the menu.
31 ;; The menu also contains submenus for indentation and justification-changing
32 ;; commands.
34 ;;; Usage:
35 ;; Selecting a face from the menu or typing the keyboard equivalent will
36 ;; change the region to use that face. If you use transient-mark-mode and the
37 ;; region is not active, the face will be remembered and used for the next
38 ;; insertion. It will be forgotten if you move point or make other
39 ;; modifications before inserting or typing anything.
41 ;; Faces can be selected from the keyboard as well.
42 ;; The standard keybindings are M-o (or ESC o) + letter:
43 ;; M-o i = "set italic", M-o b = "set bold", etc.
45 ;;; Customization:
46 ;; An alternative set of keybindings that may be easier to type can be set up
47 ;; using "Alt" or "Hyper" keys. This requires that you either have or create
48 ;; an Alt or Hyper key on your keyboard. On my keyboard, there is a key
49 ;; labeled "Alt", but to make it act as an Alt key I have to put this command
50 ;; into my .xinitrc:
51 ;; xmodmap -e "add Mod3 = Alt_L"
52 ;; Or, I can make it into a Hyper key with this:
53 ;; xmodmap -e "keysym Alt_L = Hyper_L" -e "add Mod2 = Hyper_L"
54 ;; Check with local X-perts for how to do it on your system.
55 ;; Then you can define your keybindings with code like this in your .emacs:
56 ;; (setq facemenu-keybindings
57 ;; '((default . [?\H-d])
58 ;; (bold . [?\H-b])
59 ;; (italic . [?\H-i])
60 ;; (bold-italic . [?\H-l])
61 ;; (underline . [?\H-u])))
62 ;; (facemenu-update)
63 ;; (setq facemenu-keymap global-map)
64 ;; (define-key global-map [?\H-c] 'facemenu-set-foreground) ; set fg color
65 ;; (define-key global-map [?\H-C] 'facemenu-set-background) ; set bg color
67 ;; The order of the faces that appear in the menu and their keybindings can be
68 ;; controlled by setting the variables `facemenu-keybindings' and
69 ;; `facemenu-new-faces-at-end'. List faces that you want to use in documents
70 ;; in `facemenu-listed-faces'.
72 ;;; Known Problems:
73 ;; Bold and Italic do not combine to create bold-italic if you select them
74 ;; both, although most other combinations (eg bold + underline + some color)
75 ;; do the intuitive thing.
77 ;; There is at present no way to display what the faces look like in
78 ;; the menu itself.
80 ;; `list-faces-display' shows the faces in a different order than
81 ;; this menu, which could be confusing. I do /not/ sort the list
82 ;; alphabetically, because I like the default order: it puts the most
83 ;; basic, common fonts first.
85 ;; Please send me any other problems, comments or ideas.
87 ;;; Code:
89 (eval-when-compile
90 (require 'help)
91 (require 'button))
93 ;; Global bindings:
94 (define-key global-map [C-down-mouse-2] 'facemenu-menu)
95 (define-key global-map "\M-o" 'facemenu-keymap)
97 (defgroup facemenu nil
98 "Create a face menu for interactively adding fonts to text."
99 :group 'faces
100 :prefix "facemenu-")
102 (defcustom facemenu-keybindings
103 (mapcar 'purecopy
104 '((default . "d")
105 (bold . "b")
106 (italic . "i")
107 (bold-italic . "l") ; {bold} intersect {italic} = {l}
108 (underline . "u")))
109 "Alist of interesting faces and keybindings.
110 Each element is itself a list: the car is the name of the face,
111 the next element is the key to use as a keyboard equivalent of the menu item;
112 the binding is made in `facemenu-keymap'.
114 The faces specifically mentioned in this list are put at the top of
115 the menu, in the order specified. All other faces which are defined
116 in `facemenu-listed-faces' are listed after them, but get no
117 keyboard equivalents.
119 If you change this variable after loading facemenu.el, you will need to call
120 `facemenu-update' to make it take effect."
121 :type '(repeat (cons face string))
122 :group 'facemenu)
124 (defcustom facemenu-new-faces-at-end t
125 "Where in the menu to insert newly-created faces.
126 This should be nil to put them at the top of the menu, or t to put them
127 just before \"Other\" at the end."
128 :type 'boolean
129 :group 'facemenu)
131 (defvar facemenu-unlisted-faces
132 `(modeline region secondary-selection highlight scratch-face
133 ,(purecopy "^font-lock-") ,(purecopy "^gnus-") ,(purecopy "^message-")
134 ,(purecopy "^ediff-") ,(purecopy "^term-") ,(purecopy "^vc-")
135 ,(purecopy "^widget-") ,(purecopy "^custom-") ,(purecopy "^vm-"))
136 "*List of faces that are of no interest to the user.")
137 (make-obsolete-variable 'facemenu-unlisted-faces 'facemenu-listed-faces
138 "22.1,\n and has no effect on the Face menu")
140 (defcustom facemenu-listed-faces nil
141 "List of faces to include in the Face menu.
142 Each element should be a symbol, the name of a face.
143 The \"basic \" faces in `facemenu-keybindings' are automatically
144 added to the Face menu, and need not be in this list.
146 This value takes effect when you load facemenu.el. If the
147 list includes symbols which are not defined as faces, they
148 are ignored; however, subsequently defining or creating
149 those faces adds them to the menu then. You can call
150 `facemenu-update' to recalculate the menu contents, such as
151 if you change the value of this variable,
153 If this variable is t, all faces that you apply to text
154 using the face menu commands (even by name), and all faces
155 that you define or create, are added to the menu. You may
156 find it useful to set this variable to t temporarily while
157 you define some faces, so that they will be added. However,
158 if the value is no longer t and you call `facemenu-update',
159 it will remove any faces not explicitly in the list."
160 :type '(choice (const :tag "List all faces" t)
161 (const :tag "None" nil)
162 (repeat symbol))
163 :group 'facemenu
164 :version "22.1")
166 (defvar facemenu-face-menu
167 (let ((map (make-sparse-keymap "Face")))
168 (define-key map "o" (cons (purecopy "Other...") 'facemenu-set-face))
169 map)
170 "Menu keymap for faces.")
171 (defalias 'facemenu-face-menu facemenu-face-menu)
172 (put 'facemenu-face-menu 'menu-enable '(facemenu-enable-faces-p))
174 (defvar facemenu-foreground-menu
175 (let ((map (make-sparse-keymap "Foreground Color")))
176 (define-key map "o" (cons (purecopy "Other...") 'facemenu-set-foreground))
177 map)
178 "Menu keymap for foreground colors.")
179 (defalias 'facemenu-foreground-menu facemenu-foreground-menu)
180 (put 'facemenu-foreground-menu 'menu-enable '(facemenu-enable-faces-p))
182 (defvar facemenu-background-menu
183 (let ((map (make-sparse-keymap "Background Color")))
184 (define-key map "o" (cons (purecopy "Other...") 'facemenu-set-background))
185 map)
186 "Menu keymap for background colors.")
187 (defalias 'facemenu-background-menu facemenu-background-menu)
188 (put 'facemenu-background-menu 'menu-enable '(facemenu-enable-faces-p))
190 ;;; Condition for enabling menu items that set faces.
191 (defun facemenu-enable-faces-p ()
192 ;; Enable the facemenu if facemenu-add-face-function is defined
193 ;; (e.g. in Tex-mode and SGML mode), or if font-lock is off.
194 (or (not (and font-lock-mode font-lock-defaults))
195 facemenu-add-face-function))
197 (defvar facemenu-special-menu
198 (let ((map (make-sparse-keymap "Special")))
199 (define-key map [?s] (cons (purecopy "Remove Special")
200 'facemenu-remove-special))
201 (define-key map [?t] (cons (purecopy "Intangible")
202 'facemenu-set-intangible))
203 (define-key map [?v] (cons (purecopy "Invisible")
204 'facemenu-set-invisible))
205 (define-key map [?r] (cons (purecopy "Read-Only")
206 'facemenu-set-read-only))
207 map)
208 "Menu keymap for non-face text-properties.")
209 (defalias 'facemenu-special-menu facemenu-special-menu)
211 (defvar facemenu-justification-menu
212 (let ((map (make-sparse-keymap "Justification")))
213 (define-key map [?c] (cons (purecopy "Center") 'set-justification-center))
214 (define-key map [?b] (cons (purecopy "Full") 'set-justification-full))
215 (define-key map [?r] (cons (purecopy "Right") 'set-justification-right))
216 (define-key map [?l] (cons (purecopy "Left") 'set-justification-left))
217 (define-key map [?u] (cons (purecopy "Unfilled") 'set-justification-none))
218 map)
219 "Submenu for text justification commands.")
220 (defalias 'facemenu-justification-menu facemenu-justification-menu)
222 (defvar facemenu-indentation-menu
223 (let ((map (make-sparse-keymap "Indentation")))
224 (define-key map [decrease-right-margin]
225 (cons (purecopy "Indent Right Less") 'decrease-right-margin))
226 (define-key map [increase-right-margin]
227 (cons (purecopy "Indent Right More") 'increase-right-margin))
228 (define-key map [decrease-left-margin]
229 (cons (purecopy "Indent Less") 'decrease-left-margin))
230 (define-key map [increase-left-margin]
231 (cons (purecopy "Indent More") 'increase-left-margin))
232 map)
233 "Submenu for indentation commands.")
234 (defalias 'facemenu-indentation-menu facemenu-indentation-menu)
236 ;; This is split up to avoid an overlong line in loaddefs.el.
237 (defvar facemenu-menu nil
238 "Facemenu top-level menu keymap.")
239 (setq facemenu-menu (make-sparse-keymap "Text Properties"))
240 (let ((map facemenu-menu))
241 (define-key map [dc] (cons (purecopy "Display Colors") 'list-colors-display))
242 (define-key map [df] (cons (purecopy "Display Faces") 'list-faces-display))
243 (define-key map [dp] (cons (purecopy "Describe Properties")
244 'describe-text-properties))
245 (define-key map [ra] (cons (purecopy "Remove Text Properties")
246 'facemenu-remove-all))
247 (define-key map [rm] (cons (purecopy "Remove Face Properties")
248 'facemenu-remove-face-props))
249 (define-key map [s1] (list (purecopy "--"))))
250 (let ((map facemenu-menu))
251 (define-key map [in] (cons (purecopy "Indentation")
252 'facemenu-indentation-menu))
253 (define-key map [ju] (cons (purecopy "Justification")
254 'facemenu-justification-menu))
255 (define-key map [s2] (list (purecopy "--")))
256 (define-key map [sp] (cons (purecopy "Special Properties")
257 'facemenu-special-menu))
258 (define-key map [bg] (cons (purecopy "Background Color")
259 'facemenu-background-menu))
260 (define-key map [fg] (cons (purecopy "Foreground Color")
261 'facemenu-foreground-menu))
262 (define-key map [fc] (cons (purecopy "Face")
263 'facemenu-face-menu)))
264 (defalias 'facemenu-menu facemenu-menu)
266 (defvar facemenu-keymap
267 (let ((map (make-sparse-keymap "Set face")))
268 (define-key map "o" (cons (purecopy "Other...") 'facemenu-set-face))
269 (define-key map "\M-o" 'font-lock-fontify-block)
270 map)
271 "Keymap for face-changing commands.
272 `Facemenu-update' fills in the keymap according to the bindings
273 requested in `facemenu-keybindings'.")
274 (defalias 'facemenu-keymap facemenu-keymap)
277 (defcustom facemenu-add-face-function nil
278 "Function called at beginning of text to change or nil.
279 This function is passed the FACE to set and END of text to change, and must
280 return a string which is inserted. It may set `facemenu-end-add-face'."
281 :type '(choice (const :tag "None" nil)
282 function)
283 :group 'facemenu)
285 (defcustom facemenu-end-add-face nil
286 "String to insert or function called at end of text to change or nil.
287 This function is passed the FACE to set, and must return a string which is
288 inserted."
289 :type '(choice (const :tag "None" nil)
290 string
291 function)
292 :group 'facemenu)
294 (defcustom facemenu-remove-face-function nil
295 "When non-nil, this is a function called to remove faces.
296 This function is passed the START and END of text to change.
297 May also be t meaning to use `facemenu-add-face-function'."
298 :type '(choice (const :tag "None" nil)
299 (const :tag "Use add-face" t)
300 function)
301 :group 'facemenu)
303 ;;; Internal Variables
305 (defvar facemenu-color-alist nil
306 "Alist of colors, used for completion.
307 If this is nil, then the value of (defined-colors) is used.")
309 (defun facemenu-update ()
310 "Add or update the \"Face\" menu in the menu bar.
311 You can call this to update things if you change any of the menu configuration
312 variables."
313 (interactive)
315 ;; Add each defined face to the menu.
316 (facemenu-iterate 'facemenu-add-new-face
317 (facemenu-complete-face-list facemenu-keybindings)))
319 (defun facemenu-set-face (face &optional start end)
320 "Apply FACE to the region or next character typed.
322 If the region is active (normally true except in Transient
323 Mark mode) and nonempty, and there is no prefix argument,
324 this command applies FACE to the region. Otherwise, it applies FACE
325 to the faces to use for the next character
326 inserted. (Moving point or switching buffers before typing
327 a character to insert cancels the specification.)
329 If FACE is `default', to \"apply\" it means clearing
330 the list of faces to be used. For any other value of FACE,
331 to \"apply\" it means putting FACE at the front of the list
332 of faces to be used, and removing any faces further
333 along in the list that would be completely overridden by
334 preceding faces (including FACE).
336 This command can also add FACE to the menu of faces,
337 if `facemenu-listed-faces' says to do that."
338 (interactive (list (progn
339 (barf-if-buffer-read-only)
340 (read-face-name "Use face"))
341 (if (and mark-active (not current-prefix-arg))
342 (region-beginning))
343 (if (and mark-active (not current-prefix-arg))
344 (region-end))))
345 (facemenu-add-new-face face)
346 (facemenu-add-face face start end))
348 (defun facemenu-set-foreground (color &optional start end)
349 "Set the foreground COLOR of the region or next character typed.
350 This command reads the color in the minibuffer.
352 If the region is active (normally true except in Transient Mark mode)
353 and there is no prefix argument, this command sets the region to the
354 requested face.
356 Otherwise, this command specifies the face for the next character
357 inserted. Moving point or switching buffers before
358 typing a character to insert cancels the specification."
359 (interactive (list (progn
360 (barf-if-buffer-read-only)
361 (facemenu-read-color "Foreground color: "))
362 (if (and mark-active (not current-prefix-arg))
363 (region-beginning))
364 (if (and mark-active (not current-prefix-arg))
365 (region-end))))
366 (facemenu-set-face-from-menu
367 (facemenu-add-new-color color 'facemenu-foreground-menu)
368 start end))
370 (defun facemenu-set-background (color &optional start end)
371 "Set the background COLOR of the region or next character typed.
372 This command reads the color in the minibuffer.
374 If the region is active (normally true except in Transient Mark mode)
375 and there is no prefix argument, this command sets the region to the
376 requested face.
378 Otherwise, this command specifies the face for the next character
379 inserted. Moving point or switching buffers before
380 typing a character to insert cancels the specification."
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))))
388 (facemenu-set-face-from-menu
389 (facemenu-add-new-color color 'facemenu-background-menu)
390 start end))
392 (defun facemenu-set-face-from-menu (face start end)
393 "Set the FACE of the region or next character typed.
394 This function is designed to be called from a menu; FACE is determined
395 using the event type of the menu entry. If FACE is a symbol whose
396 name starts with \"fg:\" or \"bg:\", then this functions sets the
397 foreground or background to the color specified by the rest of the
398 symbol's name. Any other symbol is considered the name of a face.
400 If the region is active (normally true except in Transient Mark mode)
401 and there is no prefix argument, this command sets the region to the
402 requested face.
404 Otherwise, this command specifies the face for the next character
405 inserted. Moving point or switching buffers before typing a character
406 to insert cancels the specification."
407 (interactive (list last-command-event
408 (if (and mark-active (not current-prefix-arg))
409 (region-beginning))
410 (if (and mark-active (not current-prefix-arg))
411 (region-end))))
412 (barf-if-buffer-read-only)
413 (facemenu-add-face
414 (let ((fn (symbol-name face)))
415 (if (string-match "\\`\\([fb]\\)g:\\(.+\\)" fn)
416 (list (list (if (string= (match-string 1 fn) "f")
417 :foreground
418 :background)
419 (match-string 2 fn)))
420 face))
421 start end))
423 (defun facemenu-set-invisible (start end)
424 "Make the region invisible.
425 This sets the `invisible' text property; it can be undone with
426 `facemenu-remove-special'."
427 (interactive "r")
428 (add-text-properties start end '(invisible t)))
430 (defun facemenu-set-intangible (start end)
431 "Make the region intangible: disallow moving into it.
432 This sets the `intangible' text property; it can be undone with
433 `facemenu-remove-special'."
434 (interactive "r")
435 (add-text-properties start end '(intangible t)))
437 (defun facemenu-set-read-only (start end)
438 "Make the region unmodifiable.
439 This sets the `read-only' text property; it can be undone with
440 `facemenu-remove-special'."
441 (interactive "r")
442 (add-text-properties start end '(read-only t)))
444 (defun facemenu-remove-face-props (start end)
445 "Remove `face' and `mouse-face' text properties."
446 (interactive "*r") ; error if buffer is read-only despite the next line.
447 (let ((inhibit-read-only t))
448 (remove-text-properties
449 start end '(face nil mouse-face nil))))
451 (defun facemenu-remove-all (start end)
452 "Remove all text properties from the region."
453 (interactive "*r") ; error if buffer is read-only despite the next line.
454 (let ((inhibit-read-only t))
455 (set-text-properties start end nil)))
457 (defun facemenu-remove-special (start end)
458 "Remove all the \"special\" text properties from the region.
459 These special properties include `invisible', `intangible' and `read-only'."
460 (interactive "*r") ; error if buffer is read-only despite the next line.
461 (let ((inhibit-read-only t))
462 (remove-text-properties
463 start end '(invisible nil intangible nil read-only nil))))
465 (defun facemenu-read-color (&optional prompt)
466 "Read a color using the minibuffer."
467 (let* ((completion-ignore-case t)
468 (color-list (or facemenu-color-alist (defined-colors)))
469 (completer
470 (lambda (string pred all-completions)
471 (if all-completions
472 (or (all-completions string color-list pred)
473 (if (color-defined-p string)
474 (list string)))
475 (or (try-completion string color-list pred)
476 (if (color-defined-p string)
477 string)))))
478 (col (completing-read (or prompt "Color: ") completer nil t)))
479 (if (equal "" col)
481 col)))
483 (defun color-rgb-to-hsv (r g b)
484 "For R, G, B color components return a list of hue, saturation, value.
485 R, G, B input values should be in [0..65535] range.
486 Output values for hue are integers in [0..360] range.
487 Output values for saturation and value are integers in [0..100] range."
488 (let* ((r (/ r 65535.0))
489 (g (/ g 65535.0))
490 (b (/ b 65535.0))
491 (max (max r g b))
492 (min (min r g b))
493 (h (cond ((= max min) 0)
494 ((= max r) (mod (+ (* 60 (/ (- g b) (- max min))) 360) 360))
495 ((= max g) (+ (* 60 (/ (- b r) (- max min))) 120))
496 ((= max b) (+ (* 60 (/ (- r g) (- max min))) 240))))
497 (s (cond ((= max 0) 0)
498 (t (- 1 (/ min max)))))
499 (v max))
500 (list (round h) (round s 0.01) (round v 0.01))))
502 (defcustom list-colors-sort nil
503 "Color sort order for `list-colors-display'.
504 `nil' means default implementation-dependent order (defined in `x-colors').
505 `name' sorts by color name.
506 `rgb' sorts by red, green, blue components.
507 `(rgb-dist . COLOR)' sorts by the RGB distance to the specified color.
508 `hsv' sorts by hue, saturation, value.
509 `(hsv-dist . COLOR)' sorts by the HSV distance to the specified color
510 and excludes grayscale colors."
511 :type '(choice (const :tag "Unsorted" nil)
512 (const :tag "Color Name" name)
513 (const :tag "Red-Green-Blue" rgb)
514 (cons :tag "Distance on RGB cube"
515 (const :tag "Distance from Color" rgb-dist)
516 (color :tag "Source Color Name"))
517 (const :tag "Hue-Saturation-Value" hsv)
518 (cons :tag "Distance on HSV cylinder"
519 (const :tag "Distance from Color" hsv-dist)
520 (color :tag "Source Color Name")))
521 :group 'facemenu
522 :version "24.1")
524 (defun list-colors-sort-key (color)
525 "Return a list of keys for sorting colors depending on `list-colors-sort'.
526 COLOR is the name of the color. When return value is nil,
527 filter out the color from the output."
528 (cond
529 ((null list-colors-sort) color)
530 ((eq list-colors-sort 'name)
531 (downcase color))
532 ((eq list-colors-sort 'rgb)
533 (color-values color))
534 ((eq (car-safe list-colors-sort) 'rgb-dist)
535 (color-distance color (cdr list-colors-sort)))
536 ((eq list-colors-sort 'hsv)
537 (apply 'color-rgb-to-hsv (color-values color)))
538 ((eq (car-safe list-colors-sort) 'hsv-dist)
539 (let* ((c-rgb (color-values color))
540 (c-hsv (apply 'color-rgb-to-hsv c-rgb))
541 (o-hsv (apply 'color-rgb-to-hsv
542 (color-values (cdr list-colors-sort)))))
543 (unless (and (eq (nth 0 c-rgb) (nth 1 c-rgb)) ; exclude grayscale
544 (eq (nth 1 c-rgb) (nth 2 c-rgb)))
545 ;; 3D Euclidean distance (sqrt is not needed for sorting)
546 (+ (expt (- 180 (abs (- 180 (abs (- (nth 0 c-hsv) ; wrap hue
547 (nth 0 o-hsv)))))) 2)
548 (expt (- (nth 1 c-hsv) (nth 1 o-hsv)) 2)
549 (expt (- (nth 2 c-hsv) (nth 2 o-hsv)) 2)))))))
551 (defun list-colors-display (&optional list buffer-name callback)
552 "Display names of defined colors, and show what they look like.
553 If the optional argument LIST is non-nil, it should be a list of
554 colors to display. Otherwise, this command computes a list of
555 colors that the current display can handle.
557 If the optional argument BUFFER-NAME is nil, it defaults to
558 *Colors*.
560 If the optional argument CALLBACK is non-nil, it should be a
561 function to call each time the user types RET or clicks on a
562 color. The function should accept a single argument, the color
563 name.
565 You can change the color sort order by customizing `list-colors-sort'."
566 (interactive)
567 (when (and (null list) (> (display-color-cells) 0))
568 (setq list (list-colors-duplicates (defined-colors)))
569 (when list-colors-sort
570 ;; Schwartzian transform with `(color key1 key2 key3 ...)'.
571 (setq list (mapcar
572 'car
573 (sort (delq nil (mapcar
574 (lambda (c)
575 (let ((key (list-colors-sort-key
576 (car c))))
577 (when key
578 (cons c (if (consp key) key
579 (list key))))))
580 list))
581 (lambda (a b)
582 (let* ((a-keys (cdr a))
583 (b-keys (cdr b))
584 (a-key (car a-keys))
585 (b-key (car b-keys)))
586 ;; Skip common keys at the beginning of key lists.
587 (while (and a-key b-key (equal a-key b-key))
588 (setq a-keys (cdr a-keys) a-key (car a-keys)
589 b-keys (cdr b-keys) b-key (car b-keys)))
590 (cond
591 ((and (numberp a-key) (numberp b-key))
592 (< a-key b-key))
593 ((and (stringp a-key) (stringp b-key))
594 (string< a-key b-key)))))))))
595 (when (memq (display-visual-class) '(gray-scale pseudo-color direct-color))
596 ;; Don't show more than what the display can handle.
597 (let ((lc (nthcdr (1- (display-color-cells)) list)))
598 (if lc
599 (setcdr lc nil)))))
600 (let ((buf (get-buffer-create "*Colors*")))
601 (with-current-buffer buf
602 (erase-buffer)
603 (setq truncate-lines t)
604 ;; Display buffer before generating content to allow
605 ;; `list-colors-print' to get the right window-width.
606 (pop-to-buffer buf)
607 (list-colors-print list callback)
608 (set-buffer-modified-p nil)))
609 (if callback
610 (message "Click on a color to select it.")))
612 (defun list-colors-print (list &optional callback)
613 (let ((callback-fn
614 (if callback
615 `(lambda (button)
616 (funcall ,callback (button-get button 'color-name))))))
617 (dolist (color list)
618 (if (consp color)
619 (if (cdr color)
620 (setq color (sort color (lambda (a b)
621 (string< (downcase a)
622 (downcase b))))))
623 (setq color (list color)))
624 (let* ((opoint (point))
625 (color-values (color-values (car color)))
626 (light-p (>= (apply 'max color-values)
627 (* (car (color-values "white")) .5)))
628 (max-len (max (- (window-width) 33) 20)))
629 (insert (car color))
630 (indent-to 22)
631 (put-text-property opoint (point) 'face `(:background ,(car color)))
632 (put-text-property
633 (prog1 (point)
634 (insert " ")
635 (if (cdr color)
636 ;; Insert as many color names as possible, fitting max-len.
637 (let ((names (list (car color)))
638 (others (cdr color))
639 (len (length (car color)))
640 newlen)
641 (while (and others
642 (< (setq newlen (+ len 2 (length (car others))))
643 max-len))
644 (setq len newlen)
645 (push (pop others) names))
646 (insert (mapconcat 'identity (nreverse names) ", ")))
647 (insert (car color))))
648 (point)
649 'face (list :foreground (car color)))
650 (indent-to (max (- (window-width) 8) 44))
651 (insert (propertize
652 (apply 'format "#%02x%02x%02x"
653 (mapcar (lambda (c) (lsh c -8))
654 color-values))
655 'mouse-face 'highlight
656 'help-echo
657 (let ((hsv (apply 'color-rgb-to-hsv
658 (color-values (car color)))))
659 (format "H:%d S:%d V:%d"
660 (nth 0 hsv) (nth 1 hsv) (nth 2 hsv)))))
661 (when callback
662 (make-text-button
663 opoint (point)
664 'follow-link t
665 'mouse-face (list :background (car color)
666 :foreground (if light-p "black" "white"))
667 'color-name (car color)
668 'action callback-fn)))
669 (insert "\n"))
670 (goto-char (point-min))))
673 (defun list-colors-duplicates (&optional list)
674 "Return a list of colors with grouped duplicate colors.
675 If a color has no duplicates, then the element of the returned list
676 has the form '(COLOR-NAME). The element of the returned list with
677 duplicate colors has the form '(COLOR-NAME DUPLICATE-COLOR-NAME ...).
678 This function uses the predicate `facemenu-color-equal' to compare
679 color names. If the optional argument LIST is non-nil, it should
680 be a list of colors to display. Otherwise, this function uses
681 a list of colors that the current display can handle."
682 (let* ((list (mapcar 'list (or list (defined-colors))))
683 (l list))
684 (while (cdr l)
685 (if (and (facemenu-color-equal (car (car l)) (car (car (cdr l))))
686 (not (if (fboundp 'w32-default-color-map)
687 (not (assoc (car (car l)) (w32-default-color-map))))))
688 (progn
689 (setcdr (car l) (cons (car (car (cdr l))) (cdr (car l))))
690 (setcdr l (cdr (cdr l))))
691 (setq l (cdr l))))
692 list))
694 (defun facemenu-color-equal (a b)
695 "Return t if colors A and B are the same color.
696 A and B should be strings naming colors.
697 This function queries the display system to find out what the color
698 names mean. It returns nil if the colors differ or if it can't
699 determine the correct answer."
700 (cond ((equal a b) t)
701 ((equal (color-values a) (color-values b)))))
704 (defvar facemenu-self-insert-data nil)
706 (defun facemenu-post-self-insert-function ()
707 (when (and (car facemenu-self-insert-data)
708 (eq last-command (cdr facemenu-self-insert-data)))
709 (put-text-property (1- (point)) (point)
710 'face (car facemenu-self-insert-data))
711 (setq facemenu-self-insert-data nil))
712 (remove-hook 'post-self-insert-hook 'facemenu-post-self-insert-function))
714 (defun facemenu-set-self-insert-face (face)
715 "Arrange for the next self-inserted char to have face `face'."
716 (setq facemenu-self-insert-data (cons face this-command))
717 (add-hook 'post-self-insert-hook 'facemenu-post-self-insert-function))
719 (defun facemenu-add-face (face &optional start end)
720 "Add FACE to text between START and END.
721 If START is nil or START to END is empty, add FACE to next typed character
722 instead. For each section of that region that has a different face property,
723 FACE will be consed onto it, and other faces that are completely hidden by
724 that will be removed from the list.
725 If `facemenu-add-face-function' and maybe `facemenu-end-add-face' are non-nil,
726 they are used to set the face information.
728 As a special case, if FACE is `default', then the region is left with NO face
729 text property. Otherwise, selecting the default face would not have any
730 effect. See `facemenu-remove-face-function'."
731 (interactive "*xFace: \nr")
732 (cond
733 ((and (eq face 'default)
734 (not (eq facemenu-remove-face-function t)))
735 (if facemenu-remove-face-function
736 (funcall facemenu-remove-face-function start end)
737 (if (and start (< start end))
738 (remove-text-properties start end '(face default))
739 (facemenu-set-self-insert-face 'default))))
740 (facemenu-add-face-function
741 (save-excursion
742 (if end (goto-char end))
743 (save-excursion
744 (if start (goto-char start))
745 (insert-before-markers
746 (funcall facemenu-add-face-function face end)))
747 (if facemenu-end-add-face
748 (insert (if (stringp facemenu-end-add-face)
749 facemenu-end-add-face
750 (funcall facemenu-end-add-face face))))))
751 ((and start (< start end))
752 (let ((part-start start) part-end)
753 (while (not (= part-start end))
754 (setq part-end (next-single-property-change part-start 'face
755 nil end))
756 (let ((prev (get-text-property part-start 'face)))
757 (put-text-property part-start part-end 'face
758 (if (null prev)
759 face
760 (facemenu-active-faces
761 (cons face
762 (if (listp prev)
763 prev
764 (list prev)))
765 ;; Specify the selected frame
766 ;; because nil would mean to use
767 ;; the new-frame default settings,
768 ;; and those are usually nil.
769 (selected-frame)))))
770 (setq part-start part-end))))
772 (facemenu-set-self-insert-face
773 (if (eq last-command (cdr facemenu-self-insert-data))
774 (cons face (if (listp (car facemenu-self-insert-data))
775 (car facemenu-self-insert-data)
776 (list (car facemenu-self-insert-data))))
777 face))))
778 (unless (facemenu-enable-faces-p)
779 (message "Font-lock mode will override any faces you set in this buffer")))
781 (defun facemenu-active-faces (face-list &optional frame)
782 "Return from FACE-LIST those faces that would be used for display.
783 This means each face attribute is not specified in a face earlier in FACE-LIST
784 and such a face is therefore active when used to display text.
785 If the optional argument FRAME is given, use the faces in that frame; otherwise
786 use the selected frame. If t, then the global, non-frame faces are used."
787 (let* ((mask-atts (copy-sequence
788 (if (consp (car face-list))
789 (face-attributes-as-vector (car face-list))
790 (or (internal-lisp-face-p (car face-list) frame)
791 (check-face (car face-list))))))
792 (active-list (list (car face-list)))
793 (face-list (cdr face-list))
794 (mask-len (length mask-atts)))
795 (while face-list
796 (if (let ((face-atts
797 (if (consp (car face-list))
798 (face-attributes-as-vector (car face-list))
799 (or (internal-lisp-face-p (car face-list) frame)
800 (check-face (car face-list)))))
801 (i mask-len)
802 (useful nil))
803 (while (>= (setq i (1- i)) 0)
804 (and (not (memq (aref face-atts i) '(nil unspecified)))
805 (memq (aref mask-atts i) '(nil unspecified))
806 (aset mask-atts i (setq useful t))))
807 useful)
808 (setq active-list (cons (car face-list) active-list)))
809 (setq face-list (cdr face-list)))
810 (nreverse active-list)))
812 (defun facemenu-add-new-face (face)
813 "Add FACE (a face) to the Face menu if `facemenu-listed-faces' says so.
814 This is called whenever you create a new face, and at other times."
815 (let* (name
816 symbol
817 menu docstring
818 (key (cdr (assoc face facemenu-keybindings)))
819 function menu-val)
820 (if (symbolp face)
821 (setq name (symbol-name face)
822 symbol face)
823 (setq name face
824 symbol (intern name)))
825 (setq menu 'facemenu-face-menu)
826 (setq docstring
827 (purecopy (format "Select face `%s' for subsequent insertion.
828 If the mark is active and there is no prefix argument,
829 apply face `%s' to the region instead.
830 This command was defined by `facemenu-add-new-face'."
831 name name)))
832 (cond ((facemenu-iterate ; check if equivalent face is already in the menu
833 (lambda (m) (and (listp m)
834 (symbolp (car m))
835 ;; Avoid error in face-equal
836 ;; when a non-face is erroneously present.
837 (facep (car m))
838 (face-equal (car m) symbol)))
839 (cdr (symbol-function menu))))
840 ;; Faces with a keyboard equivalent. These go at the front.
841 (key
842 (setq function (intern (concat "facemenu-set-" name)))
843 (fset function
844 `(lambda ()
845 ,docstring
846 (interactive)
847 (facemenu-set-face
848 (quote ,symbol)
849 (if (and mark-active (not current-prefix-arg))
850 (region-beginning))
851 (if (and mark-active (not current-prefix-arg))
852 (region-end)))))
853 (define-key 'facemenu-keymap key (cons name function))
854 (define-key menu key (cons name function)))
855 ;; Faces with no keyboard equivalent. Figure out where to put it:
856 ((or (eq t facemenu-listed-faces)
857 (memq symbol facemenu-listed-faces))
858 (setq key (vector symbol)
859 function 'facemenu-set-face-from-menu
860 menu-val (symbol-function menu))
861 (if (and facemenu-new-faces-at-end
862 (> (length menu-val) 3))
863 (define-key-after menu-val key (cons name function)
864 (car (nth (- (length menu-val) 3) menu-val)))
865 (define-key menu key (cons name function))))))
866 nil) ; Return nil for facemenu-iterate
868 (defun facemenu-add-new-color (color menu)
869 "Add COLOR (a color name string) to the appropriate Face menu.
870 MENU should be `facemenu-foreground-menu' or `facemenu-background-menu'.
871 Return the event type (a symbol) of the added menu entry.
873 This is called whenever you use a new color."
874 (let (symbol docstring)
875 (unless (color-defined-p color)
876 (error "Color `%s' undefined" color))
877 (cond ((eq menu 'facemenu-foreground-menu)
878 (setq docstring
879 (format "Select foreground color %s for subsequent insertion."
880 color)
881 symbol (intern (concat "fg:" color))))
882 ((eq menu 'facemenu-background-menu)
883 (setq docstring
884 (format "Select background color %s for subsequent insertion."
885 color)
886 symbol (intern (concat "bg:" color))))
887 (t (error "MENU should be `facemenu-foreground-menu' or `facemenu-background-menu'")))
888 (unless (facemenu-iterate ; Check if color is already in the menu.
889 (lambda (m) (and (listp m)
890 (eq (car m) symbol)))
891 (cdr (symbol-function menu)))
892 ;; Color is not in the menu. Figure out where to put it.
893 (let ((key (vector symbol))
894 (function 'facemenu-set-face-from-menu)
895 (menu-val (symbol-function menu)))
896 (if (and facemenu-new-faces-at-end
897 (> (length menu-val) 3))
898 (define-key-after menu-val key (cons color function)
899 (car (nth (- (length menu-val) 3) menu-val)))
900 (define-key menu key (cons color function)))))
901 symbol))
903 (defun facemenu-complete-face-list (&optional oldlist)
904 "Return list of all faces that look different.
905 Starts with given ALIST of faces, and adds elements only if they display
906 differently from any face already on the list.
907 The faces on ALIST will end up at the end of the returned list, in reverse
908 order."
909 (let ((list (nreverse (mapcar 'car oldlist))))
910 (facemenu-iterate
911 (lambda (new-face)
912 (if (not (memq new-face list))
913 (setq list (cons new-face list)))
914 nil)
915 (nreverse (face-list)))
916 list))
918 (defun facemenu-iterate (func list)
919 "Apply FUNC to each element of LIST until one returns non-nil.
920 Returns the non-nil value it found, or nil if all were nil."
921 (while (and list (not (funcall func (car list))))
922 (setq list (cdr list)))
923 (car list))
925 (facemenu-update)
927 (provide 'facemenu)
929 ;; arch-tag: 85f6d02b-9085-420e-b651-0678f0e9c7eb
930 ;;; facemenu.el ends here