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