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