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