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