* lisp/calendar/cal-dst.el: Use lexical-binding
[emacs.git] / lisp / cus-edit.el
blob4965adfd56c9c2e145f54cae1f1f083b4da3ae50
1 ;;; cus-edit.el --- tools for customizing Emacs and Lisp packages -*- lexical-binding:t -*-
2 ;;
3 ;; Copyright (C) 1996-1997, 1999-2017 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 a 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-tag
2435 `((((class color) (background dark))
2436 :foreground "light blue" :weight bold)
2437 (((min-colors 88) (class color) (background light))
2438 :foreground "blue1" :weight bold)
2439 (((class color) (background light))
2440 :foreground "blue" :weight bold)
2441 (t :weight bold))
2442 "Face used for unpushable variable tags."
2443 :group 'custom-faces)
2445 (defface custom-variable-button '((t :underline t :weight bold))
2446 "Face used for pushable variable tags."
2447 :group 'custom-faces)
2449 (defcustom custom-variable-default-form 'edit
2450 "Default form of displaying variable values."
2451 :type '(choice (const edit)
2452 (const lisp))
2453 :group 'custom-buffer
2454 :version "20.3")
2456 (defun custom-variable-documentation (variable)
2457 "Return documentation of VARIABLE for use in Custom buffer.
2458 Normally just return the docstring. But if VARIABLE automatically
2459 becomes buffer local when set, append a message to that effect."
2460 (format "%s%s" (documentation-property variable 'variable-documentation t)
2461 (if (and (local-variable-if-set-p variable)
2462 (or (not (local-variable-p variable))
2463 (with-temp-buffer
2464 (local-variable-if-set-p variable))))
2466 This variable automatically becomes buffer-local when set outside Custom.
2467 However, setting it through Custom sets the default value."
2468 "")))
2470 (define-widget 'custom-variable 'custom
2471 "A widget for displaying a Custom variable.
2472 The following properties have special meanings for this widget:
2474 :hidden-states should be a list of widget states for which the
2475 widget's initial contents are to be hidden.
2477 :custom-form should be a symbol describing how to display and
2478 edit the variable---either `edit' (using edit widgets),
2479 `lisp' (as a Lisp sexp), or `mismatch' (should not happen);
2480 if nil, use the return value of `custom-variable-default-form'.
2482 :shown-value, if non-nil, should be a list whose `car' is the
2483 variable value to display in place of the current value.
2485 :custom-style describes the widget interface style; nil is the
2486 default style, while `simple' means a simpler interface that
2487 inhibits the magic custom-state widget."
2488 :format "%v"
2489 :help-echo "Set or reset this variable."
2490 :documentation-property #'custom-variable-documentation
2491 :custom-category 'option
2492 :custom-state nil
2493 :custom-menu 'custom-variable-menu-create
2494 :custom-form nil
2495 :value-create 'custom-variable-value-create
2496 :action 'custom-variable-action
2497 :hidden-states '(standard)
2498 :custom-set 'custom-variable-set
2499 :custom-mark-to-save 'custom-variable-mark-to-save
2500 :custom-reset-current 'custom-redraw
2501 :custom-reset-saved 'custom-variable-reset-saved
2502 :custom-reset-standard 'custom-variable-reset-standard
2503 :custom-mark-to-reset-standard 'custom-variable-mark-to-reset-standard
2504 :custom-standard-value 'custom-variable-standard-value
2505 :custom-state-set-and-redraw 'custom-variable-state-set-and-redraw)
2507 (defun custom-variable-type (symbol)
2508 "Return a widget suitable for editing the value of SYMBOL.
2509 If SYMBOL has a `custom-type' property, use that.
2510 Otherwise, try matching SYMBOL against `custom-guess-name-alist' and
2511 try matching its doc string against `custom-guess-doc-alist'."
2512 (let* ((type (or (get symbol 'custom-type)
2513 (and (not (get symbol 'standard-value))
2514 (custom-guess-type symbol))
2515 'sexp))
2516 (options (get symbol 'custom-options))
2517 (tmp (if (listp type)
2518 (copy-sequence type)
2519 (list type))))
2520 (when options
2521 ;; This used to use widget-put, but with strict plists that
2522 ;; fails when type is an even-length list, eg (repeat character).
2523 ;; Passing our result through widget-convert makes it a valid widget.
2524 (setcdr tmp (append (list :options options) (cdr tmp))))
2525 tmp))
2527 (defun custom-variable-value-create (widget)
2528 "Here is where you edit the variable's value."
2529 (custom-load-widget widget)
2530 (unless (widget-get widget :custom-form)
2531 (widget-put widget :custom-form custom-variable-default-form))
2532 (let* ((buttons (widget-get widget :buttons))
2533 (children (widget-get widget :children))
2534 (form (widget-get widget :custom-form))
2535 (symbol (widget-get widget :value))
2536 (tag (widget-get widget :tag))
2537 (type (custom-variable-type symbol))
2538 (conv (widget-convert type))
2539 (get (or (get symbol 'custom-get) 'default-value))
2540 (prefix (widget-get widget :custom-prefix))
2541 (last (widget-get widget :custom-last))
2542 (style (widget-get widget :custom-style))
2543 (value (let ((shown-value (widget-get widget :shown-value)))
2544 (cond (shown-value
2545 (car shown-value))
2546 ((default-boundp symbol)
2547 (funcall get symbol))
2548 (t (widget-get conv :value)))))
2549 (state (or (widget-get widget :custom-state)
2550 (if (memq (custom-variable-state symbol value)
2551 (widget-get widget :hidden-states))
2552 'hidden))))
2554 ;; If we don't know the state, see if we need to edit it in lisp form.
2555 (unless state
2556 (setq state (if (custom-show type value) 'unknown 'hidden)))
2557 (when (eq state 'unknown)
2558 (unless (widget-apply conv :match value)
2559 (setq form 'mismatch)))
2560 ;; Now we can create the child widget.
2561 (cond ((eq custom-buffer-style 'tree)
2562 (insert prefix (if last " `--- " " |--- "))
2563 (push (widget-create-child-and-convert
2564 widget 'custom-browse-variable-tag)
2565 buttons)
2566 (insert " " tag "\n")
2567 (widget-put widget :buttons buttons))
2568 ((eq state 'hidden)
2569 ;; Indicate hidden value.
2570 (push (widget-create-child-and-convert
2571 widget 'custom-visibility
2572 :help-echo "Show the value of this option."
2573 :on-glyph "down"
2574 :on "Hide"
2575 :off-glyph "right"
2576 :off "Show Value"
2577 :action 'custom-toggle-hide-variable
2578 nil)
2579 buttons)
2580 (insert " ")
2581 (push (widget-create-child-and-convert
2582 widget 'item
2583 :format "%{%t%} "
2584 :sample-face 'custom-variable-tag
2585 :tag tag
2586 :parent widget)
2587 buttons))
2588 ((memq form '(lisp mismatch))
2589 (push (widget-create-child-and-convert
2590 widget 'custom-visibility
2591 :help-echo "Hide the value of this option."
2592 :on "Hide"
2593 :off "Show"
2594 :on-glyph "down"
2595 :off-glyph "right"
2596 :action 'custom-toggle-hide-variable
2598 buttons)
2599 (insert " ")
2600 ;; This used to try presenting the saved value or the
2601 ;; standard value, but it seems more intuitive to present
2602 ;; the current value (Bug#7600).
2603 (let* ((value (cond ((default-boundp symbol)
2604 (custom-quote (funcall get symbol)))
2606 (custom-quote (widget-get conv :value))))))
2607 (insert (symbol-name symbol) ": ")
2608 (push (widget-create-child-and-convert
2609 widget 'sexp
2610 :button-face 'custom-variable-button-face
2611 :format "%v"
2612 :tag (symbol-name symbol)
2613 :parent widget
2614 :value value)
2615 children)))
2617 ;; Edit mode.
2618 (push (widget-create-child-and-convert
2619 widget 'custom-visibility
2620 :help-echo "Hide or show this option."
2621 :on "Hide"
2622 :off "Show"
2623 :on-glyph "down"
2624 :off-glyph "right"
2625 :action 'custom-toggle-hide-variable
2627 buttons)
2628 (insert " ")
2629 (let* ((format (widget-get type :format))
2630 tag-format value-format)
2631 (unless (string-match ":" format)
2632 (error "Bad format"))
2633 (setq tag-format (substring format 0 (match-end 0)))
2634 (setq value-format (substring format (match-end 0)))
2635 (push (widget-create-child-and-convert
2636 widget 'item
2637 :format tag-format
2638 :action 'custom-tag-action
2639 :help-echo "Change value of this option."
2640 :mouse-down-action 'custom-tag-mouse-down-action
2641 :button-face 'custom-variable-button
2642 :sample-face 'custom-variable-tag
2643 tag)
2644 buttons)
2645 (push (widget-create-child-and-convert
2646 widget type
2647 :format value-format
2648 :value value)
2649 children))))
2650 (unless (eq custom-buffer-style 'tree)
2651 (unless (eq (preceding-char) ?\n)
2652 (widget-insert "\n"))
2653 ;; Create the magic button.
2654 (unless (eq style 'simple)
2655 (let ((magic (widget-create-child-and-convert
2656 widget 'custom-magic nil)))
2657 (widget-put widget :custom-magic magic)
2658 (push magic buttons)))
2659 (widget-put widget :buttons buttons)
2660 ;; Insert documentation.
2661 (widget-put widget :documentation-indent 3)
2662 (unless (and (eq style 'simple)
2663 (eq state 'hidden))
2664 (widget-add-documentation-string-button
2665 widget :visibility-widget 'custom-visibility))
2667 ;; The comment field
2668 (unless (eq state 'hidden)
2669 (let* ((comment (get symbol 'variable-comment))
2670 (comment-widget
2671 (widget-create-child-and-convert
2672 widget 'custom-comment
2673 :parent widget
2674 :value (or comment ""))))
2675 (widget-put widget :comment-widget comment-widget)
2676 ;; Don't push it !!! Custom assumes that the first child is the
2677 ;; value one.
2678 (setq children (append children (list comment-widget)))))
2679 ;; Update the rest of the properties.
2680 (widget-put widget :custom-form form)
2681 (widget-put widget :children children)
2682 ;; Now update the state.
2683 (if (eq state 'hidden)
2684 (widget-put widget :custom-state state)
2685 (custom-variable-state-set widget))
2686 ;; See also.
2687 (unless (eq state 'hidden)
2688 (when (eq (widget-get widget :custom-level) 1)
2689 (custom-add-parent-links widget))
2690 (custom-add-see-also widget)))))
2692 (defun custom-toggle-hide-variable (visibility-widget &rest _ignore)
2693 "Toggle the visibility of a `custom-variable' parent widget.
2694 By default, this signals an error if the parent has unsaved
2695 changes. If the parent has a `simple' :custom-style property,
2696 the present value is saved to its :shown-value property instead."
2697 (let ((widget (widget-get visibility-widget :parent)))
2698 (unless (eq (widget-type widget) 'custom-variable)
2699 (error "Invalid widget type"))
2700 (custom-load-widget widget)
2701 (let ((state (widget-get widget :custom-state)))
2702 (if (eq state 'hidden)
2703 (widget-put widget :custom-state 'unknown)
2704 ;; In normal interface, widget can't be hidden if modified.
2705 (when (memq state '(invalid modified set))
2706 (if (eq (widget-get widget :custom-style) 'simple)
2707 (widget-put widget :shown-value
2708 (list (widget-value
2709 (car-safe
2710 (widget-get widget :children)))))
2711 (error "There are unsaved changes")))
2712 (widget-put widget :documentation-shown nil)
2713 (widget-put widget :custom-state 'hidden))
2714 (custom-redraw widget)
2715 (widget-setup))))
2717 (defun custom-tag-action (widget &rest args)
2718 "Pass :action to first child of WIDGET's parent."
2719 (apply 'widget-apply (car (widget-get (widget-get widget :parent) :children))
2720 :action args))
2722 (defun custom-tag-mouse-down-action (widget &rest args)
2723 "Pass :mouse-down-action to first child of WIDGET's parent."
2724 (apply 'widget-apply (car (widget-get (widget-get widget :parent) :children))
2725 :mouse-down-action args))
2727 (defun custom-variable-state (symbol val)
2728 "Return the state of SYMBOL if its value is VAL.
2729 If SYMBOL has a non-nil `custom-get' property, it overrides VAL.
2730 Possible return values are `standard', `saved', `set', `themed',
2731 `changed', and `rogue'."
2732 (let* ((get (or (get symbol 'custom-get) 'default-value))
2733 (value (if (default-boundp symbol)
2734 (funcall get symbol)
2735 val))
2736 (comment (get symbol 'variable-comment))
2738 temp)
2739 (cond ((progn (setq tmp (get symbol 'customized-value))
2740 (setq temp
2741 (get symbol 'customized-variable-comment))
2742 (or tmp temp))
2743 (if (condition-case nil
2744 (and (equal value (eval (car tmp)))
2745 (equal comment temp))
2746 (error nil))
2747 'set
2748 'changed))
2749 ((progn (setq tmp (get symbol 'theme-value))
2750 (setq temp (get symbol 'saved-variable-comment))
2751 (or tmp temp))
2752 (if (condition-case nil
2753 (and (equal comment temp)
2754 (equal value
2755 (eval
2756 (car (custom-variable-theme-value
2757 symbol)))))
2758 (error nil))
2759 (cond
2760 ((eq (caar tmp) 'user) 'saved)
2761 ((eq (caar tmp) 'changed)
2762 (if (condition-case nil
2763 (and (null comment)
2764 (equal value
2765 (eval
2766 (car (get symbol 'standard-value)))))
2767 (error nil))
2768 ;; The value was originally set outside
2769 ;; custom, but it was set to the standard
2770 ;; value (probably an autoloaded defcustom).
2771 'standard
2772 'changed))
2773 (t 'themed))
2774 'changed))
2775 ((setq tmp (get symbol 'standard-value))
2776 (if (condition-case nil
2777 (and (equal value (eval (car tmp)))
2778 (equal comment nil))
2779 (error nil))
2780 'standard
2781 'changed))
2782 (t 'rogue))))
2784 (defun custom-variable-state-set (widget &optional state)
2785 "Set the state of WIDGET to STATE.
2786 If STATE is nil, the value is computed by `custom-variable-state'."
2787 (widget-put widget :custom-state
2788 (or state (custom-variable-state (widget-value widget)
2789 (widget-get widget :value)))))
2791 (defun custom-variable-standard-value (widget)
2792 (get (widget-value widget) 'standard-value))
2794 (defvar custom-variable-menu
2795 `(("Set for Current Session" custom-variable-set
2796 (lambda (widget)
2797 (eq (widget-get widget :custom-state) 'modified)))
2798 ;; Note that in all the backquoted code in this file, we test
2799 ;; init-file-user rather than user-init-file. This is in case
2800 ;; cus-edit is loaded by something in site-start.el, because
2801 ;; user-init-file is not set at that stage.
2802 ;; https://lists.gnu.org/r/emacs-devel/2007-10/msg00310.html
2803 ,@(when (or custom-file init-file-user)
2804 '(("Save for Future Sessions" custom-variable-save
2805 (lambda (widget)
2806 (memq (widget-get widget :custom-state)
2807 '(modified set changed rogue))))))
2808 ("Undo Edits" custom-redraw
2809 (lambda (widget)
2810 (and (default-boundp (widget-value widget))
2811 (memq (widget-get widget :custom-state) '(modified changed)))))
2812 ("Revert This Session's Customization" custom-variable-reset-saved
2813 (lambda (widget)
2814 (memq (widget-get widget :custom-state)
2815 '(modified set changed rogue))))
2816 ,@(when (or custom-file init-file-user)
2817 '(("Erase Customization" custom-variable-reset-standard
2818 (lambda (widget)
2819 (and (get (widget-value widget) 'standard-value)
2820 (memq (widget-get widget :custom-state)
2821 '(modified set changed saved rogue)))))))
2822 ("Set to Backup Value" custom-variable-reset-backup
2823 (lambda (widget)
2824 (get (widget-value widget) 'backup-value)))
2825 ("---" ignore ignore)
2826 ("Add Comment" custom-comment-show custom-comment-invisible-p)
2827 ("---" ignore ignore)
2828 ("Show Current Value" custom-variable-edit
2829 (lambda (widget)
2830 (eq (widget-get widget :custom-form) 'lisp)))
2831 ("Show Saved Lisp Expression" custom-variable-edit-lisp
2832 (lambda (widget)
2833 (eq (widget-get widget :custom-form) 'edit))))
2834 "Alist of actions for the `custom-variable' widget.
2835 Each entry has the form (NAME ACTION FILTER) where NAME is the name of
2836 the menu entry, ACTION is the function to call on the widget when the
2837 menu is selected, and FILTER is a predicate which takes a `custom-variable'
2838 widget as an argument, and returns non-nil if ACTION is valid on that
2839 widget. If FILTER is nil, ACTION is always valid.")
2841 (defun custom-variable-action (widget &optional event)
2842 "Show the menu for `custom-variable' WIDGET.
2843 Optional EVENT is the location for the menu."
2844 (if (eq (widget-get widget :custom-state) 'hidden)
2845 (custom-toggle-hide widget)
2846 (unless (eq (widget-get widget :custom-state) 'modified)
2847 (custom-variable-state-set widget))
2848 (custom-redraw-magic widget)
2849 (let* ((completion-ignore-case t)
2850 (answer (widget-choose (concat "Operation on "
2851 (custom-unlispify-tag-name
2852 (widget-get widget :value)))
2853 (custom-menu-filter custom-variable-menu
2854 widget)
2855 event)))
2856 (if answer
2857 (funcall answer widget)))))
2859 (defun custom-variable-edit (widget)
2860 "Edit value of WIDGET."
2861 (widget-put widget :custom-state 'unknown)
2862 (widget-put widget :custom-form 'edit)
2863 (custom-redraw widget))
2865 (defun custom-variable-edit-lisp (widget)
2866 "Edit the Lisp representation of the value of WIDGET."
2867 (widget-put widget :custom-state 'unknown)
2868 (widget-put widget :custom-form 'lisp)
2869 (custom-redraw widget))
2871 (defun custom-variable-set (widget)
2872 "Set the current value for the variable being edited by WIDGET."
2873 (let* ((form (widget-get widget :custom-form))
2874 (state (widget-get widget :custom-state))
2875 (child (car (widget-get widget :children)))
2876 (symbol (widget-value widget))
2877 (set (or (get symbol 'custom-set) 'set-default))
2878 (comment-widget (widget-get widget :comment-widget))
2879 (comment (widget-value comment-widget))
2880 val)
2881 (cond ((eq state 'hidden)
2882 (user-error "Cannot set hidden variable"))
2883 ((setq val (widget-apply child :validate))
2884 (goto-char (widget-get val :from))
2885 (error "%s" (widget-get val :error)))
2886 ((memq form '(lisp mismatch))
2887 (when (equal comment "")
2888 (setq comment nil)
2889 ;; Make the comment invisible by hand if it's empty
2890 (custom-comment-hide comment-widget))
2891 (custom-variable-backup-value widget)
2892 (custom-push-theme 'theme-value symbol 'user
2893 'set (custom-quote (widget-value child)))
2894 (funcall set symbol (eval (setq val (widget-value child))))
2895 (put symbol 'customized-value (list val))
2896 (put symbol 'variable-comment comment)
2897 (put symbol 'customized-variable-comment comment))
2899 (when (equal comment "")
2900 (setq comment nil)
2901 ;; Make the comment invisible by hand if it's empty
2902 (custom-comment-hide comment-widget))
2903 (custom-variable-backup-value widget)
2904 (custom-push-theme 'theme-value symbol 'user
2905 'set (custom-quote (widget-value child)))
2906 (funcall set symbol (setq val (widget-value child)))
2907 (put symbol 'customized-value (list (custom-quote val)))
2908 (put symbol 'variable-comment comment)
2909 (put symbol 'customized-variable-comment comment)))
2910 (custom-variable-state-set widget)
2911 (custom-redraw-magic widget)))
2913 (defun custom-variable-mark-to-save (widget)
2914 "Set value and mark for saving the variable edited by WIDGET."
2915 (let* ((form (widget-get widget :custom-form))
2916 (state (widget-get widget :custom-state))
2917 (child (car (widget-get widget :children)))
2918 (symbol (widget-value widget))
2919 (set (or (get symbol 'custom-set) 'set-default))
2920 (comment-widget (widget-get widget :comment-widget))
2921 (comment (widget-value comment-widget))
2922 val)
2923 (cond ((eq state 'hidden)
2924 (user-error "Cannot set hidden variable"))
2925 ((setq val (widget-apply child :validate))
2926 (goto-char (widget-get val :from))
2927 (error "Saving %s: %s" symbol (widget-get val :error)))
2928 ((memq form '(lisp mismatch))
2929 (when (equal comment "")
2930 (setq comment nil)
2931 ;; Make the comment invisible by hand if it's empty
2932 (custom-comment-hide comment-widget))
2933 (put symbol 'saved-value (list (widget-value child)))
2934 (custom-push-theme 'theme-value symbol 'user
2935 'set (custom-quote (widget-value child)))
2936 (funcall set symbol (eval (widget-value child)))
2937 (put symbol 'variable-comment comment)
2938 (put symbol 'saved-variable-comment comment))
2940 (when (equal comment "")
2941 (setq comment nil)
2942 ;; Make the comment invisible by hand if it's empty
2943 (custom-comment-hide comment-widget))
2944 (put symbol 'saved-value
2945 (list (custom-quote (widget-value child))))
2946 (custom-push-theme 'theme-value symbol 'user
2947 'set (custom-quote (widget-value child)))
2948 (funcall set symbol (widget-value child))
2949 (put symbol 'variable-comment comment)
2950 (put symbol 'saved-variable-comment comment)))
2951 (put symbol 'customized-value nil)
2952 (put symbol 'customized-variable-comment nil)))
2954 (defsubst custom-variable-state-set-and-redraw (widget)
2955 "Set state of variable widget WIDGET and redraw with current settings."
2956 (custom-variable-state-set widget)
2957 (custom-redraw-magic widget))
2959 (defun custom-variable-save (widget)
2960 "Save value of variable edited by widget WIDGET."
2961 (custom-variable-mark-to-save widget)
2962 (custom-save-all)
2963 (custom-variable-state-set-and-redraw widget))
2965 (defun custom-variable-reset-saved (widget)
2966 "Restore the value of the variable being edited by WIDGET.
2967 If there is a saved value, restore it; otherwise reset to the
2968 uncustomized (themed or standard) value.
2970 Update the widget to show that value. The value that was current
2971 before this operation becomes the backup value."
2972 (let* ((symbol (widget-value widget))
2973 (saved-value (get symbol 'saved-value))
2974 (comment (get symbol 'saved-variable-comment)))
2975 (custom-variable-backup-value widget)
2976 (if (not (or saved-value comment))
2977 ;; If there is no saved value, remove the setting.
2978 (custom-push-theme 'theme-value symbol 'user 'reset)
2979 ;; Otherwise, apply the saved value.
2980 (put symbol 'variable-comment comment)
2981 (custom-push-theme 'theme-value symbol 'user 'set (car-safe saved-value))
2982 (ignore-errors
2983 (funcall (or (get symbol 'custom-set) 'set-default)
2984 symbol (eval (car saved-value)))))
2985 (put symbol 'customized-value nil)
2986 (put symbol 'customized-variable-comment nil)
2987 (widget-put widget :custom-state 'unknown)
2988 ;; This call will possibly make the comment invisible
2989 (custom-redraw widget)))
2991 (defun custom-variable-mark-to-reset-standard (widget)
2992 "Mark to restore standard setting for the variable edited by widget WIDGET.
2993 If `custom-reset-standard-variables-list' is nil, save, reset and
2994 redraw the widget immediately."
2995 (let* ((symbol (widget-value widget)))
2996 (if (get symbol 'standard-value)
2997 (custom-variable-backup-value widget)
2998 (user-error "No standard setting known for %S" symbol))
2999 (put symbol 'variable-comment nil)
3000 (put symbol 'customized-value nil)
3001 (put symbol 'customized-variable-comment nil)
3002 (custom-push-theme 'theme-value symbol 'user 'reset)
3003 (custom-theme-recalc-variable symbol)
3004 (if (and custom-reset-standard-variables-list
3005 (or (get symbol 'saved-value) (get symbol 'saved-variable-comment)))
3006 (progn
3007 (put symbol 'saved-value nil)
3008 (put symbol 'saved-variable-comment nil)
3009 ;; Append this to `custom-reset-standard-variables-list' to
3010 ;; have `custom-reset-standard-save-and-update' save setting
3011 ;; to the file, update the widget's state, and redraw it.
3012 (setq custom-reset-standard-variables-list
3013 (cons widget custom-reset-standard-variables-list)))
3014 (when (or (get symbol 'saved-value) (get symbol 'saved-variable-comment))
3015 (put symbol 'saved-value nil)
3016 (put symbol 'saved-variable-comment nil)
3017 (custom-save-all))
3018 (widget-put widget :custom-state 'unknown)
3019 ;; This call will possibly make the comment invisible
3020 (custom-redraw widget))))
3022 (defun custom-variable-reset-standard (widget)
3023 "Restore standard setting for the variable edited by WIDGET.
3024 This operation eliminates any saved setting for the variable,
3025 restoring it to the state of a variable that has never been customized.
3026 The value that was current before this operation
3027 becomes the backup value, so you can get it again."
3028 (let (custom-reset-standard-variables-list)
3029 (custom-variable-mark-to-reset-standard widget)))
3031 (defun custom-variable-backup-value (widget)
3032 "Back up the current value for WIDGET's variable.
3033 The backup value is kept in the car of the `backup-value' property."
3034 (let* ((symbol (widget-value widget))
3035 (get (or (get symbol 'custom-get) 'default-value))
3036 (type (custom-variable-type symbol))
3037 (conv (widget-convert type))
3038 (value (if (default-boundp symbol)
3039 (funcall get symbol)
3040 (widget-get conv :value))))
3041 (put symbol 'backup-value (list value))))
3043 (defun custom-variable-reset-backup (widget)
3044 "Restore the backup value for the variable being edited by WIDGET.
3045 The value that was current before this operation
3046 becomes the backup value, so you can use this operation repeatedly
3047 to switch between two values."
3048 (let* ((symbol (widget-value widget))
3049 (set (or (get symbol 'custom-set) 'set-default))
3050 (value (get symbol 'backup-value))
3051 (comment-widget (widget-get widget :comment-widget))
3052 (comment (widget-value comment-widget)))
3053 (if value
3054 (progn
3055 (custom-variable-backup-value widget)
3056 (custom-push-theme 'theme-value symbol 'user 'set value)
3057 (condition-case nil
3058 (funcall set symbol (car value))
3059 (error nil)))
3060 (user-error "No backup value for %s" symbol))
3061 (put symbol 'customized-value (list (custom-quote (car value))))
3062 (put symbol 'variable-comment comment)
3063 (put symbol 'customized-variable-comment comment)
3064 (custom-variable-state-set widget)
3065 ;; This call will possibly make the comment invisible
3066 (custom-redraw widget)))
3068 ;;; The `custom-visibility' Widget
3070 (define-widget 'custom-visibility 'visibility
3071 "Show or hide a documentation string."
3072 :button-face 'custom-visibility
3073 :pressed-face 'custom-visibility
3074 :mouse-face 'highlight
3075 :pressed-face 'highlight
3076 :on-glyph nil
3077 :off-glyph nil)
3079 (defface custom-visibility
3080 '((t :height 0.8 :inherit link))
3081 "Face for the `custom-visibility' widget."
3082 :version "23.1"
3083 :group 'custom-faces)
3085 ;;; The `custom-face-edit' Widget.
3087 (define-widget 'custom-face-edit 'checklist
3088 "Widget for editing face attributes.
3089 The following properties have special meanings for this widget:
3091 :value is a plist of face attributes.
3093 :default-face-attributes, if non-nil, is a plist of defaults for
3094 face attributes (as specified by a `default' defface entry)."
3095 :format "%v"
3096 :extra-offset 3
3097 :button-args '(:help-echo "Control whether this attribute has any effect.")
3098 :value-to-internal 'custom-face-edit-fix-value
3099 :match (lambda (widget value)
3100 (widget-checklist-match widget
3101 (custom-face-edit-fix-value widget value)))
3102 :value-create 'custom-face-edit-value-create
3103 :convert-widget 'custom-face-edit-convert-widget
3104 :args (mapcar (lambda (att)
3105 (list 'group :inline t
3106 :sibling-args (widget-get (nth 1 att) :sibling-args)
3107 (list 'const :format "" :value (nth 0 att))
3108 (nth 1 att)))
3109 custom-face-attributes))
3111 (defun custom-face-edit-value-create (widget)
3112 (let* ((alist (widget-checklist-match-find
3113 widget (widget-get widget :value)))
3114 (args (widget-get widget :args))
3115 (show-all (widget-get widget :show-all-attributes))
3116 (buttons (widget-get widget :buttons))
3117 (defaults (widget-checklist-match-find
3118 widget
3119 (widget-get widget :default-face-attributes)))
3120 entry)
3121 (unless (looking-back "^ *" (line-beginning-position))
3122 (insert ?\n))
3123 (insert-char ?\s (widget-get widget :extra-offset))
3124 (if (or alist defaults show-all)
3125 (dolist (prop args)
3126 (setq entry (or (assq prop alist)
3127 (assq prop defaults)))
3128 (if (or entry show-all)
3129 (widget-checklist-add-item widget prop entry)))
3130 (insert (propertize "-- Empty face --" 'face 'shadow) ?\n))
3131 (let ((indent (widget-get widget :indent)))
3132 (if indent (insert-char ?\s (widget-get widget :indent))))
3133 (push (widget-create-child-and-convert
3134 widget 'visibility
3135 :help-echo "Show or hide all face attributes."
3136 :button-face 'custom-visibility
3137 :pressed-face 'custom-visibility
3138 :mouse-face 'highlight
3139 :on "Hide Unused Attributes" :off "Show All Attributes"
3140 :on-glyph nil :off-glyph nil
3141 :always-active t
3142 :action 'custom-face-edit-value-visibility-action
3143 show-all)
3144 buttons)
3145 (insert ?\n)
3146 (widget-put widget :buttons buttons)
3147 (widget-put widget :children (nreverse (widget-get widget :children)))))
3149 (defun custom-face-edit-value-visibility-action (widget &rest _ignore)
3150 ;; Toggle hiding of face attributes.
3151 (let ((parent (widget-get widget :parent)))
3152 (widget-put parent :show-all-attributes
3153 (not (widget-get parent :show-all-attributes)))
3154 (custom-redraw parent)))
3156 (defun custom-face-edit-fix-value (_widget value)
3157 "Ignoring WIDGET, convert :bold and :italic in VALUE to new form.
3158 Also change :reverse-video to :inverse-video."
3159 (custom-fix-face-spec value))
3161 (defun custom-face-edit-convert-widget (widget)
3162 "Convert :args as widget types in WIDGET."
3163 (widget-put
3164 widget
3165 :args (mapcar (lambda (arg)
3166 (widget-convert arg
3167 :deactivate 'custom-face-edit-deactivate
3168 :activate 'custom-face-edit-activate
3169 :delete 'custom-face-edit-delete))
3170 (widget-get widget :args)))
3171 widget)
3173 (defconst custom-face-edit (widget-convert 'custom-face-edit)
3174 "Converted version of the `custom-face-edit' widget.")
3176 (defun custom-face-edit-deactivate (widget)
3177 "Make face widget WIDGET inactive for user modifications."
3178 (unless (widget-get widget :inactive)
3179 (let ((tag (custom-face-edit-attribute-tag widget))
3180 (from (copy-marker (widget-get widget :from)))
3181 (value (widget-value widget))
3182 (inhibit-read-only t)
3183 (inhibit-modification-hooks t))
3184 (save-excursion
3185 (goto-char from)
3186 (widget-default-delete widget)
3187 (insert tag ": " (propertize "--" 'face 'shadow) "\n")
3188 (widget-put widget :inactive
3189 (cons value (cons from (- (point) from))))))))
3191 (defun custom-face-edit-activate (widget)
3192 "Make face widget WIDGET active for user modifications."
3193 (let ((inactive (widget-get widget :inactive))
3194 (inhibit-read-only t)
3195 (inhibit-modification-hooks t))
3196 (when (consp inactive)
3197 (save-excursion
3198 (goto-char (car (cdr inactive)))
3199 (delete-region (point) (+ (point) (cdr (cdr inactive))))
3200 (widget-put widget :inactive nil)
3201 (widget-apply widget :create)
3202 (widget-value-set widget (car inactive))
3203 (widget-setup)))))
3205 (defun custom-face-edit-delete (widget)
3206 "Remove WIDGET from the buffer."
3207 (let ((inactive (widget-get widget :inactive))
3208 (inhibit-read-only t)
3209 (inhibit-modification-hooks t))
3210 (if (not inactive)
3211 ;; Widget is alive, we don't have to do anything special
3212 (widget-default-delete widget)
3213 ;; WIDGET is already deleted because we did so to deactivate it;
3214 ;; now just get rid of the label we put in its place.
3215 (delete-region (car (cdr inactive))
3216 (+ (car (cdr inactive)) (cdr (cdr inactive))))
3217 (widget-put widget :inactive nil))))
3220 (defun custom-face-edit-attribute-tag (widget)
3221 "Return the first :tag property in WIDGET or one of its children."
3222 (let ((tag (widget-get widget :tag)))
3223 (or (and (not (equal tag "")) tag)
3224 (let ((children (widget-get widget :children)))
3225 (while (and (null tag) children)
3226 (setq tag (custom-face-edit-attribute-tag (pop children))))
3227 tag))))
3229 ;;; The `custom-display' Widget.
3231 (define-widget 'custom-display 'menu-choice
3232 "Select a display type."
3233 :tag "Display"
3234 :value t
3235 :help-echo "Specify frames where the face attributes should be used."
3236 :args '((const :tag "all" t)
3237 (const :tag "defaults" default)
3238 (checklist
3239 :tag "specific display"
3240 :offset 0
3241 :extra-offset 9
3242 :args ((group :sibling-args (:help-echo "\
3243 Only match the specified window systems.")
3244 (const :format "Type: "
3245 type)
3246 (checklist :inline t
3247 :offset 0
3248 (const :format "X "
3249 :sibling-args (:help-echo "\
3250 The X11 Window System.")
3252 (const :format "W32 "
3253 :sibling-args (:help-echo "\
3254 MS Windows.")
3255 w32)
3256 (const :format "NS "
3257 :sibling-args (:help-echo "\
3258 GNUstep or Macintosh OS Cocoa interface.")
3260 (const :format "DOS "
3261 :sibling-args (:help-echo "\
3262 Plain MS-DOS.")
3264 (const :format "TTY%n"
3265 :sibling-args (:help-echo "\
3266 Plain text terminals.")
3267 tty)))
3268 (group :sibling-args (:help-echo "\
3269 Only match the frames with the specified color support.")
3270 (const :format "Class: "
3271 class)
3272 (checklist :inline t
3273 :offset 0
3274 (const :format "Color "
3275 :sibling-args (:help-echo "\
3276 Match color frames.")
3277 color)
3278 (const :format "Grayscale "
3279 :sibling-args (:help-echo "\
3280 Match grayscale frames.")
3281 grayscale)
3282 (const :format "Monochrome%n"
3283 :sibling-args (:help-echo "\
3284 Match frames with no color support.")
3285 mono)))
3286 (group :sibling-args (:help-echo "\
3287 The minimum number of colors the frame should support.")
3288 (const :format "" min-colors)
3289 (integer :tag "Minimum number of colors" ))
3290 (group :sibling-args (:help-echo "\
3291 Only match frames with the specified intensity.")
3292 (const :format "\
3293 Background brightness: "
3294 background)
3295 (checklist :inline t
3296 :offset 0
3297 (const :format "Light "
3298 :sibling-args (:help-echo "\
3299 Match frames with light backgrounds.")
3300 light)
3301 (const :format "Dark\n"
3302 :sibling-args (:help-echo "\
3303 Match frames with dark backgrounds.")
3304 dark)))
3305 (group :sibling-args (:help-echo "\
3306 Only match frames that support the specified face attributes.")
3307 (const :format "Supports attributes:" supports)
3308 (custom-face-edit :inline t :format "%n%v"))))))
3310 ;;; The `custom-face' Widget.
3312 (defface custom-face-tag
3313 '((t :inherit custom-variable-tag))
3314 "Face used for face tags."
3315 :group 'custom-faces)
3317 (defcustom custom-face-default-form 'selected
3318 "Default form of displaying face definition."
3319 :type '(choice (const all)
3320 (const selected)
3321 (const lisp))
3322 :group 'custom-buffer
3323 :version "20.3")
3325 (define-widget 'custom-face 'custom
3326 "Widget for customizing a face.
3327 The following properties have special meanings for this widget:
3329 :value is the face name (a symbol).
3331 :custom-form should be a symbol describing how to display and
3332 edit the face attributes---either `selected' (attributes for
3333 selected display only), `all' (all attributes), `lisp' (as a
3334 Lisp sexp), or `mismatch' (should not happen); if nil, use
3335 the return value of `custom-face-default-form'.
3337 :custom-style describes the widget interface style; nil is the
3338 default style, while `simple' means a simpler interface that
3339 inhibits the magic custom-state widget.
3341 :sample-indent, if non-nil, is the number of columns to which to
3342 indent the face sample (an integer).
3344 :shown-value, if non-nil, is the face spec to display as the value
3345 of the widget, instead of the current face spec."
3346 :sample-face 'custom-face-tag
3347 :help-echo "Set or reset this face."
3348 :documentation-property #'face-doc-string
3349 :value-create 'custom-face-value-create
3350 :action 'custom-face-action
3351 :custom-category 'face
3352 :custom-form nil
3353 :custom-set 'custom-face-set
3354 :custom-mark-to-save 'custom-face-mark-to-save
3355 :custom-reset-current 'custom-redraw
3356 :custom-reset-saved 'custom-face-reset-saved
3357 :custom-reset-standard 'custom-face-reset-standard
3358 :custom-mark-to-reset-standard 'custom-face-mark-to-reset-standard
3359 :custom-standard-value 'custom-face-standard-value
3360 :custom-state-set-and-redraw 'custom-face-state-set-and-redraw
3361 :custom-menu 'custom-face-menu-create)
3363 (define-widget 'custom-face-all 'editable-list
3364 "An editable list of display specifications and attributes."
3365 :entry-format "%i %d %v"
3366 :insert-button-args '(:help-echo "Insert new display specification here.")
3367 :append-button-args '(:help-echo "Append new display specification here.")
3368 :delete-button-args '(:help-echo "Delete this display specification.")
3369 :args '((group :format "%v" custom-display custom-face-edit)))
3371 (defconst custom-face-all (widget-convert 'custom-face-all)
3372 "Converted version of the `custom-face-all' widget.")
3374 (defun custom-filter-face-spec (spec filter-index &optional default-filter)
3375 "Return a canonicalized version of SPEC.
3376 FILTER-INDEX is the index in the entry for each attribute in
3377 `custom-face-attributes' at which the appropriate filter function can be
3378 found, and DEFAULT-FILTER is the filter to apply for attributes that
3379 don't specify one."
3380 (mapcar (lambda (entry)
3381 ;; Filter a single face-spec entry
3382 (let ((tests (car entry))
3383 (unfiltered-attrs
3384 ;; Handle both old- and new-style attribute syntax
3385 (if (listp (car (cdr entry)))
3386 (car (cdr entry))
3387 (cdr entry)))
3388 (filtered-attrs nil))
3389 ;; Filter each face attribute
3390 (while unfiltered-attrs
3391 (let* ((attr (pop unfiltered-attrs))
3392 (pre-filtered-value (pop unfiltered-attrs))
3393 (filter
3394 (or (nth filter-index (assq attr custom-face-attributes))
3395 default-filter))
3396 (filtered-value
3397 (if filter
3398 (funcall filter pre-filtered-value)
3399 pre-filtered-value)))
3400 (push filtered-value filtered-attrs)
3401 (push attr filtered-attrs)))
3403 (list tests filtered-attrs)))
3404 spec))
3406 (defun custom-pre-filter-face-spec (spec)
3407 "Return SPEC changed as necessary for editing by the face customization widget.
3408 SPEC must be a full face spec."
3409 (custom-filter-face-spec spec 2))
3411 (defun custom-post-filter-face-spec (spec)
3412 "Return the customized SPEC in a form suitable for setting the face."
3413 (custom-filter-face-spec spec 3))
3415 (defun custom-face-widget-to-spec (widget)
3416 "Return a face spec corresponding to WIDGET.
3417 WIDGET should be a `custom-face' widget."
3418 (unless (eq (widget-type widget) 'custom-face)
3419 (error "Invalid widget"))
3420 (let ((child (car (widget-get widget :children))))
3421 (custom-post-filter-face-spec
3422 (if (eq (widget-type child) 'custom-face-edit)
3423 `((t ,(widget-value child)))
3424 (widget-value child)))))
3426 (defun custom-face-get-current-spec (face)
3427 (let ((spec (or (get face 'customized-face)
3428 (get face 'saved-face)
3429 (get face 'face-defface-spec)
3430 ;; Attempt to construct it.
3431 `((t ,(custom-face-attributes-get
3432 face (selected-frame)))))))
3433 ;; If the user has changed this face in some other way,
3434 ;; edit it as the user has specified it.
3435 (if (not (face-spec-match-p face spec (selected-frame)))
3436 (setq spec `((t ,(face-attr-construct face (selected-frame))))))
3437 (custom-pre-filter-face-spec spec)))
3439 (defun custom-toggle-hide-face (visibility-widget &rest _ignore)
3440 "Toggle the visibility of a `custom-face' parent widget.
3441 By default, this signals an error if the parent has unsaved
3442 changes. If the parent has a `simple' :custom-style property,
3443 the present value is saved to its :shown-value property instead."
3444 (let ((widget (widget-get visibility-widget :parent)))
3445 (unless (eq (widget-type widget) 'custom-face)
3446 (error "Invalid widget type"))
3447 (custom-load-widget widget)
3448 (let ((state (widget-get widget :custom-state)))
3449 (if (eq state 'hidden)
3450 (widget-put widget :custom-state 'unknown)
3451 ;; In normal interface, widget can't be hidden if modified.
3452 (when (memq state '(invalid modified set))
3453 (if (eq (widget-get widget :custom-style) 'simple)
3454 (widget-put widget :shown-value
3455 (custom-face-widget-to-spec widget))
3456 (error "There are unsaved changes")))
3457 (widget-put widget :documentation-shown nil)
3458 (widget-put widget :custom-state 'hidden))
3459 (custom-redraw widget)
3460 (widget-setup))))
3462 (defun custom-face-value-create (widget)
3463 "Create a list of the display specifications for WIDGET."
3464 (let* ((buttons (widget-get widget :buttons))
3465 (symbol (widget-get widget :value))
3466 (tag (or (widget-get widget :tag)
3467 (prin1-to-string symbol)))
3468 (hiddenp (eq (widget-get widget :custom-state) 'hidden))
3469 (style (widget-get widget :custom-style))
3470 children)
3472 (if (eq custom-buffer-style 'tree)
3474 ;; Draw a tree-style `custom-face' widget
3475 (progn
3476 (insert (widget-get widget :custom-prefix)
3477 (if (widget-get widget :custom-last) " `--- " " |--- "))
3478 (push (widget-create-child-and-convert
3479 widget 'custom-browse-face-tag)
3480 buttons)
3481 (insert " " tag "\n")
3482 (widget-put widget :buttons buttons))
3484 ;; Draw an ordinary `custom-face' widget
3485 (let ((opoint (point)))
3486 ;; Visibility indicator.
3487 (push (widget-create-child-and-convert
3488 widget 'custom-visibility
3489 :help-echo "Hide or show this face."
3490 :on "Hide" :off "Show"
3491 :on-glyph "down" :off-glyph "right"
3492 :action 'custom-toggle-hide-face
3493 (not hiddenp))
3494 buttons)
3495 ;; Face name (tag).
3496 (insert " " tag)
3497 (widget-specify-sample widget opoint (point)))
3498 (insert
3499 (cond ((eq custom-buffer-style 'face) " ")
3500 ((string-match-p "face\\'" tag) ":")
3501 (t " face: ")))
3503 ;; Face sample.
3504 (let ((sample-indent (widget-get widget :sample-indent))
3505 (indent-tabs-mode nil))
3506 (and sample-indent
3507 (<= (current-column) sample-indent)
3508 (indent-to-column sample-indent)))
3509 (push (widget-create-child-and-convert
3510 widget 'item
3511 :format "[%{%t%}]"
3512 :sample-face (let ((spec (widget-get widget :shown-value)))
3513 (if spec (face-spec-choose spec) symbol))
3514 :tag "sample")
3515 buttons)
3516 (insert "\n")
3518 ;; Magic.
3519 (unless (eq (widget-get widget :custom-style) 'simple)
3520 (let ((magic (widget-create-child-and-convert
3521 widget 'custom-magic nil)))
3522 (widget-put widget :custom-magic magic)
3523 (push magic buttons)))
3525 ;; Update buttons.
3526 (widget-put widget :buttons buttons)
3528 ;; Insert documentation.
3529 (unless (and hiddenp (eq style 'simple))
3530 (widget-put widget :documentation-indent 3)
3531 (widget-add-documentation-string-button
3532 widget :visibility-widget 'custom-visibility)
3533 ;; The comment field
3534 (unless hiddenp
3535 (let* ((comment (get symbol 'face-comment))
3536 (comment-widget
3537 (widget-create-child-and-convert
3538 widget 'custom-comment
3539 :parent widget
3540 :value (or comment ""))))
3541 (widget-put widget :comment-widget comment-widget)
3542 (push comment-widget children))))
3544 ;; Editor.
3545 (unless (eq (preceding-char) ?\n)
3546 (insert "\n"))
3547 (unless hiddenp
3548 (custom-load-widget widget)
3549 (unless (widget-get widget :custom-form)
3550 (widget-put widget :custom-form custom-face-default-form))
3552 (let* ((spec (or (widget-get widget :shown-value)
3553 (custom-face-get-current-spec symbol)))
3554 (form (widget-get widget :custom-form))
3555 (indent (widget-get widget :indent))
3556 face-alist face-entry spec-default spec-match editor)
3558 ;; Find a display in SPEC matching the selected display.
3559 ;; This will use the usual face customization interface.
3560 (setq face-alist spec)
3561 (when (eq (car-safe (car-safe face-alist)) 'default)
3562 (setq spec-default (pop face-alist)))
3564 (while (and face-alist (listp face-alist) (null spec-match))
3565 (setq face-entry (car face-alist))
3566 (and (listp face-entry)
3567 (face-spec-set-match-display (car face-entry)
3568 (selected-frame))
3569 (widget-apply custom-face-edit :match (cadr face-entry))
3570 (setq spec-match face-entry))
3571 (setq face-alist (cdr face-alist)))
3573 ;; Insert the appropriate editing widget.
3574 (setq editor
3575 (cond
3576 ((and (eq form 'selected)
3577 (or spec-match spec-default))
3578 (when indent (insert-char ?\s indent))
3579 (widget-create-child-and-convert
3580 widget 'custom-face-edit
3581 :value (cadr spec-match)
3582 :default-face-attributes (cadr spec-default)))
3583 ((and (not (eq form 'lisp))
3584 (widget-apply custom-face-all :match spec))
3585 (widget-create-child-and-convert
3586 widget 'custom-face-all :value spec))
3588 (when indent
3589 (insert-char ?\s indent))
3590 (widget-create-child-and-convert
3591 widget 'sexp :value spec))))
3592 (custom-face-state-set widget)
3593 (push editor children)
3594 (widget-put widget :children children))))))
3596 (defvar custom-face-menu
3597 `(("Set for Current Session" custom-face-set)
3598 ,@(when (or custom-file init-file-user)
3599 '(("Save for Future Sessions" custom-face-save)))
3600 ("Undo Edits" custom-redraw
3601 (lambda (widget)
3602 (memq (widget-get widget :custom-state) '(modified changed))))
3603 ("Revert This Session's Customization" custom-face-reset-saved
3604 (lambda (widget)
3605 (memq (widget-get widget :custom-state) '(modified set changed))))
3606 ,@(when (or custom-file init-file-user)
3607 '(("Erase Customization" custom-face-reset-standard
3608 (lambda (widget)
3609 (get (widget-value widget) 'face-defface-spec)))))
3610 ("---" ignore ignore)
3611 ("Add Comment" custom-comment-show custom-comment-invisible-p)
3612 ("---" ignore ignore)
3613 ("For Current Display" custom-face-edit-selected
3614 (lambda (widget)
3615 (not (eq (widget-get widget :custom-form) 'selected))))
3616 ("For All Kinds of Displays" custom-face-edit-all
3617 (lambda (widget)
3618 (not (eq (widget-get widget :custom-form) 'all))))
3619 ("Show Lisp Expression" custom-face-edit-lisp
3620 (lambda (widget)
3621 (not (eq (widget-get widget :custom-form) 'lisp)))))
3622 "Alist of actions for the `custom-face' widget.
3623 Each entry has the form (NAME ACTION FILTER) where NAME is the name of
3624 the menu entry, ACTION is the function to call on the widget when the
3625 menu is selected, and FILTER is a predicate which takes a `custom-face'
3626 widget as an argument, and returns non-nil if ACTION is valid on that
3627 widget. If FILTER is nil, ACTION is always valid.")
3629 (defun custom-face-edit-selected (widget)
3630 "Edit selected attributes of the value of WIDGET."
3631 (widget-put widget :custom-state 'unknown)
3632 (widget-put widget :custom-form 'selected)
3633 (custom-redraw widget))
3635 (defun custom-face-edit-all (widget)
3636 "Edit all attributes of the value of WIDGET."
3637 (widget-put widget :custom-state 'unknown)
3638 (widget-put widget :custom-form 'all)
3639 (custom-redraw widget))
3641 (defun custom-face-edit-lisp (widget)
3642 "Edit the Lisp representation of the value of WIDGET."
3643 (widget-put widget :custom-state 'unknown)
3644 (widget-put widget :custom-form 'lisp)
3645 (custom-redraw widget))
3647 (defun custom-face-state (face)
3648 "Return the current state of the face FACE.
3649 This is one of `set', `saved', `changed', `themed', or `rogue'."
3650 (let* ((comment (get face 'face-comment))
3651 (state
3652 (cond
3653 ((or (get face 'customized-face)
3654 (get face 'customized-face-comment))
3655 (if (equal (get face 'customized-face-comment) comment)
3656 'set
3657 'changed))
3658 ((or (get face 'saved-face)
3659 (get face 'saved-face-comment))
3660 (cond ((not (equal (get face 'saved-face-comment) comment))
3661 'changed)
3662 ((eq 'user (caar (get face 'theme-face)))
3663 'saved)
3664 ((eq 'changed (caar (get face 'theme-face)))
3665 'changed)
3666 (t 'themed)))
3667 ((get face 'face-defface-spec)
3668 (cond (comment 'changed)
3669 ((get face 'theme-face) 'themed)
3670 (t 'standard)))
3671 (t 'rogue))))
3672 ;; If the user called set-face-attribute to change the default for
3673 ;; new frames, this face is "set outside of Customize".
3674 (if (and (not (eq state 'rogue))
3675 (get face 'face-modified))
3676 'changed
3677 state)))
3679 (defun custom-face-state-set (widget)
3680 "Set the state of WIDGET."
3681 (widget-put widget :custom-state
3682 (custom-face-state (widget-value widget))))
3684 (defun custom-face-action (widget &optional event)
3685 "Show the menu for `custom-face' WIDGET.
3686 Optional EVENT is the location for the menu."
3687 (if (eq (widget-get widget :custom-state) 'hidden)
3688 (custom-toggle-hide widget)
3689 (let* ((completion-ignore-case t)
3690 (symbol (widget-get widget :value))
3691 (answer (widget-choose (concat "Operation on "
3692 (custom-unlispify-tag-name symbol))
3693 (custom-menu-filter custom-face-menu
3694 widget)
3695 event)))
3696 (if answer
3697 (funcall answer widget)))))
3699 (defun custom-face-set (widget)
3700 "Make the face attributes in WIDGET take effect."
3701 (let* ((symbol (widget-value widget))
3702 (value (custom-face-widget-to-spec widget))
3703 (comment-widget (widget-get widget :comment-widget))
3704 (comment (widget-value comment-widget)))
3705 (when (equal comment "")
3706 (setq comment nil)
3707 ;; Make the comment invisible by hand if it's empty
3708 (custom-comment-hide comment-widget))
3709 (custom-push-theme 'theme-face symbol 'user 'set value)
3710 (face-spec-set symbol value 'customized-face)
3711 (put symbol 'face-comment comment)
3712 (put symbol 'customized-face-comment comment)
3713 (custom-face-state-set widget)
3714 (custom-redraw-magic widget)))
3716 (defun custom-face-mark-to-save (widget)
3717 "Mark for saving the face edited by WIDGET."
3718 (let* ((symbol (widget-value widget))
3719 (value (custom-face-widget-to-spec widget))
3720 (comment-widget (widget-get widget :comment-widget))
3721 (comment (widget-value comment-widget))
3722 (standard (eq (widget-get widget :custom-state) 'standard)))
3723 (when (equal comment "")
3724 (setq comment nil)
3725 ;; Make the comment invisible by hand if it's empty
3726 (custom-comment-hide comment-widget))
3727 (custom-push-theme 'theme-face symbol 'user 'set value)
3728 (face-spec-set symbol value (if standard 'reset 'saved-face))
3729 (put symbol 'face-comment comment)
3730 (put symbol 'customized-face-comment nil)
3731 (put symbol 'saved-face-comment comment)))
3733 (defsubst custom-face-state-set-and-redraw (widget)
3734 "Set state of face widget WIDGET and redraw with current settings."
3735 (custom-face-state-set widget)
3736 (custom-redraw-magic widget))
3738 (defun custom-face-save (widget)
3739 "Save the face edited by WIDGET."
3740 (custom-face-mark-to-save widget)
3741 (custom-save-all)
3742 (custom-face-state-set-and-redraw widget))
3744 ;; For backward compatibility.
3745 (define-obsolete-function-alias 'custom-face-save-command 'custom-face-save
3746 "22.1")
3748 (defun custom-face-reset-saved (widget)
3749 "Restore WIDGET to the face's default attributes.
3750 If there is a saved face, restore it; otherwise reset to the
3751 uncustomized (themed or standard) face."
3752 (let* ((face (widget-value widget))
3753 (child (car (widget-get widget :children)))
3754 (saved-face (get face 'saved-face))
3755 (comment (get face 'saved-face-comment))
3756 (comment-widget (widget-get widget :comment-widget)))
3757 (custom-push-theme 'theme-face face 'user
3758 (if saved-face 'set 'reset)
3759 saved-face)
3760 (face-spec-set face saved-face 'saved-face)
3761 (put face 'face-comment comment)
3762 (put face 'customized-face-comment nil)
3763 (widget-value-set child saved-face)
3764 ;; This call manages the comment visibility
3765 (widget-value-set comment-widget (or comment ""))
3766 (custom-face-state-set widget)
3767 (custom-redraw widget)))
3769 (defun custom-face-standard-value (widget)
3770 (get (widget-value widget) 'face-defface-spec))
3772 (defun custom-face-mark-to-reset-standard (widget)
3773 "Restore widget WIDGET to the face's standard attribute values.
3774 If `custom-reset-standard-faces-list' is nil, save, reset and
3775 redraw the widget immediately."
3776 (let* ((symbol (widget-value widget))
3777 (child (car (widget-get widget :children)))
3778 (value (get symbol 'face-defface-spec))
3779 (comment-widget (widget-get widget :comment-widget)))
3780 (unless value
3781 (user-error "No standard setting for this face"))
3782 (custom-push-theme 'theme-face symbol 'user 'reset)
3783 (face-spec-set symbol value 'reset)
3784 (put symbol 'face-comment nil)
3785 (put symbol 'customized-face-comment nil)
3786 (if (and custom-reset-standard-faces-list
3787 (or (get symbol 'saved-face) (get symbol 'saved-face-comment)))
3788 ;; Do this later.
3789 (progn
3790 (put symbol 'saved-face nil)
3791 (put symbol 'saved-face-comment nil)
3792 ;; Append this to `custom-reset-standard-faces-list' and have
3793 ;; `custom-reset-standard-save-and-update' save setting to the
3794 ;; file, update the widget's state, and redraw it.
3795 (setq custom-reset-standard-faces-list
3796 (cons widget custom-reset-standard-faces-list)))
3797 (when (or (get symbol 'saved-face) (get symbol 'saved-face-comment))
3798 (put symbol 'saved-face nil)
3799 (put symbol 'saved-face-comment nil)
3800 (custom-save-all))
3801 (widget-value-set child
3802 (custom-pre-filter-face-spec
3803 (list (list t (custom-face-attributes-get
3804 symbol nil)))))
3805 ;; This call manages the comment visibility
3806 (widget-value-set comment-widget "")
3807 (custom-face-state-set widget)
3808 (custom-redraw-magic widget))))
3810 (defun custom-face-reset-standard (widget)
3811 "Restore WIDGET to the face's standard attribute values.
3812 This operation eliminates any saved attributes for the face,
3813 restoring it to the state of a face that has never been customized."
3814 (let (custom-reset-standard-faces-list)
3815 (custom-face-mark-to-reset-standard widget)))
3817 ;;; The `face' Widget.
3819 (defvar widget-face-prompt-value-history nil
3820 "History of input to `widget-face-prompt-value'.")
3822 (define-widget 'face 'symbol
3823 "A Lisp face name (with sample)."
3824 :format "%{%t%}: (%{sample%}) %v"
3825 :tag "Face"
3826 :value 'default
3827 :sample-face-get 'widget-face-sample-face-get
3828 :notify 'widget-face-notify
3829 :match (lambda (_widget value) (facep value))
3830 :completions (apply-partially #'completion-table-with-predicate
3831 obarray #'facep 'strict)
3832 :prompt-match 'facep
3833 :prompt-history 'widget-face-prompt-value-history
3834 :validate (lambda (widget)
3835 (unless (facep (widget-value widget))
3836 (widget-put widget
3837 :error (format "Invalid face: %S"
3838 (widget-value widget)))
3839 widget)))
3841 (defun widget-face-sample-face-get (widget)
3842 (let ((value (widget-value widget)))
3843 (if (facep value)
3844 value
3845 'default)))
3847 (defun widget-face-notify (widget child &optional event)
3848 "Update the sample, and notify the parent."
3849 (overlay-put (widget-get widget :sample-overlay)
3850 'face (widget-apply widget :sample-face-get))
3851 (widget-default-notify widget child event))
3854 ;;; The `hook' Widget.
3856 (define-widget 'hook 'list
3857 "An Emacs Lisp hook."
3858 :value-to-internal (lambda (_widget value)
3859 (if (and value (symbolp value))
3860 (list value)
3861 value))
3862 :match (lambda (widget value)
3863 (or (symbolp value)
3864 (widget-group-match widget value)))
3865 ;; Avoid adding undefined functions to the hook, especially for
3866 ;; things like `find-file-hook' or even more basic ones, to avoid
3867 ;; chaos.
3868 :set (lambda (symbol value)
3869 (dolist (elt value)
3870 (if (fboundp elt)
3871 (add-hook symbol elt))))
3872 :convert-widget 'custom-hook-convert-widget
3873 :tag "Hook")
3875 (defun custom-hook-convert-widget (widget)
3876 ;; Handle `:options'.
3877 (let* ((options (widget-get widget :options))
3878 (other `(editable-list :inline t
3879 :entry-format "%i %d%v"
3880 (function :format " %v")))
3881 (args (if options
3882 (list `(checklist :inline t
3883 ,@(mapcar (lambda (entry)
3884 `(function-item ,entry))
3885 options))
3886 other)
3887 (list other))))
3888 (widget-put widget :args args)
3889 widget))
3891 ;;; The `custom-group-link' Widget.
3893 (define-widget 'custom-group-link 'link
3894 "Show parent in other window when activated."
3895 :button-face 'custom-link
3896 :mouse-face 'highlight
3897 :pressed-face 'highlight
3898 :help-echo "Create customization buffer for this group."
3899 :keymap custom-mode-link-map
3900 :follow-link 'mouse-face
3901 :action 'custom-group-link-action)
3903 (defun custom-group-link-action (widget &rest _ignore)
3904 (customize-group (widget-value widget)))
3906 ;;; The `custom-group' Widget.
3908 (defcustom custom-group-tag-faces nil
3909 "Face used for group tags.
3910 The first member is used for level 1 groups, the second for level 2,
3911 and so forth. The remaining group tags are shown with `custom-group-tag'."
3912 :type '(repeat face)
3913 :group 'custom-faces)
3915 (defface custom-group-tag-1
3916 '((default :weight bold :height 1.2 :inherit variable-pitch)
3917 (((class color) (background dark)) :foreground "pink")
3918 (((min-colors 88) (class color) (background light)) :foreground "red1")
3919 (((class color) (background light)) :foreground "red"))
3920 "Face for group tags."
3921 :group 'custom-faces)
3923 (defface custom-group-tag
3924 '((default :weight bold :height 1.2 :inherit variable-pitch)
3925 (((class color) (background dark)) :foreground "light blue")
3926 (((min-colors 88) (class color) (background light)) :foreground "blue1")
3927 (((class color) (background light)) :foreground "blue")
3928 (t :weight bold))
3929 "Face for low level group tags."
3930 :group 'custom-faces)
3932 (defface custom-group-subtitle
3933 '((t :weight bold))
3934 "Face for the \"Subgroups:\" subtitle in Custom buffers."
3935 :group 'custom-faces)
3937 (defvar custom-group-doc-align-col 20)
3939 (define-widget 'custom-group 'custom
3940 "Customize group."
3941 :format "%v"
3942 :sample-face-get 'custom-group-sample-face-get
3943 :documentation-property 'group-documentation
3944 :help-echo "Set or reset all members of this group."
3945 :value-create 'custom-group-value-create
3946 :action 'custom-group-action
3947 :custom-category 'group
3948 :custom-set 'custom-group-set
3949 :custom-mark-to-save 'custom-group-mark-to-save
3950 :custom-reset-current 'custom-group-reset-current
3951 :custom-reset-saved 'custom-group-reset-saved
3952 :custom-reset-standard 'custom-group-reset-standard
3953 :custom-mark-to-reset-standard 'custom-group-mark-to-reset-standard
3954 :custom-state-set-and-redraw 'custom-group-state-set-and-redraw
3955 :custom-menu 'custom-group-menu-create)
3957 (defun custom-group-sample-face-get (widget)
3958 ;; Use :sample-face.
3959 (or (nth (1- (widget-get widget :custom-level)) custom-group-tag-faces)
3960 'custom-group-tag))
3962 (define-widget 'custom-group-visibility 'visibility
3963 "An indicator and manipulator for hidden group contents."
3964 :create 'custom-group-visibility-create)
3966 (defun custom-group-visibility-create (widget)
3967 (let ((visible (widget-value widget)))
3968 (if visible
3969 (insert "--------")))
3970 (widget-default-create widget))
3972 (defun custom-group-members (symbol groups-only)
3973 "Return SYMBOL's custom group members.
3974 If GROUPS-ONLY is non-nil, return only those members that are groups."
3975 (if (not groups-only)
3976 (get symbol 'custom-group)
3977 (let (members)
3978 (dolist (entry (get symbol 'custom-group))
3979 (when (eq (nth 1 entry) 'custom-group)
3980 (push entry members)))
3981 (nreverse members))))
3983 (defun custom-group-value-create (widget)
3984 "Insert a customize group for WIDGET in the current buffer."
3985 (unless (eq (widget-get widget :custom-state) 'hidden)
3986 (custom-load-widget widget))
3987 (let* ((state (widget-get widget :custom-state))
3988 (level (widget-get widget :custom-level))
3989 ;; (indent (widget-get widget :indent))
3990 (prefix (widget-get widget :custom-prefix))
3991 (buttons (widget-get widget :buttons))
3992 (tag (substitute-command-keys (widget-get widget :tag)))
3993 (symbol (widget-value widget))
3994 (members (custom-group-members symbol
3995 (and (eq custom-buffer-style 'tree)
3996 custom-browse-only-groups)))
3997 (doc (substitute-command-keys (widget-docstring widget))))
3998 (cond ((and (eq custom-buffer-style 'tree)
3999 (eq state 'hidden)
4000 (or members (custom-unloaded-widget-p widget)))
4001 (custom-browse-insert-prefix prefix)
4002 (push (widget-create-child-and-convert
4003 widget 'custom-browse-visibility
4004 :tag "+")
4005 buttons)
4006 (insert "-- ")
4007 (push (widget-create-child-and-convert
4008 widget 'custom-browse-group-tag)
4009 buttons)
4010 (insert " " tag "\n")
4011 (widget-put widget :buttons buttons))
4012 ((and (eq custom-buffer-style 'tree)
4013 (zerop (length members)))
4014 (custom-browse-insert-prefix prefix)
4015 (insert "[ ]-- ")
4016 (push (widget-create-child-and-convert
4017 widget 'custom-browse-group-tag)
4018 buttons)
4019 (insert " " tag "\n")
4020 (widget-put widget :buttons buttons))
4021 ((eq custom-buffer-style 'tree)
4022 (custom-browse-insert-prefix prefix)
4023 (if (zerop (length members))
4024 (progn
4025 (custom-browse-insert-prefix prefix)
4026 (insert "[ ]-- ")
4027 ;; (widget-glyph-insert nil "[ ]" "empty")
4028 ;; (widget-glyph-insert nil "-- " "horizontal")
4029 (push (widget-create-child-and-convert
4030 widget 'custom-browse-group-tag)
4031 buttons)
4032 (insert " " tag "\n")
4033 (widget-put widget :buttons buttons))
4034 (push (widget-create-child-and-convert
4035 widget 'custom-browse-visibility
4036 ;; :tag-glyph "minus"
4037 :tag "-")
4038 buttons)
4039 (insert "-\\ ")
4040 ;; (widget-glyph-insert nil "-\\ " "top")
4041 (push (widget-create-child-and-convert
4042 widget 'custom-browse-group-tag)
4043 buttons)
4044 (insert " " tag "\n")
4045 (widget-put widget :buttons buttons)
4046 (message "Creating group...")
4047 (let* ((members (custom-sort-items
4048 members
4049 ;; Never sort the top-level custom group.
4050 (unless (eq symbol 'emacs)
4051 custom-browse-sort-alphabetically)
4052 custom-browse-order-groups))
4053 (prefixes (widget-get widget :custom-prefixes))
4054 (custom-prefix-list (custom-prefix-add symbol prefixes))
4055 (extra-prefix (if (widget-get widget :custom-last)
4057 " | "))
4058 (prefix (concat prefix extra-prefix))
4059 children entry)
4060 (while members
4061 (setq entry (car members)
4062 members (cdr members))
4063 (push (widget-create-child-and-convert
4064 widget (nth 1 entry)
4065 :group widget
4066 :tag (custom-unlispify-tag-name (nth 0 entry))
4067 :custom-prefixes custom-prefix-list
4068 :custom-level (1+ level)
4069 :custom-last (null members)
4070 :value (nth 0 entry)
4071 :custom-prefix prefix)
4072 children))
4073 (widget-put widget :children (reverse children)))
4074 (message "Creating group...done")))
4075 ;; Nested style.
4076 ((eq state 'hidden)
4077 ;; Create level indicator.
4078 ;; Create tag.
4079 (if (eq custom-buffer-style 'links)
4080 (push (widget-create-child-and-convert
4081 widget 'custom-group-link
4082 :tag tag
4083 symbol)
4084 buttons)
4085 (insert-char ?\s (* custom-buffer-indent (1- level)))
4086 (insert "-- ")
4087 (push (widget-create-child-and-convert
4088 widget 'custom-group-visibility
4089 :help-echo "Show members of this group."
4090 :action 'custom-toggle-parent
4091 (not (eq state 'hidden)))
4092 buttons))
4093 (if (>= (current-column) custom-group-doc-align-col)
4094 (insert " "))
4095 ;; Create magic button.
4096 (let ((magic (widget-create-child-and-convert
4097 widget 'custom-magic nil)))
4098 (widget-put widget :custom-magic magic)
4099 (push magic buttons))
4100 ;; Update buttons.
4101 (widget-put widget :buttons buttons)
4102 ;; Insert documentation.
4103 (if (and (eq custom-buffer-style 'links) (> level 1))
4104 (widget-put widget :documentation-indent
4105 custom-group-doc-align-col))
4106 (widget-add-documentation-string-button
4107 widget :visibility-widget 'custom-visibility))
4109 ;; Nested style.
4110 (t ;Visible.
4111 ;; Draw a horizontal line (this works for both graphical
4112 ;; and text displays):
4113 (let ((p (point)))
4114 (insert "\n")
4115 (put-text-property p (1+ p) 'face '(:underline t))
4116 (overlay-put (make-overlay p (1+ p))
4117 'before-string
4118 (propertize "\n" 'face '(:underline t)
4119 'display '(space :align-to 999))))
4121 ;; Add parent groups references above the group.
4122 (when (eq level 1)
4123 (if (custom-add-parent-links widget "Parent groups:")
4124 (insert "\n")))
4125 (insert-char ?\s (* custom-buffer-indent (1- level)))
4126 ;; Create tag.
4127 (let ((start (point)))
4128 (insert tag " group: ")
4129 (widget-specify-sample widget start (point)))
4130 (cond
4131 ((not doc)
4132 (insert " Group definition missing. "))
4133 ((< (length doc) 50)
4134 (insert doc)))
4135 ;; Create visibility indicator.
4136 (unless (eq custom-buffer-style 'links)
4137 (insert "--------")
4138 (push (widget-create-child-and-convert
4139 widget 'visibility
4140 :help-echo "Hide members of this group."
4141 :action 'custom-toggle-parent
4142 (not (eq state 'hidden)))
4143 buttons)
4144 (insert " "))
4145 (insert "\n")
4146 ;; Create magic button.
4147 (let ((magic (widget-create-child-and-convert
4148 widget 'custom-magic
4149 :indent 0
4150 nil)))
4151 (widget-put widget :custom-magic magic)
4152 (push magic buttons))
4153 ;; Update buttons.
4154 (widget-put widget :buttons buttons)
4155 ;; Insert documentation.
4156 (when (and doc (>= (length doc) 50))
4157 (widget-add-documentation-string-button
4158 widget :visibility-widget 'custom-visibility))
4160 ;; Parent groups.
4161 (if nil ;;; This should test that the buffer
4162 ;;; was not made to display a group.
4163 (when (eq level 1)
4164 (insert-char ?\s custom-buffer-indent)
4165 (custom-add-parent-links widget)))
4166 (custom-add-see-also widget
4167 (make-string (* custom-buffer-indent level)
4168 ?\s))
4169 ;; Members.
4170 (message "Creating group...")
4171 (let* ((members (custom-sort-items
4172 members
4173 ;; Never sort the top-level custom group.
4174 (unless (eq symbol 'emacs)
4175 custom-buffer-sort-alphabetically)
4176 custom-buffer-order-groups))
4177 (prefixes (widget-get widget :custom-prefixes))
4178 (custom-prefix-list (custom-prefix-add symbol prefixes))
4179 (len (length members))
4180 (count 0)
4181 (reporter (make-progress-reporter
4182 "Creating group entries..." 0 len))
4183 (have-subtitle (and (not (eq symbol 'emacs))
4184 (eq custom-buffer-order-groups 'last)))
4185 prev-type
4186 children)
4188 (dolist (entry members)
4189 (unless (eq prev-type 'custom-group)
4190 (widget-insert "\n"))
4191 (progress-reporter-update reporter (setq count (1+ count)))
4192 (let ((sym (nth 0 entry))
4193 (type (nth 1 entry)))
4194 (when (and have-subtitle (eq type 'custom-group))
4195 (setq have-subtitle nil)
4196 (widget-insert
4197 (propertize "Subgroups:\n" 'face 'custom-group-subtitle)))
4198 (setq prev-type type)
4199 (push (widget-create-child-and-convert
4200 widget type
4201 :group widget
4202 :tag (custom-unlispify-tag-name sym)
4203 :custom-prefixes custom-prefix-list
4204 :custom-level (1+ level)
4205 :value sym)
4206 children)
4207 (unless (eq (preceding-char) ?\n)
4208 (widget-insert "\n"))))
4210 (setq children (nreverse children))
4211 (mapc 'custom-magic-reset children)
4212 (widget-put widget :children children)
4213 (custom-group-state-update widget)
4214 (progress-reporter-done reporter))
4215 ;; End line
4216 (let ((p (1+ (point))))
4217 (insert "\n\n")
4218 (put-text-property p (1+ p) 'face '(:underline t))
4219 (overlay-put (make-overlay p (1+ p))
4220 'before-string
4221 (propertize "\n" 'face '(:underline t)
4222 'display '(space :align-to 999))))))))
4224 (defvar custom-group-menu
4225 `(("Set for Current Session" custom-group-set
4226 (lambda (widget)
4227 (eq (widget-get widget :custom-state) 'modified)))
4228 ,@(when (or custom-file init-file-user)
4229 '(("Save for Future Sessions" custom-group-save
4230 (lambda (widget)
4231 (memq (widget-get widget :custom-state) '(modified set))))))
4232 ("Undo Edits" custom-group-reset-current
4233 (lambda (widget)
4234 (memq (widget-get widget :custom-state) '(modified))))
4235 ("Revert This Session's Customizations" custom-group-reset-saved
4236 (lambda (widget)
4237 (memq (widget-get widget :custom-state) '(modified set))))
4238 ,@(when (or custom-file init-file-user)
4239 '(("Erase Customization" custom-group-reset-standard
4240 (lambda (widget)
4241 (memq (widget-get widget :custom-state) '(modified set saved)))))))
4242 "Alist of actions for the `custom-group' widget.
4243 Each entry has the form (NAME ACTION FILTER) where NAME is the name of
4244 the menu entry, ACTION is the function to call on the widget when the
4245 menu is selected, and FILTER is a predicate which takes a `custom-group'
4246 widget as an argument, and returns non-nil if ACTION is valid on that
4247 widget. If FILTER is nil, ACTION is always valid.")
4249 (defun custom-group-action (widget &optional event)
4250 "Show the menu for `custom-group' WIDGET.
4251 Optional EVENT is the location for the menu."
4252 (if (eq (widget-get widget :custom-state) 'hidden)
4253 (custom-toggle-hide widget)
4254 (let* ((completion-ignore-case t)
4255 (answer (widget-choose (concat "Operation on "
4256 (custom-unlispify-tag-name
4257 (widget-get widget :value)))
4258 (custom-menu-filter custom-group-menu
4259 widget)
4260 event)))
4261 (if answer
4262 (funcall answer widget)))))
4264 (defun custom-group-set (widget)
4265 "Set changes in all modified group members."
4266 (dolist (child (widget-get widget :children))
4267 (when (eq (widget-get child :custom-state) 'modified)
4268 (widget-apply child :custom-set))))
4270 (defun custom-group-mark-to-save (widget)
4271 "Mark all modified group members for saving."
4272 (dolist (child (widget-get widget :children))
4273 (when (memq (widget-get child :custom-state) '(modified set))
4274 (widget-apply child :custom-mark-to-save))))
4276 (defsubst custom-group-state-set-and-redraw (widget)
4277 "Set state of group widget WIDGET and redraw with current settings."
4278 (dolist (child (widget-get widget :children))
4279 (when (memq (widget-get child :custom-state) '(modified set))
4280 (widget-apply child :custom-state-set-and-redraw))))
4282 (defun custom-group-save (widget)
4283 "Save all modified group members."
4284 (custom-group-mark-to-save widget)
4285 (custom-save-all)
4286 (custom-group-state-set-and-redraw widget))
4288 (defun custom-group-reset-current (widget)
4289 "Reset all modified group members."
4290 (dolist (child (widget-get widget :children))
4291 (when (eq (widget-get child :custom-state) 'modified)
4292 (widget-apply child :custom-reset-current))))
4294 (defun custom-group-reset-saved (widget)
4295 "Reset all modified or set group members."
4296 (dolist (child (widget-get widget :children))
4297 (when (memq (widget-get child :custom-state) '(modified set))
4298 (widget-apply child :custom-reset-saved))))
4300 (defun custom-group-reset-standard (widget)
4301 "Reset all modified, set, or saved group members."
4302 (let ((custom-reset-standard-variables-list '(t))
4303 (custom-reset-standard-faces-list '(t)))
4304 (custom-group-mark-to-reset-standard widget)
4305 (custom-reset-standard-save-and-update)))
4307 (defun custom-group-mark-to-reset-standard (widget)
4308 "Mark to reset all modified, set, or saved group members."
4309 (dolist (child (widget-get widget :children))
4310 (when (memq (widget-get child :custom-state)
4311 '(modified set saved))
4312 (widget-apply child :custom-mark-to-reset-standard))))
4314 (defun custom-group-state-update (widget)
4315 "Update magic."
4316 (unless (eq (widget-get widget :custom-state) 'hidden)
4317 (let* ((children (widget-get widget :children))
4318 (states (mapcar (lambda (child)
4319 (widget-get child :custom-state))
4320 children))
4321 (magics custom-magic-alist)
4322 (found 'standard))
4323 (while magics
4324 (let ((magic (car (car magics))))
4325 (if (and (not (eq magic 'hidden))
4326 (memq magic states))
4327 (setq found magic
4328 magics nil)
4329 (setq magics (cdr magics)))))
4330 (widget-put widget :custom-state found)))
4331 (custom-magic-reset widget))
4333 ;;; Reading and writing the custom file.
4335 ;;;###autoload
4336 (defcustom custom-file nil
4337 "File used for storing customization information.
4338 The default is nil, which means to use your init file
4339 as specified by `user-init-file'. If the value is not nil,
4340 it should be an absolute file name.
4342 You can set this option through Custom, if you carefully read the
4343 last paragraph below. However, usually it is simpler to write
4344 something like the following in your init file:
4346 \(setq custom-file \"~/.emacs-custom.el\")
4347 \(load custom-file)
4349 Note that both lines are necessary: the first line tells Custom to
4350 save all customizations in this file, but does not load it.
4352 When you change this variable outside Custom, look in the
4353 previous custom file (usually your init file) for the
4354 forms `(custom-set-variables ...)' and `(custom-set-faces ...)',
4355 and copy them (whichever ones you find) to the new custom file.
4356 This will preserve your existing customizations.
4358 If you save this option using Custom, Custom will write all
4359 currently saved customizations, including the new one for this
4360 option itself, into the file you specify, overwriting any
4361 `custom-set-variables' and `custom-set-faces' forms already
4362 present in that file. It will not delete any customizations from
4363 the old custom file. You should do that manually if that is what you
4364 want. You also have to put something like (load \"CUSTOM-FILE\")
4365 in your init file, where CUSTOM-FILE is the actual name of the
4366 file. Otherwise, Emacs will not load the file when it starts up,
4367 and hence will not set `custom-file' to that file either."
4368 :type '(choice (const :tag "Your Emacs init file" nil)
4369 (file :format "%t:%v%d"
4370 :doc
4371 "Please read entire docstring below before setting \
4372 this through Custom.
4373 Click on \"More\" (or position point there and press RETURN)
4374 if only the first line of the docstring is shown."))
4375 :group 'customize)
4377 (defun custom-file (&optional no-error)
4378 "Return the file name for saving customizations."
4379 (if (or (null user-init-file)
4380 (and (null custom-file) init-file-had-error))
4381 ;; Started with -q, i.e. the file containing Custom settings
4382 ;; hasn't been read. Saving settings there won't make much
4383 ;; sense.
4384 (if no-error
4386 (user-error "Saving settings from \"emacs -q\" would overwrite existing customizations"))
4387 (file-chase-links (or custom-file user-init-file))))
4389 ;; If recentf-mode is non-nil, this is defined.
4390 (declare-function recentf-expand-file-name "recentf" (name))
4392 ;;;###autoload
4393 (defun custom-save-all ()
4394 "Save all customizations in `custom-file'."
4395 (when (and (null custom-file) init-file-had-error)
4396 (error "Cannot save customizations; init file was not fully loaded"))
4397 (let* ((filename (custom-file))
4398 (recentf-exclude
4399 (if recentf-mode
4400 (cons (concat "\\`"
4401 (regexp-quote
4402 (recentf-expand-file-name (custom-file)))
4403 "\\'")
4404 recentf-exclude)))
4405 (old-buffer (find-buffer-visiting filename))
4406 old-buffer-name)
4408 (with-current-buffer (let ((find-file-visit-truename t))
4409 (or old-buffer
4410 (let ((delay-mode-hooks t))
4411 (find-file-noselect filename))))
4412 ;; We'll save using file-precious-flag, so avoid destroying
4413 ;; symlinks. (If we're not already visiting the buffer, this is
4414 ;; handled by find-file-visit-truename, above.)
4415 (when old-buffer
4416 (setq old-buffer-name (buffer-file-name))
4417 (set-visited-file-name (file-chase-links filename)))
4419 (unless (eq major-mode 'emacs-lisp-mode)
4420 (delay-mode-hooks (emacs-lisp-mode)))
4421 (let ((inhibit-read-only t)
4422 (print-length nil)
4423 (print-level nil))
4424 (custom-save-variables)
4425 (custom-save-faces))
4426 (let ((file-precious-flag t))
4427 (save-buffer))
4428 (if old-buffer
4429 (progn
4430 (set-visited-file-name old-buffer-name)
4431 (set-buffer-modified-p nil))
4432 (kill-buffer (current-buffer))))))
4434 ;;;###autoload
4435 (defun customize-save-customized ()
4436 "Save all user options which have been set in this session."
4437 (interactive)
4438 (mapatoms (lambda (symbol)
4439 (let ((face (get symbol 'customized-face))
4440 (value (get symbol 'customized-value))
4441 (face-comment (get symbol 'customized-face-comment))
4442 (variable-comment
4443 (get symbol 'customized-variable-comment)))
4444 (when face
4445 (put symbol 'saved-face face)
4446 (custom-push-theme 'theme-face symbol 'user 'set value)
4447 (put symbol 'customized-face nil))
4448 (when value
4449 (put symbol 'saved-value value)
4450 (custom-push-theme 'theme-value symbol 'user 'set value)
4451 (put symbol 'customized-value nil))
4452 (when variable-comment
4453 (put symbol 'saved-variable-comment variable-comment)
4454 (put symbol 'customized-variable-comment nil))
4455 (when face-comment
4456 (put symbol 'saved-face-comment face-comment)
4457 (put symbol 'customized-face-comment nil)))))
4458 ;; We really should update all custom buffers here.
4459 (custom-save-all))
4461 ;; Editing the custom file contents in a buffer.
4463 (defun custom-save-delete (symbol)
4464 "Delete all calls to SYMBOL from the contents of the current buffer.
4465 Leave point at the old location of the first such call,
4466 or (if there were none) at the end of the buffer.
4468 This function does not save the buffer."
4469 (goto-char (point-min))
4470 ;; Skip all whitespace and comments.
4471 (while (forward-comment 1))
4472 (or (eobp)
4473 (save-excursion (forward-sexp (buffer-size)))) ; Test for scan errors.
4474 (let (first)
4475 (catch 'found
4476 (while t ;; We exit this loop only via throw.
4477 ;; Skip all whitespace and comments.
4478 (while (forward-comment 1))
4479 (let ((start (point))
4480 (sexp (condition-case nil
4481 (read (current-buffer))
4482 (end-of-file (throw 'found nil)))))
4483 (when (and (listp sexp)
4484 (eq (car sexp) symbol))
4485 (delete-region start (point))
4486 (unless first
4487 (setq first (point)))))))
4488 (if first
4489 (goto-char first)
4490 ;; Move in front of local variables, otherwise long Custom
4491 ;; entries would make them ineffective.
4492 (let ((pos (point-max))
4493 (case-fold-search t))
4494 (save-excursion
4495 (goto-char (point-max))
4496 (search-backward "\n\^L" (max (- (point-max) 3000) (point-min))
4497 'move)
4498 (when (search-forward "Local Variables:" nil t)
4499 (setq pos (line-beginning-position))))
4500 (goto-char pos)))))
4502 (defvar sort-fold-case) ; defined in sort.el
4504 (defun custom-save-variables ()
4505 "Save all customized variables in `custom-file'."
4506 (save-excursion
4507 (custom-save-delete 'custom-set-variables)
4508 (let ((standard-output (current-buffer))
4509 (saved-list (make-list 1 0))
4510 sort-fold-case)
4511 ;; First create a sorted list of saved variables.
4512 (mapatoms
4513 (lambda (symbol)
4514 (if (and (get symbol 'saved-value)
4515 ;; ignore theme values
4516 (or (null (get symbol 'theme-value))
4517 (eq 'user (caar (get symbol 'theme-value)))))
4518 (nconc saved-list (list symbol)))))
4519 (setq saved-list (sort (cdr saved-list) 'string<))
4520 (unless (bolp)
4521 (princ "\n"))
4522 (princ "(custom-set-variables
4523 ;; custom-set-variables was added by Custom.
4524 ;; If you edit it by hand, you could mess it up, so be careful.
4525 ;; Your init file should contain only one such instance.
4526 ;; If there is more than one, they won't work right.\n")
4527 (dolist (symbol saved-list)
4528 (let ((spec (car-safe (get symbol 'theme-value)))
4529 (value (get symbol 'saved-value))
4530 (requests (get symbol 'custom-requests))
4531 (now (and (not (custom-variable-p symbol))
4532 (or (boundp symbol)
4533 (eq (get symbol 'force-value)
4534 'rogue))))
4535 (comment (get symbol 'saved-variable-comment)))
4536 ;; Check REQUESTS for validity.
4537 (dolist (request requests)
4538 (when (and (symbolp request) (not (featurep request)))
4539 (message "Unknown requested feature: %s" request)
4540 (setq requests (delq request requests))))
4541 ;; Is there anything customized about this variable?
4542 (when (or (and spec (eq (car spec) 'user))
4543 comment
4544 (and (null spec) (get symbol 'saved-value)))
4545 ;; Output an element for this variable.
4546 ;; It has the form (SYMBOL VALUE-FORM NOW REQUESTS COMMENT).
4547 ;; SYMBOL is the variable name.
4548 ;; VALUE-FORM is an expression to return the customized value.
4549 ;; NOW if non-nil means always set the variable immediately
4550 ;; when the customizations are reloaded. This is used
4551 ;; for rogue variables
4552 ;; REQUESTS is a list of packages to load before setting the
4553 ;; variable. Each element of it will be passed to `require'.
4554 ;; COMMENT is whatever comment the user has specified
4555 ;; with the customize facility.
4556 (unless (bolp)
4557 (princ "\n"))
4558 (princ " '(")
4559 (prin1 symbol)
4560 (princ " ")
4561 (let ((val (prin1-to-string (car value))))
4562 (if (< (length val) 60)
4563 (insert val)
4564 (newline-and-indent)
4565 (let ((beginning-of-val (point)))
4566 (insert val)
4567 (save-excursion
4568 (goto-char beginning-of-val)
4569 (indent-pp-sexp 1)))))
4570 (when (or now requests comment)
4571 (princ " ")
4572 (prin1 now)
4573 (when (or requests comment)
4574 (princ " ")
4575 (prin1 requests)
4576 (when comment
4577 (princ " ")
4578 (prin1 comment))))
4579 (princ ")"))))
4580 (if (bolp)
4581 (princ " "))
4582 (princ ")")
4583 (when (/= (following-char) ?\n)
4584 (princ "\n")))))
4586 (defun custom-save-faces ()
4587 "Save all customized faces in `custom-file'."
4588 (save-excursion
4589 (custom-save-delete 'custom-reset-faces)
4590 (custom-save-delete 'custom-set-faces)
4591 (let ((standard-output (current-buffer))
4592 (saved-list (make-list 1 0))
4593 sort-fold-case)
4594 ;; First create a sorted list of saved faces.
4595 (mapatoms
4596 (lambda (symbol)
4597 (if (and (get symbol 'saved-face)
4598 (eq 'user (car (car-safe (get symbol 'theme-face)))))
4599 (nconc saved-list (list symbol)))))
4600 (setq saved-list (sort (cdr saved-list) 'string<))
4601 ;; The default face must be first, since it affects the others.
4602 (if (memq 'default saved-list)
4603 (setq saved-list (cons 'default (delq 'default saved-list))))
4604 (unless (bolp)
4605 (princ "\n"))
4606 (princ "(custom-set-faces
4607 ;; custom-set-faces was added by Custom.
4608 ;; If you edit it by hand, you could mess it up, so be careful.
4609 ;; Your init file should contain only one such instance.
4610 ;; If there is more than one, they won't work right.\n")
4611 (dolist (symbol saved-list)
4612 (let ((spec (car-safe (get symbol 'theme-face)))
4613 (value (get symbol 'saved-face))
4614 (now (not (or (get symbol 'face-defface-spec)
4615 (and (not (custom-facep symbol))
4616 (not (get symbol 'force-face))))))
4617 (comment (get symbol 'saved-face-comment)))
4618 (when (or (and spec (eq (nth 0 spec) 'user))
4619 comment
4620 (and (null spec) (get symbol 'saved-face)))
4621 ;; Don't print default face here.
4622 (unless (bolp)
4623 (princ "\n"))
4624 (princ " '(")
4625 (prin1 symbol)
4626 (princ " ")
4627 (prin1 value)
4628 (when (or now comment)
4629 (princ " ")
4630 (prin1 now)
4631 (when comment
4632 (princ " ")
4633 (prin1 comment)))
4634 (princ ")"))))
4635 (if (bolp)
4636 (princ " "))
4637 (princ ")")
4638 (when (/= (following-char) ?\n)
4639 (princ "\n")))))
4641 ;;; The Customize Menu.
4643 ;;; Menu support
4645 (defcustom custom-menu-nesting 2
4646 "Maximum nesting in custom menus."
4647 :type 'integer
4648 :group 'custom-menu)
4650 (defun custom-face-menu-create (_widget symbol)
4651 "Ignoring WIDGET, create a menu entry for customization face SYMBOL."
4652 (vector (custom-unlispify-menu-entry symbol)
4653 `(customize-face ',symbol)
4656 (defun custom-variable-menu-create (_widget symbol)
4657 "Ignoring WIDGET, create a menu entry for customization variable SYMBOL."
4658 (let ((type (get symbol 'custom-type)))
4659 (unless (listp type)
4660 (setq type (list type)))
4661 (if (and type (widget-get type :custom-menu))
4662 (widget-apply type :custom-menu symbol)
4663 (vector (custom-unlispify-menu-entry symbol)
4664 `(customize-variable ',symbol)
4665 t))))
4667 ;; Add checkboxes to boolean variable entries.
4668 (widget-put (get 'boolean 'widget-type)
4669 :custom-menu (lambda (_widget symbol)
4670 (vector (custom-unlispify-menu-entry symbol)
4671 `(customize-variable ',symbol)
4672 ':style 'toggle
4673 ':selected symbol)))
4675 (defun custom-group-menu-create (_widget symbol)
4676 "Ignoring WIDGET, create a menu entry for customization group SYMBOL."
4677 `( ,(custom-unlispify-menu-entry symbol t)
4678 :filter (lambda (&rest junk)
4679 (let* ((menu (custom-menu-create ',symbol)))
4680 (if (consp menu) (cdr menu) menu)))))
4682 ;;;###autoload
4683 (defun custom-menu-create (symbol)
4684 "Create menu for customization group SYMBOL.
4685 The menu is in a format applicable to `easy-menu-define'."
4686 (let* ((deactivate-mark nil)
4687 (item (vector (custom-unlispify-menu-entry symbol)
4688 `(customize-group ',symbol)
4689 t)))
4690 (if (and (or (not (boundp 'custom-menu-nesting))
4691 (>= custom-menu-nesting 0))
4692 (progn
4693 (custom-load-symbol symbol)
4694 (< (length (get symbol 'custom-group)) widget-menu-max-size)))
4695 (let ((custom-prefix-list (custom-prefix-add symbol
4696 custom-prefix-list))
4697 (members (custom-sort-items (get symbol 'custom-group)
4698 custom-menu-sort-alphabetically
4699 custom-menu-order-groups)))
4700 `(,(custom-unlispify-menu-entry symbol t)
4701 ,item
4702 "--"
4703 ,@(mapcar (lambda (entry)
4704 (widget-apply (if (listp (nth 1 entry))
4705 (nth 1 entry)
4706 (list (nth 1 entry)))
4707 :custom-menu (nth 0 entry)))
4708 members)))
4709 item)))
4711 ;;;###autoload
4712 (defun customize-menu-create (symbol &optional name)
4713 "Return a customize menu for customization group SYMBOL.
4714 If optional NAME is given, use that as the name of the menu.
4715 Otherwise the menu will be named `Customize'.
4716 The format is suitable for use with `easy-menu-define'."
4717 (unless name
4718 (setq name "Customize"))
4719 `(,name
4720 :filter (lambda (&rest junk)
4721 (let ((menu (custom-menu-create ',symbol)))
4722 (if (consp menu) (cdr menu) menu)))))
4724 ;;; Toolbar and menubar support
4726 (easy-menu-define
4727 Custom-mode-menu (list custom-mode-map custom-field-keymap)
4728 "Menu used in customization buffers."
4729 (nconc (list "Custom"
4730 (customize-menu-create 'customize))
4731 (mapcar (lambda (arg)
4732 (let ((tag (nth 0 arg))
4733 (command (nth 1 arg))
4734 (active (nth 2 arg))
4735 (help (nth 3 arg)))
4736 (vector tag command :active (eval active) :help help)))
4737 custom-commands)))
4739 (defvar tool-bar-map)
4741 ;;; `custom-tool-bar-map' used to be set up here. This will fail to
4742 ;;; DTRT when `display-graphic-p' returns nil during compilation. Hence
4743 ;;; we set this up lazily in `Custom-mode'.
4744 (defvar custom-tool-bar-map nil
4745 "Keymap for toolbar in Custom mode.")
4747 ;;; The Custom Mode.
4749 (defun Custom-no-edit (_pos &optional _event)
4750 "Invoke button at POS, or refuse to allow editing of Custom buffer."
4751 (interactive "@d")
4752 (error "You can't edit this part of the Custom buffer"))
4754 (defun Custom-newline (pos &optional event)
4755 "Invoke button at POS, or refuse to allow editing of Custom buffer."
4756 (interactive "@d")
4757 (let ((button (get-char-property pos 'button)))
4758 ;; If there is no button at point, then use the one at the start
4759 ;; of the line, if it is a custom-group-link (bug#2298).
4760 (or button
4761 (if (setq button (get-char-property (line-beginning-position) 'button))
4762 (or (eq (widget-type button) 'custom-group-link)
4763 (setq button nil))))
4764 (if button
4765 (widget-apply-action button event)
4766 (error "You can't edit this part of the Custom buffer"))))
4768 (defun Custom-goto-parent ()
4769 "Go to the parent group listed at the top of this buffer.
4770 If several parents are listed, go to the first of them."
4771 (interactive)
4772 (save-excursion
4773 (goto-char (point-min))
4774 (if (search-forward "\nParent groups: " nil t)
4775 (let* ((button (get-char-property (point) 'button))
4776 (parent (downcase (widget-get button :tag))))
4777 (customize-group parent)))))
4779 (defcustom Custom-mode-hook nil
4780 "Hook called when entering Custom mode."
4781 :type 'hook
4782 :group 'custom-buffer)
4784 (defun custom-state-buffer-message (widget)
4785 (if (eq (widget-get (widget-get widget :parent) :custom-state) 'modified)
4786 (message "To install your edits, invoke [State] and choose the Set operation")))
4788 (defun custom--initialize-widget-variables ()
4789 (setq-local widget-documentation-face 'custom-documentation)
4790 (setq-local widget-button-face custom-button)
4791 (setq-local widget-button-pressed-face custom-button-pressed)
4792 (setq-local widget-mouse-face custom-button-mouse)
4793 ;; We need this because of the "More" button on docstrings.
4794 ;; Otherwise clicking on "More" can push point offscreen, which
4795 ;; causes the window to recenter on point, which pushes the
4796 ;; newly-revealed docstring offscreen; which is annoying. -- cyd.
4797 (setq-local widget-button-click-moves-point t)
4798 ;; When possible, use relief for buttons, not bracketing. This test
4799 ;; may not be optimal.
4800 (when custom-raised-buttons
4801 (setq-local widget-push-button-prefix "")
4802 (setq-local widget-push-button-suffix "")
4803 (setq-local widget-link-prefix "")
4804 (setq-local widget-link-suffix ""))
4805 (setq show-trailing-whitespace nil))
4807 (define-obsolete-variable-alias 'custom-mode-hook 'Custom-mode-hook "23.1")
4808 (define-derived-mode Custom-mode nil "Custom"
4809 "Major mode for editing customization buffers.
4811 The following commands are available:
4813 \\<widget-keymap>\
4814 Move to next button, link or editable field. \\[widget-forward]
4815 Move to previous button, link or editable field. \\[widget-backward]
4816 \\<custom-field-keymap>\
4817 Complete content of editable text field. \\[widget-complete]
4818 \\<custom-mode-map>\
4819 Invoke button under the mouse pointer. \\[widget-button-click]
4820 Invoke button under point. \\[widget-button-press]
4821 Set all options from current text. \\[Custom-set]
4822 Make values in current text permanent. \\[Custom-save]
4823 Make text match actual option values. \\[Custom-reset-current]
4824 Reset options to permanent settings. \\[Custom-reset-saved]
4825 Erase customizations; set options
4826 and buffer text to the standard values. \\[Custom-reset-standard]
4828 Entry to this mode calls the value of `Custom-mode-hook'
4829 if that value is non-nil."
4830 (use-local-map custom-mode-map)
4831 (easy-menu-add Custom-mode-menu)
4832 (setq-local tool-bar-map
4833 (or custom-tool-bar-map
4834 ;; Set up `custom-tool-bar-map'.
4835 (let ((map (make-sparse-keymap)))
4836 (mapc
4837 (lambda (arg)
4838 (tool-bar-local-item-from-menu
4839 (nth 1 arg) (nth 4 arg) map custom-mode-map
4840 :label (nth 5 arg)))
4841 custom-commands)
4842 (setq custom-tool-bar-map map))))
4843 (make-local-variable 'custom-options)
4844 (make-local-variable 'custom-local-buffer)
4845 (custom--initialize-widget-variables)
4846 (add-hook 'widget-edit-functions 'custom-state-buffer-message nil t))
4848 (put 'Custom-mode 'mode-class 'special)
4850 (define-obsolete-function-alias 'custom-mode 'Custom-mode "23.1")
4852 ;;; The End.
4854 (provide 'cus-edit)
4856 ;;; cus-edit.el ends here