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