xref: /386bsd/usr/local/lib/emacs/19.25/lisp/faces.el (revision a2142627)
1;;; faces.el --- Lisp interface to the c "face" structure
2
3;; Copyright (C) 1992, 1993, 1994 Free Software Foundation, Inc.
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
27
28;;;; Functions for manipulating face vectors.
29
30;;; A face vector is a vector of the form:
31;;;    [face NAME ID FONT FOREGROUND BACKGROUND BACKGROUND-PIXMAP UNDERLINE]
32
33;;; Type checkers.
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
41;;; Accessors.
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.
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."
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.
62If FRAME is t, report on the defaults for face FACE (for new frames).
63If FRAME is omitted or nil, use the selected frame."
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.
69If FRAME is t, report on the defaults for face FACE (for new frames).
70If FRAME is omitted or nil, use the selected frame."
71  (aref (internal-get-face face frame) 5))
72
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))
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.
82If FRAME is t, report on the defaults for face FACE (for new frames).
83If FRAME is omitted or nil, use the selected frame."
84 (aref (internal-get-face face frame) 7))
85
86
87;;; Mutators.
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"))
94  (if (stringp font) (setq font (x-resolve-font-name font face frame)))
95  (internal-set-face-1 face 'font font 3 frame))
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"))
102  (internal-set-face-1 face 'foreground color 4 frame))
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"))
109  (internal-set-face-1 face 'background color 5 frame))
110
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.
115
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.
119
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))
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"))
130  (internal-set-face-1 face 'underline underline-p 7 frame))
131
132
133;;;; Associating face names (symbols) with their face vectors.
134
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
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
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."
214  (interactive "SMake face: ")
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))
233	face)))
234
235;; Fill in a face by default based on X resources, for all existing frames.
236;; This has to be done when a new face is made.
237(defun make-face-x-resource-internal (face &optional frame set-anyway)
238  (cond ((null frame)
239	 (let ((frames (frame-list)))
240	   (while frames
241	     (if (eq (framep (car frames)) 'x)
242		 (make-face-x-resource-internal (face-name face)
243						(car frames) set-anyway))
244	     (setq frames (cdr frames)))))
245	(t
246	 (setq face (internal-get-face (face-name face) frame))
247	 ;;
248	 ;; These are things like "attributeForeground" instead of simply
249	 ;; "foreground" because people tend to do things like "*foreground",
250	 ;; which would cause all faces to be fully qualified, making faces
251	 ;; inherit attributes in a non-useful way.  So we've made them slightly
252	 ;; less obvious to specify in order to make them work correctly in
253	 ;; more random environments.
254	 ;;
255	 ;; I think these should be called "face.faceForeground" instead of
256	 ;; "face.attributeForeground", but they're the way they are for
257	 ;; hysterical reasons.
258	 ;;
259	 (let* ((name (symbol-name (face-name face)))
260		(fn  (or (x-get-resource (concat name ".attributeFont")
261					 "Face.AttributeFont")
262			 (and set-anyway (face-font face))))
263		(fg  (or (x-get-resource (concat name ".attributeForeground")
264					 "Face.AttributeForeground")
265			 (and set-anyway (face-foreground face))))
266		(bg  (or (x-get-resource (concat name ".attributeBackground")
267					 "Face.AttributeBackground")
268			 (and set-anyway (face-background face))))
269;;		(bgp (or (x-get-resource (concat name ".attributeBackgroundPixmap")
270;;					 "Face.AttributeBackgroundPixmap")
271;;			 (and set-anyway (face-background-pixmap face))))
272		(ulp (or (x-get-resource (concat name ".attributeUnderline")
273					 "Face.AttributeUnderline")
274			 (and set-anyway (face-underline-p face))))
275		)
276	   (if fn
277	       (condition-case ()
278		   (set-face-font face fn frame)
279		 (error (message "font `%s' not found for face `%s'" fn name))))
280	   (if fg
281	       (condition-case ()
282		   (set-face-foreground face fg frame)
283		 (error (message "color `%s' not allocated for face `%s'" fg name))))
284	   (if bg
285	       (condition-case ()
286		   (set-face-background face bg frame)
287		 (error (message "color `%s' not allocated for face `%s'" bg name))))
288;;	   (if bgp
289;;	       (condition-case ()
290;;		   (set-face-background-pixmap face bgp frame)
291;;		 (error (message "pixmap `%s' not found for face `%s'" bgp name))))
292	   (if (or ulp set-anyway)
293	       (set-face-underline-p face ulp frame))
294	   )))
295  face)
296
297(defun copy-face (old-face new-face &optional frame new-frame)
298  "Define a face just like OLD-FACE, with name NEW-FACE.
299If NEW-FACE already exists as a face, it is modified to be like OLD-FACE.
300If it doesn't already exist, it is created.
301
302If the optional argument FRAME is given as a frame,
303NEW-FACE is changed on FRAME only.
304If FRAME is t, the frame-independent default specification for OLD-FACE
305is copied to NEW-FACE.
306If FRAME is nil, copying is done for the frame-independent defaults
307and for each existing frame.
308If the optional fourth argument NEW-FRAME is given,
309copy the information from face OLD-FACE on frame FRAME
310to NEW-FACE on frame NEW-FRAME."
311  (or new-frame (setq new-frame frame))
312  (let ((inhibit-quit t))
313    (if (null frame)
314	(let ((frames (frame-list)))
315	  (while frames
316	    (copy-face old-face new-face (car frames))
317	    (setq frames (cdr frames)))
318	  (copy-face old-face new-face t))
319      (setq old-face (internal-get-face old-face frame))
320      (setq new-face (or (internal-find-face new-face new-frame)
321			 (make-face new-face)))
322      (set-face-font new-face (face-font old-face frame) new-frame)
323      (set-face-foreground new-face (face-foreground old-face frame) new-frame)
324      (set-face-background new-face (face-background old-face frame) new-frame)
325;;;      (set-face-background-pixmap
326;;;       new-face (face-background-pixmap old-face frame) new-frame)
327      (set-face-underline-p new-face (face-underline-p old-face frame)
328			    new-frame))
329    new-face))
330
331(defun face-equal (face1 face2 &optional frame)
332  "True if the faces FACE1 and FACE2 display in the same way."
333  (setq face1 (internal-get-face face1 frame)
334	face2 (internal-get-face face2 frame))
335  (and (equal (face-foreground face1 frame) (face-foreground face2 frame))
336       (equal (face-background face1 frame) (face-background face2 frame))
337       (equal (face-font face1 frame) (face-font face2 frame))
338;;       (equal (face-background-pixmap face1 frame)
339;;	      (face-background-pixmap face2 frame))
340       ))
341
342(defun face-differs-from-default-p (face &optional frame)
343  "True if face FACE displays differently from the default face, on FRAME.
344A face is considered to be ``the same'' as the default face if it is
345actually specified in the same way (equivalent fonts, etc) or if it is
346fully unspecified, and thus inherits the attributes of any face it
347is displayed on top of."
348  (let ((default (internal-get-face 'default frame)))
349    (setq face (internal-get-face face frame))
350    (not (and (or (equal (face-foreground default frame)
351			 (face-foreground face frame))
352		  (null (face-foreground face frame)))
353	      (or (equal (face-background default frame)
354			 (face-background face frame))
355		  (null (face-background face frame)))
356	      (or (equal (face-font default frame) (face-font face frame))
357		  (null (face-font face frame)))
358;;;	      (or (equal (face-background-pixmap default frame)
359;;;			 (face-background-pixmap face frame))
360;;;		  (null (face-background-pixmap face frame)))
361	      (equal (face-underline-p default frame)
362		     (face-underline-p face frame))
363	      ))))
364
365
366(defun invert-face (face &optional frame)
367  "Swap the foreground and background colors of face FACE.
368If the face doesn't specify both foreground and background, then
369set its foreground and background to the default background and foreground."
370  (interactive (list (read-face-name "Invert face: ")))
371  (setq face (internal-get-face face frame))
372  (let ((fg (face-foreground face frame))
373	(bg (face-background face frame)))
374    (if (or fg bg)
375	(progn
376	  (set-face-foreground face bg frame)
377	  (set-face-background face fg frame))
378      (set-face-foreground face (or (face-background 'default frame)
379				    (cdr (assq 'background-color (frame-parameters frame))))
380			   frame)
381      (set-face-background face (or (face-foreground 'default frame)
382				    (cdr (assq 'foreground-color (frame-parameters frame))))
383			   frame)))
384  face)
385
386
387(defun internal-try-face-font (face font &optional frame)
388  "Like set-face-font, but returns nil on failure instead of an error."
389  (condition-case ()
390      (set-face-font face font frame)
391    (error nil)))
392
393;; Manipulating font names.
394
395(defconst x-font-regexp nil)
396(defconst x-font-regexp-head nil)
397(defconst x-font-regexp-weight nil)
398(defconst x-font-regexp-slant nil)
399
400;;; Regexps matching font names in "Host Portable Character Representation."
401;;;
402(let ((- 		"[-?]")
403      (foundry		"[^-]+")
404      (family 		"[^-]+")
405      (weight		"\\(bold\\|demibold\\|medium\\)")		; 1
406;     (weight\?		"\\(\\*\\|bold\\|demibold\\|medium\\|\\)")	; 1
407      (weight\?		"\\([^-]*\\)")					; 1
408      (slant		"\\([ior]\\)")					; 2
409;     (slant\?		"\\([ior?*]?\\)")				; 2
410      (slant\?		"\\([^-]?\\)")					; 2
411;     (swidth		"\\(\\*\\|normal\\|semicondensed\\|\\)")	; 3
412      (swidth		"\\([^-]*\\)")					; 3
413;     (adstyle		"\\(\\*\\|sans\\|\\)")				; 4
414      (adstyle		"[^-]*")					; 4
415      (pixelsize	"[0-9]+")
416      (pointsize	"[0-9][0-9]+")
417      (resx		"[0-9][0-9]+")
418      (resy		"[0-9][0-9]+")
419      (spacing		"[cmp?*]")
420      (avgwidth		"[0-9]+")
421      (registry		"[^-]+")
422      (encoding		"[^-]+")
423      )
424  (setq x-font-regexp
425	(concat "\\`\\*?[-?*]"
426		foundry - family - weight\? - slant\? - swidth - adstyle -
427		pixelsize - pointsize - resx - resy - spacing - registry -
428		encoding "[-?*]\\*?\\'"
429		))
430  (setq x-font-regexp-head
431	(concat "\\`[-?*]" foundry - family - weight\? - slant\?
432		"\\([-*?]\\|\\'\\)"))
433  (setq x-font-regexp-slant (concat - slant -))
434  (setq x-font-regexp-weight (concat - weight -))
435  nil)
436
437(defun x-resolve-font-name (pattern &optional face frame)
438  "Return a font name matching PATTERN.
439All wildcards in PATTERN become substantiated.
440If PATTERN is nil, return the name of the frame's base font, which never
441contains wildcards.
442Given optional arguments FACE and FRAME, try to return a font which is
443also the same size as FACE on FRAME."
444  (or (symbolp face)
445      (setq face (face-name face)))
446  (and (eq frame t)
447       (setq frame nil))
448  (if pattern
449      ;; Note that x-list-fonts has code to handle a face with nil as its font.
450      (let ((fonts (x-list-fonts pattern face frame)))
451	(or fonts
452	    (if face
453		(error "No fonts matching pattern are the same size as `%s'"
454		       face)
455	      (error "No fonts match `%s'" pattern)))
456	(car fonts))
457    (cdr (assq 'font (frame-parameters (selected-frame))))))
458
459(defun x-frob-font-weight (font which)
460  (if (or (string-match x-font-regexp font)
461	  (string-match x-font-regexp-head font)
462	  (string-match x-font-regexp-weight font))
463      (concat (substring font 0 (match-beginning 1)) which
464	      (substring font (match-end 1)))
465    nil))
466
467(defun x-frob-font-slant (font which)
468  (cond ((or (string-match x-font-regexp font)
469	     (string-match x-font-regexp-head font))
470	 (concat (substring font 0 (match-beginning 2)) which
471		 (substring font (match-end 2))))
472	((string-match x-font-regexp-slant font)
473	 (concat (substring font 0 (match-beginning 1)) which
474		 (substring font (match-end 1))))
475	(t nil)))
476
477
478(defun x-make-font-bold (font)
479  "Given an X font specification, make a bold version of it.
480If that can't be done, return nil."
481  (x-frob-font-weight font "bold"))
482
483(defun x-make-font-demibold (font)
484  "Given an X font specification, make a demibold version of it.
485If that can't be done, return nil."
486  (x-frob-font-weight font "demibold"))
487
488(defun x-make-font-unbold (font)
489  "Given an X font specification, make a non-bold version of it.
490If that can't be done, return nil."
491  (x-frob-font-weight font "medium"))
492
493(defun x-make-font-italic (font)
494  "Given an X font specification, make an italic version of it.
495If that can't be done, return nil."
496  (x-frob-font-slant font "i"))
497
498(defun x-make-font-oblique (font) ; you say tomayto...
499  "Given an X font specification, make an oblique version of it.
500If that can't be done, return nil."
501  (x-frob-font-slant font "o"))
502
503(defun x-make-font-unitalic (font)
504  "Given an X font specification, make a non-italic version of it.
505If that can't be done, return nil."
506  (x-frob-font-slant font "r"))
507
508;;; non-X-specific interface
509
510(defun make-face-bold (face &optional frame noerror)
511  "Make the font of the given face be bold, if possible.
512If NOERROR is non-nil, return nil on failure."
513  (interactive (list (read-face-name "Make which face bold: ")))
514  (if (and (eq frame t) (listp (face-font face t)))
515      (set-face-font face (if (memq 'italic (face-font face t))
516			      '(bold italic) '(bold))
517		     t)
518    (let ((ofont (face-font face frame))
519	  font f2)
520      (if (null frame)
521	  (let ((frames (frame-list)))
522	    ;; Make this face bold in global-face-data.
523	    (make-face-bold face t noerror)
524	    ;; Make this face bold in each frame.
525	    (while frames
526	      (make-face-bold face (car frames) noerror)
527	      (setq frames (cdr frames))))
528	(setq face (internal-get-face face frame))
529	(setq font (or (face-font face frame)
530		       (face-font face t)))
531	(if (listp font)
532	    (setq font nil))
533	(setq font (or font
534		       (face-font 'default frame)
535		       (cdr (assq 'font (frame-parameters frame)))))
536	(make-face-bold-internal face frame))
537      (or (not (equal ofont (face-font face)))
538	  (and (not noerror)
539	       (error "No bold version of %S" font))))))
540
541(defun make-face-bold-internal (face frame)
542  (or (and (setq f2 (x-make-font-bold font))
543	   (internal-try-face-font face f2 frame))
544      (and (setq f2 (x-make-font-demibold font))
545	   (internal-try-face-font face f2 frame))))
546
547(defun make-face-italic (face &optional frame noerror)
548  "Make the font of the given face be italic, if possible.
549If NOERROR is non-nil, return nil on failure."
550  (interactive (list (read-face-name "Make which face italic: ")))
551  (if (and (eq frame t) (listp (face-font face t)))
552      (set-face-font face (if (memq 'bold (face-font face t))
553			      '(bold italic) '(italic))
554		     t)
555    (let ((ofont (face-font face frame))
556	  font f2)
557      (if (null frame)
558	  (let ((frames (frame-list)))
559	    ;; Make this face italic in global-face-data.
560	    (make-face-italic face t noerror)
561	    ;; Make this face italic in each frame.
562	    (while frames
563	      (make-face-italic face (car frames) noerror)
564	      (setq frames (cdr frames))))
565	(setq face (internal-get-face face frame))
566	(setq font (or (face-font face frame)
567		       (face-font face t)))
568	(if (listp font)
569	    (setq font nil))
570	(setq font (or font
571		       (face-font 'default frame)
572		       (cdr (assq 'font (frame-parameters frame)))))
573	(make-face-italic-internal face frame))
574      (or (not (equal ofont (face-font face)))
575	  (and (not noerror)
576	       (error "No italic version of %S" font))))))
577
578(defun make-face-italic-internal (face frame)
579  (or (and (setq f2 (x-make-font-italic font))
580	   (internal-try-face-font face f2 frame))
581      (and (setq f2 (x-make-font-oblique font))
582	   (internal-try-face-font face f2 frame))))
583
584(defun make-face-bold-italic (face &optional frame noerror)
585  "Make the font of the given face be bold and italic, if possible.
586If NOERROR is non-nil, return nil on failure."
587  (interactive (list (read-face-name "Make which face bold-italic: ")))
588  (if (and (eq frame t) (listp (face-font face t)))
589      (set-face-font face '(bold italic) t)
590    (let ((ofont (face-font face frame))
591	  font)
592      (if (null frame)
593	  (let ((frames (frame-list)))
594	    ;; Make this face bold-italic in global-face-data.
595	    (make-face-bold-italic face t noerror)
596	    ;; Make this face bold in each frame.
597	    (while frames
598	      (make-face-bold-italic face (car frames) noerror)
599	      (setq frames (cdr frames))))
600	(setq face (internal-get-face face frame))
601	(setq font (or (face-font face frame)
602		       (face-font face t)))
603	(if (listp font)
604	    (setq font nil))
605	(setq font (or font
606		       (face-font 'default frame)
607		       (cdr (assq 'font (frame-parameters frame)))))
608	(make-face-bold-italic-internal face frame))
609      (or (not (equal ofont (face-font face)))
610	  (and (not noerror)
611	       (error "No bold italic version of %S" font))))))
612
613(defun make-face-bold-italic-internal (face frame)
614  (let (f2 f3)
615    (or (and (setq f2 (x-make-font-italic font))
616	     (not (equal font f2))
617	     (setq f3 (x-make-font-bold f2))
618	     (not (equal f2 f3))
619	     (internal-try-face-font face f3 frame))
620	(and (setq f2 (x-make-font-oblique font))
621	     (not (equal font f2))
622	     (setq f3 (x-make-font-bold f2))
623	     (not (equal f2 f3))
624	     (internal-try-face-font face f3 frame))
625	(and (setq f2 (x-make-font-italic font))
626	     (not (equal font f2))
627	     (setq f3 (x-make-font-demibold f2))
628	     (not (equal f2 f3))
629	     (internal-try-face-font face f3 frame))
630	(and (setq f2 (x-make-font-oblique font))
631	     (not (equal font f2))
632	     (setq f3 (x-make-font-demibold f2))
633	     (not (equal f2 f3))
634	     (internal-try-face-font face f3 frame)))))
635
636(defun make-face-unbold (face &optional frame noerror)
637  "Make the font of the given face be non-bold, if possible.
638If NOERROR is non-nil, return nil on failure."
639  (interactive (list (read-face-name "Make which face non-bold: ")))
640  (if (and (eq frame t) (listp (face-font face t)))
641      (set-face-font face (if (memq 'italic (face-font face t))
642			      '(italic) nil)
643		     t)
644    (let ((ofont (face-font face frame))
645	  font font1)
646      (if (null frame)
647	  (let ((frames (frame-list)))
648	    ;; Make this face unbold in global-face-data.
649	    (make-face-unbold face t noerror)
650	    ;; Make this face unbold in each frame.
651	    (while frames
652	      (make-face-unbold face (car frames) noerror)
653	      (setq frames (cdr frames))))
654	(setq face (internal-get-face face frame))
655	(setq font1 (or (face-font face frame)
656			(face-font face t)))
657	(if (listp font1)
658	    (setq font1 nil))
659	(setq font1 (or font1
660			(face-font 'default frame)
661			(cdr (assq 'font (frame-parameters frame)))))
662	(setq font (x-make-font-unbold font1))
663	(if font (internal-try-face-font face font frame)))
664      (or (not (equal ofont (face-font face)))
665	  (and (not noerror)
666	       (error "No unbold version of %S" font1))))))
667
668(defun make-face-unitalic (face &optional frame noerror)
669  "Make the font of the given face be non-italic, if possible.
670If NOERROR is non-nil, return nil on failure."
671  (interactive (list (read-face-name "Make which face non-italic: ")))
672  (if (and (eq frame t) (listp (face-font face t)))
673      (set-face-font face (if (memq 'bold (face-font face t))
674			      '(bold) nil)
675		     t)
676    (let ((ofont (face-font face frame))
677	  font font1)
678      (if (null frame)
679	  (let ((frames (frame-list)))
680	    ;; Make this face unitalic in global-face-data.
681	    (make-face-unitalic face t noerror)
682	    ;; Make this face unitalic in each frame.
683	    (while frames
684	      (make-face-unitalic face (car frames) noerror)
685	      (setq frames (cdr frames))))
686	(setq face (internal-get-face face frame))
687	(setq font1 (or (face-font face frame)
688			(face-font face t)))
689	(if (listp font1)
690	    (setq font1 nil))
691	(setq font1 (or font1
692			(face-font 'default frame)
693			(cdr (assq 'font (frame-parameters frame)))))
694	(setq font (x-make-font-unitalic font1))
695	(if font (internal-try-face-font face font frame)))
696      (or (not (equal ofont (face-font face)))
697	  (and (not noerror)
698	       (error "No unitalic version of %S" font1))))))
699
700(defvar list-faces-sample-text
701  "abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ"
702  "*Text string to display as the sample text for `list-faces-display'.")
703
704;; The name list-faces would be more consistent, but let's avoid a conflict
705;; with Lucid, which uses that name differently.
706(defun list-faces-display ()
707  "List all faces, using the same sample text in each.
708The sample text is a string that comes from the variable
709`list-faces-sample-text'.
710
711It is possible to give a particular face name different appearances in
712different frames.  This command shows the appearance in the
713selected frame."
714  (interactive)
715  (let ((faces (sort (face-list) (function string-lessp)))
716	(face nil)
717	(frame (selected-frame))
718	disp-frame window)
719    (with-output-to-temp-buffer "*Faces*"
720      (save-excursion
721	(set-buffer standard-output)
722	(setq truncate-lines t)
723	(while faces
724	  (setq face (car faces))
725	  (setq faces (cdr faces))
726	  (insert (format "%25s " (symbol-name face)))
727	  (let ((beg (point)))
728	    (insert list-faces-sample-text)
729	    (insert "\n")
730	    (put-text-property beg (1- (point)) 'face face)))
731	(goto-char (point-min))))
732    ;; If the *Faces* buffer appears in a different frame,
733    ;; copy all the face definitions from FRAME,
734    ;; so that the display will reflect the frame that was selected.
735    (setq window (get-buffer-window (get-buffer "*Faces*") t))
736    (setq disp-frame (if window (window-frame window)
737		       (car (frame-list))))
738    (or (eq frame disp-frame)
739	(let ((faces (face-list)))
740	  (while faces
741	    (copy-face (car faces) (car faces) frame disp-frame)
742	    (setq faces (cdr faces)))))))
743
744;;; Make the standard faces.
745;;; The C code knows the default and modeline faces as faces 0 and 1,
746;;; so they must be the first two faces made.
747(defun face-initialize ()
748  (make-face 'default)
749  (make-face 'modeline)
750  (make-face 'highlight)
751
752  ;; These aren't really special in any way, but they're nice to have around.
753
754  (make-face 'bold)
755  (make-face 'italic)
756  (make-face 'bold-italic)
757  (make-face 'region)
758  (make-face 'secondary-selection)
759  (make-face 'underline)
760
761  (setq region-face (face-id 'region))
762
763  ;; Specify the global properties of these faces
764  ;; so they will come out right on new frames.
765
766  (make-face-bold 'bold t)
767  (make-face-italic 'italic t)
768  (make-face-bold-italic 'bold-italic t)
769
770  (set-face-background 'highlight '("darkseagreen2" "green" t) t)
771  (set-face-background 'region '("gray" t) t)
772  (set-face-background 'secondary-selection '("paleturquoise" "green" t) t)
773  (set-face-background 'modeline '(t) t)
774  (set-face-underline-p 'underline t t)
775
776  ;; Set up the faces of all existing X Window frames
777  ;; from those global properties, unless already set in a given frame.
778
779  (let ((frames (frame-list)))
780    (while frames
781      (if (eq (framep (car frames)) 'x)
782	  (let ((frame (car frames))
783		(rest global-face-data))
784	    (while rest
785	      (let ((face (car (car rest))))
786		(or (face-differs-from-default-p face)
787		    (face-fill-in face (cdr (car rest)) frame)))
788	      (setq rest (cdr rest)))))
789      (setq frames (cdr frames)))))
790
791
792;; Like x-create-frame but also set up the faces.
793
794(defun x-create-frame-with-faces (&optional parameters)
795  (if (null global-face-data)
796      (x-create-frame parameters)
797    (let* ((visibility-spec (assq 'visibility parameters))
798	   (frame (x-create-frame (cons '(visibility . nil) parameters)))
799	   (faces (copy-alist global-face-data))
800	   (rest faces))
801      (set-frame-face-alist frame faces)
802
803      (if (cdr (or (assq 'reverse parameters)
804		   (assq 'reverse default-frame-alist)
805		   (cons nil
806			 (member (x-get-resource "reverseVideo" "ReverseVideo")
807				 '("on" "true")))))
808	  (let ((params (frame-parameters frame)))
809	    (modify-frame-parameters
810	     frame
811	     (list (cons 'foreground-color (cdr (assq 'background-color params)))
812		   (cons 'background-color (cdr (assq 'foreground-color params)))
813		   (cons 'mouse-color (cdr (assq 'background-color params)))
814		   (cons 'cursor-color (cdr (assq 'background-color params)))
815		   (cons 'border-color (cdr (assq 'background-color params)))))))
816
817      ;; Copy the vectors that represent the faces.
818      ;; Also fill them in from X resources.
819      (while rest
820	(let ((global (cdr (car rest))))
821	  (setcdr (car rest) (vector 'face
822				     (face-name (cdr (car rest)))
823				     (face-id (cdr (car rest)))
824				     nil nil nil nil nil))
825	  (face-fill-in (car (car rest)) global frame))
826	(make-face-x-resource-internal (cdr (car rest)) frame t)
827	(setq rest (cdr rest)))
828      (if (null visibility-spec)
829	  (make-frame-visible frame)
830	(modify-frame-parameters frame (list visibility-spec)))
831      frame)))
832
833;; Update a frame's faces when we change its default font.
834(defun frame-update-faces (frame)
835  (let* ((faces global-face-data)
836	 (rest faces))
837    (while rest
838      (let* ((face (car (car rest)))
839	     (font (face-font face t)))
840	(if (listp font)
841	    (let ((bold (memq 'bold font))
842		  (italic (memq 'italic font)))
843	      ;; Ignore any previous (string-valued) font, it might not even
844	      ;; be the right size anymore.
845	      (set-face-font face nil frame)
846	      (cond ((and bold italic)
847		     (make-face-bold-italic face frame t))
848		    (bold
849		     (make-face-bold face frame t))
850		    (italic
851		     (make-face-italic face frame t)))))
852      (setq rest (cdr rest)))
853    frame)))
854
855;; Fill in the face FACE from frame-independent face data DATA.
856;; DATA should be the non-frame-specific ("global") face vector
857;; for the face.  FACE should be a face name or face object.
858;; FRAME is the frame to act on; it must be an actual frame, not nil or t.
859(defun face-fill-in (face data frame)
860  (condition-case nil
861      (let ((foreground (face-foreground data))
862	    (background (face-background data))
863	    (font (face-font data)))
864	(set-face-underline-p face (face-underline-p data) frame)
865	(if foreground
866	    (face-try-color-list 'set-face-foreground
867				 face foreground frame))
868	(if background
869	    (face-try-color-list 'set-face-background
870				 face background frame))
871	(if (listp font)
872	    (let ((bold (memq 'bold font))
873		  (italic (memq 'italic font)))
874	      (cond ((and bold italic)
875		     (make-face-bold-italic face frame))
876		    (bold
877		     (make-face-bold face frame))
878		    (italic
879		     (make-face-italic face frame))))
880	  (if font
881	      (set-face-font face font frame))))
882    (error nil)))
883
884;; Use FUNCTION to store a color in FACE on FRAME.
885;; COLORS is either a single color or a list of colors.
886;; If it is a list, try the colors one by one until one of them
887;; succeeds.  We signal an error only if all the colors failed.
888;; t as COLORS or as an element of COLORS means to invert the face.
889;; That can't fail, so any subsequent elements after the t are ignored.
890(defun face-try-color-list (function face colors frame)
891  (if (stringp colors)
892      (if (or (and (not (x-display-color-p)) (not (string= colors "gray")))
893	      (= (x-display-planes) 1))
894	  nil
895	(funcall function face colors frame))
896    (if (eq colors t)
897	(invert-face face frame)
898      (let (done)
899	(while (and colors (not done))
900	  (if (and (stringp (car colors))
901		   (or (and (not (x-display-color-p))
902			    (not (string= (car colors) "gray")))
903		       (= (x-display-planes) 1)))
904	      nil
905	    (if (cdr colors)
906		;; If there are more colors to try, catch errors
907		;; and set `done' if we succeed.
908		(condition-case nil
909		    (progn
910		      (if (eq (car colors) t)
911			  (invert-face face frame)
912			(funcall function face (car colors) frame))
913		      (setq done t))
914		  (error nil))
915	      ;; If this is the last color, let the error get out if it fails.
916	      ;; If it succeeds, we will exit anyway after this iteration.
917	      (if (eq (car colors) t)
918		  (invert-face face frame)
919		(funcall function face (car colors) frame))))
920	  (setq colors (cdr colors)))))))
921
922;; If we are already using x-window frames, initialize faces for them.
923(if (eq (framep (selected-frame)) 'x)
924    (face-initialize))
925
926(provide 'faces)
927
928;;; faces.el ends here
929