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