Added gud-speedbar-buttons, and support for GDB buttons.
[emacs.git] / lisp / facemenu.el
blob3036e44358c62009a635c0cb3561352aad13d3ac
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>
6 ;; Keywords: faces
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)
13 ;; any later version.
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.
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. 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
38 ;; commands.
40 ;;; Usage:
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.
51 ;;; Customization:
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
56 ;; into my .xinitrc:
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])
64 ;; (bold . [?\H-b])
65 ;; (italic . [?\H-i])
66 ;; (bold-italic . [?\H-l])
67 ;; (underline . [?\H-u])))
68 ;; (facemenu-update)
69 ;; (setq facemenu-keymap global-map)
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
73 ;; The order of the faces that appear in the menu and their keybindings can be
74 ;; controlled by setting the variables `facemenu-keybindings' and
75 ;; `facemenu-new-faces-at-end'. List faces that you don't use in documents
76 ;; (eg, `region') in `facemenu-unlisted-faces'.
78 ;;; Known Problems:
79 ;; Bold and Italic do not combine to create bold-italic if you select them
80 ;; both, although most other combinations (eg bold + underline + some color)
81 ;; do the intuitive thing.
83 ;; There is at present no way to display what the faces look like in
84 ;; the menu itself.
86 ;; `list-faces-display' shows the faces in a different order than
87 ;; this menu, which could be confusing. I do /not/ sort the list
88 ;; alphabetically, because I like the default order: it puts the most
89 ;; basic, common fonts first.
91 ;; Please send me any other problems, comments or ideas.
93 ;;; Code:
95 (provide 'facemenu)
97 ;;; Provide some binding for startup:
98 ;;;###autoload (define-key global-map "\M-g" 'facemenu-keymap)
99 ;;;###autoload (autoload 'facemenu-keymap "facemenu" "Keymap for face-changing commands." t 'keymap)
101 ;; Global bindings:
102 (define-key global-map [C-down-mouse-2] 'facemenu-menu)
103 (define-key global-map "\M-g" 'facemenu-keymap)
105 (defgroup facemenu nil
106 "Create a face menu for interactively adding fonts to text"
107 :group 'faces
108 :prefix "facemenu-")
110 (defcustom facemenu-keybindings
111 '((default . "d")
112 (bold . "b")
113 (italic . "i")
114 (bold-italic . "l") ; {bold} intersect {italic} = {l}
115 (underline . "u"))
116 "Alist of interesting faces and keybindings.
117 Each element is itself a list: the car is the name of the face,
118 the next element is the key to use as a keyboard equivalent of the menu item;
119 the binding is made in `facemenu-keymap'.
121 The faces specifically mentioned in this list are put at the top of
122 the menu, in the order specified. All other faces which are defined,
123 except for those in `facemenu-unlisted-faces', are listed after them,
124 but get no keyboard equivalents.
126 If you change this variable after loading facemenu.el, you will need to call
127 `facemenu-update' to make it take effect."
128 :type '(repeat (cons face string))
129 :group 'facemenu)
131 (defcustom facemenu-new-faces-at-end t
132 "*Where in the menu to insert newly-created faces.
133 This should be nil to put them at the top of the menu, or t to put them
134 just before \"Other\" at the end."
135 :type 'boolean
136 :group 'facemenu)
138 (defcustom facemenu-unlisted-faces
139 '(modeline region secondary-selection highlight scratch-face
140 "^font-lock-" "^gnus-" "^message-" "^ediff-" "^term-" "^vc-"
141 "^widget-" "^custom-" "^vm-")
142 "*List of faces not to include in the Face menu.
143 Each element may be either a symbol, which is the name of a face, or a string,
144 which is a regular expression to be matched against face names. Matching
145 faces will not be added to the menu.
147 You can set this list before loading facemenu.el, or add a face to it before
148 creating that face if you do not want it to be listed. If you change the
149 variable so as to eliminate faces that have already been added to the menu,
150 call `facemenu-update' to recalculate the menu contents.
152 If this variable is t, no faces will be added to the menu. This is useful for
153 temporarily turning off the feature that automatically adds faces to the menu
154 when they are created."
155 :type '(choice (const :tag "Don't add" t)
156 (const :tag "None" nil)
157 (repeat (choice symbol regexp)))
158 :group 'facemenu)
160 ;;;###autoload
161 (defvar facemenu-face-menu
162 (let ((map (make-sparse-keymap "Face")))
163 (define-key map "o" (cons "Other..." 'facemenu-set-face))
164 map)
165 "Menu keymap for faces.")
166 ;;;###autoload
167 (defalias 'facemenu-face-menu facemenu-face-menu)
169 ;;;###autoload
170 (defvar facemenu-foreground-menu
171 (let ((map (make-sparse-keymap "Foreground Color")))
172 (define-key map "o" (cons "Other..." 'facemenu-set-foreground))
173 map)
174 "Menu keymap for foreground colors.")
175 ;;;###autoload
176 (defalias 'facemenu-foreground-menu facemenu-foreground-menu)
178 ;;;###autoload
179 (defvar facemenu-background-menu
180 (let ((map (make-sparse-keymap "Background Color")))
181 (define-key map "o" (cons "Other..." 'facemenu-set-background))
182 map)
183 "Menu keymap for background colors")
184 ;;;###autoload
185 (defalias 'facemenu-background-menu facemenu-background-menu)
187 ;;;###autoload
188 (defvar facemenu-special-menu
189 (let ((map (make-sparse-keymap "Special")))
190 (define-key map [?s] (cons "Remove Special" 'facemenu-remove-special))
191 (define-key map [?t] (cons "Intangible" 'facemenu-set-intangible))
192 (define-key map [?v] (cons "Invisible" 'facemenu-set-invisible))
193 (define-key map [?r] (cons "Read-Only" 'facemenu-set-read-only))
194 map)
195 "Menu keymap for non-face text-properties.")
196 ;;;###autoload
197 (defalias 'facemenu-special-menu facemenu-special-menu)
199 ;;;###autoload
200 (defvar facemenu-justification-menu
201 (let ((map (make-sparse-keymap "Justification")))
202 (define-key map [?c] (cons "Center" 'set-justification-center))
203 (define-key map [?b] (cons "Full" 'set-justification-full))
204 (define-key map [?r] (cons "Right" 'set-justification-right))
205 (define-key map [?l] (cons "Left" 'set-justification-left))
206 (define-key map [?u] (cons "Unfilled" 'set-justification-none))
207 map)
208 "Submenu for text justification commands.")
209 ;;;###autoload
210 (defalias 'facemenu-justification-menu facemenu-justification-menu)
212 ;;;###autoload
213 (defvar facemenu-indentation-menu
214 (let ((map (make-sparse-keymap "Indentation")))
215 (define-key map [decrease-right-margin]
216 (cons "Indent Right Less" 'decrease-right-margin))
217 (define-key map [increase-right-margin]
218 (cons "Indent Right More" 'increase-right-margin))
219 (define-key map [decrease-left-margin]
220 (cons "Indent Less" 'decrease-left-margin))
221 (define-key map [increase-left-margin]
222 (cons "Indent More" 'increase-left-margin))
223 map)
224 "Submenu for indentation commands.")
225 ;;;###autoload
226 (defalias 'facemenu-indentation-menu facemenu-indentation-menu)
228 ;; This is split up to avoid an overlong line in loaddefs.el.
229 ;;;###autoload
230 (defvar facemenu-menu nil
231 "Facemenu top-level menu keymap.")
232 ;;;###autoload
233 (setq facemenu-menu (make-sparse-keymap "Text Properties"))
234 ;;;###autoload
235 (let ((map facemenu-menu))
236 (define-key map [dc] (cons "Display Colors" 'list-colors-display))
237 (define-key map [df] (cons "Display Faces" 'list-faces-display))
238 (define-key map [dp] (cons "List Properties" 'list-text-properties-at))
239 (define-key map [ra] (cons "Remove Text Properties" 'facemenu-remove-all))
240 (define-key map [rm] (cons "Remove Face Properties" 'facemenu-remove-face-props))
241 (define-key map [s1] (list "-----------------")))
242 ;;;###autoload
243 (let ((map facemenu-menu))
244 (define-key map [in] (cons "Indentation" 'facemenu-indentation-menu))
245 (define-key map [ju] (cons "Justification" 'facemenu-justification-menu))
246 (define-key map [s2] (list "-----------------"))
247 (define-key map [sp] (cons "Special Properties" 'facemenu-special-menu))
248 (define-key map [bg] (cons "Background Color" 'facemenu-background-menu))
249 (define-key map [fg] (cons "Foreground Color" 'facemenu-foreground-menu))
250 (define-key map [fc] (cons "Face" 'facemenu-face-menu)))
251 ;;;###autoload
252 (defalias 'facemenu-menu facemenu-menu)
254 (defvar facemenu-keymap
255 (let ((map (make-sparse-keymap "Set face")))
256 (define-key map "o" (cons "Other..." 'facemenu-set-face))
257 map)
258 "Keymap for face-changing commands.
259 `Facemenu-update' fills in the keymap according to the bindings
260 requested in `facemenu-keybindings'.")
261 (defalias 'facemenu-keymap facemenu-keymap)
264 (defcustom facemenu-add-face-function nil
265 "Function called at beginning of text to change or `nil'.
266 This function is passed the FACE to set and END of text to change, and must
267 return a string which is inserted. It may set `facemenu-end-add-face'."
268 :type '(choice (const :tag "None" nil)
269 function)
270 :group 'facemenu)
272 (defcustom facemenu-end-add-face nil
273 "String to insert or function called at end of text to change or `nil'.
274 This function is passed the FACE to set, and must return a string which is
275 inserted."
276 :type '(choice (const :tag "None" nil)
277 string
278 function)
279 :group 'facemenu)
281 (defcustom facemenu-remove-face-function nil
282 "When non-nil, this is a function called to remove faces.
283 This function is passed the START and END of text to change.
284 May also be `t' meaning to use `facemenu-add-face-function'."
285 :type '(choice (const :tag "None" nil)
286 (const :tag "Use add-face" t)
287 function)
288 :group 'facemenu)
290 ;;; Internal Variables
292 (defvar facemenu-color-alist nil
293 ;; Don't initialize here; that doesn't work if preloaded.
294 "Alist of colors, used for completion.
295 If null, `facemenu-read-color' will set it.")
297 (defun facemenu-update ()
298 "Add or update the \"Face\" menu in the menu bar.
299 You can call this to update things if you change any of the menu configuration
300 variables."
301 (interactive)
303 ;; Add each defined face to the menu.
304 (facemenu-iterate 'facemenu-add-new-face
305 (facemenu-complete-face-list facemenu-keybindings)))
307 ;;;###autoload
308 (defun facemenu-set-face (face &optional start end)
309 "Add FACE to the region or next character typed.
310 It will be added to the top of the face list; any faces lower on the list that
311 will not show through at all will be removed.
313 Interactively, the face to be used is read with the minibuffer.
315 If the region is active and there is no prefix argument,
316 this command sets the region to the requested face.
318 Otherwise, this command specifies the face for the next character
319 inserted. Moving point or switching buffers before
320 typing a character to insert cancels the specification."
321 (interactive (list (read-face-name "Use face: ")))
322 (barf-if-buffer-read-only)
323 (facemenu-add-new-face face)
324 (if (and mark-active (not current-prefix-arg))
325 (let ((start (or start (region-beginning)))
326 (end (or end (region-end))))
327 (facemenu-add-face face start end))
328 (facemenu-add-face face)))
330 ;;;###autoload
331 (defun facemenu-set-foreground (color &optional start end)
332 "Set the foreground color of the region or next character typed.
333 The color is prompted for. A face named `fg:color' is used \(or created).
334 If the region is active, it will be set to the requested face. If
335 it is inactive \(even if mark-even-if-inactive is set) the next
336 character that is typed \(via `self-insert-command') will be set to
337 the selected face. Moving point or switching buffers before
338 typing a character cancels the request."
339 (interactive (list (facemenu-read-color "Foreground color: ")))
340 (let ((face (intern (concat "fg:" color))))
341 (or (facemenu-get-face face)
342 (error "Unknown color: %s" color))
343 (facemenu-set-face face start end)))
345 ;;;###autoload
346 (defun facemenu-set-background (color &optional start end)
347 "Set the background color of the region or next character typed.
348 The color is prompted for. A face named `bg:color' is used \(or created).
349 If the region is active, it will be set to the requested face. If
350 it is inactive \(even if mark-even-if-inactive is set) the next
351 character that is typed \(via `self-insert-command') will be set to
352 the selected face. Moving point or switching buffers before
353 typing a character cancels the request."
354 (interactive (list (facemenu-read-color "Background color: ")))
355 (let ((face (intern (concat "bg:" color))))
356 (or (facemenu-get-face face)
357 (error "Unknown color: %s" color))
358 (facemenu-set-face face start end)))
360 ;;;###autoload
361 (defun facemenu-set-face-from-menu (face start end)
362 "Set the face of the region or next character typed.
363 This function is designed to be called from a menu; the face to use
364 is the menu item's name.
366 If the region is active and there is no prefix argument,
367 this command sets the region to the requested face.
369 Otherwise, this command specifies the face for the next character
370 inserted. Moving point or switching buffers before
371 typing a character to insert cancels the specification."
372 (interactive (list last-command-event
373 (if (and mark-active (not current-prefix-arg))
374 (region-beginning))
375 (if (and mark-active (not current-prefix-arg))
376 (region-end))))
377 (barf-if-buffer-read-only)
378 (facemenu-get-face face)
379 (if start
380 (facemenu-add-face face start end)
381 (facemenu-add-face face)))
383 ;;;###autoload
384 (defun facemenu-set-invisible (start end)
385 "Make the region invisible.
386 This sets the `invisible' text property; it can be undone with
387 `facemenu-remove-special'."
388 (interactive "r")
389 (add-text-properties start end '(invisible t)))
391 ;;;###autoload
392 (defun facemenu-set-intangible (start end)
393 "Make the region intangible: disallow moving into it.
394 This sets the `intangible' text property; it can be undone with
395 `facemenu-remove-special'."
396 (interactive "r")
397 (add-text-properties start end '(intangible t)))
399 ;;;###autoload
400 (defun facemenu-set-read-only (start end)
401 "Make the region unmodifiable.
402 This sets the `read-only' text property; it can be undone with
403 `facemenu-remove-special'."
404 (interactive "r")
405 (add-text-properties start end '(read-only t)))
407 ;;;###autoload
408 (defun facemenu-remove-face-props (start end)
409 "Remove `face' and `mouse-face' text properties."
410 (interactive "*r") ; error if buffer is read-only despite the next line.
411 (let ((inhibit-read-only t))
412 (remove-text-properties
413 start end '(face nil mouse-face nil))))
415 ;;;###autoload
416 (defun facemenu-remove-all (start end)
417 "Remove all text properties from the region."
418 (interactive "*r") ; error if buffer is read-only despite the next line.
419 (let ((inhibit-read-only t))
420 (set-text-properties start end nil)))
422 ;;;###autoload
423 (defun facemenu-remove-special (start end)
424 "Remove all the \"special\" text properties from the region.
425 These special properties include `invisible', `intangible' and `read-only'."
426 (interactive "*r") ; error if buffer is read-only despite the next line.
427 (let ((inhibit-read-only t))
428 (remove-text-properties
429 start end '(invisible nil intangible nil read-only nil))))
431 ;;;###autoload
432 (defun list-text-properties-at (p)
433 "Pop up a buffer listing text-properties at LOCATION."
434 (interactive "d")
435 (let ((props (text-properties-at p))
436 category
437 str)
438 (if (null props)
439 (message "None")
440 (if (and (not (cdr (cdr props)))
441 (not (eq (car props) 'category))
442 (< (length (setq str (format "Text property at %d: %s %S"
443 p (car props) (car (cdr props)))))
444 (frame-width)))
445 (message "%s" str)
446 (with-output-to-temp-buffer "*Text Properties*"
447 (princ (format "Text properties at %d:\n\n" p))
448 (while props
449 (if (eq (car props) 'category)
450 (setq category (car (cdr props))))
451 (princ (format "%-20s %S\n"
452 (car props) (car (cdr props))))
453 (setq props (cdr (cdr props))))
454 (if category
455 (progn
456 (setq props (symbol-plist category))
457 (princ (format "\nCategory %s:\n\n" category))
458 (while props
459 (princ (format "%-20s %S\n"
460 (car props) (car (cdr props))))
461 (if (eq (car props) 'category)
462 (setq category (car (cdr props))))
463 (setq props (cdr (cdr props)))))))))))
465 ;;;###autoload
466 (defun facemenu-read-color (&optional prompt)
467 "Read a color using the minibuffer."
468 (let ((col (completing-read (or prompt "Color: ")
469 (or facemenu-color-alist
470 (if window-system
471 (mapcar 'list (x-defined-colors))))
472 nil t)))
473 (if (equal "" col)
475 col)))
477 ;;;###autoload
478 (defun list-colors-display (&optional list)
479 "Display names of defined colors, and show what they look like.
480 If the optional argument LIST is non-nil, it should be a list of
481 colors to display. Otherwise, this command computes a list
482 of colors that the current display can handle."
483 (interactive)
484 (if (and (null list) window-system)
485 (progn
486 (setq list (x-defined-colors))
487 ;; Delete duplicate colors.
488 (let ((l list))
489 (while (cdr l)
490 (if (facemenu-color-equal (car l) (car (cdr l)))
491 (setcdr l (cdr (cdr l)))
492 (setq l (cdr l)))))))
493 (with-output-to-temp-buffer "*Colors*"
494 (save-excursion
495 (set-buffer standard-output)
496 (let (s)
497 (while list
498 (setq s (point))
499 (insert (car list))
500 (indent-to 20)
501 (put-text-property s (point) 'face
502 (cons 'background-color (car list)))
503 (setq s (point))
504 (insert " " (car list) "\n")
505 (put-text-property s (point) 'face
506 (cons 'foreground-color (car list)))
507 (setq list (cdr list)))))))
509 (defun facemenu-color-equal (a b)
510 "Return t if colors A and B are the same color.
511 A and B should be strings naming colors.
512 This function queries the window-system server to find out what the
513 color names mean. It returns nil if the colors differ or if it can't
514 determine the correct answer."
515 (cond ((equal a b) t)
516 ((and (memq window-system '(x w32))
517 (equal (x-color-values a) (x-color-values b))))
518 ((eq window-system 'pc)
519 (and (x-color-defined-p a) (x-color-defined-p b)
520 (eq (msdos-color-translate a) (msdos-color-translate b))))))
522 (defun facemenu-add-face (face &optional start end)
523 "Add FACE to text between START and END.
524 If START is `nil' or START to END is empty, add FACE to next typed character
525 instead. For each section of that region that has a different face property,
526 FACE will be consed onto it, and other faces that are completely hidden by
527 that will be removed from the list.
528 If `facemenu-add-face-function' and maybe `facemenu-end-add-face' are non-`nil'
529 they are used to set the face information.
531 As a special case, if FACE is `default', then the region is left with NO face
532 text property. Otherwise, selecting the default face would not have any
533 effect. See `facemenu-remove-face-function'."
534 (interactive "*xFace: \nr")
535 (if (and (eq face 'default)
536 (not (eq facemenu-remove-face-function t)))
537 (if facemenu-remove-face-function
538 (funcall facemenu-remove-face-function start end)
539 (if (and start (< start end))
540 (remove-text-properties start end '(face default))
541 (setq self-insert-face 'default
542 self-insert-face-command this-command)))
543 (if facemenu-add-face-function
544 (save-excursion
545 (if end (goto-char end))
546 (save-excursion
547 (if start (goto-char start))
548 (insert-before-markers
549 (funcall facemenu-add-face-function face end)))
550 (if facemenu-end-add-face
551 (insert (if (stringp facemenu-end-add-face)
552 facemenu-end-add-face
553 (funcall facemenu-end-add-face face)))))
554 (if (and start (< start end))
555 (let ((part-start start) part-end)
556 (while (not (= part-start end))
557 (setq part-end (next-single-property-change part-start 'face
558 nil end))
559 (let ((prev (get-text-property part-start 'face)))
560 (put-text-property part-start part-end 'face
561 (if (null prev)
562 face
563 (facemenu-active-faces
564 (cons face
565 (if (listp prev)
566 prev
567 (list prev)))))))
568 (setq part-start part-end)))
569 (setq self-insert-face (if (eq last-command self-insert-face-command)
570 (cons face (if (listp self-insert-face)
571 self-insert-face
572 (list self-insert-face)))
573 face)
574 self-insert-face-command this-command)))))
576 (defun facemenu-active-faces (face-list &optional frame)
577 "Return from FACE-LIST those faces that would be used for display.
578 This means each face attribute is not specified in a face earlier in FACE-LIST
579 and such a face is therefore active when used to display text.
580 If the optional argument FRAME is given, use the faces in that frame; otherwise
581 use the selected frame. If t, then the global, non-frame faces are used."
582 (let* ((mask-atts (copy-sequence (internal-get-face (car face-list) frame)))
583 (active-list (list (car face-list)))
584 (face-list (cdr face-list))
585 (mask-len (length mask-atts)))
586 (while face-list
587 (if (let ((face-atts (internal-get-face (car face-list) frame))
588 (i mask-len) (useful nil))
589 (while (> (setq i (1- i)) 1)
590 (and (aref face-atts i) (not (aref mask-atts i))
591 (aset mask-atts i (setq useful t))))
592 useful)
593 (setq active-list (cons (car face-list) active-list)))
594 (setq face-list (cdr face-list)))
595 (nreverse active-list)))
597 (defun facemenu-get-face (symbol)
598 "Make sure FACE exists.
599 If not, create it and add it to the appropriate menu. Return the symbol.
601 If a window system is in use, and this function creates a face named
602 `fg:color', then it sets the foreground to that color. Likewise, `bg:color'
603 means to set the background. In either case, if the color is undefined,
604 no color is set and a warning is issued."
605 (let ((name (symbol-name symbol))
606 foreground)
607 (cond ((internal-find-face symbol))
608 ((and window-system
609 (or (setq foreground (string-match "^fg:" name))
610 (string-match "^bg:" name)))
611 (let ((face (make-face symbol))
612 (color (substring name 3)))
613 (if (x-color-defined-p color)
614 (if foreground
615 (set-face-foreground face color)
616 (set-face-background face color))
617 (message "Color \"%s\" undefined" color))))
618 (t (make-face symbol))))
619 symbol)
621 (defun facemenu-add-new-face (face)
622 "Add a FACE to the appropriate Face menu.
623 Automatically called when a new face is created."
624 (let* ((name (symbol-name face))
625 menu docstring
626 (key (cdr (assoc face facemenu-keybindings)))
627 function menu-val)
628 (cond ((string-match "^fg:" name)
629 (setq name (substring name 3))
630 (setq docstring
631 (format "Select foreground color %s for subsequent insertion."
632 name))
633 (setq menu 'facemenu-foreground-menu))
634 ((string-match "^bg:" name)
635 (setq name (substring name 3))
636 (setq docstring
637 (format "Select background color %s for subsequent insertion."
638 name))
639 (setq menu 'facemenu-background-menu))
641 (setq docstring
642 (format "Select face `%s' for subsequent insertion."
643 name))
644 (setq menu 'facemenu-face-menu)))
645 (cond ((eq t facemenu-unlisted-faces))
646 ((memq face facemenu-unlisted-faces))
647 ;; test against regexps in facemenu-unlisted-faces
648 ((let ((unlisted facemenu-unlisted-faces)
649 (matched nil))
650 (while (and unlisted (not matched))
651 (if (and (stringp (car unlisted))
652 (string-match (car unlisted) name))
653 (setq matched t)
654 (setq unlisted (cdr unlisted))))
655 matched))
656 (key ; has a keyboard equivalent. These go at the front.
657 (setq function (intern (concat "facemenu-set-" name)))
658 (fset function
659 `(lambda ()
660 ,docstring
661 (interactive)
662 (facemenu-set-face (quote ,face))))
663 (define-key 'facemenu-keymap key (cons name function))
664 (define-key menu key (cons name function)))
665 ((facemenu-iterate ; check if equivalent face is already in the menu
666 (lambda (m) (and (listp m)
667 (symbolp (car m))
668 (face-equal (car m) face)))
669 (cdr (symbol-function menu))))
670 (t ; No keyboard equivalent. Figure out where to put it:
671 (setq key (vector face)
672 function 'facemenu-set-face-from-menu
673 menu-val (symbol-function menu))
674 (if (and facemenu-new-faces-at-end
675 (> (length menu-val) 3))
676 (define-key-after menu-val key (cons name function)
677 (car (nth (- (length menu-val) 3) menu-val)))
678 (define-key menu key (cons name function))))))
679 nil) ; Return nil for facemenu-iterate
681 (defun facemenu-complete-face-list (&optional oldlist)
682 "Return list of all faces that look different.
683 Starts with given ALIST of faces, and adds elements only if they display
684 differently from any face already on the list.
685 The faces on ALIST will end up at the end of the returned list, in reverse
686 order."
687 (let ((list (nreverse (mapcar 'car oldlist))))
688 (facemenu-iterate
689 (lambda (new-face)
690 (if (not (memq new-face list))
691 (setq list (cons new-face list)))
692 nil)
693 (nreverse (face-list)))
694 list))
696 (defun facemenu-iterate (func iterate-list)
697 "Apply FUNC to each element of LIST until one returns non-nil.
698 Returns the non-nil value it found, or nil if all were nil."
699 (while (and iterate-list (not (funcall func (car iterate-list))))
700 (setq iterate-list (cdr iterate-list)))
701 (car iterate-list))
703 (facemenu-update)
705 ;;; facemenu.el ends here