*** empty log message ***
[emacs.git] / lisp / custom.el
blobcae8450c968c568b33748ae5cf22872014553bc6
1 ;;; custom.el --- tools for declaring and initializing options
2 ;;
3 ;; Copyright (C) 1996, 1997, 1999, 2001, 2002 Free Software Foundation, Inc.
4 ;;
5 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
6 ;; Maintainer: FSF
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)
14 ;; any later version.
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.
26 ;;; Commentary:
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'
34 ;;; Code:
36 (require 'widget)
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-current-group-alist nil
43 "Alist of (FILE . GROUP) indicating the current group to use for FILE.")
45 ;;; The `defcustom' Macro.
47 (defun custom-initialize-default (symbol value)
48 "Initialize SYMBOL with VALUE.
49 This will do nothing if symbol already has a default binding.
50 Otherwise, if symbol has a `saved-value' property, it will evaluate
51 the car of that and used as the default binding for symbol.
52 Otherwise, VALUE will be evaluated and used as the default binding for
53 symbol."
54 (unless (default-boundp symbol)
55 ;; Use the saved value if it exists, otherwise the standard setting.
56 (set-default symbol (if (get symbol 'saved-value)
57 (eval (car (get symbol 'saved-value)))
58 (eval value)))))
60 (defun custom-initialize-set (symbol value)
61 "Initialize SYMBOL based on VALUE.
62 If the symbol doesn't have a default binding already,
63 then set it using its `:set' function (or `set-default' if it has none).
64 The value is either the value in the symbol's `saved-value' property,
65 if any, or VALUE."
66 (unless (default-boundp symbol)
67 (funcall (or (get symbol 'custom-set) 'set-default)
68 symbol
69 (if (get symbol 'saved-value)
70 (eval (car (get symbol 'saved-value)))
71 (eval value)))))
73 (defun custom-initialize-reset (symbol value)
74 "Initialize SYMBOL based on VALUE.
75 Set the symbol, using its `:set' function (or `set-default' if it has none).
76 The value is either the symbol's current value
77 \(as obtained using the `:get' function), if any,
78 or the value in the symbol's `saved-value' property if any,
79 or (last of all) VALUE."
80 (funcall (or (get symbol 'custom-set) 'set-default)
81 symbol
82 (cond ((default-boundp symbol)
83 (funcall (or (get symbol 'custom-get) 'default-value)
84 symbol))
85 ((get symbol 'saved-value)
86 (eval (car (get symbol 'saved-value))))
88 (eval value)))))
90 (defun custom-initialize-changed (symbol value)
91 "Initialize SYMBOL with VALUE.
92 Like `custom-initialize-reset', but only use the `:set' function if
93 not using the standard setting.
94 For the standard setting, use `set-default'."
95 (cond ((default-boundp symbol)
96 (funcall (or (get symbol 'custom-set) 'set-default)
97 symbol
98 (funcall (or (get symbol 'custom-get) 'default-value)
99 symbol)))
100 ((get symbol 'saved-value)
101 (funcall (or (get symbol 'custom-set) 'set-default)
102 symbol
103 (eval (car (get symbol 'saved-value)))))
105 (set-default symbol (eval value)))))
107 (defun custom-declare-variable (symbol default doc &rest args)
108 "Like `defcustom', but SYMBOL and DEFAULT are evaluated as normal arguments.
109 DEFAULT should be an expression to evaluate to compute the default value,
110 not the default value itself."
111 ;; Remember the standard setting.
112 (put symbol 'standard-value (list default))
113 ;; Maybe this option was rogue in an earlier version. It no longer is.
114 (when (get symbol 'force-value)
115 (put symbol 'force-value nil))
116 (when doc
117 (put symbol 'variable-documentation doc))
118 (let ((initialize 'custom-initialize-reset)
119 (requests nil))
120 (unless (memq :group args)
121 (custom-add-to-group (custom-current-group) symbol 'custom-variable))
122 (while args
123 (let ((arg (car args)))
124 (setq args (cdr args))
125 (unless (symbolp arg)
126 (error "Junk in args %S" args))
127 (let ((keyword arg)
128 (value (car args)))
129 (unless args
130 (error "Keyword %s is missing an argument" keyword))
131 (setq args (cdr args))
132 (cond ((eq keyword :initialize)
133 (setq initialize value))
134 ((eq keyword :set)
135 (put symbol 'custom-set value))
136 ((eq keyword :get)
137 (put symbol 'custom-get value))
138 ((eq keyword :require)
139 (setq requests (cons value requests)))
140 ((eq keyword :type)
141 (put symbol 'custom-type (purecopy value)))
142 ((eq keyword :options)
143 (if (get symbol 'custom-options)
144 ;; Slow safe code to avoid duplicates.
145 (mapc (lambda (option)
146 (custom-add-option symbol option))
147 value)
148 ;; Fast code for the common case.
149 (put symbol 'custom-options (copy-sequence value))))
151 (custom-handle-keyword symbol keyword value
152 'custom-variable))))))
153 (put symbol 'custom-requests requests)
154 ;; Do the actual initialization.
155 (funcall initialize symbol default))
156 (setq current-load-list (cons symbol current-load-list))
157 (run-hooks 'custom-define-hook)
158 symbol)
160 (defmacro defcustom (symbol value doc &rest args)
161 "Declare SYMBOL as a customizable variable that defaults to VALUE.
162 DOC is the variable documentation.
164 Neither SYMBOL nor VALUE needs to be quoted.
165 If SYMBOL is not already bound, initialize it to VALUE.
166 The remaining arguments should have the form
168 [KEYWORD VALUE]...
170 The following keywords are meaningful:
172 :type VALUE should be a widget type for editing the symbols value.
173 :options VALUE should be a list of valid members of the widget type.
174 :group VALUE should be a customization group.
175 Add SYMBOL to that group.
176 :link LINK-DATA
177 Include an external link after the documentation string for this
178 item. This is a sentence containing an active field which
179 references some other documentation.
181 There are three alternatives you can use for LINK-DATA:
183 (custom-manual INFO-NODE)
184 Link to an Info node; INFO-NODE is a string which specifies
185 the node name, as in \"(emacs)Top\". The link appears as
186 `[manual]' in the customization buffer.
188 (info-link INFO-NODE)
189 Like `custom-manual' except that the link appears in the
190 customization buffer with the Info node name.
192 (url-link URL)
193 Link to a web page; URL is a string which specifies the URL.
194 The link appears in the customization buffer as URL.
196 You can specify the text to use in the customization buffer by
197 adding `:tag NAME' after the first element of the LINK-DATA; for
198 example, (info-link :tag \"foo\" \"(emacs)Top\") makes a link to the
199 Emacs manual which appears in the buffer as `foo'.
201 An item can have more than one external link; however, most items
202 have none at all.
203 :initialize
204 VALUE should be a function used to initialize the
205 variable. It takes two arguments, the symbol and value
206 given in the `defcustom' call. The default is
207 `custom-initialize-reset'.
208 :set VALUE should be a function to set the value of the symbol.
209 It takes two arguments, the symbol to set and the value to
210 give it. The default choice of function is `custom-set-default'.
211 :get VALUE should be a function to extract the value of symbol.
212 The function takes one argument, a symbol, and should return
213 the current value for that symbol. The default choice of function
214 is `custom-default-value'.
215 :require
216 VALUE should be a feature symbol. If you save a value
217 for this option, then when your `.emacs' file loads the value,
218 it does (require VALUE) first.
219 :version
220 VALUE should be a string specifying that the variable was
221 first introduced, or its default value was changed, in Emacs
222 version VERSION.
223 :tag LABEL
224 Use LABEL, a string, instead of the item's name, to label the item
225 in customization menus and buffers.
226 :load FILE
227 Load file FILE (a string) before displaying this customization
228 item. Loading is done with `load-library', and only if the file is
229 not already loaded.
230 :set-after VARIABLE
231 Specifies that SYMBOL should be set after VARIABLE when
232 both have been customized.
234 Read the section about customization in the Emacs Lisp manual for more
235 information."
236 ;; It is better not to use backquote in this file,
237 ;; because that makes a bootstrapping problem
238 ;; if you need to recompile all the Lisp files using interpreted code.
239 (nconc (list 'custom-declare-variable
240 (list 'quote symbol)
241 (list 'quote value)
242 doc)
243 args))
245 ;;; The `defface' Macro.
247 (defmacro defface (face spec doc &rest args)
248 "Declare FACE as a customizable face that defaults to SPEC.
249 FACE does not need to be quoted.
251 Third argument DOC is the face documentation.
253 If FACE has been set with `custom-set-face', set the face attributes
254 as specified by that function, otherwise set the face attributes
255 according to SPEC.
257 The remaining arguments should have the form
259 [KEYWORD VALUE]...
261 The following KEYWORDs are defined:
263 :group VALUE should be a customization group.
264 Add FACE to that group.
266 SPEC should be an alist of the form ((DISPLAY ATTS)...).
268 The first element of SPEC where the DISPLAY matches the frame
269 is the one that takes effect in that frame. The ATTRs in this
270 element take effect; the other elements are ignored, on that frame.
272 ATTS is a list of face attributes followed by their values:
273 (ATTR VALUE ATTR VALUE...)
275 The possible attributes are `:family', `:width', `:height', `:weight',
276 `:slant', `:underline', `:overline', `:strike-through', `:box',
277 `:foreground', `:background', `:stipple', `:inverse-video', and `:inherit'.
279 DISPLAY can either be the symbol t, which will match all frames, or an
280 alist of the form \((REQ ITEM...)...). For the DISPLAY to match a
281 FRAME, the REQ property of the frame must match one of the ITEM. The
282 following REQ are defined:
284 `type' (the value of `window-system')
285 Under X, in addition to the values `window-system' can take,
286 `motif', `lucid' and `x-toolkit' are allowed, and match when
287 the Motif toolkit, Lucid toolkit, or any X toolkit is in use.
289 `class' (the frame's color support)
290 Should be one of `color', `grayscale', or `mono'.
292 `background' (what color is used for the background text)
293 Should be one of `light' or `dark'.
295 Read the section about customization in the Emacs Lisp manual for more
296 information."
297 ;; It is better not to use backquote in this file,
298 ;; because that makes a bootstrapping problem
299 ;; if you need to recompile all the Lisp files using interpreted code.
300 (nconc (list 'custom-declare-face (list 'quote face) spec doc) args))
302 ;;; The `defgroup' Macro.
304 (defun custom-current-group ()
305 (cdr (assoc load-file-name custom-current-group-alist)))
307 (defun custom-declare-group (symbol members doc &rest args)
308 "Like `defgroup', but SYMBOL is evaluated as a normal argument."
309 (while members
310 (apply 'custom-add-to-group symbol (car members))
311 (setq members (cdr members)))
312 (put symbol 'custom-group (nconc members (get symbol 'custom-group)))
313 (when doc
314 ;; This text doesn't get into DOC.
315 (put symbol 'group-documentation (purecopy doc)))
316 (while args
317 (let ((arg (car args)))
318 (setq args (cdr args))
319 (unless (symbolp arg)
320 (error "Junk in args %S" args))
321 (let ((keyword arg)
322 (value (car args)))
323 (unless args
324 (error "Keyword %s is missing an argument" keyword))
325 (setq args (cdr args))
326 (cond ((eq keyword :prefix)
327 (put symbol 'custom-prefix value))
329 (custom-handle-keyword symbol keyword value
330 'custom-group))))))
331 ;; Record the group on the `current' list.
332 (let ((elt (assoc load-file-name custom-current-group-alist)))
333 (if elt (setcdr elt symbol)
334 (push (cons load-file-name symbol) custom-current-group-alist)))
335 (run-hooks 'custom-define-hook)
336 symbol)
338 (defmacro defgroup (symbol members doc &rest args)
339 "Declare SYMBOL as a customization group containing MEMBERS.
340 SYMBOL does not need to be quoted.
342 Third arg DOC is the group documentation.
344 MEMBERS should be an alist of the form ((NAME WIDGET)...) where
345 NAME is a symbol and WIDGET is a widget for editing that symbol.
346 Useful widgets are `custom-variable' for editing variables,
347 `custom-face' for edit faces, and `custom-group' for editing groups.
349 The remaining arguments should have the form
351 [KEYWORD VALUE]...
353 The following KEYWORDs are defined:
355 :group VALUE should be a customization group.
356 Add SYMBOL to that group.
358 :version VALUE should be a string specifying that the group was introduced
359 in Emacs version VERSION.
361 Read the section about customization in the Emacs Lisp manual for more
362 information."
363 ;; It is better not to use backquote in this file,
364 ;; because that makes a bootstrapping problem
365 ;; if you need to recompile all the Lisp files using interpreted code.
366 (nconc (list 'custom-declare-group (list 'quote symbol) members doc) args))
368 (defun custom-add-to-group (group option widget)
369 "To existing GROUP add a new OPTION of type WIDGET.
370 If there already is an entry for OPTION and WIDGET, nothing is done."
371 (let ((members (get group 'custom-group))
372 (entry (list option widget)))
373 (unless (member entry members)
374 (put group 'custom-group (nconc members (list entry))))))
376 ;;; Properties.
378 (defun custom-handle-all-keywords (symbol args type)
379 "For customization option SYMBOL, handle keyword arguments ARGS.
380 Third argument TYPE is the custom option type."
381 (unless (memq :group args)
382 (custom-add-to-group (custom-current-group) symbol 'custom-face))
383 (while args
384 (let ((arg (car args)))
385 (setq args (cdr args))
386 (unless (symbolp arg)
387 (error "Junk in args %S" args))
388 (let ((keyword arg)
389 (value (car args)))
390 (unless args
391 (error "Keyword %s is missing an argument" keyword))
392 (setq args (cdr args))
393 (custom-handle-keyword symbol keyword value type)))))
395 (defun custom-handle-keyword (symbol keyword value type)
396 "For customization option SYMBOL, handle KEYWORD with VALUE.
397 Fourth argument TYPE is the custom option type."
398 (if purify-flag
399 (setq value (purecopy value)))
400 (cond ((eq keyword :group)
401 (custom-add-to-group value symbol type))
402 ((eq keyword :version)
403 (custom-add-version symbol value))
404 ((eq keyword :link)
405 (custom-add-link symbol value))
406 ((eq keyword :load)
407 (custom-add-load symbol value))
408 ((eq keyword :tag)
409 (put symbol 'custom-tag value))
410 ((eq keyword :set-after)
411 (custom-add-dependencies symbol value))
413 (error "Unknown keyword %s" keyword))))
415 (defun custom-add-dependencies (symbol value)
416 "To the custom option SYMBOL, add dependencies specified by VALUE.
417 VALUE should be a list of symbols. For each symbol in that list,
418 this specifies that SYMBOL should be set after the specified symbol, if
419 both appear in constructs like `custom-set-variables'."
420 (unless (listp value)
421 (error "Invalid custom dependency `%s'" value))
422 (let* ((deps (get symbol 'custom-dependencies))
423 (new-deps deps))
424 (while value
425 (let ((dep (car value)))
426 (unless (symbolp dep)
427 (error "Invalid custom dependency `%s'" dep))
428 (unless (memq dep new-deps)
429 (setq new-deps (cons dep new-deps)))
430 (setq value (cdr value))))
431 (unless (eq deps new-deps)
432 (put symbol 'custom-dependencies new-deps))))
434 (defun custom-add-option (symbol option)
435 "To the variable SYMBOL add OPTION.
437 If SYMBOL is a hook variable, OPTION should be a hook member.
438 For other types variables, the effect is undefined."
439 (let ((options (get symbol 'custom-options)))
440 (unless (member option options)
441 (put symbol 'custom-options (cons option options)))))
443 (defun custom-add-link (symbol widget)
444 "To the custom option SYMBOL add the link WIDGET."
445 (let ((links (get symbol 'custom-links)))
446 (unless (member widget links)
447 (put symbol 'custom-links (cons (purecopy widget) links)))))
449 (defun custom-add-version (symbol version)
450 "To the custom option SYMBOL add the version VERSION."
451 (put symbol 'custom-version (purecopy version)))
453 (defun custom-add-load (symbol load)
454 "To the custom option SYMBOL add the dependency LOAD.
455 LOAD should be either a library file name, or a feature name."
456 (let ((loads (get symbol 'custom-loads)))
457 (unless (member load loads)
458 (put symbol 'custom-loads (cons (purecopy load) loads)))))
460 ;;; Initializing.
462 (defvar custom-local-buffer nil
463 "Non-nil, in a Customization buffer, means customize a specific buffer.
464 If this variable is non-nil, it should be a buffer,
465 and it means customize the local bindings of that buffer.
466 This variable is a permanent local, and it normally has a local binding
467 in every Customization buffer.")
468 (put 'custom-local-buffer 'permanent-local t)
470 (defun custom-set-variables (&rest args)
471 "Initialize variables according to user preferences.
473 The arguments should be a list where each entry has the form:
475 (SYMBOL VALUE [NOW [REQUEST [COMMENT]]])
477 The unevaluated VALUE is stored as the saved value for SYMBOL.
478 If NOW is present and non-nil, VALUE is also evaluated and bound as
479 the default value for the SYMBOL.
480 REQUEST is a list of features we must require for SYMBOL.
481 COMMENT is a comment string about SYMBOL."
482 (setq args
483 (sort args
484 (lambda (a1 a2)
485 (let* ((sym1 (car a1))
486 (sym2 (car a2))
487 (1-then-2 (memq sym1 (get sym2 'custom-dependencies)))
488 (2-then-1 (memq sym2 (get sym1 'custom-dependencies))))
489 (cond ((and 1-then-2 2-then-1)
490 (error "Circular custom dependency between `%s' and `%s'"
491 sym1 sym2))
492 (1-then-2 t)
493 (2-then-1 nil)
494 ;; Put symbols with :require last. The macro
495 ;; define-minor-mode generates a defcustom
496 ;; with a :require and a :set, where the
497 ;; setter function calls the mode function.
498 ;; Putting symbols with :require last ensures
499 ;; that the mode function will see other
500 ;; customized values rather than default
501 ;; values.
502 (t (nth 3 a2)))))))
503 (while args
504 (let ((entry (car args)))
505 (if (listp entry)
506 (let* ((symbol (nth 0 entry))
507 (value (nth 1 entry))
508 (now (nth 2 entry))
509 (requests (nth 3 entry))
510 (comment (nth 4 entry))
511 set)
512 (when requests
513 (put symbol 'custom-requests requests)
514 (mapc 'require requests))
515 (setq set (or (get symbol 'custom-set) 'custom-set-default))
516 (put symbol 'saved-value (list value))
517 (put symbol 'saved-variable-comment comment)
518 ;; Allow for errors in the case where the setter has
519 ;; changed between versions, say, but let the user know.
520 (condition-case data
521 (cond (now
522 ;; Rogue variable, set it now.
523 (put symbol 'force-value t)
524 (funcall set symbol (eval value)))
525 ((default-boundp symbol)
526 ;; Something already set this, overwrite it.
527 (funcall set symbol (eval value))))
528 (error
529 (message "Error setting %s: %s" symbol data)))
530 (setq args (cdr args))
531 (and (or now (default-boundp symbol))
532 (put symbol 'variable-comment comment)))
533 ;; Old format, a plist of SYMBOL VALUE pairs.
534 (message "Warning: old format `custom-set-variables'")
535 (ding)
536 (sit-for 2)
537 (let ((symbol (nth 0 args))
538 (value (nth 1 args)))
539 (put symbol 'saved-value (list value)))
540 (setq args (cdr (cdr args)))))))
542 (defun custom-set-default (variable value)
543 "Default :set function for a customizable variable.
544 Normally, this sets the default value of VARIABLE to VALUE,
545 but if `custom-local-buffer' is non-nil,
546 this sets the local binding in that buffer instead."
547 (if custom-local-buffer
548 (with-current-buffer custom-local-buffer
549 (set variable value))
550 (set-default variable value)))
552 (defun custom-quote (sexp)
553 "Quote SEXP iff it is not self quoting."
554 (if (or (memq sexp '(t nil))
555 (keywordp sexp)
556 (and (listp sexp)
557 (memq (car sexp) '(lambda)))
558 (stringp sexp)
559 (numberp sexp)
560 (vectorp sexp)
561 ;;; (and (fboundp 'characterp)
562 ;;; (characterp sexp))
564 sexp
565 (list 'quote sexp)))
567 (defun customize-mark-to-save (symbol)
568 "Mark SYMBOL for later saving.
570 If the default value of SYMBOL is different from the standard value,
571 set the `saved-value' property to a list whose car evaluates to the
572 default value. Otherwise, set it til nil.
574 To actually save the value, call `custom-save-all'.
576 Return non-nil iff the `saved-value' property actually changed."
577 (let* ((get (or (get symbol 'custom-get) 'default-value))
578 (value (funcall get symbol))
579 (saved (get symbol 'saved-value))
580 (standard (get symbol 'standard-value))
581 (comment (get symbol 'customized-variable-comment)))
582 ;; Save default value iff different from standard value.
583 (if (or (null standard)
584 (not (equal value (condition-case nil
585 (eval (car standard))
586 (error nil)))))
587 (put symbol 'saved-value (list (custom-quote value)))
588 (put symbol 'saved-value nil))
589 ;; Clear customized information (set, but not saved).
590 (put symbol 'customized-value nil)
591 ;; Save any comment that might have been set.
592 (when comment
593 (put symbol 'saved-variable-comment comment))
594 (not (equal saved (get symbol 'saved-value)))))
596 (defun customize-mark-as-set (symbol)
597 "Mark current value of SYMBOL as being set from customize.
599 If the default value of SYMBOL is different from the saved value if any,
600 or else if it is different from the standard value, set the
601 `customized-value' property to a list whose car evaluates to the
602 default value. Otherwise, set it til nil.
604 Return non-nil iff the `customized-value' property actually changed."
605 (let* ((get (or (get symbol 'custom-get) 'default-value))
606 (value (funcall get symbol))
607 (customized (get symbol 'customized-value))
608 (old (or (get symbol 'saved-value) (get symbol 'standard-value))))
609 ;; Mark default value as set iff different from old value.
610 (if (or (null old)
611 (not (equal value (condition-case nil
612 (eval (car old))
613 (error nil)))))
614 (put symbol 'customized-value (list (custom-quote value)))
615 (put symbol 'customized-value nil))
616 ;; Changed?
617 (not (equal customized (get symbol 'customized-value)))))
619 ;;; The End.
621 ;; Process the defcustoms for variables loaded before this file.
622 (while custom-declare-variable-list
623 (apply 'custom-declare-variable (car custom-declare-variable-list))
624 (setq custom-declare-variable-list (cdr custom-declare-variable-list)))
626 (provide 'custom)
628 ;;; custom.el ends here