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