+(defun custom-reevaluate-setting (symbol)
+ "Reset the value of SYMBOL by re-evaluating its saved or standard value.
+Use the :set function to do so. This is useful for customizable options
+that are defined before their standard value can really be computed.
+E.g. dumped variables whose default depends on run-time information."
+ (funcall (or (get symbol 'custom-set) 'set-default)
+ symbol
+ (eval (car (or (get symbol 'saved-value) (get symbol 'standard-value))))))
+
+\f
+;;; Custom Themes
+
+;; Custom themes are collections of settings that can be enabled or
+;; disabled as a unit.
+
+;; Each Custom theme is defined by a symbol, called the theme name.
+;; The `theme-settings' property of the theme name records the
+;; variable and face settings of the theme. This property is a list
+;; of elements, each of the form
+;;
+;; (PROP SYMBOL THEME VALUE)
+;;
+;; - PROP is either `theme-value' or `theme-face'
+;; - SYMBOL is the face or variable name
+;; - THEME is the theme name (redundant, but simplifies the code)
+;; - VALUE is an expression that gives the theme's setting for SYMBOL.
+;;
+;; The theme name also has a `theme-feature' property, whose value is
+;; specified when the theme is defined (see `custom-declare-theme').
+;; Usually, this is just a symbol named THEME-theme. This lets
+;; external libraries call (require 'foo-theme).
+
+;; In addition, each symbol (either a variable or a face) affected by
+;; an *enabled* theme has a `theme-value' or `theme-face' property,
+;; which is a list of elements each of the form
+;;
+;; (THEME VALUE)
+;;
+;; which have the same meanings as in `theme-settings'.
+;;
+;; The `theme-value' and `theme-face' lists are ordered by decreasing
+;; theme precedence. Thus, the first element is always the one that
+;; is in effect.
+
+;; Each theme is stored in a theme file, with filename THEME-theme.el.
+;; Loading a theme basically involves calling (load "THEME-theme")
+;; This is done by the function `load-theme'. Loading a theme
+;; automatically enables it.
+;;
+;; When a theme is enabled, the `theme-value' and `theme-face'
+;; properties for the affected symbols are set. When a theme is
+;; disabled, its settings are removed from the `theme-value' and
+;; `theme-face' properties, but the theme's own `theme-settings'
+;; property remains unchanged.
+
+(defvar custom-known-themes '(user changed)
+ "Themes that have been defined with `deftheme'.
+The default value is the list (user changed). The theme `changed'
+contains the settings before custom themes are applied. The
+theme `user' contains all the settings the user customized and saved.
+Additional themes declared with the `deftheme' macro will be added to
+the front of this list.")
+
+(defsubst custom-theme-p (theme)
+ "Non-nil when THEME has been defined."
+ (memq theme custom-known-themes))
+
+(defsubst custom-check-theme (theme)
+ "Check whether THEME is valid, and signal an error if it is not."
+ (unless (custom-theme-p theme)
+ (error "Unknown theme `%s'" theme)))
+
+(defun custom-push-theme (prop symbol theme mode &optional value)
+ "Record VALUE for face or variable SYMBOL in custom theme THEME.
+PROP is `theme-face' for a face, `theme-value' for a variable.
+
+MODE can be either the symbol `set' or the symbol `reset'. If it is the
+symbol `set', then VALUE is the value to use. If it is the symbol
+`reset', then SYMBOL will be removed from THEME (VALUE is ignored).
+
+See `custom-known-themes' for a list of known themes."
+ (unless (memq prop '(theme-value theme-face))
+ (error "Unknown theme property"))
+ (let* ((old (get symbol prop))
+ (setting (assq theme old)) ; '(theme value)
+ (theme-settings ; '(prop symbol theme value)
+ (get theme 'theme-settings)))
+ (if (eq mode 'reset)
+ ;; Remove a setting.
+ (when setting
+ (let (res)
+ (dolist (theme-setting theme-settings)
+ (if (and (eq (car theme-setting) prop)
+ (eq (cadr theme-setting) symbol))
+ (setq res theme-setting)))
+ (put theme 'theme-settings (delq res theme-settings)))
+ (put symbol prop (delq setting old)))
+ (if setting
+ ;; Alter an existing setting.
+ (let (res)
+ (dolist (theme-setting theme-settings)
+ (if (and (eq (car theme-setting) prop)
+ (eq (cadr theme-setting) symbol))
+ (setq res theme-setting)))
+ (put theme 'theme-settings
+ (cons (list prop symbol theme value)
+ (delq res theme-settings)))
+ (setcar (cdr setting) value))
+ ;; Add a new setting.
+ ;; If the user changed the value outside of Customize, we
+ ;; first save the current value to a fake theme, `changed'.
+ ;; This ensures that the user-set value comes back if the
+ ;; theme is later disabled.
+ (if (null old)
+ (if (and (eq prop 'theme-value)
+ (boundp symbol)
+ (or (null (get symbol 'standard-value))
+ (not (equal (eval (car (get symbol 'standard-value)))
+ (symbol-value symbol)))))
+ (setq old (list (list 'changed (symbol-value symbol))))
+ (if (and (facep symbol)
+ (not (face-spec-match-p symbol (get symbol 'face-defface-spec))))
+ (setq old (list (list 'changed (list
+ (append '(t) (custom-face-attributes-get symbol nil)))))))))
+ (put symbol prop (cons (list theme value) old))
+ (put theme 'theme-settings
+ (cons (list prop symbol theme value)
+ theme-settings))))))
+
+\f
+(defun custom-set-variables (&rest args)
+ "Install user customizations of variable values specified in ARGS.
+These settings are registered as theme `user'.
+The arguments should each be a list of the form:
+
+ (SYMBOL EXP [NOW [REQUEST [COMMENT]]])
+
+This stores EXP (without evaluating it) as the saved value for SYMBOL.
+If NOW is present and non-nil, then also evaluate EXP and set
+the default value for the SYMBOL to the value of EXP.
+
+REQUEST is a list of features we must require in order to
+handle SYMBOL properly.
+COMMENT is a comment string about SYMBOL."
+ (apply 'custom-theme-set-variables 'user args))
+
+(defun custom-theme-set-variables (theme &rest args)
+ "Initialize variables for theme THEME according to settings in ARGS.
+Each of the arguments in ARGS should be a list of this form:
+
+ (SYMBOL EXP [NOW [REQUEST [COMMENT]]])
+
+This stores EXP (without evaluating it) as the saved value for SYMBOL.
+If NOW is present and non-nil, then also evaluate EXP and set
+the default value for the SYMBOL to the value of EXP.
+
+REQUEST is a list of features we must require in order to
+handle SYMBOL properly.
+COMMENT is a comment string about SYMBOL.
+
+EXP itself is saved unevaluated as SYMBOL property `saved-value' and
+in SYMBOL's list property `theme-value' \(using `custom-push-theme')."
+ (custom-check-theme theme)
+ (setq args
+ (sort args
+ (lambda (a1 a2)
+ (let* ((sym1 (car a1))
+ (sym2 (car a2))
+ (1-then-2 (memq sym1 (get sym2 'custom-dependencies)))
+ (2-then-1 (memq sym2 (get sym1 'custom-dependencies))))
+ (cond ((and 1-then-2 2-then-1)
+ (error "Circular custom dependency between `%s' and `%s'"
+ sym1 sym2))
+ (2-then-1 nil)
+ ;; Put minor modes and symbols with :require last.
+ ;; Putting minor modes last ensures that the mode
+ ;; function will see other customized values rather
+ ;; than default values.
+ (t (or (nth 3 a2)
+ (eq (get sym2 'custom-set)
+ 'custom-set-minor-mode))))))))
+ (while args
+ (let ((entry (car args)))
+ (if (listp entry)
+ (let* ((symbol (indirect-variable (nth 0 entry)))
+ (value (nth 1 entry))
+ (now (nth 2 entry))
+ (requests (nth 3 entry))
+ (comment (nth 4 entry))
+ set)
+ (when requests
+ (put symbol 'custom-requests requests)
+ (mapc 'require requests))
+ (setq set (or (get symbol 'custom-set) 'custom-set-default))
+ (put symbol 'saved-value (list value))
+ (put symbol 'saved-variable-comment comment)
+ (custom-push-theme 'theme-value symbol theme 'set value)
+ ;; Allow for errors in the case where the setter has
+ ;; changed between versions, say, but let the user know.
+ (condition-case data
+ (cond (now
+ ;; Rogue variable, set it now.
+ (put symbol 'force-value t)
+ (funcall set symbol (eval value)))
+ ((default-boundp symbol)
+ ;; Something already set this, overwrite it.
+ (funcall set symbol (eval value))))
+ (error
+ (message "Error setting %s: %s" symbol data)))
+ (setq args (cdr args))
+ (and (or now (default-boundp symbol))
+ (put symbol 'variable-comment comment)))
+ ;; Old format, a plist of SYMBOL VALUE pairs.
+ (message "Warning: old format `custom-set-variables'")
+ (ding)
+ (sit-for 2)
+ (let ((symbol (indirect-variable (nth 0 args)))
+ (value (nth 1 args)))
+ (put symbol 'saved-value (list value))
+ (custom-push-theme 'theme-value symbol theme 'set value))
+ (setq args (cdr (cdr args)))))))
+
+\f
+;;; Defining themes.
+
+;; A theme file should be named `THEME-theme.el' (where THEME is the theme
+;; name), and found in either `custom-theme-directory' or the load path.
+;; It has the following format:
+;;
+;; (deftheme THEME
+;; DOCSTRING)
+;;
+;; (custom-theme-set-variables
+;; 'THEME
+;; [THEME-VARIABLES])
+;;
+;; (custom-theme-set-faces
+;; 'THEME
+;; [THEME-FACES])
+;;
+;; (provide-theme 'THEME)
+
+
+;; The IGNORED arguments to deftheme come from the XEmacs theme code, where
+;; they were used to supply keyword-value pairs like `:immediate',
+;; `:variable-reset-string', etc. We don't use any of these, so ignore them.
+
+(defmacro deftheme (theme &optional doc &rest ignored)
+ "Declare THEME to be a Custom theme.
+The optional argument DOC is a doc string describing the theme.
+
+Any theme `foo' should be defined in a file called `foo-theme.el';
+see `custom-make-theme-feature' for more information."
+ (let ((feature (custom-make-theme-feature theme)))
+ ;; It is better not to use backquote in this file,
+ ;; because that makes a bootstrapping problem
+ ;; if you need to recompile all the Lisp files using interpreted code.
+ (list 'custom-declare-theme (list 'quote theme) (list 'quote feature) doc)))
+
+(defun custom-declare-theme (theme feature &optional doc &rest ignored)
+ "Like `deftheme', but THEME is evaluated as a normal argument.
+FEATURE is the feature this theme provides. Normally, this is a symbol
+created from THEME by `custom-make-theme-feature'."
+ (if (memq theme '(user changed))
+ (error "Custom theme cannot be named %S" theme))
+ (add-to-list 'custom-known-themes theme)
+ (put theme 'theme-feature feature)
+ (when doc (put theme 'theme-documentation doc)))
+
+(defun custom-make-theme-feature (theme)
+ "Given a symbol THEME, create a new symbol by appending \"-theme\".
+Store this symbol in the `theme-feature' property of THEME.
+Calling `provide-theme' to provide THEME actually puts `THEME-theme'
+into `features'.
+
+This allows for a file-name convention for autoloading themes:
+Every theme X has a property `provide-theme' whose value is \"X-theme\".
+\(load-theme X) then attempts to load the file `X-theme.el'."
+ (intern (concat (symbol-name theme) "-theme")))
+\f
+;;; Loading themes.
+
+(defcustom custom-theme-directory
+ (if (eq system-type 'ms-dos)
+ ;; MS-DOS cannot have initial dot.
+ "~/_emacs.d/"
+ "~/.emacs.d/")
+ "Directory in which Custom theme files should be written.
+`load-theme' searches this directory in addition to load-path.
+The command `customize-create-theme' writes the files it produces
+into this directory."
+ :type 'string
+ :group 'customize
+ :version "22.1")
+
+(defun provide-theme (theme)
+ "Indicate that this file provides THEME.
+This calls `provide' to provide the feature name stored in THEME's
+property `theme-feature' (which is usually a symbol created by
+`custom-make-theme-feature')."
+ (if (memq theme '(user changed))
+ (error "Custom theme cannot be named %S" theme))
+ (custom-check-theme theme)
+ (provide (get theme 'theme-feature))
+ ;; Loading a theme also enables it.
+ (push theme custom-enabled-themes)
+ ;; `user' must always be the highest-precedence enabled theme.
+ ;; Make that remain true. (This has the effect of making user settings
+ ;; override the ones just loaded, too.)
+ (let ((custom-enabling-themes t))
+ (enable-theme 'user)))
+
+(defun load-theme (theme)
+ "Load a theme's settings from its file.
+This also enables the theme; use `disable-theme' to disable it."
+ ;; Note we do no check for validity of the theme here.
+ ;; This allows to pull in themes by a file-name convention
+ (interactive "SCustom theme name: ")
+ ;; If reloading, clear out the old theme settings.
+ (when (custom-theme-p theme)
+ (disable-theme theme)
+ (put theme 'theme-settings nil)
+ (put theme 'theme-feature nil)
+ (put theme 'theme-documentation nil))
+ (let ((load-path (if (file-directory-p custom-theme-directory)
+ (cons custom-theme-directory load-path)
+ load-path)))
+ (load (symbol-name (custom-make-theme-feature theme)))))
+\f
+;;; Enabling and disabling loaded themes.
+
+(defvar custom-enabling-themes nil)
+
+(defun enable-theme (theme)
+ "Reenable all variable and face settings defined by THEME.
+The newly enabled theme gets the highest precedence (after `user').
+If it is already enabled, just give it highest precedence (after `user').
+
+If THEME does not specify any theme settings, this tries to load
+the theme from its theme file, by calling `load-theme'."
+ (interactive "SEnable Custom theme: ")
+ (if (not (custom-theme-p theme))
+ (load-theme theme)
+ ;; This could use a bit of optimization -- cyd
+ (let ((settings (get theme 'theme-settings)))
+ (dolist (s settings)
+ (let* ((prop (car s))
+ (symbol (cadr s))
+ (spec-list (get symbol prop)))
+ (put symbol prop (cons (cddr s) (assq-delete-all theme spec-list)))
+ (if (eq prop 'theme-value)
+ (custom-theme-recalc-variable symbol)
+ (custom-theme-recalc-face symbol)))))
+ (unless (eq theme 'user)
+ (setq custom-enabled-themes
+ (cons theme (delq theme custom-enabled-themes)))
+ (unless custom-enabling-themes
+ (enable-theme 'user)))))
+
+(defcustom custom-enabled-themes nil
+ "List of enabled Custom Themes, highest precedence first.
+
+This does not include the `user' theme, which is set by Customize,
+and always takes precedence over other Custom Themes."
+ :group 'customize
+ :type '(repeat symbol)
+ :set (lambda (symbol themes)
+ ;; Avoid an infinite loop when custom-enabled-themes is
+ ;; defined in a theme (e.g. `user'). Enabling the theme sets
+ ;; custom-enabled-themes, which enables the theme...
+ (unless custom-enabling-themes
+ (let ((custom-enabling-themes t) failures)
+ (setq themes (delq 'user (delete-dups themes)))
+ (if (boundp symbol)
+ (dolist (theme (symbol-value symbol))
+ (if (not (memq theme themes))
+ (disable-theme theme))))
+ (dolist (theme (reverse themes))
+ (condition-case nil
+ (enable-theme theme)
+ (error (progn (push theme failures)
+ (setq themes (delq theme themes))))))
+ (enable-theme 'user)
+ (custom-set-default symbol themes)
+ (if failures
+ (message "Failed to enable themes: %s"
+ (mapconcat 'symbol-name failures " ")))))))
+
+(defsubst custom-theme-enabled-p (theme)
+ "Return non-nil if THEME is enabled."
+ (memq theme custom-enabled-themes))
+
+(defun disable-theme (theme)
+ "Disable all variable and face settings defined by THEME.
+See `custom-enabled-themes' for a list of enabled themes."
+ (interactive (list (intern
+ (completing-read
+ "Disable Custom theme: "
+ (mapcar 'symbol-name custom-enabled-themes)
+ nil t))))
+ (when (custom-theme-enabled-p theme)
+ (let ((settings (get theme 'theme-settings)))
+ (dolist (s settings)
+ (let* ((prop (car s))
+ (symbol (cadr s))
+ (spec-list (get symbol prop)))
+ (put symbol prop (assq-delete-all theme spec-list))
+ (if (eq prop 'theme-value)
+ (custom-theme-recalc-variable symbol)
+ (custom-theme-recalc-face symbol)))))
+ (setq custom-enabled-themes
+ (delq theme custom-enabled-themes))))
+
+(defun custom-variable-theme-value (variable)
+ "Return (list VALUE) indicating the custom theme value of VARIABLE.
+That is to say, it specifies what the value should be according to
+currently enabled custom themes.
+
+This function returns nil if no custom theme specifies a value for VARIABLE."
+ (let* ((theme-value (get variable 'theme-value)))
+ (if theme-value
+ (cdr (car theme-value)))))
+
+(defun custom-theme-recalc-variable (variable)
+ "Set VARIABLE according to currently enabled custom themes."
+ (let ((valspec (custom-variable-theme-value variable)))
+ (if valspec
+ (put variable 'saved-value valspec)
+ (setq valspec (get variable 'standard-value)))
+ (if (and valspec
+ (or (get variable 'force-value)
+ (default-boundp variable)))
+ (funcall (or (get variable 'custom-set) 'set-default) variable
+ (eval (car valspec))))))
+
+(defun custom-theme-recalc-face (face)
+ "Set FACE according to currently enabled custom themes."
+ (if (facep face)
+ (let ((theme-faces (reverse (get face 'theme-face))))
+ (dolist (spec theme-faces)
+ (face-spec-set face (cadr spec))))))
+\f
+;;; XEmacs compability functions
+
+;; In XEmacs, when you reset a Custom Theme, you have to specify the
+;; theme to reset it to. We just apply the next available theme, so
+;; just ignore the IGNORED arguments.
+
+(defun custom-theme-reset-variables (theme &rest args)
+ "Reset some variable settings in THEME to their values in other themes.
+Each of the arguments ARGS has this form:
+
+ (VARIABLE IGNORED)
+
+This means reset VARIABLE. (The argument IGNORED is ignored)."
+ (custom-check-theme theme)
+ (dolist (arg args)
+ (custom-push-theme 'theme-value (car arg) theme 'reset)))
+
+(defun custom-reset-variables (&rest args)
+ "Reset the specs of some variables to their values in other themes.
+This creates settings in the `user' theme.
+
+Each of the arguments ARGS has this form:
+
+ (VARIABLE IGNORED)
+
+This means reset VARIABLE. (The argument IGNORED is ignored)."
+ (apply 'custom-theme-reset-variables 'user args))
+