1 ;;; facemenu.el --- create a face menu for interactively adding fonts to text
3 ;; Copyright (c) 1994, 1995, 1996 Free Software Foundation, Inc.
5 ;; Author: Boris Goldowsky <boris@gnu.ai.mit.edu>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
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. Faces with names beginning with
30 ;; "fg:" or "bg:", as in "fg:red", are treated specially.
31 ;; Such faces are assumed to consist only of a foreground (if "fg:") or
32 ;; background (if "bg:") color. They are thus put into the color submenus
33 ;; rather than the general Face submenu. These faces can also be
34 ;; automatically created by selecting the "Other..." menu items in the
35 ;; "Foreground" and "Background" submenus.
37 ;; The menu also contains submenus for indentation and justification-changing
41 ;; Selecting a face from the menu or typing the keyboard equivalent will
42 ;; change the region to use that face. If you use transient-mark-mode and the
43 ;; region is not active, the face will be remembered and used for the next
44 ;; insertion. It will be forgotten if you move point or make other
45 ;; modifications before inserting or typing anything.
47 ;; Faces can be selected from the keyboard as well.
48 ;; The standard keybindings are M-g (or ESC g) + letter:
49 ;; M-g i = "set italic", M-g b = "set bold", etc.
52 ;; An alternative set of keybindings that may be easier to type can be set up
53 ;; using "Alt" or "Hyper" keys. This requires that you either have or create
54 ;; an Alt or Hyper key on your keyboard. On my keyboard, there is a key
55 ;; labeled "Alt", but to make it act as an Alt key I have to put this command
57 ;; xmodmap -e "add Mod3 = Alt_L"
58 ;; Or, I can make it into a Hyper key with this:
59 ;; xmodmap -e "keysym Alt_L = Hyper_L" -e "add Mod2 = Hyper_L"
60 ;; Check with local X-perts for how to do it on your system.
61 ;; Then you can define your keybindings with code like this in your .emacs:
62 ;; (setq facemenu-keybindings
63 ;; '((default . [?\H-d])
66 ;; (bold-italic . [?\H-l])
67 ;; (underline . [?\H-u])))
68 ;; (setq facemenu-keymap global-map)
69 ;; (setq facemenu-key nil)
70 ;; (define-key global-map [?\H-c] 'facemenu-set-foreground) ; set fg color
71 ;; (define-key global-map [?\H-C] 'facemenu-set-background) ; set bg color
72 ;; (require 'facemenu)
74 ;; The order of the faces that appear in the menu and their keybindings can be
75 ;; controlled by setting the variables `facemenu-keybindings' and
76 ;; `facemenu-new-faces-at-end'. List faces that you don't use in documents
77 ;; (eg, `region') in `facemenu-unlisted-faces'.
80 ;; Bold and Italic do not combine to create bold-italic if you select them
81 ;; both, although most other combinations (eg bold + underline + some color)
82 ;; do the intuitive thing.
84 ;; There is at present no way to display what the faces look like in
87 ;; `list-faces-display' shows the faces in a different order than
88 ;; this menu, which could be confusing. I do /not/ sort the list
89 ;; alphabetically, because I like the default order: it puts the most
90 ;; basic, common fonts first.
92 ;; Please send me any other problems, comments or ideas.
98 ;;; Provide some binding for startup:
99 ;;;###autoload (define-key global-map "\M-g" 'facemenu-keymap)
100 ;;;###autoload (autoload 'facemenu-keymap "facemenu" "Keymap for face-changing commands." t 'keymap)
102 (defvar facemenu-key
"\M-g"
103 "Prefix key to use for facemenu commands.")
105 (defvar facemenu-keybindings
109 (bold-italic .
"l") ; {bold} intersect {italic} = {l}
111 "Alist of interesting faces and keybindings.
112 Each element is itself a list: the car is the name of the face,
113 the next element is the key to use as a keyboard equivalent of the menu item;
114 the binding is made in facemenu-keymap.
116 The faces specifically mentioned in this list are put at the top of
117 the menu, in the order specified. All other faces which are defined,
118 except for those in `facemenu-unlisted-faces', are listed after them,
119 but get no keyboard equivalents.
121 If you change this variable after loading facemenu.el, you will need to call
122 `facemenu-update' to make it take effect.")
124 (defvar 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.")
129 (defvar facemenu-unlisted-faces
130 '(modeline region secondary-selection highlight scratch-face
)
131 "List of faces not to include in the Face menu.
132 You can set this list before loading facemenu.el, or add a face to it before
133 creating that face if you do not want it to be listed. If you change the
134 variable so as to eliminate faces that have already been added to the menu,
135 call `facemenu-update' to recalculate the menu contents.
137 If this variable is t, no faces will be added to the menu. This is useful for
138 temporarily turning off the feature that automatically adds faces to the menu
139 when they are created.")
142 (defvar facemenu-face-menu
143 (let ((map (make-sparse-keymap "Face")))
144 (define-key map
"o" (cons "Other..." 'facemenu-set-face
))
146 "Menu keymap for faces.")
148 (defalias 'facemenu-face-menu facemenu-face-menu
)
151 (defvar facemenu-foreground-menu
152 (let ((map (make-sparse-keymap "Foreground Color")))
153 (define-key map
"o" (cons "Other..." 'facemenu-set-foreground
))
155 "Menu keymap for foreground colors.")
157 (defalias 'facemenu-foreground-menu facemenu-foreground-menu
)
160 (defvar facemenu-background-menu
161 (let ((map (make-sparse-keymap "Background Color")))
162 (define-key map
"o" (cons "Other..." 'facemenu-set-background
))
164 "Menu keymap for background colors")
166 (defalias 'facemenu-background-menu facemenu-background-menu
)
169 (defvar facemenu-special-menu
170 (let ((map (make-sparse-keymap "Special")))
171 (define-key map
[?s
] (cons "Remove Special" 'facemenu-remove-special
))
172 (define-key map
[?t
] (cons "Intangible" 'facemenu-set-intangible
))
173 (define-key map
[?v
] (cons "Invisible" 'facemenu-set-invisible
))
174 (define-key map
[?r
] (cons "Read-Only" 'facemenu-set-read-only
))
176 "Menu keymap for non-face text-properties.")
178 (defalias 'facemenu-special-menu facemenu-special-menu
)
181 (defvar facemenu-justification-menu
182 (let ((map (make-sparse-keymap "Justification")))
183 (define-key map
[?c
] (cons "Center" 'set-justification-center
))
184 (define-key map
[?b
] (cons "Full" 'set-justification-full
))
185 (define-key map
[?r
] (cons "Right" 'set-justification-right
))
186 (define-key map
[?l
] (cons "Left" 'set-justification-left
))
187 (define-key map
[?u
] (cons "Unfilled" 'set-justification-none
))
189 "Submenu for text justification commands.")
191 (defalias 'facemenu-justification-menu facemenu-justification-menu
)
194 (defvar facemenu-indentation-menu
195 (let ((map (make-sparse-keymap "Indentation")))
196 (define-key map
[decrease-right-margin
]
197 (cons "Indent Right Less" 'decrease-right-margin
))
198 (define-key map
[increase-right-margin
]
199 (cons "Indent Right More" 'increase-right-margin
))
200 (define-key map
[decrease-left-margin
]
201 (cons "Indent Less" 'decrease-left-margin
))
202 (define-key map
[increase-left-margin
]
203 (cons "Indent More" 'increase-left-margin
))
205 "Submenu for indentation commands.")
207 (defalias 'facemenu-indentation-menu facemenu-indentation-menu
)
209 ;; This is split up to avoid an overlong line in loaddefs.el.
211 (defvar facemenu-menu nil
212 "Facemenu top-level menu keymap.")
214 (setq facemenu-menu
(make-sparse-keymap "Text Properties"))
216 (let ((map facemenu-menu
))
217 (define-key map
[dc] (cons "Display Colors" 'list-colors-display))
218 (define-key map [df] (cons "Display Faces" 'list-faces-display))
219 (define-key map [dp] (cons "List Properties" 'list-text-properties-at))
220 (define-key map [ra] (cons "Remove All" 'facemenu-remove-all))
221 (define-key map [rm] (cons "Remove Properties" 'facemenu-remove-props))
222 (define-key map [s1] (list "-----------------")))
224 (let ((map facemenu-menu))
225 (define-key map [in] (cons "Indentation" 'facemenu-indentation-menu))
226 (define-key map [ju] (cons "Justification" 'facemenu-justification-menu))
227 (define-key map [s2] (list "-----------------"))
228 (define-key map [sp] (cons "Special Properties" 'facemenu-special-menu))
229 (define-key map [bg] (cons "Background Color" 'facemenu-background-menu))
230 (define-key map [fg] (cons "Foreground Color" 'facemenu-foreground-menu))
231 (define-key map [fc] (cons "Face" 'facemenu-face-menu)))
233 (defalias 'facemenu-menu facemenu-menu)
235 (defvar facemenu-keymap
236 (let ((map (make-sparse-keymap "Set face")))
237 (define-key map "o" (cons "Other..." 'facemenu-set-face))
239 "Keymap for face-changing commands.
240 `Facemenu-update' fills in the keymap according to the bindings
241 requested in `facemenu-keybindings'.")
242 (defalias 'facemenu-keymap facemenu-keymap)
245 (defvar facemenu-add-face-function nil
246 "Function called at beginning of text to change or `nil'.
247 This function is passed the FACE to set and END of text to change, and must
248 return a string which is inserted. It may set `facemenu-end-add-face'.")
250 (defvar facemenu-end-add-face nil
251 "String to insert or function called at end of text to change or `nil'.
252 This function is passed the FACE to set, and must return a string which is
255 (defvar facemenu-remove-face-function nil
256 "When non-`nil' function called to remove faces.
257 This function is passed the START and END of text to change.
258 May also be `t' meaning to use `facemenu-add-face-function'.")
260 ;;; Internal Variables
262 (defvar facemenu-color-alist nil
263 ;; Don't initialize here; that doesn't work if preloaded.
264 "Alist of colors, used for completion.
265 If null, `facemenu-read-color' will set it.")
267 (defun facemenu-update ()
268 "Add or update the \"Face\" menu in the menu bar.
269 You can call this to update things if you change any of the menu configuration
274 (define-key global-map [C-down-mouse-2] 'facemenu-menu)
275 (if facemenu-key (define-key global-map facemenu-key 'facemenu-keymap))
277 ;; Add each defined face to the menu.
278 (facemenu-iterate 'facemenu-add-new-face
279 (facemenu-complete-face-list facemenu-keybindings)))
282 (defun facemenu-set-face (face &optional start end)
283 "Add FACE to the region or next character typed.
284 It will be added to the top of the face list; any faces lower on the list that
285 will not show through at all will be removed.
287 Interactively, the face to be used is read with the minibuffer.
289 If the region is active and there is no prefix argument,
290 this command sets the region to the requested face.
292 Otherwise, this command specifies the face for the next character
293 inserted. Moving point or switching buffers before
294 typing a character to insert cancels the specification."
295 (interactive (list (read-face-name "Use face: ")))
296 (barf-if-buffer-read-only)
297 (facemenu-add-new-face face)
298 (if (and mark-active (not current-prefix-arg))
299 (let ((start (or start (region-beginning)))
300 (end (or end (region-end))))
301 (facemenu-add-face face start end))
302 (facemenu-add-face face)))
305 (defun facemenu-set-foreground (color &optional start end)
306 "Set the foreground color of the region or next character typed.
307 The color is prompted for. A face named `fg:color' is used \(or created).
308 If the region is active, it will be set to the requested face. If
309 it is inactive \(even if mark-even-if-inactive is set) the next
310 character that is typed \(via `self-insert-command') will be set to
311 the selected face. Moving point or switching buffers before
312 typing a character cancels the request."
313 (interactive (list (facemenu-read-color "Foreground color: ")))
314 (let ((face (intern (concat "fg:" color))))
315 (or (facemenu-get-face face)
316 (error "Unknown color: %s" color))
317 (facemenu-set-face face start end)))
320 (defun facemenu-set-background (color &optional start end)
321 "Set the background color of the region or next character typed.
322 The color is prompted for. A face named `bg:color' is used \(or created).
323 If the region is active, it will be set to the requested face. If
324 it is inactive \(even if mark-even-if-inactive is set) the next
325 character that is typed \(via `self-insert-command') will be set to
326 the selected face. Moving point or switching buffers before
327 typing a character cancels the request."
328 (interactive (list (facemenu-read-color "Background color: ")))
329 (let ((face (intern (concat "bg:" color))))
330 (or (facemenu-get-face face)
331 (error "Unknown color: %s" color))
332 (facemenu-set-face face start end)))
335 (defun facemenu-set-face-from-menu (face start end)
336 "Set the face of the region or next character typed.
337 This function is designed to be called from a menu; the face to use
338 is the menu item's name.
340 If the region is active and there is no prefix argument,
341 this command sets the region to the requested face.
343 Otherwise, this command specifies the face for the next character
344 inserted. Moving point or switching buffers before
345 typing a character to insert cancels the specification."
346 (interactive (list last-command-event
347 (if (and mark-active (not current-prefix-arg))
349 (if (and mark-active (not current-prefix-arg))
351 (barf-if-buffer-read-only)
352 (facemenu-get-face face)
354 (facemenu-add-face face start end)
355 (facemenu-add-face face)))
358 (defun facemenu-set-invisible (start end)
359 "Make the region invisible.
360 This sets the `invisible' text property; it can be undone with
361 `facemenu-remove-special'."
363 (put-text-property start end 'invisible t))
366 (defun facemenu-set-intangible (start end)
367 "Make the region intangible: disallow moving into it.
368 This sets the `intangible' text property; it can be undone with
369 `facemenu-remove-special'."
371 (put-text-property start end 'intangible t))
374 (defun facemenu-set-read-only (start end)
375 "Make the region unmodifiable.
376 This sets the `read-only' text property; it can be undone with
377 `facemenu-remove-special'."
379 (put-text-property start end 'read-only t))
382 (defun facemenu-remove-props (start end)
383 "Remove all text properties that facemenu added to region."
384 (interactive "*r") ; error if buffer is read-only despite the next line.
385 (let ((inhibit-read-only t))
386 (remove-text-properties
387 start end '(face nil invisible nil intangible nil
388 read-only nil category nil))))
391 (defun facemenu-remove-all (start end)
392 "Remove all text properties from the region."
393 (interactive "*r") ; error if buffer is read-only despite the next line.
394 (let ((inhibit-read-only t))
395 (set-text-properties start end nil)))
398 (defun facemenu-remove-special (start end)
399 "Remove all the \"special\" text properties from the region.
400 These special properties include `invisible', `intangible' and `read-only'."
401 (interactive "*r") ; error if buffer is read-only despite the next line.
402 (let ((inhibit-read-only t))
403 (remove-text-properties
404 start end '(invisible nil intangible nil read-only nil))))
407 (defun list-text-properties-at (p)
408 "Pop up a buffer listing text-properties at LOCATION."
410 (let ((props (text-properties-at p))
415 (if (and (not (cdr (cdr props)))
416 (not (eq (car props) 'category))
417 (< (length (setq str (format "Text property at %d: %s %S"
418 p (car props) (car (cdr props)))))
421 (with-output-to-temp-buffer "*Text Properties*"
422 (princ (format "Text properties at %d:\n\n" p))
424 (if (eq (car props) 'category)
425 (setq category (car (cdr props))))
426 (princ (format "%-20s %S\n"
427 (car props) (car (cdr props))))
428 (setq props (cdr (cdr props))))
431 (setq props (symbol-plist category))
432 (princ (format "\nCategory %s:\n\n" category))
434 (princ (format "%-20s %S\n"
435 (car props) (car (cdr props))))
436 (if (eq (car props) 'category)
437 (setq category (car (cdr props))))
438 (setq props (cdr (cdr props)))))))))))
441 (defun facemenu-read-color (&optional prompt)
442 "Read a color using the minibuffer."
443 (let ((col (completing-read (or prompt "Color: ")
444 (or facemenu-color-alist
446 (mapcar 'list (x-defined-colors))))
453 (defun list-colors-display (&optional list)
454 "Display names of defined colors, and show what they look like.
455 If the optional argument LIST is non-nil, it should be a list of
456 colors to display. Otherwise, this command computes a list
457 of colors that the current display can handle."
459 (if (and (null list) window-system)
461 (setq list (x-defined-colors))
462 ;; Delete duplicate colors.
465 (if (facemenu-color-equal (car l) (car (cdr l)))
466 (setcdr l (cdr (cdr l)))
467 (setq l (cdr l)))))))
468 (with-output-to-temp-buffer "*Colors*"
470 (set-buffer standard-output)
471 (let ((facemenu-unlisted-faces t)
477 (put-text-property s (point) 'face
479 (intern (concat "bg:" (car list)))))
481 (insert " " (car list) "\n")
482 (put-text-property s (point) 'face
484 (intern (concat "fg:" (car list)))))
485 (setq list (cdr list)))))))
487 (defun facemenu-color-equal (a b)
488 "Return t if colors A and B are the same color.
489 A and B should be strings naming colors.
490 This function queries the window-system server to find out what the
491 color names mean. It returns nil if the colors differ or if it can't
492 determine the correct answer."
493 (cond ((equal a b) t)
494 ((and (or (eq window-system 'x) (eq window-system 'win32))
495 (equal (x-color-values a) (x-color-values b))))
496 ((eq window-system 'pc)
497 (and (x-color-defined-p a) (x-color-defined-p b)
498 (eq (msdos-color-translate a) (msdos-color-translate b))))))
500 (defun facemenu-add-face (face &optional start end)
501 "Add FACE to text between START and END.
502 If START is `nil' or START to END is empty, add FACE to next typed character
503 instead. For each section of that region that has a different face property,
504 FACE will be consed onto it, and other faces that are completely hidden by
505 that will be removed from the list.
506 If `facemenu-add-face-function' and maybe `facemenu-end-add-face' are non-`nil'
507 they are used to set the face information.
509 As a special case, if FACE is `default', then the region is left with NO face
510 text property. Otherwise, selecting the default face would not have any
511 effect. See `facemenu-remove-face-function'."
512 (interactive "*xFace: \nr")
513 (if (and (eq face 'default)
514 (not (eq facemenu-remove-face-function t)))
515 (if facemenu-remove-face-function
516 (funcall facemenu-remove-face-function start end)
517 (if (and start (< start end))
518 (remove-text-properties start end '(face default))
519 (setq self-insert-face 'default
520 self-insert-face-command this-command)))
521 (if facemenu-add-face-function
523 (if end (goto-char end))
525 (if start (goto-char start))
526 (insert-before-markers
527 (funcall facemenu-add-face-function face end)))
528 (if facemenu-end-add-face
529 (insert (if (stringp facemenu-end-add-face)
530 facemenu-end-add-face
531 (funcall facemenu-end-add-face face)))))
532 (if (and start (< start end))
533 (let ((part-start start) part-end)
534 (while (not (= part-start end))
535 (setq part-end (next-single-property-change part-start 'face
537 (let ((prev (get-text-property part-start 'face)))
538 (put-text-property part-start part-end 'face
541 (facemenu-active-faces
546 (setq part-start part-end)))
547 (setq self-insert-face (if (eq last-command self-insert-face-command)
548 (cons face (if (listp self-insert-face)
550 (list self-insert-face)))
552 self-insert-face-command this-command)))))
554 (defun facemenu-active-faces (face-list &optional frame)
555 "Return from FACE-LIST those faces that would be used for display.
556 This means each face attribute is not specified in a face earlier in FACE-LIST
557 and such a face is therefore active when used to display text.
558 If the optional argument FRAME is given, use the faces in that frame; otherwise
559 use the selected frame. If t, then the global, non-frame faces are used."
560 (let* ((mask-atts (copy-sequence (internal-get-face (car face-list) frame)))
561 (active-list (list (car face-list)))
562 (face-list (cdr face-list))
563 (mask-len (length mask-atts)))
565 (if (let ((face-atts (internal-get-face (car face-list) frame))
566 (i mask-len) (useful nil))
567 (while (> (setq i (1- i)) 1)
568 (and (aref face-atts i) (not (aref mask-atts i))
569 (aset mask-atts i (setq useful t))))
571 (setq active-list (cons (car face-list) active-list)))
572 (setq face-list (cdr face-list)))
573 (nreverse active-list)))
575 (defun facemenu-get-face (symbol)
576 "Make sure FACE exists.
577 If not, it is created. If it is created and is of the form `fg:color', then
578 set the foreground to that color. If of the form `bg:color', set the
579 background. In any case, add it to the appropriate menu. Returns the face,
580 or nil if given a bad color."
581 (if (or (internal-find-face symbol)
582 (let* ((face (make-face symbol))
583 (name (symbol-name symbol))
584 (color (substring name 3)))
585 (cond ((string-match "^fg:" name)
586 (set-face-foreground face color)
588 (x-color-defined-p color)))
589 ((string-match "^bg:" name)
590 (set-face-background face color)
592 (x-color-defined-p color)))
596 (defun facemenu-add-new-face (face)
597 "Add a FACE to the appropriate Face menu.
598 Automatically called when a new face is created."
599 (let* ((name (symbol-name face))
600 (menu (cond ((string-match "^fg:" name)
601 (setq name (substring name 3))
602 'facemenu-foreground-menu)
603 ((string-match "^bg:" name)
604 (setq name (substring name 3))
605 'facemenu-background-menu)
606 (t 'facemenu-face-menu)))
607 (key (cdr (assoc face facemenu-keybindings)))
609 (cond ((eq t facemenu-unlisted-faces))
610 ((memq face facemenu-unlisted-faces))
611 (key ; has a keyboard equivalent. These go at the front.
612 (setq function (intern (concat "facemenu-set-" name)))
614 (` (lambda () (interactive)
615 (facemenu-set-face (quote (, face))))))
616 (define-key 'facemenu-keymap key (cons name function))
617 (define-key menu key (cons name function)))
618 ((facemenu-iterate ; check if equivalent face is already in the menu
619 (lambda (m) (and (listp m)
621 (face-equal (car m) face)))
622 (cdr (symbol-function menu))))
623 (t ; No keyboard equivalent. Figure out where to put it:
624 (setq key (vector face)
625 function 'facemenu-set-face-from-menu
626 menu-val (symbol-function menu))
627 (if (and facemenu-new-faces-at-end
628 (> (length menu-val) 3))
629 (define-key-after menu-val key (cons name function)
630 (car (nth (- (length menu-val) 3) menu-val)))
631 (define-key menu key (cons name function))))))
632 nil) ; Return nil for facemenu-iterate
634 (defun facemenu-complete-face-list (&optional oldlist)
635 "Return list of all faces that look different.
636 Starts with given ALIST of faces, and adds elements only if they display
637 differently from any face already on the list.
638 The faces on ALIST will end up at the end of the returned list, in reverse
640 (let ((list (nreverse (mapcar 'car oldlist))))
643 (if (not (memq new-face list))
644 (setq list (cons new-face list)))
646 (nreverse (face-list)))
649 (defun facemenu-iterate (func iterate-list)
650 "Apply FUNC to each element of LIST until one returns non-nil.
651 Returns the non-nil value it found, or nil if all were nil."
652 (while (and iterate-list (not (funcall func (car iterate-list))))
653 (setq iterate-list (cdr iterate-list)))
658 ;;; facemenu.el ends here