(vertical_scroll_bar_type): Add enumerated
[bpt/emacs.git] / lisp / faces.el
1 ;;; faces.el --- Lisp interface to the c "face" structure
2
3 ;; Copyright (C) 1992, 1993, 1994, 1995, 1996 Free Software Foundation, Inc.
4
5 ;; This file is part of GNU Emacs.
6
7 ;; GNU Emacs is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 2, or (at your option)
10 ;; any later version.
11
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
16
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING. If not, write to the
19 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 ;; Boston, MA 02111-1307, USA.
21
22 ;;; Commentary:
23
24 ;; Mostly derived from Lucid.
25
26 ;;; Code:
27
28 (eval-when-compile
29 ;; These used to be defsubsts, now they're subrs. Avoid losing if we're
30 ;; being compiled with an old Emacs that still has defsubrs in it.
31 (put 'face-name 'byte-optimizer nil)
32 (put 'face-id 'byte-optimizer nil)
33 (put 'face-font 'byte-optimizer nil)
34 (put 'face-foreground 'byte-optimizer nil)
35 (put 'face-background 'byte-optimizer nil)
36 (put 'face-stipple 'byte-optimizer nil)
37 (put 'face-underline-p 'byte-optimizer nil)
38 (put 'set-face-font 'byte-optimizer nil)
39 (put 'set-face-foreground 'byte-optimizer nil)
40 (put 'set-face-background 'byte-optimizer nil)
41 (put 'set-face-stipple 'byte-optimizer nil)
42 (put 'set-face-underline-p 'byte-optimizer nil))
43 \f
44 ;;;; Functions for manipulating face vectors.
45
46 ;;; A face vector is a vector of the form:
47 ;;; [face NAME ID FONT FOREGROUND BACKGROUND STIPPLE UNDERLINE]
48
49 ;;; Type checkers.
50 (defsubst internal-facep (x)
51 (and (vectorp x) (= (length x) 8) (eq (aref x 0) 'face)))
52
53 (defun facep (x)
54 "Return t if X is a face name or an internal face vector."
55 (and (or (internal-facep x)
56 (and (symbolp x) (assq x global-face-data)))
57 t))
58
59 (defmacro internal-check-face (face)
60 (` (or (internal-facep (, face))
61 (signal 'wrong-type-argument (list 'internal-facep (, face))))))
62
63 ;;; Accessors.
64 (defun face-name (face)
65 "Return the name of face FACE."
66 (aref (internal-get-face face) 1))
67
68 (defun face-id (face)
69 "Return the internal ID number of face FACE."
70 (aref (internal-get-face face) 2))
71
72 (defun face-font (face &optional frame)
73 "Return the font name of face FACE, or nil if it is unspecified.
74 If the optional argument FRAME is given, report on face FACE in that frame.
75 If FRAME is t, report on the defaults for face FACE (for new frames).
76 The font default for a face is either nil, or a list
77 of the form (bold), (italic) or (bold italic).
78 If FRAME is omitted or nil, use the selected frame."
79 (aref (internal-get-face face frame) 3))
80
81 (defun face-foreground (face &optional frame)
82 "Return the foreground color name of face FACE, or nil if unspecified.
83 If the optional argument FRAME is given, report on face FACE in that frame.
84 If FRAME is t, report on the defaults for face FACE (for new frames).
85 If FRAME is omitted or nil, use the selected frame."
86 (aref (internal-get-face face frame) 4))
87
88 (defun face-background (face &optional frame)
89 "Return the background color name of face FACE, or nil if unspecified.
90 If the optional argument FRAME is given, report on face FACE in that frame.
91 If FRAME is t, report on the defaults for face FACE (for new frames).
92 If FRAME is omitted or nil, use the selected frame."
93 (aref (internal-get-face face frame) 5))
94
95 (defun face-stipple (face &optional frame)
96 "Return the stipple pixmap name of face FACE, or nil if unspecified.
97 If the optional argument FRAME is given, report on face FACE in that frame.
98 If FRAME is t, report on the defaults for face FACE (for new frames).
99 If FRAME is omitted or nil, use the selected frame."
100 (aref (internal-get-face face frame) 6))
101
102 (defalias 'face-background-pixmap 'face-stipple)
103
104 (defun face-underline-p (face &optional frame)
105 "Return t if face FACE is underlined.
106 If the optional argument FRAME is given, report on face FACE in that frame.
107 If FRAME is t, report on the defaults for face FACE (for new frames).
108 If FRAME is omitted or nil, use the selected frame."
109 (aref (internal-get-face face frame) 7))
110
111 \f
112 ;;; Mutators.
113
114 (defun set-face-font (face font &optional frame)
115 "Change the font of face FACE to FONT (a string).
116 If the optional FRAME argument is provided, change only
117 in that frame; otherwise change each frame."
118 (interactive (internal-face-interactive "font"))
119 (if (stringp font) (setq font (x-resolve-font-name font 'default frame)))
120 (internal-set-face-1 face 'font font 3 frame))
121
122 (defun set-face-foreground (face color &optional frame)
123 "Change the foreground color of face FACE to COLOR (a string).
124 If the optional FRAME argument is provided, change only
125 in that frame; otherwise change each frame."
126 (interactive (internal-face-interactive "foreground"))
127 (internal-set-face-1 face 'foreground color 4 frame))
128
129 (defvar face-default-stipple "gray3"
130 "Default stipple pattern used on monochrome displays.
131 This stipple pattern is used on monochrome displays
132 instead of shades of gray for a face background color.
133 See `set-face-stipple' for possible values for this variable.")
134
135 (defun face-color-gray-p (color &optional frame)
136 "Return t if COLOR is a shade of gray (or white or black).
137 FRAME specifies the frame and thus the display for interpreting COLOR."
138 (let* ((values (x-color-values color frame))
139 (r (nth 0 values))
140 (g (nth 1 values))
141 (b (nth 2 values)))
142 (and values
143 (< (abs (- r g)) (/ (max 1 (abs r) (abs g)) 20))
144 (< (abs (- g b)) (/ (max 1 (abs g) (abs b)) 20))
145 (< (abs (- b r)) (/ (max 1 (abs b) (abs r)) 20)))))
146
147 (defun set-face-background (face color &optional frame)
148 "Change the background color of face FACE to COLOR (a string).
149 If the optional FRAME argument is provided, change only
150 in that frame; otherwise change each frame."
151 (interactive (internal-face-interactive "background"))
152 ;; For a specific frame, use gray stipple instead of gray color
153 ;; if the display does not support a gray color.
154 (if (and frame (not (eq frame t)) color
155 ;; Check for support for foreground, not for background!
156 ;; face-color-supported-p is smart enough to know
157 ;; that grays are "supported" as background
158 ;; because we are supposed to use stipple for them!
159 (not (face-color-supported-p frame color nil)))
160 (set-face-stipple face face-default-stipple frame)
161 (if (null frame)
162 (let ((frames (frame-list)))
163 (while frames
164 (set-face-background (face-name face) color (car frames))
165 (setq frames (cdr frames)))
166 (set-face-background face color t)
167 color)
168 (internal-set-face-1 face 'background color 5 frame))))
169
170 (defun set-face-stipple (face pixmap &optional frame)
171 "Change the stipple pixmap of face FACE to PIXMAP.
172 PIXMAP should be a string, the name of a file of pixmap data.
173 The directories listed in the `x-bitmap-file-path' variable are searched.
174
175 Alternatively, PIXMAP may be a list of the form (WIDTH HEIGHT DATA)
176 where WIDTH and HEIGHT are the size in pixels,
177 and DATA is a string, containing the raw bits of the bitmap.
178
179 If the optional FRAME argument is provided, change only
180 in that frame; otherwise change each frame."
181 (interactive (internal-face-interactive-stipple "stipple"))
182 (internal-set-face-1 face 'background-pixmap pixmap 6 frame))
183
184 (defalias 'set-face-background-pixmap 'set-face-stipple)
185
186 (defun set-face-underline-p (face underline-p &optional frame)
187 "Specify whether face FACE is underlined. (Yes if UNDERLINE-P is non-nil.)
188 If the optional FRAME argument is provided, change only
189 in that frame; otherwise change each frame."
190 (interactive (internal-face-interactive "underline-p" "underlined"))
191 (internal-set-face-1 face 'underline underline-p 7 frame))
192 \f
193 (defun modify-face-read-string (face default name alist)
194 (let ((value
195 (completing-read
196 (if default
197 (format "Set face %s %s (default %s): "
198 face name (downcase default))
199 (format "Set face %s %s: " face name))
200 alist)))
201 (cond ((equal value "none")
202 nil)
203 ((equal value "")
204 default)
205 (t value))))
206
207 (defun modify-face (face foreground background stipple
208 bold-p italic-p underline-p &optional frame)
209 "Change the display attributes for face FACE.
210 If the optional FRAME argument is provided, change only
211 in that frame; otherwise change each frame.
212
213 FOREGROUND and BACKGROUND should be a colour name string (or list of strings to
214 try) or nil. STIPPLE should be a stipple pattern name string or nil.
215 If nil, means do not change the display attribute corresponding to that arg.
216
217 BOLD-P, ITALIC-P, and UNDERLINE-P specify whether the face should be set bold,
218 in italic, and underlined, respectively. If neither nil or t, means do not
219 change the display attribute corresponding to that arg.
220
221 If called interactively, prompts for a face name and face attributes."
222 (interactive
223 (let* ((completion-ignore-case t)
224 (face (symbol-name (read-face-name "Modify face: ")))
225 (colors (mapcar 'list x-colors))
226 (stipples (mapcar 'list (apply 'nconc
227 (mapcar 'directory-files
228 x-bitmap-file-path))))
229 (foreground (modify-face-read-string
230 face (face-foreground (intern face))
231 "foreground" colors))
232 (background (modify-face-read-string
233 face (face-background (intern face))
234 "background" colors))
235 ;; If the stipple value is a list (WIDTH HEIGHT DATA),
236 ;; represent that as a string by printing it out.
237 (old-stipple-string
238 (if (stringp (face-stipple (intern face)))
239 (face-stipple (intern face))
240 (if (face-stipple (intern face))
241 (prin1-to-string (face-stipple (intern face))))))
242 (new-stipple-string
243 (modify-face-read-string
244 face old-stipple-string
245 "stipple" stipples))
246 ;; Convert the stipple value text we read
247 ;; back to a list if it looks like one.
248 ;; This makes the assumption that a pixmap file name
249 ;; won't start with an open-paren.
250 (stipple
251 (and new-stipple-string
252 (if (string-match "^(" new-stipple-string)
253 (read new-stipple-string)
254 new-stipple-string)))
255 (bold-p (y-or-n-p (concat "Should face " face " be bold ")))
256 (italic-p (y-or-n-p (concat "Should face " face " be italic ")))
257 (underline-p (y-or-n-p (concat "Should face " face " be underlined ")))
258 (all-frames-p (y-or-n-p (concat "Modify face " face " in all frames "))))
259 (message "Face %s: %s" face
260 (mapconcat 'identity
261 (delq nil
262 (list (and foreground (concat (downcase foreground) " foreground"))
263 (and background (concat (downcase background) " background"))
264 (and stipple (concat (downcase new-stipple-string) " stipple"))
265 (and bold-p "bold") (and italic-p "italic")
266 (and underline-p "underline"))) ", "))
267 (list (intern face) foreground background stipple
268 bold-p italic-p underline-p
269 (if all-frames-p nil (selected-frame)))))
270 (condition-case nil
271 (face-try-color-list 'set-face-foreground face foreground frame)
272 (error nil))
273 (condition-case nil
274 (face-try-color-list 'set-face-background face background frame)
275 (error nil))
276 (condition-case nil
277 (set-face-stipple face stipple frame)
278 (error nil))
279 (cond ((eq bold-p nil) (make-face-unbold face frame t))
280 ((eq bold-p t) (make-face-bold face frame t)))
281 (cond ((eq italic-p nil) (make-face-unitalic face frame t))
282 ((eq italic-p t) (make-face-italic face frame t)))
283 (if (memq underline-p '(nil t))
284 (set-face-underline-p face underline-p frame))
285 (and (interactive-p) (redraw-display)))
286 \f
287 ;;;; Associating face names (symbols) with their face vectors.
288
289 (defvar global-face-data nil
290 "Internal data for face support functions. Not for external use.
291 This is an alist associating face names with the default values for
292 their parameters. Newly created frames get their data from here.")
293
294 (defun face-list ()
295 "Returns a list of all defined face names."
296 (mapcar 'car global-face-data))
297
298 (defun internal-find-face (name &optional frame)
299 "Retrieve the face named NAME. Return nil if there is no such face.
300 If the optional argument FRAME is given, this gets the face NAME for
301 that frame; otherwise, it uses the selected frame.
302 If FRAME is the symbol t, then the global, non-frame face is returned.
303 If NAME is already a face, it is simply returned."
304 (if (and (eq frame t) (not (symbolp name)))
305 (setq name (face-name name)))
306 (if (symbolp name)
307 (cdr (assq name
308 (if (eq frame t)
309 global-face-data
310 (frame-face-alist (or frame (selected-frame))))))
311 (internal-check-face name)
312 name))
313
314 (defun internal-get-face (name &optional frame)
315 "Retrieve the face named NAME; error if there is none.
316 If the optional argument FRAME is given, this gets the face NAME for
317 that frame; otherwise, it uses the selected frame.
318 If FRAME is the symbol t, then the global, non-frame face is returned.
319 If NAME is already a face, it is simply returned."
320 (or (internal-find-face name frame)
321 (internal-check-face name)))
322
323
324 (defun internal-set-face-1 (face name value index frame)
325 (let ((inhibit-quit t))
326 (if (null frame)
327 (let ((frames (frame-list)))
328 (while frames
329 (internal-set-face-1 (face-name face) name value index (car frames))
330 (setq frames (cdr frames)))
331 (aset (internal-get-face (if (symbolp face) face (face-name face)) t)
332 index value)
333 value)
334 (or (eq frame t)
335 (set-face-attribute-internal (face-id face) name value frame))
336 (aset (internal-get-face face frame) index value))))
337
338
339 (defun read-face-name (prompt)
340 (let (face)
341 (while (= (length face) 0)
342 (setq face (completing-read prompt
343 (mapcar '(lambda (x) (list (symbol-name x)))
344 (face-list))
345 nil t)))
346 (intern face)))
347
348 (defun internal-face-interactive (what &optional bool)
349 (let* ((fn (intern (concat "face-" what)))
350 (prompt (concat "Set " what " of face"))
351 (face (read-face-name (concat prompt ": ")))
352 (default (if (fboundp fn)
353 (or (funcall fn face (selected-frame))
354 (funcall fn 'default (selected-frame)))))
355 (value (if bool
356 (y-or-n-p (concat "Should face " (symbol-name face)
357 " be " bool "? "))
358 (read-string (concat prompt " " (symbol-name face) " to: ")
359 default))))
360 (list face (if (equal value "") nil value))))
361
362 (defun internal-face-interactive-stipple (what)
363 (let* ((fn (intern (concat "face-" what)))
364 (prompt (concat "Set " what " of face"))
365 (face (read-face-name (concat prompt ": ")))
366 (default (if (fboundp fn)
367 (or (funcall fn face (selected-frame))
368 (funcall fn 'default (selected-frame)))))
369 ;; If the stipple value is a list (WIDTH HEIGHT DATA),
370 ;; represent that as a string by printing it out.
371 (old-stipple-string
372 (if (stringp (face-stipple face))
373 (face-stipple face)
374 (if (null (face-stipple face))
375 nil
376 (prin1-to-string (face-stipple face)))))
377 (new-stipple-string
378 (read-string
379 (concat prompt " " (symbol-name face) " to: ")
380 old-stipple-string))
381 ;; Convert the stipple value text we read
382 ;; back to a list if it looks like one.
383 ;; This makes the assumption that a pixmap file name
384 ;; won't start with an open-paren.
385 (stipple
386 (if (string-match "^(" new-stipple-string)
387 (read new-stipple-string)
388 new-stipple-string)))
389 (list face (if (equal stipple "") nil stipple))))
390
391 (defun make-face (name)
392 "Define a new FACE on all frames.
393 You can modify the font, color, etc of this face with the set-face- functions.
394 If the face already exists, it is unmodified."
395 (interactive "SMake face: ")
396 (or (internal-find-face name)
397 (let ((face (make-vector 8 nil)))
398 (aset face 0 'face)
399 (aset face 1 name)
400 (let* ((frames (frame-list))
401 (inhibit-quit t)
402 (id (internal-next-face-id)))
403 (make-face-internal id)
404 (aset face 2 id)
405 (while frames
406 (set-frame-face-alist (car frames)
407 (cons (cons name (copy-sequence face))
408 (frame-face-alist (car frames))))
409 (setq frames (cdr frames)))
410 (setq global-face-data (cons (cons name face) global-face-data)))
411 ;; when making a face after frames already exist
412 (if (memq window-system '(x ms-windows))
413 (make-face-x-resource-internal face))
414 ;; add to menu
415 (if (fboundp 'facemenu-add-new-face)
416 (facemenu-add-new-face name))
417 face))
418 name)
419
420 ;; Fill in a face by default based on X resources, for all existing frames.
421 ;; This has to be done when a new face is made.
422 (defun make-face-x-resource-internal (face &optional frame set-anyway)
423 (cond ((null frame)
424 (let ((frames (frame-list)))
425 (while frames
426 (if (memq (framep (car frames)) '(x ms-windows))
427 (make-face-x-resource-internal (face-name face)
428 (car frames) set-anyway))
429 (setq frames (cdr frames)))))
430 (t
431 (setq face (internal-get-face (face-name face) frame))
432 ;;
433 ;; These are things like "attributeForeground" instead of simply
434 ;; "foreground" because people tend to do things like "*foreground",
435 ;; which would cause all faces to be fully qualified, making faces
436 ;; inherit attributes in a non-useful way. So we've made them slightly
437 ;; less obvious to specify in order to make them work correctly in
438 ;; more random environments.
439 ;;
440 ;; I think these should be called "face.faceForeground" instead of
441 ;; "face.attributeForeground", but they're the way they are for
442 ;; hysterical reasons.
443 ;;
444 (let* ((name (symbol-name (face-name face)))
445 (fn (or (x-get-resource (concat name ".attributeFont")
446 "Face.AttributeFont")
447 (and set-anyway (face-font face))))
448 (fg (or (x-get-resource (concat name ".attributeForeground")
449 "Face.AttributeForeground")
450 (and set-anyway (face-foreground face))))
451 (bg (or (x-get-resource (concat name ".attributeBackground")
452 "Face.AttributeBackground")
453 (and set-anyway (face-background face))))
454 (bgp (or (x-get-resource (concat name ".attributeStipple")
455 "Face.AttributeStipple")
456 (x-get-resource (concat name ".attributeBackgroundPixmap")
457 "Face.AttributeBackgroundPixmap")
458 (and set-anyway (face-stipple face))))
459 (ulp (let ((resource (x-get-resource
460 (concat name ".attributeUnderline")
461 "Face.AttributeUnderline")))
462 (if resource
463 (member (downcase resource) '("on" "true"))
464 (and set-anyway (face-underline-p face)))))
465 )
466 (if fn
467 (condition-case ()
468 (cond ((string= fn "italic")
469 (make-face-italic face))
470 ((string= fn "bold")
471 (make-face-bold face))
472 ((string= fn "bold-italic")
473 (make-face-bold-italic face))
474 (t
475 (set-face-font face fn frame)))
476 (error
477 (if (member fn '("italic" "bold" "bold-italic"))
478 (message "no %s version found for face `%s'" fn name)
479 (message "font `%s' not found for face `%s'" fn name)))))
480 (if fg
481 (condition-case ()
482 (set-face-foreground face fg frame)
483 (error (message "color `%s' not allocated for face `%s'" fg name))))
484 (if bg
485 (condition-case ()
486 (set-face-background face bg frame)
487 (error (message "color `%s' not allocated for face `%s'" bg name))))
488 (if bgp
489 (condition-case ()
490 (set-face-stipple face bgp frame)
491 (error (message "pixmap `%s' not found for face `%s'" bgp name))))
492 (if (or ulp set-anyway)
493 (set-face-underline-p face ulp frame))
494 )))
495 face)
496
497 (defun copy-face (old-face new-face &optional frame new-frame)
498 "Define a face just like OLD-FACE, with name NEW-FACE.
499 If NEW-FACE already exists as a face, it is modified to be like OLD-FACE.
500 If it doesn't already exist, it is created.
501
502 If the optional argument FRAME is given as a frame,
503 NEW-FACE is changed on FRAME only.
504 If FRAME is t, the frame-independent default specification for OLD-FACE
505 is copied to NEW-FACE.
506 If FRAME is nil, copying is done for the frame-independent defaults
507 and for each existing frame.
508 If the optional fourth argument NEW-FRAME is given,
509 copy the information from face OLD-FACE on frame FRAME
510 to NEW-FACE on frame NEW-FRAME."
511 (or new-frame (setq new-frame frame))
512 (let ((inhibit-quit t))
513 (if (null frame)
514 (let ((frames (frame-list)))
515 (while frames
516 (copy-face old-face new-face (car frames))
517 (setq frames (cdr frames)))
518 (copy-face old-face new-face t))
519 (setq old-face (internal-get-face old-face frame))
520 (setq new-face (or (internal-find-face new-face new-frame)
521 (make-face new-face)))
522 (condition-case nil
523 ;; A face that has a global symbolic font modifier such as `bold'
524 ;; might legitimately get an error here.
525 ;; Use the frame's default font in that case.
526 (set-face-font new-face (face-font old-face frame) new-frame)
527 (error
528 (set-face-font new-face nil new-frame)))
529 (set-face-foreground new-face (face-foreground old-face frame) new-frame)
530 (set-face-background new-face (face-background old-face frame) new-frame)
531 (set-face-stipple new-face
532 (face-stipple old-face frame)
533 new-frame)
534 (set-face-underline-p new-face (face-underline-p old-face frame)
535 new-frame))
536 new-face))
537
538 (defun face-equal (face1 face2 &optional frame)
539 "True if the faces FACE1 and FACE2 display in the same way."
540 (setq face1 (internal-get-face face1 frame)
541 face2 (internal-get-face face2 frame))
542 (and (equal (face-foreground face1 frame) (face-foreground face2 frame))
543 (equal (face-background face1 frame) (face-background face2 frame))
544 (equal (face-font face1 frame) (face-font face2 frame))
545 (eq (face-underline-p face1 frame) (face-underline-p face2 frame))
546 (equal (face-stipple face1 frame)
547 (face-stipple face2 frame))))
548
549 (defun face-differs-from-default-p (face &optional frame)
550 "True if face FACE displays differently from the default face, on FRAME.
551 A face is considered to be ``the same'' as the default face if it is
552 actually specified in the same way (equivalent fonts, etc) or if it is
553 fully unspecified, and thus inherits the attributes of any face it
554 is displayed on top of.
555
556 The optional argument FRAME specifies which frame to test;
557 if FRAME is t, test the default for new frames.
558 If FRAME is nil or omitted, test the selected frame."
559 (let ((default (internal-get-face 'default frame)))
560 (setq face (internal-get-face face frame))
561 (not (and (or (equal (face-foreground default frame)
562 (face-foreground face frame))
563 (null (face-foreground face frame)))
564 (or (equal (face-background default frame)
565 (face-background face frame))
566 (null (face-background face frame)))
567 (or (equal (face-font default frame) (face-font face frame))
568 (null (face-font face frame)))
569 (or (equal (face-stipple default frame)
570 (face-stipple face frame))
571 (null (face-stipple face frame)))
572 (equal (face-underline-p default frame)
573 (face-underline-p face frame))
574 ))))
575
576 (defun face-nontrivial-p (face &optional frame)
577 "True if face FACE has some non-nil attribute.
578 The optional argument FRAME specifies which frame to test;
579 if FRAME is t, test the default for new frames.
580 If FRAME is nil or omitted, test the selected frame."
581 (setq face (internal-get-face face frame))
582 (or (face-foreground face frame)
583 (face-background face frame)
584 (face-font face frame)
585 (face-stipple face frame)
586 (face-underline-p face frame)))
587
588
589 (defun invert-face (face &optional frame)
590 "Swap the foreground and background colors of face FACE.
591 If the face doesn't specify both foreground and background, then
592 set its foreground and background to the default background and foreground."
593 (interactive (list (read-face-name "Invert face: ")))
594 (setq face (internal-get-face face frame))
595 (let ((fg (face-foreground face frame))
596 (bg (face-background face frame)))
597 (if (or fg bg)
598 (progn
599 (set-face-foreground face bg frame)
600 (set-face-background face fg frame))
601 (set-face-foreground face (or (face-background 'default frame)
602 (cdr (assq 'background-color (frame-parameters frame))))
603 frame)
604 (set-face-background face (or (face-foreground 'default frame)
605 (cdr (assq 'foreground-color (frame-parameters frame))))
606 frame)))
607 face)
608
609
610 (defun internal-try-face-font (face font &optional frame)
611 "Like set-face-font, but returns nil on failure instead of an error."
612 (condition-case ()
613 (set-face-font face font frame)
614 (error nil)))
615 \f
616 ;; Manipulating font names.
617
618 (defconst x-font-regexp nil)
619 (defconst x-font-regexp-head nil)
620 (defconst x-font-regexp-weight nil)
621 (defconst x-font-regexp-slant nil)
622
623 (defconst x-font-regexp-weight-subnum 1)
624 (defconst x-font-regexp-slant-subnum 2)
625 (defconst x-font-regexp-swidth-subnum 3)
626 (defconst x-font-regexp-adstyle-subnum 4)
627
628 ;;; Regexps matching font names in "Host Portable Character Representation."
629 ;;;
630 (let ((- "[-?]")
631 (foundry "[^-]+")
632 (family "[^-]+")
633 (weight "\\(bold\\|demibold\\|medium\\)") ; 1
634 ; (weight\? "\\(\\*\\|bold\\|demibold\\|medium\\|\\)") ; 1
635 (weight\? "\\([^-]*\\)") ; 1
636 (slant "\\([ior]\\)") ; 2
637 ; (slant\? "\\([ior?*]?\\)") ; 2
638 (slant\? "\\([^-]?\\)") ; 2
639 ; (swidth "\\(\\*\\|normal\\|semicondensed\\|\\)") ; 3
640 (swidth "\\([^-]*\\)") ; 3
641 ; (adstyle "\\(\\*\\|sans\\|\\)") ; 4
642 (adstyle "\\([^-]*\\)") ; 4
643 (pixelsize "[0-9]+")
644 (pointsize "[0-9][0-9]+")
645 (resx "[0-9][0-9]+")
646 (resy "[0-9][0-9]+")
647 (spacing "[cmp?*]")
648 (avgwidth "[0-9]+")
649 (registry "[^-]+")
650 (encoding "[^-]+")
651 )
652 (setq x-font-regexp
653 (concat "\\`\\*?[-?*]"
654 foundry - family - weight\? - slant\? - swidth - adstyle -
655 pixelsize - pointsize - resx - resy - spacing - avgwidth -
656 registry - encoding "\\*?\\'"
657 ))
658 (setq x-font-regexp-head
659 (concat "\\`[-?*]" foundry - family - weight\? - slant\?
660 "\\([-*?]\\|\\'\\)"))
661 (setq x-font-regexp-slant (concat - slant -))
662 (setq x-font-regexp-weight (concat - weight -))
663 nil)
664
665 (defun x-resolve-font-name (pattern &optional face frame)
666 "Return a font name matching PATTERN.
667 All wildcards in PATTERN become substantiated.
668 If PATTERN is nil, return the name of the frame's base font, which never
669 contains wildcards.
670 Given optional arguments FACE and FRAME, return a font which is
671 also the same size as FACE on FRAME, or fail."
672 (or (symbolp face)
673 (setq face (face-name face)))
674 (and (eq frame t)
675 (setq frame nil))
676 (if pattern
677 ;; Note that x-list-fonts has code to handle a face with nil as its font.
678 (let ((fonts (x-list-fonts pattern face frame 1)))
679 (or fonts
680 (if face
681 (if (string-match "\\*" pattern)
682 (if (null (face-font face))
683 (error "No matching fonts are the same height as the frame default font")
684 (error "No matching fonts are the same height as face `%s'" face))
685 (if (null (face-font face))
686 (error "Height of font `%s' doesn't match the frame default font"
687 pattern)
688 (error "Height of font `%s' doesn't match face `%s'"
689 pattern face)))
690 (error "No fonts match `%s'" pattern)))
691 (car fonts))
692 (cdr (assq 'font (frame-parameters (selected-frame))))))
693
694 (defun x-frob-font-weight (font which)
695 (let ((case-fold-search t))
696 (cond ((string-match x-font-regexp font)
697 (concat (substring font 0
698 (match-beginning x-font-regexp-weight-subnum))
699 which
700 (substring font (match-end x-font-regexp-weight-subnum)
701 (match-beginning x-font-regexp-adstyle-subnum))
702 ;; Replace the ADD_STYLE_NAME field with *
703 ;; because the info in it may not be the same
704 ;; for related fonts.
705 "*"
706 (substring font (match-end x-font-regexp-adstyle-subnum))))
707 ((string-match x-font-regexp-head font)
708 (concat (substring font 0 (match-beginning 1)) which
709 (substring font (match-end 1))))
710 ((string-match x-font-regexp-weight font)
711 (concat (substring font 0 (match-beginning 1)) which
712 (substring font (match-end 1)))))))
713
714 (defun x-frob-font-slant (font which)
715 (let ((case-fold-search t))
716 (cond ((string-match x-font-regexp font)
717 (concat (substring font 0
718 (match-beginning x-font-regexp-slant-subnum))
719 which
720 (substring font (match-end x-font-regexp-slant-subnum)
721 (match-beginning x-font-regexp-adstyle-subnum))
722 ;; Replace the ADD_STYLE_NAME field with *
723 ;; because the info in it may not be the same
724 ;; for related fonts.
725 "*"
726 (substring font (match-end x-font-regexp-adstyle-subnum))))
727 ((string-match x-font-regexp-head font)
728 (concat (substring font 0 (match-beginning 2)) which
729 (substring font (match-end 2))))
730 ((string-match x-font-regexp-slant font)
731 (concat (substring font 0 (match-beginning 1)) which
732 (substring font (match-end 1)))))))
733
734 (defun x-make-font-bold (font)
735 "Given an X font specification, make a bold version of it.
736 If that can't be done, return nil."
737 (x-frob-font-weight font "bold"))
738
739 (defun x-make-font-demibold (font)
740 "Given an X font specification, make a demibold version of it.
741 If that can't be done, return nil."
742 (x-frob-font-weight font "demibold"))
743
744 (defun x-make-font-unbold (font)
745 "Given an X font specification, make a non-bold version of it.
746 If that can't be done, return nil."
747 (x-frob-font-weight font "medium"))
748
749 (defun x-make-font-italic (font)
750 "Given an X font specification, make an italic version of it.
751 If that can't be done, return nil."
752 (x-frob-font-slant font "i"))
753
754 (defun x-make-font-oblique (font) ; you say tomayto...
755 "Given an X font specification, make an oblique version of it.
756 If that can't be done, return nil."
757 (x-frob-font-slant font "o"))
758
759 (defun x-make-font-unitalic (font)
760 "Given an X font specification, make a non-italic version of it.
761 If that can't be done, return nil."
762 (x-frob-font-slant font "r"))
763 \f
764 ;;; non-X-specific interface
765
766 (defun make-face-bold (face &optional frame noerror)
767 "Make the font of the given face be bold, if possible.
768 If NOERROR is non-nil, return nil on failure."
769 (interactive (list (read-face-name "Make which face bold: ")))
770 (if (and (eq frame t) (listp (face-font face t)))
771 (set-face-font face (if (memq 'italic (face-font face t))
772 '(bold italic) '(bold))
773 t)
774 (let (font)
775 (if (null frame)
776 (let ((frames (frame-list)))
777 ;; Make this face bold in global-face-data.
778 (make-face-bold face t noerror)
779 ;; Make this face bold in each frame.
780 (while frames
781 (make-face-bold face (car frames) noerror)
782 (setq frames (cdr frames))))
783 (setq face (internal-get-face face frame))
784 (setq font (or (face-font face frame)
785 (face-font face t)))
786 (if (listp font)
787 (setq font nil))
788 (setq font (or font
789 (face-font 'default frame)
790 (cdr (assq 'font (frame-parameters frame)))))
791 (or (and font (make-face-bold-internal face frame font))
792 ;; We failed to find a bold version of the font.
793 noerror
794 (error "No bold version of %S" font))))))
795
796 (defun make-face-bold-internal (face frame font)
797 (let (f2)
798 (or (and (setq f2 (x-make-font-bold font))
799 (internal-try-face-font face f2 frame))
800 (and (setq f2 (x-make-font-demibold font))
801 (internal-try-face-font face f2 frame)))))
802
803 (defun make-face-italic (face &optional frame noerror)
804 "Make the font of the given face be italic, if possible.
805 If NOERROR is non-nil, return nil on failure."
806 (interactive (list (read-face-name "Make which face italic: ")))
807 (if (and (eq frame t) (listp (face-font face t)))
808 (set-face-font face (if (memq 'bold (face-font face t))
809 '(bold italic) '(italic))
810 t)
811 (let (font)
812 (if (null frame)
813 (let ((frames (frame-list)))
814 ;; Make this face italic in global-face-data.
815 (make-face-italic face t noerror)
816 ;; Make this face italic in each frame.
817 (while frames
818 (make-face-italic face (car frames) noerror)
819 (setq frames (cdr frames))))
820 (setq face (internal-get-face face frame))
821 (setq font (or (face-font face frame)
822 (face-font face t)))
823 (if (listp font)
824 (setq font nil))
825 (setq font (or font
826 (face-font 'default frame)
827 (cdr (assq 'font (frame-parameters frame)))))
828 (or (and font (make-face-italic-internal face frame font))
829 ;; We failed to find an italic version of the font.
830 noerror
831 (error "No italic version of %S" font))))))
832
833 (defun make-face-italic-internal (face frame font)
834 (let (f2)
835 (or (and (setq f2 (x-make-font-italic font))
836 (internal-try-face-font face f2 frame))
837 (and (setq f2 (x-make-font-oblique font))
838 (internal-try-face-font face f2 frame)))))
839
840 (defun make-face-bold-italic (face &optional frame noerror)
841 "Make the font of the given face be bold and italic, if possible.
842 If NOERROR is non-nil, return nil on failure."
843 (interactive (list (read-face-name "Make which face bold-italic: ")))
844 (if (and (eq frame t) (listp (face-font face t)))
845 (set-face-font face '(bold italic) t)
846 (let (font)
847 (if (null frame)
848 (let ((frames (frame-list)))
849 ;; Make this face bold-italic in global-face-data.
850 (make-face-bold-italic face t noerror)
851 ;; Make this face bold in each frame.
852 (while frames
853 (make-face-bold-italic face (car frames) noerror)
854 (setq frames (cdr frames))))
855 (setq face (internal-get-face face frame))
856 (setq font (or (face-font face frame)
857 (face-font face t)))
858 (if (listp font)
859 (setq font nil))
860 (setq font (or font
861 (face-font 'default frame)
862 (cdr (assq 'font (frame-parameters frame)))))
863 (or (and font (make-face-bold-italic-internal face frame font))
864 ;; We failed to find a bold italic version.
865 noerror
866 (error "No bold italic version of %S" font))))))
867
868 (defun make-face-bold-italic-internal (face frame font)
869 (let (f2 f3)
870 (or (and (setq f2 (x-make-font-italic font))
871 (not (equal font f2))
872 (setq f3 (x-make-font-bold f2))
873 (not (equal f2 f3))
874 (internal-try-face-font face f3 frame))
875 (and (setq f2 (x-make-font-oblique font))
876 (not (equal font f2))
877 (setq f3 (x-make-font-bold f2))
878 (not (equal f2 f3))
879 (internal-try-face-font face f3 frame))
880 (and (setq f2 (x-make-font-italic font))
881 (not (equal font f2))
882 (setq f3 (x-make-font-demibold f2))
883 (not (equal f2 f3))
884 (internal-try-face-font face f3 frame))
885 (and (setq f2 (x-make-font-oblique font))
886 (not (equal font f2))
887 (setq f3 (x-make-font-demibold f2))
888 (not (equal f2 f3))
889 (internal-try-face-font face f3 frame)))))
890
891 (defun make-face-unbold (face &optional frame noerror)
892 "Make the font of the given face be non-bold, if possible.
893 If NOERROR is non-nil, return nil on failure."
894 (interactive (list (read-face-name "Make which face non-bold: ")))
895 (if (and (eq frame t) (listp (face-font face t)))
896 (set-face-font face (if (memq 'italic (face-font face t))
897 '(italic) nil)
898 t)
899 (let (font font1)
900 (if (null frame)
901 (let ((frames (frame-list)))
902 ;; Make this face unbold in global-face-data.
903 (make-face-unbold face t noerror)
904 ;; Make this face unbold in each frame.
905 (while frames
906 (make-face-unbold face (car frames) noerror)
907 (setq frames (cdr frames))))
908 (setq face (internal-get-face face frame))
909 (setq font1 (or (face-font face frame)
910 (face-font face t)))
911 (if (listp font1)
912 (setq font1 nil))
913 (setq font1 (or font1
914 (face-font 'default frame)
915 (cdr (assq 'font (frame-parameters frame)))))
916 (setq font (and font1 (x-make-font-unbold font1)))
917 (or (if font (internal-try-face-font face font frame))
918 noerror
919 (error "No unbold version of %S" font1))))))
920
921 (defun make-face-unitalic (face &optional frame noerror)
922 "Make the font of the given face be non-italic, if possible.
923 If NOERROR is non-nil, return nil on failure."
924 (interactive (list (read-face-name "Make which face non-italic: ")))
925 (if (and (eq frame t) (listp (face-font face t)))
926 (set-face-font face (if (memq 'bold (face-font face t))
927 '(bold) nil)
928 t)
929 (let (font font1)
930 (if (null frame)
931 (let ((frames (frame-list)))
932 ;; Make this face unitalic in global-face-data.
933 (make-face-unitalic face t noerror)
934 ;; Make this face unitalic in each frame.
935 (while frames
936 (make-face-unitalic face (car frames) noerror)
937 (setq frames (cdr frames))))
938 (setq face (internal-get-face face frame))
939 (setq font1 (or (face-font face frame)
940 (face-font face t)))
941 (if (listp font1)
942 (setq font1 nil))
943 (setq font1 (or font1
944 (face-font 'default frame)
945 (cdr (assq 'font (frame-parameters frame)))))
946 (setq font (and font1 (x-make-font-unitalic font1)))
947 (or (if font (internal-try-face-font face font frame))
948 noerror
949 (error "No unitalic version of %S" font1))))))
950 \f
951 (defvar list-faces-sample-text
952 "abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ"
953 "*Text string to display as the sample text for `list-faces-display'.")
954
955 ;; The name list-faces would be more consistent, but let's avoid a conflict
956 ;; with Lucid, which uses that name differently.
957 (defun list-faces-display ()
958 "List all faces, using the same sample text in each.
959 The sample text is a string that comes from the variable
960 `list-faces-sample-text'.
961
962 It is possible to give a particular face name different appearances in
963 different frames. This command shows the appearance in the
964 selected frame."
965 (interactive)
966 (let ((faces (sort (face-list) (function string-lessp)))
967 (face nil)
968 (frame (selected-frame))
969 disp-frame window)
970 (with-output-to-temp-buffer "*Faces*"
971 (save-excursion
972 (set-buffer standard-output)
973 (setq truncate-lines t)
974 (while faces
975 (setq face (car faces))
976 (setq faces (cdr faces))
977 (insert (format "%25s " (symbol-name face)))
978 (let ((beg (point)))
979 (insert list-faces-sample-text)
980 (insert "\n")
981 (put-text-property beg (1- (point)) 'face face)
982 ;; If the sample text has multiple lines, line up all of them.
983 (goto-char beg)
984 (forward-line 1)
985 (while (not (eobp))
986 (insert " ")
987 (forward-line 1))))
988 (goto-char (point-min))))
989 ;; If the *Faces* buffer appears in a different frame,
990 ;; copy all the face definitions from FRAME,
991 ;; so that the display will reflect the frame that was selected.
992 (setq window (get-buffer-window (get-buffer "*Faces*") t))
993 (setq disp-frame (if window (window-frame window)
994 (car (frame-list))))
995 (or (eq frame disp-frame)
996 (let ((faces (face-list)))
997 (while faces
998 (copy-face (car faces) (car faces) frame disp-frame)
999 (setq faces (cdr faces)))))))
1000
1001 (defun describe-face (face)
1002 "Display the properties of face FACE."
1003 (interactive (list (read-face-name "Describe face: ")))
1004 (with-output-to-temp-buffer "*Help*"
1005 (princ "Properties of face `")
1006 (princ (face-name face))
1007 (princ "':") (terpri)
1008 (princ "Foreground: ") (princ (face-foreground face)) (terpri)
1009 (princ "Background: ") (princ (face-background face)) (terpri)
1010 (princ " Font: ") (princ (face-font face)) (terpri)
1011 (princ "Underlined: ") (princ (if (face-underline-p face) "yes" "no")) (terpri)
1012 (princ " Stipple: ") (princ (or (face-stipple face) "none"))))
1013 \f
1014 ;;; Make the standard faces.
1015 ;;; The C code knows the default and modeline faces as faces 0 and 1,
1016 ;;; so they must be the first two faces made.
1017 (defun face-initialize ()
1018 (make-face 'default)
1019 (make-face 'modeline)
1020 (make-face 'highlight)
1021
1022 ;; These aren't really special in any way, but they're nice to have around.
1023
1024 (make-face 'bold)
1025 (make-face 'italic)
1026 (make-face 'bold-italic)
1027 (make-face 'region)
1028 (make-face 'secondary-selection)
1029 (make-face 'underline)
1030
1031 (setq region-face (face-id 'region))
1032
1033 ;; Specify the global properties of these faces
1034 ;; so they will come out right on new frames.
1035
1036 (make-face-bold 'bold t)
1037 (make-face-italic 'italic t)
1038 (make-face-bold-italic 'bold-italic t)
1039
1040 (set-face-background 'highlight '("darkseagreen2" "green" t) t)
1041 (set-face-background 'region '("gray" underline) t)
1042 (set-face-background 'secondary-selection '("paleturquoise" "green" t) t)
1043 (set-face-background 'modeline '(t) t)
1044 (set-face-underline-p 'underline t t)
1045
1046 ;; Set up the faces of all existing X Window frames
1047 ;; from those global properties, unless already set in a given frame.
1048
1049 (let ((frames (frame-list)))
1050 (while frames
1051 (if (not (memq (framep (car frames)) '(t nil)))
1052 (let ((frame (car frames))
1053 (rest global-face-data))
1054 (while rest
1055 (let ((face (car (car rest))))
1056 (or (face-differs-from-default-p face)
1057 (face-fill-in face (cdr (car rest)) frame)))
1058 (setq rest (cdr rest)))))
1059 (setq frames (cdr frames)))))
1060
1061 \f
1062 ;; Like x-create-frame but also set up the faces.
1063
1064 (defun x-create-frame-with-faces (&optional parameters)
1065 ;; Read this frame's geometry resource, if it has an explicit name,
1066 ;; and put the specs into PARAMETERS.
1067 (let* ((name (or (cdr (assq 'name parameters))
1068 (cdr (assq 'name default-frame-alist))))
1069 (x-resource-name name)
1070 (res-geometry (if name (x-get-resource "geometry" "Geometry")))
1071 parsed)
1072 (if res-geometry
1073 (progn
1074 (setq parsed (x-parse-geometry res-geometry))
1075 ;; If the resource specifies a position,
1076 ;; call the position and size "user-specified".
1077 (if (or (assq 'top parsed) (assq 'left parsed))
1078 (setq parsed (cons '(user-position . t)
1079 (cons '(user-size . t) parsed))))
1080 ;; Put the geometry parameters at the end.
1081 ;; Copy default-frame-alist so that they go after it.
1082 (setq parameters (append parameters
1083 default-frame-alist
1084 parsed)))))
1085 (let (frame)
1086 (if (null global-face-data)
1087 (setq frame (x-create-frame parameters))
1088 (let* ((visibility-spec (assq 'visibility parameters))
1089 (faces (copy-alist global-face-data))
1090 success
1091 (rest faces))
1092 (setq frame (x-create-frame (cons '(visibility . nil) parameters)))
1093 (unwind-protect
1094 (progn
1095 (set-frame-face-alist frame faces)
1096
1097 (if (cdr (or (assq 'reverse parameters)
1098 (assq 'reverse default-frame-alist)
1099 (let ((resource (x-get-resource "reverseVideo"
1100 "ReverseVideo")))
1101 (if resource
1102 (cons nil (member (downcase resource)
1103 '("on" "true")))))))
1104 (let* ((params (frame-parameters frame))
1105 (bg (cdr (assq 'foreground-color params)))
1106 (fg (cdr (assq 'background-color params))))
1107 (modify-frame-parameters frame
1108 (list (cons 'foreground-color fg)
1109 (cons 'background-color bg)))
1110 (if (equal bg (cdr (assq 'border-color params)))
1111 (modify-frame-parameters frame
1112 (list (cons 'border-color fg))))
1113 (if (equal bg (cdr (assq 'mouse-color params)))
1114 (modify-frame-parameters frame
1115 (list (cons 'mouse-color fg))))
1116 (if (equal bg (cdr (assq 'cursor-color params)))
1117 (modify-frame-parameters frame
1118 (list (cons 'cursor-color fg))))))
1119 ;; Copy the vectors that represent the faces.
1120 ;; Also fill them in from X resources.
1121 (while rest
1122 (let ((global (cdr (car rest))))
1123 (setcdr (car rest) (vector 'face
1124 (face-name (cdr (car rest)))
1125 (face-id (cdr (car rest)))
1126 nil nil nil nil nil))
1127 (face-fill-in (car (car rest)) global frame))
1128 (make-face-x-resource-internal (cdr (car rest)) frame t)
1129 (setq rest (cdr rest)))
1130 (if (null visibility-spec)
1131 (make-frame-visible frame)
1132 (modify-frame-parameters frame (list visibility-spec)))
1133 (setq success t))
1134 (or success
1135 (delete-frame frame)))))
1136 ;; Set up the background-mode frame parameter
1137 ;; so that programs can decide good ways of highlighting
1138 ;; on this frame.
1139 (let ((bg-resource (x-get-resource ".backgroundMode"
1140 "BackgroundMode"))
1141 (params (frame-parameters frame))
1142 (bg-mode))
1143 (setq bg-mode
1144 (cond (bg-resource (intern (downcase bg-resource)))
1145 ((< (apply '+ (x-color-values
1146 (cdr (assq 'background-color params))
1147 frame))
1148 ;; Just looking at the screen,
1149 ;; colors whose values add up to .6 of the white total
1150 ;; still look dark to me.
1151 (* (apply '+ (x-color-values "white" frame)) .6))
1152 'dark)
1153 (t 'light)))
1154 (modify-frame-parameters frame
1155 (list (cons 'background-mode bg-mode)
1156 (cons 'display-type
1157 (cond ((x-display-color-p frame)
1158 'color)
1159 ((x-display-grayscale-p frame)
1160 'grayscale)
1161 (t 'mono))))))
1162 frame))
1163
1164 ;; Update a frame's faces when we change its default font.
1165 (defun frame-update-faces (frame)
1166 (let* ((faces global-face-data)
1167 (rest faces))
1168 (while rest
1169 (let* ((face (car (car rest)))
1170 (font (face-font face t)))
1171 (if (listp font)
1172 (let ((bold (memq 'bold font))
1173 (italic (memq 'italic font)))
1174 ;; Ignore any previous (string-valued) font, it might not even
1175 ;; be the right size anymore.
1176 (set-face-font face nil frame)
1177 (cond ((and bold italic)
1178 (make-face-bold-italic face frame t))
1179 (bold
1180 (make-face-bold face frame t))
1181 (italic
1182 (make-face-italic face frame t)))))
1183 (setq rest (cdr rest)))
1184 frame)))
1185
1186 ;; Update the colors of FACE, after FRAME's own colors have been changed.
1187 ;; This applies only to faces with global color specifications
1188 ;; that are not simple constants.
1189 (defun frame-update-face-colors (frame)
1190 (let ((faces global-face-data))
1191 (while faces
1192 (condition-case nil
1193 (let* ((data (cdr (car faces)))
1194 (face (car (car faces)))
1195 (foreground (face-foreground data))
1196 (background (face-background data)))
1197 ;; If the global spec is a specific color,
1198 ;; which doesn't depend on the frame's attributes,
1199 ;; we don't need to recalculate it now.
1200 (or (listp foreground)
1201 (setq foreground nil))
1202 (or (listp background)
1203 (setq background nil))
1204 ;; If we are going to frob this face at all,
1205 ;; reinitialize it first.
1206 (if (or foreground background)
1207 (progn (set-face-foreground face nil frame)
1208 (set-face-background face nil frame)))
1209 (if foreground
1210 (face-try-color-list 'set-face-foreground
1211 face foreground frame))
1212 (if background
1213 (face-try-color-list 'set-face-background
1214 face background frame)))
1215 (error nil))
1216 (setq faces (cdr faces)))))
1217
1218 ;; Fill in the face FACE from frame-independent face data DATA.
1219 ;; DATA should be the non-frame-specific ("global") face vector
1220 ;; for the face. FACE should be a face name or face object.
1221 ;; FRAME is the frame to act on; it must be an actual frame, not nil or t.
1222 (defun face-fill-in (face data frame)
1223 (condition-case nil
1224 (let ((foreground (face-foreground data))
1225 (background (face-background data))
1226 (font (face-font data))
1227 (stipple (face-stipple data)))
1228 (set-face-underline-p face (face-underline-p data) frame)
1229 (if foreground
1230 (face-try-color-list 'set-face-foreground
1231 face foreground frame))
1232 (if background
1233 (face-try-color-list 'set-face-background
1234 face background frame))
1235 (if (listp font)
1236 (let ((bold (memq 'bold font))
1237 (italic (memq 'italic font)))
1238 (cond ((and bold italic)
1239 (make-face-bold-italic face frame))
1240 (bold
1241 (make-face-bold face frame))
1242 (italic
1243 (make-face-italic face frame))))
1244 (if font
1245 (set-face-font face font frame)))
1246 (if stipple
1247 (set-face-stipple face stipple frame)))
1248 (error nil)))
1249
1250 ;; Assuming COLOR is a valid color name,
1251 ;; return t if it can be displayed on FRAME.
1252 (defun face-color-supported-p (frame color background-p)
1253 (and window-system
1254 (or (x-display-color-p frame)
1255 ;; A black-and-white display can implement these.
1256 (member color '("black" "white"))
1257 ;; A black-and-white display can fake gray for background.
1258 (and background-p
1259 (face-color-gray-p color frame))
1260 ;; A grayscale display can implement colors that are gray (more or less).
1261 (and (x-display-grayscale-p frame)
1262 (face-color-gray-p color frame)))))
1263
1264 ;; Use FUNCTION to store a color in FACE on FRAME.
1265 ;; COLORS is either a single color or a list of colors.
1266 ;; If it is a list, try the colors one by one until one of them
1267 ;; succeeds. We signal an error only if all the colors failed.
1268 ;; t as COLORS or as an element of COLORS means to invert the face.
1269 ;; That can't fail, so any subsequent elements after the t are ignored.
1270 (defun face-try-color-list (function face colors frame)
1271 (if (stringp colors)
1272 (if (face-color-supported-p frame colors
1273 (eq function 'set-face-background))
1274 (funcall function face colors frame))
1275 (if (eq colors t)
1276 (invert-face face frame)
1277 (let (done)
1278 (while (and colors (not done))
1279 (if (or (memq (car colors) '(t underline))
1280 (face-color-supported-p frame (car colors)
1281 (eq function 'set-face-background)))
1282 (if (cdr colors)
1283 ;; If there are more colors to try, catch errors
1284 ;; and set `done' if we succeed.
1285 (condition-case nil
1286 (progn
1287 (cond ((eq (car colors) t)
1288 (invert-face face frame))
1289 ((eq (car colors) 'underline)
1290 (set-face-underline-p face t frame))
1291 (t
1292 (funcall function face (car colors) frame)))
1293 (setq done t))
1294 (error nil))
1295 ;; If this is the last color, let the error get out if it fails.
1296 ;; If it succeeds, we will exit anyway after this iteration.
1297 (cond ((eq (car colors) t)
1298 (invert-face face frame))
1299 ((eq (car colors) 'underline)
1300 (set-face-underline-p face t frame))
1301 (t
1302 (funcall function face (car colors) frame)))))
1303 (setq colors (cdr colors)))))))
1304
1305 ;; If we are already using x-window frames, initialize faces for them.
1306 (if (memq (framep (selected-frame)) '(x ms-windows))
1307 (face-initialize))
1308
1309 (provide 'faces)
1310
1311 ;;; faces.el ends here