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