* lisp/progmodes/compile.el (compilation-start): Use compilation-always-kill
[bpt/emacs.git] / lisp / custom.el
CommitLineData
55535639
PJ
1;;; custom.el --- tools for declaring and initializing options
2;;
acaf905b 3;; Copyright (C) 1996-1997, 1999, 2001-2012 Free Software Foundation, Inc.
55535639
PJ
4;;
5;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
6;; Maintainer: FSF
7;; Keywords: help, faces
bd78fa1d 8;; Package: emacs
55535639
PJ
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."
d032d5e7
SM
58 (eval `(defvar ,symbol ,(if (get symbol 'saved-value)
59 (car (get symbol 'saved-value))
60 value))))
55535639
PJ
61
62(defun custom-initialize-set (symbol value)
63 "Initialize SYMBOL based on VALUE.
64If the symbol doesn't have a default binding already,
65then set it using its `:set' function (or `set-default' if it has none).
66The value is either the value in the symbol's `saved-value' property,
67if any, or VALUE."
68 (unless (default-boundp symbol)
69 (funcall (or (get symbol 'custom-set) 'set-default)
70 symbol
b3c7c12d
SM
71 (eval (if (get symbol 'saved-value)
72 (car (get symbol 'saved-value))
73 value)))))
55535639
PJ
74
75(defun custom-initialize-reset (symbol value)
76 "Initialize SYMBOL based on VALUE.
77Set the symbol, using its `:set' function (or `set-default' if it has none).
78The value is either the symbol's current value
79 \(as obtained using the `:get' function), if any,
80or the value in the symbol's `saved-value' property if any,
81or (last of all) VALUE."
d032d5e7
SM
82 (funcall (or (get symbol 'custom-set) 'set-default)
83 symbol
84 (cond ((default-boundp symbol)
85 (funcall (or (get symbol 'custom-get) 'default-value)
86 symbol))
87 ((get symbol 'saved-value)
88 (eval (car (get symbol 'saved-value))))
89 (t
90 (eval value)))))
55535639
PJ
91
92(defun custom-initialize-changed (symbol value)
93 "Initialize SYMBOL with VALUE.
94Like `custom-initialize-reset', but only use the `:set' function if
95not using the standard setting.
96For the standard setting, use `set-default'."
97 (cond ((default-boundp symbol)
98 (funcall (or (get symbol 'custom-set) 'set-default)
99 symbol
100 (funcall (or (get symbol 'custom-get) 'default-value)
101 symbol)))
102 ((get symbol 'saved-value)
103 (funcall (or (get symbol 'custom-set) 'set-default)
104 symbol
105 (eval (car (get symbol 'saved-value)))))
106 (t
107 (set-default symbol (eval value)))))
108
790d0270
SM
109(defvar custom-delayed-init-variables nil
110 "List of variables whose initialization is pending.")
111
06b60517 112(defun custom-initialize-delay (symbol _value)
790d0270 113 "Delay initialization of SYMBOL to the next Emacs start.
90a94603
GM
114This is used in files that are preloaded (or for autoloaded
115variables), so that the initialization is done in the run-time
116context rather than the build-time context. This also has the
117side-effect that the (delayed) initialization is performed with
118the :set function.
119
120For variables in preloaded files, you can simply use this
121function for the :initialize property. For autoloaded variables,
122you will also need to add an autoload stanza calling this
0ea0e51b
GM
123function, and another one setting the standard-value property.
124Or you can wrap the defcustom in a progn, to force the autoloader
125to include all of it." ; see eg vc-sccs-search-project-dir
f042cfd8
AS
126 ;; No longer true:
127 ;; "See `send-mail-function' in sendmail.el for an example."
128
adba8116
SM
129 ;; Until the var is actually initialized, it is kept unbound.
130 ;; This seemed to be at least as good as setting it to an arbitrary
131 ;; value like nil (evaluating `value' is not an option because it
132 ;; may have undesirable side-effects).
790d0270
SM
133 (push symbol custom-delayed-init-variables))
134
55535639
PJ
135(defun custom-declare-variable (symbol default doc &rest args)
136 "Like `defcustom', but SYMBOL and DEFAULT are evaluated as normal arguments.
137DEFAULT should be an expression to evaluate to compute the default value,
e1bab181
RS
138not the default value itself.
139
8989771d 140DEFAULT is stored as SYMBOL's standard value, in SYMBOL's property
e1bab181
RS
141`standard-value'. At the same time, SYMBOL's property `force-value' is
142set to nil, as the value is no longer rogue."
b6f8ba09 143 (put symbol 'standard-value (purecopy (list default)))
55535639
PJ
144 ;; Maybe this option was rogue in an earlier version. It no longer is.
145 (when (get symbol 'force-value)
146 (put symbol 'force-value nil))
d032d5e7
SM
147 (if (keywordp doc)
148 (error "Doc string is missing"))
55535639
PJ
149 (let ((initialize 'custom-initialize-reset)
150 (requests nil))
d3b80e9b
SM
151 (unless (memq :group args)
152 (custom-add-to-group (custom-current-group) symbol 'custom-variable))
55535639
PJ
153 (while args
154 (let ((arg (car args)))
155 (setq args (cdr args))
156 (unless (symbolp arg)
157 (error "Junk in args %S" args))
158 (let ((keyword arg)
159 (value (car args)))
160 (unless args
161 (error "Keyword %s is missing an argument" keyword))
162 (setq args (cdr args))
163 (cond ((eq keyword :initialize)
164 (setq initialize value))
165 ((eq keyword :set)
166 (put symbol 'custom-set value))
167 ((eq keyword :get)
168 (put symbol 'custom-get value))
169 ((eq keyword :require)
6f0d61bf 170 (push value requests))
fd045a34
GM
171 ((eq keyword :risky)
172 (put symbol 'risky-local-variable value))
173 ((eq keyword :safe)
174 (put symbol 'safe-local-variable value))
55535639
PJ
175 ((eq keyword :type)
176 (put symbol 'custom-type (purecopy value)))
177 ((eq keyword :options)
178 (if (get symbol 'custom-options)
179 ;; Slow safe code to avoid duplicates.
180 (mapc (lambda (option)
181 (custom-add-option symbol option))
6f0d61bf 182 value)
55535639
PJ
183 ;; Fast code for the common case.
184 (put symbol 'custom-options (copy-sequence value))))
185 (t
186 (custom-handle-keyword symbol keyword value
187 'custom-variable))))))
188 (put symbol 'custom-requests requests)
189 ;; Do the actual initialization.
6da43544
RS
190 (unless custom-dont-initialize
191 (funcall initialize symbol default)))
d032d5e7
SM
192 ;; Use defvar to set the docstring as well as the special-variable-p flag.
193 ;; FIXME: We should reproduce more of `defvar's behavior, such as the warning
194 ;; when the var is currently let-bound.
195 (if (not (default-boundp symbol))
196 ;; Don't use defvar to avoid setting a default-value when undesired.
197 (when doc (put symbol 'variable-documentation doc))
198 (eval `(defvar ,symbol nil ,@(when doc (list doc)))))
80888260 199 (push symbol current-load-list)
55535639
PJ
200 (run-hooks 'custom-define-hook)
201 symbol)
202
f2475f97
CY
203(defmacro defcustom (symbol standard doc &rest args)
204 "Declare SYMBOL as a customizable variable.
9a6dd747 205SYMBOL is the variable name; it should not be quoted.
f2475f97
CY
206STANDARD is an expression specifying the variable's standard
207value. It should not be quoted. It is evaluated once by
9a6dd747 208`defcustom', and the value is assigned to SYMBOL if the variable
f2475f97
CY
209is unbound. The expression itself is also stored, so that
210Customize can re-evaluate it later to get the standard value.
55535639
PJ
211DOC is the variable documentation.
212
c3a70e2b
CY
213This macro uses `defvar' as a subroutine, which also marks the
214variable as \"special\", so that it is always dynamically bound
215even when `lexical-binding' is t.
216
217The remaining arguments to `defcustom' should have the form
55535639
PJ
218
219 [KEYWORD VALUE]...
220
221The following keywords are meaningful:
222
693d0bd3 223:type VALUE should be a widget type for editing the symbol's value.
55535639 224:options VALUE should be a list of valid members of the widget type.
81117bdd
BW
225:initialize
226 VALUE should be a function used to initialize the
227 variable. It takes two arguments, the symbol and value
228 given in the `defcustom' call. The default is
229 `custom-initialize-reset'.
8a20ca4c
LMI
230:set VALUE should be a function to set the value of the symbol
231 when using the Customize user interface.
81117bdd 232 It takes two arguments, the symbol to set and the value to
f2276b69 233 give it. The default choice of function is `set-default'.
81117bdd
BW
234:get VALUE should be a function to extract the value of symbol.
235 The function takes one argument, a symbol, and should return
236 the current value for that symbol. The default choice of function
f2276b69 237 is `default-value'.
81117bdd
BW
238:require
239 VALUE should be a feature symbol. If you save a value
865fe16f 240 for this option, then when your init file loads the value,
81117bdd 241 it does (require VALUE) first.
03988c98
CY
242:set-after VARIABLES
243 Specifies that SYMBOL should be set after the list of variables
244 VARIABLES when both have been customized.
fd045a34
GM
245:risky Set SYMBOL's `risky-local-variable' property to VALUE.
246:safe Set SYMBOL's `safe-local-variable' property to VALUE.
d6e6f4b1 247 See Info node `(elisp) File Local Variables'.
81117bdd
BW
248
249The following common keywords are also meaningful.
250
55535639 251:group VALUE should be a customization group.
81117bdd 252 Add SYMBOL (or FACE with `defface') to that group.
3dc5f18e 253:link LINK-DATA
00644b82
EZ
254 Include an external link after the documentation string for this
255 item. This is a sentence containing an active field which
256 references some other documentation.
71296446 257
11ee7d4e 258 There are several alternatives you can use for LINK-DATA:
71296446 259
3dc5f18e 260 (custom-manual INFO-NODE)
00644b82 261 Link to an Info node; INFO-NODE is a string which specifies
11ee7d4e 262 the node name, as in \"(emacs)Top\".
71296446 263
3dc5f18e 264 (info-link INFO-NODE)
00644b82
EZ
265 Like `custom-manual' except that the link appears in the
266 customization buffer with the Info node name.
71296446 267
3dc5f18e 268 (url-link URL)
00644b82 269 Link to a web page; URL is a string which specifies the URL.
11ee7d4e
JL
270
271 (emacs-commentary-link LIBRARY)
272 Link to the commentary section of LIBRARY.
273
274 (emacs-library-link LIBRARY)
275 Link to an Emacs Lisp LIBRARY file.
276
277 (file-link FILE)
278 Link to FILE.
279
280 (function-link FUNCTION)
281 Link to the documentation of FUNCTION.
282
283 (variable-link VARIABLE)
284 Link to the documentation of VARIABLE.
285
286 (custom-group-link GROUP)
287 Link to another customization GROUP.
71296446 288
00644b82
EZ
289 You can specify the text to use in the customization buffer by
290 adding `:tag NAME' after the first element of the LINK-DATA; for
3dc5f18e 291 example, (info-link :tag \"foo\" \"(emacs)Top\") makes a link to the
00644b82 292 Emacs manual which appears in the buffer as `foo'.
71296446 293
00644b82
EZ
294 An item can have more than one external link; however, most items
295 have none at all.
55535639
PJ
296:version
297 VALUE should be a string specifying that the variable was
298 first introduced, or its default value was changed, in Emacs
299 version VERSION.
4e9c705e 300:package-version
994019df 301 VALUE should be a list with the form (PACKAGE . VERSION)
4e9c705e
BW
302 specifying that the variable was first introduced, or its
303 default value was changed, in PACKAGE version VERSION. This
304 keyword takes priority over :version. The PACKAGE and VERSION
305 must appear in the alist `customize-package-emacs-version-alist'.
994019df
BW
306 Since PACKAGE must be unique and the user might see it in an
307 error message, a good choice is the official name of the
308 package, such as MH-E or Gnus.
00644b82
EZ
309:tag LABEL
310 Use LABEL, a string, instead of the item's name, to label the item
1c1da418 311 in customization menus and buffers.
00644b82
EZ
312:load FILE
313 Load file FILE (a string) before displaying this customization
29512a0f 314 item. Loading is done with `load', and only if the file is
00644b82 315 not already loaded.
55535639 316
19229e5e
LT
317If SYMBOL has a local binding, then this form affects the local
318binding. This is normally not what you want. Thus, if you need
319to load a file defining variables with this form, or with
320`defvar' or `defconst', you should always load that file
321_outside_ any bindings for these variables. \(`defvar' and
322`defconst' behave similarly in this respect.)
323
81117bdd
BW
324See Info node `(elisp) Customization' in the Emacs Lisp manual
325for more information."
98923800 326 (declare (doc-string 3) (debug (name body)))
55535639
PJ
327 ;; It is better not to use backquote in this file,
328 ;; because that makes a bootstrapping problem
329 ;; if you need to recompile all the Lisp files using interpreted code.
850256b5
SM
330 `(custom-declare-variable
331 ',symbol
332 ,(if lexical-binding ;FIXME: This is not reliable, but is all we have.
f2475f97
CY
333 ;; The STANDARD arg should be an expression that evaluates to
334 ;; the standard value. The use of `eval' for it is spread
335 ;; over many different places and hence difficult to
336 ;; eliminate, yet we want to make sure that the `standard'
337 ;; expression is checked by the byte-compiler, and that
338 ;; lexical-binding is obeyed, so quote the expression with
339 ;; `lambda' rather than with `quote'.
2bd785a2 340 ``(funcall #',(lambda () ,standard))
f2475f97 341 `',standard)
850256b5
SM
342 ,doc
343 ,@args))
55535639
PJ
344
345;;; The `defface' Macro.
346
347(defmacro defface (face spec doc &rest args)
348 "Declare FACE as a customizable face that defaults to SPEC.
349FACE does not need to be quoted.
350
351Third argument DOC is the face documentation.
352
2a69edce 353If FACE has been set with `custom-set-faces', set the face attributes
55535639
PJ
354as specified by that function, otherwise set the face attributes
355according to SPEC.
356
357The remaining arguments should have the form
358
359 [KEYWORD VALUE]...
360
81117bdd
BW
361For a list of valid keywords, see the common keywords listed in
362`defcustom'.
55535639
PJ
363
364SPEC should be an alist of the form ((DISPLAY ATTS)...).
365
ebdaed17 366In the first element, DISPLAY can be `default'. The ATTS in that
9c27debd
RS
367element then act as defaults for all the following elements.
368
369Aside from that, DISPLAY specifies conditions to match some or
370all frames. For each frame, the first element of SPEC where the
371DISPLAY conditions are satisfied is the one that applies to that
372frame. The ATTRs in this element take effect, and the following
373elements are ignored, on that frame.
374
375In the last element, DISPLAY can be t. That element applies to a
ebdaed17 376frame if none of the previous elements (except the `default' if
9c27debd 377any) did.
55535639
PJ
378
379ATTS is a list of face attributes followed by their values:
380 (ATTR VALUE ATTR VALUE...)
381
382The possible attributes are `:family', `:width', `:height', `:weight',
383`:slant', `:underline', `:overline', `:strike-through', `:box',
3d58b15e 384`:foreground', `:background', `:stipple', `:inverse-video', and `:inherit'.
55535639 385
ebdaed17 386DISPLAY can be `default' (only in the first element), the symbol
9c27debd
RS
387t (only in the last element) to match all frames, or an alist of
388conditions of the form \(REQ ITEM...). For such an alist to
389match a frame, each of the conditions must be satisfied, meaning
390that the REQ property of the frame must match one of the
391corresponding ITEMs. These are the defined REQ values:
55535639
PJ
392
393`type' (the value of `window-system')
394 Under X, in addition to the values `window-system' can take,
9b44aa8b
JD
395 `motif', `lucid', `gtk' and `x-toolkit' are allowed, and match when
396 the Motif toolkit, Lucid toolkit, GTK toolkit or any X toolkit is in use.
55535639
PJ
397
398`class' (the frame's color support)
399 Should be one of `color', `grayscale', or `mono'.
400
401`background' (what color is used for the background text)
402 Should be one of `light' or `dark'.
403
6e166990
EZ
404`min-colors' (the minimum number of colors the frame should support)
405 Should be an integer, it is compared with the result of
406 `display-color-cells'.
407
ec85856c
JL
408`supports' (only match frames that support the specified face attributes)
409 Should be a list of face attributes. See the documentation for
410 the function `display-supports-face-attributes-p' for more
411 information on exactly how testing is done.
412
81117bdd
BW
413See Info node `(elisp) Customization' in the Emacs Lisp manual
414for more information."
67a60caa 415 (declare (doc-string 3))
55535639
PJ
416 ;; It is better not to use backquote in this file,
417 ;; because that makes a bootstrapping problem
418 ;; if you need to recompile all the Lisp files using interpreted code.
419 (nconc (list 'custom-declare-face (list 'quote face) spec doc) args))
420
421;;; The `defgroup' Macro.
422
d3b80e9b
SM
423(defun custom-current-group ()
424 (cdr (assoc load-file-name custom-current-group-alist)))
425
55535639
PJ
426(defun custom-declare-group (symbol members doc &rest args)
427 "Like `defgroup', but SYMBOL is evaluated as a normal argument."
428 (while members
429 (apply 'custom-add-to-group symbol (car members))
430 (setq members (cdr members)))
55535639
PJ
431 (when doc
432 ;; This text doesn't get into DOC.
433 (put symbol 'group-documentation (purecopy doc)))
434 (while args
435 (let ((arg (car args)))
436 (setq args (cdr args))
437 (unless (symbolp arg)
438 (error "Junk in args %S" args))
439 (let ((keyword arg)
440 (value (car args)))
441 (unless args
442 (error "Keyword %s is missing an argument" keyword))
443 (setq args (cdr args))
444 (cond ((eq keyword :prefix)
b6f8ba09 445 (put symbol 'custom-prefix (purecopy value)))
55535639
PJ
446 (t
447 (custom-handle-keyword symbol keyword value
448 'custom-group))))))
d3b80e9b
SM
449 ;; Record the group on the `current' list.
450 (let ((elt (assoc load-file-name custom-current-group-alist)))
451 (if elt (setcdr elt symbol)
af89cf77
DN
452 (push (cons (purecopy load-file-name) symbol)
453 custom-current-group-alist)))
55535639
PJ
454 (run-hooks 'custom-define-hook)
455 symbol)
456
457(defmacro defgroup (symbol members doc &rest args)
458 "Declare SYMBOL as a customization group containing MEMBERS.
459SYMBOL does not need to be quoted.
460
7a41cd7f
GM
461Third argument DOC is the group documentation. This should be a short
462description of the group, beginning with a capital and ending with
463a period. Words other than the first should not be capitalized, if they
464are not usually written so.
55535639
PJ
465
466MEMBERS should be an alist of the form ((NAME WIDGET)...) where
467NAME is a symbol and WIDGET is a widget for editing that symbol.
468Useful widgets are `custom-variable' for editing variables,
469`custom-face' for edit faces, and `custom-group' for editing groups.
470
471The remaining arguments should have the form
472
473 [KEYWORD VALUE]...
474
81117bdd
BW
475For a list of valid keywords, see the common keywords listed in
476`defcustom'.
55535639 477
81117bdd
BW
478See Info node `(elisp) Customization' in the Emacs Lisp manual
479for more information."
45aacac6 480 (declare (doc-string 3))
55535639
PJ
481 ;; It is better not to use backquote in this file,
482 ;; because that makes a bootstrapping problem
483 ;; if you need to recompile all the Lisp files using interpreted code.
484 (nconc (list 'custom-declare-group (list 'quote symbol) members doc) args))
485
486(defun custom-add-to-group (group option widget)
487 "To existing GROUP add a new OPTION of type WIDGET.
488If there already is an entry for OPTION and WIDGET, nothing is done."
489 (let ((members (get group 'custom-group))
490 (entry (list option widget)))
491 (unless (member entry members)
492 (put group 'custom-group (nconc members (list entry))))))
493
29512a0f
SM
494(defun custom-group-of-mode (mode)
495 "Return the custom group corresponding to the major or minor MODE.
496If no such group is found, return nil."
497 (or (get mode 'custom-mode-group)
498 (if (or (get mode 'custom-group)
499 (and (string-match "-mode\\'" (symbol-name mode))
500 (get (setq mode (intern (substring (symbol-name mode)
501 0 (match-beginning 0))))
502 'custom-group)))
503 mode)))
504
55535639
PJ
505;;; Properties.
506
507(defun custom-handle-all-keywords (symbol args type)
508 "For customization option SYMBOL, handle keyword arguments ARGS.
509Third argument TYPE is the custom option type."
d3b80e9b 510 (unless (memq :group args)
9b6098b9 511 (custom-add-to-group (custom-current-group) symbol type))
55535639
PJ
512 (while args
513 (let ((arg (car args)))
514 (setq args (cdr args))
515 (unless (symbolp arg)
516 (error "Junk in args %S" args))
517 (let ((keyword arg)
518 (value (car args)))
519 (unless args
520 (error "Keyword %s is missing an argument" keyword))
521 (setq args (cdr args))
522 (custom-handle-keyword symbol keyword value type)))))
523
524(defun custom-handle-keyword (symbol keyword value type)
525 "For customization option SYMBOL, handle KEYWORD with VALUE.
526Fourth argument TYPE is the custom option type."
527 (if purify-flag
528 (setq value (purecopy value)))
529 (cond ((eq keyword :group)
530 (custom-add-to-group value symbol type))
531 ((eq keyword :version)
532 (custom-add-version symbol value))
4e9c705e
BW
533 ((eq keyword :package-version)
534 (custom-add-package-version symbol value))
55535639
PJ
535 ((eq keyword :link)
536 (custom-add-link symbol value))
537 ((eq keyword :load)
538 (custom-add-load symbol value))
539 ((eq keyword :tag)
540 (put symbol 'custom-tag value))
541 ((eq keyword :set-after)
542 (custom-add-dependencies symbol value))
543 (t
544 (error "Unknown keyword %s" keyword))))
545
546(defun custom-add-dependencies (symbol value)
547 "To the custom option SYMBOL, add dependencies specified by VALUE.
548VALUE should be a list of symbols. For each symbol in that list,
549this specifies that SYMBOL should be set after the specified symbol, if
550both appear in constructs like `custom-set-variables'."
551 (unless (listp value)
552 (error "Invalid custom dependency `%s'" value))
553 (let* ((deps (get symbol 'custom-dependencies))
554 (new-deps deps))
555 (while value
556 (let ((dep (car value)))
557 (unless (symbolp dep)
558 (error "Invalid custom dependency `%s'" dep))
559 (unless (memq dep new-deps)
560 (setq new-deps (cons dep new-deps)))
561 (setq value (cdr value))))
562 (unless (eq deps new-deps)
563 (put symbol 'custom-dependencies new-deps))))
e1bab181 564
55535639
PJ
565(defun custom-add-option (symbol option)
566 "To the variable SYMBOL add OPTION.
567
a4842ffa
RS
568If SYMBOL's custom type is a hook, OPTION should be a hook member.
569If SYMBOL's custom type is an alist, OPTION specifies a symbol
570to offer to the user as a possible key in the alist.
571For other custom types, this has no effect."
55535639
PJ
572 (let ((options (get symbol 'custom-options)))
573 (unless (member option options)
574 (put symbol 'custom-options (cons option options)))))
467064a4 575(defalias 'custom-add-frequent-value 'custom-add-option)
55535639
PJ
576
577(defun custom-add-link (symbol widget)
578 "To the custom option SYMBOL add the link WIDGET."
579 (let ((links (get symbol 'custom-links)))
580 (unless (member widget links)
581 (put symbol 'custom-links (cons (purecopy widget) links)))))
582
583(defun custom-add-version (symbol version)
584 "To the custom option SYMBOL add the version VERSION."
585 (put symbol 'custom-version (purecopy version)))
586
4e9c705e
BW
587(defun custom-add-package-version (symbol version)
588 "To the custom option SYMBOL add the package version VERSION."
589 (put symbol 'custom-package-version (purecopy version)))
590
55535639
PJ
591(defun custom-add-load (symbol load)
592 "To the custom option SYMBOL add the dependency LOAD.
593LOAD should be either a library file name, or a feature name."
594 (let ((loads (get symbol 'custom-loads)))
595 (unless (member load loads)
596 (put symbol 'custom-loads (cons (purecopy load) loads)))))
597
d54fbdfd
SM
598(defun custom-autoload (symbol load &optional noset)
599 "Mark SYMBOL as autoloaded custom variable and add dependency LOAD.
600If NOSET is non-nil, don't bother autoloading LOAD when setting the variable."
601 (put symbol 'custom-autoload (if noset 'noset t))
1669290d
MR
602 (custom-add-load symbol load))
603
1669290d 604(defun custom-variable-p (variable)
0b21c100
CY
605 "Return non-nil if VARIABLE is a customizable variable.
606A customizable variable is either (i) a variable whose property
607list contains a non-nil `standard-value' or `custom-autoload'
608property, or (ii) an alias for another customizable variable."
b4d3bc10
CY
609 (when (symbolp variable)
610 (setq variable (indirect-variable variable))
611 (or (get variable 'standard-value)
612 (get variable 'custom-autoload))))
613
2a1e2476 614(define-obsolete-function-alias 'user-variable-p 'custom-variable-p "24.3")
1669290d 615
400b960e
RS
616(defun custom-note-var-changed (variable)
617 "Inform Custom that VARIABLE has been set (changed).
618VARIABLE is a symbol that names a user option.
619The result is that the change is treated as having been made through Custom."
400b960e 620 (put variable 'customized-value (list (custom-quote (eval variable)))))
06f5c483
JL
621
622\f
623;;; Custom Themes
400b960e 624
8f772dfd
RS
625;;; Loading files needed to customize a symbol.
626;;; This is in custom.el because menu-bar.el needs it for toggle cmds.
627
628(defvar custom-load-recursion nil
629 "Hack to avoid recursive dependencies.")
630
631(defun custom-load-symbol (symbol)
632 "Load all dependencies for SYMBOL."
633 (unless custom-load-recursion
29512a0f 634 (let ((custom-load-recursion t))
a8c78057
RS
635 ;; Load these files if not already done,
636 ;; to make sure we know all the dependencies of SYMBOL.
637 (condition-case nil
638 (require 'cus-load)
639 (error nil))
640 (condition-case nil
641 (require 'cus-start)
642 (error nil))
29512a0f
SM
643 (dolist (load (get symbol 'custom-loads))
644 (cond ((symbolp load) (condition-case nil (require load) (error nil)))
645 ;; This is subsumed by the test below, but it's much faster.
8f772dfd
RS
646 ((assoc load load-history))
647 ;; This was just (assoc (locate-library load) load-history)
648 ;; but has been optimized not to load locate-library
649 ;; if not necessary.
29512a0f
SM
650 ((let ((regexp (concat "\\(\\`\\|/\\)" (regexp-quote load)
651 "\\(\\'\\|\\.\\)"))
652 (found nil))
8f772dfd 653 (dolist (loaded load-history)
c3891447
RS
654 (and (stringp (car loaded))
655 (string-match regexp (car loaded))
8f772dfd
RS
656 (setq found t)))
657 found))
658 ;; Without this, we would load cus-edit recursively.
659 ;; We are still loading it when we call this,
660 ;; and it is not in load-history yet.
661 ((equal load "cus-edit"))
29512a0f 662 (t (condition-case nil (load load) (error nil))))))))
46ce5feb 663\f
c2e2f9be
CY
664(defvar custom-local-buffer nil
665 "Non-nil, in a Customization buffer, means customize a specific buffer.
666If this variable is non-nil, it should be a buffer,
667and it means customize the local bindings of that buffer.
668This variable is a permanent local, and it normally has a local binding
669in every Customization buffer.")
670(put 'custom-local-buffer 'permanent-local t)
671
672(defun custom-set-default (variable value)
673 "Default :set function for a customizable variable.
674Normally, this sets the default value of VARIABLE to VALUE,
675but if `custom-local-buffer' is non-nil,
676this sets the local binding in that buffer instead."
677 (if custom-local-buffer
678 (with-current-buffer custom-local-buffer
679 (set variable value))
680 (set-default variable value)))
681
682(defun custom-set-minor-mode (variable value)
683 ":set function for minor mode variables.
684Normally, this sets the default value of VARIABLE to nil if VALUE
685is nil and to t otherwise,
686but if `custom-local-buffer' is non-nil,
687this sets the local binding in that buffer instead."
688 (if custom-local-buffer
689 (with-current-buffer custom-local-buffer
690 (funcall variable (if value 1 0)))
691 (funcall variable (if value 1 0))))
692
693(defun custom-quote (sexp)
4837b516 694 "Quote SEXP if it is not self quoting."
c2e2f9be
CY
695 (if (or (memq sexp '(t nil))
696 (keywordp sexp)
697 (and (listp sexp)
698 (memq (car sexp) '(lambda)))
699 (stringp sexp)
700 (numberp sexp)
701 (vectorp sexp)
702;;; (and (fboundp 'characterp)
703;;; (characterp sexp))
704 )
705 sexp
706 (list 'quote sexp)))
707
708(defun customize-mark-to-save (symbol)
709 "Mark SYMBOL for later saving.
710
711If the default value of SYMBOL is different from the standard value,
712set the `saved-value' property to a list whose car evaluates to the
713default value. Otherwise, set it to nil.
714
715To actually save the value, call `custom-save-all'.
716
4837b516 717Return non-nil if the `saved-value' property actually changed."
ec9f0a62 718 (custom-load-symbol symbol)
c2e2f9be
CY
719 (let* ((get (or (get symbol 'custom-get) 'default-value))
720 (value (funcall get symbol))
721 (saved (get symbol 'saved-value))
722 (standard (get symbol 'standard-value))
723 (comment (get symbol 'customized-variable-comment)))
4837b516 724 ;; Save default value if different from standard value.
c2e2f9be
CY
725 (if (or (null standard)
726 (not (equal value (condition-case nil
727 (eval (car standard))
728 (error nil)))))
729 (put symbol 'saved-value (list (custom-quote value)))
730 (put symbol 'saved-value nil))
731 ;; Clear customized information (set, but not saved).
732 (put symbol 'customized-value nil)
733 ;; Save any comment that might have been set.
734 (when comment
735 (put symbol 'saved-variable-comment comment))
736 (not (equal saved (get symbol 'saved-value)))))
737
738(defun customize-mark-as-set (symbol)
739 "Mark current value of SYMBOL as being set from customize.
740
741If the default value of SYMBOL is different from the saved value if any,
742or else if it is different from the standard value, set the
743`customized-value' property to a list whose car evaluates to the
744default value. Otherwise, set it to nil.
745
4837b516 746Return non-nil if the `customized-value' property actually changed."
ec9f0a62 747 (custom-load-symbol symbol)
c2e2f9be
CY
748 (let* ((get (or (get symbol 'custom-get) 'default-value))
749 (value (funcall get symbol))
750 (customized (get symbol 'customized-value))
751 (old (or (get symbol 'saved-value) (get symbol 'standard-value))))
4837b516 752 ;; Mark default value as set if different from old value.
d54fbdfd
SM
753 (if (not (and old
754 (equal value (condition-case nil
755 (eval (car old))
756 (error nil)))))
fccf2784
CY
757 (progn (put symbol 'customized-value (list (custom-quote value)))
758 (custom-push-theme 'theme-value symbol 'user 'set
759 (custom-quote value)))
c2e2f9be
CY
760 (put symbol 'customized-value nil))
761 ;; Changed?
762 (not (equal customized (get symbol 'customized-value)))))
763
764(defun custom-reevaluate-setting (symbol)
765 "Reset the value of SYMBOL by re-evaluating its saved or standard value.
766Use the :set function to do so. This is useful for customizable options
767that are defined before their standard value can really be computed.
768E.g. dumped variables whose default depends on run-time information."
769 (funcall (or (get symbol 'custom-set) 'set-default)
770 symbol
771 (eval (car (or (get symbol 'saved-value) (get symbol 'standard-value))))))
772
773\f
d358aa10
CY
774;;; Custom Themes
775
776;; Custom themes are collections of settings that can be enabled or
777;; disabled as a unit.
778
779;; Each Custom theme is defined by a symbol, called the theme name.
780;; The `theme-settings' property of the theme name records the
781;; variable and face settings of the theme. This property is a list
782;; of elements, each of the form
783;;
784;; (PROP SYMBOL THEME VALUE)
785;;
786;; - PROP is either `theme-value' or `theme-face'
787;; - SYMBOL is the face or variable name
788;; - THEME is the theme name (redundant, but simplifies the code)
789;; - VALUE is an expression that gives the theme's setting for SYMBOL.
790;;
791;; The theme name also has a `theme-feature' property, whose value is
792;; specified when the theme is defined (see `custom-declare-theme').
793;; Usually, this is just a symbol named THEME-theme. This lets
794;; external libraries call (require 'foo-theme).
795
796;; In addition, each symbol (either a variable or a face) affected by
797;; an *enabled* theme has a `theme-value' or `theme-face' property,
798;; which is a list of elements each of the form
799;;
800;; (THEME VALUE)
801;;
802;; which have the same meanings as in `theme-settings'.
803;;
804;; The `theme-value' and `theme-face' lists are ordered by decreasing
805;; theme precedence. Thus, the first element is always the one that
806;; is in effect.
807
808;; Each theme is stored in a theme file, with filename THEME-theme.el.
809;; Loading a theme basically involves calling (load "THEME-theme")
810;; This is done by the function `load-theme'. Loading a theme
811;; automatically enables it.
812;;
813;; When a theme is enabled, the `theme-value' and `theme-face'
814;; properties for the affected symbols are set. When a theme is
815;; disabled, its settings are removed from the `theme-value' and
816;; `theme-face' properties, but the theme's own `theme-settings'
817;; property remains unchanged.
818
d358aa10 819(defvar custom-known-themes '(user changed)
9c4b6e94 820 "Themes that have been defined with `deftheme'.
d358aa10 821The default value is the list (user changed). The theme `changed'
171fc304
JB
822contains the settings before custom themes are applied. The theme
823`user' contains all the settings the user customized and saved.
824Additional themes declared with the `deftheme' macro will be added
825to the front of this list.")
e1bab181 826
e1bab181
RS
827(defsubst custom-theme-p (theme)
828 "Non-nil when THEME has been defined."
829 (memq theme custom-known-themes))
830
831(defsubst custom-check-theme (theme)
832 "Check whether THEME is valid, and signal an error if it is not."
833 (unless (custom-theme-p theme)
834 (error "Unknown theme `%s'" theme)))
835
d358aa10
CY
836(defun custom-push-theme (prop symbol theme mode &optional value)
837 "Record VALUE for face or variable SYMBOL in custom theme THEME.
838PROP is `theme-face' for a face, `theme-value' for a variable.
e1bab181
RS
839
840MODE can be either the symbol `set' or the symbol `reset'. If it is the
841symbol `set', then VALUE is the value to use. If it is the symbol
d358aa10 842`reset', then SYMBOL will be removed from THEME (VALUE is ignored).
e1bab181 843
e1bab181 844See `custom-known-themes' for a list of known themes."
b2a41d12 845 (unless (memq prop '(theme-value theme-face))
d820f1fb 846 (error "Unknown theme property"))
46ce5feb 847 (let* ((old (get symbol prop))
d358aa10
CY
848 (setting (assq theme old)) ; '(theme value)
849 (theme-settings ; '(prop symbol theme value)
850 (get theme 'theme-settings)))
05d22d02
CY
851 (cond
852 ;; Remove a setting:
853 ((eq mode 'reset)
854 (when setting
855 (let (res)
856 (dolist (theme-setting theme-settings)
857 (if (and (eq (car theme-setting) prop)
858 (eq (cadr theme-setting) symbol))
859 (setq res theme-setting)))
860 (put theme 'theme-settings (delq res theme-settings)))
861 (put symbol prop (delq setting old))))
862 ;; Alter an existing setting:
863 (setting
864 (let (res)
865 (dolist (theme-setting theme-settings)
866 (if (and (eq (car theme-setting) prop)
867 (eq (cadr theme-setting) symbol))
868 (setq res theme-setting)))
869 (put theme 'theme-settings
870 (cons (list prop symbol theme value)
871 (delq res theme-settings)))
872 (setcar (cdr setting) value)))
873 ;; Add a new setting:
874 (t
875 (unless old
1485f4c0
CY
876 ;; If the user changed a variable outside of Customize, save
877 ;; the value to a fake theme, `changed'. If the theme is
878 ;; later disabled, we use this to bring back the old value.
879 ;;
880 ;; For faces, we just use `face-new-frame-defaults' to
881 ;; recompute when the theme is disabled.
882 (when (and (eq prop 'theme-value)
883 (boundp symbol))
884 (let ((sv (get symbol 'standard-value))
885 (val (symbol-value symbol)))
886 (unless (and sv (equal (eval (car sv)) val))
887 (setq old `((changed ,(custom-quote val))))))))
05d22d02
CY
888 (put symbol prop (cons (list theme value) old))
889 (put theme 'theme-settings
890 (cons (list prop symbol theme value) theme-settings))))))
891
892(defun custom-fix-face-spec (spec)
893 "Convert face SPEC, replacing obsolete :bold and :italic attributes.
894Also change :reverse-video to :inverse-video."
895 (when (listp spec)
896 (if (or (memq :bold spec)
897 (memq :italic spec)
898 (memq :inverse-video spec))
899 (let (result)
900 (while spec
901 (let ((key (car spec))
902 (val (car (cdr spec))))
903 (cond ((eq key :italic)
904 (push :slant result)
905 (push (if val 'italic 'normal) result))
906 ((eq key :bold)
907 (push :weight result)
908 (push (if val 'bold 'normal) result))
909 ((eq key :reverse-video)
910 (push :inverse-video result)
911 (push val result))
912 (t
913 (push key result)
914 (push val result))))
915 (setq spec (cddr spec)))
916 (nreverse result))
917 spec)))
c2e2f9be 918\f
55535639 919(defun custom-set-variables (&rest args)
f1a262ed
RS
920 "Install user customizations of variable values specified in ARGS.
921These settings are registered as theme `user'.
64e9f9d9 922The arguments should each be a list of the form:
55535639 923
f1a262ed 924 (SYMBOL EXP [NOW [REQUEST [COMMENT]]])
55535639 925
f1a262ed
RS
926This stores EXP (without evaluating it) as the saved value for SYMBOL.
927If NOW is present and non-nil, then also evaluate EXP and set
928the default value for the SYMBOL to the value of EXP.
e1bab181 929
f1a262ed
RS
930REQUEST is a list of features we must require in order to
931handle SYMBOL properly.
55535639 932COMMENT is a comment string about SYMBOL."
e1bab181
RS
933 (apply 'custom-theme-set-variables 'user args))
934
935(defun custom-theme-set-variables (theme &rest args)
f1a262ed
RS
936 "Initialize variables for theme THEME according to settings in ARGS.
937Each of the arguments in ARGS should be a list of this form:
e1bab181 938
f1a262ed 939 (SYMBOL EXP [NOW [REQUEST [COMMENT]]])
e1bab181 940
81927dd2
CY
941SYMBOL is the variable name, and EXP is an expression which
942evaluates to the customized value. EXP will also be stored,
943without evaluating it, in SYMBOL's `saved-value' property, so
944that it can be restored via the Customize interface. It is also
945added to the alist in SYMBOL's `theme-value' property \(by
946calling `custom-push-theme').
e1bab181 947
81927dd2
CY
948NOW, if present and non-nil, means to install the variable's
949value directly now, even if its `defcustom' declaration has not
950been executed. This is for internal use only.
951
952REQUEST is a list of features to `require' (which are loaded
953prior to evaluating EXP).
e1bab181 954
81927dd2 955COMMENT is a comment string about SYMBOL."
e1bab181 956 (custom-check-theme theme)
05d22d02 957
9277ee6c
SM
958 ;; Process all the needed autoloads before anything else, so that the
959 ;; subsequent code has all the info it needs (e.g. which var corresponds
960 ;; to a minor mode), regardless of the ordering of the variables.
961 (dolist (entry args)
962 (let* ((symbol (indirect-variable (nth 0 entry))))
963 (unless (or (get symbol 'standard-value)
964 (memq (get symbol 'custom-autoload) '(nil noset)))
965 ;; This symbol needs to be autoloaded, even just for a `set'.
966 (custom-load-symbol symbol))))
1e281896 967
9277ee6c 968 ;; Move minor modes and variables with explicit requires to the end.
059290d6
SM
969 (setq args
970 (sort args
971 (lambda (a1 a2)
972 (let* ((sym1 (car a1))
973 (sym2 (car a2))
974 (1-then-2 (memq sym1 (get sym2 'custom-dependencies)))
975 (2-then-1 (memq sym2 (get sym1 'custom-dependencies))))
976 (cond ((and 1-then-2 2-then-1)
977 (error "Circular custom dependency between `%s' and `%s'"
978 sym1 sym2))
979 (2-then-1 nil)
3d0c2a29
GM
980 ;; 1 is a dependency of 2, so needs to be set first.
981 (1-then-2)
b6790c3e
SM
982 ;; Put minor modes and symbols with :require last.
983 ;; Putting minor modes last ensures that the mode
984 ;; function will see other customized values rather
985 ;; than default values.
986 (t (or (nth 3 a2)
987 (eq (get sym2 'custom-set)
988 'custom-set-minor-mode))))))))
6b09b5d1
CY
989
990 (dolist (entry args)
991 (unless (listp entry)
992 (error "Incompatible Custom theme spec"))
993 (let* ((symbol (indirect-variable (nth 0 entry)))
994 (value (nth 1 entry)))
995 (custom-push-theme 'theme-value symbol theme 'set value)
996 (unless custom--inhibit-theme-enable
997 ;; Now set the variable.
998 (let* ((now (nth 2 entry))
999 (requests (nth 3 entry))
1000 (comment (nth 4 entry))
1001 set)
1002 (when requests
1003 (put symbol 'custom-requests requests)
1004 (mapc 'require requests))
1005 (setq set (or (get symbol 'custom-set) 'custom-set-default))
059290d6 1006 (put symbol 'saved-value (list value))
6b09b5d1
CY
1007 (put symbol 'saved-variable-comment comment)
1008 ;; Allow for errors in the case where the setter has
1009 ;; changed between versions, say, but let the user know.
1010 (condition-case data
1011 (cond (now
1012 ;; Rogue variable, set it now.
1013 (put symbol 'force-value t)
1014 (funcall set symbol (eval value)))
1015 ((default-boundp symbol)
1016 ;; Something already set this, overwrite it.
1017 (funcall set symbol (eval value))))
1018 (error
1019 (message "Error setting %s: %s" symbol data)))
1020 (and (or now (default-boundp symbol))
1021 (put symbol 'variable-comment comment)))))))
55535639 1022
46ce5feb
RS
1023\f
1024;;; Defining themes.
1025
782b5e8d
CY
1026;; A theme file is named `THEME-theme.el' (where THEME is the theme
1027;; name) found in `custom-theme-load-path'. It has this format:
d358aa10
CY
1028;;
1029;; (deftheme THEME
1030;; DOCSTRING)
1031;;
1032;; (custom-theme-set-variables
1033;; 'THEME
1034;; [THEME-VARIABLES])
1035;;
1036;; (custom-theme-set-faces
1037;; 'THEME
1038;; [THEME-FACES])
1039;;
1040;; (provide-theme 'THEME)
46ce5feb 1041
46ce5feb 1042
d358aa10
CY
1043;; The IGNORED arguments to deftheme come from the XEmacs theme code, where
1044;; they were used to supply keyword-value pairs like `:immediate',
1045;; `:variable-reset-string', etc. We don't use any of these, so ignore them.
6d912ee1 1046
d358aa10
CY
1047(defmacro deftheme (theme &optional doc &rest ignored)
1048 "Declare THEME to be a Custom theme.
1049The optional argument DOC is a doc string describing the theme.
46ce5feb
RS
1050
1051Any theme `foo' should be defined in a file called `foo-theme.el';
1052see `custom-make-theme-feature' for more information."
b581bb5c 1053 (declare (doc-string 2))
46ce5feb
RS
1054 (let ((feature (custom-make-theme-feature theme)))
1055 ;; It is better not to use backquote in this file,
1056 ;; because that makes a bootstrapping problem
1057 ;; if you need to recompile all the Lisp files using interpreted code.
d358aa10 1058 (list 'custom-declare-theme (list 'quote theme) (list 'quote feature) doc)))
46ce5feb 1059
d358aa10 1060(defun custom-declare-theme (theme feature &optional doc &rest ignored)
46ce5feb 1061 "Like `deftheme', but THEME is evaluated as a normal argument.
d358aa10
CY
1062FEATURE is the feature this theme provides. Normally, this is a symbol
1063created from THEME by `custom-make-theme-feature'."
29a4c45b
CY
1064 (unless (custom-theme-name-valid-p theme)
1065 (error "Custom theme cannot be named %S" theme))
46ce5feb
RS
1066 (add-to-list 'custom-known-themes theme)
1067 (put theme 'theme-feature feature)
d358aa10 1068 (when doc (put theme 'theme-documentation doc)))
46ce5feb
RS
1069
1070(defun custom-make-theme-feature (theme)
1071 "Given a symbol THEME, create a new symbol by appending \"-theme\".
1072Store this symbol in the `theme-feature' property of THEME.
1073Calling `provide-theme' to provide THEME actually puts `THEME-theme'
1074into `features'.
1075
1076This allows for a file-name convention for autoloading themes:
1077Every theme X has a property `provide-theme' whose value is \"X-theme\".
87d737ae 1078\(load-theme X) then attempts to load the file `X-theme.el'."
46ce5feb
RS
1079 (intern (concat (symbol-name theme) "-theme")))
1080\f
1081;;; Loading themes.
1082
782b5e8d
CY
1083(defcustom custom-theme-directory user-emacs-directory
1084 "Default user directory for storing custom theme files.
1085The command `customize-create-theme' writes theme files into this
1086directory. By default, Emacs searches for custom themes in this
1087directory first---see `custom-theme-load-path'."
9c4b6e94
LT
1088 :type 'string
1089 :group 'customize
1090 :version "22.1")
1091
782b5e8d
CY
1092(defcustom custom-theme-load-path (list 'custom-theme-directory t)
1093 "List of directories to search for custom theme files.
29a4c45b
CY
1094When loading custom themes (e.g. in `customize-themes' and
1095`load-theme'), Emacs searches for theme files in the specified
782b5e8d 1096order. Each element in the list should be one of the following:
647bc502
CY
1097- the symbol `custom-theme-directory', meaning the value of
1098 `custom-theme-directory'.
1099- the symbol t, meaning the built-in theme directory (a directory
1100 named \"themes\" in `data-directory').
29a4c45b
CY
1101- a directory name (a string).
1102
171fc304 1103Each theme file is named THEME-theme.el, where THEME is the theme
29a4c45b 1104name."
782b5e8d
CY
1105 :type '(repeat (choice (const :tag "custom-theme-directory"
1106 custom-theme-directory)
1107 (const :tag "Built-in theme directory" t)
1108 directory))
1109 :group 'customize
1110 :version "24.1")
1111
6b09b5d1 1112(defvar custom--inhibit-theme-enable nil
fccee4ab
CY
1113 "Whether the custom-theme-set-* functions act immediately.
1114If nil, `custom-theme-set-variables' and `custom-theme-set-faces'
1115change the current values of the given variable or face. If
1116non-nil, they just make a record of the theme settings.")
6b09b5d1 1117
e1bab181 1118(defun provide-theme (theme)
d358aa10
CY
1119 "Indicate that this file provides THEME.
1120This calls `provide' to provide the feature name stored in THEME's
1121property `theme-feature' (which is usually a symbol created by
1122`custom-make-theme-feature')."
29a4c45b
CY
1123 (unless (custom-theme-name-valid-p theme)
1124 (error "Custom theme cannot be named %S" theme))
e1bab181 1125 (custom-check-theme theme)
fccee4ab 1126 (provide (get theme 'theme-feature)))
6b09b5d1 1127
b7617f6d 1128(defcustom custom-safe-themes '(default)
04c52e2f 1129 "Themes that are considered safe to load.
1de76afe 1130If the value is a list, each element should be either the SHA-256
04c52e2f
CY
1131hash of a safe theme file, or the symbol `default', which stands
1132for any theme in the built-in Emacs theme directory (a directory
1133named \"themes\" in `data-directory').
1134
04482335
CY
1135If the value is t, Emacs treats all themes as safe.
1136
1137This variable cannot be set in a Custom theme."
04c52e2f
CY
1138 :type '(choice (repeat :tag "List of safe themes"
1139 (choice string
1140 (const :tag "Built-in themes" default)))
1141 (const :tag "All themes" t))
278f6845 1142 :group 'customize
b7617f6d 1143 :risky t
278f6845
CY
1144 :version "24.1")
1145
658d8eb8 1146(defun load-theme (theme &optional no-confirm no-enable)
928f4e73 1147 "Load Custom theme named THEME from its file.
928f4e73
CY
1148The theme file is named THEME-theme.el, in one of the directories
1149specified by `custom-theme-load-path'.
278f6845 1150
abd1f678
CY
1151If the theme is not considered safe by `custom-safe-themes',
1152prompt the user for confirmation before loading it. But if
1153optional arg NO-CONFIRM is non-nil, load the theme without
1154prompting.
658d8eb8 1155
4125cb8b
CY
1156Normally, this function also enables THEME. If optional arg
1157NO-ENABLE is non-nil, load the theme but don't enable it, unless
1158the theme was already enabled.
658d8eb8
CY
1159
1160This function is normally called through Customize when setting
1161`custom-enabled-themes'. If used directly in your init file, it
1162should be called with a non-nil NO-CONFIRM argument, or after
1163`custom-safe-themes' has been loaded.
1164
928f4e73 1165Return t if THEME was successfully loaded, nil otherwise."
05d22d02
CY
1166 (interactive
1167 (list
1168 (intern (completing-read "Load custom theme: "
6b09b5d1 1169 (mapcar 'symbol-name
658d8eb8
CY
1170 (custom-available-themes))))
1171 nil nil))
6b09b5d1
CY
1172 (unless (custom-theme-name-valid-p theme)
1173 (error "Invalid theme name `%s'" theme))
4125cb8b
CY
1174 ;; If THEME is already enabled, re-enable it after loading, even if
1175 ;; NO-ENABLE is t.
1176 (if no-enable
1177 (setq no-enable (not (custom-theme-enabled-p theme))))
73e60f53
CY
1178 ;; If reloading, clear out the old theme settings.
1179 (when (custom-theme-p theme)
1180 (disable-theme theme)
1181 (put theme 'theme-settings nil)
1182 (put theme 'theme-feature nil)
1183 (put theme 'theme-documentation nil))
6b09b5d1 1184 (let ((fn (locate-file (concat (symbol-name theme) "-theme.el")
782b5e8d 1185 (custom-theme--load-path)
b7617f6d
CY
1186 '("" "c")))
1187 hash)
6b09b5d1 1188 (unless fn
171fc304 1189 (error "Unable to find theme file for `%s'" theme))
b7617f6d
CY
1190 (with-temp-buffer
1191 (insert-file-contents fn)
1de76afe 1192 (setq hash (secure-hash 'sha256 (current-buffer)))
928f4e73
CY
1193 ;; Check file safety with `custom-safe-themes', prompting the
1194 ;; user if necessary.
658d8eb8 1195 (when (or no-confirm
04c52e2f 1196 (eq custom-safe-themes t)
658d8eb8 1197 (and (memq 'default custom-safe-themes)
b7617f6d
CY
1198 (equal (file-name-directory fn)
1199 (expand-file-name "themes/" data-directory)))
1200 (member hash custom-safe-themes)
928f4e73 1201 (custom-theme-load-confirm hash))
fccee4ab
CY
1202 (let ((custom--inhibit-theme-enable t))
1203 (eval-buffer))
3b2ff876
CY
1204 ;; Optimization: if the theme changes the `default' face, put that
1205 ;; entry first. This avoids some `frame-set-background-mode' rigmarole
1206 ;; by assigning the new background immediately.
1207 (let* ((settings (get theme 'theme-settings))
1208 (tail settings)
1209 found)
1210 (while (and tail (not found))
1211 (and (eq (nth 0 (car tail)) 'theme-face)
1212 (eq (nth 1 (car tail)) 'default)
1213 (setq found (car tail)))
1214 (setq tail (cdr tail)))
1215 (if found
1216 (put theme 'theme-settings (cons found (delq found settings)))))
1217 ;; Finally, enable the theme.
fccee4ab
CY
1218 (unless no-enable
1219 (enable-theme theme))
1220 t))))
b7617f6d
CY
1221
1222(defun custom-theme-load-confirm (hash)
1223 "Query the user about loading a Custom theme that may not be safe.
1224The theme should be in the current buffer. If the user agrees,
1225query also about adding HASH to `custom-safe-themes'."
011474aa
CY
1226 (unless noninteractive
1227 (save-window-excursion
1228 (rename-buffer "*Custom Theme*" t)
1229 (emacs-lisp-mode)
399a361b 1230 (pop-to-buffer (current-buffer))
011474aa
CY
1231 (goto-char (point-min))
1232 (prog1 (when (y-or-n-p "Loading a theme can run Lisp code. Really load? ")
1233 ;; Offer to save to `custom-safe-themes'.
1234 (and (or custom-file user-init-file)
1235 (y-or-n-p "Treat this theme as safe in future sessions? ")
1236 (customize-push-and-save 'custom-safe-themes (list hash)))
1237 t)
1238 (quit-window)))))
6b09b5d1
CY
1239
1240(defun custom-theme-name-valid-p (name)
1241 "Return t if NAME is a valid name for a Custom theme, nil otherwise.
1242NAME should be a symbol."
1243 (and (symbolp name)
1244 name
1245 (not (or (zerop (length (symbol-name name)))
6b09b5d1
CY
1246 (eq name 'user)
1247 (eq name 'changed)))))
05d22d02
CY
1248
1249(defun custom-available-themes ()
6b09b5d1 1250 "Return a list of available Custom themes (symbols)."
171fc304 1251 (let (sym themes)
782b5e8d
CY
1252 (dolist (dir (custom-theme--load-path))
1253 (when (file-directory-p dir)
1254 (dolist (file (file-expand-wildcards
1255 (expand-file-name "*-theme.el" dir) t))
1256 (setq file (file-name-nondirectory file))
1257 (and (string-match "\\`\\(.+\\)-theme.el\\'" file)
1258 (setq sym (intern (match-string 1 file)))
1259 (custom-theme-name-valid-p sym)
1260 (push sym themes)))))
2bb5649e 1261 (nreverse (delete-dups themes))))
782b5e8d
CY
1262
1263(defun custom-theme--load-path ()
1264 (let (lpath)
1265 (dolist (f custom-theme-load-path)
1266 (cond ((eq f 'custom-theme-directory)
1267 (setq f custom-theme-directory))
1268 ((eq f t)
1269 (setq f (expand-file-name "themes" data-directory))))
1270 (if (file-directory-p f)
1271 (push f lpath)))
1272 (nreverse lpath)))
1273
46ce5feb
RS
1274\f
1275;;; Enabling and disabling loaded themes.
1276
87d737ae 1277(defun enable-theme (theme)
46ce5feb 1278 "Reenable all variable and face settings defined by THEME.
fccee4ab
CY
1279THEME should be either `user', or a theme loaded via `load-theme'.
1280After this function completes, THEME will have the highest
1281precedence (after `user')."
05d22d02
CY
1282 (interactive (list (intern
1283 (completing-read
1284 "Enable custom theme: "
fccee4ab 1285 obarray (lambda (sym) (get sym 'theme-settings)) t))))
d358aa10 1286 (if (not (custom-theme-p theme))
fccee4ab
CY
1287 (error "Undefined Custom theme %s" theme))
1288 (let ((settings (get theme 'theme-settings)))
1289 ;; Loop through theme settings, recalculating vars/faces.
1290 (dolist (s settings)
1291 (let* ((prop (car s))
1292 (symbol (cadr s))
1293 (spec-list (get symbol prop)))
1294 (put symbol prop (cons (cddr s) (assq-delete-all theme spec-list)))
1295 (cond
1296 ((eq prop 'theme-face)
1297 (custom-theme-recalc-face symbol))
1298 ((eq prop 'theme-value)
04482335
CY
1299 ;; Ignore `custom-enabled-themes' and `custom-safe-themes'.
1300 (unless (memq symbol '(custom-enabled-themes custom-safe-themes))
fccee4ab
CY
1301 (custom-theme-recalc-variable symbol)))))))
1302 (unless (eq theme 'user)
1303 (setq custom-enabled-themes
1304 (cons theme (delq theme custom-enabled-themes)))
1305 ;; Give the `user' theme the highest priority.
1306 (enable-theme 'user)))
b2a41d12
CY
1307
1308(defcustom custom-enabled-themes nil
1309 "List of enabled Custom Themes, highest precedence first.
fccee4ab
CY
1310This list does not include the `user' theme, which is set by
1311Customize and always takes precedence over other Custom Themes.
b2a41d12 1312
fccee4ab 1313This variable cannot be defined inside a Custom theme; there, it
658d8eb8
CY
1314is simply ignored.
1315
1316Setting this variable through Customize calls `enable-theme' or
1317`load-theme' for each theme in the list."
b2a41d12
CY
1318 :group 'customize
1319 :type '(repeat symbol)
928f4e73
CY
1320 :set-after '(custom-theme-directory custom-theme-load-path
1321 custom-safe-themes)
7fd8732d 1322 :risky t
b2a41d12 1323 :set (lambda (symbol themes)
fccee4ab
CY
1324 (let (failures)
1325 (setq themes (delq 'user (delete-dups themes)))
1326 ;; Disable all themes not in THEMES.
1327 (if (boundp symbol)
1328 (dolist (theme (symbol-value symbol))
1329 (if (not (memq theme themes))
1330 (disable-theme theme))))
1331 ;; Call `enable-theme' or `load-theme' on each of THEMES.
1332 (dolist (theme (reverse themes))
1333 (condition-case nil
1334 (if (custom-theme-p theme)
1335 (enable-theme theme)
1336 (load-theme theme))
1337 (error (setq failures (cons theme failures)
1338 themes (delq theme themes)))))
1339 (enable-theme 'user)
1340 (custom-set-default symbol themes)
1341 (if failures
1342 (message "Failed to enable theme: %s"
1343 (mapconcat 'symbol-name failures ", "))))))
b2a41d12 1344
d358aa10 1345(defsubst custom-theme-enabled-p (theme)
b2a41d12
CY
1346 "Return non-nil if THEME is enabled."
1347 (memq theme custom-enabled-themes))
46ce5feb 1348
87d737ae 1349(defun disable-theme (theme)
46ce5feb 1350 "Disable all variable and face settings defined by THEME.
b2a41d12 1351See `custom-enabled-themes' for a list of enabled themes."
d358aa10
CY
1352 (interactive (list (intern
1353 (completing-read
05d22d02 1354 "Disable custom theme: "
d358aa10
CY
1355 (mapcar 'symbol-name custom-enabled-themes)
1356 nil t))))
1357 (when (custom-theme-enabled-p theme)
8913f945
RS
1358 (let ((settings (get theme 'theme-settings)))
1359 (dolist (s settings)
05d22d02 1360 (let* ((prop (car s))
8913f945 1361 (symbol (cadr s))
05d22d02
CY
1362 (val (assq-delete-all theme (get symbol prop))))
1363 (put symbol prop val)
1364 (cond
1365 ((eq prop 'theme-value)
1366 (custom-theme-recalc-variable symbol))
1367 ((eq prop 'theme-face)
1368 ;; If the face spec specified by this theme is in the
1369 ;; saved-face property, reset that property.
1370 (when (equal (nth 3 s) (get symbol 'saved-face))
1485f4c0
CY
1371 (put symbol 'saved-face (and val (cadr (car val)))))))))
1372 ;; Recompute faces on all frames.
1373 (dolist (frame (frame-list))
1374 ;; We must reset the fg and bg color frame parameters, or
1375 ;; `face-set-after-frame-default' will use the existing
1376 ;; parameters, which could be from the disabled theme.
1377 (set-frame-parameter frame 'background-color
1378 (custom--frame-color-default
1379 frame :background "background" "Background"
1380 "unspecified-bg" "white"))
1381 (set-frame-parameter frame 'foreground-color
1382 (custom--frame-color-default
1383 frame :foreground "foreground" "Foreground"
1384 "unspecified-fg" "black"))
1385 (face-set-after-frame-default frame))
05d22d02
CY
1386 (setq custom-enabled-themes
1387 (delq theme custom-enabled-themes)))))
46ce5feb 1388
1485f4c0
CY
1389(defun custom--frame-color-default (frame attribute resource-attr resource-class
1390 tty-default x-default)
1391 (let ((col (face-attribute 'default attribute t)))
1392 (cond
1393 ((and col (not (eq col 'unspecified))) col)
1394 ((null (window-system frame)) tty-default)
1395 ((setq col (x-get-resource resource-attr resource-class)) col)
1396 (t x-default))))
1397
46ce5feb
RS
1398(defun custom-variable-theme-value (variable)
1399 "Return (list VALUE) indicating the custom theme value of VARIABLE.
1400That is to say, it specifies what the value should be according to
1401currently enabled custom themes.
1402
1403This function returns nil if no custom theme specifies a value for VARIABLE."
171fc304 1404 (let ((theme-value (get variable 'theme-value)))
46ce5feb 1405 (if theme-value
d358aa10 1406 (cdr (car theme-value)))))
46ce5feb 1407
46ce5feb
RS
1408(defun custom-theme-recalc-variable (variable)
1409 "Set VARIABLE according to currently enabled custom themes."
1410 (let ((valspec (custom-variable-theme-value variable)))
d358aa10
CY
1411 (if valspec
1412 (put variable 'saved-value valspec)
46ce5feb 1413 (setq valspec (get variable 'standard-value)))
d358aa10
CY
1414 (if (and valspec
1415 (or (get variable 'force-value)
1416 (default-boundp variable)))
1417 (funcall (or (get variable 'custom-set) 'set-default) variable
1418 (eval (car valspec))))))
46ce5feb
RS
1419
1420(defun custom-theme-recalc-face (face)
1421 "Set FACE according to currently enabled custom themes."
05d22d02
CY
1422 (if (get face 'face-alias)
1423 (setq face (get face 'face-alias)))
4983ddea
CY
1424 ;; Reset the faces for each frame.
1425 (dolist (frame (frame-list))
1426 (face-spec-recalc face frame)))
05d22d02 1427
46ce5feb 1428\f
9173deec 1429;;; XEmacs compatibility functions
d358aa10
CY
1430
1431;; In XEmacs, when you reset a Custom Theme, you have to specify the
1432;; theme to reset it to. We just apply the next available theme, so
1433;; just ignore the IGNORED arguments.
1434
e1bab181 1435(defun custom-theme-reset-variables (theme &rest args)
d358aa10 1436 "Reset some variable settings in THEME to their values in other themes.
46ce5feb 1437Each of the arguments ARGS has this form:
e1bab181 1438
d358aa10 1439 (VARIABLE IGNORED)
e1bab181 1440
7e8b9dc3 1441This means reset VARIABLE. (The argument IGNORED is ignored)."
e1bab181 1442 (custom-check-theme theme)
46ce5feb 1443 (dolist (arg args)
d358aa10 1444 (custom-push-theme 'theme-value (car arg) theme 'reset)))
e1bab181
RS
1445
1446(defun custom-reset-variables (&rest args)
d358aa10 1447 "Reset the specs of some variables to their values in other themes.
46ce5feb 1448This creates settings in the `user' theme.
e1bab181 1449
46ce5feb 1450Each of the arguments ARGS has this form:
e1bab181 1451
d358aa10 1452 (VARIABLE IGNORED)
e1bab181 1453
7e8b9dc3 1454This means reset VARIABLE. (The argument IGNORED is ignored)."
e1bab181
RS
1455 (apply 'custom-theme-reset-variables 'user args))
1456
55535639
PJ
1457;;; The End.
1458
1459;; Process the defcustoms for variables loaded before this file.
1460(while custom-declare-variable-list
1461 (apply 'custom-declare-variable (car custom-declare-variable-list))
1462 (setq custom-declare-variable-list (cdr custom-declare-variable-list)))
1463
1464(provide 'custom)
1465
1466;;; custom.el ends here