mail-mailer-swallows-blank-line: Handle full range of legal header names as
[emacs.git] / lisp / faces.el
blob75b12fb03ec6b08e51ebf842193bf579e93fa3e0
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:
27 (eval-when-compile
28 ;; These used to be defsubsts, now they're subrs. Avoid losing if we're
29 ;; being compiled with an old Emacs that still has defsubrs in it.
30 (put 'face-name 'byte-optimizer nil)
31 (put 'face-id 'byte-optimizer nil)
32 (put 'face-font 'byte-optimizer nil)
33 (put 'face-foreground 'byte-optimizer nil)
34 (put 'face-background 'byte-optimizer nil)
35 (put 'face-stipple 'byte-optimizer nil)
36 (put 'face-underline-p 'byte-optimizer nil)
37 (put 'set-face-font 'byte-optimizer nil)
38 (put 'set-face-foreground 'byte-optimizer nil)
39 (put 'set-face-background 'byte-optimizer nil)
40 (put 'set-stipple 'byte-optimizer nil)
41 (put 'set-face-underline-p 'byte-optimizer nil))
43 ;;;; Functions for manipulating face vectors.
45 ;;; A face vector is a vector of the form:
46 ;;; [face NAME ID FONT FOREGROUND BACKGROUND STIPPLE UNDERLINE]
48 ;;; Type checkers.
49 (defsubst internal-facep (x)
50 (and (vectorp x) (= (length x) 8) (eq (aref x 0) 'face)))
52 (defun facep (x)
53 "Return t if X is a face name or an internal face vector."
54 (and (or (internal-facep x)
55 (and (symbolp x) (assq x global-face-data)))
56 t))
58 (defmacro internal-check-face (face)
59 (` (or (internal-facep (, face))
60 (signal 'wrong-type-argument (list 'internal-facep (, face))))))
62 ;;; Accessors.
63 (defun face-name (face)
64 "Return the name of face FACE."
65 (aref (internal-get-face face) 1))
67 (defun face-id (face)
68 "Return the internal ID number of face FACE."
69 (aref (internal-get-face face) 2))
71 (defun face-font (face &optional frame)
72 "Return the font name of face FACE, or nil if it is unspecified.
73 If the optional argument FRAME is given, report on face FACE in that frame.
74 If FRAME is t, report on the defaults for face FACE (for new frames).
75 The font default for a face is either nil, or a list
76 of the form (bold), (italic) or (bold italic).
77 If FRAME is omitted or nil, use the selected frame."
78 (aref (internal-get-face face frame) 3))
80 (defun face-foreground (face &optional frame)
81 "Return the foreground color name of face FACE, or nil if unspecified.
82 If the optional argument FRAME is given, report on face FACE in that frame.
83 If FRAME is t, report on the defaults for face FACE (for new frames).
84 If FRAME is omitted or nil, use the selected frame."
85 (aref (internal-get-face face frame) 4))
87 (defun face-background (face &optional frame)
88 "Return the background color name of face FACE, or nil if unspecified.
89 If the optional argument FRAME is given, report on face FACE in that frame.
90 If FRAME is t, report on the defaults for face FACE (for new frames).
91 If FRAME is omitted or nil, use the selected frame."
92 (aref (internal-get-face face frame) 5))
94 (defun face-stipple (face &optional frame)
95 "Return the stipple pixmap name of face FACE, or nil if unspecified.
96 If the optional argument FRAME is given, report on face FACE in that frame.
97 If FRAME is t, report on the defaults for face FACE (for new frames).
98 If FRAME is omitted or nil, use the selected frame."
99 (aref (internal-get-face face frame) 6))
101 (defalias 'face-background-pixmap 'face-stipple)
103 (defun face-underline-p (face &optional frame)
104 "Return t if face FACE is underlined.
105 If the optional argument FRAME is given, report on face FACE in that frame.
106 If FRAME is t, report on the defaults for face FACE (for new frames).
107 If FRAME is omitted or nil, use the selected frame."
108 (aref (internal-get-face face frame) 7))
111 ;;; Mutators.
113 (defun set-face-font (face font &optional frame)
114 "Change the font of face FACE to FONT (a string).
115 If the optional FRAME argument is provided, change only
116 in that frame; otherwise change each frame."
117 (interactive (internal-face-interactive "font"))
118 (if (stringp font) (setq font (x-resolve-font-name font 'default frame)))
119 (internal-set-face-1 face 'font font 3 frame))
121 (defun set-face-foreground (face color &optional frame)
122 "Change the foreground color of face FACE to COLOR (a string).
123 If the optional FRAME argument is provided, change only
124 in that frame; otherwise change each frame."
125 (interactive (internal-face-interactive "foreground"))
126 (internal-set-face-1 face 'foreground color 4 frame))
128 (defun set-face-background (face color &optional frame)
129 "Change the background color of face FACE to COLOR (a string).
130 If the optional FRAME argument is provided, change only
131 in that frame; otherwise change each frame."
132 (interactive (internal-face-interactive "background"))
133 ;; For a specific frame, use gray stipple instead of gray color
134 ;; if the display does not support a gray color.
135 (if (and frame (not (eq frame t))
136 (member color '("gray" "gray1" "gray3"))
137 (not (x-display-color-p frame))
138 (not (x-display-grayscale-p frame)))
139 (set-face-stipple face color frame)
140 (internal-set-face-1 face 'background color 5 frame)))
142 (defun set-face-stipple (face name &optional frame)
143 "Change the stipple pixmap of face FACE to PIXMAP.
144 PIXMAP should be a string, the name of a file of pixmap data.
145 The directories listed in the `x-bitmap-file-path' variable are searched.
147 Alternatively, PIXMAP may be a list of the form (WIDTH HEIGHT DATA)
148 where WIDTH and HEIGHT are the size in pixels,
149 and DATA is a string, containing the raw bits of the bitmap.
151 If the optional FRAME argument is provided, change only
152 in that frame; otherwise change each frame."
153 (interactive (internal-face-interactive "stipple"))
154 (internal-set-face-1 face 'background-pixmap name 6 frame))
156 (defalias 'set-face-background-pixmap 'set-face-stipple)
158 (defun set-face-underline-p (face underline-p &optional frame)
159 "Specify whether face FACE is underlined. (Yes if UNDERLINE-P is non-nil.)
160 If the optional FRAME argument is provided, change only
161 in that frame; otherwise change each frame."
162 (interactive (internal-face-interactive "underline-p" "underlined"))
163 (internal-set-face-1 face 'underline underline-p 7 frame))
165 (defun modify-face (face foreground background bold-p italic-p underline-p)
166 "Change the display attributes for face FACE.
167 FOREGROUND and BACKGROUND should be color strings. (Default color if nil.)
168 BOLD-P, ITALIC-P, and UNDERLINE-P specify whether the face should be set bold,
169 in italic, and underlined, respectively. (Yes if non-nil.)
170 If called interactively, prompts for a face and face attributes."
171 (interactive
172 (let* ((completion-ignore-case t)
173 (face (symbol-name (read-face-name "Face: ")))
174 (foreground (completing-read
175 (format "Face %s set foreground (default %s): " face
176 (downcase (or (face-foreground (intern face))
177 "foreground")))
178 (mapcar 'list (x-defined-colors))))
179 (background (completing-read
180 (format "Face %s set background (default %s): " face
181 (downcase (or (face-background (intern face))
182 "background")))
183 (mapcar 'list (x-defined-colors))))
184 (bold-p (y-or-n-p (concat "Face " face ": set bold ")))
185 (italic-p (y-or-n-p (concat "Face " face ": set italic ")))
186 (underline-p (y-or-n-p (concat "Face " face ": set underline "))))
187 (if (string-equal background "") (setq background nil))
188 (if (string-equal foreground "") (setq foreground nil))
189 (message "Face %s: %s" face
190 (mapconcat 'identity
191 (delq nil
192 (list (and foreground (concat (downcase foreground) " foreground"))
193 (and background (concat (downcase background) " background"))
194 (and bold-p "bold") (and italic-p "italic")
195 (and underline-p "underline"))) ", "))
196 (list (intern face) foreground background bold-p italic-p underline-p)))
197 (condition-case nil (set-face-foreground face foreground) (error nil))
198 (condition-case nil (set-face-background face background) (error nil))
199 (funcall (if bold-p 'make-face-bold 'make-face-unbold) face nil t)
200 (funcall (if italic-p 'make-face-italic 'make-face-unitalic) face nil t)
201 (set-face-underline-p face underline-p)
202 (and (interactive-p) (redraw-display)))
204 ;;;; Associating face names (symbols) with their face vectors.
206 (defvar global-face-data nil
207 "Internal data for face support functions. Not for external use.
208 This is an alist associating face names with the default values for
209 their parameters. Newly created frames get their data from here.")
211 (defun face-list ()
212 "Returns a list of all defined face names."
213 (mapcar 'car global-face-data))
215 (defun internal-find-face (name &optional frame)
216 "Retrieve the face named NAME. Return nil if there is no such face.
217 If the optional argument FRAME is given, this gets the face NAME for
218 that frame; otherwise, it uses the selected frame.
219 If FRAME is the symbol t, then the global, non-frame face is returned.
220 If NAME is already a face, it is simply returned."
221 (if (and (eq frame t) (not (symbolp name)))
222 (setq name (face-name name)))
223 (if (symbolp name)
224 (cdr (assq name
225 (if (eq frame t)
226 global-face-data
227 (frame-face-alist (or frame (selected-frame))))))
228 (internal-check-face name)
229 name))
231 (defun internal-get-face (name &optional frame)
232 "Retrieve the face named NAME; error if there is none.
233 If the optional argument FRAME is given, this gets the face NAME for
234 that frame; otherwise, it uses the selected frame.
235 If FRAME is the symbol t, then the global, non-frame face is returned.
236 If NAME is already a face, it is simply returned."
237 (or (internal-find-face name frame)
238 (internal-check-face name)))
241 (defun internal-set-face-1 (face name value index frame)
242 (let ((inhibit-quit t))
243 (if (null frame)
244 (let ((frames (frame-list)))
245 (while frames
246 (internal-set-face-1 (face-name face) name value index (car frames))
247 (setq frames (cdr frames)))
248 (aset (internal-get-face (if (symbolp face) face (face-name face)) t)
249 index value)
250 value)
251 (or (eq frame t)
252 (set-face-attribute-internal (face-id face) name value frame))
253 (aset (internal-get-face face frame) index value))))
256 (defun read-face-name (prompt)
257 (let (face)
258 (while (= (length face) 0)
259 (setq face (completing-read prompt
260 (mapcar '(lambda (x) (list (symbol-name x)))
261 (face-list))
262 nil t)))
263 (intern face)))
265 (defun internal-face-interactive (what &optional bool)
266 (let* ((fn (intern (concat "face-" what)))
267 (prompt (concat "Set " what " of face"))
268 (face (read-face-name (concat prompt ": ")))
269 (default (if (fboundp fn)
270 (or (funcall fn face (selected-frame))
271 (funcall fn 'default (selected-frame)))))
272 (value (if bool
273 (y-or-n-p (concat "Should face " (symbol-name face)
274 " be " bool "? "))
275 (read-string (concat prompt " " (symbol-name face) " to: ")
276 default))))
277 (list face (if (equal value "") nil value))))
281 (defun make-face (name)
282 "Define a new FACE on all frames.
283 You can modify the font, color, etc of this face with the set-face- functions.
284 If the face already exists, it is unmodified."
285 (interactive "SMake face: ")
286 (or (internal-find-face name)
287 (let ((face (make-vector 8 nil)))
288 (aset face 0 'face)
289 (aset face 1 name)
290 (let* ((frames (frame-list))
291 (inhibit-quit t)
292 (id (internal-next-face-id)))
293 (make-face-internal id)
294 (aset face 2 id)
295 (while frames
296 (set-frame-face-alist (car frames)
297 (cons (cons name (copy-sequence face))
298 (frame-face-alist (car frames))))
299 (setq frames (cdr frames)))
300 (setq global-face-data (cons (cons name face) global-face-data)))
301 ;; when making a face after frames already exist
302 (if (eq window-system 'x)
303 (make-face-x-resource-internal face))
304 ;; add to menu
305 (if (fboundp 'facemenu-add-new-face)
306 (facemenu-add-new-face name))
307 face))
308 name)
310 ;; Fill in a face by default based on X resources, for all existing frames.
311 ;; This has to be done when a new face is made.
312 (defun make-face-x-resource-internal (face &optional frame set-anyway)
313 (cond ((null frame)
314 (let ((frames (frame-list)))
315 (while frames
316 (if (eq (framep (car frames)) 'x)
317 (make-face-x-resource-internal (face-name face)
318 (car frames) set-anyway))
319 (setq frames (cdr frames)))))
321 (setq face (internal-get-face (face-name face) frame))
323 ;; These are things like "attributeForeground" instead of simply
324 ;; "foreground" because people tend to do things like "*foreground",
325 ;; which would cause all faces to be fully qualified, making faces
326 ;; inherit attributes in a non-useful way. So we've made them slightly
327 ;; less obvious to specify in order to make them work correctly in
328 ;; more random environments.
330 ;; I think these should be called "face.faceForeground" instead of
331 ;; "face.attributeForeground", but they're the way they are for
332 ;; hysterical reasons.
334 (let* ((name (symbol-name (face-name face)))
335 (fn (or (x-get-resource (concat name ".attributeFont")
336 "Face.AttributeFont")
337 (and set-anyway (face-font face))))
338 (fg (or (x-get-resource (concat name ".attributeForeground")
339 "Face.AttributeForeground")
340 (and set-anyway (face-foreground face))))
341 (bg (or (x-get-resource (concat name ".attributeBackground")
342 "Face.AttributeBackground")
343 (and set-anyway (face-background face))))
344 (bgp (or (x-get-resource (concat name ".attributeStipple")
345 "Face.AttributeStipple")
346 (x-get-resource (concat name ".attributeBackgroundPixmap")
347 "Face.AttributeBackgroundPixmap")
348 (and set-anyway (face-stipple face))))
349 (ulp (let ((resource (x-get-resource
350 (concat name ".attributeUnderline")
351 "Face.AttributeUnderline")))
352 (if resource
353 (member (downcase resource) '("on" "true"))
354 (and set-anyway (face-underline-p face)))))
356 (if fn
357 (condition-case ()
358 (set-face-font face fn frame)
359 (error (message "font `%s' not found for face `%s'" fn name))))
360 (if fg
361 (condition-case ()
362 (set-face-foreground face fg frame)
363 (error (message "color `%s' not allocated for face `%s'" fg name))))
364 (if bg
365 (condition-case ()
366 (set-face-background face bg frame)
367 (error (message "color `%s' not allocated for face `%s'" bg name))))
368 (if bgp
369 (condition-case ()
370 (set-face-stipple face bgp frame)
371 (error (message "pixmap `%s' not found for face `%s'" bgp name))))
372 (if (or ulp set-anyway)
373 (set-face-underline-p face ulp frame))
375 face)
377 (defun copy-face (old-face new-face &optional frame new-frame)
378 "Define a face just like OLD-FACE, with name NEW-FACE.
379 If NEW-FACE already exists as a face, it is modified to be like OLD-FACE.
380 If it doesn't already exist, it is created.
382 If the optional argument FRAME is given as a frame,
383 NEW-FACE is changed on FRAME only.
384 If FRAME is t, the frame-independent default specification for OLD-FACE
385 is copied to NEW-FACE.
386 If FRAME is nil, copying is done for the frame-independent defaults
387 and for each existing frame.
388 If the optional fourth argument NEW-FRAME is given,
389 copy the information from face OLD-FACE on frame FRAME
390 to NEW-FACE on frame NEW-FRAME."
391 (or new-frame (setq new-frame frame))
392 (let ((inhibit-quit t))
393 (if (null frame)
394 (let ((frames (frame-list)))
395 (while frames
396 (copy-face old-face new-face (car frames))
397 (setq frames (cdr frames)))
398 (copy-face old-face new-face t))
399 (setq old-face (internal-get-face old-face frame))
400 (setq new-face (or (internal-find-face new-face new-frame)
401 (make-face new-face)))
402 (condition-case nil
403 ;; A face that has a global symbolic font modifier such as `bold'
404 ;; might legitimately get an error here.
405 ;; Use the frame's default font in that case.
406 (set-face-font new-face (face-font old-face frame) new-frame)
407 (error
408 (set-face-font new-face nil new-frame)))
409 (set-face-foreground new-face (face-foreground old-face frame) new-frame)
410 (set-face-background new-face (face-background old-face frame) new-frame)
411 (set-face-stipple new-face
412 (face-stipple old-face frame)
413 new-frame)
414 (set-face-underline-p new-face (face-underline-p old-face frame)
415 new-frame))
416 new-face))
418 (defun face-equal (face1 face2 &optional frame)
419 "True if the faces FACE1 and FACE2 display in the same way."
420 (setq face1 (internal-get-face face1 frame)
421 face2 (internal-get-face face2 frame))
422 (and (equal (face-foreground face1 frame) (face-foreground face2 frame))
423 (equal (face-background face1 frame) (face-background face2 frame))
424 (equal (face-font face1 frame) (face-font face2 frame))
425 (eq (face-underline-p face1 frame) (face-underline-p face2 frame))
426 (equal (face-stipple face1 frame)
427 (face-stipple face2 frame))))
429 (defun face-differs-from-default-p (face &optional frame)
430 "True if face FACE displays differently from the default face, on FRAME.
431 A face is considered to be ``the same'' as the default face if it is
432 actually specified in the same way (equivalent fonts, etc) or if it is
433 fully unspecified, and thus inherits the attributes of any face it
434 is displayed on top of.
436 The optional argument FRAME specifies which frame to test;
437 if FRAME is t, test the default for new frames.
438 If FRAME is nil or omitted, test the selected frame."
439 (let ((default (internal-get-face 'default frame)))
440 (setq face (internal-get-face face frame))
441 (not (and (or (equal (face-foreground default frame)
442 (face-foreground face frame))
443 (null (face-foreground face frame)))
444 (or (equal (face-background default frame)
445 (face-background face frame))
446 (null (face-background face frame)))
447 (or (equal (face-font default frame) (face-font face frame))
448 (null (face-font face frame)))
449 (or (equal (face-stipple default frame)
450 (face-stipple face frame))
451 (null (face-stipple face frame)))
452 (equal (face-underline-p default frame)
453 (face-underline-p face frame))
454 ))))
456 (defun face-nontrivial-p (face &optional frame)
457 "True if face FACE has some non-nil attribute.
458 The optional argument FRAME specifies which frame to test;
459 if FRAME is t, test the default for new frames.
460 If FRAME is nil or omitted, test the selected frame."
461 (setq face (internal-get-face face frame))
462 (or (face-foreground face frame)
463 (face-background face frame)
464 (face-font face frame)
465 (face-stipple face frame)
466 (face-underline-p face frame)))
469 (defun invert-face (face &optional frame)
470 "Swap the foreground and background colors of face FACE.
471 If the face doesn't specify both foreground and background, then
472 set its foreground and background to the default background and foreground."
473 (interactive (list (read-face-name "Invert face: ")))
474 (setq face (internal-get-face face frame))
475 (let ((fg (face-foreground face frame))
476 (bg (face-background face frame)))
477 (if (or fg bg)
478 (progn
479 (set-face-foreground face bg frame)
480 (set-face-background face fg frame))
481 (set-face-foreground face (or (face-background 'default frame)
482 (cdr (assq 'background-color (frame-parameters frame))))
483 frame)
484 (set-face-background face (or (face-foreground 'default frame)
485 (cdr (assq 'foreground-color (frame-parameters frame))))
486 frame)))
487 face)
490 (defun internal-try-face-font (face font &optional frame)
491 "Like set-face-font, but returns nil on failure instead of an error."
492 (condition-case ()
493 (set-face-font face font frame)
494 (error nil)))
496 ;; Manipulating font names.
498 (defconst x-font-regexp nil)
499 (defconst x-font-regexp-head nil)
500 (defconst x-font-regexp-weight nil)
501 (defconst x-font-regexp-slant nil)
503 ;;; Regexps matching font names in "Host Portable Character Representation."
505 (let ((- "[-?]")
506 (foundry "[^-]+")
507 (family "[^-]+")
508 (weight "\\(bold\\|demibold\\|medium\\)") ; 1
509 ; (weight\? "\\(\\*\\|bold\\|demibold\\|medium\\|\\)") ; 1
510 (weight\? "\\([^-]*\\)") ; 1
511 (slant "\\([ior]\\)") ; 2
512 ; (slant\? "\\([ior?*]?\\)") ; 2
513 (slant\? "\\([^-]?\\)") ; 2
514 ; (swidth "\\(\\*\\|normal\\|semicondensed\\|\\)") ; 3
515 (swidth "\\([^-]*\\)") ; 3
516 ; (adstyle "\\(\\*\\|sans\\|\\)") ; 4
517 (adstyle "[^-]*") ; 4
518 (pixelsize "[0-9]+")
519 (pointsize "[0-9][0-9]+")
520 (resx "[0-9][0-9]+")
521 (resy "[0-9][0-9]+")
522 (spacing "[cmp?*]")
523 (avgwidth "[0-9]+")
524 (registry "[^-]+")
525 (encoding "[^-]+")
527 (setq x-font-regexp
528 (concat "\\`\\*?[-?*]"
529 foundry - family - weight\? - slant\? - swidth - adstyle -
530 pixelsize - pointsize - resx - resy - spacing - registry -
531 encoding "[-?*]\\*?\\'"
533 (setq x-font-regexp-head
534 (concat "\\`[-?*]" foundry - family - weight\? - slant\?
535 "\\([-*?]\\|\\'\\)"))
536 (setq x-font-regexp-slant (concat - slant -))
537 (setq x-font-regexp-weight (concat - weight -))
538 nil)
540 (defun x-resolve-font-name (pattern &optional face frame)
541 "Return a font name matching PATTERN.
542 All wildcards in PATTERN become substantiated.
543 If PATTERN is nil, return the name of the frame's base font, which never
544 contains wildcards.
545 Given optional arguments FACE and FRAME, return a font which is
546 also the same size as FACE on FRAME, or fail."
547 (or (symbolp face)
548 (setq face (face-name face)))
549 (and (eq frame t)
550 (setq frame nil))
551 (if pattern
552 ;; Note that x-list-fonts has code to handle a face with nil as its font.
553 (let ((fonts (x-list-fonts pattern face frame)))
554 (or fonts
555 (if face
556 (if (string-match "\\*" pattern)
557 (if (null (face-font face))
558 (error "No matching fonts are the same height as the frame default font")
559 (error "No matching fonts are the same height as face `%s'" face))
560 (if (null (face-font face))
561 (error "Height of font `%s' doesn't match the frame default font"
562 pattern)
563 (error "Height of font `%s' doesn't match face `%s'"
564 pattern face)))
565 (error "No fonts match `%s'" pattern)))
566 (car fonts))
567 (cdr (assq 'font (frame-parameters (selected-frame))))))
569 (defun x-frob-font-weight (font which)
570 (if (or (string-match x-font-regexp font)
571 (string-match x-font-regexp-head font)
572 (string-match x-font-regexp-weight font))
573 (concat (substring font 0 (match-beginning 1)) which
574 (substring font (match-end 1)))
575 nil))
577 (defun x-frob-font-slant (font which)
578 (cond ((or (string-match x-font-regexp font)
579 (string-match x-font-regexp-head font))
580 (concat (substring font 0 (match-beginning 2)) which
581 (substring font (match-end 2))))
582 ((string-match x-font-regexp-slant font)
583 (concat (substring font 0 (match-beginning 1)) which
584 (substring font (match-end 1))))
585 (t nil)))
588 (defun x-make-font-bold (font)
589 "Given an X font specification, make a bold version of it.
590 If that can't be done, return nil."
591 (x-frob-font-weight font "bold"))
593 (defun x-make-font-demibold (font)
594 "Given an X font specification, make a demibold version of it.
595 If that can't be done, return nil."
596 (x-frob-font-weight font "demibold"))
598 (defun x-make-font-unbold (font)
599 "Given an X font specification, make a non-bold version of it.
600 If that can't be done, return nil."
601 (x-frob-font-weight font "medium"))
603 (defun x-make-font-italic (font)
604 "Given an X font specification, make an italic version of it.
605 If that can't be done, return nil."
606 (x-frob-font-slant font "i"))
608 (defun x-make-font-oblique (font) ; you say tomayto...
609 "Given an X font specification, make an oblique version of it.
610 If that can't be done, return nil."
611 (x-frob-font-slant font "o"))
613 (defun x-make-font-unitalic (font)
614 "Given an X font specification, make a non-italic version of it.
615 If that can't be done, return nil."
616 (x-frob-font-slant font "r"))
618 ;;; non-X-specific interface
620 (defun make-face-bold (face &optional frame noerror)
621 "Make the font of the given face be bold, if possible.
622 If NOERROR is non-nil, return nil on failure."
623 (interactive (list (read-face-name "Make which face bold: ")))
624 (if (and (eq frame t) (listp (face-font face t)))
625 (set-face-font face (if (memq 'italic (face-font face t))
626 '(bold italic) '(bold))
628 (let ((ofont (face-font face frame))
629 font)
630 (if (null frame)
631 (let ((frames (frame-list)))
632 ;; Make this face bold in global-face-data.
633 (make-face-bold face t noerror)
634 ;; Make this face bold in each frame.
635 (while frames
636 (make-face-bold face (car frames) noerror)
637 (setq frames (cdr frames))))
638 (setq face (internal-get-face face frame))
639 (setq font (or (face-font face frame)
640 (face-font face t)))
641 (if (listp font)
642 (setq font nil))
643 (setq font (or font
644 (face-font 'default frame)
645 (cdr (assq 'font (frame-parameters frame)))))
646 (and font (make-face-bold-internal face frame font)))
647 (or (not (equal ofont (face-font face)))
648 (and (not noerror)
649 (error "No bold version of %S" font))))))
651 (defun make-face-bold-internal (face frame font)
652 (let (f2)
653 (or (and (setq f2 (x-make-font-bold font))
654 (internal-try-face-font face f2 frame))
655 (and (setq f2 (x-make-font-demibold font))
656 (internal-try-face-font face f2 frame)))))
658 (defun make-face-italic (face &optional frame noerror)
659 "Make the font of the given face be italic, if possible.
660 If NOERROR is non-nil, return nil on failure."
661 (interactive (list (read-face-name "Make which face italic: ")))
662 (if (and (eq frame t) (listp (face-font face t)))
663 (set-face-font face (if (memq 'bold (face-font face t))
664 '(bold italic) '(italic))
666 (let ((ofont (face-font face frame))
667 font)
668 (if (null frame)
669 (let ((frames (frame-list)))
670 ;; Make this face italic in global-face-data.
671 (make-face-italic face t noerror)
672 ;; Make this face italic in each frame.
673 (while frames
674 (make-face-italic face (car frames) noerror)
675 (setq frames (cdr frames))))
676 (setq face (internal-get-face face frame))
677 (setq font (or (face-font face frame)
678 (face-font face t)))
679 (if (listp font)
680 (setq font nil))
681 (setq font (or font
682 (face-font 'default frame)
683 (cdr (assq 'font (frame-parameters frame)))))
684 (and font (make-face-italic-internal face frame font)))
685 (or (not (equal ofont (face-font face)))
686 (and (not noerror)
687 (error "No italic version of %S" font))))))
689 (defun make-face-italic-internal (face frame font)
690 (let (f2)
691 (or (and (setq f2 (x-make-font-italic font))
692 (internal-try-face-font face f2 frame))
693 (and (setq f2 (x-make-font-oblique font))
694 (internal-try-face-font face f2 frame)))))
696 (defun make-face-bold-italic (face &optional frame noerror)
697 "Make the font of the given face be bold and italic, if possible.
698 If NOERROR is non-nil, return nil on failure."
699 (interactive (list (read-face-name "Make which face bold-italic: ")))
700 (if (and (eq frame t) (listp (face-font face t)))
701 (set-face-font face '(bold italic) t)
702 (let ((ofont (face-font face frame))
703 font)
704 (if (null frame)
705 (let ((frames (frame-list)))
706 ;; Make this face bold-italic in global-face-data.
707 (make-face-bold-italic face t noerror)
708 ;; Make this face bold in each frame.
709 (while frames
710 (make-face-bold-italic face (car frames) noerror)
711 (setq frames (cdr frames))))
712 (setq face (internal-get-face face frame))
713 (setq font (or (face-font face frame)
714 (face-font face t)))
715 (if (listp font)
716 (setq font nil))
717 (setq font (or font
718 (face-font 'default frame)
719 (cdr (assq 'font (frame-parameters frame)))))
720 (and font (make-face-bold-italic-internal face frame font)))
721 (or (not (equal ofont (face-font face)))
722 (and (not noerror)
723 (error "No bold italic version of %S" font))))))
725 (defun make-face-bold-italic-internal (face frame font)
726 (let (f2 f3)
727 (or (and (setq f2 (x-make-font-italic font))
728 (not (equal font f2))
729 (setq f3 (x-make-font-bold f2))
730 (not (equal f2 f3))
731 (internal-try-face-font face f3 frame))
732 (and (setq f2 (x-make-font-oblique font))
733 (not (equal font f2))
734 (setq f3 (x-make-font-bold f2))
735 (not (equal f2 f3))
736 (internal-try-face-font face f3 frame))
737 (and (setq f2 (x-make-font-italic font))
738 (not (equal font f2))
739 (setq f3 (x-make-font-demibold f2))
740 (not (equal f2 f3))
741 (internal-try-face-font face f3 frame))
742 (and (setq f2 (x-make-font-oblique font))
743 (not (equal font f2))
744 (setq f3 (x-make-font-demibold f2))
745 (not (equal f2 f3))
746 (internal-try-face-font face f3 frame)))))
748 (defun make-face-unbold (face &optional frame noerror)
749 "Make the font of the given face be non-bold, if possible.
750 If NOERROR is non-nil, return nil on failure."
751 (interactive (list (read-face-name "Make which face non-bold: ")))
752 (if (and (eq frame t) (listp (face-font face t)))
753 (set-face-font face (if (memq 'italic (face-font face t))
754 '(italic) nil)
756 (let ((ofont (face-font face frame))
757 font font1)
758 (if (null frame)
759 (let ((frames (frame-list)))
760 ;; Make this face unbold in global-face-data.
761 (make-face-unbold face t noerror)
762 ;; Make this face unbold in each frame.
763 (while frames
764 (make-face-unbold face (car frames) noerror)
765 (setq frames (cdr frames))))
766 (setq face (internal-get-face face frame))
767 (setq font1 (or (face-font face frame)
768 (face-font face t)))
769 (if (listp font1)
770 (setq font1 nil))
771 (setq font1 (or font1
772 (face-font 'default frame)
773 (cdr (assq 'font (frame-parameters frame)))))
774 (setq font (and font1 (x-make-font-unbold font1)))
775 (if font (internal-try-face-font face font frame)))
776 (or (not (equal ofont (face-font face)))
777 (and (not noerror)
778 (error "No unbold version of %S" font1))))))
780 (defun make-face-unitalic (face &optional frame noerror)
781 "Make the font of the given face be non-italic, if possible.
782 If NOERROR is non-nil, return nil on failure."
783 (interactive (list (read-face-name "Make which face non-italic: ")))
784 (if (and (eq frame t) (listp (face-font face t)))
785 (set-face-font face (if (memq 'bold (face-font face t))
786 '(bold) nil)
788 (let ((ofont (face-font face frame))
789 font font1)
790 (if (null frame)
791 (let ((frames (frame-list)))
792 ;; Make this face unitalic in global-face-data.
793 (make-face-unitalic face t noerror)
794 ;; Make this face unitalic in each frame.
795 (while frames
796 (make-face-unitalic face (car frames) noerror)
797 (setq frames (cdr frames))))
798 (setq face (internal-get-face face frame))
799 (setq font1 (or (face-font face frame)
800 (face-font face t)))
801 (if (listp font1)
802 (setq font1 nil))
803 (setq font1 (or font1
804 (face-font 'default frame)
805 (cdr (assq 'font (frame-parameters frame)))))
806 (setq font (and font1 (x-make-font-unitalic font1)))
807 (if font (internal-try-face-font face font frame)))
808 (or (not (equal ofont (face-font face)))
809 (and (not noerror)
810 (error "No unitalic version of %S" font1))))))
812 (defvar list-faces-sample-text
813 "abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ"
814 "*Text string to display as the sample text for `list-faces-display'.")
816 ;; The name list-faces would be more consistent, but let's avoid a conflict
817 ;; with Lucid, which uses that name differently.
818 (defun list-faces-display ()
819 "List all faces, using the same sample text in each.
820 The sample text is a string that comes from the variable
821 `list-faces-sample-text'.
823 It is possible to give a particular face name different appearances in
824 different frames. This command shows the appearance in the
825 selected frame."
826 (interactive)
827 (let ((faces (sort (face-list) (function string-lessp)))
828 (face nil)
829 (frame (selected-frame))
830 disp-frame window)
831 (with-output-to-temp-buffer "*Faces*"
832 (save-excursion
833 (set-buffer standard-output)
834 (setq truncate-lines t)
835 (while faces
836 (setq face (car faces))
837 (setq faces (cdr faces))
838 (insert (format "%25s " (symbol-name face)))
839 (let ((beg (point)))
840 (insert list-faces-sample-text)
841 (insert "\n")
842 (put-text-property beg (1- (point)) 'face face)
843 ;; If the sample text has multiple lines, line up all of them.
844 (goto-char beg)
845 (forward-line 1)
846 (while (not (eobp))
847 (insert " ")
848 (forward-line 1))))
849 (goto-char (point-min))))
850 ;; If the *Faces* buffer appears in a different frame,
851 ;; copy all the face definitions from FRAME,
852 ;; so that the display will reflect the frame that was selected.
853 (setq window (get-buffer-window (get-buffer "*Faces*") t))
854 (setq disp-frame (if window (window-frame window)
855 (car (frame-list))))
856 (or (eq frame disp-frame)
857 (let ((faces (face-list)))
858 (while faces
859 (copy-face (car faces) (car faces) frame disp-frame)
860 (setq faces (cdr faces)))))))
862 ;;; Make the standard faces.
863 ;;; The C code knows the default and modeline faces as faces 0 and 1,
864 ;;; so they must be the first two faces made.
865 (defun face-initialize ()
866 (make-face 'default)
867 (make-face 'modeline)
868 (make-face 'highlight)
870 ;; These aren't really special in any way, but they're nice to have around.
872 (make-face 'bold)
873 (make-face 'italic)
874 (make-face 'bold-italic)
875 (make-face 'region)
876 (make-face 'secondary-selection)
877 (make-face 'underline)
879 (setq region-face (face-id 'region))
881 ;; Specify the global properties of these faces
882 ;; so they will come out right on new frames.
884 (make-face-bold 'bold t)
885 (make-face-italic 'italic t)
886 (make-face-bold-italic 'bold-italic t)
888 (set-face-background 'highlight '("darkseagreen2" "green" t) t)
889 (set-face-background 'region '("gray" underline) t)
890 (set-face-background 'secondary-selection '("paleturquoise" "green" t) t)
891 (set-face-background 'modeline '(t) t)
892 (set-face-underline-p 'underline t t)
894 ;; Set up the faces of all existing X Window frames
895 ;; from those global properties, unless already set in a given frame.
897 (let ((frames (frame-list)))
898 (while frames
899 (if (not (memq (framep (car frames)) '(t nil)))
900 (let ((frame (car frames))
901 (rest global-face-data))
902 (while rest
903 (let ((face (car (car rest))))
904 (or (face-differs-from-default-p face)
905 (face-fill-in face (cdr (car rest)) frame)))
906 (setq rest (cdr rest)))))
907 (setq frames (cdr frames)))))
910 ;; Like x-create-frame but also set up the faces.
912 (defun x-create-frame-with-faces (&optional parameters)
913 (if (null global-face-data)
914 (x-create-frame parameters)
915 (let* ((visibility-spec (assq 'visibility parameters))
916 (frame (x-create-frame (cons '(visibility . nil) parameters)))
917 (faces (copy-alist global-face-data))
918 success
919 (rest faces))
920 (unwind-protect
921 (progn
922 (set-frame-face-alist frame faces)
924 (if (cdr (or (assq 'reverse parameters)
925 (assq 'reverse default-frame-alist)
926 (let ((resource (x-get-resource "reverseVideo"
927 "ReverseVideo")))
928 (if resource
929 (cons nil (member (downcase resource)
930 '("on" "true")))))))
931 (let* ((params (frame-parameters frame))
932 (bg (cdr (assq 'foreground-color params)))
933 (fg (cdr (assq 'background-color params))))
934 (modify-frame-parameters frame
935 (list (cons 'foreground-color fg)
936 (cons 'background-color bg)))
937 (if (equal bg (cdr (assq 'border-color params)))
938 (modify-frame-parameters frame
939 (list (cons 'border-color fg))))
940 (if (equal bg (cdr (assq 'mouse-color params)))
941 (modify-frame-parameters frame
942 (list (cons 'mouse-color fg))))
943 (if (equal bg (cdr (assq 'cursor-color params)))
944 (modify-frame-parameters frame
945 (list (cons 'cursor-color fg))))))
946 ;; Copy the vectors that represent the faces.
947 ;; Also fill them in from X resources.
948 (while rest
949 (let ((global (cdr (car rest))))
950 (setcdr (car rest) (vector 'face
951 (face-name (cdr (car rest)))
952 (face-id (cdr (car rest)))
953 nil nil nil nil nil))
954 (face-fill-in (car (car rest)) global frame))
955 (make-face-x-resource-internal (cdr (car rest)) frame t)
956 (setq rest (cdr rest)))
957 (if (null visibility-spec)
958 (make-frame-visible frame)
959 (modify-frame-parameters frame (list visibility-spec)))
960 (setq success t)
961 frame)
962 (or success
963 (delete-frame frame))))))
965 ;; Update a frame's faces when we change its default font.
966 (defun frame-update-faces (frame)
967 (let* ((faces global-face-data)
968 (rest faces))
969 (while rest
970 (let* ((face (car (car rest)))
971 (font (face-font face t)))
972 (if (listp font)
973 (let ((bold (memq 'bold font))
974 (italic (memq 'italic font)))
975 ;; Ignore any previous (string-valued) font, it might not even
976 ;; be the right size anymore.
977 (set-face-font face nil frame)
978 (cond ((and bold italic)
979 (make-face-bold-italic face frame t))
980 (bold
981 (make-face-bold face frame t))
982 (italic
983 (make-face-italic face frame t)))))
984 (setq rest (cdr rest)))
985 frame)))
987 ;; Update the colors of FACE, after FRAME's own colors have been changed.
988 ;; This applies only to faces with global color specifications
989 ;; that are not simple constants.
990 (defun frame-update-face-colors (frame)
991 (let ((faces global-face-data))
992 (while faces
993 (condition-case nil
994 (let* ((data (cdr (car faces)))
995 (face (car (car faces)))
996 (foreground (face-foreground data))
997 (background (face-background data)))
998 ;; If the global spec is a specific color,
999 ;; which doesn't depend on the frame's attributes,
1000 ;; we don't need to recalculate it now.
1001 (or (listp foreground)
1002 (setq foreground nil))
1003 (or (listp background)
1004 (setq background nil))
1005 ;; If we are going to frob this face at all,
1006 ;; reinitialize it first.
1007 (if (or foreground background)
1008 (progn (set-face-foreground face nil frame)
1009 (set-face-background face nil frame)))
1010 (if foreground
1011 (face-try-color-list 'set-face-foreground
1012 face foreground frame))
1013 (if background
1014 (face-try-color-list 'set-face-background
1015 face background frame)))
1016 (error nil))
1017 (setq faces (cdr faces)))))
1019 ;; Fill in the face FACE from frame-independent face data DATA.
1020 ;; DATA should be the non-frame-specific ("global") face vector
1021 ;; for the face. FACE should be a face name or face object.
1022 ;; FRAME is the frame to act on; it must be an actual frame, not nil or t.
1023 (defun face-fill-in (face data frame)
1024 (condition-case nil
1025 (let ((foreground (face-foreground data))
1026 (background (face-background data))
1027 (font (face-font data)))
1028 (set-face-underline-p face (face-underline-p data) frame)
1029 (if foreground
1030 (face-try-color-list 'set-face-foreground
1031 face foreground frame))
1032 (if background
1033 (face-try-color-list 'set-face-background
1034 face background frame))
1035 (if (listp font)
1036 (let ((bold (memq 'bold font))
1037 (italic (memq 'italic font)))
1038 (cond ((and bold italic)
1039 (make-face-bold-italic face frame))
1040 (bold
1041 (make-face-bold face frame))
1042 (italic
1043 (make-face-italic face frame))))
1044 (if font
1045 (set-face-font face font frame))))
1046 (error nil)))
1048 ;; Assuming COLOR is a valid color name,
1049 ;; return t if it can be displayed on FRAME.
1050 (defun face-color-supported-p (frame color background-p)
1051 (or (x-display-color-p frame)
1052 ;; A black-and-white display can implement these.
1053 (member color '("black" "white"))
1054 ;; A black-and-white display can fake these for background.
1055 (and background-p
1056 (member color '("gray" "gray1" "gray3")))
1057 ;; A grayscale display can implement colors that are gray (more or less).
1058 (and (x-display-grayscale-p frame)
1059 (let* ((values (x-color-values color frame))
1060 (r (nth 0 values))
1061 (g (nth 1 values))
1062 (b (nth 2 values)))
1063 (and (< (abs (- r g)) (/ (abs (+ r g)) 20))
1064 (< (abs (- g b)) (/ (abs (+ g b)) 20))
1065 (< (abs (- b r)) (/ (abs (+ b r)) 20)))))))
1067 ;; Use FUNCTION to store a color in FACE on FRAME.
1068 ;; COLORS is either a single color or a list of colors.
1069 ;; If it is a list, try the colors one by one until one of them
1070 ;; succeeds. We signal an error only if all the colors failed.
1071 ;; t as COLORS or as an element of COLORS means to invert the face.
1072 ;; That can't fail, so any subsequent elements after the t are ignored.
1073 (defun face-try-color-list (function face colors frame)
1074 (if (stringp colors)
1075 (if (face-color-supported-p frame colors
1076 (eq function 'set-face-background))
1077 (funcall function face colors frame))
1078 (if (eq colors t)
1079 (invert-face face frame)
1080 (let (done)
1081 (while (and colors (not done))
1082 (if (or (memq (car colors) '(t underline))
1083 (face-color-supported-p frame (car colors)
1084 (eq function 'set-face-background)))
1085 (if (cdr colors)
1086 ;; If there are more colors to try, catch errors
1087 ;; and set `done' if we succeed.
1088 (condition-case nil
1089 (progn
1090 (cond ((eq (car colors) t)
1091 (invert-face face frame))
1092 ((eq (car colors) 'underline)
1093 (set-face-underline-p face t frame))
1095 (funcall function face (car colors) frame)))
1096 (setq done t))
1097 (error nil))
1098 ;; If this is the last color, let the error get out if it fails.
1099 ;; If it succeeds, we will exit anyway after this iteration.
1100 (cond ((eq (car colors) t)
1101 (invert-face face frame))
1102 ((eq (car colors) 'underline)
1103 (set-face-underline-p face t frame))
1105 (funcall function face (car colors) frame)))))
1106 (setq colors (cdr colors)))))))
1108 ;; If we are already using x-window frames, initialize faces for them.
1109 (if (eq (framep (selected-frame)) 'x)
1110 (face-initialize))
1112 (provide 'faces)
1114 ;;; faces.el ends here