98
|
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
|
120
|
7 ;; Version: 1.74
|
98
|
8 ;; X-URL: http://www.dina.kvl.dk/~abraham/custom/
|
|
9
|
|
10 ;;; Commentary:
|
|
11 ;;
|
|
12 ;; If you want to use this code, please visit the URL above.
|
|
13 ;;
|
|
14 ;; This file only contain the code needed to declare and initialize
|
|
15 ;; user options. The code to customize options is autoloaded from
|
106
|
16 ;; `cus-edit.el'.
|
|
17
|
|
18 ;; The code implementing face declarations is in `cus-face.el'
|
98
|
19
|
|
20 ;;; Code:
|
|
21
|
|
22 (require 'widget)
|
|
23
|
|
24 (define-widget-keywords :prefix :tag :load :link :options :type :group)
|
|
25
|
120
|
26 (defvar custom-define-hook nil
|
|
27 "Hook called after defining each customize option.")
|
|
28
|
98
|
29 ;; These autoloads should be deleted when the file is added to Emacs
|
|
30
|
|
31 (unless (fboundp 'load-gc)
|
106
|
32 ;; From cus-edit.el
|
|
33 (autoload 'customize "cus-edit" nil t)
|
|
34 (autoload 'customize-variable "cus-edit" nil t)
|
|
35 (autoload 'customize-face "cus-edit" nil t)
|
|
36 (autoload 'customize-apropos "cus-edit" nil t)
|
|
37 (autoload 'customize-customized "cus-edit" nil t)
|
|
38 (autoload 'custom-buffer-create "cus-edit")
|
|
39 (autoload 'custom-make-dependencies "cus-edit")
|
|
40 ;; From cus-face.el
|
|
41 (autoload 'custom-declare-face "cus-face")
|
|
42 (autoload 'custom-set-faces "cus-face"))
|
98
|
43
|
|
44 ;;; The `defcustom' Macro.
|
|
45
|
|
46 (defun custom-declare-variable (symbol value doc &rest args)
|
100
|
47 "Like `defcustom', but SYMBOL and VALUE are evaluated as normal arguments."
|
120
|
48 ;; Bind this variable unless it already is bound.
|
|
49 (unless (default-boundp symbol)
|
|
50 ;; Use the saved value if it exists, otherwise the factory setting.
|
98
|
51 (set-default symbol (if (get symbol 'saved-value)
|
|
52 (eval (car (get symbol 'saved-value)))
|
|
53 (eval value))))
|
120
|
54 ;; Remember the factory setting.
|
98
|
55 (put symbol 'factory-value (list value))
|
120
|
56 ;; Maybe this option was rogue in an earlier version. It no longer is.
|
|
57 (when (get symbol 'force-value)
|
|
58 ;; It no longer is.
|
|
59 (put symbol 'force-value nil))
|
98
|
60 (when doc
|
|
61 (put symbol 'variable-documentation doc))
|
|
62 (while args
|
|
63 (let ((arg (car args)))
|
|
64 (setq args (cdr args))
|
|
65 (unless (symbolp arg)
|
|
66 (error "Junk in args %S" args))
|
|
67 (let ((keyword arg)
|
|
68 (value (car args)))
|
|
69 (unless args
|
|
70 (error "Keyword %s is missing an argument" keyword))
|
|
71 (setq args (cdr args))
|
|
72 (cond ((eq keyword :type)
|
|
73 (put symbol 'custom-type value))
|
|
74 ((eq keyword :options)
|
|
75 (if (get symbol 'custom-options)
|
|
76 ;; Slow safe code to avoid duplicates.
|
|
77 (mapcar (lambda (option)
|
|
78 (custom-add-option symbol option))
|
|
79 value)
|
|
80 ;; Fast code for the common case.
|
|
81 (put symbol 'custom-options (copy-list value))))
|
|
82 (t
|
|
83 (custom-handle-keyword symbol keyword value
|
|
84 'custom-variable))))))
|
|
85 (run-hooks 'custom-define-hook)
|
|
86 symbol)
|
|
87
|
|
88 (defmacro defcustom (symbol value doc &rest args)
|
|
89 "Declare SYMBOL as a customizable variable that defaults to VALUE.
|
|
90 DOC is the variable documentation.
|
|
91
|
|
92 Neither SYMBOL nor VALUE needs to be quoted.
|
|
93 If SYMBOL is not already bound, initialize it to VALUE.
|
|
94 The remaining arguments should have the form
|
|
95
|
|
96 [KEYWORD VALUE]...
|
|
97
|
|
98 The following KEYWORD's are defined:
|
|
99
|
|
100 :type VALUE should be a widget type.
|
|
101 :options VALUE should be a list of valid members of the widget type.
|
|
102 :group VALUE should be a customization group.
|
|
103 Add SYMBOL to that group.
|
|
104
|
|
105 Read the section about customization in the emacs lisp manual for more
|
|
106 information."
|
|
107 `(eval-and-compile
|
|
108 (custom-declare-variable (quote ,symbol) (quote ,value) ,doc ,@args)))
|
|
109
|
|
110 ;;; The `defface' Macro.
|
|
111
|
|
112 (defmacro defface (face spec doc &rest args)
|
|
113 "Declare FACE as a customizable face that defaults to SPEC.
|
|
114 FACE does not need to be quoted.
|
|
115
|
|
116 Third argument DOC is the face documentation.
|
|
117
|
|
118 If FACE has been set with `custom-set-face', set the face attributes
|
|
119 as specified by that function, otherwise set the face attributes
|
|
120 according to SPEC.
|
|
121
|
|
122 The remaining arguments should have the form
|
|
123
|
|
124 [KEYWORD VALUE]...
|
|
125
|
|
126 The following KEYWORD's are defined:
|
|
127
|
|
128 :group VALUE should be a customization group.
|
|
129 Add FACE to that group.
|
|
130
|
|
131 SPEC should be an alist of the form ((DISPLAY ATTS)...).
|
|
132
|
|
133 ATTS is a list of face attributes and their values. The possible
|
|
134 attributes are defined in the variable `custom-face-attributes'.
|
|
135 Alternatively, ATTS can be a face in which case the attributes of that
|
|
136 face is used.
|
|
137
|
|
138 The ATTS of the first entry in SPEC where the DISPLAY matches the
|
|
139 frame should take effect in that frame. DISPLAY can either be the
|
118
|
140 symbol t, which will match all frames, or an alist of the form
|
98
|
141 \((REQ ITEM...)...)
|
|
142
|
|
143 For the DISPLAY to match a FRAME, the REQ property of the frame must
|
|
144 match one of the ITEM. The following REQ are defined:
|
|
145
|
118
|
146 `type' (the value of `window-system')
|
98
|
147 Should be one of `x' or `tty'.
|
|
148
|
|
149 `class' (the frame's color support)
|
|
150 Should be one of `color', `grayscale', or `mono'.
|
|
151
|
|
152 `background' (what color is used for the background text)
|
|
153 Should be one of `light' or `dark'.
|
|
154
|
|
155 Read the section about customization in the emacs lisp manual for more
|
|
156 information."
|
|
157 `(custom-declare-face (quote ,face) ,spec ,doc ,@args))
|
|
158
|
|
159 ;;; The `defgroup' Macro.
|
|
160
|
|
161 (defun custom-declare-group (symbol members doc &rest args)
|
|
162 "Like `defgroup', but SYMBOL is evaluated as a normal argument."
|
|
163 (put symbol 'custom-group (nconc members (get symbol 'custom-group)))
|
|
164 (when doc
|
|
165 (put symbol 'group-documentation doc))
|
|
166 (while args
|
|
167 (let ((arg (car args)))
|
|
168 (setq args (cdr args))
|
|
169 (unless (symbolp arg)
|
|
170 (error "Junk in args %S" args))
|
|
171 (let ((keyword arg)
|
|
172 (value (car args)))
|
|
173 (unless args
|
|
174 (error "Keyword %s is missing an argument" keyword))
|
|
175 (setq args (cdr args))
|
|
176 (cond ((eq keyword :prefix)
|
|
177 (put symbol 'custom-prefix value))
|
|
178 (t
|
|
179 (custom-handle-keyword symbol keyword value
|
|
180 'custom-group))))))
|
|
181 (run-hooks 'custom-define-hook)
|
|
182 symbol)
|
|
183
|
|
184 (defmacro defgroup (symbol members doc &rest args)
|
|
185 "Declare SYMBOL as a customization group containing MEMBERS.
|
|
186 SYMBOL does not need to be quoted.
|
|
187
|
|
188 Third arg DOC is the group documentation.
|
|
189
|
|
190 MEMBERS should be an alist of the form ((NAME WIDGET)...) where
|
|
191 NAME is a symbol and WIDGET is a widget is a widget for editing that
|
|
192 symbol. Useful widgets are `custom-variable' for editing variables,
|
|
193 `custom-face' for edit faces, and `custom-group' for editing groups.
|
|
194
|
|
195 The remaining arguments should have the form
|
|
196
|
|
197 [KEYWORD VALUE]...
|
|
198
|
|
199 The following KEYWORD's are defined:
|
|
200
|
|
201 :group VALUE should be a customization group.
|
|
202 Add SYMBOL to that group.
|
|
203
|
|
204 Read the section about customization in the emacs lisp manual for more
|
|
205 information."
|
|
206 `(custom-declare-group (quote ,symbol) ,members ,doc ,@args))
|
|
207
|
|
208 (defun custom-add-to-group (group option widget)
|
118
|
209 "To existing GROUP add a new OPTION of type WIDGET.
|
98
|
210 If there already is an entry for that option, overwrite it."
|
|
211 (let* ((members (get group 'custom-group))
|
|
212 (old (assq option members)))
|
|
213 (if old
|
|
214 (setcar (cdr old) widget)
|
|
215 (put group 'custom-group (nconc members (list (list option widget)))))))
|
|
216
|
|
217 ;;; Properties.
|
|
218
|
|
219 (defun custom-handle-all-keywords (symbol args type)
|
|
220 "For customization option SYMBOL, handle keyword arguments ARGS.
|
|
221 Third argument TYPE is the custom option type."
|
|
222 (while args
|
|
223 (let ((arg (car args)))
|
|
224 (setq args (cdr args))
|
|
225 (unless (symbolp arg)
|
|
226 (error "Junk in args %S" args))
|
|
227 (let ((keyword arg)
|
|
228 (value (car args)))
|
|
229 (unless args
|
|
230 (error "Keyword %s is missing an argument" keyword))
|
|
231 (setq args (cdr args))
|
|
232 (custom-handle-keyword symbol keyword value type)))))
|
|
233
|
|
234 (defun custom-handle-keyword (symbol keyword value type)
|
|
235 "For customization option SYMBOL, handle KEYWORD with VALUE.
|
|
236 Fourth argument TYPE is the custom option type."
|
|
237 (cond ((eq keyword :group)
|
|
238 (custom-add-to-group value symbol type))
|
|
239 ((eq keyword :link)
|
|
240 (custom-add-link symbol value))
|
|
241 ((eq keyword :load)
|
|
242 (custom-add-load symbol value))
|
|
243 ((eq keyword :tag)
|
|
244 (put symbol 'custom-tag value))
|
|
245 (t
|
|
246 (error "Unknown keyword %s" symbol))))
|
|
247
|
|
248 (defun custom-add-option (symbol option)
|
|
249 "To the variable SYMBOL add OPTION.
|
|
250
|
|
251 If SYMBOL is a hook variable, OPTION should be a hook member.
|
|
252 For other types variables, the effect is undefined."
|
|
253 (let ((options (get symbol 'custom-options)))
|
|
254 (unless (member option options)
|
|
255 (put symbol 'custom-options (cons option options)))))
|
|
256
|
|
257 (defun custom-add-link (symbol widget)
|
|
258 "To the custom option SYMBOL add the link WIDGET."
|
|
259 (let ((links (get symbol 'custom-links)))
|
|
260 (unless (member widget links)
|
|
261 (put symbol 'custom-links (cons widget links)))))
|
|
262
|
|
263 (defun custom-add-load (symbol load)
|
|
264 "To the custom option SYMBOL add the dependency LOAD.
|
|
265 LOAD should be either a library file name, or a feature name."
|
|
266 (let ((loads (get symbol 'custom-loads)))
|
|
267 (unless (member load loads)
|
|
268 (put symbol 'custom-loads (cons load loads)))))
|
|
269
|
|
270 ;;; Initializing.
|
|
271
|
|
272 (defun custom-set-variables (&rest args)
|
|
273 "Initialize variables according to user preferences.
|
|
274
|
|
275 The arguments should be a list where each entry has the form:
|
|
276
|
|
277 (SYMBOL VALUE [NOW])
|
|
278
|
|
279 The unevaluated VALUE is stored as the saved value for SYMBOL.
|
|
280 If NOW is present and non-nil, VALUE is also evaluated and bound as
|
|
281 the default value for the SYMBOL."
|
|
282 (while args
|
|
283 (let ((entry (car args)))
|
|
284 (if (listp entry)
|
|
285 (let ((symbol (nth 0 entry))
|
|
286 (value (nth 1 entry))
|
|
287 (now (nth 2 entry)))
|
|
288 (put symbol 'saved-value (list value))
|
120
|
289 (cond (now
|
|
290 ;; Rogue variable, set it now.
|
|
291 (put symbol 'force-value t)
|
|
292 (set-default symbol (eval value)))
|
|
293 ((default-boundp symbol)
|
|
294 ;; Something already set this, overwrite it.
|
|
295 (set-default symbol (eval value))))
|
98
|
296 (setq args (cdr args)))
|
|
297 ;; Old format, a plist of SYMBOL VALUE pairs.
|
|
298 (let ((symbol (nth 0 args))
|
|
299 (value (nth 1 args)))
|
|
300 (put symbol 'saved-value (list value)))
|
|
301 (setq args (cdr (cdr args)))))))
|
|
302
|
|
303 ;;; The End.
|
|
304
|
|
305 (provide 'custom)
|
|
306
|
|
307 ;; custom.el ends here
|