[HAVE_UNISTD_H]: Include unistd.h.
[emacs.git] / lisp / cus-edit.el
blob1b3cebb69621ed4a8be4c676378b6d41059e6179
1 ;;; cus-edit.el --- Tools for customizating Emacs and Lisp packages.
2 ;;
3 ;; Copyright (C) 1996, 1997 Free Software Foundation, Inc.
4 ;;
5 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
6 ;; Keywords: help, faces
7 ;; Version: 1.9944
8 ;; X-URL: http://www.dina.kvl.dk/~abraham/custom/
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
27 ;;; Commentary:
29 ;; This file implements the code to create and edit customize buffers.
30 ;;
31 ;; See `custom.el'.
33 ;; No commands should have names starting with `custom-' because
34 ;; that interferes with completion. Use `customize-' for commands
35 ;; that the user will run with M-x, and `Custom-' for interactive commands.
37 ;;; Code:
39 (require 'cus-face)
40 (require 'wid-edit)
41 (require 'easymenu)
42 (eval-when-compile (require 'cl))
44 (condition-case nil
45 (require 'cus-load)
46 (error nil))
48 (condition-case nil
49 (require 'cus-start)
50 (error nil))
52 (define-widget-keywords :custom-last :custom-prefix :custom-category
53 :custom-prefixes :custom-menu
54 :custom-show
55 :custom-magic :custom-state :custom-level :custom-form
56 :custom-set :custom-save :custom-reset-current :custom-reset-saved
57 :custom-reset-standard)
59 (put 'custom-define-hook 'custom-type 'hook)
60 (put 'custom-define-hook 'standard-value '(nil))
61 (custom-add-to-group 'customize 'custom-define-hook 'custom-variable)
63 ;;; Customization Groups.
65 (defgroup emacs nil
66 "Customization of the One True Editor."
67 :link '(custom-manual "(emacs)Top"))
69 ;; Most of these groups are stolen from `finder.el',
70 (defgroup editing nil
71 "Basic text editing facilities."
72 :group 'emacs)
74 (defgroup abbrev nil
75 "Abbreviation handling, typing shortcuts, macros."
76 :tag "Abbreviations"
77 :group 'editing)
79 (defgroup matching nil
80 "Various sorts of searching and matching."
81 :group 'editing)
83 (defgroup emulations nil
84 "Emulations of other editors."
85 :group 'editing)
87 (defgroup mouse nil
88 "Mouse support."
89 :group 'editing)
91 (defgroup outlines nil
92 "Support for hierarchical outlining."
93 :group 'editing)
95 (defgroup external nil
96 "Interfacing to external utilities."
97 :group 'emacs)
99 (defgroup bib nil
100 "Code related to the `bib' bibliography processor."
101 :tag "Bibliography"
102 :group 'external)
104 (defgroup processes nil
105 "Process, subshell, compilation, and job control support."
106 :group 'external
107 :group 'development)
109 (defgroup programming nil
110 "Support for programming in other languages."
111 :group 'emacs)
113 (defgroup languages nil
114 "Specialized modes for editing programming languages."
115 :group 'programming)
117 (defgroup lisp nil
118 "Lisp support, including Emacs Lisp."
119 :group 'languages
120 :group 'development)
122 (defgroup c nil
123 "Support for the C language and related languages."
124 :group 'languages)
126 (defgroup tools nil
127 "Programming tools."
128 :group 'programming)
130 (defgroup oop nil
131 "Support for object-oriented programming."
132 :group 'programming)
134 (defgroup applications nil
135 "Applications written in Emacs."
136 :group 'emacs)
138 (defgroup calendar nil
139 "Calendar and time management support."
140 :group 'applications)
142 (defgroup mail nil
143 "Modes for electronic-mail handling."
144 :group 'applications)
146 (defgroup news nil
147 "Support for netnews reading and posting."
148 :group 'applications)
150 (defgroup games nil
151 "Games, jokes and amusements."
152 :group 'applications)
154 (defgroup development nil
155 "Support for further development of Emacs."
156 :group 'emacs)
158 (defgroup docs nil
159 "Support for Emacs documentation."
160 :group 'development)
162 (defgroup extensions nil
163 "Emacs Lisp language extensions."
164 :group 'development)
166 (defgroup internal nil
167 "Code for Emacs internals, build process, defaults."
168 :group 'development)
170 (defgroup maint nil
171 "Maintenance aids for the Emacs development group."
172 :tag "Maintenance"
173 :group 'development)
175 (defgroup environment nil
176 "Fitting Emacs with its environment."
177 :group 'emacs)
179 (defgroup comm nil
180 "Communications, networking, remote access to files."
181 :tag "Communication"
182 :group 'environment)
184 (defgroup hardware nil
185 "Support for interfacing with exotic hardware."
186 :group 'environment)
188 (defgroup terminals nil
189 "Support for terminal types."
190 :group 'environment)
192 (defgroup unix nil
193 "Front-ends/assistants for, or emulators of, UNIX features."
194 :group 'environment)
196 (defgroup vms nil
197 "Support code for vms."
198 :group 'environment)
200 (defgroup i18n nil
201 "Internationalization and alternate character-set support."
202 :group 'environment
203 :group 'editing)
205 (defgroup x nil
206 "The X Window system."
207 :group 'environment)
209 (defgroup frames nil
210 "Support for Emacs frames and window systems."
211 :group 'environment)
213 (defgroup data nil
214 "Support editing files of data."
215 :group 'emacs)
217 (defgroup files nil
218 "Support editing files."
219 :group 'emacs)
221 (defgroup wp nil
222 "Word processing."
223 :group 'emacs)
225 (defgroup tex nil
226 "Code related to the TeX formatter."
227 :group 'wp)
229 (defgroup faces nil
230 "Support for multiple fonts."
231 :group 'emacs)
233 (defgroup hypermedia nil
234 "Support for links between text or other media types."
235 :group 'emacs)
237 (defgroup help nil
238 "Support for on-line help systems."
239 :group 'emacs)
241 (defgroup local nil
242 "Code local to your site."
243 :group 'emacs)
245 (defgroup customize '((widgets custom-group))
246 "Customization of the Customization support."
247 :link '(custom-manual "(custom)Top")
248 :link '(url-link :tag "Development Page"
249 "http://www.dina.kvl.dk/~abraham/custom/")
250 :prefix "custom-"
251 :group 'help)
253 (defgroup custom-faces nil
254 "Faces used by customize."
255 :group 'customize
256 :group 'faces)
258 (defgroup custom-browse nil
259 "Control customize browser."
260 :prefix "custom-"
261 :group 'customize)
263 (defgroup custom-buffer nil
264 "Control customize buffers."
265 :prefix "custom-"
266 :group 'customize)
268 (defgroup custom-menu nil
269 "Control customize menus."
270 :prefix "custom-"
271 :group 'customize)
273 (defgroup abbrev-mode nil
274 "Word abbreviations mode."
275 :group 'abbrev)
277 (defgroup alloc nil
278 "Storage allocation and gc for GNU Emacs Lisp interpreter."
279 :tag "Storage Allocation"
280 :group 'internal)
282 (defgroup undo nil
283 "Undoing changes in buffers."
284 :group 'editing)
286 (defgroup modeline nil
287 "Content of the modeline."
288 :group 'environment)
290 (defgroup fill nil
291 "Indenting and filling text."
292 :group 'editing)
294 (defgroup editing-basics nil
295 "Most basic editing facilities."
296 :group 'editing)
298 (defgroup display nil
299 "How characters are displayed in buffers."
300 :group 'environment)
302 (defgroup execute nil
303 "Executing external commands."
304 :group 'processes)
306 (defgroup installation nil
307 "The Emacs installation."
308 :group 'environment)
310 (defgroup dired nil
311 "Directory editing."
312 :group 'environment)
314 (defgroup limits nil
315 "Internal Emacs limits."
316 :group 'internal)
318 (defgroup debug nil
319 "Debugging Emacs itself."
320 :group 'development)
322 (defgroup minibuffer nil
323 "Controling the behaviour of the minibuffer."
324 :group 'environment)
326 (defgroup keyboard nil
327 "Input from the keyboard."
328 :group 'environment)
330 (defgroup mouse nil
331 "Input from the mouse."
332 :group 'environment)
334 (defgroup menu nil
335 "Input from the menus."
336 :group 'environment)
338 (defgroup auto-save nil
339 "Preventing accidential loss of data."
340 :group 'files)
342 (defgroup processes-basics nil
343 "Basic stuff dealing with processes."
344 :group 'processes)
346 (defgroup mule nil
347 "MULE Emacs internationalization."
348 :group 'i18n)
350 (defgroup windows nil
351 "Windows within a frame."
352 :group 'environment)
354 ;;; Utilities.
356 (defun custom-last (x &optional n)
357 ;; Stolen from `cl.el'.
358 "Returns the last link in the list LIST.
359 With optional argument N, returns Nth-to-last link (default 1)."
360 (if n
361 (let ((m 0) (p x))
362 (while (consp p) (incf m) (pop p))
363 (if (<= n 0) p
364 (if (< n m) (nthcdr (- m n) x) x)))
365 (while (consp (cdr x)) (pop x))
368 (defun custom-quote (sexp)
369 "Quote SEXP iff it is not self quoting."
370 (if (or (memq sexp '(t nil))
371 (and (symbolp sexp)
372 (eq (aref (symbol-name sexp) 0) ?:))
373 (and (listp sexp)
374 (memq (car sexp) '(lambda)))
375 (stringp sexp)
376 (numberp sexp)
377 (and (fboundp 'characterp)
378 (characterp sexp)))
379 sexp
380 (list 'quote sexp)))
382 (defun custom-split-regexp-maybe (regexp)
383 "If REGEXP is a string, split it to a list at `\\|'.
384 You can get the original back with from the result with:
385 (mapconcat 'identity result \"\\|\")
387 IF REGEXP is not a string, return it unchanged."
388 (if (stringp regexp)
389 (let ((start 0)
390 all)
391 (while (string-match "\\\\|" regexp start)
392 (setq all (cons (substring regexp start (match-beginning 0)) all)
393 start (match-end 0)))
394 (nreverse (cons (substring regexp start) all)))
395 regexp))
397 (defun custom-variable-prompt ()
398 ;; Code stolen from `help.el'.
399 "Prompt for a variable, defaulting to the variable at point.
400 Return a list suitable for use in `interactive'."
401 (let ((v (variable-at-point))
402 (enable-recursive-minibuffers t)
403 val)
404 (setq val (completing-read
405 (if (symbolp v)
406 (format "Customize option: (default %s) " v)
407 "Customize variable: ")
408 obarray (lambda (symbol)
409 (and (boundp symbol)
410 (or (get symbol 'custom-type)
411 (user-variable-p symbol))))))
412 (list (if (equal val "")
413 (if (symbolp v) v nil)
414 (intern val)))))
416 (defun custom-menu-filter (menu widget)
417 "Convert MENU to the form used by `widget-choose'.
418 MENU should be in the same format as `custom-variable-menu'.
419 WIDGET is the widget to apply the filter entries of MENU on."
420 (let ((result nil)
421 current name action filter)
422 (while menu
423 (setq current (car menu)
424 name (nth 0 current)
425 action (nth 1 current)
426 filter (nth 2 current)
427 menu (cdr menu))
428 (if (or (null filter) (funcall filter widget))
429 (push (cons name action) result)
430 (push name result)))
431 (nreverse result)))
433 ;;; Unlispify.
435 (defvar custom-prefix-list nil
436 "List of prefixes that should be ignored by `custom-unlispify'")
438 (defcustom custom-unlispify-menu-entries t
439 "Display menu entries as words instead of symbols if non nil."
440 :group 'custom-menu
441 :type 'boolean)
443 (defcustom custom-unlispify-remove-prefixes nil
444 "Non-nil means remove group prefixes from option names in buffer."
445 :group 'custom-menu
446 :type 'boolean)
448 (defun custom-unlispify-menu-entry (symbol &optional no-suffix)
449 "Convert symbol into a menu entry."
450 (cond ((not custom-unlispify-menu-entries)
451 (symbol-name symbol))
452 ((get symbol 'custom-tag)
453 (if no-suffix
454 (get symbol 'custom-tag)
455 (concat (get symbol 'custom-tag) "...")))
457 (save-excursion
458 (set-buffer (get-buffer-create " *Custom-Work*"))
459 (erase-buffer)
460 (princ symbol (current-buffer))
461 (goto-char (point-min))
462 (when (and (eq (get symbol 'custom-type) 'boolean)
463 (re-search-forward "-p\\'" nil t))
464 (replace-match "" t t)
465 (goto-char (point-min)))
466 (if custom-unlispify-remove-prefixes
467 (let ((prefixes custom-prefix-list)
468 prefix)
469 (while prefixes
470 (setq prefix (car prefixes))
471 (if (search-forward prefix (+ (point) (length prefix)) t)
472 (progn
473 (setq prefixes nil)
474 (delete-region (point-min) (point)))
475 (setq prefixes (cdr prefixes))))))
476 (subst-char-in-region (point-min) (point-max) ?- ?\ t)
477 (capitalize-region (point-min) (point-max))
478 (unless no-suffix
479 (goto-char (point-max))
480 (insert "..."))
481 (buffer-string)))))
483 (defcustom custom-unlispify-tag-names t
484 "Display tag names as words instead of symbols if non nil."
485 :group 'custom-buffer
486 :type 'boolean)
488 (defun custom-unlispify-tag-name (symbol)
489 "Convert symbol into a menu entry."
490 (let ((custom-unlispify-menu-entries custom-unlispify-tag-names))
491 (custom-unlispify-menu-entry symbol t)))
493 (defun custom-prefix-add (symbol prefixes)
494 ;; Addd SYMBOL to list of ignored PREFIXES.
495 (cons (or (get symbol 'custom-prefix)
496 (concat (symbol-name symbol) "-"))
497 prefixes))
499 ;;; Guess.
501 (defcustom custom-guess-name-alist
502 '(("-p\\'" boolean)
503 ("-hook\\'" hook)
504 ("-face\\'" face)
505 ("-file\\'" file)
506 ("-function\\'" function)
507 ("-functions\\'" (repeat function))
508 ("-list\\'" (repeat sexp))
509 ("-alist\\'" (repeat (cons sexp sexp))))
510 "Alist of (MATCH TYPE).
512 MATCH should be a regexp matching the name of a symbol, and TYPE should
513 be a widget suitable for editing the value of that symbol. The TYPE
514 of the first entry where MATCH matches the name of the symbol will be
515 used.
517 This is used for guessing the type of variables not declared with
518 customize."
519 :type '(repeat (group (regexp :tag "Match") (sexp :tag "Type")))
520 :group 'customize)
522 (defcustom custom-guess-doc-alist
523 '(("\\`\\*?Non-nil " boolean))
524 "Alist of (MATCH TYPE).
526 MATCH should be a regexp matching a documentation string, and TYPE
527 should be a widget suitable for editing the value of a variable with
528 that documentation string. The TYPE of the first entry where MATCH
529 matches the name of the symbol will be used.
531 This is used for guessing the type of variables not declared with
532 customize."
533 :type '(repeat (group (regexp :tag "Match") (sexp :tag "Type")))
534 :group 'customize)
536 (defun custom-guess-type (symbol)
537 "Guess a widget suitable for editing the value of SYMBOL.
538 This is done by matching SYMBOL with `custom-guess-name-alist' and
539 if that fails, the doc string with `custom-guess-doc-alist'."
540 (let ((name (symbol-name symbol))
541 (names custom-guess-name-alist)
542 current found)
543 (while names
544 (setq current (car names)
545 names (cdr names))
546 (when (string-match (nth 0 current) name)
547 (setq found (nth 1 current)
548 names nil)))
549 (unless found
550 (let ((doc (documentation-property symbol 'variable-documentation))
551 (docs custom-guess-doc-alist))
552 (when doc
553 (while docs
554 (setq current (car docs)
555 docs (cdr docs))
556 (when (string-match (nth 0 current) doc)
557 (setq found (nth 1 current)
558 docs nil))))))
559 found))
561 ;;; Sorting.
563 (defcustom custom-browse-sort-alphabetically nil
564 "If non-nil, sort members of each customization group alphabetically."
565 :type 'boolean
566 :group 'custom-browse)
568 (defcustom custom-browse-order-groups nil
569 "If non-nil, order group members within each customization group.
570 If `first', order groups before non-groups.
571 If `last', order groups after non-groups."
572 :type '(choice (const first)
573 (const last)
574 (const :tag "none" nil))
575 :group 'custom-browse)
577 (defcustom custom-browse-only-groups nil
578 "If non-nil, show group members only within each customization group."
579 :type 'boolean
580 :group 'custom-browse)
582 (defcustom custom-buffer-sort-alphabetically nil
583 "If non-nil, sort members of each customization group alphabetically."
584 :type 'boolean
585 :group 'custom-buffer)
587 (defcustom custom-buffer-order-groups 'last
588 "If non-nil, order group members within each customization group.
589 If `first', order groups before non-groups.
590 If `last', order groups after non-groups."
591 :type '(choice (const first)
592 (const last)
593 (const :tag "none" nil))
594 :group 'custom-buffer)
596 (defcustom custom-menu-sort-alphabetically nil
597 "If non-nil, sort members of each customization group alphabetically."
598 :type 'boolean
599 :group 'custom-menu)
601 (defcustom custom-menu-order-groups 'first
602 "If non-nil, order group members within each customization group.
603 If `first', order groups before non-groups.
604 If `last', order groups after non-groups."
605 :type '(choice (const first)
606 (const last)
607 (const :tag "none" nil))
608 :group 'custom-menu)
610 (defun custom-sort-items (items sort-alphabetically order-groups)
611 "Return a sorted copy of ITEMS.
612 ITEMS should be a `custom-group' property.
613 If SORT-ALPHABETICALLY non-nil, sort alphabetically.
614 If ORDER-GROUPS is `first' order groups before non-groups, if `last' order
615 groups after non-groups, if nil do not order groups at all."
616 (sort (copy-sequence items)
617 (lambda (a b)
618 (let ((typea (nth 1 a)) (typeb (nth 1 b))
619 (namea (symbol-name (nth 0 a))) (nameb (symbol-name (nth 0 b))))
620 (cond ((not order-groups)
621 ;; Since we don't care about A and B order, maybe sort.
622 (when sort-alphabetically
623 (string-lessp namea nameb)))
624 ((eq typea 'custom-group)
625 ;; If B is also a group, maybe sort. Otherwise, order A and B.
626 (if (eq typeb 'custom-group)
627 (when sort-alphabetically
628 (string-lessp namea nameb))
629 (eq order-groups 'first)))
630 ((eq typeb 'custom-group)
631 ;; Since A cannot be a group, order A and B.
632 (eq order-groups 'last))
633 (sort-alphabetically
634 ;; Since A and B cannot be groups, sort.
635 (string-lessp namea nameb)))))))
637 ;;; Custom Mode Commands.
639 (defvar custom-options nil
640 "Customization widgets in the current buffer.")
642 (defun Custom-set ()
643 "Set changes in all modified options."
644 (interactive)
645 (let ((children custom-options))
646 (mapcar (lambda (child)
647 (when (eq (widget-get child :custom-state) 'modified)
648 (widget-apply child :custom-set)))
649 children)))
651 (defun Custom-save ()
652 "Set all modified group members and save them."
653 (interactive)
654 (let ((children custom-options))
655 (mapcar (lambda (child)
656 (when (memq (widget-get child :custom-state) '(modified set))
657 (widget-apply child :custom-save)))
658 children))
659 (custom-save-all))
661 (defvar custom-reset-menu
662 '(("Current" . Custom-reset-current)
663 ("Saved" . Custom-reset-saved)
664 ("Standard Settings" . Custom-reset-standard))
665 "Alist of actions for the `Reset' button.
666 The key is a string containing the name of the action, the value is a
667 lisp function taking the widget as an element which will be called
668 when the action is chosen.")
670 (defun custom-reset (event)
671 "Select item from reset menu."
672 (let* ((completion-ignore-case t)
673 (answer (widget-choose "Reset to"
674 custom-reset-menu
675 event)))
676 (if answer
677 (funcall answer))))
679 (defun Custom-reset-current (&rest ignore)
680 "Reset all modified group members to their current value."
681 (interactive)
682 (let ((children custom-options))
683 (mapcar (lambda (child)
684 (when (eq (widget-get child :custom-state) 'modified)
685 (widget-apply child :custom-reset-current)))
686 children)))
688 (defun Custom-reset-saved (&rest ignore)
689 "Reset all modified or set group members to their saved value."
690 (interactive)
691 (let ((children custom-options))
692 (mapcar (lambda (child)
693 (when (eq (widget-get child :custom-state) 'modified)
694 (widget-apply child :custom-reset-saved)))
695 children)))
697 (defun Custom-reset-standard (&rest ignore)
698 "Reset all modified, set, or saved group members to their standard settings."
699 (interactive)
700 (let ((children custom-options))
701 (mapcar (lambda (child)
702 (when (eq (widget-get child :custom-state) 'modified)
703 (widget-apply child :custom-reset-standard)))
704 children)))
706 ;;; The Customize Commands
708 (defun custom-prompt-variable (prompt-var prompt-val)
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.
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.
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 (let* ((var (read-variable prompt-var))
720 (minibuffer-help-form '(describe-variable var)))
721 (list var
722 (let ((prop (get var 'variable-interactive))
723 (type (get var 'custom-type))
724 (prompt (format prompt-val var)))
725 (unless (listp type)
726 (setq type (list type)))
727 (cond (prop
728 ;; Use VAR's `variable-interactive' property
729 ;; as an interactive spec for prompting.
730 (call-interactively (list 'lambda '(arg)
731 (list 'interactive prop)
732 'arg)))
733 (type
734 (widget-prompt-value type
735 prompt
736 (if (boundp var)
737 (symbol-value var))
738 (not (boundp var))))
740 (eval-minibuffer prompt)))))))
742 ;;;###autoload
743 (defun customize-set-value (var val)
744 "Set VARIABLE to VALUE. VALUE is a Lisp object.
746 If VARIABLE has a `variable-interactive' property, that is used as if
747 it were the arg to `interactive' (which see) to interactively read the value.
749 If VARIABLE has a `custom-type' property, it must be a widget and the
750 `:prompt-value' property of that widget will be used for reading the value."
751 (interactive (custom-prompt-variable "Set variable: "
752 "Set %s to value: "))
754 (set var val))
756 ;;;###autoload
757 (defun customize-set-variable (var val)
758 "Set the default for VARIABLE to VALUE. VALUE is a Lisp object.
760 If VARIABLE has a `custom-set' property, that is used for setting
761 VARIABLE, otherwise `set-default' is used.
763 The `customized-value' property of the VARIABLE will be set to a list
764 with a quoted VALUE as its sole list member.
766 If VARIABLE has a `variable-interactive' property, that is used as if
767 it were the arg to `interactive' (which see) to interactively read the value.
769 If VARIABLE has a `custom-type' property, it must be a widget and the
770 `:prompt-value' property of that widget will be used for reading the value. "
771 (interactive (custom-prompt-variable "Set variable: "
772 "Set customized value for %s to: "))
773 (funcall (or (get var 'custom-set) 'set-default) var val)
774 (put var 'customized-value (list (custom-quote val))))
776 ;;;###autoload
777 (defun customize ()
778 "Select a customization buffer which you can use to set user options.
779 User options are structured into \"groups\".
780 Initially the top-level group `Emacs' and its immediate subgroups
781 are shown; the contents of those subgroups are initially hidden."
782 (interactive)
783 (customize-group 'emacs))
785 ;;;###autoload
786 (defun customize-group (group)
787 "Customize GROUP, which must be a customization group."
788 (interactive (list (let ((completion-ignore-case t))
789 (completing-read "Customize group: (default emacs) "
790 obarray
791 (lambda (symbol)
792 (get symbol 'custom-group))
793 t))))
795 (when (stringp group)
796 (if (string-equal "" group)
797 (setq group 'emacs)
798 (setq group (intern group))))
799 (let ((name (format "*Customize Group: %s*"
800 (custom-unlispify-tag-name group))))
801 (if (get-buffer name)
802 (switch-to-buffer name)
803 (custom-buffer-create (list (list group 'custom-group))
804 name
805 (concat " for group "
806 (custom-unlispify-tag-name group))))))
808 ;;;###autoload
809 (defun customize-group-other-window (symbol)
810 "Customize SYMBOL, which must be a customization group."
811 (interactive (list (completing-read "Customize group: (default emacs) "
812 obarray
813 (lambda (symbol)
814 (get symbol 'custom-group))
815 t)))
817 (when (stringp symbol)
818 (if (string-equal "" symbol)
819 (setq symbol 'emacs)
820 (setq symbol (intern symbol))))
821 (custom-buffer-create-other-window
822 (list (list symbol 'custom-group))
823 (format "*Customize Group: %s*" (custom-unlispify-tag-name symbol))))
825 ;;;###autoload
826 (defalias 'customize-variable 'customize-option)
828 ;;;###autoload
829 (defun customize-option (symbol)
830 "Customize SYMBOL, which must be a user option variable."
831 (interactive (custom-variable-prompt))
832 (custom-buffer-create (list (list symbol 'custom-variable))
833 (format "*Customize Option: %s*"
834 (custom-unlispify-tag-name symbol))))
836 ;;;###autoload
837 (defalias 'customize-variable-other-window 'customize-option-other-window)
839 ;;;###autoload
840 (defun customize-option-other-window (symbol)
841 "Customize SYMBOL, which must be a user option variable.
842 Show the buffer in another window, but don't select it."
843 (interactive (custom-variable-prompt))
844 (custom-buffer-create-other-window
845 (list (list symbol 'custom-variable))
846 (format "*Customize Option: %s*" (custom-unlispify-tag-name symbol))))
848 ;;;###autoload
849 (defun customize-face (&optional symbol)
850 "Customize SYMBOL, which should be a face name or nil.
851 If SYMBOL is nil, customize all faces."
852 (interactive (list (completing-read "Customize face: (default all) "
853 obarray 'custom-facep)))
854 (if (or (null symbol) (and (stringp symbol) (zerop (length symbol))))
855 (custom-buffer-create (custom-sort-items
856 (mapcar (lambda (symbol)
857 (list symbol 'custom-face))
858 (face-list))
859 t nil)
860 "*Customize Faces*")
861 (when (stringp symbol)
862 (setq symbol (intern symbol)))
863 (unless (symbolp symbol)
864 (error "Should be a symbol %S" symbol))
865 (custom-buffer-create (list (list symbol 'custom-face))
866 (format "*Customize Face: %s*"
867 (custom-unlispify-tag-name symbol)))))
869 ;;;###autoload
870 (defun customize-face-other-window (&optional symbol)
871 "Show customization buffer for FACE in other window."
872 (interactive (list (completing-read "Customize face: "
873 obarray 'custom-facep)))
874 (if (or (null symbol) (and (stringp symbol) (zerop (length symbol))))
876 (if (stringp symbol)
877 (setq symbol (intern symbol)))
878 (unless (symbolp symbol)
879 (error "Should be a symbol %S" symbol))
880 (custom-buffer-create-other-window
881 (list (list symbol 'custom-face))
882 (format "*Customize Face: %s*" (custom-unlispify-tag-name symbol)))))
884 ;;;###autoload
885 (defun customize-customized ()
886 "Customize all user options set since the last save in this session."
887 (interactive)
888 (let ((found nil))
889 (mapatoms (lambda (symbol)
890 (and (get symbol 'customized-face)
891 (custom-facep symbol)
892 (push (list symbol 'custom-face) found))
893 (and (get symbol 'customized-value)
894 (boundp symbol)
895 (push (list symbol 'custom-variable) found))))
896 (if (not found)
897 (error "No customized user options")
898 (custom-buffer-create (custom-sort-items found t nil)
899 "*Customize Customized*"))))
901 ;;;###autoload
902 (defun customize-saved ()
903 "Customize all already saved user options."
904 (interactive)
905 (let ((found nil))
906 (mapatoms (lambda (symbol)
907 (and (get symbol 'saved-face)
908 (custom-facep symbol)
909 (push (list symbol 'custom-face) found))
910 (and (get symbol 'saved-value)
911 (boundp symbol)
912 (push (list symbol 'custom-variable) found))))
913 (if (not found )
914 (error "No saved user options")
915 (custom-buffer-create (custom-sort-items found t nil)
916 "*Customize Saved*"))))
918 ;;;###autoload
919 (defun customize-apropos (regexp &optional all)
920 "Customize all user options matching REGEXP.
921 If ALL is `options', include only options.
922 If ALL is `faces', include only faces.
923 If ALL is `groups', include only groups.
924 If ALL is t (interactively, with prefix arg), include options which are not
925 user-settable, as well as faces and groups."
926 (interactive "sCustomize regexp: \nP")
927 (let ((found nil))
928 (mapatoms (lambda (symbol)
929 (when (string-match regexp (symbol-name symbol))
930 (when (and (not (memq all '(faces options)))
931 (get symbol 'custom-group))
932 (push (list symbol 'custom-group) found))
933 (when (and (not (memq all '(options groups)))
934 (custom-facep symbol))
935 (push (list symbol 'custom-face) found))
936 (when (and (not (memq all '(groups faces)))
937 (boundp symbol)
938 (or (get symbol 'saved-value)
939 (get symbol 'standard-value)
940 (if (memq all '(nil options))
941 (user-variable-p symbol)
942 (get symbol 'variable-documentation))))
943 (push (list symbol 'custom-variable) found)))))
944 (if (not found)
945 (error "No matches")
946 (custom-buffer-create (custom-sort-items found t
947 custom-buffer-order-groups)
948 "*Customize Apropos*"))))
950 ;;;###autoload
951 (defun customize-apropos-options (regexp &optional arg)
952 "Customize all user options matching REGEXP.
953 With prefix arg, include options which are not user-settable."
954 (interactive "sCustomize regexp: \nP")
955 (customize-apropos regexp (or arg 'options)))
957 ;;;###autoload
958 (defun customize-apropos-faces (regexp)
959 "Customize all user faces matching REGEXP."
960 (interactive "sCustomize regexp: \n")
961 (customize-apropos regexp 'faces))
963 ;;;###autoload
964 (defun customize-apropos-groups (regexp)
965 "Customize all user groups matching REGEXP."
966 (interactive "sCustomize regexp: \n")
967 (customize-apropos regexp 'groups))
969 ;;; Buffer.
971 (defcustom custom-buffer-style 'links
972 "Control the presentation style for customization buffers.
973 The value should be a symbol, one of:
975 brackets: groups nest within each other with big horizontal brackets.
976 links: groups have links to subgroups."
977 :type '(radio (const brackets)
978 (const links))
979 :group 'custom-buffer)
981 (defcustom custom-buffer-indent 3
982 "Number of spaces to indent nested groups."
983 :type 'integer
984 :group 'custom-buffer)
986 ;;;###autoload
987 (defun custom-buffer-create (options &optional name description)
988 "Create a buffer containing OPTIONS.
989 Optional NAME is the name of the buffer.
990 OPTIONS should be an alist of the form ((SYMBOL WIDGET)...), where
991 SYMBOL is a customization option, and WIDGET is a widget for editing
992 that option."
993 (unless name (setq name "*Customization*"))
994 (kill-buffer (get-buffer-create name))
995 (switch-to-buffer (get-buffer-create name))
996 (custom-buffer-create-internal options description))
998 ;;;###autoload
999 (defun custom-buffer-create-other-window (options &optional name description)
1000 "Create a buffer containing OPTIONS.
1001 Optional NAME is the name of the buffer.
1002 OPTIONS should be an alist of the form ((SYMBOL WIDGET)...), where
1003 SYMBOL is a customization option, and WIDGET is a widget for editing
1004 that option."
1005 (unless name (setq name "*Customization*"))
1006 (kill-buffer (get-buffer-create name))
1007 (let ((window (selected-window)))
1008 (switch-to-buffer-other-window (get-buffer-create name))
1009 (custom-buffer-create-internal options description)
1010 (select-window window)))
1012 (defcustom custom-reset-button-menu nil
1013 "If non-nil, only show a single reset button in customize buffers.
1014 This button will have a menu with all three reset operations."
1015 :type 'boolean
1016 :group 'custom-buffer)
1018 (defun custom-buffer-create-internal (options &optional description)
1019 (message "Creating customization buffer...")
1020 (custom-mode)
1021 (widget-insert "This is a customization buffer")
1022 (if description
1023 (widget-insert description))
1024 (widget-insert ".
1025 Square brackets show active fields; type RET or click mouse-1
1026 on an active field to invoke its action. Editing an option value
1027 changes the text in the buffer; invoke the State button and
1028 choose the Set operation to set the option value.
1029 Invoke ")
1030 (widget-create 'info-link
1031 :tag "Help"
1032 :help-echo "Read the online help."
1033 "(emacs)Easy Customization")
1034 (widget-insert " for more information.\n\n")
1035 (message "Creating customization buttons...")
1036 (widget-insert "Operate on everything in this buffer:\n ")
1037 (widget-create 'push-button
1038 :tag "Set for Current Session"
1039 :help-echo "\
1040 Make your editing in this buffer take effect for this session."
1041 :action (lambda (widget &optional event)
1042 (Custom-set)))
1043 (widget-insert " ")
1044 (widget-create 'push-button
1045 :tag "Save for Future Sessions"
1046 :help-echo "\
1047 Make your editing in this buffer take effect for future Emacs sessions."
1048 :action (lambda (widget &optional event)
1049 (Custom-save)))
1050 (if custom-reset-button-menu
1051 (progn
1052 (widget-insert " ")
1053 (widget-create 'push-button
1054 :tag "Reset"
1055 :help-echo "Show a menu with reset operations."
1056 :mouse-down-action (lambda (&rest junk) t)
1057 :action (lambda (widget &optional event)
1058 (custom-reset event))))
1059 (widget-insert "\n ")
1060 (widget-create 'push-button
1061 :tag "Reset"
1062 :help-echo "\
1063 Reset all edited text in this buffer to reflect current values."
1064 :action 'Custom-reset-current)
1065 (widget-insert " ")
1066 (widget-create 'push-button
1067 :tag "Reset to Saved"
1068 :help-echo "\
1069 Reset all values in this buffer to their saved settings."
1070 :action 'Custom-reset-saved)
1071 (widget-insert " ")
1072 (widget-create 'push-button
1073 :tag "Reset to Standard"
1074 :help-echo "\
1075 Reset all values in this buffer to their standard settings."
1076 :action 'Custom-reset-standard))
1077 (widget-insert " ")
1078 (widget-create 'push-button
1079 :tag "Bury Buffer"
1080 :help-echo "Bury the buffer."
1081 :action (lambda (widget &optional event)
1082 (bury-buffer)))
1083 (widget-insert "\n\n")
1084 (message "Creating customization items...")
1085 (setq custom-options
1086 (if (= (length options) 1)
1087 (mapcar (lambda (entry)
1088 (widget-create (nth 1 entry)
1089 :documentation-shown t
1090 :custom-state 'unknown
1091 :tag (custom-unlispify-tag-name
1092 (nth 0 entry))
1093 :value (nth 0 entry)))
1094 options)
1095 (let ((count 0)
1096 (length (length options)))
1097 (mapcar (lambda (entry)
1098 (prog2
1099 (message "Creating customization items %2d%%..."
1100 (/ (* 100.0 count) length))
1101 (widget-create (nth 1 entry)
1102 :tag (custom-unlispify-tag-name
1103 (nth 0 entry))
1104 :value (nth 0 entry))
1105 (setq count (1+ count))
1106 (unless (eq (preceding-char) ?\n)
1107 (widget-insert "\n"))
1108 (widget-insert "\n")))
1109 options))))
1110 (unless (eq (preceding-char) ?\n)
1111 (widget-insert "\n"))
1112 (unless (eq custom-buffer-style 'tree)
1113 (mapcar 'custom-magic-reset custom-options))
1114 (message "Creating customization setup...")
1115 (widget-setup)
1116 (goto-char (point-min))
1117 (message "Creating customization buffer...done"))
1119 ;;; The Tree Browser.
1121 ;;;###autoload
1122 (defun customize-browse ()
1123 "Create a tree browser for the customize hierarchy."
1124 (interactive)
1125 (let ((group 'emacs))
1126 (let ((name "*Customize Browser*"))
1127 (kill-buffer (get-buffer-create name))
1128 (switch-to-buffer (get-buffer-create name)))
1129 (custom-mode)
1130 (widget-insert "\
1131 Square brackets show active fields; type RET or click mouse-1
1132 on an active field to invoke its action.
1133 Invoke [+] or [?] below to expand items, and [-] to collapse items.\n")
1134 (if custom-browse-only-groups
1135 (widget-insert "\
1136 Invoke the [Group] button below to edit that item in another window.\n\n")
1137 (widget-insert "Invoke the ")
1138 (widget-create 'item
1139 :format "%t"
1140 :tag "[Group]"
1141 :tag-glyph "folder")
1142 (widget-insert ", ")
1143 (widget-create 'item
1144 :format "%t"
1145 :tag "[Face]"
1146 :tag-glyph "face")
1147 (widget-insert ", and ")
1148 (widget-create 'item
1149 :format "%t"
1150 :tag "[Option]"
1151 :tag-glyph "option")
1152 (widget-insert " buttons below to edit that
1153 item in another window.\n\n"))
1154 (let ((custom-buffer-style 'tree))
1155 (widget-create 'custom-group
1156 :custom-last t
1157 :custom-state 'unknown
1158 :tag (custom-unlispify-tag-name group)
1159 :value group))
1160 (goto-char (point-min))))
1162 (define-widget 'custom-browse-visibility 'item
1163 "Control visibility of of items in the customize tree browser."
1164 :format "%[[%t]%]"
1165 :action 'custom-browse-visibility-action)
1167 (defun custom-browse-visibility-action (widget &rest ignore)
1168 (let ((custom-buffer-style 'tree))
1169 (custom-toggle-parent widget)))
1171 (define-widget 'custom-browse-group-tag 'push-button
1172 "Show parent in other window when activated."
1173 :tag "Group"
1174 :tag-glyph "folder"
1175 :action 'custom-browse-group-tag-action)
1177 (defun custom-browse-group-tag-action (widget &rest ignore)
1178 (let ((parent (widget-get widget :parent)))
1179 (customize-group-other-window (widget-value parent))))
1181 (define-widget 'custom-browse-variable-tag 'push-button
1182 "Show parent in other window when activated."
1183 :tag "Option"
1184 :tag-glyph "option"
1185 :action 'custom-browse-variable-tag-action)
1187 (defun custom-browse-variable-tag-action (widget &rest ignore)
1188 (let ((parent (widget-get widget :parent)))
1189 (customize-variable-other-window (widget-value parent))))
1191 (define-widget 'custom-browse-face-tag 'push-button
1192 "Show parent in other window when activated."
1193 :tag "Face"
1194 :tag-glyph "face"
1195 :action 'custom-browse-face-tag-action)
1197 (defun custom-browse-face-tag-action (widget &rest ignore)
1198 (let ((parent (widget-get widget :parent)))
1199 (customize-face-other-window (widget-value parent))))
1201 (defconst custom-browse-alist '((" " "space")
1202 (" | " "vertical")
1203 ("-\\ " "top")
1204 (" |-" "middle")
1205 (" `-" "bottom")))
1207 (defun custom-browse-insert-prefix (prefix)
1208 "Insert PREFIX. On XEmacs convert it to line graphics."
1209 (if nil ; (string-match "XEmacs" emacs-version)
1210 (progn
1211 (insert "*")
1212 (while (not (string-equal prefix ""))
1213 (let ((entry (substring prefix 0 3)))
1214 (setq prefix (substring prefix 3))
1215 (let ((overlay (make-overlay (1- (point)) (point) nil t nil))
1216 (name (nth 1 (assoc entry custom-browse-alist))))
1217 (overlay-put overlay 'end-glyph (widget-glyph-find name entry))
1218 (overlay-put overlay 'start-open t)
1219 (overlay-put overlay 'end-open t)))))
1220 (insert prefix)))
1222 ;;; Modification of Basic Widgets.
1224 ;; We add extra properties to the basic widgets needed here. This is
1225 ;; fine, as long as we are careful to stay within out own namespace.
1227 ;; We want simple widgets to be displayed by default, but complex
1228 ;; widgets to be hidden.
1230 (widget-put (get 'item 'widget-type) :custom-show t)
1231 (widget-put (get 'editable-field 'widget-type)
1232 :custom-show (lambda (widget value)
1233 (let ((pp (pp-to-string value)))
1234 (cond ((string-match "\n" pp)
1235 nil)
1236 ((> (length pp) 40)
1237 nil)
1238 (t t)))))
1239 (widget-put (get 'menu-choice 'widget-type) :custom-show t)
1241 ;;; The `custom-manual' Widget.
1243 (define-widget 'custom-manual 'info-link
1244 "Link to the manual entry for this customization option."
1245 :help-echo "Read the manual entry for this option."
1246 :tag "Manual")
1248 ;;; The `custom-magic' Widget.
1250 (defgroup custom-magic-faces nil
1251 "Faces used by the magic button."
1252 :group 'custom-faces
1253 :group 'custom-buffer)
1255 (defface custom-invalid-face '((((class color))
1256 (:foreground "yellow" :background "red"))
1258 (:bold t :italic t :underline t)))
1259 "Face used when the customize item is invalid."
1260 :group 'custom-magic-faces)
1262 (defface custom-rogue-face '((((class color))
1263 (:foreground "pink" :background "black"))
1265 (:underline t)))
1266 "Face used when the customize item is not defined for customization."
1267 :group 'custom-magic-faces)
1269 (defface custom-modified-face '((((class color))
1270 (:foreground "white" :background "blue"))
1272 (:italic t :bold)))
1273 "Face used when the customize item has been modified."
1274 :group 'custom-magic-faces)
1276 (defface custom-set-face '((((class color))
1277 (:foreground "blue" :background "white"))
1279 (:italic t)))
1280 "Face used when the customize item has been set."
1281 :group 'custom-magic-faces)
1283 (defface custom-changed-face '((((class color))
1284 (:foreground "white" :background "blue"))
1286 (:italic t)))
1287 "Face used when the customize item has been changed."
1288 :group 'custom-magic-faces)
1290 (defface custom-saved-face '((t (:underline t)))
1291 "Face used when the customize item has been saved."
1292 :group 'custom-magic-faces)
1294 (defconst custom-magic-alist '((nil "#" underline "\
1295 uninitialized, you should not see this.")
1296 (unknown "?" italic "\
1297 unknown, you should not see this.")
1298 (hidden "-" default "\
1299 hidden, invoke \"Show\" in the previous line to show." "\
1300 group now hidden, invoke \"Show\", above, to show contents.")
1301 (invalid "x" custom-invalid-face "\
1302 the value displayed for this %c is invalid and cannot be set.")
1303 (modified "*" custom-modified-face "\
1304 you have edited the value as text, but you have not set the %c." "\
1305 you have edited something in this group, but not set it.")
1306 (set "+" custom-set-face "\
1307 you have set this %c, but not saved it for future sessions." "\
1308 something in this group has been set, but not saved.")
1309 (changed ":" custom-changed-face "\
1310 this %c has been changed outside the customize buffer." "\
1311 something in this group has been changed outside customize.")
1312 (saved "!" custom-saved-face "\
1313 this %c has been set and saved." "\
1314 something in this group has been set and saved.")
1315 (rogue "@" custom-rogue-face "\
1316 this %c has not been changed with customize." "\
1317 something in this group is not prepared for customization.")
1318 (standard " " nil "\
1319 this %c is unchanged from its standard setting." "\
1320 visible group members are all at standard settings."))
1321 "Alist of customize option states.
1322 Each entry is of the form (STATE MAGIC FACE ITEM-DESC [ GROUP-DESC ]), where
1324 STATE is one of the following symbols:
1326 `nil'
1327 For internal use, should never occur.
1328 `unknown'
1329 For internal use, should never occur.
1330 `hidden'
1331 This item is not being displayed.
1332 `invalid'
1333 This item is modified, but has an invalid form.
1334 `modified'
1335 This item is modified, and has a valid form.
1336 `set'
1337 This item has been set but not saved.
1338 `changed'
1339 The current value of this item has been changed temporarily.
1340 `saved'
1341 This item is marked for saving.
1342 `rogue'
1343 This item has no customization information.
1344 `standard'
1345 This item is unchanged from the standard setting.
1347 MAGIC is a string used to present that state.
1349 FACE is a face used to present the state.
1351 ITEM-DESC is a string describing the state for options.
1353 GROUP-DESC is a string describing the state for groups. If this is
1354 left out, ITEM-DESC will be used.
1356 The string %c in either description will be replaced with the
1357 category of the item. These are `group'. `option', and `face'.
1359 The list should be sorted most significant first.")
1361 (defcustom custom-magic-show 'long
1362 "If non-nil, show textual description of the state.
1363 If `long', show a full-line description, not just one word."
1364 :type '(choice (const :tag "no" nil)
1365 (const short)
1366 (const long))
1367 :group 'custom-buffer)
1369 (defcustom custom-magic-show-hidden '(option face)
1370 "Control whether the State button is shown for hidden items.
1371 The value should be a list with the custom categories where the State
1372 button should be visible. Possible categories are `group', `option',
1373 and `face'."
1374 :type '(set (const group) (const option) (const face))
1375 :group 'custom-buffer)
1377 (defcustom custom-magic-show-button nil
1378 "Show a \"magic\" button indicating the state of each customization option."
1379 :type 'boolean
1380 :group 'custom-buffer)
1382 (define-widget 'custom-magic 'default
1383 "Show and manipulate state for a customization option."
1384 :format "%v"
1385 :action 'widget-parent-action
1386 :notify 'ignore
1387 :value-get 'ignore
1388 :value-create 'custom-magic-value-create
1389 :value-delete 'widget-children-value-delete)
1391 (defun widget-magic-mouse-down-action (widget &optional event)
1392 ;; Non-nil unless hidden.
1393 (not (eq (widget-get (widget-get (widget-get widget :parent) :parent)
1394 :custom-state)
1395 'hidden)))
1397 (defun custom-magic-value-create (widget)
1398 ;; Create compact status report for WIDGET.
1399 (let* ((parent (widget-get widget :parent))
1400 (state (widget-get parent :custom-state))
1401 (hidden (eq state 'hidden))
1402 (entry (assq state custom-magic-alist))
1403 (magic (nth 1 entry))
1404 (face (nth 2 entry))
1405 (category (widget-get parent :custom-category))
1406 (text (or (and (eq category 'group)
1407 (nth 4 entry))
1408 (nth 3 entry)))
1409 (lisp (eq (widget-get parent :custom-form) 'lisp))
1410 children)
1411 (while (string-match "\\`\\(.*\\)%c\\(.*\\)\\'" text)
1412 (setq text (concat (match-string 1 text)
1413 (symbol-name category)
1414 (match-string 2 text))))
1415 (when (and custom-magic-show
1416 (or (not hidden)
1417 (memq category custom-magic-show-hidden)))
1418 (insert " ")
1419 (when (and (eq category 'group)
1420 (not (and (eq custom-buffer-style 'links)
1421 (> (widget-get parent :custom-level) 1))))
1422 (insert-char ?\ (* custom-buffer-indent
1423 (widget-get parent :custom-level))))
1424 (push (widget-create-child-and-convert
1425 widget 'choice-item
1426 :help-echo "Change the state of this item."
1427 :format (if hidden "%t" "%[%t%]")
1428 :button-prefix 'widget-push-button-prefix
1429 :button-suffix 'widget-push-button-suffix
1430 :mouse-down-action 'widget-magic-mouse-down-action
1431 :tag "State")
1432 children)
1433 (insert ": ")
1434 (let ((start (point)))
1435 (if (eq custom-magic-show 'long)
1436 (insert text)
1437 (insert (symbol-name state)))
1438 (when lisp
1439 (insert " (lisp)"))
1440 (put-text-property start (point) 'face 'custom-state-face))
1441 (insert "\n"))
1442 (when (and (eq category 'group)
1443 (not (and (eq custom-buffer-style 'links)
1444 (> (widget-get parent :custom-level) 1))))
1445 (insert-char ?\ (* custom-buffer-indent
1446 (widget-get parent :custom-level))))
1447 (when custom-magic-show-button
1448 (when custom-magic-show
1449 (let ((indent (widget-get parent :indent)))
1450 (when indent
1451 (insert-char ? indent))))
1452 (push (widget-create-child-and-convert
1453 widget 'choice-item
1454 :mouse-down-action 'widget-magic-mouse-down-action
1455 :button-face face
1456 :button-prefix ""
1457 :button-suffix ""
1458 :help-echo "Change the state."
1459 :format (if hidden "%t" "%[%t%]")
1460 :tag (if lisp
1461 (concat "(" magic ")")
1462 (concat "[" magic "]")))
1463 children)
1464 (insert " "))
1465 (widget-put widget :children children)))
1467 (defun custom-magic-reset (widget)
1468 "Redraw the :custom-magic property of WIDGET."
1469 (let ((magic (widget-get widget :custom-magic)))
1470 (widget-value-set magic (widget-value magic))))
1472 ;;; The `custom' Widget.
1474 (defface custom-button-face nil
1475 "Face used for buttons in customization buffers."
1476 :group 'custom-faces)
1478 (defface custom-documentation-face nil
1479 "Face used for documentation strings in customization buffers."
1480 :group 'custom-faces)
1482 (defface custom-state-face '((((class color)
1483 (background dark))
1484 (:foreground "lime green"))
1485 (((class color)
1486 (background light))
1487 (:foreground "dark green"))
1488 (t nil))
1489 "Face used for State descriptions in the customize buffer."
1490 :group 'custom-faces)
1492 (define-widget 'custom 'default
1493 "Customize a user option."
1494 :format "%v"
1495 :convert-widget 'custom-convert-widget
1496 :notify 'custom-notify
1497 :custom-prefix ""
1498 :custom-level 1
1499 :custom-state 'hidden
1500 :documentation-property 'widget-subclass-responsibility
1501 :value-create 'widget-subclass-responsibility
1502 :value-delete 'widget-children-value-delete
1503 :value-get 'widget-value-value-get
1504 :validate 'widget-children-validate
1505 :match (lambda (widget value) (symbolp value)))
1507 (defun custom-convert-widget (widget)
1508 ;; Initialize :value and :tag from :args in WIDGET.
1509 (let ((args (widget-get widget :args)))
1510 (when args
1511 (widget-put widget :value (widget-apply widget
1512 :value-to-internal (car args)))
1513 (widget-put widget :tag (custom-unlispify-tag-name (car args)))
1514 (widget-put widget :args nil)))
1515 widget)
1517 (defun custom-notify (widget &rest args)
1518 "Keep track of changes."
1519 (let ((state (widget-get widget :custom-state)))
1520 (unless (eq state 'modified)
1521 (unless (memq state '(nil unknown hidden))
1522 (widget-put widget :custom-state 'modified))
1523 (custom-magic-reset widget)
1524 (apply 'widget-default-notify widget args))))
1526 (defun custom-redraw (widget)
1527 "Redraw WIDGET with current settings."
1528 (let ((line (count-lines (point-min) (point)))
1529 (column (current-column))
1530 (pos (point))
1531 (from (marker-position (widget-get widget :from)))
1532 (to (marker-position (widget-get widget :to))))
1533 (save-excursion
1534 (widget-value-set widget (widget-value widget))
1535 (custom-redraw-magic widget))
1536 (when (and (>= pos from) (<= pos to))
1537 (condition-case nil
1538 (progn
1539 (if (> column 0)
1540 (goto-line line)
1541 (goto-line (1+ line)))
1542 (move-to-column column))
1543 (error nil)))))
1545 (defun custom-redraw-magic (widget)
1546 "Redraw WIDGET state with current settings."
1547 (while widget
1548 (let ((magic (widget-get widget :custom-magic)))
1549 (cond (magic
1550 (widget-value-set magic (widget-value magic))
1551 (when (setq widget (widget-get widget :group))
1552 (custom-group-state-update widget)))
1554 (setq widget nil)))))
1555 (widget-setup))
1557 (defun custom-show (widget value)
1558 "Non-nil if WIDGET should be shown with VALUE by default."
1559 (let ((show (widget-get widget :custom-show)))
1560 (cond ((null show)
1561 nil)
1562 ((eq t show)
1565 (funcall show widget value)))))
1567 (defvar custom-load-recursion nil
1568 "Hack to avoid recursive dependencies.")
1570 (defun custom-load-symbol (symbol)
1571 "Load all dependencies for SYMBOL."
1572 (unless custom-load-recursion
1573 (let ((custom-load-recursion t)
1574 (loads (get symbol 'custom-loads))
1575 load)
1576 (while loads
1577 (setq load (car loads)
1578 loads (cdr loads))
1579 (cond ((symbolp load)
1580 (condition-case nil
1581 (require load)
1582 (error nil)))
1583 ;; Don't reload a file already loaded.
1584 ((member load preloaded-file-list))
1585 ((assoc load load-history))
1586 ((assoc (locate-library load) load-history))
1588 (condition-case nil
1589 ;; Without this, we would load cus-edit recursively.
1590 ;; We are still loading it when we call this,
1591 ;; and it is not in load-history yet.
1592 (or (equal load "cus-edit")
1593 (load-library load))
1594 (error nil))))))))
1596 (defun custom-load-widget (widget)
1597 "Load all dependencies for WIDGET."
1598 (custom-load-symbol (widget-value widget)))
1600 (defun custom-unloaded-symbol-p (symbol)
1601 "Return non-nil if the dependencies of SYMBOL has not yet been loaded."
1602 (let ((found nil)
1603 (loads (get symbol 'custom-loads))
1604 load)
1605 (while loads
1606 (setq load (car loads)
1607 loads (cdr loads))
1608 (cond ((symbolp load)
1609 (unless (featurep load)
1610 (setq found t)))
1611 ((assoc load load-history))
1612 ((assoc (locate-library load) load-history)
1613 (message nil))
1615 (setq found t))))
1616 found))
1618 (defun custom-unloaded-widget-p (widget)
1619 "Return non-nil if the dependencies of WIDGET has not yet been loaded."
1620 (custom-unloaded-symbol-p (widget-value widget)))
1622 (defun custom-toggle-hide (widget)
1623 "Toggle visibility of WIDGET."
1624 (custom-load-widget widget)
1625 (let ((state (widget-get widget :custom-state)))
1626 (cond ((memq state '(invalid modified))
1627 (error "There are unset changes"))
1628 ((eq state 'hidden)
1629 (widget-put widget :custom-state 'unknown))
1631 (widget-put widget :documentation-shown nil)
1632 (widget-put widget :custom-state 'hidden)))
1633 (custom-redraw widget)
1634 (widget-setup)))
1636 (defun custom-toggle-parent (widget &rest ignore)
1637 "Toggle visibility of parent of WIDGET."
1638 (custom-toggle-hide (widget-get widget :parent)))
1640 (defun custom-add-see-also (widget &optional prefix)
1641 "Add `See also ...' to WIDGET if there are any links.
1642 Insert PREFIX first if non-nil."
1643 (let* ((symbol (widget-get widget :value))
1644 (links (get symbol 'custom-links))
1645 (many (> (length links) 2))
1646 (buttons (widget-get widget :buttons))
1647 (indent (widget-get widget :indent)))
1648 (when links
1649 (when indent
1650 (insert-char ?\ indent))
1651 (when prefix
1652 (insert prefix))
1653 (insert "See also ")
1654 (while links
1655 (push (widget-create-child-and-convert widget (car links))
1656 buttons)
1657 (setq links (cdr links))
1658 (cond ((null links)
1659 (insert ".\n"))
1660 ((null (cdr links))
1661 (if many
1662 (insert ", and ")
1663 (insert " and ")))
1665 (insert ", "))))
1666 (widget-put widget :buttons buttons))))
1668 (defun custom-add-parent-links (widget &optional initial-string)
1669 "Add \"Parent groups: ...\" to WIDGET if the group has parents.
1670 The value if non-nil if any parents were found.
1671 If INITIAL-STRING is non-nil, use that rather than \"Parent groups:\"."
1672 (let ((name (widget-value widget))
1673 (type (widget-type widget))
1674 (buttons (widget-get widget :buttons))
1675 (start (point))
1676 found)
1677 (insert (or initial-string "Parent groups:"))
1678 (mapatoms (lambda (symbol)
1679 (let ((entry (assq name (get symbol 'custom-group))))
1680 (when (eq (nth 1 entry) type)
1681 (insert " ")
1682 (push (widget-create-child-and-convert
1683 widget 'custom-group-link
1684 :tag (custom-unlispify-tag-name symbol)
1685 symbol)
1686 buttons)
1687 (setq found t)))))
1688 (widget-put widget :buttons buttons)
1689 (if found
1690 (insert "\n")
1691 (delete-region start (point)))
1692 found))
1694 ;;; The `custom-variable' Widget.
1696 (defface custom-variable-tag-face '((((class color)
1697 (background dark))
1698 (:foreground "light blue" :underline t))
1699 (((class color)
1700 (background light))
1701 (:foreground "blue" :underline t))
1702 (t (:underline t)))
1703 "Face used for unpushable variable tags."
1704 :group 'custom-faces)
1706 (defface custom-variable-button-face '((t (:underline t :bold t)))
1707 "Face used for pushable variable tags."
1708 :group 'custom-faces)
1710 (define-widget 'custom-variable 'custom
1711 "Customize variable."
1712 :format "%v"
1713 :help-echo "Set or reset this variable."
1714 :documentation-property 'variable-documentation
1715 :custom-category 'option
1716 :custom-state nil
1717 :custom-menu 'custom-variable-menu-create
1718 :custom-form 'edit
1719 :value-create 'custom-variable-value-create
1720 :action 'custom-variable-action
1721 :custom-set 'custom-variable-set
1722 :custom-save 'custom-variable-save
1723 :custom-reset-current 'custom-redraw
1724 :custom-reset-saved 'custom-variable-reset-saved
1725 :custom-reset-standard 'custom-variable-reset-standard)
1727 (defun custom-variable-type (symbol)
1728 "Return a widget suitable for editing the value of SYMBOL.
1729 If SYMBOL has a `custom-type' property, use that.
1730 Otherwise, look up symbol in `custom-guess-type-alist'."
1731 (let* ((type (or (get symbol 'custom-type)
1732 (and (not (get symbol 'standard-value))
1733 (custom-guess-type symbol))
1734 'sexp))
1735 (options (get symbol 'custom-options))
1736 (tmp (if (listp type)
1737 (copy-sequence type)
1738 (list type))))
1739 (when options
1740 (widget-put tmp :options options))
1741 tmp))
1743 (defun custom-variable-value-create (widget)
1744 "Here is where you edit the variables value."
1745 (custom-load-widget widget)
1746 (let* ((buttons (widget-get widget :buttons))
1747 (children (widget-get widget :children))
1748 (form (widget-get widget :custom-form))
1749 (state (widget-get widget :custom-state))
1750 (symbol (widget-get widget :value))
1751 (tag (widget-get widget :tag))
1752 (type (custom-variable-type symbol))
1753 (conv (widget-convert type))
1754 (get (or (get symbol 'custom-get) 'default-value))
1755 (prefix (widget-get widget :custom-prefix))
1756 (last (widget-get widget :custom-last))
1757 (value (if (default-boundp symbol)
1758 (funcall get symbol)
1759 (widget-get conv :value))))
1760 ;; If the widget is new, the child determine whether it is hidden.
1761 (cond (state)
1762 ((custom-show type value)
1763 (setq state 'unknown))
1765 (setq state 'hidden)))
1766 ;; If we don't know the state, see if we need to edit it in lisp form.
1767 (when (eq state 'unknown)
1768 (unless (widget-apply conv :match value)
1769 ;; (widget-apply (widget-convert type) :match value)
1770 (setq form 'lisp)))
1771 ;; Now we can create the child widget.
1772 (cond ((eq custom-buffer-style 'tree)
1773 (insert prefix (if last " `--- " " |--- "))
1774 (push (widget-create-child-and-convert
1775 widget 'custom-browse-variable-tag)
1776 buttons)
1777 (insert " " tag "\n")
1778 (widget-put widget :buttons buttons))
1779 ((eq state 'hidden)
1780 ;; Indicate hidden value.
1781 (push (widget-create-child-and-convert
1782 widget 'item
1783 :format "%{%t%}: "
1784 :sample-face 'custom-variable-tag-face
1785 :tag tag
1786 :parent widget)
1787 buttons)
1788 (push (widget-create-child-and-convert
1789 widget 'visibility
1790 :help-echo "Show the value of this option."
1791 :action 'custom-toggle-parent
1792 nil)
1793 buttons))
1794 ((eq form 'lisp)
1795 ;; In lisp mode edit the saved value when possible.
1796 (let* ((value (cond ((get symbol 'saved-value)
1797 (car (get symbol 'saved-value)))
1798 ((get symbol 'standard-value)
1799 (car (get symbol 'standard-value)))
1800 ((default-boundp symbol)
1801 (custom-quote (funcall get symbol)))
1803 (custom-quote (widget-get conv :value))))))
1804 (insert (symbol-name symbol) ": ")
1805 (push (widget-create-child-and-convert
1806 widget 'visibility
1807 :help-echo "Hide the value of this option."
1808 :action 'custom-toggle-parent
1810 buttons)
1811 (insert " ")
1812 (push (widget-create-child-and-convert
1813 widget 'sexp
1814 :button-face 'custom-variable-button-face
1815 :format "%v"
1816 :tag (symbol-name symbol)
1817 :parent widget
1818 :value value)
1819 children)))
1821 ;; Edit mode.
1822 (let* ((format (widget-get type :format))
1823 tag-format value-format)
1824 (unless (string-match ":" format)
1825 (error "Bad format."))
1826 (setq tag-format (substring format 0 (match-end 0)))
1827 (setq value-format (substring format (match-end 0)))
1828 (push (widget-create-child-and-convert
1829 widget 'item
1830 :format tag-format
1831 :action 'custom-tag-action
1832 :help-echo "Change value of this option."
1833 :mouse-down-action 'custom-tag-mouse-down-action
1834 :button-face 'custom-variable-button-face
1835 :sample-face 'custom-variable-tag-face
1836 tag)
1837 buttons)
1838 (insert " ")
1839 (push (widget-create-child-and-convert
1840 widget 'visibility
1841 :help-echo "Hide the value of this option."
1842 :action 'custom-toggle-parent
1844 buttons)
1845 (push (widget-create-child-and-convert
1846 widget type
1847 :format value-format
1848 :value value)
1849 children))))
1850 (unless (eq custom-buffer-style 'tree)
1851 ;; Now update the state.
1852 (unless (eq (preceding-char) ?\n)
1853 (widget-insert "\n"))
1854 (if (eq state 'hidden)
1855 (widget-put widget :custom-state state)
1856 (custom-variable-state-set widget))
1857 ;; Create the magic button.
1858 (let ((magic (widget-create-child-and-convert
1859 widget 'custom-magic nil)))
1860 (widget-put widget :custom-magic magic)
1861 (push magic buttons))
1862 ;; Update properties.
1863 (widget-put widget :custom-form form)
1864 (widget-put widget :buttons buttons)
1865 (widget-put widget :children children)
1866 ;; Insert documentation.
1867 (widget-default-format-handler widget ?h)
1868 ;; See also.
1869 (unless (eq state 'hidden)
1870 (when (eq (widget-get widget :custom-level) 1)
1871 (custom-add-parent-links widget))
1872 (custom-add-see-also widget)))))
1874 (defun custom-tag-action (widget &rest args)
1875 "Pass :action to first child of WIDGET's parent."
1876 (apply 'widget-apply (car (widget-get (widget-get widget :parent) :children))
1877 :action args))
1879 (defun custom-tag-mouse-down-action (widget &rest args)
1880 "Pass :mouse-down-action to first child of WIDGET's parent."
1881 (apply 'widget-apply (car (widget-get (widget-get widget :parent) :children))
1882 :mouse-down-action args))
1884 (defun custom-variable-state-set (widget)
1885 "Set the state of WIDGET."
1886 (let* ((symbol (widget-value widget))
1887 (get (or (get symbol 'custom-get) 'default-value))
1888 (value (if (default-boundp symbol)
1889 (funcall get symbol)
1890 (widget-get widget :value)))
1892 (state (cond ((setq tmp (get symbol 'customized-value))
1893 (if (condition-case nil
1894 (equal value (eval (car tmp)))
1895 (error nil))
1896 'set
1897 'changed))
1898 ((setq tmp (get symbol 'saved-value))
1899 (if (condition-case nil
1900 (equal value (eval (car tmp)))
1901 (error nil))
1902 'saved
1903 'changed))
1904 ((setq tmp (get symbol 'standard-value))
1905 (if (condition-case nil
1906 (equal value (eval (car tmp)))
1907 (error nil))
1908 'standard
1909 'changed))
1910 (t 'rogue))))
1911 (widget-put widget :custom-state state)))
1913 (defvar custom-variable-menu
1914 '(("Set for Current Session" custom-variable-set
1915 (lambda (widget)
1916 (eq (widget-get widget :custom-state) 'modified)))
1917 ("Save for Future Sessions" custom-variable-save
1918 (lambda (widget)
1919 (memq (widget-get widget :custom-state) '(modified set changed rogue))))
1920 ("Reset to Current" custom-redraw
1921 (lambda (widget)
1922 (and (default-boundp (widget-value widget))
1923 (memq (widget-get widget :custom-state) '(modified changed)))))
1924 ("Reset to Saved" custom-variable-reset-saved
1925 (lambda (widget)
1926 (and (get (widget-value widget) 'saved-value)
1927 (memq (widget-get widget :custom-state)
1928 '(modified set changed rogue)))))
1929 ("Reset to Standard Settings" custom-variable-reset-standard
1930 (lambda (widget)
1931 (and (get (widget-value widget) 'standard-value)
1932 (memq (widget-get widget :custom-state)
1933 '(modified set changed saved rogue)))))
1934 ("---" ignore ignore)
1935 ("Don't show as Lisp expression" custom-variable-edit
1936 (lambda (widget)
1937 (not (eq (widget-get widget :custom-form) 'edit))))
1938 ("Show as Lisp expression" custom-variable-edit-lisp
1939 (lambda (widget)
1940 (not (eq (widget-get widget :custom-form) 'lisp)))))
1941 "Alist of actions for the `custom-variable' widget.
1942 Each entry has the form (NAME ACTION FILTER) where NAME is the name of
1943 the menu entry, ACTION is the function to call on the widget when the
1944 menu is selected, and FILTER is a predicate which takes a `custom-variable'
1945 widget as an argument, and returns non-nil if ACTION is valid on that
1946 widget. If FILTER is nil, ACTION is always valid.")
1948 (defun custom-variable-action (widget &optional event)
1949 "Show the menu for `custom-variable' WIDGET.
1950 Optional EVENT is the location for the menu."
1951 (if (eq (widget-get widget :custom-state) 'hidden)
1952 (custom-toggle-hide widget)
1953 (unless (eq (widget-get widget :custom-state) 'modified)
1954 (custom-variable-state-set widget))
1955 (custom-redraw-magic widget)
1956 (let* ((completion-ignore-case t)
1957 (answer (widget-choose (concat "Operation on "
1958 (custom-unlispify-tag-name
1959 (widget-get widget :value)))
1960 (custom-menu-filter custom-variable-menu
1961 widget)
1962 event)))
1963 (if answer
1964 (funcall answer widget)))))
1966 (defun custom-variable-edit (widget)
1967 "Edit value of WIDGET."
1968 (widget-put widget :custom-state 'unknown)
1969 (widget-put widget :custom-form 'edit)
1970 (custom-redraw widget))
1972 (defun custom-variable-edit-lisp (widget)
1973 "Edit the lisp representation of the value of WIDGET."
1974 (widget-put widget :custom-state 'unknown)
1975 (widget-put widget :custom-form 'lisp)
1976 (custom-redraw widget))
1978 (defun custom-variable-set (widget)
1979 "Set the current value for the variable being edited by WIDGET."
1980 (let* ((form (widget-get widget :custom-form))
1981 (state (widget-get widget :custom-state))
1982 (child (car (widget-get widget :children)))
1983 (symbol (widget-value widget))
1984 (set (or (get symbol 'custom-set) 'set-default))
1985 val)
1986 (cond ((eq state 'hidden)
1987 (error "Cannot set hidden variable."))
1988 ((setq val (widget-apply child :validate))
1989 (goto-char (widget-get val :from))
1990 (error "%s" (widget-get val :error)))
1991 ((eq form 'lisp)
1992 (funcall set symbol (eval (setq val (widget-value child))))
1993 (put symbol 'customized-value (list val)))
1995 (funcall set symbol (setq val (widget-value child)))
1996 (put symbol 'customized-value (list (custom-quote val)))))
1997 (custom-variable-state-set widget)
1998 (custom-redraw-magic widget)))
2000 (defun custom-variable-save (widget)
2001 "Set the default value for the variable being edited by WIDGET."
2002 (let* ((form (widget-get widget :custom-form))
2003 (state (widget-get widget :custom-state))
2004 (child (car (widget-get widget :children)))
2005 (symbol (widget-value widget))
2006 (set (or (get symbol 'custom-set) 'set-default))
2007 val)
2008 (cond ((eq state 'hidden)
2009 (error "Cannot set hidden variable."))
2010 ((setq val (widget-apply child :validate))
2011 (goto-char (widget-get val :from))
2012 (error "%s" (widget-get val :error)))
2013 ((eq form 'lisp)
2014 (put symbol 'saved-value (list (widget-value child)))
2015 (funcall set symbol (eval (widget-value child))))
2017 (put symbol
2018 'saved-value (list (custom-quote (widget-value
2019 child))))
2020 (funcall set symbol (widget-value child))))
2021 (put symbol 'customized-value nil)
2022 (custom-save-all)
2023 (custom-variable-state-set widget)
2024 (custom-redraw-magic widget)))
2026 (defun custom-variable-reset-saved (widget)
2027 "Restore the saved value for the variable being edited by WIDGET."
2028 (let* ((symbol (widget-value widget))
2029 (set (or (get symbol 'custom-set) 'set-default)))
2030 (if (get symbol 'saved-value)
2031 (condition-case nil
2032 (funcall set symbol (eval (car (get symbol 'saved-value))))
2033 (error nil))
2034 (error "No saved value for %s" symbol))
2035 (put symbol 'customized-value nil)
2036 (widget-put widget :custom-state 'unknown)
2037 (custom-redraw widget)))
2039 (defun custom-variable-reset-standard (widget)
2040 "Restore the standard setting for the variable being edited by WIDGET."
2041 (let* ((symbol (widget-value widget))
2042 (set (or (get symbol 'custom-set) 'set-default)))
2043 (if (get symbol 'standard-value)
2044 (funcall set symbol (eval (car (get symbol 'standard-value))))
2045 (error "No standard setting known for %S" symbol))
2046 (put symbol 'customized-value nil)
2047 (when (get symbol 'saved-value)
2048 (put symbol 'saved-value nil)
2049 (custom-save-all))
2050 (widget-put widget :custom-state 'unknown)
2051 (custom-redraw widget)))
2053 ;;; The `custom-face-edit' Widget.
2055 (define-widget 'custom-face-edit 'checklist
2056 "Edit face attributes."
2057 :format "%t: %v"
2058 :tag "Attributes"
2059 :extra-offset 12
2060 :button-args '(:help-echo "Control whether this attribute have any effect.")
2061 :args (mapcar (lambda (att)
2062 (list 'group
2063 :inline t
2064 :sibling-args (widget-get (nth 1 att) :sibling-args)
2065 (list 'const :format "" :value (nth 0 att))
2066 (nth 1 att)))
2067 custom-face-attributes))
2069 ;;; The `custom-display' Widget.
2071 (define-widget 'custom-display 'menu-choice
2072 "Select a display type."
2073 :tag "Display"
2074 :value t
2075 :help-echo "Specify frames where the face attributes should be used."
2076 :args '((const :tag "all" t)
2077 (checklist
2078 :offset 0
2079 :extra-offset 9
2080 :args ((group :sibling-args (:help-echo "\
2081 Only match the specified window systems.")
2082 (const :format "Type: "
2083 type)
2084 (checklist :inline t
2085 :offset 0
2086 (const :format "X "
2087 :sibling-args (:help-echo "\
2088 The X11 Window System.")
2090 (const :format "PM "
2091 :sibling-args (:help-echo "\
2092 OS/2 Presentation Manager.")
2094 (const :format "Win32 "
2095 :sibling-args (:help-echo "\
2096 Windows NT/95/97.")
2097 win32)
2098 (const :format "DOS "
2099 :sibling-args (:help-echo "\
2100 Plain MS-DOS.")
2102 (const :format "TTY%n"
2103 :sibling-args (:help-echo "\
2104 Plain text terminals.")
2105 tty)))
2106 (group :sibling-args (:help-echo "\
2107 Only match the frames with the specified color support.")
2108 (const :format "Class: "
2109 class)
2110 (checklist :inline t
2111 :offset 0
2112 (const :format "Color "
2113 :sibling-args (:help-echo "\
2114 Match color frames.")
2115 color)
2116 (const :format "Grayscale "
2117 :sibling-args (:help-echo "\
2118 Match grayscale frames.")
2119 grayscale)
2120 (const :format "Monochrome%n"
2121 :sibling-args (:help-echo "\
2122 Match frames with no color support.")
2123 mono)))
2124 (group :sibling-args (:help-echo "\
2125 Only match frames with the specified intensity.")
2126 (const :format "\
2127 Background brightness: "
2128 background)
2129 (checklist :inline t
2130 :offset 0
2131 (const :format "Light "
2132 :sibling-args (:help-echo "\
2133 Match frames with light backgrounds.")
2134 light)
2135 (const :format "Dark\n"
2136 :sibling-args (:help-echo "\
2137 Match frames with dark backgrounds.")
2138 dark)))))))
2140 ;;; The `custom-face' Widget.
2142 (defface custom-face-tag-face '((t (:underline t)))
2143 "Face used for face tags."
2144 :group 'custom-faces)
2146 (define-widget 'custom-face 'custom
2147 "Customize face."
2148 :sample-face 'custom-face-tag-face
2149 :help-echo "Set or reset this face."
2150 :documentation-property '(lambda (face)
2151 (face-doc-string face))
2152 :value-create 'custom-face-value-create
2153 :action 'custom-face-action
2154 :custom-category 'face
2155 :custom-form 'selected
2156 :custom-set 'custom-face-set
2157 :custom-save 'custom-face-save
2158 :custom-reset-current 'custom-redraw
2159 :custom-reset-saved 'custom-face-reset-saved
2160 :custom-reset-standard 'custom-face-reset-standard
2161 :custom-menu 'custom-face-menu-create)
2163 (define-widget 'custom-face-all 'editable-list
2164 "An editable list of display specifications and attributes."
2165 :entry-format "%i %d %v"
2166 :insert-button-args '(:help-echo "Insert new display specification here.")
2167 :append-button-args '(:help-echo "Append new display specification here.")
2168 :delete-button-args '(:help-echo "Delete this display specification.")
2169 :args '((group :format "%v" custom-display custom-face-edit)))
2171 (defconst custom-face-all (widget-convert 'custom-face-all)
2172 "Converted version of the `custom-face-all' widget.")
2174 (define-widget 'custom-display-unselected 'item
2175 "A display specification that doesn't match the selected display."
2176 :match 'custom-display-unselected-match)
2178 (defun custom-display-unselected-match (widget value)
2179 "Non-nil if VALUE is an unselected display specification."
2180 (not (face-spec-set-match-display value (selected-frame))))
2182 (define-widget 'custom-face-selected 'group
2183 "Edit the attributes of the selected display in a face specification."
2184 :args '((repeat :format ""
2185 :inline t
2186 (group custom-display-unselected sexp))
2187 (group (sexp :format "") custom-face-edit)
2188 (repeat :format ""
2189 :inline t
2190 sexp)))
2192 (defconst custom-face-selected (widget-convert 'custom-face-selected)
2193 "Converted version of the `custom-face-selected' widget.")
2195 (defun custom-face-value-create (widget)
2196 "Create a list of the display specifications for WIDGET."
2197 (let ((buttons (widget-get widget :buttons))
2198 (symbol (widget-get widget :value))
2199 (tag (widget-get widget :tag))
2200 (state (widget-get widget :custom-state))
2201 (begin (point))
2202 (is-last (widget-get widget :custom-last))
2203 (prefix (widget-get widget :custom-prefix)))
2204 (unless tag
2205 (setq tag (prin1-to-string symbol)))
2206 (cond ((eq custom-buffer-style 'tree)
2207 (insert prefix (if is-last " `--- " " |--- "))
2208 (push (widget-create-child-and-convert
2209 widget 'custom-browse-face-tag)
2210 buttons)
2211 (insert " " tag "\n")
2212 (widget-put widget :buttons buttons))
2214 ;; Create tag.
2215 (insert tag)
2216 (if (eq custom-buffer-style 'face)
2217 (insert " ")
2218 (widget-specify-sample widget begin (point))
2219 (insert ": "))
2220 ;; Sample.
2221 (and (string-match "XEmacs" emacs-version)
2222 ;; XEmacs cannot display uninitialized faces.
2223 (not (custom-facep symbol))
2224 (copy-face 'custom-face-empty symbol))
2225 (push (widget-create-child-and-convert widget 'item
2226 :format "(%{%t%})"
2227 :sample-face symbol
2228 :tag "sample")
2229 buttons)
2230 ;; Visibility.
2231 (insert " ")
2232 (push (widget-create-child-and-convert
2233 widget 'visibility
2234 :help-echo "Hide or show this face."
2235 :action 'custom-toggle-parent
2236 (not (eq state 'hidden)))
2237 buttons)
2238 ;; Magic.
2239 (insert "\n")
2240 (let ((magic (widget-create-child-and-convert
2241 widget 'custom-magic nil)))
2242 (widget-put widget :custom-magic magic)
2243 (push magic buttons))
2244 ;; Update buttons.
2245 (widget-put widget :buttons buttons)
2246 ;; Insert documentation.
2247 (widget-default-format-handler widget ?h)
2248 ;; See also.
2249 (unless (eq state 'hidden)
2250 (when (eq (widget-get widget :custom-level) 1)
2251 (custom-add-parent-links widget))
2252 (custom-add-see-also widget))
2253 ;; Editor.
2254 (unless (eq (preceding-char) ?\n)
2255 (insert "\n"))
2256 (unless (eq state 'hidden)
2257 (message "Creating face editor...")
2258 (custom-load-widget widget)
2259 (let* ((symbol (widget-value widget))
2260 (spec (or (get symbol 'saved-face)
2261 (get symbol 'face-defface-spec)
2262 ;; Attempt to construct it.
2263 (list (list t (custom-face-attributes-get
2264 symbol (selected-frame))))))
2265 (form (widget-get widget :custom-form))
2266 (indent (widget-get widget :indent))
2267 (edit (widget-create-child-and-convert
2268 widget
2269 (cond ((and (eq form 'selected)
2270 (widget-apply custom-face-selected
2271 :match spec))
2272 (when indent (insert-char ?\ indent))
2273 'custom-face-selected)
2274 ((and (not (eq form 'lisp))
2275 (widget-apply custom-face-all
2276 :match spec))
2277 'custom-face-all)
2279 (when indent (insert-char ?\ indent))
2280 'sexp))
2281 :value spec)))
2282 (custom-face-state-set widget)
2283 (widget-put widget :children (list edit)))
2284 (message "Creating face editor...done"))))))
2286 (defvar custom-face-menu
2287 '(("Set for Current Session" custom-face-set)
2288 ("Save for Future Sessions" custom-face-save)
2289 ("Reset to Saved" custom-face-reset-saved
2290 (lambda (widget)
2291 (get (widget-value widget) 'saved-face)))
2292 ("Reset to Standard Setting" custom-face-reset-standard
2293 (lambda (widget)
2294 (get (widget-value widget) 'face-defface-spec)))
2295 ("---" ignore ignore)
2296 ("Show all display specs" custom-face-edit-all
2297 (lambda (widget)
2298 (not (eq (widget-get widget :custom-form) 'all))))
2299 ("Just current attributes" custom-face-edit-selected
2300 (lambda (widget)
2301 (not (eq (widget-get widget :custom-form) 'selected))))
2302 ("Show as Lisp expression" custom-face-edit-lisp
2303 (lambda (widget)
2304 (not (eq (widget-get widget :custom-form) 'lisp)))))
2305 "Alist of actions for the `custom-face' widget.
2306 Each entry has the form (NAME ACTION FILTER) where NAME is the name of
2307 the menu entry, ACTION is the function to call on the widget when the
2308 menu is selected, and FILTER is a predicate which takes a `custom-face'
2309 widget as an argument, and returns non-nil if ACTION is valid on that
2310 widget. If FILTER is nil, ACTION is always valid.")
2312 (defun custom-face-edit-selected (widget)
2313 "Edit selected attributes of the value of WIDGET."
2314 (widget-put widget :custom-state 'unknown)
2315 (widget-put widget :custom-form 'selected)
2316 (custom-redraw widget))
2318 (defun custom-face-edit-all (widget)
2319 "Edit all attributes of the value of WIDGET."
2320 (widget-put widget :custom-state 'unknown)
2321 (widget-put widget :custom-form 'all)
2322 (custom-redraw widget))
2324 (defun custom-face-edit-lisp (widget)
2325 "Edit the lisp representation of the value of WIDGET."
2326 (widget-put widget :custom-state 'unknown)
2327 (widget-put widget :custom-form 'lisp)
2328 (custom-redraw widget))
2330 (defun custom-face-state-set (widget)
2331 "Set the state of WIDGET."
2332 (let ((symbol (widget-value widget)))
2333 (widget-put widget :custom-state (cond ((get symbol 'customized-face)
2334 'set)
2335 ((get symbol 'saved-face)
2336 'saved)
2337 ((get symbol 'face-defface-spec)
2338 'standard)
2340 'rogue)))))
2342 (defun custom-face-action (widget &optional event)
2343 "Show the menu for `custom-face' WIDGET.
2344 Optional EVENT is the location for the menu."
2345 (if (eq (widget-get widget :custom-state) 'hidden)
2346 (custom-toggle-hide widget)
2347 (let* ((completion-ignore-case t)
2348 (symbol (widget-get widget :value))
2349 (answer (widget-choose (concat "Operation on "
2350 (custom-unlispify-tag-name symbol))
2351 (custom-menu-filter custom-face-menu
2352 widget)
2353 event)))
2354 (if answer
2355 (funcall answer widget)))))
2357 (defun custom-face-set (widget)
2358 "Make the face attributes in WIDGET take effect."
2359 (let* ((symbol (widget-value widget))
2360 (child (car (widget-get widget :children)))
2361 (value (widget-value child)))
2362 (put symbol 'customized-face value)
2363 (face-spec-set symbol value)
2364 (custom-face-state-set widget)
2365 (custom-redraw-magic widget)))
2367 (defun custom-face-save (widget)
2368 "Make the face attributes in WIDGET default."
2369 (let* ((symbol (widget-value widget))
2370 (child (car (widget-get widget :children)))
2371 (value (widget-value child)))
2372 (face-spec-set symbol value)
2373 (put symbol 'saved-face value)
2374 (put symbol 'customized-face nil)
2375 (custom-face-state-set widget)
2376 (custom-redraw-magic widget)))
2378 (defun custom-face-reset-saved (widget)
2379 "Restore WIDGET to the face's default attributes."
2380 (let* ((symbol (widget-value widget))
2381 (child (car (widget-get widget :children)))
2382 (value (get symbol 'saved-face)))
2383 (unless value
2384 (error "No saved value for this face"))
2385 (put symbol 'customized-face nil)
2386 (face-spec-set symbol value)
2387 (widget-value-set child value)
2388 (custom-face-state-set widget)
2389 (custom-redraw-magic widget)))
2391 (defun custom-face-reset-standard (widget)
2392 "Restore WIDGET to the face's standard settings."
2393 (let* ((symbol (widget-value widget))
2394 (child (car (widget-get widget :children)))
2395 (value (get symbol 'face-defface-spec)))
2396 (unless value
2397 (error "No standard setting for this face"))
2398 (put symbol 'customized-face nil)
2399 (when (get symbol 'saved-face)
2400 (put symbol 'saved-face nil)
2401 (custom-save-all))
2402 (face-spec-set symbol value)
2403 (widget-value-set child value)
2404 (custom-face-state-set widget)
2405 (custom-redraw-magic widget)))
2407 ;;; The `face' Widget.
2409 (define-widget 'face 'default
2410 "Select and customize a face."
2411 :convert-widget 'widget-value-convert-widget
2412 :button-prefix 'widget-push-button-prefix
2413 :button-suffix 'widget-push-button-suffix
2414 :format "%t: %[select face%] %v"
2415 :tag "Face"
2416 :value 'default
2417 :value-create 'widget-face-value-create
2418 :value-delete 'widget-face-value-delete
2419 :value-get 'widget-value-value-get
2420 :validate 'widget-children-validate
2421 :action 'widget-face-action
2422 :match '(lambda (widget value) (symbolp value)))
2424 (defun widget-face-value-create (widget)
2425 ;; Create a `custom-face' child.
2426 (let* ((symbol (widget-value widget))
2427 (custom-buffer-style 'face)
2428 (child (widget-create-child-and-convert
2429 widget 'custom-face
2430 :custom-level nil
2431 :value symbol)))
2432 (custom-magic-reset child)
2433 (setq custom-options (cons child custom-options))
2434 (widget-put widget :children (list child))))
2436 (defun widget-face-value-delete (widget)
2437 ;; Remove the child from the options.
2438 (let ((child (car (widget-get widget :children))))
2439 (setq custom-options (delq child custom-options))
2440 (widget-children-value-delete widget)))
2442 (defvar face-history nil
2443 "History of entered face names.")
2445 (defun widget-face-action (widget &optional event)
2446 "Prompt for a face."
2447 (let ((answer (completing-read "Face: "
2448 (mapcar (lambda (face)
2449 (list (symbol-name face)))
2450 (face-list))
2451 nil nil nil
2452 'face-history)))
2453 (unless (zerop (length answer))
2454 (widget-value-set widget (intern answer))
2455 (widget-apply widget :notify widget event)
2456 (widget-setup))))
2458 ;;; The `hook' Widget.
2460 (define-widget 'hook 'list
2461 "A emacs lisp hook"
2462 :convert-widget 'custom-hook-convert-widget
2463 :tag "Hook")
2465 (defun custom-hook-convert-widget (widget)
2466 ;; Handle `:custom-options'.
2467 (let* ((options (widget-get widget :options))
2468 (other `(editable-list :inline t
2469 :entry-format "%i %d%v"
2470 (function :format " %v")))
2471 (args (if options
2472 (list `(checklist :inline t
2473 ,@(mapcar (lambda (entry)
2474 `(function-item ,entry))
2475 options))
2476 other)
2477 (list other))))
2478 (widget-put widget :args args)
2479 widget))
2481 ;;; The `custom-group-link' Widget.
2483 (define-widget 'custom-group-link 'link
2484 "Show parent in other window when activated."
2485 :help-echo "Create customization buffer for this group."
2486 :action 'custom-group-link-action)
2488 (defun custom-group-link-action (widget &rest ignore)
2489 (customize-group (widget-value widget)))
2491 ;;; The `custom-group' Widget.
2493 (defcustom custom-group-tag-faces nil
2494 ;; In XEmacs, this ought to play games with font size.
2495 "Face used for group tags.
2496 The first member is used for level 1 groups, the second for level 2,
2497 and so forth. The remaining group tags are shown with
2498 `custom-group-tag-face'."
2499 :type '(repeat face)
2500 :group 'custom-faces)
2502 (defface custom-group-tag-face-1 '((((class color)
2503 (background dark))
2504 (:foreground "pink" :underline t))
2505 (((class color)
2506 (background light))
2507 (:foreground "red" :underline t))
2508 (t (:underline t)))
2509 "Face used for group tags.")
2511 (defface custom-group-tag-face '((((class color)
2512 (background dark))
2513 (:foreground "light blue" :underline t))
2514 (((class color)
2515 (background light))
2516 (:foreground "blue" :underline t))
2517 (t (:underline t)))
2518 "Face used for low level group tags."
2519 :group 'custom-faces)
2521 (define-widget 'custom-group 'custom
2522 "Customize group."
2523 :format "%v"
2524 :sample-face-get 'custom-group-sample-face-get
2525 :documentation-property 'group-documentation
2526 :help-echo "Set or reset all members of this group."
2527 :value-create 'custom-group-value-create
2528 :action 'custom-group-action
2529 :custom-category 'group
2530 :custom-set 'custom-group-set
2531 :custom-save 'custom-group-save
2532 :custom-reset-current 'custom-group-reset-current
2533 :custom-reset-saved 'custom-group-reset-saved
2534 :custom-reset-standard 'custom-group-reset-standard
2535 :custom-menu 'custom-group-menu-create)
2537 (defun custom-group-sample-face-get (widget)
2538 ;; Use :sample-face.
2539 (or (nth (1- (widget-get widget :custom-level)) custom-group-tag-faces)
2540 'custom-group-tag-face))
2542 (define-widget 'custom-group-visibility 'visibility
2543 "An indicator and manipulator for hidden group contents."
2544 :create 'custom-group-visibility-create)
2546 (defun custom-group-visibility-create (widget)
2547 (let ((visible (widget-value widget)))
2548 (if visible
2549 (insert "--------")))
2550 (widget-default-create widget))
2552 (defun custom-group-value-create (widget)
2553 "Insert a customize group for WIDGET in the current buffer."
2554 (let ((state (widget-get widget :custom-state))
2555 (level (widget-get widget :custom-level))
2556 (indent (widget-get widget :indent))
2557 (prefix (widget-get widget :custom-prefix))
2558 (buttons (widget-get widget :buttons))
2559 (tag (widget-get widget :tag))
2560 (symbol (widget-value widget)))
2561 (cond ((and (eq custom-buffer-style 'tree)
2562 (eq state 'hidden)
2563 (or (get symbol 'custom-group)
2564 (custom-unloaded-widget-p widget)))
2565 (custom-browse-insert-prefix prefix)
2566 (push (widget-create-child-and-convert
2567 widget 'custom-browse-visibility
2568 ;; :tag-glyph "plus"
2569 :tag (if (custom-unloaded-widget-p widget) "?" "+"))
2570 buttons)
2571 (insert "-- ")
2572 ;; (widget-glyph-insert nil "-- " "horizontal")
2573 (push (widget-create-child-and-convert
2574 widget 'custom-browse-group-tag)
2575 buttons)
2576 (insert " " tag "\n")
2577 (widget-put widget :buttons buttons))
2578 ((and (eq custom-buffer-style 'tree)
2579 (zerop (length (get symbol 'custom-group))))
2580 (custom-browse-insert-prefix prefix)
2581 (insert "[ ]-- ")
2582 ;; (widget-glyph-insert nil "[ ]" "empty")
2583 ;; (widget-glyph-insert nil "-- " "horizontal")
2584 (push (widget-create-child-and-convert
2585 widget 'custom-browse-group-tag)
2586 buttons)
2587 (insert " " tag "\n")
2588 (widget-put widget :buttons buttons))
2589 ((eq custom-buffer-style 'tree)
2590 (custom-browse-insert-prefix prefix)
2591 (custom-load-widget widget)
2592 (if (zerop (length (get symbol 'custom-group)))
2593 (progn
2594 (custom-browse-insert-prefix prefix)
2595 (insert "[ ]-- ")
2596 ;; (widget-glyph-insert nil "[ ]" "empty")
2597 ;; (widget-glyph-insert nil "-- " "horizontal")
2598 (push (widget-create-child-and-convert
2599 widget 'custom-browse-group-tag)
2600 buttons)
2601 (insert " " tag "\n")
2602 (widget-put widget :buttons buttons))
2603 (push (widget-create-child-and-convert
2604 widget 'custom-browse-visibility
2605 ;; :tag-glyph "minus"
2606 :tag "-")
2607 buttons)
2608 (insert "-\\ ")
2609 ;; (widget-glyph-insert nil "-\\ " "top")
2610 (push (widget-create-child-and-convert
2611 widget 'custom-browse-group-tag)
2612 buttons)
2613 (insert " " tag "\n")
2614 (widget-put widget :buttons buttons)
2615 (message "Creating group...")
2616 (let* ((members (custom-sort-items (get symbol 'custom-group)
2617 custom-browse-sort-alphabetically
2618 custom-browse-order-groups))
2619 (prefixes (widget-get widget :custom-prefixes))
2620 (custom-prefix-list (custom-prefix-add symbol prefixes))
2621 (extra-prefix (if (widget-get widget :custom-last)
2623 " | "))
2624 (prefix (concat prefix extra-prefix))
2625 children entry)
2626 (while members
2627 (setq entry (car members)
2628 members (cdr members))
2629 (when (or (not custom-browse-only-groups)
2630 (eq (nth 1 entry) 'custom-group))
2631 (push (widget-create-child-and-convert
2632 widget (nth 1 entry)
2633 :group widget
2634 :tag (custom-unlispify-tag-name (nth 0 entry))
2635 :custom-prefixes custom-prefix-list
2636 :custom-level (1+ level)
2637 :custom-last (null members)
2638 :value (nth 0 entry)
2639 :custom-prefix prefix)
2640 children)))
2641 (widget-put widget :children (reverse children)))
2642 (message "Creating group...done")))
2643 ;; Nested style.
2644 ((eq state 'hidden)
2645 ;; Create level indicator.
2646 (unless (eq custom-buffer-style 'links)
2647 (insert-char ?\ (* custom-buffer-indent (1- level)))
2648 (insert "-- "))
2649 ;; Create tag.
2650 (let ((begin (point)))
2651 (insert tag)
2652 (widget-specify-sample widget begin (point)))
2653 (insert " group: ")
2654 ;; Create link/visibility indicator.
2655 (if (eq custom-buffer-style 'links)
2656 (push (widget-create-child-and-convert
2657 widget 'custom-group-link
2658 :tag "Go to Group"
2659 symbol)
2660 buttons)
2661 (push (widget-create-child-and-convert
2662 widget 'group-visibility
2663 :help-echo "Show members of this group."
2664 :action 'custom-toggle-parent
2665 (not (eq state 'hidden)))
2666 buttons))
2667 (insert " \n")
2668 ;; Create magic button.
2669 (let ((magic (widget-create-child-and-convert
2670 widget 'custom-magic nil)))
2671 (widget-put widget :custom-magic magic)
2672 (push magic buttons))
2673 ;; Update buttons.
2674 (widget-put widget :buttons buttons)
2675 ;; Insert documentation.
2676 (if (and (eq custom-buffer-style 'links) (> level 1))
2677 (widget-put widget :documentation-indent 0))
2678 (widget-default-format-handler widget ?h))
2679 ;; Nested style.
2680 (t ;Visible.
2681 ;; Add parent groups references above the group.
2682 (if t ;;; This should test that the buffer
2683 ;;; was made to display a group.
2684 (when (eq level 1)
2685 (if (custom-add-parent-links widget
2686 "Go to parent group:")
2687 (insert "\n"))))
2688 ;; Create level indicator.
2689 (insert-char ?\ (* custom-buffer-indent (1- level)))
2690 (insert "/- ")
2691 ;; Create tag.
2692 (let ((start (point)))
2693 (insert tag)
2694 (widget-specify-sample widget start (point)))
2695 (insert " group: ")
2696 ;; Create visibility indicator.
2697 (unless (eq custom-buffer-style 'links)
2698 (insert "--------")
2699 (push (widget-create-child-and-convert
2700 widget 'visibility
2701 :help-echo "Hide members of this group."
2702 :action 'custom-toggle-parent
2703 (not (eq state 'hidden)))
2704 buttons)
2705 (insert " "))
2706 ;; Create more dashes.
2707 ;; Use 76 instead of 75 to compensate for the temporary "<"
2708 ;; added by `widget-insert'.
2709 (insert-char ?- (- 76 (current-column)
2710 (* custom-buffer-indent level)))
2711 (insert "\\\n")
2712 ;; Create magic button.
2713 (let ((magic (widget-create-child-and-convert
2714 widget 'custom-magic
2715 :indent 0
2716 nil)))
2717 (widget-put widget :custom-magic magic)
2718 (push magic buttons))
2719 ;; Update buttons.
2720 (widget-put widget :buttons buttons)
2721 ;; Insert documentation.
2722 (widget-default-format-handler widget ?h)
2723 ;; Parent groups.
2724 (if nil ;;; This should test that the buffer
2725 ;;; was not made to display a group.
2726 (when (eq level 1)
2727 (insert-char ?\ custom-buffer-indent)
2728 (custom-add-parent-links widget)))
2729 (custom-add-see-also widget
2730 (make-string (* custom-buffer-indent level)
2731 ?\ ))
2732 ;; Members.
2733 (message "Creating group...")
2734 (custom-load-widget widget)
2735 (let* ((members (custom-sort-items (get symbol 'custom-group)
2736 custom-buffer-sort-alphabetically
2737 custom-buffer-order-groups))
2738 (prefixes (widget-get widget :custom-prefixes))
2739 (custom-prefix-list (custom-prefix-add symbol prefixes))
2740 (length (length members))
2741 (count 0)
2742 (children (mapcar (lambda (entry)
2743 (widget-insert "\n")
2744 (message "\
2745 Creating group members... %2d%%"
2746 (/ (* 100.0 count) length))
2747 (setq count (1+ count))
2748 (prog1
2749 (widget-create-child-and-convert
2750 widget (nth 1 entry)
2751 :group widget
2752 :tag (custom-unlispify-tag-name
2753 (nth 0 entry))
2754 :custom-prefixes custom-prefix-list
2755 :custom-level (1+ level)
2756 :value (nth 0 entry))
2757 (unless (eq (preceding-char) ?\n)
2758 (widget-insert "\n"))))
2759 members)))
2760 (message "Creating group magic...")
2761 (mapcar 'custom-magic-reset children)
2762 (message "Creating group state...")
2763 (widget-put widget :children children)
2764 (custom-group-state-update widget)
2765 (message "Creating group... done"))
2766 ;; End line
2767 (insert "\n")
2768 (insert-char ?\ (* custom-buffer-indent (1- level)))
2769 (insert "\\- " (widget-get widget :tag) " group end ")
2770 (insert-char ?- (- 75 (current-column) (* custom-buffer-indent level)))
2771 (insert "/\n")))))
2773 (defvar custom-group-menu
2774 '(("Set for Current Session" custom-group-set
2775 (lambda (widget)
2776 (eq (widget-get widget :custom-state) 'modified)))
2777 ("Save for Future Sessions" custom-group-save
2778 (lambda (widget)
2779 (memq (widget-get widget :custom-state) '(modified set))))
2780 ("Reset to Current" custom-group-reset-current
2781 (lambda (widget)
2782 (memq (widget-get widget :custom-state) '(modified))))
2783 ("Reset to Saved" custom-group-reset-saved
2784 (lambda (widget)
2785 (memq (widget-get widget :custom-state) '(modified set))))
2786 ("Reset to standard setting" custom-group-reset-standard
2787 (lambda (widget)
2788 (memq (widget-get widget :custom-state) '(modified set saved)))))
2789 "Alist of actions for the `custom-group' widget.
2790 Each entry has the form (NAME ACTION FILTER) where NAME is the name of
2791 the menu entry, ACTION is the function to call on the widget when the
2792 menu is selected, and FILTER is a predicate which takes a `custom-group'
2793 widget as an argument, and returns non-nil if ACTION is valid on that
2794 widget. If FILTER is nil, ACTION is always valid.")
2796 (defun custom-group-action (widget &optional event)
2797 "Show the menu for `custom-group' WIDGET.
2798 Optional EVENT is the location for the menu."
2799 (if (eq (widget-get widget :custom-state) 'hidden)
2800 (custom-toggle-hide widget)
2801 (let* ((completion-ignore-case t)
2802 (answer (widget-choose (concat "Operation on "
2803 (custom-unlispify-tag-name
2804 (widget-get widget :value)))
2805 (custom-menu-filter custom-group-menu
2806 widget)
2807 event)))
2808 (if answer
2809 (funcall answer widget)))))
2811 (defun custom-group-set (widget)
2812 "Set changes in all modified group members."
2813 (let ((children (widget-get widget :children)))
2814 (mapcar (lambda (child)
2815 (when (eq (widget-get child :custom-state) 'modified)
2816 (widget-apply child :custom-set)))
2817 children )))
2819 (defun custom-group-save (widget)
2820 "Save all modified group members."
2821 (let ((children (widget-get widget :children)))
2822 (mapcar (lambda (child)
2823 (when (memq (widget-get child :custom-state) '(modified set))
2824 (widget-apply child :custom-save)))
2825 children )))
2827 (defun custom-group-reset-current (widget)
2828 "Reset all modified group members."
2829 (let ((children (widget-get widget :children)))
2830 (mapcar (lambda (child)
2831 (when (eq (widget-get child :custom-state) 'modified)
2832 (widget-apply child :custom-reset-current)))
2833 children )))
2835 (defun custom-group-reset-saved (widget)
2836 "Reset all modified or set group members."
2837 (let ((children (widget-get widget :children)))
2838 (mapcar (lambda (child)
2839 (when (memq (widget-get child :custom-state) '(modified set))
2840 (widget-apply child :custom-reset-saved)))
2841 children )))
2843 (defun custom-group-reset-standard (widget)
2844 "Reset all modified, set, or saved group members."
2845 (let ((children (widget-get widget :children)))
2846 (mapcar (lambda (child)
2847 (when (memq (widget-get child :custom-state)
2848 '(modified set saved))
2849 (widget-apply child :custom-reset-standard)))
2850 children )))
2852 (defun custom-group-state-update (widget)
2853 "Update magic."
2854 (unless (eq (widget-get widget :custom-state) 'hidden)
2855 (let* ((children (widget-get widget :children))
2856 (states (mapcar (lambda (child)
2857 (widget-get child :custom-state))
2858 children))
2859 (magics custom-magic-alist)
2860 (found 'standard))
2861 (while magics
2862 (let ((magic (car (car magics))))
2863 (if (and (not (eq magic 'hidden))
2864 (memq magic states))
2865 (setq found magic
2866 magics nil)
2867 (setq magics (cdr magics)))))
2868 (widget-put widget :custom-state found)))
2869 (custom-magic-reset widget))
2871 ;;; The `custom-save-all' Function.
2872 ;;;###autoload
2873 (defcustom custom-file (if (featurep 'xemacs)
2874 "~/.xemacs-custom"
2875 "~/.emacs")
2876 "File used for storing customization information.
2877 If you change this from the default \"~/.emacs\" you need to
2878 explicitly load that file for the settings to take effect."
2879 :type 'file
2880 :group 'customize)
2882 (defun custom-save-delete (symbol)
2883 "Delete the call to SYMBOL form `custom-file'.
2884 Leave point at the location of the call, or after the last expression."
2885 (set-buffer (find-file-noselect custom-file))
2886 (goto-char (point-min))
2887 (catch 'found
2888 (while t
2889 (let ((sexp (condition-case nil
2890 (read (current-buffer))
2891 (end-of-file (throw 'found nil)))))
2892 (when (and (listp sexp)
2893 (eq (car sexp) symbol))
2894 (delete-region (save-excursion
2895 (backward-sexp)
2896 (point))
2897 (point))
2898 (throw 'found nil))))))
2900 (defun custom-save-variables ()
2901 "Save all customized variables in `custom-file'."
2902 (save-excursion
2903 (custom-save-delete 'custom-set-variables)
2904 (let ((standard-output (current-buffer)))
2905 (unless (bolp)
2906 (princ "\n"))
2907 (princ "(custom-set-variables")
2908 (mapatoms (lambda (symbol)
2909 (let ((value (get symbol 'saved-value))
2910 (requests (get symbol 'custom-requests))
2911 (now (not (or (get symbol 'standard-value)
2912 (and (not (boundp symbol))
2913 (not (get symbol 'force-value)))))))
2914 (when value
2915 (princ "\n '(")
2916 (princ symbol)
2917 (princ " ")
2918 (prin1 (car value))
2919 (cond (requests
2920 (if now
2921 (princ " t ")
2922 (princ " nil "))
2923 (prin1 requests)
2924 (princ ")"))
2925 (now
2926 (princ " t)"))
2928 (princ ")")))))))
2929 (princ ")")
2930 (unless (looking-at "\n")
2931 (princ "\n")))))
2933 (defun custom-save-faces ()
2934 "Save all customized faces in `custom-file'."
2935 (save-excursion
2936 (custom-save-delete 'custom-set-faces)
2937 (let ((standard-output (current-buffer)))
2938 (unless (bolp)
2939 (princ "\n"))
2940 (princ "(custom-set-faces")
2941 (let ((value (get 'default 'saved-face)))
2942 ;; The default face must be first, since it affects the others.
2943 (when value
2944 (princ "\n '(default ")
2945 (prin1 value)
2946 (if (or (get 'default 'face-defface-spec)
2947 (and (not (custom-facep 'default))
2948 (not (get 'default 'force-face))))
2949 (princ ")")
2950 (princ " t)"))))
2951 (mapatoms (lambda (symbol)
2952 (let ((value (get symbol 'saved-face)))
2953 (when (and (not (eq symbol 'default))
2954 ;; Don't print default face here.
2955 value)
2956 (princ "\n '(")
2957 (princ symbol)
2958 (princ " ")
2959 (prin1 value)
2960 (if (or (get symbol 'face-defface-spec)
2961 (and (not (custom-facep symbol))
2962 (not (get symbol 'force-face))))
2963 (princ ")")
2964 (princ " t)"))))))
2965 (princ ")")
2966 (unless (looking-at "\n")
2967 (princ "\n")))))
2969 ;;;###autoload
2970 (defun customize-save-customized ()
2971 "Save all user options which have been set in this session."
2972 (interactive)
2973 (mapatoms (lambda (symbol)
2974 (let ((face (get symbol 'customized-face))
2975 (value (get symbol 'customized-value)))
2976 (when face
2977 (put symbol 'saved-face face)
2978 (put symbol 'customized-face nil))
2979 (when value
2980 (put symbol 'saved-value value)
2981 (put symbol 'customized-value nil)))))
2982 ;; We really should update all custom buffers here.
2983 (custom-save-all))
2985 ;;;###autoload
2986 (defun custom-save-all ()
2987 "Save all customizations in `custom-file'."
2988 (custom-save-variables)
2989 (custom-save-faces)
2990 (save-excursion
2991 (set-buffer (find-file-noselect custom-file))
2992 (save-buffer)))
2994 ;;; The Customize Menu.
2996 ;;; Menu support
2998 (defcustom custom-menu-nesting 2
2999 "Maximum nesting in custom menus."
3000 :type 'integer
3001 :group 'custom-menu)
3003 (defun custom-face-menu-create (widget symbol)
3004 "Ignoring WIDGET, create a menu entry for customization face SYMBOL."
3005 (vector (custom-unlispify-menu-entry symbol)
3006 `(customize-face ',symbol)
3009 (defun custom-variable-menu-create (widget symbol)
3010 "Ignoring WIDGET, create a menu entry for customization variable SYMBOL."
3011 (let ((type (get symbol 'custom-type)))
3012 (unless (listp type)
3013 (setq type (list type)))
3014 (if (and type (widget-get type :custom-menu))
3015 (widget-apply type :custom-menu symbol)
3016 (vector (custom-unlispify-menu-entry symbol)
3017 `(customize-variable ',symbol)
3018 t))))
3020 ;; Add checkboxes to boolean variable entries.
3021 (widget-put (get 'boolean 'widget-type)
3022 :custom-menu (lambda (widget symbol)
3023 (vector (custom-unlispify-menu-entry symbol)
3024 `(customize-variable ',symbol)
3025 ':style 'toggle
3026 ':selected symbol)))
3028 (if (string-match "XEmacs" emacs-version)
3029 ;; XEmacs can create menus dynamically.
3030 (defun custom-group-menu-create (widget symbol)
3031 "Ignoring WIDGET, create a menu entry for customization group SYMBOL."
3032 `( ,(custom-unlispify-menu-entry symbol t)
3033 :filter (lambda (&rest junk)
3034 (cdr (custom-menu-create ',symbol)))))
3035 ;; But emacs can't.
3036 (defun custom-group-menu-create (widget symbol)
3037 "Ignoring WIDGET, create a menu entry for customization group SYMBOL."
3038 ;; Limit the nesting.
3039 (let ((custom-menu-nesting (1- custom-menu-nesting)))
3040 (custom-menu-create symbol))))
3042 ;;;###autoload
3043 (defun custom-menu-create (symbol)
3044 "Create menu for customization group SYMBOL.
3045 The menu is in a format applicable to `easy-menu-define'."
3046 (let* ((item (vector (custom-unlispify-menu-entry symbol)
3047 `(customize-group ',symbol)
3048 t)))
3049 (if (and (or (not (boundp 'custom-menu-nesting))
3050 (>= custom-menu-nesting 0))
3051 (< (length (get symbol 'custom-group)) widget-menu-max-size))
3052 (let ((custom-prefix-list (custom-prefix-add symbol
3053 custom-prefix-list))
3054 (members (custom-sort-items (get symbol 'custom-group)
3055 custom-menu-sort-alphabetically
3056 custom-menu-order-groups)))
3057 (custom-load-symbol symbol)
3058 `(,(custom-unlispify-menu-entry symbol t)
3059 ,item
3060 "--"
3061 ,@(mapcar (lambda (entry)
3062 (widget-apply (if (listp (nth 1 entry))
3063 (nth 1 entry)
3064 (list (nth 1 entry)))
3065 :custom-menu (nth 0 entry)))
3066 members)))
3067 item)))
3069 ;;;###autoload
3070 (defun customize-menu-create (symbol &optional name)
3071 "Return a customize menu for customization group SYMBOL.
3072 If optional NAME is given, use that as the name of the menu.
3073 Otherwise the menu will be named `Customize'.
3074 The format is suitable for use with `easy-menu-define'."
3075 (unless name
3076 (setq name "Customize"))
3077 (if (string-match "XEmacs" emacs-version)
3078 ;; We can delay it under XEmacs.
3079 `(,name
3080 :filter (lambda (&rest junk)
3081 (cdr (custom-menu-create ',symbol))))
3082 ;; But we must create it now under Emacs.
3083 (cons name (cdr (custom-menu-create symbol)))))
3085 ;;; The Custom Mode.
3087 (defvar custom-mode-map nil
3088 "Keymap for `custom-mode'.")
3090 (unless custom-mode-map
3091 (setq custom-mode-map (make-sparse-keymap))
3092 (set-keymap-parent custom-mode-map widget-keymap)
3093 (suppress-keymap custom-mode-map)
3094 (define-key custom-mode-map " " 'scroll-up)
3095 (define-key custom-mode-map "\177" 'scroll-down)
3096 (define-key custom-mode-map "q" 'bury-buffer)
3097 (define-key custom-mode-map "u" 'Custom-goto-parent)
3098 (define-key custom-mode-map "n" 'widget-forward)
3099 (define-key custom-mode-map "p" 'widget-backward)
3100 (define-key custom-mode-map [mouse-1] 'Custom-move-and-invoke))
3102 (defun Custom-move-and-invoke (event)
3103 "Move to where you click, and if it is an active field, invoke it."
3104 (interactive "e")
3105 (mouse-set-point event)
3106 (if (widget-event-point event)
3107 (let* ((pos (widget-event-point event))
3108 (button (get-char-property pos 'button)))
3109 (if button
3110 (widget-button-click event)))))
3112 (easy-menu-define Custom-mode-menu
3113 custom-mode-map
3114 "Menu used in customization buffers."
3115 `("Custom"
3116 ,(customize-menu-create 'customize)
3117 ["Set" Custom-set t]
3118 ["Save" Custom-save t]
3119 ["Reset to Current" Custom-reset-current t]
3120 ["Reset to Saved" Custom-reset-saved t]
3121 ["Reset to Standard Settings" Custom-reset-standard t]
3122 ["Info" (Info-goto-node "(custom)The Customization Buffer") t]))
3124 (defun Custom-goto-parent ()
3125 "Go to the parent group listed at the top of this buffer.
3126 If several parents are listed, go to the first of them."
3127 (interactive)
3128 (save-excursion
3129 (goto-char (point-min))
3130 (if (search-forward "\nGo to parent group: " nil t)
3131 (let* ((button (get-char-property (point) 'button))
3132 (parent (downcase (widget-get button :tag))))
3133 (customize-group parent)))))
3135 (defcustom custom-mode-hook nil
3136 "Hook called when entering custom-mode."
3137 :type 'hook
3138 :group 'custom-buffer )
3140 (defun custom-state-buffer-message (widget)
3141 (if (eq (widget-get (widget-get widget :parent) :custom-state) 'modified)
3142 (message "To install your edits, invoke [State] and choose the Set operation")))
3144 (defun custom-mode ()
3145 "Major mode for editing customization buffers.
3147 The following commands are available:
3149 Move to next button or editable field. \\[widget-forward]
3150 Move to previous button or editable field. \\[widget-backward]
3151 Invoke button under the mouse pointer. \\[Custom-move-and-invoke]
3152 Invoke button under point. \\[widget-button-press]
3153 Set all modifications. \\[Custom-set]
3154 Make all modifications default. \\[Custom-save]
3155 Reset all modified options. \\[Custom-reset-current]
3156 Reset all modified or set options. \\[Custom-reset-saved]
3157 Reset all options. \\[Custom-reset-standard]
3159 Entry to this mode calls the value of `custom-mode-hook'
3160 if that value is non-nil."
3161 (kill-all-local-variables)
3162 (setq major-mode 'custom-mode
3163 mode-name "Custom")
3164 (use-local-map custom-mode-map)
3165 (easy-menu-add Custom-mode-menu)
3166 (make-local-variable 'custom-options)
3167 (make-local-variable 'widget-documentation-face)
3168 (setq widget-documentation-face 'custom-documentation-face)
3169 (make-local-variable 'widget-button-face)
3170 (setq widget-button-face 'custom-button-face)
3171 (make-local-hook 'widget-edit-functions)
3172 (add-hook 'widget-edit-functions 'custom-state-buffer-message nil t)
3173 (run-hooks 'custom-mode-hook))
3175 ;;; The End.
3177 (provide 'cus-edit)
3179 ;; cus-edit.el ends here