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