* lispref/modes.texi (Region to Refontify): Rename from "Region to Fontify".
[emacs.git] / lisp / custom.el
blob9a87bf68ac22c87bb749a03404456f4ff6fdc651
1 ;;; custom.el --- tools for declaring and initializing options
2 ;;
3 ;; Copyright (C) 1996, 1997, 1999, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009, 2010, 2011 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 3 of the License, or
15 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; This file only contains the code needed to declare and initialize
28 ;; user options. The code to customize options is autoloaded from
29 ;; `cus-edit.el' and is documented in the Emacs Lisp Reference manual.
31 ;; The code implementing face declarations is in `cus-face.el'.
33 ;;; Code:
35 (require 'widget)
37 (defvar custom-define-hook nil
38 ;; Customize information for this option is in `cus-edit.el'.
39 "Hook called after defining each customize option.")
41 (defvar custom-dont-initialize nil
42 "Non-nil means `defcustom' should not initialize the variable.
43 That is used for the sake of `custom-make-dependencies'.
44 Users should not set it.")
46 (defvar custom-current-group-alist nil
47 "Alist of (FILE . GROUP) indicating the current group to use for FILE.")
49 ;;; The `defcustom' Macro.
51 (defun custom-initialize-default (symbol value)
52 "Initialize SYMBOL with VALUE.
53 This will do nothing if symbol already has a default binding.
54 Otherwise, if symbol has a `saved-value' property, it will evaluate
55 the car of that and use it as the default binding for symbol.
56 Otherwise, VALUE will be evaluated and used as the default binding for
57 symbol."
58 (unless (default-boundp symbol)
59 ;; Use the saved value if it exists, otherwise the standard setting.
60 (set-default symbol (eval (if (get symbol 'saved-value)
61 (car (get symbol 'saved-value))
62 value)))))
64 (defun custom-initialize-set (symbol value)
65 "Initialize SYMBOL based on VALUE.
66 If the symbol doesn't have a default binding already,
67 then set it using its `:set' function (or `set-default' if it has none).
68 The value is either the value in the symbol's `saved-value' property,
69 if any, or VALUE."
70 (unless (default-boundp symbol)
71 (funcall (or (get symbol 'custom-set) 'set-default)
72 symbol
73 (eval (if (get symbol 'saved-value)
74 (car (get symbol 'saved-value))
75 value)))))
77 (defun custom-initialize-reset (symbol value)
78 "Initialize SYMBOL based on VALUE.
79 Set the symbol, using its `:set' function (or `set-default' if it has none).
80 The value is either the symbol's current value
81 \(as obtained using the `:get' function), if any,
82 or the value in the symbol's `saved-value' property if any,
83 or (last of all) VALUE."
84 (funcall (or (get symbol 'custom-set) 'set-default)
85 symbol
86 (cond ((default-boundp symbol)
87 (funcall (or (get symbol 'custom-get) 'default-value)
88 symbol))
89 ((get symbol 'saved-value)
90 (eval (car (get symbol 'saved-value))))
92 (eval value)))))
94 (defun custom-initialize-changed (symbol value)
95 "Initialize SYMBOL with VALUE.
96 Like `custom-initialize-reset', but only use the `:set' function if
97 not using the standard setting.
98 For the standard setting, use `set-default'."
99 (cond ((default-boundp symbol)
100 (funcall (or (get symbol 'custom-set) 'set-default)
101 symbol
102 (funcall (or (get symbol 'custom-get) 'default-value)
103 symbol)))
104 ((get symbol 'saved-value)
105 (funcall (or (get symbol 'custom-set) 'set-default)
106 symbol
107 (eval (car (get symbol 'saved-value)))))
109 (set-default symbol (eval value)))))
111 (defvar custom-delayed-init-variables nil
112 "List of variables whose initialization is pending.")
114 (defun custom-initialize-delay (symbol value)
115 "Delay initialization of SYMBOL to the next Emacs start.
116 This is used in files that are preloaded (or for autoloaded
117 variables), so that the initialization is done in the run-time
118 context rather than the build-time context. This also has the
119 side-effect that the (delayed) initialization is performed with
120 the :set function.
122 For variables in preloaded files, you can simply use this
123 function for the :initialize property. For autoloaded variables,
124 you will also need to add an autoload stanza calling this
125 function, and another one setting the standard-value property.
126 See `send-mail-function' in sendmail.el for an example."
127 ;; Until the var is actually initialized, it is kept unbound.
128 ;; This seemed to be at least as good as setting it to an arbitrary
129 ;; value like nil (evaluating `value' is not an option because it
130 ;; may have undesirable side-effects).
131 (push symbol custom-delayed-init-variables))
133 (defun custom-declare-variable (symbol default doc &rest args)
134 "Like `defcustom', but SYMBOL and DEFAULT are evaluated as normal arguments.
135 DEFAULT should be an expression to evaluate to compute the default value,
136 not the default value itself.
138 DEFAULT is stored as SYMBOL's standard value, in SYMBOL's property
139 `standard-value'. At the same time, SYMBOL's property `force-value' is
140 set to nil, as the value is no longer rogue."
141 (put symbol 'standard-value (purecopy (list default)))
142 ;; Maybe this option was rogue in an earlier version. It no longer is.
143 (when (get symbol 'force-value)
144 (put symbol 'force-value nil))
145 (when doc
146 (put symbol 'variable-documentation doc))
147 (let ((initialize 'custom-initialize-reset)
148 (requests nil))
149 (unless (memq :group args)
150 (custom-add-to-group (custom-current-group) symbol 'custom-variable))
151 (while args
152 (let ((arg (car args)))
153 (setq args (cdr args))
154 (unless (symbolp arg)
155 (error "Junk in args %S" args))
156 (let ((keyword arg)
157 (value (car args)))
158 (unless args
159 (error "Keyword %s is missing an argument" keyword))
160 (setq args (cdr args))
161 (cond ((eq keyword :initialize)
162 (setq initialize value))
163 ((eq keyword :set)
164 (put symbol 'custom-set value))
165 ((eq keyword :get)
166 (put symbol 'custom-get value))
167 ((eq keyword :require)
168 (push value requests))
169 ((eq keyword :risky)
170 (put symbol 'risky-local-variable value))
171 ((eq keyword :safe)
172 (put symbol 'safe-local-variable value))
173 ((eq keyword :type)
174 (put symbol 'custom-type (purecopy value)))
175 ((eq keyword :options)
176 (if (get symbol 'custom-options)
177 ;; Slow safe code to avoid duplicates.
178 (mapc (lambda (option)
179 (custom-add-option symbol option))
180 value)
181 ;; Fast code for the common case.
182 (put symbol 'custom-options (copy-sequence value))))
184 (custom-handle-keyword symbol keyword value
185 'custom-variable))))))
186 (put symbol 'custom-requests requests)
187 ;; Do the actual initialization.
188 (unless custom-dont-initialize
189 (funcall initialize symbol default)))
190 (push symbol current-load-list)
191 (run-hooks 'custom-define-hook)
192 symbol)
194 (defmacro defcustom (symbol value doc &rest args)
195 "Declare SYMBOL as a customizable variable that defaults to VALUE.
196 DOC is the variable documentation.
198 Neither SYMBOL nor VALUE need to be quoted.
199 If SYMBOL is not already bound, initialize it to VALUE.
200 The remaining arguments should have the form
202 [KEYWORD VALUE]...
204 The following keywords are meaningful:
206 :type VALUE should be a widget type for editing the symbol's value.
207 :options VALUE should be a list of valid members of the widget type.
208 :initialize
209 VALUE should be a function used to initialize the
210 variable. It takes two arguments, the symbol and value
211 given in the `defcustom' call. The default is
212 `custom-initialize-reset'.
213 :set VALUE should be a function to set the value of the symbol.
214 It takes two arguments, the symbol to set and the value to
215 give it. The default choice of function is `set-default'.
216 :get VALUE should be a function to extract the value of symbol.
217 The function takes one argument, a symbol, and should return
218 the current value for that symbol. The default choice of function
219 is `default-value'.
220 :require
221 VALUE should be a feature symbol. If you save a value
222 for this option, then when your `.emacs' file loads the value,
223 it does (require VALUE) first.
224 :risky Set SYMBOL's `risky-local-variable' property to VALUE.
225 :safe Set SYMBOL's `safe-local-variable' property to VALUE.
227 The following common keywords are also meaningful.
229 :group VALUE should be a customization group.
230 Add SYMBOL (or FACE with `defface') to that group.
231 :link LINK-DATA
232 Include an external link after the documentation string for this
233 item. This is a sentence containing an active field which
234 references some other documentation.
236 There are several alternatives you can use for LINK-DATA:
238 (custom-manual INFO-NODE)
239 Link to an Info node; INFO-NODE is a string which specifies
240 the node name, as in \"(emacs)Top\".
242 (info-link INFO-NODE)
243 Like `custom-manual' except that the link appears in the
244 customization buffer with the Info node name.
246 (url-link URL)
247 Link to a web page; URL is a string which specifies the URL.
249 (emacs-commentary-link LIBRARY)
250 Link to the commentary section of LIBRARY.
252 (emacs-library-link LIBRARY)
253 Link to an Emacs Lisp LIBRARY file.
255 (file-link FILE)
256 Link to FILE.
258 (function-link FUNCTION)
259 Link to the documentation of FUNCTION.
261 (variable-link VARIABLE)
262 Link to the documentation of VARIABLE.
264 (custom-group-link GROUP)
265 Link to another customization GROUP.
267 You can specify the text to use in the customization buffer by
268 adding `:tag NAME' after the first element of the LINK-DATA; for
269 example, (info-link :tag \"foo\" \"(emacs)Top\") makes a link to the
270 Emacs manual which appears in the buffer as `foo'.
272 An item can have more than one external link; however, most items
273 have none at all.
274 :version
275 VALUE should be a string specifying that the variable was
276 first introduced, or its default value was changed, in Emacs
277 version VERSION.
278 :package-version
279 VALUE should be a list with the form (PACKAGE . VERSION)
280 specifying that the variable was first introduced, or its
281 default value was changed, in PACKAGE version VERSION. This
282 keyword takes priority over :version. The PACKAGE and VERSION
283 must appear in the alist `customize-package-emacs-version-alist'.
284 Since PACKAGE must be unique and the user might see it in an
285 error message, a good choice is the official name of the
286 package, such as MH-E or Gnus.
287 :tag LABEL
288 Use LABEL, a string, instead of the item's name, to label the item
289 in customization menus and buffers.
290 :load FILE
291 Load file FILE (a string) before displaying this customization
292 item. Loading is done with `load', and only if the file is
293 not already loaded.
294 :set-after VARIABLES
295 Specifies that SYMBOL should be set after the list of variables
296 VARIABLES when both have been customized.
298 If SYMBOL has a local binding, then this form affects the local
299 binding. This is normally not what you want. Thus, if you need
300 to load a file defining variables with this form, or with
301 `defvar' or `defconst', you should always load that file
302 _outside_ any bindings for these variables. \(`defvar' and
303 `defconst' behave similarly in this respect.)
305 See Info node `(elisp) Customization' in the Emacs Lisp manual
306 for more information."
307 (declare (doc-string 3))
308 ;; It is better not to use backquote in this file,
309 ;; because that makes a bootstrapping problem
310 ;; if you need to recompile all the Lisp files using interpreted code.
311 (nconc (list 'custom-declare-variable
312 (list 'quote symbol)
313 (list 'quote value)
314 doc)
315 args))
317 ;;; The `defface' Macro.
319 (defmacro defface (face spec doc &rest args)
320 "Declare FACE as a customizable face that defaults to SPEC.
321 FACE does not need to be quoted.
323 Third argument DOC is the face documentation.
325 If FACE has been set with `custom-set-faces', set the face attributes
326 as specified by that function, otherwise set the face attributes
327 according to SPEC.
329 The remaining arguments should have the form
331 [KEYWORD VALUE]...
333 For a list of valid keywords, see the common keywords listed in
334 `defcustom'.
336 SPEC should be an alist of the form ((DISPLAY ATTS)...).
338 In the first element, DISPLAY can be `default'. The ATTS in that
339 element then act as defaults for all the following elements.
341 Aside from that, DISPLAY specifies conditions to match some or
342 all frames. For each frame, the first element of SPEC where the
343 DISPLAY conditions are satisfied is the one that applies to that
344 frame. The ATTRs in this element take effect, and the following
345 elements are ignored, on that frame.
347 In the last element, DISPLAY can be t. That element applies to a
348 frame if none of the previous elements (except the `default' if
349 any) did.
351 ATTS is a list of face attributes followed by their values:
352 (ATTR VALUE ATTR VALUE...)
354 The possible attributes are `:family', `:width', `:height', `:weight',
355 `:slant', `:underline', `:overline', `:strike-through', `:box',
356 `:foreground', `:background', `:stipple', `:inverse-video', and `:inherit'.
358 DISPLAY can be `default' (only in the first element), the symbol
359 t (only in the last element) to match all frames, or an alist of
360 conditions of the form \(REQ ITEM...). For such an alist to
361 match a frame, each of the conditions must be satisfied, meaning
362 that the REQ property of the frame must match one of the
363 corresponding ITEMs. These are the defined REQ values:
365 `type' (the value of `window-system')
366 Under X, in addition to the values `window-system' can take,
367 `motif', `lucid', `gtk' and `x-toolkit' are allowed, and match when
368 the Motif toolkit, Lucid toolkit, GTK toolkit or any X toolkit is in use.
370 `class' (the frame's color support)
371 Should be one of `color', `grayscale', or `mono'.
373 `background' (what color is used for the background text)
374 Should be one of `light' or `dark'.
376 `min-colors' (the minimum number of colors the frame should support)
377 Should be an integer, it is compared with the result of
378 `display-color-cells'.
380 `supports' (only match frames that support the specified face attributes)
381 Should be a list of face attributes. See the documentation for
382 the function `display-supports-face-attributes-p' for more
383 information on exactly how testing is done.
385 See Info node `(elisp) Customization' in the Emacs Lisp manual
386 for more information."
387 (declare (doc-string 3))
388 ;; It is better not to use backquote in this file,
389 ;; because that makes a bootstrapping problem
390 ;; if you need to recompile all the Lisp files using interpreted code.
391 (nconc (list 'custom-declare-face (list 'quote face) spec doc) args))
393 ;;; The `defgroup' Macro.
395 (defun custom-current-group ()
396 (cdr (assoc load-file-name custom-current-group-alist)))
398 (defun custom-declare-group (symbol members doc &rest args)
399 "Like `defgroup', but SYMBOL is evaluated as a normal argument."
400 (while members
401 (apply 'custom-add-to-group symbol (car members))
402 (setq members (cdr members)))
403 (when doc
404 ;; This text doesn't get into DOC.
405 (put symbol 'group-documentation (purecopy doc)))
406 (while args
407 (let ((arg (car args)))
408 (setq args (cdr args))
409 (unless (symbolp arg)
410 (error "Junk in args %S" args))
411 (let ((keyword arg)
412 (value (car args)))
413 (unless args
414 (error "Keyword %s is missing an argument" keyword))
415 (setq args (cdr args))
416 (cond ((eq keyword :prefix)
417 (put symbol 'custom-prefix (purecopy value)))
419 (custom-handle-keyword symbol keyword value
420 'custom-group))))))
421 ;; Record the group on the `current' list.
422 (let ((elt (assoc load-file-name custom-current-group-alist)))
423 (if elt (setcdr elt symbol)
424 (push (cons (purecopy load-file-name) symbol)
425 custom-current-group-alist)))
426 (run-hooks 'custom-define-hook)
427 symbol)
429 (defmacro defgroup (symbol members doc &rest args)
430 "Declare SYMBOL as a customization group containing MEMBERS.
431 SYMBOL does not need to be quoted.
433 Third argument DOC is the group documentation. This should be a short
434 description of the group, beginning with a capital and ending with
435 a period. Words other than the first should not be capitalized, if they
436 are not usually written so.
438 MEMBERS should be an alist of the form ((NAME WIDGET)...) where
439 NAME is a symbol and WIDGET is a widget for editing that symbol.
440 Useful widgets are `custom-variable' for editing variables,
441 `custom-face' for edit faces, and `custom-group' for editing groups.
443 The remaining arguments should have the form
445 [KEYWORD VALUE]...
447 For a list of valid keywords, see the common keywords listed in
448 `defcustom'.
450 See Info node `(elisp) Customization' in the Emacs Lisp manual
451 for more information."
452 (declare (doc-string 3))
453 ;; It is better not to use backquote in this file,
454 ;; because that makes a bootstrapping problem
455 ;; if you need to recompile all the Lisp files using interpreted code.
456 (nconc (list 'custom-declare-group (list 'quote symbol) members doc) args))
458 (defun custom-add-to-group (group option widget)
459 "To existing GROUP add a new OPTION of type WIDGET.
460 If there already is an entry for OPTION and WIDGET, nothing is done."
461 (let ((members (get group 'custom-group))
462 (entry (list option widget)))
463 (unless (member entry members)
464 (put group 'custom-group (nconc members (list entry))))))
466 (defun custom-group-of-mode (mode)
467 "Return the custom group corresponding to the major or minor MODE.
468 If no such group is found, return nil."
469 (or (get mode 'custom-mode-group)
470 (if (or (get mode 'custom-group)
471 (and (string-match "-mode\\'" (symbol-name mode))
472 (get (setq mode (intern (substring (symbol-name mode)
473 0 (match-beginning 0))))
474 'custom-group)))
475 mode)))
477 ;;; Properties.
479 (defun custom-handle-all-keywords (symbol args type)
480 "For customization option SYMBOL, handle keyword arguments ARGS.
481 Third argument TYPE is the custom option type."
482 (unless (memq :group args)
483 (custom-add-to-group (custom-current-group) symbol type))
484 (while args
485 (let ((arg (car args)))
486 (setq args (cdr args))
487 (unless (symbolp arg)
488 (error "Junk in args %S" args))
489 (let ((keyword arg)
490 (value (car args)))
491 (unless args
492 (error "Keyword %s is missing an argument" keyword))
493 (setq args (cdr args))
494 (custom-handle-keyword symbol keyword value type)))))
496 (defun custom-handle-keyword (symbol keyword value type)
497 "For customization option SYMBOL, handle KEYWORD with VALUE.
498 Fourth argument TYPE is the custom option type."
499 (if purify-flag
500 (setq value (purecopy value)))
501 (cond ((eq keyword :group)
502 (custom-add-to-group value symbol type))
503 ((eq keyword :version)
504 (custom-add-version symbol value))
505 ((eq keyword :package-version)
506 (custom-add-package-version symbol value))
507 ((eq keyword :link)
508 (custom-add-link symbol value))
509 ((eq keyword :load)
510 (custom-add-load symbol value))
511 ((eq keyword :tag)
512 (put symbol 'custom-tag value))
513 ((eq keyword :set-after)
514 (custom-add-dependencies symbol value))
516 (error "Unknown keyword %s" keyword))))
518 (defun custom-add-dependencies (symbol value)
519 "To the custom option SYMBOL, add dependencies specified by VALUE.
520 VALUE should be a list of symbols. For each symbol in that list,
521 this specifies that SYMBOL should be set after the specified symbol, if
522 both appear in constructs like `custom-set-variables'."
523 (unless (listp value)
524 (error "Invalid custom dependency `%s'" value))
525 (let* ((deps (get symbol 'custom-dependencies))
526 (new-deps deps))
527 (while value
528 (let ((dep (car value)))
529 (unless (symbolp dep)
530 (error "Invalid custom dependency `%s'" dep))
531 (unless (memq dep new-deps)
532 (setq new-deps (cons dep new-deps)))
533 (setq value (cdr value))))
534 (unless (eq deps new-deps)
535 (put symbol 'custom-dependencies new-deps))))
537 (defun custom-add-option (symbol option)
538 "To the variable SYMBOL add OPTION.
540 If SYMBOL's custom type is a hook, OPTION should be a hook member.
541 If SYMBOL's custom type is an alist, OPTION specifies a symbol
542 to offer to the user as a possible key in the alist.
543 For other custom types, this has no effect."
544 (let ((options (get symbol 'custom-options)))
545 (unless (member option options)
546 (put symbol 'custom-options (cons option options)))))
547 (defalias 'custom-add-frequent-value 'custom-add-option)
549 (defun custom-add-link (symbol widget)
550 "To the custom option SYMBOL add the link WIDGET."
551 (let ((links (get symbol 'custom-links)))
552 (unless (member widget links)
553 (put symbol 'custom-links (cons (purecopy widget) links)))))
555 (defun custom-add-version (symbol version)
556 "To the custom option SYMBOL add the version VERSION."
557 (put symbol 'custom-version (purecopy version)))
559 (defun custom-add-package-version (symbol version)
560 "To the custom option SYMBOL add the package version VERSION."
561 (put symbol 'custom-package-version (purecopy version)))
563 (defun custom-add-load (symbol load)
564 "To the custom option SYMBOL add the dependency LOAD.
565 LOAD should be either a library file name, or a feature name."
566 (let ((loads (get symbol 'custom-loads)))
567 (unless (member load loads)
568 (put symbol 'custom-loads (cons (purecopy load) loads)))))
570 (defun custom-autoload (symbol load &optional noset)
571 "Mark SYMBOL as autoloaded custom variable and add dependency LOAD.
572 If NOSET is non-nil, don't bother autoloading LOAD when setting the variable."
573 (put symbol 'custom-autoload (if noset 'noset t))
574 (custom-add-load symbol load))
576 ;; This test is also in the C code of `user-variable-p'.
577 (defun custom-variable-p (variable)
578 "Return non-nil if VARIABLE is a custom variable.
579 This recursively follows aliases."
580 (setq variable (indirect-variable variable))
581 (or (get variable 'standard-value)
582 (get variable 'custom-autoload)))
584 (defun custom-note-var-changed (variable)
585 "Inform Custom that VARIABLE has been set (changed).
586 VARIABLE is a symbol that names a user option.
587 The result is that the change is treated as having been made through Custom."
588 (put variable 'customized-value (list (custom-quote (eval variable)))))
591 ;;; Custom Themes
593 ;;; Loading files needed to customize a symbol.
594 ;;; This is in custom.el because menu-bar.el needs it for toggle cmds.
596 (defvar custom-load-recursion nil
597 "Hack to avoid recursive dependencies.")
599 (defun custom-load-symbol (symbol)
600 "Load all dependencies for SYMBOL."
601 (unless custom-load-recursion
602 (let ((custom-load-recursion t))
603 ;; Load these files if not already done,
604 ;; to make sure we know all the dependencies of SYMBOL.
605 (condition-case nil
606 (require 'cus-load)
607 (error nil))
608 (condition-case nil
609 (require 'cus-start)
610 (error nil))
611 (dolist (load (get symbol 'custom-loads))
612 (cond ((symbolp load) (condition-case nil (require load) (error nil)))
613 ;; This is subsumed by the test below, but it's much faster.
614 ((assoc load load-history))
615 ;; This was just (assoc (locate-library load) load-history)
616 ;; but has been optimized not to load locate-library
617 ;; if not necessary.
618 ((let ((regexp (concat "\\(\\`\\|/\\)" (regexp-quote load)
619 "\\(\\'\\|\\.\\)"))
620 (found nil))
621 (dolist (loaded load-history)
622 (and (stringp (car loaded))
623 (string-match regexp (car loaded))
624 (setq found t)))
625 found))
626 ;; Without this, we would load cus-edit recursively.
627 ;; We are still loading it when we call this,
628 ;; and it is not in load-history yet.
629 ((equal load "cus-edit"))
630 (t (condition-case nil (load load) (error nil))))))))
632 (defvar custom-local-buffer nil
633 "Non-nil, in a Customization buffer, means customize a specific buffer.
634 If this variable is non-nil, it should be a buffer,
635 and it means customize the local bindings of that buffer.
636 This variable is a permanent local, and it normally has a local binding
637 in every Customization buffer.")
638 (put 'custom-local-buffer 'permanent-local t)
640 (defun custom-set-default (variable value)
641 "Default :set function for a customizable variable.
642 Normally, this sets the default value of VARIABLE to VALUE,
643 but if `custom-local-buffer' is non-nil,
644 this sets the local binding in that buffer instead."
645 (if custom-local-buffer
646 (with-current-buffer custom-local-buffer
647 (set variable value))
648 (set-default variable value)))
650 (defun custom-set-minor-mode (variable value)
651 ":set function for minor mode variables.
652 Normally, this sets the default value of VARIABLE to nil if VALUE
653 is nil and to t otherwise,
654 but if `custom-local-buffer' is non-nil,
655 this sets the local binding in that buffer instead."
656 (if custom-local-buffer
657 (with-current-buffer custom-local-buffer
658 (funcall variable (if value 1 0)))
659 (funcall variable (if value 1 0))))
661 (defun custom-quote (sexp)
662 "Quote SEXP if it is not self quoting."
663 (if (or (memq sexp '(t nil))
664 (keywordp sexp)
665 (and (listp sexp)
666 (memq (car sexp) '(lambda)))
667 (stringp sexp)
668 (numberp sexp)
669 (vectorp sexp)
670 ;;; (and (fboundp 'characterp)
671 ;;; (characterp sexp))
673 sexp
674 (list 'quote sexp)))
676 (defun customize-mark-to-save (symbol)
677 "Mark SYMBOL for later saving.
679 If the default value of SYMBOL is different from the standard value,
680 set the `saved-value' property to a list whose car evaluates to the
681 default value. Otherwise, set it to nil.
683 To actually save the value, call `custom-save-all'.
685 Return non-nil if the `saved-value' property actually changed."
686 (custom-load-symbol symbol)
687 (let* ((get (or (get symbol 'custom-get) 'default-value))
688 (value (funcall get symbol))
689 (saved (get symbol 'saved-value))
690 (standard (get symbol 'standard-value))
691 (comment (get symbol 'customized-variable-comment)))
692 ;; Save default value if different from standard value.
693 (if (or (null standard)
694 (not (equal value (condition-case nil
695 (eval (car standard))
696 (error nil)))))
697 (put symbol 'saved-value (list (custom-quote value)))
698 (put symbol 'saved-value nil))
699 ;; Clear customized information (set, but not saved).
700 (put symbol 'customized-value nil)
701 ;; Save any comment that might have been set.
702 (when comment
703 (put symbol 'saved-variable-comment comment))
704 (not (equal saved (get symbol 'saved-value)))))
706 (defun customize-mark-as-set (symbol)
707 "Mark current value of SYMBOL as being set from customize.
709 If the default value of SYMBOL is different from the saved value if any,
710 or else if it is different from the standard value, set the
711 `customized-value' property to a list whose car evaluates to the
712 default value. Otherwise, set it to nil.
714 Return non-nil if the `customized-value' property actually changed."
715 (custom-load-symbol symbol)
716 (let* ((get (or (get symbol 'custom-get) 'default-value))
717 (value (funcall get symbol))
718 (customized (get symbol 'customized-value))
719 (old (or (get symbol 'saved-value) (get symbol 'standard-value))))
720 ;; Mark default value as set if different from old value.
721 (if (not (and old
722 (equal value (condition-case nil
723 (eval (car old))
724 (error nil)))))
725 (progn (put symbol 'customized-value (list (custom-quote value)))
726 (custom-push-theme 'theme-value symbol 'user 'set
727 (custom-quote value)))
728 (put symbol 'customized-value nil))
729 ;; Changed?
730 (not (equal customized (get symbol 'customized-value)))))
732 (defun custom-reevaluate-setting (symbol)
733 "Reset the value of SYMBOL by re-evaluating its saved or standard value.
734 Use the :set function to do so. This is useful for customizable options
735 that are defined before their standard value can really be computed.
736 E.g. dumped variables whose default depends on run-time information."
737 (funcall (or (get symbol 'custom-set) 'set-default)
738 symbol
739 (eval (car (or (get symbol 'saved-value) (get symbol 'standard-value))))))
742 ;;; Custom Themes
744 ;; Custom themes are collections of settings that can be enabled or
745 ;; disabled as a unit.
747 ;; Each Custom theme is defined by a symbol, called the theme name.
748 ;; The `theme-settings' property of the theme name records the
749 ;; variable and face settings of the theme. This property is a list
750 ;; of elements, each of the form
752 ;; (PROP SYMBOL THEME VALUE)
754 ;; - PROP is either `theme-value' or `theme-face'
755 ;; - SYMBOL is the face or variable name
756 ;; - THEME is the theme name (redundant, but simplifies the code)
757 ;; - VALUE is an expression that gives the theme's setting for SYMBOL.
759 ;; The theme name also has a `theme-feature' property, whose value is
760 ;; specified when the theme is defined (see `custom-declare-theme').
761 ;; Usually, this is just a symbol named THEME-theme. This lets
762 ;; external libraries call (require 'foo-theme).
764 ;; In addition, each symbol (either a variable or a face) affected by
765 ;; an *enabled* theme has a `theme-value' or `theme-face' property,
766 ;; which is a list of elements each of the form
768 ;; (THEME VALUE)
770 ;; which have the same meanings as in `theme-settings'.
772 ;; The `theme-value' and `theme-face' lists are ordered by decreasing
773 ;; theme precedence. Thus, the first element is always the one that
774 ;; is in effect.
776 ;; Each theme is stored in a theme file, with filename THEME-theme.el.
777 ;; Loading a theme basically involves calling (load "THEME-theme")
778 ;; This is done by the function `load-theme'. Loading a theme
779 ;; automatically enables it.
781 ;; When a theme is enabled, the `theme-value' and `theme-face'
782 ;; properties for the affected symbols are set. When a theme is
783 ;; disabled, its settings are removed from the `theme-value' and
784 ;; `theme-face' properties, but the theme's own `theme-settings'
785 ;; property remains unchanged.
787 (defvar custom-known-themes '(user changed)
788 "Themes that have been defined with `deftheme'.
789 The default value is the list (user changed). The theme `changed'
790 contains the settings before custom themes are applied. The
791 theme `user' contains all the settings the user customized and saved.
792 Additional themes declared with the `deftheme' macro will be added to
793 the front of this list.")
795 (defsubst custom-theme-p (theme)
796 "Non-nil when THEME has been defined."
797 (memq theme custom-known-themes))
799 (defsubst custom-check-theme (theme)
800 "Check whether THEME is valid, and signal an error if it is not."
801 (unless (custom-theme-p theme)
802 (error "Unknown theme `%s'" theme)))
804 (defun custom-push-theme (prop symbol theme mode &optional value)
805 "Record VALUE for face or variable SYMBOL in custom theme THEME.
806 PROP is `theme-face' for a face, `theme-value' for a variable.
808 MODE can be either the symbol `set' or the symbol `reset'. If it is the
809 symbol `set', then VALUE is the value to use. If it is the symbol
810 `reset', then SYMBOL will be removed from THEME (VALUE is ignored).
812 See `custom-known-themes' for a list of known themes."
813 (unless (memq prop '(theme-value theme-face))
814 (error "Unknown theme property"))
815 (let* ((old (get symbol prop))
816 (setting (assq theme old)) ; '(theme value)
817 (theme-settings ; '(prop symbol theme value)
818 (get theme 'theme-settings)))
819 (if (eq mode 'reset)
820 ;; Remove a setting.
821 (when setting
822 (let (res)
823 (dolist (theme-setting theme-settings)
824 (if (and (eq (car theme-setting) prop)
825 (eq (cadr theme-setting) symbol))
826 (setq res theme-setting)))
827 (put theme 'theme-settings (delq res theme-settings)))
828 (put symbol prop (delq setting old)))
829 (if setting
830 ;; Alter an existing setting.
831 (let (res)
832 (dolist (theme-setting theme-settings)
833 (if (and (eq (car theme-setting) prop)
834 (eq (cadr theme-setting) symbol))
835 (setq res theme-setting)))
836 (put theme 'theme-settings
837 (cons (list prop symbol theme value)
838 (delq res theme-settings)))
839 (setcar (cdr setting) value))
840 ;; Add a new setting.
841 ;; If the user changed the value outside of Customize, we
842 ;; first save the current value to a fake theme, `changed'.
843 ;; This ensures that the user-set value comes back if the
844 ;; theme is later disabled.
845 (if (null old)
846 (if (and (eq prop 'theme-value)
847 (boundp symbol))
848 (let ((sv (get symbol 'standard-value)))
849 (unless (and sv
850 (equal (eval (car sv)) (symbol-value symbol)))
851 (setq old (list (list 'changed (symbol-value symbol))))))
852 (if (and (facep symbol)
853 (not (face-spec-match-p symbol (get symbol 'face-defface-spec))))
854 (setq old (list (list 'changed (list
855 (append '(t) (custom-face-attributes-get symbol nil)))))))))
856 (put symbol prop (cons (list theme value) old))
857 (put theme 'theme-settings
858 (cons (list prop symbol theme value)
859 theme-settings))))))
862 (defun custom-set-variables (&rest args)
863 "Install user customizations of variable values specified in ARGS.
864 These settings are registered as theme `user'.
865 The arguments should each be a list of the form:
867 (SYMBOL EXP [NOW [REQUEST [COMMENT]]])
869 This stores EXP (without evaluating it) as the saved value for SYMBOL.
870 If NOW is present and non-nil, then also evaluate EXP and set
871 the default value for the SYMBOL to the value of EXP.
873 REQUEST is a list of features we must require in order to
874 handle SYMBOL properly.
875 COMMENT is a comment string about SYMBOL."
876 (apply 'custom-theme-set-variables 'user args))
878 (defun custom-theme-set-variables (theme &rest args)
879 "Initialize variables for theme THEME according to settings in ARGS.
880 Each of the arguments in ARGS should be a list of this form:
882 (SYMBOL EXP [NOW [REQUEST [COMMENT]]])
884 This stores EXP (without evaluating it) as the saved value for SYMBOL.
885 If NOW is present and non-nil, then also evaluate EXP and set
886 the default value for the SYMBOL to the value of EXP.
888 REQUEST is a list of features we must require in order to
889 handle SYMBOL properly.
890 COMMENT is a comment string about SYMBOL.
892 EXP itself is saved unevaluated as SYMBOL property `saved-value' and
893 in SYMBOL's list property `theme-value' \(using `custom-push-theme')."
894 (custom-check-theme theme)
896 ;; Process all the needed autoloads before anything else, so that the
897 ;; subsequent code has all the info it needs (e.g. which var corresponds
898 ;; to a minor mode), regardless of the ordering of the variables.
899 (dolist (entry args)
900 (let* ((symbol (indirect-variable (nth 0 entry))))
901 (unless (or (get symbol 'standard-value)
902 (memq (get symbol 'custom-autoload) '(nil noset)))
903 ;; This symbol needs to be autoloaded, even just for a `set'.
904 (custom-load-symbol symbol))))
906 ;; Move minor modes and variables with explicit requires to the end.
907 (setq args
908 (sort args
909 (lambda (a1 a2)
910 (let* ((sym1 (car a1))
911 (sym2 (car a2))
912 (1-then-2 (memq sym1 (get sym2 'custom-dependencies)))
913 (2-then-1 (memq sym2 (get sym1 'custom-dependencies))))
914 (cond ((and 1-then-2 2-then-1)
915 (error "Circular custom dependency between `%s' and `%s'"
916 sym1 sym2))
917 (2-then-1 nil)
918 ;; 1 is a dependency of 2, so needs to be set first.
919 (1-then-2)
920 ;; Put minor modes and symbols with :require last.
921 ;; Putting minor modes last ensures that the mode
922 ;; function will see other customized values rather
923 ;; than default values.
924 (t (or (nth 3 a2)
925 (eq (get sym2 'custom-set)
926 'custom-set-minor-mode))))))))
927 (while args
928 (let ((entry (car args)))
929 (if (listp entry)
930 (let* ((symbol (indirect-variable (nth 0 entry)))
931 (value (nth 1 entry))
932 (now (nth 2 entry))
933 (requests (nth 3 entry))
934 (comment (nth 4 entry))
935 set)
936 (when requests
937 (put symbol 'custom-requests requests)
938 (mapc 'require requests))
939 (setq set (or (get symbol 'custom-set) 'custom-set-default))
940 (put symbol 'saved-value (list value))
941 (put symbol 'saved-variable-comment comment)
942 (custom-push-theme 'theme-value symbol theme 'set value)
943 ;; Allow for errors in the case where the setter has
944 ;; changed between versions, say, but let the user know.
945 (condition-case data
946 (cond (now
947 ;; Rogue variable, set it now.
948 (put symbol 'force-value t)
949 (funcall set symbol (eval value)))
950 ((default-boundp symbol)
951 ;; Something already set this, overwrite it.
952 (funcall set symbol (eval value))))
953 (error
954 (message "Error setting %s: %s" symbol data)))
955 (setq args (cdr args))
956 (and (or now (default-boundp symbol))
957 (put symbol 'variable-comment comment)))
958 ;; I believe this is dead-code, because the `sort' code above would
959 ;; have burped before we could get here. --Stef
960 ;; Old format, a plist of SYMBOL VALUE pairs.
961 (message "Warning: old format `custom-set-variables'")
962 (ding)
963 (sit-for 2)
964 (let ((symbol (indirect-variable (nth 0 args)))
965 (value (nth 1 args)))
966 (put symbol 'saved-value (list value))
967 (custom-push-theme 'theme-value symbol theme 'set value))
968 (setq args (cdr (cdr args)))))))
971 ;;; Defining themes.
973 ;; A theme file should be named `THEME-theme.el' (where THEME is the theme
974 ;; name), and found in either `custom-theme-directory' or the load path.
975 ;; It has the following format:
977 ;; (deftheme THEME
978 ;; DOCSTRING)
980 ;; (custom-theme-set-variables
981 ;; 'THEME
982 ;; [THEME-VARIABLES])
984 ;; (custom-theme-set-faces
985 ;; 'THEME
986 ;; [THEME-FACES])
988 ;; (provide-theme 'THEME)
991 ;; The IGNORED arguments to deftheme come from the XEmacs theme code, where
992 ;; they were used to supply keyword-value pairs like `:immediate',
993 ;; `:variable-reset-string', etc. We don't use any of these, so ignore them.
995 (defmacro deftheme (theme &optional doc &rest ignored)
996 "Declare THEME to be a Custom theme.
997 The optional argument DOC is a doc string describing the theme.
999 Any theme `foo' should be defined in a file called `foo-theme.el';
1000 see `custom-make-theme-feature' for more information."
1001 (let ((feature (custom-make-theme-feature theme)))
1002 ;; It is better not to use backquote in this file,
1003 ;; because that makes a bootstrapping problem
1004 ;; if you need to recompile all the Lisp files using interpreted code.
1005 (list 'custom-declare-theme (list 'quote theme) (list 'quote feature) doc)))
1007 (defun custom-declare-theme (theme feature &optional doc &rest ignored)
1008 "Like `deftheme', but THEME is evaluated as a normal argument.
1009 FEATURE is the feature this theme provides. Normally, this is a symbol
1010 created from THEME by `custom-make-theme-feature'."
1011 (if (memq theme '(user changed))
1012 (error "Custom theme cannot be named %S" theme))
1013 (add-to-list 'custom-known-themes theme)
1014 (put theme 'theme-feature feature)
1015 (when doc (put theme 'theme-documentation doc)))
1017 (defun custom-make-theme-feature (theme)
1018 "Given a symbol THEME, create a new symbol by appending \"-theme\".
1019 Store this symbol in the `theme-feature' property of THEME.
1020 Calling `provide-theme' to provide THEME actually puts `THEME-theme'
1021 into `features'.
1023 This allows for a file-name convention for autoloading themes:
1024 Every theme X has a property `provide-theme' whose value is \"X-theme\".
1025 \(load-theme X) then attempts to load the file `X-theme.el'."
1026 (intern (concat (symbol-name theme) "-theme")))
1028 ;;; Loading themes.
1030 (defcustom custom-theme-directory
1031 user-emacs-directory
1032 "Directory in which Custom theme files should be written.
1033 `load-theme' searches this directory in addition to load-path.
1034 The command `customize-create-theme' writes the files it produces
1035 into this directory."
1036 :type 'string
1037 :group 'customize
1038 :version "22.1")
1040 (defun provide-theme (theme)
1041 "Indicate that this file provides THEME.
1042 This calls `provide' to provide the feature name stored in THEME's
1043 property `theme-feature' (which is usually a symbol created by
1044 `custom-make-theme-feature')."
1045 (if (memq theme '(user changed))
1046 (error "Custom theme cannot be named %S" theme))
1047 (custom-check-theme theme)
1048 (provide (get theme 'theme-feature))
1049 ;; Loading a theme also enables it.
1050 (push theme custom-enabled-themes)
1051 ;; `user' must always be the highest-precedence enabled theme.
1052 ;; Make that remain true. (This has the effect of making user settings
1053 ;; override the ones just loaded, too.)
1054 (let ((custom-enabling-themes t))
1055 (enable-theme 'user)))
1057 (defun load-theme (theme)
1058 "Load a theme's settings from its file.
1059 This also enables the theme; use `disable-theme' to disable it."
1060 ;; Note we do no check for validity of the theme here.
1061 ;; This allows to pull in themes by a file-name convention
1062 (interactive "SCustom theme name: ")
1063 ;; If reloading, clear out the old theme settings.
1064 (when (custom-theme-p theme)
1065 (disable-theme theme)
1066 (put theme 'theme-settings nil)
1067 (put theme 'theme-feature nil)
1068 (put theme 'theme-documentation nil))
1069 (let ((load-path (if (file-directory-p custom-theme-directory)
1070 (cons custom-theme-directory load-path)
1071 load-path)))
1072 (load (symbol-name (custom-make-theme-feature theme)))))
1074 ;;; Enabling and disabling loaded themes.
1076 (defvar custom-enabling-themes nil)
1078 (defun enable-theme (theme)
1079 "Reenable all variable and face settings defined by THEME.
1080 The newly enabled theme gets the highest precedence (after `user').
1081 If it is already enabled, just give it highest precedence (after `user').
1083 If THEME does not specify any theme settings, this tries to load
1084 the theme from its theme file, by calling `load-theme'."
1085 (interactive "SEnable Custom theme: ")
1086 (if (not (custom-theme-p theme))
1087 (load-theme theme)
1088 ;; This could use a bit of optimization -- cyd
1089 (let ((settings (get theme 'theme-settings)))
1090 (dolist (s settings)
1091 (let* ((prop (car s))
1092 (symbol (cadr s))
1093 (spec-list (get symbol prop)))
1094 (put symbol prop (cons (cddr s) (assq-delete-all theme spec-list)))
1095 (if (eq prop 'theme-value)
1096 (custom-theme-recalc-variable symbol)
1097 (custom-theme-recalc-face symbol)))))
1098 (unless (eq theme 'user)
1099 (setq custom-enabled-themes
1100 (cons theme (delq theme custom-enabled-themes)))
1101 (unless custom-enabling-themes
1102 (enable-theme 'user)))))
1104 (defcustom custom-enabled-themes nil
1105 "List of enabled Custom Themes, highest precedence first.
1107 This does not include the `user' theme, which is set by Customize,
1108 and always takes precedence over other Custom Themes."
1109 :group 'customize
1110 :type '(repeat symbol)
1111 :set-after '(custom-theme-directory) ; so we can find the themes
1112 :set (lambda (symbol themes)
1113 ;; Avoid an infinite loop when custom-enabled-themes is
1114 ;; defined in a theme (e.g. `user'). Enabling the theme sets
1115 ;; custom-enabled-themes, which enables the theme...
1116 (unless custom-enabling-themes
1117 (let ((custom-enabling-themes t) failures)
1118 (setq themes (delq 'user (delete-dups themes)))
1119 (if (boundp symbol)
1120 (dolist (theme (symbol-value symbol))
1121 (if (not (memq theme themes))
1122 (disable-theme theme))))
1123 (dolist (theme (reverse themes))
1124 (condition-case nil
1125 (enable-theme theme)
1126 (error (progn (push theme failures)
1127 (setq themes (delq theme themes))))))
1128 (enable-theme 'user)
1129 (custom-set-default symbol themes)
1130 (if failures
1131 (message "Failed to enable themes: %s"
1132 (mapconcat 'symbol-name failures " ")))))))
1134 (defsubst custom-theme-enabled-p (theme)
1135 "Return non-nil if THEME is enabled."
1136 (memq theme custom-enabled-themes))
1138 (defun disable-theme (theme)
1139 "Disable all variable and face settings defined by THEME.
1140 See `custom-enabled-themes' for a list of enabled themes."
1141 (interactive (list (intern
1142 (completing-read
1143 "Disable Custom theme: "
1144 (mapcar 'symbol-name custom-enabled-themes)
1145 nil t))))
1146 (when (custom-theme-enabled-p theme)
1147 (let ((settings (get theme 'theme-settings)))
1148 (dolist (s settings)
1149 (let* ((prop (car s))
1150 (symbol (cadr s))
1151 (spec-list (get symbol prop)))
1152 (put symbol prop (assq-delete-all theme spec-list))
1153 (if (eq prop 'theme-value)
1154 (custom-theme-recalc-variable symbol)
1155 (custom-theme-recalc-face symbol)))))
1156 (setq custom-enabled-themes
1157 (delq theme custom-enabled-themes))))
1159 (defun custom-variable-theme-value (variable)
1160 "Return (list VALUE) indicating the custom theme value of VARIABLE.
1161 That is to say, it specifies what the value should be according to
1162 currently enabled custom themes.
1164 This function returns nil if no custom theme specifies a value for VARIABLE."
1165 (let* ((theme-value (get variable 'theme-value)))
1166 (if theme-value
1167 (cdr (car theme-value)))))
1169 (defun custom-theme-recalc-variable (variable)
1170 "Set VARIABLE according to currently enabled custom themes."
1171 (let ((valspec (custom-variable-theme-value variable)))
1172 (if valspec
1173 (put variable 'saved-value valspec)
1174 (setq valspec (get variable 'standard-value)))
1175 (if (and valspec
1176 (or (get variable 'force-value)
1177 (default-boundp variable)))
1178 (funcall (or (get variable 'custom-set) 'set-default) variable
1179 (eval (car valspec))))))
1181 (defun custom-theme-recalc-face (face)
1182 "Set FACE according to currently enabled custom themes."
1183 (if (facep face)
1184 (face-spec-set face
1185 (get (or (get face 'face-alias) face)
1186 'face-override-spec))))
1188 ;;; XEmacs compability functions
1190 ;; In XEmacs, when you reset a Custom Theme, you have to specify the
1191 ;; theme to reset it to. We just apply the next available theme, so
1192 ;; just ignore the IGNORED arguments.
1194 (defun custom-theme-reset-variables (theme &rest args)
1195 "Reset some variable settings in THEME to their values in other themes.
1196 Each of the arguments ARGS has this form:
1198 (VARIABLE IGNORED)
1200 This means reset VARIABLE. (The argument IGNORED is ignored)."
1201 (custom-check-theme theme)
1202 (dolist (arg args)
1203 (custom-push-theme 'theme-value (car arg) theme 'reset)))
1205 (defun custom-reset-variables (&rest args)
1206 "Reset the specs of some variables to their values in other themes.
1207 This creates settings in the `user' theme.
1209 Each of the arguments ARGS has this form:
1211 (VARIABLE IGNORED)
1213 This means reset VARIABLE. (The argument IGNORED is ignored)."
1214 (apply 'custom-theme-reset-variables 'user args))
1216 ;;; The End.
1218 ;; Process the defcustoms for variables loaded before this file.
1219 (while custom-declare-variable-list
1220 (apply 'custom-declare-variable (car custom-declare-variable-list))
1221 (setq custom-declare-variable-list (cdr custom-declare-variable-list)))
1223 (provide 'custom)
1225 ;; arch-tag: 041b6116-aabe-4f9a-902d-74092bc3dab2
1226 ;;; custom.el ends here