Fix MS-DOS build in lib-src.
[emacs.git] / lisp / custom.el
blobd9cba2704d8a0157fb689ec6c286a3d1164f47dd
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 value doc &rest args)
202 "Declare SYMBOL as a customizable variable that defaults to VALUE.
203 DOC is the variable documentation.
205 Neither SYMBOL nor VALUE need to be quoted.
206 If SYMBOL is not already bound, initialize it to VALUE.
207 The remaining arguments should have the form
209 [KEYWORD VALUE]...
211 The following keywords are meaningful:
213 :type VALUE should be a widget type for editing the symbol's value.
214 :options VALUE should be a list of valid members of the widget type.
215 :initialize
216 VALUE should be a function used to initialize the
217 variable. It takes two arguments, the symbol and value
218 given in the `defcustom' call. The default is
219 `custom-initialize-reset'.
220 :set VALUE should be a function to set the value of the symbol
221 when using the Customize user interface.
222 It takes two arguments, the symbol to set and the value to
223 give it. The default choice of function is `set-default'.
224 :get VALUE should be a function to extract the value of symbol.
225 The function takes one argument, a symbol, and should return
226 the current value for that symbol. The default choice of function
227 is `default-value'.
228 :require
229 VALUE should be a feature symbol. If you save a value
230 for this option, then when your `.emacs' file loads the value,
231 it does (require VALUE) first.
232 :risky Set SYMBOL's `risky-local-variable' property to VALUE.
233 :safe Set SYMBOL's `safe-local-variable' property to VALUE.
234 See Info node `(elisp) File Local Variables'.
236 The following common keywords are also meaningful.
238 :group VALUE should be a customization group.
239 Add SYMBOL (or FACE with `defface') to that group.
240 :link LINK-DATA
241 Include an external link after the documentation string for this
242 item. This is a sentence containing an active field which
243 references some other documentation.
245 There are several alternatives you can use for LINK-DATA:
247 (custom-manual INFO-NODE)
248 Link to an Info node; INFO-NODE is a string which specifies
249 the node name, as in \"(emacs)Top\".
251 (info-link INFO-NODE)
252 Like `custom-manual' except that the link appears in the
253 customization buffer with the Info node name.
255 (url-link URL)
256 Link to a web page; URL is a string which specifies the URL.
258 (emacs-commentary-link LIBRARY)
259 Link to the commentary section of LIBRARY.
261 (emacs-library-link LIBRARY)
262 Link to an Emacs Lisp LIBRARY file.
264 (file-link FILE)
265 Link to FILE.
267 (function-link FUNCTION)
268 Link to the documentation of FUNCTION.
270 (variable-link VARIABLE)
271 Link to the documentation of VARIABLE.
273 (custom-group-link GROUP)
274 Link to another customization GROUP.
276 You can specify the text to use in the customization buffer by
277 adding `:tag NAME' after the first element of the LINK-DATA; for
278 example, (info-link :tag \"foo\" \"(emacs)Top\") makes a link to the
279 Emacs manual which appears in the buffer as `foo'.
281 An item can have more than one external link; however, most items
282 have none at all.
283 :version
284 VALUE should be a string specifying that the variable was
285 first introduced, or its default value was changed, in Emacs
286 version VERSION.
287 :package-version
288 VALUE should be a list with the form (PACKAGE . VERSION)
289 specifying that the variable was first introduced, or its
290 default value was changed, in PACKAGE version VERSION. This
291 keyword takes priority over :version. The PACKAGE and VERSION
292 must appear in the alist `customize-package-emacs-version-alist'.
293 Since PACKAGE must be unique and the user might see it in an
294 error message, a good choice is the official name of the
295 package, such as MH-E or Gnus.
296 :tag LABEL
297 Use LABEL, a string, instead of the item's name, to label the item
298 in customization menus and buffers.
299 :load FILE
300 Load file FILE (a string) before displaying this customization
301 item. Loading is done with `load', and only if the file is
302 not already loaded.
303 :set-after VARIABLES
304 Specifies that SYMBOL should be set after the list of variables
305 VARIABLES when both have been customized.
307 If SYMBOL has a local binding, then this form affects the local
308 binding. This is normally not what you want. Thus, if you need
309 to load a file defining variables with this form, or with
310 `defvar' or `defconst', you should always load that file
311 _outside_ any bindings for these variables. \(`defvar' and
312 `defconst' behave similarly in this respect.)
314 See Info node `(elisp) Customization' in the Emacs Lisp manual
315 for more information."
316 (declare (doc-string 3) (debug (name body)))
317 ;; It is better not to use backquote in this file,
318 ;; because that makes a bootstrapping problem
319 ;; if you need to recompile all the Lisp files using interpreted code.
320 `(custom-declare-variable
321 ',symbol
322 ,(if lexical-binding ;FIXME: This is not reliable, but is all we have.
323 ;; The `default' arg should be an expression that evaluates to
324 ;; the value to use. The use of `eval' for it is spread over
325 ;; many different places and hence difficult to eliminate, yet
326 ;; we want to make sure that the `value' expression is checked by the
327 ;; byte-compiler, and that lexical-binding is obeyed, so quote the
328 ;; expression with `lambda' rather than with `quote'.
329 `(list (lambda () ,value))
330 `',value)
331 ,doc
332 ,@args))
334 ;;; The `defface' Macro.
336 (defmacro defface (face spec doc &rest args)
337 "Declare FACE as a customizable face that defaults to SPEC.
338 FACE does not need to be quoted.
340 Third argument DOC is the face documentation.
342 If FACE has been set with `custom-set-faces', set the face attributes
343 as specified by that function, otherwise set the face attributes
344 according to SPEC.
346 The remaining arguments should have the form
348 [KEYWORD VALUE]...
350 For a list of valid keywords, see the common keywords listed in
351 `defcustom'.
353 SPEC should be an alist of the form ((DISPLAY ATTS)...).
355 In the first element, DISPLAY can be `default'. The ATTS in that
356 element then act as defaults for all the following elements.
358 Aside from that, DISPLAY specifies conditions to match some or
359 all frames. For each frame, the first element of SPEC where the
360 DISPLAY conditions are satisfied is the one that applies to that
361 frame. The ATTRs in this element take effect, and the following
362 elements are ignored, on that frame.
364 In the last element, DISPLAY can be t. That element applies to a
365 frame if none of the previous elements (except the `default' if
366 any) did.
368 ATTS is a list of face attributes followed by their values:
369 (ATTR VALUE ATTR VALUE...)
371 The possible attributes are `:family', `:width', `:height', `:weight',
372 `:slant', `:underline', `:overline', `:strike-through', `:box',
373 `:foreground', `:background', `:stipple', `:inverse-video', and `:inherit'.
375 DISPLAY can be `default' (only in the first element), the symbol
376 t (only in the last element) to match all frames, or an alist of
377 conditions of the form \(REQ ITEM...). For such an alist to
378 match a frame, each of the conditions must be satisfied, meaning
379 that the REQ property of the frame must match one of the
380 corresponding ITEMs. These are the defined REQ values:
382 `type' (the value of `window-system')
383 Under X, in addition to the values `window-system' can take,
384 `motif', `lucid', `gtk' and `x-toolkit' are allowed, and match when
385 the Motif toolkit, Lucid toolkit, GTK toolkit or any X toolkit is in use.
387 `class' (the frame's color support)
388 Should be one of `color', `grayscale', or `mono'.
390 `background' (what color is used for the background text)
391 Should be one of `light' or `dark'.
393 `min-colors' (the minimum number of colors the frame should support)
394 Should be an integer, it is compared with the result of
395 `display-color-cells'.
397 `supports' (only match frames that support the specified face attributes)
398 Should be a list of face attributes. See the documentation for
399 the function `display-supports-face-attributes-p' for more
400 information on exactly how testing is done.
402 See Info node `(elisp) Customization' in the Emacs Lisp manual
403 for more information."
404 (declare (doc-string 3))
405 ;; It is better not to use backquote in this file,
406 ;; because that makes a bootstrapping problem
407 ;; if you need to recompile all the Lisp files using interpreted code.
408 (nconc (list 'custom-declare-face (list 'quote face) spec doc) args))
410 ;;; The `defgroup' Macro.
412 (defun custom-current-group ()
413 (cdr (assoc load-file-name custom-current-group-alist)))
415 (defun custom-declare-group (symbol members doc &rest args)
416 "Like `defgroup', but SYMBOL is evaluated as a normal argument."
417 (while members
418 (apply 'custom-add-to-group symbol (car members))
419 (setq members (cdr members)))
420 (when doc
421 ;; This text doesn't get into DOC.
422 (put symbol 'group-documentation (purecopy doc)))
423 (while args
424 (let ((arg (car args)))
425 (setq args (cdr args))
426 (unless (symbolp arg)
427 (error "Junk in args %S" args))
428 (let ((keyword arg)
429 (value (car args)))
430 (unless args
431 (error "Keyword %s is missing an argument" keyword))
432 (setq args (cdr args))
433 (cond ((eq keyword :prefix)
434 (put symbol 'custom-prefix (purecopy value)))
436 (custom-handle-keyword symbol keyword value
437 'custom-group))))))
438 ;; Record the group on the `current' list.
439 (let ((elt (assoc load-file-name custom-current-group-alist)))
440 (if elt (setcdr elt symbol)
441 (push (cons (purecopy load-file-name) symbol)
442 custom-current-group-alist)))
443 (run-hooks 'custom-define-hook)
444 symbol)
446 (defmacro defgroup (symbol members doc &rest args)
447 "Declare SYMBOL as a customization group containing MEMBERS.
448 SYMBOL does not need to be quoted.
450 Third argument DOC is the group documentation. This should be a short
451 description of the group, beginning with a capital and ending with
452 a period. Words other than the first should not be capitalized, if they
453 are not usually written so.
455 MEMBERS should be an alist of the form ((NAME WIDGET)...) where
456 NAME is a symbol and WIDGET is a widget for editing that symbol.
457 Useful widgets are `custom-variable' for editing variables,
458 `custom-face' for edit faces, and `custom-group' for editing groups.
460 The remaining arguments should have the form
462 [KEYWORD VALUE]...
464 For a list of valid keywords, see the common keywords listed in
465 `defcustom'.
467 See Info node `(elisp) Customization' in the Emacs Lisp manual
468 for more information."
469 (declare (doc-string 3))
470 ;; It is better not to use backquote in this file,
471 ;; because that makes a bootstrapping problem
472 ;; if you need to recompile all the Lisp files using interpreted code.
473 (nconc (list 'custom-declare-group (list 'quote symbol) members doc) args))
475 (defun custom-add-to-group (group option widget)
476 "To existing GROUP add a new OPTION of type WIDGET.
477 If there already is an entry for OPTION and WIDGET, nothing is done."
478 (let ((members (get group 'custom-group))
479 (entry (list option widget)))
480 (unless (member entry members)
481 (put group 'custom-group (nconc members (list entry))))))
483 (defun custom-group-of-mode (mode)
484 "Return the custom group corresponding to the major or minor MODE.
485 If no such group is found, return nil."
486 (or (get mode 'custom-mode-group)
487 (if (or (get mode 'custom-group)
488 (and (string-match "-mode\\'" (symbol-name mode))
489 (get (setq mode (intern (substring (symbol-name mode)
490 0 (match-beginning 0))))
491 'custom-group)))
492 mode)))
494 ;;; Properties.
496 (defun custom-handle-all-keywords (symbol args type)
497 "For customization option SYMBOL, handle keyword arguments ARGS.
498 Third argument TYPE is the custom option type."
499 (unless (memq :group args)
500 (custom-add-to-group (custom-current-group) symbol type))
501 (while args
502 (let ((arg (car args)))
503 (setq args (cdr args))
504 (unless (symbolp arg)
505 (error "Junk in args %S" args))
506 (let ((keyword arg)
507 (value (car args)))
508 (unless args
509 (error "Keyword %s is missing an argument" keyword))
510 (setq args (cdr args))
511 (custom-handle-keyword symbol keyword value type)))))
513 (defun custom-handle-keyword (symbol keyword value type)
514 "For customization option SYMBOL, handle KEYWORD with VALUE.
515 Fourth argument TYPE is the custom option type."
516 (if purify-flag
517 (setq value (purecopy value)))
518 (cond ((eq keyword :group)
519 (custom-add-to-group value symbol type))
520 ((eq keyword :version)
521 (custom-add-version symbol value))
522 ((eq keyword :package-version)
523 (custom-add-package-version symbol value))
524 ((eq keyword :link)
525 (custom-add-link symbol value))
526 ((eq keyword :load)
527 (custom-add-load symbol value))
528 ((eq keyword :tag)
529 (put symbol 'custom-tag value))
530 ((eq keyword :set-after)
531 (custom-add-dependencies symbol value))
533 (error "Unknown keyword %s" keyword))))
535 (defun custom-add-dependencies (symbol value)
536 "To the custom option SYMBOL, add dependencies specified by VALUE.
537 VALUE should be a list of symbols. For each symbol in that list,
538 this specifies that SYMBOL should be set after the specified symbol, if
539 both appear in constructs like `custom-set-variables'."
540 (unless (listp value)
541 (error "Invalid custom dependency `%s'" value))
542 (let* ((deps (get symbol 'custom-dependencies))
543 (new-deps deps))
544 (while value
545 (let ((dep (car value)))
546 (unless (symbolp dep)
547 (error "Invalid custom dependency `%s'" dep))
548 (unless (memq dep new-deps)
549 (setq new-deps (cons dep new-deps)))
550 (setq value (cdr value))))
551 (unless (eq deps new-deps)
552 (put symbol 'custom-dependencies new-deps))))
554 (defun custom-add-option (symbol option)
555 "To the variable SYMBOL add OPTION.
557 If SYMBOL's custom type is a hook, OPTION should be a hook member.
558 If SYMBOL's custom type is an alist, OPTION specifies a symbol
559 to offer to the user as a possible key in the alist.
560 For other custom types, this has no effect."
561 (let ((options (get symbol 'custom-options)))
562 (unless (member option options)
563 (put symbol 'custom-options (cons option options)))))
564 (defalias 'custom-add-frequent-value 'custom-add-option)
566 (defun custom-add-link (symbol widget)
567 "To the custom option SYMBOL add the link WIDGET."
568 (let ((links (get symbol 'custom-links)))
569 (unless (member widget links)
570 (put symbol 'custom-links (cons (purecopy widget) links)))))
572 (defun custom-add-version (symbol version)
573 "To the custom option SYMBOL add the version VERSION."
574 (put symbol 'custom-version (purecopy version)))
576 (defun custom-add-package-version (symbol version)
577 "To the custom option SYMBOL add the package version VERSION."
578 (put symbol 'custom-package-version (purecopy version)))
580 (defun custom-add-load (symbol load)
581 "To the custom option SYMBOL add the dependency LOAD.
582 LOAD should be either a library file name, or a feature name."
583 (let ((loads (get symbol 'custom-loads)))
584 (unless (member load loads)
585 (put symbol 'custom-loads (cons (purecopy load) loads)))))
587 (defun custom-autoload (symbol load &optional noset)
588 "Mark SYMBOL as autoloaded custom variable and add dependency LOAD.
589 If NOSET is non-nil, don't bother autoloading LOAD when setting the variable."
590 (put symbol 'custom-autoload (if noset 'noset t))
591 (custom-add-load symbol load))
593 ;; This test is also in the C code of `user-variable-p'.
594 (defun custom-variable-p (variable)
595 "Return non-nil if VARIABLE is a customizable variable.
596 A customizable variable is either (i) a variable whose property
597 list contains a non-nil `standard-value' or `custom-autoload'
598 property, or (ii) an alias for another customizable variable."
599 (setq variable (indirect-variable variable))
600 (or (get variable 'standard-value)
601 (get variable 'custom-autoload)))
603 (defun custom-note-var-changed (variable)
604 "Inform Custom that VARIABLE has been set (changed).
605 VARIABLE is a symbol that names a user option.
606 The result is that the change is treated as having been made through Custom."
607 (put variable 'customized-value (list (custom-quote (eval variable)))))
610 ;;; Custom Themes
612 ;;; Loading files needed to customize a symbol.
613 ;;; This is in custom.el because menu-bar.el needs it for toggle cmds.
615 (defvar custom-load-recursion nil
616 "Hack to avoid recursive dependencies.")
618 (defun custom-load-symbol (symbol)
619 "Load all dependencies for SYMBOL."
620 (unless custom-load-recursion
621 (let ((custom-load-recursion t))
622 ;; Load these files if not already done,
623 ;; to make sure we know all the dependencies of SYMBOL.
624 (condition-case nil
625 (require 'cus-load)
626 (error nil))
627 (condition-case nil
628 (require 'cus-start)
629 (error nil))
630 (dolist (load (get symbol 'custom-loads))
631 (cond ((symbolp load) (condition-case nil (require load) (error nil)))
632 ;; This is subsumed by the test below, but it's much faster.
633 ((assoc load load-history))
634 ;; This was just (assoc (locate-library load) load-history)
635 ;; but has been optimized not to load locate-library
636 ;; if not necessary.
637 ((let ((regexp (concat "\\(\\`\\|/\\)" (regexp-quote load)
638 "\\(\\'\\|\\.\\)"))
639 (found nil))
640 (dolist (loaded load-history)
641 (and (stringp (car loaded))
642 (string-match regexp (car loaded))
643 (setq found t)))
644 found))
645 ;; Without this, we would load cus-edit recursively.
646 ;; We are still loading it when we call this,
647 ;; and it is not in load-history yet.
648 ((equal load "cus-edit"))
649 (t (condition-case nil (load load) (error nil))))))))
651 (defvar custom-local-buffer nil
652 "Non-nil, in a Customization buffer, means customize a specific buffer.
653 If this variable is non-nil, it should be a buffer,
654 and it means customize the local bindings of that buffer.
655 This variable is a permanent local, and it normally has a local binding
656 in every Customization buffer.")
657 (put 'custom-local-buffer 'permanent-local t)
659 (defun custom-set-default (variable value)
660 "Default :set function for a customizable variable.
661 Normally, this sets the default value of VARIABLE to VALUE,
662 but if `custom-local-buffer' is non-nil,
663 this sets the local binding in that buffer instead."
664 (if custom-local-buffer
665 (with-current-buffer custom-local-buffer
666 (set variable value))
667 (set-default variable value)))
669 (defun custom-set-minor-mode (variable value)
670 ":set function for minor mode variables.
671 Normally, this sets the default value of VARIABLE to nil if VALUE
672 is nil and to t otherwise,
673 but if `custom-local-buffer' is non-nil,
674 this sets the local binding in that buffer instead."
675 (if custom-local-buffer
676 (with-current-buffer custom-local-buffer
677 (funcall variable (if value 1 0)))
678 (funcall variable (if value 1 0))))
680 (defun custom-quote (sexp)
681 "Quote SEXP if it is not self quoting."
682 (if (or (memq sexp '(t nil))
683 (keywordp sexp)
684 (and (listp sexp)
685 (memq (car sexp) '(lambda)))
686 (stringp sexp)
687 (numberp sexp)
688 (vectorp sexp)
689 ;;; (and (fboundp 'characterp)
690 ;;; (characterp sexp))
692 sexp
693 (list 'quote sexp)))
695 (defun customize-mark-to-save (symbol)
696 "Mark SYMBOL for later saving.
698 If the default value of SYMBOL is different from the standard value,
699 set the `saved-value' property to a list whose car evaluates to the
700 default value. Otherwise, set it to nil.
702 To actually save the value, call `custom-save-all'.
704 Return non-nil if the `saved-value' property actually changed."
705 (custom-load-symbol symbol)
706 (let* ((get (or (get symbol 'custom-get) 'default-value))
707 (value (funcall get symbol))
708 (saved (get symbol 'saved-value))
709 (standard (get symbol 'standard-value))
710 (comment (get symbol 'customized-variable-comment)))
711 ;; Save default value if different from standard value.
712 (if (or (null standard)
713 (not (equal value (condition-case nil
714 (eval (car standard))
715 (error nil)))))
716 (put symbol 'saved-value (list (custom-quote value)))
717 (put symbol 'saved-value nil))
718 ;; Clear customized information (set, but not saved).
719 (put symbol 'customized-value nil)
720 ;; Save any comment that might have been set.
721 (when comment
722 (put symbol 'saved-variable-comment comment))
723 (not (equal saved (get symbol 'saved-value)))))
725 (defun customize-mark-as-set (symbol)
726 "Mark current value of SYMBOL as being set from customize.
728 If the default value of SYMBOL is different from the saved value if any,
729 or else if it is different from the standard value, set the
730 `customized-value' property to a list whose car evaluates to the
731 default value. Otherwise, set it to nil.
733 Return non-nil if the `customized-value' property actually changed."
734 (custom-load-symbol symbol)
735 (let* ((get (or (get symbol 'custom-get) 'default-value))
736 (value (funcall get symbol))
737 (customized (get symbol 'customized-value))
738 (old (or (get symbol 'saved-value) (get symbol 'standard-value))))
739 ;; Mark default value as set if different from old value.
740 (if (not (and old
741 (equal value (condition-case nil
742 (eval (car old))
743 (error nil)))))
744 (progn (put symbol 'customized-value (list (custom-quote value)))
745 (custom-push-theme 'theme-value symbol 'user 'set
746 (custom-quote value)))
747 (put symbol 'customized-value nil))
748 ;; Changed?
749 (not (equal customized (get symbol 'customized-value)))))
751 (defun custom-reevaluate-setting (symbol)
752 "Reset the value of SYMBOL by re-evaluating its saved or standard value.
753 Use the :set function to do so. This is useful for customizable options
754 that are defined before their standard value can really be computed.
755 E.g. dumped variables whose default depends on run-time information."
756 (funcall (or (get symbol 'custom-set) 'set-default)
757 symbol
758 (eval (car (or (get symbol 'saved-value) (get symbol 'standard-value))))))
761 ;;; Custom Themes
763 ;; Custom themes are collections of settings that can be enabled or
764 ;; disabled as a unit.
766 ;; Each Custom theme is defined by a symbol, called the theme name.
767 ;; The `theme-settings' property of the theme name records the
768 ;; variable and face settings of the theme. This property is a list
769 ;; of elements, each of the form
771 ;; (PROP SYMBOL THEME VALUE)
773 ;; - PROP is either `theme-value' or `theme-face'
774 ;; - SYMBOL is the face or variable name
775 ;; - THEME is the theme name (redundant, but simplifies the code)
776 ;; - VALUE is an expression that gives the theme's setting for SYMBOL.
778 ;; The theme name also has a `theme-feature' property, whose value is
779 ;; specified when the theme is defined (see `custom-declare-theme').
780 ;; Usually, this is just a symbol named THEME-theme. This lets
781 ;; external libraries call (require 'foo-theme).
783 ;; In addition, each symbol (either a variable or a face) affected by
784 ;; an *enabled* theme has a `theme-value' or `theme-face' property,
785 ;; which is a list of elements each of the form
787 ;; (THEME VALUE)
789 ;; which have the same meanings as in `theme-settings'.
791 ;; The `theme-value' and `theme-face' lists are ordered by decreasing
792 ;; theme precedence. Thus, the first element is always the one that
793 ;; is in effect.
795 ;; Each theme is stored in a theme file, with filename THEME-theme.el.
796 ;; Loading a theme basically involves calling (load "THEME-theme")
797 ;; This is done by the function `load-theme'. Loading a theme
798 ;; automatically enables it.
800 ;; When a theme is enabled, the `theme-value' and `theme-face'
801 ;; properties for the affected symbols are set. When a theme is
802 ;; disabled, its settings are removed from the `theme-value' and
803 ;; `theme-face' properties, but the theme's own `theme-settings'
804 ;; property remains unchanged.
806 (defvar custom-known-themes '(user changed)
807 "Themes that have been defined with `deftheme'.
808 The default value is the list (user changed). The theme `changed'
809 contains the settings before custom themes are applied. The theme
810 `user' contains all the settings the user customized and saved.
811 Additional themes declared with the `deftheme' macro will be added
812 to the front of this list.")
814 (defsubst custom-theme-p (theme)
815 "Non-nil when THEME has been defined."
816 (memq theme custom-known-themes))
818 (defsubst custom-check-theme (theme)
819 "Check whether THEME is valid, and signal an error if it is not."
820 (unless (custom-theme-p theme)
821 (error "Unknown theme `%s'" theme)))
823 (defun custom-push-theme (prop symbol theme mode &optional value)
824 "Record VALUE for face or variable SYMBOL in custom theme THEME.
825 PROP is `theme-face' for a face, `theme-value' for a variable.
827 MODE can be either the symbol `set' or the symbol `reset'. If it is the
828 symbol `set', then VALUE is the value to use. If it is the symbol
829 `reset', then SYMBOL will be removed from THEME (VALUE is ignored).
831 See `custom-known-themes' for a list of known themes."
832 (unless (memq prop '(theme-value theme-face))
833 (error "Unknown theme property"))
834 (let* ((old (get symbol prop))
835 (setting (assq theme old)) ; '(theme value)
836 (theme-settings ; '(prop symbol theme value)
837 (get theme 'theme-settings)))
838 (cond
839 ;; Remove a setting:
840 ((eq mode 'reset)
841 (when setting
842 (let (res)
843 (dolist (theme-setting theme-settings)
844 (if (and (eq (car theme-setting) prop)
845 (eq (cadr theme-setting) symbol))
846 (setq res theme-setting)))
847 (put theme 'theme-settings (delq res theme-settings)))
848 (put symbol prop (delq setting old))))
849 ;; Alter an existing setting:
850 (setting
851 (let (res)
852 (dolist (theme-setting theme-settings)
853 (if (and (eq (car theme-setting) prop)
854 (eq (cadr theme-setting) symbol))
855 (setq res theme-setting)))
856 (put theme 'theme-settings
857 (cons (list prop symbol theme value)
858 (delq res theme-settings)))
859 (setcar (cdr setting) value)))
860 ;; Add a new setting:
862 (unless old
863 ;; If the user changed a variable outside of Customize, save
864 ;; the value to a fake theme, `changed'. If the theme is
865 ;; later disabled, we use this to bring back the old value.
867 ;; For faces, we just use `face-new-frame-defaults' to
868 ;; recompute when the theme is disabled.
869 (when (and (eq prop 'theme-value)
870 (boundp symbol))
871 (let ((sv (get symbol 'standard-value))
872 (val (symbol-value symbol)))
873 (unless (and sv (equal (eval (car sv)) val))
874 (setq old `((changed ,(custom-quote val))))))))
875 (put symbol prop (cons (list theme value) old))
876 (put theme 'theme-settings
877 (cons (list prop symbol theme value) theme-settings))))))
879 (defun custom-fix-face-spec (spec)
880 "Convert face SPEC, replacing obsolete :bold and :italic attributes.
881 Also change :reverse-video to :inverse-video."
882 (when (listp spec)
883 (if (or (memq :bold spec)
884 (memq :italic spec)
885 (memq :inverse-video spec))
886 (let (result)
887 (while spec
888 (let ((key (car spec))
889 (val (car (cdr spec))))
890 (cond ((eq key :italic)
891 (push :slant result)
892 (push (if val 'italic 'normal) result))
893 ((eq key :bold)
894 (push :weight result)
895 (push (if val 'bold 'normal) result))
896 ((eq key :reverse-video)
897 (push :inverse-video result)
898 (push val result))
900 (push key result)
901 (push val result))))
902 (setq spec (cddr spec)))
903 (nreverse result))
904 spec)))
906 (defun custom-set-variables (&rest args)
907 "Install user customizations of variable values specified in ARGS.
908 These settings are registered as theme `user'.
909 The arguments should each be a list of the form:
911 (SYMBOL EXP [NOW [REQUEST [COMMENT]]])
913 This stores EXP (without evaluating it) as the saved value for SYMBOL.
914 If NOW is present and non-nil, then also evaluate EXP and set
915 the default value for the SYMBOL to the value of EXP.
917 REQUEST is a list of features we must require in order to
918 handle SYMBOL properly.
919 COMMENT is a comment string about SYMBOL."
920 (apply 'custom-theme-set-variables 'user args))
922 (defun custom-theme-set-variables (theme &rest args)
923 "Initialize variables for theme THEME according to settings in ARGS.
924 Each of the arguments in ARGS should be a list of this form:
926 (SYMBOL EXP [NOW [REQUEST [COMMENT]]])
928 This stores EXP (without evaluating it) as the saved value for SYMBOL.
929 If NOW is present and non-nil, then also evaluate EXP and set
930 the default value for the SYMBOL to the value of EXP.
932 REQUEST is a list of features we must require in order to
933 handle SYMBOL properly.
934 COMMENT is a comment string about SYMBOL.
936 EXP itself is saved unevaluated as SYMBOL property `saved-value' and
937 in SYMBOL's list property `theme-value' \(using `custom-push-theme')."
938 (custom-check-theme theme)
940 ;; Process all the needed autoloads before anything else, so that the
941 ;; subsequent code has all the info it needs (e.g. which var corresponds
942 ;; to a minor mode), regardless of the ordering of the variables.
943 (dolist (entry args)
944 (let* ((symbol (indirect-variable (nth 0 entry))))
945 (unless (or (get symbol 'standard-value)
946 (memq (get symbol 'custom-autoload) '(nil noset)))
947 ;; This symbol needs to be autoloaded, even just for a `set'.
948 (custom-load-symbol symbol))))
950 ;; Move minor modes and variables with explicit requires to the end.
951 (setq args
952 (sort args
953 (lambda (a1 a2)
954 (let* ((sym1 (car a1))
955 (sym2 (car a2))
956 (1-then-2 (memq sym1 (get sym2 'custom-dependencies)))
957 (2-then-1 (memq sym2 (get sym1 'custom-dependencies))))
958 (cond ((and 1-then-2 2-then-1)
959 (error "Circular custom dependency between `%s' and `%s'"
960 sym1 sym2))
961 (2-then-1 nil)
962 ;; 1 is a dependency of 2, so needs to be set first.
963 (1-then-2)
964 ;; Put minor modes and symbols with :require last.
965 ;; Putting minor modes last ensures that the mode
966 ;; function will see other customized values rather
967 ;; than default values.
968 (t (or (nth 3 a2)
969 (eq (get sym2 'custom-set)
970 'custom-set-minor-mode))))))))
972 (dolist (entry args)
973 (unless (listp entry)
974 (error "Incompatible Custom theme spec"))
975 (let* ((symbol (indirect-variable (nth 0 entry)))
976 (value (nth 1 entry)))
977 (custom-push-theme 'theme-value symbol theme 'set value)
978 (unless custom--inhibit-theme-enable
979 ;; Now set the variable.
980 (let* ((now (nth 2 entry))
981 (requests (nth 3 entry))
982 (comment (nth 4 entry))
983 set)
984 (when requests
985 (put symbol 'custom-requests requests)
986 (mapc 'require requests))
987 (setq set (or (get symbol 'custom-set) 'custom-set-default))
988 (put symbol 'saved-value (list value))
989 (put symbol 'saved-variable-comment comment)
990 ;; Allow for errors in the case where the setter has
991 ;; changed between versions, say, but let the user know.
992 (condition-case data
993 (cond (now
994 ;; Rogue variable, set it now.
995 (put symbol 'force-value t)
996 (funcall set symbol (eval value)))
997 ((default-boundp symbol)
998 ;; Something already set this, overwrite it.
999 (funcall set symbol (eval value))))
1000 (error
1001 (message "Error setting %s: %s" symbol data)))
1002 (and (or now (default-boundp symbol))
1003 (put symbol 'variable-comment comment)))))))
1006 ;;; Defining themes.
1008 ;; A theme file is named `THEME-theme.el' (where THEME is the theme
1009 ;; name) found in `custom-theme-load-path'. It has this format:
1011 ;; (deftheme THEME
1012 ;; DOCSTRING)
1014 ;; (custom-theme-set-variables
1015 ;; 'THEME
1016 ;; [THEME-VARIABLES])
1018 ;; (custom-theme-set-faces
1019 ;; 'THEME
1020 ;; [THEME-FACES])
1022 ;; (provide-theme 'THEME)
1025 ;; The IGNORED arguments to deftheme come from the XEmacs theme code, where
1026 ;; they were used to supply keyword-value pairs like `:immediate',
1027 ;; `:variable-reset-string', etc. We don't use any of these, so ignore them.
1029 (defmacro deftheme (theme &optional doc &rest ignored)
1030 "Declare THEME to be a Custom theme.
1031 The optional argument DOC is a doc string describing the theme.
1033 Any theme `foo' should be defined in a file called `foo-theme.el';
1034 see `custom-make-theme-feature' for more information."
1035 (let ((feature (custom-make-theme-feature theme)))
1036 ;; It is better not to use backquote in this file,
1037 ;; because that makes a bootstrapping problem
1038 ;; if you need to recompile all the Lisp files using interpreted code.
1039 (list 'custom-declare-theme (list 'quote theme) (list 'quote feature) doc)))
1041 (defun custom-declare-theme (theme feature &optional doc &rest ignored)
1042 "Like `deftheme', but THEME is evaluated as a normal argument.
1043 FEATURE is the feature this theme provides. Normally, this is a symbol
1044 created from THEME by `custom-make-theme-feature'."
1045 (unless (custom-theme-name-valid-p theme)
1046 (error "Custom theme cannot be named %S" theme))
1047 (add-to-list 'custom-known-themes theme)
1048 (put theme 'theme-feature feature)
1049 (when doc (put theme 'theme-documentation doc)))
1051 (defun custom-make-theme-feature (theme)
1052 "Given a symbol THEME, create a new symbol by appending \"-theme\".
1053 Store this symbol in the `theme-feature' property of THEME.
1054 Calling `provide-theme' to provide THEME actually puts `THEME-theme'
1055 into `features'.
1057 This allows for a file-name convention for autoloading themes:
1058 Every theme X has a property `provide-theme' whose value is \"X-theme\".
1059 \(load-theme X) then attempts to load the file `X-theme.el'."
1060 (intern (concat (symbol-name theme) "-theme")))
1062 ;;; Loading themes.
1064 (defcustom custom-theme-directory user-emacs-directory
1065 "Default user directory for storing custom theme files.
1066 The command `customize-create-theme' writes theme files into this
1067 directory. By default, Emacs searches for custom themes in this
1068 directory first---see `custom-theme-load-path'."
1069 :type 'string
1070 :group 'customize
1071 :version "22.1")
1073 (defcustom custom-theme-load-path (list 'custom-theme-directory t)
1074 "List of directories to search for custom theme files.
1075 When loading custom themes (e.g. in `customize-themes' and
1076 `load-theme'), Emacs searches for theme files in the specified
1077 order. Each element in the list should be one of the following:
1078 - the symbol `custom-theme-directory', meaning the value of
1079 `custom-theme-directory'.
1080 - the symbol t, meaning the built-in theme directory (a directory
1081 named \"themes\" in `data-directory').
1082 - a directory name (a string).
1084 Each theme file is named THEME-theme.el, where THEME is the theme
1085 name."
1086 :type '(repeat (choice (const :tag "custom-theme-directory"
1087 custom-theme-directory)
1088 (const :tag "Built-in theme directory" t)
1089 directory))
1090 :group 'customize
1091 :version "24.1")
1093 (defvar custom--inhibit-theme-enable nil
1094 "Whether the custom-theme-set-* functions act immediately.
1095 If nil, `custom-theme-set-variables' and `custom-theme-set-faces'
1096 change the current values of the given variable or face. If
1097 non-nil, they just make a record of the theme settings.")
1099 (defun provide-theme (theme)
1100 "Indicate that this file provides THEME.
1101 This calls `provide' to provide the feature name stored in THEME's
1102 property `theme-feature' (which is usually a symbol created by
1103 `custom-make-theme-feature')."
1104 (unless (custom-theme-name-valid-p theme)
1105 (error "Custom theme cannot be named %S" theme))
1106 (custom-check-theme theme)
1107 (provide (get theme 'theme-feature)))
1109 (defcustom custom-safe-themes '(default)
1110 "Themes that are considered safe to load.
1111 If the value is a list, each element should be either the SHA-256
1112 hash of a safe theme file, or the symbol `default', which stands
1113 for any theme in the built-in Emacs theme directory (a directory
1114 named \"themes\" in `data-directory').
1116 If the value is t, Emacs treats all themes as safe.
1118 This variable cannot be set in a Custom theme."
1119 :type '(choice (repeat :tag "List of safe themes"
1120 (choice string
1121 (const :tag "Built-in themes" default)))
1122 (const :tag "All themes" t))
1123 :group 'customize
1124 :risky t
1125 :version "24.1")
1127 (defun load-theme (theme &optional no-confirm no-enable)
1128 "Load Custom theme named THEME from its file.
1129 The theme file is named THEME-theme.el, in one of the directories
1130 specified by `custom-theme-load-path'.
1132 If optional arg NO-CONFIRM is non-nil, and THEME is not
1133 considered safe according to `custom-safe-themes', prompt the
1134 user for confirmation.
1136 Normally, this function also enables THEME; if optional arg
1137 NO-ENABLE is non-nil, load the theme but don't enable it.
1139 This function is normally called through Customize when setting
1140 `custom-enabled-themes'. If used directly in your init file, it
1141 should be called with a non-nil NO-CONFIRM argument, or after
1142 `custom-safe-themes' has been loaded.
1144 Return t if THEME was successfully loaded, nil otherwise."
1145 (interactive
1146 (list
1147 (intern (completing-read "Load custom theme: "
1148 (mapcar 'symbol-name
1149 (custom-available-themes))))
1150 nil nil))
1151 (unless (custom-theme-name-valid-p theme)
1152 (error "Invalid theme name `%s'" theme))
1153 ;; If reloading, clear out the old theme settings.
1154 (when (custom-theme-p theme)
1155 (disable-theme theme)
1156 (put theme 'theme-settings nil)
1157 (put theme 'theme-feature nil)
1158 (put theme 'theme-documentation nil))
1159 (let ((fn (locate-file (concat (symbol-name theme) "-theme.el")
1160 (custom-theme--load-path)
1161 '("" "c")))
1162 hash)
1163 (unless fn
1164 (error "Unable to find theme file for `%s'" theme))
1165 (with-temp-buffer
1166 (insert-file-contents fn)
1167 (setq hash (secure-hash 'sha256 (current-buffer)))
1168 ;; Check file safety with `custom-safe-themes', prompting the
1169 ;; user if necessary.
1170 (when (or no-confirm
1171 (eq custom-safe-themes t)
1172 (and (memq 'default custom-safe-themes)
1173 (equal (file-name-directory fn)
1174 (expand-file-name "themes/" data-directory)))
1175 (member hash custom-safe-themes)
1176 (custom-theme-load-confirm hash))
1177 (let ((custom--inhibit-theme-enable t))
1178 (eval-buffer))
1179 ;; Optimization: if the theme changes the `default' face, put that
1180 ;; entry first. This avoids some `frame-set-background-mode' rigmarole
1181 ;; by assigning the new background immediately.
1182 (let* ((settings (get theme 'theme-settings))
1183 (tail settings)
1184 found)
1185 (while (and tail (not found))
1186 (and (eq (nth 0 (car tail)) 'theme-face)
1187 (eq (nth 1 (car tail)) 'default)
1188 (setq found (car tail)))
1189 (setq tail (cdr tail)))
1190 (if found
1191 (put theme 'theme-settings (cons found (delq found settings)))))
1192 ;; Finally, enable the theme.
1193 (unless no-enable
1194 (enable-theme theme))
1195 t))))
1197 (defun custom-theme-load-confirm (hash)
1198 "Query the user about loading a Custom theme that may not be safe.
1199 The theme should be in the current buffer. If the user agrees,
1200 query also about adding HASH to `custom-safe-themes'."
1201 (if noninteractive
1203 (let ((exit-chars '(?y ?n ?\s))
1204 window prompt char)
1205 (save-window-excursion
1206 (rename-buffer "*Custom Theme*" t)
1207 (emacs-lisp-mode)
1208 (setq window (display-buffer (current-buffer)))
1209 (setq prompt
1210 (format "Loading a theme can run Lisp code. Really load?%s"
1211 (if (and window
1212 (< (line-number-at-pos (point-max))
1213 (window-body-height)))
1214 " (y or n) "
1215 (push ?\C-v exit-chars)
1216 "\nType y or n, or C-v to scroll: ")))
1217 (goto-char (point-min))
1218 (while (null char)
1219 (setq char (read-char-choice prompt exit-chars))
1220 (when (eq char ?\C-v)
1221 (if window
1222 (with-selected-window window
1223 (condition-case nil
1224 (scroll-up)
1225 (error (goto-char (point-min))))))
1226 (setq char nil)))
1227 (when (memq char '(?\s ?y))
1228 ;; Offer to save to `custom-safe-themes'.
1229 (and (or custom-file user-init-file)
1230 (y-or-n-p "Treat this theme as safe in future sessions? ")
1231 (customize-push-and-save 'custom-safe-themes (list hash)))
1232 t)))))
1234 (defun custom-theme-name-valid-p (name)
1235 "Return t if NAME is a valid name for a Custom theme, nil otherwise.
1236 NAME should be a symbol."
1237 (and (symbolp name)
1238 name
1239 (not (or (zerop (length (symbol-name name)))
1240 (eq name 'user)
1241 (eq name 'changed)))))
1243 (defun custom-available-themes ()
1244 "Return a list of available Custom themes (symbols)."
1245 (let (sym themes)
1246 (dolist (dir (custom-theme--load-path))
1247 (when (file-directory-p dir)
1248 (dolist (file (file-expand-wildcards
1249 (expand-file-name "*-theme.el" dir) t))
1250 (setq file (file-name-nondirectory file))
1251 (and (string-match "\\`\\(.+\\)-theme.el\\'" file)
1252 (setq sym (intern (match-string 1 file)))
1253 (custom-theme-name-valid-p sym)
1254 (push sym themes)))))
1255 (nreverse (delete-dups themes))))
1257 (defun custom-theme--load-path ()
1258 (let (lpath)
1259 (dolist (f custom-theme-load-path)
1260 (cond ((eq f 'custom-theme-directory)
1261 (setq f custom-theme-directory))
1262 ((eq f t)
1263 (setq f (expand-file-name "themes" data-directory))))
1264 (if (file-directory-p f)
1265 (push f lpath)))
1266 (nreverse lpath)))
1269 ;;; Enabling and disabling loaded themes.
1271 (defun enable-theme (theme)
1272 "Reenable all variable and face settings defined by THEME.
1273 THEME should be either `user', or a theme loaded via `load-theme'.
1274 After this function completes, THEME will have the highest
1275 precedence (after `user')."
1276 (interactive (list (intern
1277 (completing-read
1278 "Enable custom theme: "
1279 obarray (lambda (sym) (get sym 'theme-settings)) t))))
1280 (if (not (custom-theme-p theme))
1281 (error "Undefined Custom theme %s" theme))
1282 (let ((settings (get theme 'theme-settings)))
1283 ;; Loop through theme settings, recalculating vars/faces.
1284 (dolist (s settings)
1285 (let* ((prop (car s))
1286 (symbol (cadr s))
1287 (spec-list (get symbol prop)))
1288 (put symbol prop (cons (cddr s) (assq-delete-all theme spec-list)))
1289 (cond
1290 ((eq prop 'theme-face)
1291 (custom-theme-recalc-face symbol))
1292 ((eq prop 'theme-value)
1293 ;; Ignore `custom-enabled-themes' and `custom-safe-themes'.
1294 (unless (memq symbol '(custom-enabled-themes custom-safe-themes))
1295 (custom-theme-recalc-variable symbol)))))))
1296 (unless (eq theme 'user)
1297 (setq custom-enabled-themes
1298 (cons theme (delq theme custom-enabled-themes)))
1299 ;; Give the `user' theme the highest priority.
1300 (enable-theme 'user)))
1302 (defcustom custom-enabled-themes nil
1303 "List of enabled Custom Themes, highest precedence first.
1304 This list does not include the `user' theme, which is set by
1305 Customize and always takes precedence over other Custom Themes.
1307 This variable cannot be defined inside a Custom theme; there, it
1308 is simply ignored.
1310 Setting this variable through Customize calls `enable-theme' or
1311 `load-theme' for each theme in the list."
1312 :group 'customize
1313 :type '(repeat symbol)
1314 :set-after '(custom-theme-directory custom-theme-load-path
1315 custom-safe-themes)
1316 :risky t
1317 :set (lambda (symbol themes)
1318 (let (failures)
1319 (setq themes (delq 'user (delete-dups themes)))
1320 ;; Disable all themes not in THEMES.
1321 (if (boundp symbol)
1322 (dolist (theme (symbol-value symbol))
1323 (if (not (memq theme themes))
1324 (disable-theme theme))))
1325 ;; Call `enable-theme' or `load-theme' on each of THEMES.
1326 (dolist (theme (reverse themes))
1327 (condition-case nil
1328 (if (custom-theme-p theme)
1329 (enable-theme theme)
1330 (load-theme theme))
1331 (error (setq failures (cons theme failures)
1332 themes (delq theme themes)))))
1333 (enable-theme 'user)
1334 (custom-set-default symbol themes)
1335 (if failures
1336 (message "Failed to enable theme: %s"
1337 (mapconcat 'symbol-name failures ", "))))))
1339 (defsubst custom-theme-enabled-p (theme)
1340 "Return non-nil if THEME is enabled."
1341 (memq theme custom-enabled-themes))
1343 (defun disable-theme (theme)
1344 "Disable all variable and face settings defined by THEME.
1345 See `custom-enabled-themes' for a list of enabled themes."
1346 (interactive (list (intern
1347 (completing-read
1348 "Disable custom theme: "
1349 (mapcar 'symbol-name custom-enabled-themes)
1350 nil t))))
1351 (when (custom-theme-enabled-p theme)
1352 (let ((settings (get theme 'theme-settings)))
1353 (dolist (s settings)
1354 (let* ((prop (car s))
1355 (symbol (cadr s))
1356 (val (assq-delete-all theme (get symbol prop))))
1357 (put symbol prop val)
1358 (cond
1359 ((eq prop 'theme-value)
1360 (custom-theme-recalc-variable symbol))
1361 ((eq prop 'theme-face)
1362 ;; If the face spec specified by this theme is in the
1363 ;; saved-face property, reset that property.
1364 (when (equal (nth 3 s) (get symbol 'saved-face))
1365 (put symbol 'saved-face (and val (cadr (car val)))))))))
1366 ;; Recompute faces on all frames.
1367 (dolist (frame (frame-list))
1368 ;; We must reset the fg and bg color frame parameters, or
1369 ;; `face-set-after-frame-default' will use the existing
1370 ;; parameters, which could be from the disabled theme.
1371 (set-frame-parameter frame 'background-color
1372 (custom--frame-color-default
1373 frame :background "background" "Background"
1374 "unspecified-bg" "white"))
1375 (set-frame-parameter frame 'foreground-color
1376 (custom--frame-color-default
1377 frame :foreground "foreground" "Foreground"
1378 "unspecified-fg" "black"))
1379 (face-set-after-frame-default frame))
1380 (setq custom-enabled-themes
1381 (delq theme custom-enabled-themes)))))
1383 (defun custom--frame-color-default (frame attribute resource-attr resource-class
1384 tty-default x-default)
1385 (let ((col (face-attribute 'default attribute t)))
1386 (cond
1387 ((and col (not (eq col 'unspecified))) col)
1388 ((null (window-system frame)) tty-default)
1389 ((setq col (x-get-resource resource-attr resource-class)) col)
1390 (t x-default))))
1392 (defun custom-variable-theme-value (variable)
1393 "Return (list VALUE) indicating the custom theme value of VARIABLE.
1394 That is to say, it specifies what the value should be according to
1395 currently enabled custom themes.
1397 This function returns nil if no custom theme specifies a value for VARIABLE."
1398 (let ((theme-value (get variable 'theme-value)))
1399 (if theme-value
1400 (cdr (car theme-value)))))
1402 (defun custom-theme-recalc-variable (variable)
1403 "Set VARIABLE according to currently enabled custom themes."
1404 (let ((valspec (custom-variable-theme-value variable)))
1405 (if valspec
1406 (put variable 'saved-value valspec)
1407 (setq valspec (get variable 'standard-value)))
1408 (if (and valspec
1409 (or (get variable 'force-value)
1410 (default-boundp variable)))
1411 (funcall (or (get variable 'custom-set) 'set-default) variable
1412 (eval (car valspec))))))
1414 (defun custom-theme-recalc-face (face)
1415 "Set FACE according to currently enabled custom themes."
1416 (if (get face 'face-alias)
1417 (setq face (get face 'face-alias)))
1418 ;; Reset the faces for each frame.
1419 (dolist (frame (frame-list))
1420 (face-spec-recalc face frame)))
1423 ;;; XEmacs compatibility functions
1425 ;; In XEmacs, when you reset a Custom Theme, you have to specify the
1426 ;; theme to reset it to. We just apply the next available theme, so
1427 ;; just ignore the IGNORED arguments.
1429 (defun custom-theme-reset-variables (theme &rest args)
1430 "Reset some variable settings in THEME to their values in other themes.
1431 Each of the arguments ARGS has this form:
1433 (VARIABLE IGNORED)
1435 This means reset VARIABLE. (The argument IGNORED is ignored)."
1436 (custom-check-theme theme)
1437 (dolist (arg args)
1438 (custom-push-theme 'theme-value (car arg) theme 'reset)))
1440 (defun custom-reset-variables (&rest args)
1441 "Reset the specs of some variables to their values in other themes.
1442 This creates settings in the `user' theme.
1444 Each of the arguments ARGS has this form:
1446 (VARIABLE IGNORED)
1448 This means reset VARIABLE. (The argument IGNORED is ignored)."
1449 (apply 'custom-theme-reset-variables 'user args))
1451 ;;; The End.
1453 ;; Process the defcustoms for variables loaded before this file.
1454 (while custom-declare-variable-list
1455 (apply 'custom-declare-variable (car custom-declare-variable-list))
1456 (setq custom-declare-variable-list (cdr custom-declare-variable-list)))
1458 (provide 'custom)
1460 ;;; custom.el ends here