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