1 ;;; cus-edit.el --- Tools for customization Emacs.
3 ;; Copyright (C) 1996, 1997 Free Software Foundation, Inc.
5 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
6 ;; Keywords: help, faces
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)
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.
37 (defun custom-face-display-set (face spec &optional frame)
38 (face-spec-set face spec frame))
40 (defun custom-display-match-frame (display frame)
41 (face-spec-set-match-display display frame))
43 (define-widget-keywords :custom-prefixes :custom-menu :custom-show
44 :custom-magic :custom-state :custom-level :custom-form
45 :custom-set :custom-save :custom-reset-current :custom-reset-saved
46 :custom-reset-factory)
48 (put 'custom-define-hook 'custom-type 'hook)
49 (put 'custom-define-hook 'factory-value '(nil))
50 (custom-add-to-group 'customize 'custom-define-hook 'custom-variable)
52 ;;; Customization Groups.
55 "Customization of the One True Editor."
56 :link '(custom-manual "(emacs)Top"))
58 ;; Most of these groups are stolen from `finder.el',
60 "Basic text editing facilities."
64 "Abbreviation handling, typing shortcuts, macros."
68 (defgroup matching nil
69 "Various sorts of searching and matching."
72 (defgroup emulations nil
73 "Emulations of other editors."
80 (defgroup outlines nil
81 "Support for hierarchical outlining."
84 (defgroup external nil
85 "Interfacing to external utilities."
89 "Code related to the `bib' bibliography processor."
93 (defgroup processes nil
94 "Process, subshell, compilation, and job control support."
98 (defgroup programming nil
99 "Support for programming in other languages."
102 (defgroup languages nil
103 "Specialized modes for editing programming languages."
107 "Lisp support, including Emacs Lisp."
112 "Support for the C language and related languages."
120 "Support for object-oriented programming."
123 (defgroup applications nil
124 "Applications written in Emacs."
127 (defgroup calendar nil
128 "Calendar and time management support."
129 :group 'applications)
132 "Modes for electronic-mail handling."
133 :group 'applications)
136 "Support for netnews reading and posting."
137 :group 'applications)
140 "Games, jokes and amusements."
141 :group 'applications)
143 (defgroup development nil
144 "Support for further development of Emacs."
148 "Support for Emacs documentation."
151 (defgroup extensions nil
152 "Emacs Lisp language extensions."
155 (defgroup internal nil
156 "Code for Emacs internals, build process, defaults."
160 "Maintenance aids for the Emacs development group."
164 (defgroup environment nil
165 "Fitting Emacs with its environment."
169 "Communications, networking, remote access to files."
173 (defgroup hardware nil
174 "Support for interfacing with exotic hardware."
177 (defgroup terminals nil
178 "Support for terminal types."
182 "Front-ends/assistants for, or emulators of, UNIX features."
186 "Support code for vms."
190 "Internationalization and alternate character-set support."
195 "Support for Emacs frames and window systems."
199 "Support editing files of data."
207 "Code related to the TeX formatter."
211 "Support for multiple fonts."
214 (defgroup hypermedia nil
215 "Support for links between text or other media types."
219 "Support for on-line help systems."
223 "Code local to your site."
226 (defgroup customize '((widgets custom-group))
227 "Customization of the Customization support."
228 :link '(custom-manual "(custom)Top")
229 :link '(url-link :tag "Development Page"
230 "http://www.dina.kvl.dk/~abraham/custom/")
234 (defgroup custom-faces nil
235 "Faces used by customize."
239 (defgroup abbrev-mode nil
240 "Word abbreviations mode."
244 "Storage allocation and gc for GNU Emacs Lisp interpreter."
245 :tag "Storage Allocation"
249 "Undoing changes in buffers."
252 (defgroup modeline nil
253 "Content of the modeline."
257 "Indenting and filling text."
260 (defgroup editing-basics nil
261 "Most basic editing facilities."
264 (defgroup display nil
265 "How characters are displayed in buffers."
268 (defgroup execute nil
269 "Executing external commands."
272 (defgroup installation nil
273 "The Emacs installation."
281 "Internal Emacs limits."
285 "Debugging Emacs itself."
288 (defgroup minibuffer nil
289 "Controling the behaviour of the minibuffer."
292 (defgroup keyboard nil
293 "Input from the keyboard."
297 "Input from the mouse."
301 "Input from the menus."
304 (defgroup auto-save nil
305 "Preventing accidential loss of data."
308 (defgroup processes-basics nil
309 "Basic stuff dealing with processes."
312 (defgroup windows nil
313 "Windows within a frame."
318 (defun custom-quote (sexp)
319 "Quote SEXP iff it is not self quoting."
320 (if (or (memq sexp '(t nil))
322 (eq (aref (symbol-name sexp) 0) ?:))
324 (memq (car sexp) '(lambda)))
327 (and (fboundp 'characterp)
332 (defun custom-split-regexp-maybe (regexp)
333 "If REGEXP is a string, split it to a list at `\\|'.
334 You can get the original back with from the result with:
335 (mapconcat 'identity result \"\\|\")
337 IF REGEXP is not a string, return it unchanged."
341 (while (string-match "\\\\|" regexp start)
342 (setq all (cons (substring regexp start (match-beginning 0)) all)
343 start (match-end 0)))
344 (nreverse (cons (substring regexp start) all)))
347 (defun custom-variable-prompt ()
348 ;; Code stolen from `help.el'.
349 "Prompt for a variable, defaulting to the variable at point.
350 Return a list suitable for use in `interactive'."
351 (let ((v (variable-at-point))
352 (enable-recursive-minibuffers t)
354 (setq val (completing-read
356 (format "Customize variable (default %s): " v)
357 "Customize variable: ")
359 (list (if (equal val "")
364 (defvar custom-prefix-list nil
365 "List of prefixes that should be ignored by `custom-unlispify'")
367 (defcustom custom-unlispify-menu-entries t
368 "Display menu entries as words instead of symbols if non nil."
372 (defun custom-unlispify-menu-entry (symbol &optional no-suffix)
373 "Convert symbol into a menu entry."
374 (cond ((not custom-unlispify-menu-entries)
375 (symbol-name symbol))
376 ((get symbol 'custom-tag)
378 (get symbol 'custom-tag)
379 (concat (get symbol 'custom-tag) "...")))
382 (set-buffer (get-buffer-create " *Custom-Work*"))
384 (princ symbol (current-buffer))
385 (goto-char (point-min))
386 (when (and (eq (get symbol 'custom-type) 'boolean)
387 (re-search-forward "-p\\'" nil t))
388 (replace-match "" t t)
389 (goto-char (point-min)))
390 (let ((prefixes custom-prefix-list)
393 (setq prefix (car prefixes))
394 (if (search-forward prefix (+ (point) (length prefix)) t)
397 (delete-region (point-min) (point)))
398 (setq prefixes (cdr prefixes)))))
399 (subst-char-in-region (point-min) (point-max) ?- ?\ t)
400 (capitalize-region (point-min) (point-max))
402 (goto-char (point-max))
406 (defcustom custom-unlispify-tag-names t
407 "Display tag names as words instead of symbols if non nil."
411 (defun custom-unlispify-tag-name (symbol)
412 "Convert symbol into a menu entry."
413 (let ((custom-unlispify-menu-entries custom-unlispify-tag-names))
414 (custom-unlispify-menu-entry symbol t)))
416 (defun custom-prefix-add (symbol prefixes)
417 ;; Addd SYMBOL to list of ignored PREFIXES.
418 (cons (or (get symbol 'custom-prefix)
419 (concat (symbol-name symbol) "-"))
424 (defcustom custom-guess-name-alist
429 ("-function\\'" function)
430 ("-functions\\'" (repeat function))
431 ("-list\\'" (repeat sexp))
432 ("-alist\\'" (repeat (cons sexp sexp))))
433 "Alist of (MATCH TYPE).
435 MATCH should be a regexp matching the name of a symbol, and TYPE should
436 be a widget suitable for editing the value of that symbol. The TYPE
437 of the first entry where MATCH matches the name of the symbol will be
440 This is used for guessing the type of variables not declared with
442 :type '(repeat (group (regexp :tag "Match") (sexp :tag "Type")))
445 (defcustom custom-guess-doc-alist
446 '(("\\`\\*?Non-nil " boolean))
447 "Alist of (MATCH TYPE).
449 MATCH should be a regexp matching a documentation string, and TYPE
450 should be a widget suitable for editing the value of a variable with
451 that documentation string. The TYPE of the first entry where MATCH
452 matches the name of the symbol will be used.
454 This is used for guessing the type of variables not declared with
456 :type '(repeat (group (regexp :tag "Match") (sexp :tag "Type")))
459 (defun custom-guess-type (symbol)
460 "Guess a widget suitable for editing the value of SYMBOL.
461 This is done by matching SYMBOL with `custom-guess-name-alist' and
462 if that fails, the doc string with `custom-guess-doc-alist'."
463 (let ((name (symbol-name symbol))
464 (names custom-guess-name-alist)
467 (setq current (car names)
469 (when (string-match (nth 0 current) name)
470 (setq found (nth 1 current)
473 (let ((doc (documentation-property symbol 'variable-documentation))
474 (docs custom-guess-doc-alist))
477 (setq current (car docs)
479 (when (string-match (nth 0 current) doc)
480 (setq found (nth 1 current)
484 ;;; Custom Mode Commands.
486 (defvar custom-options nil
487 "Customization widgets in the current buffer.")
490 "Set changes in all modified options."
492 (let ((children custom-options))
493 (mapcar (lambda (child)
494 (when (eq (widget-get child :custom-state) 'modified)
495 (widget-apply child :custom-set)))
498 (defun custom-save ()
499 "Set all modified group members and save them."
501 (let ((children custom-options))
502 (mapcar (lambda (child)
503 (when (memq (widget-get child :custom-state) '(modified set))
504 (widget-apply child :custom-save)))
508 (defvar custom-reset-menu
509 '(("Current" . custom-reset-current)
510 ("Saved" . custom-reset-saved)
511 ("Factory Settings" . custom-reset-factory))
512 "Alist of actions for the `Reset' button.
513 The key is a string containing the name of the action, the value is a
514 lisp function taking the widget as an element which will be called
515 when the action is chosen.")
517 (defun custom-reset (event)
518 "Select item from reset menu."
519 (let* ((completion-ignore-case t)
520 (answer (widget-choose "Reset to"
526 (defun custom-reset-current ()
527 "Reset all modified group members to their current value."
529 (let ((children custom-options))
530 (mapcar (lambda (child)
531 (when (eq (widget-get child :custom-state) 'modified)
532 (widget-apply child :custom-reset-current)))
535 (defun custom-reset-saved ()
536 "Reset all modified or set group members to their saved value."
538 (let ((children custom-options))
539 (mapcar (lambda (child)
540 (when (eq (widget-get child :custom-state) 'modified)
541 (widget-apply child :custom-reset-current)))
544 (defun custom-reset-factory ()
545 "Reset all modified, set, or saved group members to their factory settings."
547 (let ((children custom-options))
548 (mapcar (lambda (child)
549 (when (eq (widget-get child :custom-state) 'modified)
550 (widget-apply child :custom-reset-current)))
553 ;;; The Customize Commands
556 (defun customize (symbol)
557 "Customize SYMBOL, which must be a customization group."
558 (interactive (list (completing-read "Customize group: (default emacs) "
561 (get symbol 'custom-group))
564 (when (stringp symbol)
565 (if (string-equal "" symbol)
567 (setq symbol (intern symbol))))
568 (custom-buffer-create (list (list symbol 'custom-group))))
571 (defun customize-variable (symbol)
572 "Customize SYMBOL, which must be a variable."
573 (interactive (custom-variable-prompt))
574 (custom-buffer-create (list (list symbol 'custom-variable))))
577 (defun customize-variable-other-window (symbol)
578 "Customize SYMBOL, which must be a variable.
579 Show the buffer in another window, but don't select it."
580 (interactive (custom-variable-prompt))
581 (custom-buffer-create-other-window (list (list symbol 'custom-variable))))
584 (defun customize-face (&optional symbol)
585 "Customize SYMBOL, which should be a face name or nil.
586 If SYMBOL is nil, customize all faces."
587 (interactive (list (completing-read "Customize face: (default all) "
588 obarray 'custom-facep)))
589 (if (or (null symbol) (and (stringp symbol) (zerop (length symbol))))
591 (message "Looking for faces...")
592 (mapcar (lambda (symbol)
593 (setq found (cons (list symbol 'custom-face) found)))
594 (nreverse (mapcar 'intern
595 (sort (mapcar 'symbol-name (face-list))
598 (custom-buffer-create found))
600 (setq symbol (intern symbol)))
601 (unless (symbolp symbol)
602 (error "Should be a symbol %S" symbol))
603 (custom-buffer-create (list (list symbol 'custom-face)))))
606 (defun customize-face-other-window (&optional symbol)
607 "Show customization buffer for FACE in other window."
608 (interactive (list (completing-read "Customize face: "
609 obarray 'custom-facep)))
610 (if (or (null symbol) (and (stringp symbol) (zerop (length symbol))))
613 (setq symbol (intern symbol)))
614 (unless (symbolp symbol)
615 (error "Should be a symbol %S" symbol))
616 (custom-buffer-create-other-window (list (list symbol 'custom-face)))))
619 (defun customize-customized ()
620 "Customize all already customized user options."
623 (mapatoms (lambda (symbol)
624 (and (get symbol 'saved-face)
625 (custom-facep symbol)
626 (setq found (cons (list symbol 'custom-face) found)))
627 (and (get symbol 'saved-value)
630 (cons (list symbol 'custom-variable) found)))))
632 (custom-buffer-create found)
633 (error "No customized user options"))))
636 (defun customize-apropos (regexp &optional all)
637 "Customize all user options matching REGEXP.
638 If ALL (e.g., started with a prefix key), include options which are not
640 (interactive "sCustomize regexp: \nP")
642 (mapatoms (lambda (symbol)
643 (when (string-match regexp (symbol-name symbol))
644 (when (get symbol 'custom-group)
645 (setq found (cons (list symbol 'custom-group) found)))
646 (when (custom-facep symbol)
647 (setq found (cons (list symbol 'custom-face) found)))
648 (when (and (boundp symbol)
649 (or (get symbol 'saved-value)
650 (get symbol 'factory-value)
652 (get symbol 'variable-documentation)
653 (user-variable-p symbol))))
655 (cons (list symbol 'custom-variable) found))))))
657 (custom-buffer-create found)
658 (error "No matches"))))
661 (defun custom-buffer-create (options)
662 "Create a buffer containing OPTIONS.
663 OPTIONS should be an alist of the form ((SYMBOL WIDGET)...), where
664 SYMBOL is a customization option, and WIDGET is a widget for editing
666 (kill-buffer (get-buffer-create "*Customization*"))
667 (switch-to-buffer (get-buffer-create "*Customization*"))
668 (custom-buffer-create-internal options))
670 (defun custom-buffer-create-other-window (options)
671 "Create a buffer containing OPTIONS.
672 OPTIONS should be an alist of the form ((SYMBOL WIDGET)...), where
673 SYMBOL is a customization option, and WIDGET is a widget for editing
675 (kill-buffer (get-buffer-create "*Customization*"))
676 (let ((window (selected-window)))
677 (switch-to-buffer-other-window (get-buffer-create "*Customization*"))
678 (custom-buffer-create-internal options)
679 (select-window window)))
682 (defun custom-buffer-create-internal (options)
683 (message "Creating customization buffer...")
685 (widget-insert "This is a customization buffer.
686 Push RET or click mouse-2 on the word ")
687 ;; (put-text-property 1 2 'start-open nil)
688 (widget-create 'info-link
690 :help-echo "Read the online help."
691 "(custom)The Customization Buffer")
692 (widget-insert " for more information.\n\n")
694 (if (= (length options) 1)
695 (mapcar (lambda (entry)
696 (widget-create (nth 1 entry)
697 :custom-state 'unknown
698 :tag (custom-unlispify-tag-name
700 :value (nth 0 entry)))
703 (length (length options)))
704 (mapcar (lambda (entry)
706 (message "Creating customization items %2d%%..."
707 (/ (* 100.0 count) length))
708 (widget-create (nth 1 entry)
709 :tag (custom-unlispify-tag-name
711 :value (nth 0 entry))
712 (setq count (1+ count))
713 (unless (eq (preceding-char) ?\n)
714 (widget-insert "\n"))
715 (widget-insert "\n")))
717 (unless (eq (preceding-char) ?\n)
718 (widget-insert "\n"))
720 (message "Creating customization magic...")
721 (mapcar 'custom-magic-reset custom-options)
722 (message "Creating customization buttons...")
723 (widget-create 'push-button
725 :help-echo "Set all modifications for this session."
726 :action (lambda (widget &optional event)
729 (widget-create 'push-button
732 Make the modifications default for future sessions."
733 :action (lambda (widget &optional event)
736 (widget-create 'push-button
738 :help-echo "Undo all modifications."
739 :action (lambda (widget &optional event)
740 (custom-reset event)))
742 (widget-create 'push-button
744 :help-echo "Bury the buffer."
745 :action (lambda (widget &optional event)
747 ;; Steal button release event.
748 (if (and (fboundp 'button-press-event-p)
749 (fboundp 'next-command-event))
752 (button-press-event-p event)
753 (next-command-event))
755 (when (memq 'down (event-modifiers event))
758 (message "Creating customization setup...")
760 (goto-char (point-min))
761 (message "Creating customization buffer...done"))
763 ;;; Modification of Basic Widgets.
765 ;; We add extra properties to the basic widgets needed here. This is
766 ;; fine, as long as we are careful to stay within out own namespace.
768 ;; We want simple widgets to be displayed by default, but complex
769 ;; widgets to be hidden.
771 (widget-put (get 'item 'widget-type) :custom-show t)
772 (widget-put (get 'editable-field 'widget-type)
773 :custom-show (lambda (widget value)
774 (let ((pp (pp-to-string value)))
775 (cond ((string-match "\n" pp)
780 (widget-put (get 'menu-choice 'widget-type) :custom-show t)
782 ;;; The `custom-manual' Widget.
784 (define-widget 'custom-manual 'info-link
785 "Link to the manual entry for this customization option."
786 :help-echo "Read the manual entry for this option."
789 ;;; The `custom-magic' Widget.
791 (defface custom-invalid-face '((((class color))
792 (:foreground "yellow" :background "red"))
794 (:bold t :italic t :underline t)))
795 "Face used when the customize item is invalid.")
797 (defface custom-rogue-face '((((class color))
798 (:foreground "pink" :background "black"))
801 "Face used when the customize item is not defined for customization.")
803 (defface custom-modified-face '((((class color))
804 (:foreground "white" :background "blue"))
807 "Face used when the customize item has been modified.")
809 (defface custom-set-face '((((class color))
810 (:foreground "blue" :background "white"))
813 "Face used when the customize item has been set.")
815 (defface custom-changed-face '((((class color))
816 (:foreground "white" :background "blue"))
819 "Face used when the customize item has been changed.")
821 (defface custom-saved-face '((t (:underline t)))
822 "Face used when the customize item has been saved.")
824 (defcustom custom-magic-alist '((nil "#" underline "\
825 uninitialized, you should not see this.")
826 (unknown "?" italic "\
827 unknown, you should not see this.")
828 (hidden "-" default "\
829 hidden, press the state button to show.")
830 (invalid "x" custom-invalid-face "\
831 the value displayed for this item is invalid and cannot be set.")
832 (modified "*" custom-modified-face "\
833 you have edited the item, and can now set it.")
834 (set "+" custom-set-face "\
835 you have set this item, but not saved it.")
836 (changed ":" custom-changed-face "\
837 this item has been changed outside customize.")
838 (saved "!" custom-saved-face "\
839 this item has been saved.")
840 (rogue "@" custom-rogue-face "\
841 this item is not prepared for customization.")
843 this item is unchanged from its factory setting."))
844 "Alist of customize option states.
845 Each entry is of the form (STATE MAGIC FACE DESCRIPTION), where
847 STATE is one of the following symbols:
850 For internal use, should never occur.
852 For internal use, should never occur.
854 This item is not being displayed.
856 This item is modified, but has an invalid form.
858 This item is modified, and has a valid form.
860 This item has been set but not saved.
862 The current value of this item has been changed temporarily.
864 This item is marked for saving.
866 This item has no customization information.
868 This item is unchanged from the factory default.
870 MAGIC is a string used to present that state.
872 FACE is a face used to present the state.
874 DESCRIPTION is a string describing the state.
876 The list should be sorted most significant first."
877 :type '(list (checklist :inline t
879 (string :tag "Magic")
881 (string :tag "Description"))
882 (group (const unknown)
883 (string :tag "Magic")
885 (string :tag "Description"))
886 (group (const hidden)
887 (string :tag "Magic")
889 (string :tag "Description"))
890 (group (const invalid)
891 (string :tag "Magic")
893 (string :tag "Description"))
894 (group (const modified)
895 (string :tag "Magic")
897 (string :tag "Description"))
899 (string :tag "Magic")
901 (string :tag "Description"))
902 (group (const changed)
903 (string :tag "Magic")
905 (string :tag "Description"))
907 (string :tag "Magic")
909 (string :tag "Description"))
911 (string :tag "Magic")
913 (string :tag "Description"))
914 (group (const factory)
915 (string :tag "Magic")
917 (string :tag "Description")))
918 (editable-list :inline t
920 (string :tag "Magic")
922 (string :tag "Description"))))
924 :group 'custom-faces)
926 (defcustom custom-magic-show 'long
927 "Show long description of the state of each customization option."
928 :type '(choice (const :tag "no" nil)
933 (defcustom custom-magic-show-button t
934 "Show a magic button indicating the state of each customization option."
938 (define-widget 'custom-magic 'default
939 "Show and manipulate state for a customization option."
941 :action 'widget-choice-item-action
943 :value-create 'custom-magic-value-create
944 :value-delete 'widget-children-value-delete)
946 (defun custom-magic-value-create (widget)
947 ;; Create compact status report for WIDGET.
948 (let* ((parent (widget-get widget :parent))
949 (state (widget-get parent :custom-state))
950 (entry (assq state custom-magic-alist))
951 (magic (nth 1 entry))
954 (lisp (eq (widget-get parent :custom-form) 'lisp))
956 (when custom-magic-show
957 (push (widget-create-child-and-convert widget 'choice-item
959 Change the state of this item."
964 (if (eq custom-magic-show 'long)
966 (insert (symbol-name state)))
970 (when custom-magic-show-button
971 (when custom-magic-show
972 (let ((indent (widget-get parent :indent)))
974 (insert-char ? indent))))
975 (push (widget-create-child-and-convert widget 'choice-item
977 :help-echo "Change the state."
980 (concat "(" magic ")")
981 (concat "[" magic "]")))
984 (widget-put widget :children children)))
986 (defun custom-magic-reset (widget)
987 "Redraw the :custom-magic property of WIDGET."
988 (let ((magic (widget-get widget :custom-magic)))
989 (widget-value-set magic (widget-value magic))))
991 ;;; The `custom-level' Widget.
993 (define-widget 'custom-level 'item
994 "The custom level buttons."
996 :help-echo "Expand or collapse this item."
997 :action 'custom-level-action)
999 (defun custom-level-action (widget &optional event)
1000 "Toggle visibility for parent to WIDGET."
1001 (let* ((parent (widget-get widget :parent))
1002 (state (widget-get parent :custom-state)))
1003 (cond ((memq state '(invalid modified))
1004 (error "There are unset changes"))
1006 (widget-put parent :custom-state 'unknown))
1008 (widget-put parent :custom-state 'hidden)))
1009 (custom-redraw parent)))
1011 ;;; The `custom' Widget.
1013 (define-widget 'custom 'default
1014 "Customize a user option."
1015 :convert-widget 'custom-convert-widget
1016 :format "%l%[%t%]: %v%m%h%a"
1017 :format-handler 'custom-format-handler
1018 :notify 'custom-notify
1020 :custom-state 'hidden
1021 :documentation-property 'widget-subclass-responsibility
1022 :value-create 'widget-subclass-responsibility
1023 :value-delete 'widget-children-value-delete
1024 :value-get 'widget-item-value-get
1025 :validate 'widget-editable-list-validate
1026 :match (lambda (widget value) (symbolp value)))
1028 (defun custom-convert-widget (widget)
1029 ;; Initialize :value and :tag from :args in WIDGET.
1030 (let ((args (widget-get widget :args)))
1032 (widget-put widget :value (widget-apply widget
1033 :value-to-internal (car args)))
1034 (widget-put widget :tag (custom-unlispify-tag-name (car args)))
1035 (widget-put widget :args nil)))
1038 (defun custom-format-handler (widget escape)
1039 ;; We recognize extra escape sequences.
1040 (let* ((buttons (widget-get widget :buttons))
1041 (state (widget-get widget :custom-state))
1042 (level (widget-get widget :custom-level)))
1043 (cond ((eq escape ?l)
1045 (push (widget-create-child-and-convert
1046 widget 'custom-level (make-string level ?*))
1049 (widget-put widget :buttons buttons)))
1051 (when (eq state 'hidden)
1052 (widget-insert " ...")))
1054 (and (eq (preceding-char) ?\n)
1055 (widget-get widget :indent)
1056 (insert-char ? (widget-get widget :indent)))
1057 (let ((magic (widget-create-child-and-convert
1058 widget 'custom-magic nil)))
1059 (widget-put widget :custom-magic magic)
1060 (push magic buttons)
1061 (widget-put widget :buttons buttons)))
1063 (let* ((symbol (widget-get widget :value))
1064 (links (get symbol 'custom-links))
1065 (many (> (length links) 2)))
1067 (and (eq (preceding-char) ?\n)
1068 (widget-get widget :indent)
1069 (insert-char ? (widget-get widget :indent)))
1070 (insert "See also ")
1072 (push (widget-create-child-and-convert widget (car links))
1074 (setq links (cdr links))
1083 (widget-put widget :buttons buttons))))
1085 (widget-default-format-handler widget escape)))))
1087 (defun custom-notify (widget &rest args)
1088 "Keep track of changes."
1089 (unless (memq (widget-get widget :custom-state) '(nil unknown hidden))
1090 (widget-put widget :custom-state 'modified))
1091 (let ((buffer-undo-list t))
1092 (custom-magic-reset widget))
1093 (apply 'widget-default-notify widget args))
1095 (defun custom-redraw (widget)
1096 "Redraw WIDGET with current settings."
1098 (from (marker-position (widget-get widget :from)))
1099 (to (marker-position (widget-get widget :to))))
1101 (widget-value-set widget (widget-value widget))
1102 (custom-redraw-magic widget))
1103 (when (and (>= pos from) (<= pos to))
1106 (defun custom-redraw-magic (widget)
1107 "Redraw WIDGET state with current settings."
1109 (let ((magic (widget-get widget :custom-magic)))
1112 (widget-value-set magic (widget-value magic))
1113 (when (setq widget (widget-get widget :group))
1114 (custom-group-state-update widget))))
1117 (defun custom-show (widget value)
1118 "Non-nil if WIDGET should be shown with VALUE by default."
1119 (let ((show (widget-get widget :custom-show)))
1125 (funcall show widget value)))))
1127 (defvar custom-load-recursion nil
1128 "Hack to avoid recursive dependencies.")
1130 (defun custom-load-symbol (symbol)
1131 "Load all dependencies for SYMBOL."
1132 (unless custom-load-recursion
1133 (let ((custom-load-recursion t)
1134 (loads (get symbol 'custom-loads))
1137 (setq load (car loads)
1139 (cond ((symbolp load)
1143 ((assoc load load-history))
1149 (defun custom-load-widget (widget)
1150 "Load all dependencies for WIDGET."
1151 (custom-load-symbol (widget-value widget)))
1153 ;;; The `custom-variable' Widget.
1155 (defface custom-variable-sample-face '((t (:underline t)))
1156 "Face used for unpushable variable tags."
1157 :group 'custom-faces)
1159 (defface custom-variable-button-face '((t (:underline t :bold t)))
1160 "Face used for pushable variable tags."
1161 :group 'custom-faces)
1163 (define-widget 'custom-variable 'custom
1164 "Customize variable."
1165 :format "%l%v%m%h%a"
1166 :help-echo "Set or reset this variable."
1167 :documentation-property 'variable-documentation
1169 :custom-menu 'custom-variable-menu-create
1171 :value-create 'custom-variable-value-create
1172 :action 'custom-variable-action
1173 :custom-set 'custom-variable-set
1174 :custom-save 'custom-variable-save
1175 :custom-reset-current 'custom-redraw
1176 :custom-reset-saved 'custom-variable-reset-saved
1177 :custom-reset-factory 'custom-variable-reset-factory)
1179 (defun custom-variable-type (symbol)
1180 "Return a widget suitable for editing the value of SYMBOL.
1181 If SYMBOL has a `custom-type' property, use that.
1182 Otherwise, look up symbol in `custom-guess-type-alist'."
1183 (let* ((type (or (get symbol 'custom-type)
1184 (and (not (get symbol 'factory-value))
1185 (custom-guess-type symbol))
1187 (options (get symbol 'custom-options))
1188 (tmp (if (listp type)
1192 (widget-put tmp :options options))
1195 (defun custom-variable-value-create (widget)
1196 "Here is where you edit the variables value."
1197 (custom-load-widget widget)
1198 (let* ((buttons (widget-get widget :buttons))
1199 (children (widget-get widget :children))
1200 (form (widget-get widget :custom-form))
1201 (state (widget-get widget :custom-state))
1202 (symbol (widget-get widget :value))
1203 (tag (widget-get widget :tag))
1204 (type (custom-variable-type symbol))
1205 (conv (widget-convert type))
1206 (value (if (default-boundp symbol)
1207 (default-value symbol)
1208 (widget-get conv :value))))
1209 ;; If the widget is new, the child determine whether it is hidden.
1211 ((custom-show type value)
1212 (setq state 'unknown))
1214 (setq state 'hidden)))
1215 ;; If we don't know the state, see if we need to edit it in lisp form.
1216 (when (eq state 'unknown)
1217 (unless (widget-apply conv :match value)
1218 ;; (widget-apply (widget-convert type) :match value)
1220 ;; Now we can create the child widget.
1221 (cond ((eq state 'hidden)
1222 ;; Indicate hidden value.
1223 (push (widget-create-child-and-convert
1225 :format "%{%t%}: ..."
1226 :sample-face 'custom-variable-sample-face
1231 ;; In lisp mode edit the saved value when possible.
1232 (let* ((value (cond ((get symbol 'saved-value)
1233 (car (get symbol 'saved-value)))
1234 ((get symbol 'factory-value)
1235 (car (get symbol 'factory-value)))
1236 ((default-boundp symbol)
1237 (custom-quote (default-value symbol)))
1239 (custom-quote (widget-get conv :value))))))
1240 (push (widget-create-child-and-convert
1242 :button-face 'custom-variable-button-face
1243 :tag (symbol-name symbol)
1249 (push (widget-create-child-and-convert
1252 :button-face 'custom-variable-button-face
1253 :sample-face 'custom-variable-sample-face
1256 ;; Now update the state.
1257 (unless (eq (preceding-char) ?\n)
1258 (widget-insert "\n"))
1259 (if (eq state 'hidden)
1260 (widget-put widget :custom-state state)
1261 (custom-variable-state-set widget))
1262 (widget-put widget :custom-form form)
1263 (widget-put widget :buttons buttons)
1264 (widget-put widget :children children)))
1266 (defun custom-variable-state-set (widget)
1267 "Set the state of WIDGET."
1268 (let* ((symbol (widget-value widget))
1269 (value (if (default-boundp symbol)
1270 (default-value symbol)
1271 (widget-get widget :value)))
1273 (state (cond ((setq tmp (get symbol 'customized-value))
1274 (if (condition-case nil
1275 (equal value (eval (car tmp)))
1279 ((setq tmp (get symbol 'saved-value))
1280 (if (condition-case nil
1281 (equal value (eval (car tmp)))
1285 ((setq tmp (get symbol 'factory-value))
1286 (if (condition-case nil
1287 (equal value (eval (car tmp)))
1292 (widget-put widget :custom-state state)))
1294 (defvar custom-variable-menu
1295 '(("Edit" . custom-variable-edit)
1296 ("Edit Lisp" . custom-variable-edit-lisp)
1297 ("Set" . custom-variable-set)
1298 ("Save" . custom-variable-save)
1299 ("Reset to Current" . custom-redraw)
1300 ("Reset to Saved" . custom-variable-reset-saved)
1301 ("Reset to Factory Settings" . custom-variable-reset-factory))
1302 "Alist of actions for the `custom-variable' widget.
1303 The key is a string containing the name of the action, the value is a
1304 lisp function taking the widget as an element which will be called
1305 when the action is chosen.")
1307 (defun custom-variable-action (widget &optional event)
1308 "Show the menu for `custom-variable' WIDGET.
1309 Optional EVENT is the location for the menu."
1310 (if (eq (widget-get widget :custom-state) 'hidden)
1312 (widget-put widget :custom-state 'unknown)
1313 (custom-redraw widget))
1314 (let* ((completion-ignore-case t)
1315 (answer (widget-choose (custom-unlispify-tag-name
1316 (widget-get widget :value))
1317 custom-variable-menu
1320 (funcall answer widget)))))
1322 (defun custom-variable-edit (widget)
1323 "Edit value of WIDGET."
1324 (widget-put widget :custom-state 'unknown)
1325 (widget-put widget :custom-form 'edit)
1326 (custom-redraw widget))
1328 (defun custom-variable-edit-lisp (widget)
1329 "Edit the lisp representation of the value of WIDGET."
1330 (widget-put widget :custom-state 'unknown)
1331 (widget-put widget :custom-form 'lisp)
1332 (custom-redraw widget))
1334 (defun custom-variable-set (widget)
1335 "Set the current value for the variable being edited by WIDGET."
1336 (let ((form (widget-get widget :custom-form))
1337 (state (widget-get widget :custom-state))
1338 (child (car (widget-get widget :children)))
1339 (symbol (widget-value widget))
1341 (cond ((eq state 'hidden)
1342 (error "Cannot set hidden variable."))
1343 ((setq val (widget-apply child :validate))
1344 (goto-char (widget-get val :from))
1345 (error "%s" (widget-get val :error)))
1347 (set-default symbol (eval (setq val (widget-value child))))
1348 (put symbol 'customized-value (list val)))
1350 (set-default symbol (setq val (widget-value child)))
1351 (put symbol 'customized-value (list (custom-quote val)))))
1352 (custom-variable-state-set widget)
1353 (custom-redraw-magic widget)))
1355 (defun custom-variable-save (widget)
1356 "Set the default value for the variable being edited by WIDGET."
1357 (let ((form (widget-get widget :custom-form))
1358 (state (widget-get widget :custom-state))
1359 (child (car (widget-get widget :children)))
1360 (symbol (widget-value widget))
1362 (cond ((eq state 'hidden)
1363 (error "Cannot set hidden variable."))
1364 ((setq val (widget-apply child :validate))
1365 (goto-char (widget-get val :from))
1366 (error "%s" (widget-get val :error)))
1368 (put symbol 'saved-value (list (widget-value child)))
1369 (set-default symbol (eval (widget-value child))))
1372 'saved-value (list (custom-quote (widget-value
1374 (set-default symbol (widget-value child))))
1375 (put symbol 'customized-value nil)
1377 (custom-variable-state-set widget)
1378 (custom-redraw-magic widget)))
1380 (defun custom-variable-reset-saved (widget)
1381 "Restore the saved value for the variable being edited by WIDGET."
1382 (let ((symbol (widget-value widget)))
1383 (if (get symbol 'saved-value)
1385 (set-default symbol (eval (car (get symbol 'saved-value))))
1387 (error "No saved value for %s" symbol))
1388 (put symbol 'customized-value nil)
1389 (widget-put widget :custom-state 'unknown)
1390 (custom-redraw widget)))
1392 (defun custom-variable-reset-factory (widget)
1393 "Restore the factory setting for the variable being edited by WIDGET."
1394 (let ((symbol (widget-value widget)))
1395 (if (get symbol 'factory-value)
1396 (set-default symbol (eval (car (get symbol 'factory-value))))
1397 (error "No factory default for %S" symbol))
1398 (put symbol 'customized-value nil)
1399 (when (get symbol 'saved-value)
1400 (put symbol 'saved-value nil)
1402 (widget-put widget :custom-state 'unknown)
1403 (custom-redraw widget)))
1405 ;;; The `custom-face-edit' Widget.
1407 (define-widget 'custom-face-edit 'checklist
1408 "Edit face attributes."
1412 :button-args '(:help-echo "Control whether this attribute have any effect.")
1413 :args (mapcar (lambda (att)
1416 :sibling-args (widget-get (nth 1 att) :sibling-args)
1417 (list 'const :format "" :value (nth 0 att))
1419 custom-face-attributes))
1421 ;;; The `custom-display' Widget.
1423 (define-widget 'custom-display 'menu-choice
1424 "Select a display type."
1427 :help-echo "Specify frames where the face attributes should be used."
1428 :args '((const :tag "all" t)
1432 :args ((group :sibling-args (:help-echo "\
1433 Only match the specified window systems.")
1434 (const :format "Type: "
1436 (checklist :inline t
1439 :sibling-args (:help-echo "\
1440 The X11 Window System.")
1442 (const :format "PM "
1443 :sibling-args (:help-echo "\
1444 OS/2 Presentation Manager.")
1446 (const :format "Win32 "
1447 :sibling-args (:help-echo "\
1450 (const :format "DOS "
1451 :sibling-args (:help-echo "\
1454 (const :format "TTY%n"
1455 :sibling-args (:help-echo "\
1456 Plain text terminals.")
1458 (group :sibling-args (:help-echo "\
1459 Only match the frames with the specified color support.")
1460 (const :format "Class: "
1462 (checklist :inline t
1464 (const :format "Color "
1465 :sibling-args (:help-echo "\
1466 Match color frames.")
1468 (const :format "Grayscale "
1469 :sibling-args (:help-echo "\
1470 Match grayscale frames.")
1472 (const :format "Monochrome%n"
1473 :sibling-args (:help-echo "\
1474 Match frames with no color support.")
1476 (group :sibling-args (:help-echo "\
1477 Only match frames with the specified intensity.")
1479 Background brightness: "
1481 (checklist :inline t
1483 (const :format "Light "
1484 :sibling-args (:help-echo "\
1485 Match frames with light backgrounds.")
1487 (const :format "Dark\n"
1488 :sibling-args (:help-echo "\
1489 Match frames with dark backgrounds.")
1492 ;;; The `custom-face' Widget.
1494 (defface custom-face-tag-face '((t (:underline t)))
1495 "Face used for face tags."
1496 :group 'custom-faces)
1498 (define-widget 'custom-face 'custom
1500 :format "%l%{%t%}: %s%m%h%a%v"
1501 :format-handler 'custom-face-format-handler
1502 :sample-face 'custom-face-tag-face
1503 :help-echo "Set or reset this face."
1504 :documentation-property '(lambda (face)
1505 (face-doc-string face))
1506 :value-create 'custom-face-value-create
1507 :action 'custom-face-action
1508 :custom-form 'selected
1509 :custom-set 'custom-face-set
1510 :custom-save 'custom-face-save
1511 :custom-reset-current 'custom-redraw
1512 :custom-reset-saved 'custom-face-reset-saved
1513 :custom-reset-factory 'custom-face-reset-factory
1514 :custom-menu 'custom-face-menu-create)
1516 (defun custom-face-format-handler (widget escape)
1517 ;; We recognize extra escape sequences.
1519 (symbol (widget-get widget :value)))
1520 (cond ((eq escape ?s)
1521 (and (string-match "XEmacs" emacs-version)
1522 ;; XEmacs cannot display initialized faces.
1523 (not (custom-facep symbol))
1524 (copy-face 'custom-face-empty symbol))
1525 (setq child (widget-create-child-and-convert
1527 :format "(%{%t%})\n"
1531 (custom-format-handler widget escape)))
1534 :buttons (cons child (widget-get widget :buttons))))))
1536 (define-widget 'custom-face-all 'editable-list
1537 "An editable list of display specifications and attributes."
1538 :entry-format "%i %d %v"
1539 :insert-button-args '(:help-echo "Insert new display specification here.")
1540 :append-button-args '(:help-echo "Append new display specification here.")
1541 :delete-button-args '(:help-echo "Delete this display specification.")
1542 :args '((group :format "%v" custom-display custom-face-edit)))
1544 (defconst custom-face-all (widget-convert 'custom-face-all)
1545 "Converted version of the `custom-face-all' widget.")
1547 (define-widget 'custom-display-unselected 'item
1548 "A display specification that doesn't match the selected display."
1549 :match 'custom-display-unselected-match)
1551 (defun custom-display-unselected-match (widget value)
1552 "Non-nil if VALUE is an unselected display specification."
1554 (eq (length value) 2)
1555 (not (custom-display-match-frame value (selected-frame)))))
1557 (define-widget 'custom-face-selected 'group
1558 "Edit the attributes of the selected display in a face specification."
1559 :args '((repeat :format ""
1561 (group custom-display-unselected sexp))
1562 (group (sexp :format "") custom-face-edit)
1567 (defconst custom-face-selected (widget-convert 'custom-face-selected)
1568 "Converted version of the `custom-face-selected' widget.")
1570 (defun custom-face-value-create (widget)
1571 ;; Create a list of the display specifications.
1572 (unless (eq (preceding-char) ?\n)
1574 (when (not (eq (widget-get widget :custom-state) 'hidden))
1575 (message "Creating face editor...")
1576 (custom-load-widget widget)
1577 (let* ((symbol (widget-value widget))
1578 (spec (or (get symbol 'saved-face)
1579 (get symbol 'factory-face)
1580 ;; Attempt to construct it.
1581 (list (list t (custom-face-attributes-get
1582 symbol (selected-frame))))))
1583 (form (widget-get widget :custom-form))
1584 (indent (widget-get widget :indent))
1585 (edit (widget-create-child-and-convert
1587 (cond ((and (eq form 'selected)
1588 (widget-apply custom-face-selected :match spec))
1589 (when indent (insert-char ?\ indent))
1590 'custom-face-selected)
1591 ((and (not (eq form 'lisp))
1592 (widget-apply custom-face-all :match spec))
1595 (when indent (insert-char ?\ indent))
1598 (custom-face-state-set widget)
1599 (widget-put widget :children (list edit)))
1600 (message "Creating face editor...done")))
1602 (defvar custom-face-menu
1603 '(("Edit Selected" . custom-face-edit-selected)
1604 ("Edit All" . custom-face-edit-all)
1605 ("Edit Lisp" . custom-face-edit-lisp)
1606 ("Set" . custom-face-set)
1607 ("Save" . custom-face-save)
1608 ("Reset to Saved" . custom-face-reset-saved)
1609 ("Reset to Factory Setting" . custom-face-reset-factory))
1610 "Alist of actions for the `custom-face' widget.
1611 The key is a string containing the name of the action, the value is a
1612 lisp function taking the widget as an element which will be called
1613 when the action is chosen.")
1615 (defun custom-face-edit-selected (widget)
1616 "Edit selected attributes of the value of WIDGET."
1617 (widget-put widget :custom-state 'unknown)
1618 (widget-put widget :custom-form 'selected)
1619 (custom-redraw widget))
1621 (defun custom-face-edit-all (widget)
1622 "Edit all attributes of the value of WIDGET."
1623 (widget-put widget :custom-state 'unknown)
1624 (widget-put widget :custom-form 'all)
1625 (custom-redraw widget))
1627 (defun custom-face-edit-lisp (widget)
1628 "Edit the lisp representation of the value of WIDGET."
1629 (widget-put widget :custom-state 'unknown)
1630 (widget-put widget :custom-form 'lisp)
1631 (custom-redraw widget))
1633 (defun custom-face-state-set (widget)
1634 "Set the state of WIDGET."
1635 (let ((symbol (widget-value widget)))
1636 (widget-put widget :custom-state (cond ((get symbol 'customized-face)
1638 ((get symbol 'saved-face)
1640 ((get symbol 'factory-face)
1645 (defun custom-face-action (widget &optional event)
1646 "Show the menu for `custom-face' WIDGET.
1647 Optional EVENT is the location for the menu."
1648 (if (eq (widget-get widget :custom-state) 'hidden)
1650 (widget-put widget :custom-state 'unknown)
1651 (custom-redraw widget))
1652 (let* ((completion-ignore-case t)
1653 (symbol (widget-get widget :value))
1654 (answer (widget-choose (custom-unlispify-tag-name symbol)
1655 custom-face-menu event)))
1657 (funcall answer widget)))))
1659 (defun custom-face-set (widget)
1660 "Make the face attributes in WIDGET take effect."
1661 (let* ((symbol (widget-value widget))
1662 (child (car (widget-get widget :children)))
1663 (value (widget-value child)))
1664 (put symbol 'customized-face value)
1665 (custom-face-display-set symbol value)
1666 (custom-face-state-set widget)
1667 (custom-redraw-magic widget)))
1669 (defun custom-face-save (widget)
1670 "Make the face attributes in WIDGET default."
1671 (let* ((symbol (widget-value widget))
1672 (child (car (widget-get widget :children)))
1673 (value (widget-value child)))
1674 (custom-face-display-set symbol value)
1675 (put symbol 'saved-face value)
1676 (put symbol 'customized-face nil)
1677 (custom-face-state-set widget)
1678 (custom-redraw-magic widget)))
1680 (defun custom-face-reset-saved (widget)
1681 "Restore WIDGET to the face's default attributes."
1682 (let* ((symbol (widget-value widget))
1683 (child (car (widget-get widget :children)))
1684 (value (get symbol 'saved-face)))
1686 (error "No saved value for this face"))
1687 (put symbol 'customized-face nil)
1688 (custom-face-display-set symbol value)
1689 (widget-value-set child value)
1690 (custom-face-state-set widget)
1691 (custom-redraw-magic widget)))
1693 (defun custom-face-reset-factory (widget)
1694 "Restore WIDGET to the face's factory settings."
1695 (let* ((symbol (widget-value widget))
1696 (child (car (widget-get widget :children)))
1697 (value (get symbol 'factory-face)))
1699 (error "No factory default for this face"))
1700 (put symbol 'customized-face nil)
1701 (when (get symbol 'saved-face)
1702 (put symbol 'saved-face nil)
1704 (custom-face-display-set symbol value)
1705 (widget-value-set child value)
1706 (custom-face-state-set widget)
1707 (custom-redraw-magic widget)))
1709 ;;; The `face' Widget.
1711 (define-widget 'face 'default
1712 "Select and customize a face."
1713 :convert-widget 'widget-item-convert-widget
1714 :format "%[%t%]: %v"
1717 :value-create 'widget-face-value-create
1718 :value-delete 'widget-face-value-delete
1719 :value-get 'widget-item-value-get
1720 :validate 'widget-editable-list-validate
1721 :action 'widget-face-action
1722 :match '(lambda (widget value) (symbolp value)))
1724 (defun widget-face-value-create (widget)
1725 ;; Create a `custom-face' child.
1726 (let* ((symbol (widget-value widget))
1727 (child (widget-create-child-and-convert
1729 :format "%t %s%m%h%v"
1732 (custom-magic-reset child)
1733 (setq custom-options (cons child custom-options))
1734 (widget-put widget :children (list child))))
1736 (defun widget-face-value-delete (widget)
1737 ;; Remove the child from the options.
1738 (let ((child (car (widget-get widget :children))))
1739 (setq custom-options (delq child custom-options))
1740 (widget-children-value-delete widget)))
1742 (defvar face-history nil
1743 "History of entered face names.")
1745 (defun widget-face-action (widget &optional event)
1746 "Prompt for a face."
1747 (let ((answer (completing-read "Face: "
1748 (mapcar (lambda (face)
1749 (list (symbol-name face)))
1753 (unless (zerop (length answer))
1754 (widget-value-set widget (intern answer))
1755 (widget-apply widget :notify widget event)
1758 ;;; The `hook' Widget.
1760 (define-widget 'hook 'list
1762 :convert-widget 'custom-hook-convert-widget
1765 (defun custom-hook-convert-widget (widget)
1766 ;; Handle `:custom-options'.
1767 (let* ((options (widget-get widget :options))
1768 (other `(editable-list :inline t
1769 :entry-format "%i %d%v"
1770 (function :format " %v")))
1772 (list `(checklist :inline t
1773 ,@(mapcar (lambda (entry)
1774 `(function-item ,entry))
1778 (widget-put widget :args args)
1781 ;;; The `custom-group' Widget.
1783 (defcustom custom-group-tag-faces '(custom-group-tag-face-1)
1784 ;; In XEmacs, this ought to play games with font size.
1785 "Face used for group tags.
1786 The first member is used for level 1 groups, the second for level 2,
1787 and so forth. The remaining group tags are shown with
1788 `custom-group-tag-face'."
1789 :type '(repeat face)
1790 :group 'custom-faces)
1792 (defface custom-group-tag-face-1 '((((class color)
1794 (:foreground "pink" :underline t))
1797 (:foreground "red" :underline t))
1799 "Face used for group tags.")
1801 (defface custom-group-tag-face '((((class color)
1803 (:foreground "light blue" :underline t))
1806 (:foreground "blue" :underline t))
1808 "Face used for low level group tags."
1809 :group 'custom-faces)
1811 (define-widget 'custom-group 'custom
1813 :format "%l%{%t%}:%L\n%m%h%a%v"
1814 :sample-face-get 'custom-group-sample-face-get
1815 :documentation-property 'group-documentation
1816 :help-echo "Set or reset all members of this group."
1817 :value-create 'custom-group-value-create
1818 :action 'custom-group-action
1819 :custom-set 'custom-group-set
1820 :custom-save 'custom-group-save
1821 :custom-reset-current 'custom-group-reset-current
1822 :custom-reset-saved 'custom-group-reset-saved
1823 :custom-reset-factory 'custom-group-reset-factory
1824 :custom-menu 'custom-group-menu-create)
1826 (defun custom-group-sample-face-get (widget)
1827 ;; Use :sample-face.
1828 (or (nth (1- (widget-get widget :custom-level)) custom-group-tag-faces)
1829 'custom-group-tag-face))
1831 (defun custom-group-value-create (widget)
1832 (let ((state (widget-get widget :custom-state)))
1833 (unless (eq state 'hidden)
1834 (message "Creating group...")
1835 (custom-load-widget widget)
1836 (let* ((level (widget-get widget :custom-level))
1837 (symbol (widget-value widget))
1838 (members (get symbol 'custom-group))
1839 (prefixes (widget-get widget :custom-prefixes))
1840 (custom-prefix-list (custom-prefix-add symbol prefixes))
1841 (length (length members))
1843 (children (mapcar (lambda (entry)
1844 (widget-insert "\n")
1845 (message "Creating group members... %2d%%"
1846 (/ (* 100.0 count) length))
1847 (setq count (1+ count))
1849 (widget-create-child-and-convert
1850 widget (nth 1 entry)
1852 :tag (custom-unlispify-tag-name
1854 :custom-prefixes custom-prefix-list
1855 :custom-level (1+ level)
1856 :value (nth 0 entry))
1857 (unless (eq (preceding-char) ?\n)
1858 (widget-insert "\n"))))
1860 (message "Creating group magic...")
1861 (mapcar 'custom-magic-reset children)
1862 (message "Creating group state...")
1863 (widget-put widget :children children)
1864 (custom-group-state-update widget)
1865 (message "Creating group... done")))))
1867 (defvar custom-group-menu
1868 '(("Set" . custom-group-set)
1869 ("Save" . custom-group-save)
1870 ("Reset to Current" . custom-group-reset-current)
1871 ("Reset to Saved" . custom-group-reset-saved)
1872 ("Reset to Factory" . custom-group-reset-factory))
1873 "Alist of actions for the `custom-group' widget.
1874 The key is a string containing the name of the action, the value is a
1875 lisp function taking the widget as an element which will be called
1876 when the action is chosen.")
1878 (defun custom-group-action (widget &optional event)
1879 "Show the menu for `custom-group' WIDGET.
1880 Optional EVENT is the location for the menu."
1881 (if (eq (widget-get widget :custom-state) 'hidden)
1883 (widget-put widget :custom-state 'unknown)
1884 (custom-redraw widget))
1885 (let* ((completion-ignore-case t)
1886 (answer (widget-choose (custom-unlispify-tag-name
1887 (widget-get widget :value))
1891 (funcall answer widget)))))
1893 (defun custom-group-set (widget)
1894 "Set changes in all modified group members."
1895 (let ((children (widget-get widget :children)))
1896 (mapcar (lambda (child)
1897 (when (eq (widget-get child :custom-state) 'modified)
1898 (widget-apply child :custom-set)))
1901 (defun custom-group-save (widget)
1902 "Save all modified group members."
1903 (let ((children (widget-get widget :children)))
1904 (mapcar (lambda (child)
1905 (when (memq (widget-get child :custom-state) '(modified set))
1906 (widget-apply child :custom-save)))
1909 (defun custom-group-reset-current (widget)
1910 "Reset all modified group members."
1911 (let ((children (widget-get widget :children)))
1912 (mapcar (lambda (child)
1913 (when (eq (widget-get child :custom-state) 'modified)
1914 (widget-apply child :custom-reset-current)))
1917 (defun custom-group-reset-saved (widget)
1918 "Reset all modified or set group members."
1919 (let ((children (widget-get widget :children)))
1920 (mapcar (lambda (child)
1921 (when (memq (widget-get child :custom-state) '(modified set))
1922 (widget-apply child :custom-reset-saved)))
1925 (defun custom-group-reset-factory (widget)
1926 "Reset all modified, set, or saved group members."
1927 (let ((children (widget-get widget :children)))
1928 (mapcar (lambda (child)
1929 (when (memq (widget-get child :custom-state)
1930 '(modified set saved))
1931 (widget-apply child :custom-reset-factory)))
1934 (defun custom-group-state-update (widget)
1936 (unless (eq (widget-get widget :custom-state) 'hidden)
1937 (let* ((children (widget-get widget :children))
1938 (states (mapcar (lambda (child)
1939 (widget-get child :custom-state))
1941 (magics custom-magic-alist)
1944 (let ((magic (car (car magics))))
1945 (if (and (not (eq magic 'hidden))
1946 (memq magic states))
1949 (setq magics (cdr magics)))))
1950 (widget-put widget :custom-state found)))
1951 (custom-magic-reset widget))
1953 ;;; The `custom-save-all' Function.
1955 (defcustom custom-file "~/.emacs"
1956 "File used for storing customization information.
1957 If you change this from the default \"~/.emacs\" you need to
1958 explicitly load that file for the settings to take effect."
1962 (defun custom-save-delete (symbol)
1963 "Delete the call to SYMBOL form `custom-file'.
1964 Leave point at the location of the call, or after the last expression."
1965 (set-buffer (find-file-noselect custom-file))
1966 (goto-char (point-min))
1969 (let ((sexp (condition-case nil
1970 (read (current-buffer))
1971 (end-of-file (throw 'found nil)))))
1972 (when (and (listp sexp)
1973 (eq (car sexp) symbol))
1974 (delete-region (save-excursion
1978 (throw 'found nil))))))
1980 (defun custom-save-variables ()
1981 "Save all customized variables in `custom-file'."
1983 (custom-save-delete 'custom-set-variables)
1984 (let ((standard-output (current-buffer)))
1987 (princ "(custom-set-variables")
1988 (mapatoms (lambda (symbol)
1989 (let ((value (get symbol 'saved-value)))
1995 (if (or (get symbol 'factory-value)
1996 (and (not (boundp symbol))
1997 (not (get symbol 'force-value))))
2001 (unless (looking-at "\n")
2004 (defun custom-save-faces ()
2005 "Save all customized faces in `custom-file'."
2007 (custom-save-delete 'custom-set-faces)
2008 (let ((standard-output (current-buffer)))
2011 (princ "(custom-set-faces")
2012 (let ((value (get 'default 'saved-face)))
2013 ;; The default face must be first, since it affects the others.
2015 (princ "\n '(default ")
2017 (if (or (get 'default 'factory-face)
2018 (and (not (custom-facep 'default))
2019 (not (get 'default 'force-face))))
2022 (mapatoms (lambda (symbol)
2023 (let ((value (get symbol 'saved-face)))
2024 (when (and (not (eq symbol 'default))
2025 ;; Don't print default face here.
2031 (if (or (get symbol 'factory-face)
2032 (and (not (custom-facep symbol))
2033 (not (get symbol 'force-face))))
2037 (unless (looking-at "\n")
2041 (defun custom-save-all ()
2042 "Save all customizations in `custom-file'."
2043 (custom-save-variables)
2046 (set-buffer (find-file-noselect custom-file))
2049 ;;; The Customize Menu.
2053 (unless (string-match "XEmacs" emacs-version)
2054 (defconst custom-help-menu '("Customize"
2055 ["Update menu..." custom-menu-update t]
2056 ["Group..." customize t]
2057 ["Variable..." customize-variable t]
2058 ["Face..." customize-face t]
2059 ["Saved..." customize-customized t]
2060 ["Apropos..." customize-apropos t])
2061 ;; This menu should be identical to the one defined in `menu-bar.el'.
2064 (defun custom-menu-reset ()
2065 "Reset customize menu."
2066 (remove-hook 'custom-define-hook 'custom-menu-reset)
2067 (define-key global-map [menu-bar help-menu customize-menu]
2068 (cons (car custom-help-menu)
2069 (easy-menu-create-keymaps (car custom-help-menu)
2070 (cdr custom-help-menu)))))
2072 (defun custom-menu-update (event)
2073 "Update customize menu."
2075 (add-hook 'custom-define-hook 'custom-menu-reset)
2076 (let* ((emacs (widget-apply '(custom-group) :custom-menu 'emacs))
2077 (menu `(,(car custom-help-menu)
2079 ,@(cdr (cdr custom-help-menu)))))
2080 (let ((map (easy-menu-create-keymaps (car menu) (cdr menu))))
2081 (define-key global-map [menu-bar help-menu customize-menu]
2082 (cons (car menu) map)))))
2084 (defcustom custom-menu-nesting 2
2085 "Maximum nesting in custom menus."
2089 (defun custom-face-menu-create (widget symbol)
2090 "Ignoring WIDGET, create a menu entry for customization face SYMBOL."
2091 (vector (custom-unlispify-menu-entry symbol)
2092 `(custom-buffer-create '((,symbol custom-face)))
2095 (defun custom-variable-menu-create (widget symbol)
2096 "Ignoring WIDGET, create a menu entry for customization variable SYMBOL."
2097 (let ((type (get symbol 'custom-type)))
2098 (unless (listp type)
2099 (setq type (list type)))
2100 (if (and type (widget-get type :custom-menu))
2101 (widget-apply type :custom-menu symbol)
2102 (vector (custom-unlispify-menu-entry symbol)
2103 `(custom-buffer-create '((,symbol custom-variable)))
2106 ;; Add checkboxes to boolean variable entries.
2107 (widget-put (get 'boolean 'widget-type)
2108 :custom-menu (lambda (widget symbol)
2109 (vector (custom-unlispify-menu-entry symbol)
2110 `(custom-buffer-create
2111 '((,symbol custom-variable)))
2113 ':selected symbol)))
2115 (if (string-match "XEmacs" emacs-version)
2116 ;; XEmacs can create menus dynamically.
2117 (defun custom-group-menu-create (widget symbol)
2118 "Ignoring WIDGET, create a menu entry for customization group SYMBOL."
2119 `( ,(custom-unlispify-menu-entry symbol t)
2120 :filter (lambda (&rest junk)
2121 (cdr (custom-menu-create ',symbol)))))
2123 (defun custom-group-menu-create (widget symbol)
2124 "Ignoring WIDGET, create a menu entry for customization group SYMBOL."
2125 ;; Limit the nesting.
2126 (let ((custom-menu-nesting (1- custom-menu-nesting)))
2127 (custom-menu-create symbol))))
2130 (defun custom-menu-create (symbol)
2131 "Create menu for customization group SYMBOL.
2132 The menu is in a format applicable to `easy-menu-define'."
2133 (let* ((item (vector (custom-unlispify-menu-entry symbol)
2134 `(custom-buffer-create '((,symbol custom-group)))
2136 (if (and (or (not (boundp 'custom-menu-nesting))
2137 (>= custom-menu-nesting 0))
2138 (< (length (get symbol 'custom-group)) widget-menu-max-size))
2139 (let ((custom-prefix-list (custom-prefix-add symbol
2140 custom-prefix-list)))
2141 (custom-load-symbol symbol)
2142 `(,(custom-unlispify-menu-entry symbol t)
2145 ,@(mapcar (lambda (entry)
2146 (widget-apply (if (listp (nth 1 entry))
2148 (list (nth 1 entry)))
2149 :custom-menu (nth 0 entry)))
2150 (get symbol 'custom-group))))
2154 (defun customize-menu-create (symbol &optional name)
2155 "Return a customize menu for customization group SYMBOL.
2156 If optional NAME is given, use that as the name of the menu.
2157 Otherwise the menu will be named `Customize'.
2158 The format is suitable for use with `easy-menu-define'."
2160 (setq name "Customize"))
2161 (if (string-match "XEmacs" emacs-version)
2162 ;; We can delay it under XEmacs.
2164 :filter (lambda (&rest junk)
2165 (cdr (custom-menu-create ',symbol))))
2166 ;; But we must create it now under Emacs.
2167 (cons name (cdr (custom-menu-create symbol)))))
2169 ;;; The Custom Mode.
2171 (defvar custom-mode-map nil
2172 "Keymap for `custom-mode'.")
2174 (unless custom-mode-map
2175 (setq custom-mode-map (make-sparse-keymap))
2176 (set-keymap-parent custom-mode-map widget-keymap)
2177 (define-key custom-mode-map "q" 'bury-buffer))
2179 (easy-menu-define custom-mode-customize-menu
2181 "Menu used in customization buffers."
2182 (customize-menu-create 'customize))
2184 (easy-menu-define custom-mode-menu
2186 "Menu used in customization buffers."
2188 ["Set" custom-set t]
2189 ["Save" custom-save t]
2190 ["Reset to Current" custom-reset-current t]
2191 ["Reset to Saved" custom-reset-saved t]
2192 ["Reset to Factory Settings" custom-reset-factory t]
2193 ["Info" (Info-goto-node "(custom)The Customization Buffer") t]))
2195 (defcustom custom-mode-hook nil
2196 "Hook called when entering custom-mode."
2200 (defun custom-mode ()
2201 "Major mode for editing customization buffers.
2203 The following commands are available:
2205 Move to next button or editable field. \\[widget-forward]
2206 Move to previous button or editable field. \\[widget-backward]
2207 Activate button under the mouse pointer. \\[widget-button-click]
2208 Activate button under point. \\[widget-button-press]
2209 Set all modifications. \\[custom-set]
2210 Make all modifications default. \\[custom-save]
2211 Reset all modified options. \\[custom-reset-current]
2212 Reset all modified or set options. \\[custom-reset-saved]
2213 Reset all options. \\[custom-reset-factory]
2215 Entry to this mode calls the value of `custom-mode-hook'
2216 if that value is non-nil."
2217 (kill-all-local-variables)
2218 (setq major-mode 'custom-mode
2220 (use-local-map custom-mode-map)
2221 (easy-menu-add custom-mode-customize-menu)
2222 (easy-menu-add custom-mode-menu)
2223 (make-local-variable 'custom-options)
2224 (run-hooks 'custom-mode-hook))
2230 ;; cus-edit.el ends here