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