Switch to recommended form of GPLv3 permissions notice.
[bpt/emacs.git] / lisp / custom.el
CommitLineData
55535639
PJ
1;;; custom.el --- tools for declaring and initializing options
2;;
0d30b337 3;; Copyright (C) 1996, 1997, 1999, 2001, 2002, 2003, 2004,
409cc4a3 4;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
55535639
PJ
5;;
6;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
7;; Maintainer: FSF
8;; Keywords: help, faces
9
10;; This file is part of GNU Emacs.
11
eb3fa2cf 12;; GNU Emacs is free software: you can redistribute it and/or modify
55535639 13;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
55535639
PJ
16
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.
21
22;; You should have received a copy of the GNU General Public License
eb3fa2cf 23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
55535639
PJ
24
25;;; Commentary:
26;;
e6bb31f3 27;; This file only contains the code needed to declare and initialize
55535639
PJ
28;; user options. The code to customize options is autoloaded from
29;; `cus-edit.el' and is documented in the Emacs Lisp Reference manual.
30
e6bb31f3 31;; The code implementing face declarations is in `cus-face.el'.
55535639
PJ
32
33;;; Code:
34
35(require 'widget)
36
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.")
40
6da43544
RS
41(defvar custom-dont-initialize nil
42 "Non-nil means `defcustom' should not initialize the variable.
43That is used for the sake of `custom-make-dependencies'.
44Users should not set it.")
45
d3b80e9b
SM
46(defvar custom-current-group-alist nil
47 "Alist of (FILE . GROUP) indicating the current group to use for FILE.")
48
55535639
PJ
49;;; The `defcustom' Macro.
50
51(defun custom-initialize-default (symbol value)
52 "Initialize SYMBOL with VALUE.
53This will do nothing if symbol already has a default binding.
54Otherwise, if symbol has a `saved-value' property, it will evaluate
494bcd27 55the car of that and use it as the default binding for symbol.
55535639
PJ
56Otherwise, VALUE will be evaluated and used as the default binding for
57symbol."
58 (unless (default-boundp symbol)
59 ;; Use the saved value if it exists, otherwise the standard setting.
60 (set-default symbol (if (get symbol 'saved-value)
61 (eval (car (get symbol 'saved-value)))
62 (eval value)))))
63
64(defun custom-initialize-set (symbol value)
65 "Initialize SYMBOL based on VALUE.
66If the symbol doesn't have a default binding already,
67then set it using its `:set' function (or `set-default' if it has none).
68The value is either the value in the symbol's `saved-value' property,
69if any, or VALUE."
70 (unless (default-boundp symbol)
71 (funcall (or (get symbol 'custom-set) 'set-default)
72 symbol
73 (if (get symbol 'saved-value)
74 (eval (car (get symbol 'saved-value)))
75 (eval value)))))
76
ff48d7e6
LT
77(defun custom-initialize-safe-set (symbol value)
78 "Like `custom-initialize-set', but catches errors.
79If an error occurs during initialization, SYMBOL is set to nil
80and no error is thrown. This is meant for use in pre-loaded files
f0856b6f 81where some variables or functions used to compute VALUE may not yet
c94112bf 82be defined. You can then re-evaluate VALUE in startup.el, for instance
a042b6ae 83using `custom-reevaluate-setting'."
ff48d7e6
LT
84 (condition-case nil
85 (custom-initialize-set symbol value)
86 (error (set-default symbol nil))))
87
88(defun custom-initialize-safe-default (symbol value)
89 "Like `custom-initialize-default', but catches errors.
90If an error occurs during initialization, SYMBOL is set to nil
91and no error is thrown. This is meant for use in pre-loaded files
f0856b6f 92where some variables or functions used to compute VALUE may not yet
c94112bf 93be defined. You can then re-evaluate VALUE in startup.el, for instance
a042b6ae 94using `custom-reevaluate-setting'."
ff48d7e6
LT
95 (condition-case nil
96 (custom-initialize-default symbol value)
97 (error (set-default symbol nil))))
98
55535639
PJ
99(defun custom-initialize-reset (symbol value)
100 "Initialize SYMBOL based on VALUE.
101Set the symbol, using its `:set' function (or `set-default' if it has none).
102The value is either the symbol's current value
103 \(as obtained using the `:get' function), if any,
104or the value in the symbol's `saved-value' property if any,
105or (last of all) VALUE."
106 (funcall (or (get symbol 'custom-set) 'set-default)
107 symbol
108 (cond ((default-boundp symbol)
109 (funcall (or (get symbol 'custom-get) 'default-value)
110 symbol))
111 ((get symbol 'saved-value)
112 (eval (car (get symbol 'saved-value))))
113 (t
114 (eval value)))))
115
116(defun custom-initialize-changed (symbol value)
117 "Initialize SYMBOL with VALUE.
118Like `custom-initialize-reset', but only use the `:set' function if
119not using the standard setting.
120For the standard setting, use `set-default'."
121 (cond ((default-boundp symbol)
122 (funcall (or (get symbol 'custom-set) 'set-default)
123 symbol
124 (funcall (or (get symbol 'custom-get) 'default-value)
125 symbol)))
126 ((get symbol 'saved-value)
127 (funcall (or (get symbol 'custom-set) 'set-default)
128 symbol
129 (eval (car (get symbol 'saved-value)))))
130 (t
131 (set-default symbol (eval value)))))
132
133(defun custom-declare-variable (symbol default doc &rest args)
134 "Like `defcustom', but SYMBOL and DEFAULT are evaluated as normal arguments.
135DEFAULT should be an expression to evaluate to compute the default value,
e1bab181
RS
136not the default value itself.
137
8989771d 138DEFAULT is stored as SYMBOL's standard value, in SYMBOL's property
e1bab181
RS
139`standard-value'. At the same time, SYMBOL's property `force-value' is
140set to nil, as the value is no longer rogue."
55535639
PJ
141 (put symbol 'standard-value (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 (put symbol 'variable-documentation doc))
147 (let ((initialize 'custom-initialize-reset)
148 (requests nil))
d3b80e9b
SM
149 (unless (memq :group args)
150 (custom-add-to-group (custom-current-group) symbol 'custom-variable))
55535639
PJ
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)
6f0d61bf 168 (push value requests))
fd045a34
GM
169 ((eq keyword :risky)
170 (put symbol 'risky-local-variable value))
171 ((eq keyword :safe)
172 (put symbol 'safe-local-variable value))
55535639
PJ
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))
6f0d61bf 180 value)
55535639
PJ
181 ;; Fast code for the common case.
182 (put symbol 'custom-options (copy-sequence value))))
183 (t
184 (custom-handle-keyword symbol keyword value
185 'custom-variable))))))
186 (put symbol 'custom-requests requests)
187 ;; Do the actual initialization.
6da43544
RS
188 (unless custom-dont-initialize
189 (funcall initialize symbol default)))
80888260 190 (push symbol current-load-list)
55535639
PJ
191 (run-hooks 'custom-define-hook)
192 symbol)
193
194(defmacro defcustom (symbol value doc &rest args)
195 "Declare SYMBOL as a customizable variable that defaults to VALUE.
196DOC is the variable documentation.
197
494bcd27 198Neither SYMBOL nor VALUE need to be quoted.
55535639
PJ
199If SYMBOL is not already bound, initialize it to VALUE.
200The remaining arguments should have the form
201
202 [KEYWORD VALUE]...
203
204The following keywords are meaningful:
205
693d0bd3 206:type VALUE should be a widget type for editing the symbol's value.
55535639 207:options VALUE should be a list of valid members of the widget type.
81117bdd
BW
208:initialize
209 VALUE should be a function used to initialize the
210 variable. It takes two arguments, the symbol and value
211 given in the `defcustom' call. The default is
212 `custom-initialize-reset'.
213:set VALUE should be a function to set the value of the symbol.
214 It takes two arguments, the symbol to set and the value to
f2276b69 215 give it. The default choice of function is `set-default'.
81117bdd
BW
216:get VALUE should be a function to extract the value of symbol.
217 The function takes one argument, a symbol, and should return
218 the current value for that symbol. The default choice of function
f2276b69 219 is `default-value'.
81117bdd
BW
220:require
221 VALUE should be a feature symbol. If you save a value
222 for this option, then when your `.emacs' file loads the value,
223 it does (require VALUE) first.
fd045a34
GM
224:risky Set SYMBOL's `risky-local-variable' property to VALUE.
225:safe Set SYMBOL's `safe-local-variable' property to VALUE.
81117bdd
BW
226
227The following common keywords are also meaningful.
228
55535639 229:group VALUE should be a customization group.
81117bdd 230 Add SYMBOL (or FACE with `defface') to that group.
3dc5f18e 231:link LINK-DATA
00644b82
EZ
232 Include an external link after the documentation string for this
233 item. This is a sentence containing an active field which
234 references some other documentation.
71296446 235
11ee7d4e 236 There are several alternatives you can use for LINK-DATA:
71296446 237
3dc5f18e 238 (custom-manual INFO-NODE)
00644b82 239 Link to an Info node; INFO-NODE is a string which specifies
11ee7d4e 240 the node name, as in \"(emacs)Top\".
71296446 241
3dc5f18e 242 (info-link INFO-NODE)
00644b82
EZ
243 Like `custom-manual' except that the link appears in the
244 customization buffer with the Info node name.
71296446 245
3dc5f18e 246 (url-link URL)
00644b82 247 Link to a web page; URL is a string which specifies the URL.
11ee7d4e
JL
248
249 (emacs-commentary-link LIBRARY)
250 Link to the commentary section of LIBRARY.
251
252 (emacs-library-link LIBRARY)
253 Link to an Emacs Lisp LIBRARY file.
254
255 (file-link FILE)
256 Link to FILE.
257
258 (function-link FUNCTION)
259 Link to the documentation of FUNCTION.
260
261 (variable-link VARIABLE)
262 Link to the documentation of VARIABLE.
263
264 (custom-group-link GROUP)
265 Link to another customization GROUP.
71296446 266
00644b82
EZ
267 You can specify the text to use in the customization buffer by
268 adding `:tag NAME' after the first element of the LINK-DATA; for
3dc5f18e 269 example, (info-link :tag \"foo\" \"(emacs)Top\") makes a link to the
00644b82 270 Emacs manual which appears in the buffer as `foo'.
71296446 271
00644b82
EZ
272 An item can have more than one external link; however, most items
273 have none at all.
55535639
PJ
274:version
275 VALUE should be a string specifying that the variable was
276 first introduced, or its default value was changed, in Emacs
277 version VERSION.
4e9c705e 278:package-version
994019df 279 VALUE should be a list with the form (PACKAGE . VERSION)
4e9c705e
BW
280 specifying that the variable was first introduced, or its
281 default value was changed, in PACKAGE version VERSION. This
282 keyword takes priority over :version. The PACKAGE and VERSION
283 must appear in the alist `customize-package-emacs-version-alist'.
994019df
BW
284 Since PACKAGE must be unique and the user might see it in an
285 error message, a good choice is the official name of the
286 package, such as MH-E or Gnus.
00644b82
EZ
287:tag LABEL
288 Use LABEL, a string, instead of the item's name, to label the item
1c1da418 289 in customization menus and buffers.
00644b82
EZ
290:load FILE
291 Load file FILE (a string) before displaying this customization
29512a0f 292 item. Loading is done with `load', and only if the file is
00644b82 293 not already loaded.
64e9f9d9
DL
294:set-after VARIABLES
295 Specifies that SYMBOL should be set after the list of variables
296 VARIABLES when both have been customized.
55535639 297
19229e5e
LT
298If SYMBOL has a local binding, then this form affects the local
299binding. This is normally not what you want. Thus, if you need
300to load a file defining variables with this form, or with
301`defvar' or `defconst', you should always load that file
302_outside_ any bindings for these variables. \(`defvar' and
303`defconst' behave similarly in this respect.)
304
81117bdd
BW
305See Info node `(elisp) Customization' in the Emacs Lisp manual
306for more information."
67a60caa 307 (declare (doc-string 3))
55535639
PJ
308 ;; It is better not to use backquote in this file,
309 ;; because that makes a bootstrapping problem
310 ;; if you need to recompile all the Lisp files using interpreted code.
311 (nconc (list 'custom-declare-variable
312 (list 'quote symbol)
313 (list 'quote value)
314 doc)
315 args))
316
317;;; The `defface' Macro.
318
319(defmacro defface (face spec doc &rest args)
320 "Declare FACE as a customizable face that defaults to SPEC.
321FACE does not need to be quoted.
322
323Third argument DOC is the face documentation.
324
2a69edce 325If FACE has been set with `custom-set-faces', set the face attributes
55535639
PJ
326as specified by that function, otherwise set the face attributes
327according to SPEC.
328
329The remaining arguments should have the form
330
331 [KEYWORD VALUE]...
332
81117bdd
BW
333For a list of valid keywords, see the common keywords listed in
334`defcustom'.
55535639
PJ
335
336SPEC should be an alist of the form ((DISPLAY ATTS)...).
337
ebdaed17 338In the first element, DISPLAY can be `default'. The ATTS in that
9c27debd
RS
339element then act as defaults for all the following elements.
340
341Aside from that, DISPLAY specifies conditions to match some or
342all frames. For each frame, the first element of SPEC where the
343DISPLAY conditions are satisfied is the one that applies to that
344frame. The ATTRs in this element take effect, and the following
345elements are ignored, on that frame.
346
347In the last element, DISPLAY can be t. That element applies to a
ebdaed17 348frame if none of the previous elements (except the `default' if
9c27debd 349any) did.
55535639
PJ
350
351ATTS is a list of face attributes followed by their values:
352 (ATTR VALUE ATTR VALUE...)
353
354The possible attributes are `:family', `:width', `:height', `:weight',
355`:slant', `:underline', `:overline', `:strike-through', `:box',
3d58b15e 356`:foreground', `:background', `:stipple', `:inverse-video', and `:inherit'.
55535639 357
ebdaed17 358DISPLAY can be `default' (only in the first element), the symbol
9c27debd
RS
359t (only in the last element) to match all frames, or an alist of
360conditions of the form \(REQ ITEM...). For such an alist to
361match a frame, each of the conditions must be satisfied, meaning
362that the REQ property of the frame must match one of the
363corresponding ITEMs. These are the defined REQ values:
55535639
PJ
364
365`type' (the value of `window-system')
366 Under X, in addition to the values `window-system' can take,
9b44aa8b
JD
367 `motif', `lucid', `gtk' and `x-toolkit' are allowed, and match when
368 the Motif toolkit, Lucid toolkit, GTK toolkit or any X toolkit is in use.
55535639
PJ
369
370`class' (the frame's color support)
371 Should be one of `color', `grayscale', or `mono'.
372
373`background' (what color is used for the background text)
374 Should be one of `light' or `dark'.
375
6e166990
EZ
376`min-colors' (the minimum number of colors the frame should support)
377 Should be an integer, it is compared with the result of
378 `display-color-cells'.
379
ec85856c
JL
380`supports' (only match frames that support the specified face attributes)
381 Should be a list of face attributes. See the documentation for
382 the function `display-supports-face-attributes-p' for more
383 information on exactly how testing is done.
384
81117bdd
BW
385See Info node `(elisp) Customization' in the Emacs Lisp manual
386for more information."
67a60caa 387 (declare (doc-string 3))
55535639
PJ
388 ;; It is better not to use backquote in this file,
389 ;; because that makes a bootstrapping problem
390 ;; if you need to recompile all the Lisp files using interpreted code.
391 (nconc (list 'custom-declare-face (list 'quote face) spec doc) args))
392
393;;; The `defgroup' Macro.
394
d3b80e9b
SM
395(defun custom-current-group ()
396 (cdr (assoc load-file-name custom-current-group-alist)))
397
55535639
PJ
398(defun custom-declare-group (symbol members doc &rest args)
399 "Like `defgroup', but SYMBOL is evaluated as a normal argument."
400 (while members
401 (apply 'custom-add-to-group symbol (car members))
402 (setq members (cdr members)))
55535639
PJ
403 (when doc
404 ;; This text doesn't get into DOC.
405 (put symbol 'group-documentation (purecopy doc)))
406 (while args
407 (let ((arg (car args)))
408 (setq args (cdr args))
409 (unless (symbolp arg)
410 (error "Junk in args %S" args))
411 (let ((keyword arg)
412 (value (car args)))
413 (unless args
414 (error "Keyword %s is missing an argument" keyword))
415 (setq args (cdr args))
416 (cond ((eq keyword :prefix)
417 (put symbol 'custom-prefix value))
418 (t
419 (custom-handle-keyword symbol keyword value
420 'custom-group))))))
d3b80e9b
SM
421 ;; Record the group on the `current' list.
422 (let ((elt (assoc load-file-name custom-current-group-alist)))
423 (if elt (setcdr elt symbol)
424 (push (cons load-file-name symbol) custom-current-group-alist)))
55535639
PJ
425 (run-hooks 'custom-define-hook)
426 symbol)
427
428(defmacro defgroup (symbol members doc &rest args)
429 "Declare SYMBOL as a customization group containing MEMBERS.
430SYMBOL does not need to be quoted.
431
432Third arg DOC is the group documentation.
433
434MEMBERS should be an alist of the form ((NAME WIDGET)...) where
435NAME is a symbol and WIDGET is a widget for editing that symbol.
436Useful widgets are `custom-variable' for editing variables,
437`custom-face' for edit faces, and `custom-group' for editing groups.
438
439The remaining arguments should have the form
440
441 [KEYWORD VALUE]...
442
81117bdd
BW
443For a list of valid keywords, see the common keywords listed in
444`defcustom'.
55535639 445
81117bdd
BW
446See Info node `(elisp) Customization' in the Emacs Lisp manual
447for more information."
45aacac6 448 (declare (doc-string 3))
55535639
PJ
449 ;; It is better not to use backquote in this file,
450 ;; because that makes a bootstrapping problem
451 ;; if you need to recompile all the Lisp files using interpreted code.
452 (nconc (list 'custom-declare-group (list 'quote symbol) members doc) args))
453
454(defun custom-add-to-group (group option widget)
455 "To existing GROUP add a new OPTION of type WIDGET.
456If there already is an entry for OPTION and WIDGET, nothing is done."
457 (let ((members (get group 'custom-group))
458 (entry (list option widget)))
459 (unless (member entry members)
460 (put group 'custom-group (nconc members (list entry))))))
461
29512a0f
SM
462(defun custom-group-of-mode (mode)
463 "Return the custom group corresponding to the major or minor MODE.
464If no such group is found, return nil."
465 (or (get mode 'custom-mode-group)
466 (if (or (get mode 'custom-group)
467 (and (string-match "-mode\\'" (symbol-name mode))
468 (get (setq mode (intern (substring (symbol-name mode)
469 0 (match-beginning 0))))
470 'custom-group)))
471 mode)))
472
55535639
PJ
473;;; Properties.
474
475(defun custom-handle-all-keywords (symbol args type)
476 "For customization option SYMBOL, handle keyword arguments ARGS.
477Third argument TYPE is the custom option type."
d3b80e9b 478 (unless (memq :group args)
9b6098b9 479 (custom-add-to-group (custom-current-group) symbol type))
55535639
PJ
480 (while args
481 (let ((arg (car args)))
482 (setq args (cdr args))
483 (unless (symbolp arg)
484 (error "Junk in args %S" args))
485 (let ((keyword arg)
486 (value (car args)))
487 (unless args
488 (error "Keyword %s is missing an argument" keyword))
489 (setq args (cdr args))
490 (custom-handle-keyword symbol keyword value type)))))
491
492(defun custom-handle-keyword (symbol keyword value type)
493 "For customization option SYMBOL, handle KEYWORD with VALUE.
494Fourth argument TYPE is the custom option type."
495 (if purify-flag
496 (setq value (purecopy value)))
497 (cond ((eq keyword :group)
498 (custom-add-to-group value symbol type))
499 ((eq keyword :version)
500 (custom-add-version symbol value))
4e9c705e
BW
501 ((eq keyword :package-version)
502 (custom-add-package-version symbol value))
55535639
PJ
503 ((eq keyword :link)
504 (custom-add-link symbol value))
505 ((eq keyword :load)
506 (custom-add-load symbol value))
507 ((eq keyword :tag)
508 (put symbol 'custom-tag value))
509 ((eq keyword :set-after)
510 (custom-add-dependencies symbol value))
511 (t
512 (error "Unknown keyword %s" keyword))))
513
514(defun custom-add-dependencies (symbol value)
515 "To the custom option SYMBOL, add dependencies specified by VALUE.
516VALUE should be a list of symbols. For each symbol in that list,
517this specifies that SYMBOL should be set after the specified symbol, if
518both appear in constructs like `custom-set-variables'."
519 (unless (listp value)
520 (error "Invalid custom dependency `%s'" value))
521 (let* ((deps (get symbol 'custom-dependencies))
522 (new-deps deps))
523 (while value
524 (let ((dep (car value)))
525 (unless (symbolp dep)
526 (error "Invalid custom dependency `%s'" dep))
527 (unless (memq dep new-deps)
528 (setq new-deps (cons dep new-deps)))
529 (setq value (cdr value))))
530 (unless (eq deps new-deps)
531 (put symbol 'custom-dependencies new-deps))))
e1bab181 532
55535639
PJ
533(defun custom-add-option (symbol option)
534 "To the variable SYMBOL add OPTION.
535
a4842ffa
RS
536If SYMBOL's custom type is a hook, OPTION should be a hook member.
537If SYMBOL's custom type is an alist, OPTION specifies a symbol
538to offer to the user as a possible key in the alist.
539For other custom types, this has no effect."
55535639
PJ
540 (let ((options (get symbol 'custom-options)))
541 (unless (member option options)
542 (put symbol 'custom-options (cons option options)))))
467064a4 543(defalias 'custom-add-frequent-value 'custom-add-option)
55535639
PJ
544
545(defun custom-add-link (symbol widget)
546 "To the custom option SYMBOL add the link WIDGET."
547 (let ((links (get symbol 'custom-links)))
548 (unless (member widget links)
549 (put symbol 'custom-links (cons (purecopy widget) links)))))
550
551(defun custom-add-version (symbol version)
552 "To the custom option SYMBOL add the version VERSION."
553 (put symbol 'custom-version (purecopy version)))
554
4e9c705e
BW
555(defun custom-add-package-version (symbol version)
556 "To the custom option SYMBOL add the package version VERSION."
557 (put symbol 'custom-package-version (purecopy version)))
558
55535639
PJ
559(defun custom-add-load (symbol load)
560 "To the custom option SYMBOL add the dependency LOAD.
561LOAD should be either a library file name, or a feature name."
562 (let ((loads (get symbol 'custom-loads)))
563 (unless (member load loads)
564 (put symbol 'custom-loads (cons (purecopy load) loads)))))
565
d54fbdfd
SM
566(defun custom-autoload (symbol load &optional noset)
567 "Mark SYMBOL as autoloaded custom variable and add dependency LOAD.
568If NOSET is non-nil, don't bother autoloading LOAD when setting the variable."
569 (put symbol 'custom-autoload (if noset 'noset t))
1669290d
MR
570 (custom-add-load symbol load))
571
572;; This test is also in the C code of `user-variable-p'.
573(defun custom-variable-p (variable)
289e1999
LT
574 "Return non-nil if VARIABLE is a custom variable.
575This recursively follows aliases."
576 (setq variable (indirect-variable variable))
1669290d
MR
577 (or (get variable 'standard-value)
578 (get variable 'custom-autoload)))
579
400b960e
RS
580(defun custom-note-var-changed (variable)
581 "Inform Custom that VARIABLE has been set (changed).
582VARIABLE is a symbol that names a user option.
583The result is that the change is treated as having been made through Custom."
400b960e 584 (put variable 'customized-value (list (custom-quote (eval variable)))))
06f5c483
JL
585
586\f
587;;; Custom Themes
400b960e 588
8f772dfd
RS
589;;; Loading files needed to customize a symbol.
590;;; This is in custom.el because menu-bar.el needs it for toggle cmds.
591
592(defvar custom-load-recursion nil
593 "Hack to avoid recursive dependencies.")
594
595(defun custom-load-symbol (symbol)
596 "Load all dependencies for SYMBOL."
597 (unless custom-load-recursion
29512a0f 598 (let ((custom-load-recursion t))
a8c78057
RS
599 ;; Load these files if not already done,
600 ;; to make sure we know all the dependencies of SYMBOL.
601 (condition-case nil
602 (require 'cus-load)
603 (error nil))
604 (condition-case nil
605 (require 'cus-start)
606 (error nil))
29512a0f
SM
607 (dolist (load (get symbol 'custom-loads))
608 (cond ((symbolp load) (condition-case nil (require load) (error nil)))
609 ;; This is subsumed by the test below, but it's much faster.
8f772dfd
RS
610 ((assoc load load-history))
611 ;; This was just (assoc (locate-library load) load-history)
612 ;; but has been optimized not to load locate-library
613 ;; if not necessary.
29512a0f
SM
614 ((let ((regexp (concat "\\(\\`\\|/\\)" (regexp-quote load)
615 "\\(\\'\\|\\.\\)"))
616 (found nil))
8f772dfd 617 (dolist (loaded load-history)
c3891447
RS
618 (and (stringp (car loaded))
619 (string-match regexp (car loaded))
8f772dfd
RS
620 (setq found t)))
621 found))
622 ;; Without this, we would load cus-edit recursively.
623 ;; We are still loading it when we call this,
624 ;; and it is not in load-history yet.
625 ((equal load "cus-edit"))
29512a0f 626 (t (condition-case nil (load load) (error nil))))))))
46ce5feb 627\f
c2e2f9be
CY
628(defvar custom-local-buffer nil
629 "Non-nil, in a Customization buffer, means customize a specific buffer.
630If this variable is non-nil, it should be a buffer,
631and it means customize the local bindings of that buffer.
632This variable is a permanent local, and it normally has a local binding
633in every Customization buffer.")
634(put 'custom-local-buffer 'permanent-local t)
635
636(defun custom-set-default (variable value)
637 "Default :set function for a customizable variable.
638Normally, this sets the default value of VARIABLE to VALUE,
639but if `custom-local-buffer' is non-nil,
640this sets the local binding in that buffer instead."
641 (if custom-local-buffer
642 (with-current-buffer custom-local-buffer
643 (set variable value))
644 (set-default variable value)))
645
646(defun custom-set-minor-mode (variable value)
647 ":set function for minor mode variables.
648Normally, this sets the default value of VARIABLE to nil if VALUE
649is nil and to t otherwise,
650but if `custom-local-buffer' is non-nil,
651this sets the local binding in that buffer instead."
652 (if custom-local-buffer
653 (with-current-buffer custom-local-buffer
654 (funcall variable (if value 1 0)))
655 (funcall variable (if value 1 0))))
656
657(defun custom-quote (sexp)
4837b516 658 "Quote SEXP if it is not self quoting."
c2e2f9be
CY
659 (if (or (memq sexp '(t nil))
660 (keywordp sexp)
661 (and (listp sexp)
662 (memq (car sexp) '(lambda)))
663 (stringp sexp)
664 (numberp sexp)
665 (vectorp sexp)
666;;; (and (fboundp 'characterp)
667;;; (characterp sexp))
668 )
669 sexp
670 (list 'quote sexp)))
671
672(defun customize-mark-to-save (symbol)
673 "Mark SYMBOL for later saving.
674
675If the default value of SYMBOL is different from the standard value,
676set the `saved-value' property to a list whose car evaluates to the
677default value. Otherwise, set it to nil.
678
679To actually save the value, call `custom-save-all'.
680
4837b516 681Return non-nil if the `saved-value' property actually changed."
ec9f0a62 682 (custom-load-symbol symbol)
c2e2f9be
CY
683 (let* ((get (or (get symbol 'custom-get) 'default-value))
684 (value (funcall get symbol))
685 (saved (get symbol 'saved-value))
686 (standard (get symbol 'standard-value))
687 (comment (get symbol 'customized-variable-comment)))
4837b516 688 ;; Save default value if different from standard value.
c2e2f9be
CY
689 (if (or (null standard)
690 (not (equal value (condition-case nil
691 (eval (car standard))
692 (error nil)))))
693 (put symbol 'saved-value (list (custom-quote value)))
694 (put symbol 'saved-value nil))
695 ;; Clear customized information (set, but not saved).
696 (put symbol 'customized-value nil)
697 ;; Save any comment that might have been set.
698 (when comment
699 (put symbol 'saved-variable-comment comment))
700 (not (equal saved (get symbol 'saved-value)))))
701
702(defun customize-mark-as-set (symbol)
703 "Mark current value of SYMBOL as being set from customize.
704
705If the default value of SYMBOL is different from the saved value if any,
706or else if it is different from the standard value, set the
707`customized-value' property to a list whose car evaluates to the
708default value. Otherwise, set it to nil.
709
4837b516 710Return non-nil if the `customized-value' property actually changed."
ec9f0a62 711 (custom-load-symbol symbol)
c2e2f9be
CY
712 (let* ((get (or (get symbol 'custom-get) 'default-value))
713 (value (funcall get symbol))
714 (customized (get symbol 'customized-value))
715 (old (or (get symbol 'saved-value) (get symbol 'standard-value))))
4837b516 716 ;; Mark default value as set if different from old value.
d54fbdfd
SM
717 (if (not (and old
718 (equal value (condition-case nil
719 (eval (car old))
720 (error nil)))))
fccf2784
CY
721 (progn (put symbol 'customized-value (list (custom-quote value)))
722 (custom-push-theme 'theme-value symbol 'user 'set
723 (custom-quote value)))
c2e2f9be
CY
724 (put symbol 'customized-value nil))
725 ;; Changed?
726 (not (equal customized (get symbol 'customized-value)))))
727
728(defun custom-reevaluate-setting (symbol)
729 "Reset the value of SYMBOL by re-evaluating its saved or standard value.
730Use the :set function to do so. This is useful for customizable options
731that are defined before their standard value can really be computed.
732E.g. dumped variables whose default depends on run-time information."
733 (funcall (or (get symbol 'custom-set) 'set-default)
734 symbol
735 (eval (car (or (get symbol 'saved-value) (get symbol 'standard-value))))))
736
737\f
d358aa10
CY
738;;; Custom Themes
739
740;; Custom themes are collections of settings that can be enabled or
741;; disabled as a unit.
742
743;; Each Custom theme is defined by a symbol, called the theme name.
744;; The `theme-settings' property of the theme name records the
745;; variable and face settings of the theme. This property is a list
746;; of elements, each of the form
747;;
748;; (PROP SYMBOL THEME VALUE)
749;;
750;; - PROP is either `theme-value' or `theme-face'
751;; - SYMBOL is the face or variable name
752;; - THEME is the theme name (redundant, but simplifies the code)
753;; - VALUE is an expression that gives the theme's setting for SYMBOL.
754;;
755;; The theme name also has a `theme-feature' property, whose value is
756;; specified when the theme is defined (see `custom-declare-theme').
757;; Usually, this is just a symbol named THEME-theme. This lets
758;; external libraries call (require 'foo-theme).
759
760;; In addition, each symbol (either a variable or a face) affected by
761;; an *enabled* theme has a `theme-value' or `theme-face' property,
762;; which is a list of elements each of the form
763;;
764;; (THEME VALUE)
765;;
766;; which have the same meanings as in `theme-settings'.
767;;
768;; The `theme-value' and `theme-face' lists are ordered by decreasing
769;; theme precedence. Thus, the first element is always the one that
770;; is in effect.
771
772;; Each theme is stored in a theme file, with filename THEME-theme.el.
773;; Loading a theme basically involves calling (load "THEME-theme")
774;; This is done by the function `load-theme'. Loading a theme
775;; automatically enables it.
776;;
777;; When a theme is enabled, the `theme-value' and `theme-face'
778;; properties for the affected symbols are set. When a theme is
779;; disabled, its settings are removed from the `theme-value' and
780;; `theme-face' properties, but the theme's own `theme-settings'
781;; property remains unchanged.
782
d358aa10 783(defvar custom-known-themes '(user changed)
9c4b6e94 784 "Themes that have been defined with `deftheme'.
d358aa10 785The default value is the list (user changed). The theme `changed'
79a0aa11 786contains the settings before custom themes are applied. The
9b05fb22 787theme `user' contains all the settings the user customized and saved.
e1bab181
RS
788Additional themes declared with the `deftheme' macro will be added to
789the front of this list.")
790
e1bab181
RS
791(defsubst custom-theme-p (theme)
792 "Non-nil when THEME has been defined."
793 (memq theme custom-known-themes))
794
795(defsubst custom-check-theme (theme)
796 "Check whether THEME is valid, and signal an error if it is not."
797 (unless (custom-theme-p theme)
798 (error "Unknown theme `%s'" theme)))
799
d358aa10
CY
800(defun custom-push-theme (prop symbol theme mode &optional value)
801 "Record VALUE for face or variable SYMBOL in custom theme THEME.
802PROP is `theme-face' for a face, `theme-value' for a variable.
e1bab181
RS
803
804MODE can be either the symbol `set' or the symbol `reset'. If it is the
805symbol `set', then VALUE is the value to use. If it is the symbol
d358aa10 806`reset', then SYMBOL will be removed from THEME (VALUE is ignored).
e1bab181 807
e1bab181 808See `custom-known-themes' for a list of known themes."
b2a41d12 809 (unless (memq prop '(theme-value theme-face))
d820f1fb 810 (error "Unknown theme property"))
46ce5feb 811 (let* ((old (get symbol prop))
d358aa10
CY
812 (setting (assq theme old)) ; '(theme value)
813 (theme-settings ; '(prop symbol theme value)
814 (get theme 'theme-settings)))
815 (if (eq mode 'reset)
d820f1fb
CY
816 ;; Remove a setting.
817 (when setting
818 (let (res)
819 (dolist (theme-setting theme-settings)
820 (if (and (eq (car theme-setting) prop)
821 (eq (cadr theme-setting) symbol))
822 (setq res theme-setting)))
823 (put theme 'theme-settings (delq res theme-settings)))
824 (put symbol prop (delq setting old)))
825 (if setting
826 ;; Alter an existing setting.
827 (let (res)
828 (dolist (theme-setting theme-settings)
829 (if (and (eq (car theme-setting) prop)
830 (eq (cadr theme-setting) symbol))
831 (setq res theme-setting)))
832 (put theme 'theme-settings
d358aa10 833 (cons (list prop symbol theme value)
d820f1fb 834 (delq res theme-settings)))
d358aa10 835 (setcar (cdr setting) value))
d820f1fb
CY
836 ;; Add a new setting.
837 ;; If the user changed the value outside of Customize, we
d358aa10 838 ;; first save the current value to a fake theme, `changed'.
d820f1fb
CY
839 ;; This ensures that the user-set value comes back if the
840 ;; theme is later disabled.
841 (if (null old)
842 (if (and (eq prop 'theme-value)
e58d8ef5
CY
843 (boundp symbol))
844 (let ((sv (get symbol 'standard-value)))
d54fbdfd
SM
845 (unless (and sv
846 (equal (eval (car sv)) (symbol-value symbol)))
847 (setq old (list (list 'changed (symbol-value symbol))))))
e02577b7
CY
848 (if (and (facep symbol)
849 (not (face-spec-match-p symbol (get symbol 'face-defface-spec))))
d358aa10 850 (setq old (list (list 'changed (list
d820f1fb 851 (append '(t) (custom-face-attributes-get symbol nil)))))))))
d358aa10 852 (put symbol prop (cons (list theme value) old))
d820f1fb 853 (put theme 'theme-settings
d358aa10 854 (cons (list prop symbol theme value)
d820f1fb 855 theme-settings))))))
55535639 856
c2e2f9be 857\f
55535639 858(defun custom-set-variables (&rest args)
f1a262ed
RS
859 "Install user customizations of variable values specified in ARGS.
860These settings are registered as theme `user'.
64e9f9d9 861The arguments should each be a list of the form:
55535639 862
f1a262ed 863 (SYMBOL EXP [NOW [REQUEST [COMMENT]]])
55535639 864
f1a262ed
RS
865This stores EXP (without evaluating it) as the saved value for SYMBOL.
866If NOW is present and non-nil, then also evaluate EXP and set
867the default value for the SYMBOL to the value of EXP.
e1bab181 868
f1a262ed
RS
869REQUEST is a list of features we must require in order to
870handle SYMBOL properly.
55535639 871COMMENT is a comment string about SYMBOL."
e1bab181
RS
872 (apply 'custom-theme-set-variables 'user args))
873
874(defun custom-theme-set-variables (theme &rest args)
f1a262ed
RS
875 "Initialize variables for theme THEME according to settings in ARGS.
876Each of the arguments in ARGS should be a list of this form:
e1bab181 877
f1a262ed 878 (SYMBOL EXP [NOW [REQUEST [COMMENT]]])
e1bab181 879
f1a262ed
RS
880This stores EXP (without evaluating it) as the saved value for SYMBOL.
881If NOW is present and non-nil, then also evaluate EXP and set
882the default value for the SYMBOL to the value of EXP.
e1bab181 883
f1a262ed
RS
884REQUEST is a list of features we must require in order to
885handle SYMBOL properly.
e1bab181
RS
886COMMENT is a comment string about SYMBOL.
887
f1a262ed 888EXP itself is saved unevaluated as SYMBOL property `saved-value' and
e1bab181
RS
889in SYMBOL's list property `theme-value' \(using `custom-push-theme')."
890 (custom-check-theme theme)
9277ee6c
SM
891
892 ;; Process all the needed autoloads before anything else, so that the
893 ;; subsequent code has all the info it needs (e.g. which var corresponds
894 ;; to a minor mode), regardless of the ordering of the variables.
895 (dolist (entry args)
896 (let* ((symbol (indirect-variable (nth 0 entry))))
897 (unless (or (get symbol 'standard-value)
898 (memq (get symbol 'custom-autoload) '(nil noset)))
899 ;; This symbol needs to be autoloaded, even just for a `set'.
900 (custom-load-symbol symbol))))
1e281896 901
9277ee6c 902 ;; Move minor modes and variables with explicit requires to the end.
059290d6
SM
903 (setq args
904 (sort args
905 (lambda (a1 a2)
906 (let* ((sym1 (car a1))
907 (sym2 (car a2))
908 (1-then-2 (memq sym1 (get sym2 'custom-dependencies)))
909 (2-then-1 (memq sym2 (get sym1 'custom-dependencies))))
910 (cond ((and 1-then-2 2-then-1)
911 (error "Circular custom dependency between `%s' and `%s'"
912 sym1 sym2))
913 (2-then-1 nil)
3d0c2a29
GM
914 ;; 1 is a dependency of 2, so needs to be set first.
915 (1-then-2)
b6790c3e
SM
916 ;; Put minor modes and symbols with :require last.
917 ;; Putting minor modes last ensures that the mode
918 ;; function will see other customized values rather
919 ;; than default values.
920 (t (or (nth 3 a2)
921 (eq (get sym2 'custom-set)
922 'custom-set-minor-mode))))))))
059290d6
SM
923 (while args
924 (let ((entry (car args)))
925 (if (listp entry)
926 (let* ((symbol (indirect-variable (nth 0 entry)))
927 (value (nth 1 entry))
928 (now (nth 2 entry))
929 (requests (nth 3 entry))
930 (comment (nth 4 entry))
931 set)
932 (when requests
933 (put symbol 'custom-requests requests)
934 (mapc 'require requests))
935 (setq set (or (get symbol 'custom-set) 'custom-set-default))
936 (put symbol 'saved-value (list value))
937 (put symbol 'saved-variable-comment comment)
938 (custom-push-theme 'theme-value symbol theme 'set value)
939 ;; Allow for errors in the case where the setter has
55535639
PJ
940 ;; changed between versions, say, but let the user know.
941 (condition-case data
942 (cond (now
943 ;; Rogue variable, set it now.
944 (put symbol 'force-value t)
945 (funcall set symbol (eval value)))
946 ((default-boundp symbol)
947 ;; Something already set this, overwrite it.
948 (funcall set symbol (eval value))))
71296446 949 (error
55535639 950 (message "Error setting %s: %s" symbol data)))
059290d6
SM
951 (setq args (cdr args))
952 (and (or now (default-boundp symbol))
953 (put symbol 'variable-comment comment)))
d54fbdfd
SM
954 ;; I believe this is dead-code, because the `sort' code above would
955 ;; have burped before we could get here. --Stef
059290d6
SM
956 ;; Old format, a plist of SYMBOL VALUE pairs.
957 (message "Warning: old format `custom-set-variables'")
958 (ding)
959 (sit-for 2)
960 (let ((symbol (indirect-variable (nth 0 args)))
961 (value (nth 1 args)))
962 (put symbol 'saved-value (list value))
963 (custom-push-theme 'theme-value symbol theme 'set value))
964 (setq args (cdr (cdr args)))))))
55535639 965
46ce5feb
RS
966\f
967;;; Defining themes.
968
d358aa10
CY
969;; A theme file should be named `THEME-theme.el' (where THEME is the theme
970;; name), and found in either `custom-theme-directory' or the load path.
971;; It has the following format:
972;;
973;; (deftheme THEME
974;; DOCSTRING)
975;;
976;; (custom-theme-set-variables
977;; 'THEME
978;; [THEME-VARIABLES])
979;;
980;; (custom-theme-set-faces
981;; 'THEME
982;; [THEME-FACES])
983;;
984;; (provide-theme 'THEME)
46ce5feb 985
46ce5feb 986
d358aa10
CY
987;; The IGNORED arguments to deftheme come from the XEmacs theme code, where
988;; they were used to supply keyword-value pairs like `:immediate',
989;; `:variable-reset-string', etc. We don't use any of these, so ignore them.
6d912ee1 990
d358aa10
CY
991(defmacro deftheme (theme &optional doc &rest ignored)
992 "Declare THEME to be a Custom theme.
993The optional argument DOC is a doc string describing the theme.
46ce5feb
RS
994
995Any theme `foo' should be defined in a file called `foo-theme.el';
996see `custom-make-theme-feature' for more information."
997 (let ((feature (custom-make-theme-feature theme)))
998 ;; It is better not to use backquote in this file,
999 ;; because that makes a bootstrapping problem
1000 ;; if you need to recompile all the Lisp files using interpreted code.
d358aa10 1001 (list 'custom-declare-theme (list 'quote theme) (list 'quote feature) doc)))
46ce5feb 1002
d358aa10 1003(defun custom-declare-theme (theme feature &optional doc &rest ignored)
46ce5feb 1004 "Like `deftheme', but THEME is evaluated as a normal argument.
d358aa10
CY
1005FEATURE is the feature this theme provides. Normally, this is a symbol
1006created from THEME by `custom-make-theme-feature'."
1007 (if (memq theme '(user changed))
1008 (error "Custom theme cannot be named %S" theme))
46ce5feb
RS
1009 (add-to-list 'custom-known-themes theme)
1010 (put theme 'theme-feature feature)
d358aa10 1011 (when doc (put theme 'theme-documentation doc)))
46ce5feb
RS
1012
1013(defun custom-make-theme-feature (theme)
1014 "Given a symbol THEME, create a new symbol by appending \"-theme\".
1015Store this symbol in the `theme-feature' property of THEME.
1016Calling `provide-theme' to provide THEME actually puts `THEME-theme'
1017into `features'.
1018
1019This allows for a file-name convention for autoloading themes:
1020Every theme X has a property `provide-theme' whose value is \"X-theme\".
87d737ae 1021\(load-theme X) then attempts to load the file `X-theme.el'."
46ce5feb
RS
1022 (intern (concat (symbol-name theme) "-theme")))
1023\f
1024;;; Loading themes.
1025
9c4b6e94 1026(defcustom custom-theme-directory
63212d45 1027 user-emacs-directory
9c4b6e94 1028 "Directory in which Custom theme files should be written.
87d737ae 1029`load-theme' searches this directory in addition to load-path.
9c4b6e94
LT
1030The command `customize-create-theme' writes the files it produces
1031into this directory."
1032 :type 'string
1033 :group 'customize
1034 :version "22.1")
1035
e1bab181 1036(defun provide-theme (theme)
d358aa10
CY
1037 "Indicate that this file provides THEME.
1038This calls `provide' to provide the feature name stored in THEME's
1039property `theme-feature' (which is usually a symbol created by
1040`custom-make-theme-feature')."
1041 (if (memq theme '(user changed))
1042 (error "Custom theme cannot be named %S" theme))
e1bab181
RS
1043 (custom-check-theme theme)
1044 (provide (get theme 'theme-feature))
d358aa10 1045 ;; Loading a theme also enables it.
46ce5feb
RS
1046 (push theme custom-enabled-themes)
1047 ;; `user' must always be the highest-precedence enabled theme.
1048 ;; Make that remain true. (This has the effect of making user settings
1049 ;; override the ones just loaded, too.)
d358aa10
CY
1050 (let ((custom-enabling-themes t))
1051 (enable-theme 'user)))
e1bab181 1052
87d737ae 1053(defun load-theme (theme)
73e60f53 1054 "Load a theme's settings from its file.
87d737ae 1055This also enables the theme; use `disable-theme' to disable it."
e1bab181
RS
1056 ;; Note we do no check for validity of the theme here.
1057 ;; This allows to pull in themes by a file-name convention
87d737ae 1058 (interactive "SCustom theme name: ")
73e60f53
CY
1059 ;; If reloading, clear out the old theme settings.
1060 (when (custom-theme-p theme)
1061 (disable-theme theme)
1062 (put theme 'theme-settings nil)
1063 (put theme 'theme-feature nil)
1064 (put theme 'theme-documentation nil))
9c4b6e94
LT
1065 (let ((load-path (if (file-directory-p custom-theme-directory)
1066 (cons custom-theme-directory load-path)
1067 load-path)))
d358aa10 1068 (load (symbol-name (custom-make-theme-feature theme)))))
46ce5feb
RS
1069\f
1070;;; Enabling and disabling loaded themes.
1071
b2a41d12
CY
1072(defvar custom-enabling-themes nil)
1073
87d737ae 1074(defun enable-theme (theme)
46ce5feb
RS
1075 "Reenable all variable and face settings defined by THEME.
1076The newly enabled theme gets the highest precedence (after `user').
e335f09e
CY
1077If it is already enabled, just give it highest precedence (after `user').
1078
d358aa10
CY
1079If THEME does not specify any theme settings, this tries to load
1080the theme from its theme file, by calling `load-theme'."
87d737ae 1081 (interactive "SEnable Custom theme: ")
d358aa10
CY
1082 (if (not (custom-theme-p theme))
1083 (load-theme theme)
1084 ;; This could use a bit of optimization -- cyd
1085 (let ((settings (get theme 'theme-settings)))
1086 (dolist (s settings)
1087 (let* ((prop (car s))
1088 (symbol (cadr s))
1089 (spec-list (get symbol prop)))
1090 (put symbol prop (cons (cddr s) (assq-delete-all theme spec-list)))
1091 (if (eq prop 'theme-value)
1092 (custom-theme-recalc-variable symbol)
1093 (custom-theme-recalc-face symbol)))))
1094 (unless (eq theme 'user)
1095 (setq custom-enabled-themes
1096 (cons theme (delq theme custom-enabled-themes)))
1097 (unless custom-enabling-themes
1098 (enable-theme 'user)))))
b2a41d12
CY
1099
1100(defcustom custom-enabled-themes nil
1101 "List of enabled Custom Themes, highest precedence first.
1102
1103This does not include the `user' theme, which is set by Customize,
1104and always takes precedence over other Custom Themes."
1105 :group 'customize
1106 :type '(repeat symbol)
3d0c2a29 1107 :set-after '(custom-theme-directory) ; so we can find the themes
b2a41d12
CY
1108 :set (lambda (symbol themes)
1109 ;; Avoid an infinite loop when custom-enabled-themes is
1110 ;; defined in a theme (e.g. `user'). Enabling the theme sets
1111 ;; custom-enabled-themes, which enables the theme...
1112 (unless custom-enabling-themes
d358aa10 1113 (let ((custom-enabling-themes t) failures)
b2a41d12
CY
1114 (setq themes (delq 'user (delete-dups themes)))
1115 (if (boundp symbol)
1116 (dolist (theme (symbol-value symbol))
1117 (if (not (memq theme themes))
1118 (disable-theme theme))))
1119 (dolist (theme (reverse themes))
d358aa10 1120 (condition-case nil
b2a41d12 1121 (enable-theme theme)
d358aa10
CY
1122 (error (progn (push theme failures)
1123 (setq themes (delq theme themes))))))
b2a41d12 1124 (enable-theme 'user)
d358aa10
CY
1125 (custom-set-default symbol themes)
1126 (if failures
1127 (message "Failed to enable themes: %s"
1128 (mapconcat 'symbol-name failures " ")))))))
b2a41d12 1129
d358aa10 1130(defsubst custom-theme-enabled-p (theme)
b2a41d12
CY
1131 "Return non-nil if THEME is enabled."
1132 (memq theme custom-enabled-themes))
46ce5feb 1133
87d737ae 1134(defun disable-theme (theme)
46ce5feb 1135 "Disable all variable and face settings defined by THEME.
b2a41d12 1136See `custom-enabled-themes' for a list of enabled themes."
d358aa10
CY
1137 (interactive (list (intern
1138 (completing-read
1139 "Disable Custom theme: "
1140 (mapcar 'symbol-name custom-enabled-themes)
1141 nil t))))
1142 (when (custom-theme-enabled-p theme)
8913f945
RS
1143 (let ((settings (get theme 'theme-settings)))
1144 (dolist (s settings)
1145 (let* ((prop (car s))
1146 (symbol (cadr s))
1147 (spec-list (get symbol prop)))
1148 (put symbol prop (assq-delete-all theme spec-list))
1149 (if (eq prop 'theme-value)
1150 (custom-theme-recalc-variable symbol)
b2a41d12
CY
1151 (custom-theme-recalc-face symbol)))))
1152 (setq custom-enabled-themes
1153 (delq theme custom-enabled-themes))))
46ce5feb 1154
46ce5feb
RS
1155(defun custom-variable-theme-value (variable)
1156 "Return (list VALUE) indicating the custom theme value of VARIABLE.
1157That is to say, it specifies what the value should be according to
1158currently enabled custom themes.
1159
1160This function returns nil if no custom theme specifies a value for VARIABLE."
1161 (let* ((theme-value (get variable 'theme-value)))
1162 (if theme-value
d358aa10 1163 (cdr (car theme-value)))))
46ce5feb 1164
46ce5feb
RS
1165(defun custom-theme-recalc-variable (variable)
1166 "Set VARIABLE according to currently enabled custom themes."
1167 (let ((valspec (custom-variable-theme-value variable)))
d358aa10
CY
1168 (if valspec
1169 (put variable 'saved-value valspec)
46ce5feb 1170 (setq valspec (get variable 'standard-value)))
d358aa10
CY
1171 (if (and valspec
1172 (or (get variable 'force-value)
1173 (default-boundp variable)))
1174 (funcall (or (get variable 'custom-set) 'set-default) variable
1175 (eval (car valspec))))))
46ce5feb
RS
1176
1177(defun custom-theme-recalc-face (face)
1178 "Set FACE according to currently enabled custom themes."
b2a41d12 1179 (if (facep face)
279c4408
GM
1180 (face-spec-set face
1181 (get (or (get face 'face-alias) face)
1182 'face-override-spec))))
46ce5feb 1183\f
d358aa10
CY
1184;;; XEmacs compability functions
1185
1186;; In XEmacs, when you reset a Custom Theme, you have to specify the
1187;; theme to reset it to. We just apply the next available theme, so
1188;; just ignore the IGNORED arguments.
1189
e1bab181 1190(defun custom-theme-reset-variables (theme &rest args)
d358aa10 1191 "Reset some variable settings in THEME to their values in other themes.
46ce5feb 1192Each of the arguments ARGS has this form:
e1bab181 1193
d358aa10 1194 (VARIABLE IGNORED)
e1bab181 1195
7e8b9dc3 1196This means reset VARIABLE. (The argument IGNORED is ignored)."
e1bab181 1197 (custom-check-theme theme)
46ce5feb 1198 (dolist (arg args)
d358aa10 1199 (custom-push-theme 'theme-value (car arg) theme 'reset)))
e1bab181
RS
1200
1201(defun custom-reset-variables (&rest args)
d358aa10 1202 "Reset the specs of some variables to their values in other themes.
46ce5feb 1203This creates settings in the `user' theme.
e1bab181 1204
46ce5feb 1205Each of the arguments ARGS has this form:
e1bab181 1206
d358aa10 1207 (VARIABLE IGNORED)
e1bab181 1208
7e8b9dc3 1209This means reset VARIABLE. (The argument IGNORED is ignored)."
e1bab181
RS
1210 (apply 'custom-theme-reset-variables 'user args))
1211
55535639
PJ
1212;;; The End.
1213
1214;; Process the defcustoms for variables loaded before this file.
1215(while custom-declare-variable-list
1216 (apply 'custom-declare-variable (car custom-declare-variable-list))
1217 (setq custom-declare-variable-list (cdr custom-declare-variable-list)))
1218
1219(provide 'custom)
1220
059290d6 1221;; arch-tag: 041b6116-aabe-4f9a-902d-74092bc3dab2
55535639 1222;;; custom.el ends here