.
[bpt/emacs.git] / lisp / custom.el
CommitLineData
d543e20b 1;;; custom.el -- Tools for declaring and initializing options.
41487370 2;;
70647e33 3;; Copyright (C) 1996, 1997, 1999 Free Software Foundation, Inc.
41487370 4;;
d543e20b 5;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
32651a48 6;; Maintainer: FSF
d543e20b 7;; Keywords: help, faces
d543e20b 8
c2383d2d
RS
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
d543e20b 26;;; Commentary:
41487370 27;;
d543e20b
PA
28;; This file only contain the code needed to declare and initialize
29;; user options. The code to customize options is autoloaded from
70647e33 30;; `cus-edit.el' and is documented in the Emacs Lisp Reference manual.
41487370 31
d543e20b 32;; The code implementing face declarations is in `cus-face.el'
41487370
LMI
33
34;;; Code:
35
d543e20b
PA
36(require 'widget)
37
bd042c03
PA
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
d543e20b
PA
42;;; The `defcustom' Macro.
43
6d528fc5
PA
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."
bd042c03 51 (unless (default-boundp symbol)
25ac13b5 52 ;; Use the saved value if it exists, otherwise the standard setting.
d543e20b
PA
53 (set-default symbol (if (get symbol 'saved-value)
54 (eval (car (get symbol 'saved-value)))
6d528fc5
PA
55 (eval value)))))
56
57(defun custom-initialize-set (symbol value)
71bd46d2
RS
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."
6d528fc5
PA
63 (unless (default-boundp symbol)
64 (funcall (or (get symbol 'custom-set) 'set-default)
70647e33 65 symbol
6d528fc5
PA
66 (if (get symbol 'saved-value)
67 (eval (car (get symbol 'saved-value)))
68 (eval value)))))
69
70(defun custom-initialize-reset (symbol value)
71bd46d2
RS
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."
6d528fc5 77 (funcall (or (get symbol 'custom-set) 'set-default)
70647e33 78 symbol
6d528fc5
PA
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.
70647e33 89Like `custom-initialize-reset', but only use the `:set' function if the
71bd46d2
RS
90not using the standard setting.
91For the standard setting, use the `set-default'."
6d528fc5
PA
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
255d8b97
RS
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."
25ac13b5 108 ;; Remember the standard setting.
255d8b97 109 (put symbol 'standard-value (list default))
bd042c03
PA
110 ;; Maybe this option was rogue in an earlier version. It no longer is.
111 (when (get symbol 'force-value)
bd042c03 112 (put symbol 'force-value nil))
d543e20b
PA
113 (when doc
114 (put symbol 'variable-documentation doc))
71bd46d2 115 (let ((initialize 'custom-initialize-reset)
6d528fc5 116 (requests nil))
70647e33 117 (while args
6d528fc5 118 (let ((arg (car args)))
d543e20b 119 (setq args (cdr args))
6d528fc5
PA
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)
67efacf8 134 (setq requests (cons value requests)))
6d528fc5 135 ((eq keyword :type)
3920868a 136 (put symbol 'custom-type (purecopy value)))
6d528fc5
PA
137 ((eq keyword :options)
138 (if (get symbol 'custom-options)
139 ;; Slow safe code to avoid duplicates.
140 (mapcar (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.
255d8b97 150 (funcall initialize symbol default))
7c573adb 151 (setq current-load-list (cons symbol current-load-list))
d543e20b
PA
152 (run-hooks 'custom-define-hook)
153 symbol)
41487370 154
d543e20b
PA
155(defmacro defcustom (symbol value doc &rest args)
156 "Declare SYMBOL as a customizable variable that defaults to VALUE.
157DOC is the variable documentation.
41487370 158
d543e20b
PA
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
41487370 162
70647e33 163 [KEYWORD VALUE]...
41487370 164
e612f4bd 165The following keywords are meaningful:
41487370 166
6d528fc5
PA
167:type VALUE should be a widget type for editing the symbols value.
168 The default is `sexp'.
d543e20b 169:options VALUE should be a list of valid members of the widget type.
70647e33 170:group VALUE should be a customization group.
d543e20b 171 Add SYMBOL to that group.
e612f4bd
RS
172:initialize
173 VALUE should be a function used to initialize the
6d528fc5
PA
174 variable. It takes two arguments, the symbol and value
175 given in the `defcustom' call. The default is
70647e33
DL
176 `custom-initialize-default'
177:set VALUE should be a function to set the value of the symbol.
6d528fc5 178 It takes two arguments, the symbol to set and the value to
7ddd6031 179 give it. The default choice of function is `custom-set-default'.
6d528fc5
PA
180:get VALUE should be a function to extract the value of symbol.
181 The function takes one argument, a symbol, and should return
e612f4bd 182 the current value for that symbol. The default choice of function
70647e33 183 is `custom-default-value'.
e612f4bd
RS
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.
41487370 188
305ec106 189Read the section about customization in the Emacs Lisp manual for more
d543e20b 190information."
d7c0adec
RS
191 ;; It is better not to use backquote in this file,
192 ;; because that makes a bootstrapping problem
193 ;; if you need to recompile all the Lisp files using interpreted code.
194 (nconc (list 'custom-declare-variable
195 (list 'quote symbol)
196 (list 'quote value)
197 doc)
198 args))
41487370 199
d543e20b 200;;; The `defface' Macro.
41487370 201
d543e20b
PA
202(defmacro defface (face spec doc &rest args)
203 "Declare FACE as a customizable face that defaults to SPEC.
204FACE does not need to be quoted.
41487370 205
d543e20b 206Third argument DOC is the face documentation.
41487370 207
d543e20b
PA
208If FACE has been set with `custom-set-face', set the face attributes
209as specified by that function, otherwise set the face attributes
210according to SPEC.
41487370 211
d543e20b 212The remaining arguments should have the form
41487370 213
d543e20b 214 [KEYWORD VALUE]...
41487370 215
aafc86d2 216The following KEYWORDs are defined:
41487370 217
d543e20b
PA
218:group VALUE should be a customization group.
219 Add FACE to that group.
41487370 220
d543e20b 221SPEC should be an alist of the form ((DISPLAY ATTS)...).
41487370 222
aafc86d2
RS
223The first element of SPEC where the DISPLAY matches the frame
224is the one that takes effect in that frame. The ATTRs in this
225element take effect; the other elements are ignored, on that frame.
41487370 226
aafc86d2
RS
227ATTS is a list of face attributes followed by their values:
228 (ATTR VALUE ATTR VALUE...)
9e5962d0
GM
229
230The possible attributes are `:family', `:width', `:height', `:weight',
231`:slant', `:underline', `:overline', `:strike-through', `:box',
232`:foreground', `:background', `:stipple', and `:inverse-video'.
41487370 233
aafc86d2
RS
234DISPLAY can either be the symbol t, which will match all frames, or an
235alist of the form \((REQ ITEM...)...). For the DISPLAY to match a
236FRAME, the REQ property of the frame must match one of the ITEM. The
237following REQ are defined:
41487370 238
d543e20b 239`type' (the value of `window-system')
48f68b55
GM
240 Under X, in addition to the values `window-system' can take,
241 `motif', `lucid' and `x-toolkit' are allowed, and match when
242 the Motif toolkit, Lucid toolkit, or any X toolkit is in use.
41487370 243
d543e20b
PA
244`class' (the frame's color support)
245 Should be one of `color', `grayscale', or `mono'.
41487370 246
d543e20b
PA
247`background' (what color is used for the background text)
248 Should be one of `light' or `dark'.
41487370 249
305ec106 250Read the section about customization in the Emacs Lisp manual for more
d543e20b 251information."
d7c0adec
RS
252 ;; It is better not to use backquote in this file,
253 ;; because that makes a bootstrapping problem
254 ;; if you need to recompile all the Lisp files using interpreted code.
255 (nconc (list 'custom-declare-face (list 'quote face) spec doc) args))
41487370 256
d543e20b 257;;; The `defgroup' Macro.
41487370 258
d543e20b
PA
259(defun custom-declare-group (symbol members doc &rest args)
260 "Like `defgroup', but SYMBOL is evaluated as a normal argument."
70647e33 261 (while members
6d528fc5
PA
262 (apply 'custom-add-to-group symbol (car members))
263 (setq members (cdr members)))
d543e20b
PA
264 (put symbol 'custom-group (nconc members (get symbol 'custom-group)))
265 (when doc
32651a48
DL
266 ;; This text doesn't get into DOC.
267 (put symbol 'group-documentation (purecopy doc)))
70647e33 268 (while args
d543e20b
PA
269 (let ((arg (car args)))
270 (setq args (cdr args))
271 (unless (symbolp arg)
272 (error "Junk in args %S" args))
273 (let ((keyword arg)
274 (value (car args)))
275 (unless args
276 (error "Keyword %s is missing an argument" keyword))
277 (setq args (cdr args))
278 (cond ((eq keyword :prefix)
279 (put symbol 'custom-prefix value))
280 (t
281 (custom-handle-keyword symbol keyword value
282 'custom-group))))))
283 (run-hooks 'custom-define-hook)
284 symbol)
285
286(defmacro defgroup (symbol members doc &rest args)
287 "Declare SYMBOL as a customization group containing MEMBERS.
288SYMBOL does not need to be quoted.
289
290Third arg DOC is the group documentation.
291
292MEMBERS should be an alist of the form ((NAME WIDGET)...) where
5e5e5b54
AS
293NAME is a symbol and WIDGET is a widget for editing that symbol.
294Useful widgets are `custom-variable' for editing variables,
d543e20b
PA
295`custom-face' for edit faces, and `custom-group' for editing groups.
296
297The remaining arguments should have the form
298
70647e33 299 [KEYWORD VALUE]...
d543e20b
PA
300
301The following KEYWORD's are defined:
302
303:group VALUE should be a customization group.
304 Add SYMBOL to that group.
305
305ec106 306Read the section about customization in the Emacs Lisp manual for more
d543e20b 307information."
d7c0adec
RS
308 ;; It is better not to use backquote in this file,
309 ;; because that makes a bootstrapping problem
310 ;; if you need to recompile all the Lisp files using interpreted code.
311 (nconc (list 'custom-declare-group (list 'quote symbol) members doc) args))
d543e20b
PA
312
313(defun custom-add-to-group (group option widget)
314 "To existing GROUP add a new OPTION of type WIDGET.
315If there already is an entry for that option, overwrite it."
316 (let* ((members (get group 'custom-group))
317 (old (assq option members)))
318 (if old
319 (setcar (cdr old) widget)
320 (put group 'custom-group (nconc members (list (list option widget)))))))
321
322;;; Properties.
323
324(defun custom-handle-all-keywords (symbol args type)
325 "For customization option SYMBOL, handle keyword arguments ARGS.
326Third argument TYPE is the custom option type."
70647e33 327 (while args
d543e20b
PA
328 (let ((arg (car args)))
329 (setq args (cdr args))
330 (unless (symbolp arg)
331 (error "Junk in args %S" args))
332 (let ((keyword arg)
333 (value (car args)))
334 (unless args
335 (error "Keyword %s is missing an argument" keyword))
336 (setq args (cdr args))
70647e33 337 (custom-handle-keyword symbol keyword value type)))))
d543e20b
PA
338
339(defun custom-handle-keyword (symbol keyword value type)
340 "For customization option SYMBOL, handle KEYWORD with VALUE.
341Fourth argument TYPE is the custom option type."
32651a48
DL
342 (if purify-flag
343 (setq value (purecopy value)))
d543e20b
PA
344 (cond ((eq keyword :group)
345 (custom-add-to-group value symbol type))
c743cc52
KH
346 ((eq keyword :version)
347 (custom-add-version symbol value))
d543e20b
PA
348 ((eq keyword :link)
349 (custom-add-link symbol value))
350 ((eq keyword :load)
351 (custom-add-load symbol value))
352 ((eq keyword :tag)
353 (put symbol 'custom-tag value))
13b5da04
GM
354 ((eq keyword :set-after)
355 (custom-add-dependencies symbol value))
41487370 356 (t
2a6b2356 357 (error "Unknown keyword %s" keyword))))
d543e20b 358
13b5da04
GM
359(defun custom-add-dependencies (symbol value)
360 "To the custom option SYMBOL, add dependencies specified by VALUE.
361VALUE should be a list of symbols. For each symbol in that list,
362this specifies that SYMBOL should be set after the specified symbol, if
363both appear in constructs like `custom-set-variables'."
364 (unless (listp value)
365 (error "Invalid custom dependency `%s'" value))
366 (let* ((deps (get symbol 'custom-dependencies))
367 (new-deps deps))
368 (while value
369 (let ((dep (car value)))
370 (unless (symbolp dep)
371 (error "Invalid custom dependency `%s'" dep))
372 (unless (memq dep new-deps)
373 (setq new-deps (cons dep new-deps)))
374 (setq value (cdr value))))
375 (unless (eq deps new-deps)
376 (put symbol 'custom-dependencies new-deps))))
377
d543e20b
PA
378(defun custom-add-option (symbol option)
379 "To the variable SYMBOL add OPTION.
380
381If SYMBOL is a hook variable, OPTION should be a hook member.
382For other types variables, the effect is undefined."
383 (let ((options (get symbol 'custom-options)))
384 (unless (member option options)
385 (put symbol 'custom-options (cons option options)))))
386
387(defun custom-add-link (symbol widget)
388 "To the custom option SYMBOL add the link WIDGET."
389 (let ((links (get symbol 'custom-links)))
390 (unless (member widget links)
32651a48 391 (put symbol 'custom-links (cons (purecopy widget) links)))))
d543e20b 392
c743cc52
KH
393(defun custom-add-version (symbol version)
394 "To the custom option SYMBOL add the version VERSION."
32651a48 395 (put symbol 'custom-version (purecopy version)))
c743cc52 396
d543e20b
PA
397(defun custom-add-load (symbol load)
398 "To the custom option SYMBOL add the dependency LOAD.
399LOAD should be either a library file name, or a feature name."
400 (let ((loads (get symbol 'custom-loads)))
401 (unless (member load loads)
32651a48 402 (put symbol 'custom-loads (cons (purecopy load) loads)))))
d543e20b
PA
403
404;;; Initializing.
405
7ddd6031
RS
406(defvar custom-local-buffer nil
407 "Non-nil, in a Customization buffer, means customize a specific buffer.
408If this variable is non-nil, it should be a buffer,
409and it means customize the local bindings of that buffer.
410This variable is a permanent local, and it normally has a local binding
411in every Customization buffer.")
412(put 'custom-local-buffer 'permanent-local t)
413
d543e20b 414(defun custom-set-variables (&rest args)
70647e33 415 "Initialize variables according to user preferences.
d543e20b
PA
416
417The arguments should be a list where each entry has the form:
418
70647e33 419 (SYMBOL VALUE [NOW [REQUEST [COMMENT]]])
d543e20b
PA
420
421The unevaluated VALUE is stored as the saved value for SYMBOL.
422If NOW is present and non-nil, VALUE is also evaluated and bound as
70647e33
DL
423the default value for the SYMBOL.
424REQUEST is a list of features we must require for SYMBOL.
425COMMENT is a comment string about SYMBOL."
13b5da04
GM
426 (setq args
427 (sort args
428 (lambda (a1 a2)
429 (let* ((sym1 (car a1))
430 (sym2 (car a2))
431 (1-then-2 (memq sym1 (get sym2 'custom-dependencies)))
432 (2-then-1 (memq sym2 (get sym1 'custom-dependencies))))
433 (cond ((and 1-then-2 2-then-1)
434 (error "Circular custom dependency between `%s' and `%s'"
435 sym1 sym2))
436 (2-then-1 nil)
437 (t t))))))
70647e33 438 (while args
d543e20b
PA
439 (let ((entry (car args)))
440 (if (listp entry)
6d528fc5
PA
441 (let* ((symbol (nth 0 entry))
442 (value (nth 1 entry))
443 (now (nth 2 entry))
444 (requests (nth 3 entry))
70647e33 445 (comment (nth 4 entry))
98743206
KH
446 set)
447 (when requests
448 (put symbol 'custom-requests requests)
449 (mapcar 'require requests))
450 (setq set (or (get symbol 'custom-set) 'custom-set-default))
d543e20b 451 (put symbol 'saved-value (list value))
70647e33 452 (put symbol 'saved-variable-comment comment)
733e8270
DL
453 ;; Allow for errors in the case where the setter has
454 ;; changed between versions, say.
455 (condition-case nil
456 (cond (now
457 ;; Rogue variable, set it now.
458 (put symbol 'force-value t)
459 (funcall set symbol (eval value)))
460 ((default-boundp symbol)
461 ;; Something already set this, overwrite it.
462 (funcall set symbol (eval value))))
463 (error nil))
70647e33
DL
464 (setq args (cdr args))
465 (and (or now (default-boundp symbol))
466 (put symbol 'variable-comment comment)))
d543e20b 467 ;; Old format, a plist of SYMBOL VALUE pairs.
bd042c03
PA
468 (message "Warning: old format `custom-set-variables'")
469 (ding)
470 (sit-for 2)
d543e20b
PA
471 (let ((symbol (nth 0 args))
472 (value (nth 1 args)))
473 (put symbol 'saved-value (list value)))
474 (setq args (cdr (cdr args)))))))
475
7ddd6031
RS
476(defun custom-set-default (variable value)
477 "Default :set function for a customizable variable.
478Normally, this sets the default value of VARIABLE to VALUE,
479but if `custom-local-buffer' is non-nil,
480this sets the local binding in that buffer instead."
481 (if custom-local-buffer
482 (with-current-buffer custom-local-buffer
483 (set variable value))
484 (set-default variable value)))
485
d543e20b 486;;; The End.
41487370 487
a7d6bdac
RS
488;; Process the defcustoms for variables loaded before this file.
489(while custom-declare-variable-list
490 (apply 'custom-declare-variable (car custom-declare-variable-list))
491 (setq custom-declare-variable-list (cdr custom-declare-variable-list)))
492
41487370
LMI
493(provide 'custom)
494
70647e33 495;;; custom.el ends here