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