* src/process.c: Include <sys/ioctl.h> unconditionally,
[emacs.git] / lisp / facemenu.el
blobf2a7958d93b56a1ed0b2bc1240f1fb3c054d63ea
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 (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 (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 (defalias 'facemenu-read-color 'read-color)
467 (defun color-rgb-to-hsv (r g b)
468 "For R, G, B color components return a list of hue, saturation, value.
469 R, G, B input values should be in [0..65535] range.
470 Output values for hue are integers in [0..360] range.
471 Output values for saturation and value are integers in [0..100] range."
472 (let* ((r (/ r 65535.0))
473 (g (/ g 65535.0))
474 (b (/ b 65535.0))
475 (max (max r g b))
476 (min (min r g b))
477 (h (cond ((= max min) 0)
478 ((= max r) (mod (+ (* 60 (/ (- g b) (- max min))) 360) 360))
479 ((= max g) (+ (* 60 (/ (- b r) (- max min))) 120))
480 ((= max b) (+ (* 60 (/ (- r g) (- max min))) 240))))
481 (s (cond ((= max 0) 0)
482 (t (- 1 (/ min max)))))
483 (v max))
484 (list (round h) (round s 0.01) (round v 0.01))))
486 (defcustom list-colors-sort nil
487 "Color sort order for `list-colors-display'.
488 `nil' means default implementation-dependent order (defined in `x-colors').
489 `name' sorts by color name.
490 `rgb' sorts by red, green, blue components.
491 `(rgb-dist . COLOR)' sorts by the RGB distance to the specified color.
492 `hsv' sorts by hue, saturation, value.
493 `(hsv-dist . COLOR)' sorts by the HSV distance to the specified color
494 and excludes grayscale colors."
495 :type '(choice (const :tag "Unsorted" nil)
496 (const :tag "Color Name" name)
497 (const :tag "Red-Green-Blue" rgb)
498 (cons :tag "Distance on RGB cube"
499 (const :tag "Distance from Color" rgb-dist)
500 (color :tag "Source Color Name"))
501 (const :tag "Hue-Saturation-Value" hsv)
502 (cons :tag "Distance on HSV cylinder"
503 (const :tag "Distance from Color" hsv-dist)
504 (color :tag "Source Color Name")))
505 :group 'facemenu
506 :version "24.1")
508 (defun list-colors-sort-key (color)
509 "Return a list of keys for sorting colors depending on `list-colors-sort'.
510 COLOR is the name of the color. When return value is nil,
511 filter out the color from the output."
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-values color)))
522 ((eq (car-safe list-colors-sort) 'hsv-dist)
523 (let* ((c-rgb (color-values color))
524 (c-hsv (apply 'color-rgb-to-hsv c-rgb))
525 (o-hsv (apply 'color-rgb-to-hsv
526 (color-values (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)))))))
535 (defun list-colors-display (&optional list buffer-name callback)
536 "Display names of defined colors, and show what they look like.
537 If the optional argument LIST is non-nil, it should be a list of
538 colors to display. Otherwise, this command computes a list of
539 colors that the current display can handle.
541 If the optional argument BUFFER-NAME is nil, it defaults to
542 *Colors*.
544 If the optional argument CALLBACK is non-nil, it should be a
545 function to call each time the user types RET or clicks on a
546 color. The function should accept a single argument, the color
547 name.
549 You can change the color sort order by customizing `list-colors-sort'."
550 (interactive)
551 (when (and (null list) (> (display-color-cells) 0))
552 (setq list (list-colors-duplicates (defined-colors)))
553 (when list-colors-sort
554 ;; Schwartzian transform with `(color key1 key2 key3 ...)'.
555 (setq list (mapcar
556 'car
557 (sort (delq nil (mapcar
558 (lambda (c)
559 (let ((key (list-colors-sort-key
560 (car c))))
561 (when key
562 (cons c (if (consp key) key
563 (list key))))))
564 list))
565 (lambda (a b)
566 (let* ((a-keys (cdr a))
567 (b-keys (cdr b))
568 (a-key (car a-keys))
569 (b-key (car b-keys)))
570 ;; Skip common keys at the beginning of key lists.
571 (while (and a-key b-key (equal a-key b-key))
572 (setq a-keys (cdr a-keys) a-key (car a-keys)
573 b-keys (cdr b-keys) b-key (car b-keys)))
574 (cond
575 ((and (numberp a-key) (numberp b-key))
576 (< a-key b-key))
577 ((and (stringp a-key) (stringp b-key))
578 (string< a-key b-key)))))))))
579 (when (memq (display-visual-class) '(gray-scale pseudo-color direct-color))
580 ;; Don't show more than what the display can handle.
581 (let ((lc (nthcdr (1- (display-color-cells)) list)))
582 (if lc
583 (setcdr lc nil)))))
584 (let ((buf (get-buffer-create "*Colors*")))
585 (with-current-buffer buf
586 (erase-buffer)
587 (setq truncate-lines t)
588 ;; Display buffer before generating content to allow
589 ;; `list-colors-print' to get the right window-width.
590 (pop-to-buffer buf)
591 (list-colors-print list callback)
592 (set-buffer-modified-p nil)))
593 (if callback
594 (message "Click on a color to select it.")))
596 (defun list-colors-print (list &optional callback)
597 (let ((callback-fn
598 (if callback
599 `(lambda (button)
600 (funcall ,callback (button-get button 'color-name))))))
601 (dolist (color list)
602 (if (consp color)
603 (if (cdr color)
604 (setq color (sort color (lambda (a b)
605 (string< (downcase a)
606 (downcase b))))))
607 (setq color (list color)))
608 (let* ((opoint (point))
609 (color-values (color-values (car color)))
610 (light-p (>= (apply 'max color-values)
611 (* (car (color-values "white")) .5)))
612 (max-len (max (- (window-width) 33) 20)))
613 (insert (car color))
614 (indent-to 22)
615 (put-text-property opoint (point) 'face `(:background ,(car color)))
616 (put-text-property
617 (prog1 (point)
618 (insert " ")
619 (if (cdr color)
620 ;; Insert as many color names as possible, fitting max-len.
621 (let ((names (list (car color)))
622 (others (cdr color))
623 (len (length (car color)))
624 newlen)
625 (while (and others
626 (< (setq newlen (+ len 2 (length (car others))))
627 max-len))
628 (setq len newlen)
629 (push (pop others) names))
630 (insert (mapconcat 'identity (nreverse names) ", ")))
631 (insert (car color))))
632 (point)
633 'face (list :foreground (car color)))
634 (indent-to (max (- (window-width) 8) 44))
635 (insert (propertize
636 (apply 'format "#%02x%02x%02x"
637 (mapcar (lambda (c) (lsh c -8))
638 color-values))
639 'mouse-face 'highlight
640 'help-echo
641 (let ((hsv (apply 'color-rgb-to-hsv
642 (color-values (car color)))))
643 (format "H:%d S:%d V:%d"
644 (nth 0 hsv) (nth 1 hsv) (nth 2 hsv)))))
645 (when callback
646 (make-text-button
647 opoint (point)
648 'follow-link t
649 'mouse-face (list :background (car color)
650 :foreground (if light-p "black" "white"))
651 'color-name (car color)
652 'action callback-fn)))
653 (insert "\n"))
654 (goto-char (point-min))))
657 (defun list-colors-duplicates (&optional list)
658 "Return a list of colors with grouped duplicate colors.
659 If a color has no duplicates, then the element of the returned list
660 has the form '(COLOR-NAME). The element of the returned list with
661 duplicate colors has the form '(COLOR-NAME DUPLICATE-COLOR-NAME ...).
662 This function uses the predicate `facemenu-color-equal' to compare
663 color names. If the optional argument LIST is non-nil, it should
664 be a list of colors to display. Otherwise, this function uses
665 a list of colors that the current display can handle."
666 (let* ((list (mapcar 'list (or list (defined-colors))))
667 (l list))
668 (while (cdr l)
669 (if (and (facemenu-color-equal (car (car l)) (car (car (cdr l))))
670 (not (if (fboundp 'w32-default-color-map)
671 (not (assoc (car (car l)) (w32-default-color-map))))))
672 (progn
673 (setcdr (car l) (cons (car (car (cdr l))) (cdr (car l))))
674 (setcdr l (cdr (cdr l))))
675 (setq l (cdr l))))
676 list))
678 (defun facemenu-color-equal (a b)
679 "Return t if colors A and B are the same color.
680 A and B should be strings naming colors.
681 This function queries the display system to find out what the color
682 names mean. It returns nil if the colors differ or if it can't
683 determine the correct answer."
684 (cond ((equal a b) t)
685 ((equal (color-values a) (color-values b)))))
688 (defvar facemenu-self-insert-data nil)
690 (defun facemenu-post-self-insert-function ()
691 (when (and (car facemenu-self-insert-data)
692 (eq last-command (cdr facemenu-self-insert-data)))
693 (put-text-property (1- (point)) (point)
694 'face (car facemenu-self-insert-data))
695 (setq facemenu-self-insert-data nil))
696 (remove-hook 'post-self-insert-hook 'facemenu-post-self-insert-function))
698 (defun facemenu-set-self-insert-face (face)
699 "Arrange for the next self-inserted char to have face `face'."
700 (setq facemenu-self-insert-data (cons face this-command))
701 (add-hook 'post-self-insert-hook 'facemenu-post-self-insert-function))
703 (defun facemenu-add-face (face &optional start end)
704 "Add FACE to text between START and END.
705 If START is nil or START to END is empty, add FACE to next typed character
706 instead. For each section of that region that has a different face property,
707 FACE will be consed onto it, and other faces that are completely hidden by
708 that will be removed from the list.
709 If `facemenu-add-face-function' and maybe `facemenu-end-add-face' are non-nil,
710 they are used to set the face information.
712 As a special case, if FACE is `default', then the region is left with NO face
713 text property. Otherwise, selecting the default face would not have any
714 effect. See `facemenu-remove-face-function'."
715 (interactive "*xFace: \nr")
716 (cond
717 ((and (eq face 'default)
718 (not (eq facemenu-remove-face-function t)))
719 (if facemenu-remove-face-function
720 (funcall facemenu-remove-face-function start end)
721 (if (and start (< start end))
722 (remove-text-properties start end '(face default))
723 (facemenu-set-self-insert-face 'default))))
724 (facemenu-add-face-function
725 (save-excursion
726 (if end (goto-char end))
727 (save-excursion
728 (if start (goto-char start))
729 (insert-before-markers
730 (funcall facemenu-add-face-function face end)))
731 (if facemenu-end-add-face
732 (insert (if (stringp facemenu-end-add-face)
733 facemenu-end-add-face
734 (funcall facemenu-end-add-face face))))))
735 ((and start (< start end))
736 (let ((part-start start) part-end)
737 (while (not (= part-start end))
738 (setq part-end (next-single-property-change part-start 'face
739 nil end))
740 (let ((prev (get-text-property part-start 'face)))
741 (put-text-property part-start part-end 'face
742 (if (null prev)
743 face
744 (facemenu-active-faces
745 (cons face
746 (if (listp prev)
747 prev
748 (list prev)))
749 ;; Specify the selected frame
750 ;; because nil would mean to use
751 ;; the new-frame default settings,
752 ;; and those are usually nil.
753 (selected-frame)))))
754 (setq part-start part-end))))
756 (facemenu-set-self-insert-face
757 (if (eq last-command (cdr facemenu-self-insert-data))
758 (cons face (if (listp (car facemenu-self-insert-data))
759 (car facemenu-self-insert-data)
760 (list (car facemenu-self-insert-data))))
761 face))))
762 (unless (facemenu-enable-faces-p)
763 (message "Font-lock mode will override any faces you set in this buffer")))
765 (defun facemenu-active-faces (face-list &optional frame)
766 "Return from FACE-LIST those faces that would be used for display.
767 This means each face attribute is not specified in a face earlier in FACE-LIST
768 and such a face is therefore active when used to display text.
769 If the optional argument FRAME is given, use the faces in that frame; otherwise
770 use the selected frame. If t, then the global, non-frame faces are used."
771 (let* ((mask-atts (copy-sequence
772 (if (consp (car face-list))
773 (face-attributes-as-vector (car face-list))
774 (or (internal-lisp-face-p (car face-list) frame)
775 (check-face (car face-list))))))
776 (active-list (list (car face-list)))
777 (face-list (cdr face-list))
778 (mask-len (length mask-atts)))
779 (while face-list
780 (if (let ((face-atts
781 (if (consp (car face-list))
782 (face-attributes-as-vector (car face-list))
783 (or (internal-lisp-face-p (car face-list) frame)
784 (check-face (car face-list)))))
785 (i mask-len)
786 (useful nil))
787 (while (>= (setq i (1- i)) 0)
788 (and (not (memq (aref face-atts i) '(nil unspecified)))
789 (memq (aref mask-atts i) '(nil unspecified))
790 (aset mask-atts i (setq useful t))))
791 useful)
792 (setq active-list (cons (car face-list) active-list)))
793 (setq face-list (cdr face-list)))
794 (nreverse active-list)))
796 (defun facemenu-add-new-face (face)
797 "Add FACE (a face) to the Face menu if `facemenu-listed-faces' says so.
798 This is called whenever you create a new face, and at other times."
799 (let* (name
800 symbol
801 menu docstring
802 (key (cdr (assoc face facemenu-keybindings)))
803 function menu-val)
804 (if (symbolp face)
805 (setq name (symbol-name face)
806 symbol face)
807 (setq name face
808 symbol (intern name)))
809 (setq menu 'facemenu-face-menu)
810 (setq docstring
811 (purecopy (format "Select face `%s' for subsequent insertion.
812 If the mark is active and there is no prefix argument,
813 apply face `%s' to the region instead.
814 This command was defined by `facemenu-add-new-face'."
815 name name)))
816 (cond ((facemenu-iterate ; check if equivalent face is already in the menu
817 (lambda (m) (and (listp m)
818 (symbolp (car m))
819 ;; Avoid error in face-equal
820 ;; when a non-face is erroneously present.
821 (facep (car m))
822 (face-equal (car m) symbol)))
823 (cdr (symbol-function menu))))
824 ;; Faces with a keyboard equivalent. These go at the front.
825 (key
826 (setq function (intern (concat "facemenu-set-" name)))
827 (fset function
828 `(lambda ()
829 ,docstring
830 (interactive)
831 (facemenu-set-face
832 (quote ,symbol)
833 (if (and mark-active (not current-prefix-arg))
834 (region-beginning))
835 (if (and mark-active (not current-prefix-arg))
836 (region-end)))))
837 (define-key 'facemenu-keymap key (cons name function))
838 (define-key menu key (cons name function)))
839 ;; Faces with no keyboard equivalent. Figure out where to put it:
840 ((or (eq t facemenu-listed-faces)
841 (memq symbol facemenu-listed-faces))
842 (setq key (vector symbol)
843 function 'facemenu-set-face-from-menu
844 menu-val (symbol-function menu))
845 (if (and facemenu-new-faces-at-end
846 (> (length menu-val) 3))
847 (define-key-after menu-val key (cons name function)
848 (car (nth (- (length menu-val) 3) menu-val)))
849 (define-key menu key (cons name function))))))
850 nil) ; Return nil for facemenu-iterate
852 (defun facemenu-add-new-color (color menu)
853 "Add COLOR (a color name string) to the appropriate Face menu.
854 MENU should be `facemenu-foreground-menu' or `facemenu-background-menu'.
855 Return the event type (a symbol) of the added menu entry.
857 This is called whenever you use a new color."
858 (let (symbol docstring)
859 (unless (color-defined-p color)
860 (error "Color `%s' undefined" color))
861 (cond ((eq menu 'facemenu-foreground-menu)
862 (setq docstring
863 (format "Select foreground color %s for subsequent insertion."
864 color)
865 symbol (intern (concat "fg:" color))))
866 ((eq menu 'facemenu-background-menu)
867 (setq docstring
868 (format "Select background color %s for subsequent insertion."
869 color)
870 symbol (intern (concat "bg:" color))))
871 (t (error "MENU should be `facemenu-foreground-menu' or `facemenu-background-menu'")))
872 (unless (facemenu-iterate ; Check if color is already in the menu.
873 (lambda (m) (and (listp m)
874 (eq (car m) symbol)))
875 (cdr (symbol-function menu)))
876 ;; Color is not in the menu. Figure out where to put it.
877 (let ((key (vector symbol))
878 (function 'facemenu-set-face-from-menu)
879 (menu-val (symbol-function menu)))
880 (if (and facemenu-new-faces-at-end
881 (> (length menu-val) 3))
882 (define-key-after menu-val key (cons color function)
883 (car (nth (- (length menu-val) 3) menu-val)))
884 (define-key menu key (cons color function)))))
885 symbol))
887 (defun facemenu-complete-face-list (&optional oldlist)
888 "Return list of all faces that look different.
889 Starts with given ALIST of faces, and adds elements only if they display
890 differently from any face already on the list.
891 The faces on ALIST will end up at the end of the returned list, in reverse
892 order."
893 (let ((list (nreverse (mapcar 'car oldlist))))
894 (facemenu-iterate
895 (lambda (new-face)
896 (if (not (memq new-face list))
897 (setq list (cons new-face list)))
898 nil)
899 (nreverse (face-list)))
900 list))
902 (defun facemenu-iterate (func list)
903 "Apply FUNC to each element of LIST until one returns non-nil.
904 Returns the non-nil value it found, or nil if all were nil."
905 (while (and list (not (funcall func (car list))))
906 (setq list (cdr list)))
907 (car list))
909 (facemenu-update)
911 (provide 'facemenu)
913 ;; arch-tag: 85f6d02b-9085-420e-b651-0678f0e9c7eb
914 ;;; facemenu.el ends here