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