custom-buffer-style doc fix
[emacs.git] / lisp / cus-edit.el
blobd7db3530099a8f6a4840f1475c795d054a38580e
1 ;;; cus-edit.el --- tools for customizing Emacs and Lisp packages -*- lexical-binding:t -*-
2 ;;
3 ;; Copyright (C) 1996-1997, 1999-2016 Free Software Foundation, Inc.
4 ;;
5 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
6 ;; Maintainer: emacs-devel@gnu.org
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 "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 "Reading and posting to newsgroups."
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 "Interfaces, assistants, and emulators for 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 Emacs 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 "Contents of the mode line."
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-command)
446 (define-key map [?\S-\ ] 'scroll-down-command)
447 (define-key map "\177" 'scroll-down-command)
448 (define-key map "\C-c\C-c" 'Custom-set)
449 (define-key map "\C-x\C-s" 'Custom-save)
450 (define-key map "q" 'Custom-buffer-done)
451 (define-key map "u" 'Custom-goto-parent)
452 (define-key map "n" 'widget-forward)
453 (define-key map "p" 'widget-backward)
454 map)
455 "Keymap for `Custom-mode'.")
457 (defvar custom-mode-link-map
458 (let ((map (make-keymap)))
459 (set-keymap-parent map custom-mode-map)
460 (define-key map [down-mouse-2] nil)
461 (define-key map [down-mouse-1] 'mouse-drag-region)
462 (define-key map [mouse-2] 'widget-move-and-invoke)
463 map)
464 "Local keymap for links in `Custom-mode'.")
466 (defvar custom-field-keymap
467 (let ((map (copy-keymap widget-field-keymap)))
468 (define-key map "\C-c\C-c" 'Custom-set)
469 (define-key map "\C-x\C-s" 'Custom-save)
470 map)
471 "Keymap used inside editable fields in customization buffers.")
473 (widget-put (get 'editable-field 'widget-type) :keymap custom-field-keymap)
475 ;;; Utilities.
477 (defun custom-split-regexp-maybe (regexp)
478 "If REGEXP is a string, split it to a list at `\\|'.
479 You can get the original back from the result with:
480 (mapconcat \\='identity result \"\\|\")
482 IF REGEXP is not a string, return it unchanged."
483 (if (stringp regexp)
484 (split-string regexp "\\\\|")
485 regexp))
487 (defun custom-variable-prompt ()
488 "Prompt for a custom variable, defaulting to the variable at point.
489 Return a list suitable for use in `interactive'."
490 (let* ((v (variable-at-point))
491 (default (and (symbolp v) (custom-variable-p v) (symbol-name v)))
492 (enable-recursive-minibuffers t)
493 val)
494 (setq val (completing-read
495 (if default (format "Customize variable (default %s): " default)
496 "Customize variable: ")
497 obarray 'custom-variable-p t nil nil default))
498 (list (if (equal val "")
499 (if (symbolp v) v nil)
500 (intern val)))))
502 (defun custom-menu-filter (menu widget)
503 "Convert MENU to the form used by `widget-choose'.
504 MENU should be in the same format as `custom-variable-menu'.
505 WIDGET is the widget to apply the filter entries of MENU on."
506 (let ((result nil)
507 current name action filter)
508 (while menu
509 (setq current (car menu)
510 name (nth 0 current)
511 action (nth 1 current)
512 filter (nth 2 current)
513 menu (cdr menu))
514 (if (or (null filter) (funcall filter widget))
515 (push (cons name action) result)
516 (push name result)))
517 (nreverse result)))
519 ;;; Unlispify.
521 (defvar custom-prefix-list nil
522 "List of prefixes that should be ignored by `custom-unlispify'.")
524 (defcustom custom-unlispify-menu-entries t
525 "Display menu entries as words instead of symbols if non-nil."
526 :group 'custom-menu
527 :type 'boolean)
529 (defcustom custom-unlispify-remove-prefixes nil
530 "Non-nil means remove group prefixes from option names in buffer.
531 Discarding prefixes often leads to confusing names for options
532 and faces in Customize buffers, so do not set this to a non-nil
533 value unless you are sure you know what it does."
534 :group 'custom-menu
535 :group 'custom-buffer
536 :type 'boolean)
538 (defun custom-unlispify-menu-entry (symbol &optional no-suffix)
539 "Convert SYMBOL into a menu entry."
540 (cond ((not custom-unlispify-menu-entries)
541 (symbol-name symbol))
542 ((get symbol 'custom-tag)
543 (if no-suffix
544 (get symbol 'custom-tag)
545 (concat (get symbol 'custom-tag) "...")))
547 (with-current-buffer (get-buffer-create " *Custom-Work*")
548 (erase-buffer)
549 (princ symbol (current-buffer))
550 (goto-char (point-min))
551 (if custom-unlispify-remove-prefixes
552 (let ((prefixes custom-prefix-list)
553 prefix)
554 (while prefixes
555 (setq prefix (car prefixes))
556 (if (search-forward prefix (+ (point) (length prefix)) t)
557 (progn
558 (setq prefixes nil)
559 (delete-region (point-min) (point)))
560 (setq prefixes (cdr prefixes))))))
561 (subst-char-in-region (point-min) (point-max) ?- ?\s t)
562 (capitalize-region (point-min) (point-max))
563 (unless no-suffix
564 (goto-char (point-max))
565 (insert "..."))
566 (buffer-string)))))
568 (defcustom custom-unlispify-tag-names t
569 "Display tag names as words instead of symbols if non-nil."
570 :group 'custom-buffer
571 :type 'boolean)
573 (defun custom-unlispify-tag-name (symbol)
574 "Convert SYMBOL into a menu entry."
575 (let ((custom-unlispify-menu-entries custom-unlispify-tag-names))
576 (custom-unlispify-menu-entry symbol t)))
578 (defun custom-prefix-add (symbol prefixes)
579 "Add SYMBOL to list of ignored PREFIXES."
580 (cons (or (get symbol 'custom-prefix)
581 (concat (symbol-name symbol) "-"))
582 prefixes))
584 ;;; Guess.
586 (defcustom custom-guess-name-alist
587 '(("-p\\'" boolean)
588 ("-flag\\'" boolean)
589 ("-hook\\'" hook)
590 ("-face\\'" face)
591 ("-file\\'" file)
592 ("-function\\'" function)
593 ("-functions\\'" (repeat function))
594 ("-list\\'" (repeat sexp))
595 ("-alist\\'" (alist :key-type sexp :value-type sexp)))
596 "Alist of (MATCH TYPE).
598 MATCH should be a regexp matching the name of a symbol, and TYPE should
599 be a widget suitable for editing the value of that symbol. The TYPE
600 of the first entry where MATCH matches the name of the symbol will be
601 used.
603 This is used for guessing the type of variables not declared with
604 customize."
605 :type '(repeat (group (regexp :tag "Match") (sexp :tag "Type")))
606 :group 'custom-buffer)
608 (defcustom custom-guess-doc-alist
609 '(("\\`\\*?Non-nil " boolean))
610 "Alist of (MATCH TYPE).
612 MATCH should be a regexp matching a documentation string, and TYPE
613 should be a widget suitable for editing the value of a variable with
614 that documentation string. The TYPE of the first entry where MATCH
615 matches the name of the symbol will be used.
617 This is used for guessing the type of variables not declared with
618 customize."
619 :type '(repeat (group (regexp :tag "Match") (sexp :tag "Type")))
620 :group 'custom-buffer)
622 (defun custom-guess-type (symbol)
623 "Guess a widget suitable for editing the value of SYMBOL.
624 This is done by matching SYMBOL with `custom-guess-name-alist' and
625 if that fails, the doc string with `custom-guess-doc-alist'."
626 (let ((name (symbol-name symbol))
627 (names custom-guess-name-alist)
628 current found)
629 (while names
630 (setq current (car names)
631 names (cdr names))
632 (when (string-match-p (nth 0 current) name)
633 (setq found (nth 1 current)
634 names nil)))
635 (unless found
636 (let ((doc (documentation-property symbol 'variable-documentation t))
637 (docs custom-guess-doc-alist))
638 (when doc
639 (while docs
640 (setq current (car docs)
641 docs (cdr docs))
642 (when (string-match-p (nth 0 current) doc)
643 (setq found (nth 1 current)
644 docs nil))))))
645 found))
647 ;;; Sorting.
649 ;;;###autoload
650 (defcustom custom-browse-sort-alphabetically nil
651 "If non-nil, sort customization group alphabetically in `custom-browse'."
652 :type 'boolean
653 :group 'custom-browse)
655 (defcustom custom-browse-order-groups nil
656 "If non-nil, order group members within each customization group.
657 If `first', order groups before non-groups.
658 If `last', order groups after non-groups."
659 :type '(choice (const first)
660 (const last)
661 (const :tag "none" nil))
662 :group 'custom-browse)
664 (defcustom custom-browse-only-groups nil
665 "If non-nil, show group members only within each customization group."
666 :type 'boolean
667 :group 'custom-browse)
669 ;;;###autoload
670 (defcustom custom-buffer-sort-alphabetically t
671 "Whether to sort customization groups alphabetically in Custom buffer."
672 :type 'boolean
673 :group 'custom-buffer
674 :version "24.1")
676 (defcustom custom-buffer-order-groups 'last
677 "If non-nil, order group members within each customization group.
678 If `first', order groups before non-groups.
679 If `last', order groups after non-groups."
680 :type '(choice (const first)
681 (const last)
682 (const :tag "none" nil))
683 :group 'custom-buffer)
685 ;;;###autoload
686 (defcustom custom-menu-sort-alphabetically nil
687 "If non-nil, sort each customization group alphabetically in menus."
688 :type 'boolean
689 :group 'custom-menu)
691 (defcustom custom-menu-order-groups 'first
692 "If non-nil, order group members within each customization group.
693 If `first', order groups before non-groups.
694 If `last', order groups after non-groups."
695 :type '(choice (const first)
696 (const last)
697 (const :tag "none" nil))
698 :group 'custom-menu)
700 (defun custom-sort-items (items sort-alphabetically order-groups)
701 "Return a sorted copy of ITEMS.
702 ITEMS should be a list of `custom-group' properties.
703 If SORT-ALPHABETICALLY non-nil, sort alphabetically.
704 If ORDER-GROUPS is `first' order groups before non-groups, if `last' order
705 groups after non-groups, if nil do not order groups at all."
706 (sort (copy-sequence items)
707 (lambda (a b)
708 (let ((typea (nth 1 a)) (typeb (nth 1 b))
709 (namea (nth 0 a)) (nameb (nth 0 b)))
710 (cond ((not order-groups)
711 ;; Since we don't care about A and B order, maybe sort.
712 (when sort-alphabetically
713 (string-lessp namea nameb)))
714 ((eq typea 'custom-group)
715 ;; If B is also a group, maybe sort. Otherwise, order A and B.
716 (if (eq typeb 'custom-group)
717 (when sort-alphabetically
718 (string-lessp namea nameb))
719 (eq order-groups 'first)))
720 ((eq typeb 'custom-group)
721 ;; Since A cannot be a group, order A and B.
722 (eq order-groups 'last))
723 (sort-alphabetically
724 ;; Since A and B cannot be groups, sort.
725 (string-lessp namea nameb)))))))
727 ;;; Custom Mode Commands.
729 ;; This variable is used by `custom-tool-bar-map', or directly by
730 ;; `custom-buffer-create-internal' if `custom-buffer-verbose-help' is non-nil.
732 (defvar custom-commands
733 '((" Apply " Custom-set t
734 "Apply settings (for the current session only)."
735 "index"
736 "Apply")
737 (" Apply and Save " Custom-save
738 (or custom-file user-init-file)
739 "Apply settings and save for future sessions."
740 "save"
741 "Save")
742 (" Undo Edits " Custom-reset-current t
743 "Restore customization buffer to reflect existing settings."
744 "refresh"
745 "Undo")
746 (" Reset Customizations " Custom-reset-saved t
747 "Undo any settings applied only for the current session."
748 "undo"
749 "Reset")
750 (" Erase Customizations " Custom-reset-standard
751 (or custom-file user-init-file)
752 "Un-customize settings in this and future sessions."
753 "delete"
754 "Uncustomize")
755 (" Help for Customize " Custom-help t
756 "Get help for using Customize."
757 "help"
758 "Help")
759 (" Exit " Custom-buffer-done t "Exit Customize." "exit" "Exit")))
761 (defun Custom-help ()
762 "Read the node on Easy Customization in the Emacs manual."
763 (interactive)
764 (info "(emacs)Easy Customization"))
766 (defvar custom-reset-menu
767 '(("Undo Edits in Customization Buffer" . Custom-reset-current)
768 ("Revert This Session's Customizations" . Custom-reset-saved)
769 ("Erase Customizations" . Custom-reset-standard))
770 "Alist of actions for the `Reset' button.
771 The key is a string containing the name of the action, the value is a
772 Lisp function taking the widget as an element which will be called
773 when the action is chosen.")
775 (defvar custom-options nil
776 "Customization widgets in the current buffer.")
778 (defun custom-command-apply (fun query &optional strong-query)
779 "Call function FUN on all widgets in `custom-options'.
780 If there is more than one widget, ask user for confirmation using
781 the query string QUERY, using `y-or-n-p' if STRONG-QUERY is nil,
782 and `yes-or-no-p' otherwise. Return non-nil if the functionality
783 has been executed, nil otherwise."
784 (if (or (and (= 1 (length custom-options))
785 (memq (widget-type (car custom-options))
786 '(custom-variable custom-face)))
787 (funcall (if strong-query 'yes-or-no-p 'y-or-n-p) query))
788 (progn (mapc fun custom-options) t)
789 (message "Aborted")
790 nil))
792 (defun Custom-set (&rest _ignore)
793 "Set the current value of all edited settings in the buffer."
794 (interactive)
795 (custom-command-apply
796 (lambda (child)
797 (when (eq (widget-get child :custom-state) 'modified)
798 (widget-apply child :custom-set)))
799 "Set all values according to this buffer? "))
801 (defun Custom-save (&rest _ignore)
802 "Set all edited settings, then save all settings that have been set.
803 If a setting was edited and set before, this saves it. If a
804 setting was merely edited before, this sets it then saves it."
805 (interactive)
806 (when (custom-command-apply
807 (lambda (child)
808 (when (memq (widget-get child :custom-state)
809 '(modified set changed rogue))
810 (widget-apply child :custom-mark-to-save)))
811 "Save all settings in this buffer? " t)
812 ;; Save changes to buffer and redraw.
813 (custom-save-all)
814 (dolist (child custom-options)
815 (widget-apply child :custom-state-set-and-redraw))))
817 (defun custom-reset (_widget &optional event)
818 "Select item from reset menu."
819 (let* ((completion-ignore-case t)
820 (answer (widget-choose "Reset settings"
821 custom-reset-menu
822 event)))
823 (if answer
824 (funcall answer))))
826 (defun Custom-reset-current (&rest _ignore)
827 "Reset all edited settings in the buffer to show their current values."
828 (interactive)
829 (custom-command-apply
830 (lambda (widget)
831 (if (memq (widget-get widget :custom-state) '(modified changed))
832 (widget-apply widget :custom-reset-current)))
833 "Reset all settings' buffer text to show current values? "))
835 (defun Custom-reset-saved (&rest _ignore)
836 "Reset all edited or set settings in the buffer to their saved value.
837 This also shows the saved values in the buffer."
838 (interactive)
839 (custom-command-apply
840 (lambda (widget)
841 (if (memq (widget-get widget :custom-state) '(modified set changed rogue))
842 (widget-apply widget :custom-reset-saved)))
843 "Reset all settings (current values and buffer text) to saved values? "))
845 ;; The next two variables are bound to '(t) by `Custom-reset-standard'
846 ;; and `custom-group-reset-standard'. If these variables are nil, both
847 ;; `custom-variable-reset-standard' and `custom-face-reset-standard'
848 ;; save, reset and redraw the handled widget immediately. Otherwise,
849 ;; they add the widget to the corresponding list and leave it to
850 ;; `custom-reset-standard-save-and-update' to save, reset and redraw it.
851 (defvar custom-reset-standard-variables-list nil)
852 (defvar custom-reset-standard-faces-list nil)
854 ;; The next function was excerpted from `custom-variable-reset-standard'
855 ;; and `custom-face-reset-standard' and is used to avoid calling
856 ;; `custom-save-all' repeatedly (and thus saving settings to file one by
857 ;; one) when erasing all customizations.
858 (defun custom-reset-standard-save-and-update ()
859 "Save settings and redraw after erasing customizations."
860 (when (or (and custom-reset-standard-variables-list
861 (not (eq custom-reset-standard-variables-list '(t))))
862 (and custom-reset-standard-faces-list
863 (not (eq custom-reset-standard-faces-list '(t)))))
864 ;; Save settings to file.
865 (custom-save-all)
866 ;; Set state of and redraw variables.
867 (dolist (widget custom-reset-standard-variables-list)
868 (unless (eq widget t)
869 (widget-put widget :custom-state 'unknown)
870 (custom-redraw widget)))
871 ;; Set state of and redraw faces.
872 (dolist (widget custom-reset-standard-faces-list)
873 (unless (eq widget t)
874 (let* ((symbol (widget-value widget))
875 (child (car (widget-get widget :children)))
876 (comment-widget (widget-get widget :comment-widget)))
877 (put symbol 'face-comment nil)
878 (widget-value-set child
879 (custom-pre-filter-face-spec
880 (list (list t (custom-face-attributes-get
881 symbol nil)))))
882 ;; This call manages the comment visibility
883 (widget-value-set comment-widget "")
884 (custom-face-state-set widget)
885 (custom-redraw-magic widget))))))
887 (defun Custom-reset-standard (&rest _ignore)
888 "Erase all customizations (either current or saved) in current buffer.
889 The immediate result is to restore them to their standard values.
890 This operation eliminates any saved values for the group members,
891 making them as if they had never been customized at all."
892 (interactive)
893 ;; Bind these temporarily.
894 (let ((custom-reset-standard-variables-list '(t))
895 (custom-reset-standard-faces-list '(t)))
896 (if (custom-command-apply
897 (lambda (widget)
898 (and (or (null (widget-get widget :custom-standard-value))
899 (widget-apply widget :custom-standard-value))
900 (memq (widget-get widget :custom-state)
901 '(modified set changed saved rogue))
902 (widget-apply widget :custom-mark-to-reset-standard)))
903 "The settings will revert to their default values, in this
904 and future sessions. Really erase customizations? " 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 (cond
1115 ((null (get-buffer name))
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))))
1123 (other-window
1124 (switch-to-buffer-other-window name))
1126 (pop-to-buffer-same-window name)))))
1128 ;;;###autoload
1129 (defun customize-group-other-window (&optional group)
1130 "Customize GROUP, which must be a customization group, in another window."
1131 (interactive (list (customize-read-group)))
1132 (customize-group group t))
1134 ;;;###autoload
1135 (defalias 'customize-variable 'customize-option)
1137 ;;;###autoload
1138 (defun customize-option (symbol)
1139 "Customize SYMBOL, which must be a user option."
1140 (interactive (custom-variable-prompt))
1141 (unless symbol
1142 (error "No variable specified"))
1143 (let ((basevar (indirect-variable symbol)))
1144 (custom-buffer-create (list (list basevar 'custom-variable))
1145 (format "*Customize Option: %s*"
1146 (custom-unlispify-tag-name basevar)))
1147 (unless (eq symbol basevar)
1148 (message "`%s' is an alias for `%s'" symbol basevar))))
1150 ;;;###autoload
1151 (defalias 'customize-variable-other-window 'customize-option-other-window)
1153 ;;;###autoload
1154 (defun customize-option-other-window (symbol)
1155 "Customize SYMBOL, which must be a user option.
1156 Show the buffer in another window, but don't select it."
1157 (interactive (custom-variable-prompt))
1158 (unless symbol
1159 (error "No variable specified"))
1160 (let ((basevar (indirect-variable symbol)))
1161 (custom-buffer-create-other-window
1162 (list (list basevar 'custom-variable))
1163 (format "*Customize Option: %s*" (custom-unlispify-tag-name basevar)))
1164 (unless (eq symbol basevar)
1165 (message "`%s' is an alias for `%s'" symbol basevar))))
1167 (defvar customize-changed-options-previous-release "24.1"
1168 "Version for `customize-changed-options' to refer back to by default.")
1170 ;; Packages will update this variable, so make it available.
1171 ;;;###autoload
1172 (defvar customize-package-emacs-version-alist nil
1173 "Alist mapping versions of a package to Emacs versions.
1174 We use this for packages that have their own names, but are released
1175 as part of Emacs itself.
1177 Each elements looks like this:
1179 (PACKAGE (PVERSION . EVERSION)...)
1181 Here PACKAGE is the name of a package, as a symbol. After
1182 PACKAGE come one or more elements, each associating a
1183 package version PVERSION with the first Emacs version
1184 EVERSION in which it (or a subsequent version of PACKAGE)
1185 was first released. Both PVERSION and EVERSION are strings.
1186 PVERSION should be a string that this package used in
1187 the :package-version keyword for `defcustom', `defgroup',
1188 and `defface'.
1190 For example, the MH-E package updates this alist as follows:
1192 (add-to-list \\='customize-package-emacs-version-alist
1193 \\='(MH-E (\"6.0\" . \"22.1\") (\"6.1\" . \"22.1\")
1194 (\"7.0\" . \"22.1\") (\"7.1\" . \"22.1\")
1195 (\"7.2\" . \"22.1\") (\"7.3\" . \"22.1\")
1196 (\"7.4\" . \"22.1\") (\"8.0\" . \"22.1\")))
1198 The value of PACKAGE needs to be unique and it needs to match the
1199 PACKAGE value appearing in the :package-version keyword. Since
1200 the user might see the value in a error message, a good choice is
1201 the official name of the package, such as MH-E or Gnus.")
1203 ;;;###autoload
1204 (defalias 'customize-changed 'customize-changed-options)
1206 ;;;###autoload
1207 (defun customize-changed-options (&optional since-version)
1208 "Customize all settings whose meanings have changed in Emacs itself.
1209 This includes new user options and faces, and new customization
1210 groups, as well as older options and faces whose meanings or
1211 default values have changed since the previous major Emacs
1212 release.
1214 With argument SINCE-VERSION (a string), customize all settings
1215 that were added or redefined since that version."
1217 (interactive
1218 (list
1219 (read-from-minibuffer
1220 (format "Customize options changed, since version (default %s): "
1221 customize-changed-options-previous-release))))
1222 (if (equal since-version "")
1223 (setq since-version nil)
1224 (unless (condition-case nil
1225 (numberp (read since-version))
1226 (error nil))
1227 (signal 'wrong-type-argument (list 'numberp since-version))))
1228 (unless since-version
1229 (setq since-version customize-changed-options-previous-release))
1231 ;; Load the information for versions since since-version. We use
1232 ;; custom-load-symbol for this.
1233 (put 'custom-versions-load-alist 'custom-loads nil)
1234 (dolist (elt custom-versions-load-alist)
1235 (if (customize-version-lessp since-version (car elt))
1236 (dolist (load (cdr elt))
1237 (custom-add-load 'custom-versions-load-alist load))))
1238 (custom-load-symbol 'custom-versions-load-alist)
1239 (put 'custom-versions-load-alist 'custom-loads nil)
1241 (let (found)
1242 (mapatoms
1243 (lambda (symbol)
1244 (let* ((package-version (get symbol 'custom-package-version))
1245 (version
1246 (or (and package-version
1247 (customize-package-emacs-version symbol
1248 package-version))
1249 (get symbol 'custom-version))))
1250 (if version
1251 (when (customize-version-lessp since-version version)
1252 (if (or (get symbol 'custom-group)
1253 (get symbol 'group-documentation))
1254 (push (list symbol 'custom-group) found))
1255 (if (custom-variable-p symbol)
1256 (push (list symbol 'custom-variable) found))
1257 (if (custom-facep symbol)
1258 (push (list symbol 'custom-face) found)))))))
1259 (if found
1260 (custom-buffer-create (custom-sort-items found t 'first)
1261 "*Customize Changed Options*")
1262 (user-error "No user option defaults have been changed since Emacs %s"
1263 since-version))))
1265 (defun customize-package-emacs-version (symbol package-version)
1266 "Return the Emacs version in which SYMBOL's meaning last changed.
1267 PACKAGE-VERSION has the form (PACKAGE . VERSION). We use
1268 `customize-package-emacs-version-alist' to find the version of
1269 Emacs that is associated with version VERSION of PACKAGE."
1270 (let (package-versions emacs-version)
1271 ;; Use message instead of error since we want user to be able to
1272 ;; see the rest of the symbols even if a package author has
1273 ;; botched things up.
1274 (cond ((not (listp package-version))
1275 (message "Invalid package-version value for %s" symbol))
1276 ((setq package-versions (assq (car package-version)
1277 customize-package-emacs-version-alist))
1278 (setq emacs-version
1279 (cdr (assoc (cdr package-version) package-versions)))
1280 (unless emacs-version
1281 (message "%s version %s not found in %s" symbol
1282 (cdr package-version)
1283 "customize-package-emacs-version-alist")))
1285 (message "Package %s version %s lists no corresponding Emacs version"
1286 (car package-version)
1287 (cdr package-version))))
1288 emacs-version))
1290 (defun customize-version-lessp (version1 version2)
1291 ;; Why are the versions strings, and given that they are, why aren't
1292 ;; they converted to numbers and compared as such here? -- fx
1294 ;; In case someone made a mistake and left out the quotes
1295 ;; in the :version value.
1296 (if (numberp version2)
1297 (setq version2 (prin1-to-string version2)))
1298 (let (major1 major2 minor1 minor2)
1299 (string-match "\\([0-9]+\\)\\(\\.\\([0-9]+\\)\\)?" version1)
1300 (setq major1 (read (or (match-string 1 version1)
1301 "0")))
1302 (setq minor1 (read (or (match-string 3 version1)
1303 "0")))
1304 (string-match "\\([0-9]+\\)\\(\\.\\([0-9]+\\)\\)?" version2)
1305 (setq major2 (read (or (match-string 1 version2)
1306 "0")))
1307 (setq minor2 (read (or (match-string 3 version2)
1308 "0")))
1309 (or (< major1 major2)
1310 (and (= major1 major2)
1311 (< minor1 minor2)))))
1313 ;;;###autoload
1314 (defun customize-face (&optional face other-window)
1315 "Customize FACE, which should be a face name or nil.
1316 If FACE is nil, customize all faces. If FACE is actually a
1317 face-alias, customize the face it is aliased to.
1319 If OTHER-WINDOW is non-nil, display in another window.
1321 Interactively, when point is on text which has a face specified,
1322 suggest to customize that face, if it's customizable."
1323 (interactive (list (read-face-name "Customize face"
1324 (or (face-at-point t t) "all faces") t)))
1325 (if (member face '(nil ""))
1326 (setq face (face-list)))
1327 (if (and (listp face) (null (cdr face)))
1328 (setq face (car face)))
1329 (let ((display-fun (if other-window
1330 'custom-buffer-create-other-window
1331 'custom-buffer-create)))
1332 (if (listp face)
1333 (funcall display-fun
1334 (custom-sort-items
1335 (mapcar (lambda (s) (list s 'custom-face)) face)
1336 t nil)
1337 "*Customize Faces*")
1338 ;; If FACE is actually an alias, customize the face it is aliased to.
1339 (if (get face 'face-alias)
1340 (setq face (get face 'face-alias)))
1341 (unless (facep face)
1342 (error "Invalid face %S" face))
1343 (funcall display-fun
1344 (list (list face 'custom-face))
1345 (format "*Customize Face: %s*"
1346 (custom-unlispify-tag-name face))))))
1348 ;;;###autoload
1349 (defun customize-face-other-window (&optional face)
1350 "Show customization buffer for face FACE in other window.
1351 If FACE is actually a face-alias, customize the face it is aliased to.
1353 Interactively, when point is on text which has a face specified,
1354 suggest to customize that face, if it's customizable."
1355 (interactive (list (read-face-name "Customize face"
1356 (or (face-at-point t t) "all faces") t)))
1357 (customize-face face t))
1359 (defun custom-unsaved-options ()
1360 "List of options and faces set in this session but not saved.
1361 Each entry is of the form (SYMBOL TYPE), where TYPE is one of the
1362 symbols `custom-face' or `custom-variable'."
1363 (let ((found nil))
1364 (mapatoms (lambda (symbol)
1365 (and (or (get symbol 'customized-face)
1366 (get symbol 'customized-face-comment))
1367 (custom-facep symbol)
1368 (push (list symbol 'custom-face) found))
1369 (and (or (get symbol 'customized-value)
1370 (get symbol 'customized-variable-comment))
1371 (boundp symbol)
1372 (push (list symbol 'custom-variable) found))))
1373 found))
1375 (defalias 'customize-customized 'customize-unsaved)
1377 ;;;###autoload
1378 (defun customize-unsaved ()
1379 "Customize all options and faces set in this session but not saved."
1380 (interactive)
1381 (let ((found (custom-unsaved-options)))
1382 (if (not found)
1383 (error "No user options are set but unsaved")
1384 (custom-buffer-create (custom-sort-items found t nil)
1385 "*Customize Unsaved*"))))
1387 ;;;###autoload
1388 (defun customize-rogue ()
1389 "Customize all user variables modified outside customize."
1390 (interactive)
1391 (let ((found nil))
1392 (mapatoms (lambda (symbol)
1393 (let ((cval (or (get symbol 'customized-value)
1394 (get symbol 'saved-value)
1395 (get symbol 'standard-value))))
1396 (when (and cval ;Declared with defcustom.
1397 (default-boundp symbol) ;Has a value.
1398 (not (equal (eval (car cval))
1399 ;; Which does not match customize.
1400 (default-value symbol))))
1401 (push (list symbol 'custom-variable) found)))))
1402 (if (not found)
1403 (user-error "No rogue user options")
1404 (custom-buffer-create (custom-sort-items found t nil)
1405 "*Customize Rogue*"))))
1406 ;;;###autoload
1407 (defun customize-saved ()
1408 "Customize all saved options and faces."
1409 (interactive)
1410 (let ((found nil))
1411 (mapatoms (lambda (symbol)
1412 (and (or (get symbol 'saved-face)
1413 (get symbol 'saved-face-comment))
1414 (custom-facep symbol)
1415 (push (list symbol 'custom-face) found))
1416 (and (or (get symbol 'saved-value)
1417 (get symbol 'saved-variable-comment))
1418 (boundp symbol)
1419 (push (list symbol 'custom-variable) found))))
1420 (if (not found)
1421 (user-error "No saved user options")
1422 (custom-buffer-create (custom-sort-items found t nil)
1423 "*Customize Saved*"))))
1425 (declare-function apropos-parse-pattern "apropos" (pattern))
1426 (defvar apropos-regexp)
1428 ;;;###autoload
1429 (defun customize-apropos (pattern &optional type)
1430 "Customize loaded options, faces and groups matching PATTERN.
1431 PATTERN can be a word, a list of words (separated by spaces),
1432 or a regexp (using some regexp special characters). If it is a word,
1433 search for matches for that word as a substring. If it is a list of
1434 words, search for matches for any two (or more) of those words.
1436 If TYPE is `options', include only options.
1437 If TYPE is `faces', include only faces.
1438 If TYPE is `groups', include only groups."
1439 (interactive (list (apropos-read-pattern "symbol") nil))
1440 (require 'apropos)
1441 (unless (memq type '(nil options faces groups))
1442 (error "Invalid setting type %s" (symbol-name type)))
1443 (apropos-parse-pattern pattern) ;Sets apropos-regexp by side-effect: Yuck!
1444 (let (found)
1445 (mapatoms
1446 (lambda (symbol)
1447 (when (string-match-p apropos-regexp (symbol-name symbol))
1448 (if (memq type '(nil groups))
1449 (if (get symbol 'custom-group)
1450 (push (list symbol 'custom-group) found)))
1451 (if (memq type '(nil faces))
1452 (if (custom-facep symbol)
1453 (push (list symbol 'custom-face) found)))
1454 (if (memq type '(nil options))
1455 (if (and (boundp symbol)
1456 (eq (indirect-variable symbol) symbol)
1457 (or (get symbol 'saved-value)
1458 (custom-variable-p symbol)))
1459 (push (list symbol 'custom-variable) found))))))
1460 (unless found
1461 (error "No customizable %s matching %s" (if (not type)
1462 "group, face, or option"
1463 (symbol-name type))
1464 pattern))
1465 (custom-buffer-create
1466 (custom-sort-items found t custom-buffer-order-groups)
1467 "*Customize Apropos*")))
1469 ;;;###autoload
1470 (defun customize-apropos-options (regexp &optional ignored)
1471 "Customize all loaded customizable options matching REGEXP."
1472 (interactive (list (apropos-read-pattern "options")))
1473 (customize-apropos regexp 'options))
1475 ;;;###autoload
1476 (defun customize-apropos-faces (regexp)
1477 "Customize all loaded faces matching REGEXP."
1478 (interactive (list (apropos-read-pattern "faces")))
1479 (customize-apropos regexp 'faces))
1481 ;;;###autoload
1482 (defun customize-apropos-groups (regexp)
1483 "Customize all loaded groups matching REGEXP."
1484 (interactive (list (apropos-read-pattern "groups")))
1485 (customize-apropos regexp 'groups))
1487 ;;;###autoload
1488 (defun custom-prompt-customize-unsaved-options ()
1489 "Prompt user to customize any unsaved customization options.
1490 Return non-nil if user chooses to customize, for use in
1491 `kill-emacs-query-functions'."
1492 (not (and (custom-unsaved-options)
1493 (yes-or-no-p "Some customized options have not been saved; Examine? ")
1494 (customize-unsaved)
1495 t)))
1497 ;;; Buffer.
1499 (defcustom custom-buffer-style 'links
1500 "Control the presentation style for customization buffers.
1501 The value should be a symbol, one of:
1502 `brackets': groups nest within each other with big horizontal brackets.
1503 `links': groups have links to subgroups.
1504 `tree': display groups as trees."
1505 :type '(radio (const brackets)
1506 (const links)
1507 (const tree))
1508 :group 'custom-buffer)
1510 (defcustom custom-buffer-done-kill nil
1511 "Non-nil means exiting a Custom buffer should kill it."
1512 :type 'boolean
1513 :version "22.1"
1514 :group 'custom-buffer)
1516 (defcustom custom-buffer-indent 3
1517 "Number of spaces to indent nested groups."
1518 :type 'integer
1519 :group 'custom-buffer)
1521 (defun custom-get-fresh-buffer (name)
1522 "Get a fresh new buffer with name NAME.
1523 If the buffer already exist, clean it up to be like new.
1524 Beware: it's not quite like new. Good enough for custom, but maybe
1525 not for everybody."
1526 ;; To be more complete, we should also kill all permanent-local variables,
1527 ;; but it's not needed for custom.
1528 (let ((buf (get-buffer name)))
1529 (when (and buf (buffer-local-value 'buffer-file-name buf))
1530 ;; This will check if the file is not saved.
1531 (kill-buffer buf)
1532 (setq buf nil))
1533 (if (null buf)
1534 (get-buffer-create name)
1535 (with-current-buffer buf
1536 (kill-all-local-variables)
1537 (run-hooks 'kill-buffer-hook)
1538 ;; Delete overlays before erasing the buffer so the overlay hooks
1539 ;; don't get run spuriously when we erase the buffer.
1540 (let ((ols (overlay-lists)))
1541 (dolist (ol (nconc (car ols) (cdr ols)))
1542 (delete-overlay ol)))
1543 (erase-buffer)
1544 buf))))
1546 ;;;###autoload
1547 (defun custom-buffer-create (options &optional name description)
1548 "Create a buffer containing OPTIONS.
1549 Optional NAME is the name of the buffer.
1550 OPTIONS should be an alist of the form ((SYMBOL WIDGET)...), where
1551 SYMBOL is a customization option, and WIDGET is a widget for editing
1552 that option.
1553 DESCRIPTION is unused."
1554 (pop-to-buffer-same-window (custom-get-fresh-buffer (or name "*Customization*")))
1555 (custom-buffer-create-internal options description))
1557 ;;;###autoload
1558 (defun custom-buffer-create-other-window (options &optional name description)
1559 "Create a buffer containing OPTIONS, and display it in another window.
1560 The result includes selecting that window.
1561 Optional NAME is the name of the buffer.
1562 OPTIONS should be an alist of the form ((SYMBOL WIDGET)...), where
1563 SYMBOL is a customization option, and WIDGET is a widget for editing
1564 that option."
1565 (unless name (setq name "*Customization*"))
1566 (switch-to-buffer-other-window (custom-get-fresh-buffer name))
1567 (custom-buffer-create-internal options description))
1569 (defcustom custom-reset-button-menu t
1570 "If non-nil, only show a single reset button in customize buffers.
1571 This button will have a menu with all three reset operations."
1572 :type 'boolean
1573 :group 'custom-buffer
1574 :version "24.3")
1576 (defcustom custom-buffer-verbose-help t
1577 "If non-nil, include explanatory text in the customization buffer."
1578 :type 'boolean
1579 :group 'custom-buffer)
1581 (defun Custom-buffer-done (&rest _ignore)
1582 "Exit current Custom buffer according to `custom-buffer-done-kill'."
1583 (interactive)
1584 (quit-window custom-buffer-done-kill))
1586 (defvar custom-button nil
1587 "Face used for buttons in customization buffers.")
1589 (defvar custom-button-mouse nil
1590 "Mouse face used for buttons in customization buffers.")
1592 (defvar custom-button-pressed nil
1593 "Face used for pressed buttons in customization buffers.")
1595 (defcustom custom-search-field t
1596 "If non-nil, show a search field in Custom buffers."
1597 :type 'boolean
1598 :version "24.1"
1599 :group 'custom-buffer)
1601 (defcustom custom-raised-buttons (not (equal (face-valid-attribute-values :box)
1602 '(("unspecified" . unspecified))))
1603 "If non-nil, indicate active buttons in a raised-button style.
1604 Otherwise use brackets."
1605 :type 'boolean
1606 :version "21.1"
1607 :group 'custom-buffer
1608 :set (lambda (variable value)
1609 (custom-set-default variable value)
1610 (setq custom-button
1611 (if value 'custom-button 'custom-button-unraised))
1612 (setq custom-button-mouse
1613 (if value 'custom-button-mouse 'highlight))
1614 (setq custom-button-pressed
1615 (if value
1616 'custom-button-pressed
1617 'custom-button-pressed-unraised))))
1619 (defun custom-buffer-create-internal (options &optional _description)
1620 (Custom-mode)
1621 (let ((init-file (or custom-file user-init-file)))
1622 ;; Insert verbose help at the top of the custom buffer.
1623 (when custom-buffer-verbose-help
1624 (unless init-file
1625 (widget-insert "Custom settings cannot be saved; maybe you started Emacs with `-q'.\n"))
1626 (widget-insert "For help using this buffer, see ")
1627 (widget-create 'custom-manual
1628 :tag "Easy Customization"
1629 "(emacs)Easy Customization")
1630 (widget-insert " in the ")
1631 (widget-create 'custom-manual
1632 :tag "Emacs manual"
1633 :help-echo "Read the Emacs manual."
1634 "(emacs)Top")
1635 (widget-insert "."))
1636 (widget-insert "\n")
1638 ;; Insert the search field.
1639 (when custom-search-field
1640 (widget-insert "\n")
1641 (let* ((echo "Search for custom items.
1642 You can enter one or more words separated by spaces,
1643 or a regular expression.")
1644 (search-widget
1645 (widget-create
1646 'editable-field
1647 :size 40 :help-echo echo
1648 :action (lambda (widget &optional _event)
1649 (customize-apropos (split-string (widget-value widget)))))))
1650 (widget-insert " ")
1651 (widget-create-child-and-convert
1652 search-widget 'push-button
1653 :tag " Search "
1654 :help-echo echo :action
1655 (lambda (widget &optional _event)
1656 (customize-apropos (split-string (widget-value (widget-get widget :parent))))))
1657 (widget-insert "\n")))
1659 ;; The custom command buttons are also in the toolbar, so for a
1660 ;; time they were not inserted in the buffer if the toolbar was in use.
1661 ;; But it can be a little confusing for the buffer layout to
1662 ;; change according to whether or nor the toolbar is on, not to
1663 ;; mention that a custom buffer can in theory be created in a
1664 ;; frame with a toolbar, then later viewed in one without.
1665 ;; So now the buttons are always inserted in the buffer. (Bug#1326)
1666 (if custom-buffer-verbose-help
1667 (widget-insert "
1668 Operate on all settings in this buffer:\n"))
1669 (let ((button (lambda (tag action active help _icon _label)
1670 (widget-insert " ")
1671 (if (eval active)
1672 (widget-create 'push-button :tag tag
1673 :help-echo help :action action))))
1674 (commands custom-commands))
1675 (if custom-reset-button-menu
1676 (progn
1677 (widget-create 'push-button
1678 :tag " Revert... "
1679 :help-echo "Show a menu with reset operations."
1680 :mouse-down-action 'ignore
1681 :action 'custom-reset)
1682 (apply button (pop commands)) ; Apply
1683 (apply button (pop commands))) ; Apply and Save
1684 (apply button (pop commands)) ; Apply
1685 (apply button (pop commands)) ; Apply and Save
1686 (widget-insert "\n")
1687 (apply button (pop commands)) ; Undo
1688 (apply button (pop commands)) ; Reset
1689 (apply button (pop commands)) ; Erase
1690 (widget-insert " ")
1691 (pop commands) ; Help (omitted)
1692 (apply button (pop commands)))) ; Exit
1693 (widget-insert "\n\n"))
1695 ;; Now populate the custom buffer.
1696 (message "Creating customization items...")
1697 (buffer-disable-undo)
1698 (setq custom-options
1699 (if (= (length options) 1)
1700 (mapcar (lambda (entry)
1701 (widget-create (nth 1 entry)
1702 :documentation-shown t
1703 :custom-state 'unknown
1704 :tag (custom-unlispify-tag-name
1705 (nth 0 entry))
1706 :value (nth 0 entry)))
1707 options)
1708 (let ((count 0)
1709 (length (length options)))
1710 (mapcar (lambda (entry)
1711 (prog2
1712 (message "Creating customization items ...%2d%%"
1713 (floor (* 100.0 count) length))
1714 (widget-create (nth 1 entry)
1715 :tag (custom-unlispify-tag-name
1716 (nth 0 entry))
1717 :value (nth 0 entry))
1718 (setq count (1+ count))
1719 (unless (eq (preceding-char) ?\n)
1720 (widget-insert "\n"))
1721 (widget-insert "\n")))
1722 options))))
1723 (unless (eq (preceding-char) ?\n)
1724 (widget-insert "\n"))
1725 (message "Creating customization items ...done")
1726 (message "Resetting customization items...")
1727 (unless (eq custom-buffer-style 'tree)
1728 (mapc 'custom-magic-reset custom-options))
1729 (message "Resetting customization items...done")
1730 (message "Creating customization setup...")
1731 (widget-setup)
1732 (buffer-enable-undo)
1733 (goto-char (point-min))
1734 (message "Creating customization setup...done"))
1736 ;;; The Tree Browser.
1738 ;;;###autoload
1739 (defun customize-browse (&optional group)
1740 "Create a tree browser for the customize hierarchy."
1741 (interactive)
1742 (unless group
1743 (setq group 'emacs))
1744 (let ((name "*Customize Browser*"))
1745 (pop-to-buffer-same-window (custom-get-fresh-buffer name)))
1746 (Custom-mode)
1747 (widget-insert (format "\
1748 %s buttons; type RET or click mouse-1
1749 on a button to invoke its action.
1750 Invoke [+] to expand a group, and [-] to collapse an expanded group.\n"
1751 (if custom-raised-buttons
1752 "Raised text indicates"
1753 "Square brackets indicate")))
1756 (if custom-browse-only-groups
1757 (widget-insert "\
1758 Invoke the [Group] button below to edit that item in another window.\n\n")
1759 (widget-insert "Invoke the ")
1760 (widget-create 'item
1761 :format "%t"
1762 :tag "[Group]"
1763 :tag-glyph "folder")
1764 (widget-insert ", ")
1765 (widget-create 'item
1766 :format "%t"
1767 :tag "[Face]"
1768 :tag-glyph "face")
1769 (widget-insert ", and ")
1770 (widget-create 'item
1771 :format "%t"
1772 :tag "[Option]"
1773 :tag-glyph "option")
1774 (widget-insert " buttons below to edit that
1775 item in another window.\n\n"))
1776 (let ((custom-buffer-style 'tree))
1777 (widget-create 'custom-group
1778 :custom-last t
1779 :custom-state 'unknown
1780 :tag (custom-unlispify-tag-name group)
1781 :value group))
1782 (widget-setup)
1783 (goto-char (point-min)))
1785 (define-widget 'custom-browse-visibility 'item
1786 "Control visibility of items in the customize tree browser."
1787 :format "%[[%t]%]"
1788 :action 'custom-browse-visibility-action)
1790 (defun custom-browse-visibility-action (widget &rest _ignore)
1791 (let ((custom-buffer-style 'tree))
1792 (custom-toggle-parent widget)))
1794 (define-widget 'custom-browse-group-tag 'custom-group-link
1795 "Show parent in other window when activated."
1796 :tag "Group"
1797 :tag-glyph "folder"
1798 :action 'custom-browse-group-tag-action)
1800 (defun custom-browse-group-tag-action (widget &rest _ignore)
1801 (let ((parent (widget-get widget :parent)))
1802 (customize-group-other-window (widget-value parent))))
1804 (define-widget 'custom-browse-variable-tag 'custom-group-link
1805 "Show parent in other window when activated."
1806 :tag "Option"
1807 :tag-glyph "option"
1808 :action 'custom-browse-variable-tag-action)
1810 (defun custom-browse-variable-tag-action (widget &rest _ignore)
1811 (let ((parent (widget-get widget :parent)))
1812 (customize-variable-other-window (widget-value parent))))
1814 (define-widget 'custom-browse-face-tag 'custom-group-link
1815 "Show parent in other window when activated."
1816 :tag "Face"
1817 :tag-glyph "face"
1818 :action 'custom-browse-face-tag-action)
1820 (defun custom-browse-face-tag-action (widget &rest _ignore)
1821 (let ((parent (widget-get widget :parent)))
1822 (customize-face-other-window (widget-value parent))))
1824 (defconst custom-browse-alist '((" " "space")
1825 (" | " "vertical")
1826 ("-\\ " "top")
1827 (" |-" "middle")
1828 (" `-" "bottom")))
1830 (defun custom-browse-insert-prefix (prefix)
1831 "Insert PREFIX. On XEmacs convert it to line graphics."
1832 ;; Fixme: do graphics.
1833 (if nil ; (featurep 'xemacs)
1834 (progn
1835 (insert "*")
1836 (while (not (string-equal prefix ""))
1837 (let ((entry (substring prefix 0 3)))
1838 (setq prefix (substring prefix 3))
1839 (let ((overlay (make-overlay (1- (point)) (point) nil t nil))
1840 (name (nth 1 (assoc entry custom-browse-alist))))
1841 (overlay-put overlay 'end-glyph (widget-glyph-find name entry))
1842 (overlay-put overlay 'start-open t)
1843 (overlay-put overlay 'end-open t)))))
1844 (insert prefix)))
1846 ;;; Modification of Basic Widgets.
1848 ;; We add extra properties to the basic widgets needed here. This is
1849 ;; fine, as long as we are careful to stay within our own namespace.
1851 ;; We want simple widgets to be displayed by default, but complex
1852 ;; widgets to be hidden.
1854 ;; This widget type is obsolete as of Emacs 24.1.
1855 (widget-put (get 'item 'widget-type) :custom-show t)
1856 (widget-put (get 'editable-field 'widget-type)
1857 :custom-show (lambda (_widget value)
1858 (let ((pp (pp-to-string value)))
1859 (cond ((string-match-p "\n" pp)
1860 nil)
1861 ((> (length pp) 40)
1862 nil)
1863 (t t)))))
1864 (widget-put (get 'menu-choice 'widget-type) :custom-show t)
1866 ;;; The `custom-manual' Widget.
1868 (define-widget 'custom-manual 'info-link
1869 "Link to the manual entry for this customization option."
1870 :help-echo "Read the manual entry for this option."
1871 :keymap custom-mode-link-map
1872 :follow-link 'mouse-face
1873 :button-face 'custom-link
1874 :mouse-face 'highlight
1875 :pressed-face 'highlight
1876 :tag "Manual")
1878 ;;; The `custom-magic' Widget.
1880 (defgroup custom-magic-faces nil
1881 "Faces used by the magic button."
1882 :group 'custom-faces
1883 :group 'custom-buffer)
1885 (defface custom-invalid '((((class color))
1886 :foreground "yellow1" :background "red1")
1887 (t :weight bold :slant italic :underline t))
1888 "Face used when the customize item is invalid."
1889 :group 'custom-magic-faces)
1891 (defface custom-rogue '((((class color))
1892 :foreground "pink" :background "black")
1893 (t :underline t))
1894 "Face used when the customize item is not defined for customization."
1895 :group 'custom-magic-faces)
1897 (defface custom-modified '((((min-colors 88) (class color))
1898 :foreground "white" :background "blue1")
1899 (((class color))
1900 :foreground "white" :background "blue")
1901 (t :slant italic))
1902 "Face used when the customize item has been modified."
1903 :group 'custom-magic-faces)
1905 (defface custom-set '((((min-colors 88) (class color))
1906 :foreground "blue1" :background "white")
1907 (((class color))
1908 :foreground "blue" :background "white")
1909 (t :slant italic))
1910 "Face used when the customize item has been set."
1911 :group 'custom-magic-faces)
1913 (defface custom-changed '((((min-colors 88) (class color))
1914 :foreground "white" :background "blue1")
1915 (((class color))
1916 :foreground "white" :background "blue")
1917 (t :slant italic))
1918 "Face used when the customize item has been changed."
1919 :group 'custom-magic-faces)
1921 (defface custom-themed '((((min-colors 88) (class color))
1922 :foreground "white" :background "blue1")
1923 (((class color))
1924 :foreground "white" :background "blue")
1925 (t :slant italic))
1926 "Face used when the customize item has been set by a theme."
1927 :group 'custom-magic-faces)
1929 (defface custom-saved '((t :underline t))
1930 "Face used when the customize item has been saved."
1931 :group 'custom-magic-faces)
1933 (defconst custom-magic-alist
1934 '((nil "#" underline "\
1935 UNINITIALIZED, you should not see this.")
1936 (unknown "?" italic "\
1937 UNKNOWN, you should not see this.")
1938 (hidden "-" default "\
1939 HIDDEN, invoke \"Show\" in the previous line to show." "\
1940 group now hidden, invoke \"Show\", above, to show contents.")
1941 (invalid "x" custom-invalid "\
1942 INVALID, the displayed value cannot be set.")
1943 (modified "*" custom-modified "\
1944 EDITED, shown value does not take effect until you set or save it." "\
1945 something in this group has been edited but not set.")
1946 (set "+" custom-set "\
1947 SET for current session only." "\
1948 something in this group has been set but not saved.")
1949 (changed ":" custom-changed "\
1950 CHANGED outside Customize." "\
1951 something in this group has been changed outside customize.")
1952 (saved "!" custom-saved "\
1953 SAVED and set." "\
1954 something in this group has been set and saved.")
1955 (themed "o" custom-themed "\
1956 THEMED." "\
1957 visible group members are set by enabled themes.")
1958 (rogue "@" custom-rogue "\
1959 NO CUSTOMIZATION DATA; not intended to be customized." "\
1960 something in this group is not prepared for customization.")
1961 (standard " " nil "\
1962 STANDARD." "\
1963 visible group members are all at standard values."))
1964 "Alist of customize option states.
1965 Each entry is of the form (STATE MAGIC FACE ITEM-DESC [ GROUP-DESC ]), where
1967 STATE is one of the following symbols:
1970 For internal use, should never occur.
1971 `unknown'
1972 For internal use, should never occur.
1973 `hidden'
1974 This item is not being displayed.
1975 `invalid'
1976 This item is modified, but has an invalid form.
1977 `modified'
1978 This item is modified, and has a valid form.
1979 `set'
1980 This item has been set but not saved.
1981 `changed'
1982 The current value of this item has been changed outside Customize.
1983 `saved'
1984 This item is marked for saving.
1985 `rogue'
1986 This item has no customization information.
1987 `themed'
1988 This item was set by an enabled Custom theme.
1989 `standard'
1990 This item is unchanged from the standard setting.
1992 MAGIC is a string used to present that state.
1994 FACE is a face used to present the state.
1996 ITEM-DESC is a string describing the state for options.
1998 GROUP-DESC is a string describing the state for groups. If this is
1999 left out, ITEM-DESC will be used.
2001 The string %c in either description will be replaced with the
2002 category of the item. These are `group', `option', and `face'.
2004 The list should be sorted most significant first.")
2006 (defcustom custom-magic-show 'long
2007 "If non-nil, show textual description of the state.
2008 If `long', show a full-line description, not just one word."
2009 :type '(choice (const :tag "no" nil)
2010 (const long)
2011 (other :tag "short" short))
2012 :group 'custom-buffer)
2014 (defcustom custom-magic-show-hidden '(option face)
2015 "Control whether the State button is shown for hidden items.
2016 The value should be a list with the custom categories where the State
2017 button should be visible. Possible categories are `group', `option',
2018 and `face'."
2019 :type '(set (const group) (const option) (const face))
2020 :group 'custom-buffer)
2022 (defcustom custom-magic-show-button nil
2023 "Show a \"magic\" button indicating the state of each customization option."
2024 :type 'boolean
2025 :group 'custom-buffer)
2027 (define-widget 'custom-magic 'default
2028 "Show and manipulate state for a customization option."
2029 :format "%v"
2030 :action 'widget-parent-action
2031 :notify 'ignore
2032 :value-get 'ignore
2033 :value-create 'custom-magic-value-create
2034 :value-delete 'widget-children-value-delete)
2036 (defun widget-magic-mouse-down-action (widget &optional _event)
2037 ;; Non-nil unless hidden.
2038 (not (eq (widget-get (widget-get (widget-get widget :parent) :parent)
2039 :custom-state)
2040 'hidden)))
2042 (defun custom-magic-value-create (widget)
2043 "Create compact status report for WIDGET."
2044 (let* ((parent (widget-get widget :parent))
2045 (state (widget-get parent :custom-state))
2046 (hidden (eq state 'hidden))
2047 (entry (assq state custom-magic-alist))
2048 (magic (nth 1 entry))
2049 (face (nth 2 entry))
2050 (category (widget-get parent :custom-category))
2051 (text (or (and (eq category 'group)
2052 (nth 4 entry))
2053 (nth 3 entry)))
2054 (form (widget-get parent :custom-form))
2055 children)
2056 (unless (eq state 'hidden)
2057 (while (string-match "\\`\\(.*\\)%c\\(.*\\)\\'" text)
2058 (setq text (concat (match-string 1 text)
2059 (symbol-name category)
2060 (match-string 2 text))))
2061 (when (and custom-magic-show
2062 (or (not hidden)
2063 (memq category custom-magic-show-hidden)))
2064 (insert " ")
2065 (when (and (eq category 'group)
2066 (not (and (eq custom-buffer-style 'links)
2067 (> (widget-get parent :custom-level) 1))))
2068 (insert-char ?\s (* custom-buffer-indent
2069 (widget-get parent :custom-level))))
2070 (push (widget-create-child-and-convert
2071 widget 'choice-item
2072 :help-echo "Change the state of this item."
2073 :format (if hidden "%t" "%[%t%]")
2074 :button-prefix 'widget-push-button-prefix
2075 :button-suffix 'widget-push-button-suffix
2076 :mouse-down-action 'widget-magic-mouse-down-action
2077 :tag " State ")
2078 children)
2079 (insert ": ")
2080 (let ((start (point)))
2081 (if (eq custom-magic-show 'long)
2082 (insert text)
2083 (insert (symbol-name state)))
2084 (cond ((eq form 'lisp)
2085 (insert " (lisp)"))
2086 ((eq form 'mismatch)
2087 (insert " (mismatch)")))
2088 (put-text-property start (point) 'face 'custom-state))
2089 (insert "\n"))
2090 (when (and (eq category 'group)
2091 (not (and (eq custom-buffer-style 'links)
2092 (> (widget-get parent :custom-level) 1))))
2093 (insert-char ?\s (* custom-buffer-indent
2094 (widget-get parent :custom-level))))
2095 (when custom-magic-show-button
2096 (when custom-magic-show
2097 (let ((indent (widget-get parent :indent)))
2098 (when indent
2099 (insert-char ? indent))))
2100 (push (widget-create-child-and-convert
2101 widget 'choice-item
2102 :mouse-down-action 'widget-magic-mouse-down-action
2103 :button-face face
2104 :button-prefix ""
2105 :button-suffix ""
2106 :help-echo "Change the state."
2107 :format (if hidden "%t" "%[%t%]")
2108 :tag (if (memq form '(lisp mismatch))
2109 (concat "(" magic ")")
2110 (concat "[" magic "]")))
2111 children)
2112 (insert " "))
2113 (widget-put widget :children children))))
2115 (defun custom-magic-reset (widget)
2116 "Redraw the :custom-magic property of WIDGET."
2117 (let ((magic (widget-get widget :custom-magic)))
2118 (when magic
2119 (widget-value-set magic (widget-value magic)))))
2121 ;;; The `custom' Widget.
2123 (defface custom-button
2124 '((((type x w32 ns) (class color)) ; Like default mode line
2125 :box (:line-width 2 :style released-button)
2126 :background "lightgrey" :foreground "black"))
2127 "Face for custom buffer buttons if `custom-raised-buttons' is non-nil."
2128 :version "21.1"
2129 :group 'custom-faces)
2131 (defface custom-button-mouse
2132 '((((type x w32 ns) (class color))
2133 :box (:line-width 2 :style released-button)
2134 :background "grey90" :foreground "black")
2136 ;; This is for text terminals that support mouse, like GPM mouse
2137 ;; or the MS-DOS terminal: inverse-video makes the button stand
2138 ;; out on mouse-over.
2139 :inverse-video t))
2140 "Mouse face for custom buffer buttons if `custom-raised-buttons' is non-nil."
2141 :version "22.1"
2142 :group 'custom-faces)
2144 (defface custom-button-unraised
2145 '((t :inherit underline))
2146 "Face for custom buffer buttons if `custom-raised-buttons' is nil."
2147 :version "22.1"
2148 :group 'custom-faces)
2150 (setq custom-button
2151 (if custom-raised-buttons 'custom-button 'custom-button-unraised))
2153 (setq custom-button-mouse
2154 (if custom-raised-buttons 'custom-button-mouse 'highlight))
2156 (defface custom-button-pressed
2157 '((((type x w32 ns) (class color))
2158 :box (:line-width 2 :style pressed-button)
2159 :background "lightgrey" :foreground "black")
2160 (t :inverse-video t))
2161 "Face for pressed custom buttons if `custom-raised-buttons' is non-nil."
2162 :version "21.1"
2163 :group 'custom-faces)
2165 (defface custom-button-pressed-unraised
2166 '((default :inherit custom-button-unraised)
2167 (((class color) (background light)) :foreground "magenta4")
2168 (((class color) (background dark)) :foreground "violet"))
2169 "Face for pressed custom buttons if `custom-raised-buttons' is nil."
2170 :version "22.1"
2171 :group 'custom-faces)
2173 (setq custom-button-pressed
2174 (if custom-raised-buttons
2175 'custom-button-pressed
2176 'custom-button-pressed-unraised))
2178 (defface custom-documentation '((t nil))
2179 "Face used for documentation strings in customization buffers."
2180 :group 'custom-faces)
2182 (defface custom-state '((((class color) (background dark))
2183 :foreground "lime green")
2184 (((class color) (background light))
2185 :foreground "dark green"))
2186 "Face used for State descriptions in the customize buffer."
2187 :group 'custom-faces)
2189 (defface custom-link '((t :inherit link))
2190 "Face for links in customization buffers."
2191 :version "22.1"
2192 :group 'custom-faces)
2194 (define-widget 'custom 'default
2195 "Customize a user option."
2196 :format "%v"
2197 :convert-widget 'custom-convert-widget
2198 :notify 'custom-notify
2199 :custom-prefix ""
2200 :custom-level 1
2201 :custom-state 'hidden
2202 :documentation-property 'widget-subclass-responsibility
2203 :value-create 'widget-subclass-responsibility
2204 :value-delete 'widget-children-value-delete
2205 :value-get 'widget-value-value-get
2206 :validate 'widget-children-validate
2207 :match (lambda (_widget value) (symbolp value)))
2209 (defun custom-convert-widget (widget)
2210 "Initialize :value and :tag from :args in WIDGET."
2211 (let ((args (widget-get widget :args)))
2212 (when args
2213 (widget-put widget :value (widget-apply widget
2214 :value-to-internal (car args)))
2215 (widget-put widget :tag (custom-unlispify-tag-name (car args)))
2216 (widget-put widget :args nil)))
2217 widget)
2219 (defun custom-notify (widget &rest args)
2220 "Keep track of changes."
2221 (let ((state (widget-get widget :custom-state)))
2222 (unless (eq state 'modified)
2223 (unless (memq state '(nil unknown hidden))
2224 (widget-put widget :custom-state 'modified))
2225 (custom-magic-reset widget)
2226 (apply 'widget-default-notify widget args))))
2228 (defun custom-redraw (widget)
2229 "Redraw WIDGET with current settings."
2230 (let ((line (count-lines (point-min) (point)))
2231 (column (current-column))
2232 (pos (point))
2233 (from (marker-position (widget-get widget :from)))
2234 (to (marker-position (widget-get widget :to))))
2235 (save-excursion
2236 (widget-value-set widget (widget-value widget))
2237 (custom-redraw-magic widget))
2238 (when (and (>= pos from) (<= pos to))
2239 (condition-case nil
2240 (progn
2241 (goto-char (point-min))
2242 (forward-line (if (> column 0)
2243 (1- line)
2244 line))
2245 (move-to-column column))
2246 (error nil)))))
2248 (defun custom-redraw-magic (widget)
2249 "Redraw WIDGET state with current settings."
2250 (while widget
2251 (let ((magic (widget-get widget :custom-magic)))
2252 (cond (magic
2253 (widget-value-set magic (widget-value magic))
2254 (when (setq widget (widget-get widget :group))
2255 (custom-group-state-update widget)))
2257 (setq widget nil)))))
2258 (widget-setup))
2260 (defun custom-show (widget value)
2261 "Non-nil if WIDGET should be shown with VALUE by default."
2262 (declare (obsolete "this widget type is no longer supported." "24.1"))
2263 (let ((show (widget-get widget :custom-show)))
2264 (if (functionp show)
2265 (funcall show widget value)
2266 show)))
2268 (defun custom-load-widget (widget)
2269 "Load all dependencies for WIDGET."
2270 (custom-load-symbol (widget-value widget)))
2272 (defun custom-unloaded-symbol-p (symbol)
2273 "Return non-nil if the dependencies of SYMBOL have not yet been loaded."
2274 (let ((found nil)
2275 (loads (get symbol 'custom-loads))
2276 load)
2277 (while loads
2278 (setq load (car loads)
2279 loads (cdr loads))
2280 (cond ((symbolp load)
2281 (unless (featurep load)
2282 (setq found t)))
2283 ((assoc load load-history))
2284 ((assoc (locate-library load) load-history)
2285 (message nil))
2287 (setq found t))))
2288 found))
2290 (defun custom-unloaded-widget-p (widget)
2291 "Return non-nil if the dependencies of WIDGET have not yet been loaded."
2292 (custom-unloaded-symbol-p (widget-value widget)))
2294 (defun custom-toggle-hide (widget)
2295 "Toggle visibility of WIDGET."
2296 (custom-load-widget widget)
2297 (let ((state (widget-get widget :custom-state)))
2298 (cond ((memq state '(invalid modified set))
2299 (error "There are unsaved changes"))
2300 ((eq state 'hidden)
2301 (widget-put widget :custom-state 'unknown))
2303 (widget-put widget :documentation-shown nil)
2304 (widget-put widget :custom-state 'hidden)))
2305 (custom-redraw widget)
2306 (widget-setup)))
2308 (defun custom-toggle-parent (widget &rest _ignore)
2309 "Toggle visibility of parent of WIDGET."
2310 (custom-toggle-hide (widget-get widget :parent)))
2312 (defun custom-add-see-also (widget &optional prefix)
2313 "Add `See also ...' to WIDGET if there are any links.
2314 Insert PREFIX first if non-nil."
2315 (let* ((symbol (widget-get widget :value))
2316 (links (get symbol 'custom-links))
2317 (many (> (length links) 2))
2318 (buttons (widget-get widget :buttons))
2319 (indent (widget-get widget :indent)))
2320 (when links
2321 (when indent
2322 (insert-char ?\s indent))
2323 (when prefix
2324 (insert prefix))
2325 (insert "See also ")
2326 (while links
2327 (push (widget-create-child-and-convert
2328 widget (car links)
2329 :button-face 'custom-link
2330 :mouse-face 'highlight
2331 :pressed-face 'highlight)
2332 buttons)
2333 (setq links (cdr links))
2334 (cond ((null links)
2335 (insert ".\n"))
2336 ((null (cdr links))
2337 (if many
2338 (insert ", and ")
2339 (insert " and ")))
2341 (insert ", "))))
2342 (widget-put widget :buttons buttons))))
2344 (defun custom-add-parent-links (widget &optional initial-string _doc-initial-string)
2345 "Add \"Parent groups: ...\" to WIDGET if the group has parents.
2346 The value is non-nil if any parents were found.
2347 If INITIAL-STRING is non-nil, use that rather than \"Parent groups:\"."
2348 (let ((name (widget-value widget))
2349 (type (widget-type widget))
2350 (buttons (widget-get widget :buttons))
2351 (start (point))
2352 (parents nil))
2353 (insert (or initial-string "Groups:"))
2354 (mapatoms (lambda (symbol)
2355 (when (member (list name type) (get symbol 'custom-group))
2356 (insert " ")
2357 (push (widget-create-child-and-convert
2358 widget 'custom-group-link
2359 :tag (custom-unlispify-tag-name symbol)
2360 symbol)
2361 buttons)
2362 (setq parents (cons symbol parents)))))
2363 (if parents
2364 (insert "\n")
2365 (delete-region start (point)))
2366 (widget-put widget :buttons buttons)
2367 parents))
2369 ;;; The `custom-comment' Widget.
2371 ;; like the editable field
2372 (defface custom-comment '((((type tty))
2373 :background "yellow3"
2374 :foreground "black")
2375 (((class grayscale color)
2376 (background light))
2377 :background "gray85")
2378 (((class grayscale color)
2379 (background dark))
2380 :background "dim gray")
2382 :slant italic))
2383 "Face used for comments on variables or faces."
2384 :version "21.1"
2385 :group 'custom-faces)
2387 ;; like font-lock-comment-face
2388 (defface custom-comment-tag
2389 '((((class color) (background dark)) :foreground "gray80")
2390 (((class color) (background light)) :foreground "blue4")
2391 (((class grayscale) (background light))
2392 :foreground "DimGray" :weight bold :slant italic)
2393 (((class grayscale) (background dark))
2394 :foreground "LightGray" :weight bold :slant italic)
2395 (t :weight bold))
2396 "Face used for the comment tag on variables or faces."
2397 :group 'custom-faces)
2399 (define-widget 'custom-comment 'string
2400 "User comment."
2401 :tag "Comment"
2402 :help-echo "Edit a comment here."
2403 :sample-face 'custom-comment-tag
2404 :value-face 'custom-comment
2405 :shown nil
2406 :create 'custom-comment-create)
2408 (defun custom-comment-create (widget)
2409 (let* ((null-comment (equal "" (widget-value widget))))
2410 (if (or (widget-get (widget-get widget :parent) :comment-shown)
2411 (not null-comment))
2412 (widget-default-create widget)
2413 ;; `widget-default-delete' expects markers in these slots --
2414 ;; maybe it shouldn't.
2415 (widget-put widget :from (point-marker))
2416 (widget-put widget :to (point-marker)))))
2418 (defun custom-comment-hide (widget)
2419 (widget-put (widget-get widget :parent) :comment-shown nil))
2421 ;; Those functions are for the menu. WIDGET is NOT the comment widget. It's
2422 ;; the global custom one
2423 (defun custom-comment-show (widget)
2424 (widget-put widget :comment-shown t)
2425 (custom-redraw widget)
2426 (widget-setup))
2428 (defun custom-comment-invisible-p (widget)
2429 (let ((val (widget-value (widget-get widget :comment-widget))))
2430 (and (equal "" val)
2431 (not (widget-get widget :comment-shown)))))
2433 ;;; The `custom-variable' Widget.
2435 (defface custom-variable-tag
2436 `((((class color) (background dark))
2437 :foreground "light blue" :weight bold)
2438 (((min-colors 88) (class color) (background light))
2439 :foreground "blue1" :weight bold)
2440 (((class color) (background light))
2441 :foreground "blue" :weight bold)
2442 (t :weight bold))
2443 "Face used for unpushable variable tags."
2444 :group 'custom-faces)
2446 (defface custom-variable-button '((t :underline t :weight bold))
2447 "Face used for pushable variable tags."
2448 :group 'custom-faces)
2450 (defcustom custom-variable-default-form 'edit
2451 "Default form of displaying variable values."
2452 :type '(choice (const edit)
2453 (const lisp))
2454 :group 'custom-buffer
2455 :version "20.3")
2457 (defun custom-variable-documentation (variable)
2458 "Return documentation of VARIABLE for use in Custom buffer.
2459 Normally just return the docstring. But if VARIABLE automatically
2460 becomes buffer local when set, append a message to that effect."
2461 (format "%s%s" (documentation-property variable 'variable-documentation t)
2462 (if (and (local-variable-if-set-p variable)
2463 (or (not (local-variable-p variable))
2464 (with-temp-buffer
2465 (local-variable-if-set-p variable))))
2467 This variable automatically becomes buffer-local when set outside Custom.
2468 However, setting it through Custom sets the default value."
2469 "")))
2471 (define-widget 'custom-variable 'custom
2472 "A widget for displaying a Custom variable.
2473 The following properties have special meanings for this widget:
2475 :hidden-states should be a list of widget states for which the
2476 widget's initial contents are to be hidden.
2478 :custom-form should be a symbol describing how to display and
2479 edit the variable---either `edit' (using edit widgets),
2480 `lisp' (as a Lisp sexp), or `mismatch' (should not happen);
2481 if nil, use the return value of `custom-variable-default-form'.
2483 :shown-value, if non-nil, should be a list whose `car' is the
2484 variable value to display in place of the current value.
2486 :custom-style describes the widget interface style; nil is the
2487 default style, while `simple' means a simpler interface that
2488 inhibits the magic custom-state widget."
2489 :format "%v"
2490 :help-echo "Set or reset this variable."
2491 :documentation-property #'custom-variable-documentation
2492 :custom-category 'option
2493 :custom-state nil
2494 :custom-menu 'custom-variable-menu-create
2495 :custom-form nil
2496 :value-create 'custom-variable-value-create
2497 :action 'custom-variable-action
2498 :hidden-states '(standard)
2499 :custom-set 'custom-variable-set
2500 :custom-mark-to-save 'custom-variable-mark-to-save
2501 :custom-reset-current 'custom-redraw
2502 :custom-reset-saved 'custom-variable-reset-saved
2503 :custom-reset-standard 'custom-variable-reset-standard
2504 :custom-mark-to-reset-standard 'custom-variable-mark-to-reset-standard
2505 :custom-standard-value 'custom-variable-standard-value
2506 :custom-state-set-and-redraw 'custom-variable-state-set-and-redraw)
2508 (defun custom-variable-type (symbol)
2509 "Return a widget suitable for editing the value of SYMBOL.
2510 If SYMBOL has a `custom-type' property, use that.
2511 Otherwise, try matching SYMBOL against `custom-guess-name-alist' and
2512 try matching its doc string against `custom-guess-doc-alist'."
2513 (let* ((type (or (get symbol 'custom-type)
2514 (and (not (get symbol 'standard-value))
2515 (custom-guess-type symbol))
2516 'sexp))
2517 (options (get symbol 'custom-options))
2518 (tmp (if (listp type)
2519 (copy-sequence type)
2520 (list type))))
2521 (when options
2522 (widget-put tmp :options options))
2523 tmp))
2525 (defun custom-variable-value-create (widget)
2526 "Here is where you edit the variable's value."
2527 (custom-load-widget widget)
2528 (unless (widget-get widget :custom-form)
2529 (widget-put widget :custom-form custom-variable-default-form))
2530 (let* ((buttons (widget-get widget :buttons))
2531 (children (widget-get widget :children))
2532 (form (widget-get widget :custom-form))
2533 (symbol (widget-get widget :value))
2534 (tag (widget-get widget :tag))
2535 (type (custom-variable-type symbol))
2536 (conv (widget-convert type))
2537 (get (or (get symbol 'custom-get) 'default-value))
2538 (prefix (widget-get widget :custom-prefix))
2539 (last (widget-get widget :custom-last))
2540 (style (widget-get widget :custom-style))
2541 (value (let ((shown-value (widget-get widget :shown-value)))
2542 (cond (shown-value
2543 (car shown-value))
2544 ((default-boundp symbol)
2545 (funcall get symbol))
2546 (t (widget-get conv :value)))))
2547 (state (or (widget-get widget :custom-state)
2548 (if (memq (custom-variable-state symbol value)
2549 (widget-get widget :hidden-states))
2550 'hidden))))
2552 ;; If we don't know the state, see if we need to edit it in lisp form.
2553 (unless state
2554 (setq state (if (custom-show type value) 'unknown 'hidden)))
2555 (when (eq state 'unknown)
2556 (unless (widget-apply conv :match value)
2557 (setq form 'mismatch)))
2558 ;; Now we can create the child widget.
2559 (cond ((eq custom-buffer-style 'tree)
2560 (insert prefix (if last " `--- " " |--- "))
2561 (push (widget-create-child-and-convert
2562 widget 'custom-browse-variable-tag)
2563 buttons)
2564 (insert " " tag "\n")
2565 (widget-put widget :buttons buttons))
2566 ((eq state 'hidden)
2567 ;; Indicate hidden value.
2568 (push (widget-create-child-and-convert
2569 widget 'custom-visibility
2570 :help-echo "Show the value of this option."
2571 :on-glyph "down"
2572 :on "Hide"
2573 :off-glyph "right"
2574 :off "Show Value"
2575 :action 'custom-toggle-hide-variable
2576 nil)
2577 buttons)
2578 (insert " ")
2579 (push (widget-create-child-and-convert
2580 widget 'item
2581 :format "%{%t%} "
2582 :sample-face 'custom-variable-tag
2583 :tag tag
2584 :parent widget)
2585 buttons))
2586 ((memq form '(lisp mismatch))
2587 (push (widget-create-child-and-convert
2588 widget 'custom-visibility
2589 :help-echo "Hide the value of this option."
2590 :on "Hide"
2591 :off "Show"
2592 :on-glyph "down"
2593 :off-glyph "right"
2594 :action 'custom-toggle-hide-variable
2596 buttons)
2597 (insert " ")
2598 ;; This used to try presenting the saved value or the
2599 ;; standard value, but it seems more intuitive to present
2600 ;; the current value (Bug#7600).
2601 (let* ((value (cond ((default-boundp symbol)
2602 (custom-quote (funcall get symbol)))
2604 (custom-quote (widget-get conv :value))))))
2605 (insert (symbol-name symbol) ": ")
2606 (push (widget-create-child-and-convert
2607 widget 'sexp
2608 :button-face 'custom-variable-button-face
2609 :format "%v"
2610 :tag (symbol-name symbol)
2611 :parent widget
2612 :value value)
2613 children)))
2615 ;; Edit mode.
2616 (push (widget-create-child-and-convert
2617 widget 'custom-visibility
2618 :help-echo "Hide or show this option."
2619 :on "Hide"
2620 :off "Show"
2621 :on-glyph "down"
2622 :off-glyph "right"
2623 :action 'custom-toggle-hide-variable
2625 buttons)
2626 (insert " ")
2627 (let* ((format (widget-get type :format))
2628 tag-format value-format)
2629 (unless (string-match ":" format)
2630 (error "Bad format"))
2631 (setq tag-format (substring format 0 (match-end 0)))
2632 (setq value-format (substring format (match-end 0)))
2633 (push (widget-create-child-and-convert
2634 widget 'item
2635 :format tag-format
2636 :action 'custom-tag-action
2637 :help-echo "Change value of this option."
2638 :mouse-down-action 'custom-tag-mouse-down-action
2639 :button-face 'custom-variable-button
2640 :sample-face 'custom-variable-tag
2641 tag)
2642 buttons)
2643 (push (widget-create-child-and-convert
2644 widget type
2645 :format value-format
2646 :value value)
2647 children))))
2648 (unless (eq custom-buffer-style 'tree)
2649 (unless (eq (preceding-char) ?\n)
2650 (widget-insert "\n"))
2651 ;; Create the magic button.
2652 (unless (eq style 'simple)
2653 (let ((magic (widget-create-child-and-convert
2654 widget 'custom-magic nil)))
2655 (widget-put widget :custom-magic magic)
2656 (push magic buttons)))
2657 (widget-put widget :buttons buttons)
2658 ;; Insert documentation.
2659 (widget-put widget :documentation-indent 3)
2660 (unless (and (eq style 'simple)
2661 (eq state 'hidden))
2662 (widget-add-documentation-string-button
2663 widget :visibility-widget 'custom-visibility))
2665 ;; The comment field
2666 (unless (eq state 'hidden)
2667 (let* ((comment (get symbol 'variable-comment))
2668 (comment-widget
2669 (widget-create-child-and-convert
2670 widget 'custom-comment
2671 :parent widget
2672 :value (or comment ""))))
2673 (widget-put widget :comment-widget comment-widget)
2674 ;; Don't push it !!! Custom assumes that the first child is the
2675 ;; value one.
2676 (setq children (append children (list comment-widget)))))
2677 ;; Update the rest of the properties.
2678 (widget-put widget :custom-form form)
2679 (widget-put widget :children children)
2680 ;; Now update the state.
2681 (if (eq state 'hidden)
2682 (widget-put widget :custom-state state)
2683 (custom-variable-state-set widget))
2684 ;; See also.
2685 (unless (eq state 'hidden)
2686 (when (eq (widget-get widget :custom-level) 1)
2687 (custom-add-parent-links widget))
2688 (custom-add-see-also widget)))))
2690 (defun custom-toggle-hide-variable (visibility-widget &rest _ignore)
2691 "Toggle the visibility of a `custom-variable' parent widget.
2692 By default, this signals an error if the parent has unsaved
2693 changes. If the parent has a `simple' :custom-style property,
2694 the present value is saved to its :shown-value property instead."
2695 (let ((widget (widget-get visibility-widget :parent)))
2696 (unless (eq (widget-type widget) 'custom-variable)
2697 (error "Invalid widget type"))
2698 (custom-load-widget widget)
2699 (let ((state (widget-get widget :custom-state)))
2700 (if (eq state 'hidden)
2701 (widget-put widget :custom-state 'unknown)
2702 ;; In normal interface, widget can't be hidden if modified.
2703 (when (memq state '(invalid modified set))
2704 (if (eq (widget-get widget :custom-style) 'simple)
2705 (widget-put widget :shown-value
2706 (list (widget-value
2707 (car-safe
2708 (widget-get widget :children)))))
2709 (error "There are unsaved changes")))
2710 (widget-put widget :documentation-shown nil)
2711 (widget-put widget :custom-state 'hidden))
2712 (custom-redraw widget)
2713 (widget-setup))))
2715 (defun custom-tag-action (widget &rest args)
2716 "Pass :action to first child of WIDGET's parent."
2717 (apply 'widget-apply (car (widget-get (widget-get widget :parent) :children))
2718 :action args))
2720 (defun custom-tag-mouse-down-action (widget &rest args)
2721 "Pass :mouse-down-action to first child of WIDGET's parent."
2722 (apply 'widget-apply (car (widget-get (widget-get widget :parent) :children))
2723 :mouse-down-action args))
2725 (defun custom-variable-state (symbol val)
2726 "Return the state of SYMBOL if its value is VAL.
2727 If SYMBOL has a non-nil `custom-get' property, it overrides VAL.
2728 Possible return values are `standard', `saved', `set', `themed',
2729 `changed', and `rogue'."
2730 (let* ((get (or (get symbol 'custom-get) 'default-value))
2731 (value (if (default-boundp symbol)
2732 (funcall get symbol)
2733 val))
2734 (comment (get symbol 'variable-comment))
2736 temp)
2737 (cond ((progn (setq tmp (get symbol 'customized-value))
2738 (setq temp
2739 (get symbol 'customized-variable-comment))
2740 (or tmp temp))
2741 (if (condition-case nil
2742 (and (equal value (eval (car tmp)))
2743 (equal comment temp))
2744 (error nil))
2745 'set
2746 'changed))
2747 ((progn (setq tmp (get symbol 'theme-value))
2748 (setq temp (get symbol 'saved-variable-comment))
2749 (or tmp temp))
2750 (if (condition-case nil
2751 (and (equal comment temp)
2752 (equal value
2753 (eval
2754 (car (custom-variable-theme-value
2755 symbol)))))
2756 (error nil))
2757 (cond
2758 ((eq (caar tmp) 'user) 'saved)
2759 ((eq (caar tmp) 'changed)
2760 (if (condition-case nil
2761 (and (null comment)
2762 (equal value
2763 (eval
2764 (car (get symbol 'standard-value)))))
2765 (error nil))
2766 ;; The value was originally set outside
2767 ;; custom, but it was set to the standard
2768 ;; value (probably an autoloaded defcustom).
2769 'standard
2770 'changed))
2771 (t 'themed))
2772 'changed))
2773 ((setq tmp (get symbol 'standard-value))
2774 (if (condition-case nil
2775 (and (equal value (eval (car tmp)))
2776 (equal comment nil))
2777 (error nil))
2778 'standard
2779 'changed))
2780 (t 'rogue))))
2782 (defun custom-variable-state-set (widget &optional state)
2783 "Set the state of WIDGET to STATE.
2784 If STATE is nil, the value is computed by `custom-variable-state'."
2785 (widget-put widget :custom-state
2786 (or state (custom-variable-state (widget-value widget)
2787 (widget-get widget :value)))))
2789 (defun custom-variable-standard-value (widget)
2790 (get (widget-value widget) 'standard-value))
2792 (defvar custom-variable-menu
2793 `(("Set for Current Session" custom-variable-set
2794 (lambda (widget)
2795 (eq (widget-get widget :custom-state) 'modified)))
2796 ;; Note that in all the backquoted code in this file, we test
2797 ;; init-file-user rather than user-init-file. This is in case
2798 ;; cus-edit is loaded by something in site-start.el, because
2799 ;; user-init-file is not set at that stage.
2800 ;; http://lists.gnu.org/archive/html/emacs-devel/2007-10/msg00310.html
2801 ,@(when (or custom-file init-file-user)
2802 '(("Save for Future Sessions" custom-variable-save
2803 (lambda (widget)
2804 (memq (widget-get widget :custom-state)
2805 '(modified set changed rogue))))))
2806 ("Undo Edits" custom-redraw
2807 (lambda (widget)
2808 (and (default-boundp (widget-value widget))
2809 (memq (widget-get widget :custom-state) '(modified changed)))))
2810 ("Revert This Session's Customization" custom-variable-reset-saved
2811 (lambda (widget)
2812 (memq (widget-get widget :custom-state)
2813 '(modified set changed rogue))))
2814 ,@(when (or custom-file init-file-user)
2815 '(("Erase Customization" custom-variable-reset-standard
2816 (lambda (widget)
2817 (and (get (widget-value widget) 'standard-value)
2818 (memq (widget-get widget :custom-state)
2819 '(modified set changed saved rogue)))))))
2820 ("Set to Backup Value" custom-variable-reset-backup
2821 (lambda (widget)
2822 (get (widget-value widget) 'backup-value)))
2823 ("---" ignore ignore)
2824 ("Add Comment" custom-comment-show custom-comment-invisible-p)
2825 ("---" ignore ignore)
2826 ("Show Current Value" custom-variable-edit
2827 (lambda (widget)
2828 (eq (widget-get widget :custom-form) 'lisp)))
2829 ("Show Saved Lisp Expression" custom-variable-edit-lisp
2830 (lambda (widget)
2831 (eq (widget-get widget :custom-form) 'edit))))
2832 "Alist of actions for the `custom-variable' widget.
2833 Each entry has the form (NAME ACTION FILTER) where NAME is the name of
2834 the menu entry, ACTION is the function to call on the widget when the
2835 menu is selected, and FILTER is a predicate which takes a `custom-variable'
2836 widget as an argument, and returns non-nil if ACTION is valid on that
2837 widget. If FILTER is nil, ACTION is always valid.")
2839 (defun custom-variable-action (widget &optional event)
2840 "Show the menu for `custom-variable' WIDGET.
2841 Optional EVENT is the location for the menu."
2842 (if (eq (widget-get widget :custom-state) 'hidden)
2843 (custom-toggle-hide widget)
2844 (unless (eq (widget-get widget :custom-state) 'modified)
2845 (custom-variable-state-set widget))
2846 (custom-redraw-magic widget)
2847 (let* ((completion-ignore-case t)
2848 (answer (widget-choose (concat "Operation on "
2849 (custom-unlispify-tag-name
2850 (widget-get widget :value)))
2851 (custom-menu-filter custom-variable-menu
2852 widget)
2853 event)))
2854 (if answer
2855 (funcall answer widget)))))
2857 (defun custom-variable-edit (widget)
2858 "Edit value of WIDGET."
2859 (widget-put widget :custom-state 'unknown)
2860 (widget-put widget :custom-form 'edit)
2861 (custom-redraw widget))
2863 (defun custom-variable-edit-lisp (widget)
2864 "Edit the Lisp representation of the value of WIDGET."
2865 (widget-put widget :custom-state 'unknown)
2866 (widget-put widget :custom-form 'lisp)
2867 (custom-redraw widget))
2869 (defun custom-variable-set (widget)
2870 "Set the current value for the variable being edited by WIDGET."
2871 (let* ((form (widget-get widget :custom-form))
2872 (state (widget-get widget :custom-state))
2873 (child (car (widget-get widget :children)))
2874 (symbol (widget-value widget))
2875 (set (or (get symbol 'custom-set) 'set-default))
2876 (comment-widget (widget-get widget :comment-widget))
2877 (comment (widget-value comment-widget))
2878 val)
2879 (cond ((eq state 'hidden)
2880 (user-error "Cannot set hidden variable"))
2881 ((setq val (widget-apply child :validate))
2882 (goto-char (widget-get val :from))
2883 (error "%s" (widget-get val :error)))
2884 ((memq form '(lisp mismatch))
2885 (when (equal comment "")
2886 (setq comment nil)
2887 ;; Make the comment invisible by hand if it's empty
2888 (custom-comment-hide comment-widget))
2889 (custom-variable-backup-value widget)
2890 (custom-push-theme 'theme-value symbol 'user
2891 'set (custom-quote (widget-value child)))
2892 (funcall set symbol (eval (setq val (widget-value child))))
2893 (put symbol 'customized-value (list val))
2894 (put symbol 'variable-comment comment)
2895 (put symbol 'customized-variable-comment comment))
2897 (when (equal comment "")
2898 (setq comment nil)
2899 ;; Make the comment invisible by hand if it's empty
2900 (custom-comment-hide comment-widget))
2901 (custom-variable-backup-value widget)
2902 (custom-push-theme 'theme-value symbol 'user
2903 'set (custom-quote (widget-value child)))
2904 (funcall set symbol (setq val (widget-value child)))
2905 (put symbol 'customized-value (list (custom-quote val)))
2906 (put symbol 'variable-comment comment)
2907 (put symbol 'customized-variable-comment comment)))
2908 (custom-variable-state-set widget)
2909 (custom-redraw-magic widget)))
2911 (defun custom-variable-mark-to-save (widget)
2912 "Set value and mark for saving the variable edited by WIDGET."
2913 (let* ((form (widget-get widget :custom-form))
2914 (state (widget-get widget :custom-state))
2915 (child (car (widget-get widget :children)))
2916 (symbol (widget-value widget))
2917 (set (or (get symbol 'custom-set) 'set-default))
2918 (comment-widget (widget-get widget :comment-widget))
2919 (comment (widget-value comment-widget))
2920 val)
2921 (cond ((eq state 'hidden)
2922 (user-error "Cannot set hidden variable"))
2923 ((setq val (widget-apply child :validate))
2924 (goto-char (widget-get val :from))
2925 (error "Saving %s: %s" symbol (widget-get val :error)))
2926 ((memq form '(lisp mismatch))
2927 (when (equal comment "")
2928 (setq comment nil)
2929 ;; Make the comment invisible by hand if it's empty
2930 (custom-comment-hide comment-widget))
2931 (put symbol 'saved-value (list (widget-value child)))
2932 (custom-push-theme 'theme-value symbol 'user
2933 'set (custom-quote (widget-value child)))
2934 (funcall set symbol (eval (widget-value child)))
2935 (put symbol 'variable-comment comment)
2936 (put symbol 'saved-variable-comment comment))
2938 (when (equal comment "")
2939 (setq comment nil)
2940 ;; Make the comment invisible by hand if it's empty
2941 (custom-comment-hide comment-widget))
2942 (put symbol 'saved-value
2943 (list (custom-quote (widget-value child))))
2944 (custom-push-theme 'theme-value symbol 'user
2945 'set (custom-quote (widget-value child)))
2946 (funcall set symbol (widget-value child))
2947 (put symbol 'variable-comment comment)
2948 (put symbol 'saved-variable-comment comment)))
2949 (put symbol 'customized-value nil)
2950 (put symbol 'customized-variable-comment nil)))
2952 (defsubst custom-variable-state-set-and-redraw (widget)
2953 "Set state of variable widget WIDGET and redraw with current settings."
2954 (custom-variable-state-set widget)
2955 (custom-redraw-magic widget))
2957 (defun custom-variable-save (widget)
2958 "Save value of variable edited by widget WIDGET."
2959 (custom-variable-mark-to-save widget)
2960 (custom-save-all)
2961 (custom-variable-state-set-and-redraw widget))
2963 (defun custom-variable-reset-saved (widget)
2964 "Restore the value of the variable being edited by WIDGET.
2965 If there is a saved value, restore it; otherwise reset to the
2966 uncustomized (themed or standard) value.
2968 Update the widget to show that value. The value that was current
2969 before this operation becomes the backup value."
2970 (let* ((symbol (widget-value widget))
2971 (saved-value (get symbol 'saved-value))
2972 (comment (get symbol 'saved-variable-comment)))
2973 (custom-variable-backup-value widget)
2974 (if (not (or saved-value comment))
2975 ;; If there is no saved value, remove the setting.
2976 (custom-push-theme 'theme-value symbol 'user 'reset)
2977 ;; Otherwise, apply the saved value.
2978 (put symbol 'variable-comment comment)
2979 (custom-push-theme 'theme-value symbol 'user 'set (car-safe saved-value))
2980 (ignore-errors
2981 (funcall (or (get symbol 'custom-set) 'set-default)
2982 symbol (eval (car saved-value)))))
2983 (put symbol 'customized-value nil)
2984 (put symbol 'customized-variable-comment nil)
2985 (widget-put widget :custom-state 'unknown)
2986 ;; This call will possibly make the comment invisible
2987 (custom-redraw widget)))
2989 (defun custom-variable-mark-to-reset-standard (widget)
2990 "Mark to restore standard setting for the variable edited by widget WIDGET.
2991 If `custom-reset-standard-variables-list' is nil, save, reset and
2992 redraw the widget immediately."
2993 (let* ((symbol (widget-value widget)))
2994 (if (get symbol 'standard-value)
2995 (custom-variable-backup-value widget)
2996 (user-error "No standard setting known for %S" symbol))
2997 (put symbol 'variable-comment nil)
2998 (put symbol 'customized-value nil)
2999 (put symbol 'customized-variable-comment nil)
3000 (custom-push-theme 'theme-value symbol 'user 'reset)
3001 (custom-theme-recalc-variable symbol)
3002 (if (and custom-reset-standard-variables-list
3003 (or (get symbol 'saved-value) (get symbol 'saved-variable-comment)))
3004 (progn
3005 (put symbol 'saved-value nil)
3006 (put symbol 'saved-variable-comment nil)
3007 ;; Append this to `custom-reset-standard-variables-list' to
3008 ;; have `custom-reset-standard-save-and-update' save setting
3009 ;; to the file, update the widget's state, and redraw it.
3010 (setq custom-reset-standard-variables-list
3011 (cons widget custom-reset-standard-variables-list)))
3012 (when (or (get symbol 'saved-value) (get symbol 'saved-variable-comment))
3013 (put symbol 'saved-value nil)
3014 (put symbol 'saved-variable-comment nil)
3015 (custom-save-all))
3016 (widget-put widget :custom-state 'unknown)
3017 ;; This call will possibly make the comment invisible
3018 (custom-redraw widget))))
3020 (defun custom-variable-reset-standard (widget)
3021 "Restore standard setting for the variable edited by WIDGET.
3022 This operation eliminates any saved setting for the variable,
3023 restoring it to the state of a variable that has never been customized.
3024 The value that was current before this operation
3025 becomes the backup value, so you can get it again."
3026 (let (custom-reset-standard-variables-list)
3027 (custom-variable-mark-to-reset-standard widget)))
3029 (defun custom-variable-backup-value (widget)
3030 "Back up the current value for WIDGET's variable.
3031 The backup value is kept in the car of the `backup-value' property."
3032 (let* ((symbol (widget-value widget))
3033 (get (or (get symbol 'custom-get) 'default-value))
3034 (type (custom-variable-type symbol))
3035 (conv (widget-convert type))
3036 (value (if (default-boundp symbol)
3037 (funcall get symbol)
3038 (widget-get conv :value))))
3039 (put symbol 'backup-value (list value))))
3041 (defun custom-variable-reset-backup (widget)
3042 "Restore the backup value for the variable being edited by WIDGET.
3043 The value that was current before this operation
3044 becomes the backup value, so you can use this operation repeatedly
3045 to switch between two values."
3046 (let* ((symbol (widget-value widget))
3047 (set (or (get symbol 'custom-set) 'set-default))
3048 (value (get symbol 'backup-value))
3049 (comment-widget (widget-get widget :comment-widget))
3050 (comment (widget-value comment-widget)))
3051 (if value
3052 (progn
3053 (custom-variable-backup-value widget)
3054 (custom-push-theme 'theme-value symbol 'user 'set value)
3055 (condition-case nil
3056 (funcall set symbol (car value))
3057 (error nil)))
3058 (user-error "No backup value for %s" symbol))
3059 (put symbol 'customized-value (list (custom-quote (car value))))
3060 (put symbol 'variable-comment comment)
3061 (put symbol 'customized-variable-comment comment)
3062 (custom-variable-state-set widget)
3063 ;; This call will possibly make the comment invisible
3064 (custom-redraw widget)))
3066 ;;; The `custom-visibility' Widget
3068 (define-widget 'custom-visibility 'visibility
3069 "Show or hide a documentation string."
3070 :button-face 'custom-visibility
3071 :pressed-face 'custom-visibility
3072 :mouse-face 'highlight
3073 :pressed-face 'highlight
3074 :on-glyph nil
3075 :off-glyph nil)
3077 (defface custom-visibility
3078 '((t :height 0.8 :inherit link))
3079 "Face for the `custom-visibility' widget."
3080 :version "23.1"
3081 :group 'custom-faces)
3083 ;;; The `custom-face-edit' Widget.
3085 (define-widget 'custom-face-edit 'checklist
3086 "Widget for editing face attributes.
3087 The following properties have special meanings for this widget:
3089 :value is a plist of face attributes.
3091 :default-face-attributes, if non-nil, is a plist of defaults for
3092 face attributes (as specified by a `default' defface entry)."
3093 :format "%v"
3094 :extra-offset 3
3095 :button-args '(:help-echo "Control whether this attribute has any effect.")
3096 :value-to-internal 'custom-face-edit-fix-value
3097 :match (lambda (widget value)
3098 (widget-checklist-match widget
3099 (custom-face-edit-fix-value widget value)))
3100 :value-create 'custom-face-edit-value-create
3101 :convert-widget 'custom-face-edit-convert-widget
3102 :args (mapcar (lambda (att)
3103 (list 'group :inline t
3104 :sibling-args (widget-get (nth 1 att) :sibling-args)
3105 (list 'const :format "" :value (nth 0 att))
3106 (nth 1 att)))
3107 custom-face-attributes))
3109 (defun custom-face-edit-value-create (widget)
3110 (let* ((alist (widget-checklist-match-find
3111 widget (widget-get widget :value)))
3112 (args (widget-get widget :args))
3113 (show-all (widget-get widget :show-all-attributes))
3114 (buttons (widget-get widget :buttons))
3115 (defaults (widget-checklist-match-find
3116 widget
3117 (widget-get widget :default-face-attributes)))
3118 entry)
3119 (unless (looking-back "^ *" (line-beginning-position))
3120 (insert ?\n))
3121 (insert-char ?\s (widget-get widget :extra-offset))
3122 (if (or alist defaults show-all)
3123 (dolist (prop args)
3124 (setq entry (or (assq prop alist)
3125 (assq prop defaults)))
3126 (if (or entry show-all)
3127 (widget-checklist-add-item widget prop entry)))
3128 (insert (propertize "-- Empty face --" 'face 'shadow) ?\n))
3129 (let ((indent (widget-get widget :indent)))
3130 (if indent (insert-char ?\s (widget-get widget :indent))))
3131 (push (widget-create-child-and-convert
3132 widget 'visibility
3133 :help-echo "Show or hide all face attributes."
3134 :button-face 'custom-visibility
3135 :pressed-face 'custom-visibility
3136 :mouse-face 'highlight
3137 :on "Hide Unused Attributes" :off "Show All Attributes"
3138 :on-glyph nil :off-glyph nil
3139 :always-active t
3140 :action 'custom-face-edit-value-visibility-action
3141 show-all)
3142 buttons)
3143 (insert ?\n)
3144 (widget-put widget :buttons buttons)
3145 (widget-put widget :children (nreverse (widget-get widget :children)))))
3147 (defun custom-face-edit-value-visibility-action (widget &rest _ignore)
3148 ;; Toggle hiding of face attributes.
3149 (let ((parent (widget-get widget :parent)))
3150 (widget-put parent :show-all-attributes
3151 (not (widget-get parent :show-all-attributes)))
3152 (custom-redraw parent)))
3154 (defun custom-face-edit-fix-value (_widget value)
3155 "Ignoring WIDGET, convert :bold and :italic in VALUE to new form.
3156 Also change :reverse-video to :inverse-video."
3157 (custom-fix-face-spec value))
3159 (defun custom-face-edit-convert-widget (widget)
3160 "Convert :args as widget types in WIDGET."
3161 (widget-put
3162 widget
3163 :args (mapcar (lambda (arg)
3164 (widget-convert arg
3165 :deactivate 'custom-face-edit-deactivate
3166 :activate 'custom-face-edit-activate
3167 :delete 'custom-face-edit-delete))
3168 (widget-get widget :args)))
3169 widget)
3171 (defconst custom-face-edit (widget-convert 'custom-face-edit)
3172 "Converted version of the `custom-face-edit' widget.")
3174 (defun custom-face-edit-deactivate (widget)
3175 "Make face widget WIDGET inactive for user modifications."
3176 (unless (widget-get widget :inactive)
3177 (let ((tag (custom-face-edit-attribute-tag widget))
3178 (from (copy-marker (widget-get widget :from)))
3179 (value (widget-value widget))
3180 (inhibit-read-only t)
3181 (inhibit-modification-hooks t))
3182 (save-excursion
3183 (goto-char from)
3184 (widget-default-delete widget)
3185 (insert tag ": " (propertize "--" 'face 'shadow) "\n")
3186 (widget-put widget :inactive
3187 (cons value (cons from (- (point) from))))))))
3189 (defun custom-face-edit-activate (widget)
3190 "Make face widget WIDGET active for user modifications."
3191 (let ((inactive (widget-get widget :inactive))
3192 (inhibit-read-only t)
3193 (inhibit-modification-hooks t))
3194 (when (consp inactive)
3195 (save-excursion
3196 (goto-char (car (cdr inactive)))
3197 (delete-region (point) (+ (point) (cdr (cdr inactive))))
3198 (widget-put widget :inactive nil)
3199 (widget-apply widget :create)
3200 (widget-value-set widget (car inactive))
3201 (widget-setup)))))
3203 (defun custom-face-edit-delete (widget)
3204 "Remove WIDGET from the buffer."
3205 (let ((inactive (widget-get widget :inactive))
3206 (inhibit-read-only t)
3207 (inhibit-modification-hooks t))
3208 (if (not inactive)
3209 ;; Widget is alive, we don't have to do anything special
3210 (widget-default-delete widget)
3211 ;; WIDGET is already deleted because we did so to deactivate it;
3212 ;; now just get rid of the label we put in its place.
3213 (delete-region (car (cdr inactive))
3214 (+ (car (cdr inactive)) (cdr (cdr inactive))))
3215 (widget-put widget :inactive nil))))
3218 (defun custom-face-edit-attribute-tag (widget)
3219 "Return the first :tag property in WIDGET or one of its children."
3220 (let ((tag (widget-get widget :tag)))
3221 (or (and (not (equal tag "")) tag)
3222 (let ((children (widget-get widget :children)))
3223 (while (and (null tag) children)
3224 (setq tag (custom-face-edit-attribute-tag (pop children))))
3225 tag))))
3227 ;;; The `custom-display' Widget.
3229 (define-widget 'custom-display 'menu-choice
3230 "Select a display type."
3231 :tag "Display"
3232 :value t
3233 :help-echo "Specify frames where the face attributes should be used."
3234 :args '((const :tag "all" t)
3235 (const :tag "defaults" default)
3236 (checklist
3237 :tag "specific display"
3238 :offset 0
3239 :extra-offset 9
3240 :args ((group :sibling-args (:help-echo "\
3241 Only match the specified window systems.")
3242 (const :format "Type: "
3243 type)
3244 (checklist :inline t
3245 :offset 0
3246 (const :format "X "
3247 :sibling-args (:help-echo "\
3248 The X11 Window System.")
3250 (const :format "PM "
3251 :sibling-args (:help-echo "\
3252 OS/2 Presentation Manager.")
3254 (const :format "W32 "
3255 :sibling-args (:help-echo "\
3256 MS Windows.")
3257 w32)
3258 (const :format "NS "
3259 :sibling-args (:help-echo "\
3260 GNUstep or Macintosh OS Cocoa interface.")
3262 (const :format "DOS "
3263 :sibling-args (:help-echo "\
3264 Plain MS-DOS.")
3266 (const :format "TTY%n"
3267 :sibling-args (:help-echo "\
3268 Plain text terminals.")
3269 tty)))
3270 (group :sibling-args (:help-echo "\
3271 Only match the frames with the specified color support.")
3272 (const :format "Class: "
3273 class)
3274 (checklist :inline t
3275 :offset 0
3276 (const :format "Color "
3277 :sibling-args (:help-echo "\
3278 Match color frames.")
3279 color)
3280 (const :format "Grayscale "
3281 :sibling-args (:help-echo "\
3282 Match grayscale frames.")
3283 grayscale)
3284 (const :format "Monochrome%n"
3285 :sibling-args (:help-echo "\
3286 Match frames with no color support.")
3287 mono)))
3288 (group :sibling-args (:help-echo "\
3289 The minimum number of colors the frame should support.")
3290 (const :format "" min-colors)
3291 (integer :tag "Minimum number of colors" ))
3292 (group :sibling-args (:help-echo "\
3293 Only match frames with the specified intensity.")
3294 (const :format "\
3295 Background brightness: "
3296 background)
3297 (checklist :inline t
3298 :offset 0
3299 (const :format "Light "
3300 :sibling-args (:help-echo "\
3301 Match frames with light backgrounds.")
3302 light)
3303 (const :format "Dark\n"
3304 :sibling-args (:help-echo "\
3305 Match frames with dark backgrounds.")
3306 dark)))
3307 (group :sibling-args (:help-echo "\
3308 Only match frames that support the specified face attributes.")
3309 (const :format "Supports attributes:" supports)
3310 (custom-face-edit :inline t :format "%n%v"))))))
3312 ;;; The `custom-face' Widget.
3314 (defface custom-face-tag
3315 '((t :inherit custom-variable-tag))
3316 "Face used for face tags."
3317 :group 'custom-faces)
3319 (defcustom custom-face-default-form 'selected
3320 "Default form of displaying face definition."
3321 :type '(choice (const all)
3322 (const selected)
3323 (const lisp))
3324 :group 'custom-buffer
3325 :version "20.3")
3327 (define-widget 'custom-face 'custom
3328 "Widget for customizing a face.
3329 The following properties have special meanings for this widget:
3331 :value is the face name (a symbol).
3333 :custom-form should be a symbol describing how to display and
3334 edit the face attributes---either `selected' (attributes for
3335 selected display only), `all' (all attributes), `lisp' (as a
3336 Lisp sexp), or `mismatch' (should not happen); if nil, use
3337 the return value of `custom-face-default-form'.
3339 :custom-style describes the widget interface style; nil is the
3340 default style, while `simple' means a simpler interface that
3341 inhibits the magic custom-state widget.
3343 :sample-indent, if non-nil, is the number of columns to which to
3344 indent the face sample (an integer).
3346 :shown-value, if non-nil, is the face spec to display as the value
3347 of the widget, instead of the current face spec."
3348 :sample-face 'custom-face-tag
3349 :help-echo "Set or reset this face."
3350 :documentation-property #'face-doc-string
3351 :value-create 'custom-face-value-create
3352 :action 'custom-face-action
3353 :custom-category 'face
3354 :custom-form nil
3355 :custom-set 'custom-face-set
3356 :custom-mark-to-save 'custom-face-mark-to-save
3357 :custom-reset-current 'custom-redraw
3358 :custom-reset-saved 'custom-face-reset-saved
3359 :custom-reset-standard 'custom-face-reset-standard
3360 :custom-mark-to-reset-standard 'custom-face-mark-to-reset-standard
3361 :custom-standard-value 'custom-face-standard-value
3362 :custom-state-set-and-redraw 'custom-face-state-set-and-redraw
3363 :custom-menu 'custom-face-menu-create)
3365 (define-widget 'custom-face-all 'editable-list
3366 "An editable list of display specifications and attributes."
3367 :entry-format "%i %d %v"
3368 :insert-button-args '(:help-echo "Insert new display specification here.")
3369 :append-button-args '(:help-echo "Append new display specification here.")
3370 :delete-button-args '(:help-echo "Delete this display specification.")
3371 :args '((group :format "%v" custom-display custom-face-edit)))
3373 (defconst custom-face-all (widget-convert 'custom-face-all)
3374 "Converted version of the `custom-face-all' widget.")
3376 (defun custom-filter-face-spec (spec filter-index &optional default-filter)
3377 "Return a canonicalized version of SPEC.
3378 FILTER-INDEX is the index in the entry for each attribute in
3379 `custom-face-attributes' at which the appropriate filter function can be
3380 found, and DEFAULT-FILTER is the filter to apply for attributes that
3381 don't specify one."
3382 (mapcar (lambda (entry)
3383 ;; Filter a single face-spec entry
3384 (let ((tests (car entry))
3385 (unfiltered-attrs
3386 ;; Handle both old- and new-style attribute syntax
3387 (if (listp (car (cdr entry)))
3388 (car (cdr entry))
3389 (cdr entry)))
3390 (filtered-attrs nil))
3391 ;; Filter each face attribute
3392 (while unfiltered-attrs
3393 (let* ((attr (pop unfiltered-attrs))
3394 (pre-filtered-value (pop unfiltered-attrs))
3395 (filter
3396 (or (nth filter-index (assq attr custom-face-attributes))
3397 default-filter))
3398 (filtered-value
3399 (if filter
3400 (funcall filter pre-filtered-value)
3401 pre-filtered-value)))
3402 (push filtered-value filtered-attrs)
3403 (push attr filtered-attrs)))
3405 (list tests filtered-attrs)))
3406 spec))
3408 (defun custom-pre-filter-face-spec (spec)
3409 "Return SPEC changed as necessary for editing by the face customization widget.
3410 SPEC must be a full face spec."
3411 (custom-filter-face-spec spec 2))
3413 (defun custom-post-filter-face-spec (spec)
3414 "Return the customized SPEC in a form suitable for setting the face."
3415 (custom-filter-face-spec spec 3))
3417 (defun custom-face-widget-to-spec (widget)
3418 "Return a face spec corresponding to WIDGET.
3419 WIDGET should be a `custom-face' widget."
3420 (unless (eq (widget-type widget) 'custom-face)
3421 (error "Invalid widget"))
3422 (let ((child (car (widget-get widget :children))))
3423 (custom-post-filter-face-spec
3424 (if (eq (widget-type child) 'custom-face-edit)
3425 `((t ,(widget-value child)))
3426 (widget-value child)))))
3428 (defun custom-face-get-current-spec (face)
3429 (let ((spec (or (get face 'customized-face)
3430 (get face 'saved-face)
3431 (get face 'face-defface-spec)
3432 ;; Attempt to construct it.
3433 `((t ,(custom-face-attributes-get
3434 face (selected-frame)))))))
3435 ;; If the user has changed this face in some other way,
3436 ;; edit it as the user has specified it.
3437 (if (not (face-spec-match-p face spec (selected-frame)))
3438 (setq spec `((t ,(face-attr-construct face (selected-frame))))))
3439 (custom-pre-filter-face-spec spec)))
3441 (defun custom-toggle-hide-face (visibility-widget &rest _ignore)
3442 "Toggle the visibility of a `custom-face' parent widget.
3443 By default, this signals an error if the parent has unsaved
3444 changes. If the parent has a `simple' :custom-style property,
3445 the present value is saved to its :shown-value property instead."
3446 (let ((widget (widget-get visibility-widget :parent)))
3447 (unless (eq (widget-type widget) 'custom-face)
3448 (error "Invalid widget type"))
3449 (custom-load-widget widget)
3450 (let ((state (widget-get widget :custom-state)))
3451 (if (eq state 'hidden)
3452 (widget-put widget :custom-state 'unknown)
3453 ;; In normal interface, widget can't be hidden if modified.
3454 (when (memq state '(invalid modified set))
3455 (if (eq (widget-get widget :custom-style) 'simple)
3456 (widget-put widget :shown-value
3457 (custom-face-widget-to-spec widget))
3458 (error "There are unsaved changes")))
3459 (widget-put widget :documentation-shown nil)
3460 (widget-put widget :custom-state 'hidden))
3461 (custom-redraw widget)
3462 (widget-setup))))
3464 (defun custom-face-value-create (widget)
3465 "Create a list of the display specifications for WIDGET."
3466 (let* ((buttons (widget-get widget :buttons))
3467 (symbol (widget-get widget :value))
3468 (tag (or (widget-get widget :tag)
3469 (prin1-to-string symbol)))
3470 (hiddenp (eq (widget-get widget :custom-state) 'hidden))
3471 (style (widget-get widget :custom-style))
3472 children)
3474 (if (eq custom-buffer-style 'tree)
3476 ;; Draw a tree-style `custom-face' widget
3477 (progn
3478 (insert (widget-get widget :custom-prefix)
3479 (if (widget-get widget :custom-last) " `--- " " |--- "))
3480 (push (widget-create-child-and-convert
3481 widget 'custom-browse-face-tag)
3482 buttons)
3483 (insert " " tag "\n")
3484 (widget-put widget :buttons buttons))
3486 ;; Draw an ordinary `custom-face' widget
3487 (let ((opoint (point)))
3488 ;; Visibility indicator.
3489 (push (widget-create-child-and-convert
3490 widget 'custom-visibility
3491 :help-echo "Hide or show this face."
3492 :on "Hide" :off "Show"
3493 :on-glyph "down" :off-glyph "right"
3494 :action 'custom-toggle-hide-face
3495 (not hiddenp))
3496 buttons)
3497 ;; Face name (tag).
3498 (insert " " tag)
3499 (widget-specify-sample widget opoint (point)))
3500 (insert
3501 (cond ((eq custom-buffer-style 'face) " ")
3502 ((string-match-p "face\\'" tag) ":")
3503 (t " face: ")))
3505 ;; Face sample.
3506 (let ((sample-indent (widget-get widget :sample-indent))
3507 (indent-tabs-mode nil))
3508 (and sample-indent
3509 (<= (current-column) sample-indent)
3510 (indent-to-column sample-indent)))
3511 (push (widget-create-child-and-convert
3512 widget 'item
3513 :format "[%{%t%}]"
3514 :sample-face (let ((spec (widget-get widget :shown-value)))
3515 (if spec (face-spec-choose spec) symbol))
3516 :tag "sample")
3517 buttons)
3518 (insert "\n")
3520 ;; Magic.
3521 (unless (eq (widget-get widget :custom-style) 'simple)
3522 (let ((magic (widget-create-child-and-convert
3523 widget 'custom-magic nil)))
3524 (widget-put widget :custom-magic magic)
3525 (push magic buttons)))
3527 ;; Update buttons.
3528 (widget-put widget :buttons buttons)
3530 ;; Insert documentation.
3531 (unless (and hiddenp (eq style 'simple))
3532 (widget-put widget :documentation-indent 3)
3533 (widget-add-documentation-string-button
3534 widget :visibility-widget 'custom-visibility)
3535 ;; The comment field
3536 (unless hiddenp
3537 (let* ((comment (get symbol 'face-comment))
3538 (comment-widget
3539 (widget-create-child-and-convert
3540 widget 'custom-comment
3541 :parent widget
3542 :value (or comment ""))))
3543 (widget-put widget :comment-widget comment-widget)
3544 (push comment-widget children))))
3546 ;; Editor.
3547 (unless (eq (preceding-char) ?\n)
3548 (insert "\n"))
3549 (unless hiddenp
3550 (custom-load-widget widget)
3551 (unless (widget-get widget :custom-form)
3552 (widget-put widget :custom-form custom-face-default-form))
3554 (let* ((spec (or (widget-get widget :shown-value)
3555 (custom-face-get-current-spec symbol)))
3556 (form (widget-get widget :custom-form))
3557 (indent (widget-get widget :indent))
3558 face-alist face-entry spec-default spec-match editor)
3560 ;; Find a display in SPEC matching the selected display.
3561 ;; This will use the usual face customization interface.
3562 (setq face-alist spec)
3563 (when (eq (car-safe (car-safe face-alist)) 'default)
3564 (setq spec-default (pop face-alist)))
3566 (while (and face-alist (listp face-alist) (null spec-match))
3567 (setq face-entry (car face-alist))
3568 (and (listp face-entry)
3569 (face-spec-set-match-display (car face-entry)
3570 (selected-frame))
3571 (widget-apply custom-face-edit :match (cadr face-entry))
3572 (setq spec-match face-entry))
3573 (setq face-alist (cdr face-alist)))
3575 ;; Insert the appropriate editing widget.
3576 (setq editor
3577 (cond
3578 ((and (eq form 'selected)
3579 (or spec-match spec-default))
3580 (when indent (insert-char ?\s indent))
3581 (widget-create-child-and-convert
3582 widget 'custom-face-edit
3583 :value (cadr spec-match)
3584 :default-face-attributes (cadr spec-default)))
3585 ((and (not (eq form 'lisp))
3586 (widget-apply custom-face-all :match spec))
3587 (widget-create-child-and-convert
3588 widget 'custom-face-all :value spec))
3590 (when indent
3591 (insert-char ?\s indent))
3592 (widget-create-child-and-convert
3593 widget 'sexp :value spec))))
3594 (custom-face-state-set widget)
3595 (push editor children)
3596 (widget-put widget :children children))))))
3598 (defvar custom-face-menu
3599 `(("Set for Current Session" custom-face-set)
3600 ,@(when (or custom-file init-file-user)
3601 '(("Save for Future Sessions" custom-face-save)))
3602 ("Undo Edits" custom-redraw
3603 (lambda (widget)
3604 (memq (widget-get widget :custom-state) '(modified changed))))
3605 ("Revert This Session's Customization" custom-face-reset-saved
3606 (lambda (widget)
3607 (memq (widget-get widget :custom-state) '(modified set changed))))
3608 ,@(when (or custom-file init-file-user)
3609 '(("Erase Customization" custom-face-reset-standard
3610 (lambda (widget)
3611 (get (widget-value widget) 'face-defface-spec)))))
3612 ("---" ignore ignore)
3613 ("Add Comment" custom-comment-show custom-comment-invisible-p)
3614 ("---" ignore ignore)
3615 ("For Current Display" custom-face-edit-selected
3616 (lambda (widget)
3617 (not (eq (widget-get widget :custom-form) 'selected))))
3618 ("For All Kinds of Displays" custom-face-edit-all
3619 (lambda (widget)
3620 (not (eq (widget-get widget :custom-form) 'all))))
3621 ("Show Lisp Expression" custom-face-edit-lisp
3622 (lambda (widget)
3623 (not (eq (widget-get widget :custom-form) 'lisp)))))
3624 "Alist of actions for the `custom-face' widget.
3625 Each entry has the form (NAME ACTION FILTER) where NAME is the name of
3626 the menu entry, ACTION is the function to call on the widget when the
3627 menu is selected, and FILTER is a predicate which takes a `custom-face'
3628 widget as an argument, and returns non-nil if ACTION is valid on that
3629 widget. If FILTER is nil, ACTION is always valid.")
3631 (defun custom-face-edit-selected (widget)
3632 "Edit selected attributes of the value of WIDGET."
3633 (widget-put widget :custom-state 'unknown)
3634 (widget-put widget :custom-form 'selected)
3635 (custom-redraw widget))
3637 (defun custom-face-edit-all (widget)
3638 "Edit all attributes of the value of WIDGET."
3639 (widget-put widget :custom-state 'unknown)
3640 (widget-put widget :custom-form 'all)
3641 (custom-redraw widget))
3643 (defun custom-face-edit-lisp (widget)
3644 "Edit the Lisp representation of the value of WIDGET."
3645 (widget-put widget :custom-state 'unknown)
3646 (widget-put widget :custom-form 'lisp)
3647 (custom-redraw widget))
3649 (defun custom-face-state (face)
3650 "Return the current state of the face FACE.
3651 This is one of `set', `saved', `changed', `themed', or `rogue'."
3652 (let* ((comment (get face 'face-comment))
3653 (state
3654 (cond
3655 ((or (get face 'customized-face)
3656 (get face 'customized-face-comment))
3657 (if (equal (get face 'customized-face-comment) comment)
3658 'set
3659 'changed))
3660 ((or (get face 'saved-face)
3661 (get face 'saved-face-comment))
3662 (cond ((not (equal (get face 'saved-face-comment) comment))
3663 'changed)
3664 ((eq 'user (caar (get face 'theme-face)))
3665 'saved)
3666 ((eq 'changed (caar (get face 'theme-face)))
3667 'changed)
3668 (t 'themed)))
3669 ((get face 'face-defface-spec)
3670 (cond (comment 'changed)
3671 ((get face 'theme-face) 'themed)
3672 (t 'standard)))
3673 (t 'rogue))))
3674 ;; If the user called set-face-attribute to change the default for
3675 ;; new frames, this face is "set outside of Customize".
3676 (if (and (not (eq state 'rogue))
3677 (get face 'face-modified))
3678 'changed
3679 state)))
3681 (defun custom-face-state-set (widget)
3682 "Set the state of WIDGET."
3683 (widget-put widget :custom-state
3684 (custom-face-state (widget-value widget))))
3686 (defun custom-face-action (widget &optional event)
3687 "Show the menu for `custom-face' WIDGET.
3688 Optional EVENT is the location for the menu."
3689 (if (eq (widget-get widget :custom-state) 'hidden)
3690 (custom-toggle-hide widget)
3691 (let* ((completion-ignore-case t)
3692 (symbol (widget-get widget :value))
3693 (answer (widget-choose (concat "Operation on "
3694 (custom-unlispify-tag-name symbol))
3695 (custom-menu-filter custom-face-menu
3696 widget)
3697 event)))
3698 (if answer
3699 (funcall answer widget)))))
3701 (defun custom-face-set (widget)
3702 "Make the face attributes in WIDGET take effect."
3703 (let* ((symbol (widget-value widget))
3704 (value (custom-face-widget-to-spec widget))
3705 (comment-widget (widget-get widget :comment-widget))
3706 (comment (widget-value comment-widget)))
3707 (when (equal comment "")
3708 (setq comment nil)
3709 ;; Make the comment invisible by hand if it's empty
3710 (custom-comment-hide comment-widget))
3711 (custom-push-theme 'theme-face symbol 'user 'set value)
3712 (face-spec-set symbol value 'customized-face)
3713 (put symbol 'face-comment comment)
3714 (put symbol 'customized-face-comment comment)
3715 (custom-face-state-set widget)
3716 (custom-redraw-magic widget)))
3718 (defun custom-face-mark-to-save (widget)
3719 "Mark for saving the face edited by WIDGET."
3720 (let* ((symbol (widget-value widget))
3721 (value (custom-face-widget-to-spec widget))
3722 (comment-widget (widget-get widget :comment-widget))
3723 (comment (widget-value comment-widget))
3724 (standard (eq (widget-get widget :custom-state) 'standard)))
3725 (when (equal comment "")
3726 (setq comment nil)
3727 ;; Make the comment invisible by hand if it's empty
3728 (custom-comment-hide comment-widget))
3729 (custom-push-theme 'theme-face symbol 'user 'set value)
3730 (face-spec-set symbol value (if standard 'reset 'saved-face))
3731 (put symbol 'face-comment comment)
3732 (put symbol 'customized-face-comment nil)
3733 (put symbol 'saved-face-comment comment)))
3735 (defsubst custom-face-state-set-and-redraw (widget)
3736 "Set state of face widget WIDGET and redraw with current settings."
3737 (custom-face-state-set widget)
3738 (custom-redraw-magic widget))
3740 (defun custom-face-save (widget)
3741 "Save the face edited by WIDGET."
3742 (custom-face-mark-to-save widget)
3743 (custom-save-all)
3744 (custom-face-state-set-and-redraw widget))
3746 ;; For backward compatibility.
3747 (define-obsolete-function-alias 'custom-face-save-command 'custom-face-save
3748 "22.1")
3750 (defun custom-face-reset-saved (widget)
3751 "Restore WIDGET to the face's default attributes.
3752 If there is a saved face, restore it; otherwise reset to the
3753 uncustomized (themed or standard) face."
3754 (let* ((face (widget-value widget))
3755 (child (car (widget-get widget :children)))
3756 (saved-face (get face 'saved-face))
3757 (comment (get face 'saved-face-comment))
3758 (comment-widget (widget-get widget :comment-widget)))
3759 (custom-push-theme 'theme-face face 'user
3760 (if saved-face 'set 'reset)
3761 saved-face)
3762 (face-spec-set face saved-face 'saved-face)
3763 (put face 'face-comment comment)
3764 (put face 'customized-face-comment nil)
3765 (widget-value-set child saved-face)
3766 ;; This call manages the comment visibility
3767 (widget-value-set comment-widget (or comment ""))
3768 (custom-face-state-set widget)
3769 (custom-redraw widget)))
3771 (defun custom-face-standard-value (widget)
3772 (get (widget-value widget) 'face-defface-spec))
3774 (defun custom-face-mark-to-reset-standard (widget)
3775 "Restore widget WIDGET to the face's standard attribute values.
3776 If `custom-reset-standard-faces-list' is nil, save, reset and
3777 redraw the widget immediately."
3778 (let* ((symbol (widget-value widget))
3779 (child (car (widget-get widget :children)))
3780 (value (get symbol 'face-defface-spec))
3781 (comment-widget (widget-get widget :comment-widget)))
3782 (unless value
3783 (user-error "No standard setting for this face"))
3784 (custom-push-theme 'theme-face symbol 'user 'reset)
3785 (face-spec-set symbol value 'reset)
3786 (put symbol 'face-comment nil)
3787 (put symbol 'customized-face-comment nil)
3788 (if (and custom-reset-standard-faces-list
3789 (or (get symbol 'saved-face) (get symbol 'saved-face-comment)))
3790 ;; Do this later.
3791 (progn
3792 (put symbol 'saved-face nil)
3793 (put symbol 'saved-face-comment nil)
3794 ;; Append this to `custom-reset-standard-faces-list' and have
3795 ;; `custom-reset-standard-save-and-update' save setting to the
3796 ;; file, update the widget's state, and redraw it.
3797 (setq custom-reset-standard-faces-list
3798 (cons widget custom-reset-standard-faces-list)))
3799 (when (or (get symbol 'saved-face) (get symbol 'saved-face-comment))
3800 (put symbol 'saved-face nil)
3801 (put symbol 'saved-face-comment nil)
3802 (custom-save-all))
3803 (widget-value-set child
3804 (custom-pre-filter-face-spec
3805 (list (list t (custom-face-attributes-get
3806 symbol nil)))))
3807 ;; This call manages the comment visibility
3808 (widget-value-set comment-widget "")
3809 (custom-face-state-set widget)
3810 (custom-redraw-magic widget))))
3812 (defun custom-face-reset-standard (widget)
3813 "Restore WIDGET to the face's standard attribute values.
3814 This operation eliminates any saved attributes for the face,
3815 restoring it to the state of a face that has never been customized."
3816 (let (custom-reset-standard-faces-list)
3817 (custom-face-mark-to-reset-standard widget)))
3819 ;;; The `face' Widget.
3821 (defvar widget-face-prompt-value-history nil
3822 "History of input to `widget-face-prompt-value'.")
3824 (define-widget 'face 'symbol
3825 "A Lisp face name (with sample)."
3826 :format "%{%t%}: (%{sample%}) %v"
3827 :tag "Face"
3828 :value 'default
3829 :sample-face-get 'widget-face-sample-face-get
3830 :notify 'widget-face-notify
3831 :match (lambda (_widget value) (facep value))
3832 :completions (apply-partially #'completion-table-with-predicate
3833 obarray #'facep 'strict)
3834 :prompt-match 'facep
3835 :prompt-history 'widget-face-prompt-value-history
3836 :validate (lambda (widget)
3837 (unless (facep (widget-value widget))
3838 (widget-put widget
3839 :error (format "Invalid face: %S"
3840 (widget-value widget)))
3841 widget)))
3843 (defun widget-face-sample-face-get (widget)
3844 (let ((value (widget-value widget)))
3845 (if (facep value)
3846 value
3847 'default)))
3849 (defun widget-face-notify (widget child &optional event)
3850 "Update the sample, and notify the parent."
3851 (overlay-put (widget-get widget :sample-overlay)
3852 'face (widget-apply widget :sample-face-get))
3853 (widget-default-notify widget child event))
3856 ;;; The `hook' Widget.
3858 (define-widget 'hook 'list
3859 "An Emacs Lisp hook."
3860 :value-to-internal (lambda (_widget value)
3861 (if (and value (symbolp value))
3862 (list value)
3863 value))
3864 :match (lambda (widget value)
3865 (or (symbolp value)
3866 (widget-group-match widget value)))
3867 ;; Avoid adding undefined functions to the hook, especially for
3868 ;; things like `find-file-hook' or even more basic ones, to avoid
3869 ;; chaos.
3870 :set (lambda (symbol value)
3871 (dolist (elt value)
3872 (if (fboundp elt)
3873 (add-hook symbol elt))))
3874 :convert-widget 'custom-hook-convert-widget
3875 :tag "Hook")
3877 (defun custom-hook-convert-widget (widget)
3878 ;; Handle `:options'.
3879 (let* ((options (widget-get widget :options))
3880 (other `(editable-list :inline t
3881 :entry-format "%i %d%v"
3882 (function :format " %v")))
3883 (args (if options
3884 (list `(checklist :inline t
3885 ,@(mapcar (lambda (entry)
3886 `(function-item ,entry))
3887 options))
3888 other)
3889 (list other))))
3890 (widget-put widget :args args)
3891 widget))
3893 ;;; The `custom-group-link' Widget.
3895 (define-widget 'custom-group-link 'link
3896 "Show parent in other window when activated."
3897 :button-face 'custom-link
3898 :mouse-face 'highlight
3899 :pressed-face 'highlight
3900 :help-echo "Create customization buffer for this group."
3901 :keymap custom-mode-link-map
3902 :follow-link 'mouse-face
3903 :action 'custom-group-link-action)
3905 (defun custom-group-link-action (widget &rest _ignore)
3906 (customize-group (widget-value widget)))
3908 ;;; The `custom-group' Widget.
3910 (defcustom custom-group-tag-faces nil
3911 "Face used for group tags.
3912 The first member is used for level 1 groups, the second for level 2,
3913 and so forth. The remaining group tags are shown with `custom-group-tag'."
3914 :type '(repeat face)
3915 :group 'custom-faces)
3917 (defface custom-group-tag-1
3918 '((default :weight bold :height 1.2 :inherit variable-pitch)
3919 (((class color) (background dark)) :foreground "pink")
3920 (((min-colors 88) (class color) (background light)) :foreground "red1")
3921 (((class color) (background light)) :foreground "red"))
3922 "Face for group tags."
3923 :group 'custom-faces)
3925 (defface custom-group-tag
3926 '((default :weight bold :height 1.2 :inherit variable-pitch)
3927 (((class color) (background dark)) :foreground "light blue")
3928 (((min-colors 88) (class color) (background light)) :foreground "blue1")
3929 (((class color) (background light)) :foreground "blue")
3930 (t :weight bold))
3931 "Face for low level group tags."
3932 :group 'custom-faces)
3934 (defface custom-group-subtitle
3935 '((t :weight bold))
3936 "Face for the \"Subgroups:\" subtitle in Custom buffers."
3937 :group 'custom-faces)
3939 (defvar custom-group-doc-align-col 20)
3941 (define-widget 'custom-group 'custom
3942 "Customize group."
3943 :format "%v"
3944 :sample-face-get 'custom-group-sample-face-get
3945 :documentation-property 'group-documentation
3946 :help-echo "Set or reset all members of this group."
3947 :value-create 'custom-group-value-create
3948 :action 'custom-group-action
3949 :custom-category 'group
3950 :custom-set 'custom-group-set
3951 :custom-mark-to-save 'custom-group-mark-to-save
3952 :custom-reset-current 'custom-group-reset-current
3953 :custom-reset-saved 'custom-group-reset-saved
3954 :custom-reset-standard 'custom-group-reset-standard
3955 :custom-mark-to-reset-standard 'custom-group-mark-to-reset-standard
3956 :custom-state-set-and-redraw 'custom-group-state-set-and-redraw
3957 :custom-menu 'custom-group-menu-create)
3959 (defun custom-group-sample-face-get (widget)
3960 ;; Use :sample-face.
3961 (or (nth (1- (widget-get widget :custom-level)) custom-group-tag-faces)
3962 'custom-group-tag))
3964 (define-widget 'custom-group-visibility 'visibility
3965 "An indicator and manipulator for hidden group contents."
3966 :create 'custom-group-visibility-create)
3968 (defun custom-group-visibility-create (widget)
3969 (let ((visible (widget-value widget)))
3970 (if visible
3971 (insert "--------")))
3972 (widget-default-create widget))
3974 (defun custom-group-members (symbol groups-only)
3975 "Return SYMBOL's custom group members.
3976 If GROUPS-ONLY is non-nil, return only those members that are groups."
3977 (if (not groups-only)
3978 (get symbol 'custom-group)
3979 (let (members)
3980 (dolist (entry (get symbol 'custom-group))
3981 (when (eq (nth 1 entry) 'custom-group)
3982 (push entry members)))
3983 (nreverse members))))
3985 (defun custom-group-value-create (widget)
3986 "Insert a customize group for WIDGET in the current buffer."
3987 (unless (eq (widget-get widget :custom-state) 'hidden)
3988 (custom-load-widget widget))
3989 (let* ((state (widget-get widget :custom-state))
3990 (level (widget-get widget :custom-level))
3991 ;; (indent (widget-get widget :indent))
3992 (prefix (widget-get widget :custom-prefix))
3993 (buttons (widget-get widget :buttons))
3994 (tag (substitute-command-keys (widget-get widget :tag)))
3995 (symbol (widget-value widget))
3996 (members (custom-group-members symbol
3997 (and (eq custom-buffer-style 'tree)
3998 custom-browse-only-groups)))
3999 (doc (substitute-command-keys (widget-docstring widget))))
4000 (cond ((and (eq custom-buffer-style 'tree)
4001 (eq state 'hidden)
4002 (or members (custom-unloaded-widget-p widget)))
4003 (custom-browse-insert-prefix prefix)
4004 (push (widget-create-child-and-convert
4005 widget 'custom-browse-visibility
4006 :tag "+")
4007 buttons)
4008 (insert "-- ")
4009 (push (widget-create-child-and-convert
4010 widget 'custom-browse-group-tag)
4011 buttons)
4012 (insert " " tag "\n")
4013 (widget-put widget :buttons buttons))
4014 ((and (eq custom-buffer-style 'tree)
4015 (zerop (length members)))
4016 (custom-browse-insert-prefix prefix)
4017 (insert "[ ]-- ")
4018 (push (widget-create-child-and-convert
4019 widget 'custom-browse-group-tag)
4020 buttons)
4021 (insert " " tag "\n")
4022 (widget-put widget :buttons buttons))
4023 ((eq custom-buffer-style 'tree)
4024 (custom-browse-insert-prefix prefix)
4025 (if (zerop (length members))
4026 (progn
4027 (custom-browse-insert-prefix prefix)
4028 (insert "[ ]-- ")
4029 ;; (widget-glyph-insert nil "[ ]" "empty")
4030 ;; (widget-glyph-insert nil "-- " "horizontal")
4031 (push (widget-create-child-and-convert
4032 widget 'custom-browse-group-tag)
4033 buttons)
4034 (insert " " tag "\n")
4035 (widget-put widget :buttons buttons))
4036 (push (widget-create-child-and-convert
4037 widget 'custom-browse-visibility
4038 ;; :tag-glyph "minus"
4039 :tag "-")
4040 buttons)
4041 (insert "-\\ ")
4042 ;; (widget-glyph-insert nil "-\\ " "top")
4043 (push (widget-create-child-and-convert
4044 widget 'custom-browse-group-tag)
4045 buttons)
4046 (insert " " tag "\n")
4047 (widget-put widget :buttons buttons)
4048 (message "Creating group...")
4049 (let* ((members (custom-sort-items
4050 members
4051 ;; Never sort the top-level custom group.
4052 (unless (eq symbol 'emacs)
4053 custom-browse-sort-alphabetically)
4054 custom-browse-order-groups))
4055 (prefixes (widget-get widget :custom-prefixes))
4056 (custom-prefix-list (custom-prefix-add symbol prefixes))
4057 (extra-prefix (if (widget-get widget :custom-last)
4059 " | "))
4060 (prefix (concat prefix extra-prefix))
4061 children entry)
4062 (while members
4063 (setq entry (car members)
4064 members (cdr members))
4065 (push (widget-create-child-and-convert
4066 widget (nth 1 entry)
4067 :group widget
4068 :tag (custom-unlispify-tag-name (nth 0 entry))
4069 :custom-prefixes custom-prefix-list
4070 :custom-level (1+ level)
4071 :custom-last (null members)
4072 :value (nth 0 entry)
4073 :custom-prefix prefix)
4074 children))
4075 (widget-put widget :children (reverse children)))
4076 (message "Creating group...done")))
4077 ;; Nested style.
4078 ((eq state 'hidden)
4079 ;; Create level indicator.
4080 ;; Create tag.
4081 (if (eq custom-buffer-style 'links)
4082 (push (widget-create-child-and-convert
4083 widget 'custom-group-link
4084 :tag tag
4085 symbol)
4086 buttons)
4087 (insert-char ?\s (* custom-buffer-indent (1- level)))
4088 (insert "-- ")
4089 (push (widget-create-child-and-convert
4090 widget 'custom-group-visibility
4091 :help-echo "Show members of this group."
4092 :action 'custom-toggle-parent
4093 (not (eq state 'hidden)))
4094 buttons))
4095 (if (>= (current-column) custom-group-doc-align-col)
4096 (insert " "))
4097 ;; Create magic button.
4098 (let ((magic (widget-create-child-and-convert
4099 widget 'custom-magic nil)))
4100 (widget-put widget :custom-magic magic)
4101 (push magic buttons))
4102 ;; Update buttons.
4103 (widget-put widget :buttons buttons)
4104 ;; Insert documentation.
4105 (if (and (eq custom-buffer-style 'links) (> level 1))
4106 (widget-put widget :documentation-indent
4107 custom-group-doc-align-col))
4108 (widget-add-documentation-string-button
4109 widget :visibility-widget 'custom-visibility))
4111 ;; Nested style.
4112 (t ;Visible.
4113 ;; Draw a horizontal line (this works for both graphical
4114 ;; and text displays):
4115 (let ((p (point)))
4116 (insert "\n")
4117 (put-text-property p (1+ p) 'face '(:underline t))
4118 (overlay-put (make-overlay p (1+ p))
4119 'before-string
4120 (propertize "\n" 'face '(:underline t)
4121 'display '(space :align-to 999))))
4123 ;; Add parent groups references above the group.
4124 (when (eq level 1)
4125 (if (custom-add-parent-links widget "Parent groups:")
4126 (insert "\n")))
4127 (insert-char ?\s (* custom-buffer-indent (1- level)))
4128 ;; Create tag.
4129 (let ((start (point)))
4130 (insert tag " group: ")
4131 (widget-specify-sample widget start (point)))
4132 (cond
4133 ((not doc)
4134 (insert " Group definition missing. "))
4135 ((< (length doc) 50)
4136 (insert doc)))
4137 ;; Create visibility indicator.
4138 (unless (eq custom-buffer-style 'links)
4139 (insert "--------")
4140 (push (widget-create-child-and-convert
4141 widget 'visibility
4142 :help-echo "Hide members of this group."
4143 :action 'custom-toggle-parent
4144 (not (eq state 'hidden)))
4145 buttons)
4146 (insert " "))
4147 (insert "\n")
4148 ;; Create magic button.
4149 (let ((magic (widget-create-child-and-convert
4150 widget 'custom-magic
4151 :indent 0
4152 nil)))
4153 (widget-put widget :custom-magic magic)
4154 (push magic buttons))
4155 ;; Update buttons.
4156 (widget-put widget :buttons buttons)
4157 ;; Insert documentation.
4158 (when (and doc (>= (length doc) 50))
4159 (widget-add-documentation-string-button
4160 widget :visibility-widget 'custom-visibility))
4162 ;; Parent groups.
4163 (if nil ;;; This should test that the buffer
4164 ;;; was not made to display a group.
4165 (when (eq level 1)
4166 (insert-char ?\s custom-buffer-indent)
4167 (custom-add-parent-links widget)))
4168 (custom-add-see-also widget
4169 (make-string (* custom-buffer-indent level)
4170 ?\s))
4171 ;; Members.
4172 (message "Creating group...")
4173 (let* ((members (custom-sort-items
4174 members
4175 ;; Never sort the top-level custom group.
4176 (unless (eq symbol 'emacs)
4177 custom-buffer-sort-alphabetically)
4178 custom-buffer-order-groups))
4179 (prefixes (widget-get widget :custom-prefixes))
4180 (custom-prefix-list (custom-prefix-add symbol prefixes))
4181 (len (length members))
4182 (count 0)
4183 (reporter (make-progress-reporter
4184 "Creating group entries..." 0 len))
4185 (have-subtitle (and (not (eq symbol 'emacs))
4186 (eq custom-buffer-order-groups 'last)))
4187 prev-type
4188 children)
4190 (dolist (entry members)
4191 (unless (eq prev-type 'custom-group)
4192 (widget-insert "\n"))
4193 (progress-reporter-update reporter (setq count (1+ count)))
4194 (let ((sym (nth 0 entry))
4195 (type (nth 1 entry)))
4196 (when (and have-subtitle (eq type 'custom-group))
4197 (setq have-subtitle nil)
4198 (widget-insert
4199 (propertize "Subgroups:\n" 'face 'custom-group-subtitle)))
4200 (setq prev-type type)
4201 (push (widget-create-child-and-convert
4202 widget type
4203 :group widget
4204 :tag (custom-unlispify-tag-name sym)
4205 :custom-prefixes custom-prefix-list
4206 :custom-level (1+ level)
4207 :value sym)
4208 children)
4209 (unless (eq (preceding-char) ?\n)
4210 (widget-insert "\n"))))
4212 (setq children (nreverse children))
4213 (mapc 'custom-magic-reset children)
4214 (widget-put widget :children children)
4215 (custom-group-state-update widget)
4216 (progress-reporter-done reporter))
4217 ;; End line
4218 (let ((p (1+ (point))))
4219 (insert "\n\n")
4220 (put-text-property p (1+ p) 'face '(:underline t))
4221 (overlay-put (make-overlay p (1+ p))
4222 'before-string
4223 (propertize "\n" 'face '(:underline t)
4224 'display '(space :align-to 999))))))))
4226 (defvar custom-group-menu
4227 `(("Set for Current Session" custom-group-set
4228 (lambda (widget)
4229 (eq (widget-get widget :custom-state) 'modified)))
4230 ,@(when (or custom-file init-file-user)
4231 '(("Save for Future Sessions" custom-group-save
4232 (lambda (widget)
4233 (memq (widget-get widget :custom-state) '(modified set))))))
4234 ("Undo Edits" custom-group-reset-current
4235 (lambda (widget)
4236 (memq (widget-get widget :custom-state) '(modified))))
4237 ("Revert This Session's Customizations" custom-group-reset-saved
4238 (lambda (widget)
4239 (memq (widget-get widget :custom-state) '(modified set))))
4240 ,@(when (or custom-file init-file-user)
4241 '(("Erase Customization" custom-group-reset-standard
4242 (lambda (widget)
4243 (memq (widget-get widget :custom-state) '(modified set saved)))))))
4244 "Alist of actions for the `custom-group' widget.
4245 Each entry has the form (NAME ACTION FILTER) where NAME is the name of
4246 the menu entry, ACTION is the function to call on the widget when the
4247 menu is selected, and FILTER is a predicate which takes a `custom-group'
4248 widget as an argument, and returns non-nil if ACTION is valid on that
4249 widget. If FILTER is nil, ACTION is always valid.")
4251 (defun custom-group-action (widget &optional event)
4252 "Show the menu for `custom-group' WIDGET.
4253 Optional EVENT is the location for the menu."
4254 (if (eq (widget-get widget :custom-state) 'hidden)
4255 (custom-toggle-hide widget)
4256 (let* ((completion-ignore-case t)
4257 (answer (widget-choose (concat "Operation on "
4258 (custom-unlispify-tag-name
4259 (widget-get widget :value)))
4260 (custom-menu-filter custom-group-menu
4261 widget)
4262 event)))
4263 (if answer
4264 (funcall answer widget)))))
4266 (defun custom-group-set (widget)
4267 "Set changes in all modified group members."
4268 (dolist (child (widget-get widget :children))
4269 (when (eq (widget-get child :custom-state) 'modified)
4270 (widget-apply child :custom-set))))
4272 (defun custom-group-mark-to-save (widget)
4273 "Mark all modified group members for saving."
4274 (dolist (child (widget-get widget :children))
4275 (when (memq (widget-get child :custom-state) '(modified set))
4276 (widget-apply child :custom-mark-to-save))))
4278 (defsubst custom-group-state-set-and-redraw (widget)
4279 "Set state of group widget WIDGET and redraw with current settings."
4280 (dolist (child (widget-get widget :children))
4281 (when (memq (widget-get child :custom-state) '(modified set))
4282 (widget-apply child :custom-state-set-and-redraw))))
4284 (defun custom-group-save (widget)
4285 "Save all modified group members."
4286 (custom-group-mark-to-save widget)
4287 (custom-save-all)
4288 (custom-group-state-set-and-redraw widget))
4290 (defun custom-group-reset-current (widget)
4291 "Reset all modified group members."
4292 (dolist (child (widget-get widget :children))
4293 (when (eq (widget-get child :custom-state) 'modified)
4294 (widget-apply child :custom-reset-current))))
4296 (defun custom-group-reset-saved (widget)
4297 "Reset all modified or set group members."
4298 (dolist (child (widget-get widget :children))
4299 (when (memq (widget-get child :custom-state) '(modified set))
4300 (widget-apply child :custom-reset-saved))))
4302 (defun custom-group-reset-standard (widget)
4303 "Reset all modified, set, or saved group members."
4304 (let ((custom-reset-standard-variables-list '(t))
4305 (custom-reset-standard-faces-list '(t)))
4306 (custom-group-mark-to-reset-standard widget)
4307 (custom-reset-standard-save-and-update)))
4309 (defun custom-group-mark-to-reset-standard (widget)
4310 "Mark to reset all modified, set, or saved group members."
4311 (dolist (child (widget-get widget :children))
4312 (when (memq (widget-get child :custom-state)
4313 '(modified set saved))
4314 (widget-apply child :custom-mark-to-reset-standard))))
4316 (defun custom-group-state-update (widget)
4317 "Update magic."
4318 (unless (eq (widget-get widget :custom-state) 'hidden)
4319 (let* ((children (widget-get widget :children))
4320 (states (mapcar (lambda (child)
4321 (widget-get child :custom-state))
4322 children))
4323 (magics custom-magic-alist)
4324 (found 'standard))
4325 (while magics
4326 (let ((magic (car (car magics))))
4327 (if (and (not (eq magic 'hidden))
4328 (memq magic states))
4329 (setq found magic
4330 magics nil)
4331 (setq magics (cdr magics)))))
4332 (widget-put widget :custom-state found)))
4333 (custom-magic-reset widget))
4335 ;;; Reading and writing the custom file.
4337 ;;;###autoload
4338 (defcustom custom-file nil
4339 "File used for storing customization information.
4340 The default is nil, which means to use your init file
4341 as specified by `user-init-file'. If the value is not nil,
4342 it should be an absolute file name.
4344 You can set this option through Custom, if you carefully read the
4345 last paragraph below. However, usually it is simpler to write
4346 something like the following in your init file:
4348 \(setq custom-file \"~/.emacs-custom.el\")
4349 \(load custom-file)
4351 Note that both lines are necessary: the first line tells Custom to
4352 save all customizations in this file, but does not load it.
4354 When you change this variable outside Custom, look in the
4355 previous custom file (usually your init file) for the
4356 forms `(custom-set-variables ...)' and `(custom-set-faces ...)',
4357 and copy them (whichever ones you find) to the new custom file.
4358 This will preserve your existing customizations.
4360 If you save this option using Custom, Custom will write all
4361 currently saved customizations, including the new one for this
4362 option itself, into the file you specify, overwriting any
4363 `custom-set-variables' and `custom-set-faces' forms already
4364 present in that file. It will not delete any customizations from
4365 the old custom file. You should do that manually if that is what you
4366 want. You also have to put something like `(load \"CUSTOM-FILE\")
4367 in your init file, where CUSTOM-FILE is the actual name of the
4368 file. Otherwise, Emacs will not load the file when it starts up,
4369 and hence will not set `custom-file' to that file either."
4370 :type '(choice (const :tag "Your Emacs init file" nil)
4371 (file :format "%t:%v%d"
4372 :doc
4373 "Please read entire docstring below before setting \
4374 this through Custom.
4375 Click on \"More\" (or position point there and press RETURN)
4376 if only the first line of the docstring is shown."))
4377 :group 'customize)
4379 (defun custom-file (&optional no-error)
4380 "Return the file name for saving customizations."
4381 (if (or (null user-init-file)
4382 (and (null custom-file) init-file-had-error))
4383 ;; Started with -q, i.e. the file containing Custom settings
4384 ;; hasn't been read. Saving settings there won't make much
4385 ;; sense.
4386 (if no-error
4388 (user-error "Saving settings from \"emacs -q\" would overwrite existing customizations"))
4389 (file-chase-links (or custom-file user-init-file))))
4391 ;; If recentf-mode is non-nil, this is defined.
4392 (declare-function recentf-expand-file-name "recentf" (name))
4394 ;;;###autoload
4395 (defun custom-save-all ()
4396 "Save all customizations in `custom-file'."
4397 (when (and (null custom-file) init-file-had-error)
4398 (error "Cannot save customizations; init file was not fully loaded"))
4399 (let* ((filename (custom-file))
4400 (recentf-exclude
4401 (if recentf-mode
4402 (cons (concat "\\`"
4403 (regexp-quote
4404 (recentf-expand-file-name (custom-file)))
4405 "\\'")
4406 recentf-exclude)))
4407 (old-buffer (find-buffer-visiting filename))
4408 old-buffer-name)
4410 (with-current-buffer (let ((find-file-visit-truename t))
4411 (or old-buffer
4412 (let ((delay-mode-hooks t))
4413 (find-file-noselect filename))))
4414 ;; We'll save using file-precious-flag, so avoid destroying
4415 ;; symlinks. (If we're not already visiting the buffer, this is
4416 ;; handled by find-file-visit-truename, above.)
4417 (when old-buffer
4418 (setq old-buffer-name (buffer-file-name))
4419 (set-visited-file-name (file-chase-links filename)))
4421 (unless (eq major-mode 'emacs-lisp-mode)
4422 (delay-mode-hooks (emacs-lisp-mode)))
4423 (let ((inhibit-read-only t)
4424 (print-length nil)
4425 (print-level nil))
4426 (custom-save-variables)
4427 (custom-save-faces))
4428 (let ((file-precious-flag t))
4429 (save-buffer))
4430 (if old-buffer
4431 (progn
4432 (set-visited-file-name old-buffer-name)
4433 (set-buffer-modified-p nil))
4434 (kill-buffer (current-buffer))))))
4436 ;;;###autoload
4437 (defun customize-save-customized ()
4438 "Save all user options which have been set in this session."
4439 (interactive)
4440 (mapatoms (lambda (symbol)
4441 (let ((face (get symbol 'customized-face))
4442 (value (get symbol 'customized-value))
4443 (face-comment (get symbol 'customized-face-comment))
4444 (variable-comment
4445 (get symbol 'customized-variable-comment)))
4446 (when face
4447 (put symbol 'saved-face face)
4448 (custom-push-theme 'theme-face symbol 'user 'set value)
4449 (put symbol 'customized-face nil))
4450 (when value
4451 (put symbol 'saved-value value)
4452 (custom-push-theme 'theme-value symbol 'user 'set value)
4453 (put symbol 'customized-value nil))
4454 (when variable-comment
4455 (put symbol 'saved-variable-comment variable-comment)
4456 (put symbol 'customized-variable-comment nil))
4457 (when face-comment
4458 (put symbol 'saved-face-comment face-comment)
4459 (put symbol 'customized-face-comment nil)))))
4460 ;; We really should update all custom buffers here.
4461 (custom-save-all))
4463 ;; Editing the custom file contents in a buffer.
4465 (defun custom-save-delete (symbol)
4466 "Delete all calls to SYMBOL from the contents of the current buffer.
4467 Leave point at the old location of the first such call,
4468 or (if there were none) at the end of the buffer.
4470 This function does not save the buffer."
4471 (goto-char (point-min))
4472 ;; Skip all whitespace and comments.
4473 (while (forward-comment 1))
4474 (or (eobp)
4475 (save-excursion (forward-sexp (buffer-size)))) ; Test for scan errors.
4476 (let (first)
4477 (catch 'found
4478 (while t ;; We exit this loop only via throw.
4479 ;; Skip all whitespace and comments.
4480 (while (forward-comment 1))
4481 (let ((start (point))
4482 (sexp (condition-case nil
4483 (read (current-buffer))
4484 (end-of-file (throw 'found nil)))))
4485 (when (and (listp sexp)
4486 (eq (car sexp) symbol))
4487 (delete-region start (point))
4488 (unless first
4489 (setq first (point)))))))
4490 (if first
4491 (goto-char first)
4492 ;; Move in front of local variables, otherwise long Custom
4493 ;; entries would make them ineffective.
4494 (let ((pos (point-max))
4495 (case-fold-search t))
4496 (save-excursion
4497 (goto-char (point-max))
4498 (search-backward "\n\^L" (max (- (point-max) 3000) (point-min))
4499 'move)
4500 (when (search-forward "Local Variables:" nil t)
4501 (setq pos (line-beginning-position))))
4502 (goto-char pos)))))
4504 (defvar sort-fold-case) ; defined in sort.el
4506 (defun custom-save-variables ()
4507 "Save all customized variables in `custom-file'."
4508 (save-excursion
4509 (custom-save-delete 'custom-set-variables)
4510 (let ((standard-output (current-buffer))
4511 (saved-list (make-list 1 0))
4512 sort-fold-case)
4513 ;; First create a sorted list of saved variables.
4514 (mapatoms
4515 (lambda (symbol)
4516 (if (and (get symbol 'saved-value)
4517 ;; ignore theme values
4518 (or (null (get symbol 'theme-value))
4519 (eq 'user (caar (get symbol 'theme-value)))))
4520 (nconc saved-list (list symbol)))))
4521 (setq saved-list (sort (cdr saved-list) 'string<))
4522 (unless (bolp)
4523 (princ "\n"))
4524 (princ "(custom-set-variables
4525 ;; custom-set-variables was added by Custom.
4526 ;; If you edit it by hand, you could mess it up, so be careful.
4527 ;; Your init file should contain only one such instance.
4528 ;; If there is more than one, they won't work right.\n")
4529 (dolist (symbol saved-list)
4530 (let ((spec (car-safe (get symbol 'theme-value)))
4531 (value (get symbol 'saved-value))
4532 (requests (get symbol 'custom-requests))
4533 (now (and (not (custom-variable-p symbol))
4534 (or (boundp symbol)
4535 (eq (get symbol 'force-value)
4536 'rogue))))
4537 (comment (get symbol 'saved-variable-comment)))
4538 ;; Check REQUESTS for validity.
4539 (dolist (request requests)
4540 (when (and (symbolp request) (not (featurep request)))
4541 (message "Unknown requested feature: %s" request)
4542 (setq requests (delq request requests))))
4543 ;; Is there anything customized about this variable?
4544 (when (or (and spec (eq (car spec) 'user))
4545 comment
4546 (and (null spec) (get symbol 'saved-value)))
4547 ;; Output an element for this variable.
4548 ;; It has the form (SYMBOL VALUE-FORM NOW REQUESTS COMMENT).
4549 ;; SYMBOL is the variable name.
4550 ;; VALUE-FORM is an expression to return the customized value.
4551 ;; NOW if non-nil means always set the variable immediately
4552 ;; when the customizations are reloaded. This is used
4553 ;; for rogue variables
4554 ;; REQUESTS is a list of packages to load before setting the
4555 ;; variable. Each element of it will be passed to `require'.
4556 ;; COMMENT is whatever comment the user has specified
4557 ;; with the customize facility.
4558 (unless (bolp)
4559 (princ "\n"))
4560 (princ " '(")
4561 (prin1 symbol)
4562 (princ " ")
4563 (let ((val (prin1-to-string (car value))))
4564 (if (< (length val) 60)
4565 (insert val)
4566 (newline-and-indent)
4567 (let ((beginning-of-val (point)))
4568 (insert val)
4569 (save-excursion
4570 (goto-char beginning-of-val)
4571 (indent-pp-sexp 1)))))
4572 (when (or now requests comment)
4573 (princ " ")
4574 (prin1 now)
4575 (when (or requests comment)
4576 (princ " ")
4577 (prin1 requests)
4578 (when comment
4579 (princ " ")
4580 (prin1 comment))))
4581 (princ ")"))))
4582 (if (bolp)
4583 (princ " "))
4584 (princ ")")
4585 (unless (looking-at-p "\n")
4586 (princ "\n")))))
4588 (defun custom-save-faces ()
4589 "Save all customized faces in `custom-file'."
4590 (save-excursion
4591 (custom-save-delete 'custom-reset-faces)
4592 (custom-save-delete 'custom-set-faces)
4593 (let ((standard-output (current-buffer))
4594 (saved-list (make-list 1 0))
4595 sort-fold-case)
4596 ;; First create a sorted list of saved faces.
4597 (mapatoms
4598 (lambda (symbol)
4599 (if (and (get symbol 'saved-face)
4600 (eq 'user (car (car-safe (get symbol 'theme-face)))))
4601 (nconc saved-list (list symbol)))))
4602 (setq saved-list (sort (cdr saved-list) 'string<))
4603 ;; The default face must be first, since it affects the others.
4604 (if (memq 'default saved-list)
4605 (setq saved-list (cons 'default (delq 'default saved-list))))
4606 (unless (bolp)
4607 (princ "\n"))
4608 (princ "(custom-set-faces
4609 ;; custom-set-faces was added by Custom.
4610 ;; If you edit it by hand, you could mess it up, so be careful.
4611 ;; Your init file should contain only one such instance.
4612 ;; If there is more than one, they won't work right.\n")
4613 (dolist (symbol saved-list)
4614 (let ((spec (car-safe (get symbol 'theme-face)))
4615 (value (get symbol 'saved-face))
4616 (now (not (or (get symbol 'face-defface-spec)
4617 (and (not (custom-facep symbol))
4618 (not (get symbol 'force-face))))))
4619 (comment (get symbol 'saved-face-comment)))
4620 (when (or (and spec (eq (nth 0 spec) 'user))
4621 comment
4622 (and (null spec) (get symbol 'saved-face)))
4623 ;; Don't print default face here.
4624 (unless (bolp)
4625 (princ "\n"))
4626 (princ " '(")
4627 (prin1 symbol)
4628 (princ " ")
4629 (prin1 value)
4630 (when (or now comment)
4631 (princ " ")
4632 (prin1 now)
4633 (when comment
4634 (princ " ")
4635 (prin1 comment)))
4636 (princ ")"))))
4637 (if (bolp)
4638 (princ " "))
4639 (princ ")")
4640 (unless (looking-at-p "\n")
4641 (princ "\n")))))
4643 ;;; The Customize Menu.
4645 ;;; Menu support
4647 (defcustom custom-menu-nesting 2
4648 "Maximum nesting in custom menus."
4649 :type 'integer
4650 :group 'custom-menu)
4652 (defun custom-face-menu-create (_widget symbol)
4653 "Ignoring WIDGET, create a menu entry for customization face SYMBOL."
4654 (vector (custom-unlispify-menu-entry symbol)
4655 `(customize-face ',symbol)
4658 (defun custom-variable-menu-create (_widget symbol)
4659 "Ignoring WIDGET, create a menu entry for customization variable SYMBOL."
4660 (let ((type (get symbol 'custom-type)))
4661 (unless (listp type)
4662 (setq type (list type)))
4663 (if (and type (widget-get type :custom-menu))
4664 (widget-apply type :custom-menu symbol)
4665 (vector (custom-unlispify-menu-entry symbol)
4666 `(customize-variable ',symbol)
4667 t))))
4669 ;; Add checkboxes to boolean variable entries.
4670 (widget-put (get 'boolean 'widget-type)
4671 :custom-menu (lambda (_widget symbol)
4672 (vector (custom-unlispify-menu-entry symbol)
4673 `(customize-variable ',symbol)
4674 ':style 'toggle
4675 ':selected symbol)))
4677 (defun custom-group-menu-create (_widget symbol)
4678 "Ignoring WIDGET, create a menu entry for customization group SYMBOL."
4679 `( ,(custom-unlispify-menu-entry symbol t)
4680 :filter (lambda (&rest junk)
4681 (let* ((menu (custom-menu-create ',symbol)))
4682 (if (consp menu) (cdr menu) menu)))))
4684 ;;;###autoload
4685 (defun custom-menu-create (symbol)
4686 "Create menu for customization group SYMBOL.
4687 The menu is in a format applicable to `easy-menu-define'."
4688 (let* ((deactivate-mark nil)
4689 (item (vector (custom-unlispify-menu-entry symbol)
4690 `(customize-group ',symbol)
4691 t)))
4692 (if (and (or (not (boundp 'custom-menu-nesting))
4693 (>= custom-menu-nesting 0))
4694 (progn
4695 (custom-load-symbol symbol)
4696 (< (length (get symbol 'custom-group)) widget-menu-max-size)))
4697 (let ((custom-prefix-list (custom-prefix-add symbol
4698 custom-prefix-list))
4699 (members (custom-sort-items (get symbol 'custom-group)
4700 custom-menu-sort-alphabetically
4701 custom-menu-order-groups)))
4702 `(,(custom-unlispify-menu-entry symbol t)
4703 ,item
4704 "--"
4705 ,@(mapcar (lambda (entry)
4706 (widget-apply (if (listp (nth 1 entry))
4707 (nth 1 entry)
4708 (list (nth 1 entry)))
4709 :custom-menu (nth 0 entry)))
4710 members)))
4711 item)))
4713 ;;;###autoload
4714 (defun customize-menu-create (symbol &optional name)
4715 "Return a customize menu for customization group SYMBOL.
4716 If optional NAME is given, use that as the name of the menu.
4717 Otherwise the menu will be named `Customize'.
4718 The format is suitable for use with `easy-menu-define'."
4719 (unless name
4720 (setq name "Customize"))
4721 `(,name
4722 :filter (lambda (&rest junk)
4723 (let ((menu (custom-menu-create ',symbol)))
4724 (if (consp menu) (cdr menu) menu)))))
4726 ;;; Toolbar and menubar support
4728 (easy-menu-define
4729 Custom-mode-menu (list custom-mode-map custom-field-keymap)
4730 "Menu used in customization buffers."
4731 (nconc (list "Custom"
4732 (customize-menu-create 'customize))
4733 (mapcar (lambda (arg)
4734 (let ((tag (nth 0 arg))
4735 (command (nth 1 arg))
4736 (active (nth 2 arg))
4737 (help (nth 3 arg)))
4738 (vector tag command :active (eval active) :help help)))
4739 custom-commands)))
4741 (defvar tool-bar-map)
4743 ;;; `custom-tool-bar-map' used to be set up here. This will fail to
4744 ;;; DTRT when `display-graphic-p' returns nil during compilation. Hence
4745 ;;; we set this up lazily in `Custom-mode'.
4746 (defvar custom-tool-bar-map nil
4747 "Keymap for toolbar in Custom mode.")
4749 ;;; The Custom Mode.
4751 (defun Custom-no-edit (_pos &optional _event)
4752 "Invoke button at POS, or refuse to allow editing of Custom buffer."
4753 (interactive "@d")
4754 (error "You can't edit this part of the Custom buffer"))
4756 (defun Custom-newline (pos &optional event)
4757 "Invoke button at POS, or refuse to allow editing of Custom buffer."
4758 (interactive "@d")
4759 (let ((button (get-char-property pos 'button)))
4760 ;; If there is no button at point, then use the one at the start
4761 ;; of the line, if it is a custom-group-link (bug#2298).
4762 (or button
4763 (if (setq button (get-char-property (line-beginning-position) 'button))
4764 (or (eq (widget-type button) 'custom-group-link)
4765 (setq button nil))))
4766 (if button
4767 (widget-apply-action button event)
4768 (error "You can't edit this part of the Custom buffer"))))
4770 (defun Custom-goto-parent ()
4771 "Go to the parent group listed at the top of this buffer.
4772 If several parents are listed, go to the first of them."
4773 (interactive)
4774 (save-excursion
4775 (goto-char (point-min))
4776 (if (search-forward "\nParent groups: " nil t)
4777 (let* ((button (get-char-property (point) 'button))
4778 (parent (downcase (widget-get button :tag))))
4779 (customize-group parent)))))
4781 (defcustom Custom-mode-hook nil
4782 "Hook called when entering Custom mode."
4783 :type 'hook
4784 :group 'custom-buffer)
4786 (defun custom-state-buffer-message (widget)
4787 (if (eq (widget-get (widget-get widget :parent) :custom-state) 'modified)
4788 (message "To install your edits, invoke [State] and choose the Set operation")))
4790 (defun custom--initialize-widget-variables ()
4791 (setq-local widget-documentation-face 'custom-documentation)
4792 (setq-local widget-button-face custom-button)
4793 (setq-local widget-button-pressed-face custom-button-pressed)
4794 (setq-local widget-mouse-face custom-button-mouse)
4795 ;; We need this because of the "More" button on docstrings.
4796 ;; Otherwise clicking on "More" can push point offscreen, which
4797 ;; causes the window to recenter on point, which pushes the
4798 ;; newly-revealed docstring offscreen; which is annoying. -- cyd.
4799 (setq-local widget-button-click-moves-point t)
4800 ;; When possible, use relief for buttons, not bracketing. This test
4801 ;; may not be optimal.
4802 (when custom-raised-buttons
4803 (setq-local widget-push-button-prefix "")
4804 (setq-local widget-push-button-suffix "")
4805 (setq-local widget-link-prefix "")
4806 (setq-local widget-link-suffix ""))
4807 (setq show-trailing-whitespace nil))
4809 (define-obsolete-variable-alias 'custom-mode-hook 'Custom-mode-hook "23.1")
4810 (define-derived-mode Custom-mode nil "Custom"
4811 "Major mode for editing customization buffers.
4813 The following commands are available:
4815 \\<widget-keymap>\
4816 Move to next button, link or editable field. \\[widget-forward]
4817 Move to previous button, link or editable field. \\[widget-backward]
4818 \\<custom-field-keymap>\
4819 Complete content of editable text field. \\[widget-complete]
4820 \\<custom-mode-map>\
4821 Invoke button under the mouse pointer. \\[widget-button-click]
4822 Invoke button under point. \\[widget-button-press]
4823 Set all options from current text. \\[Custom-set]
4824 Make values in current text permanent. \\[Custom-save]
4825 Make text match actual option values. \\[Custom-reset-current]
4826 Reset options to permanent settings. \\[Custom-reset-saved]
4827 Erase customizations; set options
4828 and buffer text to the standard values. \\[Custom-reset-standard]
4830 Entry to this mode calls the value of `Custom-mode-hook'
4831 if that value is non-nil."
4832 (use-local-map custom-mode-map)
4833 (easy-menu-add Custom-mode-menu)
4834 (setq-local tool-bar-map
4835 (or custom-tool-bar-map
4836 ;; Set up `custom-tool-bar-map'.
4837 (let ((map (make-sparse-keymap)))
4838 (mapc
4839 (lambda (arg)
4840 (tool-bar-local-item-from-menu
4841 (nth 1 arg) (nth 4 arg) map custom-mode-map
4842 :label (nth 5 arg)))
4843 custom-commands)
4844 (setq custom-tool-bar-map map))))
4845 (make-local-variable 'custom-options)
4846 (make-local-variable 'custom-local-buffer)
4847 (custom--initialize-widget-variables)
4848 (add-hook 'widget-edit-functions 'custom-state-buffer-message nil t))
4850 (put 'Custom-mode 'mode-class 'special)
4852 (define-obsolete-function-alias 'custom-mode 'Custom-mode "23.1")
4854 (add-to-list 'debug-ignored-errors "^Invalid face:? ")
4856 ;;; The End.
4858 (provide 'cus-edit)
4860 ;;; cus-edit.el ends here