(customize-face, customize-face-other-window): Handle face aliases.
[bpt/emacs.git] / lisp / cus-edit.el
1 ;;; cus-edit.el --- tools for customizing Emacs and Lisp packages
2 ;;
3 ;; Copyright (C) 1996, 1997, 1999, 2000, 2001, 2002, 2003, 2004, 2005
4 ;; Free Software Foundation, Inc.
5 ;;
6 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
7 ;; Maintainer: FSF
8 ;; Keywords: help, faces
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Commentary:
28 ;;
29 ;; This file implements the code to create and edit customize buffers.
30 ;;
31 ;; See `custom.el'.
32
33 ;; No commands should have names starting with `custom-' because
34 ;; that interferes with completion. Use `customize-' for commands
35 ;; that the user will run with M-x, and `Custom-' for interactive commands.
36
37 ;; The identity of a customize option is represented by a Lisp symbol.
38 ;; The following values are associated with an option.
39
40 ;; 0. The current value.
41
42 ;; This is the value of the option as seen by "the rest of Emacs".
43
44 ;; Usually extracted by 'default-value', but can be extracted with
45 ;; different means if the option symbol has the 'custom-get'
46 ;; property. Similarly, set-default (or the 'custom-set' property)
47 ;; can set it.
48
49 ;; 1. The widget value.
50
51 ;; This is the value shown in the widget in a customize buffer.
52
53 ;; 2. The customized value.
54
55 ;; This is the last value given to the option through customize.
56
57 ;; It is stored in the 'customized-value' property of the option, in a
58 ;; cons-cell whose car evaluates to the customized value.
59
60 ;; 3. The saved value.
61
62 ;; This is last value saved from customize.
63
64 ;; It is stored in the 'saved-value' property of the option, in a
65 ;; cons-cell whose car evaluates to the saved value.
66
67 ;; 4. The standard value.
68
69 ;; This is the value given in the 'defcustom' declaration.
70
71 ;; It is stored in the 'standard-value' property of the option, in a
72 ;; cons-cell whose car evaluates to the standard value.
73
74 ;; 5. The "think" value.
75
76 ;; This is what customize thinks the current value should be.
77
78 ;; This is the customized value, if any such value exists, otherwise
79 ;; the saved value, if that exists, and as a last resort the standard
80 ;; value.
81
82 ;; The reason for storing values unevaluated: This is so you can have
83 ;; values that depend on the environment. For example, you can have a
84 ;; variable that has one value when Emacs is running under a window
85 ;; system, and another value on a tty. Since the evaluation is only done
86 ;; when the variable is first initialized, this is only relevant for the
87 ;; saved (and standard) values, but affect others values for
88 ;; compatibility.
89
90 ;; You can see (and modify and save) this unevaluated value by selecting
91 ;; "Show initial Lisp expression" from the Lisp interface. This will
92 ;; give you the unevaluated saved value, if any, otherwise the
93 ;; unevaluated standard value.
94
95 ;; The possible states for a customize widget are:
96
97 ;; 0. unknown
98
99 ;; The state has not been determined yet.
100
101 ;; 1. modified
102
103 ;; The widget value is different from the current value.
104
105 ;; 2. changed
106
107 ;; The current value is different from the "think" value.
108
109 ;; 3. set
110
111 ;; The "think" value is the customized value.
112
113 ;; 4. saved
114
115 ;; The "think" value is the saved value.
116
117 ;; 5. standard
118
119 ;; The "think" value is the standard value.
120
121 ;; 6. rogue
122
123 ;; There is no standard value. This means that the variable was
124 ;; not defined with defcustom, nor handled in cus-start.el. You
125 ;; can not create a Custom buffer for such variables using the
126 ;; normal interactive Custom commands. However, such Custom
127 ;; buffers can be created in other ways, for instance, by calling
128 ;; `customize-option' non-interactively.
129
130 ;; 7. hidden
131
132 ;; There is no widget value.
133
134 ;; 8. mismatch
135
136 ;; The widget value is not valid member of the :type specified for the
137 ;; option.
138
139 ;;; Code:
140
141 (require 'cus-face)
142 (require 'wid-edit)
143 (eval-when-compile
144 (defvar custom-versions-load-alist)) ; from cus-load
145
146 (condition-case nil
147 (require 'cus-load)
148 (error nil))
149
150 (condition-case nil
151 (require 'cus-start)
152 (error nil))
153
154 (put 'custom-define-hook 'custom-type 'hook)
155 (put 'custom-define-hook 'standard-value '(nil))
156 (custom-add-to-group 'customize 'custom-define-hook 'custom-variable)
157
158 ;;; Customization Groups.
159
160 (defgroup emacs nil
161 "Customization of the One True Editor."
162 :link '(custom-manual "(emacs)Top"))
163
164 ;; Most of these groups are stolen from `finder.el',
165 (defgroup editing nil
166 "Basic text editing facilities."
167 :group 'emacs)
168
169 (defgroup abbrev nil
170 "Abbreviation handling, typing shortcuts, macros."
171 :tag "Abbreviations"
172 :group 'editing)
173
174 (defgroup matching nil
175 "Various sorts of searching and matching."
176 :group 'editing)
177
178 (defgroup emulations nil
179 "Emulations of other editors."
180 :link '(custom-manual "(emacs)Emulation")
181 :group 'editing)
182
183 (defgroup mouse nil
184 "Mouse support."
185 :group 'editing)
186
187 (defgroup outlines nil
188 "Support for hierarchical outlining."
189 :group 'editing)
190
191 (defgroup external nil
192 "Interfacing to external utilities."
193 :group 'emacs)
194
195 (defgroup processes nil
196 "Process, subshell, compilation, and job control support."
197 :group 'external
198 :group 'development)
199
200 (defgroup convenience nil
201 "Convenience features for faster editing."
202 :group 'emacs)
203
204 (defgroup programming nil
205 "Support for programming in other languages."
206 :group 'emacs)
207
208 (defgroup languages nil
209 "Specialized modes for editing programming languages."
210 :group 'programming)
211
212 (defgroup lisp nil
213 "Lisp support, including Emacs Lisp."
214 :group 'languages
215 :group 'development)
216
217 (defgroup c nil
218 "Support for the C language and related languages."
219 :link '(custom-manual "(ccmode)")
220 :group 'languages)
221
222 (defgroup tools nil
223 "Programming tools."
224 :group 'programming)
225
226 (defgroup oop nil
227 "Support for object-oriented programming."
228 :group 'programming)
229
230 (defgroup applications nil
231 "Applications written in Emacs."
232 :group 'emacs)
233
234 (defgroup calendar nil
235 "Calendar and time management support."
236 :group 'applications)
237
238 (defgroup mail nil
239 "Modes for electronic-mail handling."
240 :group 'applications)
241
242 (defgroup news nil
243 "Support for netnews reading and posting."
244 :link '(custom-manual "(gnus)")
245 :group 'applications)
246
247 (defgroup games nil
248 "Games, jokes and amusements."
249 :group 'applications)
250
251 (defgroup development nil
252 "Support for further development of Emacs."
253 :group 'emacs)
254
255 (defgroup docs nil
256 "Support for Emacs documentation."
257 :group 'development)
258
259 (defgroup extensions nil
260 "Emacs Lisp language extensions."
261 :group 'development)
262
263 (defgroup internal nil
264 "Code for Emacs internals, build process, defaults."
265 :group 'development)
266
267 (defgroup maint nil
268 "Maintenance aids for the Emacs development group."
269 :tag "Maintenance"
270 :group 'development)
271
272 (defgroup environment nil
273 "Fitting Emacs with its environment."
274 :group 'emacs)
275
276 (defgroup comm nil
277 "Communications, networking, remote access to files."
278 :tag "Communication"
279 :group 'environment)
280
281 (defgroup hardware nil
282 "Support for interfacing with exotic hardware."
283 :group 'environment)
284
285 (defgroup terminals nil
286 "Support for terminal types."
287 :group 'environment)
288
289 (defgroup unix nil
290 "Front-ends/assistants for, or emulators of, UNIX features."
291 :group 'environment)
292
293 (defgroup vms nil
294 "Support code for vms."
295 :group 'environment)
296
297 (defgroup i18n nil
298 "Internationalization and alternate character-set support."
299 :link '(custom-manual "(emacs)International")
300 :group 'environment
301 :group 'editing)
302
303 (defgroup x nil
304 "The X Window system."
305 :group 'environment)
306
307 (defgroup frames nil
308 "Support for Emacs frames and window systems."
309 :group 'environment)
310
311 (defgroup data nil
312 "Support editing files of data."
313 :group 'emacs)
314
315 (defgroup files nil
316 "Support editing files."
317 :group 'emacs)
318
319 (defgroup wp nil
320 "Word processing."
321 :group 'emacs)
322
323 (defgroup tex nil
324 "Code related to the TeX formatter."
325 :group 'wp)
326
327 (defgroup faces nil
328 "Support for multiple fonts."
329 :group 'emacs)
330
331 (defgroup hypermedia nil
332 "Support for links between text or other media types."
333 :group 'emacs)
334
335 (defgroup help nil
336 "Support for on-line help systems."
337 :group 'emacs)
338
339 (defgroup multimedia nil
340 "Non-textual support, specifically images and sound."
341 :group 'emacs)
342
343 (defgroup local nil
344 "Code local to your site."
345 :group 'emacs)
346
347 (defgroup customize '((widgets custom-group))
348 "Customization of the Customization support."
349 :prefix "custom-"
350 :group 'help)
351
352 (defgroup custom-faces nil
353 "Faces used by customize."
354 :group 'customize
355 :group 'faces)
356
357 (defgroup custom-browse nil
358 "Control customize browser."
359 :prefix "custom-"
360 :group 'customize)
361
362 (defgroup custom-buffer nil
363 "Control customize buffers."
364 :prefix "custom-"
365 :group 'customize)
366
367 (defgroup custom-menu nil
368 "Control customize menus."
369 :prefix "custom-"
370 :group 'customize)
371
372 (defgroup abbrev-mode nil
373 "Word abbreviations mode."
374 :link '(custom-manual "(emacs)Abbrevs")
375 :group 'abbrev)
376
377 (defgroup alloc nil
378 "Storage allocation and gc for GNU Emacs Lisp interpreter."
379 :tag "Storage Allocation"
380 :group 'internal)
381
382 (defgroup undo nil
383 "Undoing changes in buffers."
384 :link '(custom-manual "(emacs)Undo")
385 :group 'editing)
386
387 (defgroup modeline nil
388 "Content of the modeline."
389 :group 'environment)
390
391 (defgroup editing-basics nil
392 "Most basic editing facilities."
393 :group 'editing)
394
395 (defgroup display nil
396 "How characters are displayed in buffers."
397 :group 'environment)
398
399 (defgroup execute nil
400 "Executing external commands."
401 :group 'processes)
402
403 (defgroup installation nil
404 "The Emacs installation."
405 :group 'environment)
406
407 (defgroup dired nil
408 "Directory editing."
409 :group 'environment)
410
411 (defgroup limits nil
412 "Internal Emacs limits."
413 :group 'internal)
414
415 (defgroup debug nil
416 "Debugging Emacs itself."
417 :group 'development)
418
419 (defgroup minibuffer nil
420 "Controling the behavior of the minibuffer."
421 :link '(custom-manual "(emacs)Minibuffer")
422 :group 'environment)
423
424 (defgroup keyboard nil
425 "Input from the keyboard."
426 :group 'environment)
427
428 (defgroup mouse nil
429 "Input from the mouse."
430 :group 'environment)
431
432 (defgroup menu nil
433 "Input from the menus."
434 :group 'environment)
435
436 (defgroup dnd nil
437 "Handling data from drag and drop."
438 :group 'environment)
439
440 (defgroup auto-save nil
441 "Preventing accidential loss of data."
442 :group 'files)
443
444 (defgroup processes-basics nil
445 "Basic stuff dealing with processes."
446 :group 'processes)
447
448 (defgroup mule nil
449 "MULE Emacs internationalization."
450 :group 'i18n)
451
452 (defgroup windows nil
453 "Windows within a frame."
454 :link '(custom-manual "(emacs)Windows")
455 :group 'environment)
456
457 ;;; Utilities.
458
459 (defun custom-quote (sexp)
460 "Quote SEXP iff it is not self quoting."
461 (if (or (memq sexp '(t nil))
462 (keywordp sexp)
463 (and (listp sexp)
464 (memq (car sexp) '(lambda)))
465 (stringp sexp)
466 (numberp sexp)
467 (vectorp sexp)
468 ;;; (and (fboundp 'characterp)
469 ;;; (characterp sexp))
470 )
471 sexp
472 (list 'quote sexp)))
473
474 (defun custom-split-regexp-maybe (regexp)
475 "If REGEXP is a string, split it to a list at `\\|'.
476 You can get the original back with from the result with:
477 (mapconcat 'identity result \"\\|\")
478
479 IF REGEXP is not a string, return it unchanged."
480 (if (stringp regexp)
481 (let ((start 0)
482 all)
483 (while (string-match "\\\\|" regexp start)
484 (setq all (cons (substring regexp start (match-beginning 0)) all)
485 start (match-end 0)))
486 (nreverse (cons (substring regexp start) all)))
487 regexp))
488
489 (defun custom-variable-prompt ()
490 "Prompt for a custom variable, defaulting to the variable at point.
491 Return a list suitable for use in `interactive'."
492 (let ((v (variable-at-point))
493 (enable-recursive-minibuffers t)
494 val)
495 (setq val (completing-read
496 (if (and (symbolp v) (custom-variable-p v))
497 (format "Customize option: (default %s) " v)
498 "Customize option: ")
499 obarray 'custom-variable-p t))
500 (list (if (equal val "")
501 (if (symbolp v) v nil)
502 (intern val)))))
503
504 (defun custom-menu-filter (menu widget)
505 "Convert MENU to the form used by `widget-choose'.
506 MENU should be in the same format as `custom-variable-menu'.
507 WIDGET is the widget to apply the filter entries of MENU on."
508 (let ((result nil)
509 current name action filter)
510 (while menu
511 (setq current (car menu)
512 name (nth 0 current)
513 action (nth 1 current)
514 filter (nth 2 current)
515 menu (cdr menu))
516 (if (or (null filter) (funcall filter widget))
517 (push (cons name action) result)
518 (push name result)))
519 (nreverse result)))
520
521 ;;; Unlispify.
522
523 (defvar custom-prefix-list nil
524 "List of prefixes that should be ignored by `custom-unlispify'.")
525
526 (defcustom custom-unlispify-menu-entries t
527 "Display menu entries as words instead of symbols if non nil."
528 :group 'custom-menu
529 :type 'boolean)
530
531 (defcustom custom-unlispify-remove-prefixes nil
532 "Non-nil means remove group prefixes from option names in buffer."
533 :group 'custom-menu
534 :group 'custom-buffer
535 :type 'boolean)
536
537 (defun custom-unlispify-menu-entry (symbol &optional no-suffix)
538 "Convert SYMBOL into a menu entry."
539 (cond ((not custom-unlispify-menu-entries)
540 (symbol-name symbol))
541 ((get symbol 'custom-tag)
542 (if no-suffix
543 (get symbol 'custom-tag)
544 (concat (get symbol 'custom-tag) "...")))
545 (t
546 (with-current-buffer (get-buffer-create " *Custom-Work*")
547 (erase-buffer)
548 (princ symbol (current-buffer))
549 (goto-char (point-min))
550 ;; FIXME: Boolean variables are not predicates, so they shouldn't
551 ;; end with `-p'. -stef
552 ;; (when (and (eq (get symbol 'custom-type) 'boolean)
553 ;; (re-search-forward "-p\\'" nil t))
554 ;; (replace-match "" t t)
555 ;; (goto-char (point-min)))
556 (if custom-unlispify-remove-prefixes
557 (let ((prefixes custom-prefix-list)
558 prefix)
559 (while prefixes
560 (setq prefix (car prefixes))
561 (if (search-forward prefix (+ (point) (length prefix)) t)
562 (progn
563 (setq prefixes nil)
564 (delete-region (point-min) (point)))
565 (setq prefixes (cdr prefixes))))))
566 (subst-char-in-region (point-min) (point-max) ?- ?\ t)
567 (capitalize-region (point-min) (point-max))
568 (unless no-suffix
569 (goto-char (point-max))
570 (insert "..."))
571 (buffer-string)))))
572
573 (defcustom custom-unlispify-tag-names t
574 "Display tag names as words instead of symbols if non nil."
575 :group 'custom-buffer
576 :type 'boolean)
577
578 (defun custom-unlispify-tag-name (symbol)
579 "Convert SYMBOL into a menu entry."
580 (let ((custom-unlispify-menu-entries custom-unlispify-tag-names))
581 (custom-unlispify-menu-entry symbol t)))
582
583 (defun custom-prefix-add (symbol prefixes)
584 "Add SYMBOL to list of ignored PREFIXES."
585 (cons (or (get symbol 'custom-prefix)
586 (concat (symbol-name symbol) "-"))
587 prefixes))
588
589 ;;; Guess.
590
591 (defcustom custom-guess-name-alist
592 '(("-p\\'" boolean)
593 ("-hook\\'" hook)
594 ("-face\\'" face)
595 ("-file\\'" file)
596 ("-function\\'" function)
597 ("-functions\\'" (repeat function))
598 ("-list\\'" (repeat sexp))
599 ("-alist\\'" (repeat (cons sexp sexp))))
600 "Alist of (MATCH TYPE).
601
602 MATCH should be a regexp matching the name of a symbol, and TYPE should
603 be a widget suitable for editing the value of that symbol. The TYPE
604 of the first entry where MATCH matches the name of the symbol will be
605 used.
606
607 This is used for guessing the type of variables not declared with
608 customize."
609 :type '(repeat (group (regexp :tag "Match") (sexp :tag "Type")))
610 :group 'customize)
611
612 (defcustom custom-guess-doc-alist
613 '(("\\`\\*?Non-nil " boolean))
614 "Alist of (MATCH TYPE).
615
616 MATCH should be a regexp matching a documentation string, and TYPE
617 should be a widget suitable for editing the value of a variable with
618 that documentation string. The TYPE of the first entry where MATCH
619 matches the name of the symbol will be used.
620
621 This is used for guessing the type of variables not declared with
622 customize."
623 :type '(repeat (group (regexp :tag "Match") (sexp :tag "Type")))
624 :group 'customize)
625
626 (defun custom-guess-type (symbol)
627 "Guess a widget suitable for editing the value of SYMBOL.
628 This is done by matching SYMBOL with `custom-guess-name-alist' and
629 if that fails, the doc string with `custom-guess-doc-alist'."
630 (let ((name (symbol-name symbol))
631 (names custom-guess-name-alist)
632 current found)
633 (while names
634 (setq current (car names)
635 names (cdr names))
636 (when (string-match (nth 0 current) name)
637 (setq found (nth 1 current)
638 names nil)))
639 (unless found
640 (let ((doc (documentation-property symbol 'variable-documentation))
641 (docs custom-guess-doc-alist))
642 (when doc
643 (while docs
644 (setq current (car docs)
645 docs (cdr docs))
646 (when (string-match (nth 0 current) doc)
647 (setq found (nth 1 current)
648 docs nil))))))
649 found))
650
651 ;;; Sorting.
652
653 (defcustom custom-browse-sort-alphabetically nil
654 "If non-nil, sort members of each customization group alphabetically."
655 :type 'boolean
656 :group 'custom-browse)
657
658 (defcustom custom-browse-order-groups nil
659 "If non-nil, order group members within each customization group.
660 If `first', order groups before non-groups.
661 If `last', order groups after non-groups."
662 :type '(choice (const first)
663 (const last)
664 (const :tag "none" nil))
665 :group 'custom-browse)
666
667 (defcustom custom-browse-only-groups nil
668 "If non-nil, show group members only within each customization group."
669 :type 'boolean
670 :group 'custom-browse)
671
672 (defcustom custom-buffer-sort-alphabetically nil
673 "If non-nil, sort members of each customization group alphabetically."
674 :type 'boolean
675 :group 'custom-buffer)
676
677 (defcustom custom-buffer-order-groups 'last
678 "If non-nil, order group members within each customization group.
679 If `first', order groups before non-groups.
680 If `last', order groups after non-groups."
681 :type '(choice (const first)
682 (const last)
683 (const :tag "none" nil))
684 :group 'custom-buffer)
685
686 (defcustom custom-menu-sort-alphabetically nil
687 "If non-nil, sort members of each customization group alphabetically."
688 :type 'boolean
689 :group 'custom-menu)
690
691 (defcustom custom-menu-order-groups 'first
692 "If non-nil, order group members within each customization group.
693 If `first', order groups before non-groups.
694 If `last', order groups after non-groups."
695 :type '(choice (const first)
696 (const last)
697 (const :tag "none" nil))
698 :group 'custom-menu)
699
700 ;;;###autoload (add-hook 'same-window-regexps "\\`\\*Customiz.*\\*\\'")
701
702 (defun custom-sort-items (items sort-alphabetically order-groups)
703 "Return a sorted copy of ITEMS.
704 ITEMS should be a `custom-group' property.
705 If SORT-ALPHABETICALLY non-nil, sort alphabetically.
706 If ORDER-GROUPS is `first' order groups before non-groups, if `last' order
707 groups after non-groups, if nil do not order groups at all."
708 (sort (copy-sequence items)
709 (lambda (a b)
710 (let ((typea (nth 1 a)) (typeb (nth 1 b))
711 (namea (nth 0 a)) (nameb (nth 0 b)))
712 (cond ((not order-groups)
713 ;; Since we don't care about A and B order, maybe sort.
714 (when sort-alphabetically
715 (string-lessp namea nameb)))
716 ((eq typea 'custom-group)
717 ;; If B is also a group, maybe sort. Otherwise, order A and B.
718 (if (eq typeb 'custom-group)
719 (when sort-alphabetically
720 (string-lessp namea nameb))
721 (eq order-groups 'first)))
722 ((eq typeb 'custom-group)
723 ;; Since A cannot be a group, order A and B.
724 (eq order-groups 'last))
725 (sort-alphabetically
726 ;; Since A and B cannot be groups, sort.
727 (string-lessp namea nameb)))))))
728
729 ;;; Custom Mode Commands.
730
731 (defvar custom-options nil
732 "Customization widgets in the current buffer.")
733
734 (defun Custom-set ()
735 "Set changes in all modified options."
736 (interactive)
737 (let ((children custom-options))
738 (mapc (lambda (child)
739 (when (eq (widget-get child :custom-state) 'modified)
740 (widget-apply child :custom-set)))
741 children)))
742
743 (defun Custom-save ()
744 "Set all modified group members and save them."
745 (interactive)
746 (let ((children custom-options))
747 (mapc (lambda (child)
748 (when (memq (widget-get child :custom-state)
749 '(modified set changed rogue))
750 (widget-apply child :custom-save)))
751 children))
752 (custom-save-all))
753
754 (defvar custom-reset-menu
755 '(("Current" . Custom-reset-current)
756 ("Saved" . Custom-reset-saved)
757 ("Erase Customization (use standard settings)" . Custom-reset-standard))
758 "Alist of actions for the `Reset' button.
759 The key is a string containing the name of the action, the value is a
760 Lisp function taking the widget as an element which will be called
761 when the action is chosen.")
762
763 (defun custom-reset (event)
764 "Select item from reset menu."
765 (let* ((completion-ignore-case t)
766 (answer (widget-choose "Reset to"
767 custom-reset-menu
768 event)))
769 (if answer
770 (funcall answer))))
771
772 (defun Custom-reset-current (&rest ignore)
773 "Reset all modified group members to their current value."
774 (interactive)
775 (let ((children custom-options))
776 (mapc (lambda (widget)
777 (if (memq (widget-get widget :custom-state)
778 '(modified changed))
779 (widget-apply widget :custom-reset-current)))
780 children)))
781
782 (defun Custom-reset-saved (&rest ignore)
783 "Reset all modified or set group members to their saved value."
784 (interactive)
785 (let ((children custom-options))
786 (mapc (lambda (widget)
787 (if (memq (widget-get widget :custom-state)
788 '(modified set changed rogue))
789 (widget-apply widget :custom-reset-saved)))
790 children)))
791
792 (defun Custom-reset-standard (&rest ignore)
793 "Erase all customization (either current or saved) for the group members.
794 The immediate result is to restore them to their standard settings.
795 This operation eliminates any saved settings for the group members,
796 making them as if they had never been customized at all."
797 (interactive)
798 (let ((children custom-options))
799 (mapc (lambda (widget)
800 (and (widget-apply widget :custom-standard-value)
801 (if (memq (widget-get widget :custom-state)
802 '(modified set changed saved rogue))
803 (widget-apply widget :custom-reset-standard))))
804 children)))
805
806 ;;; The Customize Commands
807
808 (defun custom-prompt-variable (prompt-var prompt-val &optional comment)
809 "Prompt for a variable and a value and return them as a list.
810 PROMPT-VAR is the prompt for the variable, and PROMPT-VAL is the
811 prompt for the value. The %s escape in PROMPT-VAL is replaced with
812 the name of the variable.
813
814 If the variable has a `variable-interactive' property, that is used as if
815 it were the arg to `interactive' (which see) to interactively read the value.
816
817 If the variable has a `custom-type' property, it must be a widget and the
818 `:prompt-value' property of that widget will be used for reading the value.
819
820 If optional COMMENT argument is non nil, also prompt for a comment and return
821 it as the third element in the list."
822 (let* ((var (read-variable prompt-var))
823 (minibuffer-help-form '(describe-variable var))
824 (val
825 (let ((prop (get var 'variable-interactive))
826 (type (get var 'custom-type))
827 (prompt (format prompt-val var)))
828 (unless (listp type)
829 (setq type (list type)))
830 (cond (prop
831 ;; Use VAR's `variable-interactive' property
832 ;; as an interactive spec for prompting.
833 (call-interactively (list 'lambda '(arg)
834 (list 'interactive prop)
835 'arg)))
836 (type
837 (widget-prompt-value type
838 prompt
839 (if (boundp var)
840 (symbol-value var))
841 (not (boundp var))))
842 (t
843 (eval-minibuffer prompt))))))
844 (if comment
845 (list var val
846 (read-string "Comment: " (get var 'variable-comment)))
847 (list var val))))
848
849 ;;;###autoload
850 (defun customize-set-value (variable value &optional comment)
851 "Set VARIABLE to VALUE, and return VALUE. VALUE is a Lisp object.
852
853 If VARIABLE has a `variable-interactive' property, that is used as if
854 it were the arg to `interactive' (which see) to interactively read the value.
855
856 If VARIABLE has a `custom-type' property, it must be a widget and the
857 `:prompt-value' property of that widget will be used for reading the value.
858
859 If given a prefix (or a COMMENT argument), also prompt for a comment."
860 (interactive (custom-prompt-variable "Set variable: "
861 "Set %s to value: "
862 current-prefix-arg))
863
864 (cond ((string= comment "")
865 (put variable 'variable-comment nil))
866 (comment
867 (put variable 'variable-comment comment)))
868 (set variable value))
869
870 ;;;###autoload
871 (defun customize-set-variable (variable value &optional comment)
872 "Set the default for VARIABLE to VALUE, and return VALUE.
873 VALUE is a Lisp object.
874
875 If VARIABLE has a `custom-set' property, that is used for setting
876 VARIABLE, otherwise `set-default' is used.
877
878 The `customized-value' property of the VARIABLE will be set to a list
879 with a quoted VALUE as its sole list member.
880
881 If VARIABLE has a `variable-interactive' property, that is used as if
882 it were the arg to `interactive' (which see) to interactively read the value.
883
884 If VARIABLE has a `custom-type' property, it must be a widget and the
885 `:prompt-value' property of that widget will be used for reading the value.
886
887 If given a prefix (or a COMMENT argument), also prompt for a comment."
888 (interactive (custom-prompt-variable "Set variable: "
889 "Set customized value for %s to: "
890 current-prefix-arg))
891 (custom-load-symbol variable)
892 (funcall (or (get variable 'custom-set) 'set-default) variable value)
893 (put variable 'customized-value (list (custom-quote value)))
894 (cond ((string= comment "")
895 (put variable 'variable-comment nil)
896 (put variable 'customized-variable-comment nil))
897 (comment
898 (put variable 'variable-comment comment)
899 (put variable 'customized-variable-comment comment)))
900 value)
901
902 ;;;###autoload
903 (defun customize-save-variable (variable value &optional comment)
904 "Set the default for VARIABLE to VALUE, and save it for future sessions.
905 Return VALUE.
906
907 If VARIABLE has a `custom-set' property, that is used for setting
908 VARIABLE, otherwise `set-default' is used.
909
910 The `customized-value' property of the VARIABLE will be set to a list
911 with a quoted VALUE as its sole list member.
912
913 If VARIABLE has a `variable-interactive' property, that is used as if
914 it were the arg to `interactive' (which see) to interactively read the value.
915
916 If VARIABLE has a `custom-type' property, it must be a widget and the
917 `:prompt-value' property of that widget will be used for reading the value.
918
919 If given a prefix (or a COMMENT argument), also prompt for a comment."
920 (interactive (custom-prompt-variable "Set and save variable: "
921 "Set and save value for %s as: "
922 current-prefix-arg))
923 (funcall (or (get variable 'custom-set) 'set-default) variable value)
924 (put variable 'saved-value (list (custom-quote value)))
925 (custom-push-theme 'theme-value variable 'user 'set (list (custom-quote value)))
926 (cond ((string= comment "")
927 (put variable 'variable-comment nil)
928 (put variable 'saved-variable-comment nil))
929 (comment
930 (put variable 'variable-comment comment)
931 (put variable 'saved-variable-comment comment)))
932 (custom-save-all)
933 value)
934
935 ;;;###autoload
936 (defun customize ()
937 "Select a customization buffer which you can use to set user options.
938 User options are structured into \"groups\".
939 Initially the top-level group `Emacs' and its immediate subgroups
940 are shown; the contents of those subgroups are initially hidden."
941 (interactive)
942 (customize-group 'emacs))
943
944 ;;;###autoload
945 (defun customize-mode (mode)
946 "Customize options related to the current major mode.
947 If a prefix \\[universal-argument] was given (or if the current major mode has no known group),
948 then prompt for the MODE to customize."
949 (interactive
950 (list
951 (let ((completion-regexp-list '("-mode\\'"))
952 (group (custom-group-of-mode major-mode)))
953 (if (and group (not current-prefix-arg))
954 major-mode
955 (intern
956 (completing-read (if group
957 (format "Major mode (default %s): " major-mode)
958 "Major mode: ")
959 obarray
960 'custom-group-of-mode
961 t nil nil (if group (symbol-name major-mode))))))))
962 (customize-group (custom-group-of-mode mode)))
963
964
965 ;;;###autoload
966 (defun customize-group (group)
967 "Customize GROUP, which must be a customization group."
968 (interactive (list (let ((completion-ignore-case t))
969 (completing-read "Customize group: (default emacs) "
970 obarray
971 (lambda (symbol)
972 (or (get symbol 'custom-loads)
973 (get symbol 'custom-group)))
974 t))))
975 (when (stringp group)
976 (if (string-equal "" group)
977 (setq group 'emacs)
978 (setq group (intern group))))
979 (let ((name (format "*Customize Group: %s*"
980 (custom-unlispify-tag-name group))))
981 (if (get-buffer name)
982 (pop-to-buffer name)
983 (custom-buffer-create (list (list group 'custom-group))
984 name
985 (concat " for group "
986 (custom-unlispify-tag-name group))))))
987
988 ;;;###autoload
989 (defun customize-group-other-window (group)
990 "Customize GROUP, which must be a customization group."
991 (interactive (list (let ((completion-ignore-case t))
992 (completing-read "Customize group: (default emacs) "
993 obarray
994 (lambda (symbol)
995 (or (get symbol 'custom-loads)
996 (get symbol 'custom-group)))
997 t))))
998 (when (stringp group)
999 (if (string-equal "" group)
1000 (setq group 'emacs)
1001 (setq group (intern group))))
1002 (let ((name (format "*Customize Group: %s*"
1003 (custom-unlispify-tag-name group))))
1004 (if (get-buffer name)
1005 (let (
1006 ;; Copied from `custom-buffer-create-other-window'.
1007 (pop-up-windows t)
1008 (same-window-buffer-names nil)
1009 (same-window-regexps nil))
1010 (pop-to-buffer name))
1011 (custom-buffer-create-other-window
1012 (list (list group 'custom-group))
1013 name
1014 (concat " for group "
1015 (custom-unlispify-tag-name group))))))
1016
1017 ;;;###autoload
1018 (defalias 'customize-variable 'customize-option)
1019
1020 ;;;###autoload
1021 (defun customize-option (symbol)
1022 "Customize SYMBOL, which must be a user option variable."
1023 (interactive (custom-variable-prompt))
1024 (custom-buffer-create (list (list symbol 'custom-variable))
1025 (format "*Customize Option: %s*"
1026 (custom-unlispify-tag-name symbol))))
1027
1028 ;;;###autoload
1029 (defalias 'customize-variable-other-window 'customize-option-other-window)
1030
1031 ;;;###autoload
1032 (defun customize-option-other-window (symbol)
1033 "Customize SYMBOL, which must be a user option variable.
1034 Show the buffer in another window, but don't select it."
1035 (interactive (custom-variable-prompt))
1036 (custom-buffer-create-other-window
1037 (list (list symbol 'custom-variable))
1038 (format "*Customize Option: %s*" (custom-unlispify-tag-name symbol))))
1039
1040 (defvar customize-changed-options-previous-release "20.2"
1041 "Version for `customize-changed-options' to refer back to by default.")
1042
1043 ;;;###autoload
1044 (defun customize-changed-options (since-version)
1045 "Customize all user option variables changed in Emacs itself.
1046 This includes new user option variables and faces, and new
1047 customization groups, as well as older options and faces whose default
1048 values have changed since the previous major Emacs release.
1049
1050 With argument SINCE-VERSION (a string), customize all user option
1051 variables that were added (or their meanings were changed) since that
1052 version."
1053
1054 (interactive "sCustomize options changed, since version (default all versions): ")
1055 (if (equal since-version "")
1056 (setq since-version nil)
1057 (unless (condition-case nil
1058 (numberp (read since-version))
1059 (error nil))
1060 (signal 'wrong-type-argument (list 'numberp since-version))))
1061 (unless since-version
1062 (setq since-version customize-changed-options-previous-release))
1063
1064 ;; Load the information for versions since since-version. We use
1065 ;; custom-load-symbol for this.
1066 (put 'custom-versions-load-alist 'custom-loads nil)
1067 (dolist (elt custom-versions-load-alist)
1068 (if (customize-version-lessp since-version (car elt))
1069 (dolist (load (cdr elt))
1070 (custom-add-load 'custom-versions-load-alist load))))
1071 (custom-load-symbol 'custom-versions-load-alist)
1072 (put 'custom-versions-load-alist 'custom-loads nil)
1073
1074 (let (found)
1075 (mapatoms
1076 (lambda (symbol)
1077 (let ((version (get symbol 'custom-version)))
1078 (if version
1079 (when (customize-version-lessp since-version version)
1080 (if (or (get symbol 'custom-group)
1081 (get symbol 'group-documentation))
1082 (push (list symbol 'custom-group) found))
1083 (if (custom-variable-p symbol)
1084 (push (list symbol 'custom-variable) found))
1085 (if (custom-facep symbol)
1086 (push (list symbol 'custom-face) found)))))))
1087 (if found
1088 (custom-buffer-create (custom-sort-items found t 'first)
1089 "*Customize Changed Options*")
1090 (error "No user option defaults have been changed since Emacs %s"
1091 since-version))))
1092
1093 (defun customize-version-lessp (version1 version2)
1094 ;; Why are the versions strings, and given that they are, why aren't
1095 ;; they converted to numbers and compared as such here? -- fx
1096
1097 ;; In case someone made a mistake and left out the quotes
1098 ;; in the :version value.
1099 (if (numberp version2)
1100 (setq version2 (prin1-to-string version2)))
1101 (let (major1 major2 minor1 minor2)
1102 (string-match "\\([0-9]+\\)\\(\\.\\([0-9]+\\)\\)?" version1)
1103 (setq major1 (read (or (match-string 1 version1)
1104 "0")))
1105 (setq minor1 (read (or (match-string 3 version1)
1106 "0")))
1107 (string-match "\\([0-9]+\\)\\(\\.\\([0-9]+\\)\\)?" version2)
1108 (setq major2 (read (or (match-string 1 version2)
1109 "0")))
1110 (setq minor2 (read (or (match-string 3 version2)
1111 "0")))
1112 (or (< major1 major2)
1113 (and (= major1 major2)
1114 (< minor1 minor2)))))
1115
1116 ;;;###autoload
1117 (defun customize-face (&optional face)
1118 "Customize FACE, which should be a face name or nil.
1119 If FACE is nil, customize all faces. If FACE is actually a
1120 face-alias, customize the face it is aliased to.
1121
1122 Interactively, when point is on text which has a face specified,
1123 suggest to customize that face, if it's customizable."
1124 (interactive
1125 (list (read-face-name "Customize face" "all faces" t)))
1126 (if (member face '(nil ""))
1127 (setq face (face-list)))
1128 (if (and (listp face) (null (cdr face)))
1129 (setq face (car face)))
1130 (if (listp face)
1131 (custom-buffer-create (custom-sort-items
1132 (mapcar (lambda (s)
1133 (list s 'custom-face))
1134 face)
1135 t nil)
1136 "*Customize Faces*")
1137 ;; If FACE is actually an alias, customize the face it is aliased to.
1138 (if (get face 'face-alias)
1139 (setq face (get face 'face-alias)))
1140 (unless (facep face)
1141 (error "Invalid face %S" face))
1142 (custom-buffer-create (list (list face 'custom-face))
1143 (format "*Customize Face: %s*"
1144 (custom-unlispify-tag-name face)))))
1145
1146 ;;;###autoload
1147 (defun customize-face-other-window (&optional face)
1148 "Show customization buffer for face FACE in other window.
1149 If FACE is actually a face-alias, customize the face it is aliased to.
1150
1151 Interactively, when point is on text which has a face specified,
1152 suggest to customize that face, if it's customizable."
1153 (interactive
1154 (list (read-face-name "Customize face" "all faces" t)))
1155 (if (member face '(nil ""))
1156 (setq face (face-list)))
1157 (if (and (listp face) (null (cdr face)))
1158 (setq face (car face)))
1159 (if (listp face)
1160 (custom-buffer-create-other-window
1161 (custom-sort-items
1162 (mapcar (lambda (s)
1163 (list s 'custom-face))
1164 face)
1165 t nil)
1166 "*Customize Faces*")
1167 (if (get face 'face-alias)
1168 (setq face (get face 'face-alias)))
1169 (unless (facep face)
1170 (error "Invalid face %S" face))
1171 (custom-buffer-create-other-window
1172 (list (list face 'custom-face))
1173 (format "*Customize Face: %s*"
1174 (custom-unlispify-tag-name face)))))
1175
1176 ;;;###autoload
1177 (defun customize-customized ()
1178 "Customize all user options set since the last save in this session."
1179 (interactive)
1180 (let ((found nil))
1181 (mapatoms (lambda (symbol)
1182 (and (or (get symbol 'customized-face)
1183 (get symbol 'customized-face-comment))
1184 (custom-facep symbol)
1185 (push (list symbol 'custom-face) found))
1186 (and (or (get symbol 'customized-value)
1187 (get symbol 'customized-variable-comment))
1188 (boundp symbol)
1189 (push (list symbol 'custom-variable) found))))
1190 (if (not found)
1191 (error "No customized user options")
1192 (custom-buffer-create (custom-sort-items found t nil)
1193 "*Customize Customized*"))))
1194
1195 ;;;###autoload
1196 (defun customize-rogue ()
1197 "Customize all user variable modified outside customize."
1198 (interactive)
1199 (let ((found nil))
1200 (mapatoms (lambda (symbol)
1201 (let ((cval (or (get symbol 'customized-value)
1202 (get symbol 'saved-value)
1203 (get symbol 'standard-value))))
1204 (when (and cval ;Declared with defcustom.
1205 (default-boundp symbol) ;Has a value.
1206 (not (equal (eval (car cval))
1207 ;; Which does not match customize.
1208 (default-value symbol))))
1209 (push (list symbol 'custom-variable) found)))))
1210 (if (not found)
1211 (error "No rogue user options")
1212 (custom-buffer-create (custom-sort-items found t nil)
1213 "*Customize Rogue*"))))
1214 ;;;###autoload
1215 (defun customize-saved ()
1216 "Customize all already saved user options."
1217 (interactive)
1218 (let ((found nil))
1219 (mapatoms (lambda (symbol)
1220 (and (or (get symbol 'saved-face)
1221 (get symbol 'saved-face-comment))
1222 (custom-facep symbol)
1223 (push (list symbol 'custom-face) found))
1224 (and (or (get symbol 'saved-value)
1225 (get symbol 'saved-variable-comment))
1226 (boundp symbol)
1227 (push (list symbol 'custom-variable) found))))
1228 (if (not found )
1229 (error "No saved user options")
1230 (custom-buffer-create (custom-sort-items found t nil)
1231 "*Customize Saved*"))))
1232
1233 ;;;###autoload
1234 (defun customize-apropos (regexp &optional all)
1235 "Customize all user options matching REGEXP.
1236 If ALL is `options', include only options.
1237 If ALL is `faces', include only faces.
1238 If ALL is `groups', include only groups.
1239 If ALL is t (interactively, with prefix arg), include options which are not
1240 user-settable, as well as faces and groups."
1241 (interactive "sCustomize regexp: \nP")
1242 (let ((found nil))
1243 (mapatoms (lambda (symbol)
1244 (when (string-match regexp (symbol-name symbol))
1245 (when (and (not (memq all '(faces options)))
1246 (get symbol 'custom-group))
1247 (push (list symbol 'custom-group) found))
1248 (when (and (not (memq all '(options groups)))
1249 (custom-facep symbol))
1250 (push (list symbol 'custom-face) found))
1251 (when (and (not (memq all '(groups faces)))
1252 (boundp symbol)
1253 (or (get symbol 'saved-value)
1254 (custom-variable-p symbol)
1255 (if (memq all '(nil options))
1256 (user-variable-p symbol)
1257 (get symbol 'variable-documentation))))
1258 (push (list symbol 'custom-variable) found)))))
1259 (if (not found)
1260 (error "No matches")
1261 (custom-buffer-create (custom-sort-items found t
1262 custom-buffer-order-groups)
1263 "*Customize Apropos*"))))
1264
1265 ;;;###autoload
1266 (defun customize-apropos-options (regexp &optional arg)
1267 "Customize all user options matching REGEXP.
1268 With prefix arg, include options which are not user-settable."
1269 (interactive "sCustomize regexp: \nP")
1270 (customize-apropos regexp (or arg 'options)))
1271
1272 ;;;###autoload
1273 (defun customize-apropos-faces (regexp)
1274 "Customize all user faces matching REGEXP."
1275 (interactive "sCustomize regexp: \n")
1276 (customize-apropos regexp 'faces))
1277
1278 ;;;###autoload
1279 (defun customize-apropos-groups (regexp)
1280 "Customize all user groups matching REGEXP."
1281 (interactive "sCustomize regexp: \n")
1282 (customize-apropos regexp 'groups))
1283
1284 ;;; Buffer.
1285
1286 (defcustom custom-buffer-style 'links
1287 "Control the presentation style for customization buffers.
1288 The value should be a symbol, one of:
1289
1290 brackets: groups nest within each other with big horizontal brackets.
1291 links: groups have links to subgroups."
1292 :type '(radio (const brackets)
1293 (const links))
1294 :group 'custom-buffer)
1295
1296 (defcustom custom-buffer-done-kill nil
1297 "*Non-nil means exiting a Custom buffer should kill it."
1298 :type 'boolean
1299 :version "22.1"
1300 :group 'custom-buffer)
1301
1302 (defcustom custom-buffer-indent 3
1303 "Number of spaces to indent nested groups."
1304 :type 'integer
1305 :group 'custom-buffer)
1306
1307 (defun custom-get-fresh-buffer (name)
1308 "Get a fresh new buffer with name NAME.
1309 If the buffer already exist, clean it up to be like new.
1310 Beware: it's not quite like new. Good enough for custom, but maybe
1311 not for everybody."
1312 ;; To be more complete, we should also kill all permanent-local variables,
1313 ;; but it's not needed for custom.
1314 (let ((buf (get-buffer name)))
1315 (when (and buf (buffer-local-value 'buffer-file-name buf))
1316 ;; This will check if the file is not saved.
1317 (kill-buffer buf)
1318 (setq buf nil))
1319 (if (null buf)
1320 (get-buffer-create name)
1321 (with-current-buffer buf
1322 (kill-all-local-variables)
1323 (run-hooks 'kill-buffer-hook)
1324 ;; Delete overlays before erasing the buffer so the overlay hooks
1325 ;; don't get run spuriously when we erase the buffer.
1326 (let ((ols (overlay-lists)))
1327 (dolist (ol (nconc (car ols) (cdr ols)))
1328 (delete-overlay ol)))
1329 (erase-buffer)
1330 buf))))
1331
1332 ;;;###autoload
1333 (defun custom-buffer-create (options &optional name description)
1334 "Create a buffer containing OPTIONS.
1335 Optional NAME is the name of the buffer.
1336 OPTIONS should be an alist of the form ((SYMBOL WIDGET)...), where
1337 SYMBOL is a customization option, and WIDGET is a widget for editing
1338 that option."
1339 (pop-to-buffer (custom-get-fresh-buffer (or name "*Customization*")))
1340 (custom-buffer-create-internal options description))
1341
1342 ;;;###autoload
1343 (defun custom-buffer-create-other-window (options &optional name description)
1344 "Create a buffer containing OPTIONS, and display it in another window.
1345 The result includes selecting that window.
1346 Optional NAME is the name of the buffer.
1347 OPTIONS should be an alist of the form ((SYMBOL WIDGET)...), where
1348 SYMBOL is a customization option, and WIDGET is a widget for editing
1349 that option."
1350 (unless name (setq name "*Customization*"))
1351 (let ((pop-up-windows t)
1352 (same-window-buffer-names nil)
1353 (same-window-regexps nil))
1354 (pop-to-buffer (custom-get-fresh-buffer name))
1355 (custom-buffer-create-internal options description)))
1356
1357 (defcustom custom-reset-button-menu nil
1358 "If non-nil, only show a single reset button in customize buffers.
1359 This button will have a menu with all three reset operations."
1360 :type 'boolean
1361 :group 'custom-buffer)
1362
1363 (defcustom custom-buffer-verbose-help t
1364 "If non-nil, include explanatory text in the customization buffer."
1365 :type 'boolean
1366 :group 'custom-buffer)
1367
1368 (defun Custom-buffer-done (&rest ignore)
1369 "Exit current Custom buffer according to `custom-buffer-done-kill'."
1370 (interactive)
1371 (quit-window custom-buffer-done-kill))
1372
1373 (defcustom custom-raised-buttons (not (equal (face-valid-attribute-values :box)
1374 '(("unspecified" . unspecified))))
1375 "If non-nil, indicate active buttons in a `raised-button' style.
1376 Otherwise use brackets."
1377 :type 'boolean
1378 :version "21.1"
1379 :group 'custom-buffer)
1380
1381 (defun custom-buffer-create-internal (options &optional description)
1382 (custom-mode)
1383 (if custom-buffer-verbose-help
1384 (progn
1385 (widget-insert "This is a customization buffer")
1386 (if description
1387 (widget-insert description))
1388 (widget-insert (format ".
1389 %s show active fields; type RET or click mouse-1
1390 on an active field to invoke its action. Editing an option value
1391 changes only the text in the buffer. Invoke the State button to set or
1392 save the option value. Saving an option normally edits your init file.
1393 Invoke "
1394 (if custom-raised-buttons
1395 "`Raised' buttons"
1396 "Square brackets")))
1397 (widget-create 'info-link
1398 :tag "Custom file"
1399 "(emacs)Saving Customizations")
1400 (widget-insert
1401 " for information on how to save in a different file.
1402 Invoke ")
1403 (widget-create 'info-link
1404 :tag "Help"
1405 :help-echo "Read the online help."
1406 "(emacs)Easy Customization")
1407 (widget-insert " for general information.\n\n")
1408 (widget-insert "Operate on everything in this buffer:\n "))
1409 (widget-insert " "))
1410 (widget-create 'push-button
1411 :tag "Set for Current Session"
1412 :help-echo "\
1413 Make your editing in this buffer take effect for this session."
1414 :action (lambda (widget &optional event)
1415 (Custom-set)))
1416 (widget-insert " ")
1417 (widget-create 'push-button
1418 :tag "Save for Future Sessions"
1419 :help-echo "\
1420 Make your editing in this buffer take effect for future Emacs sessions.
1421 This updates your Emacs initialization file or creates a new one."
1422 :action (lambda (widget &optional event)
1423 (Custom-save)))
1424 (if custom-reset-button-menu
1425 (progn
1426 (widget-insert " ")
1427 (widget-create 'push-button
1428 :tag "Reset"
1429 :help-echo "Show a menu with reset operations."
1430 :mouse-down-action (lambda (&rest junk) t)
1431 :action (lambda (widget &optional event)
1432 (custom-reset event))))
1433 (widget-insert "\n ")
1434 (widget-create 'push-button
1435 :tag "Reset"
1436 :help-echo "\
1437 Reset all edited text in this buffer to reflect current values."
1438 :action 'Custom-reset-current)
1439 (widget-insert " ")
1440 (widget-create 'push-button
1441 :tag "Reset to Saved"
1442 :help-echo "\
1443 Reset all values in this buffer to their saved settings."
1444 :action 'Custom-reset-saved)
1445 (widget-insert " ")
1446 (widget-create 'push-button
1447 :tag "Erase Customization"
1448 :help-echo "\
1449 Un-customize all values in this buffer. They get their standard settings."
1450 :action 'Custom-reset-standard))
1451 (if (not custom-buffer-verbose-help)
1452 (progn
1453 (widget-insert " ")
1454 (widget-create 'info-link
1455 :tag "Help"
1456 :help-echo "Read the online help."
1457 "(emacs)Easy Customization")))
1458 (widget-insert " ")
1459 (widget-create 'push-button
1460 :tag "Finish"
1461 :help-echo
1462 (lambda (&rest ignore)
1463 (if custom-buffer-done-kill
1464 "Kill this buffer"
1465 "Bury this buffer"))
1466 :action #'Custom-buffer-done)
1467 (widget-insert "\n\n")
1468 (message "Creating customization items...")
1469 (buffer-disable-undo)
1470 (setq custom-options
1471 (if (= (length options) 1)
1472 (mapcar (lambda (entry)
1473 (widget-create (nth 1 entry)
1474 :documentation-shown t
1475 :custom-state 'unknown
1476 :tag (custom-unlispify-tag-name
1477 (nth 0 entry))
1478 :value (nth 0 entry)))
1479 options)
1480 (let ((count 0)
1481 (length (length options)))
1482 (mapcar (lambda (entry)
1483 (prog2
1484 (message "Creating customization items ...%2d%%"
1485 (/ (* 100.0 count) length))
1486 (widget-create (nth 1 entry)
1487 :tag (custom-unlispify-tag-name
1488 (nth 0 entry))
1489 :value (nth 0 entry))
1490 (setq count (1+ count))
1491 (unless (eq (preceding-char) ?\n)
1492 (widget-insert "\n"))
1493 (widget-insert "\n")))
1494 options))))
1495 (unless (eq (preceding-char) ?\n)
1496 (widget-insert "\n"))
1497 (message "Creating customization items ...done")
1498 (message "Resetting customization items...")
1499 (unless (eq custom-buffer-style 'tree)
1500 (mapc 'custom-magic-reset custom-options))
1501 (message "Resetting customization items...done")
1502 (message "Creating customization setup...")
1503 (widget-setup)
1504 (buffer-enable-undo)
1505 (goto-char (point-min))
1506 (message "Creating customization setup...done"))
1507
1508 ;;; The Tree Browser.
1509
1510 ;;;###autoload
1511 (defun customize-browse (&optional group)
1512 "Create a tree browser for the customize hierarchy."
1513 (interactive)
1514 (unless group
1515 (setq group 'emacs))
1516 (let ((name "*Customize Browser*"))
1517 (pop-to-buffer (custom-get-fresh-buffer name)))
1518 (custom-mode)
1519 (widget-insert "\
1520 Square brackets show active fields; type RET or click mouse-1
1521 on an active field to invoke its action.
1522 Invoke [+] below to expand a group, and [-] to collapse an expanded group.\n")
1523 (if custom-browse-only-groups
1524 (widget-insert "\
1525 Invoke the [Group] button below to edit that item in another window.\n\n")
1526 (widget-insert "Invoke the ")
1527 (widget-create 'item
1528 :format "%t"
1529 :tag "[Group]"
1530 :tag-glyph "folder")
1531 (widget-insert ", ")
1532 (widget-create 'item
1533 :format "%t"
1534 :tag "[Face]"
1535 :tag-glyph "face")
1536 (widget-insert ", and ")
1537 (widget-create 'item
1538 :format "%t"
1539 :tag "[Option]"
1540 :tag-glyph "option")
1541 (widget-insert " buttons below to edit that
1542 item in another window.\n\n"))
1543 (let ((custom-buffer-style 'tree))
1544 (widget-create 'custom-group
1545 :custom-last t
1546 :custom-state 'unknown
1547 :tag (custom-unlispify-tag-name group)
1548 :value group))
1549 (widget-setup)
1550 (goto-char (point-min)))
1551
1552 (define-widget 'custom-browse-visibility 'item
1553 "Control visibility of items in the customize tree browser."
1554 :format "%[[%t]%]"
1555 :action 'custom-browse-visibility-action)
1556
1557 (defun custom-browse-visibility-action (widget &rest ignore)
1558 (let ((custom-buffer-style 'tree))
1559 (custom-toggle-parent widget)))
1560
1561 (define-widget 'custom-browse-group-tag 'push-button
1562 "Show parent in other window when activated."
1563 :tag "Group"
1564 :tag-glyph "folder"
1565 :action 'custom-browse-group-tag-action)
1566
1567 (defun custom-browse-group-tag-action (widget &rest ignore)
1568 (let ((parent (widget-get widget :parent)))
1569 (customize-group-other-window (widget-value parent))))
1570
1571 (define-widget 'custom-browse-variable-tag 'push-button
1572 "Show parent in other window when activated."
1573 :tag "Option"
1574 :tag-glyph "option"
1575 :action 'custom-browse-variable-tag-action)
1576
1577 (defun custom-browse-variable-tag-action (widget &rest ignore)
1578 (let ((parent (widget-get widget :parent)))
1579 (customize-variable-other-window (widget-value parent))))
1580
1581 (define-widget 'custom-browse-face-tag 'push-button
1582 "Show parent in other window when activated."
1583 :tag "Face"
1584 :tag-glyph "face"
1585 :action 'custom-browse-face-tag-action)
1586
1587 (defun custom-browse-face-tag-action (widget &rest ignore)
1588 (let ((parent (widget-get widget :parent)))
1589 (customize-face-other-window (widget-value parent))))
1590
1591 (defconst custom-browse-alist '((" " "space")
1592 (" | " "vertical")
1593 ("-\\ " "top")
1594 (" |-" "middle")
1595 (" `-" "bottom")))
1596
1597 (defun custom-browse-insert-prefix (prefix)
1598 "Insert PREFIX. On XEmacs convert it to line graphics."
1599 ;; Fixme: do graphics.
1600 (if nil ; (string-match "XEmacs" emacs-version)
1601 (progn
1602 (insert "*")
1603 (while (not (string-equal prefix ""))
1604 (let ((entry (substring prefix 0 3)))
1605 (setq prefix (substring prefix 3))
1606 (let ((overlay (make-overlay (1- (point)) (point) nil t nil))
1607 (name (nth 1 (assoc entry custom-browse-alist))))
1608 (overlay-put overlay 'end-glyph (widget-glyph-find name entry))
1609 (overlay-put overlay 'start-open t)
1610 (overlay-put overlay 'end-open t)))))
1611 (insert prefix)))
1612
1613 ;;; Modification of Basic Widgets.
1614 ;;
1615 ;; We add extra properties to the basic widgets needed here. This is
1616 ;; fine, as long as we are careful to stay within out own namespace.
1617 ;;
1618 ;; We want simple widgets to be displayed by default, but complex
1619 ;; widgets to be hidden.
1620
1621 (widget-put (get 'item 'widget-type) :custom-show t)
1622 (widget-put (get 'editable-field 'widget-type)
1623 :custom-show (lambda (widget value)
1624 (let ((pp (pp-to-string value)))
1625 (cond ((string-match "\n" pp)
1626 nil)
1627 ((> (length pp) 40)
1628 nil)
1629 (t t)))))
1630 (widget-put (get 'menu-choice 'widget-type) :custom-show t)
1631
1632 ;;; The `custom-manual' Widget.
1633
1634 (define-widget 'custom-manual 'info-link
1635 "Link to the manual entry for this customization option."
1636 :help-echo "Read the manual entry for this option."
1637 :tag "Manual")
1638
1639 ;;; The `custom-magic' Widget.
1640
1641 (defgroup custom-magic-faces nil
1642 "Faces used by the magic button."
1643 :group 'custom-faces
1644 :group 'custom-buffer)
1645
1646 (defface custom-invalid '((((class color))
1647 (:foreground "yellow1" :background "red1"))
1648 (t
1649 (:weight bold :slant italic :underline t)))
1650 "Face used when the customize item is invalid."
1651 :group 'custom-magic-faces)
1652 ;; backward-compatibility alias
1653 (put 'custom-invalid-face 'face-alias 'custom-invalid)
1654
1655 (defface custom-rogue '((((class color))
1656 (:foreground "pink" :background "black"))
1657 (t
1658 (:underline t)))
1659 "Face used when the customize item is not defined for customization."
1660 :group 'custom-magic-faces)
1661 ;; backward-compatibility alias
1662 (put 'custom-rogue-face 'face-alias 'custom-rogue)
1663
1664 (defface custom-modified '((((min-colors 88) (class color))
1665 (:foreground "white" :background "blue1"))
1666 (((class color))
1667 (:foreground "white" :background "blue"))
1668 (t
1669 (:slant italic :bold)))
1670 "Face used when the customize item has been modified."
1671 :group 'custom-magic-faces)
1672 ;; backward-compatibility alias
1673 (put 'custom-modified-face 'face-alias 'custom-modified)
1674
1675 (defface custom-set '((((min-colors 88) (class color))
1676 (:foreground "blue1" :background "white"))
1677 (((class color))
1678 (:foreground "blue" :background "white"))
1679 (t
1680 (:slant italic)))
1681 "Face used when the customize item has been set."
1682 :group 'custom-magic-faces)
1683 ;; backward-compatibility alias
1684 (put 'custom-set-face 'face-alias 'custom-set)
1685
1686 (defface custom-changed '((((min-colors 88) (class color))
1687 (:foreground "white" :background "blue1"))
1688 (((class color))
1689 (:foreground "white" :background "blue"))
1690 (t
1691 (:slant italic)))
1692 "Face used when the customize item has been changed."
1693 :group 'custom-magic-faces)
1694 ;; backward-compatibility alias
1695 (put 'custom-changed-face 'face-alias 'custom-changed)
1696
1697 (defface custom-saved '((t (:underline t)))
1698 "Face used when the customize item has been saved."
1699 :group 'custom-magic-faces)
1700 ;; backward-compatibility alias
1701 (put 'custom-saved-face 'face-alias 'custom-saved)
1702
1703 (defconst custom-magic-alist
1704 '((nil "#" underline "\
1705 UNINITIALIZED, you should not see this.")
1706 (unknown "?" italic "\
1707 UNKNOWN, you should not see this.")
1708 (hidden "-" default "\
1709 HIDDEN, invoke \"Show\" in the previous line to show." "\
1710 group now hidden, invoke \"Show\", above, to show contents.")
1711 (invalid "x" custom-invalid "\
1712 INVALID, the displayed value cannot be set.")
1713 (modified "*" custom-modified "\
1714 EDITED, shown value does not take effect until you set or save it." "\
1715 something in this group has been edited but not set.")
1716 (set "+" custom-set "\
1717 SET for current session only." "\
1718 something in this group has been set but not saved.")
1719 (changed ":" custom-changed "\
1720 CHANGED outside Customize; operating on it here may be unreliable." "\
1721 something in this group has been changed outside customize.")
1722 (saved "!" custom-saved "\
1723 SAVED and set." "\
1724 something in this group has been set and saved.")
1725 (rogue "@" custom-rogue "\
1726 NO CUSTOMIZATION DATA; you should not see this." "\
1727 something in this group is not prepared for customization.")
1728 (standard " " nil "\
1729 STANDARD." "\
1730 visible group members are all at standard settings."))
1731 "Alist of customize option states.
1732 Each entry is of the form (STATE MAGIC FACE ITEM-DESC [ GROUP-DESC ]), where
1733
1734 STATE is one of the following symbols:
1735
1736 `nil'
1737 For internal use, should never occur.
1738 `unknown'
1739 For internal use, should never occur.
1740 `hidden'
1741 This item is not being displayed.
1742 `invalid'
1743 This item is modified, but has an invalid form.
1744 `modified'
1745 This item is modified, and has a valid form.
1746 `set'
1747 This item has been set but not saved.
1748 `changed'
1749 The current value of this item has been changed outside Customize.
1750 `saved'
1751 This item is marked for saving.
1752 `rogue'
1753 This item has no customization information.
1754 `standard'
1755 This item is unchanged from the standard setting.
1756
1757 MAGIC is a string used to present that state.
1758
1759 FACE is a face used to present the state.
1760
1761 ITEM-DESC is a string describing the state for options.
1762
1763 GROUP-DESC is a string describing the state for groups. If this is
1764 left out, ITEM-DESC will be used.
1765
1766 The string %c in either description will be replaced with the
1767 category of the item. These are `group'. `option', and `face'.
1768
1769 The list should be sorted most significant first.")
1770
1771 (defcustom custom-magic-show 'long
1772 "If non-nil, show textual description of the state.
1773 If `long', show a full-line description, not just one word."
1774 :type '(choice (const :tag "no" nil)
1775 (const long)
1776 (other :tag "short" short))
1777 :group 'custom-buffer)
1778
1779 (defcustom custom-magic-show-hidden '(option face)
1780 "Control whether the State button is shown for hidden items.
1781 The value should be a list with the custom categories where the State
1782 button should be visible. Possible categories are `group', `option',
1783 and `face'."
1784 :type '(set (const group) (const option) (const face))
1785 :group 'custom-buffer)
1786
1787 (defcustom custom-magic-show-button nil
1788 "Show a \"magic\" button indicating the state of each customization option."
1789 :type 'boolean
1790 :group 'custom-buffer)
1791
1792 (define-widget 'custom-magic 'default
1793 "Show and manipulate state for a customization option."
1794 :format "%v"
1795 :action 'widget-parent-action
1796 :notify 'ignore
1797 :value-get 'ignore
1798 :value-create 'custom-magic-value-create
1799 :value-delete 'widget-children-value-delete)
1800
1801 (defun widget-magic-mouse-down-action (widget &optional event)
1802 ;; Non-nil unless hidden.
1803 (not (eq (widget-get (widget-get (widget-get widget :parent) :parent)
1804 :custom-state)
1805 'hidden)))
1806
1807 (defun custom-magic-value-create (widget)
1808 "Create compact status report for WIDGET."
1809 (let* ((parent (widget-get widget :parent))
1810 (state (widget-get parent :custom-state))
1811 (hidden (eq state 'hidden))
1812 (entry (assq state custom-magic-alist))
1813 (magic (nth 1 entry))
1814 (face (nth 2 entry))
1815 (category (widget-get parent :custom-category))
1816 (text (or (and (eq category 'group)
1817 (nth 4 entry))
1818 (nth 3 entry)))
1819 (form (widget-get parent :custom-form))
1820 children)
1821 (while (string-match "\\`\\(.*\\)%c\\(.*\\)\\'" text)
1822 (setq text (concat (match-string 1 text)
1823 (symbol-name category)
1824 (match-string 2 text))))
1825 (when (and custom-magic-show
1826 (or (not hidden)
1827 (memq category custom-magic-show-hidden)))
1828 (insert " ")
1829 (when (and (eq category 'group)
1830 (not (and (eq custom-buffer-style 'links)
1831 (> (widget-get parent :custom-level) 1))))
1832 (insert-char ?\ (* custom-buffer-indent
1833 (widget-get parent :custom-level))))
1834 (push (widget-create-child-and-convert
1835 widget 'choice-item
1836 :help-echo "Change the state of this item."
1837 :format (if hidden "%t" "%[%t%]")
1838 :button-prefix 'widget-push-button-prefix
1839 :button-suffix 'widget-push-button-suffix
1840 :mouse-down-action 'widget-magic-mouse-down-action
1841 :tag "State")
1842 children)
1843 (insert ": ")
1844 (let ((start (point)))
1845 (if (eq custom-magic-show 'long)
1846 (insert text)
1847 (insert (symbol-name state)))
1848 (cond ((eq form 'lisp)
1849 (insert " (lisp)"))
1850 ((eq form 'mismatch)
1851 (insert " (mismatch)")))
1852 (put-text-property start (point) 'face 'custom-state))
1853 (insert "\n"))
1854 (when (and (eq category 'group)
1855 (not (and (eq custom-buffer-style 'links)
1856 (> (widget-get parent :custom-level) 1))))
1857 (insert-char ?\ (* custom-buffer-indent
1858 (widget-get parent :custom-level))))
1859 (when custom-magic-show-button
1860 (when custom-magic-show
1861 (let ((indent (widget-get parent :indent)))
1862 (when indent
1863 (insert-char ? indent))))
1864 (push (widget-create-child-and-convert
1865 widget 'choice-item
1866 :mouse-down-action 'widget-magic-mouse-down-action
1867 :button-face face
1868 :button-prefix ""
1869 :button-suffix ""
1870 :help-echo "Change the state."
1871 :format (if hidden "%t" "%[%t%]")
1872 :tag (if (memq form '(lisp mismatch))
1873 (concat "(" magic ")")
1874 (concat "[" magic "]")))
1875 children)
1876 (insert " "))
1877 (widget-put widget :children children)))
1878
1879 (defun custom-magic-reset (widget)
1880 "Redraw the :custom-magic property of WIDGET."
1881 (let ((magic (widget-get widget :custom-magic)))
1882 (widget-value-set magic (widget-value magic))))
1883
1884 ;;; The `custom' Widget.
1885
1886 (defface custom-button
1887 '((((type x w32 mac) (class color)) ; Like default modeline
1888 (:box (:line-width 2 :style released-button)
1889 :background "lightgrey" :foreground "black"))
1890 (t
1891 nil))
1892 "Face used for buttons in customization buffers."
1893 :version "21.1"
1894 :group 'custom-faces)
1895 ;; backward-compatibility alias
1896 (put 'custom-button-face 'face-alias 'custom-button)
1897
1898 (defface custom-button-pressed
1899 '((((type x w32 mac) (class color))
1900 (:box (:line-width 2 :style pressed-button)
1901 :background "lightgrey" :foreground "black"))
1902 (t
1903 (:inverse-video t)))
1904 "Face used for buttons in customization buffers."
1905 :version "21.1"
1906 :group 'custom-faces)
1907 ;; backward-compatibility alias
1908 (put 'custom-button-pressed-face 'face-alias 'custom-button-pressed)
1909
1910 (defface custom-documentation nil
1911 "Face used for documentation strings in customization buffers."
1912 :group 'custom-faces)
1913 ;; backward-compatibility alias
1914 (put 'custom-documentation-face 'face-alias 'custom-documentation)
1915
1916 (defface custom-state '((((class color)
1917 (background dark))
1918 (:foreground "lime green"))
1919 (((class color)
1920 (background light))
1921 (:foreground "dark green"))
1922 (t nil))
1923 "Face used for State descriptions in the customize buffer."
1924 :group 'custom-faces)
1925 ;; backward-compatibility alias
1926 (put 'custom-state-face 'face-alias 'custom-state)
1927
1928 (define-widget 'custom 'default
1929 "Customize a user option."
1930 :format "%v"
1931 :convert-widget 'custom-convert-widget
1932 :notify 'custom-notify
1933 :custom-prefix ""
1934 :custom-level 1
1935 :custom-state 'hidden
1936 :documentation-property 'widget-subclass-responsibility
1937 :value-create 'widget-subclass-responsibility
1938 :value-delete 'widget-children-value-delete
1939 :value-get 'widget-value-value-get
1940 :validate 'widget-children-validate
1941 :match (lambda (widget value) (symbolp value)))
1942
1943 (defun custom-convert-widget (widget)
1944 "Initialize :value and :tag from :args in WIDGET."
1945 (let ((args (widget-get widget :args)))
1946 (when args
1947 (widget-put widget :value (widget-apply widget
1948 :value-to-internal (car args)))
1949 (widget-put widget :tag (custom-unlispify-tag-name (car args)))
1950 (widget-put widget :args nil)))
1951 widget)
1952
1953 (defun custom-notify (widget &rest args)
1954 "Keep track of changes."
1955 (let ((state (widget-get widget :custom-state)))
1956 (unless (eq state 'modified)
1957 (unless (memq state '(nil unknown hidden))
1958 (widget-put widget :custom-state 'modified))
1959 (custom-magic-reset widget)
1960 (apply 'widget-default-notify widget args))))
1961
1962 (defun custom-redraw (widget)
1963 "Redraw WIDGET with current settings."
1964 (let ((line (count-lines (point-min) (point)))
1965 (column (current-column))
1966 (pos (point))
1967 (from (marker-position (widget-get widget :from)))
1968 (to (marker-position (widget-get widget :to))))
1969 (save-excursion
1970 (widget-value-set widget (widget-value widget))
1971 (custom-redraw-magic widget))
1972 (when (and (>= pos from) (<= pos to))
1973 (condition-case nil
1974 (progn
1975 (if (> column 0)
1976 (goto-line line)
1977 (goto-line (1+ line)))
1978 (move-to-column column))
1979 (error nil)))))
1980
1981 (defun custom-redraw-magic (widget)
1982 "Redraw WIDGET state with current settings."
1983 (while widget
1984 (let ((magic (widget-get widget :custom-magic)))
1985 (cond (magic
1986 (widget-value-set magic (widget-value magic))
1987 (when (setq widget (widget-get widget :group))
1988 (custom-group-state-update widget)))
1989 (t
1990 (setq widget nil)))))
1991 (widget-setup))
1992
1993 (defun custom-show (widget value)
1994 "Non-nil if WIDGET should be shown with VALUE by default."
1995 (let ((show (widget-get widget :custom-show)))
1996 (cond ((null show)
1997 nil)
1998 ((eq t show)
1999 t)
2000 (t
2001 (funcall show widget value)))))
2002
2003 (defun custom-load-widget (widget)
2004 "Load all dependencies for WIDGET."
2005 (custom-load-symbol (widget-value widget)))
2006
2007 (defun custom-unloaded-symbol-p (symbol)
2008 "Return non-nil if the dependencies of SYMBOL have not yet been loaded."
2009 (let ((found nil)
2010 (loads (get symbol 'custom-loads))
2011 load)
2012 (while loads
2013 (setq load (car loads)
2014 loads (cdr loads))
2015 (cond ((symbolp load)
2016 (unless (featurep load)
2017 (setq found t)))
2018 ((assoc load load-history))
2019 ((assoc (locate-library load) load-history)
2020 (message nil))
2021 (t
2022 (setq found t))))
2023 found))
2024
2025 (defun custom-unloaded-widget-p (widget)
2026 "Return non-nil if the dependencies of WIDGET have not yet been loaded."
2027 (custom-unloaded-symbol-p (widget-value widget)))
2028
2029 (defun custom-toggle-hide (widget)
2030 "Toggle visibility of WIDGET."
2031 (custom-load-widget widget)
2032 (let ((state (widget-get widget :custom-state)))
2033 (cond ((memq state '(invalid modified))
2034 (error "There are unset changes"))
2035 ((eq state 'hidden)
2036 (widget-put widget :custom-state 'unknown))
2037 (t
2038 (widget-put widget :documentation-shown nil)
2039 (widget-put widget :custom-state 'hidden)))
2040 (custom-redraw widget)
2041 (widget-setup)))
2042
2043 (defun custom-toggle-parent (widget &rest ignore)
2044 "Toggle visibility of parent of WIDGET."
2045 (custom-toggle-hide (widget-get widget :parent)))
2046
2047 (defun custom-add-see-also (widget &optional prefix)
2048 "Add `See also ...' to WIDGET if there are any links.
2049 Insert PREFIX first if non-nil."
2050 (let* ((symbol (widget-get widget :value))
2051 (links (get symbol 'custom-links))
2052 (many (> (length links) 2))
2053 (buttons (widget-get widget :buttons))
2054 (indent (widget-get widget :indent)))
2055 (when links
2056 (when indent
2057 (insert-char ?\ indent))
2058 (when prefix
2059 (insert prefix))
2060 (insert "See also ")
2061 (while links
2062 (push (widget-create-child-and-convert widget (car links))
2063 buttons)
2064 (setq links (cdr links))
2065 (cond ((null links)
2066 (insert ".\n"))
2067 ((null (cdr links))
2068 (if many
2069 (insert ", and ")
2070 (insert " and ")))
2071 (t
2072 (insert ", "))))
2073 (widget-put widget :buttons buttons))))
2074
2075 (defun custom-add-parent-links (widget &optional initial-string)
2076 "Add \"Parent groups: ...\" to WIDGET if the group has parents.
2077 The value if non-nil if any parents were found.
2078 If INITIAL-STRING is non-nil, use that rather than \"Parent groups:\"."
2079 (let ((name (widget-value widget))
2080 (type (widget-type widget))
2081 (buttons (widget-get widget :buttons))
2082 (start (point))
2083 (parents nil))
2084 (insert (or initial-string "Parent groups:"))
2085 (mapatoms (lambda (symbol)
2086 (let ((entry (assq name (get symbol 'custom-group))))
2087 (when (eq (nth 1 entry) type)
2088 (insert " ")
2089 (push (widget-create-child-and-convert
2090 widget 'custom-group-link
2091 :tag (custom-unlispify-tag-name symbol)
2092 symbol)
2093 buttons)
2094 (setq parents (cons symbol parents))))))
2095 (and (null (get name 'custom-links)) ;No links of its own.
2096 (= (length parents) 1) ;A single parent.
2097 (let* ((links (get (car parents) 'custom-links))
2098 (many (> (length links) 2)))
2099 (when links
2100 (insert "\nParent documentation: ")
2101 (while links
2102 (push (widget-create-child-and-convert widget (car links))
2103 buttons)
2104 (setq links (cdr links))
2105 (cond ((null links)
2106 (insert ".\n"))
2107 ((null (cdr links))
2108 (if many
2109 (insert ", and ")
2110 (insert " and ")))
2111 (t
2112 (insert ", ")))))))
2113 (if parents
2114 (insert "\n")
2115 (delete-region start (point)))
2116 (widget-put widget :buttons buttons)
2117 parents))
2118
2119 ;;; The `custom-comment' Widget.
2120
2121 ;; like the editable field
2122 (defface custom-comment '((((class grayscale color)
2123 (background light))
2124 (:background "gray85"))
2125 (((class grayscale color)
2126 (background dark))
2127 (:background "dim gray"))
2128 (t
2129 (:slant italic)))
2130 "Face used for comments on variables or faces"
2131 :version "21.1"
2132 :group 'custom-faces)
2133 ;; backward-compatibility alias
2134 (put 'custom-comment-face 'face-alias 'custom-comment)
2135
2136 ;; like font-lock-comment-face
2137 (defface custom-comment-tag
2138 '((((class color) (background dark)) (:foreground "gray80"))
2139 (((class color) (background light)) (:foreground "blue4"))
2140 (((class grayscale) (background light))
2141 (:foreground "DimGray" :weight bold :slant italic))
2142 (((class grayscale) (background dark))
2143 (:foreground "LightGray" :weight bold :slant italic))
2144 (t (:weight bold)))
2145 "Face used for variables or faces comment tags"
2146 :group 'custom-faces)
2147 ;; backward-compatibility alias
2148 (put 'custom-comment-tag-face 'face-alias 'custom-comment-tag)
2149
2150 (define-widget 'custom-comment 'string
2151 "User comment."
2152 :tag "Comment"
2153 :help-echo "Edit a comment here."
2154 :sample-face 'custom-comment-tag-face
2155 :value-face 'custom-comment-face
2156 :shown nil
2157 :create 'custom-comment-create)
2158
2159 (defun custom-comment-create (widget)
2160 (let* ((null-comment (equal "" (widget-value widget))))
2161 (if (or (widget-get (widget-get widget :parent) :comment-shown)
2162 (not null-comment))
2163 (widget-default-create widget)
2164 ;; `widget-default-delete' expects markers in these slots --
2165 ;; maybe it shouldn't.
2166 (widget-put widget :from (point-marker))
2167 (widget-put widget :to (point-marker)))))
2168
2169 (defun custom-comment-hide (widget)
2170 (widget-put (widget-get widget :parent) :comment-shown nil))
2171
2172 ;; Those functions are for the menu. WIDGET is NOT the comment widget. It's
2173 ;; the global custom one
2174 (defun custom-comment-show (widget)
2175 (widget-put widget :comment-shown t)
2176 (custom-redraw widget)
2177 (widget-setup))
2178
2179 (defun custom-comment-invisible-p (widget)
2180 (let ((val (widget-value (widget-get widget :comment-widget))))
2181 (and (equal "" val)
2182 (not (widget-get widget :comment-shown)))))
2183
2184 ;;; The `custom-variable' Widget.
2185
2186 ;; When this was underlined blue, users confused it with a
2187 ;; Mosaic-style hyperlink...
2188 (defface custom-variable-tag
2189 `((((class color)
2190 (background dark))
2191 (:foreground "light blue" :weight bold :height 1.2 :inherit variable-pitch))
2192 (((min-colors 88) (class color)
2193 (background light))
2194 (:foreground "blue1" :weight bold :height 1.2 :inherit variable-pitch))
2195 (((class color)
2196 (background light))
2197 (:foreground "blue" :weight bold :height 1.2 :inherit variable-pitch))
2198 (t (:weight bold)))
2199 "Face used for unpushable variable tags."
2200 :group 'custom-faces)
2201 ;; backward-compatibility alias
2202 (put 'custom-variable-tag-face 'face-alias 'custom-variable-tag)
2203
2204 (defface custom-variable-button '((t (:underline t :weight bold)))
2205 "Face used for pushable variable tags."
2206 :group 'custom-faces)
2207 ;; backward-compatibility alias
2208 (put 'custom-variable-button-face 'face-alias 'custom-variable-button)
2209
2210 (defcustom custom-variable-default-form 'edit
2211 "Default form of displaying variable values."
2212 :type '(choice (const edit)
2213 (const lisp))
2214 :group 'custom-buffer
2215 :version "20.3")
2216
2217 (defun custom-variable-documentation (variable)
2218 "Return documentation of VARIABLE for use in Custom buffer.
2219 Normally just return the docstring. But if VARIABLE automatically
2220 becomes buffer local when set, append a message to that effect."
2221 (if (and (local-variable-if-set-p variable)
2222 (or (not (local-variable-p variable))
2223 (with-temp-buffer
2224 (local-variable-if-set-p variable))))
2225 (concat (documentation-property variable 'variable-documentation)
2226 "\n
2227 This variable automatically becomes buffer-local when set outside Custom.
2228 However, setting it through Custom sets the default value.")
2229 (documentation-property variable 'variable-documentation)))
2230
2231 (define-widget 'custom-variable 'custom
2232 "Customize variable."
2233 :format "%v"
2234 :help-echo "Set or reset this variable."
2235 :documentation-property #'custom-variable-documentation
2236 :custom-category 'option
2237 :custom-state nil
2238 :custom-menu 'custom-variable-menu-create
2239 :custom-form nil ; defaults to value of `custom-variable-default-form'
2240 :value-create 'custom-variable-value-create
2241 :action 'custom-variable-action
2242 :custom-set 'custom-variable-set
2243 :custom-save 'custom-variable-save
2244 :custom-reset-current 'custom-redraw
2245 :custom-reset-saved 'custom-variable-reset-saved
2246 :custom-reset-standard 'custom-variable-reset-standard
2247 :custom-standard-value 'custom-variable-standard-value)
2248
2249 (defun custom-variable-type (symbol)
2250 "Return a widget suitable for editing the value of SYMBOL.
2251 If SYMBOL has a `custom-type' property, use that.
2252 Otherwise, look up symbol in `custom-guess-type-alist'."
2253 (let* ((type (or (get symbol 'custom-type)
2254 (and (not (get symbol 'standard-value))
2255 (custom-guess-type symbol))
2256 'sexp))
2257 (options (get symbol 'custom-options))
2258 (tmp (if (listp type)
2259 (copy-sequence type)
2260 (list type))))
2261 (when options
2262 (widget-put tmp :options options))
2263 tmp))
2264
2265 (defun custom-variable-value-create (widget)
2266 "Here is where you edit the variable's value."
2267 (custom-load-widget widget)
2268 (unless (widget-get widget :custom-form)
2269 (widget-put widget :custom-form custom-variable-default-form))
2270 (let* ((buttons (widget-get widget :buttons))
2271 (children (widget-get widget :children))
2272 (form (widget-get widget :custom-form))
2273 (state (widget-get widget :custom-state))
2274 (symbol (widget-get widget :value))
2275 (tag (widget-get widget :tag))
2276 (type (custom-variable-type symbol))
2277 (conv (widget-convert type))
2278 (get (or (get symbol 'custom-get) 'default-value))
2279 (prefix (widget-get widget :custom-prefix))
2280 (last (widget-get widget :custom-last))
2281 (value (if (default-boundp symbol)
2282 (funcall get symbol)
2283 (widget-get conv :value))))
2284 ;; If the widget is new, the child determines whether it is hidden.
2285 (cond (state)
2286 ((custom-show type value)
2287 (setq state 'unknown))
2288 (t
2289 (setq state 'hidden)))
2290 ;; If we don't know the state, see if we need to edit it in lisp form.
2291 (when (eq state 'unknown)
2292 (unless (widget-apply conv :match value)
2293 ;; (widget-apply (widget-convert type) :match value)
2294 (setq form 'mismatch)))
2295 ;; Now we can create the child widget.
2296 (cond ((eq custom-buffer-style 'tree)
2297 (insert prefix (if last " `--- " " |--- "))
2298 (push (widget-create-child-and-convert
2299 widget 'custom-browse-variable-tag)
2300 buttons)
2301 (insert " " tag "\n")
2302 (widget-put widget :buttons buttons))
2303 ((eq state 'hidden)
2304 ;; Indicate hidden value.
2305 (push (widget-create-child-and-convert
2306 widget 'item
2307 :format "%{%t%}: "
2308 :sample-face 'custom-variable-tag-face
2309 :tag tag
2310 :parent widget)
2311 buttons)
2312 (push (widget-create-child-and-convert
2313 widget 'visibility
2314 :help-echo "Show the value of this option."
2315 :off "Show Value"
2316 :action 'custom-toggle-parent
2317 nil)
2318 buttons))
2319 ((memq form '(lisp mismatch))
2320 ;; In lisp mode edit the saved value when possible.
2321 (let* ((value (cond ((get symbol 'saved-value)
2322 (car (get symbol 'saved-value)))
2323 ((get symbol 'standard-value)
2324 (car (get symbol 'standard-value)))
2325 ((default-boundp symbol)
2326 (custom-quote (funcall get symbol)))
2327 (t
2328 (custom-quote (widget-get conv :value))))))
2329 (insert (symbol-name symbol) ": ")
2330 (push (widget-create-child-and-convert
2331 widget 'visibility
2332 :help-echo "Hide the value of this option."
2333 :on "Hide Value"
2334 :off "Show Value"
2335 :action 'custom-toggle-parent
2336 t)
2337 buttons)
2338 (insert " ")
2339 (push (widget-create-child-and-convert
2340 widget 'sexp
2341 :button-face 'custom-variable-button-face
2342 :format "%v"
2343 :tag (symbol-name symbol)
2344 :parent widget
2345 :value value)
2346 children)))
2347 (t
2348 ;; Edit mode.
2349 (let* ((format (widget-get type :format))
2350 tag-format value-format)
2351 (unless (string-match ":" format)
2352 (error "Bad format"))
2353 (setq tag-format (substring format 0 (match-end 0)))
2354 (setq value-format (substring format (match-end 0)))
2355 (push (widget-create-child-and-convert
2356 widget 'item
2357 :format tag-format
2358 :action 'custom-tag-action
2359 :help-echo "Change value of this option."
2360 :mouse-down-action 'custom-tag-mouse-down-action
2361 :button-face 'custom-variable-button-face
2362 :sample-face 'custom-variable-tag-face
2363 tag)
2364 buttons)
2365 (insert " ")
2366 (push (widget-create-child-and-convert
2367 widget 'visibility
2368 :help-echo "Hide the value of this option."
2369 :on "Hide Value"
2370 :off "Show Value"
2371 :action 'custom-toggle-parent
2372 t)
2373 buttons)
2374 (push (widget-create-child-and-convert
2375 widget type
2376 :format value-format
2377 :value value)
2378 children))))
2379 (unless (eq custom-buffer-style 'tree)
2380 (unless (eq (preceding-char) ?\n)
2381 (widget-insert "\n"))
2382 ;; Create the magic button.
2383 (let ((magic (widget-create-child-and-convert
2384 widget 'custom-magic nil)))
2385 (widget-put widget :custom-magic magic)
2386 (push magic buttons))
2387 ;; ### NOTE: this is ugly!!!! I need to update the :buttons property
2388 ;; before the call to `widget-default-format-handler'. Otherwise, I
2389 ;; loose my current `buttons'. This function shouldn't be called like
2390 ;; this anyway. The doc string widget should be added like the others.
2391 ;; --dv
2392 (widget-put widget :buttons buttons)
2393 (insert "\n")
2394 ;; Insert documentation.
2395 (widget-default-format-handler widget ?h)
2396
2397 ;; The comment field
2398 (unless (eq state 'hidden)
2399 (let* ((comment (get symbol 'variable-comment))
2400 (comment-widget
2401 (widget-create-child-and-convert
2402 widget 'custom-comment
2403 :parent widget
2404 :value (or comment ""))))
2405 (widget-put widget :comment-widget comment-widget)
2406 ;; Don't push it !!! Custom assumes that the first child is the
2407 ;; value one.
2408 (setq children (append children (list comment-widget)))))
2409 ;; Update the rest of the properties properties.
2410 (widget-put widget :custom-form form)
2411 (widget-put widget :children children)
2412 ;; Now update the state.
2413 (if (eq state 'hidden)
2414 (widget-put widget :custom-state state)
2415 (custom-variable-state-set widget))
2416 ;; See also.
2417 (unless (eq state 'hidden)
2418 (when (eq (widget-get widget :custom-level) 1)
2419 (custom-add-parent-links widget))
2420 (custom-add-see-also widget)))))
2421
2422 (defun custom-tag-action (widget &rest args)
2423 "Pass :action to first child of WIDGET's parent."
2424 (apply 'widget-apply (car (widget-get (widget-get widget :parent) :children))
2425 :action args))
2426
2427 (defun custom-tag-mouse-down-action (widget &rest args)
2428 "Pass :mouse-down-action to first child of WIDGET's parent."
2429 (apply 'widget-apply (car (widget-get (widget-get widget :parent) :children))
2430 :mouse-down-action args))
2431
2432 (defun custom-variable-state-set (widget)
2433 "Set the state of WIDGET."
2434 (let* ((symbol (widget-value widget))
2435 (get (or (get symbol 'custom-get) 'default-value))
2436 (value (if (default-boundp symbol)
2437 (funcall get symbol)
2438 (widget-get widget :value)))
2439 (comment (get symbol 'variable-comment))
2440 tmp
2441 temp
2442 (state (cond ((progn (setq tmp (get symbol 'customized-value))
2443 (setq temp
2444 (get symbol 'customized-variable-comment))
2445 (or tmp temp))
2446 (if (condition-case nil
2447 (and (equal value (eval (car tmp)))
2448 (equal comment temp))
2449 (error nil))
2450 'set
2451 'changed))
2452 ((progn (setq tmp (get symbol 'saved-value))
2453 (setq temp (get symbol 'saved-variable-comment))
2454 (or tmp temp))
2455 (if (condition-case nil
2456 (and (equal value (eval (car tmp)))
2457 (equal comment temp))
2458 (error nil))
2459 'saved
2460 'changed))
2461 ((setq tmp (get symbol 'standard-value))
2462 (if (condition-case nil
2463 (and (equal value (eval (car tmp)))
2464 (equal comment nil))
2465 (error nil))
2466 'standard
2467 'changed))
2468 (t 'rogue))))
2469 (widget-put widget :custom-state state)))
2470
2471 (defun custom-variable-standard-value (widget)
2472 (get (widget-value widget) 'standard-value))
2473
2474 (defvar custom-variable-menu
2475 '(("Set for Current Session" custom-variable-set
2476 (lambda (widget)
2477 (eq (widget-get widget :custom-state) 'modified)))
2478 ("Save for Future Sessions" custom-variable-save
2479 (lambda (widget)
2480 (memq (widget-get widget :custom-state) '(modified set changed rogue))))
2481 ("Reset to Current" custom-redraw
2482 (lambda (widget)
2483 (and (default-boundp (widget-value widget))
2484 (memq (widget-get widget :custom-state) '(modified changed)))))
2485 ("Reset to Saved" custom-variable-reset-saved
2486 (lambda (widget)
2487 (and (or (get (widget-value widget) 'saved-value)
2488 (get (widget-value widget) 'saved-variable-comment))
2489 (memq (widget-get widget :custom-state)
2490 '(modified set changed rogue)))))
2491 ("Erase Customization" custom-variable-reset-standard
2492 (lambda (widget)
2493 (and (get (widget-value widget) 'standard-value)
2494 (memq (widget-get widget :custom-state)
2495 '(modified set changed saved rogue)))))
2496 ("Use Backup Value" custom-variable-reset-backup
2497 (lambda (widget)
2498 (get (widget-value widget) 'backup-value)))
2499 ("---" ignore ignore)
2500 ("Add Comment" custom-comment-show custom-comment-invisible-p)
2501 ("---" ignore ignore)
2502 ("Don't show as Lisp expression" custom-variable-edit
2503 (lambda (widget)
2504 (eq (widget-get widget :custom-form) 'lisp)))
2505 ("Show initial Lisp expression" custom-variable-edit-lisp
2506 (lambda (widget)
2507 (eq (widget-get widget :custom-form) 'edit))))
2508 "Alist of actions for the `custom-variable' widget.
2509 Each entry has the form (NAME ACTION FILTER) where NAME is the name of
2510 the menu entry, ACTION is the function to call on the widget when the
2511 menu is selected, and FILTER is a predicate which takes a `custom-variable'
2512 widget as an argument, and returns non-nil if ACTION is valid on that
2513 widget. If FILTER is nil, ACTION is always valid.")
2514
2515 (defun custom-variable-action (widget &optional event)
2516 "Show the menu for `custom-variable' WIDGET.
2517 Optional EVENT is the location for the menu."
2518 (if (eq (widget-get widget :custom-state) 'hidden)
2519 (custom-toggle-hide widget)
2520 (unless (eq (widget-get widget :custom-state) 'modified)
2521 (custom-variable-state-set widget))
2522 (custom-redraw-magic widget)
2523 (let* ((completion-ignore-case t)
2524 (answer (widget-choose (concat "Operation on "
2525 (custom-unlispify-tag-name
2526 (widget-get widget :value)))
2527 (custom-menu-filter custom-variable-menu
2528 widget)
2529 event)))
2530 (if answer
2531 (funcall answer widget)))))
2532
2533 (defun custom-variable-edit (widget)
2534 "Edit value of WIDGET."
2535 (widget-put widget :custom-state 'unknown)
2536 (widget-put widget :custom-form 'edit)
2537 (custom-redraw widget))
2538
2539 (defun custom-variable-edit-lisp (widget)
2540 "Edit the Lisp representation of the value of WIDGET."
2541 (widget-put widget :custom-state 'unknown)
2542 (widget-put widget :custom-form 'lisp)
2543 (custom-redraw widget))
2544
2545 (defun custom-variable-set (widget)
2546 "Set the current value for the variable being edited by WIDGET."
2547 (let* ((form (widget-get widget :custom-form))
2548 (state (widget-get widget :custom-state))
2549 (child (car (widget-get widget :children)))
2550 (symbol (widget-value widget))
2551 (set (or (get symbol 'custom-set) 'set-default))
2552 (comment-widget (widget-get widget :comment-widget))
2553 (comment (widget-value comment-widget))
2554 val)
2555 (cond ((eq state 'hidden)
2556 (error "Cannot set hidden variable"))
2557 ((setq val (widget-apply child :validate))
2558 (goto-char (widget-get val :from))
2559 (error "%s" (widget-get val :error)))
2560 ((memq form '(lisp mismatch))
2561 (when (equal comment "")
2562 (setq comment nil)
2563 ;; Make the comment invisible by hand if it's empty
2564 (custom-comment-hide comment-widget))
2565 (custom-variable-backup-value widget)
2566 (funcall set symbol (eval (setq val (widget-value child))))
2567 (put symbol 'customized-value (list val))
2568 (put symbol 'variable-comment comment)
2569 (put symbol 'customized-variable-comment comment))
2570 (t
2571 (when (equal comment "")
2572 (setq comment nil)
2573 ;; Make the comment invisible by hand if it's empty
2574 (custom-comment-hide comment-widget))
2575 (custom-variable-backup-value widget)
2576 (funcall set symbol (setq val (widget-value child)))
2577 (put symbol 'customized-value (list (custom-quote val)))
2578 (put symbol 'variable-comment comment)
2579 (put symbol 'customized-variable-comment comment)))
2580 (custom-variable-state-set widget)
2581 (custom-redraw-magic widget)))
2582
2583 (defun custom-variable-save (widget)
2584 "Set and save the value for the variable being edited by WIDGET."
2585 (let* ((form (widget-get widget :custom-form))
2586 (state (widget-get widget :custom-state))
2587 (child (car (widget-get widget :children)))
2588 (symbol (widget-value widget))
2589 (set (or (get symbol 'custom-set) 'set-default))
2590 (comment-widget (widget-get widget :comment-widget))
2591 (comment (widget-value comment-widget))
2592 val)
2593 (cond ((eq state 'hidden)
2594 (error "Cannot set hidden variable"))
2595 ((setq val (widget-apply child :validate))
2596 (goto-char (widget-get val :from))
2597 (error "Saving %s: %s" symbol (widget-get val :error)))
2598 ((memq form '(lisp mismatch))
2599 (when (equal comment "")
2600 (setq comment nil)
2601 ;; Make the comment invisible by hand if it's empty
2602 (custom-comment-hide comment-widget))
2603 (put symbol 'saved-value (list (widget-value child)))
2604 (custom-push-theme 'theme-value symbol 'user
2605 'set (list (widget-value child)))
2606 (funcall set symbol (eval (widget-value child)))
2607 (put symbol 'variable-comment comment)
2608 (put symbol 'saved-variable-comment comment))
2609 (t
2610 (when (equal comment "")
2611 (setq comment nil)
2612 ;; Make the comment invisible by hand if it's empty
2613 (custom-comment-hide comment-widget))
2614 (put symbol 'saved-value
2615 (list (custom-quote (widget-value child))))
2616 (custom-push-theme 'theme-value symbol 'user
2617 'set (list (custom-quote (widget-value
2618 child))))
2619 (funcall set symbol (widget-value child))
2620 (put symbol 'variable-comment comment)
2621 (put symbol 'saved-variable-comment comment)))
2622 (put symbol 'customized-value nil)
2623 (put symbol 'customized-variable-comment nil)
2624 (custom-save-all)
2625 (custom-variable-state-set widget)
2626 (custom-redraw-magic widget)))
2627
2628 (defun custom-variable-reset-saved (widget)
2629 "Restore the saved value for the variable being edited by WIDGET.
2630 The value that was current before this operation
2631 becomes the backup value, so you can get it again."
2632 (let* ((symbol (widget-value widget))
2633 (set (or (get symbol 'custom-set) 'set-default))
2634 (value (get symbol 'saved-value))
2635 (comment (get symbol 'saved-variable-comment)))
2636 (cond ((or value comment)
2637 (put symbol 'variable-comment comment)
2638 (custom-variable-backup-value widget)
2639 (condition-case nil
2640 (funcall set symbol (eval (car value)))
2641 (error nil)))
2642 (t
2643 (error "No saved value for %s" symbol)))
2644 (put symbol 'customized-value nil)
2645 (put symbol 'customized-variable-comment nil)
2646 (widget-put widget :custom-state 'unknown)
2647 ;; This call will possibly make the comment invisible
2648 (custom-redraw widget)))
2649
2650 (defun custom-variable-reset-standard (widget)
2651 "Restore the standard setting for the variable being edited by WIDGET.
2652 This operation eliminates any saved setting for the variable,
2653 restoring it to the state of a variable that has never been customized.
2654 The value that was current before this operation
2655 becomes the backup value, so you can get it again."
2656 (let* ((symbol (widget-value widget))
2657 (set (or (get symbol 'custom-set) 'set-default)))
2658 (if (get symbol 'standard-value)
2659 (progn
2660 (custom-variable-backup-value widget)
2661 (funcall set symbol (eval (car (get symbol 'standard-value)))))
2662 (error "No standard setting known for %S" symbol))
2663 (put symbol 'variable-comment nil)
2664 (put symbol 'customized-value nil)
2665 (put symbol 'customized-variable-comment nil)
2666 (when (or (get symbol 'saved-value) (get symbol 'saved-variable-comment))
2667 (put symbol 'saved-value nil)
2668 (custom-push-theme 'theme-value symbol 'user 'reset 'standard)
2669 ;; As a special optimizations we do not (explictly)
2670 ;; save resets to standard when no theme set the value.
2671 (if (null (cdr (get symbol 'theme-value)))
2672 (put symbol 'theme-value nil))
2673 (put symbol 'saved-variable-comment nil)
2674 (custom-save-all))
2675 (widget-put widget :custom-state 'unknown)
2676 ;; This call will possibly make the comment invisible
2677 (custom-redraw widget)))
2678
2679 (defun custom-variable-backup-value (widget)
2680 "Back up the current value for WIDGET's variable.
2681 The backup value is kept in the car of the `backup-value' property."
2682 (let* ((symbol (widget-value widget))
2683 (get (or (get symbol 'custom-get) 'default-value))
2684 (type (custom-variable-type symbol))
2685 (conv (widget-convert type))
2686 (value (if (default-boundp symbol)
2687 (funcall get symbol)
2688 (widget-get conv :value))))
2689 (put symbol 'backup-value (list value))))
2690
2691 (defun custom-variable-reset-backup (widget)
2692 "Restore the backup value for the variable being edited by WIDGET.
2693 The value that was current before this operation
2694 becomes the backup value, so you can use this operation repeatedly
2695 to switch between two values."
2696 (let* ((symbol (widget-value widget))
2697 (set (or (get symbol 'custom-set) 'set-default))
2698 (value (get symbol 'backup-value))
2699 (comment-widget (widget-get widget :comment-widget))
2700 (comment (widget-value comment-widget)))
2701 (if value
2702 (progn
2703 (custom-variable-backup-value widget)
2704 (condition-case nil
2705 (funcall set symbol (car value))
2706 (error nil)))
2707 (error "No backup value for %s" symbol))
2708 (put symbol 'customized-value (list (car value)))
2709 (put symbol 'variable-comment comment)
2710 (put symbol 'customized-variable-comment comment)
2711 (custom-variable-state-set widget)
2712 ;; This call will possibly make the comment invisible
2713 (custom-redraw widget)))
2714
2715 ;;; The `custom-face-edit' Widget.
2716
2717 (define-widget 'custom-face-edit 'checklist
2718 "Edit face attributes."
2719 :format "%t: %v"
2720 :tag "Attributes"
2721 :extra-offset 13
2722 :button-args '(:help-echo "Control whether this attribute has any effect.")
2723 :value-to-internal 'custom-face-edit-fix-value
2724 :match (lambda (widget value)
2725 (widget-checklist-match widget
2726 (custom-face-edit-fix-value widget value)))
2727 :convert-widget 'custom-face-edit-convert-widget
2728 :args (mapcar (lambda (att)
2729 (list 'group
2730 :inline t
2731 :sibling-args (widget-get (nth 1 att) :sibling-args)
2732 (list 'const :format "" :value (nth 0 att))
2733 (nth 1 att)))
2734 custom-face-attributes))
2735
2736 (defun custom-face-edit-fix-value (widget value)
2737 "Ignoring WIDGET, convert :bold and :italic in VALUE to new form.
2738 Also change :reverse-video to :inverse-video."
2739 (if (listp value)
2740 (let (result)
2741 (while value
2742 (let ((key (car value))
2743 (val (car (cdr value))))
2744 (cond ((eq key :italic)
2745 (push :slant result)
2746 (push (if val 'italic 'normal) result))
2747 ((eq key :bold)
2748 (push :weight result)
2749 (push (if val 'bold 'normal) result))
2750 ((eq key :reverse-video)
2751 (push :inverse-video result)
2752 (push val result))
2753 (t
2754 (push key result)
2755 (push val result))))
2756 (setq value (cdr (cdr value))))
2757 (setq result (nreverse result))
2758 result)
2759 value))
2760
2761 (defun custom-face-edit-convert-widget (widget)
2762 "Convert :args as widget types in WIDGET."
2763 (widget-put
2764 widget
2765 :args (mapcar (lambda (arg)
2766 (widget-convert arg
2767 :deactivate 'custom-face-edit-deactivate
2768 :activate 'custom-face-edit-activate
2769 :delete 'custom-face-edit-delete))
2770 (widget-get widget :args)))
2771 widget)
2772
2773 (defun custom-face-edit-deactivate (widget)
2774 "Make face widget WIDGET inactive for user modifications."
2775 (unless (widget-get widget :inactive)
2776 (let ((tag (custom-face-edit-attribute-tag widget))
2777 (from (copy-marker (widget-get widget :from)))
2778 (value (widget-value widget))
2779 (inhibit-read-only t)
2780 (inhibit-modification-hooks t))
2781 (save-excursion
2782 (goto-char from)
2783 (widget-default-delete widget)
2784 (insert tag ": *\n")
2785 (widget-put widget :inactive
2786 (cons value (cons from (- (point) from))))))))
2787
2788 (defun custom-face-edit-activate (widget)
2789 "Make face widget WIDGET inactive for user modifications."
2790 (let ((inactive (widget-get widget :inactive))
2791 (inhibit-read-only t)
2792 (inhibit-modification-hooks t))
2793 (when (consp inactive)
2794 (save-excursion
2795 (goto-char (car (cdr inactive)))
2796 (delete-region (point) (+ (point) (cdr (cdr inactive))))
2797 (widget-put widget :inactive nil)
2798 (widget-apply widget :create)
2799 (widget-value-set widget (car inactive))
2800 (widget-setup)))))
2801
2802 (defun custom-face-edit-delete (widget)
2803 "Remove WIDGET from the buffer."
2804 (let ((inactive (widget-get widget :inactive))
2805 (inhibit-read-only t)
2806 (inhibit-modification-hooks t))
2807 (if (not inactive)
2808 ;; Widget is alive, we don't have to do anything special
2809 (widget-default-delete widget)
2810 ;; WIDGET is already deleted because we did so to inactivate it;
2811 ;; now just get rid of the label we put in its place.
2812 (delete-region (car (cdr inactive))
2813 (+ (car (cdr inactive)) (cdr (cdr inactive))))
2814 (widget-put widget :inactive nil))))
2815
2816
2817 (defun custom-face-edit-attribute-tag (widget)
2818 "Returns the first :tag property in WIDGET or one of its children."
2819 (let ((tag (widget-get widget :tag)))
2820 (or (and (not (equal tag "")) tag)
2821 (let ((children (widget-get widget :children)))
2822 (while (and (null tag) children)
2823 (setq tag (custom-face-edit-attribute-tag (pop children))))
2824 tag))))
2825
2826 ;;; The `custom-display' Widget.
2827
2828 (define-widget 'custom-display 'menu-choice
2829 "Select a display type."
2830 :tag "Display"
2831 :value t
2832 :help-echo "Specify frames where the face attributes should be used."
2833 :args '((const :tag "all" t)
2834 (const :tag "defaults" default)
2835 (checklist
2836 :offset 0
2837 :extra-offset 9
2838 :args ((group :sibling-args (:help-echo "\
2839 Only match the specified window systems.")
2840 (const :format "Type: "
2841 type)
2842 (checklist :inline t
2843 :offset 0
2844 (const :format "X "
2845 :sibling-args (:help-echo "\
2846 The X11 Window System.")
2847 x)
2848 (const :format "PM "
2849 :sibling-args (:help-echo "\
2850 OS/2 Presentation Manager.")
2851 pm)
2852 (const :format "W32 "
2853 :sibling-args (:help-echo "\
2854 Windows NT/9X.")
2855 w32)
2856 (const :format "MAC "
2857 :sibling-args (:help-echo "\
2858 Macintosh OS.")
2859 mac)
2860 (const :format "DOS "
2861 :sibling-args (:help-echo "\
2862 Plain MS-DOS.")
2863 pc)
2864 (const :format "TTY%n"
2865 :sibling-args (:help-echo "\
2866 Plain text terminals.")
2867 tty)))
2868 (group :sibling-args (:help-echo "\
2869 Only match the frames with the specified color support.")
2870 (const :format "Class: "
2871 class)
2872 (checklist :inline t
2873 :offset 0
2874 (const :format "Color "
2875 :sibling-args (:help-echo "\
2876 Match color frames.")
2877 color)
2878 (const :format "Grayscale "
2879 :sibling-args (:help-echo "\
2880 Match grayscale frames.")
2881 grayscale)
2882 (const :format "Monochrome%n"
2883 :sibling-args (:help-echo "\
2884 Match frames with no color support.")
2885 mono)))
2886 (group :sibling-args (:help-echo "\
2887 The minimum number of colors the frame should support.")
2888 (const :format "" min-colors)
2889 (integer :tag "Minimum number of colors" ))
2890 (group :sibling-args (:help-echo "\
2891 Only match frames with the specified intensity.")
2892 (const :format "\
2893 Background brightness: "
2894 background)
2895 (checklist :inline t
2896 :offset 0
2897 (const :format "Light "
2898 :sibling-args (:help-echo "\
2899 Match frames with light backgrounds.")
2900 light)
2901 (const :format "Dark\n"
2902 :sibling-args (:help-echo "\
2903 Match frames with dark backgrounds.")
2904 dark)))
2905 (group :sibling-args (:help-echo "\
2906 Only match frames that support the specified face attributes.")
2907 (const :format "Supports attributes:" supports)
2908 (custom-face-edit :inline t :format "%n%v"))))))
2909
2910 ;;; The `custom-face' Widget.
2911
2912 (defface custom-face-tag
2913 `((t (:weight bold :height 1.2 :inherit variable-pitch)))
2914 "Face used for face tags."
2915 :group 'custom-faces)
2916 ;; backward-compatibility alias
2917 (put 'custom-face-tag-face 'face-alias 'custom-face-tag)
2918
2919 (defcustom custom-face-default-form 'selected
2920 "Default form of displaying face definition."
2921 :type '(choice (const all)
2922 (const selected)
2923 (const lisp))
2924 :group 'custom-buffer
2925 :version "20.3")
2926
2927 (define-widget 'custom-face 'custom
2928 "Customize face."
2929 :sample-face 'custom-face-tag-face
2930 :help-echo "Set or reset this face."
2931 :documentation-property #'face-doc-string
2932 :value-create 'custom-face-value-create
2933 :action 'custom-face-action
2934 :custom-category 'face
2935 :custom-form nil ; defaults to value of `custom-face-default-form'
2936 :custom-set 'custom-face-set
2937 :custom-save 'custom-face-save
2938 :custom-reset-current 'custom-redraw
2939 :custom-reset-saved 'custom-face-reset-saved
2940 :custom-reset-standard 'custom-face-reset-standard
2941 :custom-standard-value 'custom-face-standard-value
2942 :custom-menu 'custom-face-menu-create)
2943
2944 (define-widget 'custom-face-all 'editable-list
2945 "An editable list of display specifications and attributes."
2946 :entry-format "%i %d %v"
2947 :insert-button-args '(:help-echo "Insert new display specification here.")
2948 :append-button-args '(:help-echo "Append new display specification here.")
2949 :delete-button-args '(:help-echo "Delete this display specification.")
2950 :args '((group :format "%v" custom-display custom-face-edit)))
2951
2952 (defconst custom-face-all (widget-convert 'custom-face-all)
2953 "Converted version of the `custom-face-all' widget.")
2954
2955 (define-widget 'custom-display-unselected 'item
2956 "A display specification that doesn't match the selected display."
2957 :match 'custom-display-unselected-match)
2958
2959 (defun custom-display-unselected-match (widget value)
2960 "Non-nil if VALUE is an unselected display specification."
2961 (not (face-spec-set-match-display value (selected-frame))))
2962
2963 (define-widget 'custom-face-selected 'group
2964 "Edit the attributes of the selected display in a face specification."
2965 :args '((choice :inline t
2966 (group :tag "With Defaults" :inline t
2967 (group (const :tag "" default)
2968 (custom-face-edit :tag " Default\n Attributes"))
2969 (repeat :format ""
2970 :inline t
2971 (group custom-display-unselected sexp))
2972 (group (sexp :format "")
2973 (custom-face-edit :tag " Overriding\n Attributes"))
2974 (repeat :format ""
2975 :inline t
2976 sexp))
2977 (group :tag "No Defaults" :inline t
2978 (repeat :format ""
2979 :inline t
2980 (group custom-display-unselected sexp))
2981 (group (sexp :format "")
2982 (custom-face-edit :tag "\n Attributes"))
2983 (repeat :format ""
2984 :inline t
2985 sexp)))))
2986
2987
2988
2989 (defconst custom-face-selected (widget-convert 'custom-face-selected)
2990 "Converted version of the `custom-face-selected' widget.")
2991
2992 (defun custom-filter-face-spec (spec filter-index &optional default-filter)
2993 "Return a canonicalized version of SPEC using.
2994 FILTER-INDEX is the index in the entry for each attribute in
2995 `custom-face-attributes' at which the appropriate filter function can be
2996 found, and DEFAULT-FILTER is the filter to apply for attributes that
2997 don't specify one."
2998 (mapcar (lambda (entry)
2999 ;; Filter a single face-spec entry
3000 (let ((tests (car entry))
3001 (unfiltered-attrs
3002 ;; Handle both old- and new-style attribute syntax
3003 (if (listp (car (cdr entry)))
3004 (car (cdr entry))
3005 (cdr entry)))
3006 (filtered-attrs nil))
3007 ;; Filter each face attribute
3008 (while unfiltered-attrs
3009 (let* ((attr (pop unfiltered-attrs))
3010 (pre-filtered-value (pop unfiltered-attrs))
3011 (filter
3012 (or (nth filter-index (assq attr custom-face-attributes))
3013 default-filter))
3014 (filtered-value
3015 (if filter
3016 (funcall filter pre-filtered-value)
3017 pre-filtered-value)))
3018 (push filtered-value filtered-attrs)
3019 (push attr filtered-attrs)))
3020 ;;
3021 (list tests filtered-attrs)))
3022 spec))
3023
3024 (defun custom-pre-filter-face-spec (spec)
3025 "Return SPEC changed as necessary for editing by the face customization widget.
3026 SPEC must be a full face spec."
3027 (custom-filter-face-spec spec 2))
3028
3029 (defun custom-post-filter-face-spec (spec)
3030 "Return the customized SPEC in a form suitable for setting the face."
3031 (custom-filter-face-spec spec 3))
3032
3033 (defun custom-face-value-create (widget)
3034 "Create a list of the display specifications for WIDGET."
3035 (let ((buttons (widget-get widget :buttons))
3036 children
3037 (symbol (widget-get widget :value))
3038 (tag (widget-get widget :tag))
3039 (state (widget-get widget :custom-state))
3040 (begin (point))
3041 (is-last (widget-get widget :custom-last))
3042 (prefix (widget-get widget :custom-prefix)))
3043 (unless tag
3044 (setq tag (prin1-to-string symbol)))
3045 (cond ((eq custom-buffer-style 'tree)
3046 (insert prefix (if is-last " `--- " " |--- "))
3047 (push (widget-create-child-and-convert
3048 widget 'custom-browse-face-tag)
3049 buttons)
3050 (insert " " tag "\n")
3051 (widget-put widget :buttons buttons))
3052 (t
3053 ;; Create tag.
3054 (insert tag)
3055 (widget-specify-sample widget begin (point))
3056 (if (eq custom-buffer-style 'face)
3057 (insert " ")
3058 (if (string-match "face\\'" tag)
3059 (insert ":")
3060 (insert " face: ")))
3061 ;; Sample.
3062 (push (widget-create-child-and-convert widget 'item
3063 :format "(%{%t%})"
3064 :sample-face symbol
3065 :tag "sample")
3066 buttons)
3067 ;; Visibility.
3068 (insert " ")
3069 (push (widget-create-child-and-convert
3070 widget 'visibility
3071 :help-echo "Hide or show this face."
3072 :on "Hide Face"
3073 :off "Show Face"
3074 :action 'custom-toggle-parent
3075 (not (eq state 'hidden)))
3076 buttons)
3077 ;; Magic.
3078 (insert "\n")
3079 (let ((magic (widget-create-child-and-convert
3080 widget 'custom-magic nil)))
3081 (widget-put widget :custom-magic magic)
3082 (push magic buttons))
3083 ;; Update buttons.
3084 (widget-put widget :buttons buttons)
3085 ;; Insert documentation.
3086 (widget-default-format-handler widget ?h)
3087 ;; The comment field
3088 (unless (eq state 'hidden)
3089 (let* ((comment (get symbol 'face-comment))
3090 (comment-widget
3091 (widget-create-child-and-convert
3092 widget 'custom-comment
3093 :parent widget
3094 :value (or comment ""))))
3095 (widget-put widget :comment-widget comment-widget)
3096 (push comment-widget children)))
3097 ;; See also.
3098 (unless (eq state 'hidden)
3099 (when (eq (widget-get widget :custom-level) 1)
3100 (custom-add-parent-links widget))
3101 (custom-add-see-also widget))
3102 ;; Editor.
3103 (unless (eq (preceding-char) ?\n)
3104 (insert "\n"))
3105 (unless (eq state 'hidden)
3106 (message "Creating face editor...")
3107 (custom-load-widget widget)
3108 (unless (widget-get widget :custom-form)
3109 (widget-put widget :custom-form custom-face-default-form))
3110 (let* ((symbol (widget-value widget))
3111 (spec (or (get symbol 'customized-face)
3112 (get symbol 'saved-face)
3113 (get symbol 'face-defface-spec)
3114 ;; Attempt to construct it.
3115 (list (list t (custom-face-attributes-get
3116 symbol (selected-frame))))))
3117 (form (widget-get widget :custom-form))
3118 (indent (widget-get widget :indent))
3119 edit)
3120 ;; If the user has changed this face in some other way,
3121 ;; edit it as the user has specified it.
3122 (if (not (face-spec-match-p symbol spec (selected-frame)))
3123 (setq spec (list (list t (face-attr-construct symbol (selected-frame))))))
3124 (setq spec (custom-pre-filter-face-spec spec))
3125 (setq edit (widget-create-child-and-convert
3126 widget
3127 (cond ((and (eq form 'selected)
3128 (widget-apply custom-face-selected
3129 :match spec))
3130 (when indent (insert-char ?\ indent))
3131 'custom-face-selected)
3132 ((and (not (eq form 'lisp))
3133 (widget-apply custom-face-all
3134 :match spec))
3135 'custom-face-all)
3136 (t
3137 (when indent (insert-char ?\ indent))
3138 'sexp))
3139 :value spec))
3140 (custom-face-state-set widget)
3141 (push edit children)
3142 (widget-put widget :children children))
3143 (message "Creating face editor...done"))))))
3144
3145 (defvar custom-face-menu
3146 '(("Set for Current Session" custom-face-set)
3147 ("Save for Future Sessions" custom-face-save-command)
3148 ("Reset to Saved" custom-face-reset-saved
3149 (lambda (widget)
3150 (or (get (widget-value widget) 'saved-face)
3151 (get (widget-value widget) 'saved-face-comment))))
3152 ("Erase Customization" custom-face-reset-standard
3153 (lambda (widget)
3154 (get (widget-value widget) 'face-defface-spec)))
3155 ("---" ignore ignore)
3156 ("Add Comment" custom-comment-show custom-comment-invisible-p)
3157 ("---" ignore ignore)
3158 ("Show all display specs" custom-face-edit-all
3159 (lambda (widget)
3160 (not (eq (widget-get widget :custom-form) 'all))))
3161 ("Just current attributes" custom-face-edit-selected
3162 (lambda (widget)
3163 (not (eq (widget-get widget :custom-form) 'selected))))
3164 ("Show as Lisp expression" custom-face-edit-lisp
3165 (lambda (widget)
3166 (not (eq (widget-get widget :custom-form) 'lisp)))))
3167 "Alist of actions for the `custom-face' widget.
3168 Each entry has the form (NAME ACTION FILTER) where NAME is the name of
3169 the menu entry, ACTION is the function to call on the widget when the
3170 menu is selected, and FILTER is a predicate which takes a `custom-face'
3171 widget as an argument, and returns non-nil if ACTION is valid on that
3172 widget. If FILTER is nil, ACTION is always valid.")
3173
3174 (defun custom-face-edit-selected (widget)
3175 "Edit selected attributes of the value of WIDGET."
3176 (widget-put widget :custom-state 'unknown)
3177 (widget-put widget :custom-form 'selected)
3178 (custom-redraw widget))
3179
3180 (defun custom-face-edit-all (widget)
3181 "Edit all attributes of the value of WIDGET."
3182 (widget-put widget :custom-state 'unknown)
3183 (widget-put widget :custom-form 'all)
3184 (custom-redraw widget))
3185
3186 (defun custom-face-edit-lisp (widget)
3187 "Edit the Lisp representation of the value of WIDGET."
3188 (widget-put widget :custom-state 'unknown)
3189 (widget-put widget :custom-form 'lisp)
3190 (custom-redraw widget))
3191
3192 (defun custom-face-state-set (widget)
3193 "Set the state of WIDGET."
3194 (let* ((symbol (widget-value widget))
3195 (comment (get symbol 'face-comment))
3196 tmp temp
3197 (state
3198 (cond ((progn
3199 (setq tmp (get symbol 'customized-face))
3200 (setq temp (get symbol 'customized-face-comment))
3201 (or tmp temp))
3202 (if (equal temp comment)
3203 'set
3204 'changed))
3205 ((progn
3206 (setq tmp (get symbol 'saved-face))
3207 (setq temp (get symbol 'saved-face-comment))
3208 (or tmp temp))
3209 (if (equal temp comment)
3210 'saved
3211 'changed))
3212 ((get symbol 'face-defface-spec)
3213 (if (equal comment nil)
3214 'standard
3215 'changed))
3216 (t
3217 'rogue))))
3218 ;; If the user called set-face-attribute to change the default
3219 ;; for new frames, this face is "set outside of Customize".
3220 (if (and (not (eq state 'rogue))
3221 (get symbol 'face-modified))
3222 (setq state 'changed))
3223 (widget-put widget :custom-state state)))
3224
3225 (defun custom-face-action (widget &optional event)
3226 "Show the menu for `custom-face' WIDGET.
3227 Optional EVENT is the location for the menu."
3228 (if (eq (widget-get widget :custom-state) 'hidden)
3229 (custom-toggle-hide widget)
3230 (let* ((completion-ignore-case t)
3231 (symbol (widget-get widget :value))
3232 (answer (widget-choose (concat "Operation on "
3233 (custom-unlispify-tag-name symbol))
3234 (custom-menu-filter custom-face-menu
3235 widget)
3236 event)))
3237 (if answer
3238 (funcall answer widget)))))
3239
3240 (defun custom-face-set (widget)
3241 "Make the face attributes in WIDGET take effect."
3242 (let* ((symbol (widget-value widget))
3243 (child (car (widget-get widget :children)))
3244 (value (custom-post-filter-face-spec (widget-value child)))
3245 (comment-widget (widget-get widget :comment-widget))
3246 (comment (widget-value comment-widget)))
3247 (when (equal comment "")
3248 (setq comment nil)
3249 ;; Make the comment invisible by hand if it's empty
3250 (custom-comment-hide comment-widget))
3251 (put symbol 'customized-face value)
3252 (if (face-spec-choose value)
3253 (face-spec-set symbol value)
3254 ;; face-set-spec ignores empty attribute lists, so just give it
3255 ;; something harmless instead.
3256 (face-spec-set symbol '((t :foreground unspecified))))
3257 (put symbol 'customized-face-comment comment)
3258 (put symbol 'face-comment comment)
3259 (custom-face-state-set widget)
3260 (custom-redraw-magic widget)))
3261
3262 (defun custom-face-save-command (widget)
3263 "Save in `.emacs' the face attributes in WIDGET."
3264 (custom-face-save widget)
3265 (custom-save-all))
3266
3267 (defun custom-face-save (widget)
3268 "Prepare for saving WIDGET's face attributes, but don't write `.emacs'."
3269 (let* ((symbol (widget-value widget))
3270 (child (car (widget-get widget :children)))
3271 (value (custom-post-filter-face-spec (widget-value child)))
3272 (comment-widget (widget-get widget :comment-widget))
3273 (comment (widget-value comment-widget)))
3274 (when (equal comment "")
3275 (setq comment nil)
3276 ;; Make the comment invisible by hand if it's empty
3277 (custom-comment-hide comment-widget))
3278 (if (face-spec-choose value)
3279 (face-spec-set symbol value)
3280 ;; face-set-spec ignores empty attribute lists, so just give it
3281 ;; something harmless instead.
3282 (face-spec-set symbol '((t :foreground unspecified))))
3283 (unless (eq (widget-get widget :custom-state) 'standard)
3284 (put symbol 'saved-face value))
3285 (custom-push-theme 'theme-face symbol 'user 'set value)
3286 (put symbol 'customized-face nil)
3287 (put symbol 'face-comment comment)
3288 (put symbol 'customized-face-comment nil)
3289 (put symbol 'saved-face-comment comment)
3290 (custom-save-all)
3291 (custom-face-state-set widget)
3292 (custom-redraw-magic widget)))
3293
3294 (defun custom-face-reset-saved (widget)
3295 "Restore WIDGET to the face's default attributes."
3296 (let* ((symbol (widget-value widget))
3297 (child (car (widget-get widget :children)))
3298 (value (get symbol 'saved-face))
3299 (comment (get symbol 'saved-face-comment))
3300 (comment-widget (widget-get widget :comment-widget)))
3301 (unless (or value comment)
3302 (error "No saved value for this face"))
3303 (put symbol 'customized-face nil)
3304 (put symbol 'customized-face-comment nil)
3305 (face-spec-set symbol value)
3306 (put symbol 'face-comment comment)
3307 (widget-value-set child value)
3308 ;; This call manages the comment visibility
3309 (widget-value-set comment-widget (or comment ""))
3310 (custom-face-state-set widget)
3311 (custom-redraw-magic widget)))
3312
3313 (defun custom-face-standard-value (widget)
3314 (get (widget-value widget) 'face-defface-spec))
3315
3316 (defun custom-face-reset-standard (widget)
3317 "Restore WIDGET to the face's standard settings.
3318 This operation eliminates any saved setting for the face,
3319 restoring it to the state of a face that has never been customized."
3320 (let* ((symbol (widget-value widget))
3321 (child (car (widget-get widget :children)))
3322 (value (get symbol 'face-defface-spec))
3323 (comment-widget (widget-get widget :comment-widget)))
3324 (unless value
3325 (error "No standard setting for this face"))
3326 (put symbol 'customized-face nil)
3327 (put symbol 'customized-face-comment nil)
3328 (when (or (get symbol 'saved-face) (get symbol 'saved-face-comment))
3329 (put symbol 'saved-face nil)
3330 (custom-push-theme 'theme-face symbol 'user 'reset 'standard)
3331 ;; Do not explictly save resets to standards without themes.
3332 (if (null (cdr (get symbol 'theme-face)))
3333 (put symbol 'theme-face nil))
3334 (put symbol 'saved-face-comment nil)
3335 (custom-save-all))
3336 (face-spec-set symbol value)
3337 (put symbol 'face-comment nil)
3338 (widget-value-set child value)
3339 ;; This call manages the comment visibility
3340 (widget-value-set comment-widget "")
3341 (custom-face-state-set widget)
3342 (custom-redraw-magic widget)))
3343
3344 ;;; The `face' Widget.
3345
3346 (defvar widget-face-prompt-value-history nil
3347 "History of input to `widget-face-prompt-value'.")
3348
3349 (define-widget 'face 'symbol
3350 "A Lisp face name (with sample)."
3351 :format "%t: (%{sample%}) %v"
3352 :tag "Face"
3353 :value 'default
3354 :sample-face-get 'widget-face-sample-face-get
3355 :notify 'widget-face-notify
3356 :match (lambda (widget value) (facep value))
3357 :complete-function (lambda ()
3358 (interactive)
3359 (lisp-complete-symbol 'facep))
3360 :prompt-match 'facep
3361 :prompt-history 'widget-face-prompt-value-history
3362 :validate (lambda (widget)
3363 (unless (facep (widget-value widget))
3364 (widget-put widget
3365 :error (format "Invalid face: %S"
3366 (widget-value widget)))
3367 widget)))
3368
3369 (defun widget-face-sample-face-get (widget)
3370 (let ((value (widget-value widget)))
3371 (if (facep value)
3372 value
3373 'default)))
3374
3375 (defun widget-face-notify (widget child &optional event)
3376 "Update the sample, and notify the parent."
3377 (overlay-put (widget-get widget :sample-overlay)
3378 'face (widget-apply widget :sample-face-get))
3379 (widget-default-notify widget child event))
3380
3381
3382 ;;; The `hook' Widget.
3383
3384 (define-widget 'hook 'list
3385 "A emacs lisp hook"
3386 :value-to-internal (lambda (widget value)
3387 (if (and value (symbolp value))
3388 (list value)
3389 value))
3390 :match (lambda (widget value)
3391 (or (symbolp value)
3392 (widget-group-match widget value)))
3393 ;; Avoid adding undefined functions to the hook, especially for
3394 ;; things like `find-file-hook' or even more basic ones, to avoid
3395 ;; chaos.
3396 :set (lambda (symbol value)
3397 (dolist (elt value)
3398 (if (fboundp elt)
3399 (add-hook symbol elt))))
3400 :convert-widget 'custom-hook-convert-widget
3401 :tag "Hook")
3402
3403 (defun custom-hook-convert-widget (widget)
3404 ;; Handle `:options'.
3405 (let* ((options (widget-get widget :options))
3406 (other `(editable-list :inline t
3407 :entry-format "%i %d%v"
3408 (function :format " %v")))
3409 (args (if options
3410 (list `(checklist :inline t
3411 ,@(mapcar (lambda (entry)
3412 `(function-item ,entry))
3413 options))
3414 other)
3415 (list other))))
3416 (widget-put widget :args args)
3417 widget))
3418
3419 ;;; The `custom-group-link' Widget.
3420
3421 (define-widget 'custom-group-link 'link
3422 "Show parent in other window when activated."
3423 :help-echo "Create customization buffer for this group."
3424 :action 'custom-group-link-action)
3425
3426 (defun custom-group-link-action (widget &rest ignore)
3427 (customize-group (widget-value widget)))
3428
3429 ;;; The `custom-group' Widget.
3430
3431 (defcustom custom-group-tag-faces nil
3432 ;; In XEmacs, this ought to play games with font size.
3433 ;; Fixme: make it do so in Emacs.
3434 "Face used for group tags.
3435 The first member is used for level 1 groups, the second for level 2,
3436 and so forth. The remaining group tags are shown with `custom-group-tag'."
3437 :type '(repeat face)
3438 :group 'custom-faces)
3439
3440 (defface custom-group-tag-1
3441 `((((class color)
3442 (background dark))
3443 (:foreground "pink" :weight bold :height 1.2 :inherit variable-pitch))
3444 (((min-colors 88) (class color)
3445 (background light))
3446 (:foreground "red1" :weight bold :height 1.2 :inherit variable-pitch))
3447 (((class color)
3448 (background light))
3449 (:foreground "red" :weight bold :height 1.2 :inherit variable-pitch))
3450 (t (:weight bold)))
3451 "Face used for group tags."
3452 :group 'custom-faces)
3453 ;; backward-compatibility alias
3454 (put 'custom-group-tag-face-1 'face-alias 'custom-group-tag-1)
3455
3456 (defface custom-group-tag
3457 `((((class color)
3458 (background dark))
3459 (:foreground "light blue" :weight bold :height 1.2))
3460 (((min-colors 88) (class color)
3461 (background light))
3462 (:foreground "blue1" :weight bold :height 1.2))
3463 (((class color)
3464 (background light))
3465 (:foreground "blue" :weight bold :height 1.2))
3466 (t (:weight bold)))
3467 "Face used for low level group tags."
3468 :group 'custom-faces)
3469 ;; backward-compatibility alias
3470 (put 'custom-group-tag-face 'face-alias 'custom-group-tag)
3471
3472 (define-widget 'custom-group 'custom
3473 "Customize group."
3474 :format "%v"
3475 :sample-face-get 'custom-group-sample-face-get
3476 :documentation-property 'group-documentation
3477 :help-echo "Set or reset all members of this group."
3478 :value-create 'custom-group-value-create
3479 :action 'custom-group-action
3480 :custom-category 'group
3481 :custom-set 'custom-group-set
3482 :custom-save 'custom-group-save
3483 :custom-reset-current 'custom-group-reset-current
3484 :custom-reset-saved 'custom-group-reset-saved
3485 :custom-reset-standard 'custom-group-reset-standard
3486 :custom-menu 'custom-group-menu-create)
3487
3488 (defun custom-group-sample-face-get (widget)
3489 ;; Use :sample-face.
3490 (or (nth (1- (widget-get widget :custom-level)) custom-group-tag-faces)
3491 'custom-group-tag))
3492
3493 (define-widget 'custom-group-visibility 'visibility
3494 "An indicator and manipulator for hidden group contents."
3495 :create 'custom-group-visibility-create)
3496
3497 (defun custom-group-visibility-create (widget)
3498 (let ((visible (widget-value widget)))
3499 (if visible
3500 (insert "--------")))
3501 (widget-default-create widget))
3502
3503 (defun custom-group-members (symbol groups-only)
3504 "Return SYMBOL's custom group members.
3505 If GROUPS-ONLY non-nil, return only those members that are groups."
3506 (if (not groups-only)
3507 (get symbol 'custom-group)
3508 (let (members)
3509 (dolist (entry (get symbol 'custom-group))
3510 (when (eq (nth 1 entry) 'custom-group)
3511 (push entry members)))
3512 (nreverse members))))
3513
3514 (defun custom-group-value-create (widget)
3515 "Insert a customize group for WIDGET in the current buffer."
3516 (unless (eq (widget-get widget :custom-state) 'hidden)
3517 (custom-load-widget widget))
3518 (let* ((state (widget-get widget :custom-state))
3519 (level (widget-get widget :custom-level))
3520 ;; (indent (widget-get widget :indent))
3521 (prefix (widget-get widget :custom-prefix))
3522 (buttons (widget-get widget :buttons))
3523 (tag (widget-get widget :tag))
3524 (symbol (widget-value widget))
3525 (members (custom-group-members symbol
3526 (and (eq custom-buffer-style 'tree)
3527 custom-browse-only-groups))))
3528 (cond ((and (eq custom-buffer-style 'tree)
3529 (eq state 'hidden)
3530 (or members (custom-unloaded-widget-p widget)))
3531 (custom-browse-insert-prefix prefix)
3532 (push (widget-create-child-and-convert
3533 widget 'custom-browse-visibility
3534 ;; :tag-glyph "plus"
3535 :tag "+")
3536 buttons)
3537 (insert "-- ")
3538 ;; (widget-glyph-insert nil "-- " "horizontal")
3539 (push (widget-create-child-and-convert
3540 widget 'custom-browse-group-tag)
3541 buttons)
3542 (insert " " tag "\n")
3543 (widget-put widget :buttons buttons))
3544 ((and (eq custom-buffer-style 'tree)
3545 (zerop (length members)))
3546 (custom-browse-insert-prefix prefix)
3547 (insert "[ ]-- ")
3548 ;; (widget-glyph-insert nil "[ ]" "empty")
3549 ;; (widget-glyph-insert nil "-- " "horizontal")
3550 (push (widget-create-child-and-convert
3551 widget 'custom-browse-group-tag)
3552 buttons)
3553 (insert " " tag "\n")
3554 (widget-put widget :buttons buttons))
3555 ((eq custom-buffer-style 'tree)
3556 (custom-browse-insert-prefix prefix)
3557 (if (zerop (length members))
3558 (progn
3559 (custom-browse-insert-prefix prefix)
3560 (insert "[ ]-- ")
3561 ;; (widget-glyph-insert nil "[ ]" "empty")
3562 ;; (widget-glyph-insert nil "-- " "horizontal")
3563 (push (widget-create-child-and-convert
3564 widget 'custom-browse-group-tag)
3565 buttons)
3566 (insert " " tag "\n")
3567 (widget-put widget :buttons buttons))
3568 (push (widget-create-child-and-convert
3569 widget 'custom-browse-visibility
3570 ;; :tag-glyph "minus"
3571 :tag "-")
3572 buttons)
3573 (insert "-\\ ")
3574 ;; (widget-glyph-insert nil "-\\ " "top")
3575 (push (widget-create-child-and-convert
3576 widget 'custom-browse-group-tag)
3577 buttons)
3578 (insert " " tag "\n")
3579 (widget-put widget :buttons buttons)
3580 (message "Creating group...")
3581 (let* ((members (custom-sort-items members
3582 custom-browse-sort-alphabetically
3583 custom-browse-order-groups))
3584 (prefixes (widget-get widget :custom-prefixes))
3585 (custom-prefix-list (custom-prefix-add symbol prefixes))
3586 (extra-prefix (if (widget-get widget :custom-last)
3587 " "
3588 " | "))
3589 (prefix (concat prefix extra-prefix))
3590 children entry)
3591 (while members
3592 (setq entry (car members)
3593 members (cdr members))
3594 (push (widget-create-child-and-convert
3595 widget (nth 1 entry)
3596 :group widget
3597 :tag (custom-unlispify-tag-name (nth 0 entry))
3598 :custom-prefixes custom-prefix-list
3599 :custom-level (1+ level)
3600 :custom-last (null members)
3601 :value (nth 0 entry)
3602 :custom-prefix prefix)
3603 children))
3604 (widget-put widget :children (reverse children)))
3605 (message "Creating group...done")))
3606 ;; Nested style.
3607 ((eq state 'hidden)
3608 ;; Create level indicator.
3609 (unless (eq custom-buffer-style 'links)
3610 (insert-char ?\ (* custom-buffer-indent (1- level)))
3611 (insert "-- "))
3612 ;; Create tag.
3613 (let ((begin (point)))
3614 (insert tag)
3615 (widget-specify-sample widget begin (point)))
3616 (insert " group: ")
3617 ;; Create link/visibility indicator.
3618 (if (eq custom-buffer-style 'links)
3619 (push (widget-create-child-and-convert
3620 widget 'custom-group-link
3621 :tag "Go to Group"
3622 symbol)
3623 buttons)
3624 (push (widget-create-child-and-convert
3625 widget 'custom-group-visibility
3626 :help-echo "Show members of this group."
3627 :action 'custom-toggle-parent
3628 (not (eq state 'hidden)))
3629 buttons))
3630 (insert " \n")
3631 ;; Create magic button.
3632 (let ((magic (widget-create-child-and-convert
3633 widget 'custom-magic nil)))
3634 (widget-put widget :custom-magic magic)
3635 (push magic buttons))
3636 ;; Update buttons.
3637 (widget-put widget :buttons buttons)
3638 ;; Insert documentation.
3639 (if (and (eq custom-buffer-style 'links) (> level 1))
3640 (widget-put widget :documentation-indent 0))
3641 (widget-default-format-handler widget ?h))
3642 ;; Nested style.
3643 (t ;Visible.
3644 ;; Add parent groups references above the group.
3645 (if t ;;; This should test that the buffer
3646 ;;; was made to display a group.
3647 (when (eq level 1)
3648 (if (custom-add-parent-links widget
3649 "Go to parent group:")
3650 (insert "\n"))))
3651 ;; Create level indicator.
3652 (insert-char ?\ (* custom-buffer-indent (1- level)))
3653 (insert "/- ")
3654 ;; Create tag.
3655 (let ((start (point)))
3656 (insert tag)
3657 (widget-specify-sample widget start (point)))
3658 (insert " group: ")
3659 ;; Create visibility indicator.
3660 (unless (eq custom-buffer-style 'links)
3661 (insert "--------")
3662 (push (widget-create-child-and-convert
3663 widget 'visibility
3664 :help-echo "Hide members of this group."
3665 :action 'custom-toggle-parent
3666 (not (eq state 'hidden)))
3667 buttons)
3668 (insert " "))
3669 ;; Create more dashes.
3670 ;; Use 76 instead of 75 to compensate for the temporary "<"
3671 ;; added by `widget-insert'.
3672 (insert-char ?- (- 76 (current-column)
3673 (* custom-buffer-indent level)))
3674 (insert "\\\n")
3675 ;; Create magic button.
3676 (let ((magic (widget-create-child-and-convert
3677 widget 'custom-magic
3678 :indent 0
3679 nil)))
3680 (widget-put widget :custom-magic magic)
3681 (push magic buttons))
3682 ;; Update buttons.
3683 (widget-put widget :buttons buttons)
3684 ;; Insert documentation.
3685 (widget-default-format-handler widget ?h)
3686 ;; Parent groups.
3687 (if nil ;;; This should test that the buffer
3688 ;;; was not made to display a group.
3689 (when (eq level 1)
3690 (insert-char ?\ custom-buffer-indent)
3691 (custom-add-parent-links widget)))
3692 (custom-add-see-also widget
3693 (make-string (* custom-buffer-indent level)
3694 ?\ ))
3695 ;; Members.
3696 (message "Creating group...")
3697 (let* ((members (custom-sort-items members
3698 custom-buffer-sort-alphabetically
3699 custom-buffer-order-groups))
3700 (prefixes (widget-get widget :custom-prefixes))
3701 (custom-prefix-list (custom-prefix-add symbol prefixes))
3702 (length (length members))
3703 (count 0)
3704 (children (mapcar (lambda (entry)
3705 (widget-insert "\n")
3706 (message "\
3707 Creating group members... %2d%%"
3708 (/ (* 100.0 count) length))
3709 (setq count (1+ count))
3710 (prog1
3711 (widget-create-child-and-convert
3712 widget (nth 1 entry)
3713 :group widget
3714 :tag (custom-unlispify-tag-name
3715 (nth 0 entry))
3716 :custom-prefixes custom-prefix-list
3717 :custom-level (1+ level)
3718 :value (nth 0 entry))
3719 (unless (eq (preceding-char) ?\n)
3720 (widget-insert "\n"))))
3721 members)))
3722 (message "Creating group magic...")
3723 (mapc 'custom-magic-reset children)
3724 (message "Creating group state...")
3725 (widget-put widget :children children)
3726 (custom-group-state-update widget)
3727 (message "Creating group... done"))
3728 ;; End line
3729 (insert "\n")
3730 (insert-char ?\ (* custom-buffer-indent (1- level)))
3731 (insert "\\- " (widget-get widget :tag) " group end ")
3732 (insert-char ?- (- 75 (current-column) (* custom-buffer-indent level)))
3733 (insert "/\n")))))
3734
3735 (defvar custom-group-menu
3736 '(("Set for Current Session" custom-group-set
3737 (lambda (widget)
3738 (eq (widget-get widget :custom-state) 'modified)))
3739 ("Save for Future Sessions" custom-group-save
3740 (lambda (widget)
3741 (memq (widget-get widget :custom-state) '(modified set))))
3742 ("Reset to Current" custom-group-reset-current
3743 (lambda (widget)
3744 (memq (widget-get widget :custom-state) '(modified))))
3745 ("Reset to Saved" custom-group-reset-saved
3746 (lambda (widget)
3747 (memq (widget-get widget :custom-state) '(modified set))))
3748 ("Reset to standard setting" custom-group-reset-standard
3749 (lambda (widget)
3750 (memq (widget-get widget :custom-state) '(modified set saved)))))
3751 "Alist of actions for the `custom-group' widget.
3752 Each entry has the form (NAME ACTION FILTER) where NAME is the name of
3753 the menu entry, ACTION is the function to call on the widget when the
3754 menu is selected, and FILTER is a predicate which takes a `custom-group'
3755 widget as an argument, and returns non-nil if ACTION is valid on that
3756 widget. If FILTER is nil, ACTION is always valid.")
3757
3758 (defun custom-group-action (widget &optional event)
3759 "Show the menu for `custom-group' WIDGET.
3760 Optional EVENT is the location for the menu."
3761 (if (eq (widget-get widget :custom-state) 'hidden)
3762 (custom-toggle-hide widget)
3763 (let* ((completion-ignore-case t)
3764 (answer (widget-choose (concat "Operation on "
3765 (custom-unlispify-tag-name
3766 (widget-get widget :value)))
3767 (custom-menu-filter custom-group-menu
3768 widget)
3769 event)))
3770 (if answer
3771 (funcall answer widget)))))
3772
3773 (defun custom-group-set (widget)
3774 "Set changes in all modified group members."
3775 (let ((children (widget-get widget :children)))
3776 (mapc (lambda (child)
3777 (when (eq (widget-get child :custom-state) 'modified)
3778 (widget-apply child :custom-set)))
3779 children )))
3780
3781 (defun custom-group-save (widget)
3782 "Save all modified group members."
3783 (let ((children (widget-get widget :children)))
3784 (mapc (lambda (child)
3785 (when (memq (widget-get child :custom-state) '(modified set))
3786 (widget-apply child :custom-save)))
3787 children )))
3788
3789 (defun custom-group-reset-current (widget)
3790 "Reset all modified group members."
3791 (let ((children (widget-get widget :children)))
3792 (mapc (lambda (child)
3793 (when (eq (widget-get child :custom-state) 'modified)
3794 (widget-apply child :custom-reset-current)))
3795 children )))
3796
3797 (defun custom-group-reset-saved (widget)
3798 "Reset all modified or set group members."
3799 (let ((children (widget-get widget :children)))
3800 (mapc (lambda (child)
3801 (when (memq (widget-get child :custom-state) '(modified set))
3802 (widget-apply child :custom-reset-saved)))
3803 children )))
3804
3805 (defun custom-group-reset-standard (widget)
3806 "Reset all modified, set, or saved group members."
3807 (let ((children (widget-get widget :children)))
3808 (mapc (lambda (child)
3809 (when (memq (widget-get child :custom-state)
3810 '(modified set saved))
3811 (widget-apply child :custom-reset-standard)))
3812 children )))
3813
3814 (defun custom-group-state-update (widget)
3815 "Update magic."
3816 (unless (eq (widget-get widget :custom-state) 'hidden)
3817 (let* ((children (widget-get widget :children))
3818 (states (mapcar (lambda (child)
3819 (widget-get child :custom-state))
3820 children))
3821 (magics custom-magic-alist)
3822 (found 'standard))
3823 (while magics
3824 (let ((magic (car (car magics))))
3825 (if (and (not (eq magic 'hidden))
3826 (memq magic states))
3827 (setq found magic
3828 magics nil)
3829 (setq magics (cdr magics)))))
3830 (widget-put widget :custom-state found)))
3831 (custom-magic-reset widget))
3832
3833 ;;; The `custom-save-all' Function.
3834 ;;;###autoload
3835 (defcustom custom-file nil
3836 "File used for storing customization information.
3837 The default is nil, which means to use your init file
3838 as specified by `user-init-file'. If the value is not nil,
3839 it should be an absolute file name.
3840
3841 You can set this option through Custom, if you carefully read the
3842 last paragraph below. However, usually it is simpler to write
3843 something like the following in your init file:
3844
3845 \(setq custom-file \"~/.emacs-custom.el\")
3846 \(load custom-file)
3847
3848 Note that both lines are necessary: the first line tells Custom to
3849 save all customizations in this file, but does not load it.
3850
3851 When you change this variable outside Custom, look in the
3852 previous custom file \(usually your init file) for the
3853 forms `(custom-set-variables ...)' and `(custom-set-faces ...)',
3854 and copy them (whichever ones you find) to the new custom file.
3855 This will preserve your existing customizations.
3856
3857 If you save this option using Custom, Custom will write all
3858 currently saved customizations, including the new one for this
3859 option itself, into the file you specify, overwriting any
3860 `custom-set-variables' and `custom-set-faces' forms already
3861 present in that file. It will not delete any customizations from
3862 the old custom file. You should do that manually if that is what you
3863 want. You also have to put something like `\(load \"CUSTOM-FILE\")
3864 in your init file, where CUSTOM-FILE is the actual name of the
3865 file. Otherwise, Emacs will not load the file when it starts up,
3866 and hence will not set `custom-file' to that file either."
3867 :type '(choice (const :tag "Your Emacs init file" nil)
3868 (file :format "%t:%v%d"
3869 :doc
3870 "Please read entire docstring below before setting \
3871 this through Custom.
3872 Click om \"More\" \(or position point there and press RETURN)
3873 if only the first line of the docstring is shown."))
3874 :group 'customize)
3875
3876 (defun custom-file ()
3877 "Return the file name for saving customizations."
3878 (file-chase-links
3879 (or custom-file
3880 (let ((user-init-file user-init-file)
3881 (default-init-file
3882 (if (eq system-type 'ms-dos) "~/_emacs" "~/.emacs")))
3883 (when (null user-init-file)
3884 (if (or (file-exists-p default-init-file)
3885 (and (eq system-type 'windows-nt)
3886 (file-exists-p "~/_emacs")))
3887 ;; Started with -q, i.e. the file containing
3888 ;; Custom settings hasn't been read. Saving
3889 ;; settings there would overwrite other settings.
3890 (error "Saving settings from \"emacs -q\" would overwrite existing customizations"))
3891 (setq user-init-file default-init-file))
3892 user-init-file))))
3893
3894 (defun custom-save-delete (symbol)
3895 "Visit `custom-file' and delete all calls to SYMBOL from it.
3896 Leave point at the old location of the first such call,
3897 or (if there were none) at the end of the buffer."
3898 (let ((default-major-mode 'emacs-lisp-mode))
3899 (set-buffer (find-file-noselect (custom-file))))
3900 (goto-char (point-min))
3901 ;; Skip all whitespace and comments.
3902 (while (forward-comment 1))
3903 (or (eobp)
3904 (save-excursion (forward-sexp (buffer-size)))) ; Test for scan errors.
3905 (let (first)
3906 (catch 'found
3907 (while t ;; We exit this loop only via throw.
3908 ;; Skip all whitespace and comments.
3909 (while (forward-comment 1))
3910 (let ((start (point))
3911 (sexp (condition-case nil
3912 (read (current-buffer))
3913 (end-of-file (throw 'found nil)))))
3914 (when (and (listp sexp)
3915 (eq (car sexp) symbol))
3916 (delete-region start (point))
3917 (unless first
3918 (setq first (point)))))))
3919 (if first
3920 (goto-char first)
3921 ;; Move in front of local variables, otherwise long Custom
3922 ;; entries would make them ineffective.
3923 (let ((pos (point-max))
3924 (case-fold-search t))
3925 (save-excursion
3926 (goto-char (point-max))
3927 (search-backward "\n\^L" (max (- (point-max) 3000) (point-min))
3928 'move)
3929 (when (search-forward "Local Variables:" nil t)
3930 (setq pos (line-beginning-position))))
3931 (goto-char pos)))))
3932
3933 (defun custom-save-variables ()
3934 "Save all customized variables in `custom-file'."
3935 (save-excursion
3936 (custom-save-delete 'custom-load-themes)
3937 (custom-save-delete 'custom-reset-variables)
3938 (custom-save-delete 'custom-set-variables)
3939 (custom-save-loaded-themes)
3940 (custom-save-resets 'theme-value 'custom-reset-variables nil)
3941 (let ((standard-output (current-buffer))
3942 (saved-list (make-list 1 0))
3943 sort-fold-case)
3944 ;; First create a sorted list of saved variables.
3945 (mapatoms
3946 (lambda (symbol)
3947 (if (get symbol 'saved-value)
3948 (nconc saved-list (list symbol)))))
3949 (setq saved-list (sort (cdr saved-list) 'string<))
3950 (unless (bolp)
3951 (princ "\n"))
3952 (princ "(custom-set-variables
3953 ;; custom-set-variables was added by Custom.
3954 ;; If you edit it by hand, you could mess it up, so be careful.
3955 ;; Your init file should contain only one such instance.
3956 ;; If there is more than one, they won't work right.\n")
3957 (dolist (symbol saved-list)
3958 (let ((spec (car-safe (get symbol 'theme-value)))
3959 (value (get symbol 'saved-value))
3960 (requests (get symbol 'custom-requests))
3961 (now (not (or (custom-variable-p symbol)
3962 (and (not (boundp symbol))
3963 (not (eq (get symbol 'force-value)
3964 'rogue))))))
3965 (comment (get symbol 'saved-variable-comment)))
3966 ;; Check `requests'.
3967 (dolist (request requests)
3968 (when (and (symbolp request) (not (featurep request)))
3969 (message "Unknown requested feature: %s" request)
3970 (setq requests (delq request requests))))
3971 (when (or (and spec
3972 (eq (nth 0 spec) 'user)
3973 (eq (nth 1 spec) 'set))
3974 comment
3975 (and (null spec) (get symbol 'saved-value)))
3976 (unless (bolp)
3977 (princ "\n"))
3978 (princ " '(")
3979 (prin1 symbol)
3980 (princ " ")
3981 (prin1 (car value))
3982 (when (or now requests comment)
3983 (princ " ")
3984 (prin1 now)
3985 (when (or requests comment)
3986 (princ " ")
3987 (prin1 requests)
3988 (when comment
3989 (princ " ")
3990 (prin1 comment))))
3991 (princ ")"))))
3992 (if (bolp)
3993 (princ " "))
3994 (princ ")")
3995 (unless (looking-at "\n")
3996 (princ "\n")))))
3997
3998 (defun custom-save-faces ()
3999 "Save all customized faces in `custom-file'."
4000 (save-excursion
4001 (custom-save-delete 'custom-reset-faces)
4002 (custom-save-delete 'custom-set-faces)
4003 (custom-save-resets 'theme-face 'custom-reset-faces '(default))
4004 (let ((standard-output (current-buffer))
4005 (saved-list (make-list 1 0))
4006 sort-fold-case)
4007 ;; First create a sorted list of saved faces.
4008 (mapatoms
4009 (lambda (symbol)
4010 (if (get symbol 'saved-face)
4011 (nconc saved-list (list symbol)))))
4012 (setq saved-list (sort (cdr saved-list) 'string<))
4013 ;; The default face must be first, since it affects the others.
4014 (if (memq 'default saved-list)
4015 (setq saved-list (cons 'default (delq 'default saved-list))))
4016 (unless (bolp)
4017 (princ "\n"))
4018 (princ "(custom-set-faces
4019 ;; custom-set-faces was added by Custom.
4020 ;; If you edit it by hand, you could mess it up, so be careful.
4021 ;; Your init file should contain only one such instance.
4022 ;; If there is more than one, they won't work right.\n")
4023 (dolist (symbol saved-list)
4024 (let ((spec (car-safe (get symbol 'theme-face)))
4025 (value (get symbol 'saved-face))
4026 (now (not (or (get symbol 'face-defface-spec)
4027 (and (not (custom-facep symbol))
4028 (not (get symbol 'force-face))))))
4029 (comment (get symbol 'saved-face-comment)))
4030 (when (or (and spec
4031 (eq (nth 0 spec) 'user)
4032 (eq (nth 1 spec) 'set))
4033 comment
4034 (and (null spec) (get symbol 'saved-face)))
4035 ;; Don't print default face here.
4036 (unless (bolp)
4037 (princ "\n"))
4038 (princ " '(")
4039 (prin1 symbol)
4040 (princ " ")
4041 (prin1 value)
4042 (when (or now comment)
4043 (princ " ")
4044 (prin1 now)
4045 (when comment
4046 (princ " ")
4047 (prin1 comment)))
4048 (princ ")"))))
4049 (if (bolp)
4050 (princ " "))
4051 (princ ")")
4052 (unless (looking-at "\n")
4053 (princ "\n")))))
4054
4055 (defun custom-save-resets (property setter special)
4056 (let (started-writing ignored-special)
4057 ;; (custom-save-delete setter) Done by caller
4058 (let ((standard-output (current-buffer))
4059 (mapper `(lambda (object)
4060 (let ((spec (car-safe (get object (quote ,property)))))
4061 (when (and (not (memq object ignored-special))
4062 (eq (nth 0 spec) 'user)
4063 (eq (nth 1 spec) 'reset))
4064 ;; Do not write reset statements unless necessary.
4065 (unless started-writing
4066 (setq started-writing t)
4067 (unless (bolp)
4068 (princ "\n"))
4069 (princ "(")
4070 (princ (quote ,setter))
4071 (princ "\n '(")
4072 (prin1 object)
4073 (princ " ")
4074 (prin1 (nth 3 spec))
4075 (princ ")")))))))
4076 (mapc mapper special)
4077 (setq ignored-special special)
4078 (mapatoms mapper)
4079 (when started-writing
4080 (princ ")\n")))))
4081
4082 (defun custom-save-loaded-themes ()
4083 (let ((themes (reverse (get 'user 'theme-loads-themes)))
4084 (standard-output (current-buffer)))
4085 (when themes
4086 (unless (bolp) (princ "\n"))
4087 (princ "(custom-load-themes")
4088 (mapc (lambda (theme)
4089 (princ "\n '")
4090 (prin1 theme)) themes)
4091 (princ " )\n"))))
4092
4093 ;;;###autoload
4094 (defun customize-save-customized ()
4095 "Save all user options which have been set in this session."
4096 (interactive)
4097 (mapatoms (lambda (symbol)
4098 (let ((face (get symbol 'customized-face))
4099 (value (get symbol 'customized-value))
4100 (face-comment (get symbol 'customized-face-comment))
4101 (variable-comment
4102 (get symbol 'customized-variable-comment)))
4103 (when face
4104 (put symbol 'saved-face face)
4105 (custom-push-theme 'theme-face symbol 'user 'set value)
4106 (put symbol 'customized-face nil))
4107 (when value
4108 (put symbol 'saved-value value)
4109 (custom-push-theme 'theme-value symbol 'user 'set value)
4110 (put symbol 'customized-value nil))
4111 (when variable-comment
4112 (put symbol 'saved-variable-comment variable-comment)
4113 (put symbol 'customized-variable-comment nil))
4114 (when face-comment
4115 (put symbol 'saved-face-comment face-comment)
4116 (put symbol 'customized-face-comment nil)))))
4117 ;; We really should update all custom buffers here.
4118 (custom-save-all))
4119
4120 ;;;###autoload
4121 (defun custom-save-all ()
4122 "Save all customizations in `custom-file'."
4123 (let ((inhibit-read-only t))
4124 (custom-save-variables)
4125 (custom-save-faces)
4126 (save-excursion
4127 (let ((default-major-mode nil))
4128 (set-buffer (find-file-noselect (custom-file))))
4129 (let ((file-precious-flag t))
4130 (save-buffer)))))
4131
4132 ;;; The Customize Menu.
4133
4134 ;;; Menu support
4135
4136 (defcustom custom-menu-nesting 2
4137 "Maximum nesting in custom menus."
4138 :type 'integer
4139 :group 'custom-menu)
4140
4141 (defun custom-face-menu-create (widget symbol)
4142 "Ignoring WIDGET, create a menu entry for customization face SYMBOL."
4143 (vector (custom-unlispify-menu-entry symbol)
4144 `(customize-face ',symbol)
4145 t))
4146
4147 (defun custom-variable-menu-create (widget symbol)
4148 "Ignoring WIDGET, create a menu entry for customization variable SYMBOL."
4149 (let ((type (get symbol 'custom-type)))
4150 (unless (listp type)
4151 (setq type (list type)))
4152 (if (and type (widget-get type :custom-menu))
4153 (widget-apply type :custom-menu symbol)
4154 (vector (custom-unlispify-menu-entry symbol)
4155 `(customize-variable ',symbol)
4156 t))))
4157
4158 ;; Add checkboxes to boolean variable entries.
4159 (widget-put (get 'boolean 'widget-type)
4160 :custom-menu (lambda (widget symbol)
4161 (vector (custom-unlispify-menu-entry symbol)
4162 `(customize-variable ',symbol)
4163 ':style 'toggle
4164 ':selected symbol)))
4165
4166 (defun custom-group-menu-create (widget symbol)
4167 "Ignoring WIDGET, create a menu entry for customization group SYMBOL."
4168 `( ,(custom-unlispify-menu-entry symbol t)
4169 :filter (lambda (&rest junk)
4170 (let ((menu (custom-menu-create ',symbol)))
4171 (if (consp menu) (cdr menu) menu)))))
4172
4173 ;;;###autoload
4174 (defun custom-menu-create (symbol)
4175 "Create menu for customization group SYMBOL.
4176 The menu is in a format applicable to `easy-menu-define'."
4177 (let* ((item (vector (custom-unlispify-menu-entry symbol)
4178 `(customize-group ',symbol)
4179 t)))
4180 (if (and (or (not (boundp 'custom-menu-nesting))
4181 (>= custom-menu-nesting 0))
4182 (progn
4183 (custom-load-symbol symbol)
4184 (< (length (get symbol 'custom-group)) widget-menu-max-size)))
4185 (let ((custom-prefix-list (custom-prefix-add symbol
4186 custom-prefix-list))
4187 (members (custom-sort-items (get symbol 'custom-group)
4188 custom-menu-sort-alphabetically
4189 custom-menu-order-groups)))
4190 `(,(custom-unlispify-menu-entry symbol t)
4191 ,item
4192 "--"
4193 ,@(mapcar (lambda (entry)
4194 (widget-apply (if (listp (nth 1 entry))
4195 (nth 1 entry)
4196 (list (nth 1 entry)))
4197 :custom-menu (nth 0 entry)))
4198 members)))
4199 item)))
4200
4201 ;;;###autoload
4202 (defun customize-menu-create (symbol &optional name)
4203 "Return a customize menu for customization group SYMBOL.
4204 If optional NAME is given, use that as the name of the menu.
4205 Otherwise the menu will be named `Customize'.
4206 The format is suitable for use with `easy-menu-define'."
4207 (unless name
4208 (setq name "Customize"))
4209 `(,name
4210 :filter (lambda (&rest junk)
4211 (let ((menu (custom-menu-create ',symbol)))
4212 (if (consp menu) (cdr menu) menu)))))
4213
4214 ;;; The Custom Mode.
4215
4216 (defvar custom-mode-map
4217 ;; This keymap should be dense, but a dense keymap would prevent inheriting
4218 ;; "\r" bindings from the parent map.
4219 ;; Actually, this misfeature of dense keymaps was fixed on 2001-11-26.
4220 (let ((map (make-keymap)))
4221 (set-keymap-parent map widget-keymap)
4222 (suppress-keymap map)
4223 (define-key map " " 'scroll-up)
4224 (define-key map "\177" 'scroll-down)
4225 (define-key map "\C-x\C-s" 'Custom-save)
4226 (define-key map "q" 'Custom-buffer-done)
4227 (define-key map "u" 'Custom-goto-parent)
4228 (define-key map "n" 'widget-forward)
4229 (define-key map "p" 'widget-backward)
4230 (define-key map [mouse-1] 'Custom-move-and-invoke)
4231 map)
4232 "Keymap for `custom-mode'.")
4233
4234 (defun Custom-move-and-invoke (event)
4235 "Move to where you click, and if it is an active field, invoke it."
4236 (interactive "e")
4237 (mouse-set-point event)
4238 (if (widget-event-point event)
4239 (let* ((pos (widget-event-point event))
4240 (button (get-char-property pos 'button)))
4241 (if button
4242 (widget-button-click event)))))
4243
4244 (easy-menu-define Custom-mode-menu
4245 custom-mode-map
4246 "Menu used in customization buffers."
4247 `("Custom"
4248 ,(customize-menu-create 'customize)
4249 ["Set" Custom-set t]
4250 ["Save" Custom-save t]
4251 ["Reset to Current" Custom-reset-current t]
4252 ["Reset to Saved" Custom-reset-saved t]
4253 ["Reset to Standard Settings" Custom-reset-standard t]
4254 ["Info" (Info-goto-node "(emacs)Easy Customization") t]))
4255
4256 (defun Custom-goto-parent ()
4257 "Go to the parent group listed at the top of this buffer.
4258 If several parents are listed, go to the first of them."
4259 (interactive)
4260 (save-excursion
4261 (goto-char (point-min))
4262 (if (search-forward "\nGo to parent group: " nil t)
4263 (let* ((button (get-char-property (point) 'button))
4264 (parent (downcase (widget-get button :tag))))
4265 (customize-group parent)))))
4266
4267 (defcustom custom-mode-hook nil
4268 "Hook called when entering Custom mode."
4269 :type 'hook
4270 :group 'custom-buffer )
4271
4272 (defun custom-state-buffer-message (widget)
4273 (if (eq (widget-get (widget-get widget :parent) :custom-state) 'modified)
4274 (message "To install your edits, invoke [State] and choose the Set operation")))
4275
4276 (defun custom-mode ()
4277 "Major mode for editing customization buffers.
4278
4279 The following commands are available:
4280
4281 Move to next button or editable field. \\[widget-forward]
4282 Move to previous button or editable field. \\[widget-backward]
4283 \\<widget-field-keymap>\
4284 Complete content of editable text field. \\[widget-complete]
4285 \\<custom-mode-map>\
4286 Invoke button under the mouse pointer. \\[Custom-move-and-invoke]
4287 Invoke button under point. \\[widget-button-press]
4288 Set all modifications. \\[Custom-set]
4289 Make all modifications default. \\[Custom-save]
4290 Reset all modified options. \\[Custom-reset-current]
4291 Reset all modified or set options. \\[Custom-reset-saved]
4292 Reset all options. \\[Custom-reset-standard]
4293
4294 Entry to this mode calls the value of `custom-mode-hook'
4295 if that value is non-nil."
4296 (kill-all-local-variables)
4297 (setq major-mode 'custom-mode
4298 mode-name "Custom")
4299 (use-local-map custom-mode-map)
4300 (easy-menu-add Custom-mode-menu)
4301 (make-local-variable 'custom-options)
4302 (make-local-variable 'custom-local-buffer)
4303 (make-local-variable 'widget-documentation-face)
4304 (setq widget-documentation-face 'custom-documentation)
4305 (make-local-variable 'widget-button-face)
4306 (setq widget-button-face 'custom-button)
4307 (set (make-local-variable 'widget-button-pressed-face) 'custom-button-pressed)
4308 (set (make-local-variable 'widget-mouse-face)
4309 'custom-button-pressed) ; buttons `depress' when moused
4310 ;; When possible, use relief for buttons, not bracketing. This test
4311 ;; may not be optimal.
4312 (when custom-raised-buttons
4313 (set (make-local-variable 'widget-push-button-prefix) "")
4314 (set (make-local-variable 'widget-push-button-suffix) "")
4315 (set (make-local-variable 'widget-link-prefix) "")
4316 (set (make-local-variable 'widget-link-suffix) ""))
4317 (add-hook 'widget-edit-functions 'custom-state-buffer-message nil t)
4318 (run-mode-hooks 'custom-mode-hook))
4319
4320 (put 'custom-mode 'mode-class 'special)
4321
4322 (add-to-list
4323 'debug-ignored-errors
4324 "^No user options have changed defaults in recent Emacs versions$")
4325
4326 ;;; The End.
4327
4328 (provide 'cus-edit)
4329
4330 ;; arch-tag: 64533aa4-1b1a-48c3-8812-f9dc718e8a6f
4331 ;;; cus-edit.el ends here