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