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