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/
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)
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.
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
35 ;; The code implementing face declarations is in `cus-face.el'
41 (define-widget-keywords :prefix :tag :load :link :options :type :group)
43 (defvar custom-define-hook nil
44 ;; Customize information for this option is in `cus-edit.el'.
45 "Hook called after defining each customize option.")
47 ;;; The `defcustom' Macro.
49 (defun custom-declare-variable (symbol value doc &rest args)
50 "Like `defcustom', but SYMBOL and VALUE are evaluated as normal arguments."
51 ;; Bind this variable unless it already is bound.
52 (unless (default-boundp symbol)
53 ;; Use the saved value if it exists, otherwise the factory setting.
54 (set-default symbol (if (get symbol 'saved-value)
55 (eval (car (get symbol 'saved-value)))
57 ;; Remember the factory setting.
58 (put symbol 'factory-value (list value))
59 ;; Maybe this option was rogue in an earlier version. It no longer is.
60 (when (get symbol 'force-value)
62 (put symbol 'force-value nil))
64 (put symbol 'variable-documentation doc))
66 (let ((arg (car args)))
67 (setq args (cdr args))
69 (error "Junk in args %S" args))
73 (error "Keyword %s is missing an argument" keyword))
74 (setq args (cdr args))
75 (cond ((eq keyword :type)
76 (put symbol 'custom-type value))
77 ((eq keyword :options)
78 (if (get symbol 'custom-options)
79 ;; Slow safe code to avoid duplicates.
80 (mapcar (lambda (option)
81 (custom-add-option symbol option))
83 ;; Fast code for the common case.
84 (put symbol 'custom-options (copy-sequence value))))
86 (custom-handle-keyword symbol keyword value
87 'custom-variable))))))
88 (run-hooks 'custom-define-hook)
91 (defmacro defcustom (symbol value doc &rest args)
92 "Declare SYMBOL as a customizable variable that defaults to VALUE.
93 DOC is the variable documentation.
95 Neither SYMBOL nor VALUE needs to be quoted.
96 If SYMBOL is not already bound, initialize it to VALUE.
97 The remaining arguments should have the form
101 The following KEYWORD's are defined:
103 :type VALUE should be a widget type.
104 :options VALUE should be a list of valid members of the widget type.
105 :group VALUE should be a customization group.
106 Add SYMBOL to that group.
108 Read the section about customization in the Emacs Lisp manual for more
111 (custom-declare-variable (quote ,symbol) (quote ,value) ,doc ,@args)))
113 ;;; The `defface' Macro.
115 (defmacro defface (face spec doc &rest args)
116 "Declare FACE as a customizable face that defaults to SPEC.
117 FACE does not need to be quoted.
119 Third argument DOC is the face documentation.
121 If FACE has been set with `custom-set-face', set the face attributes
122 as specified by that function, otherwise set the face attributes
125 The remaining arguments should have the form
129 The following KEYWORD's are defined:
131 :group VALUE should be a customization group.
132 Add FACE to that group.
134 SPEC should be an alist of the form ((DISPLAY ATTS)...).
136 ATTS is a list of face attributes and their values. The possible
137 attributes are defined in the variable `custom-face-attributes'.
138 Alternatively, ATTS can be a face in which case the attributes of that
141 The ATTS of the first entry in SPEC where the DISPLAY matches the
142 frame should take effect in that frame. DISPLAY can either be the
143 symbol t, which will match all frames, or an alist of the form
146 For the DISPLAY to match a FRAME, the REQ property of the frame must
147 match one of the ITEM. The following REQ are defined:
149 `type' (the value of `window-system')
150 Should be one of `x' or `tty'.
152 `class' (the frame's color support)
153 Should be one of `color', `grayscale', or `mono'.
155 `background' (what color is used for the background text)
156 Should be one of `light' or `dark'.
158 Read the section about customization in the Emacs Lisp manual for more
160 `(custom-declare-face (quote ,face) ,spec ,doc ,@args))
162 ;;; The `defgroup' Macro.
164 (defun custom-declare-group (symbol members doc &rest args)
165 "Like `defgroup', but SYMBOL is evaluated as a normal argument."
166 (put symbol 'custom-group (nconc members (get symbol 'custom-group)))
168 (put symbol 'group-documentation doc))
170 (let ((arg (car args)))
171 (setq args (cdr args))
172 (unless (symbolp arg)
173 (error "Junk in args %S" args))
177 (error "Keyword %s is missing an argument" keyword))
178 (setq args (cdr args))
179 (cond ((eq keyword :prefix)
180 (put symbol 'custom-prefix value))
182 (custom-handle-keyword symbol keyword value
184 (run-hooks 'custom-define-hook)
187 (defmacro defgroup (symbol members doc &rest args)
188 "Declare SYMBOL as a customization group containing MEMBERS.
189 SYMBOL does not need to be quoted.
191 Third arg DOC is the group documentation.
193 MEMBERS should be an alist of the form ((NAME WIDGET)...) where
194 NAME is a symbol and WIDGET is a widget is a widget for editing that
195 symbol. Useful widgets are `custom-variable' for editing variables,
196 `custom-face' for edit faces, and `custom-group' for editing groups.
198 The remaining arguments should have the form
202 The following KEYWORD's are defined:
204 :group VALUE should be a customization group.
205 Add SYMBOL to that group.
207 Read the section about customization in the Emacs Lisp manual for more
209 `(custom-declare-group (quote ,symbol) ,members ,doc ,@args))
211 (defun custom-add-to-group (group option widget)
212 "To existing GROUP add a new OPTION of type WIDGET.
213 If there already is an entry for that option, overwrite it."
214 (let* ((members (get group 'custom-group))
215 (old (assq option members)))
217 (setcar (cdr old) widget)
218 (put group 'custom-group (nconc members (list (list option widget)))))))
222 (defun custom-handle-all-keywords (symbol args type)
223 "For customization option SYMBOL, handle keyword arguments ARGS.
224 Third argument TYPE is the custom option type."
226 (let ((arg (car args)))
227 (setq args (cdr args))
228 (unless (symbolp arg)
229 (error "Junk in args %S" args))
233 (error "Keyword %s is missing an argument" keyword))
234 (setq args (cdr args))
235 (custom-handle-keyword symbol keyword value type)))))
237 (defun custom-handle-keyword (symbol keyword value type)
238 "For customization option SYMBOL, handle KEYWORD with VALUE.
239 Fourth argument TYPE is the custom option type."
240 (cond ((eq keyword :group)
241 (custom-add-to-group value symbol type))
243 (custom-add-link symbol value))
245 (custom-add-load symbol value))
247 (put symbol 'custom-tag value))
249 (error "Unknown keyword %s" symbol))))
251 (defun custom-add-option (symbol option)
252 "To the variable SYMBOL add OPTION.
254 If SYMBOL is a hook variable, OPTION should be a hook member.
255 For other types variables, the effect is undefined."
256 (let ((options (get symbol 'custom-options)))
257 (unless (member option options)
258 (put symbol 'custom-options (cons option options)))))
260 (defun custom-add-link (symbol widget)
261 "To the custom option SYMBOL add the link WIDGET."
262 (let ((links (get symbol 'custom-links)))
263 (unless (member widget links)
264 (put symbol 'custom-links (cons widget links)))))
266 (defun custom-add-load (symbol load)
267 "To the custom option SYMBOL add the dependency LOAD.
268 LOAD should be either a library file name, or a feature name."
269 (let ((loads (get symbol 'custom-loads)))
270 (unless (member load loads)
271 (put symbol 'custom-loads (cons load loads)))))
275 (defun custom-set-variables (&rest args)
276 "Initialize variables according to user preferences.
278 The arguments should be a list where each entry has the form:
282 The unevaluated VALUE is stored as the saved value for SYMBOL.
283 If NOW is present and non-nil, VALUE is also evaluated and bound as
284 the default value for the SYMBOL."
286 (let ((entry (car args)))
288 (let ((symbol (nth 0 entry))
289 (value (nth 1 entry))
291 (put symbol 'saved-value (list value))
293 ;; Rogue variable, set it now.
294 (put symbol 'force-value t)
295 (set-default symbol (eval value)))
296 ((default-boundp symbol)
297 ;; Something already set this, overwrite it.
298 (set-default symbol (eval value))))
299 (setq args (cdr args)))
300 ;; Old format, a plist of SYMBOL VALUE pairs.
301 (message "Warning: old format `custom-set-variables'")
304 (let ((symbol (nth 0 args))
305 (value (nth 1 args)))
306 (put symbol 'saved-value (list value)))
307 (setq args (cdr (cdr args)))))))
313 ;; custom.el ends here