*** empty log message ***
[emacs.git] / lisp / custom.el
blob2ddd7ceb9435b57105c015bf2cbc97d2933a7668
1 ;;; custom.el --- tools for declaring and initializing options
2 ;;
3 ;; Copyright (C) 1996, 1997, 1999, 2001, 2002, 2004
4 ;; 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., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, 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-reset (symbol value)
80 "Initialize SYMBOL based on VALUE.
81 Set the symbol, using its `:set' function (or `set-default' if it has none).
82 The value is either the symbol's current value
83 \(as obtained using the `:get' function), if any,
84 or the value in the symbol's `saved-value' property if any,
85 or (last of all) VALUE."
86 (funcall (or (get symbol 'custom-set) 'set-default)
87 symbol
88 (cond ((default-boundp symbol)
89 (funcall (or (get symbol 'custom-get) 'default-value)
90 symbol))
91 ((get symbol 'saved-value)
92 (eval (car (get symbol 'saved-value))))
94 (eval value)))))
96 (defun custom-initialize-changed (symbol value)
97 "Initialize SYMBOL with VALUE.
98 Like `custom-initialize-reset', but only use the `:set' function if
99 not using the standard setting.
100 For the standard setting, use `set-default'."
101 (cond ((default-boundp symbol)
102 (funcall (or (get symbol 'custom-set) 'set-default)
103 symbol
104 (funcall (or (get symbol 'custom-get) 'default-value)
105 symbol)))
106 ((get symbol 'saved-value)
107 (funcall (or (get symbol 'custom-set) 'set-default)
108 symbol
109 (eval (car (get symbol 'saved-value)))))
111 (set-default symbol (eval value)))))
113 (defun custom-declare-variable (symbol default doc &rest args)
114 "Like `defcustom', but SYMBOL and DEFAULT are evaluated as normal arguments.
115 DEFAULT should be an expression to evaluate to compute the default value,
116 not the default value itself.
118 DEFAULT is stored as SYMBOL's value in the standard theme. See
119 `custom-known-themes' for a list of known themes. For backwards
120 compatibility, DEFAULT is also stored in SYMBOL's property
121 `standard-value'. At the same time, SYMBOL's property `force-value' is
122 set to nil, as the value is no longer rogue."
123 ;; Remember the standard setting. The value should be in the standard
124 ;; theme, not in this property. However, his would require changeing
125 ;; the C source of defvar and others as well...
126 (put symbol 'standard-value (list default))
127 ;; Maybe this option was rogue in an earlier version. It no longer is.
128 (when (get symbol 'force-value)
129 (put symbol 'force-value nil))
130 (when doc
131 (put symbol 'variable-documentation doc))
132 (let ((initialize 'custom-initialize-reset)
133 (requests nil))
134 (unless (memq :group args)
135 (custom-add-to-group (custom-current-group) symbol 'custom-variable))
136 (while args
137 (let ((arg (car args)))
138 (setq args (cdr args))
139 (unless (symbolp arg)
140 (error "Junk in args %S" args))
141 (let ((keyword arg)
142 (value (car args)))
143 (unless args
144 (error "Keyword %s is missing an argument" keyword))
145 (setq args (cdr args))
146 (cond ((eq keyword :initialize)
147 (setq initialize value))
148 ((eq keyword :set)
149 (put symbol 'custom-set value))
150 ((eq keyword :get)
151 (put symbol 'custom-get value))
152 ((eq keyword :require)
153 (push value requests))
154 ((eq keyword :type)
155 (put symbol 'custom-type (purecopy value)))
156 ((eq keyword :options)
157 (if (get symbol 'custom-options)
158 ;; Slow safe code to avoid duplicates.
159 (mapc (lambda (option)
160 (custom-add-option symbol option))
161 value)
162 ;; Fast code for the common case.
163 (put symbol 'custom-options (copy-sequence value))))
165 (custom-handle-keyword symbol keyword value
166 'custom-variable))))))
167 (put symbol 'custom-requests requests)
168 ;; Do the actual initialization.
169 (unless custom-dont-initialize
170 (funcall initialize symbol default)))
171 (push (cons 'defvar symbol) current-load-list)
172 (run-hooks 'custom-define-hook)
173 symbol)
175 (defmacro defcustom (symbol value doc &rest args)
176 "Declare SYMBOL as a customizable variable that defaults to VALUE.
177 DOC is the variable documentation.
179 Neither SYMBOL nor VALUE need to be quoted.
180 If SYMBOL is not already bound, initialize it to VALUE.
181 The remaining arguments should have the form
183 [KEYWORD VALUE]...
185 The following keywords are meaningful:
187 :type VALUE should be a widget type for editing the symbol's value.
188 :options VALUE should be a list of valid members of the widget type.
189 :group VALUE should be a customization group.
190 Add SYMBOL to that group.
191 :link LINK-DATA
192 Include an external link after the documentation string for this
193 item. This is a sentence containing an active field which
194 references some other documentation.
196 There are three alternatives you can use for LINK-DATA:
198 (custom-manual INFO-NODE)
199 Link to an Info node; INFO-NODE is a string which specifies
200 the node name, as in \"(emacs)Top\". The link appears as
201 `[manual]' in the customization buffer.
203 (info-link INFO-NODE)
204 Like `custom-manual' except that the link appears in the
205 customization buffer with the Info node name.
207 (url-link URL)
208 Link to a web page; URL is a string which specifies the URL.
209 The link appears in the customization buffer as URL.
211 You can specify the text to use in the customization buffer by
212 adding `:tag NAME' after the first element of the LINK-DATA; for
213 example, (info-link :tag \"foo\" \"(emacs)Top\") makes a link to the
214 Emacs manual which appears in the buffer as `foo'.
216 An item can have more than one external link; however, most items
217 have none at all.
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 It takes two arguments, the symbol to set and the value to
225 give it. The default choice of function is `custom-set-default'.
226 :get VALUE should be a function to extract the value of symbol.
227 The function takes one argument, a symbol, and should return
228 the current value for that symbol. The default choice of function
229 is `custom-default-value'.
230 :require
231 VALUE should be a feature symbol. If you save a value
232 for this option, then when your `.emacs' file loads the value,
233 it does (require VALUE) first.
234 :version
235 VALUE should be a string specifying that the variable was
236 first introduced, or its default value was changed, in Emacs
237 version VERSION.
238 :tag LABEL
239 Use LABEL, a string, instead of the item's name, to label the item
240 in customization menus and buffers.
241 :load FILE
242 Load file FILE (a string) before displaying this customization
243 item. Loading is done with `load', and only if the file is
244 not already loaded.
245 :set-after VARIABLES
246 Specifies that SYMBOL should be set after the list of variables
247 VARIABLES when both have been customized.
249 If SYMBOL has a local binding, then this form affects the local
250 binding. This is normally not what you want. Thus, if you need
251 to load a file defining variables with this form, or with
252 `defvar' or `defconst', you should always load that file
253 _outside_ any bindings for these variables. \(`defvar' and
254 `defconst' behave similarly in this respect.)
256 Read the section about customization in the Emacs Lisp manual for more
257 information."
258 ;; It is better not to use backquote in this file,
259 ;; because that makes a bootstrapping problem
260 ;; if you need to recompile all the Lisp files using interpreted code.
261 (nconc (list 'custom-declare-variable
262 (list 'quote symbol)
263 (list 'quote value)
264 doc)
265 args))
267 ;;; The `defface' Macro.
269 (defmacro defface (face spec doc &rest args)
270 "Declare FACE as a customizable face that defaults to SPEC.
271 FACE does not need to be quoted.
273 Third argument DOC is the face documentation.
275 If FACE has been set with `custom-set-faces', set the face attributes
276 as specified by that function, otherwise set the face attributes
277 according to SPEC.
279 The remaining arguments should have the form
281 [KEYWORD VALUE]...
283 The following KEYWORDs are defined:
285 :group VALUE should be a customization group.
286 Add FACE to that group.
288 SPEC should be an alist of the form ((DISPLAY ATTS)...).
290 The first element of SPEC where the DISPLAY matches the frame
291 is the one that takes effect in that frame. The ATTRs in this
292 element take effect; the other elements are ignored, on that frame.
294 ATTS is a list of face attributes followed by their values:
295 (ATTR VALUE ATTR VALUE...)
297 The possible attributes are `:family', `:width', `:height', `:weight',
298 `:slant', `:underline', `:overline', `:strike-through', `:box',
299 `:foreground', `:background', `:stipple', `:inverse-video', and `:inherit'.
301 DISPLAY can either be the symbol t, which will match all frames, or an
302 alist of the form \((REQ ITEM...)...). For the DISPLAY to match a
303 FRAME, the REQ property of the frame must match one of the ITEM. The
304 following REQ are defined:
306 `type' (the value of `window-system')
307 Under X, in addition to the values `window-system' can take,
308 `motif', `lucid', `gtk' and `x-toolkit' are allowed, and match when
309 the Motif toolkit, Lucid toolkit, GTK toolkit or any X toolkit is in use.
311 `class' (the frame's color support)
312 Should be one of `color', `grayscale', or `mono'.
314 `background' (what color is used for the background text)
315 Should be one of `light' or `dark'.
317 `min-colors' (the minimum number of colors the frame should support)
318 Should be an integer, it is compared with the result of
319 `display-color-cells'.
321 `supports' (only match frames that support the specified face attributes)
322 Should be a list of face attributes. See the documentation for
323 the function `display-supports-face-attributes-p' for more
324 information on exactly how testing is done.
326 Read the section about customization in the Emacs Lisp manual for more
327 information."
328 ;; It is better not to use backquote in this file,
329 ;; because that makes a bootstrapping problem
330 ;; if you need to recompile all the Lisp files using interpreted code.
331 (nconc (list 'custom-declare-face (list 'quote face) spec doc) args))
333 ;;; The `defgroup' Macro.
335 (defun custom-current-group ()
336 (cdr (assoc load-file-name custom-current-group-alist)))
338 (defun custom-declare-group (symbol members doc &rest args)
339 "Like `defgroup', but SYMBOL is evaluated as a normal argument."
340 (while members
341 (apply 'custom-add-to-group symbol (car members))
342 (setq members (cdr members)))
343 (when doc
344 ;; This text doesn't get into DOC.
345 (put symbol 'group-documentation (purecopy doc)))
346 (while args
347 (let ((arg (car args)))
348 (setq args (cdr args))
349 (unless (symbolp arg)
350 (error "Junk in args %S" args))
351 (let ((keyword arg)
352 (value (car args)))
353 (unless args
354 (error "Keyword %s is missing an argument" keyword))
355 (setq args (cdr args))
356 (cond ((eq keyword :prefix)
357 (put symbol 'custom-prefix value))
359 (custom-handle-keyword symbol keyword value
360 'custom-group))))))
361 ;; Record the group on the `current' list.
362 (let ((elt (assoc load-file-name custom-current-group-alist)))
363 (if elt (setcdr elt symbol)
364 (push (cons load-file-name symbol) custom-current-group-alist)))
365 (run-hooks 'custom-define-hook)
366 symbol)
368 (defmacro defgroup (symbol members doc &rest args)
369 "Declare SYMBOL as a customization group containing MEMBERS.
370 SYMBOL does not need to be quoted.
372 Third arg DOC is the group documentation.
374 MEMBERS should be an alist of the form ((NAME WIDGET)...) where
375 NAME is a symbol and WIDGET is a widget for editing that symbol.
376 Useful widgets are `custom-variable' for editing variables,
377 `custom-face' for edit faces, and `custom-group' for editing groups.
379 The remaining arguments should have the form
381 [KEYWORD VALUE]...
383 The following KEYWORDs are defined:
385 :group VALUE should be a customization group.
386 Add SYMBOL to that group.
388 :version VALUE should be a string specifying that the group was introduced
389 in Emacs version VERSION.
391 Read the section about customization in the Emacs Lisp manual for more
392 information."
393 ;; It is better not to use backquote in this file,
394 ;; because that makes a bootstrapping problem
395 ;; if you need to recompile all the Lisp files using interpreted code.
396 (nconc (list 'custom-declare-group (list 'quote symbol) members doc) args))
398 (defun custom-add-to-group (group option widget)
399 "To existing GROUP add a new OPTION of type WIDGET.
400 If there already is an entry for OPTION and WIDGET, nothing is done."
401 (let ((members (get group 'custom-group))
402 (entry (list option widget)))
403 (unless (member entry members)
404 (put group 'custom-group (nconc members (list entry))))))
406 (defun custom-group-of-mode (mode)
407 "Return the custom group corresponding to the major or minor MODE.
408 If no such group is found, return nil."
409 (or (get mode 'custom-mode-group)
410 (if (or (get mode 'custom-group)
411 (and (string-match "-mode\\'" (symbol-name mode))
412 (get (setq mode (intern (substring (symbol-name mode)
413 0 (match-beginning 0))))
414 'custom-group)))
415 mode)))
417 ;;; Properties.
419 (defun custom-handle-all-keywords (symbol args type)
420 "For customization option SYMBOL, handle keyword arguments ARGS.
421 Third argument TYPE is the custom option type."
422 (unless (memq :group args)
423 (custom-add-to-group (custom-current-group) symbol type))
424 (while args
425 (let ((arg (car args)))
426 (setq args (cdr args))
427 (unless (symbolp arg)
428 (error "Junk in args %S" args))
429 (let ((keyword arg)
430 (value (car args)))
431 (unless args
432 (error "Keyword %s is missing an argument" keyword))
433 (setq args (cdr args))
434 (custom-handle-keyword symbol keyword value type)))))
436 (defun custom-handle-keyword (symbol keyword value type)
437 "For customization option SYMBOL, handle KEYWORD with VALUE.
438 Fourth argument TYPE is the custom option type."
439 (if purify-flag
440 (setq value (purecopy value)))
441 (cond ((eq keyword :group)
442 (custom-add-to-group value symbol type))
443 ((eq keyword :version)
444 (custom-add-version symbol value))
445 ((eq keyword :link)
446 (custom-add-link symbol value))
447 ((eq keyword :load)
448 (custom-add-load symbol value))
449 ((eq keyword :tag)
450 (put symbol 'custom-tag value))
451 ((eq keyword :set-after)
452 (custom-add-dependencies symbol value))
454 (error "Unknown keyword %s" keyword))))
456 (defun custom-add-dependencies (symbol value)
457 "To the custom option SYMBOL, add dependencies specified by VALUE.
458 VALUE should be a list of symbols. For each symbol in that list,
459 this specifies that SYMBOL should be set after the specified symbol, if
460 both appear in constructs like `custom-set-variables'."
461 (unless (listp value)
462 (error "Invalid custom dependency `%s'" value))
463 (let* ((deps (get symbol 'custom-dependencies))
464 (new-deps deps))
465 (while value
466 (let ((dep (car value)))
467 (unless (symbolp dep)
468 (error "Invalid custom dependency `%s'" dep))
469 (unless (memq dep new-deps)
470 (setq new-deps (cons dep new-deps)))
471 (setq value (cdr value))))
472 (unless (eq deps new-deps)
473 (put symbol 'custom-dependencies new-deps))))
475 (defun custom-add-option (symbol option)
476 "To the variable SYMBOL add OPTION.
478 If SYMBOL is a hook variable, OPTION should be a hook member.
479 For other types variables, the effect is undefined."
480 (let ((options (get symbol 'custom-options)))
481 (unless (member option options)
482 (put symbol 'custom-options (cons option options)))))
484 (defun custom-add-link (symbol widget)
485 "To the custom option SYMBOL add the link WIDGET."
486 (let ((links (get symbol 'custom-links)))
487 (unless (member widget links)
488 (put symbol 'custom-links (cons (purecopy widget) links)))))
490 (defun custom-add-version (symbol version)
491 "To the custom option SYMBOL add the version VERSION."
492 (put symbol 'custom-version (purecopy version)))
494 (defun custom-add-load (symbol load)
495 "To the custom option SYMBOL add the dependency LOAD.
496 LOAD should be either a library file name, or a feature name."
497 (let ((loads (get symbol 'custom-loads)))
498 (unless (member load loads)
499 (put symbol 'custom-loads (cons (purecopy load) loads)))))
501 (defun custom-autoload (symbol load)
502 "Mark SYMBOL as autoloaded custom variable and add dependency LOAD."
503 (put symbol 'custom-autoload t)
504 (custom-add-load symbol load))
506 ;; This test is also in the C code of `user-variable-p'.
507 (defun custom-variable-p (variable)
508 "Return non-nil if VARIABLE is a custom variable."
509 (or (get variable 'standard-value)
510 (get variable 'custom-autoload)))
512 ;;; Loading files needed to customize a symbol.
513 ;;; This is in custom.el because menu-bar.el needs it for toggle cmds.
515 (defvar custom-load-recursion nil
516 "Hack to avoid recursive dependencies.")
518 (defun custom-load-symbol (symbol)
519 "Load all dependencies for SYMBOL."
520 (unless custom-load-recursion
521 (let ((custom-load-recursion t))
522 ;; Load these files if not already done,
523 ;; to make sure we know all the dependencies of SYMBOL.
524 (condition-case nil
525 (require 'cus-load)
526 (error nil))
527 (condition-case nil
528 (require 'cus-start)
529 (error nil))
530 (dolist (load (get symbol 'custom-loads))
531 (cond ((symbolp load) (condition-case nil (require load) (error nil)))
532 ;; This is subsumed by the test below, but it's much faster.
533 ((assoc load load-history))
534 ;; This was just (assoc (locate-library load) load-history)
535 ;; but has been optimized not to load locate-library
536 ;; if not necessary.
537 ((let ((regexp (concat "\\(\\`\\|/\\)" (regexp-quote load)
538 "\\(\\'\\|\\.\\)"))
539 (found nil))
540 (dolist (loaded load-history)
541 (and (stringp (car loaded))
542 (string-match regexp (car loaded))
543 (setq found t)))
544 found))
545 ;; Without this, we would load cus-edit recursively.
546 ;; We are still loading it when we call this,
547 ;; and it is not in load-history yet.
548 ((equal load "cus-edit"))
549 (t (condition-case nil (load load) (error nil))))))))
551 (defvar custom-known-themes '(user standard)
552 "Themes that have been define with `deftheme'.
553 The default value is the list (user standard). The theme `standard'
554 contains the Emacs standard settings from the original Lisp files. The
555 theme `user' contains all the the settings the user customized and saved.
556 Additional themes declared with the `deftheme' macro will be added to
557 the front of this list.")
559 (defun custom-declare-theme (theme feature &optional doc &rest args)
560 "Like `deftheme', but THEME is evaluated as a normal argument.
561 FEATURE is the feature this theme provides. This symbol is created
562 from THEME by `custom-make-theme-feature'."
563 (add-to-list 'custom-known-themes theme)
564 (put theme 'theme-feature feature)
565 (when doc
566 (put theme 'theme-documentation doc))
567 (while args
568 (let ((arg (car args)))
569 (setq args (cdr args))
570 (unless (symbolp arg)
571 (error "Junk in args %S" args))
572 (let ((keyword arg)
573 (value (car args)))
574 (unless args
575 (error "Keyword %s is missing an argument" keyword))
576 (setq args (cdr args))
577 (cond ((eq keyword :short-description)
578 (put theme 'theme-short-description value))
579 ((eq keyword :immediate)
580 (put theme 'theme-immediate value))
581 ((eq keyword :variable-set-string)
582 (put theme 'theme-variable-set-string value))
583 ((eq keyword :variable-reset-string)
584 (put theme 'theme-variable-reset-string value))
585 ((eq keyword :face-set-string)
586 (put theme 'theme-face-set-string value))
587 ((eq keyword :face-reset-string)
588 (put theme 'theme-face-reset-string value)))))))
590 (defmacro deftheme (theme &optional doc &rest args)
591 "Declare custom theme THEME.
592 The optional argument DOC is a doc string describing the theme.
593 The remaining arguments should have the form
595 [KEYWORD VALUE]...
597 The following KEYWORD's are defined:
599 :short-description
600 VALUE is a short (one line) description of the theme. If not
601 given, DOC is used.
602 :immediate
603 If VALUE is non-nil, variables specified in this theme are set
604 immediately when loading the theme.
605 :variable-set-string
606 VALUE is a string used to indicate that a variable takes its
607 setting from this theme. It is passed to FORMAT with the name
608 of the theme as an additional argument. If not given, a
609 generic description is used.
610 :variable-reset-string
611 VALUE is a string used in the case a variable has been forced
612 to its value in this theme. It is passed to FORMAT with the
613 name of the theme as an additional argument. If not given, a
614 generic description is used.
615 :face-set-string
616 VALUE is a string used to indicate that a face takes its
617 setting from this theme. It is passed to FORMAT with the name
618 of the theme as an additional argument. If not given, a
619 generic description is used.
620 :face-reset-string
621 VALUE is a string used in the case a face has been forced to
622 its value in this theme. It is passed to FORMAT with the name
623 of the theme as an additional argument. If not given, a
624 generic description is used.
626 Any theme `foo' should be defined in a file called `foo-theme.el';
627 see `custom-make-theme-feature' for more information."
628 (let ((feature (custom-make-theme-feature theme)))
629 ;; It is better not to use backquote in this file,
630 ;; because that makes a bootstrapping problem
631 ;; if you need to recompile all the Lisp files using interpreted code.
632 (nconc (list 'custom-declare-theme
633 (list 'quote theme)
634 (list 'quote feature)
635 doc) args)))
637 (defun custom-make-theme-feature (theme)
638 "Given a symbol THEME, create a new symbol by appending \"-theme\".
639 Store this symbol in the `theme-feature' property of THEME.
640 Calling `provide-theme' to provide THEME actually puts `THEME-theme'
641 into `features'.
643 This allows for a file-name convention for autoloading themes:
644 Every theme X has a property `provide-theme' whose value is \"X-theme\".
645 \(require-theme X) then attempts to load the file `X-theme.el'."
646 (intern (concat (symbol-name theme) "-theme")))
648 (defsubst custom-theme-p (theme)
649 "Non-nil when THEME has been defined."
650 (memq theme custom-known-themes))
652 (defsubst custom-check-theme (theme)
653 "Check whether THEME is valid, and signal an error if it is not."
654 (unless (custom-theme-p theme)
655 (error "Unknown theme `%s'" theme)))
657 ;;; Initializing.
659 (defun custom-push-theme (prop symbol theme mode value)
660 "Add (THEME MODE VALUE) to the list in property PROP of SYMBOL.
661 If the first element in that list is already (THEME ...),
662 discard it first.
664 MODE can be either the symbol `set' or the symbol `reset'. If it is the
665 symbol `set', then VALUE is the value to use. If it is the symbol
666 `reset', then VALUE is the mode to query instead.
668 In the following example for the variable `goto-address-url-face', the
669 theme `subtle-hacker' uses the same value for the variable as the theme
670 `gnome2':
672 \((standard set bold)
673 \(gnome2 set info-xref)
674 \(jonadab set underline)
675 \(subtle-hacker reset gnome2))
678 If a value has been stored for themes A B and C, and a new value
679 is to be stored for theme C, then the old value of C is discarded.
680 If a new value is to be stored for theme B, however, the old value
681 of B is not discarded because B is not the car of the list.
683 For variables, list property PROP is `theme-value'.
684 For faces, list property PROP is `theme-face'.
685 This is used in `custom-do-theme-reset', for example.
687 The list looks the same in any case; the examples shows a possible
688 value of the `theme-face' property for the face `region':
690 \((gnome2 set ((t (:foreground \"cyan\" :background \"dark cyan\"))))
691 \(standard set ((((class color) (background dark))
692 \(:background \"blue\"))
693 \(t (:background \"gray\")))))
695 This records values for the `standard' and the `gnome2' themes.
696 The user has not customized the face; had he done that,
697 the list would contain an entry for the `user' theme, too.
698 See `custom-known-themes' for a list of known themes."
699 (let ((old (get symbol prop)))
700 (if (eq (car-safe (car-safe old)) theme)
701 (setq old (cdr old)))
702 (put symbol prop (cons (list theme mode value) old))))
704 (defvar custom-local-buffer nil
705 "Non-nil, in a Customization buffer, means customize a specific buffer.
706 If this variable is non-nil, it should be a buffer,
707 and it means customize the local bindings of that buffer.
708 This variable is a permanent local, and it normally has a local binding
709 in every Customization buffer.")
710 (put 'custom-local-buffer 'permanent-local t)
712 (defun custom-set-variables (&rest args)
713 "Initialize variables according to user preferences.
714 The settings are registered as theme `user'.
715 The arguments should each be a list of the form:
717 (SYMBOL VALUE [NOW [REQUEST [COMMENT]]])
719 The unevaluated VALUE is stored as the saved value for SYMBOL.
720 If NOW is present and non-nil, VALUE is also evaluated and bound as
721 the default value for the SYMBOL.
723 REQUEST is a list of features we must 'require for SYMBOL.
724 COMMENT is a comment string about SYMBOL."
725 (apply 'custom-theme-set-variables 'user args))
727 (defun custom-theme-set-variables (theme &rest args)
728 "Initialize variables according to settings specified by args.
729 Records the settings as belonging to THEME.
731 The arguments should be a list where each entry has the form:
733 (SYMBOL VALUE [NOW [REQUEST [COMMENT]]])
735 The unevaluated VALUE is stored as the saved value for SYMBOL.
736 If NOW is present and non-nil, VALUE is also evaluated and bound as
737 the default value for the SYMBOL.
738 REQUEST is a list of features we must 'require for SYMBOL.
739 COMMENT is a comment string about SYMBOL.
741 Several properties of THEME and SYMBOL are used in the process:
743 If THEME property `theme-immediate' is non-nil, this is equivalent of
744 providing the NOW argument to all symbols in the argument list: SYMBOL
745 is bound to the evaluated VALUE. The only difference is SYMBOL property
746 `force-value': if NOW is non-nil, SYMBOL's property `force-value' is set to
747 the symbol `rogue', else if THEME's property `theme-immediate' is non-nil,
748 FACE's property `force-face' is set to the symbol `immediate'.
750 VALUE itself is saved unevaluated as SYMBOL property `saved-value' and
751 in SYMBOL's list property `theme-value' \(using `custom-push-theme')."
752 (custom-check-theme theme)
753 (let ((immediate (get theme 'theme-immediate)))
754 (setq args
755 (sort args
756 (lambda (a1 a2)
757 (let* ((sym1 (car a1))
758 (sym2 (car a2))
759 (1-then-2 (memq sym1 (get sym2 'custom-dependencies)))
760 (2-then-1 (memq sym2 (get sym1 'custom-dependencies))))
761 (cond ((and 1-then-2 2-then-1)
762 (error "Circular custom dependency between `%s' and `%s'"
763 sym1 sym2))
764 (2-then-1 nil)
765 ;; Put symbols with :require last. The macro
766 ;; define-minor-mode generates a defcustom
767 ;; with a :require and a :set, where the
768 ;; setter function calls the mode function.
769 ;; Putting symbols with :require last ensures
770 ;; that the mode function will see other
771 ;; customized values rather than default
772 ;; values.
773 (t (nth 3 a2)))))))
774 (while args
775 (let ((entry (car args)))
776 (if (listp entry)
777 (let* ((symbol (nth 0 entry))
778 (value (nth 1 entry))
779 (now (nth 2 entry))
780 (requests (nth 3 entry))
781 (comment (nth 4 entry))
782 set)
783 (when requests
784 (put symbol 'custom-requests requests)
785 (mapc 'require requests))
786 (setq set (or (get symbol 'custom-set) 'custom-set-default))
787 (put symbol 'saved-value (list value))
788 (put symbol 'saved-variable-comment comment)
789 (custom-push-theme 'theme-value symbol theme 'set value)
790 ;; Allow for errors in the case where the setter has
791 ;; changed between versions, say, but let the user know.
792 (condition-case data
793 (cond (now
794 ;; Rogue variable, set it now.
795 (put symbol 'force-value t)
796 (funcall set symbol (eval value)))
797 ((default-boundp symbol)
798 ;; Something already set this, overwrite it.
799 (funcall set symbol (eval value))))
800 (error
801 (message "Error setting %s: %s" symbol data)))
802 (setq args (cdr args))
803 (and (or now (default-boundp symbol))
804 (put symbol 'variable-comment comment)))
805 ;; Old format, a plist of SYMBOL VALUE pairs.
806 (message "Warning: old format `custom-set-variables'")
807 (ding)
808 (sit-for 2)
809 (let ((symbol (nth 0 args))
810 (value (nth 1 args)))
811 (put symbol 'saved-value (list value))
812 (custom-push-theme 'theme-value symbol theme 'set value))
813 (setq args (cdr (cdr args))))))))
815 (defun custom-set-default (variable value)
816 "Default :set function for a customizable variable.
817 Normally, this sets the default value of VARIABLE to VALUE,
818 but if `custom-local-buffer' is non-nil,
819 this sets the local binding in that buffer instead."
820 (if custom-local-buffer
821 (with-current-buffer custom-local-buffer
822 (set variable value))
823 (set-default variable value)))
825 (defun custom-set-minor-mode (variable value)
826 ":set function for minor mode variables.
827 Normally, this sets the default value of VARIABLE to nil if VALUE
828 is nil and to t otherwise,
829 but if `custom-local-buffer' is non-nil,
830 this sets the local binding in that buffer instead."
831 (if custom-local-buffer
832 (with-current-buffer custom-local-buffer
833 (funcall variable (or value 0)))
834 (funcall variable (or value 0))))
836 (defun custom-quote (sexp)
837 "Quote SEXP iff it is not self quoting."
838 (if (or (memq sexp '(t nil))
839 (keywordp sexp)
840 (and (listp sexp)
841 (memq (car sexp) '(lambda)))
842 (stringp sexp)
843 (numberp sexp)
844 (vectorp sexp)
845 ;;; (and (fboundp 'characterp)
846 ;;; (characterp sexp))
848 sexp
849 (list 'quote sexp)))
851 (defun customize-mark-to-save (symbol)
852 "Mark SYMBOL for later saving.
854 If the default value of SYMBOL is different from the standard value,
855 set the `saved-value' property to a list whose car evaluates to the
856 default value. Otherwise, set it to nil.
858 To actually save the value, call `custom-save-all'.
860 Return non-nil iff the `saved-value' property actually changed."
861 (let* ((get (or (get symbol 'custom-get) 'default-value))
862 (value (funcall get symbol))
863 (saved (get symbol 'saved-value))
864 (standard (get symbol 'standard-value))
865 (comment (get symbol 'customized-variable-comment)))
866 ;; Save default value iff different from standard value.
867 (if (or (null standard)
868 (not (equal value (condition-case nil
869 (eval (car standard))
870 (error nil)))))
871 (put symbol 'saved-value (list (custom-quote value)))
872 (put symbol 'saved-value nil))
873 ;; Clear customized information (set, but not saved).
874 (put symbol 'customized-value nil)
875 ;; Save any comment that might have been set.
876 (when comment
877 (put symbol 'saved-variable-comment comment))
878 (not (equal saved (get symbol 'saved-value)))))
880 (defun customize-mark-as-set (symbol)
881 "Mark current value of SYMBOL as being set from customize.
883 If the default value of SYMBOL is different from the saved value if any,
884 or else if it is different from the standard value, set the
885 `customized-value' property to a list whose car evaluates to the
886 default value. Otherwise, set it to nil.
888 Return non-nil iff the `customized-value' property actually changed."
889 (let* ((get (or (get symbol 'custom-get) 'default-value))
890 (value (funcall get symbol))
891 (customized (get symbol 'customized-value))
892 (old (or (get symbol 'saved-value) (get symbol 'standard-value))))
893 ;; Mark default value as set iff different from old value.
894 (if (or (null old)
895 (not (equal value (condition-case nil
896 (eval (car old))
897 (error nil)))))
898 (put symbol 'customized-value (list (custom-quote value)))
899 (put symbol 'customized-value nil))
900 ;; Changed?
901 (not (equal customized (get symbol 'customized-value)))))
903 ;;; Theme Manipulation
905 (defvar custom-loaded-themes nil
906 "Themes in the order they are loaded.")
908 (defun custom-theme-loaded-p (theme)
909 "Return non-nil when THEME has been loaded."
910 (memq theme custom-loaded-themes))
912 (defun provide-theme (theme)
913 "Indicate that this file provides THEME.
914 Add THEME to `custom-loaded-themes' and `provide' whatever
915 is stored in THEME's property `theme-feature'.
917 Usually the theme-feature property contains a symbol created
918 by `custom-make-theme-feature'."
919 (custom-check-theme theme)
920 (provide (get theme 'theme-feature))
921 (setq custom-loaded-themes (nconc (list theme) custom-loaded-themes)))
923 (defun require-theme (theme)
924 "Try to load a theme by requiring its feature.
925 THEME's feature is stored in THEME's `theme-feature' property.
927 Usually the `theme-feature' property contains a symbol created
928 by `custom-make-theme-feature'."
929 ;; Note we do no check for validity of the theme here.
930 ;; This allows to pull in themes by a file-name convention
931 (require (or (get theme 'theme-feature)
932 (custom-make-theme-feature theme))))
934 (defun custom-remove-theme (spec-alist theme)
935 "Delete all elements from SPEC-ALIST whose car is THEME."
936 (let ((elt (assoc theme spec-alist)))
937 (while elt
938 (setq spec-alist (delete elt spec-alist)
939 elt (assoc theme spec-alist))))
940 spec-alist)
942 (defun custom-do-theme-reset (theme)
943 "Undo all settings defined by THEME.
945 A variable remains unchanged if its property `theme-value' does not
946 contain a value for THEME. A face remains unchanged if its property
947 `theme-face' does not contain a value for THEME. In either case, all
948 settings for THEME are removed from the property and the variable or
949 face is set to the `user' theme.
951 See `custom-known-themes' for a list of known themes."
952 (let (spec-list)
953 (mapatoms (lambda (symbol)
954 ;; This works even if symbol is both a variable and a
955 ;; face.
956 (setq spec-list (get symbol 'theme-value))
957 (when spec-list
958 (put symbol 'theme-value (custom-remove-theme spec-list theme))
959 (custom-theme-reset-internal symbol 'user))
960 (setq spec-list (get symbol 'theme-face))
961 (when spec-list
962 (put symbol 'theme-face (custom-remove-theme spec-list theme))
963 (custom-theme-reset-internal-face symbol 'user))))))
965 (defun custom-theme-load-themes (by-theme &rest body)
966 "Load the themes specified by BODY.
967 Record them as required by theme BY-THEME. BODY is a sequence of either
969 THEME
970 BY-THEME requires THEME
971 \(reset THEME)
972 Undo all the settings made by THEME
973 \(hidden THEME)
974 Require THEME but hide it from the user
976 All the themes loaded for BY-THEME are recorded in BY-THEME's property
977 `theme-loads-themes'. Any theme loaded with the hidden predicate will
978 be given the property `theme-hidden' unless it has been loaded before.
979 Whether a theme has been loaded before is determined by the function
980 `custom-theme-loaded-p'."
981 (custom-check-theme by-theme)
982 (let ((theme)
983 (themes-loaded (get by-theme 'theme-loads-themes)))
984 (while theme
985 (setq theme (car body)
986 body (cdr body))
987 (cond ((and (consp theme) (eq (car theme) 'reset))
988 (custom-do-theme-reset (cadr theme)))
989 ((and (consp theme) (eq (car theme) 'hidden))
990 (require-theme (cadr theme))
991 (unless (custom-theme-loaded-p (cadr theme))
992 (put (cadr theme) 'theme-hidden t)))
994 (require-theme theme)
995 (put theme 'theme-hidden nil)))
996 (setq themes-loaded (nconc (list theme) themes-loaded)))
997 (put by-theme 'theme-loads-themes themes-loaded)))
999 (defun custom-load-themes (&rest body)
1000 "Load themes for the USER theme as specified by BODY.
1002 See `custom-theme-load-themes' for more information on BODY."
1003 (apply 'custom-theme-load-themes 'user body))
1005 ; (defsubst copy-upto-last (elt list)
1006 ; "Copy all the elements of the list upto the last occurence of elt"
1007 ; ;; Is it faster to do more work in C than to do less in elisp?
1008 ; (nreverse (cdr (member elt (reverse list)))))
1010 (defun custom-theme-value (theme theme-spec-list)
1011 "Determine the value for THEME defined by THEME-SPEC-LIST.
1012 Returns a list with the original value if found; nil otherwise.
1014 THEME-SPEC-LIST is an alist with themes as its key. As new themes are
1015 installed, these are added to the front of THEME-SPEC-LIST.
1016 Each element has the form
1018 \(THEME MODE VALUE)
1020 MODE is either the symbol `set' or the symbol `reset'. See
1021 `custom-push-theme' for more information on the format of
1022 THEME-SPEC-LIST."
1023 ;; Note we do _NOT_ signal an error if the theme is unknown
1024 ;; it might have gone away without the user knowing.
1025 (let ((value (cdr (assoc theme theme-spec-list))))
1026 (if value
1027 (if (eq (car value) 'set)
1028 (cdr value)
1029 (custom-theme-value (cadr value) theme-spec-list)))))
1031 (defun custom-theme-variable-value (variable theme)
1032 "Return (list value) indicating value of VARIABLE in THEME.
1033 If THEME does not define a value for VARIABLE, return nil. The value
1034 definitions per theme are stored in VARIABLE's property `theme-value'.
1035 The actual work is done by function `custom-theme-value', which see.
1036 See `custom-push-theme' for more information on how these definitions
1037 are stored."
1038 (custom-theme-value theme (get variable 'theme-value)))
1040 (defun custom-theme-reset-internal (symbol to-theme)
1041 "Reset SYMBOL to the value defined by TO-THEME.
1042 If SYMBOL is not defined in TO-THEME, reset SYMBOL to the standard
1043 value. See `custom-theme-variable-value'. The standard value is
1044 stored in SYMBOL's property `standard-value'."
1045 (let ((value (custom-theme-variable-value symbol to-theme))
1046 was-in-theme)
1047 (setq was-in-theme value)
1048 (setq value (or value (get symbol 'standard-value)))
1049 (when value
1050 (put symbol 'saved-value was-in-theme)
1051 (if (or (get 'force-value symbol) (default-boundp symbol))
1052 (funcall (or (get symbol 'custom-set) 'set-default) symbol
1053 (eval (car value)))))
1054 value))
1056 (defun custom-theme-reset-variables (theme &rest args)
1057 "Reset the value of the variables to values previously defined.
1058 Associate this setting with THEME.
1060 ARGS is a list of lists of the form
1062 (VARIABLE TO-THEME)
1064 This means reset VARIABLE to its value in TO-THEME."
1065 (custom-check-theme theme)
1066 (mapcar '(lambda (arg)
1067 (apply 'custom-theme-reset-internal arg)
1068 (custom-push-theme 'theme-value (car arg) theme 'reset (cadr arg)))
1069 args))
1071 (defun custom-reset-variables (&rest args)
1072 "Reset the value of the variables to values previously saved.
1073 This is the setting associated the `user' theme.
1075 ARGS is a list of lists of the form
1077 (VARIABLE TO-THEME)
1079 This means reset VARIABLE to its value in TO-THEME."
1080 (apply 'custom-theme-reset-variables 'user args))
1082 ;;; The End.
1084 ;; Process the defcustoms for variables loaded before this file.
1085 (while custom-declare-variable-list
1086 (apply 'custom-declare-variable (car custom-declare-variable-list))
1087 (setq custom-declare-variable-list (cdr custom-declare-variable-list)))
1089 (provide 'custom)
1091 ;;; arch-tag: 041b6116-aabe-4f9a-902d-74092bc3dab2
1092 ;;; custom.el ends here