(delphi): Finish `defgroup' description with period.
[emacs.git] / lisp / custom.el
blobcb4e76dd65a523e8de3fa51c423fe7ab45848078
1 ;;; custom.el --- tools for declaring and initializing options
2 ;;
3 ;; Copyright (C) 1996, 1997, 1999, 2001, 2002, 2004, 2005
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, this would require changing
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 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 In the first element, DISPLAY can be :default. The ATTS in that
291 element then act as defaults for all the following elements.
293 Aside from that, DISPLAY specifies conditions to match some or
294 all frames. For each frame, the first element of SPEC where the
295 DISPLAY conditions are satisfied is the one that applies to that
296 frame. The ATTRs in this element take effect, and the following
297 elements are ignored, on that frame.
299 In the last element, DISPLAY can be t. That element applies to a
300 frame if none of the previous elements (except the :default if
301 any) did.
303 ATTS is a list of face attributes followed by their values:
304 (ATTR VALUE ATTR VALUE...)
306 The possible attributes are `:family', `:width', `:height', `:weight',
307 `:slant', `:underline', `:overline', `:strike-through', `:box',
308 `:foreground', `:background', `:stipple', `:inverse-video', and `:inherit'.
310 DISPLAY can be `:default' (only in the first element), the symbol
311 t (only in the last element) to match all frames, or an alist of
312 conditions of the form \(REQ ITEM...). For such an alist to
313 match a frame, each of the conditions must be satisfied, meaning
314 that the REQ property of the frame must match one of the
315 corresponding ITEMs. These are the defined REQ values:
317 `type' (the value of `window-system')
318 Under X, in addition to the values `window-system' can take,
319 `motif', `lucid', `gtk' and `x-toolkit' are allowed, and match when
320 the Motif toolkit, Lucid toolkit, GTK toolkit or any X toolkit is in use.
322 `class' (the frame's color support)
323 Should be one of `color', `grayscale', or `mono'.
325 `background' (what color is used for the background text)
326 Should be one of `light' or `dark'.
328 `min-colors' (the minimum number of colors the frame should support)
329 Should be an integer, it is compared with the result of
330 `display-color-cells'.
332 `supports' (only match frames that support the specified face attributes)
333 Should be a list of face attributes. See the documentation for
334 the function `display-supports-face-attributes-p' for more
335 information on exactly how testing is done.
337 Read the section about customization in the Emacs Lisp manual for more
338 information."
339 ;; It is better not to use backquote in this file,
340 ;; because that makes a bootstrapping problem
341 ;; if you need to recompile all the Lisp files using interpreted code.
342 (nconc (list 'custom-declare-face (list 'quote face) spec doc) args))
344 ;;; The `defgroup' Macro.
346 (defun custom-current-group ()
347 (cdr (assoc load-file-name custom-current-group-alist)))
349 (defun custom-declare-group (symbol members doc &rest args)
350 "Like `defgroup', but SYMBOL is evaluated as a normal argument."
351 (while members
352 (apply 'custom-add-to-group symbol (car members))
353 (setq members (cdr members)))
354 (when doc
355 ;; This text doesn't get into DOC.
356 (put symbol 'group-documentation (purecopy doc)))
357 (while args
358 (let ((arg (car args)))
359 (setq args (cdr args))
360 (unless (symbolp arg)
361 (error "Junk in args %S" args))
362 (let ((keyword arg)
363 (value (car args)))
364 (unless args
365 (error "Keyword %s is missing an argument" keyword))
366 (setq args (cdr args))
367 (cond ((eq keyword :prefix)
368 (put symbol 'custom-prefix value))
370 (custom-handle-keyword symbol keyword value
371 'custom-group))))))
372 ;; Record the group on the `current' list.
373 (let ((elt (assoc load-file-name custom-current-group-alist)))
374 (if elt (setcdr elt symbol)
375 (push (cons load-file-name symbol) custom-current-group-alist)))
376 (run-hooks 'custom-define-hook)
377 symbol)
379 (defmacro defgroup (symbol members doc &rest args)
380 "Declare SYMBOL as a customization group containing MEMBERS.
381 SYMBOL does not need to be quoted.
383 Third arg DOC is the group documentation.
385 MEMBERS should be an alist of the form ((NAME WIDGET)...) where
386 NAME is a symbol and WIDGET is a widget for editing that symbol.
387 Useful widgets are `custom-variable' for editing variables,
388 `custom-face' for edit faces, and `custom-group' for editing groups.
390 The remaining arguments should have the form
392 [KEYWORD VALUE]...
394 The following KEYWORDs are defined:
396 :group VALUE should be a customization group.
397 Add SYMBOL to that group.
399 :version VALUE should be a string specifying that the group was introduced
400 in Emacs version VERSION.
402 Read the section about customization in the Emacs Lisp manual for more
403 information."
404 ;; It is better not to use backquote in this file,
405 ;; because that makes a bootstrapping problem
406 ;; if you need to recompile all the Lisp files using interpreted code.
407 (nconc (list 'custom-declare-group (list 'quote symbol) members doc) args))
409 (defun custom-add-to-group (group option widget)
410 "To existing GROUP add a new OPTION of type WIDGET.
411 If there already is an entry for OPTION and WIDGET, nothing is done."
412 (let ((members (get group 'custom-group))
413 (entry (list option widget)))
414 (unless (member entry members)
415 (put group 'custom-group (nconc members (list entry))))))
417 (defun custom-group-of-mode (mode)
418 "Return the custom group corresponding to the major or minor MODE.
419 If no such group is found, return nil."
420 (or (get mode 'custom-mode-group)
421 (if (or (get mode 'custom-group)
422 (and (string-match "-mode\\'" (symbol-name mode))
423 (get (setq mode (intern (substring (symbol-name mode)
424 0 (match-beginning 0))))
425 'custom-group)))
426 mode)))
428 ;;; Properties.
430 (defun custom-handle-all-keywords (symbol args type)
431 "For customization option SYMBOL, handle keyword arguments ARGS.
432 Third argument TYPE is the custom option type."
433 (unless (memq :group args)
434 (custom-add-to-group (custom-current-group) symbol type))
435 (while args
436 (let ((arg (car args)))
437 (setq args (cdr args))
438 (unless (symbolp arg)
439 (error "Junk in args %S" args))
440 (let ((keyword arg)
441 (value (car args)))
442 (unless args
443 (error "Keyword %s is missing an argument" keyword))
444 (setq args (cdr args))
445 (custom-handle-keyword symbol keyword value type)))))
447 (defun custom-handle-keyword (symbol keyword value type)
448 "For customization option SYMBOL, handle KEYWORD with VALUE.
449 Fourth argument TYPE is the custom option type."
450 (if purify-flag
451 (setq value (purecopy value)))
452 (cond ((eq keyword :group)
453 (custom-add-to-group value symbol type))
454 ((eq keyword :version)
455 (custom-add-version symbol value))
456 ((eq keyword :link)
457 (custom-add-link symbol value))
458 ((eq keyword :load)
459 (custom-add-load symbol value))
460 ((eq keyword :tag)
461 (put symbol 'custom-tag value))
462 ((eq keyword :set-after)
463 (custom-add-dependencies symbol value))
465 (error "Unknown keyword %s" keyword))))
467 (defun custom-add-dependencies (symbol value)
468 "To the custom option SYMBOL, add dependencies specified by VALUE.
469 VALUE should be a list of symbols. For each symbol in that list,
470 this specifies that SYMBOL should be set after the specified symbol, if
471 both appear in constructs like `custom-set-variables'."
472 (unless (listp value)
473 (error "Invalid custom dependency `%s'" value))
474 (let* ((deps (get symbol 'custom-dependencies))
475 (new-deps deps))
476 (while value
477 (let ((dep (car value)))
478 (unless (symbolp dep)
479 (error "Invalid custom dependency `%s'" dep))
480 (unless (memq dep new-deps)
481 (setq new-deps (cons dep new-deps)))
482 (setq value (cdr value))))
483 (unless (eq deps new-deps)
484 (put symbol 'custom-dependencies new-deps))))
486 (defun custom-add-option (symbol option)
487 "To the variable SYMBOL add OPTION.
489 If SYMBOL's custom type is a hook, OPTION should be a hook member.
490 If SYMBOL's custom type is an alist, OPTION specifies a symbol
491 to offer to the user as a possible key in the alist.
492 For other custom types, this has no effect."
493 (let ((options (get symbol 'custom-options)))
494 (unless (member option options)
495 (put symbol 'custom-options (cons option options)))))
497 (defun custom-add-link (symbol widget)
498 "To the custom option SYMBOL add the link WIDGET."
499 (let ((links (get symbol 'custom-links)))
500 (unless (member widget links)
501 (put symbol 'custom-links (cons (purecopy widget) links)))))
503 (defun custom-add-version (symbol version)
504 "To the custom option SYMBOL add the version VERSION."
505 (put symbol 'custom-version (purecopy version)))
507 (defun custom-add-load (symbol load)
508 "To the custom option SYMBOL add the dependency LOAD.
509 LOAD should be either a library file name, or a feature name."
510 (let ((loads (get symbol 'custom-loads)))
511 (unless (member load loads)
512 (put symbol 'custom-loads (cons (purecopy load) loads)))))
514 (defun custom-autoload (symbol load)
515 "Mark SYMBOL as autoloaded custom variable and add dependency LOAD."
516 (put symbol 'custom-autoload t)
517 (custom-add-load symbol load))
519 ;; This test is also in the C code of `user-variable-p'.
520 (defun custom-variable-p (variable)
521 "Return non-nil if VARIABLE is a custom variable."
522 (or (get variable 'standard-value)
523 (get variable 'custom-autoload)))
525 ;;; Loading files needed to customize a symbol.
526 ;;; This is in custom.el because menu-bar.el needs it for toggle cmds.
528 (defvar custom-load-recursion nil
529 "Hack to avoid recursive dependencies.")
531 (defun custom-load-symbol (symbol)
532 "Load all dependencies for SYMBOL."
533 (unless custom-load-recursion
534 (let ((custom-load-recursion t))
535 ;; Load these files if not already done,
536 ;; to make sure we know all the dependencies of SYMBOL.
537 (condition-case nil
538 (require 'cus-load)
539 (error nil))
540 (condition-case nil
541 (require 'cus-start)
542 (error nil))
543 (dolist (load (get symbol 'custom-loads))
544 (cond ((symbolp load) (condition-case nil (require load) (error nil)))
545 ;; This is subsumed by the test below, but it's much faster.
546 ((assoc load load-history))
547 ;; This was just (assoc (locate-library load) load-history)
548 ;; but has been optimized not to load locate-library
549 ;; if not necessary.
550 ((let ((regexp (concat "\\(\\`\\|/\\)" (regexp-quote load)
551 "\\(\\'\\|\\.\\)"))
552 (found nil))
553 (dolist (loaded load-history)
554 (and (stringp (car loaded))
555 (string-match regexp (car loaded))
556 (setq found t)))
557 found))
558 ;; Without this, we would load cus-edit recursively.
559 ;; We are still loading it when we call this,
560 ;; and it is not in load-history yet.
561 ((equal load "cus-edit"))
562 (t (condition-case nil (load load) (error nil))))))))
564 (defvar custom-known-themes '(user standard)
565 "Themes that have been defined with `deftheme'.
566 The default value is the list (user standard). The theme `standard'
567 contains the Emacs standard settings from the original Lisp files. The
568 theme `user' contains all the the settings the user customized and saved.
569 Additional themes declared with the `deftheme' macro will be added to
570 the front of this list.")
572 (defun custom-declare-theme (theme feature &optional doc &rest args)
573 "Like `deftheme', but THEME is evaluated as a normal argument.
574 FEATURE is the feature this theme provides. This symbol is created
575 from THEME by `custom-make-theme-feature'."
576 (add-to-list 'custom-known-themes theme)
577 (put theme 'theme-feature feature)
578 (when doc
579 (put theme 'theme-documentation doc))
580 (while args
581 (let ((arg (car args)))
582 (setq args (cdr args))
583 (unless (symbolp arg)
584 (error "Junk in args %S" args))
585 (let ((keyword arg)
586 (value (car args)))
587 (unless args
588 (error "Keyword %s is missing an argument" keyword))
589 (setq args (cdr args))
590 (cond ((eq keyword :short-description)
591 (put theme 'theme-short-description value))
592 ((eq keyword :immediate)
593 (put theme 'theme-immediate value))
594 ((eq keyword :variable-set-string)
595 (put theme 'theme-variable-set-string value))
596 ((eq keyword :variable-reset-string)
597 (put theme 'theme-variable-reset-string value))
598 ((eq keyword :face-set-string)
599 (put theme 'theme-face-set-string value))
600 ((eq keyword :face-reset-string)
601 (put theme 'theme-face-reset-string value)))))))
603 (defmacro deftheme (theme &optional doc &rest args)
604 "Declare custom theme THEME.
605 The optional argument DOC is a doc string describing the theme.
606 The remaining arguments should have the form
608 [KEYWORD VALUE]...
610 The following KEYWORD's are defined:
612 :short-description
613 VALUE is a short (one line) description of the theme. If not
614 given, DOC is used.
615 :immediate
616 If VALUE is non-nil, variables specified in this theme are set
617 immediately when loading the theme.
618 :variable-set-string
619 VALUE is a string used to indicate that a variable takes its
620 setting from this theme. It is passed to FORMAT with the name
621 of the theme as an additional argument. If not given, a
622 generic description is used.
623 :variable-reset-string
624 VALUE is a string used in the case a variable has been forced
625 to its value in this theme. It is passed to FORMAT with the
626 name of the theme as an additional argument. If not given, a
627 generic description is used.
628 :face-set-string
629 VALUE is a string used to indicate that a face takes its
630 setting from this theme. It is passed to FORMAT with the name
631 of the theme as an additional argument. If not given, a
632 generic description is used.
633 :face-reset-string
634 VALUE is a string used in the case a face has been forced to
635 its value in this theme. It is passed to FORMAT with the name
636 of the theme as an additional argument. If not given, a
637 generic description is used.
639 Any theme `foo' should be defined in a file called `foo-theme.el';
640 see `custom-make-theme-feature' for more information."
641 (let ((feature (custom-make-theme-feature theme)))
642 ;; It is better not to use backquote in this file,
643 ;; because that makes a bootstrapping problem
644 ;; if you need to recompile all the Lisp files using interpreted code.
645 (nconc (list 'custom-declare-theme
646 (list 'quote theme)
647 (list 'quote feature)
648 doc) args)))
650 (defun custom-make-theme-feature (theme)
651 "Given a symbol THEME, create a new symbol by appending \"-theme\".
652 Store this symbol in the `theme-feature' property of THEME.
653 Calling `provide-theme' to provide THEME actually puts `THEME-theme'
654 into `features'.
656 This allows for a file-name convention for autoloading themes:
657 Every theme X has a property `provide-theme' whose value is \"X-theme\".
658 \(require-theme X) then attempts to load the file `X-theme.el'."
659 (intern (concat (symbol-name theme) "-theme")))
661 (defsubst custom-theme-p (theme)
662 "Non-nil when THEME has been defined."
663 (memq theme custom-known-themes))
665 (defsubst custom-check-theme (theme)
666 "Check whether THEME is valid, and signal an error if it is not."
667 (unless (custom-theme-p theme)
668 (error "Unknown theme `%s'" theme)))
670 ;;; Initializing.
672 (defun custom-push-theme (prop symbol theme mode value)
673 "Add (THEME MODE VALUE) to the list in property PROP of SYMBOL.
674 If the first element in that list is already (THEME ...),
675 discard it first.
677 MODE can be either the symbol `set' or the symbol `reset'. If it is the
678 symbol `set', then VALUE is the value to use. If it is the symbol
679 `reset', then VALUE is the mode to query instead.
681 In the following example for the variable `goto-address-url-face', the
682 theme `subtle-hacker' uses the same value for the variable as the theme
683 `gnome2':
685 \((standard set bold)
686 \(gnome2 set info-xref)
687 \(jonadab set underline)
688 \(subtle-hacker reset gnome2))
691 If a value has been stored for themes A B and C, and a new value
692 is to be stored for theme C, then the old value of C is discarded.
693 If a new value is to be stored for theme B, however, the old value
694 of B is not discarded because B is not the car of the list.
696 For variables, list property PROP is `theme-value'.
697 For faces, list property PROP is `theme-face'.
698 This is used in `custom-do-theme-reset', for example.
700 The list looks the same in any case; the examples shows a possible
701 value of the `theme-face' property for the face `region':
703 \((gnome2 set ((t (:foreground \"cyan\" :background \"dark cyan\"))))
704 \(standard set ((((class color) (background dark))
705 \(:background \"blue\"))
706 \(t (:background \"gray\")))))
708 This records values for the `standard' and the `gnome2' themes.
709 The user has not customized the face; had he done that,
710 the list would contain an entry for the `user' theme, too.
711 See `custom-known-themes' for a list of known themes."
712 (let ((old (get symbol prop)))
713 (if (eq (car-safe (car-safe old)) theme)
714 (setq old (cdr old)))
715 (put symbol prop (cons (list theme mode value) old))))
717 (defvar custom-local-buffer nil
718 "Non-nil, in a Customization buffer, means customize a specific buffer.
719 If this variable is non-nil, it should be a buffer,
720 and it means customize the local bindings of that buffer.
721 This variable is a permanent local, and it normally has a local binding
722 in every Customization buffer.")
723 (put 'custom-local-buffer 'permanent-local t)
725 (defun custom-set-variables (&rest args)
726 "Install user customizations of variable values specified in ARGS.
727 These settings are registered as theme `user'.
728 The arguments should each be a list of the form:
730 (SYMBOL EXP [NOW [REQUEST [COMMENT]]])
732 This stores EXP (without evaluating it) as the saved value for SYMBOL.
733 If NOW is present and non-nil, then also evaluate EXP and set
734 the default value for the SYMBOL to the value of EXP.
736 REQUEST is a list of features we must require in order to
737 handle SYMBOL properly.
738 COMMENT is a comment string about SYMBOL."
739 (apply 'custom-theme-set-variables 'user args))
741 (defun custom-reevaluate-setting (symbol)
742 "Reset the value of SYMBOL by re-evaluating its saved or default value.
743 This is useful for variables that are defined before their default value
744 can really be computed. E.g. dumped variables whose default depends on
745 run-time information."
746 (funcall (or (get symbol 'custom-set) 'set-default)
747 symbol
748 (eval (car (or (get symbol 'saved-value) (get symbol 'standard-value))))))
750 (defun custom-theme-set-variables (theme &rest args)
751 "Initialize variables for theme THEME according to settings in ARGS.
752 Each of the arguments in ARGS should be a list of this form:
754 (SYMBOL EXP [NOW [REQUEST [COMMENT]]])
756 This stores EXP (without evaluating it) as the saved value for SYMBOL.
757 If NOW is present and non-nil, then also evaluate EXP and set
758 the default value for the SYMBOL to the value of EXP.
760 REQUEST is a list of features we must require in order to
761 handle SYMBOL properly.
762 COMMENT is a comment string about SYMBOL.
764 Several properties of THEME and SYMBOL are used in the process:
766 If THEME's property `theme-immediate' is non-nil, this is equivalent of
767 providing the NOW argument to all symbols in the argument list:
768 evaluate each EXP and set the corresponding SYMBOL. However,
769 there's a difference in the handling of SYMBOL's property
770 `force-value': if NOW is non-nil, SYMBOL's property `force-value' is set to
771 the symbol `rogue', else if THEME's property `theme-immediate' is non-nil,
772 SYMBOL's property `force-value' is set to the symbol `immediate'.
774 EXP itself is saved unevaluated as SYMBOL property `saved-value' and
775 in SYMBOL's list property `theme-value' \(using `custom-push-theme')."
776 (custom-check-theme theme)
777 (setq args
778 (sort args
779 (lambda (a1 a2)
780 (let* ((sym1 (car a1))
781 (sym2 (car a2))
782 (1-then-2 (memq sym1 (get sym2 'custom-dependencies)))
783 (2-then-1 (memq sym2 (get sym1 'custom-dependencies))))
784 (cond ((and 1-then-2 2-then-1)
785 (error "Circular custom dependency between `%s' and `%s'"
786 sym1 sym2))
787 (2-then-1 nil)
788 ;; Put symbols with :require last. The macro
789 ;; define-minor-mode generates a defcustom
790 ;; with a :require and a :set, where the
791 ;; setter function calls the mode function.
792 ;; Putting symbols with :require last ensures
793 ;; that the mode function will see other
794 ;; customized values rather than default
795 ;; values.
796 (t (nth 3 a2)))))))
797 (while args
798 (let ((entry (car args)))
799 (if (listp entry)
800 (let* ((symbol (indirect-variable (nth 0 entry)))
801 (value (nth 1 entry))
802 (now (nth 2 entry))
803 (requests (nth 3 entry))
804 (comment (nth 4 entry))
805 set)
806 (when requests
807 (put symbol 'custom-requests requests)
808 (mapc 'require requests))
809 (setq set (or (get symbol 'custom-set) 'custom-set-default))
810 (put symbol 'saved-value (list value))
811 (put symbol 'saved-variable-comment comment)
812 (custom-push-theme 'theme-value symbol theme 'set value)
813 ;; Allow for errors in the case where the setter has
814 ;; changed between versions, say, but let the user know.
815 (condition-case data
816 (cond (now
817 ;; Rogue variable, set it now.
818 (put symbol 'force-value t)
819 (funcall set symbol (eval value)))
820 ((default-boundp symbol)
821 ;; Something already set this, overwrite it.
822 (funcall set symbol (eval value))))
823 (error
824 (message "Error setting %s: %s" symbol data)))
825 (setq args (cdr args))
826 (and (or now (default-boundp symbol))
827 (put symbol 'variable-comment comment)))
828 ;; Old format, a plist of SYMBOL VALUE pairs.
829 (message "Warning: old format `custom-set-variables'")
830 (ding)
831 (sit-for 2)
832 (let ((symbol (indirect-variable (nth 0 args)))
833 (value (nth 1 args)))
834 (put symbol 'saved-value (list value))
835 (custom-push-theme 'theme-value symbol theme 'set value))
836 (setq args (cdr (cdr args)))))))
838 (defun custom-set-default (variable value)
839 "Default :set function for a customizable variable.
840 Normally, this sets the default value of VARIABLE to VALUE,
841 but if `custom-local-buffer' is non-nil,
842 this sets the local binding in that buffer instead."
843 (if custom-local-buffer
844 (with-current-buffer custom-local-buffer
845 (set variable value))
846 (set-default variable value)))
848 (defun custom-set-minor-mode (variable value)
849 ":set function for minor mode variables.
850 Normally, this sets the default value of VARIABLE to nil if VALUE
851 is nil and to t otherwise,
852 but if `custom-local-buffer' is non-nil,
853 this sets the local binding in that buffer instead."
854 (if custom-local-buffer
855 (with-current-buffer custom-local-buffer
856 (funcall variable (if value 1 0)))
857 (funcall variable (if value 1 0))))
859 (defun custom-quote (sexp)
860 "Quote SEXP iff it is not self quoting."
861 (if (or (memq sexp '(t nil))
862 (keywordp sexp)
863 (and (listp sexp)
864 (memq (car sexp) '(lambda)))
865 (stringp sexp)
866 (numberp sexp)
867 (vectorp sexp)
868 ;;; (and (fboundp 'characterp)
869 ;;; (characterp sexp))
871 sexp
872 (list 'quote sexp)))
874 (defun customize-mark-to-save (symbol)
875 "Mark SYMBOL for later saving.
877 If the default value of SYMBOL is different from the standard value,
878 set the `saved-value' property to a list whose car evaluates to the
879 default value. Otherwise, set it to nil.
881 To actually save the value, call `custom-save-all'.
883 Return non-nil iff the `saved-value' property actually changed."
884 (let* ((get (or (get symbol 'custom-get) 'default-value))
885 (value (funcall get symbol))
886 (saved (get symbol 'saved-value))
887 (standard (get symbol 'standard-value))
888 (comment (get symbol 'customized-variable-comment)))
889 ;; Save default value iff different from standard value.
890 (if (or (null standard)
891 (not (equal value (condition-case nil
892 (eval (car standard))
893 (error nil)))))
894 (put symbol 'saved-value (list (custom-quote value)))
895 (put symbol 'saved-value nil))
896 ;; Clear customized information (set, but not saved).
897 (put symbol 'customized-value nil)
898 ;; Save any comment that might have been set.
899 (when comment
900 (put symbol 'saved-variable-comment comment))
901 (not (equal saved (get symbol 'saved-value)))))
903 (defun customize-mark-as-set (symbol)
904 "Mark current value of SYMBOL as being set from customize.
906 If the default value of SYMBOL is different from the saved value if any,
907 or else if it is different from the standard value, set the
908 `customized-value' property to a list whose car evaluates to the
909 default value. Otherwise, set it to nil.
911 Return non-nil iff the `customized-value' property actually changed."
912 (let* ((get (or (get symbol 'custom-get) 'default-value))
913 (value (funcall get symbol))
914 (customized (get symbol 'customized-value))
915 (old (or (get symbol 'saved-value) (get symbol 'standard-value))))
916 ;; Mark default value as set iff different from old value.
917 (if (or (null old)
918 (not (equal value (condition-case nil
919 (eval (car old))
920 (error nil)))))
921 (put symbol 'customized-value (list (custom-quote value)))
922 (put symbol 'customized-value nil))
923 ;; Changed?
924 (not (equal customized (get symbol 'customized-value)))))
926 ;;; Theme Manipulation
928 (defvar custom-loaded-themes nil
929 "Themes in the order they are loaded.")
931 (defcustom custom-theme-directory
932 (if (eq system-type 'ms-dos)
933 ;; MS-DOS cannot have initial dot.
934 "~/_emacs.d/"
935 "~/.emacs.d/")
936 "Directory in which Custom theme files should be written.
937 `require-theme' searches this directory in addition to load-path.
938 The command `customize-create-theme' writes the files it produces
939 into this directory."
940 :type 'string
941 :group 'customize
942 :version "22.1")
944 (defun custom-theme-loaded-p (theme)
945 "Return non-nil when THEME has been loaded."
946 (memq theme custom-loaded-themes))
948 (defun provide-theme (theme)
949 "Indicate that this file provides THEME.
950 Add THEME to `custom-loaded-themes' and `provide' whatever
951 is stored in THEME's property `theme-feature'.
953 Usually the theme-feature property contains a symbol created
954 by `custom-make-theme-feature'."
955 (custom-check-theme theme)
956 (provide (get theme 'theme-feature))
957 (setq custom-loaded-themes (nconc (list theme) custom-loaded-themes)))
959 (defun require-theme (theme)
960 "Try to load a theme by requiring its feature.
961 THEME's feature is stored in THEME's `theme-feature' property.
963 Usually the `theme-feature' property contains a symbol created
964 by `custom-make-theme-feature'."
965 ;; Note we do no check for validity of the theme here.
966 ;; This allows to pull in themes by a file-name convention
967 (let ((load-path (if (file-directory-p custom-theme-directory)
968 (cons custom-theme-directory load-path)
969 load-path)))
970 (require (or (get theme 'theme-feature)
971 (custom-make-theme-feature theme)))))
973 (defun custom-remove-theme (spec-alist theme)
974 "Delete all elements from SPEC-ALIST whose car is THEME."
975 (let ((elt (assoc theme spec-alist)))
976 (while elt
977 (setq spec-alist (delete elt spec-alist)
978 elt (assoc theme spec-alist))))
979 spec-alist)
981 (defun custom-do-theme-reset (theme)
982 "Undo all settings defined by THEME.
984 A variable remains unchanged if its property `theme-value' does not
985 contain a value for THEME. A face remains unchanged if its property
986 `theme-face' does not contain a value for THEME. In either case, all
987 settings for THEME are removed from the property and the variable or
988 face is set to the `user' theme.
990 See `custom-known-themes' for a list of known themes."
991 (let (spec-list)
992 (mapatoms (lambda (symbol)
993 ;; This works even if symbol is both a variable and a
994 ;; face.
995 (setq spec-list (get symbol 'theme-value))
996 (when spec-list
997 (put symbol 'theme-value (custom-remove-theme spec-list theme))
998 (custom-theme-reset-internal symbol 'user))
999 (setq spec-list (get symbol 'theme-face))
1000 (when spec-list
1001 (put symbol 'theme-face (custom-remove-theme spec-list theme))
1002 (custom-theme-reset-internal-face symbol 'user))))))
1004 (defun custom-theme-load-themes (by-theme &rest body)
1005 "Load the themes specified by BODY.
1006 Record them as required by theme BY-THEME. BODY is a sequence of either
1008 THEME
1009 BY-THEME requires THEME
1010 \(reset THEME)
1011 Undo all the settings made by THEME
1012 \(hidden THEME)
1013 Require THEME but hide it from the user
1015 All the themes loaded for BY-THEME are recorded in BY-THEME's property
1016 `theme-loads-themes'. Any theme loaded with the hidden predicate will
1017 be given the property `theme-hidden' unless it has been loaded before.
1018 Whether a theme has been loaded before is determined by the function
1019 `custom-theme-loaded-p'."
1020 (custom-check-theme by-theme)
1021 (let ((theme)
1022 (themes-loaded (get by-theme 'theme-loads-themes)))
1023 (while theme
1024 (setq theme (car body)
1025 body (cdr body))
1026 (cond ((and (consp theme) (eq (car theme) 'reset))
1027 (custom-do-theme-reset (cadr theme)))
1028 ((and (consp theme) (eq (car theme) 'hidden))
1029 (require-theme (cadr theme))
1030 (unless (custom-theme-loaded-p (cadr theme))
1031 (put (cadr theme) 'theme-hidden t)))
1033 (require-theme theme)
1034 (put theme 'theme-hidden nil)))
1035 (setq themes-loaded (nconc (list theme) themes-loaded)))
1036 (put by-theme 'theme-loads-themes themes-loaded)))
1038 (defun custom-load-themes (&rest body)
1039 "Load themes for the USER theme as specified by BODY.
1041 See `custom-theme-load-themes' for more information on BODY."
1042 (apply 'custom-theme-load-themes 'user body))
1044 ; (defsubst copy-upto-last (elt list)
1045 ; "Copy all the elements of the list upto the last occurence of elt"
1046 ; ;; Is it faster to do more work in C than to do less in elisp?
1047 ; (nreverse (cdr (member elt (reverse list)))))
1049 (defun custom-theme-value (theme theme-spec-list)
1050 "Determine the value for THEME defined by THEME-SPEC-LIST.
1051 Returns a list with the original value if found; nil otherwise.
1053 THEME-SPEC-LIST is an alist with themes as its key. As new themes are
1054 installed, these are added to the front of THEME-SPEC-LIST.
1055 Each element has the form
1057 \(THEME MODE VALUE)
1059 MODE is either the symbol `set' or the symbol `reset'. See
1060 `custom-push-theme' for more information on the format of
1061 THEME-SPEC-LIST."
1062 ;; Note we do _NOT_ signal an error if the theme is unknown
1063 ;; it might have gone away without the user knowing.
1064 (let ((value (cdr (assoc theme theme-spec-list))))
1065 (if value
1066 (if (eq (car value) 'set)
1067 (cdr value)
1068 (custom-theme-value (cadr value) theme-spec-list)))))
1070 (defun custom-theme-variable-value (variable theme)
1071 "Return (list value) indicating value of VARIABLE in THEME.
1072 If THEME does not define a value for VARIABLE, return nil. The value
1073 definitions per theme are stored in VARIABLE's property `theme-value'.
1074 The actual work is done by function `custom-theme-value', which see.
1075 See `custom-push-theme' for more information on how these definitions
1076 are stored."
1077 (custom-theme-value theme (get variable 'theme-value)))
1079 (defun custom-theme-reset-internal (symbol to-theme)
1080 "Reset SYMBOL to the value defined by TO-THEME.
1081 If SYMBOL is not defined in TO-THEME, reset SYMBOL to the standard
1082 value. See `custom-theme-variable-value'. The standard value is
1083 stored in SYMBOL's property `standard-value'."
1084 (let ((value (custom-theme-variable-value symbol to-theme))
1085 was-in-theme)
1086 (setq was-in-theme value)
1087 (setq value (or value (get symbol 'standard-value)))
1088 (when value
1089 (put symbol 'saved-value was-in-theme)
1090 (if (or (get 'force-value symbol) (default-boundp symbol))
1091 (funcall (or (get symbol 'custom-set) 'set-default) symbol
1092 (eval (car value)))))
1093 value))
1095 (defun custom-theme-reset-variables (theme &rest args)
1096 "Reset the value of the variables to values previously defined.
1097 Associate this setting with THEME.
1099 ARGS is a list of lists of the form
1101 (VARIABLE TO-THEME)
1103 This means reset VARIABLE to its value in TO-THEME."
1104 (custom-check-theme theme)
1105 (mapcar '(lambda (arg)
1106 (apply 'custom-theme-reset-internal arg)
1107 (custom-push-theme 'theme-value (car arg) theme 'reset (cadr arg)))
1108 args))
1110 (defun custom-reset-variables (&rest args)
1111 "Reset the value of the variables to values previously saved.
1112 This is the setting associated the `user' theme.
1114 ARGS is a list of lists of the form
1116 (VARIABLE TO-THEME)
1118 This means reset VARIABLE to its value in TO-THEME."
1119 (apply 'custom-theme-reset-variables 'user args))
1121 ;;; The End.
1123 ;; Process the defcustoms for variables loaded before this file.
1124 (while custom-declare-variable-list
1125 (apply 'custom-declare-variable (car custom-declare-variable-list))
1126 (setq custom-declare-variable-list (cdr custom-declare-variable-list)))
1128 (provide 'custom)
1130 ;; arch-tag: 041b6116-aabe-4f9a-902d-74092bc3dab2
1131 ;;; custom.el ends here