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