(vc-mode-line): Don't write-protect a
[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 ()
19deb21e
RS
386 (cond ((string= fn "italic")
387 (make-face-italic face))
388 ((string= fn "bold")
389 (make-face-bold face))
390 ((string= fn "bold-italic")
391 (make-face-bold-italic face))
392 (t
393 (set-face-font face fn frame)))
394 (error
395 (if (member fn '("italic" "bold" "bold-italic"))
396 (message "no %s version found for face `%s'" fn name)
397 (message "font `%s' not found for face `%s'" fn name)))))
465fceed
ER
398 (if fg
399 (condition-case ()
400 (set-face-foreground face fg frame)
401 (error (message "color `%s' not allocated for face `%s'" fg name))))
402 (if bg
403 (condition-case ()
404 (set-face-background face bg frame)
405 (error (message "color `%s' not allocated for face `%s'" bg name))))
72418504
RS
406 (if bgp
407 (condition-case ()
408 (set-face-stipple face bgp frame)
409 (error (message "pixmap `%s' not found for face `%s'" bgp name))))
465fceed
ER
410 (if (or ulp set-anyway)
411 (set-face-underline-p face ulp frame))
412 )))
413 face)
414
2f2ddd1e
RS
415(defun copy-face (old-face new-face &optional frame new-frame)
416 "Define a face just like OLD-FACE, with name NEW-FACE.
417If NEW-FACE already exists as a face, it is modified to be like OLD-FACE.
418If it doesn't already exist, it is created.
419
420If the optional argument FRAME is given as a frame,
421NEW-FACE is changed on FRAME only.
422If FRAME is t, the frame-independent default specification for OLD-FACE
423is copied to NEW-FACE.
424If FRAME is nil, copying is done for the frame-independent defaults
425and for each existing frame.
710e7005
RS
426If the optional fourth argument NEW-FRAME is given,
427copy the information from face OLD-FACE on frame FRAME
2f2ddd1e 428to NEW-FACE on frame NEW-FRAME."
710e7005 429 (or new-frame (setq new-frame frame))
da41135a 430 (let ((inhibit-quit t))
465fceed
ER
431 (if (null frame)
432 (let ((frames (frame-list)))
433 (while frames
2f2ddd1e 434 (copy-face old-face new-face (car frames))
465fceed 435 (setq frames (cdr frames)))
2f2ddd1e 436 (copy-face old-face new-face t))
da41135a
KH
437 (setq old-face (internal-get-face old-face frame))
438 (setq new-face (or (internal-find-face new-face new-frame)
439 (make-face new-face)))
52267b56
RS
440 (condition-case nil
441 ;; A face that has a global symbolic font modifier such as `bold'
442 ;; might legitimately get an error here.
443 ;; Use the frame's default font in that case.
444 (set-face-font new-face (face-font old-face frame) new-frame)
445 (error
446 (set-face-font new-face nil new-frame)))
710e7005
RS
447 (set-face-foreground new-face (face-foreground old-face frame) new-frame)
448 (set-face-background new-face (face-background old-face frame) new-frame)
72418504
RS
449 (set-face-stipple new-face
450 (face-stipple old-face frame)
451 new-frame)
465fceed 452 (set-face-underline-p new-face (face-underline-p old-face frame)
710e7005 453 new-frame))
465fceed
ER
454 new-face))
455
456(defun face-equal (face1 face2 &optional frame)
ade516a1 457 "True if the faces FACE1 and FACE2 display in the same way."
465fceed
ER
458 (setq face1 (internal-get-face face1 frame)
459 face2 (internal-get-face face2 frame))
460 (and (equal (face-foreground face1 frame) (face-foreground face2 frame))
461 (equal (face-background face1 frame) (face-background face2 frame))
462 (equal (face-font face1 frame) (face-font face2 frame))
ae0249df 463 (eq (face-underline-p face1 frame) (face-underline-p face2 frame))
72418504
RS
464 (equal (face-stipple face1 frame)
465 (face-stipple face2 frame))))
465fceed
ER
466
467(defun face-differs-from-default-p (face &optional frame)
468 "True if face FACE displays differently from the default face, on FRAME.
469A face is considered to be ``the same'' as the default face if it is
470actually specified in the same way (equivalent fonts, etc) or if it is
471fully unspecified, and thus inherits the attributes of any face it
93d6fcef
RS
472is displayed on top of.
473
474The optional argument FRAME specifies which frame to test;
475if FRAME is t, test the default for new frames.
476If FRAME is nil or omitted, test the selected frame."
465fceed
ER
477 (let ((default (internal-get-face 'default frame)))
478 (setq face (internal-get-face face frame))
479 (not (and (or (equal (face-foreground default frame)
480 (face-foreground face frame))
481 (null (face-foreground face frame)))
482 (or (equal (face-background default frame)
483 (face-background face frame))
484 (null (face-background face frame)))
485 (or (equal (face-font default frame) (face-font face frame))
486 (null (face-font face frame)))
72418504
RS
487 (or (equal (face-stipple default frame)
488 (face-stipple face frame))
489 (null (face-stipple face frame)))
465fceed
ER
490 (equal (face-underline-p default frame)
491 (face-underline-p face frame))
492 ))))
493
93d6fcef
RS
494(defun face-nontrivial-p (face &optional frame)
495 "True if face FACE has some non-nil attribute.
496The optional argument FRAME specifies which frame to test;
497if FRAME is t, test the default for new frames.
498If FRAME is nil or omitted, test the selected frame."
499 (setq face (internal-get-face face frame))
500 (or (face-foreground face frame)
501 (face-background face frame)
502 (face-font face frame)
503 (face-stipple face frame)
504 (face-underline-p face frame)))
505
465fceed
ER
506
507(defun invert-face (face &optional frame)
508 "Swap the foreground and background colors of face FACE.
509If the face doesn't specify both foreground and background, then
8494bbf1 510set its foreground and background to the default background and foreground."
465fceed
ER
511 (interactive (list (read-face-name "Invert face: ")))
512 (setq face (internal-get-face face frame))
513 (let ((fg (face-foreground face frame))
514 (bg (face-background face frame)))
515 (if (or fg bg)
516 (progn
517 (set-face-foreground face bg frame)
518 (set-face-background face fg frame))
8494bbf1
RS
519 (set-face-foreground face (or (face-background 'default frame)
520 (cdr (assq 'background-color (frame-parameters frame))))
521 frame)
522 (set-face-background face (or (face-foreground 'default frame)
523 (cdr (assq 'foreground-color (frame-parameters frame))))
524 frame)))
465fceed
ER
525 face)
526
527
528(defun internal-try-face-font (face font &optional frame)
529 "Like set-face-font, but returns nil on failure instead of an error."
530 (condition-case ()
531 (set-face-font face font frame)
532 (error nil)))
465fceed
ER
533\f
534;; Manipulating font names.
535
536(defconst x-font-regexp nil)
537(defconst x-font-regexp-head nil)
538(defconst x-font-regexp-weight nil)
539(defconst x-font-regexp-slant nil)
540
541;;; Regexps matching font names in "Host Portable Character Representation."
542;;;
543(let ((- "[-?]")
544 (foundry "[^-]+")
545 (family "[^-]+")
546 (weight "\\(bold\\|demibold\\|medium\\)") ; 1
547; (weight\? "\\(\\*\\|bold\\|demibold\\|medium\\|\\)") ; 1
548 (weight\? "\\([^-]*\\)") ; 1
549 (slant "\\([ior]\\)") ; 2
550; (slant\? "\\([ior?*]?\\)") ; 2
551 (slant\? "\\([^-]?\\)") ; 2
552; (swidth "\\(\\*\\|normal\\|semicondensed\\|\\)") ; 3
553 (swidth "\\([^-]*\\)") ; 3
554; (adstyle "\\(\\*\\|sans\\|\\)") ; 4
555 (adstyle "[^-]*") ; 4
556 (pixelsize "[0-9]+")
557 (pointsize "[0-9][0-9]+")
558 (resx "[0-9][0-9]+")
559 (resy "[0-9][0-9]+")
560 (spacing "[cmp?*]")
561 (avgwidth "[0-9]+")
562 (registry "[^-]+")
563 (encoding "[^-]+")
564 )
565 (setq x-font-regexp
566 (concat "\\`\\*?[-?*]"
567 foundry - family - weight\? - slant\? - swidth - adstyle -
e20d641f
RS
568 pixelsize - pointsize - resx - resy - spacing - avgwidth -
569 registry - encoding "\\*?\\'"
465fceed
ER
570 ))
571 (setq x-font-regexp-head
572 (concat "\\`[-?*]" foundry - family - weight\? - slant\?
573 "\\([-*?]\\|\\'\\)"))
574 (setq x-font-regexp-slant (concat - slant -))
575 (setq x-font-regexp-weight (concat - weight -))
576 nil)
577
281dc1c2
JB
578(defun x-resolve-font-name (pattern &optional face frame)
579 "Return a font name matching PATTERN.
580All wildcards in PATTERN become substantiated.
10d89673
JB
581If PATTERN is nil, return the name of the frame's base font, which never
582contains wildcards.
e17dbede
RS
583Given optional arguments FACE and FRAME, return a font which is
584also the same size as FACE on FRAME, or fail."
14e6867c
RS
585 (or (symbolp face)
586 (setq face (face-name face)))
587 (and (eq frame t)
588 (setq frame nil))
10d89673 589 (if pattern
8db93e45
RS
590 ;; Note that x-list-fonts has code to handle a face with nil as its font.
591 (let ((fonts (x-list-fonts pattern face frame)))
10d89673
JB
592 (or fonts
593 (if face
962a60aa
RS
594 (if (string-match "\\*" pattern)
595 (if (null (face-font face))
596 (error "No matching fonts are the same height as the frame default font")
597 (error "No matching fonts are the same height as face `%s'" face))
598 (if (null (face-font face))
599 (error "Height of font `%s' doesn't match the frame default font"
600 pattern)
601 (error "Height of font `%s' doesn't match face `%s'"
602 pattern face)))
7cf6fac1 603 (error "No fonts match `%s'" pattern)))
10d89673
JB
604 (car fonts))
605 (cdr (assq 'font (frame-parameters (selected-frame))))))
281dc1c2 606
465fceed
ER
607(defun x-frob-font-weight (font which)
608 (if (or (string-match x-font-regexp font)
609 (string-match x-font-regexp-head font)
610 (string-match x-font-regexp-weight font))
611 (concat (substring font 0 (match-beginning 1)) which
612 (substring font (match-end 1)))
613 nil))
614
615(defun x-frob-font-slant (font which)
616 (cond ((or (string-match x-font-regexp font)
617 (string-match x-font-regexp-head font))
618 (concat (substring font 0 (match-beginning 2)) which
619 (substring font (match-end 2))))
620 ((string-match x-font-regexp-slant font)
621 (concat (substring font 0 (match-beginning 1)) which
622 (substring font (match-end 1))))
623 (t nil)))
624
625
626(defun x-make-font-bold (font)
f3f31ccf
RS
627 "Given an X font specification, make a bold version of it.
628If that can't be done, return nil."
465fceed
ER
629 (x-frob-font-weight font "bold"))
630
631(defun x-make-font-demibold (font)
f3f31ccf
RS
632 "Given an X font specification, make a demibold version of it.
633If that can't be done, return nil."
465fceed
ER
634 (x-frob-font-weight font "demibold"))
635
636(defun x-make-font-unbold (font)
f3f31ccf
RS
637 "Given an X font specification, make a non-bold version of it.
638If that can't be done, return nil."
465fceed
ER
639 (x-frob-font-weight font "medium"))
640
641(defun x-make-font-italic (font)
f3f31ccf
RS
642 "Given an X font specification, make an italic version of it.
643If that can't be done, return nil."
465fceed
ER
644 (x-frob-font-slant font "i"))
645
646(defun x-make-font-oblique (font) ; you say tomayto...
f3f31ccf
RS
647 "Given an X font specification, make an oblique version of it.
648If that can't be done, return nil."
465fceed
ER
649 (x-frob-font-slant font "o"))
650
651(defun x-make-font-unitalic (font)
f3f31ccf
RS
652 "Given an X font specification, make a non-italic version of it.
653If that can't be done, return nil."
465fceed 654 (x-frob-font-slant font "r"))
465fceed
ER
655\f
656;;; non-X-specific interface
657
bb9a81fc 658(defun make-face-bold (face &optional frame noerror)
465fceed 659 "Make the font of the given face be bold, if possible.
bb9a81fc 660If NOERROR is non-nil, return nil on failure."
465fceed 661 (interactive (list (read-face-name "Make which face bold: ")))
534dbc97 662 (if (and (eq frame t) (listp (face-font face t)))
f3f31ccf
RS
663 (set-face-font face (if (memq 'italic (face-font face t))
664 '(bold italic) '(bold))
665 t)
666 (let ((ofont (face-font face frame))
4b3203d9 667 font)
f3f31ccf
RS
668 (if (null frame)
669 (let ((frames (frame-list)))
670 ;; Make this face bold in global-face-data.
671 (make-face-bold face t noerror)
672 ;; Make this face bold in each frame.
673 (while frames
674 (make-face-bold face (car frames) noerror)
675 (setq frames (cdr frames))))
676 (setq face (internal-get-face face frame))
677 (setq font (or (face-font face frame)
678 (face-font face t)))
679 (if (listp font)
680 (setq font nil))
681 (setq font (or font
682 (face-font 'default frame)
683 (cdr (assq 'font (frame-parameters frame)))))
96616c04 684 (and font (make-face-bold-internal face frame font)))
f3f31ccf
RS
685 (or (not (equal ofont (face-font face)))
686 (and (not noerror)
687 (error "No bold version of %S" font))))))
688
4b3203d9
RS
689(defun make-face-bold-internal (face frame font)
690 (let (f2)
691 (or (and (setq f2 (x-make-font-bold font))
692 (internal-try-face-font face f2 frame))
693 (and (setq f2 (x-make-font-demibold font))
694 (internal-try-face-font face f2 frame)))))
bb9a81fc
JB
695
696(defun make-face-italic (face &optional frame noerror)
465fceed 697 "Make the font of the given face be italic, if possible.
bb9a81fc 698If NOERROR is non-nil, return nil on failure."
465fceed 699 (interactive (list (read-face-name "Make which face italic: ")))
534dbc97 700 (if (and (eq frame t) (listp (face-font face t)))
f3f31ccf
RS
701 (set-face-font face (if (memq 'bold (face-font face t))
702 '(bold italic) '(italic))
703 t)
704 (let ((ofont (face-font face frame))
4b3203d9 705 font)
f3f31ccf
RS
706 (if (null frame)
707 (let ((frames (frame-list)))
708 ;; Make this face italic in global-face-data.
709 (make-face-italic face t noerror)
710 ;; Make this face italic in each frame.
711 (while frames
712 (make-face-italic face (car frames) noerror)
713 (setq frames (cdr frames))))
714 (setq face (internal-get-face face frame))
715 (setq font (or (face-font face frame)
716 (face-font face t)))
717 (if (listp font)
718 (setq font nil))
719 (setq font (or font
720 (face-font 'default frame)
721 (cdr (assq 'font (frame-parameters frame)))))
96616c04 722 (and font (make-face-italic-internal face frame font)))
f3f31ccf
RS
723 (or (not (equal ofont (face-font face)))
724 (and (not noerror)
725 (error "No italic version of %S" font))))))
726
4b3203d9
RS
727(defun make-face-italic-internal (face frame font)
728 (let (f2)
729 (or (and (setq f2 (x-make-font-italic font))
730 (internal-try-face-font face f2 frame))
731 (and (setq f2 (x-make-font-oblique font))
732 (internal-try-face-font face f2 frame)))))
bb9a81fc
JB
733
734(defun make-face-bold-italic (face &optional frame noerror)
465fceed 735 "Make the font of the given face be bold and italic, if possible.
bb9a81fc 736If NOERROR is non-nil, return nil on failure."
465fceed 737 (interactive (list (read-face-name "Make which face bold-italic: ")))
534dbc97 738 (if (and (eq frame t) (listp (face-font face t)))
f3f31ccf
RS
739 (set-face-font face '(bold italic) t)
740 (let ((ofont (face-font face frame))
741 font)
742 (if (null frame)
743 (let ((frames (frame-list)))
744 ;; Make this face bold-italic in global-face-data.
745 (make-face-bold-italic face t noerror)
746 ;; Make this face bold in each frame.
747 (while frames
748 (make-face-bold-italic face (car frames) noerror)
749 (setq frames (cdr frames))))
750 (setq face (internal-get-face face frame))
751 (setq font (or (face-font face frame)
752 (face-font face t)))
753 (if (listp font)
754 (setq font nil))
755 (setq font (or font
756 (face-font 'default frame)
757 (cdr (assq 'font (frame-parameters frame)))))
96616c04 758 (and font (make-face-bold-italic-internal face frame font)))
f3f31ccf
RS
759 (or (not (equal ofont (face-font face)))
760 (and (not noerror)
761 (error "No bold italic version of %S" font))))))
762
4b3203d9 763(defun make-face-bold-italic-internal (face frame font)
f3f31ccf
RS
764 (let (f2 f3)
765 (or (and (setq f2 (x-make-font-italic font))
766 (not (equal font f2))
767 (setq f3 (x-make-font-bold 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-bold f2))
773 (not (equal f2 f3))
774 (internal-try-face-font face f3 frame))
775 (and (setq f2 (x-make-font-italic font))
776 (not (equal font f2))
777 (setq f3 (x-make-font-demibold f2))
778 (not (equal f2 f3))
779 (internal-try-face-font face f3 frame))
780 (and (setq f2 (x-make-font-oblique font))
781 (not (equal font f2))
782 (setq f3 (x-make-font-demibold f2))
783 (not (equal f2 f3))
784 (internal-try-face-font face f3 frame)))))
bb9a81fc
JB
785
786(defun make-face-unbold (face &optional frame noerror)
465fceed 787 "Make the font of the given face be non-bold, if possible.
bb9a81fc 788If NOERROR is non-nil, return nil on failure."
465fceed 789 (interactive (list (read-face-name "Make which face non-bold: ")))
534dbc97 790 (if (and (eq frame t) (listp (face-font face t)))
f3f31ccf
RS
791 (set-face-font face (if (memq 'italic (face-font face t))
792 '(italic) nil)
793 t)
794 (let ((ofont (face-font face frame))
795 font font1)
796 (if (null frame)
797 (let ((frames (frame-list)))
798 ;; Make this face unbold in global-face-data.
799 (make-face-unbold face t noerror)
800 ;; Make this face unbold in each frame.
801 (while frames
802 (make-face-unbold face (car frames) noerror)
803 (setq frames (cdr frames))))
804 (setq face (internal-get-face face frame))
805 (setq font1 (or (face-font face frame)
806 (face-font face t)))
807 (if (listp font1)
808 (setq font1 nil))
809 (setq font1 (or font1
810 (face-font 'default frame)
811 (cdr (assq 'font (frame-parameters frame)))))
96616c04 812 (setq font (and font1 (x-make-font-unbold font1)))
f3f31ccf
RS
813 (if font (internal-try-face-font face font frame)))
814 (or (not (equal ofont (face-font face)))
815 (and (not noerror)
816 (error "No unbold version of %S" font1))))))
bb9a81fc
JB
817
818(defun make-face-unitalic (face &optional frame noerror)
465fceed 819 "Make the font of the given face be non-italic, if possible.
bb9a81fc 820If NOERROR is non-nil, return nil on failure."
465fceed 821 (interactive (list (read-face-name "Make which face non-italic: ")))
534dbc97 822 (if (and (eq frame t) (listp (face-font face t)))
f3f31ccf
RS
823 (set-face-font face (if (memq 'bold (face-font face t))
824 '(bold) nil)
825 t)
826 (let ((ofont (face-font face frame))
827 font font1)
828 (if (null frame)
829 (let ((frames (frame-list)))
830 ;; Make this face unitalic in global-face-data.
831 (make-face-unitalic face t noerror)
832 ;; Make this face unitalic in each frame.
833 (while frames
834 (make-face-unitalic face (car frames) noerror)
835 (setq frames (cdr frames))))
836 (setq face (internal-get-face face frame))
837 (setq font1 (or (face-font face frame)
838 (face-font face t)))
839 (if (listp font1)
840 (setq font1 nil))
841 (setq font1 (or font1
842 (face-font 'default frame)
843 (cdr (assq 'font (frame-parameters frame)))))
96616c04 844 (setq font (and font1 (x-make-font-unitalic font1)))
f3f31ccf
RS
845 (if font (internal-try-face-font face font frame)))
846 (or (not (equal ofont (face-font face)))
847 (and (not noerror)
848 (error "No unitalic version of %S" font1))))))
465fceed 849\f
710e7005
RS
850(defvar list-faces-sample-text
851 "abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ"
852 "*Text string to display as the sample text for `list-faces-display'.")
853
854;; The name list-faces would be more consistent, but let's avoid a conflict
855;; with Lucid, which uses that name differently.
856(defun list-faces-display ()
857 "List all faces, using the same sample text in each.
858The sample text is a string that comes from the variable
859`list-faces-sample-text'.
860
861It is possible to give a particular face name different appearances in
862different frames. This command shows the appearance in the
863selected frame."
864 (interactive)
865 (let ((faces (sort (face-list) (function string-lessp)))
866 (face nil)
867 (frame (selected-frame))
868 disp-frame window)
869 (with-output-to-temp-buffer "*Faces*"
870 (save-excursion
871 (set-buffer standard-output)
872 (setq truncate-lines t)
873 (while faces
874 (setq face (car faces))
875 (setq faces (cdr faces))
876 (insert (format "%25s " (symbol-name face)))
877 (let ((beg (point)))
878 (insert list-faces-sample-text)
879 (insert "\n")
23b04eac
RS
880 (put-text-property beg (1- (point)) 'face face)
881 ;; If the sample text has multiple lines, line up all of them.
882 (goto-char beg)
883 (forward-line 1)
884 (while (not (eobp))
885 (insert " ")
886 (forward-line 1))))
710e7005
RS
887 (goto-char (point-min))))
888 ;; If the *Faces* buffer appears in a different frame,
889 ;; copy all the face definitions from FRAME,
890 ;; so that the display will reflect the frame that was selected.
891 (setq window (get-buffer-window (get-buffer "*Faces*") t))
892 (setq disp-frame (if window (window-frame window)
893 (car (frame-list))))
894 (or (eq frame disp-frame)
895 (let ((faces (face-list)))
896 (while faces
897 (copy-face (car faces) (car faces) frame disp-frame)
898 (setq faces (cdr faces)))))))
19deb21e
RS
899
900(defun describe-face (face)
901 "Display the properties of face FACE."
902 (interactive (list (read-face-name "Describe face: ")))
903 (with-output-to-temp-buffer "*Help*"
904 (princ "Properties of face `")
905 (princ (face-name face))
906 (princ "':") (terpri)
907 (princ "Foreground: ") (princ (face-foreground face)) (terpri)
908 (princ "Background: ") (princ (face-background face)) (terpri)
909 (princ " Font: ") (princ (face-font face)) (terpri)
910 (princ "Underlined: ") (princ (if (face-underline-p face) "yes" "no")) (terpri)
911 (princ " Stipple: ") (princ (or (face-stipple face) "none"))))
710e7005 912\f
19ae9866
RS
913;;; Make the standard faces.
914;;; The C code knows the default and modeline faces as faces 0 and 1,
915;;; so they must be the first two faces made.
e09c52a8 916(defun face-initialize ()
465fceed 917 (make-face 'default)
ebea97d8 918 (make-face 'modeline)
465fceed 919 (make-face 'highlight)
19ae9866 920
465fceed 921 ;; These aren't really special in any way, but they're nice to have around.
19ae9866 922
465fceed
ER
923 (make-face 'bold)
924 (make-face 'italic)
925 (make-face 'bold-italic)
578d09a6 926 (make-face 'region)
e09c52a8 927 (make-face 'secondary-selection)
f4b9e76b 928 (make-face 'underline)
e09c52a8 929
578d09a6 930 (setq region-face (face-id 'region))
8494bbf1 931
19ae9866
RS
932 ;; Specify the global properties of these faces
933 ;; so they will come out right on new frames.
934
935 (make-face-bold 'bold t)
936 (make-face-italic 'italic t)
937 (make-face-bold-italic 'bold-italic t)
938
939 (set-face-background 'highlight '("darkseagreen2" "green" t) t)
566b0ad2 940 (set-face-background 'region '("gray" underline) t)
19ae9866
RS
941 (set-face-background 'secondary-selection '("paleturquoise" "green" t) t)
942 (set-face-background 'modeline '(t) t)
943 (set-face-underline-p 'underline t t)
944
945 ;; Set up the faces of all existing X Window frames
946 ;; from those global properties, unless already set in a given frame.
947
e09c52a8
RS
948 (let ((frames (frame-list)))
949 (while frames
e17dbede 950 (if (not (memq (framep (car frames)) '(t nil)))
19ae9866
RS
951 (let ((frame (car frames))
952 (rest global-face-data))
953 (while rest
954 (let ((face (car (car rest))))
955 (or (face-differs-from-default-p face)
956 (face-fill-in face (cdr (car rest)) frame)))
957 (setq rest (cdr rest)))))
e09c52a8
RS
958 (setq frames (cdr frames)))))
959
465fceed 960\f
465fceed
ER
961;; Like x-create-frame but also set up the faces.
962
963(defun x-create-frame-with-faces (&optional parameters)
6e4aafdc
KH
964 ;; Read this frame's geometry resource, if it has an explicit name,
965 ;; and put the specs into PARAMETERS.
966 (let* ((name (or (cdr (assq 'name parameters))
313b841c 967 (cdr (assq 'name default-frame-alist))))
6e4aafdc 968 (x-resource-name name)
c0553327 969 (res-geometry (if name (x-get-resource "geometry" "Geometry")))
6e4aafdc
KH
970 parsed)
971 (if res-geometry
972 (progn
973 (setq parsed (x-parse-geometry res-geometry))
974 ;; If the resource specifies a position,
975 ;; call the position and size "user-specified".
976 (if (or (assq 'top parsed) (assq 'left parsed))
977 (setq parsed (cons '(user-position . t)
978 (cons '(user-size . t) parsed))))
313b841c
KH
979 ;; Put the geometry parameters at the end.
980 ;; Copy default-frame-alist so that they go after it.
981 (setq parameters (append parameters
982 default-frame-alist
983 parsed)))))
e09c52a8
RS
984 (if (null global-face-data)
985 (x-create-frame parameters)
e69e8fad
RS
986 (let* ((visibility-spec (assq 'visibility parameters))
987 (frame (x-create-frame (cons '(visibility . nil) parameters)))
e09c52a8 988 (faces (copy-alist global-face-data))
e7cc6aa5 989 success
ebea97d8 990 (rest faces))
e7cc6aa5
RS
991 (unwind-protect
992 (progn
993 (set-frame-face-alist frame faces)
994
995 (if (cdr (or (assq 'reverse parameters)
996 (assq 'reverse default-frame-alist)
997 (let ((resource (x-get-resource "reverseVideo"
998 "ReverseVideo")))
999 (if resource
1000 (cons nil (member (downcase resource)
1001 '("on" "true")))))))
66cf1bd7
KH
1002 (let* ((params (frame-parameters frame))
1003 (bg (cdr (assq 'foreground-color params)))
1004 (fg (cdr (assq 'background-color params))))
1005 (modify-frame-parameters frame
1006 (list (cons 'foreground-color fg)
1007 (cons 'background-color bg)))
1008 (if (equal bg (cdr (assq 'border-color params)))
1009 (modify-frame-parameters frame
1010 (list (cons 'border-color fg))))
1011 (if (equal bg (cdr (assq 'mouse-color params)))
1012 (modify-frame-parameters frame
1013 (list (cons 'mouse-color fg))))
1014 (if (equal bg (cdr (assq 'cursor-color params)))
1015 (modify-frame-parameters frame
1016 (list (cons 'cursor-color fg))))))
e7cc6aa5
RS
1017 ;; Copy the vectors that represent the faces.
1018 ;; Also fill them in from X resources.
1019 (while rest
1020 (let ((global (cdr (car rest))))
1021 (setcdr (car rest) (vector 'face
1022 (face-name (cdr (car rest)))
1023 (face-id (cdr (car rest)))
1024 nil nil nil nil nil))
1025 (face-fill-in (car (car rest)) global frame))
1026 (make-face-x-resource-internal (cdr (car rest)) frame t)
1027 (setq rest (cdr rest)))
1028 (if (null visibility-spec)
1029 (make-frame-visible frame)
1030 (modify-frame-parameters frame (list visibility-spec)))
1031 (setq success t)
1032 frame)
1033 (or success
1034 (delete-frame frame))))))
e09c52a8 1035
9d52c8d3
RS
1036;; Update a frame's faces when we change its default font.
1037(defun frame-update-faces (frame)
1038 (let* ((faces global-face-data)
1039 (rest faces))
1040 (while rest
1041 (let* ((face (car (car rest)))
1042 (font (face-font face t)))
1043 (if (listp font)
1044 (let ((bold (memq 'bold font))
1045 (italic (memq 'italic font)))
99fb9482
KH
1046 ;; Ignore any previous (string-valued) font, it might not even
1047 ;; be the right size anymore.
1048 (set-face-font face nil frame)
9d52c8d3
RS
1049 (cond ((and bold italic)
1050 (make-face-bold-italic face frame t))
1051 (bold
1052 (make-face-bold face frame t))
1053 (italic
1054 (make-face-italic face frame t)))))
1055 (setq rest (cdr rest)))
1056 frame)))
1057
f16c38ae
RS
1058;; Update the colors of FACE, after FRAME's own colors have been changed.
1059;; This applies only to faces with global color specifications
1060;; that are not simple constants.
1061(defun frame-update-face-colors (frame)
1062 (let ((faces global-face-data))
1063 (while faces
1064 (condition-case nil
1065 (let* ((data (cdr (car faces)))
1066 (face (car (car faces)))
1067 (foreground (face-foreground data))
1068 (background (face-background data)))
1069 ;; If the global spec is a specific color,
1070 ;; which doesn't depend on the frame's attributes,
1071 ;; we don't need to recalculate it now.
1072 (or (listp foreground)
1073 (setq foreground nil))
1074 (or (listp background)
1075 (setq background nil))
1076 ;; If we are going to frob this face at all,
1077 ;; reinitialize it first.
1078 (if (or foreground background)
1079 (progn (set-face-foreground face nil frame)
1080 (set-face-background face nil frame)))
1081 (if foreground
1082 (face-try-color-list 'set-face-foreground
1083 face foreground frame))
1084 (if background
1085 (face-try-color-list 'set-face-background
1086 face background frame)))
1087 (error nil))
1088 (setq faces (cdr faces)))))
1089
19ae9866
RS
1090;; Fill in the face FACE from frame-independent face data DATA.
1091;; DATA should be the non-frame-specific ("global") face vector
1092;; for the face. FACE should be a face name or face object.
1093;; FRAME is the frame to act on; it must be an actual frame, not nil or t.
1094(defun face-fill-in (face data frame)
1095 (condition-case nil
1096 (let ((foreground (face-foreground data))
1097 (background (face-background data))
ca58b3ec
KH
1098 (font (face-font data))
1099 (stipple (face-stipple data)))
19ae9866
RS
1100 (set-face-underline-p face (face-underline-p data) frame)
1101 (if foreground
1102 (face-try-color-list 'set-face-foreground
1103 face foreground frame))
1104 (if background
1105 (face-try-color-list 'set-face-background
1106 face background frame))
1107 (if (listp font)
1108 (let ((bold (memq 'bold font))
1109 (italic (memq 'italic font)))
1110 (cond ((and bold italic)
1111 (make-face-bold-italic face frame))
1112 (bold
1113 (make-face-bold face frame))
1114 (italic
1115 (make-face-italic face frame))))
1116 (if font
ca58b3ec
KH
1117 (set-face-font face font frame)))
1118 (if stipple
1119 (set-face-stipple face stipple frame)))
19ae9866 1120 (error nil)))
e09c52a8 1121
4099a32d
RS
1122;; Assuming COLOR is a valid color name,
1123;; return t if it can be displayed on FRAME.
1124(defun face-color-supported-p (frame color background-p)
1125 (or (x-display-color-p frame)
1126 ;; A black-and-white display can implement these.
1127 (member color '("black" "white"))
1128 ;; A black-and-white display can fake these for background.
1129 (and background-p
1130 (member color '("gray" "gray1" "gray3")))
1131 ;; A grayscale display can implement colors that are gray (more or less).
1132 (and (x-display-grayscale-p frame)
1133 (let* ((values (x-color-values color frame))
1134 (r (nth 0 values))
1135 (g (nth 1 values))
1136 (b (nth 2 values)))
1137 (and (< (abs (- r g)) (/ (abs (+ r g)) 20))
1138 (< (abs (- g b)) (/ (abs (+ g b)) 20))
1139 (< (abs (- b r)) (/ (abs (+ b r)) 20)))))))
1140
19ae9866
RS
1141;; Use FUNCTION to store a color in FACE on FRAME.
1142;; COLORS is either a single color or a list of colors.
1143;; If it is a list, try the colors one by one until one of them
1144;; succeeds. We signal an error only if all the colors failed.
1145;; t as COLORS or as an element of COLORS means to invert the face.
1146;; That can't fail, so any subsequent elements after the t are ignored.
1147(defun face-try-color-list (function face colors frame)
1148 (if (stringp colors)
4099a32d
RS
1149 (if (face-color-supported-p frame colors
1150 (eq function 'set-face-background))
1151 (funcall function face colors frame))
19ae9866
RS
1152 (if (eq colors t)
1153 (invert-face face frame)
1154 (let (done)
1155 (while (and colors (not done))
de52827f 1156 (if (or (memq (car colors) '(t underline))
4099a32d
RS
1157 (face-color-supported-p frame (car colors)
1158 (eq function 'set-face-background)))
1159 (if (cdr colors)
1160 ;; If there are more colors to try, catch errors
1161 ;; and set `done' if we succeed.
1162 (condition-case nil
1163 (progn
1164 (cond ((eq (car colors) t)
1165 (invert-face face frame))
1166 ((eq (car colors) 'underline)
1167 (set-face-underline-p face t frame))
1168 (t
1169 (funcall function face (car colors) frame)))
1170 (setq done t))
1171 (error nil))
1172 ;; If this is the last color, let the error get out if it fails.
1173 ;; If it succeeds, we will exit anyway after this iteration.
1174 (cond ((eq (car colors) t)
1175 (invert-face face frame))
1176 ((eq (car colors) 'underline)
1177 (set-face-underline-p face t frame))
1178 (t
1179 (funcall function face (car colors) frame)))))
19ae9866 1180 (setq colors (cdr colors)))))))
e09c52a8
RS
1181
1182;; If we are already using x-window frames, initialize faces for them.
1183(if (eq (framep (selected-frame)) 'x)
1184 (face-initialize))
465fceed 1185
f0138172
JB
1186(provide 'faces)
1187
465fceed 1188;;; faces.el ends here