* newcomment.el (comment-add, comment-valid-prefix-p): Docfix.
[emacs.git] / lisp / cus-edit.el
blob620ecdba40cb3669bbfa40c32a0261c54cfef753
1 ;;; cus-edit.el --- tools for customizing Emacs and Lisp packages
2 ;;
3 ;; Copyright (C) 1996-1997, 1999-2011 Free Software Foundation, Inc.
4 ;;
5 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
6 ;; Maintainer: FSF
7 ;; Keywords: help, faces
8 ;; Package: emacs
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 3 of the License, or
15 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; This file implements the code to create and edit customize buffers.
29 ;; See `custom.el'.
31 ;; No commands should have names starting with `custom-' because
32 ;; that interferes with completion. Use `customize-' for commands
33 ;; that the user will run with M-x, and `Custom-' for interactive commands.
35 ;; The identity of a customize option is represented by a Lisp symbol.
36 ;; The following values are associated with an option.
38 ;; 0. The current value.
40 ;; This is the value of the option as seen by "the rest of Emacs".
42 ;; Usually extracted by 'default-value', but can be extracted with
43 ;; different means if the option symbol has the 'custom-get'
44 ;; property. Similarly, set-default (or the 'custom-set' property)
45 ;; can set it.
47 ;; 1. The widget value.
49 ;; This is the value shown in the widget in a customize buffer.
51 ;; 2. The customized value.
53 ;; This is the last value given to the option through customize.
55 ;; It is stored in the 'customized-value' property of the option, in a
56 ;; cons-cell whose car evaluates to the customized value.
58 ;; 3. The saved value.
60 ;; This is last value saved from customize.
62 ;; It is stored in the 'saved-value' property of the option, in a
63 ;; cons-cell whose car evaluates to the saved value.
65 ;; 4. The standard value.
67 ;; This is the value given in the 'defcustom' declaration.
69 ;; It is stored in the 'standard-value' property of the option, in a
70 ;; cons-cell whose car evaluates to the standard value.
72 ;; 5. The "think" value.
74 ;; This is what customize thinks the current value should be.
76 ;; This is the customized value, if any such value exists, otherwise
77 ;; the saved value, if that exists, and as a last resort the standard
78 ;; value.
80 ;; The reason for storing values unevaluated: This is so you can have
81 ;; values that depend on the environment. For example, you can have a
82 ;; variable that has one value when Emacs is running under a window
83 ;; system, and another value on a tty. Since the evaluation is only done
84 ;; when the variable is first initialized, this is only relevant for the
85 ;; saved (and standard) values, but affect others values for
86 ;; compatibility.
88 ;; You can see (and modify and save) this unevaluated value by selecting
89 ;; "Show Saved Lisp Expression" from the Lisp interface. This will
90 ;; give you the unevaluated saved value, if any, otherwise the
91 ;; unevaluated standard value.
93 ;; The possible states for a customize widget are:
95 ;; 0. unknown
97 ;; The state has not been determined yet.
99 ;; 1. modified
101 ;; The widget value is different from the current value.
103 ;; 2. changed
105 ;; The current value is different from the "think" value.
107 ;; 3. set
109 ;; The "think" value is the customized value.
111 ;; 4. saved
113 ;; The "think" value is the saved value.
115 ;; 5. standard
117 ;; The "think" value is the standard value.
119 ;; 6. rogue
121 ;; There is no standard value. This means that the variable was
122 ;; not defined with defcustom, nor handled in cus-start.el. Most
123 ;; standard interactive Custom commands do not let you create a
124 ;; Custom buffer containing such variables. However, such Custom
125 ;; buffers can be created, for instance, by calling
126 ;; `customize-apropos' with a prefix arg or by calling
127 ;; `customize-option' non-interactively.
129 ;; 7. hidden
131 ;; There is no widget value.
133 ;; 8. mismatch
135 ;; The widget value is not valid member of the :type specified for the
136 ;; option.
138 ;;; Code:
140 (require 'cus-face)
141 (require 'wid-edit)
143 (defvar custom-versions-load-alist) ; from cus-load
144 (defvar recentf-exclude) ; from recentf.el
146 (condition-case nil
147 (require 'cus-load)
148 (error nil))
150 (condition-case nil
151 (require 'cus-start)
152 (error nil))
154 (put 'custom-define-hook 'custom-type 'hook)
155 (put 'custom-define-hook 'standard-value '(nil))
156 (custom-add-to-group 'customize 'custom-define-hook 'custom-variable)
158 ;;; Customization Groups.
160 (defgroup emacs nil
161 "Customization of the One True Editor."
162 :link '(custom-manual "(emacs)Top"))
164 ;; Most of these groups are stolen from `finder.el',
165 (defgroup editing nil
166 "Basic text editing facilities."
167 :group 'emacs)
169 (defgroup convenience nil
170 "Convenience features for faster editing."
171 :group 'emacs)
173 (defgroup files nil
174 "Support for editing files."
175 :group 'emacs)
177 (defgroup wp nil
178 "Support for editing text files."
179 :tag "Text"
180 :group 'emacs)
182 (defgroup data nil
183 "Support for editing binary data files."
184 :group 'emacs)
186 (defgroup abbrev nil
187 "Abbreviation handling, typing shortcuts, macros."
188 :tag "Abbreviations"
189 :group 'convenience)
191 (defgroup matching nil
192 "Various sorts of searching and matching."
193 :group 'editing)
195 (defgroup emulations nil
196 "Emulations of other editors."
197 :link '(custom-manual "(emacs)Emulation")
198 :group 'editing)
200 (defgroup mouse nil
201 "Mouse support."
202 :group 'editing)
204 (defgroup outlines nil
205 "Support for hierarchical outlining."
206 :group 'wp)
208 (defgroup external nil
209 "Interfacing to external utilities."
210 :group 'emacs)
212 (defgroup comm nil
213 "Communications, networking, and remote access to files."
214 :tag "Communication"
215 :group 'emacs)
217 (defgroup processes nil
218 "Process, subshell, compilation, and job control support."
219 :group 'external)
221 (defgroup programming nil
222 "Support for programming in other languages."
223 :group 'emacs)
225 (defgroup languages nil
226 "Specialized modes for editing programming languages."
227 :group 'programming)
229 (defgroup lisp nil
230 "Lisp support, including Emacs Lisp."
231 :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces)
232 :group 'languages
233 :group 'development)
235 (defgroup c nil
236 "Support for the C language and related languages."
237 :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces)
238 :link '(custom-manual "(ccmode)")
239 :group 'languages)
241 (defgroup tools nil
242 "Programming tools."
243 :group 'programming)
245 (defgroup applications nil
246 "Applications written in Emacs."
247 :group 'emacs)
249 (defgroup calendar nil
250 "Calendar and time management support."
251 :group 'applications)
253 (defgroup mail nil
254 "Modes for electronic-mail handling."
255 :group 'applications)
257 (defgroup news nil
258 "Support for netnews reading and posting."
259 :link '(custom-manual "(gnus)")
260 :group 'applications)
262 (defgroup games nil
263 "Games, jokes and amusements."
264 :group 'applications)
266 (defgroup development nil
267 "Support for further development of Emacs."
268 :group 'emacs)
270 (defgroup docs nil
271 "Support for Emacs documentation."
272 :group 'development)
274 (defgroup extensions nil
275 "Emacs Lisp language extensions."
276 :group 'development)
278 (defgroup internal nil
279 "Code for Emacs internals, build process, defaults."
280 :group 'development)
282 (defgroup maint nil
283 "Maintenance aids for the Emacs development group."
284 :tag "Maintenance"
285 :group 'development)
287 (defgroup environment nil
288 "Fitting Emacs with its environment."
289 :group 'emacs)
291 (defgroup hardware nil
292 "Support for interfacing with miscellaneous hardware."
293 :group 'environment)
295 (defgroup terminals nil
296 "Support for terminal types."
297 :group 'environment)
299 (defgroup unix nil
300 "Front-ends/assistants for, or emulators of, UNIX features."
301 :group 'environment)
303 (defgroup i18n nil
304 "Internationalization and alternate character-set support."
305 :link '(custom-manual "(emacs)International")
306 :group 'environment
307 :group 'editing)
309 (defgroup x nil
310 "The X Window system."
311 :group 'environment)
313 (defgroup frames nil
314 "Support for Emacs frames and window systems."
315 :group 'environment)
317 (defgroup tex nil
318 "Code related to the TeX formatter."
319 :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces)
320 :group 'wp)
322 (defgroup faces nil
323 "Support for multiple fonts."
324 :group 'emacs)
326 (defgroup help nil
327 "Support for on-line help systems."
328 :group 'emacs)
330 (defgroup multimedia nil
331 "Non-textual support, specifically images and sound."
332 :group 'emacs)
334 (defgroup local nil
335 "Code local to your site."
336 :group 'emacs)
338 (defgroup customize '((widgets custom-group))
339 "Customization of the Customization support."
340 :prefix "custom-"
341 :group 'help)
343 (defgroup custom-faces nil
344 "Faces used by customize."
345 :group 'customize
346 :group 'faces)
348 (defgroup custom-browse nil
349 "Control customize browser."
350 :prefix "custom-"
351 :group 'customize)
353 (defgroup custom-buffer nil
354 "Control customize buffers."
355 :prefix "custom-"
356 :group 'customize)
358 (defgroup custom-menu nil
359 "Control customize menus."
360 :prefix "custom-"
361 :group 'customize)
363 (defgroup alloc nil
364 "Storage allocation and gc for GNU Emacs Lisp interpreter."
365 :tag "Storage Allocation"
366 :group 'internal)
368 (defgroup undo nil
369 "Undoing changes in buffers."
370 :link '(custom-manual "(emacs)Undo")
371 :group 'editing)
373 (defgroup mode-line nil
374 "Content of the modeline."
375 :group 'environment)
377 (defgroup editing-basics nil
378 "Most basic editing facilities."
379 :group 'editing)
381 (defgroup display nil
382 "How characters are displayed in buffers."
383 :group 'environment)
385 (defgroup execute nil
386 "Executing external commands."
387 :group 'processes)
389 (defgroup installation nil
390 "The Emacs installation."
391 :group 'environment)
393 (defgroup dired nil
394 "Directory editing."
395 :group 'environment)
397 (defgroup limits nil
398 "Internal Emacs limits."
399 :group 'internal)
401 (defgroup debug nil
402 "Debugging Emacs itself."
403 :group 'development)
405 (defgroup keyboard nil
406 "Input from the keyboard."
407 :group 'environment)
409 (defgroup mouse nil
410 "Input from the mouse."
411 :group 'environment)
413 (defgroup menu nil
414 "Input from the menus."
415 :group 'environment)
417 (defgroup dnd nil
418 "Handling data from drag and drop."
419 :group 'environment)
421 (defgroup auto-save nil
422 "Preventing accidental loss of data."
423 :group 'files)
425 (defgroup processes-basics nil
426 "Basic stuff dealing with processes."
427 :group 'processes)
429 (defgroup mule nil
430 "MULE Emacs internationalization."
431 :group 'i18n)
433 (defgroup windows nil
434 "Windows within a frame."
435 :link '(custom-manual "(emacs)Windows")
436 :group 'environment)
438 ;;; Custom mode keymaps
440 (defvar custom-mode-map
441 (let ((map (make-keymap)))
442 (set-keymap-parent map widget-keymap)
443 (define-key map [remap self-insert-command] 'Custom-no-edit)
444 (define-key map "\^m" 'Custom-newline)
445 (define-key map " " 'scroll-up)
446 (define-key map "\177" 'scroll-down)
447 (define-key map "\C-c\C-c" 'Custom-set)
448 (define-key map "\C-x\C-s" 'Custom-save)
449 (define-key map "q" 'Custom-buffer-done)
450 (define-key map "u" 'Custom-goto-parent)
451 (define-key map "n" 'widget-forward)
452 (define-key map "p" 'widget-backward)
453 map)
454 "Keymap for `Custom-mode'.")
456 (defvar custom-mode-link-map
457 (let ((map (make-keymap)))
458 (set-keymap-parent map custom-mode-map)
459 (define-key map [down-mouse-2] nil)
460 (define-key map [down-mouse-1] 'mouse-drag-region)
461 (define-key map [mouse-2] 'widget-move-and-invoke)
462 map)
463 "Local keymap for links in `Custom-mode'.")
465 (defvar custom-field-keymap
466 (let ((map (copy-keymap widget-field-keymap)))
467 (define-key map "\C-c\C-c" 'Custom-set)
468 (define-key map "\C-x\C-s" 'Custom-save)
469 map)
470 "Keymap used inside editable fields in customization buffers.")
472 (widget-put (get 'editable-field 'widget-type) :keymap custom-field-keymap)
474 ;;; Utilities.
476 (defun custom-split-regexp-maybe (regexp)
477 "If REGEXP is a string, split it to a list at `\\|'.
478 You can get the original back from the result with:
479 (mapconcat 'identity result \"\\|\")
481 IF REGEXP is not a string, return it unchanged."
482 (if (stringp regexp)
483 (split-string regexp "\\\\|")
484 regexp))
486 (defun custom-variable-prompt ()
487 "Prompt for a custom variable, defaulting to the variable at point.
488 Return a list suitable for use in `interactive'."
489 (let* ((v (variable-at-point))
490 (default (and (symbolp v) (custom-variable-p v) (symbol-name v)))
491 (enable-recursive-minibuffers t)
492 val)
493 (setq val (completing-read
494 (if default (format "Customize variable (default %s): " default)
495 "Customize variable: ")
496 obarray 'custom-variable-p t nil nil default))
497 (list (if (equal val "")
498 (if (symbolp v) v nil)
499 (intern val)))))
501 (defun custom-menu-filter (menu widget)
502 "Convert MENU to the form used by `widget-choose'.
503 MENU should be in the same format as `custom-variable-menu'.
504 WIDGET is the widget to apply the filter entries of MENU on."
505 (let ((result nil)
506 current name action filter)
507 (while menu
508 (setq current (car menu)
509 name (nth 0 current)
510 action (nth 1 current)
511 filter (nth 2 current)
512 menu (cdr menu))
513 (if (or (null filter) (funcall filter widget))
514 (push (cons name action) result)
515 (push name result)))
516 (nreverse result)))
518 ;;; Unlispify.
520 (defvar custom-prefix-list nil
521 "List of prefixes that should be ignored by `custom-unlispify'.")
523 (defcustom custom-unlispify-menu-entries t
524 "Display menu entries as words instead of symbols if non-nil."
525 :group 'custom-menu
526 :type 'boolean)
528 (defcustom custom-unlispify-remove-prefixes nil
529 "Non-nil means remove group prefixes from option names in buffer."
530 :group 'custom-menu
531 :group 'custom-buffer
532 :type 'boolean)
534 (defun custom-unlispify-menu-entry (symbol &optional no-suffix)
535 "Convert SYMBOL into a menu entry."
536 (cond ((not custom-unlispify-menu-entries)
537 (symbol-name symbol))
538 ((get symbol 'custom-tag)
539 (if no-suffix
540 (get symbol 'custom-tag)
541 (concat (get symbol 'custom-tag) "...")))
543 (with-current-buffer (get-buffer-create " *Custom-Work*")
544 (erase-buffer)
545 (princ symbol (current-buffer))
546 (goto-char (point-min))
547 ;; FIXME: Boolean variables are not predicates, so they shouldn't
548 ;; end with `-p'. -stef
549 ;; (when (and (eq (get symbol 'custom-type) 'boolean)
550 ;; (re-search-forward "-p\\'" nil t))
551 ;; (replace-match "" t t)
552 ;; (goto-char (point-min)))
553 (if custom-unlispify-remove-prefixes
554 (let ((prefixes custom-prefix-list)
555 prefix)
556 (while prefixes
557 (setq prefix (car prefixes))
558 (if (search-forward prefix (+ (point) (length prefix)) t)
559 (progn
560 (setq prefixes nil)
561 (delete-region (point-min) (point)))
562 (setq prefixes (cdr prefixes))))))
563 (subst-char-in-region (point-min) (point-max) ?- ?\ t)
564 (capitalize-region (point-min) (point-max))
565 (unless no-suffix
566 (goto-char (point-max))
567 (insert "..."))
568 (buffer-string)))))
570 (defcustom custom-unlispify-tag-names t
571 "Display tag names as words instead of symbols if non-nil."
572 :group 'custom-buffer
573 :type 'boolean)
575 (defun custom-unlispify-tag-name (symbol)
576 "Convert SYMBOL into a menu entry."
577 (let ((custom-unlispify-menu-entries custom-unlispify-tag-names))
578 (custom-unlispify-menu-entry symbol t)))
580 (defun custom-prefix-add (symbol prefixes)
581 "Add SYMBOL to list of ignored PREFIXES."
582 (cons (or (get symbol 'custom-prefix)
583 (concat (symbol-name symbol) "-"))
584 prefixes))
586 ;;; Guess.
588 (defcustom custom-guess-name-alist
589 '(("-p\\'" boolean)
590 ("-flag\\'" boolean)
591 ("-hook\\'" hook)
592 ("-face\\'" face)
593 ("-file\\'" file)
594 ("-function\\'" function)
595 ("-functions\\'" (repeat function))
596 ("-list\\'" (repeat sexp))
597 ("-alist\\'" (alist :key-type sexp :value-type sexp)))
598 "Alist of (MATCH TYPE).
600 MATCH should be a regexp matching the name of a symbol, and TYPE should
601 be a widget suitable for editing the value of that symbol. The TYPE
602 of the first entry where MATCH matches the name of the symbol will be
603 used.
605 This is used for guessing the type of variables not declared with
606 customize."
607 :type '(repeat (group (regexp :tag "Match") (sexp :tag "Type")))
608 :group 'custom-buffer)
610 (defcustom custom-guess-doc-alist
611 '(("\\`\\*?Non-nil " boolean))
612 "Alist of (MATCH TYPE).
614 MATCH should be a regexp matching a documentation string, and TYPE
615 should be a widget suitable for editing the value of a variable with
616 that documentation string. The TYPE of the first entry where MATCH
617 matches the name of the symbol will be used.
619 This is used for guessing the type of variables not declared with
620 customize."
621 :type '(repeat (group (regexp :tag "Match") (sexp :tag "Type")))
622 :group 'custom-buffer)
624 (defun custom-guess-type (symbol)
625 "Guess a widget suitable for editing the value of SYMBOL.
626 This is done by matching SYMBOL with `custom-guess-name-alist' and
627 if that fails, the doc string with `custom-guess-doc-alist'."
628 (let ((name (symbol-name symbol))
629 (names custom-guess-name-alist)
630 current found)
631 (while names
632 (setq current (car names)
633 names (cdr names))
634 (when (string-match (nth 0 current) name)
635 (setq found (nth 1 current)
636 names nil)))
637 (unless found
638 (let ((doc (documentation-property symbol 'variable-documentation))
639 (docs custom-guess-doc-alist))
640 (when doc
641 (while docs
642 (setq current (car docs)
643 docs (cdr docs))
644 (when (string-match (nth 0 current) doc)
645 (setq found (nth 1 current)
646 docs nil))))))
647 found))
649 ;;; Sorting.
651 ;;;###autoload
652 (defcustom custom-browse-sort-alphabetically nil
653 "If non-nil, sort customization group alphabetically in `custom-browse'."
654 :type 'boolean
655 :group 'custom-browse)
657 (defcustom custom-browse-order-groups nil
658 "If non-nil, order group members within each customization group.
659 If `first', order groups before non-groups.
660 If `last', order groups after non-groups."
661 :type '(choice (const first)
662 (const last)
663 (const :tag "none" nil))
664 :group 'custom-browse)
666 (defcustom custom-browse-only-groups nil
667 "If non-nil, show group members only within each customization group."
668 :type 'boolean
669 :group 'custom-browse)
671 ;;;###autoload
672 (defcustom custom-buffer-sort-alphabetically t
673 "Whether to sort customization groups alphabetically in Custom buffer."
674 :type 'boolean
675 :group 'custom-buffer
676 :version "24.1")
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 ;;;###autoload
688 (defcustom custom-menu-sort-alphabetically nil
689 "If non-nil, sort each customization group alphabetically in menus."
690 :type 'boolean
691 :group 'custom-menu)
693 (defcustom custom-menu-order-groups 'first
694 "If non-nil, order group members within each customization group.
695 If `first', order groups before non-groups.
696 If `last', order groups after non-groups."
697 :type '(choice (const first)
698 (const last)
699 (const :tag "none" nil))
700 :group 'custom-menu)
702 (defun custom-sort-items (items sort-alphabetically order-groups)
703 "Return a sorted copy of ITEMS.
704 ITEMS should be a `custom-group' property.
705 If SORT-ALPHABETICALLY non-nil, sort alphabetically.
706 If ORDER-GROUPS is `first' order groups before non-groups, if `last' order
707 groups after non-groups, if nil do not order groups at all."
708 (sort (copy-sequence items)
709 (lambda (a b)
710 (let ((typea (nth 1 a)) (typeb (nth 1 b))
711 (namea (nth 0 a)) (nameb (nth 0 b)))
712 (cond ((not order-groups)
713 ;; Since we don't care about A and B order, maybe sort.
714 (when sort-alphabetically
715 (string-lessp namea nameb)))
716 ((eq typea 'custom-group)
717 ;; If B is also a group, maybe sort. Otherwise, order A and B.
718 (if (eq typeb 'custom-group)
719 (when sort-alphabetically
720 (string-lessp namea nameb))
721 (eq order-groups 'first)))
722 ((eq typeb 'custom-group)
723 ;; Since A cannot be a group, order A and B.
724 (eq order-groups 'last))
725 (sort-alphabetically
726 ;; Since A and B cannot be groups, sort.
727 (string-lessp namea nameb)))))))
729 ;;; Custom Mode Commands.
731 ;; This variable is used by `custom-tool-bar-map', or directly by
732 ;; `custom-buffer-create-internal' if `custom-buffer-verbose-help' is non-nil.
734 (defvar custom-commands
735 '((" Set for current session " Custom-set t
736 "Apply all settings in this buffer to the current session"
737 "index"
738 "Apply")
739 (" Save for future sessions " Custom-save
740 (or custom-file user-init-file)
741 "Apply all settings in this buffer and save them for future Emacs sessions."
742 "save"
743 "Save")
744 (" Undo edits " Custom-reset-current t
745 "Restore all settings in this buffer to reflect their current values."
746 "refresh"
747 "Undo")
748 (" Reset to saved " Custom-reset-saved t
749 "Restore all settings in this buffer to their saved values (if any)."
750 "undo"
751 "Reset")
752 (" Erase customizations " Custom-reset-standard
753 (or custom-file user-init-file)
754 "Un-customize all settings in this buffer and save them with standard values."
755 "delete"
756 "Uncustomize")
757 (" Help for Customize " Custom-help t
758 "Get help for using Customize."
759 "help"
760 "Help")
761 (" Exit " Custom-buffer-done t "Exit Customize." "exit" "Exit")))
763 (defun Custom-help ()
764 "Read the node on Easy Customization in the Emacs manual."
765 (interactive)
766 (info "(emacs)Easy Customization"))
768 (defvar custom-reset-menu
769 '(("Undo Edits" . Custom-reset-current)
770 ("Reset to Saved" . Custom-reset-saved)
771 ("Erase Customizations (use standard values)" . Custom-reset-standard))
772 "Alist of actions for the `Reset' button.
773 The key is a string containing the name of the action, the value is a
774 Lisp function taking the widget as an element which will be called
775 when the action is chosen.")
777 (defvar custom-options nil
778 "Customization widgets in the current buffer.")
780 (defun custom-command-apply (fun query &optional strong-query)
781 "Call function FUN on all widgets in `custom-options'.
782 If there is more than one widget, ask user for confirmation using
783 the query string QUERY, using `y-or-n-p' if STRONG-QUERY is nil,
784 and `yes-or-no-p' otherwise."
785 (if (or (and (= 1 (length custom-options))
786 (memq (widget-type (car custom-options))
787 '(custom-variable custom-face)))
788 (funcall (if strong-query 'yes-or-no-p 'y-or-n-p) query))
789 (progn (mapc fun custom-options) t)
790 (message "Aborted")
791 nil))
793 (defun Custom-set (&rest _ignore)
794 "Set the current value of all edited settings in the buffer."
795 (interactive)
796 (custom-command-apply
797 (lambda (child)
798 (when (eq (widget-get child :custom-state) 'modified)
799 (widget-apply child :custom-set)))
800 "Set all values according to this buffer? "))
802 (defun Custom-save (&rest _ignore)
803 "Set all edited settings, then save all settings that have been set.
804 If a setting was edited and set before, this saves it. If a
805 setting was merely edited before, this sets it then saves it."
806 (interactive)
807 (when (custom-command-apply
808 (lambda (child)
809 (when (memq (widget-get child :custom-state)
810 '(modified set changed rogue))
811 (widget-apply child :custom-mark-to-save)))
812 "Save all settings in this buffer? " t)
813 ;; Save changes to buffer and redraw.
814 (custom-save-all)
815 (dolist (child custom-options)
816 (widget-apply child :custom-state-set-and-redraw))))
818 (defun custom-reset (_widget &optional event)
819 "Select item from reset menu."
820 (let* ((completion-ignore-case t)
821 (answer (widget-choose "Reset settings"
822 custom-reset-menu
823 event)))
824 (if answer
825 (funcall answer))))
827 (defun Custom-reset-current (&rest _ignore)
828 "Reset all edited settings in the buffer to show their current values."
829 (interactive)
830 (custom-command-apply
831 (lambda (widget)
832 (if (memq (widget-get widget :custom-state) '(modified changed))
833 (widget-apply widget :custom-reset-current)))
834 "Reset all settings' buffer text to show current values? "))
836 (defun Custom-reset-saved (&rest _ignore)
837 "Reset all edited or set settings in the buffer to their saved value.
838 This also shows the saved values in the buffer."
839 (interactive)
840 (custom-command-apply
841 (lambda (widget)
842 (if (memq (widget-get widget :custom-state) '(modified set changed rogue))
843 (widget-apply widget :custom-reset-saved)))
844 "Reset all settings (current values and buffer text) to saved values? "))
846 ;; The next two variables are bound to '(t) by `Custom-reset-standard'
847 ;; and `custom-group-reset-standard'. If these variables are nil, both
848 ;; `custom-variable-reset-standard' and `custom-face-reset-standard'
849 ;; save, reset and redraw the handled widget immediately. Otherwise,
850 ;; they add the widget to the corresponding list and leave it to
851 ;; `custom-reset-standard-save-and-update' to save, reset and redraw it.
852 (defvar custom-reset-standard-variables-list nil)
853 (defvar custom-reset-standard-faces-list nil)
855 ;; The next function was excerpted from `custom-variable-reset-standard'
856 ;; and `custom-face-reset-standard' and is used to avoid calling
857 ;; `custom-save-all' repeatedly (and thus saving settings to file one by
858 ;; one) when erasing all customizations.
859 (defun custom-reset-standard-save-and-update ()
860 "Save settings and redraw after erasing customizations."
861 (when (or (and custom-reset-standard-variables-list
862 (not (eq custom-reset-standard-variables-list '(t))))
863 (and custom-reset-standard-faces-list
864 (not (eq custom-reset-standard-faces-list '(t)))))
865 ;; Save settings to file.
866 (custom-save-all)
867 ;; Set state of and redraw variables.
868 (dolist (widget custom-reset-standard-variables-list)
869 (unless (eq widget t)
870 (widget-put widget :custom-state 'unknown)
871 (custom-redraw widget)))
872 ;; Set state of and redraw faces.
873 (dolist (widget custom-reset-standard-faces-list)
874 (unless (eq widget t)
875 (let* ((symbol (widget-value widget))
876 (child (car (widget-get widget :children)))
877 (comment-widget (widget-get widget :comment-widget)))
878 (put symbol 'face-comment nil)
879 (widget-value-set child
880 (custom-pre-filter-face-spec
881 (list (list t (custom-face-attributes-get
882 symbol nil)))))
883 ;; This call manages the comment visibility
884 (widget-value-set comment-widget "")
885 (custom-face-state-set widget)
886 (custom-redraw-magic widget))))))
888 (defun Custom-reset-standard (&rest _ignore)
889 "Erase all customizations (either current or saved) in current buffer.
890 The immediate result is to restore them to their standard values.
891 This operation eliminates any saved values for the group members,
892 making them as if they had never been customized at all."
893 (interactive)
894 ;; Bind these temporarily.
895 (let ((custom-reset-standard-variables-list '(t))
896 (custom-reset-standard-faces-list '(t)))
897 (custom-command-apply
898 (lambda (widget)
899 (and (or (null (widget-get widget :custom-standard-value))
900 (widget-apply widget :custom-standard-value))
901 (memq (widget-get widget :custom-state)
902 '(modified set changed saved rogue))
903 (widget-apply widget :custom-mark-to-reset-standard)))
904 "Erase all customizations for settings in this buffer? " t)
905 (custom-reset-standard-save-and-update)))
907 ;;; The Customize Commands
909 (defun custom-prompt-variable (prompt-var prompt-val &optional comment)
910 "Prompt for a variable and a value and return them as a list.
911 PROMPT-VAR is the prompt for the variable, and PROMPT-VAL is the
912 prompt for the value. The %s escape in PROMPT-VAL is replaced with
913 the name of the variable.
915 If the variable has a `variable-interactive' property, that is used as if
916 it were the arg to `interactive' (which see) to interactively read the value.
918 If the variable has a `custom-type' property, it must be a widget and the
919 `:prompt-value' property of that widget will be used for reading the value.
920 If the variable also has a `custom-get' property, that is used for finding
921 the current value of the variable, otherwise `symbol-value' is used.
923 If optional COMMENT argument is non-nil, also prompt for a comment and return
924 it as the third element in the list."
925 (let* ((var (read-variable prompt-var))
926 (minibuffer-help-form '(describe-variable var))
927 (val
928 (let ((prop (get var 'variable-interactive))
929 (type (get var 'custom-type))
930 (prompt (format prompt-val var)))
931 (unless (listp type)
932 (setq type (list type)))
933 (cond (prop
934 ;; Use VAR's `variable-interactive' property
935 ;; as an interactive spec for prompting.
936 (call-interactively `(lambda (arg)
937 (interactive ,prop)
938 arg)))
939 (type
940 (widget-prompt-value type
941 prompt
942 (if (boundp var)
943 (funcall
944 (or (get var 'custom-get) 'symbol-value)
945 var))
946 (not (boundp var))))
948 (eval-minibuffer prompt))))))
949 (if comment
950 (list var val
951 (read-string "Comment: " (get var 'variable-comment)))
952 (list var val))))
954 ;;;###autoload
955 (defun customize-set-value (variable value &optional comment)
956 "Set VARIABLE to VALUE, and return VALUE. VALUE is a Lisp object.
958 If VARIABLE has a `variable-interactive' property, that is used as if
959 it were the arg to `interactive' (which see) to interactively read the value.
961 If VARIABLE has a `custom-type' property, it must be a widget and the
962 `:prompt-value' property of that widget will be used for reading the value.
964 If given a prefix (or a COMMENT argument), also prompt for a comment."
965 (interactive (custom-prompt-variable "Set variable: "
966 "Set %s to value: "
967 current-prefix-arg))
969 (cond ((string= comment "")
970 (put variable 'variable-comment nil))
971 (comment
972 (put variable 'variable-comment comment)))
973 (set variable value))
975 ;;;###autoload
976 (defun customize-set-variable (variable value &optional comment)
977 "Set the default for VARIABLE to VALUE, and return VALUE.
978 VALUE is a Lisp object.
980 If VARIABLE has a `custom-set' property, that is used for setting
981 VARIABLE, otherwise `set-default' is used.
983 If VARIABLE has a `variable-interactive' property, that is used as if
984 it were the arg to `interactive' (which see) to interactively read the value.
986 If VARIABLE has a `custom-type' property, it must be a widget and the
987 `:prompt-value' property of that widget will be used for reading the value.
989 If given a prefix (or a COMMENT argument), also prompt for a comment."
990 (interactive (custom-prompt-variable "Set variable: "
991 "Set customized value for %s to: "
992 current-prefix-arg))
993 (custom-load-symbol variable)
994 (custom-push-theme 'theme-value variable 'user 'set (custom-quote value))
995 (funcall (or (get variable 'custom-set) 'set-default) variable value)
996 (put variable 'customized-value (list (custom-quote value)))
997 (cond ((string= comment "")
998 (put variable 'variable-comment nil)
999 (put variable 'customized-variable-comment nil))
1000 (comment
1001 (put variable 'variable-comment comment)
1002 (put variable 'customized-variable-comment comment)))
1003 value)
1005 ;;;###autoload
1006 (defun customize-save-variable (variable value &optional comment)
1007 "Set the default for VARIABLE to VALUE, and save it for future sessions.
1008 Return VALUE.
1010 If VARIABLE has a `custom-set' property, that is used for setting
1011 VARIABLE, otherwise `set-default' is used.
1013 If VARIABLE has a `variable-interactive' property, that is used as if
1014 it were the arg to `interactive' (which see) to interactively read the value.
1016 If VARIABLE has a `custom-type' property, it must be a widget and the
1017 `:prompt-value' property of that widget will be used for reading the value.
1019 If given a prefix (or a COMMENT argument), also prompt for a comment."
1020 (interactive (custom-prompt-variable "Set and save variable: "
1021 "Set and save value for %s as: "
1022 current-prefix-arg))
1023 (funcall (or (get variable 'custom-set) 'set-default) variable value)
1024 (put variable 'saved-value (list (custom-quote value)))
1025 (custom-push-theme 'theme-value variable 'user 'set (custom-quote value))
1026 (cond ((string= comment "")
1027 (put variable 'variable-comment nil)
1028 (put variable 'saved-variable-comment nil))
1029 (comment
1030 (put variable 'variable-comment comment)
1031 (put variable 'saved-variable-comment comment)))
1032 (put variable 'customized-value nil)
1033 (put variable 'customized-variable-comment nil)
1034 (if (custom-file t)
1035 (custom-save-all)
1036 (message "Setting `%s' temporarily since \"emacs -q\" would overwrite customizations"
1037 variable)
1038 (set variable value))
1039 value)
1041 ;; Some parts of Emacs might prompt the user to save customizations,
1042 ;; during startup before customizations are loaded. This function
1043 ;; handles this corner case by avoiding calling `custom-save-variable'
1044 ;; too early, which could wipe out existing customizations.
1046 ;;;###autoload
1047 (defun customize-push-and-save (list-var elts)
1048 "Add ELTS to LIST-VAR and save for future sessions, safely.
1049 ELTS should be a list. This function adds each entry to the
1050 value of LIST-VAR using `add-to-list'.
1052 If Emacs is initialized, call `customize-save-variable' to save
1053 the resulting list value now. Otherwise, add an entry to
1054 `after-init-hook' to save it after initialization."
1055 (dolist (entry elts)
1056 (add-to-list list-var entry))
1057 (if after-init-time
1058 (let ((coding-system-for-read nil))
1059 (customize-save-variable list-var (eval list-var)))
1060 (add-hook 'after-init-hook
1061 `(lambda ()
1062 (customize-push-and-save ',list-var ',elts)))))
1064 ;;;###autoload
1065 (defun customize ()
1066 "Select a customization buffer which you can use to set user options.
1067 User options are structured into \"groups\".
1068 Initially the top-level group `Emacs' and its immediate subgroups
1069 are shown; the contents of those subgroups are initially hidden."
1070 (interactive)
1071 (customize-group 'emacs))
1073 ;;;###autoload
1074 (defun customize-mode (mode)
1075 "Customize options related to the current major mode.
1076 If a prefix \\[universal-argument] was given (or if the current major mode has no known group),
1077 then prompt for the MODE to customize."
1078 (interactive
1079 (list
1080 (let ((completion-regexp-list '("-mode\\'"))
1081 (group (custom-group-of-mode major-mode)))
1082 (if (and group (not current-prefix-arg))
1083 major-mode
1084 (intern
1085 (completing-read (if group
1086 (format "Major mode (default %s): " major-mode)
1087 "Major mode: ")
1088 obarray
1089 'custom-group-of-mode
1090 t nil nil (if group (symbol-name major-mode))))))))
1091 (customize-group (custom-group-of-mode mode)))
1093 (defun customize-read-group ()
1094 (let ((completion-ignore-case t))
1095 (completing-read "Customize group (default emacs): "
1096 obarray
1097 (lambda (symbol)
1098 (or (and (get symbol 'custom-loads)
1099 (not (get symbol 'custom-autoload)))
1100 (get symbol 'custom-group)))
1101 t)))
1103 ;;;###autoload
1104 (defun customize-group (&optional group other-window)
1105 "Customize GROUP, which must be a customization group.
1106 If OTHER-WINDOW is non-nil, display in another window."
1107 (interactive (list (customize-read-group)))
1108 (when (stringp group)
1109 (if (string-equal "" group)
1110 (setq group 'emacs)
1111 (setq group (intern group))))
1112 (let ((name (format "*Customize Group: %s*"
1113 (custom-unlispify-tag-name group))))
1114 (if (get-buffer name)
1115 (switch-to-buffer name other-window)
1116 (funcall (if other-window
1117 'custom-buffer-create-other-window
1118 'custom-buffer-create)
1119 (list (list group 'custom-group))
1120 name
1121 (concat " for group "
1122 (custom-unlispify-tag-name group))))))
1124 ;;;###autoload
1125 (defun customize-group-other-window (&optional group)
1126 "Customize GROUP, which must be a customization group, in another window."
1127 (interactive (list (customize-read-group)))
1128 (customize-group group t))
1130 ;;;###autoload
1131 (defalias 'customize-variable 'customize-option)
1133 ;;;###autoload
1134 (defun customize-option (symbol)
1135 "Customize SYMBOL, which must be a user option variable."
1136 (interactive (custom-variable-prompt))
1137 (unless symbol
1138 (error "No variable specified"))
1139 (let ((basevar (indirect-variable symbol)))
1140 (custom-buffer-create (list (list basevar 'custom-variable))
1141 (format "*Customize Option: %s*"
1142 (custom-unlispify-tag-name basevar)))
1143 (unless (eq symbol basevar)
1144 (message "`%s' is an alias for `%s'" symbol basevar))))
1146 ;;;###autoload
1147 (defalias 'customize-variable-other-window 'customize-option-other-window)
1149 ;;;###autoload
1150 (defun customize-option-other-window (symbol)
1151 "Customize SYMBOL, which must be a user option variable.
1152 Show the buffer in another window, but don't select it."
1153 (interactive (custom-variable-prompt))
1154 (unless symbol
1155 (error "No variable specified"))
1156 (let ((basevar (indirect-variable symbol)))
1157 (custom-buffer-create-other-window
1158 (list (list basevar 'custom-variable))
1159 (format "*Customize Option: %s*" (custom-unlispify-tag-name basevar)))
1160 (unless (eq symbol basevar)
1161 (message "`%s' is an alias for `%s'" symbol basevar))))
1163 (defvar customize-changed-options-previous-release "23.1"
1164 "Version for `customize-changed-options' to refer back to by default.")
1166 ;; Packages will update this variable, so make it available.
1167 ;;;###autoload
1168 (defvar customize-package-emacs-version-alist nil
1169 "Alist mapping versions of a package to Emacs versions.
1170 We use this for packages that have their own names, but are released
1171 as part of Emacs itself.
1173 Each elements looks like this:
1175 (PACKAGE (PVERSION . EVERSION)...)
1177 Here PACKAGE is the name of a package, as a symbol. After
1178 PACKAGE come one or more elements, each associating a
1179 package version PVERSION with the first Emacs version
1180 EVERSION in which it (or a subsequent version of PACKAGE)
1181 was first released. Both PVERSION and EVERSION are strings.
1182 PVERSION should be a string that this package used in
1183 the :package-version keyword for `defcustom', `defgroup',
1184 and `defface'.
1186 For example, the MH-E package updates this alist as follows:
1188 (add-to-list 'customize-package-emacs-version-alist
1189 '(MH-E (\"6.0\" . \"22.1\") (\"6.1\" . \"22.1\")
1190 (\"7.0\" . \"22.1\") (\"7.1\" . \"22.1\")
1191 (\"7.2\" . \"22.1\") (\"7.3\" . \"22.1\")
1192 (\"7.4\" . \"22.1\") (\"8.0\" . \"22.1\")))
1194 The value of PACKAGE needs to be unique and it needs to match the
1195 PACKAGE value appearing in the :package-version keyword. Since
1196 the user might see the value in a error message, a good choice is
1197 the official name of the package, such as MH-E or Gnus.")
1199 ;;;###autoload
1200 (defalias 'customize-changed 'customize-changed-options)
1202 ;;;###autoload
1203 (defun customize-changed-options (&optional since-version)
1204 "Customize all settings whose meanings have changed in Emacs itself.
1205 This includes new user option variables and faces, and new
1206 customization groups, as well as older options and faces whose meanings
1207 or default values have changed since the previous major Emacs release.
1209 With argument SINCE-VERSION (a string), customize all settings
1210 that were added or redefined since that version."
1212 (interactive
1213 (list
1214 (read-from-minibuffer
1215 (format "Customize options changed, since version (default %s): "
1216 customize-changed-options-previous-release))))
1217 (if (equal since-version "")
1218 (setq since-version nil)
1219 (unless (condition-case nil
1220 (numberp (read since-version))
1221 (error nil))
1222 (signal 'wrong-type-argument (list 'numberp since-version))))
1223 (unless since-version
1224 (setq since-version customize-changed-options-previous-release))
1226 ;; Load the information for versions since since-version. We use
1227 ;; custom-load-symbol for this.
1228 (put 'custom-versions-load-alist 'custom-loads nil)
1229 (dolist (elt custom-versions-load-alist)
1230 (if (customize-version-lessp since-version (car elt))
1231 (dolist (load (cdr elt))
1232 (custom-add-load 'custom-versions-load-alist load))))
1233 (custom-load-symbol 'custom-versions-load-alist)
1234 (put 'custom-versions-load-alist 'custom-loads nil)
1236 (let (found)
1237 (mapatoms
1238 (lambda (symbol)
1239 (let* ((package-version (get symbol 'custom-package-version))
1240 (version
1241 (or (and package-version
1242 (customize-package-emacs-version symbol
1243 package-version))
1244 (get symbol 'custom-version))))
1245 (if version
1246 (when (customize-version-lessp since-version version)
1247 (if (or (get symbol 'custom-group)
1248 (get symbol 'group-documentation))
1249 (push (list symbol 'custom-group) found))
1250 (if (custom-variable-p symbol)
1251 (push (list symbol 'custom-variable) found))
1252 (if (custom-facep symbol)
1253 (push (list symbol 'custom-face) found)))))))
1254 (if found
1255 (custom-buffer-create (custom-sort-items found t 'first)
1256 "*Customize Changed Options*")
1257 (error "No user option defaults have been changed since Emacs %s"
1258 since-version))))
1260 (defun customize-package-emacs-version (symbol package-version)
1261 "Return the Emacs version in which SYMBOL's meaning last changed.
1262 PACKAGE-VERSION has the form (PACKAGE . VERSION). We use
1263 `customize-package-emacs-version-alist' to find the version of
1264 Emacs that is associated with version VERSION of PACKAGE."
1265 (let (package-versions emacs-version)
1266 ;; Use message instead of error since we want user to be able to
1267 ;; see the rest of the symbols even if a package author has
1268 ;; botched things up.
1269 (cond ((not (listp package-version))
1270 (message "Invalid package-version value for %s" symbol))
1271 ((setq package-versions (assq (car package-version)
1272 customize-package-emacs-version-alist))
1273 (setq emacs-version
1274 (cdr (assoc (cdr package-version) package-versions)))
1275 (unless emacs-version
1276 (message "%s version %s not found in %s" symbol
1277 (cdr package-version)
1278 "customize-package-emacs-version-alist")))
1280 (message "Package %s version %s lists no corresponding Emacs version"
1281 (car package-version)
1282 (cdr package-version))))
1283 emacs-version))
1285 (defun customize-version-lessp (version1 version2)
1286 ;; Why are the versions strings, and given that they are, why aren't
1287 ;; they converted to numbers and compared as such here? -- fx
1289 ;; In case someone made a mistake and left out the quotes
1290 ;; in the :version value.
1291 (if (numberp version2)
1292 (setq version2 (prin1-to-string version2)))
1293 (let (major1 major2 minor1 minor2)
1294 (string-match "\\([0-9]+\\)\\(\\.\\([0-9]+\\)\\)?" version1)
1295 (setq major1 (read (or (match-string 1 version1)
1296 "0")))
1297 (setq minor1 (read (or (match-string 3 version1)
1298 "0")))
1299 (string-match "\\([0-9]+\\)\\(\\.\\([0-9]+\\)\\)?" version2)
1300 (setq major2 (read (or (match-string 1 version2)
1301 "0")))
1302 (setq minor2 (read (or (match-string 3 version2)
1303 "0")))
1304 (or (< major1 major2)
1305 (and (= major1 major2)
1306 (< minor1 minor2)))))
1308 ;;;###autoload
1309 (defun customize-face (&optional face other-window)
1310 "Customize FACE, which should be a face name or nil.
1311 If FACE is nil, customize all faces. If FACE is actually a
1312 face-alias, customize the face it is aliased to.
1314 If OTHER-WINDOW is non-nil, display in another window.
1316 Interactively, when point is on text which has a face specified,
1317 suggest to customize that face, if it's customizable."
1318 (interactive (list (read-face-name "Customize face" "all faces" t)))
1319 (if (member face '(nil ""))
1320 (setq face (face-list)))
1321 (if (and (listp face) (null (cdr face)))
1322 (setq face (car face)))
1323 (let ((display-fun (if other-window
1324 'custom-buffer-create-other-window
1325 'custom-buffer-create)))
1326 (if (listp face)
1327 (funcall display-fun
1328 (custom-sort-items
1329 (mapcar (lambda (s) (list s 'custom-face)) face)
1330 t nil)
1331 "*Customize Faces*")
1332 ;; If FACE is actually an alias, customize the face it is aliased to.
1333 (if (get face 'face-alias)
1334 (setq face (get face 'face-alias)))
1335 (unless (facep face)
1336 (error "Invalid face %S" face))
1337 (funcall display-fun
1338 (list (list face 'custom-face))
1339 (format "*Customize Face: %s*"
1340 (custom-unlispify-tag-name face))))))
1342 ;;;###autoload
1343 (defun customize-face-other-window (&optional face)
1344 "Show customization buffer for face FACE in other window.
1345 If FACE is actually a face-alias, customize the face it is aliased to.
1347 Interactively, when point is on text which has a face specified,
1348 suggest to customize that face, if it's customizable."
1349 (interactive (list (read-face-name "Customize face" "all faces" t)))
1350 (customize-face face t))
1352 (defalias 'customize-customized 'customize-unsaved)
1354 ;;;###autoload
1355 (defun customize-unsaved ()
1356 "Customize all user options set in this session but not saved."
1357 (interactive)
1358 (let ((found nil))
1359 (mapatoms (lambda (symbol)
1360 (and (or (get symbol 'customized-face)
1361 (get symbol 'customized-face-comment))
1362 (custom-facep symbol)
1363 (push (list symbol 'custom-face) found))
1364 (and (or (get symbol 'customized-value)
1365 (get symbol 'customized-variable-comment))
1366 (boundp symbol)
1367 (push (list symbol 'custom-variable) found))))
1368 (if (not found)
1369 (error "No user options are set but unsaved")
1370 (custom-buffer-create (custom-sort-items found t nil)
1371 "*Customize Unsaved*"))))
1373 ;;;###autoload
1374 (defun customize-rogue ()
1375 "Customize all user variables modified outside customize."
1376 (interactive)
1377 (let ((found nil))
1378 (mapatoms (lambda (symbol)
1379 (let ((cval (or (get symbol 'customized-value)
1380 (get symbol 'saved-value)
1381 (get symbol 'standard-value))))
1382 (when (and cval ;Declared with defcustom.
1383 (default-boundp symbol) ;Has a value.
1384 (not (equal (eval (car cval))
1385 ;; Which does not match customize.
1386 (default-value symbol))))
1387 (push (list symbol 'custom-variable) found)))))
1388 (if (not found)
1389 (error "No rogue user options")
1390 (custom-buffer-create (custom-sort-items found t nil)
1391 "*Customize Rogue*"))))
1392 ;;;###autoload
1393 (defun customize-saved ()
1394 "Customize all already saved user options."
1395 (interactive)
1396 (let ((found nil))
1397 (mapatoms (lambda (symbol)
1398 (and (or (get symbol 'saved-face)
1399 (get symbol 'saved-face-comment))
1400 (custom-facep symbol)
1401 (push (list symbol 'custom-face) found))
1402 (and (or (get symbol 'saved-value)
1403 (get symbol 'saved-variable-comment))
1404 (boundp symbol)
1405 (push (list symbol 'custom-variable) found))))
1406 (if (not found )
1407 (error "No saved user options")
1408 (custom-buffer-create (custom-sort-items found t nil)
1409 "*Customize Saved*"))))
1411 (declare-function apropos-parse-pattern "apropos" (pattern))
1413 ;;;###autoload
1414 (defun customize-apropos (pattern &optional type)
1415 "Customize all loaded options, faces and groups matching PATTERN.
1416 PATTERN can be a word, a list of words (separated by spaces),
1417 or a regexp (using some regexp special characters). If it is a word,
1418 search for matches for that word as a substring. If it is a list of words,
1419 search for matches for any two (or more) of those words.
1421 If TYPE is `options', include only options.
1422 If TYPE is `faces', include only faces.
1423 If TYPE is `groups', include only groups.
1424 If TYPE is t (interactively, with prefix arg), include variables
1425 that are not customizable options, as well as faces and groups
1426 \(but we recommend using `apropos-variable' instead)."
1427 (interactive (list (apropos-read-pattern "symbol") current-prefix-arg))
1428 (require 'apropos)
1429 (apropos-parse-pattern pattern)
1430 (let (found)
1431 (mapatoms
1432 `(lambda (symbol)
1433 (when (string-match apropos-regexp (symbol-name symbol))
1434 ,(if (not (memq type '(faces options)))
1435 '(if (get symbol 'custom-group)
1436 (push (list symbol 'custom-group) found)))
1437 ,(if (not (memq type '(options groups)))
1438 '(if (custom-facep symbol)
1439 (push (list symbol 'custom-face) found)))
1440 ,(if (not (memq type '(groups faces)))
1441 `(if (and (boundp symbol)
1442 (eq (indirect-variable symbol) symbol)
1443 (or (get symbol 'saved-value)
1444 (custom-variable-p symbol)
1445 ,(if (not (memq type '(nil options)))
1446 '(get symbol 'variable-documentation))))
1447 (push (list symbol 'custom-variable) found))))))
1448 (if (not found)
1449 (error "No %s matching %s"
1450 (if (eq type t)
1451 "items"
1452 (format "customizable %s"
1453 (if (memq type '(options faces groups))
1454 (symbol-name type)
1455 "items")))
1456 pattern)
1457 (custom-buffer-create
1458 (custom-sort-items found t custom-buffer-order-groups)
1459 "*Customize Apropos*"))))
1461 ;;;###autoload
1462 (defun customize-apropos-options (regexp &optional arg)
1463 "Customize all loaded customizable options matching REGEXP.
1464 With prefix ARG, include variables that are not customizable options
1465 \(but it is better to use `apropos-variable' if you want to find those)."
1466 (interactive "sCustomize options (regexp): \nP")
1467 (customize-apropos regexp (or arg 'options)))
1469 ;;;###autoload
1470 (defun customize-apropos-faces (regexp)
1471 "Customize all loaded faces matching REGEXP."
1472 (interactive "sCustomize faces (regexp): \n")
1473 (customize-apropos regexp 'faces))
1475 ;;;###autoload
1476 (defun customize-apropos-groups (regexp)
1477 "Customize all loaded groups matching REGEXP."
1478 (interactive "sCustomize groups (regexp): \n")
1479 (customize-apropos regexp 'groups))
1481 ;;; Buffer.
1483 (defcustom custom-buffer-style 'links
1484 "Control the presentation style for customization buffers.
1485 The value should be a symbol, one of:
1487 brackets: groups nest within each other with big horizontal brackets.
1488 links: groups have links to subgroups."
1489 :type '(radio (const brackets)
1490 (const links))
1491 :group 'custom-buffer)
1493 (defcustom custom-buffer-done-kill nil
1494 "Non-nil means exiting a Custom buffer should kill it."
1495 :type 'boolean
1496 :version "22.1"
1497 :group 'custom-buffer)
1499 (defcustom custom-buffer-indent 3
1500 "Number of spaces to indent nested groups."
1501 :type 'integer
1502 :group 'custom-buffer)
1504 (defun custom-get-fresh-buffer (name)
1505 "Get a fresh new buffer with name NAME.
1506 If the buffer already exist, clean it up to be like new.
1507 Beware: it's not quite like new. Good enough for custom, but maybe
1508 not for everybody."
1509 ;; To be more complete, we should also kill all permanent-local variables,
1510 ;; but it's not needed for custom.
1511 (let ((buf (get-buffer name)))
1512 (when (and buf (buffer-local-value 'buffer-file-name buf))
1513 ;; This will check if the file is not saved.
1514 (kill-buffer buf)
1515 (setq buf nil))
1516 (if (null buf)
1517 (get-buffer-create name)
1518 (with-current-buffer buf
1519 (kill-all-local-variables)
1520 (run-hooks 'kill-buffer-hook)
1521 ;; Delete overlays before erasing the buffer so the overlay hooks
1522 ;; don't get run spuriously when we erase the buffer.
1523 (let ((ols (overlay-lists)))
1524 (dolist (ol (nconc (car ols) (cdr ols)))
1525 (delete-overlay ol)))
1526 (erase-buffer)
1527 buf))))
1529 ;;;###autoload
1530 (defun custom-buffer-create (options &optional name description)
1531 "Create a buffer containing OPTIONS.
1532 Optional NAME is the name of the buffer.
1533 OPTIONS should be an alist of the form ((SYMBOL WIDGET)...), where
1534 SYMBOL is a customization option, and WIDGET is a widget for editing
1535 that option."
1536 (switch-to-buffer (custom-get-fresh-buffer (or name "*Customization*")))
1537 (custom-buffer-create-internal options description))
1539 ;;;###autoload
1540 (defun custom-buffer-create-other-window (options &optional name description)
1541 "Create a buffer containing OPTIONS, and display it in another window.
1542 The result includes selecting that window.
1543 Optional NAME is the name of the buffer.
1544 OPTIONS should be an alist of the form ((SYMBOL WIDGET)...), where
1545 SYMBOL is a customization option, and WIDGET is a widget for editing
1546 that option."
1547 (unless name (setq name "*Customization*"))
1548 (switch-to-buffer-other-window (custom-get-fresh-buffer name))
1549 (custom-buffer-create-internal options description))
1551 (defcustom custom-reset-button-menu nil
1552 "If non-nil, only show a single reset button in customize buffers.
1553 This button will have a menu with all three reset operations."
1554 :type 'boolean
1555 :group 'custom-buffer)
1557 (defcustom custom-buffer-verbose-help t
1558 "If non-nil, include explanatory text in the customization buffer."
1559 :type 'boolean
1560 :group 'custom-buffer)
1562 (defun Custom-buffer-done (&rest _ignore)
1563 "Exit current Custom buffer according to `custom-buffer-done-kill'."
1564 (interactive)
1565 (quit-window custom-buffer-done-kill))
1567 (defvar custom-button nil
1568 "Face used for buttons in customization buffers.")
1570 (defvar custom-button-mouse nil
1571 "Mouse face used for buttons in customization buffers.")
1573 (defvar custom-button-pressed nil
1574 "Face used for pressed buttons in customization buffers.")
1576 (defcustom custom-search-field t
1577 "If non-nil, show a search field in Custom buffers."
1578 :type 'boolean
1579 :version "24.1"
1580 :group 'custom-buffer)
1582 (defcustom custom-raised-buttons (not (equal (face-valid-attribute-values :box)
1583 '(("unspecified" . unspecified))))
1584 "If non-nil, indicate active buttons in a `raised-button' style.
1585 Otherwise use brackets."
1586 :type 'boolean
1587 :version "21.1"
1588 :group 'custom-buffer
1589 :set (lambda (variable value)
1590 (custom-set-default variable value)
1591 (setq custom-button
1592 (if value 'custom-button 'custom-button-unraised))
1593 (setq custom-button-mouse
1594 (if value 'custom-button-mouse 'highlight))
1595 (setq custom-button-pressed
1596 (if value
1597 'custom-button-pressed
1598 'custom-button-pressed-unraised))))
1600 (defun custom-buffer-create-internal (options &optional _description)
1601 (Custom-mode)
1602 (let ((init-file (or custom-file user-init-file)))
1603 ;; Insert verbose help at the top of the custom buffer.
1604 (when custom-buffer-verbose-help
1605 (widget-insert (if init-file
1606 "To apply changes, use the Save or Set buttons."
1607 "Custom settings cannot be saved; maybe you started Emacs with `-q'.")
1608 "\nFor details, see ")
1609 (widget-create 'custom-manual
1610 :tag "Saving Customizations"
1611 "(emacs)Saving Customizations")
1612 (widget-insert " in the ")
1613 (widget-create 'custom-manual
1614 :tag "Emacs manual"
1615 :help-echo "Read the Emacs manual."
1616 "(emacs)Top")
1617 (widget-insert "."))
1618 (widget-insert "\n")
1620 ;; Insert the search field.
1621 (when custom-search-field
1622 (widget-insert "\n")
1623 (let* ((echo "Search for custom items")
1624 (search-widget
1625 (widget-create
1626 'editable-field
1627 :size 40 :help-echo echo
1628 :action `(lambda (widget &optional event)
1629 (customize-apropos (split-string (widget-value widget)))))))
1630 (widget-insert " ")
1631 (widget-create-child-and-convert
1632 search-widget 'push-button
1633 :tag " Search "
1634 :help-echo echo :action
1635 (lambda (widget &optional _event)
1636 (customize-apropos (widget-value (widget-get widget :parent)))))
1637 (widget-insert "\n")))
1639 ;; The custom command buttons are also in the toolbar, so for a
1640 ;; time they were not inserted in the buffer if the toolbar was in use.
1641 ;; But it can be a little confusing for the buffer layout to
1642 ;; change according to whether or nor the toolbar is on, not to
1643 ;; mention that a custom buffer can in theory be created in a
1644 ;; frame with a toolbar, then later viewed in one without.
1645 ;; So now the buttons are always inserted in the buffer. (Bug#1326)
1646 (if custom-buffer-verbose-help
1647 (widget-insert "
1648 Operate on all settings in this buffer:\n"))
1649 (let ((button (lambda (tag action active help _icon _label)
1650 (widget-insert " ")
1651 (if (eval active)
1652 (widget-create 'push-button :tag tag
1653 :help-echo help :action action))))
1654 (commands custom-commands))
1655 (apply button (pop commands)) ; Set for current session
1656 (apply button (pop commands)) ; Save for future sessions
1657 (if custom-reset-button-menu
1658 (progn
1659 (widget-insert " ")
1660 (widget-create 'push-button
1661 :tag "Reset buffer"
1662 :help-echo "Show a menu with reset operations."
1663 :mouse-down-action 'ignore
1664 :action 'custom-reset))
1665 (widget-insert "\n")
1666 (apply button (pop commands)) ; Undo edits
1667 (apply button (pop commands)) ; Reset to saved
1668 (apply button (pop commands)) ; Erase customization
1669 (widget-insert " ")
1670 (pop commands) ; Help (omitted)
1671 (apply button (pop commands)))) ; Exit
1672 (widget-insert "\n\n"))
1674 ;; Now populate the custom buffer.
1675 (message "Creating customization items...")
1676 (buffer-disable-undo)
1677 (setq custom-options
1678 (if (= (length options) 1)
1679 (mapcar (lambda (entry)
1680 (widget-create (nth 1 entry)
1681 :documentation-shown t
1682 :custom-state 'unknown
1683 :tag (custom-unlispify-tag-name
1684 (nth 0 entry))
1685 :value (nth 0 entry)))
1686 options)
1687 (let ((count 0)
1688 (length (length options)))
1689 (mapcar (lambda (entry)
1690 (prog2
1691 (message "Creating customization items ...%2d%%"
1692 (/ (* 100.0 count) length))
1693 (widget-create (nth 1 entry)
1694 :tag (custom-unlispify-tag-name
1695 (nth 0 entry))
1696 :value (nth 0 entry))
1697 (setq count (1+ count))
1698 (unless (eq (preceding-char) ?\n)
1699 (widget-insert "\n"))
1700 (widget-insert "\n")))
1701 options))))
1702 (unless (eq (preceding-char) ?\n)
1703 (widget-insert "\n"))
1704 (message "Creating customization items ...done")
1705 (message "Resetting customization items...")
1706 (unless (eq custom-buffer-style 'tree)
1707 (mapc 'custom-magic-reset custom-options))
1708 (message "Resetting customization items...done")
1709 (message "Creating customization setup...")
1710 (widget-setup)
1711 (buffer-enable-undo)
1712 (goto-char (point-min))
1713 (message "Creating customization setup...done"))
1715 ;;; The Tree Browser.
1717 ;;;###autoload
1718 (defun customize-browse (&optional group)
1719 "Create a tree browser for the customize hierarchy."
1720 (interactive)
1721 (unless group
1722 (setq group 'emacs))
1723 (let ((name "*Customize Browser*"))
1724 (switch-to-buffer (custom-get-fresh-buffer name)))
1725 (Custom-mode)
1726 (widget-insert (format "\
1727 %s buttons; type RET or click mouse-1
1728 on a button to invoke its action.
1729 Invoke [+] to expand a group, and [-] to collapse an expanded group.\n"
1730 (if custom-raised-buttons
1731 "`Raised' text indicates"
1732 "Square brackets indicate")))
1735 (if custom-browse-only-groups
1736 (widget-insert "\
1737 Invoke the [Group] button below to edit that item in another window.\n\n")
1738 (widget-insert "Invoke the ")
1739 (widget-create 'item
1740 :format "%t"
1741 :tag "[Group]"
1742 :tag-glyph "folder")
1743 (widget-insert ", ")
1744 (widget-create 'item
1745 :format "%t"
1746 :tag "[Face]"
1747 :tag-glyph "face")
1748 (widget-insert ", and ")
1749 (widget-create 'item
1750 :format "%t"
1751 :tag "[Option]"
1752 :tag-glyph "option")
1753 (widget-insert " buttons below to edit that
1754 item in another window.\n\n"))
1755 (let ((custom-buffer-style 'tree))
1756 (widget-create 'custom-group
1757 :custom-last t
1758 :custom-state 'unknown
1759 :tag (custom-unlispify-tag-name group)
1760 :value group))
1761 (widget-setup)
1762 (goto-char (point-min)))
1764 (define-widget 'custom-browse-visibility 'item
1765 "Control visibility of items in the customize tree browser."
1766 :format "%[[%t]%]"
1767 :action 'custom-browse-visibility-action)
1769 (defun custom-browse-visibility-action (widget &rest _ignore)
1770 (let ((custom-buffer-style 'tree))
1771 (custom-toggle-parent widget)))
1773 (define-widget 'custom-browse-group-tag 'custom-group-link
1774 "Show parent in other window when activated."
1775 :tag "Group"
1776 :tag-glyph "folder"
1777 :action 'custom-browse-group-tag-action)
1779 (defun custom-browse-group-tag-action (widget &rest _ignore)
1780 (let ((parent (widget-get widget :parent)))
1781 (customize-group-other-window (widget-value parent))))
1783 (define-widget 'custom-browse-variable-tag 'custom-group-link
1784 "Show parent in other window when activated."
1785 :tag "Option"
1786 :tag-glyph "option"
1787 :action 'custom-browse-variable-tag-action)
1789 (defun custom-browse-variable-tag-action (widget &rest _ignore)
1790 (let ((parent (widget-get widget :parent)))
1791 (customize-variable-other-window (widget-value parent))))
1793 (define-widget 'custom-browse-face-tag 'custom-group-link
1794 "Show parent in other window when activated."
1795 :tag "Face"
1796 :tag-glyph "face"
1797 :action 'custom-browse-face-tag-action)
1799 (defun custom-browse-face-tag-action (widget &rest _ignore)
1800 (let ((parent (widget-get widget :parent)))
1801 (customize-face-other-window (widget-value parent))))
1803 (defconst custom-browse-alist '((" " "space")
1804 (" | " "vertical")
1805 ("-\\ " "top")
1806 (" |-" "middle")
1807 (" `-" "bottom")))
1809 (defun custom-browse-insert-prefix (prefix)
1810 "Insert PREFIX. On XEmacs convert it to line graphics."
1811 ;; Fixme: do graphics.
1812 (if nil ; (featurep 'xemacs)
1813 (progn
1814 (insert "*")
1815 (while (not (string-equal prefix ""))
1816 (let ((entry (substring prefix 0 3)))
1817 (setq prefix (substring prefix 3))
1818 (let ((overlay (make-overlay (1- (point)) (point) nil t nil))
1819 (name (nth 1 (assoc entry custom-browse-alist))))
1820 (overlay-put overlay 'end-glyph (widget-glyph-find name entry))
1821 (overlay-put overlay 'start-open t)
1822 (overlay-put overlay 'end-open t)))))
1823 (insert prefix)))
1825 ;;; Modification of Basic Widgets.
1827 ;; We add extra properties to the basic widgets needed here. This is
1828 ;; fine, as long as we are careful to stay within our own namespace.
1830 ;; We want simple widgets to be displayed by default, but complex
1831 ;; widgets to be hidden.
1833 ;; This widget type is obsolete as of Emacs 24.1.
1834 (widget-put (get 'item 'widget-type) :custom-show t)
1835 (widget-put (get 'editable-field 'widget-type)
1836 :custom-show (lambda (_widget value)
1837 (let ((pp (pp-to-string value)))
1838 (cond ((string-match "\n" pp)
1839 nil)
1840 ((> (length pp) 40)
1841 nil)
1842 (t t)))))
1843 (widget-put (get 'menu-choice 'widget-type) :custom-show t)
1845 ;;; The `custom-manual' Widget.
1847 (define-widget 'custom-manual 'info-link
1848 "Link to the manual entry for this customization option."
1849 :help-echo "Read the manual entry for this option."
1850 :keymap custom-mode-link-map
1851 :follow-link 'mouse-face
1852 :button-face 'custom-link
1853 :mouse-face 'highlight
1854 :pressed-face 'highlight
1855 :tag "Manual")
1857 ;;; The `custom-magic' Widget.
1859 (defgroup custom-magic-faces nil
1860 "Faces used by the magic button."
1861 :group 'custom-faces
1862 :group 'custom-buffer)
1864 (defface custom-invalid '((((class color))
1865 (:foreground "yellow1" :background "red1"))
1867 (:weight bold :slant italic :underline t)))
1868 "Face used when the customize item is invalid."
1869 :group 'custom-magic-faces)
1870 (define-obsolete-face-alias 'custom-invalid-face 'custom-invalid "22.1")
1872 (defface custom-rogue '((((class color))
1873 (:foreground "pink" :background "black"))
1875 (:underline t)))
1876 "Face used when the customize item is not defined for customization."
1877 :group 'custom-magic-faces)
1878 (define-obsolete-face-alias 'custom-rogue-face 'custom-rogue "22.1")
1880 (defface custom-modified '((((min-colors 88) (class color))
1881 (:foreground "white" :background "blue1"))
1882 (((class color))
1883 (:foreground "white" :background "blue"))
1885 (:slant italic :bold)))
1886 "Face used when the customize item has been modified."
1887 :group 'custom-magic-faces)
1888 (define-obsolete-face-alias 'custom-modified-face 'custom-modified "22.1")
1890 (defface custom-set '((((min-colors 88) (class color))
1891 (:foreground "blue1" :background "white"))
1892 (((class color))
1893 (:foreground "blue" :background "white"))
1895 (:slant italic)))
1896 "Face used when the customize item has been set."
1897 :group 'custom-magic-faces)
1898 (define-obsolete-face-alias 'custom-set-face 'custom-set "22.1")
1900 (defface custom-changed '((((min-colors 88) (class color))
1901 (:foreground "white" :background "blue1"))
1902 (((class color))
1903 (:foreground "white" :background "blue"))
1905 (:slant italic)))
1906 "Face used when the customize item has been changed."
1907 :group 'custom-magic-faces)
1908 (define-obsolete-face-alias 'custom-changed-face 'custom-changed "22.1")
1910 (defface custom-themed '((((min-colors 88) (class color))
1911 (:foreground "white" :background "blue1"))
1912 (((class color))
1913 (:foreground "white" :background "blue"))
1915 (:slant italic)))
1916 "Face used when the customize item has been set by a theme."
1917 :group 'custom-magic-faces)
1919 (defface custom-saved '((t (:underline t)))
1920 "Face used when the customize item has been saved."
1921 :group 'custom-magic-faces)
1922 (define-obsolete-face-alias 'custom-saved-face 'custom-saved "22.1")
1924 (defconst custom-magic-alist
1925 '((nil "#" underline "\
1926 UNINITIALIZED, you should not see this.")
1927 (unknown "?" italic "\
1928 UNKNOWN, you should not see this.")
1929 (hidden "-" default "\
1930 HIDDEN, invoke \"Show\" in the previous line to show." "\
1931 group now hidden, invoke \"Show\", above, to show contents.")
1932 (invalid "x" custom-invalid "\
1933 INVALID, the displayed value cannot be set.")
1934 (modified "*" custom-modified "\
1935 EDITED, shown value does not take effect until you set or save it." "\
1936 something in this group has been edited but not set.")
1937 (set "+" custom-set "\
1938 SET for current session only." "\
1939 something in this group has been set but not saved.")
1940 (changed ":" custom-changed "\
1941 CHANGED outside Customize." "\
1942 something in this group has been changed outside customize.")
1943 (saved "!" custom-saved "\
1944 SAVED and set." "\
1945 something in this group has been set and saved.")
1946 (themed "o" custom-themed "\
1947 THEMED." "\
1948 visible group members are all at standard values.")
1949 (rogue "@" custom-rogue "\
1950 NO CUSTOMIZATION DATA; not intended to be customized." "\
1951 something in this group is not prepared for customization.")
1952 (standard " " nil "\
1953 STANDARD." "\
1954 visible group members are all at standard values."))
1955 "Alist of customize option states.
1956 Each entry is of the form (STATE MAGIC FACE ITEM-DESC [ GROUP-DESC ]), where
1958 STATE is one of the following symbols:
1960 `nil'
1961 For internal use, should never occur.
1962 `unknown'
1963 For internal use, should never occur.
1964 `hidden'
1965 This item is not being displayed.
1966 `invalid'
1967 This item is modified, but has an invalid form.
1968 `modified'
1969 This item is modified, and has a valid form.
1970 `set'
1971 This item has been set but not saved.
1972 `changed'
1973 The current value of this item has been changed outside Customize.
1974 `saved'
1975 This item is marked for saving.
1976 `rogue'
1977 This item has no customization information.
1978 `standard'
1979 This item is unchanged from the standard setting.
1981 MAGIC is a string used to present that state.
1983 FACE is a face used to present the state.
1985 ITEM-DESC is a string describing the state for options.
1987 GROUP-DESC is a string describing the state for groups. If this is
1988 left out, ITEM-DESC will be used.
1990 The string %c in either description will be replaced with the
1991 category of the item. These are `group'. `option', and `face'.
1993 The list should be sorted most significant first.")
1995 (defcustom custom-magic-show 'long
1996 "If non-nil, show textual description of the state.
1997 If `long', show a full-line description, not just one word."
1998 :type '(choice (const :tag "no" nil)
1999 (const long)
2000 (other :tag "short" short))
2001 :group 'custom-buffer)
2003 (defcustom custom-magic-show-hidden '(option face)
2004 "Control whether the State button is shown for hidden items.
2005 The value should be a list with the custom categories where the State
2006 button should be visible. Possible categories are `group', `option',
2007 and `face'."
2008 :type '(set (const group) (const option) (const face))
2009 :group 'custom-buffer)
2011 (defcustom custom-magic-show-button nil
2012 "Show a \"magic\" button indicating the state of each customization option."
2013 :type 'boolean
2014 :group 'custom-buffer)
2016 (define-widget 'custom-magic 'default
2017 "Show and manipulate state for a customization option."
2018 :format "%v"
2019 :action 'widget-parent-action
2020 :notify 'ignore
2021 :value-get 'ignore
2022 :value-create 'custom-magic-value-create
2023 :value-delete 'widget-children-value-delete)
2025 (defun widget-magic-mouse-down-action (widget &optional _event)
2026 ;; Non-nil unless hidden.
2027 (not (eq (widget-get (widget-get (widget-get widget :parent) :parent)
2028 :custom-state)
2029 'hidden)))
2031 (defun custom-magic-value-create (widget)
2032 "Create compact status report for WIDGET."
2033 (let* ((parent (widget-get widget :parent))
2034 (state (widget-get parent :custom-state))
2035 (hidden (eq state 'hidden))
2036 (entry (assq state custom-magic-alist))
2037 (magic (nth 1 entry))
2038 (face (nth 2 entry))
2039 (category (widget-get parent :custom-category))
2040 (text (or (and (eq category 'group)
2041 (nth 4 entry))
2042 (nth 3 entry)))
2043 (form (widget-get parent :custom-form))
2044 children)
2045 (unless (eq state 'hidden)
2046 (while (string-match "\\`\\(.*\\)%c\\(.*\\)\\'" text)
2047 (setq text (concat (match-string 1 text)
2048 (symbol-name category)
2049 (match-string 2 text))))
2050 (when (and custom-magic-show
2051 (or (not hidden)
2052 (memq category custom-magic-show-hidden)))
2053 (insert " ")
2054 (when (and (eq category 'group)
2055 (not (and (eq custom-buffer-style 'links)
2056 (> (widget-get parent :custom-level) 1))))
2057 (insert-char ?\ (* custom-buffer-indent
2058 (widget-get parent :custom-level))))
2059 (push (widget-create-child-and-convert
2060 widget 'choice-item
2061 :help-echo "Change the state of this item."
2062 :format (if hidden "%t" "%[%t%]")
2063 :button-prefix 'widget-push-button-prefix
2064 :button-suffix 'widget-push-button-suffix
2065 :mouse-down-action 'widget-magic-mouse-down-action
2066 :tag " State ")
2067 children)
2068 (insert ": ")
2069 (let ((start (point)))
2070 (if (eq custom-magic-show 'long)
2071 (insert text)
2072 (insert (symbol-name state)))
2073 (cond ((eq form 'lisp)
2074 (insert " (lisp)"))
2075 ((eq form 'mismatch)
2076 (insert " (mismatch)")))
2077 (put-text-property start (point) 'face 'custom-state))
2078 (insert "\n"))
2079 (when (and (eq category 'group)
2080 (not (and (eq custom-buffer-style 'links)
2081 (> (widget-get parent :custom-level) 1))))
2082 (insert-char ?\ (* custom-buffer-indent
2083 (widget-get parent :custom-level))))
2084 (when custom-magic-show-button
2085 (when custom-magic-show
2086 (let ((indent (widget-get parent :indent)))
2087 (when indent
2088 (insert-char ? indent))))
2089 (push (widget-create-child-and-convert
2090 widget 'choice-item
2091 :mouse-down-action 'widget-magic-mouse-down-action
2092 :button-face face
2093 :button-prefix ""
2094 :button-suffix ""
2095 :help-echo "Change the state."
2096 :format (if hidden "%t" "%[%t%]")
2097 :tag (if (memq form '(lisp mismatch))
2098 (concat "(" magic ")")
2099 (concat "[" magic "]")))
2100 children)
2101 (insert " "))
2102 (widget-put widget :children children))))
2104 (defun custom-magic-reset (widget)
2105 "Redraw the :custom-magic property of WIDGET."
2106 (let ((magic (widget-get widget :custom-magic)))
2107 (when magic
2108 (widget-value-set magic (widget-value magic)))))
2110 ;;; The `custom' Widget.
2112 (defface custom-button
2113 '((((type x w32 ns) (class color)) ; Like default modeline
2114 (:box (:line-width 2 :style released-button)
2115 :background "lightgrey" :foreground "black"))
2117 nil))
2118 "Face for custom buffer buttons if `custom-raised-buttons' is non-nil."
2119 :version "21.1"
2120 :group 'custom-faces)
2121 (define-obsolete-face-alias 'custom-button-face 'custom-button "22.1")
2123 (defface custom-button-mouse
2124 '((((type x w32 ns) (class color))
2125 (:box (:line-width 2 :style released-button)
2126 :background "grey90" :foreground "black"))
2128 ;; This is for text terminals that support mouse, like GPM mouse
2129 ;; or the MS-DOS terminal: inverse-video makes the button stand
2130 ;; out on mouse-over.
2131 (:inverse-video t)))
2132 "Mouse face for custom buffer buttons if `custom-raised-buttons' is non-nil."
2133 :version "22.1"
2134 :group 'custom-faces)
2136 (defface custom-button-unraised
2137 '((t :inherit underline))
2138 "Face for custom buffer buttons if `custom-raised-buttons' is nil."
2139 :version "22.1"
2140 :group 'custom-faces)
2142 (setq custom-button
2143 (if custom-raised-buttons 'custom-button 'custom-button-unraised))
2145 (setq custom-button-mouse
2146 (if custom-raised-buttons 'custom-button-mouse 'highlight))
2148 (defface custom-button-pressed
2149 '((((type x w32 ns) (class color))
2150 (:box (:line-width 2 :style pressed-button)
2151 :background "lightgrey" :foreground "black"))
2153 (:inverse-video t)))
2154 "Face for pressed custom buttons if `custom-raised-buttons' is non-nil."
2155 :version "21.1"
2156 :group 'custom-faces)
2157 (define-obsolete-face-alias 'custom-button-pressed-face
2158 'custom-button-pressed "22.1")
2160 (defface custom-button-pressed-unraised
2161 '((default :inherit custom-button-unraised)
2162 (((class color) (background light)) :foreground "magenta4")
2163 (((class color) (background dark)) :foreground "violet"))
2164 "Face for pressed custom buttons if `custom-raised-buttons' is nil."
2165 :version "22.1"
2166 :group 'custom-faces)
2168 (setq custom-button-pressed
2169 (if custom-raised-buttons
2170 'custom-button-pressed
2171 'custom-button-pressed-unraised))
2173 (defface custom-documentation '((t nil))
2174 "Face used for documentation strings in customization buffers."
2175 :group 'custom-faces)
2176 (define-obsolete-face-alias 'custom-documentation-face
2177 'custom-documentation "22.1")
2179 (defface custom-state '((((class color)
2180 (background dark))
2181 (:foreground "lime green"))
2182 (((class color)
2183 (background light))
2184 (:foreground "dark green"))
2185 (t nil))
2186 "Face used for State descriptions in the customize buffer."
2187 :group 'custom-faces)
2188 (define-obsolete-face-alias 'custom-state-face 'custom-state "22.1")
2190 (defface custom-link
2191 '((t :inherit link))
2192 "Face for links in customization buffers."
2193 :version "22.1"
2194 :group 'custom-faces)
2196 (define-widget 'custom 'default
2197 "Customize a user option."
2198 :format "%v"
2199 :convert-widget 'custom-convert-widget
2200 :notify 'custom-notify
2201 :custom-prefix ""
2202 :custom-level 1
2203 :custom-state 'hidden
2204 :documentation-property 'widget-subclass-responsibility
2205 :value-create 'widget-subclass-responsibility
2206 :value-delete 'widget-children-value-delete
2207 :value-get 'widget-value-value-get
2208 :validate 'widget-children-validate
2209 :match (lambda (_widget value) (symbolp value)))
2211 (defun custom-convert-widget (widget)
2212 "Initialize :value and :tag from :args in WIDGET."
2213 (let ((args (widget-get widget :args)))
2214 (when args
2215 (widget-put widget :value (widget-apply widget
2216 :value-to-internal (car args)))
2217 (widget-put widget :tag (custom-unlispify-tag-name (car args)))
2218 (widget-put widget :args nil)))
2219 widget)
2221 (defun custom-notify (widget &rest args)
2222 "Keep track of changes."
2223 (let ((state (widget-get widget :custom-state)))
2224 (unless (eq state 'modified)
2225 (unless (memq state '(nil unknown hidden))
2226 (widget-put widget :custom-state 'modified))
2227 (custom-magic-reset widget)
2228 (apply 'widget-default-notify widget args))))
2230 (defun custom-redraw (widget)
2231 "Redraw WIDGET with current settings."
2232 (let ((line (count-lines (point-min) (point)))
2233 (column (current-column))
2234 (pos (point))
2235 (from (marker-position (widget-get widget :from)))
2236 (to (marker-position (widget-get widget :to))))
2237 (save-excursion
2238 (widget-value-set widget (widget-value widget))
2239 (custom-redraw-magic widget))
2240 (when (and (>= pos from) (<= pos to))
2241 (condition-case nil
2242 (progn
2243 (goto-char (point-min))
2244 (forward-line (if (> column 0)
2245 (1- line)
2246 line))
2247 (move-to-column column))
2248 (error nil)))))
2250 (defun custom-redraw-magic (widget)
2251 "Redraw WIDGET state with current settings."
2252 (while widget
2253 (let ((magic (widget-get widget :custom-magic)))
2254 (cond (magic
2255 (widget-value-set magic (widget-value magic))
2256 (when (setq widget (widget-get widget :group))
2257 (custom-group-state-update widget)))
2259 (setq widget nil)))))
2260 (widget-setup))
2262 (make-obsolete 'custom-show "this widget type is no longer supported." "24.1")
2263 (defun custom-show (widget value)
2264 "Non-nil if WIDGET should be shown with VALUE by default."
2265 (let ((show (widget-get widget :custom-show)))
2266 (if (functionp show)
2267 (funcall show widget value)
2268 show)))
2270 (defun custom-load-widget (widget)
2271 "Load all dependencies for WIDGET."
2272 (custom-load-symbol (widget-value widget)))
2274 (defun custom-unloaded-symbol-p (symbol)
2275 "Return non-nil if the dependencies of SYMBOL have not yet been loaded."
2276 (let ((found nil)
2277 (loads (get symbol 'custom-loads))
2278 load)
2279 (while loads
2280 (setq load (car loads)
2281 loads (cdr loads))
2282 (cond ((symbolp load)
2283 (unless (featurep load)
2284 (setq found t)))
2285 ((assoc load load-history))
2286 ((assoc (locate-library load) load-history)
2287 (message nil))
2289 (setq found t))))
2290 found))
2292 (defun custom-unloaded-widget-p (widget)
2293 "Return non-nil if the dependencies of WIDGET have not yet been loaded."
2294 (custom-unloaded-symbol-p (widget-value widget)))
2296 (defun custom-toggle-hide (widget)
2297 "Toggle visibility of WIDGET."
2298 (custom-load-widget widget)
2299 (let ((state (widget-get widget :custom-state)))
2300 (cond ((memq state '(invalid modified set))
2301 (error "There are unsaved changes"))
2302 ((eq state 'hidden)
2303 (widget-put widget :custom-state 'unknown))
2305 (widget-put widget :documentation-shown nil)
2306 (widget-put widget :custom-state 'hidden)))
2307 (custom-redraw widget)
2308 (widget-setup)))
2310 (defun custom-toggle-parent (widget &rest _ignore)
2311 "Toggle visibility of parent of WIDGET."
2312 (custom-toggle-hide (widget-get widget :parent)))
2314 (defun custom-add-see-also (widget &optional prefix)
2315 "Add `See also ...' to WIDGET if there are any links.
2316 Insert PREFIX first if non-nil."
2317 (let* ((symbol (widget-get widget :value))
2318 (links (get symbol 'custom-links))
2319 (many (> (length links) 2))
2320 (buttons (widget-get widget :buttons))
2321 (indent (widget-get widget :indent)))
2322 (when links
2323 (when indent
2324 (insert-char ?\ indent))
2325 (when prefix
2326 (insert prefix))
2327 (insert "See also ")
2328 (while links
2329 (push (widget-create-child-and-convert
2330 widget (car links)
2331 :button-face 'custom-link
2332 :mouse-face 'highlight
2333 :pressed-face 'highlight)
2334 buttons)
2335 (setq links (cdr links))
2336 (cond ((null links)
2337 (insert ".\n"))
2338 ((null (cdr links))
2339 (if many
2340 (insert ", and ")
2341 (insert " and ")))
2343 (insert ", "))))
2344 (widget-put widget :buttons buttons))))
2346 (defun custom-add-parent-links (widget &optional initial-string _doc-initial-string)
2347 "Add \"Parent groups: ...\" to WIDGET if the group has parents.
2348 The value is non-nil if any parents were found.
2349 If INITIAL-STRING is non-nil, use that rather than \"Parent groups:\"."
2350 (let ((name (widget-value widget))
2351 (type (widget-type widget))
2352 (buttons (widget-get widget :buttons))
2353 (start (point))
2354 (parents nil))
2355 (insert (or initial-string "Groups:"))
2356 (mapatoms (lambda (symbol)
2357 (when (member (list name type) (get symbol 'custom-group))
2358 (insert " ")
2359 (push (widget-create-child-and-convert
2360 widget 'custom-group-link
2361 :tag (custom-unlispify-tag-name symbol)
2362 symbol)
2363 buttons)
2364 (setq parents (cons symbol parents)))))
2365 (if parents
2366 (insert "\n")
2367 (delete-region start (point)))
2368 (widget-put widget :buttons buttons)
2369 parents))
2371 ;;; The `custom-comment' Widget.
2373 ;; like the editable field
2374 (defface custom-comment '((((type tty))
2375 :background "yellow3"
2376 :foreground "black")
2377 (((class grayscale color)
2378 (background light))
2379 :background "gray85")
2380 (((class grayscale color)
2381 (background dark))
2382 :background "dim gray")
2384 :slant italic))
2385 "Face used for comments on variables or faces."
2386 :version "21.1"
2387 :group 'custom-faces)
2388 (define-obsolete-face-alias 'custom-comment-face 'custom-comment "22.1")
2390 ;; like font-lock-comment-face
2391 (defface custom-comment-tag
2392 '((((class color) (background dark)) (:foreground "gray80"))
2393 (((class color) (background light)) (:foreground "blue4"))
2394 (((class grayscale) (background light))
2395 (:foreground "DimGray" :weight bold :slant italic))
2396 (((class grayscale) (background dark))
2397 (:foreground "LightGray" :weight bold :slant italic))
2398 (t (:weight bold)))
2399 "Face used for the comment tag on variables or faces."
2400 :group 'custom-faces)
2401 (define-obsolete-face-alias 'custom-comment-tag-face 'custom-comment-tag "22.1")
2403 (define-widget 'custom-comment 'string
2404 "User comment."
2405 :tag "Comment"
2406 :help-echo "Edit a comment here."
2407 :sample-face 'custom-comment-tag
2408 :value-face 'custom-comment
2409 :shown nil
2410 :create 'custom-comment-create)
2412 (defun custom-comment-create (widget)
2413 (let* ((null-comment (equal "" (widget-value widget))))
2414 (if (or (widget-get (widget-get widget :parent) :comment-shown)
2415 (not null-comment))
2416 (widget-default-create widget)
2417 ;; `widget-default-delete' expects markers in these slots --
2418 ;; maybe it shouldn't.
2419 (widget-put widget :from (point-marker))
2420 (widget-put widget :to (point-marker)))))
2422 (defun custom-comment-hide (widget)
2423 (widget-put (widget-get widget :parent) :comment-shown nil))
2425 ;; Those functions are for the menu. WIDGET is NOT the comment widget. It's
2426 ;; the global custom one
2427 (defun custom-comment-show (widget)
2428 (widget-put widget :comment-shown t)
2429 (custom-redraw widget)
2430 (widget-setup))
2432 (defun custom-comment-invisible-p (widget)
2433 (let ((val (widget-value (widget-get widget :comment-widget))))
2434 (and (equal "" val)
2435 (not (widget-get widget :comment-shown)))))
2437 ;;; The `custom-variable' Widget.
2439 (defface custom-variable-tag
2440 `((((class color)
2441 (background dark))
2442 (:foreground "light blue" :weight bold))
2443 (((min-colors 88) (class color)
2444 (background light))
2445 (:foreground "blue1" :weight bold))
2446 (((class color)
2447 (background light))
2448 (:foreground "blue" :weight bold))
2449 (t (:weight bold)))
2450 "Face used for unpushable variable tags."
2451 :group 'custom-faces)
2452 (define-obsolete-face-alias 'custom-variable-tag-face
2453 'custom-variable-tag "22.1")
2455 (defface custom-variable-button '((t (:underline t :weight bold)))
2456 "Face used for pushable variable tags."
2457 :group 'custom-faces)
2458 (define-obsolete-face-alias 'custom-variable-button-face
2459 'custom-variable-button "22.1")
2461 (defcustom custom-variable-default-form 'edit
2462 "Default form of displaying variable values."
2463 :type '(choice (const edit)
2464 (const lisp))
2465 :group 'custom-buffer
2466 :version "20.3")
2468 (defun custom-variable-documentation (variable)
2469 "Return documentation of VARIABLE for use in Custom buffer.
2470 Normally just return the docstring. But if VARIABLE automatically
2471 becomes buffer local when set, append a message to that effect."
2472 (if (and (local-variable-if-set-p variable)
2473 (or (not (local-variable-p variable))
2474 (with-temp-buffer
2475 (local-variable-if-set-p variable))))
2476 (concat (documentation-property variable 'variable-documentation)
2478 This variable automatically becomes buffer-local when set outside Custom.
2479 However, setting it through Custom sets the default value.")
2480 (documentation-property variable 'variable-documentation)))
2482 (define-widget 'custom-variable 'custom
2483 "A widget for displaying a Custom variable.
2484 The following properties have special meanings for this widget:
2486 :hidden-states should be a list of widget states for which the
2487 widget's initial contents are to be hidden.
2489 :custom-form should be a symbol describing how to display and
2490 edit the variable---either `edit' (using edit widgets),
2491 `lisp' (as a Lisp sexp), or `mismatch' (should not happen);
2492 if nil, use the return value of `custom-variable-default-form'.
2494 :shown-value, if non-nil, should be a list whose `car' is the
2495 variable value to display in place of the current value.
2497 :custom-style describes the widget interface style; nil is the
2498 default style, while `simple' means a simpler interface that
2499 inhibits the magic custom-state widget."
2500 :format "%v"
2501 :help-echo "Set or reset this variable."
2502 :documentation-property #'custom-variable-documentation
2503 :custom-category 'option
2504 :custom-state nil
2505 :custom-menu 'custom-variable-menu-create
2506 :custom-form nil
2507 :value-create 'custom-variable-value-create
2508 :action 'custom-variable-action
2509 :hidden-states '(standard)
2510 :custom-set 'custom-variable-set
2511 :custom-mark-to-save 'custom-variable-mark-to-save
2512 :custom-reset-current 'custom-redraw
2513 :custom-reset-saved 'custom-variable-reset-saved
2514 :custom-reset-standard 'custom-variable-reset-standard
2515 :custom-mark-to-reset-standard 'custom-variable-mark-to-reset-standard
2516 :custom-standard-value 'custom-variable-standard-value
2517 :custom-state-set-and-redraw 'custom-variable-state-set-and-redraw)
2519 (defun custom-variable-type (symbol)
2520 "Return a widget suitable for editing the value of SYMBOL.
2521 If SYMBOL has a `custom-type' property, use that.
2522 Otherwise, try matching SYMBOL against `custom-guess-name-alist' and
2523 try matching its doc string against `custom-guess-doc-alist'."
2524 (let* ((type (or (get symbol 'custom-type)
2525 (and (not (get symbol 'standard-value))
2526 (custom-guess-type symbol))
2527 'sexp))
2528 (options (get symbol 'custom-options))
2529 (tmp (if (listp type)
2530 (copy-sequence type)
2531 (list type))))
2532 (when options
2533 (widget-put tmp :options options))
2534 tmp))
2536 (defun custom-variable-value-create (widget)
2537 "Here is where you edit the variable's value."
2538 (custom-load-widget widget)
2539 (unless (widget-get widget :custom-form)
2540 (widget-put widget :custom-form custom-variable-default-form))
2541 (let* ((buttons (widget-get widget :buttons))
2542 (children (widget-get widget :children))
2543 (form (widget-get widget :custom-form))
2544 (symbol (widget-get widget :value))
2545 (tag (widget-get widget :tag))
2546 (type (custom-variable-type symbol))
2547 (conv (widget-convert type))
2548 (get (or (get symbol 'custom-get) 'default-value))
2549 (prefix (widget-get widget :custom-prefix))
2550 (last (widget-get widget :custom-last))
2551 (style (widget-get widget :custom-style))
2552 (value (let ((shown-value (widget-get widget :shown-value)))
2553 (cond (shown-value
2554 (car shown-value))
2555 ((default-boundp symbol)
2556 (funcall get symbol))
2557 (t (widget-get conv :value)))))
2558 (state (or (widget-get widget :custom-state)
2559 (if (memq (custom-variable-state symbol value)
2560 (widget-get widget :hidden-states))
2561 'hidden))))
2563 ;; If we don't know the state, see if we need to edit it in lisp form.
2564 (unless state
2565 (setq state (if (custom-show type value) 'unknown 'hidden)))
2566 (when (eq state 'unknown)
2567 (unless (widget-apply conv :match value)
2568 (setq form 'mismatch)))
2569 ;; Now we can create the child widget.
2570 (cond ((eq custom-buffer-style 'tree)
2571 (insert prefix (if last " `--- " " |--- "))
2572 (push (widget-create-child-and-convert
2573 widget 'custom-browse-variable-tag)
2574 buttons)
2575 (insert " " tag "\n")
2576 (widget-put widget :buttons buttons))
2577 ((eq state 'hidden)
2578 ;; Indicate hidden value.
2579 (push (widget-create-child-and-convert
2580 widget 'custom-visibility
2581 :help-echo "Show the value of this option."
2582 :on-glyph "down"
2583 :on "Hide"
2584 :off-glyph "right"
2585 :off "Show Value"
2586 :action 'custom-toggle-hide-variable
2587 nil)
2588 buttons)
2589 (insert " ")
2590 (push (widget-create-child-and-convert
2591 widget 'item
2592 :format "%{%t%} "
2593 :sample-face 'custom-variable-tag
2594 :tag tag
2595 :parent widget)
2596 buttons))
2597 ((memq form '(lisp mismatch))
2598 ;; In lisp mode edit the saved value when possible.
2599 (push (widget-create-child-and-convert
2600 widget 'custom-visibility
2601 :help-echo "Hide the value of this option."
2602 :on "Hide"
2603 :off "Show"
2604 :on-glyph "down"
2605 :off-glyph "right"
2606 :action 'custom-toggle-hide-variable
2608 buttons)
2609 (insert " ")
2610 (let* ((value (cond ((get symbol 'saved-value)
2611 (car (get symbol 'saved-value)))
2612 ((get symbol 'standard-value)
2613 (car (get symbol 'standard-value)))
2614 ((default-boundp symbol)
2615 (custom-quote (funcall get symbol)))
2617 (custom-quote (widget-get conv :value))))))
2618 (insert (symbol-name symbol) ": ")
2619 (push (widget-create-child-and-convert
2620 widget 'sexp
2621 :button-face 'custom-variable-button-face
2622 :format "%v"
2623 :tag (symbol-name symbol)
2624 :parent widget
2625 :value value)
2626 children)))
2628 ;; Edit mode.
2629 (push (widget-create-child-and-convert
2630 widget 'custom-visibility
2631 :help-echo "Hide or show this option."
2632 :on "Hide"
2633 :off "Show"
2634 :on-glyph "down"
2635 :off-glyph "right"
2636 :action 'custom-toggle-hide-variable
2638 buttons)
2639 (insert " ")
2640 (let* ((format (widget-get type :format))
2641 tag-format value-format)
2642 (unless (string-match ":" format)
2643 (error "Bad format"))
2644 (setq tag-format (substring format 0 (match-end 0)))
2645 (setq value-format (substring format (match-end 0)))
2646 (push (widget-create-child-and-convert
2647 widget 'item
2648 :format tag-format
2649 :action 'custom-tag-action
2650 :help-echo "Change value of this option."
2651 :mouse-down-action 'custom-tag-mouse-down-action
2652 :button-face 'custom-variable-button
2653 :sample-face 'custom-variable-tag
2654 tag)
2655 buttons)
2656 (push (widget-create-child-and-convert
2657 widget type
2658 :format value-format
2659 :value value)
2660 children))))
2661 (unless (eq custom-buffer-style 'tree)
2662 (unless (eq (preceding-char) ?\n)
2663 (widget-insert "\n"))
2664 ;; Create the magic button.
2665 (unless (eq style 'simple)
2666 (let ((magic (widget-create-child-and-convert
2667 widget 'custom-magic nil)))
2668 (widget-put widget :custom-magic magic)
2669 (push magic buttons)))
2670 (widget-put widget :buttons buttons)
2671 ;; Insert documentation.
2672 (widget-put widget :documentation-indent 3)
2673 (unless (and (eq style 'simple)
2674 (eq state 'hidden))
2675 (widget-add-documentation-string-button
2676 widget :visibility-widget 'custom-visibility))
2678 ;; The comment field
2679 (unless (eq state 'hidden)
2680 (let* ((comment (get symbol 'variable-comment))
2681 (comment-widget
2682 (widget-create-child-and-convert
2683 widget 'custom-comment
2684 :parent widget
2685 :value (or comment ""))))
2686 (widget-put widget :comment-widget comment-widget)
2687 ;; Don't push it !!! Custom assumes that the first child is the
2688 ;; value one.
2689 (setq children (append children (list comment-widget)))))
2690 ;; Update the rest of the properties.
2691 (widget-put widget :custom-form form)
2692 (widget-put widget :children children)
2693 ;; Now update the state.
2694 (if (eq state 'hidden)
2695 (widget-put widget :custom-state state)
2696 (custom-variable-state-set widget))
2697 ;; See also.
2698 (unless (eq state 'hidden)
2699 (when (eq (widget-get widget :custom-level) 1)
2700 (custom-add-parent-links widget))
2701 (custom-add-see-also widget)))))
2703 (defun custom-toggle-hide-variable (visibility-widget &rest _ignore)
2704 "Toggle the visibility of a `custom-variable' parent widget.
2705 By default, this signals an error if the parent has unsaved
2706 changes. If the parent has a `simple' :custom-style property,
2707 the present value is saved to its :shown-value property instead."
2708 (let ((widget (widget-get visibility-widget :parent)))
2709 (unless (eq (widget-type widget) 'custom-variable)
2710 (error "Invalid widget type"))
2711 (custom-load-widget widget)
2712 (let ((state (widget-get widget :custom-state)))
2713 (if (eq state 'hidden)
2714 (widget-put widget :custom-state 'unknown)
2715 ;; In normal interface, widget can't be hidden if modified.
2716 (when (memq state '(invalid modified set))
2717 (if (eq (widget-get widget :custom-style) 'simple)
2718 (widget-put widget :shown-value
2719 (list (widget-value
2720 (car-safe
2721 (widget-get widget :children)))))
2722 (error "There are unsaved changes")))
2723 (widget-put widget :documentation-shown nil)
2724 (widget-put widget :custom-state 'hidden))
2725 (custom-redraw widget)
2726 (widget-setup))))
2728 (defun custom-tag-action (widget &rest args)
2729 "Pass :action to first child of WIDGET's parent."
2730 (apply 'widget-apply (car (widget-get (widget-get widget :parent) :children))
2731 :action args))
2733 (defun custom-tag-mouse-down-action (widget &rest args)
2734 "Pass :mouse-down-action to first child of WIDGET's parent."
2735 (apply 'widget-apply (car (widget-get (widget-get widget :parent) :children))
2736 :mouse-down-action args))
2738 (defun custom-variable-state (symbol val)
2739 "Return the state of SYMBOL if its value is VAL.
2740 If SYMBOL has a non-nil `custom-get' property, it overrides VAL.
2741 Possible return values are `standard', `saved', `set', `themed',
2742 `changed', and `rogue'."
2743 (let* ((get (or (get symbol 'custom-get) 'default-value))
2744 (value (if (default-boundp symbol)
2745 (funcall get symbol)
2746 val))
2747 (comment (get symbol 'variable-comment))
2749 temp)
2750 (cond ((progn (setq tmp (get symbol 'customized-value))
2751 (setq temp
2752 (get symbol 'customized-variable-comment))
2753 (or tmp temp))
2754 (if (condition-case nil
2755 (and (equal value (eval (car tmp)))
2756 (equal comment temp))
2757 (error nil))
2758 'set
2759 'changed))
2760 ((progn (setq tmp (get symbol 'theme-value))
2761 (setq temp (get symbol 'saved-variable-comment))
2762 (or tmp temp))
2763 (if (condition-case nil
2764 (and (equal comment temp)
2765 (equal value
2766 (eval
2767 (car (custom-variable-theme-value
2768 symbol)))))
2769 (error nil))
2770 (cond
2771 ((eq (caar tmp) 'user) 'saved)
2772 ((eq (caar tmp) 'changed)
2773 (if (condition-case nil
2774 (and (null comment)
2775 (equal value
2776 (eval
2777 (car (get symbol 'standard-value)))))
2778 (error nil))
2779 ;; The value was originally set outside
2780 ;; custom, but it was set to the standard
2781 ;; value (probably an autoloaded defcustom).
2782 'standard
2783 'changed))
2784 (t 'themed))
2785 'changed))
2786 ((setq tmp (get symbol 'standard-value))
2787 (if (condition-case nil
2788 (and (equal value (eval (car tmp)))
2789 (equal comment nil))
2790 (error nil))
2791 'standard
2792 'changed))
2793 (t 'rogue))))
2795 (defun custom-variable-state-set (widget &optional state)
2796 "Set the state of WIDGET to STATE.
2797 If STATE is nil, the value is computed by `custom-variable-state'."
2798 (widget-put widget :custom-state
2799 (or state (custom-variable-state (widget-value widget)
2800 (widget-get widget :value)))))
2802 (defun custom-variable-standard-value (widget)
2803 (get (widget-value widget) 'standard-value))
2805 (defvar custom-variable-menu
2806 `(("Set for Current Session" custom-variable-set
2807 (lambda (widget)
2808 (eq (widget-get widget :custom-state) 'modified)))
2809 ;; Note that in all the backquoted code in this file, we test
2810 ;; init-file-user rather than user-init-file. This is in case
2811 ;; cus-edit is loaded by something in site-start.el, because
2812 ;; user-init-file is not set at that stage.
2813 ;; http://lists.gnu.org/archive/html/emacs-devel/2007-10/msg00310.html
2814 ,@(when (or custom-file init-file-user)
2815 '(("Save for Future Sessions" custom-variable-save
2816 (lambda (widget)
2817 (memq (widget-get widget :custom-state)
2818 '(modified set changed rogue))))))
2819 ("Undo Edits" custom-redraw
2820 (lambda (widget)
2821 (and (default-boundp (widget-value widget))
2822 (memq (widget-get widget :custom-state) '(modified changed)))))
2823 ("Reset to Saved" custom-variable-reset-saved
2824 (lambda (widget)
2825 (and (or (get (widget-value widget) 'saved-value)
2826 (get (widget-value widget) 'saved-variable-comment))
2827 (memq (widget-get widget :custom-state)
2828 '(modified set changed rogue)))))
2829 ,@(when (or custom-file init-file-user)
2830 '(("Erase Customization" custom-variable-reset-standard
2831 (lambda (widget)
2832 (and (get (widget-value widget) 'standard-value)
2833 (memq (widget-get widget :custom-state)
2834 '(modified set changed saved rogue)))))))
2835 ("Set to Backup Value" custom-variable-reset-backup
2836 (lambda (widget)
2837 (get (widget-value widget) 'backup-value)))
2838 ("---" ignore ignore)
2839 ("Add Comment" custom-comment-show custom-comment-invisible-p)
2840 ("---" ignore ignore)
2841 ("Show Current Value" custom-variable-edit
2842 (lambda (widget)
2843 (eq (widget-get widget :custom-form) 'lisp)))
2844 ("Show Saved Lisp Expression" custom-variable-edit-lisp
2845 (lambda (widget)
2846 (eq (widget-get widget :custom-form) 'edit))))
2847 "Alist of actions for the `custom-variable' widget.
2848 Each entry has the form (NAME ACTION FILTER) where NAME is the name of
2849 the menu entry, ACTION is the function to call on the widget when the
2850 menu is selected, and FILTER is a predicate which takes a `custom-variable'
2851 widget as an argument, and returns non-nil if ACTION is valid on that
2852 widget. If FILTER is nil, ACTION is always valid.")
2854 (defun custom-variable-action (widget &optional event)
2855 "Show the menu for `custom-variable' WIDGET.
2856 Optional EVENT is the location for the menu."
2857 (if (eq (widget-get widget :custom-state) 'hidden)
2858 (custom-toggle-hide widget)
2859 (unless (eq (widget-get widget :custom-state) 'modified)
2860 (custom-variable-state-set widget))
2861 (custom-redraw-magic widget)
2862 (let* ((completion-ignore-case t)
2863 (answer (widget-choose (concat "Operation on "
2864 (custom-unlispify-tag-name
2865 (widget-get widget :value)))
2866 (custom-menu-filter custom-variable-menu
2867 widget)
2868 event)))
2869 (if answer
2870 (funcall answer widget)))))
2872 (defun custom-variable-edit (widget)
2873 "Edit value of WIDGET."
2874 (widget-put widget :custom-state 'unknown)
2875 (widget-put widget :custom-form 'edit)
2876 (custom-redraw widget))
2878 (defun custom-variable-edit-lisp (widget)
2879 "Edit the Lisp representation of the value of WIDGET."
2880 (widget-put widget :custom-state 'unknown)
2881 (widget-put widget :custom-form 'lisp)
2882 (custom-redraw widget))
2884 (defun custom-variable-set (widget)
2885 "Set the current value for the variable being edited by WIDGET."
2886 (let* ((form (widget-get widget :custom-form))
2887 (state (widget-get widget :custom-state))
2888 (child (car (widget-get widget :children)))
2889 (symbol (widget-value widget))
2890 (set (or (get symbol 'custom-set) 'set-default))
2891 (comment-widget (widget-get widget :comment-widget))
2892 (comment (widget-value comment-widget))
2893 val)
2894 (cond ((eq state 'hidden)
2895 (error "Cannot set hidden variable"))
2896 ((setq val (widget-apply child :validate))
2897 (goto-char (widget-get val :from))
2898 (error "%s" (widget-get val :error)))
2899 ((memq form '(lisp mismatch))
2900 (when (equal comment "")
2901 (setq comment nil)
2902 ;; Make the comment invisible by hand if it's empty
2903 (custom-comment-hide comment-widget))
2904 (custom-variable-backup-value widget)
2905 (custom-push-theme 'theme-value symbol 'user
2906 'set (custom-quote (widget-value child)))
2907 (funcall set symbol (eval (setq val (widget-value child))))
2908 (put symbol 'customized-value (list val))
2909 (put symbol 'variable-comment comment)
2910 (put symbol 'customized-variable-comment comment))
2912 (when (equal comment "")
2913 (setq comment nil)
2914 ;; Make the comment invisible by hand if it's empty
2915 (custom-comment-hide comment-widget))
2916 (custom-variable-backup-value widget)
2917 (custom-push-theme 'theme-value symbol 'user
2918 'set (custom-quote (widget-value child)))
2919 (funcall set symbol (setq val (widget-value child)))
2920 (put symbol 'customized-value (list (custom-quote val)))
2921 (put symbol 'variable-comment comment)
2922 (put symbol 'customized-variable-comment comment)))
2923 (custom-variable-state-set widget)
2924 (custom-redraw-magic widget)))
2926 (defun custom-variable-mark-to-save (widget)
2927 "Set value and mark for saving the variable edited by WIDGET."
2928 (let* ((form (widget-get widget :custom-form))
2929 (state (widget-get widget :custom-state))
2930 (child (car (widget-get widget :children)))
2931 (symbol (widget-value widget))
2932 (set (or (get symbol 'custom-set) 'set-default))
2933 (comment-widget (widget-get widget :comment-widget))
2934 (comment (widget-value comment-widget))
2935 val)
2936 (cond ((eq state 'hidden)
2937 (error "Cannot set hidden variable"))
2938 ((setq val (widget-apply child :validate))
2939 (goto-char (widget-get val :from))
2940 (error "Saving %s: %s" symbol (widget-get val :error)))
2941 ((memq form '(lisp mismatch))
2942 (when (equal comment "")
2943 (setq comment nil)
2944 ;; Make the comment invisible by hand if it's empty
2945 (custom-comment-hide comment-widget))
2946 (put symbol 'saved-value (list (widget-value child)))
2947 (custom-push-theme 'theme-value symbol 'user
2948 'set (custom-quote (widget-value child)))
2949 (funcall set symbol (eval (widget-value child)))
2950 (put symbol 'variable-comment comment)
2951 (put symbol 'saved-variable-comment comment))
2953 (when (equal comment "")
2954 (setq comment nil)
2955 ;; Make the comment invisible by hand if it's empty
2956 (custom-comment-hide comment-widget))
2957 (put symbol 'saved-value
2958 (list (custom-quote (widget-value child))))
2959 (custom-push-theme 'theme-value symbol 'user
2960 'set (custom-quote (widget-value child)))
2961 (funcall set symbol (widget-value child))
2962 (put symbol 'variable-comment comment)
2963 (put symbol 'saved-variable-comment comment)))
2964 (put symbol 'customized-value nil)
2965 (put symbol 'customized-variable-comment nil)))
2967 (defsubst custom-variable-state-set-and-redraw (widget)
2968 "Set state of variable widget WIDGET and redraw with current settings."
2969 (custom-variable-state-set widget)
2970 (custom-redraw-magic widget))
2972 (defun custom-variable-save (widget)
2973 "Save value of variable edited by widget WIDGET."
2974 (custom-variable-mark-to-save widget)
2975 (custom-save-all)
2976 (custom-variable-state-set-and-redraw widget))
2978 (defun custom-variable-reset-saved (widget)
2979 "Restore the saved value for the variable being edited by WIDGET.
2980 This also updates the buffer to show that value.
2981 The value that was current before this operation
2982 becomes the backup value, so you can get it again."
2983 (let* ((symbol (widget-value widget))
2984 (set (or (get symbol 'custom-set) 'set-default))
2985 (value (get symbol 'saved-value))
2986 (comment (get symbol 'saved-variable-comment)))
2987 (cond ((or value comment)
2988 (put symbol 'variable-comment comment)
2989 (custom-variable-backup-value widget)
2990 (custom-push-theme 'theme-value symbol 'user 'set (car-safe value))
2991 (condition-case nil
2992 (funcall set symbol (eval (car value)))
2993 (error nil)))
2995 (error "No saved value for %s" symbol)))
2996 (put symbol 'customized-value nil)
2997 (put symbol 'customized-variable-comment nil)
2998 (widget-put widget :custom-state 'unknown)
2999 ;; This call will possibly make the comment invisible
3000 (custom-redraw widget)))
3002 (defun custom-variable-mark-to-reset-standard (widget)
3003 "Mark to restore standard setting for the variable edited by widget WIDGET.
3004 If `custom-reset-standard-variables-list' is nil, save, reset and
3005 redraw the widget immediately."
3006 (let* ((symbol (widget-value widget)))
3007 (if (get symbol 'standard-value)
3008 (custom-variable-backup-value widget)
3009 (error "No standard setting known for %S" symbol))
3010 (put symbol 'variable-comment nil)
3011 (put symbol 'customized-value nil)
3012 (put symbol 'customized-variable-comment nil)
3013 (custom-push-theme 'theme-value symbol 'user 'reset)
3014 (custom-theme-recalc-variable symbol)
3015 (if (and custom-reset-standard-variables-list
3016 (or (get symbol 'saved-value) (get symbol 'saved-variable-comment)))
3017 (progn
3018 (put symbol 'saved-value nil)
3019 (put symbol 'saved-variable-comment nil)
3020 ;; Append this to `custom-reset-standard-variables-list' to
3021 ;; have `custom-reset-standard-save-and-update' save setting
3022 ;; to the file, update the widget's state, and redraw it.
3023 (setq custom-reset-standard-variables-list
3024 (cons widget custom-reset-standard-variables-list)))
3025 (when (or (get symbol 'saved-value) (get symbol 'saved-variable-comment))
3026 (put symbol 'saved-value nil)
3027 (put symbol 'saved-variable-comment nil)
3028 (custom-save-all))
3029 (widget-put widget :custom-state 'unknown)
3030 ;; This call will possibly make the comment invisible
3031 (custom-redraw widget))))
3033 (defun custom-variable-reset-standard (widget)
3034 "Restore standard setting for the variable edited by WIDGET.
3035 This operation eliminates any saved setting for the variable,
3036 restoring it to the state of a variable that has never been customized.
3037 The value that was current before this operation
3038 becomes the backup value, so you can get it again."
3039 (let (custom-reset-standard-variables-list)
3040 (custom-variable-mark-to-reset-standard widget)))
3042 (defun custom-variable-backup-value (widget)
3043 "Back up the current value for WIDGET's variable.
3044 The backup value is kept in the car of the `backup-value' property."
3045 (let* ((symbol (widget-value widget))
3046 (get (or (get symbol 'custom-get) 'default-value))
3047 (type (custom-variable-type symbol))
3048 (conv (widget-convert type))
3049 (value (if (default-boundp symbol)
3050 (funcall get symbol)
3051 (widget-get conv :value))))
3052 (put symbol 'backup-value (list value))))
3054 (defun custom-variable-reset-backup (widget)
3055 "Restore the backup value for the variable being edited by WIDGET.
3056 The value that was current before this operation
3057 becomes the backup value, so you can use this operation repeatedly
3058 to switch between two values."
3059 (let* ((symbol (widget-value widget))
3060 (set (or (get symbol 'custom-set) 'set-default))
3061 (value (get symbol 'backup-value))
3062 (comment-widget (widget-get widget :comment-widget))
3063 (comment (widget-value comment-widget)))
3064 (if value
3065 (progn
3066 (custom-variable-backup-value widget)
3067 (custom-push-theme 'theme-value symbol 'user 'set value)
3068 (condition-case nil
3069 (funcall set symbol (car value))
3070 (error nil)))
3071 (error "No backup value for %s" symbol))
3072 (put symbol 'customized-value (list (car value)))
3073 (put symbol 'variable-comment comment)
3074 (put symbol 'customized-variable-comment comment)
3075 (custom-variable-state-set widget)
3076 ;; This call will possibly make the comment invisible
3077 (custom-redraw widget)))
3079 ;;; The `custom-visibility' Widget
3081 (define-widget 'custom-visibility 'visibility
3082 "Show or hide a documentation string."
3083 :button-face 'custom-visibility
3084 :pressed-face 'custom-visibility
3085 :mouse-face 'highlight
3086 :pressed-face 'highlight
3087 :on-glyph nil
3088 :off-glyph nil)
3090 (defface custom-visibility
3091 '((t :height 0.8 :inherit link))
3092 "Face for the `custom-visibility' widget."
3093 :version "23.1"
3094 :group 'custom-faces)
3096 ;;; The `custom-face-edit' Widget.
3098 (define-widget 'custom-face-edit 'checklist
3099 "Widget for editing face attributes.
3100 The following properties have special meanings for this widget:
3102 :value is a plist of face attributes.
3104 :default-face-attributes, if non-nil, is a plist of defaults for
3105 face attributes (as specified by a `default' defface entry)."
3106 :format "%v"
3107 :extra-offset 3
3108 :button-args '(:help-echo "Control whether this attribute has any effect.")
3109 :value-to-internal 'custom-face-edit-fix-value
3110 :match (lambda (widget value)
3111 (widget-checklist-match widget
3112 (custom-face-edit-fix-value widget value)))
3113 :value-create 'custom-face-edit-value-create
3114 :convert-widget 'custom-face-edit-convert-widget
3115 :args (mapcar (lambda (att)
3116 (list 'group :inline t
3117 :sibling-args (widget-get (nth 1 att) :sibling-args)
3118 (list 'const :format "" :value (nth 0 att))
3119 (nth 1 att)))
3120 custom-face-attributes))
3122 (defun custom-face-edit-value-create (widget)
3123 (let* ((alist (widget-checklist-match-find
3124 widget (widget-get widget :value)))
3125 (args (widget-get widget :args))
3126 (show-all (widget-get widget :show-all-attributes))
3127 (buttons (widget-get widget :buttons))
3128 (defaults (widget-checklist-match-find
3129 widget
3130 (widget-get widget :default-face-attributes)))
3131 entry)
3132 (unless (looking-back "^ *")
3133 (insert ?\n))
3134 (insert-char ?\s (widget-get widget :extra-offset))
3135 (if (or alist defaults show-all)
3136 (dolist (prop args)
3137 (setq entry (or (assq prop alist)
3138 (assq prop defaults)))
3139 (if (or entry show-all)
3140 (widget-checklist-add-item widget prop entry)))
3141 (insert (propertize "-- Empty face --" 'face 'shadow) ?\n))
3142 (let ((indent (widget-get widget :indent)))
3143 (if indent (insert-char ?\s (widget-get widget :indent))))
3144 (push (widget-create-child-and-convert
3145 widget 'visibility
3146 :help-echo "Show or hide all face attributes."
3147 :button-face 'custom-visibility
3148 :pressed-face 'custom-visibility
3149 :mouse-face 'highlight
3150 :on "Hide Unused Attributes" :off "Show All Attributes"
3151 :on-glyph nil :off-glyph nil
3152 :always-active t
3153 :action 'custom-face-edit-value-visibility-action
3154 show-all)
3155 buttons)
3156 (insert ?\n)
3157 (widget-put widget :buttons buttons)
3158 (widget-put widget :children (nreverse (widget-get widget :children)))))
3160 (defun custom-face-edit-value-visibility-action (widget &rest _ignore)
3161 ;; Toggle hiding of face attributes.
3162 (let ((parent (widget-get widget :parent)))
3163 (widget-put parent :show-all-attributes
3164 (not (widget-get parent :show-all-attributes)))
3165 (custom-redraw parent)))
3167 (defun custom-face-edit-fix-value (_widget value)
3168 "Ignoring WIDGET, convert :bold and :italic in VALUE to new form.
3169 Also change :reverse-video to :inverse-video."
3170 (custom-fix-face-spec value))
3172 (defun custom-face-edit-convert-widget (widget)
3173 "Convert :args as widget types in WIDGET."
3174 (widget-put
3175 widget
3176 :args (mapcar (lambda (arg)
3177 (widget-convert arg
3178 :deactivate 'custom-face-edit-deactivate
3179 :activate 'custom-face-edit-activate
3180 :delete 'custom-face-edit-delete))
3181 (widget-get widget :args)))
3182 widget)
3184 (defconst custom-face-edit (widget-convert 'custom-face-edit)
3185 "Converted version of the `custom-face-edit' widget.")
3187 (defun custom-face-edit-deactivate (widget)
3188 "Make face widget WIDGET inactive for user modifications."
3189 (unless (widget-get widget :inactive)
3190 (let ((tag (custom-face-edit-attribute-tag widget))
3191 (from (copy-marker (widget-get widget :from)))
3192 (value (widget-value widget))
3193 (inhibit-read-only t)
3194 (inhibit-modification-hooks t))
3195 (save-excursion
3196 (goto-char from)
3197 (widget-default-delete widget)
3198 (insert tag ": " (propertize "--" 'face 'shadow) "\n")
3199 (widget-put widget :inactive
3200 (cons value (cons from (- (point) from))))))))
3202 (defun custom-face-edit-activate (widget)
3203 "Make face widget WIDGET active for user modifications."
3204 (let ((inactive (widget-get widget :inactive))
3205 (inhibit-read-only t)
3206 (inhibit-modification-hooks t))
3207 (when (consp inactive)
3208 (save-excursion
3209 (goto-char (car (cdr inactive)))
3210 (delete-region (point) (+ (point) (cdr (cdr inactive))))
3211 (widget-put widget :inactive nil)
3212 (widget-apply widget :create)
3213 (widget-value-set widget (car inactive))
3214 (widget-setup)))))
3216 (defun custom-face-edit-delete (widget)
3217 "Remove WIDGET from the buffer."
3218 (let ((inactive (widget-get widget :inactive))
3219 (inhibit-read-only t)
3220 (inhibit-modification-hooks t))
3221 (if (not inactive)
3222 ;; Widget is alive, we don't have to do anything special
3223 (widget-default-delete widget)
3224 ;; WIDGET is already deleted because we did so to inactivate it;
3225 ;; now just get rid of the label we put in its place.
3226 (delete-region (car (cdr inactive))
3227 (+ (car (cdr inactive)) (cdr (cdr inactive))))
3228 (widget-put widget :inactive nil))))
3231 (defun custom-face-edit-attribute-tag (widget)
3232 "Return the first :tag property in WIDGET or one of its children."
3233 (let ((tag (widget-get widget :tag)))
3234 (or (and (not (equal tag "")) tag)
3235 (let ((children (widget-get widget :children)))
3236 (while (and (null tag) children)
3237 (setq tag (custom-face-edit-attribute-tag (pop children))))
3238 tag))))
3240 ;;; The `custom-display' Widget.
3242 (define-widget 'custom-display 'menu-choice
3243 "Select a display type."
3244 :tag "Display"
3245 :value t
3246 :help-echo "Specify frames where the face attributes should be used."
3247 :args '((const :tag "all" t)
3248 (const :tag "defaults" default)
3249 (checklist
3250 :offset 0
3251 :extra-offset 9
3252 :args ((group :sibling-args (:help-echo "\
3253 Only match the specified window systems.")
3254 (const :format "Type: "
3255 type)
3256 (checklist :inline t
3257 :offset 0
3258 (const :format "X "
3259 :sibling-args (:help-echo "\
3260 The X11 Window System.")
3262 (const :format "PM "
3263 :sibling-args (:help-echo "\
3264 OS/2 Presentation Manager.")
3266 (const :format "W32 "
3267 :sibling-args (:help-echo "\
3268 Windows NT/9X.")
3269 w32)
3270 (const :format "NS "
3271 :sibling-args (:help-echo "\
3272 GNUstep or Macintosh OS Cocoa interface.")
3274 (const :format "DOS "
3275 :sibling-args (:help-echo "\
3276 Plain MS-DOS.")
3278 (const :format "TTY%n"
3279 :sibling-args (:help-echo "\
3280 Plain text terminals.")
3281 tty)))
3282 (group :sibling-args (:help-echo "\
3283 Only match the frames with the specified color support.")
3284 (const :format "Class: "
3285 class)
3286 (checklist :inline t
3287 :offset 0
3288 (const :format "Color "
3289 :sibling-args (:help-echo "\
3290 Match color frames.")
3291 color)
3292 (const :format "Grayscale "
3293 :sibling-args (:help-echo "\
3294 Match grayscale frames.")
3295 grayscale)
3296 (const :format "Monochrome%n"
3297 :sibling-args (:help-echo "\
3298 Match frames with no color support.")
3299 mono)))
3300 (group :sibling-args (:help-echo "\
3301 The minimum number of colors the frame should support.")
3302 (const :format "" min-colors)
3303 (integer :tag "Minimum number of colors" ))
3304 (group :sibling-args (:help-echo "\
3305 Only match frames with the specified intensity.")
3306 (const :format "\
3307 Background brightness: "
3308 background)
3309 (checklist :inline t
3310 :offset 0
3311 (const :format "Light "
3312 :sibling-args (:help-echo "\
3313 Match frames with light backgrounds.")
3314 light)
3315 (const :format "Dark\n"
3316 :sibling-args (:help-echo "\
3317 Match frames with dark backgrounds.")
3318 dark)))
3319 (group :sibling-args (:help-echo "\
3320 Only match frames that support the specified face attributes.")
3321 (const :format "Supports attributes:" supports)
3322 (custom-face-edit :inline t :format "%n%v"))))))
3324 ;;; The `custom-face' Widget.
3326 (defface custom-face-tag
3327 `((t :inherit custom-variable-tag))
3328 "Face used for face tags."
3329 :group 'custom-faces)
3330 (define-obsolete-face-alias 'custom-face-tag-face 'custom-face-tag "22.1")
3332 (defcustom custom-face-default-form 'selected
3333 "Default form of displaying face definition."
3334 :type '(choice (const all)
3335 (const selected)
3336 (const lisp))
3337 :group 'custom-buffer
3338 :version "20.3")
3340 (define-widget 'custom-face 'custom
3341 "Widget for customizing a face.
3342 The following properties have special meanings for this widget:
3344 :value is the face name (a symbol).
3346 :custom-form should be a symbol describing how to display and
3347 edit the face attributes---either `selected' (attributes for
3348 selected display only), `all' (all attributes), `lisp' (as a
3349 Lisp sexp), or `mismatch' (should not happen); if nil, use
3350 the return value of `custom-face-default-form'.
3352 :custom-style describes the widget interface style; nil is the
3353 default style, while `simple' means a simpler interface that
3354 inhibits the magic custom-state widget.
3356 :sample-indent, if non-nil, is the number of columns to which to
3357 indent the face sample (an integer).
3359 :shown-value, if non-nil, is the face spec to display as the value
3360 of the widget, instead of the current face spec."
3361 :sample-face 'custom-face-tag
3362 :help-echo "Set or reset this face."
3363 :documentation-property #'face-doc-string
3364 :value-create 'custom-face-value-create
3365 :action 'custom-face-action
3366 :custom-category 'face
3367 :custom-form nil
3368 :custom-set 'custom-face-set
3369 :custom-mark-to-save 'custom-face-mark-to-save
3370 :custom-reset-current 'custom-redraw
3371 :custom-reset-saved 'custom-face-reset-saved
3372 :custom-reset-standard 'custom-face-reset-standard
3373 :custom-mark-to-reset-standard 'custom-face-mark-to-reset-standard
3374 :custom-standard-value 'custom-face-standard-value
3375 :custom-state-set-and-redraw 'custom-face-state-set-and-redraw
3376 :custom-menu 'custom-face-menu-create)
3378 (define-widget 'custom-face-all 'editable-list
3379 "An editable list of display specifications and attributes."
3380 :entry-format "%i %d %v"
3381 :insert-button-args '(:help-echo "Insert new display specification here.")
3382 :append-button-args '(:help-echo "Append new display specification here.")
3383 :delete-button-args '(:help-echo "Delete this display specification.")
3384 :args '((group :format "%v" custom-display custom-face-edit)))
3386 (defconst custom-face-all (widget-convert 'custom-face-all)
3387 "Converted version of the `custom-face-all' widget.")
3389 (defun custom-filter-face-spec (spec filter-index &optional default-filter)
3390 "Return a canonicalized version of SPEC using.
3391 FILTER-INDEX is the index in the entry for each attribute in
3392 `custom-face-attributes' at which the appropriate filter function can be
3393 found, and DEFAULT-FILTER is the filter to apply for attributes that
3394 don't specify one."
3395 (mapcar (lambda (entry)
3396 ;; Filter a single face-spec entry
3397 (let ((tests (car entry))
3398 (unfiltered-attrs
3399 ;; Handle both old- and new-style attribute syntax
3400 (if (listp (car (cdr entry)))
3401 (car (cdr entry))
3402 (cdr entry)))
3403 (filtered-attrs nil))
3404 ;; Filter each face attribute
3405 (while unfiltered-attrs
3406 (let* ((attr (pop unfiltered-attrs))
3407 (pre-filtered-value (pop unfiltered-attrs))
3408 (filter
3409 (or (nth filter-index (assq attr custom-face-attributes))
3410 default-filter))
3411 (filtered-value
3412 (if filter
3413 (funcall filter pre-filtered-value)
3414 pre-filtered-value)))
3415 (push filtered-value filtered-attrs)
3416 (push attr filtered-attrs)))
3418 (list tests filtered-attrs)))
3419 spec))
3421 (defun custom-pre-filter-face-spec (spec)
3422 "Return SPEC changed as necessary for editing by the face customization widget.
3423 SPEC must be a full face spec."
3424 (custom-filter-face-spec spec 2))
3426 (defun custom-post-filter-face-spec (spec)
3427 "Return the customized SPEC in a form suitable for setting the face."
3428 (custom-filter-face-spec spec 3))
3430 (defun custom-face-widget-to-spec (widget)
3431 "Return a face spec corresponding to WIDGET.
3432 WIDGET should be a `custom-face' widget."
3433 (unless (eq (widget-type widget) 'custom-face)
3434 (error "Invalid widget"))
3435 (let ((child (car (widget-get widget :children))))
3436 (custom-post-filter-face-spec
3437 (if (eq (widget-type child) 'custom-face-edit)
3438 `((t ,(widget-value child)))
3439 (widget-value child)))))
3441 (defun custom-face-get-current-spec (face)
3442 (let ((spec (or (get face 'customized-face)
3443 (get face 'saved-face)
3444 (get face 'face-defface-spec)
3445 ;; Attempt to construct it.
3446 `((t ,(custom-face-attributes-get
3447 face (selected-frame)))))))
3448 ;; If the user has changed this face in some other way,
3449 ;; edit it as the user has specified it.
3450 (if (not (face-spec-match-p face spec (selected-frame)))
3451 (setq spec `((t ,(face-attr-construct face (selected-frame))))))
3452 (custom-pre-filter-face-spec spec)))
3454 (defun custom-toggle-hide-face (visibility-widget &rest _ignore)
3455 "Toggle the visibility of a `custom-face' parent widget.
3456 By default, this signals an error if the parent has unsaved
3457 changes. If the parent has a `simple' :custom-style property,
3458 the present value is saved to its :shown-value property instead."
3459 (let ((widget (widget-get visibility-widget :parent)))
3460 (unless (eq (widget-type widget) 'custom-face)
3461 (error "Invalid widget type"))
3462 (custom-load-widget widget)
3463 (let ((state (widget-get widget :custom-state)))
3464 (if (eq state 'hidden)
3465 (widget-put widget :custom-state 'unknown)
3466 ;; In normal interface, widget can't be hidden if modified.
3467 (when (memq state '(invalid modified set))
3468 (if (eq (widget-get widget :custom-style) 'simple)
3469 (widget-put widget :shown-value
3470 (custom-face-widget-to-spec widget))
3471 (error "There are unsaved changes")))
3472 (widget-put widget :documentation-shown nil)
3473 (widget-put widget :custom-state 'hidden))
3474 (custom-redraw widget)
3475 (widget-setup))))
3477 (defun custom-face-value-create (widget)
3478 "Create a list of the display specifications for WIDGET."
3479 (let* ((buttons (widget-get widget :buttons))
3480 (symbol (widget-get widget :value))
3481 (tag (or (widget-get widget :tag)
3482 (prin1-to-string symbol)))
3483 (hiddenp (eq (widget-get widget :custom-state) 'hidden))
3484 (style (widget-get widget :custom-style))
3485 children)
3487 (if (eq custom-buffer-style 'tree)
3489 ;; Draw a tree-style `custom-face' widget
3490 (progn
3491 (insert (widget-get widget :custom-prefix)
3492 (if (widget-get widget :custom-last) " `--- " " |--- "))
3493 (push (widget-create-child-and-convert
3494 widget 'custom-browse-face-tag)
3495 buttons)
3496 (insert " " tag "\n")
3497 (widget-put widget :buttons buttons))
3499 ;; Draw an ordinary `custom-face' widget
3500 (let ((opoint (point)))
3501 ;; Visibility indicator.
3502 (push (widget-create-child-and-convert
3503 widget 'custom-visibility
3504 :help-echo "Hide or show this face."
3505 :on "Hide" :off "Show"
3506 :on-glyph "down" :off-glyph "right"
3507 :action 'custom-toggle-hide-face
3508 (not hiddenp))
3509 buttons)
3510 ;; Face name (tag).
3511 (insert " " tag)
3512 (widget-specify-sample widget opoint (point)))
3513 (insert
3514 (cond ((eq custom-buffer-style 'face) " ")
3515 ((string-match "face\\'" tag) ":")
3516 (t " face: ")))
3518 ;; Face sample.
3519 (let ((sample-indent (widget-get widget :sample-indent))
3520 (indent-tabs-mode nil))
3521 (and sample-indent
3522 (<= (current-column) sample-indent)
3523 (indent-to-column sample-indent)))
3524 (push (widget-create-child-and-convert
3525 widget 'item
3526 :format "[%{%t%}]"
3527 :sample-face (let ((spec (widget-get widget :shown-value)))
3528 (if spec (face-spec-choose spec) symbol))
3529 :tag "sample")
3530 buttons)
3531 (insert "\n")
3533 ;; Magic.
3534 (unless (eq (widget-get widget :custom-style) 'simple)
3535 (let ((magic (widget-create-child-and-convert
3536 widget 'custom-magic nil)))
3537 (widget-put widget :custom-magic magic)
3538 (push magic buttons)))
3540 ;; Update buttons.
3541 (widget-put widget :buttons buttons)
3543 ;; Insert documentation.
3544 (unless (and hiddenp (eq style 'simple))
3545 (widget-put widget :documentation-indent 3)
3546 (widget-add-documentation-string-button
3547 widget :visibility-widget 'custom-visibility)
3548 ;; The comment field
3549 (unless hiddenp
3550 (let* ((comment (get symbol 'face-comment))
3551 (comment-widget
3552 (widget-create-child-and-convert
3553 widget 'custom-comment
3554 :parent widget
3555 :value (or comment ""))))
3556 (widget-put widget :comment-widget comment-widget)
3557 (push comment-widget children))))
3559 ;; Editor.
3560 (unless (eq (preceding-char) ?\n)
3561 (insert "\n"))
3562 (unless hiddenp
3563 (custom-load-widget widget)
3564 (unless (widget-get widget :custom-form)
3565 (widget-put widget :custom-form custom-face-default-form))
3567 (let* ((spec (or (widget-get widget :shown-value)
3568 (custom-face-get-current-spec symbol)))
3569 (form (widget-get widget :custom-form))
3570 (indent (widget-get widget :indent))
3571 face-alist face-entry spec-default spec-match editor)
3573 ;; Find a display in SPEC matching the selected display.
3574 ;; This will use the usual face customization interface.
3575 (setq face-alist spec)
3576 (when (eq (car-safe (car-safe face-alist)) 'default)
3577 (setq spec-default (pop face-alist)))
3579 (while (and face-alist (listp face-alist) (null spec-match))
3580 (setq face-entry (car face-alist))
3581 (and (listp face-entry)
3582 (face-spec-set-match-display (car face-entry)
3583 (selected-frame))
3584 (widget-apply custom-face-edit :match (cadr face-entry))
3585 (setq spec-match face-entry))
3586 (setq face-alist (cdr face-alist)))
3588 ;; Insert the appropriate editing widget.
3589 (setq editor
3590 (cond
3591 ((and (eq form 'selected)
3592 (or spec-match spec-default))
3593 (when indent (insert-char ?\s indent))
3594 (widget-create-child-and-convert
3595 widget 'custom-face-edit
3596 :value (cadr spec-match)
3597 :default-face-attributes (cadr spec-default)))
3598 ((and (not (eq form 'lisp))
3599 (widget-apply custom-face-all :match spec))
3600 (widget-create-child-and-convert
3601 widget 'custom-face-all :value spec))
3603 (when indent
3604 (insert-char ?\s indent))
3605 (widget-create-child-and-convert
3606 widget 'sexp :value spec))))
3607 (custom-face-state-set widget)
3608 (push editor children)
3609 (widget-put widget :children children))))))
3611 (defvar custom-face-menu
3612 `(("Set for Current Session" custom-face-set)
3613 ,@(when (or custom-file init-file-user)
3614 '(("Save for Future Sessions" custom-face-save)))
3615 ("Undo Edits" custom-redraw
3616 (lambda (widget)
3617 (memq (widget-get widget :custom-state) '(modified changed))))
3618 ("Reset to Saved" custom-face-reset-saved
3619 (lambda (widget)
3620 (or (get (widget-value widget) 'saved-face)
3621 (get (widget-value widget) 'saved-face-comment))))
3622 ,@(when (or custom-file init-file-user)
3623 '(("Erase Customization" custom-face-reset-standard
3624 (lambda (widget)
3625 (get (widget-value widget) 'face-defface-spec)))))
3626 ("---" ignore ignore)
3627 ("Add Comment" custom-comment-show custom-comment-invisible-p)
3628 ("---" ignore ignore)
3629 ("For Current Display" custom-face-edit-selected
3630 (lambda (widget)
3631 (not (eq (widget-get widget :custom-form) 'selected))))
3632 ("For All Kinds of Displays" custom-face-edit-all
3633 (lambda (widget)
3634 (not (eq (widget-get widget :custom-form) 'all))))
3635 ("Show Lisp Expression" custom-face-edit-lisp
3636 (lambda (widget)
3637 (not (eq (widget-get widget :custom-form) 'lisp)))))
3638 "Alist of actions for the `custom-face' widget.
3639 Each entry has the form (NAME ACTION FILTER) where NAME is the name of
3640 the menu entry, ACTION is the function to call on the widget when the
3641 menu is selected, and FILTER is a predicate which takes a `custom-face'
3642 widget as an argument, and returns non-nil if ACTION is valid on that
3643 widget. If FILTER is nil, ACTION is always valid.")
3645 (defun custom-face-edit-selected (widget)
3646 "Edit selected attributes of the value of WIDGET."
3647 (widget-put widget :custom-state 'unknown)
3648 (widget-put widget :custom-form 'selected)
3649 (custom-redraw widget))
3651 (defun custom-face-edit-all (widget)
3652 "Edit all attributes of the value of WIDGET."
3653 (widget-put widget :custom-state 'unknown)
3654 (widget-put widget :custom-form 'all)
3655 (custom-redraw widget))
3657 (defun custom-face-edit-lisp (widget)
3658 "Edit the Lisp representation of the value of WIDGET."
3659 (widget-put widget :custom-state 'unknown)
3660 (widget-put widget :custom-form 'lisp)
3661 (custom-redraw widget))
3663 (defun custom-face-state (face)
3664 "Return the current state of the face FACE.
3665 This is one of `set', `saved', `changed', `themed', or `rogue'."
3666 (let* ((comment (get face 'face-comment))
3667 (state
3668 (cond
3669 ((or (get face 'customized-face)
3670 (get face 'customized-face-comment))
3671 (if (equal (get face 'customized-face-comment) comment)
3672 'set
3673 'changed))
3674 ((or (get face 'saved-face)
3675 (get face 'saved-face-comment))
3676 (if (equal (get face 'saved-face-comment) comment)
3677 (cond
3678 ((eq 'user (caar (get face 'theme-face)))
3679 'saved)
3680 ((eq 'changed (caar (get face 'theme-face)))
3681 'changed)
3682 (t 'themed))
3683 'changed))
3684 ((get face 'face-defface-spec)
3685 (if (equal comment nil)
3686 'standard
3687 'changed))
3688 (t 'rogue))))
3689 ;; If the user called set-face-attribute to change the default for
3690 ;; new frames, this face is "set outside of Customize".
3691 (if (and (not (eq state 'rogue))
3692 (get face 'face-modified))
3693 'changed
3694 state)))
3696 (defun custom-face-state-set (widget)
3697 "Set the state of WIDGET."
3698 (widget-put widget :custom-state
3699 (custom-face-state (widget-value widget))))
3701 (defun custom-face-action (widget &optional event)
3702 "Show the menu for `custom-face' WIDGET.
3703 Optional EVENT is the location for the menu."
3704 (if (eq (widget-get widget :custom-state) 'hidden)
3705 (custom-toggle-hide widget)
3706 (let* ((completion-ignore-case t)
3707 (symbol (widget-get widget :value))
3708 (answer (widget-choose (concat "Operation on "
3709 (custom-unlispify-tag-name symbol))
3710 (custom-menu-filter custom-face-menu
3711 widget)
3712 event)))
3713 (if answer
3714 (funcall answer widget)))))
3716 (defun custom-face-set (widget)
3717 "Make the face attributes in WIDGET take effect."
3718 (let* ((symbol (widget-value widget))
3719 (value (custom-face-widget-to-spec widget))
3720 (comment-widget (widget-get widget :comment-widget))
3721 (comment (widget-value comment-widget)))
3722 (when (equal comment "")
3723 (setq comment nil)
3724 ;; Make the comment invisible by hand if it's empty
3725 (custom-comment-hide comment-widget))
3726 (put symbol 'customized-face value)
3727 (custom-push-theme 'theme-face symbol 'user 'set value)
3728 (if (face-spec-choose value)
3729 (face-spec-set symbol value t)
3730 ;; face-set-spec ignores empty attribute lists, so just give it
3731 ;; something harmless instead.
3732 (face-spec-set symbol '((t :foreground unspecified)) t))
3733 (put symbol 'customized-face-comment comment)
3734 (put symbol 'face-comment comment)
3735 (custom-face-state-set widget)
3736 (custom-redraw-magic widget)))
3738 (defun custom-face-mark-to-save (widget)
3739 "Mark for saving the face edited by WIDGET."
3740 (let* ((symbol (widget-value widget))
3741 (value (custom-face-widget-to-spec widget))
3742 (comment-widget (widget-get widget :comment-widget))
3743 (comment (widget-value comment-widget)))
3744 (when (equal comment "")
3745 (setq comment nil)
3746 ;; Make the comment invisible by hand if it's empty
3747 (custom-comment-hide comment-widget))
3748 (custom-push-theme 'theme-face symbol 'user 'set value)
3749 (if (face-spec-choose value)
3750 (face-spec-set symbol value t)
3751 ;; face-set-spec ignores empty attribute lists, so just give it
3752 ;; something harmless instead.
3753 (face-spec-set symbol '((t :foreground unspecified)) t))
3754 (unless (eq (widget-get widget :custom-state) 'standard)
3755 (put symbol 'saved-face value))
3756 (put symbol 'customized-face nil)
3757 (put symbol 'face-comment comment)
3758 (put symbol 'customized-face-comment nil)
3759 (put symbol 'saved-face-comment comment)))
3761 (defsubst custom-face-state-set-and-redraw (widget)
3762 "Set state of face widget WIDGET and redraw with current settings."
3763 (custom-face-state-set widget)
3764 (custom-redraw-magic widget))
3766 (defun custom-face-save (widget)
3767 "Save the face edited by WIDGET."
3768 (custom-face-mark-to-save widget)
3769 (custom-save-all)
3770 (custom-face-state-set-and-redraw widget))
3772 ;; For backward compatibility.
3773 (define-obsolete-function-alias 'custom-face-save-command 'custom-face-save
3774 "22.1")
3776 (defun custom-face-reset-saved (widget)
3777 "Restore WIDGET to the face's default attributes."
3778 (let* ((symbol (widget-value widget))
3779 (child (car (widget-get widget :children)))
3780 (value (get symbol 'saved-face))
3781 (comment (get symbol 'saved-face-comment))
3782 (comment-widget (widget-get widget :comment-widget)))
3783 (unless (or value comment)
3784 (error "No saved value for this face"))
3785 (put symbol 'customized-face nil)
3786 (put symbol 'customized-face-comment nil)
3787 (custom-push-theme 'theme-face symbol 'user 'set value)
3788 (face-spec-set symbol value t)
3789 (put symbol 'face-comment comment)
3790 (widget-value-set child value)
3791 ;; This call manages the comment visibility
3792 (widget-value-set comment-widget (or comment ""))
3793 (custom-face-state-set widget)
3794 (custom-redraw-magic widget)))
3796 (defun custom-face-standard-value (widget)
3797 (get (widget-value widget) 'face-defface-spec))
3799 (defun custom-face-mark-to-reset-standard (widget)
3800 "Restore widget WIDGET to the face's standard attribute values.
3801 If `custom-reset-standard-faces-list' is nil, save, reset and
3802 redraw the widget immediately."
3803 (let* ((symbol (widget-value widget))
3804 (child (car (widget-get widget :children)))
3805 (value (get symbol 'face-defface-spec))
3806 (comment-widget (widget-get widget :comment-widget)))
3807 (unless value
3808 (error "No standard setting for this face"))
3809 (put symbol 'customized-face nil)
3810 (put symbol 'customized-face-comment nil)
3811 (custom-push-theme 'theme-face symbol 'user 'reset)
3812 (face-spec-set symbol value t)
3813 (custom-theme-recalc-face symbol)
3814 (if (and custom-reset-standard-faces-list
3815 (or (get symbol 'saved-face) (get symbol 'saved-face-comment)))
3816 ;; Do this later.
3817 (progn
3818 (put symbol 'saved-face nil)
3819 (put symbol 'saved-face-comment nil)
3820 ;; Append this to `custom-reset-standard-faces-list' and have
3821 ;; `custom-reset-standard-save-and-update' save setting to the
3822 ;; file, update the widget's state, and redraw it.
3823 (setq custom-reset-standard-faces-list
3824 (cons widget custom-reset-standard-faces-list)))
3825 (when (or (get symbol 'saved-face) (get symbol 'saved-face-comment))
3826 (put symbol 'saved-face nil)
3827 (put symbol 'saved-face-comment nil)
3828 (custom-save-all))
3829 (put symbol 'face-comment nil)
3830 (widget-value-set child
3831 (custom-pre-filter-face-spec
3832 (list (list t (custom-face-attributes-get
3833 symbol nil)))))
3834 ;; This call manages the comment visibility
3835 (widget-value-set comment-widget "")
3836 (custom-face-state-set widget)
3837 (custom-redraw-magic widget))))
3839 (defun custom-face-reset-standard (widget)
3840 "Restore WIDGET to the face's standard attribute values.
3841 This operation eliminates any saved attributes for the face,
3842 restoring it to the state of a face that has never been customized."
3843 (let (custom-reset-standard-faces-list)
3844 (custom-face-mark-to-reset-standard widget)))
3846 ;;; The `face' Widget.
3848 (defvar widget-face-prompt-value-history nil
3849 "History of input to `widget-face-prompt-value'.")
3851 (define-widget 'face 'symbol
3852 "A Lisp face name (with sample)."
3853 :format "%{%t%}: (%{sample%}) %v"
3854 :tag "Face"
3855 :value 'default
3856 :sample-face-get 'widget-face-sample-face-get
3857 :notify 'widget-face-notify
3858 :match (lambda (_widget value) (facep value))
3859 :completions (apply-partially #'completion-table-with-predicate
3860 obarray #'facep 'strict)
3861 :prompt-match 'facep
3862 :prompt-history 'widget-face-prompt-value-history
3863 :validate (lambda (widget)
3864 (unless (facep (widget-value widget))
3865 (widget-put widget
3866 :error (format "Invalid face: %S"
3867 (widget-value widget)))
3868 widget)))
3870 (defun widget-face-sample-face-get (widget)
3871 (let ((value (widget-value widget)))
3872 (if (facep value)
3873 value
3874 'default)))
3876 (defun widget-face-notify (widget child &optional event)
3877 "Update the sample, and notify the parent."
3878 (overlay-put (widget-get widget :sample-overlay)
3879 'face (widget-apply widget :sample-face-get))
3880 (widget-default-notify widget child event))
3883 ;;; The `hook' Widget.
3885 (define-widget 'hook 'list
3886 "An Emacs Lisp hook."
3887 :value-to-internal (lambda (_widget value)
3888 (if (and value (symbolp value))
3889 (list value)
3890 value))
3891 :match (lambda (widget value)
3892 (or (symbolp value)
3893 (widget-group-match widget value)))
3894 ;; Avoid adding undefined functions to the hook, especially for
3895 ;; things like `find-file-hook' or even more basic ones, to avoid
3896 ;; chaos.
3897 :set (lambda (symbol value)
3898 (dolist (elt value)
3899 (if (fboundp elt)
3900 (add-hook symbol elt))))
3901 :convert-widget 'custom-hook-convert-widget
3902 :tag "Hook")
3904 (defun custom-hook-convert-widget (widget)
3905 ;; Handle `:options'.
3906 (let* ((options (widget-get widget :options))
3907 (other `(editable-list :inline t
3908 :entry-format "%i %d%v"
3909 (function :format " %v")))
3910 (args (if options
3911 (list `(checklist :inline t
3912 ,@(mapcar (lambda (entry)
3913 `(function-item ,entry))
3914 options))
3915 other)
3916 (list other))))
3917 (widget-put widget :args args)
3918 widget))
3920 ;;; The `custom-group-link' Widget.
3922 (define-widget 'custom-group-link 'link
3923 "Show parent in other window when activated."
3924 :button-face 'custom-link
3925 :mouse-face 'highlight
3926 :pressed-face 'highlight
3927 :help-echo "Create customization buffer for this group."
3928 :keymap custom-mode-link-map
3929 :follow-link 'mouse-face
3930 :action 'custom-group-link-action)
3932 (defun custom-group-link-action (widget &rest _ignore)
3933 (customize-group (widget-value widget)))
3935 ;;; The `custom-group' Widget.
3937 (defcustom custom-group-tag-faces nil
3938 ;; In XEmacs, this ought to play games with font size.
3939 ;; Fixme: make it do so in Emacs.
3940 "Face used for group tags.
3941 The first member is used for level 1 groups, the second for level 2,
3942 and so forth. The remaining group tags are shown with `custom-group-tag'."
3943 :type '(repeat face)
3944 :group 'custom-faces)
3946 (defface custom-group-tag-1
3947 `((((class color)
3948 (background dark))
3949 (:foreground "pink" :weight bold :height 1.2 :inherit variable-pitch))
3950 (((min-colors 88) (class color)
3951 (background light))
3952 (:foreground "red1" :weight bold :height 1.2 :inherit variable-pitch))
3953 (((class color)
3954 (background light))
3955 (:foreground "red" :weight bold :height 1.2 :inherit variable-pitch))
3956 (t (:weight bold)))
3957 "Face used for group tags."
3958 :group 'custom-faces)
3959 (define-obsolete-face-alias 'custom-group-tag-face-1 'custom-group-tag-1 "22.1")
3961 (defface custom-group-tag
3962 `((((class color)
3963 (background dark))
3964 (:foreground "light blue" :weight bold :height 1.2 :inherit variable-pitch))
3965 (((min-colors 88) (class color)
3966 (background light))
3967 (:foreground "blue1" :weight bold :height 1.2 :inherit variable-pitch))
3968 (((class color)
3969 (background light))
3970 (:foreground "blue" :weight bold :height 1.2 :inherit variable-pitch))
3971 (t (:weight bold)))
3972 "Face used for low level group tags."
3973 :group 'custom-faces)
3974 (define-obsolete-face-alias 'custom-group-tag-face 'custom-group-tag "22.1")
3976 (define-widget 'custom-group 'custom
3977 "Customize group."
3978 :format "%v"
3979 :sample-face-get 'custom-group-sample-face-get
3980 :documentation-property 'group-documentation
3981 :help-echo "Set or reset all members of this group."
3982 :value-create 'custom-group-value-create
3983 :action 'custom-group-action
3984 :custom-category 'group
3985 :custom-set 'custom-group-set
3986 :custom-mark-to-save 'custom-group-mark-to-save
3987 :custom-reset-current 'custom-group-reset-current
3988 :custom-reset-saved 'custom-group-reset-saved
3989 :custom-reset-standard 'custom-group-reset-standard
3990 :custom-mark-to-reset-standard 'custom-group-mark-to-reset-standard
3991 :custom-state-set-and-redraw 'custom-group-state-set-and-redraw
3992 :custom-menu 'custom-group-menu-create)
3994 (defun custom-group-sample-face-get (widget)
3995 ;; Use :sample-face.
3996 (or (nth (1- (widget-get widget :custom-level)) custom-group-tag-faces)
3997 'custom-group-tag))
3999 (define-widget 'custom-group-visibility 'visibility
4000 "An indicator and manipulator for hidden group contents."
4001 :create 'custom-group-visibility-create)
4003 (defun custom-group-visibility-create (widget)
4004 (let ((visible (widget-value widget)))
4005 (if visible
4006 (insert "--------")))
4007 (widget-default-create widget))
4009 (defun custom-group-members (symbol groups-only)
4010 "Return SYMBOL's custom group members.
4011 If GROUPS-ONLY non-nil, return only those members that are groups."
4012 (if (not groups-only)
4013 (get symbol 'custom-group)
4014 (let (members)
4015 (dolist (entry (get symbol 'custom-group))
4016 (when (eq (nth 1 entry) 'custom-group)
4017 (push entry members)))
4018 (nreverse members))))
4020 (defun custom-group-value-create (widget)
4021 "Insert a customize group for WIDGET in the current buffer."
4022 (unless (eq (widget-get widget :custom-state) 'hidden)
4023 (custom-load-widget widget))
4024 (let* ((state (widget-get widget :custom-state))
4025 (level (widget-get widget :custom-level))
4026 ;; (indent (widget-get widget :indent))
4027 (prefix (widget-get widget :custom-prefix))
4028 (buttons (widget-get widget :buttons))
4029 (tag (widget-get widget :tag))
4030 (symbol (widget-value widget))
4031 (members (custom-group-members symbol
4032 (and (eq custom-buffer-style 'tree)
4033 custom-browse-only-groups)))
4034 (doc (widget-docstring widget)))
4035 (cond ((and (eq custom-buffer-style 'tree)
4036 (eq state 'hidden)
4037 (or members (custom-unloaded-widget-p widget)))
4038 (custom-browse-insert-prefix prefix)
4039 (push (widget-create-child-and-convert
4040 widget 'custom-browse-visibility
4041 ;; :tag-glyph "plus"
4042 :tag "+")
4043 buttons)
4044 (insert "-- ")
4045 ;; (widget-glyph-insert nil "-- " "horizontal")
4046 (push (widget-create-child-and-convert
4047 widget 'custom-browse-group-tag)
4048 buttons)
4049 (insert " " tag "\n")
4050 (widget-put widget :buttons buttons))
4051 ((and (eq custom-buffer-style 'tree)
4052 (zerop (length members)))
4053 (custom-browse-insert-prefix prefix)
4054 (insert "[ ]-- ")
4055 ;; (widget-glyph-insert nil "[ ]" "empty")
4056 ;; (widget-glyph-insert nil "-- " "horizontal")
4057 (push (widget-create-child-and-convert
4058 widget 'custom-browse-group-tag)
4059 buttons)
4060 (insert " " tag "\n")
4061 (widget-put widget :buttons buttons))
4062 ((eq custom-buffer-style 'tree)
4063 (custom-browse-insert-prefix prefix)
4064 (if (zerop (length members))
4065 (progn
4066 (custom-browse-insert-prefix prefix)
4067 (insert "[ ]-- ")
4068 ;; (widget-glyph-insert nil "[ ]" "empty")
4069 ;; (widget-glyph-insert nil "-- " "horizontal")
4070 (push (widget-create-child-and-convert
4071 widget 'custom-browse-group-tag)
4072 buttons)
4073 (insert " " tag "\n")
4074 (widget-put widget :buttons buttons))
4075 (push (widget-create-child-and-convert
4076 widget 'custom-browse-visibility
4077 ;; :tag-glyph "minus"
4078 :tag "-")
4079 buttons)
4080 (insert "-\\ ")
4081 ;; (widget-glyph-insert nil "-\\ " "top")
4082 (push (widget-create-child-and-convert
4083 widget 'custom-browse-group-tag)
4084 buttons)
4085 (insert " " tag "\n")
4086 (widget-put widget :buttons buttons)
4087 (message "Creating group...")
4088 (let* ((members (custom-sort-items
4089 members
4090 ;; Never sort the top-level custom group.
4091 (unless (eq symbol 'emacs)
4092 custom-browse-sort-alphabetically)
4093 custom-browse-order-groups))
4094 (prefixes (widget-get widget :custom-prefixes))
4095 (custom-prefix-list (custom-prefix-add symbol prefixes))
4096 (extra-prefix (if (widget-get widget :custom-last)
4098 " | "))
4099 (prefix (concat prefix extra-prefix))
4100 children entry)
4101 (while members
4102 (setq entry (car members)
4103 members (cdr members))
4104 (push (widget-create-child-and-convert
4105 widget (nth 1 entry)
4106 :group widget
4107 :tag (custom-unlispify-tag-name (nth 0 entry))
4108 :custom-prefixes custom-prefix-list
4109 :custom-level (1+ level)
4110 :custom-last (null members)
4111 :value (nth 0 entry)
4112 :custom-prefix prefix)
4113 children))
4114 (widget-put widget :children (reverse children)))
4115 (message "Creating group...done")))
4116 ;; Nested style.
4117 ((eq state 'hidden)
4118 ;; Create level indicator.
4119 ;; Create tag.
4120 (if (eq custom-buffer-style 'links)
4121 (push (widget-create-child-and-convert
4122 widget 'custom-group-link
4123 :tag tag
4124 symbol)
4125 buttons)
4126 (insert-char ?\ (* custom-buffer-indent (1- level)))
4127 (insert "-- ")
4128 (push (widget-create-child-and-convert
4129 widget 'custom-group-visibility
4130 :help-echo "Show members of this group."
4131 :action 'custom-toggle-parent
4132 (not (eq state 'hidden)))
4133 buttons))
4134 (insert " : ")
4135 ;; Create magic button.
4136 (let ((magic (widget-create-child-and-convert
4137 widget 'custom-magic nil)))
4138 (widget-put widget :custom-magic magic)
4139 (push magic buttons))
4140 ;; Update buttons.
4141 (widget-put widget :buttons buttons)
4142 ;; Insert documentation.
4143 (if (and (eq custom-buffer-style 'links) (> level 1))
4144 (widget-put widget :documentation-indent 0))
4145 (widget-add-documentation-string-button
4146 widget :visibility-widget 'custom-visibility))
4148 ;; Nested style.
4149 (t ;Visible.
4150 ;; Draw a horizontal line (this works for both graphical
4151 ;; and text displays):
4152 (let ((p (point)))
4153 (insert "\n")
4154 (put-text-property p (1+ p) 'face '(:underline t))
4155 (overlay-put (make-overlay p (1+ p))
4156 'before-string
4157 (propertize "\n" 'face '(:underline t)
4158 'display '(space :align-to 999))))
4160 ;; Add parent groups references above the group.
4161 (when (eq level 1)
4162 (if (custom-add-parent-links widget "Parent groups:")
4163 (insert "\n")))
4164 (insert-char ?\ (* custom-buffer-indent (1- level)))
4165 ;; Create tag.
4166 (let ((start (point)))
4167 (insert tag " group: ")
4168 (widget-specify-sample widget start (point)))
4169 (cond
4170 ((not doc)
4171 (insert " Group definition missing. "))
4172 ((< (length doc) 50)
4173 (insert doc)))
4174 ;; Create visibility indicator.
4175 (unless (eq custom-buffer-style 'links)
4176 (insert "--------")
4177 (push (widget-create-child-and-convert
4178 widget 'visibility
4179 :help-echo "Hide members of this group."
4180 :action 'custom-toggle-parent
4181 (not (eq state 'hidden)))
4182 buttons)
4183 (insert " "))
4184 (insert "\n")
4185 ;; Create magic button.
4186 (let ((magic (widget-create-child-and-convert
4187 widget 'custom-magic
4188 :indent 0
4189 nil)))
4190 (widget-put widget :custom-magic magic)
4191 (push magic buttons))
4192 ;; Update buttons.
4193 (widget-put widget :buttons buttons)
4194 ;; Insert documentation.
4195 (when (and doc (>= (length doc) 50))
4196 (widget-add-documentation-string-button
4197 widget :visibility-widget 'custom-visibility))
4199 ;; Parent groups.
4200 (if nil ;;; This should test that the buffer
4201 ;;; was not made to display a group.
4202 (when (eq level 1)
4203 (insert-char ?\ custom-buffer-indent)
4204 (custom-add-parent-links widget)))
4205 (custom-add-see-also widget
4206 (make-string (* custom-buffer-indent level)
4207 ?\ ))
4208 ;; Members.
4209 (message "Creating group...")
4210 (let* ((members (custom-sort-items
4211 members
4212 ;; Never sort the top-level custom group.
4213 (unless (eq symbol 'emacs)
4214 custom-buffer-sort-alphabetically)
4215 custom-buffer-order-groups))
4216 (prefixes (widget-get widget :custom-prefixes))
4217 (custom-prefix-list (custom-prefix-add symbol prefixes))
4218 (len (length members))
4219 (count 0)
4220 (reporter (make-progress-reporter
4221 "Creating group entries..." 0 len))
4222 children)
4223 (setq children
4224 (mapcar
4225 (lambda (entry)
4226 (widget-insert "\n")
4227 (progress-reporter-update reporter (setq count (1+ count)))
4228 (let ((sym (nth 0 entry))
4229 (type (nth 1 entry)))
4230 (prog1
4231 (widget-create-child-and-convert
4232 widget type
4233 :group widget
4234 :tag (custom-unlispify-tag-name sym)
4235 :custom-prefixes custom-prefix-list
4236 :custom-level (1+ level)
4237 :value sym)
4238 (unless (eq (preceding-char) ?\n)
4239 (widget-insert "\n")))))
4240 members))
4241 (mapc 'custom-magic-reset children)
4242 (widget-put widget :children children)
4243 (custom-group-state-update widget)
4244 (progress-reporter-done reporter))
4245 ;; End line
4246 (let ((p (1+ (point))))
4247 (insert "\n\n")
4248 (put-text-property p (1+ p) 'face '(:underline t))
4249 (overlay-put (make-overlay p (1+ p))
4250 'before-string
4251 (propertize "\n" 'face '(:underline t)
4252 'display '(space :align-to 999))))))))
4254 (defvar custom-group-menu
4255 `(("Set for Current Session" custom-group-set
4256 (lambda (widget)
4257 (eq (widget-get widget :custom-state) 'modified)))
4258 ,@(when (or custom-file init-file-user)
4259 '(("Save for Future Sessions" custom-group-save
4260 (lambda (widget)
4261 (memq (widget-get widget :custom-state) '(modified set))))))
4262 ("Undo Edits" custom-group-reset-current
4263 (lambda (widget)
4264 (memq (widget-get widget :custom-state) '(modified))))
4265 ("Reset to Saved" custom-group-reset-saved
4266 (lambda (widget)
4267 (memq (widget-get widget :custom-state) '(modified set))))
4268 ,@(when (or custom-file init-file-user)
4269 '(("Erase Customization" custom-group-reset-standard
4270 (lambda (widget)
4271 (memq (widget-get widget :custom-state) '(modified set saved)))))))
4272 "Alist of actions for the `custom-group' widget.
4273 Each entry has the form (NAME ACTION FILTER) where NAME is the name of
4274 the menu entry, ACTION is the function to call on the widget when the
4275 menu is selected, and FILTER is a predicate which takes a `custom-group'
4276 widget as an argument, and returns non-nil if ACTION is valid on that
4277 widget. If FILTER is nil, ACTION is always valid.")
4279 (defun custom-group-action (widget &optional event)
4280 "Show the menu for `custom-group' WIDGET.
4281 Optional EVENT is the location for the menu."
4282 (if (eq (widget-get widget :custom-state) 'hidden)
4283 (custom-toggle-hide widget)
4284 (let* ((completion-ignore-case t)
4285 (answer (widget-choose (concat "Operation on "
4286 (custom-unlispify-tag-name
4287 (widget-get widget :value)))
4288 (custom-menu-filter custom-group-menu
4289 widget)
4290 event)))
4291 (if answer
4292 (funcall answer widget)))))
4294 (defun custom-group-set (widget)
4295 "Set changes in all modified group members."
4296 (dolist (child (widget-get widget :children))
4297 (when (eq (widget-get child :custom-state) 'modified)
4298 (widget-apply child :custom-set))))
4300 (defun custom-group-mark-to-save (widget)
4301 "Mark all modified group members for saving."
4302 (dolist (child (widget-get widget :children))
4303 (when (memq (widget-get child :custom-state) '(modified set))
4304 (widget-apply child :custom-mark-to-save))))
4306 (defsubst custom-group-state-set-and-redraw (widget)
4307 "Set state of group widget WIDGET and redraw with current settings."
4308 (dolist (child (widget-get widget :children))
4309 (when (memq (widget-get child :custom-state) '(modified set))
4310 (widget-apply child :custom-state-set-and-redraw))))
4312 (defun custom-group-save (widget)
4313 "Save all modified group members."
4314 (custom-group-mark-to-save widget)
4315 (custom-save-all)
4316 (custom-group-state-set-and-redraw widget))
4318 (defun custom-group-reset-current (widget)
4319 "Reset all modified group members."
4320 (dolist (child (widget-get widget :children))
4321 (when (eq (widget-get child :custom-state) 'modified)
4322 (widget-apply child :custom-reset-current))))
4324 (defun custom-group-reset-saved (widget)
4325 "Reset all modified or set group members."
4326 (dolist (child (widget-get widget :children))
4327 (when (memq (widget-get child :custom-state) '(modified set))
4328 (widget-apply child :custom-reset-saved))))
4330 (defun custom-group-reset-standard (widget)
4331 "Reset all modified, set, or saved group members."
4332 (let ((custom-reset-standard-variables-list '(t))
4333 (custom-reset-standard-faces-list '(t)))
4334 (custom-group-mark-to-reset-standard widget)
4335 (custom-reset-standard-save-and-update)))
4337 (defun custom-group-mark-to-reset-standard (widget)
4338 "Mark to reset all modified, set, or saved group members."
4339 (dolist (child (widget-get widget :children))
4340 (when (memq (widget-get child :custom-state)
4341 '(modified set saved))
4342 (widget-apply child :custom-mark-to-reset-standard))))
4344 (defun custom-group-state-update (widget)
4345 "Update magic."
4346 (unless (eq (widget-get widget :custom-state) 'hidden)
4347 (let* ((children (widget-get widget :children))
4348 (states (mapcar (lambda (child)
4349 (widget-get child :custom-state))
4350 children))
4351 (magics custom-magic-alist)
4352 (found 'standard))
4353 (while magics
4354 (let ((magic (car (car magics))))
4355 (if (and (not (eq magic 'hidden))
4356 (memq magic states))
4357 (setq found magic
4358 magics nil)
4359 (setq magics (cdr magics)))))
4360 (widget-put widget :custom-state found)))
4361 (custom-magic-reset widget))
4363 ;;; Reading and writing the custom file.
4365 ;;;###autoload
4366 (defcustom custom-file nil
4367 "File used for storing customization information.
4368 The default is nil, which means to use your init file
4369 as specified by `user-init-file'. If the value is not nil,
4370 it should be an absolute file name.
4372 You can set this option through Custom, if you carefully read the
4373 last paragraph below. However, usually it is simpler to write
4374 something like the following in your init file:
4376 \(setq custom-file \"~/.emacs-custom.el\")
4377 \(load custom-file)
4379 Note that both lines are necessary: the first line tells Custom to
4380 save all customizations in this file, but does not load it.
4382 When you change this variable outside Custom, look in the
4383 previous custom file \(usually your init file) for the
4384 forms `(custom-set-variables ...)' and `(custom-set-faces ...)',
4385 and copy them (whichever ones you find) to the new custom file.
4386 This will preserve your existing customizations.
4388 If you save this option using Custom, Custom will write all
4389 currently saved customizations, including the new one for this
4390 option itself, into the file you specify, overwriting any
4391 `custom-set-variables' and `custom-set-faces' forms already
4392 present in that file. It will not delete any customizations from
4393 the old custom file. You should do that manually if that is what you
4394 want. You also have to put something like `\(load \"CUSTOM-FILE\")
4395 in your init file, where CUSTOM-FILE is the actual name of the
4396 file. Otherwise, Emacs will not load the file when it starts up,
4397 and hence will not set `custom-file' to that file either."
4398 :type '(choice (const :tag "Your Emacs init file" nil)
4399 (file :format "%t:%v%d"
4400 :doc
4401 "Please read entire docstring below before setting \
4402 this through Custom.
4403 Click on \"More\" \(or position point there and press RETURN)
4404 if only the first line of the docstring is shown."))
4405 :group 'customize)
4407 (defun custom-file (&optional no-error)
4408 "Return the file name for saving customizations."
4409 (if (null user-init-file)
4410 ;; Started with -q, i.e. the file containing Custom settings
4411 ;; hasn't been read. Saving settings there won't make much
4412 ;; sense.
4413 (if no-error
4415 (error "Saving settings from \"emacs -q\" would overwrite existing customizations"))
4416 (file-chase-links (or custom-file user-init-file))))
4418 ;; If recentf-mode is non-nil, this is defined.
4419 (declare-function recentf-expand-file-name "recentf" (name))
4421 ;;;###autoload
4422 (defun custom-save-all ()
4423 "Save all customizations in `custom-file'."
4424 (when (and (null custom-file) init-file-had-error)
4425 (error "Cannot save customizations; init file was not fully loaded"))
4426 (let* ((filename (custom-file))
4427 (recentf-exclude
4428 (if recentf-mode
4429 (cons (concat "\\`"
4430 (regexp-quote
4431 (recentf-expand-file-name (custom-file)))
4432 "\\'")
4433 recentf-exclude)))
4434 (old-buffer (find-buffer-visiting filename))
4435 old-buffer-name)
4437 (with-current-buffer (let ((find-file-visit-truename t))
4438 (or old-buffer (find-file-noselect filename)))
4439 ;; We'll save using file-precious-flag, so avoid destroying
4440 ;; symlinks. (If we're not already visiting the buffer, this is
4441 ;; handled by find-file-visit-truename, above.)
4442 (when old-buffer
4443 (setq old-buffer-name (buffer-file-name))
4444 (set-visited-file-name (file-chase-links filename)))
4446 (unless (eq major-mode 'emacs-lisp-mode)
4447 (emacs-lisp-mode))
4448 (let ((inhibit-read-only t)
4449 (print-length nil)
4450 (print-level nil))
4451 (custom-save-variables)
4452 (custom-save-faces))
4453 (let ((file-precious-flag t))
4454 (save-buffer))
4455 (if old-buffer
4456 (progn
4457 (set-visited-file-name old-buffer-name)
4458 (set-buffer-modified-p nil))
4459 (kill-buffer (current-buffer))))))
4461 ;;;###autoload
4462 (defun customize-save-customized ()
4463 "Save all user options which have been set in this session."
4464 (interactive)
4465 (mapatoms (lambda (symbol)
4466 (let ((face (get symbol 'customized-face))
4467 (value (get symbol 'customized-value))
4468 (face-comment (get symbol 'customized-face-comment))
4469 (variable-comment
4470 (get symbol 'customized-variable-comment)))
4471 (when face
4472 (put symbol 'saved-face face)
4473 (custom-push-theme 'theme-face symbol 'user 'set value)
4474 (put symbol 'customized-face nil))
4475 (when value
4476 (put symbol 'saved-value value)
4477 (custom-push-theme 'theme-value symbol 'user 'set value)
4478 (put symbol 'customized-value nil))
4479 (when variable-comment
4480 (put symbol 'saved-variable-comment variable-comment)
4481 (put symbol 'customized-variable-comment nil))
4482 (when face-comment
4483 (put symbol 'saved-face-comment face-comment)
4484 (put symbol 'customized-face-comment nil)))))
4485 ;; We really should update all custom buffers here.
4486 (custom-save-all))
4488 ;; Editing the custom file contents in a buffer.
4490 (defun custom-save-delete (symbol)
4491 "Delete all calls to SYMBOL from the contents of the current buffer.
4492 Leave point at the old location of the first such call,
4493 or (if there were none) at the end of the buffer.
4495 This function does not save the buffer."
4496 (goto-char (point-min))
4497 ;; Skip all whitespace and comments.
4498 (while (forward-comment 1))
4499 (or (eobp)
4500 (save-excursion (forward-sexp (buffer-size)))) ; Test for scan errors.
4501 (let (first)
4502 (catch 'found
4503 (while t ;; We exit this loop only via throw.
4504 ;; Skip all whitespace and comments.
4505 (while (forward-comment 1))
4506 (let ((start (point))
4507 (sexp (condition-case nil
4508 (read (current-buffer))
4509 (end-of-file (throw 'found nil)))))
4510 (when (and (listp sexp)
4511 (eq (car sexp) symbol))
4512 (delete-region start (point))
4513 (unless first
4514 (setq first (point)))))))
4515 (if first
4516 (goto-char first)
4517 ;; Move in front of local variables, otherwise long Custom
4518 ;; entries would make them ineffective.
4519 (let ((pos (point-max))
4520 (case-fold-search t))
4521 (save-excursion
4522 (goto-char (point-max))
4523 (search-backward "\n\^L" (max (- (point-max) 3000) (point-min))
4524 'move)
4525 (when (search-forward "Local Variables:" nil t)
4526 (setq pos (line-beginning-position))))
4527 (goto-char pos)))))
4529 (defvar sort-fold-case) ; defined in sort.el
4531 (defun custom-save-variables ()
4532 "Save all customized variables in `custom-file'."
4533 (save-excursion
4534 (custom-save-delete 'custom-set-variables)
4535 (let ((standard-output (current-buffer))
4536 (saved-list (make-list 1 0))
4537 sort-fold-case)
4538 ;; First create a sorted list of saved variables.
4539 (mapatoms
4540 (lambda (symbol)
4541 (if (and (get symbol 'saved-value)
4542 ;; ignore theme values
4543 (or (null (get symbol 'theme-value))
4544 (eq 'user (caar (get symbol 'theme-value)))))
4545 (nconc saved-list (list symbol)))))
4546 (setq saved-list (sort (cdr saved-list) 'string<))
4547 (unless (bolp)
4548 (princ "\n"))
4549 (princ "(custom-set-variables
4550 ;; custom-set-variables was added by Custom.
4551 ;; If you edit it by hand, you could mess it up, so be careful.
4552 ;; Your init file should contain only one such instance.
4553 ;; If there is more than one, they won't work right.\n")
4554 (dolist (symbol saved-list)
4555 (let ((spec (car-safe (get symbol 'theme-value)))
4556 (value (get symbol 'saved-value))
4557 (requests (get symbol 'custom-requests))
4558 (now (and (not (custom-variable-p symbol))
4559 (or (boundp symbol)
4560 (eq (get symbol 'force-value)
4561 'rogue))))
4562 (comment (get symbol 'saved-variable-comment)))
4563 ;; Check REQUESTS for validity.
4564 (dolist (request requests)
4565 (when (and (symbolp request) (not (featurep request)))
4566 (message "Unknown requested feature: %s" request)
4567 (setq requests (delq request requests))))
4568 ;; Is there anything customized about this variable?
4569 (when (or (and spec (eq (car spec) 'user))
4570 comment
4571 (and (null spec) (get symbol 'saved-value)))
4572 ;; Output an element for this variable.
4573 ;; It has the form (SYMBOL VALUE-FORM NOW REQUESTS COMMENT).
4574 ;; SYMBOL is the variable name.
4575 ;; VALUE-FORM is an expression to return the customized value.
4576 ;; NOW if non-nil means always set the variable immediately
4577 ;; when the customizations are reloaded. This is used
4578 ;; for rogue variables
4579 ;; REQUESTS is a list of packages to load before setting the
4580 ;; variable. Each element of it will be passed to `require'.
4581 ;; COMMENT is whatever comment the user has specified
4582 ;; with the customize facility.
4583 (unless (bolp)
4584 (princ "\n"))
4585 (princ " '(")
4586 (prin1 symbol)
4587 (princ " ")
4588 (prin1 (car value))
4589 (when (or now requests comment)
4590 (princ " ")
4591 (prin1 now)
4592 (when (or requests comment)
4593 (princ " ")
4594 (prin1 requests)
4595 (when comment
4596 (princ " ")
4597 (prin1 comment))))
4598 (princ ")"))))
4599 (if (bolp)
4600 (princ " "))
4601 (princ ")")
4602 (unless (looking-at "\n")
4603 (princ "\n")))))
4605 (defun custom-save-faces ()
4606 "Save all customized faces in `custom-file'."
4607 (save-excursion
4608 (custom-save-delete 'custom-reset-faces)
4609 (custom-save-delete 'custom-set-faces)
4610 (let ((standard-output (current-buffer))
4611 (saved-list (make-list 1 0))
4612 sort-fold-case)
4613 ;; First create a sorted list of saved faces.
4614 (mapatoms
4615 (lambda (symbol)
4616 (if (and (get symbol 'saved-face)
4617 (eq 'user (car (car-safe (get symbol 'theme-face)))))
4618 (nconc saved-list (list symbol)))))
4619 (setq saved-list (sort (cdr saved-list) 'string<))
4620 ;; The default face must be first, since it affects the others.
4621 (if (memq 'default saved-list)
4622 (setq saved-list (cons 'default (delq 'default saved-list))))
4623 (unless (bolp)
4624 (princ "\n"))
4625 (princ "(custom-set-faces
4626 ;; custom-set-faces was added by Custom.
4627 ;; If you edit it by hand, you could mess it up, so be careful.
4628 ;; Your init file should contain only one such instance.
4629 ;; If there is more than one, they won't work right.\n")
4630 (dolist (symbol saved-list)
4631 (let ((spec (car-safe (get symbol 'theme-face)))
4632 (value (get symbol 'saved-face))
4633 (now (not (or (get symbol 'face-defface-spec)
4634 (and (not (custom-facep symbol))
4635 (not (get symbol 'force-face))))))
4636 (comment (get symbol 'saved-face-comment)))
4637 (when (or (and spec (eq (nth 0 spec) 'user))
4638 comment
4639 (and (null spec) (get symbol 'saved-face)))
4640 ;; Don't print default face here.
4641 (unless (bolp)
4642 (princ "\n"))
4643 (princ " '(")
4644 (prin1 symbol)
4645 (princ " ")
4646 (prin1 value)
4647 (when (or now comment)
4648 (princ " ")
4649 (prin1 now)
4650 (when comment
4651 (princ " ")
4652 (prin1 comment)))
4653 (princ ")"))))
4654 (if (bolp)
4655 (princ " "))
4656 (princ ")")
4657 (unless (looking-at "\n")
4658 (princ "\n")))))
4660 ;;; The Customize Menu.
4662 ;;; Menu support
4664 (defcustom custom-menu-nesting 2
4665 "Maximum nesting in custom menus."
4666 :type 'integer
4667 :group 'custom-menu)
4669 (defun custom-face-menu-create (_widget symbol)
4670 "Ignoring WIDGET, create a menu entry for customization face SYMBOL."
4671 (vector (custom-unlispify-menu-entry symbol)
4672 `(customize-face ',symbol)
4675 (defun custom-variable-menu-create (_widget symbol)
4676 "Ignoring WIDGET, create a menu entry for customization variable SYMBOL."
4677 (let ((type (get symbol 'custom-type)))
4678 (unless (listp type)
4679 (setq type (list type)))
4680 (if (and type (widget-get type :custom-menu))
4681 (widget-apply type :custom-menu symbol)
4682 (vector (custom-unlispify-menu-entry symbol)
4683 `(customize-variable ',symbol)
4684 t))))
4686 ;; Add checkboxes to boolean variable entries.
4687 (widget-put (get 'boolean 'widget-type)
4688 :custom-menu (lambda (_widget symbol)
4689 (vector (custom-unlispify-menu-entry symbol)
4690 `(customize-variable ',symbol)
4691 ':style 'toggle
4692 ':selected symbol)))
4694 (defun custom-group-menu-create (_widget symbol)
4695 "Ignoring WIDGET, create a menu entry for customization group SYMBOL."
4696 `( ,(custom-unlispify-menu-entry symbol t)
4697 :filter (lambda (&rest junk)
4698 (let* ((menu (custom-menu-create ',symbol)))
4699 (if (consp menu) (cdr menu) menu)))))
4701 ;;;###autoload
4702 (defun custom-menu-create (symbol)
4703 "Create menu for customization group SYMBOL.
4704 The menu is in a format applicable to `easy-menu-define'."
4705 (let* ((deactivate-mark nil)
4706 (item (vector (custom-unlispify-menu-entry symbol)
4707 `(customize-group ',symbol)
4708 t)))
4709 (if (and (or (not (boundp 'custom-menu-nesting))
4710 (>= custom-menu-nesting 0))
4711 (progn
4712 (custom-load-symbol symbol)
4713 (< (length (get symbol 'custom-group)) widget-menu-max-size)))
4714 (let ((custom-prefix-list (custom-prefix-add symbol
4715 custom-prefix-list))
4716 (members (custom-sort-items (get symbol 'custom-group)
4717 custom-menu-sort-alphabetically
4718 custom-menu-order-groups)))
4719 `(,(custom-unlispify-menu-entry symbol t)
4720 ,item
4721 "--"
4722 ,@(mapcar (lambda (entry)
4723 (widget-apply (if (listp (nth 1 entry))
4724 (nth 1 entry)
4725 (list (nth 1 entry)))
4726 :custom-menu (nth 0 entry)))
4727 members)))
4728 item)))
4730 ;;;###autoload
4731 (defun customize-menu-create (symbol &optional name)
4732 "Return a customize menu for customization group SYMBOL.
4733 If optional NAME is given, use that as the name of the menu.
4734 Otherwise the menu will be named `Customize'.
4735 The format is suitable for use with `easy-menu-define'."
4736 (unless name
4737 (setq name "Customize"))
4738 `(,name
4739 :filter (lambda (&rest junk)
4740 (let ((menu (custom-menu-create ',symbol)))
4741 (if (consp menu) (cdr menu) menu)))))
4743 ;;; Toolbar and menubar support
4745 (easy-menu-define
4746 Custom-mode-menu (list custom-mode-map custom-field-keymap)
4747 "Menu used in customization buffers."
4748 (nconc (list "Custom"
4749 (customize-menu-create 'customize))
4750 (mapcar (lambda (arg)
4751 (let ((tag (nth 0 arg))
4752 (command (nth 1 arg))
4753 (active (nth 2 arg))
4754 (help (nth 3 arg)))
4755 (vector tag command :active (eval active) :help help)))
4756 custom-commands)))
4758 (defvar tool-bar-map)
4760 ;;; `custom-tool-bar-map' used to be set up here. This will fail to
4761 ;;; DTRT when `display-graphic-p' returns nil during compilation. Hence
4762 ;;; we set this up lazily in `Custom-mode'.
4763 (defvar custom-tool-bar-map nil
4764 "Keymap for toolbar in Custom mode.")
4766 ;;; The Custom Mode.
4768 (defun Custom-no-edit (_pos &optional _event)
4769 "Invoke button at POS, or refuse to allow editing of Custom buffer."
4770 (interactive "@d")
4771 (error "You can't edit this part of the Custom buffer"))
4773 (defun Custom-newline (pos &optional event)
4774 "Invoke button at POS, or refuse to allow editing of Custom buffer."
4775 (interactive "@d")
4776 (let ((button (get-char-property pos 'button)))
4777 ;; If there is no button at point, then use the one at the start
4778 ;; of the line, if it is a custom-group-link (bug#2298).
4779 (or button
4780 (if (setq button (get-char-property (line-beginning-position) 'button))
4781 (or (eq (widget-type button) 'custom-group-link)
4782 (setq button nil))))
4783 (if button
4784 (widget-apply-action button event)
4785 (error "You can't edit this part of the Custom buffer"))))
4787 (defun Custom-goto-parent ()
4788 "Go to the parent group listed at the top of this buffer.
4789 If several parents are listed, go to the first of them."
4790 (interactive)
4791 (save-excursion
4792 (goto-char (point-min))
4793 (if (search-forward "\nParent groups: " nil t)
4794 (let* ((button (get-char-property (point) 'button))
4795 (parent (downcase (widget-get button :tag))))
4796 (customize-group parent)))))
4798 (defcustom Custom-mode-hook nil
4799 "Hook called when entering Custom mode."
4800 :type 'hook
4801 :group 'custom-buffer)
4803 (defun custom-state-buffer-message (widget)
4804 (if (eq (widget-get (widget-get widget :parent) :custom-state) 'modified)
4805 (message "To install your edits, invoke [State] and choose the Set operation")))
4807 (defun custom--initialize-widget-variables ()
4808 (set (make-local-variable 'widget-documentation-face) 'custom-documentation)
4809 (set (make-local-variable 'widget-button-face) custom-button)
4810 (set (make-local-variable 'widget-button-pressed-face) custom-button-pressed)
4811 (set (make-local-variable 'widget-mouse-face) custom-button-mouse)
4812 ;; We need this because of the "More" button on docstrings.
4813 ;; Otherwise clicking on "More" can push point offscreen, which
4814 ;; causes the window to recenter on point, which pushes the
4815 ;; newly-revealed docstring offscreen; which is annoying. -- cyd.
4816 (set (make-local-variable 'widget-button-click-moves-point) t)
4817 ;; When possible, use relief for buttons, not bracketing. This test
4818 ;; may not be optimal.
4819 (when custom-raised-buttons
4820 (set (make-local-variable 'widget-push-button-prefix) "")
4821 (set (make-local-variable 'widget-push-button-suffix) "")
4822 (set (make-local-variable 'widget-link-prefix) "")
4823 (set (make-local-variable 'widget-link-suffix) ""))
4824 (setq show-trailing-whitespace nil))
4826 (define-derived-mode Custom-mode nil "Custom"
4827 "Major mode for editing customization buffers.
4829 The following commands are available:
4831 \\<widget-keymap>\
4832 Move to next button, link or editable field. \\[widget-forward]
4833 Move to previous button, link or editable field. \\[widget-backward]
4834 \\<custom-field-keymap>\
4835 Complete content of editable text field. \\[widget-complete]
4836 \\<custom-mode-map>\
4837 Invoke button under the mouse pointer. \\[widget-button-click]
4838 Invoke button under point. \\[widget-button-press]
4839 Set all options from current text. \\[Custom-set]
4840 Make values in current text permanent. \\[Custom-save]
4841 Make text match actual option values. \\[Custom-reset-current]
4842 Reset options to permanent settings. \\[Custom-reset-saved]
4843 Erase customizations; set options
4844 and buffer text to the standard values. \\[Custom-reset-standard]
4846 Entry to this mode calls the value of `Custom-mode-hook'
4847 if that value is non-nil."
4848 (use-local-map custom-mode-map)
4849 (easy-menu-add Custom-mode-menu)
4850 (set (make-local-variable 'tool-bar-map)
4851 (or custom-tool-bar-map
4852 ;; Set up `custom-tool-bar-map'.
4853 (let ((map (make-sparse-keymap)))
4854 (mapc
4855 (lambda (arg)
4856 (tool-bar-local-item-from-menu
4857 (nth 1 arg) (nth 4 arg) map custom-mode-map
4858 :label (nth 5 arg)))
4859 custom-commands)
4860 (setq custom-tool-bar-map map))))
4861 (make-local-variable 'custom-options)
4862 (make-local-variable 'custom-local-buffer)
4863 (custom--initialize-widget-variables)
4864 (add-hook 'widget-edit-functions 'custom-state-buffer-message nil t))
4866 (put 'Custom-mode 'mode-class 'special)
4868 ;; backward-compatibility
4869 (defun custom-mode ()
4870 "Non-interactive variant of `Custom-mode'."
4871 (Custom-mode))
4872 (make-obsolete 'custom-mode 'Custom-mode "23.1")
4873 (put 'custom-mode 'mode-class 'special)
4874 (define-obsolete-variable-alias 'custom-mode-hook 'Custom-mode-hook "23.1")
4876 (dolist (regexp
4877 '("^No user option defaults have been changed since Emacs "
4878 "^Invalid face:? "
4879 "^No \\(?:customized\\|rogue\\|saved\\) user options"
4880 "^No customizable items matching "
4881 "^There are unset changes"
4882 "^Cannot set hidden variable"
4883 "^No \\(?:saved\\|backup\\) value for "
4884 "^No standard setting known for "
4885 "^No standard setting for this face"
4886 "^Saving settings from \"emacs -q\" would overwrite existing customizations"))
4887 (add-to-list 'debug-ignored-errors regexp))
4889 ;;; The End.
4891 (provide 'cus-edit)
4893 ;;; cus-edit.el ends here