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