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