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-menu :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)))
53 (defvar custom-prefix-list nil
54 "List of prefixes that should be ignored by `custom-unlispify'")
56 (defcustom custom-unlispify-menu-entries t
57 "Display menu entries as words instead of symbols if non nil."
61 (defun custom-unlispify-menu-entry (symbol &optional no-suffix)
62 "Convert symbol into a menu entry."
63 (cond ((not custom-unlispify-menu-entries)
65 ((get symbol 'custom-tag)
67 (get symbol 'custom-tag)
68 (concat (get symbol 'custom-tag) "...")))
71 (set-buffer (get-buffer-create " *Custom-Work*"))
73 (princ symbol (current-buffer))
74 (goto-char (point-min))
75 (let ((prefixes custom-prefix-list)
78 (setq prefix (car prefixes))
79 (if (search-forward prefix (+ (point) (length prefix)) t)
82 (delete-region (point-min) (point)))
83 (setq prefixes (cdr prefixes)))))
84 (subst-char-in-region (point-min) (point-max) ?- ?\ t)
85 (capitalize-region (point-min) (point-max))
87 (goto-char (point-max))
93 (defvar custom-options nil
94 "Customization widgets in the current buffer.")
96 (defvar custom-mode-map nil
97 "Keymap for `custom-mode'.")
99 (unless custom-mode-map
100 (setq custom-mode-map (make-sparse-keymap))
101 (set-keymap-parent custom-mode-map widget-keymap))
103 (easy-menu-define custom-mode-menu
105 "Menu used in customization buffers."
107 ["Apply" custom-apply t]
108 ["Set Default" custom-set-default t]
109 ["Reset" custom-reset t]
110 ["Save" custom-save t]))
112 (defcustom custom-mode-hook nil
113 "Hook called when entering custom-mode."
117 (defun custom-mode ()
118 "Major mode for editing customization buffers.
120 The following commands are available:
122 \\[widget-forward] Move to next button or editable field.
123 \\[widget-backward] Move to previous button or editable field.
124 \\[widget-button-click] Activate button under the mouse pointer.
125 \\[widget-button-press] Activate button under point.
126 \\[custom-apply] Apply all modifications.
127 \\[custom-set-default] Make all modifications default.
128 \\[custom-reset] Undo all modifications.
129 \\[custom-save] Save defaults for future emacs sessions.
131 Entry to this mode calls the value of `custom-mode-hook'
132 if that value is non-nil."
133 (kill-all-local-variables)
134 (setq major-mode 'custom-mode
136 (use-local-map custom-mode-map)
137 (make-local-variable 'custom-options)
138 (run-hooks 'custom-mode-hook))
140 ;;; Custom Mode Commands.
142 (defun custom-apply ()
143 "Apply changes in all modified options."
145 (let ((children custom-options))
146 (mapcar (lambda (child)
147 (when (eq (widget-get child :custom-state) 'modified)
148 (widget-apply child :custom-apply)))
151 (defun custom-set-default ()
152 "Set default in all modified group members."
154 (let ((children custom-options))
155 (mapcar (lambda (child)
156 (when (eq (widget-get child :custom-state) 'modified)
157 (widget-apply child :custom-set-default)))
160 (defun custom-reset ()
161 "Reset all modified group members."
163 (let ((children custom-options))
164 (mapcar (lambda (child)
165 (when (eq (widget-get child :custom-state) 'modified)
166 (widget-apply child :custom-reset)))
169 ;;; The Customize Commands
172 (defun customize (symbol)
173 "Customize SYMBOL, which must be a customization group."
174 (interactive (list (completing-read "Customize group: (default emacs) "
177 (get symbol 'custom-group))
180 (when (stringp symbol)
181 (if (string-equal "" symbol)
183 (setq symbol (intern symbol))))
184 (custom-buffer-create (list (list symbol 'custom-group))))
187 (defun customize-variable (symbol)
188 "Customize SYMBOL, which must be a variable."
190 ;; Code stolen from `help.el'.
191 (let ((v (variable-at-point))
192 (enable-recursive-minibuffers t)
194 (setq val (completing-read
196 (format "Customize variable (default %s): " v)
197 "Customize variable: ")
199 (list (if (equal val "")
201 (custom-buffer-create (list (list symbol 'custom-variable))))
204 (defun customize-face (symbol)
206 (interactive (list (completing-read "Customize face: "
207 obarray 'custom-facep)))
209 (setq symbol (intern symbol)))
210 (unless (symbolp symbol)
211 (error "Should be a symbol %S" symbol))
212 (custom-buffer-create (list (list symbol 'custom-face))))
215 (defun customize-customized ()
216 "Customize all already customized user options."
219 (mapatoms (lambda (symbol)
220 (and (get symbol 'saved-face)
221 (custom-facep symbol)
222 (setq found (cons (list symbol 'custom-face) found)))
223 (and (get symbol 'saved-value)
226 (cons (list symbol 'custom-variable) found)))))
228 (custom-buffer-create found)
229 (error "No customized user options"))))
232 (defun customize-apropos (regexp &optional all)
233 "Customize all user options matching REGEXP.
234 If ALL (e.g., started with a prefix key), include options which are not
236 (interactive "sCustomize regexp: \nP")
238 (mapatoms (lambda (symbol)
239 (when (string-match regexp (symbol-name symbol))
240 (when (get symbol 'custom-group)
241 (setq found (cons (list symbol 'custom-group) found)))
242 (when (custom-facep symbol)
243 (setq found (cons (list symbol 'custom-face) found)))
244 (when (and (boundp symbol)
245 (or (get symbol 'saved-value)
246 (get symbol 'factory-value)
248 (get symbol 'variable-documentation)
249 (user-variable-p symbol))))
251 (cons (list symbol 'custom-variable) found))))))
253 (custom-buffer-create found)
254 (error "No matches"))))
257 (defun custom-buffer-create (options)
258 "Create a buffer containing OPTIONS.
259 OPTIONS should be an alist of the form ((SYMBOL WIDGET)...), where
260 SYMBOL is a customization option, and WIDGET is a widget for editing
262 (kill-buffer (get-buffer-create "*Customization*"))
263 (switch-to-buffer (get-buffer-create "*Customization*"))
265 (widget-insert "This is a customization buffer.
266 Push RET or click mouse-2 on the word ")
267 (widget-create 'info-link
269 :help-echo "Push me for help."
270 "(custom)The Customization Buffer")
271 (widget-insert " for more information.\n\n")
273 (mapcar (lambda (entry)
275 (if (> (length options) 1)
276 (widget-create (nth 1 entry)
277 :value (nth 0 entry))
278 ;; If there is only one entry, don't hide it!
279 (widget-create (nth 1 entry)
280 :custom-state 'unknown
281 :value (nth 0 entry)))
282 (unless (eq (preceding-char) ?\n)
283 (widget-insert "\n"))
284 (widget-insert "\n")))
286 (mapcar 'custom-magic-reset custom-options)
287 (widget-create 'push-button
289 :help-echo "Push me to apply all modifications."
290 :action (lambda (widget &optional event)
293 (widget-create 'push-button
295 :help-echo "Push me to make the modifications default."
296 :action (lambda (widget &optional event)
297 (custom-set-default)))
299 (widget-create 'push-button
301 :help-echo "Push me to undo all modifications.."
302 :action (lambda (widget &optional event)
305 (widget-create 'push-button
307 :help-echo "Push me to store the new defaults permanently."
308 :action (lambda (widget &optional event)
313 ;;; Modification of Basic Widgets.
315 ;; We add extra properties to the basic widgets needed here. This is
316 ;; fine, as long as we are careful to stay within out own namespace.
318 ;; We want simple widgets to be displayed by default, but complex
319 ;; widgets to be hidden.
321 (widget-put (get 'item 'widget-type) :custom-show t)
322 (widget-put (get 'editable-field 'widget-type)
323 :custom-show (lambda (widget value)
324 (let ((pp (pp-to-string value)))
325 (cond ((string-match "\n" pp)
330 (widget-put (get 'menu-choice 'widget-type) :custom-show t)
332 ;;; The `custom-manual' Widget.
334 (define-widget 'custom-manual 'info-link
335 "Link to the manual entry for this customization option."
338 ;;; The `custom-magic' Widget.
340 (define-widget 'custom-magic 'item
341 "Status feedback for customization option."
343 :action 'widget-choice-item-action
344 :value-create 'custom-magic-value-create)
346 (defface custom-invalid-face '((((class color))
347 (:foreground "yellow" :background "red"))
349 (:bold t :italic t :underline t)))
350 "Face used when the customize item is invalid.")
352 (defface custom-rogue-face '((((class color))
353 (:foreground "pink" :background "black"))
356 "Face used when the customize item is not defined for customization.")
358 (defface custom-modified-face '((((class color))
359 (:foreground "white" :background "blue"))
362 "Face used when the customize item has been modified.")
364 (defface custom-applied-face '((((class color))
365 (:foreground "blue" :background "white"))
368 "Face used when the customize item has been applied.")
370 (defface custom-saved-face '((t (:underline t)))
371 "Face used when the customize item has been saved.")
373 (defcustom custom-magic-alist '((nil "#" underline)
376 (invalid "x" custom-invalid-face)
377 (modified "*" custom-modified-face)
378 (applied "+" custom-applied-face)
379 (saved "!" custom-saved-face)
380 (rogue "@" custom-rogue-face)
382 "Alist of magic representing a customize items status.
383 Each entry is of the form (STATE MAGIC FACE), where
385 STATE is one of the following symbols:
388 For internal use, should never occur.
390 For internal use, should never occur.
392 This item is not being displayed.
394 This item is modified, but has an invalid form.
396 This item is modified, and has a valid form.
398 This items current value has been changed temporarily.
400 This item is marked for saving.
402 This item has no customization information.
404 This item is unchanged from the factory default.
406 MAGIC is a string used to present that state.
408 FACE is a face used to present the state.
410 The list should be sorted most significant first."
411 :type '(repeat (list (choice (const nil)
423 (defun custom-magic-value-create (widget)
424 ;; Create compact status report for WIDGET.
425 (let* ((parent (widget-get widget :parent))
426 (state (widget-get parent :custom-state))
427 (entry (assq state custom-magic-alist))
428 (magic (nth 1 entry))
429 (face (nth 2 entry)))
430 (if (eq (widget-get parent :custom-form) 'lisp)
431 (widget-insert "(" magic ")")
432 (widget-insert "[" magic "]"))
433 (widget-put widget :button-face face)))
435 (defun custom-magic-reset (widget)
436 "Redraw the :custom-magic property of WIDGET."
437 (let ((magic (widget-get widget :custom-magic)))
438 (widget-value-set magic (widget-value magic))))
440 ;;; The `custom-level' Widget.
442 (define-widget 'custom-level 'item
443 "The custom level buttons."
445 :help-echo "Push me to expand or collapse this item."
446 :action 'custom-level-action)
448 (defun custom-level-action (widget &optional event)
449 "Toggle visibility for parent to WIDGET."
450 (let* ((parent (widget-get widget :parent))
451 (state (widget-get parent :custom-state)))
452 (cond ((memq state '(invalid modified))
453 (error "There are unapplied changes"))
455 (widget-put parent :custom-state 'unknown))
457 (widget-put parent :custom-state 'hidden)))
458 (custom-redraw parent)))
460 ;;; The `custom' Widget.
462 (defvar custom-save-needed-p nil
463 "Non-nil if any customizations need to be saved.")
465 (add-hook 'kill-emacs-hook 'custom-save-maybe)
467 (defun custom-save-maybe ()
468 (and custom-save-needed-p
469 (y-or-n-p "You have unsaved customizations, save them now? ")
472 (define-widget 'custom 'default
473 "Customize a user option."
474 :convert-widget 'widget-item-convert-widget
475 :format "%l%[%t%]: %v%m %h%a"
476 :format-handler 'custom-format-handler
477 :notify 'custom-notify
479 :custom-state 'hidden
480 :documentation-property 'widget-subclass-responsibility
481 :value-create 'widget-subclass-responsibility
482 :value-delete 'widget-children-value-delete
483 :value-get 'widget-item-value-get
484 :validate 'widget-editable-list-validate
485 :match (lambda (widget value) (symbolp value)))
487 (defun custom-format-handler (widget escape)
488 ;; We recognize extra escape sequences.
489 (let* ((buttons (widget-get widget :buttons))
490 (state (widget-get widget :custom-state))
491 (level (widget-get widget :custom-level)))
492 (cond ((eq escape ?l)
494 (push (widget-create-child-and-convert
495 widget 'custom-level (make-string level ?*))
498 (widget-put widget :buttons buttons)))
500 (push (widget-create-child-and-convert
503 (if (eq state 'hidden) "show" "hide"))
506 (widget-put widget :buttons buttons))
508 (and (eq (preceding-char) ?\n)
509 (widget-get widget :indent)
510 (insert-char ? (widget-get widget :indent)))
511 (let ((magic (widget-create-child-and-convert
512 widget 'custom-magic nil)))
513 (widget-put widget :custom-magic magic)
515 (widget-put widget :buttons buttons)))
517 (let* ((symbol (widget-get widget :value))
518 (links (get symbol 'custom-links))
519 (many (> (length links) 2)))
521 (and (eq (preceding-char) ?\n)
522 (widget-get widget :indent)
523 (insert-char ? (widget-get widget :indent)))
526 (push (widget-create-child-and-convert widget (car links))
528 (setq links (cdr links))
537 (widget-put widget :buttons buttons))))
539 (widget-default-format-handler widget escape)))))
541 (defun custom-notify (widget &rest args)
542 "Keep track of changes."
543 (widget-put widget :custom-state 'modified)
544 (let ((buffer-undo-list t))
545 (custom-magic-reset widget))
546 (apply 'widget-default-notify widget args))
548 (defun custom-redraw (widget)
549 "Redraw WIDGET with current settings."
550 (widget-value-set widget (widget-value widget))
551 (custom-redraw-magic widget))
553 (defun custom-redraw-magic (widget)
554 "Redraw WIDGET state with current settings."
556 (let ((magic (widget-get widget :custom-magic)))
559 (widget-value-set magic (widget-value magic))
560 (when (setq widget (widget-get widget :group))
561 (custom-group-state-update widget))))
564 (defun custom-show (widget value)
565 "Non-nil if WIDGET should be shown with VALUE by default."
566 (let ((show (widget-get widget :custom-show)))
572 (funcall show widget value)))))
574 (defun custom-load-symbol (symbol)
575 "Load all dependencies for SYMBOL."
576 (let ((loads (get symbol 'custom-loads))
579 (setq load (car loads)
581 (cond ((symbolp load)
583 ((member load load-history))
585 (load-library load))))))
587 (defun custom-load-widget (widget)
588 "Load all dependencies for WIDGET."
589 (custom-load-symbol (widget-value widget)))
591 ;;; The `custom-variable' Widget.
593 (define-widget 'custom-variable 'custom
594 "Customize variable."
595 :format "%l%v%m %h%a"
596 :help-echo "Push me to set or reset this variable."
597 :documentation-property 'variable-documentation
599 :custom-menu 'custom-variable-menu-create
601 :value-create 'custom-variable-value-create
602 :action 'custom-variable-action
603 :custom-apply 'custom-variable-apply
604 :custom-set-default 'custom-variable-set-default
605 :custom-reset 'custom-redraw)
607 (defun custom-variable-value-create (widget)
608 "Here is where you edit the variables value."
609 (custom-load-widget widget)
610 (let* ((buttons (widget-get widget :buttons))
611 (children (widget-get widget :children))
612 (form (widget-get widget :custom-form))
613 (state (widget-get widget :custom-state))
614 (symbol (widget-get widget :value))
615 (options (get symbol 'custom-options))
616 (child-type (or (get symbol 'custom-type) 'sexp))
617 (type (let ((tmp (if (listp child-type)
618 (copy-list child-type)
621 (widget-put tmp :options options))
623 (conv (widget-convert type))
624 (value (if (default-boundp symbol)
625 (default-value symbol)
626 (widget-get conv :value))))
627 ;; If the widget is new, the child determine whether it is hidden.
629 ((custom-show type value)
630 (setq state 'unknown))
632 (setq state 'hidden)))
633 ;; If we don't know the state, see if we need to edit it in lisp form.
634 (when (eq state 'unknown)
635 (unless (widget-apply conv :match value)
636 ;; (widget-apply (widget-convert type) :match value)
638 ;; Now we can create the child widget.
639 (cond ((eq state 'hidden)
640 ;; Make hidden value easy to show.
641 (push (widget-create-child-and-convert
643 :tag (symbol-name symbol)
644 :format "%t: %[show%]")
647 ;; In lisp mode edit the saved value when possible.
648 (let* ((value (cond ((get symbol 'saved-value)
649 (car (get symbol 'saved-value)))
650 ((get symbol 'factory-value)
651 (car (get symbol 'factory-value)))
652 ((default-boundp symbol)
653 (custom-quote (default-value symbol)))
655 (custom-quote (widget-get conv :value))))))
656 (push (widget-create-child-and-convert widget 'sexp
657 :tag (symbol-name symbol)
664 (push (widget-create-child-and-convert widget type
665 :tag (symbol-name symbol)
668 ;; Now update the state.
669 (unless (eq (preceding-char) ?\n)
670 (widget-insert "\n"))
671 (if (eq state 'hidden)
672 (widget-put widget :custom-state state)
673 (custom-variable-state-set widget))
674 (widget-put widget :custom-form form)
675 (widget-put widget :buttons buttons)
676 (widget-put widget :children children)))
678 (defun custom-variable-state-set (widget)
679 "Set the state of WIDGET."
680 (let* ((symbol (widget-value widget))
681 (value (if (default-boundp symbol)
682 (default-value symbol)
683 (widget-get widget :value)))
684 (state (if (get symbol 'saved-value)
685 (if (condition-case nil
687 (eval (car (get symbol 'saved-value))))
691 (if (get symbol 'factory-value)
692 (if (condition-case nil
694 (eval (car (get symbol 'factory-value))))
699 (widget-put widget :custom-state state)))
701 (defvar custom-variable-menu
702 '(("Edit" . custom-variable-edit)
703 ("Edit Default" . custom-variable-edit-lisp)
704 ("Apply" . custom-variable-apply)
705 ("Set Default" . custom-variable-set-default)
706 ("Reset" . custom-redraw)
707 ("Reset to Default" . custom-variable-default)
708 ("Reset to Factory Settings" . custom-variable-factory))
709 "Alist of actions for the `custom-variable' widget.
710 The key is a string containing the name of the action, the value is a
711 lisp function taking the widget as an element which will be called
712 when the action is chosen.")
714 (defun custom-variable-action (widget &optional event)
715 "Show the menu for `custom-variable' WIDGET.
716 Optional EVENT is the location for the menu."
717 (let* ((completion-ignore-case t)
718 (answer (widget-choose (symbol-name (widget-get widget :value))
722 (funcall answer widget))))
724 (defun custom-variable-edit (widget)
725 "Edit value of WIDGET."
726 (widget-put widget :custom-state 'unknown)
727 (widget-put widget :custom-form 'edit)
728 (custom-redraw widget))
730 (defun custom-variable-edit-lisp (widget)
731 "Edit the lisp representation of the value of WIDGET."
732 (widget-put widget :custom-state 'unknown)
733 (widget-put widget :custom-form 'lisp)
734 (custom-redraw widget))
736 (defun custom-variable-apply (widget)
737 "Set the current value for the variable being edited by WIDGET."
738 (let ((form (widget-get widget :custom-form))
739 (state (widget-get widget :custom-state))
740 (child (car (widget-get widget :children)))
741 (symbol (widget-value widget))
743 (cond ((eq state 'hidden)
744 (error "Cannot apply hidden variable."))
745 ((setq val (widget-apply child :validate))
746 (error "Invalid %S" val))
748 (set symbol (eval (widget-value child))))
750 (set symbol (widget-value child))))
751 (custom-variable-state-set widget)
752 (custom-redraw-magic widget)))
754 (defun custom-variable-set-default (widget)
755 "Set the default value for the variable being edited by WIDGET."
756 (let ((form (widget-get widget :custom-form))
757 (state (widget-get widget :custom-state))
758 (child (car (widget-get widget :children)))
759 (symbol (widget-value widget))
761 (cond ((eq state 'hidden)
762 (error "Cannot apply hidden variable."))
763 ((setq val (widget-apply child :validate))
764 (error "Invalid %S" val))
766 (setq custom-save-needed-p (cons symbol custom-save-needed-p))
767 (put symbol 'saved-value (list (widget-value child)))
768 (set symbol (eval (widget-value child))))
770 (setq custom-save-needed-p (cons symbol custom-save-needed-p))
772 'saved-value (list (custom-quote (widget-value
774 (set symbol (widget-value child))))
775 (custom-variable-state-set widget)
776 (custom-redraw-magic widget)))
778 (defun custom-variable-default (widget)
779 "Restore the default value for the variable being edited by WIDGET."
780 (let ((symbol (widget-value widget)))
781 (if (get symbol 'saved-value)
783 (set symbol (eval (car (get symbol 'saved-value))))
785 (error "No default value for %s" symbol))
786 (widget-put widget :custom-state 'unknown)
787 (custom-redraw widget)))
789 (defun custom-variable-factory (widget)
790 "Restore the factory setting for the variable being edited by WIDGET."
791 (let ((symbol (widget-value widget)))
792 (if (get symbol 'factory-value)
793 (set symbol (eval (car (get symbol 'factory-value))))
794 (error "No factory default for %S" symbol))
795 (when (get symbol 'saved-value)
796 (setq custom-save-needed-p (cons symbol custom-save-needed-p))
797 (put symbol 'saved-value nil))
798 (widget-put widget :custom-state 'unknown)
799 (custom-redraw widget)))
801 ;;; The `custom-face-edit' Widget.
803 (defvar custom-face-edit-args
804 (mapcar (lambda (att)
807 (list 'const :format "" :value (nth 0 att))
809 custom-face-attributes))
811 (define-widget 'custom-face-edit 'checklist
812 "Edit face attributes."
816 :args (mapcar (lambda (att)
819 (list 'const :format "" :value (nth 0 att))
821 custom-face-attributes))
823 ;;; The `custom-display' Widget.
825 (define-widget 'custom-display 'menu-choice
826 "Select a display type."
829 :args '((const :tag "all" t)
832 :args ((group (const :format "Type: " type)
837 (const :format "TTY%n"
839 (group (const :format "Class: " class)
842 (const :format "Color "
847 (const :format "Monochrome%n"
849 (group (const :format "Background: " background)
852 (const :format "Light "
854 (const :format "Dark\n"
857 ;;; The `custom-face' Widget.
859 (define-widget 'custom-face 'custom
861 :format "%l%[%t%]: %s%m %h%a%v"
862 :format-handler 'custom-face-format-handler
863 :help-echo "Push me to set or reset this face."
864 :documentation-property 'face-documentation
865 :value-create 'custom-face-value-create
866 :action 'custom-face-action
867 :custom-apply 'custom-face-apply
868 :custom-set-default 'custom-face-set-default
869 :custom-reset 'custom-redraw
870 :custom-menu 'custom-face-menu-create)
872 (defun custom-face-format-handler (widget escape)
873 ;; We recognize extra escape sequences.
875 (state (widget-get widget :custom-state))
876 (symbol (widget-get widget :value)))
877 (cond ((eq escape ?s)
878 (and (string-match "XEmacs" emacs-version)
879 ;; XEmacs cannot display initialized faces.
880 (not (custom-facep symbol))
881 (copy-face 'custom-face-empty symbol))
882 (setq child (widget-create-child-and-convert
886 (if (eq state 'hidden) "show" "hide"))))
888 (custom-format-handler widget escape)))
891 :buttons (cons child (widget-get widget :buttons))))))
893 (defun custom-face-value-create (widget)
894 ;; Create a list of the display specifications.
895 (unless (eq (preceding-char) ?\n)
897 (when (not (eq (widget-get widget :custom-state) 'hidden))
898 (custom-load-widget widget)
899 (let* ((symbol (widget-value widget))
900 (edit (widget-create-child-and-convert
901 widget 'editable-list
902 :entry-format "%i %d %v"
903 :value (or (get symbol 'saved-face)
904 (get symbol 'factory-face))
906 custom-display custom-face-edit))))
907 (custom-face-state-set widget)
908 (widget-put widget :children (list edit)))))
910 (defvar custom-face-menu
911 '(("Apply" . custom-face-apply)
912 ("Set Default" . custom-face-set-default)
913 ("Reset to Default" . custom-face-default)
914 ("Reset to Factory Setting" . custom-face-factory))
915 "Alist of actions for the `custom-face' widget.
916 The key is a string containing the name of the action, the value is a
917 lisp function taking the widget as an element which will be called
918 when the action is chosen.")
920 (defun custom-face-state-set (widget)
921 "Set the state of WIDGET."
922 (let ((symbol (widget-value widget)))
923 (widget-put widget :custom-state (cond ((get symbol 'saved-face)
925 ((get symbol 'factory-face)
930 (defun custom-face-action (widget &optional event)
931 "Show the menu for `custom-face' WIDGET.
932 Optional EVENT is the location for the menu."
933 (when (eq (widget-get widget :custom-state) 'hidden)
934 (error "You cannot edit a hidden face"))
935 (let* ((completion-ignore-case t)
936 (symbol (widget-get widget :value))
937 (answer (widget-choose (symbol-name symbol) custom-face-menu event)))
939 (funcall answer widget))))
941 (defun custom-face-apply (widget)
942 "Make the face attributes in WIDGET take effect."
943 (let* ((symbol (widget-value widget))
944 (child (car (widget-get widget :children)))
945 (value (widget-value child)))
946 (custom-face-display-set symbol value)
947 (custom-face-state-set widget)
948 (custom-redraw-magic widget)))
950 (defun custom-face-set-default (widget)
951 "Make the face attributes in WIDGET default."
952 (let* ((symbol (widget-value widget))
953 (child (car (widget-get widget :children)))
954 (value (widget-value child)))
955 (custom-face-display-set symbol value)
956 (put symbol 'saved-face value)
957 (custom-face-state-set widget)
958 (custom-redraw-magic widget)))
960 (defun custom-face-default (widget)
961 "Restore WIDGET to the face's default attributes."
962 (let* ((symbol (widget-value widget))
963 (child (car (widget-get widget :children)))
964 (value (get symbol 'saved-face)))
966 (error "No saved value for this face")
967 (custom-face-display-set symbol value)
968 (widget-value-set child value)
969 (custom-face-state-set widget)
970 (custom-redraw-magic widget))))
972 (defun custom-face-factory (widget)
973 "Restore WIDGET to the face's factory settings."
974 (let* ((symbol (widget-value widget))
975 (child (car (widget-get widget :children)))
976 (value (get symbol 'factory-face)))
978 (error "No factory default for this face"))
979 (when (get symbol 'saved-face)
980 (put symbol 'saved-face nil))
981 (custom-face-display-set symbol value)
982 (widget-value-set child value)
983 (custom-face-state-set widget)
984 (custom-redraw-magic widget)))
986 ;;; The `face' Widget.
988 (define-widget 'face 'default
989 "Select and customize a face."
990 :convert-widget 'widget-item-convert-widget
994 :value-create 'widget-face-value-create
995 :value-delete 'widget-face-value-delete
996 :value-get 'widget-item-value-get
997 :validate 'widget-editable-list-validate
998 :action 'widget-face-action
999 :match '(lambda (widget value) (symbolp value)))
1001 (defun widget-face-value-create (widget)
1002 ;; Create a `custom-face' child.
1003 (let* ((symbol (widget-value widget))
1004 (child (widget-create-child-and-convert
1006 :format "%t %s%m %h%v"
1009 (custom-magic-reset child)
1010 (setq custom-options (cons child custom-options))
1011 (widget-put widget :children (list child))))
1013 (defun widget-face-value-delete (widget)
1014 ;; Remove the child from the options.
1015 (let ((child (car (widget-get widget :children))))
1016 (setq custom-options (delq child custom-options))
1017 (widget-children-value-delete widget)))
1019 (defvar face-history nil
1020 "History of entered face names.")
1022 (defun widget-face-action (widget &optional event)
1023 "Prompt for a face."
1024 (let ((answer (completing-read "Face: "
1025 (mapcar (lambda (face)
1026 (list (symbol-name face)))
1030 (unless (zerop (length answer))
1031 (widget-value-set widget (intern answer))
1032 (widget-apply widget :notify widget event)
1035 ;;; The `hook' Widget.
1037 (define-widget 'hook 'list
1039 :convert-widget 'custom-hook-convert-widget
1042 (defun custom-hook-convert-widget (widget)
1043 ;; Handle `:custom-options'.
1044 (let* ((options (widget-get widget :options))
1045 (other `(editable-list :inline t
1046 :entry-format "%i %d%v"
1047 (function :format " %v")))
1049 (list `(checklist :inline t
1050 ,@(mapcar (lambda (entry)
1051 `(function-item ,entry))
1055 (widget-put widget :args args)
1058 ;;; The `custom-group' Widget.
1060 (define-widget 'custom-group 'custom
1062 :format "%l%[%t%]: %L\n%m %h%a%v"
1063 :documentation-property 'group-documentation
1064 :help-echo "Push me to set or reset all members of this group."
1065 :value-create 'custom-group-value-create
1066 :action 'custom-group-action
1067 :custom-apply 'custom-group-apply
1068 :custom-set-default 'custom-group-set-default
1069 :custom-reset 'custom-group-reset
1070 :custom-menu 'custom-group-menu-create)
1072 (defun custom-group-value-create (widget)
1073 (let ((state (widget-get widget :custom-state)))
1074 (unless (eq state 'hidden)
1075 (custom-load-widget widget)
1076 (let* ((level (widget-get widget :custom-level))
1077 (symbol (widget-value widget))
1078 (members (get symbol 'custom-group))
1079 (children (mapcar (lambda (entry)
1080 (widget-insert "\n")
1082 (widget-create-child-and-convert
1083 widget (nth 1 entry)
1085 :custom-level (1+ level)
1086 :value (nth 0 entry))
1087 (unless (eq (preceding-char) ?\n)
1088 (widget-insert "\n"))))
1090 (mapcar 'custom-magic-reset children)
1091 (widget-put widget :children children)
1092 (custom-group-state-update widget)))))
1094 (defvar custom-group-menu
1095 '(("Apply" . custom-group-apply)
1096 ("Set Default" . custom-group-set-default)
1097 ("Reset" . custom-group-reset))
1098 "Alist of actions for the `custom-group' widget.
1099 The key is a string containing the name of the action, the value is a
1100 lisp function taking the widget as an element which will be called
1101 when the action is chosen.")
1103 (defun custom-group-action (widget &optional event)
1104 "Show the menu for `custom-group' WIDGET.
1105 Optional EVENT is the location for the menu."
1106 (let* ((completion-ignore-case t)
1107 (answer (widget-choose (symbol-name (widget-get widget :value))
1111 (funcall answer widget))))
1113 (defun custom-group-apply (widget)
1114 "Apply changes in all modified group members."
1115 (let ((children (widget-get widget :children)))
1116 (mapcar (lambda (child)
1117 (when (eq (widget-get child :custom-state) 'modified)
1118 (widget-apply child :custom-apply)))
1121 (defun custom-group-set-default (widget)
1122 "Set default in all modified group members."
1123 (let ((children (widget-get widget :children)))
1124 (mapcar (lambda (child)
1125 (when (eq (widget-get child :custom-state) 'modified)
1126 (widget-apply child :custom-set-default)))
1129 (defun custom-group-reset (widget)
1130 "Reset all modified group members."
1131 (let ((children (widget-get widget :children)))
1132 (mapcar (lambda (child)
1133 (when (eq (widget-get child :custom-state) 'modified)
1134 (widget-apply child :custom-reset)))
1137 (defun custom-group-state-update (widget)
1139 (unless (eq (widget-get widget :custom-state) 'hidden)
1140 (let* ((children (widget-get widget :children))
1141 (states (mapcar (lambda (child)
1142 (widget-get child :custom-state))
1144 (magics custom-magic-alist)
1147 (let ((magic (car (car magics))))
1148 (if (and (not (eq magic 'hidden))
1149 (memq magic states))
1152 (setq magics (cdr magics)))))
1153 (widget-put widget :custom-state found)))
1154 (custom-magic-reset widget))
1156 ;;; The `custom-save' Command.
1158 (defcustom custom-file "~/.emacs"
1159 "File used for storing customization information.
1160 If you change this from the default \"~/.emacs\" you need to
1161 explicitly load that file for the settings to take effect."
1165 (defun custom-save-delete (symbol)
1166 "Delete the call to SYMBOL form `custom-file'.
1167 Leave point at the location of the call, or after the last expression."
1168 (set-buffer (find-file-noselect custom-file))
1169 (goto-char (point-min))
1172 (let ((sexp (condition-case nil
1173 (read (current-buffer))
1174 (end-of-file (throw 'found nil)))))
1175 (when (and (listp sexp)
1176 (eq (car sexp) symbol))
1177 (delete-region (save-excursion
1181 (throw 'found nil))))))
1183 (defun custom-save-variables ()
1184 "Save all customized variables in `custom-file'."
1186 (custom-save-delete 'custom-set-variables)
1187 (let ((standard-output (current-buffer)))
1190 (princ "(custom-set-variables")
1191 (mapatoms (lambda (symbol)
1192 (let ((value (get symbol 'saved-value)))
1198 (if (or (get symbol 'factory-value)
1199 (and (not (boundp symbol))
1200 (not (get symbol 'force-value))))
1207 (defun custom-save-faces ()
1208 "Save all customized faces in `custom-file'."
1210 (custom-save-delete 'custom-set-faces)
1211 (let ((standard-output (current-buffer)))
1214 (princ "(custom-set-faces")
1215 (mapatoms (lambda (symbol)
1216 (let ((value (get symbol 'saved-face)))
1222 (if (or (get symbol 'factory-face)
1223 (and (not (custom-facep symbol))
1224 (not (get symbol 'force-face))))
1231 (defun custom-save ()
1232 "Save all customizations in `custom-file'."
1234 (custom-save-variables)
1236 (setq custom-save-needed-p nil)
1238 (set-buffer (find-file-noselect custom-file))
1241 ;;; The Customize Menu.
1243 (defcustom custom-menu-nesting 2
1244 "Maximum nesting in custom menus."
1248 (defun custom-face-menu-create (widget symbol)
1249 "Ignoring WIDGET, create a menu entry for customization face SYMBOL."
1250 (vector (custom-unlispify-menu-entry symbol)
1251 `(custom-buffer-create '((,symbol custom-face)))
1254 (defun custom-variable-menu-create (widget symbol)
1255 "Ignoring WIDGET, create a menu entry for customization variable SYMBOL."
1256 (let ((type (get symbol 'custom-type)))
1257 (unless (listp type)
1258 (setq type (list type)))
1259 (if (and type (widget-get type :custom-menu))
1260 (widget-apply type :custom-menu symbol)
1261 (vector (custom-unlispify-menu-entry symbol)
1262 `(custom-buffer-create '((,symbol custom-variable)))
1265 (widget-put (get 'boolean 'widget-type)
1266 :custom-menu (lambda (widget symbol)
1267 (vector (custom-unlispify-menu-entry symbol)
1268 `(custom-buffer-create
1269 '((,symbol custom-variable)))
1271 ':selected symbol)))
1273 (defun custom-group-menu-create (widget symbol)
1274 "Ignoring WIDGET, create a menu entry for customization group SYMBOL."
1275 (custom-menu-create symbol))
1277 (defun custom-menu-create (symbol &optional name)
1278 "Create menu for customization group SYMBOL.
1279 If optional NAME is given, use that as the name of the menu.
1280 Otherwise make up a name from SYMBOL.
1281 The menu is in a format applicable to `easy-menu-define'."
1283 (setq name (custom-unlispify-menu-entry symbol)))
1284 (let ((item (vector name
1285 `(custom-buffer-create '((,symbol custom-group)))
1287 (if (> custom-menu-nesting 0)
1288 (let ((custom-menu-nesting (1- custom-menu-nesting))
1289 (custom-prefix-list (cons (or (get symbol 'custom-prefix)
1290 (concat (symbol-name symbol) "-"))
1291 custom-prefix-list)))
1292 (custom-load-symbol symbol)
1293 `(,(custom-unlispify-menu-entry symbol t)
1296 ,@(mapcar (lambda (entry)
1297 (widget-apply (if (listp (nth 1 entry))
1299 (list (nth 1 entry)))
1300 :custom-menu (nth 0 entry)))
1301 (get symbol 'custom-group))))
1305 (defun custom-menu-update ()
1306 "Update customize menu."
1308 (add-hook 'custom-define-hook 'custom-menu-reset)
1309 (let ((menu `(,(car custom-help-menu)
1310 ,(widget-apply '(custom-group) :custom-menu 'emacs)
1311 ,@(cdr (cdr custom-help-menu)))))
1312 (if (fboundp 'add-submenu)
1313 (add-submenu '("Help") menu)
1314 (define-key global-map [menu-bar help-menu customize-menu]
1315 (cons (car menu) (easy-menu-create-keymaps (car menu) (cdr menu)))))))
1320 (defun custom-make-dependencies ()
1321 "Batch function to extract custom dependencies from .el files.
1322 Usage: emacs -batch *.el -f custom-make-dependencies > deps.el"
1323 (let ((buffers (buffer-list)))
1325 (set-buffer (car buffers))
1326 (setq buffers (cdr buffers))
1327 (let ((file (buffer-file-name)))
1328 (when (and file (string-match "\\`\\(.*\\)\\.el\\'" file))
1329 (goto-char (point-min))
1331 (let ((name (file-name-nondirectory (match-string 1 file))))
1333 (let ((expr (read (current-buffer))))
1334 (when (and (listp expr)
1335 (memq (car expr) '(defcustom defface defgroup)))
1337 (put (nth 1 expr) 'custom-where name)))))
1339 (mapatoms (lambda (symbol)
1340 (let ((members (get symbol 'custom-group))
1345 (princ " 'custom-loads '(")
1347 (setq item (car (car members))
1348 members (cdr members)
1349 where (get item 'custom-where))
1350 (unless (or (null where)
1351 (member where found))
1355 (push where found)))
1360 (provide 'custom-edit)
1362 ;; custom-edit.el ends here