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