NEWS.
[bpt/emacs.git] / lisp / cus-edit.el
CommitLineData
e8af40ee 1;;; cus-edit.el --- tools for customizing Emacs and Lisp packages
d543e20b 2;;
73b0cd50 3;; Copyright (C) 1996-1997, 1999-2011 Free Software Foundation, Inc.
d543e20b
PA
4;;
5;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
c942535f 6;; Maintainer: FSF
d543e20b 7;; Keywords: help, faces
bd78fa1d 8;; Package: emacs
d543e20b 9
f2b98a56
RS
10;; This file is part of GNU Emacs.
11
eb3fa2cf 12;; GNU Emacs is free software: you can redistribute it and/or modify
f2b98a56 13;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
f2b98a56
RS
16
17;; GNU Emacs is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
eb3fa2cf 23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
f2b98a56 24
d543e20b
PA
25;;; Commentary:
26;;
6d528fc5 27;; This file implements the code to create and edit customize buffers.
c942535f 28;;
d543e20b
PA
29;; See `custom.el'.
30
ab678382
RS
31;; No commands should have names starting with `custom-' because
32;; that interferes with completion. Use `customize-' for commands
33;; that the user will run with M-x, and `Custom-' for interactive commands.
34
9f5d3236 35;; The identity of a customize option is represented by a Lisp symbol.
62bf2fd1 36;; The following values are associated with an option.
9f5d3236
RS
37
38;; 0. The current value.
39
40;; This is the value of the option as seen by "the rest of Emacs".
41
42;; Usually extracted by 'default-value', but can be extracted with
43;; different means if the option symbol has the 'custom-get'
44;; property. Similarly, set-default (or the 'custom-set' property)
45;; can set it.
46
47;; 1. The widget value.
48
62bf2fd1 49;; This is the value shown in the widget in a customize buffer.
9f5d3236
RS
50
51;; 2. The customized value.
52
53;; This is the last value given to the option through customize.
54
55;; It is stored in the 'customized-value' property of the option, in a
62bf2fd1 56;; cons-cell whose car evaluates to the customized value.
9f5d3236
RS
57
58;; 3. The saved value.
59
60;; This is last value saved from customize.
61
62;; It is stored in the 'saved-value' property of the option, in a
62bf2fd1 63;; cons-cell whose car evaluates to the saved value.
9f5d3236
RS
64
65;; 4. The standard value.
66
67;; This is the value given in the 'defcustom' declaration.
68
69;; It is stored in the 'standard-value' property of the option, in a
62bf2fd1 70;; cons-cell whose car evaluates to the standard value.
9f5d3236
RS
71
72;; 5. The "think" value.
62bf2fd1
LT
73
74;; This is what customize thinks the current value should be.
75
76;; This is the customized value, if any such value exists, otherwise
9f5d3236 77;; the saved value, if that exists, and as a last resort the standard
62bf2fd1 78;; value.
9f5d3236
RS
79
80;; The reason for storing values unevaluated: This is so you can have
81;; values that depend on the environment. For example, you can have a
62bf2fd1 82;; variable that has one value when Emacs is running under a window
9f5d3236 83;; system, and another value on a tty. Since the evaluation is only done
62bf2fd1 84;; when the variable is first initialized, this is only relevant for the
9f5d3236
RS
85;; saved (and standard) values, but affect others values for
86;; compatibility.
87
88;; You can see (and modify and save) this unevaluated value by selecting
70482877 89;; "Show Saved Lisp Expression" from the Lisp interface. This will
9f5d3236
RS
90;; give you the unevaluated saved value, if any, otherwise the
91;; unevaluated standard value.
92
93;; The possible states for a customize widget are:
94
95;; 0. unknown
96
97;; The state has not been determined yet.
98
99;; 1. modified
100
101;; The widget value is different from the current value.
102
103;; 2. changed
62bf2fd1
LT
104
105;; The current value is different from the "think" value.
9f5d3236
RS
106
107;; 3. set
108
109;; The "think" value is the customized value.
110
111;; 4. saved
112
113;; The "think" value is the saved value.
114
115;; 5. standard
116
117;; The "think" value is the standard value.
118
119;; 6. rogue
120
62bf2fd1 121;; There is no standard value. This means that the variable was
f63d5a0e
LT
122;; not defined with defcustom, nor handled in cus-start.el. Most
123;; standard interactive Custom commands do not let you create a
124;; Custom buffer containing such variables. However, such Custom
125;; buffers can be created, for instance, by calling
126;; `customize-apropos' with a prefix arg or by calling
f32a1cb5 127;; `customize-option' non-interactively.
9f5d3236
RS
128
129;; 7. hidden
130
131;; There is no widget value.
132
133;; 8. mismatch
134
135;; The widget value is not valid member of the :type specified for the
62bf2fd1 136;; option.
9f5d3236 137
d543e20b
PA
138;;; Code:
139
140(require 'cus-face)
141(require 'wid-edit)
a0247dcf
SM
142
143(defvar custom-versions-load-alist) ; from cus-load
144(defvar recentf-exclude) ; from recentf.el
6d528fc5
PA
145
146(condition-case nil
147 (require 'cus-load)
148 (error nil))
d543e20b 149
9097aeb7
PA
150(condition-case nil
151 (require 'cus-start)
152 (error nil))
153
bd042c03 154(put 'custom-define-hook 'custom-type 'hook)
25ac13b5 155(put 'custom-define-hook 'standard-value '(nil))
bd042c03
PA
156(custom-add-to-group 'customize 'custom-define-hook 'custom-variable)
157
d543e20b
PA
158;;; Customization Groups.
159
160(defgroup emacs nil
161 "Customization of the One True Editor."
162 :link '(custom-manual "(emacs)Top"))
163
164;; Most of these groups are stolen from `finder.el',
165(defgroup editing nil
166 "Basic text editing facilities."
167 :group 'emacs)
168
647f9993
CY
169(defgroup convenience nil
170 "Convenience features for faster editing."
171 :group 'emacs)
172
173(defgroup files nil
174 "Support for editing files."
175 :group 'emacs)
176
177(defgroup wp nil
178 "Support for editing text files."
179 :tag "Text"
180 :group 'emacs)
181
182(defgroup data nil
183 "Support for editing binary data files."
184 :group 'emacs)
185
d543e20b
PA
186(defgroup abbrev nil
187 "Abbreviation handling, typing shortcuts, macros."
188 :tag "Abbreviations"
eba5b4dd 189 :group 'convenience)
d543e20b
PA
190
191(defgroup matching nil
192 "Various sorts of searching and matching."
193 :group 'editing)
194
195(defgroup emulations nil
196 "Emulations of other editors."
6ea2832e 197 :link '(custom-manual "(emacs)Emulation")
d543e20b
PA
198 :group 'editing)
199
200(defgroup mouse nil
201 "Mouse support."
202 :group 'editing)
203
204(defgroup outlines nil
205 "Support for hierarchical outlining."
eba5b4dd 206 :group 'wp)
d543e20b
PA
207
208(defgroup external nil
209 "Interfacing to external utilities."
210 :group 'emacs)
211
26f4b8ab
CY
212(defgroup comm nil
213 "Communications, networking, and remote access to files."
214 :tag "Communication"
eba5b4dd
CY
215 :group 'emacs)
216
d543e20b
PA
217(defgroup processes nil
218 "Process, subshell, compilation, and job control support."
eba5b4dd 219 :group 'external)
d543e20b
PA
220
221(defgroup programming nil
222 "Support for programming in other languages."
223 :group 'emacs)
224
225(defgroup languages nil
226 "Specialized modes for editing programming languages."
227 :group 'programming)
228
229(defgroup lisp nil
230 "Lisp support, including Emacs Lisp."
3e721a14 231 :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces)
d543e20b
PA
232 :group 'languages
233 :group 'development)
234
235(defgroup c nil
236 "Support for the C language and related languages."
3e721a14 237 :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces)
6ea2832e 238 :link '(custom-manual "(ccmode)")
d543e20b
PA
239 :group 'languages)
240
241(defgroup tools nil
242 "Programming tools."
243 :group 'programming)
244
d543e20b
PA
245(defgroup applications nil
246 "Applications written in Emacs."
247 :group 'emacs)
248
249(defgroup calendar nil
250 "Calendar and time management support."
251 :group 'applications)
252
253(defgroup mail nil
254 "Modes for electronic-mail handling."
255 :group 'applications)
256
257(defgroup news nil
258 "Support for netnews reading and posting."
6ea2832e 259 :link '(custom-manual "(gnus)")
d543e20b
PA
260 :group 'applications)
261
262(defgroup games nil
263 "Games, jokes and amusements."
264 :group 'applications)
265
266(defgroup development nil
267 "Support for further development of Emacs."
268 :group 'emacs)
269
270(defgroup docs nil
271 "Support for Emacs documentation."
272 :group 'development)
273
274(defgroup extensions nil
275 "Emacs Lisp language extensions."
276 :group 'development)
277
278(defgroup internal nil
279 "Code for Emacs internals, build process, defaults."
280 :group 'development)
281
282(defgroup maint nil
283 "Maintenance aids for the Emacs development group."
284 :tag "Maintenance"
285 :group 'development)
286
287(defgroup environment nil
288 "Fitting Emacs with its environment."
289 :group 'emacs)
290
d543e20b 291(defgroup hardware nil
ff90f4b0 292 "Support for interfacing with miscellaneous hardware."
d543e20b
PA
293 :group 'environment)
294
295(defgroup terminals nil
296 "Support for terminal types."
297 :group 'environment)
298
299(defgroup unix nil
300 "Front-ends/assistants for, or emulators of, UNIX features."
301 :group 'environment)
302
d543e20b
PA
303(defgroup i18n nil
304 "Internationalization and alternate character-set support."
6ea2832e 305 :link '(custom-manual "(emacs)International")
d543e20b
PA
306 :group 'environment
307 :group 'editing)
308
86bd10bc
PA
309(defgroup x nil
310 "The X Window system."
311 :group 'environment)
312
d543e20b
PA
313(defgroup frames nil
314 "Support for Emacs frames and window systems."
315 :group 'environment)
316
d543e20b
PA
317(defgroup tex nil
318 "Code related to the TeX formatter."
3e721a14 319 :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces)
d543e20b
PA
320 :group 'wp)
321
322(defgroup faces nil
323 "Support for multiple fonts."
324 :group 'emacs)
325
d543e20b
PA
326(defgroup help nil
327 "Support for on-line help systems."
328 :group 'emacs)
329
d3d4df42
DL
330(defgroup multimedia nil
331 "Non-textual support, specifically images and sound."
332 :group 'emacs)
333
d543e20b
PA
334(defgroup local nil
335 "Code local to your site."
336 :group 'emacs)
337
338(defgroup customize '((widgets custom-group))
339 "Customization of the Customization support."
d543e20b 340 :prefix "custom-"
bd042c03
PA
341 :group 'help)
342
343(defgroup custom-faces nil
344 "Faces used by customize."
345 :group 'customize
d543e20b
PA
346 :group 'faces)
347
da5ec617
PA
348(defgroup custom-browse nil
349 "Control customize browser."
350 :prefix "custom-"
351 :group 'customize)
352
6aaedd12 353(defgroup custom-buffer nil
da5ec617 354 "Control customize buffers."
6aaedd12
PA
355 :prefix "custom-"
356 :group 'customize)
357
358(defgroup custom-menu nil
da5ec617 359 "Control customize menus."
6aaedd12
PA
360 :prefix "custom-"
361 :group 'customize)
362
bd042c03
PA
363(defgroup alloc nil
364 "Storage allocation and gc for GNU Emacs Lisp interpreter."
365 :tag "Storage Allocation"
366 :group 'internal)
367
368(defgroup undo nil
369 "Undoing changes in buffers."
6ea2832e 370 :link '(custom-manual "(emacs)Undo")
bd042c03
PA
371 :group 'editing)
372
0518917a 373(defgroup mode-line nil
bd042c03
PA
374 "Content of the modeline."
375 :group 'environment)
376
bd042c03
PA
377(defgroup editing-basics nil
378 "Most basic editing facilities."
379 :group 'editing)
380
381(defgroup display nil
382 "How characters are displayed in buffers."
383 :group 'environment)
384
385(defgroup execute nil
386 "Executing external commands."
387 :group 'processes)
388
389(defgroup installation nil
390 "The Emacs installation."
391 :group 'environment)
392
393(defgroup dired nil
394 "Directory editing."
395 :group 'environment)
396
397(defgroup limits nil
398 "Internal Emacs limits."
399 :group 'internal)
400
401(defgroup debug nil
402 "Debugging Emacs itself."
403 :group 'development)
404
bd042c03
PA
405(defgroup keyboard nil
406 "Input from the keyboard."
407 :group 'environment)
408
409(defgroup mouse nil
410 "Input from the mouse."
411 :group 'environment)
412
413(defgroup menu nil
414 "Input from the menus."
415 :group 'environment)
416
153bd017
JD
417(defgroup dnd nil
418 "Handling data from drag and drop."
419 :group 'environment)
420
bd042c03 421(defgroup auto-save nil
040ee34f 422 "Preventing accidental loss of data."
482e54f3 423 :group 'files)
bd042c03
PA
424
425(defgroup processes-basics nil
426 "Basic stuff dealing with processes."
427 :group 'processes)
428
25ac13b5
PA
429(defgroup mule nil
430 "MULE Emacs internationalization."
70bc91bc 431 :group 'i18n)
25ac13b5 432
bd042c03
PA
433(defgroup windows nil
434 "Windows within a frame."
6ea2832e 435 :link '(custom-manual "(emacs)Windows")
86bd10bc 436 :group 'environment)
bd042c03 437
f3093f77
CY
438;;; Custom mode keymaps
439
440(defvar custom-mode-map
f3093f77
CY
441 (let ((map (make-keymap)))
442 (set-keymap-parent map widget-keymap)
443 (define-key map [remap self-insert-command] 'Custom-no-edit)
444 (define-key map "\^m" 'Custom-newline)
445 (define-key map " " 'scroll-up)
446 (define-key map "\177" 'scroll-down)
447 (define-key map "\C-c\C-c" 'Custom-set)
448 (define-key map "\C-x\C-s" 'Custom-save)
449 (define-key map "q" 'Custom-buffer-done)
450 (define-key map "u" 'Custom-goto-parent)
451 (define-key map "n" 'widget-forward)
452 (define-key map "p" 'widget-backward)
453 map)
6aec3b9d 454 "Keymap for `Custom-mode'.")
f3093f77
CY
455
456(defvar custom-mode-link-map
457 (let ((map (make-keymap)))
458 (set-keymap-parent map custom-mode-map)
459 (define-key map [down-mouse-2] nil)
460 (define-key map [down-mouse-1] 'mouse-drag-region)
461 (define-key map [mouse-2] 'widget-move-and-invoke)
462 map)
6aec3b9d 463 "Local keymap for links in `Custom-mode'.")
f3093f77 464
34cf517c
MR
465(defvar custom-field-keymap
466 (let ((map (copy-keymap widget-field-keymap)))
467 (define-key map "\C-c\C-c" 'Custom-set)
468 (define-key map "\C-x\C-s" 'Custom-save)
469 map)
470 "Keymap used inside editable fields in customization buffers.")
471
472(widget-put (get 'editable-field 'widget-type) :keymap custom-field-keymap)
f3093f77 473
d543e20b
PA
474;;; Utilities.
475
d543e20b
PA
476(defun custom-split-regexp-maybe (regexp)
477 "If REGEXP is a string, split it to a list at `\\|'.
12bafdaa 478You can get the original back from the result with:
d543e20b
PA
479 (mapconcat 'identity result \"\\|\")
480
481IF REGEXP is not a string, return it unchanged."
482 (if (stringp regexp)
12bafdaa 483 (split-string regexp "\\\\|")
d543e20b
PA
484 regexp))
485
bd042c03 486(defun custom-variable-prompt ()
3a495e15 487 "Prompt for a custom variable, defaulting to the variable at point.
bd042c03 488Return a list suitable for use in `interactive'."
3e721a14
JL
489 (let* ((v (variable-at-point))
490 (default (and (symbolp v) (custom-variable-p v) (symbol-name v)))
491 (enable-recursive-minibuffers t)
492 val)
493 (setq val (completing-read
0a57ffed
RS
494 (if default (format "Customize variable (default %s): " default)
495 "Customize variable: ")
3e721a14 496 obarray 'custom-variable-p t nil nil default))
bd042c03 497 (list (if (equal val "")
5b5cdd97
RS
498 (if (symbolp v) v nil)
499 (intern val)))))
bd042c03 500
6d528fc5
PA
501(defun custom-menu-filter (menu widget)
502 "Convert MENU to the form used by `widget-choose'.
503MENU should be in the same format as `custom-variable-menu'.
504WIDGET is the widget to apply the filter entries of MENU on."
505 (let ((result nil)
506 current name action filter)
d3d4df42 507 (while menu
6d528fc5
PA
508 (setq current (car menu)
509 name (nth 0 current)
510 action (nth 1 current)
511 filter (nth 2 current)
512 menu (cdr menu))
513 (if (or (null filter) (funcall filter widget))
514 (push (cons name action) result)
515 (push name result)))
516 (nreverse result)))
517
bd042c03
PA
518;;; Unlispify.
519
d543e20b 520(defvar custom-prefix-list nil
2365594b 521 "List of prefixes that should be ignored by `custom-unlispify'.")
d543e20b
PA
522
523(defcustom custom-unlispify-menu-entries t
10cb6e7b 524 "Display menu entries as words instead of symbols if non-nil."
6aaedd12 525 :group 'custom-menu
d543e20b
PA
526 :type 'boolean)
527
cda987f4
RS
528(defcustom custom-unlispify-remove-prefixes nil
529 "Non-nil means remove group prefixes from option names in buffer."
530 :group 'custom-menu
42b4edc5 531 :group 'custom-buffer
cda987f4
RS
532 :type 'boolean)
533
d543e20b 534(defun custom-unlispify-menu-entry (symbol &optional no-suffix)
2365594b 535 "Convert SYMBOL into a menu entry."
d543e20b
PA
536 (cond ((not custom-unlispify-menu-entries)
537 (symbol-name symbol))
538 ((get symbol 'custom-tag)
539 (if no-suffix
540 (get symbol 'custom-tag)
541 (concat (get symbol 'custom-tag) "...")))
542 (t
d9f67134 543 (with-current-buffer (get-buffer-create " *Custom-Work*")
d543e20b
PA
544 (erase-buffer)
545 (princ symbol (current-buffer))
546 (goto-char (point-min))
3b2f3d30
SM
547 ;; FIXME: Boolean variables are not predicates, so they shouldn't
548 ;; end with `-p'. -stef
549 ;; (when (and (eq (get symbol 'custom-type) 'boolean)
550 ;; (re-search-forward "-p\\'" nil t))
551 ;; (replace-match "" t t)
552 ;; (goto-char (point-min)))
cda987f4
RS
553 (if custom-unlispify-remove-prefixes
554 (let ((prefixes custom-prefix-list)
555 prefix)
556 (while prefixes
557 (setq prefix (car prefixes))
558 (if (search-forward prefix (+ (point) (length prefix)) t)
d3d4df42 559 (progn
cda987f4
RS
560 (setq prefixes nil)
561 (delete-region (point-min) (point)))
562 (setq prefixes (cdr prefixes))))))
d543e20b
PA
563 (subst-char-in-region (point-min) (point-max) ?- ?\ t)
564 (capitalize-region (point-min) (point-max))
d3d4df42 565 (unless no-suffix
d543e20b
PA
566 (goto-char (point-max))
567 (insert "..."))
568 (buffer-string)))))
569
570(defcustom custom-unlispify-tag-names t
10cb6e7b 571 "Display tag names as words instead of symbols if non-nil."
6aaedd12 572 :group 'custom-buffer
d543e20b
PA
573 :type 'boolean)
574
575(defun custom-unlispify-tag-name (symbol)
2365594b 576 "Convert SYMBOL into a menu entry."
d543e20b
PA
577 (let ((custom-unlispify-menu-entries custom-unlispify-tag-names))
578 (custom-unlispify-menu-entry symbol t)))
579
580(defun custom-prefix-add (symbol prefixes)
2365594b 581 "Add SYMBOL to list of ignored PREFIXES."
d543e20b
PA
582 (cons (or (get symbol 'custom-prefix)
583 (concat (symbol-name symbol) "-"))
584 prefixes))
585
bd042c03
PA
586;;; Guess.
587
588(defcustom custom-guess-name-alist
589 '(("-p\\'" boolean)
11f0cdf8 590 ("-flag\\'" boolean)
bd042c03
PA
591 ("-hook\\'" hook)
592 ("-face\\'" face)
593 ("-file\\'" file)
594 ("-function\\'" function)
595 ("-functions\\'" (repeat function))
596 ("-list\\'" (repeat sexp))
597 ("-alist\\'" (repeat (cons sexp sexp))))
598 "Alist of (MATCH TYPE).
599
d3d4df42 600MATCH should be a regexp matching the name of a symbol, and TYPE should
bd042c03
PA
601be a widget suitable for editing the value of that symbol. The TYPE
602of the first entry where MATCH matches the name of the symbol will be
d3d4df42 603used.
bd042c03
PA
604
605This is used for guessing the type of variables not declared with
606customize."
607 :type '(repeat (group (regexp :tag "Match") (sexp :tag "Type")))
3ee07367 608 :group 'custom-buffer)
d543e20b 609
bd042c03
PA
610(defcustom custom-guess-doc-alist
611 '(("\\`\\*?Non-nil " boolean))
612 "Alist of (MATCH TYPE).
d543e20b 613
bd042c03
PA
614MATCH should be a regexp matching a documentation string, and TYPE
615should be a widget suitable for editing the value of a variable with
616that documentation string. The TYPE of the first entry where MATCH
617matches the name of the symbol will be used.
d543e20b 618
bd042c03
PA
619This is used for guessing the type of variables not declared with
620customize."
621 :type '(repeat (group (regexp :tag "Match") (sexp :tag "Type")))
3ee07367 622 :group 'custom-buffer)
d543e20b 623
bd042c03
PA
624(defun custom-guess-type (symbol)
625 "Guess a widget suitable for editing the value of SYMBOL.
d3d4df42 626This is done by matching SYMBOL with `custom-guess-name-alist' and
bd042c03
PA
627if that fails, the doc string with `custom-guess-doc-alist'."
628 (let ((name (symbol-name symbol))
629 (names custom-guess-name-alist)
630 current found)
631 (while names
632 (setq current (car names)
633 names (cdr names))
634 (when (string-match (nth 0 current) name)
635 (setq found (nth 1 current)
636 names nil)))
637 (unless found
638 (let ((doc (documentation-property symbol 'variable-documentation))
639 (docs custom-guess-doc-alist))
d3d4df42 640 (when doc
bd042c03
PA
641 (while docs
642 (setq current (car docs)
643 docs (cdr docs))
644 (when (string-match (nth 0 current) doc)
645 (setq found (nth 1 current)
646 docs nil))))))
647 found))
d543e20b 648
25ac13b5
PA
649;;; Sorting.
650
ba5b643e 651;;;###autoload
da5ec617 652(defcustom custom-browse-sort-alphabetically nil
ba5b643e 653 "If non-nil, sort customization group alphabetically in `custom-browse'."
da5ec617
PA
654 :type 'boolean
655 :group 'custom-browse)
656
657(defcustom custom-browse-order-groups nil
658 "If non-nil, order group members within each customization group.
659If `first', order groups before non-groups.
660If `last', order groups after non-groups."
661 :type '(choice (const first)
662 (const last)
663 (const :tag "none" nil))
664 :group 'custom-browse)
665
c953515e
PA
666(defcustom custom-browse-only-groups nil
667 "If non-nil, show group members only within each customization group."
668 :type 'boolean
669 :group 'custom-browse)
670
ba5b643e 671;;;###autoload
647f9993
CY
672(defcustom custom-buffer-sort-alphabetically t
673 "Whether to sort customization groups alphabetically in Custom buffer."
944c91b6 674 :type 'boolean
1625d379
CY
675 :group 'custom-buffer
676 :version "24.1")
25ac13b5 677
da5ec617
PA
678(defcustom custom-buffer-order-groups 'last
679 "If non-nil, order group members within each customization group.
680If `first', order groups before non-groups.
681If `last', order groups after non-groups."
682 :type '(choice (const first)
683 (const last)
684 (const :tag "none" nil))
6aaedd12 685 :group 'custom-buffer)
25ac13b5 686
ba5b643e 687;;;###autoload
944c91b6 688(defcustom custom-menu-sort-alphabetically nil
ba5b643e 689 "If non-nil, sort each customization group alphabetically in menus."
944c91b6 690 :type 'boolean
6aaedd12 691 :group 'custom-menu)
25ac13b5 692
da5ec617
PA
693(defcustom custom-menu-order-groups 'first
694 "If non-nil, order group members within each customization group.
695If `first', order groups before non-groups.
696If `last', order groups after non-groups."
697 :type '(choice (const first)
698 (const last)
699 (const :tag "none" nil))
6aaedd12
PA
700 :group 'custom-menu)
701
6bdad9ae 702;;;###autoload (add-hook 'same-window-regexps (purecopy "\\`\\*Customiz.*\\*\\'"))
b4854a23 703
da5ec617
PA
704(defun custom-sort-items (items sort-alphabetically order-groups)
705 "Return a sorted copy of ITEMS.
706ITEMS should be a `custom-group' property.
707If SORT-ALPHABETICALLY non-nil, sort alphabetically.
708If ORDER-GROUPS is `first' order groups before non-groups, if `last' order
709groups after non-groups, if nil do not order groups at all."
710 (sort (copy-sequence items)
711 (lambda (a b)
712 (let ((typea (nth 1 a)) (typeb (nth 1 b))
359476e0 713 (namea (nth 0 a)) (nameb (nth 0 b)))
da5ec617
PA
714 (cond ((not order-groups)
715 ;; Since we don't care about A and B order, maybe sort.
716 (when sort-alphabetically
717 (string-lessp namea nameb)))
718 ((eq typea 'custom-group)
719 ;; If B is also a group, maybe sort. Otherwise, order A and B.
720 (if (eq typeb 'custom-group)
721 (when sort-alphabetically
722 (string-lessp namea nameb))
723 (eq order-groups 'first)))
724 ((eq typeb 'custom-group)
725 ;; Since A cannot be a group, order A and B.
726 (eq order-groups 'last))
727 (sort-alphabetically
728 ;; Since A and B cannot be groups, sort.
729 (string-lessp namea nameb)))))))
25ac13b5 730
d543e20b
PA
731;;; Custom Mode Commands.
732
9db1942d 733;; This variable is used by `custom-tool-bar-map', or directly by
f6542587 734;; `custom-buffer-create-internal' if `custom-buffer-verbose-help' is non-nil.
9db1942d
CY
735
736(defvar custom-commands
587faadd 737 '((" Set for current session " Custom-set t
9db1942d 738 "Apply all settings in this buffer to the current session"
f904c0f9
JD
739 "index"
740 "Apply")
587faadd 741 (" Save for future sessions " Custom-save
9db1942d
CY
742 (or custom-file user-init-file)
743 "Apply all settings in this buffer and save them for future Emacs sessions."
f904c0f9
JD
744 "save"
745 "Save")
587faadd 746 (" Undo edits " Custom-reset-current t
9db1942d 747 "Restore all settings in this buffer to reflect their current values."
f904c0f9
JD
748 "refresh"
749 "Undo")
587faadd 750 (" Reset to saved " Custom-reset-saved t
9db1942d 751 "Restore all settings in this buffer to their saved values (if any)."
f904c0f9
JD
752 "undo"
753 "Reset")
587faadd 754 (" Erase customizations " Custom-reset-standard
9db1942d
CY
755 (or custom-file user-init-file)
756 "Un-customize all settings in this buffer and save them with standard values."
f904c0f9
JD
757 "delete"
758 "Uncustomize")
587faadd 759 (" Help for Customize " Custom-help t
9db1942d 760 "Get help for using Customize."
f904c0f9
JD
761 "help"
762 "Help")
587faadd 763 (" Exit " Custom-buffer-done t "Exit Customize." "exit" "Exit")))
9db1942d
CY
764
765(defun Custom-help ()
766 "Read the node on Easy Customization in the Emacs manual."
d543e20b 767 (interactive)
9db1942d 768 (info "(emacs)Easy Customization"))
d543e20b 769
d3d4df42 770(defvar custom-reset-menu
70482877
LT
771 '(("Undo Edits" . Custom-reset-current)
772 ("Reset to Saved" . Custom-reset-saved)
9db1942d 773 ("Erase Customizations (use standard values)" . Custom-reset-standard))
d543e20b
PA
774 "Alist of actions for the `Reset' button.
775The key is a string containing the name of the action, the value is a
2365594b 776Lisp function taking the widget as an element which will be called
d543e20b
PA
777when the action is chosen.")
778
9db1942d
CY
779(defvar custom-options nil
780 "Customization widgets in the current buffer.")
781
782(defun custom-command-apply (fun query &optional strong-query)
783 "Call function FUN on all widgets in `custom-options'.
784If there is more than one widget, ask user for confirmation using
785the query string QUERY, using `y-or-n-p' if STRONG-QUERY is nil,
786and `yes-or-no-p' otherwise."
787 (if (or (and (= 1 (length custom-options))
788 (memq (widget-type (car custom-options))
789 '(custom-variable custom-face)))
790 (funcall (if strong-query 'yes-or-no-p 'y-or-n-p) query))
791 (progn (mapc fun custom-options) t)
792 (message "Aborted")
793 nil))
794
795(defun Custom-set (&rest ignore)
796 "Set the current value of all edited settings in the buffer."
797 (interactive)
798 (custom-command-apply
799 (lambda (child)
800 (when (eq (widget-get child :custom-state) 'modified)
801 (widget-apply child :custom-set)))
802 "Set all values according to this buffer? "))
803
804(defun Custom-save (&rest ignore)
805 "Set all edited settings, then save all settings that have been set.
5ae87ed4
MR
806If a setting was edited and set before, this saves it. If a
807setting was merely edited before, this sets it then saves it."
9db1942d 808 (interactive)
5ae87ed4
MR
809 (when (custom-command-apply
810 (lambda (child)
811 (when (memq (widget-get child :custom-state)
812 '(modified set changed rogue))
813 (widget-apply child :custom-mark-to-save)))
814 "Save all settings in this buffer? " t)
815 ;; Save changes to buffer and redraw.
816 (custom-save-all)
817 (dolist (child custom-options)
818 (widget-apply child :custom-state-set-and-redraw))))
9db1942d
CY
819
820(defun custom-reset (widget &optional event)
d543e20b
PA
821 "Select item from reset menu."
822 (let* ((completion-ignore-case t)
eaa99205 823 (answer (widget-choose "Reset settings"
d543e20b
PA
824 custom-reset-menu
825 event)))
826 (if answer
827 (funcall answer))))
828
ab678382 829(defun Custom-reset-current (&rest ignore)
67177975 830 "Reset all edited settings in the buffer to show their current values."
d543e20b 831 (interactive)
9db1942d
CY
832 (custom-command-apply
833 (lambda (widget)
834 (if (memq (widget-get widget :custom-state) '(modified changed))
835 (widget-apply widget :custom-reset-current)))
836 "Reset all settings' buffer text to show current values? "))
d543e20b 837
ab678382 838(defun Custom-reset-saved (&rest ignore)
67177975
RS
839 "Reset all edited or set settings in the buffer to their saved value.
840This also shows the saved values in the buffer."
d543e20b 841 (interactive)
9db1942d
CY
842 (custom-command-apply
843 (lambda (widget)
844 (if (memq (widget-get widget :custom-state) '(modified set changed rogue))
845 (widget-apply widget :custom-reset-saved)))
846 "Reset all settings (current values and buffer text) to saved values? "))
d543e20b 847
5ae87ed4
MR
848;; The next two variables are bound to '(t) by `Custom-reset-standard'
849;; and `custom-group-reset-standard'. If these variables are nil, both
850;; `custom-variable-reset-standard' and `custom-face-reset-standard'
851;; save, reset and redraw the handled widget immediately. Otherwise,
852;; they add the widget to the corresponding list and leave it to
853;; `custom-reset-standard-save-and-update' to save, reset and redraw it.
854(defvar custom-reset-standard-variables-list nil)
855(defvar custom-reset-standard-faces-list nil)
856
857;; The next function was excerpted from `custom-variable-reset-standard'
858;; and `custom-face-reset-standard' and is used to avoid calling
859;; `custom-save-all' repeatedly (and thus saving settings to file one by
860;; one) when erasing all customizations.
861(defun custom-reset-standard-save-and-update ()
862 "Save settings and redraw after erasing customizations."
863 (when (or (and custom-reset-standard-variables-list
864 (not (eq custom-reset-standard-variables-list '(t))))
865 (and custom-reset-standard-faces-list
866 (not (eq custom-reset-standard-faces-list '(t)))))
867 ;; Save settings to file.
868 (custom-save-all)
869 ;; Set state of and redraw variables.
870 (dolist (widget custom-reset-standard-variables-list)
871 (unless (eq widget t)
872 (widget-put widget :custom-state 'unknown)
873 (custom-redraw widget)))
874 ;; Set state of and redraw faces.
875 (dolist (widget custom-reset-standard-faces-list)
876 (unless (eq widget t)
877 (let* ((symbol (widget-value widget))
878 (child (car (widget-get widget :children)))
879 (value (get symbol 'face-defface-spec))
880 (comment-widget (widget-get widget :comment-widget)))
881 (put symbol 'face-comment nil)
882 (widget-value-set child
883 (custom-pre-filter-face-spec
884 (list (list t (custom-face-attributes-get
885 symbol nil)))))
886 ;; This call manages the comment visibility
887 (widget-value-set comment-widget "")
888 (custom-face-state-set widget)
889 (custom-redraw-magic widget))))))
890
ab678382 891(defun Custom-reset-standard (&rest ignore)
5ae87ed4 892 "Erase all customizations (either current or saved) in current buffer.
50137744
RS
893The immediate result is to restore them to their standard values.
894This operation eliminates any saved values for the group members,
19d63704 895making them as if they had never been customized at all."
d543e20b 896 (interactive)
5ae87ed4
MR
897 ;; Bind these temporarily.
898 (let ((custom-reset-standard-variables-list '(t))
899 (custom-reset-standard-faces-list '(t)))
900 (custom-command-apply
901 (lambda (widget)
902 (and (or (null (widget-get widget :custom-standard-value))
903 (widget-apply widget :custom-standard-value))
904 (memq (widget-get widget :custom-state)
905 '(modified set changed saved rogue))
906 (widget-apply widget :custom-mark-to-reset-standard)))
907 "Erase all customizations for settings in this buffer? " t)
908 (custom-reset-standard-save-and-update)))
d543e20b
PA
909
910;;; The Customize Commands
911
d3d4df42 912(defun custom-prompt-variable (prompt-var prompt-val &optional comment)
6d528fc5
PA
913 "Prompt for a variable and a value and return them as a list.
914PROMPT-VAR is the prompt for the variable, and PROMPT-VAL is the
915prompt for the value. The %s escape in PROMPT-VAL is replaced with
916the name of the variable.
917
918If the variable has a `variable-interactive' property, that is used as if
919it were the arg to `interactive' (which see) to interactively read the value.
920
921If the variable has a `custom-type' property, it must be a widget and the
d3d4df42
DL
922`:prompt-value' property of that widget will be used for reading the value.
923
10cb6e7b 924If optional COMMENT argument is non-nil, also prompt for a comment and return
d3d4df42 925it as the third element in the list."
6d528fc5 926 (let* ((var (read-variable prompt-var))
d3d4df42
DL
927 (minibuffer-help-form '(describe-variable var))
928 (val
6d528fc5
PA
929 (let ((prop (get var 'variable-interactive))
930 (type (get var 'custom-type))
931 (prompt (format prompt-val var)))
932 (unless (listp type)
933 (setq type (list type)))
934 (cond (prop
935 ;; Use VAR's `variable-interactive' property
936 ;; as an interactive spec for prompting.
9db1942d
CY
937 (call-interactively `(lambda (arg)
938 (interactive ,prop)
939 arg)))
6d528fc5
PA
940 (type
941 (widget-prompt-value type
942 prompt
943 (if (boundp var)
944 (symbol-value var))
945 (not (boundp var))))
946 (t
d3d4df42
DL
947 (eval-minibuffer prompt))))))
948 (if comment
949 (list var val
950 (read-string "Comment: " (get var 'variable-comment)))
951 (list var val))))
6d528fc5
PA
952
953;;;###autoload
7ec8d2c6 954(defun customize-set-value (variable value &optional comment)
4f9b9060 955 "Set VARIABLE to VALUE, and return VALUE. VALUE is a Lisp object.
6d528fc5
PA
956
957If VARIABLE has a `variable-interactive' property, that is used as if
958it were the arg to `interactive' (which see) to interactively read the value.
959
960If VARIABLE has a `custom-type' property, it must be a widget and the
d3d4df42
DL
961`:prompt-value' property of that widget will be used for reading the value.
962
963If given a prefix (or a COMMENT argument), also prompt for a comment."
6d528fc5 964 (interactive (custom-prompt-variable "Set variable: "
d3d4df42
DL
965 "Set %s to value: "
966 current-prefix-arg))
ee1f522d 967
d3d4df42 968 (cond ((string= comment "")
7ec8d2c6 969 (put variable 'variable-comment nil))
d3d4df42 970 (comment
7ec8d2c6
PJ
971 (put variable 'variable-comment comment)))
972 (set variable value))
6d528fc5
PA
973
974;;;###autoload
2365594b 975(defun customize-set-variable (variable value &optional comment)
4f9b9060
PA
976 "Set the default for VARIABLE to VALUE, and return VALUE.
977VALUE is a Lisp object.
6d528fc5
PA
978
979If VARIABLE has a `custom-set' property, that is used for setting
980VARIABLE, otherwise `set-default' is used.
981
6d528fc5
PA
982If VARIABLE has a `variable-interactive' property, that is used as if
983it were the arg to `interactive' (which see) to interactively read the value.
984
985If VARIABLE has a `custom-type' property, it must be a widget and the
d3d4df42
DL
986`:prompt-value' property of that widget will be used for reading the value.
987
988If given a prefix (or a COMMENT argument), also prompt for a comment."
6d528fc5 989 (interactive (custom-prompt-variable "Set variable: "
d3d4df42
DL
990 "Set customized value for %s to: "
991 current-prefix-arg))
4f9b9060 992 (custom-load-symbol variable)
8ed50a66 993 (custom-push-theme 'theme-value variable 'user 'set (custom-quote value))
2365594b
DL
994 (funcall (or (get variable 'custom-set) 'set-default) variable value)
995 (put variable 'customized-value (list (custom-quote value)))
d3d4df42 996 (cond ((string= comment "")
2365594b
DL
997 (put variable 'variable-comment nil)
998 (put variable 'customized-variable-comment nil))
d3d4df42 999 (comment
2365594b 1000 (put variable 'variable-comment comment)
4f9b9060
PA
1001 (put variable 'customized-variable-comment comment)))
1002 value)
6d528fc5 1003
4ee1cf9f 1004;;;###autoload
7ec8d2c6 1005(defun customize-save-variable (variable value &optional comment)
4ee1cf9f 1006 "Set the default for VARIABLE to VALUE, and save it for future sessions.
4f9b9060
PA
1007Return VALUE.
1008
4ee1cf9f
PA
1009If VARIABLE has a `custom-set' property, that is used for setting
1010VARIABLE, otherwise `set-default' is used.
1011
4ee1cf9f
PA
1012If VARIABLE has a `variable-interactive' property, that is used as if
1013it were the arg to `interactive' (which see) to interactively read the value.
1014
1015If VARIABLE has a `custom-type' property, it must be a widget and the
d3d4df42
DL
1016`:prompt-value' property of that widget will be used for reading the value.
1017
1018If given a prefix (or a COMMENT argument), also prompt for a comment."
901cd78b 1019 (interactive (custom-prompt-variable "Set and save variable: "
d3d4df42
DL
1020 "Set and save value for %s as: "
1021 current-prefix-arg))
7ec8d2c6
PJ
1022 (funcall (or (get variable 'custom-set) 'set-default) variable value)
1023 (put variable 'saved-value (list (custom-quote value)))
c5d290fb 1024 (custom-push-theme 'theme-value variable 'user 'set (custom-quote value))
d3d4df42 1025 (cond ((string= comment "")
7ec8d2c6
PJ
1026 (put variable 'variable-comment nil)
1027 (put variable 'saved-variable-comment nil))
d3d4df42 1028 (comment
7ec8d2c6
PJ
1029 (put variable 'variable-comment comment)
1030 (put variable 'saved-variable-comment comment)))
fe4acc60
CY
1031 (put variable 'customized-value nil)
1032 (put variable 'customized-variable-comment nil)
4f9b9060
PA
1033 (custom-save-all)
1034 value)
4ee1cf9f 1035
d543e20b 1036;;;###autoload
5dd0cad0
RS
1037(defun customize ()
1038 "Select a customization buffer which you can use to set user options.
1039User options are structured into \"groups\".
1040Initially the top-level group `Emacs' and its immediate subgroups
1041are shown; the contents of those subgroups are initially hidden."
1042 (interactive)
dc2e979f 1043 (customize-group 'emacs))
5dd0cad0 1044
87434e7a
SM
1045;;;###autoload
1046(defun customize-mode (mode)
1047 "Customize options related to the current major mode.
1048If a prefix \\[universal-argument] was given (or if the current major mode has no known group),
1049then prompt for the MODE to customize."
1050 (interactive
1051 (list
1052 (let ((completion-regexp-list '("-mode\\'"))
1053 (group (custom-group-of-mode major-mode)))
1054 (if (and group (not current-prefix-arg))
1055 major-mode
1056 (intern
1057 (completing-read (if group
1058 (format "Major mode (default %s): " major-mode)
1059 "Major mode: ")
1060 obarray
1061 'custom-group-of-mode
1062 t nil nil (if group (symbol-name major-mode))))))))
1063 (customize-group (custom-group-of-mode mode)))
1064
a0247dcf
SM
1065(defun customize-read-group ()
1066 (let ((completion-ignore-case t))
1067 (completing-read "Customize group (default emacs): "
1068 obarray
1069 (lambda (symbol)
1070 (or (and (get symbol 'custom-loads)
1071 (not (get symbol 'custom-autoload)))
1072 (get symbol 'custom-group)))
1073 t)))
87434e7a 1074
5dd0cad0 1075;;;###autoload
a0247dcf 1076(defun customize-group (&optional group)
5dd0cad0 1077 "Customize GROUP, which must be a customization group."
a0247dcf 1078 (interactive (list (customize-read-group)))
5dd0cad0
RS
1079 (when (stringp group)
1080 (if (string-equal "" group)
1081 (setq group 'emacs)
1082 (setq group (intern group))))
241d3080
RS
1083 (let ((name (format "*Customize Group: %s*"
1084 (custom-unlispify-tag-name group))))
1085 (if (get-buffer name)
a0247dcf
SM
1086 (pop-to-buffer name)
1087 (custom-buffer-create
1088 (list (list group 'custom-group))
1089 name
1090 (concat " for group "
1091 (custom-unlispify-tag-name group))))))
d543e20b 1092
6d528fc5 1093;;;###autoload
9db1942d
CY
1094(defun customize-group-other-window (&optional group)
1095 "Customize GROUP, which must be a customization group, in another window."
a0247dcf
SM
1096 (interactive (list (customize-read-group)))
1097 (let ((pop-up-windows t)
1098 (same-window-buffer-names nil)
1099 (same-window-regexps nil))
1100 (customize-group group)))
6d528fc5 1101
9097aeb7
PA
1102;;;###autoload
1103(defalias 'customize-variable 'customize-option)
38d58078 1104
d543e20b 1105;;;###autoload
38d58078
RS
1106(defun customize-option (symbol)
1107 "Customize SYMBOL, which must be a user option variable."
bd042c03 1108 (interactive (custom-variable-prompt))
161c7127
RS
1109 (unless symbol
1110 (error "No variable specified"))
dcb380c0
LT
1111 (let ((basevar (indirect-variable symbol)))
1112 (custom-buffer-create (list (list basevar 'custom-variable))
1113 (format "*Customize Option: %s*"
1114 (custom-unlispify-tag-name basevar)))
1115 (unless (eq symbol basevar)
1116 (message "`%s' is an alias for `%s'" symbol basevar))))
d543e20b 1117
ffec8c5a
MR
1118;;;###autoload
1119(defalias 'customize-variable-other-window 'customize-option-other-window)
1120
1121;;;###autoload
1122(defun customize-option-other-window (symbol)
1123 "Customize SYMBOL, which must be a user option variable.
1124Show the buffer in another window, but don't select it."
1125 (interactive (custom-variable-prompt))
161c7127
RS
1126 (unless symbol
1127 (error "No variable specified"))
dcb380c0
LT
1128 (let ((basevar (indirect-variable symbol)))
1129 (custom-buffer-create-other-window
1130 (list (list basevar 'custom-variable))
1131 (format "*Customize Option: %s*" (custom-unlispify-tag-name basevar)))
1132 (unless (eq symbol basevar)
1133 (message "`%s' is an alias for `%s'" symbol basevar))))
ffec8c5a 1134
0dcf7d7e 1135(defvar customize-changed-options-previous-release "23.1"
e418be26
KH
1136 "Version for `customize-changed-options' to refer back to by default.")
1137
4e9c705e
BW
1138;; Packages will update this variable, so make it available.
1139;;;###autoload
1140(defvar customize-package-emacs-version-alist nil
7eb944cf
RS
1141 "Alist mapping versions of a package to Emacs versions.
1142We use this for packages that have their own names, but are released
1143as part of Emacs itself.
1144
1145Each elements looks like this:
994019df
BW
1146
1147 (PACKAGE (PVERSION . EVERSION)...)
1148
7eb944cf
RS
1149Here PACKAGE is the name of a package, as a symbol. After
1150PACKAGE come one or more elements, each associating a
1151package version PVERSION with the first Emacs version
1152EVERSION in which it (or a subsequent version of PACKAGE)
1153was first released. Both PVERSION and EVERSION are strings.
1154PVERSION should be a string that this package used in
1155the :package-version keyword for `defcustom', `defgroup',
1156and `defface'.
1157
1158For example, the MH-E package updates this alist as follows:
994019df
BW
1159
1160 (add-to-list 'customize-package-emacs-version-alist
1161 '(MH-E (\"6.0\" . \"22.1\") (\"6.1\" . \"22.1\")
1162 (\"7.0\" . \"22.1\") (\"7.1\" . \"22.1\")
1163 (\"7.2\" . \"22.1\") (\"7.3\" . \"22.1\")
1164 (\"7.4\" . \"22.1\") (\"8.0\" . \"22.1\")))
1165
1166The value of PACKAGE needs to be unique and it needs to match the
1167PACKAGE value appearing in the :package-version keyword. Since
1168the user might see the value in a error message, a good choice is
1169the official name of the package, such as MH-E or Gnus.")
4e9c705e 1170
bba50f8a
RS
1171;;;###autoload
1172(defalias 'customize-changed 'customize-changed-options)
1173
c32de15e 1174;;;###autoload
8c89fb87 1175(defun customize-changed-options (&optional since-version)
bba50f8a 1176 "Customize all settings whose meanings have changed in Emacs itself.
e418be26 1177This includes new user option variables and faces, and new
bba50f8a
RS
1178customization groups, as well as older options and faces whose meanings
1179or default values have changed since the previous major Emacs release.
e418be26 1180
bba50f8a
RS
1181With argument SINCE-VERSION (a string), customize all settings
1182that were added or redefined since that version."
e418be26 1183
2071d184
LT
1184 (interactive
1185 (list
1186 (read-from-minibuffer
1187 (format "Customize options changed, since version (default %s): "
1188 customize-changed-options-previous-release))))
f50dc5d2 1189 (if (equal since-version "")
26c67de8
DL
1190 (setq since-version nil)
1191 (unless (condition-case nil
1192 (numberp (read since-version))
1193 (error nil))
1194 (signal 'wrong-type-argument (list 'numberp since-version))))
e418be26
KH
1195 (unless since-version
1196 (setq since-version customize-changed-options-previous-release))
cba752d0
MR
1197
1198 ;; Load the information for versions since since-version. We use
1199 ;; custom-load-symbol for this.
1200 (put 'custom-versions-load-alist 'custom-loads nil)
1201 (dolist (elt custom-versions-load-alist)
2ee398c4
MR
1202 (if (customize-version-lessp since-version (car elt))
1203 (dolist (load (cdr elt))
1204 (custom-add-load 'custom-versions-load-alist load))))
cba752d0
MR
1205 (custom-load-symbol 'custom-versions-load-alist)
1206 (put 'custom-versions-load-alist 'custom-loads nil)
1207
1208 (let (found)
1209 (mapatoms
1210 (lambda (symbol)
4e9c705e
BW
1211 (let* ((package-version (get symbol 'custom-package-version))
1212 (version
1213 (or (and package-version
1214 (customize-package-emacs-version symbol
1215 package-version))
1216 (get symbol 'custom-version))))
cba752d0
MR
1217 (if version
1218 (when (customize-version-lessp since-version version)
1219 (if (or (get symbol 'custom-group)
1220 (get symbol 'group-documentation))
1221 (push (list symbol 'custom-group) found))
1222 (if (custom-variable-p symbol)
1223 (push (list symbol 'custom-variable) found))
1224 (if (custom-facep symbol)
1225 (push (list symbol 'custom-face) found)))))))
1226 (if found
1227 (custom-buffer-create (custom-sort-items found t 'first)
1228 "*Customize Changed Options*")
1229 (error "No user option defaults have been changed since Emacs %s"
1230 since-version))))
f50dc5d2 1231
4e9c705e 1232(defun customize-package-emacs-version (symbol package-version)
7eb944cf
RS
1233 "Return the Emacs version in which SYMBOL's meaning last changed.
1234PACKAGE-VERSION has the form (PACKAGE . VERSION). We use
4e9c705e 1235`customize-package-emacs-version-alist' to find the version of
7eb944cf 1236Emacs that is associated with version VERSION of PACKAGE."
4e9c705e
BW
1237 (let (package-versions emacs-version)
1238 ;; Use message instead of error since we want user to be able to
1239 ;; see the rest of the symbols even if a package author has
1240 ;; botched things up.
1241 (cond ((not (listp package-version))
1242 (message "Invalid package-version value for %s" symbol))
1243 ((setq package-versions (assq (car package-version)
1244 customize-package-emacs-version-alist))
1245 (setq emacs-version
994019df 1246 (cdr (assoc (cdr package-version) package-versions)))
4e9c705e 1247 (unless emacs-version
994019df
BW
1248 (message "%s version %s not found in %s" symbol
1249 (cdr package-version)
4e9c705e
BW
1250 "customize-package-emacs-version-alist")))
1251 (t
7eb944cf 1252 (message "Package %s version %s lists no corresponding Emacs version"
4e9c705e 1253 (car package-version)
7eb944cf 1254 (cdr package-version))))
4e9c705e
BW
1255 emacs-version))
1256
f50dc5d2 1257(defun customize-version-lessp (version1 version2)
26c67de8
DL
1258 ;; Why are the versions strings, and given that they are, why aren't
1259 ;; they converted to numbers and compared as such here? -- fx
1260
e418be26
KH
1261 ;; In case someone made a mistake and left out the quotes
1262 ;; in the :version value.
1263 (if (numberp version2)
1264 (setq version2 (prin1-to-string version2)))
f50dc5d2 1265 (let (major1 major2 minor1 minor2)
26c67de8
DL
1266 (string-match "\\([0-9]+\\)\\(\\.\\([0-9]+\\)\\)?" version1)
1267 (setq major1 (read (or (match-string 1 version1)
1268 "0")))
1269 (setq minor1 (read (or (match-string 3 version1)
1270 "0")))
1271 (string-match "\\([0-9]+\\)\\(\\.\\([0-9]+\\)\\)?" version2)
1272 (setq major2 (read (or (match-string 1 version2)
1273 "0")))
1274 (setq minor2 (read (or (match-string 3 version2)
1275 "0")))
f50dc5d2
KH
1276 (or (< major1 major2)
1277 (and (= major1 major2)
1278 (< minor1 minor2)))))
c942535f 1279
d543e20b 1280;;;###autoload
a0247dcf 1281(defun customize-face (&optional face)
3127aa13 1282 "Customize FACE, which should be a face name or nil.
c40f3193
GM
1283If FACE is nil, customize all faces. If FACE is actually a
1284face-alias, customize the face it is aliased to.
d29a536a
GM
1285
1286Interactively, when point is on text which has a face specified,
3127aa13 1287suggest to customize that face, if it's customizable."
a0247dcf 1288 (interactive (list (read-face-name "Customize face" "all faces" t)))
cdd50dea
RS
1289 (if (member face '(nil ""))
1290 (setq face (face-list)))
1291 (if (and (listp face) (null (cdr face)))
1292 (setq face (car face)))
a0247dcf
SM
1293 (if (listp face)
1294 (custom-buffer-create
1295 (custom-sort-items
1296 (mapcar (lambda (s) (list s 'custom-face)) face)
1297 t nil)
1298 "*Customize Faces*")
1299 ;; If FACE is actually an alias, customize the face it is aliased to.
1300 (if (get face 'face-alias)
1301 (setq face (get face 'face-alias)))
1302 (unless (facep face)
1303 (error "Invalid face %S" face))
1304 (custom-buffer-create
1305 (list (list face 'custom-face))
1306 (format "*Customize Face: %s*"
1307 (custom-unlispify-tag-name face)))))
d543e20b 1308
bd042c03 1309;;;###autoload
701f49d2 1310(defun customize-face-other-window (&optional face)
3127aa13 1311 "Show customization buffer for face FACE in other window.
c40f3193 1312If FACE is actually a face-alias, customize the face it is aliased to.
d29a536a
GM
1313
1314Interactively, when point is on text which has a face specified,
3127aa13 1315suggest to customize that face, if it's customizable."
a0247dcf
SM
1316 (interactive (list (read-face-name "Customize face" "all faces" t)))
1317 (let ((pop-up-windows t)
1318 (same-window-buffer-names nil)
1319 (same-window-regexps nil))
1320 (customize-face face)))
bd042c03 1321
0b97da8f
RS
1322(defalias 'customize-customized 'customize-unsaved)
1323
d543e20b 1324;;;###autoload
0b97da8f
RS
1325(defun customize-unsaved ()
1326 "Customize all user options set in this session but not saved."
6d528fc5
PA
1327 (interactive)
1328 (let ((found nil))
1329 (mapatoms (lambda (symbol)
d3d4df42
DL
1330 (and (or (get symbol 'customized-face)
1331 (get symbol 'customized-face-comment))
6d528fc5 1332 (custom-facep symbol)
a1a4fa22 1333 (push (list symbol 'custom-face) found))
d3d4df42
DL
1334 (and (or (get symbol 'customized-value)
1335 (get symbol 'customized-variable-comment))
6d528fc5 1336 (boundp symbol)
a1a4fa22 1337 (push (list symbol 'custom-variable) found))))
da5ec617 1338 (if (not found)
0b97da8f 1339 (error "No user options are set but unsaved")
da5ec617 1340 (custom-buffer-create (custom-sort-items found t nil)
0b97da8f 1341 "*Customize Unsaved*"))))
6d528fc5
PA
1342
1343;;;###autoload
963c2e09 1344(defun customize-rogue ()
c111d7ae 1345 "Customize all user variables modified outside customize."
963c2e09
PA
1346 (interactive)
1347 (let ((found nil))
1348 (mapatoms (lambda (symbol)
1349 (let ((cval (or (get symbol 'customized-value)
1350 (get symbol 'saved-value)
1351 (get symbol 'standard-value))))
1352 (when (and cval ;Declared with defcustom.
1353 (default-boundp symbol) ;Has a value.
3127aa13 1354 (not (equal (eval (car cval))
963c2e09
PA
1355 ;; Which does not match customize.
1356 (default-value symbol))))
1357 (push (list symbol 'custom-variable) found)))))
1358 (if (not found)
1359 (error "No rogue user options")
1360 (custom-buffer-create (custom-sort-items found t nil)
1361 "*Customize Rogue*"))))
1362;;;###autoload
6d528fc5
PA
1363(defun customize-saved ()
1364 "Customize all already saved user options."
d543e20b
PA
1365 (interactive)
1366 (let ((found nil))
1367 (mapatoms (lambda (symbol)
d3d4df42
DL
1368 (and (or (get symbol 'saved-face)
1369 (get symbol 'saved-face-comment))
d543e20b 1370 (custom-facep symbol)
a1a4fa22 1371 (push (list symbol 'custom-face) found))
d3d4df42
DL
1372 (and (or (get symbol 'saved-value)
1373 (get symbol 'saved-variable-comment))
d543e20b 1374 (boundp symbol)
a1a4fa22 1375 (push (list symbol 'custom-variable) found))))
da5ec617
PA
1376 (if (not found )
1377 (error "No saved user options")
1378 (custom-buffer-create (custom-sort-items found t nil)
1379 "*Customize Saved*"))))
d543e20b 1380
647f9993
CY
1381(declare-function apropos-parse-pattern "apropos" (pattern))
1382
d543e20b 1383;;;###autoload
647f9993
CY
1384(defun customize-apropos (pattern &optional type)
1385 "Customize all loaded options, faces and groups matching PATTERN.
1386PATTERN can be a word, a list of words (separated by spaces),
1387or a regexp (using some regexp special characters). If it is a word,
1388search for matches for that word as a substring. If it is a list of words,
1389search for matches for any two (or more) of those words.
1390
1391If TYPE is `options', include only options.
1392If TYPE is `faces', include only faces.
1393If TYPE is `groups', include only groups.
1394If TYPE is t (interactively, with prefix arg), include variables
06cdb5db
LT
1395that are not customizable options, as well as faces and groups
1396\(but we recommend using `apropos-variable' instead)."
647f9993
CY
1397 (interactive (list (apropos-read-pattern "symbol") current-prefix-arg))
1398 (require 'apropos)
1399 (apropos-parse-pattern pattern)
1400 (let (found tests)
1401 (mapatoms
1402 `(lambda (symbol)
1403 (when (string-match apropos-regexp (symbol-name symbol))
1404 ,(if (not (memq type '(faces options)))
1405 '(if (get symbol 'custom-group)
1406 (push (list symbol 'custom-group) found)))
1407 ,(if (not (memq type '(options groups)))
1408 '(if (custom-facep symbol)
1409 (push (list symbol 'custom-face) found)))
1410 ,(if (not (memq type '(groups faces)))
1411 `(if (and (boundp symbol)
1412 (eq (indirect-variable symbol) symbol)
1413 (or (get symbol 'saved-value)
1414 (custom-variable-p symbol)
1415 ,(if (not (memq type '(nil options)))
1416 '(get symbol 'variable-documentation))))
1417 (push (list symbol 'custom-variable) found))))))
a1a4fa22 1418 (if (not found)
da435d1c 1419 (error "No %s matching %s"
647f9993
CY
1420 (if (eq type t)
1421 "items"
1422 (format "customizable %s"
1423 (if (memq type '(options faces groups))
1424 (symbol-name type)
1425 "items")))
1426 pattern)
81711dba
EZ
1427 (custom-buffer-create
1428 (custom-sort-items found t custom-buffer-order-groups)
1429 "*Customize Apropos*"))))
a1a4fa22
PA
1430
1431;;;###autoload
1432(defun customize-apropos-options (regexp &optional arg)
0322b9c3 1433 "Customize all loaded customizable options matching REGEXP.
6cda144f 1434With prefix ARG, include variables that are not customizable options
8ed10e73 1435\(but it is better to use `apropos-variable' if you want to find those)."
bd18cd19 1436 (interactive "sCustomize options (regexp): \nP")
a1a4fa22
PA
1437 (customize-apropos regexp (or arg 'options)))
1438
1439;;;###autoload
1440(defun customize-apropos-faces (regexp)
0322b9c3 1441 "Customize all loaded faces matching REGEXP."
bd18cd19 1442 (interactive "sCustomize faces (regexp): \n")
a1a4fa22
PA
1443 (customize-apropos regexp 'faces))
1444
1445;;;###autoload
1446(defun customize-apropos-groups (regexp)
0322b9c3 1447 "Customize all loaded groups matching REGEXP."
bd18cd19 1448 (interactive "sCustomize groups (regexp): \n")
a1a4fa22 1449 (customize-apropos regexp 'groups))
d543e20b 1450
6d528fc5
PA
1451;;; Buffer.
1452
944c91b6
PA
1453(defcustom custom-buffer-style 'links
1454 "Control the presentation style for customization buffers.
1455The value should be a symbol, one of:
1456
1457brackets: groups nest within each other with big horizontal brackets.
1458links: groups have links to subgroups."
1459 :type '(radio (const brackets)
1460 (const links))
1461 :group 'custom-buffer)
1462
97733c3d 1463(defcustom custom-buffer-done-kill nil
6cda144f 1464 "Non-nil means exiting a Custom buffer should kill it."
97733c3d 1465 :type 'boolean
bf247b6e 1466 :version "22.1"
d3d4df42
DL
1467 :group 'custom-buffer)
1468
944c91b6
PA
1469(defcustom custom-buffer-indent 3
1470 "Number of spaces to indent nested groups."
1471 :type 'integer
1472 :group 'custom-buffer)
1473
40627755
SM
1474(defun custom-get-fresh-buffer (name)
1475 "Get a fresh new buffer with name NAME.
a153acb7
SM
1476If the buffer already exist, clean it up to be like new.
1477Beware: it's not quite like new. Good enough for custom, but maybe
1478not for everybody."
1479 ;; To be more complete, we should also kill all permanent-local variables,
1480 ;; but it's not needed for custom.
40627755 1481 (let ((buf (get-buffer name)))
fd1c38f4 1482 (when (and buf (buffer-local-value 'buffer-file-name buf))
a153acb7
SM
1483 ;; This will check if the file is not saved.
1484 (kill-buffer buf)
1485 (setq buf nil))
40627755
SM
1486 (if (null buf)
1487 (get-buffer-create name)
1488 (with-current-buffer buf
1489 (kill-all-local-variables)
a153acb7 1490 (run-hooks 'kill-buffer-hook)
f5a9b180
SM
1491 ;; Delete overlays before erasing the buffer so the overlay hooks
1492 ;; don't get run spuriously when we erase the buffer.
40627755
SM
1493 (let ((ols (overlay-lists)))
1494 (dolist (ol (nconc (car ols) (cdr ols)))
1495 (delete-overlay ol)))
f5a9b180 1496 (erase-buffer)
40627755
SM
1497 buf))))
1498
d543e20b 1499;;;###autoload
3aec85bf 1500(defun custom-buffer-create (options &optional name description)
d543e20b 1501 "Create a buffer containing OPTIONS.
86bd10bc 1502Optional NAME is the name of the buffer.
d543e20b
PA
1503OPTIONS should be an alist of the form ((SYMBOL WIDGET)...), where
1504SYMBOL is a customization option, and WIDGET is a widget for editing
1505that option."
40627755 1506 (pop-to-buffer (custom-get-fresh-buffer (or name "*Customization*")))
3aec85bf 1507 (custom-buffer-create-internal options description))
bd042c03 1508
6d528fc5 1509;;;###autoload
3aec85bf 1510(defun custom-buffer-create-other-window (options &optional name description)
5f97a49d
RS
1511 "Create a buffer containing OPTIONS, and display it in another window.
1512The result includes selecting that window.
86bd10bc 1513Optional NAME is the name of the buffer.
bd042c03
PA
1514OPTIONS should be an alist of the form ((SYMBOL WIDGET)...), where
1515SYMBOL is a customization option, and WIDGET is a widget for editing
1516that option."
86bd10bc 1517 (unless name (setq name "*Customization*"))
5f97a49d 1518 (let ((pop-up-windows t)
b4854a23
KH
1519 (same-window-buffer-names nil)
1520 (same-window-regexps nil))
40627755 1521 (pop-to-buffer (custom-get-fresh-buffer name))
5f97a49d 1522 (custom-buffer-create-internal options description)))
9097aeb7
PA
1523
1524(defcustom custom-reset-button-menu nil
1525 "If non-nil, only show a single reset button in customize buffers.
1526This button will have a menu with all three reset operations."
1527 :type 'boolean
6aaedd12 1528 :group 'custom-buffer)
bd042c03 1529
c42c5c7c
KS
1530(defcustom custom-buffer-verbose-help t
1531 "If non-nil, include explanatory text in the customization buffer."
1532 :type 'boolean
1533 :group 'custom-buffer)
1534
d3d4df42 1535(defun Custom-buffer-done (&rest ignore)
97733c3d 1536 "Exit current Custom buffer according to `custom-buffer-done-kill'."
d3d4df42 1537 (interactive)
97733c3d 1538 (quit-window custom-buffer-done-kill))
d3d4df42 1539
87911bdb
CY
1540(defvar custom-button nil
1541 "Face used for buttons in customization buffers.")
1542
85a5eb0e
CY
1543(defvar custom-button-mouse nil
1544 "Mouse face used for buttons in customization buffers.")
1545
87911bdb
CY
1546(defvar custom-button-pressed nil
1547 "Face used for pressed buttons in customization buffers.")
1548
647f9993
CY
1549(defcustom custom-search-field t
1550 "If non-nil, show a search field in Custom buffers."
1551 :type 'boolean
1552 :version "24.1"
1553 :group 'custom-buffer)
1554
d3d4df42
DL
1555(defcustom custom-raised-buttons (not (equal (face-valid-attribute-values :box)
1556 '(("unspecified" . unspecified))))
1557 "If non-nil, indicate active buttons in a `raised-button' style.
1558Otherwise use brackets."
1559 :type 'boolean
1560 :version "21.1"
87911bdb
CY
1561 :group 'custom-buffer
1562 :set (lambda (variable value)
1563 (custom-set-default variable value)
1564 (setq custom-button
1565 (if value 'custom-button 'custom-button-unraised))
85a5eb0e
CY
1566 (setq custom-button-mouse
1567 (if value 'custom-button-mouse 'highlight))
87911bdb
CY
1568 (setq custom-button-pressed
1569 (if value
1570 'custom-button-pressed
1571 'custom-button-pressed-unraised))))
d3d4df42 1572
3aec85bf 1573(defun custom-buffer-create-internal (options &optional description)
6aec3b9d 1574 (Custom-mode)
9db1942d
CY
1575 (let ((init-file (or custom-file user-init-file)))
1576 ;; Insert verbose help at the top of the custom buffer.
1577 (when custom-buffer-verbose-help
647f9993
CY
1578 (widget-insert (if init-file
1579 "To apply changes, use the Save or Set buttons."
1580 "Custom settings cannot be saved; maybe you started Emacs with `-q'.")
9db1942d
CY
1581 "\nFor details, see ")
1582 (widget-create 'custom-manual
1583 :tag "Saving Customizations"
1584 "(emacs)Saving Customizations")
1585 (widget-insert " in the ")
1586 (widget-create 'custom-manual
1587 :tag "Emacs manual"
1588 :help-echo "Read the Emacs manual."
1589 "(emacs)Top")
1590 (widget-insert "."))
9db1942d 1591 (widget-insert "\n")
647f9993
CY
1592
1593 ;; Insert the search field.
1594 (when custom-search-field
1595 (widget-insert "\n")
1596 (let* ((echo "Search for custom items")
1597 (search-widget
1598 (widget-create
1599 'editable-field
1600 :size 40 :help-echo echo
1601 :action `(lambda (widget &optional event)
d8c8743b 1602 (customize-apropos (split-string (widget-value widget)))))))
647f9993
CY
1603 (widget-insert " ")
1604 (widget-create-child-and-convert
1605 search-widget 'push-button
587faadd 1606 :tag " Search "
647f9993
CY
1607 :help-echo echo :action
1608 (lambda (widget &optional event)
1609 (customize-apropos (widget-value (widget-get widget :parent)))))
1610 (widget-insert "\n")))
1611
f6542587
GM
1612 ;; The custom command buttons are also in the toolbar, so for a
1613 ;; time they were not inserted in the buffer if the toolbar was in use.
1614 ;; But it can be a little confusing for the buffer layout to
1615 ;; change according to whether or nor the toolbar is on, not to
1616 ;; mention that a custom buffer can in theory be created in a
1617 ;; frame with a toolbar, then later viewed in one without.
1618 ;; So now the buttons are always inserted in the buffer. (Bug#1326)
f6542587 1619 (if custom-buffer-verbose-help
647f9993
CY
1620 (widget-insert "
1621 Operate on all settings in this buffer:\n"))
f904c0f9 1622 (let ((button (lambda (tag action active help icon label)
f6542587
GM
1623 (widget-insert " ")
1624 (if (eval active)
1625 (widget-create 'push-button :tag tag
1626 :help-echo help :action action))))
1627 (commands custom-commands))
1628 (apply button (pop commands)) ; Set for current session
1629 (apply button (pop commands)) ; Save for future sessions
1630 (if custom-reset-button-menu
1631 (progn
1632 (widget-insert " ")
1633 (widget-create 'push-button
1634 :tag "Reset buffer"
1635 :help-echo "Show a menu with reset operations."
1636 :mouse-down-action 'ignore
1637 :action 'custom-reset))
1638 (widget-insert "\n")
1639 (apply button (pop commands)) ; Undo edits
1640 (apply button (pop commands)) ; Reset to saved
1641 (apply button (pop commands)) ; Erase customization
1642 (widget-insert " ")
1643 (pop commands) ; Help (omitted)
1644 (apply button (pop commands)))) ; Exit
9db1942d
CY
1645 (widget-insert "\n\n"))
1646
1647 ;; Now populate the custom buffer.
25ac13b5 1648 (message "Creating customization items...")
fadbdfea 1649 (buffer-disable-undo)
d3d4df42 1650 (setq custom-options
d543e20b
PA
1651 (if (= (length options) 1)
1652 (mapcar (lambda (entry)
1653 (widget-create (nth 1 entry)
c32de15e 1654 :documentation-shown t
d543e20b
PA
1655 :custom-state 'unknown
1656 :tag (custom-unlispify-tag-name
1657 (nth 0 entry))
1658 :value (nth 0 entry)))
1659 options)
1660 (let ((count 0)
1661 (length (length options)))
1662 (mapcar (lambda (entry)
6b292312
DL
1663 (prog2
1664 (message "Creating customization items ...%2d%%"
1665 (/ (* 100.0 count) length))
1666 (widget-create (nth 1 entry)
d543e20b
PA
1667 :tag (custom-unlispify-tag-name
1668 (nth 0 entry))
1669 :value (nth 0 entry))
6b292312
DL
1670 (setq count (1+ count))
1671 (unless (eq (preceding-char) ?\n)
1672 (widget-insert "\n"))
1673 (widget-insert "\n")))
1674 options))))
d543e20b
PA
1675 (unless (eq (preceding-char) ?\n)
1676 (widget-insert "\n"))
a9bcbf3f 1677 (message "Creating customization items ...done")
1833b7b3 1678 (message "Resetting customization items...")
944c91b6 1679 (unless (eq custom-buffer-style 'tree)
fadbdfea 1680 (mapc 'custom-magic-reset custom-options))
1833b7b3 1681 (message "Resetting customization items...done")
d543e20b
PA
1682 (message "Creating customization setup...")
1683 (widget-setup)
fadbdfea 1684 (buffer-enable-undo)
d543e20b 1685 (goto-char (point-min))
1833b7b3 1686 (message "Creating customization setup...done"))
d543e20b 1687
944c91b6
PA
1688;;; The Tree Browser.
1689
1690;;;###autoload
4ee1cf9f 1691(defun customize-browse (&optional group)
944c91b6 1692 "Create a tree browser for the customize hierarchy."
cda987f4 1693 (interactive)
4ee1cf9f
PA
1694 (unless group
1695 (setq group 'emacs))
1696 (let ((name "*Customize Browser*"))
40627755 1697 (pop-to-buffer (custom-get-fresh-buffer name)))
6aec3b9d 1698 (Custom-mode)
b758ae9a
RS
1699 (widget-insert (format "\
1700%s buttons; type RET or click mouse-1
1701on a button to invoke its action.
1702Invoke [+] to expand a group, and [-] to collapse an expanded group.\n"
1703 (if custom-raised-buttons
1704 "`Raised' text indicates"
1705 "Square brackets indicate")))
1706
1707
4ee1cf9f
PA
1708 (if custom-browse-only-groups
1709 (widget-insert "\
c953515e 1710Invoke the [Group] button below to edit that item in another window.\n\n")
d3d4df42
DL
1711 (widget-insert "Invoke the ")
1712 (widget-create 'item
4ee1cf9f
PA
1713 :format "%t"
1714 :tag "[Group]"
1715 :tag-glyph "folder")
1716 (widget-insert ", ")
d3d4df42 1717 (widget-create 'item
4ee1cf9f
PA
1718 :format "%t"
1719 :tag "[Face]"
1720 :tag-glyph "face")
1721 (widget-insert ", and ")
d3d4df42 1722 (widget-create 'item
4ee1cf9f
PA
1723 :format "%t"
1724 :tag "[Option]"
1725 :tag-glyph "option")
1726 (widget-insert " buttons below to edit that
c953515e 1727item in another window.\n\n"))
4ee1cf9f 1728 (let ((custom-buffer-style 'tree))
d3d4df42 1729 (widget-create 'custom-group
4ee1cf9f
PA
1730 :custom-last t
1731 :custom-state 'unknown
1732 :tag (custom-unlispify-tag-name group)
1733 :value group))
f134b461 1734 (widget-setup)
4ee1cf9f 1735 (goto-char (point-min)))
944c91b6 1736
c953515e 1737(define-widget 'custom-browse-visibility 'item
1edec9cf 1738 "Control visibility of items in the customize tree browser."
da5ec617 1739 :format "%[[%t]%]"
c953515e 1740 :action 'custom-browse-visibility-action)
944c91b6 1741
c953515e 1742(defun custom-browse-visibility-action (widget &rest ignore)
944c91b6
PA
1743 (let ((custom-buffer-style 'tree))
1744 (custom-toggle-parent widget)))
1745
192e44fc 1746(define-widget 'custom-browse-group-tag 'custom-group-link
944c91b6 1747 "Show parent in other window when activated."
cd6c0940 1748 :tag "Group"
da5ec617 1749 :tag-glyph "folder"
c953515e 1750 :action 'custom-browse-group-tag-action)
944c91b6 1751
c953515e 1752(defun custom-browse-group-tag-action (widget &rest ignore)
944c91b6
PA
1753 (let ((parent (widget-get widget :parent)))
1754 (customize-group-other-window (widget-value parent))))
1755
192e44fc 1756(define-widget 'custom-browse-variable-tag 'custom-group-link
944c91b6 1757 "Show parent in other window when activated."
cd6c0940 1758 :tag "Option"
da5ec617 1759 :tag-glyph "option"
c953515e 1760 :action 'custom-browse-variable-tag-action)
944c91b6 1761
c953515e 1762(defun custom-browse-variable-tag-action (widget &rest ignore)
944c91b6
PA
1763 (let ((parent (widget-get widget :parent)))
1764 (customize-variable-other-window (widget-value parent))))
1765
192e44fc 1766(define-widget 'custom-browse-face-tag 'custom-group-link
944c91b6 1767 "Show parent in other window when activated."
cd6c0940 1768 :tag "Face"
da5ec617 1769 :tag-glyph "face"
c953515e 1770 :action 'custom-browse-face-tag-action)
944c91b6 1771
c953515e 1772(defun custom-browse-face-tag-action (widget &rest ignore)
944c91b6
PA
1773 (let ((parent (widget-get widget :parent)))
1774 (customize-face-other-window (widget-value parent))))
1775
c953515e 1776(defconst custom-browse-alist '((" " "space")
da5ec617
PA
1777 (" | " "vertical")
1778 ("-\\ " "top")
1779 (" |-" "middle")
1780 (" `-" "bottom")))
1781
c953515e 1782(defun custom-browse-insert-prefix (prefix)
da5ec617 1783 "Insert PREFIX. On XEmacs convert it to line graphics."
d3d4df42 1784 ;; Fixme: do graphics.
a7f58f4b 1785 (if nil ; (featurep 'xemacs)
d3d4df42 1786 (progn
da5ec617
PA
1787 (insert "*")
1788 (while (not (string-equal prefix ""))
1789 (let ((entry (substring prefix 0 3)))
1790 (setq prefix (substring prefix 3))
1791 (let ((overlay (make-overlay (1- (point)) (point) nil t nil))
c953515e 1792 (name (nth 1 (assoc entry custom-browse-alist))))
da5ec617
PA
1793 (overlay-put overlay 'end-glyph (widget-glyph-find name entry))
1794 (overlay-put overlay 'start-open t)
1795 (overlay-put overlay 'end-open t)))))
1796 (insert prefix)))
1797
d543e20b
PA
1798;;; Modification of Basic Widgets.
1799;;
1800;; We add extra properties to the basic widgets needed here. This is
62a918ca 1801;; fine, as long as we are careful to stay within our own namespace.
d543e20b
PA
1802;;
1803;; We want simple widgets to be displayed by default, but complex
1804;; widgets to be hidden.
1805
1806(widget-put (get 'item 'widget-type) :custom-show t)
1807(widget-put (get 'editable-field 'widget-type)
1808 :custom-show (lambda (widget value)
1809 (let ((pp (pp-to-string value)))
1810 (cond ((string-match "\n" pp)
1811 nil)
1812 ((> (length pp) 40)
1813 nil)
1814 (t t)))))
1815(widget-put (get 'menu-choice 'widget-type) :custom-show t)
1816
1817;;; The `custom-manual' Widget.
1818
1819(define-widget 'custom-manual 'info-link
1820 "Link to the manual entry for this customization option."
1821 :help-echo "Read the manual entry for this option."
f3093f77
CY
1822 :keymap custom-mode-link-map
1823 :follow-link 'mouse-face
73e60f53
CY
1824 :button-face 'custom-link
1825 :mouse-face 'highlight
e1ec62a5 1826 :pressed-face 'highlight
d543e20b
PA
1827 :tag "Manual")
1828
1829;;; The `custom-magic' Widget.
1830
6aaedd12
PA
1831(defgroup custom-magic-faces nil
1832 "Faces used by the magic button."
1833 :group 'custom-faces
1834 :group 'custom-buffer)
1835
d478e69d
MB
1836(defface custom-invalid '((((class color))
1837 (:foreground "yellow1" :background "red1"))
1838 (t
1839 (:weight bold :slant italic :underline t)))
6aaedd12
PA
1840 "Face used when the customize item is invalid."
1841 :group 'custom-magic-faces)
c4f6e489 1842(define-obsolete-face-alias 'custom-invalid-face 'custom-invalid "22.1")
d543e20b 1843
d478e69d
MB
1844(defface custom-rogue '((((class color))
1845 (:foreground "pink" :background "black"))
1846 (t
1847 (:underline t)))
6aaedd12
PA
1848 "Face used when the customize item is not defined for customization."
1849 :group 'custom-magic-faces)
c4f6e489 1850(define-obsolete-face-alias 'custom-rogue-face 'custom-rogue "22.1")
d543e20b 1851
d478e69d
MB
1852(defface custom-modified '((((min-colors 88) (class color))
1853 (:foreground "white" :background "blue1"))
ea81d57e 1854 (((class color))
d478e69d 1855 (:foreground "white" :background "blue"))
2dfa4c57 1856 (t
d478e69d
MB
1857 (:slant italic :bold)))
1858 "Face used when the customize item has been modified."
1859 :group 'custom-magic-faces)
c4f6e489 1860(define-obsolete-face-alias 'custom-modified-face 'custom-modified "22.1")
d478e69d
MB
1861
1862(defface custom-set '((((min-colors 88) (class color))
1863 (:foreground "blue1" :background "white"))
1864 (((class color))
1865 (:foreground "blue" :background "white"))
1866 (t
1867 (:slant italic)))
6aaedd12
PA
1868 "Face used when the customize item has been set."
1869 :group 'custom-magic-faces)
c4f6e489 1870(define-obsolete-face-alias 'custom-set-face 'custom-set "22.1")
d478e69d
MB
1871
1872(defface custom-changed '((((min-colors 88) (class color))
1873 (:foreground "white" :background "blue1"))
1874 (((class color))
1875 (:foreground "white" :background "blue"))
1876 (t
1877 (:slant italic)))
6aaedd12
PA
1878 "Face used when the customize item has been changed."
1879 :group 'custom-magic-faces)
c4f6e489 1880(define-obsolete-face-alias 'custom-changed-face 'custom-changed "22.1")
d543e20b 1881
d820f1fb
CY
1882(defface custom-themed '((((min-colors 88) (class color))
1883 (:foreground "white" :background "blue1"))
1884 (((class color))
1885 (:foreground "white" :background "blue"))
1886 (t
1887 (:slant italic)))
1888 "Face used when the customize item has been set by a theme."
1889 :group 'custom-magic-faces)
1890
d478e69d 1891(defface custom-saved '((t (:underline t)))
6aaedd12
PA
1892 "Face used when the customize item has been saved."
1893 :group 'custom-magic-faces)
c4f6e489 1894(define-obsolete-face-alias 'custom-saved-face 'custom-saved "22.1")
d543e20b 1895
2dfa4c57
RS
1896(defconst custom-magic-alist
1897 '((nil "#" underline "\
167eefc5 1898UNINITIALIZED, you should not see this.")
2dfa4c57 1899 (unknown "?" italic "\
167eefc5 1900UNKNOWN, you should not see this.")
2dfa4c57 1901 (hidden "-" default "\
167eefc5 1902HIDDEN, invoke \"Show\" in the previous line to show." "\
cbc7d892 1903group now hidden, invoke \"Show\", above, to show contents.")
d478e69d 1904 (invalid "x" custom-invalid "\
167eefc5 1905INVALID, the displayed value cannot be set.")
d478e69d 1906 (modified "*" custom-modified "\
167eefc5 1907EDITED, shown value does not take effect until you set or save it." "\
1833b7b3 1908something in this group has been edited but not set.")
d478e69d 1909 (set "+" custom-set "\
167eefc5 1910SET for current session only." "\
1833b7b3 1911something in this group has been set but not saved.")
d478e69d 1912 (changed ":" custom-changed "\
61328d7c 1913CHANGED outside Customize." "\
25ac13b5 1914something in this group has been changed outside customize.")
d478e69d 1915 (saved "!" custom-saved "\
167eefc5 1916SAVED and set." "\
5dd0cad0 1917something in this group has been set and saved.")
d820f1fb
CY
1918 (themed "o" custom-themed "\
1919THEMED." "\
1920visible group members are all at standard values.")
d478e69d 1921 (rogue "@" custom-rogue "\
f63d5a0e 1922NO CUSTOMIZATION DATA; not intended to be customized." "\
25ac13b5 1923something in this group is not prepared for customization.")
2dfa4c57 1924 (standard " " nil "\
167eefc5 1925STANDARD." "\
50137744 1926visible group members are all at standard values."))
d543e20b 1927 "Alist of customize option states.
d3d4df42 1928Each entry is of the form (STATE MAGIC FACE ITEM-DESC [ GROUP-DESC ]), where
d543e20b
PA
1929
1930STATE is one of the following symbols:
1931
1932`nil'
1933 For internal use, should never occur.
1934`unknown'
1935 For internal use, should never occur.
1936`hidden'
d3d4df42 1937 This item is not being displayed.
d543e20b
PA
1938`invalid'
1939 This item is modified, but has an invalid form.
1940`modified'
1941 This item is modified, and has a valid form.
1942`set'
1943 This item has been set but not saved.
1944`changed'
167eefc5 1945 The current value of this item has been changed outside Customize.
d543e20b
PA
1946`saved'
1947 This item is marked for saving.
1948`rogue'
1949 This item has no customization information.
25ac13b5 1950`standard'
5dd0cad0 1951 This item is unchanged from the standard setting.
d543e20b
PA
1952
1953MAGIC is a string used to present that state.
1954
1955FACE is a face used to present the state.
1956
25ac13b5
PA
1957ITEM-DESC is a string describing the state for options.
1958
1959GROUP-DESC is a string describing the state for groups. If this is
1960left out, ITEM-DESC will be used.
1961
9097aeb7
PA
1962The string %c in either description will be replaced with the
1963category of the item. These are `group'. `option', and `face'.
1964
25ac13b5 1965The list should be sorted most significant first.")
d543e20b
PA
1966
1967(defcustom custom-magic-show 'long
3acab5ef 1968 "If non-nil, show textual description of the state.
b62c92bb 1969If `long', show a full-line description, not just one word."
d543e20b 1970 :type '(choice (const :tag "no" nil)
c992338c
AS
1971 (const long)
1972 (other :tag "short" short))
6aaedd12 1973 :group 'custom-buffer)
d543e20b 1974
9097aeb7 1975(defcustom custom-magic-show-hidden '(option face)
b62c92bb
RS
1976 "Control whether the State button is shown for hidden items.
1977The value should be a list with the custom categories where the State
9097aeb7
PA
1978button should be visible. Possible categories are `group', `option',
1979and `face'."
1980 :type '(set (const group) (const option) (const face))
6aaedd12 1981 :group 'custom-buffer)
3acab5ef 1982
25ac13b5 1983(defcustom custom-magic-show-button nil
b62c92bb 1984 "Show a \"magic\" button indicating the state of each customization option."
d543e20b 1985 :type 'boolean
6aaedd12 1986 :group 'custom-buffer)
d543e20b
PA
1987
1988(define-widget 'custom-magic 'default
1989 "Show and manipulate state for a customization option."
1990 :format "%v"
86bd10bc 1991 :action 'widget-parent-action
6d528fc5 1992 :notify 'ignore
d543e20b
PA
1993 :value-get 'ignore
1994 :value-create 'custom-magic-value-create
1995 :value-delete 'widget-children-value-delete)
1996
86bd10bc
PA
1997(defun widget-magic-mouse-down-action (widget &optional event)
1998 ;; Non-nil unless hidden.
d3d4df42 1999 (not (eq (widget-get (widget-get (widget-get widget :parent) :parent)
86bd10bc
PA
2000 :custom-state)
2001 'hidden)))
2002
d543e20b 2003(defun custom-magic-value-create (widget)
2365594b 2004 "Create compact status report for WIDGET."
d543e20b
PA
2005 (let* ((parent (widget-get widget :parent))
2006 (state (widget-get parent :custom-state))
3acab5ef 2007 (hidden (eq state 'hidden))
25ac13b5 2008 (entry (assq state custom-magic-alist))
d543e20b
PA
2009 (magic (nth 1 entry))
2010 (face (nth 2 entry))
9097aeb7
PA
2011 (category (widget-get parent :custom-category))
2012 (text (or (and (eq category 'group)
25ac13b5
PA
2013 (nth 4 entry))
2014 (nth 3 entry)))
f985c5f7 2015 (form (widget-get parent :custom-form))
d543e20b 2016 children)
647f9993
CY
2017 (unless (eq state 'hidden)
2018 (while (string-match "\\`\\(.*\\)%c\\(.*\\)\\'" text)
2019 (setq text (concat (match-string 1 text)
2020 (symbol-name category)
2021 (match-string 2 text))))
2022 (when (and custom-magic-show
2023 (or (not hidden)
2024 (memq category custom-magic-show-hidden)))
2025 (insert " ")
2026 (when (and (eq category 'group)
2027 (not (and (eq custom-buffer-style 'links)
2028 (> (widget-get parent :custom-level) 1))))
2029 (insert-char ?\ (* custom-buffer-indent
2030 (widget-get parent :custom-level))))
2031 (push (widget-create-child-and-convert
2032 widget 'choice-item
2033 :help-echo "Change the state of this item."
2034 :format (if hidden "%t" "%[%t%]")
2035 :button-prefix 'widget-push-button-prefix
2036 :button-suffix 'widget-push-button-suffix
2037 :mouse-down-action 'widget-magic-mouse-down-action
587faadd 2038 :tag " State ")
647f9993
CY
2039 children)
2040 (insert ": ")
2041 (let ((start (point)))
2042 (if (eq custom-magic-show 'long)
2043 (insert text)
2044 (insert (symbol-name state)))
2045 (cond ((eq form 'lisp)
2046 (insert " (lisp)"))
2047 ((eq form 'mismatch)
2048 (insert " (mismatch)")))
2049 (put-text-property start (point) 'face 'custom-state))
2050 (insert "\n"))
26c7b3ef
RS
2051 (when (and (eq category 'group)
2052 (not (and (eq custom-buffer-style 'links)
2053 (> (widget-get parent :custom-level) 1))))
944c91b6
PA
2054 (insert-char ?\ (* custom-buffer-indent
2055 (widget-get parent :custom-level))))
647f9993
CY
2056 (when custom-magic-show-button
2057 (when custom-magic-show
2058 (let ((indent (widget-get parent :indent)))
2059 (when indent
2060 (insert-char ? indent))))
2061 (push (widget-create-child-and-convert
2062 widget 'choice-item
2063 :mouse-down-action 'widget-magic-mouse-down-action
2064 :button-face face
2065 :button-prefix ""
2066 :button-suffix ""
2067 :help-echo "Change the state."
2068 :format (if hidden "%t" "%[%t%]")
2069 :tag (if (memq form '(lisp mismatch))
2070 (concat "(" magic ")")
2071 (concat "[" magic "]")))
2072 children)
2073 (insert " "))
2074 (widget-put widget :children children))))
d543e20b
PA
2075
2076(defun custom-magic-reset (widget)
2077 "Redraw the :custom-magic property of WIDGET."
2078 (let ((magic (widget-get widget :custom-magic)))
647bc502
CY
2079 (when magic
2080 (widget-value-set magic (widget-value magic)))))
d543e20b 2081
d543e20b
PA
2082;;; The `custom' Widget.
2083
d478e69d 2084(defface custom-button
9e2a2647 2085 '((((type x w32 ns) (class color)) ; Like default modeline
1a578e9b
AC
2086 (:box (:line-width 2 :style released-button)
2087 :background "lightgrey" :foreground "black"))
d3d4df42
DL
2088 (t
2089 nil))
87911bdb 2090 "Face for custom buffer buttons if `custom-raised-buttons' is non-nil."
d3d4df42
DL
2091 :version "21.1"
2092 :group 'custom-faces)
c4f6e489 2093(define-obsolete-face-alias 'custom-button-face 'custom-button "22.1")
d3d4df42 2094
85a5eb0e 2095(defface custom-button-mouse
9e2a2647 2096 '((((type x w32 ns) (class color))
85a5eb0e
CY
2097 (:box (:line-width 2 :style released-button)
2098 :background "grey90" :foreground "black"))
2099 (t
746abd4d
EZ
2100 ;; This is for text terminals that support mouse, like GPM mouse
2101 ;; or the MS-DOS terminal: inverse-video makes the button stand
2102 ;; out on mouse-over.
19758b95 2103 (:inverse-video t)))
85a5eb0e
CY
2104 "Mouse face for custom buffer buttons if `custom-raised-buttons' is non-nil."
2105 :version "22.1"
2106 :group 'custom-faces)
2107
87911bdb 2108(defface custom-button-unraised
192e44fc 2109 '((t :inherit underline))
87911bdb
CY
2110 "Face for custom buffer buttons if `custom-raised-buttons' is nil."
2111 :version "22.1"
2112 :group 'custom-faces)
2113
2114(setq custom-button
2115 (if custom-raised-buttons 'custom-button 'custom-button-unraised))
2116
85a5eb0e
CY
2117(setq custom-button-mouse
2118 (if custom-raised-buttons 'custom-button-mouse 'highlight))
2119
d478e69d 2120(defface custom-button-pressed
9e2a2647 2121 '((((type x w32 ns) (class color))
1a578e9b
AC
2122 (:box (:line-width 2 :style pressed-button)
2123 :background "lightgrey" :foreground "black"))
d3d4df42
DL
2124 (t
2125 (:inverse-video t)))
87911bdb 2126 "Face for pressed custom buttons if `custom-raised-buttons' is non-nil."
d3d4df42 2127 :version "21.1"
b62c92bb 2128 :group 'custom-faces)
c4f6e489
GM
2129(define-obsolete-face-alias 'custom-button-pressed-face
2130 'custom-button-pressed "22.1")
b62c92bb 2131
87911bdb
CY
2132(defface custom-button-pressed-unraised
2133 '((default :inherit custom-button-unraised)
2134 (((class color) (background light)) :foreground "magenta4")
2135 (((class color) (background dark)) :foreground "violet"))
2136 "Face for pressed custom buttons if `custom-raised-buttons' is nil."
2137 :version "22.1"
2138 :group 'custom-faces)
2139
2140(setq custom-button-pressed
2141 (if custom-raised-buttons
2142 'custom-button-pressed
2143 'custom-button-pressed-unraised))
2144
dbc1f997 2145(defface custom-documentation '((t nil))
b62c92bb
RS
2146 "Face used for documentation strings in customization buffers."
2147 :group 'custom-faces)
c4f6e489
GM
2148(define-obsolete-face-alias 'custom-documentation-face
2149 'custom-documentation "22.1")
d478e69d
MB
2150
2151(defface custom-state '((((class color)
2152 (background dark))
2153 (:foreground "lime green"))
2154 (((class color)
2155 (background light))
2156 (:foreground "dark green"))
2157 (t nil))
b62c92bb
RS
2158 "Face used for State descriptions in the customize buffer."
2159 :group 'custom-faces)
c4f6e489 2160(define-obsolete-face-alias 'custom-state-face 'custom-state "22.1")
b62c92bb 2161
73e60f53 2162(defface custom-link
192e44fc
JL
2163 '((t :inherit link))
2164 "Face for links in customization buffers."
2165 :version "22.1"
2166 :group 'custom-faces)
73e60f53 2167
d543e20b
PA
2168(define-widget 'custom 'default
2169 "Customize a user option."
944c91b6 2170 :format "%v"
d543e20b 2171 :convert-widget 'custom-convert-widget
d543e20b 2172 :notify 'custom-notify
944c91b6 2173 :custom-prefix ""
d543e20b
PA
2174 :custom-level 1
2175 :custom-state 'hidden
2176 :documentation-property 'widget-subclass-responsibility
2177 :value-create 'widget-subclass-responsibility
2178 :value-delete 'widget-children-value-delete
86bd10bc
PA
2179 :value-get 'widget-value-value-get
2180 :validate 'widget-children-validate
d543e20b
PA
2181 :match (lambda (widget value) (symbolp value)))
2182
2183(defun custom-convert-widget (widget)
2365594b 2184 "Initialize :value and :tag from :args in WIDGET."
d543e20b 2185 (let ((args (widget-get widget :args)))
d3d4df42 2186 (when args
d543e20b
PA
2187 (widget-put widget :value (widget-apply widget
2188 :value-to-internal (car args)))
2189 (widget-put widget :tag (custom-unlispify-tag-name (car args)))
2190 (widget-put widget :args nil)))
2191 widget)
2192
d543e20b
PA
2193(defun custom-notify (widget &rest args)
2194 "Keep track of changes."
0a3a0b56
PA
2195 (let ((state (widget-get widget :custom-state)))
2196 (unless (eq state 'modified)
2197 (unless (memq state '(nil unknown hidden))
2198 (widget-put widget :custom-state 'modified))
2199 (custom-magic-reset widget)
2200 (apply 'widget-default-notify widget args))))
d543e20b
PA
2201
2202(defun custom-redraw (widget)
2203 "Redraw WIDGET with current settings."
6d528fc5
PA
2204 (let ((line (count-lines (point-min) (point)))
2205 (column (current-column))
2206 (pos (point))
d543e20b
PA
2207 (from (marker-position (widget-get widget :from)))
2208 (to (marker-position (widget-get widget :to))))
2209 (save-excursion
2210 (widget-value-set widget (widget-value widget))
2211 (custom-redraw-magic widget))
2212 (when (and (>= pos from) (<= pos to))
6d528fc5 2213 (condition-case nil
d3d4df42 2214 (progn
e6ce8c42
GM
2215 (goto-char (point-min))
2216 (forward-line (if (> column 0)
2217 (1- line)
2218 line))
6d528fc5
PA
2219 (move-to-column column))
2220 (error nil)))))
d543e20b
PA
2221
2222(defun custom-redraw-magic (widget)
2223 "Redraw WIDGET state with current settings."
d3d4df42 2224 (while widget
d543e20b 2225 (let ((magic (widget-get widget :custom-magic)))
d3d4df42 2226 (cond (magic
944c91b6
PA
2227 (widget-value-set magic (widget-value magic))
2228 (when (setq widget (widget-get widget :group))
2229 (custom-group-state-update widget)))
2230 (t
2231 (setq widget nil)))))
d543e20b
PA
2232 (widget-setup))
2233
2234(defun custom-show (widget value)
2235 "Non-nil if WIDGET should be shown with VALUE by default."
2236 (let ((show (widget-get widget :custom-show)))
647f9993
CY
2237 (if (functionp show)
2238 (funcall show widget value)
2239 show)))
d543e20b 2240
d543e20b
PA
2241(defun custom-load-widget (widget)
2242 "Load all dependencies for WIDGET."
2243 (custom-load-symbol (widget-value widget)))
2244
c953515e 2245(defun custom-unloaded-symbol-p (symbol)
3127aa13 2246 "Return non-nil if the dependencies of SYMBOL have not yet been loaded."
c953515e
PA
2247 (let ((found nil)
2248 (loads (get symbol 'custom-loads))
2249 load)
2250 (while loads
2251 (setq load (car loads)
2252 loads (cdr loads))
2253 (cond ((symbolp load)
2254 (unless (featurep load)
2255 (setq found t)))
2256 ((assoc load load-history))
2257 ((assoc (locate-library load) load-history)
2258 (message nil))
2259 (t
2260 (setq found t))))
2261 found))
2262
2263(defun custom-unloaded-widget-p (widget)
3127aa13 2264 "Return non-nil if the dependencies of WIDGET have not yet been loaded."
c953515e
PA
2265 (custom-unloaded-symbol-p (widget-value widget)))
2266
6d528fc5
PA
2267(defun custom-toggle-hide (widget)
2268 "Toggle visibility of WIDGET."
c953515e 2269 (custom-load-widget widget)
6d528fc5 2270 (let ((state (widget-get widget :custom-state)))
fdbdd068
CY
2271 (cond ((memq state '(invalid modified set))
2272 (error "There are unsaved changes"))
6d528fc5
PA
2273 ((eq state 'hidden)
2274 (widget-put widget :custom-state 'unknown))
d3d4df42 2275 (t
3acab5ef 2276 (widget-put widget :documentation-shown nil)
6d528fc5 2277 (widget-put widget :custom-state 'hidden)))
8697863a
PA
2278 (custom-redraw widget)
2279 (widget-setup)))
6d528fc5 2280
3acab5ef 2281(defun custom-toggle-parent (widget &rest ignore)
b62c92bb 2282 "Toggle visibility of parent of WIDGET."
3acab5ef
PA
2283 (custom-toggle-hide (widget-get widget :parent)))
2284
944c91b6
PA
2285(defun custom-add-see-also (widget &optional prefix)
2286 "Add `See also ...' to WIDGET if there are any links.
2287Insert PREFIX first if non-nil."
2288 (let* ((symbol (widget-get widget :value))
2289 (links (get symbol 'custom-links))
2290 (many (> (length links) 2))
2291 (buttons (widget-get widget :buttons))
2292 (indent (widget-get widget :indent)))
2293 (when links
2294 (when indent
2295 (insert-char ?\ indent))
2296 (when prefix
2297 (insert prefix))
2298 (insert "See also ")
2299 (while links
83dc149d
EZ
2300 (push (widget-create-child-and-convert
2301 widget (car links)
2302 :button-face 'custom-link
e1ec62a5
CY
2303 :mouse-face 'highlight
2304 :pressed-face 'highlight)
944c91b6
PA
2305 buttons)
2306 (setq links (cdr links))
2307 (cond ((null links)
2308 (insert ".\n"))
2309 ((null (cdr links))
2310 (if many
2311 (insert ", and ")
2312 (insert " and ")))
d3d4df42 2313 (t
944c91b6
PA
2314 (insert ", "))))
2315 (widget-put widget :buttons buttons))))
2316
647f9993 2317(defun custom-add-parent-links (widget &optional initial-string doc-initial-string)
cd6c0940 2318 "Add \"Parent groups: ...\" to WIDGET if the group has parents.
08adfdbb 2319The value is non-nil if any parents were found.
cd6c0940 2320If INITIAL-STRING is non-nil, use that rather than \"Parent groups:\"."
944c91b6
PA
2321 (let ((name (widget-value widget))
2322 (type (widget-type widget))
2323 (buttons (widget-get widget :buttons))
d377bee9 2324 (start (point))
ac00d71c 2325 (parents nil))
f1201e3a 2326 (insert (or initial-string "Groups:"))
944c91b6 2327 (mapatoms (lambda (symbol)
08adfdbb
LT
2328 (when (member (list name type) (get symbol 'custom-group))
2329 (insert " ")
2330 (push (widget-create-child-and-convert
2331 widget 'custom-group-link
2332 :tag (custom-unlispify-tag-name symbol)
2333 symbol)
2334 buttons)
2335 (setq parents (cons symbol parents)))))
ac00d71c
PA
2336 (if parents
2337 (insert "\n")
d377bee9 2338 (delete-region start (point)))
ac00d71c
PA
2339 (widget-put widget :buttons buttons)
2340 parents))
944c91b6 2341
d3d4df42
DL
2342;;; The `custom-comment' Widget.
2343
2344;; like the editable field
ee5137b3
JPW
2345(defface custom-comment '((((type tty))
2346 :background "yellow3"
2347 :foreground "black")
2348 (((class grayscale color)
d478e69d 2349 (background light))
ee5137b3 2350 :background "gray85")
d478e69d
MB
2351 (((class grayscale color)
2352 (background dark))
ee5137b3 2353 :background "dim gray")
d478e69d 2354 (t
ee5137b3 2355 :slant italic))
6cda144f 2356 "Face used for comments on variables or faces."
d3d4df42
DL
2357 :version "21.1"
2358 :group 'custom-faces)
c4f6e489 2359(define-obsolete-face-alias 'custom-comment-face 'custom-comment "22.1")
d3d4df42
DL
2360
2361;; like font-lock-comment-face
d478e69d 2362(defface custom-comment-tag
d3d4df42
DL
2363 '((((class color) (background dark)) (:foreground "gray80"))
2364 (((class color) (background light)) (:foreground "blue4"))
2365 (((class grayscale) (background light))
b5555381 2366 (:foreground "DimGray" :weight bold :slant italic))
d3d4df42 2367 (((class grayscale) (background dark))
b5555381
RS
2368 (:foreground "LightGray" :weight bold :slant italic))
2369 (t (:weight bold)))
6200ac6c 2370 "Face used for the comment tag on variables or faces."
d3d4df42 2371 :group 'custom-faces)
c4f6e489 2372(define-obsolete-face-alias 'custom-comment-tag-face 'custom-comment-tag "22.1")
d3d4df42
DL
2373
2374(define-widget 'custom-comment 'string
164cfaeb 2375 "User comment."
d3d4df42 2376 :tag "Comment"
164cfaeb 2377 :help-echo "Edit a comment here."
6200ac6c
GM
2378 :sample-face 'custom-comment-tag
2379 :value-face 'custom-comment
164cfaeb
DL
2380 :shown nil
2381 :create 'custom-comment-create)
d3d4df42
DL
2382
2383(defun custom-comment-create (widget)
164cfaeb 2384 (let* ((null-comment (equal "" (widget-value widget))))
6171a945
DL
2385 (if (or (widget-get (widget-get widget :parent) :comment-shown)
2386 (not null-comment))
2387 (widget-default-create widget)
2388 ;; `widget-default-delete' expects markers in these slots --
2389 ;; maybe it shouldn't.
2390 (widget-put widget :from (point-marker))
2391 (widget-put widget :to (point-marker)))))
164cfaeb
DL
2392
2393(defun custom-comment-hide (widget)
2394 (widget-put (widget-get widget :parent) :comment-shown nil))
d3d4df42
DL
2395
2396;; Those functions are for the menu. WIDGET is NOT the comment widget. It's
2397;; the global custom one
2398(defun custom-comment-show (widget)
164cfaeb
DL
2399 (widget-put widget :comment-shown t)
2400 (custom-redraw widget)
2401 (widget-setup))
d3d4df42
DL
2402
2403(defun custom-comment-invisible-p (widget)
164cfaeb
DL
2404 (let ((val (widget-value (widget-get widget :comment-widget))))
2405 (and (equal "" val)
2406 (not (widget-get widget :comment-shown)))))
d3d4df42 2407
d543e20b
PA
2408;;; The `custom-variable' Widget.
2409
d478e69d 2410(defface custom-variable-tag
16b20ed9
GM
2411 `((((class color)
2412 (background dark))
9db1942d 2413 (:foreground "light blue" :weight bold))
ea81d57e
DN
2414 (((min-colors 88) (class color)
2415 (background light))
9db1942d 2416 (:foreground "blue1" :weight bold))
16b20ed9
GM
2417 (((class color)
2418 (background light))
9db1942d 2419 (:foreground "blue" :weight bold))
b5555381 2420 (t (:weight bold)))
d543e20b 2421 "Face used for unpushable variable tags."
bd042c03 2422 :group 'custom-faces)
c4f6e489
GM
2423(define-obsolete-face-alias 'custom-variable-tag-face
2424 'custom-variable-tag "22.1")
d543e20b 2425
d478e69d 2426(defface custom-variable-button '((t (:underline t :weight bold)))
d543e20b 2427 "Face used for pushable variable tags."
bd042c03 2428 :group 'custom-faces)
c4f6e489
GM
2429(define-obsolete-face-alias 'custom-variable-button-face
2430 'custom-variable-button "22.1")
d543e20b 2431
d64478da
KH
2432(defcustom custom-variable-default-form 'edit
2433 "Default form of displaying variable values."
2434 :type '(choice (const edit)
2435 (const lisp))
cd32a7ba
DN
2436 :group 'custom-buffer
2437 :version "20.3")
d64478da 2438
a075569a
LT
2439(defun custom-variable-documentation (variable)
2440 "Return documentation of VARIABLE for use in Custom buffer.
2441Normally just return the docstring. But if VARIABLE automatically
2442becomes buffer local when set, append a message to that effect."
2443 (if (and (local-variable-if-set-p variable)
2444 (or (not (local-variable-p variable))
2445 (with-temp-buffer
2446 (local-variable-if-set-p variable))))
2447 (concat (documentation-property variable 'variable-documentation)
2448 "\n
2449This variable automatically becomes buffer-local when set outside Custom.
2450However, setting it through Custom sets the default value.")
2451 (documentation-property variable 'variable-documentation)))
2452
d543e20b 2453(define-widget 'custom-variable 'custom
647f9993 2454 "A widget for displaying a Custom variable.
61328d7c
CY
2455The following properties have special meanings for this widget:
2456
2457:hidden-states should be a list of widget states for which the
2458 widget's initial contents are to be hidden.
2459
2460:custom-form should be a symbol describing how to display and
2461 edit the variable---either `edit' (using edit widgets),
2462 `lisp' (as a Lisp sexp), or `mismatch' (should not happen);
da16abfc
CY
2463 if nil, use the return value of `custom-variable-default-form'.
2464
2465:shown-value, if non-nil, should be a list whose `car' is the
2466 variable value to display in place of the current value.
2467
647bc502
CY
2468:custom-style describes the widget interface style; nil is the
2469 default style, while `simple' means a simpler interface that
2470 inhibits the magic custom-state widget."
944c91b6 2471 :format "%v"
d543e20b 2472 :help-echo "Set or reset this variable."
a075569a 2473 :documentation-property #'custom-variable-documentation
9097aeb7 2474 :custom-category 'option
d543e20b
PA
2475 :custom-state nil
2476 :custom-menu 'custom-variable-menu-create
61328d7c 2477 :custom-form nil
d543e20b
PA
2478 :value-create 'custom-variable-value-create
2479 :action 'custom-variable-action
647f9993 2480 :hidden-states '(standard)
d543e20b 2481 :custom-set 'custom-variable-set
5ae87ed4 2482 :custom-mark-to-save 'custom-variable-mark-to-save
d543e20b
PA
2483 :custom-reset-current 'custom-redraw
2484 :custom-reset-saved 'custom-variable-reset-saved
4f985043 2485 :custom-reset-standard 'custom-variable-reset-standard
5ae87ed4
MR
2486 :custom-mark-to-reset-standard 'custom-variable-mark-to-reset-standard
2487 :custom-standard-value 'custom-variable-standard-value
2488 :custom-state-set-and-redraw 'custom-variable-state-set-and-redraw)
d543e20b 2489
bd042c03
PA
2490(defun custom-variable-type (symbol)
2491 "Return a widget suitable for editing the value of SYMBOL.
d3d4df42 2492If SYMBOL has a `custom-type' property, use that.
f0fc8583
RS
2493Otherwise, try matching SYMBOL against `custom-guess-name-alist' and
2494try matching its doc string against `custom-guess-doc-alist'."
bd042c03 2495 (let* ((type (or (get symbol 'custom-type)
25ac13b5 2496 (and (not (get symbol 'standard-value))
bd042c03
PA
2497 (custom-guess-type symbol))
2498 'sexp))
2499 (options (get symbol 'custom-options))
2500 (tmp (if (listp type)
46fa5a83 2501 (copy-sequence type)
bd042c03
PA
2502 (list type))))
2503 (when options
2504 (widget-put tmp :options options))
2505 tmp))
2506
d543e20b 2507(defun custom-variable-value-create (widget)
164cfaeb 2508 "Here is where you edit the variable's value."
d543e20b 2509 (custom-load-widget widget)
d64478da
KH
2510 (unless (widget-get widget :custom-form)
2511 (widget-put widget :custom-form custom-variable-default-form))
d543e20b
PA
2512 (let* ((buttons (widget-get widget :buttons))
2513 (children (widget-get widget :children))
2514 (form (widget-get widget :custom-form))
d543e20b 2515 (symbol (widget-get widget :value))
d543e20b 2516 (tag (widget-get widget :tag))
bd042c03 2517 (type (custom-variable-type symbol))
d543e20b 2518 (conv (widget-convert type))
6d528fc5 2519 (get (or (get symbol 'custom-get) 'default-value))
944c91b6
PA
2520 (prefix (widget-get widget :custom-prefix))
2521 (last (widget-get widget :custom-last))
29a4c45b 2522 (style (widget-get widget :custom-style))
da16abfc
CY
2523 (value (let ((shown-value (widget-get widget :shown-value)))
2524 (cond (shown-value
2525 (car shown-value))
2526 ((default-boundp symbol)
2527 (funcall get symbol))
2528 (t (widget-get conv :value)))))
647f9993
CY
2529 (state (or (widget-get widget :custom-state)
2530 (if (memq (custom-variable-state symbol value)
2531 (widget-get widget :hidden-states))
2532 'hidden))))
2533
d543e20b 2534 ;; If we don't know the state, see if we need to edit it in lisp form.
647f9993
CY
2535 (unless state
2536 (setq state (if (custom-show type value) 'unknown 'hidden)))
d543e20b
PA
2537 (when (eq state 'unknown)
2538 (unless (widget-apply conv :match value)
f985c5f7 2539 (setq form 'mismatch)))
d543e20b 2540 ;; Now we can create the child widget.
944c91b6 2541 (cond ((eq custom-buffer-style 'tree)
da5ec617 2542 (insert prefix (if last " `--- " " |--- "))
944c91b6 2543 (push (widget-create-child-and-convert
c953515e 2544 widget 'custom-browse-variable-tag)
944c91b6
PA
2545 buttons)
2546 (insert " " tag "\n")
2547 (widget-put widget :buttons buttons))
2548 ((eq state 'hidden)
d543e20b 2549 ;; Indicate hidden value.
d3d4df42 2550 (push (widget-create-child-and-convert
647f9993 2551 widget 'custom-visibility
8697863a 2552 :help-echo "Show the value of this option."
895123f5 2553 :on-glyph "down"
647f9993 2554 :on "Hide"
895123f5 2555 :off-glyph "right"
7f663295 2556 :off "Show Value"
647bc502 2557 :action 'custom-toggle-hide-variable
3acab5ef 2558 nil)
647f9993
CY
2559 buttons)
2560 (insert " ")
2561 (push (widget-create-child-and-convert
2562 widget 'item
2563 :format "%{%t%} "
2564 :sample-face 'custom-variable-tag
2565 :tag tag
2566 :parent widget)
3acab5ef 2567 buttons))
f985c5f7 2568 ((memq form '(lisp mismatch))
d543e20b 2569 ;; In lisp mode edit the saved value when possible.
647f9993
CY
2570 (push (widget-create-child-and-convert
2571 widget 'custom-visibility
2572 :help-echo "Hide the value of this option."
2573 :on "Hide"
2574 :off "Show"
895123f5
CY
2575 :on-glyph "down"
2576 :off-glyph "right"
647bc502 2577 :action 'custom-toggle-hide-variable
647f9993
CY
2578 t)
2579 buttons)
2580 (insert " ")
d543e20b
PA
2581 (let* ((value (cond ((get symbol 'saved-value)
2582 (car (get symbol 'saved-value)))
25ac13b5
PA
2583 ((get symbol 'standard-value)
2584 (car (get symbol 'standard-value)))
d543e20b 2585 ((default-boundp symbol)
6d528fc5 2586 (custom-quote (funcall get symbol)))
d543e20b
PA
2587 (t
2588 (custom-quote (widget-get conv :value))))))
3acab5ef 2589 (insert (symbol-name symbol) ": ")
d3d4df42
DL
2590 (push (widget-create-child-and-convert
2591 widget 'sexp
d543e20b 2592 :button-face 'custom-variable-button-face
3acab5ef 2593 :format "%v"
d543e20b
PA
2594 :tag (symbol-name symbol)
2595 :parent widget
2596 :value value)
2597 children)))
2598 (t
2599 ;; Edit mode.
647f9993
CY
2600 (push (widget-create-child-and-convert
2601 widget 'custom-visibility
2602 :help-echo "Hide or show this option."
2603 :on "Hide"
2604 :off "Show"
895123f5
CY
2605 :on-glyph "down"
2606 :off-glyph "right"
647bc502 2607 :action 'custom-toggle-hide-variable
647f9993
CY
2608 t)
2609 buttons)
2610 (insert " ")
3acab5ef
PA
2611 (let* ((format (widget-get type :format))
2612 tag-format value-format)
2613 (unless (string-match ":" format)
896a6a5d 2614 (error "Bad format"))
3acab5ef
PA
2615 (setq tag-format (substring format 0 (match-end 0)))
2616 (setq value-format (substring format (match-end 0)))
2617 (push (widget-create-child-and-convert
d3d4df42 2618 widget 'item
3acab5ef
PA
2619 :format tag-format
2620 :action 'custom-tag-action
8697863a 2621 :help-echo "Change value of this option."
3acab5ef 2622 :mouse-down-action 'custom-tag-mouse-down-action
ee108e88
GM
2623 :button-face 'custom-variable-button
2624 :sample-face 'custom-variable-tag
3acab5ef
PA
2625 tag)
2626 buttons)
3acab5ef 2627 (push (widget-create-child-and-convert
d3d4df42 2628 widget type
3acab5ef
PA
2629 :format value-format
2630 :value value)
2631 children))))
944c91b6 2632 (unless (eq custom-buffer-style 'tree)
944c91b6
PA
2633 (unless (eq (preceding-char) ?\n)
2634 (widget-insert "\n"))
944c91b6 2635 ;; Create the magic button.
29a4c45b 2636 (unless (eq style 'simple)
da16abfc
CY
2637 (let ((magic (widget-create-child-and-convert
2638 widget 'custom-magic nil)))
2639 (widget-put widget :custom-magic magic)
2640 (push magic buttons)))
944c91b6 2641 (widget-put widget :buttons buttons)
944c91b6 2642 ;; Insert documentation.
9db1942d 2643 (widget-put widget :documentation-indent 3)
29a4c45b
CY
2644 (unless (and (eq style 'simple)
2645 (eq state 'hidden))
2646 (widget-add-documentation-string-button
2647 widget :visibility-widget 'custom-visibility))
d3d4df42
DL
2648
2649 ;; The comment field
2650 (unless (eq state 'hidden)
2651 (let* ((comment (get symbol 'variable-comment))
2652 (comment-widget
2653 (widget-create-child-and-convert
2654 widget 'custom-comment
2655 :parent widget
2656 :value (or comment ""))))
2657 (widget-put widget :comment-widget comment-widget)
2658 ;; Don't push it !!! Custom assumes that the first child is the
2659 ;; value one.
2660 (setq children (append children (list comment-widget)))))
647f9993 2661 ;; Update the rest of the properties.
d3d4df42
DL
2662 (widget-put widget :custom-form form)
2663 (widget-put widget :children children)
2664 ;; Now update the state.
2665 (if (eq state 'hidden)
2666 (widget-put widget :custom-state state)
2667 (custom-variable-state-set widget))
944c91b6
PA
2668 ;; See also.
2669 (unless (eq state 'hidden)
2670 (when (eq (widget-get widget :custom-level) 1)
2671 (custom-add-parent-links widget))
2672 (custom-add-see-also widget)))))
d543e20b 2673
647bc502
CY
2674(defun custom-toggle-hide-variable (visibility-widget &rest ignore)
2675 "Toggle the visibility of a `custom-variable' parent widget.
2676By default, this signals an error if the parent has unsaved
2677changes. If the parent has a `simple' :custom-style property,
2678the present value is saved to its :shown-value property instead."
2679 (let ((widget (widget-get visibility-widget :parent)))
2680 (unless (eq (widget-type widget) 'custom-variable)
2681 (error "Invalid widget type"))
2682 (custom-load-widget widget)
2683 (let ((state (widget-get widget :custom-state)))
2684 (if (eq state 'hidden)
2685 (widget-put widget :custom-state 'unknown)
2686 ;; In normal interface, widget can't be hidden if modified.
2687 (when (memq state '(invalid modified set))
2688 (if (eq (widget-get widget :custom-style) 'simple)
2689 (widget-put widget :shown-value
2690 (list (widget-value
2691 (car-safe
2692 (widget-get widget :children)))))
2693 (error "There are unsaved changes")))
2694 (widget-put widget :documentation-shown nil)
2695 (widget-put widget :custom-state 'hidden))
2696 (custom-redraw widget)
2697 (widget-setup))))
2698
3acab5ef
PA
2699(defun custom-tag-action (widget &rest args)
2700 "Pass :action to first child of WIDGET's parent."
2701 (apply 'widget-apply (car (widget-get (widget-get widget :parent) :children))
2702 :action args))
2703
2704(defun custom-tag-mouse-down-action (widget &rest args)
2705 "Pass :mouse-down-action to first child of WIDGET's parent."
2706 (apply 'widget-apply (car (widget-get (widget-get widget :parent) :children))
2707 :mouse-down-action args))
2708
647f9993
CY
2709(defun custom-variable-state (symbol val)
2710 "Return the state of SYMBOL if its value is VAL.
2711If SYMBOL has a non-nil `custom-get' property, it overrides VAL.
2712Possible return values are `standard', `saved', `set', `themed',
2713`changed', and `rogue'."
2714 (let* ((get (or (get symbol 'custom-get) 'default-value))
d543e20b 2715 (value (if (default-boundp symbol)
6d528fc5 2716 (funcall get symbol)
647f9993 2717 val))
d3d4df42 2718 (comment (get symbol 'variable-comment))
d543e20b 2719 tmp
647f9993
CY
2720 temp)
2721 (cond ((progn (setq tmp (get symbol 'customized-value))
2722 (setq temp
2723 (get symbol 'customized-variable-comment))
2724 (or tmp temp))
2725 (if (condition-case nil
2726 (and (equal value (eval (car tmp)))
2727 (equal comment temp))
2728 (error nil))
2729 'set
2730 'changed))
2731 ((progn (setq tmp (get symbol 'theme-value))
2732 (setq temp (get symbol 'saved-variable-comment))
2733 (or tmp temp))
2734 (if (condition-case nil
2735 (and (equal comment temp)
2736 (equal value
2737 (eval
2738 (car (custom-variable-theme-value
2739 symbol)))))
2740 (error nil))
2741 (cond
2742 ((eq (caar tmp) 'user) 'saved)
2743 ((eq (caar tmp) 'changed)
2744 (if (condition-case nil
2745 (and (null comment)
2746 (equal value
2747 (eval
2748 (car (get symbol 'standard-value)))))
2749 (error nil))
2750 ;; The value was originally set outside
2751 ;; custom, but it was set to the standard
2752 ;; value (probably an autoloaded defcustom).
2753 'standard
2754 'changed))
2755 (t 'themed))
2756 'changed))
2757 ((setq tmp (get symbol 'standard-value))
2758 (if (condition-case nil
2759 (and (equal value (eval (car tmp)))
2760 (equal comment nil))
2761 (error nil))
2762 'standard
2763 'changed))
2764 (t 'rogue))))
2765
2766(defun custom-variable-state-set (widget &optional state)
2767 "Set the state of WIDGET to STATE.
2768If STATE is nil, the value is computed by `custom-variable-state'."
2769 (widget-put widget :custom-state
2770 (or state (custom-variable-state (widget-value widget)
2771 (widget-get widget :value)))))
d543e20b 2772
4f985043
RS
2773(defun custom-variable-standard-value (widget)
2774 (get (widget-value widget) 'standard-value))
2775
d3d4df42 2776(defvar custom-variable-menu
70482877 2777 `(("Set for Current Session" custom-variable-set
0c731046 2778 (lambda (widget)
eaa99205 2779 (eq (widget-get widget :custom-state) 'modified)))
9b5007e7
GM
2780 ;; Note that in all the backquoted code in this file, we test
2781 ;; init-file-user rather than user-init-file. This is in case
2782 ;; cus-edit is loaded by something in site-start.el, because
2783 ;; user-init-file is not set at that stage.
2784 ;; http://lists.gnu.org/archive/html/emacs-devel/2007-10/msg00310.html
2785 ,@(when (or custom-file init-file-user)
70482877 2786 '(("Save for Future Sessions" custom-variable-save
eaa99205
CY
2787 (lambda (widget)
2788 (memq (widget-get widget :custom-state)
2789 '(modified set changed rogue))))))
70482877 2790 ("Undo Edits" custom-redraw
6d528fc5
PA
2791 (lambda (widget)
2792 (and (default-boundp (widget-value widget))
86bd10bc 2793 (memq (widget-get widget :custom-state) '(modified changed)))))
70482877 2794 ("Reset to Saved" custom-variable-reset-saved
6d528fc5 2795 (lambda (widget)
d3d4df42
DL
2796 (and (or (get (widget-value widget) 'saved-value)
2797 (get (widget-value widget) 'saved-variable-comment))
6d528fc5
PA
2798 (memq (widget-get widget :custom-state)
2799 '(modified set changed rogue)))))
9b5007e7 2800 ,@(when (or custom-file init-file-user)
70482877 2801 '(("Erase Customization" custom-variable-reset-standard
73e60f53
CY
2802 (lambda (widget)
2803 (and (get (widget-value widget) 'standard-value)
2804 (memq (widget-get widget :custom-state)
2805 '(modified set changed saved rogue)))))))
70482877
LT
2806 ("Set to Backup Value" custom-variable-reset-backup
2807 (lambda (widget)
2808 (get (widget-value widget) 'backup-value)))
2809 ("---" ignore ignore)
2810 ("Add Comment" custom-comment-show custom-comment-invisible-p)
eaa99205 2811 ("---" ignore ignore)
70482877 2812 ("Show Current Value" custom-variable-edit
73e60f53 2813 (lambda (widget)
eaa99205 2814 (eq (widget-get widget :custom-form) 'lisp)))
70482877 2815 ("Show Saved Lisp Expression" custom-variable-edit-lisp
eaa99205
CY
2816 (lambda (widget)
2817 (eq (widget-get widget :custom-form) 'edit))))
d543e20b 2818 "Alist of actions for the `custom-variable' widget.
6d528fc5
PA
2819Each entry has the form (NAME ACTION FILTER) where NAME is the name of
2820the menu entry, ACTION is the function to call on the widget when the
2821menu is selected, and FILTER is a predicate which takes a `custom-variable'
2822widget as an argument, and returns non-nil if ACTION is valid on that
19d63704 2823widget. If FILTER is nil, ACTION is always valid.")
d543e20b
PA
2824
2825(defun custom-variable-action (widget &optional event)
2826 "Show the menu for `custom-variable' WIDGET.
2827Optional EVENT is the location for the menu."
2828 (if (eq (widget-get widget :custom-state) 'hidden)
6d528fc5 2829 (custom-toggle-hide widget)
86bd10bc
PA
2830 (unless (eq (widget-get widget :custom-state) 'modified)
2831 (custom-variable-state-set widget))
2832 (custom-redraw-magic widget)
d543e20b 2833 (let* ((completion-ignore-case t)
25ac13b5
PA
2834 (answer (widget-choose (concat "Operation on "
2835 (custom-unlispify-tag-name
2836 (widget-get widget :value)))
6d528fc5
PA
2837 (custom-menu-filter custom-variable-menu
2838 widget)
d543e20b
PA
2839 event)))
2840 (if answer
2841 (funcall answer widget)))))
2842
2843(defun custom-variable-edit (widget)
2844 "Edit value of WIDGET."
2845 (widget-put widget :custom-state 'unknown)
2846 (widget-put widget :custom-form 'edit)
2847 (custom-redraw widget))
2848
2849(defun custom-variable-edit-lisp (widget)
2365594b 2850 "Edit the Lisp representation of the value of WIDGET."
d543e20b
PA
2851 (widget-put widget :custom-state 'unknown)
2852 (widget-put widget :custom-form 'lisp)
2853 (custom-redraw widget))
2854
2855(defun custom-variable-set (widget)
2856 "Set the current value for the variable being edited by WIDGET."
6d528fc5
PA
2857 (let* ((form (widget-get widget :custom-form))
2858 (state (widget-get widget :custom-state))
2859 (child (car (widget-get widget :children)))
2860 (symbol (widget-value widget))
2861 (set (or (get symbol 'custom-set) 'set-default))
d3d4df42
DL
2862 (comment-widget (widget-get widget :comment-widget))
2863 (comment (widget-value comment-widget))
2864 val)
d543e20b 2865 (cond ((eq state 'hidden)
896a6a5d 2866 (error "Cannot set hidden variable"))
d543e20b
PA
2867 ((setq val (widget-apply child :validate))
2868 (goto-char (widget-get val :from))
2869 (error "%s" (widget-get val :error)))
f985c5f7 2870 ((memq form '(lisp mismatch))
d3d4df42
DL
2871 (when (equal comment "")
2872 (setq comment nil)
2873 ;; Make the comment invisible by hand if it's empty
164cfaeb 2874 (custom-comment-hide comment-widget))
bde4f354 2875 (custom-variable-backup-value widget)
25c6d447 2876 (custom-push-theme 'theme-value symbol 'user
c5d290fb 2877 'set (custom-quote (widget-value child)))
6d528fc5 2878 (funcall set symbol (eval (setq val (widget-value child))))
d3d4df42
DL
2879 (put symbol 'customized-value (list val))
2880 (put symbol 'variable-comment comment)
2881 (put symbol 'customized-variable-comment comment))
d543e20b 2882 (t
d3d4df42
DL
2883 (when (equal comment "")
2884 (setq comment nil)
2885 ;; Make the comment invisible by hand if it's empty
164cfaeb 2886 (custom-comment-hide comment-widget))
bde4f354 2887 (custom-variable-backup-value widget)
25c6d447 2888 (custom-push-theme 'theme-value symbol 'user
c5d290fb 2889 'set (custom-quote (widget-value child)))
6d528fc5 2890 (funcall set symbol (setq val (widget-value child)))
d3d4df42
DL
2891 (put symbol 'customized-value (list (custom-quote val)))
2892 (put symbol 'variable-comment comment)
2893 (put symbol 'customized-variable-comment comment)))
d543e20b
PA
2894 (custom-variable-state-set widget)
2895 (custom-redraw-magic widget)))
2896
5ae87ed4
MR
2897(defun custom-variable-mark-to-save (widget)
2898 "Set value and mark for saving the variable edited by WIDGET."
6d528fc5
PA
2899 (let* ((form (widget-get widget :custom-form))
2900 (state (widget-get widget :custom-state))
2901 (child (car (widget-get widget :children)))
2902 (symbol (widget-value widget))
2903 (set (or (get symbol 'custom-set) 'set-default))
d3d4df42
DL
2904 (comment-widget (widget-get widget :comment-widget))
2905 (comment (widget-value comment-widget))
6d528fc5 2906 val)
d543e20b 2907 (cond ((eq state 'hidden)
896a6a5d 2908 (error "Cannot set hidden variable"))
d543e20b
PA
2909 ((setq val (widget-apply child :validate))
2910 (goto-char (widget-get val :from))
6b292312 2911 (error "Saving %s: %s" symbol (widget-get val :error)))
f985c5f7 2912 ((memq form '(lisp mismatch))
d3d4df42
DL
2913 (when (equal comment "")
2914 (setq comment nil)
2915 ;; Make the comment invisible by hand if it's empty
164cfaeb 2916 (custom-comment-hide comment-widget))
d543e20b 2917 (put symbol 'saved-value (list (widget-value child)))
c942535f 2918 (custom-push-theme 'theme-value symbol 'user
c5d290fb 2919 'set (custom-quote (widget-value child)))
d3d4df42
DL
2920 (funcall set symbol (eval (widget-value child)))
2921 (put symbol 'variable-comment comment)
2922 (put symbol 'saved-variable-comment comment))
d543e20b 2923 (t
d3d4df42
DL
2924 (when (equal comment "")
2925 (setq comment nil)
2926 ;; Make the comment invisible by hand if it's empty
164cfaeb 2927 (custom-comment-hide comment-widget))
d3d4df42
DL
2928 (put symbol 'saved-value
2929 (list (custom-quote (widget-value child))))
c942535f 2930 (custom-push-theme 'theme-value symbol 'user
c5d290fb 2931 'set (custom-quote (widget-value child)))
d3d4df42
DL
2932 (funcall set symbol (widget-value child))
2933 (put symbol 'variable-comment comment)
2934 (put symbol 'saved-variable-comment comment)))
d543e20b 2935 (put symbol 'customized-value nil)
5ae87ed4
MR
2936 (put symbol 'customized-variable-comment nil)))
2937
2938(defsubst custom-variable-state-set-and-redraw (widget)
2939 "Set state of variable widget WIDGET and redraw with current settings."
2940 (custom-variable-state-set widget)
2941 (custom-redraw-magic widget))
2942
2943(defun custom-variable-save (widget)
2944 "Save value of variable edited by widget WIDGET."
2945 (custom-variable-mark-to-save widget)
2946 (custom-save-all)
2947 (custom-variable-state-set-and-redraw widget))
d543e20b
PA
2948
2949(defun custom-variable-reset-saved (widget)
bde4f354 2950 "Restore the saved value for the variable being edited by WIDGET.
67177975 2951This also updates the buffer to show that value.
bde4f354
RS
2952The value that was current before this operation
2953becomes the backup value, so you can get it again."
6d528fc5 2954 (let* ((symbol (widget-value widget))
d3d4df42 2955 (set (or (get symbol 'custom-set) 'set-default))
d3d4df42
DL
2956 (value (get symbol 'saved-value))
2957 (comment (get symbol 'saved-variable-comment)))
2958 (cond ((or value comment)
2959 (put symbol 'variable-comment comment)
bde4f354 2960 (custom-variable-backup-value widget)
d358aa10 2961 (custom-push-theme 'theme-value symbol 'user 'set (car-safe value))
d3d4df42
DL
2962 (condition-case nil
2963 (funcall set symbol (eval (car value)))
2964 (error nil)))
2965 (t
2966 (error "No saved value for %s" symbol)))
d543e20b 2967 (put symbol 'customized-value nil)
d3d4df42 2968 (put symbol 'customized-variable-comment nil)
d543e20b 2969 (widget-put widget :custom-state 'unknown)
d3d4df42 2970 ;; This call will possibly make the comment invisible
d543e20b
PA
2971 (custom-redraw widget)))
2972
5ae87ed4
MR
2973(defun custom-variable-mark-to-reset-standard (widget)
2974 "Mark to restore standard setting for the variable edited by widget WIDGET.
2975If `custom-reset-standard-variables-list' is nil, save, reset and
2976redraw the widget immediately."
d358aa10 2977 (let* ((symbol (widget-value widget)))
25ac13b5 2978 (if (get symbol 'standard-value)
25c6d447 2979 (custom-variable-backup-value widget)
5dd0cad0 2980 (error "No standard setting known for %S" symbol))
164cfaeb 2981 (put symbol 'variable-comment nil)
d543e20b 2982 (put symbol 'customized-value nil)
d3d4df42 2983 (put symbol 'customized-variable-comment nil)
d358aa10 2984 (custom-push-theme 'theme-value symbol 'user 'reset)
25c6d447 2985 (custom-theme-recalc-variable symbol)
5ae87ed4
MR
2986 (if (and custom-reset-standard-variables-list
2987 (or (get symbol 'saved-value) (get symbol 'saved-variable-comment)))
2988 (progn
2989 (put symbol 'saved-value nil)
2990 (put symbol 'saved-variable-comment nil)
2991 ;; Append this to `custom-reset-standard-variables-list' to
2992 ;; have `custom-reset-standard-save-and-update' save setting
2993 ;; to the file, update the widget's state, and redraw it.
2994 (setq custom-reset-standard-variables-list
2995 (cons widget custom-reset-standard-variables-list)))
2996 (when (or (get symbol 'saved-value) (get symbol 'saved-variable-comment))
2997 (put symbol 'saved-value nil)
2998 (put symbol 'saved-variable-comment nil)
2999 (custom-save-all))
3000 (widget-put widget :custom-state 'unknown)
3001 ;; This call will possibly make the comment invisible
3002 (custom-redraw widget))))
3003
3004(defun custom-variable-reset-standard (widget)
3005 "Restore standard setting for the variable edited by WIDGET.
3006This operation eliminates any saved setting for the variable,
3007restoring it to the state of a variable that has never been customized.
3008The value that was current before this operation
3009becomes the backup value, so you can get it again."
3010 (let (custom-reset-standard-variables-list)
3011 (custom-variable-mark-to-reset-standard widget)))
d543e20b 3012
bde4f354
RS
3013(defun custom-variable-backup-value (widget)
3014 "Back up the current value for WIDGET's variable.
3015The backup value is kept in the car of the `backup-value' property."
3016 (let* ((symbol (widget-value widget))
3017 (get (or (get symbol 'custom-get) 'default-value))
3018 (type (custom-variable-type symbol))
3019 (conv (widget-convert type))
3020 (value (if (default-boundp symbol)
3021 (funcall get symbol)
3022 (widget-get conv :value))))
3023 (put symbol 'backup-value (list value))))
3024
3025(defun custom-variable-reset-backup (widget)
3026 "Restore the backup value for the variable being edited by WIDGET.
3027The value that was current before this operation
3028becomes the backup value, so you can use this operation repeatedly
3029to switch between two values."
3030 (let* ((symbol (widget-value widget))
3031 (set (or (get symbol 'custom-set) 'set-default))
3032 (value (get symbol 'backup-value))
3033 (comment-widget (widget-get widget :comment-widget))
3034 (comment (widget-value comment-widget)))
3035 (if value
3036 (progn
3037 (custom-variable-backup-value widget)
25c6d447 3038 (custom-push-theme 'theme-value symbol 'user 'set value)
bde4f354
RS
3039 (condition-case nil
3040 (funcall set symbol (car value))
3041 (error nil)))
3042 (error "No backup value for %s" symbol))
3043 (put symbol 'customized-value (list (car value)))
3044 (put symbol 'variable-comment comment)
3045 (put symbol 'customized-variable-comment comment)
3046 (custom-variable-state-set widget)
3047 ;; This call will possibly make the comment invisible
3048 (custom-redraw widget)))
3049
12bafdaa
CY
3050;;; The `custom-visibility' Widget
3051
3052(define-widget 'custom-visibility 'visibility
3053 "Show or hide a documentation string."
3054 :button-face 'custom-visibility
3055 :pressed-face 'custom-visibility
3056 :mouse-face 'highlight
647f9993 3057 :pressed-face 'highlight
895123f5
CY
3058 :on-glyph nil
3059 :off-glyph nil)
12bafdaa
CY
3060
3061(defface custom-visibility
3062 '((t :height 0.8 :inherit link))
3063 "Face for the `custom-visibility' widget."
3064 :version "23.1"
3065 :group 'custom-faces)
3066
d543e20b
PA
3067;;; The `custom-face-edit' Widget.
3068
3069(define-widget 'custom-face-edit 'checklist
587faadd
CY
3070 "Widget for editing face attributes.
3071The following properties have special meanings for this widget:
3072
3073:value is a plist of face attributes.
3074
3075:default-face-attributes, if non-nil, is a plist of defaults for
3076face attributes (as specified by a `default' defface entry)."
61328d7c
CY
3077 :format "%v"
3078 :extra-offset 3
d3d4df42 3079 :button-args '(:help-echo "Control whether this attribute has any effect.")
d75fa08f
RS
3080 :value-to-internal 'custom-face-edit-fix-value
3081 :match (lambda (widget value)
ee1f522d 3082 (widget-checklist-match widget
d75fa08f 3083 (custom-face-edit-fix-value widget value)))
61328d7c 3084 :value-create 'custom-face-edit-value-create
3ea051cb 3085 :convert-widget 'custom-face-edit-convert-widget
d543e20b 3086 :args (mapcar (lambda (att)
61328d7c 3087 (list 'group :inline t
d543e20b 3088 :sibling-args (widget-get (nth 1 att) :sibling-args)
d3d4df42 3089 (list 'const :format "" :value (nth 0 att))
d543e20b
PA
3090 (nth 1 att)))
3091 custom-face-attributes))
3092
61328d7c 3093(defun custom-face-edit-value-create (widget)
587faadd
CY
3094 (let* ((alist (widget-checklist-match-find
3095 widget (widget-get widget :value)))
61328d7c
CY
3096 (args (widget-get widget :args))
3097 (show-all (widget-get widget :show-all-attributes))
587faadd
CY
3098 (buttons (widget-get widget :buttons))
3099 (defaults (widget-checklist-match-find
3100 widget
3101 (widget-get widget :default-face-attributes)))
61328d7c
CY
3102 entry)
3103 (unless (looking-back "^ *")
3104 (insert ?\n))
3105 (insert-char ?\s (widget-get widget :extra-offset))
587faadd 3106 (if (or alist defaults show-all)
61328d7c 3107 (dolist (prop args)
587faadd
CY
3108 (setq entry (or (assq prop alist)
3109 (assq prop defaults)))
61328d7c
CY
3110 (if (or entry show-all)
3111 (widget-checklist-add-item widget prop entry)))
3112 (insert (propertize "-- Empty face --" 'face 'shadow) ?\n))
3113 (let ((indent (widget-get widget :indent)))
3114 (if indent (insert-char ?\s (widget-get widget :indent))))
3115 (push (widget-create-child-and-convert
3116 widget 'visibility
3117 :help-echo "Show or hide all face attributes."
3118 :button-face 'custom-visibility
3119 :pressed-face 'custom-visibility
3120 :mouse-face 'highlight
3121 :on "Hide Unused Attributes" :off "Show All Attributes"
895123f5 3122 :on-glyph nil :off-glyph nil
61328d7c
CY
3123 :always-active t
3124 :action 'custom-face-edit-value-visibility-action
3125 show-all)
3126 buttons)
3127 (insert ?\n)
3128 (widget-put widget :buttons buttons)
3129 (widget-put widget :children (nreverse (widget-get widget :children)))))
3130
3131(defun custom-face-edit-value-visibility-action (widget &rest ignore)
3132 ;; Toggle hiding of face attributes.
3133 (let ((parent (widget-get widget :parent)))
3134 (widget-put parent :show-all-attributes
3135 (not (widget-get parent :show-all-attributes)))
3136 (custom-redraw parent)))
3137
d75fa08f 3138(defun custom-face-edit-fix-value (widget value)
0bbe869a
AS
3139 "Ignoring WIDGET, convert :bold and :italic in VALUE to new form.
3140Also change :reverse-video to :inverse-video."
05d22d02 3141 (custom-fix-face-spec value))
d75fa08f 3142
3ea051cb
MB
3143(defun custom-face-edit-convert-widget (widget)
3144 "Convert :args as widget types in WIDGET."
3145 (widget-put
3146 widget
3147 :args (mapcar (lambda (arg)
3148 (widget-convert arg
3149 :deactivate 'custom-face-edit-deactivate
3150 :activate 'custom-face-edit-activate
3151 :delete 'custom-face-edit-delete))
3152 (widget-get widget :args)))
3153 widget)
3154
587faadd
CY
3155(defconst custom-face-edit (widget-convert 'custom-face-edit)
3156 "Converted version of the `custom-face-edit' widget.")
3157
3ea051cb
MB
3158(defun custom-face-edit-deactivate (widget)
3159 "Make face widget WIDGET inactive for user modifications."
3160 (unless (widget-get widget :inactive)
3161 (let ((tag (custom-face-edit-attribute-tag widget))
3162 (from (copy-marker (widget-get widget :from)))
3ea051cb
MB
3163 (value (widget-value widget))
3164 (inhibit-read-only t)
3165 (inhibit-modification-hooks t))
3166 (save-excursion
3167 (goto-char from)
3168 (widget-default-delete widget)
61328d7c 3169 (insert tag ": " (propertize "--" 'face 'shadow) "\n")
3ea051cb
MB
3170 (widget-put widget :inactive
3171 (cons value (cons from (- (point) from))))))))
3172
3173(defun custom-face-edit-activate (widget)
47fdaab8 3174 "Make face widget WIDGET active for user modifications."
3ea051cb
MB
3175 (let ((inactive (widget-get widget :inactive))
3176 (inhibit-read-only t)
3177 (inhibit-modification-hooks t))
3178 (when (consp inactive)
3179 (save-excursion
3180 (goto-char (car (cdr inactive)))
3181 (delete-region (point) (+ (point) (cdr (cdr inactive))))
3182 (widget-put widget :inactive nil)
3183 (widget-apply widget :create)
3184 (widget-value-set widget (car inactive))
3185 (widget-setup)))))
3186
3187(defun custom-face-edit-delete (widget)
3127aa13 3188 "Remove WIDGET from the buffer."
3ea051cb
MB
3189 (let ((inactive (widget-get widget :inactive))
3190 (inhibit-read-only t)
3191 (inhibit-modification-hooks t))
3192 (if (not inactive)
3193 ;; Widget is alive, we don't have to do anything special
3194 (widget-default-delete widget)
3195 ;; WIDGET is already deleted because we did so to inactivate it;
3196 ;; now just get rid of the label we put in its place.
3197 (delete-region (car (cdr inactive))
3198 (+ (car (cdr inactive)) (cdr (cdr inactive))))
3199 (widget-put widget :inactive nil))))
ee1f522d 3200
3ea051cb
MB
3201
3202(defun custom-face-edit-attribute-tag (widget)
6cda144f 3203 "Return the first :tag property in WIDGET or one of its children."
3ea051cb
MB
3204 (let ((tag (widget-get widget :tag)))
3205 (or (and (not (equal tag "")) tag)
3206 (let ((children (widget-get widget :children)))
3207 (while (and (null tag) children)
3208 (setq tag (custom-face-edit-attribute-tag (pop children))))
3209 tag))))
3210
d543e20b
PA
3211;;; The `custom-display' Widget.
3212
3213(define-widget 'custom-display 'menu-choice
3214 "Select a display type."
3215 :tag "Display"
3216 :value t
3217 :help-echo "Specify frames where the face attributes should be used."
3218 :args '((const :tag "all" t)
b9afd6dd 3219 (const :tag "defaults" default)
d543e20b
PA
3220 (checklist
3221 :offset 0
3222 :extra-offset 9
3223 :args ((group :sibling-args (:help-echo "\
3224Only match the specified window systems.")
3225 (const :format "Type: "
3226 type)
3227 (checklist :inline t
3228 :offset 0
3229 (const :format "X "
3230 :sibling-args (:help-echo "\
3231The X11 Window System.")
3232 x)
3233 (const :format "PM "
3234 :sibling-args (:help-echo "\
3235OS/2 Presentation Manager.")
3236 pm)
b97aca27 3237 (const :format "W32 "
d543e20b 3238 :sibling-args (:help-echo "\
b97aca27
GV
3239Windows NT/9X.")
3240 w32)
edfda783
AR
3241 (const :format "NS "
3242 :sibling-args (:help-echo "\
3243GNUstep or Macintosh OS Cocoa interface.")
3244 ns)
d543e20b
PA
3245 (const :format "DOS "
3246 :sibling-args (:help-echo "\
3247Plain MS-DOS.")
3248 pc)
3249 (const :format "TTY%n"
3250 :sibling-args (:help-echo "\
3251Plain text terminals.")
3252 tty)))
3253 (group :sibling-args (:help-echo "\
3254Only match the frames with the specified color support.")
3255 (const :format "Class: "
3256 class)
3257 (checklist :inline t
3258 :offset 0
3259 (const :format "Color "
3260 :sibling-args (:help-echo "\
3261Match color frames.")
3262 color)
3263 (const :format "Grayscale "
3264 :sibling-args (:help-echo "\
3265Match grayscale frames.")
3266 grayscale)
3267 (const :format "Monochrome%n"
3268 :sibling-args (:help-echo "\
3269Match frames with no color support.")
3270 mono)))
3271 (group :sibling-args (:help-echo "\
c4d9734c
JL
3272The minimum number of colors the frame should support.")
3273 (const :format "" min-colors)
3274 (integer :tag "Minimum number of colors" ))
3275 (group :sibling-args (:help-echo "\
d543e20b
PA
3276Only match frames with the specified intensity.")
3277 (const :format "\
3278Background brightness: "
3279 background)
3280 (checklist :inline t
3281 :offset 0
3282 (const :format "Light "
3283 :sibling-args (:help-echo "\
3284Match frames with light backgrounds.")
3285 light)
3286 (const :format "Dark\n"
3287 :sibling-args (:help-echo "\
3288Match frames with dark backgrounds.")
2b32d1a7
MB
3289 dark)))
3290 (group :sibling-args (:help-echo "\
3291Only match frames that support the specified face attributes.")
3292 (const :format "Supports attributes:" supports)
4abe16b5 3293 (custom-face-edit :inline t :format "%n%v"))))))
d543e20b
PA
3294
3295;;; The `custom-face' Widget.
3296
d478e69d 3297(defface custom-face-tag
01b38d61 3298 `((t :inherit custom-variable-tag))
d543e20b 3299 "Face used for face tags."
bd042c03 3300 :group 'custom-faces)
c4f6e489 3301(define-obsolete-face-alias 'custom-face-tag-face 'custom-face-tag "22.1")
d543e20b 3302
d64478da
KH
3303(defcustom custom-face-default-form 'selected
3304 "Default form of displaying face definition."
3305 :type '(choice (const all)
3306 (const selected)
3307 (const lisp))
cd32a7ba
DN
3308 :group 'custom-buffer
3309 :version "20.3")
d64478da 3310
d543e20b 3311(define-widget 'custom-face 'custom
61328d7c 3312 "Widget for customizing a face.
61328d7c
CY
3313The following properties have special meanings for this widget:
3314
587faadd
CY
3315:value is the face name (a symbol).
3316
61328d7c
CY
3317:custom-form should be a symbol describing how to display and
3318 edit the face attributes---either `selected' (attributes for
3319 selected display only), `all' (all attributes), `lisp' (as a
3320 Lisp sexp), or `mismatch' (should not happen); if nil, use
587faadd
CY
3321 the return value of `custom-face-default-form'.
3322
647bc502
CY
3323:custom-style describes the widget interface style; nil is the
3324 default style, while `simple' means a simpler interface that
3325 inhibits the magic custom-state widget.
da16abfc
CY
3326
3327:sample-indent, if non-nil, is the number of columns to which to
3328 indent the face sample (an integer).
587faadd 3329
da16abfc 3330:shown-value, if non-nil, is the face spec to display as the value
647bc502 3331 of the widget, instead of the current face spec."
ee108e88 3332 :sample-face 'custom-face-tag
d543e20b 3333 :help-echo "Set or reset this face."
23c0fb21 3334 :documentation-property #'face-doc-string
d543e20b
PA
3335 :value-create 'custom-face-value-create
3336 :action 'custom-face-action
9097aeb7 3337 :custom-category 'face
61328d7c 3338 :custom-form nil
d543e20b 3339 :custom-set 'custom-face-set
5ae87ed4 3340 :custom-mark-to-save 'custom-face-mark-to-save
d543e20b
PA
3341 :custom-reset-current 'custom-redraw
3342 :custom-reset-saved 'custom-face-reset-saved
25ac13b5 3343 :custom-reset-standard 'custom-face-reset-standard
5ae87ed4 3344 :custom-mark-to-reset-standard 'custom-face-mark-to-reset-standard
4f985043 3345 :custom-standard-value 'custom-face-standard-value
5ae87ed4 3346 :custom-state-set-and-redraw 'custom-face-state-set-and-redraw
d543e20b
PA
3347 :custom-menu 'custom-face-menu-create)
3348
d3d4df42 3349(define-widget 'custom-face-all 'editable-list
d543e20b
PA
3350 "An editable list of display specifications and attributes."
3351 :entry-format "%i %d %v"
3352 :insert-button-args '(:help-echo "Insert new display specification here.")
3353 :append-button-args '(:help-echo "Append new display specification here.")
3354 :delete-button-args '(:help-echo "Delete this display specification.")
3355 :args '((group :format "%v" custom-display custom-face-edit)))
3356
3357(defconst custom-face-all (widget-convert 'custom-face-all)
3358 "Converted version of the `custom-face-all' widget.")
3359
3ea051cb 3360(defun custom-filter-face-spec (spec filter-index &optional default-filter)
f5b50baa
MB
3361 "Return a canonicalized version of SPEC using.
3362FILTER-INDEX is the index in the entry for each attribute in
3363`custom-face-attributes' at which the appropriate filter function can be
3364found, and DEFAULT-FILTER is the filter to apply for attributes that
3365don't specify one."
3366 (mapcar (lambda (entry)
3367 ;; Filter a single face-spec entry
3368 (let ((tests (car entry))
3369 (unfiltered-attrs
3370 ;; Handle both old- and new-style attribute syntax
3371 (if (listp (car (cdr entry)))
3372 (car (cdr entry))
3373 (cdr entry)))
3374 (filtered-attrs nil))
3375 ;; Filter each face attribute
3376 (while unfiltered-attrs
3377 (let* ((attr (pop unfiltered-attrs))
3378 (pre-filtered-value (pop unfiltered-attrs))
3379 (filter
3380 (or (nth filter-index (assq attr custom-face-attributes))
3381 default-filter))
3382 (filtered-value
3383 (if filter
3384 (funcall filter pre-filtered-value)
3385 pre-filtered-value)))
3386 (push filtered-value filtered-attrs)
3387 (push attr filtered-attrs)))
3388 ;;
3389 (list tests filtered-attrs)))
3390 spec))
3391
3392(defun custom-pre-filter-face-spec (spec)
3393 "Return SPEC changed as necessary for editing by the face customization widget.
3394SPEC must be a full face spec."
3ea051cb 3395 (custom-filter-face-spec spec 2))
f5b50baa
MB
3396
3397(defun custom-post-filter-face-spec (spec)
3398 "Return the customized SPEC in a form suitable for setting the face."
3ea051cb 3399 (custom-filter-face-spec spec 3))
f5b50baa 3400
76c16af8
CY
3401(defun custom-face-widget-to-spec (widget)
3402 "Return a face spec corresponding to WIDGET.
3403WIDGET should be a `custom-face' widget."
3404 (unless (eq (widget-type widget) 'custom-face)
3405 (error "Invalid widget"))
3406 (let ((child (car (widget-get widget :children))))
3407 (custom-post-filter-face-spec
3408 (if (eq (widget-type child) 'custom-face-edit)
3409 `((t ,(widget-value child)))
3410 (widget-value child)))))
3411
3412(defun custom-face-get-current-spec (face)
3413 (let ((spec (or (get face 'customized-face)
3414 (get face 'saved-face)
3415 (get face 'face-defface-spec)
3416 ;; Attempt to construct it.
3417 `((t ,(custom-face-attributes-get
3418 face (selected-frame)))))))
3419 ;; If the user has changed this face in some other way,
3420 ;; edit it as the user has specified it.
3421 (if (not (face-spec-match-p face spec (selected-frame)))
3422 (setq spec `((t ,(face-attr-construct face (selected-frame))))))
3423 (custom-pre-filter-face-spec spec)))
3424
647bc502
CY
3425(defun custom-toggle-hide-face (visibility-widget &rest ignore)
3426 "Toggle the visibility of a `custom-face' parent widget.
3427By default, this signals an error if the parent has unsaved
3428changes. If the parent has a `simple' :custom-style property,
3429the present value is saved to its :shown-value property instead."
3430 (let ((widget (widget-get visibility-widget :parent)))
3431 (unless (eq (widget-type widget) 'custom-face)
3432 (error "Invalid widget type"))
3433 (custom-load-widget widget)
3434 (let ((state (widget-get widget :custom-state)))
3435 (if (eq state 'hidden)
3436 (widget-put widget :custom-state 'unknown)
3437 ;; In normal interface, widget can't be hidden if modified.
3438 (when (memq state '(invalid modified set))
3439 (if (eq (widget-get widget :custom-style) 'simple)
3440 (widget-put widget :shown-value
3441 (custom-face-widget-to-spec widget))
3442 (error "There are unsaved changes")))
3443 (widget-put widget :documentation-shown nil)
3444 (widget-put widget :custom-state 'hidden))
3445 (custom-redraw widget)
3446 (widget-setup))))
3447
d543e20b 3448(defun custom-face-value-create (widget)
944c91b6 3449 "Create a list of the display specifications for WIDGET."
61328d7c
CY
3450 (let* ((buttons (widget-get widget :buttons))
3451 (symbol (widget-get widget :value))
3452 (tag (or (widget-get widget :tag)
3453 (prin1-to-string symbol)))
3454 (hiddenp (eq (widget-get widget :custom-state) 'hidden))
647bc502 3455 (style (widget-get widget :custom-style))
61328d7c
CY
3456 children)
3457
3458 (if (eq custom-buffer-style 'tree)
3459
3460 ;; Draw a tree-style `custom-face' widget
3461 (progn
3462 (insert (widget-get widget :custom-prefix)
3463 (if (widget-get widget :custom-last) " `--- " " |--- "))
3464 (push (widget-create-child-and-convert
3465 widget 'custom-browse-face-tag)
3466 buttons)
3467 (insert " " tag "\n")
3468 (widget-put widget :buttons buttons))
3469
3470 ;; Draw an ordinary `custom-face' widget
3471 (let ((opoint (point)))
3472 ;; Visibility indicator.
3473 (push (widget-create-child-and-convert
3474 widget 'custom-visibility
3475 :help-echo "Hide or show this face."
3476 :on "Hide" :off "Show"
895123f5 3477 :on-glyph "down" :off-glyph "right"
647bc502 3478 :action 'custom-toggle-hide-face
61328d7c
CY
3479 (not hiddenp))
3480 buttons)
3481 ;; Face name (tag).
3482 (insert " " tag)
3483 (widget-specify-sample widget opoint (point)))
3484 (insert
3485 (cond ((eq custom-buffer-style 'face) " ")
3486 ((string-match "face\\'" tag) ":")
3487 (t " face: ")))
3488
3489 ;; Face sample.
587faadd
CY
3490 (let ((sample-indent (widget-get widget :sample-indent))
3491 (indent-tabs-mode nil))
3492 (and sample-indent
3493 (<= (current-column) sample-indent)
3494 (indent-to-column sample-indent)))
61328d7c
CY
3495 (push (widget-create-child-and-convert
3496 widget 'item
da16abfc
CY
3497 :format "[%{%t%}]"
3498 :sample-face (let ((spec (widget-get widget :shown-value)))
3499 (if spec (face-spec-choose spec) symbol))
3500 :tag "sample")
61328d7c 3501 buttons)
61328d7c 3502 (insert "\n")
da16abfc
CY
3503
3504 ;; Magic.
647bc502 3505 (unless (eq (widget-get widget :custom-style) 'simple)
da16abfc
CY
3506 (let ((magic (widget-create-child-and-convert
3507 widget 'custom-magic nil)))
3508 (widget-put widget :custom-magic magic)
3509 (push magic buttons)))
61328d7c
CY
3510
3511 ;; Update buttons.
3512 (widget-put widget :buttons buttons)
3513
3514 ;; Insert documentation.
647bc502 3515 (unless (and hiddenp (eq style 'simple))
587faadd
CY
3516 (widget-put widget :documentation-indent 3)
3517 (widget-add-documentation-string-button
3518 widget :visibility-widget 'custom-visibility)
3519 ;; The comment field
3520 (unless hiddenp
3521 (let* ((comment (get symbol 'face-comment))
3522 (comment-widget
3523 (widget-create-child-and-convert
3524 widget 'custom-comment
3525 :parent widget
3526 :value (or comment ""))))
3527 (widget-put widget :comment-widget comment-widget)
3528 (push comment-widget children))))
61328d7c
CY
3529
3530 ;; Editor.
3531 (unless (eq (preceding-char) ?\n)
3532 (insert "\n"))
3533 (unless hiddenp
3534 (custom-load-widget widget)
3535 (unless (widget-get widget :custom-form)
3536 (widget-put widget :custom-form custom-face-default-form))
3537
da16abfc
CY
3538 (let* ((spec (or (widget-get widget :shown-value)
3539 (custom-face-get-current-spec symbol)))
61328d7c
CY
3540 (form (widget-get widget :custom-form))
3541 (indent (widget-get widget :indent))
587faadd 3542 face-alist face-entry spec-default spec-match editor)
61328d7c 3543
587faadd
CY
3544 ;; Find a display in SPEC matching the selected display.
3545 ;; This will use the usual face customization interface.
3546 (setq face-alist spec)
3547 (when (eq (car-safe (car-safe face-alist)) 'default)
3548 (setq spec-default (pop face-alist)))
3549
3550 (while (and face-alist (listp face-alist) (null spec-match))
3551 (setq face-entry (car face-alist))
3552 (and (listp face-entry)
3553 (face-spec-set-match-display (car face-entry)
3554 (selected-frame))
3555 (widget-apply custom-face-edit :match (cadr face-entry))
3556 (setq spec-match face-entry))
3557 (setq face-alist (cdr face-alist)))
3558
3559 ;; Insert the appropriate editing widget.
3560 (setq editor
3561 (cond
3562 ((and (eq form 'selected)
3563 (or spec-match spec-default))
3564 (when indent (insert-char ?\s indent))
3565 (widget-create-child-and-convert
3566 widget 'custom-face-edit
3567 :value (cadr spec-match)
3568 :default-face-attributes (cadr spec-default)))
3569 ((and (not (eq form 'lisp))
3570 (widget-apply custom-face-all :match spec))
3571 (widget-create-child-and-convert
3572 widget 'custom-face-all :value spec))
3573 (t
3574 (when indent
3575 (insert-char ?\s indent))
3576 (widget-create-child-and-convert
3577 widget 'sexp :value spec))))
61328d7c 3578 (custom-face-state-set widget)
587faadd 3579 (push editor children)
61328d7c 3580 (widget-put widget :children children))))))
d543e20b 3581
d3d4df42 3582(defvar custom-face-menu
70482877 3583 `(("Set for Current Session" custom-face-set)
9b5007e7 3584 ,@(when (or custom-file init-file-user)
633bb8a9 3585 '(("Save for Future Sessions" custom-face-save)))
70482877
LT
3586 ("Undo Edits" custom-redraw
3587 (lambda (widget)
3588 (memq (widget-get widget :custom-state) '(modified changed))))
3589 ("Reset to Saved" custom-face-reset-saved
0c731046
CY
3590 (lambda (widget)
3591 (or (get (widget-value widget) 'saved-face)
3592 (get (widget-value widget) 'saved-face-comment))))
9b5007e7 3593 ,@(when (or custom-file init-file-user)
70482877 3594 '(("Erase Customization" custom-face-reset-standard
73e60f53
CY
3595 (lambda (widget)
3596 (get (widget-value widget) 'face-defface-spec)))))
eaa99205 3597 ("---" ignore ignore)
70482877
LT
3598 ("Add Comment" custom-comment-show custom-comment-invisible-p)
3599 ("---" ignore ignore)
3600 ("For Current Display" custom-face-edit-selected
eaa99205
CY
3601 (lambda (widget)
3602 (not (eq (widget-get widget :custom-form) 'selected))))
70482877
LT
3603 ("For All Kinds of Displays" custom-face-edit-all
3604 (lambda (widget)
3605 (not (eq (widget-get widget :custom-form) 'all))))
3606 ("Show Lisp Expression" custom-face-edit-lisp
eaa99205
CY
3607 (lambda (widget)
3608 (not (eq (widget-get widget :custom-form) 'lisp)))))
d543e20b 3609 "Alist of actions for the `custom-face' widget.
6d528fc5
PA
3610Each entry has the form (NAME ACTION FILTER) where NAME is the name of
3611the menu entry, ACTION is the function to call on the widget when the
3612menu is selected, and FILTER is a predicate which takes a `custom-face'
3613widget as an argument, and returns non-nil if ACTION is valid on that
19d63704 3614widget. If FILTER is nil, ACTION is always valid.")
d543e20b
PA
3615
3616(defun custom-face-edit-selected (widget)
3617 "Edit selected attributes of the value of WIDGET."
3618 (widget-put widget :custom-state 'unknown)
3619 (widget-put widget :custom-form 'selected)
3620 (custom-redraw widget))
3621
3622(defun custom-face-edit-all (widget)
3623 "Edit all attributes of the value of WIDGET."
3624 (widget-put widget :custom-state 'unknown)
3625 (widget-put widget :custom-form 'all)
3626 (custom-redraw widget))
3627
3628(defun custom-face-edit-lisp (widget)
2365594b 3629 "Edit the Lisp representation of the value of WIDGET."
d543e20b
PA
3630 (widget-put widget :custom-state 'unknown)
3631 (widget-put widget :custom-form 'lisp)
3632 (custom-redraw widget))
3633
76c16af8
CY
3634(defun custom-face-state (face)
3635 "Return the current state of the face FACE.
3636This is one of `set', `saved', `changed', `themed', or `rogue'."
3637 (let* ((comment (get face 'face-comment))
a4992f73 3638 (state
76c16af8
CY
3639 (cond
3640 ((or (get face 'customized-face)
3641 (get face 'customized-face-comment))
3642 (if (equal (get face 'customized-face-comment) comment)
3643 'set
3644 'changed))
3645 ((or (get face 'saved-face)
3646 (get face 'saved-face-comment))
3647 (if (equal (get face 'saved-face-comment) comment)
3648 (cond
3649 ((eq 'user (caar (get face 'theme-face)))
3650 'saved)
3651 ((eq 'changed (caar (get face 'theme-face)))
3652 'changed)
3653 (t 'themed))
3654 'changed))
3655 ((get face 'face-defface-spec)
3656 (if (equal comment nil)
3657 'standard
3658 'changed))
3659 (t 'rogue))))
3660 ;; If the user called set-face-attribute to change the default for
3661 ;; new frames, this face is "set outside of Customize".
a4992f73 3662 (if (and (not (eq state 'rogue))
76c16af8
CY
3663 (get face 'face-modified))
3664 'changed
3665 state)))
3666
3667(defun custom-face-state-set (widget)
3668 "Set the state of WIDGET."
3669 (widget-put widget :custom-state
3670 (custom-face-state (widget-value widget))))
d543e20b
PA
3671
3672(defun custom-face-action (widget &optional event)
3673 "Show the menu for `custom-face' WIDGET.
3674Optional EVENT is the location for the menu."
3675 (if (eq (widget-get widget :custom-state) 'hidden)
6d528fc5 3676 (custom-toggle-hide widget)
d543e20b
PA
3677 (let* ((completion-ignore-case t)
3678 (symbol (widget-get widget :value))
25ac13b5
PA
3679 (answer (widget-choose (concat "Operation on "
3680 (custom-unlispify-tag-name symbol))
6d528fc5
PA
3681 (custom-menu-filter custom-face-menu
3682 widget)
3683 event)))
d543e20b
PA
3684 (if answer
3685 (funcall answer widget)))))
3686
3687(defun custom-face-set (widget)
3688 "Make the face attributes in WIDGET take effect."
3689 (let* ((symbol (widget-value widget))
76c16af8 3690 (value (custom-face-widget-to-spec widget))
d3d4df42
DL
3691 (comment-widget (widget-get widget :comment-widget))
3692 (comment (widget-value comment-widget)))
3693 (when (equal comment "")
3694 (setq comment nil)
3695 ;; Make the comment invisible by hand if it's empty
164cfaeb 3696 (custom-comment-hide comment-widget))
d543e20b 3697 (put symbol 'customized-face value)
e02577b7 3698 (custom-push-theme 'theme-face symbol 'user 'set value)
f5b50baa 3699 (if (face-spec-choose value)
d50e9d5b 3700 (face-spec-set symbol value t)
f5b50baa
MB
3701 ;; face-set-spec ignores empty attribute lists, so just give it
3702 ;; something harmless instead.
d50e9d5b 3703 (face-spec-set symbol '((t :foreground unspecified)) t))
d3d4df42
DL
3704 (put symbol 'customized-face-comment comment)
3705 (put symbol 'face-comment comment)
d543e20b
PA
3706 (custom-face-state-set widget)
3707 (custom-redraw-magic widget)))
3708
5ae87ed4
MR
3709(defun custom-face-mark-to-save (widget)
3710 "Mark for saving the face edited by WIDGET."
d543e20b 3711 (let* ((symbol (widget-value widget))
76c16af8 3712 (value (custom-face-widget-to-spec widget))
d3d4df42
DL
3713 (comment-widget (widget-get widget :comment-widget))
3714 (comment (widget-value comment-widget)))
3715 (when (equal comment "")
3716 (setq comment nil)
3717 ;; Make the comment invisible by hand if it's empty
164cfaeb 3718 (custom-comment-hide comment-widget))
b76747af 3719 (custom-push-theme 'theme-face symbol 'user 'set value)
e475612a 3720 (if (face-spec-choose value)
d50e9d5b 3721 (face-spec-set symbol value t)
e475612a
MB
3722 ;; face-set-spec ignores empty attribute lists, so just give it
3723 ;; something harmless instead.
d50e9d5b 3724 (face-spec-set symbol '((t :foreground unspecified)) t))
4f985043
RS
3725 (unless (eq (widget-get widget :custom-state) 'standard)
3726 (put symbol 'saved-face value))
d543e20b 3727 (put symbol 'customized-face nil)
d3d4df42
DL
3728 (put symbol 'face-comment comment)
3729 (put symbol 'customized-face-comment nil)
5ae87ed4
MR
3730 (put symbol 'saved-face-comment comment)))
3731
3732(defsubst custom-face-state-set-and-redraw (widget)
3733 "Set state of face widget WIDGET and redraw with current settings."
3734 (custom-face-state-set widget)
3735 (custom-redraw-magic widget))
3736
3737(defun custom-face-save (widget)
3738 "Save the face edited by WIDGET."
3739 (custom-face-mark-to-save widget)
3740 (custom-save-all)
3741 (custom-face-state-set-and-redraw widget))
d543e20b 3742
633bb8a9
LT
3743;; For backward compatibility.
3744(define-obsolete-function-alias 'custom-face-save-command 'custom-face-save
3745 "22.1")
3746
d543e20b
PA
3747(defun custom-face-reset-saved (widget)
3748 "Restore WIDGET to the face's default attributes."
3749 (let* ((symbol (widget-value widget))
3750 (child (car (widget-get widget :children)))
d3d4df42
DL
3751 (value (get symbol 'saved-face))
3752 (comment (get symbol 'saved-face-comment))
3753 (comment-widget (widget-get widget :comment-widget)))
3754 (unless (or value comment)
d543e20b
PA
3755 (error "No saved value for this face"))
3756 (put symbol 'customized-face nil)
d3d4df42 3757 (put symbol 'customized-face-comment nil)
25c6d447 3758 (custom-push-theme 'theme-face symbol 'user 'set value)
d50e9d5b 3759 (face-spec-set symbol value t)
d3d4df42 3760 (put symbol 'face-comment comment)
d543e20b 3761 (widget-value-set child value)
d3d4df42
DL
3762 ;; This call manages the comment visibility
3763 (widget-value-set comment-widget (or comment ""))
d543e20b
PA
3764 (custom-face-state-set widget)
3765 (custom-redraw-magic widget)))
3766
4f985043
RS
3767(defun custom-face-standard-value (widget)
3768 (get (widget-value widget) 'face-defface-spec))
3769
5ae87ed4
MR
3770(defun custom-face-mark-to-reset-standard (widget)
3771 "Restore widget WIDGET to the face's standard attribute values.
3772If `custom-reset-standard-faces-list' is nil, save, reset and
3773redraw the widget immediately."
d543e20b
PA
3774 (let* ((symbol (widget-value widget))
3775 (child (car (widget-get widget :children)))
d3d4df42
DL
3776 (value (get symbol 'face-defface-spec))
3777 (comment-widget (widget-get widget :comment-widget)))
d543e20b 3778 (unless value
5dd0cad0 3779 (error "No standard setting for this face"))
d543e20b 3780 (put symbol 'customized-face nil)
d3d4df42 3781 (put symbol 'customized-face-comment nil)
d358aa10 3782 (custom-push-theme 'theme-face symbol 'user 'reset)
d50e9d5b 3783 (face-spec-set symbol value t)
25c6d447 3784 (custom-theme-recalc-face symbol)
5ae87ed4
MR
3785 (if (and custom-reset-standard-faces-list
3786 (or (get symbol 'saved-face) (get symbol 'saved-face-comment)))
3787 ;; Do this later.
3788 (progn
3789 (put symbol 'saved-face nil)
3790 (put symbol 'saved-face-comment nil)
3791 ;; Append this to `custom-reset-standard-faces-list' and have
3792 ;; `custom-reset-standard-save-and-update' save setting to the
3793 ;; file, update the widget's state, and redraw it.
3794 (setq custom-reset-standard-faces-list
3795 (cons widget custom-reset-standard-faces-list)))
3796 (when (or (get symbol 'saved-face) (get symbol 'saved-face-comment))
3797 (put symbol 'saved-face nil)
3798 (put symbol 'saved-face-comment nil)
3799 (custom-save-all))
3800 (put symbol 'face-comment nil)
3801 (widget-value-set child
3802 (custom-pre-filter-face-spec
3803 (list (list t (custom-face-attributes-get
3804 symbol nil)))))
3805 ;; This call manages the comment visibility
3806 (widget-value-set comment-widget "")
3807 (custom-face-state-set widget)
3808 (custom-redraw-magic widget))))
3809
3810(defun custom-face-reset-standard (widget)
3811 "Restore WIDGET to the face's standard attribute values.
3812This operation eliminates any saved attributes for the face,
3813restoring it to the state of a face that has never been customized."
3814 (let (custom-reset-standard-faces-list)
3815 (custom-face-mark-to-reset-standard widget)))
d543e20b
PA
3816
3817;;; The `face' Widget.
3818
1833b7b3
RS
3819(defvar widget-face-prompt-value-history nil
3820 "History of input to `widget-face-prompt-value'.")
3821
0e739597
DP
3822(define-widget 'face 'symbol
3823 "A Lisp face name (with sample)."
49698bb7 3824 :format "%{%t%}: (%{sample%}) %v"
0e739597
DP
3825 :tag "Face"
3826 :value 'default
3827 :sample-face-get 'widget-face-sample-face-get
3828 :notify 'widget-face-notify
3829 :match (lambda (widget value) (facep value))
1833b7b3
RS
3830 :complete-function (lambda ()
3831 (interactive)
3832 (lisp-complete-symbol 'facep))
1833b7b3
RS
3833 :prompt-match 'facep
3834 :prompt-history 'widget-face-prompt-value-history
1833b7b3
RS
3835 :validate (lambda (widget)
3836 (unless (facep (widget-value widget))
0e739597
DP
3837 (widget-put widget
3838 :error (format "Invalid face: %S"
3839 (widget-value widget)))
3840 widget)))
3841
3842(defun widget-face-sample-face-get (widget)
3843 (let ((value (widget-value widget)))
3844 (if (facep value)
3845 value
3846 'default)))
3847
3848(defun widget-face-notify (widget child &optional event)
3849 "Update the sample, and notify the parent."
3850 (overlay-put (widget-get widget :sample-overlay)
3851 'face (widget-apply widget :sample-face-get))
3852 (widget-default-notify widget child event))
1833b7b3 3853
d543e20b
PA
3854
3855;;; The `hook' Widget.
3856
3857(define-widget 'hook 'list
c8c9333c 3858 "An Emacs Lisp hook."
f985c5f7 3859 :value-to-internal (lambda (widget value)
5aa3f181 3860 (if (and value (symbolp value))
f985c5f7
PA
3861 (list value)
3862 value))
3863 :match (lambda (widget value)
3864 (or (symbolp value)
4743fc91 3865 (widget-group-match widget value)))
2365594b
DL
3866 ;; Avoid adding undefined functions to the hook, especially for
3867 ;; things like `find-file-hook' or even more basic ones, to avoid
3868 ;; chaos.
3869 :set (lambda (symbol value)
d4881668
SM
3870 (dolist (elt value)
3871 (if (fboundp elt)
3872 (add-hook symbol elt))))
d543e20b
PA
3873 :convert-widget 'custom-hook-convert-widget
3874 :tag "Hook")
3875
3876(defun custom-hook-convert-widget (widget)
3c708e98 3877 ;; Handle `:options'.
d543e20b 3878 (let* ((options (widget-get widget :options))
d3d4df42 3879 (other `(editable-list :inline t
d543e20b
PA
3880 :entry-format "%i %d%v"
3881 (function :format " %v")))
3882 (args (if options
3883 (list `(checklist :inline t
3884 ,@(mapcar (lambda (entry)
3885 `(function-item ,entry))
3886 options))
3887 other)
3888 (list other))))
3889 (widget-put widget :args args)
3890 widget))
3891
944c91b6
PA
3892;;; The `custom-group-link' Widget.
3893
3894(define-widget 'custom-group-link 'link
3895 "Show parent in other window when activated."
192e44fc
JL
3896 :button-face 'custom-link
3897 :mouse-face 'highlight
e1ec62a5 3898 :pressed-face 'highlight
b62c92bb 3899 :help-echo "Create customization buffer for this group."
f3093f77
CY
3900 :keymap custom-mode-link-map
3901 :follow-link 'mouse-face
944c91b6
PA
3902 :action 'custom-group-link-action)
3903
3904(defun custom-group-link-action (widget &rest ignore)
3905 (customize-group (widget-value widget)))
3906
d543e20b
PA
3907;;; The `custom-group' Widget.
3908
b62c92bb 3909(defcustom custom-group-tag-faces nil
d543e20b 3910 ;; In XEmacs, this ought to play games with font size.
d3d4df42 3911 ;; Fixme: make it do so in Emacs.
d543e20b
PA
3912 "Face used for group tags.
3913The first member is used for level 1 groups, the second for level 2,
d478e69d 3914and so forth. The remaining group tags are shown with `custom-group-tag'."
d543e20b 3915 :type '(repeat face)
bd042c03 3916 :group 'custom-faces)
d543e20b 3917
d478e69d 3918(defface custom-group-tag-1
16b20ed9
GM
3919 `((((class color)
3920 (background dark))
b5555381 3921 (:foreground "pink" :weight bold :height 1.2 :inherit variable-pitch))
ea81d57e
DN
3922 (((min-colors 88) (class color)
3923 (background light))
3924 (:foreground "red1" :weight bold :height 1.2 :inherit variable-pitch))
16b20ed9
GM
3925 (((class color)
3926 (background light))
b5555381
RS
3927 (:foreground "red" :weight bold :height 1.2 :inherit variable-pitch))
3928 (t (:weight bold)))
16b20ed9
GM
3929 "Face used for group tags."
3930 :group 'custom-faces)
c4f6e489 3931(define-obsolete-face-alias 'custom-group-tag-face-1 'custom-group-tag-1 "22.1")
16b20ed9 3932
d478e69d 3933(defface custom-group-tag
16b20ed9
GM
3934 `((((class color)
3935 (background dark))
9db1942d 3936 (:foreground "light blue" :weight bold :height 1.2 :inherit variable-pitch))
ea81d57e
DN
3937 (((min-colors 88) (class color)
3938 (background light))
9db1942d 3939 (:foreground "blue1" :weight bold :height 1.2 :inherit variable-pitch))
16b20ed9
GM
3940 (((class color)
3941 (background light))
9db1942d 3942 (:foreground "blue" :weight bold :height 1.2 :inherit variable-pitch))
b5555381 3943 (t (:weight bold)))
d543e20b 3944 "Face used for low level group tags."
bd042c03 3945 :group 'custom-faces)
c4f6e489 3946(define-obsolete-face-alias 'custom-group-tag-face 'custom-group-tag "22.1")
d543e20b
PA
3947
3948(define-widget 'custom-group 'custom
3949 "Customize group."
944c91b6 3950 :format "%v"
d543e20b
PA
3951 :sample-face-get 'custom-group-sample-face-get
3952 :documentation-property 'group-documentation
3953 :help-echo "Set or reset all members of this group."
3954 :value-create 'custom-group-value-create
3955 :action 'custom-group-action
9097aeb7 3956 :custom-category 'group
d543e20b 3957 :custom-set 'custom-group-set
5ae87ed4 3958 :custom-mark-to-save 'custom-group-mark-to-save
d543e20b
PA
3959 :custom-reset-current 'custom-group-reset-current
3960 :custom-reset-saved 'custom-group-reset-saved
25ac13b5 3961 :custom-reset-standard 'custom-group-reset-standard
5ae87ed4
MR
3962 :custom-mark-to-reset-standard 'custom-group-mark-to-reset-standard
3963 :custom-state-set-and-redraw 'custom-group-state-set-and-redraw
d543e20b
PA
3964 :custom-menu 'custom-group-menu-create)
3965
3966(defun custom-group-sample-face-get (widget)
3967 ;; Use :sample-face.
3968 (or (nth (1- (widget-get widget :custom-level)) custom-group-tag-faces)
d478e69d 3969 'custom-group-tag))
d543e20b 3970
8691cfa7
RS
3971(define-widget 'custom-group-visibility 'visibility
3972 "An indicator and manipulator for hidden group contents."
3973 :create 'custom-group-visibility-create)
3974
3975(defun custom-group-visibility-create (widget)
3976 (let ((visible (widget-value widget)))
3977 (if visible
3978 (insert "--------")))
3979 (widget-default-create widget))
3980
4ee1cf9f
PA
3981(defun custom-group-members (symbol groups-only)
3982 "Return SYMBOL's custom group members.
3983If GROUPS-ONLY non-nil, return only those members that are groups."
3984 (if (not groups-only)
3985 (get symbol 'custom-group)
3986 (let (members)
3987 (dolist (entry (get symbol 'custom-group))
3988 (when (eq (nth 1 entry) 'custom-group)
3989 (push entry members)))
3990 (nreverse members))))
3991
d543e20b 3992(defun custom-group-value-create (widget)
944c91b6 3993 "Insert a customize group for WIDGET in the current buffer."
2de2cb02 3994 (unless (eq (widget-get widget :custom-state) 'hidden)
2ee398c4 3995 (custom-load-widget widget))
4ee1cf9f
PA
3996 (let* ((state (widget-get widget :custom-state))
3997 (level (widget-get widget :custom-level))
f985c5f7 3998 ;; (indent (widget-get widget :indent))
4ee1cf9f
PA
3999 (prefix (widget-get widget :custom-prefix))
4000 (buttons (widget-get widget :buttons))
4001 (tag (widget-get widget :tag))
4002 (symbol (widget-value widget))
4003 (members (custom-group-members symbol
4004 (and (eq custom-buffer-style 'tree)
c19aae89
MR
4005 custom-browse-only-groups)))
4006 (doc (widget-docstring widget)))
944c91b6 4007 (cond ((and (eq custom-buffer-style 'tree)
c953515e 4008 (eq state 'hidden)
4ee1cf9f 4009 (or members (custom-unloaded-widget-p widget)))
c953515e 4010 (custom-browse-insert-prefix prefix)
944c91b6 4011 (push (widget-create-child-and-convert
d3d4df42 4012 widget 'custom-browse-visibility
da5ec617 4013 ;; :tag-glyph "plus"
df816618 4014 :tag "+")
944c91b6
PA
4015 buttons)
4016 (insert "-- ")
da5ec617 4017 ;; (widget-glyph-insert nil "-- " "horizontal")
944c91b6 4018 (push (widget-create-child-and-convert
c953515e 4019 widget 'custom-browse-group-tag)
944c91b6
PA
4020 buttons)
4021 (insert " " tag "\n")
4022 (widget-put widget :buttons buttons))
4023 ((and (eq custom-buffer-style 'tree)
4ee1cf9f 4024 (zerop (length members)))
c953515e 4025 (custom-browse-insert-prefix prefix)
da5ec617
PA
4026 (insert "[ ]-- ")
4027 ;; (widget-glyph-insert nil "[ ]" "empty")
4028 ;; (widget-glyph-insert nil "-- " "horizontal")
d3d4df42 4029 (push (widget-create-child-and-convert
c953515e 4030 widget 'custom-browse-group-tag)
944c91b6
PA
4031 buttons)
4032 (insert " " tag "\n")
4033 (widget-put widget :buttons buttons))
4034 ((eq custom-buffer-style 'tree)
c953515e 4035 (custom-browse-insert-prefix prefix)
4ee1cf9f 4036 (if (zerop (length members))
d3d4df42 4037 (progn
c953515e 4038 (custom-browse-insert-prefix prefix)
da5ec617
PA
4039 (insert "[ ]-- ")
4040 ;; (widget-glyph-insert nil "[ ]" "empty")
4041 ;; (widget-glyph-insert nil "-- " "horizontal")
d3d4df42 4042 (push (widget-create-child-and-convert
c953515e 4043 widget 'custom-browse-group-tag)
944c91b6
PA
4044 buttons)
4045 (insert " " tag "\n")
4046 (widget-put widget :buttons buttons))
d3d4df42
DL
4047 (push (widget-create-child-and-convert
4048 widget 'custom-browse-visibility
da5ec617
PA
4049 ;; :tag-glyph "minus"
4050 :tag "-")
944c91b6 4051 buttons)
da5ec617
PA
4052 (insert "-\\ ")
4053 ;; (widget-glyph-insert nil "-\\ " "top")
d3d4df42 4054 (push (widget-create-child-and-convert
c953515e 4055 widget 'custom-browse-group-tag)
944c91b6
PA
4056 buttons)
4057 (insert " " tag "\n")
4058 (widget-put widget :buttons buttons)
4059 (message "Creating group...")
647f9993
CY
4060 (let* ((members (custom-sort-items
4061 members
4062 ;; Never sort the top-level custom group.
4063 (unless (eq symbol 'emacs)
4064 custom-browse-sort-alphabetically)
da5ec617 4065 custom-browse-order-groups))
944c91b6
PA
4066 (prefixes (widget-get widget :custom-prefixes))
4067 (custom-prefix-list (custom-prefix-add symbol prefixes))
944c91b6
PA
4068 (extra-prefix (if (widget-get widget :custom-last)
4069 " "
4070 " | "))
4071 (prefix (concat prefix extra-prefix))
4072 children entry)
4073 (while members
4074 (setq entry (car members)
4075 members (cdr members))
4ee1cf9f
PA
4076 (push (widget-create-child-and-convert
4077 widget (nth 1 entry)
4078 :group widget
4079 :tag (custom-unlispify-tag-name (nth 0 entry))
4080 :custom-prefixes custom-prefix-list
4081 :custom-level (1+ level)
4082 :custom-last (null members)
4083 :value (nth 0 entry)
4084 :custom-prefix prefix)
4085 children))
944c91b6
PA
4086 (widget-put widget :children (reverse children)))
4087 (message "Creating group...done")))
4088 ;; Nested style.
4089 ((eq state 'hidden)
4090 ;; Create level indicator.
944c91b6 4091 ;; Create tag.
944c91b6
PA
4092 (if (eq custom-buffer-style 'links)
4093 (push (widget-create-child-and-convert
d3d4df42 4094 widget 'custom-group-link
9db1942d 4095 :tag tag
944c91b6
PA
4096 symbol)
4097 buttons)
9db1942d
CY
4098 (insert-char ?\ (* custom-buffer-indent (1- level)))
4099 (insert "-- ")
d3d4df42 4100 (push (widget-create-child-and-convert
98d5aafe 4101 widget 'custom-group-visibility
944c91b6
PA
4102 :help-echo "Show members of this group."
4103 :action 'custom-toggle-parent
4104 (not (eq state 'hidden)))
4105 buttons))
9db1942d 4106 (insert " : ")
944c91b6
PA
4107 ;; Create magic button.
4108 (let ((magic (widget-create-child-and-convert
4109 widget 'custom-magic nil)))
4110 (widget-put widget :custom-magic magic)
4111 (push magic buttons))
4112 ;; Update buttons.
4113 (widget-put widget :buttons buttons)
4114 ;; Insert documentation.
26c7b3ef
RS
4115 (if (and (eq custom-buffer-style 'links) (> level 1))
4116 (widget-put widget :documentation-indent 0))
12bafdaa
CY
4117 (widget-add-documentation-string-button
4118 widget :visibility-widget 'custom-visibility))
4119
944c91b6
PA
4120 ;; Nested style.
4121 (t ;Visible.
647f9993
CY
4122 ;; Draw a horizontal line (this works for both graphical
4123 ;; and text displays):
4124 (let ((p (point)))
4125 (insert "\n")
4126 (put-text-property p (1+ p) 'face '(:underline t))
4127 (overlay-put (make-overlay p (1+ p))
4128 'before-string
4129 (propertize "\n" 'face '(:underline t)
4130 'display '(space :align-to 999))))
4131
d377bee9 4132 ;; Add parent groups references above the group.
647f9993
CY
4133 (when (eq level 1)
4134 (if (custom-add-parent-links widget "Parent groups:")
4135 (insert "\n")))
944c91b6 4136 (insert-char ?\ (* custom-buffer-indent (1- level)))
944c91b6
PA
4137 ;; Create tag.
4138 (let ((start (point)))
9db1942d 4139 (insert tag " group: ")
944c91b6 4140 (widget-specify-sample widget start (point)))
1e3d9386
MR
4141 (cond
4142 ((not doc)
4143 (insert " Group definition missing. "))
4144 ((< (length doc) 50)
4145 (insert doc)))
944c91b6
PA
4146 ;; Create visibility indicator.
4147 (unless (eq custom-buffer-style 'links)
4148 (insert "--------")
d3d4df42 4149 (push (widget-create-child-and-convert
944c91b6
PA
4150 widget 'visibility
4151 :help-echo "Hide members of this group."
4152 :action 'custom-toggle-parent
4153 (not (eq state 'hidden)))
4154 buttons)
4155 (insert " "))
647f9993 4156 (insert "\n")
944c91b6
PA
4157 ;; Create magic button.
4158 (let ((magic (widget-create-child-and-convert
d3d4df42 4159 widget 'custom-magic
944c91b6
PA
4160 :indent 0
4161 nil)))
4162 (widget-put widget :custom-magic magic)
4163 (push magic buttons))
4164 ;; Update buttons.
4165 (widget-put widget :buttons buttons)
4166 ;; Insert documentation.
c19aae89
MR
4167 (when (and doc (>= (length doc) 50))
4168 (widget-add-documentation-string-button
4169 widget :visibility-widget 'custom-visibility))
12bafdaa 4170
d377bee9
RS
4171 ;; Parent groups.
4172 (if nil ;;; This should test that the buffer
4173 ;;; was not made to display a group.
4174 (when (eq level 1)
4175 (insert-char ?\ custom-buffer-indent)
4176 (custom-add-parent-links widget)))
d3d4df42 4177 (custom-add-see-also widget
944c91b6
PA
4178 (make-string (* custom-buffer-indent level)
4179 ?\ ))
4180 ;; Members.
4181 (message "Creating group...")
647f9993
CY
4182 (let* ((members (custom-sort-items
4183 members
4184 ;; Never sort the top-level custom group.
4185 (unless (eq symbol 'emacs)
4186 custom-buffer-sort-alphabetically)
4187 custom-buffer-order-groups))
944c91b6
PA
4188 (prefixes (widget-get widget :custom-prefixes))
4189 (custom-prefix-list (custom-prefix-add symbol prefixes))
647f9993 4190 (len (length members))
944c91b6 4191 (count 0)
647f9993
CY
4192 (reporter (make-progress-reporter
4193 "Creating group entries..." 0 len))
4194 children)
4195 (setq children
4196 (mapcar
4197 (lambda (entry)
4198 (widget-insert "\n")
4199 (progress-reporter-update reporter (setq count (1+ count)))
4200 (let ((sym (nth 0 entry))
4201 (type (nth 1 entry))
4202 hidden-p)
4203 (prog1
4204 (widget-create-child-and-convert
4205 widget type
4206 :group widget
4207 :tag (custom-unlispify-tag-name sym)
4208 :custom-prefixes custom-prefix-list
4209 :custom-level (1+ level)
4210 :value sym)
4211 (unless (eq (preceding-char) ?\n)
4212 (widget-insert "\n")))))
4213 members))
fadbdfea 4214 (mapc 'custom-magic-reset children)
944c91b6
PA
4215 (widget-put widget :children children)
4216 (custom-group-state-update widget)
647f9993 4217 (progress-reporter-done reporter))
944c91b6 4218 ;; End line
d64b8a68
CY
4219 (let ((p (1+ (point))))
4220 (insert "\n\n")
647f9993
CY
4221 (put-text-property p (1+ p) 'face '(:underline t))
4222 (overlay-put (make-overlay p (1+ p))
4223 'before-string
4224 (propertize "\n" 'face '(:underline t)
4225 'display '(space :align-to 999))))))))
d543e20b 4226
d3d4df42 4227(defvar custom-group-menu
70482877 4228 `(("Set for Current Session" custom-group-set
eaa99205
CY
4229 (lambda (widget)
4230 (eq (widget-get widget :custom-state) 'modified)))
9b5007e7 4231 ,@(when (or custom-file init-file-user)
70482877 4232 '(("Save for Future Sessions" custom-group-save
eaa99205
CY
4233 (lambda (widget)
4234 (memq (widget-get widget :custom-state) '(modified set))))))
70482877 4235 ("Undo Edits" custom-group-reset-current
6d528fc5 4236 (lambda (widget)
86bd10bc 4237 (memq (widget-get widget :custom-state) '(modified))))
70482877 4238 ("Reset to Saved" custom-group-reset-saved
6d528fc5 4239 (lambda (widget)
86bd10bc 4240 (memq (widget-get widget :custom-state) '(modified set))))
9b5007e7 4241 ,@(when (or custom-file init-file-user)
70482877 4242 '(("Erase Customization" custom-group-reset-standard
73e60f53 4243 (lambda (widget)
eaa99205 4244 (memq (widget-get widget :custom-state) '(modified set saved)))))))
d543e20b 4245 "Alist of actions for the `custom-group' widget.
6d528fc5
PA
4246Each entry has the form (NAME ACTION FILTER) where NAME is the name of
4247the menu entry, ACTION is the function to call on the widget when the
4248menu is selected, and FILTER is a predicate which takes a `custom-group'
4249widget as an argument, and returns non-nil if ACTION is valid on that
d3d4df42 4250widget. If FILTER is nil, ACTION is always valid.")
d543e20b
PA
4251
4252(defun custom-group-action (widget &optional event)
4253 "Show the menu for `custom-group' WIDGET.
4254Optional EVENT is the location for the menu."
4255 (if (eq (widget-get widget :custom-state) 'hidden)
6d528fc5 4256 (custom-toggle-hide widget)
d543e20b 4257 (let* ((completion-ignore-case t)
25ac13b5
PA
4258 (answer (widget-choose (concat "Operation on "
4259 (custom-unlispify-tag-name
4260 (widget-get widget :value)))
6d528fc5
PA
4261 (custom-menu-filter custom-group-menu
4262 widget)
d543e20b
PA
4263 event)))
4264 (if answer
4265 (funcall answer widget)))))
4266
4267(defun custom-group-set (widget)
4268 "Set changes in all modified group members."
9db1942d
CY
4269 (dolist (child (widget-get widget :children))
4270 (when (eq (widget-get child :custom-state) 'modified)
4271 (widget-apply child :custom-set))))
d543e20b 4272
5ae87ed4
MR
4273(defun custom-group-mark-to-save (widget)
4274 "Mark all modified group members for saving."
ed8c9cf1 4275 (dolist (child (widget-get widget :children))
9db1942d 4276 (when (memq (widget-get child :custom-state) '(modified set))
5ae87ed4
MR
4277 (widget-apply child :custom-mark-to-save))))
4278
4279(defsubst custom-group-state-set-and-redraw (widget)
4280 "Set state of group widget WIDGET and redraw with current settings."
4281 (dolist (child (widget-get widget :children))
4282 (when (memq (widget-get child :custom-state) '(modified set))
4283 (widget-apply child :custom-state-set-and-redraw))))
4284
4285(defun custom-group-save (widget)
4286 "Save all modified group members."
4287 (custom-group-mark-to-save widget)
4288 (custom-save-all)
4289 (custom-group-state-set-and-redraw widget))
d543e20b
PA
4290
4291(defun custom-group-reset-current (widget)
4292 "Reset all modified group members."
9db1942d
CY
4293 (dolist (child (widget-get widget :children))
4294 (when (eq (widget-get child :custom-state) 'modified)
4295 (widget-apply child :custom-reset-current))))
d543e20b
PA
4296
4297(defun custom-group-reset-saved (widget)
4298 "Reset all modified or set group members."
9db1942d
CY
4299 (dolist (child (widget-get widget :children))
4300 (when (memq (widget-get child :custom-state) '(modified set))
4301 (widget-apply child :custom-reset-saved))))
d543e20b 4302
25ac13b5 4303(defun custom-group-reset-standard (widget)
d543e20b 4304 "Reset all modified, set, or saved group members."
5ae87ed4
MR
4305 (let ((custom-reset-standard-variables-list '(t))
4306 (custom-reset-standard-faces-list '(t)))
4307 (custom-group-mark-to-reset-standard widget)
4308 (custom-reset-standard-save-and-update)))
4309
4310(defun custom-group-mark-to-reset-standard (widget)
4311 "Mark to reset all modified, set, or saved group members."
9db1942d
CY
4312 (dolist (child (widget-get widget :children))
4313 (when (memq (widget-get child :custom-state)
4314 '(modified set saved))
5ae87ed4 4315 (widget-apply child :custom-mark-to-reset-standard))))
d543e20b
PA
4316
4317(defun custom-group-state-update (widget)
4318 "Update magic."
4319 (unless (eq (widget-get widget :custom-state) 'hidden)
4320 (let* ((children (widget-get widget :children))
4321 (states (mapcar (lambda (child)
4322 (widget-get child :custom-state))
4323 children))
25ac13b5
PA
4324 (magics custom-magic-alist)
4325 (found 'standard))
d543e20b
PA
4326 (while magics
4327 (let ((magic (car (car magics))))
4328 (if (and (not (eq magic 'hidden))
4329 (memq magic states))
4330 (setq found magic
4331 magics nil)
4332 (setq magics (cdr magics)))))
4333 (widget-put widget :custom-state found)))
4334 (custom-magic-reset widget))
f8d869d1
RS
4335\f
4336;;; Reading and writing the custom file.
d543e20b 4337
a1a4fa22 4338;;;###autoload
1e4ed6df 4339(defcustom custom-file nil
d543e20b 4340 "File used for storing customization information.
1e4ed6df 4341The default is nil, which means to use your init file
09b73f49
RS
4342as specified by `user-init-file'. If the value is not nil,
4343it should be an absolute file name.
4344
5aa971e3
LT
4345You can set this option through Custom, if you carefully read the
4346last paragraph below. However, usually it is simpler to write
4347something like the following in your init file:
4348
4349\(setq custom-file \"~/.emacs-custom.el\")
4350\(load custom-file)
4351
4352Note that both lines are necessary: the first line tells Custom to
4353save all customizations in this file, but does not load it.
4354
4355When you change this variable outside Custom, look in the
4356previous custom file \(usually your init file) for the
4357forms `(custom-set-variables ...)' and `(custom-set-faces ...)',
4358and copy them (whichever ones you find) to the new custom file.
4359This will preserve your existing customizations.
4360
4361If you save this option using Custom, Custom will write all
4362currently saved customizations, including the new one for this
4363option itself, into the file you specify, overwriting any
4364`custom-set-variables' and `custom-set-faces' forms already
4365present in that file. It will not delete any customizations from
4366the old custom file. You should do that manually if that is what you
4367want. You also have to put something like `\(load \"CUSTOM-FILE\")
4368in your init file, where CUSTOM-FILE is the actual name of the
4369file. Otherwise, Emacs will not load the file when it starts up,
4370and hence will not set `custom-file' to that file either."
4371 :type '(choice (const :tag "Your Emacs init file" nil)
4372 (file :format "%t:%v%d"
4373 :doc
4374 "Please read entire docstring below before setting \
4375this through Custom.
6cda144f 4376Click on \"More\" \(or position point there and press RETURN)
5aa971e3 4377if only the first line of the docstring is shown."))
d543e20b
PA
4378 :group 'customize)
4379
176eb8cb
KH
4380(defun custom-file ()
4381 "Return the file name for saving customizations."
541044b0
RS
4382 (file-chase-links
4383 (or custom-file
4384 (let ((user-init-file user-init-file)
4385 (default-init-file
4386 (if (eq system-type 'ms-dos) "~/_emacs" "~/.emacs")))
4387 (when (null user-init-file)
4388 (if (or (file-exists-p default-init-file)
4389 (and (eq system-type 'windows-nt)
4390 (file-exists-p "~/_emacs")))
4391 ;; Started with -q, i.e. the file containing
4392 ;; Custom settings hasn't been read. Saving
4393 ;; settings there would overwrite other settings.
4394 (error "Saving settings from \"emacs -q\" would overwrite existing customizations"))
4395 (setq user-init-file default-init-file))
4396 user-init-file))))
176eb8cb 4397
b581d2ea
GM
4398;; If recentf-mode is non-nil, this is defined.
4399(declare-function recentf-expand-file-name "recentf" (name))
4400
f8d869d1
RS
4401;;;###autoload
4402(defun custom-save-all ()
4403 "Save all customizations in `custom-file'."
e29e9065
RS
4404 (when (and (null custom-file) init-file-had-error)
4405 (error "Cannot save customizations; init file was not fully loaded"))
f8d869d1 4406 (let* ((filename (custom-file))
c34a8a87
EZ
4407 (recentf-exclude
4408 (if recentf-mode
4409 (cons (concat "\\`"
4410 (regexp-quote
4411 (recentf-expand-file-name (custom-file)))
4412 "\\'")
4413 recentf-exclude)))
ac183163
CY
4414 (old-buffer (find-buffer-visiting filename))
4415 old-buffer-name)
4416
9e2f5050
RS
4417 (with-current-buffer (let ((find-file-visit-truename t))
4418 (or old-buffer (find-file-noselect filename)))
ac183163
CY
4419 ;; We'll save using file-precious-flag, so avoid destroying
4420 ;; symlinks. (If we're not already visiting the buffer, this is
4421 ;; handled by find-file-visit-truename, above.)
4422 (when old-buffer
4423 (setq old-buffer-name (buffer-file-name))
4424 (set-visited-file-name (file-chase-links filename)))
4425
31b7fa51
CY
4426 (unless (eq major-mode 'emacs-lisp-mode)
4427 (emacs-lisp-mode))
478834e6
TB
4428 (let ((inhibit-read-only t)
4429 (print-length nil)
4430 (print-level nil))
f8d869d1
RS
4431 (custom-save-variables)
4432 (custom-save-faces))
4433 (let ((file-precious-flag t))
4434 (save-buffer))
ac183163
CY
4435 (if old-buffer
4436 (progn
4437 (set-visited-file-name old-buffer-name)
4438 (set-buffer-modified-p nil))
f8d869d1 4439 (kill-buffer (current-buffer))))))
16ecd4c5
RS
4440
4441;;;###autoload
4442(defun customize-save-customized ()
4443 "Save all user options which have been set in this session."
4444 (interactive)
4445 (mapatoms (lambda (symbol)
4446 (let ((face (get symbol 'customized-face))
4447 (value (get symbol 'customized-value))
4448 (face-comment (get symbol 'customized-face-comment))
4449 (variable-comment
4450 (get symbol 'customized-variable-comment)))
4451 (when face
4452 (put symbol 'saved-face face)
4453 (custom-push-theme 'theme-face symbol 'user 'set value)
4454 (put symbol 'customized-face nil))
4455 (when value
4456 (put symbol 'saved-value value)
4457 (custom-push-theme 'theme-value symbol 'user 'set value)
4458 (put symbol 'customized-value nil))
4459 (when variable-comment
4460 (put symbol 'saved-variable-comment variable-comment)
4461 (put symbol 'customized-variable-comment nil))
4462 (when face-comment
4463 (put symbol 'saved-face-comment face-comment)
4464 (put symbol 'customized-face-comment nil)))))
4465 ;; We really should update all custom buffers here.
4466 (custom-save-all))
f8d869d1
RS
4467\f
4468;; Editing the custom file contents in a buffer.
4469
d543e20b 4470(defun custom-save-delete (symbol)
f8d869d1 4471 "Delete all calls to SYMBOL from the contents of the current buffer.
a34511a1 4472Leave point at the old location of the first such call,
f8d869d1
RS
4473or (if there were none) at the end of the buffer.
4474
4475This function does not save the buffer."
d543e20b 4476 (goto-char (point-min))
cbe8bb8e
KH
4477 ;; Skip all whitespace and comments.
4478 (while (forward-comment 1))
4479 (or (eobp)
4480 (save-excursion (forward-sexp (buffer-size)))) ; Test for scan errors.
a34511a1
RS
4481 (let (first)
4482 (catch 'found
4483 (while t ;; We exit this loop only via throw.
4484 ;; Skip all whitespace and comments.
4485 (while (forward-comment 1))
4486 (let ((start (point))
4487 (sexp (condition-case nil
4488 (read (current-buffer))
4489 (end-of-file (throw 'found nil)))))
4490 (when (and (listp sexp)
4491 (eq (car sexp) symbol))
4492 (delete-region start (point))
4493 (unless first
4494 (setq first (point)))))))
4495 (if first
4496 (goto-char first)
189638d5
GM
4497 ;; Move in front of local variables, otherwise long Custom
4498 ;; entries would make them ineffective.
4499 (let ((pos (point-max))
4500 (case-fold-search t))
4501 (save-excursion
4502 (goto-char (point-max))
4503 (search-backward "\n\^L" (max (- (point-max) 3000) (point-min))
4504 'move)
4505 (when (search-forward "Local Variables:" nil t)
4506 (setq pos (line-beginning-position))))
4507 (goto-char pos)))))
d543e20b
PA
4508
4509(defun custom-save-variables ()
4510 "Save all customized variables in `custom-file'."
4511 (save-excursion
4512 (custom-save-delete 'custom-set-variables)
d151422c
MR
4513 (let ((standard-output (current-buffer))
4514 (saved-list (make-list 1 0))
4515 sort-fold-case)
4516 ;; First create a sorted list of saved variables.
4517 (mapatoms
4518 (lambda (symbol)
d358aa10 4519 (if (and (get symbol 'saved-value)
2d656942
CY
4520 ;; ignore theme values
4521 (or (null (get symbol 'theme-value))
4522 (eq 'user (caar (get symbol 'theme-value)))))
d151422c
MR
4523 (nconc saved-list (list symbol)))))
4524 (setq saved-list (sort (cdr saved-list) 'string<))
d543e20b
PA
4525 (unless (bolp)
4526 (princ "\n"))
aec2bb63 4527 (princ "(custom-set-variables
61b6abbd
AL
4528 ;; custom-set-variables was added by Custom.
4529 ;; If you edit it by hand, you could mess it up, so be careful.
4530 ;; Your init file should contain only one such instance.
4531 ;; If there is more than one, they won't work right.\n")
d9f67134
SM
4532 (dolist (symbol saved-list)
4533 (let ((spec (car-safe (get symbol 'theme-value)))
4534 (value (get symbol 'saved-value))
4535 (requests (get symbol 'custom-requests))
adcc7a37
RS
4536 (now (and (not (custom-variable-p symbol))
4537 (or (boundp symbol)
4538 (eq (get symbol 'force-value)
4539 'rogue))))
40627755 4540 (comment (get symbol 'saved-variable-comment)))
c8c9333c 4541 ;; Check REQUESTS for validity.
d9f67134
SM
4542 (dolist (request requests)
4543 (when (and (symbolp request) (not (featurep request)))
4544 (message "Unknown requested feature: %s" request)
4545 (setq requests (delq request requests))))
adcc7a37 4546 ;; Is there anything customized about this variable?
d358aa10 4547 (when (or (and spec (eq (car spec) 'user))
d9f67134
SM
4548 comment
4549 (and (null spec) (get symbol 'saved-value)))
adcc7a37
RS
4550 ;; Output an element for this variable.
4551 ;; It has the form (SYMBOL VALUE-FORM NOW REQUESTS COMMENT).
4552 ;; SYMBOL is the variable name.
4553 ;; VALUE-FORM is an expression to return the customized value.
4554 ;; NOW if non-nil means always set the variable immediately
4555 ;; when the customizations are reloaded. This is used
4556 ;; for rogue variables
4557 ;; REQUESTS is a list of packages to load before setting the
4558 ;; variable. Each element of it will be passed to `require'.
4559 ;; COMMENT is whatever comment the user has specified
4560 ;; with the customize facility.
d9f67134
SM
4561 (unless (bolp)
4562 (princ "\n"))
4563 (princ " '(")
4564 (prin1 symbol)
4565 (princ " ")
4566 (prin1 (car value))
4567 (when (or now requests comment)
4568 (princ " ")
4569 (prin1 now)
4570 (when (or requests comment)
4571 (princ " ")
4572 (prin1 requests)
4573 (when comment
4574 (princ " ")
4575 (prin1 comment))))
4576 (princ ")"))))
a34511a1
RS
4577 (if (bolp)
4578 (princ " "))
d543e20b
PA
4579 (princ ")")
4580 (unless (looking-at "\n")
4581 (princ "\n")))))
4582
4583(defun custom-save-faces ()
4584 "Save all customized faces in `custom-file'."
4585 (save-excursion
c942535f 4586 (custom-save-delete 'custom-reset-faces)
d543e20b 4587 (custom-save-delete 'custom-set-faces)
d151422c
MR
4588 (let ((standard-output (current-buffer))
4589 (saved-list (make-list 1 0))
4590 sort-fold-case)
4591 ;; First create a sorted list of saved faces.
4592 (mapatoms
4593 (lambda (symbol)
d358aa10
CY
4594 (if (and (get symbol 'saved-face)
4595 (eq 'user (car (car-safe (get symbol 'theme-face)))))
d151422c
MR
4596 (nconc saved-list (list symbol)))))
4597 (setq saved-list (sort (cdr saved-list) 'string<))
4598 ;; The default face must be first, since it affects the others.
4599 (if (memq 'default saved-list)
4600 (setq saved-list (cons 'default (delq 'default saved-list))))
d543e20b
PA
4601 (unless (bolp)
4602 (princ "\n"))
aec2bb63 4603 (princ "(custom-set-faces
61b6abbd
AL
4604 ;; custom-set-faces was added by Custom.
4605 ;; If you edit it by hand, you could mess it up, so be careful.
4606 ;; Your init file should contain only one such instance.
4607 ;; If there is more than one, they won't work right.\n")
d9f67134
SM
4608 (dolist (symbol saved-list)
4609 (let ((spec (car-safe (get symbol 'theme-face)))
4610 (value (get symbol 'saved-face))
4611 (now (not (or (get symbol 'face-defface-spec)
4612 (and (not (custom-facep symbol))
4613 (not (get symbol 'force-face))))))
4614 (comment (get symbol 'saved-face-comment)))
d358aa10 4615 (when (or (and spec (eq (nth 0 spec) 'user))
d9f67134
SM
4616 comment
4617 (and (null spec) (get symbol 'saved-face)))
4618 ;; Don't print default face here.
4619 (unless (bolp)
4620 (princ "\n"))
4621 (princ " '(")
4622 (prin1 symbol)
4623 (princ " ")
4624 (prin1 value)
4625 (when (or now comment)
4626 (princ " ")
4627 (prin1 now)
4628 (when comment
4629 (princ " ")
4630 (prin1 comment)))
4631 (princ ")"))))
a34511a1
RS
4632 (if (bolp)
4633 (princ " "))
d543e20b
PA
4634 (princ ")")
4635 (unless (looking-at "\n")
08b4ae6c 4636 (princ "\n")))))
f8d869d1 4637\f
d543e20b
PA
4638;;; The Customize Menu.
4639
bd042c03
PA
4640;;; Menu support
4641
25ac13b5
PA
4642(defcustom custom-menu-nesting 2
4643 "Maximum nesting in custom menus."
4644 :type 'integer
6aaedd12 4645 :group 'custom-menu)
d543e20b
PA
4646
4647(defun custom-face-menu-create (widget symbol)
4648 "Ignoring WIDGET, create a menu entry for customization face SYMBOL."
4649 (vector (custom-unlispify-menu-entry symbol)
86bd10bc 4650 `(customize-face ',symbol)
d543e20b
PA
4651 t))
4652
4653(defun custom-variable-menu-create (widget symbol)
4654 "Ignoring WIDGET, create a menu entry for customization variable SYMBOL."
4655 (let ((type (get symbol 'custom-type)))
4656 (unless (listp type)
4657 (setq type (list type)))
4658 (if (and type (widget-get type :custom-menu))
4659 (widget-apply type :custom-menu symbol)
4660 (vector (custom-unlispify-menu-entry symbol)
86bd10bc 4661 `(customize-variable ',symbol)
d543e20b
PA
4662 t))))
4663
bd042c03 4664;; Add checkboxes to boolean variable entries.
d543e20b
PA
4665(widget-put (get 'boolean 'widget-type)
4666 :custom-menu (lambda (widget symbol)
4667 (vector (custom-unlispify-menu-entry symbol)
86bd10bc 4668 `(customize-variable ',symbol)
d543e20b
PA
4669 ':style 'toggle
4670 ':selected symbol)))
4671
d04a3972
DL
4672(defun custom-group-menu-create (widget symbol)
4673 "Ignoring WIDGET, create a menu entry for customization group SYMBOL."
4674 `( ,(custom-unlispify-menu-entry symbol t)
4675 :filter (lambda (&rest junk)
809f0c35 4676 (let* ((menu (custom-menu-create ',symbol)))
3b2f3d30 4677 (if (consp menu) (cdr menu) menu)))))
d543e20b 4678
bd042c03
PA
4679;;;###autoload
4680(defun custom-menu-create (symbol)
d543e20b 4681 "Create menu for customization group SYMBOL.
d543e20b 4682The menu is in a format applicable to `easy-menu-define'."
809f0c35
RS
4683 (let* ((deactivate-mark nil)
4684 (item (vector (custom-unlispify-menu-entry symbol)
86bd10bc 4685 `(customize-group ',symbol)
bd042c03
PA
4686 t)))
4687 (if (and (or (not (boundp 'custom-menu-nesting))
4688 (>= custom-menu-nesting 0))
2de2cb02
MR
4689 (progn
4690 (custom-load-symbol symbol)
4691 (< (length (get symbol 'custom-group)) widget-menu-max-size)))
d543e20b 4692 (let ((custom-prefix-list (custom-prefix-add symbol
25ac13b5 4693 custom-prefix-list))
da5ec617
PA
4694 (members (custom-sort-items (get symbol 'custom-group)
4695 custom-menu-sort-alphabetically
4696 custom-menu-order-groups)))
d543e20b
PA
4697 `(,(custom-unlispify-menu-entry symbol t)
4698 ,item
4699 "--"
4700 ,@(mapcar (lambda (entry)
4701 (widget-apply (if (listp (nth 1 entry))
4702 (nth 1 entry)
4703 (list (nth 1 entry)))
4704 :custom-menu (nth 0 entry)))
25ac13b5 4705 members)))
d543e20b
PA
4706 item)))
4707
4708;;;###autoload
bd042c03
PA
4709(defun customize-menu-create (symbol &optional name)
4710 "Return a customize menu for customization group SYMBOL.
d3d4df42 4711If optional NAME is given, use that as the name of the menu.
bd042c03
PA
4712Otherwise the menu will be named `Customize'.
4713The format is suitable for use with `easy-menu-define'."
4714 (unless name
4715 (setq name "Customize"))
d04a3972
DL
4716 `(,name
4717 :filter (lambda (&rest junk)
3b2f3d30
SM
4718 (let ((menu (custom-menu-create ',symbol)))
4719 (if (consp menu) (cdr menu) menu)))))
d543e20b 4720
9db1942d
CY
4721;;; Toolbar and menubar support
4722
4723(easy-menu-define
34cf517c 4724 Custom-mode-menu (list custom-mode-map custom-field-keymap)
9db1942d
CY
4725 "Menu used in customization buffers."
4726 (nconc (list "Custom"
4727 (customize-menu-create 'customize))
4728 (mapcar (lambda (arg)
4729 (let ((tag (nth 0 arg))
4730 (command (nth 1 arg))
4731 (active (nth 2 arg))
4732 (help (nth 3 arg)))
4733 (vector tag command :active (eval active) :help help)))
4734 custom-commands)))
4735
4736(defvar tool-bar-map)
2614ccc3
MR
4737
4738;;; `custom-tool-bar-map' used to be set up here. This will fail to
4739;;; DTRT when `display-graphic-p' returns nil during compilation. Hence
6aec3b9d 4740;;; we set this up lazily in `Custom-mode'.
2614ccc3
MR
4741(defvar custom-tool-bar-map nil
4742 "Keymap for toolbar in Custom mode.")
9db1942d 4743
bd042c03
PA
4744;;; The Custom Mode.
4745
adcc7a37 4746(defun Custom-no-edit (pos &optional event)
7eb944cf
RS
4747 "Invoke button at POS, or refuse to allow editing of Custom buffer."
4748 (interactive "@d")
a8da2abd 4749 (error "You can't edit this part of the Custom buffer"))
0ce026b1 4750
adcc7a37 4751(defun Custom-newline (pos &optional event)
19f0515a
RS
4752 "Invoke button at POS, or refuse to allow editing of Custom buffer."
4753 (interactive "@d")
4754 (let ((button (get-char-property pos 'button)))
4755 (if button
4756 (widget-apply-action button event)
4757 (error "You can't edit this part of the Custom buffer"))))
4758
b62c92bb
RS
4759(defun Custom-goto-parent ()
4760 "Go to the parent group listed at the top of this buffer.
4761If several parents are listed, go to the first of them."
4762 (interactive)
4763 (save-excursion
4764 (goto-char (point-min))
d84fcc30 4765 (if (search-forward "\nParent groups: " nil t)
b62c92bb
RS
4766 (let* ((button (get-char-property (point) 'button))
4767 (parent (downcase (widget-get button :tag))))
4768 (customize-group parent)))))
4769
6aec3b9d 4770(defcustom Custom-mode-hook nil
d3d4df42 4771 "Hook called when entering Custom mode."
bd042c03 4772 :type 'hook
6aec3b9d 4773 :group 'custom-buffer)
bd042c03 4774
b62c92bb
RS
4775(defun custom-state-buffer-message (widget)
4776 (if (eq (widget-get (widget-get widget :parent) :custom-state) 'modified)
4777 (message "To install your edits, invoke [State] and choose the Set operation")))
8691cfa7 4778
6b09b5d1
CY
4779(defun custom--initialize-widget-variables ()
4780 (set (make-local-variable 'widget-documentation-face) 'custom-documentation)
4781 (set (make-local-variable 'widget-button-face) custom-button)
4782 (set (make-local-variable 'widget-button-pressed-face) custom-button-pressed)
4783 (set (make-local-variable 'widget-mouse-face) custom-button-mouse)
4784 ;; We need this because of the "More" button on docstrings.
4785 ;; Otherwise clicking on "More" can push point offscreen, which
4786 ;; causes the window to recenter on point, which pushes the
4787 ;; newly-revealed docstring offscreen; which is annoying. -- cyd.
4788 (set (make-local-variable 'widget-button-click-moves-point) t)
4789 ;; When possible, use relief for buttons, not bracketing. This test
4790 ;; may not be optimal.
4791 (when custom-raised-buttons
4792 (set (make-local-variable 'widget-push-button-prefix) "")
4793 (set (make-local-variable 'widget-push-button-suffix) "")
4794 (set (make-local-variable 'widget-link-prefix) "")
4795 (set (make-local-variable 'widget-link-suffix) ""))
4796 (setq show-trailing-whitespace nil))
4797
6aec3b9d 4798(define-derived-mode Custom-mode nil "Custom"
bd042c03
PA
4799 "Major mode for editing customization buffers.
4800
4801The following commands are available:
4802
192e44fc
JL
4803\\<widget-keymap>\
4804Move to next button, link or editable field. \\[widget-forward]
fd40bad4 4805Move to previous button, link or editable field. \\[widget-backward]
192e44fc 4806\\<custom-field-keymap>\
4ee1cf9f
PA
4807Complete content of editable text field. \\[widget-complete]
4808\\<custom-mode-map>\
9ca66103 4809Invoke button under the mouse pointer. \\[widget-button-click]
192e44fc 4810Invoke button under point. \\[widget-button-press]
107736a2
RS
4811Set all options from current text. \\[Custom-set]
4812Make values in current text permanent. \\[Custom-save]
192e44fc
JL
4813Make text match actual option values. \\[Custom-reset-current]
4814Reset options to permanent settings. \\[Custom-reset-saved]
107736a2
RS
4815Erase customizations; set options
4816 and buffer text to the standard values. \\[Custom-reset-standard]
bd042c03 4817
6aec3b9d 4818Entry to this mode calls the value of `Custom-mode-hook'
bd042c03 4819if that value is non-nil."
bd042c03 4820 (use-local-map custom-mode-map)
ab678382 4821 (easy-menu-add Custom-mode-menu)
8db5a4fe
CY
4822 (set (make-local-variable 'tool-bar-map)
4823 (or custom-tool-bar-map
4824 ;; Set up `custom-tool-bar-map'.
4825 (let ((map (make-sparse-keymap)))
4826 (mapc
4827 (lambda (arg)
4828 (tool-bar-local-item-from-menu
f904c0f9
JD
4829 (nth 1 arg) (nth 4 arg) map custom-mode-map
4830 :label (nth 5 arg)))
8db5a4fe
CY
4831 custom-commands)
4832 (setq custom-tool-bar-map map))))
bd042c03 4833 (make-local-variable 'custom-options)
9b7826f4 4834 (make-local-variable 'custom-local-buffer)
6b09b5d1 4835 (custom--initialize-widget-variables)
d1a3873f 4836 (add-hook 'widget-edit-functions 'custom-state-buffer-message nil t))
d543e20b 4837
6aec3b9d
JL
4838(put 'Custom-mode 'mode-class 'special)
4839
4840;; backward-compatibility
4841(defun custom-mode ()
4842 "Non-interactive variant of `Custom-mode'."
4843 (Custom-mode))
82c02591 4844(make-obsolete 'custom-mode 'Custom-mode "23.1")
7f352f86 4845(put 'custom-mode 'mode-class 'special)
82c02591 4846(define-obsolete-variable-alias 'custom-mode-hook 'Custom-mode-hook "23.1")
7f352f86 4847
81711dba
EZ
4848(dolist (regexp
4849 '("^No user option defaults have been changed since Emacs "
4850 "^Invalid face:? "
4851 "^No \\(?:customized\\|rogue\\|saved\\) user options"
4852 "^No customizable items matching "
4853 "^There are unset changes"
4854 "^Cannot set hidden variable"
4855 "^No \\(?:saved\\|backup\\) value for "
4856 "^No standard setting known for "
4857 "^No standard setting for this face"
4858 "^Saving settings from \"emacs -q\" would overwrite existing customizations"))
4859 (add-to-list 'debug-ignored-errors regexp))
2365594b 4860
d543e20b
PA
4861;;; The End.
4862
4863(provide 'cus-edit)
4864
d3d4df42 4865;;; cus-edit.el ends here