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