Add menu for M-x man
[emacs.git] / lisp / custom.el
blobd721198da0bb1b7eb537e157a6bf5fca94ba0308
1 ;;; custom.el --- tools for declaring and initializing options
2 ;;
3 ;; Copyright (C) 1996-1997, 1999, 2001-2013 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 only contains the code needed to declare and initialize
28 ;; user options. The code to customize options is autoloaded from
29 ;; `cus-edit.el' and is documented in the Emacs Lisp Reference manual.
31 ;; The code implementing face declarations is in `cus-face.el'.
33 ;;; Code:
35 (require 'widget)
37 (defvar custom-define-hook nil
38 ;; Customize information for this option is in `cus-edit.el'.
39 "Hook called after defining each customize option.")
41 (defvar custom-dont-initialize nil
42 "Non-nil means `defcustom' should not initialize the variable.
43 That is used for the sake of `custom-make-dependencies'.
44 Users should not set it.")
46 (defvar custom-current-group-alist nil
47 "Alist of (FILE . GROUP) indicating the current group to use for FILE.")
49 ;;; The `defcustom' Macro.
51 (defun custom-initialize-default (symbol exp)
52 "Initialize SYMBOL with EXP.
53 This will do nothing if symbol already has a default binding.
54 Otherwise, if symbol has a `saved-value' property, it will evaluate
55 the car of that and use it as the default binding for symbol.
56 Otherwise, EXP will be evaluated and used as the default binding for
57 symbol."
58 (eval `(defvar ,symbol ,(let ((sv (get symbol 'saved-value)))
59 (if sv (car sv) exp)))))
61 (defun custom-initialize-set (symbol exp)
62 "Initialize SYMBOL based on EXP.
63 If the symbol doesn't have a default binding already,
64 then set it using its `:set' function (or `set-default' if it has none).
65 The value is either the value in the symbol's `saved-value' property,
66 if any, or the value of EXP."
67 (condition-case nil
68 (default-toplevel-value symbol)
69 (error
70 (funcall (or (get symbol 'custom-set) #'set-default-toplevel-value)
71 symbol
72 (eval (let ((sv (get symbol 'saved-value)))
73 (if sv (car sv) exp)))))))
75 (defun custom-initialize-reset (symbol exp)
76 "Initialize SYMBOL based on EXP.
77 Set the symbol, using its `:set' function (or `set-default' if it has none).
78 The value is either the symbol's current value
79 (as obtained using the `:get' function), if any,
80 or the value in the symbol's `saved-value' property if any,
81 or (last of all) the value of EXP."
82 (funcall (or (get symbol 'custom-set) #'set-default-toplevel-value)
83 symbol
84 (condition-case nil
85 (let ((def (default-toplevel-value symbol))
86 (getter (get symbol 'custom-get)))
87 (if getter (funcall getter symbol) def))
88 (error
89 (eval (let ((sv (get symbol 'saved-value)))
90 (if sv (car sv) exp)))))))
92 (defun custom-initialize-changed (symbol exp)
93 "Initialize SYMBOL with EXP.
94 Like `custom-initialize-reset', but only use the `:set' function if
95 not using the standard setting.
96 For the standard setting, use `set-default'."
97 (condition-case nil
98 (let ((def (default-toplevel-value symbol)))
99 (funcall (or (get symbol 'custom-set) #'set-default-toplevel-value)
100 symbol
101 (let ((getter (get symbol 'custom-get)))
102 (if getter (funcall getter symbol) def))))
103 (error
104 (cond
105 ((get symbol 'saved-value)
106 (funcall (or (get symbol 'custom-set) #'set-default-toplevel-value)
107 symbol
108 (eval (car (get symbol 'saved-value)))))
110 (set-default symbol (eval exp)))))))
112 (defvar custom-delayed-init-variables nil
113 "List of variables whose initialization is pending.")
115 (defun custom-initialize-delay (symbol _value)
116 "Delay initialization of SYMBOL to the next Emacs start.
117 This is used in files that are preloaded (or for autoloaded
118 variables), so that the initialization is done in the run-time
119 context rather than the build-time context. This also has the
120 side-effect that the (delayed) initialization is performed with
121 the :set function.
123 For variables in preloaded files, you can simply use this
124 function for the :initialize property. For autoloaded variables,
125 you will also need to add an autoload stanza calling this
126 function, and another one setting the standard-value property.
127 Or you can wrap the defcustom in a progn, to force the autoloader
128 to include all of it." ; see eg vc-sccs-search-project-dir
129 ;; No longer true:
130 ;; "See `send-mail-function' in sendmail.el for an example."
132 ;; Until the var is actually initialized, it is kept unbound.
133 ;; This seemed to be at least as good as setting it to an arbitrary
134 ;; value like nil (evaluating `value' is not an option because it
135 ;; may have undesirable side-effects).
136 (push symbol custom-delayed-init-variables))
138 (defun custom-declare-variable (symbol default doc &rest args)
139 "Like `defcustom', but SYMBOL and DEFAULT are evaluated as normal arguments.
140 DEFAULT should be an expression to evaluate to compute the default value,
141 not the default value itself.
143 DEFAULT is stored as SYMBOL's standard value, in SYMBOL's property
144 `standard-value'. At the same time, SYMBOL's property `force-value' is
145 set to nil, as the value is no longer rogue."
146 (put symbol 'standard-value (purecopy (list default)))
147 ;; Maybe this option was rogue in an earlier version. It no longer is.
148 (when (get symbol 'force-value)
149 (put symbol 'force-value nil))
150 (if (keywordp doc)
151 (error "Doc string is missing"))
152 (let ((initialize 'custom-initialize-reset)
153 (requests nil))
154 (unless (memq :group args)
155 (custom-add-to-group (custom-current-group) symbol 'custom-variable))
156 (while args
157 (let ((arg (car args)))
158 (setq args (cdr args))
159 (unless (symbolp arg)
160 (error "Junk in args %S" args))
161 (let ((keyword arg)
162 (value (car args)))
163 (unless args
164 (error "Keyword %s is missing an argument" keyword))
165 (setq args (cdr args))
166 (cond ((eq keyword :initialize)
167 (setq initialize value))
168 ((eq keyword :set)
169 (put symbol 'custom-set value))
170 ((eq keyword :get)
171 (put symbol 'custom-get value))
172 ((eq keyword :require)
173 (push value requests))
174 ((eq keyword :risky)
175 (put symbol 'risky-local-variable value))
176 ((eq keyword :safe)
177 (put symbol 'safe-local-variable value))
178 ((eq keyword :type)
179 (put symbol 'custom-type (purecopy value)))
180 ((eq keyword :options)
181 (if (get symbol 'custom-options)
182 ;; Slow safe code to avoid duplicates.
183 (mapc (lambda (option)
184 (custom-add-option symbol option))
185 value)
186 ;; Fast code for the common case.
187 (put symbol 'custom-options (copy-sequence value))))
189 (custom-handle-keyword symbol keyword value
190 'custom-variable))))))
191 (put symbol 'custom-requests requests)
192 ;; Do the actual initialization.
193 (unless custom-dont-initialize
194 (funcall initialize symbol default)))
195 ;; Use defvar to set the docstring as well as the special-variable-p flag.
196 ;; FIXME: We should reproduce more of `defvar's behavior, such as the warning
197 ;; when the var is currently let-bound.
198 (if (not (default-boundp symbol))
199 ;; Don't use defvar to avoid setting a default-value when undesired.
200 (when doc (put symbol 'variable-documentation doc))
201 (eval `(defvar ,symbol nil ,@(when doc (list doc)))))
202 (push symbol current-load-list)
203 (run-hooks 'custom-define-hook)
204 symbol)
206 (defmacro defcustom (symbol standard doc &rest args)
207 "Declare SYMBOL as a customizable variable.
208 SYMBOL is the variable name; it should not be quoted.
209 STANDARD is an expression specifying the variable's standard
210 value. It should not be quoted. It is evaluated once by
211 `defcustom', and the value is assigned to SYMBOL if the variable
212 is unbound. The expression itself is also stored, so that
213 Customize can re-evaluate it later to get the standard value.
214 DOC is the variable documentation.
216 This macro uses `defvar' as a subroutine, which also marks the
217 variable as \"special\", so that it is always dynamically bound
218 even when `lexical-binding' is t.
220 The remaining arguments to `defcustom' should have the form
222 [KEYWORD VALUE]...
224 The following keywords are meaningful:
226 :type VALUE should be a widget type for editing the symbol's value.
227 :options VALUE should be a list of valid members of the widget type.
228 :initialize
229 VALUE should be a function used to initialize the
230 variable. It takes two arguments, the symbol and value
231 given in the `defcustom' call. The default is
232 `custom-initialize-reset'.
233 :set VALUE should be a function to set the value of the symbol
234 when using the Customize user interface.
235 It takes two arguments, the symbol to set and the value to
236 give it. The default choice of function is `set-default'.
237 :get VALUE should be a function to extract the value of symbol.
238 The function takes one argument, a symbol, and should return
239 the current value for that symbol. The default choice of function
240 is `default-value'.
241 :require
242 VALUE should be a feature symbol. If you save a value
243 for this option, then when your init file loads the value,
244 it does (require VALUE) first.
245 :set-after VARIABLES
246 Specifies that SYMBOL should be set after the list of variables
247 VARIABLES when both have been customized.
248 :risky Set SYMBOL's `risky-local-variable' property to VALUE.
249 :safe Set SYMBOL's `safe-local-variable' property to VALUE.
250 See Info node `(elisp) File Local Variables'.
252 The following common keywords are also meaningful.
254 :group VALUE should be a customization group.
255 Add SYMBOL (or FACE with `defface') to that group.
256 :link LINK-DATA
257 Include an external link after the documentation string for this
258 item. This is a sentence containing an active field which
259 references some other documentation.
261 There are several alternatives you can use for LINK-DATA:
263 (custom-manual INFO-NODE)
264 Link to an Info node; INFO-NODE is a string which specifies
265 the node name, as in \"(emacs)Top\".
267 (info-link INFO-NODE)
268 Like `custom-manual' except that the link appears in the
269 customization buffer with the Info node name.
271 (url-link URL)
272 Link to a web page; URL is a string which specifies the URL.
274 (emacs-commentary-link LIBRARY)
275 Link to the commentary section of LIBRARY.
277 (emacs-library-link LIBRARY)
278 Link to an Emacs Lisp LIBRARY file.
280 (file-link FILE)
281 Link to FILE.
283 (function-link FUNCTION)
284 Link to the documentation of FUNCTION.
286 (variable-link VARIABLE)
287 Link to the documentation of VARIABLE.
289 (custom-group-link GROUP)
290 Link to another customization GROUP.
292 You can specify the text to use in the customization buffer by
293 adding `:tag NAME' after the first element of the LINK-DATA; for
294 example, (info-link :tag \"foo\" \"(emacs)Top\") makes a link to the
295 Emacs manual which appears in the buffer as `foo'.
297 An item can have more than one external link; however, most items
298 have none at all.
299 :version
300 VALUE should be a string specifying that the variable was
301 first introduced, or its default value was changed, in Emacs
302 version VERSION.
303 :package-version
304 VALUE should be a list with the form (PACKAGE . VERSION)
305 specifying that the variable was first introduced, or its
306 default value was changed, in PACKAGE version VERSION. This
307 keyword takes priority over :version. The PACKAGE and VERSION
308 must appear in the alist `customize-package-emacs-version-alist'.
309 Since PACKAGE must be unique and the user might see it in an
310 error message, a good choice is the official name of the
311 package, such as MH-E or Gnus.
312 :tag LABEL
313 Use LABEL, a string, instead of the item's name, to label the item
314 in customization menus and buffers.
315 :load FILE
316 Load file FILE (a string) before displaying this customization
317 item. Loading is done with `load', and only if the file is
318 not already loaded.
320 If SYMBOL has a local binding, then this form affects the local
321 binding. This is normally not what you want. Thus, if you need
322 to load a file defining variables with this form, or with
323 `defvar' or `defconst', you should always load that file
324 _outside_ any bindings for these variables. (`defvar' and
325 `defconst' behave similarly in this respect.)
327 See Info node `(elisp) Customization' in the Emacs Lisp manual
328 for more information."
329 (declare (doc-string 3) (debug (name body)))
330 ;; It is better not to use backquote in this file,
331 ;; because that makes a bootstrapping problem
332 ;; if you need to recompile all the Lisp files using interpreted code.
333 `(custom-declare-variable
334 ',symbol
335 ,(if lexical-binding ;FIXME: This is not reliable, but is all we have.
336 ;; The STANDARD arg should be an expression that evaluates to
337 ;; the standard value. The use of `eval' for it is spread
338 ;; over many different places and hence difficult to
339 ;; eliminate, yet we want to make sure that the `standard'
340 ;; expression is checked by the byte-compiler, and that
341 ;; lexical-binding is obeyed, so quote the expression with
342 ;; `lambda' rather than with `quote'.
343 ``(funcall #',(lambda () ,standard))
344 `',standard)
345 ,doc
346 ,@args))
348 ;;; The `defface' Macro.
350 (defmacro defface (face spec doc &rest args)
351 "Declare FACE as a customizable face that defaults to SPEC.
352 FACE does not need to be quoted.
354 Third argument DOC is the face documentation.
356 If FACE has been set with `custom-set-faces', set the face
357 attributes as specified by that function, otherwise set the face
358 attributes according to SPEC.
360 The remaining arguments should have the form [KEYWORD VALUE]...
361 For a list of valid keywords, see the common keywords listed in
362 `defcustom'.
364 SPEC should be an alist of the form
366 ((DISPLAY . ATTS)...)
368 where DISPLAY is a form specifying conditions to match certain
369 terminals and ATTS is a property list (ATTR VALUE ATTR VALUE...)
370 specifying face attributes and values for frames on those
371 terminals. On each terminal, the first element with a matching
372 DISPLAY specification takes effect, and the remaining elements in
373 SPEC are disregarded.
375 As a special exception, in the first element of SPEC, DISPLAY can
376 be the special value `default'. Then the ATTS in that element
377 act as defaults for all the following elements.
379 For backward compatibility, elements of SPEC can be written
380 as (DISPLAY ATTS) instead of (DISPLAY . ATTS).
382 Each DISPLAY can have the following values:
383 - `default' (only in the first element).
384 - The symbol t, which matches all terminals.
385 - An alist of conditions. Each alist element must have the form
386 (REQ ITEM...). A matching terminal must satisfy each
387 specified condition by matching one of its ITEMs. Each REQ
388 must be one of the following:
389 - `type' (the terminal type).
390 Each ITEM must be one of the values returned by
391 `window-system'. Under X, additional allowed values are
392 `motif', `lucid', `gtk' and `x-toolkit'.
393 - `class' (the terminal's color support).
394 Each ITEM should be one of `color', `grayscale', or `mono'.
395 - `background' (what color is used for the background text)
396 Each ITEM should be one of `light' or `dark'.
397 - `min-colors' (the minimum number of supported colors)
398 Each ITEM should be an integer, which is compared with the
399 result of `display-color-cells'.
400 - `supports' (match terminals supporting certain attributes).
401 Each ITEM should be a list of face attributes. See
402 `display-supports-face-attributes-p' for more information on
403 exactly how testing is done.
405 In the ATTS property list, possible attributes are `:family',
406 `:width', `:height', `:weight', `:slant', `:underline',
407 `:overline', `:strike-through', `:box', `:foreground',
408 `:background', `:stipple', `:inverse-video', and `:inherit'.
410 See Info node `(elisp) Faces' in the Emacs Lisp manual for more
411 information."
412 (declare (doc-string 3))
413 ;; It is better not to use backquote in this file,
414 ;; because that makes a bootstrapping problem
415 ;; if you need to recompile all the Lisp files using interpreted code.
416 (nconc (list 'custom-declare-face (list 'quote face) spec doc) args))
418 ;;; The `defgroup' Macro.
420 (defun custom-current-group ()
421 (cdr (assoc load-file-name custom-current-group-alist)))
423 (defun custom-declare-group (symbol members doc &rest args)
424 "Like `defgroup', but SYMBOL is evaluated as a normal argument."
425 (while members
426 (apply 'custom-add-to-group symbol (car members))
427 (setq members (cdr members)))
428 (when doc
429 ;; This text doesn't get into DOC.
430 (put symbol 'group-documentation (purecopy doc)))
431 (while args
432 (let ((arg (car args)))
433 (setq args (cdr args))
434 (unless (symbolp arg)
435 (error "Junk in args %S" args))
436 (let ((keyword arg)
437 (value (car args)))
438 (unless args
439 (error "Keyword %s is missing an argument" keyword))
440 (setq args (cdr args))
441 (cond ((eq keyword :prefix)
442 (put symbol 'custom-prefix (purecopy value)))
444 (custom-handle-keyword symbol keyword value
445 'custom-group))))))
446 ;; Record the group on the `current' list.
447 (let ((elt (assoc load-file-name custom-current-group-alist)))
448 (if elt (setcdr elt symbol)
449 (push (cons (purecopy load-file-name) symbol)
450 custom-current-group-alist)))
451 (run-hooks 'custom-define-hook)
452 symbol)
454 (defmacro defgroup (symbol members doc &rest args)
455 "Declare SYMBOL as a customization group containing MEMBERS.
456 SYMBOL does not need to be quoted.
458 Third argument DOC is the group documentation. This should be a short
459 description of the group, beginning with a capital and ending with
460 a period. Words other than the first should not be capitalized, if they
461 are not usually written so.
463 MEMBERS should be an alist of the form ((NAME WIDGET)...) where
464 NAME is a symbol and WIDGET is a widget for editing that symbol.
465 Useful widgets are `custom-variable' for editing variables,
466 `custom-face' for edit faces, and `custom-group' for editing groups.
468 The remaining arguments should have the form
470 [KEYWORD VALUE]...
472 For a list of valid keywords, see the common keywords listed in
473 `defcustom'.
475 See Info node `(elisp) Customization' in the Emacs Lisp manual
476 for more information."
477 (declare (doc-string 3))
478 ;; It is better not to use backquote in this file,
479 ;; because that makes a bootstrapping problem
480 ;; if you need to recompile all the Lisp files using interpreted code.
481 (nconc (list 'custom-declare-group (list 'quote symbol) members doc) args))
483 (defun custom-add-to-group (group option widget)
484 "To existing GROUP add a new OPTION of type WIDGET.
485 If there already is an entry for OPTION and WIDGET, nothing is done."
486 (let ((members (get group 'custom-group))
487 (entry (list option widget)))
488 (unless (member entry members)
489 (put group 'custom-group (nconc members (list entry))))))
491 (defun custom-group-of-mode (mode)
492 "Return the custom group corresponding to the major or minor MODE.
493 If no such group is found, return nil."
494 (or (get mode 'custom-mode-group)
495 (if (or (get mode 'custom-group)
496 (and (string-match "-mode\\'" (symbol-name mode))
497 (get (setq mode (intern (substring (symbol-name mode)
498 0 (match-beginning 0))))
499 'custom-group)))
500 mode)))
502 ;;; Properties.
504 (defun custom-handle-all-keywords (symbol args type)
505 "For customization option SYMBOL, handle keyword arguments ARGS.
506 Third argument TYPE is the custom option type."
507 (unless (memq :group args)
508 (custom-add-to-group (custom-current-group) symbol type))
509 (while args
510 (let ((arg (car args)))
511 (setq args (cdr args))
512 (unless (symbolp arg)
513 (error "Junk in args %S" args))
514 (let ((keyword arg)
515 (value (car args)))
516 (unless args
517 (error "Keyword %s is missing an argument" keyword))
518 (setq args (cdr args))
519 (custom-handle-keyword symbol keyword value type)))))
521 (defun custom-handle-keyword (symbol keyword value type)
522 "For customization option SYMBOL, handle KEYWORD with VALUE.
523 Fourth argument TYPE is the custom option type."
524 (if purify-flag
525 (setq value (purecopy value)))
526 (cond ((eq keyword :group)
527 (custom-add-to-group value symbol type))
528 ((eq keyword :version)
529 (custom-add-version symbol value))
530 ((eq keyword :package-version)
531 (custom-add-package-version symbol value))
532 ((eq keyword :link)
533 (custom-add-link symbol value))
534 ((eq keyword :load)
535 (custom-add-load symbol value))
536 ((eq keyword :tag)
537 (put symbol 'custom-tag value))
538 ((eq keyword :set-after)
539 (custom-add-dependencies symbol value))
541 (error "Unknown keyword %s" keyword))))
543 (defun custom-add-dependencies (symbol value)
544 "To the custom option SYMBOL, add dependencies specified by VALUE.
545 VALUE should be a list of symbols. For each symbol in that list,
546 this specifies that SYMBOL should be set after the specified symbol,
547 if both appear in constructs like `custom-set-variables'."
548 (unless (listp value)
549 (error "Invalid custom dependency `%s'" value))
550 (let* ((deps (get symbol 'custom-dependencies))
551 (new-deps deps))
552 (while value
553 (let ((dep (car value)))
554 (unless (symbolp dep)
555 (error "Invalid custom dependency `%s'" dep))
556 (unless (memq dep new-deps)
557 (setq new-deps (cons dep new-deps)))
558 (setq value (cdr value))))
559 (unless (eq deps new-deps)
560 (put symbol 'custom-dependencies new-deps))))
562 (defun custom-add-option (symbol option)
563 "To the variable SYMBOL add OPTION.
565 If SYMBOL's custom type is a hook, OPTION should be a hook member.
566 If SYMBOL's custom type is an alist, OPTION specifies a symbol
567 to offer to the user as a possible key in the alist.
568 For other custom types, this has no effect."
569 (let ((options (get symbol 'custom-options)))
570 (unless (member option options)
571 (put symbol 'custom-options (cons option options)))))
572 (defalias 'custom-add-frequent-value 'custom-add-option)
574 (defun custom-add-link (symbol widget)
575 "To the custom option SYMBOL add the link WIDGET."
576 (let ((links (get symbol 'custom-links)))
577 (unless (member widget links)
578 (put symbol 'custom-links (cons (purecopy widget) links)))))
580 (defun custom-add-version (symbol version)
581 "To the custom option SYMBOL add the version VERSION."
582 (put symbol 'custom-version (purecopy version)))
584 (defun custom-add-package-version (symbol version)
585 "To the custom option SYMBOL add the package version VERSION."
586 (put symbol 'custom-package-version (purecopy version)))
588 (defun custom-add-load (symbol load)
589 "To the custom option SYMBOL add the dependency LOAD.
590 LOAD should be either a library file name, or a feature name."
591 (let ((loads (get symbol 'custom-loads)))
592 (unless (member load loads)
593 (put symbol 'custom-loads (cons (purecopy load) loads)))))
595 (defun custom-autoload (symbol load &optional noset)
596 "Mark SYMBOL as autoloaded custom variable and add dependency LOAD.
597 If NOSET is non-nil, don't bother autoloading LOAD when setting the variable."
598 (put symbol 'custom-autoload (if noset 'noset t))
599 (custom-add-load symbol load))
601 (defun custom-variable-p (variable)
602 "Return non-nil if VARIABLE is a customizable variable.
603 A customizable variable is either (i) a variable whose property
604 list contains a non-nil `standard-value' or `custom-autoload'
605 property, or (ii) an alias for another customizable variable."
606 (when (symbolp variable)
607 (setq variable (indirect-variable variable))
608 (or (get variable 'standard-value)
609 (get variable 'custom-autoload))))
611 (define-obsolete-function-alias 'user-variable-p 'custom-variable-p "24.3")
613 (defun custom-note-var-changed (variable)
614 "Inform Custom that VARIABLE has been set (changed).
615 VARIABLE is a symbol that names a user option.
616 The result is that the change is treated as having been made through Custom."
617 (put variable 'customized-value (list (custom-quote (eval variable)))))
620 ;;; Custom Themes
622 ;;; Loading files needed to customize a symbol.
623 ;;; This is in custom.el because menu-bar.el needs it for toggle cmds.
625 (defvar custom-load-recursion nil
626 "Hack to avoid recursive dependencies.")
628 (defun custom-load-symbol (symbol)
629 "Load all dependencies for SYMBOL."
630 (unless custom-load-recursion
631 (let ((custom-load-recursion t))
632 ;; Load these files if not already done,
633 ;; to make sure we know all the dependencies of SYMBOL.
634 (condition-case nil
635 (require 'cus-load)
636 (error nil))
637 (condition-case nil
638 (require 'cus-start)
639 (error nil))
640 (dolist (load (get symbol 'custom-loads))
641 (cond ((symbolp load) (condition-case nil (require load) (error nil)))
642 ;; This is subsumed by the test below, but it's much faster.
643 ((assoc load load-history))
644 ;; This was just (assoc (locate-library load) load-history)
645 ;; but has been optimized not to load locate-library
646 ;; if not necessary.
647 ((let ((regexp (concat "\\(\\`\\|/\\)" (regexp-quote load)
648 "\\(\\'\\|\\.\\)"))
649 (found nil))
650 (dolist (loaded load-history)
651 (and (stringp (car loaded))
652 (string-match-p regexp (car loaded))
653 (setq found t)))
654 found))
655 ;; Without this, we would load cus-edit recursively.
656 ;; We are still loading it when we call this,
657 ;; and it is not in load-history yet.
658 ((equal load "cus-edit"))
659 (t (condition-case nil (load load) (error nil))))))))
661 (defvar custom-local-buffer nil
662 "Non-nil, in a Customization buffer, means customize a specific buffer.
663 If this variable is non-nil, it should be a buffer,
664 and it means customize the local bindings of that buffer.
665 This variable is a permanent local, and it normally has a local binding
666 in every Customization buffer.")
667 (put 'custom-local-buffer 'permanent-local t)
669 (defun custom-set-default (variable value)
670 "Default :set function for a customizable variable.
671 Normally, this sets the default value of VARIABLE to VALUE,
672 but if `custom-local-buffer' is non-nil,
673 this sets the local binding in that buffer instead."
674 (if custom-local-buffer
675 (with-current-buffer custom-local-buffer
676 (set variable value))
677 (set-default variable value)))
679 (defun custom-set-minor-mode (variable value)
680 ":set function for minor mode variables.
681 Normally, this sets the default value of VARIABLE to nil if VALUE
682 is nil and to t otherwise,
683 but if `custom-local-buffer' is non-nil,
684 this sets the local binding in that buffer instead."
685 (if custom-local-buffer
686 (with-current-buffer custom-local-buffer
687 (funcall variable (if value 1 0)))
688 (funcall variable (if value 1 0))))
690 (defun custom-quote (sexp)
691 "Quote SEXP if it is not self quoting."
692 (if (or (memq sexp '(t nil))
693 (keywordp sexp)
694 (and (listp sexp)
695 (memq (car sexp) '(lambda)))
696 (stringp sexp)
697 (numberp sexp)
698 (vectorp sexp)
699 ;;; (and (fboundp 'characterp)
700 ;;; (characterp sexp))
702 sexp
703 (list 'quote sexp)))
705 (defun customize-mark-to-save (symbol)
706 "Mark SYMBOL for later saving.
708 If the default value of SYMBOL is different from the standard value,
709 set the `saved-value' property to a list whose car evaluates to the
710 default value. Otherwise, set it to nil.
712 To actually save the value, call `custom-save-all'.
714 Return non-nil if the `saved-value' property actually changed."
715 (custom-load-symbol symbol)
716 (let* ((get (or (get symbol 'custom-get) 'default-value))
717 (value (funcall get symbol))
718 (saved (get symbol 'saved-value))
719 (standard (get symbol 'standard-value))
720 (comment (get symbol 'customized-variable-comment)))
721 ;; Save default value if different from standard value.
722 (if (or (null standard)
723 (not (equal value (condition-case nil
724 (eval (car standard))
725 (error nil)))))
726 (put symbol 'saved-value (list (custom-quote value)))
727 (put symbol 'saved-value nil))
728 ;; Clear customized information (set, but not saved).
729 (put symbol 'customized-value nil)
730 ;; Save any comment that might have been set.
731 (when comment
732 (put symbol 'saved-variable-comment comment))
733 (not (equal saved (get symbol 'saved-value)))))
735 (defun customize-mark-as-set (symbol)
736 "Mark current value of SYMBOL as being set from customize.
738 If the default value of SYMBOL is different from the saved value if any,
739 or else if it is different from the standard value, set the
740 `customized-value' property to a list whose car evaluates to the
741 default value. Otherwise, set it to nil.
743 Return non-nil if the `customized-value' property actually changed."
744 (custom-load-symbol symbol)
745 (let* ((get (or (get symbol 'custom-get) 'default-value))
746 (value (funcall get symbol))
747 (customized (get symbol 'customized-value))
748 (old (or (get symbol 'saved-value) (get symbol 'standard-value))))
749 ;; Mark default value as set if different from old value.
750 (if (not (and old
751 (equal value (condition-case nil
752 (eval (car old))
753 (error nil)))))
754 (progn (put symbol 'customized-value (list (custom-quote value)))
755 (custom-push-theme 'theme-value symbol 'user 'set
756 (custom-quote value)))
757 (put symbol 'customized-value nil))
758 ;; Changed?
759 (not (equal customized (get symbol 'customized-value)))))
761 (defun custom-reevaluate-setting (symbol)
762 "Reset the value of SYMBOL by re-evaluating its saved or standard value.
763 Use the :set function to do so. This is useful for customizable options
764 that are defined before their standard value can really be computed.
765 E.g. dumped variables whose default depends on run-time information."
766 (funcall (or (get symbol 'custom-set) 'set-default)
767 symbol
768 (eval (car (or (get symbol 'saved-value) (get symbol 'standard-value))))))
771 ;;; Custom Themes
773 ;; Custom themes are collections of settings that can be enabled or
774 ;; disabled as a unit.
776 ;; Each Custom theme is defined by a symbol, called the theme name.
777 ;; The `theme-settings' property of the theme name records the
778 ;; variable and face settings of the theme. This property is a list
779 ;; of elements, each of the form
781 ;; (PROP SYMBOL THEME VALUE)
783 ;; - PROP is either `theme-value' or `theme-face'
784 ;; - SYMBOL is the face or variable name
785 ;; - THEME is the theme name (redundant, but simplifies the code)
786 ;; - VALUE is an expression that gives the theme's setting for SYMBOL.
788 ;; The theme name also has a `theme-feature' property, whose value is
789 ;; specified when the theme is defined (see `custom-declare-theme').
790 ;; Usually, this is just a symbol named THEME-theme. This lets
791 ;; external libraries call (require 'foo-theme).
793 ;; In addition, each symbol (either a variable or a face) affected by
794 ;; an *enabled* theme has a `theme-value' or `theme-face' property,
795 ;; which is a list of elements each of the form
797 ;; (THEME VALUE)
799 ;; which have the same meanings as in `theme-settings'.
801 ;; The `theme-value' and `theme-face' lists are ordered by decreasing
802 ;; theme precedence. Thus, the first element is always the one that
803 ;; is in effect.
805 ;; Each theme is stored in a theme file, with filename THEME-theme.el.
806 ;; Loading a theme basically involves calling (load "THEME-theme")
807 ;; This is done by the function `load-theme'. Loading a theme
808 ;; automatically enables it.
810 ;; When a theme is enabled, the `theme-value' and `theme-face'
811 ;; properties for the affected symbols are set. When a theme is
812 ;; disabled, its settings are removed from the `theme-value' and
813 ;; `theme-face' properties, but the theme's own `theme-settings'
814 ;; property remains unchanged.
816 (defvar custom-known-themes '(user changed)
817 "Themes that have been defined with `deftheme'.
818 The default value is the list (user changed). The theme `changed'
819 contains the settings before custom themes are applied. The theme
820 `user' contains all the settings the user customized and saved.
821 Additional themes declared with the `deftheme' macro will be added
822 to the front of this list.")
824 (defsubst custom-theme-p (theme)
825 "Non-nil when THEME has been defined."
826 (memq theme custom-known-themes))
828 (defsubst custom-check-theme (theme)
829 "Check whether THEME is valid, and signal an error if it is not."
830 (unless (custom-theme-p theme)
831 (error "Unknown theme `%s'" theme)))
833 (defun custom-push-theme (prop symbol theme mode &optional value)
834 "Record VALUE for face or variable SYMBOL in custom theme THEME.
835 PROP is `theme-face' for a face, `theme-value' for a variable.
837 MODE can be either the symbol `set' or the symbol `reset'. If it is the
838 symbol `set', then VALUE is the value to use. If it is the symbol
839 `reset', then SYMBOL will be removed from THEME (VALUE is ignored).
841 See `custom-known-themes' for a list of known themes."
842 (unless (memq prop '(theme-value theme-face))
843 (error "Unknown theme property"))
844 (let* ((old (get symbol prop))
845 (setting (assq theme old)) ; '(theme value)
846 (theme-settings ; '(prop symbol theme value)
847 (get theme 'theme-settings)))
848 (cond
849 ;; Remove a setting:
850 ((eq mode 'reset)
851 (when setting
852 (let (res)
853 (dolist (theme-setting theme-settings)
854 (if (and (eq (car theme-setting) prop)
855 (eq (cadr theme-setting) symbol))
856 (setq res theme-setting)))
857 (put theme 'theme-settings (delq res theme-settings)))
858 (put symbol prop (delq setting old))))
859 ;; Alter an existing setting:
860 (setting
861 (let (res)
862 (dolist (theme-setting theme-settings)
863 (if (and (eq (car theme-setting) prop)
864 (eq (cadr theme-setting) symbol))
865 (setq res theme-setting)))
866 (put theme 'theme-settings
867 (cons (list prop symbol theme value)
868 (delq res theme-settings)))
869 (setcar (cdr setting) value)))
870 ;; Add a new setting:
872 (unless old
873 ;; If the user changed a variable outside of Customize, save
874 ;; the value to a fake theme, `changed'. If the theme is
875 ;; later disabled, we use this to bring back the old value.
877 ;; For faces, we just use `face-new-frame-defaults' to
878 ;; recompute when the theme is disabled.
879 (when (and (eq prop 'theme-value)
880 (boundp symbol))
881 (let ((sv (get symbol 'standard-value))
882 (val (symbol-value symbol)))
883 (unless (and sv (equal (eval (car sv)) val))
884 (setq old `((changed ,(custom-quote val))))))))
885 (put symbol prop (cons (list theme value) old))
886 (put theme 'theme-settings
887 (cons (list prop symbol theme value) theme-settings))))))
889 (defun custom-fix-face-spec (spec)
890 "Convert face SPEC, replacing obsolete :bold and :italic attributes.
891 Also change :reverse-video to :inverse-video."
892 (when (listp spec)
893 (if (or (memq :bold spec)
894 (memq :italic spec)
895 (memq :inverse-video spec))
896 (let (result)
897 (while spec
898 (let ((key (car spec))
899 (val (car (cdr spec))))
900 (cond ((eq key :italic)
901 (push :slant result)
902 (push (if val 'italic 'normal) result))
903 ((eq key :bold)
904 (push :weight result)
905 (push (if val 'bold 'normal) result))
906 ((eq key :reverse-video)
907 (push :inverse-video result)
908 (push val result))
910 (push key result)
911 (push val result))))
912 (setq spec (cddr spec)))
913 (nreverse result))
914 spec)))
916 (defun custom-set-variables (&rest args)
917 "Install user customizations of variable values specified in ARGS.
918 These settings are registered as theme `user'.
919 The arguments should each be a list of the form:
921 (SYMBOL EXP [NOW [REQUEST [COMMENT]]])
923 This stores EXP (without evaluating it) as the saved value for SYMBOL.
924 If NOW is present and non-nil, then also evaluate EXP and set
925 the default value for the SYMBOL to the value of EXP.
927 REQUEST is a list of features we must require in order to
928 handle SYMBOL properly.
929 COMMENT is a comment string about SYMBOL."
930 (apply 'custom-theme-set-variables 'user args))
932 (defun custom-theme-set-variables (theme &rest args)
933 "Initialize variables for theme THEME according to settings in ARGS.
934 Each of the arguments in ARGS should be a list of this form:
936 (SYMBOL EXP [NOW [REQUEST [COMMENT]]])
938 SYMBOL is the variable name, and EXP is an expression which
939 evaluates to the customized value. EXP will also be stored,
940 without evaluating it, in SYMBOL's `saved-value' property, so
941 that it can be restored via the Customize interface. It is also
942 added to the alist in SYMBOL's `theme-value' property (by
943 calling `custom-push-theme').
945 NOW, if present and non-nil, means to install the variable's
946 value directly now, even if its `defcustom' declaration has not
947 been executed. This is for internal use only.
949 REQUEST is a list of features to `require' (which are loaded
950 prior to evaluating EXP).
952 COMMENT is a comment string about SYMBOL."
953 (custom-check-theme theme)
954 ;; Process all the needed autoloads before anything else, so that the
955 ;; subsequent code has all the info it needs (e.g. which var corresponds
956 ;; to a minor mode), regardless of the ordering of the variables.
957 (dolist (entry args)
958 (let* ((symbol (indirect-variable (nth 0 entry))))
959 (unless (or (get symbol 'standard-value)
960 (memq (get symbol 'custom-autoload) '(nil noset)))
961 ;; This symbol needs to be autoloaded, even just for a `set'.
962 (custom-load-symbol symbol))))
963 (setq args (custom--sort-vars args))
964 (dolist (entry args)
965 (unless (listp entry)
966 (error "Incompatible Custom theme spec"))
967 (let* ((symbol (indirect-variable (nth 0 entry)))
968 (value (nth 1 entry)))
969 (custom-push-theme 'theme-value symbol theme 'set value)
970 (unless custom--inhibit-theme-enable
971 ;; Now set the variable.
972 (let* ((now (nth 2 entry))
973 (requests (nth 3 entry))
974 (comment (nth 4 entry))
975 set)
976 (when requests
977 (put symbol 'custom-requests requests)
978 (mapc 'require requests))
979 (setq set (or (get symbol 'custom-set) 'custom-set-default))
980 (put symbol 'saved-value (list value))
981 (put symbol 'saved-variable-comment comment)
982 ;; Allow for errors in the case where the setter has
983 ;; changed between versions, say, but let the user know.
984 (condition-case data
985 (cond (now
986 ;; Rogue variable, set it now.
987 (put symbol 'force-value t)
988 (funcall set symbol (eval value)))
989 ((default-boundp symbol)
990 ;; Something already set this, overwrite it.
991 (funcall set symbol (eval value))))
992 (error
993 (message "Error setting %s: %s" symbol data)))
994 (and (or now (default-boundp symbol))
995 (put symbol 'variable-comment comment)))))))
997 (defvar custom--sort-vars-table)
998 (defvar custom--sort-vars-result)
1000 (defun custom--sort-vars (vars)
1001 "Sort VARS based on custom dependencies.
1002 VARS is a list whose elements have the same form as the ARGS
1003 arguments to `custom-theme-set-variables'. Return the sorted
1004 list, in which A occurs before B if B was defined with a
1005 `:set-after' keyword specifying A (see `defcustom')."
1006 (let ((custom--sort-vars-table (make-hash-table))
1007 (dependants (make-hash-table))
1008 (custom--sort-vars-result nil)
1009 last)
1010 ;; Construct a pair of tables keyed with the symbols of VARS.
1011 (dolist (var vars)
1012 (puthash (car var) (cons t var) custom--sort-vars-table)
1013 (puthash (car var) var dependants))
1014 ;; From the second table, remove symbols that are depended-on.
1015 (dolist (var vars)
1016 (dolist (dep (get (car var) 'custom-dependencies))
1017 (remhash dep dependants)))
1018 ;; If a variable is "stand-alone", put it last if it's a minor
1019 ;; mode or has a :require flag. This is not really necessary, but
1020 ;; putting minor modes last helps ensure that the mode function
1021 ;; sees other customized values rather than default values.
1022 (maphash (lambda (sym var)
1023 (when (and (null (get sym 'custom-dependencies))
1024 (or (nth 3 var)
1025 (eq (get sym 'custom-set)
1026 'custom-set-minor-mode)))
1027 (remhash sym dependants)
1028 (push var last)))
1029 dependants)
1030 ;; The remaining symbols depend on others but are not
1031 ;; depended-upon. Do a depth-first topological sort.
1032 (maphash #'custom--sort-vars-1 dependants)
1033 (nreverse (append last custom--sort-vars-result))))
1035 (defun custom--sort-vars-1 (sym &optional _ignored)
1036 (let ((elt (gethash sym custom--sort-vars-table)))
1037 ;; The car of the hash table value is nil if the variable has
1038 ;; already been processed, `dependant' if it is a dependant in the
1039 ;; current graph descent, and t otherwise.
1040 (when elt
1041 (cond
1042 ((eq (car elt) 'dependant)
1043 (error "Circular custom dependency on `%s'" sym))
1044 ((car elt)
1045 (setcar elt 'dependant)
1046 (dolist (dep (get sym 'custom-dependencies))
1047 (custom--sort-vars-1 dep))
1048 (setcar elt nil)
1049 (push (cdr elt) custom--sort-vars-result))))))
1052 ;;; Defining themes.
1054 ;; A theme file is named `THEME-theme.el' (where THEME is the theme
1055 ;; name) found in `custom-theme-load-path'. It has this format:
1057 ;; (deftheme THEME
1058 ;; DOCSTRING)
1060 ;; (custom-theme-set-variables
1061 ;; 'THEME
1062 ;; [THEME-VARIABLES])
1064 ;; (custom-theme-set-faces
1065 ;; 'THEME
1066 ;; [THEME-FACES])
1068 ;; (provide-theme 'THEME)
1071 ;; The IGNORED arguments to deftheme come from the XEmacs theme code, where
1072 ;; they were used to supply keyword-value pairs like `:immediate',
1073 ;; `:variable-reset-string', etc. We don't use any of these, so ignore them.
1075 (defmacro deftheme (theme &optional doc &rest ignored)
1076 "Declare THEME to be a Custom theme.
1077 The optional argument DOC is a doc string describing the theme.
1079 Any theme `foo' should be defined in a file called `foo-theme.el';
1080 see `custom-make-theme-feature' for more information."
1081 (declare (doc-string 2))
1082 (let ((feature (custom-make-theme-feature theme)))
1083 ;; It is better not to use backquote in this file,
1084 ;; because that makes a bootstrapping problem
1085 ;; if you need to recompile all the Lisp files using interpreted code.
1086 (list 'custom-declare-theme (list 'quote theme) (list 'quote feature) doc)))
1088 (defun custom-declare-theme (theme feature &optional doc &rest ignored)
1089 "Like `deftheme', but THEME is evaluated as a normal argument.
1090 FEATURE is the feature this theme provides. Normally, this is a symbol
1091 created from THEME by `custom-make-theme-feature'."
1092 (unless (custom-theme-name-valid-p theme)
1093 (error "Custom theme cannot be named %S" theme))
1094 (add-to-list 'custom-known-themes theme)
1095 (put theme 'theme-feature feature)
1096 (when doc (put theme 'theme-documentation doc)))
1098 (defun custom-make-theme-feature (theme)
1099 "Given a symbol THEME, create a new symbol by appending \"-theme\".
1100 Store this symbol in the `theme-feature' property of THEME.
1101 Calling `provide-theme' to provide THEME actually puts `THEME-theme'
1102 into `features'.
1104 This allows for a file-name convention for autoloading themes:
1105 Every theme X has a property `provide-theme' whose value is \"X-theme\".
1106 \(load-theme X) then attempts to load the file `X-theme.el'."
1107 (intern (concat (symbol-name theme) "-theme")))
1109 ;;; Loading themes.
1111 (defcustom custom-theme-directory user-emacs-directory
1112 "Default user directory for storing custom theme files.
1113 The command `customize-create-theme' writes theme files into this
1114 directory. By default, Emacs searches for custom themes in this
1115 directory first---see `custom-theme-load-path'."
1116 :type 'string
1117 :group 'customize
1118 :version "22.1")
1120 (defcustom custom-theme-load-path (list 'custom-theme-directory t)
1121 "List of directories to search for custom theme files.
1122 When loading custom themes (e.g. in `customize-themes' and
1123 `load-theme'), Emacs searches for theme files in the specified
1124 order. Each element in the list should be one of the following:
1125 - the symbol `custom-theme-directory', meaning the value of
1126 `custom-theme-directory'.
1127 - the symbol t, meaning the built-in theme directory (a directory
1128 named \"themes\" in `data-directory').
1129 - a directory name (a string).
1131 Each theme file is named THEME-theme.el, where THEME is the theme
1132 name."
1133 :type '(repeat (choice (const :tag "custom-theme-directory"
1134 custom-theme-directory)
1135 (const :tag "Built-in theme directory" t)
1136 directory))
1137 :group 'customize
1138 :version "24.1")
1140 (defvar custom--inhibit-theme-enable nil
1141 "Whether the custom-theme-set-* functions act immediately.
1142 If nil, `custom-theme-set-variables' and `custom-theme-set-faces'
1143 change the current values of the given variable or face. If
1144 non-nil, they just make a record of the theme settings.")
1146 (defun provide-theme (theme)
1147 "Indicate that this file provides THEME.
1148 This calls `provide' to provide the feature name stored in THEME's
1149 property `theme-feature' (which is usually a symbol created by
1150 `custom-make-theme-feature')."
1151 (unless (custom-theme-name-valid-p theme)
1152 (error "Custom theme cannot be named %S" theme))
1153 (custom-check-theme theme)
1154 (provide (get theme 'theme-feature)))
1156 (defcustom custom-safe-themes '(default)
1157 "Themes that are considered safe to load.
1158 If the value is a list, each element should be either the SHA-256
1159 hash of a safe theme file, or the symbol `default', which stands
1160 for any theme in the built-in Emacs theme directory (a directory
1161 named \"themes\" in `data-directory').
1163 If the value is t, Emacs treats all themes as safe.
1165 This variable cannot be set in a Custom theme."
1166 :type '(choice (repeat :tag "List of safe themes"
1167 (choice string
1168 (const :tag "Built-in themes" default)))
1169 (const :tag "All themes" t))
1170 :group 'customize
1171 :risky t
1172 :version "24.1")
1174 (defun load-theme (theme &optional no-confirm no-enable)
1175 "Load Custom theme named THEME from its file.
1176 The theme file is named THEME-theme.el, in one of the directories
1177 specified by `custom-theme-load-path'.
1179 If the theme is not considered safe by `custom-safe-themes',
1180 prompt the user for confirmation before loading it. But if
1181 optional arg NO-CONFIRM is non-nil, load the theme without
1182 prompting.
1184 Normally, this function also enables THEME. If optional arg
1185 NO-ENABLE is non-nil, load the theme but don't enable it, unless
1186 the theme was already enabled.
1188 This function is normally called through Customize when setting
1189 `custom-enabled-themes'. If used directly in your init file, it
1190 should be called with a non-nil NO-CONFIRM argument, or after
1191 `custom-safe-themes' has been loaded.
1193 Return t if THEME was successfully loaded, nil otherwise."
1194 (interactive
1195 (list
1196 (intern (completing-read "Load custom theme: "
1197 (mapcar 'symbol-name
1198 (custom-available-themes))))
1199 nil nil))
1200 (unless (custom-theme-name-valid-p theme)
1201 (error "Invalid theme name `%s'" theme))
1202 ;; If THEME is already enabled, re-enable it after loading, even if
1203 ;; NO-ENABLE is t.
1204 (if no-enable
1205 (setq no-enable (not (custom-theme-enabled-p theme))))
1206 ;; If reloading, clear out the old theme settings.
1207 (when (custom-theme-p theme)
1208 (disable-theme theme)
1209 (put theme 'theme-settings nil)
1210 (put theme 'theme-feature nil)
1211 (put theme 'theme-documentation nil))
1212 (let ((fn (locate-file (concat (symbol-name theme) "-theme.el")
1213 (custom-theme--load-path)
1214 '("" "c")))
1215 hash)
1216 (unless fn
1217 (error "Unable to find theme file for `%s'" theme))
1218 (with-temp-buffer
1219 (insert-file-contents fn)
1220 (setq hash (secure-hash 'sha256 (current-buffer)))
1221 ;; Check file safety with `custom-safe-themes', prompting the
1222 ;; user if necessary.
1223 (when (or no-confirm
1224 (eq custom-safe-themes t)
1225 (and (memq 'default custom-safe-themes)
1226 (equal (file-name-directory fn)
1227 (expand-file-name "themes/" data-directory)))
1228 (member hash custom-safe-themes)
1229 (custom-theme-load-confirm hash))
1230 (let ((custom--inhibit-theme-enable t)
1231 (buffer-file-name fn)) ;For load-history.
1232 (eval-buffer))
1233 ;; Optimization: if the theme changes the `default' face, put that
1234 ;; entry first. This avoids some `frame-set-background-mode' rigmarole
1235 ;; by assigning the new background immediately.
1236 (let* ((settings (get theme 'theme-settings))
1237 (tail settings)
1238 found)
1239 (while (and tail (not found))
1240 (and (eq (nth 0 (car tail)) 'theme-face)
1241 (eq (nth 1 (car tail)) 'default)
1242 (setq found (car tail)))
1243 (setq tail (cdr tail)))
1244 (if found
1245 (put theme 'theme-settings (cons found (delq found settings)))))
1246 ;; Finally, enable the theme.
1247 (unless no-enable
1248 (enable-theme theme))
1249 t))))
1251 (defun custom-theme-load-confirm (hash)
1252 "Query the user about loading a Custom theme that may not be safe.
1253 The theme should be in the current buffer. If the user agrees,
1254 query also about adding HASH to `custom-safe-themes'."
1255 (unless noninteractive
1256 (save-window-excursion
1257 (rename-buffer "*Custom Theme*" t)
1258 (emacs-lisp-mode)
1259 (pop-to-buffer (current-buffer))
1260 (goto-char (point-min))
1261 (prog1 (when (y-or-n-p "Loading a theme can run Lisp code. Really load? ")
1262 ;; Offer to save to `custom-safe-themes'.
1263 (and (or custom-file user-init-file)
1264 (y-or-n-p "Treat this theme as safe in future sessions? ")
1265 (customize-push-and-save 'custom-safe-themes (list hash)))
1267 (quit-window)))))
1269 (defun custom-theme-name-valid-p (name)
1270 "Return t if NAME is a valid name for a Custom theme, nil otherwise.
1271 NAME should be a symbol."
1272 (and (symbolp name)
1273 name
1274 (not (or (zerop (length (symbol-name name)))
1275 (eq name 'user)
1276 (eq name 'changed)))))
1278 (defun custom-available-themes ()
1279 "Return a list of available Custom themes (symbols)."
1280 (let (sym themes)
1281 (dolist (dir (custom-theme--load-path))
1282 (when (file-directory-p dir)
1283 (dolist (file (file-expand-wildcards
1284 (expand-file-name "*-theme.el" dir) t))
1285 (setq file (file-name-nondirectory file))
1286 (and (string-match "\\`\\(.+\\)-theme.el\\'" file)
1287 (setq sym (intern (match-string 1 file)))
1288 (custom-theme-name-valid-p sym)
1289 (push sym themes)))))
1290 (nreverse (delete-dups themes))))
1292 (defun custom-theme--load-path ()
1293 (let (lpath)
1294 (dolist (f custom-theme-load-path)
1295 (cond ((eq f 'custom-theme-directory)
1296 (setq f custom-theme-directory))
1297 ((eq f t)
1298 (setq f (expand-file-name "themes" data-directory))))
1299 (if (file-directory-p f)
1300 (push f lpath)))
1301 (nreverse lpath)))
1304 ;;; Enabling and disabling loaded themes.
1306 (defun enable-theme (theme)
1307 "Reenable all variable and face settings defined by THEME.
1308 THEME should be either `user', or a theme loaded via `load-theme'.
1309 After this function completes, THEME will have the highest
1310 precedence (after `user')."
1311 (interactive (list (intern
1312 (completing-read
1313 "Enable custom theme: "
1314 obarray (lambda (sym) (get sym 'theme-settings)) t))))
1315 (if (not (custom-theme-p theme))
1316 (error "Undefined Custom theme %s" theme))
1317 (let ((settings (get theme 'theme-settings)))
1318 ;; Loop through theme settings, recalculating vars/faces.
1319 (dolist (s settings)
1320 (let* ((prop (car s))
1321 (symbol (cadr s))
1322 (spec-list (get symbol prop)))
1323 (put symbol prop (cons (cddr s) (assq-delete-all theme spec-list)))
1324 (cond
1325 ((eq prop 'theme-face)
1326 (custom-theme-recalc-face symbol))
1327 ((eq prop 'theme-value)
1328 ;; Ignore `custom-enabled-themes' and `custom-safe-themes'.
1329 (unless (memq symbol '(custom-enabled-themes custom-safe-themes))
1330 (custom-theme-recalc-variable symbol)))))))
1331 (unless (eq theme 'user)
1332 (setq custom-enabled-themes
1333 (cons theme (delq theme custom-enabled-themes)))
1334 ;; Give the `user' theme the highest priority.
1335 (enable-theme 'user)))
1337 (defcustom custom-enabled-themes nil
1338 "List of enabled Custom Themes, highest precedence first.
1339 This list does not include the `user' theme, which is set by
1340 Customize and always takes precedence over other Custom Themes.
1342 This variable cannot be defined inside a Custom theme; there, it
1343 is simply ignored.
1345 Setting this variable through Customize calls `enable-theme' or
1346 `load-theme' for each theme in the list."
1347 :group 'customize
1348 :type '(repeat symbol)
1349 :set-after '(custom-theme-directory custom-theme-load-path
1350 custom-safe-themes)
1351 :risky t
1352 :set (lambda (symbol themes)
1353 (let (failures)
1354 (setq themes (delq 'user (delete-dups themes)))
1355 ;; Disable all themes not in THEMES.
1356 (if (boundp symbol)
1357 (dolist (theme (symbol-value symbol))
1358 (if (not (memq theme themes))
1359 (disable-theme theme))))
1360 ;; Call `enable-theme' or `load-theme' on each of THEMES.
1361 (dolist (theme (reverse themes))
1362 (condition-case nil
1363 (if (custom-theme-p theme)
1364 (enable-theme theme)
1365 (load-theme theme))
1366 (error (setq failures (cons theme failures)
1367 themes (delq theme themes)))))
1368 (enable-theme 'user)
1369 (custom-set-default symbol themes)
1370 (if failures
1371 (message "Failed to enable theme: %s"
1372 (mapconcat 'symbol-name failures ", "))))))
1374 (defsubst custom-theme-enabled-p (theme)
1375 "Return non-nil if THEME is enabled."
1376 (memq theme custom-enabled-themes))
1378 (defun disable-theme (theme)
1379 "Disable all variable and face settings defined by THEME.
1380 See `custom-enabled-themes' for a list of enabled themes."
1381 (interactive (list (intern
1382 (completing-read
1383 "Disable custom theme: "
1384 (mapcar 'symbol-name custom-enabled-themes)
1385 nil t))))
1386 (when (custom-theme-enabled-p theme)
1387 (let ((settings (get theme 'theme-settings)))
1388 (dolist (s settings)
1389 (let* ((prop (car s))
1390 (symbol (cadr s))
1391 (val (assq-delete-all theme (get symbol prop))))
1392 (put symbol prop val)
1393 (cond
1394 ((eq prop 'theme-value)
1395 (custom-theme-recalc-variable symbol))
1396 ((eq prop 'theme-face)
1397 ;; If the face spec specified by this theme is in the
1398 ;; saved-face property, reset that property.
1399 (when (equal (nth 3 s) (get symbol 'saved-face))
1400 (put symbol 'saved-face (and val (cadr (car val)))))))))
1401 ;; Recompute faces on all frames.
1402 (dolist (frame (frame-list))
1403 ;; We must reset the fg and bg color frame parameters, or
1404 ;; `face-set-after-frame-default' will use the existing
1405 ;; parameters, which could be from the disabled theme.
1406 (set-frame-parameter frame 'background-color
1407 (custom--frame-color-default
1408 frame :background "background" "Background"
1409 "unspecified-bg" "white"))
1410 (set-frame-parameter frame 'foreground-color
1411 (custom--frame-color-default
1412 frame :foreground "foreground" "Foreground"
1413 "unspecified-fg" "black"))
1414 (face-set-after-frame-default frame))
1415 (setq custom-enabled-themes
1416 (delq theme custom-enabled-themes)))))
1418 ;; Only used if window-system not null.
1419 (declare-function x-get-resource "frame.c"
1420 (attribute class &optional component subclass))
1422 (defun custom--frame-color-default (frame attribute resource-attr resource-class
1423 tty-default x-default)
1424 (let ((col (face-attribute 'default attribute t)))
1425 (cond
1426 ((and col (not (eq col 'unspecified))) col)
1427 ((null (window-system frame)) tty-default)
1428 ((setq col (x-get-resource resource-attr resource-class)) col)
1429 (t x-default))))
1431 (defun custom-variable-theme-value (variable)
1432 "Return (list VALUE) indicating the custom theme value of VARIABLE.
1433 That is to say, it specifies what the value should be according to
1434 currently enabled custom themes.
1436 This function returns nil if no custom theme specifies a value for VARIABLE."
1437 (let ((theme-value (get variable 'theme-value)))
1438 (if theme-value
1439 (cdr (car theme-value)))))
1441 (defun custom-theme-recalc-variable (variable)
1442 "Set VARIABLE according to currently enabled custom themes."
1443 (let ((valspec (custom-variable-theme-value variable)))
1444 (if valspec
1445 (put variable 'saved-value valspec)
1446 (setq valspec (get variable 'standard-value)))
1447 (if (and valspec
1448 (or (get variable 'force-value)
1449 (default-boundp variable)))
1450 (funcall (or (get variable 'custom-set) 'set-default) variable
1451 (eval (car valspec))))))
1453 (defun custom-theme-recalc-face (face)
1454 "Set FACE according to currently enabled custom themes."
1455 (if (get face 'face-alias)
1456 (setq face (get face 'face-alias)))
1457 ;; Reset the faces for each frame.
1458 (dolist (frame (frame-list))
1459 (face-spec-recalc face frame)))
1462 ;;; XEmacs compatibility functions
1464 ;; In XEmacs, when you reset a Custom Theme, you have to specify the
1465 ;; theme to reset it to. We just apply the next available theme, so
1466 ;; just ignore the IGNORED arguments.
1468 (defun custom-theme-reset-variables (theme &rest args)
1469 "Reset some variable settings in THEME to their values in other themes.
1470 Each of the arguments ARGS has this form:
1472 (VARIABLE IGNORED)
1474 This means reset VARIABLE. (The argument IGNORED is ignored)."
1475 (custom-check-theme theme)
1476 (dolist (arg args)
1477 (custom-push-theme 'theme-value (car arg) theme 'reset)))
1479 (defun custom-reset-variables (&rest args)
1480 "Reset the specs of some variables to their values in other themes.
1481 This creates settings in the `user' theme.
1483 Each of the arguments ARGS has this form:
1485 (VARIABLE IGNORED)
1487 This means reset VARIABLE. (The argument IGNORED is ignored)."
1488 (apply 'custom-theme-reset-variables 'user args))
1490 ;;; The End.
1492 ;; Process the defcustoms for variables loaded before this file.
1493 (while custom-declare-variable-list
1494 (apply 'custom-declare-variable (car custom-declare-variable-list))
1495 (setq custom-declare-variable-list (cdr custom-declare-variable-list)))
1497 (provide 'custom)
1499 ;;; custom.el ends here