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
|
134
|
7 ;; Version: 1.89
|
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
|
134
|
24 (define-widget-keywords :initialize :set :get :require :prefix :tag
|
|
25 :load :link :options :type :group)
|
98
|
26
|
124
|
27 ;; These autoloads should be deleted eventually.
|
98
|
28 (unless (fboundp 'load-gc)
|
106
|
29 ;; From cus-edit.el
|
|
30 (autoload 'customize "cus-edit" nil t)
|
134
|
31 (autoload 'customize-other-window "cus-edit" nil t)
|
106
|
32 (autoload 'customize-variable "cus-edit" nil t)
|
124
|
33 (autoload 'customize-variable-other-window "cus-edit" nil t)
|
106
|
34 (autoload 'customize-face "cus-edit" nil t)
|
124
|
35 (autoload 'customize-face-other-window "cus-edit" nil t)
|
106
|
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")
|
124
|
40 (autoload 'custom-menu-create "cus-edit")
|
|
41 (autoload 'customize-menu-create "cus-edit")
|
|
42
|
106
|
43 ;; From cus-face.el
|
|
44 (autoload 'custom-declare-face "cus-face")
|
|
45 (autoload 'custom-set-faces "cus-face"))
|
98
|
46
|
124
|
47 (defvar custom-define-hook nil
|
|
48 ;; Customize information for this option is in `cus-edit.el'.
|
|
49 "Hook called after defining each customize option.")
|
|
50
|
98
|
51 ;;; The `defcustom' Macro.
|
|
52
|
134
|
53 (defun custom-initialize-default (symbol value)
|
|
54 "Initialize SYMBOL with VALUE.
|
|
55 This will do nothing if symbol already has a default binding.
|
|
56 Otherwise, if symbol has a `saved-value' property, it will evaluate
|
|
57 the car of that and used as the default binding for symbol.
|
|
58 Otherwise, VALUE will be evaluated and used as the default binding for
|
|
59 symbol."
|
120
|
60 (unless (default-boundp symbol)
|
|
61 ;; Use the saved value if it exists, otherwise the factory setting.
|
98
|
62 (set-default symbol (if (get symbol 'saved-value)
|
|
63 (eval (car (get symbol 'saved-value)))
|
134
|
64 (eval value)))))
|
|
65
|
|
66 (defun custom-initialize-set (symbol value)
|
|
67 "Initialize SYMBOL with VALUE.
|
|
68 Like `custom-initialize-default', but use the function specified by
|
|
69 `:set' to initialize SYMBOL."
|
|
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)))))
|
|
76
|
|
77 (defun custom-initialize-reset (symbol value)
|
|
78 "Initialize SYMBOL with VALUE.
|
|
79 Like `custom-initialize-set', but use the function specified by
|
|
80 `:get' to reinitialize SYMBOL if it is already bound."
|
|
81 (funcall (or (get symbol 'custom-set) 'set-default)
|
|
82 symbol
|
|
83 (cond ((default-boundp symbol)
|
|
84 (funcall (or (get symbol 'custom-get) 'default-value)
|
|
85 symbol))
|
|
86 ((get symbol 'saved-value)
|
|
87 (eval (car (get symbol 'saved-value))))
|
|
88 (t
|
|
89 (eval value)))))
|
|
90
|
|
91 (defun custom-initialize-changed (symbol value)
|
|
92 "Initialize SYMBOL with VALUE.
|
|
93 Like `custom-initialize-reset', but only use the `:set' function if the
|
|
94 not using the factory setting. Otherwise, use the `set-default'."
|
|
95 (cond ((default-boundp symbol)
|
|
96 (funcall (or (get symbol 'custom-set) 'set-default)
|
|
97 symbol
|
|
98 (funcall (or (get symbol 'custom-get) 'default-value)
|
|
99 symbol)))
|
|
100 ((get symbol 'saved-value)
|
|
101 (funcall (or (get symbol 'custom-set) 'set-default)
|
|
102 symbol
|
|
103 (eval (car (get symbol 'saved-value)))))
|
|
104 (t
|
|
105 (set-default symbol (eval value)))))
|
|
106
|
|
107 (defun custom-declare-variable (symbol value doc &rest args)
|
|
108 "Like `defcustom', but SYMBOL and VALUE are evaluated as normal arguments."
|
120
|
109 ;; Remember the factory setting.
|
98
|
110 (put symbol 'factory-value (list value))
|
120
|
111 ;; Maybe this option was rogue in an earlier version. It no longer is.
|
|
112 (when (get symbol 'force-value)
|
|
113 ;; It no longer is.
|
|
114 (put symbol 'force-value nil))
|
98
|
115 (when doc
|
|
116 (put symbol 'variable-documentation doc))
|
134
|
117 (let ((initialize 'custom-initialize-set)
|
|
118 (requests nil))
|
|
119 (while args
|
|
120 (let ((arg (car args)))
|
98
|
121 (setq args (cdr args))
|
134
|
122 (unless (symbolp arg)
|
|
123 (error "Junk in args %S" args))
|
|
124 (let ((keyword arg)
|
|
125 (value (car args)))
|
|
126 (unless args
|
|
127 (error "Keyword %s is missing an argument" keyword))
|
|
128 (setq args (cdr args))
|
|
129 (cond ((eq keyword :initialize)
|
|
130 (setq initialize value))
|
|
131 ((eq keyword :set)
|
|
132 (put symbol 'custom-set value))
|
|
133 ((eq keyword :get)
|
|
134 (put symbol 'custom-get value))
|
|
135 ((eq keyword :require)
|
|
136 (push value requests))
|
|
137 ((eq keyword :type)
|
|
138 (put symbol 'custom-type value))
|
|
139 ((eq keyword :options)
|
|
140 (if (get symbol 'custom-options)
|
|
141 ;; Slow safe code to avoid duplicates.
|
|
142 (mapcar (lambda (option)
|
|
143 (custom-add-option symbol option))
|
|
144 value)
|
|
145 ;; Fast code for the common case.
|
|
146 (put symbol 'custom-options (append value nil))))
|
|
147 (t
|
|
148 (custom-handle-keyword symbol keyword value
|
|
149 'custom-variable))))))
|
|
150 (put symbol 'custom-requests requests)
|
|
151 ;; Do the actual initialization.
|
|
152 (funcall initialize symbol value))
|
98
|
153 (run-hooks 'custom-define-hook)
|
|
154 symbol)
|
|
155
|
|
156 (defmacro defcustom (symbol value doc &rest args)
|
|
157 "Declare SYMBOL as a customizable variable that defaults to VALUE.
|
|
158 DOC is the variable documentation.
|
|
159
|
|
160 Neither SYMBOL nor VALUE needs to be quoted.
|
|
161 If SYMBOL is not already bound, initialize it to VALUE.
|
|
162 The remaining arguments should have the form
|
|
163
|
|
164 [KEYWORD VALUE]...
|
|
165
|
|
166 The following KEYWORD's are defined:
|
|
167
|
134
|
168 :type VALUE should be a widget type for editing the symbols value.
|
|
169 The default is `sexp'.
|
98
|
170 :options VALUE should be a list of valid members of the widget type.
|
|
171 :group VALUE should be a customization group.
|
|
172 Add SYMBOL to that group.
|
134
|
173 :initialize VALUE should be a function used to initialize the
|
|
174 variable. It takes two arguments, the symbol and value
|
|
175 given in the `defcustom' call. The default is
|
|
176 `custom-initialize-default'
|
|
177 :set VALUE should be a function to set the value of the symbol.
|
|
178 It takes two arguments, the symbol to set and the value to
|
|
179 give it. The default is `set-default'.
|
|
180 :get VALUE should be a function to extract the value of symbol.
|
|
181 The function takes one argument, a symbol, and should return
|
|
182 the current value for that symbol. The default is
|
|
183 `default-value'.
|
|
184 :require VALUE should be a feature symbol. Each feature will be
|
|
185 required after initialization, of the the user have saved this
|
|
186 option.
|
98
|
187
|
134
|
188 Read the section about customization in the Emacs Lisp manual for more
|
98
|
189 information."
|
|
190 `(eval-and-compile
|
|
191 (custom-declare-variable (quote ,symbol) (quote ,value) ,doc ,@args)))
|
|
192
|
|
193 ;;; The `defface' Macro.
|
|
194
|
|
195 (defmacro defface (face spec doc &rest args)
|
|
196 "Declare FACE as a customizable face that defaults to SPEC.
|
|
197 FACE does not need to be quoted.
|
|
198
|
|
199 Third argument DOC is the face documentation.
|
|
200
|
|
201 If FACE has been set with `custom-set-face', set the face attributes
|
|
202 as specified by that function, otherwise set the face attributes
|
|
203 according to SPEC.
|
|
204
|
|
205 The remaining arguments should have the form
|
|
206
|
|
207 [KEYWORD VALUE]...
|
|
208
|
|
209 The following KEYWORD's are defined:
|
|
210
|
|
211 :group VALUE should be a customization group.
|
|
212 Add FACE to that group.
|
|
213
|
|
214 SPEC should be an alist of the form ((DISPLAY ATTS)...).
|
|
215
|
|
216 ATTS is a list of face attributes and their values. The possible
|
|
217 attributes are defined in the variable `custom-face-attributes'.
|
|
218 Alternatively, ATTS can be a face in which case the attributes of that
|
|
219 face is used.
|
|
220
|
|
221 The ATTS of the first entry in SPEC where the DISPLAY matches the
|
|
222 frame should take effect in that frame. DISPLAY can either be the
|
118
|
223 symbol t, which will match all frames, or an alist of the form
|
98
|
224 \((REQ ITEM...)...)
|
|
225
|
|
226 For the DISPLAY to match a FRAME, the REQ property of the frame must
|
|
227 match one of the ITEM. The following REQ are defined:
|
|
228
|
118
|
229 `type' (the value of `window-system')
|
98
|
230 Should be one of `x' or `tty'.
|
|
231
|
|
232 `class' (the frame's color support)
|
|
233 Should be one of `color', `grayscale', or `mono'.
|
|
234
|
|
235 `background' (what color is used for the background text)
|
|
236 Should be one of `light' or `dark'.
|
|
237
|
134
|
238 Read the section about customization in the Emacs Lisp manual for more
|
98
|
239 information."
|
|
240 `(custom-declare-face (quote ,face) ,spec ,doc ,@args))
|
|
241
|
|
242 ;;; The `defgroup' Macro.
|
|
243
|
|
244 (defun custom-declare-group (symbol members doc &rest args)
|
|
245 "Like `defgroup', but SYMBOL is evaluated as a normal argument."
|
134
|
246 (while members
|
|
247 (apply 'custom-add-to-group symbol (car members))
|
|
248 (setq members (cdr members)))
|
98
|
249 (put symbol 'custom-group (nconc members (get symbol 'custom-group)))
|
|
250 (when doc
|
|
251 (put symbol 'group-documentation doc))
|
|
252 (while args
|
|
253 (let ((arg (car args)))
|
|
254 (setq args (cdr args))
|
|
255 (unless (symbolp arg)
|
|
256 (error "Junk in args %S" args))
|
|
257 (let ((keyword arg)
|
|
258 (value (car args)))
|
|
259 (unless args
|
|
260 (error "Keyword %s is missing an argument" keyword))
|
|
261 (setq args (cdr args))
|
|
262 (cond ((eq keyword :prefix)
|
|
263 (put symbol 'custom-prefix value))
|
|
264 (t
|
|
265 (custom-handle-keyword symbol keyword value
|
|
266 'custom-group))))))
|
|
267 (run-hooks 'custom-define-hook)
|
|
268 symbol)
|
|
269
|
|
270 (defmacro defgroup (symbol members doc &rest args)
|
|
271 "Declare SYMBOL as a customization group containing MEMBERS.
|
|
272 SYMBOL does not need to be quoted.
|
|
273
|
|
274 Third arg DOC is the group documentation.
|
|
275
|
|
276 MEMBERS should be an alist of the form ((NAME WIDGET)...) where
|
|
277 NAME is a symbol and WIDGET is a widget is a widget for editing that
|
|
278 symbol. Useful widgets are `custom-variable' for editing variables,
|
|
279 `custom-face' for edit faces, and `custom-group' for editing groups.
|
|
280
|
|
281 The remaining arguments should have the form
|
|
282
|
|
283 [KEYWORD VALUE]...
|
|
284
|
|
285 The following KEYWORD's are defined:
|
|
286
|
|
287 :group VALUE should be a customization group.
|
|
288 Add SYMBOL to that group.
|
|
289
|
134
|
290 Read the section about customization in the Emacs Lisp manual for more
|
98
|
291 information."
|
|
292 `(custom-declare-group (quote ,symbol) ,members ,doc ,@args))
|
|
293
|
|
294 (defun custom-add-to-group (group option widget)
|
118
|
295 "To existing GROUP add a new OPTION of type WIDGET.
|
98
|
296 If there already is an entry for that option, overwrite it."
|
|
297 (let* ((members (get group 'custom-group))
|
|
298 (old (assq option members)))
|
|
299 (if old
|
|
300 (setcar (cdr old) widget)
|
|
301 (put group 'custom-group (nconc members (list (list option widget)))))))
|
|
302
|
|
303 ;;; Properties.
|
|
304
|
|
305 (defun custom-handle-all-keywords (symbol args type)
|
|
306 "For customization option SYMBOL, handle keyword arguments ARGS.
|
|
307 Third argument TYPE is the custom option type."
|
|
308 (while args
|
|
309 (let ((arg (car args)))
|
|
310 (setq args (cdr args))
|
|
311 (unless (symbolp arg)
|
|
312 (error "Junk in args %S" args))
|
|
313 (let ((keyword arg)
|
|
314 (value (car args)))
|
|
315 (unless args
|
|
316 (error "Keyword %s is missing an argument" keyword))
|
|
317 (setq args (cdr args))
|
|
318 (custom-handle-keyword symbol keyword value type)))))
|
|
319
|
|
320 (defun custom-handle-keyword (symbol keyword value type)
|
|
321 "For customization option SYMBOL, handle KEYWORD with VALUE.
|
|
322 Fourth argument TYPE is the custom option type."
|
|
323 (cond ((eq keyword :group)
|
|
324 (custom-add-to-group value symbol type))
|
|
325 ((eq keyword :link)
|
|
326 (custom-add-link symbol value))
|
|
327 ((eq keyword :load)
|
|
328 (custom-add-load symbol value))
|
|
329 ((eq keyword :tag)
|
|
330 (put symbol 'custom-tag value))
|
|
331 (t
|
|
332 (error "Unknown keyword %s" symbol))))
|
|
333
|
|
334 (defun custom-add-option (symbol option)
|
|
335 "To the variable SYMBOL add OPTION.
|
|
336
|
|
337 If SYMBOL is a hook variable, OPTION should be a hook member.
|
|
338 For other types variables, the effect is undefined."
|
|
339 (let ((options (get symbol 'custom-options)))
|
|
340 (unless (member option options)
|
|
341 (put symbol 'custom-options (cons option options)))))
|
|
342
|
|
343 (defun custom-add-link (symbol widget)
|
|
344 "To the custom option SYMBOL add the link WIDGET."
|
|
345 (let ((links (get symbol 'custom-links)))
|
|
346 (unless (member widget links)
|
|
347 (put symbol 'custom-links (cons widget links)))))
|
|
348
|
|
349 (defun custom-add-load (symbol load)
|
|
350 "To the custom option SYMBOL add the dependency LOAD.
|
|
351 LOAD should be either a library file name, or a feature name."
|
|
352 (let ((loads (get symbol 'custom-loads)))
|
|
353 (unless (member load loads)
|
|
354 (put symbol 'custom-loads (cons load loads)))))
|
|
355
|
|
356 ;;; Initializing.
|
|
357
|
|
358 (defun custom-set-variables (&rest args)
|
|
359 "Initialize variables according to user preferences.
|
|
360
|
|
361 The arguments should be a list where each entry has the form:
|
|
362
|
|
363 (SYMBOL VALUE [NOW])
|
|
364
|
|
365 The unevaluated VALUE is stored as the saved value for SYMBOL.
|
|
366 If NOW is present and non-nil, VALUE is also evaluated and bound as
|
|
367 the default value for the SYMBOL."
|
|
368 (while args
|
|
369 (let ((entry (car args)))
|
|
370 (if (listp entry)
|
134
|
371 (let* ((symbol (nth 0 entry))
|
|
372 (value (nth 1 entry))
|
|
373 (now (nth 2 entry))
|
|
374 (requests (nth 3 entry))
|
|
375 (set (or (get symbol 'custom-set) 'set-default)))
|
98
|
376 (put symbol 'saved-value (list value))
|
120
|
377 (cond (now
|
|
378 ;; Rogue variable, set it now.
|
|
379 (put symbol 'force-value t)
|
134
|
380 (funcall set symbol (eval value)))
|
120
|
381 ((default-boundp symbol)
|
|
382 ;; Something already set this, overwrite it.
|
134
|
383 (funcall set symbol (eval value))))
|
|
384 (when requests
|
|
385 (put symbol 'custom-requests requests)
|
|
386 (mapcar 'require requests))
|
98
|
387 (setq args (cdr args)))
|
|
388 ;; Old format, a plist of SYMBOL VALUE pairs.
|
124
|
389 (message "Warning: old format `custom-set-variables'")
|
|
390 (ding)
|
|
391 (sit-for 2)
|
98
|
392 (let ((symbol (nth 0 args))
|
|
393 (value (nth 1 args)))
|
|
394 (put symbol 'saved-value (list value)))
|
|
395 (setq args (cdr (cdr args)))))))
|
|
396
|
|
397 ;;; The End.
|
|
398
|
|
399 (provide 'custom)
|
|
400
|
|
401 ;; custom.el ends here
|