lisp/font-lock.el: Remove obsolete code.
[emacs.git] / lisp / custom.el
blob964d8d9ea4a274997dcd7f4ae2367a838c6db9ab
1 ;;; custom.el --- tools for declaring and initializing options
2 ;;
3 ;; Copyright (C) 1996-1997, 1999, 2001-2011 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 See `send-mail-function' in sendmail.el for an example."
125 ;; Until the var is actually initialized, it is kept unbound.
126 ;; This seemed to be at least as good as setting it to an arbitrary
127 ;; value like nil (evaluating `value' is not an option because it
128 ;; may have undesirable side-effects).
129 (push symbol custom-delayed-init-variables))
131 (defun custom-declare-variable (symbol default doc &rest args)
132 "Like `defcustom', but SYMBOL and DEFAULT are evaluated as normal arguments.
133 DEFAULT should be an expression to evaluate to compute the default value,
134 not the default value itself.
136 DEFAULT is stored as SYMBOL's standard value, in SYMBOL's property
137 `standard-value'. At the same time, SYMBOL's property `force-value' is
138 set to nil, as the value is no longer rogue."
139 (put symbol 'standard-value (purecopy (list default)))
140 ;; Maybe this option was rogue in an earlier version. It no longer is.
141 (when (get symbol 'force-value)
142 (put symbol 'force-value nil))
143 (if (keywordp doc)
144 (error "Doc string is missing"))
145 (let ((initialize 'custom-initialize-reset)
146 (requests nil))
147 (unless (memq :group args)
148 (custom-add-to-group (custom-current-group) symbol 'custom-variable))
149 (while args
150 (let ((arg (car args)))
151 (setq args (cdr args))
152 (unless (symbolp arg)
153 (error "Junk in args %S" args))
154 (let ((keyword arg)
155 (value (car args)))
156 (unless args
157 (error "Keyword %s is missing an argument" keyword))
158 (setq args (cdr args))
159 (cond ((eq keyword :initialize)
160 (setq initialize value))
161 ((eq keyword :set)
162 (put symbol 'custom-set value))
163 ((eq keyword :get)
164 (put symbol 'custom-get value))
165 ((eq keyword :require)
166 (push value requests))
167 ((eq keyword :risky)
168 (put symbol 'risky-local-variable value))
169 ((eq keyword :safe)
170 (put symbol 'safe-local-variable value))
171 ((eq keyword :type)
172 (put symbol 'custom-type (purecopy value)))
173 ((eq keyword :options)
174 (if (get symbol 'custom-options)
175 ;; Slow safe code to avoid duplicates.
176 (mapc (lambda (option)
177 (custom-add-option symbol option))
178 value)
179 ;; Fast code for the common case.
180 (put symbol 'custom-options (copy-sequence value))))
182 (custom-handle-keyword symbol keyword value
183 'custom-variable))))))
184 (put symbol 'custom-requests requests)
185 ;; Do the actual initialization.
186 (unless custom-dont-initialize
187 (funcall initialize symbol default)))
188 ;; Use defvar to set the docstring as well as the special-variable-p flag.
189 ;; FIXME: We should reproduce more of `defvar's behavior, such as the warning
190 ;; when the var is currently let-bound.
191 (if (not (default-boundp symbol))
192 ;; Don't use defvar to avoid setting a default-value when undesired.
193 (when doc (put symbol 'variable-documentation doc))
194 (eval `(defvar ,symbol nil ,@(when doc (list doc)))))
195 (push symbol current-load-list)
196 (run-hooks 'custom-define-hook)
197 symbol)
199 (defmacro defcustom (symbol value doc &rest args)
200 "Declare SYMBOL as a customizable variable that defaults to VALUE.
201 DOC is the variable documentation.
203 Neither SYMBOL nor VALUE need to be quoted.
204 If SYMBOL is not already bound, initialize it to VALUE.
205 The remaining arguments should have the form
207 [KEYWORD VALUE]...
209 The following keywords are meaningful:
211 :type VALUE should be a widget type for editing the symbol's value.
212 :options VALUE should be a list of valid members of the widget type.
213 :initialize
214 VALUE should be a function used to initialize the
215 variable. It takes two arguments, the symbol and value
216 given in the `defcustom' call. The default is
217 `custom-initialize-reset'.
218 :set VALUE should be a function to set the value of the symbol.
219 It takes two arguments, the symbol to set and the value to
220 give it. The default choice of function is `set-default'.
221 :get VALUE should be a function to extract the value of symbol.
222 The function takes one argument, a symbol, and should return
223 the current value for that symbol. The default choice of function
224 is `default-value'.
225 :require
226 VALUE should be a feature symbol. If you save a value
227 for this option, then when your `.emacs' file loads the value,
228 it does (require VALUE) first.
229 :risky Set SYMBOL's `risky-local-variable' property to VALUE.
230 :safe Set SYMBOL's `safe-local-variable' property to VALUE.
232 The following common keywords are also meaningful.
234 :group VALUE should be a customization group.
235 Add SYMBOL (or FACE with `defface') to that group.
236 :link LINK-DATA
237 Include an external link after the documentation string for this
238 item. This is a sentence containing an active field which
239 references some other documentation.
241 There are several alternatives you can use for LINK-DATA:
243 (custom-manual INFO-NODE)
244 Link to an Info node; INFO-NODE is a string which specifies
245 the node name, as in \"(emacs)Top\".
247 (info-link INFO-NODE)
248 Like `custom-manual' except that the link appears in the
249 customization buffer with the Info node name.
251 (url-link URL)
252 Link to a web page; URL is a string which specifies the URL.
254 (emacs-commentary-link LIBRARY)
255 Link to the commentary section of LIBRARY.
257 (emacs-library-link LIBRARY)
258 Link to an Emacs Lisp LIBRARY file.
260 (file-link FILE)
261 Link to FILE.
263 (function-link FUNCTION)
264 Link to the documentation of FUNCTION.
266 (variable-link VARIABLE)
267 Link to the documentation of VARIABLE.
269 (custom-group-link GROUP)
270 Link to another customization GROUP.
272 You can specify the text to use in the customization buffer by
273 adding `:tag NAME' after the first element of the LINK-DATA; for
274 example, (info-link :tag \"foo\" \"(emacs)Top\") makes a link to the
275 Emacs manual which appears in the buffer as `foo'.
277 An item can have more than one external link; however, most items
278 have none at all.
279 :version
280 VALUE should be a string specifying that the variable was
281 first introduced, or its default value was changed, in Emacs
282 version VERSION.
283 :package-version
284 VALUE should be a list with the form (PACKAGE . VERSION)
285 specifying that the variable was first introduced, or its
286 default value was changed, in PACKAGE version VERSION. This
287 keyword takes priority over :version. The PACKAGE and VERSION
288 must appear in the alist `customize-package-emacs-version-alist'.
289 Since PACKAGE must be unique and the user might see it in an
290 error message, a good choice is the official name of the
291 package, such as MH-E or Gnus.
292 :tag LABEL
293 Use LABEL, a string, instead of the item's name, to label the item
294 in customization menus and buffers.
295 :load FILE
296 Load file FILE (a string) before displaying this customization
297 item. Loading is done with `load', and only if the file is
298 not already loaded.
299 :set-after VARIABLES
300 Specifies that SYMBOL should be set after the list of variables
301 VARIABLES when both have been customized.
303 If SYMBOL has a local binding, then this form affects the local
304 binding. This is normally not what you want. Thus, if you need
305 to load a file defining variables with this form, or with
306 `defvar' or `defconst', you should always load that file
307 _outside_ any bindings for these variables. \(`defvar' and
308 `defconst' behave similarly in this respect.)
310 See Info node `(elisp) Customization' in the Emacs Lisp manual
311 for more information."
312 (declare (doc-string 3) (debug (name body)))
313 ;; It is better not to use backquote in this file,
314 ;; because that makes a bootstrapping problem
315 ;; if you need to recompile all the Lisp files using interpreted code.
316 (nconc (list 'custom-declare-variable
317 (list 'quote symbol)
318 (list 'quote value)
319 doc)
320 args))
322 ;;; The `defface' Macro.
324 (defmacro defface (face spec doc &rest args)
325 "Declare FACE as a customizable face that defaults to SPEC.
326 FACE does not need to be quoted.
328 Third argument DOC is the face documentation.
330 If FACE has been set with `custom-set-faces', set the face attributes
331 as specified by that function, otherwise set the face attributes
332 according to SPEC.
334 The remaining arguments should have the form
336 [KEYWORD VALUE]...
338 For a list of valid keywords, see the common keywords listed in
339 `defcustom'.
341 SPEC should be an alist of the form ((DISPLAY ATTS)...).
343 In the first element, DISPLAY can be `default'. The ATTS in that
344 element then act as defaults for all the following elements.
346 Aside from that, DISPLAY specifies conditions to match some or
347 all frames. For each frame, the first element of SPEC where the
348 DISPLAY conditions are satisfied is the one that applies to that
349 frame. The ATTRs in this element take effect, and the following
350 elements are ignored, on that frame.
352 In the last element, DISPLAY can be t. That element applies to a
353 frame if none of the previous elements (except the `default' if
354 any) did.
356 ATTS is a list of face attributes followed by their values:
357 (ATTR VALUE ATTR VALUE...)
359 The possible attributes are `:family', `:width', `:height', `:weight',
360 `:slant', `:underline', `:overline', `:strike-through', `:box',
361 `:foreground', `:background', `:stipple', `:inverse-video', and `:inherit'.
363 DISPLAY can be `default' (only in the first element), the symbol
364 t (only in the last element) to match all frames, or an alist of
365 conditions of the form \(REQ ITEM...). For such an alist to
366 match a frame, each of the conditions must be satisfied, meaning
367 that the REQ property of the frame must match one of the
368 corresponding ITEMs. These are the defined REQ values:
370 `type' (the value of `window-system')
371 Under X, in addition to the values `window-system' can take,
372 `motif', `lucid', `gtk' and `x-toolkit' are allowed, and match when
373 the Motif toolkit, Lucid toolkit, GTK toolkit or any X toolkit is in use.
375 `class' (the frame's color support)
376 Should be one of `color', `grayscale', or `mono'.
378 `background' (what color is used for the background text)
379 Should be one of `light' or `dark'.
381 `min-colors' (the minimum number of colors the frame should support)
382 Should be an integer, it is compared with the result of
383 `display-color-cells'.
385 `supports' (only match frames that support the specified face attributes)
386 Should be a list of face attributes. See the documentation for
387 the function `display-supports-face-attributes-p' for more
388 information on exactly how testing is done.
390 See Info node `(elisp) Customization' in the Emacs Lisp manual
391 for more information."
392 (declare (doc-string 3))
393 ;; It is better not to use backquote in this file,
394 ;; because that makes a bootstrapping problem
395 ;; if you need to recompile all the Lisp files using interpreted code.
396 (nconc (list 'custom-declare-face (list 'quote face) spec doc) args))
398 ;;; The `defgroup' Macro.
400 (defun custom-current-group ()
401 (cdr (assoc load-file-name custom-current-group-alist)))
403 (defun custom-declare-group (symbol members doc &rest args)
404 "Like `defgroup', but SYMBOL is evaluated as a normal argument."
405 (while members
406 (apply 'custom-add-to-group symbol (car members))
407 (setq members (cdr members)))
408 (when doc
409 ;; This text doesn't get into DOC.
410 (put symbol 'group-documentation (purecopy doc)))
411 (while args
412 (let ((arg (car args)))
413 (setq args (cdr args))
414 (unless (symbolp arg)
415 (error "Junk in args %S" args))
416 (let ((keyword arg)
417 (value (car args)))
418 (unless args
419 (error "Keyword %s is missing an argument" keyword))
420 (setq args (cdr args))
421 (cond ((eq keyword :prefix)
422 (put symbol 'custom-prefix (purecopy value)))
424 (custom-handle-keyword symbol keyword value
425 'custom-group))))))
426 ;; Record the group on the `current' list.
427 (let ((elt (assoc load-file-name custom-current-group-alist)))
428 (if elt (setcdr elt symbol)
429 (push (cons (purecopy load-file-name) symbol)
430 custom-current-group-alist)))
431 (run-hooks 'custom-define-hook)
432 symbol)
434 (defmacro defgroup (symbol members doc &rest args)
435 "Declare SYMBOL as a customization group containing MEMBERS.
436 SYMBOL does not need to be quoted.
438 Third argument DOC is the group documentation. This should be a short
439 description of the group, beginning with a capital and ending with
440 a period. Words other than the first should not be capitalized, if they
441 are not usually written so.
443 MEMBERS should be an alist of the form ((NAME WIDGET)...) where
444 NAME is a symbol and WIDGET is a widget for editing that symbol.
445 Useful widgets are `custom-variable' for editing variables,
446 `custom-face' for edit faces, and `custom-group' for editing groups.
448 The remaining arguments should have the form
450 [KEYWORD VALUE]...
452 For a list of valid keywords, see the common keywords listed in
453 `defcustom'.
455 See Info node `(elisp) Customization' in the Emacs Lisp manual
456 for more information."
457 (declare (doc-string 3))
458 ;; It is better not to use backquote in this file,
459 ;; because that makes a bootstrapping problem
460 ;; if you need to recompile all the Lisp files using interpreted code.
461 (nconc (list 'custom-declare-group (list 'quote symbol) members doc) args))
463 (defun custom-add-to-group (group option widget)
464 "To existing GROUP add a new OPTION of type WIDGET.
465 If there already is an entry for OPTION and WIDGET, nothing is done."
466 (let ((members (get group 'custom-group))
467 (entry (list option widget)))
468 (unless (member entry members)
469 (put group 'custom-group (nconc members (list entry))))))
471 (defun custom-group-of-mode (mode)
472 "Return the custom group corresponding to the major or minor MODE.
473 If no such group is found, return nil."
474 (or (get mode 'custom-mode-group)
475 (if (or (get mode 'custom-group)
476 (and (string-match "-mode\\'" (symbol-name mode))
477 (get (setq mode (intern (substring (symbol-name mode)
478 0 (match-beginning 0))))
479 'custom-group)))
480 mode)))
482 ;;; Properties.
484 (defun custom-handle-all-keywords (symbol args type)
485 "For customization option SYMBOL, handle keyword arguments ARGS.
486 Third argument TYPE is the custom option type."
487 (unless (memq :group args)
488 (custom-add-to-group (custom-current-group) symbol type))
489 (while args
490 (let ((arg (car args)))
491 (setq args (cdr args))
492 (unless (symbolp arg)
493 (error "Junk in args %S" args))
494 (let ((keyword arg)
495 (value (car args)))
496 (unless args
497 (error "Keyword %s is missing an argument" keyword))
498 (setq args (cdr args))
499 (custom-handle-keyword symbol keyword value type)))))
501 (defun custom-handle-keyword (symbol keyword value type)
502 "For customization option SYMBOL, handle KEYWORD with VALUE.
503 Fourth argument TYPE is the custom option type."
504 (if purify-flag
505 (setq value (purecopy value)))
506 (cond ((eq keyword :group)
507 (custom-add-to-group value symbol type))
508 ((eq keyword :version)
509 (custom-add-version symbol value))
510 ((eq keyword :package-version)
511 (custom-add-package-version symbol value))
512 ((eq keyword :link)
513 (custom-add-link symbol value))
514 ((eq keyword :load)
515 (custom-add-load symbol value))
516 ((eq keyword :tag)
517 (put symbol 'custom-tag value))
518 ((eq keyword :set-after)
519 (custom-add-dependencies symbol value))
521 (error "Unknown keyword %s" keyword))))
523 (defun custom-add-dependencies (symbol value)
524 "To the custom option SYMBOL, add dependencies specified by VALUE.
525 VALUE should be a list of symbols. For each symbol in that list,
526 this specifies that SYMBOL should be set after the specified symbol, if
527 both appear in constructs like `custom-set-variables'."
528 (unless (listp value)
529 (error "Invalid custom dependency `%s'" value))
530 (let* ((deps (get symbol 'custom-dependencies))
531 (new-deps deps))
532 (while value
533 (let ((dep (car value)))
534 (unless (symbolp dep)
535 (error "Invalid custom dependency `%s'" dep))
536 (unless (memq dep new-deps)
537 (setq new-deps (cons dep new-deps)))
538 (setq value (cdr value))))
539 (unless (eq deps new-deps)
540 (put symbol 'custom-dependencies new-deps))))
542 (defun custom-add-option (symbol option)
543 "To the variable SYMBOL add OPTION.
545 If SYMBOL's custom type is a hook, OPTION should be a hook member.
546 If SYMBOL's custom type is an alist, OPTION specifies a symbol
547 to offer to the user as a possible key in the alist.
548 For other custom types, this has no effect."
549 (let ((options (get symbol 'custom-options)))
550 (unless (member option options)
551 (put symbol 'custom-options (cons option options)))))
552 (defalias 'custom-add-frequent-value 'custom-add-option)
554 (defun custom-add-link (symbol widget)
555 "To the custom option SYMBOL add the link WIDGET."
556 (let ((links (get symbol 'custom-links)))
557 (unless (member widget links)
558 (put symbol 'custom-links (cons (purecopy widget) links)))))
560 (defun custom-add-version (symbol version)
561 "To the custom option SYMBOL add the version VERSION."
562 (put symbol 'custom-version (purecopy version)))
564 (defun custom-add-package-version (symbol version)
565 "To the custom option SYMBOL add the package version VERSION."
566 (put symbol 'custom-package-version (purecopy version)))
568 (defun custom-add-load (symbol load)
569 "To the custom option SYMBOL add the dependency LOAD.
570 LOAD should be either a library file name, or a feature name."
571 (let ((loads (get symbol 'custom-loads)))
572 (unless (member load loads)
573 (put symbol 'custom-loads (cons (purecopy load) loads)))))
575 (defun custom-autoload (symbol load &optional noset)
576 "Mark SYMBOL as autoloaded custom variable and add dependency LOAD.
577 If NOSET is non-nil, don't bother autoloading LOAD when setting the variable."
578 (put symbol 'custom-autoload (if noset 'noset t))
579 (custom-add-load symbol load))
581 ;; This test is also in the C code of `user-variable-p'.
582 (defun custom-variable-p (variable)
583 "Return non-nil if VARIABLE is a custom variable.
584 This recursively follows aliases."
585 (setq variable (indirect-variable variable))
586 (or (get variable 'standard-value)
587 (get variable 'custom-autoload)))
589 (defun custom-note-var-changed (variable)
590 "Inform Custom that VARIABLE has been set (changed).
591 VARIABLE is a symbol that names a user option.
592 The result is that the change is treated as having been made through Custom."
593 (put variable 'customized-value (list (custom-quote (eval variable)))))
596 ;;; Custom Themes
598 ;;; Loading files needed to customize a symbol.
599 ;;; This is in custom.el because menu-bar.el needs it for toggle cmds.
601 (defvar custom-load-recursion nil
602 "Hack to avoid recursive dependencies.")
604 (defun custom-load-symbol (symbol)
605 "Load all dependencies for SYMBOL."
606 (unless custom-load-recursion
607 (let ((custom-load-recursion t))
608 ;; Load these files if not already done,
609 ;; to make sure we know all the dependencies of SYMBOL.
610 (condition-case nil
611 (require 'cus-load)
612 (error nil))
613 (condition-case nil
614 (require 'cus-start)
615 (error nil))
616 (dolist (load (get symbol 'custom-loads))
617 (cond ((symbolp load) (condition-case nil (require load) (error nil)))
618 ;; This is subsumed by the test below, but it's much faster.
619 ((assoc load load-history))
620 ;; This was just (assoc (locate-library load) load-history)
621 ;; but has been optimized not to load locate-library
622 ;; if not necessary.
623 ((let ((regexp (concat "\\(\\`\\|/\\)" (regexp-quote load)
624 "\\(\\'\\|\\.\\)"))
625 (found nil))
626 (dolist (loaded load-history)
627 (and (stringp (car loaded))
628 (string-match regexp (car loaded))
629 (setq found t)))
630 found))
631 ;; Without this, we would load cus-edit recursively.
632 ;; We are still loading it when we call this,
633 ;; and it is not in load-history yet.
634 ((equal load "cus-edit"))
635 (t (condition-case nil (load load) (error nil))))))))
637 (defvar custom-local-buffer nil
638 "Non-nil, in a Customization buffer, means customize a specific buffer.
639 If this variable is non-nil, it should be a buffer,
640 and it means customize the local bindings of that buffer.
641 This variable is a permanent local, and it normally has a local binding
642 in every Customization buffer.")
643 (put 'custom-local-buffer 'permanent-local t)
645 (defun custom-set-default (variable value)
646 "Default :set function for a customizable variable.
647 Normally, this sets the default value of VARIABLE to VALUE,
648 but if `custom-local-buffer' is non-nil,
649 this sets the local binding in that buffer instead."
650 (if custom-local-buffer
651 (with-current-buffer custom-local-buffer
652 (set variable value))
653 (set-default variable value)))
655 (defun custom-set-minor-mode (variable value)
656 ":set function for minor mode variables.
657 Normally, this sets the default value of VARIABLE to nil if VALUE
658 is nil and to t otherwise,
659 but if `custom-local-buffer' is non-nil,
660 this sets the local binding in that buffer instead."
661 (if custom-local-buffer
662 (with-current-buffer custom-local-buffer
663 (funcall variable (if value 1 0)))
664 (funcall variable (if value 1 0))))
666 (defun custom-quote (sexp)
667 "Quote SEXP if it is not self quoting."
668 (if (or (memq sexp '(t nil))
669 (keywordp sexp)
670 (and (listp sexp)
671 (memq (car sexp) '(lambda)))
672 (stringp sexp)
673 (numberp sexp)
674 (vectorp sexp)
675 ;;; (and (fboundp 'characterp)
676 ;;; (characterp sexp))
678 sexp
679 (list 'quote sexp)))
681 (defun customize-mark-to-save (symbol)
682 "Mark SYMBOL for later saving.
684 If the default value of SYMBOL is different from the standard value,
685 set the `saved-value' property to a list whose car evaluates to the
686 default value. Otherwise, set it to nil.
688 To actually save the value, call `custom-save-all'.
690 Return non-nil if the `saved-value' property actually changed."
691 (custom-load-symbol symbol)
692 (let* ((get (or (get symbol 'custom-get) 'default-value))
693 (value (funcall get symbol))
694 (saved (get symbol 'saved-value))
695 (standard (get symbol 'standard-value))
696 (comment (get symbol 'customized-variable-comment)))
697 ;; Save default value if different from standard value.
698 (if (or (null standard)
699 (not (equal value (condition-case nil
700 (eval (car standard))
701 (error nil)))))
702 (put symbol 'saved-value (list (custom-quote value)))
703 (put symbol 'saved-value nil))
704 ;; Clear customized information (set, but not saved).
705 (put symbol 'customized-value nil)
706 ;; Save any comment that might have been set.
707 (when comment
708 (put symbol 'saved-variable-comment comment))
709 (not (equal saved (get symbol 'saved-value)))))
711 (defun customize-mark-as-set (symbol)
712 "Mark current value of SYMBOL as being set from customize.
714 If the default value of SYMBOL is different from the saved value if any,
715 or else if it is different from the standard value, set the
716 `customized-value' property to a list whose car evaluates to the
717 default value. Otherwise, set it to nil.
719 Return non-nil if the `customized-value' property actually changed."
720 (custom-load-symbol symbol)
721 (let* ((get (or (get symbol 'custom-get) 'default-value))
722 (value (funcall get symbol))
723 (customized (get symbol 'customized-value))
724 (old (or (get symbol 'saved-value) (get symbol 'standard-value))))
725 ;; Mark default value as set if different from old value.
726 (if (not (and old
727 (equal value (condition-case nil
728 (eval (car old))
729 (error nil)))))
730 (progn (put symbol 'customized-value (list (custom-quote value)))
731 (custom-push-theme 'theme-value symbol 'user 'set
732 (custom-quote value)))
733 (put symbol 'customized-value nil))
734 ;; Changed?
735 (not (equal customized (get symbol 'customized-value)))))
737 (defun custom-reevaluate-setting (symbol)
738 "Reset the value of SYMBOL by re-evaluating its saved or standard value.
739 Use the :set function to do so. This is useful for customizable options
740 that are defined before their standard value can really be computed.
741 E.g. dumped variables whose default depends on run-time information."
742 (funcall (or (get symbol 'custom-set) 'set-default)
743 symbol
744 (eval (car (or (get symbol 'saved-value) (get symbol 'standard-value))))))
747 ;;; Custom Themes
749 ;; Custom themes are collections of settings that can be enabled or
750 ;; disabled as a unit.
752 ;; Each Custom theme is defined by a symbol, called the theme name.
753 ;; The `theme-settings' property of the theme name records the
754 ;; variable and face settings of the theme. This property is a list
755 ;; of elements, each of the form
757 ;; (PROP SYMBOL THEME VALUE)
759 ;; - PROP is either `theme-value' or `theme-face'
760 ;; - SYMBOL is the face or variable name
761 ;; - THEME is the theme name (redundant, but simplifies the code)
762 ;; - VALUE is an expression that gives the theme's setting for SYMBOL.
764 ;; The theme name also has a `theme-feature' property, whose value is
765 ;; specified when the theme is defined (see `custom-declare-theme').
766 ;; Usually, this is just a symbol named THEME-theme. This lets
767 ;; external libraries call (require 'foo-theme).
769 ;; In addition, each symbol (either a variable or a face) affected by
770 ;; an *enabled* theme has a `theme-value' or `theme-face' property,
771 ;; which is a list of elements each of the form
773 ;; (THEME VALUE)
775 ;; which have the same meanings as in `theme-settings'.
777 ;; The `theme-value' and `theme-face' lists are ordered by decreasing
778 ;; theme precedence. Thus, the first element is always the one that
779 ;; is in effect.
781 ;; Each theme is stored in a theme file, with filename THEME-theme.el.
782 ;; Loading a theme basically involves calling (load "THEME-theme")
783 ;; This is done by the function `load-theme'. Loading a theme
784 ;; automatically enables it.
786 ;; When a theme is enabled, the `theme-value' and `theme-face'
787 ;; properties for the affected symbols are set. When a theme is
788 ;; disabled, its settings are removed from the `theme-value' and
789 ;; `theme-face' properties, but the theme's own `theme-settings'
790 ;; property remains unchanged.
792 (defvar custom-known-themes '(user changed)
793 "Themes that have been defined with `deftheme'.
794 The default value is the list (user changed). The theme `changed'
795 contains the settings before custom themes are applied. The theme
796 `user' contains all the settings the user customized and saved.
797 Additional themes declared with the `deftheme' macro will be added
798 to the front of this list.")
800 (defsubst custom-theme-p (theme)
801 "Non-nil when THEME has been defined."
802 (memq theme custom-known-themes))
804 (defsubst custom-check-theme (theme)
805 "Check whether THEME is valid, and signal an error if it is not."
806 (unless (custom-theme-p theme)
807 (error "Unknown theme `%s'" theme)))
809 (defun custom-push-theme (prop symbol theme mode &optional value)
810 "Record VALUE for face or variable SYMBOL in custom theme THEME.
811 PROP is `theme-face' for a face, `theme-value' for a variable.
813 MODE can be either the symbol `set' or the symbol `reset'. If it is the
814 symbol `set', then VALUE is the value to use. If it is the symbol
815 `reset', then SYMBOL will be removed from THEME (VALUE is ignored).
817 See `custom-known-themes' for a list of known themes."
818 (unless (memq prop '(theme-value theme-face))
819 (error "Unknown theme property"))
820 (let* ((old (get symbol prop))
821 (setting (assq theme old)) ; '(theme value)
822 (theme-settings ; '(prop symbol theme value)
823 (get theme 'theme-settings)))
824 (cond
825 ;; Remove a setting:
826 ((eq mode 'reset)
827 (when setting
828 (let (res)
829 (dolist (theme-setting theme-settings)
830 (if (and (eq (car theme-setting) prop)
831 (eq (cadr theme-setting) symbol))
832 (setq res theme-setting)))
833 (put theme 'theme-settings (delq res theme-settings)))
834 (put symbol prop (delq setting old))))
835 ;; Alter an existing setting:
836 (setting
837 (let (res)
838 (dolist (theme-setting theme-settings)
839 (if (and (eq (car theme-setting) prop)
840 (eq (cadr theme-setting) symbol))
841 (setq res theme-setting)))
842 (put theme 'theme-settings
843 (cons (list prop symbol theme value)
844 (delq res theme-settings)))
845 (setcar (cdr setting) value)))
846 ;; Add a new setting:
848 (unless old
849 ;; If the user changed the value outside of Customize, we
850 ;; first save the current value to a fake theme, `changed'.
851 ;; This ensures that the user-set value comes back if the
852 ;; theme is later disabled.
853 (cond ((and (eq prop 'theme-value)
854 (boundp symbol))
855 (let ((sv (get symbol 'standard-value))
856 (val (symbol-value symbol)))
857 (unless (and sv (equal (eval (car sv)) val))
858 (setq old `((changed ,(custom-quote val)))))))
859 ((and (facep symbol)
860 (not (face-attr-match-p
861 symbol
862 (custom-fix-face-spec
863 (face-spec-choose
864 (get symbol 'face-defface-spec))))))
865 (setq old `((changed
866 (,(append '(t) (custom-face-attributes-get
867 symbol nil)))))))))
868 (put symbol prop (cons (list theme value) old))
869 (put theme 'theme-settings
870 (cons (list prop symbol theme value) theme-settings))))))
872 (defun custom-fix-face-spec (spec)
873 "Convert face SPEC, replacing obsolete :bold and :italic attributes.
874 Also change :reverse-video to :inverse-video."
875 (when (listp spec)
876 (if (or (memq :bold spec)
877 (memq :italic spec)
878 (memq :inverse-video spec))
879 (let (result)
880 (while spec
881 (let ((key (car spec))
882 (val (car (cdr spec))))
883 (cond ((eq key :italic)
884 (push :slant result)
885 (push (if val 'italic 'normal) result))
886 ((eq key :bold)
887 (push :weight result)
888 (push (if val 'bold 'normal) result))
889 ((eq key :reverse-video)
890 (push :inverse-video result)
891 (push val result))
893 (push key result)
894 (push val result))))
895 (setq spec (cddr spec)))
896 (nreverse result))
897 spec)))
899 (defun custom-set-variables (&rest args)
900 "Install user customizations of variable values specified in ARGS.
901 These settings are registered as theme `user'.
902 The arguments should each be a list of the form:
904 (SYMBOL EXP [NOW [REQUEST [COMMENT]]])
906 This stores EXP (without evaluating it) as the saved value for SYMBOL.
907 If NOW is present and non-nil, then also evaluate EXP and set
908 the default value for the SYMBOL to the value of EXP.
910 REQUEST is a list of features we must require in order to
911 handle SYMBOL properly.
912 COMMENT is a comment string about SYMBOL."
913 (apply 'custom-theme-set-variables 'user args))
915 (defun custom-theme-set-variables (theme &rest args)
916 "Initialize variables for theme THEME according to settings in ARGS.
917 Each of the arguments in ARGS should be a list of this form:
919 (SYMBOL EXP [NOW [REQUEST [COMMENT]]])
921 This stores EXP (without evaluating it) as the saved value for SYMBOL.
922 If NOW is present and non-nil, then also evaluate EXP and set
923 the default value for the SYMBOL to the value of EXP.
925 REQUEST is a list of features we must require in order to
926 handle SYMBOL properly.
927 COMMENT is a comment string about SYMBOL.
929 EXP itself is saved unevaluated as SYMBOL property `saved-value' and
930 in SYMBOL's list property `theme-value' \(using `custom-push-theme')."
931 (custom-check-theme theme)
933 ;; Process all the needed autoloads before anything else, so that the
934 ;; subsequent code has all the info it needs (e.g. which var corresponds
935 ;; to a minor mode), regardless of the ordering of the variables.
936 (dolist (entry args)
937 (let* ((symbol (indirect-variable (nth 0 entry))))
938 (unless (or (get symbol 'standard-value)
939 (memq (get symbol 'custom-autoload) '(nil noset)))
940 ;; This symbol needs to be autoloaded, even just for a `set'.
941 (custom-load-symbol symbol))))
943 ;; Move minor modes and variables with explicit requires to the end.
944 (setq args
945 (sort args
946 (lambda (a1 a2)
947 (let* ((sym1 (car a1))
948 (sym2 (car a2))
949 (1-then-2 (memq sym1 (get sym2 'custom-dependencies)))
950 (2-then-1 (memq sym2 (get sym1 'custom-dependencies))))
951 (cond ((and 1-then-2 2-then-1)
952 (error "Circular custom dependency between `%s' and `%s'"
953 sym1 sym2))
954 (2-then-1 nil)
955 ;; 1 is a dependency of 2, so needs to be set first.
956 (1-then-2)
957 ;; Put minor modes and symbols with :require last.
958 ;; Putting minor modes last ensures that the mode
959 ;; function will see other customized values rather
960 ;; than default values.
961 (t (or (nth 3 a2)
962 (eq (get sym2 'custom-set)
963 'custom-set-minor-mode))))))))
965 (dolist (entry args)
966 (unless (listp entry)
967 (error "Incompatible Custom theme spec"))
968 (let* ((symbol (indirect-variable (nth 0 entry)))
969 (value (nth 1 entry)))
970 (custom-push-theme 'theme-value symbol theme 'set value)
971 (unless custom--inhibit-theme-enable
972 ;; Now set the variable.
973 (let* ((now (nth 2 entry))
974 (requests (nth 3 entry))
975 (comment (nth 4 entry))
976 set)
977 (when requests
978 (put symbol 'custom-requests requests)
979 (mapc 'require requests))
980 (setq set (or (get symbol 'custom-set) 'custom-set-default))
981 (put symbol 'saved-value (list value))
982 (put symbol 'saved-variable-comment comment)
983 ;; Allow for errors in the case where the setter has
984 ;; changed between versions, say, but let the user know.
985 (condition-case data
986 (cond (now
987 ;; Rogue variable, set it now.
988 (put symbol 'force-value t)
989 (funcall set symbol (eval value)))
990 ((default-boundp symbol)
991 ;; Something already set this, overwrite it.
992 (funcall set symbol (eval value))))
993 (error
994 (message "Error setting %s: %s" symbol data)))
995 (and (or now (default-boundp symbol))
996 (put symbol 'variable-comment comment)))))))
999 ;;; Defining themes.
1001 ;; A theme file is named `THEME-theme.el' (where THEME is the theme
1002 ;; name) found in `custom-theme-load-path'. It has this format:
1004 ;; (deftheme THEME
1005 ;; DOCSTRING)
1007 ;; (custom-theme-set-variables
1008 ;; 'THEME
1009 ;; [THEME-VARIABLES])
1011 ;; (custom-theme-set-faces
1012 ;; 'THEME
1013 ;; [THEME-FACES])
1015 ;; (provide-theme 'THEME)
1018 ;; The IGNORED arguments to deftheme come from the XEmacs theme code, where
1019 ;; they were used to supply keyword-value pairs like `:immediate',
1020 ;; `:variable-reset-string', etc. We don't use any of these, so ignore them.
1022 (defmacro deftheme (theme &optional doc &rest ignored)
1023 "Declare THEME to be a Custom theme.
1024 The optional argument DOC is a doc string describing the theme.
1026 Any theme `foo' should be defined in a file called `foo-theme.el';
1027 see `custom-make-theme-feature' for more information."
1028 (let ((feature (custom-make-theme-feature theme)))
1029 ;; It is better not to use backquote in this file,
1030 ;; because that makes a bootstrapping problem
1031 ;; if you need to recompile all the Lisp files using interpreted code.
1032 (list 'custom-declare-theme (list 'quote theme) (list 'quote feature) doc)))
1034 (defun custom-declare-theme (theme feature &optional doc &rest ignored)
1035 "Like `deftheme', but THEME is evaluated as a normal argument.
1036 FEATURE is the feature this theme provides. Normally, this is a symbol
1037 created from THEME by `custom-make-theme-feature'."
1038 (unless (custom-theme-name-valid-p theme)
1039 (error "Custom theme cannot be named %S" theme))
1040 (add-to-list 'custom-known-themes theme)
1041 (put theme 'theme-feature feature)
1042 (when doc (put theme 'theme-documentation doc)))
1044 (defun custom-make-theme-feature (theme)
1045 "Given a symbol THEME, create a new symbol by appending \"-theme\".
1046 Store this symbol in the `theme-feature' property of THEME.
1047 Calling `provide-theme' to provide THEME actually puts `THEME-theme'
1048 into `features'.
1050 This allows for a file-name convention for autoloading themes:
1051 Every theme X has a property `provide-theme' whose value is \"X-theme\".
1052 \(load-theme X) then attempts to load the file `X-theme.el'."
1053 (intern (concat (symbol-name theme) "-theme")))
1055 ;;; Loading themes.
1057 (defcustom custom-theme-directory user-emacs-directory
1058 "Default user directory for storing custom theme files.
1059 The command `customize-create-theme' writes theme files into this
1060 directory. By default, Emacs searches for custom themes in this
1061 directory first---see `custom-theme-load-path'."
1062 :type 'string
1063 :group 'customize
1064 :version "22.1")
1066 (defcustom custom-theme-load-path (list 'custom-theme-directory t)
1067 "List of directories to search for custom theme files.
1068 When loading custom themes (e.g. in `customize-themes' and
1069 `load-theme'), Emacs searches for theme files in the specified
1070 order. Each element in the list should be one of the following:
1071 - the symbol `custom-theme-directory', meaning the value of
1072 `custom-theme-directory'.
1073 - the symbol t, meaning the built-in theme directory (a directory
1074 named \"themes\" in `data-directory').
1075 - a directory name (a string).
1077 Each theme file is named THEME-theme.el, where THEME is the theme
1078 name."
1079 :type '(repeat (choice (const :tag "custom-theme-directory"
1080 custom-theme-directory)
1081 (const :tag "Built-in theme directory" t)
1082 directory))
1083 :group 'customize
1084 :version "24.1")
1086 (defvar custom--inhibit-theme-enable nil
1087 "Whether the custom-theme-set-* functions act immediately.
1088 If nil, `custom-theme-set-variables' and `custom-theme-set-faces'
1089 change the current values of the given variable or face. If
1090 non-nil, they just make a record of the theme settings.")
1092 (defun provide-theme (theme)
1093 "Indicate that this file provides THEME.
1094 This calls `provide' to provide the feature name stored in THEME's
1095 property `theme-feature' (which is usually a symbol created by
1096 `custom-make-theme-feature')."
1097 (unless (custom-theme-name-valid-p theme)
1098 (error "Custom theme cannot be named %S" theme))
1099 (custom-check-theme theme)
1100 (provide (get theme 'theme-feature)))
1102 (defcustom custom-safe-themes '(default)
1103 "List of themes that are considered safe to load.
1104 Each list element should be the `sha1' hash of a theme file, or
1105 the symbol `default', which stands for any theme in the built-in
1106 Emacs theme directory (a directory named \"themes\" in
1107 `data-directory')."
1108 :type '(repeat
1109 (choice string (const :tag "Built-in themes" default)))
1110 :group 'customize
1111 :risky t
1112 :version "24.1")
1114 (defun load-theme (theme &optional no-enable)
1115 "Load Custom theme named THEME from its file.
1116 Normally, this also enables THEME. If optional arg NO-ENABLE is
1117 non-nil, load THEME but don't enable it.
1119 The theme file is named THEME-theme.el, in one of the directories
1120 specified by `custom-theme-load-path'.
1122 Return t if THEME was successfully loaded, nil otherwise."
1123 (interactive
1124 (list
1125 (intern (completing-read "Load custom theme: "
1126 (mapcar 'symbol-name
1127 (custom-available-themes))))))
1128 (unless (custom-theme-name-valid-p theme)
1129 (error "Invalid theme name `%s'" theme))
1130 ;; If reloading, clear out the old theme settings.
1131 (when (custom-theme-p theme)
1132 (disable-theme theme)
1133 (put theme 'theme-settings nil)
1134 (put theme 'theme-feature nil)
1135 (put theme 'theme-documentation nil))
1136 (let ((fn (locate-file (concat (symbol-name theme) "-theme.el")
1137 (custom-theme--load-path)
1138 '("" "c")))
1139 hash)
1140 (unless fn
1141 (error "Unable to find theme file for `%s'" theme))
1142 (with-temp-buffer
1143 (insert-file-contents fn)
1144 (setq hash (sha1 (current-buffer)))
1145 ;; Check file safety with `custom-safe-themes', prompting the
1146 ;; user if necessary.
1147 (when (or (and (memq 'default custom-safe-themes)
1148 (equal (file-name-directory fn)
1149 (expand-file-name "themes/" data-directory)))
1150 (member hash custom-safe-themes)
1151 (custom-theme-load-confirm hash))
1152 (let ((custom--inhibit-theme-enable t))
1153 (eval-buffer))
1154 ;; Optimization: if the theme changes the `default' face, put that
1155 ;; entry first. This avoids some `frame-set-background-mode' rigmarole
1156 ;; by assigning the new background immediately.
1157 (let* ((settings (get theme 'theme-settings))
1158 (tail settings)
1159 found)
1160 (while (and tail (not found))
1161 (and (eq (nth 0 (car tail)) 'theme-face)
1162 (eq (nth 1 (car tail)) 'default)
1163 (setq found (car tail)))
1164 (setq tail (cdr tail)))
1165 (if found
1166 (put theme 'theme-settings (cons found (delq found settings)))))
1167 ;; Finally, enable the theme.
1168 (unless no-enable
1169 (enable-theme theme))
1170 t))))
1172 (defun custom-theme-load-confirm (hash)
1173 "Query the user about loading a Custom theme that may not be safe.
1174 The theme should be in the current buffer. If the user agrees,
1175 query also about adding HASH to `custom-safe-themes'."
1176 (if noninteractive
1178 (let ((exit-chars '(?y ?n ?\s))
1179 window prompt char)
1180 (save-window-excursion
1181 (rename-buffer "*Custom Theme*" t)
1182 (emacs-lisp-mode)
1183 (setq window (display-buffer (current-buffer)))
1184 (setq prompt
1185 (format "Loading a theme can run Lisp code. Really load?%s"
1186 (if (and window
1187 (< (line-number-at-pos (point-max))
1188 (window-body-height)))
1189 " (y or n) "
1190 (push ?\C-v exit-chars)
1191 "\nType y or n, or C-v to scroll: ")))
1192 (goto-char (point-min))
1193 (while (null char)
1194 (setq char (read-char-choice prompt exit-chars))
1195 (when (eq char ?\C-v)
1196 (if window
1197 (with-selected-window window
1198 (condition-case nil
1199 (scroll-up)
1200 (error (goto-char (point-min))))))
1201 (setq char nil)))
1202 (when (memq char '(?\s ?y))
1203 ;; Offer to save to `custom-safe-themes'.
1204 (and (or custom-file user-init-file)
1205 (y-or-n-p "Treat this theme as safe in future sessions? ")
1206 (let ((coding-system-for-read nil))
1207 (push hash custom-safe-themes)
1208 (customize-save-variable 'custom-safe-themes
1209 custom-safe-themes)))
1210 t)))))
1212 (defun custom-theme-name-valid-p (name)
1213 "Return t if NAME is a valid name for a Custom theme, nil otherwise.
1214 NAME should be a symbol."
1215 (and (symbolp name)
1216 name
1217 (not (or (zerop (length (symbol-name name)))
1218 (eq name 'user)
1219 (eq name 'changed)))))
1221 (defun custom-available-themes ()
1222 "Return a list of available Custom themes (symbols)."
1223 (let (sym themes)
1224 (dolist (dir (custom-theme--load-path))
1225 (when (file-directory-p dir)
1226 (dolist (file (file-expand-wildcards
1227 (expand-file-name "*-theme.el" dir) t))
1228 (setq file (file-name-nondirectory file))
1229 (and (string-match "\\`\\(.+\\)-theme.el\\'" file)
1230 (setq sym (intern (match-string 1 file)))
1231 (custom-theme-name-valid-p sym)
1232 (push sym themes)))))
1233 (nreverse (delete-dups themes))))
1235 (defun custom-theme--load-path ()
1236 (let (lpath)
1237 (dolist (f custom-theme-load-path)
1238 (cond ((eq f 'custom-theme-directory)
1239 (setq f custom-theme-directory))
1240 ((eq f t)
1241 (setq f (expand-file-name "themes" data-directory))))
1242 (if (file-directory-p f)
1243 (push f lpath)))
1244 (nreverse lpath)))
1247 ;;; Enabling and disabling loaded themes.
1249 (defun enable-theme (theme)
1250 "Reenable all variable and face settings defined by THEME.
1251 THEME should be either `user', or a theme loaded via `load-theme'.
1252 After this function completes, THEME will have the highest
1253 precedence (after `user')."
1254 (interactive (list (intern
1255 (completing-read
1256 "Enable custom theme: "
1257 obarray (lambda (sym) (get sym 'theme-settings)) t))))
1258 (if (not (custom-theme-p theme))
1259 (error "Undefined Custom theme %s" theme))
1260 (let ((settings (get theme 'theme-settings)))
1261 ;; Loop through theme settings, recalculating vars/faces.
1262 (dolist (s settings)
1263 (let* ((prop (car s))
1264 (symbol (cadr s))
1265 (spec-list (get symbol prop)))
1266 (put symbol prop (cons (cddr s) (assq-delete-all theme spec-list)))
1267 (cond
1268 ((eq prop 'theme-face)
1269 (custom-theme-recalc-face symbol))
1270 ((eq prop 'theme-value)
1271 ;; Don't change `custom-enabled-themes'; that's special.
1272 (unless (eq symbol 'custom-enabled-themes)
1273 (custom-theme-recalc-variable symbol)))))))
1274 (unless (eq theme 'user)
1275 (setq custom-enabled-themes
1276 (cons theme (delq theme custom-enabled-themes)))
1277 ;; Give the `user' theme the highest priority.
1278 (enable-theme 'user)))
1280 (defcustom custom-enabled-themes nil
1281 "List of enabled Custom Themes, highest precedence first.
1282 This list does not include the `user' theme, which is set by
1283 Customize and always takes precedence over other Custom Themes.
1285 This variable cannot be defined inside a Custom theme; there, it
1286 is simply ignored."
1287 :group 'customize
1288 :type '(repeat symbol)
1289 :set-after '(custom-theme-directory custom-theme-load-path
1290 custom-safe-themes)
1291 :risky t
1292 :set (lambda (symbol themes)
1293 (let (failures)
1294 (setq themes (delq 'user (delete-dups themes)))
1295 ;; Disable all themes not in THEMES.
1296 (if (boundp symbol)
1297 (dolist (theme (symbol-value symbol))
1298 (if (not (memq theme themes))
1299 (disable-theme theme))))
1300 ;; Call `enable-theme' or `load-theme' on each of THEMES.
1301 (dolist (theme (reverse themes))
1302 (condition-case nil
1303 (if (custom-theme-p theme)
1304 (enable-theme theme)
1305 (load-theme theme))
1306 (error (setq failures (cons theme failures)
1307 themes (delq theme themes)))))
1308 (enable-theme 'user)
1309 (custom-set-default symbol themes)
1310 (if failures
1311 (message "Failed to enable theme: %s"
1312 (mapconcat 'symbol-name failures ", "))))))
1314 (defsubst custom-theme-enabled-p (theme)
1315 "Return non-nil if THEME is enabled."
1316 (memq theme custom-enabled-themes))
1318 (defun disable-theme (theme)
1319 "Disable all variable and face settings defined by THEME.
1320 See `custom-enabled-themes' for a list of enabled themes."
1321 (interactive (list (intern
1322 (completing-read
1323 "Disable custom theme: "
1324 (mapcar 'symbol-name custom-enabled-themes)
1325 nil t))))
1326 (when (custom-theme-enabled-p theme)
1327 (let ((settings (get theme 'theme-settings)))
1328 (dolist (s settings)
1329 (let* ((prop (car s))
1330 (symbol (cadr s))
1331 (val (assq-delete-all theme (get symbol prop))))
1332 (put symbol prop val)
1333 (cond
1334 ((eq prop 'theme-value)
1335 (custom-theme-recalc-variable symbol))
1336 ((eq prop 'theme-face)
1337 ;; If the face spec specified by this theme is in the
1338 ;; saved-face property, reset that property.
1339 (when (equal (nth 3 s) (get symbol 'saved-face))
1340 (put symbol 'saved-face (and val (cadr (car val)))))
1341 (custom-theme-recalc-face symbol)))))
1342 (setq custom-enabled-themes
1343 (delq theme custom-enabled-themes)))))
1345 (defun custom-variable-theme-value (variable)
1346 "Return (list VALUE) indicating the custom theme value of VARIABLE.
1347 That is to say, it specifies what the value should be according to
1348 currently enabled custom themes.
1350 This function returns nil if no custom theme specifies a value for VARIABLE."
1351 (let ((theme-value (get variable 'theme-value)))
1352 (if theme-value
1353 (cdr (car theme-value)))))
1355 (defun custom-theme-recalc-variable (variable)
1356 "Set VARIABLE according to currently enabled custom themes."
1357 (let ((valspec (custom-variable-theme-value variable)))
1358 (if valspec
1359 (put variable 'saved-value valspec)
1360 (setq valspec (get variable 'standard-value)))
1361 (if (and valspec
1362 (or (get variable 'force-value)
1363 (default-boundp variable)))
1364 (funcall (or (get variable 'custom-set) 'set-default) variable
1365 (eval (car valspec))))))
1367 (defun custom-theme-recalc-face (face)
1368 "Set FACE according to currently enabled custom themes."
1369 (if (get face 'face-alias)
1370 (setq face (get face 'face-alias)))
1371 ;; Reset the faces for each frame.
1372 (dolist (frame (frame-list))
1373 (face-spec-recalc face frame)))
1376 ;;; XEmacs compability functions
1378 ;; In XEmacs, when you reset a Custom Theme, you have to specify the
1379 ;; theme to reset it to. We just apply the next available theme, so
1380 ;; just ignore the IGNORED arguments.
1382 (defun custom-theme-reset-variables (theme &rest args)
1383 "Reset some variable settings in THEME to their values in other themes.
1384 Each of the arguments ARGS has this form:
1386 (VARIABLE IGNORED)
1388 This means reset VARIABLE. (The argument IGNORED is ignored)."
1389 (custom-check-theme theme)
1390 (dolist (arg args)
1391 (custom-push-theme 'theme-value (car arg) theme 'reset)))
1393 (defun custom-reset-variables (&rest args)
1394 "Reset the specs of some variables to their values in other themes.
1395 This creates settings in the `user' theme.
1397 Each of the arguments ARGS has this form:
1399 (VARIABLE IGNORED)
1401 This means reset VARIABLE. (The argument IGNORED is ignored)."
1402 (apply 'custom-theme-reset-variables 'user args))
1404 ;;; The End.
1406 ;; Process the defcustoms for variables loaded before this file.
1407 (while custom-declare-variable-list
1408 (apply 'custom-declare-variable (car custom-declare-variable-list))
1409 (setq custom-declare-variable-list (cdr custom-declare-variable-list)))
1411 (provide 'custom)
1413 ;;; custom.el ends here