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