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