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 (let ((keywords '(:type :group)))
24 (or (boundp (car keywords))
25 (set (car keywords) (car keywords)))
26 (setq keywords (cdr keywords))))
28 ;; These autoloads should be deleted when the file is added to Emacs
29 (autoload 'customize "custom-edit" nil t)
30 (autoload 'customize-variable "custom-edit" nil t)
31 (autoload 'customize-face "custom-edit" nil t)
32 (autoload 'customize-apropos "custom-edit" nil t)
36 (make-face 'custom-face-empty)
38 (defun custom-face-display-set (face spec &optional frame)
39 "Set FACE to the attributes to the first matching entry in SPEC.
40 Iff optional FRAME is non-nil, set it for that frame only.
41 See `defface' for information about SPEC."
43 (copy-face 'custom-face-empty face)
45 (let* ((entry (car spec))
46 (display (nth 0 entry))
48 (setq spec (cdr spec))
49 (when (custom-display-match-frame display frame)
50 (apply 'custom-face-attribites-set face frame atts)
53 (defun custom-display-match-frame (display frame)
54 "Non-nil iff DISPLAY matches FRAME.
55 If FRAME is nil, the current FRAME is used."
57 (setq frame (selected-frame)))
61 (pars (frame-parameters frame)))
62 (while (and display match)
63 (let* ((entry (car display))
65 (options (cdr entry)))
66 (setq display (cdr display))
68 (setq match (memq window-system options)))
70 (let ((class (cdr (assq 'display-type pars))))
71 (setq match (memq class options))))
73 (let ((background (cdr (assq 'background-mode pars))))
74 (setq match (memq background options))))
76 (error "Unknown req `%S' with options `%S'" req options)))))
79 (defvar custom-face-attributes
80 '((:bold (toggle :format "Bold: %v") custom-set-face-bold)
81 (:italic (toggle :format "Italic: %v") custom-set-face-italic)
83 (toggle :format "Underline: %v") set-face-underline-p)
84 (:foreground (color :tag "Foreground") set-face-foreground)
85 (:background (color :tag "Background") set-face-background)
86 (:stipple (field :format "Stipple: %v") set-face-stipple))
87 "Alist of face attributes.
89 The elements are of the form (KEY TYPE SET) where KEY is a symbol
90 identifying the attribute, TYPE is a widget type for editing the
91 attibute, SET is a function for setting the attribute value.
93 The SET function should take three arguments, the face to modify, the
94 value of the attribute, and optionally the frame where the face should
97 (defun custom-face-attribites-set (face frame &rest atts)
98 "For FACE on FRAME set the attributes [KEYWORD VALUE]....
99 Each keyword should be listed in `custom-face-attributes'.
101 If FRAME is nil, set the default face."
103 (let* ((name (nth 0 atts))
105 (fun (nth 2 (assq name custom-face-attributes))))
106 (setq atts (cdr (cdr atts)))
107 (funcall fun face value))))
109 (defun custom-set-face-bold (face value &optional frame)
110 "Set the bold property of FACE to VALUE."
113 (make-face-bold face frame)
114 (make-face-unbold face frame))
117 (defun custom-set-face-italic (face value &optional frame)
118 "Set the italic property of FACE to VALUE."
121 (make-face-italic face frame)
122 (make-face-unitalic face frame))
126 (defun custom-initialize-faces (&optional frame)
127 "Initialize all custom faces for FRAME.
128 If FRAME is nil or omitted, initialize them for all frames."
129 (mapatoms (lambda (symbol)
130 (let ((spec (or (get symbol 'saved-face)
131 (get symbol 'factory-face))))
133 (custom-face-display-set symbol spec frame))))))
135 ;;; The `defcustom' Macro.
138 (defun custom-declare-variable (symbol value &rest args)
139 "Like `defcustom', but SYMBOL and VALUE are evaluated as notmal arguments."
140 (unless (default-boundp symbol)
141 (set-default symbol (eval value)))
142 (put symbol 'factory-value (list value))
144 (let ((arg (car args)))
145 (setq args (cdr args))
150 (error "Keyword %s is missing an argument" keyword))
151 (setq args (cdr args))
152 (cond ((eq keyword :type)
153 (put symbol 'custom-type value))
155 (custom-add-to-group value symbol 'custom-variable))
157 (error "Unknown keyword %s" symbol)))))
159 (put symbol 'variable-documentation arg)
161 (error "Junk at end of args %s" args)))
163 (error "Junk in args %S"))))))
166 (defmacro defcustom (symbol value &rest args)
167 "Declare SYMBOL as a customizable variable that defaults to VALUE.
168 Neither SYMBOL nor VALUE needs to be quoted.
169 If SYMBOL is not already bound, initialize it to VALUE.
170 The remaining arguments should have the form
172 [KEYWORD VALUE]... DOC
174 where DOC is the variable documentation.
176 The following KEYWORD's are defined:
178 :type VALUE should be a sexp widget.
179 :group VALUE should be a customization group.
180 Add SYMBOL to that group.
182 Read the section about customization in the emacs lisp manual for more
184 `(custom-declare-variable (quote ,symbol) (quote ,value) ,@args))
186 ;;; The `defface' Macro.
189 (defun custom-declare-face (face spec &rest args)
190 "Like `defface', but FACE is evaluated as a normal argument."
191 (put face 'factory-face spec)
192 (let ((value (or (get face 'saved-face) spec)))
193 (custom-face-display-set face value))
195 (let ((arg (car args)))
196 (setq args (cdr args))
201 (error "Keyword %s is missing an argument" :type))
202 (setq args (cdr args))
203 (cond ((eq keyword :group)
204 (custom-add-to-group value face 'custom-face))
206 (error "Unknown keyword %s" face)))))
208 (put face 'face-documentation arg)
210 (error "Junk at end of args %s" args)))
212 (error "Junk in args %S"))))))
215 (defmacro defface (face spec &rest args)
216 "Declare FACE as a customizable face that defaults to SPEC.
217 FACE does not need to be quoted.
219 If FACE has been set with `custom-set-face', set the face attributes
220 as specified by that function, otherwise set the face attributes
223 The remaining arguments should have the form
225 [KEYWORD VALUE]... DOC
227 where DOC is the face documentation.
229 The following KEYWORD's are defined:
231 :group VALUE should be a customization group.
232 Add FACE to that group.
234 SPEC should be an alist of the form ((DISPLAY ATTS)...).
236 ATTS is a list of face attributes and their values. The possible
237 attributes are defined in the variable `custom-face-attributes'.
238 Alternatively, ATTS can be a face in which case the attributes of that
241 The ATTS of the first entry in SPEC where the DISPLAY matches the
242 frame should take effect in that frame. DISPLAY can either be the
243 symbol `t', which will match all frames, or an alist of the form
246 For the DISPLAY to match a FRAME, the REQ property of the frame must
247 match one of the ITEM. The following REQ are defined:
249 `type' (the value of (window-system))
250 Should be one of `x' or `tty'.
252 `class' (the frame's color support)
253 Should be one of `color', `grayscale', or `mono'.
255 `background' (what color is used for the background text)
256 Should be one of `light' or `dark'.
258 Read the section about customization in the emacs lisp manual for more
260 `(custom-declare-face (quote ,face) ,spec ,@args))
262 ;;; The `defgroup' Macro.
265 (defun custom-declare-group (symbol members &rest args)
266 "Like `defgroup', but SYMBOL is evaluated as a normal argument."
267 (put symbol 'custom-group (nconc members (get symbol 'custom-group)))
269 (let ((arg (car args)))
270 (setq args (cdr args))
275 (error "Keyword %s is missing an argument" :type))
276 (setq args (cdr args))
277 (cond ((eq keyword :group)
278 (custom-add-to-group value symbol 'custom-group))
280 (error "Unknown keyword %s" symbol)))))
282 (put symbol 'group-documentation arg)
284 (error "Junk at end of args %s" args)))
286 (error "Junk in args %S"))))))
289 (defmacro defgroup (symbol members &rest args)
290 "Declare SYMBOL as a customization group containing MEMBERS.
291 SYMBOL does not need to be quoted.
293 MEMBERS should be an alist of the form ((NAME WIDGET)...) where
294 NAME is a symbol and WIDGET is a widget is a widget for editing that
295 symbol. Useful widgets are `custom-variable' for editing variables,
296 `custom-face' for edit faces, and `custom-group' for editing groups.
298 The remaining arguments should have the form
300 [KEYWORD VALUE]... DOC
302 where DOC is the group documentation.
304 The following KEYWORD's are defined:
306 :group VALUE should be a customization group.
307 Add SYMBOL to that group.
309 Read the section about customization in the emacs lisp manual for more
311 `(custom-declare-group (quote ,symbol) ,members ,@args))
314 (defun custom-add-to-group (group option widget)
315 "To existing GROUP add a new OPTION of type WIDGET,
316 If there already is an entry for that option, overwrite it."
317 (let* ((members (get group 'custom-group))
318 (old (assq option members)))
320 (setcar (cdr old) widget)
321 (put group 'custom-group (nconc members (list (list option widget)))))))
326 (defun custom-set-variables (&rest args)
327 "Initialize variables according to user preferences.
328 The arguments should have the form [SYMBOL VALUE]...
329 For each symbol, VALUE is evaluated and bound as the default value for
330 the symbol. The unevaluated VALUE is also stored as the saved value
333 (let ((symbol (nth 0 args))
334 (value (nth 1 args)))
335 (set-default symbol (eval value))
336 (put symbol 'saved-value (list value)))
337 (setq args (cdr (cdr args)))))
340 (defun custom-set-faces (&rest args)
341 "Initialize faces according to user preferences.
342 The arguments should have the form [SYMBOL SPEC]...
343 For each symbol, a face with that name is created according to SPEC.
344 See `defface' for the format of SPEC."
346 (let ((face (nth 0 args))
348 (put face 'saved-face spec)
349 (custom-face-display-set face spec))
350 (setq args (cdr (cdr args)))))
352 ;;; Meta Customization
355 "Customization of the One True Editor.")
357 (defgroup customize nil
359 "Customization of the Customization support.")
365 ;; custom.el ends here