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