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