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