merge emacs-23
[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,
114f9c96 4;; 2005, 2006, 2007, 2008, 2009, 2010 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.
b3c7c12d
SM
60 (set-default symbol (eval (if (get symbol 'saved-value)
61 (car (get symbol 'saved-value))
62 value)))))
55535639
PJ
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
b3c7c12d
SM
73 (eval (if (get symbol 'saved-value)
74 (car (get symbol 'saved-value))
75 value)))))
55535639
PJ
76
77(defun custom-initialize-reset (symbol value)
78 "Initialize SYMBOL based on VALUE.
79Set the symbol, using its `:set' function (or `set-default' if it has none).
80The value is either the symbol's current value
81 \(as obtained using the `:get' function), if any,
82or the value in the symbol's `saved-value' property if any,
83or (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))))
91 (t
92 (eval value)))))
93
94(defun custom-initialize-changed (symbol value)
95 "Initialize SYMBOL with VALUE.
96Like `custom-initialize-reset', but only use the `:set' function if
97not using the standard setting.
98For 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)))))
108 (t
109 (set-default symbol (eval value)))))
110
790d0270
SM
111(defvar custom-delayed-init-variables nil
112 "List of variables whose initialization is pending.")
113
114(defun custom-initialize-delay (symbol value)
115 "Delay initialization of SYMBOL to the next Emacs start.
90a94603
GM
116This is used in files that are preloaded (or for autoloaded
117variables), so that the initialization is done in the run-time
118context rather than the build-time context. This also has the
119side-effect that the (delayed) initialization is performed with
120the :set function.
121
122For variables in preloaded files, you can simply use this
123function for the :initialize property. For autoloaded variables,
124you will also need to add an autoload stanza calling this
125function, and another one setting the standard-value property.
126See `send-mail-function' in sendmail.el for an example."
adba8116
SM
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).
790d0270
SM
131 (push symbol custom-delayed-init-variables))
132
55535639
PJ
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."
b6f8ba09 141 (put symbol 'standard-value (purecopy (list default)))
55535639
PJ
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)
b6f8ba09 417 (put symbol 'custom-prefix (purecopy value)))
55535639
PJ
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)
af89cf77
DN
424 (push (cons (purecopy load-file-name) symbol)
425 custom-current-group-alist)))
55535639
PJ
426 (run-hooks 'custom-define-hook)
427 symbol)
428
429(defmacro defgroup (symbol members doc &rest args)
430 "Declare SYMBOL as a customization group containing MEMBERS.
431SYMBOL does not need to be quoted.
432
7a41cd7f
GM
433Third argument DOC is the group documentation. This should be a short
434description of the group, beginning with a capital and ending with
435a period. Words other than the first should not be capitalized, if they
436are not usually written so.
55535639
PJ
437
438MEMBERS should be an alist of the form ((NAME WIDGET)...) where
439NAME is a symbol and WIDGET is a widget for editing that symbol.
440Useful widgets are `custom-variable' for editing variables,
441`custom-face' for edit faces, and `custom-group' for editing groups.
442
443The remaining arguments should have the form
444
445 [KEYWORD VALUE]...
446
81117bdd
BW
447For a list of valid keywords, see the common keywords listed in
448`defcustom'.
55535639 449
81117bdd
BW
450See Info node `(elisp) Customization' in the Emacs Lisp manual
451for more information."
45aacac6 452 (declare (doc-string 3))
55535639
PJ
453 ;; It is better not to use backquote in this file,
454 ;; because that makes a bootstrapping problem
455 ;; if you need to recompile all the Lisp files using interpreted code.
456 (nconc (list 'custom-declare-group (list 'quote symbol) members doc) args))
457
458(defun custom-add-to-group (group option widget)
459 "To existing GROUP add a new OPTION of type WIDGET.
460If there already is an entry for OPTION and WIDGET, nothing is done."
461 (let ((members (get group 'custom-group))
462 (entry (list option widget)))
463 (unless (member entry members)
464 (put group 'custom-group (nconc members (list entry))))))
465
29512a0f
SM
466(defun custom-group-of-mode (mode)
467 "Return the custom group corresponding to the major or minor MODE.
468If no such group is found, return nil."
469 (or (get mode 'custom-mode-group)
470 (if (or (get mode 'custom-group)
471 (and (string-match "-mode\\'" (symbol-name mode))
472 (get (setq mode (intern (substring (symbol-name mode)
473 0 (match-beginning 0))))
474 'custom-group)))
475 mode)))
476
55535639
PJ
477;;; Properties.
478
479(defun custom-handle-all-keywords (symbol args type)
480 "For customization option SYMBOL, handle keyword arguments ARGS.
481Third argument TYPE is the custom option type."
d3b80e9b 482 (unless (memq :group args)
9b6098b9 483 (custom-add-to-group (custom-current-group) symbol type))
55535639
PJ
484 (while args
485 (let ((arg (car args)))
486 (setq args (cdr args))
487 (unless (symbolp arg)
488 (error "Junk in args %S" args))
489 (let ((keyword arg)
490 (value (car args)))
491 (unless args
492 (error "Keyword %s is missing an argument" keyword))
493 (setq args (cdr args))
494 (custom-handle-keyword symbol keyword value type)))))
495
496(defun custom-handle-keyword (symbol keyword value type)
497 "For customization option SYMBOL, handle KEYWORD with VALUE.
498Fourth argument TYPE is the custom option type."
499 (if purify-flag
500 (setq value (purecopy value)))
501 (cond ((eq keyword :group)
502 (custom-add-to-group value symbol type))
503 ((eq keyword :version)
504 (custom-add-version symbol value))
4e9c705e
BW
505 ((eq keyword :package-version)
506 (custom-add-package-version symbol value))
55535639
PJ
507 ((eq keyword :link)
508 (custom-add-link symbol value))
509 ((eq keyword :load)
510 (custom-add-load symbol value))
511 ((eq keyword :tag)
512 (put symbol 'custom-tag value))
513 ((eq keyword :set-after)
514 (custom-add-dependencies symbol value))
515 (t
516 (error "Unknown keyword %s" keyword))))
517
518(defun custom-add-dependencies (symbol value)
519 "To the custom option SYMBOL, add dependencies specified by VALUE.
520VALUE should be a list of symbols. For each symbol in that list,
521this specifies that SYMBOL should be set after the specified symbol, if
522both appear in constructs like `custom-set-variables'."
523 (unless (listp value)
524 (error "Invalid custom dependency `%s'" value))
525 (let* ((deps (get symbol 'custom-dependencies))
526 (new-deps deps))
527 (while value
528 (let ((dep (car value)))
529 (unless (symbolp dep)
530 (error "Invalid custom dependency `%s'" dep))
531 (unless (memq dep new-deps)
532 (setq new-deps (cons dep new-deps)))
533 (setq value (cdr value))))
534 (unless (eq deps new-deps)
535 (put symbol 'custom-dependencies new-deps))))
e1bab181 536
55535639
PJ
537(defun custom-add-option (symbol option)
538 "To the variable SYMBOL add OPTION.
539
a4842ffa
RS
540If SYMBOL's custom type is a hook, OPTION should be a hook member.
541If SYMBOL's custom type is an alist, OPTION specifies a symbol
542to offer to the user as a possible key in the alist.
543For other custom types, this has no effect."
55535639
PJ
544 (let ((options (get symbol 'custom-options)))
545 (unless (member option options)
546 (put symbol 'custom-options (cons option options)))))
467064a4 547(defalias 'custom-add-frequent-value 'custom-add-option)
55535639
PJ
548
549(defun custom-add-link (symbol widget)
550 "To the custom option SYMBOL add the link WIDGET."
551 (let ((links (get symbol 'custom-links)))
552 (unless (member widget links)
553 (put symbol 'custom-links (cons (purecopy widget) links)))))
554
555(defun custom-add-version (symbol version)
556 "To the custom option SYMBOL add the version VERSION."
557 (put symbol 'custom-version (purecopy version)))
558
4e9c705e
BW
559(defun custom-add-package-version (symbol version)
560 "To the custom option SYMBOL add the package version VERSION."
561 (put symbol 'custom-package-version (purecopy version)))
562
55535639
PJ
563(defun custom-add-load (symbol load)
564 "To the custom option SYMBOL add the dependency LOAD.
565LOAD should be either a library file name, or a feature name."
566 (let ((loads (get symbol 'custom-loads)))
567 (unless (member load loads)
568 (put symbol 'custom-loads (cons (purecopy load) loads)))))
569
d54fbdfd
SM
570(defun custom-autoload (symbol load &optional noset)
571 "Mark SYMBOL as autoloaded custom variable and add dependency LOAD.
572If NOSET is non-nil, don't bother autoloading LOAD when setting the variable."
573 (put symbol 'custom-autoload (if noset 'noset t))
1669290d
MR
574 (custom-add-load symbol load))
575
576;; This test is also in the C code of `user-variable-p'.
577(defun custom-variable-p (variable)
289e1999
LT
578 "Return non-nil if VARIABLE is a custom variable.
579This recursively follows aliases."
580 (setq variable (indirect-variable variable))
1669290d
MR
581 (or (get variable 'standard-value)
582 (get variable 'custom-autoload)))
583
400b960e
RS
584(defun custom-note-var-changed (variable)
585 "Inform Custom that VARIABLE has been set (changed).
586VARIABLE is a symbol that names a user option.
587The result is that the change is treated as having been made through Custom."
400b960e 588 (put variable 'customized-value (list (custom-quote (eval variable)))))
06f5c483
JL
589
590\f
591;;; Custom Themes
400b960e 592
8f772dfd
RS
593;;; Loading files needed to customize a symbol.
594;;; This is in custom.el because menu-bar.el needs it for toggle cmds.
595
596(defvar custom-load-recursion nil
597 "Hack to avoid recursive dependencies.")
598
599(defun custom-load-symbol (symbol)
600 "Load all dependencies for SYMBOL."
601 (unless custom-load-recursion
29512a0f 602 (let ((custom-load-recursion t))
a8c78057
RS
603 ;; Load these files if not already done,
604 ;; to make sure we know all the dependencies of SYMBOL.
605 (condition-case nil
606 (require 'cus-load)
607 (error nil))
608 (condition-case nil
609 (require 'cus-start)
610 (error nil))
29512a0f
SM
611 (dolist (load (get symbol 'custom-loads))
612 (cond ((symbolp load) (condition-case nil (require load) (error nil)))
613 ;; This is subsumed by the test below, but it's much faster.
8f772dfd
RS
614 ((assoc load load-history))
615 ;; This was just (assoc (locate-library load) load-history)
616 ;; but has been optimized not to load locate-library
617 ;; if not necessary.
29512a0f
SM
618 ((let ((regexp (concat "\\(\\`\\|/\\)" (regexp-quote load)
619 "\\(\\'\\|\\.\\)"))
620 (found nil))
8f772dfd 621 (dolist (loaded load-history)
c3891447
RS
622 (and (stringp (car loaded))
623 (string-match regexp (car loaded))
8f772dfd
RS
624 (setq found t)))
625 found))
626 ;; Without this, we would load cus-edit recursively.
627 ;; We are still loading it when we call this,
628 ;; and it is not in load-history yet.
629 ((equal load "cus-edit"))
29512a0f 630 (t (condition-case nil (load load) (error nil))))))))
46ce5feb 631\f
c2e2f9be
CY
632(defvar custom-local-buffer nil
633 "Non-nil, in a Customization buffer, means customize a specific buffer.
634If this variable is non-nil, it should be a buffer,
635and it means customize the local bindings of that buffer.
636This variable is a permanent local, and it normally has a local binding
637in every Customization buffer.")
638(put 'custom-local-buffer 'permanent-local t)
639
640(defun custom-set-default (variable value)
641 "Default :set function for a customizable variable.
642Normally, this sets the default value of VARIABLE to VALUE,
643but if `custom-local-buffer' is non-nil,
644this sets the local binding in that buffer instead."
645 (if custom-local-buffer
646 (with-current-buffer custom-local-buffer
647 (set variable value))
648 (set-default variable value)))
649
650(defun custom-set-minor-mode (variable value)
651 ":set function for minor mode variables.
652Normally, this sets the default value of VARIABLE to nil if VALUE
653is nil and to t otherwise,
654but if `custom-local-buffer' is non-nil,
655this sets the local binding in that buffer instead."
656 (if custom-local-buffer
657 (with-current-buffer custom-local-buffer
658 (funcall variable (if value 1 0)))
659 (funcall variable (if value 1 0))))
660
661(defun custom-quote (sexp)
4837b516 662 "Quote SEXP if it is not self quoting."
c2e2f9be
CY
663 (if (or (memq sexp '(t nil))
664 (keywordp sexp)
665 (and (listp sexp)
666 (memq (car sexp) '(lambda)))
667 (stringp sexp)
668 (numberp sexp)
669 (vectorp sexp)
670;;; (and (fboundp 'characterp)
671;;; (characterp sexp))
672 )
673 sexp
674 (list 'quote sexp)))
675
676(defun customize-mark-to-save (symbol)
677 "Mark SYMBOL for later saving.
678
679If the default value of SYMBOL is different from the standard value,
680set the `saved-value' property to a list whose car evaluates to the
681default value. Otherwise, set it to nil.
682
683To actually save the value, call `custom-save-all'.
684
4837b516 685Return non-nil if the `saved-value' property actually changed."
ec9f0a62 686 (custom-load-symbol symbol)
c2e2f9be
CY
687 (let* ((get (or (get symbol 'custom-get) 'default-value))
688 (value (funcall get symbol))
689 (saved (get symbol 'saved-value))
690 (standard (get symbol 'standard-value))
691 (comment (get symbol 'customized-variable-comment)))
4837b516 692 ;; Save default value if different from standard value.
c2e2f9be
CY
693 (if (or (null standard)
694 (not (equal value (condition-case nil
695 (eval (car standard))
696 (error nil)))))
697 (put symbol 'saved-value (list (custom-quote value)))
698 (put symbol 'saved-value nil))
699 ;; Clear customized information (set, but not saved).
700 (put symbol 'customized-value nil)
701 ;; Save any comment that might have been set.
702 (when comment
703 (put symbol 'saved-variable-comment comment))
704 (not (equal saved (get symbol 'saved-value)))))
705
706(defun customize-mark-as-set (symbol)
707 "Mark current value of SYMBOL as being set from customize.
708
709If the default value of SYMBOL is different from the saved value if any,
710or else if it is different from the standard value, set the
711`customized-value' property to a list whose car evaluates to the
712default value. Otherwise, set it to nil.
713
4837b516 714Return non-nil if the `customized-value' property actually changed."
ec9f0a62 715 (custom-load-symbol symbol)
c2e2f9be
CY
716 (let* ((get (or (get symbol 'custom-get) 'default-value))
717 (value (funcall get symbol))
718 (customized (get symbol 'customized-value))
719 (old (or (get symbol 'saved-value) (get symbol 'standard-value))))
4837b516 720 ;; Mark default value as set if different from old value.
d54fbdfd
SM
721 (if (not (and old
722 (equal value (condition-case nil
723 (eval (car old))
724 (error nil)))))
fccf2784
CY
725 (progn (put symbol 'customized-value (list (custom-quote value)))
726 (custom-push-theme 'theme-value symbol 'user 'set
727 (custom-quote value)))
c2e2f9be
CY
728 (put symbol 'customized-value nil))
729 ;; Changed?
730 (not (equal customized (get symbol 'customized-value)))))
731
732(defun custom-reevaluate-setting (symbol)
733 "Reset the value of SYMBOL by re-evaluating its saved or standard value.
734Use the :set function to do so. This is useful for customizable options
735that are defined before their standard value can really be computed.
736E.g. dumped variables whose default depends on run-time information."
737 (funcall (or (get symbol 'custom-set) 'set-default)
738 symbol
739 (eval (car (or (get symbol 'saved-value) (get symbol 'standard-value))))))
740
741\f
d358aa10
CY
742;;; Custom Themes
743
744;; Custom themes are collections of settings that can be enabled or
745;; disabled as a unit.
746
747;; Each Custom theme is defined by a symbol, called the theme name.
748;; The `theme-settings' property of the theme name records the
749;; variable and face settings of the theme. This property is a list
750;; of elements, each of the form
751;;
752;; (PROP SYMBOL THEME VALUE)
753;;
754;; - PROP is either `theme-value' or `theme-face'
755;; - SYMBOL is the face or variable name
756;; - THEME is the theme name (redundant, but simplifies the code)
757;; - VALUE is an expression that gives the theme's setting for SYMBOL.
758;;
759;; The theme name also has a `theme-feature' property, whose value is
760;; specified when the theme is defined (see `custom-declare-theme').
761;; Usually, this is just a symbol named THEME-theme. This lets
762;; external libraries call (require 'foo-theme).
763
764;; In addition, each symbol (either a variable or a face) affected by
765;; an *enabled* theme has a `theme-value' or `theme-face' property,
766;; which is a list of elements each of the form
767;;
768;; (THEME VALUE)
769;;
770;; which have the same meanings as in `theme-settings'.
771;;
772;; The `theme-value' and `theme-face' lists are ordered by decreasing
773;; theme precedence. Thus, the first element is always the one that
774;; is in effect.
775
776;; Each theme is stored in a theme file, with filename THEME-theme.el.
777;; Loading a theme basically involves calling (load "THEME-theme")
778;; This is done by the function `load-theme'. Loading a theme
779;; automatically enables it.
780;;
781;; When a theme is enabled, the `theme-value' and `theme-face'
782;; properties for the affected symbols are set. When a theme is
783;; disabled, its settings are removed from the `theme-value' and
784;; `theme-face' properties, but the theme's own `theme-settings'
785;; property remains unchanged.
786
d358aa10 787(defvar custom-known-themes '(user changed)
9c4b6e94 788 "Themes that have been defined with `deftheme'.
d358aa10 789The default value is the list (user changed). The theme `changed'
79a0aa11 790contains the settings before custom themes are applied. The
9b05fb22 791theme `user' contains all the settings the user customized and saved.
e1bab181
RS
792Additional themes declared with the `deftheme' macro will be added to
793the front of this list.")
794
e1bab181
RS
795(defsubst custom-theme-p (theme)
796 "Non-nil when THEME has been defined."
797 (memq theme custom-known-themes))
798
799(defsubst custom-check-theme (theme)
800 "Check whether THEME is valid, and signal an error if it is not."
801 (unless (custom-theme-p theme)
802 (error "Unknown theme `%s'" theme)))
803
d358aa10
CY
804(defun custom-push-theme (prop symbol theme mode &optional value)
805 "Record VALUE for face or variable SYMBOL in custom theme THEME.
806PROP is `theme-face' for a face, `theme-value' for a variable.
e1bab181
RS
807
808MODE can be either the symbol `set' or the symbol `reset'. If it is the
809symbol `set', then VALUE is the value to use. If it is the symbol
d358aa10 810`reset', then SYMBOL will be removed from THEME (VALUE is ignored).
e1bab181 811
e1bab181 812See `custom-known-themes' for a list of known themes."
b2a41d12 813 (unless (memq prop '(theme-value theme-face))
d820f1fb 814 (error "Unknown theme property"))
46ce5feb 815 (let* ((old (get symbol prop))
d358aa10
CY
816 (setting (assq theme old)) ; '(theme value)
817 (theme-settings ; '(prop symbol theme value)
818 (get theme 'theme-settings)))
819 (if (eq mode 'reset)
d820f1fb
CY
820 ;; Remove a setting.
821 (when setting
822 (let (res)
823 (dolist (theme-setting theme-settings)
824 (if (and (eq (car theme-setting) prop)
825 (eq (cadr theme-setting) symbol))
826 (setq res theme-setting)))
827 (put theme 'theme-settings (delq res theme-settings)))
828 (put symbol prop (delq setting old)))
829 (if setting
830 ;; Alter an existing setting.
831 (let (res)
832 (dolist (theme-setting theme-settings)
833 (if (and (eq (car theme-setting) prop)
834 (eq (cadr theme-setting) symbol))
835 (setq res theme-setting)))
836 (put theme 'theme-settings
d358aa10 837 (cons (list prop symbol theme value)
d820f1fb 838 (delq res theme-settings)))
d358aa10 839 (setcar (cdr setting) value))
d820f1fb
CY
840 ;; Add a new setting.
841 ;; If the user changed the value outside of Customize, we
d358aa10 842 ;; first save the current value to a fake theme, `changed'.
d820f1fb
CY
843 ;; This ensures that the user-set value comes back if the
844 ;; theme is later disabled.
845 (if (null old)
846 (if (and (eq prop 'theme-value)
e58d8ef5
CY
847 (boundp symbol))
848 (let ((sv (get symbol 'standard-value)))
d54fbdfd
SM
849 (unless (and sv
850 (equal (eval (car sv)) (symbol-value symbol)))
851 (setq old (list (list 'changed (symbol-value symbol))))))
e02577b7
CY
852 (if (and (facep symbol)
853 (not (face-spec-match-p symbol (get symbol 'face-defface-spec))))
d358aa10 854 (setq old (list (list 'changed (list
d820f1fb 855 (append '(t) (custom-face-attributes-get symbol nil)))))))))
d358aa10 856 (put symbol prop (cons (list theme value) old))
d820f1fb 857 (put theme 'theme-settings
d358aa10 858 (cons (list prop symbol theme value)
d820f1fb 859 theme-settings))))))
55535639 860
c2e2f9be 861\f
55535639 862(defun custom-set-variables (&rest args)
f1a262ed
RS
863 "Install user customizations of variable values specified in ARGS.
864These settings are registered as theme `user'.
64e9f9d9 865The arguments should each be a list of the form:
55535639 866
f1a262ed 867 (SYMBOL EXP [NOW [REQUEST [COMMENT]]])
55535639 868
f1a262ed
RS
869This stores EXP (without evaluating it) as the saved value for SYMBOL.
870If NOW is present and non-nil, then also evaluate EXP and set
871the default value for the SYMBOL to the value of EXP.
e1bab181 872
f1a262ed
RS
873REQUEST is a list of features we must require in order to
874handle SYMBOL properly.
55535639 875COMMENT is a comment string about SYMBOL."
e1bab181
RS
876 (apply 'custom-theme-set-variables 'user args))
877
878(defun custom-theme-set-variables (theme &rest args)
f1a262ed
RS
879 "Initialize variables for theme THEME according to settings in ARGS.
880Each of the arguments in ARGS should be a list of this form:
e1bab181 881
f1a262ed 882 (SYMBOL EXP [NOW [REQUEST [COMMENT]]])
e1bab181 883
f1a262ed
RS
884This stores EXP (without evaluating it) as the saved value for SYMBOL.
885If NOW is present and non-nil, then also evaluate EXP and set
886the default value for the SYMBOL to the value of EXP.
e1bab181 887
f1a262ed
RS
888REQUEST is a list of features we must require in order to
889handle SYMBOL properly.
e1bab181
RS
890COMMENT is a comment string about SYMBOL.
891
f1a262ed 892EXP itself is saved unevaluated as SYMBOL property `saved-value' and
e1bab181
RS
893in SYMBOL's list property `theme-value' \(using `custom-push-theme')."
894 (custom-check-theme theme)
9277ee6c
SM
895
896 ;; Process all the needed autoloads before anything else, so that the
897 ;; subsequent code has all the info it needs (e.g. which var corresponds
898 ;; to a minor mode), regardless of the ordering of the variables.
899 (dolist (entry args)
900 (let* ((symbol (indirect-variable (nth 0 entry))))
901 (unless (or (get symbol 'standard-value)
902 (memq (get symbol 'custom-autoload) '(nil noset)))
903 ;; This symbol needs to be autoloaded, even just for a `set'.
904 (custom-load-symbol symbol))))
1e281896 905
9277ee6c 906 ;; Move minor modes and variables with explicit requires to the end.
059290d6
SM
907 (setq args
908 (sort args
909 (lambda (a1 a2)
910 (let* ((sym1 (car a1))
911 (sym2 (car a2))
912 (1-then-2 (memq sym1 (get sym2 'custom-dependencies)))
913 (2-then-1 (memq sym2 (get sym1 'custom-dependencies))))
914 (cond ((and 1-then-2 2-then-1)
915 (error "Circular custom dependency between `%s' and `%s'"
916 sym1 sym2))
917 (2-then-1 nil)
3d0c2a29
GM
918 ;; 1 is a dependency of 2, so needs to be set first.
919 (1-then-2)
b6790c3e
SM
920 ;; Put minor modes and symbols with :require last.
921 ;; Putting minor modes last ensures that the mode
922 ;; function will see other customized values rather
923 ;; than default values.
924 (t (or (nth 3 a2)
925 (eq (get sym2 'custom-set)
926 'custom-set-minor-mode))))))))
059290d6
SM
927 (while args
928 (let ((entry (car args)))
929 (if (listp entry)
930 (let* ((symbol (indirect-variable (nth 0 entry)))
931 (value (nth 1 entry))
932 (now (nth 2 entry))
933 (requests (nth 3 entry))
934 (comment (nth 4 entry))
935 set)
936 (when requests
937 (put symbol 'custom-requests requests)
938 (mapc 'require requests))
939 (setq set (or (get symbol 'custom-set) 'custom-set-default))
940 (put symbol 'saved-value (list value))
941 (put symbol 'saved-variable-comment comment)
942 (custom-push-theme 'theme-value symbol theme 'set value)
943 ;; Allow for errors in the case where the setter has
55535639
PJ
944 ;; changed between versions, say, but let the user know.
945 (condition-case data
946 (cond (now
947 ;; Rogue variable, set it now.
948 (put symbol 'force-value t)
949 (funcall set symbol (eval value)))
950 ((default-boundp symbol)
951 ;; Something already set this, overwrite it.
952 (funcall set symbol (eval value))))
71296446 953 (error
55535639 954 (message "Error setting %s: %s" symbol data)))
059290d6
SM
955 (setq args (cdr args))
956 (and (or now (default-boundp symbol))
957 (put symbol 'variable-comment comment)))
d54fbdfd
SM
958 ;; I believe this is dead-code, because the `sort' code above would
959 ;; have burped before we could get here. --Stef
059290d6
SM
960 ;; Old format, a plist of SYMBOL VALUE pairs.
961 (message "Warning: old format `custom-set-variables'")
962 (ding)
963 (sit-for 2)
964 (let ((symbol (indirect-variable (nth 0 args)))
965 (value (nth 1 args)))
966 (put symbol 'saved-value (list value))
967 (custom-push-theme 'theme-value symbol theme 'set value))
968 (setq args (cdr (cdr args)))))))
55535639 969
46ce5feb
RS
970\f
971;;; Defining themes.
972
d358aa10
CY
973;; A theme file should be named `THEME-theme.el' (where THEME is the theme
974;; name), and found in either `custom-theme-directory' or the load path.
975;; It has the following format:
976;;
977;; (deftheme THEME
978;; DOCSTRING)
979;;
980;; (custom-theme-set-variables
981;; 'THEME
982;; [THEME-VARIABLES])
983;;
984;; (custom-theme-set-faces
985;; 'THEME
986;; [THEME-FACES])
987;;
988;; (provide-theme 'THEME)
46ce5feb 989
46ce5feb 990
d358aa10
CY
991;; The IGNORED arguments to deftheme come from the XEmacs theme code, where
992;; they were used to supply keyword-value pairs like `:immediate',
993;; `:variable-reset-string', etc. We don't use any of these, so ignore them.
6d912ee1 994
d358aa10
CY
995(defmacro deftheme (theme &optional doc &rest ignored)
996 "Declare THEME to be a Custom theme.
997The optional argument DOC is a doc string describing the theme.
46ce5feb
RS
998
999Any theme `foo' should be defined in a file called `foo-theme.el';
1000see `custom-make-theme-feature' for more information."
1001 (let ((feature (custom-make-theme-feature theme)))
1002 ;; It is better not to use backquote in this file,
1003 ;; because that makes a bootstrapping problem
1004 ;; if you need to recompile all the Lisp files using interpreted code.
d358aa10 1005 (list 'custom-declare-theme (list 'quote theme) (list 'quote feature) doc)))
46ce5feb 1006
d358aa10 1007(defun custom-declare-theme (theme feature &optional doc &rest ignored)
46ce5feb 1008 "Like `deftheme', but THEME is evaluated as a normal argument.
d358aa10
CY
1009FEATURE is the feature this theme provides. Normally, this is a symbol
1010created from THEME by `custom-make-theme-feature'."
1011 (if (memq theme '(user changed))
1012 (error "Custom theme cannot be named %S" theme))
46ce5feb
RS
1013 (add-to-list 'custom-known-themes theme)
1014 (put theme 'theme-feature feature)
d358aa10 1015 (when doc (put theme 'theme-documentation doc)))
46ce5feb
RS
1016
1017(defun custom-make-theme-feature (theme)
1018 "Given a symbol THEME, create a new symbol by appending \"-theme\".
1019Store this symbol in the `theme-feature' property of THEME.
1020Calling `provide-theme' to provide THEME actually puts `THEME-theme'
1021into `features'.
1022
1023This allows for a file-name convention for autoloading themes:
1024Every theme X has a property `provide-theme' whose value is \"X-theme\".
87d737ae 1025\(load-theme X) then attempts to load the file `X-theme.el'."
46ce5feb
RS
1026 (intern (concat (symbol-name theme) "-theme")))
1027\f
1028;;; Loading themes.
1029
9c4b6e94 1030(defcustom custom-theme-directory
63212d45 1031 user-emacs-directory
9c4b6e94 1032 "Directory in which Custom theme files should be written.
87d737ae 1033`load-theme' searches this directory in addition to load-path.
9c4b6e94
LT
1034The command `customize-create-theme' writes the files it produces
1035into this directory."
1036 :type 'string
1037 :group 'customize
1038 :version "22.1")
1039
e1bab181 1040(defun provide-theme (theme)
d358aa10
CY
1041 "Indicate that this file provides THEME.
1042This calls `provide' to provide the feature name stored in THEME's
1043property `theme-feature' (which is usually a symbol created by
1044`custom-make-theme-feature')."
1045 (if (memq theme '(user changed))
1046 (error "Custom theme cannot be named %S" theme))
e1bab181
RS
1047 (custom-check-theme theme)
1048 (provide (get theme 'theme-feature))
d358aa10 1049 ;; Loading a theme also enables it.
46ce5feb
RS
1050 (push theme custom-enabled-themes)
1051 ;; `user' must always be the highest-precedence enabled theme.
1052 ;; Make that remain true. (This has the effect of making user settings
1053 ;; override the ones just loaded, too.)
d358aa10
CY
1054 (let ((custom-enabling-themes t))
1055 (enable-theme 'user)))
e1bab181 1056
87d737ae 1057(defun load-theme (theme)
73e60f53 1058 "Load a theme's settings from its file.
87d737ae 1059This also enables the theme; use `disable-theme' to disable it."
e1bab181
RS
1060 ;; Note we do no check for validity of the theme here.
1061 ;; This allows to pull in themes by a file-name convention
87d737ae 1062 (interactive "SCustom theme name: ")
73e60f53
CY
1063 ;; If reloading, clear out the old theme settings.
1064 (when (custom-theme-p theme)
1065 (disable-theme theme)
1066 (put theme 'theme-settings nil)
1067 (put theme 'theme-feature nil)
1068 (put theme 'theme-documentation nil))
9c4b6e94
LT
1069 (let ((load-path (if (file-directory-p custom-theme-directory)
1070 (cons custom-theme-directory load-path)
1071 load-path)))
d358aa10 1072 (load (symbol-name (custom-make-theme-feature theme)))))
46ce5feb
RS
1073\f
1074;;; Enabling and disabling loaded themes.
1075
b2a41d12
CY
1076(defvar custom-enabling-themes nil)
1077
87d737ae 1078(defun enable-theme (theme)
46ce5feb
RS
1079 "Reenable all variable and face settings defined by THEME.
1080The newly enabled theme gets the highest precedence (after `user').
e335f09e
CY
1081If it is already enabled, just give it highest precedence (after `user').
1082
d358aa10
CY
1083If THEME does not specify any theme settings, this tries to load
1084the theme from its theme file, by calling `load-theme'."
87d737ae 1085 (interactive "SEnable Custom theme: ")
d358aa10
CY
1086 (if (not (custom-theme-p theme))
1087 (load-theme theme)
1088 ;; This could use a bit of optimization -- cyd
1089 (let ((settings (get theme 'theme-settings)))
1090 (dolist (s settings)
1091 (let* ((prop (car s))
1092 (symbol (cadr s))
1093 (spec-list (get symbol prop)))
1094 (put symbol prop (cons (cddr s) (assq-delete-all theme spec-list)))
1095 (if (eq prop 'theme-value)
1096 (custom-theme-recalc-variable symbol)
1097 (custom-theme-recalc-face symbol)))))
1098 (unless (eq theme 'user)
1099 (setq custom-enabled-themes
1100 (cons theme (delq theme custom-enabled-themes)))
1101 (unless custom-enabling-themes
1102 (enable-theme 'user)))))
b2a41d12
CY
1103
1104(defcustom custom-enabled-themes nil
1105 "List of enabled Custom Themes, highest precedence first.
1106
1107This does not include the `user' theme, which is set by Customize,
1108and always takes precedence over other Custom Themes."
1109 :group 'customize
1110 :type '(repeat symbol)
3d0c2a29 1111 :set-after '(custom-theme-directory) ; so we can find the themes
b2a41d12
CY
1112 :set (lambda (symbol themes)
1113 ;; Avoid an infinite loop when custom-enabled-themes is
1114 ;; defined in a theme (e.g. `user'). Enabling the theme sets
1115 ;; custom-enabled-themes, which enables the theme...
1116 (unless custom-enabling-themes
d358aa10 1117 (let ((custom-enabling-themes t) failures)
b2a41d12
CY
1118 (setq themes (delq 'user (delete-dups themes)))
1119 (if (boundp symbol)
1120 (dolist (theme (symbol-value symbol))
1121 (if (not (memq theme themes))
1122 (disable-theme theme))))
1123 (dolist (theme (reverse themes))
d358aa10 1124 (condition-case nil
b2a41d12 1125 (enable-theme theme)
d358aa10
CY
1126 (error (progn (push theme failures)
1127 (setq themes (delq theme themes))))))
b2a41d12 1128 (enable-theme 'user)
d358aa10
CY
1129 (custom-set-default symbol themes)
1130 (if failures
1131 (message "Failed to enable themes: %s"
1132 (mapconcat 'symbol-name failures " ")))))))
b2a41d12 1133
d358aa10 1134(defsubst custom-theme-enabled-p (theme)
b2a41d12
CY
1135 "Return non-nil if THEME is enabled."
1136 (memq theme custom-enabled-themes))
46ce5feb 1137
87d737ae 1138(defun disable-theme (theme)
46ce5feb 1139 "Disable all variable and face settings defined by THEME.
b2a41d12 1140See `custom-enabled-themes' for a list of enabled themes."
d358aa10
CY
1141 (interactive (list (intern
1142 (completing-read
1143 "Disable Custom theme: "
1144 (mapcar 'symbol-name custom-enabled-themes)
1145 nil t))))
1146 (when (custom-theme-enabled-p theme)
8913f945
RS
1147 (let ((settings (get theme 'theme-settings)))
1148 (dolist (s settings)
1149 (let* ((prop (car s))
1150 (symbol (cadr s))
1151 (spec-list (get symbol prop)))
1152 (put symbol prop (assq-delete-all theme spec-list))
1153 (if (eq prop 'theme-value)
1154 (custom-theme-recalc-variable symbol)
b2a41d12
CY
1155 (custom-theme-recalc-face symbol)))))
1156 (setq custom-enabled-themes
1157 (delq theme custom-enabled-themes))))
46ce5feb 1158
46ce5feb
RS
1159(defun custom-variable-theme-value (variable)
1160 "Return (list VALUE) indicating the custom theme value of VARIABLE.
1161That is to say, it specifies what the value should be according to
1162currently enabled custom themes.
1163
1164This function returns nil if no custom theme specifies a value for VARIABLE."
1165 (let* ((theme-value (get variable 'theme-value)))
1166 (if theme-value
d358aa10 1167 (cdr (car theme-value)))))
46ce5feb 1168
46ce5feb
RS
1169(defun custom-theme-recalc-variable (variable)
1170 "Set VARIABLE according to currently enabled custom themes."
1171 (let ((valspec (custom-variable-theme-value variable)))
d358aa10
CY
1172 (if valspec
1173 (put variable 'saved-value valspec)
46ce5feb 1174 (setq valspec (get variable 'standard-value)))
d358aa10
CY
1175 (if (and valspec
1176 (or (get variable 'force-value)
1177 (default-boundp variable)))
1178 (funcall (or (get variable 'custom-set) 'set-default) variable
1179 (eval (car valspec))))))
46ce5feb
RS
1180
1181(defun custom-theme-recalc-face (face)
1182 "Set FACE according to currently enabled custom themes."
b2a41d12 1183 (if (facep face)
279c4408
GM
1184 (face-spec-set face
1185 (get (or (get face 'face-alias) face)
1186 'face-override-spec))))
46ce5feb 1187\f
d358aa10
CY
1188;;; XEmacs compability functions
1189
1190;; In XEmacs, when you reset a Custom Theme, you have to specify the
1191;; theme to reset it to. We just apply the next available theme, so
1192;; just ignore the IGNORED arguments.
1193
e1bab181 1194(defun custom-theme-reset-variables (theme &rest args)
d358aa10 1195 "Reset some variable settings in THEME to their values in other themes.
46ce5feb 1196Each of the arguments ARGS has this form:
e1bab181 1197
d358aa10 1198 (VARIABLE IGNORED)
e1bab181 1199
7e8b9dc3 1200This means reset VARIABLE. (The argument IGNORED is ignored)."
e1bab181 1201 (custom-check-theme theme)
46ce5feb 1202 (dolist (arg args)
d358aa10 1203 (custom-push-theme 'theme-value (car arg) theme 'reset)))
e1bab181
RS
1204
1205(defun custom-reset-variables (&rest args)
d358aa10 1206 "Reset the specs of some variables to their values in other themes.
46ce5feb 1207This creates settings in the `user' theme.
e1bab181 1208
46ce5feb 1209Each of the arguments ARGS has this form:
e1bab181 1210
d358aa10 1211 (VARIABLE IGNORED)
e1bab181 1212
7e8b9dc3 1213This means reset VARIABLE. (The argument IGNORED is ignored)."
e1bab181
RS
1214 (apply 'custom-theme-reset-variables 'user args))
1215
55535639
PJ
1216;;; The End.
1217
1218;; Process the defcustoms for variables loaded before this file.
1219(while custom-declare-variable-list
1220 (apply 'custom-declare-variable (car custom-declare-variable-list))
1221 (setq custom-declare-variable-list (cdr custom-declare-variable-list)))
1222
1223(provide 'custom)
1224
059290d6 1225;; arch-tag: 041b6116-aabe-4f9a-902d-74092bc3dab2
55535639 1226;;; custom.el ends here