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 ;; These autoloads should be deleted eventually.
27 (unless (fboundp 'load-gc)
29 (autoload 'customize "cus-edit" nil t)
30 (autoload 'customize-variable "cus-edit" nil t)
31 (autoload 'customize-variable-other-window "cus-edit" nil t)
32 (autoload 'customize-face "cus-edit" nil t)
33 (autoload 'customize-face-other-window "cus-edit" nil t)
34 (autoload 'customize-apropos "cus-edit" nil t)
35 (autoload 'customize-customized "cus-edit" nil t)
36 (autoload 'custom-buffer-create "cus-edit")
37 (autoload 'custom-make-dependencies "cus-edit")
38 (autoload 'customize-menu-create "cus-edit")
41 (autoload 'custom-declare-face "cus-face")
42 (autoload 'custom-set-faces "cus-face"))
44 (defvar custom-define-hook nil
45 ;; Customize information for this option is in `cus-edit.el'.
46 "Hook called after defining each customize option.")
48 ;;; The `defcustom' Macro.
50 (defun custom-declare-variable (symbol value doc &rest args)
51 "Like `defcustom', but SYMBOL and VALUE are evaluated as normal arguments."
52 ;; Bind this variable unless it already is bound.
53 (unless (default-boundp symbol)
54 ;; Use the saved value if it exists, otherwise the factory setting.
55 (set-default symbol (if (get symbol 'saved-value)
56 (eval (car (get symbol 'saved-value)))
58 ;; Remember the factory setting.
59 (put symbol 'factory-value (list value))
60 ;; Maybe this option was rogue in an earlier version. It no longer is.
61 (when (get symbol 'force-value)
63 (put symbol 'force-value nil))
65 (put symbol 'variable-documentation doc))
67 (let ((arg (car args)))
68 (setq args (cdr args))
70 (error "Junk in args %S" args))
74 (error "Keyword %s is missing an argument" keyword))
75 (setq args (cdr args))
76 (cond ((eq keyword :type)
77 (put symbol 'custom-type value))
78 ((eq keyword :options)
79 (if (get symbol 'custom-options)
80 ;; Slow safe code to avoid duplicates.
81 (mapcar (lambda (option)
82 (custom-add-option symbol option))
84 ;; Fast code for the common case.
85 (put symbol 'custom-options (copy-list value))))
87 (custom-handle-keyword symbol keyword value
88 'custom-variable))))))
89 (run-hooks 'custom-define-hook)
92 (defmacro defcustom (symbol value doc &rest args)
93 "Declare SYMBOL as a customizable variable that defaults to VALUE.
94 DOC is the variable documentation.
96 Neither SYMBOL nor VALUE needs to be quoted.
97 If SYMBOL is not already bound, initialize it to VALUE.
98 The remaining arguments should have the form
102 The following KEYWORD's are defined:
104 :type VALUE should be a widget type.
105 :options VALUE should be a list of valid members of the widget type.
106 :group VALUE should be a customization group.
107 Add SYMBOL to that group.
109 Read the section about customization in the emacs lisp manual for more
112 (custom-declare-variable (quote ,symbol) (quote ,value) ,doc ,@args)))
114 ;;; The `defface' Macro.
116 (defmacro defface (face spec doc &rest args)
117 "Declare FACE as a customizable face that defaults to SPEC.
118 FACE does not need to be quoted.
120 Third argument DOC is the face documentation.
122 If FACE has been set with `custom-set-face', set the face attributes
123 as specified by that function, otherwise set the face attributes
126 The remaining arguments should have the form
130 The following KEYWORD's are defined:
132 :group VALUE should be a customization group.
133 Add FACE to that group.
135 SPEC should be an alist of the form ((DISPLAY ATTS)...).
137 ATTS is a list of face attributes and their values. The possible
138 attributes are defined in the variable `custom-face-attributes'.
139 Alternatively, ATTS can be a face in which case the attributes of that
142 The ATTS of the first entry in SPEC where the DISPLAY matches the
143 frame should take effect in that frame. DISPLAY can either be the
144 symbol t, which will match all frames, or an alist of the form
147 For the DISPLAY to match a FRAME, the REQ property of the frame must
148 match one of the ITEM. The following REQ are defined:
150 `type' (the value of `window-system')
151 Should be one of `x' or `tty'.
153 `class' (the frame's color support)
154 Should be one of `color', `grayscale', or `mono'.
156 `background' (what color is used for the background text)
157 Should be one of `light' or `dark'.
159 Read the section about customization in the emacs lisp manual for more
161 `(custom-declare-face (quote ,face) ,spec ,doc ,@args))
163 ;;; The `defgroup' Macro.
165 (defun custom-declare-group (symbol members doc &rest args)
166 "Like `defgroup', but SYMBOL is evaluated as a normal argument."
167 (put symbol 'custom-group (nconc members (get symbol 'custom-group)))
169 (put symbol 'group-documentation doc))
171 (let ((arg (car args)))
172 (setq args (cdr args))
173 (unless (symbolp arg)
174 (error "Junk in args %S" args))
178 (error "Keyword %s is missing an argument" keyword))
179 (setq args (cdr args))
180 (cond ((eq keyword :prefix)
181 (put symbol 'custom-prefix value))
183 (custom-handle-keyword symbol keyword value
185 (run-hooks 'custom-define-hook)
188 (defmacro defgroup (symbol members doc &rest args)
189 "Declare SYMBOL as a customization group containing MEMBERS.
190 SYMBOL does not need to be quoted.
192 Third arg DOC is the group documentation.
194 MEMBERS should be an alist of the form ((NAME WIDGET)...) where
195 NAME is a symbol and WIDGET is a widget is a widget for editing that
196 symbol. Useful widgets are `custom-variable' for editing variables,
197 `custom-face' for edit faces, and `custom-group' for editing groups.
199 The remaining arguments should have the form
203 The following KEYWORD's are defined:
205 :group VALUE should be a customization group.
206 Add SYMBOL to that group.
208 Read the section about customization in the emacs lisp manual for more
210 `(custom-declare-group (quote ,symbol) ,members ,doc ,@args))
212 (defun custom-add-to-group (group option widget)
213 "To existing GROUP add a new OPTION of type WIDGET.
214 If there already is an entry for that option, overwrite it."
215 (let* ((members (get group 'custom-group))
216 (old (assq option members)))
218 (setcar (cdr old) widget)
219 (put group 'custom-group (nconc members (list (list option widget)))))))
223 (defun custom-handle-all-keywords (symbol args type)
224 "For customization option SYMBOL, handle keyword arguments ARGS.
225 Third argument TYPE is the custom option type."
227 (let ((arg (car args)))
228 (setq args (cdr args))
229 (unless (symbolp arg)
230 (error "Junk in args %S" args))
234 (error "Keyword %s is missing an argument" keyword))
235 (setq args (cdr args))
236 (custom-handle-keyword symbol keyword value type)))))
238 (defun custom-handle-keyword (symbol keyword value type)
239 "For customization option SYMBOL, handle KEYWORD with VALUE.
240 Fourth argument TYPE is the custom option type."
241 (cond ((eq keyword :group)
242 (custom-add-to-group value symbol type))
244 (custom-add-link symbol value))
246 (custom-add-load symbol value))
248 (put symbol 'custom-tag value))
250 (error "Unknown keyword %s" symbol))))
252 (defun custom-add-option (symbol option)
253 "To the variable SYMBOL add OPTION.
255 If SYMBOL is a hook variable, OPTION should be a hook member.
256 For other types variables, the effect is undefined."
257 (let ((options (get symbol 'custom-options)))
258 (unless (member option options)
259 (put symbol 'custom-options (cons option options)))))
261 (defun custom-add-link (symbol widget)
262 "To the custom option SYMBOL add the link WIDGET."
263 (let ((links (get symbol 'custom-links)))
264 (unless (member widget links)
265 (put symbol 'custom-links (cons widget links)))))
267 (defun custom-add-load (symbol load)
268 "To the custom option SYMBOL add the dependency LOAD.
269 LOAD should be either a library file name, or a feature name."
270 (let ((loads (get symbol 'custom-loads)))
271 (unless (member load loads)
272 (put symbol 'custom-loads (cons load loads)))))
276 (defun custom-set-variables (&rest args)
277 "Initialize variables according to user preferences.
279 The arguments should be a list where each entry has the form:
283 The unevaluated VALUE is stored as the saved value for SYMBOL.
284 If NOW is present and non-nil, VALUE is also evaluated and bound as
285 the default value for the SYMBOL."
287 (let ((entry (car args)))
289 (let ((symbol (nth 0 entry))
290 (value (nth 1 entry))
292 (put symbol 'saved-value (list value))
294 ;; Rogue variable, set it now.
295 (put symbol 'force-value t)
296 (set-default symbol (eval value)))
297 ((default-boundp symbol)
298 ;; Something already set this, overwrite it.
299 (set-default symbol (eval value))))
300 (setq args (cdr args)))
301 ;; Old format, a plist of SYMBOL VALUE pairs.
302 (let ((symbol (nth 0 args))
303 (value (nth 1 args)))
304 (put symbol 'saved-value (list value)))
305 (setq args (cdr (cdr args)))))))
311 ;; custom.el ends here