]> code.delx.au - gnu-emacs/blob - lisp/faces.el
(tmm-get-keymap): Recognize explicit lambda expressions as valid.
[gnu-emacs] / 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 w32))
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 w32))
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 (let* ((frame-bg (cdr (assq 'background-color (frame-parameters frame))))
602 (default-bg (or (face-background 'default frame)
603 frame-bg))
604 (frame-fg (cdr (assq 'foreground-color (frame-parameters frame))))
605 (default-fg (or (face-foreground 'default frame)
606 frame-fg)))
607 (set-face-foreground face default-bg frame)
608 (set-face-background face default-fg frame))))
609 face)
610
611
612 (defun internal-try-face-font (face font &optional frame)
613 "Like set-face-font, but returns nil on failure instead of an error."
614 (condition-case ()
615 (set-face-font face font frame)
616 (error nil)))
617 \f
618 ;; Manipulating font names.
619
620 (defvar x-font-regexp nil)
621 (defvar x-font-regexp-head nil)
622 (defvar x-font-regexp-weight nil)
623 (defvar x-font-regexp-slant nil)
624
625 (defconst x-font-regexp-weight-subnum 1)
626 (defconst x-font-regexp-slant-subnum 2)
627 (defconst x-font-regexp-swidth-subnum 3)
628 (defconst x-font-regexp-adstyle-subnum 4)
629
630 ;;; Regexps matching font names in "Host Portable Character Representation."
631 ;;;
632 (let ((- "[-?]")
633 (foundry "[^-]+")
634 (family "[^-]+")
635 (weight "\\(bold\\|demibold\\|medium\\)") ; 1
636 ; (weight\? "\\(\\*\\|bold\\|demibold\\|medium\\|\\)") ; 1
637 (weight\? "\\([^-]*\\)") ; 1
638 (slant "\\([ior]\\)") ; 2
639 ; (slant\? "\\([ior?*]?\\)") ; 2
640 (slant\? "\\([^-]?\\)") ; 2
641 ; (swidth "\\(\\*\\|normal\\|semicondensed\\|\\)") ; 3
642 (swidth "\\([^-]*\\)") ; 3
643 ; (adstyle "\\(\\*\\|sans\\|\\)") ; 4
644 (adstyle "\\([^-]*\\)") ; 4
645 (pixelsize "[0-9]+")
646 (pointsize "[0-9][0-9]+")
647 (resx "[0-9][0-9]+")
648 (resy "[0-9][0-9]+")
649 (spacing "[cmp?*]")
650 (avgwidth "[0-9]+")
651 (registry "[^-]+")
652 (encoding "[^-]+")
653 )
654 (setq x-font-regexp
655 (concat "\\`\\*?[-?*]"
656 foundry - family - weight\? - slant\? - swidth - adstyle -
657 pixelsize - pointsize - resx - resy - spacing - avgwidth -
658 registry - encoding "\\*?\\'"
659 ))
660 (setq x-font-regexp-head
661 (concat "\\`[-?*]" foundry - family - weight\? - slant\?
662 "\\([-*?]\\|\\'\\)"))
663 (setq x-font-regexp-slant (concat - slant -))
664 (setq x-font-regexp-weight (concat - weight -))
665 nil)
666
667 (defun x-resolve-font-name (pattern &optional face frame)
668 "Return a font name matching PATTERN.
669 All wildcards in PATTERN become substantiated.
670 If PATTERN is nil, return the name of the frame's base font, which never
671 contains wildcards.
672 Given optional arguments FACE and FRAME, return a font which is
673 also the same size as FACE on FRAME, or fail."
674 (or (symbolp face)
675 (setq face (face-name face)))
676 (and (eq frame t)
677 (setq frame nil))
678 (if pattern
679 ;; Note that x-list-fonts has code to handle a face with nil as its font.
680 (let ((fonts (x-list-fonts pattern face frame 1)))
681 (or fonts
682 (if face
683 (if (string-match "\\*" pattern)
684 (if (null (face-font face))
685 (error "No matching fonts are the same height as the frame default font")
686 (error "No matching fonts are the same height as face `%s'" face))
687 (if (null (face-font face))
688 (error "Height of font `%s' doesn't match the frame default font"
689 pattern)
690 (error "Height of font `%s' doesn't match face `%s'"
691 pattern face)))
692 (error "No fonts match `%s'" pattern)))
693 (car fonts))
694 (cdr (assq 'font (frame-parameters (selected-frame))))))
695
696 (defun x-frob-font-weight (font which)
697 (let ((case-fold-search t))
698 (cond ((string-match x-font-regexp font)
699 (concat (substring font 0
700 (match-beginning x-font-regexp-weight-subnum))
701 which
702 (substring font (match-end x-font-regexp-weight-subnum)
703 (match-beginning x-font-regexp-adstyle-subnum))
704 ;; Replace the ADD_STYLE_NAME field with *
705 ;; because the info in it may not be the same
706 ;; for related fonts.
707 "*"
708 (substring font (match-end x-font-regexp-adstyle-subnum))))
709 ((string-match x-font-regexp-head font)
710 (concat (substring font 0 (match-beginning 1)) which
711 (substring font (match-end 1))))
712 ((string-match x-font-regexp-weight font)
713 (concat (substring font 0 (match-beginning 1)) which
714 (substring font (match-end 1)))))))
715
716 (defun x-frob-font-slant (font which)
717 (let ((case-fold-search t))
718 (cond ((string-match x-font-regexp font)
719 (concat (substring font 0
720 (match-beginning x-font-regexp-slant-subnum))
721 which
722 (substring font (match-end x-font-regexp-slant-subnum)
723 (match-beginning x-font-regexp-adstyle-subnum))
724 ;; Replace the ADD_STYLE_NAME field with *
725 ;; because the info in it may not be the same
726 ;; for related fonts.
727 "*"
728 (substring font (match-end x-font-regexp-adstyle-subnum))))
729 ((string-match x-font-regexp-head font)
730 (concat (substring font 0 (match-beginning 2)) which
731 (substring font (match-end 2))))
732 ((string-match x-font-regexp-slant font)
733 (concat (substring font 0 (match-beginning 1)) which
734 (substring font (match-end 1)))))))
735
736 (defun x-make-font-bold (font)
737 "Given an X font specification, make a bold version of it.
738 If that can't be done, return nil."
739 (x-frob-font-weight font "bold"))
740
741 (defun x-make-font-demibold (font)
742 "Given an X font specification, make a demibold version of it.
743 If that can't be done, return nil."
744 (x-frob-font-weight font "demibold"))
745
746 (defun x-make-font-unbold (font)
747 "Given an X font specification, make a non-bold version of it.
748 If that can't be done, return nil."
749 (x-frob-font-weight font "medium"))
750
751 (defun x-make-font-italic (font)
752 "Given an X font specification, make an italic version of it.
753 If that can't be done, return nil."
754 (x-frob-font-slant font "i"))
755
756 (defun x-make-font-oblique (font) ; you say tomayto...
757 "Given an X font specification, make an oblique version of it.
758 If that can't be done, return nil."
759 (x-frob-font-slant font "o"))
760
761 (defun x-make-font-unitalic (font)
762 "Given an X font specification, make a non-italic version of it.
763 If that can't be done, return nil."
764 (x-frob-font-slant font "r"))
765 \f
766 ;;; non-X-specific interface
767
768 (defun make-face-bold (face &optional frame noerror)
769 "Make the font of the given face be bold, if possible.
770 If NOERROR is non-nil, return nil on failure."
771 (interactive (list (read-face-name "Make which face bold: ")))
772 (if (and (eq frame t) (listp (face-font face t)))
773 (set-face-font face (if (memq 'italic (face-font face t))
774 '(bold italic) '(bold))
775 t)
776 (let (font)
777 (if (null frame)
778 (let ((frames (frame-list)))
779 ;; Make this face bold in global-face-data.
780 (make-face-bold face t noerror)
781 ;; Make this face bold in each frame.
782 (while frames
783 (make-face-bold face (car frames) noerror)
784 (setq frames (cdr frames))))
785 (setq face (internal-get-face face frame))
786 (setq font (or (face-font face frame)
787 (face-font face t)))
788 (if (listp font)
789 (setq font nil))
790 (setq font (or font
791 (face-font 'default frame)
792 (cdr (assq 'font (frame-parameters frame)))))
793 (or (and font (make-face-bold-internal face frame font))
794 ;; We failed to find a bold version of the font.
795 noerror
796 (error "No bold version of %S" font))))))
797
798 (defun make-face-bold-internal (face frame font)
799 (let (f2)
800 (or (and (setq f2 (x-make-font-bold font))
801 (internal-try-face-font face f2 frame))
802 (and (setq f2 (x-make-font-demibold font))
803 (internal-try-face-font face f2 frame)))))
804
805 (defun make-face-italic (face &optional frame noerror)
806 "Make the font of the given face be italic, if possible.
807 If NOERROR is non-nil, return nil on failure."
808 (interactive (list (read-face-name "Make which face italic: ")))
809 (if (and (eq frame t) (listp (face-font face t)))
810 (set-face-font face (if (memq 'bold (face-font face t))
811 '(bold italic) '(italic))
812 t)
813 (let (font)
814 (if (null frame)
815 (let ((frames (frame-list)))
816 ;; Make this face italic in global-face-data.
817 (make-face-italic face t noerror)
818 ;; Make this face italic in each frame.
819 (while frames
820 (make-face-italic face (car frames) noerror)
821 (setq frames (cdr frames))))
822 (setq face (internal-get-face face frame))
823 (setq font (or (face-font face frame)
824 (face-font face t)))
825 (if (listp font)
826 (setq font nil))
827 (setq font (or font
828 (face-font 'default frame)
829 (cdr (assq 'font (frame-parameters frame)))))
830 (or (and font (make-face-italic-internal face frame font))
831 ;; We failed to find an italic version of the font.
832 noerror
833 (error "No italic version of %S" font))))))
834
835 (defun make-face-italic-internal (face frame font)
836 (let (f2)
837 (or (and (setq f2 (x-make-font-italic font))
838 (internal-try-face-font face f2 frame))
839 (and (setq f2 (x-make-font-oblique font))
840 (internal-try-face-font face f2 frame)))))
841
842 (defun make-face-bold-italic (face &optional frame noerror)
843 "Make the font of the given face be bold and italic, if possible.
844 If NOERROR is non-nil, return nil on failure."
845 (interactive (list (read-face-name "Make which face bold-italic: ")))
846 (if (and (eq frame t) (listp (face-font face t)))
847 (set-face-font face '(bold italic) t)
848 (let (font)
849 (if (null frame)
850 (let ((frames (frame-list)))
851 ;; Make this face bold-italic in global-face-data.
852 (make-face-bold-italic face t noerror)
853 ;; Make this face bold in each frame.
854 (while frames
855 (make-face-bold-italic face (car frames) noerror)
856 (setq frames (cdr frames))))
857 (setq face (internal-get-face face frame))
858 (setq font (or (face-font face frame)
859 (face-font face t)))
860 (if (listp font)
861 (setq font nil))
862 (setq font (or font
863 (face-font 'default frame)
864 (cdr (assq 'font (frame-parameters frame)))))
865 (or (and font (make-face-bold-italic-internal face frame font))
866 ;; We failed to find a bold italic version.
867 noerror
868 (error "No bold italic version of %S" font))))))
869
870 (defun make-face-bold-italic-internal (face frame font)
871 (let (f2 f3)
872 (or (and (setq f2 (x-make-font-italic font))
873 (not (equal font f2))
874 (setq f3 (x-make-font-bold f2))
875 (not (equal f2 f3))
876 (internal-try-face-font face f3 frame))
877 (and (setq f2 (x-make-font-oblique font))
878 (not (equal font f2))
879 (setq f3 (x-make-font-bold f2))
880 (not (equal f2 f3))
881 (internal-try-face-font face f3 frame))
882 (and (setq f2 (x-make-font-italic font))
883 (not (equal font f2))
884 (setq f3 (x-make-font-demibold f2))
885 (not (equal f2 f3))
886 (internal-try-face-font face f3 frame))
887 (and (setq f2 (x-make-font-oblique font))
888 (not (equal font f2))
889 (setq f3 (x-make-font-demibold f2))
890 (not (equal f2 f3))
891 (internal-try-face-font face f3 frame)))))
892
893 (defun make-face-unbold (face &optional frame noerror)
894 "Make the font of the given face be non-bold, if possible.
895 If NOERROR is non-nil, return nil on failure."
896 (interactive (list (read-face-name "Make which face non-bold: ")))
897 (if (and (eq frame t) (listp (face-font face t)))
898 (set-face-font face (if (memq 'italic (face-font face t))
899 '(italic) nil)
900 t)
901 (let (font font1)
902 (if (null frame)
903 (let ((frames (frame-list)))
904 ;; Make this face unbold in global-face-data.
905 (make-face-unbold face t noerror)
906 ;; Make this face unbold in each frame.
907 (while frames
908 (make-face-unbold face (car frames) noerror)
909 (setq frames (cdr frames))))
910 (setq face (internal-get-face face frame))
911 (setq font1 (or (face-font face frame)
912 (face-font face t)))
913 (if (listp font1)
914 (setq font1 nil))
915 (setq font1 (or font1
916 (face-font 'default frame)
917 (cdr (assq 'font (frame-parameters frame)))))
918 (setq font (and font1 (x-make-font-unbold font1)))
919 (or (if font (internal-try-face-font face font frame))
920 noerror
921 (error "No unbold version of %S" font1))))))
922
923 (defun make-face-unitalic (face &optional frame noerror)
924 "Make the font of the given face be non-italic, if possible.
925 If NOERROR is non-nil, return nil on failure."
926 (interactive (list (read-face-name "Make which face non-italic: ")))
927 (if (and (eq frame t) (listp (face-font face t)))
928 (set-face-font face (if (memq 'bold (face-font face t))
929 '(bold) nil)
930 t)
931 (let (font font1)
932 (if (null frame)
933 (let ((frames (frame-list)))
934 ;; Make this face unitalic in global-face-data.
935 (make-face-unitalic face t noerror)
936 ;; Make this face unitalic in each frame.
937 (while frames
938 (make-face-unitalic face (car frames) noerror)
939 (setq frames (cdr frames))))
940 (setq face (internal-get-face face frame))
941 (setq font1 (or (face-font face frame)
942 (face-font face t)))
943 (if (listp font1)
944 (setq font1 nil))
945 (setq font1 (or font1
946 (face-font 'default frame)
947 (cdr (assq 'font (frame-parameters frame)))))
948 (setq font (and font1 (x-make-font-unitalic font1)))
949 (or (if font (internal-try-face-font face font frame))
950 noerror
951 (error "No unitalic version of %S" font1))))))
952 \f
953 (defvar list-faces-sample-text
954 "abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ"
955 "*Text string to display as the sample text for `list-faces-display'.")
956
957 ;; The name list-faces would be more consistent, but let's avoid a conflict
958 ;; with Lucid, which uses that name differently.
959 (defun list-faces-display ()
960 "List all faces, using the same sample text in each.
961 The sample text is a string that comes from the variable
962 `list-faces-sample-text'.
963
964 It is possible to give a particular face name different appearances in
965 different frames. This command shows the appearance in the
966 selected frame."
967 (interactive)
968 (let ((faces (sort (face-list) (function string-lessp)))
969 (face nil)
970 (frame (selected-frame))
971 disp-frame window)
972 (with-output-to-temp-buffer "*Faces*"
973 (save-excursion
974 (set-buffer standard-output)
975 (setq truncate-lines t)
976 (while faces
977 (setq face (car faces))
978 (setq faces (cdr faces))
979 (insert (format "%25s " (symbol-name face)))
980 (let ((beg (point)))
981 (insert list-faces-sample-text)
982 (insert "\n")
983 (put-text-property beg (1- (point)) 'face face)
984 ;; If the sample text has multiple lines, line up all of them.
985 (goto-char beg)
986 (forward-line 1)
987 (while (not (eobp))
988 (insert " ")
989 (forward-line 1))))
990 (goto-char (point-min))))
991 ;; If the *Faces* buffer appears in a different frame,
992 ;; copy all the face definitions from FRAME,
993 ;; so that the display will reflect the frame that was selected.
994 (setq window (get-buffer-window (get-buffer "*Faces*") t))
995 (setq disp-frame (if window (window-frame window)
996 (car (frame-list))))
997 (or (eq frame disp-frame)
998 (let ((faces (face-list)))
999 (while faces
1000 (copy-face (car faces) (car faces) frame disp-frame)
1001 (setq faces (cdr faces)))))))
1002
1003 (defun describe-face (face)
1004 "Display the properties of face FACE."
1005 (interactive (list (read-face-name "Describe face: ")))
1006 (with-output-to-temp-buffer "*Help*"
1007 (princ "Properties of face `")
1008 (princ (face-name face))
1009 (princ "':") (terpri)
1010 (princ "Foreground: ") (princ (face-foreground face)) (terpri)
1011 (princ "Background: ") (princ (face-background face)) (terpri)
1012 (princ " Font: ") (princ (face-font face)) (terpri)
1013 (princ "Underlined: ") (princ (if (face-underline-p face) "yes" "no")) (terpri)
1014 (princ " Stipple: ") (princ (or (face-stipple face) "none"))))
1015 \f
1016 ;;; Make the standard faces.
1017 ;;; The C code knows the default and modeline faces as faces 0 and 1,
1018 ;;; so they must be the first two faces made.
1019 (defun face-initialize ()
1020 (make-face 'default)
1021 (make-face 'modeline)
1022 (make-face 'highlight)
1023
1024 ;; These aren't really special in any way, but they're nice to have around.
1025
1026 (make-face 'bold)
1027 (make-face 'italic)
1028 (make-face 'bold-italic)
1029 (make-face 'region)
1030 (make-face 'secondary-selection)
1031 (make-face 'underline)
1032
1033 (setq region-face (face-id 'region))
1034
1035 ;; Specify the global properties of these faces
1036 ;; so they will come out right on new frames.
1037
1038 (make-face-bold 'bold t)
1039 (make-face-italic 'italic t)
1040 (make-face-bold-italic 'bold-italic t)
1041
1042 (set-face-background 'highlight '("darkseagreen2" "green" t) t)
1043 (set-face-background 'region '("gray" underline) t)
1044 (set-face-background 'secondary-selection '("paleturquoise" "green" t) t)
1045 (set-face-background 'modeline '(t) t)
1046 (set-face-underline-p 'underline t t)
1047
1048 ;; Set up the faces of all existing X Window frames
1049 ;; from those global properties, unless already set in a given frame.
1050
1051 (let ((frames (frame-list)))
1052 (while frames
1053 (if (not (memq (framep (car frames)) '(t nil)))
1054 (let ((frame (car frames))
1055 (rest global-face-data))
1056 (while rest
1057 (let ((face (car (car rest))))
1058 (or (face-differs-from-default-p face)
1059 (face-fill-in face (cdr (car rest)) frame)))
1060 (setq rest (cdr rest)))))
1061 (setq frames (cdr frames)))))
1062
1063 \f
1064 ;; Like x-create-frame but also set up the faces.
1065
1066 (defun x-create-frame-with-faces (&optional parameters)
1067 ;; Read this frame's geometry resource, if it has an explicit name,
1068 ;; and put the specs into PARAMETERS.
1069 (let* ((name (or (cdr (assq 'name parameters))
1070 (cdr (assq 'name default-frame-alist))))
1071 (x-resource-name name)
1072 (res-geometry (if name (x-get-resource "geometry" "Geometry")))
1073 parsed)
1074 (if res-geometry
1075 (progn
1076 (setq parsed (x-parse-geometry res-geometry))
1077 ;; If the resource specifies a position,
1078 ;; call the position and size "user-specified".
1079 (if (or (assq 'top parsed) (assq 'left parsed))
1080 (setq parsed (cons '(user-position . t)
1081 (cons '(user-size . t) parsed))))
1082 ;; Put the geometry parameters at the end.
1083 ;; Copy default-frame-alist so that they go after it.
1084 (setq parameters (append parameters
1085 default-frame-alist
1086 parsed)))))
1087 (let (frame)
1088 (if (null global-face-data)
1089 (setq frame (x-create-frame parameters))
1090 (let* ((visibility-spec (assq 'visibility parameters))
1091 (faces (copy-alist global-face-data))
1092 success
1093 (rest faces))
1094 (setq frame (x-create-frame (cons '(visibility . nil) parameters)))
1095 (unwind-protect
1096 (progn
1097 (set-frame-face-alist frame faces)
1098
1099 (if (cdr (or (assq 'reverse parameters)
1100 (assq 'reverse default-frame-alist)
1101 (let ((resource (x-get-resource "reverseVideo"
1102 "ReverseVideo")))
1103 (if resource
1104 (cons nil (member (downcase resource)
1105 '("on" "true")))))))
1106 (let* ((params (frame-parameters frame))
1107 (bg (cdr (assq 'foreground-color params)))
1108 (fg (cdr (assq 'background-color params))))
1109 (modify-frame-parameters frame
1110 (list (cons 'foreground-color fg)
1111 (cons 'background-color bg)))
1112 (if (equal bg (cdr (assq 'border-color params)))
1113 (modify-frame-parameters frame
1114 (list (cons 'border-color fg))))
1115 (if (equal bg (cdr (assq 'mouse-color params)))
1116 (modify-frame-parameters frame
1117 (list (cons 'mouse-color fg))))
1118 (if (equal bg (cdr (assq 'cursor-color params)))
1119 (modify-frame-parameters frame
1120 (list (cons 'cursor-color fg))))))
1121 ;; Copy the vectors that represent the faces.
1122 ;; Also fill them in from X resources.
1123 (while rest
1124 (let ((global (cdr (car rest))))
1125 (setcdr (car rest) (vector 'face
1126 (face-name (cdr (car rest)))
1127 (face-id (cdr (car rest)))
1128 nil nil nil nil nil))
1129 (face-fill-in (car (car rest)) global frame))
1130 (make-face-x-resource-internal (cdr (car rest)) frame t)
1131 (setq rest (cdr rest)))
1132 (if (null visibility-spec)
1133 (make-frame-visible frame)
1134 (modify-frame-parameters frame (list visibility-spec)))
1135 (setq success t))
1136 (or success
1137 (delete-frame frame)))))
1138 ;; Set up the background-mode frame parameter
1139 ;; so that programs can decide good ways of highlighting
1140 ;; on this frame.
1141 (let ((bg-resource (x-get-resource ".backgroundMode"
1142 "BackgroundMode"))
1143 (params (frame-parameters frame))
1144 (bg-mode))
1145 (setq bg-mode
1146 (cond (bg-resource (intern (downcase bg-resource)))
1147 ((< (apply '+ (x-color-values
1148 (cdr (assq 'background-color params))
1149 frame))
1150 ;; Just looking at the screen,
1151 ;; colors whose values add up to .6 of the white total
1152 ;; still look dark to me.
1153 (* (apply '+ (x-color-values "white" frame)) .6))
1154 'dark)
1155 (t 'light)))
1156 (modify-frame-parameters frame
1157 (list (cons 'background-mode bg-mode)
1158 (cons 'display-type
1159 (cond ((x-display-color-p frame)
1160 'color)
1161 ((x-display-grayscale-p frame)
1162 'grayscale)
1163 (t 'mono))))))
1164 frame))
1165
1166 ;; Update a frame's faces when we change its default font.
1167 (defun frame-update-faces (frame)
1168 (let* ((faces global-face-data)
1169 (rest faces))
1170 (while rest
1171 (let* ((face (car (car rest)))
1172 (font (face-font face t)))
1173 (if (listp font)
1174 (let ((bold (memq 'bold font))
1175 (italic (memq 'italic font)))
1176 ;; Ignore any previous (string-valued) font, it might not even
1177 ;; be the right size anymore.
1178 (set-face-font face nil frame)
1179 (cond ((and bold italic)
1180 (make-face-bold-italic face frame t))
1181 (bold
1182 (make-face-bold face frame t))
1183 (italic
1184 (make-face-italic face frame t)))))
1185 (setq rest (cdr rest)))
1186 frame)))
1187
1188 ;; Update the colors of FACE, after FRAME's own colors have been changed.
1189 ;; This applies only to faces with global color specifications
1190 ;; that are not simple constants.
1191 (defun frame-update-face-colors (frame)
1192 (let ((faces global-face-data))
1193 (while faces
1194 (condition-case nil
1195 (let* ((data (cdr (car faces)))
1196 (face (car (car faces)))
1197 (foreground (face-foreground data))
1198 (background (face-background data)))
1199 ;; If the global spec is a specific color,
1200 ;; which doesn't depend on the frame's attributes,
1201 ;; we don't need to recalculate it now.
1202 (or (listp foreground)
1203 (setq foreground nil))
1204 (or (listp background)
1205 (setq background nil))
1206 ;; If we are going to frob this face at all,
1207 ;; reinitialize it first.
1208 (if (or foreground background)
1209 (progn (set-face-foreground face nil frame)
1210 (set-face-background face nil frame)))
1211 (if foreground
1212 (face-try-color-list 'set-face-foreground
1213 face foreground frame))
1214 (if background
1215 (face-try-color-list 'set-face-background
1216 face background frame)))
1217 (error nil))
1218 (setq faces (cdr faces)))))
1219
1220 ;; Fill in the face FACE from frame-independent face data DATA.
1221 ;; DATA should be the non-frame-specific ("global") face vector
1222 ;; for the face. FACE should be a face name or face object.
1223 ;; FRAME is the frame to act on; it must be an actual frame, not nil or t.
1224 (defun face-fill-in (face data frame)
1225 (condition-case nil
1226 (let ((foreground (face-foreground data))
1227 (background (face-background data))
1228 (font (face-font data))
1229 (stipple (face-stipple data)))
1230 (set-face-underline-p face (face-underline-p data) frame)
1231 (if foreground
1232 (face-try-color-list 'set-face-foreground
1233 face foreground frame))
1234 (if background
1235 (face-try-color-list 'set-face-background
1236 face background frame))
1237 (if (listp font)
1238 (let ((bold (memq 'bold font))
1239 (italic (memq 'italic font)))
1240 (cond ((and bold italic)
1241 (make-face-bold-italic face frame))
1242 (bold
1243 (make-face-bold face frame))
1244 (italic
1245 (make-face-italic face frame))))
1246 (if font
1247 (set-face-font face font frame)))
1248 (if stipple
1249 (set-face-stipple face stipple frame)))
1250 (error nil)))
1251
1252 ;; Assuming COLOR is a valid color name,
1253 ;; return t if it can be displayed on FRAME.
1254 (defun face-color-supported-p (frame color background-p)
1255 (and window-system
1256 (or (x-display-color-p frame)
1257 ;; A black-and-white display can implement these.
1258 (member color '("black" "white"))
1259 ;; A black-and-white display can fake gray for background.
1260 (and background-p
1261 (face-color-gray-p color frame))
1262 ;; A grayscale display can implement colors that are gray (more or less).
1263 (and (x-display-grayscale-p frame)
1264 (face-color-gray-p color frame)))))
1265
1266 ;; Use FUNCTION to store a color in FACE on FRAME.
1267 ;; COLORS is either a single color or a list of colors.
1268 ;; If it is a list, try the colors one by one until one of them
1269 ;; succeeds. We signal an error only if all the colors failed.
1270 ;; t as COLORS or as an element of COLORS means to invert the face.
1271 ;; That can't fail, so any subsequent elements after the t are ignored.
1272 (defun face-try-color-list (function face colors frame)
1273 (if (stringp colors)
1274 (if (face-color-supported-p frame colors
1275 (eq function 'set-face-background))
1276 (funcall function face colors frame))
1277 (if (eq colors t)
1278 (invert-face face frame)
1279 (let (done)
1280 (while (and colors (not done))
1281 (if (or (memq (car colors) '(t underline))
1282 (face-color-supported-p frame (car colors)
1283 (eq function 'set-face-background)))
1284 (if (cdr colors)
1285 ;; If there are more colors to try, catch errors
1286 ;; and set `done' if we succeed.
1287 (condition-case nil
1288 (progn
1289 (cond ((eq (car colors) t)
1290 (invert-face face frame))
1291 ((eq (car colors) 'underline)
1292 (set-face-underline-p face t frame))
1293 (t
1294 (funcall function face (car colors) frame)))
1295 (setq done t))
1296 (error nil))
1297 ;; If this is the last color, let the error get out if it fails.
1298 ;; If it succeeds, we will exit anyway after this iteration.
1299 (cond ((eq (car colors) t)
1300 (invert-face face frame))
1301 ((eq (car colors) 'underline)
1302 (set-face-underline-p face t frame))
1303 (t
1304 (funcall function face (car colors) frame)))))
1305 (setq colors (cdr colors)))))))
1306
1307 ;; If we are already using x-window frames, initialize faces for them.
1308 (if (memq (framep (selected-frame)) '(x w32))
1309 (face-initialize))
1310
1311 (provide 'faces)
1312
1313 ;;; faces.el ends here