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