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