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
|
185
|
7 ;; Version: 1.9956
|
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
|
165
|
47 (autoload 'customize-set-value "cus-edit" nil t)
|
|
48 (autoload 'customize-set-variable "cus-edit" nil t)
|
106
|
49 (autoload 'customize "cus-edit" nil t)
|
163
|
50 (autoload 'customize-browse "cus-edit" nil t)
|
153
|
51 (autoload 'customize-group "cus-edit" nil t)
|
|
52 (autoload 'customize-group-other-window "cus-edit" nil t)
|
106
|
53 (autoload 'customize-variable "cus-edit" nil t)
|
124
|
54 (autoload 'customize-variable-other-window "cus-edit" nil t)
|
106
|
55 (autoload 'customize-face "cus-edit" nil t)
|
124
|
56 (autoload 'customize-face-other-window "cus-edit" nil t)
|
106
|
57 (autoload 'customize-apropos "cus-edit" nil t)
|
|
58 (autoload 'customize-customized "cus-edit" nil t)
|
149
|
59 (autoload 'customize-saved "cus-edit" nil t)
|
106
|
60 (autoload 'custom-buffer-create "cus-edit")
|
|
61 (autoload 'custom-make-dependencies "cus-edit")
|
124
|
62 (autoload 'custom-menu-create "cus-edit")
|
|
63 (autoload 'customize-menu-create "cus-edit")
|
|
64
|
106
|
65 ;; From cus-face.el
|
|
66 (autoload 'custom-declare-face "cus-face")
|
|
67 (autoload 'custom-set-faces "cus-face"))
|
98
|
68
|
124
|
69 (defvar custom-define-hook nil
|
|
70 ;; Customize information for this option is in `cus-edit.el'.
|
|
71 "Hook called after defining each customize option.")
|
|
72
|
98
|
73 ;;; The `defcustom' Macro.
|
|
74
|
149
|
75 (defun custom-initialize-default (symbol value)
|
|
76 "Initialize SYMBOL with VALUE.
|
|
77 This will do nothing if symbol already has a default binding.
|
|
78 Otherwise, if symbol has a `saved-value' property, it will evaluate
|
|
79 the car of that and used as the default binding for symbol.
|
|
80 Otherwise, VALUE will be evaluated and used as the default binding for
|
|
81 symbol."
|
120
|
82 (unless (default-boundp symbol)
|
153
|
83 ;; Use the saved value if it exists, otherwise the standard setting.
|
98
|
84 (set-default symbol (if (get symbol 'saved-value)
|
|
85 (eval (car (get symbol 'saved-value)))
|
149
|
86 (eval value)))))
|
|
87
|
|
88 (defun custom-initialize-set (symbol value)
|
|
89 "Initialize SYMBOL with VALUE.
|
|
90 Like `custom-initialize-default', but use the function specified by
|
|
91 `:set' to initialize SYMBOL."
|
|
92 (unless (default-boundp symbol)
|
|
93 (funcall (or (get symbol 'custom-set) 'set-default)
|
|
94 symbol
|
|
95 (if (get symbol 'saved-value)
|
|
96 (eval (car (get symbol 'saved-value)))
|
|
97 (eval value)))))
|
|
98
|
|
99 (defun custom-initialize-reset (symbol value)
|
|
100 "Initialize SYMBOL with VALUE.
|
|
101 Like `custom-initialize-set', but use the function specified by
|
|
102 `:get' to reinitialize SYMBOL if it is already bound."
|
|
103 (funcall (or (get symbol 'custom-set) 'set-default)
|
|
104 symbol
|
|
105 (cond ((default-boundp symbol)
|
|
106 (funcall (or (get symbol 'custom-get) 'default-value)
|
|
107 symbol))
|
|
108 ((get symbol 'saved-value)
|
|
109 (eval (car (get symbol 'saved-value))))
|
|
110 (t
|
|
111 (eval value)))))
|
|
112
|
|
113 (defun custom-initialize-changed (symbol value)
|
|
114 "Initialize SYMBOL with VALUE.
|
|
115 Like `custom-initialize-reset', but only use the `:set' function if the
|
153
|
116 not using the standard setting. Otherwise, use the `set-default'."
|
149
|
117 (cond ((default-boundp symbol)
|
|
118 (funcall (or (get symbol 'custom-set) 'set-default)
|
|
119 symbol
|
|
120 (funcall (or (get symbol 'custom-get) 'default-value)
|
|
121 symbol)))
|
|
122 ((get symbol 'saved-value)
|
|
123 (funcall (or (get symbol 'custom-set) 'set-default)
|
|
124 symbol
|
|
125 (eval (car (get symbol 'saved-value)))))
|
|
126 (t
|
|
127 (set-default symbol (eval value)))))
|
|
128
|
|
129 (defun custom-declare-variable (symbol value doc &rest args)
|
|
130 "Like `defcustom', but SYMBOL and VALUE are evaluated as normal arguments."
|
153
|
131 ;; Remember the standard setting.
|
|
132 (put symbol 'standard-value (list value))
|
120
|
133 ;; Maybe this option was rogue in an earlier version. It no longer is.
|
|
134 (when (get symbol 'force-value)
|
|
135 ;; It no longer is.
|
|
136 (put symbol 'force-value nil))
|
98
|
137 (when doc
|
|
138 (put symbol 'variable-documentation doc))
|
149
|
139 (let ((initialize 'custom-initialize-set)
|
|
140 (requests nil))
|
|
141 (while args
|
|
142 (let ((arg (car args)))
|
98
|
143 (setq args (cdr args))
|
149
|
144 (unless (symbolp arg)
|
|
145 (error "Junk in args %S" args))
|
|
146 (let ((keyword arg)
|
|
147 (value (car args)))
|
|
148 (unless args
|
|
149 (error "Keyword %s is missing an argument" keyword))
|
|
150 (setq args (cdr args))
|
|
151 (cond ((eq keyword :initialize)
|
|
152 (setq initialize value))
|
|
153 ((eq keyword :set)
|
|
154 (put symbol 'custom-set value))
|
|
155 ((eq keyword :get)
|
|
156 (put symbol 'custom-get value))
|
|
157 ((eq keyword :require)
|
153
|
158 (setq requests (cons value requests)))
|
149
|
159 ((eq keyword :type)
|
|
160 (put symbol 'custom-type value))
|
|
161 ((eq keyword :options)
|
|
162 (if (get symbol 'custom-options)
|
|
163 ;; Slow safe code to avoid duplicates.
|
|
164 (mapcar (lambda (option)
|
|
165 (custom-add-option symbol option))
|
|
166 value)
|
|
167 ;; Fast code for the common case.
|
|
168 (put symbol 'custom-options (copy-sequence value))))
|
|
169 (t
|
|
170 (custom-handle-keyword symbol keyword value
|
|
171 'custom-variable))))))
|
|
172 (put symbol 'custom-requests requests)
|
|
173 ;; Do the actual initialization.
|
|
174 (funcall initialize symbol value))
|
98
|
175 (run-hooks 'custom-define-hook)
|
|
176 symbol)
|
|
177
|
|
178 (defmacro defcustom (symbol value doc &rest args)
|
|
179 "Declare SYMBOL as a customizable variable that defaults to VALUE.
|
|
180 DOC is the variable documentation.
|
|
181
|
|
182 Neither SYMBOL nor VALUE needs to be quoted.
|
|
183 If SYMBOL is not already bound, initialize it to VALUE.
|
|
184 The remaining arguments should have the form
|
|
185
|
|
186 [KEYWORD VALUE]...
|
|
187
|
|
188 The following KEYWORD's are defined:
|
|
189
|
149
|
190 :type VALUE should be a widget type for editing the symbols value.
|
|
191 The default is `sexp'.
|
98
|
192 :options VALUE should be a list of valid members of the widget type.
|
|
193 :group VALUE should be a customization group.
|
|
194 Add SYMBOL to that group.
|
149
|
195 :initialize VALUE should be a function used to initialize the
|
|
196 variable. It takes two arguments, the symbol and value
|
|
197 given in the `defcustom' call. The default is
|
173
|
198 `custom-initialize-set'
|
149
|
199 :set VALUE should be a function to set the value of the symbol.
|
|
200 It takes two arguments, the symbol to set and the value to
|
|
201 give it. The default is `set-default'.
|
|
202 :get VALUE should be a function to extract the value of symbol.
|
|
203 The function takes one argument, a symbol, and should return
|
|
204 the current value for that symbol. The default is
|
|
205 `default-value'.
|
|
206 :require VALUE should be a feature symbol. Each feature will be
|
|
207 required after initialization, of the the user have saved this
|
|
208 option.
|
98
|
209
|
149
|
210 Read the section about customization in the Emacs Lisp manual for more
|
98
|
211 information."
|
149
|
212 `(custom-declare-variable (quote ,symbol) (quote ,value) ,doc ,@args))
|
98
|
213
|
|
214 ;;; The `defface' Macro.
|
|
215
|
|
216 (defmacro defface (face spec doc &rest args)
|
|
217 "Declare FACE as a customizable face that defaults to SPEC.
|
|
218 FACE does not need to be quoted.
|
|
219
|
|
220 Third argument DOC is the face documentation.
|
|
221
|
|
222 If FACE has been set with `custom-set-face', set the face attributes
|
|
223 as specified by that function, otherwise set the face attributes
|
|
224 according to SPEC.
|
|
225
|
|
226 The remaining arguments should have the form
|
|
227
|
|
228 [KEYWORD VALUE]...
|
|
229
|
155
|
230 The following KEYWORDs are defined:
|
98
|
231
|
|
232 :group VALUE should be a customization group.
|
|
233 Add FACE to that group.
|
|
234
|
|
235 SPEC should be an alist of the form ((DISPLAY ATTS)...).
|
|
236
|
|
237 ATTS is a list of face attributes and their values. The possible
|
|
238 attributes are defined in the variable `custom-face-attributes'.
|
|
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
|
177
|
295 MEMBERS should be an alist of the form ((NAME WIDGET)...) where NAME
|
|
296 is a symbol and WIDGET is a widget for editing that symbol. Useful
|
|
297 widgets are `custom-variable' for editing variables, `custom-face' for
|
|
298 edit faces, and `custom-group' for editing groups.
|
98
|
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
|