(custom-initialize-safe-set, custom-initialize-safe-default): New functions.
[bpt/emacs.git] / lisp / custom.el
1 ;;; custom.el --- tools for declaring and initializing options
2 ;;
3 ;; Copyright (C) 1996, 1997, 1999, 2001, 2002, 2004, 2005
4 ;; Free Software Foundation, Inc.
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., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
26
27 ;;; Commentary:
28 ;;
29 ;; This file only contains the code needed to declare and initialize
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
33 ;; The code implementing face declarations is in `cus-face.el'.
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
43 (defvar custom-dont-initialize nil
44 "Non-nil means `defcustom' should not initialize the variable.
45 That is used for the sake of `custom-make-dependencies'.
46 Users should not set it.")
47
48 (defvar custom-current-group-alist nil
49 "Alist of (FILE . GROUP) indicating the current group to use for FILE.")
50
51 ;;; The `defcustom' Macro.
52
53 (defun custom-initialize-default (symbol value)
54 "Initialize SYMBOL with VALUE.
55 This will do nothing if symbol already has a default binding.
56 Otherwise, if symbol has a `saved-value' property, it will evaluate
57 the car of that and use it as the default binding for symbol.
58 Otherwise, VALUE will be evaluated and used as the default binding for
59 symbol."
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.
68 If the symbol doesn't have a default binding already,
69 then set it using its `:set' function (or `set-default' if it has none).
70 The value is either the value in the symbol's `saved-value' property,
71 if 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-safe-set (symbol value)
80 "Like `custom-initialize-set', but catches errors.
81 If an error occurs during initialization, SYMBOL is set to nil
82 and no error is thrown. This is meant for use in pre-loaded files
83 where some variables used to compute VALUE are not yet defined.
84 You 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.
92 If an error occurs during initialization, SYMBOL is set to nil
93 and no error is thrown. This is meant for use in pre-loaded files
94 where some variables used to compute VALUE are not yet defined.
95 You 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
101 (defun custom-initialize-reset (symbol value)
102 "Initialize SYMBOL based on VALUE.
103 Set the symbol, using its `:set' function (or `set-default' if it has none).
104 The value is either the symbol's current value
105 \(as obtained using the `:get' function), if any,
106 or the value in the symbol's `saved-value' property if any,
107 or (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.
120 Like `custom-initialize-reset', but only use the `:set' function if
121 not using the standard setting.
122 For 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.
137 DEFAULT should be an expression to evaluate to compute the default value,
138 not the default value itself.
139
140 DEFAULT is stored as SYMBOL's value in the standard theme. See
141 `custom-known-themes' for a list of known themes. For backwards
142 compatibility, DEFAULT is also stored in SYMBOL's property
143 `standard-value'. At the same time, SYMBOL's property `force-value' is
144 set to nil, as the value is no longer rogue."
145 ;; Remember the standard setting. The value should be in the standard
146 ;; theme, not in this property. However, this would require changing
147 ;; the C source of defvar and others as well...
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))
156 (unless (memq :group args)
157 (custom-add-to-group (custom-current-group) symbol 'custom-variable))
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)
175 (push value requests))
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))
183 value)
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.
191 (unless custom-dont-initialize
192 (funcall initialize symbol default)))
193 (push symbol current-load-list)
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.
199 DOC is the variable documentation.
200
201 Neither SYMBOL nor VALUE need to be quoted.
202 If SYMBOL is not already bound, initialize it to VALUE.
203 The remaining arguments should have the form
204
205 [KEYWORD VALUE]...
206
207 The following keywords are meaningful:
208
209 :type VALUE should be a widget type for editing the symbol's value.
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.
213 :link LINK-DATA
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.
217
218 There are three alternatives you can use for LINK-DATA:
219
220 (custom-manual INFO-NODE)
221 Link to an Info node; INFO-NODE is a string which specifies
222 the node name, as in \"(emacs)Top\". The link appears as
223 `[manual]' in the customization buffer.
224
225 (info-link INFO-NODE)
226 Like `custom-manual' except that the link appears in the
227 customization buffer with the Info node name.
228
229 (url-link URL)
230 Link to a web page; URL is a string which specifies the URL.
231 The link appears in the customization buffer as URL.
232
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
235 example, (info-link :tag \"foo\" \"(emacs)Top\") makes a link to the
236 Emacs manual which appears in the buffer as `foo'.
237
238 An item can have more than one external link; however, most items
239 have none at all.
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
244 `custom-initialize-reset'.
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.
260 :tag LABEL
261 Use LABEL, a string, instead of the item's name, to label the item
262 in customization menus and buffers.
263 :load FILE
264 Load file FILE (a string) before displaying this customization
265 item. Loading is done with `load', and only if the file is
266 not already loaded.
267 :set-after VARIABLES
268 Specifies that SYMBOL should be set after the list of variables
269 VARIABLES when both have been customized.
270
271 If SYMBOL has a local binding, then this form affects the local
272 binding. This is normally not what you want. Thus, if you need
273 to 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
278 Read the section about customization in the Emacs Lisp manual for more
279 information."
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.
293 FACE does not need to be quoted.
294
295 Third argument DOC is the face documentation.
296
297 If FACE has been set with `custom-set-faces', set the face attributes
298 as specified by that function, otherwise set the face attributes
299 according to SPEC.
300
301 The remaining arguments should have the form
302
303 [KEYWORD VALUE]...
304
305 The following KEYWORDs are defined:
306
307 :group VALUE should be a customization group.
308 Add FACE to that group.
309
310 SPEC should be an alist of the form ((DISPLAY ATTS)...).
311
312 In the first element, DISPLAY can be :default. The ATTS in that
313 element then act as defaults for all the following elements.
314
315 Aside from that, DISPLAY specifies conditions to match some or
316 all frames. For each frame, the first element of SPEC where the
317 DISPLAY conditions are satisfied is the one that applies to that
318 frame. The ATTRs in this element take effect, and the following
319 elements are ignored, on that frame.
320
321 In the last element, DISPLAY can be t. That element applies to a
322 frame if none of the previous elements (except the :default if
323 any) did.
324
325 ATTS is a list of face attributes followed by their values:
326 (ATTR VALUE ATTR VALUE...)
327
328 The possible attributes are `:family', `:width', `:height', `:weight',
329 `:slant', `:underline', `:overline', `:strike-through', `:box',
330 `:foreground', `:background', `:stipple', `:inverse-video', and `:inherit'.
331
332 DISPLAY can be `:default' (only in the first element), the symbol
333 t (only in the last element) to match all frames, or an alist of
334 conditions of the form \(REQ ITEM...). For such an alist to
335 match a frame, each of the conditions must be satisfied, meaning
336 that the REQ property of the frame must match one of the
337 corresponding ITEMs. These are the defined REQ values:
338
339 `type' (the value of `window-system')
340 Under X, in addition to the values `window-system' can take,
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.
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
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
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
359 Read the section about customization in the Emacs Lisp manual for more
360 information."
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
368 (defun custom-current-group ()
369 (cdr (assoc load-file-name custom-current-group-alist)))
370
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)))
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))))))
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)))
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.
403 SYMBOL does not need to be quoted.
404
405 Third arg DOC is the group documentation.
406
407 MEMBERS should be an alist of the form ((NAME WIDGET)...) where
408 NAME is a symbol and WIDGET is a widget for editing that symbol.
409 Useful widgets are `custom-variable' for editing variables,
410 `custom-face' for edit faces, and `custom-group' for editing groups.
411
412 The remaining arguments should have the form
413
414 [KEYWORD VALUE]...
415
416 The 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
424 Read the section about customization in the Emacs Lisp manual for more
425 information."
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.
433 If 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
439 (defun custom-group-of-mode (mode)
440 "Return the custom group corresponding to the major or minor MODE.
441 If 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
450 ;;; Properties.
451
452 (defun custom-handle-all-keywords (symbol args type)
453 "For customization option SYMBOL, handle keyword arguments ARGS.
454 Third argument TYPE is the custom option type."
455 (unless (memq :group args)
456 (custom-add-to-group (custom-current-group) symbol type))
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.
471 Fourth 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.
491 VALUE should be a list of symbols. For each symbol in that list,
492 this specifies that SYMBOL should be set after the specified symbol, if
493 both 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))))
507
508 (defun custom-add-option (symbol option)
509 "To the variable SYMBOL add OPTION.
510
511 If SYMBOL's custom type is a hook, OPTION should be a hook member.
512 If SYMBOL's custom type is an alist, OPTION specifies a symbol
513 to offer to the user as a possible key in the alist.
514 For other custom types, this has no effect."
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.
531 LOAD 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
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)
543 "Return non-nil if VARIABLE is a custom variable.
544 This recursively follows aliases."
545 (setq variable (indirect-variable variable))
546 (or (get variable 'standard-value)
547 (get variable 'custom-autoload)))
548
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
558 (let ((custom-load-recursion t))
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))
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.
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.
574 ((let ((regexp (concat "\\(\\`\\|/\\)" (regexp-quote load)
575 "\\(\\'\\|\\.\\)"))
576 (found nil))
577 (dolist (loaded load-history)
578 (and (stringp (car loaded))
579 (string-match regexp (car loaded))
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"))
586 (t (condition-case nil (load load) (error nil))))))))
587
588 (defvar custom-known-themes '(user standard)
589 "Themes that have been defined with `deftheme'.
590 The default value is the list (user standard). The theme `standard'
591 contains the Emacs standard settings from the original Lisp files. The
592 theme `user' contains all the the settings the user customized and saved.
593 Additional themes declared with the `deftheme' macro will be added to
594 the 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.
598 FEATURE is the feature this theme provides. This symbol is created
599 from 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)
615 (put theme 'theme-short-description value))
616 ((eq keyword :immediate)
617 (put theme 'theme-immediate value))
618 ((eq keyword :variable-set-string)
619 (put theme 'theme-variable-set-string value))
620 ((eq keyword :variable-reset-string)
621 (put theme 'theme-variable-reset-string value))
622 ((eq keyword :face-set-string)
623 (put theme 'theme-face-set-string value))
624 ((eq keyword :face-reset-string)
625 (put theme 'theme-face-reset-string value)))))))
626
627 (defmacro deftheme (theme &optional doc &rest args)
628 "Declare custom theme THEME.
629 The optional argument DOC is a doc string describing the theme.
630 The remaining arguments should have the form
631
632 [KEYWORD VALUE]...
633
634 The 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
663 Any theme `foo' should be defined in a file called `foo-theme.el';
664 see `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\".
676 Store this symbol in the `theme-feature' property of THEME.
677 Calling `provide-theme' to provide THEME actually puts `THEME-theme'
678 into `features'.
679
680 This allows for a file-name convention for autoloading themes:
681 Every 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
694 ;;; Initializing.
695
696 (defun custom-push-theme (prop symbol theme mode value)
697 "Add (THEME MODE VALUE) to the list in property PROP of SYMBOL.
698 If the first element in that list is already (THEME ...),
699 discard it first.
700
701 MODE can be either the symbol `set' or the symbol `reset'. If it is the
702 symbol `set', then VALUE is the value to use. If it is the symbol
703 `reset', then VALUE is the mode to query instead.
704
705 In the following example for the variable `goto-address-url-face', the
706 theme `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
715 If a value has been stored for themes A B and C, and a new value
716 is to be stored for theme C, then the old value of C is discarded.
717 If a new value is to be stored for theme B, however, the old value
718 of B is not discarded because B is not the car of the list.
719
720 For variables, list property PROP is `theme-value'.
721 For faces, list property PROP is `theme-face'.
722 This is used in `custom-do-theme-reset', for example.
723
724 The list looks the same in any case; the examples shows a possible
725 value 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
732 This records values for the `standard' and the `gnome2' themes.
733 The user has not customized the face; had he done that,
734 the list would contain an entry for the `user' theme, too.
735 See `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
741 (defvar custom-local-buffer nil
742 "Non-nil, in a Customization buffer, means customize a specific buffer.
743 If this variable is non-nil, it should be a buffer,
744 and it means customize the local bindings of that buffer.
745 This variable is a permanent local, and it normally has a local binding
746 in every Customization buffer.")
747 (put 'custom-local-buffer 'permanent-local t)
748
749 (defun custom-set-variables (&rest args)
750 "Install user customizations of variable values specified in ARGS.
751 These settings are registered as theme `user'.
752 The arguments should each be a list of the form:
753
754 (SYMBOL EXP [NOW [REQUEST [COMMENT]]])
755
756 This stores EXP (without evaluating it) as the saved value for SYMBOL.
757 If NOW is present and non-nil, then also evaluate EXP and set
758 the default value for the SYMBOL to the value of EXP.
759
760 REQUEST is a list of features we must require in order to
761 handle SYMBOL properly.
762 COMMENT is a comment string about SYMBOL."
763 (apply 'custom-theme-set-variables 'user args))
764
765 (defun custom-reevaluate-setting (symbol)
766 "Reset the value of SYMBOL by re-evaluating its saved or default value.
767 This is useful for variables that are defined before their default value
768 can really be computed. E.g. dumped variables whose default depends on
769 run-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
774 (defun custom-theme-set-variables (theme &rest args)
775 "Initialize variables for theme THEME according to settings in ARGS.
776 Each of the arguments in ARGS should be a list of this form:
777
778 (SYMBOL EXP [NOW [REQUEST [COMMENT]]])
779
780 This stores EXP (without evaluating it) as the saved value for SYMBOL.
781 If NOW is present and non-nil, then also evaluate EXP and set
782 the default value for the SYMBOL to the value of EXP.
783
784 REQUEST is a list of features we must require in order to
785 handle SYMBOL properly.
786 COMMENT is a comment string about SYMBOL.
787
788 Several properties of THEME and SYMBOL are used in the process:
789
790 If THEME's property `theme-immediate' is non-nil, this is equivalent of
791 providing the NOW argument to all symbols in the argument list:
792 evaluate each EXP and set the corresponding SYMBOL. However,
793 there's a difference in the handling of SYMBOL's property
794 `force-value': if NOW is non-nil, SYMBOL's property `force-value' is set to
795 the symbol `rogue', else if THEME's property `theme-immediate' is non-nil,
796 SYMBOL's property `force-value' is set to the symbol `immediate'.
797
798 EXP itself is saved unevaluated as SYMBOL property `saved-value' and
799 in SYMBOL's list property `theme-value' \(using `custom-push-theme')."
800 (custom-check-theme theme)
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
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))))
847 (error
848 (message "Error setting %s: %s" symbol data)))
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)))))))
861
862 (defun custom-set-default (variable value)
863 "Default :set function for a customizable variable.
864 Normally, this sets the default value of VARIABLE to VALUE,
865 but if `custom-local-buffer' is non-nil,
866 this 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
872 (defun custom-set-minor-mode (variable value)
873 ":set function for minor mode variables.
874 Normally, this sets the default value of VARIABLE to nil if VALUE
875 is nil and to t otherwise,
876 but if `custom-local-buffer' is non-nil,
877 this sets the local binding in that buffer instead."
878 (if custom-local-buffer
879 (with-current-buffer custom-local-buffer
880 (funcall variable (if value 1 0)))
881 (funcall variable (if value 1 0))))
882
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
901 If the default value of SYMBOL is different from the standard value,
902 set the `saved-value' property to a list whose car evaluates to the
903 default value. Otherwise, set it to nil.
904
905 To actually save the value, call `custom-save-all'.
906
907 Return 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
930 If the default value of SYMBOL is different from the saved value if any,
931 or else if it is different from the standard value, set the
932 `customized-value' property to a list whose car evaluates to the
933 default value. Otherwise, set it to nil.
934
935 Return 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)
942 (not (equal value (condition-case nil
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
950 ;;; Theme Manipulation
951
952 (defvar custom-loaded-themes nil
953 "Themes in the order they are loaded.")
954
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.
962 The command `customize-create-theme' writes the files it produces
963 into this directory."
964 :type 'string
965 :group 'customize
966 :version "22.1")
967
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.
974 Add THEME to `custom-loaded-themes' and `provide' whatever
975 is stored in THEME's property `theme-feature'.
976
977 Usually the theme-feature property contains a symbol created
978 by `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.
985 THEME's feature is stored in THEME's `theme-feature' property.
986
987 Usually the `theme-feature' property contains a symbol created
988 by `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
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)))))
996
997 (defun custom-remove-theme (spec-alist theme)
998 "Delete all elements from SPEC-ALIST whose car is THEME."
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
1008 A variable remains unchanged if its property `theme-value' does not
1009 contain 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
1011 settings for THEME are removed from the property and the variable or
1012 face is set to the `user' theme.
1013
1014 See `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.
1030 Record them as required by theme BY-THEME. BODY is a sequence of either
1031
1032 THEME
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
1039 All 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
1041 be given the property `theme-hidden' unless it has been loaded before.
1042 Whether 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
1065 See `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.
1075 Returns a list with the original value if found; nil otherwise.
1076
1077 THEME-SPEC-LIST is an alist with themes as its key. As new themes are
1078 installed, these are added to the front of THEME-SPEC-LIST.
1079 Each element has the form
1080
1081 \(THEME MODE VALUE)
1082
1083 MODE is either the symbol `set' or the symbol `reset'. See
1084 `custom-push-theme' for more information on the format of
1085 THEME-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.
1096 If THEME does not define a value for VARIABLE, return nil. The value
1097 definitions per theme are stored in VARIABLE's property `theme-value'.
1098 The actual work is done by function `custom-theme-value', which see.
1099 See `custom-push-theme' for more information on how these definitions
1100 are 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.
1105 If SYMBOL is not defined in TO-THEME, reset SYMBOL to the standard
1106 value. See `custom-theme-variable-value'. The standard value is
1107 stored 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.
1121 Associate this setting with THEME.
1122
1123 ARGS is a list of lists of the form
1124
1125 (VARIABLE TO-THEME)
1126
1127 This 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.
1136 This is the setting associated the `user' theme.
1137
1138 ARGS is a list of lists of the form
1139
1140 (VARIABLE TO-THEME)
1141
1142 This means reset VARIABLE to its value in TO-THEME."
1143 (apply 'custom-theme-reset-variables 'user args))
1144
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
1154 ;; arch-tag: 041b6116-aabe-4f9a-902d-74092bc3dab2
1155 ;;; custom.el ends here