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