Update some function declarations
[emacs.git] / lisp / custom.el
blob2d880d23955ab9a5bc8549e0f9eaa4ab2ea4d405
1 ;;; custom.el --- tools for declaring and initializing options
2 ;;
3 ;; Copyright (C) 1996-1997, 1999, 2001-2012 Free Software Foundation, Inc.
4 ;;
5 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
6 ;; Maintainer: FSF
7 ;; Keywords: help, faces
8 ;; Package: emacs
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; This file 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 value)
52 "Initialize SYMBOL with VALUE.
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, VALUE will be evaluated and used as the default binding for
57 symbol."
58 (eval `(defvar ,symbol ,(if (get symbol 'saved-value)
59 (car (get symbol 'saved-value))
60 value))))
62 (defun custom-initialize-set (symbol value)
63 "Initialize SYMBOL based on VALUE.
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 VALUE."
68 (unless (default-boundp symbol)
69 (funcall (or (get symbol 'custom-set) 'set-default)
70 symbol
71 (eval (if (get symbol 'saved-value)
72 (car (get symbol 'saved-value))
73 value)))))
75 (defun custom-initialize-reset (symbol value)
76 "Initialize SYMBOL based on VALUE.
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) VALUE."
82 (funcall (or (get symbol 'custom-set) 'set-default)
83 symbol
84 (cond ((default-boundp symbol)
85 (funcall (or (get symbol 'custom-get) 'default-value)
86 symbol))
87 ((get symbol 'saved-value)
88 (eval (car (get symbol 'saved-value))))
90 (eval value)))))
92 (defun custom-initialize-changed (symbol value)
93 "Initialize SYMBOL with VALUE.
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 (cond ((default-boundp symbol)
98 (funcall (or (get symbol 'custom-set) 'set-default)
99 symbol
100 (funcall (or (get symbol 'custom-get) 'default-value)
101 symbol)))
102 ((get symbol 'saved-value)
103 (funcall (or (get symbol 'custom-set) 'set-default)
104 symbol
105 (eval (car (get symbol 'saved-value)))))
107 (set-default symbol (eval value)))))
109 (defvar custom-delayed-init-variables nil
110 "List of variables whose initialization is pending.")
112 (defun custom-initialize-delay (symbol _value)
113 "Delay initialization of SYMBOL to the next Emacs start.
114 This is used in files that are preloaded (or for autoloaded
115 variables), so that the initialization is done in the run-time
116 context rather than the build-time context. This also has the
117 side-effect that the (delayed) initialization is performed with
118 the :set function.
120 For variables in preloaded files, you can simply use this
121 function for the :initialize property. For autoloaded variables,
122 you will also need to add an autoload stanza calling this
123 function, and another one setting the standard-value property."
124 ;; No longer true:
125 ;; "See `send-mail-function' in sendmail.el for an example."
127 ;; Until the var is actually initialized, it is kept unbound.
128 ;; This seemed to be at least as good as setting it to an arbitrary
129 ;; value like nil (evaluating `value' is not an option because it
130 ;; may have undesirable side-effects).
131 (push symbol custom-delayed-init-variables))
133 (defun custom-declare-variable (symbol default doc &rest args)
134 "Like `defcustom', but SYMBOL and DEFAULT are evaluated as normal arguments.
135 DEFAULT should be an expression to evaluate to compute the default value,
136 not the default value itself.
138 DEFAULT is stored as SYMBOL's standard value, in SYMBOL's property
139 `standard-value'. At the same time, SYMBOL's property `force-value' is
140 set to nil, as the value is no longer rogue."
141 (put symbol 'standard-value (purecopy (list default)))
142 ;; Maybe this option was rogue in an earlier version. It no longer is.
143 (when (get symbol 'force-value)
144 (put symbol 'force-value nil))
145 (if (keywordp doc)
146 (error "Doc string is missing"))
147 (let ((initialize 'custom-initialize-reset)
148 (requests nil))
149 (unless (memq :group args)
150 (custom-add-to-group (custom-current-group) symbol 'custom-variable))
151 (while args
152 (let ((arg (car args)))
153 (setq args (cdr args))
154 (unless (symbolp arg)
155 (error "Junk in args %S" args))
156 (let ((keyword arg)
157 (value (car args)))
158 (unless args
159 (error "Keyword %s is missing an argument" keyword))
160 (setq args (cdr args))
161 (cond ((eq keyword :initialize)
162 (setq initialize value))
163 ((eq keyword :set)
164 (put symbol 'custom-set value))
165 ((eq keyword :get)
166 (put symbol 'custom-get value))
167 ((eq keyword :require)
168 (push value requests))
169 ((eq keyword :risky)
170 (put symbol 'risky-local-variable value))
171 ((eq keyword :safe)
172 (put symbol 'safe-local-variable value))
173 ((eq keyword :type)
174 (put symbol 'custom-type (purecopy value)))
175 ((eq keyword :options)
176 (if (get symbol 'custom-options)
177 ;; Slow safe code to avoid duplicates.
178 (mapc (lambda (option)
179 (custom-add-option symbol option))
180 value)
181 ;; Fast code for the common case.
182 (put symbol 'custom-options (copy-sequence value))))
184 (custom-handle-keyword symbol keyword value
185 'custom-variable))))))
186 (put symbol 'custom-requests requests)
187 ;; Do the actual initialization.
188 (unless custom-dont-initialize
189 (funcall initialize symbol default)))
190 ;; Use defvar to set the docstring as well as the special-variable-p flag.
191 ;; FIXME: We should reproduce more of `defvar's behavior, such as the warning
192 ;; when the var is currently let-bound.
193 (if (not (default-boundp symbol))
194 ;; Don't use defvar to avoid setting a default-value when undesired.
195 (when doc (put symbol 'variable-documentation doc))
196 (eval `(defvar ,symbol nil ,@(when doc (list doc)))))
197 (push symbol current-load-list)
198 (run-hooks 'custom-define-hook)
199 symbol)
201 (defmacro defcustom (symbol standard doc &rest args)
202 "Declare SYMBOL as a customizable variable.
203 SYMBOL is the variable name; it should not be quoted.
204 STANDARD is an expression specifying the variable's standard
205 value. It should not be quoted. It is evaluated once by
206 `defcustom', and the value is assigned to SYMBOL if the variable
207 is unbound. The expression itself is also stored, so that
208 Customize can re-evaluate it later to get the standard value.
209 DOC is the variable documentation.
211 The remaining arguments should have the form
213 [KEYWORD VALUE]...
215 The following keywords are meaningful:
217 :type VALUE should be a widget type for editing the symbol's value.
218 :options VALUE should be a list of valid members of the widget type.
219 :initialize
220 VALUE should be a function used to initialize the
221 variable. It takes two arguments, the symbol and value
222 given in the `defcustom' call. The default is
223 `custom-initialize-reset'.
224 :set VALUE should be a function to set the value of the symbol
225 when using the Customize user interface.
226 It takes two arguments, the symbol to set and the value to
227 give it. The default choice of function is `set-default'.
228 :get VALUE should be a function to extract the value of symbol.
229 The function takes one argument, a symbol, and should return
230 the current value for that symbol. The default choice of function
231 is `default-value'.
232 :require
233 VALUE should be a feature symbol. If you save a value
234 for this option, then when your `.emacs' file loads the value,
235 it does (require VALUE) first.
236 :set-after VARIABLES
237 Specifies that SYMBOL should be set after the list of variables
238 VARIABLES when both have been customized.
239 :risky Set SYMBOL's `risky-local-variable' property to VALUE.
240 :safe Set SYMBOL's `safe-local-variable' property to VALUE.
241 See Info node `(elisp) File Local Variables'.
243 The following common keywords are also meaningful.
245 :group VALUE should be a customization group.
246 Add SYMBOL (or FACE with `defface') to that group.
247 :link LINK-DATA
248 Include an external link after the documentation string for this
249 item. This is a sentence containing an active field which
250 references some other documentation.
252 There are several alternatives you can use for LINK-DATA:
254 (custom-manual INFO-NODE)
255 Link to an Info node; INFO-NODE is a string which specifies
256 the node name, as in \"(emacs)Top\".
258 (info-link INFO-NODE)
259 Like `custom-manual' except that the link appears in the
260 customization buffer with the Info node name.
262 (url-link URL)
263 Link to a web page; URL is a string which specifies the URL.
265 (emacs-commentary-link LIBRARY)
266 Link to the commentary section of LIBRARY.
268 (emacs-library-link LIBRARY)
269 Link to an Emacs Lisp LIBRARY file.
271 (file-link FILE)
272 Link to FILE.
274 (function-link FUNCTION)
275 Link to the documentation of FUNCTION.
277 (variable-link VARIABLE)
278 Link to the documentation of VARIABLE.
280 (custom-group-link GROUP)
281 Link to another customization GROUP.
283 You can specify the text to use in the customization buffer by
284 adding `:tag NAME' after the first element of the LINK-DATA; for
285 example, (info-link :tag \"foo\" \"(emacs)Top\") makes a link to the
286 Emacs manual which appears in the buffer as `foo'.
288 An item can have more than one external link; however, most items
289 have none at all.
290 :version
291 VALUE should be a string specifying that the variable was
292 first introduced, or its default value was changed, in Emacs
293 version VERSION.
294 :package-version
295 VALUE should be a list with the form (PACKAGE . VERSION)
296 specifying that the variable was first introduced, or its
297 default value was changed, in PACKAGE version VERSION. This
298 keyword takes priority over :version. The PACKAGE and VERSION
299 must appear in the alist `customize-package-emacs-version-alist'.
300 Since PACKAGE must be unique and the user might see it in an
301 error message, a good choice is the official name of the
302 package, such as MH-E or Gnus.
303 :tag LABEL
304 Use LABEL, a string, instead of the item's name, to label the item
305 in customization menus and buffers.
306 :load FILE
307 Load file FILE (a string) before displaying this customization
308 item. Loading is done with `load', and only if the file is
309 not already loaded.
311 If SYMBOL has a local binding, then this form affects the local
312 binding. This is normally not what you want. Thus, if you need
313 to load a file defining variables with this form, or with
314 `defvar' or `defconst', you should always load that file
315 _outside_ any bindings for these variables. \(`defvar' and
316 `defconst' behave similarly in this respect.)
318 See Info node `(elisp) Customization' in the Emacs Lisp manual
319 for more information."
320 (declare (doc-string 3) (debug (name body)))
321 ;; It is better not to use backquote in this file,
322 ;; because that makes a bootstrapping problem
323 ;; if you need to recompile all the Lisp files using interpreted code.
324 `(custom-declare-variable
325 ',symbol
326 ,(if lexical-binding ;FIXME: This is not reliable, but is all we have.
327 ;; The STANDARD arg should be an expression that evaluates to
328 ;; the standard value. The use of `eval' for it is spread
329 ;; over many different places and hence difficult to
330 ;; eliminate, yet we want to make sure that the `standard'
331 ;; expression is checked by the byte-compiler, and that
332 ;; lexical-binding is obeyed, so quote the expression with
333 ;; `lambda' rather than with `quote'.
334 `(list (lambda () ,standard))
335 `',standard)
336 ,doc
337 ,@args))
339 ;;; The `defface' Macro.
341 (defmacro defface (face spec doc &rest args)
342 "Declare FACE as a customizable face that defaults to SPEC.
343 FACE does not need to be quoted.
345 Third argument DOC is the face documentation.
347 If FACE has been set with `custom-set-faces', set the face attributes
348 as specified by that function, otherwise set the face attributes
349 according to SPEC.
351 The remaining arguments should have the form
353 [KEYWORD VALUE]...
355 For a list of valid keywords, see the common keywords listed in
356 `defcustom'.
358 SPEC should be an alist of the form ((DISPLAY ATTS)...).
360 In the first element, DISPLAY can be `default'. The ATTS in that
361 element then act as defaults for all the following elements.
363 Aside from that, DISPLAY specifies conditions to match some or
364 all frames. For each frame, the first element of SPEC where the
365 DISPLAY conditions are satisfied is the one that applies to that
366 frame. The ATTRs in this element take effect, and the following
367 elements are ignored, on that frame.
369 In the last element, DISPLAY can be t. That element applies to a
370 frame if none of the previous elements (except the `default' if
371 any) did.
373 ATTS is a list of face attributes followed by their values:
374 (ATTR VALUE ATTR VALUE...)
376 The possible attributes are `:family', `:width', `:height', `:weight',
377 `:slant', `:underline', `:overline', `:strike-through', `:box',
378 `:foreground', `:background', `:stipple', `:inverse-video', and `:inherit'.
380 DISPLAY can be `default' (only in the first element), the symbol
381 t (only in the last element) to match all frames, or an alist of
382 conditions of the form \(REQ ITEM...). For such an alist to
383 match a frame, each of the conditions must be satisfied, meaning
384 that the REQ property of the frame must match one of the
385 corresponding ITEMs. These are the defined REQ values:
387 `type' (the value of `window-system')
388 Under X, in addition to the values `window-system' can take,
389 `motif', `lucid', `gtk' and `x-toolkit' are allowed, and match when
390 the Motif toolkit, Lucid toolkit, GTK toolkit or any X toolkit is in use.
392 `class' (the frame's color support)
393 Should be one of `color', `grayscale', or `mono'.
395 `background' (what color is used for the background text)
396 Should be one of `light' or `dark'.
398 `min-colors' (the minimum number of colors the frame should support)
399 Should be an integer, it is compared with the result of
400 `display-color-cells'.
402 `supports' (only match frames that support the specified face attributes)
403 Should be a list of face attributes. See the documentation for
404 the function `display-supports-face-attributes-p' for more
405 information on exactly how testing is done.
407 See Info node `(elisp) Customization' in the Emacs Lisp manual
408 for more information."
409 (declare (doc-string 3))
410 ;; It is better not to use backquote in this file,
411 ;; because that makes a bootstrapping problem
412 ;; if you need to recompile all the Lisp files using interpreted code.
413 (nconc (list 'custom-declare-face (list 'quote face) spec doc) args))
415 ;;; The `defgroup' Macro.
417 (defun custom-current-group ()
418 (cdr (assoc load-file-name custom-current-group-alist)))
420 (defun custom-declare-group (symbol members doc &rest args)
421 "Like `defgroup', but SYMBOL is evaluated as a normal argument."
422 (while members
423 (apply 'custom-add-to-group symbol (car members))
424 (setq members (cdr members)))
425 (when doc
426 ;; This text doesn't get into DOC.
427 (put symbol 'group-documentation (purecopy doc)))
428 (while args
429 (let ((arg (car args)))
430 (setq args (cdr args))
431 (unless (symbolp arg)
432 (error "Junk in args %S" args))
433 (let ((keyword arg)
434 (value (car args)))
435 (unless args
436 (error "Keyword %s is missing an argument" keyword))
437 (setq args (cdr args))
438 (cond ((eq keyword :prefix)
439 (put symbol 'custom-prefix (purecopy value)))
441 (custom-handle-keyword symbol keyword value
442 'custom-group))))))
443 ;; Record the group on the `current' list.
444 (let ((elt (assoc load-file-name custom-current-group-alist)))
445 (if elt (setcdr elt symbol)
446 (push (cons (purecopy load-file-name) symbol)
447 custom-current-group-alist)))
448 (run-hooks 'custom-define-hook)
449 symbol)
451 (defmacro defgroup (symbol members doc &rest args)
452 "Declare SYMBOL as a customization group containing MEMBERS.
453 SYMBOL does not need to be quoted.
455 Third argument DOC is the group documentation. This should be a short
456 description of the group, beginning with a capital and ending with
457 a period. Words other than the first should not be capitalized, if they
458 are not usually written so.
460 MEMBERS should be an alist of the form ((NAME WIDGET)...) where
461 NAME is a symbol and WIDGET is a widget for editing that symbol.
462 Useful widgets are `custom-variable' for editing variables,
463 `custom-face' for edit faces, and `custom-group' for editing groups.
465 The remaining arguments should have the form
467 [KEYWORD VALUE]...
469 For a list of valid keywords, see the common keywords listed in
470 `defcustom'.
472 See Info node `(elisp) Customization' in the Emacs Lisp manual
473 for more information."
474 (declare (doc-string 3))
475 ;; It is better not to use backquote in this file,
476 ;; because that makes a bootstrapping problem
477 ;; if you need to recompile all the Lisp files using interpreted code.
478 (nconc (list 'custom-declare-group (list 'quote symbol) members doc) args))
480 (defun custom-add-to-group (group option widget)
481 "To existing GROUP add a new OPTION of type WIDGET.
482 If there already is an entry for OPTION and WIDGET, nothing is done."
483 (let ((members (get group 'custom-group))
484 (entry (list option widget)))
485 (unless (member entry members)
486 (put group 'custom-group (nconc members (list entry))))))
488 (defun custom-group-of-mode (mode)
489 "Return the custom group corresponding to the major or minor MODE.
490 If no such group is found, return nil."
491 (or (get mode 'custom-mode-group)
492 (if (or (get mode 'custom-group)
493 (and (string-match "-mode\\'" (symbol-name mode))
494 (get (setq mode (intern (substring (symbol-name mode)
495 0 (match-beginning 0))))
496 'custom-group)))
497 mode)))
499 ;;; Properties.
501 (defun custom-handle-all-keywords (symbol args type)
502 "For customization option SYMBOL, handle keyword arguments ARGS.
503 Third argument TYPE is the custom option type."
504 (unless (memq :group args)
505 (custom-add-to-group (custom-current-group) symbol type))
506 (while args
507 (let ((arg (car args)))
508 (setq args (cdr args))
509 (unless (symbolp arg)
510 (error "Junk in args %S" args))
511 (let ((keyword arg)
512 (value (car args)))
513 (unless args
514 (error "Keyword %s is missing an argument" keyword))
515 (setq args (cdr args))
516 (custom-handle-keyword symbol keyword value type)))))
518 (defun custom-handle-keyword (symbol keyword value type)
519 "For customization option SYMBOL, handle KEYWORD with VALUE.
520 Fourth argument TYPE is the custom option type."
521 (if purify-flag
522 (setq value (purecopy value)))
523 (cond ((eq keyword :group)
524 (custom-add-to-group value symbol type))
525 ((eq keyword :version)
526 (custom-add-version symbol value))
527 ((eq keyword :package-version)
528 (custom-add-package-version symbol value))
529 ((eq keyword :link)
530 (custom-add-link symbol value))
531 ((eq keyword :load)
532 (custom-add-load symbol value))
533 ((eq keyword :tag)
534 (put symbol 'custom-tag value))
535 ((eq keyword :set-after)
536 (custom-add-dependencies symbol value))
538 (error "Unknown keyword %s" keyword))))
540 (defun custom-add-dependencies (symbol value)
541 "To the custom option SYMBOL, add dependencies specified by VALUE.
542 VALUE should be a list of symbols. For each symbol in that list,
543 this specifies that SYMBOL should be set after the specified symbol, if
544 both appear in constructs like `custom-set-variables'."
545 (unless (listp value)
546 (error "Invalid custom dependency `%s'" value))
547 (let* ((deps (get symbol 'custom-dependencies))
548 (new-deps deps))
549 (while value
550 (let ((dep (car value)))
551 (unless (symbolp dep)
552 (error "Invalid custom dependency `%s'" dep))
553 (unless (memq dep new-deps)
554 (setq new-deps (cons dep new-deps)))
555 (setq value (cdr value))))
556 (unless (eq deps new-deps)
557 (put symbol 'custom-dependencies new-deps))))
559 (defun custom-add-option (symbol option)
560 "To the variable SYMBOL add OPTION.
562 If SYMBOL's custom type is a hook, OPTION should be a hook member.
563 If SYMBOL's custom type is an alist, OPTION specifies a symbol
564 to offer to the user as a possible key in the alist.
565 For other custom types, this has no effect."
566 (let ((options (get symbol 'custom-options)))
567 (unless (member option options)
568 (put symbol 'custom-options (cons option options)))))
569 (defalias 'custom-add-frequent-value 'custom-add-option)
571 (defun custom-add-link (symbol widget)
572 "To the custom option SYMBOL add the link WIDGET."
573 (let ((links (get symbol 'custom-links)))
574 (unless (member widget links)
575 (put symbol 'custom-links (cons (purecopy widget) links)))))
577 (defun custom-add-version (symbol version)
578 "To the custom option SYMBOL add the version VERSION."
579 (put symbol 'custom-version (purecopy version)))
581 (defun custom-add-package-version (symbol version)
582 "To the custom option SYMBOL add the package version VERSION."
583 (put symbol 'custom-package-version (purecopy version)))
585 (defun custom-add-load (symbol load)
586 "To the custom option SYMBOL add the dependency LOAD.
587 LOAD should be either a library file name, or a feature name."
588 (let ((loads (get symbol 'custom-loads)))
589 (unless (member load loads)
590 (put symbol 'custom-loads (cons (purecopy load) loads)))))
592 (defun custom-autoload (symbol load &optional noset)
593 "Mark SYMBOL as autoloaded custom variable and add dependency LOAD.
594 If NOSET is non-nil, don't bother autoloading LOAD when setting the variable."
595 (put symbol 'custom-autoload (if noset 'noset t))
596 (custom-add-load symbol load))
598 ;; This test is also in the C code of `user-variable-p'.
599 (defun custom-variable-p (variable)
600 "Return non-nil if VARIABLE is a customizable variable.
601 A customizable variable is either (i) a variable whose property
602 list contains a non-nil `standard-value' or `custom-autoload'
603 property, or (ii) an alias for another customizable variable."
604 (setq variable (indirect-variable variable))
605 (or (get variable 'standard-value)
606 (get variable 'custom-autoload)))
608 (defun custom-note-var-changed (variable)
609 "Inform Custom that VARIABLE has been set (changed).
610 VARIABLE is a symbol that names a user option.
611 The result is that the change is treated as having been made through Custom."
612 (put variable 'customized-value (list (custom-quote (eval variable)))))
615 ;;; Custom Themes
617 ;;; Loading files needed to customize a symbol.
618 ;;; This is in custom.el because menu-bar.el needs it for toggle cmds.
620 (defvar custom-load-recursion nil
621 "Hack to avoid recursive dependencies.")
623 (defun custom-load-symbol (symbol)
624 "Load all dependencies for SYMBOL."
625 (unless custom-load-recursion
626 (let ((custom-load-recursion t))
627 ;; Load these files if not already done,
628 ;; to make sure we know all the dependencies of SYMBOL.
629 (condition-case nil
630 (require 'cus-load)
631 (error nil))
632 (condition-case nil
633 (require 'cus-start)
634 (error nil))
635 (dolist (load (get symbol 'custom-loads))
636 (cond ((symbolp load) (condition-case nil (require load) (error nil)))
637 ;; This is subsumed by the test below, but it's much faster.
638 ((assoc load load-history))
639 ;; This was just (assoc (locate-library load) load-history)
640 ;; but has been optimized not to load locate-library
641 ;; if not necessary.
642 ((let ((regexp (concat "\\(\\`\\|/\\)" (regexp-quote load)
643 "\\(\\'\\|\\.\\)"))
644 (found nil))
645 (dolist (loaded load-history)
646 (and (stringp (car loaded))
647 (string-match regexp (car loaded))
648 (setq found t)))
649 found))
650 ;; Without this, we would load cus-edit recursively.
651 ;; We are still loading it when we call this,
652 ;; and it is not in load-history yet.
653 ((equal load "cus-edit"))
654 (t (condition-case nil (load load) (error nil))))))))
656 (defvar custom-local-buffer nil
657 "Non-nil, in a Customization buffer, means customize a specific buffer.
658 If this variable is non-nil, it should be a buffer,
659 and it means customize the local bindings of that buffer.
660 This variable is a permanent local, and it normally has a local binding
661 in every Customization buffer.")
662 (put 'custom-local-buffer 'permanent-local t)
664 (defun custom-set-default (variable value)
665 "Default :set function for a customizable variable.
666 Normally, this sets the default value of VARIABLE to VALUE,
667 but if `custom-local-buffer' is non-nil,
668 this sets the local binding in that buffer instead."
669 (if custom-local-buffer
670 (with-current-buffer custom-local-buffer
671 (set variable value))
672 (set-default variable value)))
674 (defun custom-set-minor-mode (variable value)
675 ":set function for minor mode variables.
676 Normally, this sets the default value of VARIABLE to nil if VALUE
677 is nil and to t otherwise,
678 but if `custom-local-buffer' is non-nil,
679 this sets the local binding in that buffer instead."
680 (if custom-local-buffer
681 (with-current-buffer custom-local-buffer
682 (funcall variable (if value 1 0)))
683 (funcall variable (if value 1 0))))
685 (defun custom-quote (sexp)
686 "Quote SEXP if it is not self quoting."
687 (if (or (memq sexp '(t nil))
688 (keywordp sexp)
689 (and (listp sexp)
690 (memq (car sexp) '(lambda)))
691 (stringp sexp)
692 (numberp sexp)
693 (vectorp sexp)
694 ;;; (and (fboundp 'characterp)
695 ;;; (characterp sexp))
697 sexp
698 (list 'quote sexp)))
700 (defun customize-mark-to-save (symbol)
701 "Mark SYMBOL for later saving.
703 If the default value of SYMBOL is different from the standard value,
704 set the `saved-value' property to a list whose car evaluates to the
705 default value. Otherwise, set it to nil.
707 To actually save the value, call `custom-save-all'.
709 Return non-nil if the `saved-value' property actually changed."
710 (custom-load-symbol symbol)
711 (let* ((get (or (get symbol 'custom-get) 'default-value))
712 (value (funcall get symbol))
713 (saved (get symbol 'saved-value))
714 (standard (get symbol 'standard-value))
715 (comment (get symbol 'customized-variable-comment)))
716 ;; Save default value if different from standard value.
717 (if (or (null standard)
718 (not (equal value (condition-case nil
719 (eval (car standard))
720 (error nil)))))
721 (put symbol 'saved-value (list (custom-quote value)))
722 (put symbol 'saved-value nil))
723 ;; Clear customized information (set, but not saved).
724 (put symbol 'customized-value nil)
725 ;; Save any comment that might have been set.
726 (when comment
727 (put symbol 'saved-variable-comment comment))
728 (not (equal saved (get symbol 'saved-value)))))
730 (defun customize-mark-as-set (symbol)
731 "Mark current value of SYMBOL as being set from customize.
733 If the default value of SYMBOL is different from the saved value if any,
734 or else if it is different from the standard value, set the
735 `customized-value' property to a list whose car evaluates to the
736 default value. Otherwise, set it to nil.
738 Return non-nil if the `customized-value' property actually changed."
739 (custom-load-symbol symbol)
740 (let* ((get (or (get symbol 'custom-get) 'default-value))
741 (value (funcall get symbol))
742 (customized (get symbol 'customized-value))
743 (old (or (get symbol 'saved-value) (get symbol 'standard-value))))
744 ;; Mark default value as set if different from old value.
745 (if (not (and old
746 (equal value (condition-case nil
747 (eval (car old))
748 (error nil)))))
749 (progn (put symbol 'customized-value (list (custom-quote value)))
750 (custom-push-theme 'theme-value symbol 'user 'set
751 (custom-quote value)))
752 (put symbol 'customized-value nil))
753 ;; Changed?
754 (not (equal customized (get symbol 'customized-value)))))
756 (defun custom-reevaluate-setting (symbol)
757 "Reset the value of SYMBOL by re-evaluating its saved or standard value.
758 Use the :set function to do so. This is useful for customizable options
759 that are defined before their standard value can really be computed.
760 E.g. dumped variables whose default depends on run-time information."
761 (funcall (or (get symbol 'custom-set) 'set-default)
762 symbol
763 (eval (car (or (get symbol 'saved-value) (get symbol 'standard-value))))))
766 ;;; Custom Themes
768 ;; Custom themes are collections of settings that can be enabled or
769 ;; disabled as a unit.
771 ;; Each Custom theme is defined by a symbol, called the theme name.
772 ;; The `theme-settings' property of the theme name records the
773 ;; variable and face settings of the theme. This property is a list
774 ;; of elements, each of the form
776 ;; (PROP SYMBOL THEME VALUE)
778 ;; - PROP is either `theme-value' or `theme-face'
779 ;; - SYMBOL is the face or variable name
780 ;; - THEME is the theme name (redundant, but simplifies the code)
781 ;; - VALUE is an expression that gives the theme's setting for SYMBOL.
783 ;; The theme name also has a `theme-feature' property, whose value is
784 ;; specified when the theme is defined (see `custom-declare-theme').
785 ;; Usually, this is just a symbol named THEME-theme. This lets
786 ;; external libraries call (require 'foo-theme).
788 ;; In addition, each symbol (either a variable or a face) affected by
789 ;; an *enabled* theme has a `theme-value' or `theme-face' property,
790 ;; which is a list of elements each of the form
792 ;; (THEME VALUE)
794 ;; which have the same meanings as in `theme-settings'.
796 ;; The `theme-value' and `theme-face' lists are ordered by decreasing
797 ;; theme precedence. Thus, the first element is always the one that
798 ;; is in effect.
800 ;; Each theme is stored in a theme file, with filename THEME-theme.el.
801 ;; Loading a theme basically involves calling (load "THEME-theme")
802 ;; This is done by the function `load-theme'. Loading a theme
803 ;; automatically enables it.
805 ;; When a theme is enabled, the `theme-value' and `theme-face'
806 ;; properties for the affected symbols are set. When a theme is
807 ;; disabled, its settings are removed from the `theme-value' and
808 ;; `theme-face' properties, but the theme's own `theme-settings'
809 ;; property remains unchanged.
811 (defvar custom-known-themes '(user changed)
812 "Themes that have been defined with `deftheme'.
813 The default value is the list (user changed). The theme `changed'
814 contains the settings before custom themes are applied. The theme
815 `user' contains all the settings the user customized and saved.
816 Additional themes declared with the `deftheme' macro will be added
817 to the front of this list.")
819 (defsubst custom-theme-p (theme)
820 "Non-nil when THEME has been defined."
821 (memq theme custom-known-themes))
823 (defsubst custom-check-theme (theme)
824 "Check whether THEME is valid, and signal an error if it is not."
825 (unless (custom-theme-p theme)
826 (error "Unknown theme `%s'" theme)))
828 (defun custom-push-theme (prop symbol theme mode &optional value)
829 "Record VALUE for face or variable SYMBOL in custom theme THEME.
830 PROP is `theme-face' for a face, `theme-value' for a variable.
832 MODE can be either the symbol `set' or the symbol `reset'. If it is the
833 symbol `set', then VALUE is the value to use. If it is the symbol
834 `reset', then SYMBOL will be removed from THEME (VALUE is ignored).
836 See `custom-known-themes' for a list of known themes."
837 (unless (memq prop '(theme-value theme-face))
838 (error "Unknown theme property"))
839 (let* ((old (get symbol prop))
840 (setting (assq theme old)) ; '(theme value)
841 (theme-settings ; '(prop symbol theme value)
842 (get theme 'theme-settings)))
843 (cond
844 ;; Remove a setting:
845 ((eq mode 'reset)
846 (when setting
847 (let (res)
848 (dolist (theme-setting theme-settings)
849 (if (and (eq (car theme-setting) prop)
850 (eq (cadr theme-setting) symbol))
851 (setq res theme-setting)))
852 (put theme 'theme-settings (delq res theme-settings)))
853 (put symbol prop (delq setting old))))
854 ;; Alter an existing setting:
855 (setting
856 (let (res)
857 (dolist (theme-setting theme-settings)
858 (if (and (eq (car theme-setting) prop)
859 (eq (cadr theme-setting) symbol))
860 (setq res theme-setting)))
861 (put theme 'theme-settings
862 (cons (list prop symbol theme value)
863 (delq res theme-settings)))
864 (setcar (cdr setting) value)))
865 ;; Add a new setting:
867 (unless old
868 ;; If the user changed a variable outside of Customize, save
869 ;; the value to a fake theme, `changed'. If the theme is
870 ;; later disabled, we use this to bring back the old value.
872 ;; For faces, we just use `face-new-frame-defaults' to
873 ;; recompute when the theme is disabled.
874 (when (and (eq prop 'theme-value)
875 (boundp symbol))
876 (let ((sv (get symbol 'standard-value))
877 (val (symbol-value symbol)))
878 (unless (and sv (equal (eval (car sv)) val))
879 (setq old `((changed ,(custom-quote val))))))))
880 (put symbol prop (cons (list theme value) old))
881 (put theme 'theme-settings
882 (cons (list prop symbol theme value) theme-settings))))))
884 (defun custom-fix-face-spec (spec)
885 "Convert face SPEC, replacing obsolete :bold and :italic attributes.
886 Also change :reverse-video to :inverse-video."
887 (when (listp spec)
888 (if (or (memq :bold spec)
889 (memq :italic spec)
890 (memq :inverse-video spec))
891 (let (result)
892 (while spec
893 (let ((key (car spec))
894 (val (car (cdr spec))))
895 (cond ((eq key :italic)
896 (push :slant result)
897 (push (if val 'italic 'normal) result))
898 ((eq key :bold)
899 (push :weight result)
900 (push (if val 'bold 'normal) result))
901 ((eq key :reverse-video)
902 (push :inverse-video result)
903 (push val result))
905 (push key result)
906 (push val result))))
907 (setq spec (cddr spec)))
908 (nreverse result))
909 spec)))
911 (defun custom-set-variables (&rest args)
912 "Install user customizations of variable values specified in ARGS.
913 These settings are registered as theme `user'.
914 The arguments should each be a list of the form:
916 (SYMBOL EXP [NOW [REQUEST [COMMENT]]])
918 This stores EXP (without evaluating it) as the saved value for SYMBOL.
919 If NOW is present and non-nil, then also evaluate EXP and set
920 the default value for the SYMBOL to the value of EXP.
922 REQUEST is a list of features we must require in order to
923 handle SYMBOL properly.
924 COMMENT is a comment string about SYMBOL."
925 (apply 'custom-theme-set-variables 'user args))
927 (defun custom-theme-set-variables (theme &rest args)
928 "Initialize variables for theme THEME according to settings in ARGS.
929 Each of the arguments in ARGS should be a list of this form:
931 (SYMBOL EXP [NOW [REQUEST [COMMENT]]])
933 This stores EXP (without evaluating it) as the saved value for SYMBOL.
934 If NOW is present and non-nil, then also evaluate EXP and set
935 the default value for the SYMBOL to the value of EXP.
937 REQUEST is a list of features we must require in order to
938 handle SYMBOL properly.
939 COMMENT is a comment string about SYMBOL.
941 EXP itself is saved unevaluated as SYMBOL property `saved-value' and
942 in SYMBOL's list property `theme-value' \(using `custom-push-theme')."
943 (custom-check-theme theme)
945 ;; Process all the needed autoloads before anything else, so that the
946 ;; subsequent code has all the info it needs (e.g. which var corresponds
947 ;; to a minor mode), regardless of the ordering of the variables.
948 (dolist (entry args)
949 (let* ((symbol (indirect-variable (nth 0 entry))))
950 (unless (or (get symbol 'standard-value)
951 (memq (get symbol 'custom-autoload) '(nil noset)))
952 ;; This symbol needs to be autoloaded, even just for a `set'.
953 (custom-load-symbol symbol))))
955 ;; Move minor modes and variables with explicit requires to the end.
956 (setq args
957 (sort args
958 (lambda (a1 a2)
959 (let* ((sym1 (car a1))
960 (sym2 (car a2))
961 (1-then-2 (memq sym1 (get sym2 'custom-dependencies)))
962 (2-then-1 (memq sym2 (get sym1 'custom-dependencies))))
963 (cond ((and 1-then-2 2-then-1)
964 (error "Circular custom dependency between `%s' and `%s'"
965 sym1 sym2))
966 (2-then-1 nil)
967 ;; 1 is a dependency of 2, so needs to be set first.
968 (1-then-2)
969 ;; Put minor modes and symbols with :require last.
970 ;; Putting minor modes last ensures that the mode
971 ;; function will see other customized values rather
972 ;; than default values.
973 (t (or (nth 3 a2)
974 (eq (get sym2 'custom-set)
975 'custom-set-minor-mode))))))))
977 (dolist (entry args)
978 (unless (listp entry)
979 (error "Incompatible Custom theme spec"))
980 (let* ((symbol (indirect-variable (nth 0 entry)))
981 (value (nth 1 entry)))
982 (custom-push-theme 'theme-value symbol theme 'set value)
983 (unless custom--inhibit-theme-enable
984 ;; Now set the variable.
985 (let* ((now (nth 2 entry))
986 (requests (nth 3 entry))
987 (comment (nth 4 entry))
988 set)
989 (when requests
990 (put symbol 'custom-requests requests)
991 (mapc 'require requests))
992 (setq set (or (get symbol 'custom-set) 'custom-set-default))
993 (put symbol 'saved-value (list value))
994 (put symbol 'saved-variable-comment comment)
995 ;; Allow for errors in the case where the setter has
996 ;; changed between versions, say, but let the user know.
997 (condition-case data
998 (cond (now
999 ;; Rogue variable, set it now.
1000 (put symbol 'force-value t)
1001 (funcall set symbol (eval value)))
1002 ((default-boundp symbol)
1003 ;; Something already set this, overwrite it.
1004 (funcall set symbol (eval value))))
1005 (error
1006 (message "Error setting %s: %s" symbol data)))
1007 (and (or now (default-boundp symbol))
1008 (put symbol 'variable-comment comment)))))))
1011 ;;; Defining themes.
1013 ;; A theme file is named `THEME-theme.el' (where THEME is the theme
1014 ;; name) found in `custom-theme-load-path'. It has this format:
1016 ;; (deftheme THEME
1017 ;; DOCSTRING)
1019 ;; (custom-theme-set-variables
1020 ;; 'THEME
1021 ;; [THEME-VARIABLES])
1023 ;; (custom-theme-set-faces
1024 ;; 'THEME
1025 ;; [THEME-FACES])
1027 ;; (provide-theme 'THEME)
1030 ;; The IGNORED arguments to deftheme come from the XEmacs theme code, where
1031 ;; they were used to supply keyword-value pairs like `:immediate',
1032 ;; `:variable-reset-string', etc. We don't use any of these, so ignore them.
1034 (defmacro deftheme (theme &optional doc &rest ignored)
1035 "Declare THEME to be a Custom theme.
1036 The optional argument DOC is a doc string describing the theme.
1038 Any theme `foo' should be defined in a file called `foo-theme.el';
1039 see `custom-make-theme-feature' for more information."
1040 (let ((feature (custom-make-theme-feature theme)))
1041 ;; It is better not to use backquote in this file,
1042 ;; because that makes a bootstrapping problem
1043 ;; if you need to recompile all the Lisp files using interpreted code.
1044 (list 'custom-declare-theme (list 'quote theme) (list 'quote feature) doc)))
1046 (defun custom-declare-theme (theme feature &optional doc &rest ignored)
1047 "Like `deftheme', but THEME is evaluated as a normal argument.
1048 FEATURE is the feature this theme provides. Normally, this is a symbol
1049 created from THEME by `custom-make-theme-feature'."
1050 (unless (custom-theme-name-valid-p theme)
1051 (error "Custom theme cannot be named %S" theme))
1052 (add-to-list 'custom-known-themes theme)
1053 (put theme 'theme-feature feature)
1054 (when doc (put theme 'theme-documentation doc)))
1056 (defun custom-make-theme-feature (theme)
1057 "Given a symbol THEME, create a new symbol by appending \"-theme\".
1058 Store this symbol in the `theme-feature' property of THEME.
1059 Calling `provide-theme' to provide THEME actually puts `THEME-theme'
1060 into `features'.
1062 This allows for a file-name convention for autoloading themes:
1063 Every theme X has a property `provide-theme' whose value is \"X-theme\".
1064 \(load-theme X) then attempts to load the file `X-theme.el'."
1065 (intern (concat (symbol-name theme) "-theme")))
1067 ;;; Loading themes.
1069 (defcustom custom-theme-directory user-emacs-directory
1070 "Default user directory for storing custom theme files.
1071 The command `customize-create-theme' writes theme files into this
1072 directory. By default, Emacs searches for custom themes in this
1073 directory first---see `custom-theme-load-path'."
1074 :type 'string
1075 :group 'customize
1076 :version "22.1")
1078 (defcustom custom-theme-load-path (list 'custom-theme-directory t)
1079 "List of directories to search for custom theme files.
1080 When loading custom themes (e.g. in `customize-themes' and
1081 `load-theme'), Emacs searches for theme files in the specified
1082 order. Each element in the list should be one of the following:
1083 - the symbol `custom-theme-directory', meaning the value of
1084 `custom-theme-directory'.
1085 - the symbol t, meaning the built-in theme directory (a directory
1086 named \"themes\" in `data-directory').
1087 - a directory name (a string).
1089 Each theme file is named THEME-theme.el, where THEME is the theme
1090 name."
1091 :type '(repeat (choice (const :tag "custom-theme-directory"
1092 custom-theme-directory)
1093 (const :tag "Built-in theme directory" t)
1094 directory))
1095 :group 'customize
1096 :version "24.1")
1098 (defvar custom--inhibit-theme-enable nil
1099 "Whether the custom-theme-set-* functions act immediately.
1100 If nil, `custom-theme-set-variables' and `custom-theme-set-faces'
1101 change the current values of the given variable or face. If
1102 non-nil, they just make a record of the theme settings.")
1104 (defun provide-theme (theme)
1105 "Indicate that this file provides THEME.
1106 This calls `provide' to provide the feature name stored in THEME's
1107 property `theme-feature' (which is usually a symbol created by
1108 `custom-make-theme-feature')."
1109 (unless (custom-theme-name-valid-p theme)
1110 (error "Custom theme cannot be named %S" theme))
1111 (custom-check-theme theme)
1112 (provide (get theme 'theme-feature)))
1114 (defcustom custom-safe-themes '(default)
1115 "Themes that are considered safe to load.
1116 If the value is a list, each element should be either the SHA-256
1117 hash of a safe theme file, or the symbol `default', which stands
1118 for any theme in the built-in Emacs theme directory (a directory
1119 named \"themes\" in `data-directory').
1121 If the value is t, Emacs treats all themes as safe.
1123 This variable cannot be set in a Custom theme."
1124 :type '(choice (repeat :tag "List of safe themes"
1125 (choice string
1126 (const :tag "Built-in themes" default)))
1127 (const :tag "All themes" t))
1128 :group 'customize
1129 :risky t
1130 :version "24.1")
1132 (defun load-theme (theme &optional no-confirm no-enable)
1133 "Load Custom theme named THEME from its file.
1134 The theme file is named THEME-theme.el, in one of the directories
1135 specified by `custom-theme-load-path'.
1137 If optional arg NO-CONFIRM is non-nil, and THEME is not
1138 considered safe according to `custom-safe-themes', prompt the
1139 user for confirmation.
1141 Normally, this function also enables THEME; if optional arg
1142 NO-ENABLE is non-nil, load the theme but don't enable it.
1144 This function is normally called through Customize when setting
1145 `custom-enabled-themes'. If used directly in your init file, it
1146 should be called with a non-nil NO-CONFIRM argument, or after
1147 `custom-safe-themes' has been loaded.
1149 Return t if THEME was successfully loaded, nil otherwise."
1150 (interactive
1151 (list
1152 (intern (completing-read "Load custom theme: "
1153 (mapcar 'symbol-name
1154 (custom-available-themes))))
1155 nil nil))
1156 (unless (custom-theme-name-valid-p theme)
1157 (error "Invalid theme name `%s'" theme))
1158 ;; If reloading, clear out the old theme settings.
1159 (when (custom-theme-p theme)
1160 (disable-theme theme)
1161 (put theme 'theme-settings nil)
1162 (put theme 'theme-feature nil)
1163 (put theme 'theme-documentation nil))
1164 (let ((fn (locate-file (concat (symbol-name theme) "-theme.el")
1165 (custom-theme--load-path)
1166 '("" "c")))
1167 hash)
1168 (unless fn
1169 (error "Unable to find theme file for `%s'" theme))
1170 (with-temp-buffer
1171 (insert-file-contents fn)
1172 (setq hash (secure-hash 'sha256 (current-buffer)))
1173 ;; Check file safety with `custom-safe-themes', prompting the
1174 ;; user if necessary.
1175 (when (or no-confirm
1176 (eq custom-safe-themes t)
1177 (and (memq 'default custom-safe-themes)
1178 (equal (file-name-directory fn)
1179 (expand-file-name "themes/" data-directory)))
1180 (member hash custom-safe-themes)
1181 (custom-theme-load-confirm hash))
1182 (let ((custom--inhibit-theme-enable t))
1183 (eval-buffer))
1184 ;; Optimization: if the theme changes the `default' face, put that
1185 ;; entry first. This avoids some `frame-set-background-mode' rigmarole
1186 ;; by assigning the new background immediately.
1187 (let* ((settings (get theme 'theme-settings))
1188 (tail settings)
1189 found)
1190 (while (and tail (not found))
1191 (and (eq (nth 0 (car tail)) 'theme-face)
1192 (eq (nth 1 (car tail)) 'default)
1193 (setq found (car tail)))
1194 (setq tail (cdr tail)))
1195 (if found
1196 (put theme 'theme-settings (cons found (delq found settings)))))
1197 ;; Finally, enable the theme.
1198 (unless no-enable
1199 (enable-theme theme))
1200 t))))
1202 (defun custom-theme-load-confirm (hash)
1203 "Query the user about loading a Custom theme that may not be safe.
1204 The theme should be in the current buffer. If the user agrees,
1205 query also about adding HASH to `custom-safe-themes'."
1206 (if noninteractive
1208 (let ((exit-chars '(?y ?n ?\s))
1209 window prompt char)
1210 (save-window-excursion
1211 (rename-buffer "*Custom Theme*" t)
1212 (emacs-lisp-mode)
1213 (setq window (display-buffer (current-buffer)))
1214 (setq prompt
1215 (format "Loading a theme can run Lisp code. Really load?%s"
1216 (if (and window
1217 (< (line-number-at-pos (point-max))
1218 (window-body-height)))
1219 " (y or n) "
1220 (push ?\C-v exit-chars)
1221 "\nType y or n, or C-v to scroll: ")))
1222 (goto-char (point-min))
1223 (while (null char)
1224 (setq char (read-char-choice prompt exit-chars))
1225 (when (eq char ?\C-v)
1226 (if window
1227 (with-selected-window window
1228 (condition-case nil
1229 (scroll-up)
1230 (error (goto-char (point-min))))))
1231 (setq char nil)))
1232 (when (memq char '(?\s ?y))
1233 ;; Offer to save to `custom-safe-themes'.
1234 (and (or custom-file user-init-file)
1235 (y-or-n-p "Treat this theme as safe in future sessions? ")
1236 (customize-push-and-save 'custom-safe-themes (list hash)))
1237 t)))))
1239 (defun custom-theme-name-valid-p (name)
1240 "Return t if NAME is a valid name for a Custom theme, nil otherwise.
1241 NAME should be a symbol."
1242 (and (symbolp name)
1243 name
1244 (not (or (zerop (length (symbol-name name)))
1245 (eq name 'user)
1246 (eq name 'changed)))))
1248 (defun custom-available-themes ()
1249 "Return a list of available Custom themes (symbols)."
1250 (let (sym themes)
1251 (dolist (dir (custom-theme--load-path))
1252 (when (file-directory-p dir)
1253 (dolist (file (file-expand-wildcards
1254 (expand-file-name "*-theme.el" dir) t))
1255 (setq file (file-name-nondirectory file))
1256 (and (string-match "\\`\\(.+\\)-theme.el\\'" file)
1257 (setq sym (intern (match-string 1 file)))
1258 (custom-theme-name-valid-p sym)
1259 (push sym themes)))))
1260 (nreverse (delete-dups themes))))
1262 (defun custom-theme--load-path ()
1263 (let (lpath)
1264 (dolist (f custom-theme-load-path)
1265 (cond ((eq f 'custom-theme-directory)
1266 (setq f custom-theme-directory))
1267 ((eq f t)
1268 (setq f (expand-file-name "themes" data-directory))))
1269 (if (file-directory-p f)
1270 (push f lpath)))
1271 (nreverse lpath)))
1274 ;;; Enabling and disabling loaded themes.
1276 (defun enable-theme (theme)
1277 "Reenable all variable and face settings defined by THEME.
1278 THEME should be either `user', or a theme loaded via `load-theme'.
1279 After this function completes, THEME will have the highest
1280 precedence (after `user')."
1281 (interactive (list (intern
1282 (completing-read
1283 "Enable custom theme: "
1284 obarray (lambda (sym) (get sym 'theme-settings)) t))))
1285 (if (not (custom-theme-p theme))
1286 (error "Undefined Custom theme %s" theme))
1287 (let ((settings (get theme 'theme-settings)))
1288 ;; Loop through theme settings, recalculating vars/faces.
1289 (dolist (s settings)
1290 (let* ((prop (car s))
1291 (symbol (cadr s))
1292 (spec-list (get symbol prop)))
1293 (put symbol prop (cons (cddr s) (assq-delete-all theme spec-list)))
1294 (cond
1295 ((eq prop 'theme-face)
1296 (custom-theme-recalc-face symbol))
1297 ((eq prop 'theme-value)
1298 ;; Ignore `custom-enabled-themes' and `custom-safe-themes'.
1299 (unless (memq symbol '(custom-enabled-themes custom-safe-themes))
1300 (custom-theme-recalc-variable symbol)))))))
1301 (unless (eq theme 'user)
1302 (setq custom-enabled-themes
1303 (cons theme (delq theme custom-enabled-themes)))
1304 ;; Give the `user' theme the highest priority.
1305 (enable-theme 'user)))
1307 (defcustom custom-enabled-themes nil
1308 "List of enabled Custom Themes, highest precedence first.
1309 This list does not include the `user' theme, which is set by
1310 Customize and always takes precedence over other Custom Themes.
1312 This variable cannot be defined inside a Custom theme; there, it
1313 is simply ignored.
1315 Setting this variable through Customize calls `enable-theme' or
1316 `load-theme' for each theme in the list."
1317 :group 'customize
1318 :type '(repeat symbol)
1319 :set-after '(custom-theme-directory custom-theme-load-path
1320 custom-safe-themes)
1321 :risky t
1322 :set (lambda (symbol themes)
1323 (let (failures)
1324 (setq themes (delq 'user (delete-dups themes)))
1325 ;; Disable all themes not in THEMES.
1326 (if (boundp symbol)
1327 (dolist (theme (symbol-value symbol))
1328 (if (not (memq theme themes))
1329 (disable-theme theme))))
1330 ;; Call `enable-theme' or `load-theme' on each of THEMES.
1331 (dolist (theme (reverse themes))
1332 (condition-case nil
1333 (if (custom-theme-p theme)
1334 (enable-theme theme)
1335 (load-theme theme))
1336 (error (setq failures (cons theme failures)
1337 themes (delq theme themes)))))
1338 (enable-theme 'user)
1339 (custom-set-default symbol themes)
1340 (if failures
1341 (message "Failed to enable theme: %s"
1342 (mapconcat 'symbol-name failures ", "))))))
1344 (defsubst custom-theme-enabled-p (theme)
1345 "Return non-nil if THEME is enabled."
1346 (memq theme custom-enabled-themes))
1348 (defun disable-theme (theme)
1349 "Disable all variable and face settings defined by THEME.
1350 See `custom-enabled-themes' for a list of enabled themes."
1351 (interactive (list (intern
1352 (completing-read
1353 "Disable custom theme: "
1354 (mapcar 'symbol-name custom-enabled-themes)
1355 nil t))))
1356 (when (custom-theme-enabled-p theme)
1357 (let ((settings (get theme 'theme-settings)))
1358 (dolist (s settings)
1359 (let* ((prop (car s))
1360 (symbol (cadr s))
1361 (val (assq-delete-all theme (get symbol prop))))
1362 (put symbol prop val)
1363 (cond
1364 ((eq prop 'theme-value)
1365 (custom-theme-recalc-variable symbol))
1366 ((eq prop 'theme-face)
1367 ;; If the face spec specified by this theme is in the
1368 ;; saved-face property, reset that property.
1369 (when (equal (nth 3 s) (get symbol 'saved-face))
1370 (put symbol 'saved-face (and val (cadr (car val)))))))))
1371 ;; Recompute faces on all frames.
1372 (dolist (frame (frame-list))
1373 ;; We must reset the fg and bg color frame parameters, or
1374 ;; `face-set-after-frame-default' will use the existing
1375 ;; parameters, which could be from the disabled theme.
1376 (set-frame-parameter frame 'background-color
1377 (custom--frame-color-default
1378 frame :background "background" "Background"
1379 "unspecified-bg" "white"))
1380 (set-frame-parameter frame 'foreground-color
1381 (custom--frame-color-default
1382 frame :foreground "foreground" "Foreground"
1383 "unspecified-fg" "black"))
1384 (face-set-after-frame-default frame))
1385 (setq custom-enabled-themes
1386 (delq theme custom-enabled-themes)))))
1388 (defun custom--frame-color-default (frame attribute resource-attr resource-class
1389 tty-default x-default)
1390 (let ((col (face-attribute 'default attribute t)))
1391 (cond
1392 ((and col (not (eq col 'unspecified))) col)
1393 ((null (window-system frame)) tty-default)
1394 ((setq col (x-get-resource resource-attr resource-class)) col)
1395 (t x-default))))
1397 (defun custom-variable-theme-value (variable)
1398 "Return (list VALUE) indicating the custom theme value of VARIABLE.
1399 That is to say, it specifies what the value should be according to
1400 currently enabled custom themes.
1402 This function returns nil if no custom theme specifies a value for VARIABLE."
1403 (let ((theme-value (get variable 'theme-value)))
1404 (if theme-value
1405 (cdr (car theme-value)))))
1407 (defun custom-theme-recalc-variable (variable)
1408 "Set VARIABLE according to currently enabled custom themes."
1409 (let ((valspec (custom-variable-theme-value variable)))
1410 (if valspec
1411 (put variable 'saved-value valspec)
1412 (setq valspec (get variable 'standard-value)))
1413 (if (and valspec
1414 (or (get variable 'force-value)
1415 (default-boundp variable)))
1416 (funcall (or (get variable 'custom-set) 'set-default) variable
1417 (eval (car valspec))))))
1419 (defun custom-theme-recalc-face (face)
1420 "Set FACE according to currently enabled custom themes."
1421 (if (get face 'face-alias)
1422 (setq face (get face 'face-alias)))
1423 ;; Reset the faces for each frame.
1424 (dolist (frame (frame-list))
1425 (face-spec-recalc face frame)))
1428 ;;; XEmacs compatibility functions
1430 ;; In XEmacs, when you reset a Custom Theme, you have to specify the
1431 ;; theme to reset it to. We just apply the next available theme, so
1432 ;; just ignore the IGNORED arguments.
1434 (defun custom-theme-reset-variables (theme &rest args)
1435 "Reset some variable settings in THEME to their values in other themes.
1436 Each of the arguments ARGS has this form:
1438 (VARIABLE IGNORED)
1440 This means reset VARIABLE. (The argument IGNORED is ignored)."
1441 (custom-check-theme theme)
1442 (dolist (arg args)
1443 (custom-push-theme 'theme-value (car arg) theme 'reset)))
1445 (defun custom-reset-variables (&rest args)
1446 "Reset the specs of some variables to their values in other themes.
1447 This creates settings in the `user' theme.
1449 Each of the arguments ARGS has this form:
1451 (VARIABLE IGNORED)
1453 This means reset VARIABLE. (The argument IGNORED is ignored)."
1454 (apply 'custom-theme-reset-variables 'user args))
1456 ;;; The End.
1458 ;; Process the defcustoms for variables loaded before this file.
1459 (while custom-declare-variable-list
1460 (apply 'custom-declare-variable (car custom-declare-variable-list))
1461 (setq custom-declare-variable-list (cdr custom-declare-variable-list)))
1463 (provide 'custom)
1465 ;;; custom.el ends here