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