(diff-default-read-only): Change default.
[emacs.git] / lisp / custom.el
blobf5cfd55400a59b2add1a328c82f0c8eab5a552b4
1 ;;; custom.el --- tools for declaring and initializing options
2 ;;
3 ;; Copyright (C) 1996, 1997, 1999, 2001, 2002, 2004
4 ;; Free Software Foundation, Inc.
5 ;;
6 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
7 ;; Maintainer: FSF
8 ;; Keywords: help, faces
10 ;; This file is part of GNU Emacs.
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.
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.
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.
27 ;;; Commentary:
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.
33 ;; The code implementing face declarations is in `cus-face.el'.
35 ;;; Code:
37 (require 'widget)
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.")
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.")
48 (defvar custom-current-group-alist nil
49 "Alist of (FILE . GROUP) indicating the current group to use for FILE.")
51 ;;; The `defcustom' Macro.
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 used 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)))))
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)))))
79 (defun custom-initialize-reset (symbol value)
80 "Initialize SYMBOL based on VALUE.
81 Set the symbol, using its `:set' function (or `set-default' if it has none).
82 The value is either the symbol's current value
83 \(as obtained using the `:get' function), if any,
84 or the value in the symbol's `saved-value' property if any,
85 or (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))))
94 (eval value)))))
96 (defun custom-initialize-changed (symbol value)
97 "Initialize SYMBOL with VALUE.
98 Like `custom-initialize-reset', but only use the `:set' function if
99 not using the standard setting.
100 For 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)))))
111 (set-default symbol (eval value)))))
113 (defun custom-declare-variable (symbol default doc &rest args)
114 "Like `defcustom', but SYMBOL and DEFAULT are evaluated as normal arguments.
115 DEFAULT should be an expression to evaluate to compute the default value,
116 not the default value itself.
118 DEFAULT is stored as SYMBOL's value in the standard theme. See
119 `custom-known-themes' for a list of known themes. For backwards
120 compatibility, DEFAULT is also stored in SYMBOL's property
121 `standard-value'. At the same time, SYMBOL's property `force-value' is
122 set 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...
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))
134 (unless (memq :group args)
135 (custom-add-to-group (custom-current-group) symbol 'custom-variable))
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)
153 (push value requests))
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))
161 value)
162 ;; Fast code for the common case.
163 (put symbol 'custom-options (copy-sequence value))))
165 (custom-handle-keyword symbol keyword value
166 'custom-variable))))))
167 (put symbol 'custom-requests requests)
168 ;; Do the actual initialization.
169 (unless custom-dont-initialize
170 (funcall initialize symbol default)))
171 (push (cons 'defvar symbol) current-load-list)
172 (run-hooks 'custom-define-hook)
173 symbol)
175 (defmacro defcustom (symbol value doc &rest args)
176 "Declare SYMBOL as a customizable variable that defaults to VALUE.
177 DOC is the variable documentation.
179 Neither SYMBOL nor VALUE needs to be quoted.
180 If SYMBOL is not already bound, initialize it to VALUE.
181 The remaining arguments should have the form
183 [KEYWORD VALUE]...
185 The following keywords are meaningful:
187 :type VALUE should be a widget type for editing the symbol's value.
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.
191 :link LINK-DATA
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.
196 There are three alternatives you can use for LINK-DATA:
198 (custom-manual INFO-NODE)
199 Link to an Info node; INFO-NODE is a string which specifies
200 the node name, as in \"(emacs)Top\". The link appears as
201 `[manual]' in the customization buffer.
203 (info-link INFO-NODE)
204 Like `custom-manual' except that the link appears in the
205 customization buffer with the Info node name.
207 (url-link URL)
208 Link to a web page; URL is a string which specifies the URL.
209 The link appears in the customization buffer as URL.
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
213 example, (info-link :tag \"foo\" \"(emacs)Top\") makes a link to the
214 Emacs manual which appears in the buffer as `foo'.
216 An item can have more than one external link; however, most items
217 have none at all.
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
222 `custom-initialize-reset'.
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.
238 :tag LABEL
239 Use LABEL, a string, instead of the item's name, to label the item
240 in customization menus and buffers.
241 :load FILE
242 Load file FILE (a string) before displaying this customization
243 item. Loading is done with `load', and only if the file is
244 not already loaded.
245 :set-after VARIABLES
246 Specifies that SYMBOL should be set after the list of variables
247 VARIABLES when both have been customized.
249 Read the section about customization in the Emacs Lisp manual for more
250 information."
251 ;; It is better not to use backquote in this file,
252 ;; because that makes a bootstrapping problem
253 ;; if you need to recompile all the Lisp files using interpreted code.
254 (nconc (list 'custom-declare-variable
255 (list 'quote symbol)
256 (list 'quote value)
257 doc)
258 args))
260 ;;; The `defface' Macro.
262 (defmacro defface (face spec doc &rest args)
263 "Declare FACE as a customizable face that defaults to SPEC.
264 FACE does not need to be quoted.
266 Third argument DOC is the face documentation.
268 If FACE has been set with `custom-set-faces', set the face attributes
269 as specified by that function, otherwise set the face attributes
270 according to SPEC.
272 The remaining arguments should have the form
274 [KEYWORD VALUE]...
276 The following KEYWORDs are defined:
278 :group VALUE should be a customization group.
279 Add FACE to that group.
281 SPEC should be an alist of the form ((DISPLAY ATTS)...).
283 The first element of SPEC where the DISPLAY matches the frame
284 is the one that takes effect in that frame. The ATTRs in this
285 element take effect; the other elements are ignored, on that frame.
287 ATTS is a list of face attributes followed by their values:
288 (ATTR VALUE ATTR VALUE...)
290 The possible attributes are `:family', `:width', `:height', `:weight',
291 `:slant', `:underline', `:overline', `:strike-through', `:box',
292 `:foreground', `:background', `:stipple', `:inverse-video', and `:inherit'.
294 DISPLAY can either be the symbol t, which will match all frames, or an
295 alist of the form \((REQ ITEM...)...). For the DISPLAY to match a
296 FRAME, the REQ property of the frame must match one of the ITEM. The
297 following REQ are defined:
299 `type' (the value of `window-system')
300 Under X, in addition to the values `window-system' can take,
301 `motif', `lucid' and `x-toolkit' are allowed, and match when
302 the Motif toolkit, Lucid toolkit, or any X toolkit is in use.
304 `class' (the frame's color support)
305 Should be one of `color', `grayscale', or `mono'.
307 `background' (what color is used for the background text)
308 Should be one of `light' or `dark'.
310 `min-colors' (the minimum number of colors the frame should support)
311 Should be an integer, it is compared with the result of
312 `display-color-cells'.
314 Read the section about customization in the Emacs Lisp manual for more
315 information."
316 ;; It is better not to use backquote in this file,
317 ;; because that makes a bootstrapping problem
318 ;; if you need to recompile all the Lisp files using interpreted code.
319 (nconc (list 'custom-declare-face (list 'quote face) spec doc) args))
321 ;;; The `defgroup' Macro.
323 (defun custom-current-group ()
324 (cdr (assoc load-file-name custom-current-group-alist)))
326 (defun custom-declare-group (symbol members doc &rest args)
327 "Like `defgroup', but SYMBOL is evaluated as a normal argument."
328 (while members
329 (apply 'custom-add-to-group symbol (car members))
330 (setq members (cdr members)))
331 (when doc
332 ;; This text doesn't get into DOC.
333 (put symbol 'group-documentation (purecopy doc)))
334 (while args
335 (let ((arg (car args)))
336 (setq args (cdr args))
337 (unless (symbolp arg)
338 (error "Junk in args %S" args))
339 (let ((keyword arg)
340 (value (car args)))
341 (unless args
342 (error "Keyword %s is missing an argument" keyword))
343 (setq args (cdr args))
344 (cond ((eq keyword :prefix)
345 (put symbol 'custom-prefix value))
347 (custom-handle-keyword symbol keyword value
348 'custom-group))))))
349 ;; Record the group on the `current' list.
350 (let ((elt (assoc load-file-name custom-current-group-alist)))
351 (if elt (setcdr elt symbol)
352 (push (cons load-file-name symbol) custom-current-group-alist)))
353 (run-hooks 'custom-define-hook)
354 symbol)
356 (defmacro defgroup (symbol members doc &rest args)
357 "Declare SYMBOL as a customization group containing MEMBERS.
358 SYMBOL does not need to be quoted.
360 Third arg DOC is the group documentation.
362 MEMBERS should be an alist of the form ((NAME WIDGET)...) where
363 NAME is a symbol and WIDGET is a widget for editing that symbol.
364 Useful widgets are `custom-variable' for editing variables,
365 `custom-face' for edit faces, and `custom-group' for editing groups.
367 The remaining arguments should have the form
369 [KEYWORD VALUE]...
371 The following KEYWORDs are defined:
373 :group VALUE should be a customization group.
374 Add SYMBOL to that group.
376 :version VALUE should be a string specifying that the group was introduced
377 in Emacs version VERSION.
379 Read the section about customization in the Emacs Lisp manual for more
380 information."
381 ;; It is better not to use backquote in this file,
382 ;; because that makes a bootstrapping problem
383 ;; if you need to recompile all the Lisp files using interpreted code.
384 (nconc (list 'custom-declare-group (list 'quote symbol) members doc) args))
386 (defun custom-add-to-group (group option widget)
387 "To existing GROUP add a new OPTION of type WIDGET.
388 If there already is an entry for OPTION and WIDGET, nothing is done."
389 (let ((members (get group 'custom-group))
390 (entry (list option widget)))
391 (unless (member entry members)
392 (put group 'custom-group (nconc members (list entry))))))
394 (defun custom-group-of-mode (mode)
395 "Return the custom group corresponding to the major or minor MODE.
396 If no such group is found, return nil."
397 (or (get mode 'custom-mode-group)
398 (if (or (get mode 'custom-group)
399 (and (string-match "-mode\\'" (symbol-name mode))
400 (get (setq mode (intern (substring (symbol-name mode)
401 0 (match-beginning 0))))
402 'custom-group)))
403 mode)))
405 ;;; Properties.
407 (defun custom-handle-all-keywords (symbol args type)
408 "For customization option SYMBOL, handle keyword arguments ARGS.
409 Third argument TYPE is the custom option type."
410 (unless (memq :group args)
411 (custom-add-to-group (custom-current-group) symbol type))
412 (while args
413 (let ((arg (car args)))
414 (setq args (cdr args))
415 (unless (symbolp arg)
416 (error "Junk in args %S" args))
417 (let ((keyword arg)
418 (value (car args)))
419 (unless args
420 (error "Keyword %s is missing an argument" keyword))
421 (setq args (cdr args))
422 (custom-handle-keyword symbol keyword value type)))))
424 (defun custom-handle-keyword (symbol keyword value type)
425 "For customization option SYMBOL, handle KEYWORD with VALUE.
426 Fourth argument TYPE is the custom option type."
427 (if purify-flag
428 (setq value (purecopy value)))
429 (cond ((eq keyword :group)
430 (custom-add-to-group value symbol type))
431 ((eq keyword :version)
432 (custom-add-version symbol value))
433 ((eq keyword :link)
434 (custom-add-link symbol value))
435 ((eq keyword :load)
436 (custom-add-load symbol value))
437 ((eq keyword :tag)
438 (put symbol 'custom-tag value))
439 ((eq keyword :set-after)
440 (custom-add-dependencies symbol value))
442 (error "Unknown keyword %s" keyword))))
444 (defun custom-add-dependencies (symbol value)
445 "To the custom option SYMBOL, add dependencies specified by VALUE.
446 VALUE should be a list of symbols. For each symbol in that list,
447 this specifies that SYMBOL should be set after the specified symbol, if
448 both appear in constructs like `custom-set-variables'."
449 (unless (listp value)
450 (error "Invalid custom dependency `%s'" value))
451 (let* ((deps (get symbol 'custom-dependencies))
452 (new-deps deps))
453 (while value
454 (let ((dep (car value)))
455 (unless (symbolp dep)
456 (error "Invalid custom dependency `%s'" dep))
457 (unless (memq dep new-deps)
458 (setq new-deps (cons dep new-deps)))
459 (setq value (cdr value))))
460 (unless (eq deps new-deps)
461 (put symbol 'custom-dependencies new-deps))))
463 (defun custom-add-option (symbol option)
464 "To the variable SYMBOL add OPTION.
466 If SYMBOL is a hook variable, OPTION should be a hook member.
467 For other types variables, the effect is undefined."
468 (let ((options (get symbol 'custom-options)))
469 (unless (member option options)
470 (put symbol 'custom-options (cons option options)))))
472 (defun custom-add-link (symbol widget)
473 "To the custom option SYMBOL add the link WIDGET."
474 (let ((links (get symbol 'custom-links)))
475 (unless (member widget links)
476 (put symbol 'custom-links (cons (purecopy widget) links)))))
478 (defun custom-add-version (symbol version)
479 "To the custom option SYMBOL add the version VERSION."
480 (put symbol 'custom-version (purecopy version)))
482 (defun custom-add-load (symbol load)
483 "To the custom option SYMBOL add the dependency LOAD.
484 LOAD should be either a library file name, or a feature name."
485 (let ((loads (get symbol 'custom-loads)))
486 (unless (member load loads)
487 (put symbol 'custom-loads (cons (purecopy load) loads)))))
489 (defun custom-autoload (symbol load)
490 "Mark SYMBOL as autoloaded custom variable and add dependency LOAD."
491 (put symbol 'custom-autoload t)
492 (custom-add-load symbol load))
494 ;; This test is also in the C code of `user-variable-p'.
495 (defun custom-variable-p (variable)
496 "Return non-nil if VARIABLE is a custom variable."
497 (or (get variable 'standard-value)
498 (get variable 'custom-autoload)))
500 ;;; Loading files needed to customize a symbol.
501 ;;; This is in custom.el because menu-bar.el needs it for toggle cmds.
503 (defvar custom-load-recursion nil
504 "Hack to avoid recursive dependencies.")
506 (defun custom-load-symbol (symbol)
507 "Load all dependencies for SYMBOL."
508 (unless custom-load-recursion
509 (let ((custom-load-recursion t))
510 ;; Load these files if not already done,
511 ;; to make sure we know all the dependencies of SYMBOL.
512 (condition-case nil
513 (require 'cus-load)
514 (error nil))
515 (condition-case nil
516 (require 'cus-start)
517 (error nil))
518 (dolist (load (get symbol 'custom-loads))
519 (cond ((symbolp load) (condition-case nil (require load) (error nil)))
520 ;; This is subsumed by the test below, but it's much faster.
521 ((assoc load load-history))
522 ;; This was just (assoc (locate-library load) load-history)
523 ;; but has been optimized not to load locate-library
524 ;; if not necessary.
525 ((let ((regexp (concat "\\(\\`\\|/\\)" (regexp-quote load)
526 "\\(\\'\\|\\.\\)"))
527 (found nil))
528 (dolist (loaded load-history)
529 (and (stringp (car loaded))
530 (string-match regexp (car loaded))
531 (setq found t)))
532 found))
533 ;; Without this, we would load cus-edit recursively.
534 ;; We are still loading it when we call this,
535 ;; and it is not in load-history yet.
536 ((equal load "cus-edit"))
537 (t (condition-case nil (load load) (error nil))))))))
539 (defvar custom-known-themes '(user standard)
540 "Themes that have been define with `deftheme'.
541 The default value is the list (user standard). The theme `standard'
542 contains the Emacs standard settings from the original Lisp files. The
543 theme `user' contains all the the settings the user customized and saved.
544 Additional themes declared with the `deftheme' macro will be added to
545 the front of this list.")
547 (defun custom-declare-theme (theme feature &optional doc &rest args)
548 "Like `deftheme', but THEME is evaluated as a normal argument.
549 FEATURE is the feature this theme provides. This symbol is created
550 from THEME by `custom-make-theme-feature'."
551 (add-to-list 'custom-known-themes theme)
552 (put theme 'theme-feature feature)
553 (when doc
554 (put theme 'theme-documentation doc))
555 (while args
556 (let ((arg (car args)))
557 (setq args (cdr args))
558 (unless (symbolp arg)
559 (error "Junk in args %S" args))
560 (let ((keyword arg)
561 (value (car args)))
562 (unless args
563 (error "Keyword %s is missing an argument" keyword))
564 (setq args (cdr args))
565 (cond ((eq keyword :short-description)
566 (put theme 'theme-short-description value))
567 ((eq keyword :immediate)
568 (put theme 'theme-immediate value))
569 ((eq keyword :variable-set-string)
570 (put theme 'theme-variable-set-string value))
571 ((eq keyword :variable-reset-string)
572 (put theme 'theme-variable-reset-string value))
573 ((eq keyword :face-set-string)
574 (put theme 'theme-face-set-string value))
575 ((eq keyword :face-reset-string)
576 (put theme 'theme-face-reset-string value)))))))
578 (defmacro deftheme (theme &optional doc &rest args)
579 "Declare custom theme THEME.
580 The optional argument DOC is a doc string describing the theme.
581 The remaining arguments should have the form
583 [KEYWORD VALUE]...
585 The following KEYWORD's are defined:
587 :short-description
588 VALUE is a short (one line) description of the theme. If not
589 given, DOC is used.
590 :immediate
591 If VALUE is non-nil, variables specified in this theme are set
592 immediately when loading the theme.
593 :variable-set-string
594 VALUE is a string used to indicate that a variable takes its
595 setting from this theme. It is passed to FORMAT with the name
596 of the theme as an additional argument. If not given, a
597 generic description is used.
598 :variable-reset-string
599 VALUE is a string used in the case a variable has been forced
600 to its value in this theme. It is passed to FORMAT with the
601 name of the theme as an additional argument. If not given, a
602 generic description is used.
603 :face-set-string
604 VALUE is a string used to indicate that a face takes its
605 setting from this theme. It is passed to FORMAT with the name
606 of the theme as an additional argument. If not given, a
607 generic description is used.
608 :face-reset-string
609 VALUE is a string used in the case a face has been forced to
610 its value in this theme. It is passed to FORMAT with the name
611 of the theme as an additional argument. If not given, a
612 generic description is used.
614 Any theme `foo' should be defined in a file called `foo-theme.el';
615 see `custom-make-theme-feature' for more information."
616 (let ((feature (custom-make-theme-feature theme)))
617 ;; It is better not to use backquote in this file,
618 ;; because that makes a bootstrapping problem
619 ;; if you need to recompile all the Lisp files using interpreted code.
620 (nconc (list 'custom-declare-theme
621 (list 'quote theme)
622 (list 'quote feature)
623 doc) args)))
625 (defun custom-make-theme-feature (theme)
626 "Given a symbol THEME, create a new symbol by appending \"-theme\".
627 Store this symbol in the `theme-feature' property of THEME.
628 Calling `provide-theme' to provide THEME actually puts `THEME-theme'
629 into `features'.
631 This allows for a file-name convention for autoloading themes:
632 Every theme X has a property `provide-theme' whose value is \"X-theme\".
633 \(require-theme X) then attempts to load the file `X-theme.el'."
634 (intern (concat (symbol-name theme) "-theme")))
636 (defsubst custom-theme-p (theme)
637 "Non-nil when THEME has been defined."
638 (memq theme custom-known-themes))
640 (defsubst custom-check-theme (theme)
641 "Check whether THEME is valid, and signal an error if it is not."
642 (unless (custom-theme-p theme)
643 (error "Unknown theme `%s'" theme)))
645 ;;; Initializing.
647 (defun custom-push-theme (prop symbol theme mode value)
648 "Add (THEME MODE VALUE) to the list in property PROP of SYMBOL.
649 If the first element in that list is already (THEME ...),
650 discard it first.
652 MODE can be either the symbol `set' or the symbol `reset'. If it is the
653 symbol `set', then VALUE is the value to use. If it is the symbol
654 `reset', then VALUE is the mode to query instead.
656 In the following example for the variable `goto-address-url-face', the
657 theme `subtle-hacker' uses the same value for the variable as the theme
658 `gnome2':
660 \((standard set bold)
661 \(gnome2 set info-xref)
662 \(jonadab set underline)
663 \(subtle-hacker reset gnome2))
666 If a value has been stored for themes A B and C, and a new value
667 is to be stored for theme C, then the old value of C is discarded.
668 If a new value is to be stored for theme B, however, the old value
669 of B is not discarded because B is not the car of the list.
671 For variables, list property PROP is `theme-value'.
672 For faces, list property PROP is `theme-face'.
673 This is used in `custom-do-theme-reset', for example.
675 The list looks the same in any case; the examples shows a possible
676 value of the `theme-face' property for the face `region':
678 \((gnome2 set ((t (:foreground \"cyan\" :background \"dark cyan\"))))
679 \(standard set ((((class color) (background dark))
680 \(:background \"blue\"))
681 \(t (:background \"gray\")))))
683 This records values for the `standard' and the `gnome2' themes.
684 The user has not customized the face; had he done that,
685 the list would contain an entry for the `user' theme, too.
686 See `custom-known-themes' for a list of known themes."
687 (let ((old (get symbol prop)))
688 (if (eq (car-safe (car-safe old)) theme)
689 (setq old (cdr old)))
690 (put symbol prop (cons (list theme mode value) old))))
692 (defvar custom-local-buffer nil
693 "Non-nil, in a Customization buffer, means customize a specific buffer.
694 If this variable is non-nil, it should be a buffer,
695 and it means customize the local bindings of that buffer.
696 This variable is a permanent local, and it normally has a local binding
697 in every Customization buffer.")
698 (put 'custom-local-buffer 'permanent-local t)
700 (defun custom-set-variables (&rest args)
701 "Initialize variables according to user preferences.
702 The settings are registered as theme `user'.
703 The arguments should each be a list of the form:
705 (SYMBOL VALUE [NOW [REQUEST [COMMENT]]])
707 The unevaluated VALUE is stored as the saved value for SYMBOL.
708 If NOW is present and non-nil, VALUE is also evaluated and bound as
709 the default value for the SYMBOL.
711 REQUEST is a list of features we must 'require for SYMBOL.
712 COMMENT is a comment string about SYMBOL."
713 (apply 'custom-theme-set-variables 'user args))
715 (defun custom-theme-set-variables (theme &rest args)
716 "Initialize variables according to settings specified by args.
717 Records the settings as belonging to THEME.
719 The arguments should be a list where each entry has the form:
721 (SYMBOL VALUE [NOW [REQUEST [COMMENT]]])
723 The unevaluated VALUE is stored as the saved value for SYMBOL.
724 If NOW is present and non-nil, VALUE is also evaluated and bound as
725 the default value for the SYMBOL.
726 REQUEST is a list of features we must 'require for SYMBOL.
727 COMMENT is a comment string about SYMBOL.
729 Several properties of THEME and SYMBOL are used in the process:
731 If THEME property `theme-immediate' is non-nil, this is equivalent of
732 providing the NOW argument to all symbols in the argument list: SYMBOL
733 is bound to the evaluated VALUE. The only difference is SYMBOL property
734 `force-value': if NOW is non-nil, SYMBOL's property `force-value' is set to
735 the symbol `rogue', else if THEME's property `theme-immediate' is non-nil,
736 FACE's property `force-face' is set to the symbol `immediate'.
738 VALUE itself is saved unevaluated as SYMBOL property `saved-value' and
739 in SYMBOL's list property `theme-value' \(using `custom-push-theme')."
740 (custom-check-theme theme)
741 (let ((immediate (get theme 'theme-immediate)))
742 (setq args
743 (sort args
744 (lambda (a1 a2)
745 (let* ((sym1 (car a1))
746 (sym2 (car a2))
747 (1-then-2 (memq sym1 (get sym2 'custom-dependencies)))
748 (2-then-1 (memq sym2 (get sym1 'custom-dependencies))))
749 (cond ((and 1-then-2 2-then-1)
750 (error "Circular custom dependency between `%s' and `%s'"
751 sym1 sym2))
752 (2-then-1 nil)
753 ;; Put symbols with :require last. The macro
754 ;; define-minor-mode generates a defcustom
755 ;; with a :require and a :set, where the
756 ;; setter function calls the mode function.
757 ;; Putting symbols with :require last ensures
758 ;; that the mode function will see other
759 ;; customized values rather than default
760 ;; values.
761 (t (nth 3 a2)))))))
762 (while args
763 (let ((entry (car args)))
764 (if (listp entry)
765 (let* ((symbol (nth 0 entry))
766 (value (nth 1 entry))
767 (now (nth 2 entry))
768 (requests (nth 3 entry))
769 (comment (nth 4 entry))
770 set)
771 (when requests
772 (put symbol 'custom-requests requests)
773 (mapc 'require requests))
774 (setq set (or (get symbol 'custom-set) 'custom-set-default))
775 (put symbol 'saved-value (list value))
776 (put symbol 'saved-variable-comment comment)
777 (custom-push-theme 'theme-value symbol theme 'set value)
778 ;; Allow for errors in the case where the setter has
779 ;; changed between versions, say, but let the user know.
780 (condition-case data
781 (cond (now
782 ;; Rogue variable, set it now.
783 (put symbol 'force-value t)
784 (funcall set symbol (eval value)))
785 ((default-boundp symbol)
786 ;; Something already set this, overwrite it.
787 (funcall set symbol (eval value))))
788 (error
789 (message "Error setting %s: %s" symbol data)))
790 (setq args (cdr args))
791 (and (or now (default-boundp symbol))
792 (put symbol 'variable-comment comment)))
793 ;; Old format, a plist of SYMBOL VALUE pairs.
794 (message "Warning: old format `custom-set-variables'")
795 (ding)
796 (sit-for 2)
797 (let ((symbol (nth 0 args))
798 (value (nth 1 args)))
799 (put symbol 'saved-value (list value))
800 (custom-push-theme 'theme-value symbol theme 'set value))
801 (setq args (cdr (cdr args))))))))
803 (defun custom-set-default (variable value)
804 "Default :set function for a customizable variable.
805 Normally, this sets the default value of VARIABLE to VALUE,
806 but if `custom-local-buffer' is non-nil,
807 this sets the local binding in that buffer instead."
808 (if custom-local-buffer
809 (with-current-buffer custom-local-buffer
810 (set variable value))
811 (set-default variable value)))
813 (defun custom-set-minor-mode (variable value)
814 ":set function for minor mode variables.
815 Normally, this sets the default value of VARIABLE to nil if VALUE
816 is nil and to t otherwise,
817 but if `custom-local-buffer' is non-nil,
818 this sets the local binding in that buffer instead."
819 (if custom-local-buffer
820 (with-current-buffer custom-local-buffer
821 (funcall variable (or value 0)))
822 (funcall variable (or value 0))))
824 (defun custom-quote (sexp)
825 "Quote SEXP iff it is not self quoting."
826 (if (or (memq sexp '(t nil))
827 (keywordp sexp)
828 (and (listp sexp)
829 (memq (car sexp) '(lambda)))
830 (stringp sexp)
831 (numberp sexp)
832 (vectorp sexp)
833 ;;; (and (fboundp 'characterp)
834 ;;; (characterp sexp))
836 sexp
837 (list 'quote sexp)))
839 (defun customize-mark-to-save (symbol)
840 "Mark SYMBOL for later saving.
842 If the default value of SYMBOL is different from the standard value,
843 set the `saved-value' property to a list whose car evaluates to the
844 default value. Otherwise, set it to nil.
846 To actually save the value, call `custom-save-all'.
848 Return non-nil iff the `saved-value' property actually changed."
849 (let* ((get (or (get symbol 'custom-get) 'default-value))
850 (value (funcall get symbol))
851 (saved (get symbol 'saved-value))
852 (standard (get symbol 'standard-value))
853 (comment (get symbol 'customized-variable-comment)))
854 ;; Save default value iff different from standard value.
855 (if (or (null standard)
856 (not (equal value (condition-case nil
857 (eval (car standard))
858 (error nil)))))
859 (put symbol 'saved-value (list (custom-quote value)))
860 (put symbol 'saved-value nil))
861 ;; Clear customized information (set, but not saved).
862 (put symbol 'customized-value nil)
863 ;; Save any comment that might have been set.
864 (when comment
865 (put symbol 'saved-variable-comment comment))
866 (not (equal saved (get symbol 'saved-value)))))
868 (defun customize-mark-as-set (symbol)
869 "Mark current value of SYMBOL as being set from customize.
871 If the default value of SYMBOL is different from the saved value if any,
872 or else if it is different from the standard value, set the
873 `customized-value' property to a list whose car evaluates to the
874 default value. Otherwise, set it to nil.
876 Return non-nil iff the `customized-value' property actually changed."
877 (let* ((get (or (get symbol 'custom-get) 'default-value))
878 (value (funcall get symbol))
879 (customized (get symbol 'customized-value))
880 (old (or (get symbol 'saved-value) (get symbol 'standard-value))))
881 ;; Mark default value as set iff different from old value.
882 (if (or (null old)
883 (not (equal value (condition-case nil
884 (eval (car old))
885 (error nil)))))
886 (put symbol 'customized-value (list (custom-quote value)))
887 (put symbol 'customized-value nil))
888 ;; Changed?
889 (not (equal customized (get symbol 'customized-value)))))
891 ;;; Theme Manipulation
893 (defvar custom-loaded-themes nil
894 "Themes in the order they are loaded.")
896 (defun custom-theme-loaded-p (theme)
897 "Return non-nil when THEME has been loaded."
898 (memq theme custom-loaded-themes))
900 (defun provide-theme (theme)
901 "Indicate that this file provides THEME.
902 Add THEME to `custom-loaded-themes' and `provide' whatever
903 is stored in THEME's property `theme-feature'.
905 Usually the theme-feature property contains a symbol created
906 by `custom-make-theme-feature'."
907 (custom-check-theme theme)
908 (provide (get theme 'theme-feature))
909 (setq custom-loaded-themes (nconc (list theme) custom-loaded-themes)))
911 (defun require-theme (theme)
912 "Try to load a theme by requiring its feature.
913 THEME's feature is stored in THEME's `theme-feature' property.
915 Usually the `theme-feature' property contains a symbol created
916 by `custom-make-theme-feature'."
917 ;; Note we do no check for validity of the theme here.
918 ;; This allows to pull in themes by a file-name convention
919 (require (or (get theme 'theme-feature)
920 (custom-make-theme-feature theme))))
922 (defun custom-remove-theme (spec-alist theme)
923 "Delete all elements from SPEC-ALIST whose car is THEME."
924 (let ((elt (assoc theme spec-alist)))
925 (while elt
926 (setq spec-alist (delete elt spec-alist)
927 elt (assoc theme spec-alist))))
928 spec-alist)
930 (defun custom-do-theme-reset (theme)
931 "Undo all settings defined by THEME.
933 A variable remains unchanged if its property `theme-value' does not
934 contain a value for THEME. A face remains unchanged if its property
935 `theme-face' does not contain a value for THEME. In either case, all
936 settings for THEME are removed from the property and the variable or
937 face is set to the `user' theme.
939 See `custom-known-themes' for a list of known themes."
940 (let (spec-list)
941 (mapatoms (lambda (symbol)
942 ;; This works even if symbol is both a variable and a
943 ;; face.
944 (setq spec-list (get symbol 'theme-value))
945 (when spec-list
946 (put symbol 'theme-value (custom-remove-theme spec-list theme))
947 (custom-theme-reset-internal symbol 'user))
948 (setq spec-list (get symbol 'theme-face))
949 (when spec-list
950 (put symbol 'theme-face (custom-remove-theme spec-list theme))
951 (custom-theme-reset-internal-face symbol 'user))))))
953 (defun custom-theme-load-themes (by-theme &rest body)
954 "Load the themes specified by BODY.
955 Record them as required by theme BY-THEME. BODY is a sequence of either
957 THEME
958 BY-THEME requires THEME
959 \(reset THEME)
960 Undo all the settings made by THEME
961 \(hidden THEME)
962 Require THEME but hide it from the user
964 All the themes loaded for BY-THEME are recorded in BY-THEME's property
965 `theme-loads-themes'. Any theme loaded with the hidden predicate will
966 be given the property `theme-hidden' unless it has been loaded before.
967 Whether a theme has been loaded before is determined by the function
968 `custom-theme-loaded-p'."
969 (custom-check-theme by-theme)
970 (let ((theme)
971 (themes-loaded (get by-theme 'theme-loads-themes)))
972 (while theme
973 (setq theme (car body)
974 body (cdr body))
975 (cond ((and (consp theme) (eq (car theme) 'reset))
976 (custom-do-theme-reset (cadr theme)))
977 ((and (consp theme) (eq (car theme) 'hidden))
978 (require-theme (cadr theme))
979 (unless (custom-theme-loaded-p (cadr theme))
980 (put (cadr theme) 'theme-hidden t)))
982 (require-theme theme)
983 (put theme 'theme-hidden nil)))
984 (setq themes-loaded (nconc (list theme) themes-loaded)))
985 (put by-theme 'theme-loads-themes themes-loaded)))
987 (defun custom-load-themes (&rest body)
988 "Load themes for the USER theme as specified by BODY.
990 See `custom-theme-load-themes' for more information on BODY."
991 (apply 'custom-theme-load-themes 'user body))
993 ; (defsubst copy-upto-last (elt list)
994 ; "Copy all the elements of the list upto the last occurence of elt"
995 ; ;; Is it faster to do more work in C than to do less in elisp?
996 ; (nreverse (cdr (member elt (reverse list)))))
998 (defun custom-theme-value (theme theme-spec-list)
999 "Determine the value for THEME defined by THEME-SPEC-LIST.
1000 Returns a list with the original value if found; nil otherwise.
1002 THEME-SPEC-LIST is an alist with themes as its key. As new themes are
1003 installed, these are added to the front of THEME-SPEC-LIST.
1004 Each element has the form
1006 \(THEME MODE VALUE)
1008 MODE is either the symbol `set' or the symbol `reset'. See
1009 `custom-push-theme' for more information on the format of
1010 THEME-SPEC-LIST."
1011 ;; Note we do _NOT_ signal an error if the theme is unknown
1012 ;; it might have gone away without the user knowing.
1013 (let ((value (cdr (assoc theme theme-spec-list))))
1014 (if value
1015 (if (eq (car value) 'set)
1016 (cdr value)
1017 (custom-theme-value (cadr value) theme-spec-list)))))
1019 (defun custom-theme-variable-value (variable theme)
1020 "Return (list value) indicating value of VARIABLE in THEME.
1021 If THEME does not define a value for VARIABLE, return nil. The value
1022 definitions per theme are stored in VARIABLE's property `theme-value'.
1023 The actual work is done by function `custom-theme-value', which see.
1024 See `custom-push-theme' for more information on how these definitions
1025 are stored."
1026 (custom-theme-value theme (get variable 'theme-value)))
1028 (defun custom-theme-reset-internal (symbol to-theme)
1029 "Reset SYMBOL to the value defined by TO-THEME.
1030 If SYMBOL is not defined in TO-THEME, reset SYMBOL to the standard
1031 value. See `custom-theme-variable-value'. The standard value is
1032 stored in SYMBOL's property `standard-value'."
1033 (let ((value (custom-theme-variable-value symbol to-theme))
1034 was-in-theme)
1035 (setq was-in-theme value)
1036 (setq value (or value (get symbol 'standard-value)))
1037 (when value
1038 (put symbol 'saved-value was-in-theme)
1039 (if (or (get 'force-value symbol) (default-boundp symbol))
1040 (funcall (or (get symbol 'custom-set) 'set-default) symbol
1041 (eval (car value)))))
1042 value))
1044 (defun custom-theme-reset-variables (theme &rest args)
1045 "Reset the value of the variables to values previously defined.
1046 Associate this setting with THEME.
1048 ARGS is a list of lists of the form
1050 (VARIABLE TO-THEME)
1052 This means reset VARIABLE to its value in TO-THEME."
1053 (custom-check-theme theme)
1054 (mapcar '(lambda (arg)
1055 (apply 'custom-theme-reset-internal arg)
1056 (custom-push-theme 'theme-value (car arg) theme 'reset (cadr arg)))
1057 args))
1059 (defun custom-reset-variables (&rest args)
1060 "Reset the value of the variables to values previously saved.
1061 This is the setting associated the `user' theme.
1063 ARGS is a list of lists of the form
1065 (VARIABLE TO-THEME)
1067 This means reset VARIABLE to its value in TO-THEME."
1068 (apply 'custom-theme-reset-variables 'user args))
1070 ;;; The End.
1072 ;; Process the defcustoms for variables loaded before this file.
1073 (while custom-declare-variable-list
1074 (apply 'custom-declare-variable (car custom-declare-variable-list))
1075 (setq custom-declare-variable-list (cdr custom-declare-variable-list)))
1077 (provide 'custom)
1079 ;;; arch-tag: 041b6116-aabe-4f9a-902d-74092bc3dab2
1080 ;;; custom.el ends here