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