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