*** empty log message ***
[gnus] / lisp / custom.el
1 ;;; custom.el -- Tools for declaring and initializing options.
2 ;;
3 ;; Copyright (C) 1996 Free Software Foundation, Inc.
4 ;;
5 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
6 ;; Keywords: help, faces
7 ;; Version: 0.94
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 ;; `custom-edit.el'. 
17
18 ;;; Code:
19
20 (require 'widget)
21
22 (define-widget-keywords :type :group)
23
24 ;; These autoloads should be deleted when the file is added to Emacs
25 (autoload 'customize "custom-edit" nil t)
26 (autoload 'customize-variable "custom-edit" nil t)
27 (autoload 'customize-face "custom-edit" nil t)
28 (autoload 'customize-apropos "custom-edit" nil t)
29
30 ;;; Compatibility.
31
32 (fset 'custom-x-color-values 
33       (if (fboundp 'x-color-values)
34           'x-color-values
35         (lambda (color)
36           (color-instance-rgb-components
37            (make-color-instance color)))))
38
39 (defun custom-background-mode ()
40   "Kludge to detext background mode."
41   (let* ((bg-resource 
42           (condition-case ()
43               (x-get-resource ".backgroundMode" "BackgroundMode" 'string)
44             (error nil)))
45          (params (frame-parameters))
46          (color (condition-case ()
47                     (or (assq 'background-color params)
48                         (color-instance-name
49                          (specifier-instance
50                           (face-background 'default))))
51                   (error nil))))
52     (cond (bg-resource (intern (downcase bg-resource)))
53           ((and color
54                 (< (apply '+ (custom-x-color-values color))
55                    (/ (apply '+ (custom-x-color-values "white")) 3)))
56            'dark)
57           (t 'light))))
58
59 ;;; The `defcustom' Macro.
60
61 ;;;###autoload
62 (defun custom-declare-variable (symbol value doc &rest args)
63   "Like `defcustom', but SYMBOL and VALUE are evaluated as notmal arguments."
64   (unless (default-boundp symbol)
65     (set-default symbol (eval value)))
66   (put symbol 'factory-value (list value))
67   (when doc
68     (put symbol 'variable-documentation doc))
69   (while args 
70     (let ((arg (car args)))
71       (setq args (cdr args))
72       (unless (symbolp arg)
73         (error "Junk in args %S" args))
74       (let ((keyword arg)
75             (value (car args)))
76         (unless args
77           (error "Keyword %s is missing an argument" keyword))
78         (setq args (cdr args))
79         (cond ((eq keyword :type)
80                (put symbol 'custom-type value))
81               ((eq keyword :group)
82                (custom-add-to-group value symbol 'custom-variable))
83               (t
84                (error "Unknown keyword %s" symbol)))))))
85
86 ;;;###autoload
87 (defmacro defcustom (symbol value doc &rest args)
88   "Declare SYMBOL as a customizable variable that defaults to VALUE.
89 DOC is the variable documentation.
90
91 Neither SYMBOL nor VALUE needs to be quoted.
92 If SYMBOL is not already bound, initialize it to VALUE.
93 The remaining arguments should have the form
94
95    [KEYWORD VALUE]... 
96
97 The following KEYWORD's are defined:
98
99 :type   VALUE should be a sexp widget.
100 :group  VALUE should be a customization group.  
101         Add SYMBOL to that group.
102
103 Read the section about customization in the emacs lisp manual for more
104 information."
105   `(eval-and-compile
106      (custom-declare-variable (quote ,symbol) (quote ,value) ,doc ,@args)))
107
108 ;;; The `defface' Macro.
109
110 ;;;###autoload
111 (defun custom-declare-face (face spec doc &rest args)
112   "Like `defface', but FACE is evaluated as a normal argument."
113   (put face 'factory-face spec)
114   (let ((value (or (get face 'saved-face) spec)))
115     (custom-face-display-set face value))
116   (when doc
117     (put face 'face-documentation doc))
118   (while args 
119     (let ((arg (car args)))
120       (setq args (cdr args))
121       (unless (symbolp arg)
122         (error "Junk in args %S" args))
123       (let ((keyword arg)
124             (value (car args)))
125         (unless args
126           (error "Keyword %s is missing an argument" :type))
127         (setq args (cdr args))
128         (cond ((eq keyword :group)
129                (custom-add-to-group value face 'custom-face))
130               (t
131                (error "Unknown keyword %s" face)))))))
132
133 ;;;###autoload
134 (defmacro defface (face spec doc &rest args)
135   "Declare FACE as a customizable face that defaults to SPEC.
136 FACE does not need to be quoted.
137
138 Third argument DOC is the face documentation.
139
140 If FACE has been set with `custom-set-face', set the face attributes
141 as specified by that function, otherwise set the face attributes
142 according to SPEC.
143
144 The remaining arguments should have the form
145
146    [KEYWORD VALUE]...
147
148 The following KEYWORD's are defined:
149
150 :group  VALUE should be a customization group.
151         Add FACE to that group.
152
153 SPEC should be an alist of the form ((DISPLAY ATTS)...).
154
155 ATTS is a list of face attributes and their values.  The possible
156 attributes are defined in the variable `custom-face-attributes'.
157 Alternatively, ATTS can be a face in which case the attributes of that
158 face is used.
159
160 The ATTS of the first entry in SPEC where the DISPLAY matches the
161 frame should take effect in that frame.  DISPLAY can either be the
162 symbol `t', which will match all frames, or an alist of the form
163 \((REQ ITEM...)...)
164
165 For the DISPLAY to match a FRAME, the REQ property of the frame must
166 match one of the ITEM.  The following REQ are defined:
167
168 `type' (the value of (window-system))
169   Should be one of `x' or `tty'.
170
171 `class' (the frame's color support)
172   Should be one of `color', `grayscale', or `mono'.
173
174 `background' (what color is used for the background text)
175   Should be one of `light' or `dark'.
176
177 Read the section about customization in the emacs lisp manual for more
178 information."
179   `(custom-declare-face (quote ,face) ,spec ,doc ,@args))
180
181 ;;; The `defgroup' Macro.
182
183 ;;;###autoload
184 (defun custom-declare-group (symbol members doc &rest args)
185   "Like `defgroup', but SYMBOL is evaluated as a normal argument."
186   (put symbol 'custom-group (nconc members (get symbol 'custom-group)))
187   (when doc
188     (put symbol 'group-documentation doc))
189   (while args 
190     (let ((arg (car args)))
191       (setq args (cdr args))
192       (unless (symbolp arg)
193         (error "Junk in args %S" args))
194       (let ((keyword arg)
195             (value (car args)))
196         (unless args
197           (error "Keyword %s is missing an argument" :type))
198         (setq args (cdr args))
199         (cond ((eq keyword :group)
200                (custom-add-to-group value symbol 'custom-group))
201               (t
202                (error "Unknown keyword %s" symbol)))))))
203
204 ;;;###autoload
205 (defmacro defgroup (symbol members doc &rest args)
206   "Declare SYMBOL as a customization group containing MEMBERS.
207 SYMBOL does not need to be quoted.
208
209 Third arg DOC is the group documentation.
210
211 MEMBERS should be an alist of the form ((NAME WIDGET)...) where
212 NAME is a symbol and WIDGET is a widget is a widget for editing that
213 symbol.  Useful widgets are `custom-variable' for editing variables,
214 `custom-face' for edit faces, and `custom-group' for editing groups.
215
216 The remaining arguments should have the form
217
218    [KEYWORD VALUE]... 
219
220 The following KEYWORD's are defined:
221
222 :group  VALUE should be a customization group.
223         Add SYMBOL to that group.
224
225 Read the section about customization in the emacs lisp manual for more
226 information."
227   `(custom-declare-group (quote ,symbol) ,members ,doc ,@args))
228
229 ;;;###autoload
230 (defun custom-add-to-group (group option widget)
231   "To existing GROUP add a new OPTION of type WIDGET,
232 If there already is an entry for that option, overwrite it."
233   (let* ((members (get group 'custom-group))
234          (old (assq option members)))
235     (if old
236         (setcar (cdr old) widget)
237       (put group 'custom-group (nconc members (list (list option widget)))))))
238
239 ;;; Face Utilities.
240
241 (make-face 'custom-face-empty)
242
243 (defun custom-face-display-set (face spec &optional frame)
244   "Set FACE to the attributes to the first matching entry in SPEC.
245 Iff optional FRAME is non-nil, set it for that frame only.
246 See `defface' for information about SPEC."
247   (make-face face)
248   (copy-face 'custom-face-empty face)
249   (while spec 
250     (let* ((entry (car spec))
251            (display (nth 0 entry))
252            (atts (nth 1 entry)))
253       (setq spec (cdr spec))
254       (when (custom-display-match-frame display frame)
255         (apply 'custom-face-attribites-set face frame atts)
256         (setq spec nil)))))
257
258 (defcustom custom-background-mode nil
259   "The brightness of the background.
260 Set this to the symbol dark if your background color is dark, light if
261 your background is light, or nil (default) if you want Emacs to
262 examine the brightness for you."
263   :group 'customize
264   :type '(choice (choice-item dark) 
265                  (choice-item light)
266                  (choice-item :tag "default" nil)))
267
268 (defun custom-display-match-frame (display frame)
269   "Non-nil iff DISPLAY matches FRAME.
270 If FRAME is nil, the current FRAME is used."
271   ;; This is a kludge to get started, we realle should use specifiers!
272   (unless frame 
273     (setq frame (selected-frame)))
274   (if (eq display t)
275       t
276     (let ((match t)
277           (pars (frame-parameters frame)))
278       (while (and display match)
279         (let* ((entry (car display))
280                (req (car entry))
281                (options (cdr entry)))
282           (setq display (cdr display))
283           (cond ((eq req 'type)
284                  (setq match (if (fboundp 'device-type)
285                                  (device-type frame)
286                                (memq window-system options))))
287                 ((eq req 'class)
288                  (let ((class (if (fboundp 'device-class)
289                                   (device-class frame)
290                                 (cdr (assq 'display-type pars)))))
291                    (setq match (memq class options))))
292                 ((eq req 'background)
293                  (let ((background (or custom-background-mode
294                                        (cdr (assq 'background-mode pars))
295                                        (custom-background-mode))))
296                    (setq match (memq background options))))
297                 (t
298                  (error "Unknown req `%S' with options `%S'" req options)))))
299       match)))
300
301 (defvar custom-face-attributes
302   '((:bold (toggle :format "Bold: %v") custom-set-face-bold)
303     (:italic (toggle :format "Italic: %v") custom-set-face-italic)
304     (:underline
305      (toggle :format "Underline: %v") set-face-underline-p)
306     (:foreground (color :tag "Foreground") set-face-foreground)
307     (:background (color :tag "Background") set-face-background)
308     (:stipple (editable-field :format "Stipple: %v") set-face-stipple))
309   "Alist of face attributes. 
310
311 The elements are of the form (KEY TYPE SET) where KEY is a symbol
312 identifying the attribute, TYPE is a widget type for editing the
313 attibute, SET is a function for setting the attribute value.
314
315 The SET function should take three arguments, the face to modify, the
316 value of the attribute, and optionally the frame where the face should
317 be changed.")
318
319 (defun custom-face-attribites-set (face frame &rest atts)
320   "For FACE on FRAME set the attributes [KEYWORD VALUE]....
321 Each keyword should be listed in `custom-face-attributes'.
322
323 If FRAME is nil, set the default face."
324   (while atts 
325     (let* ((name (nth 0 atts))
326            (value (nth 1 atts))
327            (fun (nth 2 (assq name custom-face-attributes))))
328       (setq atts (cdr (cdr atts))) 
329       (funcall fun face value))))
330
331 (defun custom-set-face-bold (face value &optional frame)
332   "Set the bold property of FACE to VALUE."
333   (condition-case nil
334       (if value
335           (make-face-bold face frame)
336         (make-face-unbold face frame))
337     (error nil)))
338
339 (defun custom-set-face-italic (face value &optional frame)
340   "Set the italic property of FACE to VALUE."
341   (condition-case nil
342       (if value
343           (make-face-italic face frame)
344         (make-face-unitalic face frame))
345     (error nil)))
346
347 ;;;###autoload
348 (defun custom-initialize-faces (&optional frame)
349   "Initialize all custom faces for FRAME.
350 If FRAME is nil or omitted, initialize them for all frames."
351   (mapatoms (lambda (symbol)
352               (let ((spec (or (get symbol 'saved-face)
353                               (get symbol 'factory-face))))
354                 (when spec 
355                   (custom-face-display-set symbol spec frame))))))
356
357 ;;; Initializing.
358
359 ;;;###autoload
360 (defun custom-set-variables (&rest args)
361   "Initialize variables according to user preferences.  
362 The arguments should have the form [SYMBOL VALUE]...
363 For each symbol, VALUE is evaluated and bound as the default value for
364 the symbol.  The unevaluated VALUE is also stored as the saved value
365 for that symbol."
366   (while args 
367     (let ((symbol (nth 0 args))
368           (value (nth 1 args)))
369       (set-default symbol (eval value))
370       (put symbol 'saved-value (list value)))
371     (setq args (cdr (cdr args)))))
372
373 ;;;###autoload
374 (defun custom-set-faces (&rest args)
375   "Initialize faces according to user preferences.
376 The arguments should have the form [SYMBOL SPEC]...
377 For each symbol, a face with that name is created according to SPEC.
378 See `defface' for the format of SPEC."
379   (while args
380     (let ((face (nth 0 args))
381           (spec (nth 1 args)))
382       (put face 'saved-face spec)
383       (custom-face-display-set face spec))
384     (setq args (cdr (cdr args)))))
385
386 ;;; Meta Customization
387
388 (defgroup emacs nil
389   "Customization of the One True Editor.")
390
391 (defgroup customize nil
392   "Customization of the Customization support."
393   :group 'emacs)
394
395 ;;; The End.
396
397 (provide 'custom)
398
399 ;; custom.el ends here