1 ;;; custom.el -- Tools for declaring and initializing options.
3 ;; Copyright (C) 1996 Free Software Foundation, Inc.
5 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
6 ;; Keywords: help, faces
8 ;; X-URL: http://www.dina.kvl.dk/~abraham/custom/
12 ;; If you want to use this code, please visit the URL above.
14 ;; This file only contain the code needed to declare and initialize
15 ;; user options. The code to customize options is autoloaded from
22 (define-widget-keywords :options :type :group)
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")
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))))
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)))
51 (defun custom-background-mode ()
52 "Kludge to detext background mode."
55 (x-get-resource ".backgroundMode" "BackgroundMode" 'string)
58 (mode (cond (bg-resource
59 (intern (downcase bg-resource)))
60 ((and (setq color (condition-case ()
66 (face-background 'default))))
68 (< (apply '+ (x-color-values color))
69 (/ (apply '+ (x-color-values "white"))
73 (modify-frame-parameters (selected-frame)
74 (list (cons 'background-mode mode)))
77 ;;; The `defcustom' Macro.
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)))
87 (put symbol 'factory-value (list value))
89 (put symbol 'variable-documentation doc))
91 (let ((arg (car args)))
92 (setq args (cdr args))
94 (error "Junk in args %S" 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))
108 ;; Fast code for the common case.
109 (put symbol 'custom-options (copy-list value))))
111 (custom-add-to-group value symbol 'custom-variable))
113 (error "Unknown keyword %s" symbol))))))
114 (run-hooks 'custom-define-hook))
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.
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
127 The following KEYWORD's are defined:
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.
134 Read the section about customization in the emacs lisp manual for more
137 (custom-declare-variable (quote ,symbol) (quote ,value) ,doc ,@args)))
139 ;;; The `defface' Macro.
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))))
152 (put face 'face-documentation doc))
154 (let ((arg (car args)))
155 (setq args (cdr args))
156 (unless (symbolp arg)
157 (error "Junk in args %S" 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))
166 (error "Unknown keyword %s" face))))))
167 (run-hooks 'custom-define-hook))
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.
174 Third argument DOC is the face documentation.
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
180 The remaining arguments should have the form
184 The following KEYWORD's are defined:
186 :group VALUE should be a customization group.
187 Add FACE to that group.
189 SPEC should be an alist of the form ((DISPLAY ATTS)...).
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
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
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:
204 `type' (the value of (window-system))
205 Should be one of `x' or `tty'.
207 `class' (the frame's color support)
208 Should be one of `color', `grayscale', or `mono'.
210 `background' (what color is used for the background text)
211 Should be one of `light' or `dark'.
213 Read the section about customization in the emacs lisp manual for more
215 `(custom-declare-face (quote ,face) ,spec ,doc ,@args))
217 ;;; The `defgroup' Macro.
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)))
224 (put symbol 'group-documentation doc))
226 (let ((arg (car args)))
227 (setq args (cdr args))
228 (unless (symbolp arg)
229 (error "Junk in args %S" 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))
238 (error "Unknown keyword %s" symbol))))))
239 (run-hooks 'custom-define-hook))
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.
246 Third arg DOC is the group documentation.
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.
253 The remaining arguments should have the form
257 The following KEYWORD's are defined:
259 :group VALUE should be a customization group.
260 Add SYMBOL to that group.
262 Read the section about customization in the emacs lisp manual for more
264 `(custom-declare-group (quote ,symbol) ,members ,doc ,@args))
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)))
273 (setcar (cdr old) widget)
274 (put group 'custom-group (nconc members (list (list option widget)))))))
278 (defun custom-add-option (symbol option)
279 "To the variable SYMBOL add OPTION.
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)))))
289 (and (fboundp 'make-face)
290 (make-face 'custom-face-empty))
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)
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)
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."
313 :type '(choice (choice-item dark)
315 (choice-item :tag "default" nil)))
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!
322 (setq frame (selected-frame)))
326 (while (and display match)
327 (let* ((entry (car display))
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))
335 (setq match (memq type options))))
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))))
347 (error "Unknown req `%S' with options `%S'" req options)))))
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)
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.
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.
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
368 (when (string-match "XEmacs" emacs-version)
369 ;; Support for special XEmacs font attributes.
372 (unless (fboundp 'face-font-name)
373 (defun face-font-name (face &rest args)
374 (apply 'face-font face args)))
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)))
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)))
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))))
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'.
400 If FRAME is nil, set the default face."
402 (let* ((name (nth 0 atts))
404 (fun (nth 2 (assq name custom-face-attributes))))
405 (setq atts (cdr (cdr atts)))
407 (funcall fun face value)
410 (defun custom-set-face-bold (face value &optional frame)
411 "Set the bold property of FACE to VALUE."
413 (make-face-bold face frame)
414 (make-face-unbold face frame)))
416 (defun custom-set-face-italic (face value &optional frame)
417 "Set the italic property of FACE to VALUE."
419 (make-face-italic face frame)
420 (make-face-unitalic face frame)))
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))))
430 (custom-face-display-set symbol spec frame))))))
435 (defun custom-set-variables (&rest args)
436 "Initialize variables according to user preferences.
438 The arguments should be a list where each entry has the form:
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."
446 (let ((entry (car args)))
448 (let ((symbol (nth 0 entry))
449 (value (nth 1 entry))
451 (put symbol 'saved-value (list value))
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)))))))
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:
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.
472 See `defface' for the format of SPEC."
474 (let ((entry (car args)))
476 (let ((face (nth 0 entry))
479 (put face 'saved-face spec)
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))
487 (put face 'saved-face spec))
488 (setq args (cdr (cdr args)))))))
490 ;;; Meta Customization
493 "Customization of the One True Editor.")
495 (defgroup customize nil
496 "Customization of the Customization support."
499 (defcustom custom-define-hook nil
500 "Hook called after defining each customize option."
506 (defcustom custom-menu-nesting 2
507 "Maximum nesting in custom menus."
511 (defun custom-menu-create-entry (entry)
512 "Create a easy menu entry for customization option ENTRY.
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.
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)))
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)
529 ,@(mapcar 'custom-menu-create-entry (get symbol 'custom-group))))
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])
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))))))
551 (defun custom-menu-update ()
552 "Update customize menu."
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)))))))
569 ;; custom.el ends here