Output alists with dotted pair notation in .dir-locals.el
[emacs.git] / lisp / facemenu.el
blob7c10d6097c5a29279c6bb52fc9dacfd96513a0aa
1 ;;; facemenu.el --- create a face menu for interactively adding fonts to text
3 ;; Copyright (C) 1994-1996, 2001-2018 Free Software Foundation, Inc.
5 ;; Author: Boris Goldowsky <boris@gnu.org>
6 ;; Keywords: faces
7 ;; Package: emacs
9 ;; This file is part of GNU Emacs.
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 3 of the License, or
14 ;; (at your option) any later version.
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.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
24 ;;; Commentary:
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
28 ;; keybindings, which are shown in the menu.
30 ;; The menu also contains submenus for indentation and justification-changing
31 ;; commands.
33 ;;; Usage:
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.
40 ;; Faces can be selected from the keyboard as well.
41 ;; The standard keybindings are M-o (or ESC o) + letter:
42 ;; M-o i = "set italic", M-o b = "set bold", etc.
44 ;;; Customization:
45 ;; An alternative set of keybindings that may be easier to type can be set up
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:
52 ;; xmodmap -e "keysym Alt_L = Hyper_L" -e "add Mod2 = Hyper_L"
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:
55 ;; (setq facemenu-keybindings
56 ;; '((default . [?\H-d])
57 ;; (bold . [?\H-b])
58 ;; (italic . [?\H-i])
59 ;; (bold-italic . [?\H-l])
60 ;; (underline . [?\H-u])))
61 ;; (facemenu-update)
62 ;; (setq facemenu-keymap global-map)
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
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
68 ;; `facemenu-new-faces-at-end'. List faces that you want to use in documents
69 ;; in `facemenu-listed-faces'.
71 ;;; Known Problems:
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.
76 ;; There is at present no way to display what the faces look like in
77 ;; the menu itself.
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.
84 ;; Please send me any other problems, comments or ideas.
86 ;;; Code:
88 (eval-when-compile
89 (require 'help)
90 (require 'button))
92 ;; Global bindings:
93 (define-key global-map [C-down-mouse-2] 'facemenu-menu)
94 (define-key global-map "\M-o" 'facemenu-keymap)
96 (defgroup facemenu nil
97 "Create a face menu for interactively adding fonts to text."
98 :group 'faces
99 :prefix "facemenu-")
101 (defcustom facemenu-keybindings
102 (mapcar 'purecopy
103 '((default . "d")
104 (bold . "b")
105 (italic . "i")
106 (bold-italic . "l") ; {bold} intersect {italic} = {l}
107 (underline . "u")))
108 "Alist of interesting faces and keybindings.
109 Each element is itself a list: the car is the name of the face,
110 the next element is the key to use as a keyboard equivalent of the menu item;
111 the binding is made in `facemenu-keymap'.
113 The faces specifically mentioned in this list are put at the top of
114 the menu, in the order specified. All other faces which are defined
115 in `facemenu-listed-faces' are listed after them, but get no
116 keyboard equivalents.
118 If you change this variable after loading facemenu.el, you will need to call
119 `facemenu-update' to make it take effect."
120 :type '(repeat (cons face string))
121 :group 'facemenu)
123 (defcustom facemenu-new-faces-at-end t
124 "Where in the menu to insert newly-created faces.
125 This should be nil to put them at the top of the menu, or t to put them
126 just before \"Other\" at the end."
127 :type 'boolean
128 :group 'facemenu)
130 (defcustom facemenu-listed-faces nil
131 "List of faces to include in the Face menu.
132 Each element should be a symbol, the name of a face.
133 The \"basic \" faces in `facemenu-keybindings' are automatically
134 added to the Face menu, and need not be in this list.
136 This value takes effect when you load facemenu.el. If the
137 list includes symbols which are not defined as faces, they
138 are ignored; however, subsequently defining or creating
139 those faces adds them to the menu then. You can call
140 `facemenu-update' to recalculate the menu contents, such as
141 if you change the value of this variable,
143 If this variable is t, all faces that you apply to text
144 using the face menu commands (even by name), and all faces
145 that you define or create, are added to the menu. You may
146 find it useful to set this variable to t temporarily while
147 you define some faces, so that they will be added. However,
148 if the value is no longer t and you call `facemenu-update',
149 it will remove any faces not explicitly in the list."
150 :type '(choice (const :tag "List all faces" t)
151 (const :tag "None" nil)
152 (repeat symbol))
153 :group 'facemenu
154 :version "22.1")
156 (defvar facemenu-face-menu
157 (let ((map (make-sparse-keymap "Face")))
158 (define-key map "o" (cons (purecopy "Other...") 'facemenu-set-face))
159 map)
160 "Menu keymap for faces.")
161 (defalias 'facemenu-face-menu facemenu-face-menu)
162 (put 'facemenu-face-menu 'menu-enable '(facemenu-enable-faces-p))
164 (defvar facemenu-foreground-menu
165 (let ((map (make-sparse-keymap "Foreground Color")))
166 (define-key map "o" (cons (purecopy "Other...") 'facemenu-set-foreground))
167 map)
168 "Menu keymap for foreground colors.")
169 (defalias 'facemenu-foreground-menu facemenu-foreground-menu)
170 (put 'facemenu-foreground-menu 'menu-enable '(facemenu-enable-faces-p))
172 (defvar facemenu-background-menu
173 (let ((map (make-sparse-keymap "Background Color")))
174 (define-key map "o" (cons (purecopy "Other...") 'facemenu-set-background))
175 map)
176 "Menu keymap for background colors.")
177 (defalias 'facemenu-background-menu facemenu-background-menu)
178 (put 'facemenu-background-menu 'menu-enable '(facemenu-enable-faces-p))
180 ;;; Condition for enabling menu items that set faces.
181 (defun facemenu-enable-faces-p ()
182 ;; Enable the facemenu if facemenu-add-face-function is defined
183 ;; (e.g. in Tex-mode and SGML mode), or if font-lock is off.
184 (or (not (and font-lock-mode font-lock-defaults))
185 facemenu-add-face-function))
187 (defvar facemenu-special-menu
188 (let ((map (make-sparse-keymap "Special")))
189 (define-key map [?s] (cons (purecopy "Remove Special")
190 'facemenu-remove-special))
191 (define-key map [?c] (cons (purecopy "Charset")
192 'facemenu-set-charset))
193 (define-key map [?t] (cons (purecopy "Intangible")
194 'facemenu-set-intangible))
195 (define-key map [?v] (cons (purecopy "Invisible")
196 'facemenu-set-invisible))
197 (define-key map [?r] (cons (purecopy "Read-Only")
198 'facemenu-set-read-only))
199 map)
200 "Menu keymap for non-face text-properties.")
201 (defalias 'facemenu-special-menu facemenu-special-menu)
203 (defvar facemenu-justification-menu
204 (let ((map (make-sparse-keymap "Justification")))
205 (define-key map [?c] (cons (purecopy "Center") 'set-justification-center))
206 (define-key map [?b] (cons (purecopy "Full") 'set-justification-full))
207 (define-key map [?r] (cons (purecopy "Right") 'set-justification-right))
208 (define-key map [?l] (cons (purecopy "Left") 'set-justification-left))
209 (define-key map [?u] (cons (purecopy "Unfilled") 'set-justification-none))
210 map)
211 "Submenu for text justification commands.")
212 (defalias 'facemenu-justification-menu facemenu-justification-menu)
214 (defvar facemenu-indentation-menu
215 (let ((map (make-sparse-keymap "Indentation")))
216 (define-key map [decrease-right-margin]
217 (cons (purecopy "Indent Right Less") 'decrease-right-margin))
218 (define-key map [increase-right-margin]
219 (cons (purecopy "Indent Right More") 'increase-right-margin))
220 (define-key map [decrease-left-margin]
221 (cons (purecopy "Indent Less") 'decrease-left-margin))
222 (define-key map [increase-left-margin]
223 (cons (purecopy "Indent More") 'increase-left-margin))
224 map)
225 "Submenu for indentation commands.")
226 (defalias 'facemenu-indentation-menu facemenu-indentation-menu)
228 ;; This is split up to avoid an overlong line in loaddefs.el.
229 (defvar facemenu-menu nil
230 "Facemenu top-level menu keymap.")
231 (setq facemenu-menu (make-sparse-keymap "Text Properties"))
232 (let ((map facemenu-menu))
233 (define-key map [dc] (cons (purecopy "Display Colors") 'list-colors-display))
234 (define-key map [df] (cons (purecopy "Display Faces") 'list-faces-display))
235 (define-key map [dp] (cons (purecopy "Describe Properties")
236 'describe-text-properties))
237 (define-key map [ra] (list 'menu-item (purecopy "Remove Text Properties")
238 'facemenu-remove-all
239 :enable 'mark-active))
240 (define-key map [rm] (list 'menu-item (purecopy "Remove Face Properties")
241 'facemenu-remove-face-props
242 :enable 'mark-active))
243 (define-key map [s1] (list (purecopy "--"))))
244 (let ((map facemenu-menu))
245 (define-key map [in] (cons (purecopy "Indentation")
246 'facemenu-indentation-menu))
247 (define-key map [ju] (cons (purecopy "Justification")
248 'facemenu-justification-menu))
249 (define-key map [s2] (list (purecopy "--")))
250 (define-key map [sp] (cons (purecopy "Special Properties")
251 'facemenu-special-menu))
252 (define-key map [bg] (cons (purecopy "Background Color")
253 'facemenu-background-menu))
254 (define-key map [fg] (cons (purecopy "Foreground Color")
255 'facemenu-foreground-menu))
256 (define-key map [fc] (cons (purecopy "Face")
257 'facemenu-face-menu)))
258 (defalias 'facemenu-menu facemenu-menu)
260 (defvar facemenu-keymap
261 (let ((map (make-sparse-keymap "Set face")))
262 (define-key map "o" (cons (purecopy "Other...") 'facemenu-set-face))
263 (define-key map "\M-o" 'font-lock-fontify-block)
264 map)
265 "Keymap for face-changing commands.
266 `Facemenu-update' fills in the keymap according to the bindings
267 requested in `facemenu-keybindings'.")
268 (defalias 'facemenu-keymap facemenu-keymap)
271 (defcustom facemenu-add-face-function nil
272 "Function called at beginning of text to change or nil.
273 This function is passed the FACE to set and END of text to change, and must
274 return a string which is inserted. It may set `facemenu-end-add-face'."
275 :type '(choice (const :tag "None" nil)
276 function)
277 :group 'facemenu)
279 (defcustom facemenu-end-add-face nil
280 "String to insert or function called at end of text to change or nil.
281 This function is passed the FACE to set, and must return a string which is
282 inserted."
283 :type '(choice (const :tag "None" nil)
284 string
285 function)
286 :group 'facemenu)
288 (defcustom facemenu-remove-face-function nil
289 "When non-nil, this is a function called to remove faces.
290 This function is passed the START and END of text to change.
291 May also be t meaning to use `facemenu-add-face-function'."
292 :type '(choice (const :tag "None" nil)
293 (const :tag "Use add-face" t)
294 function)
295 :group 'facemenu)
297 ;;; Internal Variables
299 (defvar facemenu-color-alist nil
300 "Alist of colors, used for completion.
301 If this is nil, then the value of (defined-colors) is used.")
303 (defun facemenu-update ()
304 "Add or update the \"Face\" menu in the menu bar.
305 You can call this to update things if you change any of the menu configuration
306 variables."
307 (interactive)
309 ;; Add each defined face to the menu.
310 (facemenu-iterate 'facemenu-add-new-face
311 (facemenu-complete-face-list facemenu-keybindings)))
313 (defun facemenu-set-face (face &optional start end)
314 "Apply FACE to the region or next character typed.
316 If the region is active (normally true except in Transient
317 Mark mode) and nonempty, and there is no prefix argument,
318 this command applies FACE to the region. Otherwise, it applies FACE
319 to the faces to use for the next character
320 inserted. (Moving point or switching buffers before typing
321 a character to insert cancels the specification.)
323 If FACE is `default', to \"apply\" it means clearing
324 the list of faces to be used. For any other value of FACE,
325 to \"apply\" it means putting FACE at the front of the list
326 of faces to be used, and removing any faces further
327 along in the list that would be completely overridden by
328 preceding faces (including FACE).
330 This command can also add FACE to the menu of faces,
331 if `facemenu-listed-faces' says to do that."
332 (interactive (list (progn
333 (barf-if-buffer-read-only)
334 (read-face-name "Use face" (face-at-point t)))
335 (if (and mark-active (not current-prefix-arg))
336 (region-beginning))
337 (if (and mark-active (not current-prefix-arg))
338 (region-end))))
339 (facemenu-add-new-face face)
340 (facemenu-add-face face start end))
342 (defun facemenu-set-foreground (color &optional start end)
343 "Set the foreground COLOR of the region or next character typed.
344 This command reads the color in the minibuffer.
346 If the region is active (normally true except in Transient Mark mode)
347 and there is no prefix argument, this command sets the region to the
348 requested face.
350 Otherwise, this command specifies the face for the next character
351 inserted. Moving point or switching buffers before
352 typing a character to insert cancels the specification."
353 (interactive (list (progn
354 (barf-if-buffer-read-only)
355 (read-color "Foreground color: "))
356 (if (and mark-active (not current-prefix-arg))
357 (region-beginning))
358 (if (and mark-active (not current-prefix-arg))
359 (region-end))))
360 (facemenu-set-face-from-menu
361 (facemenu-add-new-color color 'facemenu-foreground-menu)
362 start end))
364 (defun facemenu-set-background (color &optional start end)
365 "Set the background COLOR of the region or next character typed.
366 This command reads the color in the minibuffer.
368 If the region is active (normally true except in Transient Mark mode)
369 and there is no prefix argument, this command sets the region to the
370 requested face.
372 Otherwise, this command specifies the face for the next character
373 inserted. Moving point or switching buffers before
374 typing a character to insert cancels the specification."
375 (interactive (list (progn
376 (barf-if-buffer-read-only)
377 (read-color "Background color: "))
378 (if (and mark-active (not current-prefix-arg))
379 (region-beginning))
380 (if (and mark-active (not current-prefix-arg))
381 (region-end))))
382 (facemenu-set-face-from-menu
383 (facemenu-add-new-color color 'facemenu-background-menu)
384 start end))
386 (defun facemenu-set-face-from-menu (face start end)
387 "Set the FACE of the region or next character typed.
388 This function is designed to be called from a menu; FACE is determined
389 using the event type of the menu entry. If FACE is a symbol whose
390 name starts with \"fg:\" or \"bg:\", then this functions sets the
391 foreground or background to the color specified by the rest of the
392 symbol's name. Any other symbol is considered the name of a face.
394 If the region is active (normally true except in Transient Mark mode)
395 and there is no prefix argument, this command sets the region to the
396 requested face.
398 Otherwise, this command specifies the face for the next character
399 inserted. Moving point or switching buffers before typing a character
400 to insert cancels the specification."
401 (interactive (list last-command-event
402 (if (and mark-active (not current-prefix-arg))
403 (region-beginning))
404 (if (and mark-active (not current-prefix-arg))
405 (region-end))))
406 (barf-if-buffer-read-only)
407 (facemenu-add-face
408 (let ((fn (symbol-name face)))
409 (if (string-match "\\`\\([fb]\\)g:\\(.+\\)" fn)
410 (list (list (if (string= (match-string 1 fn) "f")
411 :foreground
412 :background)
413 (match-string 2 fn)))
414 face))
415 start end))
417 (defun facemenu-set-invisible (start end)
418 "Make the region invisible.
419 This sets the `invisible' text property; it can be undone with
420 `facemenu-remove-special'."
421 (interactive "r")
422 (add-text-properties start end '(invisible t)))
424 (defun facemenu-set-intangible (start end)
425 "Make the region intangible: disallow moving into it.
426 This sets the `intangible' text property; it can be undone with
427 `facemenu-remove-special'."
428 (interactive "r")
429 (add-text-properties start end '(intangible t)))
431 (defun facemenu-set-read-only (start end)
432 "Make the region unmodifiable.
433 This sets the `read-only' text property; it can be undone with
434 `facemenu-remove-special'."
435 (interactive "r")
436 (add-text-properties start end '(read-only t)))
438 (defun facemenu-set-charset (cset &optional start end)
439 "Apply CHARSET text property to the region or next character typed.
441 If the region is active (normally true except in Transient
442 Mark mode) and nonempty, and there is no prefix argument,
443 this command adds CHARSET property to the region. Otherwise, it
444 sets the CHARSET property of the character at point."
445 (interactive (list (progn
446 (barf-if-buffer-read-only)
447 (read-charset
448 (format "Use charset (default %s): " (charset-after))
449 (charset-after)))
450 (if (and mark-active (not current-prefix-arg))
451 (region-beginning))
452 (if (and mark-active (not current-prefix-arg))
453 (region-end))))
454 (or start
455 (setq start (min (point) (1- (point-max)))
456 end (1+ start)))
457 (remove-text-properties start end '(charset nil))
458 (put-text-property start end 'charset cset))
460 (defun facemenu-remove-face-props (start end)
461 "Remove `face' and `mouse-face' text properties."
462 (interactive "*r") ; error if buffer is read-only despite the next line.
463 (let ((inhibit-read-only t))
464 (remove-text-properties
465 start end '(face nil mouse-face nil))))
467 (defun facemenu-remove-all (start end)
468 "Remove all text properties from the region."
469 (interactive "*r") ; error if buffer is read-only despite the next line.
470 (let ((inhibit-read-only t))
471 (set-text-properties start end nil)))
473 (defun facemenu-remove-special (start end)
474 "Remove all the \"special\" text properties from the region.
475 These special properties include `invisible', `intangible' and `read-only'."
476 (interactive "*r") ; error if buffer is read-only despite the next line.
477 (let ((inhibit-read-only t))
478 (remove-text-properties
479 start end '(invisible nil intangible nil read-only nil charset nil))))
481 (defalias 'facemenu-read-color 'read-color)
483 (defcustom list-colors-sort nil
484 "Color sort order for `list-colors-display'.
485 nil means default implementation-dependent order (defined in `x-colors').
486 `name' sorts by color name.
487 `rgb' sorts by red, green, blue components.
488 `(rgb-dist . COLOR)' sorts by the RGB distance to the specified color.
489 `hsv' sorts by hue, saturation, value.
490 `(hsv-dist . COLOR)' sorts by the HSV distance to the specified color
491 and excludes grayscale colors.
492 `luminance' sorts by relative luminance in the CIE XYZ color space."
493 :type '(choice (const :tag "Unsorted" nil)
494 (const :tag "Color Name" name)
495 (const :tag "Red-Green-Blue" rgb)
496 (cons :tag "Distance on RGB cube"
497 (const :tag "Distance from Color" rgb-dist)
498 (color :tag "Source Color Name"))
499 (const :tag "Hue-Saturation-Value" hsv)
500 (cons :tag "Distance on HSV cylinder"
501 (const :tag "Distance from Color" hsv-dist)
502 (color :tag "Source Color Name"))
503 (const :tag "Luminance" luminance))
504 :group 'facemenu
505 :version "24.1")
507 (defun list-colors-sort-key (color)
508 "Return a list of keys for sorting colors depending on `list-colors-sort'.
509 COLOR is the name of the color. When return value is nil,
510 filter out the color from the output."
511 (require 'color)
512 (cond
513 ((null list-colors-sort) color)
514 ((eq list-colors-sort 'name)
515 (downcase color))
516 ((eq list-colors-sort 'rgb)
517 (color-values color))
518 ((eq (car-safe list-colors-sort) 'rgb-dist)
519 (color-distance color (cdr list-colors-sort)))
520 ((eq list-colors-sort 'hsv)
521 (apply 'color-rgb-to-hsv (color-name-to-rgb color)))
522 ((eq (car-safe list-colors-sort) 'hsv-dist)
523 (let* ((c-rgb (color-name-to-rgb color))
524 (c-hsv (apply 'color-rgb-to-hsv c-rgb))
525 (o-hsv (apply 'color-rgb-to-hsv
526 (color-name-to-rgb (cdr list-colors-sort)))))
527 (unless (and (eq (nth 0 c-rgb) (nth 1 c-rgb)) ; exclude grayscale
528 (eq (nth 1 c-rgb) (nth 2 c-rgb)))
529 ;; 3D Euclidean distance (sqrt is not needed for sorting)
530 (+ (expt (- 180 (abs (- 180 (abs (- (nth 0 c-hsv) ; wrap hue
531 (nth 0 o-hsv)))))) 2)
532 (expt (- (nth 1 c-hsv) (nth 1 o-hsv)) 2)
533 (expt (- (nth 2 c-hsv) (nth 2 o-hsv)) 2)))))
534 ((eq list-colors-sort 'luminance)
535 (let ((c-rgb (color-name-to-rgb color)))
536 (+ (* (nth 0 c-rgb) 0.21266729)
537 (* (nth 1 c-rgb) 0.7151522)
538 (* (nth 2 c-rgb) 0.0721750))))))
540 (defvar list-colors-callback nil
541 "Value of CALLBACK arg passed to `list-colors-display'; internal use.")
543 (defun list-colors-redisplay (_ignore-auto _noconfirm)
544 "Redisplay the colors using `list-colors-sort'.
546 This is installed as a `revert-buffer-function' in the *Colors* buffer."
547 (list-colors-display nil (buffer-name) list-colors-callback))
549 (defun list-colors-display (&optional list buffer-name callback)
550 "Display names of defined colors, and show what they look like.
551 If the optional argument LIST is non-nil, it should be a list of
552 colors to display. Otherwise, this command computes a list of
553 colors that the current display can handle. Customize
554 `list-colors-sort' to change the order in which colors are shown.
555 Type `g' or \\[revert-buffer] after customizing `list-colors-sort'
556 to redisplay colors in the new order.
558 If the optional argument BUFFER-NAME is nil, it defaults to *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 name."
563 (interactive)
564 (when (and (null list) (> (display-color-cells) 0))
565 (setq list (list-colors-duplicates (defined-colors)))
566 (when list-colors-sort
567 ;; Schwartzian transform with `(color key1 key2 key3 ...)'.
568 (setq list (mapcar
569 'car
570 (sort (delq nil (mapcar
571 (lambda (c)
572 (let ((key (list-colors-sort-key
573 (car c))))
574 (when key
575 (cons c (if (consp key) key
576 (list key))))))
577 list))
578 (lambda (a b)
579 (let* ((a-keys (cdr a))
580 (b-keys (cdr b))
581 (a-key (car a-keys))
582 (b-key (car b-keys)))
583 ;; Skip common keys at the beginning of key lists.
584 (while (and a-key b-key (equal a-key b-key))
585 (setq a-keys (cdr a-keys) a-key (car a-keys)
586 b-keys (cdr b-keys) b-key (car b-keys)))
587 (cond
588 ((and (numberp a-key) (numberp b-key))
589 (< a-key b-key))
590 ((and (stringp a-key) (stringp b-key))
591 (string< a-key b-key)))))))))
592 (when (memq (display-visual-class) '(gray-scale pseudo-color direct-color))
593 ;; Don't show more than what the display can handle.
594 (let ((lc (nthcdr (1- (display-color-cells)) list)))
595 (if lc
596 (setcdr lc nil)))))
597 (unless buffer-name
598 (setq buffer-name "*Colors*"))
599 (with-help-window buffer-name
600 (with-current-buffer standard-output
601 (erase-buffer)
602 (list-colors-print list callback)
603 (set-buffer-modified-p nil)
604 (setq truncate-lines t)
605 (setq-local list-colors-callback callback)
606 (setq revert-buffer-function 'list-colors-redisplay)))
607 (when callback
608 (pop-to-buffer buffer-name)
609 (message "Click on a color to select it.")))
611 (defun list-colors-print (list &optional callback)
612 (let ((callback-fn
613 (if callback
614 `(lambda (button)
615 (funcall ,callback (button-get button 'color-name))))))
616 (dolist (color list)
617 (if (consp color)
618 (if (cdr color)
619 (setq color (sort color (lambda (a b)
620 (string< (downcase a)
621 (downcase b))))))
622 (setq color (list color)))
623 (let* ((opoint (point))
624 (color-values (color-values (car color)))
625 (light-p (>= (apply 'max color-values)
626 (* (car (color-values "white")) .5))))
627 (insert (car color))
628 (indent-to 22)
629 (put-text-property opoint (point) 'face `(:background ,(car color)))
630 (put-text-property
631 (prog1 (point)
632 (insert " ")
633 ;; Insert all color names.
634 (insert (mapconcat 'identity color ",")))
635 (point)
636 'face (list :foreground (car color)))
637 (insert (propertize " " 'display '(space :align-to (- right 9))))
638 (insert " ")
639 (insert (propertize
640 (apply 'format "#%02x%02x%02x"
641 (mapcar (lambda (c) (ash c -8))
642 color-values))
643 'mouse-face 'highlight
644 'help-echo
645 (let ((hsv (apply 'color-rgb-to-hsv
646 (color-name-to-rgb (car color)))))
647 (format "H:%.2f S:%.2f V:%.2f"
648 (nth 0 hsv) (nth 1 hsv) (nth 2 hsv)))))
649 (when callback
650 (make-text-button
651 opoint (point)
652 'follow-link t
653 'mouse-face (list :background (car color)
654 :foreground (if light-p "black" "white"))
655 'color-name (car color)
656 'action callback-fn)))
657 (insert "\n"))
658 (goto-char (point-min))))
661 (defun list-colors-duplicates (&optional list)
662 "Return a list of colors with grouped duplicate colors.
663 If a color has no duplicates, then the element of the returned list
664 has the form (COLOR-NAME). The element of the returned list with
665 duplicate colors has the form (COLOR-NAME DUPLICATE-COLOR-NAME ...).
666 This function uses the predicate `facemenu-color-equal' to compare
667 color names. If the optional argument LIST is non-nil, it should
668 be a list of colors to display. Otherwise, this function uses
669 a list of colors that the current display can handle."
670 (let* ((list (mapcar 'list (or list (defined-colors))))
671 (l list))
672 (while (cdr l)
673 (if (and (facemenu-color-equal (car (car l)) (car (car (cdr l))))
674 ;; On MS-Windows, there are logical colors that might have
675 ;; the same value but different names and meanings. For
676 ;; example, `SystemMenuText' (the color w32 uses for the
677 ;; text in menu entries) and `SystemWindowText' (the default
678 ;; color w32 uses for the text in windows and dialogs) may
679 ;; be the same display color and be adjacent in the list.
680 ;; These system colors all have names prefixed with "System",
681 ;; which is hardcoded in w32fns.c (SYSTEM_COLOR_PREFIX).
682 ;; This makes them different to any other color. Bug#9722
683 (not (and (eq system-type 'windows-nt)
684 (string-match-p "^System" (car (car l))))))
685 (progn
686 (setcdr (car l) (cons (car (car (cdr l))) (cdr (car l))))
687 (setcdr l (cdr (cdr l))))
688 (setq l (cdr l))))
689 list))
691 (defun facemenu-color-equal (a b)
692 "Return t if colors A and B are the same color.
693 A and B should be strings naming colors.
694 This function queries the display system to find out what the color
695 names mean. It returns nil if the colors differ or if it can't
696 determine the correct answer."
697 (cond ((equal a b) t)
698 ((equal (color-values a) (color-values b)))))
701 (defvar facemenu-self-insert-data nil)
703 (defun facemenu-post-self-insert-function ()
704 (when (and (car facemenu-self-insert-data)
705 (eq last-command (cdr facemenu-self-insert-data)))
706 (put-text-property (1- (point)) (point)
707 'face (car facemenu-self-insert-data))
708 (setq facemenu-self-insert-data nil))
709 (remove-hook 'post-self-insert-hook 'facemenu-post-self-insert-function))
711 (defun facemenu-set-self-insert-face (face)
712 "Arrange for the next self-inserted char to have face `face'."
713 (setq facemenu-self-insert-data (cons face this-command))
714 (add-hook 'post-self-insert-hook 'facemenu-post-self-insert-function))
716 (defun facemenu-add-face (face &optional start end)
717 "Add FACE to text between START and END.
718 If START is nil or START to END is empty, add FACE to next typed character
719 instead. For each section of that region that has a different face property,
720 FACE will be consed onto it, and other faces that are completely hidden by
721 that will be removed from the list.
722 If `facemenu-add-face-function' and maybe `facemenu-end-add-face' are non-nil,
723 they are used to set the face information.
725 As a special case, if FACE is `default', then the region is left with NO face
726 text property. Otherwise, selecting the default face would not have any
727 effect. See `facemenu-remove-face-function'."
728 (interactive "*xFace: \nr")
729 (cond
730 ((and (eq face 'default)
731 (not (eq facemenu-remove-face-function t)))
732 (if facemenu-remove-face-function
733 (funcall facemenu-remove-face-function start end)
734 (if (and start (< start end))
735 (remove-text-properties start end '(face default))
736 (facemenu-set-self-insert-face 'default))))
737 (facemenu-add-face-function
738 (save-excursion
739 (if end (goto-char end))
740 (save-excursion
741 (if start (goto-char start))
742 (insert-before-markers
743 (funcall facemenu-add-face-function face end)))
744 (if facemenu-end-add-face
745 (insert (if (stringp facemenu-end-add-face)
746 facemenu-end-add-face
747 (funcall facemenu-end-add-face face))))))
748 ((and start (< start end))
749 (let ((part-start start) part-end)
750 (while (not (= part-start end))
751 (setq part-end (next-single-property-change part-start 'face
752 nil end))
753 (let ((prev (get-text-property part-start 'face)))
754 (put-text-property part-start part-end 'face
755 (if (null prev)
756 face
757 (facemenu-active-faces
758 (cons face
759 (if (face-list-p prev)
760 prev
761 (list prev)))
762 ;; Specify the selected frame
763 ;; because nil would mean to use
764 ;; the new-frame default settings,
765 ;; and those are usually nil.
766 (selected-frame)))))
767 (setq part-start part-end))))
769 (facemenu-set-self-insert-face
770 (if (eq last-command (cdr facemenu-self-insert-data))
771 (cons face (if (listp (car facemenu-self-insert-data))
772 (car facemenu-self-insert-data)
773 (list (car facemenu-self-insert-data))))
774 face))))
775 (unless (facemenu-enable-faces-p)
776 (message "Font-lock mode will override any faces you set in this buffer")))
778 (defun facemenu-active-faces (face-list &optional frame)
779 "Return from FACE-LIST those faces that would be used for display.
780 This means each face attribute is not specified in a face earlier in FACE-LIST
781 and such a face is therefore active when used to display text.
782 If the optional argument FRAME is given, use the faces in that frame; otherwise
783 use the selected frame. If t, then the global, non-frame faces are used."
784 (let* ((mask-atts (copy-sequence
785 (if (consp (car face-list))
786 (face-attributes-as-vector (car face-list))
787 (or (internal-lisp-face-p (car face-list) frame)
788 (check-face (car face-list))))))
789 (active-list (list (car face-list)))
790 (face-list (cdr face-list))
791 (mask-len (length mask-atts)))
792 (while face-list
793 (if (let ((face-atts
794 (if (consp (car face-list))
795 (face-attributes-as-vector (car face-list))
796 (or (internal-lisp-face-p (car face-list) frame)
797 (check-face (car face-list)))))
798 (i mask-len)
799 (useful nil))
800 (while (>= (setq i (1- i)) 0)
801 (and (not (memq (aref face-atts i) '(nil unspecified)))
802 (memq (aref mask-atts i) '(nil unspecified))
803 (aset mask-atts i (setq useful t))))
804 useful)
805 (setq active-list (cons (car face-list) active-list)))
806 (setq face-list (cdr face-list)))
807 (nreverse active-list)))
809 (defun facemenu-add-new-face (face)
810 "Add FACE (a face) to the Face menu if `facemenu-listed-faces' says so.
811 This is called whenever you create a new face, and at other times."
812 (let* (name
813 symbol
814 menu docstring
815 (key (cdr (assoc face facemenu-keybindings)))
816 function menu-val)
817 (if (symbolp face)
818 (setq name (symbol-name face)
819 symbol face)
820 (setq name face
821 symbol (intern name)))
822 (setq menu 'facemenu-face-menu)
823 (setq docstring
824 (purecopy (format "Select face `%s' for subsequent insertion.
825 If the mark is active and there is no prefix argument,
826 apply face `%s' to the region instead.
827 This command was defined by `facemenu-add-new-face'."
828 name name)))
829 (cond ((facemenu-iterate ; check if equivalent face is already in the menu
830 (lambda (m) (and (listp m)
831 (symbolp (car m))
832 ;; Avoid error in face-equal
833 ;; when a non-face is erroneously present.
834 (facep (car m))
835 (face-equal (car m) symbol)))
836 (cdr (symbol-function menu))))
837 ;; Faces with a keyboard equivalent. These go at the front.
838 (key
839 (setq function (intern (concat "facemenu-set-" name)))
840 (fset function
841 `(lambda ()
842 ,docstring
843 (interactive)
844 (facemenu-set-face
845 (quote ,symbol)
846 (if (and mark-active (not current-prefix-arg))
847 (region-beginning))
848 (if (and mark-active (not current-prefix-arg))
849 (region-end)))))
850 (define-key 'facemenu-keymap key (cons name function))
851 (define-key menu key (cons name function)))
852 ;; Faces with no keyboard equivalent. Figure out where to put it:
853 ((or (eq t facemenu-listed-faces)
854 (memq symbol facemenu-listed-faces))
855 (setq key (vector symbol)
856 function 'facemenu-set-face-from-menu
857 menu-val (symbol-function menu))
858 (if (and facemenu-new-faces-at-end
859 (> (length menu-val) 3))
860 (define-key-after menu-val key (cons name function)
861 (car (nth (- (length menu-val) 3) menu-val)))
862 (define-key menu key (cons name function))))))
863 nil) ; Return nil for facemenu-iterate
865 (defun facemenu-add-new-color (color menu)
866 "Add COLOR (a color name string) to the appropriate Face menu.
867 MENU should be `facemenu-foreground-menu' or `facemenu-background-menu'.
868 Return the event type (a symbol) of the added menu entry.
870 This is called whenever you use a new color."
871 (let (symbol)
872 (unless (color-defined-p color)
873 (error "Color `%s' undefined" color))
874 (cond ((eq menu 'facemenu-foreground-menu)
875 (setq symbol (intern (concat "fg:" color))))
876 ((eq menu 'facemenu-background-menu)
877 (setq symbol (intern (concat "bg:" color))))
878 (t (error "MENU should be `facemenu-foreground-menu' or `facemenu-background-menu'")))
879 (unless (facemenu-iterate ; Check if color is already in the menu.
880 (lambda (m) (and (listp m)
881 (eq (car m) symbol)))
882 (cdr (symbol-function menu)))
883 ;; Color is not in the menu. Figure out where to put it.
884 (let ((key (vector symbol))
885 (function 'facemenu-set-face-from-menu)
886 (menu-val (symbol-function menu)))
887 (if (and facemenu-new-faces-at-end
888 (> (length menu-val) 3))
889 (define-key-after menu-val key (cons color function)
890 (car (nth (- (length menu-val) 3) menu-val)))
891 (define-key menu key (cons color function)))))
892 symbol))
894 (defun facemenu-complete-face-list (&optional oldlist)
895 "Return list of all faces that look different.
896 Starts with given ALIST of faces, and adds elements only if they display
897 differently from any face already on the list.
898 The faces on ALIST will end up at the end of the returned list, in reverse
899 order."
900 (let ((list (nreverse (mapcar 'car oldlist))))
901 (facemenu-iterate
902 (lambda (new-face)
903 (if (not (memq new-face list))
904 (setq list (cons new-face list)))
905 nil)
906 (nreverse (face-list)))
907 list))
909 (defun facemenu-iterate (func list)
910 "Apply FUNC to each element of LIST until one returns non-nil.
911 Returns the non-nil value it found, or nil if all were nil."
912 (while (and list (not (funcall func (car list))))
913 (setq list (cdr list)))
914 (car list))
916 (facemenu-update)
918 (provide 'facemenu)
920 ;;; facemenu.el ends here