("Czech"): Fix the documentation.
[bpt/emacs.git] / lisp / facemenu.el
1 ;;; facemenu.el --- create a face menu for interactively adding fonts to text
2
3 ;; Copyright (c) 1994, 1995, 1996, 2001, 2002 Free Software Foundation, Inc.
4
5 ;; Author: Boris Goldowsky <boris@gnu.org>
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
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.
24
25 ;;; Commentary:
26
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.
36 ;;
37 ;; The menu also contains submenus for indentation and justification-changing
38 ;; commands.
39
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.
46 ;;
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.
50
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
72 ;;
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'.
77
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.
82 ;;
83 ;; There is at present no way to display what the faces look like in
84 ;; the menu itself.
85 ;;
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.
90 ;;
91 ;; Please send me any other problems, comments or ideas.
92
93 ;;; Code:
94
95 (provide 'facemenu)
96
97 (eval-when-compile
98 (require 'help)
99 (require 'button))
100
101 ;;; Provide some binding for startup:
102 ;;;###autoload (define-key global-map "\M-g" 'facemenu-keymap)
103 ;;;###autoload (autoload 'facemenu-keymap "facemenu" "Keymap for face-changing commands." t 'keymap)
104
105 ;; Global bindings:
106 (define-key global-map [C-down-mouse-2] 'facemenu-menu)
107 (define-key global-map "\M-g" 'facemenu-keymap)
108
109 (defgroup facemenu nil
110 "Create a face menu for interactively adding fonts to text"
111 :group 'faces
112 :prefix "facemenu-")
113
114 (defcustom facemenu-keybindings
115 '((default . "d")
116 (bold . "b")
117 (italic . "i")
118 (bold-italic . "l") ; {bold} intersect {italic} = {l}
119 (underline . "u"))
120 "Alist of interesting faces and keybindings.
121 Each element is itself a list: the car is the name of the face,
122 the next element is the key to use as a keyboard equivalent of the menu item;
123 the binding is made in `facemenu-keymap'.
124
125 The faces specifically mentioned in this list are put at the top of
126 the menu, in the order specified. All other faces which are defined,
127 except for those in `facemenu-unlisted-faces', are listed after them,
128 but get no keyboard equivalents.
129
130 If you change this variable after loading facemenu.el, you will need to call
131 `facemenu-update' to make it take effect."
132 :type '(repeat (cons face string))
133 :group 'facemenu)
134
135 (defcustom facemenu-new-faces-at-end t
136 "*Where in the menu to insert newly-created faces.
137 This should be nil to put them at the top of the menu, or t to put them
138 just before \"Other\" at the end."
139 :type 'boolean
140 :group 'facemenu)
141
142 (defcustom facemenu-unlisted-faces
143 `(modeline region secondary-selection highlight scratch-face
144 ,(purecopy "^font-lock-") ,(purecopy "^gnus-") ,(purecopy "^message-")
145 ,(purecopy "^ediff-") ,(purecopy "^term-") ,(purecopy "^vc-")
146 ,(purecopy "^widget-") ,(purecopy "^custom-") ,(purecopy "^vm-"))
147 "*List of faces not to include in the Face menu.
148 Each element may be either a symbol, which is the name of a face, or a string,
149 which is a regular expression to be matched against face names. Matching
150 faces will not be added to the menu.
151
152 You can set this list before loading facemenu.el, or add a face to it before
153 creating that face if you do not want it to be listed. If you change the
154 variable so as to eliminate faces that have already been added to the menu,
155 call `facemenu-update' to recalculate the menu contents.
156
157 If this variable is t, no faces will be added to the menu. This is useful for
158 temporarily turning off the feature that automatically adds faces to the menu
159 when they are created."
160 :type '(choice (const :tag "Don't add faces" t)
161 (const :tag "None (do add any face)" nil)
162 (repeat (choice symbol regexp)))
163 :group 'facemenu)
164
165 ;;;###autoload
166 (defvar facemenu-face-menu
167 (let ((map (make-sparse-keymap "Face")))
168 (define-key map "o" (cons "Other..." 'facemenu-set-face))
169 map)
170 "Menu keymap for faces.")
171 ;;;###autoload
172 (defalias 'facemenu-face-menu facemenu-face-menu)
173
174 ;;;###autoload
175 (defvar facemenu-foreground-menu
176 (let ((map (make-sparse-keymap "Foreground Color")))
177 (define-key map "o" (cons "Other..." 'facemenu-set-foreground))
178 map)
179 "Menu keymap for foreground colors.")
180 ;;;###autoload
181 (defalias 'facemenu-foreground-menu facemenu-foreground-menu)
182
183 ;;;###autoload
184 (defvar facemenu-background-menu
185 (let ((map (make-sparse-keymap "Background Color")))
186 (define-key map "o" (cons "Other..." 'facemenu-set-background))
187 map)
188 "Menu keymap for background colors.")
189 ;;;###autoload
190 (defalias 'facemenu-background-menu facemenu-background-menu)
191
192 ;;;###autoload
193 (defvar facemenu-special-menu
194 (let ((map (make-sparse-keymap "Special")))
195 (define-key map [?s] (cons (purecopy "Remove Special")
196 'facemenu-remove-special))
197 (define-key map [?t] (cons (purecopy "Intangible")
198 'facemenu-set-intangible))
199 (define-key map [?v] (cons (purecopy "Invisible")
200 'facemenu-set-invisible))
201 (define-key map [?r] (cons (purecopy "Read-Only")
202 'facemenu-set-read-only))
203 map)
204 "Menu keymap for non-face text-properties.")
205 ;;;###autoload
206 (defalias 'facemenu-special-menu facemenu-special-menu)
207
208 ;;;###autoload
209 (defvar facemenu-justification-menu
210 (let ((map (make-sparse-keymap "Justification")))
211 (define-key map [?c] (cons (purecopy "Center") 'set-justification-center))
212 (define-key map [?b] (cons (purecopy "Full") 'set-justification-full))
213 (define-key map [?r] (cons (purecopy "Right") 'set-justification-right))
214 (define-key map [?l] (cons (purecopy "Left") 'set-justification-left))
215 (define-key map [?u] (cons (purecopy "Unfilled") 'set-justification-none))
216 map)
217 "Submenu for text justification commands.")
218 ;;;###autoload
219 (defalias 'facemenu-justification-menu facemenu-justification-menu)
220
221 ;;;###autoload
222 (defvar facemenu-indentation-menu
223 (let ((map (make-sparse-keymap "Indentation")))
224 (define-key map [decrease-right-margin]
225 (cons (purecopy "Indent Right Less") 'decrease-right-margin))
226 (define-key map [increase-right-margin]
227 (cons (purecopy "Indent Right More") 'increase-right-margin))
228 (define-key map [decrease-left-margin]
229 (cons (purecopy "Indent Less") 'decrease-left-margin))
230 (define-key map [increase-left-margin]
231 (cons (purecopy "Indent More") 'increase-left-margin))
232 map)
233 "Submenu for indentation commands.")
234 ;;;###autoload
235 (defalias 'facemenu-indentation-menu facemenu-indentation-menu)
236
237 ;; This is split up to avoid an overlong line in loaddefs.el.
238 ;;;###autoload
239 (defvar facemenu-menu nil
240 "Facemenu top-level menu keymap.")
241 ;;;###autoload
242 (setq facemenu-menu (make-sparse-keymap "Text Properties"))
243 ;;;###autoload
244 (let ((map facemenu-menu))
245 (define-key map [dc] (cons (purecopy "Display Colors") 'list-colors-display))
246 (define-key map [df] (cons (purecopy "Display Faces") 'list-faces-display))
247 (define-key map [dp] (cons (purecopy "Describe Text")
248 'describe-text-at))
249 (define-key map [ra] (cons (purecopy "Remove Text Properties")
250 'facemenu-remove-all))
251 (define-key map [rm] (cons (purecopy "Remove Face Properties")
252 'facemenu-remove-face-props))
253 (define-key map [s1] (list (purecopy "--"))))
254 ;;;###autoload
255 (let ((map facemenu-menu))
256 (define-key map [in] (cons (purecopy "Indentation")
257 'facemenu-indentation-menu))
258 (define-key map [ju] (cons (purecopy "Justification")
259 'facemenu-justification-menu))
260 (define-key map [s2] (list (purecopy "--")))
261 (define-key map [sp] (cons (purecopy "Special Properties")
262 'facemenu-special-menu))
263 (define-key map [bg] (cons (purecopy "Background Color")
264 'facemenu-background-menu))
265 (define-key map [fg] (cons (purecopy "Foreground Color")
266 'facemenu-foreground-menu))
267 (define-key map [fc] (cons (purecopy "Face")
268 'facemenu-face-menu)))
269 ;;;###autoload
270 (defalias 'facemenu-menu facemenu-menu)
271
272 (defvar facemenu-keymap
273 (let ((map (make-sparse-keymap "Set face")))
274 (define-key map "o" (cons (purecopy "Other...") 'facemenu-set-face))
275 map)
276 "Keymap for face-changing commands.
277 `Facemenu-update' fills in the keymap according to the bindings
278 requested in `facemenu-keybindings'.")
279 (defalias 'facemenu-keymap facemenu-keymap)
280
281
282 (defcustom facemenu-add-face-function nil
283 "Function called at beginning of text to change or nil.
284 This function is passed the FACE to set and END of text to change, and must
285 return a string which is inserted. It may set `facemenu-end-add-face'."
286 :type '(choice (const :tag "None" nil)
287 function)
288 :group 'facemenu)
289
290 (defcustom facemenu-end-add-face nil
291 "String to insert or function called at end of text to change or nil.
292 This function is passed the FACE to set, and must return a string which is
293 inserted."
294 :type '(choice (const :tag "None" nil)
295 string
296 function)
297 :group 'facemenu)
298
299 (defcustom facemenu-remove-face-function nil
300 "When non-nil, this is a function called to remove faces.
301 This function is passed the START and END of text to change.
302 May also be t meaning to use `facemenu-add-face-function'."
303 :type '(choice (const :tag "None" nil)
304 (const :tag "Use add-face" t)
305 function)
306 :group 'facemenu)
307
308 ;;; Internal Variables
309
310 (defvar facemenu-color-alist nil
311 ;; Don't initialize here; that doesn't work if preloaded.
312 "Alist of colors, used for completion.
313 If null, `facemenu-read-color' will set it.")
314
315 (defun facemenu-update ()
316 "Add or update the \"Face\" menu in the menu bar.
317 You can call this to update things if you change any of the menu configuration
318 variables."
319 (interactive)
320
321 ;; Add each defined face to the menu.
322 (facemenu-iterate 'facemenu-add-new-face
323 (facemenu-complete-face-list facemenu-keybindings)))
324
325 ;;;###autoload
326 (defun facemenu-set-face (face &optional start end)
327 "Add FACE to the region or next character typed.
328 This adds FACE to the top of the face list; any faces lower on the list that
329 will not show through at all will be removed.
330
331 Interactively, reads the face name with the minibuffer.
332
333 If the region is active (normally true except in Transient Mark mode)
334 and there is no prefix argument, this command sets the region to the
335 requested face.
336
337 Otherwise, this command specifies the face for the next character
338 inserted. Moving point or switching buffers before
339 typing a character to insert cancels the specification."
340 (interactive (list (progn
341 (barf-if-buffer-read-only)
342 (read-face-name "Use face"))
343 (if (and mark-active (not current-prefix-arg))
344 (region-beginning))
345 (if (and mark-active (not current-prefix-arg))
346 (region-end))))
347 (facemenu-add-new-face face)
348 (facemenu-add-face face start end))
349
350 ;;;###autoload
351 (defun facemenu-set-foreground (color &optional start end)
352 "Set the foreground COLOR of the region or next character typed.
353 The color is prompted for. A face named `fg:color' is used \(or created).
354
355 If the region is active (normally true except in Transient Mark mode)
356 and there is no prefix argument, this command sets the region to the
357 requested face.
358
359 Otherwise, this command specifies the face for the next character
360 inserted. Moving point or switching buffers before
361 typing a character to insert cancels the specification."
362 (interactive (list (progn
363 (barf-if-buffer-read-only)
364 (facemenu-read-color "Foreground color: "))
365 (if (and mark-active (not current-prefix-arg))
366 (region-beginning))
367 (if (and mark-active (not current-prefix-arg))
368 (region-end))))
369 (unless (color-defined-p color)
370 (message "Color `%s' undefined" color))
371 (facemenu-add-new-face color 'facemenu-foreground-menu)
372 (facemenu-add-face (list (list :foreground color)) start end))
373
374 ;;;###autoload
375 (defun facemenu-set-background (color &optional start end)
376 "Set the background COLOR of the region or next character typed.
377 Reads the color in the minibuffer.
378
379 If the region is active (normally true except in Transient Mark mode)
380 and there is no prefix argument, this command sets the region to the
381 requested face.
382
383 Otherwise, this command specifies the face for the next character
384 inserted. Moving point or switching buffers before
385 typing a character to insert cancels the specification."
386 (interactive (list (progn
387 (barf-if-buffer-read-only)
388 (facemenu-read-color "Background color: "))
389 (if (and mark-active (not current-prefix-arg))
390 (region-beginning))
391 (if (and mark-active (not current-prefix-arg))
392 (region-end))))
393 (unless (color-defined-p color)
394 (message "Color `%s' undefined" color))
395 (facemenu-add-new-face color 'facemenu-background-menu)
396 (facemenu-add-face (list (list :background color)) start end))
397
398 ;;;###autoload
399 (defun facemenu-set-face-from-menu (face start end)
400 "Set the FACE of the region or next character typed.
401 This function is designed to be called from a menu; the face to use
402 is the menu item's name.
403
404 If the region is active (normally true except in Transient Mark mode)
405 and there is no prefix argument, this command sets the region to the
406 requested face.
407
408 Otherwise, this command specifies the face for the next character
409 inserted. Moving point or switching buffers before
410 typing a character to insert cancels the specification."
411 (interactive (list last-command-event
412 (if (and mark-active (not current-prefix-arg))
413 (region-beginning))
414 (if (and mark-active (not current-prefix-arg))
415 (region-end))))
416 (barf-if-buffer-read-only)
417 (facemenu-get-face face)
418 (if start
419 (facemenu-add-face face start end)
420 (facemenu-add-face face)))
421
422 ;;;###autoload
423 (defun facemenu-set-invisible (start end)
424 "Make the region invisible.
425 This sets the `invisible' text property; it can be undone with
426 `facemenu-remove-special'."
427 (interactive "r")
428 (add-text-properties start end '(invisible t)))
429
430 ;;;###autoload
431 (defun facemenu-set-intangible (start end)
432 "Make the region intangible: disallow moving into it.
433 This sets the `intangible' text property; it can be undone with
434 `facemenu-remove-special'."
435 (interactive "r")
436 (add-text-properties start end '(intangible t)))
437
438 ;;;###autoload
439 (defun facemenu-set-read-only (start end)
440 "Make the region unmodifiable.
441 This sets the `read-only' text property; it can be undone with
442 `facemenu-remove-special'."
443 (interactive "r")
444 (add-text-properties start end '(read-only t)))
445
446 ;;;###autoload
447 (defun facemenu-remove-face-props (start end)
448 "Remove `face' and `mouse-face' text properties."
449 (interactive "*r") ; error if buffer is read-only despite the next line.
450 (let ((inhibit-read-only t))
451 (remove-text-properties
452 start end '(face nil mouse-face nil))))
453
454 ;;;###autoload
455 (defun facemenu-remove-all (start end)
456 "Remove all text properties from the region."
457 (interactive "*r") ; error if buffer is read-only despite the next line.
458 (let ((inhibit-read-only t))
459 (set-text-properties start end nil)))
460
461 ;;;###autoload
462 (defun facemenu-remove-special (start end)
463 "Remove all the \"special\" text properties from the region.
464 These special properties include `invisible', `intangible' and `read-only'."
465 (interactive "*r") ; error if buffer is read-only despite the next line.
466 (let ((inhibit-read-only t))
467 (remove-text-properties
468 start end '(invisible nil intangible nil read-only nil))))
469
470 ;;; Describe-Text Mode.
471
472 (defun describe-text-done ()
473 "Delete the current window or bury the current buffer."
474 (interactive)
475 (if (> (count-windows) 1)
476 (delete-window)
477 (bury-buffer)))
478
479 (defvar describe-text-mode-map
480 (let ((map (make-sparse-keymap)))
481 (set-keymap-parent map widget-keymap)
482 map)
483 "Keymap for `describe-text-mode'.")
484
485 (defcustom describe-text-mode-hook nil
486 "List of hook functions ran by `describe-text-mode'."
487 :type 'hook)
488
489 (defun describe-text-mode ()
490 "Major mode for buffers created by `describe-text-at'.
491
492 \\{describe-text-mode-map}
493 Entry to this mode calls the value of `describe-text-mode-hook'
494 if that value is non-nil."
495 (kill-all-local-variables)
496 (setq major-mode 'describe-text-mode
497 mode-name "Describe-Text")
498 (use-local-map describe-text-mode-map)
499 (widget-setup)
500 (run-hooks 'describe-text-mode-hook))
501
502 ;;; Describe-Text Utilities.
503
504 (defun describe-text-widget (widget)
505 "Insert text to describe WIDGET in the current buffer."
506 (widget-create 'link
507 :notify `(lambda (&rest ignore)
508 (widget-browse ',widget))
509 (format "%S" (if (symbolp widget)
510 widget
511 (car widget))))
512 (widget-insert " ")
513 (widget-create 'info-link :tag "widget" "(widget)Top"))
514
515 (defun describe-text-sexp (sexp)
516 "Insert a short description of SEXP in the current buffer."
517 (let ((pp (condition-case signal
518 (pp-to-string sexp)
519 (error (prin1-to-string signal)))))
520 (when (string-match "\n\\'" pp)
521 (setq pp (substring pp 0 (1- (length pp)))))
522 (if (cond ((string-match "\n" pp)
523 nil)
524 ((> (length pp) (- (window-width) (current-column)))
525 nil)
526 (t t))
527 (widget-insert pp)
528 (widget-create 'push-button
529 :tag "show"
530 :action (lambda (widget &optional event)
531 (with-output-to-temp-buffer
532 "*Pp Eval Output*"
533 (princ (widget-get widget :value))))
534 pp))))
535
536
537 (defun describe-text-properties (properties)
538 "Insert a description of PROPERTIES in the current buffer.
539 PROPERTIES should be a list of overlay or text properties.
540 The `category' property is made into a widget button that call
541 `describe-text-category' when pushed."
542 (while properties
543 (widget-insert (format " %-20s " (car properties)))
544 (let ((key (nth 0 properties))
545 (value (nth 1 properties)))
546 (cond ((eq key 'category)
547 (widget-create 'link
548 :notify `(lambda (&rest ignore)
549 (describe-text-category ',value))
550 (format "%S" value)))
551 ((widgetp value)
552 (describe-text-widget value))
553 (t
554 (describe-text-sexp value))))
555 (widget-insert "\n")
556 (setq properties (cdr (cdr properties)))))
557
558 ;;; Describe-Text Commands.
559
560 (defun describe-text-category (category)
561 "Describe a text property category."
562 (interactive "S")
563 (when (get-buffer "*Text Category*")
564 (kill-buffer "*Text Category*"))
565 (save-excursion
566 (with-output-to-temp-buffer "*Text Category*"
567 (set-buffer "*Text Category*")
568 (widget-insert "Category " (format "%S" category) ":\n\n")
569 (describe-text-properties (symbol-plist category))
570 (describe-text-mode)
571 (goto-char (point-min)))))
572
573 ;;;###autoload
574 (defun describe-text-at (pos)
575 "Describe widgets, buttons, overlays and text properties at POS."
576 (interactive "d")
577 (when (eq (current-buffer) (get-buffer "*Text Description*"))
578 (error "Can't do self inspection"))
579 (let* ((properties (text-properties-at pos))
580 (overlays (overlays-at pos))
581 overlay
582 (wid-field (get-char-property pos 'field))
583 (wid-button (get-char-property pos 'button))
584 (wid-doc (get-char-property pos 'widget-doc))
585 ;; If button.el is not loaded, we have no buttons in the text.
586 (button (and (fboundp 'button-at) (button-at pos)))
587 (button-type (and button (button-type button)))
588 (button-label (and button (button-label button)))
589 (widget (or wid-field wid-button wid-doc)))
590 (if (not (or properties overlays))
591 (message "This is plain text.")
592 (when (get-buffer "*Text Description*")
593 (kill-buffer "*Text Description*"))
594 (save-excursion
595 (with-output-to-temp-buffer "*Text Description*"
596 (set-buffer "*Text Description*")
597 (widget-insert "Text content at position " (format "%d" pos) ":\n\n")
598 ;; Widgets
599 (when (widgetp widget)
600 (widget-insert (cond (wid-field "This is an editable text area")
601 (wid-button "This is an active area")
602 (wid-doc "This is documentation text")))
603 (widget-insert " of a ")
604 (describe-text-widget widget)
605 (widget-insert ".\n\n"))
606 ;; Buttons
607 (when (and button (not (widgetp wid-button)))
608 (widget-insert "Here is a " (format "%S" button-type)
609 " button labeled `" button-label "'.\n\n"))
610 ;; Overlays
611 (when overlays
612 (if (eq (length overlays) 1)
613 (widget-insert "There is an overlay here:\n")
614 (widget-insert "There are " (format "%d" (length overlays))
615 " overlays here:\n"))
616 (dolist (overlay overlays)
617 (widget-insert " From " (format "%d" (overlay-start overlay))
618 " to " (format "%d" (overlay-end overlay)) "\n")
619 (describe-text-properties (overlay-properties overlay)))
620 (widget-insert "\n"))
621 ;; Text properties
622 (when properties
623 (widget-insert "There are text properties here:\n")
624 (describe-text-properties properties))
625 (describe-text-mode)
626 (goto-char (point-min)))))))
627
628 ;;; List Text Properties
629
630 ;;;###autoload
631 (defun list-text-properties-at (p)
632 "Pop up a buffer listing text-properties at LOCATION."
633 (interactive "d")
634 (let ((props (text-properties-at p))
635 category
636 str)
637 (if (null props)
638 (message "None")
639 (if (and (not (cdr (cdr props)))
640 (not (eq (car props) 'category))
641 (< (length (setq str (format "Text property at %d: %s %S"
642 p (car props) (car (cdr props)))))
643 (frame-width)))
644 (message "%s" str)
645 (with-output-to-temp-buffer "*Text Properties*"
646 (princ (format "Text properties at %d:\n\n" p))
647 (setq help-xref-stack nil)
648 (while props
649 (if (eq (car props) 'category)
650 (setq category (car (cdr props))))
651 (princ (format "%-20s %S\n"
652 (car props) (car (cdr props))))
653 (setq props (cdr (cdr props))))
654 (if category
655 (progn
656 (setq props (symbol-plist category))
657 (princ (format "\nCategory %s:\n\n" category))
658 (while props
659 (princ (format "%-20s %S\n"
660 (car props) (car (cdr props))))
661 (if (eq (car props) 'category)
662 (setq category (car (cdr props))))
663 (setq props (cdr (cdr props)))))))))))
664
665 ;;;###autoload
666 (defun facemenu-read-color (&optional prompt)
667 "Read a color using the minibuffer."
668 (let ((col (completing-read (or prompt "Color: ")
669 (or facemenu-color-alist
670 (mapcar 'list (defined-colors)))
671 nil t)))
672 (if (equal "" col)
673 nil
674 col)))
675
676 ;;;###autoload
677 (defun list-colors-display (&optional list)
678 "Display names of defined colors, and show what they look like.
679 If the optional argument LIST is non-nil, it should be a list of
680 colors to display. Otherwise, this command computes a list
681 of colors that the current display can handle."
682 (interactive)
683 (when (and (null list) (> (display-color-cells) 0))
684 (setq list (defined-colors))
685 ;; Delete duplicate colors.
686 (let ((l list))
687 (while (cdr l)
688 (if (facemenu-color-equal (car l) (car (cdr l)))
689 (setcdr l (cdr (cdr l)))
690 (setq l (cdr l)))))
691 ;; Don't show more than what the display can handle.
692 (let ((lc (nthcdr (1- (display-color-cells)) list)))
693 (if lc
694 (setcdr lc nil))))
695 (with-output-to-temp-buffer "*Colors*"
696 (save-excursion
697 (set-buffer standard-output)
698 (let (s)
699 (while list
700 (setq s (point))
701 (insert (car list))
702 (indent-to 20)
703 (put-text-property s (point) 'face
704 (cons 'background-color (car list)))
705 (setq s (point))
706 (insert " " (car list) "\n")
707 (put-text-property s (point) 'face
708 (cons 'foreground-color (car list)))
709 (setq list (cdr list)))))))
710
711 (defun facemenu-color-equal (a b)
712 "Return t if colors A and B are the same color.
713 A and B should be strings naming colors.
714 This function queries the display system to find out what the color
715 names mean. It returns nil if the colors differ or if it can't
716 determine the correct answer."
717 (cond ((equal a b) t)
718 ((equal (color-values a) (color-values b)))))
719
720 (defun facemenu-add-face (face &optional start end)
721 "Add FACE to text between START and END.
722 If START is nil or START to END is empty, add FACE to next typed character
723 instead. For each section of that region that has a different face property,
724 FACE will be consed onto it, and other faces that are completely hidden by
725 that will be removed from the list.
726 If `facemenu-add-face-function' and maybe `facemenu-end-add-face' are non-`nil'
727 they are used to set the face information.
728
729 As a special case, if FACE is `default', then the region is left with NO face
730 text property. Otherwise, selecting the default face would not have any
731 effect. See `facemenu-remove-face-function'."
732 (interactive "*xFace: \nr")
733 (if (and (eq face 'default)
734 (not (eq facemenu-remove-face-function t)))
735 (if facemenu-remove-face-function
736 (funcall facemenu-remove-face-function start end)
737 (if (and start (< start end))
738 (remove-text-properties start end '(face default))
739 (setq self-insert-face 'default
740 self-insert-face-command this-command)))
741 (if facemenu-add-face-function
742 (save-excursion
743 (if end (goto-char end))
744 (save-excursion
745 (if start (goto-char start))
746 (insert-before-markers
747 (funcall facemenu-add-face-function face end)))
748 (if facemenu-end-add-face
749 (insert (if (stringp facemenu-end-add-face)
750 facemenu-end-add-face
751 (funcall facemenu-end-add-face face)))))
752 (if (and start (< start end))
753 (let ((part-start start) part-end)
754 (while (not (= part-start end))
755 (setq part-end (next-single-property-change part-start 'face
756 nil end))
757 (let ((prev (get-text-property part-start 'face)))
758 (put-text-property part-start part-end 'face
759 (if (null prev)
760 face
761 (facemenu-active-faces
762 (cons face
763 (if (listp prev)
764 prev
765 (list prev)))))))
766 (setq part-start part-end)))
767 (setq self-insert-face (if (eq last-command self-insert-face-command)
768 (cons face (if (listp self-insert-face)
769 self-insert-face
770 (list self-insert-face)))
771 face)
772 self-insert-face-command this-command)))))
773
774 (defun facemenu-active-faces (face-list &optional frame)
775 "Return from FACE-LIST those faces that would be used for display.
776 This means each face attribute is not specified in a face earlier in FACE-LIST
777 and such a face is therefore active when used to display text.
778 If the optional argument FRAME is given, use the faces in that frame; otherwise
779 use the selected frame. If t, then the global, non-frame faces are used."
780 (let* ((mask-atts (copy-sequence
781 (if (consp (car face-list))
782 (face-attributes-as-vector (car face-list))
783 (or (internal-lisp-face-p (car face-list) frame)
784 (check-face (car face-list))))))
785 (active-list (list (car face-list)))
786 (face-list (cdr face-list))
787 (mask-len (length mask-atts)))
788 (while face-list
789 (if (let ((face-atts
790 (if (consp (car face-list))
791 (face-attributes-as-vector (car face-list))
792 (or (internal-lisp-face-p (car face-list) frame)
793 (check-face (car face-list)))))
794 (i mask-len)
795 (useful nil))
796 (while (> (setq i (1- i)) 1)
797 (and (not (memq (aref face-atts i) '(nil unspecified)))
798 (memq (aref mask-atts i) '(nil unspecified))
799 (aset mask-atts i (setq useful t))))
800 useful)
801 (setq active-list (cons (car face-list) active-list)))
802 (setq face-list (cdr face-list)))
803 (nreverse active-list)))
804
805 (defun facemenu-get-face (symbol)
806 "Make sure FACE exists.
807 If not, create it and add it to the appropriate menu. Return the SYMBOL."
808 (let ((name (symbol-name symbol))
809 foreground)
810 (cond ((facep symbol))
811 (t (make-face symbol))))
812 symbol)
813
814 (defun facemenu-add-new-face (face-or-color &optional menu)
815 "Add FACE-OR-COLOR (a face or a color) to the appropriate Face menu.
816 If MENU is nil, then FACE-OR-COLOR is a face to be added
817 to `facemenu-face-menu'. If MENU is `facemenu-foreground-menu'
818 or `facemenu-background-menu', FACE-OR-COLOR is a color
819 to be added to the specified menu.
820
821 This is called whenever you create a new face."
822 (let* (name
823 symbol
824 docstring
825 (key (cdr (assoc face-or-color facemenu-keybindings)))
826 function menu-val)
827 (if (symbolp face-or-color)
828 (setq name (symbol-name face-or-color)
829 symbol face-or-color)
830 (setq name face-or-color
831 symbol (intern name)))
832 (cond ((eq menu 'facemenu-foreground-menu)
833 (setq docstring
834 (format "Select foreground color %s for subsequent insertion."
835 name)))
836 ((eq menu 'facemenu-background-menu)
837 (setq docstring
838 (format "Select background color %s for subsequent insertion."
839 name)))
840 (t
841 (setq menu 'facemenu-face-menu)
842 (setq docstring
843 (format "Select face `%s' for subsequent insertion."
844 name))))
845 (cond ((eq t facemenu-unlisted-faces))
846 ((memq symbol facemenu-unlisted-faces))
847 ;; test against regexps in facemenu-unlisted-faces
848 ((let ((unlisted facemenu-unlisted-faces)
849 (matched nil))
850 (while (and unlisted (not matched))
851 (if (and (stringp (car unlisted))
852 (string-match (car unlisted) name))
853 (setq matched t)
854 (setq unlisted (cdr unlisted))))
855 matched))
856 (key ; has a keyboard equivalent. These go at the front.
857 (setq function (intern (concat "facemenu-set-" name)))
858 (fset function
859 `(lambda ()
860 ,docstring
861 (interactive)
862 (facemenu-set-face (quote ,symbol))))
863 (define-key 'facemenu-keymap key (cons name function))
864 (define-key menu key (cons name function)))
865 ((facemenu-iterate ; check if equivalent face is already in the menu
866 (lambda (m) (and (listp m)
867 (symbolp (car m))
868 (face-equal (car m) symbol)))
869 (cdr (symbol-function menu))))
870 (t ; No keyboard equivalent. Figure out where to put it:
871 (setq key (vector symbol)
872 function 'facemenu-set-face-from-menu
873 menu-val (symbol-function menu))
874 (if (and facemenu-new-faces-at-end
875 (> (length menu-val) 3))
876 (define-key-after menu-val key (cons name function)
877 (car (nth (- (length menu-val) 3) menu-val)))
878 (define-key menu key (cons name function))))))
879 nil) ; Return nil for facemenu-iterate
880
881 (defun facemenu-complete-face-list (&optional oldlist)
882 "Return list of all faces that look different.
883 Starts with given ALIST of faces, and adds elements only if they display
884 differently from any face already on the list.
885 The faces on ALIST will end up at the end of the returned list, in reverse
886 order."
887 (let ((list (nreverse (mapcar 'car oldlist))))
888 (facemenu-iterate
889 (lambda (new-face)
890 (if (not (memq new-face list))
891 (setq list (cons new-face list)))
892 nil)
893 (nreverse (face-list)))
894 list))
895
896 (defun facemenu-iterate (func list)
897 "Apply FUNC to each element of LIST until one returns non-nil.
898 Returns the non-nil value it found, or nil if all were nil."
899 (while (and list (not (funcall func (car list))))
900 (setq list (cdr list)))
901 (car list))
902
903 (facemenu-update)
904
905 ;;; facemenu.el ends here