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