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