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