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