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