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