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