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