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