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