Remove CVS merge cookie left in.
[emacs.git] / lisp / custom.el
blobef0fce7ecb49985b06df7137182665acb53e003a
1 ;;; custom.el -- Tools for declaring and initializing options.
2 ;;
3 ;; Copyright (C) 1996, 1997, 1999 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 ;;; The `defcustom' Macro.
44 (defun custom-initialize-default (symbol value)
45 "Initialize SYMBOL with VALUE.
46 This will do nothing if symbol already has a default binding.
47 Otherwise, if symbol has a `saved-value' property, it will evaluate
48 the car of that and used as the default binding for symbol.
49 Otherwise, VALUE will be evaluated and used as the default binding for
50 symbol."
51 (unless (default-boundp symbol)
52 ;; Use the saved value if it exists, otherwise the standard setting.
53 (set-default symbol (if (get symbol 'saved-value)
54 (eval (car (get symbol 'saved-value)))
55 (eval value)))))
57 (defun custom-initialize-set (symbol value)
58 "Initialize SYMBOL based on VALUE.
59 If the symbol doesn't have a default binding already,
60 then set it using its `:set' function (or `set-default' if it has none).
61 The value is either the value in the symbol's `saved-value' property,
62 if any, or VALUE."
63 (unless (default-boundp symbol)
64 (funcall (or (get symbol 'custom-set) 'set-default)
65 symbol
66 (if (get symbol 'saved-value)
67 (eval (car (get symbol 'saved-value)))
68 (eval value)))))
70 (defun custom-initialize-reset (symbol value)
71 "Initialize SYMBOL based on VALUE.
72 Set the symbol, using its `:set' function (or `set-default' if it has none).
73 The value is either the symbol's current value
74 \(as obtained using the `:get' function), if any,
75 or the value in the symbol's `saved-value' property if any,
76 or (last of all) VALUE."
77 (funcall (or (get symbol 'custom-set) 'set-default)
78 symbol
79 (cond ((default-boundp symbol)
80 (funcall (or (get symbol 'custom-get) 'default-value)
81 symbol))
82 ((get symbol 'saved-value)
83 (eval (car (get symbol 'saved-value))))
85 (eval value)))))
87 (defun custom-initialize-changed (symbol value)
88 "Initialize SYMBOL with VALUE.
89 Like `custom-initialize-reset', but only use the `:set' function if the
90 not using the standard setting.
91 For the standard setting, use the `set-default'."
92 (cond ((default-boundp symbol)
93 (funcall (or (get symbol 'custom-set) 'set-default)
94 symbol
95 (funcall (or (get symbol 'custom-get) 'default-value)
96 symbol)))
97 ((get symbol 'saved-value)
98 (funcall (or (get symbol 'custom-set) 'set-default)
99 symbol
100 (eval (car (get symbol 'saved-value)))))
102 (set-default symbol (eval value)))))
104 (defun custom-declare-variable (symbol default doc &rest args)
105 "Like `defcustom', but SYMBOL and DEFAULT are evaluated as normal arguments.
106 DEFAULT should be an expression to evaluate to compute the default value,
107 not the default value itself."
108 ;; Remember the standard setting.
109 (put symbol 'standard-value (list default))
110 ;; Maybe this option was rogue in an earlier version. It no longer is.
111 (when (get symbol 'force-value)
112 (put symbol 'force-value nil))
113 (when doc
114 (put symbol 'variable-documentation doc))
115 (let ((initialize 'custom-initialize-reset)
116 (requests nil))
117 (while args
118 (let ((arg (car args)))
119 (setq args (cdr args))
120 (unless (symbolp arg)
121 (error "Junk in args %S" args))
122 (let ((keyword arg)
123 (value (car args)))
124 (unless args
125 (error "Keyword %s is missing an argument" keyword))
126 (setq args (cdr args))
127 (cond ((eq keyword :initialize)
128 (setq initialize value))
129 ((eq keyword :set)
130 (put symbol 'custom-set value))
131 ((eq keyword :get)
132 (put symbol 'custom-get value))
133 ((eq keyword :require)
134 (setq requests (cons value requests)))
135 ((eq keyword :type)
136 (put symbol 'custom-type (purecopy value)))
137 ((eq keyword :options)
138 (if (get symbol 'custom-options)
139 ;; Slow safe code to avoid duplicates.
140 (mapc (lambda (option)
141 (custom-add-option symbol option))
142 value)
143 ;; Fast code for the common case.
144 (put symbol 'custom-options (copy-sequence value))))
146 (custom-handle-keyword symbol keyword value
147 'custom-variable))))))
148 (put symbol 'custom-requests requests)
149 ;; Do the actual initialization.
150 (funcall initialize symbol default))
151 (setq current-load-list (cons symbol current-load-list))
152 (run-hooks 'custom-define-hook)
153 symbol)
155 (defmacro defcustom (symbol value doc &rest args)
156 "Declare SYMBOL as a customizable variable that defaults to VALUE.
157 DOC is the variable documentation.
159 Neither SYMBOL nor VALUE needs to be quoted.
160 If SYMBOL is not already bound, initialize it to VALUE.
161 The remaining arguments should have the form
163 [KEYWORD VALUE]...
165 The following keywords are meaningful:
167 :type VALUE should be a widget type for editing the symbols value.
168 The default is `sexp'.
169 :options VALUE should be a list of valid members of the widget type.
170 :group VALUE should be a customization group.
171 Add SYMBOL to that group.
172 :initialize
173 VALUE should be a function used to initialize the
174 variable. It takes two arguments, the symbol and value
175 given in the `defcustom' call. The default is
176 `custom-initialize-default'
177 :set VALUE should be a function to set the value of the symbol.
178 It takes two arguments, the symbol to set and the value to
179 give it. The default choice of function is `custom-set-default'.
180 :get VALUE should be a function to extract the value of symbol.
181 The function takes one argument, a symbol, and should return
182 the current value for that symbol. The default choice of function
183 is `custom-default-value'.
184 :require
185 VALUE should be a feature symbol. If you save a value
186 for this option, then when your `.emacs' file loads the value,
187 it does (require VALUE) first.
188 :version
189 VALUE should be a string specifying that the variable was
190 first introduced, or its default value was changed, in Emacs
191 version VERSION.
193 Read the section about customization in the Emacs Lisp manual for more
194 information."
195 ;; It is better not to use backquote in this file,
196 ;; because that makes a bootstrapping problem
197 ;; if you need to recompile all the Lisp files using interpreted code.
198 (nconc (list 'custom-declare-variable
199 (list 'quote symbol)
200 (list 'quote value)
201 doc)
202 args))
204 ;;; The `defface' Macro.
206 (defmacro defface (face spec doc &rest args)
207 "Declare FACE as a customizable face that defaults to SPEC.
208 FACE does not need to be quoted.
210 Third argument DOC is the face documentation.
212 If FACE has been set with `custom-set-face', set the face attributes
213 as specified by that function, otherwise set the face attributes
214 according to SPEC.
216 The remaining arguments should have the form
218 [KEYWORD VALUE]...
220 The following KEYWORDs are defined:
222 :group VALUE should be a customization group.
223 Add FACE to that group.
225 SPEC should be an alist of the form ((DISPLAY ATTS)...).
227 The first element of SPEC where the DISPLAY matches the frame
228 is the one that takes effect in that frame. The ATTRs in this
229 element take effect; the other elements are ignored, on that frame.
231 ATTS is a list of face attributes followed by their values:
232 (ATTR VALUE ATTR VALUE...)
234 The possible attributes are `:family', `:width', `:height', `:weight',
235 `:slant', `:underline', `:overline', `:strike-through', `:box',
236 `:foreground', `:background', `:stipple', and `:inverse-video'.
238 DISPLAY can either be the symbol t, which will match all frames, or an
239 alist of the form \((REQ ITEM...)...). For the DISPLAY to match a
240 FRAME, the REQ property of the frame must match one of the ITEM. The
241 following REQ are defined:
243 `type' (the value of `window-system')
244 Under X, in addition to the values `window-system' can take,
245 `motif', `lucid' and `x-toolkit' are allowed, and match when
246 the Motif toolkit, Lucid toolkit, or any X toolkit is in use.
248 `class' (the frame's color support)
249 Should be one of `color', `grayscale', or `mono'.
251 `background' (what color is used for the background text)
252 Should be one of `light' or `dark'.
254 Read the section about customization in the Emacs Lisp manual for more
255 information."
256 ;; It is better not to use backquote in this file,
257 ;; because that makes a bootstrapping problem
258 ;; if you need to recompile all the Lisp files using interpreted code.
259 (nconc (list 'custom-declare-face (list 'quote face) spec doc) args))
261 ;;; The `defgroup' Macro.
263 (defun custom-declare-group (symbol members doc &rest args)
264 "Like `defgroup', but SYMBOL is evaluated as a normal argument."
265 (while members
266 (apply 'custom-add-to-group symbol (car members))
267 (setq members (cdr members)))
268 (put symbol 'custom-group (nconc members (get symbol 'custom-group)))
269 (when doc
270 ;; This text doesn't get into DOC.
271 (put symbol 'group-documentation (purecopy doc)))
272 (while args
273 (let ((arg (car args)))
274 (setq args (cdr args))
275 (unless (symbolp arg)
276 (error "Junk in args %S" args))
277 (let ((keyword arg)
278 (value (car args)))
279 (unless args
280 (error "Keyword %s is missing an argument" keyword))
281 (setq args (cdr args))
282 (cond ((eq keyword :prefix)
283 (put symbol 'custom-prefix value))
285 (custom-handle-keyword symbol keyword value
286 'custom-group))))))
287 (run-hooks 'custom-define-hook)
288 symbol)
290 (defmacro defgroup (symbol members doc &rest args)
291 "Declare SYMBOL as a customization group containing MEMBERS.
292 SYMBOL does not need to be quoted.
294 Third arg DOC is the group documentation.
296 MEMBERS should be an alist of the form ((NAME WIDGET)...) where
297 NAME is a symbol and WIDGET is a widget for editing that symbol.
298 Useful widgets are `custom-variable' for editing variables,
299 `custom-face' for edit faces, and `custom-group' for editing groups.
301 The remaining arguments should have the form
303 [KEYWORD VALUE]...
305 The following KEYWORDs are defined:
307 :group VALUE should be a customization group.
308 Add SYMBOL to that group.
310 :version VALUE should be a string specifying that the group was introduced
311 in Emacs version VERSION.
313 Read the section about customization in the Emacs Lisp manual for more
314 information."
315 ;; It is better not to use backquote in this file,
316 ;; because that makes a bootstrapping problem
317 ;; if you need to recompile all the Lisp files using interpreted code.
318 (nconc (list 'custom-declare-group (list 'quote symbol) members doc) args))
320 (defun custom-add-to-group (group option widget)
321 "To existing GROUP add a new OPTION of type WIDGET.
322 If there already is an entry for that option, overwrite it."
323 (let* ((members (get group 'custom-group))
324 (old (assq option members)))
325 (if old
326 (setcar (cdr old) widget)
327 (put group 'custom-group (nconc members (list (list option widget)))))))
329 ;;; Properties.
331 (defun custom-handle-all-keywords (symbol args type)
332 "For customization option SYMBOL, handle keyword arguments ARGS.
333 Third argument TYPE is the custom option type."
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 (custom-handle-keyword symbol keyword value type)))))
346 (defun custom-handle-keyword (symbol keyword value type)
347 "For customization option SYMBOL, handle KEYWORD with VALUE.
348 Fourth argument TYPE is the custom option type."
349 (if purify-flag
350 (setq value (purecopy value)))
351 (cond ((eq keyword :group)
352 (custom-add-to-group value symbol type))
353 ((eq keyword :version)
354 (custom-add-version symbol value))
355 ((eq keyword :link)
356 (custom-add-link symbol value))
357 ((eq keyword :load)
358 (custom-add-load symbol value))
359 ((eq keyword :tag)
360 (put symbol 'custom-tag value))
361 ((eq keyword :set-after)
362 (custom-add-dependencies symbol value))
364 (error "Unknown keyword %s" keyword))))
366 (defun custom-add-dependencies (symbol value)
367 "To the custom option SYMBOL, add dependencies specified by VALUE.
368 VALUE should be a list of symbols. For each symbol in that list,
369 this specifies that SYMBOL should be set after the specified symbol, if
370 both appear in constructs like `custom-set-variables'."
371 (unless (listp value)
372 (error "Invalid custom dependency `%s'" value))
373 (let* ((deps (get symbol 'custom-dependencies))
374 (new-deps deps))
375 (while value
376 (let ((dep (car value)))
377 (unless (symbolp dep)
378 (error "Invalid custom dependency `%s'" dep))
379 (unless (memq dep new-deps)
380 (setq new-deps (cons dep new-deps)))
381 (setq value (cdr value))))
382 (unless (eq deps new-deps)
383 (put symbol 'custom-dependencies new-deps))))
385 (defun custom-add-option (symbol option)
386 "To the variable SYMBOL add OPTION.
388 If SYMBOL is a hook variable, OPTION should be a hook member.
389 For other types variables, the effect is undefined."
390 (let ((options (get symbol 'custom-options)))
391 (unless (member option options)
392 (put symbol 'custom-options (cons option options)))))
394 (defun custom-add-link (symbol widget)
395 "To the custom option SYMBOL add the link WIDGET."
396 (let ((links (get symbol 'custom-links)))
397 (unless (member widget links)
398 (put symbol 'custom-links (cons (purecopy widget) links)))))
400 (defun custom-add-version (symbol version)
401 "To the custom option SYMBOL add the version VERSION."
402 (put symbol 'custom-version (purecopy version)))
404 (defun custom-add-load (symbol load)
405 "To the custom option SYMBOL add the dependency LOAD.
406 LOAD should be either a library file name, or a feature name."
407 (let ((loads (get symbol 'custom-loads)))
408 (unless (member load loads)
409 (put symbol 'custom-loads (cons (purecopy load) loads)))))
411 ;;; Initializing.
413 (defvar custom-local-buffer nil
414 "Non-nil, in a Customization buffer, means customize a specific buffer.
415 If this variable is non-nil, it should be a buffer,
416 and it means customize the local bindings of that buffer.
417 This variable is a permanent local, and it normally has a local binding
418 in every Customization buffer.")
419 (put 'custom-local-buffer 'permanent-local t)
421 (defun custom-set-variables (&rest args)
422 "Initialize variables according to user preferences.
424 The arguments should be a list where each entry has the form:
426 (SYMBOL VALUE [NOW [REQUEST [COMMENT]]])
428 The unevaluated VALUE is stored as the saved value for SYMBOL.
429 If NOW is present and non-nil, VALUE is also evaluated and bound as
430 the default value for the SYMBOL.
431 REQUEST is a list of features we must require for SYMBOL.
432 COMMENT is a comment string about SYMBOL."
433 (setq args
434 (sort args
435 (lambda (a1 a2)
436 (let* ((sym1 (car a1))
437 (sym2 (car a2))
438 (1-then-2 (memq sym1 (get sym2 'custom-dependencies)))
439 (2-then-1 (memq sym2 (get sym1 'custom-dependencies))))
440 (cond ((and 1-then-2 2-then-1)
441 (error "Circular custom dependency between `%s' and `%s'"
442 sym1 sym2))
443 (2-then-1 nil)
444 (t t))))))
445 (while args
446 (let ((entry (car args)))
447 (if (listp entry)
448 (let* ((symbol (nth 0 entry))
449 (value (nth 1 entry))
450 (now (nth 2 entry))
451 (requests (nth 3 entry))
452 (comment (nth 4 entry))
453 set)
454 (when requests
455 (put symbol 'custom-requests requests)
456 (mapc 'require requests))
457 (setq set (or (get symbol 'custom-set) 'custom-set-default))
458 (put symbol 'saved-value (list value))
459 (put symbol 'saved-variable-comment comment)
460 ;; Allow for errors in the case where the setter has
461 ;; changed between versions, say, but let the user know.
462 (condition-case data
463 (cond (now
464 ;; Rogue variable, set it now.
465 (put symbol 'force-value t)
466 (funcall set symbol (eval value)))
467 ((default-boundp symbol)
468 ;; Something already set this, overwrite it.
469 (funcall set symbol (eval value))))
470 (error
471 (message "Error setting %s: %s" symbol data)))
472 (setq args (cdr args))
473 (and (or now (default-boundp symbol))
474 (put symbol 'variable-comment comment)))
475 ;; Old format, a plist of SYMBOL VALUE pairs.
476 (message "Warning: old format `custom-set-variables'")
477 (ding)
478 (sit-for 2)
479 (let ((symbol (nth 0 args))
480 (value (nth 1 args)))
481 (put symbol 'saved-value (list value)))
482 (setq args (cdr (cdr args)))))))
484 (defun custom-set-default (variable value)
485 "Default :set function for a customizable variable.
486 Normally, this sets the default value of VARIABLE to VALUE,
487 but if `custom-local-buffer' is non-nil,
488 this sets the local binding in that buffer instead."
489 (if custom-local-buffer
490 (with-current-buffer custom-local-buffer
491 (set variable value))
492 (set-default variable value)))
494 ;;; The End.
496 ;; Process the defcustoms for variables loaded before this file.
497 (while custom-declare-variable-list
498 (apply 'custom-declare-variable (car custom-declare-variable-list))
499 (setq custom-declare-variable-list (cdr custom-declare-variable-list)))
501 (provide 'custom)
503 ;;; custom.el ends here