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