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 :prefix :tag :load :link :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")
31 (autoload 'custom-menu-update "custom-edit")
32 (autoload 'custom-make-dependencies "custom-edit")
36 (unless (fboundp 'x-color-values)
37 ;; Emacs function missing in XEmacs 19.14.
38 (defun x-color-values (color)
39 "Return a description of the color named COLOR on frame FRAME.
40 The value is a list of integer RGB values--(RED GREEN BLUE).
41 These values appear to range from 0 to 65280 or 65535, depending
42 on the system; white is (65280 65280 65280) or (65535 65535 65535).
43 If FRAME is omitted or nil, use the selected frame."
44 (color-instance-rgb-components (make-color-instance color))))
46 (unless (fboundp 'frame-property)
47 ;; XEmacs function missing in Emacs 19.34.
48 (defun frame-property (frame property &optional default)
49 "Return FRAME's value for property PROPERTY."
50 (or (cdr (assq property (frame-parameters frame)))
53 (defun custom-background-mode ()
54 "Kludge to detext background mode."
57 (x-get-resource ".backgroundMode" "BackgroundMode" 'string)
60 (mode (cond (bg-resource
61 (intern (downcase bg-resource)))
62 ((and (setq color (condition-case ()
68 (face-background 'default))))
70 (< (apply '+ (x-color-values color))
71 (/ (apply '+ (x-color-values "white"))
75 (modify-frame-parameters (selected-frame)
76 (list (cons 'background-mode mode)))
79 ;; XEmacs and Emacs have different definitions of `facep'.
80 ;; The Emacs definition is the useful one, so emulate that.
81 (cond ((not (fboundp 'facep))
82 (defun custom-facep (face)
85 ((string-match "XEmacs" emacs-version)
86 (defun custom-facep (face)
87 "Face symbol or object."
91 (defalias 'custom-facep 'facep)))
93 ;;; The `defcustom' Macro.
96 (defun custom-declare-variable (symbol value doc &rest args)
97 "Like `defcustom', but SYMBOL and VALUE are evaluated as notmal arguments."
98 (unless (and (default-boundp symbol)
99 (not (get symbol 'saved-value)))
100 (set-default symbol (if (get symbol 'saved-value)
101 (eval (car (get symbol 'saved-value)))
103 (put symbol 'factory-value (list value))
105 (put symbol 'variable-documentation doc))
107 (let ((arg (car args)))
108 (setq args (cdr args))
109 (unless (symbolp arg)
110 (error "Junk in args %S" args))
114 (error "Keyword %s is missing an argument" keyword))
115 (setq args (cdr args))
116 (cond ((eq keyword :type)
117 (put symbol 'custom-type value))
118 ((eq keyword :options)
119 (if (get symbol 'custom-options)
120 ;; Slow safe code to avoid duplicates.
121 (mapcar (lambda (option)
122 (custom-add-option symbol option))
124 ;; Fast code for the common case.
125 (put symbol 'custom-options (copy-list value))))
127 (custom-handle-keyword symbol keyword value
128 'custom-variable))))))
129 (run-hooks 'custom-define-hook)
133 (defmacro defcustom (symbol value doc &rest args)
134 "Declare SYMBOL as a customizable variable that defaults to VALUE.
135 DOC is the variable documentation.
137 Neither SYMBOL nor VALUE needs to be quoted.
138 If SYMBOL is not already bound, initialize it to VALUE.
139 The remaining arguments should have the form
143 The following KEYWORD's are defined:
145 :type VALUE should be a widget type.
146 :options VALUE should be a list of valid members of the widget type.
147 :group VALUE should be a customization group.
148 Add SYMBOL to that group.
150 Read the section about customization in the emacs lisp manual for more
153 (custom-declare-variable (quote ,symbol) (quote ,value) ,doc ,@args)))
155 ;;; The `defface' Macro.
158 (defun custom-declare-face (face spec doc &rest args)
159 "Like `defface', but FACE is evaluated as a normal argument."
160 (put face 'factory-face spec)
161 (when (fboundp 'facep)
162 (unless (and (custom-facep face)
163 (not (get face 'saved-face)))
164 ;; If the user has already created the face, respect that.
165 (let ((value (or (get face 'saved-face) spec)))
166 (custom-face-display-set face value))))
168 (put face 'face-documentation doc))
169 (custom-handle-all-keywords face args 'custom-face)
170 (run-hooks 'custom-define-hook)
174 (defmacro defface (face spec doc &rest args)
175 "Declare FACE as a customizable face that defaults to SPEC.
176 FACE does not need to be quoted.
178 Third argument DOC is the face documentation.
180 If FACE has been set with `custom-set-face', set the face attributes
181 as specified by that function, otherwise set the face attributes
184 The remaining arguments should have the form
188 The following KEYWORD's are defined:
190 :group VALUE should be a customization group.
191 Add FACE to that group.
193 SPEC should be an alist of the form ((DISPLAY ATTS)...).
195 ATTS is a list of face attributes and their values. The possible
196 attributes are defined in the variable `custom-face-attributes'.
197 Alternatively, ATTS can be a face in which case the attributes of that
200 The ATTS of the first entry in SPEC where the DISPLAY matches the
201 frame should take effect in that frame. DISPLAY can either be the
202 symbol `t', which will match all frames, or an alist of the form
205 For the DISPLAY to match a FRAME, the REQ property of the frame must
206 match one of the ITEM. The following REQ are defined:
208 `type' (the value of (window-system))
209 Should be one of `x' or `tty'.
211 `class' (the frame's color support)
212 Should be one of `color', `grayscale', or `mono'.
214 `background' (what color is used for the background text)
215 Should be one of `light' or `dark'.
217 Read the section about customization in the emacs lisp manual for more
219 `(custom-declare-face (quote ,face) ,spec ,doc ,@args))
221 ;;; The `defgroup' Macro.
224 (defun custom-declare-group (symbol members doc &rest args)
225 "Like `defgroup', but SYMBOL is evaluated as a normal argument."
226 (put symbol 'custom-group (nconc members (get symbol 'custom-group)))
228 (put symbol 'group-documentation doc))
230 (let ((arg (car args)))
231 (setq args (cdr args))
232 (unless (symbolp arg)
233 (error "Junk in args %S" args))
237 (error "Keyword %s is missing an argument" keyword))
238 (setq args (cdr args))
239 (cond ((eq keyword :prefix)
240 (put symbol 'custom-prefix value))
242 (custom-handle-keyword symbol keyword value
244 (run-hooks 'custom-define-hook)
248 (defmacro defgroup (symbol members doc &rest args)
249 "Declare SYMBOL as a customization group containing MEMBERS.
250 SYMBOL does not need to be quoted.
252 Third arg DOC is the group documentation.
254 MEMBERS should be an alist of the form ((NAME WIDGET)...) where
255 NAME is a symbol and WIDGET is a widget is a widget for editing that
256 symbol. Useful widgets are `custom-variable' for editing variables,
257 `custom-face' for edit faces, and `custom-group' for editing groups.
259 The remaining arguments should have the form
263 The following KEYWORD's are defined:
265 :group VALUE should be a customization group.
266 Add SYMBOL to that group.
268 Read the section about customization in the emacs lisp manual for more
270 `(custom-declare-group (quote ,symbol) ,members ,doc ,@args))
273 (defun custom-add-to-group (group option widget)
274 "To existing GROUP add a new OPTION of type WIDGET,
275 If there already is an entry for that option, overwrite it."
276 (let* ((members (get group 'custom-group))
277 (old (assq option members)))
279 (setcar (cdr old) widget)
280 (put group 'custom-group (nconc members (list (list option widget)))))))
284 (defun custom-handle-all-keywords (symbol args type)
285 "For customization option SYMBOL, handle keyword arguments ARGS.
286 Third argument TYPE is the custom option type."
288 (let ((arg (car args)))
289 (setq args (cdr args))
290 (unless (symbolp arg)
291 (error "Junk in args %S" args))
295 (error "Keyword %s is missing an argument" keyword))
296 (setq args (cdr args))
297 (custom-handle-keyword symbol keyword value type)))))
299 (defun custom-handle-keyword (symbol keyword value type)
300 "For customization option SYMBOL, handle KEYWORD with VALUE.
301 Fourth argument TYPE is the custom option type."
302 (cond ((eq keyword :group)
303 (custom-add-to-group value symbol type))
305 (custom-add-link symbol value))
307 (custom-add-load symbol value))
309 (put symbol 'custom-tag value))
311 (error "Unknown keyword %s" symbol))))
313 (defun custom-add-option (symbol option)
314 "To the variable SYMBOL add OPTION.
316 If SYMBOL is a hook variable, OPTION should be a hook member.
317 For other types variables, the effect is undefined."
318 (let ((options (get symbol 'custom-options)))
319 (unless (member option options)
320 (put symbol 'custom-options (cons option options)))))
322 (defun custom-add-link (symbol widget)
323 "To the custom option SYMBOL add the link WIDGET."
324 (let ((links (get symbol 'custom-links)))
325 (unless (member widget links)
326 (put symbol 'custom-links (cons widget links)))))
328 (defun custom-add-load (symbol load)
329 "To the custom option SYMBOL add the dependency LOAD.
330 LOAD should be either a library file name, or a feature name."
331 (let ((loads (get symbol 'custom-loads)))
332 (unless (member load loads)
333 (put symbol 'custom-loads (cons load loads)))))
337 (and (fboundp 'make-face)
338 (make-face 'custom-face-empty))
340 (defun custom-face-display-set (face spec &optional frame)
341 "Set FACE to the attributes to the first matching entry in SPEC.
342 Iff optional FRAME is non-nil, set it for that frame only.
343 See `defface' for information about SPEC."
344 (when (fboundp 'copy-face)
345 (copy-face 'custom-face-empty face)
347 (let* ((entry (car spec))
348 (display (nth 0 entry))
349 (atts (nth 1 entry)))
350 (setq spec (cdr spec))
351 (when (custom-display-match-frame display frame)
352 (apply 'custom-face-attribites-set face frame atts)
355 (defcustom custom-background-mode nil
356 "The brightness of the background.
357 Set this to the symbol dark if your background color is dark, light if
358 your background is light, or nil (default) if you want Emacs to
359 examine the brightness for you."
361 :type '(choice (choice-item dark)
363 (choice-item :tag "default" nil)))
365 (defun custom-display-match-frame (display frame)
366 "Non-nil iff DISPLAY matches FRAME.
367 If FRAME is nil, the current FRAME is used."
368 ;; This is a kludge to get started, we really should use specifiers!
370 (setq frame (selected-frame)))
374 (while (and display match)
375 (let* ((entry (car display))
377 (options (cdr entry)))
378 (setq display (cdr display))
379 (cond ((eq req 'type)
380 (let ((type (if (fboundp 'device-type)
381 (device-type (frame-device frame))
383 (setq match (memq type options))))
385 (let ((class (if (fboundp 'device-class)
386 (device-class (frame-device frame))
387 (frame-property frame 'display-type))))
388 (setq match (memq class options))))
389 ((eq req 'background)
390 (let ((background (or custom-background-mode
391 (frame-property frame 'background-mode)
392 (custom-background-mode))))
393 (setq match (memq background options))))
395 (error "Unknown req `%S' with options `%S'" req options)))))
398 (defconst custom-face-attributes
399 '((:bold (toggle :format "Bold: %v") custom-set-face-bold)
400 (:italic (toggle :format "Italic: %v") custom-set-face-italic)
402 (toggle :format "Underline: %v") set-face-underline-p)
403 (:foreground (color :tag "Foreground") set-face-foreground)
404 (:background (color :tag "Background") set-face-background)
405 (:stipple (editable-field :format "Stipple: %v") set-face-stipple))
406 "Alist of face attributes.
408 The elements are of the form (KEY TYPE SET) where KEY is a symbol
409 identifying the attribute, TYPE is a widget type for editing the
410 attibute, SET is a function for setting the attribute value.
412 The SET function should take three arguments, the face to modify, the
413 value of the attribute, and optionally the frame where the face should
416 (when (string-match "XEmacs" emacs-version)
417 ;; Support for special XEmacs font attributes.
420 (unless (fboundp 'face-font-name)
421 (defun face-font-name (face &rest args)
422 (apply 'face-font face args)))
424 (defun set-face-font-size (face size &rest args)
425 "Set the font of FACE to SIZE"
426 (let* ((font (apply 'face-font-name face args))
427 (fontobj (font-create-object font)))
428 (set-font-size fontobj size)
429 (apply 'set-face-font face fontobj args)))
431 (defun set-face-font-family (face family &rest args)
432 "Set the font of FACE to FAMILY"
433 (let* ((font (apply 'face-font-name face args))
434 (fontobj (font-create-object font)))
435 (set-font-family fontobj family)
436 (apply 'set-face-font face fontobj args)))
438 (nconc custom-face-attributes
439 '((:family (editable-field :format "Family: %v")
440 set-face-font-family)
441 (:size (editable-field :format "Size: %v")
442 set-face-font-size))))
444 (defun custom-face-attribites-set (face frame &rest atts)
445 "For FACE on FRAME set the attributes [KEYWORD VALUE]....
446 Each keyword should be listed in `custom-face-attributes'.
448 If FRAME is nil, set the default face."
450 (let* ((name (nth 0 atts))
452 (fun (nth 2 (assq name custom-face-attributes))))
453 (setq atts (cdr (cdr atts)))
455 (funcall fun face value)
458 (defun custom-set-face-bold (face value &optional frame)
459 "Set the bold property of FACE to VALUE."
461 (make-face-bold face frame)
462 (make-face-unbold face frame)))
464 (defun custom-set-face-italic (face value &optional frame)
465 "Set the italic property of FACE to VALUE."
467 (make-face-italic face frame)
468 (make-face-unitalic face frame)))
471 (defun custom-initialize-faces (&optional frame)
472 "Initialize all custom faces for FRAME.
473 If FRAME is nil or omitted, initialize them for all frames."
474 (mapatoms (lambda (symbol)
475 (let ((spec (or (get symbol 'saved-face)
476 (get symbol 'factory-face))))
478 (custom-face-display-set symbol spec frame))))))
483 (defun custom-set-variables (&rest args)
484 "Initialize variables according to user preferences.
486 The arguments should be a list where each entry has the form:
490 The unevaluated VALUE is stored as the saved value for SYMBOL.
491 If NOW is present and non-nil, VALUE is also evaluated and bound as
492 the default value for the SYMBOL."
494 (let ((entry (car args)))
496 (let ((symbol (nth 0 entry))
497 (value (nth 1 entry))
499 (put symbol 'saved-value (list value))
501 (put symbol 'force-value t)
502 (set-default symbol (eval value)))
503 (setq args (cdr args)))
504 ;; Old format, a plist of SYMBOL VALUE pairs.
505 (let ((symbol (nth 0 args))
506 (value (nth 1 args)))
507 (put symbol 'saved-value (list value)))
508 (setq args (cdr (cdr args)))))))
511 (defun custom-set-faces (&rest args)
512 "Initialize faces according to user preferences.
513 The arguments should be a list where each entry has the form:
517 SPEC will be stored as the saved value for FACE. If NOW is present
518 and non-nil, FACE will also be created according to SPEC.
520 See `defface' for the format of SPEC."
522 (let ((entry (car args)))
524 (let ((face (nth 0 entry))
527 (put face 'saved-face spec)
529 (put face 'force-face t)
530 (custom-face-display-set face spec))
531 (setq args (cdr args)))
532 ;; Old format, a plist of FACE SPEC pairs.
533 (let ((face (nth 0 args))
535 (put face 'saved-face spec))
536 (setq args (cdr (cdr args)))))))
538 ;;; Meta Customization
541 "Customization of the One True Editor."
542 :link '(custom-manual "(emacs)Top"))
544 (defgroup customize '((widgets custom-group))
545 "Customization of the Customization support."
546 :link '(custom-manual "(custom)Top")
547 :link '(url-link :tag "Development Page"
548 "http://www.dina.kvl.dk/~abraham/custom/")
552 (defcustom custom-define-hook nil
553 "Hook called after defining each customize option."
559 (defconst custom-help-menu '("Customize"
560 ["Update menu..." custom-menu-update t]
561 ["Group..." customize t]
562 ["Variable..." customize-variable t]
563 ["Face..." customize-face t]
564 ["Saved..." customize-customized t]
565 ["Apropos..." customize-apropos t])
568 (defun custom-menu-reset ()
569 "Reset customize menu."
570 (remove-hook 'custom-define-hook 'custom-menu-reset)
571 (cond ((fboundp 'add-submenu)
572 ;; XEmacs with menus.
573 (add-submenu '("Help") custom-help-menu))
574 ((string-match "XEmacs" emacs-version)
575 ;; XEmacs without menus.
579 (define-key global-map [menu-bar help-menu customize-menu]
580 (cons (car custom-help-menu)
581 (easy-menu-create-keymaps (car custom-help-menu)
582 (cdr custom-help-menu)))))))
590 ;; custom.el ends here