]> code.delx.au - gnu-emacs/blob - lisp/custom.el
(custom-handle-keyword): Add :set-after.
[gnu-emacs] / lisp / custom.el
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
8 ;; X-URL: http://www.dina.kvl.dk/~abraham/custom/ (probably obsolete)
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Commentary:
28 ;;
29 ;; This file only contain the code needed to declare and initialize
30 ;; user options. The code to customize options is autoloaded from
31 ;; `cus-edit.el' and is documented in the Emacs Lisp Reference manual.
32
33 ;; The code implementing face declarations is in `cus-face.el'
34
35 ;;; Code:
36
37 (require 'widget)
38
39 (defvar custom-define-hook nil
40 ;; Customize information for this option is in `cus-edit.el'.
41 "Hook called after defining each customize option.")
42
43 ;;; The `defcustom' Macro.
44
45 (defun custom-initialize-default (symbol value)
46 "Initialize SYMBOL with VALUE.
47 This will do nothing if symbol already has a default binding.
48 Otherwise, if symbol has a `saved-value' property, it will evaluate
49 the car of that and used as the default binding for symbol.
50 Otherwise, VALUE will be evaluated and used as the default binding for
51 symbol."
52 (unless (default-boundp symbol)
53 ;; Use the saved value if it exists, otherwise the standard setting.
54 (set-default symbol (if (get symbol 'saved-value)
55 (eval (car (get symbol 'saved-value)))
56 (eval value)))))
57
58 (defun custom-initialize-set (symbol value)
59 "Initialize SYMBOL based on VALUE.
60 If the symbol doesn't have a default binding already,
61 then set it using its `:set' function (or `set-default' if it has none).
62 The value is either the value in the symbol's `saved-value' property,
63 if any, or VALUE."
64 (unless (default-boundp symbol)
65 (funcall (or (get symbol 'custom-set) 'set-default)
66 symbol
67 (if (get symbol 'saved-value)
68 (eval (car (get symbol 'saved-value)))
69 (eval value)))))
70
71 (defun custom-initialize-reset (symbol value)
72 "Initialize SYMBOL based on VALUE.
73 Set the symbol, using its `:set' function (or `set-default' if it has none).
74 The value is either the symbol's current value
75 \(as obtained using the `:get' function), if any,
76 or the value in the symbol's `saved-value' property if any,
77 or (last of all) VALUE."
78 (funcall (or (get symbol 'custom-set) 'set-default)
79 symbol
80 (cond ((default-boundp symbol)
81 (funcall (or (get symbol 'custom-get) 'default-value)
82 symbol))
83 ((get symbol 'saved-value)
84 (eval (car (get symbol 'saved-value))))
85 (t
86 (eval value)))))
87
88 (defun custom-initialize-changed (symbol value)
89 "Initialize SYMBOL with VALUE.
90 Like `custom-initialize-reset', but only use the `:set' function if the
91 not using the standard setting.
92 For the standard setting, use the `set-default'."
93 (cond ((default-boundp symbol)
94 (funcall (or (get symbol 'custom-set) 'set-default)
95 symbol
96 (funcall (or (get symbol 'custom-get) 'default-value)
97 symbol)))
98 ((get symbol 'saved-value)
99 (funcall (or (get symbol 'custom-set) 'set-default)
100 symbol
101 (eval (car (get symbol 'saved-value)))))
102 (t
103 (set-default symbol (eval value)))))
104
105 (defun custom-declare-variable (symbol default doc &rest args)
106 "Like `defcustom', but SYMBOL and DEFAULT are evaluated as normal arguments.
107 DEFAULT should be an expression to evaluate to compute the default value,
108 not the default value itself."
109 ;; Remember the standard setting.
110 (put symbol 'standard-value (list default))
111 ;; Maybe this option was rogue in an earlier version. It no longer is.
112 (when (get symbol 'force-value)
113 ;; It no longer is.
114 (put symbol 'force-value nil))
115 (when doc
116 (put symbol 'variable-documentation doc))
117 (let ((initialize 'custom-initialize-reset)
118 (requests nil))
119 (while args
120 (let ((arg (car args)))
121 (setq args (cdr args))
122 (unless (symbolp arg)
123 (error "Junk in args %S" args))
124 (let ((keyword arg)
125 (value (car args)))
126 (unless args
127 (error "Keyword %s is missing an argument" keyword))
128 (setq args (cdr args))
129 (cond ((eq keyword :initialize)
130 (setq initialize value))
131 ((eq keyword :set)
132 (put symbol 'custom-set value))
133 ((eq keyword :get)
134 (put symbol 'custom-get value))
135 ((eq keyword :require)
136 (setq requests (cons value requests)))
137 ((eq keyword :type)
138 (put symbol 'custom-type value))
139 ((eq keyword :options)
140 (if (get symbol 'custom-options)
141 ;; Slow safe code to avoid duplicates.
142 (mapcar (lambda (option)
143 (custom-add-option symbol option))
144 value)
145 ;; Fast code for the common case.
146 (put symbol 'custom-options (copy-sequence value))))
147 (t
148 (custom-handle-keyword symbol keyword value
149 'custom-variable))))))
150 (put symbol 'custom-requests requests)
151 ;; Do the actual initialization.
152 (funcall initialize symbol default))
153 (setq current-load-list (cons symbol current-load-list))
154 (run-hooks 'custom-define-hook)
155 symbol)
156
157 (defmacro defcustom (symbol value doc &rest args)
158 "Declare SYMBOL as a customizable variable that defaults to VALUE.
159 DOC is the variable documentation.
160
161 Neither SYMBOL nor VALUE needs to be quoted.
162 If SYMBOL is not already bound, initialize it to VALUE.
163 The remaining arguments should have the form
164
165 [KEYWORD VALUE]...
166
167 The following keywords are meaningful:
168
169 :type VALUE should be a widget type for editing the symbols value.
170 The default is `sexp'.
171 :options VALUE should be a list of valid members of the widget type.
172 :group VALUE should be a customization group.
173 Add SYMBOL to that group.
174 :initialize
175 VALUE should be a function used to initialize the
176 variable. It takes two arguments, the symbol and value
177 given in the `defcustom' call. The default is
178 `custom-initialize-default'
179 :set VALUE should be a function to set the value of the symbol.
180 It takes two arguments, the symbol to set and the value to
181 give it. The default choice of function is `custom-set-default'.
182 :get VALUE should be a function to extract the value of symbol.
183 The function takes one argument, a symbol, and should return
184 the current value for that symbol. The default choice of function
185 is `custom-default-value'.
186 :require
187 VALUE should be a feature symbol. If you save a value
188 for this option, then when your `.emacs' file loads the value,
189 it does (require VALUE) first.
190
191 Read the section about customization in the Emacs Lisp manual for more
192 information."
193 ;; It is better not to use backquote in this file,
194 ;; because that makes a bootstrapping problem
195 ;; if you need to recompile all the Lisp files using interpreted code.
196 (nconc (list 'custom-declare-variable
197 (list 'quote symbol)
198 (list 'quote value)
199 doc)
200 args))
201
202 ;;; The `defface' Macro.
203
204 (defmacro defface (face spec doc &rest args)
205 "Declare FACE as a customizable face that defaults to SPEC.
206 FACE does not need to be quoted.
207
208 Third argument DOC is the face documentation.
209
210 If FACE has been set with `custom-set-face', set the face attributes
211 as specified by that function, otherwise set the face attributes
212 according to SPEC.
213
214 The remaining arguments should have the form
215
216 [KEYWORD VALUE]...
217
218 The following KEYWORDs are defined:
219
220 :group VALUE should be a customization group.
221 Add FACE to that group.
222
223 SPEC should be an alist of the form ((DISPLAY ATTS)...).
224
225 The first element of SPEC where the DISPLAY matches the frame
226 is the one that takes effect in that frame. The ATTRs in this
227 element take effect; the other elements are ignored, on that frame.
228
229 ATTS is a list of face attributes followed by their values:
230 (ATTR VALUE ATTR VALUE...)
231
232 The possible attributes are `:family', `:width', `:height', `:weight',
233 `:slant', `:underline', `:overline', `:strike-through', `:box',
234 `:foreground', `:background', `:stipple', and `:inverse-video'.
235
236 DISPLAY can either be the symbol t, which will match all frames, or an
237 alist of the form \((REQ ITEM...)...). For the DISPLAY to match a
238 FRAME, the REQ property of the frame must match one of the ITEM. The
239 following REQ are defined:
240
241 `type' (the value of `window-system')
242 Under X, in addition to the values `window-system' can take,
243 `motif', `lucid' and `x-toolkit' are allowed, and match when
244 the Motif toolkit, Lucid toolkit, or any X toolkit is in use.
245
246 `class' (the frame's color support)
247 Should be one of `color', `grayscale', or `mono'.
248
249 `background' (what color is used for the background text)
250 Should be one of `light' or `dark'.
251
252 Read the section about customization in the Emacs Lisp manual for more
253 information."
254 ;; It is better not to use backquote in this file,
255 ;; because that makes a bootstrapping problem
256 ;; if you need to recompile all the Lisp files using interpreted code.
257 (nconc (list 'custom-declare-face (list 'quote face) spec doc) args))
258
259 ;;; The `defgroup' Macro.
260
261 (defun custom-declare-group (symbol members doc &rest args)
262 "Like `defgroup', but SYMBOL is evaluated as a normal argument."
263 (while members
264 (apply 'custom-add-to-group symbol (car members))
265 (setq members (cdr members)))
266 (put symbol 'custom-group (nconc members (get symbol 'custom-group)))
267 (when doc
268 ;; This text doesn't get into DOC.
269 (put symbol 'group-documentation (purecopy doc)))
270 (while args
271 (let ((arg (car args)))
272 (setq args (cdr args))
273 (unless (symbolp arg)
274 (error "Junk in args %S" args))
275 (let ((keyword arg)
276 (value (car args)))
277 (unless args
278 (error "Keyword %s is missing an argument" keyword))
279 (setq args (cdr args))
280 (cond ((eq keyword :prefix)
281 (put symbol 'custom-prefix value))
282 (t
283 (custom-handle-keyword symbol keyword value
284 'custom-group))))))
285 (run-hooks 'custom-define-hook)
286 symbol)
287
288 (defmacro defgroup (symbol members doc &rest args)
289 "Declare SYMBOL as a customization group containing MEMBERS.
290 SYMBOL does not need to be quoted.
291
292 Third arg DOC is the group documentation.
293
294 MEMBERS should be an alist of the form ((NAME WIDGET)...) where
295 NAME is a symbol and WIDGET is a widget for editing that symbol.
296 Useful widgets are `custom-variable' for editing variables,
297 `custom-face' for edit faces, and `custom-group' for editing groups.
298
299 The remaining arguments should have the form
300
301 [KEYWORD VALUE]...
302
303 The following KEYWORD's are defined:
304
305 :group VALUE should be a customization group.
306 Add SYMBOL to that group.
307
308 Read the section about customization in the Emacs Lisp manual for more
309 information."
310 ;; It is better not to use backquote in this file,
311 ;; because that makes a bootstrapping problem
312 ;; if you need to recompile all the Lisp files using interpreted code.
313 (nconc (list 'custom-declare-group (list 'quote symbol) members doc) args))
314
315 (defun custom-add-to-group (group option widget)
316 "To existing GROUP add a new OPTION of type WIDGET.
317 If there already is an entry for that option, overwrite it."
318 (let* ((members (get group 'custom-group))
319 (old (assq option members)))
320 (if old
321 (setcar (cdr old) widget)
322 (put group 'custom-group (nconc members (list (list option widget)))))))
323
324 ;;; Properties.
325
326 (defun custom-handle-all-keywords (symbol args type)
327 "For customization option SYMBOL, handle keyword arguments ARGS.
328 Third argument TYPE is the custom option type."
329 (while args
330 (let ((arg (car args)))
331 (setq args (cdr args))
332 (unless (symbolp arg)
333 (error "Junk in args %S" args))
334 (let ((keyword arg)
335 (value (car args)))
336 (unless args
337 (error "Keyword %s is missing an argument" keyword))
338 (setq args (cdr args))
339 (custom-handle-keyword symbol keyword value type)))))
340
341 (defun custom-handle-keyword (symbol keyword value type)
342 "For customization option SYMBOL, handle KEYWORD with VALUE.
343 Fourth argument TYPE is the custom option type."
344 (if purify-flag
345 (setq value (purecopy value)))
346 (cond ((eq keyword :group)
347 (custom-add-to-group value symbol type))
348 ((eq keyword :version)
349 (custom-add-version symbol value))
350 ((eq keyword :link)
351 (custom-add-link symbol value))
352 ((eq keyword :load)
353 (custom-add-load symbol value))
354 ((eq keyword :tag)
355 (put symbol 'custom-tag value))
356 ((eq keyword :set-after)
357 (custom-add-dependencies symbol value))
358 (t
359 (error "Unknown keyword %s" keyword))))
360
361 (defun custom-add-dependencies (symbol value)
362 "To the custom option SYMBOL, add dependencies specified by VALUE.
363 VALUE should be a list of symbols. For each symbol in that list,
364 this specifies that SYMBOL should be set after the specified symbol, if
365 both appear in constructs like `custom-set-variables'."
366 (unless (listp value)
367 (error "Invalid custom dependency `%s'" value))
368 (let* ((deps (get symbol 'custom-dependencies))
369 (new-deps deps))
370 (while value
371 (let ((dep (car value)))
372 (unless (symbolp dep)
373 (error "Invalid custom dependency `%s'" dep))
374 (unless (memq dep new-deps)
375 (setq new-deps (cons dep new-deps)))
376 (setq value (cdr value))))
377 (unless (eq deps new-deps)
378 (put symbol 'custom-dependencies new-deps))))
379
380 (defun custom-add-option (symbol option)
381 "To the variable SYMBOL add OPTION.
382
383 If SYMBOL is a hook variable, OPTION should be a hook member.
384 For other types variables, the effect is undefined."
385 (let ((options (get symbol 'custom-options)))
386 (unless (member option options)
387 (put symbol 'custom-options (cons option options)))))
388
389 (defun custom-add-link (symbol widget)
390 "To the custom option SYMBOL add the link WIDGET."
391 (let ((links (get symbol 'custom-links)))
392 (unless (member widget links)
393 (put symbol 'custom-links (cons (purecopy widget) links)))))
394
395 (defun custom-add-version (symbol version)
396 "To the custom option SYMBOL add the version VERSION."
397 (put symbol 'custom-version (purecopy version)))
398
399 (defun custom-add-load (symbol load)
400 "To the custom option SYMBOL add the dependency LOAD.
401 LOAD should be either a library file name, or a feature name."
402 (let ((loads (get symbol 'custom-loads)))
403 (unless (member load loads)
404 (put symbol 'custom-loads (cons (purecopy load) loads)))))
405
406 ;;; Initializing.
407
408 (defvar custom-local-buffer nil
409 "Non-nil, in a Customization buffer, means customize a specific buffer.
410 If this variable is non-nil, it should be a buffer,
411 and it means customize the local bindings of that buffer.
412 This variable is a permanent local, and it normally has a local binding
413 in every Customization buffer.")
414 (put 'custom-local-buffer 'permanent-local t)
415
416 (defun custom-set-variables (&rest args)
417 "Initialize variables according to user preferences.
418
419 The arguments should be a list where each entry has the form:
420
421 (SYMBOL VALUE [NOW [REQUEST [COMMENT]]])
422
423 The unevaluated VALUE is stored as the saved value for SYMBOL.
424 If NOW is present and non-nil, VALUE is also evaluated and bound as
425 the default value for the SYMBOL.
426 REQUEST is a list of features we must require for SYMBOL.
427 COMMENT is a comment string about SYMBOL."
428 (setq args
429 (sort args
430 (lambda (a1 a2)
431 (let* ((sym1 (car a1))
432 (sym2 (car a2))
433 (1-then-2 (memq sym1 (get sym2 'custom-dependencies)))
434 (2-then-1 (memq sym2 (get sym1 'custom-dependencies))))
435 (cond ((and 1-then-2 2-then-1)
436 (error "Circular custom dependency between `%s' and `%s'"
437 sym1 sym2))
438 (2-then-1 nil)
439 (t t))))))
440 (while args
441 (let ((entry (car args)))
442 (if (listp entry)
443 (let* ((symbol (nth 0 entry))
444 (value (nth 1 entry))
445 (now (nth 2 entry))
446 (requests (nth 3 entry))
447 (comment (nth 4 entry))
448 set)
449 (when requests
450 (put symbol 'custom-requests requests)
451 (mapcar 'require requests))
452 (setq set (or (get symbol 'custom-set) 'custom-set-default))
453 (put symbol 'saved-value (list value))
454 (put symbol 'saved-variable-comment comment)
455 ;; Allow for errors in the case where the setter has
456 ;; changed between versions, say.
457 (condition-case nil
458 (cond (now
459 ;; Rogue variable, set it now.
460 (put symbol 'force-value t)
461 (funcall set symbol (eval value)))
462 ((default-boundp symbol)
463 ;; Something already set this, overwrite it.
464 (funcall set symbol (eval value))))
465 (error nil))
466 (setq args (cdr args))
467 (and (or now (default-boundp symbol))
468 (put symbol 'variable-comment comment)))
469 ;; Old format, a plist of SYMBOL VALUE pairs.
470 (message "Warning: old format `custom-set-variables'")
471 (ding)
472 (sit-for 2)
473 (let ((symbol (nth 0 args))
474 (value (nth 1 args)))
475 (put symbol 'saved-value (list value)))
476 (setq args (cdr (cdr args)))))))
477
478 (defun custom-set-default (variable value)
479 "Default :set function for a customizable variable.
480 Normally, this sets the default value of VARIABLE to VALUE,
481 but if `custom-local-buffer' is non-nil,
482 this sets the local binding in that buffer instead."
483 (if custom-local-buffer
484 (with-current-buffer custom-local-buffer
485 (set variable value))
486 (set-default variable value)))
487
488 ;;; The End.
489
490 ;; Process the defcustoms for variables loaded before this file.
491 (while custom-declare-variable-list
492 (apply 'custom-declare-variable (car custom-declare-variable-list))
493 (setq custom-declare-variable-list (cdr custom-declare-variable-list)))
494
495 (provide 'custom)
496
497 ;;; custom.el ends here