Update copyright year to 2014 by running admin/update-copyright.
[emacs.git] / lisp / cus-edit.el
blob5b03d3d57c79e3cee41f525983f8425c6c163035
1 ;;; cus-edit.el --- tools for customizing Emacs and Lisp packages -*- lexical-binding:t -*-
2 ;;
3 ;; Copyright (C) 1996-1997, 1999-2014 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 [?\S-\ ] 'scroll-down-command)
447 (define-key map "\177" 'scroll-down-command)
448 (define-key map "\C-c\C-c" 'Custom-set)
449 (define-key map "\C-x\C-s" 'Custom-save)
450 (define-key map "q" 'Custom-buffer-done)
451 (define-key map "u" 'Custom-goto-parent)
452 (define-key map "n" 'widget-forward)
453 (define-key map "p" 'widget-backward)
454 map)
455 "Keymap for `Custom-mode'.")
457 (defvar custom-mode-link-map
458 (let ((map (make-keymap)))
459 (set-keymap-parent map custom-mode-map)
460 (define-key map [down-mouse-2] nil)
461 (define-key map [down-mouse-1] 'mouse-drag-region)
462 (define-key map [mouse-2] 'widget-move-and-invoke)
463 map)
464 "Local keymap for links in `Custom-mode'.")
466 (defvar custom-field-keymap
467 (let ((map (copy-keymap widget-field-keymap)))
468 (define-key map "\C-c\C-c" 'Custom-set)
469 (define-key map "\C-x\C-s" 'Custom-save)
470 map)
471 "Keymap used inside editable fields in customization buffers.")
473 (widget-put (get 'editable-field 'widget-type) :keymap custom-field-keymap)
475 ;;; Utilities.
477 (defun custom-split-regexp-maybe (regexp)
478 "If REGEXP is a string, split it to a list at `\\|'.
479 You can get the original back from the result with:
480 (mapconcat 'identity result \"\\|\")
482 IF REGEXP is not a string, return it unchanged."
483 (if (stringp regexp)
484 (split-string regexp "\\\\|")
485 regexp))
487 (defun custom-variable-prompt ()
488 "Prompt for a custom variable, defaulting to the variable at point.
489 Return a list suitable for use in `interactive'."
490 (let* ((v (variable-at-point))
491 (default (and (symbolp v) (custom-variable-p v) (symbol-name v)))
492 (enable-recursive-minibuffers t)
493 val)
494 (setq val (completing-read
495 (if default (format "Customize variable (default %s): " default)
496 "Customize variable: ")
497 obarray 'custom-variable-p t nil nil default))
498 (list (if (equal val "")
499 (if (symbolp v) v nil)
500 (intern val)))))
502 (defun custom-menu-filter (menu widget)
503 "Convert MENU to the form used by `widget-choose'.
504 MENU should be in the same format as `custom-variable-menu'.
505 WIDGET is the widget to apply the filter entries of MENU on."
506 (let ((result nil)
507 current name action filter)
508 (while menu
509 (setq current (car menu)
510 name (nth 0 current)
511 action (nth 1 current)
512 filter (nth 2 current)
513 menu (cdr menu))
514 (if (or (null filter) (funcall filter widget))
515 (push (cons name action) result)
516 (push name result)))
517 (nreverse result)))
519 ;;; Unlispify.
521 (defvar custom-prefix-list nil
522 "List of prefixes that should be ignored by `custom-unlispify'.")
524 (defcustom custom-unlispify-menu-entries t
525 "Display menu entries as words instead of symbols if non-nil."
526 :group 'custom-menu
527 :type 'boolean)
529 (defcustom custom-unlispify-remove-prefixes nil
530 "Non-nil means remove group prefixes from option names in buffer.
531 Discarding prefixes often leads to confusing names for options
532 and faces in Customize buffers, so do not set this to a non-nil
533 value unless you are sure you know what it does."
534 :group 'custom-menu
535 :group 'custom-buffer
536 :type 'boolean)
538 (defun custom-unlispify-menu-entry (symbol &optional no-suffix)
539 "Convert SYMBOL into a menu entry."
540 (cond ((not custom-unlispify-menu-entries)
541 (symbol-name symbol))
542 ((get symbol 'custom-tag)
543 (if no-suffix
544 (get symbol 'custom-tag)
545 (concat (get symbol 'custom-tag) "...")))
547 (with-current-buffer (get-buffer-create " *Custom-Work*")
548 (erase-buffer)
549 (princ symbol (current-buffer))
550 (goto-char (point-min))
551 (if custom-unlispify-remove-prefixes
552 (let ((prefixes custom-prefix-list)
553 prefix)
554 (while prefixes
555 (setq prefix (car prefixes))
556 (if (search-forward prefix (+ (point) (length prefix)) t)
557 (progn
558 (setq prefixes nil)
559 (delete-region (point-min) (point)))
560 (setq prefixes (cdr prefixes))))))
561 (subst-char-in-region (point-min) (point-max) ?- ?\s t)
562 (capitalize-region (point-min) (point-max))
563 (unless no-suffix
564 (goto-char (point-max))
565 (insert "..."))
566 (buffer-string)))))
568 (defcustom custom-unlispify-tag-names t
569 "Display tag names as words instead of symbols if non-nil."
570 :group 'custom-buffer
571 :type 'boolean)
573 (defun custom-unlispify-tag-name (symbol)
574 "Convert SYMBOL into a menu entry."
575 (let ((custom-unlispify-menu-entries custom-unlispify-tag-names))
576 (custom-unlispify-menu-entry symbol t)))
578 (defun custom-prefix-add (symbol prefixes)
579 "Add SYMBOL to list of ignored PREFIXES."
580 (cons (or (get symbol 'custom-prefix)
581 (concat (symbol-name symbol) "-"))
582 prefixes))
584 ;;; Guess.
586 (defcustom custom-guess-name-alist
587 '(("-p\\'" boolean)
588 ("-flag\\'" boolean)
589 ("-hook\\'" hook)
590 ("-face\\'" face)
591 ("-file\\'" file)
592 ("-function\\'" function)
593 ("-functions\\'" (repeat function))
594 ("-list\\'" (repeat sexp))
595 ("-alist\\'" (alist :key-type sexp :value-type sexp)))
596 "Alist of (MATCH TYPE).
598 MATCH should be a regexp matching the name of a symbol, and TYPE should
599 be a widget suitable for editing the value of that symbol. The TYPE
600 of the first entry where MATCH matches the name of the symbol will be
601 used.
603 This is used for guessing the type of variables not declared with
604 customize."
605 :type '(repeat (group (regexp :tag "Match") (sexp :tag "Type")))
606 :group 'custom-buffer)
608 (defcustom custom-guess-doc-alist
609 '(("\\`\\*?Non-nil " boolean))
610 "Alist of (MATCH TYPE).
612 MATCH should be a regexp matching a documentation string, and TYPE
613 should be a widget suitable for editing the value of a variable with
614 that documentation string. The TYPE of the first entry where MATCH
615 matches the name of the symbol will be used.
617 This is used for guessing the type of variables not declared with
618 customize."
619 :type '(repeat (group (regexp :tag "Match") (sexp :tag "Type")))
620 :group 'custom-buffer)
622 (defun custom-guess-type (symbol)
623 "Guess a widget suitable for editing the value of SYMBOL.
624 This is done by matching SYMBOL with `custom-guess-name-alist' and
625 if that fails, the doc string with `custom-guess-doc-alist'."
626 (let ((name (symbol-name symbol))
627 (names custom-guess-name-alist)
628 current found)
629 (while names
630 (setq current (car names)
631 names (cdr names))
632 (when (string-match-p (nth 0 current) name)
633 (setq found (nth 1 current)
634 names nil)))
635 (unless found
636 (let ((doc (documentation-property symbol 'variable-documentation))
637 (docs custom-guess-doc-alist))
638 (when doc
639 (while docs
640 (setq current (car docs)
641 docs (cdr docs))
642 (when (string-match-p (nth 0 current) doc)
643 (setq found (nth 1 current)
644 docs nil))))))
645 found))
647 ;;; Sorting.
649 ;;;###autoload
650 (defcustom custom-browse-sort-alphabetically nil
651 "If non-nil, sort customization group alphabetically in `custom-browse'."
652 :type 'boolean
653 :group 'custom-browse)
655 (defcustom custom-browse-order-groups nil
656 "If non-nil, order group members within each customization group.
657 If `first', order groups before non-groups.
658 If `last', order groups after non-groups."
659 :type '(choice (const first)
660 (const last)
661 (const :tag "none" nil))
662 :group 'custom-browse)
664 (defcustom custom-browse-only-groups nil
665 "If non-nil, show group members only within each customization group."
666 :type 'boolean
667 :group 'custom-browse)
669 ;;;###autoload
670 (defcustom custom-buffer-sort-alphabetically t
671 "Whether to sort customization groups alphabetically in Custom buffer."
672 :type 'boolean
673 :group 'custom-buffer
674 :version "24.1")
676 (defcustom custom-buffer-order-groups 'last
677 "If non-nil, order group members within each customization group.
678 If `first', order groups before non-groups.
679 If `last', order groups after non-groups."
680 :type '(choice (const first)
681 (const last)
682 (const :tag "none" nil))
683 :group 'custom-buffer)
685 ;;;###autoload
686 (defcustom custom-menu-sort-alphabetically nil
687 "If non-nil, sort each customization group alphabetically in menus."
688 :type 'boolean
689 :group 'custom-menu)
691 (defcustom custom-menu-order-groups 'first
692 "If non-nil, order group members within each customization group.
693 If `first', order groups before non-groups.
694 If `last', order groups after non-groups."
695 :type '(choice (const first)
696 (const last)
697 (const :tag "none" nil))
698 :group 'custom-menu)
700 (defun custom-sort-items (items sort-alphabetically order-groups)
701 "Return a sorted copy of ITEMS.
702 ITEMS should be a `custom-group' property.
703 If SORT-ALPHABETICALLY non-nil, sort alphabetically.
704 If ORDER-GROUPS is `first' order groups before non-groups, if `last' order
705 groups after non-groups, if nil do not order groups at all."
706 (sort (copy-sequence items)
707 (lambda (a b)
708 (let ((typea (nth 1 a)) (typeb (nth 1 b))
709 (namea (nth 0 a)) (nameb (nth 0 b)))
710 (cond ((not order-groups)
711 ;; Since we don't care about A and B order, maybe sort.
712 (when sort-alphabetically
713 (string-lessp namea nameb)))
714 ((eq typea 'custom-group)
715 ;; If B is also a group, maybe sort. Otherwise, order A and B.
716 (if (eq typeb 'custom-group)
717 (when sort-alphabetically
718 (string-lessp namea nameb))
719 (eq order-groups 'first)))
720 ((eq typeb 'custom-group)
721 ;; Since A cannot be a group, order A and B.
722 (eq order-groups 'last))
723 (sort-alphabetically
724 ;; Since A and B cannot be groups, sort.
725 (string-lessp namea nameb)))))))
727 ;;; Custom Mode Commands.
729 ;; This variable is used by `custom-tool-bar-map', or directly by
730 ;; `custom-buffer-create-internal' if `custom-buffer-verbose-help' is non-nil.
732 (defvar custom-commands
733 '((" Apply " Custom-set t
734 "Apply settings (for the current session only)."
735 "index"
736 "Apply")
737 (" Apply and Save " Custom-save
738 (or custom-file user-init-file)
739 "Apply settings and save for future sessions."
740 "save"
741 "Save")
742 (" Undo Edits " Custom-reset-current t
743 "Restore customization buffer to reflect existing settings."
744 "refresh"
745 "Undo")
746 (" Reset Customizations " Custom-reset-saved t
747 "Undo any settings applied only for the current session."
748 "undo"
749 "Reset")
750 (" Erase Customizations " Custom-reset-standard
751 (or custom-file user-init-file)
752 "Un-customize settings in this and future sessions."
753 "delete"
754 "Uncustomize")
755 (" Help for Customize " Custom-help t
756 "Get help for using Customize."
757 "help"
758 "Help")
759 (" Exit " Custom-buffer-done t "Exit Customize." "exit" "Exit")))
761 (defun Custom-help ()
762 "Read the node on Easy Customization in the Emacs manual."
763 (interactive)
764 (info "(emacs)Easy Customization"))
766 (defvar custom-reset-menu
767 '(("Undo Edits in Customization Buffer" . Custom-reset-current)
768 ("Revert This Session's Customizations" . Custom-reset-saved)
769 ("Erase Customizations" . Custom-reset-standard))
770 "Alist of actions for the `Reset' button.
771 The key is a string containing the name of the action, the value is a
772 Lisp function taking the widget as an element which will be called
773 when the action is chosen.")
775 (defvar custom-options nil
776 "Customization widgets in the current buffer.")
778 (defun custom-command-apply (fun query &optional strong-query)
779 "Call function FUN on all widgets in `custom-options'.
780 If there is more than one widget, ask user for confirmation using
781 the query string QUERY, using `y-or-n-p' if STRONG-QUERY is nil,
782 and `yes-or-no-p' otherwise."
783 (if (or (and (= 1 (length custom-options))
784 (memq (widget-type (car custom-options))
785 '(custom-variable custom-face)))
786 (funcall (if strong-query 'yes-or-no-p 'y-or-n-p) query))
787 (progn (mapc fun custom-options) t)
788 (message "Aborted")
789 nil))
791 (defun Custom-set (&rest _ignore)
792 "Set the current value of all edited settings in the buffer."
793 (interactive)
794 (custom-command-apply
795 (lambda (child)
796 (when (eq (widget-get child :custom-state) 'modified)
797 (widget-apply child :custom-set)))
798 "Set all values according to this buffer? "))
800 (defun Custom-save (&rest _ignore)
801 "Set all edited settings, then save all settings that have been set.
802 If a setting was edited and set before, this saves it. If a
803 setting was merely edited before, this sets it then saves it."
804 (interactive)
805 (when (custom-command-apply
806 (lambda (child)
807 (when (memq (widget-get child :custom-state)
808 '(modified set changed rogue))
809 (widget-apply child :custom-mark-to-save)))
810 "Save all settings in this buffer? " t)
811 ;; Save changes to buffer and redraw.
812 (custom-save-all)
813 (dolist (child custom-options)
814 (widget-apply child :custom-state-set-and-redraw))))
816 (defun custom-reset (_widget &optional event)
817 "Select item from reset menu."
818 (let* ((completion-ignore-case t)
819 (answer (widget-choose "Reset settings"
820 custom-reset-menu
821 event)))
822 (if answer
823 (funcall answer))))
825 (defun Custom-reset-current (&rest _ignore)
826 "Reset all edited settings in the buffer to show their current values."
827 (interactive)
828 (custom-command-apply
829 (lambda (widget)
830 (if (memq (widget-get widget :custom-state) '(modified changed))
831 (widget-apply widget :custom-reset-current)))
832 "Reset all settings' buffer text to show current values? "))
834 (defun Custom-reset-saved (&rest _ignore)
835 "Reset all edited or set settings in the buffer to their saved value.
836 This also shows the saved values in the buffer."
837 (interactive)
838 (custom-command-apply
839 (lambda (widget)
840 (if (memq (widget-get widget :custom-state) '(modified set changed rogue))
841 (widget-apply widget :custom-reset-saved)))
842 "Reset all settings (current values and buffer text) to saved values? "))
844 ;; The next two variables are bound to '(t) by `Custom-reset-standard'
845 ;; and `custom-group-reset-standard'. If these variables are nil, both
846 ;; `custom-variable-reset-standard' and `custom-face-reset-standard'
847 ;; save, reset and redraw the handled widget immediately. Otherwise,
848 ;; they add the widget to the corresponding list and leave it to
849 ;; `custom-reset-standard-save-and-update' to save, reset and redraw it.
850 (defvar custom-reset-standard-variables-list nil)
851 (defvar custom-reset-standard-faces-list nil)
853 ;; The next function was excerpted from `custom-variable-reset-standard'
854 ;; and `custom-face-reset-standard' and is used to avoid calling
855 ;; `custom-save-all' repeatedly (and thus saving settings to file one by
856 ;; one) when erasing all customizations.
857 (defun custom-reset-standard-save-and-update ()
858 "Save settings and redraw after erasing customizations."
859 (when (or (and custom-reset-standard-variables-list
860 (not (eq custom-reset-standard-variables-list '(t))))
861 (and custom-reset-standard-faces-list
862 (not (eq custom-reset-standard-faces-list '(t)))))
863 ;; Save settings to file.
864 (custom-save-all)
865 ;; Set state of and redraw variables.
866 (dolist (widget custom-reset-standard-variables-list)
867 (unless (eq widget t)
868 (widget-put widget :custom-state 'unknown)
869 (custom-redraw widget)))
870 ;; Set state of and redraw faces.
871 (dolist (widget custom-reset-standard-faces-list)
872 (unless (eq widget t)
873 (let* ((symbol (widget-value widget))
874 (child (car (widget-get widget :children)))
875 (comment-widget (widget-get widget :comment-widget)))
876 (put symbol 'face-comment nil)
877 (widget-value-set child
878 (custom-pre-filter-face-spec
879 (list (list t (custom-face-attributes-get
880 symbol nil)))))
881 ;; This call manages the comment visibility
882 (widget-value-set comment-widget "")
883 (custom-face-state-set widget)
884 (custom-redraw-magic widget))))))
886 (defun Custom-reset-standard (&rest _ignore)
887 "Erase all customizations (either current or saved) in current buffer.
888 The immediate result is to restore them to their standard values.
889 This operation eliminates any saved values for the group members,
890 making them as if they had never been customized at all."
891 (interactive)
892 ;; Bind these temporarily.
893 (let ((custom-reset-standard-variables-list '(t))
894 (custom-reset-standard-faces-list '(t)))
895 (custom-command-apply
896 (lambda (widget)
897 (and (or (null (widget-get widget :custom-standard-value))
898 (widget-apply widget :custom-standard-value))
899 (memq (widget-get widget :custom-state)
900 '(modified set changed saved rogue))
901 (widget-apply widget :custom-mark-to-reset-standard)))
902 "The settings will revert to their default values, in this
903 and future sessions. Really erase customizations? " t)
904 (custom-reset-standard-save-and-update)))
906 ;;; The Customize Commands
908 (defun custom-prompt-variable (prompt-var prompt-val &optional comment)
909 "Prompt for a variable and a value and return them as a list.
910 PROMPT-VAR is the prompt for the variable, and PROMPT-VAL is the
911 prompt for the value. The %s escape in PROMPT-VAL is replaced with
912 the name of the variable.
914 If the variable has a `variable-interactive' property, that is used as if
915 it were the arg to `interactive' (which see) to interactively read the value.
917 If the variable has a `custom-type' property, it must be a widget and the
918 `:prompt-value' property of that widget will be used for reading the value.
919 If the variable also has a `custom-get' property, that is used for finding
920 the current value of the variable, otherwise `symbol-value' is used.
922 If optional COMMENT argument is non-nil, also prompt for a comment and return
923 it as the third element in the list."
924 (let* ((var (read-variable prompt-var))
925 (minibuffer-help-form '(describe-variable var))
926 (val
927 (let ((prop (get var 'variable-interactive))
928 (type (get var 'custom-type))
929 (prompt (format prompt-val var)))
930 (unless (listp type)
931 (setq type (list type)))
932 (cond (prop
933 ;; Use VAR's `variable-interactive' property
934 ;; as an interactive spec for prompting.
935 (call-interactively `(lambda (arg)
936 (interactive ,prop)
937 arg)))
938 (type
939 (widget-prompt-value type
940 prompt
941 (if (boundp var)
942 (funcall
943 (or (get var 'custom-get) 'symbol-value)
944 var))
945 (not (boundp var))))
947 (eval-minibuffer prompt))))))
948 (if comment
949 (list var val
950 (read-string "Comment: " (get var 'variable-comment)))
951 (list var val))))
953 ;;;###autoload
954 (defun customize-set-value (variable value &optional comment)
955 "Set VARIABLE to VALUE, and return VALUE. VALUE is a Lisp object.
957 If VARIABLE has a `variable-interactive' property, that is used as if
958 it were the arg to `interactive' (which see) to interactively read the value.
960 If VARIABLE has a `custom-type' property, it must be a widget and the
961 `:prompt-value' property of that widget will be used for reading the value.
963 If given a prefix (or a COMMENT argument), also prompt for a comment."
964 (interactive (custom-prompt-variable "Set variable: "
965 "Set %s to value: "
966 current-prefix-arg))
968 (cond ((string= comment "")
969 (put variable 'variable-comment nil))
970 (comment
971 (put variable 'variable-comment comment)))
972 (set variable value))
974 ;;;###autoload
975 (defun customize-set-variable (variable value &optional comment)
976 "Set the default for VARIABLE to VALUE, and return VALUE.
977 VALUE is a Lisp object.
979 If VARIABLE has a `custom-set' property, that is used for setting
980 VARIABLE, otherwise `set-default' is used.
982 If VARIABLE has a `variable-interactive' property, that is used as if
983 it were the arg to `interactive' (which see) to interactively read the value.
985 If VARIABLE has a `custom-type' property, it must be a widget and the
986 `:prompt-value' property of that widget will be used for reading the value.
988 If given a prefix (or a COMMENT argument), also prompt for a comment."
989 (interactive (custom-prompt-variable "Set variable: "
990 "Set customized value for %s to: "
991 current-prefix-arg))
992 (custom-load-symbol variable)
993 (custom-push-theme 'theme-value variable 'user 'set (custom-quote value))
994 (funcall (or (get variable 'custom-set) 'set-default) variable value)
995 (put variable 'customized-value (list (custom-quote value)))
996 (cond ((string= comment "")
997 (put variable 'variable-comment nil)
998 (put variable 'customized-variable-comment nil))
999 (comment
1000 (put variable 'variable-comment comment)
1001 (put variable 'customized-variable-comment comment)))
1002 value)
1004 ;;;###autoload
1005 (defun customize-save-variable (variable value &optional comment)
1006 "Set the default for VARIABLE to VALUE, and save it for future sessions.
1007 Return VALUE.
1009 If VARIABLE has a `custom-set' property, that is used for setting
1010 VARIABLE, otherwise `set-default' is used.
1012 If VARIABLE has a `variable-interactive' property, that is used as if
1013 it were the arg to `interactive' (which see) to interactively read the value.
1015 If VARIABLE has a `custom-type' property, it must be a widget and the
1016 `:prompt-value' property of that widget will be used for reading the value.
1018 If given a prefix (or a COMMENT argument), also prompt for a comment."
1019 (interactive (custom-prompt-variable "Set and save variable: "
1020 "Set and save value for %s as: "
1021 current-prefix-arg))
1022 (funcall (or (get variable 'custom-set) 'set-default) variable value)
1023 (put variable 'saved-value (list (custom-quote value)))
1024 (custom-push-theme 'theme-value variable 'user 'set (custom-quote value))
1025 (cond ((string= comment "")
1026 (put variable 'variable-comment nil)
1027 (put variable 'saved-variable-comment nil))
1028 (comment
1029 (put variable 'variable-comment comment)
1030 (put variable 'saved-variable-comment comment)))
1031 (put variable 'customized-value nil)
1032 (put variable 'customized-variable-comment nil)
1033 (if (custom-file t)
1034 (custom-save-all)
1035 (message "Setting `%s' temporarily since \"emacs -q\" would overwrite customizations"
1036 variable)
1037 (set variable value))
1038 value)
1040 ;; Some parts of Emacs might prompt the user to save customizations,
1041 ;; during startup before customizations are loaded. This function
1042 ;; handles this corner case by avoiding calling `custom-save-variable'
1043 ;; too early, which could wipe out existing customizations.
1045 ;;;###autoload
1046 (defun customize-push-and-save (list-var elts)
1047 "Add ELTS to LIST-VAR and save for future sessions, safely.
1048 ELTS should be a list. This function adds each entry to the
1049 value of LIST-VAR using `add-to-list'.
1051 If Emacs is initialized, call `customize-save-variable' to save
1052 the resulting list value now. Otherwise, add an entry to
1053 `after-init-hook' to save it after initialization."
1054 (dolist (entry elts)
1055 (add-to-list list-var entry))
1056 (if after-init-time
1057 (let ((coding-system-for-read nil))
1058 (customize-save-variable list-var (eval list-var)))
1059 (add-hook 'after-init-hook
1060 (lambda ()
1061 (customize-push-and-save list-var elts)))))
1063 ;;;###autoload
1064 (defun customize ()
1065 "Select a customization buffer which you can use to set user options.
1066 User options are structured into \"groups\".
1067 Initially the top-level group `Emacs' and its immediate subgroups
1068 are shown; the contents of those subgroups are initially hidden."
1069 (interactive)
1070 (customize-group 'emacs))
1072 ;;;###autoload
1073 (defun customize-mode (mode)
1074 "Customize options related to the current major mode.
1075 If a prefix \\[universal-argument] was given (or if the current major mode has no known group),
1076 then prompt for the MODE to customize."
1077 (interactive
1078 (list
1079 (let ((completion-regexp-list '("-mode\\'"))
1080 (group (custom-group-of-mode major-mode)))
1081 (if (and group (not current-prefix-arg))
1082 major-mode
1083 (intern
1084 (completing-read (if group
1085 (format "Major mode (default %s): " major-mode)
1086 "Major mode: ")
1087 obarray
1088 'custom-group-of-mode
1089 t nil nil (if group (symbol-name major-mode))))))))
1090 (customize-group (custom-group-of-mode mode)))
1092 (defun customize-read-group ()
1093 (let ((completion-ignore-case t))
1094 (completing-read "Customize group (default emacs): "
1095 obarray
1096 (lambda (symbol)
1097 (or (and (get symbol 'custom-loads)
1098 (not (get symbol 'custom-autoload)))
1099 (get symbol 'custom-group)))
1100 t)))
1102 ;;;###autoload
1103 (defun customize-group (&optional group other-window)
1104 "Customize GROUP, which must be a customization group.
1105 If OTHER-WINDOW is non-nil, display in another window."
1106 (interactive (list (customize-read-group)))
1107 (when (stringp group)
1108 (if (string-equal "" group)
1109 (setq group 'emacs)
1110 (setq group (intern group))))
1111 (let ((name (format "*Customize Group: %s*"
1112 (custom-unlispify-tag-name group))))
1113 (cond
1114 ((null (get-buffer name))
1115 (funcall (if other-window
1116 'custom-buffer-create-other-window
1117 'custom-buffer-create)
1118 (list (list group 'custom-group))
1119 name
1120 (concat " for group "
1121 (custom-unlispify-tag-name group))))
1122 (other-window
1123 (switch-to-buffer-other-window name))
1125 (pop-to-buffer-same-window name)))))
1127 ;;;###autoload
1128 (defun customize-group-other-window (&optional group)
1129 "Customize GROUP, which must be a customization group, in another window."
1130 (interactive (list (customize-read-group)))
1131 (customize-group group t))
1133 ;;;###autoload
1134 (defalias 'customize-variable 'customize-option)
1136 ;;;###autoload
1137 (defun customize-option (symbol)
1138 "Customize SYMBOL, which must be a user option."
1139 (interactive (custom-variable-prompt))
1140 (unless symbol
1141 (error "No variable specified"))
1142 (let ((basevar (indirect-variable symbol)))
1143 (custom-buffer-create (list (list basevar 'custom-variable))
1144 (format "*Customize Option: %s*"
1145 (custom-unlispify-tag-name basevar)))
1146 (unless (eq symbol basevar)
1147 (message "`%s' is an alias for `%s'" symbol basevar))))
1149 ;;;###autoload
1150 (defalias 'customize-variable-other-window 'customize-option-other-window)
1152 ;;;###autoload
1153 (defun customize-option-other-window (symbol)
1154 "Customize SYMBOL, which must be a user option.
1155 Show the buffer in another window, but don't select it."
1156 (interactive (custom-variable-prompt))
1157 (unless symbol
1158 (error "No variable specified"))
1159 (let ((basevar (indirect-variable symbol)))
1160 (custom-buffer-create-other-window
1161 (list (list basevar 'custom-variable))
1162 (format "*Customize Option: %s*" (custom-unlispify-tag-name basevar)))
1163 (unless (eq symbol basevar)
1164 (message "`%s' is an alias for `%s'" symbol basevar))))
1166 (defvar customize-changed-options-previous-release "24.1"
1167 "Version for `customize-changed-options' to refer back to by default.")
1169 ;; Packages will update this variable, so make it available.
1170 ;;;###autoload
1171 (defvar customize-package-emacs-version-alist nil
1172 "Alist mapping versions of a package to Emacs versions.
1173 We use this for packages that have their own names, but are released
1174 as part of Emacs itself.
1176 Each elements looks like this:
1178 (PACKAGE (PVERSION . EVERSION)...)
1180 Here PACKAGE is the name of a package, as a symbol. After
1181 PACKAGE come one or more elements, each associating a
1182 package version PVERSION with the first Emacs version
1183 EVERSION in which it (or a subsequent version of PACKAGE)
1184 was first released. Both PVERSION and EVERSION are strings.
1185 PVERSION should be a string that this package used in
1186 the :package-version keyword for `defcustom', `defgroup',
1187 and `defface'.
1189 For example, the MH-E package updates this alist as follows:
1191 (add-to-list 'customize-package-emacs-version-alist
1192 '(MH-E (\"6.0\" . \"22.1\") (\"6.1\" . \"22.1\")
1193 (\"7.0\" . \"22.1\") (\"7.1\" . \"22.1\")
1194 (\"7.2\" . \"22.1\") (\"7.3\" . \"22.1\")
1195 (\"7.4\" . \"22.1\") (\"8.0\" . \"22.1\")))
1197 The value of PACKAGE needs to be unique and it needs to match the
1198 PACKAGE value appearing in the :package-version keyword. Since
1199 the user might see the value in a error message, a good choice is
1200 the official name of the package, such as MH-E or Gnus.")
1202 ;;;###autoload
1203 (defalias 'customize-changed 'customize-changed-options)
1205 ;;;###autoload
1206 (defun customize-changed-options (&optional since-version)
1207 "Customize all settings whose meanings have changed in Emacs itself.
1208 This includes new user options and faces, and new customization
1209 groups, as well as older options and faces whose meanings or
1210 default values have changed since the previous major Emacs
1211 release.
1213 With argument SINCE-VERSION (a string), customize all settings
1214 that were added or redefined since that version."
1216 (interactive
1217 (list
1218 (read-from-minibuffer
1219 (format "Customize options changed, since version (default %s): "
1220 customize-changed-options-previous-release))))
1221 (if (equal since-version "")
1222 (setq since-version nil)
1223 (unless (condition-case nil
1224 (numberp (read since-version))
1225 (error nil))
1226 (signal 'wrong-type-argument (list 'numberp since-version))))
1227 (unless since-version
1228 (setq since-version customize-changed-options-previous-release))
1230 ;; Load the information for versions since since-version. We use
1231 ;; custom-load-symbol for this.
1232 (put 'custom-versions-load-alist 'custom-loads nil)
1233 (dolist (elt custom-versions-load-alist)
1234 (if (customize-version-lessp since-version (car elt))
1235 (dolist (load (cdr elt))
1236 (custom-add-load 'custom-versions-load-alist load))))
1237 (custom-load-symbol 'custom-versions-load-alist)
1238 (put 'custom-versions-load-alist 'custom-loads nil)
1240 (let (found)
1241 (mapatoms
1242 (lambda (symbol)
1243 (let* ((package-version (get symbol 'custom-package-version))
1244 (version
1245 (or (and package-version
1246 (customize-package-emacs-version symbol
1247 package-version))
1248 (get symbol 'custom-version))))
1249 (if version
1250 (when (customize-version-lessp since-version version)
1251 (if (or (get symbol 'custom-group)
1252 (get symbol 'group-documentation))
1253 (push (list symbol 'custom-group) found))
1254 (if (custom-variable-p symbol)
1255 (push (list symbol 'custom-variable) found))
1256 (if (custom-facep symbol)
1257 (push (list symbol 'custom-face) found)))))))
1258 (if found
1259 (custom-buffer-create (custom-sort-items found t 'first)
1260 "*Customize Changed Options*")
1261 (user-error "No user option defaults have been changed since Emacs %s"
1262 since-version))))
1264 (defun customize-package-emacs-version (symbol package-version)
1265 "Return the Emacs version in which SYMBOL's meaning last changed.
1266 PACKAGE-VERSION has the form (PACKAGE . VERSION). We use
1267 `customize-package-emacs-version-alist' to find the version of
1268 Emacs that is associated with version VERSION of PACKAGE."
1269 (let (package-versions emacs-version)
1270 ;; Use message instead of error since we want user to be able to
1271 ;; see the rest of the symbols even if a package author has
1272 ;; botched things up.
1273 (cond ((not (listp package-version))
1274 (message "Invalid package-version value for %s" symbol))
1275 ((setq package-versions (assq (car package-version)
1276 customize-package-emacs-version-alist))
1277 (setq emacs-version
1278 (cdr (assoc (cdr package-version) package-versions)))
1279 (unless emacs-version
1280 (message "%s version %s not found in %s" symbol
1281 (cdr package-version)
1282 "customize-package-emacs-version-alist")))
1284 (message "Package %s version %s lists no corresponding Emacs version"
1285 (car package-version)
1286 (cdr package-version))))
1287 emacs-version))
1289 (defun customize-version-lessp (version1 version2)
1290 ;; Why are the versions strings, and given that they are, why aren't
1291 ;; they converted to numbers and compared as such here? -- fx
1293 ;; In case someone made a mistake and left out the quotes
1294 ;; in the :version value.
1295 (if (numberp version2)
1296 (setq version2 (prin1-to-string version2)))
1297 (let (major1 major2 minor1 minor2)
1298 (string-match "\\([0-9]+\\)\\(\\.\\([0-9]+\\)\\)?" version1)
1299 (setq major1 (read (or (match-string 1 version1)
1300 "0")))
1301 (setq minor1 (read (or (match-string 3 version1)
1302 "0")))
1303 (string-match "\\([0-9]+\\)\\(\\.\\([0-9]+\\)\\)?" version2)
1304 (setq major2 (read (or (match-string 1 version2)
1305 "0")))
1306 (setq minor2 (read (or (match-string 3 version2)
1307 "0")))
1308 (or (< major1 major2)
1309 (and (= major1 major2)
1310 (< minor1 minor2)))))
1312 ;;;###autoload
1313 (defun customize-face (&optional face other-window)
1314 "Customize FACE, which should be a face name or nil.
1315 If FACE is nil, customize all faces. If FACE is actually a
1316 face-alias, customize the face it is aliased to.
1318 If OTHER-WINDOW is non-nil, display in another window.
1320 Interactively, when point is on text which has a face specified,
1321 suggest to customize that face, if it's customizable."
1322 (interactive (list (read-face-name "Customize face"
1323 (or (face-at-point t t) "all faces") t)))
1324 (if (member face '(nil ""))
1325 (setq face (face-list)))
1326 (if (and (listp face) (null (cdr face)))
1327 (setq face (car face)))
1328 (let ((display-fun (if other-window
1329 'custom-buffer-create-other-window
1330 'custom-buffer-create)))
1331 (if (listp face)
1332 (funcall display-fun
1333 (custom-sort-items
1334 (mapcar (lambda (s) (list s 'custom-face)) face)
1335 t nil)
1336 "*Customize Faces*")
1337 ;; If FACE is actually an alias, customize the face it is aliased to.
1338 (if (get face 'face-alias)
1339 (setq face (get face 'face-alias)))
1340 (unless (facep face)
1341 (error "Invalid face %S" face))
1342 (funcall display-fun
1343 (list (list face 'custom-face))
1344 (format "*Customize Face: %s*"
1345 (custom-unlispify-tag-name face))))))
1347 ;;;###autoload
1348 (defun customize-face-other-window (&optional face)
1349 "Show customization buffer for face FACE in other window.
1350 If FACE is actually a face-alias, customize the face it is aliased to.
1352 Interactively, when point is on text which has a face specified,
1353 suggest to customize that face, if it's customizable."
1354 (interactive (list (read-face-name "Customize face"
1355 (or (face-at-point t t) "all faces") t)))
1356 (customize-face face t))
1358 (defalias 'customize-customized 'customize-unsaved)
1360 ;;;###autoload
1361 (defun customize-unsaved ()
1362 "Customize all options and faces set in this session but not saved."
1363 (interactive)
1364 (let ((found nil))
1365 (mapatoms (lambda (symbol)
1366 (and (or (get symbol 'customized-face)
1367 (get symbol 'customized-face-comment))
1368 (custom-facep symbol)
1369 (push (list symbol 'custom-face) found))
1370 (and (or (get symbol 'customized-value)
1371 (get symbol 'customized-variable-comment))
1372 (boundp symbol)
1373 (push (list symbol 'custom-variable) found))))
1374 (if (not found)
1375 (error "No user options are set but unsaved")
1376 (custom-buffer-create (custom-sort-items found t nil)
1377 "*Customize Unsaved*"))))
1379 ;;;###autoload
1380 (defun customize-rogue ()
1381 "Customize all user variables modified outside customize."
1382 (interactive)
1383 (let ((found nil))
1384 (mapatoms (lambda (symbol)
1385 (let ((cval (or (get symbol 'customized-value)
1386 (get symbol 'saved-value)
1387 (get symbol 'standard-value))))
1388 (when (and cval ;Declared with defcustom.
1389 (default-boundp symbol) ;Has a value.
1390 (not (equal (eval (car cval))
1391 ;; Which does not match customize.
1392 (default-value symbol))))
1393 (push (list symbol 'custom-variable) found)))))
1394 (if (not found)
1395 (user-error "No rogue user options")
1396 (custom-buffer-create (custom-sort-items found t nil)
1397 "*Customize Rogue*"))))
1398 ;;;###autoload
1399 (defun customize-saved ()
1400 "Customize all saved options and faces."
1401 (interactive)
1402 (let ((found nil))
1403 (mapatoms (lambda (symbol)
1404 (and (or (get symbol 'saved-face)
1405 (get symbol 'saved-face-comment))
1406 (custom-facep symbol)
1407 (push (list symbol 'custom-face) found))
1408 (and (or (get symbol 'saved-value)
1409 (get symbol 'saved-variable-comment))
1410 (boundp symbol)
1411 (push (list symbol 'custom-variable) found))))
1412 (if (not found)
1413 (user-error "No saved user options")
1414 (custom-buffer-create (custom-sort-items found t nil)
1415 "*Customize Saved*"))))
1417 (declare-function apropos-parse-pattern "apropos" (pattern))
1418 (defvar apropos-regexp)
1420 ;;;###autoload
1421 (defun customize-apropos (pattern &optional type)
1422 "Customize loaded options, faces and groups matching PATTERN.
1423 PATTERN can be a word, a list of words (separated by spaces),
1424 or a regexp (using some regexp special characters). If it is a word,
1425 search for matches for that word as a substring. If it is a list of
1426 words, search for matches for any two (or more) of those words.
1428 If TYPE is `options', include only options.
1429 If TYPE is `faces', include only faces.
1430 If TYPE is `groups', include only groups."
1431 (interactive (list (apropos-read-pattern "symbol") nil))
1432 (require 'apropos)
1433 (unless (memq type '(nil options faces groups))
1434 (error "Invalid setting type %s" (symbol-name type)))
1435 (apropos-parse-pattern pattern) ;Sets apropos-regexp by side-effect: Yuck!
1436 (let (found)
1437 (mapatoms
1438 (lambda (symbol)
1439 (when (string-match-p apropos-regexp (symbol-name symbol))
1440 (if (memq type '(nil groups))
1441 (if (get symbol 'custom-group)
1442 (push (list symbol 'custom-group) found)))
1443 (if (memq type '(nil faces))
1444 (if (custom-facep symbol)
1445 (push (list symbol 'custom-face) found)))
1446 (if (memq type '(nil options))
1447 (if (and (boundp symbol)
1448 (eq (indirect-variable symbol) symbol)
1449 (or (get symbol 'saved-value)
1450 (custom-variable-p symbol)))
1451 (push (list symbol 'custom-variable) found))))))
1452 (unless found
1453 (error "No customizable %s matching %s" (symbol-name type) pattern))
1454 (custom-buffer-create
1455 (custom-sort-items found t custom-buffer-order-groups)
1456 "*Customize Apropos*")))
1458 ;;;###autoload
1459 (defun customize-apropos-options (regexp &optional ignored)
1460 "Customize all loaded customizable options matching REGEXP."
1461 (interactive (list (apropos-read-pattern "options")))
1462 (customize-apropos regexp 'options))
1464 ;;;###autoload
1465 (defun customize-apropos-faces (regexp)
1466 "Customize all loaded faces matching REGEXP."
1467 (interactive (list (apropos-read-pattern "faces")))
1468 (customize-apropos regexp 'faces))
1470 ;;;###autoload
1471 (defun customize-apropos-groups (regexp)
1472 "Customize all loaded groups matching REGEXP."
1473 (interactive (list (apropos-read-pattern "groups")))
1474 (customize-apropos regexp 'groups))
1476 ;;; Buffer.
1478 (defcustom custom-buffer-style 'links
1479 "Control the presentation style for customization buffers.
1480 The value should be a symbol, one of:
1482 brackets: groups nest within each other with big horizontal brackets.
1483 links: groups have links to subgroups."
1484 :type '(radio (const brackets)
1485 (const links))
1486 :group 'custom-buffer)
1488 (defcustom custom-buffer-done-kill nil
1489 "Non-nil means exiting a Custom buffer should kill it."
1490 :type 'boolean
1491 :version "22.1"
1492 :group 'custom-buffer)
1494 (defcustom custom-buffer-indent 3
1495 "Number of spaces to indent nested groups."
1496 :type 'integer
1497 :group 'custom-buffer)
1499 (defun custom-get-fresh-buffer (name)
1500 "Get a fresh new buffer with name NAME.
1501 If the buffer already exist, clean it up to be like new.
1502 Beware: it's not quite like new. Good enough for custom, but maybe
1503 not for everybody."
1504 ;; To be more complete, we should also kill all permanent-local variables,
1505 ;; but it's not needed for custom.
1506 (let ((buf (get-buffer name)))
1507 (when (and buf (buffer-local-value 'buffer-file-name buf))
1508 ;; This will check if the file is not saved.
1509 (kill-buffer buf)
1510 (setq buf nil))
1511 (if (null buf)
1512 (get-buffer-create name)
1513 (with-current-buffer buf
1514 (kill-all-local-variables)
1515 (run-hooks 'kill-buffer-hook)
1516 ;; Delete overlays before erasing the buffer so the overlay hooks
1517 ;; don't get run spuriously when we erase the buffer.
1518 (let ((ols (overlay-lists)))
1519 (dolist (ol (nconc (car ols) (cdr ols)))
1520 (delete-overlay ol)))
1521 (erase-buffer)
1522 buf))))
1524 ;;;###autoload
1525 (defun custom-buffer-create (options &optional name description)
1526 "Create a buffer containing OPTIONS.
1527 Optional NAME is the name of the buffer.
1528 OPTIONS should be an alist of the form ((SYMBOL WIDGET)...), where
1529 SYMBOL is a customization option, and WIDGET is a widget for editing
1530 that option."
1531 (pop-to-buffer-same-window (custom-get-fresh-buffer (or name "*Customization*")))
1532 (custom-buffer-create-internal options description))
1534 ;;;###autoload
1535 (defun custom-buffer-create-other-window (options &optional name description)
1536 "Create a buffer containing OPTIONS, and display it in another window.
1537 The result includes selecting that window.
1538 Optional NAME is the name of the buffer.
1539 OPTIONS should be an alist of the form ((SYMBOL WIDGET)...), where
1540 SYMBOL is a customization option, and WIDGET is a widget for editing
1541 that option."
1542 (unless name (setq name "*Customization*"))
1543 (switch-to-buffer-other-window (custom-get-fresh-buffer name))
1544 (custom-buffer-create-internal options description))
1546 (defcustom custom-reset-button-menu t
1547 "If non-nil, only show a single reset button in customize buffers.
1548 This button will have a menu with all three reset operations."
1549 :type 'boolean
1550 :group 'custom-buffer
1551 :version "24.3")
1553 (defcustom custom-buffer-verbose-help t
1554 "If non-nil, include explanatory text in the customization buffer."
1555 :type 'boolean
1556 :group 'custom-buffer)
1558 (defun Custom-buffer-done (&rest _ignore)
1559 "Exit current Custom buffer according to `custom-buffer-done-kill'."
1560 (interactive)
1561 (quit-window custom-buffer-done-kill))
1563 (defvar custom-button nil
1564 "Face used for buttons in customization buffers.")
1566 (defvar custom-button-mouse nil
1567 "Mouse face used for buttons in customization buffers.")
1569 (defvar custom-button-pressed nil
1570 "Face used for pressed buttons in customization buffers.")
1572 (defcustom custom-search-field t
1573 "If non-nil, show a search field in Custom buffers."
1574 :type 'boolean
1575 :version "24.1"
1576 :group 'custom-buffer)
1578 (defcustom custom-raised-buttons (not (equal (face-valid-attribute-values :box)
1579 '(("unspecified" . unspecified))))
1580 "If non-nil, indicate active buttons in a `raised-button' style.
1581 Otherwise use brackets."
1582 :type 'boolean
1583 :version "21.1"
1584 :group 'custom-buffer
1585 :set (lambda (variable value)
1586 (custom-set-default variable value)
1587 (setq custom-button
1588 (if value 'custom-button 'custom-button-unraised))
1589 (setq custom-button-mouse
1590 (if value 'custom-button-mouse 'highlight))
1591 (setq custom-button-pressed
1592 (if value
1593 'custom-button-pressed
1594 'custom-button-pressed-unraised))))
1596 (defun custom-buffer-create-internal (options &optional _description)
1597 (Custom-mode)
1598 (let ((init-file (or custom-file user-init-file)))
1599 ;; Insert verbose help at the top of the custom buffer.
1600 (when custom-buffer-verbose-help
1601 (unless init-file
1602 (widget-insert "Custom settings cannot be saved; maybe you started Emacs with `-q'.\n"))
1603 (widget-insert "For help using this buffer, see ")
1604 (widget-create 'custom-manual
1605 :tag "Easy Customization"
1606 "(emacs)Easy Customization")
1607 (widget-insert " in the ")
1608 (widget-create 'custom-manual
1609 :tag "Emacs manual"
1610 :help-echo "Read the Emacs manual."
1611 "(emacs)Top")
1612 (widget-insert "."))
1613 (widget-insert "\n")
1615 ;; Insert the search field.
1616 (when custom-search-field
1617 (widget-insert "\n")
1618 (let* ((echo "Search for custom items.
1619 You can enter one or more words separated by spaces,
1620 or a regular expression.")
1621 (search-widget
1622 (widget-create
1623 'editable-field
1624 :size 40 :help-echo echo
1625 :action (lambda (widget &optional _event)
1626 (customize-apropos (split-string (widget-value widget)))))))
1627 (widget-insert " ")
1628 (widget-create-child-and-convert
1629 search-widget 'push-button
1630 :tag " Search "
1631 :help-echo echo :action
1632 (lambda (widget &optional _event)
1633 (customize-apropos (split-string (widget-value (widget-get widget :parent))))))
1634 (widget-insert "\n")))
1636 ;; The custom command buttons are also in the toolbar, so for a
1637 ;; time they were not inserted in the buffer if the toolbar was in use.
1638 ;; But it can be a little confusing for the buffer layout to
1639 ;; change according to whether or nor the toolbar is on, not to
1640 ;; mention that a custom buffer can in theory be created in a
1641 ;; frame with a toolbar, then later viewed in one without.
1642 ;; So now the buttons are always inserted in the buffer. (Bug#1326)
1643 (if custom-buffer-verbose-help
1644 (widget-insert "
1645 Operate on all settings in this buffer:\n"))
1646 (let ((button (lambda (tag action active help _icon _label)
1647 (widget-insert " ")
1648 (if (eval active)
1649 (widget-create 'push-button :tag tag
1650 :help-echo help :action action))))
1651 (commands custom-commands))
1652 (if custom-reset-button-menu
1653 (progn
1654 (widget-create 'push-button
1655 :tag " Revert... "
1656 :help-echo "Show a menu with reset operations."
1657 :mouse-down-action 'ignore
1658 :action 'custom-reset)
1659 (apply button (pop commands)) ; Apply
1660 (apply button (pop commands))) ; Apply and Save
1661 (apply button (pop commands)) ; Apply
1662 (apply button (pop commands)) ; Apply and Save
1663 (widget-insert "\n")
1664 (apply button (pop commands)) ; Undo
1665 (apply button (pop commands)) ; Reset
1666 (apply button (pop commands)) ; Erase
1667 (widget-insert " ")
1668 (pop commands) ; Help (omitted)
1669 (apply button (pop commands)))) ; Exit
1670 (widget-insert "\n\n"))
1672 ;; Now populate the custom buffer.
1673 (message "Creating customization items...")
1674 (buffer-disable-undo)
1675 (setq custom-options
1676 (if (= (length options) 1)
1677 (mapcar (lambda (entry)
1678 (widget-create (nth 1 entry)
1679 :documentation-shown t
1680 :custom-state 'unknown
1681 :tag (custom-unlispify-tag-name
1682 (nth 0 entry))
1683 :value (nth 0 entry)))
1684 options)
1685 (let ((count 0)
1686 (length (length options)))
1687 (mapcar (lambda (entry)
1688 (prog2
1689 (message "Creating customization items ...%2d%%"
1690 (/ (* 100.0 count) length))
1691 (widget-create (nth 1 entry)
1692 :tag (custom-unlispify-tag-name
1693 (nth 0 entry))
1694 :value (nth 0 entry))
1695 (setq count (1+ count))
1696 (unless (eq (preceding-char) ?\n)
1697 (widget-insert "\n"))
1698 (widget-insert "\n")))
1699 options))))
1700 (unless (eq (preceding-char) ?\n)
1701 (widget-insert "\n"))
1702 (message "Creating customization items ...done")
1703 (message "Resetting customization items...")
1704 (unless (eq custom-buffer-style 'tree)
1705 (mapc 'custom-magic-reset custom-options))
1706 (message "Resetting customization items...done")
1707 (message "Creating customization setup...")
1708 (widget-setup)
1709 (buffer-enable-undo)
1710 (goto-char (point-min))
1711 (message "Creating customization setup...done"))
1713 ;;; The Tree Browser.
1715 ;;;###autoload
1716 (defun customize-browse (&optional group)
1717 "Create a tree browser for the customize hierarchy."
1718 (interactive)
1719 (unless group
1720 (setq group 'emacs))
1721 (let ((name "*Customize Browser*"))
1722 (pop-to-buffer-same-window (custom-get-fresh-buffer name)))
1723 (Custom-mode)
1724 (widget-insert (format "\
1725 %s buttons; type RET or click mouse-1
1726 on a button to invoke its action.
1727 Invoke [+] to expand a group, and [-] to collapse an expanded group.\n"
1728 (if custom-raised-buttons
1729 "`Raised' text indicates"
1730 "Square brackets indicate")))
1733 (if custom-browse-only-groups
1734 (widget-insert "\
1735 Invoke the [Group] button below to edit that item in another window.\n\n")
1736 (widget-insert "Invoke the ")
1737 (widget-create 'item
1738 :format "%t"
1739 :tag "[Group]"
1740 :tag-glyph "folder")
1741 (widget-insert ", ")
1742 (widget-create 'item
1743 :format "%t"
1744 :tag "[Face]"
1745 :tag-glyph "face")
1746 (widget-insert ", and ")
1747 (widget-create 'item
1748 :format "%t"
1749 :tag "[Option]"
1750 :tag-glyph "option")
1751 (widget-insert " buttons below to edit that
1752 item in another window.\n\n"))
1753 (let ((custom-buffer-style 'tree))
1754 (widget-create 'custom-group
1755 :custom-last t
1756 :custom-state 'unknown
1757 :tag (custom-unlispify-tag-name group)
1758 :value group))
1759 (widget-setup)
1760 (goto-char (point-min)))
1762 (define-widget 'custom-browse-visibility 'item
1763 "Control visibility of items in the customize tree browser."
1764 :format "%[[%t]%]"
1765 :action 'custom-browse-visibility-action)
1767 (defun custom-browse-visibility-action (widget &rest _ignore)
1768 (let ((custom-buffer-style 'tree))
1769 (custom-toggle-parent widget)))
1771 (define-widget 'custom-browse-group-tag 'custom-group-link
1772 "Show parent in other window when activated."
1773 :tag "Group"
1774 :tag-glyph "folder"
1775 :action 'custom-browse-group-tag-action)
1777 (defun custom-browse-group-tag-action (widget &rest _ignore)
1778 (let ((parent (widget-get widget :parent)))
1779 (customize-group-other-window (widget-value parent))))
1781 (define-widget 'custom-browse-variable-tag 'custom-group-link
1782 "Show parent in other window when activated."
1783 :tag "Option"
1784 :tag-glyph "option"
1785 :action 'custom-browse-variable-tag-action)
1787 (defun custom-browse-variable-tag-action (widget &rest _ignore)
1788 (let ((parent (widget-get widget :parent)))
1789 (customize-variable-other-window (widget-value parent))))
1791 (define-widget 'custom-browse-face-tag 'custom-group-link
1792 "Show parent in other window when activated."
1793 :tag "Face"
1794 :tag-glyph "face"
1795 :action 'custom-browse-face-tag-action)
1797 (defun custom-browse-face-tag-action (widget &rest _ignore)
1798 (let ((parent (widget-get widget :parent)))
1799 (customize-face-other-window (widget-value parent))))
1801 (defconst custom-browse-alist '((" " "space")
1802 (" | " "vertical")
1803 ("-\\ " "top")
1804 (" |-" "middle")
1805 (" `-" "bottom")))
1807 (defun custom-browse-insert-prefix (prefix)
1808 "Insert PREFIX. On XEmacs convert it to line graphics."
1809 ;; Fixme: do graphics.
1810 (if nil ; (featurep 'xemacs)
1811 (progn
1812 (insert "*")
1813 (while (not (string-equal prefix ""))
1814 (let ((entry (substring prefix 0 3)))
1815 (setq prefix (substring prefix 3))
1816 (let ((overlay (make-overlay (1- (point)) (point) nil t nil))
1817 (name (nth 1 (assoc entry custom-browse-alist))))
1818 (overlay-put overlay 'end-glyph (widget-glyph-find name entry))
1819 (overlay-put overlay 'start-open t)
1820 (overlay-put overlay 'end-open t)))))
1821 (insert prefix)))
1823 ;;; Modification of Basic Widgets.
1825 ;; We add extra properties to the basic widgets needed here. This is
1826 ;; fine, as long as we are careful to stay within our own namespace.
1828 ;; We want simple widgets to be displayed by default, but complex
1829 ;; widgets to be hidden.
1831 ;; This widget type is obsolete as of Emacs 24.1.
1832 (widget-put (get 'item 'widget-type) :custom-show t)
1833 (widget-put (get 'editable-field 'widget-type)
1834 :custom-show (lambda (_widget value)
1835 (let ((pp (pp-to-string value)))
1836 (cond ((string-match-p "\n" pp)
1837 nil)
1838 ((> (length pp) 40)
1839 nil)
1840 (t t)))))
1841 (widget-put (get 'menu-choice 'widget-type) :custom-show t)
1843 ;;; The `custom-manual' Widget.
1845 (define-widget 'custom-manual 'info-link
1846 "Link to the manual entry for this customization option."
1847 :help-echo "Read the manual entry for this option."
1848 :keymap custom-mode-link-map
1849 :follow-link 'mouse-face
1850 :button-face 'custom-link
1851 :mouse-face 'highlight
1852 :pressed-face 'highlight
1853 :tag "Manual")
1855 ;;; The `custom-magic' Widget.
1857 (defgroup custom-magic-faces nil
1858 "Faces used by the magic button."
1859 :group 'custom-faces
1860 :group 'custom-buffer)
1862 (defface custom-invalid '((((class color))
1863 :foreground "yellow1" :background "red1")
1864 (t :weight bold :slant italic :underline t))
1865 "Face used when the customize item is invalid."
1866 :group 'custom-magic-faces)
1868 (defface custom-rogue '((((class color))
1869 :foreground "pink" :background "black")
1870 (t :underline t))
1871 "Face used when the customize item is not defined for customization."
1872 :group 'custom-magic-faces)
1874 (defface custom-modified '((((min-colors 88) (class color))
1875 :foreground "white" :background "blue1")
1876 (((class color))
1877 :foreground "white" :background "blue")
1878 (t :slant italic))
1879 "Face used when the customize item has been modified."
1880 :group 'custom-magic-faces)
1882 (defface custom-set '((((min-colors 88) (class color))
1883 :foreground "blue1" :background "white")
1884 (((class color))
1885 :foreground "blue" :background "white")
1886 (t :slant italic))
1887 "Face used when the customize item has been set."
1888 :group 'custom-magic-faces)
1890 (defface custom-changed '((((min-colors 88) (class color))
1891 :foreground "white" :background "blue1")
1892 (((class color))
1893 :foreground "white" :background "blue")
1894 (t :slant italic))
1895 "Face used when the customize item has been changed."
1896 :group 'custom-magic-faces)
1898 (defface custom-themed '((((min-colors 88) (class color))
1899 :foreground "white" :background "blue1")
1900 (((class color))
1901 :foreground "white" :background "blue")
1902 (t :slant italic))
1903 "Face used when the customize item has been set by a theme."
1904 :group 'custom-magic-faces)
1906 (defface custom-saved '((t :underline t))
1907 "Face used when the customize item has been saved."
1908 :group 'custom-magic-faces)
1910 (defconst custom-magic-alist
1911 '((nil "#" underline "\
1912 UNINITIALIZED, you should not see this.")
1913 (unknown "?" italic "\
1914 UNKNOWN, you should not see this.")
1915 (hidden "-" default "\
1916 HIDDEN, invoke \"Show\" in the previous line to show." "\
1917 group now hidden, invoke \"Show\", above, to show contents.")
1918 (invalid "x" custom-invalid "\
1919 INVALID, the displayed value cannot be set.")
1920 (modified "*" custom-modified "\
1921 EDITED, shown value does not take effect until you set or save it." "\
1922 something in this group has been edited but not set.")
1923 (set "+" custom-set "\
1924 SET for current session only." "\
1925 something in this group has been set but not saved.")
1926 (changed ":" custom-changed "\
1927 CHANGED outside Customize." "\
1928 something in this group has been changed outside customize.")
1929 (saved "!" custom-saved "\
1930 SAVED and set." "\
1931 something in this group has been set and saved.")
1932 (themed "o" custom-themed "\
1933 THEMED." "\
1934 visible group members are set by enabled themes.")
1935 (rogue "@" custom-rogue "\
1936 NO CUSTOMIZATION DATA; not intended to be customized." "\
1937 something in this group is not prepared for customization.")
1938 (standard " " nil "\
1939 STANDARD." "\
1940 visible group members are all at standard values."))
1941 "Alist of customize option states.
1942 Each entry is of the form (STATE MAGIC FACE ITEM-DESC [ GROUP-DESC ]), where
1944 STATE is one of the following symbols:
1946 `nil'
1947 For internal use, should never occur.
1948 `unknown'
1949 For internal use, should never occur.
1950 `hidden'
1951 This item is not being displayed.
1952 `invalid'
1953 This item is modified, but has an invalid form.
1954 `modified'
1955 This item is modified, and has a valid form.
1956 `set'
1957 This item has been set but not saved.
1958 `changed'
1959 The current value of this item has been changed outside Customize.
1960 `saved'
1961 This item is marked for saving.
1962 `rogue'
1963 This item has no customization information.
1964 `themed'
1965 This item was set by an enabled Custom theme.
1966 `standard'
1967 This item is unchanged from the standard setting.
1969 MAGIC is a string used to present that state.
1971 FACE is a face used to present the state.
1973 ITEM-DESC is a string describing the state for options.
1975 GROUP-DESC is a string describing the state for groups. If this is
1976 left out, ITEM-DESC will be used.
1978 The string %c in either description will be replaced with the
1979 category of the item. These are `group', `option', and `face'.
1981 The list should be sorted most significant first.")
1983 (defcustom custom-magic-show 'long
1984 "If non-nil, show textual description of the state.
1985 If `long', show a full-line description, not just one word."
1986 :type '(choice (const :tag "no" nil)
1987 (const long)
1988 (other :tag "short" short))
1989 :group 'custom-buffer)
1991 (defcustom custom-magic-show-hidden '(option face)
1992 "Control whether the State button is shown for hidden items.
1993 The value should be a list with the custom categories where the State
1994 button should be visible. Possible categories are `group', `option',
1995 and `face'."
1996 :type '(set (const group) (const option) (const face))
1997 :group 'custom-buffer)
1999 (defcustom custom-magic-show-button nil
2000 "Show a \"magic\" button indicating the state of each customization option."
2001 :type 'boolean
2002 :group 'custom-buffer)
2004 (define-widget 'custom-magic 'default
2005 "Show and manipulate state for a customization option."
2006 :format "%v"
2007 :action 'widget-parent-action
2008 :notify 'ignore
2009 :value-get 'ignore
2010 :value-create 'custom-magic-value-create
2011 :value-delete 'widget-children-value-delete)
2013 (defun widget-magic-mouse-down-action (widget &optional _event)
2014 ;; Non-nil unless hidden.
2015 (not (eq (widget-get (widget-get (widget-get widget :parent) :parent)
2016 :custom-state)
2017 'hidden)))
2019 (defun custom-magic-value-create (widget)
2020 "Create compact status report for WIDGET."
2021 (let* ((parent (widget-get widget :parent))
2022 (state (widget-get parent :custom-state))
2023 (hidden (eq state 'hidden))
2024 (entry (assq state custom-magic-alist))
2025 (magic (nth 1 entry))
2026 (face (nth 2 entry))
2027 (category (widget-get parent :custom-category))
2028 (text (or (and (eq category 'group)
2029 (nth 4 entry))
2030 (nth 3 entry)))
2031 (form (widget-get parent :custom-form))
2032 children)
2033 (unless (eq state 'hidden)
2034 (while (string-match "\\`\\(.*\\)%c\\(.*\\)\\'" text)
2035 (setq text (concat (match-string 1 text)
2036 (symbol-name category)
2037 (match-string 2 text))))
2038 (when (and custom-magic-show
2039 (or (not hidden)
2040 (memq category custom-magic-show-hidden)))
2041 (insert " ")
2042 (when (and (eq category 'group)
2043 (not (and (eq custom-buffer-style 'links)
2044 (> (widget-get parent :custom-level) 1))))
2045 (insert-char ?\s (* custom-buffer-indent
2046 (widget-get parent :custom-level))))
2047 (push (widget-create-child-and-convert
2048 widget 'choice-item
2049 :help-echo "Change the state of this item."
2050 :format (if hidden "%t" "%[%t%]")
2051 :button-prefix 'widget-push-button-prefix
2052 :button-suffix 'widget-push-button-suffix
2053 :mouse-down-action 'widget-magic-mouse-down-action
2054 :tag " State ")
2055 children)
2056 (insert ": ")
2057 (let ((start (point)))
2058 (if (eq custom-magic-show 'long)
2059 (insert text)
2060 (insert (symbol-name state)))
2061 (cond ((eq form 'lisp)
2062 (insert " (lisp)"))
2063 ((eq form 'mismatch)
2064 (insert " (mismatch)")))
2065 (put-text-property start (point) 'face 'custom-state))
2066 (insert "\n"))
2067 (when (and (eq category 'group)
2068 (not (and (eq custom-buffer-style 'links)
2069 (> (widget-get parent :custom-level) 1))))
2070 (insert-char ?\s (* custom-buffer-indent
2071 (widget-get parent :custom-level))))
2072 (when custom-magic-show-button
2073 (when custom-magic-show
2074 (let ((indent (widget-get parent :indent)))
2075 (when indent
2076 (insert-char ? indent))))
2077 (push (widget-create-child-and-convert
2078 widget 'choice-item
2079 :mouse-down-action 'widget-magic-mouse-down-action
2080 :button-face face
2081 :button-prefix ""
2082 :button-suffix ""
2083 :help-echo "Change the state."
2084 :format (if hidden "%t" "%[%t%]")
2085 :tag (if (memq form '(lisp mismatch))
2086 (concat "(" magic ")")
2087 (concat "[" magic "]")))
2088 children)
2089 (insert " "))
2090 (widget-put widget :children children))))
2092 (defun custom-magic-reset (widget)
2093 "Redraw the :custom-magic property of WIDGET."
2094 (let ((magic (widget-get widget :custom-magic)))
2095 (when magic
2096 (widget-value-set magic (widget-value magic)))))
2098 ;;; The `custom' Widget.
2100 (defface custom-button
2101 '((((type x w32 ns) (class color)) ; Like default mode line
2102 :box (:line-width 2 :style released-button)
2103 :background "lightgrey" :foreground "black"))
2104 "Face for custom buffer buttons if `custom-raised-buttons' is non-nil."
2105 :version "21.1"
2106 :group 'custom-faces)
2108 (defface custom-button-mouse
2109 '((((type x w32 ns) (class color))
2110 :box (:line-width 2 :style released-button)
2111 :background "grey90" :foreground "black")
2113 ;; This is for text terminals that support mouse, like GPM mouse
2114 ;; or the MS-DOS terminal: inverse-video makes the button stand
2115 ;; out on mouse-over.
2116 :inverse-video t))
2117 "Mouse face for custom buffer buttons if `custom-raised-buttons' is non-nil."
2118 :version "22.1"
2119 :group 'custom-faces)
2121 (defface custom-button-unraised
2122 '((t :inherit underline))
2123 "Face for custom buffer buttons if `custom-raised-buttons' is nil."
2124 :version "22.1"
2125 :group 'custom-faces)
2127 (setq custom-button
2128 (if custom-raised-buttons 'custom-button 'custom-button-unraised))
2130 (setq custom-button-mouse
2131 (if custom-raised-buttons 'custom-button-mouse 'highlight))
2133 (defface custom-button-pressed
2134 '((((type x w32 ns) (class color))
2135 :box (:line-width 2 :style pressed-button)
2136 :background "lightgrey" :foreground "black")
2137 (t :inverse-video t))
2138 "Face for pressed custom buttons if `custom-raised-buttons' is non-nil."
2139 :version "21.1"
2140 :group 'custom-faces)
2142 (defface custom-button-pressed-unraised
2143 '((default :inherit custom-button-unraised)
2144 (((class color) (background light)) :foreground "magenta4")
2145 (((class color) (background dark)) :foreground "violet"))
2146 "Face for pressed custom buttons if `custom-raised-buttons' is nil."
2147 :version "22.1"
2148 :group 'custom-faces)
2150 (setq custom-button-pressed
2151 (if custom-raised-buttons
2152 'custom-button-pressed
2153 'custom-button-pressed-unraised))
2155 (defface custom-documentation '((t nil))
2156 "Face used for documentation strings in customization buffers."
2157 :group 'custom-faces)
2159 (defface custom-state '((((class color) (background dark))
2160 :foreground "lime green")
2161 (((class color) (background light))
2162 :foreground "dark green"))
2163 "Face used for State descriptions in the customize buffer."
2164 :group 'custom-faces)
2166 (defface custom-link '((t :inherit link))
2167 "Face for links in customization buffers."
2168 :version "22.1"
2169 :group 'custom-faces)
2171 (define-widget 'custom 'default
2172 "Customize a user option."
2173 :format "%v"
2174 :convert-widget 'custom-convert-widget
2175 :notify 'custom-notify
2176 :custom-prefix ""
2177 :custom-level 1
2178 :custom-state 'hidden
2179 :documentation-property 'widget-subclass-responsibility
2180 :value-create 'widget-subclass-responsibility
2181 :value-delete 'widget-children-value-delete
2182 :value-get 'widget-value-value-get
2183 :validate 'widget-children-validate
2184 :match (lambda (_widget value) (symbolp value)))
2186 (defun custom-convert-widget (widget)
2187 "Initialize :value and :tag from :args in WIDGET."
2188 (let ((args (widget-get widget :args)))
2189 (when args
2190 (widget-put widget :value (widget-apply widget
2191 :value-to-internal (car args)))
2192 (widget-put widget :tag (custom-unlispify-tag-name (car args)))
2193 (widget-put widget :args nil)))
2194 widget)
2196 (defun custom-notify (widget &rest args)
2197 "Keep track of changes."
2198 (let ((state (widget-get widget :custom-state)))
2199 (unless (eq state 'modified)
2200 (unless (memq state '(nil unknown hidden))
2201 (widget-put widget :custom-state 'modified))
2202 (custom-magic-reset widget)
2203 (apply 'widget-default-notify widget args))))
2205 (defun custom-redraw (widget)
2206 "Redraw WIDGET with current settings."
2207 (let ((line (count-lines (point-min) (point)))
2208 (column (current-column))
2209 (pos (point))
2210 (from (marker-position (widget-get widget :from)))
2211 (to (marker-position (widget-get widget :to))))
2212 (save-excursion
2213 (widget-value-set widget (widget-value widget))
2214 (custom-redraw-magic widget))
2215 (when (and (>= pos from) (<= pos to))
2216 (condition-case nil
2217 (progn
2218 (goto-char (point-min))
2219 (forward-line (if (> column 0)
2220 (1- line)
2221 line))
2222 (move-to-column column))
2223 (error nil)))))
2225 (defun custom-redraw-magic (widget)
2226 "Redraw WIDGET state with current settings."
2227 (while widget
2228 (let ((magic (widget-get widget :custom-magic)))
2229 (cond (magic
2230 (widget-value-set magic (widget-value magic))
2231 (when (setq widget (widget-get widget :group))
2232 (custom-group-state-update widget)))
2234 (setq widget nil)))))
2235 (widget-setup))
2237 (defun custom-show (widget value)
2238 "Non-nil if WIDGET should be shown with VALUE by default."
2239 (declare (obsolete "this widget type is no longer supported." "24.1"))
2240 (let ((show (widget-get widget :custom-show)))
2241 (if (functionp show)
2242 (funcall show widget value)
2243 show)))
2245 (defun custom-load-widget (widget)
2246 "Load all dependencies for WIDGET."
2247 (custom-load-symbol (widget-value widget)))
2249 (defun custom-unloaded-symbol-p (symbol)
2250 "Return non-nil if the dependencies of SYMBOL have not yet been loaded."
2251 (let ((found nil)
2252 (loads (get symbol 'custom-loads))
2253 load)
2254 (while loads
2255 (setq load (car loads)
2256 loads (cdr loads))
2257 (cond ((symbolp load)
2258 (unless (featurep load)
2259 (setq found t)))
2260 ((assoc load load-history))
2261 ((assoc (locate-library load) load-history)
2262 (message nil))
2264 (setq found t))))
2265 found))
2267 (defun custom-unloaded-widget-p (widget)
2268 "Return non-nil if the dependencies of WIDGET have not yet been loaded."
2269 (custom-unloaded-symbol-p (widget-value widget)))
2271 (defun custom-toggle-hide (widget)
2272 "Toggle visibility of WIDGET."
2273 (custom-load-widget widget)
2274 (let ((state (widget-get widget :custom-state)))
2275 (cond ((memq state '(invalid modified set))
2276 (error "There are unsaved changes"))
2277 ((eq state 'hidden)
2278 (widget-put widget :custom-state 'unknown))
2280 (widget-put widget :documentation-shown nil)
2281 (widget-put widget :custom-state 'hidden)))
2282 (custom-redraw widget)
2283 (widget-setup)))
2285 (defun custom-toggle-parent (widget &rest _ignore)
2286 "Toggle visibility of parent of WIDGET."
2287 (custom-toggle-hide (widget-get widget :parent)))
2289 (defun custom-add-see-also (widget &optional prefix)
2290 "Add `See also ...' to WIDGET if there are any links.
2291 Insert PREFIX first if non-nil."
2292 (let* ((symbol (widget-get widget :value))
2293 (links (get symbol 'custom-links))
2294 (many (> (length links) 2))
2295 (buttons (widget-get widget :buttons))
2296 (indent (widget-get widget :indent)))
2297 (when links
2298 (when indent
2299 (insert-char ?\s indent))
2300 (when prefix
2301 (insert prefix))
2302 (insert "See also ")
2303 (while links
2304 (push (widget-create-child-and-convert
2305 widget (car links)
2306 :button-face 'custom-link
2307 :mouse-face 'highlight
2308 :pressed-face 'highlight)
2309 buttons)
2310 (setq links (cdr links))
2311 (cond ((null links)
2312 (insert ".\n"))
2313 ((null (cdr links))
2314 (if many
2315 (insert ", and ")
2316 (insert " and ")))
2318 (insert ", "))))
2319 (widget-put widget :buttons buttons))))
2321 (defun custom-add-parent-links (widget &optional initial-string _doc-initial-string)
2322 "Add \"Parent groups: ...\" to WIDGET if the group has parents.
2323 The value is non-nil if any parents were found.
2324 If INITIAL-STRING is non-nil, use that rather than \"Parent groups:\"."
2325 (let ((name (widget-value widget))
2326 (type (widget-type widget))
2327 (buttons (widget-get widget :buttons))
2328 (start (point))
2329 (parents nil))
2330 (insert (or initial-string "Groups:"))
2331 (mapatoms (lambda (symbol)
2332 (when (member (list name type) (get symbol 'custom-group))
2333 (insert " ")
2334 (push (widget-create-child-and-convert
2335 widget 'custom-group-link
2336 :tag (custom-unlispify-tag-name symbol)
2337 symbol)
2338 buttons)
2339 (setq parents (cons symbol parents)))))
2340 (if parents
2341 (insert "\n")
2342 (delete-region start (point)))
2343 (widget-put widget :buttons buttons)
2344 parents))
2346 ;;; The `custom-comment' Widget.
2348 ;; like the editable field
2349 (defface custom-comment '((((type tty))
2350 :background "yellow3"
2351 :foreground "black")
2352 (((class grayscale color)
2353 (background light))
2354 :background "gray85")
2355 (((class grayscale color)
2356 (background dark))
2357 :background "dim gray")
2359 :slant italic))
2360 "Face used for comments on variables or faces."
2361 :version "21.1"
2362 :group 'custom-faces)
2364 ;; like font-lock-comment-face
2365 (defface custom-comment-tag
2366 '((((class color) (background dark)) :foreground "gray80")
2367 (((class color) (background light)) :foreground "blue4")
2368 (((class grayscale) (background light))
2369 :foreground "DimGray" :weight bold :slant italic)
2370 (((class grayscale) (background dark))
2371 :foreground "LightGray" :weight bold :slant italic)
2372 (t :weight bold))
2373 "Face used for the comment tag on variables or faces."
2374 :group 'custom-faces)
2376 (define-widget 'custom-comment 'string
2377 "User comment."
2378 :tag "Comment"
2379 :help-echo "Edit a comment here."
2380 :sample-face 'custom-comment-tag
2381 :value-face 'custom-comment
2382 :shown nil
2383 :create 'custom-comment-create)
2385 (defun custom-comment-create (widget)
2386 (let* ((null-comment (equal "" (widget-value widget))))
2387 (if (or (widget-get (widget-get widget :parent) :comment-shown)
2388 (not null-comment))
2389 (widget-default-create widget)
2390 ;; `widget-default-delete' expects markers in these slots --
2391 ;; maybe it shouldn't.
2392 (widget-put widget :from (point-marker))
2393 (widget-put widget :to (point-marker)))))
2395 (defun custom-comment-hide (widget)
2396 (widget-put (widget-get widget :parent) :comment-shown nil))
2398 ;; Those functions are for the menu. WIDGET is NOT the comment widget. It's
2399 ;; the global custom one
2400 (defun custom-comment-show (widget)
2401 (widget-put widget :comment-shown t)
2402 (custom-redraw widget)
2403 (widget-setup))
2405 (defun custom-comment-invisible-p (widget)
2406 (let ((val (widget-value (widget-get widget :comment-widget))))
2407 (and (equal "" val)
2408 (not (widget-get widget :comment-shown)))))
2410 ;;; The `custom-variable' Widget.
2412 (defface custom-variable-tag
2413 `((((class color) (background dark))
2414 :foreground "light blue" :weight bold)
2415 (((min-colors 88) (class color) (background light))
2416 :foreground "blue1" :weight bold)
2417 (((class color) (background light))
2418 :foreground "blue" :weight bold)
2419 (t :weight bold))
2420 "Face used for unpushable variable tags."
2421 :group 'custom-faces)
2423 (defface custom-variable-button '((t :underline t :weight bold))
2424 "Face used for pushable variable tags."
2425 :group 'custom-faces)
2427 (defcustom custom-variable-default-form 'edit
2428 "Default form of displaying variable values."
2429 :type '(choice (const edit)
2430 (const lisp))
2431 :group 'custom-buffer
2432 :version "20.3")
2434 (defun custom-variable-documentation (variable)
2435 "Return documentation of VARIABLE for use in Custom buffer.
2436 Normally just return the docstring. But if VARIABLE automatically
2437 becomes buffer local when set, append a message to that effect."
2438 (format "%s%s" (documentation-property variable 'variable-documentation)
2439 (if (and (local-variable-if-set-p variable)
2440 (or (not (local-variable-p variable))
2441 (with-temp-buffer
2442 (local-variable-if-set-p variable))))
2444 This variable automatically becomes buffer-local when set outside Custom.
2445 However, setting it through Custom sets the default value."
2446 "")))
2448 (define-widget 'custom-variable 'custom
2449 "A widget for displaying a Custom variable.
2450 The following properties have special meanings for this widget:
2452 :hidden-states should be a list of widget states for which the
2453 widget's initial contents are to be hidden.
2455 :custom-form should be a symbol describing how to display and
2456 edit the variable---either `edit' (using edit widgets),
2457 `lisp' (as a Lisp sexp), or `mismatch' (should not happen);
2458 if nil, use the return value of `custom-variable-default-form'.
2460 :shown-value, if non-nil, should be a list whose `car' is the
2461 variable value to display in place of the current value.
2463 :custom-style describes the widget interface style; nil is the
2464 default style, while `simple' means a simpler interface that
2465 inhibits the magic custom-state widget."
2466 :format "%v"
2467 :help-echo "Set or reset this variable."
2468 :documentation-property #'custom-variable-documentation
2469 :custom-category 'option
2470 :custom-state nil
2471 :custom-menu 'custom-variable-menu-create
2472 :custom-form nil
2473 :value-create 'custom-variable-value-create
2474 :action 'custom-variable-action
2475 :hidden-states '(standard)
2476 :custom-set 'custom-variable-set
2477 :custom-mark-to-save 'custom-variable-mark-to-save
2478 :custom-reset-current 'custom-redraw
2479 :custom-reset-saved 'custom-variable-reset-saved
2480 :custom-reset-standard 'custom-variable-reset-standard
2481 :custom-mark-to-reset-standard 'custom-variable-mark-to-reset-standard
2482 :custom-standard-value 'custom-variable-standard-value
2483 :custom-state-set-and-redraw 'custom-variable-state-set-and-redraw)
2485 (defun custom-variable-type (symbol)
2486 "Return a widget suitable for editing the value of SYMBOL.
2487 If SYMBOL has a `custom-type' property, use that.
2488 Otherwise, try matching SYMBOL against `custom-guess-name-alist' and
2489 try matching its doc string against `custom-guess-doc-alist'."
2490 (let* ((type (or (get symbol 'custom-type)
2491 (and (not (get symbol 'standard-value))
2492 (custom-guess-type symbol))
2493 'sexp))
2494 (options (get symbol 'custom-options))
2495 (tmp (if (listp type)
2496 (copy-sequence type)
2497 (list type))))
2498 (when options
2499 (widget-put tmp :options options))
2500 tmp))
2502 (defun custom-variable-value-create (widget)
2503 "Here is where you edit the variable's value."
2504 (custom-load-widget widget)
2505 (unless (widget-get widget :custom-form)
2506 (widget-put widget :custom-form custom-variable-default-form))
2507 (let* ((buttons (widget-get widget :buttons))
2508 (children (widget-get widget :children))
2509 (form (widget-get widget :custom-form))
2510 (symbol (widget-get widget :value))
2511 (tag (widget-get widget :tag))
2512 (type (custom-variable-type symbol))
2513 (conv (widget-convert type))
2514 (get (or (get symbol 'custom-get) 'default-value))
2515 (prefix (widget-get widget :custom-prefix))
2516 (last (widget-get widget :custom-last))
2517 (style (widget-get widget :custom-style))
2518 (value (let ((shown-value (widget-get widget :shown-value)))
2519 (cond (shown-value
2520 (car shown-value))
2521 ((default-boundp symbol)
2522 (funcall get symbol))
2523 (t (widget-get conv :value)))))
2524 (state (or (widget-get widget :custom-state)
2525 (if (memq (custom-variable-state symbol value)
2526 (widget-get widget :hidden-states))
2527 'hidden))))
2529 ;; If we don't know the state, see if we need to edit it in lisp form.
2530 (unless state
2531 (setq state (if (custom-show type value) 'unknown 'hidden)))
2532 (when (eq state 'unknown)
2533 (unless (widget-apply conv :match value)
2534 (setq form 'mismatch)))
2535 ;; Now we can create the child widget.
2536 (cond ((eq custom-buffer-style 'tree)
2537 (insert prefix (if last " `--- " " |--- "))
2538 (push (widget-create-child-and-convert
2539 widget 'custom-browse-variable-tag)
2540 buttons)
2541 (insert " " tag "\n")
2542 (widget-put widget :buttons buttons))
2543 ((eq state 'hidden)
2544 ;; Indicate hidden value.
2545 (push (widget-create-child-and-convert
2546 widget 'custom-visibility
2547 :help-echo "Show the value of this option."
2548 :on-glyph "down"
2549 :on "Hide"
2550 :off-glyph "right"
2551 :off "Show Value"
2552 :action 'custom-toggle-hide-variable
2553 nil)
2554 buttons)
2555 (insert " ")
2556 (push (widget-create-child-and-convert
2557 widget 'item
2558 :format "%{%t%} "
2559 :sample-face 'custom-variable-tag
2560 :tag tag
2561 :parent widget)
2562 buttons))
2563 ((memq form '(lisp mismatch))
2564 (push (widget-create-child-and-convert
2565 widget 'custom-visibility
2566 :help-echo "Hide the value of this option."
2567 :on "Hide"
2568 :off "Show"
2569 :on-glyph "down"
2570 :off-glyph "right"
2571 :action 'custom-toggle-hide-variable
2573 buttons)
2574 (insert " ")
2575 ;; This used to try presenting the saved value or the
2576 ;; standard value, but it seems more intuitive to present
2577 ;; the current value (Bug#7600).
2578 (let* ((value (cond ((default-boundp symbol)
2579 (custom-quote (funcall get symbol)))
2581 (custom-quote (widget-get conv :value))))))
2582 (insert (symbol-name symbol) ": ")
2583 (push (widget-create-child-and-convert
2584 widget 'sexp
2585 :button-face 'custom-variable-button-face
2586 :format "%v"
2587 :tag (symbol-name symbol)
2588 :parent widget
2589 :value value)
2590 children)))
2592 ;; Edit mode.
2593 (push (widget-create-child-and-convert
2594 widget 'custom-visibility
2595 :help-echo "Hide or show this option."
2596 :on "Hide"
2597 :off "Show"
2598 :on-glyph "down"
2599 :off-glyph "right"
2600 :action 'custom-toggle-hide-variable
2602 buttons)
2603 (insert " ")
2604 (let* ((format (widget-get type :format))
2605 tag-format value-format)
2606 (unless (string-match ":" format)
2607 (error "Bad format"))
2608 (setq tag-format (substring format 0 (match-end 0)))
2609 (setq value-format (substring format (match-end 0)))
2610 (push (widget-create-child-and-convert
2611 widget 'item
2612 :format tag-format
2613 :action 'custom-tag-action
2614 :help-echo "Change value of this option."
2615 :mouse-down-action 'custom-tag-mouse-down-action
2616 :button-face 'custom-variable-button
2617 :sample-face 'custom-variable-tag
2618 tag)
2619 buttons)
2620 (push (widget-create-child-and-convert
2621 widget type
2622 :format value-format
2623 :value value)
2624 children))))
2625 (unless (eq custom-buffer-style 'tree)
2626 (unless (eq (preceding-char) ?\n)
2627 (widget-insert "\n"))
2628 ;; Create the magic button.
2629 (unless (eq style 'simple)
2630 (let ((magic (widget-create-child-and-convert
2631 widget 'custom-magic nil)))
2632 (widget-put widget :custom-magic magic)
2633 (push magic buttons)))
2634 (widget-put widget :buttons buttons)
2635 ;; Insert documentation.
2636 (widget-put widget :documentation-indent 3)
2637 (unless (and (eq style 'simple)
2638 (eq state 'hidden))
2639 (widget-add-documentation-string-button
2640 widget :visibility-widget 'custom-visibility))
2642 ;; The comment field
2643 (unless (eq state 'hidden)
2644 (let* ((comment (get symbol 'variable-comment))
2645 (comment-widget
2646 (widget-create-child-and-convert
2647 widget 'custom-comment
2648 :parent widget
2649 :value (or comment ""))))
2650 (widget-put widget :comment-widget comment-widget)
2651 ;; Don't push it !!! Custom assumes that the first child is the
2652 ;; value one.
2653 (setq children (append children (list comment-widget)))))
2654 ;; Update the rest of the properties.
2655 (widget-put widget :custom-form form)
2656 (widget-put widget :children children)
2657 ;; Now update the state.
2658 (if (eq state 'hidden)
2659 (widget-put widget :custom-state state)
2660 (custom-variable-state-set widget))
2661 ;; See also.
2662 (unless (eq state 'hidden)
2663 (when (eq (widget-get widget :custom-level) 1)
2664 (custom-add-parent-links widget))
2665 (custom-add-see-also widget)))))
2667 (defun custom-toggle-hide-variable (visibility-widget &rest _ignore)
2668 "Toggle the visibility of a `custom-variable' parent widget.
2669 By default, this signals an error if the parent has unsaved
2670 changes. If the parent has a `simple' :custom-style property,
2671 the present value is saved to its :shown-value property instead."
2672 (let ((widget (widget-get visibility-widget :parent)))
2673 (unless (eq (widget-type widget) 'custom-variable)
2674 (error "Invalid widget type"))
2675 (custom-load-widget widget)
2676 (let ((state (widget-get widget :custom-state)))
2677 (if (eq state 'hidden)
2678 (widget-put widget :custom-state 'unknown)
2679 ;; In normal interface, widget can't be hidden if modified.
2680 (when (memq state '(invalid modified set))
2681 (if (eq (widget-get widget :custom-style) 'simple)
2682 (widget-put widget :shown-value
2683 (list (widget-value
2684 (car-safe
2685 (widget-get widget :children)))))
2686 (error "There are unsaved changes")))
2687 (widget-put widget :documentation-shown nil)
2688 (widget-put widget :custom-state 'hidden))
2689 (custom-redraw widget)
2690 (widget-setup))))
2692 (defun custom-tag-action (widget &rest args)
2693 "Pass :action to first child of WIDGET's parent."
2694 (apply 'widget-apply (car (widget-get (widget-get widget :parent) :children))
2695 :action args))
2697 (defun custom-tag-mouse-down-action (widget &rest args)
2698 "Pass :mouse-down-action to first child of WIDGET's parent."
2699 (apply 'widget-apply (car (widget-get (widget-get widget :parent) :children))
2700 :mouse-down-action args))
2702 (defun custom-variable-state (symbol val)
2703 "Return the state of SYMBOL if its value is VAL.
2704 If SYMBOL has a non-nil `custom-get' property, it overrides VAL.
2705 Possible return values are `standard', `saved', `set', `themed',
2706 `changed', and `rogue'."
2707 (let* ((get (or (get symbol 'custom-get) 'default-value))
2708 (value (if (default-boundp symbol)
2709 (funcall get symbol)
2710 val))
2711 (comment (get symbol 'variable-comment))
2713 temp)
2714 (cond ((progn (setq tmp (get symbol 'customized-value))
2715 (setq temp
2716 (get symbol 'customized-variable-comment))
2717 (or tmp temp))
2718 (if (condition-case nil
2719 (and (equal value (eval (car tmp)))
2720 (equal comment temp))
2721 (error nil))
2722 'set
2723 'changed))
2724 ((progn (setq tmp (get symbol 'theme-value))
2725 (setq temp (get symbol 'saved-variable-comment))
2726 (or tmp temp))
2727 (if (condition-case nil
2728 (and (equal comment temp)
2729 (equal value
2730 (eval
2731 (car (custom-variable-theme-value
2732 symbol)))))
2733 (error nil))
2734 (cond
2735 ((eq (caar tmp) 'user) 'saved)
2736 ((eq (caar tmp) 'changed)
2737 (if (condition-case nil
2738 (and (null comment)
2739 (equal value
2740 (eval
2741 (car (get symbol 'standard-value)))))
2742 (error nil))
2743 ;; The value was originally set outside
2744 ;; custom, but it was set to the standard
2745 ;; value (probably an autoloaded defcustom).
2746 'standard
2747 'changed))
2748 (t 'themed))
2749 'changed))
2750 ((setq tmp (get symbol 'standard-value))
2751 (if (condition-case nil
2752 (and (equal value (eval (car tmp)))
2753 (equal comment nil))
2754 (error nil))
2755 'standard
2756 'changed))
2757 (t 'rogue))))
2759 (defun custom-variable-state-set (widget &optional state)
2760 "Set the state of WIDGET to STATE.
2761 If STATE is nil, the value is computed by `custom-variable-state'."
2762 (widget-put widget :custom-state
2763 (or state (custom-variable-state (widget-value widget)
2764 (widget-get widget :value)))))
2766 (defun custom-variable-standard-value (widget)
2767 (get (widget-value widget) 'standard-value))
2769 (defvar custom-variable-menu
2770 `(("Set for Current Session" custom-variable-set
2771 (lambda (widget)
2772 (eq (widget-get widget :custom-state) 'modified)))
2773 ;; Note that in all the backquoted code in this file, we test
2774 ;; init-file-user rather than user-init-file. This is in case
2775 ;; cus-edit is loaded by something in site-start.el, because
2776 ;; user-init-file is not set at that stage.
2777 ;; http://lists.gnu.org/archive/html/emacs-devel/2007-10/msg00310.html
2778 ,@(when (or custom-file init-file-user)
2779 '(("Save for Future Sessions" custom-variable-save
2780 (lambda (widget)
2781 (memq (widget-get widget :custom-state)
2782 '(modified set changed rogue))))))
2783 ("Undo Edits" custom-redraw
2784 (lambda (widget)
2785 (and (default-boundp (widget-value widget))
2786 (memq (widget-get widget :custom-state) '(modified changed)))))
2787 ("Revert This Session's Customization" custom-variable-reset-saved
2788 (lambda (widget)
2789 (memq (widget-get widget :custom-state)
2790 '(modified set changed rogue))))
2791 ,@(when (or custom-file init-file-user)
2792 '(("Erase Customization" custom-variable-reset-standard
2793 (lambda (widget)
2794 (and (get (widget-value widget) 'standard-value)
2795 (memq (widget-get widget :custom-state)
2796 '(modified set changed saved rogue)))))))
2797 ("Set to Backup Value" custom-variable-reset-backup
2798 (lambda (widget)
2799 (get (widget-value widget) 'backup-value)))
2800 ("---" ignore ignore)
2801 ("Add Comment" custom-comment-show custom-comment-invisible-p)
2802 ("---" ignore ignore)
2803 ("Show Current Value" custom-variable-edit
2804 (lambda (widget)
2805 (eq (widget-get widget :custom-form) 'lisp)))
2806 ("Show Saved Lisp Expression" custom-variable-edit-lisp
2807 (lambda (widget)
2808 (eq (widget-get widget :custom-form) 'edit))))
2809 "Alist of actions for the `custom-variable' widget.
2810 Each entry has the form (NAME ACTION FILTER) where NAME is the name of
2811 the menu entry, ACTION is the function to call on the widget when the
2812 menu is selected, and FILTER is a predicate which takes a `custom-variable'
2813 widget as an argument, and returns non-nil if ACTION is valid on that
2814 widget. If FILTER is nil, ACTION is always valid.")
2816 (defun custom-variable-action (widget &optional event)
2817 "Show the menu for `custom-variable' WIDGET.
2818 Optional EVENT is the location for the menu."
2819 (if (eq (widget-get widget :custom-state) 'hidden)
2820 (custom-toggle-hide widget)
2821 (unless (eq (widget-get widget :custom-state) 'modified)
2822 (custom-variable-state-set widget))
2823 (custom-redraw-magic widget)
2824 (let* ((completion-ignore-case t)
2825 (answer (widget-choose (concat "Operation on "
2826 (custom-unlispify-tag-name
2827 (widget-get widget :value)))
2828 (custom-menu-filter custom-variable-menu
2829 widget)
2830 event)))
2831 (if answer
2832 (funcall answer widget)))))
2834 (defun custom-variable-edit (widget)
2835 "Edit value of WIDGET."
2836 (widget-put widget :custom-state 'unknown)
2837 (widget-put widget :custom-form 'edit)
2838 (custom-redraw widget))
2840 (defun custom-variable-edit-lisp (widget)
2841 "Edit the Lisp representation of the value of WIDGET."
2842 (widget-put widget :custom-state 'unknown)
2843 (widget-put widget :custom-form 'lisp)
2844 (custom-redraw widget))
2846 (defun custom-variable-set (widget)
2847 "Set the current value for the variable being edited by WIDGET."
2848 (let* ((form (widget-get widget :custom-form))
2849 (state (widget-get widget :custom-state))
2850 (child (car (widget-get widget :children)))
2851 (symbol (widget-value widget))
2852 (set (or (get symbol 'custom-set) 'set-default))
2853 (comment-widget (widget-get widget :comment-widget))
2854 (comment (widget-value comment-widget))
2855 val)
2856 (cond ((eq state 'hidden)
2857 (user-error "Cannot set hidden variable"))
2858 ((setq val (widget-apply child :validate))
2859 (goto-char (widget-get val :from))
2860 (error "%s" (widget-get val :error)))
2861 ((memq form '(lisp mismatch))
2862 (when (equal comment "")
2863 (setq comment nil)
2864 ;; Make the comment invisible by hand if it's empty
2865 (custom-comment-hide comment-widget))
2866 (custom-variable-backup-value widget)
2867 (custom-push-theme 'theme-value symbol 'user
2868 'set (custom-quote (widget-value child)))
2869 (funcall set symbol (eval (setq val (widget-value child))))
2870 (put symbol 'customized-value (list val))
2871 (put symbol 'variable-comment comment)
2872 (put symbol 'customized-variable-comment comment))
2874 (when (equal comment "")
2875 (setq comment nil)
2876 ;; Make the comment invisible by hand if it's empty
2877 (custom-comment-hide comment-widget))
2878 (custom-variable-backup-value widget)
2879 (custom-push-theme 'theme-value symbol 'user
2880 'set (custom-quote (widget-value child)))
2881 (funcall set symbol (setq val (widget-value child)))
2882 (put symbol 'customized-value (list (custom-quote val)))
2883 (put symbol 'variable-comment comment)
2884 (put symbol 'customized-variable-comment comment)))
2885 (custom-variable-state-set widget)
2886 (custom-redraw-magic widget)))
2888 (defun custom-variable-mark-to-save (widget)
2889 "Set value and mark for saving the variable edited by WIDGET."
2890 (let* ((form (widget-get widget :custom-form))
2891 (state (widget-get widget :custom-state))
2892 (child (car (widget-get widget :children)))
2893 (symbol (widget-value widget))
2894 (set (or (get symbol 'custom-set) 'set-default))
2895 (comment-widget (widget-get widget :comment-widget))
2896 (comment (widget-value comment-widget))
2897 val)
2898 (cond ((eq state 'hidden)
2899 (user-error "Cannot set hidden variable"))
2900 ((setq val (widget-apply child :validate))
2901 (goto-char (widget-get val :from))
2902 (error "Saving %s: %s" symbol (widget-get val :error)))
2903 ((memq form '(lisp mismatch))
2904 (when (equal comment "")
2905 (setq comment nil)
2906 ;; Make the comment invisible by hand if it's empty
2907 (custom-comment-hide comment-widget))
2908 (put symbol 'saved-value (list (widget-value child)))
2909 (custom-push-theme 'theme-value symbol 'user
2910 'set (custom-quote (widget-value child)))
2911 (funcall set symbol (eval (widget-value child)))
2912 (put symbol 'variable-comment comment)
2913 (put symbol 'saved-variable-comment comment))
2915 (when (equal comment "")
2916 (setq comment nil)
2917 ;; Make the comment invisible by hand if it's empty
2918 (custom-comment-hide comment-widget))
2919 (put symbol 'saved-value
2920 (list (custom-quote (widget-value child))))
2921 (custom-push-theme 'theme-value symbol 'user
2922 'set (custom-quote (widget-value child)))
2923 (funcall set symbol (widget-value child))
2924 (put symbol 'variable-comment comment)
2925 (put symbol 'saved-variable-comment comment)))
2926 (put symbol 'customized-value nil)
2927 (put symbol 'customized-variable-comment nil)))
2929 (defsubst custom-variable-state-set-and-redraw (widget)
2930 "Set state of variable widget WIDGET and redraw with current settings."
2931 (custom-variable-state-set widget)
2932 (custom-redraw-magic widget))
2934 (defun custom-variable-save (widget)
2935 "Save value of variable edited by widget WIDGET."
2936 (custom-variable-mark-to-save widget)
2937 (custom-save-all)
2938 (custom-variable-state-set-and-redraw widget))
2940 (defun custom-variable-reset-saved (widget)
2941 "Restore the value of the variable being edited by WIDGET.
2942 If there is a saved value, restore it; otherwise reset to the
2943 uncustomized (themed or standard) value.
2945 Update the widget to show that value. The value that was current
2946 before this operation becomes the backup value."
2947 (let* ((symbol (widget-value widget))
2948 (saved-value (get symbol 'saved-value))
2949 (comment (get symbol 'saved-variable-comment)))
2950 (custom-variable-backup-value widget)
2951 (if (not (or saved-value comment))
2952 ;; If there is no saved value, remove the setting.
2953 (custom-push-theme 'theme-value symbol 'user 'reset)
2954 ;; Otherwise, apply the saved value.
2955 (put symbol 'variable-comment comment)
2956 (custom-push-theme 'theme-value symbol 'user 'set (car-safe saved-value))
2957 (ignore-errors
2958 (funcall (or (get symbol 'custom-set) 'set-default)
2959 symbol (eval (car saved-value)))))
2960 (put symbol 'customized-value nil)
2961 (put symbol 'customized-variable-comment nil)
2962 (widget-put widget :custom-state 'unknown)
2963 ;; This call will possibly make the comment invisible
2964 (custom-redraw widget)))
2966 (defun custom-variable-mark-to-reset-standard (widget)
2967 "Mark to restore standard setting for the variable edited by widget WIDGET.
2968 If `custom-reset-standard-variables-list' is nil, save, reset and
2969 redraw the widget immediately."
2970 (let* ((symbol (widget-value widget)))
2971 (if (get symbol 'standard-value)
2972 (custom-variable-backup-value widget)
2973 (user-error "No standard setting known for %S" symbol))
2974 (put symbol 'variable-comment nil)
2975 (put symbol 'customized-value nil)
2976 (put symbol 'customized-variable-comment nil)
2977 (custom-push-theme 'theme-value symbol 'user 'reset)
2978 (custom-theme-recalc-variable symbol)
2979 (if (and custom-reset-standard-variables-list
2980 (or (get symbol 'saved-value) (get symbol 'saved-variable-comment)))
2981 (progn
2982 (put symbol 'saved-value nil)
2983 (put symbol 'saved-variable-comment nil)
2984 ;; Append this to `custom-reset-standard-variables-list' to
2985 ;; have `custom-reset-standard-save-and-update' save setting
2986 ;; to the file, update the widget's state, and redraw it.
2987 (setq custom-reset-standard-variables-list
2988 (cons widget custom-reset-standard-variables-list)))
2989 (when (or (get symbol 'saved-value) (get symbol 'saved-variable-comment))
2990 (put symbol 'saved-value nil)
2991 (put symbol 'saved-variable-comment nil)
2992 (custom-save-all))
2993 (widget-put widget :custom-state 'unknown)
2994 ;; This call will possibly make the comment invisible
2995 (custom-redraw widget))))
2997 (defun custom-variable-reset-standard (widget)
2998 "Restore standard setting for the variable edited by WIDGET.
2999 This operation eliminates any saved setting for the variable,
3000 restoring it to the state of a variable that has never been customized.
3001 The value that was current before this operation
3002 becomes the backup value, so you can get it again."
3003 (let (custom-reset-standard-variables-list)
3004 (custom-variable-mark-to-reset-standard widget)))
3006 (defun custom-variable-backup-value (widget)
3007 "Back up the current value for WIDGET's variable.
3008 The backup value is kept in the car of the `backup-value' property."
3009 (let* ((symbol (widget-value widget))
3010 (get (or (get symbol 'custom-get) 'default-value))
3011 (type (custom-variable-type symbol))
3012 (conv (widget-convert type))
3013 (value (if (default-boundp symbol)
3014 (funcall get symbol)
3015 (widget-get conv :value))))
3016 (put symbol 'backup-value (list value))))
3018 (defun custom-variable-reset-backup (widget)
3019 "Restore the backup value for the variable being edited by WIDGET.
3020 The value that was current before this operation
3021 becomes the backup value, so you can use this operation repeatedly
3022 to switch between two values."
3023 (let* ((symbol (widget-value widget))
3024 (set (or (get symbol 'custom-set) 'set-default))
3025 (value (get symbol 'backup-value))
3026 (comment-widget (widget-get widget :comment-widget))
3027 (comment (widget-value comment-widget)))
3028 (if value
3029 (progn
3030 (custom-variable-backup-value widget)
3031 (custom-push-theme 'theme-value symbol 'user 'set value)
3032 (condition-case nil
3033 (funcall set symbol (car value))
3034 (error nil)))
3035 (user-error "No backup value for %s" symbol))
3036 (put symbol 'customized-value (list (custom-quote (car value))))
3037 (put symbol 'variable-comment comment)
3038 (put symbol 'customized-variable-comment comment)
3039 (custom-variable-state-set widget)
3040 ;; This call will possibly make the comment invisible
3041 (custom-redraw widget)))
3043 ;;; The `custom-visibility' Widget
3045 (define-widget 'custom-visibility 'visibility
3046 "Show or hide a documentation string."
3047 :button-face 'custom-visibility
3048 :pressed-face 'custom-visibility
3049 :mouse-face 'highlight
3050 :pressed-face 'highlight
3051 :on-glyph nil
3052 :off-glyph nil)
3054 (defface custom-visibility
3055 '((t :height 0.8 :inherit link))
3056 "Face for the `custom-visibility' widget."
3057 :version "23.1"
3058 :group 'custom-faces)
3060 ;;; The `custom-face-edit' Widget.
3062 (define-widget 'custom-face-edit 'checklist
3063 "Widget for editing face attributes.
3064 The following properties have special meanings for this widget:
3066 :value is a plist of face attributes.
3068 :default-face-attributes, if non-nil, is a plist of defaults for
3069 face attributes (as specified by a `default' defface entry)."
3070 :format "%v"
3071 :extra-offset 3
3072 :button-args '(:help-echo "Control whether this attribute has any effect.")
3073 :value-to-internal 'custom-face-edit-fix-value
3074 :match (lambda (widget value)
3075 (widget-checklist-match widget
3076 (custom-face-edit-fix-value widget value)))
3077 :value-create 'custom-face-edit-value-create
3078 :convert-widget 'custom-face-edit-convert-widget
3079 :args (mapcar (lambda (att)
3080 (list 'group :inline t
3081 :sibling-args (widget-get (nth 1 att) :sibling-args)
3082 (list 'const :format "" :value (nth 0 att))
3083 (nth 1 att)))
3084 custom-face-attributes))
3086 (defun custom-face-edit-value-create (widget)
3087 (let* ((alist (widget-checklist-match-find
3088 widget (widget-get widget :value)))
3089 (args (widget-get widget :args))
3090 (show-all (widget-get widget :show-all-attributes))
3091 (buttons (widget-get widget :buttons))
3092 (defaults (widget-checklist-match-find
3093 widget
3094 (widget-get widget :default-face-attributes)))
3095 entry)
3096 (unless (looking-back "^ *")
3097 (insert ?\n))
3098 (insert-char ?\s (widget-get widget :extra-offset))
3099 (if (or alist defaults show-all)
3100 (dolist (prop args)
3101 (setq entry (or (assq prop alist)
3102 (assq prop defaults)))
3103 (if (or entry show-all)
3104 (widget-checklist-add-item widget prop entry)))
3105 (insert (propertize "-- Empty face --" 'face 'shadow) ?\n))
3106 (let ((indent (widget-get widget :indent)))
3107 (if indent (insert-char ?\s (widget-get widget :indent))))
3108 (push (widget-create-child-and-convert
3109 widget 'visibility
3110 :help-echo "Show or hide all face attributes."
3111 :button-face 'custom-visibility
3112 :pressed-face 'custom-visibility
3113 :mouse-face 'highlight
3114 :on "Hide Unused Attributes" :off "Show All Attributes"
3115 :on-glyph nil :off-glyph nil
3116 :always-active t
3117 :action 'custom-face-edit-value-visibility-action
3118 show-all)
3119 buttons)
3120 (insert ?\n)
3121 (widget-put widget :buttons buttons)
3122 (widget-put widget :children (nreverse (widget-get widget :children)))))
3124 (defun custom-face-edit-value-visibility-action (widget &rest _ignore)
3125 ;; Toggle hiding of face attributes.
3126 (let ((parent (widget-get widget :parent)))
3127 (widget-put parent :show-all-attributes
3128 (not (widget-get parent :show-all-attributes)))
3129 (custom-redraw parent)))
3131 (defun custom-face-edit-fix-value (_widget value)
3132 "Ignoring WIDGET, convert :bold and :italic in VALUE to new form.
3133 Also change :reverse-video to :inverse-video."
3134 (custom-fix-face-spec value))
3136 (defun custom-face-edit-convert-widget (widget)
3137 "Convert :args as widget types in WIDGET."
3138 (widget-put
3139 widget
3140 :args (mapcar (lambda (arg)
3141 (widget-convert arg
3142 :deactivate 'custom-face-edit-deactivate
3143 :activate 'custom-face-edit-activate
3144 :delete 'custom-face-edit-delete))
3145 (widget-get widget :args)))
3146 widget)
3148 (defconst custom-face-edit (widget-convert 'custom-face-edit)
3149 "Converted version of the `custom-face-edit' widget.")
3151 (defun custom-face-edit-deactivate (widget)
3152 "Make face widget WIDGET inactive for user modifications."
3153 (unless (widget-get widget :inactive)
3154 (let ((tag (custom-face-edit-attribute-tag widget))
3155 (from (copy-marker (widget-get widget :from)))
3156 (value (widget-value widget))
3157 (inhibit-read-only t)
3158 (inhibit-modification-hooks t))
3159 (save-excursion
3160 (goto-char from)
3161 (widget-default-delete widget)
3162 (insert tag ": " (propertize "--" 'face 'shadow) "\n")
3163 (widget-put widget :inactive
3164 (cons value (cons from (- (point) from))))))))
3166 (defun custom-face-edit-activate (widget)
3167 "Make face widget WIDGET active for user modifications."
3168 (let ((inactive (widget-get widget :inactive))
3169 (inhibit-read-only t)
3170 (inhibit-modification-hooks t))
3171 (when (consp inactive)
3172 (save-excursion
3173 (goto-char (car (cdr inactive)))
3174 (delete-region (point) (+ (point) (cdr (cdr inactive))))
3175 (widget-put widget :inactive nil)
3176 (widget-apply widget :create)
3177 (widget-value-set widget (car inactive))
3178 (widget-setup)))))
3180 (defun custom-face-edit-delete (widget)
3181 "Remove WIDGET from the buffer."
3182 (let ((inactive (widget-get widget :inactive))
3183 (inhibit-read-only t)
3184 (inhibit-modification-hooks t))
3185 (if (not inactive)
3186 ;; Widget is alive, we don't have to do anything special
3187 (widget-default-delete widget)
3188 ;; WIDGET is already deleted because we did so to deactivate it;
3189 ;; now just get rid of the label we put in its place.
3190 (delete-region (car (cdr inactive))
3191 (+ (car (cdr inactive)) (cdr (cdr inactive))))
3192 (widget-put widget :inactive nil))))
3195 (defun custom-face-edit-attribute-tag (widget)
3196 "Return the first :tag property in WIDGET or one of its children."
3197 (let ((tag (widget-get widget :tag)))
3198 (or (and (not (equal tag "")) tag)
3199 (let ((children (widget-get widget :children)))
3200 (while (and (null tag) children)
3201 (setq tag (custom-face-edit-attribute-tag (pop children))))
3202 tag))))
3204 ;;; The `custom-display' Widget.
3206 (define-widget 'custom-display 'menu-choice
3207 "Select a display type."
3208 :tag "Display"
3209 :value t
3210 :help-echo "Specify frames where the face attributes should be used."
3211 :args '((const :tag "all" t)
3212 (const :tag "defaults" default)
3213 (checklist
3214 :tag "specific display"
3215 :offset 0
3216 :extra-offset 9
3217 :args ((group :sibling-args (:help-echo "\
3218 Only match the specified window systems.")
3219 (const :format "Type: "
3220 type)
3221 (checklist :inline t
3222 :offset 0
3223 (const :format "X "
3224 :sibling-args (:help-echo "\
3225 The X11 Window System.")
3227 (const :format "PM "
3228 :sibling-args (:help-echo "\
3229 OS/2 Presentation Manager.")
3231 (const :format "W32 "
3232 :sibling-args (:help-echo "\
3233 MS Windows.")
3234 w32)
3235 (const :format "NS "
3236 :sibling-args (:help-echo "\
3237 GNUstep or Macintosh OS Cocoa interface.")
3239 (const :format "DOS "
3240 :sibling-args (:help-echo "\
3241 Plain MS-DOS.")
3243 (const :format "TTY%n"
3244 :sibling-args (:help-echo "\
3245 Plain text terminals.")
3246 tty)))
3247 (group :sibling-args (:help-echo "\
3248 Only match the frames with the specified color support.")
3249 (const :format "Class: "
3250 class)
3251 (checklist :inline t
3252 :offset 0
3253 (const :format "Color "
3254 :sibling-args (:help-echo "\
3255 Match color frames.")
3256 color)
3257 (const :format "Grayscale "
3258 :sibling-args (:help-echo "\
3259 Match grayscale frames.")
3260 grayscale)
3261 (const :format "Monochrome%n"
3262 :sibling-args (:help-echo "\
3263 Match frames with no color support.")
3264 mono)))
3265 (group :sibling-args (:help-echo "\
3266 The minimum number of colors the frame should support.")
3267 (const :format "" min-colors)
3268 (integer :tag "Minimum number of colors" ))
3269 (group :sibling-args (:help-echo "\
3270 Only match frames with the specified intensity.")
3271 (const :format "\
3272 Background brightness: "
3273 background)
3274 (checklist :inline t
3275 :offset 0
3276 (const :format "Light "
3277 :sibling-args (:help-echo "\
3278 Match frames with light backgrounds.")
3279 light)
3280 (const :format "Dark\n"
3281 :sibling-args (:help-echo "\
3282 Match frames with dark backgrounds.")
3283 dark)))
3284 (group :sibling-args (:help-echo "\
3285 Only match frames that support the specified face attributes.")
3286 (const :format "Supports attributes:" supports)
3287 (custom-face-edit :inline t :format "%n%v"))))))
3289 ;;; The `custom-face' Widget.
3291 (defface custom-face-tag
3292 '((t :inherit custom-variable-tag))
3293 "Face used for face tags."
3294 :group 'custom-faces)
3296 (defcustom custom-face-default-form 'selected
3297 "Default form of displaying face definition."
3298 :type '(choice (const all)
3299 (const selected)
3300 (const lisp))
3301 :group 'custom-buffer
3302 :version "20.3")
3304 (define-widget 'custom-face 'custom
3305 "Widget for customizing a face.
3306 The following properties have special meanings for this widget:
3308 :value is the face name (a symbol).
3310 :custom-form should be a symbol describing how to display and
3311 edit the face attributes---either `selected' (attributes for
3312 selected display only), `all' (all attributes), `lisp' (as a
3313 Lisp sexp), or `mismatch' (should not happen); if nil, use
3314 the return value of `custom-face-default-form'.
3316 :custom-style describes the widget interface style; nil is the
3317 default style, while `simple' means a simpler interface that
3318 inhibits the magic custom-state widget.
3320 :sample-indent, if non-nil, is the number of columns to which to
3321 indent the face sample (an integer).
3323 :shown-value, if non-nil, is the face spec to display as the value
3324 of the widget, instead of the current face spec."
3325 :sample-face 'custom-face-tag
3326 :help-echo "Set or reset this face."
3327 :documentation-property #'face-doc-string
3328 :value-create 'custom-face-value-create
3329 :action 'custom-face-action
3330 :custom-category 'face
3331 :custom-form nil
3332 :custom-set 'custom-face-set
3333 :custom-mark-to-save 'custom-face-mark-to-save
3334 :custom-reset-current 'custom-redraw
3335 :custom-reset-saved 'custom-face-reset-saved
3336 :custom-reset-standard 'custom-face-reset-standard
3337 :custom-mark-to-reset-standard 'custom-face-mark-to-reset-standard
3338 :custom-standard-value 'custom-face-standard-value
3339 :custom-state-set-and-redraw 'custom-face-state-set-and-redraw
3340 :custom-menu 'custom-face-menu-create)
3342 (define-widget 'custom-face-all 'editable-list
3343 "An editable list of display specifications and attributes."
3344 :entry-format "%i %d %v"
3345 :insert-button-args '(:help-echo "Insert new display specification here.")
3346 :append-button-args '(:help-echo "Append new display specification here.")
3347 :delete-button-args '(:help-echo "Delete this display specification.")
3348 :args '((group :format "%v" custom-display custom-face-edit)))
3350 (defconst custom-face-all (widget-convert 'custom-face-all)
3351 "Converted version of the `custom-face-all' widget.")
3353 (defun custom-filter-face-spec (spec filter-index &optional default-filter)
3354 "Return a canonicalized version of SPEC.
3355 FILTER-INDEX is the index in the entry for each attribute in
3356 `custom-face-attributes' at which the appropriate filter function can be
3357 found, and DEFAULT-FILTER is the filter to apply for attributes that
3358 don't specify one."
3359 (mapcar (lambda (entry)
3360 ;; Filter a single face-spec entry
3361 (let ((tests (car entry))
3362 (unfiltered-attrs
3363 ;; Handle both old- and new-style attribute syntax
3364 (if (listp (car (cdr entry)))
3365 (car (cdr entry))
3366 (cdr entry)))
3367 (filtered-attrs nil))
3368 ;; Filter each face attribute
3369 (while unfiltered-attrs
3370 (let* ((attr (pop unfiltered-attrs))
3371 (pre-filtered-value (pop unfiltered-attrs))
3372 (filter
3373 (or (nth filter-index (assq attr custom-face-attributes))
3374 default-filter))
3375 (filtered-value
3376 (if filter
3377 (funcall filter pre-filtered-value)
3378 pre-filtered-value)))
3379 (push filtered-value filtered-attrs)
3380 (push attr filtered-attrs)))
3382 (list tests filtered-attrs)))
3383 spec))
3385 (defun custom-pre-filter-face-spec (spec)
3386 "Return SPEC changed as necessary for editing by the face customization widget.
3387 SPEC must be a full face spec."
3388 (custom-filter-face-spec spec 2))
3390 (defun custom-post-filter-face-spec (spec)
3391 "Return the customized SPEC in a form suitable for setting the face."
3392 (custom-filter-face-spec spec 3))
3394 (defun custom-face-widget-to-spec (widget)
3395 "Return a face spec corresponding to WIDGET.
3396 WIDGET should be a `custom-face' widget."
3397 (unless (eq (widget-type widget) 'custom-face)
3398 (error "Invalid widget"))
3399 (let ((child (car (widget-get widget :children))))
3400 (custom-post-filter-face-spec
3401 (if (eq (widget-type child) 'custom-face-edit)
3402 `((t ,(widget-value child)))
3403 (widget-value child)))))
3405 (defun custom-face-get-current-spec (face)
3406 (let ((spec (or (get face 'customized-face)
3407 (get face 'saved-face)
3408 (get face 'face-defface-spec)
3409 ;; Attempt to construct it.
3410 `((t ,(custom-face-attributes-get
3411 face (selected-frame)))))))
3412 ;; If the user has changed this face in some other way,
3413 ;; edit it as the user has specified it.
3414 (if (not (face-spec-match-p face spec (selected-frame)))
3415 (setq spec `((t ,(face-attr-construct face (selected-frame))))))
3416 (custom-pre-filter-face-spec spec)))
3418 (defun custom-toggle-hide-face (visibility-widget &rest _ignore)
3419 "Toggle the visibility of a `custom-face' parent widget.
3420 By default, this signals an error if the parent has unsaved
3421 changes. If the parent has a `simple' :custom-style property,
3422 the present value is saved to its :shown-value property instead."
3423 (let ((widget (widget-get visibility-widget :parent)))
3424 (unless (eq (widget-type widget) 'custom-face)
3425 (error "Invalid widget type"))
3426 (custom-load-widget widget)
3427 (let ((state (widget-get widget :custom-state)))
3428 (if (eq state 'hidden)
3429 (widget-put widget :custom-state 'unknown)
3430 ;; In normal interface, widget can't be hidden if modified.
3431 (when (memq state '(invalid modified set))
3432 (if (eq (widget-get widget :custom-style) 'simple)
3433 (widget-put widget :shown-value
3434 (custom-face-widget-to-spec widget))
3435 (error "There are unsaved changes")))
3436 (widget-put widget :documentation-shown nil)
3437 (widget-put widget :custom-state 'hidden))
3438 (custom-redraw widget)
3439 (widget-setup))))
3441 (defun custom-face-value-create (widget)
3442 "Create a list of the display specifications for WIDGET."
3443 (let* ((buttons (widget-get widget :buttons))
3444 (symbol (widget-get widget :value))
3445 (tag (or (widget-get widget :tag)
3446 (prin1-to-string symbol)))
3447 (hiddenp (eq (widget-get widget :custom-state) 'hidden))
3448 (style (widget-get widget :custom-style))
3449 children)
3451 (if (eq custom-buffer-style 'tree)
3453 ;; Draw a tree-style `custom-face' widget
3454 (progn
3455 (insert (widget-get widget :custom-prefix)
3456 (if (widget-get widget :custom-last) " `--- " " |--- "))
3457 (push (widget-create-child-and-convert
3458 widget 'custom-browse-face-tag)
3459 buttons)
3460 (insert " " tag "\n")
3461 (widget-put widget :buttons buttons))
3463 ;; Draw an ordinary `custom-face' widget
3464 (let ((opoint (point)))
3465 ;; Visibility indicator.
3466 (push (widget-create-child-and-convert
3467 widget 'custom-visibility
3468 :help-echo "Hide or show this face."
3469 :on "Hide" :off "Show"
3470 :on-glyph "down" :off-glyph "right"
3471 :action 'custom-toggle-hide-face
3472 (not hiddenp))
3473 buttons)
3474 ;; Face name (tag).
3475 (insert " " tag)
3476 (widget-specify-sample widget opoint (point)))
3477 (insert
3478 (cond ((eq custom-buffer-style 'face) " ")
3479 ((string-match-p "face\\'" tag) ":")
3480 (t " face: ")))
3482 ;; Face sample.
3483 (let ((sample-indent (widget-get widget :sample-indent))
3484 (indent-tabs-mode nil))
3485 (and sample-indent
3486 (<= (current-column) sample-indent)
3487 (indent-to-column sample-indent)))
3488 (push (widget-create-child-and-convert
3489 widget 'item
3490 :format "[%{%t%}]"
3491 :sample-face (let ((spec (widget-get widget :shown-value)))
3492 (if spec (face-spec-choose spec) symbol))
3493 :tag "sample")
3494 buttons)
3495 (insert "\n")
3497 ;; Magic.
3498 (unless (eq (widget-get widget :custom-style) 'simple)
3499 (let ((magic (widget-create-child-and-convert
3500 widget 'custom-magic nil)))
3501 (widget-put widget :custom-magic magic)
3502 (push magic buttons)))
3504 ;; Update buttons.
3505 (widget-put widget :buttons buttons)
3507 ;; Insert documentation.
3508 (unless (and hiddenp (eq style 'simple))
3509 (widget-put widget :documentation-indent 3)
3510 (widget-add-documentation-string-button
3511 widget :visibility-widget 'custom-visibility)
3512 ;; The comment field
3513 (unless hiddenp
3514 (let* ((comment (get symbol 'face-comment))
3515 (comment-widget
3516 (widget-create-child-and-convert
3517 widget 'custom-comment
3518 :parent widget
3519 :value (or comment ""))))
3520 (widget-put widget :comment-widget comment-widget)
3521 (push comment-widget children))))
3523 ;; Editor.
3524 (unless (eq (preceding-char) ?\n)
3525 (insert "\n"))
3526 (unless hiddenp
3527 (custom-load-widget widget)
3528 (unless (widget-get widget :custom-form)
3529 (widget-put widget :custom-form custom-face-default-form))
3531 (let* ((spec (or (widget-get widget :shown-value)
3532 (custom-face-get-current-spec symbol)))
3533 (form (widget-get widget :custom-form))
3534 (indent (widget-get widget :indent))
3535 face-alist face-entry spec-default spec-match editor)
3537 ;; Find a display in SPEC matching the selected display.
3538 ;; This will use the usual face customization interface.
3539 (setq face-alist spec)
3540 (when (eq (car-safe (car-safe face-alist)) 'default)
3541 (setq spec-default (pop face-alist)))
3543 (while (and face-alist (listp face-alist) (null spec-match))
3544 (setq face-entry (car face-alist))
3545 (and (listp face-entry)
3546 (face-spec-set-match-display (car face-entry)
3547 (selected-frame))
3548 (widget-apply custom-face-edit :match (cadr face-entry))
3549 (setq spec-match face-entry))
3550 (setq face-alist (cdr face-alist)))
3552 ;; Insert the appropriate editing widget.
3553 (setq editor
3554 (cond
3555 ((and (eq form 'selected)
3556 (or spec-match spec-default))
3557 (when indent (insert-char ?\s indent))
3558 (widget-create-child-and-convert
3559 widget 'custom-face-edit
3560 :value (cadr spec-match)
3561 :default-face-attributes (cadr spec-default)))
3562 ((and (not (eq form 'lisp))
3563 (widget-apply custom-face-all :match spec))
3564 (widget-create-child-and-convert
3565 widget 'custom-face-all :value spec))
3567 (when indent
3568 (insert-char ?\s indent))
3569 (widget-create-child-and-convert
3570 widget 'sexp :value spec))))
3571 (custom-face-state-set widget)
3572 (push editor children)
3573 (widget-put widget :children children))))))
3575 (defvar custom-face-menu
3576 `(("Set for Current Session" custom-face-set)
3577 ,@(when (or custom-file init-file-user)
3578 '(("Save for Future Sessions" custom-face-save)))
3579 ("Undo Edits" custom-redraw
3580 (lambda (widget)
3581 (memq (widget-get widget :custom-state) '(modified changed))))
3582 ("Revert This Session's Customization" custom-face-reset-saved
3583 (lambda (widget)
3584 (memq (widget-get widget :custom-state) '(modified set changed))))
3585 ,@(when (or custom-file init-file-user)
3586 '(("Erase Customization" custom-face-reset-standard
3587 (lambda (widget)
3588 (get (widget-value widget) 'face-defface-spec)))))
3589 ("---" ignore ignore)
3590 ("Add Comment" custom-comment-show custom-comment-invisible-p)
3591 ("---" ignore ignore)
3592 ("For Current Display" custom-face-edit-selected
3593 (lambda (widget)
3594 (not (eq (widget-get widget :custom-form) 'selected))))
3595 ("For All Kinds of Displays" custom-face-edit-all
3596 (lambda (widget)
3597 (not (eq (widget-get widget :custom-form) 'all))))
3598 ("Show Lisp Expression" custom-face-edit-lisp
3599 (lambda (widget)
3600 (not (eq (widget-get widget :custom-form) 'lisp)))))
3601 "Alist of actions for the `custom-face' widget.
3602 Each entry has the form (NAME ACTION FILTER) where NAME is the name of
3603 the menu entry, ACTION is the function to call on the widget when the
3604 menu is selected, and FILTER is a predicate which takes a `custom-face'
3605 widget as an argument, and returns non-nil if ACTION is valid on that
3606 widget. If FILTER is nil, ACTION is always valid.")
3608 (defun custom-face-edit-selected (widget)
3609 "Edit selected attributes of the value of WIDGET."
3610 (widget-put widget :custom-state 'unknown)
3611 (widget-put widget :custom-form 'selected)
3612 (custom-redraw widget))
3614 (defun custom-face-edit-all (widget)
3615 "Edit all attributes of the value of WIDGET."
3616 (widget-put widget :custom-state 'unknown)
3617 (widget-put widget :custom-form 'all)
3618 (custom-redraw widget))
3620 (defun custom-face-edit-lisp (widget)
3621 "Edit the Lisp representation of the value of WIDGET."
3622 (widget-put widget :custom-state 'unknown)
3623 (widget-put widget :custom-form 'lisp)
3624 (custom-redraw widget))
3626 (defun custom-face-state (face)
3627 "Return the current state of the face FACE.
3628 This is one of `set', `saved', `changed', `themed', or `rogue'."
3629 (let* ((comment (get face 'face-comment))
3630 (state
3631 (cond
3632 ((or (get face 'customized-face)
3633 (get face 'customized-face-comment))
3634 (if (equal (get face 'customized-face-comment) comment)
3635 'set
3636 'changed))
3637 ((or (get face 'saved-face)
3638 (get face 'saved-face-comment))
3639 (cond ((not (equal (get face 'saved-face-comment) comment))
3640 'changed)
3641 ((eq 'user (caar (get face 'theme-face)))
3642 'saved)
3643 ((eq 'changed (caar (get face 'theme-face)))
3644 'changed)
3645 (t 'themed)))
3646 ((get face 'face-defface-spec)
3647 (cond (comment 'changed)
3648 ((get face 'theme-face) 'themed)
3649 (t 'standard)))
3650 (t 'rogue))))
3651 ;; If the user called set-face-attribute to change the default for
3652 ;; new frames, this face is "set outside of Customize".
3653 (if (and (not (eq state 'rogue))
3654 (get face 'face-modified))
3655 'changed
3656 state)))
3658 (defun custom-face-state-set (widget)
3659 "Set the state of WIDGET."
3660 (widget-put widget :custom-state
3661 (custom-face-state (widget-value widget))))
3663 (defun custom-face-action (widget &optional event)
3664 "Show the menu for `custom-face' WIDGET.
3665 Optional EVENT is the location for the menu."
3666 (if (eq (widget-get widget :custom-state) 'hidden)
3667 (custom-toggle-hide widget)
3668 (let* ((completion-ignore-case t)
3669 (symbol (widget-get widget :value))
3670 (answer (widget-choose (concat "Operation on "
3671 (custom-unlispify-tag-name symbol))
3672 (custom-menu-filter custom-face-menu
3673 widget)
3674 event)))
3675 (if answer
3676 (funcall answer widget)))))
3678 (defun custom-face-set (widget)
3679 "Make the face attributes in WIDGET take effect."
3680 (let* ((symbol (widget-value widget))
3681 (value (custom-face-widget-to-spec widget))
3682 (comment-widget (widget-get widget :comment-widget))
3683 (comment (widget-value comment-widget)))
3684 (when (equal comment "")
3685 (setq comment nil)
3686 ;; Make the comment invisible by hand if it's empty
3687 (custom-comment-hide comment-widget))
3688 (custom-push-theme 'theme-face symbol 'user 'set value)
3689 (face-spec-set symbol value 'customized-face)
3690 (put symbol 'face-comment comment)
3691 (put symbol 'customized-face-comment comment)
3692 (custom-face-state-set widget)
3693 (custom-redraw-magic widget)))
3695 (defun custom-face-mark-to-save (widget)
3696 "Mark for saving the face edited by WIDGET."
3697 (let* ((symbol (widget-value widget))
3698 (value (custom-face-widget-to-spec widget))
3699 (comment-widget (widget-get widget :comment-widget))
3700 (comment (widget-value comment-widget))
3701 (standard (eq (widget-get widget :custom-state) 'standard)))
3702 (when (equal comment "")
3703 (setq comment nil)
3704 ;; Make the comment invisible by hand if it's empty
3705 (custom-comment-hide comment-widget))
3706 (custom-push-theme 'theme-face symbol 'user 'set value)
3707 (face-spec-set symbol value (if standard 'reset 'saved-face))
3708 (put symbol 'face-comment comment)
3709 (put symbol 'customized-face-comment nil)
3710 (put symbol 'saved-face-comment comment)))
3712 (defsubst custom-face-state-set-and-redraw (widget)
3713 "Set state of face widget WIDGET and redraw with current settings."
3714 (custom-face-state-set widget)
3715 (custom-redraw-magic widget))
3717 (defun custom-face-save (widget)
3718 "Save the face edited by WIDGET."
3719 (custom-face-mark-to-save widget)
3720 (custom-save-all)
3721 (custom-face-state-set-and-redraw widget))
3723 ;; For backward compatibility.
3724 (define-obsolete-function-alias 'custom-face-save-command 'custom-face-save
3725 "22.1")
3727 (defun custom-face-reset-saved (widget)
3728 "Restore WIDGET to the face's default attributes.
3729 If there is a saved face, restore it; otherwise reset to the
3730 uncustomized (themed or standard) face."
3731 (let* ((face (widget-value widget))
3732 (child (car (widget-get widget :children)))
3733 (saved-face (get face 'saved-face))
3734 (comment (get face 'saved-face-comment))
3735 (comment-widget (widget-get widget :comment-widget)))
3736 (custom-push-theme 'theme-face face 'user
3737 (if saved-face 'set 'reset)
3738 saved-face)
3739 (face-spec-set face saved-face 'saved-face)
3740 (put face 'face-comment comment)
3741 (put face 'customized-face-comment nil)
3742 (widget-value-set child saved-face)
3743 ;; This call manages the comment visibility
3744 (widget-value-set comment-widget (or comment ""))
3745 (custom-face-state-set widget)
3746 (custom-redraw widget)))
3748 (defun custom-face-standard-value (widget)
3749 (get (widget-value widget) 'face-defface-spec))
3751 (defun custom-face-mark-to-reset-standard (widget)
3752 "Restore widget WIDGET to the face's standard attribute values.
3753 If `custom-reset-standard-faces-list' is nil, save, reset and
3754 redraw the widget immediately."
3755 (let* ((symbol (widget-value widget))
3756 (child (car (widget-get widget :children)))
3757 (value (get symbol 'face-defface-spec))
3758 (comment-widget (widget-get widget :comment-widget)))
3759 (unless value
3760 (user-error "No standard setting for this face"))
3761 (custom-push-theme 'theme-face symbol 'user 'reset)
3762 (face-spec-set symbol value 'reset)
3763 (put symbol 'face-comment nil)
3764 (put symbol 'customized-face-comment nil)
3765 (if (and custom-reset-standard-faces-list
3766 (or (get symbol 'saved-face) (get symbol 'saved-face-comment)))
3767 ;; Do this later.
3768 (progn
3769 (put symbol 'saved-face nil)
3770 (put symbol 'saved-face-comment nil)
3771 ;; Append this to `custom-reset-standard-faces-list' and have
3772 ;; `custom-reset-standard-save-and-update' save setting to the
3773 ;; file, update the widget's state, and redraw it.
3774 (setq custom-reset-standard-faces-list
3775 (cons widget custom-reset-standard-faces-list)))
3776 (when (or (get symbol 'saved-face) (get symbol 'saved-face-comment))
3777 (put symbol 'saved-face nil)
3778 (put symbol 'saved-face-comment nil)
3779 (custom-save-all))
3780 (widget-value-set child
3781 (custom-pre-filter-face-spec
3782 (list (list t (custom-face-attributes-get
3783 symbol nil)))))
3784 ;; This call manages the comment visibility
3785 (widget-value-set comment-widget "")
3786 (custom-face-state-set widget)
3787 (custom-redraw-magic widget))))
3789 (defun custom-face-reset-standard (widget)
3790 "Restore WIDGET to the face's standard attribute values.
3791 This operation eliminates any saved attributes for the face,
3792 restoring it to the state of a face that has never been customized."
3793 (let (custom-reset-standard-faces-list)
3794 (custom-face-mark-to-reset-standard widget)))
3796 ;;; The `face' Widget.
3798 (defvar widget-face-prompt-value-history nil
3799 "History of input to `widget-face-prompt-value'.")
3801 (define-widget 'face 'symbol
3802 "A Lisp face name (with sample)."
3803 :format "%{%t%}: (%{sample%}) %v"
3804 :tag "Face"
3805 :value 'default
3806 :sample-face-get 'widget-face-sample-face-get
3807 :notify 'widget-face-notify
3808 :match (lambda (_widget value) (facep value))
3809 :completions (apply-partially #'completion-table-with-predicate
3810 obarray #'facep 'strict)
3811 :prompt-match 'facep
3812 :prompt-history 'widget-face-prompt-value-history
3813 :validate (lambda (widget)
3814 (unless (facep (widget-value widget))
3815 (widget-put widget
3816 :error (format "Invalid face: %S"
3817 (widget-value widget)))
3818 widget)))
3820 (defun widget-face-sample-face-get (widget)
3821 (let ((value (widget-value widget)))
3822 (if (facep value)
3823 value
3824 'default)))
3826 (defun widget-face-notify (widget child &optional event)
3827 "Update the sample, and notify the parent."
3828 (overlay-put (widget-get widget :sample-overlay)
3829 'face (widget-apply widget :sample-face-get))
3830 (widget-default-notify widget child event))
3833 ;;; The `hook' Widget.
3835 (define-widget 'hook 'list
3836 "An Emacs Lisp hook."
3837 :value-to-internal (lambda (_widget value)
3838 (if (and value (symbolp value))
3839 (list value)
3840 value))
3841 :match (lambda (widget value)
3842 (or (symbolp value)
3843 (widget-group-match widget value)))
3844 ;; Avoid adding undefined functions to the hook, especially for
3845 ;; things like `find-file-hook' or even more basic ones, to avoid
3846 ;; chaos.
3847 :set (lambda (symbol value)
3848 (dolist (elt value)
3849 (if (fboundp elt)
3850 (add-hook symbol elt))))
3851 :convert-widget 'custom-hook-convert-widget
3852 :tag "Hook")
3854 (defun custom-hook-convert-widget (widget)
3855 ;; Handle `:options'.
3856 (let* ((options (widget-get widget :options))
3857 (other `(editable-list :inline t
3858 :entry-format "%i %d%v"
3859 (function :format " %v")))
3860 (args (if options
3861 (list `(checklist :inline t
3862 ,@(mapcar (lambda (entry)
3863 `(function-item ,entry))
3864 options))
3865 other)
3866 (list other))))
3867 (widget-put widget :args args)
3868 widget))
3870 ;;; The `custom-group-link' Widget.
3872 (define-widget 'custom-group-link 'link
3873 "Show parent in other window when activated."
3874 :button-face 'custom-link
3875 :mouse-face 'highlight
3876 :pressed-face 'highlight
3877 :help-echo "Create customization buffer for this group."
3878 :keymap custom-mode-link-map
3879 :follow-link 'mouse-face
3880 :action 'custom-group-link-action)
3882 (defun custom-group-link-action (widget &rest _ignore)
3883 (customize-group (widget-value widget)))
3885 ;;; The `custom-group' Widget.
3887 (defcustom custom-group-tag-faces nil
3888 "Face used for group tags.
3889 The first member is used for level 1 groups, the second for level 2,
3890 and so forth. The remaining group tags are shown with `custom-group-tag'."
3891 :type '(repeat face)
3892 :group 'custom-faces)
3894 (defface custom-group-tag-1
3895 '((default :weight bold :height 1.2 :inherit variable-pitch)
3896 (((class color) (background dark)) :foreground "pink")
3897 (((min-colors 88) (class color) (background light)) :foreground "red1")
3898 (((class color) (background light)) :foreground "red"))
3899 "Face for group tags."
3900 :group 'custom-faces)
3902 (defface custom-group-tag
3903 '((default :weight bold :height 1.2 :inherit variable-pitch)
3904 (((class color) (background dark)) :foreground "light blue")
3905 (((min-colors 88) (class color) (background light)) :foreground "blue1")
3906 (((class color) (background light)) :foreground "blue")
3907 (t :weight bold))
3908 "Face for low level group tags."
3909 :group 'custom-faces)
3911 (defface custom-group-subtitle
3912 '((t :weight bold))
3913 "Face for the \"Subgroups:\" subtitle in Custom buffers."
3914 :group 'custom-faces)
3916 (defvar custom-group-doc-align-col 20)
3918 (define-widget 'custom-group 'custom
3919 "Customize group."
3920 :format "%v"
3921 :sample-face-get 'custom-group-sample-face-get
3922 :documentation-property 'group-documentation
3923 :help-echo "Set or reset all members of this group."
3924 :value-create 'custom-group-value-create
3925 :action 'custom-group-action
3926 :custom-category 'group
3927 :custom-set 'custom-group-set
3928 :custom-mark-to-save 'custom-group-mark-to-save
3929 :custom-reset-current 'custom-group-reset-current
3930 :custom-reset-saved 'custom-group-reset-saved
3931 :custom-reset-standard 'custom-group-reset-standard
3932 :custom-mark-to-reset-standard 'custom-group-mark-to-reset-standard
3933 :custom-state-set-and-redraw 'custom-group-state-set-and-redraw
3934 :custom-menu 'custom-group-menu-create)
3936 (defun custom-group-sample-face-get (widget)
3937 ;; Use :sample-face.
3938 (or (nth (1- (widget-get widget :custom-level)) custom-group-tag-faces)
3939 'custom-group-tag))
3941 (define-widget 'custom-group-visibility 'visibility
3942 "An indicator and manipulator for hidden group contents."
3943 :create 'custom-group-visibility-create)
3945 (defun custom-group-visibility-create (widget)
3946 (let ((visible (widget-value widget)))
3947 (if visible
3948 (insert "--------")))
3949 (widget-default-create widget))
3951 (defun custom-group-members (symbol groups-only)
3952 "Return SYMBOL's custom group members.
3953 If GROUPS-ONLY is non-nil, return only those members that are groups."
3954 (if (not groups-only)
3955 (get symbol 'custom-group)
3956 (let (members)
3957 (dolist (entry (get symbol 'custom-group))
3958 (when (eq (nth 1 entry) 'custom-group)
3959 (push entry members)))
3960 (nreverse members))))
3962 (defun custom-group-value-create (widget)
3963 "Insert a customize group for WIDGET in the current buffer."
3964 (unless (eq (widget-get widget :custom-state) 'hidden)
3965 (custom-load-widget widget))
3966 (let* ((state (widget-get widget :custom-state))
3967 (level (widget-get widget :custom-level))
3968 ;; (indent (widget-get widget :indent))
3969 (prefix (widget-get widget :custom-prefix))
3970 (buttons (widget-get widget :buttons))
3971 (tag (widget-get widget :tag))
3972 (symbol (widget-value widget))
3973 (members (custom-group-members symbol
3974 (and (eq custom-buffer-style 'tree)
3975 custom-browse-only-groups)))
3976 (doc (widget-docstring widget)))
3977 (cond ((and (eq custom-buffer-style 'tree)
3978 (eq state 'hidden)
3979 (or members (custom-unloaded-widget-p widget)))
3980 (custom-browse-insert-prefix prefix)
3981 (push (widget-create-child-and-convert
3982 widget 'custom-browse-visibility
3983 :tag "+")
3984 buttons)
3985 (insert "-- ")
3986 (push (widget-create-child-and-convert
3987 widget 'custom-browse-group-tag)
3988 buttons)
3989 (insert " " tag "\n")
3990 (widget-put widget :buttons buttons))
3991 ((and (eq custom-buffer-style 'tree)
3992 (zerop (length members)))
3993 (custom-browse-insert-prefix prefix)
3994 (insert "[ ]-- ")
3995 (push (widget-create-child-and-convert
3996 widget 'custom-browse-group-tag)
3997 buttons)
3998 (insert " " tag "\n")
3999 (widget-put widget :buttons buttons))
4000 ((eq custom-buffer-style 'tree)
4001 (custom-browse-insert-prefix prefix)
4002 (if (zerop (length members))
4003 (progn
4004 (custom-browse-insert-prefix prefix)
4005 (insert "[ ]-- ")
4006 ;; (widget-glyph-insert nil "[ ]" "empty")
4007 ;; (widget-glyph-insert nil "-- " "horizontal")
4008 (push (widget-create-child-and-convert
4009 widget 'custom-browse-group-tag)
4010 buttons)
4011 (insert " " tag "\n")
4012 (widget-put widget :buttons buttons))
4013 (push (widget-create-child-and-convert
4014 widget 'custom-browse-visibility
4015 ;; :tag-glyph "minus"
4016 :tag "-")
4017 buttons)
4018 (insert "-\\ ")
4019 ;; (widget-glyph-insert nil "-\\ " "top")
4020 (push (widget-create-child-and-convert
4021 widget 'custom-browse-group-tag)
4022 buttons)
4023 (insert " " tag "\n")
4024 (widget-put widget :buttons buttons)
4025 (message "Creating group...")
4026 (let* ((members (custom-sort-items
4027 members
4028 ;; Never sort the top-level custom group.
4029 (unless (eq symbol 'emacs)
4030 custom-browse-sort-alphabetically)
4031 custom-browse-order-groups))
4032 (prefixes (widget-get widget :custom-prefixes))
4033 (custom-prefix-list (custom-prefix-add symbol prefixes))
4034 (extra-prefix (if (widget-get widget :custom-last)
4036 " | "))
4037 (prefix (concat prefix extra-prefix))
4038 children entry)
4039 (while members
4040 (setq entry (car members)
4041 members (cdr members))
4042 (push (widget-create-child-and-convert
4043 widget (nth 1 entry)
4044 :group widget
4045 :tag (custom-unlispify-tag-name (nth 0 entry))
4046 :custom-prefixes custom-prefix-list
4047 :custom-level (1+ level)
4048 :custom-last (null members)
4049 :value (nth 0 entry)
4050 :custom-prefix prefix)
4051 children))
4052 (widget-put widget :children (reverse children)))
4053 (message "Creating group...done")))
4054 ;; Nested style.
4055 ((eq state 'hidden)
4056 ;; Create level indicator.
4057 ;; Create tag.
4058 (if (eq custom-buffer-style 'links)
4059 (push (widget-create-child-and-convert
4060 widget 'custom-group-link
4061 :tag tag
4062 symbol)
4063 buttons)
4064 (insert-char ?\s (* custom-buffer-indent (1- level)))
4065 (insert "-- ")
4066 (push (widget-create-child-and-convert
4067 widget 'custom-group-visibility
4068 :help-echo "Show members of this group."
4069 :action 'custom-toggle-parent
4070 (not (eq state 'hidden)))
4071 buttons))
4072 (if (>= (current-column) custom-group-doc-align-col)
4073 (insert " "))
4074 ;; Create magic button.
4075 (let ((magic (widget-create-child-and-convert
4076 widget 'custom-magic nil)))
4077 (widget-put widget :custom-magic magic)
4078 (push magic buttons))
4079 ;; Update buttons.
4080 (widget-put widget :buttons buttons)
4081 ;; Insert documentation.
4082 (if (and (eq custom-buffer-style 'links) (> level 1))
4083 (widget-put widget :documentation-indent
4084 custom-group-doc-align-col))
4085 (widget-add-documentation-string-button
4086 widget :visibility-widget 'custom-visibility))
4088 ;; Nested style.
4089 (t ;Visible.
4090 ;; Draw a horizontal line (this works for both graphical
4091 ;; and text displays):
4092 (let ((p (point)))
4093 (insert "\n")
4094 (put-text-property p (1+ p) 'face '(:underline t))
4095 (overlay-put (make-overlay p (1+ p))
4096 'before-string
4097 (propertize "\n" 'face '(:underline t)
4098 'display '(space :align-to 999))))
4100 ;; Add parent groups references above the group.
4101 (when (eq level 1)
4102 (if (custom-add-parent-links widget "Parent groups:")
4103 (insert "\n")))
4104 (insert-char ?\s (* custom-buffer-indent (1- level)))
4105 ;; Create tag.
4106 (let ((start (point)))
4107 (insert tag " group: ")
4108 (widget-specify-sample widget start (point)))
4109 (cond
4110 ((not doc)
4111 (insert " Group definition missing. "))
4112 ((< (length doc) 50)
4113 (insert doc)))
4114 ;; Create visibility indicator.
4115 (unless (eq custom-buffer-style 'links)
4116 (insert "--------")
4117 (push (widget-create-child-and-convert
4118 widget 'visibility
4119 :help-echo "Hide members of this group."
4120 :action 'custom-toggle-parent
4121 (not (eq state 'hidden)))
4122 buttons)
4123 (insert " "))
4124 (insert "\n")
4125 ;; Create magic button.
4126 (let ((magic (widget-create-child-and-convert
4127 widget 'custom-magic
4128 :indent 0
4129 nil)))
4130 (widget-put widget :custom-magic magic)
4131 (push magic buttons))
4132 ;; Update buttons.
4133 (widget-put widget :buttons buttons)
4134 ;; Insert documentation.
4135 (when (and doc (>= (length doc) 50))
4136 (widget-add-documentation-string-button
4137 widget :visibility-widget 'custom-visibility))
4139 ;; Parent groups.
4140 (if nil ;;; This should test that the buffer
4141 ;;; was not made to display a group.
4142 (when (eq level 1)
4143 (insert-char ?\s custom-buffer-indent)
4144 (custom-add-parent-links widget)))
4145 (custom-add-see-also widget
4146 (make-string (* custom-buffer-indent level)
4147 ?\s))
4148 ;; Members.
4149 (message "Creating group...")
4150 (let* ((members (custom-sort-items
4151 members
4152 ;; Never sort the top-level custom group.
4153 (unless (eq symbol 'emacs)
4154 custom-buffer-sort-alphabetically)
4155 custom-buffer-order-groups))
4156 (prefixes (widget-get widget :custom-prefixes))
4157 (custom-prefix-list (custom-prefix-add symbol prefixes))
4158 (len (length members))
4159 (count 0)
4160 (reporter (make-progress-reporter
4161 "Creating group entries..." 0 len))
4162 (have-subtitle (and (not (eq symbol 'emacs))
4163 (eq custom-buffer-order-groups 'last)))
4164 prev-type
4165 children)
4167 (dolist (entry members)
4168 (unless (eq prev-type 'custom-group)
4169 (widget-insert "\n"))
4170 (progress-reporter-update reporter (setq count (1+ count)))
4171 (let ((sym (nth 0 entry))
4172 (type (nth 1 entry)))
4173 (when (and have-subtitle (eq type 'custom-group))
4174 (setq have-subtitle nil)
4175 (widget-insert
4176 (propertize "Subgroups:\n" 'face 'custom-group-subtitle)))
4177 (setq prev-type type)
4178 (push (widget-create-child-and-convert
4179 widget type
4180 :group widget
4181 :tag (custom-unlispify-tag-name sym)
4182 :custom-prefixes custom-prefix-list
4183 :custom-level (1+ level)
4184 :value sym)
4185 children)
4186 (unless (eq (preceding-char) ?\n)
4187 (widget-insert "\n"))))
4189 (setq children (nreverse children))
4190 (mapc 'custom-magic-reset children)
4191 (widget-put widget :children children)
4192 (custom-group-state-update widget)
4193 (progress-reporter-done reporter))
4194 ;; End line
4195 (let ((p (1+ (point))))
4196 (insert "\n\n")
4197 (put-text-property p (1+ p) 'face '(:underline t))
4198 (overlay-put (make-overlay p (1+ p))
4199 'before-string
4200 (propertize "\n" 'face '(:underline t)
4201 'display '(space :align-to 999))))))))
4203 (defvar custom-group-menu
4204 `(("Set for Current Session" custom-group-set
4205 (lambda (widget)
4206 (eq (widget-get widget :custom-state) 'modified)))
4207 ,@(when (or custom-file init-file-user)
4208 '(("Save for Future Sessions" custom-group-save
4209 (lambda (widget)
4210 (memq (widget-get widget :custom-state) '(modified set))))))
4211 ("Undo Edits" custom-group-reset-current
4212 (lambda (widget)
4213 (memq (widget-get widget :custom-state) '(modified))))
4214 ("Revert This Session's Customizations" custom-group-reset-saved
4215 (lambda (widget)
4216 (memq (widget-get widget :custom-state) '(modified set))))
4217 ,@(when (or custom-file init-file-user)
4218 '(("Erase Customization" custom-group-reset-standard
4219 (lambda (widget)
4220 (memq (widget-get widget :custom-state) '(modified set saved)))))))
4221 "Alist of actions for the `custom-group' widget.
4222 Each entry has the form (NAME ACTION FILTER) where NAME is the name of
4223 the menu entry, ACTION is the function to call on the widget when the
4224 menu is selected, and FILTER is a predicate which takes a `custom-group'
4225 widget as an argument, and returns non-nil if ACTION is valid on that
4226 widget. If FILTER is nil, ACTION is always valid.")
4228 (defun custom-group-action (widget &optional event)
4229 "Show the menu for `custom-group' WIDGET.
4230 Optional EVENT is the location for the menu."
4231 (if (eq (widget-get widget :custom-state) 'hidden)
4232 (custom-toggle-hide widget)
4233 (let* ((completion-ignore-case t)
4234 (answer (widget-choose (concat "Operation on "
4235 (custom-unlispify-tag-name
4236 (widget-get widget :value)))
4237 (custom-menu-filter custom-group-menu
4238 widget)
4239 event)))
4240 (if answer
4241 (funcall answer widget)))))
4243 (defun custom-group-set (widget)
4244 "Set changes in all modified group members."
4245 (dolist (child (widget-get widget :children))
4246 (when (eq (widget-get child :custom-state) 'modified)
4247 (widget-apply child :custom-set))))
4249 (defun custom-group-mark-to-save (widget)
4250 "Mark all modified group members for saving."
4251 (dolist (child (widget-get widget :children))
4252 (when (memq (widget-get child :custom-state) '(modified set))
4253 (widget-apply child :custom-mark-to-save))))
4255 (defsubst custom-group-state-set-and-redraw (widget)
4256 "Set state of group widget WIDGET and redraw with current settings."
4257 (dolist (child (widget-get widget :children))
4258 (when (memq (widget-get child :custom-state) '(modified set))
4259 (widget-apply child :custom-state-set-and-redraw))))
4261 (defun custom-group-save (widget)
4262 "Save all modified group members."
4263 (custom-group-mark-to-save widget)
4264 (custom-save-all)
4265 (custom-group-state-set-and-redraw widget))
4267 (defun custom-group-reset-current (widget)
4268 "Reset all modified group members."
4269 (dolist (child (widget-get widget :children))
4270 (when (eq (widget-get child :custom-state) 'modified)
4271 (widget-apply child :custom-reset-current))))
4273 (defun custom-group-reset-saved (widget)
4274 "Reset all modified or set group members."
4275 (dolist (child (widget-get widget :children))
4276 (when (memq (widget-get child :custom-state) '(modified set))
4277 (widget-apply child :custom-reset-saved))))
4279 (defun custom-group-reset-standard (widget)
4280 "Reset all modified, set, or saved group members."
4281 (let ((custom-reset-standard-variables-list '(t))
4282 (custom-reset-standard-faces-list '(t)))
4283 (custom-group-mark-to-reset-standard widget)
4284 (custom-reset-standard-save-and-update)))
4286 (defun custom-group-mark-to-reset-standard (widget)
4287 "Mark to reset all modified, set, or saved group members."
4288 (dolist (child (widget-get widget :children))
4289 (when (memq (widget-get child :custom-state)
4290 '(modified set saved))
4291 (widget-apply child :custom-mark-to-reset-standard))))
4293 (defun custom-group-state-update (widget)
4294 "Update magic."
4295 (unless (eq (widget-get widget :custom-state) 'hidden)
4296 (let* ((children (widget-get widget :children))
4297 (states (mapcar (lambda (child)
4298 (widget-get child :custom-state))
4299 children))
4300 (magics custom-magic-alist)
4301 (found 'standard))
4302 (while magics
4303 (let ((magic (car (car magics))))
4304 (if (and (not (eq magic 'hidden))
4305 (memq magic states))
4306 (setq found magic
4307 magics nil)
4308 (setq magics (cdr magics)))))
4309 (widget-put widget :custom-state found)))
4310 (custom-magic-reset widget))
4312 ;;; Reading and writing the custom file.
4314 ;;;###autoload
4315 (defcustom custom-file nil
4316 "File used for storing customization information.
4317 The default is nil, which means to use your init file
4318 as specified by `user-init-file'. If the value is not nil,
4319 it should be an absolute file name.
4321 You can set this option through Custom, if you carefully read the
4322 last paragraph below. However, usually it is simpler to write
4323 something like the following in your init file:
4325 \(setq custom-file \"~/.emacs-custom.el\")
4326 \(load custom-file)
4328 Note that both lines are necessary: the first line tells Custom to
4329 save all customizations in this file, but does not load it.
4331 When you change this variable outside Custom, look in the
4332 previous custom file (usually your init file) for the
4333 forms `(custom-set-variables ...)' and `(custom-set-faces ...)',
4334 and copy them (whichever ones you find) to the new custom file.
4335 This will preserve your existing customizations.
4337 If you save this option using Custom, Custom will write all
4338 currently saved customizations, including the new one for this
4339 option itself, into the file you specify, overwriting any
4340 `custom-set-variables' and `custom-set-faces' forms already
4341 present in that file. It will not delete any customizations from
4342 the old custom file. You should do that manually if that is what you
4343 want. You also have to put something like `(load \"CUSTOM-FILE\")
4344 in your init file, where CUSTOM-FILE is the actual name of the
4345 file. Otherwise, Emacs will not load the file when it starts up,
4346 and hence will not set `custom-file' to that file either."
4347 :type '(choice (const :tag "Your Emacs init file" nil)
4348 (file :format "%t:%v%d"
4349 :doc
4350 "Please read entire docstring below before setting \
4351 this through Custom.
4352 Click on \"More\" (or position point there and press RETURN)
4353 if only the first line of the docstring is shown."))
4354 :group 'customize)
4356 (defun custom-file (&optional no-error)
4357 "Return the file name for saving customizations."
4358 (if (null user-init-file)
4359 ;; Started with -q, i.e. the file containing Custom settings
4360 ;; hasn't been read. Saving settings there won't make much
4361 ;; sense.
4362 (if no-error
4364 (user-error "Saving settings from \"emacs -q\" would overwrite existing customizations"))
4365 (file-chase-links (or custom-file user-init-file))))
4367 ;; If recentf-mode is non-nil, this is defined.
4368 (declare-function recentf-expand-file-name "recentf" (name))
4370 ;;;###autoload
4371 (defun custom-save-all ()
4372 "Save all customizations in `custom-file'."
4373 (when (and (null custom-file) init-file-had-error)
4374 (error "Cannot save customizations; init file was not fully loaded"))
4375 (let* ((filename (custom-file))
4376 (recentf-exclude
4377 (if recentf-mode
4378 (cons (concat "\\`"
4379 (regexp-quote
4380 (recentf-expand-file-name (custom-file)))
4381 "\\'")
4382 recentf-exclude)))
4383 (old-buffer (find-buffer-visiting filename))
4384 old-buffer-name)
4386 (with-current-buffer (let ((find-file-visit-truename t))
4387 (or old-buffer (find-file-noselect filename)))
4388 ;; We'll save using file-precious-flag, so avoid destroying
4389 ;; symlinks. (If we're not already visiting the buffer, this is
4390 ;; handled by find-file-visit-truename, above.)
4391 (when old-buffer
4392 (setq old-buffer-name (buffer-file-name))
4393 (set-visited-file-name (file-chase-links filename)))
4395 (unless (eq major-mode 'emacs-lisp-mode)
4396 (emacs-lisp-mode))
4397 (let ((inhibit-read-only t)
4398 (print-length nil)
4399 (print-level nil))
4400 (custom-save-variables)
4401 (custom-save-faces))
4402 (let ((file-precious-flag t))
4403 (save-buffer))
4404 (if old-buffer
4405 (progn
4406 (set-visited-file-name old-buffer-name)
4407 (set-buffer-modified-p nil))
4408 (kill-buffer (current-buffer))))))
4410 ;;;###autoload
4411 (defun customize-save-customized ()
4412 "Save all user options which have been set in this session."
4413 (interactive)
4414 (mapatoms (lambda (symbol)
4415 (let ((face (get symbol 'customized-face))
4416 (value (get symbol 'customized-value))
4417 (face-comment (get symbol 'customized-face-comment))
4418 (variable-comment
4419 (get symbol 'customized-variable-comment)))
4420 (when face
4421 (put symbol 'saved-face face)
4422 (custom-push-theme 'theme-face symbol 'user 'set value)
4423 (put symbol 'customized-face nil))
4424 (when value
4425 (put symbol 'saved-value value)
4426 (custom-push-theme 'theme-value symbol 'user 'set value)
4427 (put symbol 'customized-value nil))
4428 (when variable-comment
4429 (put symbol 'saved-variable-comment variable-comment)
4430 (put symbol 'customized-variable-comment nil))
4431 (when face-comment
4432 (put symbol 'saved-face-comment face-comment)
4433 (put symbol 'customized-face-comment nil)))))
4434 ;; We really should update all custom buffers here.
4435 (custom-save-all))
4437 ;; Editing the custom file contents in a buffer.
4439 (defun custom-save-delete (symbol)
4440 "Delete all calls to SYMBOL from the contents of the current buffer.
4441 Leave point at the old location of the first such call,
4442 or (if there were none) at the end of the buffer.
4444 This function does not save the buffer."
4445 (goto-char (point-min))
4446 ;; Skip all whitespace and comments.
4447 (while (forward-comment 1))
4448 (or (eobp)
4449 (save-excursion (forward-sexp (buffer-size)))) ; Test for scan errors.
4450 (let (first)
4451 (catch 'found
4452 (while t ;; We exit this loop only via throw.
4453 ;; Skip all whitespace and comments.
4454 (while (forward-comment 1))
4455 (let ((start (point))
4456 (sexp (condition-case nil
4457 (read (current-buffer))
4458 (end-of-file (throw 'found nil)))))
4459 (when (and (listp sexp)
4460 (eq (car sexp) symbol))
4461 (delete-region start (point))
4462 (unless first
4463 (setq first (point)))))))
4464 (if first
4465 (goto-char first)
4466 ;; Move in front of local variables, otherwise long Custom
4467 ;; entries would make them ineffective.
4468 (let ((pos (point-max))
4469 (case-fold-search t))
4470 (save-excursion
4471 (goto-char (point-max))
4472 (search-backward "\n\^L" (max (- (point-max) 3000) (point-min))
4473 'move)
4474 (when (search-forward "Local Variables:" nil t)
4475 (setq pos (line-beginning-position))))
4476 (goto-char pos)))))
4478 (defvar sort-fold-case) ; defined in sort.el
4480 (defun custom-save-variables ()
4481 "Save all customized variables in `custom-file'."
4482 (save-excursion
4483 (custom-save-delete 'custom-set-variables)
4484 (let ((standard-output (current-buffer))
4485 (saved-list (make-list 1 0))
4486 sort-fold-case)
4487 ;; First create a sorted list of saved variables.
4488 (mapatoms
4489 (lambda (symbol)
4490 (if (and (get symbol 'saved-value)
4491 ;; ignore theme values
4492 (or (null (get symbol 'theme-value))
4493 (eq 'user (caar (get symbol 'theme-value)))))
4494 (nconc saved-list (list symbol)))))
4495 (setq saved-list (sort (cdr saved-list) 'string<))
4496 (unless (bolp)
4497 (princ "\n"))
4498 (princ "(custom-set-variables
4499 ;; custom-set-variables was added by Custom.
4500 ;; If you edit it by hand, you could mess it up, so be careful.
4501 ;; Your init file should contain only one such instance.
4502 ;; If there is more than one, they won't work right.\n")
4503 (dolist (symbol saved-list)
4504 (let ((spec (car-safe (get symbol 'theme-value)))
4505 (value (get symbol 'saved-value))
4506 (requests (get symbol 'custom-requests))
4507 (now (and (not (custom-variable-p symbol))
4508 (or (boundp symbol)
4509 (eq (get symbol 'force-value)
4510 'rogue))))
4511 (comment (get symbol 'saved-variable-comment)))
4512 ;; Check REQUESTS for validity.
4513 (dolist (request requests)
4514 (when (and (symbolp request) (not (featurep request)))
4515 (message "Unknown requested feature: %s" request)
4516 (setq requests (delq request requests))))
4517 ;; Is there anything customized about this variable?
4518 (when (or (and spec (eq (car spec) 'user))
4519 comment
4520 (and (null spec) (get symbol 'saved-value)))
4521 ;; Output an element for this variable.
4522 ;; It has the form (SYMBOL VALUE-FORM NOW REQUESTS COMMENT).
4523 ;; SYMBOL is the variable name.
4524 ;; VALUE-FORM is an expression to return the customized value.
4525 ;; NOW if non-nil means always set the variable immediately
4526 ;; when the customizations are reloaded. This is used
4527 ;; for rogue variables
4528 ;; REQUESTS is a list of packages to load before setting the
4529 ;; variable. Each element of it will be passed to `require'.
4530 ;; COMMENT is whatever comment the user has specified
4531 ;; with the customize facility.
4532 (unless (bolp)
4533 (princ "\n"))
4534 (princ " '(")
4535 (prin1 symbol)
4536 (princ " ")
4537 (let ((val (prin1-to-string (car value))))
4538 (if (< (length val) 60)
4539 (insert val)
4540 (newline-and-indent)
4541 (let ((beginning-of-val (point)))
4542 (insert val)
4543 (save-excursion
4544 (goto-char beginning-of-val)
4545 (indent-pp-sexp 1)))))
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-p "\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-p "\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 (setq-local widget-documentation-face 'custom-documentation)
4766 (setq-local widget-button-face custom-button)
4767 (setq-local widget-button-pressed-face custom-button-pressed)
4768 (setq-local 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 (setq-local 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 (setq-local widget-push-button-prefix "")
4778 (setq-local widget-push-button-suffix "")
4779 (setq-local widget-link-prefix "")
4780 (setq-local 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 (setq-local 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