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