(Fx_create_frame): Make 1 the default for menu-bar-lines.
[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)))
52267b56
RS
326 (condition-case nil
327 ;; A face that has a global symbolic font modifier such as `bold'
328 ;; might legitimately get an error here.
329 ;; Use the frame's default font in that case.
330 (set-face-font new-face (face-font old-face frame) new-frame)
331 (error
332 (set-face-font new-face nil new-frame)))
710e7005
RS
333 (set-face-foreground new-face (face-foreground old-face frame) new-frame)
334 (set-face-background new-face (face-background old-face frame) new-frame)
8494bbf1 335;;; (set-face-background-pixmap
710e7005 336;;; new-face (face-background-pixmap old-face frame) new-frame)
465fceed 337 (set-face-underline-p new-face (face-underline-p old-face frame)
710e7005 338 new-frame))
465fceed
ER
339 new-face))
340
341(defun face-equal (face1 face2 &optional frame)
ade516a1 342 "True if the faces FACE1 and FACE2 display in the same way."
465fceed
ER
343 (setq face1 (internal-get-face face1 frame)
344 face2 (internal-get-face face2 frame))
345 (and (equal (face-foreground face1 frame) (face-foreground face2 frame))
346 (equal (face-background face1 frame) (face-background face2 frame))
347 (equal (face-font face1 frame) (face-font face2 frame))
ae0249df 348 (eq (face-underline-p face1 frame) (face-underline-p face2 frame))
11872f7e
RS
349;; (equal (face-background-pixmap face1 frame)
350;; (face-background-pixmap face2 frame))
351 ))
465fceed
ER
352
353(defun face-differs-from-default-p (face &optional frame)
354 "True if face FACE displays differently from the default face, on FRAME.
355A face is considered to be ``the same'' as the default face if it is
356actually specified in the same way (equivalent fonts, etc) or if it is
357fully unspecified, and thus inherits the attributes of any face it
358is displayed on top of."
359 (let ((default (internal-get-face 'default frame)))
360 (setq face (internal-get-face face frame))
361 (not (and (or (equal (face-foreground default frame)
362 (face-foreground face frame))
363 (null (face-foreground face frame)))
364 (or (equal (face-background default frame)
365 (face-background face frame))
366 (null (face-background face frame)))
367 (or (equal (face-font default frame) (face-font face frame))
368 (null (face-font face frame)))
8494bbf1
RS
369;;; (or (equal (face-background-pixmap default frame)
370;;; (face-background-pixmap face frame))
371;;; (null (face-background-pixmap face frame)))
465fceed
ER
372 (equal (face-underline-p default frame)
373 (face-underline-p face frame))
374 ))))
375
376
377(defun invert-face (face &optional frame)
378 "Swap the foreground and background colors of face FACE.
379If the face doesn't specify both foreground and background, then
8494bbf1 380set its foreground and background to the default background and foreground."
465fceed
ER
381 (interactive (list (read-face-name "Invert face: ")))
382 (setq face (internal-get-face face frame))
383 (let ((fg (face-foreground face frame))
384 (bg (face-background face frame)))
385 (if (or fg bg)
386 (progn
387 (set-face-foreground face bg frame)
388 (set-face-background face fg frame))
8494bbf1
RS
389 (set-face-foreground face (or (face-background 'default frame)
390 (cdr (assq 'background-color (frame-parameters frame))))
391 frame)
392 (set-face-background face (or (face-foreground 'default frame)
393 (cdr (assq 'foreground-color (frame-parameters frame))))
394 frame)))
465fceed
ER
395 face)
396
397
398(defun internal-try-face-font (face font &optional frame)
399 "Like set-face-font, but returns nil on failure instead of an error."
400 (condition-case ()
401 (set-face-font face font frame)
402 (error nil)))
465fceed
ER
403\f
404;; Manipulating font names.
405
406(defconst x-font-regexp nil)
407(defconst x-font-regexp-head nil)
408(defconst x-font-regexp-weight nil)
409(defconst x-font-regexp-slant nil)
410
411;;; Regexps matching font names in "Host Portable Character Representation."
412;;;
413(let ((- "[-?]")
414 (foundry "[^-]+")
415 (family "[^-]+")
416 (weight "\\(bold\\|demibold\\|medium\\)") ; 1
417; (weight\? "\\(\\*\\|bold\\|demibold\\|medium\\|\\)") ; 1
418 (weight\? "\\([^-]*\\)") ; 1
419 (slant "\\([ior]\\)") ; 2
420; (slant\? "\\([ior?*]?\\)") ; 2
421 (slant\? "\\([^-]?\\)") ; 2
422; (swidth "\\(\\*\\|normal\\|semicondensed\\|\\)") ; 3
423 (swidth "\\([^-]*\\)") ; 3
424; (adstyle "\\(\\*\\|sans\\|\\)") ; 4
425 (adstyle "[^-]*") ; 4
426 (pixelsize "[0-9]+")
427 (pointsize "[0-9][0-9]+")
428 (resx "[0-9][0-9]+")
429 (resy "[0-9][0-9]+")
430 (spacing "[cmp?*]")
431 (avgwidth "[0-9]+")
432 (registry "[^-]+")
433 (encoding "[^-]+")
434 )
435 (setq x-font-regexp
436 (concat "\\`\\*?[-?*]"
437 foundry - family - weight\? - slant\? - swidth - adstyle -
438 pixelsize - pointsize - resx - resy - spacing - registry -
439 encoding "[-?*]\\*?\\'"
440 ))
441 (setq x-font-regexp-head
442 (concat "\\`[-?*]" foundry - family - weight\? - slant\?
443 "\\([-*?]\\|\\'\\)"))
444 (setq x-font-regexp-slant (concat - slant -))
445 (setq x-font-regexp-weight (concat - weight -))
446 nil)
447
281dc1c2
JB
448(defun x-resolve-font-name (pattern &optional face frame)
449 "Return a font name matching PATTERN.
450All wildcards in PATTERN become substantiated.
10d89673
JB
451If PATTERN is nil, return the name of the frame's base font, which never
452contains wildcards.
281dc1c2 453Given optional arguments FACE and FRAME, try to return a font which is
10d89673 454also the same size as FACE on FRAME."
14e6867c
RS
455 (or (symbolp face)
456 (setq face (face-name face)))
457 (and (eq frame t)
458 (setq frame nil))
10d89673 459 (if pattern
8db93e45
RS
460 ;; Note that x-list-fonts has code to handle a face with nil as its font.
461 (let ((fonts (x-list-fonts pattern face frame)))
10d89673
JB
462 (or fonts
463 (if face
7cf6fac1 464 (error "No fonts matching pattern are the same size as `%s'"
25c08a32 465 face)
7cf6fac1 466 (error "No fonts match `%s'" pattern)))
10d89673
JB
467 (car fonts))
468 (cdr (assq 'font (frame-parameters (selected-frame))))))
281dc1c2 469
465fceed
ER
470(defun x-frob-font-weight (font which)
471 (if (or (string-match x-font-regexp font)
472 (string-match x-font-regexp-head font)
473 (string-match x-font-regexp-weight font))
474 (concat (substring font 0 (match-beginning 1)) which
475 (substring font (match-end 1)))
476 nil))
477
478(defun x-frob-font-slant (font which)
479 (cond ((or (string-match x-font-regexp font)
480 (string-match x-font-regexp-head font))
481 (concat (substring font 0 (match-beginning 2)) which
482 (substring font (match-end 2))))
483 ((string-match x-font-regexp-slant font)
484 (concat (substring font 0 (match-beginning 1)) which
485 (substring font (match-end 1))))
486 (t nil)))
487
488
489(defun x-make-font-bold (font)
f3f31ccf
RS
490 "Given an X font specification, make a bold version of it.
491If that can't be done, return nil."
465fceed
ER
492 (x-frob-font-weight font "bold"))
493
494(defun x-make-font-demibold (font)
f3f31ccf
RS
495 "Given an X font specification, make a demibold version of it.
496If that can't be done, return nil."
465fceed
ER
497 (x-frob-font-weight font "demibold"))
498
499(defun x-make-font-unbold (font)
f3f31ccf
RS
500 "Given an X font specification, make a non-bold version of it.
501If that can't be done, return nil."
465fceed
ER
502 (x-frob-font-weight font "medium"))
503
504(defun x-make-font-italic (font)
f3f31ccf
RS
505 "Given an X font specification, make an italic version of it.
506If that can't be done, return nil."
465fceed
ER
507 (x-frob-font-slant font "i"))
508
509(defun x-make-font-oblique (font) ; you say tomayto...
f3f31ccf
RS
510 "Given an X font specification, make an oblique version of it.
511If that can't be done, return nil."
465fceed
ER
512 (x-frob-font-slant font "o"))
513
514(defun x-make-font-unitalic (font)
f3f31ccf
RS
515 "Given an X font specification, make a non-italic version of it.
516If that can't be done, return nil."
465fceed 517 (x-frob-font-slant font "r"))
465fceed
ER
518\f
519;;; non-X-specific interface
520
bb9a81fc 521(defun make-face-bold (face &optional frame noerror)
465fceed 522 "Make the font of the given face be bold, if possible.
bb9a81fc 523If NOERROR is non-nil, return nil on failure."
465fceed 524 (interactive (list (read-face-name "Make which face bold: ")))
534dbc97 525 (if (and (eq frame t) (listp (face-font face t)))
f3f31ccf
RS
526 (set-face-font face (if (memq 'italic (face-font face t))
527 '(bold italic) '(bold))
528 t)
529 (let ((ofont (face-font face frame))
4b3203d9 530 font)
f3f31ccf
RS
531 (if (null frame)
532 (let ((frames (frame-list)))
533 ;; Make this face bold in global-face-data.
534 (make-face-bold face t noerror)
535 ;; Make this face bold in each frame.
536 (while frames
537 (make-face-bold face (car frames) noerror)
538 (setq frames (cdr frames))))
539 (setq face (internal-get-face face frame))
540 (setq font (or (face-font face frame)
541 (face-font face t)))
542 (if (listp font)
543 (setq font nil))
544 (setq font (or font
545 (face-font 'default frame)
546 (cdr (assq 'font (frame-parameters frame)))))
96616c04 547 (and font (make-face-bold-internal face frame font)))
f3f31ccf
RS
548 (or (not (equal ofont (face-font face)))
549 (and (not noerror)
550 (error "No bold version of %S" font))))))
551
4b3203d9
RS
552(defun make-face-bold-internal (face frame font)
553 (let (f2)
554 (or (and (setq f2 (x-make-font-bold font))
555 (internal-try-face-font face f2 frame))
556 (and (setq f2 (x-make-font-demibold font))
557 (internal-try-face-font face f2 frame)))))
bb9a81fc
JB
558
559(defun make-face-italic (face &optional frame noerror)
465fceed 560 "Make the font of the given face be italic, if possible.
bb9a81fc 561If NOERROR is non-nil, return nil on failure."
465fceed 562 (interactive (list (read-face-name "Make which face italic: ")))
534dbc97 563 (if (and (eq frame t) (listp (face-font face t)))
f3f31ccf
RS
564 (set-face-font face (if (memq 'bold (face-font face t))
565 '(bold italic) '(italic))
566 t)
567 (let ((ofont (face-font face frame))
4b3203d9 568 font)
f3f31ccf
RS
569 (if (null frame)
570 (let ((frames (frame-list)))
571 ;; Make this face italic in global-face-data.
572 (make-face-italic face t noerror)
573 ;; Make this face italic in each frame.
574 (while frames
575 (make-face-italic face (car frames) noerror)
576 (setq frames (cdr frames))))
577 (setq face (internal-get-face face frame))
578 (setq font (or (face-font face frame)
579 (face-font face t)))
580 (if (listp font)
581 (setq font nil))
582 (setq font (or font
583 (face-font 'default frame)
584 (cdr (assq 'font (frame-parameters frame)))))
96616c04 585 (and font (make-face-italic-internal face frame font)))
f3f31ccf
RS
586 (or (not (equal ofont (face-font face)))
587 (and (not noerror)
588 (error "No italic version of %S" font))))))
589
4b3203d9
RS
590(defun make-face-italic-internal (face frame font)
591 (let (f2)
592 (or (and (setq f2 (x-make-font-italic font))
593 (internal-try-face-font face f2 frame))
594 (and (setq f2 (x-make-font-oblique font))
595 (internal-try-face-font face f2 frame)))))
bb9a81fc
JB
596
597(defun make-face-bold-italic (face &optional frame noerror)
465fceed 598 "Make the font of the given face be bold and italic, if possible.
bb9a81fc 599If NOERROR is non-nil, return nil on failure."
465fceed 600 (interactive (list (read-face-name "Make which face bold-italic: ")))
534dbc97 601 (if (and (eq frame t) (listp (face-font face t)))
f3f31ccf
RS
602 (set-face-font face '(bold italic) t)
603 (let ((ofont (face-font face frame))
604 font)
605 (if (null frame)
606 (let ((frames (frame-list)))
607 ;; Make this face bold-italic in global-face-data.
608 (make-face-bold-italic face t noerror)
609 ;; Make this face bold in each frame.
610 (while frames
611 (make-face-bold-italic face (car frames) noerror)
612 (setq frames (cdr frames))))
613 (setq face (internal-get-face face frame))
614 (setq font (or (face-font face frame)
615 (face-font face t)))
616 (if (listp font)
617 (setq font nil))
618 (setq font (or font
619 (face-font 'default frame)
620 (cdr (assq 'font (frame-parameters frame)))))
96616c04 621 (and font (make-face-bold-italic-internal face frame font)))
f3f31ccf
RS
622 (or (not (equal ofont (face-font face)))
623 (and (not noerror)
624 (error "No bold italic version of %S" font))))))
625
4b3203d9 626(defun make-face-bold-italic-internal (face frame font)
f3f31ccf
RS
627 (let (f2 f3)
628 (or (and (setq f2 (x-make-font-italic font))
629 (not (equal font f2))
630 (setq f3 (x-make-font-bold f2))
631 (not (equal f2 f3))
632 (internal-try-face-font face f3 frame))
633 (and (setq f2 (x-make-font-oblique font))
634 (not (equal font f2))
635 (setq f3 (x-make-font-bold f2))
636 (not (equal f2 f3))
637 (internal-try-face-font face f3 frame))
638 (and (setq f2 (x-make-font-italic font))
639 (not (equal font f2))
640 (setq f3 (x-make-font-demibold f2))
641 (not (equal f2 f3))
642 (internal-try-face-font face f3 frame))
643 (and (setq f2 (x-make-font-oblique font))
644 (not (equal font f2))
645 (setq f3 (x-make-font-demibold f2))
646 (not (equal f2 f3))
647 (internal-try-face-font face f3 frame)))))
bb9a81fc
JB
648
649(defun make-face-unbold (face &optional frame noerror)
465fceed 650 "Make the font of the given face be non-bold, if possible.
bb9a81fc 651If NOERROR is non-nil, return nil on failure."
465fceed 652 (interactive (list (read-face-name "Make which face non-bold: ")))
534dbc97 653 (if (and (eq frame t) (listp (face-font face t)))
f3f31ccf
RS
654 (set-face-font face (if (memq 'italic (face-font face t))
655 '(italic) nil)
656 t)
657 (let ((ofont (face-font face frame))
658 font font1)
659 (if (null frame)
660 (let ((frames (frame-list)))
661 ;; Make this face unbold in global-face-data.
662 (make-face-unbold face t noerror)
663 ;; Make this face unbold in each frame.
664 (while frames
665 (make-face-unbold face (car frames) noerror)
666 (setq frames (cdr frames))))
667 (setq face (internal-get-face face frame))
668 (setq font1 (or (face-font face frame)
669 (face-font face t)))
670 (if (listp font1)
671 (setq font1 nil))
672 (setq font1 (or font1
673 (face-font 'default frame)
674 (cdr (assq 'font (frame-parameters frame)))))
96616c04 675 (setq font (and font1 (x-make-font-unbold font1)))
f3f31ccf
RS
676 (if font (internal-try-face-font face font frame)))
677 (or (not (equal ofont (face-font face)))
678 (and (not noerror)
679 (error "No unbold version of %S" font1))))))
bb9a81fc
JB
680
681(defun make-face-unitalic (face &optional frame noerror)
465fceed 682 "Make the font of the given face be non-italic, if possible.
bb9a81fc 683If NOERROR is non-nil, return nil on failure."
465fceed 684 (interactive (list (read-face-name "Make which face non-italic: ")))
534dbc97 685 (if (and (eq frame t) (listp (face-font face t)))
f3f31ccf
RS
686 (set-face-font face (if (memq 'bold (face-font face t))
687 '(bold) nil)
688 t)
689 (let ((ofont (face-font face frame))
690 font font1)
691 (if (null frame)
692 (let ((frames (frame-list)))
693 ;; Make this face unitalic in global-face-data.
694 (make-face-unitalic face t noerror)
695 ;; Make this face unitalic in each frame.
696 (while frames
697 (make-face-unitalic face (car frames) noerror)
698 (setq frames (cdr frames))))
699 (setq face (internal-get-face face frame))
700 (setq font1 (or (face-font face frame)
701 (face-font face t)))
702 (if (listp font1)
703 (setq font1 nil))
704 (setq font1 (or font1
705 (face-font 'default frame)
706 (cdr (assq 'font (frame-parameters frame)))))
96616c04 707 (setq font (and font1 (x-make-font-unitalic font1)))
f3f31ccf
RS
708 (if font (internal-try-face-font face font frame)))
709 (or (not (equal ofont (face-font face)))
710 (and (not noerror)
711 (error "No unitalic version of %S" font1))))))
465fceed 712\f
710e7005
RS
713(defvar list-faces-sample-text
714 "abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ"
715 "*Text string to display as the sample text for `list-faces-display'.")
716
717;; The name list-faces would be more consistent, but let's avoid a conflict
718;; with Lucid, which uses that name differently.
719(defun list-faces-display ()
720 "List all faces, using the same sample text in each.
721The sample text is a string that comes from the variable
722`list-faces-sample-text'.
723
724It is possible to give a particular face name different appearances in
725different frames. This command shows the appearance in the
726selected frame."
727 (interactive)
728 (let ((faces (sort (face-list) (function string-lessp)))
729 (face nil)
730 (frame (selected-frame))
731 disp-frame window)
732 (with-output-to-temp-buffer "*Faces*"
733 (save-excursion
734 (set-buffer standard-output)
735 (setq truncate-lines t)
736 (while faces
737 (setq face (car faces))
738 (setq faces (cdr faces))
739 (insert (format "%25s " (symbol-name face)))
740 (let ((beg (point)))
741 (insert list-faces-sample-text)
742 (insert "\n")
23b04eac
RS
743 (put-text-property beg (1- (point)) 'face face)
744 ;; If the sample text has multiple lines, line up all of them.
745 (goto-char beg)
746 (forward-line 1)
747 (while (not (eobp))
748 (insert " ")
749 (forward-line 1))))
710e7005
RS
750 (goto-char (point-min))))
751 ;; If the *Faces* buffer appears in a different frame,
752 ;; copy all the face definitions from FRAME,
753 ;; so that the display will reflect the frame that was selected.
754 (setq window (get-buffer-window (get-buffer "*Faces*") t))
755 (setq disp-frame (if window (window-frame window)
756 (car (frame-list))))
757 (or (eq frame disp-frame)
758 (let ((faces (face-list)))
759 (while faces
760 (copy-face (car faces) (car faces) frame disp-frame)
761 (setq faces (cdr faces)))))))
762\f
19ae9866
RS
763;;; Make the standard faces.
764;;; The C code knows the default and modeline faces as faces 0 and 1,
765;;; so they must be the first two faces made.
e09c52a8 766(defun face-initialize ()
465fceed 767 (make-face 'default)
ebea97d8 768 (make-face 'modeline)
465fceed 769 (make-face 'highlight)
19ae9866 770
465fceed 771 ;; These aren't really special in any way, but they're nice to have around.
19ae9866 772
465fceed
ER
773 (make-face 'bold)
774 (make-face 'italic)
775 (make-face 'bold-italic)
578d09a6 776 (make-face 'region)
e09c52a8 777 (make-face 'secondary-selection)
f4b9e76b 778 (make-face 'underline)
e09c52a8 779
578d09a6 780 (setq region-face (face-id 'region))
8494bbf1 781
19ae9866
RS
782 ;; Specify the global properties of these faces
783 ;; so they will come out right on new frames.
784
785 (make-face-bold 'bold t)
786 (make-face-italic 'italic t)
787 (make-face-bold-italic 'bold-italic t)
788
789 (set-face-background 'highlight '("darkseagreen2" "green" t) t)
566b0ad2 790 (set-face-background 'region '("gray" underline) t)
19ae9866
RS
791 (set-face-background 'secondary-selection '("paleturquoise" "green" t) t)
792 (set-face-background 'modeline '(t) t)
793 (set-face-underline-p 'underline t t)
794
795 ;; Set up the faces of all existing X Window frames
796 ;; from those global properties, unless already set in a given frame.
797
e09c52a8
RS
798 (let ((frames (frame-list)))
799 (while frames
800 (if (eq (framep (car frames)) 'x)
19ae9866
RS
801 (let ((frame (car frames))
802 (rest global-face-data))
803 (while rest
804 (let ((face (car (car rest))))
805 (or (face-differs-from-default-p face)
806 (face-fill-in face (cdr (car rest)) frame)))
807 (setq rest (cdr rest)))))
e09c52a8
RS
808 (setq frames (cdr frames)))))
809
465fceed 810\f
465fceed
ER
811;; Like x-create-frame but also set up the faces.
812
813(defun x-create-frame-with-faces (&optional parameters)
e09c52a8
RS
814 (if (null global-face-data)
815 (x-create-frame parameters)
e69e8fad
RS
816 (let* ((visibility-spec (assq 'visibility parameters))
817 (frame (x-create-frame (cons '(visibility . nil) parameters)))
e09c52a8 818 (faces (copy-alist global-face-data))
ebea97d8 819 (rest faces))
e09c52a8
RS
820 (set-frame-face-alist frame faces)
821
1dd6d2a2 822 (if (cdr (or (assq 'reverse parameters)
1b691d5c 823 (assq 'reverse default-frame-alist)
a48e03c3
RS
824 (let ((resource (x-get-resource "reverseVideo"
825 "ReverseVideo")))
826 (if resource
827 (cons nil (member (downcase resource)
828 '("on" "true")))))))
1dd6d2a2
RS
829 (let ((params (frame-parameters frame)))
830 (modify-frame-parameters
831 frame
832 (list (cons 'foreground-color (cdr (assq 'background-color params)))
833 (cons 'background-color (cdr (assq 'foreground-color params)))
834 (cons 'mouse-color (cdr (assq 'background-color params)))
2dd60dfd
RS
835 (cons 'border-color (cdr (assq 'background-color params)))))
836 (modify-frame-parameters
837 frame
838 (list (cons 'cursor-color (cdr (assq 'background-color params)))))))
1dd6d2a2 839
e09c52a8
RS
840 ;; Copy the vectors that represent the faces.
841 ;; Also fill them in from X resources.
842 (while rest
19ae9866
RS
843 (let ((global (cdr (car rest))))
844 (setcdr (car rest) (vector 'face
845 (face-name (cdr (car rest)))
846 (face-id (cdr (car rest)))
847 nil nil nil nil nil))
848 (face-fill-in (car (car rest)) global frame))
e09c52a8
RS
849 (make-face-x-resource-internal (cdr (car rest)) frame t)
850 (setq rest (cdr rest)))
e69e8fad
RS
851 (if (null visibility-spec)
852 (make-frame-visible frame)
853 (modify-frame-parameters frame (list visibility-spec)))
19ae9866 854 frame)))
e09c52a8 855
9d52c8d3
RS
856;; Update a frame's faces when we change its default font.
857(defun frame-update-faces (frame)
858 (let* ((faces global-face-data)
859 (rest faces))
860 (while rest
861 (let* ((face (car (car rest)))
862 (font (face-font face t)))
863 (if (listp font)
864 (let ((bold (memq 'bold font))
865 (italic (memq 'italic font)))
99fb9482
KH
866 ;; Ignore any previous (string-valued) font, it might not even
867 ;; be the right size anymore.
868 (set-face-font face nil frame)
9d52c8d3
RS
869 (cond ((and bold italic)
870 (make-face-bold-italic face frame t))
871 (bold
872 (make-face-bold face frame t))
873 (italic
874 (make-face-italic face frame t)))))
875 (setq rest (cdr rest)))
876 frame)))
877
19ae9866
RS
878;; Fill in the face FACE from frame-independent face data DATA.
879;; DATA should be the non-frame-specific ("global") face vector
880;; for the face. FACE should be a face name or face object.
881;; FRAME is the frame to act on; it must be an actual frame, not nil or t.
882(defun face-fill-in (face data frame)
883 (condition-case nil
884 (let ((foreground (face-foreground data))
885 (background (face-background data))
886 (font (face-font data)))
887 (set-face-underline-p face (face-underline-p data) frame)
888 (if foreground
889 (face-try-color-list 'set-face-foreground
890 face foreground frame))
891 (if background
892 (face-try-color-list 'set-face-background
893 face background frame))
894 (if (listp font)
895 (let ((bold (memq 'bold font))
896 (italic (memq 'italic font)))
897 (cond ((and bold italic)
898 (make-face-bold-italic face frame))
899 (bold
900 (make-face-bold face frame))
901 (italic
902 (make-face-italic face frame))))
903 (if font
904 (set-face-font face font frame))))
905 (error nil)))
e09c52a8 906
19ae9866
RS
907;; Use FUNCTION to store a color in FACE on FRAME.
908;; COLORS is either a single color or a list of colors.
909;; If it is a list, try the colors one by one until one of them
910;; succeeds. We signal an error only if all the colors failed.
911;; t as COLORS or as an element of COLORS means to invert the face.
912;; That can't fail, so any subsequent elements after the t are ignored.
913(defun face-try-color-list (function face colors frame)
914 (if (stringp colors)
6a588f9a
RS
915 (if (or (and (not (x-display-color-p)) (not (string= colors "gray")))
916 (= (x-display-planes) 1))
917 nil
918 (funcall function face colors frame))
19ae9866
RS
919 (if (eq colors t)
920 (invert-face face frame)
921 (let (done)
922 (while (and colors (not done))
6a588f9a
RS
923 (if (and (stringp (car colors))
924 (or (and (not (x-display-color-p))
925 (not (string= (car colors) "gray")))
926 (= (x-display-planes) 1)))
927 nil
928 (if (cdr colors)
929 ;; If there are more colors to try, catch errors
930 ;; and set `done' if we succeed.
931 (condition-case nil
932 (progn
566b0ad2
RS
933 (cond ((eq (car colors) t)
934 (invert-face face frame))
935 ((eq (car colors) 'underline)
936 (set-face-underline-p face t frame))
937 (t
938 (funcall function face (car colors) frame)))
6a588f9a
RS
939 (setq done t))
940 (error nil))
941 ;; If this is the last color, let the error get out if it fails.
942 ;; If it succeeds, we will exit anyway after this iteration.
566b0ad2
RS
943 (cond ((eq (car colors) t)
944 (invert-face face frame))
945 ((eq (car colors) 'underline)
946 (set-face-underline-p face t frame))
947 (t
948 (funcall function face (car colors) frame)))))
19ae9866 949 (setq colors (cdr colors)))))))
e09c52a8
RS
950
951;; If we are already using x-window frames, initialize faces for them.
952(if (eq (framep (selected-frame)) 'x)
953 (face-initialize))
465fceed 954
f0138172
JB
955(provide 'faces)
956
465fceed 957;;; faces.el ends here