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