1 ;;; faces.el --- Lisp interface to the c "face" structure
3 ;; Copyright (C) 1992, 1993, 1994, 1995 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)
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.
23 ;; Mostly derived from Lucid.
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-face-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]
49 (defsubst internal-facep
(x)
50 (and (vectorp x
) (= (length x
) 8) (eq (aref x
0) 'face
)))
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
)))
58 (defmacro internal-check-face
(face)
59 (` (or (internal-facep (, face
))
60 (signal 'wrong-type-argument
(list 'internal-facep
(, face
))))))
63 (defun face-name (face)
64 "Return the name of face FACE."
65 (aref (internal-get-face face
) 1))
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))
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 (defvar face-default-stipple
"gray3"
129 "Default stipple pattern used on monochrome displays.
130 This stipple pattern is used on monochrome displays
131 instead of shades of gray for a face background color.
132 See `set-face-stipple' for possible values for this variable.")
134 (defun face-color-gray-p (color &optional frame
)
135 "Return t if COLOR is a shade of gray (or white or black).
136 FRAME specifies the frame and thus the display for interpreting COLOR."
137 (let* ((values (x-color-values color frame
))
141 (and (< (abs (- r g
)) (/ (max 1 (abs r
) (abs g
)) 20))
142 (< (abs (- g b
)) (/ (max 1 (abs g
) (abs b
)) 20))
143 (< (abs (- b r
)) (/ (max 1 (abs b
) (abs r
)) 20)))))
145 (defun set-face-background (face color
&optional frame
)
146 "Change the background color of face FACE to COLOR (a string).
147 If the optional FRAME argument is provided, change only
148 in that frame; otherwise change each frame."
149 (interactive (internal-face-interactive "background"))
150 ;; For a specific frame, use gray stipple instead of gray color
151 ;; if the display does not support a gray color.
152 (if (and frame
(not (eq frame t
)) color
153 ;; Check for supportedness for foreground, not for background!
154 ;; face-color-supported-p is smart enough to know
155 ;; that grays are "supported" as background
156 ;; because we are supposed to use stipple for them!
157 (not (face-color-supported-p frame color nil
)))
158 (set-face-stipple face face-default-stipple frame
)
160 (let ((frames (frame-list)))
162 (set-face-background (face-name face
) color
(car frames
))
163 (setq frames
(cdr frames
)))
164 (set-face-background face color t
)
166 (internal-set-face-1 face
'background color
5 frame
))))
168 (defun set-face-stipple (face pixmap
&optional frame
)
169 "Change the stipple pixmap of face FACE to PIXMAP.
170 PIXMAP should be a string, the name of a file of pixmap data.
171 The directories listed in the `x-bitmap-file-path' variable are searched.
173 Alternatively, PIXMAP may be a list of the form (WIDTH HEIGHT DATA)
174 where WIDTH and HEIGHT are the size in pixels,
175 and DATA is a string, containing the raw bits of the bitmap.
177 If the optional FRAME argument is provided, change only
178 in that frame; otherwise change each frame."
179 (interactive (internal-face-interactive "stipple"))
180 (internal-set-face-1 face
'background-pixmap pixmap
6 frame
))
182 (defalias 'set-face-background-pixmap
'set-face-stipple
)
184 (defun set-face-underline-p (face underline-p
&optional frame
)
185 "Specify whether face FACE is underlined. (Yes if UNDERLINE-P is non-nil.)
186 If the optional FRAME argument is provided, change only
187 in that frame; otherwise change each frame."
188 (interactive (internal-face-interactive "underline-p" "underlined"))
189 (internal-set-face-1 face
'underline underline-p
7 frame
))
191 (defun modify-face-read-string (face default name alist
)
195 (format "Set face %s %s (default %s): "
196 face name
(downcase default
))
197 (format "Set face %s %s: " face name
))
199 (cond ((equal value
"none")
205 (defun modify-face (face foreground background stipple
206 bold-p italic-p underline-p
)
207 "Change the display attributes for face FACE.
208 FOREGROUND and BACKGROUND should be color strings or nil.
209 STIPPLE should be a stipple pattern name or nil.
210 BOLD-P, ITALIC-P, and UNDERLINE-P specify whether the face should be set bold,
211 in italic, and underlined, respectively. (Yes if non-nil.)
212 If called interactively, prompts for a face and face attributes."
214 (let* ((completion-ignore-case t
)
215 (face (symbol-name (read-face-name "Modify face: ")))
216 (colors (mapcar 'list x-colors
))
217 (stipples (mapcar 'list
219 (mapcar 'directory-files
220 x-bitmap-file-path
))))
221 (foreground (modify-face-read-string
222 face
(face-foreground (intern face
))
223 "foreground" colors
))
224 (background (modify-face-read-string
225 face
(face-background (intern face
))
226 "background" colors
))
227 (stipple (modify-face-read-string
228 face
(face-stipple (intern face
))
230 (bold-p (y-or-n-p (concat "Set face " face
" bold ")))
231 (italic-p (y-or-n-p (concat "Set face " face
" italic ")))
232 (underline-p (y-or-n-p (concat "Set face " face
" underline "))))
233 (message "Face %s: %s" face
236 (list (and foreground
(concat (downcase foreground
) " foreground"))
237 (and background
(concat (downcase background
) " background"))
238 (and stipple
(concat (downcase stipple
) " stipple"))
239 (and bold-p
"bold") (and italic-p
"italic")
240 (and underline-p
"underline"))) ", "))
241 (list (intern face
) foreground background stipple
242 bold-p italic-p underline-p
)))
243 (condition-case nil
(set-face-foreground face foreground
) (error nil
))
244 (condition-case nil
(set-face-background face background
) (error nil
))
245 (condition-case nil
(set-face-stipple face stipple
) (error nil
))
246 (funcall (if bold-p
'make-face-bold
'make-face-unbold
) face nil t
)
247 (funcall (if italic-p
'make-face-italic
'make-face-unitalic
) face nil t
)
248 (set-face-underline-p face underline-p
)
249 (and (interactive-p) (redraw-display)))
251 ;;;; Associating face names (symbols) with their face vectors.
253 (defvar global-face-data nil
254 "Internal data for face support functions. Not for external use.
255 This is an alist associating face names with the default values for
256 their parameters. Newly created frames get their data from here.")
259 "Returns a list of all defined face names."
260 (mapcar 'car global-face-data
))
262 (defun internal-find-face (name &optional frame
)
263 "Retrieve the face named NAME. Return nil if there is no such face.
264 If the optional argument FRAME is given, this gets the face NAME for
265 that frame; otherwise, it uses the selected frame.
266 If FRAME is the symbol t, then the global, non-frame face is returned.
267 If NAME is already a face, it is simply returned."
268 (if (and (eq frame t
) (not (symbolp name
)))
269 (setq name
(face-name name
)))
274 (frame-face-alist (or frame
(selected-frame))))))
275 (internal-check-face name
)
278 (defun internal-get-face (name &optional frame
)
279 "Retrieve the face named NAME; error if there is none.
280 If the optional argument FRAME is given, this gets the face NAME for
281 that frame; otherwise, it uses the selected frame.
282 If FRAME is the symbol t, then the global, non-frame face is returned.
283 If NAME is already a face, it is simply returned."
284 (or (internal-find-face name frame
)
285 (internal-check-face name
)))
288 (defun internal-set-face-1 (face name value index frame
)
289 (let ((inhibit-quit t
))
291 (let ((frames (frame-list)))
293 (internal-set-face-1 (face-name face
) name value index
(car frames
))
294 (setq frames
(cdr frames
)))
295 (aset (internal-get-face (if (symbolp face
) face
(face-name face
)) t
)
299 (set-face-attribute-internal (face-id face
) name value frame
))
300 (aset (internal-get-face face frame
) index value
))))
303 (defun read-face-name (prompt)
305 (while (= (length face
) 0)
306 (setq face
(completing-read prompt
307 (mapcar '(lambda (x) (list (symbol-name x
)))
312 (defun internal-face-interactive (what &optional bool
)
313 (let* ((fn (intern (concat "face-" what
)))
314 (prompt (concat "Set " what
" of face"))
315 (face (read-face-name (concat prompt
": ")))
316 (default (if (fboundp fn
)
317 (or (funcall fn face
(selected-frame))
318 (funcall fn
'default
(selected-frame)))))
320 (y-or-n-p (concat "Should face " (symbol-name face
)
322 (read-string (concat prompt
" " (symbol-name face
) " to: ")
324 (list face
(if (equal value
"") nil value
))))
328 (defun make-face (name)
329 "Define a new FACE on all frames.
330 You can modify the font, color, etc of this face with the set-face- functions.
331 If the face already exists, it is unmodified."
332 (interactive "SMake face: ")
333 (or (internal-find-face name
)
334 (let ((face (make-vector 8 nil
)))
337 (let* ((frames (frame-list))
339 (id (internal-next-face-id)))
340 (make-face-internal id
)
343 (set-frame-face-alist (car frames
)
344 (cons (cons name
(copy-sequence face
))
345 (frame-face-alist (car frames
))))
346 (setq frames
(cdr frames
)))
347 (setq global-face-data
(cons (cons name face
) global-face-data
)))
348 ;; when making a face after frames already exist
349 (if (eq window-system
'x
)
350 (make-face-x-resource-internal face
))
352 (if (fboundp 'facemenu-add-new-face
)
353 (facemenu-add-new-face name
))
357 ;; Fill in a face by default based on X resources, for all existing frames.
358 ;; This has to be done when a new face is made.
359 (defun make-face-x-resource-internal (face &optional frame set-anyway
)
361 (let ((frames (frame-list)))
363 (if (eq (framep (car frames
)) 'x
)
364 (make-face-x-resource-internal (face-name face
)
365 (car frames
) set-anyway
))
366 (setq frames
(cdr frames
)))))
368 (setq face
(internal-get-face (face-name face
) frame
))
370 ;; These are things like "attributeForeground" instead of simply
371 ;; "foreground" because people tend to do things like "*foreground",
372 ;; which would cause all faces to be fully qualified, making faces
373 ;; inherit attributes in a non-useful way. So we've made them slightly
374 ;; less obvious to specify in order to make them work correctly in
375 ;; more random environments.
377 ;; I think these should be called "face.faceForeground" instead of
378 ;; "face.attributeForeground", but they're the way they are for
379 ;; hysterical reasons.
381 (let* ((name (symbol-name (face-name face
)))
382 (fn (or (x-get-resource (concat name
".attributeFont")
383 "Face.AttributeFont")
384 (and set-anyway
(face-font face
))))
385 (fg (or (x-get-resource (concat name
".attributeForeground")
386 "Face.AttributeForeground")
387 (and set-anyway
(face-foreground face
))))
388 (bg (or (x-get-resource (concat name
".attributeBackground")
389 "Face.AttributeBackground")
390 (and set-anyway
(face-background face
))))
391 (bgp (or (x-get-resource (concat name
".attributeStipple")
392 "Face.AttributeStipple")
393 (x-get-resource (concat name
".attributeBackgroundPixmap")
394 "Face.AttributeBackgroundPixmap")
395 (and set-anyway
(face-stipple face
))))
396 (ulp (let ((resource (x-get-resource
397 (concat name
".attributeUnderline")
398 "Face.AttributeUnderline")))
400 (member (downcase resource
) '("on" "true"))
401 (and set-anyway
(face-underline-p face
)))))
405 (cond ((string= fn
"italic")
406 (make-face-italic face
))
408 (make-face-bold face
))
409 ((string= fn
"bold-italic")
410 (make-face-bold-italic face
))
412 (set-face-font face fn frame
)))
414 (if (member fn
'("italic" "bold" "bold-italic"))
415 (message "no %s version found for face `%s'" fn name
)
416 (message "font `%s' not found for face `%s'" fn name
)))))
419 (set-face-foreground face fg frame
)
420 (error (message "color `%s' not allocated for face `%s'" fg name
))))
423 (set-face-background face bg frame
)
424 (error (message "color `%s' not allocated for face `%s'" bg name
))))
427 (set-face-stipple face bgp frame
)
428 (error (message "pixmap `%s' not found for face `%s'" bgp name
))))
429 (if (or ulp set-anyway
)
430 (set-face-underline-p face ulp frame
))
434 (defun copy-face (old-face new-face
&optional frame new-frame
)
435 "Define a face just like OLD-FACE, with name NEW-FACE.
436 If NEW-FACE already exists as a face, it is modified to be like OLD-FACE.
437 If it doesn't already exist, it is created.
439 If the optional argument FRAME is given as a frame,
440 NEW-FACE is changed on FRAME only.
441 If FRAME is t, the frame-independent default specification for OLD-FACE
442 is copied to NEW-FACE.
443 If FRAME is nil, copying is done for the frame-independent defaults
444 and for each existing frame.
445 If the optional fourth argument NEW-FRAME is given,
446 copy the information from face OLD-FACE on frame FRAME
447 to NEW-FACE on frame NEW-FRAME."
448 (or new-frame
(setq new-frame frame
))
449 (let ((inhibit-quit t
))
451 (let ((frames (frame-list)))
453 (copy-face old-face new-face
(car frames
))
454 (setq frames
(cdr frames
)))
455 (copy-face old-face new-face t
))
456 (setq old-face
(internal-get-face old-face frame
))
457 (setq new-face
(or (internal-find-face new-face new-frame
)
458 (make-face new-face
)))
460 ;; A face that has a global symbolic font modifier such as `bold'
461 ;; might legitimately get an error here.
462 ;; Use the frame's default font in that case.
463 (set-face-font new-face
(face-font old-face frame
) new-frame
)
465 (set-face-font new-face nil new-frame
)))
466 (set-face-foreground new-face
(face-foreground old-face frame
) new-frame
)
467 (set-face-background new-face
(face-background old-face frame
) new-frame
)
468 (set-face-stipple new-face
469 (face-stipple old-face frame
)
471 (set-face-underline-p new-face
(face-underline-p old-face frame
)
475 (defun face-equal (face1 face2
&optional frame
)
476 "True if the faces FACE1 and FACE2 display in the same way."
477 (setq face1
(internal-get-face face1 frame
)
478 face2
(internal-get-face face2 frame
))
479 (and (equal (face-foreground face1 frame
) (face-foreground face2 frame
))
480 (equal (face-background face1 frame
) (face-background face2 frame
))
481 (equal (face-font face1 frame
) (face-font face2 frame
))
482 (eq (face-underline-p face1 frame
) (face-underline-p face2 frame
))
483 (equal (face-stipple face1 frame
)
484 (face-stipple face2 frame
))))
486 (defun face-differs-from-default-p (face &optional frame
)
487 "True if face FACE displays differently from the default face, on FRAME.
488 A face is considered to be ``the same'' as the default face if it is
489 actually specified in the same way (equivalent fonts, etc) or if it is
490 fully unspecified, and thus inherits the attributes of any face it
491 is displayed on top of.
493 The optional argument FRAME specifies which frame to test;
494 if FRAME is t, test the default for new frames.
495 If FRAME is nil or omitted, test the selected frame."
496 (let ((default (internal-get-face 'default frame
)))
497 (setq face
(internal-get-face face frame
))
498 (not (and (or (equal (face-foreground default frame
)
499 (face-foreground face frame
))
500 (null (face-foreground face frame
)))
501 (or (equal (face-background default frame
)
502 (face-background face frame
))
503 (null (face-background face frame
)))
504 (or (equal (face-font default frame
) (face-font face frame
))
505 (null (face-font face frame
)))
506 (or (equal (face-stipple default frame
)
507 (face-stipple face frame
))
508 (null (face-stipple face frame
)))
509 (equal (face-underline-p default frame
)
510 (face-underline-p face frame
))
513 (defun face-nontrivial-p (face &optional frame
)
514 "True if face FACE has some non-nil attribute.
515 The optional argument FRAME specifies which frame to test;
516 if FRAME is t, test the default for new frames.
517 If FRAME is nil or omitted, test the selected frame."
518 (setq face
(internal-get-face face frame
))
519 (or (face-foreground face frame
)
520 (face-background face frame
)
521 (face-font face frame
)
522 (face-stipple face frame
)
523 (face-underline-p face frame
)))
526 (defun invert-face (face &optional frame
)
527 "Swap the foreground and background colors of face FACE.
528 If the face doesn't specify both foreground and background, then
529 set its foreground and background to the default background and foreground."
530 (interactive (list (read-face-name "Invert face: ")))
531 (setq face
(internal-get-face face frame
))
532 (let ((fg (face-foreground face frame
))
533 (bg (face-background face frame
)))
536 (set-face-foreground face bg frame
)
537 (set-face-background face fg frame
))
538 (set-face-foreground face
(or (face-background 'default frame
)
539 (cdr (assq 'background-color
(frame-parameters frame
))))
541 (set-face-background face
(or (face-foreground 'default frame
)
542 (cdr (assq 'foreground-color
(frame-parameters frame
))))
547 (defun internal-try-face-font (face font
&optional frame
)
548 "Like set-face-font, but returns nil on failure instead of an error."
550 (set-face-font face font frame
)
553 ;; Manipulating font names.
555 (defconst x-font-regexp nil
)
556 (defconst x-font-regexp-head nil
)
557 (defconst x-font-regexp-weight nil
)
558 (defconst x-font-regexp-slant nil
)
560 (defconst x-font-regexp-weight-subnum
1)
561 (defconst x-font-regexp-slant-subnum
2)
562 (defconst x-font-regexp-swidth-subnum
3)
563 (defconst x-font-regexp-adstyle-subnum
4)
565 ;;; Regexps matching font names in "Host Portable Character Representation."
570 (weight "\\(bold\\|demibold\\|medium\\)") ; 1
571 ; (weight\? "\\(\\*\\|bold\\|demibold\\|medium\\|\\)") ; 1
572 (weight\? "\\([^-]*\\)") ; 1
573 (slant "\\([ior]\\)") ; 2
574 ; (slant\? "\\([ior?*]?\\)") ; 2
575 (slant\? "\\([^-]?\\)") ; 2
576 ; (swidth "\\(\\*\\|normal\\|semicondensed\\|\\)") ; 3
577 (swidth "\\([^-]*\\)") ; 3
578 ; (adstyle "\\(\\*\\|sans\\|\\)") ; 4
579 (adstyle "\\([^-]*\\)") ; 4
581 (pointsize "[0-9][0-9]+")
590 (concat "\\`\\*?[-?*]"
591 foundry - family - weight
\? - slant
\? - swidth - adstyle -
592 pixelsize - pointsize - resx - resy - spacing - avgwidth -
593 registry - encoding
"\\*?\\'"
595 (setq x-font-regexp-head
596 (concat "\\`[-?*]" foundry - family - weight
\? - slant
\?
597 "\\([-*?]\\|\\'\\)"))
598 (setq x-font-regexp-slant
(concat - slant -
))
599 (setq x-font-regexp-weight
(concat - weight -
))
602 (defun x-resolve-font-name (pattern &optional face frame
)
603 "Return a font name matching PATTERN.
604 All wildcards in PATTERN become substantiated.
605 If PATTERN is nil, return the name of the frame's base font, which never
607 Given optional arguments FACE and FRAME, return a font which is
608 also the same size as FACE on FRAME, or fail."
610 (setq face
(face-name face
)))
614 ;; Note that x-list-fonts has code to handle a face with nil as its font.
615 (let ((fonts (x-list-fonts pattern face frame
)))
618 (if (string-match "\\*" pattern
)
619 (if (null (face-font face
))
620 (error "No matching fonts are the same height as the frame default font")
621 (error "No matching fonts are the same height as face `%s'" face
))
622 (if (null (face-font face
))
623 (error "Height of font `%s' doesn't match the frame default font"
625 (error "Height of font `%s' doesn't match face `%s'"
627 (error "No fonts match `%s'" pattern
)))
629 (cdr (assq 'font
(frame-parameters (selected-frame))))))
631 (defun x-frob-font-weight (font which
)
632 (cond ((string-match x-font-regexp font
)
633 (concat (substring font
0 (match-beginning x-font-regexp-weight-subnum
))
635 (substring font
(match-end x-font-regexp-weight-subnum
)
636 (match-beginning x-font-regexp-adstyle-subnum
))
637 ;; Replace the ADD_STYLE_NAME field with *
638 ;; because the info in it may not be the same
639 ;; for related fonts.
641 (substring font
(match-end x-font-regexp-adstyle-subnum
))))
642 ((or (string-match x-font-regexp-head font
)
643 (string-match x-font-regexp-weight font
))
644 (concat (substring font
0 (match-beginning 1)) which
645 (substring font
(match-end 1))))))
647 (defun x-frob-font-slant (font which
)
648 (cond ((string-match x-font-regexp font
)
649 (concat (substring font
0 (match-beginning x-font-regexp-slant-subnum
))
651 (substring font
(match-end x-font-regexp-slant-subnum
)
652 (match-beginning x-font-regexp-adstyle-subnum
))
653 ;; Replace the ADD_STYLE_NAME field with *
654 ;; because the info in it may not be the same
655 ;; for related fonts.
657 (substring font
(match-end x-font-regexp-adstyle-subnum
))))
658 ((or (string-match x-font-regexp-head font
)
659 (string-match x-font-regexp-slant font
))
660 (concat (substring font
0 (match-beginning 1)) which
661 (substring font
(match-end 1))))))
663 (defun x-make-font-bold (font)
664 "Given an X font specification, make a bold version of it.
665 If that can't be done, return nil."
666 (x-frob-font-weight font
"bold"))
668 (defun x-make-font-demibold (font)
669 "Given an X font specification, make a demibold version of it.
670 If that can't be done, return nil."
671 (x-frob-font-weight font
"demibold"))
673 (defun x-make-font-unbold (font)
674 "Given an X font specification, make a non-bold version of it.
675 If that can't be done, return nil."
676 (x-frob-font-weight font
"medium"))
678 (defun x-make-font-italic (font)
679 "Given an X font specification, make an italic version of it.
680 If that can't be done, return nil."
681 (x-frob-font-slant font
"i"))
683 (defun x-make-font-oblique (font) ; you say tomayto...
684 "Given an X font specification, make an oblique version of it.
685 If that can't be done, return nil."
686 (x-frob-font-slant font
"o"))
688 (defun x-make-font-unitalic (font)
689 "Given an X font specification, make a non-italic version of it.
690 If that can't be done, return nil."
691 (x-frob-font-slant font
"r"))
693 ;;; non-X-specific interface
695 (defun make-face-bold (face &optional frame noerror
)
696 "Make the font of the given face be bold, if possible.
697 If NOERROR is non-nil, return nil on failure."
698 (interactive (list (read-face-name "Make which face bold: ")))
699 (if (and (eq frame t
) (listp (face-font face t
)))
700 (set-face-font face
(if (memq 'italic
(face-font face t
))
701 '(bold italic
) '(bold))
705 (let ((frames (frame-list)))
706 ;; Make this face bold in global-face-data.
707 (make-face-bold face t noerror
)
708 ;; Make this face bold in each frame.
710 (make-face-bold 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
)
718 (face-font 'default frame
)
719 (cdr (assq 'font
(frame-parameters frame
)))))
720 (or (and font
(make-face-bold-internal face frame font
))
721 ;; We failed to find a bold version of the font.
723 (error "No bold version of %S" font
))))))
725 (defun make-face-bold-internal (face frame font
)
727 (or (and (setq f2
(x-make-font-bold font
))
728 (internal-try-face-font face f2 frame
))
729 (and (setq f2
(x-make-font-demibold font
))
730 (internal-try-face-font face f2 frame
)))))
732 (defun make-face-italic (face &optional frame noerror
)
733 "Make the font of the given face be italic, if possible.
734 If NOERROR is non-nil, return nil on failure."
735 (interactive (list (read-face-name "Make which face italic: ")))
736 (if (and (eq frame t
) (listp (face-font face t
)))
737 (set-face-font face
(if (memq 'bold
(face-font face t
))
738 '(bold italic
) '(italic))
742 (let ((frames (frame-list)))
743 ;; Make this face italic in global-face-data.
744 (make-face-italic face t noerror
)
745 ;; Make this face italic in each frame.
747 (make-face-italic face
(car frames
) noerror
)
748 (setq frames
(cdr frames
))))
749 (setq face
(internal-get-face face frame
))
750 (setq font
(or (face-font face frame
)
755 (face-font 'default frame
)
756 (cdr (assq 'font
(frame-parameters frame
)))))
757 (or (and font
(make-face-italic-internal face frame font
))
758 ;; We failed to find an italic version of the font.
760 (error "No italic version of %S" font
))))))
762 (defun make-face-italic-internal (face frame font
)
764 (or (and (setq f2
(x-make-font-italic font
))
765 (internal-try-face-font face f2 frame
))
766 (and (setq f2
(x-make-font-oblique font
))
767 (internal-try-face-font face f2 frame
)))))
769 (defun make-face-bold-italic (face &optional frame noerror
)
770 "Make the font of the given face be bold and italic, if possible.
771 If NOERROR is non-nil, return nil on failure."
772 (interactive (list (read-face-name "Make which face bold-italic: ")))
773 (if (and (eq frame t
) (listp (face-font face t
)))
774 (set-face-font face
'(bold italic
) t
)
777 (let ((frames (frame-list)))
778 ;; Make this face bold-italic in global-face-data.
779 (make-face-bold-italic face t noerror
)
780 ;; Make this face bold in each frame.
782 (make-face-bold-italic face
(car frames
) noerror
)
783 (setq frames
(cdr frames
))))
784 (setq face
(internal-get-face face frame
))
785 (setq font
(or (face-font face frame
)
790 (face-font 'default frame
)
791 (cdr (assq 'font
(frame-parameters frame
)))))
792 (or (and font
(make-face-bold-italic-internal face frame font
))
793 ;; We failed to find a bold italic version.
795 (error "No bold italic version of %S" font
))))))
797 (defun make-face-bold-italic-internal (face frame font
)
799 (or (and (setq f2
(x-make-font-italic font
))
800 (not (equal font f2
))
801 (setq f3
(x-make-font-bold f2
))
803 (internal-try-face-font face f3 frame
))
804 (and (setq f2
(x-make-font-oblique font
))
805 (not (equal font f2
))
806 (setq f3
(x-make-font-bold f2
))
808 (internal-try-face-font face f3 frame
))
809 (and (setq f2
(x-make-font-italic font
))
810 (not (equal font f2
))
811 (setq f3
(x-make-font-demibold f2
))
813 (internal-try-face-font face f3 frame
))
814 (and (setq f2
(x-make-font-oblique font
))
815 (not (equal font f2
))
816 (setq f3
(x-make-font-demibold f2
))
818 (internal-try-face-font face f3 frame
)))))
820 (defun make-face-unbold (face &optional frame noerror
)
821 "Make the font of the given face be non-bold, if possible.
822 If NOERROR is non-nil, return nil on failure."
823 (interactive (list (read-face-name "Make which face non-bold: ")))
824 (if (and (eq frame t
) (listp (face-font face t
)))
825 (set-face-font face
(if (memq 'italic
(face-font face t
))
830 (let ((frames (frame-list)))
831 ;; Make this face unbold in global-face-data.
832 (make-face-unbold face t noerror
)
833 ;; Make this face unbold in each frame.
835 (make-face-unbold face
(car frames
) noerror
)
836 (setq frames
(cdr frames
))))
837 (setq face
(internal-get-face face frame
))
838 (setq font1
(or (face-font face frame
)
842 (setq font1
(or font1
843 (face-font 'default frame
)
844 (cdr (assq 'font
(frame-parameters frame
)))))
845 (setq font
(and font1
(x-make-font-unbold font1
)))
846 (or (if font
(internal-try-face-font face font frame
))
848 (error "No unbold version of %S" font1
))))))
850 (defun make-face-unitalic (face &optional frame noerror
)
851 "Make the font of the given face be non-italic, if possible.
852 If NOERROR is non-nil, return nil on failure."
853 (interactive (list (read-face-name "Make which face non-italic: ")))
854 (if (and (eq frame t
) (listp (face-font face t
)))
855 (set-face-font face
(if (memq 'bold
(face-font face t
))
860 (let ((frames (frame-list)))
861 ;; Make this face unitalic in global-face-data.
862 (make-face-unitalic face t noerror
)
863 ;; Make this face unitalic in each frame.
865 (make-face-unitalic face
(car frames
) noerror
)
866 (setq frames
(cdr frames
))))
867 (setq face
(internal-get-face face frame
))
868 (setq font1
(or (face-font face frame
)
872 (setq font1
(or font1
873 (face-font 'default frame
)
874 (cdr (assq 'font
(frame-parameters frame
)))))
875 (setq font
(and font1
(x-make-font-unitalic font1
)))
876 (or (if font
(internal-try-face-font face font frame
))
878 (error "No unitalic version of %S" font1
))))))
880 (defvar list-faces-sample-text
881 "abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ"
882 "*Text string to display as the sample text for `list-faces-display'.")
884 ;; The name list-faces would be more consistent, but let's avoid a conflict
885 ;; with Lucid, which uses that name differently.
886 (defun list-faces-display ()
887 "List all faces, using the same sample text in each.
888 The sample text is a string that comes from the variable
889 `list-faces-sample-text'.
891 It is possible to give a particular face name different appearances in
892 different frames. This command shows the appearance in the
895 (let ((faces (sort (face-list) (function string-lessp
)))
897 (frame (selected-frame))
899 (with-output-to-temp-buffer "*Faces*"
901 (set-buffer standard-output
)
902 (setq truncate-lines t
)
904 (setq face
(car faces
))
905 (setq faces
(cdr faces
))
906 (insert (format "%25s " (symbol-name face
)))
908 (insert list-faces-sample-text
)
910 (put-text-property beg
(1- (point)) 'face face
)
911 ;; If the sample text has multiple lines, line up all of them.
917 (goto-char (point-min))))
918 ;; If the *Faces* buffer appears in a different frame,
919 ;; copy all the face definitions from FRAME,
920 ;; so that the display will reflect the frame that was selected.
921 (setq window
(get-buffer-window (get-buffer "*Faces*") t
))
922 (setq disp-frame
(if window
(window-frame window
)
924 (or (eq frame disp-frame
)
925 (let ((faces (face-list)))
927 (copy-face (car faces
) (car faces
) frame disp-frame
)
928 (setq faces
(cdr faces
)))))))
930 (defun describe-face (face)
931 "Display the properties of face FACE."
932 (interactive (list (read-face-name "Describe face: ")))
933 (with-output-to-temp-buffer "*Help*"
934 (princ "Properties of face `")
935 (princ (face-name face
))
936 (princ "':") (terpri)
937 (princ "Foreground: ") (princ (face-foreground face
)) (terpri)
938 (princ "Background: ") (princ (face-background face
)) (terpri)
939 (princ " Font: ") (princ (face-font face
)) (terpri)
940 (princ "Underlined: ") (princ (if (face-underline-p face
) "yes" "no")) (terpri)
941 (princ " Stipple: ") (princ (or (face-stipple face
) "none"))))
943 ;;; Make the standard faces.
944 ;;; The C code knows the default and modeline faces as faces 0 and 1,
945 ;;; so they must be the first two faces made.
946 (defun face-initialize ()
948 (make-face 'modeline
)
949 (make-face 'highlight
)
951 ;; These aren't really special in any way, but they're nice to have around.
955 (make-face 'bold-italic
)
957 (make-face 'secondary-selection
)
958 (make-face 'underline
)
960 (setq region-face
(face-id 'region
))
962 ;; Specify the global properties of these faces
963 ;; so they will come out right on new frames.
965 (make-face-bold 'bold t
)
966 (make-face-italic 'italic t
)
967 (make-face-bold-italic 'bold-italic t
)
969 (set-face-background 'highlight
'("darkseagreen2" "green" t
) t
)
970 (set-face-background 'region
'("gray" underline
) t
)
971 (set-face-background 'secondary-selection
'("paleturquoise" "green" t
) t
)
972 (set-face-background 'modeline
'(t) t
)
973 (set-face-underline-p 'underline t t
)
975 ;; Set up the faces of all existing X Window frames
976 ;; from those global properties, unless already set in a given frame.
978 (let ((frames (frame-list)))
980 (if (not (memq (framep (car frames
)) '(t nil
)))
981 (let ((frame (car frames
))
982 (rest global-face-data
))
984 (let ((face (car (car rest
))))
985 (or (face-differs-from-default-p face
)
986 (face-fill-in face
(cdr (car rest
)) frame
)))
987 (setq rest
(cdr rest
)))))
988 (setq frames
(cdr frames
)))))
991 ;; Like x-create-frame but also set up the faces.
993 (defun x-create-frame-with-faces (&optional parameters
)
994 ;; Read this frame's geometry resource, if it has an explicit name,
995 ;; and put the specs into PARAMETERS.
996 (let* ((name (or (cdr (assq 'name parameters
))
997 (cdr (assq 'name default-frame-alist
))))
998 (x-resource-name name
)
999 (res-geometry (if name
(x-get-resource "geometry" "Geometry")))
1003 (setq parsed
(x-parse-geometry res-geometry
))
1004 ;; If the resource specifies a position,
1005 ;; call the position and size "user-specified".
1006 (if (or (assq 'top parsed
) (assq 'left parsed
))
1007 (setq parsed
(cons '(user-position . t
)
1008 (cons '(user-size . t
) parsed
))))
1009 ;; Put the geometry parameters at the end.
1010 ;; Copy default-frame-alist so that they go after it.
1011 (setq parameters
(append parameters
1015 (if (null global-face-data
)
1016 (setq frame
(x-create-frame parameters
))
1017 (let* ((visibility-spec (assq 'visibility parameters
))
1018 (faces (copy-alist global-face-data
))
1021 (setq frame
(x-create-frame (cons '(visibility . nil
) parameters
)))
1024 (set-frame-face-alist frame faces
)
1026 (if (cdr (or (assq 'reverse parameters
)
1027 (assq 'reverse default-frame-alist
)
1028 (let ((resource (x-get-resource "reverseVideo"
1031 (cons nil
(member (downcase resource
)
1032 '("on" "true")))))))
1033 (let* ((params (frame-parameters frame
))
1034 (bg (cdr (assq 'foreground-color params
)))
1035 (fg (cdr (assq 'background-color params
))))
1036 (modify-frame-parameters frame
1037 (list (cons 'foreground-color fg
)
1038 (cons 'background-color bg
)))
1039 (if (equal bg
(cdr (assq 'border-color params
)))
1040 (modify-frame-parameters frame
1041 (list (cons 'border-color fg
))))
1042 (if (equal bg
(cdr (assq 'mouse-color params
)))
1043 (modify-frame-parameters frame
1044 (list (cons 'mouse-color fg
))))
1045 (if (equal bg
(cdr (assq 'cursor-color params
)))
1046 (modify-frame-parameters frame
1047 (list (cons 'cursor-color fg
))))))
1048 ;; Copy the vectors that represent the faces.
1049 ;; Also fill them in from X resources.
1051 (let ((global (cdr (car rest
))))
1052 (setcdr (car rest
) (vector 'face
1053 (face-name (cdr (car rest
)))
1054 (face-id (cdr (car rest
)))
1055 nil nil nil nil nil
))
1056 (face-fill-in (car (car rest
)) global frame
))
1057 (make-face-x-resource-internal (cdr (car rest
)) frame t
)
1058 (setq rest
(cdr rest
)))
1059 (if (null visibility-spec
)
1060 (make-frame-visible frame
)
1061 (modify-frame-parameters frame
(list visibility-spec
)))
1064 (delete-frame frame
)))))
1065 ;; Set up the background-mode frame parameter
1066 ;; so that programs can decide good ways of highlighting
1068 (let ((bg-resource (x-get-resource ".backgroundMode"
1070 (params (frame-parameters frame
))
1073 (cond (bg-resource (intern (downcase bg-resource
)))
1074 ((< (apply '+ (x-color-values
1075 (cdr (assq 'background-color params
))
1077 (/ (apply '+ (x-color-values "white" frame
)) 3))
1080 (modify-frame-parameters frame
1081 (list (cons 'background-mode bg-mode
)
1083 (cond ((x-display-color-p frame
)
1085 ((x-display-grayscale-p frame
)
1090 ;; Update a frame's faces when we change its default font.
1091 (defun frame-update-faces (frame)
1092 (let* ((faces global-face-data
)
1095 (let* ((face (car (car rest
)))
1096 (font (face-font face t
)))
1098 (let ((bold (memq 'bold font
))
1099 (italic (memq 'italic font
)))
1100 ;; Ignore any previous (string-valued) font, it might not even
1101 ;; be the right size anymore.
1102 (set-face-font face nil frame
)
1103 (cond ((and bold italic
)
1104 (make-face-bold-italic face frame t
))
1106 (make-face-bold face frame t
))
1108 (make-face-italic face frame t
)))))
1109 (setq rest
(cdr rest
)))
1112 ;; Update the colors of FACE, after FRAME's own colors have been changed.
1113 ;; This applies only to faces with global color specifications
1114 ;; that are not simple constants.
1115 (defun frame-update-face-colors (frame)
1116 (let ((faces global-face-data
))
1119 (let* ((data (cdr (car faces
)))
1120 (face (car (car faces
)))
1121 (foreground (face-foreground data
))
1122 (background (face-background data
)))
1123 ;; If the global spec is a specific color,
1124 ;; which doesn't depend on the frame's attributes,
1125 ;; we don't need to recalculate it now.
1126 (or (listp foreground
)
1127 (setq foreground nil
))
1128 (or (listp background
)
1129 (setq background nil
))
1130 ;; If we are going to frob this face at all,
1131 ;; reinitialize it first.
1132 (if (or foreground background
)
1133 (progn (set-face-foreground face nil frame
)
1134 (set-face-background face nil frame
)))
1136 (face-try-color-list 'set-face-foreground
1137 face foreground frame
))
1139 (face-try-color-list 'set-face-background
1140 face background frame
)))
1142 (setq faces
(cdr faces
)))))
1144 ;; Fill in the face FACE from frame-independent face data DATA.
1145 ;; DATA should be the non-frame-specific ("global") face vector
1146 ;; for the face. FACE should be a face name or face object.
1147 ;; FRAME is the frame to act on; it must be an actual frame, not nil or t.
1148 (defun face-fill-in (face data frame
)
1150 (let ((foreground (face-foreground data
))
1151 (background (face-background data
))
1152 (font (face-font data
))
1153 (stipple (face-stipple data
)))
1154 (set-face-underline-p face
(face-underline-p data
) frame
)
1156 (face-try-color-list 'set-face-foreground
1157 face foreground frame
))
1159 (face-try-color-list 'set-face-background
1160 face background frame
))
1162 (let ((bold (memq 'bold font
))
1163 (italic (memq 'italic font
)))
1164 (cond ((and bold italic
)
1165 (make-face-bold-italic face frame
))
1167 (make-face-bold face frame
))
1169 (make-face-italic face frame
))))
1171 (set-face-font face font frame
)))
1173 (set-face-stipple face stipple frame
)))
1176 ;; Assuming COLOR is a valid color name,
1177 ;; return t if it can be displayed on FRAME.
1178 (defun face-color-supported-p (frame color background-p
)
1179 (or (x-display-color-p frame
)
1180 ;; A black-and-white display can implement these.
1181 (member color
'("black" "white"))
1182 ;; A black-and-white display can fake gray for background.
1184 (face-color-gray-p color frame
))
1185 ;; A grayscale display can implement colors that are gray (more or less).
1186 (and (x-display-grayscale-p frame
)
1187 (face-color-gray-p color frame
))))
1189 ;; Use FUNCTION to store a color in FACE on FRAME.
1190 ;; COLORS is either a single color or a list of colors.
1191 ;; If it is a list, try the colors one by one until one of them
1192 ;; succeeds. We signal an error only if all the colors failed.
1193 ;; t as COLORS or as an element of COLORS means to invert the face.
1194 ;; That can't fail, so any subsequent elements after the t are ignored.
1195 (defun face-try-color-list (function face colors frame
)
1196 (if (stringp colors
)
1197 (if (face-color-supported-p frame colors
1198 (eq function
'set-face-background
))
1199 (funcall function face colors frame
))
1201 (invert-face face frame
)
1203 (while (and colors
(not done
))
1204 (if (or (memq (car colors
) '(t underline
))
1205 (face-color-supported-p frame
(car colors
)
1206 (eq function
'set-face-background
)))
1208 ;; If there are more colors to try, catch errors
1209 ;; and set `done' if we succeed.
1212 (cond ((eq (car colors
) t
)
1213 (invert-face face frame
))
1214 ((eq (car colors
) 'underline
)
1215 (set-face-underline-p face t frame
))
1217 (funcall function face
(car colors
) frame
)))
1220 ;; If this is the last color, let the error get out if it fails.
1221 ;; If it succeeds, we will exit anyway after this iteration.
1222 (cond ((eq (car colors
) t
)
1223 (invert-face face frame
))
1224 ((eq (car colors
) 'underline
)
1225 (set-face-underline-p face t frame
))
1227 (funcall function face
(car colors
) frame
)))))
1228 (setq colors
(cdr colors
)))))))
1230 ;; If we are already using x-window frames, initialize faces for them.
1231 (if (eq (framep (selected-frame)) 'x
)
1236 ;;; faces.el ends here