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