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