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