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