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