1 ;;; custom-edit.el --- Tools for customization Emacs.
3 ;; Copyright (C) 1996 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/
17 (require 'widget-edit)
20 (define-widget-keywords :custom-show :custom-magic
21 :custom-state :custom-level :custom-form
22 :custom-apply :custom-set-default :custom-reset)
26 (defun custom-quote (sexp)
27 "Quote SEXP iff it is not self quoting."
28 (if (or (memq sexp '(t nil))
30 (eq (aref (symbol-name sexp) 0) ?:))
32 (memq (car sexp) '(lambda)))
38 (defun custom-split-regexp-maybe (regexp)
39 "If REGEXP is a string, split it to a list at `\\|'.
40 You can get the original back with from the result with:
41 (mapconcat 'identity result \"\\|\")
43 IF REGEXP is not a string, return it unchanged."
47 (while (string-match "\\\\|" regexp start)
48 (setq all (cons (substring regexp start (match-beginning 0)) all)
50 (nreverse (cons (substring regexp start) all)))
55 (defvar custom-options nil
56 "Customization widgets in the current buffer.")
58 (defvar custom-mode-map nil
59 "Keymap for `custom-mode'.")
61 (unless custom-mode-map
62 (setq custom-mode-map (make-sparse-keymap))
63 (set-keymap-parent custom-mode-map widget-keymap))
65 (easy-menu-define custom-mode-menu
67 "Menu used in customization buffers."
69 ["Apply" custom-apply t]
70 ["Set Default" custom-set-default t]
71 ["Reset" custom-reset t]
72 ["Save" custom-save t]))
75 "Major mode for editing customization buffers.
77 The following commands are available:
79 \\[widget-forward] Move to next button or editable field.
80 \\[widget-backward] Move to previous button or editable field.
81 \\[widget-button-click] Activate button under the mouse pointer.
82 \\[widget-button-press] Activate button under point.
83 \\[custom-apply] Apply all modifications.
84 \\[custom-set-default] Make all modifications default.
85 \\[custom-reset] Undo all modifications.
86 \\[custom-save] Save defaults for future emacs sessions.
88 Entry to this mode calls the value of `custom-mode-hook'
89 if that value is non-nil."
90 (kill-all-local-variables)
91 (setq major-mode 'custom-mode
93 (use-local-map custom-mode-map)
94 (make-local-variable 'custom-options)
95 (run-hooks 'custom-mode-hook))
97 ;;; Custom Mode Commands.
99 (defun custom-apply ()
100 "Apply changes in all modified options."
102 (let ((children custom-options))
103 (mapcar (lambda (child)
104 (when (eq (widget-get child :custom-state) 'modified)
105 (widget-apply child :custom-apply)))
108 (defun custom-set-default ()
109 "Set default in all modified group members."
111 (let ((children custom-options))
112 (mapcar (lambda (child)
113 (when (eq (widget-get child :custom-state) 'modified)
114 (widget-apply child :custom-set-default)))
117 (defun custom-reset ()
118 "Reset all modified group members."
120 (let ((children custom-options))
121 (mapcar (lambda (child)
122 (when (eq (widget-get child :custom-state) 'modified)
123 (widget-apply child :custom-reset)))
126 ;;; The Customize Commands
129 (defun customize (symbol)
130 "Customize SYMBOL, which must be a customization group."
131 (interactive (list (completing-read "Customize group: (default emacs) "
134 (get symbol 'custom-group))
137 (when (stringp symbol)
138 (if (string-equal "" symbol)
140 (setq symbol (intern symbol))))
141 (custom-buffer-create (list (list symbol 'custom-group))))
144 (defun customize-variable (symbol)
145 "Customize SYMBOL, which must be a variable."
147 ;; Code stolen from `help.el'.
148 (let ((v (variable-at-point))
149 (enable-recursive-minibuffers t)
151 (setq val (completing-read
153 (format "Customize variable (default %s): " v)
154 "Customize variable: ")
156 (list (if (equal val "")
158 (custom-buffer-create (list (list symbol 'custom-variable))))
161 (defun customize-face (symbol)
163 (interactive (list (completing-read "Customize face: "
164 obarray 'custom-facep)))
166 (setq symbol (intern symbol)))
167 (unless (symbolp symbol)
168 (error "Should be a symbol %S" symbol))
169 (custom-buffer-create (list (list symbol 'custom-face))))
172 (defun customize-customized ()
173 "Customize all already customized user options."
176 (mapatoms (lambda (symbol)
177 (and (get symbol 'saved-face)
178 (custom-facep symbol)
179 (setq found (cons (list symbol 'custom-face) found)))
180 (and (get symbol 'saved-value)
183 (cons (list symbol 'custom-variable) found)))))
185 (custom-buffer-create found)
186 (error "No customized user options"))))
189 (defun customize-apropos (regexp &optional all)
190 "Customize all user options matching REGEXP.
191 If ALL (e.g., started with a prefix key), include options which are not
193 (interactive "sCustomize regexp: \nP")
195 (mapatoms (lambda (symbol)
196 (when (string-match regexp (symbol-name symbol))
197 (when (get symbol 'custom-group)
198 (setq found (cons (list symbol 'custom-group) found)))
199 (when (custom-facep symbol)
200 (setq found (cons (list symbol 'custom-face) found)))
201 (when (and (boundp symbol)
202 (or (get symbol 'saved-value)
203 (get symbol 'factory-value)
205 (get symbol 'variable-documentation)
206 (user-variable-p symbol))))
208 (cons (list symbol 'custom-variable) found))))))
210 (custom-buffer-create found)
211 (error "No matches"))))
214 (defun custom-buffer-create (options)
215 "Create a buffer containing OPTIONS.
216 OPTIONS should be an alist of the form ((SYMBOL WIDGET)...), where
217 SYMBOL is a customization option, and WIDGET is a widget for editing
219 (kill-buffer (get-buffer-create "*Customization*"))
220 (switch-to-buffer (get-buffer-create "*Customization*"))
222 (widget-insert "This is a customization buffer.
223 Push RET or click mouse-2 on the word ")
224 (widget-create 'info-link
226 :help-echo "Push me for help."
227 "(custom)The Customization Buffer")
228 (widget-insert " for more information.\n\n")
230 (mapcar (lambda (entry)
232 (if (> (length options) 1)
233 (widget-create (nth 1 entry)
234 :value (nth 0 entry))
235 ;; If there is only one entry, don't hide it!
236 (widget-create (nth 1 entry)
237 :custom-state 'unknown
238 :value (nth 0 entry)))
239 (unless (eq (preceding-char) ?\n)
240 (widget-insert "\n"))
241 (widget-insert "\n")))
243 (mapcar 'custom-magic-reset custom-options)
244 (widget-create 'push-button
246 :help-echo "Push me to apply all modifications."
247 :action (lambda (widget &optional event)
250 (widget-create 'push-button
252 :help-echo "Push me to make the modifications default."
253 :action (lambda (widget &optional event)
254 (custom-set-default)))
256 (widget-create 'push-button
258 :help-echo "Push me to undo all modifications.."
259 :action (lambda (widget &optional event)
262 (widget-create 'push-button
264 :help-echo "Push me to store the new defaults permanently."
265 :action (lambda (widget &optional event)
270 ;;; Modification of Basic Widgets.
272 ;; We add extra properties to the basic widgets needed here. This is
273 ;; fine, as long as we are careful to stay within out own namespace.
275 ;; We want simple widgets to be displayed by default, but complex
276 ;; widgets to be hidden.
278 (widget-put (get 'item 'widget-type) :custom-show t)
279 (widget-put (get 'editable-field 'widget-type)
280 :custom-show (lambda (widget value)
281 (let ((pp (pp-to-string value)))
282 (cond ((string-match "\n" pp)
287 (widget-put (get 'menu-choice 'widget-type) :custom-show t)
289 ;;; The `custom-manual' Widget.
291 (define-widget 'custom-manual 'info-link
292 "Link to the manual entry for this customization option."
295 ;;; The `custom-magic' Widget.
297 (define-widget 'custom-magic 'item
298 "Status feedback for customization option."
300 :action 'widget-choice-item-action
301 :value-create 'custom-magic-value-create)
303 (defface custom-invalid-face '((((class color))
304 (:foreground "yellow" :background "red"))
306 (:bold t :italic t :underline t)))
307 "Face used when the customize item is invalid.")
309 (defface custom-rogue-face '((((class color))
310 (:foreground "pink" :background "black"))
313 "Face used when the customize item is not defined for customization.")
315 (defface custom-modified-face '((((class color))
316 (:foreground "white" :background "blue"))
319 "Face used when the customize item has been modified.")
321 (defface custom-applied-face '((((class color))
322 (:foreground "blue" :background "white"))
325 "Face used when the customize item has been applied.")
327 (defface custom-saved-face '((t (:underline t)))
328 "Face used when the customize item has been saved.")
330 (defcustom custom-magic-alist '((nil "#" underline)
333 (invalid "x" custom-invalid-face)
334 (modified "*" custom-modified-face)
335 (applied "+" custom-applied-face)
336 (saved "!" custom-saved-face)
337 (rogue "@" custom-rogue-face)
339 "Alist of magic representing a customize items status.
340 Each entry is of the form (STATE MAGIC FACE), where
342 STATE is one of the following symbols:
345 For internal use, should never occur.
347 For internal use, should never occur.
349 This item is not being displayed.
351 This item is modified, but has an invalid form.
353 This item is modified, and has a valid form.
355 This items current value has been changed temporarily.
357 This item is marked for saving.
359 This item has no customization information.
361 This item is unchanged from the factory default.
363 MAGIC is a string used to present that state.
365 FACE is a face used to present the state.
367 The list should be sorted most significant first."
368 :type '(repeat (list (choice (const nil)
380 (defun custom-magic-value-create (widget)
381 ;; Create compact status report for WIDGET.
382 (let* ((parent (widget-get widget :parent))
383 (state (widget-get parent :custom-state))
384 (entry (assq state custom-magic-alist))
385 (magic (nth 1 entry))
386 (face (nth 2 entry)))
387 (if (eq (widget-get parent :custom-form) 'lisp)
388 (widget-insert "(" magic ")")
389 (widget-insert "[" magic "]"))
390 (widget-put widget :button-face face)))
392 (defun custom-magic-reset (widget)
393 "Redraw the :custom-magic property of WIDGET."
394 (let ((magic (widget-get widget :custom-magic)))
395 (widget-value-set magic (widget-value magic))))
397 ;;; The `custom-level' Widget.
399 (define-widget 'custom-level 'item
400 "The custom level buttons."
402 :help-echo "Push me to expand or collapse this item."
403 :action 'custom-level-action)
405 (defun custom-level-action (widget &optional event)
406 "Toggle visibility for parent to WIDGET."
407 (let* ((parent (widget-get widget :parent))
408 (state (widget-get parent :custom-state)))
409 (cond ((memq state '(invalid modified))
410 (error "There are unapplied changes"))
412 (widget-put parent :custom-state 'unknown))
414 (widget-put parent :custom-state 'hidden)))
415 (custom-redraw parent)))
417 ;;; The `custom' Widget.
419 (defvar custom-save-needed-p nil
420 "Non-nil if any customizations need to be saved.")
422 (add-hook 'kill-emacs-hook 'custom-save-maybe)
424 (defun custom-save-maybe ()
425 (and custom-save-needed-p
426 (y-or-n-p "You have unsaved customizations, save them now? ")
429 (define-widget 'custom 'default
430 "Customize a user option."
431 :convert-widget 'widget-item-convert-widget
432 :format "%l%[%t%]: %v%m %h%a"
433 :format-handler 'custom-format-handler
434 :notify 'custom-notify
436 :custom-state 'hidden
437 :documentation-property 'widget-subclass-responsibility
438 :value-create 'widget-subclass-responsibility
439 :value-delete 'widget-children-value-delete
440 :value-get 'widget-item-value-get
441 :validate 'widget-editable-list-validate
442 :match (lambda (widget value) (symbolp value)))
444 (defun custom-format-handler (widget escape)
445 ;; We recognize extra escape sequences.
446 (let* ((buttons (widget-get widget :buttons))
447 (state (widget-get widget :custom-state))
448 (level (widget-get widget :custom-level)))
449 (cond ((eq escape ?l)
451 (push (widget-create-child-and-convert
452 widget 'custom-level (make-string level ?*))
455 (widget-put widget :buttons buttons)))
457 (push (widget-create-child-and-convert
460 (if (eq state 'hidden) "show" "hide"))
463 (widget-put widget :buttons buttons))
465 (and (eq (preceding-char) ?\n)
466 (widget-get widget :indent)
467 (insert-char ? (widget-get widget :indent)))
468 (let ((magic (widget-create-child-and-convert
469 widget 'custom-magic nil)))
470 (widget-put widget :custom-magic magic)
472 (widget-put widget :buttons buttons)))
474 (let* ((symbol (widget-get widget :value))
475 (links (get symbol 'custom-links))
476 (many (> (length links) 2)))
478 (and (eq (preceding-char) ?\n)
479 (widget-get widget :indent)
480 (insert-char ? (widget-get widget :indent)))
483 (push (widget-create-child-and-convert widget (car links))
485 (setq links (cdr links))
494 (widget-put widget :buttons buttons))))
496 (widget-default-format-handler widget escape)))))
498 (defun custom-notify (widget &rest args)
499 "Keep track of changes."
500 (widget-put widget :custom-state 'modified)
501 (let ((buffer-undo-list t))
502 (custom-magic-reset widget))
503 (apply 'widget-default-notify widget args))
505 (defun custom-redraw (widget)
506 "Redraw WIDGET with current settings."
507 (widget-value-set widget (widget-value widget))
508 (custom-redraw-magic widget))
510 (defun custom-redraw-magic (widget)
511 "Redraw WIDGET state with current settings."
513 (let ((magic (widget-get widget :custom-magic)))
516 (widget-value-set magic (widget-value magic))
517 (when (setq widget (widget-get widget :group))
518 (custom-group-state-update widget))))
521 (defun custom-show (widget value)
522 "Non-nil if WIDGET should be shown with VALUE by default."
523 (let ((show (widget-get widget :custom-show)))
529 (funcall show widget value)))))
531 ;;; The `custom-variable' Widget.
533 (define-widget 'custom-variable 'custom
534 "Customize variable."
535 :format "%l%v%m %h%a"
536 :help-echo "Push me to set or reset this variable."
537 :documentation-property 'variable-documentation
540 :value-create 'custom-variable-value-create
541 :action 'custom-variable-action
542 :custom-apply 'custom-variable-apply
543 :custom-set-default 'custom-variable-set-default
544 :custom-reset 'custom-redraw)
546 (defun custom-variable-value-create (widget)
547 "Here is where you edit the variables value."
548 (let* ((buttons (widget-get widget :buttons))
549 (children (widget-get widget :children))
550 (form (widget-get widget :custom-form))
551 (state (widget-get widget :custom-state))
552 (symbol (widget-get widget :value))
553 (options (get symbol 'custom-options))
554 (child-type (or (get symbol 'custom-type) 'sexp))
555 (type (let ((tmp (if (listp child-type)
556 (copy-list child-type)
559 (widget-put tmp :options options))
561 (conv (widget-convert type))
562 (value (if (default-boundp symbol)
563 (default-value symbol)
564 (widget-get conv :value))))
565 ;; If the widget is new, the child determine whether it is hidden.
567 ((custom-show type value)
568 (setq state 'unknown))
570 (setq state 'hidden)))
571 ;; If we don't know the state, see if we need to edit it in lisp form.
572 (when (eq state 'unknown)
573 (unless (widget-apply (widget-convert type) :match value)
575 ;; Now we can create the child widget.
576 (cond ((eq state 'hidden)
577 ;; Make hidden value easy to show.
578 (push (widget-create-child-and-convert
580 :tag (symbol-name symbol)
581 :format "%t: %[show%]")
584 ;; In lisp mode edit the saved value when possible.
585 (let* ((value (cond ((get symbol 'saved-value)
586 (car (get symbol 'saved-value)))
587 ((get symbol 'factory-value)
588 (car (get symbol 'factory-value)))
589 ((default-boundp symbol)
590 (custom-quote (default-value symbol)))
592 (custom-quote (widget-get conv :value))))))
593 (push (widget-create-child-and-convert widget 'sexp
594 :tag (symbol-name symbol)
600 (push (widget-create-child-and-convert widget type
601 :tag (symbol-name symbol)
604 ;; Now update the state.
605 (unless (eq (preceding-char) ?\n)
606 (widget-insert "\n"))
607 (if (eq state 'hidden)
608 (widget-put widget :custom-state state)
609 (custom-variable-state-set widget))
610 (widget-put widget :custom-form form)
611 (widget-put widget :buttons buttons)
612 (widget-put widget :children children)))
614 (defun custom-variable-state-set (widget)
615 "Set the state of WIDGET."
616 (let* ((symbol (widget-value widget))
617 (value (default-value symbol))
618 (state (if (get symbol 'saved-value)
619 (if (condition-case nil
621 (eval (car (get symbol 'saved-value))))
625 (if (get symbol 'factory-value)
626 (if (condition-case nil
628 (eval (car (get symbol 'factory-value))))
633 (widget-put widget :custom-state state)))
635 (defvar custom-variable-menu
636 '(("Edit" . custom-variable-edit)
637 ("Edit Default" . custom-variable-edit-lisp)
638 ("Apply" . custom-variable-apply)
639 ("Set Default" . custom-variable-set-default)
640 ("Reset" . custom-redraw)
641 ("Reset to Default" . custom-variable-default)
642 ("Reset to Factory Settings" . custom-variable-factory))
643 "Alist of actions for the `custom-variable' widget.
644 The key is a string containing the name of the action, the value is a
645 lisp function taking the widget as an element which will be called
646 when the action is chosen.")
648 (defun custom-variable-action (widget &optional event)
649 "Show the menu for `custom-variable' WIDGET.
650 Optional EVENT is the location for the menu."
651 (let* ((completion-ignore-case t)
652 (answer (widget-choose (symbol-name (widget-get widget :value))
656 (funcall answer widget))))
658 (defun custom-variable-edit (widget)
659 "Edit value of WIDGET."
660 (widget-put widget :custom-state 'unknown)
661 (widget-put widget :custom-form 'edit)
662 (custom-redraw widget))
664 (defun custom-variable-edit-lisp (widget)
665 "Edit the lisp representation of the value of WIDGET."
666 (widget-put widget :custom-state 'unknown)
667 (widget-put widget :custom-form 'lisp)
668 (custom-redraw widget))
670 (defun custom-variable-apply (widget)
671 "Set the current value for the variable being edited by WIDGET."
672 (let ((form (widget-get widget :custom-form))
673 (state (widget-get widget :custom-state))
674 (child (car (widget-get widget :children)))
675 (symbol (widget-value widget))
677 (cond ((eq state 'hidden)
678 (error "Cannot apply hidden variable."))
679 ((setq val (widget-apply child :validate))
680 (error "Invalid %S" val))
682 (set symbol (eval (widget-value child))))
684 (set symbol (widget-value child))))
685 (custom-variable-state-set widget)
686 (custom-redraw-magic widget)))
688 (defun custom-variable-set-default (widget)
689 "Set the default value for the variable being edited by WIDGET."
690 (let ((form (widget-get widget :custom-form))
691 (state (widget-get widget :custom-state))
692 (child (car (widget-get widget :children)))
693 (symbol (widget-value widget))
695 (cond ((eq state 'hidden)
696 (error "Cannot apply hidden variable."))
697 ((setq val (widget-apply child :validate))
698 (error "Invalid %S" val))
700 (setq custom-save-needed-p (cons symbol custom-save-needed-p))
701 (put symbol 'saved-value (list (widget-value child)))
702 (set symbol (eval (widget-value child))))
704 (setq custom-save-needed-p (cons symbol custom-save-needed-p))
706 'saved-value (list (custom-quote (widget-value
708 (set symbol (widget-value child))))
709 (custom-variable-state-set widget)
710 (custom-redraw-magic widget)))
712 (defun custom-variable-default (widget)
713 "Restore the default value for the variable being edited by WIDGET."
714 (let ((symbol (widget-value widget)))
715 (if (get symbol 'saved-value)
717 (set symbol (eval (car (get symbol 'saved-value))))
719 (error "No default value for %s" symbol))
720 (widget-put widget :custom-state 'unknown)
721 (custom-redraw widget)))
723 (defun custom-variable-factory (widget)
724 "Restore the factory setting for the variable being edited by WIDGET."
725 (let ((symbol (widget-value widget)))
726 (if (get symbol 'factory-value)
727 (set symbol (eval (car (get symbol 'factory-value))))
728 (error "No factory default for %S" symbol))
729 (when (get symbol 'saved-value)
730 (setq custom-save-needed-p (cons symbol custom-save-needed-p))
731 (put symbol 'saved-value nil))
732 (widget-put widget :custom-state 'unknown)
733 (custom-redraw widget)))
735 ;;; The `custom-face-edit' Widget.
737 (defvar custom-face-edit-args
738 (mapcar (lambda (att)
741 (list 'const :format "" :value (nth 0 att))
743 custom-face-attributes))
745 (define-widget 'custom-face-edit 'checklist
746 "Edit face attributes."
750 :args (mapcar (lambda (att)
753 (list 'const :format "" :value (nth 0 att))
755 custom-face-attributes))
757 ;;; The `custom-display' Widget.
759 (define-widget 'custom-display 'menu-choice
760 "Select a display type."
763 :args '((const :tag "all" t)
766 :args ((group (const :format "Type: " type)
771 (const :format "TTY%n"
773 (group (const :format "Class: " class)
776 (const :format "Color "
781 (const :format "Monochrome%n"
783 (group (const :format "Background: " background)
786 (const :format "Light "
788 (const :format "Dark\n"
791 ;;; The `custom-face' Widget.
793 (define-widget 'custom-face 'custom
795 :format "%l%[%t%]: %s%m %h%a%v"
796 :format-handler 'custom-face-format-handler
797 :help-echo "Push me to set or reset this face."
798 :documentation-property 'face-documentation
799 :value-create 'custom-face-value-create
800 :action 'custom-face-action
801 :custom-apply 'custom-face-apply
802 :custom-set-default 'custom-face-set-default
803 :custom-reset 'custom-redraw)
805 (defun custom-face-format-handler (widget escape)
806 ;; We recognize extra escape sequences.
808 (state (widget-get widget :custom-state))
809 (symbol (widget-get widget :value)))
810 (cond ((eq escape ?s)
811 (setq child (widget-create-child-and-convert
815 (if (eq state 'hidden) "show" "hide"))))
817 (custom-format-handler widget escape)))
820 :buttons (cons child (widget-get widget :buttons))))))
822 (defun custom-face-value-create (widget)
823 ;; Create a list of the display specifications.
824 (unless (eq (preceding-char) ?\n)
826 (when (not (eq (widget-get widget :custom-state) 'hidden))
827 (let* ((symbol (widget-value widget))
828 (edit (widget-create-child-and-convert
829 widget 'editable-list
830 :entry-format "%i %d %v"
831 :value (or (get symbol 'saved-face)
832 (get symbol 'factory-face))
834 custom-display custom-face-edit))))
835 (custom-face-state-set widget)
836 (widget-put widget :children (list edit)))))
838 (defvar custom-face-menu
839 '(("Apply" . custom-face-apply)
840 ("Set Default" . custom-face-set-default)
841 ("Reset to Default" . custom-face-default)
842 ("Reset to Factory Setting" . custom-face-factory))
843 "Alist of actions for the `custom-face' widget.
844 The key is a string containing the name of the action, the value is a
845 lisp function taking the widget as an element which will be called
846 when the action is chosen.")
848 (defun custom-face-state-set (widget)
849 "Set the state of WIDGET."
850 (let ((symbol (widget-value widget)))
851 (widget-put widget :custom-state (cond ((get symbol 'saved-face)
853 ((get symbol 'factory-face)
858 (defun custom-face-action (widget &optional event)
859 "Show the menu for `custom-face' WIDGET.
860 Optional EVENT is the location for the menu."
861 (when (eq (widget-get widget :custom-state) 'hidden)
862 (error "You cannot edit a hidden face"))
863 (let* ((completion-ignore-case t)
864 (symbol (widget-get widget :value))
865 (answer (widget-choose (symbol-name symbol) custom-face-menu event)))
867 (funcall answer widget))))
869 (defun custom-face-apply (widget)
870 "Make the face attributes in WIDGET take effect."
871 (let* ((symbol (widget-value widget))
872 (child (car (widget-get widget :children)))
873 (value (widget-value child)))
874 (custom-face-display-set symbol value)
875 (custom-face-state-set widget)
876 (custom-redraw-magic widget)))
878 (defun custom-face-set-default (widget)
879 "Make the face attributes in WIDGET default."
880 (let* ((symbol (widget-value widget))
881 (child (car (widget-get widget :children)))
882 (value (widget-value child)))
883 (custom-face-display-set symbol value)
884 (put symbol 'saved-face value)
885 (custom-face-state-set widget)
886 (custom-redraw-magic widget)))
888 (defun custom-face-default (widget)
889 "Restore WIDGET to the face's default attributes."
890 (let* ((symbol (widget-value widget))
891 (child (car (widget-get widget :children)))
892 (value (get symbol 'saved-face)))
894 (error "No saved value for this face")
895 (custom-face-display-set symbol value)
896 (widget-value-set child value)
897 (custom-face-state-set widget)
898 (custom-redraw-magic widget))))
900 (defun custom-face-factory (widget)
901 "Restore WIDGET to the face's factory settings."
902 (let* ((symbol (widget-value widget))
903 (child (car (widget-get widget :children)))
904 (value (get symbol 'factory-face)))
906 (error "No factory default for this face"))
907 (when (get symbol 'saved-face)
908 (put symbol 'saved-face nil))
909 (custom-face-display-set symbol value)
910 (widget-value-set child value)
911 (custom-face-state-set widget)
912 (custom-redraw-magic widget)))
914 ;;; The `face' Widget.
916 (define-widget 'face 'default
917 "Select and customize a face."
918 :convert-widget 'widget-item-convert-widget
922 :value-create 'widget-face-value-create
923 :value-delete 'widget-face-value-delete
924 :value-get 'widget-item-value-get
925 :validate 'widget-editable-list-validate
926 :action 'widget-face-action
927 :match '(lambda (widget value) (symbolp value)))
929 (defun widget-face-value-create (widget)
930 ;; Create a `custom-face' child.
931 (let* ((symbol (widget-value widget))
932 (child (widget-create-child-and-convert
934 :format "%t %s%m %h%v"
937 (custom-magic-reset child)
938 (setq custom-options (cons child custom-options))
939 (widget-put widget :children (list child))))
941 (defun widget-face-value-delete (widget)
942 ;; Remove the child from the options.
943 (let ((child (car (widget-get widget :children))))
944 (setq custom-options (delq child custom-options))
945 (widget-children-value-delete widget)))
947 (defvar face-history nil
948 "History of entered face names.")
950 (defun widget-face-action (widget &optional event)
952 (let ((answer (completing-read "Face: "
953 (mapcar (lambda (face)
954 (list (symbol-name face)))
958 (unless (zerop (length answer))
959 (widget-value-set widget (intern answer))
960 (widget-apply widget :notify widget event)
963 ;;; The `hook' Widget.
965 (define-widget 'hook 'list
967 :convert-widget 'custom-hook-convert-widget
970 (defun custom-hook-convert-widget (widget)
971 ;; Handle `:custom-options'.
972 (let* ((options (widget-get widget :options))
973 (other `(editable-list :inline t (function :format "%v")))
975 (list `(checklist :inline t
976 ,@(mapcar (lambda (entry)
977 `(function-item ,entry))
981 (widget-put widget :args args)
984 ;;; The `custom-group' Widget.
986 (define-widget 'custom-group 'custom
988 :format "%l%[%t%]: %L\n%m %h%a%v"
989 :documentation-property 'group-documentation
990 :help-echo "Push me to set or reset all members of this group."
991 :value-create 'custom-group-value-create
992 :action 'custom-group-action
993 :custom-apply 'custom-group-apply
994 :custom-set-default 'custom-group-set-default
995 :custom-reset 'custom-group-reset)
997 (defun custom-group-value-create (widget)
998 (let* ((state (widget-get widget :custom-state))
999 (level (widget-get widget :custom-level))
1000 (symbol (widget-value widget))
1001 (members (get symbol 'custom-group)))
1002 (unless (eq state 'hidden)
1003 (let* ((children (mapcar (lambda (entry)
1004 (widget-insert "\n")
1006 (widget-create-child-and-convert
1007 widget (nth 1 entry)
1009 :custom-level (1+ level)
1010 :value (nth 0 entry))
1011 (unless (eq (preceding-char) ?\n)
1012 (widget-insert "\n"))))
1014 (mapcar 'custom-magic-reset children)
1015 (widget-put widget :children children)
1016 (custom-group-state-update widget)))))
1018 (defvar custom-group-menu
1019 '(("Apply" . custom-group-apply)
1020 ("Set Default" . custom-group-set-default)
1021 ("Reset" . custom-group-reset))
1022 "Alist of actions for the `custom-group' widget.
1023 The key is a string containing the name of the action, the value is a
1024 lisp function taking the widget as an element which will be called
1025 when the action is chosen.")
1027 (defun custom-group-action (widget &optional event)
1028 "Show the menu for `custom-group' WIDGET.
1029 Optional EVENT is the location for the menu."
1030 (let* ((completion-ignore-case t)
1031 (answer (widget-choose (symbol-name (widget-get widget :value))
1035 (funcall answer widget))))
1037 (defun custom-group-apply (widget)
1038 "Apply changes in all modified group members."
1039 (let ((children (widget-get widget :children)))
1040 (mapcar (lambda (child)
1041 (when (eq (widget-get child :custom-state) 'modified)
1042 (widget-apply child :custom-apply)))
1045 (defun custom-group-set-default (widget)
1046 "Set default in all modified group members."
1047 (let ((children (widget-get widget :children)))
1048 (mapcar (lambda (child)
1049 (when (eq (widget-get child :custom-state) 'modified)
1050 (widget-apply child :custom-set-default)))
1053 (defun custom-group-reset (widget)
1054 "Reset all modified group members."
1055 (let ((children (widget-get widget :children)))
1056 (mapcar (lambda (child)
1057 (when (eq (widget-get child :custom-state) 'modified)
1058 (widget-apply child :custom-reset)))
1061 (defun custom-group-state-update (widget)
1063 (unless (eq (widget-get widget :custom-state) 'hidden)
1064 (let* ((children (widget-get widget :children))
1065 (states (mapcar (lambda (child)
1066 (widget-get child :custom-state))
1068 (magics custom-magic-alist)
1071 (let ((magic (car (car magics))))
1072 (if (and (not (eq magic 'hidden))
1073 (memq magic states))
1076 (setq magics (cdr magics)))))
1077 (widget-put widget :custom-state found)))
1078 (custom-magic-reset widget))
1080 ;;; The `custom-save' Command.
1082 (defcustom custom-file "~/.emacs"
1083 "File used for storing customization information.
1084 If you change this from the default \"~/.emacs\" you need to
1085 explicitly load that file for the settings to take effect."
1089 (defun custom-save-delete (symbol)
1090 "Delete the call to SYMBOL form `custom-file'.
1091 Leave point at the location of the call, or after the last expression."
1092 (set-buffer (find-file-noselect custom-file))
1093 (goto-char (point-min))
1096 (let ((sexp (condition-case nil
1097 (read (current-buffer))
1098 (end-of-file (throw 'found nil)))))
1099 (when (and (listp sexp)
1100 (eq (car sexp) symbol))
1101 (delete-region (save-excursion
1105 (throw 'found nil))))))
1107 (defun custom-save-variables ()
1108 "Save all customized variables in `custom-file'."
1110 (custom-save-delete 'custom-set-variables)
1111 (let ((standard-output (current-buffer)))
1114 (princ "(custom-set-variables")
1115 (mapatoms (lambda (symbol)
1116 (let ((value (get symbol 'saved-value)))
1122 (if (or (get symbol 'factory-value)
1123 (and (not (boundp symbol))
1124 (not (get symbol 'force-value))))
1131 (defun custom-save-faces ()
1132 "Save all customized faces in `custom-file'."
1134 (custom-save-delete 'custom-set-faces)
1135 (let ((standard-output (current-buffer)))
1138 (princ "(custom-set-faces")
1139 (mapatoms (lambda (symbol)
1140 (let ((value (get symbol 'saved-face)))
1146 (if (or (get symbol 'factory-face)
1147 (and (not (custom-facep symbol))
1148 (not (get symbol 'force-face))))
1155 (defun custom-save ()
1156 "Save all customizations in `custom-file'."
1158 (custom-save-variables)
1160 (setq custom-save-needed-p nil)
1162 (set-buffer (find-file-noselect custom-file))
1167 (provide 'custom-edit)
1169 ;; custom-edit.el ends here