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