28
|
1 ;;; cus-face.el -- XEmacs specific custom support.
|
|
2 ;;
|
|
3 ;; Copyright (C) 1996, 1997 Free Software Foundation, Inc.
|
|
4 ;;
|
|
5 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
|
|
6 ;; Keywords: help, faces
|
32
|
7 ;; Version: 1.63
|
28
|
8 ;; X-URL: http://www.dina.kvl.dk/~abraham/custom/
|
|
9
|
|
10 ;;; Commentary:
|
|
11 ;;
|
|
12 ;; See `custom.el'.
|
|
13
|
|
14 ;;; Code:
|
|
15
|
|
16 (require 'custom)
|
|
17
|
30
|
18 (eval-and-compile (require 'cl))
|
|
19
|
28
|
20 ;;; Compatibility.
|
|
21
|
30
|
22 (eval-and-compile
|
|
23 (unless (fboundp 'frame-property)
|
|
24 ;; XEmacs function missing in Emacs 19.34.
|
|
25 (defun frame-property (frame property &optional default)
|
|
26 "Return FRAME's value for property PROPERTY."
|
|
27 (or (cdr (assq property (frame-parameters frame)))
|
|
28 default)))
|
|
29
|
|
30 (unless (fboundp 'face-doc-string)
|
|
31 ;; XEmacs function missing in Emacs.
|
|
32 (defun face-doc-string (face)
|
|
33 "Get the documentation string for FACE."
|
|
34 (get face 'face-doc-string)))
|
|
35
|
|
36 (unless (fboundp 'set-face-doc-string)
|
|
37 ;; XEmacs function missing in Emacs.
|
|
38 (defun set-face-doc-string (face string)
|
|
39 "Set the documentation string for FACE to STRING."
|
|
40 (put face 'face-doc-string string))))
|
28
|
41
|
|
42 (unless (fboundp 'x-color-values)
|
|
43 ;; Emacs function missing in XEmacs 19.14.
|
|
44 (defun x-color-values (color &optional frame)
|
|
45 "Return a description of the color named COLOR on frame FRAME.
|
|
46 The value is a list of integer RGB values--(RED GREEN BLUE).
|
|
47 These values appear to range from 0 to 65280 or 65535, depending
|
|
48 on the system; white is (65280 65280 65280) or (65535 65535 65535).
|
|
49 If FRAME is omitted or nil, use the selected frame."
|
|
50 (color-instance-rgb-components (make-color-instance color))))
|
|
51
|
|
52 ;; XEmacs and Emacs have different definitions of `facep'.
|
|
53 ;; The Emacs definition is the useful one, so emulate that.
|
|
54 (cond ((not (fboundp 'facep))
|
|
55 (defun custom-facep (face)
|
|
56 "No faces"
|
|
57 nil))
|
|
58 ((string-match "XEmacs" emacs-version)
|
|
59 (defalias 'custom-facep 'find-face))
|
|
60 (t
|
|
61 (defalias 'custom-facep 'facep)))
|
|
62
|
|
63 (unless (fboundp 'make-empty-face)
|
|
64 ;; This should be moved to `faces.el'.
|
|
65 (if (string-match "XEmacs" emacs-version)
|
30
|
66 ;; Give up for old XEmacs pre 19.15/20.1.
|
28
|
67 (defalias 'make-empty-face 'make-face)
|
30
|
68 ;; Define for Emacs pre 19.35.
|
28
|
69 (defun make-empty-face (name)
|
|
70 "Define a new FACE on all frames, ignoring X resources."
|
|
71 (interactive "SMake face: ")
|
|
72 (or (internal-find-face name)
|
|
73 (let ((face (make-vector 8 nil)))
|
|
74 (aset face 0 'face)
|
|
75 (aset face 1 name)
|
|
76 (let* ((frames (frame-list))
|
|
77 (inhibit-quit t)
|
|
78 (id (internal-next-face-id)))
|
|
79 (make-face-internal id)
|
|
80 (aset face 2 id)
|
|
81 (while frames
|
|
82 (set-frame-face-alist (car frames)
|
|
83 (cons (cons name (copy-sequence face))
|
|
84 (frame-face-alist (car frames))))
|
|
85 (setq frames (cdr frames)))
|
|
86 (setq global-face-data (cons (cons name face) global-face-data)))
|
|
87 ;; add to menu
|
|
88 (if (fboundp 'facemenu-add-new-face)
|
|
89 (facemenu-add-new-face name))
|
|
90 face))
|
|
91 name)))
|
|
92
|
|
93 (defcustom initialize-face-resources t
|
|
94 "If non nil, allow X resources to initialize face properties.
|
|
95 This only affects faces declared with `defface', and only NT or X11 frames."
|
|
96 :group 'customize
|
|
97 :type 'boolean)
|
|
98
|
|
99 (cond ((fboundp 'initialize-face-resources)
|
|
100 ;; Already bound, do nothing.
|
|
101 )
|
|
102 ((fboundp 'make-face-x-resource-internal)
|
|
103 ;; Emacs or new XEmacs.
|
|
104 (defun initialize-face-resources (face &optional frame)
|
|
105 "Initialize face according to the X11 resources.
|
|
106 This might overwrite existing face properties.
|
|
107 Does nothing when the variable initialize-face-resources is nil."
|
|
108 (when initialize-face-resources
|
|
109 (make-face-x-resource-internal face frame t))))
|
|
110 (t
|
|
111 ;; Too hard to do right on XEmacs.
|
|
112 (defalias 'initialize-face-resources 'ignore)))
|
|
113
|
|
114 (if (string-match "XEmacs" emacs-version)
|
30
|
115 ;; Xemacs.
|
|
116 (defun custom-invert-face (face &optional frame)
|
|
117 "Swap the foreground and background colors of face FACE.
|
|
118 If the colors are not specified in the face, use the default colors."
|
|
119 (interactive (list (read-face-name "Reverse face: ")))
|
|
120 (let ((fg (color-name (face-foreground face frame) frame))
|
|
121 (bg (color-name (face-background face frame) frame)))
|
|
122 (set-face-foreground face bg frame)
|
|
123 (set-face-background face fg frame)))
|
|
124 ;; Emacs.
|
|
125 (defun custom-invert-face (face &optional frame)
|
|
126 "Swap the foreground and background colors of face FACE.
|
|
127 If the colors are not specified in the face, use the default colors."
|
|
128 (interactive (list (read-face-name "Reverse face: ")))
|
|
129 (let ((fg (or (face-foreground face frame)
|
|
130 (face-foreground 'default frame)
|
|
131 (frame-property (or frame (selected-frame))
|
|
132 'foreground-color)
|
|
133 "black"))
|
|
134 (bg (or (face-background face frame)
|
|
135 (face-background 'default frame)
|
|
136 (frame-property (or frame (selected-frame))
|
|
137 'background-color)
|
|
138 "white")))
|
|
139 (set-face-foreground face bg frame)
|
|
140 (set-face-background face fg frame))))
|
28
|
141
|
|
142 (defcustom custom-background-mode nil
|
|
143 "The brightness of the background.
|
|
144 Set this to the symbol dark if your background color is dark, light if
|
|
145 your background is light, or nil (default) if you want Emacs to
|
|
146 examine the brightness for you."
|
|
147 :group 'customize
|
|
148 :type '(choice (choice-item dark)
|
|
149 (choice-item light)
|
|
150 (choice-item :tag "default" nil)))
|
|
151
|
|
152 (defun custom-background-mode (frame)
|
|
153 "Kludge to detect background mode for FRAME."
|
|
154 (let* ((bg-resource
|
|
155 (condition-case ()
|
|
156 (x-get-resource ".backgroundMode" "BackgroundMode" 'string)
|
|
157 (error nil)))
|
|
158 color
|
|
159 (mode (cond (bg-resource
|
|
160 (intern (downcase bg-resource)))
|
|
161 ((and (setq color (condition-case ()
|
|
162 (or (frame-property
|
|
163 frame
|
|
164 'background-color)
|
|
165 (color-instance-name
|
|
166 (specifier-instance
|
|
167 (face-background 'default))))
|
|
168 (error nil)))
|
|
169 (or (string-match "XEmacs" emacs-version)
|
|
170 window-system)
|
|
171 (< (apply '+ (x-color-values color))
|
|
172 (/ (apply '+ (x-color-values "white"))
|
|
173 3)))
|
|
174 'dark)
|
|
175 (t 'light))))
|
|
176 (modify-frame-parameters frame (list (cons 'background-mode mode)))
|
|
177 mode))
|
|
178
|
30
|
179 (eval-and-compile
|
|
180 (if (string-match "XEmacs" emacs-version)
|
|
181 ;; XEmacs.
|
|
182 (defun custom-extract-frame-properties (frame)
|
|
183 "Return a plist with the frame properties of FRAME used by custom."
|
|
184 (list 'type (device-type (frame-device frame))
|
|
185 'class (device-class (frame-device frame))
|
|
186 'background (or custom-background-mode
|
|
187 (frame-property frame
|
|
188 'background-mode)
|
|
189 (custom-background-mode frame))))
|
|
190 ;; Emacs.
|
|
191 (defun custom-extract-frame-properties (frame)
|
|
192 "Return a plist with the frame properties of FRAME used by custom."
|
|
193 (list 'type window-system
|
|
194 'class (frame-property frame 'display-type)
|
|
195 'background (or custom-background-mode
|
|
196 (frame-property frame 'background-mode)
|
|
197 (custom-background-mode frame))))))
|
|
198
|
|
199 ;;; Declaring a face.
|
|
200
|
|
201 ;;;###autoload
|
|
202 (defun custom-declare-face (face spec doc &rest args)
|
|
203 "Like `defface', but FACE is evaluated as a normal argument."
|
|
204 (when (fboundp 'load-gc)
|
|
205 ;; This should be allowed, somehow.
|
|
206 (error "Attempt to declare a face during dump"))
|
|
207 (unless (get face 'factory-face)
|
|
208 (put face 'factory-face spec)
|
|
209 (when (fboundp 'facep)
|
32
|
210 (unless (custom-facep face)
|
30
|
211 ;; If the user has already created the face, respect that.
|
|
212 (let ((value (or (get face 'saved-face) spec))
|
|
213 (frames (custom-relevant-frames))
|
|
214 frame)
|
|
215 ;; Create global face.
|
|
216 (make-empty-face face)
|
|
217 (custom-face-display-set face value)
|
|
218 ;; Create frame local faces
|
|
219 (while frames
|
|
220 (setq frame (car frames)
|
|
221 frames (cdr frames))
|
|
222 (custom-face-display-set face value frame))
|
|
223 (initialize-face-resources face))))
|
|
224 (when (and doc (null (face-doc-string face)))
|
|
225 (set-face-doc-string face doc))
|
|
226 (custom-handle-all-keywords face args 'custom-face)
|
|
227 (run-hooks 'custom-define-hook))
|
|
228 face)
|
|
229
|
|
230 ;;; Font Attributes.
|
|
231
|
|
232 (defconst custom-face-attributes
|
|
233 '((:bold (toggle :format "Bold: %[%v%]\n"
|
|
234 :help-echo "Control whether a bold font should be used.")
|
|
235 custom-set-face-bold)
|
|
236 (:italic (toggle :format "Italic: %[%v%]\n"
|
|
237 :help-echo "\
|
|
238 Control whether an italic font should be used.")
|
|
239 custom-set-face-italic)
|
|
240 (:underline (toggle :format "Underline: %[%v%]\n"
|
|
241 :help-echo "\
|
|
242 Control whether the text should be underlined.")
|
|
243 set-face-underline-p)
|
|
244 (:foreground (color :tag "Foreground"
|
32
|
245 :value "black"
|
30
|
246 :help-echo "Set foreground color.")
|
|
247 set-face-foreground)
|
|
248 (:background (color :tag "Background"
|
32
|
249 :value "white"
|
30
|
250 :help-echo "Set background color.")
|
|
251 set-face-background)
|
|
252 (:invert (const :format "Invert Face\n"
|
|
253 :sibling-args (:help-echo "\
|
|
254 Reverse the foreground and background color.
|
|
255 If you haven't specified them for the face, the default colors will be used.")
|
|
256 t)
|
|
257 (lambda (face value &optional frame)
|
|
258 ;; We don't use VALUE.
|
|
259 (custom-invert-face face frame)))
|
|
260 (:stipple (editable-field :format "Stipple: %v"
|
|
261 :help-echo "Name of background bitmap file.")
|
|
262 set-face-stipple))
|
|
263 "Alist of face attributes.
|
|
264
|
|
265 The elements are of the form (KEY TYPE SET) where KEY is a symbol
|
|
266 identifying the attribute, TYPE is a widget type for editing the
|
|
267 attibute, SET is a function for setting the attribute value.
|
|
268
|
|
269 The SET function should take three arguments, the face to modify, the
|
|
270 value of the attribute, and optionally the frame where the face should
|
|
271 be changed.")
|
|
272
|
|
273 (defun custom-face-attributes-set (face frame &rest atts)
|
|
274 "For FACE on FRAME set the attributes [KEYWORD VALUE]....
|
|
275 Each keyword should be listed in `custom-face-attributes'.
|
|
276
|
|
277 If FRAME is nil, set the default face."
|
|
278 (while atts
|
|
279 (let* ((name (nth 0 atts))
|
|
280 (value (nth 1 atts))
|
|
281 (fun (nth 2 (assq name custom-face-attributes))))
|
|
282 (setq atts (cdr (cdr atts)))
|
|
283 (condition-case nil
|
|
284 (funcall fun face value frame)
|
|
285 (error nil)))))
|
|
286
|
|
287 (defun custom-set-face-bold (face value &optional frame)
|
|
288 "Set the bold property of FACE to VALUE."
|
|
289 (if value
|
|
290 (make-face-bold face frame)
|
|
291 (make-face-unbold face frame)))
|
|
292
|
|
293 (defun custom-set-face-italic (face value &optional frame)
|
|
294 "Set the italic property of FACE to VALUE."
|
|
295 (if value
|
|
296 (make-face-italic face frame)
|
|
297 (make-face-unitalic face frame)))
|
|
298
|
|
299 (when (string-match "XEmacs" emacs-version)
|
|
300 ;; Support for special XEmacs font attributes.
|
|
301 (autoload 'font-create-object "font" nil)
|
|
302
|
|
303 (unless (fboundp 'face-font-name)
|
|
304 (defun face-font-name (face &rest args)
|
|
305 (apply 'face-font face args)))
|
|
306
|
|
307 (defun custom-set-face-font-size (face size &rest args)
|
|
308 "Set the font of FACE to SIZE"
|
|
309 (let* ((font (apply 'face-font-name face args))
|
|
310 (fontobj (font-create-object font)))
|
|
311 (set-font-size fontobj size)
|
32
|
312 (apply 'font-set-face-font face fontobj args)))
|
30
|
313
|
|
314 (defun custom-set-face-font-family (face family &rest args)
|
|
315 "Set the font of FACE to FAMILY"
|
|
316 (let* ((font (apply 'face-font-name face args))
|
|
317 (fontobj (font-create-object font)))
|
|
318 (set-font-family fontobj family)
|
32
|
319 (apply 'font-set-face-font face fontobj args)))
|
30
|
320
|
|
321 (nconc custom-face-attributes
|
|
322 '((:family (editable-field :format "Font Family: %v"
|
|
323 :help-echo "\
|
|
324 Name of font family to use (e.g. times).")
|
|
325 custom-set-face-font-family)
|
|
326 (:size (editable-field :format "Size: %v"
|
|
327 :help-echo "\
|
|
328 Text size (e.g. 9pt or 2mm).")
|
|
329 custom-set-face-font-size))))
|
|
330
|
|
331 ;;; Frames.
|
|
332
|
|
333 (defun custom-face-display-set (face spec &optional frame)
|
|
334 "Set FACE to the attributes to the first matching entry in SPEC.
|
|
335 Iff optional FRAME is non-nil, set it for that frame only.
|
|
336 See `defface' for information about SPEC."
|
|
337 (when (fboundp 'make-face)
|
|
338 (while spec
|
|
339 (let* ((entry (car spec))
|
|
340 (display (nth 0 entry))
|
|
341 (atts (nth 1 entry)))
|
|
342 (setq spec (cdr spec))
|
|
343 (when (custom-display-match-frame display frame)
|
|
344 ;; Avoid creating frame local duplicates of the global face.
|
|
345 (unless (and frame (eq display (get face 'custom-face-display)))
|
|
346 (apply 'custom-face-attributes-set face frame atts))
|
|
347 (unless frame
|
|
348 (put face 'custom-face-display display))
|
|
349 (setq spec nil))))))
|
|
350
|
28
|
351 (defvar custom-default-frame-properties nil
|
|
352 "The frame properties used for the global faces.
|
|
353 Frames who doesn't match these propertiess should have frame local faces.
|
|
354 The value should be nil, if uninitialized, or a plist otherwise.
|
|
355 See `defface' for a list of valid keys and values for the plist.")
|
|
356
|
|
357 (defun custom-get-frame-properties (&optional frame)
|
|
358 "Return a plist with the frame properties of FRAME used by custom.
|
|
359 If FRAME is nil, return the default frame properties."
|
|
360 (cond (frame
|
|
361 ;; Try to get from cache.
|
|
362 (let ((cache (frame-property frame 'custom-properties)))
|
|
363 (unless cache
|
|
364 ;; Oh well, get it then.
|
|
365 (setq cache (custom-extract-frame-properties frame))
|
|
366 ;; and cache it...
|
|
367 (modify-frame-parameters frame
|
|
368 (list (cons 'custom-properties cache))))
|
|
369 cache))
|
|
370 (custom-default-frame-properties)
|
|
371 (t
|
|
372 (setq custom-default-frame-properties
|
|
373 (custom-extract-frame-properties (selected-frame))))))
|
|
374
|
|
375 (defun custom-display-match-frame (display frame)
|
|
376 "Non-nil iff DISPLAY matches FRAME.
|
|
377 If FRAME is nil, the current FRAME is used."
|
|
378 ;; This is a kludge to get started, we really should use specifiers!
|
|
379 (if (eq display t)
|
|
380 t
|
|
381 (let* ((props (custom-get-frame-properties frame))
|
|
382 (type (plist-get props 'type))
|
|
383 (class (plist-get props 'class))
|
|
384 (background (plist-get props 'background))
|
|
385 (match t)
|
|
386 (entries display)
|
|
387 entry req options)
|
|
388 (while (and entries match)
|
|
389 (setq entry (car entries)
|
|
390 entries (cdr entries)
|
|
391 req (car entry)
|
|
392 options (cdr entry)
|
|
393 match (cond ((eq req 'type)
|
|
394 (memq type options))
|
|
395 ((eq req 'class)
|
|
396 (memq class options))
|
|
397 ((eq req 'background)
|
|
398 (memq background options))
|
|
399 (t
|
|
400 (error "Unknown req `%S' with options `%S'"
|
|
401 req options)))))
|
|
402 match)))
|
|
403
|
|
404 (defun custom-relevant-frames ()
|
|
405 "List of frames whose custom properties differ from the default."
|
30
|
406 (let ((relevant nil)
|
|
407 (default (custom-get-frame-properties))
|
|
408 (frames (frame-list))
|
|
409 frame)
|
|
410 (while frames
|
|
411 (setq frame (car frames)
|
|
412 frames (cdr frames))
|
|
413 (unless (equal default (custom-get-frame-properties frame))
|
|
414 (push frame relevant)))
|
|
415 relevant))
|
28
|
416
|
|
417 (defun custom-initialize-faces (&optional frame)
|
|
418 "Initialize all custom faces for FRAME.
|
|
419 If FRAME is nil or omitted, initialize them for all frames."
|
|
420 (mapcar (lambda (symbol)
|
|
421 (let ((spec (or (get symbol 'saved-face)
|
|
422 (get symbol 'factory-face))))
|
|
423 (when spec
|
|
424 (custom-face-display-set symbol spec frame)
|
|
425 (initialize-face-resources symbol frame))))
|
|
426 (face-list)))
|
|
427
|
|
428 (defun custom-initialize-frame (&optional frame)
|
|
429 "Initialize local faces for FRAME if necessary.
|
|
430 If FRAME is missing or nil, the first member (frame-list) is used."
|
|
431 (unless frame
|
|
432 (setq frame (car (frame-list))))
|
|
433 (unless (equal (custom-get-frame-properties)
|
|
434 (custom-get-frame-properties frame))
|
30
|
435 (custom-initialize-faces frame)))
|
28
|
436
|
|
437 ;; Enable. This should go away when bundled with Emacs.
|
30
|
438 (unless (string-match "XEmacs" emacs-version)
|
|
439 (add-hook 'after-make-frame-hook 'custom-initialize-frame))
|
28
|
440
|
|
441 ;;; Initializing.
|
|
442
|
|
443 (and (fboundp 'make-face)
|
|
444 (make-face 'custom-face-empty))
|
|
445
|
|
446 ;;;###autoload
|
|
447 (defun custom-set-faces (&rest args)
|
|
448 "Initialize faces according to user preferences.
|
|
449 The arguments should be a list where each entry has the form:
|
|
450
|
|
451 (FACE SPEC [NOW])
|
|
452
|
|
453 SPEC will be stored as the saved value for FACE. If NOW is present
|
|
454 and non-nil, FACE will also be created according to SPEC.
|
|
455
|
|
456 See `defface' for the format of SPEC."
|
|
457 (while args
|
|
458 (let ((entry (car args)))
|
|
459 (if (listp entry)
|
|
460 (let ((face (nth 0 entry))
|
|
461 (spec (nth 1 entry))
|
|
462 (now (nth 2 entry)))
|
|
463 (put face 'saved-face spec)
|
|
464 (when now
|
32
|
465 (put face 'force-face t))
|
|
466 (when (or now (custom-facep face))
|
28
|
467 (when (fboundp 'copy-face)
|
|
468 (copy-face 'custom-face-empty face))
|
|
469 (custom-face-display-set face spec))
|
|
470 (setq args (cdr args)))
|
|
471 ;; Old format, a plist of FACE SPEC pairs.
|
|
472 (let ((face (nth 0 args))
|
|
473 (spec (nth 1 args)))
|
|
474 (put face 'saved-face spec))
|
|
475 (setq args (cdr (cdr args)))))))
|
|
476
|
|
477 ;;; The End.
|
|
478
|
|
479 (provide 'cus-face)
|
|
480
|
|
481 ;; cus-face.el ends here
|