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