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 :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)
32 (fset 'custom-x-color-values
33 (if (fboundp 'x-color-values)
36 (color-instance-rgb-components
37 (make-color-instance color)))))
39 (defun custom-background-mode ()
40 "Kludge to detext background mode."
43 (x-get-resource ".backgroundMode" "BackgroundMode" 'string)
45 (params (frame-parameters))
46 (color (condition-case ()
47 (or (assq 'background-color params)
50 (face-background 'default))))
52 (cond (bg-resource (intern (downcase bg-resource)))
54 (< (apply '+ (custom-x-color-values color))
55 (/ (apply '+ (custom-x-color-values "white")) 3)))
59 ;;; The `defcustom' Macro.
62 (defun custom-declare-variable (symbol value doc &rest args)
63 "Like `defcustom', but SYMBOL and VALUE are evaluated as notmal arguments."
64 (unless (default-boundp symbol)
65 (set-default symbol (eval value)))
66 (put symbol 'factory-value (list value))
68 (put symbol 'variable-documentation doc))
70 (let ((arg (car args)))
71 (setq args (cdr args))
73 (error "Junk in args %S" args))
77 (error "Keyword %s is missing an argument" keyword))
78 (setq args (cdr args))
79 (cond ((eq keyword :type)
80 (put symbol 'custom-type value))
82 (custom-add-to-group value symbol 'custom-variable))
84 (error "Unknown keyword %s" symbol)))))))
87 (defmacro defcustom (symbol value doc &rest args)
88 "Declare SYMBOL as a customizable variable that defaults to VALUE.
89 DOC is the variable documentation.
91 Neither SYMBOL nor VALUE needs to be quoted.
92 If SYMBOL is not already bound, initialize it to VALUE.
93 The remaining arguments should have the form
97 The following KEYWORD's are defined:
99 :type VALUE should be a sexp widget.
100 :group VALUE should be a customization group.
101 Add SYMBOL to that group.
103 Read the section about customization in the emacs lisp manual for more
106 (custom-declare-variable (quote ,symbol) (quote ,value) ,doc ,@args)))
108 ;;; The `defface' Macro.
111 (defun custom-declare-face (face spec doc &rest args)
112 "Like `defface', but FACE is evaluated as a normal argument."
113 (put face 'factory-face spec)
114 (let ((value (or (get face 'saved-face) spec)))
115 (custom-face-display-set face value))
117 (put face 'face-documentation doc))
119 (let ((arg (car args)))
120 (setq args (cdr args))
121 (unless (symbolp arg)
122 (error "Junk in args %S" args))
126 (error "Keyword %s is missing an argument" :type))
127 (setq args (cdr args))
128 (cond ((eq keyword :group)
129 (custom-add-to-group value face 'custom-face))
131 (error "Unknown keyword %s" face)))))))
134 (defmacro defface (face spec doc &rest args)
135 "Declare FACE as a customizable face that defaults to SPEC.
136 FACE does not need to be quoted.
138 Third argument DOC is the face documentation.
140 If FACE has been set with `custom-set-face', set the face attributes
141 as specified by that function, otherwise set the face attributes
144 The remaining arguments should have the form
148 The following KEYWORD's are defined:
150 :group VALUE should be a customization group.
151 Add FACE to that group.
153 SPEC should be an alist of the form ((DISPLAY ATTS)...).
155 ATTS is a list of face attributes and their values. The possible
156 attributes are defined in the variable `custom-face-attributes'.
157 Alternatively, ATTS can be a face in which case the attributes of that
160 The ATTS of the first entry in SPEC where the DISPLAY matches the
161 frame should take effect in that frame. DISPLAY can either be the
162 symbol `t', which will match all frames, or an alist of the form
165 For the DISPLAY to match a FRAME, the REQ property of the frame must
166 match one of the ITEM. The following REQ are defined:
168 `type' (the value of (window-system))
169 Should be one of `x' or `tty'.
171 `class' (the frame's color support)
172 Should be one of `color', `grayscale', or `mono'.
174 `background' (what color is used for the background text)
175 Should be one of `light' or `dark'.
177 Read the section about customization in the emacs lisp manual for more
179 `(custom-declare-face (quote ,face) ,spec ,doc ,@args))
181 ;;; The `defgroup' Macro.
184 (defun custom-declare-group (symbol members doc &rest args)
185 "Like `defgroup', but SYMBOL is evaluated as a normal argument."
186 (put symbol 'custom-group (nconc members (get symbol 'custom-group)))
188 (put symbol 'group-documentation doc))
190 (let ((arg (car args)))
191 (setq args (cdr args))
192 (unless (symbolp arg)
193 (error "Junk in args %S" args))
197 (error "Keyword %s is missing an argument" :type))
198 (setq args (cdr args))
199 (cond ((eq keyword :group)
200 (custom-add-to-group value symbol 'custom-group))
202 (error "Unknown keyword %s" symbol)))))))
205 (defmacro defgroup (symbol members doc &rest args)
206 "Declare SYMBOL as a customization group containing MEMBERS.
207 SYMBOL does not need to be quoted.
209 Third arg DOC is the group documentation.
211 MEMBERS should be an alist of the form ((NAME WIDGET)...) where
212 NAME is a symbol and WIDGET is a widget is a widget for editing that
213 symbol. Useful widgets are `custom-variable' for editing variables,
214 `custom-face' for edit faces, and `custom-group' for editing groups.
216 The remaining arguments should have the form
220 The following KEYWORD's are defined:
222 :group VALUE should be a customization group.
223 Add SYMBOL to that group.
225 Read the section about customization in the emacs lisp manual for more
227 `(custom-declare-group (quote ,symbol) ,members ,doc ,@args))
230 (defun custom-add-to-group (group option widget)
231 "To existing GROUP add a new OPTION of type WIDGET,
232 If there already is an entry for that option, overwrite it."
233 (let* ((members (get group 'custom-group))
234 (old (assq option members)))
236 (setcar (cdr old) widget)
237 (put group 'custom-group (nconc members (list (list option widget)))))))
241 (and (fboundp 'make-face)
242 (make-face 'custom-face-empty))
244 (defun custom-face-display-set (face spec &optional frame)
245 "Set FACE to the attributes to the first matching entry in SPEC.
246 Iff optional FRAME is non-nil, set it for that frame only.
247 See `defface' for information about SPEC."
248 (when (fboundp 'make-face)
250 (copy-face 'custom-face-empty face)
252 (let* ((entry (car spec))
253 (display (nth 0 entry))
254 (atts (nth 1 entry)))
255 (setq spec (cdr spec))
256 (when (custom-display-match-frame display frame)
257 (apply 'custom-face-attribites-set face frame atts)
260 (defcustom custom-background-mode nil
261 "The brightness of the background.
262 Set this to the symbol dark if your background color is dark, light if
263 your background is light, or nil (default) if you want Emacs to
264 examine the brightness for you."
266 :type '(choice (choice-item dark)
268 (choice-item :tag "default" nil)))
270 (defun custom-display-match-frame (display frame)
271 "Non-nil iff DISPLAY matches FRAME.
272 If FRAME is nil, the current FRAME is used."
273 ;; This is a kludge to get started, we realle should use specifiers!
275 (setq frame (selected-frame)))
279 (pars (frame-parameters frame)))
280 (while (and display match)
281 (let* ((entry (car display))
283 (options (cdr entry)))
284 (setq display (cdr display))
285 (cond ((eq req 'type)
286 (setq match (if (fboundp 'device-type)
288 (memq window-system options))))
290 (let ((class (if (fboundp 'device-class)
292 (cdr (assq 'display-type pars)))))
293 (setq match (memq class options))))
294 ((eq req 'background)
295 (let ((background (or custom-background-mode
296 (cdr (assq 'background-mode pars))
297 (custom-background-mode))))
298 (setq match (memq background options))))
300 (error "Unknown req `%S' with options `%S'" req options)))))
303 (defvar custom-face-attributes
304 '((:bold (toggle :format "Bold: %v") custom-set-face-bold)
305 (:italic (toggle :format "Italic: %v") custom-set-face-italic)
307 (toggle :format "Underline: %v") set-face-underline-p)
308 (:foreground (color :tag "Foreground") set-face-foreground)
309 (:background (color :tag "Background") set-face-background)
310 (:stipple (editable-field :format "Stipple: %v") set-face-stipple))
311 "Alist of face attributes.
313 The elements are of the form (KEY TYPE SET) where KEY is a symbol
314 identifying the attribute, TYPE is a widget type for editing the
315 attibute, SET is a function for setting the attribute value.
317 The SET function should take three arguments, the face to modify, the
318 value of the attribute, and optionally the frame where the face should
321 (defun custom-face-attribites-set (face frame &rest atts)
322 "For FACE on FRAME set the attributes [KEYWORD VALUE]....
323 Each keyword should be listed in `custom-face-attributes'.
325 If FRAME is nil, set the default face."
327 (let* ((name (nth 0 atts))
329 (fun (nth 2 (assq name custom-face-attributes))))
330 (setq atts (cdr (cdr atts)))
331 (funcall fun face value))))
333 (defun custom-set-face-bold (face value &optional frame)
334 "Set the bold property of FACE to VALUE."
337 (make-face-bold face frame)
338 (make-face-unbold face frame))
341 (defun custom-set-face-italic (face value &optional frame)
342 "Set the italic property of FACE to VALUE."
345 (make-face-italic face frame)
346 (make-face-unitalic face frame))
350 (defun custom-initialize-faces (&optional frame)
351 "Initialize all custom faces for FRAME.
352 If FRAME is nil or omitted, initialize them for all frames."
353 (mapatoms (lambda (symbol)
354 (let ((spec (or (get symbol 'saved-face)
355 (get symbol 'factory-face))))
357 (custom-face-display-set symbol spec frame))))))
362 (defun custom-set-variables (&rest args)
363 "Initialize variables according to user preferences.
364 The arguments should have the form [SYMBOL VALUE]...
365 For each symbol, VALUE is evaluated and bound as the default value for
366 the symbol. The unevaluated VALUE is also stored as the saved value
369 (let ((symbol (nth 0 args))
370 (value (nth 1 args)))
371 (set-default symbol (eval value))
372 (put symbol 'saved-value (list value)))
373 (setq args (cdr (cdr args)))))
376 (defun custom-set-faces (&rest args)
377 "Initialize faces according to user preferences.
378 The arguments should have the form [SYMBOL SPEC]...
379 For each symbol, a face with that name is created according to SPEC.
380 See `defface' for the format of SPEC."
382 (let ((face (nth 0 args))
384 (put face 'saved-face spec)
385 (custom-face-display-set face spec))
386 (setq args (cdr (cdr args)))))
388 ;;; Meta Customization
391 "Customization of the One True Editor.")
393 (defgroup customize nil
394 "Customization of the Customization support."
401 ;; custom.el ends here