(bat-generic-mode): "::"-style comments don't
[bpt/emacs.git] / lisp / custom.el
1 ;;; custom.el -- Tools for declaring and initializing options.
2 ;;
3 ;; Copyright (C) 1996, 1997 Free Software Foundation, Inc.
4 ;;
5 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
6 ;; Keywords: help, faces
7 ;; Version: 1.9900
8 ;; X-URL: http://www.dina.kvl.dk/~abraham/custom/
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 ;; If you want to use this code, please visit the URL above.
30 ;;
31 ;; This file only contain the code needed to declare and initialize
32 ;; user options. The code to customize options is autoloaded from
33 ;; `cus-edit.el'.
34
35 ;; The code implementing face declarations is in `cus-face.el'
36
37 ;;; Code:
38
39 (require 'widget)
40
41 (define-widget-keywords :initialize :set :get :require :prefix :tag
42 :load :link :options :type :group)
43
44
45 (defvar custom-define-hook nil
46 ;; Customize information for this option is in `cus-edit.el'.
47 "Hook called after defining each customize option.")
48
49 ;;; The `defcustom' Macro.
50
51 (defun custom-initialize-default (symbol value)
52 "Initialize SYMBOL with VALUE.
53 This will do nothing if symbol already has a default binding.
54 Otherwise, if symbol has a `saved-value' property, it will evaluate
55 the car of that and used as the default binding for symbol.
56 Otherwise, VALUE will be evaluated and used as the default binding for
57 symbol."
58 (unless (default-boundp symbol)
59 ;; Use the saved value if it exists, otherwise the standard setting.
60 (set-default symbol (if (get symbol 'saved-value)
61 (eval (car (get symbol 'saved-value)))
62 (eval value)))))
63
64 (defun custom-initialize-set (symbol value)
65 "Initialize SYMBOL based on VALUE.
66 If the symbol doesn't have a default binding already,
67 then set it using its `:set' function (or `set-default' if it has none).
68 The value is either the value in the symbol's `saved-value' property,
69 if any, or VALUE."
70 (unless (default-boundp symbol)
71 (funcall (or (get symbol 'custom-set) 'set-default)
72 symbol
73 (if (get symbol 'saved-value)
74 (eval (car (get symbol 'saved-value)))
75 (eval value)))))
76
77 (defun custom-initialize-reset (symbol value)
78 "Initialize SYMBOL based on VALUE.
79 Set the symbol, using its `:set' function (or `set-default' if it has none).
80 The value is either the symbol's current value
81 \(as obtained using the `:get' function), if any,
82 or the value in the symbol's `saved-value' property if any,
83 or (last of all) VALUE."
84 (funcall (or (get symbol 'custom-set) 'set-default)
85 symbol
86 (cond ((default-boundp symbol)
87 (funcall (or (get symbol 'custom-get) 'default-value)
88 symbol))
89 ((get symbol 'saved-value)
90 (eval (car (get symbol 'saved-value))))
91 (t
92 (eval value)))))
93
94 (defun custom-initialize-changed (symbol value)
95 "Initialize SYMBOL with VALUE.
96 Like `custom-initialize-reset', but only use the `:set' function if the
97 not using the standard setting.
98 For the standard setting, use the `set-default'."
99 (cond ((default-boundp symbol)
100 (funcall (or (get symbol 'custom-set) 'set-default)
101 symbol
102 (funcall (or (get symbol 'custom-get) 'default-value)
103 symbol)))
104 ((get symbol 'saved-value)
105 (funcall (or (get symbol 'custom-set) 'set-default)
106 symbol
107 (eval (car (get symbol 'saved-value)))))
108 (t
109 (set-default symbol (eval value)))))
110
111 (defun custom-declare-variable (symbol default doc &rest args)
112 "Like `defcustom', but SYMBOL and DEFAULT are evaluated as normal arguments.
113 DEFAULT should be an expression to evaluate to compute the default value,
114 not the default value itself."
115 ;; Remember the standard setting.
116 (put symbol 'standard-value (list default))
117 ;; Maybe this option was rogue in an earlier version. It no longer is.
118 (when (get symbol 'force-value)
119 ;; It no longer is.
120 (put symbol 'force-value nil))
121 (when doc
122 (put symbol 'variable-documentation doc))
123 (let ((initialize 'custom-initialize-reset)
124 (requests nil))
125 (while args
126 (let ((arg (car args)))
127 (setq args (cdr args))
128 (unless (symbolp arg)
129 (error "Junk in args %S" args))
130 (let ((keyword arg)
131 (value (car args)))
132 (unless args
133 (error "Keyword %s is missing an argument" keyword))
134 (setq args (cdr args))
135 (cond ((eq keyword :initialize)
136 (setq initialize value))
137 ((eq keyword :set)
138 (put symbol 'custom-set value))
139 ((eq keyword :get)
140 (put symbol 'custom-get value))
141 ((eq keyword :require)
142 (setq requests (cons value requests)))
143 ((eq keyword :type)
144 (put symbol 'custom-type value))
145 ((eq keyword :options)
146 (if (get symbol 'custom-options)
147 ;; Slow safe code to avoid duplicates.
148 (mapcar (lambda (option)
149 (custom-add-option symbol option))
150 value)
151 ;; Fast code for the common case.
152 (put symbol 'custom-options (copy-sequence value))))
153 (t
154 (custom-handle-keyword symbol keyword value
155 'custom-variable))))))
156 (put symbol 'custom-requests requests)
157 ;; Do the actual initialization.
158 (funcall initialize symbol default))
159 (setq current-load-list (cons symbol current-load-list))
160 (run-hooks 'custom-define-hook)
161 symbol)
162
163 (defmacro defcustom (symbol value doc &rest args)
164 "Declare SYMBOL as a customizable variable that defaults to VALUE.
165 DOC is the variable documentation.
166
167 Neither SYMBOL nor VALUE needs to be quoted.
168 If SYMBOL is not already bound, initialize it to VALUE.
169 The remaining arguments should have the form
170
171 [KEYWORD VALUE]...
172
173 The following keywords are meaningful:
174
175 :type VALUE should be a widget type for editing the symbols value.
176 The default is `sexp'.
177 :options VALUE should be a list of valid members of the widget type.
178 :group VALUE should be a customization group.
179 Add SYMBOL to that group.
180 :initialize
181 VALUE should be a function used to initialize the
182 variable. It takes two arguments, the symbol and value
183 given in the `defcustom' call. The default is
184 `custom-initialize-default'
185 :set VALUE should be a function to set the value of the symbol.
186 It takes two arguments, the symbol to set and the value to
187 give it. The default choice of function is `custom-set-default'.
188 :get VALUE should be a function to extract the value of symbol.
189 The function takes one argument, a symbol, and should return
190 the current value for that symbol. The default choice of function
191 is `custom-default-value'.
192 :require
193 VALUE should be a feature symbol. If you save a value
194 for this option, then when your `.emacs' file loads the value,
195 it does (require VALUE) first.
196
197 Read the section about customization in the Emacs Lisp manual for more
198 information."
199 ;; It is better not to use backquote in this file,
200 ;; because that makes a bootstrapping problem
201 ;; if you need to recompile all the Lisp files using interpreted code.
202 (nconc (list 'custom-declare-variable
203 (list 'quote symbol)
204 (list 'quote value)
205 doc)
206 args))
207
208 ;;; The `defface' Macro.
209
210 (defmacro defface (face spec doc &rest args)
211 "Declare FACE as a customizable face that defaults to SPEC.
212 FACE does not need to be quoted.
213
214 Third argument DOC is the face documentation.
215
216 If FACE has been set with `custom-set-face', set the face attributes
217 as specified by that function, otherwise set the face attributes
218 according to SPEC.
219
220 The remaining arguments should have the form
221
222 [KEYWORD VALUE]...
223
224 The following KEYWORDs are defined:
225
226 :group VALUE should be a customization group.
227 Add FACE to that group.
228
229 SPEC should be an alist of the form ((DISPLAY ATTS)...).
230
231 The first element of SPEC where the DISPLAY matches the frame
232 is the one that takes effect in that frame. The ATTRs in this
233 element take effect; the other elements are ignored, on that frame.
234
235 ATTS is a list of face attributes followed by their values:
236 (ATTR VALUE ATTR VALUE...)
237
238 The possible attributes are `:family', `:width', `:height', `:weight',
239 `:slant', `:underline', `:overline', `:strike-through', `:box',
240 `:foreground', `:background', `:stipple', and `:inverse-video'.
241
242 DISPLAY can either be the symbol t, which will match all frames, or an
243 alist of the form \((REQ ITEM...)...). For the DISPLAY to match a
244 FRAME, the REQ property of the frame must match one of the ITEM. The
245 following REQ are defined:
246
247 `type' (the value of `window-system')
248 Should be one of `x' or `tty'.
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
256 Read the section about customization in the Emacs Lisp manual for more
257 information."
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 (put symbol 'group-documentation doc))
273 (while args
274 (let ((arg (car args)))
275 (setq args (cdr args))
276 (unless (symbolp arg)
277 (error "Junk in args %S" args))
278 (let ((keyword arg)
279 (value (car args)))
280 (unless args
281 (error "Keyword %s is missing an argument" keyword))
282 (setq args (cdr args))
283 (cond ((eq keyword :prefix)
284 (put symbol 'custom-prefix value))
285 (t
286 (custom-handle-keyword symbol keyword value
287 'custom-group))))))
288 (run-hooks 'custom-define-hook)
289 symbol)
290
291 (defmacro defgroup (symbol members doc &rest args)
292 "Declare SYMBOL as a customization group containing MEMBERS.
293 SYMBOL does not need to be quoted.
294
295 Third arg DOC is the group documentation.
296
297 MEMBERS should be an alist of the form ((NAME WIDGET)...) where
298 NAME is a symbol and WIDGET is a widget for editing that symbol.
299 Useful widgets are `custom-variable' for editing variables,
300 `custom-face' for edit faces, and `custom-group' for editing groups.
301
302 The remaining arguments should have the form
303
304 [KEYWORD VALUE]...
305
306 The following KEYWORD's are defined:
307
308 :group VALUE should be a customization group.
309 Add SYMBOL to that group.
310
311 Read the section about customization in the Emacs Lisp manual for more
312 information."
313 ;; It is better not to use backquote in this file,
314 ;; because that makes a bootstrapping problem
315 ;; if you need to recompile all the Lisp files using interpreted code.
316 (nconc (list 'custom-declare-group (list 'quote symbol) members doc) args))
317
318 (defun custom-add-to-group (group option widget)
319 "To existing GROUP add a new OPTION of type WIDGET.
320 If there already is an entry for that option, overwrite it."
321 (let* ((members (get group 'custom-group))
322 (old (assq option members)))
323 (if old
324 (setcar (cdr old) widget)
325 (put group 'custom-group (nconc members (list (list option widget)))))))
326
327 ;;; Properties.
328
329 (defun custom-handle-all-keywords (symbol args type)
330 "For customization option SYMBOL, handle keyword arguments ARGS.
331 Third argument TYPE is the custom option type."
332 (while args
333 (let ((arg (car args)))
334 (setq args (cdr args))
335 (unless (symbolp arg)
336 (error "Junk in args %S" args))
337 (let ((keyword arg)
338 (value (car args)))
339 (unless args
340 (error "Keyword %s is missing an argument" keyword))
341 (setq args (cdr args))
342 (custom-handle-keyword symbol keyword value type)))))
343
344 (defun custom-handle-keyword (symbol keyword value type)
345 "For customization option SYMBOL, handle KEYWORD with VALUE.
346 Fourth argument TYPE is the custom option type."
347 (cond ((eq keyword :group)
348 (custom-add-to-group value symbol type))
349 ((eq keyword :version)
350 (custom-add-version symbol value))
351 ((eq keyword :link)
352 (custom-add-link symbol value))
353 ((eq keyword :load)
354 (custom-add-load symbol value))
355 ((eq keyword :tag)
356 (put symbol 'custom-tag value))
357 (t
358 (error "Unknown keyword %s" keyword))))
359
360 (defun custom-add-option (symbol option)
361 "To the variable SYMBOL add OPTION.
362
363 If SYMBOL is a hook variable, OPTION should be a hook member.
364 For other types variables, the effect is undefined."
365 (let ((options (get symbol 'custom-options)))
366 (unless (member option options)
367 (put symbol 'custom-options (cons option options)))))
368
369 (defun custom-add-link (symbol widget)
370 "To the custom option SYMBOL add the link WIDGET."
371 (let ((links (get symbol 'custom-links)))
372 (unless (member widget links)
373 (put symbol 'custom-links (cons widget links)))))
374
375 (defun custom-add-version (symbol version)
376 "To the custom option SYMBOL add the version VERSION."
377 (put symbol 'custom-version version))
378
379 (defun custom-add-load (symbol load)
380 "To the custom option SYMBOL add the dependency LOAD.
381 LOAD should be either a library file name, or a feature name."
382 (let ((loads (get symbol 'custom-loads)))
383 (unless (member load loads)
384 (put symbol 'custom-loads (cons load loads)))))
385
386 ;;; Initializing.
387
388 (defvar custom-local-buffer nil
389 "Non-nil, in a Customization buffer, means customize a specific buffer.
390 If this variable is non-nil, it should be a buffer,
391 and it means customize the local bindings of that buffer.
392 This variable is a permanent local, and it normally has a local binding
393 in every Customization buffer.")
394 (put 'custom-local-buffer 'permanent-local t)
395
396 (defun custom-set-variables (&rest args)
397 "Initialize variables according to user preferences.
398
399 The arguments should be a list where each entry has the form:
400
401 (SYMBOL VALUE [NOW])
402
403 The unevaluated VALUE is stored as the saved value for SYMBOL.
404 If NOW is present and non-nil, VALUE is also evaluated and bound as
405 the default value for the SYMBOL."
406 (while args
407 (let ((entry (car args)))
408 (if (listp entry)
409 (let* ((symbol (nth 0 entry))
410 (value (nth 1 entry))
411 (now (nth 2 entry))
412 (requests (nth 3 entry))
413 set)
414 (when requests
415 (put symbol 'custom-requests requests)
416 (mapcar 'require requests))
417 (setq set (or (get symbol 'custom-set) 'custom-set-default))
418 (put symbol 'saved-value (list value))
419 ;; Allow for errors in the case where the setter has
420 ;; changed between versions, say.
421 (condition-case nil
422 (cond (now
423 ;; Rogue variable, set it now.
424 (put symbol 'force-value t)
425 (funcall set symbol (eval value)))
426 ((default-boundp symbol)
427 ;; Something already set this, overwrite it.
428 (funcall set symbol (eval value))))
429 (error nil))
430 (setq args (cdr args)))
431 ;; Old format, a plist of SYMBOL VALUE pairs.
432 (message "Warning: old format `custom-set-variables'")
433 (ding)
434 (sit-for 2)
435 (let ((symbol (nth 0 args))
436 (value (nth 1 args)))
437 (put symbol 'saved-value (list value)))
438 (setq args (cdr (cdr args)))))))
439
440 (defun custom-set-default (variable value)
441 "Default :set function for a customizable variable.
442 Normally, this sets the default value of VARIABLE to VALUE,
443 but if `custom-local-buffer' is non-nil,
444 this sets the local binding in that buffer instead."
445 (if custom-local-buffer
446 (with-current-buffer custom-local-buffer
447 (set variable value))
448 (set-default variable value)))
449
450 ;;; The End.
451
452 ;; Process the defcustoms for variables loaded before this file.
453 (while custom-declare-variable-list
454 (apply 'custom-declare-variable (car custom-declare-variable-list))
455 (setq custom-declare-variable-list (cdr custom-declare-variable-list)))
456
457 (provide 'custom)
458
459 ;; custom.el ends here