1 ;;; facemenu.el -- Create a face menu for interactively adding fonts to text
2 ;; Copyright (c) 1994 Free Software Foundation, Inc.
4 ;; Author: Boris Goldowsky <boris@cs.rochester.edu>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to
21 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24 ;; This file defines a menu of faces (bold, italic, etc) which allows you to
25 ;; set the face used for a region of the buffer. Some faces also have
26 ;; keybindings, which are shown in the menu. Faces with names beginning with
27 ;; "fg:" or "bg:", as in "fg:red", are treated specially. It is assumed that
28 ;; Such faces are assumed to consist only of a foreground (if "fg:") or
29 ;; background (if "bg:") color. They are thus put into the color submenus
30 ;; rather than the general Face submenu. Such faces can also be created on
31 ;; demand from the "Other..." menu items.
34 ;; Put this file somewhere on emacs's load-path, and put
35 ;; (require 'facemenu)
36 ;; in your .emacs file.
39 ;; Selecting a face from the menu or typing the keyboard equivalent will
40 ;; change the region to use that face. If you use transient-mark-mode and the
41 ;; region is not active, the face will be remembered and used for the next
42 ;; insertion. It will be forgotten if you move point or make other
43 ;; modifications before inserting or typing anything.
45 ;; Faces can be selected from the keyboard as well.
46 ;; The standard keybindings are M-s (or ESC s) + letter:
47 ;; M-s i = "set italic", M-s b = "set bold", etc.
50 ;; An alternative set of keybindings that may be easier to type can be set up
51 ;; using "Hyper" keys. This requires that you set up a hyper-key on your
52 ;; keyboard. On my system, putting the following command in my .xinitrc:
53 ;; xmodmap -e "keysym Alt_L = Hyper_L" -e "add Mod2 = Hyper_L"
54 ;; makes the key labelled "Alt" act as a hyper key, but check with local
55 ;; X-perts for how to do it on your system. If you do this, then put the
56 ;; following in your .emacs before the (require 'facemenu):
57 ;; (setq facemenu-keybindings
58 ;; '((default . [?\H-d])
61 ;; (bold-italic . [?\H-o])
62 ;; (underline . [?\H-u])))
63 ;; (setq facemenu-keymap global-map)
64 ;; (setq facemenu-key nil)
66 ;; In general, the order of the faces that appear in the menu and their
67 ;; keybindings can be controlled by setting the variable
68 ;; `facemenu-keybindings'. Faces that you never want to add to your
69 ;; document (e.g., `region') are listed in `facemenu-unlisted-faces'.
72 ;; There is at present no way to display what the faces look like in
75 ;; `list-faces-display' shows the faces in a different order than
76 ;; this menu, which could be confusing. I do /not/ sort the list
77 ;; alphabetically, because I like the default order: it puts the most
78 ;; basic, common fonts first.
80 ;; Please send me any other problems, comments or ideas.
86 (defvar facemenu-key
"\M-s"
87 "Prefix to use for facemenu commands.")
89 (defvar facemenu-keybindings
93 (bold-italic .
"o") ; O for "Oblique" or "bOld"...
95 "Alist of interesting faces and keybindings.
96 Each element is itself a list: the car is the name of the face,
97 the next element is the key to use as a keyboard equivalent of the menu item;
98 the binding is made in facemenu-keymap.
100 The faces specifically mentioned in this list are put at the top of
101 the menu, in the order specified. All other faces which are defined,
102 except for those in `facemenu-unlisted-faces', are listed after them,
103 but get no keyboard equivalents.
105 If you change this variable after loading facemenu.el, you will need to call
106 `facemenu-update' to make it take effect.")
108 (defvar facemenu-unlisted-faces
109 '(modeline region secondary-selection highlight scratch-face
)
110 "Faces that are not included in the Face menu.
111 Set this before loading facemenu.el, or call `facemenu-update' after
114 (defvar facemenu-face-menu
115 (let ((map (make-sparse-keymap "Face")))
116 (define-key map
[other] (cons "Other..." 'facemenu-set-face))
118 "Menu keymap for faces.")
120 (defvar facemenu-foreground-menu
121 (let ((map (make-sparse-keymap "Foreground Color")))
122 (define-key map "o" (cons "Other" 'facemenu-set-foreground))
124 "Menu keymap for foreground colors.")
126 (defvar facemenu-background-menu
127 (let ((map (make-sparse-keymap "Background Color")))
128 (define-key map "o" (cons "Other" 'facemenu-set-background))
130 "Menu keymap for background colors")
132 (defvar facemenu-special-menu
133 (let ((map (make-sparse-keymap "Special")))
134 (define-key map [read-only] (cons "Read-Only" 'facemenu-set-read-only))
135 (define-key map [invisible] (cons "Invisible" 'facemenu-set-invisible))
137 "Menu keymap for non-face text-properties.")
139 (defvar facemenu-menu
140 (let ((map (make-sparse-keymap "Face")))
141 (define-key map [display] (cons "Display Faces" 'list-faces-display))
142 (define-key map [remove] (cons "Remove Props" 'facemenu-remove-all))
143 (define-key map [sep1] (list "-----------------"))
144 (define-key map [special] (cons "Special Props" facemenu-special-menu))
145 (define-key map [bg] (cons "Background Color" facemenu-background-menu))
146 (define-key map [fg] (cons "Foreground Color" facemenu-foreground-menu))
147 (define-key map [face] (cons "Face" facemenu-face-menu))
149 "Facemenu top-level menu keymap")
151 (defvar facemenu-keymap (make-sparse-keymap "Set face")
152 "Map for keyboard face-changing commands.
153 `Facemenu-update' fills in the keymap according to the bindings
154 requested in facemenu-keybindings.")
156 ;;; Internal Variables
158 (defvar facemenu-color-alist nil
159 ;; Don't initialize here; that doesn't work if preloaded.
160 "Alist of colors, used for completion.
161 If null, `facemenu-read-color' will set it.")
163 (defvar facemenu-next nil) ; set when we are going to set a face on next char.
164 (defvar facemenu-loc nil)
166 (defun facemenu-update ()
167 "Add or update the \"Face\" menu in the menu bar.
168 You can call this to update things if you change any of the menu configuration
173 (define-key global-map [C-down-mouse-3] facemenu-menu)
174 (if facemenu-key (define-key global-map facemenu-key facemenu-keymap))
176 ;; Add each defined face to the menu.
177 (facemenu-iterate 'facemenu-add-new-face
178 (facemenu-complete-face-list facemenu-keybindings)))
181 (defun facemenu-set-face (face &optional start end)
182 "Add FACE to the region or next character typed.
183 It will be added to the top of the face list; any faces lower on the list that
184 will not show through at all will be removed.
186 Interactively, the face to be used is prompted for.
187 If the region is active, it will be set to the requested face. If
188 it is inactive \(even if mark-even-if-inactive is set) the next
189 character that is typed \(via `self-insert-command') will be set to
190 the the selected face. Moving point or switching buffers before
191 typing a character cancels the request."
192 (interactive (list (read-face-name "Use face: ")))
194 (let ((start (or start (region-beginning)))
195 (end (or end (region-end))))
196 (facemenu-add-face face start end))
197 (setq facemenu-next face
198 facemenu-loc (point))))
201 (defun facemenu-set-foreground (color &optional start end)
202 "Set the foreground color of the region or next character typed.
203 The color is prompted for. A face named `fg:color' is used \(or created).
204 If the region is active, it will be set to the requested face. If
205 it is inactive \(even if mark-even-if-inactive is set) the next
206 character that is typed \(via `self-insert-command') will be set to
207 the the selected face. Moving point or switching buffers before
208 typing a character cancels the request."
209 (interactive (list (facemenu-read-color "Foreground color: ")))
210 (let ((face (intern (concat "fg:" color))))
211 (or (facemenu-get-face face)
212 (error "Unknown color: %s" color))
213 (facemenu-set-face face start end)))
216 (defun facemenu-set-background (color &optional start end)
217 "Set the background color of the region or next character typed.
218 The color is prompted for. A face named `bg:color' is used \(or created).
219 If the region is active, it will be set to the requested face. If
220 it is inactive \(even if mark-even-if-inactive is set) the next
221 character that is typed \(via `self-insert-command') will be set to
222 the the selected face. Moving point or switching buffers before
223 typing a character cancels the request."
224 (interactive (list (facemenu-read-color "Background color: ")))
225 (let ((face (intern (concat "bg:" color))))
226 (or (facemenu-get-face face)
227 (error "Unknown color: %s" color))
228 (facemenu-set-face face start end)))
230 (defun facemenu-set-face-from-menu (face start end)
231 "Set the face of the region or next character typed.
232 This function is designed to be called from a menu; the face to use
233 is the menu item's name.
234 If the region is active, it will be set to the requested face. If
235 it is inactive \(even if mark-even-if-inactive is set) the next
236 character that is typed \(via `self-insert-command') will be set to
237 the the selected face. Moving point or switching buffers before
238 typing a character cancels the request."
239 (interactive (list last-command-event
240 (if mark-active (region-beginning))
241 (if mark-active (region-end))))
242 (facemenu-get-face face)
244 (facemenu-add-face face start end)
245 (setq facemenu-next face facemenu-loc (point))))
247 (defun facemenu-set-invisible (start end)
248 "Make the region invisible.
249 This sets the `invisible' text property; it can be undone with
250 `facemenu-remove-all'."
252 (put-text-property start end 'invisible t))
254 (defun facemenu-set-intangible (start end)
255 "Make the region intangible: disallow moving into it.
256 This sets the `intangible' text property; it can be undone with
257 `facemenu-remove-all'."
259 (put-text-property start end 'intangible t))
261 (defun facemenu-set-read-only (start end)
262 "Make the region unmodifiable.
263 This sets the `read-only' text property; it can be undone with
264 `facemenu-remove-all'."
266 (put-text-property start end 'read-only t))
268 (defun facemenu-remove-all (start end)
269 "Remove all text properties that facemenu added to region."
270 (interactive "*r") ; error if buffer is read-only despite the next line.
271 (let ((inhibit-read-only t))
272 (remove-text-properties
273 start end '(face nil invisible nil intangible nil
274 read-only nil category nil))))
277 (defun facemenu-read-color (prompt)
278 "Read a color using the minibuffer."
279 (let ((col (completing-read (or "Color: ")
280 (or facemenu-color-alist
281 (if (eq 'x window-system)
282 (mapcar 'list (x-defined-colors))))
288 (defun facemenu-add-face (face start end)
289 "Add FACE to text between START and END.
290 For each section of that region that has a different face property, FACE will
291 be consed onto it, and other faces that are completely hidden by that will be
292 removed from the list.
294 As a special case, if FACE is `default', then the region is left with NO face
295 text property. Otherwise, selecting the default face would not have any
297 (interactive "*xFace:\nr")
298 (if (eq face 'default)
299 (remove-text-properties start end '(face default))
300 (let ((part-start start) part-end)
301 (while (not (= part-start end))
302 (setq part-end (next-single-property-change part-start 'face nil end))
303 (let ((prev (get-text-property part-start 'face)))
304 (put-text-property part-start part-end 'face
307 (facemenu-discard-redundant-faces
309 (if (listp prev) prev (list prev)))))))
310 (setq part-start part-end)))))
312 (defun facemenu-discard-redundant-faces (face-list &optional mask)
313 "Remove from FACE-LIST any faces that won't show at all.
314 This means they have no non-nil elements that aren't also non-nil in an
317 (cond ((null face-list) nil)
319 (cons (car face-list)
320 (facemenu-discard-redundant-faces
322 (copy-sequence (internal-get-face (car face-list))))))
323 ((let ((i (length mask))
324 (face (internal-get-face (car face-list))))
325 (while (>= (setq i (1- i)) 0)
326 (if (and (aref face i)
328 (progn (setq useful t)
331 (cons (car face-list)
332 (facemenu-discard-redundant-faces (cdr face-list) mask)))
333 (t (facemenu-discard-redundant-faces (cdr face-list) mask)))))
335 (defun facemenu-get-face (symbol)
336 "Make sure FACE exists.
337 If not, it is created. If it is created and is of the form `fg:color', then
338 set the foreground to that color. If of the form `bg:color', set the
339 background. In any case, add it to the appropriate menu. Returns nil if
341 (or (internal-find-face symbol)
342 (let* ((face (make-face symbol))
343 (name (symbol-name symbol))
344 (color (substring name 3)))
345 (cond ((string-match "^fg:" name)
346 (set-face-foreground face color)
347 (and (eq 'x window-system) (x-color-defined-p color)))
348 ((string-match "^bg:" name)
349 (set-face-background face color)
350 (and (eq 'x window-system) (x-color-defined-p color)))
353 (defun facemenu-add-new-face (face)
354 "Add a FACE to the appropriate Face menu.
355 Automatically called when a new face is created."
356 (let* ((name (symbol-name face))
357 (menu (cond ((string-match "^fg:" name)
358 (setq name (substring name 3))
359 facemenu-foreground-menu)
360 ((string-match "^bg:" name)
361 (setq name (substring name 3))
362 facemenu-background-menu)
363 (t facemenu-face-menu)))
365 (cond ((memq face facemenu-unlisted-faces)
367 ((setq key (cdr (assoc face facemenu-keybindings)))
368 (let ((function (intern (concat "facemenu-set-" name))))
370 (` (lambda () (interactive)
371 (facemenu-set-face (quote (, face))))))
372 (define-key facemenu-keymap key (cons name function))
373 (define-key menu key (cons name function))))
374 (t (define-key menu (vector face)
375 (cons name 'facemenu-set-face-from-menu)))))
376 ;; Return nil for facemenu-iterate's benefit:
379 (defun facemenu-after-change (begin end old-length)
380 "May set the face of just-inserted text to user's request.
381 This only happens if the change is an insertion, and
382 `facemenu-set-face[-from-menu]' was called with point at the
383 beginning of the insertion."
384 (if (null facemenu-next) ; exit immediately if no work
386 (if (and (= 0 old-length) ; insertion
387 (= facemenu-loc begin)) ; point wasn't moved in between
388 (facemenu-add-face facemenu-next begin end))
389 (setq facemenu-next nil)))
391 (defun facemenu-complete-face-list (&optional oldlist)
392 "Return list of all faces that are look different.
393 Starts with given ALIST of faces, and adds elements only if they display
394 differently from any face already on the list.
395 The faces on ALIST will end up at the end of the returned list, in reverse
397 (let ((list (nreverse (mapcar 'car oldlist))))
400 (if (not (memq new-face list))
401 (setq list (cons new-face list)))
403 (nreverse (face-list)))
406 (defun facemenu-iterate (func iterate-list)
407 "Apply FUNC to each element of LIST until one returns non-nil.
408 Returns the non-nil value it found, or nil if all were nil."
409 (while (and iterate-list (not (funcall func (car iterate-list))))
410 (setq iterate-list (cdr iterate-list)))
414 (add-hook 'after-change-functions 'facemenu-after-change)
416 ;;; facemenu.el ends here