*** empty log message ***
[emacs.git] / lisp / custom.el
blobb2a9ba6443ce8dff77663e7960c491e3e1908452
1 ;;; custom.el --- tools for declaring and initializing options
2 ;;
3 ;; Copyright (C) 1996, 1997, 1999, 2001, 2002, 2003, 2004,
4 ;; 2005 Free Software Foundation, Inc.
5 ;;
6 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
7 ;; Maintainer: FSF
8 ;; Keywords: help, faces
10 ;; This file is part of GNU Emacs.
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 2, or (at your option)
15 ;; any later version.
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.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
27 ;;; Commentary:
29 ;; This file only contains the code needed to declare and initialize
30 ;; user options. The code to customize options is autoloaded from
31 ;; `cus-edit.el' and is documented in the Emacs Lisp Reference manual.
33 ;; The code implementing face declarations is in `cus-face.el'.
35 ;;; Code:
37 (require 'widget)
39 (defvar custom-define-hook nil
40 ;; Customize information for this option is in `cus-edit.el'.
41 "Hook called after defining each customize option.")
43 (defvar custom-dont-initialize nil
44 "Non-nil means `defcustom' should not initialize the variable.
45 That is used for the sake of `custom-make-dependencies'.
46 Users should not set it.")
48 (defvar custom-current-group-alist nil
49 "Alist of (FILE . GROUP) indicating the current group to use for FILE.")
51 ;;; The `defcustom' Macro.
53 (defun custom-initialize-default (symbol value)
54 "Initialize SYMBOL with VALUE.
55 This will do nothing if symbol already has a default binding.
56 Otherwise, if symbol has a `saved-value' property, it will evaluate
57 the car of that and use it as the default binding for symbol.
58 Otherwise, VALUE will be evaluated and used as the default binding for
59 symbol."
60 (unless (default-boundp symbol)
61 ;; Use the saved value if it exists, otherwise the standard setting.
62 (set-default symbol (if (get symbol 'saved-value)
63 (eval (car (get symbol 'saved-value)))
64 (eval value)))))
66 (defun custom-initialize-set (symbol value)
67 "Initialize SYMBOL based on VALUE.
68 If the symbol doesn't have a default binding already,
69 then set it using its `:set' function (or `set-default' if it has none).
70 The value is either the value in the symbol's `saved-value' property,
71 if any, or VALUE."
72 (unless (default-boundp symbol)
73 (funcall (or (get symbol 'custom-set) 'set-default)
74 symbol
75 (if (get symbol 'saved-value)
76 (eval (car (get symbol 'saved-value)))
77 (eval value)))))
79 (defun custom-initialize-safe-set (symbol value)
80 "Like `custom-initialize-set', but catches errors.
81 If an error occurs during initialization, SYMBOL is set to nil
82 and no error is thrown. This is meant for use in pre-loaded files
83 where some variables or functions used to compute VALUE may not yet
84 be defined. You can then re-evaluate VALUE in startup.el, for instance
85 using `custom-reevaluate-setting'."
86 (condition-case nil
87 (custom-initialize-set symbol value)
88 (error (set-default symbol nil))))
90 (defun custom-initialize-safe-default (symbol value)
91 "Like `custom-initialize-default', but catches errors.
92 If an error occurs during initialization, SYMBOL is set to nil
93 and no error is thrown. This is meant for use in pre-loaded files
94 where some variables or functions used to compute VALUE may not yet
95 be defined. You can then re-evaluate VALUE in startup.el, for instance
96 using `custom-reevaluate-setting'."
97 (condition-case nil
98 (custom-initialize-default symbol value)
99 (error (set-default symbol nil))))
101 (defun custom-initialize-reset (symbol value)
102 "Initialize SYMBOL based on VALUE.
103 Set the symbol, using its `:set' function (or `set-default' if it has none).
104 The value is either the symbol's current value
105 \(as obtained using the `:get' function), if any,
106 or the value in the symbol's `saved-value' property if any,
107 or (last of all) VALUE."
108 (funcall (or (get symbol 'custom-set) 'set-default)
109 symbol
110 (cond ((default-boundp symbol)
111 (funcall (or (get symbol 'custom-get) 'default-value)
112 symbol))
113 ((get symbol 'saved-value)
114 (eval (car (get symbol 'saved-value))))
116 (eval value)))))
118 (defun custom-initialize-changed (symbol value)
119 "Initialize SYMBOL with VALUE.
120 Like `custom-initialize-reset', but only use the `:set' function if
121 not using the standard setting.
122 For the standard setting, use `set-default'."
123 (cond ((default-boundp symbol)
124 (funcall (or (get symbol 'custom-set) 'set-default)
125 symbol
126 (funcall (or (get symbol 'custom-get) 'default-value)
127 symbol)))
128 ((get symbol 'saved-value)
129 (funcall (or (get symbol 'custom-set) 'set-default)
130 symbol
131 (eval (car (get symbol 'saved-value)))))
133 (set-default symbol (eval value)))))
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.
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 (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 (when doc
148 (put symbol 'variable-documentation doc))
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 :type)
172 (put symbol 'custom-type (purecopy value)))
173 ((eq keyword :options)
174 (if (get symbol 'custom-options)
175 ;; Slow safe code to avoid duplicates.
176 (mapc (lambda (option)
177 (custom-add-option symbol option))
178 value)
179 ;; Fast code for the common case.
180 (put symbol 'custom-options (copy-sequence value))))
182 (custom-handle-keyword symbol keyword value
183 'custom-variable))))))
184 (put symbol 'custom-requests requests)
185 ;; Do the actual initialization.
186 (unless custom-dont-initialize
187 (funcall initialize symbol default)))
188 (push symbol current-load-list)
189 (run-hooks 'custom-define-hook)
190 symbol)
192 (defmacro defcustom (symbol value doc &rest args)
193 "Declare SYMBOL as a customizable variable that defaults to VALUE.
194 DOC is the variable documentation.
196 Neither SYMBOL nor VALUE need to be quoted.
197 If SYMBOL is not already bound, initialize it to VALUE.
198 The remaining arguments should have the form
200 [KEYWORD VALUE]...
202 The following keywords are meaningful:
204 :type VALUE should be a widget type for editing the symbol's value.
205 :options VALUE should be a list of valid members of the widget type.
206 :group VALUE should be a customization group.
207 Add SYMBOL to that group.
208 :link LINK-DATA
209 Include an external link after the documentation string for this
210 item. This is a sentence containing an active field which
211 references some other documentation.
213 There are several alternatives you can use for LINK-DATA:
215 (custom-manual INFO-NODE)
216 Link to an Info node; INFO-NODE is a string which specifies
217 the node name, as in \"(emacs)Top\".
219 (info-link INFO-NODE)
220 Like `custom-manual' except that the link appears in the
221 customization buffer with the Info node name.
223 (url-link URL)
224 Link to a web page; URL is a string which specifies the URL.
226 (emacs-commentary-link LIBRARY)
227 Link to the commentary section of LIBRARY.
229 (emacs-library-link LIBRARY)
230 Link to an Emacs Lisp LIBRARY file.
232 (file-link FILE)
233 Link to FILE.
235 (function-link FUNCTION)
236 Link to the documentation of FUNCTION.
238 (variable-link VARIABLE)
239 Link to the documentation of VARIABLE.
241 (custom-group-link GROUP)
242 Link to another customization GROUP.
244 You can specify the text to use in the customization buffer by
245 adding `:tag NAME' after the first element of the LINK-DATA; for
246 example, (info-link :tag \"foo\" \"(emacs)Top\") makes a link to the
247 Emacs manual which appears in the buffer as `foo'.
249 An item can have more than one external link; however, most items
250 have none at all.
251 :initialize
252 VALUE should be a function used to initialize the
253 variable. It takes two arguments, the symbol and value
254 given in the `defcustom' call. The default is
255 `custom-initialize-reset'.
256 :set VALUE should be a function to set the value of the symbol.
257 It takes two arguments, the symbol to set and the value to
258 give it. The default choice of function is `custom-set-default'.
259 :get VALUE should be a function to extract the value of symbol.
260 The function takes one argument, a symbol, and should return
261 the current value for that symbol. The default choice of function
262 is `custom-default-value'.
263 :require
264 VALUE should be a feature symbol. If you save a value
265 for this option, then when your `.emacs' file loads the value,
266 it does (require VALUE) first.
267 :version
268 VALUE should be a string specifying that the variable was
269 first introduced, or its default value was changed, in Emacs
270 version VERSION.
271 :tag LABEL
272 Use LABEL, a string, instead of the item's name, to label the item
273 in customization menus and buffers.
274 :load FILE
275 Load file FILE (a string) before displaying this customization
276 item. Loading is done with `load', and only if the file is
277 not already loaded.
278 :set-after VARIABLES
279 Specifies that SYMBOL should be set after the list of variables
280 VARIABLES when both have been customized.
282 If SYMBOL has a local binding, then this form affects the local
283 binding. This is normally not what you want. Thus, if you need
284 to load a file defining variables with this form, or with
285 `defvar' or `defconst', you should always load that file
286 _outside_ any bindings for these variables. \(`defvar' and
287 `defconst' behave similarly in this respect.)
289 Read the section about customization in the Emacs Lisp manual for more
290 information."
291 (declare (doc-string 3))
292 ;; It is better not to use backquote in this file,
293 ;; because that makes a bootstrapping problem
294 ;; if you need to recompile all the Lisp files using interpreted code.
295 (nconc (list 'custom-declare-variable
296 (list 'quote symbol)
297 (list 'quote value)
298 doc)
299 args))
301 ;;; The `defface' Macro.
303 (defmacro defface (face spec doc &rest args)
304 "Declare FACE as a customizable face that defaults to SPEC.
305 FACE does not need to be quoted.
307 Third argument DOC is the face documentation.
309 If FACE has been set with `custom-set-faces', set the face attributes
310 as specified by that function, otherwise set the face attributes
311 according to SPEC.
313 The remaining arguments should have the form
315 [KEYWORD VALUE]...
317 The following KEYWORDs are defined:
319 :group VALUE should be a customization group.
320 Add FACE to that group.
322 SPEC should be an alist of the form ((DISPLAY ATTS)...).
324 In the first element, DISPLAY can be :default. The ATTS in that
325 element then act as defaults for all the following elements.
327 Aside from that, DISPLAY specifies conditions to match some or
328 all frames. For each frame, the first element of SPEC where the
329 DISPLAY conditions are satisfied is the one that applies to that
330 frame. The ATTRs in this element take effect, and the following
331 elements are ignored, on that frame.
333 In the last element, DISPLAY can be t. That element applies to a
334 frame if none of the previous elements (except the :default if
335 any) did.
337 ATTS is a list of face attributes followed by their values:
338 (ATTR VALUE ATTR VALUE...)
340 The possible attributes are `:family', `:width', `:height', `:weight',
341 `:slant', `:underline', `:overline', `:strike-through', `:box',
342 `:foreground', `:background', `:stipple', `:inverse-video', and `:inherit'.
344 DISPLAY can be `:default' (only in the first element), the symbol
345 t (only in the last element) to match all frames, or an alist of
346 conditions of the form \(REQ ITEM...). For such an alist to
347 match a frame, each of the conditions must be satisfied, meaning
348 that the REQ property of the frame must match one of the
349 corresponding ITEMs. These are the defined REQ values:
351 `type' (the value of `window-system')
352 Under X, in addition to the values `window-system' can take,
353 `motif', `lucid', `gtk' and `x-toolkit' are allowed, and match when
354 the Motif toolkit, Lucid toolkit, GTK toolkit or any X toolkit is in use.
356 `class' (the frame's color support)
357 Should be one of `color', `grayscale', or `mono'.
359 `background' (what color is used for the background text)
360 Should be one of `light' or `dark'.
362 `min-colors' (the minimum number of colors the frame should support)
363 Should be an integer, it is compared with the result of
364 `display-color-cells'.
366 `supports' (only match frames that support the specified face attributes)
367 Should be a list of face attributes. See the documentation for
368 the function `display-supports-face-attributes-p' for more
369 information on exactly how testing is done.
371 Read the section about customization in the Emacs Lisp manual for more
372 information."
373 (declare (doc-string 3))
374 ;; It is better not to use backquote in this file,
375 ;; because that makes a bootstrapping problem
376 ;; if you need to recompile all the Lisp files using interpreted code.
377 (nconc (list 'custom-declare-face (list 'quote face) spec doc) args))
379 ;;; The `defgroup' Macro.
381 (defun custom-current-group ()
382 (cdr (assoc load-file-name custom-current-group-alist)))
384 (defun custom-declare-group (symbol members doc &rest args)
385 "Like `defgroup', but SYMBOL is evaluated as a normal argument."
386 (while members
387 (apply 'custom-add-to-group symbol (car members))
388 (setq members (cdr members)))
389 (when doc
390 ;; This text doesn't get into DOC.
391 (put symbol 'group-documentation (purecopy doc)))
392 (while args
393 (let ((arg (car args)))
394 (setq args (cdr args))
395 (unless (symbolp arg)
396 (error "Junk in args %S" args))
397 (let ((keyword arg)
398 (value (car args)))
399 (unless args
400 (error "Keyword %s is missing an argument" keyword))
401 (setq args (cdr args))
402 (cond ((eq keyword :prefix)
403 (put symbol 'custom-prefix value))
405 (custom-handle-keyword symbol keyword value
406 'custom-group))))))
407 ;; Record the group on the `current' list.
408 (let ((elt (assoc load-file-name custom-current-group-alist)))
409 (if elt (setcdr elt symbol)
410 (push (cons load-file-name symbol) custom-current-group-alist)))
411 (run-hooks 'custom-define-hook)
412 symbol)
414 (defmacro defgroup (symbol members doc &rest args)
415 "Declare SYMBOL as a customization group containing MEMBERS.
416 SYMBOL does not need to be quoted.
418 Third arg DOC is the group documentation.
420 MEMBERS should be an alist of the form ((NAME WIDGET)...) where
421 NAME is a symbol and WIDGET is a widget for editing that symbol.
422 Useful widgets are `custom-variable' for editing variables,
423 `custom-face' for edit faces, and `custom-group' for editing groups.
425 The remaining arguments should have the form
427 [KEYWORD VALUE]...
429 The following KEYWORDs are defined:
431 :group VALUE should be a customization group.
432 Add SYMBOL to that group.
434 :version VALUE should be a string specifying that the group was introduced
435 in Emacs version VERSION.
437 Read the section about customization in the Emacs Lisp manual for more
438 information."
439 (declare (doc-string 3))
440 ;; It is better not to use backquote in this file,
441 ;; because that makes a bootstrapping problem
442 ;; if you need to recompile all the Lisp files using interpreted code.
443 (nconc (list 'custom-declare-group (list 'quote symbol) members doc) args))
445 (defun custom-add-to-group (group option widget)
446 "To existing GROUP add a new OPTION of type WIDGET.
447 If there already is an entry for OPTION and WIDGET, nothing is done."
448 (let ((members (get group 'custom-group))
449 (entry (list option widget)))
450 (unless (member entry members)
451 (put group 'custom-group (nconc members (list entry))))))
453 (defun custom-group-of-mode (mode)
454 "Return the custom group corresponding to the major or minor MODE.
455 If no such group is found, return nil."
456 (or (get mode 'custom-mode-group)
457 (if (or (get mode 'custom-group)
458 (and (string-match "-mode\\'" (symbol-name mode))
459 (get (setq mode (intern (substring (symbol-name mode)
460 0 (match-beginning 0))))
461 'custom-group)))
462 mode)))
464 ;;; Properties.
466 (defun custom-handle-all-keywords (symbol args type)
467 "For customization option SYMBOL, handle keyword arguments ARGS.
468 Third argument TYPE is the custom option type."
469 (unless (memq :group args)
470 (custom-add-to-group (custom-current-group) symbol type))
471 (while args
472 (let ((arg (car args)))
473 (setq args (cdr args))
474 (unless (symbolp arg)
475 (error "Junk in args %S" args))
476 (let ((keyword arg)
477 (value (car args)))
478 (unless args
479 (error "Keyword %s is missing an argument" keyword))
480 (setq args (cdr args))
481 (custom-handle-keyword symbol keyword value type)))))
483 (defun custom-handle-keyword (symbol keyword value type)
484 "For customization option SYMBOL, handle KEYWORD with VALUE.
485 Fourth argument TYPE is the custom option type."
486 (if purify-flag
487 (setq value (purecopy value)))
488 (cond ((eq keyword :group)
489 (custom-add-to-group value symbol type))
490 ((eq keyword :version)
491 (custom-add-version symbol value))
492 ((eq keyword :link)
493 (custom-add-link symbol value))
494 ((eq keyword :load)
495 (custom-add-load symbol value))
496 ((eq keyword :tag)
497 (put symbol 'custom-tag value))
498 ((eq keyword :set-after)
499 (custom-add-dependencies symbol value))
501 (error "Unknown keyword %s" keyword))))
503 (defun custom-add-dependencies (symbol value)
504 "To the custom option SYMBOL, add dependencies specified by VALUE.
505 VALUE should be a list of symbols. For each symbol in that list,
506 this specifies that SYMBOL should be set after the specified symbol, if
507 both appear in constructs like `custom-set-variables'."
508 (unless (listp value)
509 (error "Invalid custom dependency `%s'" value))
510 (let* ((deps (get symbol 'custom-dependencies))
511 (new-deps deps))
512 (while value
513 (let ((dep (car value)))
514 (unless (symbolp dep)
515 (error "Invalid custom dependency `%s'" dep))
516 (unless (memq dep new-deps)
517 (setq new-deps (cons dep new-deps)))
518 (setq value (cdr value))))
519 (unless (eq deps new-deps)
520 (put symbol 'custom-dependencies new-deps))))
522 (defun custom-add-option (symbol option)
523 "To the variable SYMBOL add OPTION.
525 If SYMBOL's custom type is a hook, OPTION should be a hook member.
526 If SYMBOL's custom type is an alist, OPTION specifies a symbol
527 to offer to the user as a possible key in the alist.
528 For other custom types, this has no effect."
529 (let ((options (get symbol 'custom-options)))
530 (unless (member option options)
531 (put symbol 'custom-options (cons option options)))))
533 (defun custom-add-link (symbol widget)
534 "To the custom option SYMBOL add the link WIDGET."
535 (let ((links (get symbol 'custom-links)))
536 (unless (member widget links)
537 (put symbol 'custom-links (cons (purecopy widget) links)))))
539 (defun custom-add-version (symbol version)
540 "To the custom option SYMBOL add the version VERSION."
541 (put symbol 'custom-version (purecopy version)))
543 (defun custom-add-load (symbol load)
544 "To the custom option SYMBOL add the dependency LOAD.
545 LOAD should be either a library file name, or a feature name."
546 (let ((loads (get symbol 'custom-loads)))
547 (unless (member load loads)
548 (put symbol 'custom-loads (cons (purecopy load) loads)))))
550 (defun custom-autoload (symbol load)
551 "Mark SYMBOL as autoloaded custom variable and add dependency LOAD."
552 (put symbol 'custom-autoload t)
553 (custom-add-load symbol load))
555 ;; This test is also in the C code of `user-variable-p'.
556 (defun custom-variable-p (variable)
557 "Return non-nil if VARIABLE is a custom variable.
558 This recursively follows aliases."
559 (setq variable (indirect-variable variable))
560 (or (get variable 'standard-value)
561 (get variable 'custom-autoload)))
563 ;;; Loading files needed to customize a symbol.
564 ;;; This is in custom.el because menu-bar.el needs it for toggle cmds.
566 (defvar custom-load-recursion nil
567 "Hack to avoid recursive dependencies.")
569 (defun custom-load-symbol (symbol)
570 "Load all dependencies for SYMBOL."
571 (unless custom-load-recursion
572 (let ((custom-load-recursion t))
573 ;; Load these files if not already done,
574 ;; to make sure we know all the dependencies of SYMBOL.
575 (condition-case nil
576 (require 'cus-load)
577 (error nil))
578 (condition-case nil
579 (require 'cus-start)
580 (error nil))
581 (dolist (load (get symbol 'custom-loads))
582 (cond ((symbolp load) (condition-case nil (require load) (error nil)))
583 ;; This is subsumed by the test below, but it's much faster.
584 ((assoc load load-history))
585 ;; This was just (assoc (locate-library load) load-history)
586 ;; but has been optimized not to load locate-library
587 ;; if not necessary.
588 ((let ((regexp (concat "\\(\\`\\|/\\)" (regexp-quote load)
589 "\\(\\'\\|\\.\\)"))
590 (found nil))
591 (dolist (loaded load-history)
592 (and (stringp (car loaded))
593 (string-match regexp (car loaded))
594 (setq found t)))
595 found))
596 ;; Without this, we would load cus-edit recursively.
597 ;; We are still loading it when we call this,
598 ;; and it is not in load-history yet.
599 ((equal load "cus-edit"))
600 (t (condition-case nil (load load) (error nil))))))))
602 (defvar custom-known-themes '(user standard)
603 "Themes that have been defined with `deftheme'.
604 The default value is the list (user standard). The theme `standard'
605 contains the settings before custom themes are applied. The
606 theme `user' contains all the settings the user customized and saved.
607 Additional themes declared with the `deftheme' macro will be added to
608 the front of this list.")
610 (defsubst custom-theme-p (theme)
611 "Non-nil when THEME has been defined."
612 (memq theme custom-known-themes))
614 (defsubst custom-check-theme (theme)
615 "Check whether THEME is valid, and signal an error if it is not."
616 (unless (custom-theme-p theme)
617 (error "Unknown theme `%s'" theme)))
619 ;;; Initializing.
621 (defun custom-push-theme (prop symbol theme mode value)
622 "Record a value for face or variable SYMBOL in custom theme THEME.
623 PROP is`theme-face' for a face, `theme-value' for a variable.
624 The value is specified by (THEME MODE VALUE), which is interpreted
625 by `custom-theme-value'.
627 MODE can be either the symbol `set' or the symbol `reset'. If it is the
628 symbol `set', then VALUE is the value to use. If it is the symbol
629 `reset', then VALUE is another theme, whose value for this face or
630 variable should be used.
632 In the following example for the variable `goto-address-url-face', the
633 theme `subtle-hacker' uses the same value for the variable as the theme
634 `gnome2':
636 \((standard set bold)
637 \(gnome2 set info-xref)
638 \(jonadab set underline)
639 \(subtle-hacker reset gnome2))
642 If a value has been stored for themes A B and C, and a new value
643 is to be stored for theme C, then the old value of C is discarded.
644 If a new value is to be stored for theme B, however, the old value
645 of B is not discarded because B is not the car of the list.
647 For variables, list property PROP is `theme-value'.
648 For faces, list property PROP is `theme-face'.
649 This is used in `custom-do-theme-reset', for example.
651 The list looks the same in any case; the examples shows a possible
652 value of the `theme-face' property for the face `region':
654 \((gnome2 set ((t (:foreground \"cyan\" :background \"dark cyan\"))))
655 \(standard set ((((class color) (background dark))
656 \(:background \"blue\"))
657 \(t (:background \"gray\")))))
659 This records values for the `standard' and the `gnome2' themes.
660 The user has not customized the face; had he done that,
661 the list would contain an entry for the `user' theme, too.
662 See `custom-known-themes' for a list of known themes."
663 (let* ((old (get symbol prop))
664 (setting (assq theme old)))
665 ;; Alter an existing theme-setting for the symbol,
666 ;; or add a new one.
667 (if setting
668 (progn
669 (setcar (cdr setting) mode)
670 (setcar (cddr setting) value))
671 ;; If no custom theme has been applied yet, first save the
672 ;; current values to the 'standard theme.
673 (if (null old)
674 (if (and (eq prop 'theme-value)
675 (boundp symbol))
676 (setq old
677 (list (list 'standard 'set (symbol-value symbol))))
678 (if (facep symbol)
679 (setq old (list (list 'standard 'set (list
680 (append '(t) (custom-face-attributes-get symbol nil)))))))))
681 (put symbol prop (cons (list theme mode value) old)))
682 ;; Record, for each theme, all its settings.
683 (put theme 'theme-settings
684 (cons (list prop symbol theme mode value)
685 (get theme 'theme-settings)))))
687 (defvar custom-local-buffer nil
688 "Non-nil, in a Customization buffer, means customize a specific buffer.
689 If this variable is non-nil, it should be a buffer,
690 and it means customize the local bindings of that buffer.
691 This variable is a permanent local, and it normally has a local binding
692 in every Customization buffer.")
693 (put 'custom-local-buffer 'permanent-local t)
695 (defun custom-set-variables (&rest args)
696 "Install user customizations of variable values specified in ARGS.
697 These settings are registered as theme `user'.
698 The arguments should each be a list of the form:
700 (SYMBOL EXP [NOW [REQUEST [COMMENT]]])
702 This stores EXP (without evaluating it) as the saved value for SYMBOL.
703 If NOW is present and non-nil, then also evaluate EXP and set
704 the default value for the SYMBOL to the value of EXP.
706 REQUEST is a list of features we must require in order to
707 handle SYMBOL properly.
708 COMMENT is a comment string about SYMBOL."
709 (apply 'custom-theme-set-variables 'user args))
711 (defun custom-reevaluate-setting (symbol)
712 "Reset the value of SYMBOL by re-evaluating its saved or standard value.
713 Use the :set function to do so. This is useful for customizable options
714 that are defined before their standard value can really be computed.
715 E.g. dumped variables whose default depends on run-time information."
716 (funcall (or (get symbol 'custom-set) 'set-default)
717 symbol
718 (eval (car (or (get symbol 'saved-value) (get symbol 'standard-value))))))
720 (defun custom-theme-set-variables (theme &rest args)
721 "Initialize variables for theme THEME according to settings in ARGS.
722 Each of the arguments in ARGS should be a list of this form:
724 (SYMBOL EXP [NOW [REQUEST [COMMENT]]])
726 This stores EXP (without evaluating it) as the saved value for SYMBOL.
727 If NOW is present and non-nil, then also evaluate EXP and set
728 the default value for the SYMBOL to the value of EXP.
730 REQUEST is a list of features we must require in order to
731 handle SYMBOL properly.
732 COMMENT is a comment string about SYMBOL.
734 Several properties of THEME and SYMBOL are used in the process:
736 If THEME's property `theme-immediate' is non-nil, this is equivalent of
737 providing the NOW argument to all symbols in the argument list:
738 evaluate each EXP and set the corresponding SYMBOL. However,
739 there's a difference in the handling of SYMBOL's property
740 `force-value': if NOW is non-nil, SYMBOL's property `force-value' is set to
741 the symbol `rogue', else if THEME's property `theme-immediate' is non-nil,
742 SYMBOL's property `force-value' is set to the symbol `immediate'.
744 EXP itself is saved unevaluated as SYMBOL property `saved-value' and
745 in SYMBOL's list property `theme-value' \(using `custom-push-theme')."
746 (custom-check-theme theme)
747 (setq args
748 (sort args
749 (lambda (a1 a2)
750 (let* ((sym1 (car a1))
751 (sym2 (car a2))
752 (1-then-2 (memq sym1 (get sym2 'custom-dependencies)))
753 (2-then-1 (memq sym2 (get sym1 'custom-dependencies))))
754 (cond ((and 1-then-2 2-then-1)
755 (error "Circular custom dependency between `%s' and `%s'"
756 sym1 sym2))
757 (2-then-1 nil)
758 ;; Put minor modes and symbols with :require last.
759 ;; Putting minor modes last ensures that the mode
760 ;; function will see other customized values rather
761 ;; than default values.
762 (t (or (nth 3 a2)
763 (eq (get sym2 'custom-set)
764 'custom-set-minor-mode))))))))
765 (while args
766 (let ((entry (car args)))
767 (if (listp entry)
768 (let* ((symbol (indirect-variable (nth 0 entry)))
769 (value (nth 1 entry))
770 (now (nth 2 entry))
771 (requests (nth 3 entry))
772 (comment (nth 4 entry))
773 set)
774 (when requests
775 (put symbol 'custom-requests requests)
776 (mapc 'require requests))
777 (setq set (or (get symbol 'custom-set) 'custom-set-default))
778 (put symbol 'saved-value (list value))
779 (put symbol 'saved-variable-comment comment)
780 (custom-push-theme 'theme-value symbol theme 'set value)
781 ;; Allow for errors in the case where the setter has
782 ;; changed between versions, say, but let the user know.
783 (condition-case data
784 (cond (now
785 ;; Rogue variable, set it now.
786 (put symbol 'force-value t)
787 (funcall set symbol (eval value)))
788 ((default-boundp symbol)
789 ;; Something already set this, overwrite it.
790 (funcall set symbol (eval value))))
791 (error
792 (message "Error setting %s: %s" symbol data)))
793 (setq args (cdr args))
794 (and (or now (default-boundp symbol))
795 (put symbol 'variable-comment comment)))
796 ;; Old format, a plist of SYMBOL VALUE pairs.
797 (message "Warning: old format `custom-set-variables'")
798 (ding)
799 (sit-for 2)
800 (let ((symbol (indirect-variable (nth 0 args)))
801 (value (nth 1 args)))
802 (put symbol 'saved-value (list value))
803 (custom-push-theme 'theme-value symbol theme 'set value))
804 (setq args (cdr (cdr args)))))))
806 (defun custom-set-default (variable value)
807 "Default :set function for a customizable variable.
808 Normally, this sets the default value of VARIABLE to VALUE,
809 but if `custom-local-buffer' is non-nil,
810 this sets the local binding in that buffer instead."
811 (if custom-local-buffer
812 (with-current-buffer custom-local-buffer
813 (set variable value))
814 (set-default variable value)))
816 (defun custom-set-minor-mode (variable value)
817 ":set function for minor mode variables.
818 Normally, this sets the default value of VARIABLE to nil if VALUE
819 is nil and to t otherwise,
820 but if `custom-local-buffer' is non-nil,
821 this sets the local binding in that buffer instead."
822 (if custom-local-buffer
823 (with-current-buffer custom-local-buffer
824 (funcall variable (if value 1 0)))
825 (funcall variable (if value 1 0))))
827 (defun custom-quote (sexp)
828 "Quote SEXP iff it is not self quoting."
829 (if (or (memq sexp '(t nil))
830 (keywordp sexp)
831 (and (listp sexp)
832 (memq (car sexp) '(lambda)))
833 (stringp sexp)
834 (numberp sexp)
835 (vectorp sexp)
836 ;;; (and (fboundp 'characterp)
837 ;;; (characterp sexp))
839 sexp
840 (list 'quote sexp)))
842 (defun customize-mark-to-save (symbol)
843 "Mark SYMBOL for later saving.
845 If the default value of SYMBOL is different from the standard value,
846 set the `saved-value' property to a list whose car evaluates to the
847 default value. Otherwise, set it to nil.
849 To actually save the value, call `custom-save-all'.
851 Return non-nil iff the `saved-value' property actually changed."
852 (let* ((get (or (get symbol 'custom-get) 'default-value))
853 (value (funcall get symbol))
854 (saved (get symbol 'saved-value))
855 (standard (get symbol 'standard-value))
856 (comment (get symbol 'customized-variable-comment)))
857 ;; Save default value iff different from standard value.
858 (if (or (null standard)
859 (not (equal value (condition-case nil
860 (eval (car standard))
861 (error nil)))))
862 (put symbol 'saved-value (list (custom-quote value)))
863 (put symbol 'saved-value nil))
864 ;; Clear customized information (set, but not saved).
865 (put symbol 'customized-value nil)
866 ;; Save any comment that might have been set.
867 (when comment
868 (put symbol 'saved-variable-comment comment))
869 (not (equal saved (get symbol 'saved-value)))))
871 (defun customize-mark-as-set (symbol)
872 "Mark current value of SYMBOL as being set from customize.
874 If the default value of SYMBOL is different from the saved value if any,
875 or else if it is different from the standard value, set the
876 `customized-value' property to a list whose car evaluates to the
877 default value. Otherwise, set it to nil.
879 Return non-nil iff the `customized-value' property actually changed."
880 (let* ((get (or (get symbol 'custom-get) 'default-value))
881 (value (funcall get symbol))
882 (customized (get symbol 'customized-value))
883 (old (or (get symbol 'saved-value) (get symbol 'standard-value))))
884 ;; Mark default value as set iff different from old value.
885 (if (or (null old)
886 (not (equal value (condition-case nil
887 (eval (car old))
888 (error nil)))))
889 (put symbol 'customized-value (list (custom-quote value)))
890 (put symbol 'customized-value nil))
891 ;; Changed?
892 (not (equal customized (get symbol 'customized-value)))))
894 ;;; Defining themes.
896 ;; deftheme is used at the beginning of the file that records a theme.
898 (defmacro deftheme (theme &optional doc &rest args)
899 "Declare custom theme THEME.
900 The optional argument DOC is a doc string describing the theme.
901 The remaining arguments should have the form
903 [KEYWORD VALUE]...
905 The following KEYWORD's are defined:
907 :short-description
908 VALUE is a short (one line) description of the theme. If not
909 given, DOC is used.
910 :immediate
911 If VALUE is non-nil, variables specified in this theme are set
912 immediately when loading the theme.
913 :variable-set-string
914 VALUE is a string used to indicate that a variable takes its
915 setting from this theme. It is passed to FORMAT with the name
916 of the theme as an additional argument. If not given, a
917 generic description is used.
918 :variable-reset-string
919 VALUE is a string used in the case a variable has been forced
920 to its value in this theme. It is passed to FORMAT with the
921 name of the theme as an additional argument. If not given, a
922 generic description is used.
923 :face-set-string
924 VALUE is a string used to indicate that a face takes its
925 setting from this theme. It is passed to FORMAT with the name
926 of the theme as an additional argument. If not given, a
927 generic description is used.
928 :face-reset-string
929 VALUE is a string used in the case a face has been forced to
930 its value in this theme. It is passed to FORMAT with the name
931 of the theme as an additional argument. If not given, a
932 generic description is used.
934 Any theme `foo' should be defined in a file called `foo-theme.el';
935 see `custom-make-theme-feature' for more information."
936 (let ((feature (custom-make-theme-feature theme)))
937 ;; It is better not to use backquote in this file,
938 ;; because that makes a bootstrapping problem
939 ;; if you need to recompile all the Lisp files using interpreted code.
940 (nconc (list 'custom-declare-theme
941 (list 'quote theme)
942 (list 'quote feature)
943 doc)
944 args)))
946 (defun custom-declare-theme (theme feature &optional doc &rest args)
947 "Like `deftheme', but THEME is evaluated as a normal argument.
948 FEATURE is the feature this theme provides. This symbol is created
949 from THEME by `custom-make-theme-feature'."
950 (add-to-list 'custom-known-themes theme)
951 (put theme 'theme-feature feature)
952 (when doc
953 (put theme 'theme-documentation doc))
954 (while args
955 (let ((arg (car args)))
956 (setq args (cdr args))
957 (unless (symbolp arg)
958 (error "Junk in args %S" args))
959 (let ((keyword arg)
960 (value (car args)))
961 (unless args
962 (error "Keyword %s is missing an argument" keyword))
963 (setq args (cdr args))
964 (cond ((eq keyword :short-description)
965 (put theme 'theme-short-description value))
966 ((eq keyword :immediate)
967 (put theme 'theme-immediate value))
968 ((eq keyword :variable-set-string)
969 (put theme 'theme-variable-set-string value))
970 ((eq keyword :variable-reset-string)
971 (put theme 'theme-variable-reset-string value))
972 ((eq keyword :face-set-string)
973 (put theme 'theme-face-set-string value))
974 ((eq keyword :face-reset-string)
975 (put theme 'theme-face-reset-string value)))))))
977 (defun custom-make-theme-feature (theme)
978 "Given a symbol THEME, create a new symbol by appending \"-theme\".
979 Store this symbol in the `theme-feature' property of THEME.
980 Calling `provide-theme' to provide THEME actually puts `THEME-theme'
981 into `features'.
983 This allows for a file-name convention for autoloading themes:
984 Every theme X has a property `provide-theme' whose value is \"X-theme\".
985 \(load-theme X) then attempts to load the file `X-theme.el'."
986 (intern (concat (symbol-name theme) "-theme")))
988 ;;; Loading themes.
990 ;; The variable and face settings of a theme are recorded in
991 ;; the `theme-settings' property of the theme name.
992 ;; This property's value is a list of elements, each of the form
993 ;; (PROP SYMBOL THEME MODE VALUE), where PROP is `theme-value' or `theme-face'
994 ;; and SYMBOL is the face or variable name.
995 ;; THEME is the theme name itself; that's redundant, but simplifies things.
996 ;; MODE is `set' or `reset'.
997 ;; If MODE is `set', then VALUE is an expression that specifies the
998 ;; theme's setting for SYMBOL.
999 ;; If MODE is `reset', then VALUE is another theme,
1000 ;; and it means to use the value from that theme.
1002 ;; Each variable has a `theme-value' property that describes all the
1003 ;; settings of enabled themes that apply to it.
1004 ;; Each face name has a `theme-face' property that describes all the
1005 ;; settings of enabled themes that apply to it.
1006 ;; The property value is a list of settings, each with the form
1007 ;; (THEME MODE VALUE). THEME, MODE and VALUE are as above.
1008 ;; Each of these lists is ordered by decreasing theme precedence.
1009 ;; Thus, the first element is always the one that is in effect.
1011 ;; Disabling a theme removes its settings from the `theme-value' and
1012 ;; `theme-face' properties, but the theme's own `theme-settings'
1013 ;; property remains unchanged.
1015 ;; Loading a theme implicitly enables it. Enabling a theme adds its
1016 ;; settings to the symbols' `theme-value' and `theme-face' properties,
1017 ;; or moves them to the front of those lists if they're already present.
1019 (defvar custom-loaded-themes nil
1020 "Custom themes that have been loaded.")
1022 (defcustom custom-theme-directory
1023 (if (eq system-type 'ms-dos)
1024 ;; MS-DOS cannot have initial dot.
1025 "~/_emacs.d/"
1026 "~/.emacs.d/")
1027 "Directory in which Custom theme files should be written.
1028 `load-theme' searches this directory in addition to load-path.
1029 The command `customize-create-theme' writes the files it produces
1030 into this directory."
1031 :type 'string
1032 :group 'customize
1033 :version "22.1")
1035 (defun custom-theme-loaded-p (theme)
1036 "Return non-nil if THEME has been loaded."
1037 (memq theme custom-loaded-themes))
1039 (defvar custom-enabled-themes '(user)
1040 "Custom themes currently enabled, highest precedence first.
1041 The first one is always `user'.")
1043 (defun custom-theme-enabled-p (theme)
1044 "Return non-nil if THEME is enabled."
1045 (memq theme custom-enabled-themes))
1047 (defun provide-theme (theme)
1048 "Indicate that this file provides THEME.
1049 Add THEME to `custom-loaded-themes', and `provide' whatever
1050 feature name is stored in THEME's property `theme-feature'.
1052 Usually the `theme-feature' property contains a symbol created
1053 by `custom-make-theme-feature'."
1054 (custom-check-theme theme)
1055 (provide (get theme 'theme-feature))
1056 (push theme custom-loaded-themes)
1057 ;; Loading a theme also installs its settings,
1058 ;; so mark it as "enabled".
1059 (push theme custom-enabled-themes)
1060 ;; `user' must always be the highest-precedence enabled theme.
1061 ;; Make that remain true. (This has the effect of making user settings
1062 ;; override the ones just loaded, too.)
1063 (enable-theme 'user))
1065 (defun load-theme (theme)
1066 "Try to load a theme's settings from its file.
1067 This also enables the theme; use `disable-theme' to disable it."
1069 ;; THEME's feature is stored in THEME's `theme-feature' property.
1070 ;; Usually the `theme-feature' property contains a symbol created
1071 ;; by `custom-make-theme-feature'.
1073 ;; Note we do no check for validity of the theme here.
1074 ;; This allows to pull in themes by a file-name convention
1075 (interactive "SCustom theme name: ")
1076 (let ((load-path (if (file-directory-p custom-theme-directory)
1077 (cons custom-theme-directory load-path)
1078 load-path)))
1079 (require (or (get theme 'theme-feature)
1080 (custom-make-theme-feature theme)))))
1082 ;;; How to load and enable various themes as part of `user'.
1084 (defun custom-theme-load-themes (by-theme &rest body)
1085 "Load the themes specified by BODY.
1086 Record them as required by theme BY-THEME.
1088 BODY is a sequence of either
1090 THEME
1091 Load THEME and enable it.
1092 \(reset THEME)
1093 Undo all the settings made by THEME
1094 \(hidden THEME)
1095 Load THEME but do not enable it.
1097 All the themes loaded for BY-THEME are recorded in BY-THEME's property
1098 `theme-loads-themes'."
1099 (custom-check-theme by-theme)
1100 (let ((themes-loaded (get by-theme 'theme-loads-themes)))
1101 (dolist (theme body)
1102 (cond ((and (consp theme) (eq (car theme) 'reset))
1103 (disable-theme (cadr theme)))
1104 ((and (consp theme) (eq (car theme) 'hidden))
1105 (load-theme (cadr theme))
1106 (disable-theme (cadr theme)))
1108 (load-theme theme)))
1109 (push theme themes-loaded))
1110 (put by-theme 'theme-loads-themes themes-loaded)))
1112 (defun custom-load-themes (&rest body)
1113 "Load themes for the USER theme as specified by BODY.
1115 See `custom-theme-load-themes' for more information on BODY."
1116 (apply 'custom-theme-load-themes 'user body))
1118 ;;; Enabling and disabling loaded themes.
1120 (defun enable-theme (theme)
1121 "Reenable all variable and face settings defined by THEME.
1122 The newly enabled theme gets the highest precedence (after `user').
1123 If it is already enabled, just give it highest precedence (after `user').
1125 This signals an error if THEME does not specify any theme
1126 settings. Theme settings are set using `load-theme'."
1127 (interactive "SEnable Custom theme: ")
1128 (let ((settings (get theme 'theme-settings)))
1129 (if (and (not (eq theme 'user)) (null settings))
1130 (error "No theme settings defined in %s." (symbol-name theme)))
1131 (dolist (s settings)
1132 (let* ((prop (car s))
1133 (symbol (cadr s))
1134 (spec-list (get symbol prop)))
1135 (put symbol prop (cons (cddr s) (assq-delete-all theme spec-list)))
1136 (if (eq prop 'theme-value)
1137 (custom-theme-recalc-variable symbol)
1138 (if (facep symbol)
1139 (custom-theme-recalc-face symbol))))))
1140 (setq custom-enabled-themes
1141 (cons theme (delq theme custom-enabled-themes)))
1142 ;; `user' must always be the highest-precedence enabled theme.
1143 (unless (eq theme 'user)
1144 (enable-theme 'user)))
1146 (defun disable-theme (theme)
1147 "Disable all variable and face settings defined by THEME.
1148 See `custom-known-themes' for a list of known themes."
1149 (interactive "SDisable Custom theme: ")
1150 (let ((settings (get theme 'theme-settings)))
1151 (dolist (s settings)
1152 (let* ((prop (car s))
1153 (symbol (cadr s))
1154 (spec-list (get symbol prop)))
1155 (put symbol prop (assq-delete-all theme spec-list))
1156 (if (eq prop 'theme-value)
1157 (custom-theme-recalc-variable symbol)
1158 (custom-theme-recalc-face symbol)))))
1159 (setq custom-enabled-themes
1160 (delq theme custom-enabled-themes)))
1162 (defun custom-theme-value (theme setting-list)
1163 "Determine the value specified for THEME according to SETTING-LIST.
1164 Returns a list whose car is the specified value, if we
1165 find one; nil otherwise.
1167 SETTING-LIST is an alist with themes as its key.
1168 Each element has the form:
1170 \(THEME MODE VALUE)
1172 MODE is either the symbol `set' or the symbol `reset'. See
1173 `custom-push-theme' for more information on the format of
1174 SETTING-LIST."
1175 ;; Note we do _NOT_ signal an error if the theme is unknown
1176 ;; it might have gone away without the user knowing.
1177 (let ((elt (cdr (assoc theme setting-list))))
1178 (if elt
1179 (if (eq (car elt) 'set)
1180 (cdr elt)
1181 ;; `reset' means refer to another theme's value in the same alist.
1182 (custom-theme-value (cadr elt) setting-list)))))
1184 (defun custom-variable-theme-value (variable)
1185 "Return (list VALUE) indicating the custom theme value of VARIABLE.
1186 That is to say, it specifies what the value should be according to
1187 currently enabled custom themes.
1189 This function returns nil if no custom theme specifies a value for VARIABLE."
1190 (let* ((theme-value (get variable 'theme-value)))
1191 (if theme-value
1192 (custom-theme-value (car (car theme-value)) theme-value))))
1194 (defun custom-theme-recalc-variable (variable)
1195 "Set VARIABLE according to currently enabled custom themes."
1196 (let ((valspec (custom-variable-theme-value variable)))
1197 (when valspec
1198 (put variable 'saved-value valspec))
1199 (unless valspec
1200 (setq valspec (get variable 'standard-value)))
1201 (when valspec
1202 (if (or (get 'force-value variable) (default-boundp variable))
1203 (funcall (or (get variable 'custom-set) 'set-default) variable
1204 (eval (car valspec)))))))
1206 (defun custom-theme-recalc-face (face)
1207 "Set FACE according to currently enabled custom themes."
1208 (let ((theme-faces (reverse (get face 'theme-face))))
1209 (dolist (spec theme-faces)
1210 (face-spec-set face (car (cddr spec))))))
1212 (defun custom-theme-reset-variables (theme &rest args)
1213 "Reset the specs in THEME of some variables to their values in other themes.
1214 Each of the arguments ARGS has this form:
1216 (VARIABLE FROM-THEME)
1218 This means reset VARIABLE to its value in FROM-THEME."
1219 (custom-check-theme theme)
1220 (dolist (arg args)
1221 (custom-push-theme 'theme-value (car arg) theme 'reset (cadr arg))))
1223 (defun custom-reset-variables (&rest args)
1224 "Reset the specs of some variables to their values in certain themes.
1225 This creates settings in the `user' theme.
1227 Each of the arguments ARGS has this form:
1229 (VARIABLE FROM-THEME)
1231 This means reset VARIABLE to its value in FROM-THEME."
1232 (apply 'custom-theme-reset-variables 'user args))
1234 ;;; The End.
1236 ;; Process the defcustoms for variables loaded before this file.
1237 (while custom-declare-variable-list
1238 (apply 'custom-declare-variable (car custom-declare-variable-list))
1239 (setq custom-declare-variable-list (cdr custom-declare-variable-list)))
1241 (provide 'custom)
1243 ;; arch-tag: 041b6116-aabe-4f9a-902d-74092bc3dab2
1244 ;;; custom.el ends here