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