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