Fix previous change.
[bpt/emacs.git] / lisp / facemenu.el
CommitLineData
be010748 1;;; facemenu.el --- create a face menu for interactively adding fonts to text
b578f267 2
682e437e 3;; Copyright (c) 1994, 1995, 1996 Free Software Foundation, Inc.
4e8aa578 4
7865eac6 5;; Author: Boris Goldowsky <boris@gnu.ai.mit.edu>
4e8aa578
RS
6;; Keywords: faces
7
8;; This file is part of GNU Emacs.
9
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)
13;; any later version.
14
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.
19
20;; You should have received a copy of the GNU General Public License
b578f267
EN
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.
4e8aa578
RS
24
25;;; Commentary:
b578f267 26
bf7d4561
BG
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
88d690a9 30;; "fg:" or "bg:", as in "fg:red", are treated specially.
bf7d4561
BG
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
88d690a9
RS
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.
36;;
37;; The menu also contains submenus for indentation and justification-changing
38;; commands.
4e8aa578 39
4e8aa578 40;;; Usage:
bf7d4561
BG
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.
4e8aa578
RS
46;;
47;; Faces can be selected from the keyboard as well.
88d690a9
RS
48;; The standard keybindings are M-g (or ESC g) + letter:
49;; M-g i = "set italic", M-g b = "set bold", etc.
4e8aa578
RS
50
51;;; Customization:
52;; An alternative set of keybindings that may be easier to type can be set up
88d690a9
RS
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
56;; into my .xinitrc:
57;; xmodmap -e "add Mod3 = Alt_L"
58;; Or, I can make it into a Hyper key with this:
4e8aa578 59;; xmodmap -e "keysym Alt_L = Hyper_L" -e "add Mod2 = Hyper_L"
88d690a9
RS
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:
4e8aa578
RS
62;; (setq facemenu-keybindings
63;; '((default . [?\H-d])
64;; (bold . [?\H-b])
65;; (italic . [?\H-i])
88d690a9 66;; (bold-italic . [?\H-l])
4e8aa578
RS
67;; (underline . [?\H-u])))
68;; (setq facemenu-keymap global-map)
69;; (setq facemenu-key nil)
88d690a9
RS
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)
4e8aa578 73;;
88d690a9
RS
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'.
4e8aa578
RS
78
79;;; Known Problems:
88d690a9
RS
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.
83;;
4e8aa578
RS
84;; There is at present no way to display what the faces look like in
85;; the menu itself.
86;;
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.
91;;
92;; Please send me any other problems, comments or ideas.
93
94;;; Code:
95
96(provide 'facemenu)
97
9dc90430
BG
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)
101
d2eafd88 102(defvar facemenu-key "\M-g"
9dc90430 103 "Prefix key to use for facemenu commands.")
4e8aa578 104
4e8aa578
RS
105(defvar facemenu-keybindings
106 '((default . "d")
107 (bold . "b")
108 (italic . "i")
88d690a9 109 (bold-italic . "l") ; {bold} intersect {italic} = {l}
4e8aa578
RS
110 (underline . "u"))
111 "Alist of interesting faces and keybindings.
112Each element is itself a list: the car is the name of the face,
113the next element is the key to use as a keyboard equivalent of the menu item;
114the binding is made in facemenu-keymap.
115
116The faces specifically mentioned in this list are put at the top of
117the menu, in the order specified. All other faces which are defined,
118except for those in `facemenu-unlisted-faces', are listed after them,
119but get no keyboard equivalents.
120
121If you change this variable after loading facemenu.el, you will need to call
122`facemenu-update' to make it take effect.")
123
88d690a9
RS
124(defvar facemenu-new-faces-at-end t
125 "Where in the menu to insert newly-created faces.
126This should be nil to put them at the top of the menu, or t to put them
127just before \"Other\" at the end.")
128
4e8aa578 129(defvar facemenu-unlisted-faces
5a79ed26 130 '(modeline region secondary-selection highlight scratch-face)
88d690a9 131 "List of faces not to include in the Face menu.
5a79ed26
KH
132You can set this list before loading facemenu.el, or add a face to it before
133creating that face if you do not want it to be listed. If you change the
134variable so as to eliminate faces that have already been added to the menu,
135call `facemenu-update' to recalculate the menu contents.
4e8aa578 136
88d690a9
RS
137If this variable is t, no faces will be added to the menu. This is useful for
138temporarily turning off the feature that automatically adds faces to the menu
139when they are created.")
140
9dc90430 141;;;###autoload
88d690a9 142(defvar facemenu-face-menu
bf7d4561 143 (let ((map (make-sparse-keymap "Face")))
88d690a9 144 (define-key map "o" (cons "Other..." 'facemenu-set-face))
bf7d4561
BG
145 map)
146 "Menu keymap for faces.")
9dc90430 147;;;###autoload
88d690a9 148(defalias 'facemenu-face-menu facemenu-face-menu)
bf7d4561 149
9dc90430 150;;;###autoload
bf7d4561
BG
151(defvar facemenu-foreground-menu
152 (let ((map (make-sparse-keymap "Foreground Color")))
f34eaa2c 153 (define-key map "o" (cons "Other..." 'facemenu-set-foreground))
bf7d4561
BG
154 map)
155 "Menu keymap for foreground colors.")
9dc90430 156;;;###autoload
88d690a9 157(defalias 'facemenu-foreground-menu facemenu-foreground-menu)
bf7d4561 158
9dc90430 159;;;###autoload
bf7d4561
BG
160(defvar facemenu-background-menu
161 (let ((map (make-sparse-keymap "Background Color")))
f34eaa2c 162 (define-key map "o" (cons "Other..." 'facemenu-set-background))
bf7d4561
BG
163 map)
164 "Menu keymap for background colors")
9dc90430 165;;;###autoload
88d690a9 166(defalias 'facemenu-background-menu facemenu-background-menu)
bf7d4561 167
9dc90430 168;;;###autoload
bf7d4561
BG
169(defvar facemenu-special-menu
170 (let ((map (make-sparse-keymap "Special")))
f34eaa2c
KH
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))
bf7d4561
BG
175 map)
176 "Menu keymap for non-face text-properties.")
9dc90430 177;;;###autoload
88d690a9
RS
178(defalias 'facemenu-special-menu facemenu-special-menu)
179
9dc90430 180;;;###autoload
88d690a9
RS
181(defvar facemenu-justification-menu
182 (let ((map (make-sparse-keymap "Justification")))
9dc90430
BG
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))
88d690a9
RS
188 map)
189 "Submenu for text justification commands.")
9dc90430 190;;;###autoload
88d690a9
RS
191(defalias 'facemenu-justification-menu facemenu-justification-menu)
192
9dc90430 193;;;###autoload
88d690a9
RS
194(defvar facemenu-indentation-menu
195 (let ((map (make-sparse-keymap "Indentation")))
f34eaa2c
KH
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))
88d690a9
RS
204 map)
205 "Submenu for indentation commands.")
9dc90430 206;;;###autoload
88d690a9 207(defalias 'facemenu-indentation-menu facemenu-indentation-menu)
bf7d4561 208
f34eaa2c 209;; This is split up to avoid an overlong line in loaddefs.el.
9dc90430 210;;;###autoload
f34eaa2c 211(defvar facemenu-menu nil
535d2617 212 "Facemenu top-level menu keymap.")
9dc90430 213;;;###autoload
f34eaa2c
KH
214(setq facemenu-menu (make-sparse-keymap "Text Properties"))
215;;;###autoload
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 "-----------------")))
223;;;###autoload
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 "-----------------"))
29008daa 228 (define-key map [sp] (cons "Special Properties" 'facemenu-special-menu))
f34eaa2c
KH
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)))
232;;;###autoload
88d690a9 233(defalias 'facemenu-menu facemenu-menu)
bf7d4561 234
88d690a9
RS
235(defvar facemenu-keymap
236 (let ((map (make-sparse-keymap "Set face")))
f34eaa2c 237 (define-key map "o" (cons "Other..." 'facemenu-set-face))
88d690a9 238 map)
9dc90430 239 "Keymap for face-changing commands.
bf7d4561 240`Facemenu-update' fills in the keymap according to the bindings
535d2617 241requested in `facemenu-keybindings'.")
88d690a9 242(defalias 'facemenu-keymap facemenu-keymap)
bf7d4561 243
cb5bec6e
RS
244
245(defvar facemenu-add-face-function nil
246 "Function called at beginning of text to change or `nil'.
247This function is passed the FACE to set and END of text to change, and must
248return a string which is inserted. It may set `facemenu-end-add-face'.")
249
250(defvar facemenu-end-add-face nil
251 "String to insert or function called at end of text to change or `nil'.
252This function is passed the FACE to set, and must return a string which is
253inserted.")
254
255(defvar facemenu-remove-face-function nil
256 "When non-`nil' function called to remove faces.
257This function is passed the START and END of text to change.
258May also be `t' meaning to use `facemenu-add-face-function'.")
259
bf7d4561
BG
260;;; Internal Variables
261
262(defvar facemenu-color-alist nil
263 ;; Don't initialize here; that doesn't work if preloaded.
264 "Alist of colors, used for completion.
265If null, `facemenu-read-color' will set it.")
4a24b314 266
4e8aa578 267(defun facemenu-update ()
bf7d4561
BG
268 "Add or update the \"Face\" menu in the menu bar.
269You can call this to update things if you change any of the menu configuration
270variables."
4e8aa578
RS
271 (interactive)
272
bf7d4561 273 ;; Global bindings:
88d690a9
RS
274 (define-key global-map [C-down-mouse-2] 'facemenu-menu)
275 (if facemenu-key (define-key global-map facemenu-key 'facemenu-keymap))
4e8aa578 276
bf7d4561
BG
277 ;; Add each defined face to the menu.
278 (facemenu-iterate 'facemenu-add-new-face
279 (facemenu-complete-face-list facemenu-keybindings)))
4a24b314 280
4e8aa578
RS
281;;;###autoload
282(defun facemenu-set-face (face &optional start end)
4a24b314
RS
283 "Add FACE to the region or next character typed.
284It will be added to the top of the face list; any faces lower on the list that
285will not show through at all will be removed.
286
f34eaa2c
KH
287Interactively, the face to be used is read with the minibuffer.
288
289If the region is active and there is no prefix argument,
290this command sets the region to the requested face.
291
292Otherwise, this command specifies the face for the next character
293inserted. Moving point or switching buffers before
294typing a character to insert cancels the specification."
4e8aa578 295 (interactive (list (read-face-name "Use face: ")))
88d690a9
RS
296 (barf-if-buffer-read-only)
297 (facemenu-add-new-face face)
f34eaa2c 298 (if (and mark-active (not current-prefix-arg))
4a24b314
RS
299 (let ((start (or start (region-beginning)))
300 (end (or end (region-end))))
301 (facemenu-add-face face start end))
cb5bec6e 302 (facemenu-add-face face)))
4a24b314 303
bf7d4561 304;;;###autoload
4a24b314
RS
305(defun facemenu-set-foreground (color &optional start end)
306 "Set the foreground color of the region or next character typed.
307The color is prompted for. A face named `fg:color' is used \(or created).
308If the region is active, it will be set to the requested face. If
309it is inactive \(even if mark-even-if-inactive is set) the next
310character that is typed \(via `self-insert-command') will be set to
f61deddc 311the selected face. Moving point or switching buffers before
4a24b314
RS
312typing 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)))
318
bf7d4561 319;;;###autoload
4a24b314
RS
320(defun facemenu-set-background (color &optional start end)
321 "Set the background color of the region or next character typed.
322The color is prompted for. A face named `bg:color' is used \(or created).
323If the region is active, it will be set to the requested face. If
324it is inactive \(even if mark-even-if-inactive is set) the next
325character that is typed \(via `self-insert-command') will be set to
f61deddc 326the selected face. Moving point or switching buffers before
4a24b314
RS
327typing 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)))
4e8aa578 333
9dc90430 334;;;###autoload
4e8aa578
RS
335(defun facemenu-set-face-from-menu (face start end)
336 "Set the face of the region or next character typed.
337This function is designed to be called from a menu; the face to use
338is the menu item's name.
f34eaa2c
KH
339
340If the region is active and there is no prefix argument,
341this command sets the region to the requested face.
342
343Otherwise, this command specifies the face for the next character
344inserted. Moving point or switching buffers before
345typing a character to insert cancels the specification."
4a24b314 346 (interactive (list last-command-event
f34eaa2c
KH
347 (if (and mark-active (not current-prefix-arg))
348 (region-beginning))
349 (if (and mark-active (not current-prefix-arg))
350 (region-end))))
88d690a9 351 (barf-if-buffer-read-only)
4a24b314 352 (facemenu-get-face face)
4e8aa578 353 (if start
4a24b314 354 (facemenu-add-face face start end)
cb5bec6e 355 (facemenu-add-face face)))
4e8aa578 356
9dc90430 357;;;###autoload
4e8aa578
RS
358(defun facemenu-set-invisible (start end)
359 "Make the region invisible.
360This sets the `invisible' text property; it can be undone with
f34eaa2c 361`facemenu-remove-special'."
4e8aa578
RS
362 (interactive "r")
363 (put-text-property start end 'invisible t))
364
9dc90430 365;;;###autoload
4e8aa578
RS
366(defun facemenu-set-intangible (start end)
367 "Make the region intangible: disallow moving into it.
368This sets the `intangible' text property; it can be undone with
f34eaa2c 369`facemenu-remove-special'."
4e8aa578
RS
370 (interactive "r")
371 (put-text-property start end 'intangible t))
372
9dc90430 373;;;###autoload
4e8aa578
RS
374(defun facemenu-set-read-only (start end)
375 "Make the region unmodifiable.
376This sets the `read-only' text property; it can be undone with
f34eaa2c 377`facemenu-remove-special'."
4e8aa578
RS
378 (interactive "r")
379 (put-text-property start end 'read-only t))
380
9dc90430 381;;;###autoload
f34eaa2c 382(defun facemenu-remove-props (start end)
4e8aa578
RS
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))))
389
f34eaa2c
KH
390;;;###autoload
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)))
396
397;;;###autoload
398(defun facemenu-remove-special (start end)
399 "Remove all the \"special\" text properties from the region.
400These 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))))
405
c0a7db84
BG
406;;;###autoload
407(defun list-text-properties-at (p)
408 "Pop up a buffer listing text-properties at LOCATION."
409 (interactive "d")
cb5bec6e 410 (let ((props (text-properties-at p))
25a4509f 411 category
cb5bec6e 412 str)
c0a7db84
BG
413 (if (null props)
414 (message "None")
cb5bec6e 415 (if (and (not (cdr (cdr props)))
25a4509f 416 (not (eq (car props) 'category))
cb5bec6e
RS
417 (< (length (setq str (format "Text property at %d: %s %S"
418 p (car props) (car (cdr props)))))
419 (frame-width)))
f2b7756c 420 (message "%s" str)
cb5bec6e
RS
421 (with-output-to-temp-buffer "*Text Properties*"
422 (princ (format "Text properties at %d:\n\n" p))
423 (while props
25a4509f
RS
424 (if (eq (car props) 'category)
425 (setq category (car (cdr props))))
cb5bec6e
RS
426 (princ (format "%-20s %S\n"
427 (car props) (car (cdr props))))
25a4509f
RS
428 (setq props (cdr (cdr props))))
429 (if category
430 (progn
431 (setq props (symbol-plist category))
432 (princ (format "\nCategory %s:\n\n" category))
433 (while props
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)))))))))))
c0a7db84 439
bf7d4561 440;;;###autoload
da627a71 441(defun facemenu-read-color (&optional prompt)
bf7d4561 442 "Read a color using the minibuffer."
da627a71 443 (let ((col (completing-read (or prompt "Color: ")
bf7d4561 444 (or facemenu-color-alist
cb5bec6e 445 (if window-system
bf7d4561
BG
446 (mapcar 'list (x-defined-colors))))
447 nil t)))
448 (if (equal "" col)
449 nil
450 col)))
4e8aa578 451
88d690a9
RS
452;;;###autoload
453(defun list-colors-display (&optional list)
7c49006b
RS
454 "Display names of defined colors, and show what they look like.
455If the optional argument LIST is non-nil, it should be a list of
456colors to display. Otherwise, this command computes a list
457of colors that the current display can handle."
88d690a9 458 (interactive)
cb5bec6e 459 (if (and (null list) window-system)
7c49006b
RS
460 (progn
461 (setq list (x-defined-colors))
462 ;; Delete duplicate colors.
463 (let ((l list))
464 (while (cdr l)
465 (if (facemenu-color-equal (car l) (car (cdr l)))
466 (setcdr l (cdr (cdr l)))
467 (setq l (cdr l)))))))
88d690a9
RS
468 (with-output-to-temp-buffer "*Colors*"
469 (save-excursion
470 (set-buffer standard-output)
471 (let ((facemenu-unlisted-faces t)
472 s)
473 (while list
474 (setq s (point))
475 (insert (car list))
476 (indent-to 20)
477 (put-text-property s (point) 'face
478 (facemenu-get-face
479 (intern (concat "bg:" (car list)))))
480 (setq s (point))
481 (insert " " (car list) "\n")
482 (put-text-property s (point) 'face
483 (facemenu-get-face
484 (intern (concat "fg:" (car list)))))
485 (setq list (cdr list)))))))
486
487(defun facemenu-color-equal (a b)
488 "Return t if colors A and B are the same color.
7c49006b
RS
489A and B should be strings naming colors.
490This function queries the window-system server to find out what the
491color names mean. It returns nil if the colors differ or if it can't
492determine the correct answer."
88d690a9 493 (cond ((equal a b) t)
b86b9918 494 ((and (memq window-system '(x w32))
cb5bec6e
RS
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))))))
88d690a9 499
cb5bec6e 500(defun facemenu-add-face (face &optional start end)
4a24b314 501 "Add FACE to text between START and END.
cb5bec6e
RS
502If START is `nil' or START to END is empty, add FACE to next typed character
503instead. For each section of that region that has a different face property,
504FACE will be consed onto it, and other faces that are completely hidden by
505that will be removed from the list.
506If `facemenu-add-face-function' and maybe `facemenu-end-add-face' are non-`nil'
507they are used to set the face information.
bf7d4561
BG
508
509As a special case, if FACE is `default', then the region is left with NO face
510text property. Otherwise, selecting the default face would not have any
cb5bec6e
RS
511effect. 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)
682e437e
RS
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)))
cb5bec6e
RS
521 (if facemenu-add-face-function
522 (save-excursion
523 (if end (goto-char end))
524 (save-excursion
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
536 nil end))
537 (let ((prev (get-text-property part-start 'face)))
538 (put-text-property part-start part-end 'face
539 (if (null prev)
540 face
541 (facemenu-active-faces
542 (cons face
543 (if (listp prev)
544 prev
545 (list prev)))))))
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)
549 self-insert-face
550 (list self-insert-face)))
551 face)
552 self-insert-face-command this-command)))))
4a24b314 553
5a79ed26
KH
554(defun facemenu-active-faces (face-list &optional frame)
555 "Return from FACE-LIST those faces that would be used for display.
556This means each face attribute is not specified in a face earlier in FACE-LIST
557and such a face is therefore active when used to display text.
558If the optional argument FRAME is given, use the faces in that frame; otherwise
559use 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)))
564 (while face-list
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))))
570 useful)
571 (setq active-list (cons (car face-list) active-list)))
572 (setq face-list (cdr face-list)))
573 (nreverse active-list)))
4a24b314 574
bf7d4561
BG
575(defun facemenu-get-face (symbol)
576 "Make sure FACE exists.
577If not, it is created. If it is created and is of the form `fg:color', then
578set the foreground to that color. If of the form `bg:color', set the
88d690a9
RS
579background. In any case, add it to the appropriate menu. Returns the face,
580or 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)
cb5bec6e
RS
587 (and window-system
588 (x-color-defined-p color)))
88d690a9
RS
589 ((string-match "^bg:" name)
590 (set-face-background face color)
cb5bec6e
RS
591 (and window-system
592 (x-color-defined-p color)))
88d690a9
RS
593 (t))))
594 symbol))
bf7d4561
BG
595
596(defun facemenu-add-new-face (face)
597 "Add a FACE to the appropriate Face menu.
598Automatically 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))
88d690a9 602 'facemenu-foreground-menu)
bf7d4561
BG
603 ((string-match "^bg:" name)
604 (setq name (substring name 3))
88d690a9
RS
605 'facemenu-background-menu)
606 (t 'facemenu-face-menu)))
607 (key (cdr (assoc face facemenu-keybindings)))
608 function menu-val)
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)))
613 (fset function
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)
620 (symbolp (car 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
bf7d4561 633
bf7d4561 634(defun facemenu-complete-face-list (&optional oldlist)
7cd49450 635 "Return list of all faces that look different.
bf7d4561
BG
636Starts with given ALIST of faces, and adds elements only if they display
637differently from any face already on the list.
638The faces on ALIST will end up at the end of the returned list, in reverse
639order."
640 (let ((list (nreverse (mapcar 'car oldlist))))
641 (facemenu-iterate
642 (lambda (new-face)
643 (if (not (memq new-face list))
644 (setq list (cons new-face list)))
645 nil)
646 (nreverse (face-list)))
647 list))
648
4e8aa578
RS
649(defun facemenu-iterate (func iterate-list)
650 "Apply FUNC to each element of LIST until one returns non-nil.
651Returns 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)))
654 (car iterate-list))
655
656(facemenu-update)
4e8aa578
RS
657
658;;; facemenu.el ends here