]> code.delx.au - gnu-emacs/blob - lisp/faces.el
d5cb0e5a6ebefa2f81d7b6b9d9f67c60e01e3ad3
[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"))
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 (prin1-to-string (face-stipple (intern face)))))
241 (new-stipple-string
242 (modify-face-read-string
243 face old-stipple-string
244 "stipple" stipples))
245 ;; Convert the stipple value text we read
246 ;; back to a list if it looks like one.
247 ;; This makes the assumption that a pixmap file name
248 ;; won't start with an open-paren.
249 (stipple
250 (if (string-match "^(" new-stipple-string)
251 (read new-stipple-string)
252 new-stipple-string))
253 (bold-p (y-or-n-p (concat "Set face " face " bold ")))
254 (italic-p (y-or-n-p (concat "Set face " face " italic ")))
255 (underline-p (y-or-n-p (concat "Set face " face " underline ")))
256 (all-frames-p (y-or-n-p (concat "Modify face " face " in all frames "))))
257 (message "Face %s: %s" face
258 (mapconcat 'identity
259 (delq nil
260 (list (and foreground (concat (downcase foreground) " foreground"))
261 (and background (concat (downcase background) " background"))
262 (and stipple (concat (downcase new-stipple-string) " stipple"))
263 (and bold-p "bold") (and italic-p "italic")
264 (and underline-p "underline"))) ", "))
265 (list (intern face) foreground background stipple
266 bold-p italic-p underline-p
267 (if all-frames-p nil (selected-frame)))))
268 (condition-case nil
269 (face-try-color-list 'set-face-foreground face foreground frame)
270 (error nil))
271 (condition-case nil
272 (face-try-color-list 'set-face-background face background frame)
273 (error nil))
274 (condition-case nil
275 (set-face-stipple face stipple frame)
276 (error nil))
277 (cond ((eq bold-p nil) (make-face-unbold face frame t))
278 ((eq bold-p t) (make-face-bold face frame t)))
279 (cond ((eq italic-p nil) (make-face-unitalic face frame t))
280 ((eq italic-p t) (make-face-italic face frame t)))
281 (if (memq underline-p '(nil t))
282 (set-face-underline-p face underline-p frame))
283 (and (interactive-p) (redraw-display)))
284 \f
285 ;;;; Associating face names (symbols) with their face vectors.
286
287 (defvar global-face-data nil
288 "Internal data for face support functions. Not for external use.
289 This is an alist associating face names with the default values for
290 their parameters. Newly created frames get their data from here.")
291
292 (defun face-list ()
293 "Returns a list of all defined face names."
294 (mapcar 'car global-face-data))
295
296 (defun internal-find-face (name &optional frame)
297 "Retrieve the face named NAME. Return nil if there is no such face.
298 If the optional argument FRAME is given, this gets the face NAME for
299 that frame; otherwise, it uses the selected frame.
300 If FRAME is the symbol t, then the global, non-frame face is returned.
301 If NAME is already a face, it is simply returned."
302 (if (and (eq frame t) (not (symbolp name)))
303 (setq name (face-name name)))
304 (if (symbolp name)
305 (cdr (assq name
306 (if (eq frame t)
307 global-face-data
308 (frame-face-alist (or frame (selected-frame))))))
309 (internal-check-face name)
310 name))
311
312 (defun internal-get-face (name &optional frame)
313 "Retrieve the face named NAME; error if there is none.
314 If the optional argument FRAME is given, this gets the face NAME for
315 that frame; otherwise, it uses the selected frame.
316 If FRAME is the symbol t, then the global, non-frame face is returned.
317 If NAME is already a face, it is simply returned."
318 (or (internal-find-face name frame)
319 (internal-check-face name)))
320
321
322 (defun internal-set-face-1 (face name value index frame)
323 (let ((inhibit-quit t))
324 (if (null frame)
325 (let ((frames (frame-list)))
326 (while frames
327 (internal-set-face-1 (face-name face) name value index (car frames))
328 (setq frames (cdr frames)))
329 (aset (internal-get-face (if (symbolp face) face (face-name face)) t)
330 index value)
331 value)
332 (or (eq frame t)
333 (set-face-attribute-internal (face-id face) name value frame))
334 (aset (internal-get-face face frame) index value))))
335
336
337 (defun read-face-name (prompt)
338 (let (face)
339 (while (= (length face) 0)
340 (setq face (completing-read prompt
341 (mapcar '(lambda (x) (list (symbol-name x)))
342 (face-list))
343 nil t)))
344 (intern face)))
345
346 (defun internal-face-interactive (what &optional bool)
347 (let* ((fn (intern (concat "face-" what)))
348 (prompt (concat "Set " what " of face"))
349 (face (read-face-name (concat prompt ": ")))
350 (default (if (fboundp fn)
351 (or (funcall fn face (selected-frame))
352 (funcall fn 'default (selected-frame)))))
353 (value (if bool
354 (y-or-n-p (concat "Should face " (symbol-name face)
355 " be " bool "? "))
356 (read-string (concat prompt " " (symbol-name face) " to: ")
357 default))))
358 (list face (if (equal value "") nil value))))
359
360
361
362 (defun make-face (name)
363 "Define a new FACE on all frames.
364 You can modify the font, color, etc of this face with the set-face- functions.
365 If the face already exists, it is unmodified."
366 (interactive "SMake face: ")
367 (or (internal-find-face name)
368 (let ((face (make-vector 8 nil)))
369 (aset face 0 'face)
370 (aset face 1 name)
371 (let* ((frames (frame-list))
372 (inhibit-quit t)
373 (id (internal-next-face-id)))
374 (make-face-internal id)
375 (aset face 2 id)
376 (while frames
377 (set-frame-face-alist (car frames)
378 (cons (cons name (copy-sequence face))
379 (frame-face-alist (car frames))))
380 (setq frames (cdr frames)))
381 (setq global-face-data (cons (cons name face) global-face-data)))
382 ;; when making a face after frames already exist
383 (if (or (eq window-system 'x) (eq window-system 'win32))
384 (make-face-x-resource-internal face))
385 ;; add to menu
386 (if (fboundp 'facemenu-add-new-face)
387 (facemenu-add-new-face name))
388 face))
389 name)
390
391 ;; Fill in a face by default based on X resources, for all existing frames.
392 ;; This has to be done when a new face is made.
393 (defun make-face-x-resource-internal (face &optional frame set-anyway)
394 (cond ((null frame)
395 (let ((frames (frame-list)))
396 (while frames
397 (if (or (eq (framep (car frames)) 'x) (eq (framep (car frames)) 'win32))
398 (make-face-x-resource-internal (face-name face)
399 (car frames) set-anyway))
400 (setq frames (cdr frames)))))
401 (t
402 (setq face (internal-get-face (face-name face) frame))
403 ;;
404 ;; These are things like "attributeForeground" instead of simply
405 ;; "foreground" because people tend to do things like "*foreground",
406 ;; which would cause all faces to be fully qualified, making faces
407 ;; inherit attributes in a non-useful way. So we've made them slightly
408 ;; less obvious to specify in order to make them work correctly in
409 ;; more random environments.
410 ;;
411 ;; I think these should be called "face.faceForeground" instead of
412 ;; "face.attributeForeground", but they're the way they are for
413 ;; hysterical reasons.
414 ;;
415 (let* ((name (symbol-name (face-name face)))
416 (fn (or (x-get-resource (concat name ".attributeFont")
417 "Face.AttributeFont")
418 (and set-anyway (face-font face))))
419 (fg (or (x-get-resource (concat name ".attributeForeground")
420 "Face.AttributeForeground")
421 (and set-anyway (face-foreground face))))
422 (bg (or (x-get-resource (concat name ".attributeBackground")
423 "Face.AttributeBackground")
424 (and set-anyway (face-background face))))
425 (bgp (or (x-get-resource (concat name ".attributeStipple")
426 "Face.AttributeStipple")
427 (x-get-resource (concat name ".attributeBackgroundPixmap")
428 "Face.AttributeBackgroundPixmap")
429 (and set-anyway (face-stipple face))))
430 (ulp (let ((resource (x-get-resource
431 (concat name ".attributeUnderline")
432 "Face.AttributeUnderline")))
433 (if resource
434 (member (downcase resource) '("on" "true"))
435 (and set-anyway (face-underline-p face)))))
436 )
437 (if fn
438 (condition-case ()
439 (cond ((string= fn "italic")
440 (make-face-italic face))
441 ((string= fn "bold")
442 (make-face-bold face))
443 ((string= fn "bold-italic")
444 (make-face-bold-italic face))
445 (t
446 (set-face-font face fn frame)))
447 (error
448 (if (member fn '("italic" "bold" "bold-italic"))
449 (message "no %s version found for face `%s'" fn name)
450 (message "font `%s' not found for face `%s'" fn name)))))
451 (if fg
452 (condition-case ()
453 (set-face-foreground face fg frame)
454 (error (message "color `%s' not allocated for face `%s'" fg name))))
455 (if bg
456 (condition-case ()
457 (set-face-background face bg frame)
458 (error (message "color `%s' not allocated for face `%s'" bg name))))
459 (if bgp
460 (condition-case ()
461 (set-face-stipple face bgp frame)
462 (error (message "pixmap `%s' not found for face `%s'" bgp name))))
463 (if (or ulp set-anyway)
464 (set-face-underline-p face ulp frame))
465 )))
466 face)
467
468 (defun copy-face (old-face new-face &optional frame new-frame)
469 "Define a face just like OLD-FACE, with name NEW-FACE.
470 If NEW-FACE already exists as a face, it is modified to be like OLD-FACE.
471 If it doesn't already exist, it is created.
472
473 If the optional argument FRAME is given as a frame,
474 NEW-FACE is changed on FRAME only.
475 If FRAME is t, the frame-independent default specification for OLD-FACE
476 is copied to NEW-FACE.
477 If FRAME is nil, copying is done for the frame-independent defaults
478 and for each existing frame.
479 If the optional fourth argument NEW-FRAME is given,
480 copy the information from face OLD-FACE on frame FRAME
481 to NEW-FACE on frame NEW-FRAME."
482 (or new-frame (setq new-frame frame))
483 (let ((inhibit-quit t))
484 (if (null frame)
485 (let ((frames (frame-list)))
486 (while frames
487 (copy-face old-face new-face (car frames))
488 (setq frames (cdr frames)))
489 (copy-face old-face new-face t))
490 (setq old-face (internal-get-face old-face frame))
491 (setq new-face (or (internal-find-face new-face new-frame)
492 (make-face new-face)))
493 (condition-case nil
494 ;; A face that has a global symbolic font modifier such as `bold'
495 ;; might legitimately get an error here.
496 ;; Use the frame's default font in that case.
497 (set-face-font new-face (face-font old-face frame) new-frame)
498 (error
499 (set-face-font new-face nil new-frame)))
500 (set-face-foreground new-face (face-foreground old-face frame) new-frame)
501 (set-face-background new-face (face-background old-face frame) new-frame)
502 (set-face-stipple new-face
503 (face-stipple old-face frame)
504 new-frame)
505 (set-face-underline-p new-face (face-underline-p old-face frame)
506 new-frame))
507 new-face))
508
509 (defun face-equal (face1 face2 &optional frame)
510 "True if the faces FACE1 and FACE2 display in the same way."
511 (setq face1 (internal-get-face face1 frame)
512 face2 (internal-get-face face2 frame))
513 (and (equal (face-foreground face1 frame) (face-foreground face2 frame))
514 (equal (face-background face1 frame) (face-background face2 frame))
515 (equal (face-font face1 frame) (face-font face2 frame))
516 (eq (face-underline-p face1 frame) (face-underline-p face2 frame))
517 (equal (face-stipple face1 frame)
518 (face-stipple face2 frame))))
519
520 (defun face-differs-from-default-p (face &optional frame)
521 "True if face FACE displays differently from the default face, on FRAME.
522 A face is considered to be ``the same'' as the default face if it is
523 actually specified in the same way (equivalent fonts, etc) or if it is
524 fully unspecified, and thus inherits the attributes of any face it
525 is displayed on top of.
526
527 The optional argument FRAME specifies which frame to test;
528 if FRAME is t, test the default for new frames.
529 If FRAME is nil or omitted, test the selected frame."
530 (let ((default (internal-get-face 'default frame)))
531 (setq face (internal-get-face face frame))
532 (not (and (or (equal (face-foreground default frame)
533 (face-foreground face frame))
534 (null (face-foreground face frame)))
535 (or (equal (face-background default frame)
536 (face-background face frame))
537 (null (face-background face frame)))
538 (or (equal (face-font default frame) (face-font face frame))
539 (null (face-font face frame)))
540 (or (equal (face-stipple default frame)
541 (face-stipple face frame))
542 (null (face-stipple face frame)))
543 (equal (face-underline-p default frame)
544 (face-underline-p face frame))
545 ))))
546
547 (defun face-nontrivial-p (face &optional frame)
548 "True if face FACE has some non-nil attribute.
549 The optional argument FRAME specifies which frame to test;
550 if FRAME is t, test the default for new frames.
551 If FRAME is nil or omitted, test the selected frame."
552 (setq face (internal-get-face face frame))
553 (or (face-foreground face frame)
554 (face-background face frame)
555 (face-font face frame)
556 (face-stipple face frame)
557 (face-underline-p face frame)))
558
559
560 (defun invert-face (face &optional frame)
561 "Swap the foreground and background colors of face FACE.
562 If the face doesn't specify both foreground and background, then
563 set its foreground and background to the default background and foreground."
564 (interactive (list (read-face-name "Invert face: ")))
565 (setq face (internal-get-face face frame))
566 (let ((fg (face-foreground face frame))
567 (bg (face-background face frame)))
568 (if (or fg bg)
569 (progn
570 (set-face-foreground face bg frame)
571 (set-face-background face fg frame))
572 (set-face-foreground face (or (face-background 'default frame)
573 (cdr (assq 'background-color (frame-parameters frame))))
574 frame)
575 (set-face-background face (or (face-foreground 'default frame)
576 (cdr (assq 'foreground-color (frame-parameters frame))))
577 frame)))
578 face)
579
580
581 (defun internal-try-face-font (face font &optional frame)
582 "Like set-face-font, but returns nil on failure instead of an error."
583 (condition-case ()
584 (set-face-font face font frame)
585 (error nil)))
586 \f
587 ;; Manipulating font names.
588
589 (defconst x-font-regexp nil)
590 (defconst x-font-regexp-head nil)
591 (defconst x-font-regexp-weight nil)
592 (defconst x-font-regexp-slant nil)
593
594 (defconst x-font-regexp-weight-subnum 1)
595 (defconst x-font-regexp-slant-subnum 2)
596 (defconst x-font-regexp-swidth-subnum 3)
597 (defconst x-font-regexp-adstyle-subnum 4)
598
599 ;;; Regexps matching font names in "Host Portable Character Representation."
600 ;;;
601 (let ((- "[-?]")
602 (foundry "[^-]+")
603 (family "[^-]+")
604 (weight "\\(bold\\|demibold\\|medium\\)") ; 1
605 ; (weight\? "\\(\\*\\|bold\\|demibold\\|medium\\|\\)") ; 1
606 (weight\? "\\([^-]*\\)") ; 1
607 (slant "\\([ior]\\)") ; 2
608 ; (slant\? "\\([ior?*]?\\)") ; 2
609 (slant\? "\\([^-]?\\)") ; 2
610 ; (swidth "\\(\\*\\|normal\\|semicondensed\\|\\)") ; 3
611 (swidth "\\([^-]*\\)") ; 3
612 ; (adstyle "\\(\\*\\|sans\\|\\)") ; 4
613 (adstyle "\\([^-]*\\)") ; 4
614 (pixelsize "[0-9]+")
615 (pointsize "[0-9][0-9]+")
616 (resx "[0-9][0-9]+")
617 (resy "[0-9][0-9]+")
618 (spacing "[cmp?*]")
619 (avgwidth "[0-9]+")
620 (registry "[^-]+")
621 (encoding "[^-]+")
622 )
623 (setq x-font-regexp
624 (concat "\\`\\*?[-?*]"
625 foundry - family - weight\? - slant\? - swidth - adstyle -
626 pixelsize - pointsize - resx - resy - spacing - avgwidth -
627 registry - encoding "\\*?\\'"
628 ))
629 (setq x-font-regexp-head
630 (concat "\\`[-?*]" foundry - family - weight\? - slant\?
631 "\\([-*?]\\|\\'\\)"))
632 (setq x-font-regexp-slant (concat - slant -))
633 (setq x-font-regexp-weight (concat - weight -))
634 nil)
635
636 (defun x-resolve-font-name (pattern &optional face frame)
637 "Return a font name matching PATTERN.
638 All wildcards in PATTERN become substantiated.
639 If PATTERN is nil, return the name of the frame's base font, which never
640 contains wildcards.
641 Given optional arguments FACE and FRAME, return a font which is
642 also the same size as FACE on FRAME, or fail."
643 (or (symbolp face)
644 (setq face (face-name face)))
645 (and (eq frame t)
646 (setq frame nil))
647 (if pattern
648 ;; Note that x-list-fonts has code to handle a face with nil as its font.
649 (let ((fonts (x-list-fonts pattern face frame)))
650 (or fonts
651 (if face
652 (if (string-match "\\*" pattern)
653 (if (null (face-font face))
654 (error "No matching fonts are the same height as the frame default font")
655 (error "No matching fonts are the same height as face `%s'" face))
656 (if (null (face-font face))
657 (error "Height of font `%s' doesn't match the frame default font"
658 pattern)
659 (error "Height of font `%s' doesn't match face `%s'"
660 pattern face)))
661 (error "No fonts match `%s'" pattern)))
662 (car fonts))
663 (cdr (assq 'font (frame-parameters (selected-frame))))))
664
665 (defun x-frob-font-weight (font which)
666 (let ((case-fold-search t))
667 (cond ((string-match x-font-regexp font)
668 (concat (substring font 0
669 (match-beginning x-font-regexp-weight-subnum))
670 which
671 (substring font (match-end x-font-regexp-weight-subnum)
672 (match-beginning x-font-regexp-adstyle-subnum))
673 ;; Replace the ADD_STYLE_NAME field with *
674 ;; because the info in it may not be the same
675 ;; for related fonts.
676 "*"
677 (substring font (match-end x-font-regexp-adstyle-subnum))))
678 ((string-match x-font-regexp-head font)
679 (concat (substring font 0 (match-beginning 1)) which
680 (substring font (match-end 1))))
681 ((string-match x-font-regexp-weight font)
682 (concat (substring font 0 (match-beginning 1)) which
683 (substring font (match-end 1)))))))
684
685 (defun x-frob-font-slant (font which)
686 (let ((case-fold-search t))
687 (cond ((string-match x-font-regexp font)
688 (concat (substring font 0
689 (match-beginning x-font-regexp-slant-subnum))
690 which
691 (substring font (match-end x-font-regexp-slant-subnum)
692 (match-beginning x-font-regexp-adstyle-subnum))
693 ;; Replace the ADD_STYLE_NAME field with *
694 ;; because the info in it may not be the same
695 ;; for related fonts.
696 "*"
697 (substring font (match-end x-font-regexp-adstyle-subnum))))
698 ((string-match x-font-regexp-head font)
699 (concat (substring font 0 (match-beginning 2)) which
700 (substring font (match-end 2))))
701 ((string-match x-font-regexp-slant font)
702 (concat (substring font 0 (match-beginning 1)) which
703 (substring font (match-end 1)))))))
704
705 (defun x-make-font-bold (font)
706 "Given an X font specification, make a bold version of it.
707 If that can't be done, return nil."
708 (x-frob-font-weight font "bold"))
709
710 (defun x-make-font-demibold (font)
711 "Given an X font specification, make a demibold version of it.
712 If that can't be done, return nil."
713 (x-frob-font-weight font "demibold"))
714
715 (defun x-make-font-unbold (font)
716 "Given an X font specification, make a non-bold version of it.
717 If that can't be done, return nil."
718 (x-frob-font-weight font "medium"))
719
720 (defun x-make-font-italic (font)
721 "Given an X font specification, make an italic version of it.
722 If that can't be done, return nil."
723 (x-frob-font-slant font "i"))
724
725 (defun x-make-font-oblique (font) ; you say tomayto...
726 "Given an X font specification, make an oblique version of it.
727 If that can't be done, return nil."
728 (x-frob-font-slant font "o"))
729
730 (defun x-make-font-unitalic (font)
731 "Given an X font specification, make a non-italic version of it.
732 If that can't be done, return nil."
733 (x-frob-font-slant font "r"))
734 \f
735 ;;; non-X-specific interface
736
737 (defun make-face-bold (face &optional frame noerror)
738 "Make the font of the given face be bold, if possible.
739 If NOERROR is non-nil, return nil on failure."
740 (interactive (list (read-face-name "Make which face bold: ")))
741 (if (and (eq frame t) (listp (face-font face t)))
742 (set-face-font face (if (memq 'italic (face-font face t))
743 '(bold italic) '(bold))
744 t)
745 (let (font)
746 (if (null frame)
747 (let ((frames (frame-list)))
748 ;; Make this face bold in global-face-data.
749 (make-face-bold face t noerror)
750 ;; Make this face bold in each frame.
751 (while frames
752 (make-face-bold face (car frames) noerror)
753 (setq frames (cdr frames))))
754 (setq face (internal-get-face face frame))
755 (setq font (or (face-font face frame)
756 (face-font face t)))
757 (if (listp font)
758 (setq font nil))
759 (setq font (or font
760 (face-font 'default frame)
761 (cdr (assq 'font (frame-parameters frame)))))
762 (or (and font (make-face-bold-internal face frame font))
763 ;; We failed to find a bold version of the font.
764 noerror
765 (error "No bold version of %S" font))))))
766
767 (defun make-face-bold-internal (face frame font)
768 (let (f2)
769 (or (and (setq f2 (x-make-font-bold font))
770 (internal-try-face-font face f2 frame))
771 (and (setq f2 (x-make-font-demibold font))
772 (internal-try-face-font face f2 frame)))))
773
774 (defun make-face-italic (face &optional frame noerror)
775 "Make the font of the given face be italic, if possible.
776 If NOERROR is non-nil, return nil on failure."
777 (interactive (list (read-face-name "Make which face italic: ")))
778 (if (and (eq frame t) (listp (face-font face t)))
779 (set-face-font face (if (memq 'bold (face-font face t))
780 '(bold italic) '(italic))
781 t)
782 (let (font)
783 (if (null frame)
784 (let ((frames (frame-list)))
785 ;; Make this face italic in global-face-data.
786 (make-face-italic face t noerror)
787 ;; Make this face italic in each frame.
788 (while frames
789 (make-face-italic face (car frames) noerror)
790 (setq frames (cdr frames))))
791 (setq face (internal-get-face face frame))
792 (setq font (or (face-font face frame)
793 (face-font face t)))
794 (if (listp font)
795 (setq font nil))
796 (setq font (or font
797 (face-font 'default frame)
798 (cdr (assq 'font (frame-parameters frame)))))
799 (or (and font (make-face-italic-internal face frame font))
800 ;; We failed to find an italic version of the font.
801 noerror
802 (error "No italic version of %S" font))))))
803
804 (defun make-face-italic-internal (face frame font)
805 (let (f2)
806 (or (and (setq f2 (x-make-font-italic font))
807 (internal-try-face-font face f2 frame))
808 (and (setq f2 (x-make-font-oblique font))
809 (internal-try-face-font face f2 frame)))))
810
811 (defun make-face-bold-italic (face &optional frame noerror)
812 "Make the font of the given face be bold and italic, if possible.
813 If NOERROR is non-nil, return nil on failure."
814 (interactive (list (read-face-name "Make which face bold-italic: ")))
815 (if (and (eq frame t) (listp (face-font face t)))
816 (set-face-font face '(bold italic) t)
817 (let (font)
818 (if (null frame)
819 (let ((frames (frame-list)))
820 ;; Make this face bold-italic in global-face-data.
821 (make-face-bold-italic face t noerror)
822 ;; Make this face bold in each frame.
823 (while frames
824 (make-face-bold-italic face (car frames) noerror)
825 (setq frames (cdr frames))))
826 (setq face (internal-get-face face frame))
827 (setq font (or (face-font face frame)
828 (face-font face t)))
829 (if (listp font)
830 (setq font nil))
831 (setq font (or font
832 (face-font 'default frame)
833 (cdr (assq 'font (frame-parameters frame)))))
834 (or (and font (make-face-bold-italic-internal face frame font))
835 ;; We failed to find a bold italic version.
836 noerror
837 (error "No bold italic version of %S" font))))))
838
839 (defun make-face-bold-italic-internal (face frame font)
840 (let (f2 f3)
841 (or (and (setq f2 (x-make-font-italic font))
842 (not (equal font f2))
843 (setq f3 (x-make-font-bold f2))
844 (not (equal f2 f3))
845 (internal-try-face-font face f3 frame))
846 (and (setq f2 (x-make-font-oblique font))
847 (not (equal font f2))
848 (setq f3 (x-make-font-bold f2))
849 (not (equal f2 f3))
850 (internal-try-face-font face f3 frame))
851 (and (setq f2 (x-make-font-italic font))
852 (not (equal font f2))
853 (setq f3 (x-make-font-demibold f2))
854 (not (equal f2 f3))
855 (internal-try-face-font face f3 frame))
856 (and (setq f2 (x-make-font-oblique font))
857 (not (equal font f2))
858 (setq f3 (x-make-font-demibold f2))
859 (not (equal f2 f3))
860 (internal-try-face-font face f3 frame)))))
861
862 (defun make-face-unbold (face &optional frame noerror)
863 "Make the font of the given face be non-bold, if possible.
864 If NOERROR is non-nil, return nil on failure."
865 (interactive (list (read-face-name "Make which face non-bold: ")))
866 (if (and (eq frame t) (listp (face-font face t)))
867 (set-face-font face (if (memq 'italic (face-font face t))
868 '(italic) nil)
869 t)
870 (let (font font1)
871 (if (null frame)
872 (let ((frames (frame-list)))
873 ;; Make this face unbold in global-face-data.
874 (make-face-unbold face t noerror)
875 ;; Make this face unbold in each frame.
876 (while frames
877 (make-face-unbold face (car frames) noerror)
878 (setq frames (cdr frames))))
879 (setq face (internal-get-face face frame))
880 (setq font1 (or (face-font face frame)
881 (face-font face t)))
882 (if (listp font1)
883 (setq font1 nil))
884 (setq font1 (or font1
885 (face-font 'default frame)
886 (cdr (assq 'font (frame-parameters frame)))))
887 (setq font (and font1 (x-make-font-unbold font1)))
888 (or (if font (internal-try-face-font face font frame))
889 noerror
890 (error "No unbold version of %S" font1))))))
891
892 (defun make-face-unitalic (face &optional frame noerror)
893 "Make the font of the given face be non-italic, if possible.
894 If NOERROR is non-nil, return nil on failure."
895 (interactive (list (read-face-name "Make which face non-italic: ")))
896 (if (and (eq frame t) (listp (face-font face t)))
897 (set-face-font face (if (memq 'bold (face-font face t))
898 '(bold) nil)
899 t)
900 (let (font font1)
901 (if (null frame)
902 (let ((frames (frame-list)))
903 ;; Make this face unitalic in global-face-data.
904 (make-face-unitalic face t noerror)
905 ;; Make this face unitalic in each frame.
906 (while frames
907 (make-face-unitalic face (car frames) noerror)
908 (setq frames (cdr frames))))
909 (setq face (internal-get-face face frame))
910 (setq font1 (or (face-font face frame)
911 (face-font face t)))
912 (if (listp font1)
913 (setq font1 nil))
914 (setq font1 (or font1
915 (face-font 'default frame)
916 (cdr (assq 'font (frame-parameters frame)))))
917 (setq font (and font1 (x-make-font-unitalic font1)))
918 (or (if font (internal-try-face-font face font frame))
919 noerror
920 (error "No unitalic version of %S" font1))))))
921 \f
922 (defvar list-faces-sample-text
923 "abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ"
924 "*Text string to display as the sample text for `list-faces-display'.")
925
926 ;; The name list-faces would be more consistent, but let's avoid a conflict
927 ;; with Lucid, which uses that name differently.
928 (defun list-faces-display ()
929 "List all faces, using the same sample text in each.
930 The sample text is a string that comes from the variable
931 `list-faces-sample-text'.
932
933 It is possible to give a particular face name different appearances in
934 different frames. This command shows the appearance in the
935 selected frame."
936 (interactive)
937 (let ((faces (sort (face-list) (function string-lessp)))
938 (face nil)
939 (frame (selected-frame))
940 disp-frame window)
941 (with-output-to-temp-buffer "*Faces*"
942 (save-excursion
943 (set-buffer standard-output)
944 (setq truncate-lines t)
945 (while faces
946 (setq face (car faces))
947 (setq faces (cdr faces))
948 (insert (format "%25s " (symbol-name face)))
949 (let ((beg (point)))
950 (insert list-faces-sample-text)
951 (insert "\n")
952 (put-text-property beg (1- (point)) 'face face)
953 ;; If the sample text has multiple lines, line up all of them.
954 (goto-char beg)
955 (forward-line 1)
956 (while (not (eobp))
957 (insert " ")
958 (forward-line 1))))
959 (goto-char (point-min))))
960 ;; If the *Faces* buffer appears in a different frame,
961 ;; copy all the face definitions from FRAME,
962 ;; so that the display will reflect the frame that was selected.
963 (setq window (get-buffer-window (get-buffer "*Faces*") t))
964 (setq disp-frame (if window (window-frame window)
965 (car (frame-list))))
966 (or (eq frame disp-frame)
967 (let ((faces (face-list)))
968 (while faces
969 (copy-face (car faces) (car faces) frame disp-frame)
970 (setq faces (cdr faces)))))))
971
972 (defun describe-face (face)
973 "Display the properties of face FACE."
974 (interactive (list (read-face-name "Describe face: ")))
975 (with-output-to-temp-buffer "*Help*"
976 (princ "Properties of face `")
977 (princ (face-name face))
978 (princ "':") (terpri)
979 (princ "Foreground: ") (princ (face-foreground face)) (terpri)
980 (princ "Background: ") (princ (face-background face)) (terpri)
981 (princ " Font: ") (princ (face-font face)) (terpri)
982 (princ "Underlined: ") (princ (if (face-underline-p face) "yes" "no")) (terpri)
983 (princ " Stipple: ") (princ (or (face-stipple face) "none"))))
984 \f
985 ;;; Make the standard faces.
986 ;;; The C code knows the default and modeline faces as faces 0 and 1,
987 ;;; so they must be the first two faces made.
988 (defun face-initialize ()
989 (make-face 'default)
990 (make-face 'modeline)
991 (make-face 'highlight)
992
993 ;; These aren't really special in any way, but they're nice to have around.
994
995 (make-face 'bold)
996 (make-face 'italic)
997 (make-face 'bold-italic)
998 (make-face 'region)
999 (make-face 'secondary-selection)
1000 (make-face 'underline)
1001
1002 (setq region-face (face-id 'region))
1003
1004 ;; Specify the global properties of these faces
1005 ;; so they will come out right on new frames.
1006
1007 (make-face-bold 'bold t)
1008 (make-face-italic 'italic t)
1009 (make-face-bold-italic 'bold-italic t)
1010
1011 (set-face-background 'highlight '("darkseagreen2" "green" t) t)
1012 (set-face-background 'region '("gray" underline) t)
1013 (set-face-background 'secondary-selection '("paleturquoise" "green" t) t)
1014 (set-face-background 'modeline '(t) t)
1015 (set-face-underline-p 'underline t t)
1016
1017 ;; Set up the faces of all existing X Window frames
1018 ;; from those global properties, unless already set in a given frame.
1019
1020 (let ((frames (frame-list)))
1021 (while frames
1022 (if (not (memq (framep (car frames)) '(t nil)))
1023 (let ((frame (car frames))
1024 (rest global-face-data))
1025 (while rest
1026 (let ((face (car (car rest))))
1027 (or (face-differs-from-default-p face)
1028 (face-fill-in face (cdr (car rest)) frame)))
1029 (setq rest (cdr rest)))))
1030 (setq frames (cdr frames)))))
1031
1032 \f
1033 ;; Like x-create-frame but also set up the faces.
1034
1035 (defun x-create-frame-with-faces (&optional parameters)
1036 ;; Read this frame's geometry resource, if it has an explicit name,
1037 ;; and put the specs into PARAMETERS.
1038 (let* ((name (or (cdr (assq 'name parameters))
1039 (cdr (assq 'name default-frame-alist))))
1040 (x-resource-name name)
1041 (res-geometry (if name (x-get-resource "geometry" "Geometry")))
1042 parsed)
1043 (if res-geometry
1044 (progn
1045 (setq parsed (x-parse-geometry res-geometry))
1046 ;; If the resource specifies a position,
1047 ;; call the position and size "user-specified".
1048 (if (or (assq 'top parsed) (assq 'left parsed))
1049 (setq parsed (cons '(user-position . t)
1050 (cons '(user-size . t) parsed))))
1051 ;; Put the geometry parameters at the end.
1052 ;; Copy default-frame-alist so that they go after it.
1053 (setq parameters (append parameters
1054 default-frame-alist
1055 parsed)))))
1056 (let (frame)
1057 (if (null global-face-data)
1058 (setq frame (x-create-frame parameters))
1059 (let* ((visibility-spec (assq 'visibility parameters))
1060 (faces (copy-alist global-face-data))
1061 success
1062 (rest faces))
1063 (setq frame (x-create-frame (cons '(visibility . nil) parameters)))
1064 (unwind-protect
1065 (progn
1066 (set-frame-face-alist frame faces)
1067
1068 (if (cdr (or (assq 'reverse parameters)
1069 (assq 'reverse default-frame-alist)
1070 (let ((resource (x-get-resource "reverseVideo"
1071 "ReverseVideo")))
1072 (if resource
1073 (cons nil (member (downcase resource)
1074 '("on" "true")))))))
1075 (let* ((params (frame-parameters frame))
1076 (bg (cdr (assq 'foreground-color params)))
1077 (fg (cdr (assq 'background-color params))))
1078 (modify-frame-parameters frame
1079 (list (cons 'foreground-color fg)
1080 (cons 'background-color bg)))
1081 (if (equal bg (cdr (assq 'border-color params)))
1082 (modify-frame-parameters frame
1083 (list (cons 'border-color fg))))
1084 (if (equal bg (cdr (assq 'mouse-color params)))
1085 (modify-frame-parameters frame
1086 (list (cons 'mouse-color fg))))
1087 (if (equal bg (cdr (assq 'cursor-color params)))
1088 (modify-frame-parameters frame
1089 (list (cons 'cursor-color fg))))))
1090 ;; Copy the vectors that represent the faces.
1091 ;; Also fill them in from X resources.
1092 (while rest
1093 (let ((global (cdr (car rest))))
1094 (setcdr (car rest) (vector 'face
1095 (face-name (cdr (car rest)))
1096 (face-id (cdr (car rest)))
1097 nil nil nil nil nil))
1098 (face-fill-in (car (car rest)) global frame))
1099 (make-face-x-resource-internal (cdr (car rest)) frame t)
1100 (setq rest (cdr rest)))
1101 (if (null visibility-spec)
1102 (make-frame-visible frame)
1103 (modify-frame-parameters frame (list visibility-spec)))
1104 (setq success t))
1105 (or success
1106 (delete-frame frame)))))
1107 ;; Set up the background-mode frame parameter
1108 ;; so that programs can decide good ways of highlighting
1109 ;; on this frame.
1110 (let ((bg-resource (x-get-resource ".backgroundMode"
1111 "BackgroundMode"))
1112 (params (frame-parameters frame))
1113 (bg-mode))
1114 (setq bg-mode
1115 (cond (bg-resource (intern (downcase bg-resource)))
1116 ((< (apply '+ (x-color-values
1117 (cdr (assq 'background-color params))
1118 frame))
1119 ;; Just looking at the screen,
1120 ;; colors whose values add up to .6 of the white total
1121 ;; still look dark to me.
1122 (* (apply '+ (x-color-values "white" frame)) .6))
1123 'dark)
1124 (t 'light)))
1125 (modify-frame-parameters frame
1126 (list (cons 'background-mode bg-mode)
1127 (cons 'display-type
1128 (cond ((x-display-color-p frame)
1129 'color)
1130 ((x-display-grayscale-p frame)
1131 'grayscale)
1132 (t 'mono))))))
1133 frame))
1134
1135 ;; Update a frame's faces when we change its default font.
1136 (defun frame-update-faces (frame)
1137 (let* ((faces global-face-data)
1138 (rest faces))
1139 (while rest
1140 (let* ((face (car (car rest)))
1141 (font (face-font face t)))
1142 (if (listp font)
1143 (let ((bold (memq 'bold font))
1144 (italic (memq 'italic font)))
1145 ;; Ignore any previous (string-valued) font, it might not even
1146 ;; be the right size anymore.
1147 (set-face-font face nil frame)
1148 (cond ((and bold italic)
1149 (make-face-bold-italic face frame t))
1150 (bold
1151 (make-face-bold face frame t))
1152 (italic
1153 (make-face-italic face frame t)))))
1154 (setq rest (cdr rest)))
1155 frame)))
1156
1157 ;; Update the colors of FACE, after FRAME's own colors have been changed.
1158 ;; This applies only to faces with global color specifications
1159 ;; that are not simple constants.
1160 (defun frame-update-face-colors (frame)
1161 (let ((faces global-face-data))
1162 (while faces
1163 (condition-case nil
1164 (let* ((data (cdr (car faces)))
1165 (face (car (car faces)))
1166 (foreground (face-foreground data))
1167 (background (face-background data)))
1168 ;; If the global spec is a specific color,
1169 ;; which doesn't depend on the frame's attributes,
1170 ;; we don't need to recalculate it now.
1171 (or (listp foreground)
1172 (setq foreground nil))
1173 (or (listp background)
1174 (setq background nil))
1175 ;; If we are going to frob this face at all,
1176 ;; reinitialize it first.
1177 (if (or foreground background)
1178 (progn (set-face-foreground face nil frame)
1179 (set-face-background face nil frame)))
1180 (if foreground
1181 (face-try-color-list 'set-face-foreground
1182 face foreground frame))
1183 (if background
1184 (face-try-color-list 'set-face-background
1185 face background frame)))
1186 (error nil))
1187 (setq faces (cdr faces)))))
1188
1189 ;; Fill in the face FACE from frame-independent face data DATA.
1190 ;; DATA should be the non-frame-specific ("global") face vector
1191 ;; for the face. FACE should be a face name or face object.
1192 ;; FRAME is the frame to act on; it must be an actual frame, not nil or t.
1193 (defun face-fill-in (face data frame)
1194 (condition-case nil
1195 (let ((foreground (face-foreground data))
1196 (background (face-background data))
1197 (font (face-font data))
1198 (stipple (face-stipple data)))
1199 (set-face-underline-p face (face-underline-p data) frame)
1200 (if foreground
1201 (face-try-color-list 'set-face-foreground
1202 face foreground frame))
1203 (if background
1204 (face-try-color-list 'set-face-background
1205 face background frame))
1206 (if (listp font)
1207 (let ((bold (memq 'bold font))
1208 (italic (memq 'italic font)))
1209 (cond ((and bold italic)
1210 (make-face-bold-italic face frame))
1211 (bold
1212 (make-face-bold face frame))
1213 (italic
1214 (make-face-italic face frame))))
1215 (if font
1216 (set-face-font face font frame)))
1217 (if stipple
1218 (set-face-stipple face stipple frame)))
1219 (error nil)))
1220
1221 ;; Assuming COLOR is a valid color name,
1222 ;; return t if it can be displayed on FRAME.
1223 (defun face-color-supported-p (frame color background-p)
1224 (and window-system
1225 (or (x-display-color-p frame)
1226 ;; A black-and-white display can implement these.
1227 (member color '("black" "white"))
1228 ;; A black-and-white display can fake gray for background.
1229 (and background-p
1230 (face-color-gray-p color frame))
1231 ;; A grayscale display can implement colors that are gray (more or less).
1232 (and (x-display-grayscale-p frame)
1233 (face-color-gray-p color frame)))))
1234
1235 ;; Use FUNCTION to store a color in FACE on FRAME.
1236 ;; COLORS is either a single color or a list of colors.
1237 ;; If it is a list, try the colors one by one until one of them
1238 ;; succeeds. We signal an error only if all the colors failed.
1239 ;; t as COLORS or as an element of COLORS means to invert the face.
1240 ;; That can't fail, so any subsequent elements after the t are ignored.
1241 (defun face-try-color-list (function face colors frame)
1242 (if (stringp colors)
1243 (if (face-color-supported-p frame colors
1244 (eq function 'set-face-background))
1245 (funcall function face colors frame))
1246 (if (eq colors t)
1247 (invert-face face frame)
1248 (let (done)
1249 (while (and colors (not done))
1250 (if (or (memq (car colors) '(t underline))
1251 (face-color-supported-p frame (car colors)
1252 (eq function 'set-face-background)))
1253 (if (cdr colors)
1254 ;; If there are more colors to try, catch errors
1255 ;; and set `done' if we succeed.
1256 (condition-case nil
1257 (progn
1258 (cond ((eq (car colors) t)
1259 (invert-face face frame))
1260 ((eq (car colors) 'underline)
1261 (set-face-underline-p face t frame))
1262 (t
1263 (funcall function face (car colors) frame)))
1264 (setq done t))
1265 (error nil))
1266 ;; If this is the last color, let the error get out if it fails.
1267 ;; If it succeeds, we will exit anyway after this iteration.
1268 (cond ((eq (car colors) t)
1269 (invert-face face frame))
1270 ((eq (car colors) 'underline)
1271 (set-face-underline-p face t frame))
1272 (t
1273 (funcall function face (car colors) frame)))))
1274 (setq colors (cdr colors)))))))
1275
1276 ;; If we are already using x-window frames, initialize faces for them.
1277 (if (or (eq (framep (selected-frame)) 'x) (eq (framep (selected-frame)) 'win32))
1278 (face-initialize))
1279
1280 (provide 'faces)
1281
1282 ;;; faces.el ends here