]> code.delx.au - gnu-emacs/blob - lisp/custom.el
Add new maintainer (deego).
[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 (dolist (load (get symbol 'custom-loads))
506 (cond ((symbolp load) (condition-case nil (require load) (error nil)))
507 ;; This is subsumed by the test below, but it's much faster.
508 ((assoc load load-history))
509 ;; This was just (assoc (locate-library load) load-history)
510 ;; but has been optimized not to load locate-library
511 ;; if not necessary.
512 ((let ((regexp (concat "\\(\\`\\|/\\)" (regexp-quote load)
513 "\\(\\'\\|\\.\\)"))
514 (found nil))
515 (dolist (loaded load-history)
516 (and (stringp (car loaded))
517 (string-match regexp (car loaded))
518 (setq found t)))
519 found))
520 ;; Without this, we would load cus-edit recursively.
521 ;; We are still loading it when we call this,
522 ;; and it is not in load-history yet.
523 ((equal load "cus-edit"))
524 (t (condition-case nil (load load) (error nil))))))))
525
526 (defvar custom-known-themes '(user standard)
527 "Themes that have been define with `deftheme'.
528 The default value is the list (user standard). The theme `standard'
529 contains the Emacs standard settings from the original Lisp files. The
530 theme `user' contains all the the settings the user customized and saved.
531 Additional themes declared with the `deftheme' macro will be added to
532 the front of this list.")
533
534 (defun custom-declare-theme (theme feature &optional doc &rest args)
535 "Like `deftheme', but THEME is evaluated as a normal argument.
536 FEATURE is the feature this theme provides. This symbol is created
537 from THEME by `custom-make-theme-feature'."
538 (add-to-list 'custom-known-themes theme)
539 (put theme 'theme-feature feature)
540 (when doc
541 (put theme 'theme-documentation doc))
542 (while args
543 (let ((arg (car args)))
544 (setq args (cdr args))
545 (unless (symbolp arg)
546 (error "Junk in args %S" args))
547 (let ((keyword arg)
548 (value (car args)))
549 (unless args
550 (error "Keyword %s is missing an argument" keyword))
551 (setq args (cdr args))
552 (cond ((eq keyword :short-description)
553 (put theme 'theme-short-description short-description))
554 ((eq keyword :immediate)
555 (put theme 'theme-immediate immediate))
556 ((eq keyword :variable-set-string)
557 (put theme 'theme-variable-set-string variable-set-string))
558 ((eq keyword :variable-reset-string)
559 (put theme 'theme-variable-reset-string variable-reset-string))
560 ((eq keyword :face-set-string)
561 (put theme 'theme-face-set-string face-set-string))
562 ((eq keyword :face-reset-string)
563 (put theme 'theme-face-reset-string face-reset-string)))))))
564
565 (defmacro deftheme (theme &optional doc &rest args)
566 "Declare custom theme THEME.
567 The optional argument DOC is a doc string describing the theme.
568 The remaining arguments should have the form
569
570 [KEYWORD VALUE]...
571
572 The following KEYWORD's are defined:
573
574 :short-description
575 VALUE is a short (one line) description of the theme. If not
576 given, DOC is used.
577 :immediate
578 If VALUE is non-nil, variables specified in this theme are set
579 immediately when loading the theme.
580 :variable-set-string
581 VALUE is a string used to indicate that a variable takes its
582 setting from this theme. It is passed to FORMAT with the name
583 of the theme as an additional argument. If not given, a
584 generic description is used.
585 :variable-reset-string
586 VALUE is a string used in the case a variable has been forced
587 to its value in this theme. It is passed to FORMAT with the
588 name of the theme as an additional argument. If not given, a
589 generic description is used.
590 :face-set-string
591 VALUE is a string used to indicate that a face takes its
592 setting from this theme. It is passed to FORMAT with the name
593 of the theme as an additional argument. If not given, a
594 generic description is used.
595 :face-reset-string
596 VALUE is a string used in the case a face has been forced to
597 its value in this theme. It is passed to FORMAT with the name
598 of the theme as an additional argument. If not given, a
599 generic description is used.
600
601 Any theme `foo' should be defined in a file called `foo-theme.el';
602 see `custom-make-theme-feature' for more information."
603 (let ((feature (custom-make-theme-feature theme)))
604 ;; It is better not to use backquote in this file,
605 ;; because that makes a bootstrapping problem
606 ;; if you need to recompile all the Lisp files using interpreted code.
607 (nconc (list 'custom-declare-theme
608 (list 'quote theme)
609 (list 'quote feature)
610 doc) args)))
611
612 (defun custom-make-theme-feature (theme)
613 "Given a symbol THEME, create a new symbol by appending \"-theme\".
614 Store this symbol in the `theme-feature' property of THEME.
615 Calling `provide-theme' to provide THEME actually puts `THEME-theme'
616 into `features'.
617
618 This allows for a file-name convention for autoloading themes:
619 Every theme X has a property `provide-theme' whose value is \"X-theme\".
620 \(require-theme X) then attempts to load the file `X-theme.el'."
621 (intern (concat (symbol-name theme) "-theme")))
622
623 (defsubst custom-theme-p (theme)
624 "Non-nil when THEME has been defined."
625 (memq theme custom-known-themes))
626
627 (defsubst custom-check-theme (theme)
628 "Check whether THEME is valid, and signal an error if it is not."
629 (unless (custom-theme-p theme)
630 (error "Unknown theme `%s'" theme)))
631
632 ;;; Initializing.
633
634 (defun custom-push-theme (prop symbol theme mode value)
635 "Add (THEME MODE VALUE) to the list in property PROP of SYMBOL.
636 If the first element in that list is already (THEME ...),
637 discard it first.
638
639 MODE can be either the symbol `set' or the symbol `reset'. If it is the
640 symbol `set', then VALUE is the value to use. If it is the symbol
641 `reset', then VALUE is the mode to query instead.
642
643 In the following example for the variable `goto-address-url-face', the
644 theme `subtle-hacker' uses the same value for the variable as the theme
645 `gnome2':
646
647 \((standard set bold)
648 \(gnome2 set info-xref)
649 \(jonadab set underline)
650 \(subtle-hacker reset gnome2))
651
652
653 If a value has been stored for themes A B and C, and a new value
654 is to be stored for theme C, then the old value of C is discarded.
655 If a new value is to be stored for theme B, however, the old value
656 of B is not discarded because B is not the car of the list.
657
658 For variables, list property PROP is `theme-value'.
659 For faces, list property PROP is `theme-face'.
660 This is used in `custom-do-theme-reset', for example.
661
662 The list looks the same in any case; the examples shows a possible
663 value of the `theme-face' property for the face `region':
664
665 \((gnome2 set ((t (:foreground \"cyan\" :background \"dark cyan\"))))
666 \(standard set ((((class color) (background dark))
667 \(:background \"blue\"))
668 \(t (:background \"gray\")))))
669
670 This records values for the `standard' and the `gnome2' themes.
671 The user has not customized the face; had he done that,
672 the list would contain an entry for the `user' theme, too.
673 See `custom-known-themes' for a list of known themes."
674 (let ((old (get symbol prop)))
675 (if (eq (car-safe (car-safe old)) theme)
676 (setq old (cdr old)))
677 (put symbol prop (cons (list theme mode value) old))))
678
679 (defvar custom-local-buffer nil
680 "Non-nil, in a Customization buffer, means customize a specific buffer.
681 If this variable is non-nil, it should be a buffer,
682 and it means customize the local bindings of that buffer.
683 This variable is a permanent local, and it normally has a local binding
684 in every Customization buffer.")
685 (put 'custom-local-buffer 'permanent-local t)
686
687 (defun custom-set-variables (&rest args)
688 "Initialize variables according to user preferences.
689 The settings are registered as theme `user'.
690 The arguments should each be a list of the form:
691
692 (SYMBOL VALUE [NOW [REQUEST [COMMENT]]])
693
694 The unevaluated VALUE is stored as the saved value for SYMBOL.
695 If NOW is present and non-nil, VALUE is also evaluated and bound as
696 the default value for the SYMBOL.
697
698 REQUEST is a list of features we must 'require for SYMBOL.
699 COMMENT is a comment string about SYMBOL."
700 (apply 'custom-theme-set-variables 'user args))
701
702 (defun custom-theme-set-variables (theme &rest args)
703 "Initialize variables according to settings specified by args.
704 Records the settings as belonging to THEME.
705
706 The arguments should be a list where each entry has the form:
707
708 (SYMBOL VALUE [NOW [REQUEST [COMMENT]]])
709
710 The unevaluated VALUE is stored as the saved value for SYMBOL.
711 If NOW is present and non-nil, VALUE is also evaluated and bound as
712 the default value for the SYMBOL.
713 REQUEST is a list of features we must 'require for SYMBOL.
714 COMMENT is a comment string about SYMBOL.
715
716 Several properties of THEME and SYMBOL are used in the process:
717
718 If THEME property `theme-immediate' is non-nil, this is equivalent of
719 providing the NOW argument to all symbols in the argument list: SYMBOL
720 is bound to the evaluated VALUE. The only difference is SYMBOL property
721 `force-value': if NOW is non-nil, SYMBOL's property `force-value' is set to
722 the symbol `rogue', else if THEME's property `theme-immediate' is non-nil,
723 FACE's property `force-face' is set to the symbol `immediate'.
724
725 VALUE itself is saved unevaluated as SYMBOL property `saved-value' and
726 in SYMBOL's list property `theme-value' \(using `custom-push-theme')."
727 (custom-check-theme theme)
728 (let ((immediate (get theme 'theme-immediate)))
729 (setq args
730 (sort args
731 (lambda (a1 a2)
732 (let* ((sym1 (car a1))
733 (sym2 (car a2))
734 (1-then-2 (memq sym1 (get sym2 'custom-dependencies)))
735 (2-then-1 (memq sym2 (get sym1 'custom-dependencies))))
736 (cond ((and 1-then-2 2-then-1)
737 (error "Circular custom dependency between `%s' and `%s'"
738 sym1 sym2))
739 (2-then-1 nil)
740 ;; Put symbols with :require last. The macro
741 ;; define-minor-mode generates a defcustom
742 ;; with a :require and a :set, where the
743 ;; setter function calls the mode function.
744 ;; Putting symbols with :require last ensures
745 ;; that the mode function will see other
746 ;; customized values rather than default
747 ;; values.
748 (t (nth 3 a2)))))))
749 (while args
750 (let ((entry (car args)))
751 (if (listp entry)
752 (let* ((symbol (nth 0 entry))
753 (value (nth 1 entry))
754 (now (nth 2 entry))
755 (requests (nth 3 entry))
756 (comment (nth 4 entry))
757 set)
758 (when requests
759 (put symbol 'custom-requests requests)
760 (mapc 'require requests))
761 (setq set (or (get symbol 'custom-set) 'custom-set-default))
762 (put symbol 'saved-value (list value))
763 (put symbol 'saved-variable-comment comment)
764 (custom-push-theme 'theme-value symbol theme 'set value)
765 ;; Allow for errors in the case where the setter has
766 ;; changed between versions, say, but let the user know.
767 (condition-case data
768 (cond (now
769 ;; Rogue variable, set it now.
770 (put symbol 'force-value t)
771 (funcall set symbol (eval value)))
772 ((default-boundp symbol)
773 ;; Something already set this, overwrite it.
774 (funcall set symbol (eval value))))
775 (error
776 (message "Error setting %s: %s" symbol data)))
777 (setq args (cdr args))
778 (and (or now (default-boundp symbol))
779 (put symbol 'variable-comment comment)))
780 ;; Old format, a plist of SYMBOL VALUE pairs.
781 (message "Warning: old format `custom-set-variables'")
782 (ding)
783 (sit-for 2)
784 (let ((symbol (nth 0 args))
785 (value (nth 1 args)))
786 (put symbol 'saved-value (list value))
787 (custom-push-theme 'theme-value symbol theme 'set value))
788 (setq args (cdr (cdr args))))))))
789
790 (defun custom-set-default (variable value)
791 "Default :set function for a customizable variable.
792 Normally, this sets the default value of VARIABLE to VALUE,
793 but if `custom-local-buffer' is non-nil,
794 this sets the local binding in that buffer instead."
795 (if custom-local-buffer
796 (with-current-buffer custom-local-buffer
797 (set variable value))
798 (set-default variable value)))
799
800 (defun custom-quote (sexp)
801 "Quote SEXP iff it is not self quoting."
802 (if (or (memq sexp '(t nil))
803 (keywordp sexp)
804 (and (listp sexp)
805 (memq (car sexp) '(lambda)))
806 (stringp sexp)
807 (numberp sexp)
808 (vectorp sexp)
809 ;;; (and (fboundp 'characterp)
810 ;;; (characterp sexp))
811 )
812 sexp
813 (list 'quote sexp)))
814
815 (defun customize-mark-to-save (symbol)
816 "Mark SYMBOL for later saving.
817
818 If the default value of SYMBOL is different from the standard value,
819 set the `saved-value' property to a list whose car evaluates to the
820 default value. Otherwise, set it til nil.
821
822 To actually save the value, call `custom-save-all'.
823
824 Return non-nil iff the `saved-value' property actually changed."
825 (let* ((get (or (get symbol 'custom-get) 'default-value))
826 (value (funcall get symbol))
827 (saved (get symbol 'saved-value))
828 (standard (get symbol 'standard-value))
829 (comment (get symbol 'customized-variable-comment)))
830 ;; Save default value iff different from standard value.
831 (if (or (null standard)
832 (not (equal value (condition-case nil
833 (eval (car standard))
834 (error nil)))))
835 (put symbol 'saved-value (list (custom-quote value)))
836 (put symbol 'saved-value nil))
837 ;; Clear customized information (set, but not saved).
838 (put symbol 'customized-value nil)
839 ;; Save any comment that might have been set.
840 (when comment
841 (put symbol 'saved-variable-comment comment))
842 (not (equal saved (get symbol 'saved-value)))))
843
844 (defun customize-mark-as-set (symbol)
845 "Mark current value of SYMBOL as being set from customize.
846
847 If the default value of SYMBOL is different from the saved value if any,
848 or else if it is different from the standard value, set the
849 `customized-value' property to a list whose car evaluates to the
850 default value. Otherwise, set it til nil.
851
852 Return non-nil iff the `customized-value' property actually changed."
853 (let* ((get (or (get symbol 'custom-get) 'default-value))
854 (value (funcall get symbol))
855 (customized (get symbol 'customized-value))
856 (old (or (get symbol 'saved-value) (get symbol 'standard-value))))
857 ;; Mark default value as set iff different from old value.
858 (if (or (null old)
859 (not (equal value (condition-case nil
860 (eval (car old))
861 (error nil)))))
862 (put symbol 'customized-value (list (custom-quote value)))
863 (put symbol 'customized-value nil))
864 ;; Changed?
865 (not (equal customized (get symbol 'customized-value)))))
866
867 ;;; Theme Manipulation
868
869 (defvar custom-loaded-themes nil
870 "Themes in the order they are loaded.")
871
872 (defun custom-theme-loaded-p (theme)
873 "Return non-nil when THEME has been loaded."
874 (memq theme custom-loaded-themes))
875
876 (defun provide-theme (theme)
877 "Indicate that this file provides THEME.
878 Add THEME to `custom-loaded-themes' and `provide' whatever
879 is stored in THEME's property `theme-feature'.
880
881 Usually the theme-feature property contains a symbol created
882 by `custom-make-theme-feature'."
883 (custom-check-theme theme)
884 (provide (get theme 'theme-feature))
885 (setq custom-loaded-themes (nconc (list theme) custom-loaded-themes)))
886
887 (defun require-theme (theme)
888 "Try to load a theme by requiring its feature.
889 THEME's feature is stored in THEME's `theme-feature' property.
890
891 Usually the `theme-feature' property contains a symbol created
892 by `custom-make-theme-feature'."
893 ;; Note we do no check for validity of the theme here.
894 ;; This allows to pull in themes by a file-name convention
895 (require (or (get theme 'theme-feature)
896 (custom-make-theme-feature theme))))
897
898 (defun custom-remove-theme (spec-alist theme)
899 "Detelete all elements from SPEC-ALIST whose car is THEME."
900 (let ((elt (assoc theme spec-alist)))
901 (while elt
902 (setq spec-alist (delete elt spec-alist)
903 elt (assoc theme spec-alist))))
904 spec-alist)
905
906 (defun custom-do-theme-reset (theme)
907 "Undo all settings defined by THEME.
908
909 A variable remains unchanged if its property `theme-value' does not
910 contain a value for THEME. A face remains unchanged if its property
911 `theme-face' does not contain a value for THEME. In either case, all
912 settings for THEME are removed from the property and the variable or
913 face is set to the `user' theme.
914
915 See `custom-known-themes' for a list of known themes."
916 (let (spec-list)
917 (mapatoms (lambda (symbol)
918 ;; This works even if symbol is both a variable and a
919 ;; face.
920 (setq spec-list (get symbol 'theme-value))
921 (when spec-list
922 (put symbol 'theme-value (custom-remove-theme spec-list theme))
923 (custom-theme-reset-internal symbol 'user))
924 (setq spec-list (get symbol 'theme-face))
925 (when spec-list
926 (put symbol 'theme-face (custom-remove-theme spec-list theme))
927 (custom-theme-reset-internal-face symbol 'user))))))
928
929 (defun custom-theme-load-themes (by-theme &rest body)
930 "Load the themes specified by BODY.
931 Record them as required by theme BY-THEME. BODY is a sequence of either
932
933 THEME
934 BY-THEME requires THEME
935 \(reset THEME)
936 Undo all the settings made by THEME
937 \(hidden THEME)
938 Require THEME but hide it from the user
939
940 All the themes loaded for BY-THEME are recorded in BY-THEME's property
941 `theme-loads-themes'. Any theme loaded with the hidden predicate will
942 be given the property `theme-hidden' unless it has been loaded before.
943 Whether a theme has been loaded before is determined by the function
944 `custom-theme-loaded-p'."
945 (custom-check-theme by-theme)
946 (let ((theme)
947 (themes-loaded (get by-theme 'theme-loads-themes)))
948 (while theme
949 (setq theme (car body)
950 body (cdr body))
951 (cond ((and (consp theme) (eq (car theme) 'reset))
952 (custom-do-theme-reset (cadr theme)))
953 ((and (consp theme) (eq (car theme) 'hidden))
954 (require-theme (cadr theme))
955 (unless (custom-theme-loaded-p (cadr theme))
956 (put (cadr theme) 'theme-hidden t)))
957 (t
958 (require-theme theme)
959 (put theme 'theme-hidden nil)))
960 (setq themes-loaded (nconc (list theme) themes-loaded)))
961 (put by-theme 'theme-loads-themes themes-loaded)))
962
963 (defun custom-load-themes (&rest body)
964 "Load themes for the USER theme as specified by BODY.
965
966 See `custom-theme-load-themes' for more information on BODY."
967 (apply 'custom-theme-load-themes 'user body))
968
969 ; (defsubst copy-upto-last (elt list)
970 ; "Copy all the elements of the list upto the last occurence of elt"
971 ; ;; Is it faster to do more work in C than to do less in elisp?
972 ; (nreverse (cdr (member elt (reverse list)))))
973
974 (defun custom-theme-value (theme theme-spec-list)
975 "Determine the value for THEME defined by THEME-SPEC-LIST.
976 Returns a list with the original value if found; nil otherwise.
977
978 THEME-SPEC-LIST is an alist with themes as its key. As new themes are
979 installed, these are added to the front of THEME-SPEC-LIST.
980 Each element has the form
981
982 \(THEME MODE VALUE)
983
984 MODE is either the symbol `set' or the symbol `reset'. See
985 `custom-push-theme' for more information on the format of
986 THEME-SPEC-LIST."
987 ;; Note we do _NOT_ signal an error if the theme is unknown
988 ;; it might have gone away without the user knowing.
989 (let ((value (cdr (assoc theme theme-spec-list))))
990 (if value
991 (if (eq (car value) 'set)
992 (cdr value)
993 (custom-theme-value (cadr value) theme-spec-list)))))
994
995 (defun custom-theme-variable-value (variable theme)
996 "Return (list value) indicating value of VARIABLE in THEME.
997 If THEME does not define a value for VARIABLE, return nil. The value
998 definitions per theme are stored in VARIABLE's property `theme-value'.
999 The actual work is done by function `custom-theme-value', which see.
1000 See `custom-push-theme' for more information on how these definitions
1001 are stored."
1002 (custom-theme-value theme (get variable 'theme-value)))
1003
1004 (defun custom-theme-reset-internal (symbol to-theme)
1005 "Reset SYMBOL to the value defined by TO-THEME.
1006 If SYMBOL is not defined in TO-THEME, reset SYMBOL to the standard
1007 value. See `custom-theme-variable-value'. The standard value is
1008 stored in SYMBOL's property `standard-value'."
1009 (let ((value (custom-theme-variable-value symbol to-theme))
1010 was-in-theme)
1011 (setq was-in-theme value)
1012 (setq value (or value (get symbol 'standard-value)))
1013 (when value
1014 (put symbol 'saved-value was-in-theme)
1015 (if (or (get 'force-value symbol) (default-boundp symbol))
1016 (funcall (or (get symbol 'custom-set) 'set-default) symbol
1017 (eval (car value)))))
1018 value))
1019
1020 (defun custom-theme-reset-variables (theme &rest args)
1021 "Reset the value of the variables to values previously defined.
1022 Associate this setting with THEME.
1023
1024 ARGS is a list of lists of the form
1025
1026 (VARIABLE TO-THEME)
1027
1028 This means reset VARIABLE to its value in TO-THEME."
1029 (custom-check-theme theme)
1030 (mapcar '(lambda (arg)
1031 (apply 'custom-theme-reset-internal arg)
1032 (custom-push-theme 'theme-value (car arg) theme 'reset (cadr arg)))
1033 args))
1034
1035 (defun custom-reset-variables (&rest args)
1036 "Reset the value of the variables to values previously saved.
1037 This is the setting associated the `user' theme.
1038
1039 ARGS is a list of lists of the form
1040
1041 (VARIABLE TO-THEME)
1042
1043 This means reset VARIABLE to its value in TO-THEME."
1044 (apply 'custom-theme-reset-variables 'user args))
1045
1046 ;;; The End.
1047
1048 ;; Process the defcustoms for variables loaded before this file.
1049 (while custom-declare-variable-list
1050 (apply 'custom-declare-variable (car custom-declare-variable-list))
1051 (setq custom-declare-variable-list (cdr custom-declare-variable-list)))
1052
1053 (provide 'custom)
1054
1055 ;;; custom.el ends here