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