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