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