(main): Call syms_of_term.
[emacs.git] / lisp / faces.el
blob891a37c24aab9f9bf9187d7f36950711b9da5011
1 ;;; faces.el --- Lisp interface to the c "face" structure
3 ;; Copyright (C) 1992, 1993, 1994 Free Software Foundation, Inc.
5 ;; This file is part of GNU Emacs.
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.
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.
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.
21 ;;; Commentary:
23 ;; Mostly derived from Lucid.
25 ;;; Code:
28 ;;;; Functions for manipulating face vectors.
30 ;;; A face vector is a vector of the form:
31 ;;; [face NAME ID FONT FOREGROUND BACKGROUND BACKGROUND-PIXMAP UNDERLINE]
33 ;;; Type checkers.
34 (defsubst internal-facep (x)
35 (and (vectorp x) (= (length x) 8) (eq (aref x 0) 'face)))
37 (defmacro internal-check-face (face)
38 (` (while (not (internal-facep (, face)))
39 (setq (, face) (signal 'wrong-type-argument (list 'internal-facep (, face)))))))
41 ;;; Accessors.
42 (defsubst face-name (face)
43 "Return the name of face FACE."
44 (aref (internal-get-face face) 1))
46 (defsubst face-id (face)
47 "Return the internal ID number of face FACE."
48 (aref (internal-get-face face) 2))
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))
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))
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))
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))
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))
87 ;;; Mutators.
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))
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))
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))
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.
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.
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))
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))
133 ;;;; Associating face names (symbols) with their face vectors.
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.")
140 (defun face-list ()
141 "Returns a list of all defined face names."
142 (mapcar 'car global-face-data))
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))
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)))
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))))
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)))
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))))
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)))
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)))))
245 (setq face (internal-get-face (face-name face) frame))
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.
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.
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))))
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))
294 face)
296 (defun copy-face (old-face new-face &optional frame new-frame)
297 "Define a face just like OLD-FACE, with name NEW-FACE.
298 If NEW-FACE already exists as a face, it is modified to be like OLD-FACE.
299 If it doesn't already exist, it is created.
301 If the optional argument FRAME is given as a frame,
302 NEW-FACE is changed on FRAME only.
303 If FRAME is t, the frame-independent default specification for OLD-FACE
304 is copied to NEW-FACE.
305 If FRAME is nil, copying is done for the frame-independent defaults
306 and for each existing frame.
307 If the optional fourth argument NEW-FRAME is given,
308 copy the information from face OLD-FACE on frame FRAME
309 to NEW-FACE on frame NEW-FRAME."
310 (or new-frame (setq new-frame frame))
311 (let ((inhibit-quit t))
312 (if (null frame)
313 (let ((frames (frame-list)))
314 (while frames
315 (copy-face old-face new-face (car frames))
316 (setq frames (cdr frames)))
317 (copy-face old-face new-face t))
318 (setq old-face (internal-get-face old-face frame))
319 (setq new-face (or (internal-find-face new-face new-frame)
320 (make-face new-face)))
321 (set-face-font new-face (face-font old-face frame) new-frame)
322 (set-face-foreground new-face (face-foreground old-face frame) new-frame)
323 (set-face-background new-face (face-background old-face frame) new-frame)
324 ;;; (set-face-background-pixmap
325 ;;; new-face (face-background-pixmap old-face frame) new-frame)
326 (set-face-underline-p new-face (face-underline-p old-face frame)
327 new-frame))
328 new-face))
330 (defun face-equal (face1 face2 &optional frame)
331 "True if the faces FACE1 and FACE2 display in the same way."
332 (setq face1 (internal-get-face face1 frame)
333 face2 (internal-get-face face2 frame))
334 (and (equal (face-foreground face1 frame) (face-foreground face2 frame))
335 (equal (face-background face1 frame) (face-background face2 frame))
336 (equal (face-font face1 frame) (face-font face2 frame))
337 ;; (equal (face-background-pixmap face1 frame)
338 ;; (face-background-pixmap face2 frame))
341 (defun face-differs-from-default-p (face &optional frame)
342 "True if face FACE displays differently from the default face, on FRAME.
343 A face is considered to be ``the same'' as the default face if it is
344 actually specified in the same way (equivalent fonts, etc) or if it is
345 fully unspecified, and thus inherits the attributes of any face it
346 is displayed on top of."
347 (let ((default (internal-get-face 'default frame)))
348 (setq face (internal-get-face face frame))
349 (not (and (or (equal (face-foreground default frame)
350 (face-foreground face frame))
351 (null (face-foreground face frame)))
352 (or (equal (face-background default frame)
353 (face-background face frame))
354 (null (face-background face frame)))
355 (or (equal (face-font default frame) (face-font face frame))
356 (null (face-font face frame)))
357 ;;; (or (equal (face-background-pixmap default frame)
358 ;;; (face-background-pixmap face frame))
359 ;;; (null (face-background-pixmap face frame)))
360 (equal (face-underline-p default frame)
361 (face-underline-p face frame))
362 ))))
365 (defun invert-face (face &optional frame)
366 "Swap the foreground and background colors of face FACE.
367 If the face doesn't specify both foreground and background, then
368 set its foreground and background to the default background and foreground."
369 (interactive (list (read-face-name "Invert face: ")))
370 (setq face (internal-get-face face frame))
371 (let ((fg (face-foreground face frame))
372 (bg (face-background face frame)))
373 (if (or fg bg)
374 (progn
375 (set-face-foreground face bg frame)
376 (set-face-background face fg frame))
377 (set-face-foreground face (or (face-background 'default frame)
378 (cdr (assq 'background-color (frame-parameters frame))))
379 frame)
380 (set-face-background face (or (face-foreground 'default frame)
381 (cdr (assq 'foreground-color (frame-parameters frame))))
382 frame)))
383 face)
386 (defun internal-try-face-font (face font &optional frame)
387 "Like set-face-font, but returns nil on failure instead of an error."
388 (condition-case ()
389 (set-face-font face font frame)
390 (error nil)))
392 ;; Manipulating font names.
394 (defconst x-font-regexp nil)
395 (defconst x-font-regexp-head nil)
396 (defconst x-font-regexp-weight nil)
397 (defconst x-font-regexp-slant nil)
399 ;;; Regexps matching font names in "Host Portable Character Representation."
401 (let ((- "[-?]")
402 (foundry "[^-]+")
403 (family "[^-]+")
404 (weight "\\(bold\\|demibold\\|medium\\)") ; 1
405 ; (weight\? "\\(\\*\\|bold\\|demibold\\|medium\\|\\)") ; 1
406 (weight\? "\\([^-]*\\)") ; 1
407 (slant "\\([ior]\\)") ; 2
408 ; (slant\? "\\([ior?*]?\\)") ; 2
409 (slant\? "\\([^-]?\\)") ; 2
410 ; (swidth "\\(\\*\\|normal\\|semicondensed\\|\\)") ; 3
411 (swidth "\\([^-]*\\)") ; 3
412 ; (adstyle "\\(\\*\\|sans\\|\\)") ; 4
413 (adstyle "[^-]*") ; 4
414 (pixelsize "[0-9]+")
415 (pointsize "[0-9][0-9]+")
416 (resx "[0-9][0-9]+")
417 (resy "[0-9][0-9]+")
418 (spacing "[cmp?*]")
419 (avgwidth "[0-9]+")
420 (registry "[^-]+")
421 (encoding "[^-]+")
423 (setq x-font-regexp
424 (concat "\\`\\*?[-?*]"
425 foundry - family - weight\? - slant\? - swidth - adstyle -
426 pixelsize - pointsize - resx - resy - spacing - registry -
427 encoding "[-?*]\\*?\\'"
429 (setq x-font-regexp-head
430 (concat "\\`[-?*]" foundry - family - weight\? - slant\?
431 "\\([-*?]\\|\\'\\)"))
432 (setq x-font-regexp-slant (concat - slant -))
433 (setq x-font-regexp-weight (concat - weight -))
434 nil)
436 (defun x-resolve-font-name (pattern &optional face frame)
437 "Return a font name matching PATTERN.
438 All wildcards in PATTERN become substantiated.
439 If PATTERN is nil, return the name of the frame's base font, which never
440 contains wildcards.
441 Given optional arguments FACE and FRAME, try to return a font which is
442 also the same size as FACE on FRAME."
443 (or (symbolp face)
444 (setq face (face-name face)))
445 (and (eq frame t)
446 (setq frame nil))
447 (if pattern
448 ;; Note that x-list-fonts has code to handle a face with nil as its font.
449 (let ((fonts (x-list-fonts pattern face frame)))
450 (or fonts
451 (if face
452 (error "No fonts matching pattern are the same size as `%s'"
453 face)
454 (error "No fonts match `%s'" pattern)))
455 (car fonts))
456 (cdr (assq 'font (frame-parameters (selected-frame))))))
458 (defun x-frob-font-weight (font which)
459 (if (or (string-match x-font-regexp font)
460 (string-match x-font-regexp-head font)
461 (string-match x-font-regexp-weight font))
462 (concat (substring font 0 (match-beginning 1)) which
463 (substring font (match-end 1)))
464 nil))
466 (defun x-frob-font-slant (font which)
467 (cond ((or (string-match x-font-regexp font)
468 (string-match x-font-regexp-head font))
469 (concat (substring font 0 (match-beginning 2)) which
470 (substring font (match-end 2))))
471 ((string-match x-font-regexp-slant font)
472 (concat (substring font 0 (match-beginning 1)) which
473 (substring font (match-end 1))))
474 (t nil)))
477 (defun x-make-font-bold (font)
478 "Given an X font specification, make a bold version of it.
479 If that can't be done, return nil."
480 (x-frob-font-weight font "bold"))
482 (defun x-make-font-demibold (font)
483 "Given an X font specification, make a demibold version of it.
484 If that can't be done, return nil."
485 (x-frob-font-weight font "demibold"))
487 (defun x-make-font-unbold (font)
488 "Given an X font specification, make a non-bold version of it.
489 If that can't be done, return nil."
490 (x-frob-font-weight font "medium"))
492 (defun x-make-font-italic (font)
493 "Given an X font specification, make an italic version of it.
494 If that can't be done, return nil."
495 (x-frob-font-slant font "i"))
497 (defun x-make-font-oblique (font) ; you say tomayto...
498 "Given an X font specification, make an oblique version of it.
499 If that can't be done, return nil."
500 (x-frob-font-slant font "o"))
502 (defun x-make-font-unitalic (font)
503 "Given an X font specification, make a non-italic version of it.
504 If that can't be done, return nil."
505 (x-frob-font-slant font "r"))
507 ;;; non-X-specific interface
509 (defun make-face-bold (face &optional frame noerror)
510 "Make the font of the given face be bold, if possible.
511 If NOERROR is non-nil, return nil on failure."
512 (interactive (list (read-face-name "Make which face bold: ")))
513 (if (and (eq frame t) (listp (face-font face t)))
514 (set-face-font face (if (memq 'italic (face-font face t))
515 '(bold italic) '(bold))
517 (let ((ofont (face-font face frame))
518 font f2)
519 (if (null frame)
520 (let ((frames (frame-list)))
521 ;; Make this face bold in global-face-data.
522 (make-face-bold face t noerror)
523 ;; Make this face bold in each frame.
524 (while frames
525 (make-face-bold face (car frames) noerror)
526 (setq frames (cdr frames))))
527 (setq face (internal-get-face face frame))
528 (setq font (or (face-font face frame)
529 (face-font face t)))
530 (if (listp font)
531 (setq font nil))
532 (setq font (or font
533 (face-font 'default frame)
534 (cdr (assq 'font (frame-parameters frame)))))
535 (make-face-bold-internal face frame))
536 (or (not (equal ofont (face-font face)))
537 (and (not noerror)
538 (error "No bold version of %S" font))))))
540 (defun make-face-bold-internal (face frame)
541 (or (and (setq f2 (x-make-font-bold font))
542 (internal-try-face-font face f2 frame))
543 (and (setq f2 (x-make-font-demibold font))
544 (internal-try-face-font face f2 frame))))
546 (defun make-face-italic (face &optional frame noerror)
547 "Make the font of the given face be italic, if possible.
548 If NOERROR is non-nil, return nil on failure."
549 (interactive (list (read-face-name "Make which face italic: ")))
550 (if (and (eq frame t) (listp (face-font face t)))
551 (set-face-font face (if (memq 'bold (face-font face t))
552 '(bold italic) '(italic))
554 (let ((ofont (face-font face frame))
555 font f2)
556 (if (null frame)
557 (let ((frames (frame-list)))
558 ;; Make this face italic in global-face-data.
559 (make-face-italic face t noerror)
560 ;; Make this face italic in each frame.
561 (while frames
562 (make-face-italic face (car frames) noerror)
563 (setq frames (cdr frames))))
564 (setq face (internal-get-face face frame))
565 (setq font (or (face-font face frame)
566 (face-font face t)))
567 (if (listp font)
568 (setq font nil))
569 (setq font (or font
570 (face-font 'default frame)
571 (cdr (assq 'font (frame-parameters frame)))))
572 (make-face-italic-internal face frame))
573 (or (not (equal ofont (face-font face)))
574 (and (not noerror)
575 (error "No italic version of %S" font))))))
577 (defun make-face-italic-internal (face frame)
578 (or (and (setq f2 (x-make-font-italic font))
579 (internal-try-face-font face f2 frame))
580 (and (setq f2 (x-make-font-oblique font))
581 (internal-try-face-font face f2 frame))))
583 (defun make-face-bold-italic (face &optional frame noerror)
584 "Make the font of the given face be bold and italic, if possible.
585 If NOERROR is non-nil, return nil on failure."
586 (interactive (list (read-face-name "Make which face bold-italic: ")))
587 (if (and (eq frame t) (listp (face-font face t)))
588 (set-face-font face '(bold italic) t)
589 (let ((ofont (face-font face frame))
590 font)
591 (if (null frame)
592 (let ((frames (frame-list)))
593 ;; Make this face bold-italic in global-face-data.
594 (make-face-bold-italic face t noerror)
595 ;; Make this face bold in each frame.
596 (while frames
597 (make-face-bold-italic face (car frames) noerror)
598 (setq frames (cdr frames))))
599 (setq face (internal-get-face face frame))
600 (setq font (or (face-font face frame)
601 (face-font face t)))
602 (if (listp font)
603 (setq font nil))
604 (setq font (or font
605 (face-font 'default frame)
606 (cdr (assq 'font (frame-parameters frame)))))
607 (make-face-bold-italic-internal face frame))
608 (or (not (equal ofont (face-font face)))
609 (and (not noerror)
610 (error "No bold italic version of %S" font))))))
612 (defun make-face-bold-italic-internal (face frame)
613 (let (f2 f3)
614 (or (and (setq f2 (x-make-font-italic font))
615 (not (equal font f2))
616 (setq f3 (x-make-font-bold f2))
617 (not (equal f2 f3))
618 (internal-try-face-font face f3 frame))
619 (and (setq f2 (x-make-font-oblique font))
620 (not (equal font f2))
621 (setq f3 (x-make-font-bold f2))
622 (not (equal f2 f3))
623 (internal-try-face-font face f3 frame))
624 (and (setq f2 (x-make-font-italic font))
625 (not (equal font f2))
626 (setq f3 (x-make-font-demibold f2))
627 (not (equal f2 f3))
628 (internal-try-face-font face f3 frame))
629 (and (setq f2 (x-make-font-oblique font))
630 (not (equal font f2))
631 (setq f3 (x-make-font-demibold f2))
632 (not (equal f2 f3))
633 (internal-try-face-font face f3 frame)))))
635 (defun make-face-unbold (face &optional frame noerror)
636 "Make the font of the given face be non-bold, if possible.
637 If NOERROR is non-nil, return nil on failure."
638 (interactive (list (read-face-name "Make which face non-bold: ")))
639 (if (and (eq frame t) (listp (face-font face t)))
640 (set-face-font face (if (memq 'italic (face-font face t))
641 '(italic) nil)
643 (let ((ofont (face-font face frame))
644 font font1)
645 (if (null frame)
646 (let ((frames (frame-list)))
647 ;; Make this face unbold in global-face-data.
648 (make-face-unbold face t noerror)
649 ;; Make this face unbold in each frame.
650 (while frames
651 (make-face-unbold face (car frames) noerror)
652 (setq frames (cdr frames))))
653 (setq face (internal-get-face face frame))
654 (setq font1 (or (face-font face frame)
655 (face-font face t)))
656 (if (listp font1)
657 (setq font1 nil))
658 (setq font1 (or font1
659 (face-font 'default frame)
660 (cdr (assq 'font (frame-parameters frame)))))
661 (setq font (x-make-font-unbold font1))
662 (if font (internal-try-face-font face font frame)))
663 (or (not (equal ofont (face-font face)))
664 (and (not noerror)
665 (error "No unbold version of %S" font1))))))
667 (defun make-face-unitalic (face &optional frame noerror)
668 "Make the font of the given face be non-italic, if possible.
669 If NOERROR is non-nil, return nil on failure."
670 (interactive (list (read-face-name "Make which face non-italic: ")))
671 (if (and (eq frame t) (listp (face-font face t)))
672 (set-face-font face (if (memq 'bold (face-font face t))
673 '(bold) nil)
675 (let ((ofont (face-font face frame))
676 font font1)
677 (if (null frame)
678 (let ((frames (frame-list)))
679 ;; Make this face unitalic in global-face-data.
680 (make-face-unitalic face t noerror)
681 ;; Make this face unitalic in each frame.
682 (while frames
683 (make-face-unitalic face (car frames) noerror)
684 (setq frames (cdr frames))))
685 (setq face (internal-get-face face frame))
686 (setq font1 (or (face-font face frame)
687 (face-font face t)))
688 (if (listp font1)
689 (setq font1 nil))
690 (setq font1 (or font1
691 (face-font 'default frame)
692 (cdr (assq 'font (frame-parameters frame)))))
693 (setq font (x-make-font-unitalic font1))
694 (if font (internal-try-face-font face font frame)))
695 (or (not (equal ofont (face-font face)))
696 (and (not noerror)
697 (error "No unitalic version of %S" font1))))))
699 (defvar list-faces-sample-text
700 "abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ"
701 "*Text string to display as the sample text for `list-faces-display'.")
703 ;; The name list-faces would be more consistent, but let's avoid a conflict
704 ;; with Lucid, which uses that name differently.
705 (defun list-faces-display ()
706 "List all faces, using the same sample text in each.
707 The sample text is a string that comes from the variable
708 `list-faces-sample-text'.
710 It is possible to give a particular face name different appearances in
711 different frames. This command shows the appearance in the
712 selected frame."
713 (interactive)
714 (let ((faces (sort (face-list) (function string-lessp)))
715 (face nil)
716 (frame (selected-frame))
717 disp-frame window)
718 (with-output-to-temp-buffer "*Faces*"
719 (save-excursion
720 (set-buffer standard-output)
721 (setq truncate-lines t)
722 (while faces
723 (setq face (car faces))
724 (setq faces (cdr faces))
725 (insert (format "%25s " (symbol-name face)))
726 (let ((beg (point)))
727 (insert list-faces-sample-text)
728 (insert "\n")
729 (put-text-property beg (1- (point)) 'face face)))
730 (goto-char (point-min))))
731 ;; If the *Faces* buffer appears in a different frame,
732 ;; copy all the face definitions from FRAME,
733 ;; so that the display will reflect the frame that was selected.
734 (setq window (get-buffer-window (get-buffer "*Faces*") t))
735 (setq disp-frame (if window (window-frame window)
736 (car (frame-list))))
737 (or (eq frame disp-frame)
738 (let ((faces (face-list)))
739 (while faces
740 (copy-face (car faces) (car faces) frame disp-frame)
741 (setq faces (cdr faces)))))))
743 ;;; Make the standard faces.
744 ;;; The C code knows the default and modeline faces as faces 0 and 1,
745 ;;; so they must be the first two faces made.
746 (defun face-initialize ()
747 (make-face 'default)
748 (make-face 'modeline)
749 (make-face 'highlight)
751 ;; These aren't really special in any way, but they're nice to have around.
753 (make-face 'bold)
754 (make-face 'italic)
755 (make-face 'bold-italic)
756 (make-face 'region)
757 (make-face 'secondary-selection)
758 (make-face 'underline)
760 (setq region-face (face-id 'region))
762 ;; Specify the global properties of these faces
763 ;; so they will come out right on new frames.
765 (make-face-bold 'bold t)
766 (make-face-italic 'italic t)
767 (make-face-bold-italic 'bold-italic t)
769 (set-face-background 'highlight '("darkseagreen2" "green" t) t)
770 (set-face-background 'region '("gray" t) t)
771 (set-face-background 'secondary-selection '("paleturquoise" "green" t) t)
772 (set-face-background 'modeline '(t) t)
773 (set-face-underline-p 'underline t t)
775 ;; Set up the faces of all existing X Window frames
776 ;; from those global properties, unless already set in a given frame.
778 (let ((frames (frame-list)))
779 (while frames
780 (if (eq (framep (car frames)) 'x)
781 (let ((frame (car frames))
782 (rest global-face-data))
783 (while rest
784 (let ((face (car (car rest))))
785 (or (face-differs-from-default-p face)
786 (face-fill-in face (cdr (car rest)) frame)))
787 (setq rest (cdr rest)))))
788 (setq frames (cdr frames)))))
791 ;; Like x-create-frame but also set up the faces.
793 (defun x-create-frame-with-faces (&optional parameters)
794 (if (null global-face-data)
795 (x-create-frame parameters)
796 (let* ((frame (x-create-frame parameters))
797 (faces (copy-alist global-face-data))
798 (rest faces))
799 (set-frame-face-alist frame faces)
801 (if (cdr (or (assq 'reverse parameters)
802 (assq 'reverse default-frame-alist)
803 (cons nil
804 (member (x-get-resource "reverseVideo" "ReverseVideo")
805 '("on" "true")))))
806 (let ((params (frame-parameters frame)))
807 (modify-frame-parameters
808 frame
809 (list (cons 'foreground-color (cdr (assq 'background-color params)))
810 (cons 'background-color (cdr (assq 'foreground-color params)))
811 (cons 'mouse-color (cdr (assq 'background-color params)))
812 (cons 'cursor-color (cdr (assq 'background-color params)))
813 (cons 'border-color (cdr (assq 'background-color params)))))))
815 ;; Copy the vectors that represent the faces.
816 ;; Also fill them in from X resources.
817 (while rest
818 (let ((global (cdr (car rest))))
819 (setcdr (car rest) (vector 'face
820 (face-name (cdr (car rest)))
821 (face-id (cdr (car rest)))
822 nil nil nil nil nil))
823 (face-fill-in (car (car rest)) global frame))
824 (make-face-x-resource-internal (cdr (car rest)) frame t)
825 (setq rest (cdr rest)))
826 frame)))
828 ;; Fill in the face FACE from frame-independent face data DATA.
829 ;; DATA should be the non-frame-specific ("global") face vector
830 ;; for the face. FACE should be a face name or face object.
831 ;; FRAME is the frame to act on; it must be an actual frame, not nil or t.
832 (defun face-fill-in (face data frame)
833 (condition-case nil
834 (let ((foreground (face-foreground data))
835 (background (face-background data))
836 (font (face-font data)))
837 (set-face-underline-p face (face-underline-p data) frame)
838 (if foreground
839 (face-try-color-list 'set-face-foreground
840 face foreground frame))
841 (if background
842 (face-try-color-list 'set-face-background
843 face background frame))
844 (if (listp font)
845 (let ((bold (memq 'bold font))
846 (italic (memq 'italic font)))
847 (cond ((and bold italic)
848 (make-face-bold-italic face frame))
849 (bold
850 (make-face-bold face frame))
851 (italic
852 (make-face-italic face frame))))
853 (if font
854 (set-face-font face font frame))))
855 (error nil)))
857 ;; Use FUNCTION to store a color in FACE on FRAME.
858 ;; COLORS is either a single color or a list of colors.
859 ;; If it is a list, try the colors one by one until one of them
860 ;; succeeds. We signal an error only if all the colors failed.
861 ;; t as COLORS or as an element of COLORS means to invert the face.
862 ;; That can't fail, so any subsequent elements after the t are ignored.
863 (defun face-try-color-list (function face colors frame)
864 (if (stringp colors)
865 (if (or (and (not (x-display-color-p)) (not (string= colors "gray")))
866 (= (x-display-planes) 1))
868 (funcall function face colors frame))
869 (if (eq colors t)
870 (invert-face face frame)
871 (let (done)
872 (while (and colors (not done))
873 (if (and (stringp (car colors))
874 (or (and (not (x-display-color-p))
875 (not (string= (car colors) "gray")))
876 (= (x-display-planes) 1)))
878 (if (cdr colors)
879 ;; If there are more colors to try, catch errors
880 ;; and set `done' if we succeed.
881 (condition-case nil
882 (progn
883 (if (eq (car colors) t)
884 (invert-face face frame)
885 (funcall function face (car colors) frame))
886 (setq done t))
887 (error nil))
888 ;; If this is the last color, let the error get out if it fails.
889 ;; If it succeeds, we will exit anyway after this iteration.
890 (if (eq (car colors) t)
891 (invert-face face frame)
892 (funcall function face (car colors) frame))))
893 (setq colors (cdr colors)))))))
895 ;; If we are already using x-window frames, initialize faces for them.
896 (if (eq (framep (selected-frame)) 'x)
897 (face-initialize))
899 (provide 'faces)
901 ;;; faces.el ends here