Use make-temp-file.
[emacs.git] / lisp / custom.el
blob821e0f3232fa529984ed737e78c5348f160a1292
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)
9 ;; This file is part of GNU Emacs.
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.
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.
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.
26 ;;; Commentary:
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.
32 ;; The code implementing face declarations is in `cus-face.el'
34 ;;; Code:
36 (require 'widget)
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.")
42 ;;; The `defcustom' Macro.
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)))))
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)))))
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))))
85 (eval value)))))
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)))))
102 (set-default symbol (eval value)))))
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))))
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)
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.
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
164 [KEYWORD VALUE]...
166 The following keywords are meaningful:
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.
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))
201 ;;; The `defface' Macro.
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.
207 Third argument DOC is the face documentation.
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.
213 The remaining arguments should have the form
215 [KEYWORD VALUE]...
217 The following KEYWORDs are defined:
219 :group VALUE should be a customization group.
220 Add FACE to that group.
222 SPEC should be an alist of the form ((DISPLAY ATTS)...).
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.
228 ATTS is a list of face attributes followed by their values:
229 (ATTR VALUE ATTR VALUE...)
231 The possible attributes are `:family', `:width', `:height', `:weight',
232 `:slant', `:underline', `:overline', `:strike-through', `:box',
233 `:foreground', `:background', `:stipple', and `:inverse-video'.
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:
240 `type' (the value of `window-system')
241 Under X, in addition to the values `window-system' can take,
242 `motif', `lucid' and `x-toolkit' are allowed, and match when
243 the Motif toolkit, Lucid toolkit, or any X toolkit is in use.
245 `class' (the frame's color support)
246 Should be one of `color', `grayscale', or `mono'.
248 `background' (what color is used for the background text)
249 Should be one of `light' or `dark'.
251 Read the section about customization in the Emacs Lisp manual for more
252 information."
253 ;; It is better not to use backquote in this file,
254 ;; because that makes a bootstrapping problem
255 ;; if you need to recompile all the Lisp files using interpreted code.
256 (nconc (list 'custom-declare-face (list 'quote face) spec doc) args))
258 ;;; The `defgroup' Macro.
260 (defun custom-declare-group (symbol members doc &rest args)
261 "Like `defgroup', but SYMBOL is evaluated as a normal argument."
262 (while members
263 (apply 'custom-add-to-group symbol (car members))
264 (setq members (cdr members)))
265 (put symbol 'custom-group (nconc members (get symbol 'custom-group)))
266 (when doc
267 (put symbol 'group-documentation doc))
268 (while args
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))
281 (custom-handle-keyword symbol keyword value
282 'custom-group))))))
283 (run-hooks 'custom-define-hook)
284 symbol)
286 (defmacro defgroup (symbol members doc &rest args)
287 "Declare SYMBOL as a customization group containing MEMBERS.
288 SYMBOL does not need to be quoted.
290 Third arg DOC is the group documentation.
292 MEMBERS should be an alist of the form ((NAME WIDGET)...) where
293 NAME is a symbol and WIDGET is a widget for editing that symbol.
294 Useful widgets are `custom-variable' for editing variables,
295 `custom-face' for edit faces, and `custom-group' for editing groups.
297 The remaining arguments should have the form
299 [KEYWORD VALUE]...
301 The following KEYWORD's are defined:
303 :group VALUE should be a customization group.
304 Add SYMBOL to that group.
306 Read the section about customization in the Emacs Lisp manual for more
307 information."
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))
313 (defun custom-add-to-group (group option widget)
314 "To existing GROUP add a new OPTION of type WIDGET.
315 If 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)))))))
322 ;;; Properties.
324 (defun custom-handle-all-keywords (symbol args type)
325 "For customization option SYMBOL, handle keyword arguments ARGS.
326 Third argument TYPE is the custom option type."
327 (while args
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))
337 (custom-handle-keyword symbol keyword value type)))))
339 (defun custom-handle-keyword (symbol keyword value type)
340 "For customization option SYMBOL, handle KEYWORD with VALUE.
341 Fourth argument TYPE is the custom option type."
342 (cond ((eq keyword :group)
343 (custom-add-to-group value symbol type))
344 ((eq keyword :version)
345 (custom-add-version symbol value))
346 ((eq keyword :link)
347 (custom-add-link symbol value))
348 ((eq keyword :load)
349 (custom-add-load symbol value))
350 ((eq keyword :tag)
351 (put symbol 'custom-tag value))
353 (error "Unknown keyword %s" keyword))))
355 (defun custom-add-option (symbol option)
356 "To the variable SYMBOL add OPTION.
358 If SYMBOL is a hook variable, OPTION should be a hook member.
359 For other types variables, the effect is undefined."
360 (let ((options (get symbol 'custom-options)))
361 (unless (member option options)
362 (put symbol 'custom-options (cons option options)))))
364 (defun custom-add-link (symbol widget)
365 "To the custom option SYMBOL add the link WIDGET."
366 (let ((links (get symbol 'custom-links)))
367 (unless (member widget links)
368 (put symbol 'custom-links (cons widget links)))))
370 (defun custom-add-version (symbol version)
371 "To the custom option SYMBOL add the version VERSION."
372 (put symbol 'custom-version version))
374 (defun custom-add-load (symbol load)
375 "To the custom option SYMBOL add the dependency LOAD.
376 LOAD should be either a library file name, or a feature name."
377 (let ((loads (get symbol 'custom-loads)))
378 (unless (member load loads)
379 (put symbol 'custom-loads (cons load loads)))))
381 ;;; Initializing.
383 (defvar custom-local-buffer nil
384 "Non-nil, in a Customization buffer, means customize a specific buffer.
385 If this variable is non-nil, it should be a buffer,
386 and it means customize the local bindings of that buffer.
387 This variable is a permanent local, and it normally has a local binding
388 in every Customization buffer.")
389 (put 'custom-local-buffer 'permanent-local t)
391 (defun custom-set-variables (&rest args)
392 "Initialize variables according to user preferences.
394 The arguments should be a list where each entry has the form:
396 (SYMBOL VALUE [NOW [REQUEST [COMMENT]]])
398 The unevaluated VALUE is stored as the saved value for SYMBOL.
399 If NOW is present and non-nil, VALUE is also evaluated and bound as
400 the default value for the SYMBOL.
401 REQUEST is a list of features we must require for SYMBOL.
402 COMMENT is a comment string about SYMBOL."
403 (while args
404 (let ((entry (car args)))
405 (if (listp entry)
406 (let* ((symbol (nth 0 entry))
407 (value (nth 1 entry))
408 (now (nth 2 entry))
409 (requests (nth 3 entry))
410 (comment (nth 4 entry))
411 set)
412 (when requests
413 (put symbol 'custom-requests requests)
414 (mapcar 'require requests))
415 (setq set (or (get symbol 'custom-set) 'custom-set-default))
416 (put symbol 'saved-value (list value))
417 (put symbol 'saved-variable-comment comment)
418 ;; Allow for errors in the case where the setter has
419 ;; changed between versions, say.
420 (condition-case nil
421 (cond (now
422 ;; Rogue variable, set it now.
423 (put symbol 'force-value t)
424 (funcall set symbol (eval value)))
425 ((default-boundp symbol)
426 ;; Something already set this, overwrite it.
427 (funcall set symbol (eval value))))
428 (error nil))
429 (setq args (cdr args))
430 (and (or now (default-boundp symbol))
431 (put symbol 'variable-comment comment)))
432 ;; Old format, a plist of SYMBOL VALUE pairs.
433 (message "Warning: old format `custom-set-variables'")
434 (ding)
435 (sit-for 2)
436 (let ((symbol (nth 0 args))
437 (value (nth 1 args)))
438 (put symbol 'saved-value (list value)))
439 (setq args (cdr (cdr args)))))))
441 (defun custom-set-default (variable value)
442 "Default :set function for a customizable variable.
443 Normally, this sets the default value of VARIABLE to VALUE,
444 but if `custom-local-buffer' is non-nil,
445 this sets the local binding in that buffer instead."
446 (if custom-local-buffer
447 (with-current-buffer custom-local-buffer
448 (set variable value))
449 (set-default variable value)))
451 ;;; The End.
453 ;; Process the defcustoms for variables loaded before this file.
454 (while custom-declare-variable-list
455 (apply 'custom-declare-variable (car custom-declare-variable-list))
456 (setq custom-declare-variable-list (cdr custom-declare-variable-list)))
458 (provide 'custom)
460 ;;; custom.el ends here