Regenerated.
[bpt/emacs.git] / lisp / custom.el
CommitLineData
55535639
PJ
1;;; custom.el --- tools for declaring and initializing options
2;;
00644b82 3;; Copyright (C) 1996, 1997, 1999, 2001, 2002 Free Software Foundation, Inc.
55535639
PJ
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;;
e6bb31f3 28;; This file only contains the code needed to declare and initialize
55535639
PJ
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
e6bb31f3 32;; The code implementing face declarations is in `cus-face.el'.
55535639
PJ
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
6da43544
RS
42(defvar custom-dont-initialize nil
43 "Non-nil means `defcustom' should not initialize the variable.
44That is used for the sake of `custom-make-dependencies'.
45Users should not set it.")
46
d3b80e9b
SM
47(defvar custom-current-group-alist nil
48 "Alist of (FILE . GROUP) indicating the current group to use for FILE.")
49
55535639
PJ
50;;; The `defcustom' Macro.
51
52(defun custom-initialize-default (symbol value)
53 "Initialize SYMBOL with VALUE.
54This will do nothing if symbol already has a default binding.
55Otherwise, if symbol has a `saved-value' property, it will evaluate
56the car of that and used as the default binding for symbol.
57Otherwise, VALUE will be evaluated and used as the default binding for
58symbol."
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.
67If the symbol doesn't have a default binding already,
68then set it using its `:set' function (or `set-default' if it has none).
69The value is either the value in the symbol's `saved-value' property,
70if 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.
80Set the symbol, using its `:set' function (or `set-default' if it has none).
81The value is either the symbol's current value
82 \(as obtained using the `:get' function), if any,
83or the value in the symbol's `saved-value' property if any,
84or (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.
97Like `custom-initialize-reset', but only use the `:set' function if
98not using the standard setting.
99For 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.
114DEFAULT should be an expression to evaluate to compute the default value,
e1bab181
RS
115not the default value itself.
116
117DEFAULT is stored as SYMBOL's value in the standard theme. See
118`custom-known-themes' for a list of known themes. For backwards
119compatibility, DEFAULT is also stored in SYMBOL's property
120`standard-value'. At the same time, SYMBOL's property `force-value' is
121set 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...
55535639
PJ
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))
d3b80e9b
SM
133 (unless (memq :group args)
134 (custom-add-to-group (custom-current-group) symbol 'custom-variable))
55535639
PJ
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)
6f0d61bf 152 (push value requests))
55535639
PJ
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))
6f0d61bf 160 value)
55535639
PJ
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.
6da43544
RS
168 (unless custom-dont-initialize
169 (funcall initialize symbol default)))
6f0d61bf 170 (push (cons 'defvar symbol) current-load-list)
55535639
PJ
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.
176DOC is the variable documentation.
177
178Neither SYMBOL nor VALUE needs to be quoted.
179If SYMBOL is not already bound, initialize it to VALUE.
180The remaining arguments should have the form
181
182 [KEYWORD VALUE]...
183
184The following keywords are meaningful:
185
693d0bd3 186:type VALUE should be a widget type for editing the symbol's value.
55535639
PJ
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.
3dc5f18e 190:link LINK-DATA
00644b82
EZ
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
3dc5f18e 197 (custom-manual INFO-NODE)
00644b82 198 Link to an Info node; INFO-NODE is a string which specifies
3dc5f18e 199 the node name, as in \"(emacs)Top\". The link appears as
00644b82
EZ
200 `[manual]' in the customization buffer.
201
3dc5f18e 202 (info-link INFO-NODE)
00644b82
EZ
203 Like `custom-manual' except that the link appears in the
204 customization buffer with the Info node name.
205
3dc5f18e 206 (url-link URL)
00644b82
EZ
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
3dc5f18e 212 example, (info-link :tag \"foo\" \"(emacs)Top\") makes a link to the
00644b82
EZ
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.
55535639
PJ
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
a01639a3 221 `custom-initialize-reset'.
55535639
PJ
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.
00644b82
EZ
237:tag LABEL
238 Use LABEL, a string, instead of the item's name, to label the item
1c1da418 239 in customization menus and buffers.
00644b82
EZ
240:load FILE
241 Load file FILE (a string) before displaying this customization
29512a0f 242 item. Loading is done with `load', and only if the file is
00644b82 243 not already loaded.
64e9f9d9
DL
244:set-after VARIABLES
245 Specifies that SYMBOL should be set after the list of variables
246 VARIABLES when both have been customized.
55535639
PJ
247
248Read the section about customization in the Emacs Lisp manual for more
249information."
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.
263FACE does not need to be quoted.
264
265Third argument DOC is the face documentation.
266
267If FACE has been set with `custom-set-face', set the face attributes
268as specified by that function, otherwise set the face attributes
269according to SPEC.
270
271The remaining arguments should have the form
272
273 [KEYWORD VALUE]...
274
275The following KEYWORDs are defined:
276
277:group VALUE should be a customization group.
278 Add FACE to that group.
279
280SPEC should be an alist of the form ((DISPLAY ATTS)...).
281
282The first element of SPEC where the DISPLAY matches the frame
283is the one that takes effect in that frame. The ATTRs in this
284element take effect; the other elements are ignored, on that frame.
285
286ATTS is a list of face attributes followed by their values:
287 (ATTR VALUE ATTR VALUE...)
288
289The possible attributes are `:family', `:width', `:height', `:weight',
290`:slant', `:underline', `:overline', `:strike-through', `:box',
3d58b15e 291`:foreground', `:background', `:stipple', `:inverse-video', and `:inherit'.
55535639
PJ
292
293DISPLAY can either be the symbol t, which will match all frames, or an
294alist of the form \((REQ ITEM...)...). For the DISPLAY to match a
295FRAME, the REQ property of the frame must match one of the ITEM. The
296following 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
309Read the section about customization in the Emacs Lisp manual for more
310information."
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
d3b80e9b
SM
318(defun custom-current-group ()
319 (cdr (assoc load-file-name custom-current-group-alist)))
320
55535639
PJ
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)))
55535639
PJ
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))))))
d3b80e9b
SM
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)))
55535639
PJ
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.
353SYMBOL does not need to be quoted.
354
355Third arg DOC is the group documentation.
356
357MEMBERS should be an alist of the form ((NAME WIDGET)...) where
358NAME is a symbol and WIDGET is a widget for editing that symbol.
359Useful widgets are `custom-variable' for editing variables,
360`custom-face' for edit faces, and `custom-group' for editing groups.
361
362The remaining arguments should have the form
363
364 [KEYWORD VALUE]...
365
366The 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
374Read the section about customization in the Emacs Lisp manual for more
375information."
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.
383If 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
29512a0f
SM
389(defun custom-group-of-mode (mode)
390 "Return the custom group corresponding to the major or minor MODE.
391If 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
55535639
PJ
400;;; Properties.
401
402(defun custom-handle-all-keywords (symbol args type)
403 "For customization option SYMBOL, handle keyword arguments ARGS.
404Third argument TYPE is the custom option type."
d3b80e9b 405 (unless (memq :group args)
9b6098b9 406 (custom-add-to-group (custom-current-group) symbol type))
55535639
PJ
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.
421Fourth 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.
441VALUE should be a list of symbols. For each symbol in that list,
442this specifies that SYMBOL should be set after the specified symbol, if
443both 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))))
e1bab181 457
55535639
PJ
458(defun custom-add-option (symbol option)
459 "To the variable SYMBOL add OPTION.
460
461If SYMBOL is a hook variable, OPTION should be a hook member.
462For 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.
479LOAD 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
8f772dfd
RS
484;;; Loading files needed to customize a symbol.
485;;; This is in custom.el because menu-bar.el needs it for toggle cmds.
486
487(defvar custom-load-recursion nil
488 "Hack to avoid recursive dependencies.")
489
490(defun custom-load-symbol (symbol)
491 "Load all dependencies for SYMBOL."
492 (unless custom-load-recursion
29512a0f
SM
493 (let ((custom-load-recursion t))
494 (dolist (load (get symbol 'custom-loads))
495 (cond ((symbolp load) (condition-case nil (require load) (error nil)))
496 ;; This is subsumed by the test below, but it's much faster.
8f772dfd
RS
497 ((assoc load load-history))
498 ;; This was just (assoc (locate-library load) load-history)
499 ;; but has been optimized not to load locate-library
500 ;; if not necessary.
29512a0f
SM
501 ((let ((regexp (concat "\\(\\`\\|/\\)" (regexp-quote load)
502 "\\(\\'\\|\\.\\)"))
503 (found nil))
8f772dfd 504 (dolist (loaded load-history)
c3891447
RS
505 (and (stringp (car loaded))
506 (string-match regexp (car loaded))
8f772dfd
RS
507 (setq found t)))
508 found))
509 ;; Without this, we would load cus-edit recursively.
510 ;; We are still loading it when we call this,
511 ;; and it is not in load-history yet.
512 ((equal load "cus-edit"))
29512a0f 513 (t (condition-case nil (load load) (error nil))))))))
8f772dfd 514
e1bab181
RS
515(defvar custom-known-themes '(user standard)
516 "Themes that have been define with `deftheme'.
517The default value is the list (user standard). The theme `standard'
518contains the Emacs standard settings from the original Lisp files. The
519theme `user' contains all the the settings the user customized and saved.
520Additional themes declared with the `deftheme' macro will be added to
521the front of this list.")
522
523(defun custom-declare-theme (theme feature &optional doc &rest args)
524 "Like `deftheme', but THEME is evaluated as a normal argument.
525FEATURE is the feature this theme provides. This symbol is created
526from THEME by `custom-make-theme-feature'."
527 (add-to-list 'custom-known-themes theme)
528 (put theme 'theme-feature feature)
529 (when doc
530 (put theme 'theme-documentation doc))
531 (while args
532 (let ((arg (car args)))
533 (setq args (cdr args))
534 (unless (symbolp arg)
535 (error "Junk in args %S" args))
536 (let ((keyword arg)
537 (value (car args)))
538 (unless args
539 (error "Keyword %s is missing an argument" keyword))
540 (setq args (cdr args))
541 (cond ((eq keyword :short-description)
542 (put theme 'theme-short-description short-description))
543 ((eq keyword :immediate)
544 (put theme 'theme-immediate immediate))
545 ((eq keyword :variable-set-string)
546 (put theme 'theme-variable-set-string variable-set-string))
547 ((eq keyword :variable-reset-string)
548 (put theme 'theme-variable-reset-string variable-reset-string))
549 ((eq keyword :face-set-string)
550 (put theme 'theme-face-set-string face-set-string))
551 ((eq keyword :face-reset-string)
552 (put theme 'theme-face-reset-string face-reset-string)))))))
553
554(defmacro deftheme (theme &optional doc &rest args)
555 "Declare custom theme THEME.
556The optional argument DOC is a doc string describing the theme.
557The remaining arguments should have the form
558
559 [KEYWORD VALUE]...
560
561The following KEYWORD's are defined:
562
563:short-description
564 VALUE is a short (one line) description of the theme. If not
565 given, DOC is used.
566:immediate
567 If VALUE is non-nil, variables specified in this theme are set
568 immediately when loading the theme.
569:variable-set-string
570 VALUE is a string used to indicate that a variable takes its
571 setting from this theme. It is passed to FORMAT with the name
572 of the theme as an additional argument. If not given, a
573 generic description is used.
574:variable-reset-string
575 VALUE is a string used in the case a variable has been forced
576 to its value in this theme. It is passed to FORMAT with the
577 name of the theme as an additional argument. If not given, a
578 generic description is used.
579:face-set-string
580 VALUE is a string used to indicate that a face takes its
581 setting from this theme. It is passed to FORMAT with the name
582 of the theme as an additional argument. If not given, a
583 generic description is used.
584:face-reset-string
585 VALUE is a string used in the case a face has been forced to
586 its value in this theme. It is passed to FORMAT with the name
587 of the theme as an additional argument. If not given, a
588 generic description is used.
589
590Any theme `foo' should be defined in a file called `foo-theme.el';
591see `custom-make-theme-feature' for more information."
592 (let ((feature (custom-make-theme-feature theme)))
593 ;; It is better not to use backquote in this file,
594 ;; because that makes a bootstrapping problem
595 ;; if you need to recompile all the Lisp files using interpreted code.
596 (nconc (list 'custom-declare-theme
597 (list 'quote theme)
598 (list 'quote feature)
599 doc) args)))
600
601(defun custom-make-theme-feature (theme)
602 "Given a symbol THEME, create a new symbol by appending \"-theme\".
603Store this symbol in the `theme-feature' property of THEME.
604Calling `provide-theme' to provide THEME actually puts `THEME-theme'
605into `features'.
606
607This allows for a file-name convention for autoloading themes:
608Every theme X has a property `provide-theme' whose value is \"X-theme\".
609\(require-theme X) then attempts to load the file `X-theme.el'."
610 (intern (concat (symbol-name theme) "-theme")))
611
612(defsubst custom-theme-p (theme)
613 "Non-nil when THEME has been defined."
614 (memq theme custom-known-themes))
615
616(defsubst custom-check-theme (theme)
617 "Check whether THEME is valid, and signal an error if it is not."
618 (unless (custom-theme-p theme)
619 (error "Unknown theme `%s'" theme)))
620
55535639
PJ
621;;; Initializing.
622
e1bab181
RS
623(defun custom-push-theme (prop symbol theme mode value)
624 "Add (THEME MODE VALUE) to the list in property PROP of SYMBOL.
625If the first element in that list is already (THEME ...),
626discard it first.
627
628MODE can be either the symbol `set' or the symbol `reset'. If it is the
629symbol `set', then VALUE is the value to use. If it is the symbol
630`reset', then VALUE is the mode to query instead.
631
632In the following example for the variable `goto-address-url-face', the
633theme `subtle-hacker' uses the same value for the variable as the theme
634`gnome2':
635
636 \((standard set bold)
637 \(gnome2 set info-xref)
638 \(jonadab set underline)
639 \(subtle-hacker reset gnome2))
640
641
642If a value has been stored for themes A B and C, and a new value
643is to be stored for theme C, then the old value of C is discarded.
644If a new value is to be stored for theme B, however, the old value
645of B is not discarded because B is not the car of the list.
646
647For variables, list property PROP is `theme-value'.
648For faces, list property PROP is `theme-face'.
649This is used in `custom-do-theme-reset', for example.
650
651The list looks the same in any case; the examples shows a possible
652value of the `theme-face' property for the face `region':
653
654 \((gnome2 set ((t (:foreground \"cyan\" :background \"dark cyan\"))))
655 \(standard set ((((class color) (background dark))
656 \(:background \"blue\"))
657 \(t (:background \"gray\")))))
658
659This records values for the `standard' and the `gnome2' themes.
660The user has not customized the face; had he done that,
661the list would contain an entry for the `user' theme, too.
662See `custom-known-themes' for a list of known themes."
663 (let ((old (get symbol prop)))
664 (if (eq (car-safe (car-safe old)) theme)
665 (setq old (cdr old)))
666 (put symbol prop (cons (list theme mode value) old))))
667
55535639
PJ
668(defvar custom-local-buffer nil
669 "Non-nil, in a Customization buffer, means customize a specific buffer.
670If this variable is non-nil, it should be a buffer,
671and it means customize the local bindings of that buffer.
672This variable is a permanent local, and it normally has a local binding
673in every Customization buffer.")
674(put 'custom-local-buffer 'permanent-local t)
675
676(defun custom-set-variables (&rest args)
677 "Initialize variables according to user preferences.
e1bab181 678The settings are registered as theme `user'.
64e9f9d9 679The arguments should each be a list of the form:
55535639
PJ
680
681 (SYMBOL VALUE [NOW [REQUEST [COMMENT]]])
682
683The unevaluated VALUE is stored as the saved value for SYMBOL.
684If NOW is present and non-nil, VALUE is also evaluated and bound as
685the default value for the SYMBOL.
e1bab181
RS
686
687REQUEST is a list of features we must 'require for SYMBOL.
55535639 688COMMENT is a comment string about SYMBOL."
e1bab181
RS
689 (apply 'custom-theme-set-variables 'user args))
690
691(defun custom-theme-set-variables (theme &rest args)
692 "Initialize variables according to settings specified by args.
693Records the settings as belonging to THEME.
694
695The arguments should be a list where each entry has the form:
696
697 (SYMBOL VALUE [NOW [REQUEST [COMMENT]]])
698
699The unevaluated VALUE is stored as the saved value for SYMBOL.
700If NOW is present and non-nil, VALUE is also evaluated and bound as
701the default value for the SYMBOL.
702REQUEST is a list of features we must 'require for SYMBOL.
703COMMENT is a comment string about SYMBOL.
704
705Several properties of THEME and SYMBOL are used in the process:
706
707If THEME property `theme-immediate' is non-nil, this is equivalent of
708providing the NOW argument to all symbols in the argument list: SYMBOL
709is bound to the evaluated VALUE. The only difference is SYMBOL property
710`force-value': if NOW is non-nil, SYMBOL's property `force-value' is set to
711the symbol `rogue', else if THEME's property `theme-immediate' is non-nil,
712FACE's property `force-face' is set to the symbol `immediate'.
713
714VALUE itself is saved unevaluated as SYMBOL property `saved-value' and
715in SYMBOL's list property `theme-value' \(using `custom-push-theme')."
716 (custom-check-theme theme)
717 (let ((immediate (get theme 'theme-immediate)))
718 (setq args
719 (sort args
720 (lambda (a1 a2)
721 (let* ((sym1 (car a1))
722 (sym2 (car a2))
723 (1-then-2 (memq sym1 (get sym2 'custom-dependencies)))
724 (2-then-1 (memq sym2 (get sym1 'custom-dependencies))))
725 (cond ((and 1-then-2 2-then-1)
726 (error "Circular custom dependency between `%s' and `%s'"
727 sym1 sym2))
728 (2-then-1 nil)
729 ;; Put symbols with :require last. The macro
730 ;; define-minor-mode generates a defcustom
731 ;; with a :require and a :set, where the
732 ;; setter function calls the mode function.
733 ;; Putting symbols with :require last ensures
734 ;; that the mode function will see other
735 ;; customized values rather than default
736 ;; values.
737 (t (nth 3 a2)))))))
738 (while args
739 (let ((entry (car args)))
740 (if (listp entry)
741 (let* ((symbol (nth 0 entry))
742 (value (nth 1 entry))
743 (now (nth 2 entry))
744 (requests (nth 3 entry))
745 (comment (nth 4 entry))
746 set)
747 (when requests
748 (put symbol 'custom-requests requests)
749 (mapc 'require requests))
750 (setq set (or (get symbol 'custom-set) 'custom-set-default))
751 (put symbol 'saved-value (list value))
752 (put symbol 'saved-variable-comment comment)
753 (custom-push-theme 'theme-value symbol theme 'set value)
754 ;; Allow for errors in the case where the setter has
55535639
PJ
755 ;; changed between versions, say, but let the user know.
756 (condition-case data
757 (cond (now
758 ;; Rogue variable, set it now.
759 (put symbol 'force-value t)
760 (funcall set symbol (eval value)))
761 ((default-boundp symbol)
762 ;; Something already set this, overwrite it.
763 (funcall set symbol (eval value))))
764 (error
765 (message "Error setting %s: %s" symbol data)))
e1bab181
RS
766 (setq args (cdr args))
767 (and (or now (default-boundp symbol))
768 (put symbol 'variable-comment comment)))
769 ;; Old format, a plist of SYMBOL VALUE pairs.
770 (message "Warning: old format `custom-set-variables'")
771 (ding)
772 (sit-for 2)
773 (let ((symbol (nth 0 args))
774 (value (nth 1 args)))
775 (put symbol 'saved-value (list value))
776 (custom-push-theme 'theme-value symbol theme 'set value))
777 (setq args (cdr (cdr args))))))))
55535639
PJ
778
779(defun custom-set-default (variable value)
780 "Default :set function for a customizable variable.
781Normally, this sets the default value of VARIABLE to VALUE,
782but if `custom-local-buffer' is non-nil,
783this sets the local binding in that buffer instead."
784 (if custom-local-buffer
785 (with-current-buffer custom-local-buffer
786 (set variable value))
787 (set-default variable value)))
788
6d912ee1
MB
789(defun custom-quote (sexp)
790 "Quote SEXP iff it is not self quoting."
791 (if (or (memq sexp '(t nil))
792 (keywordp sexp)
793 (and (listp sexp)
794 (memq (car sexp) '(lambda)))
795 (stringp sexp)
796 (numberp sexp)
797 (vectorp sexp)
798;;; (and (fboundp 'characterp)
799;;; (characterp sexp))
800 )
801 sexp
802 (list 'quote sexp)))
803
804(defun customize-mark-to-save (symbol)
805 "Mark SYMBOL for later saving.
806
807If the default value of SYMBOL is different from the standard value,
808set the `saved-value' property to a list whose car evaluates to the
809default value. Otherwise, set it til nil.
810
811To actually save the value, call `custom-save-all'.
812
813Return non-nil iff the `saved-value' property actually changed."
814 (let* ((get (or (get symbol 'custom-get) 'default-value))
815 (value (funcall get symbol))
816 (saved (get symbol 'saved-value))
817 (standard (get symbol 'standard-value))
818 (comment (get symbol 'customized-variable-comment)))
819 ;; Save default value iff different from standard value.
820 (if (or (null standard)
821 (not (equal value (condition-case nil
822 (eval (car standard))
823 (error nil)))))
824 (put symbol 'saved-value (list (custom-quote value)))
825 (put symbol 'saved-value nil))
826 ;; Clear customized information (set, but not saved).
827 (put symbol 'customized-value nil)
828 ;; Save any comment that might have been set.
829 (when comment
830 (put symbol 'saved-variable-comment comment))
831 (not (equal saved (get symbol 'saved-value)))))
832
833(defun customize-mark-as-set (symbol)
834 "Mark current value of SYMBOL as being set from customize.
835
836If the default value of SYMBOL is different from the saved value if any,
837or else if it is different from the standard value, set the
838`customized-value' property to a list whose car evaluates to the
839default value. Otherwise, set it til nil.
840
841Return non-nil iff the `customized-value' property actually changed."
842 (let* ((get (or (get symbol 'custom-get) 'default-value))
843 (value (funcall get symbol))
844 (customized (get symbol 'customized-value))
845 (old (or (get symbol 'saved-value) (get symbol 'standard-value))))
846 ;; Mark default value as set iff different from old value.
847 (if (or (null old)
848 (not (equal value (condition-case nil
849 (eval (car old))
850 (error nil)))))
851 (put symbol 'customized-value (list (custom-quote value)))
852 (put symbol 'customized-value nil))
853 ;; Changed?
854 (not (equal customized (get symbol 'customized-value)))))
855
e1bab181
RS
856;;; Theme Manipulation
857
858(defvar custom-loaded-themes nil
859 "Themes in the order they are loaded.")
860
861(defun custom-theme-loaded-p (theme)
862 "Return non-nil when THEME has been loaded."
863 (memq theme custom-loaded-themes))
864
865(defun provide-theme (theme)
866 "Indicate that this file provides THEME.
867Add THEME to `custom-loaded-themes' and `provide' whatever
868is stored in THEME's property `theme-feature'.
869
870Usually the theme-feature property contains a symbol created
871by `custom-make-theme-feature'."
872 (custom-check-theme theme)
873 (provide (get theme 'theme-feature))
874 (setq custom-loaded-themes (nconc (list theme) custom-loaded-themes)))
875
876(defun require-theme (theme)
877 "Try to load a theme by requiring its feature.
878THEME's feature is stored in THEME's `theme-feature' property.
879
880Usually the `theme-feature' property contains a symbol created
881by `custom-make-theme-feature'."
882 ;; Note we do no check for validity of the theme here.
883 ;; This allows to pull in themes by a file-name convention
884 (require (or (get theme 'theme-feature)
885 (custom-make-theme-feature theme))))
886
887(defun custom-remove-theme (spec-alist theme)
888 "Detelete all elements from SPEC-ALIST whose car is THEME."
889 (let ((elt (assoc theme spec-alist)))
890 (while elt
891 (setq spec-alist (delete elt spec-alist)
892 elt (assoc theme spec-alist))))
893 spec-alist)
894
895(defun custom-do-theme-reset (theme)
896 "Undo all settings defined by THEME.
897
898A variable remains unchanged if its property `theme-value' does not
899contain a value for THEME. A face remains unchanged if its property
900`theme-face' does not contain a value for THEME. In either case, all
901settings for THEME are removed from the property and the variable or
902face is set to the `user' theme.
903
904See `custom-known-themes' for a list of known themes."
905 (let (spec-list)
906 (mapatoms (lambda (symbol)
907 ;; This works even if symbol is both a variable and a
908 ;; face.
909 (setq spec-list (get symbol 'theme-value))
910 (when spec-list
911 (put symbol 'theme-value (custom-remove-theme spec-list theme))
912 (custom-theme-reset-internal symbol 'user))
913 (setq spec-list (get symbol 'theme-face))
914 (when spec-list
915 (put symbol 'theme-face (custom-remove-theme spec-list theme))
916 (custom-theme-reset-internal-face symbol 'user))))))
917
918(defun custom-theme-load-themes (by-theme &rest body)
919 "Load the themes specified by BODY.
920Record them as required by theme BY-THEME. BODY is a sequence of either
921
922THEME
923 BY-THEME requires THEME
924\(reset THEME)
925 Undo all the settings made by THEME
926\(hidden THEME)
927 Require THEME but hide it from the user
928
929All the themes loaded for BY-THEME are recorded in BY-THEME's property
930`theme-loads-themes'. Any theme loaded with the hidden predicate will
931be given the property `theme-hidden' unless it has been loaded before.
932Whether a theme has been loaded before is determined by the function
933`custom-theme-loaded-p'."
934 (custom-check-theme by-theme)
935 (let ((theme)
936 (themes-loaded (get by-theme 'theme-loads-themes)))
937 (while theme
938 (setq theme (car body)
939 body (cdr body))
940 (cond ((and (consp theme) (eq (car theme) 'reset))
941 (custom-do-theme-reset (cadr theme)))
942 ((and (consp theme) (eq (car theme) 'hidden))
943 (require-theme (cadr theme))
944 (unless (custom-theme-loaded-p (cadr theme))
945 (put (cadr theme) 'theme-hidden t)))
946 (t
947 (require-theme theme)
948 (put theme 'theme-hidden nil)))
949 (setq themes-loaded (nconc (list theme) themes-loaded)))
950 (put by-theme 'theme-loads-themes themes-loaded)))
951
952(defun custom-load-themes (&rest body)
953 "Load themes for the USER theme as specified by BODY.
954
955See `custom-theme-load-themes' for more information on BODY."
956 (apply 'custom-theme-load-themes 'user body))
957
958; (defsubst copy-upto-last (elt list)
959; "Copy all the elements of the list upto the last occurence of elt"
960; ;; Is it faster to do more work in C than to do less in elisp?
961; (nreverse (cdr (member elt (reverse list)))))
962
963(defun custom-theme-value (theme theme-spec-list)
964 "Determine the value for THEME defined by THEME-SPEC-LIST.
965Returns a list with the original value if found; nil otherwise.
966
967THEME-SPEC-LIST is an alist with themes as its key. As new themes are
968installed, these are added to the front of THEME-SPEC-LIST.
969Each element has the form
970
971 \(THEME MODE VALUE)
972
973MODE is either the symbol `set' or the symbol `reset'. See
974`custom-push-theme' for more information on the format of
975THEME-SPEC-LIST."
976 ;; Note we do _NOT_ signal an error if the theme is unknown
977 ;; it might have gone away without the user knowing.
978 (let ((value (cdr (assoc theme theme-spec-list))))
979 (if value
980 (if (eq (car value) 'set)
981 (cdr value)
982 (custom-theme-value (cadr value) theme-spec-list)))))
983
984(defun custom-theme-variable-value (variable theme)
985 "Return (list value) indicating value of VARIABLE in THEME.
986If THEME does not define a value for VARIABLE, return nil. The value
987definitions per theme are stored in VARIABLE's property `theme-value'.
988The actual work is done by function `custom-theme-value', which see.
989See `custom-push-theme' for more information on how these definitions
990are stored."
991 (custom-theme-value theme (get variable 'theme-value)))
992
993(defun custom-theme-reset-internal (symbol to-theme)
994 "Reset SYMBOL to the value defined by TO-THEME.
995If SYMBOL is not defined in TO-THEME, reset SYMBOL to the standard
996value. See `custom-theme-variable-value'. The standard value is
997stored in SYMBOL's property `standard-value'."
998 (let ((value (custom-theme-variable-value symbol to-theme))
999 was-in-theme)
1000 (setq was-in-theme value)
1001 (setq value (or value (get symbol 'standard-value)))
1002 (when value
1003 (put symbol 'saved-value was-in-theme)
1004 (if (or (get 'force-value symbol) (default-boundp symbol))
1005 (funcall (or (get symbol 'custom-set) 'set-default) symbol
1006 (eval (car value)))))
1007 value))
1008
1009(defun custom-theme-reset-variables (theme &rest args)
1010 "Reset the value of the variables to values previously defined.
1011Associate this setting with THEME.
1012
1013ARGS is a list of lists of the form
1014
1015 (VARIABLE TO-THEME)
1016
1017This means reset VARIABLE to its value in TO-THEME."
1018 (custom-check-theme theme)
1019 (mapcar '(lambda (arg)
1020 (apply 'custom-theme-reset-internal arg)
1021 (custom-push-theme 'theme-value (car arg) theme 'reset (cadr arg)))
1022 args))
1023
1024(defun custom-reset-variables (&rest args)
1025 "Reset the value of the variables to values previously saved.
1026This is the setting associated the `user' theme.
1027
1028ARGS is a list of lists of the form
1029
1030 (VARIABLE TO-THEME)
1031
1032This means reset VARIABLE to its value in TO-THEME."
1033 (apply 'custom-theme-reset-variables 'user args))
1034
55535639
PJ
1035;;; The End.
1036
1037;; Process the defcustoms for variables loaded before this file.
1038(while custom-declare-variable-list
1039 (apply 'custom-declare-variable (car custom-declare-variable-list))
1040 (setq custom-declare-variable-list (cdr custom-declare-variable-list)))
1041
1042(provide 'custom)
1043
1044;;; custom.el ends here