1 ;;; custom.el -- Tools for declaring and initializing options.
3 ;; Copyright (C) 1996, 1997 Free Software Foundation, Inc.
5 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
6 ;; Keywords: help, faces
8 ;; X-URL: http://www.dina.kvl.dk/~abraham/custom/
12 ;; If you want to use this code, please visit the URL above.
14 ;; This file only contain the code needed to declare and initialize
15 ;; user options. The code to customize options is autoloaded from
18 ;; The code implementing face declarations is in `cus-face.el'
24 (define-widget-keywords :prefix
:tag
:load
:link
:options
:type
:group
)
26 (defvar custom-define-hook nil
27 ;; Customize information for this option is in `cus-edit.el'.
28 "Hook called after defining each customize option.")
30 ;;; The `defcustom' Macro.
32 (defun custom-declare-variable (symbol value doc
&rest args
)
33 "Like `defcustom', but SYMBOL and VALUE are evaluated as normal arguments."
34 ;; Bind this variable unless it already is bound.
35 (unless (default-boundp symbol
)
36 ;; Use the saved value if it exists, otherwise the factory setting.
37 (set-default symbol
(if (get symbol
'saved-value
)
38 (eval (car (get symbol
'saved-value
)))
40 ;; Remember the factory setting.
41 (put symbol
'factory-value
(list value
))
42 ;; Maybe this option was rogue in an earlier version. It no longer is.
43 (when (get symbol
'force-value
)
45 (put symbol
'force-value nil
))
47 (put symbol
'variable-documentation doc
))
49 (let ((arg (car args
)))
50 (setq args
(cdr args
))
52 (error "Junk in args %S" args
))
56 (error "Keyword %s is missing an argument" keyword
))
57 (setq args
(cdr args
))
58 (cond ((eq keyword
:type
)
59 (put symbol
'custom-type value
))
60 ((eq keyword
:options
)
61 (if (get symbol
'custom-options
)
62 ;; Slow safe code to avoid duplicates.
63 (mapcar (lambda (option)
64 (custom-add-option symbol option
))
66 ;; Fast code for the common case.
67 (put symbol
'custom-options
(copy-list value
))))
69 (custom-handle-keyword symbol keyword value
70 'custom-variable
))))))
71 (run-hooks 'custom-define-hook
)
74 (defmacro defcustom
(symbol value doc
&rest args
)
75 "Declare SYMBOL as a customizable variable that defaults to VALUE.
76 DOC is the variable documentation.
78 Neither SYMBOL nor VALUE needs to be quoted.
79 If SYMBOL is not already bound, initialize it to VALUE.
80 The remaining arguments should have the form
84 The following KEYWORD's are defined:
86 :type VALUE should be a widget type.
87 :options VALUE should be a list of valid members of the widget type.
88 :group VALUE should be a customization group.
89 Add SYMBOL to that group.
91 Read the section about customization in the Emacs Lisp manual for more
94 (custom-declare-variable (quote ,symbol
) (quote ,value
) ,doc
,@args
)))
96 ;;; The `defface' Macro.
98 (defmacro defface
(face spec doc
&rest args
)
99 "Declare FACE as a customizable face that defaults to SPEC.
100 FACE does not need to be quoted.
102 Third argument DOC is the face documentation.
104 If FACE has been set with `custom-set-face', set the face attributes
105 as specified by that function, otherwise set the face attributes
108 The remaining arguments should have the form
112 The following KEYWORD's are defined:
114 :group VALUE should be a customization group.
115 Add FACE to that group.
117 SPEC should be an alist of the form ((DISPLAY ATTS)...).
119 ATTS is a list of face attributes and their values. The possible
120 attributes are defined in the variable `custom-face-attributes'.
121 Alternatively, ATTS can be a face in which case the attributes of that
124 The ATTS of the first entry in SPEC where the DISPLAY matches the
125 frame should take effect in that frame. DISPLAY can either be the
126 symbol t, which will match all frames, or an alist of the form
129 For the DISPLAY to match a FRAME, the REQ property of the frame must
130 match one of the ITEM. The following REQ are defined:
132 `type' (the value of `window-system')
133 Should be one of `x' or `tty'.
135 `class' (the frame's color support)
136 Should be one of `color', `grayscale', or `mono'.
138 `background' (what color is used for the background text)
139 Should be one of `light' or `dark'.
141 Read the section about customization in the Emacs Lisp manual for more
143 `(custom-declare-face (quote ,face
) ,spec
,doc
,@args
))
145 ;;; The `defgroup' Macro.
147 (defun custom-declare-group (symbol members doc
&rest args
)
148 "Like `defgroup', but SYMBOL is evaluated as a normal argument."
149 (put symbol
'custom-group
(nconc members
(get symbol
'custom-group
)))
151 (put symbol
'group-documentation doc
))
153 (let ((arg (car args
)))
154 (setq args
(cdr args
))
155 (unless (symbolp arg
)
156 (error "Junk in args %S" args
))
160 (error "Keyword %s is missing an argument" keyword
))
161 (setq args
(cdr args
))
162 (cond ((eq keyword
:prefix
)
163 (put symbol
'custom-prefix value
))
165 (custom-handle-keyword symbol keyword value
167 (run-hooks 'custom-define-hook
)
170 (defmacro defgroup
(symbol members doc
&rest args
)
171 "Declare SYMBOL as a customization group containing MEMBERS.
172 SYMBOL does not need to be quoted.
174 Third arg DOC is the group documentation.
176 MEMBERS should be an alist of the form ((NAME WIDGET)...) where
177 NAME is a symbol and WIDGET is a widget is a widget for editing that
178 symbol. Useful widgets are `custom-variable' for editing variables,
179 `custom-face' for edit faces, and `custom-group' for editing groups.
181 The remaining arguments should have the form
185 The following KEYWORD's are defined:
187 :group VALUE should be a customization group.
188 Add SYMBOL to that group.
190 Read the section about customization in the Emacs Lisp manual for more
192 `(custom-declare-group (quote ,symbol
) ,members
,doc
,@args
))
194 (defun custom-add-to-group (group option widget
)
195 "To existing GROUP add a new OPTION of type WIDGET.
196 If there already is an entry for that option, overwrite it."
197 (let* ((members (get group
'custom-group
))
198 (old (assq option members
)))
200 (setcar (cdr old
) widget
)
201 (put group
'custom-group
(nconc members
(list (list option widget
)))))))
205 (defun custom-handle-all-keywords (symbol args type
)
206 "For customization option SYMBOL, handle keyword arguments ARGS.
207 Third argument TYPE is the custom option type."
209 (let ((arg (car args
)))
210 (setq args
(cdr args
))
211 (unless (symbolp arg
)
212 (error "Junk in args %S" args
))
216 (error "Keyword %s is missing an argument" keyword
))
217 (setq args
(cdr args
))
218 (custom-handle-keyword symbol keyword value type
)))))
220 (defun custom-handle-keyword (symbol keyword value type
)
221 "For customization option SYMBOL, handle KEYWORD with VALUE.
222 Fourth argument TYPE is the custom option type."
223 (cond ((eq keyword
:group
)
224 (custom-add-to-group value symbol type
))
226 (custom-add-link symbol value
))
228 (custom-add-load symbol value
))
230 (put symbol
'custom-tag value
))
232 (error "Unknown keyword %s" symbol
))))
234 (defun custom-add-option (symbol option
)
235 "To the variable SYMBOL add OPTION.
237 If SYMBOL is a hook variable, OPTION should be a hook member.
238 For other types variables, the effect is undefined."
239 (let ((options (get symbol
'custom-options
)))
240 (unless (member option options
)
241 (put symbol
'custom-options
(cons option options
)))))
243 (defun custom-add-link (symbol widget
)
244 "To the custom option SYMBOL add the link WIDGET."
245 (let ((links (get symbol
'custom-links
)))
246 (unless (member widget links
)
247 (put symbol
'custom-links
(cons widget links
)))))
249 (defun custom-add-load (symbol load
)
250 "To the custom option SYMBOL add the dependency LOAD.
251 LOAD should be either a library file name, or a feature name."
252 (let ((loads (get symbol
'custom-loads
)))
253 (unless (member load loads
)
254 (put symbol
'custom-loads
(cons load loads
)))))
258 (defun custom-set-variables (&rest args
)
259 "Initialize variables according to user preferences.
261 The arguments should be a list where each entry has the form:
265 The unevaluated VALUE is stored as the saved value for SYMBOL.
266 If NOW is present and non-nil, VALUE is also evaluated and bound as
267 the default value for the SYMBOL."
269 (let ((entry (car args
)))
271 (let ((symbol (nth 0 entry
))
272 (value (nth 1 entry
))
274 (put symbol
'saved-value
(list value
))
276 ;; Rogue variable, set it now.
277 (put symbol
'force-value t
)
278 (set-default symbol
(eval value
)))
279 ((default-boundp symbol
)
280 ;; Something already set this, overwrite it.
281 (set-default symbol
(eval value
))))
282 (setq args
(cdr args
)))
283 ;; Old format, a plist of SYMBOL VALUE pairs.
284 (message "Warning: old format `custom-set-variables'")
287 (let ((symbol (nth 0 args
))
288 (value (nth 1 args
)))
289 (put symbol
'saved-value
(list value
)))
290 (setq args
(cdr (cdr args
)))))))
296 ;; custom.el ends here