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