new version
[emacs.git] / lisp / custom.el
blob127d96e1af9d0c6afa559b1cccd9a30c8c3ba448
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/
10 ;; This file is part of GNU Emacs.
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.
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.
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.
27 ;;; Commentary:
29 ;; If you want to use this code, please visit the URL above.
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'.
35 ;; The code implementing face declarations is in `cus-face.el'
37 ;;; Code:
39 (require 'widget)
41 (define-widget-keywords :initialize :set :get :require :prefix :tag
42 :load :link :options :type :group)
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.")
49 ;;; The `defcustom' Macro.
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)))))
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)))))
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))))
92 (eval value)))))
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)))))
109 (set-default symbol (eval value)))))
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))))
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 (run-hooks 'custom-define-hook)
160 symbol)
162 (defmacro defcustom (symbol value doc &rest args)
163 "Declare SYMBOL as a customizable variable that defaults to VALUE.
164 DOC is the variable documentation.
166 Neither SYMBOL nor VALUE needs to be quoted.
167 If SYMBOL is not already bound, initialize it to VALUE.
168 The remaining arguments should have the form
170 [KEYWORD VALUE]...
172 The following KEYWORD's are defined:
174 :type VALUE should be a widget type for editing the symbols value.
175 The default is `sexp'.
176 :options VALUE should be a list of valid members of the widget type.
177 :group VALUE should be a customization group.
178 Add SYMBOL to that group.
179 :initialize VALUE should be a function used to initialize the
180 variable. It takes two arguments, the symbol and value
181 given in the `defcustom' call. The default is
182 `custom-initialize-default'
183 :set VALUE should be a function to set the value of the symbol.
184 It takes two arguments, the symbol to set and the value to
185 give it. The default is `set-default'.
186 :get VALUE should be a function to extract the value of symbol.
187 The function takes one argument, a symbol, and should return
188 the current value for that symbol. The default is
189 `default-value'.
190 :require VALUE should be a feature symbol. Each feature will be
191 required after initialization, of the the user have saved this
192 option.
194 Read the section about customization in the Emacs Lisp manual for more
195 information."
196 ;; It is better not to use backquote in this file,
197 ;; because that makes a bootstrapping problem
198 ;; if you need to recompile all the Lisp files using interpreted code.
199 (nconc (list 'custom-declare-variable
200 (list 'quote symbol)
201 (list 'quote value)
202 doc)
203 args))
205 ;;; The `defface' Macro.
207 (defmacro defface (face spec doc &rest args)
208 "Declare FACE as a customizable face that defaults to SPEC.
209 FACE does not need to be quoted.
211 Third argument DOC is the face documentation.
213 If FACE has been set with `custom-set-face', set the face attributes
214 as specified by that function, otherwise set the face attributes
215 according to SPEC.
217 The remaining arguments should have the form
219 [KEYWORD VALUE]...
221 The following KEYWORDs are defined:
223 :group VALUE should be a customization group.
224 Add FACE to that group.
226 SPEC should be an alist of the form ((DISPLAY ATTS)...).
228 The first element of SPEC where the DISPLAY matches the frame
229 is the one that takes effect in that frame. The ATTRs in this
230 element take effect; the other elements are ignored, on that frame.
232 ATTS is a list of face attributes followed by their values:
233 (ATTR VALUE ATTR VALUE...)
234 The possible attributes are `:bold', `:italic', `:underline',
235 `:foreground', `:background', `:stipple' and `:inverse-video'.
237 DISPLAY can either be the symbol t, which will match all frames, or an
238 alist of the form \((REQ ITEM...)...). For the DISPLAY to match a
239 FRAME, the REQ property of the frame must match one of the ITEM. The
240 following REQ are defined:
242 `type' (the value of `window-system')
243 Should be one of `x' or `tty'.
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" symbol))))
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 (defun custom-set-variables (&rest args)
384 "Initialize variables according to user preferences.
386 The arguments should be a list where each entry has the form:
388 (SYMBOL VALUE [NOW])
390 The unevaluated VALUE is stored as the saved value for SYMBOL.
391 If NOW is present and non-nil, VALUE is also evaluated and bound as
392 the default value for the SYMBOL."
393 (while args
394 (let ((entry (car args)))
395 (if (listp entry)
396 (let* ((symbol (nth 0 entry))
397 (value (nth 1 entry))
398 (now (nth 2 entry))
399 (requests (nth 3 entry))
400 (set (or (get symbol 'custom-set) 'set-default)))
401 (put symbol 'saved-value (list value))
402 (cond (now
403 ;; Rogue variable, set it now.
404 (put symbol 'force-value t)
405 (funcall set symbol (eval value)))
406 ((default-boundp symbol)
407 ;; Something already set this, overwrite it.
408 (funcall set symbol (eval value))))
409 (when requests
410 (put symbol 'custom-requests requests)
411 (mapcar 'require requests))
412 (setq args (cdr args)))
413 ;; Old format, a plist of SYMBOL VALUE pairs.
414 (message "Warning: old format `custom-set-variables'")
415 (ding)
416 (sit-for 2)
417 (let ((symbol (nth 0 args))
418 (value (nth 1 args)))
419 (put symbol 'saved-value (list value)))
420 (setq args (cdr (cdr args)))))))
422 ;;; The End.
424 ;; Process the defcustoms for variables loaded before this file.
425 (while custom-declare-variable-list
426 (apply 'custom-declare-variable (car custom-declare-variable-list))
427 (setq custom-declare-variable-list (cdr custom-declare-variable-list)))
429 (provide 'custom)
431 ;; custom.el ends here