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