Update the Commentary with installation instructions.
[bpt/emacs.git] / lisp / custom.el
CommitLineData
55535639
PJ
1;;; custom.el --- tools for declaring and initializing options
2;;
3;; Copyright (C) 1996, 1997, 1999, 2001 Free Software Foundation, Inc.
4;;
5;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
6;; Maintainer: FSF
7;; Keywords: help, faces
8
9;; This file is part of GNU Emacs.
10
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.
15
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.
20
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.
25
26;;; Commentary:
27;;
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.
31
32;; The code implementing face declarations is in `cus-face.el'
33
34;;; Code:
35
36(require 'widget)
37
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.")
41
42;;; The `defcustom' Macro.
43
44(defun custom-initialize-default (symbol value)
45 "Initialize SYMBOL with VALUE.
46This will do nothing if symbol already has a default binding.
47Otherwise, if symbol has a `saved-value' property, it will evaluate
48the car of that and used as the default binding for symbol.
49Otherwise, VALUE will be evaluated and used as the default binding for
50symbol."
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)))))
56
57(defun custom-initialize-set (symbol value)
58 "Initialize SYMBOL based on VALUE.
59If the symbol doesn't have a default binding already,
60then set it using its `:set' function (or `set-default' if it has none).
61The value is either the value in the symbol's `saved-value' property,
62if 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)))))
69
70(defun custom-initialize-reset (symbol value)
71 "Initialize SYMBOL based on VALUE.
72Set the symbol, using its `:set' function (or `set-default' if it has none).
73The value is either the symbol's current value
74 \(as obtained using the `:get' function), if any,
75or the value in the symbol's `saved-value' property if any,
76or (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))))
84 (t
85 (eval value)))))
86
87(defun custom-initialize-changed (symbol value)
88 "Initialize SYMBOL with VALUE.
89Like `custom-initialize-reset', but only use the `:set' function if
90not using the standard setting.
91For the standard setting, use `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)))))
101 (t
102 (set-default symbol (eval value)))))
103
104(defun custom-declare-variable (symbol default doc &rest args)
105 "Like `defcustom', but SYMBOL and DEFAULT are evaluated as normal arguments.
106DEFAULT should be an expression to evaluate to compute the default value,
107not 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))))
145 (t
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)
154
155(defmacro defcustom (symbol value doc &rest args)
156 "Declare SYMBOL as a customizable variable that defaults to VALUE.
157DOC is the variable documentation.
158
159Neither SYMBOL nor VALUE needs to be quoted.
160If SYMBOL is not already bound, initialize it to VALUE.
161The remaining arguments should have the form
162
163 [KEYWORD VALUE]...
164
165The following keywords are meaningful:
166
167:type VALUE should be a widget type for editing the symbols value.
168:options VALUE should be a list of valid members of the widget type.
169:group VALUE should be a customization group.
170 Add SYMBOL to that group.
171:initialize
172 VALUE should be a function used to initialize the
173 variable. It takes two arguments, the symbol and value
174 given in the `defcustom' call. The default is
6c7f60a2 175 `custom-initialize-reset.'
55535639
PJ
176:set VALUE should be a function to set the value of the symbol.
177 It takes two arguments, the symbol to set and the value to
178 give it. The default choice of function is `custom-set-default'.
179:get VALUE should be a function to extract the value of symbol.
180 The function takes one argument, a symbol, and should return
181 the current value for that symbol. The default choice of function
182 is `custom-default-value'.
183:require
184 VALUE should be a feature symbol. If you save a value
185 for this option, then when your `.emacs' file loads the value,
186 it does (require VALUE) first.
187:version
188 VALUE should be a string specifying that the variable was
189 first introduced, or its default value was changed, in Emacs
190 version VERSION.
a2b38e51
GM
191:set-after VARIABLE
192 Specifies that SYMBOL should be set after VARIABLE when
193 both have been customized.
55535639
PJ
194
195Read the section about customization in the Emacs Lisp manual for more
196information."
197 ;; It is better not to use backquote in this file,
198 ;; because that makes a bootstrapping problem
199 ;; if you need to recompile all the Lisp files using interpreted code.
200 (nconc (list 'custom-declare-variable
201 (list 'quote symbol)
202 (list 'quote value)
203 doc)
204 args))
205
206;;; The `defface' Macro.
207
208(defmacro defface (face spec doc &rest args)
209 "Declare FACE as a customizable face that defaults to SPEC.
210FACE does not need to be quoted.
211
212Third argument DOC is the face documentation.
213
214If FACE has been set with `custom-set-face', set the face attributes
215as specified by that function, otherwise set the face attributes
216according to SPEC.
217
218The remaining arguments should have the form
219
220 [KEYWORD VALUE]...
221
222The following KEYWORDs are defined:
223
224:group VALUE should be a customization group.
225 Add FACE to that group.
226
227SPEC should be an alist of the form ((DISPLAY ATTS)...).
228
229The first element of SPEC where the DISPLAY matches the frame
230is the one that takes effect in that frame. The ATTRs in this
231element take effect; the other elements are ignored, on that frame.
232
233ATTS is a list of face attributes followed by their values:
234 (ATTR VALUE ATTR VALUE...)
235
236The possible attributes are `:family', `:width', `:height', `:weight',
237`:slant', `:underline', `:overline', `:strike-through', `:box',
238`:foreground', `:background', `:stipple', and `:inverse-video'.
239
240DISPLAY can either be the symbol t, which will match all frames, or an
241alist of the form \((REQ ITEM...)...). For the DISPLAY to match a
242FRAME, the REQ property of the frame must match one of the ITEM. The
243following REQ are defined:
244
245`type' (the value of `window-system')
246 Under X, in addition to the values `window-system' can take,
247 `motif', `lucid' and `x-toolkit' are allowed, and match when
248 the Motif toolkit, Lucid toolkit, or any X toolkit is in use.
249
250`class' (the frame's color support)
251 Should be one of `color', `grayscale', or `mono'.
252
253`background' (what color is used for the background text)
254 Should be one of `light' or `dark'.
255
256Read the section about customization in the Emacs Lisp manual for more
257information."
258 ;; It is better not to use backquote in this file,
259 ;; because that makes a bootstrapping problem
260 ;; if you need to recompile all the Lisp files using interpreted code.
261 (nconc (list 'custom-declare-face (list 'quote face) spec doc) args))
262
263;;; The `defgroup' Macro.
264
265(defun custom-declare-group (symbol members doc &rest args)
266 "Like `defgroup', but SYMBOL is evaluated as a normal argument."
267 (while members
268 (apply 'custom-add-to-group symbol (car members))
269 (setq members (cdr members)))
270 (put symbol 'custom-group (nconc members (get symbol 'custom-group)))
271 (when doc
272 ;; This text doesn't get into DOC.
273 (put symbol 'group-documentation (purecopy doc)))
274 (while args
275 (let ((arg (car args)))
276 (setq args (cdr args))
277 (unless (symbolp arg)
278 (error "Junk in args %S" args))
279 (let ((keyword arg)
280 (value (car args)))
281 (unless args
282 (error "Keyword %s is missing an argument" keyword))
283 (setq args (cdr args))
284 (cond ((eq keyword :prefix)
285 (put symbol 'custom-prefix value))
286 (t
287 (custom-handle-keyword symbol keyword value
288 'custom-group))))))
289 (run-hooks 'custom-define-hook)
290 symbol)
291
292(defmacro defgroup (symbol members doc &rest args)
293 "Declare SYMBOL as a customization group containing MEMBERS.
294SYMBOL does not need to be quoted.
295
296Third arg DOC is the group documentation.
297
298MEMBERS should be an alist of the form ((NAME WIDGET)...) where
299NAME is a symbol and WIDGET is a widget for editing that symbol.
300Useful widgets are `custom-variable' for editing variables,
301`custom-face' for edit faces, and `custom-group' for editing groups.
302
303The remaining arguments should have the form
304
305 [KEYWORD VALUE]...
306
307The following KEYWORDs are defined:
308
309:group VALUE should be a customization group.
310 Add SYMBOL to that group.
311
312:version VALUE should be a string specifying that the group was introduced
313 in Emacs version VERSION.
314
315Read the section about customization in the Emacs Lisp manual for more
316information."
317 ;; It is better not to use backquote in this file,
318 ;; because that makes a bootstrapping problem
319 ;; if you need to recompile all the Lisp files using interpreted code.
320 (nconc (list 'custom-declare-group (list 'quote symbol) members doc) args))
321
322(defun custom-add-to-group (group option widget)
323 "To existing GROUP add a new OPTION of type WIDGET.
324If there already is an entry for OPTION and WIDGET, nothing is done."
325 (let ((members (get group 'custom-group))
326 (entry (list option widget)))
327 (unless (member entry members)
328 (put group 'custom-group (nconc members (list entry))))))
329
330;;; Properties.
331
332(defun custom-handle-all-keywords (symbol args type)
333 "For customization option SYMBOL, handle keyword arguments ARGS.
334Third argument TYPE is the custom option type."
335 (while args
336 (let ((arg (car args)))
337 (setq args (cdr args))
338 (unless (symbolp arg)
339 (error "Junk in args %S" args))
340 (let ((keyword arg)
341 (value (car args)))
342 (unless args
343 (error "Keyword %s is missing an argument" keyword))
344 (setq args (cdr args))
345 (custom-handle-keyword symbol keyword value type)))))
346
347(defun custom-handle-keyword (symbol keyword value type)
348 "For customization option SYMBOL, handle KEYWORD with VALUE.
349Fourth argument TYPE is the custom option type."
350 (if purify-flag
351 (setq value (purecopy value)))
352 (cond ((eq keyword :group)
353 (custom-add-to-group value symbol type))
354 ((eq keyword :version)
355 (custom-add-version symbol value))
356 ((eq keyword :link)
357 (custom-add-link symbol value))
358 ((eq keyword :load)
359 (custom-add-load symbol value))
360 ((eq keyword :tag)
361 (put symbol 'custom-tag value))
362 ((eq keyword :set-after)
363 (custom-add-dependencies symbol value))
364 (t
365 (error "Unknown keyword %s" keyword))))
366
367(defun custom-add-dependencies (symbol value)
368 "To the custom option SYMBOL, add dependencies specified by VALUE.
369VALUE should be a list of symbols. For each symbol in that list,
370this specifies that SYMBOL should be set after the specified symbol, if
371both appear in constructs like `custom-set-variables'."
372 (unless (listp value)
373 (error "Invalid custom dependency `%s'" value))
374 (let* ((deps (get symbol 'custom-dependencies))
375 (new-deps deps))
376 (while value
377 (let ((dep (car value)))
378 (unless (symbolp dep)
379 (error "Invalid custom dependency `%s'" dep))
380 (unless (memq dep new-deps)
381 (setq new-deps (cons dep new-deps)))
382 (setq value (cdr value))))
383 (unless (eq deps new-deps)
384 (put symbol 'custom-dependencies new-deps))))
385
386(defun custom-add-option (symbol option)
387 "To the variable SYMBOL add OPTION.
388
389If SYMBOL is a hook variable, OPTION should be a hook member.
390For other types variables, the effect is undefined."
391 (let ((options (get symbol 'custom-options)))
392 (unless (member option options)
393 (put symbol 'custom-options (cons option options)))))
394
395(defun custom-add-link (symbol widget)
396 "To the custom option SYMBOL add the link WIDGET."
397 (let ((links (get symbol 'custom-links)))
398 (unless (member widget links)
399 (put symbol 'custom-links (cons (purecopy widget) links)))))
400
401(defun custom-add-version (symbol version)
402 "To the custom option SYMBOL add the version VERSION."
403 (put symbol 'custom-version (purecopy version)))
404
405(defun custom-add-load (symbol load)
406 "To the custom option SYMBOL add the dependency LOAD.
407LOAD should be either a library file name, or a feature name."
408 (let ((loads (get symbol 'custom-loads)))
409 (unless (member load loads)
410 (put symbol 'custom-loads (cons (purecopy load) loads)))))
411
412;;; Initializing.
413
414(defvar custom-local-buffer nil
415 "Non-nil, in a Customization buffer, means customize a specific buffer.
416If this variable is non-nil, it should be a buffer,
417and it means customize the local bindings of that buffer.
418This variable is a permanent local, and it normally has a local binding
419in every Customization buffer.")
420(put 'custom-local-buffer 'permanent-local t)
421
422(defun custom-set-variables (&rest args)
423 "Initialize variables according to user preferences.
424
425The arguments should be a list where each entry has the form:
426
427 (SYMBOL VALUE [NOW [REQUEST [COMMENT]]])
428
429The unevaluated VALUE is stored as the saved value for SYMBOL.
430If NOW is present and non-nil, VALUE is also evaluated and bound as
431the default value for the SYMBOL.
432REQUEST is a list of features we must require for SYMBOL.
433COMMENT is a comment string about SYMBOL."
434 (setq args
435 (sort args
436 (lambda (a1 a2)
437 (let* ((sym1 (car a1))
438 (sym2 (car a2))
439 (1-then-2 (memq sym1 (get sym2 'custom-dependencies)))
440 (2-then-1 (memq sym2 (get sym1 'custom-dependencies))))
441 (cond ((and 1-then-2 2-then-1)
442 (error "Circular custom dependency between `%s' and `%s'"
443 sym1 sym2))
444 (1-then-2 t)
cb3f945f
GM
445 (2-then-1 nil)
446 ;; Put symbols with :require last. The macro
447 ;; define-minor-mode generates a defcustom
448 ;; with a :require and a :set, where the
449 ;; setter function calls the mode function.
450 ;; Putting symbols with :require last ensures
451 ;; that the mode function will see other
452 ;; customized values rather than default
453 ;; values.
be48584d 454 (t (nth 3 a2)))))))
55535639
PJ
455 (while args
456 (let ((entry (car args)))
457 (if (listp entry)
458 (let* ((symbol (nth 0 entry))
459 (value (nth 1 entry))
460 (now (nth 2 entry))
461 (requests (nth 3 entry))
462 (comment (nth 4 entry))
463 set)
464 (when requests
465 (put symbol 'custom-requests requests)
466 (mapc 'require requests))
467 (setq set (or (get symbol 'custom-set) 'custom-set-default))
468 (put symbol 'saved-value (list value))
469 (put symbol 'saved-variable-comment comment)
470 ;; Allow for errors in the case where the setter has
471 ;; changed between versions, say, but let the user know.
472 (condition-case data
473 (cond (now
474 ;; Rogue variable, set it now.
475 (put symbol 'force-value t)
476 (funcall set symbol (eval value)))
477 ((default-boundp symbol)
478 ;; Something already set this, overwrite it.
479 (funcall set symbol (eval value))))
480 (error
481 (message "Error setting %s: %s" symbol data)))
482 (setq args (cdr args))
483 (and (or now (default-boundp symbol))
484 (put symbol 'variable-comment comment)))
485 ;; Old format, a plist of SYMBOL VALUE pairs.
486 (message "Warning: old format `custom-set-variables'")
487 (ding)
488 (sit-for 2)
489 (let ((symbol (nth 0 args))
490 (value (nth 1 args)))
491 (put symbol 'saved-value (list value)))
492 (setq args (cdr (cdr args)))))))
493
494(defun custom-set-default (variable value)
495 "Default :set function for a customizable variable.
496Normally, this sets the default value of VARIABLE to VALUE,
497but if `custom-local-buffer' is non-nil,
498this sets the local binding in that buffer instead."
499 (if custom-local-buffer
500 (with-current-buffer custom-local-buffer
501 (set variable value))
502 (set-default variable value)))
503
504;;; The End.
505
506;; Process the defcustoms for variables loaded before this file.
507(while custom-declare-variable-list
508 (apply 'custom-declare-variable (car custom-declare-variable-list))
509 (setq custom-declare-variable-list (cdr custom-declare-variable-list)))
510
511(provide 'custom)
512
513;;; custom.el ends here