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.
29 ;; This file implements the code to create and edit customize buffers.
38 (eval-when-compile (require 'cl))
44 (defun custom-face-display-set (face spec &optional frame)
45 (face-spec-set face spec frame))
47 (defun custom-display-match-frame (display frame)
48 (face-spec-set-match-display display frame))
50 (define-widget-keywords :custom-prefixes :custom-menu :custom-show
51 :custom-magic :custom-state :custom-level :custom-form
52 :custom-set :custom-save :custom-reset-current :custom-reset-saved
53 :custom-reset-factory)
55 (put 'custom-define-hook 'custom-type 'hook)
56 (put 'custom-define-hook 'factory-value '(nil))
57 (custom-add-to-group 'customize 'custom-define-hook 'custom-variable)
59 ;;; Customization Groups.
62 "Customization of the One True Editor."
63 :link '(custom-manual "(emacs)Top"))
65 ;; Most of these groups are stolen from `finder.el',
67 "Basic text editing facilities."
71 "Abbreviation handling, typing shortcuts, macros."
75 (defgroup matching nil
76 "Various sorts of searching and matching."
79 (defgroup emulations nil
80 "Emulations of other editors."
87 (defgroup outlines nil
88 "Support for hierarchical outlining."
91 (defgroup external nil
92 "Interfacing to external utilities."
96 "Code related to the `bib' bibliography processor."
100 (defgroup processes nil
101 "Process, subshell, compilation, and job control support."
105 (defgroup programming nil
106 "Support for programming in other languages."
109 (defgroup languages nil
110 "Specialized modes for editing programming languages."
114 "Lisp support, including Emacs Lisp."
119 "Support for the C language and related languages."
127 "Support for object-oriented programming."
130 (defgroup applications nil
131 "Applications written in Emacs."
134 (defgroup calendar nil
135 "Calendar and time management support."
136 :group 'applications)
139 "Modes for electronic-mail handling."
140 :group 'applications)
143 "Support for netnews reading and posting."
144 :group 'applications)
147 "Games, jokes and amusements."
148 :group 'applications)
150 (defgroup development nil
151 "Support for further development of Emacs."
155 "Support for Emacs documentation."
158 (defgroup extensions nil
159 "Emacs Lisp language extensions."
162 (defgroup internal nil
163 "Code for Emacs internals, build process, defaults."
167 "Maintenance aids for the Emacs development group."
171 (defgroup environment nil
172 "Fitting Emacs with its environment."
176 "Communications, networking, remote access to files."
180 (defgroup hardware nil
181 "Support for interfacing with exotic hardware."
184 (defgroup terminals nil
185 "Support for terminal types."
189 "Front-ends/assistants for, or emulators of, UNIX features."
193 "Support code for vms."
197 "Internationalization and alternate character-set support."
202 "Support for Emacs frames and window systems."
206 "Support editing files of data."
214 "Code related to the TeX formatter."
218 "Support for multiple fonts."
221 (defgroup hypermedia nil
222 "Support for links between text or other media types."
226 "Support for on-line help systems."
230 "Code local to your site."
233 (defgroup customize '((widgets custom-group))
234 "Customization of the Customization support."
235 :link '(custom-manual "(custom)Top")
236 :link '(url-link :tag "Development Page"
237 "http://www.dina.kvl.dk/~abraham/custom/")
241 (defgroup custom-faces nil
242 "Faces used by customize."
246 (defgroup abbrev-mode nil
247 "Word abbreviations mode."
251 "Storage allocation and gc for GNU Emacs Lisp interpreter."
252 :tag "Storage Allocation"
256 "Undoing changes in buffers."
259 (defgroup modeline nil
260 "Content of the modeline."
264 "Indenting and filling text."
267 (defgroup editing-basics nil
268 "Most basic editing facilities."
271 (defgroup display nil
272 "How characters are displayed in buffers."
275 (defgroup execute nil
276 "Executing external commands."
279 (defgroup installation nil
280 "The Emacs installation."
288 "Internal Emacs limits."
292 "Debugging Emacs itself."
295 (defgroup minibuffer nil
296 "Controling the behaviour of the minibuffer."
299 (defgroup keyboard nil
300 "Input from the keyboard."
304 "Input from the mouse."
308 "Input from the menus."
311 (defgroup auto-save nil
312 "Preventing accidential loss of data."
315 (defgroup processes-basics nil
316 "Basic stuff dealing with processes."
319 (defgroup windows nil
320 "Windows within a frame."
325 (defun custom-quote (sexp)
326 "Quote SEXP iff it is not self quoting."
327 (if (or (memq sexp '(t nil))
329 (eq (aref (symbol-name sexp) 0) ?:))
331 (memq (car sexp) '(lambda)))
334 (and (fboundp 'characterp)
339 (defun custom-split-regexp-maybe (regexp)
340 "If REGEXP is a string, split it to a list at `\\|'.
341 You can get the original back with from the result with:
342 (mapconcat 'identity result \"\\|\")
344 IF REGEXP is not a string, return it unchanged."
348 (while (string-match "\\\\|" regexp start)
349 (setq all (cons (substring regexp start (match-beginning 0)) all)
350 start (match-end 0)))
351 (nreverse (cons (substring regexp start) all)))
354 (defun custom-variable-prompt ()
355 ;; Code stolen from `help.el'.
356 "Prompt for a variable, defaulting to the variable at point.
357 Return a list suitable for use in `interactive'."
358 (let ((v (variable-at-point))
359 (enable-recursive-minibuffers t)
361 (setq val (completing-read
363 (format "Customize variable (default %s): " v)
364 "Customize variable: ")
365 obarray (lambda (symbol)
367 (or (get symbol 'custom-type)
368 (user-variable-p symbol))))))
369 (list (if (equal val "")
372 (defun custom-menu-filter (menu widget)
373 "Convert MENU to the form used by `widget-choose'.
374 MENU should be in the same format as `custom-variable-menu'.
375 WIDGET is the widget to apply the filter entries of MENU on."
377 current name action filter)
379 (setq current (car menu)
381 action (nth 1 current)
382 filter (nth 2 current)
384 (if (or (null filter) (funcall filter widget))
385 (push (cons name action) result)
391 (defvar custom-prefix-list nil
392 "List of prefixes that should be ignored by `custom-unlispify'")
394 (defcustom custom-unlispify-menu-entries t
395 "Display menu entries as words instead of symbols if non nil."
399 (defun custom-unlispify-menu-entry (symbol &optional no-suffix)
400 "Convert symbol into a menu entry."
401 (cond ((not custom-unlispify-menu-entries)
402 (symbol-name symbol))
403 ((get symbol 'custom-tag)
405 (get symbol 'custom-tag)
406 (concat (get symbol 'custom-tag) "...")))
409 (set-buffer (get-buffer-create " *Custom-Work*"))
411 (princ symbol (current-buffer))
412 (goto-char (point-min))
413 (when (and (eq (get symbol 'custom-type) 'boolean)
414 (re-search-forward "-p\\'" nil t))
415 (replace-match "" t t)
416 (goto-char (point-min)))
417 (let ((prefixes custom-prefix-list)
420 (setq prefix (car prefixes))
421 (if (search-forward prefix (+ (point) (length prefix)) t)
424 (delete-region (point-min) (point)))
425 (setq prefixes (cdr prefixes)))))
426 (subst-char-in-region (point-min) (point-max) ?- ?\ t)
427 (capitalize-region (point-min) (point-max))
429 (goto-char (point-max))
433 (defcustom custom-unlispify-tag-names t
434 "Display tag names as words instead of symbols if non nil."
438 (defun custom-unlispify-tag-name (symbol)
439 "Convert symbol into a menu entry."
440 (let ((custom-unlispify-menu-entries custom-unlispify-tag-names))
441 (custom-unlispify-menu-entry symbol t)))
443 (defun custom-prefix-add (symbol prefixes)
444 ;; Addd SYMBOL to list of ignored PREFIXES.
445 (cons (or (get symbol 'custom-prefix)
446 (concat (symbol-name symbol) "-"))
451 (defcustom custom-guess-name-alist
456 ("-function\\'" function)
457 ("-functions\\'" (repeat function))
458 ("-list\\'" (repeat sexp))
459 ("-alist\\'" (repeat (cons sexp sexp))))
460 "Alist of (MATCH TYPE).
462 MATCH should be a regexp matching the name of a symbol, and TYPE should
463 be a widget suitable for editing the value of that symbol. The TYPE
464 of the first entry where MATCH matches the name of the symbol will be
467 This is used for guessing the type of variables not declared with
469 :type '(repeat (group (regexp :tag "Match") (sexp :tag "Type")))
472 (defcustom custom-guess-doc-alist
473 '(("\\`\\*?Non-nil " boolean))
474 "Alist of (MATCH TYPE).
476 MATCH should be a regexp matching a documentation string, and TYPE
477 should be a widget suitable for editing the value of a variable with
478 that documentation string. The TYPE of the first entry where MATCH
479 matches the name of the symbol will be used.
481 This is used for guessing the type of variables not declared with
483 :type '(repeat (group (regexp :tag "Match") (sexp :tag "Type")))
486 (defun custom-guess-type (symbol)
487 "Guess a widget suitable for editing the value of SYMBOL.
488 This is done by matching SYMBOL with `custom-guess-name-alist' and
489 if that fails, the doc string with `custom-guess-doc-alist'."
490 (let ((name (symbol-name symbol))
491 (names custom-guess-name-alist)
494 (setq current (car names)
496 (when (string-match (nth 0 current) name)
497 (setq found (nth 1 current)
500 (let ((doc (documentation-property symbol 'variable-documentation))
501 (docs custom-guess-doc-alist))
504 (setq current (car docs)
506 (when (string-match (nth 0 current) doc)
507 (setq found (nth 1 current)
511 ;;; Custom Mode Commands.
513 (defvar custom-options nil
514 "Customization widgets in the current buffer.")
517 "Set changes in all modified options."
519 (let ((children custom-options))
520 (mapcar (lambda (child)
521 (when (eq (widget-get child :custom-state) 'modified)
522 (widget-apply child :custom-set)))
525 (defun custom-save ()
526 "Set all modified group members and save them."
528 (let ((children custom-options))
529 (mapcar (lambda (child)
530 (when (memq (widget-get child :custom-state) '(modified set))
531 (widget-apply child :custom-save)))
535 (defvar custom-reset-menu
536 '(("Current" . custom-reset-current)
537 ("Saved" . custom-reset-saved)
538 ("Factory Settings" . custom-reset-factory))
539 "Alist of actions for the `Reset' button.
540 The key is a string containing the name of the action, the value is a
541 lisp function taking the widget as an element which will be called
542 when the action is chosen.")
544 (defun custom-reset (event)
545 "Select item from reset menu."
546 (let* ((completion-ignore-case t)
547 (answer (widget-choose "Reset to"
553 (defun custom-reset-current ()
554 "Reset all modified group members to their current value."
556 (let ((children custom-options))
557 (mapcar (lambda (child)
558 (when (eq (widget-get child :custom-state) 'modified)
559 (widget-apply child :custom-reset-current)))
562 (defun custom-reset-saved ()
563 "Reset all modified or set group members to their saved value."
565 (let ((children custom-options))
566 (mapcar (lambda (child)
567 (when (eq (widget-get child :custom-state) 'modified)
568 (widget-apply child :custom-reset-current)))
571 (defun custom-reset-factory ()
572 "Reset all modified, set, or saved group members to their factory settings."
574 (let ((children custom-options))
575 (mapcar (lambda (child)
576 (when (eq (widget-get child :custom-state) 'modified)
577 (widget-apply child :custom-reset-current)))
580 ;;; The Customize Commands
582 (defun custom-prompt-variable (prompt-var prompt-val)
583 "Prompt for a variable and a value and return them as a list.
584 PROMPT-VAR is the prompt for the variable, and PROMPT-VAL is the
585 prompt for the value. The %s escape in PROMPT-VAL is replaced with
586 the name of the variable.
588 If the variable has a `variable-interactive' property, that is used as if
589 it were the arg to `interactive' (which see) to interactively read the value.
591 If the variable has a `custom-type' property, it must be a widget and the
592 `:prompt-value' property of that widget will be used for reading the value."
593 (let* ((var (read-variable prompt-var))
594 (minibuffer-help-form '(describe-variable var)))
596 (let ((prop (get var 'variable-interactive))
597 (type (get var 'custom-type))
598 (prompt (format prompt-val var)))
600 (setq type (list type)))
602 ;; Use VAR's `variable-interactive' property
603 ;; as an interactive spec for prompting.
604 (call-interactively (list 'lambda '(arg)
605 (list 'interactive prop)
608 (widget-prompt-value type
614 (eval-minibuffer prompt)))))))
617 (defun custom-set-value (var val)
618 "Set VARIABLE to VALUE. VALUE is a Lisp object.
620 If VARIABLE has a `variable-interactive' property, that is used as if
621 it were the arg to `interactive' (which see) to interactively read the value.
623 If VARIABLE has a `custom-type' property, it must be a widget and the
624 `:prompt-value' property of that widget will be used for reading the value."
625 (interactive (custom-prompt-variable "Set variable: "
626 "Set %s to value: "))
631 (defun custom-set-variable (var val)
632 "Set the default for VARIABLE to VALUE. VALUE is a Lisp object.
634 If VARIABLE has a `custom-set' property, that is used for setting
635 VARIABLE, otherwise `set-default' is used.
637 The `customized-value' property of the VARIABLE will be set to a list
638 with a quoted VALUE as its sole list member.
640 If VARIABLE has a `variable-interactive' property, that is used as if
641 it were the arg to `interactive' (which see) to interactively read the value.
643 If VARIABLE has a `custom-type' property, it must be a widget and the
644 `:prompt-value' property of that widget will be used for reading the value. "
645 (interactive (custom-prompt-variable "Set variable: "
646 "Set customized value for %s to: "))
647 (funcall (or (get var 'custom-set) 'set-default) var val)
648 (put var 'customized-value (list (custom-quote val))))
651 (defun customize (symbol)
652 "Customize SYMBOL, which must be a customization group."
653 (interactive (list (completing-read "Customize group: (default emacs) "
656 (get symbol 'custom-group))
659 (when (stringp symbol)
660 (if (string-equal "" symbol)
662 (setq symbol (intern symbol))))
663 (custom-buffer-create (list (list symbol 'custom-group))))
666 (defun customize-other-window (symbol)
667 "Customize SYMBOL, which must be a customization group."
668 (interactive (list (completing-read "Customize group: (default emacs) "
671 (get symbol 'custom-group))
674 (when (stringp symbol)
675 (if (string-equal "" symbol)
677 (setq symbol (intern symbol))))
678 (custom-buffer-create-other-window (list (list symbol 'custom-group))))
681 (defun customize-variable (symbol)
682 "Customize SYMBOL, which must be a variable."
683 (interactive (custom-variable-prompt))
684 (custom-buffer-create (list (list symbol 'custom-variable))))
687 (defun customize-variable-other-window (symbol)
688 "Customize SYMBOL, which must be a variable.
689 Show the buffer in another window, but don't select it."
690 (interactive (custom-variable-prompt))
691 (custom-buffer-create-other-window (list (list symbol 'custom-variable))))
694 (defun customize-face (&optional symbol)
695 "Customize SYMBOL, which should be a face name or nil.
696 If SYMBOL is nil, customize all faces."
697 (interactive (list (completing-read "Customize face: (default all) "
698 obarray 'custom-facep)))
699 (if (or (null symbol) (and (stringp symbol) (zerop (length symbol))))
701 (message "Looking for faces...")
702 (mapcar (lambda (symbol)
703 (setq found (cons (list symbol 'custom-face) found)))
704 (nreverse (mapcar 'intern
705 (sort (mapcar 'symbol-name (face-list))
708 (custom-buffer-create found))
710 (setq symbol (intern symbol)))
711 (unless (symbolp symbol)
712 (error "Should be a symbol %S" symbol))
713 (custom-buffer-create (list (list symbol 'custom-face)))))
716 (defun customize-face-other-window (&optional symbol)
717 "Show customization buffer for FACE in other window."
718 (interactive (list (completing-read "Customize face: "
719 obarray 'custom-facep)))
720 (if (or (null symbol) (and (stringp symbol) (zerop (length symbol))))
723 (setq symbol (intern symbol)))
724 (unless (symbolp symbol)
725 (error "Should be a symbol %S" symbol))
726 (custom-buffer-create-other-window (list (list symbol 'custom-face)))))
729 (defun customize-customized ()
730 "Customize all user options set since the last save in this session."
733 (mapatoms (lambda (symbol)
734 (and (get symbol 'customized-face)
735 (custom-facep symbol)
736 (setq found (cons (list symbol 'custom-face) found)))
737 (and (get symbol 'customized-value)
740 (cons (list symbol 'custom-variable) found)))))
742 (custom-buffer-create found)
743 (error "No customized user options"))))
746 (defun customize-saved ()
747 "Customize all already saved user options."
750 (mapatoms (lambda (symbol)
751 (and (get symbol 'saved-face)
752 (custom-facep symbol)
753 (setq found (cons (list symbol 'custom-face) found)))
754 (and (get symbol 'saved-value)
757 (cons (list symbol 'custom-variable) found)))))
759 (custom-buffer-create found)
760 (error "No saved user options"))))
763 (defun customize-apropos (regexp &optional all)
764 "Customize all user options matching REGEXP.
765 If ALL (e.g., started with a prefix key), include options which are not
767 (interactive "sCustomize regexp: \nP")
769 (mapatoms (lambda (symbol)
770 (when (string-match regexp (symbol-name symbol))
771 (when (get symbol 'custom-group)
772 (setq found (cons (list symbol 'custom-group) found)))
773 (when (custom-facep symbol)
774 (setq found (cons (list symbol 'custom-face) found)))
775 (when (and (boundp symbol)
776 (or (get symbol 'saved-value)
777 (get symbol 'factory-value)
779 (get symbol 'variable-documentation)
780 (user-variable-p symbol))))
782 (cons (list symbol 'custom-variable) found))))))
784 (custom-buffer-create found)
785 (error "No matches"))))
790 (defun custom-buffer-create (options)
791 "Create a buffer containing OPTIONS.
792 OPTIONS should be an alist of the form ((SYMBOL WIDGET)...), where
793 SYMBOL is a customization option, and WIDGET is a widget for editing
795 (kill-buffer (get-buffer-create "*Customization*"))
796 (switch-to-buffer (get-buffer-create "*Customization*"))
797 (custom-buffer-create-internal options))
800 (defun custom-buffer-create-other-window (options)
801 "Create a buffer containing OPTIONS.
802 OPTIONS should be an alist of the form ((SYMBOL WIDGET)...), where
803 SYMBOL is a customization option, and WIDGET is a widget for editing
805 (kill-buffer (get-buffer-create "*Customization*"))
806 (let ((window (selected-window)))
807 (switch-to-buffer-other-window (get-buffer-create "*Customization*"))
808 (custom-buffer-create-internal options)
809 (select-window window)))
812 (defun custom-buffer-create-internal (options)
813 (message "Creating customization buffer...")
815 (widget-insert "This is a customization buffer.
816 Push RET or click mouse-2 on the word ")
817 ;; (put-text-property 1 2 'start-open nil)
818 (widget-create 'info-link
820 :help-echo "Read the online help."
821 "(custom)The Customization Buffer")
822 (widget-insert " for more information.\n\n")
824 (if (= (length options) 1)
825 (mapcar (lambda (entry)
826 (widget-create (nth 1 entry)
827 :custom-state 'unknown
828 :tag (custom-unlispify-tag-name
830 :value (nth 0 entry)))
833 (length (length options)))
834 (mapcar (lambda (entry)
836 (message "Creating customization items %2d%%..."
837 (/ (* 100.0 count) length))
838 (widget-create (nth 1 entry)
839 :tag (custom-unlispify-tag-name
841 :value (nth 0 entry))
842 (setq count (1+ count))
843 (unless (eq (preceding-char) ?\n)
844 (widget-insert "\n"))
845 (widget-insert "\n")))
847 (unless (eq (preceding-char) ?\n)
848 (widget-insert "\n"))
850 (message "Creating customization magic...")
851 (mapcar 'custom-magic-reset custom-options)
852 (message "Creating customization buttons...")
853 (widget-create 'push-button
855 :help-echo "Set all modifications for this session."
856 :action (lambda (widget &optional event)
859 (widget-create 'push-button
862 Make the modifications default for future sessions."
863 :action (lambda (widget &optional event)
866 (widget-create 'push-button
868 :help-echo "Undo all modifications."
869 :action (lambda (widget &optional event)
870 (custom-reset event)))
872 (widget-create 'push-button
874 :help-echo "Bury the buffer."
875 :action (lambda (widget &optional event)
877 ;; Steal button release event.
878 (if (and (fboundp 'button-press-event-p)
879 (fboundp 'next-command-event))
882 (button-press-event-p event)
883 (next-command-event))
885 (when (memq 'down (event-modifiers event))
888 (message "Creating customization setup...")
890 (goto-char (point-min))
891 (forward-line 3) ;Kludge: bob is writable in XEmacs.
892 (message "Creating customization buffer...done"))
894 ;;; Modification of Basic Widgets.
896 ;; We add extra properties to the basic widgets needed here. This is
897 ;; fine, as long as we are careful to stay within out own namespace.
899 ;; We want simple widgets to be displayed by default, but complex
900 ;; widgets to be hidden.
902 (widget-put (get 'item 'widget-type) :custom-show t)
903 (widget-put (get 'editable-field 'widget-type)
904 :custom-show (lambda (widget value)
905 (let ((pp (pp-to-string value)))
906 (cond ((string-match "\n" pp)
911 (widget-put (get 'menu-choice 'widget-type) :custom-show t)
913 ;;; The `custom-manual' Widget.
915 (define-widget 'custom-manual 'info-link
916 "Link to the manual entry for this customization option."
917 :help-echo "Read the manual entry for this option."
920 ;;; The `custom-magic' Widget.
922 (defface custom-invalid-face '((((class color))
923 (:foreground "yellow" :background "red"))
925 (:bold t :italic t :underline t)))
926 "Face used when the customize item is invalid.")
928 (defface custom-rogue-face '((((class color))
929 (:foreground "pink" :background "black"))
932 "Face used when the customize item is not defined for customization.")
934 (defface custom-modified-face '((((class color))
935 (:foreground "white" :background "blue"))
938 "Face used when the customize item has been modified.")
940 (defface custom-set-face '((((class color))
941 (:foreground "blue" :background "white"))
944 "Face used when the customize item has been set.")
946 (defface custom-changed-face '((((class color))
947 (:foreground "white" :background "blue"))
950 "Face used when the customize item has been changed.")
952 (defface custom-saved-face '((t (:underline t)))
953 "Face used when the customize item has been saved.")
955 (defcustom custom-magic-alist '((nil "#" underline "\
956 uninitialized, you should not see this.")
957 (unknown "?" italic "\
958 unknown, you should not see this.")
959 (hidden "-" default "\
960 hidden, press the state button to show.")
961 (invalid "x" custom-invalid-face "\
962 the value displayed for this item is invalid and cannot be set.")
963 (modified "*" custom-modified-face "\
964 you have edited the item, and can now set it.")
965 (set "+" custom-set-face "\
966 you have set this item, but not saved it.")
967 (changed ":" custom-changed-face "\
968 this item has been changed outside customize.")
969 (saved "!" custom-saved-face "\
970 this item has been saved.")
971 (rogue "@" custom-rogue-face "\
972 this item is not prepared for customization.")
974 this item is unchanged from its factory setting."))
975 "Alist of customize option states.
976 Each entry is of the form (STATE MAGIC FACE DESCRIPTION), where
978 STATE is one of the following symbols:
981 For internal use, should never occur.
983 For internal use, should never occur.
985 This item is not being displayed.
987 This item is modified, but has an invalid form.
989 This item is modified, and has a valid form.
991 This item has been set but not saved.
993 The current value of this item has been changed temporarily.
995 This item is marked for saving.
997 This item has no customization information.
999 This item is unchanged from the factory default.
1001 MAGIC is a string used to present that state.
1003 FACE is a face used to present the state.
1005 DESCRIPTION is a string describing the state.
1007 The list should be sorted most significant first."
1008 :type '(list (checklist :inline t
1010 (string :tag "Magic")
1012 (string :tag "Description"))
1013 (group (const unknown)
1014 (string :tag "Magic")
1016 (string :tag "Description"))
1017 (group (const hidden)
1018 (string :tag "Magic")
1020 (string :tag "Description"))
1021 (group (const invalid)
1022 (string :tag "Magic")
1024 (string :tag "Description"))
1025 (group (const modified)
1026 (string :tag "Magic")
1028 (string :tag "Description"))
1030 (string :tag "Magic")
1032 (string :tag "Description"))
1033 (group (const changed)
1034 (string :tag "Magic")
1036 (string :tag "Description"))
1037 (group (const saved)
1038 (string :tag "Magic")
1040 (string :tag "Description"))
1041 (group (const rogue)
1042 (string :tag "Magic")
1044 (string :tag "Description"))
1045 (group (const factory)
1046 (string :tag "Magic")
1048 (string :tag "Description")))
1049 (editable-list :inline t
1051 (string :tag "Magic")
1053 (string :tag "Description"))))
1055 :group 'custom-faces)
1057 (defcustom custom-magic-show 'long
1058 "Show long description of the state of each customization option."
1059 :type '(choice (const :tag "no" nil)
1064 (defcustom custom-magic-show-button t
1065 "Show a magic button indicating the state of each customization option."
1069 (define-widget 'custom-magic 'default
1070 "Show and manipulate state for a customization option."
1072 :action 'widget-choice-item-action
1075 :value-create 'custom-magic-value-create
1076 :value-delete 'widget-children-value-delete)
1078 (defun custom-magic-value-create (widget)
1079 ;; Create compact status report for WIDGET.
1080 (let* ((parent (widget-get widget :parent))
1081 (state (widget-get parent :custom-state))
1082 (entry (assq state custom-magic-alist))
1083 (magic (nth 1 entry))
1084 (face (nth 2 entry))
1085 (text (nth 3 entry))
1086 (lisp (eq (widget-get parent :custom-form) 'lisp))
1088 (when custom-magic-show
1089 (push (widget-create-child-and-convert widget 'choice-item
1091 Change the state of this item."
1096 (if (eq custom-magic-show 'long)
1098 (insert (symbol-name state)))
1102 (when custom-magic-show-button
1103 (when custom-magic-show
1104 (let ((indent (widget-get parent :indent)))
1106 (insert-char ? indent))))
1107 (push (widget-create-child-and-convert widget 'choice-item
1109 :help-echo "Change the state."
1112 (concat "(" magic ")")
1113 (concat "[" magic "]")))
1116 (widget-put widget :children children)))
1118 (defun custom-magic-reset (widget)
1119 "Redraw the :custom-magic property of WIDGET."
1120 (let ((magic (widget-get widget :custom-magic)))
1121 (widget-value-set magic (widget-value magic))))
1123 ;;; The `custom-level' Widget.
1125 (define-widget 'custom-level 'item
1126 "The custom level buttons."
1128 :help-echo "Expand or collapse this item."
1129 :action 'custom-level-action)
1131 (defun custom-level-action (widget &optional event)
1132 "Toggle visibility for parent to WIDGET."
1133 (custom-toggle-hide (widget-get widget :parent)))
1135 ;;; The `custom' Widget.
1137 (define-widget 'custom 'default
1138 "Customize a user option."
1139 :convert-widget 'custom-convert-widget
1140 :format "%l%[%t%]: %v%m%h%a"
1141 :format-handler 'custom-format-handler
1142 :notify 'custom-notify
1144 :custom-state 'hidden
1145 :documentation-property 'widget-subclass-responsibility
1146 :value-create 'widget-subclass-responsibility
1147 :value-delete 'widget-children-value-delete
1148 :value-get 'widget-item-value-get
1149 :validate 'widget-editable-list-validate
1150 :match (lambda (widget value) (symbolp value)))
1152 (defun custom-convert-widget (widget)
1153 ;; Initialize :value and :tag from :args in WIDGET.
1154 (let ((args (widget-get widget :args)))
1156 (widget-put widget :value (widget-apply widget
1157 :value-to-internal (car args)))
1158 (widget-put widget :tag (custom-unlispify-tag-name (car args)))
1159 (widget-put widget :args nil)))
1162 (defun custom-format-handler (widget escape)
1163 ;; We recognize extra escape sequences.
1164 (let* ((buttons (widget-get widget :buttons))
1165 (state (widget-get widget :custom-state))
1166 (level (widget-get widget :custom-level)))
1167 (cond ((eq escape ?l)
1169 (push (widget-create-child-and-convert
1170 widget 'custom-level (make-string level ?*))
1173 (widget-put widget :buttons buttons)))
1175 (when (eq state 'hidden)
1176 (widget-insert " ...")))
1178 (and (eq (preceding-char) ?\n)
1179 (widget-get widget :indent)
1180 (insert-char ? (widget-get widget :indent)))
1181 (let ((magic (widget-create-child-and-convert
1182 widget 'custom-magic nil)))
1183 (widget-put widget :custom-magic magic)
1184 (push magic buttons)
1185 (widget-put widget :buttons buttons)))
1187 (let* ((symbol (widget-get widget :value))
1188 (links (get symbol 'custom-links))
1189 (many (> (length links) 2)))
1191 (and (eq (preceding-char) ?\n)
1192 (widget-get widget :indent)
1193 (insert-char ? (widget-get widget :indent)))
1194 (insert "See also ")
1196 (push (widget-create-child-and-convert widget (car links))
1198 (setq links (cdr links))
1207 (widget-put widget :buttons buttons))))
1209 (widget-default-format-handler widget escape)))))
1211 (defun custom-notify (widget &rest args)
1212 "Keep track of changes."
1213 (unless (memq (widget-get widget :custom-state) '(nil unknown hidden))
1214 (widget-put widget :custom-state 'modified))
1215 (let ((buffer-undo-list t))
1216 (custom-magic-reset widget))
1217 (apply 'widget-default-notify widget args))
1219 (defun custom-redraw (widget)
1220 "Redraw WIDGET with current settings."
1221 (let ((line (count-lines (point-min) (point)))
1222 (column (current-column))
1224 (from (marker-position (widget-get widget :from)))
1225 (to (marker-position (widget-get widget :to))))
1227 (widget-value-set widget (widget-value widget))
1228 (custom-redraw-magic widget))
1229 (when (and (>= pos from) (<= pos to))
1233 (move-to-column column))
1236 (defun custom-redraw-magic (widget)
1237 "Redraw WIDGET state with current settings."
1239 (let ((magic (widget-get widget :custom-magic)))
1242 (widget-value-set magic (widget-value magic))
1243 (when (setq widget (widget-get widget :group))
1244 (custom-group-state-update widget))))
1247 (defun custom-show (widget value)
1248 "Non-nil if WIDGET should be shown with VALUE by default."
1249 (let ((show (widget-get widget :custom-show)))
1255 (funcall show widget value)))))
1257 (defvar custom-load-recursion nil
1258 "Hack to avoid recursive dependencies.")
1260 (defun custom-load-symbol (symbol)
1261 "Load all dependencies for SYMBOL."
1262 (unless custom-load-recursion
1263 (let ((custom-load-recursion t)
1264 (loads (get symbol 'custom-loads))
1267 (setq load (car loads)
1269 (cond ((symbolp load)
1273 ((assoc load load-history))
1279 (defun custom-load-widget (widget)
1280 "Load all dependencies for WIDGET."
1281 (custom-load-symbol (widget-value widget)))
1283 (defun custom-toggle-hide (widget)
1284 "Toggle visibility of WIDGET."
1285 (let ((state (widget-get widget :custom-state)))
1286 (cond ((memq state '(invalid modified))
1287 (error "There are unset changes"))
1289 (widget-put widget :custom-state 'unknown))
1291 (widget-put widget :custom-state 'hidden)))
1292 (custom-redraw widget)))
1294 ;;; The `custom-variable' Widget.
1296 (defface custom-variable-sample-face '((t (:underline t)))
1297 "Face used for unpushable variable tags."
1298 :group 'custom-faces)
1300 (defface custom-variable-button-face '((t (:underline t :bold t)))
1301 "Face used for pushable variable tags."
1302 :group 'custom-faces)
1304 (define-widget 'custom-variable 'custom
1305 "Customize variable."
1306 :format "%l%v%m%h%a"
1307 :help-echo "Set or reset this variable."
1308 :documentation-property 'variable-documentation
1310 :custom-menu 'custom-variable-menu-create
1312 :value-create 'custom-variable-value-create
1313 :action 'custom-variable-action
1314 :custom-set 'custom-variable-set
1315 :custom-save 'custom-variable-save
1316 :custom-reset-current 'custom-redraw
1317 :custom-reset-saved 'custom-variable-reset-saved
1318 :custom-reset-factory 'custom-variable-reset-factory)
1320 (defun custom-variable-type (symbol)
1321 "Return a widget suitable for editing the value of SYMBOL.
1322 If SYMBOL has a `custom-type' property, use that.
1323 Otherwise, look up symbol in `custom-guess-type-alist'."
1324 (let* ((type (or (get symbol 'custom-type)
1325 (and (not (get symbol 'factory-value))
1326 (custom-guess-type symbol))
1328 (options (get symbol 'custom-options))
1329 (tmp (if (listp type)
1330 (copy-sequence type)
1333 (widget-put tmp :options options))
1336 (defun custom-variable-value-create (widget)
1337 "Here is where you edit the variables value."
1338 (custom-load-widget widget)
1339 (let* ((buttons (widget-get widget :buttons))
1340 (children (widget-get widget :children))
1341 (form (widget-get widget :custom-form))
1342 (state (widget-get widget :custom-state))
1343 (symbol (widget-get widget :value))
1344 (tag (widget-get widget :tag))
1345 (type (custom-variable-type symbol))
1346 (conv (widget-convert type))
1347 (get (or (get symbol 'custom-get) 'default-value))
1348 (set (or (get symbol 'custom-set) 'set-default))
1349 (value (if (default-boundp symbol)
1350 (funcall get symbol)
1351 (widget-get conv :value))))
1352 ;; If the widget is new, the child determine whether it is hidden.
1354 ((custom-show type value)
1355 (setq state 'unknown))
1357 (setq state 'hidden)))
1358 ;; If we don't know the state, see if we need to edit it in lisp form.
1359 (when (eq state 'unknown)
1360 (unless (widget-apply conv :match value)
1361 ;; (widget-apply (widget-convert type) :match value)
1363 ;; Now we can create the child widget.
1364 (cond ((eq state 'hidden)
1365 ;; Indicate hidden value.
1366 (push (widget-create-child-and-convert
1368 :format "%{%t%}: ..."
1369 :sample-face 'custom-variable-sample-face
1374 ;; In lisp mode edit the saved value when possible.
1375 (let* ((value (cond ((get symbol 'saved-value)
1376 (car (get symbol 'saved-value)))
1377 ((get symbol 'factory-value)
1378 (car (get symbol 'factory-value)))
1379 ((default-boundp symbol)
1380 (custom-quote (funcall get symbol)))
1382 (custom-quote (widget-get conv :value))))))
1383 (push (widget-create-child-and-convert
1385 :button-face 'custom-variable-button-face
1386 :tag (symbol-name symbol)
1392 (push (widget-create-child-and-convert
1395 :button-face 'custom-variable-button-face
1396 :sample-face 'custom-variable-sample-face
1399 ;; Now update the state.
1400 (unless (eq (preceding-char) ?\n)
1401 (widget-insert "\n"))
1402 (if (eq state 'hidden)
1403 (widget-put widget :custom-state state)
1404 (custom-variable-state-set widget))
1405 (widget-put widget :custom-form form)
1406 (widget-put widget :buttons buttons)
1407 (widget-put widget :children children)))
1409 (defun custom-variable-state-set (widget)
1410 "Set the state of WIDGET."
1411 (let* ((symbol (widget-value widget))
1412 (get (or (get symbol 'custom-get) 'default-value))
1413 (value (if (default-boundp symbol)
1414 (funcall get symbol)
1415 (widget-get widget :value)))
1417 (state (cond ((setq tmp (get symbol 'customized-value))
1418 (if (condition-case nil
1419 (equal value (eval (car tmp)))
1423 ((setq tmp (get symbol 'saved-value))
1424 (if (condition-case nil
1425 (equal value (eval (car tmp)))
1429 ((setq tmp (get symbol 'factory-value))
1430 (if (condition-case nil
1431 (equal value (eval (car tmp)))
1436 (widget-put widget :custom-state state)))
1438 (defvar custom-variable-menu
1439 '(("Hide" custom-toggle-hide
1441 (not (memq (widget-get widget :custom-state) '(modified invalid)))))
1442 ("Edit" custom-variable-edit
1444 (not (eq (widget-get widget :custom-form) 'edit))))
1445 ("Edit Lisp" custom-variable-edit-lisp
1447 (not (eq (widget-get widget :custom-form) 'lisp))))
1448 ("Set" custom-variable-set
1450 (eq (widget-get widget :custom-state) 'modified)))
1451 ("Save" custom-variable-save
1453 (memq (widget-get widget :custom-state) '(modified set changed rogue))))
1454 ("Reset to Current" custom-redraw
1456 (and (default-boundp (widget-value widget))
1457 (memq (widget-get widget :custom-state) '(modified)))))
1458 ("Reset to Saved" custom-variable-reset-saved
1460 (and (get (widget-value widget) 'saved-value)
1461 (memq (widget-get widget :custom-state)
1462 '(modified set changed rogue)))))
1463 ("Reset to Factory Settings" custom-variable-reset-factory
1465 (and (get (widget-value widget) 'factory-value)
1466 (memq (widget-get widget :custom-state)
1467 '(modified set changed saved rogue))))))
1468 "Alist of actions for the `custom-variable' widget.
1469 Each entry has the form (NAME ACTION FILTER) where NAME is the name of
1470 the menu entry, ACTION is the function to call on the widget when the
1471 menu is selected, and FILTER is a predicate which takes a `custom-variable'
1472 widget as an argument, and returns non-nil if ACTION is valid on that
1473 widget. If FILTER is nil, ACTION is always valid.")
1475 (defun custom-variable-action (widget &optional event)
1476 "Show the menu for `custom-variable' WIDGET.
1477 Optional EVENT is the location for the menu."
1478 (if (eq (widget-get widget :custom-state) 'hidden)
1479 (custom-toggle-hide widget)
1480 (let* ((completion-ignore-case t)
1481 (answer (widget-choose (custom-unlispify-tag-name
1482 (widget-get widget :value))
1483 (custom-menu-filter custom-variable-menu
1487 (funcall answer widget)))))
1489 (defun custom-variable-edit (widget)
1490 "Edit value of WIDGET."
1491 (widget-put widget :custom-state 'unknown)
1492 (widget-put widget :custom-form 'edit)
1493 (custom-redraw widget))
1495 (defun custom-variable-edit-lisp (widget)
1496 "Edit the lisp representation of the value of WIDGET."
1497 (widget-put widget :custom-state 'unknown)
1498 (widget-put widget :custom-form 'lisp)
1499 (custom-redraw widget))
1501 (defun custom-variable-set (widget)
1502 "Set the current value for the variable being edited by WIDGET."
1503 (let* ((form (widget-get widget :custom-form))
1504 (state (widget-get widget :custom-state))
1505 (child (car (widget-get widget :children)))
1506 (symbol (widget-value widget))
1507 (set (or (get symbol 'custom-set) 'set-default))
1509 (cond ((eq state 'hidden)
1510 (error "Cannot set hidden variable."))
1511 ((setq val (widget-apply child :validate))
1512 (goto-char (widget-get val :from))
1513 (error "%s" (widget-get val :error)))
1515 (funcall set symbol (eval (setq val (widget-value child))))
1516 (put symbol 'customized-value (list val)))
1518 (funcall set symbol (setq val (widget-value child)))
1519 (put symbol 'customized-value (list (custom-quote val)))))
1520 (custom-variable-state-set widget)
1521 (custom-redraw-magic widget)))
1523 (defun custom-variable-save (widget)
1524 "Set the default value for the variable being edited by WIDGET."
1525 (let* ((form (widget-get widget :custom-form))
1526 (state (widget-get widget :custom-state))
1527 (child (car (widget-get widget :children)))
1528 (symbol (widget-value widget))
1529 (set (or (get symbol 'custom-set) 'set-default))
1531 (cond ((eq state 'hidden)
1532 (error "Cannot set hidden variable."))
1533 ((setq val (widget-apply child :validate))
1534 (goto-char (widget-get val :from))
1535 (error "%s" (widget-get val :error)))
1537 (put symbol 'saved-value (list (widget-value child)))
1538 (funcall set symbol (eval (widget-value child))))
1541 'saved-value (list (custom-quote (widget-value
1543 (funcall set symbol (widget-value child))))
1544 (put symbol 'customized-value nil)
1546 (custom-variable-state-set widget)
1547 (custom-redraw-magic widget)))
1549 (defun custom-variable-reset-saved (widget)
1550 "Restore the saved value for the variable being edited by WIDGET."
1551 (let* ((symbol (widget-value widget))
1552 (set (or (get symbol 'custom-set) 'set-default)))
1553 (if (get symbol 'saved-value)
1555 (funcall set symbol (eval (car (get symbol 'saved-value))))
1557 (error "No saved value for %s" symbol))
1558 (put symbol 'customized-value nil)
1559 (widget-put widget :custom-state 'unknown)
1560 (custom-redraw widget)))
1562 (defun custom-variable-reset-factory (widget)
1563 "Restore the factory setting for the variable being edited by WIDGET."
1564 (let* ((symbol (widget-value widget))
1565 (set (or (get symbol 'custom-set) 'set-default)))
1566 (if (get symbol 'factory-value)
1567 (funcall set symbol (eval (car (get symbol 'factory-value))))
1568 (error "No factory default for %S" symbol))
1569 (put symbol 'customized-value nil)
1570 (when (get symbol 'saved-value)
1571 (put symbol 'saved-value nil)
1573 (widget-put widget :custom-state 'unknown)
1574 (custom-redraw widget)))
1576 ;;; The `custom-face-edit' Widget.
1578 (define-widget 'custom-face-edit 'checklist
1579 "Edit face attributes."
1583 :button-args '(:help-echo "Control whether this attribute have any effect.")
1584 :args (mapcar (lambda (att)
1587 :sibling-args (widget-get (nth 1 att) :sibling-args)
1588 (list 'const :format "" :value (nth 0 att))
1590 custom-face-attributes))
1592 ;;; The `custom-display' Widget.
1594 (define-widget 'custom-display 'menu-choice
1595 "Select a display type."
1598 :help-echo "Specify frames where the face attributes should be used."
1599 :args '((const :tag "all" t)
1603 :args ((group :sibling-args (:help-echo "\
1604 Only match the specified window systems.")
1605 (const :format "Type: "
1607 (checklist :inline t
1610 :sibling-args (:help-echo "\
1611 The X11 Window System.")
1613 (const :format "PM "
1614 :sibling-args (:help-echo "\
1615 OS/2 Presentation Manager.")
1617 (const :format "Win32 "
1618 :sibling-args (:help-echo "\
1621 (const :format "DOS "
1622 :sibling-args (:help-echo "\
1625 (const :format "TTY%n"
1626 :sibling-args (:help-echo "\
1627 Plain text terminals.")
1629 (group :sibling-args (:help-echo "\
1630 Only match the frames with the specified color support.")
1631 (const :format "Class: "
1633 (checklist :inline t
1635 (const :format "Color "
1636 :sibling-args (:help-echo "\
1637 Match color frames.")
1639 (const :format "Grayscale "
1640 :sibling-args (:help-echo "\
1641 Match grayscale frames.")
1643 (const :format "Monochrome%n"
1644 :sibling-args (:help-echo "\
1645 Match frames with no color support.")
1647 (group :sibling-args (:help-echo "\
1648 Only match frames with the specified intensity.")
1650 Background brightness: "
1652 (checklist :inline t
1654 (const :format "Light "
1655 :sibling-args (:help-echo "\
1656 Match frames with light backgrounds.")
1658 (const :format "Dark\n"
1659 :sibling-args (:help-echo "\
1660 Match frames with dark backgrounds.")
1663 ;;; The `custom-face' Widget.
1665 (defface custom-face-tag-face '((t (:underline t)))
1666 "Face used for face tags."
1667 :group 'custom-faces)
1669 (define-widget 'custom-face 'custom
1671 :format "%l%{%t%}: %s%m%h%a%v"
1672 :format-handler 'custom-face-format-handler
1673 :sample-face 'custom-face-tag-face
1674 :help-echo "Set or reset this face."
1675 :documentation-property '(lambda (face)
1676 (face-doc-string face))
1677 :value-create 'custom-face-value-create
1678 :action 'custom-face-action
1679 :custom-form 'selected
1680 :custom-set 'custom-face-set
1681 :custom-save 'custom-face-save
1682 :custom-reset-current 'custom-redraw
1683 :custom-reset-saved 'custom-face-reset-saved
1684 :custom-reset-factory 'custom-face-reset-factory
1685 :custom-menu 'custom-face-menu-create)
1687 (defun custom-face-format-handler (widget escape)
1688 ;; We recognize extra escape sequences.
1690 (symbol (widget-get widget :value)))
1691 (cond ((eq escape ?s)
1692 (and (string-match "XEmacs" emacs-version)
1693 ;; XEmacs cannot display initialized faces.
1694 (not (custom-facep symbol))
1695 (copy-face 'custom-face-empty symbol))
1696 (setq child (widget-create-child-and-convert
1698 :format "(%{%t%})\n"
1702 (custom-format-handler widget escape)))
1705 :buttons (cons child (widget-get widget :buttons))))))
1707 (define-widget 'custom-face-all 'editable-list
1708 "An editable list of display specifications and attributes."
1709 :entry-format "%i %d %v"
1710 :insert-button-args '(:help-echo "Insert new display specification here.")
1711 :append-button-args '(:help-echo "Append new display specification here.")
1712 :delete-button-args '(:help-echo "Delete this display specification.")
1713 :args '((group :format "%v" custom-display custom-face-edit)))
1715 (defconst custom-face-all (widget-convert 'custom-face-all)
1716 "Converted version of the `custom-face-all' widget.")
1718 (define-widget 'custom-display-unselected 'item
1719 "A display specification that doesn't match the selected display."
1720 :match 'custom-display-unselected-match)
1722 (defun custom-display-unselected-match (widget value)
1723 "Non-nil if VALUE is an unselected display specification."
1724 (not (custom-display-match-frame value (selected-frame))))
1726 (define-widget 'custom-face-selected 'group
1727 "Edit the attributes of the selected display in a face specification."
1728 :args '((repeat :format ""
1730 (group custom-display-unselected sexp))
1731 (group (sexp :format "") custom-face-edit)
1736 (defconst custom-face-selected (widget-convert 'custom-face-selected)
1737 "Converted version of the `custom-face-selected' widget.")
1739 (defun custom-face-value-create (widget)
1740 ;; Create a list of the display specifications.
1741 (unless (eq (preceding-char) ?\n)
1743 (when (not (eq (widget-get widget :custom-state) 'hidden))
1744 (message "Creating face editor...")
1745 (custom-load-widget widget)
1746 (let* ((symbol (widget-value widget))
1747 (spec (or (get symbol 'saved-face)
1748 (get symbol 'factory-face)
1749 ;; Attempt to construct it.
1750 (list (list t (custom-face-attributes-get
1751 symbol (selected-frame))))))
1752 (form (widget-get widget :custom-form))
1753 (indent (widget-get widget :indent))
1754 (edit (widget-create-child-and-convert
1756 (cond ((and (eq form 'selected)
1757 (widget-apply custom-face-selected :match spec))
1758 (when indent (insert-char ?\ indent))
1759 'custom-face-selected)
1760 ((and (not (eq form 'lisp))
1761 (widget-apply custom-face-all :match spec))
1764 (when indent (insert-char ?\ indent))
1767 (custom-face-state-set widget)
1768 (widget-put widget :children (list edit)))
1769 (message "Creating face editor...done")))
1771 (defvar custom-face-menu
1772 '(("Hide" custom-toggle-hide
1774 (not (memq (widget-get widget :custom-state) '(modified invalid)))))
1775 ("Edit Selected" custom-face-edit-selected
1777 (not (eq (widget-get widget :custom-form) 'selected))))
1778 ("Edit All" custom-face-edit-all
1780 (not (eq (widget-get widget :custom-form) 'all))))
1781 ("Edit Lisp" custom-face-edit-lisp
1783 (not (eq (widget-get widget :custom-form) 'lisp))))
1784 ("Set" custom-face-set)
1785 ("Save" custom-face-save)
1786 ("Reset to Saved" custom-face-reset-saved
1788 (get (widget-value widget) 'saved-face)))
1789 ("Reset to Factory Setting" custom-face-reset-factory
1791 (get (widget-value widget) 'factory-face))))
1792 "Alist of actions for the `custom-face' widget.
1793 Each entry has the form (NAME ACTION FILTER) where NAME is the name of
1794 the menu entry, ACTION is the function to call on the widget when the
1795 menu is selected, and FILTER is a predicate which takes a `custom-face'
1796 widget as an argument, and returns non-nil if ACTION is valid on that
1797 widget. If FILTER is nil, ACTION is always valid.")
1799 (defun custom-face-edit-selected (widget)
1800 "Edit selected attributes of the value of WIDGET."
1801 (widget-put widget :custom-state 'unknown)
1802 (widget-put widget :custom-form 'selected)
1803 (custom-redraw widget))
1805 (defun custom-face-edit-all (widget)
1806 "Edit all attributes of the value of WIDGET."
1807 (widget-put widget :custom-state 'unknown)
1808 (widget-put widget :custom-form 'all)
1809 (custom-redraw widget))
1811 (defun custom-face-edit-lisp (widget)
1812 "Edit the lisp representation of the value of WIDGET."
1813 (widget-put widget :custom-state 'unknown)
1814 (widget-put widget :custom-form 'lisp)
1815 (custom-redraw widget))
1817 (defun custom-face-state-set (widget)
1818 "Set the state of WIDGET."
1819 (let ((symbol (widget-value widget)))
1820 (widget-put widget :custom-state (cond ((get symbol 'customized-face)
1822 ((get symbol 'saved-face)
1824 ((get symbol 'factory-face)
1829 (defun custom-face-action (widget &optional event)
1830 "Show the menu for `custom-face' WIDGET.
1831 Optional EVENT is the location for the menu."
1832 (if (eq (widget-get widget :custom-state) 'hidden)
1833 (custom-toggle-hide widget)
1834 (let* ((completion-ignore-case t)
1835 (symbol (widget-get widget :value))
1836 (answer (widget-choose (custom-unlispify-tag-name symbol)
1837 (custom-menu-filter custom-face-menu
1841 (funcall answer widget)))))
1843 (defun custom-face-set (widget)
1844 "Make the face attributes in WIDGET take effect."
1845 (let* ((symbol (widget-value widget))
1846 (child (car (widget-get widget :children)))
1847 (value (widget-value child)))
1848 (put symbol 'customized-face value)
1849 (custom-face-display-set symbol value)
1850 (custom-face-state-set widget)
1851 (custom-redraw-magic widget)))
1853 (defun custom-face-save (widget)
1854 "Make the face attributes in WIDGET default."
1855 (let* ((symbol (widget-value widget))
1856 (child (car (widget-get widget :children)))
1857 (value (widget-value child)))
1858 (custom-face-display-set symbol value)
1859 (put symbol 'saved-face value)
1860 (put symbol 'customized-face nil)
1861 (custom-face-state-set widget)
1862 (custom-redraw-magic widget)))
1864 (defun custom-face-reset-saved (widget)
1865 "Restore WIDGET to the face's default attributes."
1866 (let* ((symbol (widget-value widget))
1867 (child (car (widget-get widget :children)))
1868 (value (get symbol 'saved-face)))
1870 (error "No saved value for this face"))
1871 (put symbol 'customized-face nil)
1872 (custom-face-display-set symbol value)
1873 (widget-value-set child value)
1874 (custom-face-state-set widget)
1875 (custom-redraw-magic widget)))
1877 (defun custom-face-reset-factory (widget)
1878 "Restore WIDGET to the face's factory settings."
1879 (let* ((symbol (widget-value widget))
1880 (child (car (widget-get widget :children)))
1881 (value (get symbol 'factory-face)))
1883 (error "No factory default for this face"))
1884 (put symbol 'customized-face nil)
1885 (when (get symbol 'saved-face)
1886 (put symbol 'saved-face nil)
1888 (custom-face-display-set symbol value)
1889 (widget-value-set child value)
1890 (custom-face-state-set widget)
1891 (custom-redraw-magic widget)))
1893 ;;; The `face' Widget.
1895 (define-widget 'face 'default
1896 "Select and customize a face."
1897 :convert-widget 'widget-item-convert-widget
1898 :format "%[%t%]: %v"
1901 :value-create 'widget-face-value-create
1902 :value-delete 'widget-face-value-delete
1903 :value-get 'widget-item-value-get
1904 :validate 'widget-editable-list-validate
1905 :action 'widget-face-action
1906 :match '(lambda (widget value) (symbolp value)))
1908 (defun widget-face-value-create (widget)
1909 ;; Create a `custom-face' child.
1910 (let* ((symbol (widget-value widget))
1911 (child (widget-create-child-and-convert
1913 :format "%t %s%m%h%v"
1916 (custom-magic-reset child)
1917 (setq custom-options (cons child custom-options))
1918 (widget-put widget :children (list child))))
1920 (defun widget-face-value-delete (widget)
1921 ;; Remove the child from the options.
1922 (let ((child (car (widget-get widget :children))))
1923 (setq custom-options (delq child custom-options))
1924 (widget-children-value-delete widget)))
1926 (defvar face-history nil
1927 "History of entered face names.")
1929 (defun widget-face-action (widget &optional event)
1930 "Prompt for a face."
1931 (let ((answer (completing-read "Face: "
1932 (mapcar (lambda (face)
1933 (list (symbol-name face)))
1937 (unless (zerop (length answer))
1938 (widget-value-set widget (intern answer))
1939 (widget-apply widget :notify widget event)
1942 ;;; The `hook' Widget.
1944 (define-widget 'hook 'list
1946 :convert-widget 'custom-hook-convert-widget
1949 (defun custom-hook-convert-widget (widget)
1950 ;; Handle `:custom-options'.
1951 (let* ((options (widget-get widget :options))
1952 (other `(editable-list :inline t
1953 :entry-format "%i %d%v"
1954 (function :format " %v")))
1956 (list `(checklist :inline t
1957 ,@(mapcar (lambda (entry)
1958 `(function-item ,entry))
1962 (widget-put widget :args args)
1965 ;;; The `custom-group' Widget.
1967 (defcustom custom-group-tag-faces '(custom-group-tag-face-1)
1968 ;; In XEmacs, this ought to play games with font size.
1969 "Face used for group tags.
1970 The first member is used for level 1 groups, the second for level 2,
1971 and so forth. The remaining group tags are shown with
1972 `custom-group-tag-face'."
1973 :type '(repeat face)
1974 :group 'custom-faces)
1976 (defface custom-group-tag-face-1 '((((class color)
1978 (:foreground "pink" :underline t))
1981 (:foreground "red" :underline t))
1983 "Face used for group tags.")
1985 (defface custom-group-tag-face '((((class color)
1987 (:foreground "light blue" :underline t))
1990 (:foreground "blue" :underline t))
1992 "Face used for low level group tags."
1993 :group 'custom-faces)
1995 (define-widget 'custom-group 'custom
1997 :format "%l%{%t%}:%L\n%m%h%a%v"
1998 :sample-face-get 'custom-group-sample-face-get
1999 :documentation-property 'group-documentation
2000 :help-echo "Set or reset all members of this group."
2001 :value-create 'custom-group-value-create
2002 :action 'custom-group-action
2003 :custom-set 'custom-group-set
2004 :custom-save 'custom-group-save
2005 :custom-reset-current 'custom-group-reset-current
2006 :custom-reset-saved 'custom-group-reset-saved
2007 :custom-reset-factory 'custom-group-reset-factory
2008 :custom-menu 'custom-group-menu-create)
2010 (defun custom-group-sample-face-get (widget)
2011 ;; Use :sample-face.
2012 (or (nth (1- (widget-get widget :custom-level)) custom-group-tag-faces)
2013 'custom-group-tag-face))
2015 (defun custom-group-value-create (widget)
2016 (let ((state (widget-get widget :custom-state)))
2017 (unless (eq state 'hidden)
2018 (message "Creating group...")
2019 (custom-load-widget widget)
2020 (let* ((level (widget-get widget :custom-level))
2021 (symbol (widget-value widget))
2022 (members (get symbol 'custom-group))
2023 (prefixes (widget-get widget :custom-prefixes))
2024 (custom-prefix-list (custom-prefix-add symbol prefixes))
2025 (length (length members))
2027 (children (mapcar (lambda (entry)
2028 (widget-insert "\n")
2029 (message "Creating group members... %2d%%"
2030 (/ (* 100.0 count) length))
2031 (setq count (1+ count))
2033 (widget-create-child-and-convert
2034 widget (nth 1 entry)
2036 :tag (custom-unlispify-tag-name
2038 :custom-prefixes custom-prefix-list
2039 :custom-level (1+ level)
2040 :value (nth 0 entry))
2041 (unless (eq (preceding-char) ?\n)
2042 (widget-insert "\n"))))
2044 (message "Creating group magic...")
2045 (mapcar 'custom-magic-reset children)
2046 (message "Creating group state...")
2047 (widget-put widget :children children)
2048 (custom-group-state-update widget)
2049 (message "Creating group... done")))))
2051 (defvar custom-group-menu
2052 '(("Hide" custom-toggle-hide
2054 (not (memq (widget-get widget :custom-state) '(modified invalid)))))
2055 ("Set" custom-group-set
2057 (eq (widget-get widget :custom-state) 'modified)))
2058 ("Save" custom-group-save
2060 (memq (widget-get widget :custom-state) '(modified set))))
2061 ("Reset to Current" custom-group-reset-current
2063 (and (default-boundp (widget-value widget))
2064 (memq (widget-get widget :custom-state) '(modified)))))
2065 ("Reset to Saved" custom-group-reset-saved
2067 (and (get (widget-value widget) 'saved-value)
2068 (memq (widget-get widget :custom-state) '(modified set)))))
2069 ("Reset to Factory" custom-group-reset-factory
2071 (and (get (widget-value widget) 'factory-value)
2072 (memq (widget-get widget :custom-state) '(modified set saved))))))
2073 "Alist of actions for the `custom-group' widget.
2074 Each entry has the form (NAME ACTION FILTER) where NAME is the name of
2075 the menu entry, ACTION is the function to call on the widget when the
2076 menu is selected, and FILTER is a predicate which takes a `custom-group'
2077 widget as an argument, and returns non-nil if ACTION is valid on that
2078 widget. If FILTER is nil, ACTION is always valid.")
2080 (defun custom-group-action (widget &optional event)
2081 "Show the menu for `custom-group' WIDGET.
2082 Optional EVENT is the location for the menu."
2083 (if (eq (widget-get widget :custom-state) 'hidden)
2084 (custom-toggle-hide widget)
2085 (let* ((completion-ignore-case t)
2086 (answer (widget-choose (custom-unlispify-tag-name
2087 (widget-get widget :value))
2088 (custom-menu-filter custom-group-menu
2092 (funcall answer widget)))))
2094 (defun custom-group-set (widget)
2095 "Set changes in all modified group members."
2096 (let ((children (widget-get widget :children)))
2097 (mapcar (lambda (child)
2098 (when (eq (widget-get child :custom-state) 'modified)
2099 (widget-apply child :custom-set)))
2102 (defun custom-group-save (widget)
2103 "Save all modified group members."
2104 (let ((children (widget-get widget :children)))
2105 (mapcar (lambda (child)
2106 (when (memq (widget-get child :custom-state) '(modified set))
2107 (widget-apply child :custom-save)))
2110 (defun custom-group-reset-current (widget)
2111 "Reset all modified group members."
2112 (let ((children (widget-get widget :children)))
2113 (mapcar (lambda (child)
2114 (when (eq (widget-get child :custom-state) 'modified)
2115 (widget-apply child :custom-reset-current)))
2118 (defun custom-group-reset-saved (widget)
2119 "Reset all modified or set group members."
2120 (let ((children (widget-get widget :children)))
2121 (mapcar (lambda (child)
2122 (when (memq (widget-get child :custom-state) '(modified set))
2123 (widget-apply child :custom-reset-saved)))
2126 (defun custom-group-reset-factory (widget)
2127 "Reset all modified, set, or saved group members."
2128 (let ((children (widget-get widget :children)))
2129 (mapcar (lambda (child)
2130 (when (memq (widget-get child :custom-state)
2131 '(modified set saved))
2132 (widget-apply child :custom-reset-factory)))
2135 (defun custom-group-state-update (widget)
2137 (unless (eq (widget-get widget :custom-state) 'hidden)
2138 (let* ((children (widget-get widget :children))
2139 (states (mapcar (lambda (child)
2140 (widget-get child :custom-state))
2142 (magics custom-magic-alist)
2145 (let ((magic (car (car magics))))
2146 (if (and (not (eq magic 'hidden))
2147 (memq magic states))
2150 (setq magics (cdr magics)))))
2151 (widget-put widget :custom-state found)))
2152 (custom-magic-reset widget))
2154 ;;; The `custom-save-all' Function.
2156 (defcustom custom-file "~/.emacs"
2157 "File used for storing customization information.
2158 If you change this from the default \"~/.emacs\" you need to
2159 explicitly load that file for the settings to take effect."
2163 (defun custom-save-delete (symbol)
2164 "Delete the call to SYMBOL form `custom-file'.
2165 Leave point at the location of the call, or after the last expression."
2166 (set-buffer (find-file-noselect custom-file))
2167 (goto-char (point-min))
2170 (let ((sexp (condition-case nil
2171 (read (current-buffer))
2172 (end-of-file (throw 'found nil)))))
2173 (when (and (listp sexp)
2174 (eq (car sexp) symbol))
2175 (delete-region (save-excursion
2179 (throw 'found nil))))))
2181 (defun custom-save-variables ()
2182 "Save all customized variables in `custom-file'."
2184 (custom-save-delete 'custom-set-variables)
2185 (let ((standard-output (current-buffer)))
2188 (princ "(custom-set-variables")
2189 (mapatoms (lambda (symbol)
2190 (let ((value (get symbol 'saved-value))
2191 (requests (get symbol 'custom-requests))
2192 (now (not (or (get symbol 'factory-value)
2193 (and (not (boundp symbol))
2194 (not (get symbol 'force-value)))))))
2211 (unless (looking-at "\n")
2214 (defun custom-save-faces ()
2215 "Save all customized faces in `custom-file'."
2217 (custom-save-delete 'custom-set-faces)
2218 (let ((standard-output (current-buffer)))
2221 (princ "(custom-set-faces")
2222 (let ((value (get 'default 'saved-face)))
2223 ;; The default face must be first, since it affects the others.
2225 (princ "\n '(default ")
2227 (if (or (get 'default 'factory-face)
2228 (and (not (custom-facep 'default))
2229 (not (get 'default 'force-face))))
2232 (mapatoms (lambda (symbol)
2233 (let ((value (get symbol 'saved-face)))
2234 (when (and (not (eq symbol 'default))
2235 ;; Don't print default face here.
2241 (if (or (get symbol 'factory-face)
2242 (and (not (custom-facep symbol))
2243 (not (get symbol 'force-face))))
2247 (unless (looking-at "\n")
2251 (defun custom-save-customized ()
2252 "Save all user options which have been set in this session."
2254 (mapatoms (lambda (symbol)
2255 (let ((face (get symbol 'customized-face))
2256 (value (get symbol 'customized-value)))
2258 (put symbol 'saved-face face)
2259 (put symbol 'customized-face nil))
2261 (put symbol 'saved-value value)
2262 (put symbol 'customized-value nil)))))
2263 ;; We really should update all custom buffers here.
2267 (defun custom-save-all ()
2268 "Save all customizations in `custom-file'."
2269 (custom-save-variables)
2272 (set-buffer (find-file-noselect custom-file))
2275 ;;; The Customize Menu.
2279 (unless (string-match "XEmacs" emacs-version)
2280 (defconst custom-help-menu '("Customize"
2281 ["Update menu..." custom-menu-update t]
2282 ["Group..." customize t]
2283 ["Variable..." customize-variable t]
2284 ["Face..." customize-face t]
2285 ["Saved..." customize-customized t]
2286 ["Apropos..." customize-apropos t])
2287 ;; This menu should be identical to the one defined in `menu-bar.el'.
2290 (defun custom-menu-reset ()
2291 "Reset customize menu."
2292 (remove-hook 'custom-define-hook 'custom-menu-reset)
2293 (define-key global-map [menu-bar help-menu customize-menu]
2294 (cons (car custom-help-menu)
2295 (easy-menu-create-keymaps (car custom-help-menu)
2296 (cdr custom-help-menu)))))
2298 (defun custom-menu-update (event)
2299 "Update customize menu."
2301 (add-hook 'custom-define-hook 'custom-menu-reset)
2302 (let* ((emacs (widget-apply '(custom-group) :custom-menu 'emacs))
2303 (menu `(,(car custom-help-menu)
2305 ,@(cdr (cdr custom-help-menu)))))
2306 (let ((map (easy-menu-create-keymaps (car menu) (cdr menu))))
2307 (define-key global-map [menu-bar help-menu customize-menu]
2308 (cons (car menu) map)))))
2310 (defcustom custom-menu-nesting 2
2311 "Maximum nesting in custom menus."
2315 (defun custom-face-menu-create (widget symbol)
2316 "Ignoring WIDGET, create a menu entry for customization face SYMBOL."
2317 (vector (custom-unlispify-menu-entry symbol)
2318 `(custom-buffer-create '((,symbol custom-face)))
2321 (defun custom-variable-menu-create (widget symbol)
2322 "Ignoring WIDGET, create a menu entry for customization variable SYMBOL."
2323 (let ((type (get symbol 'custom-type)))
2324 (unless (listp type)
2325 (setq type (list type)))
2326 (if (and type (widget-get type :custom-menu))
2327 (widget-apply type :custom-menu symbol)
2328 (vector (custom-unlispify-menu-entry symbol)
2329 `(custom-buffer-create '((,symbol custom-variable)))
2332 ;; Add checkboxes to boolean variable entries.
2333 (widget-put (get 'boolean 'widget-type)
2334 :custom-menu (lambda (widget symbol)
2335 (vector (custom-unlispify-menu-entry symbol)
2336 `(custom-buffer-create
2337 '((,symbol custom-variable)))
2339 ':selected symbol)))
2341 (if (string-match "XEmacs" emacs-version)
2342 ;; XEmacs can create menus dynamically.
2343 (defun custom-group-menu-create (widget symbol)
2344 "Ignoring WIDGET, create a menu entry for customization group SYMBOL."
2345 `( ,(custom-unlispify-menu-entry symbol t)
2346 :filter (lambda (&rest junk)
2347 (cdr (custom-menu-create ',symbol)))))
2349 (defun custom-group-menu-create (widget symbol)
2350 "Ignoring WIDGET, create a menu entry for customization group SYMBOL."
2351 ;; Limit the nesting.
2352 (let ((custom-menu-nesting (1- custom-menu-nesting)))
2353 (custom-menu-create symbol))))
2356 (defun custom-menu-create (symbol)
2357 "Create menu for customization group SYMBOL.
2358 The menu is in a format applicable to `easy-menu-define'."
2359 (let* ((item (vector (custom-unlispify-menu-entry symbol)
2360 `(custom-buffer-create '((,symbol custom-group)))
2362 (if (and (or (not (boundp 'custom-menu-nesting))
2363 (>= custom-menu-nesting 0))
2364 (< (length (get symbol 'custom-group)) widget-menu-max-size))
2365 (let ((custom-prefix-list (custom-prefix-add symbol
2366 custom-prefix-list)))
2367 (custom-load-symbol symbol)
2368 `(,(custom-unlispify-menu-entry symbol t)
2371 ,@(mapcar (lambda (entry)
2372 (widget-apply (if (listp (nth 1 entry))
2374 (list (nth 1 entry)))
2375 :custom-menu (nth 0 entry)))
2376 (get symbol 'custom-group))))
2380 (defun customize-menu-create (symbol &optional name)
2381 "Return a customize menu for customization group SYMBOL.
2382 If optional NAME is given, use that as the name of the menu.
2383 Otherwise the menu will be named `Customize'.
2384 The format is suitable for use with `easy-menu-define'."
2386 (setq name "Customize"))
2387 (if (string-match "XEmacs" emacs-version)
2388 ;; We can delay it under XEmacs.
2390 :filter (lambda (&rest junk)
2391 (cdr (custom-menu-create ',symbol))))
2392 ;; But we must create it now under Emacs.
2393 (cons name (cdr (custom-menu-create symbol)))))
2395 ;;; The Custom Mode.
2397 (defvar custom-mode-map nil
2398 "Keymap for `custom-mode'.")
2400 (unless custom-mode-map
2401 (setq custom-mode-map (make-sparse-keymap))
2402 (set-keymap-parent custom-mode-map widget-keymap)
2403 (define-key custom-mode-map "q" 'bury-buffer))
2405 (easy-menu-define custom-mode-customize-menu
2407 "Menu used to customize customization buffers."
2408 (customize-menu-create 'customize))
2410 (easy-menu-define custom-mode-menu
2412 "Menu used in customization buffers."
2414 ["Set" custom-set t]
2415 ["Save" custom-save t]
2416 ["Reset to Current" custom-reset-current t]
2417 ["Reset to Saved" custom-reset-saved t]
2418 ["Reset to Factory Settings" custom-reset-factory t]
2419 ["Info" (Info-goto-node "(custom)The Customization Buffer") t]))
2421 (defcustom custom-mode-hook nil
2422 "Hook called when entering custom-mode."
2426 (defun custom-mode ()
2427 "Major mode for editing customization buffers.
2429 The following commands are available:
2431 Move to next button or editable field. \\[widget-forward]
2432 Move to previous button or editable field. \\[widget-backward]
2433 Activate button under the mouse pointer. \\[widget-button-click]
2434 Activate button under point. \\[widget-button-press]
2435 Set all modifications. \\[custom-set]
2436 Make all modifications default. \\[custom-save]
2437 Reset all modified options. \\[custom-reset-current]
2438 Reset all modified or set options. \\[custom-reset-saved]
2439 Reset all options. \\[custom-reset-factory]
2441 Entry to this mode calls the value of `custom-mode-hook'
2442 if that value is non-nil."
2443 (kill-all-local-variables)
2444 (setq major-mode 'custom-mode
2446 (use-local-map custom-mode-map)
2447 (easy-menu-add custom-mode-customize-menu)
2448 (easy-menu-add custom-mode-menu)
2449 (make-local-variable 'custom-options)
2450 (run-hooks 'custom-mode-hook))
2456 ;; cus-edit.el ends here