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