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