1 ;;; custom.el --- tools for declaring and initializing options
3 ;; Copyright (C) 1996, 1997, 1999, 2001, 2002 Free Software Foundation, Inc.
5 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
7 ;; Keywords: help, faces
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
28 ;; This file only contain the code needed to declare and initialize
29 ;; user options. The code to customize options is autoloaded from
30 ;; `cus-edit.el' and is documented in the Emacs Lisp Reference manual.
32 ;; The code implementing face declarations is in `cus-face.el'
38 (defvar custom-define-hook nil
39 ;; Customize information for this option is in `cus-edit.el'.
40 "Hook called after defining each customize option.")
42 (defvar custom-dont-initialize nil
43 "Non-nil means `defcustom' should not initialize the variable.
44 That is used for the sake of `custom-make-dependencies'.
45 Users should not set it.")
47 (defvar custom-current-group-alist nil
48 "Alist of (FILE . GROUP) indicating the current group to use for FILE.")
50 ;;; The `defcustom' Macro.
52 (defun custom-initialize-default (symbol value
)
53 "Initialize SYMBOL with VALUE.
54 This will do nothing if symbol already has a default binding.
55 Otherwise, if symbol has a `saved-value' property, it will evaluate
56 the car of that and used as the default binding for symbol.
57 Otherwise, VALUE will be evaluated and used as the default binding for
59 (unless (default-boundp symbol
)
60 ;; Use the saved value if it exists, otherwise the standard setting.
61 (set-default symbol
(if (get symbol
'saved-value
)
62 (eval (car (get symbol
'saved-value
)))
65 (defun custom-initialize-set (symbol value
)
66 "Initialize SYMBOL based on VALUE.
67 If the symbol doesn't have a default binding already,
68 then set it using its `:set' function (or `set-default' if it has none).
69 The value is either the value in the symbol's `saved-value' property,
71 (unless (default-boundp symbol
)
72 (funcall (or (get symbol
'custom-set
) 'set-default
)
74 (if (get symbol
'saved-value
)
75 (eval (car (get symbol
'saved-value
)))
78 (defun custom-initialize-reset (symbol value
)
79 "Initialize SYMBOL based on VALUE.
80 Set the symbol, using its `:set' function (or `set-default' if it has none).
81 The value is either the symbol's current value
82 \(as obtained using the `:get' function), if any,
83 or the value in the symbol's `saved-value' property if any,
84 or (last of all) VALUE."
85 (funcall (or (get symbol
'custom-set
) 'set-default
)
87 (cond ((default-boundp symbol
)
88 (funcall (or (get symbol
'custom-get
) 'default-value
)
90 ((get symbol
'saved-value
)
91 (eval (car (get symbol
'saved-value
))))
95 (defun custom-initialize-changed (symbol value
)
96 "Initialize SYMBOL with VALUE.
97 Like `custom-initialize-reset', but only use the `:set' function if
98 not using the standard setting.
99 For the standard setting, use `set-default'."
100 (cond ((default-boundp symbol
)
101 (funcall (or (get symbol
'custom-set
) 'set-default
)
103 (funcall (or (get symbol
'custom-get
) 'default-value
)
105 ((get symbol
'saved-value
)
106 (funcall (or (get symbol
'custom-set
) 'set-default
)
108 (eval (car (get symbol
'saved-value
)))))
110 (set-default symbol
(eval value
)))))
112 (defun custom-declare-variable (symbol default doc
&rest args
)
113 "Like `defcustom', but SYMBOL and DEFAULT are evaluated as normal arguments.
114 DEFAULT should be an expression to evaluate to compute the default value,
115 not the default value itself."
116 ;; Remember the standard setting.
117 (put symbol
'standard-value
(list default
))
118 ;; Maybe this option was rogue in an earlier version. It no longer is.
119 (when (get symbol
'force-value
)
120 (put symbol
'force-value nil
))
122 (put symbol
'variable-documentation doc
))
123 (let ((initialize 'custom-initialize-reset
)
125 (unless (memq :group args
)
126 (custom-add-to-group (custom-current-group) symbol
'custom-variable
))
128 (let ((arg (car args
)))
129 (setq args
(cdr args
))
130 (unless (symbolp arg
)
131 (error "Junk in args %S" args
))
135 (error "Keyword %s is missing an argument" keyword
))
136 (setq args
(cdr args
))
137 (cond ((eq keyword
:initialize
)
138 (setq initialize value
))
140 (put symbol
'custom-set value
))
142 (put symbol
'custom-get value
))
143 ((eq keyword
:require
)
144 (setq requests
(cons value requests
)))
146 (put symbol
'custom-type
(purecopy value
)))
147 ((eq keyword
:options
)
148 (if (get symbol
'custom-options
)
149 ;; Slow safe code to avoid duplicates.
150 (mapc (lambda (option)
151 (custom-add-option symbol option
))
153 ;; Fast code for the common case.
154 (put symbol
'custom-options
(copy-sequence value
))))
156 (custom-handle-keyword symbol keyword value
157 'custom-variable
))))))
158 (put symbol
'custom-requests requests
)
159 ;; Do the actual initialization.
160 (unless custom-dont-initialize
161 (funcall initialize symbol default
)))
162 (setq current-load-list
(cons symbol current-load-list
))
163 (run-hooks 'custom-define-hook
)
166 (defmacro defcustom
(symbol value doc
&rest args
)
167 "Declare SYMBOL as a customizable variable that defaults to VALUE.
168 DOC is the variable documentation.
170 Neither SYMBOL nor VALUE needs to be quoted.
171 If SYMBOL is not already bound, initialize it to VALUE.
172 The remaining arguments should have the form
176 The following keywords are meaningful:
178 :type VALUE should be a widget type for editing the symbol's value.
179 :options VALUE should be a list of valid members of the widget type.
180 :group VALUE should be a customization group.
181 Add SYMBOL to that group.
183 Include an external link after the documentation string for this
184 item. This is a sentence containing an active field which
185 references some other documentation.
187 There are three alternatives you can use for LINK-DATA:
189 (custom-manual INFO-NODE)
190 Link to an Info node; INFO-NODE is a string which specifies
191 the node name, as in \"(emacs)Top\". The link appears as
192 `[manual]' in the customization buffer.
194 (info-link INFO-NODE)
195 Like `custom-manual' except that the link appears in the
196 customization buffer with the Info node name.
199 Link to a web page; URL is a string which specifies the URL.
200 The link appears in the customization buffer as URL.
202 You can specify the text to use in the customization buffer by
203 adding `:tag NAME' after the first element of the LINK-DATA; for
204 example, (info-link :tag \"foo\" \"(emacs)Top\") makes a link to the
205 Emacs manual which appears in the buffer as `foo'.
207 An item can have more than one external link; however, most items
210 VALUE should be a function used to initialize the
211 variable. It takes two arguments, the symbol and value
212 given in the `defcustom' call. The default is
213 `custom-initialize-reset'.
214 :set VALUE should be a function to set the value of the symbol.
215 It takes two arguments, the symbol to set and the value to
216 give it. The default choice of function is `custom-set-default'.
217 :get VALUE should be a function to extract the value of symbol.
218 The function takes one argument, a symbol, and should return
219 the current value for that symbol. The default choice of function
220 is `custom-default-value'.
222 VALUE should be a feature symbol. If you save a value
223 for this option, then when your `.emacs' file loads the value,
224 it does (require VALUE) first.
226 VALUE should be a string specifying that the variable was
227 first introduced, or its default value was changed, in Emacs
230 Use LABEL, a string, instead of the item's name, to label the item
231 in customization menus and buffers.
233 Load file FILE (a string) before displaying this customization
234 item. Loading is done with `load-library', and only if the file is
237 Specifies that SYMBOL should be set after VARIABLE when
238 both have been customized.
240 Read the section about customization in the Emacs Lisp manual for more
242 ;; It is better not to use backquote in this file,
243 ;; because that makes a bootstrapping problem
244 ;; if you need to recompile all the Lisp files using interpreted code.
245 (nconc (list 'custom-declare-variable
251 ;;; The `defface' Macro.
253 (defmacro defface
(face spec doc
&rest args
)
254 "Declare FACE as a customizable face that defaults to SPEC.
255 FACE does not need to be quoted.
257 Third argument DOC is the face documentation.
259 If FACE has been set with `custom-set-face', set the face attributes
260 as specified by that function, otherwise set the face attributes
263 The remaining arguments should have the form
267 The following KEYWORDs are defined:
269 :group VALUE should be a customization group.
270 Add FACE to that group.
272 SPEC should be an alist of the form ((DISPLAY ATTS)...).
274 The first element of SPEC where the DISPLAY matches the frame
275 is the one that takes effect in that frame. The ATTRs in this
276 element take effect; the other elements are ignored, on that frame.
278 ATTS is a list of face attributes followed by their values:
279 (ATTR VALUE ATTR VALUE...)
281 The possible attributes are `:family', `:width', `:height', `:weight',
282 `:slant', `:underline', `:overline', `:strike-through', `:box',
283 `:foreground', `:background', `:stipple', `:inverse-video', and `:inherit'.
285 DISPLAY can either be the symbol t, which will match all frames, or an
286 alist of the form \((REQ ITEM...)...). For the DISPLAY to match a
287 FRAME, the REQ property of the frame must match one of the ITEM. The
288 following REQ are defined:
290 `type' (the value of `window-system')
291 Under X, in addition to the values `window-system' can take,
292 `motif', `lucid' and `x-toolkit' are allowed, and match when
293 the Motif toolkit, Lucid toolkit, or any X toolkit is in use.
295 `class' (the frame's color support)
296 Should be one of `color', `grayscale', or `mono'.
298 `background' (what color is used for the background text)
299 Should be one of `light' or `dark'.
301 Read the section about customization in the Emacs Lisp manual for more
303 ;; It is better not to use backquote in this file,
304 ;; because that makes a bootstrapping problem
305 ;; if you need to recompile all the Lisp files using interpreted code.
306 (nconc (list 'custom-declare-face
(list 'quote face
) spec doc
) args
))
308 ;;; The `defgroup' Macro.
310 (defun custom-current-group ()
311 (cdr (assoc load-file-name custom-current-group-alist
)))
313 (defun custom-declare-group (symbol members doc
&rest args
)
314 "Like `defgroup', but SYMBOL is evaluated as a normal argument."
316 (apply 'custom-add-to-group symbol
(car members
))
317 (setq members
(cdr members
)))
318 (put symbol
'custom-group
(nconc members
(get symbol
'custom-group
)))
320 ;; This text doesn't get into DOC.
321 (put symbol
'group-documentation
(purecopy doc
)))
323 (let ((arg (car args
)))
324 (setq args
(cdr args
))
325 (unless (symbolp arg
)
326 (error "Junk in args %S" args
))
330 (error "Keyword %s is missing an argument" keyword
))
331 (setq args
(cdr args
))
332 (cond ((eq keyword
:prefix
)
333 (put symbol
'custom-prefix value
))
335 (custom-handle-keyword symbol keyword value
337 ;; Record the group on the `current' list.
338 (let ((elt (assoc load-file-name custom-current-group-alist
)))
339 (if elt
(setcdr elt symbol
)
340 (push (cons load-file-name symbol
) custom-current-group-alist
)))
341 (run-hooks 'custom-define-hook
)
344 (defmacro defgroup
(symbol members doc
&rest args
)
345 "Declare SYMBOL as a customization group containing MEMBERS.
346 SYMBOL does not need to be quoted.
348 Third arg DOC is the group documentation.
350 MEMBERS should be an alist of the form ((NAME WIDGET)...) where
351 NAME is a symbol and WIDGET is a widget for editing that symbol.
352 Useful widgets are `custom-variable' for editing variables,
353 `custom-face' for edit faces, and `custom-group' for editing groups.
355 The remaining arguments should have the form
359 The following KEYWORDs are defined:
361 :group VALUE should be a customization group.
362 Add SYMBOL to that group.
364 :version VALUE should be a string specifying that the group was introduced
365 in Emacs version VERSION.
367 Read the section about customization in the Emacs Lisp manual for more
369 ;; It is better not to use backquote in this file,
370 ;; because that makes a bootstrapping problem
371 ;; if you need to recompile all the Lisp files using interpreted code.
372 (nconc (list 'custom-declare-group
(list 'quote symbol
) members doc
) args
))
374 (defun custom-add-to-group (group option widget
)
375 "To existing GROUP add a new OPTION of type WIDGET.
376 If there already is an entry for OPTION and WIDGET, nothing is done."
377 (let ((members (get group
'custom-group
))
378 (entry (list option widget
)))
379 (unless (member entry members
)
380 (put group
'custom-group
(nconc members
(list entry
))))))
384 (defun custom-handle-all-keywords (symbol args type
)
385 "For customization option SYMBOL, handle keyword arguments ARGS.
386 Third argument TYPE is the custom option type."
387 (unless (memq :group args
)
388 (custom-add-to-group (custom-current-group) symbol
'custom-face
))
390 (let ((arg (car args
)))
391 (setq args
(cdr args
))
392 (unless (symbolp arg
)
393 (error "Junk in args %S" args
))
397 (error "Keyword %s is missing an argument" keyword
))
398 (setq args
(cdr args
))
399 (custom-handle-keyword symbol keyword value type
)))))
401 (defun custom-handle-keyword (symbol keyword value type
)
402 "For customization option SYMBOL, handle KEYWORD with VALUE.
403 Fourth argument TYPE is the custom option type."
405 (setq value
(purecopy value
)))
406 (cond ((eq keyword
:group
)
407 (custom-add-to-group value symbol type
))
408 ((eq keyword
:version
)
409 (custom-add-version symbol value
))
411 (custom-add-link symbol value
))
413 (custom-add-load symbol value
))
415 (put symbol
'custom-tag value
))
416 ((eq keyword
:set-after
)
417 (custom-add-dependencies symbol value
))
419 (error "Unknown keyword %s" keyword
))))
421 (defun custom-add-dependencies (symbol value
)
422 "To the custom option SYMBOL, add dependencies specified by VALUE.
423 VALUE should be a list of symbols. For each symbol in that list,
424 this specifies that SYMBOL should be set after the specified symbol, if
425 both appear in constructs like `custom-set-variables'."
426 (unless (listp value
)
427 (error "Invalid custom dependency `%s'" value
))
428 (let* ((deps (get symbol
'custom-dependencies
))
431 (let ((dep (car value
)))
432 (unless (symbolp dep
)
433 (error "Invalid custom dependency `%s'" dep
))
434 (unless (memq dep new-deps
)
435 (setq new-deps
(cons dep new-deps
)))
436 (setq value
(cdr value
))))
437 (unless (eq deps new-deps
)
438 (put symbol
'custom-dependencies new-deps
))))
440 (defun custom-add-option (symbol option
)
441 "To the variable SYMBOL add OPTION.
443 If SYMBOL is a hook variable, OPTION should be a hook member.
444 For other types variables, the effect is undefined."
445 (let ((options (get symbol
'custom-options
)))
446 (unless (member option options
)
447 (put symbol
'custom-options
(cons option options
)))))
449 (defun custom-add-link (symbol widget
)
450 "To the custom option SYMBOL add the link WIDGET."
451 (let ((links (get symbol
'custom-links
)))
452 (unless (member widget links
)
453 (put symbol
'custom-links
(cons (purecopy widget
) links
)))))
455 (defun custom-add-version (symbol version
)
456 "To the custom option SYMBOL add the version VERSION."
457 (put symbol
'custom-version
(purecopy version
)))
459 (defun custom-add-load (symbol load
)
460 "To the custom option SYMBOL add the dependency LOAD.
461 LOAD should be either a library file name, or a feature name."
462 (let ((loads (get symbol
'custom-loads
)))
463 (unless (member load loads
)
464 (put symbol
'custom-loads
(cons (purecopy load
) loads
)))))
466 ;;; Loading files needed to customize a symbol.
467 ;;; This is in custom.el because menu-bar.el needs it for toggle cmds.
469 (defvar custom-load-recursion nil
470 "Hack to avoid recursive dependencies.")
472 (defun custom-load-symbol (symbol)
473 "Load all dependencies for SYMBOL."
474 (unless custom-load-recursion
475 (let ((custom-load-recursion t
)
476 (loads (get symbol
'custom-loads
))
479 (setq load
(car loads
)
481 (cond ((symbolp load
)
485 ;; Don't reload a file already loaded.
486 ((and (boundp 'preloaded-file-list
)
487 (member load preloaded-file-list
)))
488 ((assoc load load-history
))
489 ;; This was just (assoc (locate-library load) load-history)
490 ;; but has been optimized not to load locate-library
492 ((let (found (regexp (regexp-quote load
)))
493 (dolist (loaded load-history
)
494 (and (stringp (car loaded
))
495 (string-match regexp
(car loaded
))
496 (eq (locate-library load
) (car loaded
))
499 ;; Without this, we would load cus-edit recursively.
500 ;; We are still loading it when we call this,
501 ;; and it is not in load-history yet.
502 ((equal load
"cus-edit"))
510 (defvar custom-local-buffer nil
511 "Non-nil, in a Customization buffer, means customize a specific buffer.
512 If this variable is non-nil, it should be a buffer,
513 and it means customize the local bindings of that buffer.
514 This variable is a permanent local, and it normally has a local binding
515 in every Customization buffer.")
516 (put 'custom-local-buffer
'permanent-local t
)
518 (defun custom-set-variables (&rest args
)
519 "Initialize variables according to user preferences.
521 The arguments should be a list where each entry has the form:
523 (SYMBOL VALUE [NOW [REQUEST [COMMENT]]])
525 The unevaluated VALUE is stored as the saved value for SYMBOL.
526 If NOW is present and non-nil, VALUE is also evaluated and bound as
527 the default value for the SYMBOL.
528 REQUEST is a list of features we must require for SYMBOL.
529 COMMENT is a comment string about SYMBOL."
533 (let* ((sym1 (car a1
))
535 (1-then-2 (memq sym1
(get sym2
'custom-dependencies
)))
536 (2-then-1 (memq sym2
(get sym1
'custom-dependencies
))))
537 (cond ((and 1-then-2
2-then-1
)
538 (error "Circular custom dependency between `%s' and `%s'"
542 ;; Put symbols with :require last. The macro
543 ;; define-minor-mode generates a defcustom
544 ;; with a :require and a :set, where the
545 ;; setter function calls the mode function.
546 ;; Putting symbols with :require last ensures
547 ;; that the mode function will see other
548 ;; customized values rather than default
552 (let ((entry (car args
)))
554 (let* ((symbol (nth 0 entry
))
555 (value (nth 1 entry
))
557 (requests (nth 3 entry
))
558 (comment (nth 4 entry
))
561 (put symbol
'custom-requests requests
)
562 (mapc 'require requests
))
563 (setq set
(or (get symbol
'custom-set
) 'custom-set-default
))
564 (put symbol
'saved-value
(list value
))
565 (put symbol
'saved-variable-comment comment
)
566 ;; Allow for errors in the case where the setter has
567 ;; changed between versions, say, but let the user know.
570 ;; Rogue variable, set it now.
571 (put symbol
'force-value t
)
572 (funcall set symbol
(eval value
)))
573 ((default-boundp symbol
)
574 ;; Something already set this, overwrite it.
575 (funcall set symbol
(eval value
))))
577 (message "Error setting %s: %s" symbol data
)))
578 (setq args
(cdr args
))
579 (and (or now
(default-boundp symbol
))
580 (put symbol
'variable-comment comment
)))
581 ;; Old format, a plist of SYMBOL VALUE pairs.
582 (message "Warning: old format `custom-set-variables'")
585 (let ((symbol (nth 0 args
))
586 (value (nth 1 args
)))
587 (put symbol
'saved-value
(list value
)))
588 (setq args
(cdr (cdr args
)))))))
590 (defun custom-set-default (variable value
)
591 "Default :set function for a customizable variable.
592 Normally, this sets the default value of VARIABLE to VALUE,
593 but if `custom-local-buffer' is non-nil,
594 this sets the local binding in that buffer instead."
595 (if custom-local-buffer
596 (with-current-buffer custom-local-buffer
597 (set variable value
))
598 (set-default variable value
)))
600 (defun custom-quote (sexp)
601 "Quote SEXP iff it is not self quoting."
602 (if (or (memq sexp
'(t nil
))
605 (memq (car sexp
) '(lambda)))
609 ;;; (and (fboundp 'characterp)
610 ;;; (characterp sexp))
615 (defun customize-mark-to-save (symbol)
616 "Mark SYMBOL for later saving.
618 If the default value of SYMBOL is different from the standard value,
619 set the `saved-value' property to a list whose car evaluates to the
620 default value. Otherwise, set it til nil.
622 To actually save the value, call `custom-save-all'.
624 Return non-nil iff the `saved-value' property actually changed."
625 (let* ((get (or (get symbol
'custom-get
) 'default-value
))
626 (value (funcall get symbol
))
627 (saved (get symbol
'saved-value
))
628 (standard (get symbol
'standard-value
))
629 (comment (get symbol
'customized-variable-comment
)))
630 ;; Save default value iff different from standard value.
631 (if (or (null standard
)
632 (not (equal value
(condition-case nil
633 (eval (car standard
))
635 (put symbol
'saved-value
(list (custom-quote value
)))
636 (put symbol
'saved-value nil
))
637 ;; Clear customized information (set, but not saved).
638 (put symbol
'customized-value nil
)
639 ;; Save any comment that might have been set.
641 (put symbol
'saved-variable-comment comment
))
642 (not (equal saved
(get symbol
'saved-value
)))))
644 (defun customize-mark-as-set (symbol)
645 "Mark current value of SYMBOL as being set from customize.
647 If the default value of SYMBOL is different from the saved value if any,
648 or else if it is different from the standard value, set the
649 `customized-value' property to a list whose car evaluates to the
650 default value. Otherwise, set it til nil.
652 Return non-nil iff the `customized-value' property actually changed."
653 (let* ((get (or (get symbol
'custom-get
) 'default-value
))
654 (value (funcall get symbol
))
655 (customized (get symbol
'customized-value
))
656 (old (or (get symbol
'saved-value
) (get symbol
'standard-value
))))
657 ;; Mark default value as set iff different from old value.
659 (not (equal value
(condition-case nil
662 (put symbol
'customized-value
(list (custom-quote value
)))
663 (put symbol
'customized-value nil
))
665 (not (equal customized
(get symbol
'customized-value
)))))
669 ;; Process the defcustoms for variables loaded before this file.
670 (while custom-declare-variable-list
671 (apply 'custom-declare-variable
(car custom-declare-variable-list
))
672 (setq custom-declare-variable-list
(cdr custom-declare-variable-list
)))
676 ;;; custom.el ends here