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