14d2bfd74729701d07e8cc65c12c8e34d1f14a62
[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: 1.00
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 :options :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 (autoload 'customize-customized "custom-edit" nil t)
30 (autoload 'custom-buffer-create "custom-edit")
31
32 ;;; Compatibility.
33
34 (unless (fboundp 'x-color-values)
35   ;; Emacs function missing in XEmacs 19.14.
36   (defun x-color-values  (color)
37     "Return a description of the color named COLOR on frame FRAME.
38 The value is a list of integer RGB values--(RED GREEN BLUE).
39 These values appear to range from 0 to 65280 or 65535, depending
40 on the system; white is (65280 65280 65280) or (65535 65535 65535).
41 If FRAME is omitted or nil, use the selected frame."
42     (color-instance-rgb-components (make-color-instance color))))
43
44 (unless (fboundp 'frame-property)
45   ;; XEmacs function missing in Emacs 19.34.
46   (defun frame-property (frame property &optional default)
47     "Return FRAME's value for property PROPERTY."
48     (or (cdr (assq property (frame-parameters frame)))
49         default)))
50
51 (defun custom-background-mode ()
52   "Kludge to detext background mode."
53   (let* ((bg-resource 
54           (condition-case ()
55               (x-get-resource ".backgroundMode" "BackgroundMode" 'string)
56             (error nil)))
57          color
58          (mode (cond (bg-resource
59                       (intern (downcase bg-resource)))
60                      ((and (setq color (condition-case ()
61                                            (or (frame-property
62                                                 (selected-frame)
63                                                 'background-color)
64                                                (color-instance-name
65                                                 (specifier-instance
66                                                  (face-background 'default))))
67                                          (error nil)))
68                            (< (apply '+ (x-color-values color))
69                               (/ (apply '+ (x-color-values "white"))
70                                  3)))
71                       'dark)
72                      (t 'light))))
73     (modify-frame-parameters (selected-frame)
74                              (list (cons 'background-mode mode)))
75     mode))
76
77 ;;; The `defcustom' Macro.
78
79 ;;;###autoload
80 (defun custom-declare-variable (symbol value doc &rest args)
81   "Like `defcustom', but SYMBOL and VALUE are evaluated as notmal arguments."
82   (unless (default-boundp symbol)
83     (set-default symbol (if (get symbol 'saved-value)
84                             (eval (car (get symbol 'saved-value)))
85                           (eval value))))
86   (put symbol 'factory-value (list value))
87   (when doc
88     (put symbol 'variable-documentation doc))
89   (while args 
90     (let ((arg (car args)))
91       (setq args (cdr args))
92       (unless (symbolp arg)
93         (error "Junk in args %S" args))
94       (let ((keyword arg)
95             (value (car args)))
96         (unless args
97           (error "Keyword %s is missing an argument" keyword))
98         (setq args (cdr args))
99         (cond ((eq keyword :type)
100                (put symbol 'custom-type value))
101               ((eq keyword :options)
102                (if (get symbol 'custom-options)
103                    ;; Slow safe code to avoid duplicates.
104                    (mapcar (lambda (option)
105                              (custom-add-option symbol option))
106                            value)
107                  ;; Fast code for the common case.
108                  (put symbol 'custom-options (copy-list value))))
109               ((eq keyword :group)
110                (custom-add-to-group value symbol 'custom-variable))
111               (t
112                (error "Unknown keyword %s" symbol))))))
113   (run-hooks 'custom-define-hook))
114
115 ;;;###autoload
116 (defmacro defcustom (symbol value doc &rest args)
117   "Declare SYMBOL as a customizable variable that defaults to VALUE.
118 DOC is the variable documentation.
119
120 Neither SYMBOL nor VALUE needs to be quoted.
121 If SYMBOL is not already bound, initialize it to VALUE.
122 The remaining arguments should have the form
123
124    [KEYWORD VALUE]... 
125
126 The following KEYWORD's are defined:
127
128 :type   VALUE should be a widget type.
129 :options VALUE should be a list of valid members of the widget type.
130 :group  VALUE should be a customization group.  
131         Add SYMBOL to that group.
132
133 Read the section about customization in the emacs lisp manual for more
134 information."
135   `(eval-and-compile
136      (custom-declare-variable (quote ,symbol) (quote ,value) ,doc ,@args)))
137
138 ;;; The `defface' Macro.
139
140 ;;;###autoload
141 (defun custom-declare-face (face spec doc &rest args)
142   "Like `defface', but FACE is evaluated as a normal argument."
143   (put face 'factory-face spec)
144   (when (fboundp 'facep)
145     (unless (facep face)
146       ;; If the user has already created the face, respect that.
147       (let ((value (or (get face 'saved-face) spec)))
148         (custom-face-display-set face value))))
149   (when doc
150     (put face 'face-documentation doc))
151   (while args 
152     (let ((arg (car args)))
153       (setq args (cdr args))
154       (unless (symbolp arg)
155         (error "Junk in args %S" args))
156       (let ((keyword arg)
157             (value (car args)))
158         (unless args
159           (error "Keyword %s is missing an argument" :type))
160         (setq args (cdr args))
161         (cond ((eq keyword :group)
162                (custom-add-to-group value face 'custom-face))
163               (t
164                (error "Unknown keyword %s" face))))))
165   (run-hooks 'custom-define-hook))
166
167 ;;;###autoload
168 (defmacro defface (face spec doc &rest args)
169   "Declare FACE as a customizable face that defaults to SPEC.
170 FACE does not need to be quoted.
171
172 Third argument DOC is the face documentation.
173
174 If FACE has been set with `custom-set-face', set the face attributes
175 as specified by that function, otherwise set the face attributes
176 according to SPEC.
177
178 The remaining arguments should have the form
179
180    [KEYWORD VALUE]...
181
182 The following KEYWORD's are defined:
183
184 :group  VALUE should be a customization group.
185         Add FACE to that group.
186
187 SPEC should be an alist of the form ((DISPLAY ATTS)...).
188
189 ATTS is a list of face attributes and their values.  The possible
190 attributes are defined in the variable `custom-face-attributes'.
191 Alternatively, ATTS can be a face in which case the attributes of that
192 face is used.
193
194 The ATTS of the first entry in SPEC where the DISPLAY matches the
195 frame should take effect in that frame.  DISPLAY can either be the
196 symbol `t', which will match all frames, or an alist of the form
197 \((REQ ITEM...)...)
198
199 For the DISPLAY to match a FRAME, the REQ property of the frame must
200 match one of the ITEM.  The following REQ are defined:
201
202 `type' (the value of (window-system))
203   Should be one of `x' or `tty'.
204
205 `class' (the frame's color support)
206   Should be one of `color', `grayscale', or `mono'.
207
208 `background' (what color is used for the background text)
209   Should be one of `light' or `dark'.
210
211 Read the section about customization in the emacs lisp manual for more
212 information."
213   `(custom-declare-face (quote ,face) ,spec ,doc ,@args))
214
215 ;;; The `defgroup' Macro.
216
217 ;;;###autoload
218 (defun custom-declare-group (symbol members doc &rest args)
219   "Like `defgroup', but SYMBOL is evaluated as a normal argument."
220   (put symbol 'custom-group (nconc members (get symbol 'custom-group)))
221   (when doc
222     (put symbol 'group-documentation doc))
223   (while args 
224     (let ((arg (car args)))
225       (setq args (cdr args))
226       (unless (symbolp arg)
227         (error "Junk in args %S" args))
228       (let ((keyword arg)
229             (value (car args)))
230         (unless args
231           (error "Keyword %s is missing an argument" :type))
232         (setq args (cdr args))
233         (cond ((eq keyword :group)
234                (custom-add-to-group value symbol 'custom-group))
235               (t
236                (error "Unknown keyword %s" symbol))))))
237   (run-hooks 'custom-define-hook))
238
239 ;;;###autoload
240 (defmacro defgroup (symbol members doc &rest args)
241   "Declare SYMBOL as a customization group containing MEMBERS.
242 SYMBOL does not need to be quoted.
243
244 Third arg DOC is the group documentation.
245
246 MEMBERS should be an alist of the form ((NAME WIDGET)...) where
247 NAME is a symbol and WIDGET is a widget is a widget for editing that
248 symbol.  Useful widgets are `custom-variable' for editing variables,
249 `custom-face' for edit faces, and `custom-group' for editing groups.
250
251 The remaining arguments should have the form
252
253    [KEYWORD VALUE]... 
254
255 The following KEYWORD's are defined:
256
257 :group  VALUE should be a customization group.
258         Add SYMBOL to that group.
259
260 Read the section about customization in the emacs lisp manual for more
261 information."
262   `(custom-declare-group (quote ,symbol) ,members ,doc ,@args))
263
264 ;;;###autoload
265 (defun custom-add-to-group (group option widget)
266   "To existing GROUP add a new OPTION of type WIDGET,
267 If there already is an entry for that option, overwrite it."
268   (let* ((members (get group 'custom-group))
269          (old (assq option members)))
270     (if old
271         (setcar (cdr old) widget)
272       (put group 'custom-group (nconc members (list (list option widget)))))))
273
274 ;;; Options
275
276 (defun custom-add-option (symbol option)
277   "To the variable SYMBOL add OPTION.
278
279 If SYMBOL is a hook variable, OPTION should be a hook member.
280 For other types variables, the effect is undefined."
281   (let ((options (get symbol 'custom-options)))
282     (unless (member option options)
283       (put symbol 'custom-options (cons option options)))))
284
285 ;;; Face Utilities.
286
287 (and (fboundp 'make-face)
288      (make-face 'custom-face-empty))
289
290 (defun custom-face-display-set (face spec &optional frame)
291   "Set FACE to the attributes to the first matching entry in SPEC.
292 Iff optional FRAME is non-nil, set it for that frame only.
293 See `defface' for information about SPEC."
294   (when (fboundp 'make-face)
295     (make-face face)
296     (copy-face 'custom-face-empty face)
297     (while spec 
298       (let* ((entry (car spec))
299              (display (nth 0 entry))
300              (atts (nth 1 entry)))
301         (setq spec (cdr spec))
302         (when (custom-display-match-frame display frame)
303           (apply 'custom-face-attribites-set face frame atts)
304           (setq spec nil))))))
305
306 (defcustom custom-background-mode nil
307   "The brightness of the background.
308 Set this to the symbol dark if your background color is dark, light if
309 your background is light, or nil (default) if you want Emacs to
310 examine the brightness for you."
311   :group 'customize
312   :type '(choice (choice-item dark) 
313                  (choice-item light)
314                  (choice-item :tag "default" nil)))
315
316 (defun custom-display-match-frame (display frame)
317   "Non-nil iff DISPLAY matches FRAME.
318 If FRAME is nil, the current FRAME is used."
319   ;; This is a kludge to get started, we really should use specifiers!
320   (unless frame 
321     (setq frame (selected-frame)))
322   (if (eq display t)
323       t
324     (let ((match t))
325       (while (and display match)
326         (let* ((entry (car display))
327                (req (car entry))
328                (options (cdr entry)))
329           (setq display (cdr display))
330           (cond ((eq req 'type)
331                  (let ((type (if (fboundp 'device-type)
332                                  (device-type frame)
333                                window-system)))
334                    (setq match (memq type options))))
335                 ((eq req 'class)
336                  (let ((class (if (fboundp 'device-class)
337                                   (device-class frame)
338                                 (frame-property frame 'display-type))))
339                    (setq match (memq class options))))
340                 ((eq req 'background)
341                  (let ((background (or custom-background-mode
342                                        (frame-property frame 'background-mode)
343                                        (custom-background-mode))))
344                    (setq match (memq background options))))
345                 (t
346                  (error "Unknown req `%S' with options `%S'" req options)))))
347       match)))
348
349 (defvar custom-face-attributes
350   '((:bold (toggle :format "Bold: %v") custom-set-face-bold)
351     (:italic (toggle :format "Italic: %v") custom-set-face-italic)
352     (:underline
353      (toggle :format "Underline: %v") set-face-underline-p)
354     (:foreground (color :tag "Foreground") set-face-foreground)
355     (:background (color :tag "Background") set-face-background)
356     (:stipple (editable-field :format "Stipple: %v") set-face-stipple))
357   "Alist of face attributes. 
358
359 The elements are of the form (KEY TYPE SET) where KEY is a symbol
360 identifying the attribute, TYPE is a widget type for editing the
361 attibute, SET is a function for setting the attribute value.
362
363 The SET function should take three arguments, the face to modify, the
364 value of the attribute, and optionally the frame where the face should
365 be changed.")
366
367 (defun custom-face-attribites-set (face frame &rest atts)
368   "For FACE on FRAME set the attributes [KEYWORD VALUE]....
369 Each keyword should be listed in `custom-face-attributes'.
370
371 If FRAME is nil, set the default face."
372   (while atts 
373     (let* ((name (nth 0 atts))
374            (value (nth 1 atts))
375            (fun (nth 2 (assq name custom-face-attributes))))
376       (setq atts (cdr (cdr atts)))
377       (condition-case nil
378           (funcall fun face value)
379         (error nil)))))
380
381 (defun custom-set-face-bold (face value &optional frame)
382   "Set the bold property of FACE to VALUE."
383   (if value
384       (make-face-bold face frame)
385     (make-face-unbold face frame)))
386
387 (defun custom-set-face-italic (face value &optional frame)
388   "Set the italic property of FACE to VALUE."
389   (if value
390       (make-face-italic face frame)
391     (make-face-unitalic face frame)))
392
393 ;;;###autoload
394 (defun custom-initialize-faces (&optional frame)
395   "Initialize all custom faces for FRAME.
396 If FRAME is nil or omitted, initialize them for all frames."
397   (mapatoms (lambda (symbol)
398               (let ((spec (or (get symbol 'saved-face)
399                               (get symbol 'factory-face))))
400                 (when spec 
401                   (custom-face-display-set symbol spec frame))))))
402
403 ;;; Initializing.
404
405 ;;;###autoload
406 (defun custom-set-variables (&rest args)
407   "Initialize variables according to user preferences.  
408
409 The arguments should be a list where each entry has the form:
410
411   (SYMBOL VALUE [NOW])
412
413 The unevaluated VALUE is stored as the saved value for SYMBOL.
414 If NOW is present and non-nil, VALUE is also evaluated and bound as
415 the default value for the SYMBOL."
416   (while args 
417     (let ((entry (car args)))
418       (if (listp entry)
419           (let ((symbol (nth 0 entry))
420                 (value (nth 1 entry))
421                 (now (nth 2 entry)))
422             (put symbol 'saved-value (list value))
423             (when now 
424               (put symbol 'force-value t)
425               (set-default symbol (eval value)))
426             (setq args (cdr args)))
427         ;; Old format, a plist of SYMBOL VALUE pairs.
428         (let ((symbol (nth 0 args))
429               (value (nth 1 args)))
430           (put symbol 'saved-value (list value)))
431         (setq args (cdr (cdr args)))))))
432
433 ;;;###autoload
434 (defun custom-set-faces (&rest args)
435   "Initialize faces according to user preferences.
436 The arguments should be a list where each entry has the form:
437
438   (FACE SPEC [NOW])
439
440 SPEC will be stored as the saved value for FACE.  If NOW is present
441 and non-nil, FACE will also be created according to SPEC.
442
443 See `defface' for the format of SPEC."
444   (while args
445     (let ((entry (car args)))
446       (if (listp entry)
447           (let ((face (nth 0 entry))
448                 (spec (nth 1 entry))
449                 (now (nth 2 entry)))
450             (put face 'saved-face spec)
451             (when now
452               (put face 'force-face t)
453               (custom-face-display-set face spec))
454             (setq args (cdr args)))
455         ;; Old format, a plist of FACE SPEC pairs.
456         (let ((face (nth 0 args))
457               (spec (nth 1 args)))
458           (put face 'saved-face spec))
459         (setq args (cdr (cdr args)))))))
460
461 ;;; Meta Customization
462
463 (defgroup emacs nil
464   "Customization of the One True Editor.")
465
466 (defgroup customize nil
467   "Customization of the Customization support."
468   :group 'emacs)
469
470 (defcustom custom-define-hook nil
471   "Hook called after defining each customize option."
472   :group 'customize
473   :type 'hook)
474
475 ;;; Menu support
476
477 (defcustom custom-menu-nesting 2
478   "Maximum nesting in custom menus."
479   :type 'integer
480   :group 'customize)
481
482 (defun custom-menu-create-entry (entry)
483   "Create a easy menu entry for customization option ENTRY.
484
485 ENTRY should be list whose first element is a symbol, and second
486 element is a widget type used to customize the symbol.  The widget
487 type `custom-group' is recognized, and will return a submenu
488 specification containing the group members.
489
490 The maximum nesting in the submenu is specified by `custom-menu-nesting'."
491   (let ((item (vector (symbol-name (nth 0 entry))
492                       `(custom-buffer-create (list (quote ,entry)))
493                       t)))
494     (if (and (> custom-menu-nesting 0)
495              (eq (nth 1 entry) 'custom-group))
496         (let ((custom-menu-nesting (1- custom-menu-nesting))
497               (symbol (nth 0 entry)))
498           `(,(symbol-name symbol)
499             ,item
500             ,@(mapcar 'custom-menu-create-entry (get symbol 'custom-group))))
501       item)))
502
503 (defconst custom-help-menu '("Customize"
504                              ["Update menu..." custom-menu-update t]
505                              ["Group..." customize t]
506                              ["Variable..." customize-variable t]
507                              ["Face..." customize-face t]
508                              ["Saved..." customize-customized t]
509                              ["Apropos..." customize-apropos t])
510   "Customize menu")
511
512 (defun custom-menu-reset ()
513   "Reset customize menu."
514   (remove-hook 'custom-define-hook 'custom-menu-reset)
515   (if (fboundp 'add-submenu)
516       (add-submenu '("Help") custom-help-menu)
517     (define-key global-map [menu-bar help-menu customize-menu]
518       (cons (car custom-help-menu)
519             (easy-menu-create-keymaps (car custom-help-menu)
520                                       (cdr custom-help-menu))))))
521
522 (defun custom-menu-update ()
523   "Update customize menu."
524   (interactive)
525   (add-hook 'custom-define-hook 'custom-menu-reset)
526   (let ((menu `(,(car custom-help-menu)
527                 ,(custom-menu-create-entry '(emacs custom-group))
528                 ,@(cdr (cdr custom-help-menu)))))
529     (if (fboundp 'add-submenu)
530         (add-submenu '("Help") menu)
531       (define-key global-map [menu-bar help-menu customize-menu]
532         (cons (car menu) (easy-menu-create-keymaps (car menu) (cdr menu)))))))
533
534 (custom-menu-reset)
535
536 ;;; The End.
537
538 (provide 'custom)
539
540 ;; custom.el ends here