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)
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 (mode (cond (bg-resource (intern (downcase bg-resource)))
54 (< (apply '+ (custom-x-color-values color))
55 (/ (apply '+ (custom-x-color-values "white"))
59 (if (fboundp 'set-frame-property)
60 ;; `modify-frame-properties' is borken on XEmacs 19.14.
61 (set-frame-property (selected-frame) 'background-mode mode)
62 ;; `set-frame-property' is unimplemented in Emacs 19.34.
63 (modify-frame-parameters (selected-frame)
64 (cons (cons 'background-mode mode) params)))
67 ;;; The `defcustom' Macro.
70 (defun custom-declare-variable (symbol value doc &rest args)
71 "Like `defcustom', but SYMBOL and VALUE are evaluated as notmal arguments."
72 (unless (default-boundp symbol)
73 (set-default symbol (if (get symbol 'saved-value)
74 (eval (car (get symbol 'saved-value)))
76 (put symbol 'factory-value (list value))
78 (put symbol 'variable-documentation doc))
80 (let ((arg (car args)))
81 (setq args (cdr args))
83 (error "Junk in args %S" args))
87 (error "Keyword %s is missing an argument" keyword))
88 (setq args (cdr args))
89 (cond ((eq keyword :type)
90 (put symbol 'custom-type value))
91 ((eq keyword :options)
92 (put symbol 'custom-options
93 (append value (get symbol 'custom-options))))
95 (custom-add-to-group value symbol 'custom-variable))
97 (error "Unknown keyword %s" symbol)))))))
100 (defmacro defcustom (symbol value doc &rest args)
101 "Declare SYMBOL as a customizable variable that defaults to VALUE.
102 DOC is the variable documentation.
104 Neither SYMBOL nor VALUE needs to be quoted.
105 If SYMBOL is not already bound, initialize it to VALUE.
106 The remaining arguments should have the form
110 The following KEYWORD's are defined:
112 :type VALUE should be a widget type.
113 :options VALUE should be a list of valid members of the widget type.
114 :group VALUE should be a customization group.
115 Add SYMBOL to that group.
117 Read the section about customization in the emacs lisp manual for more
120 (custom-declare-variable (quote ,symbol) (quote ,value) ,doc ,@args)))
122 ;;; The `defface' Macro.
125 (defun custom-declare-face (face spec doc &rest args)
126 "Like `defface', but FACE is evaluated as a normal argument."
127 (put face 'factory-face spec)
128 (when (fboundp 'facep)
130 ;; If the user has already created the face, respect that.
131 (let ((value (or (get face 'saved-face) spec)))
132 (custom-face-display-set face value))))
134 (put face 'face-documentation doc))
136 (let ((arg (car args)))
137 (setq args (cdr args))
138 (unless (symbolp arg)
139 (error "Junk in args %S" args))
143 (error "Keyword %s is missing an argument" :type))
144 (setq args (cdr args))
145 (cond ((eq keyword :group)
146 (custom-add-to-group value face 'custom-face))
148 (error "Unknown keyword %s" face)))))))
151 (defmacro defface (face spec doc &rest args)
152 "Declare FACE as a customizable face that defaults to SPEC.
153 FACE does not need to be quoted.
155 Third argument DOC is the face documentation.
157 If FACE has been set with `custom-set-face', set the face attributes
158 as specified by that function, otherwise set the face attributes
161 The remaining arguments should have the form
165 The following KEYWORD's are defined:
167 :group VALUE should be a customization group.
168 Add FACE to that group.
170 SPEC should be an alist of the form ((DISPLAY ATTS)...).
172 ATTS is a list of face attributes and their values. The possible
173 attributes are defined in the variable `custom-face-attributes'.
174 Alternatively, ATTS can be a face in which case the attributes of that
177 The ATTS of the first entry in SPEC where the DISPLAY matches the
178 frame should take effect in that frame. DISPLAY can either be the
179 symbol `t', which will match all frames, or an alist of the form
182 For the DISPLAY to match a FRAME, the REQ property of the frame must
183 match one of the ITEM. The following REQ are defined:
185 `type' (the value of (window-system))
186 Should be one of `x' or `tty'.
188 `class' (the frame's color support)
189 Should be one of `color', `grayscale', or `mono'.
191 `background' (what color is used for the background text)
192 Should be one of `light' or `dark'.
194 Read the section about customization in the emacs lisp manual for more
196 `(custom-declare-face (quote ,face) ,spec ,doc ,@args))
198 ;;; The `defgroup' Macro.
201 (defun custom-declare-group (symbol members doc &rest args)
202 "Like `defgroup', but SYMBOL is evaluated as a normal argument."
203 (put symbol 'custom-group (nconc members (get symbol 'custom-group)))
205 (put symbol 'group-documentation doc))
207 (let ((arg (car args)))
208 (setq args (cdr args))
209 (unless (symbolp arg)
210 (error "Junk in args %S" args))
214 (error "Keyword %s is missing an argument" :type))
215 (setq args (cdr args))
216 (cond ((eq keyword :group)
217 (custom-add-to-group value symbol 'custom-group))
219 (error "Unknown keyword %s" symbol)))))))
222 (defmacro defgroup (symbol members doc &rest args)
223 "Declare SYMBOL as a customization group containing MEMBERS.
224 SYMBOL does not need to be quoted.
226 Third arg DOC is the group documentation.
228 MEMBERS should be an alist of the form ((NAME WIDGET)...) where
229 NAME is a symbol and WIDGET is a widget is a widget for editing that
230 symbol. Useful widgets are `custom-variable' for editing variables,
231 `custom-face' for edit faces, and `custom-group' for editing groups.
233 The remaining arguments should have the form
237 The following KEYWORD's are defined:
239 :group VALUE should be a customization group.
240 Add SYMBOL to that group.
242 Read the section about customization in the emacs lisp manual for more
244 `(custom-declare-group (quote ,symbol) ,members ,doc ,@args))
247 (defun custom-add-to-group (group option widget)
248 "To existing GROUP add a new OPTION of type WIDGET,
249 If there already is an entry for that option, overwrite it."
250 (let* ((members (get group 'custom-group))
251 (old (assq option members)))
253 (setcar (cdr old) widget)
254 (put group 'custom-group (nconc members (list (list option widget)))))))
258 (and (fboundp 'make-face)
259 (make-face 'custom-face-empty))
261 (defun custom-face-display-set (face spec &optional frame)
262 "Set FACE to the attributes to the first matching entry in SPEC.
263 Iff optional FRAME is non-nil, set it for that frame only.
264 See `defface' for information about SPEC."
265 (when (fboundp 'make-face)
267 (copy-face 'custom-face-empty face)
269 (let* ((entry (car spec))
270 (display (nth 0 entry))
271 (atts (nth 1 entry)))
272 (setq spec (cdr spec))
273 (when (custom-display-match-frame display frame)
274 (apply 'custom-face-attribites-set face frame atts)
277 (defcustom custom-background-mode nil
278 "The brightness of the background.
279 Set this to the symbol dark if your background color is dark, light if
280 your background is light, or nil (default) if you want Emacs to
281 examine the brightness for you."
283 :type '(choice (choice-item dark)
285 (choice-item :tag "default" nil)))
287 (defun custom-display-match-frame (display frame)
288 "Non-nil iff DISPLAY matches FRAME.
289 If FRAME is nil, the current FRAME is used."
290 ;; This is a kludge to get started, we really should use specifiers!
292 (setq frame (selected-frame)))
296 (pars (frame-parameters frame)))
297 (while (and display match)
298 (let* ((entry (car display))
300 (options (cdr entry)))
301 (setq display (cdr display))
302 (cond ((eq req 'type)
303 (let ((type (if (fboundp 'device-type)
306 (setq match (memq type options))))
308 (let ((class (if (fboundp 'device-class)
310 (cdr (assq 'display-type pars)))))
311 (setq match (memq class options))))
312 ((eq req 'background)
313 (let ((background (or custom-background-mode
314 (cdr (assq 'background-mode pars))
315 (custom-background-mode))))
316 (setq match (memq background options))))
318 (error "Unknown req `%S' with options `%S'" req options)))))
321 (defvar custom-face-attributes
322 '((:bold (toggle :format "Bold: %v") custom-set-face-bold)
323 (:italic (toggle :format "Italic: %v") custom-set-face-italic)
325 (toggle :format "Underline: %v") set-face-underline-p)
326 (:foreground (color :tag "Foreground") set-face-foreground)
327 (:background (color :tag "Background") set-face-background)
328 (:stipple (editable-field :format "Stipple: %v") set-face-stipple))
329 "Alist of face attributes.
331 The elements are of the form (KEY TYPE SET) where KEY is a symbol
332 identifying the attribute, TYPE is a widget type for editing the
333 attibute, SET is a function for setting the attribute value.
335 The SET function should take three arguments, the face to modify, the
336 value of the attribute, and optionally the frame where the face should
339 (defun custom-face-attribites-set (face frame &rest atts)
340 "For FACE on FRAME set the attributes [KEYWORD VALUE]....
341 Each keyword should be listed in `custom-face-attributes'.
343 If FRAME is nil, set the default face."
345 (let* ((name (nth 0 atts))
347 (fun (nth 2 (assq name custom-face-attributes))))
348 (setq atts (cdr (cdr atts)))
350 (funcall fun face value)
353 (defun custom-set-face-bold (face value &optional frame)
354 "Set the bold property of FACE to VALUE."
356 (make-face-bold face frame)
357 (make-face-unbold face frame)))
359 (defun custom-set-face-italic (face value &optional frame)
360 "Set the italic property of FACE to VALUE."
362 (make-face-italic face frame)
363 (make-face-unitalic face frame)))
366 (defun custom-initialize-faces (&optional frame)
367 "Initialize all custom faces for FRAME.
368 If FRAME is nil or omitted, initialize them for all frames."
369 (mapatoms (lambda (symbol)
370 (let ((spec (or (get symbol 'saved-face)
371 (get symbol 'factory-face))))
373 (custom-face-display-set symbol spec frame))))))
378 (defun custom-set-variables (&rest args)
379 "Initialize variables according to user preferences.
380 The arguments should have the form [SYMBOL VALUE]...
381 For each symbol, VALUE is evaluated and bound as the default value for
382 the symbol. The unevaluated VALUE is also stored as the saved value
385 (let ((symbol (nth 0 args))
386 (value (nth 1 args)))
387 (put symbol 'saved-value (list value)))
388 (setq args (cdr (cdr args)))))
391 (defun custom-set-faces (&rest args)
392 "Initialize faces according to user preferences.
393 The arguments should have the form [SYMBOL SPEC]...
394 For each symbol, a face with that name is created according to SPEC.
395 See `defface' for the format of SPEC."
397 (let ((face (nth 0 args))
399 (put face 'saved-face spec))
400 (setq args (cdr (cdr args)))))
402 ;;; Meta Customization
405 "Customization of the One True Editor.")
407 (defgroup customize nil
408 "Customization of the Customization support."
415 ;; custom.el ends here