]> code.delx.au - gnu-emacs/blob - lisp/custom.el
(custom-load-symbol): Load cus-load and cus-start first.
[gnu-emacs] / lisp / custom.el
1 ;;; custom.el --- tools for declaring and initializing options
2 ;;
3 ;; Copyright (C) 1996, 1997, 1999, 2001, 2002 Free Software Foundation, Inc.
4 ;;
5 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
6 ;; Maintainer: FSF
7 ;; Keywords: help, faces
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27 ;;
28 ;; This file only contains the code needed to declare and initialize
29 ;; user options. The code to customize options is autoloaded from
30 ;; `cus-edit.el' and is documented in the Emacs Lisp Reference manual.
31
32 ;; The code implementing face declarations is in `cus-face.el'.
33
34 ;;; Code:
35
36 (require 'widget)
37
38 (defvar custom-define-hook nil
39 ;; Customize information for this option is in `cus-edit.el'.
40 "Hook called after defining each customize option.")
41
42 (defvar custom-dont-initialize nil
43 "Non-nil means `defcustom' should not initialize the variable.
44 That is used for the sake of `custom-make-dependencies'.
45 Users should not set it.")
46
47 (defvar custom-current-group-alist nil
48 "Alist of (FILE . GROUP) indicating the current group to use for FILE.")
49
50 ;;; The `defcustom' Macro.
51
52 (defun custom-initialize-default (symbol value)
53 "Initialize SYMBOL with VALUE.
54 This will do nothing if symbol already has a default binding.
55 Otherwise, if symbol has a `saved-value' property, it will evaluate
56 the car of that and used as the default binding for symbol.
57 Otherwise, VALUE will be evaluated and used as the default binding for
58 symbol."
59 (unless (default-boundp symbol)
60 ;; Use the saved value if it exists, otherwise the standard setting.
61 (set-default symbol (if (get symbol 'saved-value)
62 (eval (car (get symbol 'saved-value)))
63 (eval value)))))
64
65 (defun custom-initialize-set (symbol value)
66 "Initialize SYMBOL based on VALUE.
67 If the symbol doesn't have a default binding already,
68 then set it using its `:set' function (or `set-default' if it has none).
69 The value is either the value in the symbol's `saved-value' property,
70 if any, or VALUE."
71 (unless (default-boundp symbol)
72 (funcall (or (get symbol 'custom-set) 'set-default)
73 symbol
74 (if (get symbol 'saved-value)
75 (eval (car (get symbol 'saved-value)))
76 (eval value)))))
77
78 (defun custom-initialize-reset (symbol value)
79 "Initialize SYMBOL based on VALUE.
80 Set the symbol, using its `:set' function (or `set-default' if it has none).
81 The value is either the symbol's current value
82 \(as obtained using the `:get' function), if any,
83 or the value in the symbol's `saved-value' property if any,
84 or (last of all) VALUE."
85 (funcall (or (get symbol 'custom-set) 'set-default)
86 symbol
87 (cond ((default-boundp symbol)
88 (funcall (or (get symbol 'custom-get) 'default-value)
89 symbol))
90 ((get symbol 'saved-value)
91 (eval (car (get symbol 'saved-value))))
92 (t
93 (eval value)))))
94
95 (defun custom-initialize-changed (symbol value)
96 "Initialize SYMBOL with VALUE.
97 Like `custom-initialize-reset', but only use the `:set' function if
98 not using the standard setting.
99 For the standard setting, use `set-default'."
100 (cond ((default-boundp symbol)
101 (funcall (or (get symbol 'custom-set) 'set-default)
102 symbol
103 (funcall (or (get symbol 'custom-get) 'default-value)
104 symbol)))
105 ((get symbol 'saved-value)
106 (funcall (or (get symbol 'custom-set) 'set-default)
107 symbol
108 (eval (car (get symbol 'saved-value)))))
109 (t
110 (set-default symbol (eval value)))))
111
112 (defun custom-declare-variable (symbol default doc &rest args)
113 "Like `defcustom', but SYMBOL and DEFAULT are evaluated as normal arguments.
114 DEFAULT should be an expression to evaluate to compute the default value,
115 not the default value itself.
116
117 DEFAULT is stored as SYMBOL's value in the standard theme. See
118 `custom-known-themes' for a list of known themes. For backwards
119 compatibility, DEFAULT is also stored in SYMBOL's property
120 `standard-value'. At the same time, SYMBOL's property `force-value' is
121 set to nil, as the value is no longer rogue."
122 ;; Remember the standard setting. The value should be in the standard
123 ;; theme, not in this property. However, his would require changeing
124 ;; the C source of defvar and others as well...
125 (put symbol 'standard-value (list default))
126 ;; Maybe this option was rogue in an earlier version. It no longer is.
127 (when (get symbol 'force-value)
128 (put symbol 'force-value nil))
129 (when doc
130 (put symbol 'variable-documentation doc))
131 (let ((initialize 'custom-initialize-reset)
132 (requests nil))
133 (unless (memq :group args)
134 (custom-add-to-group (custom-current-group) symbol 'custom-variable))
135 (while args
136 (let ((arg (car args)))
137 (setq args (cdr args))
138 (unless (symbolp arg)
139 (error "Junk in args %S" args))
140 (let ((keyword arg)
141 (value (car args)))
142 (unless args
143 (error "Keyword %s is missing an argument" keyword))
144 (setq args (cdr args))
145 (cond ((eq keyword :initialize)
146 (setq initialize value))
147 ((eq keyword :set)
148 (put symbol 'custom-set value))
149 ((eq keyword :get)
150 (put symbol 'custom-get value))
151 ((eq keyword :require)
152 (push value requests))
153 ((eq keyword :type)
154 (put symbol 'custom-type (purecopy value)))
155 ((eq keyword :options)
156 (if (get symbol 'custom-options)
157 ;; Slow safe code to avoid duplicates.
158 (mapc (lambda (option)
159 (custom-add-option symbol option))
160 value)
161 ;; Fast code for the common case.
162 (put symbol 'custom-options (copy-sequence value))))
163 (t
164 (custom-handle-keyword symbol keyword value
165 'custom-variable))))))
166 (put symbol 'custom-requests requests)
167 ;; Do the actual initialization.
168 (unless custom-dont-initialize
169 (funcall initialize symbol default)))
170 (push (cons 'defvar symbol) current-load-list)
171 (run-hooks 'custom-define-hook)
172 symbol)
173
174 (defmacro defcustom (symbol value doc &rest args)
175 "Declare SYMBOL as a customizable variable that defaults to VALUE.
176 DOC is the variable documentation.
177
178 Neither SYMBOL nor VALUE needs to be quoted.
179 If SYMBOL is not already bound, initialize it to VALUE.
180 The remaining arguments should have the form
181
182 [KEYWORD VALUE]...
183
184 The following keywords are meaningful:
185
186 :type VALUE should be a widget type for editing the symbol's value.
187 :options VALUE should be a list of valid members of the widget type.
188 :group VALUE should be a customization group.
189 Add SYMBOL to that group.
190 :link LINK-DATA
191 Include an external link after the documentation string for this
192 item. This is a sentence containing an active field which
193 references some other documentation.
194
195 There are three alternatives you can use for LINK-DATA:
196
197 (custom-manual INFO-NODE)
198 Link to an Info node; INFO-NODE is a string which specifies
199 the node name, as in \"(emacs)Top\". The link appears as
200 `[manual]' in the customization buffer.
201
202 (info-link INFO-NODE)
203 Like `custom-manual' except that the link appears in the
204 customization buffer with the Info node name.
205
206 (url-link URL)
207 Link to a web page; URL is a string which specifies the URL.
208 The link appears in the customization buffer as URL.
209
210 You can specify the text to use in the customization buffer by
211 adding `:tag NAME' after the first element of the LINK-DATA; for
212 example, (info-link :tag \"foo\" \"(emacs)Top\") makes a link to the
213 Emacs manual which appears in the buffer as `foo'.
214
215 An item can have more than one external link; however, most items
216 have none at all.
217 :initialize
218 VALUE should be a function used to initialize the
219 variable. It takes two arguments, the symbol and value
220 given in the `defcustom' call. The default is
221 `custom-initialize-reset'.
222 :set VALUE should be a function to set the value of the symbol.
223 It takes two arguments, the symbol to set and the value to
224 give it. The default choice of function is `custom-set-default'.
225 :get VALUE should be a function to extract the value of symbol.
226 The function takes one argument, a symbol, and should return
227 the current value for that symbol. The default choice of function
228 is `custom-default-value'.
229 :require
230 VALUE should be a feature symbol. If you save a value
231 for this option, then when your `.emacs' file loads the value,
232 it does (require VALUE) first.
233 :version
234 VALUE should be a string specifying that the variable was
235 first introduced, or its default value was changed, in Emacs
236 version VERSION.
237 :tag LABEL
238 Use LABEL, a string, instead of the item's name, to label the item
239 in customization menus and buffers.
240 :load FILE
241 Load file FILE (a string) before displaying this customization
242 item. Loading is done with `load', and only if the file is
243 not already loaded.
244 :set-after VARIABLES
245 Specifies that SYMBOL should be set after the list of variables
246 VARIABLES when both have been customized.
247
248 Read the section about customization in the Emacs Lisp manual for more
249 information."
250 ;; It is better not to use backquote in this file,
251 ;; because that makes a bootstrapping problem
252 ;; if you need to recompile all the Lisp files using interpreted code.
253 (nconc (list 'custom-declare-variable
254 (list 'quote symbol)
255 (list 'quote value)
256 doc)
257 args))
258
259 ;;; The `defface' Macro.
260
261 (defmacro defface (face spec doc &rest args)
262 "Declare FACE as a customizable face that defaults to SPEC.
263 FACE does not need to be quoted.
264
265 Third argument DOC is the face documentation.
266
267 If FACE has been set with `custom-set-face', set the face attributes
268 as specified by that function, otherwise set the face attributes
269 according to SPEC.
270
271 The remaining arguments should have the form
272
273 [KEYWORD VALUE]...
274
275 The following KEYWORDs are defined:
276
277 :group VALUE should be a customization group.
278 Add FACE to that group.
279
280 SPEC should be an alist of the form ((DISPLAY ATTS)...).
281
282 The first element of SPEC where the DISPLAY matches the frame
283 is the one that takes effect in that frame. The ATTRs in this
284 element take effect; the other elements are ignored, on that frame.
285
286 ATTS is a list of face attributes followed by their values:
287 (ATTR VALUE ATTR VALUE...)
288
289 The possible attributes are `:family', `:width', `:height', `:weight',
290 `:slant', `:underline', `:overline', `:strike-through', `:box',
291 `:foreground', `:background', `:stipple', `:inverse-video', and `:inherit'.
292
293 DISPLAY can either be the symbol t, which will match all frames, or an
294 alist of the form \((REQ ITEM...)...). For the DISPLAY to match a
295 FRAME, the REQ property of the frame must match one of the ITEM. The
296 following REQ are defined:
297
298 `type' (the value of `window-system')
299 Under X, in addition to the values `window-system' can take,
300 `motif', `lucid' and `x-toolkit' are allowed, and match when
301 the Motif toolkit, Lucid toolkit, or any X toolkit is in use.
302
303 `class' (the frame's color support)
304 Should be one of `color', `grayscale', or `mono'.
305
306 `background' (what color is used for the background text)
307 Should be one of `light' or `dark'.
308
309 Read the section about customization in the Emacs Lisp manual for more
310 information."
311 ;; It is better not to use backquote in this file,
312 ;; because that makes a bootstrapping problem
313 ;; if you need to recompile all the Lisp files using interpreted code.
314 (nconc (list 'custom-declare-face (list 'quote face) spec doc) args))
315
316 ;;; The `defgroup' Macro.
317
318 (defun custom-current-group ()
319 (cdr (assoc load-file-name custom-current-group-alist)))
320
321 (defun custom-declare-group (symbol members doc &rest args)
322 "Like `defgroup', but SYMBOL is evaluated as a normal argument."
323 (while members
324 (apply 'custom-add-to-group symbol (car members))
325 (setq members (cdr members)))
326 (when doc
327 ;; This text doesn't get into DOC.
328 (put symbol 'group-documentation (purecopy doc)))
329 (while args
330 (let ((arg (car args)))
331 (setq args (cdr args))
332 (unless (symbolp arg)
333 (error "Junk in args %S" args))
334 (let ((keyword arg)
335 (value (car args)))
336 (unless args
337 (error "Keyword %s is missing an argument" keyword))
338 (setq args (cdr args))
339 (cond ((eq keyword :prefix)
340 (put symbol 'custom-prefix value))
341 (t
342 (custom-handle-keyword symbol keyword value
343 'custom-group))))))
344 ;; Record the group on the `current' list.
345 (let ((elt (assoc load-file-name custom-current-group-alist)))
346 (if elt (setcdr elt symbol)
347 (push (cons load-file-name symbol) custom-current-group-alist)))
348 (run-hooks 'custom-define-hook)
349 symbol)
350
351 (defmacro defgroup (symbol members doc &rest args)
352 "Declare SYMBOL as a customization group containing MEMBERS.
353 SYMBOL does not need to be quoted.
354
355 Third arg DOC is the group documentation.
356
357 MEMBERS should be an alist of the form ((NAME WIDGET)...) where
358 NAME is a symbol and WIDGET is a widget for editing that symbol.
359 Useful widgets are `custom-variable' for editing variables,
360 `custom-face' for edit faces, and `custom-group' for editing groups.
361
362 The remaining arguments should have the form
363
364 [KEYWORD VALUE]...
365
366 The following KEYWORDs are defined:
367
368 :group VALUE should be a customization group.
369 Add SYMBOL to that group.
370
371 :version VALUE should be a string specifying that the group was introduced
372 in Emacs version VERSION.
373
374 Read the section about customization in the Emacs Lisp manual for more
375 information."
376 ;; It is better not to use backquote in this file,
377 ;; because that makes a bootstrapping problem
378 ;; if you need to recompile all the Lisp files using interpreted code.
379 (nconc (list 'custom-declare-group (list 'quote symbol) members doc) args))
380
381 (defun custom-add-to-group (group option widget)
382 "To existing GROUP add a new OPTION of type WIDGET.
383 If there already is an entry for OPTION and WIDGET, nothing is done."
384 (let ((members (get group 'custom-group))
385 (entry (list option widget)))
386 (unless (member entry members)
387 (put group 'custom-group (nconc members (list entry))))))
388
389 (defun custom-group-of-mode (mode)
390 "Return the custom group corresponding to the major or minor MODE.
391 If no such group is found, return nil."
392 (or (get mode 'custom-mode-group)
393 (if (or (get mode 'custom-group)
394 (and (string-match "-mode\\'" (symbol-name mode))
395 (get (setq mode (intern (substring (symbol-name mode)
396 0 (match-beginning 0))))
397 'custom-group)))
398 mode)))
399
400 ;;; Properties.
401
402 (defun custom-handle-all-keywords (symbol args type)
403 "For customization option SYMBOL, handle keyword arguments ARGS.
404 Third argument TYPE is the custom option type."
405 (unless (memq :group args)
406 (custom-add-to-group (custom-current-group) symbol type))
407 (while args
408 (let ((arg (car args)))
409 (setq args (cdr args))
410 (unless (symbolp arg)
411 (error "Junk in args %S" args))
412 (let ((keyword arg)
413 (value (car args)))
414 (unless args
415 (error "Keyword %s is missing an argument" keyword))
416 (setq args (cdr args))
417 (custom-handle-keyword symbol keyword value type)))))
418
419 (defun custom-handle-keyword (symbol keyword value type)
420 "For customization option SYMBOL, handle KEYWORD with VALUE.
421 Fourth argument TYPE is the custom option type."
422 (if purify-flag
423 (setq value (purecopy value)))
424 (cond ((eq keyword :group)
425 (custom-add-to-group value symbol type))
426 ((eq keyword :version)
427 (custom-add-version symbol value))
428 ((eq keyword :link)
429 (custom-add-link symbol value))
430 ((eq keyword :load)
431 (custom-add-load symbol value))
432 ((eq keyword :tag)
433 (put symbol 'custom-tag value))
434 ((eq keyword :set-after)
435 (custom-add-dependencies symbol value))
436 (t
437 (error "Unknown keyword %s" keyword))))
438
439 (defun custom-add-dependencies (symbol value)
440 "To the custom option SYMBOL, add dependencies specified by VALUE.
441 VALUE should be a list of symbols. For each symbol in that list,
442 this specifies that SYMBOL should be set after the specified symbol, if
443 both appear in constructs like `custom-set-variables'."
444 (unless (listp value)
445 (error "Invalid custom dependency `%s'" value))
446 (let* ((deps (get symbol 'custom-dependencies))
447 (new-deps deps))
448 (while value
449 (let ((dep (car value)))
450 (unless (symbolp dep)
451 (error "Invalid custom dependency `%s'" dep))
452 (unless (memq dep new-deps)
453 (setq new-deps (cons dep new-deps)))
454 (setq value (cdr value))))
455 (unless (eq deps new-deps)
456 (put symbol 'custom-dependencies new-deps))))
457
458 (defun custom-add-option (symbol option)
459 "To the variable SYMBOL add OPTION.
460
461 If SYMBOL is a hook variable, OPTION should be a hook member.
462 For other types variables, the effect is undefined."
463 (let ((options (get symbol 'custom-options)))
464 (unless (member option options)
465 (put symbol 'custom-options (cons option options)))))
466
467 (defun custom-add-link (symbol widget)
468 "To the custom option SYMBOL add the link WIDGET."
469 (let ((links (get symbol 'custom-links)))
470 (unless (member widget links)
471 (put symbol 'custom-links (cons (purecopy widget) links)))))
472
473 (defun custom-add-version (symbol version)
474 "To the custom option SYMBOL add the version VERSION."
475 (put symbol 'custom-version (purecopy version)))
476
477 (defun custom-add-load (symbol load)
478 "To the custom option SYMBOL add the dependency LOAD.
479 LOAD should be either a library file name, or a feature name."
480 (let ((loads (get symbol 'custom-loads)))
481 (unless (member load loads)
482 (put symbol 'custom-loads (cons (purecopy load) loads)))))
483
484 (defun custom-autoload (symbol load)
485 "Mark SYMBOL as autoloaded custom variable and add dependency LOAD."
486 (put symbol 'custom-autoload t)
487 (custom-add-load symbol load))
488
489 ;; This test is also in the C code of `user-variable-p'.
490 (defun custom-variable-p (variable)
491 "Return non-nil if VARIABLE is a custom variable."
492 (or (get variable 'standard-value)
493 (get variable 'custom-autoload)))
494
495 ;;; Loading files needed to customize a symbol.
496 ;;; This is in custom.el because menu-bar.el needs it for toggle cmds.
497
498 (defvar custom-load-recursion nil
499 "Hack to avoid recursive dependencies.")
500
501 (defun custom-load-symbol (symbol)
502 "Load all dependencies for SYMBOL."
503 (unless custom-load-recursion
504 (let ((custom-load-recursion t))
505 ;; Load these files if not already done,
506 ;; to make sure we know all the dependencies of SYMBOL.
507 (condition-case nil
508 (require 'cus-load)
509 (error nil))
510 (condition-case nil
511 (require 'cus-start)
512 (error nil))
513 (dolist (load (get symbol 'custom-loads))
514 (cond ((symbolp load) (condition-case nil (require load) (error nil)))
515 ;; This is subsumed by the test below, but it's much faster.
516 ((assoc load load-history))
517 ;; This was just (assoc (locate-library load) load-history)
518 ;; but has been optimized not to load locate-library
519 ;; if not necessary.
520 ((let ((regexp (concat "\\(\\`\\|/\\)" (regexp-quote load)
521 "\\(\\'\\|\\.\\)"))
522 (found nil))
523 (dolist (loaded load-history)
524 (and (stringp (car loaded))
525 (string-match regexp (car loaded))
526 (setq found t)))
527 found))
528 ;; Without this, we would load cus-edit recursively.
529 ;; We are still loading it when we call this,
530 ;; and it is not in load-history yet.
531 ((equal load "cus-edit"))
532 (t (condition-case nil (load load) (error nil))))))))
533
534 (defvar custom-known-themes '(user standard)
535 "Themes that have been define with `deftheme'.
536 The default value is the list (user standard). The theme `standard'
537 contains the Emacs standard settings from the original Lisp files. The
538 theme `user' contains all the the settings the user customized and saved.
539 Additional themes declared with the `deftheme' macro will be added to
540 the front of this list.")
541
542 (defun custom-declare-theme (theme feature &optional doc &rest args)
543 "Like `deftheme', but THEME is evaluated as a normal argument.
544 FEATURE is the feature this theme provides. This symbol is created
545 from THEME by `custom-make-theme-feature'."
546 (add-to-list 'custom-known-themes theme)
547 (put theme 'theme-feature feature)
548 (when doc
549 (put theme 'theme-documentation doc))
550 (while args
551 (let ((arg (car args)))
552 (setq args (cdr args))
553 (unless (symbolp arg)
554 (error "Junk in args %S" args))
555 (let ((keyword arg)
556 (value (car args)))
557 (unless args
558 (error "Keyword %s is missing an argument" keyword))
559 (setq args (cdr args))
560 (cond ((eq keyword :short-description)
561 (put theme 'theme-short-description short-description))
562 ((eq keyword :immediate)
563 (put theme 'theme-immediate immediate))
564 ((eq keyword :variable-set-string)
565 (put theme 'theme-variable-set-string variable-set-string))
566 ((eq keyword :variable-reset-string)
567 (put theme 'theme-variable-reset-string variable-reset-string))
568 ((eq keyword :face-set-string)
569 (put theme 'theme-face-set-string face-set-string))
570 ((eq keyword :face-reset-string)
571 (put theme 'theme-face-reset-string face-reset-string)))))))
572
573 (defmacro deftheme (theme &optional doc &rest args)
574 "Declare custom theme THEME.
575 The optional argument DOC is a doc string describing the theme.
576 The remaining arguments should have the form
577
578 [KEYWORD VALUE]...
579
580 The following KEYWORD's are defined:
581
582 :short-description
583 VALUE is a short (one line) description of the theme. If not
584 given, DOC is used.
585 :immediate
586 If VALUE is non-nil, variables specified in this theme are set
587 immediately when loading the theme.
588 :variable-set-string
589 VALUE is a string used to indicate that a variable takes its
590 setting from this theme. It is passed to FORMAT with the name
591 of the theme as an additional argument. If not given, a
592 generic description is used.
593 :variable-reset-string
594 VALUE is a string used in the case a variable has been forced
595 to its value in this theme. It is passed to FORMAT with the
596 name of the theme as an additional argument. If not given, a
597 generic description is used.
598 :face-set-string
599 VALUE is a string used to indicate that a face takes its
600 setting from this theme. It is passed to FORMAT with the name
601 of the theme as an additional argument. If not given, a
602 generic description is used.
603 :face-reset-string
604 VALUE is a string used in the case a face has been forced to
605 its value in this theme. It is passed to FORMAT with the name
606 of the theme as an additional argument. If not given, a
607 generic description is used.
608
609 Any theme `foo' should be defined in a file called `foo-theme.el';
610 see `custom-make-theme-feature' for more information."
611 (let ((feature (custom-make-theme-feature theme)))
612 ;; It is better not to use backquote in this file,
613 ;; because that makes a bootstrapping problem
614 ;; if you need to recompile all the Lisp files using interpreted code.
615 (nconc (list 'custom-declare-theme
616 (list 'quote theme)
617 (list 'quote feature)
618 doc) args)))
619
620 (defun custom-make-theme-feature (theme)
621 "Given a symbol THEME, create a new symbol by appending \"-theme\".
622 Store this symbol in the `theme-feature' property of THEME.
623 Calling `provide-theme' to provide THEME actually puts `THEME-theme'
624 into `features'.
625
626 This allows for a file-name convention for autoloading themes:
627 Every theme X has a property `provide-theme' whose value is \"X-theme\".
628 \(require-theme X) then attempts to load the file `X-theme.el'."
629 (intern (concat (symbol-name theme) "-theme")))
630
631 (defsubst custom-theme-p (theme)
632 "Non-nil when THEME has been defined."
633 (memq theme custom-known-themes))
634
635 (defsubst custom-check-theme (theme)
636 "Check whether THEME is valid, and signal an error if it is not."
637 (unless (custom-theme-p theme)
638 (error "Unknown theme `%s'" theme)))
639
640 ;;; Initializing.
641
642 (defun custom-push-theme (prop symbol theme mode value)
643 "Add (THEME MODE VALUE) to the list in property PROP of SYMBOL.
644 If the first element in that list is already (THEME ...),
645 discard it first.
646
647 MODE can be either the symbol `set' or the symbol `reset'. If it is the
648 symbol `set', then VALUE is the value to use. If it is the symbol
649 `reset', then VALUE is the mode to query instead.
650
651 In the following example for the variable `goto-address-url-face', the
652 theme `subtle-hacker' uses the same value for the variable as the theme
653 `gnome2':
654
655 \((standard set bold)
656 \(gnome2 set info-xref)
657 \(jonadab set underline)
658 \(subtle-hacker reset gnome2))
659
660
661 If a value has been stored for themes A B and C, and a new value
662 is to be stored for theme C, then the old value of C is discarded.
663 If a new value is to be stored for theme B, however, the old value
664 of B is not discarded because B is not the car of the list.
665
666 For variables, list property PROP is `theme-value'.
667 For faces, list property PROP is `theme-face'.
668 This is used in `custom-do-theme-reset', for example.
669
670 The list looks the same in any case; the examples shows a possible
671 value of the `theme-face' property for the face `region':
672
673 \((gnome2 set ((t (:foreground \"cyan\" :background \"dark cyan\"))))
674 \(standard set ((((class color) (background dark))
675 \(:background \"blue\"))
676 \(t (:background \"gray\")))))
677
678 This records values for the `standard' and the `gnome2' themes.
679 The user has not customized the face; had he done that,
680 the list would contain an entry for the `user' theme, too.
681 See `custom-known-themes' for a list of known themes."
682 (let ((old (get symbol prop)))
683 (if (eq (car-safe (car-safe old)) theme)
684 (setq old (cdr old)))
685 (put symbol prop (cons (list theme mode value) old))))
686
687 (defvar custom-local-buffer nil
688 "Non-nil, in a Customization buffer, means customize a specific buffer.
689 If this variable is non-nil, it should be a buffer,
690 and it means customize the local bindings of that buffer.
691 This variable is a permanent local, and it normally has a local binding
692 in every Customization buffer.")
693 (put 'custom-local-buffer 'permanent-local t)
694
695 (defun custom-set-variables (&rest args)
696 "Initialize variables according to user preferences.
697 The settings are registered as theme `user'.
698 The arguments should each be a list of the form:
699
700 (SYMBOL VALUE [NOW [REQUEST [COMMENT]]])
701
702 The unevaluated VALUE is stored as the saved value for SYMBOL.
703 If NOW is present and non-nil, VALUE is also evaluated and bound as
704 the default value for the SYMBOL.
705
706 REQUEST is a list of features we must 'require for SYMBOL.
707 COMMENT is a comment string about SYMBOL."
708 (apply 'custom-theme-set-variables 'user args))
709
710 (defun custom-theme-set-variables (theme &rest args)
711 "Initialize variables according to settings specified by args.
712 Records the settings as belonging to THEME.
713
714 The arguments should be a list where each entry has the form:
715
716 (SYMBOL VALUE [NOW [REQUEST [COMMENT]]])
717
718 The unevaluated VALUE is stored as the saved value for SYMBOL.
719 If NOW is present and non-nil, VALUE is also evaluated and bound as
720 the default value for the SYMBOL.
721 REQUEST is a list of features we must 'require for SYMBOL.
722 COMMENT is a comment string about SYMBOL.
723
724 Several properties of THEME and SYMBOL are used in the process:
725
726 If THEME property `theme-immediate' is non-nil, this is equivalent of
727 providing the NOW argument to all symbols in the argument list: SYMBOL
728 is bound to the evaluated VALUE. The only difference is SYMBOL property
729 `force-value': if NOW is non-nil, SYMBOL's property `force-value' is set to
730 the symbol `rogue', else if THEME's property `theme-immediate' is non-nil,
731 FACE's property `force-face' is set to the symbol `immediate'.
732
733 VALUE itself is saved unevaluated as SYMBOL property `saved-value' and
734 in SYMBOL's list property `theme-value' \(using `custom-push-theme')."
735 (custom-check-theme theme)
736 (let ((immediate (get theme 'theme-immediate)))
737 (setq args
738 (sort args
739 (lambda (a1 a2)
740 (let* ((sym1 (car a1))
741 (sym2 (car a2))
742 (1-then-2 (memq sym1 (get sym2 'custom-dependencies)))
743 (2-then-1 (memq sym2 (get sym1 'custom-dependencies))))
744 (cond ((and 1-then-2 2-then-1)
745 (error "Circular custom dependency between `%s' and `%s'"
746 sym1 sym2))
747 (2-then-1 nil)
748 ;; Put symbols with :require last. The macro
749 ;; define-minor-mode generates a defcustom
750 ;; with a :require and a :set, where the
751 ;; setter function calls the mode function.
752 ;; Putting symbols with :require last ensures
753 ;; that the mode function will see other
754 ;; customized values rather than default
755 ;; values.
756 (t (nth 3 a2)))))))
757 (while args
758 (let ((entry (car args)))
759 (if (listp entry)
760 (let* ((symbol (nth 0 entry))
761 (value (nth 1 entry))
762 (now (nth 2 entry))
763 (requests (nth 3 entry))
764 (comment (nth 4 entry))
765 set)
766 (when requests
767 (put symbol 'custom-requests requests)
768 (mapc 'require requests))
769 (setq set (or (get symbol 'custom-set) 'custom-set-default))
770 (put symbol 'saved-value (list value))
771 (put symbol 'saved-variable-comment comment)
772 (custom-push-theme 'theme-value symbol theme 'set value)
773 ;; Allow for errors in the case where the setter has
774 ;; changed between versions, say, but let the user know.
775 (condition-case data
776 (cond (now
777 ;; Rogue variable, set it now.
778 (put symbol 'force-value t)
779 (funcall set symbol (eval value)))
780 ((default-boundp symbol)
781 ;; Something already set this, overwrite it.
782 (funcall set symbol (eval value))))
783 (error
784 (message "Error setting %s: %s" symbol data)))
785 (setq args (cdr args))
786 (and (or now (default-boundp symbol))
787 (put symbol 'variable-comment comment)))
788 ;; Old format, a plist of SYMBOL VALUE pairs.
789 (message "Warning: old format `custom-set-variables'")
790 (ding)
791 (sit-for 2)
792 (let ((symbol (nth 0 args))
793 (value (nth 1 args)))
794 (put symbol 'saved-value (list value))
795 (custom-push-theme 'theme-value symbol theme 'set value))
796 (setq args (cdr (cdr args))))))))
797
798 (defun custom-set-default (variable value)
799 "Default :set function for a customizable variable.
800 Normally, this sets the default value of VARIABLE to VALUE,
801 but if `custom-local-buffer' is non-nil,
802 this sets the local binding in that buffer instead."
803 (if custom-local-buffer
804 (with-current-buffer custom-local-buffer
805 (set variable value))
806 (set-default variable value)))
807
808 (defun custom-set-minor-mode (variable value)
809 ":set function for minor mode variables.
810 Normally, this sets the default value of VARIABLE to nil if VALUE
811 is nil and to t otherwise,
812 but if `custom-local-buffer' is non-nil,
813 this sets the local binding in that buffer instead."
814 (if custom-local-buffer
815 (with-current-buffer custom-local-buffer
816 (funcall variable (or value 0)))
817 (funcall variable (or value 0))))
818
819 (defun custom-quote (sexp)
820 "Quote SEXP iff it is not self quoting."
821 (if (or (memq sexp '(t nil))
822 (keywordp sexp)
823 (and (listp sexp)
824 (memq (car sexp) '(lambda)))
825 (stringp sexp)
826 (numberp sexp)
827 (vectorp sexp)
828 ;;; (and (fboundp 'characterp)
829 ;;; (characterp sexp))
830 )
831 sexp
832 (list 'quote sexp)))
833
834 (defun customize-mark-to-save (symbol)
835 "Mark SYMBOL for later saving.
836
837 If the default value of SYMBOL is different from the standard value,
838 set the `saved-value' property to a list whose car evaluates to the
839 default value. Otherwise, set it to nil.
840
841 To actually save the value, call `custom-save-all'.
842
843 Return non-nil iff the `saved-value' property actually changed."
844 (let* ((get (or (get symbol 'custom-get) 'default-value))
845 (value (funcall get symbol))
846 (saved (get symbol 'saved-value))
847 (standard (get symbol 'standard-value))
848 (comment (get symbol 'customized-variable-comment)))
849 ;; Save default value iff different from standard value.
850 (if (or (null standard)
851 (not (equal value (condition-case nil
852 (eval (car standard))
853 (error nil)))))
854 (put symbol 'saved-value (list (custom-quote value)))
855 (put symbol 'saved-value nil))
856 ;; Clear customized information (set, but not saved).
857 (put symbol 'customized-value nil)
858 ;; Save any comment that might have been set.
859 (when comment
860 (put symbol 'saved-variable-comment comment))
861 (not (equal saved (get symbol 'saved-value)))))
862
863 (defun customize-mark-as-set (symbol)
864 "Mark current value of SYMBOL as being set from customize.
865
866 If the default value of SYMBOL is different from the saved value if any,
867 or else if it is different from the standard value, set the
868 `customized-value' property to a list whose car evaluates to the
869 default value. Otherwise, set it to nil.
870
871 Return non-nil iff the `customized-value' property actually changed."
872 (let* ((get (or (get symbol 'custom-get) 'default-value))
873 (value (funcall get symbol))
874 (customized (get symbol 'customized-value))
875 (old (or (get symbol 'saved-value) (get symbol 'standard-value))))
876 ;; Mark default value as set iff different from old value.
877 (if (or (null old)
878 (not (equal value (condition-case nil
879 (eval (car old))
880 (error nil)))))
881 (put symbol 'customized-value (list (custom-quote value)))
882 (put symbol 'customized-value nil))
883 ;; Changed?
884 (not (equal customized (get symbol 'customized-value)))))
885
886 ;;; Theme Manipulation
887
888 (defvar custom-loaded-themes nil
889 "Themes in the order they are loaded.")
890
891 (defun custom-theme-loaded-p (theme)
892 "Return non-nil when THEME has been loaded."
893 (memq theme custom-loaded-themes))
894
895 (defun provide-theme (theme)
896 "Indicate that this file provides THEME.
897 Add THEME to `custom-loaded-themes' and `provide' whatever
898 is stored in THEME's property `theme-feature'.
899
900 Usually the theme-feature property contains a symbol created
901 by `custom-make-theme-feature'."
902 (custom-check-theme theme)
903 (provide (get theme 'theme-feature))
904 (setq custom-loaded-themes (nconc (list theme) custom-loaded-themes)))
905
906 (defun require-theme (theme)
907 "Try to load a theme by requiring its feature.
908 THEME's feature is stored in THEME's `theme-feature' property.
909
910 Usually the `theme-feature' property contains a symbol created
911 by `custom-make-theme-feature'."
912 ;; Note we do no check for validity of the theme here.
913 ;; This allows to pull in themes by a file-name convention
914 (require (or (get theme 'theme-feature)
915 (custom-make-theme-feature theme))))
916
917 (defun custom-remove-theme (spec-alist theme)
918 "Delete all elements from SPEC-ALIST whose car is THEME."
919 (let ((elt (assoc theme spec-alist)))
920 (while elt
921 (setq spec-alist (delete elt spec-alist)
922 elt (assoc theme spec-alist))))
923 spec-alist)
924
925 (defun custom-do-theme-reset (theme)
926 "Undo all settings defined by THEME.
927
928 A variable remains unchanged if its property `theme-value' does not
929 contain a value for THEME. A face remains unchanged if its property
930 `theme-face' does not contain a value for THEME. In either case, all
931 settings for THEME are removed from the property and the variable or
932 face is set to the `user' theme.
933
934 See `custom-known-themes' for a list of known themes."
935 (let (spec-list)
936 (mapatoms (lambda (symbol)
937 ;; This works even if symbol is both a variable and a
938 ;; face.
939 (setq spec-list (get symbol 'theme-value))
940 (when spec-list
941 (put symbol 'theme-value (custom-remove-theme spec-list theme))
942 (custom-theme-reset-internal symbol 'user))
943 (setq spec-list (get symbol 'theme-face))
944 (when spec-list
945 (put symbol 'theme-face (custom-remove-theme spec-list theme))
946 (custom-theme-reset-internal-face symbol 'user))))))
947
948 (defun custom-theme-load-themes (by-theme &rest body)
949 "Load the themes specified by BODY.
950 Record them as required by theme BY-THEME. BODY is a sequence of either
951
952 THEME
953 BY-THEME requires THEME
954 \(reset THEME)
955 Undo all the settings made by THEME
956 \(hidden THEME)
957 Require THEME but hide it from the user
958
959 All the themes loaded for BY-THEME are recorded in BY-THEME's property
960 `theme-loads-themes'. Any theme loaded with the hidden predicate will
961 be given the property `theme-hidden' unless it has been loaded before.
962 Whether a theme has been loaded before is determined by the function
963 `custom-theme-loaded-p'."
964 (custom-check-theme by-theme)
965 (let ((theme)
966 (themes-loaded (get by-theme 'theme-loads-themes)))
967 (while theme
968 (setq theme (car body)
969 body (cdr body))
970 (cond ((and (consp theme) (eq (car theme) 'reset))
971 (custom-do-theme-reset (cadr theme)))
972 ((and (consp theme) (eq (car theme) 'hidden))
973 (require-theme (cadr theme))
974 (unless (custom-theme-loaded-p (cadr theme))
975 (put (cadr theme) 'theme-hidden t)))
976 (t
977 (require-theme theme)
978 (put theme 'theme-hidden nil)))
979 (setq themes-loaded (nconc (list theme) themes-loaded)))
980 (put by-theme 'theme-loads-themes themes-loaded)))
981
982 (defun custom-load-themes (&rest body)
983 "Load themes for the USER theme as specified by BODY.
984
985 See `custom-theme-load-themes' for more information on BODY."
986 (apply 'custom-theme-load-themes 'user body))
987
988 ; (defsubst copy-upto-last (elt list)
989 ; "Copy all the elements of the list upto the last occurence of elt"
990 ; ;; Is it faster to do more work in C than to do less in elisp?
991 ; (nreverse (cdr (member elt (reverse list)))))
992
993 (defun custom-theme-value (theme theme-spec-list)
994 "Determine the value for THEME defined by THEME-SPEC-LIST.
995 Returns a list with the original value if found; nil otherwise.
996
997 THEME-SPEC-LIST is an alist with themes as its key. As new themes are
998 installed, these are added to the front of THEME-SPEC-LIST.
999 Each element has the form
1000
1001 \(THEME MODE VALUE)
1002
1003 MODE is either the symbol `set' or the symbol `reset'. See
1004 `custom-push-theme' for more information on the format of
1005 THEME-SPEC-LIST."
1006 ;; Note we do _NOT_ signal an error if the theme is unknown
1007 ;; it might have gone away without the user knowing.
1008 (let ((value (cdr (assoc theme theme-spec-list))))
1009 (if value
1010 (if (eq (car value) 'set)
1011 (cdr value)
1012 (custom-theme-value (cadr value) theme-spec-list)))))
1013
1014 (defun custom-theme-variable-value (variable theme)
1015 "Return (list value) indicating value of VARIABLE in THEME.
1016 If THEME does not define a value for VARIABLE, return nil. The value
1017 definitions per theme are stored in VARIABLE's property `theme-value'.
1018 The actual work is done by function `custom-theme-value', which see.
1019 See `custom-push-theme' for more information on how these definitions
1020 are stored."
1021 (custom-theme-value theme (get variable 'theme-value)))
1022
1023 (defun custom-theme-reset-internal (symbol to-theme)
1024 "Reset SYMBOL to the value defined by TO-THEME.
1025 If SYMBOL is not defined in TO-THEME, reset SYMBOL to the standard
1026 value. See `custom-theme-variable-value'. The standard value is
1027 stored in SYMBOL's property `standard-value'."
1028 (let ((value (custom-theme-variable-value symbol to-theme))
1029 was-in-theme)
1030 (setq was-in-theme value)
1031 (setq value (or value (get symbol 'standard-value)))
1032 (when value
1033 (put symbol 'saved-value was-in-theme)
1034 (if (or (get 'force-value symbol) (default-boundp symbol))
1035 (funcall (or (get symbol 'custom-set) 'set-default) symbol
1036 (eval (car value)))))
1037 value))
1038
1039 (defun custom-theme-reset-variables (theme &rest args)
1040 "Reset the value of the variables to values previously defined.
1041 Associate this setting with THEME.
1042
1043 ARGS is a list of lists of the form
1044
1045 (VARIABLE TO-THEME)
1046
1047 This means reset VARIABLE to its value in TO-THEME."
1048 (custom-check-theme theme)
1049 (mapcar '(lambda (arg)
1050 (apply 'custom-theme-reset-internal arg)
1051 (custom-push-theme 'theme-value (car arg) theme 'reset (cadr arg)))
1052 args))
1053
1054 (defun custom-reset-variables (&rest args)
1055 "Reset the value of the variables to values previously saved.
1056 This is the setting associated the `user' theme.
1057
1058 ARGS is a list of lists of the form
1059
1060 (VARIABLE TO-THEME)
1061
1062 This means reset VARIABLE to its value in TO-THEME."
1063 (apply 'custom-theme-reset-variables 'user args))
1064
1065 ;;; The End.
1066
1067 ;; Process the defcustoms for variables loaded before this file.
1068 (while custom-declare-variable-list
1069 (apply 'custom-declare-variable (car custom-declare-variable-list))
1070 (setq custom-declare-variable-list (cdr custom-declare-variable-list)))
1071
1072 (provide 'custom)
1073
1074 ;;; custom.el ends here