98
|
1 ;;; custom.el -- Tools for declaring and initializing options.
|
|
2 ;;
|
|
3 ;; Copyright (C) 1996, 1997 Free Software Foundation, Inc.
|
|
4 ;;
|
|
5 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
|
|
6 ;; Keywords: help, faces
|
102
|
7 ;; Version: 1.46
|
98
|
8 ;; X-URL: http://www.dina.kvl.dk/~abraham/custom/
|
|
9
|
|
10 ;;; Commentary:
|
|
11 ;;
|
|
12 ;; If you want to use this code, please visit the URL above.
|
|
13 ;;
|
|
14 ;; This file only contain the code needed to declare and initialize
|
|
15 ;; user options. The code to customize options is autoloaded from
|
|
16 ;; `custom-edit.el'.
|
|
17
|
|
18 ;;; Code:
|
|
19
|
|
20 (require 'widget)
|
|
21
|
|
22 (define-widget-keywords :prefix :tag :load :link :options :type :group)
|
|
23
|
|
24 ;; These autoloads should be deleted when the file is added to Emacs
|
|
25
|
|
26 (unless (fboundp 'load-gc)
|
|
27 (autoload 'customize "custom-edit" nil t)
|
|
28 (autoload 'customize-variable "custom-edit" nil t)
|
|
29 (autoload 'customize-face "custom-edit" nil t)
|
|
30 (autoload 'customize-apropos "custom-edit" nil t)
|
|
31 (autoload 'customize-customized "custom-edit" nil t)
|
|
32 (autoload 'custom-buffer-create "custom-edit")
|
|
33 (autoload 'custom-menu-update "custom-edit")
|
|
34 (autoload 'custom-make-dependencies "custom-edit"))
|
|
35
|
|
36 ;;; Compatibility.
|
|
37
|
|
38 (unless (fboundp 'frame-property)
|
|
39 ;; XEmacs function missing in Emacs 19.34.
|
|
40 (defun frame-property (frame property &optional default)
|
|
41 "Return FRAME's value for property PROPERTY."
|
|
42 (or (cdr (assq property (frame-parameters frame)))
|
|
43 default)))
|
|
44
|
|
45 (defun custom-background-mode ()
|
100
|
46 "Kludge to detect background mode."
|
98
|
47 (let* ((bg-resource
|
|
48 (condition-case ()
|
|
49 (x-get-resource ".backgroundMode" "BackgroundMode" 'string)
|
|
50 (error nil)))
|
|
51 color
|
|
52 (mode (cond (bg-resource
|
|
53 (intern (downcase bg-resource)))
|
|
54 ((and (setq color (condition-case ()
|
|
55 (or (frame-property
|
|
56 (selected-frame)
|
|
57 'background-color)
|
|
58 (color-instance-name
|
|
59 (specifier-instance
|
|
60 (face-background 'default))))
|
|
61 (error nil)))
|
|
62 (< (apply '+ (x-color-values color))
|
|
63 (/ (apply '+ (x-color-values "white"))
|
|
64 3)))
|
|
65 'dark)
|
|
66 (t 'light))))
|
|
67 (modify-frame-parameters (selected-frame)
|
|
68 (list (cons 'background-mode mode)))
|
|
69 mode))
|
|
70
|
|
71 ;; XEmacs and Emacs have different definitions of `facep'.
|
|
72 ;; The Emacs definition is the useful one, so emulate that.
|
100
|
73 (if (fboundp 'facep)
|
|
74 (defalias 'custom-facep 'facep)
|
|
75 (defun custom-facep (face)
|
|
76 "No faces"
|
|
77 nil))
|
98
|
78
|
|
79 ;;; The `defcustom' Macro.
|
|
80
|
|
81 (defun custom-declare-variable (symbol value doc &rest args)
|
100
|
82 "Like `defcustom', but SYMBOL and VALUE are evaluated as normal arguments."
|
98
|
83 (unless (and (default-boundp symbol)
|
|
84 (not (get symbol 'saved-value)))
|
|
85 (set-default symbol (if (get symbol 'saved-value)
|
|
86 (eval (car (get symbol 'saved-value)))
|
|
87 (eval value))))
|
|
88 (put symbol 'factory-value (list value))
|
|
89 (when doc
|
|
90 (put symbol 'variable-documentation doc))
|
|
91 (while args
|
|
92 (let ((arg (car args)))
|
|
93 (setq args (cdr args))
|
|
94 (unless (symbolp arg)
|
|
95 (error "Junk in args %S" args))
|
|
96 (let ((keyword arg)
|
|
97 (value (car args)))
|
|
98 (unless args
|
|
99 (error "Keyword %s is missing an argument" keyword))
|
|
100 (setq args (cdr args))
|
|
101 (cond ((eq keyword :type)
|
|
102 (put symbol 'custom-type value))
|
|
103 ((eq keyword :options)
|
|
104 (if (get symbol 'custom-options)
|
|
105 ;; Slow safe code to avoid duplicates.
|
|
106 (mapcar (lambda (option)
|
|
107 (custom-add-option symbol option))
|
|
108 value)
|
|
109 ;; Fast code for the common case.
|
|
110 (put symbol 'custom-options (copy-list value))))
|
|
111 (t
|
|
112 (custom-handle-keyword symbol keyword value
|
|
113 'custom-variable))))))
|
|
114 (run-hooks 'custom-define-hook)
|
|
115 symbol)
|
|
116
|
|
117 (defmacro defcustom (symbol value doc &rest args)
|
|
118 "Declare SYMBOL as a customizable variable that defaults to VALUE.
|
|
119 DOC is the variable documentation.
|
|
120
|
|
121 Neither SYMBOL nor VALUE needs to be quoted.
|
|
122 If SYMBOL is not already bound, initialize it to VALUE.
|
|
123 The remaining arguments should have the form
|
|
124
|
|
125 [KEYWORD VALUE]...
|
|
126
|
|
127 The following KEYWORD's are defined:
|
|
128
|
|
129 :type VALUE should be a widget type.
|
|
130 :options VALUE should be a list of valid members of the widget type.
|
|
131 :group VALUE should be a customization group.
|
|
132 Add SYMBOL to that group.
|
|
133
|
|
134 Read the section about customization in the emacs lisp manual for more
|
|
135 information."
|
|
136 `(eval-and-compile
|
|
137 (custom-declare-variable (quote ,symbol) (quote ,value) ,doc ,@args)))
|
|
138
|
|
139 ;;; The `defface' Macro.
|
|
140
|
100
|
141
|
|
142 ;(defun get-face-documentation (face)
|
|
143 ; "Get the documentation string for FACE."
|
|
144 ; (get face 'face-documentation))
|
|
145
|
|
146 ;(defun set-face-documentation (face string)
|
|
147 ; "Set the documentation string for FACE to STRING."
|
|
148 ; (put face 'face-documentation string))
|
|
149
|
98
|
150 (defun custom-declare-face (face spec doc &rest args)
|
|
151 "Like `defface', but FACE is evaluated as a normal argument."
|
|
152 (put face 'factory-face spec)
|
|
153 (when (fboundp 'facep)
|
|
154 (unless (and (custom-facep face)
|
|
155 (not (get face 'saved-face)))
|
|
156 ;; If the user has already created the face, respect that.
|
|
157 (let ((value (or (get face 'saved-face) spec)))
|
|
158 (custom-face-display-set face value))))
|
100
|
159 (when (and doc (null (get-face-documentation face)))
|
|
160 (set-face-documentation face doc))
|
98
|
161 (custom-handle-all-keywords face args 'custom-face)
|
|
162 (run-hooks 'custom-define-hook)
|
|
163 face)
|
|
164
|
|
165 (defmacro defface (face spec doc &rest args)
|
|
166 "Declare FACE as a customizable face that defaults to SPEC.
|
|
167 FACE does not need to be quoted.
|
|
168
|
|
169 Third argument DOC is the face documentation.
|
|
170
|
|
171 If FACE has been set with `custom-set-face', set the face attributes
|
|
172 as specified by that function, otherwise set the face attributes
|
|
173 according to SPEC.
|
|
174
|
|
175 The remaining arguments should have the form
|
|
176
|
|
177 [KEYWORD VALUE]...
|
|
178
|
|
179 The following KEYWORD's are defined:
|
|
180
|
|
181 :group VALUE should be a customization group.
|
|
182 Add FACE to that group.
|
|
183
|
|
184 SPEC should be an alist of the form ((DISPLAY ATTS)...).
|
|
185
|
|
186 ATTS is a list of face attributes and their values. The possible
|
|
187 attributes are defined in the variable `custom-face-attributes'.
|
|
188 Alternatively, ATTS can be a face in which case the attributes of that
|
|
189 face is used.
|
|
190
|
|
191 The ATTS of the first entry in SPEC where the DISPLAY matches the
|
|
192 frame should take effect in that frame. DISPLAY can either be the
|
|
193 symbol `t', which will match all frames, or an alist of the form
|
|
194 \((REQ ITEM...)...)
|
|
195
|
|
196 For the DISPLAY to match a FRAME, the REQ property of the frame must
|
|
197 match one of the ITEM. The following REQ are defined:
|
|
198
|
|
199 `type' (the value of (window-system))
|
|
200 Should be one of `x' or `tty'.
|
|
201
|
|
202 `class' (the frame's color support)
|
|
203 Should be one of `color', `grayscale', or `mono'.
|
|
204
|
|
205 `background' (what color is used for the background text)
|
|
206 Should be one of `light' or `dark'.
|
|
207
|
|
208 Read the section about customization in the emacs lisp manual for more
|
|
209 information."
|
|
210 `(custom-declare-face (quote ,face) ,spec ,doc ,@args))
|
|
211
|
|
212 ;;; The `defgroup' Macro.
|
|
213
|
|
214 (defun custom-declare-group (symbol members doc &rest args)
|
|
215 "Like `defgroup', but SYMBOL is evaluated as a normal argument."
|
|
216 (put symbol 'custom-group (nconc members (get symbol 'custom-group)))
|
|
217 (when doc
|
|
218 (put symbol 'group-documentation doc))
|
|
219 (while args
|
|
220 (let ((arg (car args)))
|
|
221 (setq args (cdr args))
|
|
222 (unless (symbolp arg)
|
|
223 (error "Junk in args %S" args))
|
|
224 (let ((keyword arg)
|
|
225 (value (car args)))
|
|
226 (unless args
|
|
227 (error "Keyword %s is missing an argument" keyword))
|
|
228 (setq args (cdr args))
|
|
229 (cond ((eq keyword :prefix)
|
|
230 (put symbol 'custom-prefix value))
|
|
231 (t
|
|
232 (custom-handle-keyword symbol keyword value
|
|
233 'custom-group))))))
|
|
234 (run-hooks 'custom-define-hook)
|
|
235 symbol)
|
|
236
|
|
237 (defmacro defgroup (symbol members doc &rest args)
|
|
238 "Declare SYMBOL as a customization group containing MEMBERS.
|
|
239 SYMBOL does not need to be quoted.
|
|
240
|
|
241 Third arg DOC is the group documentation.
|
|
242
|
|
243 MEMBERS should be an alist of the form ((NAME WIDGET)...) where
|
|
244 NAME is a symbol and WIDGET is a widget is a widget for editing that
|
|
245 symbol. Useful widgets are `custom-variable' for editing variables,
|
|
246 `custom-face' for edit faces, and `custom-group' for editing groups.
|
|
247
|
|
248 The remaining arguments should have the form
|
|
249
|
|
250 [KEYWORD VALUE]...
|
|
251
|
|
252 The following KEYWORD's are defined:
|
|
253
|
|
254 :group VALUE should be a customization group.
|
|
255 Add SYMBOL to that group.
|
|
256
|
|
257 Read the section about customization in the emacs lisp manual for more
|
|
258 information."
|
|
259 `(custom-declare-group (quote ,symbol) ,members ,doc ,@args))
|
|
260
|
|
261 (defun custom-add-to-group (group option widget)
|
|
262 "To existing GROUP add a new OPTION of type WIDGET,
|
|
263 If there already is an entry for that option, overwrite it."
|
|
264 (let* ((members (get group 'custom-group))
|
|
265 (old (assq option members)))
|
|
266 (if old
|
|
267 (setcar (cdr old) widget)
|
|
268 (put group 'custom-group (nconc members (list (list option widget)))))))
|
|
269
|
|
270 ;;; Properties.
|
|
271
|
|
272 (defun custom-handle-all-keywords (symbol args type)
|
|
273 "For customization option SYMBOL, handle keyword arguments ARGS.
|
|
274 Third argument TYPE is the custom option type."
|
|
275 (while args
|
|
276 (let ((arg (car args)))
|
|
277 (setq args (cdr args))
|
|
278 (unless (symbolp arg)
|
|
279 (error "Junk in args %S" args))
|
|
280 (let ((keyword arg)
|
|
281 (value (car args)))
|
|
282 (unless args
|
|
283 (error "Keyword %s is missing an argument" keyword))
|
|
284 (setq args (cdr args))
|
|
285 (custom-handle-keyword symbol keyword value type)))))
|
|
286
|
|
287 (defun custom-handle-keyword (symbol keyword value type)
|
|
288 "For customization option SYMBOL, handle KEYWORD with VALUE.
|
|
289 Fourth argument TYPE is the custom option type."
|
|
290 (cond ((eq keyword :group)
|
|
291 (custom-add-to-group value symbol type))
|
|
292 ((eq keyword :link)
|
|
293 (custom-add-link symbol value))
|
|
294 ((eq keyword :load)
|
|
295 (custom-add-load symbol value))
|
|
296 ((eq keyword :tag)
|
|
297 (put symbol 'custom-tag value))
|
|
298 (t
|
|
299 (error "Unknown keyword %s" symbol))))
|
|
300
|
|
301 (defun custom-add-option (symbol option)
|
|
302 "To the variable SYMBOL add OPTION.
|
|
303
|
|
304 If SYMBOL is a hook variable, OPTION should be a hook member.
|
|
305 For other types variables, the effect is undefined."
|
|
306 (let ((options (get symbol 'custom-options)))
|
|
307 (unless (member option options)
|
|
308 (put symbol 'custom-options (cons option options)))))
|
|
309
|
|
310 (defun custom-add-link (symbol widget)
|
|
311 "To the custom option SYMBOL add the link WIDGET."
|
|
312 (let ((links (get symbol 'custom-links)))
|
|
313 (unless (member widget links)
|
|
314 (put symbol 'custom-links (cons widget links)))))
|
|
315
|
|
316 (defun custom-add-load (symbol load)
|
|
317 "To the custom option SYMBOL add the dependency LOAD.
|
|
318 LOAD should be either a library file name, or a feature name."
|
|
319 (let ((loads (get symbol 'custom-loads)))
|
|
320 (unless (member load loads)
|
|
321 (put symbol 'custom-loads (cons load loads)))))
|
|
322
|
|
323 ;;; Face Utilities.
|
|
324
|
|
325 (and (fboundp 'make-face)
|
|
326 (make-face 'custom-face-empty))
|
|
327
|
|
328 (defun custom-face-display-set (face spec &optional frame)
|
|
329 "Set FACE to the attributes to the first matching entry in SPEC.
|
|
330 Iff optional FRAME is non-nil, set it for that frame only.
|
|
331 See `defface' for information about SPEC."
|
|
332 (when (fboundp 'copy-face)
|
100
|
333 (copy-face 'custom-face-empty face frame)
|
98
|
334 (while spec
|
|
335 (let* ((entry (car spec))
|
|
336 (display (nth 0 entry))
|
|
337 (atts (nth 1 entry)))
|
|
338 (setq spec (cdr spec))
|
|
339 (when (custom-display-match-frame display frame)
|
|
340 (apply 'custom-face-attribites-set face frame atts)
|
|
341 (setq spec nil))))))
|
|
342
|
|
343 (defcustom custom-background-mode nil
|
|
344 "The brightness of the background.
|
|
345 Set this to the symbol dark if your background color is dark, light if
|
|
346 your background is light, or nil (default) if you want Emacs to
|
|
347 examine the brightness for you."
|
|
348 :group 'customize
|
|
349 :type '(choice (choice-item dark)
|
|
350 (choice-item light)
|
|
351 (choice-item :tag "default" nil)))
|
|
352
|
|
353 (defun custom-display-match-frame (display frame)
|
|
354 "Non-nil iff DISPLAY matches FRAME.
|
|
355 If FRAME is nil, the current FRAME is used."
|
|
356 ;; This is a kludge to get started, we really should use specifiers!
|
|
357 (unless frame
|
|
358 (setq frame (selected-frame)))
|
|
359 (if (eq display t)
|
|
360 t
|
|
361 (let ((match t))
|
|
362 (while (and display match)
|
|
363 (let* ((entry (car display))
|
|
364 (req (car entry))
|
|
365 (options (cdr entry)))
|
|
366 (setq display (cdr display))
|
|
367 (cond ((eq req 'type)
|
|
368 (let ((type (if (fboundp 'device-type)
|
|
369 (device-type (frame-device frame))
|
|
370 window-system)))
|
|
371 (setq match (memq type options))))
|
|
372 ((eq req 'class)
|
|
373 (let ((class (if (fboundp 'device-class)
|
|
374 (device-class (frame-device frame))
|
|
375 (frame-property frame 'display-type))))
|
|
376 (setq match (memq class options))))
|
|
377 ((eq req 'background)
|
|
378 (let ((background (or custom-background-mode
|
|
379 (frame-property frame 'background-mode)
|
|
380 (custom-background-mode))))
|
|
381 (setq match (memq background options))))
|
|
382 (t
|
|
383 (error "Unknown req `%S' with options `%S'" req options)))))
|
|
384 match)))
|
|
385
|
|
386 (defconst custom-face-attributes
|
|
387 '((:bold (toggle :format "Bold: %[%v%]\n") custom-set-face-bold)
|
|
388 (:italic (toggle :format "Italic: %[%v%]\n") custom-set-face-italic)
|
|
389 (:underline
|
|
390 (toggle :format "Underline: %[%v%]\n") set-face-underline-p)
|
|
391 (:foreground (color :tag "Foreground") set-face-foreground)
|
|
392 (:background (color :tag "Background") set-face-background)
|
|
393 (:stipple (editable-field :format "Stipple: %v") set-face-stipple))
|
|
394 "Alist of face attributes.
|
|
395
|
|
396 The elements are of the form (KEY TYPE SET) where KEY is a symbol
|
|
397 identifying the attribute, TYPE is a widget type for editing the
|
|
398 attibute, SET is a function for setting the attribute value.
|
|
399
|
|
400 The SET function should take three arguments, the face to modify, the
|
|
401 value of the attribute, and optionally the frame where the face should
|
|
402 be changed.")
|
|
403
|
|
404 (defun custom-face-attribites-set (face frame &rest atts)
|
|
405 "For FACE on FRAME set the attributes [KEYWORD VALUE]....
|
|
406 Each keyword should be listed in `custom-face-attributes'.
|
|
407
|
|
408 If FRAME is nil, set the default face."
|
|
409 (while atts
|
|
410 (let* ((name (nth 0 atts))
|
|
411 (value (nth 1 atts))
|
|
412 (fun (nth 2 (assq name custom-face-attributes))))
|
|
413 (setq atts (cdr (cdr atts)))
|
|
414 (condition-case nil
|
100
|
415 (funcall fun face value frame)
|
98
|
416 (error nil)))))
|
|
417
|
|
418 (defun custom-set-face-bold (face value &optional frame)
|
|
419 "Set the bold property of FACE to VALUE."
|
|
420 (if value
|
|
421 (make-face-bold face frame)
|
|
422 (make-face-unbold face frame)))
|
|
423
|
|
424 (defun custom-set-face-italic (face value &optional frame)
|
|
425 "Set the italic property of FACE to VALUE."
|
|
426 (if value
|
|
427 (make-face-italic face frame)
|
|
428 (make-face-unitalic face frame)))
|
|
429
|
|
430 (defun custom-initialize-faces (&optional frame)
|
|
431 "Initialize all custom faces for FRAME.
|
|
432 If FRAME is nil or omitted, initialize them for all frames."
|
|
433 (mapatoms (lambda (symbol)
|
|
434 (let ((spec (or (get symbol 'saved-face)
|
|
435 (get symbol 'factory-face))))
|
|
436 (when spec
|
|
437 (custom-face-display-set symbol spec frame))))))
|
|
438
|
|
439 ;;; Initializing.
|
|
440
|
|
441 (defun custom-set-variables (&rest args)
|
|
442 "Initialize variables according to user preferences.
|
|
443
|
|
444 The arguments should be a list where each entry has the form:
|
|
445
|
|
446 (SYMBOL VALUE [NOW])
|
|
447
|
|
448 The unevaluated VALUE is stored as the saved value for SYMBOL.
|
|
449 If NOW is present and non-nil, VALUE is also evaluated and bound as
|
|
450 the default value for the SYMBOL."
|
|
451 (while args
|
|
452 (let ((entry (car args)))
|
|
453 (if (listp entry)
|
|
454 (let ((symbol (nth 0 entry))
|
|
455 (value (nth 1 entry))
|
|
456 (now (nth 2 entry)))
|
|
457 (put symbol 'saved-value (list value))
|
|
458 (when now
|
|
459 (put symbol 'force-value t)
|
|
460 (set-default symbol (eval value)))
|
|
461 (setq args (cdr args)))
|
|
462 ;; Old format, a plist of SYMBOL VALUE pairs.
|
|
463 (let ((symbol (nth 0 args))
|
|
464 (value (nth 1 args)))
|
|
465 (put symbol 'saved-value (list value)))
|
|
466 (setq args (cdr (cdr args)))))))
|
|
467
|
|
468 (defun custom-set-faces (&rest args)
|
|
469 "Initialize faces according to user preferences.
|
|
470 The arguments should be a list where each entry has the form:
|
|
471
|
|
472 (FACE SPEC [NOW])
|
|
473
|
|
474 SPEC will be stored as the saved value for FACE. If NOW is present
|
|
475 and non-nil, FACE will also be created according to SPEC.
|
|
476
|
|
477 See `defface' for the format of SPEC."
|
|
478 (while args
|
|
479 (let ((entry (car args)))
|
|
480 (if (listp entry)
|
|
481 (let ((face (nth 0 entry))
|
|
482 (spec (nth 1 entry))
|
|
483 (now (nth 2 entry)))
|
|
484 (put face 'saved-face spec)
|
|
485 (when now
|
|
486 (put face 'force-face t)
|
|
487 (custom-face-display-set face spec))
|
|
488 (setq args (cdr args)))
|
|
489 ;; Old format, a plist of FACE SPEC pairs.
|
|
490 (let ((face (nth 0 args))
|
|
491 (spec (nth 1 args)))
|
|
492 (put face 'saved-face spec))
|
|
493 (setq args (cdr (cdr args)))))))
|
|
494
|
|
495 ;;; Meta Customization
|
|
496
|
|
497 (defcustom custom-define-hook nil
|
|
498 "Hook called after defining each customize option."
|
|
499 :group 'customize
|
|
500 :type 'hook)
|
|
501
|
|
502 ;;; Menu support
|
|
503
|
|
504 (defconst custom-help-menu '("Customize"
|
|
505 ["Update menu..." custom-menu-update t]
|
|
506 ["Group..." customize t]
|
|
507 ["Variable..." customize-variable t]
|
|
508 ["Face..." customize-face t]
|
|
509 ["Saved..." customize-customized t]
|
|
510 ["Apropos..." customize-apropos t])
|
|
511 "Customize menu")
|
|
512
|
100
|
513 ;(defun custom-menu-reset ()
|
|
514 ; "Reset customize menu."
|
|
515 ; (remove-hook 'custom-define-hook 'custom-menu-reset)
|
|
516 ; (define-key global-map [menu-bar help-menu customize-menu]
|
|
517 ; (cons (car custom-help-menu)
|
|
518 ; (easy-menu-create-keymaps (car custom-help-menu)
|
|
519 ; (cdr custom-help-menu)))))
|
98
|
520
|
|
521 ;;; The End.
|
|
522
|
|
523 (provide 'custom)
|
|
524
|
100
|
525 (when (and (not (fboundp 'load-gc))
|
|
526 (string-match "XEmacs" emacs-version))
|
|
527 ;; Overwrite definitions for XEmacs.
|
|
528 (load-library "custom-xmas"))
|
|
529
|
|
530 (unless (fboundp 'load-gc)
|
|
531 (custom-menu-reset))
|
|
532
|
98
|
533 ;; custom.el ends here
|