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