2005-09-24 Emilio C. Lopes <eclig@gmx.net>
[emacs.git] / lisp / cus-edit.el
blobeb6656a426d569ae57cb8db67b092dd402392965
1 ;;; cus-edit.el --- tools for customizing Emacs and Lisp packages
2 ;;
3 ;; Copyright (C) 1996, 1997, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005 Free Software Foundation, Inc.
5 ;;
6 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
7 ;; Maintainer: FSF
8 ;; Keywords: help, faces
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., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
27 ;;; Commentary:
29 ;; This file implements the code to create and edit customize buffers.
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 ;; The identity of a customize option is represented by a Lisp symbol.
38 ;; The following values are associated with an option.
40 ;; 0. The current value.
42 ;; This is the value of the option as seen by "the rest of Emacs".
44 ;; Usually extracted by 'default-value', but can be extracted with
45 ;; different means if the option symbol has the 'custom-get'
46 ;; property. Similarly, set-default (or the 'custom-set' property)
47 ;; can set it.
49 ;; 1. The widget value.
51 ;; This is the value shown in the widget in a customize buffer.
53 ;; 2. The customized value.
55 ;; This is the last value given to the option through customize.
57 ;; It is stored in the 'customized-value' property of the option, in a
58 ;; cons-cell whose car evaluates to the customized value.
60 ;; 3. The saved value.
62 ;; This is last value saved from customize.
64 ;; It is stored in the 'saved-value' property of the option, in a
65 ;; cons-cell whose car evaluates to the saved value.
67 ;; 4. The standard value.
69 ;; This is the value given in the 'defcustom' declaration.
71 ;; It is stored in the 'standard-value' property of the option, in a
72 ;; cons-cell whose car evaluates to the standard value.
74 ;; 5. The "think" value.
76 ;; This is what customize thinks the current value should be.
78 ;; This is the customized value, if any such value exists, otherwise
79 ;; the saved value, if that exists, and as a last resort the standard
80 ;; value.
82 ;; The reason for storing values unevaluated: This is so you can have
83 ;; values that depend on the environment. For example, you can have a
84 ;; variable that has one value when Emacs is running under a window
85 ;; system, and another value on a tty. Since the evaluation is only done
86 ;; when the variable is first initialized, this is only relevant for the
87 ;; saved (and standard) values, but affect others values for
88 ;; compatibility.
90 ;; You can see (and modify and save) this unevaluated value by selecting
91 ;; "Show initial Lisp expression" from the Lisp interface. This will
92 ;; give you the unevaluated saved value, if any, otherwise the
93 ;; unevaluated standard value.
95 ;; The possible states for a customize widget are:
97 ;; 0. unknown
99 ;; The state has not been determined yet.
101 ;; 1. modified
103 ;; The widget value is different from the current value.
105 ;; 2. changed
107 ;; The current value is different from the "think" value.
109 ;; 3. set
111 ;; The "think" value is the customized value.
113 ;; 4. saved
115 ;; The "think" value is the saved value.
117 ;; 5. standard
119 ;; The "think" value is the standard value.
121 ;; 6. rogue
123 ;; There is no standard value. This means that the variable was
124 ;; not defined with defcustom, nor handled in cus-start.el. You
125 ;; can not create a Custom buffer for such variables using the
126 ;; normal interactive Custom commands. However, such Custom
127 ;; buffers can be created in other ways, for instance, by calling
128 ;; `customize-option' non-interactively.
130 ;; 7. hidden
132 ;; There is no widget value.
134 ;; 8. mismatch
136 ;; The widget value is not valid member of the :type specified for the
137 ;; option.
139 ;;; Code:
141 (require 'cus-face)
142 (require 'wid-edit)
143 (eval-when-compile
144 (defvar custom-versions-load-alist) ; from cus-load
145 (defvar recentf-exclude)) ; from recentf.el
147 (condition-case nil
148 (require 'cus-load)
149 (error nil))
151 (condition-case nil
152 (require 'cus-start)
153 (error nil))
155 (put 'custom-define-hook 'custom-type 'hook)
156 (put 'custom-define-hook 'standard-value '(nil))
157 (custom-add-to-group 'customize 'custom-define-hook 'custom-variable)
159 ;;; Customization Groups.
161 (defgroup emacs nil
162 "Customization of the One True Editor."
163 :link '(custom-manual "(emacs)Top"))
165 ;; Most of these groups are stolen from `finder.el',
166 (defgroup editing nil
167 "Basic text editing facilities."
168 :group 'emacs)
170 (defgroup abbrev nil
171 "Abbreviation handling, typing shortcuts, macros."
172 :tag "Abbreviations"
173 :group 'editing)
175 (defgroup matching nil
176 "Various sorts of searching and matching."
177 :group 'editing)
179 (defgroup emulations nil
180 "Emulations of other editors."
181 :link '(custom-manual "(emacs)Emulation")
182 :group 'editing)
184 (defgroup mouse nil
185 "Mouse support."
186 :group 'editing)
188 (defgroup outlines nil
189 "Support for hierarchical outlining."
190 :group 'editing)
192 (defgroup external nil
193 "Interfacing to external utilities."
194 :group 'emacs)
196 (defgroup processes nil
197 "Process, subshell, compilation, and job control support."
198 :group 'external
199 :group 'development)
201 (defgroup convenience nil
202 "Convenience features for faster editing."
203 :group 'emacs)
205 (defgroup programming nil
206 "Support for programming in other languages."
207 :group 'emacs)
209 (defgroup languages nil
210 "Specialized modes for editing programming languages."
211 :group 'programming)
213 (defgroup lisp nil
214 "Lisp support, including Emacs Lisp."
215 :group 'languages
216 :group 'development)
218 (defgroup c nil
219 "Support for the C language and related languages."
220 :link '(custom-manual "(ccmode)")
221 :group 'languages)
223 (defgroup tools nil
224 "Programming tools."
225 :group 'programming)
227 (defgroup oop nil
228 "Support for object-oriented programming."
229 :group 'programming)
231 (defgroup applications nil
232 "Applications written in Emacs."
233 :group 'emacs)
235 (defgroup calendar nil
236 "Calendar and time management support."
237 :group 'applications)
239 (defgroup mail nil
240 "Modes for electronic-mail handling."
241 :group 'applications)
243 (defgroup news nil
244 "Support for netnews reading and posting."
245 :link '(custom-manual "(gnus)")
246 :group 'applications)
248 (defgroup games nil
249 "Games, jokes and amusements."
250 :group 'applications)
252 (defgroup development nil
253 "Support for further development of Emacs."
254 :group 'emacs)
256 (defgroup docs nil
257 "Support for Emacs documentation."
258 :group 'development)
260 (defgroup extensions nil
261 "Emacs Lisp language extensions."
262 :group 'development)
264 (defgroup internal nil
265 "Code for Emacs internals, build process, defaults."
266 :group 'development)
268 (defgroup maint nil
269 "Maintenance aids for the Emacs development group."
270 :tag "Maintenance"
271 :group 'development)
273 (defgroup environment nil
274 "Fitting Emacs with its environment."
275 :group 'emacs)
277 (defgroup comm nil
278 "Communications, networking, remote access to files."
279 :tag "Communication"
280 :group 'environment)
282 (defgroup hardware nil
283 "Support for interfacing with exotic hardware."
284 :group 'environment)
286 (defgroup terminals nil
287 "Support for terminal types."
288 :group 'environment)
290 (defgroup unix nil
291 "Front-ends/assistants for, or emulators of, UNIX features."
292 :group 'environment)
294 (defgroup vms nil
295 "Support code for vms."
296 :group 'environment)
298 (defgroup i18n nil
299 "Internationalization and alternate character-set support."
300 :link '(custom-manual "(emacs)International")
301 :group 'environment
302 :group 'editing)
304 (defgroup x nil
305 "The X Window system."
306 :group 'environment)
308 (defgroup frames nil
309 "Support for Emacs frames and window systems."
310 :group 'environment)
312 (defgroup data nil
313 "Support editing files of data."
314 :group 'emacs)
316 (defgroup files nil
317 "Support editing files."
318 :group 'emacs)
320 (defgroup wp nil
321 "Word processing."
322 :group 'emacs)
324 (defgroup tex nil
325 "Code related to the TeX formatter."
326 :group 'wp)
328 (defgroup faces nil
329 "Support for multiple fonts."
330 :group 'emacs)
332 (defgroup hypermedia nil
333 "Support for links between text or other media types."
334 :group 'emacs)
336 (defgroup help nil
337 "Support for on-line help systems."
338 :group 'emacs)
340 (defgroup multimedia nil
341 "Non-textual support, specifically images and sound."
342 :group 'emacs)
344 (defgroup local nil
345 "Code local to your site."
346 :group 'emacs)
348 (defgroup customize '((widgets custom-group))
349 "Customization of the Customization support."
350 :prefix "custom-"
351 :group 'help)
353 (defgroup custom-faces nil
354 "Faces used by customize."
355 :group 'customize
356 :group 'faces)
358 (defgroup custom-browse nil
359 "Control customize browser."
360 :prefix "custom-"
361 :group 'customize)
363 (defgroup custom-buffer nil
364 "Control customize buffers."
365 :prefix "custom-"
366 :group 'customize)
368 (defgroup custom-menu nil
369 "Control customize menus."
370 :prefix "custom-"
371 :group 'customize)
373 (defgroup abbrev-mode nil
374 "Word abbreviations mode."
375 :link '(custom-manual "(emacs)Abbrevs")
376 :group 'abbrev)
378 (defgroup alloc nil
379 "Storage allocation and gc for GNU Emacs Lisp interpreter."
380 :tag "Storage Allocation"
381 :group 'internal)
383 (defgroup undo nil
384 "Undoing changes in buffers."
385 :link '(custom-manual "(emacs)Undo")
386 :group 'editing)
388 (defgroup modeline nil
389 "Content of the modeline."
390 :group 'environment)
392 (defgroup editing-basics nil
393 "Most basic editing facilities."
394 :group 'editing)
396 (defgroup display nil
397 "How characters are displayed in buffers."
398 :group 'environment)
400 (defgroup execute nil
401 "Executing external commands."
402 :group 'processes)
404 (defgroup installation nil
405 "The Emacs installation."
406 :group 'environment)
408 (defgroup dired nil
409 "Directory editing."
410 :group 'environment)
412 (defgroup limits nil
413 "Internal Emacs limits."
414 :group 'internal)
416 (defgroup debug nil
417 "Debugging Emacs itself."
418 :group 'development)
420 (defgroup minibuffer nil
421 "Controling the behavior of the minibuffer."
422 :link '(custom-manual "(emacs)Minibuffer")
423 :group 'environment)
425 (defgroup keyboard nil
426 "Input from the keyboard."
427 :group 'environment)
429 (defgroup mouse nil
430 "Input from the mouse."
431 :group 'environment)
433 (defgroup menu nil
434 "Input from the menus."
435 :group 'environment)
437 (defgroup dnd nil
438 "Handling data from drag and drop."
439 :group 'environment)
441 (defgroup auto-save nil
442 "Preventing accidential loss of data."
443 :group 'files)
445 (defgroup processes-basics nil
446 "Basic stuff dealing with processes."
447 :group 'processes)
449 (defgroup mule nil
450 "MULE Emacs internationalization."
451 :group 'i18n)
453 (defgroup windows nil
454 "Windows within a frame."
455 :link '(custom-manual "(emacs)Windows")
456 :group 'environment)
458 ;;; Utilities.
460 (defun custom-quote (sexp)
461 "Quote SEXP iff it is not self quoting."
462 (if (or (memq sexp '(t nil))
463 (keywordp sexp)
464 (and (listp sexp)
465 (memq (car sexp) '(lambda)))
466 (stringp sexp)
467 (numberp sexp)
468 (vectorp sexp)
469 ;;; (and (fboundp 'characterp)
470 ;;; (characterp sexp))
472 sexp
473 (list 'quote sexp)))
475 (defun custom-split-regexp-maybe (regexp)
476 "If REGEXP is a string, split it to a list at `\\|'.
477 You can get the original back with from the result with:
478 (mapconcat 'identity result \"\\|\")
480 IF REGEXP is not a string, return it unchanged."
481 (if (stringp regexp)
482 (let ((start 0)
483 all)
484 (while (string-match "\\\\|" regexp start)
485 (setq all (cons (substring regexp start (match-beginning 0)) all)
486 start (match-end 0)))
487 (nreverse (cons (substring regexp start) all)))
488 regexp))
490 (defun custom-variable-prompt ()
491 "Prompt for a custom variable, defaulting to the variable at point.
492 Return a list suitable for use in `interactive'."
493 (let ((v (variable-at-point))
494 (enable-recursive-minibuffers t)
495 val)
496 (setq val (completing-read
497 (if (and (symbolp v) (custom-variable-p v))
498 (format "Customize option (default %s): " v)
499 "Customize option: ")
500 obarray 'custom-variable-p t))
501 (list (if (equal val "")
502 (if (symbolp v) v nil)
503 (intern val)))))
505 (defun custom-menu-filter (menu widget)
506 "Convert MENU to the form used by `widget-choose'.
507 MENU should be in the same format as `custom-variable-menu'.
508 WIDGET is the widget to apply the filter entries of MENU on."
509 (let ((result nil)
510 current name action filter)
511 (while menu
512 (setq current (car menu)
513 name (nth 0 current)
514 action (nth 1 current)
515 filter (nth 2 current)
516 menu (cdr menu))
517 (if (or (null filter) (funcall filter widget))
518 (push (cons name action) result)
519 (push name result)))
520 (nreverse result)))
522 ;;; Unlispify.
524 (defvar custom-prefix-list nil
525 "List of prefixes that should be ignored by `custom-unlispify'.")
527 (defcustom custom-unlispify-menu-entries t
528 "Display menu entries as words instead of symbols if non nil."
529 :group 'custom-menu
530 :type 'boolean)
532 (defcustom custom-unlispify-remove-prefixes nil
533 "Non-nil means remove group prefixes from option names in buffer."
534 :group 'custom-menu
535 :group 'custom-buffer
536 :type 'boolean)
538 (defun custom-unlispify-menu-entry (symbol &optional no-suffix)
539 "Convert SYMBOL into a menu entry."
540 (cond ((not custom-unlispify-menu-entries)
541 (symbol-name symbol))
542 ((get symbol 'custom-tag)
543 (if no-suffix
544 (get symbol 'custom-tag)
545 (concat (get symbol 'custom-tag) "...")))
547 (with-current-buffer (get-buffer-create " *Custom-Work*")
548 (erase-buffer)
549 (princ symbol (current-buffer))
550 (goto-char (point-min))
551 ;; FIXME: Boolean variables are not predicates, so they shouldn't
552 ;; end with `-p'. -stef
553 ;; (when (and (eq (get symbol 'custom-type) 'boolean)
554 ;; (re-search-forward "-p\\'" nil t))
555 ;; (replace-match "" t t)
556 ;; (goto-char (point-min)))
557 (if custom-unlispify-remove-prefixes
558 (let ((prefixes custom-prefix-list)
559 prefix)
560 (while prefixes
561 (setq prefix (car prefixes))
562 (if (search-forward prefix (+ (point) (length prefix)) t)
563 (progn
564 (setq prefixes nil)
565 (delete-region (point-min) (point)))
566 (setq prefixes (cdr prefixes))))))
567 (subst-char-in-region (point-min) (point-max) ?- ?\ t)
568 (capitalize-region (point-min) (point-max))
569 (unless no-suffix
570 (goto-char (point-max))
571 (insert "..."))
572 (buffer-string)))))
574 (defcustom custom-unlispify-tag-names t
575 "Display tag names as words instead of symbols if non nil."
576 :group 'custom-buffer
577 :type 'boolean)
579 (defun custom-unlispify-tag-name (symbol)
580 "Convert SYMBOL into a menu entry."
581 (let ((custom-unlispify-menu-entries custom-unlispify-tag-names))
582 (custom-unlispify-menu-entry symbol t)))
584 (defun custom-prefix-add (symbol prefixes)
585 "Add SYMBOL to list of ignored PREFIXES."
586 (cons (or (get symbol 'custom-prefix)
587 (concat (symbol-name symbol) "-"))
588 prefixes))
590 ;;; Guess.
592 (defcustom custom-guess-name-alist
593 '(("-p\\'" boolean)
594 ("-hook\\'" hook)
595 ("-face\\'" face)
596 ("-file\\'" file)
597 ("-function\\'" function)
598 ("-functions\\'" (repeat function))
599 ("-list\\'" (repeat sexp))
600 ("-alist\\'" (repeat (cons sexp sexp))))
601 "Alist of (MATCH TYPE).
603 MATCH should be a regexp matching the name of a symbol, and TYPE should
604 be a widget suitable for editing the value of that symbol. The TYPE
605 of the first entry where MATCH matches the name of the symbol will be
606 used.
608 This is used for guessing the type of variables not declared with
609 customize."
610 :type '(repeat (group (regexp :tag "Match") (sexp :tag "Type")))
611 :group 'customize)
613 (defcustom custom-guess-doc-alist
614 '(("\\`\\*?Non-nil " boolean))
615 "Alist of (MATCH TYPE).
617 MATCH should be a regexp matching a documentation string, and TYPE
618 should be a widget suitable for editing the value of a variable with
619 that documentation string. The TYPE of the first entry where MATCH
620 matches the name of the symbol will be used.
622 This is used for guessing the type of variables not declared with
623 customize."
624 :type '(repeat (group (regexp :tag "Match") (sexp :tag "Type")))
625 :group 'customize)
627 (defun custom-guess-type (symbol)
628 "Guess a widget suitable for editing the value of SYMBOL.
629 This is done by matching SYMBOL with `custom-guess-name-alist' and
630 if that fails, the doc string with `custom-guess-doc-alist'."
631 (let ((name (symbol-name symbol))
632 (names custom-guess-name-alist)
633 current found)
634 (while names
635 (setq current (car names)
636 names (cdr names))
637 (when (string-match (nth 0 current) name)
638 (setq found (nth 1 current)
639 names nil)))
640 (unless found
641 (let ((doc (documentation-property symbol 'variable-documentation))
642 (docs custom-guess-doc-alist))
643 (when doc
644 (while docs
645 (setq current (car docs)
646 docs (cdr docs))
647 (when (string-match (nth 0 current) doc)
648 (setq found (nth 1 current)
649 docs nil))))))
650 found))
652 ;;; Sorting.
654 (defcustom custom-browse-sort-alphabetically nil
655 "If non-nil, sort members of each customization group alphabetically."
656 :type 'boolean
657 :group 'custom-browse)
659 (defcustom custom-browse-order-groups nil
660 "If non-nil, order group members within each customization group.
661 If `first', order groups before non-groups.
662 If `last', order groups after non-groups."
663 :type '(choice (const first)
664 (const last)
665 (const :tag "none" nil))
666 :group 'custom-browse)
668 (defcustom custom-browse-only-groups nil
669 "If non-nil, show group members only within each customization group."
670 :type 'boolean
671 :group 'custom-browse)
673 (defcustom custom-buffer-sort-alphabetically t
674 "If non-nil, sort members of each customization group alphabetically."
675 :type 'boolean
676 :group 'custom-buffer)
678 (defcustom custom-buffer-order-groups 'last
679 "If non-nil, order group members within each customization group.
680 If `first', order groups before non-groups.
681 If `last', order groups after non-groups."
682 :type '(choice (const first)
683 (const last)
684 (const :tag "none" nil))
685 :group 'custom-buffer)
687 (defcustom custom-menu-sort-alphabetically nil
688 "If non-nil, sort members of each customization group alphabetically."
689 :type 'boolean
690 :group 'custom-menu)
692 (defcustom custom-menu-order-groups 'first
693 "If non-nil, order group members within each customization group.
694 If `first', order groups before non-groups.
695 If `last', order groups after non-groups."
696 :type '(choice (const first)
697 (const last)
698 (const :tag "none" nil))
699 :group 'custom-menu)
701 ;;;###autoload (add-hook 'same-window-regexps "\\`\\*Customiz.*\\*\\'")
703 (defun custom-sort-items (items sort-alphabetically order-groups)
704 "Return a sorted copy of ITEMS.
705 ITEMS should be a `custom-group' property.
706 If SORT-ALPHABETICALLY non-nil, sort alphabetically.
707 If ORDER-GROUPS is `first' order groups before non-groups, if `last' order
708 groups after non-groups, if nil do not order groups at all."
709 (sort (copy-sequence items)
710 (lambda (a b)
711 (let ((typea (nth 1 a)) (typeb (nth 1 b))
712 (namea (nth 0 a)) (nameb (nth 0 b)))
713 (cond ((not order-groups)
714 ;; Since we don't care about A and B order, maybe sort.
715 (when sort-alphabetically
716 (string-lessp namea nameb)))
717 ((eq typea 'custom-group)
718 ;; If B is also a group, maybe sort. Otherwise, order A and B.
719 (if (eq typeb 'custom-group)
720 (when sort-alphabetically
721 (string-lessp namea nameb))
722 (eq order-groups 'first)))
723 ((eq typeb 'custom-group)
724 ;; Since A cannot be a group, order A and B.
725 (eq order-groups 'last))
726 (sort-alphabetically
727 ;; Since A and B cannot be groups, sort.
728 (string-lessp namea nameb)))))))
730 ;;; Custom Mode Commands.
732 (defvar custom-options nil
733 "Customization widgets in the current buffer.")
735 (defun Custom-set ()
736 "Set changes in all modified options."
737 (interactive)
738 (let ((children custom-options))
739 (mapc (lambda (child)
740 (when (eq (widget-get child :custom-state) 'modified)
741 (widget-apply child :custom-set)))
742 children)))
744 (defun Custom-save ()
745 "Set all modified group members and save them."
746 (interactive)
747 (let ((children custom-options))
748 (mapc (lambda (child)
749 (when (memq (widget-get child :custom-state)
750 '(modified set changed rogue))
751 (widget-apply child :custom-save)))
752 children))
753 (custom-save-all))
755 (defvar custom-reset-menu
756 '(("Current" . Custom-reset-current)
757 ("Saved" . Custom-reset-saved)
758 ("Erase Customization (use standard settings)" . Custom-reset-standard))
759 "Alist of actions for the `Reset' button.
760 The key is a string containing the name of the action, the value is a
761 Lisp function taking the widget as an element which will be called
762 when the action is chosen.")
764 (defun custom-reset (event)
765 "Select item from reset menu."
766 (let* ((completion-ignore-case t)
767 (answer (widget-choose "Reset to"
768 custom-reset-menu
769 event)))
770 (if answer
771 (funcall answer))))
773 (defun Custom-reset-current (&rest ignore)
774 "Reset all modified group members to their current value."
775 (interactive)
776 (let ((children custom-options))
777 (mapc (lambda (widget)
778 (if (memq (widget-get widget :custom-state)
779 '(modified changed))
780 (widget-apply widget :custom-reset-current)))
781 children)))
783 (defun Custom-reset-saved (&rest ignore)
784 "Reset all modified or set group members to their saved value."
785 (interactive)
786 (let ((children custom-options))
787 (mapc (lambda (widget)
788 (if (memq (widget-get widget :custom-state)
789 '(modified set changed rogue))
790 (widget-apply widget :custom-reset-saved)))
791 children)))
793 (defun Custom-reset-standard (&rest ignore)
794 "Erase all customization (either current or saved) for the group members.
795 The immediate result is to restore them to their standard settings.
796 This operation eliminates any saved settings for the group members,
797 making them as if they had never been customized at all."
798 (interactive)
799 (let ((children custom-options))
800 (mapc (lambda (widget)
801 (and (widget-apply widget :custom-standard-value)
802 (if (memq (widget-get widget :custom-state)
803 '(modified set changed saved rogue))
804 (widget-apply widget :custom-reset-standard))))
805 children)))
807 ;;; The Customize Commands
809 (defun custom-prompt-variable (prompt-var prompt-val &optional comment)
810 "Prompt for a variable and a value and return them as a list.
811 PROMPT-VAR is the prompt for the variable, and PROMPT-VAL is the
812 prompt for the value. The %s escape in PROMPT-VAL is replaced with
813 the name of the variable.
815 If the variable has a `variable-interactive' property, that is used as if
816 it were the arg to `interactive' (which see) to interactively read the value.
818 If the variable has a `custom-type' property, it must be a widget and the
819 `:prompt-value' property of that widget will be used for reading the value.
821 If optional COMMENT argument is non nil, also prompt for a comment and return
822 it as the third element in the list."
823 (let* ((var (read-variable prompt-var))
824 (minibuffer-help-form '(describe-variable var))
825 (val
826 (let ((prop (get var 'variable-interactive))
827 (type (get var 'custom-type))
828 (prompt (format prompt-val var)))
829 (unless (listp type)
830 (setq type (list type)))
831 (cond (prop
832 ;; Use VAR's `variable-interactive' property
833 ;; as an interactive spec for prompting.
834 (call-interactively (list 'lambda '(arg)
835 (list 'interactive prop)
836 'arg)))
837 (type
838 (widget-prompt-value type
839 prompt
840 (if (boundp var)
841 (symbol-value var))
842 (not (boundp var))))
844 (eval-minibuffer prompt))))))
845 (if comment
846 (list var val
847 (read-string "Comment: " (get var 'variable-comment)))
848 (list var val))))
850 ;;;###autoload
851 (defun customize-set-value (variable value &optional comment)
852 "Set VARIABLE to VALUE, and return VALUE. VALUE is a Lisp object.
854 If VARIABLE has a `variable-interactive' property, that is used as if
855 it were the arg to `interactive' (which see) to interactively read the value.
857 If VARIABLE has a `custom-type' property, it must be a widget and the
858 `:prompt-value' property of that widget will be used for reading the value.
860 If given a prefix (or a COMMENT argument), also prompt for a comment."
861 (interactive (custom-prompt-variable "Set variable: "
862 "Set %s to value: "
863 current-prefix-arg))
865 (cond ((string= comment "")
866 (put variable 'variable-comment nil))
867 (comment
868 (put variable 'variable-comment comment)))
869 (set variable value))
871 ;;;###autoload
872 (defun customize-set-variable (variable value &optional comment)
873 "Set the default for VARIABLE to VALUE, and return VALUE.
874 VALUE is a Lisp object.
876 If VARIABLE has a `custom-set' property, that is used for setting
877 VARIABLE, otherwise `set-default' is used.
879 The `customized-value' property of the VARIABLE will be set to a list
880 with a quoted VALUE as its sole list member.
882 If VARIABLE has a `variable-interactive' property, that is used as if
883 it were the arg to `interactive' (which see) to interactively read the value.
885 If VARIABLE has a `custom-type' property, it must be a widget and the
886 `:prompt-value' property of that widget will be used for reading the value.
888 If given a prefix (or a COMMENT argument), also prompt for a comment."
889 (interactive (custom-prompt-variable "Set variable: "
890 "Set customized value for %s to: "
891 current-prefix-arg))
892 (custom-load-symbol variable)
893 (funcall (or (get variable 'custom-set) 'set-default) variable value)
894 (put variable 'customized-value (list (custom-quote value)))
895 (cond ((string= comment "")
896 (put variable 'variable-comment nil)
897 (put variable 'customized-variable-comment nil))
898 (comment
899 (put variable 'variable-comment comment)
900 (put variable 'customized-variable-comment comment)))
901 value)
903 ;;;###autoload
904 (defun customize-save-variable (variable value &optional comment)
905 "Set the default for VARIABLE to VALUE, and save it for future sessions.
906 Return VALUE.
908 If VARIABLE has a `custom-set' property, that is used for setting
909 VARIABLE, otherwise `set-default' is used.
911 The `customized-value' property of the VARIABLE will be set to a list
912 with a quoted VALUE as its sole list member.
914 If VARIABLE has a `variable-interactive' property, that is used as if
915 it were the arg to `interactive' (which see) to interactively read the value.
917 If VARIABLE has a `custom-type' property, it must be a widget and the
918 `:prompt-value' property of that widget will be used for reading the value.
920 If given a prefix (or a COMMENT argument), also prompt for a comment."
921 (interactive (custom-prompt-variable "Set and save variable: "
922 "Set and save value for %s as: "
923 current-prefix-arg))
924 (funcall (or (get variable 'custom-set) 'set-default) variable value)
925 (put variable 'saved-value (list (custom-quote value)))
926 (custom-push-theme 'theme-value variable 'user 'set (list (custom-quote value)))
927 (cond ((string= comment "")
928 (put variable 'variable-comment nil)
929 (put variable 'saved-variable-comment nil))
930 (comment
931 (put variable 'variable-comment comment)
932 (put variable 'saved-variable-comment comment)))
933 (custom-save-all)
934 value)
936 ;;;###autoload
937 (defun customize ()
938 "Select a customization buffer which you can use to set user options.
939 User options are structured into \"groups\".
940 Initially the top-level group `Emacs' and its immediate subgroups
941 are shown; the contents of those subgroups are initially hidden."
942 (interactive)
943 (customize-group 'emacs))
945 ;;;###autoload
946 (defun customize-mode (mode)
947 "Customize options related to the current major mode.
948 If a prefix \\[universal-argument] was given (or if the current major mode has no known group),
949 then prompt for the MODE to customize."
950 (interactive
951 (list
952 (let ((completion-regexp-list '("-mode\\'"))
953 (group (custom-group-of-mode major-mode)))
954 (if (and group (not current-prefix-arg))
955 major-mode
956 (intern
957 (completing-read (if group
958 (format "Major mode (default %s): " major-mode)
959 "Major mode: ")
960 obarray
961 'custom-group-of-mode
962 t nil nil (if group (symbol-name major-mode))))))))
963 (customize-group (custom-group-of-mode mode)))
966 ;;;###autoload
967 (defun customize-group (group)
968 "Customize GROUP, which must be a customization group."
969 (interactive (list (let ((completion-ignore-case t))
970 (completing-read "Customize group (default emacs): "
971 obarray
972 (lambda (symbol)
973 (or (get symbol 'custom-loads)
974 (get symbol 'custom-group)))
975 t))))
976 (when (stringp group)
977 (if (string-equal "" group)
978 (setq group 'emacs)
979 (setq group (intern group))))
980 (let ((name (format "*Customize Group: %s*"
981 (custom-unlispify-tag-name group))))
982 (if (get-buffer name)
983 (pop-to-buffer name)
984 (custom-buffer-create (list (list group 'custom-group))
985 name
986 (concat " for group "
987 (custom-unlispify-tag-name group))))))
989 ;;;###autoload
990 (defun customize-group-other-window (group)
991 "Customize GROUP, which must be a customization group."
992 (interactive (list (let ((completion-ignore-case t))
993 (completing-read "Customize group (default emacs): "
994 obarray
995 (lambda (symbol)
996 (or (get symbol 'custom-loads)
997 (get symbol 'custom-group)))
998 t))))
999 (when (stringp group)
1000 (if (string-equal "" group)
1001 (setq group 'emacs)
1002 (setq group (intern group))))
1003 (let ((name (format "*Customize Group: %s*"
1004 (custom-unlispify-tag-name group))))
1005 (if (get-buffer name)
1006 (let (
1007 ;; Copied from `custom-buffer-create-other-window'.
1008 (pop-up-windows t)
1009 (same-window-buffer-names nil)
1010 (same-window-regexps nil))
1011 (pop-to-buffer name))
1012 (custom-buffer-create-other-window
1013 (list (list group 'custom-group))
1014 name
1015 (concat " for group "
1016 (custom-unlispify-tag-name group))))))
1018 ;;;###autoload
1019 (defalias 'customize-variable 'customize-option)
1021 ;;;###autoload
1022 (defun customize-option (symbol)
1023 "Customize SYMBOL, which must be a user option variable."
1024 (interactive (custom-variable-prompt))
1025 (let ((basevar (indirect-variable symbol)))
1026 (custom-buffer-create (list (list basevar 'custom-variable))
1027 (format "*Customize Option: %s*"
1028 (custom-unlispify-tag-name basevar)))
1029 (unless (eq symbol basevar)
1030 (message "`%s' is an alias for `%s'" symbol basevar))))
1032 ;;;###autoload
1033 (defalias 'customize-variable-other-window 'customize-option-other-window)
1035 ;;;###autoload
1036 (defun customize-option-other-window (symbol)
1037 "Customize SYMBOL, which must be a user option variable.
1038 Show the buffer in another window, but don't select it."
1039 (interactive (custom-variable-prompt))
1040 (let ((basevar (indirect-variable symbol)))
1041 (custom-buffer-create-other-window
1042 (list (list basevar 'custom-variable))
1043 (format "*Customize Option: %s*" (custom-unlispify-tag-name basevar)))
1044 (unless (eq symbol basevar)
1045 (message "`%s' is an alias for `%s'" symbol basevar))))
1047 (defvar customize-changed-options-previous-release "20.2"
1048 "Version for `customize-changed-options' to refer back to by default.")
1050 ;;;###autoload
1051 (defun customize-changed-options (since-version)
1052 "Customize all user option variables changed in Emacs itself.
1053 This includes new user option variables and faces, and new
1054 customization groups, as well as older options and faces whose default
1055 values have changed since the previous major Emacs release.
1057 With argument SINCE-VERSION (a string), customize all user option
1058 variables that were added (or their meanings were changed) since that
1059 version."
1061 (interactive "sCustomize options changed, since version (default all versions): ")
1062 (if (equal since-version "")
1063 (setq since-version nil)
1064 (unless (condition-case nil
1065 (numberp (read since-version))
1066 (error nil))
1067 (signal 'wrong-type-argument (list 'numberp since-version))))
1068 (unless since-version
1069 (setq since-version customize-changed-options-previous-release))
1071 ;; Load the information for versions since since-version. We use
1072 ;; custom-load-symbol for this.
1073 (put 'custom-versions-load-alist 'custom-loads nil)
1074 (dolist (elt custom-versions-load-alist)
1075 (if (customize-version-lessp since-version (car elt))
1076 (dolist (load (cdr elt))
1077 (custom-add-load 'custom-versions-load-alist load))))
1078 (custom-load-symbol 'custom-versions-load-alist)
1079 (put 'custom-versions-load-alist 'custom-loads nil)
1081 (let (found)
1082 (mapatoms
1083 (lambda (symbol)
1084 (let ((version (get symbol 'custom-version)))
1085 (if version
1086 (when (customize-version-lessp since-version version)
1087 (if (or (get symbol 'custom-group)
1088 (get symbol 'group-documentation))
1089 (push (list symbol 'custom-group) found))
1090 (if (custom-variable-p symbol)
1091 (push (list symbol 'custom-variable) found))
1092 (if (custom-facep symbol)
1093 (push (list symbol 'custom-face) found)))))))
1094 (if found
1095 (custom-buffer-create (custom-sort-items found t 'first)
1096 "*Customize Changed Options*")
1097 (error "No user option defaults have been changed since Emacs %s"
1098 since-version))))
1100 (defun customize-version-lessp (version1 version2)
1101 ;; Why are the versions strings, and given that they are, why aren't
1102 ;; they converted to numbers and compared as such here? -- fx
1104 ;; In case someone made a mistake and left out the quotes
1105 ;; in the :version value.
1106 (if (numberp version2)
1107 (setq version2 (prin1-to-string version2)))
1108 (let (major1 major2 minor1 minor2)
1109 (string-match "\\([0-9]+\\)\\(\\.\\([0-9]+\\)\\)?" version1)
1110 (setq major1 (read (or (match-string 1 version1)
1111 "0")))
1112 (setq minor1 (read (or (match-string 3 version1)
1113 "0")))
1114 (string-match "\\([0-9]+\\)\\(\\.\\([0-9]+\\)\\)?" version2)
1115 (setq major2 (read (or (match-string 1 version2)
1116 "0")))
1117 (setq minor2 (read (or (match-string 3 version2)
1118 "0")))
1119 (or (< major1 major2)
1120 (and (= major1 major2)
1121 (< minor1 minor2)))))
1123 ;;;###autoload
1124 (defun customize-face (&optional face)
1125 "Customize FACE, which should be a face name or nil.
1126 If FACE is nil, customize all faces. If FACE is actually a
1127 face-alias, customize the face it is aliased to.
1129 Interactively, when point is on text which has a face specified,
1130 suggest to customize that face, if it's customizable."
1131 (interactive
1132 (list (read-face-name "Customize face" "all faces" t)))
1133 (if (member face '(nil ""))
1134 (setq face (face-list)))
1135 (if (and (listp face) (null (cdr face)))
1136 (setq face (car face)))
1137 (if (listp face)
1138 (custom-buffer-create (custom-sort-items
1139 (mapcar (lambda (s)
1140 (list s 'custom-face))
1141 face)
1142 t nil)
1143 "*Customize Faces*")
1144 ;; If FACE is actually an alias, customize the face it is aliased to.
1145 (if (get face 'face-alias)
1146 (setq face (get face 'face-alias)))
1147 (unless (facep face)
1148 (error "Invalid face %S" face))
1149 (custom-buffer-create (list (list face 'custom-face))
1150 (format "*Customize Face: %s*"
1151 (custom-unlispify-tag-name face)))))
1153 ;;;###autoload
1154 (defun customize-face-other-window (&optional face)
1155 "Show customization buffer for face FACE in other window.
1156 If FACE is actually a face-alias, customize the face it is aliased to.
1158 Interactively, when point is on text which has a face specified,
1159 suggest to customize that face, if it's customizable."
1160 (interactive
1161 (list (read-face-name "Customize face" "all faces" t)))
1162 (if (member face '(nil ""))
1163 (setq face (face-list)))
1164 (if (and (listp face) (null (cdr face)))
1165 (setq face (car face)))
1166 (if (listp face)
1167 (custom-buffer-create-other-window
1168 (custom-sort-items
1169 (mapcar (lambda (s)
1170 (list s 'custom-face))
1171 face)
1172 t nil)
1173 "*Customize Faces*")
1174 (if (get face 'face-alias)
1175 (setq face (get face 'face-alias)))
1176 (unless (facep face)
1177 (error "Invalid face %S" face))
1178 (custom-buffer-create-other-window
1179 (list (list face 'custom-face))
1180 (format "*Customize Face: %s*"
1181 (custom-unlispify-tag-name face)))))
1183 ;;;###autoload
1184 (defun customize-customized ()
1185 "Customize all user options set since the last save in this session."
1186 (interactive)
1187 (let ((found nil))
1188 (mapatoms (lambda (symbol)
1189 (and (or (get symbol 'customized-face)
1190 (get symbol 'customized-face-comment))
1191 (custom-facep symbol)
1192 (push (list symbol 'custom-face) found))
1193 (and (or (get symbol 'customized-value)
1194 (get symbol 'customized-variable-comment))
1195 (boundp symbol)
1196 (push (list symbol 'custom-variable) found))))
1197 (if (not found)
1198 (error "No customized user options")
1199 (custom-buffer-create (custom-sort-items found t nil)
1200 "*Customize Customized*"))))
1202 ;;;###autoload
1203 (defun customize-rogue ()
1204 "Customize all user variable modified outside customize."
1205 (interactive)
1206 (let ((found nil))
1207 (mapatoms (lambda (symbol)
1208 (let ((cval (or (get symbol 'customized-value)
1209 (get symbol 'saved-value)
1210 (get symbol 'standard-value))))
1211 (when (and cval ;Declared with defcustom.
1212 (default-boundp symbol) ;Has a value.
1213 (not (equal (eval (car cval))
1214 ;; Which does not match customize.
1215 (default-value symbol))))
1216 (push (list symbol 'custom-variable) found)))))
1217 (if (not found)
1218 (error "No rogue user options")
1219 (custom-buffer-create (custom-sort-items found t nil)
1220 "*Customize Rogue*"))))
1221 ;;;###autoload
1222 (defun customize-saved ()
1223 "Customize all already saved user options."
1224 (interactive)
1225 (let ((found nil))
1226 (mapatoms (lambda (symbol)
1227 (and (or (get symbol 'saved-face)
1228 (get symbol 'saved-face-comment))
1229 (custom-facep symbol)
1230 (push (list symbol 'custom-face) found))
1231 (and (or (get symbol 'saved-value)
1232 (get symbol 'saved-variable-comment))
1233 (boundp symbol)
1234 (push (list symbol 'custom-variable) found))))
1235 (if (not found )
1236 (error "No saved user options")
1237 (custom-buffer-create (custom-sort-items found t nil)
1238 "*Customize Saved*"))))
1240 ;;;###autoload
1241 (defun customize-apropos (regexp &optional all)
1242 "Customize all user options matching REGEXP.
1243 If ALL is `options', include only options.
1244 If ALL is `faces', include only faces.
1245 If ALL is `groups', include only groups.
1246 If ALL is t (interactively, with prefix arg), include options which are not
1247 user-settable, as well as faces and groups."
1248 (interactive "sCustomize regexp: \nP")
1249 (let ((found nil))
1250 (mapatoms (lambda (symbol)
1251 (when (string-match regexp (symbol-name symbol))
1252 (when (and (not (memq all '(faces options)))
1253 (get symbol 'custom-group))
1254 (push (list symbol 'custom-group) found))
1255 (when (and (not (memq all '(options groups)))
1256 (custom-facep symbol))
1257 (push (list symbol 'custom-face) found))
1258 (when (and (not (memq all '(groups faces)))
1259 (boundp symbol)
1260 (or (get symbol 'saved-value)
1261 (custom-variable-p symbol)
1262 (if (memq all '(nil options))
1263 (user-variable-p symbol)
1264 (get symbol 'variable-documentation))))
1265 (push (list symbol 'custom-variable) found)))))
1266 (if (not found)
1267 (error "No matches")
1268 (custom-buffer-create (custom-sort-items found t
1269 custom-buffer-order-groups)
1270 "*Customize Apropos*"))))
1272 ;;;###autoload
1273 (defun customize-apropos-options (regexp &optional arg)
1274 "Customize all user options matching REGEXP.
1275 With prefix arg, include options which are not user-settable."
1276 (interactive "sCustomize regexp: \nP")
1277 (customize-apropos regexp (or arg 'options)))
1279 ;;;###autoload
1280 (defun customize-apropos-faces (regexp)
1281 "Customize all user faces matching REGEXP."
1282 (interactive "sCustomize regexp: \n")
1283 (customize-apropos regexp 'faces))
1285 ;;;###autoload
1286 (defun customize-apropos-groups (regexp)
1287 "Customize all user groups matching REGEXP."
1288 (interactive "sCustomize regexp: \n")
1289 (customize-apropos regexp 'groups))
1291 ;;; Buffer.
1293 (defcustom custom-buffer-style 'links
1294 "Control the presentation style for customization buffers.
1295 The value should be a symbol, one of:
1297 brackets: groups nest within each other with big horizontal brackets.
1298 links: groups have links to subgroups."
1299 :type '(radio (const brackets)
1300 (const links))
1301 :group 'custom-buffer)
1303 (defcustom custom-buffer-done-kill nil
1304 "*Non-nil means exiting a Custom buffer should kill it."
1305 :type 'boolean
1306 :version "22.1"
1307 :group 'custom-buffer)
1309 (defcustom custom-buffer-indent 3
1310 "Number of spaces to indent nested groups."
1311 :type 'integer
1312 :group 'custom-buffer)
1314 (defun custom-get-fresh-buffer (name)
1315 "Get a fresh new buffer with name NAME.
1316 If the buffer already exist, clean it up to be like new.
1317 Beware: it's not quite like new. Good enough for custom, but maybe
1318 not for everybody."
1319 ;; To be more complete, we should also kill all permanent-local variables,
1320 ;; but it's not needed for custom.
1321 (let ((buf (get-buffer name)))
1322 (when (and buf (buffer-local-value 'buffer-file-name buf))
1323 ;; This will check if the file is not saved.
1324 (kill-buffer buf)
1325 (setq buf nil))
1326 (if (null buf)
1327 (get-buffer-create name)
1328 (with-current-buffer buf
1329 (kill-all-local-variables)
1330 (run-hooks 'kill-buffer-hook)
1331 ;; Delete overlays before erasing the buffer so the overlay hooks
1332 ;; don't get run spuriously when we erase the buffer.
1333 (let ((ols (overlay-lists)))
1334 (dolist (ol (nconc (car ols) (cdr ols)))
1335 (delete-overlay ol)))
1336 (erase-buffer)
1337 buf))))
1339 ;;;###autoload
1340 (defun custom-buffer-create (options &optional name description)
1341 "Create a buffer containing OPTIONS.
1342 Optional NAME is the name of the buffer.
1343 OPTIONS should be an alist of the form ((SYMBOL WIDGET)...), where
1344 SYMBOL is a customization option, and WIDGET is a widget for editing
1345 that option."
1346 (pop-to-buffer (custom-get-fresh-buffer (or name "*Customization*")))
1347 (custom-buffer-create-internal options description))
1349 ;;;###autoload
1350 (defun custom-buffer-create-other-window (options &optional name description)
1351 "Create a buffer containing OPTIONS, and display it in another window.
1352 The result includes selecting that window.
1353 Optional NAME is the name of the buffer.
1354 OPTIONS should be an alist of the form ((SYMBOL WIDGET)...), where
1355 SYMBOL is a customization option, and WIDGET is a widget for editing
1356 that option."
1357 (unless name (setq name "*Customization*"))
1358 (let ((pop-up-windows t)
1359 (same-window-buffer-names nil)
1360 (same-window-regexps nil))
1361 (pop-to-buffer (custom-get-fresh-buffer name))
1362 (custom-buffer-create-internal options description)))
1364 (defcustom custom-reset-button-menu nil
1365 "If non-nil, only show a single reset button in customize buffers.
1366 This button will have a menu with all three reset operations."
1367 :type 'boolean
1368 :group 'custom-buffer)
1370 (defcustom custom-buffer-verbose-help t
1371 "If non-nil, include explanatory text in the customization buffer."
1372 :type 'boolean
1373 :group 'custom-buffer)
1375 (defun Custom-buffer-done (&rest ignore)
1376 "Exit current Custom buffer according to `custom-buffer-done-kill'."
1377 (interactive)
1378 (quit-window custom-buffer-done-kill))
1380 (defcustom custom-raised-buttons (not (equal (face-valid-attribute-values :box)
1381 '(("unspecified" . unspecified))))
1382 "If non-nil, indicate active buttons in a `raised-button' style.
1383 Otherwise use brackets."
1384 :type 'boolean
1385 :version "21.1"
1386 :group 'custom-buffer)
1388 (defun custom-buffer-create-internal (options &optional description)
1389 (custom-mode)
1390 (if custom-buffer-verbose-help
1391 (progn
1392 (widget-insert "This is a customization buffer")
1393 (if description
1394 (widget-insert description))
1395 (widget-insert (format ".
1396 %s show active fields; type RET or click mouse-1
1397 on an active field to invoke its action. Editing an option value
1398 changes only the text in the buffer. Invoke the State button to set or
1399 save the option value. Saving an option normally edits your init file.
1400 Invoke "
1401 (if custom-raised-buttons
1402 "`Raised' buttons"
1403 "Square brackets")))
1404 (widget-create 'info-link
1405 :tag "Custom file"
1406 "(emacs)Saving Customizations")
1407 (widget-insert
1408 " for information on how to save in a different file.
1409 Invoke ")
1410 (widget-create 'info-link
1411 :tag "Help"
1412 :help-echo "Read the online help."
1413 "(emacs)Easy Customization")
1414 (widget-insert " for general information.\n\n")
1415 (widget-insert "Operate on everything in this buffer:\n "))
1416 (widget-insert " "))
1417 (widget-create 'push-button
1418 :tag "Set for Current Session"
1419 :help-echo "\
1420 Make your editing in this buffer take effect for this session."
1421 :action (lambda (widget &optional event)
1422 (Custom-set)))
1423 (widget-insert " ")
1424 (widget-create 'push-button
1425 :tag "Save for Future Sessions"
1426 :help-echo "\
1427 Make your editing in this buffer take effect for future Emacs sessions.
1428 This updates your Emacs initialization file or creates a new one."
1429 :action (lambda (widget &optional event)
1430 (Custom-save)))
1431 (if custom-reset-button-menu
1432 (progn
1433 (widget-insert " ")
1434 (widget-create 'push-button
1435 :tag "Reset"
1436 :help-echo "Show a menu with reset operations."
1437 :mouse-down-action (lambda (&rest junk) t)
1438 :action (lambda (widget &optional event)
1439 (custom-reset event))))
1440 (widget-insert "\n ")
1441 (widget-create 'push-button
1442 :tag "Reset"
1443 :help-echo "\
1444 Reset all edited text in this buffer to reflect current values."
1445 :action 'Custom-reset-current)
1446 (widget-insert " ")
1447 (widget-create 'push-button
1448 :tag "Reset to Saved"
1449 :help-echo "\
1450 Reset all values in this buffer to their saved settings."
1451 :action 'Custom-reset-saved)
1452 (widget-insert " ")
1453 (widget-create 'push-button
1454 :tag "Erase Customization"
1455 :help-echo "\
1456 Un-customize all values in this buffer. They get their standard settings."
1457 :action 'Custom-reset-standard))
1458 (if (not custom-buffer-verbose-help)
1459 (progn
1460 (widget-insert " ")
1461 (widget-create 'info-link
1462 :tag "Help"
1463 :help-echo "Read the online help."
1464 "(emacs)Easy Customization")))
1465 (widget-insert " ")
1466 (widget-create 'push-button
1467 :tag "Finish"
1468 :help-echo
1469 (lambda (&rest ignore)
1470 (if custom-buffer-done-kill
1471 "Kill this buffer"
1472 "Bury this buffer"))
1473 :action #'Custom-buffer-done)
1474 (widget-insert "\n\n")
1475 (message "Creating customization items...")
1476 (buffer-disable-undo)
1477 (setq custom-options
1478 (if (= (length options) 1)
1479 (mapcar (lambda (entry)
1480 (widget-create (nth 1 entry)
1481 :documentation-shown t
1482 :custom-state 'unknown
1483 :tag (custom-unlispify-tag-name
1484 (nth 0 entry))
1485 :value (nth 0 entry)))
1486 options)
1487 (let ((count 0)
1488 (length (length options)))
1489 (mapcar (lambda (entry)
1490 (prog2
1491 (message "Creating customization items ...%2d%%"
1492 (/ (* 100.0 count) length))
1493 (widget-create (nth 1 entry)
1494 :tag (custom-unlispify-tag-name
1495 (nth 0 entry))
1496 :value (nth 0 entry))
1497 (setq count (1+ count))
1498 (unless (eq (preceding-char) ?\n)
1499 (widget-insert "\n"))
1500 (widget-insert "\n")))
1501 options))))
1502 (unless (eq (preceding-char) ?\n)
1503 (widget-insert "\n"))
1504 (message "Creating customization items ...done")
1505 (message "Resetting customization items...")
1506 (unless (eq custom-buffer-style 'tree)
1507 (mapc 'custom-magic-reset custom-options))
1508 (message "Resetting customization items...done")
1509 (message "Creating customization setup...")
1510 (widget-setup)
1511 (buffer-enable-undo)
1512 (goto-char (point-min))
1513 (message "Creating customization setup...done"))
1515 ;;; The Tree Browser.
1517 ;;;###autoload
1518 (defun customize-browse (&optional group)
1519 "Create a tree browser for the customize hierarchy."
1520 (interactive)
1521 (unless group
1522 (setq group 'emacs))
1523 (let ((name "*Customize Browser*"))
1524 (pop-to-buffer (custom-get-fresh-buffer name)))
1525 (custom-mode)
1526 (widget-insert "\
1527 Square brackets show active fields; type RET or click mouse-1
1528 on an active field to invoke its action.
1529 Invoke [+] below to expand a group, and [-] to collapse an expanded group.\n")
1530 (if custom-browse-only-groups
1531 (widget-insert "\
1532 Invoke the [Group] button below to edit that item in another window.\n\n")
1533 (widget-insert "Invoke the ")
1534 (widget-create 'item
1535 :format "%t"
1536 :tag "[Group]"
1537 :tag-glyph "folder")
1538 (widget-insert ", ")
1539 (widget-create 'item
1540 :format "%t"
1541 :tag "[Face]"
1542 :tag-glyph "face")
1543 (widget-insert ", and ")
1544 (widget-create 'item
1545 :format "%t"
1546 :tag "[Option]"
1547 :tag-glyph "option")
1548 (widget-insert " buttons below to edit that
1549 item in another window.\n\n"))
1550 (let ((custom-buffer-style 'tree))
1551 (widget-create 'custom-group
1552 :custom-last t
1553 :custom-state 'unknown
1554 :tag (custom-unlispify-tag-name group)
1555 :value group))
1556 (widget-setup)
1557 (goto-char (point-min)))
1559 (define-widget 'custom-browse-visibility 'item
1560 "Control visibility of items in the customize tree browser."
1561 :format "%[[%t]%]"
1562 :action 'custom-browse-visibility-action)
1564 (defun custom-browse-visibility-action (widget &rest ignore)
1565 (let ((custom-buffer-style 'tree))
1566 (custom-toggle-parent widget)))
1568 (define-widget 'custom-browse-group-tag 'push-button
1569 "Show parent in other window when activated."
1570 :tag "Group"
1571 :tag-glyph "folder"
1572 :action 'custom-browse-group-tag-action)
1574 (defun custom-browse-group-tag-action (widget &rest ignore)
1575 (let ((parent (widget-get widget :parent)))
1576 (customize-group-other-window (widget-value parent))))
1578 (define-widget 'custom-browse-variable-tag 'push-button
1579 "Show parent in other window when activated."
1580 :tag "Option"
1581 :tag-glyph "option"
1582 :action 'custom-browse-variable-tag-action)
1584 (defun custom-browse-variable-tag-action (widget &rest ignore)
1585 (let ((parent (widget-get widget :parent)))
1586 (customize-variable-other-window (widget-value parent))))
1588 (define-widget 'custom-browse-face-tag 'push-button
1589 "Show parent in other window when activated."
1590 :tag "Face"
1591 :tag-glyph "face"
1592 :action 'custom-browse-face-tag-action)
1594 (defun custom-browse-face-tag-action (widget &rest ignore)
1595 (let ((parent (widget-get widget :parent)))
1596 (customize-face-other-window (widget-value parent))))
1598 (defconst custom-browse-alist '((" " "space")
1599 (" | " "vertical")
1600 ("-\\ " "top")
1601 (" |-" "middle")
1602 (" `-" "bottom")))
1604 (defun custom-browse-insert-prefix (prefix)
1605 "Insert PREFIX. On XEmacs convert it to line graphics."
1606 ;; Fixme: do graphics.
1607 (if nil ; (string-match "XEmacs" emacs-version)
1608 (progn
1609 (insert "*")
1610 (while (not (string-equal prefix ""))
1611 (let ((entry (substring prefix 0 3)))
1612 (setq prefix (substring prefix 3))
1613 (let ((overlay (make-overlay (1- (point)) (point) nil t nil))
1614 (name (nth 1 (assoc entry custom-browse-alist))))
1615 (overlay-put overlay 'end-glyph (widget-glyph-find name entry))
1616 (overlay-put overlay 'start-open t)
1617 (overlay-put overlay 'end-open t)))))
1618 (insert prefix)))
1620 ;;; Modification of Basic Widgets.
1622 ;; We add extra properties to the basic widgets needed here. This is
1623 ;; fine, as long as we are careful to stay within out own namespace.
1625 ;; We want simple widgets to be displayed by default, but complex
1626 ;; widgets to be hidden.
1628 (widget-put (get 'item 'widget-type) :custom-show t)
1629 (widget-put (get 'editable-field 'widget-type)
1630 :custom-show (lambda (widget value)
1631 (let ((pp (pp-to-string value)))
1632 (cond ((string-match "\n" pp)
1633 nil)
1634 ((> (length pp) 40)
1635 nil)
1636 (t t)))))
1637 (widget-put (get 'menu-choice 'widget-type) :custom-show t)
1639 ;;; The `custom-manual' Widget.
1641 (define-widget 'custom-manual 'info-link
1642 "Link to the manual entry for this customization option."
1643 :help-echo "Read the manual entry for this option."
1644 :tag "Manual")
1646 ;;; The `custom-magic' Widget.
1648 (defgroup custom-magic-faces nil
1649 "Faces used by the magic button."
1650 :group 'custom-faces
1651 :group 'custom-buffer)
1653 (defface custom-invalid '((((class color))
1654 (:foreground "yellow1" :background "red1"))
1656 (:weight bold :slant italic :underline t)))
1657 "Face used when the customize item is invalid."
1658 :group 'custom-magic-faces)
1659 ;; backward-compatibility alias
1660 (put 'custom-invalid-face 'face-alias 'custom-invalid)
1662 (defface custom-rogue '((((class color))
1663 (:foreground "pink" :background "black"))
1665 (:underline t)))
1666 "Face used when the customize item is not defined for customization."
1667 :group 'custom-magic-faces)
1668 ;; backward-compatibility alias
1669 (put 'custom-rogue-face 'face-alias 'custom-rogue)
1671 (defface custom-modified '((((min-colors 88) (class color))
1672 (:foreground "white" :background "blue1"))
1673 (((class color))
1674 (:foreground "white" :background "blue"))
1676 (:slant italic :bold)))
1677 "Face used when the customize item has been modified."
1678 :group 'custom-magic-faces)
1679 ;; backward-compatibility alias
1680 (put 'custom-modified-face 'face-alias 'custom-modified)
1682 (defface custom-set '((((min-colors 88) (class color))
1683 (:foreground "blue1" :background "white"))
1684 (((class color))
1685 (:foreground "blue" :background "white"))
1687 (:slant italic)))
1688 "Face used when the customize item has been set."
1689 :group 'custom-magic-faces)
1690 ;; backward-compatibility alias
1691 (put 'custom-set-face 'face-alias 'custom-set)
1693 (defface custom-changed '((((min-colors 88) (class color))
1694 (:foreground "white" :background "blue1"))
1695 (((class color))
1696 (:foreground "white" :background "blue"))
1698 (:slant italic)))
1699 "Face used when the customize item has been changed."
1700 :group 'custom-magic-faces)
1701 ;; backward-compatibility alias
1702 (put 'custom-changed-face 'face-alias 'custom-changed)
1704 (defface custom-saved '((t (:underline t)))
1705 "Face used when the customize item has been saved."
1706 :group 'custom-magic-faces)
1707 ;; backward-compatibility alias
1708 (put 'custom-saved-face 'face-alias 'custom-saved)
1710 (defconst custom-magic-alist
1711 '((nil "#" underline "\
1712 UNINITIALIZED, you should not see this.")
1713 (unknown "?" italic "\
1714 UNKNOWN, you should not see this.")
1715 (hidden "-" default "\
1716 HIDDEN, invoke \"Show\" in the previous line to show." "\
1717 group now hidden, invoke \"Show\", above, to show contents.")
1718 (invalid "x" custom-invalid "\
1719 INVALID, the displayed value cannot be set.")
1720 (modified "*" custom-modified "\
1721 EDITED, shown value does not take effect until you set or save it." "\
1722 something in this group has been edited but not set.")
1723 (set "+" custom-set "\
1724 SET for current session only." "\
1725 something in this group has been set but not saved.")
1726 (changed ":" custom-changed "\
1727 CHANGED outside Customize; operating on it here may be unreliable." "\
1728 something in this group has been changed outside customize.")
1729 (saved "!" custom-saved "\
1730 SAVED and set." "\
1731 something in this group has been set and saved.")
1732 (rogue "@" custom-rogue "\
1733 NO CUSTOMIZATION DATA; you should not see this." "\
1734 something in this group is not prepared for customization.")
1735 (standard " " nil "\
1736 STANDARD." "\
1737 visible group members are all at standard settings."))
1738 "Alist of customize option states.
1739 Each entry is of the form (STATE MAGIC FACE ITEM-DESC [ GROUP-DESC ]), where
1741 STATE is one of the following symbols:
1743 `nil'
1744 For internal use, should never occur.
1745 `unknown'
1746 For internal use, should never occur.
1747 `hidden'
1748 This item is not being displayed.
1749 `invalid'
1750 This item is modified, but has an invalid form.
1751 `modified'
1752 This item is modified, and has a valid form.
1753 `set'
1754 This item has been set but not saved.
1755 `changed'
1756 The current value of this item has been changed outside Customize.
1757 `saved'
1758 This item is marked for saving.
1759 `rogue'
1760 This item has no customization information.
1761 `standard'
1762 This item is unchanged from the standard setting.
1764 MAGIC is a string used to present that state.
1766 FACE is a face used to present the state.
1768 ITEM-DESC is a string describing the state for options.
1770 GROUP-DESC is a string describing the state for groups. If this is
1771 left out, ITEM-DESC will be used.
1773 The string %c in either description will be replaced with the
1774 category of the item. These are `group'. `option', and `face'.
1776 The list should be sorted most significant first.")
1778 (defcustom custom-magic-show 'long
1779 "If non-nil, show textual description of the state.
1780 If `long', show a full-line description, not just one word."
1781 :type '(choice (const :tag "no" nil)
1782 (const long)
1783 (other :tag "short" short))
1784 :group 'custom-buffer)
1786 (defcustom custom-magic-show-hidden '(option face)
1787 "Control whether the State button is shown for hidden items.
1788 The value should be a list with the custom categories where the State
1789 button should be visible. Possible categories are `group', `option',
1790 and `face'."
1791 :type '(set (const group) (const option) (const face))
1792 :group 'custom-buffer)
1794 (defcustom custom-magic-show-button nil
1795 "Show a \"magic\" button indicating the state of each customization option."
1796 :type 'boolean
1797 :group 'custom-buffer)
1799 (define-widget 'custom-magic 'default
1800 "Show and manipulate state for a customization option."
1801 :format "%v"
1802 :action 'widget-parent-action
1803 :notify 'ignore
1804 :value-get 'ignore
1805 :value-create 'custom-magic-value-create
1806 :value-delete 'widget-children-value-delete)
1808 (defun widget-magic-mouse-down-action (widget &optional event)
1809 ;; Non-nil unless hidden.
1810 (not (eq (widget-get (widget-get (widget-get widget :parent) :parent)
1811 :custom-state)
1812 'hidden)))
1814 (defun custom-magic-value-create (widget)
1815 "Create compact status report for WIDGET."
1816 (let* ((parent (widget-get widget :parent))
1817 (state (widget-get parent :custom-state))
1818 (hidden (eq state 'hidden))
1819 (entry (assq state custom-magic-alist))
1820 (magic (nth 1 entry))
1821 (face (nth 2 entry))
1822 (category (widget-get parent :custom-category))
1823 (text (or (and (eq category 'group)
1824 (nth 4 entry))
1825 (nth 3 entry)))
1826 (form (widget-get parent :custom-form))
1827 children)
1828 (while (string-match "\\`\\(.*\\)%c\\(.*\\)\\'" text)
1829 (setq text (concat (match-string 1 text)
1830 (symbol-name category)
1831 (match-string 2 text))))
1832 (when (and custom-magic-show
1833 (or (not hidden)
1834 (memq category custom-magic-show-hidden)))
1835 (insert " ")
1836 (when (and (eq category 'group)
1837 (not (and (eq custom-buffer-style 'links)
1838 (> (widget-get parent :custom-level) 1))))
1839 (insert-char ?\ (* custom-buffer-indent
1840 (widget-get parent :custom-level))))
1841 (push (widget-create-child-and-convert
1842 widget 'choice-item
1843 :help-echo "Change the state of this item."
1844 :format (if hidden "%t" "%[%t%]")
1845 :button-prefix 'widget-push-button-prefix
1846 :button-suffix 'widget-push-button-suffix
1847 :mouse-down-action 'widget-magic-mouse-down-action
1848 :tag "State")
1849 children)
1850 (insert ": ")
1851 (let ((start (point)))
1852 (if (eq custom-magic-show 'long)
1853 (insert text)
1854 (insert (symbol-name state)))
1855 (cond ((eq form 'lisp)
1856 (insert " (lisp)"))
1857 ((eq form 'mismatch)
1858 (insert " (mismatch)")))
1859 (put-text-property start (point) 'face 'custom-state))
1860 (insert "\n"))
1861 (when (and (eq category 'group)
1862 (not (and (eq custom-buffer-style 'links)
1863 (> (widget-get parent :custom-level) 1))))
1864 (insert-char ?\ (* custom-buffer-indent
1865 (widget-get parent :custom-level))))
1866 (when custom-magic-show-button
1867 (when custom-magic-show
1868 (let ((indent (widget-get parent :indent)))
1869 (when indent
1870 (insert-char ? indent))))
1871 (push (widget-create-child-and-convert
1872 widget 'choice-item
1873 :mouse-down-action 'widget-magic-mouse-down-action
1874 :button-face face
1875 :button-prefix ""
1876 :button-suffix ""
1877 :help-echo "Change the state."
1878 :format (if hidden "%t" "%[%t%]")
1879 :tag (if (memq form '(lisp mismatch))
1880 (concat "(" magic ")")
1881 (concat "[" magic "]")))
1882 children)
1883 (insert " "))
1884 (widget-put widget :children children)))
1886 (defun custom-magic-reset (widget)
1887 "Redraw the :custom-magic property of WIDGET."
1888 (let ((magic (widget-get widget :custom-magic)))
1889 (widget-value-set magic (widget-value magic))))
1891 ;;; The `custom' Widget.
1893 (defface custom-button
1894 '((((type x w32 mac) (class color)) ; Like default modeline
1895 (:box (:line-width 2 :style released-button)
1896 :background "lightgrey" :foreground "black"))
1898 nil))
1899 "Face used for buttons in customization buffers."
1900 :version "21.1"
1901 :group 'custom-faces)
1902 ;; backward-compatibility alias
1903 (put 'custom-button-face 'face-alias 'custom-button)
1905 (defface custom-button-pressed
1906 '((((type x w32 mac) (class color))
1907 (:box (:line-width 2 :style pressed-button)
1908 :background "lightgrey" :foreground "black"))
1910 (:inverse-video t)))
1911 "Face used for buttons in customization buffers."
1912 :version "21.1"
1913 :group 'custom-faces)
1914 ;; backward-compatibility alias
1915 (put 'custom-button-pressed-face 'face-alias 'custom-button-pressed)
1917 (defface custom-documentation nil
1918 "Face used for documentation strings in customization buffers."
1919 :group 'custom-faces)
1920 ;; backward-compatibility alias
1921 (put 'custom-documentation-face 'face-alias 'custom-documentation)
1923 (defface custom-state '((((class color)
1924 (background dark))
1925 (:foreground "lime green"))
1926 (((class color)
1927 (background light))
1928 (:foreground "dark green"))
1929 (t nil))
1930 "Face used for State descriptions in the customize buffer."
1931 :group 'custom-faces)
1932 ;; backward-compatibility alias
1933 (put 'custom-state-face 'face-alias 'custom-state)
1935 (define-widget 'custom 'default
1936 "Customize a user option."
1937 :format "%v"
1938 :convert-widget 'custom-convert-widget
1939 :notify 'custom-notify
1940 :custom-prefix ""
1941 :custom-level 1
1942 :custom-state 'hidden
1943 :documentation-property 'widget-subclass-responsibility
1944 :value-create 'widget-subclass-responsibility
1945 :value-delete 'widget-children-value-delete
1946 :value-get 'widget-value-value-get
1947 :validate 'widget-children-validate
1948 :match (lambda (widget value) (symbolp value)))
1950 (defun custom-convert-widget (widget)
1951 "Initialize :value and :tag from :args in WIDGET."
1952 (let ((args (widget-get widget :args)))
1953 (when args
1954 (widget-put widget :value (widget-apply widget
1955 :value-to-internal (car args)))
1956 (widget-put widget :tag (custom-unlispify-tag-name (car args)))
1957 (widget-put widget :args nil)))
1958 widget)
1960 (defun custom-notify (widget &rest args)
1961 "Keep track of changes."
1962 (let ((state (widget-get widget :custom-state)))
1963 (unless (eq state 'modified)
1964 (unless (memq state '(nil unknown hidden))
1965 (widget-put widget :custom-state 'modified))
1966 (custom-magic-reset widget)
1967 (apply 'widget-default-notify widget args))))
1969 (defun custom-redraw (widget)
1970 "Redraw WIDGET with current settings."
1971 (let ((line (count-lines (point-min) (point)))
1972 (column (current-column))
1973 (pos (point))
1974 (from (marker-position (widget-get widget :from)))
1975 (to (marker-position (widget-get widget :to))))
1976 (save-excursion
1977 (widget-value-set widget (widget-value widget))
1978 (custom-redraw-magic widget))
1979 (when (and (>= pos from) (<= pos to))
1980 (condition-case nil
1981 (progn
1982 (if (> column 0)
1983 (goto-line line)
1984 (goto-line (1+ line)))
1985 (move-to-column column))
1986 (error nil)))))
1988 (defun custom-redraw-magic (widget)
1989 "Redraw WIDGET state with current settings."
1990 (while widget
1991 (let ((magic (widget-get widget :custom-magic)))
1992 (cond (magic
1993 (widget-value-set magic (widget-value magic))
1994 (when (setq widget (widget-get widget :group))
1995 (custom-group-state-update widget)))
1997 (setq widget nil)))))
1998 (widget-setup))
2000 (defun custom-show (widget value)
2001 "Non-nil if WIDGET should be shown with VALUE by default."
2002 (let ((show (widget-get widget :custom-show)))
2003 (cond ((null show)
2004 nil)
2005 ((eq t show)
2008 (funcall show widget value)))))
2010 (defun custom-load-widget (widget)
2011 "Load all dependencies for WIDGET."
2012 (custom-load-symbol (widget-value widget)))
2014 (defun custom-unloaded-symbol-p (symbol)
2015 "Return non-nil if the dependencies of SYMBOL have not yet been loaded."
2016 (let ((found nil)
2017 (loads (get symbol 'custom-loads))
2018 load)
2019 (while loads
2020 (setq load (car loads)
2021 loads (cdr loads))
2022 (cond ((symbolp load)
2023 (unless (featurep load)
2024 (setq found t)))
2025 ((assoc load load-history))
2026 ((assoc (locate-library load) load-history)
2027 (message nil))
2029 (setq found t))))
2030 found))
2032 (defun custom-unloaded-widget-p (widget)
2033 "Return non-nil if the dependencies of WIDGET have not yet been loaded."
2034 (custom-unloaded-symbol-p (widget-value widget)))
2036 (defun custom-toggle-hide (widget)
2037 "Toggle visibility of WIDGET."
2038 (custom-load-widget widget)
2039 (let ((state (widget-get widget :custom-state)))
2040 (cond ((memq state '(invalid modified))
2041 (error "There are unset changes"))
2042 ((eq state 'hidden)
2043 (widget-put widget :custom-state 'unknown))
2045 (widget-put widget :documentation-shown nil)
2046 (widget-put widget :custom-state 'hidden)))
2047 (custom-redraw widget)
2048 (widget-setup)))
2050 (defun custom-toggle-parent (widget &rest ignore)
2051 "Toggle visibility of parent of WIDGET."
2052 (custom-toggle-hide (widget-get widget :parent)))
2054 (defun custom-add-see-also (widget &optional prefix)
2055 "Add `See also ...' to WIDGET if there are any links.
2056 Insert PREFIX first if non-nil."
2057 (let* ((symbol (widget-get widget :value))
2058 (links (get symbol 'custom-links))
2059 (many (> (length links) 2))
2060 (buttons (widget-get widget :buttons))
2061 (indent (widget-get widget :indent)))
2062 (when links
2063 (when indent
2064 (insert-char ?\ indent))
2065 (when prefix
2066 (insert prefix))
2067 (insert "See also ")
2068 (while links
2069 (push (widget-create-child-and-convert widget (car links))
2070 buttons)
2071 (setq links (cdr links))
2072 (cond ((null links)
2073 (insert ".\n"))
2074 ((null (cdr links))
2075 (if many
2076 (insert ", and ")
2077 (insert " and ")))
2079 (insert ", "))))
2080 (widget-put widget :buttons buttons))))
2082 (defun custom-add-parent-links (widget &optional initial-string)
2083 "Add \"Parent groups: ...\" to WIDGET if the group has parents.
2084 The value if non-nil if any parents were found.
2085 If INITIAL-STRING is non-nil, use that rather than \"Parent groups:\"."
2086 (let ((name (widget-value widget))
2087 (type (widget-type widget))
2088 (buttons (widget-get widget :buttons))
2089 (start (point))
2090 (parents nil))
2091 (insert (or initial-string "Parent groups:"))
2092 (mapatoms (lambda (symbol)
2093 (let ((entry (assq name (get symbol 'custom-group))))
2094 (when (eq (nth 1 entry) type)
2095 (insert " ")
2096 (push (widget-create-child-and-convert
2097 widget 'custom-group-link
2098 :tag (custom-unlispify-tag-name symbol)
2099 symbol)
2100 buttons)
2101 (setq parents (cons symbol parents))))))
2102 (and (null (get name 'custom-links)) ;No links of its own.
2103 (= (length parents) 1) ;A single parent.
2104 (let* ((links (get (car parents) 'custom-links))
2105 (many (> (length links) 2)))
2106 (when links
2107 (insert "\nParent documentation: ")
2108 (while links
2109 (push (widget-create-child-and-convert widget (car links))
2110 buttons)
2111 (setq links (cdr links))
2112 (cond ((null links)
2113 (insert ".\n"))
2114 ((null (cdr links))
2115 (if many
2116 (insert ", and ")
2117 (insert " and ")))
2119 (insert ", ")))))))
2120 (if parents
2121 (insert "\n")
2122 (delete-region start (point)))
2123 (widget-put widget :buttons buttons)
2124 parents))
2126 ;;; The `custom-comment' Widget.
2128 ;; like the editable field
2129 (defface custom-comment '((((class grayscale color)
2130 (background light))
2131 (:background "gray85"))
2132 (((class grayscale color)
2133 (background dark))
2134 (:background "dim gray"))
2136 (:slant italic)))
2137 "Face used for comments on variables or faces"
2138 :version "21.1"
2139 :group 'custom-faces)
2140 ;; backward-compatibility alias
2141 (put 'custom-comment-face 'face-alias 'custom-comment)
2143 ;; like font-lock-comment-face
2144 (defface custom-comment-tag
2145 '((((class color) (background dark)) (:foreground "gray80"))
2146 (((class color) (background light)) (:foreground "blue4"))
2147 (((class grayscale) (background light))
2148 (:foreground "DimGray" :weight bold :slant italic))
2149 (((class grayscale) (background dark))
2150 (:foreground "LightGray" :weight bold :slant italic))
2151 (t (:weight bold)))
2152 "Face used for variables or faces comment tags"
2153 :group 'custom-faces)
2154 ;; backward-compatibility alias
2155 (put 'custom-comment-tag-face 'face-alias 'custom-comment-tag)
2157 (define-widget 'custom-comment 'string
2158 "User comment."
2159 :tag "Comment"
2160 :help-echo "Edit a comment here."
2161 :sample-face 'custom-comment-tag-face
2162 :value-face 'custom-comment-face
2163 :shown nil
2164 :create 'custom-comment-create)
2166 (defun custom-comment-create (widget)
2167 (let* ((null-comment (equal "" (widget-value widget))))
2168 (if (or (widget-get (widget-get widget :parent) :comment-shown)
2169 (not null-comment))
2170 (widget-default-create widget)
2171 ;; `widget-default-delete' expects markers in these slots --
2172 ;; maybe it shouldn't.
2173 (widget-put widget :from (point-marker))
2174 (widget-put widget :to (point-marker)))))
2176 (defun custom-comment-hide (widget)
2177 (widget-put (widget-get widget :parent) :comment-shown nil))
2179 ;; Those functions are for the menu. WIDGET is NOT the comment widget. It's
2180 ;; the global custom one
2181 (defun custom-comment-show (widget)
2182 (widget-put widget :comment-shown t)
2183 (custom-redraw widget)
2184 (widget-setup))
2186 (defun custom-comment-invisible-p (widget)
2187 (let ((val (widget-value (widget-get widget :comment-widget))))
2188 (and (equal "" val)
2189 (not (widget-get widget :comment-shown)))))
2191 ;;; The `custom-variable' Widget.
2193 ;; When this was underlined blue, users confused it with a
2194 ;; Mosaic-style hyperlink...
2195 (defface custom-variable-tag
2196 `((((class color)
2197 (background dark))
2198 (:foreground "light blue" :weight bold :height 1.2 :inherit variable-pitch))
2199 (((min-colors 88) (class color)
2200 (background light))
2201 (:foreground "blue1" :weight bold :height 1.2 :inherit variable-pitch))
2202 (((class color)
2203 (background light))
2204 (:foreground "blue" :weight bold :height 1.2 :inherit variable-pitch))
2205 (t (:weight bold)))
2206 "Face used for unpushable variable tags."
2207 :group 'custom-faces)
2208 ;; backward-compatibility alias
2209 (put 'custom-variable-tag-face 'face-alias 'custom-variable-tag)
2211 (defface custom-variable-button '((t (:underline t :weight bold)))
2212 "Face used for pushable variable tags."
2213 :group 'custom-faces)
2214 ;; backward-compatibility alias
2215 (put 'custom-variable-button-face 'face-alias 'custom-variable-button)
2217 (defcustom custom-variable-default-form 'edit
2218 "Default form of displaying variable values."
2219 :type '(choice (const edit)
2220 (const lisp))
2221 :group 'custom-buffer
2222 :version "20.3")
2224 (defun custom-variable-documentation (variable)
2225 "Return documentation of VARIABLE for use in Custom buffer.
2226 Normally just return the docstring. But if VARIABLE automatically
2227 becomes buffer local when set, append a message to that effect."
2228 (if (and (local-variable-if-set-p variable)
2229 (or (not (local-variable-p variable))
2230 (with-temp-buffer
2231 (local-variable-if-set-p variable))))
2232 (concat (documentation-property variable 'variable-documentation)
2234 This variable automatically becomes buffer-local when set outside Custom.
2235 However, setting it through Custom sets the default value.")
2236 (documentation-property variable 'variable-documentation)))
2238 (define-widget 'custom-variable 'custom
2239 "Customize variable."
2240 :format "%v"
2241 :help-echo "Set or reset this variable."
2242 :documentation-property #'custom-variable-documentation
2243 :custom-category 'option
2244 :custom-state nil
2245 :custom-menu 'custom-variable-menu-create
2246 :custom-form nil ; defaults to value of `custom-variable-default-form'
2247 :value-create 'custom-variable-value-create
2248 :action 'custom-variable-action
2249 :custom-set 'custom-variable-set
2250 :custom-save 'custom-variable-save
2251 :custom-reset-current 'custom-redraw
2252 :custom-reset-saved 'custom-variable-reset-saved
2253 :custom-reset-standard 'custom-variable-reset-standard
2254 :custom-standard-value 'custom-variable-standard-value)
2256 (defun custom-variable-type (symbol)
2257 "Return a widget suitable for editing the value of SYMBOL.
2258 If SYMBOL has a `custom-type' property, use that.
2259 Otherwise, look up symbol in `custom-guess-type-alist'."
2260 (let* ((type (or (get symbol 'custom-type)
2261 (and (not (get symbol 'standard-value))
2262 (custom-guess-type symbol))
2263 'sexp))
2264 (options (get symbol 'custom-options))
2265 (tmp (if (listp type)
2266 (copy-sequence type)
2267 (list type))))
2268 (when options
2269 (widget-put tmp :options options))
2270 tmp))
2272 (defun custom-variable-value-create (widget)
2273 "Here is where you edit the variable's value."
2274 (custom-load-widget widget)
2275 (unless (widget-get widget :custom-form)
2276 (widget-put widget :custom-form custom-variable-default-form))
2277 (let* ((buttons (widget-get widget :buttons))
2278 (children (widget-get widget :children))
2279 (form (widget-get widget :custom-form))
2280 (state (widget-get widget :custom-state))
2281 (symbol (widget-get widget :value))
2282 (tag (widget-get widget :tag))
2283 (type (custom-variable-type symbol))
2284 (conv (widget-convert type))
2285 (get (or (get symbol 'custom-get) 'default-value))
2286 (prefix (widget-get widget :custom-prefix))
2287 (last (widget-get widget :custom-last))
2288 (value (if (default-boundp symbol)
2289 (funcall get symbol)
2290 (widget-get conv :value))))
2291 ;; If the widget is new, the child determines whether it is hidden.
2292 (cond (state)
2293 ((custom-show type value)
2294 (setq state 'unknown))
2296 (setq state 'hidden)))
2297 ;; If we don't know the state, see if we need to edit it in lisp form.
2298 (when (eq state 'unknown)
2299 (unless (widget-apply conv :match value)
2300 ;; (widget-apply (widget-convert type) :match value)
2301 (setq form 'mismatch)))
2302 ;; Now we can create the child widget.
2303 (cond ((eq custom-buffer-style 'tree)
2304 (insert prefix (if last " `--- " " |--- "))
2305 (push (widget-create-child-and-convert
2306 widget 'custom-browse-variable-tag)
2307 buttons)
2308 (insert " " tag "\n")
2309 (widget-put widget :buttons buttons))
2310 ((eq state 'hidden)
2311 ;; Indicate hidden value.
2312 (push (widget-create-child-and-convert
2313 widget 'item
2314 :format "%{%t%}: "
2315 :sample-face 'custom-variable-tag-face
2316 :tag tag
2317 :parent widget)
2318 buttons)
2319 (push (widget-create-child-and-convert
2320 widget 'visibility
2321 :help-echo "Show the value of this option."
2322 :off "Show Value"
2323 :action 'custom-toggle-parent
2324 nil)
2325 buttons))
2326 ((memq form '(lisp mismatch))
2327 ;; In lisp mode edit the saved value when possible.
2328 (let* ((value (cond ((get symbol 'saved-value)
2329 (car (get symbol 'saved-value)))
2330 ((get symbol 'standard-value)
2331 (car (get symbol 'standard-value)))
2332 ((default-boundp symbol)
2333 (custom-quote (funcall get symbol)))
2335 (custom-quote (widget-get conv :value))))))
2336 (insert (symbol-name symbol) ": ")
2337 (push (widget-create-child-and-convert
2338 widget 'visibility
2339 :help-echo "Hide the value of this option."
2340 :on "Hide Value"
2341 :off "Show Value"
2342 :action 'custom-toggle-parent
2344 buttons)
2345 (insert " ")
2346 (push (widget-create-child-and-convert
2347 widget 'sexp
2348 :button-face 'custom-variable-button-face
2349 :format "%v"
2350 :tag (symbol-name symbol)
2351 :parent widget
2352 :value value)
2353 children)))
2355 ;; Edit mode.
2356 (let* ((format (widget-get type :format))
2357 tag-format value-format)
2358 (unless (string-match ":" format)
2359 (error "Bad format"))
2360 (setq tag-format (substring format 0 (match-end 0)))
2361 (setq value-format (substring format (match-end 0)))
2362 (push (widget-create-child-and-convert
2363 widget 'item
2364 :format tag-format
2365 :action 'custom-tag-action
2366 :help-echo "Change value of this option."
2367 :mouse-down-action 'custom-tag-mouse-down-action
2368 :button-face 'custom-variable-button-face
2369 :sample-face 'custom-variable-tag-face
2370 tag)
2371 buttons)
2372 (insert " ")
2373 (push (widget-create-child-and-convert
2374 widget 'visibility
2375 :help-echo "Hide the value of this option."
2376 :on "Hide Value"
2377 :off "Show Value"
2378 :action 'custom-toggle-parent
2380 buttons)
2381 (push (widget-create-child-and-convert
2382 widget type
2383 :format value-format
2384 :value value)
2385 children))))
2386 (unless (eq custom-buffer-style 'tree)
2387 (unless (eq (preceding-char) ?\n)
2388 (widget-insert "\n"))
2389 ;; Create the magic button.
2390 (let ((magic (widget-create-child-and-convert
2391 widget 'custom-magic nil)))
2392 (widget-put widget :custom-magic magic)
2393 (push magic buttons))
2394 ;; ### NOTE: this is ugly!!!! I need to update the :buttons property
2395 ;; before the call to `widget-default-format-handler'. Otherwise, I
2396 ;; loose my current `buttons'. This function shouldn't be called like
2397 ;; this anyway. The doc string widget should be added like the others.
2398 ;; --dv
2399 (widget-put widget :buttons buttons)
2400 (insert "\n")
2401 ;; Insert documentation.
2402 (widget-default-format-handler widget ?h)
2404 ;; The comment field
2405 (unless (eq state 'hidden)
2406 (let* ((comment (get symbol 'variable-comment))
2407 (comment-widget
2408 (widget-create-child-and-convert
2409 widget 'custom-comment
2410 :parent widget
2411 :value (or comment ""))))
2412 (widget-put widget :comment-widget comment-widget)
2413 ;; Don't push it !!! Custom assumes that the first child is the
2414 ;; value one.
2415 (setq children (append children (list comment-widget)))))
2416 ;; Update the rest of the properties properties.
2417 (widget-put widget :custom-form form)
2418 (widget-put widget :children children)
2419 ;; Now update the state.
2420 (if (eq state 'hidden)
2421 (widget-put widget :custom-state state)
2422 (custom-variable-state-set widget))
2423 ;; See also.
2424 (unless (eq state 'hidden)
2425 (when (eq (widget-get widget :custom-level) 1)
2426 (custom-add-parent-links widget))
2427 (custom-add-see-also widget)))))
2429 (defun custom-tag-action (widget &rest args)
2430 "Pass :action to first child of WIDGET's parent."
2431 (apply 'widget-apply (car (widget-get (widget-get widget :parent) :children))
2432 :action args))
2434 (defun custom-tag-mouse-down-action (widget &rest args)
2435 "Pass :mouse-down-action to first child of WIDGET's parent."
2436 (apply 'widget-apply (car (widget-get (widget-get widget :parent) :children))
2437 :mouse-down-action args))
2439 (defun custom-variable-state-set (widget)
2440 "Set the state of WIDGET."
2441 (let* ((symbol (widget-value widget))
2442 (get (or (get symbol 'custom-get) 'default-value))
2443 (value (if (default-boundp symbol)
2444 (funcall get symbol)
2445 (widget-get widget :value)))
2446 (comment (get symbol 'variable-comment))
2448 temp
2449 (state (cond ((progn (setq tmp (get symbol 'customized-value))
2450 (setq temp
2451 (get symbol 'customized-variable-comment))
2452 (or tmp temp))
2453 (if (condition-case nil
2454 (and (equal value (eval (car tmp)))
2455 (equal comment temp))
2456 (error nil))
2457 'set
2458 'changed))
2459 ((progn (setq tmp (get symbol 'saved-value))
2460 (setq temp (get symbol 'saved-variable-comment))
2461 (or tmp temp))
2462 (if (condition-case nil
2463 (and (equal value (eval (car tmp)))
2464 (equal comment temp))
2465 (error nil))
2466 'saved
2467 'changed))
2468 ((setq tmp (get symbol 'standard-value))
2469 (if (condition-case nil
2470 (and (equal value (eval (car tmp)))
2471 (equal comment nil))
2472 (error nil))
2473 'standard
2474 'changed))
2475 (t 'rogue))))
2476 (widget-put widget :custom-state state)))
2478 (defun custom-variable-standard-value (widget)
2479 (get (widget-value widget) 'standard-value))
2481 (defvar custom-variable-menu
2482 '(("Set for Current Session" custom-variable-set
2483 (lambda (widget)
2484 (eq (widget-get widget :custom-state) 'modified)))
2485 ("Save for Future Sessions" custom-variable-save
2486 (lambda (widget)
2487 (memq (widget-get widget :custom-state) '(modified set changed rogue))))
2488 ("Reset to Current" custom-redraw
2489 (lambda (widget)
2490 (and (default-boundp (widget-value widget))
2491 (memq (widget-get widget :custom-state) '(modified changed)))))
2492 ("Reset to Saved" custom-variable-reset-saved
2493 (lambda (widget)
2494 (and (or (get (widget-value widget) 'saved-value)
2495 (get (widget-value widget) 'saved-variable-comment))
2496 (memq (widget-get widget :custom-state)
2497 '(modified set changed rogue)))))
2498 ("Erase Customization" custom-variable-reset-standard
2499 (lambda (widget)
2500 (and (get (widget-value widget) 'standard-value)
2501 (memq (widget-get widget :custom-state)
2502 '(modified set changed saved rogue)))))
2503 ("Use Backup Value" custom-variable-reset-backup
2504 (lambda (widget)
2505 (get (widget-value widget) 'backup-value)))
2506 ("---" ignore ignore)
2507 ("Add Comment" custom-comment-show custom-comment-invisible-p)
2508 ("---" ignore ignore)
2509 ("Don't show as Lisp expression" custom-variable-edit
2510 (lambda (widget)
2511 (eq (widget-get widget :custom-form) 'lisp)))
2512 ("Show initial Lisp expression" custom-variable-edit-lisp
2513 (lambda (widget)
2514 (eq (widget-get widget :custom-form) 'edit))))
2515 "Alist of actions for the `custom-variable' widget.
2516 Each entry has the form (NAME ACTION FILTER) where NAME is the name of
2517 the menu entry, ACTION is the function to call on the widget when the
2518 menu is selected, and FILTER is a predicate which takes a `custom-variable'
2519 widget as an argument, and returns non-nil if ACTION is valid on that
2520 widget. If FILTER is nil, ACTION is always valid.")
2522 (defun custom-variable-action (widget &optional event)
2523 "Show the menu for `custom-variable' WIDGET.
2524 Optional EVENT is the location for the menu."
2525 (if (eq (widget-get widget :custom-state) 'hidden)
2526 (custom-toggle-hide widget)
2527 (unless (eq (widget-get widget :custom-state) 'modified)
2528 (custom-variable-state-set widget))
2529 (custom-redraw-magic widget)
2530 (let* ((completion-ignore-case t)
2531 (answer (widget-choose (concat "Operation on "
2532 (custom-unlispify-tag-name
2533 (widget-get widget :value)))
2534 (custom-menu-filter custom-variable-menu
2535 widget)
2536 event)))
2537 (if answer
2538 (funcall answer widget)))))
2540 (defun custom-variable-edit (widget)
2541 "Edit value of WIDGET."
2542 (widget-put widget :custom-state 'unknown)
2543 (widget-put widget :custom-form 'edit)
2544 (custom-redraw widget))
2546 (defun custom-variable-edit-lisp (widget)
2547 "Edit the Lisp representation of the value of WIDGET."
2548 (widget-put widget :custom-state 'unknown)
2549 (widget-put widget :custom-form 'lisp)
2550 (custom-redraw widget))
2552 (defun custom-variable-set (widget)
2553 "Set the current value for the variable being edited by WIDGET."
2554 (let* ((form (widget-get widget :custom-form))
2555 (state (widget-get widget :custom-state))
2556 (child (car (widget-get widget :children)))
2557 (symbol (widget-value widget))
2558 (set (or (get symbol 'custom-set) 'set-default))
2559 (comment-widget (widget-get widget :comment-widget))
2560 (comment (widget-value comment-widget))
2561 val)
2562 (cond ((eq state 'hidden)
2563 (error "Cannot set hidden variable"))
2564 ((setq val (widget-apply child :validate))
2565 (goto-char (widget-get val :from))
2566 (error "%s" (widget-get val :error)))
2567 ((memq form '(lisp mismatch))
2568 (when (equal comment "")
2569 (setq comment nil)
2570 ;; Make the comment invisible by hand if it's empty
2571 (custom-comment-hide comment-widget))
2572 (custom-variable-backup-value widget)
2573 (funcall set symbol (eval (setq val (widget-value child))))
2574 (put symbol 'customized-value (list val))
2575 (put symbol 'variable-comment comment)
2576 (put symbol 'customized-variable-comment comment))
2578 (when (equal comment "")
2579 (setq comment nil)
2580 ;; Make the comment invisible by hand if it's empty
2581 (custom-comment-hide comment-widget))
2582 (custom-variable-backup-value widget)
2583 (funcall set symbol (setq val (widget-value child)))
2584 (put symbol 'customized-value (list (custom-quote val)))
2585 (put symbol 'variable-comment comment)
2586 (put symbol 'customized-variable-comment comment)))
2587 (custom-variable-state-set widget)
2588 (custom-redraw-magic widget)))
2590 (defun custom-variable-save (widget)
2591 "Set and save the value for the variable being edited by WIDGET."
2592 (let* ((form (widget-get widget :custom-form))
2593 (state (widget-get widget :custom-state))
2594 (child (car (widget-get widget :children)))
2595 (symbol (widget-value widget))
2596 (set (or (get symbol 'custom-set) 'set-default))
2597 (comment-widget (widget-get widget :comment-widget))
2598 (comment (widget-value comment-widget))
2599 val)
2600 (cond ((eq state 'hidden)
2601 (error "Cannot set hidden variable"))
2602 ((setq val (widget-apply child :validate))
2603 (goto-char (widget-get val :from))
2604 (error "Saving %s: %s" symbol (widget-get val :error)))
2605 ((memq form '(lisp mismatch))
2606 (when (equal comment "")
2607 (setq comment nil)
2608 ;; Make the comment invisible by hand if it's empty
2609 (custom-comment-hide comment-widget))
2610 (put symbol 'saved-value (list (widget-value child)))
2611 (custom-push-theme 'theme-value symbol 'user
2612 'set (list (widget-value child)))
2613 (funcall set symbol (eval (widget-value child)))
2614 (put symbol 'variable-comment comment)
2615 (put symbol 'saved-variable-comment comment))
2617 (when (equal comment "")
2618 (setq comment nil)
2619 ;; Make the comment invisible by hand if it's empty
2620 (custom-comment-hide comment-widget))
2621 (put symbol 'saved-value
2622 (list (custom-quote (widget-value child))))
2623 (custom-push-theme 'theme-value symbol 'user
2624 'set (list (custom-quote (widget-value
2625 child))))
2626 (funcall set symbol (widget-value child))
2627 (put symbol 'variable-comment comment)
2628 (put symbol 'saved-variable-comment comment)))
2629 (put symbol 'customized-value nil)
2630 (put symbol 'customized-variable-comment nil)
2631 (custom-save-all)
2632 (custom-variable-state-set widget)
2633 (custom-redraw-magic widget)))
2635 (defun custom-variable-reset-saved (widget)
2636 "Restore the saved value for the variable being edited by WIDGET.
2637 The value that was current before this operation
2638 becomes the backup value, so you can get it again."
2639 (let* ((symbol (widget-value widget))
2640 (set (or (get symbol 'custom-set) 'set-default))
2641 (value (get symbol 'saved-value))
2642 (comment (get symbol 'saved-variable-comment)))
2643 (cond ((or value comment)
2644 (put symbol 'variable-comment comment)
2645 (custom-variable-backup-value widget)
2646 (condition-case nil
2647 (funcall set symbol (eval (car value)))
2648 (error nil)))
2650 (error "No saved value for %s" symbol)))
2651 (put symbol 'customized-value nil)
2652 (put symbol 'customized-variable-comment nil)
2653 (widget-put widget :custom-state 'unknown)
2654 ;; This call will possibly make the comment invisible
2655 (custom-redraw widget)))
2657 (defun custom-variable-reset-standard (widget)
2658 "Restore the standard setting for the variable being edited by WIDGET.
2659 This operation eliminates any saved setting for the variable,
2660 restoring it to the state of a variable that has never been customized.
2661 The value that was current before this operation
2662 becomes the backup value, so you can get it again."
2663 (let* ((symbol (widget-value widget))
2664 (set (or (get symbol 'custom-set) 'set-default)))
2665 (if (get symbol 'standard-value)
2666 (progn
2667 (custom-variable-backup-value widget)
2668 (funcall set symbol (eval (car (get symbol 'standard-value)))))
2669 (error "No standard setting known for %S" symbol))
2670 (put symbol 'variable-comment nil)
2671 (put symbol 'customized-value nil)
2672 (put symbol 'customized-variable-comment nil)
2673 (when (or (get symbol 'saved-value) (get symbol 'saved-variable-comment))
2674 (put symbol 'saved-value nil)
2675 (custom-push-theme 'theme-value symbol 'user 'reset 'standard)
2676 ;; As a special optimizations we do not (explictly)
2677 ;; save resets to standard when no theme set the value.
2678 (if (null (cdr (get symbol 'theme-value)))
2679 (put symbol 'theme-value nil))
2680 (put symbol 'saved-variable-comment nil)
2681 (custom-save-all))
2682 (widget-put widget :custom-state 'unknown)
2683 ;; This call will possibly make the comment invisible
2684 (custom-redraw widget)))
2686 (defun custom-variable-backup-value (widget)
2687 "Back up the current value for WIDGET's variable.
2688 The backup value is kept in the car of the `backup-value' property."
2689 (let* ((symbol (widget-value widget))
2690 (get (or (get symbol 'custom-get) 'default-value))
2691 (type (custom-variable-type symbol))
2692 (conv (widget-convert type))
2693 (value (if (default-boundp symbol)
2694 (funcall get symbol)
2695 (widget-get conv :value))))
2696 (put symbol 'backup-value (list value))))
2698 (defun custom-variable-reset-backup (widget)
2699 "Restore the backup value for the variable being edited by WIDGET.
2700 The value that was current before this operation
2701 becomes the backup value, so you can use this operation repeatedly
2702 to switch between two values."
2703 (let* ((symbol (widget-value widget))
2704 (set (or (get symbol 'custom-set) 'set-default))
2705 (value (get symbol 'backup-value))
2706 (comment-widget (widget-get widget :comment-widget))
2707 (comment (widget-value comment-widget)))
2708 (if value
2709 (progn
2710 (custom-variable-backup-value widget)
2711 (condition-case nil
2712 (funcall set symbol (car value))
2713 (error nil)))
2714 (error "No backup value for %s" symbol))
2715 (put symbol 'customized-value (list (car value)))
2716 (put symbol 'variable-comment comment)
2717 (put symbol 'customized-variable-comment comment)
2718 (custom-variable-state-set widget)
2719 ;; This call will possibly make the comment invisible
2720 (custom-redraw widget)))
2722 ;;; The `custom-face-edit' Widget.
2724 (define-widget 'custom-face-edit 'checklist
2725 "Edit face attributes."
2726 :format "%t: %v"
2727 :tag "Attributes"
2728 :extra-offset 13
2729 :button-args '(:help-echo "Control whether this attribute has any effect.")
2730 :value-to-internal 'custom-face-edit-fix-value
2731 :match (lambda (widget value)
2732 (widget-checklist-match widget
2733 (custom-face-edit-fix-value widget value)))
2734 :convert-widget 'custom-face-edit-convert-widget
2735 :args (mapcar (lambda (att)
2736 (list 'group
2737 :inline t
2738 :sibling-args (widget-get (nth 1 att) :sibling-args)
2739 (list 'const :format "" :value (nth 0 att))
2740 (nth 1 att)))
2741 custom-face-attributes))
2743 (defun custom-face-edit-fix-value (widget value)
2744 "Ignoring WIDGET, convert :bold and :italic in VALUE to new form.
2745 Also change :reverse-video to :inverse-video."
2746 (if (listp value)
2747 (let (result)
2748 (while value
2749 (let ((key (car value))
2750 (val (car (cdr value))))
2751 (cond ((eq key :italic)
2752 (push :slant result)
2753 (push (if val 'italic 'normal) result))
2754 ((eq key :bold)
2755 (push :weight result)
2756 (push (if val 'bold 'normal) result))
2757 ((eq key :reverse-video)
2758 (push :inverse-video result)
2759 (push val result))
2761 (push key result)
2762 (push val result))))
2763 (setq value (cdr (cdr value))))
2764 (setq result (nreverse result))
2765 result)
2766 value))
2768 (defun custom-face-edit-convert-widget (widget)
2769 "Convert :args as widget types in WIDGET."
2770 (widget-put
2771 widget
2772 :args (mapcar (lambda (arg)
2773 (widget-convert arg
2774 :deactivate 'custom-face-edit-deactivate
2775 :activate 'custom-face-edit-activate
2776 :delete 'custom-face-edit-delete))
2777 (widget-get widget :args)))
2778 widget)
2780 (defun custom-face-edit-deactivate (widget)
2781 "Make face widget WIDGET inactive for user modifications."
2782 (unless (widget-get widget :inactive)
2783 (let ((tag (custom-face-edit-attribute-tag widget))
2784 (from (copy-marker (widget-get widget :from)))
2785 (value (widget-value widget))
2786 (inhibit-read-only t)
2787 (inhibit-modification-hooks t))
2788 (save-excursion
2789 (goto-char from)
2790 (widget-default-delete widget)
2791 (insert tag ": *\n")
2792 (widget-put widget :inactive
2793 (cons value (cons from (- (point) from))))))))
2795 (defun custom-face-edit-activate (widget)
2796 "Make face widget WIDGET inactive for user modifications."
2797 (let ((inactive (widget-get widget :inactive))
2798 (inhibit-read-only t)
2799 (inhibit-modification-hooks t))
2800 (when (consp inactive)
2801 (save-excursion
2802 (goto-char (car (cdr inactive)))
2803 (delete-region (point) (+ (point) (cdr (cdr inactive))))
2804 (widget-put widget :inactive nil)
2805 (widget-apply widget :create)
2806 (widget-value-set widget (car inactive))
2807 (widget-setup)))))
2809 (defun custom-face-edit-delete (widget)
2810 "Remove WIDGET from the buffer."
2811 (let ((inactive (widget-get widget :inactive))
2812 (inhibit-read-only t)
2813 (inhibit-modification-hooks t))
2814 (if (not inactive)
2815 ;; Widget is alive, we don't have to do anything special
2816 (widget-default-delete widget)
2817 ;; WIDGET is already deleted because we did so to inactivate it;
2818 ;; now just get rid of the label we put in its place.
2819 (delete-region (car (cdr inactive))
2820 (+ (car (cdr inactive)) (cdr (cdr inactive))))
2821 (widget-put widget :inactive nil))))
2824 (defun custom-face-edit-attribute-tag (widget)
2825 "Returns the first :tag property in WIDGET or one of its children."
2826 (let ((tag (widget-get widget :tag)))
2827 (or (and (not (equal tag "")) tag)
2828 (let ((children (widget-get widget :children)))
2829 (while (and (null tag) children)
2830 (setq tag (custom-face-edit-attribute-tag (pop children))))
2831 tag))))
2833 ;;; The `custom-display' Widget.
2835 (define-widget 'custom-display 'menu-choice
2836 "Select a display type."
2837 :tag "Display"
2838 :value t
2839 :help-echo "Specify frames where the face attributes should be used."
2840 :args '((const :tag "all" t)
2841 (const :tag "defaults" default)
2842 (checklist
2843 :offset 0
2844 :extra-offset 9
2845 :args ((group :sibling-args (:help-echo "\
2846 Only match the specified window systems.")
2847 (const :format "Type: "
2848 type)
2849 (checklist :inline t
2850 :offset 0
2851 (const :format "X "
2852 :sibling-args (:help-echo "\
2853 The X11 Window System.")
2855 (const :format "PM "
2856 :sibling-args (:help-echo "\
2857 OS/2 Presentation Manager.")
2859 (const :format "W32 "
2860 :sibling-args (:help-echo "\
2861 Windows NT/9X.")
2862 w32)
2863 (const :format "MAC "
2864 :sibling-args (:help-echo "\
2865 Macintosh OS.")
2866 mac)
2867 (const :format "DOS "
2868 :sibling-args (:help-echo "\
2869 Plain MS-DOS.")
2871 (const :format "TTY%n"
2872 :sibling-args (:help-echo "\
2873 Plain text terminals.")
2874 tty)))
2875 (group :sibling-args (:help-echo "\
2876 Only match the frames with the specified color support.")
2877 (const :format "Class: "
2878 class)
2879 (checklist :inline t
2880 :offset 0
2881 (const :format "Color "
2882 :sibling-args (:help-echo "\
2883 Match color frames.")
2884 color)
2885 (const :format "Grayscale "
2886 :sibling-args (:help-echo "\
2887 Match grayscale frames.")
2888 grayscale)
2889 (const :format "Monochrome%n"
2890 :sibling-args (:help-echo "\
2891 Match frames with no color support.")
2892 mono)))
2893 (group :sibling-args (:help-echo "\
2894 The minimum number of colors the frame should support.")
2895 (const :format "" min-colors)
2896 (integer :tag "Minimum number of colors" ))
2897 (group :sibling-args (:help-echo "\
2898 Only match frames with the specified intensity.")
2899 (const :format "\
2900 Background brightness: "
2901 background)
2902 (checklist :inline t
2903 :offset 0
2904 (const :format "Light "
2905 :sibling-args (:help-echo "\
2906 Match frames with light backgrounds.")
2907 light)
2908 (const :format "Dark\n"
2909 :sibling-args (:help-echo "\
2910 Match frames with dark backgrounds.")
2911 dark)))
2912 (group :sibling-args (:help-echo "\
2913 Only match frames that support the specified face attributes.")
2914 (const :format "Supports attributes:" supports)
2915 (custom-face-edit :inline t :format "%n%v"))))))
2917 ;;; The `custom-face' Widget.
2919 (defface custom-face-tag
2920 `((t (:weight bold :height 1.2 :inherit variable-pitch)))
2921 "Face used for face tags."
2922 :group 'custom-faces)
2923 ;; backward-compatibility alias
2924 (put 'custom-face-tag-face 'face-alias 'custom-face-tag)
2926 (defcustom custom-face-default-form 'selected
2927 "Default form of displaying face definition."
2928 :type '(choice (const all)
2929 (const selected)
2930 (const lisp))
2931 :group 'custom-buffer
2932 :version "20.3")
2934 (define-widget 'custom-face 'custom
2935 "Customize face."
2936 :sample-face 'custom-face-tag-face
2937 :help-echo "Set or reset this face."
2938 :documentation-property #'face-doc-string
2939 :value-create 'custom-face-value-create
2940 :action 'custom-face-action
2941 :custom-category 'face
2942 :custom-form nil ; defaults to value of `custom-face-default-form'
2943 :custom-set 'custom-face-set
2944 :custom-save 'custom-face-save
2945 :custom-reset-current 'custom-redraw
2946 :custom-reset-saved 'custom-face-reset-saved
2947 :custom-reset-standard 'custom-face-reset-standard
2948 :custom-standard-value 'custom-face-standard-value
2949 :custom-menu 'custom-face-menu-create)
2951 (define-widget 'custom-face-all 'editable-list
2952 "An editable list of display specifications and attributes."
2953 :entry-format "%i %d %v"
2954 :insert-button-args '(:help-echo "Insert new display specification here.")
2955 :append-button-args '(:help-echo "Append new display specification here.")
2956 :delete-button-args '(:help-echo "Delete this display specification.")
2957 :args '((group :format "%v" custom-display custom-face-edit)))
2959 (defconst custom-face-all (widget-convert 'custom-face-all)
2960 "Converted version of the `custom-face-all' widget.")
2962 (define-widget 'custom-display-unselected 'item
2963 "A display specification that doesn't match the selected display."
2964 :match 'custom-display-unselected-match)
2966 (defun custom-display-unselected-match (widget value)
2967 "Non-nil if VALUE is an unselected display specification."
2968 (not (face-spec-set-match-display value (selected-frame))))
2970 (define-widget 'custom-face-selected 'group
2971 "Edit the attributes of the selected display in a face specification."
2972 :args '((choice :inline t
2973 (group :tag "With Defaults" :inline t
2974 (group (const :tag "" default)
2975 (custom-face-edit :tag " Default\n Attributes"))
2976 (repeat :format ""
2977 :inline t
2978 (group custom-display-unselected sexp))
2979 (group (sexp :format "")
2980 (custom-face-edit :tag " Overriding\n Attributes"))
2981 (repeat :format ""
2982 :inline t
2983 sexp))
2984 (group :tag "No Defaults" :inline t
2985 (repeat :format ""
2986 :inline t
2987 (group custom-display-unselected sexp))
2988 (group (sexp :format "")
2989 (custom-face-edit :tag "\n Attributes"))
2990 (repeat :format ""
2991 :inline t
2992 sexp)))))
2996 (defconst custom-face-selected (widget-convert 'custom-face-selected)
2997 "Converted version of the `custom-face-selected' widget.")
2999 (defun custom-filter-face-spec (spec filter-index &optional default-filter)
3000 "Return a canonicalized version of SPEC using.
3001 FILTER-INDEX is the index in the entry for each attribute in
3002 `custom-face-attributes' at which the appropriate filter function can be
3003 found, and DEFAULT-FILTER is the filter to apply for attributes that
3004 don't specify one."
3005 (mapcar (lambda (entry)
3006 ;; Filter a single face-spec entry
3007 (let ((tests (car entry))
3008 (unfiltered-attrs
3009 ;; Handle both old- and new-style attribute syntax
3010 (if (listp (car (cdr entry)))
3011 (car (cdr entry))
3012 (cdr entry)))
3013 (filtered-attrs nil))
3014 ;; Filter each face attribute
3015 (while unfiltered-attrs
3016 (let* ((attr (pop unfiltered-attrs))
3017 (pre-filtered-value (pop unfiltered-attrs))
3018 (filter
3019 (or (nth filter-index (assq attr custom-face-attributes))
3020 default-filter))
3021 (filtered-value
3022 (if filter
3023 (funcall filter pre-filtered-value)
3024 pre-filtered-value)))
3025 (push filtered-value filtered-attrs)
3026 (push attr filtered-attrs)))
3028 (list tests filtered-attrs)))
3029 spec))
3031 (defun custom-pre-filter-face-spec (spec)
3032 "Return SPEC changed as necessary for editing by the face customization widget.
3033 SPEC must be a full face spec."
3034 (custom-filter-face-spec spec 2))
3036 (defun custom-post-filter-face-spec (spec)
3037 "Return the customized SPEC in a form suitable for setting the face."
3038 (custom-filter-face-spec spec 3))
3040 (defun custom-face-value-create (widget)
3041 "Create a list of the display specifications for WIDGET."
3042 (let ((buttons (widget-get widget :buttons))
3043 children
3044 (symbol (widget-get widget :value))
3045 (tag (widget-get widget :tag))
3046 (state (widget-get widget :custom-state))
3047 (begin (point))
3048 (is-last (widget-get widget :custom-last))
3049 (prefix (widget-get widget :custom-prefix)))
3050 (unless tag
3051 (setq tag (prin1-to-string symbol)))
3052 (cond ((eq custom-buffer-style 'tree)
3053 (insert prefix (if is-last " `--- " " |--- "))
3054 (push (widget-create-child-and-convert
3055 widget 'custom-browse-face-tag)
3056 buttons)
3057 (insert " " tag "\n")
3058 (widget-put widget :buttons buttons))
3060 ;; Create tag.
3061 (insert tag)
3062 (widget-specify-sample widget begin (point))
3063 (if (eq custom-buffer-style 'face)
3064 (insert " ")
3065 (if (string-match "face\\'" tag)
3066 (insert ":")
3067 (insert " face: ")))
3068 ;; Sample.
3069 (push (widget-create-child-and-convert widget 'item
3070 :format "(%{%t%})"
3071 :sample-face symbol
3072 :tag "sample")
3073 buttons)
3074 ;; Visibility.
3075 (insert " ")
3076 (push (widget-create-child-and-convert
3077 widget 'visibility
3078 :help-echo "Hide or show this face."
3079 :on "Hide Face"
3080 :off "Show Face"
3081 :action 'custom-toggle-parent
3082 (not (eq state 'hidden)))
3083 buttons)
3084 ;; Magic.
3085 (insert "\n")
3086 (let ((magic (widget-create-child-and-convert
3087 widget 'custom-magic nil)))
3088 (widget-put widget :custom-magic magic)
3089 (push magic buttons))
3090 ;; Update buttons.
3091 (widget-put widget :buttons buttons)
3092 ;; Insert documentation.
3093 (widget-default-format-handler widget ?h)
3094 ;; The comment field
3095 (unless (eq state 'hidden)
3096 (let* ((comment (get symbol 'face-comment))
3097 (comment-widget
3098 (widget-create-child-and-convert
3099 widget 'custom-comment
3100 :parent widget
3101 :value (or comment ""))))
3102 (widget-put widget :comment-widget comment-widget)
3103 (push comment-widget children)))
3104 ;; See also.
3105 (unless (eq state 'hidden)
3106 (when (eq (widget-get widget :custom-level) 1)
3107 (custom-add-parent-links widget))
3108 (custom-add-see-also widget))
3109 ;; Editor.
3110 (unless (eq (preceding-char) ?\n)
3111 (insert "\n"))
3112 (unless (eq state 'hidden)
3113 (message "Creating face editor...")
3114 (custom-load-widget widget)
3115 (unless (widget-get widget :custom-form)
3116 (widget-put widget :custom-form custom-face-default-form))
3117 (let* ((symbol (widget-value widget))
3118 (spec (or (get symbol 'customized-face)
3119 (get symbol 'saved-face)
3120 (get symbol 'face-defface-spec)
3121 ;; Attempt to construct it.
3122 (list (list t (custom-face-attributes-get
3123 symbol (selected-frame))))))
3124 (form (widget-get widget :custom-form))
3125 (indent (widget-get widget :indent))
3126 edit)
3127 ;; If the user has changed this face in some other way,
3128 ;; edit it as the user has specified it.
3129 (if (not (face-spec-match-p symbol spec (selected-frame)))
3130 (setq spec (list (list t (face-attr-construct symbol (selected-frame))))))
3131 (setq spec (custom-pre-filter-face-spec spec))
3132 (setq edit (widget-create-child-and-convert
3133 widget
3134 (cond ((and (eq form 'selected)
3135 (widget-apply custom-face-selected
3136 :match spec))
3137 (when indent (insert-char ?\ indent))
3138 'custom-face-selected)
3139 ((and (not (eq form 'lisp))
3140 (widget-apply custom-face-all
3141 :match spec))
3142 'custom-face-all)
3144 (when indent (insert-char ?\ indent))
3145 'sexp))
3146 :value spec))
3147 (custom-face-state-set widget)
3148 (push edit children)
3149 (widget-put widget :children children))
3150 (message "Creating face editor...done"))))))
3152 (defvar custom-face-menu
3153 '(("Set for Current Session" custom-face-set)
3154 ("Save for Future Sessions" custom-face-save-command)
3155 ("Reset to Saved" custom-face-reset-saved
3156 (lambda (widget)
3157 (or (get (widget-value widget) 'saved-face)
3158 (get (widget-value widget) 'saved-face-comment))))
3159 ("Erase Customization" custom-face-reset-standard
3160 (lambda (widget)
3161 (get (widget-value widget) 'face-defface-spec)))
3162 ("---" ignore ignore)
3163 ("Add Comment" custom-comment-show custom-comment-invisible-p)
3164 ("---" ignore ignore)
3165 ("Show all display specs" custom-face-edit-all
3166 (lambda (widget)
3167 (not (eq (widget-get widget :custom-form) 'all))))
3168 ("Just current attributes" custom-face-edit-selected
3169 (lambda (widget)
3170 (not (eq (widget-get widget :custom-form) 'selected))))
3171 ("Show as Lisp expression" custom-face-edit-lisp
3172 (lambda (widget)
3173 (not (eq (widget-get widget :custom-form) 'lisp)))))
3174 "Alist of actions for the `custom-face' widget.
3175 Each entry has the form (NAME ACTION FILTER) where NAME is the name of
3176 the menu entry, ACTION is the function to call on the widget when the
3177 menu is selected, and FILTER is a predicate which takes a `custom-face'
3178 widget as an argument, and returns non-nil if ACTION is valid on that
3179 widget. If FILTER is nil, ACTION is always valid.")
3181 (defun custom-face-edit-selected (widget)
3182 "Edit selected attributes of the value of WIDGET."
3183 (widget-put widget :custom-state 'unknown)
3184 (widget-put widget :custom-form 'selected)
3185 (custom-redraw widget))
3187 (defun custom-face-edit-all (widget)
3188 "Edit all attributes of the value of WIDGET."
3189 (widget-put widget :custom-state 'unknown)
3190 (widget-put widget :custom-form 'all)
3191 (custom-redraw widget))
3193 (defun custom-face-edit-lisp (widget)
3194 "Edit the Lisp representation of the value of WIDGET."
3195 (widget-put widget :custom-state 'unknown)
3196 (widget-put widget :custom-form 'lisp)
3197 (custom-redraw widget))
3199 (defun custom-face-state-set (widget)
3200 "Set the state of WIDGET."
3201 (let* ((symbol (widget-value widget))
3202 (comment (get symbol 'face-comment))
3203 tmp temp
3204 (state
3205 (cond ((progn
3206 (setq tmp (get symbol 'customized-face))
3207 (setq temp (get symbol 'customized-face-comment))
3208 (or tmp temp))
3209 (if (equal temp comment)
3210 'set
3211 'changed))
3212 ((progn
3213 (setq tmp (get symbol 'saved-face))
3214 (setq temp (get symbol 'saved-face-comment))
3215 (or tmp temp))
3216 (if (equal temp comment)
3217 'saved
3218 'changed))
3219 ((get symbol 'face-defface-spec)
3220 (if (equal comment nil)
3221 'standard
3222 'changed))
3224 'rogue))))
3225 ;; If the user called set-face-attribute to change the default
3226 ;; for new frames, this face is "set outside of Customize".
3227 (if (and (not (eq state 'rogue))
3228 (get symbol 'face-modified))
3229 (setq state 'changed))
3230 (widget-put widget :custom-state state)))
3232 (defun custom-face-action (widget &optional event)
3233 "Show the menu for `custom-face' WIDGET.
3234 Optional EVENT is the location for the menu."
3235 (if (eq (widget-get widget :custom-state) 'hidden)
3236 (custom-toggle-hide widget)
3237 (let* ((completion-ignore-case t)
3238 (symbol (widget-get widget :value))
3239 (answer (widget-choose (concat "Operation on "
3240 (custom-unlispify-tag-name symbol))
3241 (custom-menu-filter custom-face-menu
3242 widget)
3243 event)))
3244 (if answer
3245 (funcall answer widget)))))
3247 (defun custom-face-set (widget)
3248 "Make the face attributes in WIDGET take effect."
3249 (let* ((symbol (widget-value widget))
3250 (child (car (widget-get widget :children)))
3251 (value (custom-post-filter-face-spec (widget-value child)))
3252 (comment-widget (widget-get widget :comment-widget))
3253 (comment (widget-value comment-widget)))
3254 (when (equal comment "")
3255 (setq comment nil)
3256 ;; Make the comment invisible by hand if it's empty
3257 (custom-comment-hide comment-widget))
3258 (put symbol 'customized-face value)
3259 (if (face-spec-choose value)
3260 (face-spec-set symbol value)
3261 ;; face-set-spec ignores empty attribute lists, so just give it
3262 ;; something harmless instead.
3263 (face-spec-set symbol '((t :foreground unspecified))))
3264 (put symbol 'customized-face-comment comment)
3265 (put symbol 'face-comment comment)
3266 (custom-face-state-set widget)
3267 (custom-redraw-magic widget)))
3269 (defun custom-face-save-command (widget)
3270 "Save in `.emacs' the face attributes in WIDGET."
3271 (custom-face-save widget)
3272 (custom-save-all))
3274 (defun custom-face-save (widget)
3275 "Prepare for saving WIDGET's face attributes, but don't write `.emacs'."
3276 (let* ((symbol (widget-value widget))
3277 (child (car (widget-get widget :children)))
3278 (value (custom-post-filter-face-spec (widget-value child)))
3279 (comment-widget (widget-get widget :comment-widget))
3280 (comment (widget-value comment-widget)))
3281 (when (equal comment "")
3282 (setq comment nil)
3283 ;; Make the comment invisible by hand if it's empty
3284 (custom-comment-hide comment-widget))
3285 (if (face-spec-choose value)
3286 (face-spec-set symbol value)
3287 ;; face-set-spec ignores empty attribute lists, so just give it
3288 ;; something harmless instead.
3289 (face-spec-set symbol '((t :foreground unspecified))))
3290 (unless (eq (widget-get widget :custom-state) 'standard)
3291 (put symbol 'saved-face value))
3292 (custom-push-theme 'theme-face symbol 'user 'set value)
3293 (put symbol 'customized-face nil)
3294 (put symbol 'face-comment comment)
3295 (put symbol 'customized-face-comment nil)
3296 (put symbol 'saved-face-comment comment)
3297 (custom-save-all)
3298 (custom-face-state-set widget)
3299 (custom-redraw-magic widget)))
3301 (defun custom-face-reset-saved (widget)
3302 "Restore WIDGET to the face's default attributes."
3303 (let* ((symbol (widget-value widget))
3304 (child (car (widget-get widget :children)))
3305 (value (get symbol 'saved-face))
3306 (comment (get symbol 'saved-face-comment))
3307 (comment-widget (widget-get widget :comment-widget)))
3308 (unless (or value comment)
3309 (error "No saved value for this face"))
3310 (put symbol 'customized-face nil)
3311 (put symbol 'customized-face-comment nil)
3312 (face-spec-set symbol value)
3313 (put symbol 'face-comment comment)
3314 (widget-value-set child value)
3315 ;; This call manages the comment visibility
3316 (widget-value-set comment-widget (or comment ""))
3317 (custom-face-state-set widget)
3318 (custom-redraw-magic widget)))
3320 (defun custom-face-standard-value (widget)
3321 (get (widget-value widget) 'face-defface-spec))
3323 (defun custom-face-reset-standard (widget)
3324 "Restore WIDGET to the face's standard settings.
3325 This operation eliminates any saved setting for the face,
3326 restoring it to the state of a face that has never been customized."
3327 (let* ((symbol (widget-value widget))
3328 (child (car (widget-get widget :children)))
3329 (value (get symbol 'face-defface-spec))
3330 (comment-widget (widget-get widget :comment-widget)))
3331 (unless value
3332 (error "No standard setting for this face"))
3333 (put symbol 'customized-face nil)
3334 (put symbol 'customized-face-comment nil)
3335 (when (or (get symbol 'saved-face) (get symbol 'saved-face-comment))
3336 (put symbol 'saved-face nil)
3337 (custom-push-theme 'theme-face symbol 'user 'reset 'standard)
3338 ;; Do not explictly save resets to standards without themes.
3339 (if (null (cdr (get symbol 'theme-face)))
3340 (put symbol 'theme-face nil))
3341 (put symbol 'saved-face-comment nil)
3342 (custom-save-all))
3343 (face-spec-set symbol value)
3344 (put symbol 'face-comment nil)
3345 (widget-value-set child value)
3346 ;; This call manages the comment visibility
3347 (widget-value-set comment-widget "")
3348 (custom-face-state-set widget)
3349 (custom-redraw-magic widget)))
3351 ;;; The `face' Widget.
3353 (defvar widget-face-prompt-value-history nil
3354 "History of input to `widget-face-prompt-value'.")
3356 (define-widget 'face 'symbol
3357 "A Lisp face name (with sample)."
3358 :format "%t: (%{sample%}) %v"
3359 :tag "Face"
3360 :value 'default
3361 :sample-face-get 'widget-face-sample-face-get
3362 :notify 'widget-face-notify
3363 :match (lambda (widget value) (facep value))
3364 :complete-function (lambda ()
3365 (interactive)
3366 (lisp-complete-symbol 'facep))
3367 :prompt-match 'facep
3368 :prompt-history 'widget-face-prompt-value-history
3369 :validate (lambda (widget)
3370 (unless (facep (widget-value widget))
3371 (widget-put widget
3372 :error (format "Invalid face: %S"
3373 (widget-value widget)))
3374 widget)))
3376 (defun widget-face-sample-face-get (widget)
3377 (let ((value (widget-value widget)))
3378 (if (facep value)
3379 value
3380 'default)))
3382 (defun widget-face-notify (widget child &optional event)
3383 "Update the sample, and notify the parent."
3384 (overlay-put (widget-get widget :sample-overlay)
3385 'face (widget-apply widget :sample-face-get))
3386 (widget-default-notify widget child event))
3389 ;;; The `hook' Widget.
3391 (define-widget 'hook 'list
3392 "A emacs lisp hook"
3393 :value-to-internal (lambda (widget value)
3394 (if (and value (symbolp value))
3395 (list value)
3396 value))
3397 :match (lambda (widget value)
3398 (or (symbolp value)
3399 (widget-group-match widget value)))
3400 ;; Avoid adding undefined functions to the hook, especially for
3401 ;; things like `find-file-hook' or even more basic ones, to avoid
3402 ;; chaos.
3403 :set (lambda (symbol value)
3404 (dolist (elt value)
3405 (if (fboundp elt)
3406 (add-hook symbol elt))))
3407 :convert-widget 'custom-hook-convert-widget
3408 :tag "Hook")
3410 (defun custom-hook-convert-widget (widget)
3411 ;; Handle `:options'.
3412 (let* ((options (widget-get widget :options))
3413 (other `(editable-list :inline t
3414 :entry-format "%i %d%v"
3415 (function :format " %v")))
3416 (args (if options
3417 (list `(checklist :inline t
3418 ,@(mapcar (lambda (entry)
3419 `(function-item ,entry))
3420 options))
3421 other)
3422 (list other))))
3423 (widget-put widget :args args)
3424 widget))
3426 ;;; The `custom-group-link' Widget.
3428 (define-widget 'custom-group-link 'link
3429 "Show parent in other window when activated."
3430 :help-echo "Create customization buffer for this group."
3431 :action 'custom-group-link-action)
3433 (defun custom-group-link-action (widget &rest ignore)
3434 (customize-group (widget-value widget)))
3436 ;;; The `custom-group' Widget.
3438 (defcustom custom-group-tag-faces nil
3439 ;; In XEmacs, this ought to play games with font size.
3440 ;; Fixme: make it do so in Emacs.
3441 "Face used for group tags.
3442 The first member is used for level 1 groups, the second for level 2,
3443 and so forth. The remaining group tags are shown with `custom-group-tag'."
3444 :type '(repeat face)
3445 :group 'custom-faces)
3447 (defface custom-group-tag-1
3448 `((((class color)
3449 (background dark))
3450 (:foreground "pink" :weight bold :height 1.2 :inherit variable-pitch))
3451 (((min-colors 88) (class color)
3452 (background light))
3453 (:foreground "red1" :weight bold :height 1.2 :inherit variable-pitch))
3454 (((class color)
3455 (background light))
3456 (:foreground "red" :weight bold :height 1.2 :inherit variable-pitch))
3457 (t (:weight bold)))
3458 "Face used for group tags."
3459 :group 'custom-faces)
3460 ;; backward-compatibility alias
3461 (put 'custom-group-tag-face-1 'face-alias 'custom-group-tag-1)
3463 (defface custom-group-tag
3464 `((((class color)
3465 (background dark))
3466 (:foreground "light blue" :weight bold :height 1.2))
3467 (((min-colors 88) (class color)
3468 (background light))
3469 (:foreground "blue1" :weight bold :height 1.2))
3470 (((class color)
3471 (background light))
3472 (:foreground "blue" :weight bold :height 1.2))
3473 (t (:weight bold)))
3474 "Face used for low level group tags."
3475 :group 'custom-faces)
3476 ;; backward-compatibility alias
3477 (put 'custom-group-tag-face 'face-alias 'custom-group-tag)
3479 (define-widget 'custom-group 'custom
3480 "Customize group."
3481 :format "%v"
3482 :sample-face-get 'custom-group-sample-face-get
3483 :documentation-property 'group-documentation
3484 :help-echo "Set or reset all members of this group."
3485 :value-create 'custom-group-value-create
3486 :action 'custom-group-action
3487 :custom-category 'group
3488 :custom-set 'custom-group-set
3489 :custom-save 'custom-group-save
3490 :custom-reset-current 'custom-group-reset-current
3491 :custom-reset-saved 'custom-group-reset-saved
3492 :custom-reset-standard 'custom-group-reset-standard
3493 :custom-menu 'custom-group-menu-create)
3495 (defun custom-group-sample-face-get (widget)
3496 ;; Use :sample-face.
3497 (or (nth (1- (widget-get widget :custom-level)) custom-group-tag-faces)
3498 'custom-group-tag))
3500 (define-widget 'custom-group-visibility 'visibility
3501 "An indicator and manipulator for hidden group contents."
3502 :create 'custom-group-visibility-create)
3504 (defun custom-group-visibility-create (widget)
3505 (let ((visible (widget-value widget)))
3506 (if visible
3507 (insert "--------")))
3508 (widget-default-create widget))
3510 (defun custom-group-members (symbol groups-only)
3511 "Return SYMBOL's custom group members.
3512 If GROUPS-ONLY non-nil, return only those members that are groups."
3513 (if (not groups-only)
3514 (get symbol 'custom-group)
3515 (let (members)
3516 (dolist (entry (get symbol 'custom-group))
3517 (when (eq (nth 1 entry) 'custom-group)
3518 (push entry members)))
3519 (nreverse members))))
3521 (defun custom-group-value-create (widget)
3522 "Insert a customize group for WIDGET in the current buffer."
3523 (unless (eq (widget-get widget :custom-state) 'hidden)
3524 (custom-load-widget widget))
3525 (let* ((state (widget-get widget :custom-state))
3526 (level (widget-get widget :custom-level))
3527 ;; (indent (widget-get widget :indent))
3528 (prefix (widget-get widget :custom-prefix))
3529 (buttons (widget-get widget :buttons))
3530 (tag (widget-get widget :tag))
3531 (symbol (widget-value widget))
3532 (members (custom-group-members symbol
3533 (and (eq custom-buffer-style 'tree)
3534 custom-browse-only-groups))))
3535 (cond ((and (eq custom-buffer-style 'tree)
3536 (eq state 'hidden)
3537 (or members (custom-unloaded-widget-p widget)))
3538 (custom-browse-insert-prefix prefix)
3539 (push (widget-create-child-and-convert
3540 widget 'custom-browse-visibility
3541 ;; :tag-glyph "plus"
3542 :tag "+")
3543 buttons)
3544 (insert "-- ")
3545 ;; (widget-glyph-insert nil "-- " "horizontal")
3546 (push (widget-create-child-and-convert
3547 widget 'custom-browse-group-tag)
3548 buttons)
3549 (insert " " tag "\n")
3550 (widget-put widget :buttons buttons))
3551 ((and (eq custom-buffer-style 'tree)
3552 (zerop (length members)))
3553 (custom-browse-insert-prefix prefix)
3554 (insert "[ ]-- ")
3555 ;; (widget-glyph-insert nil "[ ]" "empty")
3556 ;; (widget-glyph-insert nil "-- " "horizontal")
3557 (push (widget-create-child-and-convert
3558 widget 'custom-browse-group-tag)
3559 buttons)
3560 (insert " " tag "\n")
3561 (widget-put widget :buttons buttons))
3562 ((eq custom-buffer-style 'tree)
3563 (custom-browse-insert-prefix prefix)
3564 (if (zerop (length members))
3565 (progn
3566 (custom-browse-insert-prefix prefix)
3567 (insert "[ ]-- ")
3568 ;; (widget-glyph-insert nil "[ ]" "empty")
3569 ;; (widget-glyph-insert nil "-- " "horizontal")
3570 (push (widget-create-child-and-convert
3571 widget 'custom-browse-group-tag)
3572 buttons)
3573 (insert " " tag "\n")
3574 (widget-put widget :buttons buttons))
3575 (push (widget-create-child-and-convert
3576 widget 'custom-browse-visibility
3577 ;; :tag-glyph "minus"
3578 :tag "-")
3579 buttons)
3580 (insert "-\\ ")
3581 ;; (widget-glyph-insert nil "-\\ " "top")
3582 (push (widget-create-child-and-convert
3583 widget 'custom-browse-group-tag)
3584 buttons)
3585 (insert " " tag "\n")
3586 (widget-put widget :buttons buttons)
3587 (message "Creating group...")
3588 (let* ((members (custom-sort-items members
3589 custom-browse-sort-alphabetically
3590 custom-browse-order-groups))
3591 (prefixes (widget-get widget :custom-prefixes))
3592 (custom-prefix-list (custom-prefix-add symbol prefixes))
3593 (extra-prefix (if (widget-get widget :custom-last)
3595 " | "))
3596 (prefix (concat prefix extra-prefix))
3597 children entry)
3598 (while members
3599 (setq entry (car members)
3600 members (cdr members))
3601 (push (widget-create-child-and-convert
3602 widget (nth 1 entry)
3603 :group widget
3604 :tag (custom-unlispify-tag-name (nth 0 entry))
3605 :custom-prefixes custom-prefix-list
3606 :custom-level (1+ level)
3607 :custom-last (null members)
3608 :value (nth 0 entry)
3609 :custom-prefix prefix)
3610 children))
3611 (widget-put widget :children (reverse children)))
3612 (message "Creating group...done")))
3613 ;; Nested style.
3614 ((eq state 'hidden)
3615 ;; Create level indicator.
3616 (unless (eq custom-buffer-style 'links)
3617 (insert-char ?\ (* custom-buffer-indent (1- level)))
3618 (insert "-- "))
3619 ;; Create tag.
3620 (let ((begin (point)))
3621 (insert tag)
3622 (widget-specify-sample widget begin (point)))
3623 (insert " group: ")
3624 ;; Create link/visibility indicator.
3625 (if (eq custom-buffer-style 'links)
3626 (push (widget-create-child-and-convert
3627 widget 'custom-group-link
3628 :tag "Go to Group"
3629 symbol)
3630 buttons)
3631 (push (widget-create-child-and-convert
3632 widget 'custom-group-visibility
3633 :help-echo "Show members of this group."
3634 :action 'custom-toggle-parent
3635 (not (eq state 'hidden)))
3636 buttons))
3637 (insert " \n")
3638 ;; Create magic button.
3639 (let ((magic (widget-create-child-and-convert
3640 widget 'custom-magic nil)))
3641 (widget-put widget :custom-magic magic)
3642 (push magic buttons))
3643 ;; Update buttons.
3644 (widget-put widget :buttons buttons)
3645 ;; Insert documentation.
3646 (if (and (eq custom-buffer-style 'links) (> level 1))
3647 (widget-put widget :documentation-indent 0))
3648 (widget-default-format-handler widget ?h))
3649 ;; Nested style.
3650 (t ;Visible.
3651 ;; Add parent groups references above the group.
3652 (if t ;;; This should test that the buffer
3653 ;;; was made to display a group.
3654 (when (eq level 1)
3655 (if (custom-add-parent-links widget
3656 "Go to parent group:")
3657 (insert "\n"))))
3658 ;; Create level indicator.
3659 (insert-char ?\ (* custom-buffer-indent (1- level)))
3660 (insert "/- ")
3661 ;; Create tag.
3662 (let ((start (point)))
3663 (insert tag)
3664 (widget-specify-sample widget start (point)))
3665 (insert " group: ")
3666 ;; Create visibility indicator.
3667 (unless (eq custom-buffer-style 'links)
3668 (insert "--------")
3669 (push (widget-create-child-and-convert
3670 widget 'visibility
3671 :help-echo "Hide members of this group."
3672 :action 'custom-toggle-parent
3673 (not (eq state 'hidden)))
3674 buttons)
3675 (insert " "))
3676 ;; Create more dashes.
3677 ;; Use 76 instead of 75 to compensate for the temporary "<"
3678 ;; added by `widget-insert'.
3679 (insert-char ?- (- 76 (current-column)
3680 (* custom-buffer-indent level)))
3681 (insert "\\\n")
3682 ;; Create magic button.
3683 (let ((magic (widget-create-child-and-convert
3684 widget 'custom-magic
3685 :indent 0
3686 nil)))
3687 (widget-put widget :custom-magic magic)
3688 (push magic buttons))
3689 ;; Update buttons.
3690 (widget-put widget :buttons buttons)
3691 ;; Insert documentation.
3692 (widget-default-format-handler widget ?h)
3693 ;; Parent groups.
3694 (if nil ;;; This should test that the buffer
3695 ;;; was not made to display a group.
3696 (when (eq level 1)
3697 (insert-char ?\ custom-buffer-indent)
3698 (custom-add-parent-links widget)))
3699 (custom-add-see-also widget
3700 (make-string (* custom-buffer-indent level)
3701 ?\ ))
3702 ;; Members.
3703 (message "Creating group...")
3704 (let* ((members (custom-sort-items members
3705 custom-buffer-sort-alphabetically
3706 custom-buffer-order-groups))
3707 (prefixes (widget-get widget :custom-prefixes))
3708 (custom-prefix-list (custom-prefix-add symbol prefixes))
3709 (length (length members))
3710 (count 0)
3711 (children (mapcar (lambda (entry)
3712 (widget-insert "\n")
3713 (message "\
3714 Creating group members... %2d%%"
3715 (/ (* 100.0 count) length))
3716 (setq count (1+ count))
3717 (prog1
3718 (widget-create-child-and-convert
3719 widget (nth 1 entry)
3720 :group widget
3721 :tag (custom-unlispify-tag-name
3722 (nth 0 entry))
3723 :custom-prefixes custom-prefix-list
3724 :custom-level (1+ level)
3725 :value (nth 0 entry))
3726 (unless (eq (preceding-char) ?\n)
3727 (widget-insert "\n"))))
3728 members)))
3729 (message "Creating group magic...")
3730 (mapc 'custom-magic-reset children)
3731 (message "Creating group state...")
3732 (widget-put widget :children children)
3733 (custom-group-state-update widget)
3734 (message "Creating group... done"))
3735 ;; End line
3736 (insert "\n")
3737 (insert-char ?\ (* custom-buffer-indent (1- level)))
3738 (insert "\\- " (widget-get widget :tag) " group end ")
3739 (insert-char ?- (- 75 (current-column) (* custom-buffer-indent level)))
3740 (insert "/\n")))))
3742 (defvar custom-group-menu
3743 '(("Set for Current Session" custom-group-set
3744 (lambda (widget)
3745 (eq (widget-get widget :custom-state) 'modified)))
3746 ("Save for Future Sessions" custom-group-save
3747 (lambda (widget)
3748 (memq (widget-get widget :custom-state) '(modified set))))
3749 ("Reset to Current" custom-group-reset-current
3750 (lambda (widget)
3751 (memq (widget-get widget :custom-state) '(modified))))
3752 ("Reset to Saved" custom-group-reset-saved
3753 (lambda (widget)
3754 (memq (widget-get widget :custom-state) '(modified set))))
3755 ("Reset to standard setting" custom-group-reset-standard
3756 (lambda (widget)
3757 (memq (widget-get widget :custom-state) '(modified set saved)))))
3758 "Alist of actions for the `custom-group' widget.
3759 Each entry has the form (NAME ACTION FILTER) where NAME is the name of
3760 the menu entry, ACTION is the function to call on the widget when the
3761 menu is selected, and FILTER is a predicate which takes a `custom-group'
3762 widget as an argument, and returns non-nil if ACTION is valid on that
3763 widget. If FILTER is nil, ACTION is always valid.")
3765 (defun custom-group-action (widget &optional event)
3766 "Show the menu for `custom-group' WIDGET.
3767 Optional EVENT is the location for the menu."
3768 (if (eq (widget-get widget :custom-state) 'hidden)
3769 (custom-toggle-hide widget)
3770 (let* ((completion-ignore-case t)
3771 (answer (widget-choose (concat "Operation on "
3772 (custom-unlispify-tag-name
3773 (widget-get widget :value)))
3774 (custom-menu-filter custom-group-menu
3775 widget)
3776 event)))
3777 (if answer
3778 (funcall answer widget)))))
3780 (defun custom-group-set (widget)
3781 "Set changes in all modified group members."
3782 (let ((children (widget-get widget :children)))
3783 (mapc (lambda (child)
3784 (when (eq (widget-get child :custom-state) 'modified)
3785 (widget-apply child :custom-set)))
3786 children )))
3788 (defun custom-group-save (widget)
3789 "Save all modified group members."
3790 (let ((children (widget-get widget :children)))
3791 (mapc (lambda (child)
3792 (when (memq (widget-get child :custom-state) '(modified set))
3793 (widget-apply child :custom-save)))
3794 children )))
3796 (defun custom-group-reset-current (widget)
3797 "Reset all modified group members."
3798 (let ((children (widget-get widget :children)))
3799 (mapc (lambda (child)
3800 (when (eq (widget-get child :custom-state) 'modified)
3801 (widget-apply child :custom-reset-current)))
3802 children )))
3804 (defun custom-group-reset-saved (widget)
3805 "Reset all modified or set group members."
3806 (let ((children (widget-get widget :children)))
3807 (mapc (lambda (child)
3808 (when (memq (widget-get child :custom-state) '(modified set))
3809 (widget-apply child :custom-reset-saved)))
3810 children )))
3812 (defun custom-group-reset-standard (widget)
3813 "Reset all modified, set, or saved group members."
3814 (let ((children (widget-get widget :children)))
3815 (mapc (lambda (child)
3816 (when (memq (widget-get child :custom-state)
3817 '(modified set saved))
3818 (widget-apply child :custom-reset-standard)))
3819 children )))
3821 (defun custom-group-state-update (widget)
3822 "Update magic."
3823 (unless (eq (widget-get widget :custom-state) 'hidden)
3824 (let* ((children (widget-get widget :children))
3825 (states (mapcar (lambda (child)
3826 (widget-get child :custom-state))
3827 children))
3828 (magics custom-magic-alist)
3829 (found 'standard))
3830 (while magics
3831 (let ((magic (car (car magics))))
3832 (if (and (not (eq magic 'hidden))
3833 (memq magic states))
3834 (setq found magic
3835 magics nil)
3836 (setq magics (cdr magics)))))
3837 (widget-put widget :custom-state found)))
3838 (custom-magic-reset widget))
3840 ;;; Reading and writing the custom file.
3842 ;;;###autoload
3843 (defcustom custom-file nil
3844 "File used for storing customization information.
3845 The default is nil, which means to use your init file
3846 as specified by `user-init-file'. If the value is not nil,
3847 it should be an absolute file name.
3849 You can set this option through Custom, if you carefully read the
3850 last paragraph below. However, usually it is simpler to write
3851 something like the following in your init file:
3853 \(setq custom-file \"~/.emacs-custom.el\")
3854 \(load custom-file)
3856 Note that both lines are necessary: the first line tells Custom to
3857 save all customizations in this file, but does not load it.
3859 When you change this variable outside Custom, look in the
3860 previous custom file \(usually your init file) for the
3861 forms `(custom-set-variables ...)' and `(custom-set-faces ...)',
3862 and copy them (whichever ones you find) to the new custom file.
3863 This will preserve your existing customizations.
3865 If you save this option using Custom, Custom will write all
3866 currently saved customizations, including the new one for this
3867 option itself, into the file you specify, overwriting any
3868 `custom-set-variables' and `custom-set-faces' forms already
3869 present in that file. It will not delete any customizations from
3870 the old custom file. You should do that manually if that is what you
3871 want. You also have to put something like `\(load \"CUSTOM-FILE\")
3872 in your init file, where CUSTOM-FILE is the actual name of the
3873 file. Otherwise, Emacs will not load the file when it starts up,
3874 and hence will not set `custom-file' to that file either."
3875 :type '(choice (const :tag "Your Emacs init file" nil)
3876 (file :format "%t:%v%d"
3877 :doc
3878 "Please read entire docstring below before setting \
3879 this through Custom.
3880 Click om \"More\" \(or position point there and press RETURN)
3881 if only the first line of the docstring is shown."))
3882 :group 'customize)
3884 (defun custom-file ()
3885 "Return the file name for saving customizations."
3886 (file-chase-links
3887 (or custom-file
3888 (let ((user-init-file user-init-file)
3889 (default-init-file
3890 (if (eq system-type 'ms-dos) "~/_emacs" "~/.emacs")))
3891 (when (null user-init-file)
3892 (if (or (file-exists-p default-init-file)
3893 (and (eq system-type 'windows-nt)
3894 (file-exists-p "~/_emacs")))
3895 ;; Started with -q, i.e. the file containing
3896 ;; Custom settings hasn't been read. Saving
3897 ;; settings there would overwrite other settings.
3898 (error "Saving settings from \"emacs -q\" would overwrite existing customizations"))
3899 (setq user-init-file default-init-file))
3900 user-init-file))))
3902 ;;;###autoload
3903 (defun custom-save-all ()
3904 "Save all customizations in `custom-file'."
3905 (let* ((filename (custom-file))
3906 (recentf-exclude (if recentf-mode
3907 (cons (concat "\\`"
3908 (regexp-quote (custom-file))
3909 "\\'")
3910 recentf-exclude)))
3911 (old-buffer (find-buffer-visiting filename)))
3912 (with-current-buffer (or old-buffer (find-file-noselect filename))
3913 (let ((inhibit-read-only t))
3914 (custom-save-variables)
3915 (custom-save-faces))
3916 (let ((file-precious-flag t))
3917 (save-buffer))
3918 (unless old-buffer
3919 (kill-buffer (current-buffer))))))
3921 ;; Editing the custom file contents in a buffer.
3923 (defun custom-save-delete (symbol)
3924 "Delete all calls to SYMBOL from the contents of the current buffer.
3925 Leave point at the old location of the first such call,
3926 or (if there were none) at the end of the buffer.
3928 This function does not save the buffer."
3929 (goto-char (point-min))
3930 ;; Skip all whitespace and comments.
3931 (while (forward-comment 1))
3932 (or (eobp)
3933 (save-excursion (forward-sexp (buffer-size)))) ; Test for scan errors.
3934 (let (first)
3935 (catch 'found
3936 (while t ;; We exit this loop only via throw.
3937 ;; Skip all whitespace and comments.
3938 (while (forward-comment 1))
3939 (let ((start (point))
3940 (sexp (condition-case nil
3941 (read (current-buffer))
3942 (end-of-file (throw 'found nil)))))
3943 (when (and (listp sexp)
3944 (eq (car sexp) symbol))
3945 (delete-region start (point))
3946 (unless first
3947 (setq first (point)))))))
3948 (if first
3949 (goto-char first)
3950 ;; Move in front of local variables, otherwise long Custom
3951 ;; entries would make them ineffective.
3952 (let ((pos (point-max))
3953 (case-fold-search t))
3954 (save-excursion
3955 (goto-char (point-max))
3956 (search-backward "\n\^L" (max (- (point-max) 3000) (point-min))
3957 'move)
3958 (when (search-forward "Local Variables:" nil t)
3959 (setq pos (line-beginning-position))))
3960 (goto-char pos)))))
3962 (defun custom-save-variables ()
3963 "Save all customized variables in `custom-file'."
3964 (save-excursion
3965 (custom-save-delete 'custom-load-themes)
3966 (custom-save-delete 'custom-reset-variables)
3967 (custom-save-delete 'custom-set-variables)
3968 (custom-save-loaded-themes)
3969 (custom-save-resets 'theme-value 'custom-reset-variables nil)
3970 (let ((standard-output (current-buffer))
3971 (saved-list (make-list 1 0))
3972 sort-fold-case)
3973 ;; First create a sorted list of saved variables.
3974 (mapatoms
3975 (lambda (symbol)
3976 (if (get symbol 'saved-value)
3977 (nconc saved-list (list symbol)))))
3978 (setq saved-list (sort (cdr saved-list) 'string<))
3979 (unless (bolp)
3980 (princ "\n"))
3981 (princ "(custom-set-variables
3982 ;; custom-set-variables was added by Custom.
3983 ;; If you edit it by hand, you could mess it up, so be careful.
3984 ;; Your init file should contain only one such instance.
3985 ;; If there is more than one, they won't work right.\n")
3986 (dolist (symbol saved-list)
3987 (let ((spec (car-safe (get symbol 'theme-value)))
3988 (value (get symbol 'saved-value))
3989 (requests (get symbol 'custom-requests))
3990 (now (not (or (custom-variable-p symbol)
3991 (and (not (boundp symbol))
3992 (not (eq (get symbol 'force-value)
3993 'rogue))))))
3994 (comment (get symbol 'saved-variable-comment)))
3995 ;; Check `requests'.
3996 (dolist (request requests)
3997 (when (and (symbolp request) (not (featurep request)))
3998 (message "Unknown requested feature: %s" request)
3999 (setq requests (delq request requests))))
4000 (when (or (and spec
4001 (eq (nth 0 spec) 'user)
4002 (eq (nth 1 spec) 'set))
4003 comment
4004 (and (null spec) (get symbol 'saved-value)))
4005 (unless (bolp)
4006 (princ "\n"))
4007 (princ " '(")
4008 (prin1 symbol)
4009 (princ " ")
4010 (prin1 (car value))
4011 (when (or now requests comment)
4012 (princ " ")
4013 (prin1 now)
4014 (when (or requests comment)
4015 (princ " ")
4016 (prin1 requests)
4017 (when comment
4018 (princ " ")
4019 (prin1 comment))))
4020 (princ ")"))))
4021 (if (bolp)
4022 (princ " "))
4023 (princ ")")
4024 (unless (looking-at "\n")
4025 (princ "\n")))))
4027 (defun custom-save-faces ()
4028 "Save all customized faces in `custom-file'."
4029 (save-excursion
4030 (custom-save-delete 'custom-reset-faces)
4031 (custom-save-delete 'custom-set-faces)
4032 (custom-save-resets 'theme-face 'custom-reset-faces '(default))
4033 (let ((standard-output (current-buffer))
4034 (saved-list (make-list 1 0))
4035 sort-fold-case)
4036 ;; First create a sorted list of saved faces.
4037 (mapatoms
4038 (lambda (symbol)
4039 (if (get symbol 'saved-face)
4040 (nconc saved-list (list symbol)))))
4041 (setq saved-list (sort (cdr saved-list) 'string<))
4042 ;; The default face must be first, since it affects the others.
4043 (if (memq 'default saved-list)
4044 (setq saved-list (cons 'default (delq 'default saved-list))))
4045 (unless (bolp)
4046 (princ "\n"))
4047 (princ "(custom-set-faces
4048 ;; custom-set-faces was added by Custom.
4049 ;; If you edit it by hand, you could mess it up, so be careful.
4050 ;; Your init file should contain only one such instance.
4051 ;; If there is more than one, they won't work right.\n")
4052 (dolist (symbol saved-list)
4053 (let ((spec (car-safe (get symbol 'theme-face)))
4054 (value (get symbol 'saved-face))
4055 (now (not (or (get symbol 'face-defface-spec)
4056 (and (not (custom-facep symbol))
4057 (not (get symbol 'force-face))))))
4058 (comment (get symbol 'saved-face-comment)))
4059 (when (or (and spec
4060 (eq (nth 0 spec) 'user)
4061 (eq (nth 1 spec) 'set))
4062 comment
4063 (and (null spec) (get symbol 'saved-face)))
4064 ;; Don't print default face here.
4065 (unless (bolp)
4066 (princ "\n"))
4067 (princ " '(")
4068 (prin1 symbol)
4069 (princ " ")
4070 (prin1 value)
4071 (when (or now comment)
4072 (princ " ")
4073 (prin1 now)
4074 (when comment
4075 (princ " ")
4076 (prin1 comment)))
4077 (princ ")"))))
4078 (if (bolp)
4079 (princ " "))
4080 (princ ")")
4081 (unless (looking-at "\n")
4082 (princ "\n")))))
4084 (defun custom-save-resets (property setter special)
4085 (let (started-writing ignored-special)
4086 ;; (custom-save-delete setter) Done by caller
4087 (let ((standard-output (current-buffer))
4088 (mapper `(lambda (object)
4089 (let ((spec (car-safe (get object (quote ,property)))))
4090 (when (and (not (memq object ignored-special))
4091 (eq (nth 0 spec) 'user)
4092 (eq (nth 1 spec) 'reset))
4093 ;; Do not write reset statements unless necessary.
4094 (unless started-writing
4095 (setq started-writing t)
4096 (unless (bolp)
4097 (princ "\n"))
4098 (princ "(")
4099 (princ (quote ,setter))
4100 (princ "\n '(")
4101 (prin1 object)
4102 (princ " ")
4103 (prin1 (nth 3 spec))
4104 (princ ")")))))))
4105 (mapc mapper special)
4106 (setq ignored-special special)
4107 (mapatoms mapper)
4108 (when started-writing
4109 (princ ")\n")))))
4111 (defun custom-save-loaded-themes ()
4112 (let ((themes (reverse (get 'user 'theme-loads-themes)))
4113 (standard-output (current-buffer)))
4114 (when themes
4115 (unless (bolp) (princ "\n"))
4116 (princ "(custom-load-themes")
4117 (mapc (lambda (theme)
4118 (princ "\n '")
4119 (prin1 theme)) themes)
4120 (princ " )\n"))))
4122 ;;;###autoload
4123 (defun customize-save-customized ()
4124 "Save all user options which have been set in this session."
4125 (interactive)
4126 (mapatoms (lambda (symbol)
4127 (let ((face (get symbol 'customized-face))
4128 (value (get symbol 'customized-value))
4129 (face-comment (get symbol 'customized-face-comment))
4130 (variable-comment
4131 (get symbol 'customized-variable-comment)))
4132 (when face
4133 (put symbol 'saved-face face)
4134 (custom-push-theme 'theme-face symbol 'user 'set value)
4135 (put symbol 'customized-face nil))
4136 (when value
4137 (put symbol 'saved-value value)
4138 (custom-push-theme 'theme-value symbol 'user 'set value)
4139 (put symbol 'customized-value nil))
4140 (when variable-comment
4141 (put symbol 'saved-variable-comment variable-comment)
4142 (put symbol 'customized-variable-comment nil))
4143 (when face-comment
4144 (put symbol 'saved-face-comment face-comment)
4145 (put symbol 'customized-face-comment nil)))))
4146 ;; We really should update all custom buffers here.
4147 (custom-save-all))
4149 ;;; The Customize Menu.
4151 ;;; Menu support
4153 (defcustom custom-menu-nesting 2
4154 "Maximum nesting in custom menus."
4155 :type 'integer
4156 :group 'custom-menu)
4158 (defun custom-face-menu-create (widget symbol)
4159 "Ignoring WIDGET, create a menu entry for customization face SYMBOL."
4160 (vector (custom-unlispify-menu-entry symbol)
4161 `(customize-face ',symbol)
4164 (defun custom-variable-menu-create (widget symbol)
4165 "Ignoring WIDGET, create a menu entry for customization variable SYMBOL."
4166 (let ((type (get symbol 'custom-type)))
4167 (unless (listp type)
4168 (setq type (list type)))
4169 (if (and type (widget-get type :custom-menu))
4170 (widget-apply type :custom-menu symbol)
4171 (vector (custom-unlispify-menu-entry symbol)
4172 `(customize-variable ',symbol)
4173 t))))
4175 ;; Add checkboxes to boolean variable entries.
4176 (widget-put (get 'boolean 'widget-type)
4177 :custom-menu (lambda (widget symbol)
4178 (vector (custom-unlispify-menu-entry symbol)
4179 `(customize-variable ',symbol)
4180 ':style 'toggle
4181 ':selected symbol)))
4183 (defun custom-group-menu-create (widget symbol)
4184 "Ignoring WIDGET, create a menu entry for customization group SYMBOL."
4185 `( ,(custom-unlispify-menu-entry symbol t)
4186 :filter (lambda (&rest junk)
4187 (let ((menu (custom-menu-create ',symbol)))
4188 (if (consp menu) (cdr menu) menu)))))
4190 ;;;###autoload
4191 (defun custom-menu-create (symbol)
4192 "Create menu for customization group SYMBOL.
4193 The menu is in a format applicable to `easy-menu-define'."
4194 (let* ((item (vector (custom-unlispify-menu-entry symbol)
4195 `(customize-group ',symbol)
4196 t)))
4197 (if (and (or (not (boundp 'custom-menu-nesting))
4198 (>= custom-menu-nesting 0))
4199 (progn
4200 (custom-load-symbol symbol)
4201 (< (length (get symbol 'custom-group)) widget-menu-max-size)))
4202 (let ((custom-prefix-list (custom-prefix-add symbol
4203 custom-prefix-list))
4204 (members (custom-sort-items (get symbol 'custom-group)
4205 custom-menu-sort-alphabetically
4206 custom-menu-order-groups)))
4207 `(,(custom-unlispify-menu-entry symbol t)
4208 ,item
4209 "--"
4210 ,@(mapcar (lambda (entry)
4211 (widget-apply (if (listp (nth 1 entry))
4212 (nth 1 entry)
4213 (list (nth 1 entry)))
4214 :custom-menu (nth 0 entry)))
4215 members)))
4216 item)))
4218 ;;;###autoload
4219 (defun customize-menu-create (symbol &optional name)
4220 "Return a customize menu for customization group SYMBOL.
4221 If optional NAME is given, use that as the name of the menu.
4222 Otherwise the menu will be named `Customize'.
4223 The format is suitable for use with `easy-menu-define'."
4224 (unless name
4225 (setq name "Customize"))
4226 `(,name
4227 :filter (lambda (&rest junk)
4228 (let ((menu (custom-menu-create ',symbol)))
4229 (if (consp menu) (cdr menu) menu)))))
4231 ;;; The Custom Mode.
4233 (defvar custom-mode-map
4234 ;; This keymap should be dense, but a dense keymap would prevent inheriting
4235 ;; "\r" bindings from the parent map.
4236 ;; Actually, this misfeature of dense keymaps was fixed on 2001-11-26.
4237 (let ((map (make-keymap)))
4238 (set-keymap-parent map widget-keymap)
4239 (suppress-keymap map)
4240 (define-key map " " 'scroll-up)
4241 (define-key map "\177" 'scroll-down)
4242 (define-key map "\C-c\C-c" 'Custom-set)
4243 (define-key map "\C-x\C-s" 'Custom-save)
4244 (define-key map "q" 'Custom-buffer-done)
4245 (define-key map "u" 'Custom-goto-parent)
4246 (define-key map "n" 'widget-forward)
4247 (define-key map "p" 'widget-backward)
4248 (define-key map [mouse-1] 'Custom-move-and-invoke)
4249 map)
4250 "Keymap for `custom-mode'.")
4252 (defun Custom-move-and-invoke (event)
4253 "Move to where you click, and if it is an active field, invoke it."
4254 (interactive "e")
4255 (mouse-set-point event)
4256 (if (widget-event-point event)
4257 (let* ((pos (widget-event-point event))
4258 (button (get-char-property pos 'button)))
4259 (if button
4260 (widget-button-click event)))))
4262 (easy-menu-define Custom-mode-menu
4263 custom-mode-map
4264 "Menu used in customization buffers."
4265 `("Custom"
4266 ,(customize-menu-create 'customize)
4267 ["Set" Custom-set t]
4268 ["Save" Custom-save t]
4269 ["Reset to Current" Custom-reset-current t]
4270 ["Reset to Saved" Custom-reset-saved t]
4271 ["Reset to Standard Settings" Custom-reset-standard t]
4272 ["Info" (info "(emacs)Easy Customization") t]))
4274 (defun Custom-goto-parent ()
4275 "Go to the parent group listed at the top of this buffer.
4276 If several parents are listed, go to the first of them."
4277 (interactive)
4278 (save-excursion
4279 (goto-char (point-min))
4280 (if (search-forward "\nGo to parent group: " nil t)
4281 (let* ((button (get-char-property (point) 'button))
4282 (parent (downcase (widget-get button :tag))))
4283 (customize-group parent)))))
4285 (defcustom custom-mode-hook nil
4286 "Hook called when entering Custom mode."
4287 :type 'hook
4288 :group 'custom-buffer )
4290 (defun custom-state-buffer-message (widget)
4291 (if (eq (widget-get (widget-get widget :parent) :custom-state) 'modified)
4292 (message "To install your edits, invoke [State] and choose the Set operation")))
4294 (defun custom-mode ()
4295 "Major mode for editing customization buffers.
4297 The following commands are available:
4299 Move to next button or editable field. \\[widget-forward]
4300 Move to previous button or editable field. \\[widget-backward]
4301 \\<widget-field-keymap>\
4302 Complete content of editable text field. \\[widget-complete]
4303 \\<custom-mode-map>\
4304 Invoke button under the mouse pointer. \\[Custom-move-and-invoke]
4305 Invoke button under point. \\[widget-button-press]
4306 Set all modifications. \\[Custom-set]
4307 Make all modifications default. \\[Custom-save]
4308 Reset all modified options. \\[Custom-reset-current]
4309 Reset all modified or set options. \\[Custom-reset-saved]
4310 Reset all options. \\[Custom-reset-standard]
4312 Entry to this mode calls the value of `custom-mode-hook'
4313 if that value is non-nil."
4314 (kill-all-local-variables)
4315 (setq major-mode 'custom-mode
4316 mode-name "Custom")
4317 (use-local-map custom-mode-map)
4318 (easy-menu-add Custom-mode-menu)
4319 (make-local-variable 'custom-options)
4320 (make-local-variable 'custom-local-buffer)
4321 (make-local-variable 'widget-documentation-face)
4322 (setq widget-documentation-face 'custom-documentation)
4323 (make-local-variable 'widget-button-face)
4324 (setq widget-button-face 'custom-button)
4325 (set (make-local-variable 'widget-button-pressed-face) 'custom-button-pressed)
4326 (set (make-local-variable 'widget-mouse-face)
4327 'custom-button-pressed) ; buttons `depress' when moused
4328 ;; When possible, use relief for buttons, not bracketing. This test
4329 ;; may not be optimal.
4330 (when custom-raised-buttons
4331 (set (make-local-variable 'widget-push-button-prefix) "")
4332 (set (make-local-variable 'widget-push-button-suffix) "")
4333 (set (make-local-variable 'widget-link-prefix) "")
4334 (set (make-local-variable 'widget-link-suffix) ""))
4335 (add-hook 'widget-edit-functions 'custom-state-buffer-message nil t)
4336 (run-mode-hooks 'custom-mode-hook))
4338 (put 'custom-mode 'mode-class 'special)
4340 (add-to-list
4341 'debug-ignored-errors
4342 "^No user options have changed defaults in recent Emacs versions$")
4344 ;;; The End.
4346 (provide 'cus-edit)
4348 ;; arch-tag: 64533aa4-1b1a-48c3-8812-f9dc718e8a6f
4349 ;;; cus-edit.el ends here