Fix typos.
[bpt/emacs.git] / lisp / custom.el
1 ;;; custom.el --- tools for declaring and initializing options
2 ;;
3 ;; Copyright (C) 1996-1997, 1999, 2001-2011 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 See `send-mail-function' in sendmail.el for an example."
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).
129 (push symbol custom-delayed-init-variables))
130
131 (defun custom-declare-variable (symbol default doc &rest args)
132 "Like `defcustom', but SYMBOL and DEFAULT are evaluated as normal arguments.
133 DEFAULT should be an expression to evaluate to compute the default value,
134 not the default value itself.
135
136 DEFAULT is stored as SYMBOL's standard value, in SYMBOL's property
137 `standard-value'. At the same time, SYMBOL's property `force-value' is
138 set to nil, as the value is no longer rogue."
139 (put symbol 'standard-value (purecopy (list default)))
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))
143 (if (keywordp doc)
144 (error "Doc string is missing"))
145 (let ((initialize 'custom-initialize-reset)
146 (requests nil))
147 (unless (memq :group args)
148 (custom-add-to-group (custom-current-group) symbol 'custom-variable))
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)
166 (push value requests))
167 ((eq keyword :risky)
168 (put symbol 'risky-local-variable value))
169 ((eq keyword :safe)
170 (put symbol 'safe-local-variable value))
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))
178 value)
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.
186 (unless custom-dont-initialize
187 (funcall initialize symbol default)))
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)))))
195 (push symbol current-load-list)
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.
201 DOC is the variable documentation.
202
203 Neither SYMBOL nor VALUE need to be quoted.
204 If SYMBOL is not already bound, initialize it to VALUE.
205 The remaining arguments should have the form
206
207 [KEYWORD VALUE]...
208
209 The following keywords are meaningful:
210
211 :type VALUE should be a widget type for editing the symbol's value.
212 :options VALUE should be a list of valid members of the widget type.
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'.
218 :set VALUE should be a function to set the value of the symbol
219 when using the Customize user interface.
220 It takes two arguments, the symbol to set and the value to
221 give it. The default choice of function is `set-default'.
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
225 is `default-value'.
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.
230 :risky Set SYMBOL's `risky-local-variable' property to VALUE.
231 :safe Set SYMBOL's `safe-local-variable' property to VALUE.
232
233 The following common keywords are also meaningful.
234
235 :group VALUE should be a customization group.
236 Add SYMBOL (or FACE with `defface') to that group.
237 :link LINK-DATA
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.
241
242 There are several alternatives you can use for LINK-DATA:
243
244 (custom-manual INFO-NODE)
245 Link to an Info node; INFO-NODE is a string which specifies
246 the node name, as in \"(emacs)Top\".
247
248 (info-link INFO-NODE)
249 Like `custom-manual' except that the link appears in the
250 customization buffer with the Info node name.
251
252 (url-link URL)
253 Link to a web page; URL is a string which specifies the URL.
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.
272
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
275 example, (info-link :tag \"foo\" \"(emacs)Top\") makes a link to the
276 Emacs manual which appears in the buffer as `foo'.
277
278 An item can have more than one external link; however, most items
279 have none at all.
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.
284 :package-version
285 VALUE should be a list with the form (PACKAGE . VERSION)
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'.
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.
293 :tag LABEL
294 Use LABEL, a string, instead of the item's name, to label the item
295 in customization menus and buffers.
296 :load FILE
297 Load file FILE (a string) before displaying this customization
298 item. Loading is done with `load', and only if the file is
299 not already loaded.
300 :set-after VARIABLES
301 Specifies that SYMBOL should be set after the list of variables
302 VARIABLES when both have been customized.
303
304 If SYMBOL has a local binding, then this form affects the local
305 binding. This is normally not what you want. Thus, if you need
306 to 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
311 See Info node `(elisp) Customization' in the Emacs Lisp manual
312 for more information."
313 (declare (doc-string 3) (debug (name body)))
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.
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))
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.
335 FACE does not need to be quoted.
336
337 Third argument DOC is the face documentation.
338
339 If FACE has been set with `custom-set-faces', set the face attributes
340 as specified by that function, otherwise set the face attributes
341 according to SPEC.
342
343 The remaining arguments should have the form
344
345 [KEYWORD VALUE]...
346
347 For a list of valid keywords, see the common keywords listed in
348 `defcustom'.
349
350 SPEC should be an alist of the form ((DISPLAY ATTS)...).
351
352 In the first element, DISPLAY can be `default'. The ATTS in that
353 element then act as defaults for all the following elements.
354
355 Aside from that, DISPLAY specifies conditions to match some or
356 all frames. For each frame, the first element of SPEC where the
357 DISPLAY conditions are satisfied is the one that applies to that
358 frame. The ATTRs in this element take effect, and the following
359 elements are ignored, on that frame.
360
361 In the last element, DISPLAY can be t. That element applies to a
362 frame if none of the previous elements (except the `default' if
363 any) did.
364
365 ATTS is a list of face attributes followed by their values:
366 (ATTR VALUE ATTR VALUE...)
367
368 The possible attributes are `:family', `:width', `:height', `:weight',
369 `:slant', `:underline', `:overline', `:strike-through', `:box',
370 `:foreground', `:background', `:stipple', `:inverse-video', and `:inherit'.
371
372 DISPLAY can be `default' (only in the first element), the symbol
373 t (only in the last element) to match all frames, or an alist of
374 conditions of the form \(REQ ITEM...). For such an alist to
375 match a frame, each of the conditions must be satisfied, meaning
376 that the REQ property of the frame must match one of the
377 corresponding ITEMs. These are the defined REQ values:
378
379 `type' (the value of `window-system')
380 Under X, in addition to the values `window-system' can take,
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.
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
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
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
399 See Info node `(elisp) Customization' in the Emacs Lisp manual
400 for more information."
401 (declare (doc-string 3))
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
409 (defun custom-current-group ()
410 (cdr (assoc load-file-name custom-current-group-alist)))
411
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)))
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)
431 (put symbol 'custom-prefix (purecopy value)))
432 (t
433 (custom-handle-keyword symbol keyword value
434 'custom-group))))))
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)
438 (push (cons (purecopy load-file-name) symbol)
439 custom-current-group-alist)))
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.
445 SYMBOL does not need to be quoted.
446
447 Third argument DOC is the group documentation. This should be a short
448 description of the group, beginning with a capital and ending with
449 a period. Words other than the first should not be capitalized, if they
450 are not usually written so.
451
452 MEMBERS should be an alist of the form ((NAME WIDGET)...) where
453 NAME is a symbol and WIDGET is a widget for editing that symbol.
454 Useful widgets are `custom-variable' for editing variables,
455 `custom-face' for edit faces, and `custom-group' for editing groups.
456
457 The remaining arguments should have the form
458
459 [KEYWORD VALUE]...
460
461 For a list of valid keywords, see the common keywords listed in
462 `defcustom'.
463
464 See Info node `(elisp) Customization' in the Emacs Lisp manual
465 for more information."
466 (declare (doc-string 3))
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.
474 If 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
480 (defun custom-group-of-mode (mode)
481 "Return the custom group corresponding to the major or minor MODE.
482 If 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
491 ;;; Properties.
492
493 (defun custom-handle-all-keywords (symbol args type)
494 "For customization option SYMBOL, handle keyword arguments ARGS.
495 Third argument TYPE is the custom option type."
496 (unless (memq :group args)
497 (custom-add-to-group (custom-current-group) symbol type))
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.
512 Fourth 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))
519 ((eq keyword :package-version)
520 (custom-add-package-version symbol value))
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.
534 VALUE should be a list of symbols. For each symbol in that list,
535 this specifies that SYMBOL should be set after the specified symbol, if
536 both 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))))
550
551 (defun custom-add-option (symbol option)
552 "To the variable SYMBOL add OPTION.
553
554 If SYMBOL's custom type is a hook, OPTION should be a hook member.
555 If SYMBOL's custom type is an alist, OPTION specifies a symbol
556 to offer to the user as a possible key in the alist.
557 For other custom types, this has no effect."
558 (let ((options (get symbol 'custom-options)))
559 (unless (member option options)
560 (put symbol 'custom-options (cons option options)))))
561 (defalias 'custom-add-frequent-value 'custom-add-option)
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
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
577 (defun custom-add-load (symbol load)
578 "To the custom option SYMBOL add the dependency LOAD.
579 LOAD 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
584 (defun custom-autoload (symbol load &optional noset)
585 "Mark SYMBOL as autoloaded custom variable and add dependency LOAD.
586 If NOSET is non-nil, don't bother autoloading LOAD when setting the variable."
587 (put symbol 'custom-autoload (if noset 'noset t))
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)
592 "Return non-nil if VARIABLE is a custom variable.
593 This recursively follows aliases."
594 (setq variable (indirect-variable variable))
595 (or (get variable 'standard-value)
596 (get variable 'custom-autoload)))
597
598 (defun custom-note-var-changed (variable)
599 "Inform Custom that VARIABLE has been set (changed).
600 VARIABLE is a symbol that names a user option.
601 The result is that the change is treated as having been made through Custom."
602 (put variable 'customized-value (list (custom-quote (eval variable)))))
603
604 \f
605 ;;; Custom Themes
606
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
616 (let ((custom-load-recursion t))
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))
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.
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.
632 ((let ((regexp (concat "\\(\\`\\|/\\)" (regexp-quote load)
633 "\\(\\'\\|\\.\\)"))
634 (found nil))
635 (dolist (loaded load-history)
636 (and (stringp (car loaded))
637 (string-match regexp (car loaded))
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"))
644 (t (condition-case nil (load load) (error nil))))))))
645 \f
646 (defvar custom-local-buffer nil
647 "Non-nil, in a Customization buffer, means customize a specific buffer.
648 If this variable is non-nil, it should be a buffer,
649 and it means customize the local bindings of that buffer.
650 This variable is a permanent local, and it normally has a local binding
651 in 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.
656 Normally, this sets the default value of VARIABLE to VALUE,
657 but if `custom-local-buffer' is non-nil,
658 this 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.
666 Normally, this sets the default value of VARIABLE to nil if VALUE
667 is nil and to t otherwise,
668 but if `custom-local-buffer' is non-nil,
669 this 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)
676 "Quote SEXP if it is not self quoting."
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
693 If the default value of SYMBOL is different from the standard value,
694 set the `saved-value' property to a list whose car evaluates to the
695 default value. Otherwise, set it to nil.
696
697 To actually save the value, call `custom-save-all'.
698
699 Return non-nil if the `saved-value' property actually changed."
700 (custom-load-symbol symbol)
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)))
706 ;; Save default value if different from standard value.
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
723 If the default value of SYMBOL is different from the saved value if any,
724 or else if it is different from the standard value, set the
725 `customized-value' property to a list whose car evaluates to the
726 default value. Otherwise, set it to nil.
727
728 Return non-nil if the `customized-value' property actually changed."
729 (custom-load-symbol symbol)
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))))
734 ;; Mark default value as set if different from old value.
735 (if (not (and old
736 (equal value (condition-case nil
737 (eval (car old))
738 (error nil)))))
739 (progn (put symbol 'customized-value (list (custom-quote value)))
740 (custom-push-theme 'theme-value symbol 'user 'set
741 (custom-quote value)))
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.
748 Use the :set function to do so. This is useful for customizable options
749 that are defined before their standard value can really be computed.
750 E.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
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
801 (defvar custom-known-themes '(user changed)
802 "Themes that have been defined with `deftheme'.
803 The default value is the list (user changed). The theme `changed'
804 contains the settings before custom themes are applied. The theme
805 `user' contains all the settings the user customized and saved.
806 Additional themes declared with the `deftheme' macro will be added
807 to the front of this list.")
808
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
818 (defun custom-push-theme (prop symbol theme mode &optional value)
819 "Record VALUE for face or variable SYMBOL in custom theme THEME.
820 PROP is `theme-face' for a face, `theme-value' for a variable.
821
822 MODE can be either the symbol `set' or the symbol `reset'. If it is the
823 symbol `set', then VALUE is the value to use. If it is the symbol
824 `reset', then SYMBOL will be removed from THEME (VALUE is ignored).
825
826 See `custom-known-themes' for a list of known themes."
827 (unless (memq prop '(theme-value theme-face))
828 (error "Unknown theme property"))
829 (let* ((old (get symbol prop))
830 (setting (assq theme old)) ; '(theme value)
831 (theme-settings ; '(prop symbol theme value)
832 (get theme 'theme-settings)))
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
858 ;; If the user changed a variable outside of Customize, save
859 ;; the value to a fake theme, `changed'. If the theme is
860 ;; later disabled, we use this to bring back the old value.
861 ;;
862 ;; For faces, we just use `face-new-frame-defaults' to
863 ;; recompute when the theme is disabled.
864 (when (and (eq prop 'theme-value)
865 (boundp symbol))
866 (let ((sv (get symbol 'standard-value))
867 (val (symbol-value symbol)))
868 (unless (and sv (equal (eval (car sv)) val))
869 (setq old `((changed ,(custom-quote val))))))))
870 (put symbol prop (cons (list theme value) old))
871 (put theme 'theme-settings
872 (cons (list prop symbol theme value) theme-settings))))))
873
874 (defun custom-fix-face-spec (spec)
875 "Convert face SPEC, replacing obsolete :bold and :italic attributes.
876 Also change :reverse-video to :inverse-video."
877 (when (listp spec)
878 (if (or (memq :bold spec)
879 (memq :italic spec)
880 (memq :inverse-video spec))
881 (let (result)
882 (while spec
883 (let ((key (car spec))
884 (val (car (cdr spec))))
885 (cond ((eq key :italic)
886 (push :slant result)
887 (push (if val 'italic 'normal) result))
888 ((eq key :bold)
889 (push :weight result)
890 (push (if val 'bold 'normal) result))
891 ((eq key :reverse-video)
892 (push :inverse-video result)
893 (push val result))
894 (t
895 (push key result)
896 (push val result))))
897 (setq spec (cddr spec)))
898 (nreverse result))
899 spec)))
900 \f
901 (defun custom-set-variables (&rest args)
902 "Install user customizations of variable values specified in ARGS.
903 These settings are registered as theme `user'.
904 The arguments should each be a list of the form:
905
906 (SYMBOL EXP [NOW [REQUEST [COMMENT]]])
907
908 This stores EXP (without evaluating it) as the saved value for SYMBOL.
909 If NOW is present and non-nil, then also evaluate EXP and set
910 the default value for the SYMBOL to the value of EXP.
911
912 REQUEST is a list of features we must require in order to
913 handle SYMBOL properly.
914 COMMENT is a comment string about SYMBOL."
915 (apply 'custom-theme-set-variables 'user args))
916
917 (defun custom-theme-set-variables (theme &rest args)
918 "Initialize variables for theme THEME according to settings in ARGS.
919 Each of the arguments in ARGS should be a list of this form:
920
921 (SYMBOL EXP [NOW [REQUEST [COMMENT]]])
922
923 This stores EXP (without evaluating it) as the saved value for SYMBOL.
924 If NOW is present and non-nil, then also evaluate EXP and set
925 the default value for the SYMBOL to the value of EXP.
926
927 REQUEST is a list of features we must require in order to
928 handle SYMBOL properly.
929 COMMENT is a comment string about SYMBOL.
930
931 EXP itself is saved unevaluated as SYMBOL property `saved-value' and
932 in SYMBOL's list property `theme-value' \(using `custom-push-theme')."
933 (custom-check-theme theme)
934
935 ;; Process all the needed autoloads before anything else, so that the
936 ;; subsequent code has all the info it needs (e.g. which var corresponds
937 ;; to a minor mode), regardless of the ordering of the variables.
938 (dolist (entry args)
939 (let* ((symbol (indirect-variable (nth 0 entry))))
940 (unless (or (get symbol 'standard-value)
941 (memq (get symbol 'custom-autoload) '(nil noset)))
942 ;; This symbol needs to be autoloaded, even just for a `set'.
943 (custom-load-symbol symbol))))
944
945 ;; Move minor modes and variables with explicit requires to the end.
946 (setq args
947 (sort args
948 (lambda (a1 a2)
949 (let* ((sym1 (car a1))
950 (sym2 (car a2))
951 (1-then-2 (memq sym1 (get sym2 'custom-dependencies)))
952 (2-then-1 (memq sym2 (get sym1 'custom-dependencies))))
953 (cond ((and 1-then-2 2-then-1)
954 (error "Circular custom dependency between `%s' and `%s'"
955 sym1 sym2))
956 (2-then-1 nil)
957 ;; 1 is a dependency of 2, so needs to be set first.
958 (1-then-2)
959 ;; Put minor modes and symbols with :require last.
960 ;; Putting minor modes last ensures that the mode
961 ;; function will see other customized values rather
962 ;; than default values.
963 (t (or (nth 3 a2)
964 (eq (get sym2 'custom-set)
965 'custom-set-minor-mode))))))))
966
967 (dolist (entry args)
968 (unless (listp entry)
969 (error "Incompatible Custom theme spec"))
970 (let* ((symbol (indirect-variable (nth 0 entry)))
971 (value (nth 1 entry)))
972 (custom-push-theme 'theme-value symbol theme 'set value)
973 (unless custom--inhibit-theme-enable
974 ;; Now set the variable.
975 (let* ((now (nth 2 entry))
976 (requests (nth 3 entry))
977 (comment (nth 4 entry))
978 set)
979 (when requests
980 (put symbol 'custom-requests requests)
981 (mapc 'require requests))
982 (setq set (or (get symbol 'custom-set) 'custom-set-default))
983 (put symbol 'saved-value (list value))
984 (put symbol 'saved-variable-comment comment)
985 ;; Allow for errors in the case where the setter has
986 ;; changed between versions, say, but let the user know.
987 (condition-case data
988 (cond (now
989 ;; Rogue variable, set it now.
990 (put symbol 'force-value t)
991 (funcall set symbol (eval value)))
992 ((default-boundp symbol)
993 ;; Something already set this, overwrite it.
994 (funcall set symbol (eval value))))
995 (error
996 (message "Error setting %s: %s" symbol data)))
997 (and (or now (default-boundp symbol))
998 (put symbol 'variable-comment comment)))))))
999
1000 \f
1001 ;;; Defining themes.
1002
1003 ;; A theme file is named `THEME-theme.el' (where THEME is the theme
1004 ;; name) found in `custom-theme-load-path'. It has this format:
1005 ;;
1006 ;; (deftheme THEME
1007 ;; DOCSTRING)
1008 ;;
1009 ;; (custom-theme-set-variables
1010 ;; 'THEME
1011 ;; [THEME-VARIABLES])
1012 ;;
1013 ;; (custom-theme-set-faces
1014 ;; 'THEME
1015 ;; [THEME-FACES])
1016 ;;
1017 ;; (provide-theme 'THEME)
1018
1019
1020 ;; The IGNORED arguments to deftheme come from the XEmacs theme code, where
1021 ;; they were used to supply keyword-value pairs like `:immediate',
1022 ;; `:variable-reset-string', etc. We don't use any of these, so ignore them.
1023
1024 (defmacro deftheme (theme &optional doc &rest ignored)
1025 "Declare THEME to be a Custom theme.
1026 The optional argument DOC is a doc string describing the theme.
1027
1028 Any theme `foo' should be defined in a file called `foo-theme.el';
1029 see `custom-make-theme-feature' for more information."
1030 (let ((feature (custom-make-theme-feature theme)))
1031 ;; It is better not to use backquote in this file,
1032 ;; because that makes a bootstrapping problem
1033 ;; if you need to recompile all the Lisp files using interpreted code.
1034 (list 'custom-declare-theme (list 'quote theme) (list 'quote feature) doc)))
1035
1036 (defun custom-declare-theme (theme feature &optional doc &rest ignored)
1037 "Like `deftheme', but THEME is evaluated as a normal argument.
1038 FEATURE is the feature this theme provides. Normally, this is a symbol
1039 created from THEME by `custom-make-theme-feature'."
1040 (unless (custom-theme-name-valid-p theme)
1041 (error "Custom theme cannot be named %S" theme))
1042 (add-to-list 'custom-known-themes theme)
1043 (put theme 'theme-feature feature)
1044 (when doc (put theme 'theme-documentation doc)))
1045
1046 (defun custom-make-theme-feature (theme)
1047 "Given a symbol THEME, create a new symbol by appending \"-theme\".
1048 Store this symbol in the `theme-feature' property of THEME.
1049 Calling `provide-theme' to provide THEME actually puts `THEME-theme'
1050 into `features'.
1051
1052 This allows for a file-name convention for autoloading themes:
1053 Every theme X has a property `provide-theme' whose value is \"X-theme\".
1054 \(load-theme X) then attempts to load the file `X-theme.el'."
1055 (intern (concat (symbol-name theme) "-theme")))
1056 \f
1057 ;;; Loading themes.
1058
1059 (defcustom custom-theme-directory user-emacs-directory
1060 "Default user directory for storing custom theme files.
1061 The command `customize-create-theme' writes theme files into this
1062 directory. By default, Emacs searches for custom themes in this
1063 directory first---see `custom-theme-load-path'."
1064 :type 'string
1065 :group 'customize
1066 :version "22.1")
1067
1068 (defcustom custom-theme-load-path (list 'custom-theme-directory t)
1069 "List of directories to search for custom theme files.
1070 When loading custom themes (e.g. in `customize-themes' and
1071 `load-theme'), Emacs searches for theme files in the specified
1072 order. Each element in the list should be one of the following:
1073 - the symbol `custom-theme-directory', meaning the value of
1074 `custom-theme-directory'.
1075 - the symbol t, meaning the built-in theme directory (a directory
1076 named \"themes\" in `data-directory').
1077 - a directory name (a string).
1078
1079 Each theme file is named THEME-theme.el, where THEME is the theme
1080 name."
1081 :type '(repeat (choice (const :tag "custom-theme-directory"
1082 custom-theme-directory)
1083 (const :tag "Built-in theme directory" t)
1084 directory))
1085 :group 'customize
1086 :version "24.1")
1087
1088 (defvar custom--inhibit-theme-enable nil
1089 "Whether the custom-theme-set-* functions act immediately.
1090 If nil, `custom-theme-set-variables' and `custom-theme-set-faces'
1091 change the current values of the given variable or face. If
1092 non-nil, they just make a record of the theme settings.")
1093
1094 (defun provide-theme (theme)
1095 "Indicate that this file provides THEME.
1096 This calls `provide' to provide the feature name stored in THEME's
1097 property `theme-feature' (which is usually a symbol created by
1098 `custom-make-theme-feature')."
1099 (unless (custom-theme-name-valid-p theme)
1100 (error "Custom theme cannot be named %S" theme))
1101 (custom-check-theme theme)
1102 (provide (get theme 'theme-feature)))
1103
1104 (defcustom custom-safe-themes '(default)
1105 "List of themes that are considered safe to load.
1106 Each list element should be the `sha1' hash of a theme file, or
1107 the symbol `default', which stands for any theme in the built-in
1108 Emacs theme directory (a directory named \"themes\" in
1109 `data-directory')."
1110 :type '(repeat
1111 (choice string (const :tag "Built-in themes" default)))
1112 :group 'customize
1113 :risky t
1114 :version "24.1")
1115
1116 (defun load-theme (theme &optional no-confirm no-enable)
1117 "Load Custom theme named THEME from its file.
1118 The theme file is named THEME-theme.el, in one of the directories
1119 specified by `custom-theme-load-path'.
1120
1121 If THEME is not in `custom-safe-themes', prompt the user for
1122 confirmation, unless optional arg NO-CONFIRM is non-nil.
1123
1124 Normally, this function also enables THEME; if optional arg
1125 NO-ENABLE is non-nil, load the theme but don't enable it.
1126
1127 This function is normally called through Customize when setting
1128 `custom-enabled-themes'. If used directly in your init file, it
1129 should be called with a non-nil NO-CONFIRM argument, or after
1130 `custom-safe-themes' has been loaded.
1131
1132 Return t if THEME was successfully loaded, nil otherwise."
1133 (interactive
1134 (list
1135 (intern (completing-read "Load custom theme: "
1136 (mapcar 'symbol-name
1137 (custom-available-themes))))
1138 nil nil))
1139 (unless (custom-theme-name-valid-p theme)
1140 (error "Invalid theme name `%s'" theme))
1141 ;; If reloading, clear out the old theme settings.
1142 (when (custom-theme-p theme)
1143 (disable-theme theme)
1144 (put theme 'theme-settings nil)
1145 (put theme 'theme-feature nil)
1146 (put theme 'theme-documentation nil))
1147 (let ((fn (locate-file (concat (symbol-name theme) "-theme.el")
1148 (custom-theme--load-path)
1149 '("" "c")))
1150 hash)
1151 (unless fn
1152 (error "Unable to find theme file for `%s'" theme))
1153 (with-temp-buffer
1154 (insert-file-contents fn)
1155 (setq hash (sha1 (current-buffer)))
1156 ;; Check file safety with `custom-safe-themes', prompting the
1157 ;; user if necessary.
1158 (when (or no-confirm
1159 (and (memq 'default custom-safe-themes)
1160 (equal (file-name-directory fn)
1161 (expand-file-name "themes/" data-directory)))
1162 (member hash custom-safe-themes)
1163 (custom-theme-load-confirm hash))
1164 (let ((custom--inhibit-theme-enable t))
1165 (eval-buffer))
1166 ;; Optimization: if the theme changes the `default' face, put that
1167 ;; entry first. This avoids some `frame-set-background-mode' rigmarole
1168 ;; by assigning the new background immediately.
1169 (let* ((settings (get theme 'theme-settings))
1170 (tail settings)
1171 found)
1172 (while (and tail (not found))
1173 (and (eq (nth 0 (car tail)) 'theme-face)
1174 (eq (nth 1 (car tail)) 'default)
1175 (setq found (car tail)))
1176 (setq tail (cdr tail)))
1177 (if found
1178 (put theme 'theme-settings (cons found (delq found settings)))))
1179 ;; Finally, enable the theme.
1180 (unless no-enable
1181 (enable-theme theme))
1182 t))))
1183
1184 (defun custom-theme-load-confirm (hash)
1185 "Query the user about loading a Custom theme that may not be safe.
1186 The theme should be in the current buffer. If the user agrees,
1187 query also about adding HASH to `custom-safe-themes'."
1188 (if noninteractive
1189 nil
1190 (let ((exit-chars '(?y ?n ?\s))
1191 window prompt char)
1192 (save-window-excursion
1193 (rename-buffer "*Custom Theme*" t)
1194 (emacs-lisp-mode)
1195 (setq window (display-buffer (current-buffer)))
1196 (setq prompt
1197 (format "Loading a theme can run Lisp code. Really load?%s"
1198 (if (and window
1199 (< (line-number-at-pos (point-max))
1200 (window-body-height)))
1201 " (y or n) "
1202 (push ?\C-v exit-chars)
1203 "\nType y or n, or C-v to scroll: ")))
1204 (goto-char (point-min))
1205 (while (null char)
1206 (setq char (read-char-choice prompt exit-chars))
1207 (when (eq char ?\C-v)
1208 (if window
1209 (with-selected-window window
1210 (condition-case nil
1211 (scroll-up)
1212 (error (goto-char (point-min))))))
1213 (setq char nil)))
1214 (when (memq char '(?\s ?y))
1215 ;; Offer to save to `custom-safe-themes'.
1216 (and (or custom-file user-init-file)
1217 (y-or-n-p "Treat this theme as safe in future sessions? ")
1218 (customize-push-and-save 'custom-safe-themes (list hash)))
1219 t)))))
1220
1221 (defun custom-theme-name-valid-p (name)
1222 "Return t if NAME is a valid name for a Custom theme, nil otherwise.
1223 NAME should be a symbol."
1224 (and (symbolp name)
1225 name
1226 (not (or (zerop (length (symbol-name name)))
1227 (eq name 'user)
1228 (eq name 'changed)))))
1229
1230 (defun custom-available-themes ()
1231 "Return a list of available Custom themes (symbols)."
1232 (let (sym themes)
1233 (dolist (dir (custom-theme--load-path))
1234 (when (file-directory-p dir)
1235 (dolist (file (file-expand-wildcards
1236 (expand-file-name "*-theme.el" dir) t))
1237 (setq file (file-name-nondirectory file))
1238 (and (string-match "\\`\\(.+\\)-theme.el\\'" file)
1239 (setq sym (intern (match-string 1 file)))
1240 (custom-theme-name-valid-p sym)
1241 (push sym themes)))))
1242 (nreverse (delete-dups themes))))
1243
1244 (defun custom-theme--load-path ()
1245 (let (lpath)
1246 (dolist (f custom-theme-load-path)
1247 (cond ((eq f 'custom-theme-directory)
1248 (setq f custom-theme-directory))
1249 ((eq f t)
1250 (setq f (expand-file-name "themes" data-directory))))
1251 (if (file-directory-p f)
1252 (push f lpath)))
1253 (nreverse lpath)))
1254
1255 \f
1256 ;;; Enabling and disabling loaded themes.
1257
1258 (defun enable-theme (theme)
1259 "Reenable all variable and face settings defined by THEME.
1260 THEME should be either `user', or a theme loaded via `load-theme'.
1261 After this function completes, THEME will have the highest
1262 precedence (after `user')."
1263 (interactive (list (intern
1264 (completing-read
1265 "Enable custom theme: "
1266 obarray (lambda (sym) (get sym 'theme-settings)) t))))
1267 (if (not (custom-theme-p theme))
1268 (error "Undefined Custom theme %s" theme))
1269 (let ((settings (get theme 'theme-settings)))
1270 ;; Loop through theme settings, recalculating vars/faces.
1271 (dolist (s settings)
1272 (let* ((prop (car s))
1273 (symbol (cadr s))
1274 (spec-list (get symbol prop)))
1275 (put symbol prop (cons (cddr s) (assq-delete-all theme spec-list)))
1276 (cond
1277 ((eq prop 'theme-face)
1278 (custom-theme-recalc-face symbol))
1279 ((eq prop 'theme-value)
1280 ;; Don't change `custom-enabled-themes'; that's special.
1281 (unless (eq symbol 'custom-enabled-themes)
1282 (custom-theme-recalc-variable symbol)))))))
1283 (unless (eq theme 'user)
1284 (setq custom-enabled-themes
1285 (cons theme (delq theme custom-enabled-themes)))
1286 ;; Give the `user' theme the highest priority.
1287 (enable-theme 'user)))
1288
1289 (defcustom custom-enabled-themes nil
1290 "List of enabled Custom Themes, highest precedence first.
1291 This list does not include the `user' theme, which is set by
1292 Customize and always takes precedence over other Custom Themes.
1293
1294 This variable cannot be defined inside a Custom theme; there, it
1295 is simply ignored.
1296
1297 Setting this variable through Customize calls `enable-theme' or
1298 `load-theme' for each theme in the list."
1299 :group 'customize
1300 :type '(repeat symbol)
1301 :set-after '(custom-theme-directory custom-theme-load-path
1302 custom-safe-themes)
1303 :risky t
1304 :set (lambda (symbol themes)
1305 (let (failures)
1306 (setq themes (delq 'user (delete-dups themes)))
1307 ;; Disable all themes not in THEMES.
1308 (if (boundp symbol)
1309 (dolist (theme (symbol-value symbol))
1310 (if (not (memq theme themes))
1311 (disable-theme theme))))
1312 ;; Call `enable-theme' or `load-theme' on each of THEMES.
1313 (dolist (theme (reverse themes))
1314 (condition-case nil
1315 (if (custom-theme-p theme)
1316 (enable-theme theme)
1317 (load-theme theme))
1318 (error (setq failures (cons theme failures)
1319 themes (delq theme themes)))))
1320 (enable-theme 'user)
1321 (custom-set-default symbol themes)
1322 (if failures
1323 (message "Failed to enable theme: %s"
1324 (mapconcat 'symbol-name failures ", "))))))
1325
1326 (defsubst custom-theme-enabled-p (theme)
1327 "Return non-nil if THEME is enabled."
1328 (memq theme custom-enabled-themes))
1329
1330 (defun disable-theme (theme)
1331 "Disable all variable and face settings defined by THEME.
1332 See `custom-enabled-themes' for a list of enabled themes."
1333 (interactive (list (intern
1334 (completing-read
1335 "Disable custom theme: "
1336 (mapcar 'symbol-name custom-enabled-themes)
1337 nil t))))
1338 (when (custom-theme-enabled-p theme)
1339 (let ((settings (get theme 'theme-settings)))
1340 (dolist (s settings)
1341 (let* ((prop (car s))
1342 (symbol (cadr s))
1343 (val (assq-delete-all theme (get symbol prop))))
1344 (put symbol prop val)
1345 (cond
1346 ((eq prop 'theme-value)
1347 (custom-theme-recalc-variable symbol))
1348 ((eq prop 'theme-face)
1349 ;; If the face spec specified by this theme is in the
1350 ;; saved-face property, reset that property.
1351 (when (equal (nth 3 s) (get symbol 'saved-face))
1352 (put symbol 'saved-face (and val (cadr (car val)))))))))
1353 ;; Recompute faces on all frames.
1354 (dolist (frame (frame-list))
1355 ;; We must reset the fg and bg color frame parameters, or
1356 ;; `face-set-after-frame-default' will use the existing
1357 ;; parameters, which could be from the disabled theme.
1358 (set-frame-parameter frame 'background-color
1359 (custom--frame-color-default
1360 frame :background "background" "Background"
1361 "unspecified-bg" "white"))
1362 (set-frame-parameter frame 'foreground-color
1363 (custom--frame-color-default
1364 frame :foreground "foreground" "Foreground"
1365 "unspecified-fg" "black"))
1366 (face-set-after-frame-default frame))
1367 (setq custom-enabled-themes
1368 (delq theme custom-enabled-themes)))))
1369
1370 (defun custom--frame-color-default (frame attribute resource-attr resource-class
1371 tty-default x-default)
1372 (let ((col (face-attribute 'default attribute t)))
1373 (cond
1374 ((and col (not (eq col 'unspecified))) col)
1375 ((null (window-system frame)) tty-default)
1376 ((setq col (x-get-resource resource-attr resource-class)) col)
1377 (t x-default))))
1378
1379 (defun custom-variable-theme-value (variable)
1380 "Return (list VALUE) indicating the custom theme value of VARIABLE.
1381 That is to say, it specifies what the value should be according to
1382 currently enabled custom themes.
1383
1384 This function returns nil if no custom theme specifies a value for VARIABLE."
1385 (let ((theme-value (get variable 'theme-value)))
1386 (if theme-value
1387 (cdr (car theme-value)))))
1388
1389 (defun custom-theme-recalc-variable (variable)
1390 "Set VARIABLE according to currently enabled custom themes."
1391 (let ((valspec (custom-variable-theme-value variable)))
1392 (if valspec
1393 (put variable 'saved-value valspec)
1394 (setq valspec (get variable 'standard-value)))
1395 (if (and valspec
1396 (or (get variable 'force-value)
1397 (default-boundp variable)))
1398 (funcall (or (get variable 'custom-set) 'set-default) variable
1399 (eval (car valspec))))))
1400
1401 (defun custom-theme-recalc-face (face)
1402 "Set FACE according to currently enabled custom themes."
1403 (if (get face 'face-alias)
1404 (setq face (get face 'face-alias)))
1405 ;; Reset the faces for each frame.
1406 (dolist (frame (frame-list))
1407 (face-spec-recalc face frame)))
1408
1409 \f
1410 ;;; XEmacs compatibility functions
1411
1412 ;; In XEmacs, when you reset a Custom Theme, you have to specify the
1413 ;; theme to reset it to. We just apply the next available theme, so
1414 ;; just ignore the IGNORED arguments.
1415
1416 (defun custom-theme-reset-variables (theme &rest args)
1417 "Reset some variable settings in THEME to their values in other themes.
1418 Each of the arguments ARGS has this form:
1419
1420 (VARIABLE IGNORED)
1421
1422 This means reset VARIABLE. (The argument IGNORED is ignored)."
1423 (custom-check-theme theme)
1424 (dolist (arg args)
1425 (custom-push-theme 'theme-value (car arg) theme 'reset)))
1426
1427 (defun custom-reset-variables (&rest args)
1428 "Reset the specs of some variables to their values in other themes.
1429 This creates settings in the `user' theme.
1430
1431 Each of the arguments ARGS has this form:
1432
1433 (VARIABLE IGNORED)
1434
1435 This means reset VARIABLE. (The argument IGNORED is ignored)."
1436 (apply 'custom-theme-reset-variables 'user args))
1437
1438 ;;; The End.
1439
1440 ;; Process the defcustoms for variables loaded before this file.
1441 (while custom-declare-variable-list
1442 (apply 'custom-declare-variable (car custom-declare-variable-list))
1443 (setq custom-declare-variable-list (cdr custom-declare-variable-list)))
1444
1445 (provide 'custom)
1446
1447 ;;; custom.el ends here