b27db594d252b9efbac4ddfba2397dd0124b2730
[chise/xemacs-chise.git.1] / lisp / cus-edit.el
1 ;;; cus-edit.el --- Tools for customizating Emacs and Lisp packages.
2 ;;
3 ;; Copyright (C) 1996, 1997, 2000 Free Software Foundation, Inc.
4 ;;
5 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
6 ;; Maintainer: Hrvoje Niksic <hniksic@xemacs.org>
7 ;; Keywords: help, faces
8 ;; Version: 1.9960-x
9 ;; X-URL: http://www.dina.kvl.dk/~abraham/custom/
10
11 ;; This file is part of XEmacs.
12
13 ;; XEmacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; any later version.
17
18 ;; XEmacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with XEmacs; see the file COPYING.  If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
27
28 ;;; Commentary:
29 ;;
30 ;; This file implements the code to create and edit customize buffers.
31 ;;
32 ;; See `custom.el'.
33
34 ;; No commands should have names starting with `custom-' because
35 ;; that interferes with completion.  Use `customize-' for commands
36 ;; that the user will run with M-x, and `Custom-' for interactive commands.
37
38 ;; NOTE: In many places within this file we use `mapatoms', which is
39 ;; very slow in an average XEmacs because of the large number of
40 ;; symbols requiring a large number of funcalls -- XEmacs with Gnus
41 ;; can grow to some 17000 symbols without ever doing anything fancy.
42 ;; It would probably pay off to make a hash table of symbols known to
43 ;; Custom, similar to custom-group-hash-table.
44
45 ;; This is not top priority, because none of the functions that do
46 ;; mapatoms are speed-critical (the one that was now uses
47 ;; custom-group-hash-table), but it would be nice to have.
48
49 \f
50 ;;; Code:
51
52 (require 'cus-face)
53 (require 'wid-edit)
54 (require 'easymenu)
55
56 (require 'cus-load)
57 (require 'cus-start)
58 (require 'cus-file)
59
60 ;; Huh?  This looks dirty!
61 (put 'custom-define-hook 'custom-type 'hook)
62 (put 'custom-define-hook 'standard-value '(nil))
63 (custom-add-to-group 'customize 'custom-define-hook 'custom-variable)
64
65 ;;; Customization Groups.
66
67 (defgroup emacs nil
68   "Customization of the One True Editor."
69   :link '(custom-manual "(XEmacs)Top"))
70
71 ;; Most of these groups are stolen from `finder.el',
72 (defgroup editing nil
73   "Basic text editing facilities."
74   :group 'emacs)
75
76 (defgroup matching nil
77   "Various sorts of searching and matching."
78   :group 'editing)
79
80 (defgroup emulations nil
81   "Emulations of other editors."
82   :group 'editing)
83
84 (defgroup outlines nil
85   "Support for hierarchical outlining."
86   :group 'editing)
87
88 (defgroup external nil
89   "Interfacing to external utilities."
90   :group 'emacs)
91
92 (defgroup bib nil
93   "Code related to the `bib' bibliography processor."
94   :tag "Bibliography"
95   :group 'external)
96
97 (defgroup programming nil
98   "Support for programming in other languages."
99   :group 'emacs)
100
101 (defgroup languages nil
102   "Specialized modes for editing programming languages."
103   :group 'programming)
104
105 ;; #### This should be in cc-vars.el
106 (defgroup c nil
107   "Support for the C language and related languages."
108   :group 'languages)
109
110 (defgroup tools nil
111   "Programming tools."
112   :group 'programming)
113
114 (defgroup oop nil
115   "Support for object-oriented programming."
116   :group 'programming)
117
118 (defgroup applications nil
119   "Applications written in Emacs."
120   :group 'emacs)
121
122 ;; #### This should be in calendar.el
123 (defgroup calendar nil
124   "Calendar and time management support."
125   :group 'applications)
126
127 (defgroup mail nil
128   "Modes for electronic-mail handling."
129   :group 'applications)
130
131 (defgroup news nil
132   "Support for netnews reading and posting."
133   :group 'applications)
134
135 (defgroup games nil
136   "Games, jokes and amusements."
137   :group 'applications)
138
139 (defgroup development nil
140   "Support for further development of Emacs."
141   :group 'emacs)
142
143 (defgroup docs nil
144   "Support for Emacs documentation."
145   :group 'development)
146
147 (defgroup extensions nil
148   "Emacs Lisp language extensions."
149   :group 'development)
150
151 (defgroup internal nil
152   "Code for Emacs internals, build process, defaults."
153   :group 'development)
154
155 (defgroup maint nil
156   "Maintenance aids for the Emacs development group."
157   :tag "Maintenance"
158   :group 'development)
159
160 (defgroup environment nil
161   "Fitting Emacs with its environment."
162   :group 'emacs)
163
164 (defgroup comm nil
165   "Communications, networking, remote access to files."
166   :tag "Communication"
167   :group 'environment)
168
169 (defgroup hardware nil
170   "Support for interfacing with exotic hardware."
171   :group 'environment)
172
173 (defgroup terminals nil
174   "Support for terminal types."
175   :group 'environment)
176
177 (defgroup unix nil
178   "Front-ends/assistants for, or emulators of, UNIX features."
179   :group 'environment)
180
181 (defgroup i18n nil
182   "Internationalization and alternate character-set support."
183   :group 'environment
184   :group 'editing)
185
186 (defgroup data nil
187   "Support editing files of data."
188   :group 'emacs)
189
190 (defgroup wp nil
191   "Word processing."
192   :group 'emacs)
193
194 (defgroup tex nil
195   "Code related to the TeX formatter."
196   :group 'wp)
197
198 (defgroup hypermedia nil
199   "Support for links between text or other media types."
200   :group 'emacs)
201
202 (defgroup local nil
203   "Code local to your site."
204   :group 'emacs)
205
206 (defgroup customize '((widgets custom-group))
207   "Customization of the Customization support."
208   :link '(custom-manual "(custom)Top")
209   :link '(url-link :tag "Development Page"
210                    "http://www.dina.kvl.dk/~abraham/custom/")
211   :prefix "custom-"
212   :group 'help)
213
214 (defgroup custom-faces nil
215   "Faces used by customize."
216   :group 'customize
217   :group 'faces)
218
219 (defgroup custom-browse nil
220   "Control customize browser."
221   :prefix "custom-"
222   :group 'customize)
223
224 (defgroup custom-buffer nil
225   "Control customize buffers."
226   :prefix "custom-"
227   :group 'customize)
228
229 (defgroup custom-menu nil
230   "Control customize menus."
231   :prefix "custom-"
232   :group 'customize)
233
234 (defgroup alloc nil
235   "Storage allocation and gc for GNU Emacs Lisp interpreter."
236   :tag "Storage Allocation"
237   :group 'internal)
238
239 (defgroup undo nil
240   "Undoing changes in buffers."
241   :group 'editing)
242
243 (defgroup editing-basics nil
244   "Most basic editing facilities."
245   :group 'editing)
246
247 (defgroup display nil
248   "How characters are displayed in buffers."
249   :group 'environment)
250
251 (defgroup installation nil
252   "The Emacs installation."
253   :group 'environment)
254
255 (defgroup limits nil
256   "Internal Emacs limits."
257   :group 'internal)
258
259 (defgroup debug nil
260   "Debugging Emacs itself."
261   :group 'development)
262
263 (defgroup mule nil
264   "Mule XEmacs internationalization."
265   :group 'i18n)
266
267 \f
268 ;;; Utilities.
269
270 (defun custom-quote (sexp)
271   "Quote SEXP iff it is not self quoting."
272   (if (or (memq sexp '(t nil))
273           (keywordp sexp)
274           (eq (car-safe sexp) 'lambda)
275           (stringp sexp)
276           (numberp sexp)
277           (characterp sexp)
278           (vectorp sexp)
279           (bit-vector-p sexp))
280       sexp
281     (list 'quote sexp)))
282
283 (defun custom-split-regexp-maybe (regexp)
284   "If REGEXP is a string, split it to a list at `\\|'.
285 You can get the original back with from the result with:
286   (mapconcat #'identity result \"\\|\")
287
288 IF REGEXP is not a string, return it unchanged."
289   (if (stringp regexp)
290       (split-string regexp "\\\\|")
291     regexp))
292
293 (defun custom-variable-prompt ()
294   ;; Code stolen from `help.el'.
295   "Prompt for a variable, defaulting to the variable at point.
296 Return a list suitable for use in `interactive'."
297    (let ((v (variable-at-point))
298          (enable-recursive-minibuffers t)
299          val)
300      (setq val (completing-read
301                 (if (symbolp v)
302                     (format "Customize variable: (default %s) " v)
303                   "Customize variable: ")
304                 obarray (lambda (symbol)
305                           (and (boundp symbol)
306                                (or (get symbol 'custom-type)
307                                    (user-variable-p symbol)))) 
308                 t nil nil (and v (symbol-name v))))
309      (list (if (equal val "")
310                (if (symbolp v) v nil)
311              (intern val)))))
312
313 ;; Here we take not only the actual groups, but the loads, too.
314 (defun custom-group-prompt (prompt)
315   "Read group from minibuffer."
316   (let ((completion-ignore-case t))
317     (list (completing-read
318            prompt obarray
319            (lambda (symbol)
320              (or (get symbol 'custom-group)
321                  (get symbol 'custom-loads)))
322            t))))
323
324 (defun custom-menu-filter (menu widget)
325   "Convert MENU to the form used by `widget-choose'.
326 MENU should be in the same format as `custom-variable-menu'.
327 WIDGET is the widget to apply the filter entries of MENU on."
328   (let ((result nil)
329         current name action filter)
330     (while menu
331       (setq current (car menu)
332             name (nth 0 current)
333             action (nth 1 current)
334             filter (nth 2 current)
335             menu (cdr menu))
336       (if (or (null filter) (funcall filter widget))
337           (push (cons name action) result)
338         (push name result)))
339     (nreverse result)))
340
341 \f
342 ;;; Unlispify.
343
344 (defvar custom-prefix-list nil
345   "List of prefixes that should be ignored by `custom-unlispify'")
346
347 (defcustom custom-unlispify-menu-entries t
348   "Display menu entries as words instead of symbols if non nil."
349   :group 'custom-menu
350   :type 'boolean)
351
352 (defcustom custom-unlispify-remove-prefixes t
353   "Non-nil means remove group prefixes from option names in buffers and menus.
354 This only has an effect when `custom-unlispify-tag-names' or
355 `custom-unlispify-menu-entries' is on."
356   :group 'custom-menu
357   :type 'boolean)
358
359 (defun custom-unlispify-menu-entry (symbol &optional no-suffix)
360   "Convert symbol into a menu entry."
361   (cond ((not custom-unlispify-menu-entries)
362          (symbol-name symbol))
363         ((get symbol 'custom-tag)
364          (if no-suffix
365              (get symbol 'custom-tag)
366            (concat (get symbol 'custom-tag) "...")))
367         (t
368          (with-current-buffer (get-buffer-create " *Custom-Work*")
369            (erase-buffer)
370            (princ symbol (current-buffer))
371            (goto-char (point-min))
372            (when (and (eq (get symbol 'custom-type) 'boolean)
373                       (re-search-forward "-p\\'" nil t))
374              (replace-match "" t t)
375              (goto-char (point-min)))
376            (when custom-unlispify-remove-prefixes
377              (let ((prefixes custom-prefix-list)
378                    prefix)
379                (while prefixes
380                  (setq prefix (car prefixes))
381                  (if (search-forward prefix (+ (point) (length prefix)) t)
382                      (progn
383                        (setq prefixes nil)
384                        (delete-region (point-min) (point)))
385                    (setq prefixes (cdr prefixes))))))
386            (subst-char-in-region (point-min) (point-max) ?- ?\  t)
387            (capitalize-region (point-min) (point-max))
388            (unless no-suffix
389              (goto-char (point-max))
390              (insert "..."))
391            (buffer-string)))))
392
393 (defcustom custom-unlispify-tag-names t
394   "Display tag names as words instead of symbols if non nil."
395   :group 'custom-buffer
396   :type 'boolean)
397
398 (defun custom-unlispify-tag-name (symbol)
399   "Convert symbol into a menu entry."
400   (let ((custom-unlispify-menu-entries custom-unlispify-tag-names))
401     (custom-unlispify-menu-entry symbol t)))
402
403 (defun custom-prefix-add (symbol prefixes)
404   ;; Add SYMBOL to list of ignored PREFIXES.
405   (cons (or (get symbol 'custom-prefix)
406             (concat (symbol-name symbol) "-"))
407         prefixes))
408
409 \f
410 ;;; Guess.
411
412 (defcustom custom-guess-name-alist
413   '(("-p\\'" boolean)
414     ("-hooks?\\'" hook)
415     ("-face\\'" face)
416     ("-file\\'" file)
417     ("-function\\'" function)
418     ("-functions\\'" (repeat function))
419     ("-list\\'" (repeat sexp))
420     ("-alist\\'" (repeat (cons sexp sexp))))
421   "Alist of (MATCH TYPE).
422
423 MATCH should be a regexp matching the name of a symbol, and TYPE should
424 be a widget suitable for editing the value of that symbol.  The TYPE
425 of the first entry where MATCH matches the name of the symbol will be
426 used.
427
428 This is used for guessing the type of variables not declared with
429 customize."
430   :type '(repeat (group (regexp :tag "Match") (sexp :tag "Type")))
431   :group 'customize)
432
433 (defcustom custom-guess-doc-alist
434   '(("\\`\\*?Non-nil " boolean))
435   "Alist of (MATCH TYPE).
436
437 MATCH should be a regexp matching a documentation string, and TYPE
438 should be a widget suitable for editing the value of a variable with
439 that documentation string.  The TYPE of the first entry where MATCH
440 matches the name of the symbol will be used.
441
442 This is used for guessing the type of variables not declared with
443 customize."
444   :type '(repeat (group (regexp :tag "Match") (sexp :tag "Type")))
445   :group 'customize)
446
447 (defun custom-guess-type (symbol)
448   "Guess a widget suitable for editing the value of SYMBOL.
449 This is done by matching SYMBOL with `custom-guess-name-alist' and
450 if that fails, the doc string with `custom-guess-doc-alist'."
451   (let ((name (symbol-name symbol))
452         (names custom-guess-name-alist)
453         current found)
454     (while names
455       (setq current (car names)
456             names (cdr names))
457       (when (string-match (nth 0 current) name)
458         (setq found (nth 1 current)
459               names nil)))
460     (unless found
461       (let ((doc (documentation-property symbol 'variable-documentation))
462             (docs custom-guess-doc-alist))
463         (when doc
464           (while docs
465             (setq current (car docs)
466                   docs (cdr docs))
467             (when (string-match (nth 0 current) doc)
468               (setq found (nth 1 current)
469                     docs nil))))))
470     found))
471
472 \f
473 ;;; Sorting.
474
475 (defcustom custom-browse-sort-alphabetically nil
476   "If non-nil, sort members of each customization group alphabetically."
477   :type 'boolean
478   :group 'custom-browse)
479
480 (defcustom custom-browse-order-groups nil
481   "If non-nil, order group members within each customization group.
482 If `first', order groups before non-groups.
483 If `last', order groups after non-groups."
484   :type '(choice (const first)
485                  (const last)
486                  (const :tag "none" nil))
487   :group 'custom-browse)
488
489 (defcustom custom-browse-only-groups nil
490   "If non-nil, show group members only within each customization group."
491   :type 'boolean
492   :group 'custom-browse)
493
494 (defcustom custom-buffer-sort-alphabetically nil
495   "If non-nil, sort members of each customization group alphabetically."
496   :type 'boolean
497   :group 'custom-buffer)
498
499 (defcustom custom-buffer-order-groups 'last
500   "If non-nil, order group members within each customization group.
501 If `first', order groups before non-groups.
502 If `last', order groups after non-groups."
503   :type '(choice (const first)
504                  (const last)
505                  (const :tag "none" nil))
506   :group 'custom-buffer)
507
508 (defcustom custom-menu-sort-alphabetically nil
509   "If non-nil, sort members of each customization group alphabetically."
510   :type 'boolean
511   :group 'custom-menu)
512
513 (defcustom custom-menu-order-groups 'first
514   "If non-nil, order group members within each customization group.
515 If `first', order groups before non-groups.
516 If `last', order groups after non-groups."
517   :type '(choice (const first)
518                  (const last)
519                  (const :tag "none" nil))
520   :group 'custom-menu)
521
522 (defun custom-sort-items (items sort-alphabetically order-groups)
523   "Return a sorted copy of ITEMS.
524 ITEMS should be a `custom-group' property.
525 If SORT-ALPHABETICALLY non-nil, sort alphabetically.
526 If ORDER-GROUPS is `first' order groups before non-groups, if `last' order
527 groups after non-groups, if nil do not order groups at all."
528   (sort (copy-sequence items)
529    (lambda (a b)
530      (let ((typea (nth 1 a)) (typeb (nth 1 b))
531            (namea (symbol-name (nth 0 a))) (nameb (symbol-name (nth 0 b))))
532        (cond ((not order-groups)
533               ;; Since we don't care about A and B order, maybe sort.
534               (when sort-alphabetically
535                 (string-lessp namea nameb)))
536              ((eq typea 'custom-group)
537               ;; If B is also a group, maybe sort.  Otherwise, order A and B.
538               (if (eq typeb 'custom-group)
539                   (when sort-alphabetically
540                     (string-lessp namea nameb))
541                 (eq order-groups 'first)))
542              ((eq typeb 'custom-group)
543               ;; Since A cannot be a group, order A and B.
544               (eq order-groups 'last))
545              (sort-alphabetically
546               ;; Since A and B cannot be groups, sort.
547               (string-lessp namea nameb)))))))
548
549 \f
550 ;;; Custom Mode Commands.
551
552 (defvar custom-options nil
553   "Customization widgets in the current buffer.")
554
555 (defun Custom-set ()
556   "Set changes in all modified options."
557   (interactive)
558   (let ((children custom-options))
559     (mapc (lambda (child)
560             (when (eq (widget-get child :custom-state) 'modified)
561               (widget-apply child :custom-set)))
562           children)))
563
564 (defun Custom-save ()
565   "Set all modified group members and save them."
566   (interactive)
567   (let ((children custom-options))
568     (mapc (lambda (child)
569             (when (memq (widget-get child :custom-state) '(modified set))
570               (widget-apply child :custom-save)))
571           children))
572   (custom-save-all))
573
574 (defvar custom-reset-menu
575   '(("Current" . Custom-reset-current)
576     ("Saved" . Custom-reset-saved)
577     ("Standard Settings" . Custom-reset-standard))
578   "Alist of actions for the `Reset' button.
579 The key is a string containing the name of the action, the value is a
580 lisp function taking the widget as an element which will be called
581 when the action is chosen.")
582
583 (defun custom-reset (event)
584   "Select item from reset menu."
585   (let* ((completion-ignore-case t)
586          (answer (widget-choose "Reset to"
587                                 custom-reset-menu
588                                 event)))
589     (if answer
590         (funcall answer))))
591
592 (defun Custom-reset-current (&rest ignore)
593   "Reset all modified group members to their current value."
594   (interactive)
595   (let ((children custom-options))
596     (mapc (lambda (child)
597             (when (eq (widget-get child :custom-state) 'modified)
598               (widget-apply child :custom-reset-current)))
599           children)))
600
601 (defun Custom-reset-saved (&rest ignore)
602   "Reset all modified or set group members to their saved value."
603   (interactive)
604   (let ((children custom-options))
605     (mapc (lambda (child)
606             (when (eq (widget-get child :custom-state) 'modified)
607               (widget-apply child :custom-reset-saved)))
608           children)))
609
610 (defun Custom-reset-standard (&rest ignore)
611   "Reset all modified, set, or saved group members to their standard settings."
612   (interactive)
613   (let ((children custom-options))
614     (mapc (lambda (child)
615             (when (eq (widget-get child :custom-state) 'modified)
616               (widget-apply child :custom-reset-standard)))
617           children)))
618
619 \f
620 ;;; The Customize Commands
621
622 (defun custom-prompt-variable (prompt-var prompt-val &optional comment)
623   "Prompt for a variable and a value and return them as a list.
624 PROMPT-VAR is the prompt for the variable, and PROMPT-VAL is the
625 prompt for the value.  The %s escape in PROMPT-VAL is replaced with
626 the name of the variable.
627
628 If the variable has a `variable-interactive' property, that is used as if
629 it were the arg to `interactive' (which see) to interactively read the value.
630
631 If the variable has a `custom-type' property, it must be a widget and the
632 `:prompt-value' property of that widget will be used for reading the value.
633
634 If optional COMMENT argument is non nil, also prompt for a comment and return
635 it as the third element in the list."
636   (let* ((var (read-variable prompt-var))
637          (minibuffer-help-form '(describe-variable var))
638          (val
639           (let ((prop (get var 'variable-interactive))
640                 (type (get var 'custom-type))
641                 (prompt (format prompt-val var)))
642             (unless (listp type)
643               (setq type (list type)))
644             (cond (prop
645                    ;; Use VAR's `variable-interactive' property
646                    ;; as an interactive spec for prompting.
647                    (call-interactively (list 'lambda '(arg)
648                                              (list 'interactive prop)
649                                              'arg)))
650                   (type
651                    (widget-prompt-value type
652                                         prompt
653                                         (if (boundp var)
654                                             (symbol-value var))
655                                         (not (boundp var))))
656                   (t
657                    (eval-minibuffer prompt))))))
658     (if comment
659         (list var val
660               (read-string "Comment: " (get var 'variable-comment)))
661       (list var val))
662     ))
663
664 ;;;###autoload
665 (defun customize-set-value (var val &optional comment)
666   "Set VARIABLE to VALUE.  VALUE is a Lisp object.
667
668 If VARIABLE has a `variable-interactive' property, that is used as if
669 it were the arg to `interactive' (which see) to interactively read the value.
670
671 If VARIABLE has a `custom-type' property, it must be a widget and the
672 `:prompt-value' property of that widget will be used for reading the value.
673
674 If given a prefix (or a COMMENT argument), also prompt for a comment."
675   (interactive (custom-prompt-variable "Set variable: "
676                                        "Set %s to value: "
677                                        current-prefix-arg))
678
679   (set var val)
680   (cond ((string= comment "")
681          (put var 'variable-comment nil))
682         (comment
683          (put var 'variable-comment comment))))
684
685 ;;;###autoload
686 (defun customize-set-variable (variable value &optional comment)
687   "Set the default for VARIABLE to VALUE.  VALUE is any Lisp object.
688
689 If VARIABLE has a `custom-set' property, that is used for setting
690 VARIABLE, otherwise `set-default' is used.
691
692 The `customized-value' property of the VARIABLE will be set to a list
693 with a quoted VALUE as its sole list member.
694
695 If VARIABLE has a `variable-interactive' property, that is used as if
696 it were the arg to `interactive' (which see) to interactively read the value.
697
698 If VARIABLE has a `custom-type' property, it must be a widget and the
699 `:prompt-value' property of that widget will be used for reading the value.
700
701 If given a prefix (or a COMMENT argument), also prompt for a comment."
702   (interactive (custom-prompt-variable "Set variable: "
703                                        "Set customized value for %s to: "
704                                        current-prefix-arg))
705   (funcall (or (get variable 'custom-set) 'set-default) variable value)
706   (put variable 'customized-value (list (custom-quote value)))
707   (cond ((string= comment "")
708          (put variable 'variable-comment nil)
709          (put variable 'customized-variable-comment nil))
710         (comment
711          (put variable 'variable-comment comment)
712          (put variable 'customized-variable-comment comment))))
713
714
715 ;;;###autoload
716 (defun customize-save-variable (variable value &optional comment)
717   "Set the default for VARIABLE to VALUE, and save it for future sessions.
718 If VARIABLE has a `custom-set' property, that is used for setting
719 VARIABLE, otherwise `set-default' is used.
720
721 The `customized-value' property of the VARIABLE will be set to a list
722 with a quoted VALUE as its sole list member.
723
724 If VARIABLE has a `variable-interactive' property, that is used as if
725 it were the arg to `interactive' (which see) to interactively read the value.
726
727 If VARIABLE has a `custom-type' property, it must be a widget and the
728 `:prompt-value' property of that widget will be used for reading the value.
729
730 If given a prefix (or a COMMENT argument), also prompt for a comment."
731   (interactive (custom-prompt-variable "Set and ave variable: "
732                                        "Set and save value for %s as: "
733                                        current-prefix-arg))
734   (funcall (or (get variable 'custom-set) 'set-default) variable value)
735   (put variable 'saved-value (list (custom-quote value)))
736   (custom-push-theme 'theme-value variable 'user 'set (list (custom-quote value)))
737   (cond ((string= comment "")
738          (put variable 'variable-comment nil)
739          (put variable 'saved-variable-comment nil))
740         (comment
741          (put variable 'variable-comment comment)
742          (put variable 'saved-variable-comment comment)))
743   (custom-save-all))
744
745 ;;;###autoload
746 (defun customize (group)
747   "Select a customization buffer which you can use to set user options.
748 User options are structured into \"groups\".
749 The default group is `Emacs'."
750   (interactive (custom-group-prompt
751                 "Customize group: (default emacs) "))
752   (when (stringp group)
753     (if (string-equal "" group)
754         (setq group 'emacs)
755       (setq group (intern group))))
756   (let ((name (format "*Customize Group: %s*"
757                       (custom-unlispify-tag-name group))))
758     (if (get-buffer name)
759         (switch-to-buffer name)
760       (custom-buffer-create (list (list group 'custom-group))
761                             name
762                             (concat " for group "
763                                     (custom-unlispify-tag-name group))))))
764
765 ;;;###autoload
766 (defalias 'customize-group 'customize)
767
768 ;;;###autoload
769 (defun customize-other-window (symbol)
770   "Customize SYMBOL, which must be a customization group."
771   (interactive (custom-group-prompt
772                 "Customize group: (default emacs) "))
773   (when (stringp symbol)
774     (if (string-equal "" symbol)
775         (setq symbol 'emacs)
776       (setq symbol (intern symbol))))
777   (custom-buffer-create-other-window
778    (list (list symbol 'custom-group))
779    (format "*Customize Group: %s*" (custom-unlispify-tag-name symbol))))
780
781 ;;;###autoload
782 (defalias 'customize-group-other-window 'customize-other-window)
783
784 ;;;###autoload
785 (defalias 'customize-option 'customize-variable)
786
787 ;;;###autoload
788 (defun customize-variable (symbol)
789   "Customize SYMBOL, which must be a user option variable."
790   (interactive (custom-variable-prompt))
791   (custom-buffer-create (list (list symbol 'custom-variable))
792                         (format "*Customize Variable: %s*"
793                                 (custom-unlispify-tag-name symbol))))
794
795 ;;;###autoload
796 (defun customize-changed-options (since-version)
797   "Customize all user option variables whose default values changed recently.
798 This means, in other words, variables defined with a `:version' keyword."
799   (interactive "sCustomize options changed, since version (default all versions): ")
800   (if (equal since-version "")
801       (setq since-version nil))
802   (let ((found nil))
803     (mapatoms (lambda (symbol)
804                 (and (boundp symbol)
805                      (let ((version (get symbol 'custom-version)))
806                        (and version
807                             (or (null since-version)
808                                 (customize-version-lessp since-version version))))
809                      (push (list symbol 'custom-variable) found))))
810     (unless found
811       (error "No user options have changed defaults %s"
812              (if since-version
813                  (format "since XEmacs %s" since-version)
814                "in recent Emacs versions")))
815     (custom-buffer-create (custom-sort-items found t nil)
816                           "*Customize Changed Options*")))
817
818 (defun customize-version-lessp (version1 version2)
819   (let (major1 major2 minor1 minor2)
820     (string-match "\\([0-9]+\\)[.]\\([0-9]+\\)" version1)
821     (setq major1 (read (match-string 1 version1)))
822     (setq minor1 (read (match-string 2 version1)))
823     (string-match "\\([0-9]+\\)[.]\\([0-9]+\\)" version2)
824     (setq major2 (read (match-string 1 version2)))
825     (setq minor2 (read (match-string 2 version2)))
826     (or (< major1 major2)
827         (and (= major1 major2)
828              (< minor1 minor2)))))
829
830 ;;;###autoload
831 (defalias 'customize-variable-other-window 'customize-option-other-window)
832
833 ;;;###autoload
834 (defun customize-option-other-window (symbol)
835   "Customize SYMBOL, which must be a user option variable.
836 Show the buffer in another window, but don't select it."
837   (interactive (custom-variable-prompt))
838   (custom-buffer-create-other-window
839    (list (list symbol 'custom-variable))
840    (format "*Customize Option: %s*" (custom-unlispify-tag-name symbol))))
841
842 ;;;###autoload
843 (defun customize-face (&optional symbol)
844   "Customize SYMBOL, which should be a face name or nil.
845 If SYMBOL is nil, customize all faces."
846   (interactive (list (completing-read "Customize face: (default all) "
847                                       obarray 'find-face)))
848   (if (or (null symbol) (and (stringp symbol) (zerop (length symbol))))
849       (custom-buffer-create (custom-sort-items
850                              (mapcar (lambda (symbol)
851                                        (list symbol 'custom-face))
852                                      (face-list))
853                              t nil)
854                             "*Customize Faces*")
855     (when (stringp symbol)
856       (setq symbol (intern symbol)))
857     (check-argument-type 'symbolp symbol)
858     (custom-buffer-create (list (list symbol 'custom-face))
859                           (format "*Customize Face: %s*"
860                                   (custom-unlispify-tag-name symbol)))))
861
862 ;;;###autoload
863 (defun customize-face-other-window (&optional symbol)
864   "Show customization buffer for FACE in other window."
865   (interactive (list (completing-read "Customize face: "
866                                       obarray 'find-face)))
867   (if (or (null symbol) (and (stringp symbol) (zerop (length symbol))))
868       ()
869     (if (stringp symbol)
870         (setq symbol (intern symbol)))
871     (check-argument-type 'symbolp symbol)
872     (custom-buffer-create-other-window
873      (list (list symbol 'custom-face))
874      (format "*Customize Face: %s*" (custom-unlispify-tag-name symbol)))))
875
876 ;;;###autoload
877 (defun customize-customized ()
878   "Customize all user options set since the last save in this session."
879   (interactive)
880   (let ((found nil))
881     (mapatoms (lambda (symbol)
882                 (and (or (get symbol 'customized-face)
883                          (get symbol 'customized-face-comment))
884                      (find-face symbol)
885                      (push (list symbol 'custom-face) found))
886                 (and (or (get symbol 'customized-value)
887                          (get symbol 'customized-variable-comment))
888                      (boundp symbol)
889                      (push (list symbol 'custom-variable) found))))
890     (if (not found)
891         (error "No customized user options")
892       (custom-buffer-create (custom-sort-items found t nil)
893                             "*Customize Customized*"))))
894
895 ;;;###autoload
896 (defun customize-saved ()
897   "Customize all already saved user options."
898   (interactive)
899   (let ((found nil))
900     (mapatoms (lambda (symbol)
901                 (and (or (get symbol 'saved-face)
902                          (get symbol 'saved-face-comment))
903                      (find-face symbol)
904                      (push (list symbol 'custom-face) found))
905                 (and (or (get symbol 'saved-value)
906                          (get symbol 'saved-variable-comment))
907                      (boundp symbol)
908                      (push (list symbol 'custom-variable) found))))
909     (if (not found )
910         (error "No saved user options")
911       (custom-buffer-create (custom-sort-items found t nil)
912                             "*Customize Saved*"))))
913
914 ;;;###autoload
915 (defun customize-apropos (regexp &optional all)
916   "Customize all user options matching REGEXP.
917 If ALL is `options', include only options.
918 If ALL is `faces', include only faces.
919 If ALL is `groups', include only groups.
920 If ALL is t (interactively, with prefix arg), include options which are not
921 user-settable, as well as faces and groups."
922   (interactive "sCustomize regexp: \nP")
923   (let ((found nil))
924     (mapatoms (lambda (symbol)
925                 (when (string-match regexp (symbol-name symbol))
926                   (when (and (not (memq all '(faces options)))
927                              (get symbol 'custom-group))
928                     (push (list symbol 'custom-group) found))
929                   (when (and (not (memq all '(options groups)))
930                              (find-face symbol))
931                     (push (list symbol 'custom-face) found))
932                   (when (and (not (memq all '(groups faces)))
933                              (boundp symbol)
934                              (or (get symbol 'saved-value)
935                                  (get symbol 'standard-value)
936                                  (if (memq all '(nil options))
937                                      (user-variable-p symbol)
938                                    (get symbol 'variable-documentation))))
939                     (push (list symbol 'custom-variable) found)))))
940     (if (not found)
941         (error "No matches")
942       (custom-buffer-create (custom-sort-items found t
943                                                custom-buffer-order-groups)
944                             "*Customize Apropos*"))))
945
946 ;;;###autoload
947 (defun customize-apropos-options (regexp &optional arg)
948   "Customize all user options matching REGEXP.
949 With prefix arg, include options which are not user-settable."
950   (interactive "sCustomize regexp: \nP")
951   (customize-apropos regexp (or arg 'options)))
952
953 ;;;###autoload
954 (defun customize-apropos-faces (regexp)
955   "Customize all user faces matching REGEXP."
956   (interactive "sCustomize regexp: \n")
957   (customize-apropos regexp 'faces))
958
959 ;;;###autoload
960 (defun customize-apropos-groups (regexp)
961   "Customize all user groups matching REGEXP."
962   (interactive "sCustomize regexp: \n")
963   (customize-apropos regexp 'groups))
964
965 \f
966 ;;; Buffer.
967
968 (defcustom custom-buffer-style 'links
969   "*Control the presentation style for customization buffers.
970 The value should be a symbol, one of:
971
972 brackets: groups nest within each other with big horizontal brackets.
973 links: groups have links to subgroups."
974   :type '(radio (const :tag "brackets: Groups nest within each others" brackets)
975                 (const :tag "links: Group have links to subgroups" links))
976   :group 'custom-buffer)
977
978 (defcustom custom-buffer-done-function 'kill-buffer
979   "*Function to be used to remove the buffer when the user is done with it.
980 Choices include `kill-buffer' (the default) and `bury-buffer'.
981 The function will be called with one argument, the buffer to remove."
982   :type '(radio (function-item kill-buffer)
983                 (function-item bury-buffer)
984                 (function :tag "Other" nil))
985   :group 'custom-buffer)
986
987 (defcustom custom-buffer-indent 3
988   "Number of spaces to indent nested groups."
989   :type 'integer
990   :group 'custom-buffer)
991
992 ;;;###autoload
993 (defun custom-buffer-create (options &optional name description)
994   "Create a buffer containing OPTIONS.
995 Optional NAME is the name of the buffer.
996 OPTIONS should be an alist of the form ((SYMBOL WIDGET)...), where
997 SYMBOL is a customization option, and WIDGET is a widget for editing
998 that option."
999   (unless name (setq name "*Customization*"))
1000   (kill-buffer (get-buffer-create name))
1001   (switch-to-buffer (get-buffer-create name))
1002   (custom-buffer-create-internal options description))
1003
1004 ;;;###autoload
1005 (defun custom-buffer-create-other-window (options &optional name description)
1006   "Create a buffer containing OPTIONS.
1007 Optional NAME is the name of the buffer.
1008 OPTIONS should be an alist of the form ((SYMBOL WIDGET)...), where
1009 SYMBOL is a customization option, and WIDGET is a widget for editing
1010 that option."
1011   (unless name (setq name "*Customization*"))
1012   (kill-buffer (get-buffer-create name))
1013   (let ((window (selected-window)))
1014     (switch-to-buffer-other-window (get-buffer-create name))
1015     (custom-buffer-create-internal options description)
1016     (select-window window)))
1017
1018 (defcustom custom-reset-button-menu t
1019   "If non-nil, only show a single reset button in customize buffers.
1020 This button will have a menu with all three reset operations."
1021   :type 'boolean
1022   :group 'custom-buffer)
1023
1024 (defconst custom-skip-messages 5)
1025
1026 (defun Custom-buffer-done ()
1027   "Remove current buffer.
1028 This works by calling the function specified by
1029  `custom-buffer-done-function'."
1030   (interactive)
1031   (funcall custom-buffer-done-function (current-buffer)))
1032
1033 (defun custom-buffer-create-buttons ()
1034   (message "Creating customization buttons...")
1035   (widget-insert "\nOperate on everything in this buffer:\n ")
1036   (widget-create 'push-button
1037                  :tag "Set"
1038                  :help-echo "\
1039 Make your editing in this buffer take effect for this session"
1040                  :action (lambda (widget &optional event)
1041                            (Custom-set)))
1042   (widget-insert " ")
1043   (widget-create 'push-button
1044                  :tag "Save"
1045                  :help-echo "\
1046 Make your editing in this buffer take effect for future Emacs sessions"
1047                  :action (lambda (widget &optional event)
1048                            (Custom-save)))
1049   (if custom-reset-button-menu
1050       (progn
1051         (widget-insert " ")
1052         (widget-create 'push-button
1053                        :tag "Reset"
1054                        :tag-glyph '("reset-up" "reset-down")
1055                        :help-echo "Show a menu with reset operations"
1056                        :mouse-down-action (lambda (&rest junk) t)
1057                        :action (lambda (widget &optional event)
1058                                  (custom-reset event))))
1059     (widget-insert " ")
1060     (widget-create 'push-button
1061                    :tag "Reset"
1062                    :help-echo "\
1063 Reset all edited text in this buffer to reflect current values"
1064                    :action 'Custom-reset-current)
1065     (widget-insert " ")
1066     (widget-create 'push-button
1067                    :tag "Reset to Saved"
1068                    :help-echo "\
1069 Reset all values in this buffer to their saved settings"
1070                    :action 'Custom-reset-saved)
1071     (widget-insert " ")
1072     (widget-create 'push-button
1073                    :tag "Reset to Standard"
1074                    :help-echo "\
1075 Reset all values in this buffer to their standard settings"
1076                    :action 'Custom-reset-standard))
1077   (widget-insert "  ")
1078   (widget-create 'push-button
1079                  :tag "Done"
1080                  :help-echo "Remove the buffer"
1081                  :action (lambda (widget &optional event)
1082                            (Custom-buffer-done)))
1083   (widget-insert "\n"))
1084
1085 (defcustom custom-novice t
1086   "If non-nil, show help message at top of customize buffers."
1087   :type 'boolean
1088   :group 'custom-buffer)
1089
1090 (defcustom custom-display-global-buttons 'top
1091   "If `nil' don't display the global buttons.  If `top' display at the
1092 beginning of custom buffers.  If `bottom', display at the end."
1093   :type '(choice (const top)
1094                  (const bottom)
1095                  (const :tag "don't" nil))
1096   :group 'custom-buffer)
1097
1098 (defun custom-buffer-create-internal (options &optional description)
1099   (message "Creating customization buffer...")
1100   (custom-mode)
1101   (widget-insert "This is a customization buffer")
1102   (if description
1103       (widget-insert description))
1104   (when custom-novice
1105       (widget-insert ".\n\
1106 Type RET or click button2 on an active field to invoke its action.
1107 Invoke ")
1108       (widget-create 'info-link
1109                      :tag "Help"
1110                      :help-echo "Read the online help"
1111                      "(XEmacs)Easy Customization")
1112       (widget-insert " for more information."))
1113   (widget-insert "\n")
1114   (if (equal custom-display-global-buttons 'top)
1115       (custom-buffer-create-buttons))
1116   (widget-insert "\n")
1117   (message "Creating customization items...")
1118   (setq custom-options
1119         (if (= (length options) 1)
1120             (mapcar (lambda (entry)
1121                       (widget-create (nth 1 entry)
1122                                      :documentation-shown t
1123                                      :custom-state 'unknown
1124                                      :tag (custom-unlispify-tag-name
1125                                            (nth 0 entry))
1126                                      :value (nth 0 entry)))
1127                     options)
1128           (let ((count 0)
1129                 (length (length options)))
1130             (mapcar (lambda (entry)
1131                       (prog2
1132                           (display-message
1133                            'progress
1134                            (format "Creating customization items %2d%%..."
1135                                    (/ (* 100.0 count) length)))
1136                           (widget-create (nth 1 entry)
1137                                          :tag (custom-unlispify-tag-name
1138                                                (nth 0 entry))
1139                                          :value (nth 0 entry))
1140                         (incf count)
1141                         (unless (eq (preceding-char) ?\n)
1142                           (widget-insert "\n"))
1143                         (widget-insert "\n")))
1144                     options))))
1145   (unless (eq (preceding-char) ?\n)
1146     (widget-insert "\n"))
1147   (if (equal custom-display-global-buttons 'bottom)
1148       (custom-buffer-create-buttons))
1149   (display-message 'progress
1150                    (format
1151                     "Creating customization items %2d%%...done" 100))
1152   (unless (eq custom-buffer-style 'tree)
1153     (mapc 'custom-magic-reset custom-options))
1154   (message "Creating customization setup...")
1155   (widget-setup)
1156   (goto-char (point-min))
1157   (message "Creating customization buffer...done"))
1158
1159 \f
1160 ;;; The Tree Browser.
1161
1162 ;;;###autoload
1163 (defun customize-browse (&optional group)
1164   "Create a tree browser for the customize hierarchy."
1165   (interactive)
1166   (unless group
1167     (setq group 'emacs))
1168   (let ((name "*Customize Browser*"))
1169     (kill-buffer (get-buffer-create name))
1170     (switch-to-buffer (get-buffer-create name)))
1171   (custom-mode)
1172   (widget-insert "\
1173 Square brackets show active fields; type RET or click button2
1174 on an active field to invoke its action.
1175 Invoke [+] below to expand a group, and [-] to collapse an expanded group.\n")
1176   (if custom-browse-only-groups
1177       (widget-insert "\
1178 Invoke the [Group] button below to edit that item in another window.\n\n")
1179     (widget-insert "Invoke the ")
1180     (widget-create 'item
1181                    :format "%t"
1182                    :tag "[Group]"
1183                    :tag-glyph "folder")
1184     (widget-insert ", ")
1185     (widget-create 'item
1186                    :format "%t"
1187                    :tag "[Face]"
1188                    :tag-glyph "face")
1189     (widget-insert ", and ")
1190     (widget-create 'item
1191                    :format "%t"
1192                    :tag "[Option]"
1193                    :tag-glyph "option")
1194     (widget-insert " buttons below to edit that
1195 item in another window.\n\n"))
1196   (let ((custom-buffer-style 'tree))
1197     (widget-create 'custom-group
1198                    :custom-last t
1199                    :custom-state 'unknown
1200                    :tag (custom-unlispify-tag-name group)
1201                    :value group))
1202   (widget-add-change)
1203   (goto-char (point-min)))
1204
1205 (define-widget 'custom-browse-visibility 'item
1206   "Control visibility of of items in the customize tree browser."
1207   :format "%[[%t]%]"
1208   :action 'custom-browse-visibility-action)
1209
1210 (defun custom-browse-visibility-action (widget &rest ignore)
1211   (let ((custom-buffer-style 'tree))
1212     (custom-toggle-parent widget)))
1213
1214 (define-widget 'custom-browse-group-tag 'push-button
1215   "Show parent in other window when activated."
1216   :tag "Group"
1217   :tag-glyph "folder"
1218   :action 'custom-browse-group-tag-action)
1219
1220 (defun custom-browse-group-tag-action (widget &rest ignore)
1221   (let ((parent (widget-get widget :parent)))
1222     (customize-group-other-window (widget-value parent))))
1223
1224 (define-widget 'custom-browse-variable-tag 'push-button
1225   "Show parent in other window when activated."
1226   :tag "Option"
1227   :tag-glyph "option"
1228   :action 'custom-browse-variable-tag-action)
1229
1230 (defun custom-browse-variable-tag-action (widget &rest ignore)
1231   (let ((parent (widget-get widget :parent)))
1232     (customize-variable-other-window (widget-value parent))))
1233
1234 (define-widget 'custom-browse-face-tag 'push-button
1235   "Show parent in other window when activated."
1236   :tag "Face"
1237   :tag-glyph "face"
1238   :action 'custom-browse-face-tag-action)
1239
1240 (defun custom-browse-face-tag-action (widget &rest ignore)
1241   (let ((parent (widget-get widget :parent)))
1242     (customize-face-other-window (widget-value parent))))
1243
1244 (defconst custom-browse-alist '(("   " "space")
1245                                 (" | " "vertical")
1246                                 ("-\\ " "top")
1247                                 (" |-" "middle")
1248                                 (" `-" "bottom")))
1249
1250 (defun custom-browse-insert-prefix (prefix)
1251   "Insert PREFIX.  On XEmacs convert it to line graphics."
1252   ;; #### Unfinished.
1253   (if nil ; (string-match "XEmacs" emacs-version)
1254       (progn
1255         (insert "*")
1256         (while (not (string-equal prefix ""))
1257           (let ((entry (substring prefix 0 3)))
1258             (setq prefix (substring prefix 3))
1259             (let ((overlay (make-overlay (1- (point)) (point) nil t nil))
1260                   (name (nth 1 (assoc entry custom-browse-alist))))
1261               (overlay-put overlay 'end-glyph (widget-glyph-find name entry))
1262               (overlay-put overlay 'start-open t)
1263               (overlay-put overlay 'end-open t)))))
1264     (insert prefix)))
1265
1266 \f
1267 ;;; Modification of Basic Widgets.
1268 ;;
1269 ;; We add extra properties to the basic widgets needed here.  This is
1270 ;; fine, as long as we are careful to stay within out own namespace.
1271 ;;
1272 ;; We want simple widgets to be displayed by default, but complex
1273 ;; widgets to be hidden.
1274
1275 (widget-put (get 'item 'widget-type) :custom-show t)
1276 (widget-put (get 'editable-field 'widget-type)
1277             :custom-show (lambda (widget value)
1278                            ;; This used to call pp-to-string
1279                            (let ((pp (widget-prettyprint-to-string value)))
1280                              (cond ((string-match "\n" pp)
1281                                     nil)
1282                                    ((> (length pp) 40)
1283                                     nil)
1284                                    (t t)))))
1285 (widget-put (get 'menu-choice 'widget-type) :custom-show t)
1286
1287 ;;; The `custom-manual' Widget.
1288
1289 (define-widget 'custom-manual 'info-link
1290   "Link to the manual entry for this customization option."
1291   :tag "Manual")
1292
1293 ;;; The `custom-magic' Widget.
1294
1295 (defgroup custom-magic-faces nil
1296   "Faces used by the magic button."
1297   :group 'custom-faces
1298   :group 'custom-buffer)
1299
1300 (defface custom-invalid-face '((((class color))
1301                                 (:foreground "yellow" :background "red"))
1302                                (t
1303                                 (:bold t :italic t :underline t)))
1304   "Face used when the customize item is invalid."
1305   :group 'custom-magic-faces)
1306
1307 (defface custom-rogue-face '((((class color))
1308                               (:foreground "pink" :background "black"))
1309                              (t
1310                               (:underline t)))
1311   "Face used when the customize item is not defined for customization."
1312   :group 'custom-magic-faces)
1313
1314 (defface custom-modified-face '((((class color))
1315                                  (:foreground "white" :background "blue"))
1316                                 (t
1317                                  (:italic t :bold)))
1318   "Face used when the customize item has been modified."
1319   :group 'custom-magic-faces)
1320
1321 (defface custom-set-face '((((class color))
1322                                 (:foreground "blue" :background "white"))
1323                                (t
1324                                 (:italic t)))
1325   "Face used when the customize item has been set."
1326   :group 'custom-magic-faces)
1327
1328 (defface custom-changed-face '((((class color))
1329                                 (:foreground "white" :background "blue"))
1330                                (t
1331                                 (:italic t)))
1332   "Face used when the customize item has been changed."
1333   :group 'custom-magic-faces)
1334
1335 (defface custom-saved-face '((t (:underline t)))
1336   "Face used when the customize item has been saved."
1337   :group 'custom-magic-faces)
1338
1339 (defconst custom-magic-alist '((nil "#" underline "\
1340 uninitialized, you should not see this.")
1341                                (unknown "?" italic "\
1342 unknown, you should not see this.")
1343                                (hidden "-" default "\
1344 hidden, invoke \"Show\" button in the previous line to show." "\
1345 group now hidden, invoke the above \"Show\" button to show contents.")
1346                                (invalid "x" custom-invalid-face "\
1347 the value displayed for this %c is invalid and cannot be set.")
1348                                (modified "*" custom-modified-face "\
1349 you have edited the value as text, but you have not set the %c." "\
1350 you have edited something in this group, but not set it.")
1351                                (set "+" custom-set-face "\
1352 you have set this %c, but not saved it for future sessions." "\
1353 something in this group has been set, but not saved.")
1354                                (changed ":" custom-changed-face "\
1355 this %c has been changed outside the customize buffer." "\
1356 something in this group has been changed outside customize.")
1357                                (saved "!" custom-saved-face "\
1358 this %c has been set and saved." "\
1359 something in this group has been set and saved.")
1360                                (rogue "@" custom-rogue-face "\
1361 this %c has not been changed with customize." "\
1362 something in this group is not prepared for customization.")
1363                                (standard " " nil "\
1364 this %c is unchanged from its standard setting." "\
1365 visible group members are all at standard settings."))
1366   "Alist of customize option states.
1367 Each entry is of the form (STATE MAGIC FACE ITEM-DESC [ GROUP-DESC ]), where
1368
1369 STATE is one of the following symbols:
1370
1371 `nil'
1372    For internal use, should never occur.
1373 `unknown'
1374    For internal use, should never occur.
1375 `hidden'
1376    This item is not being displayed.
1377 `invalid'
1378    This item is modified, but has an invalid form.
1379 `modified'
1380    This item is modified, and has a valid form.
1381 `set'
1382    This item has been set but not saved.
1383 `changed'
1384    The current value of this item has been changed temporarily.
1385 `saved'
1386    This item is marked for saving.
1387 `rogue'
1388    This item has no customization information.
1389 `standard'
1390    This item is unchanged from the standard setting.
1391
1392 MAGIC is a string used to present that state.
1393
1394 FACE is a face used to present the state.
1395
1396 ITEM-DESC is a string describing the state for options.
1397
1398 GROUP-DESC is a string describing the state for groups.  If this is
1399 left out, ITEM-DESC will be used.
1400
1401 The string %c in either description will be replaced with the
1402 category of the item.  These are `group'. `option', and `face'.
1403
1404 The list should be sorted most significant first.")
1405
1406 (defcustom custom-magic-show 'long
1407   "If non-nil, show textual description of the state.
1408 If `long', show a full-line description, not just one word."
1409   :type '(choice (const :tag "no" nil)
1410                  (const short)
1411                  (const long))
1412   :group 'custom-buffer)
1413
1414 (defcustom custom-magic-show-hidden '(option face)
1415   "Control whether the State button is shown for hidden items.
1416 The value should be a list with the custom categories where the State
1417 button should be visible.  Possible categories are `group', `option',
1418 and `face'."
1419   :type '(set (const group) (const option) (const face))
1420   :group 'custom-buffer)
1421
1422 (defcustom custom-magic-show-button nil
1423   "Show a \"magic\" button indicating the state of each customization option."
1424   :type 'boolean
1425   :group 'custom-buffer)
1426
1427 (define-widget 'custom-magic 'default
1428   "Show and manipulate state for a customization option."
1429   :format "%v"
1430   :action 'widget-parent-action
1431   :notify 'ignore
1432   :value-get 'ignore
1433   :value-create 'custom-magic-value-create
1434   :value-delete 'widget-children-value-delete)
1435
1436 (defun widget-magic-mouse-down-action (widget &optional event)
1437   ;; Non-nil unless hidden.
1438   (not (eq (widget-get (widget-get (widget-get widget :parent) :parent)
1439                        :custom-state)
1440            'hidden)))
1441
1442 (defun custom-magic-value-create (widget)
1443   ;; Create compact status report for WIDGET.
1444   (let* ((parent (widget-get widget :parent))
1445          (state (widget-get parent :custom-state))
1446          (hidden (eq state 'hidden))
1447          (entry (assq state custom-magic-alist))
1448          (magic (nth 1 entry))
1449          (face (nth 2 entry))
1450          (category (widget-get parent :custom-category))
1451          (text (or (and (eq category 'group)
1452                         (nth 4 entry))
1453                    (nth 3 entry)))
1454          (form (widget-get parent :custom-form))
1455          children)
1456     (while (string-match "\\`\\(.*\\)%c\\(.*\\)\\'" text)
1457       (setq text (concat (match-string 1 text)
1458                          (symbol-name category)
1459                          (match-string 2 text))))
1460     (when (and custom-magic-show
1461                (or (not hidden)
1462                    (memq category custom-magic-show-hidden)))
1463       (insert "   ")
1464       (when (and (eq category 'group)
1465                  (not (and (eq custom-buffer-style 'links)
1466                            (> (widget-get parent :custom-level) 1))))
1467         (insert-char ?\  (* custom-buffer-indent
1468                             (widget-get parent :custom-level))))
1469       (push (widget-create-child-and-convert
1470              widget 'choice-item
1471              :help-echo "Change the state of this item"
1472              :format (if hidden "%t" "%[%t%]")
1473              :button-prefix 'widget-push-button-prefix
1474              :button-suffix 'widget-push-button-suffix
1475              :mouse-down-action 'widget-magic-mouse-down-action
1476              :tag "State"
1477              ;;:tag-glyph (or hidden '("state-up" "state-down"))
1478              )
1479             children)
1480       (insert ": ")
1481       (let ((start (point)))
1482         (if (eq custom-magic-show 'long)
1483             (insert text)
1484           (insert (symbol-name state)))
1485         (cond ((eq form 'lisp)
1486                (insert " (lisp)"))
1487               ((eq form 'mismatch)
1488                (insert " (mismatch)")))
1489         (put-text-property start (point) 'face 'custom-state-face))
1490       (insert "\n"))
1491     (when (and (eq category 'group)
1492                (not (and (eq custom-buffer-style 'links)
1493                          (> (widget-get parent :custom-level) 1))))
1494       (insert-char ?\  (* custom-buffer-indent
1495                           (widget-get parent :custom-level))))
1496     (when custom-magic-show-button
1497       (when custom-magic-show
1498         (let ((indent (widget-get parent :indent)))
1499           (when indent
1500             (insert-char ?\  indent))))
1501       (push (widget-create-child-and-convert
1502              widget 'choice-item
1503              :mouse-down-action 'widget-magic-mouse-down-action
1504              :button-face face
1505              :button-prefix ""
1506              :button-suffix ""
1507              :help-echo "Change the state"
1508              :format (if hidden "%t" "%[%t%]")
1509              :tag (if (memq form '(lisp mismatch))
1510                       (concat "(" magic ")")
1511                     (concat "[" magic "]")))
1512             children)
1513       (insert " "))
1514     (widget-put widget :children children)))
1515
1516 (defun custom-magic-reset (widget)
1517   "Redraw the :custom-magic property of WIDGET."
1518   (let ((magic (widget-get widget :custom-magic)))
1519     (widget-value-set magic (widget-value magic))))
1520
1521 ;;; The `custom' Widget.
1522
1523 (defface custom-button-face '((t (:bold t)))
1524   "Face used for buttons in customization buffers."
1525   :group 'custom-faces)
1526
1527 (defface custom-documentation-face nil
1528   "Face used for documentation strings in customization buffers."
1529   :group 'custom-faces)
1530
1531 (defface custom-state-face '((((class color)
1532                                (background dark))
1533                               (:foreground "lime green"))
1534                              (((class color)
1535                                (background light))
1536                               (:foreground "dark green"))
1537                              (t nil))
1538   "Face used for State descriptions in the customize buffer."
1539   :group 'custom-faces)
1540
1541 (define-widget 'custom 'default
1542   "Customize a user option."
1543   :format "%v"
1544   :convert-widget 'custom-convert-widget
1545   :notify 'custom-notify
1546   :custom-prefix ""
1547   :custom-level 1
1548   :custom-state 'hidden
1549   :documentation-property 'widget-subclass-responsibility
1550   :value-create 'widget-subclass-responsibility
1551   :value-delete 'widget-children-value-delete
1552   :value-get 'widget-value-value-get
1553   :validate 'widget-children-validate
1554   :match (lambda (widget value) (symbolp value)))
1555
1556 (defun custom-convert-widget (widget)
1557   ;; Initialize :value and :tag from :args in WIDGET.
1558   (let ((args (widget-get widget :args)))
1559     (when args
1560       (widget-put widget :value (widget-apply widget
1561                                               :value-to-internal (car args)))
1562       (widget-put widget :tag (custom-unlispify-tag-name (car args)))
1563       (widget-put widget :args nil)))
1564   widget)
1565
1566 (defun custom-notify (widget &rest args)
1567   "Keep track of changes."
1568   (let ((state (widget-get widget :custom-state)))
1569     (unless (eq state 'modified)
1570       (unless (memq state '(nil unknown hidden))
1571         (widget-put widget :custom-state 'modified))
1572       (custom-magic-reset widget)
1573       (apply 'widget-default-notify widget args))))
1574
1575 (defun custom-redraw (widget)
1576   "Redraw WIDGET with current settings."
1577   (let ((line (count-lines (point-min) (point)))
1578         (column (current-column))
1579         (pos (point))
1580         (from (marker-position (widget-get widget :from)))
1581         (to (marker-position (widget-get widget :to))))
1582     (save-excursion
1583       (widget-value-set widget (widget-value widget))
1584       (custom-redraw-magic widget))
1585     (when (and (>= pos from) (<= pos to))
1586       (condition-case nil
1587           (progn
1588             (if (> column 0)
1589                 (goto-line line)
1590               (goto-line (1+ line)))
1591             (move-to-column column))
1592         (error nil)))))
1593
1594 (defun custom-redraw-magic (widget)
1595   "Redraw WIDGET state with current settings."
1596   (while widget
1597     (let ((magic (widget-get widget :custom-magic)))
1598       (cond (magic
1599              (widget-value-set magic (widget-value magic))
1600              (when (setq widget (widget-get widget :group))
1601                (custom-group-state-update widget)))
1602             (t
1603              (setq widget nil)))))
1604   (widget-setup))
1605
1606 (defun custom-show (widget value)
1607   "Non-nil if WIDGET should be shown with VALUE by default."
1608   (let ((show (widget-get widget :custom-show)))
1609     (cond ((null show)
1610            nil)
1611           ((eq t show)
1612            t)
1613           (t
1614            (funcall show widget value)))))
1615
1616 (defvar custom-load-recursion nil
1617   "Hack to avoid recursive dependencies.")
1618
1619 (defun custom-load-symbol (symbol)
1620   "Load all dependencies for SYMBOL."
1621   (unless custom-load-recursion
1622     (let ((custom-load-recursion t)
1623           (loads (get symbol 'custom-loads))
1624           load)
1625       (while loads
1626         (setq load (car loads)
1627               loads (cdr loads))
1628         (cond ((symbolp load)
1629                (condition-case nil
1630                    (require load)
1631                  (error nil)))
1632               ;; Don't reload a file already loaded.
1633               ((and (boundp 'preloaded-file-list)
1634                     (member load preloaded-file-list)))
1635               ((assoc load load-history))
1636               ((assoc (locate-library load) load-history))
1637               (t
1638                (condition-case nil
1639                    ;; Without this, we would load cus-edit recursively.
1640                    ;; We are still loading it when we call this,
1641                    ;; and it is not in load-history yet.
1642                    (or (equal load "cus-edit")
1643                        (load-library load))
1644                  (error nil))))))))
1645
1646 (defun custom-load-widget (widget)
1647   "Load all dependencies for WIDGET."
1648   (custom-load-symbol (widget-value widget)))
1649
1650 (defun custom-unloaded-symbol-p (symbol)
1651   "Return non-nil if the dependencies of SYMBOL has not yet been loaded."
1652   (let ((found nil)
1653         (loads (get symbol 'custom-loads))
1654         load)
1655     (while loads
1656       (setq load (car loads)
1657             loads (cdr loads))
1658       (cond ((symbolp load)
1659              (unless (featurep load)
1660                (setq found t)))
1661             ((assoc load load-history))
1662             ((assoc (locate-library load) load-history)
1663              ;; #### WTF???
1664              (message nil))
1665             (t
1666              (setq found t))))
1667     found))
1668
1669 (defun custom-unloaded-widget-p (widget)
1670   "Return non-nil if the dependencies of WIDGET has not yet been loaded."
1671   (custom-unloaded-symbol-p (widget-value widget)))
1672
1673 (defun custom-toggle-hide (widget)
1674   "Toggle visibility of WIDGET."
1675   (custom-load-widget widget)
1676   (let ((state (widget-get widget :custom-state)))
1677     (cond ((memq state '(invalid modified))
1678            (error "There are unset changes"))
1679           ((eq state 'hidden)
1680            (widget-put widget :custom-state 'unknown))
1681           (t
1682            (widget-put widget :documentation-shown nil)
1683            (widget-put widget :custom-state 'hidden)))
1684     (custom-redraw widget)
1685     (widget-setup)))
1686
1687 (defun custom-toggle-parent (widget &rest ignore)
1688   "Toggle visibility of parent of WIDGET."
1689   (custom-toggle-hide (widget-get widget :parent)))
1690
1691 (defun custom-add-see-also (widget &optional prefix)
1692   "Add `See also ...' to WIDGET if there are any links.
1693 Insert PREFIX first if non-nil."
1694   (let* ((symbol (widget-get widget :value))
1695          (links (get symbol 'custom-links))
1696          (many (> (length links) 2))
1697          (buttons (widget-get widget :buttons))
1698          (indent (widget-get widget :indent)))
1699     (when links
1700       (when indent
1701         (insert-char ?\  indent))
1702       (when prefix
1703         (insert prefix))
1704       (insert "See also ")
1705       (while links
1706         (push (widget-create-child-and-convert widget (car links))
1707               buttons)
1708         (setq links (cdr links))
1709         (cond ((null links)
1710                (insert ".\n"))
1711               ((null (cdr links))
1712                (if many
1713                    (insert ", and ")
1714                  (insert " and ")))
1715               (t
1716                (insert ", "))))
1717       (widget-put widget :buttons buttons))))
1718
1719 (defun custom-add-parent-links (widget &optional initial-string)
1720   "Add \"Parent groups: ...\" to WIDGET if the group has parents.
1721 The value if non-nil if any parents were found.
1722 If INITIAL-STRING is non-nil, use that rather than \"Parent groups:\"."
1723   (let ((name (widget-value widget))
1724         (type (widget-type widget))
1725         (buttons (widget-get widget :buttons))
1726         (start (point))
1727         found)
1728     (insert (or initial-string "Parent groups:"))
1729     (maphash (lambda (group ignore)
1730                (let ((entry (assq name (get group 'custom-group))))
1731                  (when (eq (nth 1 entry) type)
1732                    (insert " ")
1733                    (push (widget-create-child-and-convert
1734                           widget 'custom-group-link
1735                           :tag (custom-unlispify-tag-name group)
1736                           group)
1737                          buttons)
1738                    (setq found t))))
1739              custom-group-hash-table)
1740     (widget-put widget :buttons buttons)
1741     (if found
1742         (insert "\n")
1743       (delete-region start (point)))
1744     found))
1745
1746 ;;; The `custom-comment' Widget.
1747
1748 ;; like the editable field
1749 (defface custom-comment-face '((((class grayscale color)
1750                                  (background light))
1751                                 (:background "gray85"))
1752                                (((class grayscale color)
1753                                  (background dark))
1754                                 (:background "dim gray"))
1755                                (t
1756                                 (:italic t)))
1757   "Face used for comments on variables or faces"
1758   :group 'custom-faces)
1759
1760 ;; like font-lock-comment-face
1761 (defface custom-comment-tag-face
1762   '((((class color) (background dark)) (:foreground "gray80"))
1763     (((class color) (background light)) (:foreground "blue4"))
1764     (((class grayscale) (background light))
1765      (:foreground "DimGray" :bold t :italic t))
1766     (((class grayscale) (background dark))
1767      (:foreground "LightGray" :bold t :italic t))
1768     (t (:bold t)))
1769   "Face used for variables or faces comment tags"
1770   :group 'custom-faces)
1771
1772 (define-widget 'custom-comment 'string
1773   "User comment"
1774   :tag "Comment"
1775   :help-echo "Edit a comment here"
1776   :sample-face 'custom-comment-tag-face
1777   :value-face 'custom-comment-face
1778   :value-set 'custom-comment-value-set
1779   :create 'custom-comment-create
1780   :delete 'custom-comment-delete)
1781
1782 (defun custom-comment-create (widget)
1783   (let (ext)
1784     (widget-default-create widget)
1785     (widget-put widget :comment-extent
1786                 (setq ext (make-extent (widget-get widget :from)
1787                                        (widget-get widget :to))))
1788     (set-extent-property ext 'start-open t)
1789     (when (equal (widget-get widget :value) "")
1790       (set-extent-property ext 'invisible t))
1791     ))
1792
1793 (defun custom-comment-delete (widget)
1794   (widget-default-delete widget)
1795   (delete-extent (widget-get widget :comment-extent)))
1796
1797 (defun custom-comment-value-set (widget value)
1798   (widget-default-value-set widget value)
1799   (if (equal value "")
1800       (set-extent-property (widget-get widget :comment-extent)
1801                            'invisible t)
1802     (set-extent-property (widget-get widget :comment-extent)
1803                          'invisible nil)))
1804
1805 ;; Those functions are for the menu. WIDGET is NOT the comment widget. It's
1806 ;; the global custom one
1807 (defun custom-comment-show (widget)
1808   (set-extent-property
1809    (widget-get (widget-get widget :comment-widget) :comment-extent)
1810    'invisible nil))
1811
1812 (defun custom-comment-invisible-p (widget)
1813   (extent-property
1814    (widget-get (widget-get widget :comment-widget) :comment-extent)
1815    'invisible))
1816
1817 ;;; The `custom-variable' Widget.
1818
1819 (defface custom-variable-tag-face '((((class color)
1820                                       (background dark))
1821                                      (:foreground "light blue" :underline t))
1822                                     (((class color)
1823                                       (background light))
1824                                      (:foreground "blue" :underline t))
1825                                     (t (:underline t)))
1826   "Face used for unpushable variable tags."
1827   :group 'custom-faces)
1828
1829 (defface custom-variable-button-face '((t (:underline t :bold t)))
1830   "Face used for pushable variable tags."
1831   :group 'custom-faces)
1832
1833 (defcustom custom-variable-default-form 'edit
1834   "Default form of displaying variable values."
1835   :type '(choice (const edit)
1836                  (const lisp))
1837   :group 'custom-buffer)
1838
1839 (define-widget 'custom-variable 'custom
1840   "Customize variable."
1841   :format "%v"
1842   :help-echo "Set or reset this variable"
1843   :documentation-property 'variable-documentation
1844   :custom-category 'option
1845   :custom-state nil
1846   :custom-menu 'custom-variable-menu-create
1847   :custom-form nil ; defaults to value of `custom-variable-default-form'
1848   :value-create 'custom-variable-value-create
1849   :action 'custom-variable-action
1850   :custom-set 'custom-variable-set
1851   :custom-save 'custom-variable-save
1852   :custom-reset-current 'custom-redraw
1853   :custom-reset-saved 'custom-variable-reset-saved
1854   :custom-reset-standard 'custom-variable-reset-standard)
1855
1856 (defun custom-variable-type (symbol)
1857   "Return a widget suitable for editing the value of SYMBOL.
1858 If SYMBOL has a `custom-type' property, use that.
1859 Otherwise, look up symbol in `custom-guess-type-alist'."
1860   (let* ((type (or (get symbol 'custom-type)
1861                    (and (not (get symbol 'standard-value))
1862                         (custom-guess-type symbol))
1863                    'sexp))
1864          (options (get symbol 'custom-options))
1865          (tmp (if (listp type)
1866                   (copy-sequence type)
1867                 (list type))))
1868     (when options
1869       (widget-put tmp :options options))
1870     tmp))
1871
1872 (defun custom-variable-value-create (widget)
1873   "Here is where you edit the variables value."
1874   (custom-load-widget widget)
1875   (unless (widget-get widget :custom-form)
1876     (widget-put widget :custom-form custom-variable-default-form))
1877   (let* ((buttons (widget-get widget :buttons))
1878          (children (widget-get widget :children))
1879          (form (widget-get widget :custom-form))
1880          (state (widget-get widget :custom-state))
1881          (symbol (widget-get widget :value))
1882          (tag (widget-get widget :tag))
1883          (type (custom-variable-type symbol))
1884          (conv (widget-convert type))
1885          (get (or (get symbol 'custom-get) 'default-value))
1886          (prefix (widget-get widget :custom-prefix))
1887          (last (widget-get widget :custom-last))
1888          (value (if (default-boundp symbol)
1889                     (funcall get symbol)
1890                   (widget-get conv :value))))
1891     ;; If the widget is new, the child determine whether it is hidden.
1892     (cond (state)
1893           ((custom-show type value)
1894            (setq state 'unknown))
1895           (t
1896            (setq state 'hidden)))
1897     ;; If we don't know the state, see if we need to edit it in lisp form.
1898     (when (eq state 'unknown)
1899       (unless (widget-apply conv :match value)
1900         ;; (widget-apply (widget-convert type) :match value)
1901         (setq form 'mismatch)))
1902     ;; Now we can create the child widget.
1903     (cond ((eq custom-buffer-style 'tree)
1904            (insert prefix (if last " `--- " " |--- "))
1905            (push (widget-create-child-and-convert
1906                   widget 'custom-browse-variable-tag)
1907                  buttons)
1908            (insert " " tag "\n")
1909            (widget-put widget :buttons buttons))
1910           ((eq state 'hidden)
1911            ;; Indicate hidden value.
1912            (push (widget-create-child-and-convert
1913                   widget 'item
1914                   :format "%{%t%}: "
1915                   :sample-face 'custom-variable-tag-face
1916                   :tag tag
1917                   :parent widget)
1918                  buttons)
1919            (push (widget-create-child-and-convert
1920                   widget 'visibility
1921                   :help-echo "Show the value of this option"
1922                   :action 'custom-toggle-parent
1923                   nil)
1924                  buttons))
1925           ((memq form '(lisp mismatch))
1926            ;; In lisp mode edit the saved value when possible.
1927            (let* ((value (cond ((get symbol 'saved-value)
1928                                 (car (get symbol 'saved-value)))
1929                                ((get symbol 'standard-value)
1930                                 (car (get symbol 'standard-value)))
1931                                ((default-boundp symbol)
1932                                 (custom-quote (funcall get symbol)))
1933                                (t
1934                                 (custom-quote (widget-get conv :value))))))
1935              (insert (symbol-name symbol) ": ")
1936              (push (widget-create-child-and-convert
1937                     widget 'visibility
1938                     :help-echo "Hide the value of this option"
1939                     :action 'custom-toggle-parent
1940                     t)
1941                    buttons)
1942              (insert " ")
1943              (push (widget-create-child-and-convert
1944                     widget 'sexp
1945                     :button-face 'custom-variable-button-face
1946                     :format "%v"
1947                     :tag (symbol-name symbol)
1948                     :parent widget
1949                     :value value)
1950                    children)))
1951           (t
1952            ;; Edit mode.
1953            (let* ((format (widget-get type :format))
1954                   tag-format value-format)
1955              (while (not (string-match ":" format))
1956                (setq format (signal 'error (list "Bad format" format))))
1957              (setq tag-format (substring format 0 (match-end 0)))
1958              (setq value-format (substring format (match-end 0)))
1959              (push (widget-create-child-and-convert
1960                     widget 'item
1961                     :format tag-format
1962                     :action 'custom-tag-action
1963                     :help-echo "Change value of this option"
1964                     :mouse-down-action 'custom-tag-mouse-down-action
1965                     :button-face 'custom-variable-button-face
1966                     :sample-face 'custom-variable-tag-face
1967                     tag)
1968                    buttons)
1969              (insert " ")
1970              (push (widget-create-child-and-convert
1971                   widget 'visibility
1972                   :help-echo "Hide the value of this option"
1973                   :action 'custom-toggle-parent
1974                   t)
1975                  buttons)
1976              (push (widget-create-child-and-convert
1977                     widget type
1978                     :format value-format
1979                     :value value)
1980                    children))))
1981     (unless (eq custom-buffer-style 'tree)
1982       (unless (eq (preceding-char) ?\n)
1983         (widget-insert "\n"))
1984       ;; Create the magic button.
1985       (let ((magic (widget-create-child-and-convert
1986                     widget 'custom-magic nil)))
1987         (widget-put widget :custom-magic magic)
1988         (push magic buttons))
1989       ;; Insert documentation.
1990       ;; #### NOTE: this is ugly!!!! I need to do update the :buttons property
1991       ;; before the call to `widget-default-format-handler'. Otherwise, I
1992       ;; lose my current `buttons'. This function shouldn't be called like
1993       ;; this anyway. The doc string widget should be added like the others.
1994       ;; --dv
1995       (widget-put widget :buttons buttons)
1996       (widget-default-format-handler widget ?h)
1997       ;; The comment field
1998       (unless (eq state 'hidden)
1999         (let* ((comment (get symbol 'variable-comment))
2000                (comment-widget
2001                 (widget-create-child-and-convert
2002                  widget 'custom-comment
2003                  :parent widget
2004                  :value (or comment ""))))
2005           (widget-put widget :comment-widget comment-widget)
2006           ;; Don't push it !!! Custom assumes that the first child is the
2007           ;; value one.
2008           (setq children (append children (list comment-widget)))))
2009       ;; Update the rest of the properties properties.
2010       (widget-put widget :custom-form form)
2011       (widget-put widget :children children)
2012       ;; Now update the state.
2013       (if (eq state 'hidden)
2014           (widget-put widget :custom-state state)
2015         (custom-variable-state-set widget))
2016       ;; See also.
2017       (unless (eq state 'hidden)
2018         (when (eq (widget-get widget :custom-level) 1)
2019           (custom-add-parent-links widget))
2020         (custom-add-see-also widget)))))
2021
2022 (defun custom-tag-action (widget &rest args)
2023   "Pass :action to first child of WIDGET's parent."
2024   (apply 'widget-apply (car (widget-get (widget-get widget :parent) :children))
2025          :action args))
2026
2027 (defun custom-tag-mouse-down-action (widget &rest args)
2028   "Pass :mouse-down-action to first child of WIDGET's parent."
2029   (apply 'widget-apply (car (widget-get (widget-get widget :parent) :children))
2030          :mouse-down-action args))
2031
2032 (defun custom-variable-state-set (widget)
2033   "Set the state of WIDGET."
2034   (let* ((symbol (widget-value widget))
2035          (get (or (get symbol 'custom-get) 'default-value))
2036          (value (if (default-boundp symbol)
2037                     (funcall get symbol)
2038                   (widget-get widget :value)))
2039          (comment (get symbol 'variable-comment))
2040          tmp
2041          temp
2042          (state (cond ((progn (setq tmp (get symbol 'customized-value))
2043                               (setq temp
2044                                     (get symbol 'customized-variable-comment))
2045                               (or tmp temp))
2046                        (if (condition-case nil
2047                                (and (equal value (eval (car tmp)))
2048                                     (equal comment temp))
2049                              (error nil))
2050                            'set
2051                          'changed))
2052                       ((progn (setq tmp (get symbol 'saved-value))
2053                               (setq temp (get symbol 'saved-variable-comment))
2054                               (or tmp temp))
2055                        (if (condition-case nil
2056                                (and (equal value (eval (car tmp)))
2057                                     (equal comment temp))
2058                              (error nil))
2059                            'saved
2060                          'changed))
2061                       ((setq tmp (get symbol 'standard-value))
2062                        (if (condition-case nil
2063                                (and (equal value (eval (car tmp)))
2064                                     (equal comment nil))
2065                              (error nil))
2066                            'standard
2067                          'changed))
2068                       (t 'rogue))))
2069     (widget-put widget :custom-state state)))
2070
2071 (defvar custom-variable-menu
2072   '(("Set for Current Session" custom-variable-set
2073      (lambda (widget)
2074        (eq (widget-get widget :custom-state) 'modified)))
2075     ("Save for Future Sessions" custom-variable-save
2076      (lambda (widget)
2077        (memq (widget-get widget :custom-state) '(modified set changed rogue))))
2078     ("Reset to Current" custom-redraw
2079      (lambda (widget)
2080        (and (default-boundp (widget-value widget))
2081             (memq (widget-get widget :custom-state) '(modified changed)))))
2082     ("Reset to Saved" custom-variable-reset-saved
2083      (lambda (widget)
2084        (and (or (get (widget-value widget) 'saved-value)
2085                 (get (widget-value widget) 'saved-variable-comment))
2086             (memq (widget-get widget :custom-state)
2087                   '(modified set changed rogue)))))
2088     ("Reset to Standard Settings" custom-variable-reset-standard
2089      (lambda (widget)
2090        (and (get (widget-value widget) 'standard-value)
2091             (memq (widget-get widget :custom-state)
2092                   '(modified set changed saved rogue)))))
2093     ("---" ignore ignore)
2094     ("Add Comment" custom-comment-show custom-comment-invisible-p)
2095     ("---" ignore ignore)
2096     ("Don't show as Lisp expression" custom-variable-edit
2097      (lambda (widget)
2098        (eq (widget-get widget :custom-form) 'lisp)))
2099     ("Show as Lisp expression" custom-variable-edit-lisp
2100      (lambda (widget)
2101        (eq (widget-get widget :custom-form) 'edit))))
2102   "Alist of actions for the `custom-variable' widget.
2103 Each entry has the form (NAME ACTION FILTER) where NAME is the name of
2104 the menu entry, ACTION is the function to call on the widget when the
2105 menu is selected, and FILTER is a predicate which takes a `custom-variable'
2106 widget as an argument, and returns non-nil if ACTION is valid on that
2107 widget. If FILTER is nil, ACTION is always valid.")
2108
2109 (defun custom-variable-action (widget &optional event)
2110   "Show the menu for `custom-variable' WIDGET.
2111 Optional EVENT is the location for the menu."
2112   (if (eq (widget-get widget :custom-state) 'hidden)
2113       (custom-toggle-hide widget)
2114     (unless (eq (widget-get widget :custom-state) 'modified)
2115       (custom-variable-state-set widget))
2116     ;; Redrawing magic also depresses the state glyph.
2117     ;(custom-redraw-magic widget)
2118     (let* ((completion-ignore-case t)
2119            (answer (widget-choose (concat "Operation on "
2120                                           (custom-unlispify-tag-name
2121                                            (widget-get widget :value)))
2122                                   (custom-menu-filter custom-variable-menu
2123                                                       widget)
2124                                   event)))
2125       (if answer
2126           (funcall answer widget)))))
2127
2128 (defun custom-variable-edit (widget)
2129   "Edit value of WIDGET."
2130   (widget-put widget :custom-state 'unknown)
2131   (widget-put widget :custom-form 'edit)
2132   (custom-redraw widget))
2133
2134 (defun custom-variable-edit-lisp (widget)
2135   "Edit the lisp representation of the value of WIDGET."
2136   (widget-put widget :custom-state 'unknown)
2137   (widget-put widget :custom-form 'lisp)
2138   (custom-redraw widget))
2139
2140 (defun custom-variable-set (widget)
2141   "Set the current value for the variable being edited by WIDGET."
2142   (let* ((form (widget-get widget :custom-form))
2143          (state (widget-get widget :custom-state))
2144          (child (car (widget-get widget :children)))
2145          (symbol (widget-value widget))
2146          (set (or (get symbol 'custom-set) 'set-default))
2147          (comment-widget (widget-get widget :comment-widget))
2148          (comment (widget-value comment-widget))
2149          val)
2150     (cond ((eq state 'hidden)
2151            (error "Cannot set hidden variable"))
2152           ((setq val (widget-apply child :validate))
2153            (goto-char (widget-get val :from))
2154            (error "%s" (widget-get val :error)))
2155           ((memq form '(lisp mismatch))
2156            (when (equal comment "")
2157              (setq comment nil)
2158              ;; Make the comment invisible by hand if it's empty
2159              (set-extent-property (widget-get comment-widget :comment-extent)
2160                                   'invisible t))
2161            (funcall set symbol (eval (setq val (widget-value child))))
2162            (put symbol 'customized-value (list val))
2163            (put symbol 'variable-comment comment)
2164            (put symbol 'customized-variable-comment comment))
2165           (t
2166            (when (equal comment "")
2167              (setq comment nil)
2168              ;; Make the comment invisible by hand if it's empty
2169              (set-extent-property (widget-get comment-widget :comment-extent)
2170                                   'invisible t))
2171            (funcall set symbol (setq val (widget-value child)))
2172            (put symbol 'customized-value (list (custom-quote val)))
2173            (put symbol 'variable-comment comment)
2174            (put symbol 'customized-variable-comment comment)))
2175     (custom-variable-state-set widget)
2176     (custom-redraw-magic widget)))
2177
2178 (defun custom-variable-save (widget)
2179   "Set and save the value for the variable being edited by WIDGET."
2180   (let* ((form (widget-get widget :custom-form))
2181          (state (widget-get widget :custom-state))
2182          (child (car (widget-get widget :children)))
2183          (symbol (widget-value widget))
2184          (set (or (get symbol 'custom-set) 'set-default))
2185          (comment-widget (widget-get widget :comment-widget))
2186          (comment (widget-value comment-widget))
2187          val)
2188     (cond ((eq state 'hidden)
2189            (error "Cannot set hidden variable"))
2190           ((setq val (widget-apply child :validate))
2191            (goto-char (widget-get val :from))
2192            (error "%s" (widget-get val :error)))
2193           ((memq form '(lisp mismatch))
2194            (when (equal comment "")
2195              (setq comment nil)
2196              ;; Make the comment invisible by hand if it's empty
2197              (set-extent-property (widget-get comment-widget :comment-extent)
2198                                   'invisible t))
2199            (put symbol 'saved-value (list (widget-value child)))
2200            (custom-push-theme 'theme-value symbol 'user
2201                               'set (list (widget-value child)))
2202            (funcall set symbol (eval (widget-value child)))
2203            (put symbol 'variable-comment comment)
2204            (put symbol 'saved-variable-comment comment))
2205           (t
2206            (when (equal comment "")
2207              (setq comment nil)
2208              ;; Make the comment invisible by hand if it's empty
2209              (set-extent-property (widget-get comment-widget :comment-extent)
2210                                   'invisible t))
2211            (put symbol
2212                 'saved-value (list (custom-quote (widget-value
2213                                                   child))))
2214            (custom-push-theme 'theme-value symbol 'user
2215                               'set (list (custom-quote (widget-value
2216                                                   child))))
2217            (funcall set symbol (widget-value child))
2218            (put symbol 'variable-comment comment)
2219            (put symbol 'saved-variable-comment comment)))
2220     (put symbol 'customized-value nil)
2221     (put symbol 'customized-variable-comment nil)
2222     (custom-save-all)
2223     (custom-variable-state-set widget)
2224     (custom-redraw-magic widget)))
2225
2226 (defun custom-variable-reset-saved (widget)
2227   "Restore the saved value for the variable being edited by WIDGET."
2228   (let* ((symbol (widget-value widget))
2229          (set (or (get symbol 'custom-set) 'set-default))
2230          (value (get symbol 'saved-value))
2231          (comment (get symbol 'saved-variable-comment)))
2232     (cond ((or value comment)
2233            (put symbol 'variable-comment comment)
2234            (condition-case nil
2235                (funcall set symbol (eval (car value)))
2236              (error nil)))
2237           (t
2238            (signal 'error (list "No saved value for variable" symbol))))
2239     (put symbol 'customized-value nil)
2240     (put symbol 'customized-variable-comment nil)
2241     (widget-put widget :custom-state 'unknown)
2242     ;; This call will possibly make the comment invisible
2243     (custom-redraw widget)))
2244
2245 (defun custom-variable-reset-standard (widget)
2246   "Restore the standard setting for the variable being edited by WIDGET."
2247   (let* ((symbol (widget-value widget))
2248          (set (or (get symbol 'custom-set) 'set-default)))
2249     (if (get symbol 'standard-value)
2250         (funcall set symbol (eval (car (get symbol 'standard-value))))
2251       (signal 'error (list "No standard setting known for variable" symbol)))
2252     (put symbol 'variable-comment nil)
2253     (put symbol 'customized-value nil)
2254     (put symbol 'customized-variable-comment nil)
2255     (when (or (get symbol 'saved-value) (get symbol 'saved-variable-comment))
2256       (put symbol 'saved-value nil)
2257       (custom-push-theme 'theme-value symbol 'user 'reset 'standard)
2258       ;; As a special optimizations we do not (explictly)
2259       ;; save resets to standard when no theme set the value.
2260       (if (null (cdr (get symbol 'theme-value)))
2261           (put symbol 'theme-value nil))
2262       (put symbol 'saved-variable-comment nil)
2263       (custom-save-all))
2264     (widget-put widget :custom-state 'unknown)
2265     ;; This call will possibly make the comment invisible
2266     (custom-redraw widget)))
2267
2268 ;;; The `custom-face-edit' Widget.
2269
2270 (define-widget 'custom-face-edit 'checklist
2271   "Edit face attributes."
2272   :format "%t: %v"
2273   :tag "Attributes"
2274   :extra-offset 12
2275   :button-args '(:help-echo "Control whether this attribute has any effect")
2276   :args (mapcar (lambda (att)
2277                   (list 'group
2278                         :inline t
2279                         :sibling-args (widget-get (nth 1 att) :sibling-args)
2280                         (list 'const :format "" :value (nth 0 att))
2281                         (nth 1 att)))
2282                 custom-face-attributes))
2283
2284 ;;; The `custom-display' Widget.
2285
2286 (define-widget 'custom-display 'menu-choice
2287   "Select a display type."
2288   :tag "Display"
2289   :value t
2290   :help-echo "Specify frames where the face attributes should be used"
2291   :args '((const :tag "all" t)
2292           (checklist
2293            :offset 0
2294            :extra-offset 9
2295            :args ((group :sibling-args (:help-echo "\
2296 Only match the specified window systems")
2297                          (const :format "Type: "
2298                                 type)
2299                          (checklist :inline t
2300                                     :offset 0
2301                                     (const :format "X "
2302                                            :sibling-args (:help-echo "\
2303 The X11 Window System")
2304                                            x)
2305                                     (const :format "PM "
2306                                            :sibling-args (:help-echo "\
2307 OS/2 Presentation Manager")
2308                                            pm)
2309                                     (const :format "MSWindows "
2310                                            :sibling-args (:help-echo "\
2311 Microsoft Windows, displays")
2312                                            mswindows)
2313                                     (const :format "MSPrinter "
2314                                            :sibling-args (:help-echo "\
2315 Microsoft Windows, printers")
2316                                            msprinter)
2317                                     (const :format "TTY%n"
2318                                            :sibling-args (:help-echo "\
2319 Plain text terminals")
2320                                            tty)))
2321                   (group :sibling-args (:help-echo "\
2322 Only match display or printer devices")
2323                          (const :format "Output: "
2324                                 class)
2325                          (checklist :inline t
2326                                     :offset 0
2327                                     (const :format "Display "
2328                                            :sibling-args (:help-echo "\
2329 Match display devices")
2330                                            display)
2331                                     (const :format "Printer%n"
2332                                            :sibling-args (:help-echo "\
2333 Match printer devices")
2334                                            printer)))
2335                   (group :sibling-args (:help-echo "\
2336 Only match the frames with the specified color support")
2337                          (const :format "Color support: "
2338                                 class)
2339                          (checklist :inline t
2340                                     :offset 0
2341                                     (const :format "Color "
2342                                            :sibling-args (:help-echo "\
2343 Match color frames")
2344                                            color)
2345                                     (const :format "Grayscale "
2346                                            :sibling-args (:help-echo "\
2347 Match grayscale frames")
2348                                            grayscale)
2349                                     (const :format "Monochrome%n"
2350                                            :sibling-args (:help-echo "\
2351 Match frames with no color support")
2352                                            mono)))
2353                   (group :sibling-args (:help-echo "\
2354 Only match frames with the specified intensity")
2355                          (const :format "\
2356 Background brightness: "
2357                                 background)
2358                          (checklist :inline t
2359                                     :offset 0
2360                                     (const :format "Light "
2361                                            :sibling-args (:help-echo "\
2362 Match frames with light backgrounds")
2363                                            light)
2364                                     (const :format "Dark\n"
2365                                            :sibling-args (:help-echo "\
2366 Match frames with dark backgrounds")
2367                                            dark)))))))
2368
2369 ;;; The `custom-face' Widget.
2370
2371 (defface custom-face-tag-face '((t (:underline t)))
2372   "Face used for face tags."
2373   :group 'custom-faces)
2374
2375 (defcustom custom-face-default-form 'selected
2376   "Default form of displaying face definition."
2377   :type '(choice (const all)
2378                  (const selected)
2379                  (const lisp))
2380   :group 'custom-buffer)
2381
2382 (define-widget 'custom-face 'custom
2383   "Customize face."
2384   :sample-face 'custom-face-tag-face
2385   :help-echo "Set or reset this face"
2386   :documentation-property '(lambda (face)
2387                              (face-doc-string face))
2388   :value-create 'custom-face-value-create
2389   :action 'custom-face-action
2390   :custom-category 'face
2391   :custom-form nil ; defaults to value of `custom-face-default-form'
2392   :custom-set 'custom-face-set
2393   :custom-save 'custom-face-save
2394   :custom-reset-current 'custom-redraw
2395   :custom-reset-saved 'custom-face-reset-saved
2396   :custom-reset-standard 'custom-face-reset-standard
2397   :custom-menu 'custom-face-menu-create)
2398
2399 (define-widget 'custom-face-all 'editable-list
2400   "An editable list of display specifications and attributes."
2401   :entry-format "%i %d %v"
2402   :insert-button-args '(:help-echo "Insert new display specification here")
2403   :append-button-args '(:help-echo "Append new display specification here")
2404   :delete-button-args '(:help-echo "Delete this display specification")
2405   :args '((group :format "%v" custom-display custom-face-edit)))
2406
2407 (defconst custom-face-all (widget-convert 'custom-face-all)
2408   "Converted version of the `custom-face-all' widget.")
2409
2410 (define-widget 'custom-display-unselected 'item
2411   "A display specification that doesn't match the selected display."
2412   :match 'custom-display-unselected-match)
2413
2414 (defun custom-display-unselected-match (widget value)
2415   "Non-nil if VALUE is an unselected display specification."
2416   (not (face-spec-set-match-display value (selected-frame))))
2417
2418 (define-widget 'custom-face-selected 'group
2419   "Edit the attributes of the selected display in a face specification."
2420   :args '((repeat :format ""
2421                   :inline t
2422                   (group custom-display-unselected sexp))
2423           (group (sexp :format "") custom-face-edit)
2424           (repeat :format ""
2425                   :inline t
2426                   sexp)))
2427
2428 (defconst custom-face-selected (widget-convert 'custom-face-selected)
2429   "Converted version of the `custom-face-selected' widget.")
2430
2431 (defun custom-face-value-create (widget)
2432   "Create a list of the display specifications for WIDGET."
2433   (let ((buttons (widget-get widget :buttons))
2434         children
2435         (symbol (widget-get widget :value))
2436         (tag (widget-get widget :tag))
2437         (state (widget-get widget :custom-state))
2438         (begin (point))
2439         (is-last (widget-get widget :custom-last))
2440         (prefix (widget-get widget :custom-prefix)))
2441     (unless tag
2442       (setq tag (prin1-to-string symbol)))
2443     (cond ((eq custom-buffer-style 'tree)
2444            (insert prefix (if is-last " `--- " " |--- "))
2445            (push (widget-create-child-and-convert
2446                   widget 'custom-browse-face-tag)
2447                  buttons)
2448            (insert " " tag "\n")
2449            (widget-put widget :buttons buttons))
2450           (t
2451            ;; Create tag.
2452            (insert tag)
2453            (if (eq custom-buffer-style 'face)
2454                (insert " ")
2455              (widget-specify-sample widget begin (point))
2456              (insert ": "))
2457            ;; Sample.
2458            (and (not (find-face symbol))
2459                 ;; XEmacs cannot display uninitialized faces.
2460                 (make-face symbol))
2461            (push (widget-create-child-and-convert widget 'item
2462                                                   :format "(%{%t%})"
2463                                                   :sample-face symbol
2464                                                   :tag "sample")
2465                  buttons)
2466            ;; Visibility.
2467            (insert " ")
2468            (push (widget-create-child-and-convert
2469                   widget 'visibility
2470                   :help-echo "Hide or show this face"
2471                   :action 'custom-toggle-parent
2472                   (not (eq state 'hidden)))
2473                  buttons)
2474            ;; Magic.
2475            (insert "\n")
2476            (let ((magic (widget-create-child-and-convert
2477                          widget 'custom-magic nil)))
2478              (widget-put widget :custom-magic magic)
2479              (push magic buttons))
2480            ;; Update buttons.
2481            (widget-put widget :buttons buttons)
2482            ;; Insert documentation.
2483            (widget-default-format-handler widget ?h)
2484            ;; The comment field
2485            (unless (eq state 'hidden)
2486              (let* ((comment (get symbol 'face-comment))
2487                     (comment-widget
2488                      (widget-create-child-and-convert
2489                       widget 'custom-comment
2490                       :parent widget
2491                       :value (or comment ""))))
2492                (widget-put widget :comment-widget comment-widget)
2493                (push comment-widget children)))
2494            ;; See also.
2495            (unless (eq state 'hidden)
2496              (when (eq (widget-get widget :custom-level) 1)
2497                (custom-add-parent-links widget))
2498              (custom-add-see-also widget))
2499            ;; Editor.
2500            (unless (eq (preceding-char) ?\n)
2501              (insert "\n"))
2502            (unless (eq state 'hidden)
2503              (message "Creating face editor...")
2504              (custom-load-widget widget)
2505              (unless (widget-get widget :custom-form)
2506                  (widget-put widget :custom-form custom-face-default-form))
2507              (let* ((symbol (widget-value widget))
2508                     (spec (custom-face-get-spec symbol))
2509                     (form (widget-get widget :custom-form))
2510                     (indent (widget-get widget :indent))
2511                     (edit (widget-create-child-and-convert
2512                            widget
2513                            (cond ((and (eq form 'selected)
2514                                        (widget-apply custom-face-selected
2515                                                      :match spec))
2516                                   (when indent (insert-char ?\  indent))
2517                                   'custom-face-selected)
2518                                  ((and (not (eq form 'lisp))
2519                                        (widget-apply custom-face-all
2520                                                      :match spec))
2521                                   'custom-face-all)
2522                                  (t
2523                                   (when indent (insert-char ?\  indent))
2524                                   'sexp))
2525                            :value spec)))
2526                (custom-face-state-set widget)
2527                (push edit children)
2528                (widget-put widget :children children))
2529              (message "Creating face editor...done"))))))
2530
2531 (defvar custom-face-menu
2532   '(("Set for Current Session" custom-face-set)
2533     ("Save for Future Sessions" custom-face-save)
2534     ("Reset to Saved" custom-face-reset-saved
2535      (lambda (widget)
2536        (or (get (widget-value widget) 'saved-face)
2537            (get (widget-value widget) 'saved-face-comment))))
2538     ("Reset to Standard Setting" custom-face-reset-standard
2539      (lambda (widget)
2540        (get (widget-value widget) 'face-defface-spec)))
2541     ("---" ignore ignore)
2542     ("Add Comment" custom-comment-show custom-comment-invisible-p)
2543     ("---" ignore ignore)
2544     ("Show all display specs" custom-face-edit-all
2545      (lambda (widget)
2546        (not (eq (widget-get widget :custom-form) 'all))))
2547     ("Just current attributes" custom-face-edit-selected
2548      (lambda (widget)
2549        (not (eq (widget-get widget :custom-form) 'selected))))
2550     ("Show as Lisp expression" custom-face-edit-lisp
2551      (lambda (widget)
2552        (not (eq (widget-get widget :custom-form) 'lisp)))))
2553   "Alist of actions for the `custom-face' widget.
2554 Each entry has the form (NAME ACTION FILTER) where NAME is the name of
2555 the menu entry, ACTION is the function to call on the widget when the
2556 menu is selected, and FILTER is a predicate which takes a `custom-face'
2557 widget as an argument, and returns non-nil if ACTION is valid on that
2558 widget. If FILTER is nil, ACTION is always valid.")
2559
2560 (defun custom-face-edit-selected (widget)
2561   "Edit selected attributes of the value of WIDGET."
2562   (widget-put widget :custom-state 'unknown)
2563   (widget-put widget :custom-form 'selected)
2564   (custom-redraw widget))
2565
2566 (defun custom-face-edit-all (widget)
2567   "Edit all attributes of the value of WIDGET."
2568   (widget-put widget :custom-state 'unknown)
2569   (widget-put widget :custom-form 'all)
2570   (custom-redraw widget))
2571
2572 (defun custom-face-edit-lisp (widget)
2573   "Edit the lisp representation of the value of WIDGET."
2574   (widget-put widget :custom-state 'unknown)
2575   (widget-put widget :custom-form 'lisp)
2576   (custom-redraw widget))
2577
2578 (defun custom-face-state-set (widget)
2579   "Set the state of WIDGET."
2580   (let* ((symbol (widget-value widget))
2581          (comment (get symbol 'face-comment))
2582          tmp temp)
2583     (widget-put widget :custom-state
2584                 (cond ((progn
2585                          (setq tmp (get symbol 'customized-face))
2586                          (setq temp (get symbol 'customized-face-comment))
2587                          (or tmp temp))
2588                        (if (equal temp comment)
2589                            'set
2590                          'changed))
2591                       ((progn
2592                          (setq tmp (get symbol 'saved-face))
2593                          (setq temp (get symbol 'saved-face-comment))
2594                          (or tmp temp))
2595                        (if (equal temp comment)
2596                            'saved
2597                          'changed))
2598                       ((get symbol 'face-defface-spec)
2599                        (if (equal comment nil)
2600                            'standard
2601                          'changed))
2602                       (t
2603                        'rogue)))))
2604
2605 (defun custom-face-action (widget &optional event)
2606   "Show the menu for `custom-face' WIDGET.
2607 Optional EVENT is the location for the menu."
2608   (if (eq (widget-get widget :custom-state) 'hidden)
2609       (custom-toggle-hide widget)
2610     (let* ((completion-ignore-case t)
2611            (symbol (widget-get widget :value))
2612            (answer (widget-choose (concat "Operation on "
2613                                           (custom-unlispify-tag-name symbol))
2614                                   (custom-menu-filter custom-face-menu
2615                                                       widget)
2616                                   event)))
2617       (if answer
2618           (funcall answer widget)))))
2619
2620 (defun custom-face-set (widget)
2621   "Make the face attributes in WIDGET take effect."
2622   (let* ((symbol (widget-value widget))
2623          (child (car (widget-get widget :children)))
2624          (value (widget-value child))
2625          (comment-widget (widget-get widget :comment-widget))
2626          (comment (widget-value comment-widget)))
2627     (when (equal comment "")
2628       (setq comment nil)
2629       ;; Make the comment invisible by hand if it's empty
2630       (set-extent-property (widget-get comment-widget :comment-extent)
2631                            'invisible t))
2632     (put symbol 'customized-face value)
2633     (face-spec-set symbol value nil '(custom))
2634     (put symbol 'customized-face-comment comment)
2635     (put symbol 'face-comment comment)
2636     (custom-face-state-set widget)
2637     (custom-redraw-magic widget)))
2638
2639 (defun custom-face-save (widget)
2640   "Make the face attributes in WIDGET default."
2641   (let* ((symbol (widget-value widget))
2642          (child (car (widget-get widget :children)))
2643          (value (widget-value child))
2644          (comment-widget (widget-get widget :comment-widget))
2645          (comment (widget-value comment-widget)))
2646     (when (equal comment "")
2647       (setq comment nil)
2648       ;; Make the comment invisible by hand if it's empty
2649       (set-extent-property (widget-get comment-widget :comment-extent)
2650                            'invisible t))
2651     (face-spec-set symbol value nil '(custom))
2652     (put symbol 'saved-face value)
2653     (custom-push-theme 'theme-face symbol 'user 'set value)
2654     (put symbol 'customized-face nil)
2655     (put symbol 'face-comment comment)
2656     (put symbol 'customized-face-comment nil)
2657     (put symbol 'saved-face-comment comment)
2658     (custom-save-all)
2659     (custom-face-state-set widget)
2660     (custom-redraw-magic widget)))
2661
2662 (defun custom-face-reset-saved (widget)
2663   "Restore WIDGET to the face's default attributes."
2664   (let* ((symbol (widget-value widget))
2665          (child (car (widget-get widget :children)))
2666          (value (get symbol 'saved-face))
2667          (comment (get symbol 'saved-face-comment))
2668          (comment-widget (widget-get widget :comment-widget)))
2669     (unless (or value comment)
2670       (signal 'error (list "No saved value for this face" symbol)))
2671     (put symbol 'customized-face nil)
2672     (put symbol 'customized-face-comment nil)
2673     (face-spec-set symbol value nil '(custom))
2674     (put symbol 'face-comment comment)
2675     (widget-value-set child value)
2676     ;; This call manages the comment visibility
2677     (widget-value-set comment-widget (or comment ""))
2678     (custom-face-state-set widget)
2679     (custom-redraw-magic widget)))
2680
2681 (defun custom-face-reset-standard (widget)
2682   "Restore WIDGET to the face's standard settings."
2683   (let* ((symbol (widget-value widget))
2684          (child (car (widget-get widget :children)))
2685          (value (get symbol 'face-defface-spec))
2686          (comment-widget (widget-get widget :comment-widget)))
2687     (unless value
2688       (signal 'error (list "No standard setting for this face" symbol)))
2689     (put symbol 'customized-face nil)
2690     (put symbol 'customized-face-comment nil)
2691     (when (or (get symbol 'saved-face) (get symbol 'saved-face-comment))
2692       (put symbol 'saved-face nil)
2693       (custom-push-theme 'theme-face symbol 'user 'reset 'standard)
2694       ;; Do not explictly save resets to standards without themes.
2695       (if (null (cdr (get symbol 'theme-face)))
2696           (put symbol  'theme-face nil))
2697       (put symbol 'saved-face-comment nil)
2698       (custom-save-all))
2699     (face-spec-set symbol value nil '(custom))
2700     (put symbol 'face-comment nil)
2701     (widget-value-set child value)
2702     ;; This call manages the comment visibility
2703     (widget-value-set comment-widget "")
2704     (custom-face-state-set widget)
2705     (custom-redraw-magic widget)))
2706
2707 ;;; The `face' Widget.
2708
2709 (define-widget 'face 'default
2710   "Select and customize a face."
2711   :convert-widget 'widget-value-convert-widget
2712   :button-prefix 'widget-push-button-prefix
2713   :button-suffix 'widget-push-button-suffix
2714   :format "%t: %[select face%] %v"
2715   :tag "Face"
2716   :value 'default
2717   :value-create 'widget-face-value-create
2718   :value-delete 'widget-face-value-delete
2719   :value-get 'widget-value-value-get
2720   :validate 'widget-children-validate
2721   :action 'widget-face-action
2722   :match (lambda (widget value) (symbolp value)))
2723
2724 (defun widget-face-value-create (widget)
2725   ;; Create a `custom-face' child.
2726   (let* ((symbol (widget-value widget))
2727          (custom-buffer-style 'face)
2728          (child (widget-create-child-and-convert
2729                  widget 'custom-face
2730                  :custom-level nil
2731                  :value symbol)))
2732     (custom-magic-reset child)
2733     (setq custom-options (cons child custom-options))
2734     (widget-put widget :children (list child))))
2735
2736 (defun widget-face-value-delete (widget)
2737   ;; Remove the child from the options.
2738   (let ((child (car (widget-get widget :children))))
2739     (setq custom-options (delq child custom-options))
2740     (widget-children-value-delete widget)))
2741
2742 (defvar face-history nil
2743   "History of entered face names.")
2744
2745 (defun widget-face-action (widget &optional event)
2746   "Prompt for a face."
2747   (let ((answer (completing-read "Face: "
2748                                  (mapcar (lambda (face)
2749                                            (list (symbol-name face)))
2750                                          (face-list))
2751                                  nil nil nil
2752                                  'face-history)))
2753     (unless (zerop (length answer))
2754       (widget-value-set widget (intern answer))
2755       (widget-apply widget :notify widget event)
2756       (widget-setup))))
2757
2758 ;;; The `hook' Widget.
2759
2760 (define-widget 'hook 'list
2761   "A emacs lisp hook"
2762   :value-to-internal (lambda (widget value)
2763                        (if (symbolp value)
2764                            (list value)
2765                          value))
2766   :match (lambda (widget value)
2767            (or (symbolp value)
2768                (widget-group-match widget value)))
2769   :convert-widget 'custom-hook-convert-widget
2770   :tag "Hook")
2771
2772 (defun custom-hook-convert-widget (widget)
2773   ;; Handle `:options'.
2774   (let* ((options (widget-get widget :options))
2775          (other `(editable-list :inline t
2776                                 :entry-format "%i %d%v"
2777                                 (function :format " %v")))
2778          (args (if options
2779                    (list `(checklist :inline t
2780                                      ,@(mapcar (lambda (entry)
2781                                                  `(function-item ,entry))
2782                                                options))
2783                          other)
2784                  (list other))))
2785     (widget-put widget :args args)
2786     widget))
2787
2788 ;;; The `plist' Widget.
2789
2790 (define-widget 'plist 'list
2791   "A property list."
2792   :match (lambda (widget value)
2793            (valid-plist-p value))
2794   :convert-widget 'custom-plist-convert-widget
2795   :tag "Property List")
2796
2797 ;; #### Should handle options better.
2798 (defun custom-plist-convert-widget (widget)
2799   (let* ((options (widget-get widget :options))
2800          (other `(editable-list :inline t
2801                                 (group :inline t
2802                                        (symbol :format "%t: %v "
2803                                                :size 10
2804                                                :tag "Property")
2805                                        (sexp :tag "Value"))))
2806          (args
2807           (if options
2808               `((checklist :inline t
2809                            ,@(mapcar 'custom-plist-process-option options))
2810                 ,other)
2811             (list other))))
2812     (widget-put widget :args args)
2813     widget))
2814
2815 (defun custom-plist-process-option (entry)
2816   `(group :inline t
2817           (const :tag "Property"
2818                  :format "%t: %v "
2819                  :size 10
2820                  ,entry)
2821           (sexp :tag "Value")))
2822
2823 ;;; The `custom-group-link' Widget.
2824
2825 (define-widget 'custom-group-link 'link
2826   "Show parent in other window when activated."
2827   :help-echo 'custom-group-link-help-echo
2828   :action 'custom-group-link-action)
2829
2830 (defun custom-group-link-help-echo (widget)
2831   (concat "Create customization buffer for the `"
2832           (custom-unlispify-tag-name (widget-value widget))
2833           "' group"))
2834
2835 (defun custom-group-link-action (widget &rest ignore)
2836   (customize-group (widget-value widget)))
2837
2838 ;;; The `custom-group' Widget.
2839
2840 (defcustom custom-group-tag-faces nil
2841   ;; In XEmacs, this ought to play games with font size.
2842   "Face used for group tags.
2843 The first member is used for level 1 groups, the second for level 2,
2844 and so forth.  The remaining group tags are shown with
2845 `custom-group-tag-face'."
2846   :type '(repeat face)
2847   :group 'custom-faces)
2848
2849 (defface custom-group-tag-face-1 '((((class color)
2850                                      (background dark))
2851                                     (:foreground "pink" :underline t))
2852                                    (((class color)
2853                                      (background light))
2854                                     (:foreground "red" :underline t))
2855                                    (t (:underline t)))
2856   "Face used for group tags.")
2857
2858 (defface custom-group-tag-face '((((class color)
2859                                    (background dark))
2860                                   (:foreground "light blue" :underline t))
2861                                  (((class color)
2862                                    (background light))
2863                                   (:foreground "blue" :underline t))
2864                                  (t (:underline t)))
2865   "Face used for low level group tags."
2866   :group 'custom-faces)
2867
2868 (define-widget 'custom-group 'custom
2869   "Customize group."
2870   :format "%v"
2871   :sample-face-get 'custom-group-sample-face-get
2872   :documentation-property 'group-documentation
2873   :help-echo "Set or reset all members of this group"
2874   :value-create 'custom-group-value-create
2875   :action 'custom-group-action
2876   :custom-category 'group
2877   :custom-set 'custom-group-set
2878   :custom-save 'custom-group-save
2879   :custom-reset-current 'custom-group-reset-current
2880   :custom-reset-saved 'custom-group-reset-saved
2881   :custom-reset-standard 'custom-group-reset-standard
2882   :custom-menu 'custom-group-menu-create)
2883
2884 (defun custom-group-sample-face-get (widget)
2885   ;; Use :sample-face.
2886   (or (nth (1- (widget-get widget :custom-level)) custom-group-tag-faces)
2887       'custom-group-tag-face))
2888
2889 (define-widget 'custom-group-visibility 'visibility
2890   "An indicator and manipulator for hidden group contents."
2891   :create 'custom-group-visibility-create)
2892
2893 (defun custom-group-visibility-create (widget)
2894   (let ((visible (widget-value widget)))
2895     (if visible
2896         (insert "--------")))
2897   (widget-default-create widget))
2898
2899 (defun custom-group-members (symbol groups-only)
2900   "Return SYMBOL's custom group members.
2901 If GROUPS-ONLY non-nil, return only those members that are groups."
2902   (if (not groups-only)
2903       (get symbol 'custom-group)
2904     (let (members)
2905       (dolist (entry (get symbol 'custom-group) (nreverse members))
2906         (when (eq (nth 1 entry) 'custom-group)
2907           (push entry members))))))
2908
2909 (defun custom-group-value-create (widget)
2910   "Insert a customize group for WIDGET in the current buffer."
2911   (let* ((state (widget-get widget :custom-state))
2912          (level (widget-get widget :custom-level))
2913          ;; (indent (widget-get widget :indent))
2914          (prefix (widget-get widget :custom-prefix))
2915          (buttons (widget-get widget :buttons))
2916          (tag (widget-get widget :tag))
2917          (symbol (widget-value widget))
2918          (members (custom-group-members symbol
2919                                         (and (eq custom-buffer-style 'tree)
2920                                              custom-browse-only-groups))))
2921     (cond ((and (eq custom-buffer-style 'tree)
2922                 (eq state 'hidden)
2923                 (or members (custom-unloaded-widget-p widget)))
2924            (custom-browse-insert-prefix prefix)
2925            (push (widget-create-child-and-convert
2926                   widget 'custom-browse-visibility
2927                   ;; :tag-glyph "plus"
2928                   :tag "+")
2929                  buttons)
2930            (insert "-- ")
2931            ;; (widget-glyph-insert nil "-- " "horizontal")
2932            (push (widget-create-child-and-convert
2933                   widget 'custom-browse-group-tag)
2934                  buttons)
2935            (insert " " tag "\n")
2936            (widget-put widget :buttons buttons))
2937           ((and (eq custom-buffer-style 'tree)
2938                 (zerop (length members)))
2939            (custom-browse-insert-prefix prefix)
2940            (insert "[ ]-- ")
2941            ;; (widget-glyph-insert nil "[ ]" "empty")
2942            ;; (widget-glyph-insert nil "-- " "horizontal")
2943            (push (widget-create-child-and-convert
2944                   widget 'custom-browse-group-tag)
2945                  buttons)
2946            (insert " " tag "\n")
2947            (widget-put widget :buttons buttons))
2948           ((eq custom-buffer-style 'tree)
2949            (custom-browse-insert-prefix prefix)
2950            (custom-load-widget widget)
2951            (if (zerop (length members))
2952                (progn
2953                  (custom-browse-insert-prefix prefix)
2954                  (insert "[ ]-- ")
2955                  ;; (widget-glyph-insert nil "[ ]" "empty")
2956                  ;; (widget-glyph-insert nil "-- " "horizontal")
2957                  (push (widget-create-child-and-convert
2958                         widget 'custom-browse-group-tag)
2959                        buttons)
2960                  (insert " " tag "\n")
2961                  (widget-put widget :buttons buttons))
2962              (push (widget-create-child-and-convert
2963                     widget 'custom-browse-visibility
2964                     ;; :tag-glyph "minus"
2965                     :tag "-")
2966                    buttons)
2967              (insert "-\\ ")
2968              ;; (widget-glyph-insert nil "-\\ " "top")
2969              (push (widget-create-child-and-convert
2970                     widget 'custom-browse-group-tag)
2971                    buttons)
2972              (insert " " tag "\n")
2973              (widget-put widget :buttons buttons)
2974              (message "Creating group...")
2975              (let* ((members (custom-sort-items members
2976                               custom-browse-sort-alphabetically
2977                               custom-browse-order-groups))
2978                     (prefixes (widget-get widget :custom-prefixes))
2979                     (custom-prefix-list (custom-prefix-add symbol prefixes))
2980                     (extra-prefix (if (widget-get widget :custom-last)
2981                                       "   "
2982                                     " | "))
2983                     (prefix (concat prefix extra-prefix))
2984                     children entry)
2985                (while members
2986                  (setq entry (car members)
2987                        members (cdr members))
2988                  (push (widget-create-child-and-convert
2989                         widget (nth 1 entry)
2990                         :group widget
2991                         :tag (custom-unlispify-tag-name (nth 0 entry))
2992                         :custom-prefixes custom-prefix-list
2993                         :custom-level (1+ level)
2994                         :custom-last (null members)
2995                         :value (nth 0 entry)
2996                         :custom-prefix prefix)
2997                        children))
2998                (widget-put widget :children (reverse children)))
2999              (message "Creating group...done")))
3000           ;; Nested style.
3001           ((eq state 'hidden)
3002            ;; Create level indicator.
3003            (unless (eq custom-buffer-style 'links)
3004              (insert-char ?\  (* custom-buffer-indent (1- level)))
3005              (insert "-- "))
3006            ;; Create link indicator.
3007            (when (eq custom-buffer-style 'links)
3008              (insert " ")
3009              (push (widget-create-child-and-convert
3010                     widget 'custom-group-link
3011                     :tag "Open"
3012                     :tag-glyph '("open-up" "open-down")
3013                     symbol)
3014                    buttons)
3015              (insert " "))
3016            ;; Create tag.
3017            (let ((begin (point)))
3018              (insert tag)
3019              (widget-specify-sample widget begin (point)))
3020            (insert " group")
3021            ;; Create visibility indicator.
3022            (unless (eq custom-buffer-style 'links)
3023              (insert ": ")
3024              (push (widget-create-child-and-convert
3025                     widget 'custom-group-visibility
3026                     :help-echo "Show members of this group"
3027                     :action 'custom-toggle-parent
3028                     (not (eq state 'hidden)))
3029                    buttons))
3030            (insert " \n")
3031            ;; Create magic button.
3032            (let ((magic (widget-create-child-and-convert
3033                          widget 'custom-magic nil)))
3034              (widget-put widget :custom-magic magic)
3035              (push magic buttons))
3036            ;; Update buttons.
3037            (widget-put widget :buttons buttons)
3038            ;; Insert documentation.
3039            (if (and (eq custom-buffer-style 'links) (> level 1))
3040                (widget-put widget :documentation-indent 0))
3041            (widget-default-format-handler widget ?h))
3042           ;; Nested style.
3043           (t                            ;Visible.
3044            (custom-load-widget widget)
3045            ;; Update members
3046            (setq members (custom-group-members
3047                           symbol (and (eq custom-buffer-style 'tree)
3048                                       custom-browse-only-groups)))
3049            ;; Add parent groups references above the group.
3050            (if t    ;;; This should test that the buffer
3051                     ;;; was made to display a group.
3052                (when (eq level 1)
3053                  (if (custom-add-parent-links widget
3054                                               "Go to parent group:")
3055                      (insert "\n"))))
3056            ;; Create level indicator.
3057            (insert-char ?\  (* custom-buffer-indent (1- level)))
3058            (insert "/- ")
3059            ;; Create tag.
3060            (let ((start (point)))
3061              (insert tag)
3062              (widget-specify-sample widget start (point)))
3063            (insert " group: ")
3064            ;; Create visibility indicator.
3065            (unless (eq custom-buffer-style 'links)
3066              (insert "--------")
3067              (push (widget-create-child-and-convert
3068                     widget 'visibility
3069                     :help-echo "Hide members of this group"
3070                     :action 'custom-toggle-parent
3071                     (not (eq state 'hidden)))
3072                    buttons)
3073              (insert " "))
3074            ;; Create more dashes.
3075            ;; Use 76 instead of 75 to compensate for the temporary "<"
3076            ;; added by `widget-insert'.
3077            (insert-char ?- (- 76 (current-column)
3078                               (* custom-buffer-indent level)))
3079            (insert "\\\n")
3080            ;; Create magic button.
3081            (let ((magic (widget-create-child-and-convert
3082                          widget 'custom-magic
3083                          :indent 0
3084                          nil)))
3085              (widget-put widget :custom-magic magic)
3086              (push magic buttons))
3087            ;; Update buttons.
3088            (widget-put widget :buttons buttons)
3089            ;; Insert documentation.
3090            (widget-default-format-handler widget ?h)
3091            ;; Parent groups.
3092            (if nil  ;;; This should test that the buffer
3093                     ;;; was not made to display a group.
3094                (when (eq level 1)
3095                  (insert-char ?\  custom-buffer-indent)
3096                  (custom-add-parent-links widget)))
3097            (custom-add-see-also widget
3098                                 (make-string (* custom-buffer-indent level)
3099                                              ?\ ))
3100            ;; Members.
3101            (message "Creating group...")
3102            (let* ((members (custom-sort-items members
3103                                               custom-buffer-sort-alphabetically
3104                                               custom-buffer-order-groups))
3105                   (prefixes (widget-get widget :custom-prefixes))
3106                   (custom-prefix-list (custom-prefix-add symbol prefixes))
3107                   (length (length members))
3108                   (count 0)
3109                   (children (mapcar
3110                              (lambda (entry)
3111                                (widget-insert "\n")
3112                                (when (zerop (% count custom-skip-messages))
3113                                  (display-message
3114                                   'progress
3115                                   (format "\
3116 Creating group members... %2d%%"
3117                                           (/ (* 100.0 count) length))))
3118                                (incf count)
3119                                (prog1
3120                                    (widget-create-child-and-convert
3121                                     widget (nth 1 entry)
3122                                     :group widget
3123                                     :tag (custom-unlispify-tag-name
3124                                           (nth 0 entry))
3125                                     :custom-prefixes custom-prefix-list
3126                                     :custom-level (1+ level)
3127                                     :value (nth 0 entry))
3128                                  (unless (eq (preceding-char) ?\n)
3129                                    (widget-insert "\n"))))
3130                              members)))
3131              (message "Creating group magic...")
3132              (mapc 'custom-magic-reset children)
3133              (message "Creating group state...")
3134              (widget-put widget :children children)
3135              (custom-group-state-update widget)
3136              (message "Creating group... done"))
3137            ;; End line
3138            (insert "\n")
3139            (insert-char ?\  (* custom-buffer-indent (1- level)))
3140            (insert "\\- " (widget-get widget :tag) " group end ")
3141            (insert-char ?- (- 75 (current-column) (* custom-buffer-indent level)))
3142            (insert "/\n")))))
3143
3144 (defvar custom-group-menu
3145   '(("Set for Current Session" custom-group-set
3146      (lambda (widget)
3147        (eq (widget-get widget :custom-state) 'modified)))
3148     ("Save for Future Sessions" custom-group-save
3149      (lambda (widget)
3150        (memq (widget-get widget :custom-state) '(modified set))))
3151     ("Reset to Current" custom-group-reset-current
3152      (lambda (widget)
3153        (memq (widget-get widget :custom-state) '(modified))))
3154     ("Reset to Saved" custom-group-reset-saved
3155      (lambda (widget)
3156        (memq (widget-get widget :custom-state) '(modified set))))
3157     ("Reset to standard setting" custom-group-reset-standard
3158      (lambda (widget)
3159        (memq (widget-get widget :custom-state) '(modified set saved)))))
3160   "Alist of actions for the `custom-group' widget.
3161 Each entry has the form (NAME ACTION FILTER) where NAME is the name of
3162 the menu entry, ACTION is the function to call on the widget when the
3163 menu is selected, and FILTER is a predicate which takes a `custom-group'
3164 widget as an argument, and returns non-nil if ACTION is valid on that
3165 widget. If FILTER is nil, ACTION is always valid.")
3166
3167 (defun custom-group-action (widget &optional event)
3168   "Show the menu for `custom-group' WIDGET.
3169 Optional EVENT is the location for the menu."
3170   (if (eq (widget-get widget :custom-state) 'hidden)
3171       (custom-toggle-hide widget)
3172     (let* ((completion-ignore-case t)
3173            (answer (widget-choose (concat "Operation on "
3174                                           (custom-unlispify-tag-name
3175                                            (widget-get widget :value)))
3176                                   (custom-menu-filter custom-group-menu
3177                                                       widget)
3178                                   event)))
3179       (if answer
3180           (funcall answer widget)))))
3181
3182 (defun custom-group-set (widget)
3183   "Set changes in all modified group members."
3184   (let ((children (widget-get widget :children)))
3185     (mapc (lambda (child)
3186             (when (eq (widget-get child :custom-state) 'modified)
3187               (widget-apply child :custom-set)))
3188           children)))
3189
3190 (defun custom-group-save (widget)
3191   "Save all modified group members."
3192   (let ((children (widget-get widget :children)))
3193     (mapc (lambda (child)
3194             (when (memq (widget-get child :custom-state) '(modified set))
3195               (widget-apply child :custom-save)))
3196           children)))
3197
3198 (defun custom-group-reset-current (widget)
3199   "Reset all modified group members."
3200   (let ((children (widget-get widget :children)))
3201     (mapc (lambda (child)
3202             (when (eq (widget-get child :custom-state) 'modified)
3203               (widget-apply child :custom-reset-current)))
3204           children)))
3205
3206 (defun custom-group-reset-saved (widget)
3207   "Reset all modified or set group members."
3208   (let ((children (widget-get widget :children)))
3209     (mapc (lambda (child)
3210             (when (memq (widget-get child :custom-state) '(modified set))
3211               (widget-apply child :custom-reset-saved)))
3212           children)))
3213
3214 (defun custom-group-reset-standard (widget)
3215   "Reset all modified, set, or saved group members."
3216   (let ((children (widget-get widget :children)))
3217     (mapc (lambda (child)
3218             (when (memq (widget-get child :custom-state)
3219                         '(modified set saved))
3220               (widget-apply child :custom-reset-standard)))
3221           children)))
3222
3223 (defun custom-group-state-update (widget)
3224   "Update magic."
3225   (unless (eq (widget-get widget :custom-state) 'hidden)
3226     (let* ((children (widget-get widget :children))
3227            (states (mapcar (lambda (child)
3228                              (widget-get child :custom-state))
3229                            children))
3230            (magics custom-magic-alist)
3231            (found 'standard))
3232       (while magics
3233         (let ((magic (car (car magics))))
3234           (if (and (not (eq magic 'hidden))
3235                    (memq magic states))
3236               (setq found magic
3237                     magics nil)
3238             (setq magics (cdr magics)))))
3239       (widget-put widget :custom-state found)))
3240   (custom-magic-reset widget))
3241
3242 (defun custom-save-delete (symbol)
3243   "Delete the call to SYMBOL form in `custom-file'.
3244 Leave point at the location of the call, or after the last expression."
3245   (let ((find-file-hooks nil)
3246         (auto-mode-alist nil))
3247     (set-buffer (find-file-noselect custom-file)))
3248   (goto-char (point-min))
3249   (catch 'found
3250     (while t
3251       (let ((sexp (condition-case nil
3252                       (read (current-buffer))
3253                     (end-of-file (throw 'found nil)))))
3254         (when (and (listp sexp)
3255                    (eq (car sexp) symbol))
3256           (delete-region (save-excursion
3257                            (backward-sexp)
3258                            (point))
3259                          (point))
3260           (throw 'found nil))))))
3261
3262 (defun custom-save-variables ()
3263    "Save all customized variables in `custom-file'."
3264    (save-excursion
3265      (custom-save-delete 'custom-load-themes)
3266      (custom-save-delete 'custom-reset-variables)
3267      (custom-save-delete 'custom-set-variables)
3268      (custom-save-loaded-themes)
3269      (custom-save-resets 'theme-value 'custom-reset-variables nil)
3270      (let ((standard-output (current-buffer)))
3271        (unless (bolp)
3272         (princ "\n"))
3273        (princ "(custom-set-variables")
3274        (mapatoms (lambda (symbol)
3275                   (let ((spec (car-safe (get symbol 'theme-value)))
3276                         (requests (get symbol 'custom-requests))
3277                         (now (not (or (get symbol 'standard-value)
3278                                       (and (not (boundp symbol))
3279                                            (not (eq (get symbol 'force-value)
3280                                                     'rogue))))))
3281                         (comment (get symbol 'saved-variable-comment)))
3282                     (when (or (and spec (eq (car spec) 'user)
3283                                (eq (second spec) 'set)) comment)
3284                       (princ "\n '(")
3285                       (prin1 symbol)
3286                       (princ " ")
3287                       ;; This comment stuff is in the way ####
3288                       ;; Is (eq (third spec) (car saved-value)) ????
3289                       ;; (prin1 (third spec))
3290                       (prin1 (car (get symbol 'saved-value)))
3291                       (when (or now requests comment)
3292                         (princ (if now " t" " nil")))
3293                       (when (or comment requests)
3294                         (princ " ")
3295                         (prin1 requests))
3296                       (when comment
3297                         (princ " ")
3298                         (prin1 comment))
3299                       (princ ")")))))
3300       (princ ")")
3301       (unless (looking-at "\n")
3302         (princ "\n")))))
3303
3304 (defvar custom-save-face-ignoring nil)
3305
3306 (defun custom-save-face-internal (symbol)
3307   (let ((theme-spec (car-safe (get symbol 'theme-face)))
3308         (comment (get symbol 'saved-face-comment))
3309         (now (not (or (get symbol 'face-defface-spec)
3310               (and (not (find-face symbol))
3311                    (not (eq (get symbol 'force-face) 'rogue)))))))
3312     (when (or (and (not (memq symbol custom-save-face-ignoring))
3313                ;; Don't print default face here.
3314                theme-spec
3315                (eq (car theme-spec) 'user)
3316                (eq (second theme-spec) 'set)) comment)
3317       (princ "\n '(")
3318       (prin1 symbol)
3319       (princ " ")
3320       (prin1 (get symbol 'saved-face))
3321       (if (or comment now)
3322           (princ (if now " t" " nil")))
3323       (when comment
3324           (princ " ")
3325           (prin1 comment))
3326       (princ ")"))))
3327
3328 (defun custom-save-faces ()
3329   "Save all customized faces in `custom-file'."
3330   (save-excursion
3331     (custom-save-delete 'custom-reset-faces)
3332     (custom-save-delete 'custom-set-faces)
3333     (custom-save-resets 'theme-face 'custom-reset-faces '(default))
3334     (let ((standard-output (current-buffer)))
3335       (unless (bolp)
3336         (princ "\n"))
3337       (princ "(custom-set-faces")
3338         ;; The default face must be first, since it affects the others.
3339       (custom-save-face-internal 'default)
3340       (let ((custom-save-face-ignoring '(default)))
3341         (mapatoms #'custom-save-face-internal))
3342       (princ ")")
3343       (unless (looking-at "\n")
3344         (princ "\n")))))
3345
3346 (defun custom-save-resets (property setter special)
3347   (let (started-writing ignored-special)
3348     (setq ignored-special ignored-special) ;; suppress byte-compiler warning
3349     ;; (custom-save-delete setter) Done by caller
3350     (let ((standard-output (current-buffer))
3351           (mapper `(lambda (object)
3352                     (let ((spec (car-safe (get object (quote ,property)))))
3353                       (when (and (not (memq object ignored-special))
3354                                  (eq (car spec) 'user)
3355                                  (eq (second spec) 'reset))
3356                         ;; Do not write reset statements unless necessary.
3357                         (unless started-writing
3358                           (setq started-writing t)
3359                           (unless (bolp)
3360                             (princ "\n"))
3361                         (princ "(")
3362                         (princ (quote ,setter))
3363                         (princ "\n '(")
3364                         (prin1 object)
3365                         (princ " ")
3366                         (prin1 (third spec))
3367                         (princ ")")))))))
3368       (mapc mapper special)
3369       (setq ignored-special special)
3370       (mapatoms mapper)
3371       (when started-writing
3372         (princ ")\n"))))
3373     )
3374
3375
3376 (defun custom-save-loaded-themes ()
3377   (let ((themes (reverse (get 'user 'theme-loads-themes)))
3378         (standard-output (current-buffer)))
3379     (when themes
3380       (unless (bolp) (princ "\n"))
3381       (princ "(custom-load-themes")
3382       (mapc (lambda (theme)
3383               (princ "\n   '")
3384               (prin1 theme)) themes)
3385       (princ " )\n"))))
3386
3387 ;;;###autoload
3388 (defun customize-save-customized ()
3389   "Save all user options which have been set in this session."
3390   (interactive)
3391   (mapatoms (lambda (symbol)
3392               (let ((face (get symbol 'customized-face))
3393                     (value (get symbol 'customized-value))
3394                     (face-comment (get symbol 'customized-face-comment))
3395                     (variable-comment
3396                      (get symbol 'customized-variable-comment)))
3397                 (when face
3398                   (put symbol 'saved-face face)
3399                   (custom-push-theme 'theme-face symbol 'user 'set value)
3400                   (put symbol 'customized-face nil))
3401                 (when value
3402                   (put symbol 'saved-value value)
3403                   (custom-push-theme 'theme-value symbol 'user 'set value)
3404                   (put symbol 'customized-value nil))
3405                 (when variable-comment
3406                   (put symbol 'saved-variable-comment variable-comment)
3407                   (put symbol 'customized-variable-comment nil))
3408                 (when face-comment
3409                   (put symbol 'saved-face-comment face-comment)
3410                   (put symbol 'customized-face-comment nil)))))
3411   ;; We really should update all custom buffers here.
3412   (custom-save-all))
3413
3414 ;;;###autoload
3415 (defun custom-save-all ()
3416   "Save all customizations in `custom-file'."
3417   (let ((inhibit-read-only t))
3418     (custom-save-variables)
3419     (custom-save-faces)
3420     (let ((find-file-hooks nil)
3421           (auto-mode-alist))
3422       (with-current-buffer (find-file-noselect custom-file)
3423         (save-buffer)))))
3424
3425 \f
3426 ;;; The Customize Menu.
3427
3428 ;;; Menu support
3429
3430 (defun custom-face-menu-create (widget symbol)
3431   "Ignoring WIDGET, create a menu entry for customization face SYMBOL."
3432   (vector (custom-unlispify-menu-entry symbol)
3433           `(customize-face ',symbol)
3434           t))
3435
3436 (defun custom-variable-menu-create (widget symbol)
3437   "Ignoring WIDGET, create a menu entry for customization variable SYMBOL."
3438   (let ((type (get symbol 'custom-type)))
3439     (unless (listp type)
3440       (setq type (list type)))
3441     (if (and type (widget-get type :custom-menu))
3442         (widget-apply type :custom-menu symbol)
3443       (vector (custom-unlispify-menu-entry symbol)
3444               `(customize-variable ',symbol)
3445               t))))
3446
3447 ;; Add checkboxes to boolean variable entries.
3448 (widget-put (get 'boolean 'widget-type)
3449             :custom-menu (lambda (widget symbol)
3450                            `[,(custom-unlispify-menu-entry symbol)
3451                              (customize-variable ',symbol)
3452                              :style toggle
3453                              :selected ,symbol]))
3454
3455 ;; XEmacs can create menus dynamically.
3456 (defun custom-group-menu-create (widget symbol)
3457   "Ignoring WIDGET, create a menu entry for customization group SYMBOL."
3458   `( ,(custom-unlispify-menu-entry symbol t)
3459      :filter (lambda (&rest junk)
3460                (let ((item (custom-menu-create ',symbol)))
3461                  (if (listp item)
3462                      (cdr item)
3463                    (list item))))))
3464
3465 ;;;###autoload
3466 (defun custom-menu-create (symbol)
3467   "Create menu for customization group SYMBOL.
3468 The menu is in a format applicable to `easy-menu-define'."
3469   (let* ((item (vector (custom-unlispify-menu-entry symbol)
3470                        `(customize-group ',symbol)
3471                        t)))
3472     ;; Item is the entry for creating a menu buffer for SYMBOL.
3473     ;; We may nest, if the menu is not too big.
3474     (custom-load-symbol symbol)
3475     (if (< (length (get symbol 'custom-group)) widget-menu-max-size)
3476         ;; The menu is not too big.
3477         (let ((custom-prefix-list (custom-prefix-add symbol
3478                                                      custom-prefix-list))
3479               (members (custom-sort-items (get symbol 'custom-group)
3480                                           custom-menu-sort-alphabetically
3481                                           custom-menu-order-groups)))
3482           ;; Create the menu.
3483           `(,(custom-unlispify-menu-entry symbol t)
3484             ,item
3485             "--"
3486             ,@(mapcar (lambda (entry)
3487                         (widget-apply (if (listp (nth 1 entry))
3488                                           (nth 1 entry)
3489                                         (list (nth 1 entry)))
3490                                       :custom-menu (nth 0 entry)))
3491                       members)))
3492       ;; The menu was too big.
3493       item)))
3494
3495 ;;;###autoload
3496 (defun customize-menu-create (symbol &optional name)
3497   "Return a customize menu for customization group SYMBOL.
3498 If optional NAME is given, use that as the name of the menu.
3499 Otherwise the menu will be named `Customize'.
3500 The format is suitable for use with `easy-menu-define'."
3501   (unless name
3502     (setq name "Customize"))
3503   `(,name
3504     :filter (lambda (&rest junk)
3505               (cdr (custom-menu-create ',symbol)))))
3506
3507 ;;; The Custom Mode.
3508
3509 (defvar custom-mode-map nil
3510   "Keymap for `custom-mode'.")
3511
3512 (unless custom-mode-map
3513   (setq custom-mode-map (make-sparse-keymap))
3514   (set-keymap-parents custom-mode-map widget-keymap)
3515   (suppress-keymap custom-mode-map)
3516   (define-key custom-mode-map " " 'scroll-up)
3517   (define-key custom-mode-map [delete] 'scroll-down)
3518   (define-key custom-mode-map "q" 'Custom-buffer-done)
3519   (define-key custom-mode-map "u" 'Custom-goto-parent)
3520   (define-key custom-mode-map "n" 'widget-forward)
3521   (define-key custom-mode-map "p" 'widget-backward))
3522
3523 (easy-menu-define Custom-mode-menu
3524     custom-mode-map
3525   "Menu used in customization buffers."
3526   `("Custom"
3527     ,(customize-menu-create 'customize)
3528     ["Set" Custom-set t]
3529     ["Save" Custom-save t]
3530     ["Reset to Current" Custom-reset-current t]
3531     ["Reset to Saved" Custom-reset-saved t]
3532     ["Reset to Standard Settings" Custom-reset-standard t]
3533     ["Info" (Info-goto-node "(xemacs)Easy Customization") t]))
3534
3535 (defun Custom-goto-parent ()
3536   "Go to the parent group listed at the top of this buffer.
3537 If several parents are listed, go to the first of them."
3538   (interactive)
3539   (save-excursion
3540     (goto-char (point-min))
3541     (if (search-forward "\nGo to parent group: " nil t)
3542         (let* ((button (get-char-property (point) 'button))
3543                (parent (downcase (widget-get  button :tag))))
3544           (customize-group parent)))))
3545
3546 (defcustom custom-mode-hook nil
3547   "Hook called when entering custom-mode."
3548   :type 'hook
3549   :group 'custom-buffer )
3550
3551 (defun custom-state-buffer-message (widget)
3552   (if (eq (widget-get (widget-get widget :parent) :custom-state) 'modified)
3553       (message
3554        "To install your edits, invoke [State] and choose the Set operation")))
3555
3556 (defun custom-mode ()
3557   "Major mode for editing customization buffers.
3558
3559 The following commands are available:
3560
3561 Move to next button or editable field.     \\[widget-forward]
3562 Move to previous button or editable field. \\[widget-backward]
3563 \\<widget-field-keymap>\
3564 Complete content of editable text field.   \\[widget-complete]
3565 \\<custom-mode-map>\
3566 Invoke button under point.                 \\[widget-button-press]
3567 Set all modifications.                     \\[Custom-set]
3568 Make all modifications default.            \\[Custom-save]
3569 Reset all modified options.                \\[Custom-reset-current]
3570 Reset all modified or set options.         \\[Custom-reset-saved]
3571 Reset all options.                         \\[Custom-reset-standard]
3572
3573 Entry to this mode calls the value of `custom-mode-hook'
3574 if that value is non-nil."
3575   (kill-all-local-variables)
3576   (setq major-mode 'custom-mode
3577         mode-name "Custom")
3578   (use-local-map custom-mode-map)
3579   (easy-menu-add Custom-mode-menu)
3580   (make-local-variable 'custom-options)
3581   (make-local-variable 'widget-documentation-face)
3582   (setq widget-documentation-face 'custom-documentation-face)
3583   (make-local-variable 'widget-button-face)
3584   (setq widget-button-face 'custom-button-face)
3585   (make-local-hook 'widget-edit-functions)
3586   (add-hook 'widget-edit-functions 'custom-state-buffer-message nil t)
3587   (run-hooks 'custom-mode-hook))
3588
3589 \f
3590 ;;;###autoload
3591 (defun custom-migrate-custom-file (new-custom-file-name)
3592   "Migrate custom file from home directory."
3593   (mapc 'custom-save-delete
3594         '(custom-load-themes custom-reset-variables
3595                              custom-set-variables
3596                              custom-set-faces
3597                              custom-reset-faces))
3598   (with-current-buffer (find-file-noselect custom-file)
3599     (save-buffer))
3600   (setq custom-file new-custom-file-name)
3601   (custom-save-all))
3602 \f
3603 ;;; The End.
3604
3605 (provide 'cus-edit)
3606
3607 ;; cus-edit.el ends here