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 (default-boundp symbol)
83 (set-default symbol (if (get symbol 'saved-value)
84 (eval (car (get symbol 'saved-value)))
86 (put symbol 'factory-value (list value))
88 (put symbol 'variable-documentation doc))
90 (let ((arg (car args)))
91 (setq args (cdr args))
93 (error "Junk in args %S" 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))
107 ;; Fast code for the common case.
108 (put symbol 'custom-options (copy-list value))))
110 (custom-add-to-group value symbol 'custom-variable))
112 (error "Unknown keyword %s" symbol))))))
113 (run-hooks 'custom-define-hook))
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.
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
126 The following KEYWORD's are defined:
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.
133 Read the section about customization in the emacs lisp manual for more
136 (custom-declare-variable (quote ,symbol) (quote ,value) ,doc ,@args)))
138 ;;; The `defface' Macro.
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)
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))))
150 (put face 'face-documentation doc))
152 (let ((arg (car args)))
153 (setq args (cdr args))
154 (unless (symbolp arg)
155 (error "Junk in args %S" 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))
164 (error "Unknown keyword %s" face))))))
165 (run-hooks 'custom-define-hook))
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.
172 Third argument DOC is the face documentation.
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
178 The remaining arguments should have the form
182 The following KEYWORD's are defined:
184 :group VALUE should be a customization group.
185 Add FACE to that group.
187 SPEC should be an alist of the form ((DISPLAY ATTS)...).
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
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
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:
202 `type' (the value of (window-system))
203 Should be one of `x' or `tty'.
205 `class' (the frame's color support)
206 Should be one of `color', `grayscale', or `mono'.
208 `background' (what color is used for the background text)
209 Should be one of `light' or `dark'.
211 Read the section about customization in the emacs lisp manual for more
213 `(custom-declare-face (quote ,face) ,spec ,doc ,@args))
215 ;;; The `defgroup' Macro.
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)))
222 (put symbol 'group-documentation doc))
224 (let ((arg (car args)))
225 (setq args (cdr args))
226 (unless (symbolp arg)
227 (error "Junk in args %S" 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))
236 (error "Unknown keyword %s" symbol))))))
237 (run-hooks 'custom-define-hook))
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.
244 Third arg DOC is the group documentation.
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.
251 The remaining arguments should have the form
255 The following KEYWORD's are defined:
257 :group VALUE should be a customization group.
258 Add SYMBOL to that group.
260 Read the section about customization in the emacs lisp manual for more
262 `(custom-declare-group (quote ,symbol) ,members ,doc ,@args))
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)))
271 (setcar (cdr old) widget)
272 (put group 'custom-group (nconc members (list (list option widget)))))))
276 (defun custom-add-option (symbol option)
277 "To the variable SYMBOL add OPTION.
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)))))
287 (and (fboundp 'make-face)
288 (make-face 'custom-face-empty))
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)
296 (copy-face 'custom-face-empty face)
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)
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."
312 :type '(choice (choice-item dark)
314 (choice-item :tag "default" nil)))
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!
321 (setq frame (selected-frame)))
325 (while (and display match)
326 (let* ((entry (car display))
328 (options (cdr entry)))
329 (setq display (cdr display))
330 (cond ((eq req 'type)
331 (let ((type (if (fboundp 'device-type)
334 (setq match (memq type options))))
336 (let ((class (if (fboundp 'device-class)
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))))
346 (error "Unknown req `%S' with options `%S'" req options)))))
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)
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.
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.
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
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'.
371 If FRAME is nil, set the default face."
373 (let* ((name (nth 0 atts))
375 (fun (nth 2 (assq name custom-face-attributes))))
376 (setq atts (cdr (cdr atts)))
378 (funcall fun face value)
381 (defun custom-set-face-bold (face value &optional frame)
382 "Set the bold property of FACE to VALUE."
384 (make-face-bold face frame)
385 (make-face-unbold face frame)))
387 (defun custom-set-face-italic (face value &optional frame)
388 "Set the italic property of FACE to VALUE."
390 (make-face-italic face frame)
391 (make-face-unitalic face frame)))
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))))
401 (custom-face-display-set symbol spec frame))))))
406 (defun custom-set-variables (&rest args)
407 "Initialize variables according to user preferences.
409 The arguments should be a list where each entry has the form:
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."
417 (let ((entry (car args)))
419 (let ((symbol (nth 0 entry))
420 (value (nth 1 entry))
422 (put symbol 'saved-value (list value))
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)))))))
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:
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.
443 See `defface' for the format of SPEC."
445 (let ((entry (car args)))
447 (let ((face (nth 0 entry))
450 (put face 'saved-face spec)
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))
458 (put face 'saved-face spec))
459 (setq args (cdr (cdr args)))))))
461 ;;; Meta Customization
464 "Customization of the One True Editor.")
466 (defgroup customize nil
467 "Customization of the Customization support."
470 (defcustom custom-define-hook nil
471 "Hook called after defining each customize option."
477 (defcustom custom-menu-nesting 2
478 "Maximum nesting in custom menus."
482 (defun custom-menu-create-entry (entry)
483 "Create a easy menu entry for customization option ENTRY.
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.
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)))
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)
500 ,@(mapcar 'custom-menu-create-entry (get symbol 'custom-group))))
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])
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))))))
522 (defun custom-menu-update ()
523 "Update customize menu."
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)))))))
540 ;; custom.el ends here