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