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