(ls-lisp-insert-directory): Allow for a trailing '/' when looking for "."
[emacs.git] / lisp / custom.el
blob07533253d767586e4a7c9d61146899d3d65d0e19
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 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 (if (get symbol 'saved-value)
61 (eval (car (get symbol 'saved-value)))
62 (eval 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 (if (get symbol 'saved-value)
74 (eval (car (get symbol 'saved-value)))
75 (eval value)))))
77 (defun custom-initialize-safe-set (symbol value)
78 "Like `custom-initialize-set', but catches errors.
79 If an error occurs during initialization, SYMBOL is set to nil
80 and no error is thrown. This is meant for use in pre-loaded files
81 where some variables or functions used to compute VALUE may not yet
82 be defined. You can then re-evaluate VALUE in startup.el, for instance
83 using `custom-reevaluate-setting'."
84 (condition-case nil
85 (custom-initialize-set symbol value)
86 (error (set-default symbol nil))))
88 (defun custom-initialize-safe-default (symbol value)
89 "Like `custom-initialize-default', but catches errors.
90 If an error occurs during initialization, SYMBOL is set to nil
91 and no error is thrown. This is meant for use in pre-loaded files
92 where some variables or functions used to compute VALUE may not yet
93 be defined. You can then re-evaluate VALUE in startup.el, for instance
94 using `custom-reevaluate-setting'."
95 (condition-case nil
96 (custom-initialize-default symbol value)
97 (error (set-default symbol nil))))
99 (defun custom-initialize-reset (symbol value)
100 "Initialize SYMBOL based on VALUE.
101 Set the symbol, using its `:set' function (or `set-default' if it has none).
102 The value is either the symbol's current value
103 \(as obtained using the `:get' function), if any,
104 or the value in the symbol's `saved-value' property if any,
105 or (last of all) VALUE."
106 (funcall (or (get symbol 'custom-set) 'set-default)
107 symbol
108 (cond ((default-boundp symbol)
109 (funcall (or (get symbol 'custom-get) 'default-value)
110 symbol))
111 ((get symbol 'saved-value)
112 (eval (car (get symbol 'saved-value))))
114 (eval value)))))
116 (defun custom-initialize-changed (symbol value)
117 "Initialize SYMBOL with VALUE.
118 Like `custom-initialize-reset', but only use the `:set' function if
119 not using the standard setting.
120 For the standard setting, use `set-default'."
121 (cond ((default-boundp symbol)
122 (funcall (or (get symbol 'custom-set) 'set-default)
123 symbol
124 (funcall (or (get symbol 'custom-get) 'default-value)
125 symbol)))
126 ((get symbol 'saved-value)
127 (funcall (or (get symbol 'custom-set) 'set-default)
128 symbol
129 (eval (car (get symbol 'saved-value)))))
131 (set-default symbol (eval value)))))
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 (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 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 load-file-name symbol) custom-current-group-alist)))
425 (run-hooks 'custom-define-hook)
426 symbol)
428 (defmacro defgroup (symbol members doc &rest args)
429 "Declare SYMBOL as a customization group containing MEMBERS.
430 SYMBOL does not need to be quoted.
432 Third arg DOC is the group documentation.
434 MEMBERS should be an alist of the form ((NAME WIDGET)...) where
435 NAME is a symbol and WIDGET is a widget for editing that symbol.
436 Useful widgets are `custom-variable' for editing variables,
437 `custom-face' for edit faces, and `custom-group' for editing groups.
439 The remaining arguments should have the form
441 [KEYWORD VALUE]...
443 For a list of valid keywords, see the common keywords listed in
444 `defcustom'.
446 See Info node `(elisp) Customization' in the Emacs Lisp manual
447 for more information."
448 (declare (doc-string 3))
449 ;; It is better not to use backquote in this file,
450 ;; because that makes a bootstrapping problem
451 ;; if you need to recompile all the Lisp files using interpreted code.
452 (nconc (list 'custom-declare-group (list 'quote symbol) members doc) args))
454 (defun custom-add-to-group (group option widget)
455 "To existing GROUP add a new OPTION of type WIDGET.
456 If there already is an entry for OPTION and WIDGET, nothing is done."
457 (let ((members (get group 'custom-group))
458 (entry (list option widget)))
459 (unless (member entry members)
460 (put group 'custom-group (nconc members (list entry))))))
462 (defun custom-group-of-mode (mode)
463 "Return the custom group corresponding to the major or minor MODE.
464 If no such group is found, return nil."
465 (or (get mode 'custom-mode-group)
466 (if (or (get mode 'custom-group)
467 (and (string-match "-mode\\'" (symbol-name mode))
468 (get (setq mode (intern (substring (symbol-name mode)
469 0 (match-beginning 0))))
470 'custom-group)))
471 mode)))
473 ;;; Properties.
475 (defun custom-handle-all-keywords (symbol args type)
476 "For customization option SYMBOL, handle keyword arguments ARGS.
477 Third argument TYPE is the custom option type."
478 (unless (memq :group args)
479 (custom-add-to-group (custom-current-group) symbol type))
480 (while args
481 (let ((arg (car args)))
482 (setq args (cdr args))
483 (unless (symbolp arg)
484 (error "Junk in args %S" args))
485 (let ((keyword arg)
486 (value (car args)))
487 (unless args
488 (error "Keyword %s is missing an argument" keyword))
489 (setq args (cdr args))
490 (custom-handle-keyword symbol keyword value type)))))
492 (defun custom-handle-keyword (symbol keyword value type)
493 "For customization option SYMBOL, handle KEYWORD with VALUE.
494 Fourth argument TYPE is the custom option type."
495 (if purify-flag
496 (setq value (purecopy value)))
497 (cond ((eq keyword :group)
498 (custom-add-to-group value symbol type))
499 ((eq keyword :version)
500 (custom-add-version symbol value))
501 ((eq keyword :package-version)
502 (custom-add-package-version symbol value))
503 ((eq keyword :link)
504 (custom-add-link symbol value))
505 ((eq keyword :load)
506 (custom-add-load symbol value))
507 ((eq keyword :tag)
508 (put symbol 'custom-tag value))
509 ((eq keyword :set-after)
510 (custom-add-dependencies symbol value))
512 (error "Unknown keyword %s" keyword))))
514 (defun custom-add-dependencies (symbol value)
515 "To the custom option SYMBOL, add dependencies specified by VALUE.
516 VALUE should be a list of symbols. For each symbol in that list,
517 this specifies that SYMBOL should be set after the specified symbol, if
518 both appear in constructs like `custom-set-variables'."
519 (unless (listp value)
520 (error "Invalid custom dependency `%s'" value))
521 (let* ((deps (get symbol 'custom-dependencies))
522 (new-deps deps))
523 (while value
524 (let ((dep (car value)))
525 (unless (symbolp dep)
526 (error "Invalid custom dependency `%s'" dep))
527 (unless (memq dep new-deps)
528 (setq new-deps (cons dep new-deps)))
529 (setq value (cdr value))))
530 (unless (eq deps new-deps)
531 (put symbol 'custom-dependencies new-deps))))
533 (defun custom-add-option (symbol option)
534 "To the variable SYMBOL add OPTION.
536 If SYMBOL's custom type is a hook, OPTION should be a hook member.
537 If SYMBOL's custom type is an alist, OPTION specifies a symbol
538 to offer to the user as a possible key in the alist.
539 For other custom types, this has no effect."
540 (let ((options (get symbol 'custom-options)))
541 (unless (member option options)
542 (put symbol 'custom-options (cons option options)))))
543 (defalias 'custom-add-frequent-value 'custom-add-option)
545 (defun custom-add-link (symbol widget)
546 "To the custom option SYMBOL add the link WIDGET."
547 (let ((links (get symbol 'custom-links)))
548 (unless (member widget links)
549 (put symbol 'custom-links (cons (purecopy widget) links)))))
551 (defun custom-add-version (symbol version)
552 "To the custom option SYMBOL add the version VERSION."
553 (put symbol 'custom-version (purecopy version)))
555 (defun custom-add-package-version (symbol version)
556 "To the custom option SYMBOL add the package version VERSION."
557 (put symbol 'custom-package-version (purecopy version)))
559 (defun custom-add-load (symbol load)
560 "To the custom option SYMBOL add the dependency LOAD.
561 LOAD should be either a library file name, or a feature name."
562 (let ((loads (get symbol 'custom-loads)))
563 (unless (member load loads)
564 (put symbol 'custom-loads (cons (purecopy load) loads)))))
566 (defun custom-autoload (symbol load &optional noset)
567 "Mark SYMBOL as autoloaded custom variable and add dependency LOAD.
568 If NOSET is non-nil, don't bother autoloading LOAD when setting the variable."
569 (put symbol 'custom-autoload (if noset 'noset t))
570 (custom-add-load symbol load))
572 ;; This test is also in the C code of `user-variable-p'.
573 (defun custom-variable-p (variable)
574 "Return non-nil if VARIABLE is a custom variable.
575 This recursively follows aliases."
576 (setq variable (indirect-variable variable))
577 (or (get variable 'standard-value)
578 (get variable 'custom-autoload)))
580 (defun custom-note-var-changed (variable)
581 "Inform Custom that VARIABLE has been set (changed).
582 VARIABLE is a symbol that names a user option.
583 The result is that the change is treated as having been made through Custom."
584 (put variable 'customized-value (list (custom-quote (eval variable)))))
587 ;;; Custom Themes
589 ;;; Loading files needed to customize a symbol.
590 ;;; This is in custom.el because menu-bar.el needs it for toggle cmds.
592 (defvar custom-load-recursion nil
593 "Hack to avoid recursive dependencies.")
595 (defun custom-load-symbol (symbol)
596 "Load all dependencies for SYMBOL."
597 (unless custom-load-recursion
598 (let ((custom-load-recursion t))
599 ;; Load these files if not already done,
600 ;; to make sure we know all the dependencies of SYMBOL.
601 (condition-case nil
602 (require 'cus-load)
603 (error nil))
604 (condition-case nil
605 (require 'cus-start)
606 (error nil))
607 (dolist (load (get symbol 'custom-loads))
608 (cond ((symbolp load) (condition-case nil (require load) (error nil)))
609 ;; This is subsumed by the test below, but it's much faster.
610 ((assoc load load-history))
611 ;; This was just (assoc (locate-library load) load-history)
612 ;; but has been optimized not to load locate-library
613 ;; if not necessary.
614 ((let ((regexp (concat "\\(\\`\\|/\\)" (regexp-quote load)
615 "\\(\\'\\|\\.\\)"))
616 (found nil))
617 (dolist (loaded load-history)
618 (and (stringp (car loaded))
619 (string-match regexp (car loaded))
620 (setq found t)))
621 found))
622 ;; Without this, we would load cus-edit recursively.
623 ;; We are still loading it when we call this,
624 ;; and it is not in load-history yet.
625 ((equal load "cus-edit"))
626 (t (condition-case nil (load load) (error nil))))))))
628 (defvar custom-local-buffer nil
629 "Non-nil, in a Customization buffer, means customize a specific buffer.
630 If this variable is non-nil, it should be a buffer,
631 and it means customize the local bindings of that buffer.
632 This variable is a permanent local, and it normally has a local binding
633 in every Customization buffer.")
634 (put 'custom-local-buffer 'permanent-local t)
636 (defun custom-set-default (variable value)
637 "Default :set function for a customizable variable.
638 Normally, this sets the default value of VARIABLE to VALUE,
639 but if `custom-local-buffer' is non-nil,
640 this sets the local binding in that buffer instead."
641 (if custom-local-buffer
642 (with-current-buffer custom-local-buffer
643 (set variable value))
644 (set-default variable value)))
646 (defun custom-set-minor-mode (variable value)
647 ":set function for minor mode variables.
648 Normally, this sets the default value of VARIABLE to nil if VALUE
649 is nil and to t otherwise,
650 but if `custom-local-buffer' is non-nil,
651 this sets the local binding in that buffer instead."
652 (if custom-local-buffer
653 (with-current-buffer custom-local-buffer
654 (funcall variable (if value 1 0)))
655 (funcall variable (if value 1 0))))
657 (defun custom-quote (sexp)
658 "Quote SEXP if it is not self quoting."
659 (if (or (memq sexp '(t nil))
660 (keywordp sexp)
661 (and (listp sexp)
662 (memq (car sexp) '(lambda)))
663 (stringp sexp)
664 (numberp sexp)
665 (vectorp sexp)
666 ;;; (and (fboundp 'characterp)
667 ;;; (characterp sexp))
669 sexp
670 (list 'quote sexp)))
672 (defun customize-mark-to-save (symbol)
673 "Mark SYMBOL for later saving.
675 If the default value of SYMBOL is different from the standard value,
676 set the `saved-value' property to a list whose car evaluates to the
677 default value. Otherwise, set it to nil.
679 To actually save the value, call `custom-save-all'.
681 Return non-nil if the `saved-value' property actually changed."
682 (custom-load-symbol symbol)
683 (let* ((get (or (get symbol 'custom-get) 'default-value))
684 (value (funcall get symbol))
685 (saved (get symbol 'saved-value))
686 (standard (get symbol 'standard-value))
687 (comment (get symbol 'customized-variable-comment)))
688 ;; Save default value if different from standard value.
689 (if (or (null standard)
690 (not (equal value (condition-case nil
691 (eval (car standard))
692 (error nil)))))
693 (put symbol 'saved-value (list (custom-quote value)))
694 (put symbol 'saved-value nil))
695 ;; Clear customized information (set, but not saved).
696 (put symbol 'customized-value nil)
697 ;; Save any comment that might have been set.
698 (when comment
699 (put symbol 'saved-variable-comment comment))
700 (not (equal saved (get symbol 'saved-value)))))
702 (defun customize-mark-as-set (symbol)
703 "Mark current value of SYMBOL as being set from customize.
705 If the default value of SYMBOL is different from the saved value if any,
706 or else if it is different from the standard value, set the
707 `customized-value' property to a list whose car evaluates to the
708 default value. Otherwise, set it to nil.
710 Return non-nil if the `customized-value' property actually changed."
711 (custom-load-symbol symbol)
712 (let* ((get (or (get symbol 'custom-get) 'default-value))
713 (value (funcall get symbol))
714 (customized (get symbol 'customized-value))
715 (old (or (get symbol 'saved-value) (get symbol 'standard-value))))
716 ;; Mark default value as set if different from old value.
717 (if (not (and old
718 (equal value (condition-case nil
719 (eval (car old))
720 (error nil)))))
721 (progn (put symbol 'customized-value (list (custom-quote value)))
722 (custom-push-theme 'theme-value symbol 'user 'set
723 (custom-quote value)))
724 (put symbol 'customized-value nil))
725 ;; Changed?
726 (not (equal customized (get symbol 'customized-value)))))
728 (defun custom-reevaluate-setting (symbol)
729 "Reset the value of SYMBOL by re-evaluating its saved or standard value.
730 Use the :set function to do so. This is useful for customizable options
731 that are defined before their standard value can really be computed.
732 E.g. dumped variables whose default depends on run-time information."
733 (funcall (or (get symbol 'custom-set) 'set-default)
734 symbol
735 (eval (car (or (get symbol 'saved-value) (get symbol 'standard-value))))))
738 ;;; Custom Themes
740 ;; Custom themes are collections of settings that can be enabled or
741 ;; disabled as a unit.
743 ;; Each Custom theme is defined by a symbol, called the theme name.
744 ;; The `theme-settings' property of the theme name records the
745 ;; variable and face settings of the theme. This property is a list
746 ;; of elements, each of the form
748 ;; (PROP SYMBOL THEME VALUE)
750 ;; - PROP is either `theme-value' or `theme-face'
751 ;; - SYMBOL is the face or variable name
752 ;; - THEME is the theme name (redundant, but simplifies the code)
753 ;; - VALUE is an expression that gives the theme's setting for SYMBOL.
755 ;; The theme name also has a `theme-feature' property, whose value is
756 ;; specified when the theme is defined (see `custom-declare-theme').
757 ;; Usually, this is just a symbol named THEME-theme. This lets
758 ;; external libraries call (require 'foo-theme).
760 ;; In addition, each symbol (either a variable or a face) affected by
761 ;; an *enabled* theme has a `theme-value' or `theme-face' property,
762 ;; which is a list of elements each of the form
764 ;; (THEME VALUE)
766 ;; which have the same meanings as in `theme-settings'.
768 ;; The `theme-value' and `theme-face' lists are ordered by decreasing
769 ;; theme precedence. Thus, the first element is always the one that
770 ;; is in effect.
772 ;; Each theme is stored in a theme file, with filename THEME-theme.el.
773 ;; Loading a theme basically involves calling (load "THEME-theme")
774 ;; This is done by the function `load-theme'. Loading a theme
775 ;; automatically enables it.
777 ;; When a theme is enabled, the `theme-value' and `theme-face'
778 ;; properties for the affected symbols are set. When a theme is
779 ;; disabled, its settings are removed from the `theme-value' and
780 ;; `theme-face' properties, but the theme's own `theme-settings'
781 ;; property remains unchanged.
783 (defvar custom-known-themes '(user changed)
784 "Themes that have been defined with `deftheme'.
785 The default value is the list (user changed). The theme `changed'
786 contains the settings before custom themes are applied. The
787 theme `user' contains all the settings the user customized and saved.
788 Additional themes declared with the `deftheme' macro will be added to
789 the front of this list.")
791 (defsubst custom-theme-p (theme)
792 "Non-nil when THEME has been defined."
793 (memq theme custom-known-themes))
795 (defsubst custom-check-theme (theme)
796 "Check whether THEME is valid, and signal an error if it is not."
797 (unless (custom-theme-p theme)
798 (error "Unknown theme `%s'" theme)))
800 (defun custom-push-theme (prop symbol theme mode &optional value)
801 "Record VALUE for face or variable SYMBOL in custom theme THEME.
802 PROP is `theme-face' for a face, `theme-value' for a variable.
804 MODE can be either the symbol `set' or the symbol `reset'. If it is the
805 symbol `set', then VALUE is the value to use. If it is the symbol
806 `reset', then SYMBOL will be removed from THEME (VALUE is ignored).
808 See `custom-known-themes' for a list of known themes."
809 (unless (memq prop '(theme-value theme-face))
810 (error "Unknown theme property"))
811 (let* ((old (get symbol prop))
812 (setting (assq theme old)) ; '(theme value)
813 (theme-settings ; '(prop symbol theme value)
814 (get theme 'theme-settings)))
815 (if (eq mode 'reset)
816 ;; Remove a setting.
817 (when setting
818 (let (res)
819 (dolist (theme-setting theme-settings)
820 (if (and (eq (car theme-setting) prop)
821 (eq (cadr theme-setting) symbol))
822 (setq res theme-setting)))
823 (put theme 'theme-settings (delq res theme-settings)))
824 (put symbol prop (delq setting old)))
825 (if setting
826 ;; Alter an existing setting.
827 (let (res)
828 (dolist (theme-setting theme-settings)
829 (if (and (eq (car theme-setting) prop)
830 (eq (cadr theme-setting) symbol))
831 (setq res theme-setting)))
832 (put theme 'theme-settings
833 (cons (list prop symbol theme value)
834 (delq res theme-settings)))
835 (setcar (cdr setting) value))
836 ;; Add a new setting.
837 ;; If the user changed the value outside of Customize, we
838 ;; first save the current value to a fake theme, `changed'.
839 ;; This ensures that the user-set value comes back if the
840 ;; theme is later disabled.
841 (if (null old)
842 (if (and (eq prop 'theme-value)
843 (boundp symbol))
844 (let ((sv (get symbol 'standard-value)))
845 (unless (and sv
846 (equal (eval (car sv)) (symbol-value symbol)))
847 (setq old (list (list 'changed (symbol-value symbol))))))
848 (if (and (facep symbol)
849 (not (face-spec-match-p symbol (get symbol 'face-defface-spec))))
850 (setq old (list (list 'changed (list
851 (append '(t) (custom-face-attributes-get symbol nil)))))))))
852 (put symbol prop (cons (list theme value) old))
853 (put theme 'theme-settings
854 (cons (list prop symbol theme value)
855 theme-settings))))))
858 (defun custom-set-variables (&rest args)
859 "Install user customizations of variable values specified in ARGS.
860 These settings are registered as theme `user'.
861 The arguments should each be a list of the form:
863 (SYMBOL EXP [NOW [REQUEST [COMMENT]]])
865 This stores EXP (without evaluating it) as the saved value for SYMBOL.
866 If NOW is present and non-nil, then also evaluate EXP and set
867 the default value for the SYMBOL to the value of EXP.
869 REQUEST is a list of features we must require in order to
870 handle SYMBOL properly.
871 COMMENT is a comment string about SYMBOL."
872 (apply 'custom-theme-set-variables 'user args))
874 (defun custom-theme-set-variables (theme &rest args)
875 "Initialize variables for theme THEME according to settings in ARGS.
876 Each of the arguments in ARGS should be a list of this form:
878 (SYMBOL EXP [NOW [REQUEST [COMMENT]]])
880 This stores EXP (without evaluating it) as the saved value for SYMBOL.
881 If NOW is present and non-nil, then also evaluate EXP and set
882 the default value for the SYMBOL to the value of EXP.
884 REQUEST is a list of features we must require in order to
885 handle SYMBOL properly.
886 COMMENT is a comment string about SYMBOL.
888 EXP itself is saved unevaluated as SYMBOL property `saved-value' and
889 in SYMBOL's list property `theme-value' \(using `custom-push-theme')."
890 (custom-check-theme theme)
892 ;; Process all the needed autoloads before anything else, so that the
893 ;; subsequent code has all the info it needs (e.g. which var corresponds
894 ;; to a minor mode), regardless of the ordering of the variables.
895 (dolist (entry args)
896 (let* ((symbol (indirect-variable (nth 0 entry))))
897 (unless (or (get symbol 'standard-value)
898 (memq (get symbol 'custom-autoload) '(nil noset)))
899 ;; This symbol needs to be autoloaded, even just for a `set'.
900 (custom-load-symbol symbol))))
902 ;; Move minor modes and variables with explicit requires to the end.
903 (setq args
904 (sort args
905 (lambda (a1 a2)
906 (let* ((sym1 (car a1))
907 (sym2 (car a2))
908 (1-then-2 (memq sym1 (get sym2 'custom-dependencies)))
909 (2-then-1 (memq sym2 (get sym1 'custom-dependencies))))
910 (cond ((and 1-then-2 2-then-1)
911 (error "Circular custom dependency between `%s' and `%s'"
912 sym1 sym2))
913 (2-then-1 nil)
914 ;; 1 is a dependency of 2, so needs to be set first.
915 (1-then-2)
916 ;; Put minor modes and symbols with :require last.
917 ;; Putting minor modes last ensures that the mode
918 ;; function will see other customized values rather
919 ;; than default values.
920 (t (or (nth 3 a2)
921 (eq (get sym2 'custom-set)
922 'custom-set-minor-mode))))))))
923 (while args
924 (let ((entry (car args)))
925 (if (listp entry)
926 (let* ((symbol (indirect-variable (nth 0 entry)))
927 (value (nth 1 entry))
928 (now (nth 2 entry))
929 (requests (nth 3 entry))
930 (comment (nth 4 entry))
931 set)
932 (when requests
933 (put symbol 'custom-requests requests)
934 (mapc 'require requests))
935 (setq set (or (get symbol 'custom-set) 'custom-set-default))
936 (put symbol 'saved-value (list value))
937 (put symbol 'saved-variable-comment comment)
938 (custom-push-theme 'theme-value symbol theme 'set value)
939 ;; Allow for errors in the case where the setter has
940 ;; changed between versions, say, but let the user know.
941 (condition-case data
942 (cond (now
943 ;; Rogue variable, set it now.
944 (put symbol 'force-value t)
945 (funcall set symbol (eval value)))
946 ((default-boundp symbol)
947 ;; Something already set this, overwrite it.
948 (funcall set symbol (eval value))))
949 (error
950 (message "Error setting %s: %s" symbol data)))
951 (setq args (cdr args))
952 (and (or now (default-boundp symbol))
953 (put symbol 'variable-comment comment)))
954 ;; I believe this is dead-code, because the `sort' code above would
955 ;; have burped before we could get here. --Stef
956 ;; Old format, a plist of SYMBOL VALUE pairs.
957 (message "Warning: old format `custom-set-variables'")
958 (ding)
959 (sit-for 2)
960 (let ((symbol (indirect-variable (nth 0 args)))
961 (value (nth 1 args)))
962 (put symbol 'saved-value (list value))
963 (custom-push-theme 'theme-value symbol theme 'set value))
964 (setq args (cdr (cdr args)))))))
967 ;;; Defining themes.
969 ;; A theme file should be named `THEME-theme.el' (where THEME is the theme
970 ;; name), and found in either `custom-theme-directory' or the load path.
971 ;; It has the following format:
973 ;; (deftheme THEME
974 ;; DOCSTRING)
976 ;; (custom-theme-set-variables
977 ;; 'THEME
978 ;; [THEME-VARIABLES])
980 ;; (custom-theme-set-faces
981 ;; 'THEME
982 ;; [THEME-FACES])
984 ;; (provide-theme 'THEME)
987 ;; The IGNORED arguments to deftheme come from the XEmacs theme code, where
988 ;; they were used to supply keyword-value pairs like `:immediate',
989 ;; `:variable-reset-string', etc. We don't use any of these, so ignore them.
991 (defmacro deftheme (theme &optional doc &rest ignored)
992 "Declare THEME to be a Custom theme.
993 The optional argument DOC is a doc string describing the theme.
995 Any theme `foo' should be defined in a file called `foo-theme.el';
996 see `custom-make-theme-feature' for more information."
997 (let ((feature (custom-make-theme-feature theme)))
998 ;; It is better not to use backquote in this file,
999 ;; because that makes a bootstrapping problem
1000 ;; if you need to recompile all the Lisp files using interpreted code.
1001 (list 'custom-declare-theme (list 'quote theme) (list 'quote feature) doc)))
1003 (defun custom-declare-theme (theme feature &optional doc &rest ignored)
1004 "Like `deftheme', but THEME is evaluated as a normal argument.
1005 FEATURE is the feature this theme provides. Normally, this is a symbol
1006 created from THEME by `custom-make-theme-feature'."
1007 (if (memq theme '(user changed))
1008 (error "Custom theme cannot be named %S" theme))
1009 (add-to-list 'custom-known-themes theme)
1010 (put theme 'theme-feature feature)
1011 (when doc (put theme 'theme-documentation doc)))
1013 (defun custom-make-theme-feature (theme)
1014 "Given a symbol THEME, create a new symbol by appending \"-theme\".
1015 Store this symbol in the `theme-feature' property of THEME.
1016 Calling `provide-theme' to provide THEME actually puts `THEME-theme'
1017 into `features'.
1019 This allows for a file-name convention for autoloading themes:
1020 Every theme X has a property `provide-theme' whose value is \"X-theme\".
1021 \(load-theme X) then attempts to load the file `X-theme.el'."
1022 (intern (concat (symbol-name theme) "-theme")))
1024 ;;; Loading themes.
1026 (defcustom custom-theme-directory
1027 user-emacs-directory
1028 "Directory in which Custom theme files should be written.
1029 `load-theme' searches this directory in addition to load-path.
1030 The command `customize-create-theme' writes the files it produces
1031 into this directory."
1032 :type 'string
1033 :group 'customize
1034 :version "22.1")
1036 (defun provide-theme (theme)
1037 "Indicate that this file provides THEME.
1038 This calls `provide' to provide the feature name stored in THEME's
1039 property `theme-feature' (which is usually a symbol created by
1040 `custom-make-theme-feature')."
1041 (if (memq theme '(user changed))
1042 (error "Custom theme cannot be named %S" theme))
1043 (custom-check-theme theme)
1044 (provide (get theme 'theme-feature))
1045 ;; Loading a theme also enables it.
1046 (push theme custom-enabled-themes)
1047 ;; `user' must always be the highest-precedence enabled theme.
1048 ;; Make that remain true. (This has the effect of making user settings
1049 ;; override the ones just loaded, too.)
1050 (let ((custom-enabling-themes t))
1051 (enable-theme 'user)))
1053 (defun load-theme (theme)
1054 "Load a theme's settings from its file.
1055 This also enables the theme; use `disable-theme' to disable it."
1056 ;; Note we do no check for validity of the theme here.
1057 ;; This allows to pull in themes by a file-name convention
1058 (interactive "SCustom theme name: ")
1059 ;; If reloading, clear out the old theme settings.
1060 (when (custom-theme-p theme)
1061 (disable-theme theme)
1062 (put theme 'theme-settings nil)
1063 (put theme 'theme-feature nil)
1064 (put theme 'theme-documentation nil))
1065 (let ((load-path (if (file-directory-p custom-theme-directory)
1066 (cons custom-theme-directory load-path)
1067 load-path)))
1068 (load (symbol-name (custom-make-theme-feature theme)))))
1070 ;;; Enabling and disabling loaded themes.
1072 (defvar custom-enabling-themes nil)
1074 (defun enable-theme (theme)
1075 "Reenable all variable and face settings defined by THEME.
1076 The newly enabled theme gets the highest precedence (after `user').
1077 If it is already enabled, just give it highest precedence (after `user').
1079 If THEME does not specify any theme settings, this tries to load
1080 the theme from its theme file, by calling `load-theme'."
1081 (interactive "SEnable Custom theme: ")
1082 (if (not (custom-theme-p theme))
1083 (load-theme theme)
1084 ;; This could use a bit of optimization -- cyd
1085 (let ((settings (get theme 'theme-settings)))
1086 (dolist (s settings)
1087 (let* ((prop (car s))
1088 (symbol (cadr s))
1089 (spec-list (get symbol prop)))
1090 (put symbol prop (cons (cddr s) (assq-delete-all theme spec-list)))
1091 (if (eq prop 'theme-value)
1092 (custom-theme-recalc-variable symbol)
1093 (custom-theme-recalc-face symbol)))))
1094 (unless (eq theme 'user)
1095 (setq custom-enabled-themes
1096 (cons theme (delq theme custom-enabled-themes)))
1097 (unless custom-enabling-themes
1098 (enable-theme 'user)))))
1100 (defcustom custom-enabled-themes nil
1101 "List of enabled Custom Themes, highest precedence first.
1103 This does not include the `user' theme, which is set by Customize,
1104 and always takes precedence over other Custom Themes."
1105 :group 'customize
1106 :type '(repeat symbol)
1107 :set-after '(custom-theme-directory) ; so we can find the themes
1108 :set (lambda (symbol themes)
1109 ;; Avoid an infinite loop when custom-enabled-themes is
1110 ;; defined in a theme (e.g. `user'). Enabling the theme sets
1111 ;; custom-enabled-themes, which enables the theme...
1112 (unless custom-enabling-themes
1113 (let ((custom-enabling-themes t) failures)
1114 (setq themes (delq 'user (delete-dups themes)))
1115 (if (boundp symbol)
1116 (dolist (theme (symbol-value symbol))
1117 (if (not (memq theme themes))
1118 (disable-theme theme))))
1119 (dolist (theme (reverse themes))
1120 (condition-case nil
1121 (enable-theme theme)
1122 (error (progn (push theme failures)
1123 (setq themes (delq theme themes))))))
1124 (enable-theme 'user)
1125 (custom-set-default symbol themes)
1126 (if failures
1127 (message "Failed to enable themes: %s"
1128 (mapconcat 'symbol-name failures " ")))))))
1130 (defsubst custom-theme-enabled-p (theme)
1131 "Return non-nil if THEME is enabled."
1132 (memq theme custom-enabled-themes))
1134 (defun disable-theme (theme)
1135 "Disable all variable and face settings defined by THEME.
1136 See `custom-enabled-themes' for a list of enabled themes."
1137 (interactive (list (intern
1138 (completing-read
1139 "Disable Custom theme: "
1140 (mapcar 'symbol-name custom-enabled-themes)
1141 nil t))))
1142 (when (custom-theme-enabled-p theme)
1143 (let ((settings (get theme 'theme-settings)))
1144 (dolist (s settings)
1145 (let* ((prop (car s))
1146 (symbol (cadr s))
1147 (spec-list (get symbol prop)))
1148 (put symbol prop (assq-delete-all theme spec-list))
1149 (if (eq prop 'theme-value)
1150 (custom-theme-recalc-variable symbol)
1151 (custom-theme-recalc-face symbol)))))
1152 (setq custom-enabled-themes
1153 (delq theme custom-enabled-themes))))
1155 (defun custom-variable-theme-value (variable)
1156 "Return (list VALUE) indicating the custom theme value of VARIABLE.
1157 That is to say, it specifies what the value should be according to
1158 currently enabled custom themes.
1160 This function returns nil if no custom theme specifies a value for VARIABLE."
1161 (let* ((theme-value (get variable 'theme-value)))
1162 (if theme-value
1163 (cdr (car theme-value)))))
1165 (defun custom-theme-recalc-variable (variable)
1166 "Set VARIABLE according to currently enabled custom themes."
1167 (let ((valspec (custom-variable-theme-value variable)))
1168 (if valspec
1169 (put variable 'saved-value valspec)
1170 (setq valspec (get variable 'standard-value)))
1171 (if (and valspec
1172 (or (get variable 'force-value)
1173 (default-boundp variable)))
1174 (funcall (or (get variable 'custom-set) 'set-default) variable
1175 (eval (car valspec))))))
1177 (defun custom-theme-recalc-face (face)
1178 "Set FACE according to currently enabled custom themes."
1179 (if (facep face)
1180 (face-spec-set face
1181 (get (or (get face 'face-alias) face)
1182 'face-override-spec))))
1184 ;;; XEmacs compability functions
1186 ;; In XEmacs, when you reset a Custom Theme, you have to specify the
1187 ;; theme to reset it to. We just apply the next available theme, so
1188 ;; just ignore the IGNORED arguments.
1190 (defun custom-theme-reset-variables (theme &rest args)
1191 "Reset some variable settings in THEME to their values in other themes.
1192 Each of the arguments ARGS has this form:
1194 (VARIABLE IGNORED)
1196 This means reset VARIABLE. (The argument IGNORED is ignored)."
1197 (custom-check-theme theme)
1198 (dolist (arg args)
1199 (custom-push-theme 'theme-value (car arg) theme 'reset)))
1201 (defun custom-reset-variables (&rest args)
1202 "Reset the specs of some variables to their values in other themes.
1203 This creates settings in the `user' theme.
1205 Each of the arguments ARGS has this form:
1207 (VARIABLE IGNORED)
1209 This means reset VARIABLE. (The argument IGNORED is ignored)."
1210 (apply 'custom-theme-reset-variables 'user args))
1212 ;;; The End.
1214 ;; Process the defcustoms for variables loaded before this file.
1215 (while custom-declare-variable-list
1216 (apply 'custom-declare-variable (car custom-declare-variable-list))
1217 (setq custom-declare-variable-list (cdr custom-declare-variable-list)))
1219 (provide 'custom)
1221 ;; arch-tag: 041b6116-aabe-4f9a-902d-74092bc3dab2
1222 ;;; custom.el ends here