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