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