(U-000278B8): Apply new conventions for glyph granularity.
[chise/xemacs-chise.git.1] / lisp / custom.el
1 ;;; custom.el -- Tools for declaring and initializing options.
2
3 ;; Copyright (C) 1996, 1997 Free Software Foundation, Inc.
4
5 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
6 ;; Maintainer: Hrvoje Niksic <hniksic@xemacs.org>
7 ;; Keywords: help, faces, dumped
8 ;; Version: 1.9960-x
9 ;; X-URL: http://www.dina.kvl.dk/~abraham/custom/
10
11 ;; This file is part of XEmacs.
12
13 ;; XEmacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; any later version.
17
18 ;; XEmacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with XEmacs; see the file COPYING.  If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
27
28 ;;; Commentary:
29
30 ;; This file is dumped with XEmacs.
31
32 ;; This file only contain the code needed to declare and initialize
33 ;; user options.  The code to customize options is autoloaded from
34 ;; `cus-edit.el'.
35 ;;
36 ;; The code implementing face declarations is in `cus-face.el'
37
38 ;;; Code:
39
40 (eval-when-compile
41   (load "cl-macs" nil t))
42
43 (autoload 'custom-declare-face "cus-face")
44 (autoload 'defun* "cl-macs")
45
46 (require 'widget)
47
48 (defvar custom-define-hook nil
49   ;; Customize information for this option is in `cus-edit.el'.
50   "Hook called after defining each customize option.")
51
52 ;;; The `defcustom' Macro.
53
54 (defun custom-initialize-default (symbol value)
55   "Initialize SYMBOL with VALUE.
56 This will do nothing if symbol already has a default binding.
57 Otherwise, if symbol has a `saved-value' property, it will evaluate
58 the car of that and used as the default binding for symbol.
59 Otherwise, VALUE will be evaluated and used as the default binding for
60 symbol."
61   (unless (default-boundp symbol)
62     ;; Use the saved value if it exists, otherwise the standard setting.
63     (set-default symbol (if (get symbol 'saved-value)
64                             (eval (car (get symbol 'saved-value)))
65                           (eval value)))))
66
67 (defun custom-initialize-set (symbol value)
68   "Initialize SYMBOL with VALUE.
69 Like `custom-initialize-default', but use the function specified by
70 `:set' to initialize SYMBOL."
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 with VALUE.
80 Like `custom-initialize-set', but use the function specified by
81 `:get' to reinitialize SYMBOL if it is already bound."
82     (funcall (or (get symbol 'custom-set) 'set-default)
83              symbol
84              (cond ((default-boundp symbol)
85                     (funcall (or (get symbol 'custom-get) 'default-value)
86                              symbol))
87                    ((get symbol 'saved-value)
88                     (eval (car (get symbol 'saved-value))))
89                    (t
90                     (eval value)))))
91
92 (defun custom-initialize-changed (symbol value)
93   "Initialize SYMBOL with VALUE.
94 Like `custom-initialize-reset', but only use the `:set' function if the
95 not using the standard setting.  Otherwise, use the `set-default'."
96   (cond ((default-boundp symbol)
97          (funcall (or (get symbol 'custom-set) 'set-default)
98                   symbol
99                   (funcall (or (get symbol 'custom-get) 'default-value)
100                            symbol)))
101         ((get symbol 'saved-value)
102          (funcall (or (get symbol 'custom-set) 'set-default)
103                   symbol
104                   (eval (car (get symbol 'saved-value)))))
105         (t
106          (set-default symbol (eval value)))))
107
108 (defun custom-declare-variable (symbol value doc &rest args)
109   "Like `defcustom', but SYMBOL and VALUE are evaluated as normal arguments."
110   ;; Remember the standard setting.
111   (put symbol 'standard-value (list value))
112   ;; Maybe this option was rogue in an earlier version.  It no longer is.
113   (when (eq (get symbol 'force-value) 'rogue)
114     ;; It no longer is.
115     (put symbol 'force-value nil))
116   (when doc
117     (put symbol 'variable-documentation doc))
118   (let ((initialize 'custom-initialize-reset)
119         (requests nil))
120     (while args
121       (let ((arg (car args)))
122         (setq args (cdr args))
123         (check-argument-type 'keywordp arg)
124         (let ((keyword arg)
125               (value (car args)))
126           (unless args
127             (signal 'error (list "Keyword is missing an argument" keyword)))
128           (setq args (cdr args))
129           (cond ((eq keyword :initialize)
130                  (setq initialize value))
131                 ((eq keyword :set)
132                  (put symbol 'custom-set value))
133                 ((eq keyword :get)
134                  (put symbol 'custom-get value))
135                 ((eq keyword :require)
136                  (setq requests (cons value requests)))
137                 ((eq keyword :type)
138                  (put symbol 'custom-type value))
139                 ((eq keyword :options)
140                  (if (get symbol 'custom-options)
141                      ;; Slow safe code to avoid duplicates.
142                      (mapc (lambda (option)
143                              (custom-add-option symbol option))
144                            value)
145                    ;; Fast code for the common case.
146                    (put symbol 'custom-options (copy-sequence value))))
147                 (t
148                  (custom-handle-keyword symbol keyword value
149                                         'custom-variable))))))
150     (put symbol 'custom-requests requests)
151     ;; Do the actual initialization.
152     (funcall initialize symbol value))
153   ;; #### This is a rough equivalent of LOADHIST_ATTACH.  However,
154   ;; LOADHIST_ATTACH also checks for `initialized'.
155   (push symbol current-load-list)
156   (run-hooks 'custom-define-hook)
157   symbol)
158
159 (defmacro defcustom (symbol value doc &rest args)
160   "Declare SYMBOL as a customizable variable that defaults to VALUE.
161 DOC is the variable documentation.
162
163 Neither SYMBOL nor VALUE needs to be quoted.
164 If SYMBOL is not already bound, initialize it to VALUE.
165 The remaining arguments should have the form
166
167    [KEYWORD VALUE]...
168
169 The following KEYWORD's are defined:
170
171 :type   VALUE should be a widget type for editing the symbols value.
172         The default is `sexp'.
173 :options VALUE should be a list of valid members of the widget type.
174 :group  VALUE should be a customization group.
175         Add SYMBOL to that group.
176 :initialize VALUE should be a function used to initialize the
177         variable.  It takes two arguments, the symbol and value
178         given in the `defcustom' call.  The default is
179         `custom-initialize-set'
180 :set    VALUE should be a function to set the value of the symbol.
181         It takes two arguments, the symbol to set and the value to
182         give it.  The default is `custom-set-default'.
183 :get    VALUE should be a function to extract the value of symbol.
184         The function takes one argument, a symbol, and should return
185         the current value for that symbol.  The default is
186         `default-value'.
187 :require VALUE should be a feature symbol.  Each feature will be
188         required after initialization, of the user have saved this
189         option.
190 :version VALUE should be a string specifying that the variable was
191         first introduced, or its default value was changed, in Emacs
192         version VERSION.
193 :set-after VARIABLE specifies that SYMBOL should be set after VARIABLE when
194         both have been customized.
195
196 Read the section about customization in the Emacs Lisp manual for more
197 information."
198   `(custom-declare-variable (quote ,symbol) (quote ,value) ,doc ,@args))
199
200 ;;; The `defface' Macro.
201
202 (defmacro defface (face spec doc &rest args)
203   "Declare FACE as a customizable face that defaults to SPEC.
204 FACE does not need to be quoted.
205
206 Third argument DOC is the face documentation.
207
208 If FACE has been set with `custom-set-face', set the face attributes
209 as specified by that function, otherwise set the face attributes
210 according to SPEC.
211
212 The remaining arguments should have the form
213
214    [KEYWORD VALUE]...
215
216 The following KEYWORDs are defined:
217
218 :group  VALUE should be a customization group.
219         Add FACE to that group.
220
221 SPEC should be an alist of the form ((DISPLAY ATTS)...).
222
223 ATTS is a list of face attributes and their values.  The possible
224 attributes are defined in the variable `custom-face-attributes'.
225
226 The ATTS of the first entry in SPEC where the DISPLAY matches the
227 frame should take effect in that frame.  DISPLAY can either be the
228 symbol t, which will match all frames, or an alist of the form
229 \((REQ ITEM...)...)
230
231 For the DISPLAY to match a FRAME, the REQ property of the frame must
232 match one of the ITEM.  The following REQ are defined:
233
234 `type' (the value of `window-system')
235   Should be one of `x', `mswindows', or `tty'.
236
237 `class' (the frame's color support)
238   Should be one of `color', `grayscale', or `mono'.
239
240 `background' (what color is used for the background text)
241   Should be one of `light' or `dark'.
242
243 Read the section about customization in the Emacs Lisp manual for more
244 information."
245   `(custom-declare-face (quote ,face) ,spec ,doc ,@args))
246
247 ;;; The `defgroup' Macro.
248
249 (defun custom-declare-group (symbol members doc &rest args)
250   "Like `defgroup', but SYMBOL is evaluated as a normal argument."
251   (while members
252     (apply 'custom-add-to-group symbol (car members))
253     (pop members))
254   (put symbol 'custom-group (nconc members (get symbol 'custom-group)))
255   (when doc
256     (put symbol 'group-documentation doc))
257   (while args
258     (let ((arg (car args)))
259       (setq args (cdr args))
260       (check-argument-type 'keywordp arg)
261       (let ((keyword arg)
262             (value (car args)))
263         (unless args
264           (signal 'error (list "Keyword is missing an argument" keyword)))
265         (setq args (cdr args))
266         (cond ((eq keyword :prefix)
267                (put symbol 'custom-prefix value))
268               (t
269                (custom-handle-keyword symbol keyword value
270                                       'custom-group))))))
271   (run-hooks 'custom-define-hook)
272   symbol)
273
274 (defmacro defgroup (symbol members doc &rest args)
275   "Declare SYMBOL as a customization group containing MEMBERS.
276 SYMBOL does not need to be quoted.
277
278 Third arg DOC is the group documentation.
279
280 MEMBERS should be an alist of the form ((NAME WIDGET)...) where NAME
281 is a symbol and WIDGET is a widget for editing that symbol.  Useful
282 widgets are `custom-variable' for editing variables, `custom-face' for
283 edit faces, and `custom-group' for editing groups.
284
285 The remaining arguments should have the form
286
287    [KEYWORD VALUE]...
288
289 The following KEYWORD's are defined:
290
291 :group  VALUE should be a customization group.
292         Add SYMBOL to that group.
293
294 Read the section about customization in the Emacs Lisp manual for more
295 information."
296   `(custom-declare-group (quote ,symbol) ,members ,doc ,@args))
297
298 (defvar custom-group-hash-table (make-hash-table :size 300 :test 'eq)
299   "Hash-table of non-empty groups.")
300
301 (defun custom-add-to-group (group option widget)
302   "To existing GROUP add a new OPTION of type WIDGET.
303 If there already is an entry for that option, overwrite it."
304   (let* ((members (get group 'custom-group))
305          (old (assq option members)))
306     (if old
307         (setcar (cdr old) widget)
308       (put group 'custom-group (nconc members (list (list option widget))))))
309   (puthash group t custom-group-hash-table))
310
311 ;;; Properties.
312
313 (defun custom-handle-all-keywords (symbol args type)
314   "For customization option SYMBOL, handle keyword arguments ARGS.
315 Third argument TYPE is the custom option type."
316   (while args
317     (let ((arg (car args)))
318       (setq args (cdr args))
319       (check-argument-type 'keywordp arg)
320       (let ((keyword arg)
321             (value (car args)))
322         (unless args
323           (signal 'error (list "Keyword is missing an argument" keyword)))
324         (setq args (cdr args))
325         (custom-handle-keyword symbol keyword value type)))))
326
327 (defun custom-handle-keyword (symbol keyword value type)
328   "For customization option SYMBOL, handle KEYWORD with VALUE.
329 Fourth argument TYPE is the custom option type."
330   (cond ((eq keyword :group)
331          (custom-add-to-group value symbol type))
332         ((eq keyword :version)
333          (custom-add-version symbol value))
334         ((eq keyword :link)
335          (custom-add-link symbol value))
336         ((eq keyword :load)
337          (custom-add-load symbol value))
338         ((eq keyword :tag)
339          (put symbol 'custom-tag value))
340         ((eq keyword :set-after)
341          (custom-add-dependencies symbol value))
342         (t
343          (signal 'error (list "Unknown keyword" keyword)))))
344
345 (defun custom-add-dependencies (symbol value)
346   "To the custom option SYMBOL, add dependencies specified by VALUE.
347 VALUE should be a list of symbols.  For each symbol in that list,
348 this specifies that SYMBOL should be set after the specified symbol, if
349 both appear in constructs like `custom-set-variables'."
350   (unless (listp value)
351     (error "Invalid custom dependency `%s'" value))
352   (let* ((deps (get symbol 'custom-dependencies))
353          (new-deps deps))
354     (while value
355       (let ((dep (car value)))
356         (unless (symbolp dep)
357           (error "Invalid custom dependency `%s'" dep))
358         (unless (memq dep new-deps)
359           (setq new-deps (cons dep new-deps)))
360         (setq value (cdr value))))
361     (unless (eq deps new-deps)
362       (put symbol 'custom-dependencies new-deps))))
363
364 (defun custom-add-option (symbol option)
365   "To the variable SYMBOL add OPTION.
366
367 If SYMBOL is a hook variable, OPTION should be a hook member.
368 For other types variables, the effect is undefined."
369   (let ((options (get symbol 'custom-options)))
370     (unless (member option options)
371       (put symbol 'custom-options (cons option options)))))
372
373 (defun custom-add-link (symbol widget)
374   "To the custom option SYMBOL add the link WIDGET."
375   (let ((links (get symbol 'custom-links)))
376     (unless (member widget links)
377       (put symbol 'custom-links (cons widget links)))))
378
379 (defun custom-add-version (symbol version)
380   "To the custom option SYMBOL add the version VERSION."
381   (put symbol 'custom-version version))
382
383 (defun custom-add-load (symbol load)
384   "To the custom option SYMBOL add the dependency LOAD.
385 LOAD should be either a library file name, or a feature name."
386   (puthash symbol t custom-group-hash-table)
387   (let ((loads (get symbol 'custom-loads)))
388     (unless (member load loads)
389       (put symbol 'custom-loads (cons load loads)))))
390
391 ;;; deftheme macro
392
393 (defvar custom-known-themes '(user standard)
394    "Themes that have been defthemed.")
395
396 ;;  #### add strings for group
397 ;; #### during bootstrap we cannot use cl-macs stuff
398 (defun* custom-define-theme (theme feature &optional doc
399          &key short-description immediate variable-reset-string
400          variable-set-string face-set-string face-reset-string
401          &allow-other-keys)
402   (push theme custom-known-themes)
403   (put theme 'theme-feature feature)
404   (put theme 'theme-documentation doc)
405   (if immediate (put theme 'theme-immediate immediate))
406   (if variable-reset-string
407       (put theme 'theme-variable-reset-string variable-reset-string ))
408   (if variable-set-string
409       (put theme 'theme-variable-set-string variable-set-string ))
410   (if face-reset-string
411       (put theme 'theme-face-reset-string face-reset-string ))
412   (if face-set-string
413       (put theme 'theme-face-set-string face-set-string ))
414   (if short-description
415       (put theme 'theme-short-description short-description )))
416
417 (defun custom-make-theme-feature (theme)
418   (intern (concat (symbol-name theme) "-theme")))
419
420 (defmacro deftheme (theme &rest body)
421   "(deftheme THEME &optional DOC &key KEYWORDS)
422
423 Define a theme labeled by SYMBOL THEME. The optional argument DOC is a
424 doc string describing the theme. It is optionally followed by the
425 following keyword arguments
426
427 :short-description DESC
428       DESC is a short (one line) description of the theme. If not given DOC
429       is used.
430 :immediate FLAG
431       If FLAG is non-nil variables set in this theme are bound
432       immediately when loading the theme.
433 :variable-set-string VARIABLE_-SET-STRING
434       A string used by the UI to indicate that the value takes it
435       setting from this theme. It is passed to FORMAT with the
436       name of the theme a additional argument.
437       If not given, a generic description is used.
438 :variable-reset-string VARIABLE-RESET-STRING
439       As above but used in the case the variable has been forced to
440       the value in this theme.
441 :face-set-string FACE-SET-STRING
442 :face-reset-string FACE-RESET-STRING
443       As above but for faces."
444   (let ((feature (custom-make-theme-feature theme)))
445     `(custom-define-theme (quote ,theme) (quote ,feature) ,@body)))
446
447 (defsubst custom-theme-p (theme)
448   "Non-nil when THEME has been defined."
449   (memq theme custom-known-themes))
450
451 (defsubst custom-check-theme (theme)
452   "Check whether THEME is valid and signal an error if NOT."
453   (unless (custom-theme-p theme)
454     (error "Unknown theme `%s'" theme)))
455
456
457 ; #### do we need to deftheme 'user and/or 'standard here to make the
458 ;      code in cus-edit cleaner?.
459
460 ;;; Initializing.
461
462 (defun custom-push-theme (prop symbol theme mode value)
463   (let ((old (get symbol prop)))
464     (if (eq (car-safe (car-safe old)) theme)
465         (setq old (cdr old)))
466     (put symbol prop (cons (list theme mode value) old))))
467
468 (defvar custom-local-buffer nil
469   "Non-nil, in a Customization buffer, means customize a specific buffer.
470 If this variable is non-nil, it should be a buffer,
471 and it means customize the local bindings of that buffer.
472 This variable is a permanent local, and it normally has a local binding
473 in every Customization buffer.")
474 (put 'custom-local-buffer 'permanent-local t)
475
476 (defun custom-set-variables (&rest args)
477   "Initialize variables according to user preferences.
478 The settings are registered as theme `user'.
479 Each argument should be a list of the form:
480
481   (SYMBOL VALUE [NOW [REQUEST [COMMENT]]])
482
483 The unevaluated VALUE is stored as the saved value for SYMBOL.
484 If NOW is present and non-nil, VALUE is also evaluated and bound as
485 the default value for the SYMBOL.
486 REQUEST is a list of features we must 'require for SYMBOL.
487 COMMENT is a comment string about SYMBOL."
488   (apply 'custom-theme-set-variables 'user args))
489
490 (defun custom-theme-set-variables (theme &rest args)
491   "Initialize variables according to settings specified by args.
492 Records the settings as belonging to THEME.
493
494 See `custom-set-variables' for a description of the arguments ARGS."
495   (custom-check-theme theme)
496   (setq args
497         (sort args
498               (lambda (a1 a2)
499                 (let* ((sym1 (car a1))
500                        (sym2 (car a2))
501                        (1-then-2 (memq sym1 (get sym2 'custom-dependencies)))
502                        (2-then-1 (memq sym2 (get sym1 'custom-dependencies))))
503                   (cond ((and 1-then-2 2-then-1)
504                          (error "Circular custom dependency between `%s' and `%s'"
505                                 sym1 sym2))
506                         (1-then-2 t)
507                         (2-then-1 nil)
508                         ;; Put symbols with :require last.  The macro
509                         ;; define-minor-mode generates a defcustom
510                         ;; with a :require and a :set, where the
511                         ;; setter function calls the mode function.
512                         ;; Putting symbols with :require last ensures
513                         ;; that the mode function will see other
514                         ;; customized values rather than default
515                         ;; values.
516                         (t (nth 3 a2)))))))
517   (let ((immediate (get theme 'theme-immediate)))
518     (while args
519       (let ((entry (car args)))
520         (if (listp entry)
521             (let* ((symbol (nth 0 entry))
522                    (value (nth 1 entry))
523                    (now (nth 2 entry))
524                    (requests (nth 3 entry))
525                    (comment (nth 4 entry))
526                    (set (or (get symbol 'custom-set) 'custom-set-default)))
527               (put symbol 'saved-value (list value))
528               (custom-push-theme 'theme-value symbol theme 'set value)
529               (put symbol 'saved-variable-comment comment)
530           ;; Allow for errors in the case where the setter has
531           ;; changed between versions, say, but let the user know.
532               (condition-case data
533               (cond ((or now immediate)
534                      ;; Rogue variable, set it now.
535                      (put symbol 'force-value (if now 'rogue 'immediate))
536                      (funcall set symbol (eval value)))
537                     ((default-boundp symbol)
538                      ;; Something already set this, overwrite it.
539                      (funcall set symbol (eval value))))
540               (error 
541                (message "Error setting %s: %s" symbol data)))
542               (and (or now (default-boundp symbol))
543                  (put symbol 'variable-comment comment))
544               (when requests
545                 (put symbol 'custom-requests requests)
546                 (mapc 'require requests))
547               (setq args (cdr args)))
548           ;; Old format, a plist of SYMBOL VALUE pairs.
549           (message "Warning: old format `custom-set-variables'")
550           (ding)
551           (sit-for 2)
552           (let ((symbol (nth 0 args))
553                 (value (nth 1 args)))
554             (put symbol 'saved-value (list value))
555             (custom-push-theme 'theme-value symbol theme 'set value))
556           (setq args (cdr (cdr args))))))))
557
558 (defvar custom-loaded-themes nil
559   "Themes in the order they are loaded.")
560
561 (defun custom-theme-loaded-p (theme)
562   "Return non-nil when THEME has been loaded."
563   (memq theme custom-loaded-themes))
564
565 (defun provide-theme (theme)
566   "Indicate that this file provides THEME."
567   (custom-check-theme theme)
568   (provide (get theme 'theme-feature))
569   (push theme custom-loaded-themes))
570
571 (defun require-theme (theme &optional soft)
572   "Try to load a theme by requiring its feature."
573   ;; Note we do no check for validity of the theme here.
574   ;; This allows to pull in themes by a file-name convention
575   (require (get theme 'theme-feature (custom-make-theme-feature theme))))
576
577 (defun custom-do-theme-reset (theme)
578   ; #### untested! slow!
579   (let (spec-list)
580     (mapatoms (lambda (symbol)
581                 (setq spec-list (get symbol 'theme-value))
582                 (when spec-list
583                   (setq spec-list (delete-if (lambda (elt)
584                                                (eq (car elt) theme))
585                                              spec-list))
586                   (put symbol 'theme-value spec-list)
587                   (custom-theme-reset-internal symbol 'user))
588                 (setq spec-list (get symbol 'theme-face))
589                 (when spec-list
590                   (setq spec-list (delete-if (lambda (elt)
591                                                (eq (car elt) theme))
592                                              spec-list))
593                   (put symbol 'theme-face spec-list)
594                   (custom-theme-reset-internal-face symbol 'user))))))
595
596 (defun custom-theme-load-themes (by-theme &rest body)
597   "Load the themes specified by BODY and record them as required by
598 theme BY-THEME. BODY is a sequence of
599        - a SYMBOL
600             require the theme SYMBOL
601        - a list (reset THEME)
602             Undo all the settings made by THEME.
603        - a list (hidden THEME)
604             require the THEME but hide it from the user."
605   (custom-check-theme by-theme)
606   (dolist (theme body)
607     (cond ((and (consp theme) (eq (car theme) 'reset))
608            (custom-do-theme-reset (cadr theme)))
609           ((and (consp theme) (eq (car theme) 'hidden))
610            (require-theme (cadr theme))
611            (unless (custom-theme-loaded-p (cadr theme))
612              (put (cadr theme) 'theme-hidden t)))
613           (t
614            (require-theme theme)
615            (remprop theme 'theme-hidden)))
616     (push theme (get by-theme 'theme-loads-themes))))
617
618 (defun custom-load-themes (&rest body)
619   "Load themes for the USER theme as specified by BODY.
620
621 BODY is as with custom-theme-load-themes."
622   (apply #'custom-theme-load-themes 'user body))
623
624
625
626
627 (defsubst copy-upto-last (elt list)
628   "Copy all the elements of the list upto the last occurrence of elt."
629   ;; Is it faster to do more work in C than to do less in elisp?
630   (nreverse (cdr (member elt (reverse list)))))
631
632 (defun custom-theme-value (theme theme-spec-list)
633   "Determine the value for THEME defined by THEME-SPEC-LIST.
634 Returns (list value) if found. Nil otherwise."
635   ;; Note we do _NOT_ signal an error if the theme is unknown
636   ;; it might have gone away without the user knowing.
637   (let ((theme-or-lower (memq theme (cons 'user custom-loaded-themes)))
638         value)
639     (mapc #'(lambda (theme-spec)
640               (when (member (car theme-spec) theme-or-lower)
641                 (setq value (cdr theme-spec))
642                 ;; We need to continue because if theme =A and we found
643                 ;; B then if the load order is B A C B
644                 ;; we actually want the value in C.
645                 (setq theme-or-lower (copy-upto-last (car theme-spec)
646                                                      theme-or-lower))
647                 ;; We could should circuit if this is now nil.
648                 ))
649           theme-spec-list)
650     (if value
651         (if (eq (car value) 'set)
652             (list (cadr value))
653           ;; Yet another reset spec. car value = reset
654           (custom-theme-value (cadr value) theme-spec-list)))))
655
656
657 (defun custom-theme-variable-value (variable theme)
658   "Return (list value) value of VARIABLE in THEME if the THEME modifies the
659 VARIABLE.  Nil otherwise."
660   (custom-theme-value theme (get variable 'theme-value)))
661
662 (defun custom-theme-reset-internal (symbol to-theme)
663   (let ((value (custom-theme-variable-value symbol to-theme))
664         was-in-theme)
665     (setq was-in-theme value)
666     (setq value (or value (get symbol 'standard-value)))
667     (when value
668       (put symbol 'saved-value was-in-theme)
669       (if (or (get 'force-value symbol) (default-boundp symbol))
670           (funcall (get symbol 'custom-set 'set-default) symbol
671                    (eval (car value)))))
672     value))
673
674
675 (defun custom-theme-reset-variables (theme &rest args)
676   "Reset the value of the variables to values previously defined.
677 Associate this setting with THEME.
678
679 ARGS is a list of lists of the form
680
681     (variable to-theme)
682
683 This means reset variable to its value in to-theme."
684   (custom-check-theme theme)
685   (mapc #'(lambda (arg)
686             (apply #'custom-theme-reset-internal arg)
687             (custom-push-theme 'theme-value (car arg) theme 'reset (cadr arg)))
688         args))
689
690 (defun custom-reset-variables (&rest args)
691     "Reset the value of the variables to values previously defined.
692 Associate this setting with the `user' theme.
693
694 The ARGS are as in `custom-theme-reset-variables'."
695     (apply #'custom-theme-reset-variables 'user args))
696
697 (defun custom-set-default (variable value)
698   "Default :set function for a customizable variable.
699 Normally, this sets the default value of VARIABLE to VALUE,
700 but if `custom-local-buffer' is non-nil,
701 this sets the local binding in that buffer instead."
702   (if custom-local-buffer
703       (with-current-buffer custom-local-buffer
704         (set variable value))
705     (set-default variable value)))
706
707 ;;; The End.
708
709 (provide 'custom)
710
711 ;; custom.el ends here