f8258fa72d696e614fea06127d1feee07dd46147
[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.98
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 (and (fboundp 'make-face)
242      (make-face 'custom-face-empty))
243
244 (defun custom-face-display-set (face spec &optional frame)
245   "Set FACE to the attributes to the first matching entry in SPEC.
246 Iff optional FRAME is non-nil, set it for that frame only.
247 See `defface' for information about SPEC."
248   (when (fboundp 'make-face)
249     (make-face face)
250     (copy-face 'custom-face-empty face)
251     (while spec 
252       (let* ((entry (car spec))
253              (display (nth 0 entry))
254              (atts (nth 1 entry)))
255         (setq spec (cdr spec))
256         (when (custom-display-match-frame display frame)
257           (apply 'custom-face-attribites-set face frame atts)
258           (setq spec nil))))))
259
260 (defcustom custom-background-mode nil
261   "The brightness of the background.
262 Set this to the symbol dark if your background color is dark, light if
263 your background is light, or nil (default) if you want Emacs to
264 examine the brightness for you."
265   :group 'customize
266   :type '(choice (choice-item dark) 
267                  (choice-item light)
268                  (choice-item :tag "default" nil)))
269
270 (defun custom-display-match-frame (display frame)
271   "Non-nil iff DISPLAY matches FRAME.
272 If FRAME is nil, the current FRAME is used."
273   ;; This is a kludge to get started, we realle should use specifiers!
274   (unless frame 
275     (setq frame (selected-frame)))
276   (if (eq display t)
277       t
278     (let ((match t)
279           (pars (frame-parameters frame)))
280       (while (and display match)
281         (let* ((entry (car display))
282                (req (car entry))
283                (options (cdr entry)))
284           (setq display (cdr display))
285           (cond ((eq req 'type)
286                  (setq match (if (fboundp 'device-type)
287                                  (device-type frame)
288                                (memq window-system options))))
289                 ((eq req 'class)
290                  (let ((class (if (fboundp 'device-class)
291                                   (device-class frame)
292                                 (cdr (assq 'display-type pars)))))
293                    (setq match (memq class options))))
294                 ((eq req 'background)
295                  (let ((background (or custom-background-mode
296                                        (cdr (assq 'background-mode pars))
297                                        (custom-background-mode))))
298                    (setq match (memq background options))))
299                 (t
300                  (error "Unknown req `%S' with options `%S'" req options)))))
301       match)))
302
303 (defvar custom-face-attributes
304   '((:bold (toggle :format "Bold: %v") custom-set-face-bold)
305     (:italic (toggle :format "Italic: %v") custom-set-face-italic)
306     (:underline
307      (toggle :format "Underline: %v") set-face-underline-p)
308     (:foreground (color :tag "Foreground") set-face-foreground)
309     (:background (color :tag "Background") set-face-background)
310     (:stipple (editable-field :format "Stipple: %v") set-face-stipple))
311   "Alist of face attributes. 
312
313 The elements are of the form (KEY TYPE SET) where KEY is a symbol
314 identifying the attribute, TYPE is a widget type for editing the
315 attibute, SET is a function for setting the attribute value.
316
317 The SET function should take three arguments, the face to modify, the
318 value of the attribute, and optionally the frame where the face should
319 be changed.")
320
321 (defun custom-face-attribites-set (face frame &rest atts)
322   "For FACE on FRAME set the attributes [KEYWORD VALUE]....
323 Each keyword should be listed in `custom-face-attributes'.
324
325 If FRAME is nil, set the default face."
326   (while atts 
327     (let* ((name (nth 0 atts))
328            (value (nth 1 atts))
329            (fun (nth 2 (assq name custom-face-attributes))))
330       (setq atts (cdr (cdr atts))) 
331       (funcall fun face value))))
332
333 (defun custom-set-face-bold (face value &optional frame)
334   "Set the bold property of FACE to VALUE."
335   (condition-case nil
336       (if value
337           (make-face-bold face frame)
338         (make-face-unbold face frame))
339     (error nil)))
340
341 (defun custom-set-face-italic (face value &optional frame)
342   "Set the italic property of FACE to VALUE."
343   (condition-case nil
344       (if value
345           (make-face-italic face frame)
346         (make-face-unitalic face frame))
347     (error nil)))
348
349 ;;;###autoload
350 (defun custom-initialize-faces (&optional frame)
351   "Initialize all custom faces for FRAME.
352 If FRAME is nil or omitted, initialize them for all frames."
353   (mapatoms (lambda (symbol)
354               (let ((spec (or (get symbol 'saved-face)
355                               (get symbol 'factory-face))))
356                 (when spec 
357                   (custom-face-display-set symbol spec frame))))))
358
359 ;;; Initializing.
360
361 ;;;###autoload
362 (defun custom-set-variables (&rest args)
363   "Initialize variables according to user preferences.  
364 The arguments should have the form [SYMBOL VALUE]...
365 For each symbol, VALUE is evaluated and bound as the default value for
366 the symbol.  The unevaluated VALUE is also stored as the saved value
367 for that symbol."
368   (while args 
369     (let ((symbol (nth 0 args))
370           (value (nth 1 args)))
371       (set-default symbol (eval value))
372       (put symbol 'saved-value (list value)))
373     (setq args (cdr (cdr args)))))
374
375 ;;;###autoload
376 (defun custom-set-faces (&rest args)
377   "Initialize faces according to user preferences.
378 The arguments should have the form [SYMBOL SPEC]...
379 For each symbol, a face with that name is created according to SPEC.
380 See `defface' for the format of SPEC."
381   (while args
382     (let ((face (nth 0 args))
383           (spec (nth 1 args)))
384       (put face 'saved-face spec)
385       (custom-face-display-set face spec))
386     (setq args (cdr (cdr args)))))
387
388 ;;; Meta Customization
389
390 (defgroup emacs nil
391   "Customization of the One True Editor.")
392
393 (defgroup customize nil
394   "Customization of the Customization support."
395   :group 'emacs)
396
397 ;;; The End.
398
399 (provide 'custom)
400
401 ;; custom.el ends here