1 ;;; faces.el --- Lisp interface to the c "face" structure
3 ;; Copyright (C) 1992, 1993, 1994, 1995, 1996 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 the
19 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 ;; Boston, MA 02111-1307, USA.
24 ;; Mostly derived from Lucid.
29 ;; These used to be defsubsts, now they're subrs. Avoid losing if we're
30 ;; being compiled with an old Emacs that still has defsubrs in it.
31 (put 'face-name
'byte-optimizer nil
)
32 (put 'face-id
'byte-optimizer nil
)
33 (put 'face-font
'byte-optimizer nil
)
34 (put 'face-foreground
'byte-optimizer nil
)
35 (put 'face-background
'byte-optimizer nil
)
36 (put 'face-stipple
'byte-optimizer nil
)
37 (put 'face-underline-p
'byte-optimizer nil
)
38 (put 'set-face-font
'byte-optimizer nil
)
39 (put 'set-face-foreground
'byte-optimizer nil
)
40 (put 'set-face-background
'byte-optimizer nil
)
41 (put 'set-face-stipple
'byte-optimizer nil
)
42 (put 'set-face-underline-p
'byte-optimizer nil
))
44 ;;;; Functions for manipulating face vectors.
46 ;;; A face vector is a vector of the form:
47 ;;; [face NAME ID FONT FOREGROUND BACKGROUND STIPPLE UNDERLINE]
50 (defsubst internal-facep
(x)
51 (and (vectorp x
) (= (length x
) 8) (eq (aref x
0) 'face
)))
54 "Return t if X is a face name or an internal face vector."
55 (and (or (internal-facep x
)
56 (and (symbolp x
) (assq x global-face-data
)))
59 (defmacro internal-check-face
(face)
60 (` (or (internal-facep (, face
))
61 (signal 'wrong-type-argument
(list 'internal-facep
(, face
))))))
64 (defun face-name (face)
65 "Return the name of face FACE."
66 (aref (internal-get-face face
) 1))
69 "Return the internal ID number of face FACE."
70 (aref (internal-get-face face
) 2))
72 (defun face-font (face &optional frame
)
73 "Return the font name of face FACE, or nil if it is unspecified.
74 If the optional argument FRAME is given, report on face FACE in that frame.
75 If FRAME is t, report on the defaults for face FACE (for new frames).
76 The font default for a face is either nil, or a list
77 of the form (bold), (italic) or (bold italic).
78 If FRAME is omitted or nil, use the selected frame."
79 (aref (internal-get-face face frame
) 3))
81 (defun face-foreground (face &optional frame
)
82 "Return the foreground color name of face FACE, or nil if unspecified.
83 If the optional argument FRAME is given, report on face FACE in that frame.
84 If FRAME is t, report on the defaults for face FACE (for new frames).
85 If FRAME is omitted or nil, use the selected frame."
86 (aref (internal-get-face face frame
) 4))
88 (defun face-background (face &optional frame
)
89 "Return the background color name of face FACE, or nil if unspecified.
90 If the optional argument FRAME is given, report on face FACE in that frame.
91 If FRAME is t, report on the defaults for face FACE (for new frames).
92 If FRAME is omitted or nil, use the selected frame."
93 (aref (internal-get-face face frame
) 5))
95 (defun face-stipple (face &optional frame
)
96 "Return the stipple pixmap name of face FACE, or nil if unspecified.
97 If the optional argument FRAME is given, report on face FACE in that frame.
98 If FRAME is t, report on the defaults for face FACE (for new frames).
99 If FRAME is omitted or nil, use the selected frame."
100 (aref (internal-get-face face frame
) 6))
102 (defalias 'face-background-pixmap
'face-stipple
)
104 (defun face-underline-p (face &optional frame
)
105 "Return t if face FACE is underlined.
106 If the optional argument FRAME is given, report on face FACE in that frame.
107 If FRAME is t, report on the defaults for face FACE (for new frames).
108 If FRAME is omitted or nil, use the selected frame."
109 (aref (internal-get-face face frame
) 7))
114 (defun set-face-font (face font
&optional frame
)
115 "Change the font of face FACE to FONT (a string).
116 If the optional FRAME argument is provided, change only
117 in that frame; otherwise change each frame."
118 (interactive (internal-face-interactive "font"))
119 (if (stringp font
) (setq font
(x-resolve-font-name font
'default frame
)))
120 (internal-set-face-1 face
'font font
3 frame
))
122 (defun set-face-foreground (face color
&optional frame
)
123 "Change the foreground color of face FACE to COLOR (a string).
124 If the optional FRAME argument is provided, change only
125 in that frame; otherwise change each frame."
126 (interactive (internal-face-interactive "foreground"))
127 (internal-set-face-1 face
'foreground color
4 frame
))
129 (defvar face-default-stipple
"gray3"
130 "Default stipple pattern used on monochrome displays.
131 This stipple pattern is used on monochrome displays
132 instead of shades of gray for a face background color.
133 See `set-face-stipple' for possible values for this variable.")
135 (defun face-color-gray-p (color &optional frame
)
136 "Return t if COLOR is a shade of gray (or white or black).
137 FRAME specifies the frame and thus the display for interpreting COLOR."
138 (let* ((values (x-color-values color frame
))
143 (< (abs (- r g
)) (/ (max 1 (abs r
) (abs g
)) 20))
144 (< (abs (- g b
)) (/ (max 1 (abs g
) (abs b
)) 20))
145 (< (abs (- b r
)) (/ (max 1 (abs b
) (abs r
)) 20)))))
147 (defun set-face-background (face color
&optional frame
)
148 "Change the background color of face FACE to COLOR (a string).
149 If the optional FRAME argument is provided, change only
150 in that frame; otherwise change each frame."
151 (interactive (internal-face-interactive "background"))
152 ;; For a specific frame, use gray stipple instead of gray color
153 ;; if the display does not support a gray color.
154 (if (and frame
(not (eq frame t
)) color
155 ;; Check for support for foreground, not for background!
156 ;; face-color-supported-p is smart enough to know
157 ;; that grays are "supported" as background
158 ;; because we are supposed to use stipple for them!
159 (not (face-color-supported-p frame color nil
)))
160 (set-face-stipple face face-default-stipple frame
)
162 (let ((frames (frame-list)))
164 (set-face-background (face-name face
) color
(car frames
))
165 (setq frames
(cdr frames
)))
166 (set-face-background face color t
)
168 (internal-set-face-1 face
'background color
5 frame
))))
170 (defun set-face-stipple (face pixmap
&optional frame
)
171 "Change the stipple pixmap of face FACE to PIXMAP.
172 PIXMAP should be a string, the name of a file of pixmap data.
173 The directories listed in the `x-bitmap-file-path' variable are searched.
175 Alternatively, PIXMAP may be a list of the form (WIDTH HEIGHT DATA)
176 where WIDTH and HEIGHT are the size in pixels,
177 and DATA is a string, containing the raw bits of the bitmap.
179 If the optional FRAME argument is provided, change only
180 in that frame; otherwise change each frame."
181 (interactive (internal-face-interactive-stipple "stipple"))
182 (internal-set-face-1 face
'background-pixmap pixmap
6 frame
))
184 (defalias 'set-face-background-pixmap
'set-face-stipple
)
186 (defun set-face-underline-p (face underline-p
&optional frame
)
187 "Specify whether face FACE is underlined. (Yes if UNDERLINE-P is non-nil.)
188 If the optional FRAME argument is provided, change only
189 in that frame; otherwise change each frame."
190 (interactive (internal-face-interactive "underline-p" "underlined"))
191 (internal-set-face-1 face
'underline underline-p
7 frame
))
193 (defun modify-face-read-string (face default name alist
)
197 (format "Set face %s %s (default %s): "
198 face name
(downcase default
))
199 (format "Set face %s %s: " face name
))
201 (cond ((equal value
"none")
207 (defun modify-face (face foreground background stipple
208 bold-p italic-p underline-p
&optional frame
)
209 "Change the display attributes for face FACE.
210 If the optional FRAME argument is provided, change only
211 in that frame; otherwise change each frame.
213 FOREGROUND and BACKGROUND should be a colour name string (or list of strings to
214 try) or nil. STIPPLE should be a stipple pattern name string or nil.
215 If nil, means do not change the display attribute corresponding to that arg.
217 BOLD-P, ITALIC-P, and UNDERLINE-P specify whether the face should be set bold,
218 in italic, and underlined, respectively. If neither nil or t, means do not
219 change the display attribute corresponding to that arg.
221 If called interactively, prompts for a face name and face attributes."
223 (let* ((completion-ignore-case t
)
224 (face (symbol-name (read-face-name "Modify face: ")))
225 (colors (mapcar 'list x-colors
))
226 (stipples (mapcar 'list
(apply 'nconc
227 (mapcar 'directory-files
228 x-bitmap-file-path
))))
229 (foreground (modify-face-read-string
230 face
(face-foreground (intern face
))
231 "foreground" colors
))
232 (background (modify-face-read-string
233 face
(face-background (intern face
))
234 "background" colors
))
235 ;; If the stipple value is a list (WIDTH HEIGHT DATA),
236 ;; represent that as a string by printing it out.
238 (if (stringp (face-stipple (intern face
)))
239 (face-stipple (intern face
))
240 (if (face-stipple (intern face
))
241 (prin1-to-string (face-stipple (intern face
))))))
243 (modify-face-read-string
244 face old-stipple-string
246 ;; Convert the stipple value text we read
247 ;; back to a list if it looks like one.
248 ;; This makes the assumption that a pixmap file name
249 ;; won't start with an open-paren.
251 (and new-stipple-string
252 (if (string-match "^(" new-stipple-string
)
253 (read new-stipple-string
)
254 new-stipple-string
)))
255 (bold-p (y-or-n-p (concat "Should face " face
" be bold ")))
256 (italic-p (y-or-n-p (concat "Should face " face
" be italic ")))
257 (underline-p (y-or-n-p (concat "Should face " face
" be underlined ")))
258 (all-frames-p (y-or-n-p (concat "Modify face " face
" in all frames "))))
259 (message "Face %s: %s" face
262 (list (and foreground
(concat (downcase foreground
) " foreground"))
263 (and background
(concat (downcase background
) " background"))
264 (and stipple
(concat (downcase new-stipple-string
) " stipple"))
265 (and bold-p
"bold") (and italic-p
"italic")
266 (and underline-p
"underline"))) ", "))
267 (list (intern face
) foreground background stipple
268 bold-p italic-p underline-p
269 (if all-frames-p nil
(selected-frame)))))
271 (face-try-color-list 'set-face-foreground face foreground frame
)
274 (face-try-color-list 'set-face-background face background frame
)
277 (set-face-stipple face stipple frame
)
279 (cond ((eq bold-p nil
) (make-face-unbold face frame t
))
280 ((eq bold-p t
) (make-face-bold face frame t
)))
281 (cond ((eq italic-p nil
) (make-face-unitalic face frame t
))
282 ((eq italic-p t
) (make-face-italic face frame t
)))
283 (if (memq underline-p
'(nil t
))
284 (set-face-underline-p face underline-p frame
))
285 (and (interactive-p) (redraw-display)))
287 ;;;; Associating face names (symbols) with their face vectors.
289 (defvar global-face-data nil
290 "Internal data for face support functions. Not for external use.
291 This is an alist associating face names with the default values for
292 their parameters. Newly created frames get their data from here.")
295 "Returns a list of all defined face names."
296 (mapcar 'car global-face-data
))
298 (defun internal-find-face (name &optional frame
)
299 "Retrieve the face named NAME. Return nil if there is no such face.
300 If the optional argument FRAME is given, this gets the face NAME for
301 that frame; otherwise, it uses the selected frame.
302 If FRAME is the symbol t, then the global, non-frame face is returned.
303 If NAME is already a face, it is simply returned."
304 (if (and (eq frame t
) (not (symbolp name
)))
305 (setq name
(face-name name
)))
310 (frame-face-alist (or frame
(selected-frame))))))
311 (internal-check-face name
)
314 (defun internal-get-face (name &optional frame
)
315 "Retrieve the face named NAME; error if there is none.
316 If the optional argument FRAME is given, this gets the face NAME for
317 that frame; otherwise, it uses the selected frame.
318 If FRAME is the symbol t, then the global, non-frame face is returned.
319 If NAME is already a face, it is simply returned."
320 (or (internal-find-face name frame
)
321 (internal-check-face name
)))
324 (defun internal-set-face-1 (face name value index frame
)
325 (let ((inhibit-quit t
))
327 (let ((frames (frame-list)))
329 (internal-set-face-1 (face-name face
) name value index
(car frames
))
330 (setq frames
(cdr frames
)))
331 (aset (internal-get-face (if (symbolp face
) face
(face-name face
)) t
)
335 (set-face-attribute-internal (face-id face
) name value frame
))
336 (aset (internal-get-face face frame
) index value
))))
339 (defun read-face-name (prompt)
341 (while (= (length face
) 0)
342 (setq face
(completing-read prompt
343 (mapcar '(lambda (x) (list (symbol-name x
)))
348 (defun internal-face-interactive (what &optional bool
)
349 (let* ((fn (intern (concat "face-" what
)))
350 (prompt (concat "Set " what
" of face"))
351 (face (read-face-name (concat prompt
": ")))
352 (default (if (fboundp fn
)
353 (or (funcall fn face
(selected-frame))
354 (funcall fn
'default
(selected-frame)))))
356 (y-or-n-p (concat "Should face " (symbol-name face
)
358 (read-string (concat prompt
" " (symbol-name face
) " to: ")
360 (list face
(if (equal value
"") nil value
))))
362 (defun internal-face-interactive-stipple (what)
363 (let* ((fn (intern (concat "face-" what
)))
364 (prompt (concat "Set " what
" of face"))
365 (face (read-face-name (concat prompt
": ")))
366 (default (if (fboundp fn
)
367 (or (funcall fn face
(selected-frame))
368 (funcall fn
'default
(selected-frame)))))
369 ;; If the stipple value is a list (WIDTH HEIGHT DATA),
370 ;; represent that as a string by printing it out.
372 (if (stringp (face-stipple face
))
374 (if (null (face-stipple face
))
376 (prin1-to-string (face-stipple face
)))))
379 (concat prompt
" " (symbol-name face
) " to: ")
381 ;; Convert the stipple value text we read
382 ;; back to a list if it looks like one.
383 ;; This makes the assumption that a pixmap file name
384 ;; won't start with an open-paren.
386 (if (string-match "^(" new-stipple-string
)
387 (read new-stipple-string
)
388 new-stipple-string
)))
389 (list face
(if (equal stipple
"") nil stipple
))))
391 (defun make-face (name)
392 "Define a new FACE on all frames.
393 You can modify the font, color, etc of this face with the set-face- functions.
394 If the face already exists, it is unmodified."
395 (interactive "SMake face: ")
396 (or (internal-find-face name
)
397 (let ((face (make-vector 8 nil
)))
400 (let* ((frames (frame-list))
402 (id (internal-next-face-id)))
403 (make-face-internal id
)
406 (set-frame-face-alist (car frames
)
407 (cons (cons name
(copy-sequence face
))
408 (frame-face-alist (car frames
))))
409 (setq frames
(cdr frames
)))
410 (setq global-face-data
(cons (cons name face
) global-face-data
)))
411 ;; when making a face after frames already exist
412 (if (or (eq window-system
'x
) (eq window-system
'win32
))
413 (make-face-x-resource-internal face
))
415 (if (fboundp 'facemenu-add-new-face
)
416 (facemenu-add-new-face name
))
420 ;; Fill in a face by default based on X resources, for all existing frames.
421 ;; This has to be done when a new face is made.
422 (defun make-face-x-resource-internal (face &optional frame set-anyway
)
424 (let ((frames (frame-list)))
426 (if (or (eq (framep (car frames
)) 'x
) (eq (framep (car frames
)) 'win32
))
427 (make-face-x-resource-internal (face-name face
)
428 (car frames
) set-anyway
))
429 (setq frames
(cdr frames
)))))
431 (setq face
(internal-get-face (face-name face
) frame
))
433 ;; These are things like "attributeForeground" instead of simply
434 ;; "foreground" because people tend to do things like "*foreground",
435 ;; which would cause all faces to be fully qualified, making faces
436 ;; inherit attributes in a non-useful way. So we've made them slightly
437 ;; less obvious to specify in order to make them work correctly in
438 ;; more random environments.
440 ;; I think these should be called "face.faceForeground" instead of
441 ;; "face.attributeForeground", but they're the way they are for
442 ;; hysterical reasons.
444 (let* ((name (symbol-name (face-name face
)))
445 (fn (or (x-get-resource (concat name
".attributeFont")
446 "Face.AttributeFont")
447 (and set-anyway
(face-font face
))))
448 (fg (or (x-get-resource (concat name
".attributeForeground")
449 "Face.AttributeForeground")
450 (and set-anyway
(face-foreground face
))))
451 (bg (or (x-get-resource (concat name
".attributeBackground")
452 "Face.AttributeBackground")
453 (and set-anyway
(face-background face
))))
454 (bgp (or (x-get-resource (concat name
".attributeStipple")
455 "Face.AttributeStipple")
456 (x-get-resource (concat name
".attributeBackgroundPixmap")
457 "Face.AttributeBackgroundPixmap")
458 (and set-anyway
(face-stipple face
))))
459 (ulp (let ((resource (x-get-resource
460 (concat name
".attributeUnderline")
461 "Face.AttributeUnderline")))
463 (member (downcase resource
) '("on" "true"))
464 (and set-anyway
(face-underline-p face
)))))
468 (cond ((string= fn
"italic")
469 (make-face-italic face
))
471 (make-face-bold face
))
472 ((string= fn
"bold-italic")
473 (make-face-bold-italic face
))
475 (set-face-font face fn frame
)))
477 (if (member fn
'("italic" "bold" "bold-italic"))
478 (message "no %s version found for face `%s'" fn name
)
479 (message "font `%s' not found for face `%s'" fn name
)))))
482 (set-face-foreground face fg frame
)
483 (error (message "color `%s' not allocated for face `%s'" fg name
))))
486 (set-face-background face bg frame
)
487 (error (message "color `%s' not allocated for face `%s'" bg name
))))
490 (set-face-stipple face bgp frame
)
491 (error (message "pixmap `%s' not found for face `%s'" bgp name
))))
492 (if (or ulp set-anyway
)
493 (set-face-underline-p face ulp frame
))
497 (defun copy-face (old-face new-face
&optional frame new-frame
)
498 "Define a face just like OLD-FACE, with name NEW-FACE.
499 If NEW-FACE already exists as a face, it is modified to be like OLD-FACE.
500 If it doesn't already exist, it is created.
502 If the optional argument FRAME is given as a frame,
503 NEW-FACE is changed on FRAME only.
504 If FRAME is t, the frame-independent default specification for OLD-FACE
505 is copied to NEW-FACE.
506 If FRAME is nil, copying is done for the frame-independent defaults
507 and for each existing frame.
508 If the optional fourth argument NEW-FRAME is given,
509 copy the information from face OLD-FACE on frame FRAME
510 to NEW-FACE on frame NEW-FRAME."
511 (or new-frame
(setq new-frame frame
))
512 (let ((inhibit-quit t
))
514 (let ((frames (frame-list)))
516 (copy-face old-face new-face
(car frames
))
517 (setq frames
(cdr frames
)))
518 (copy-face old-face new-face t
))
519 (setq old-face
(internal-get-face old-face frame
))
520 (setq new-face
(or (internal-find-face new-face new-frame
)
521 (make-face new-face
)))
523 ;; A face that has a global symbolic font modifier such as `bold'
524 ;; might legitimately get an error here.
525 ;; Use the frame's default font in that case.
526 (set-face-font new-face
(face-font old-face frame
) new-frame
)
528 (set-face-font new-face nil new-frame
)))
529 (set-face-foreground new-face
(face-foreground old-face frame
) new-frame
)
530 (set-face-background new-face
(face-background old-face frame
) new-frame
)
531 (set-face-stipple new-face
532 (face-stipple old-face frame
)
534 (set-face-underline-p new-face
(face-underline-p old-face frame
)
538 (defun face-equal (face1 face2
&optional frame
)
539 "True if the faces FACE1 and FACE2 display in the same way."
540 (setq face1
(internal-get-face face1 frame
)
541 face2
(internal-get-face face2 frame
))
542 (and (equal (face-foreground face1 frame
) (face-foreground face2 frame
))
543 (equal (face-background face1 frame
) (face-background face2 frame
))
544 (equal (face-font face1 frame
) (face-font face2 frame
))
545 (eq (face-underline-p face1 frame
) (face-underline-p face2 frame
))
546 (equal (face-stipple face1 frame
)
547 (face-stipple face2 frame
))))
549 (defun face-differs-from-default-p (face &optional frame
)
550 "True if face FACE displays differently from the default face, on FRAME.
551 A face is considered to be ``the same'' as the default face if it is
552 actually specified in the same way (equivalent fonts, etc) or if it is
553 fully unspecified, and thus inherits the attributes of any face it
554 is displayed on top of.
556 The optional argument FRAME specifies which frame to test;
557 if FRAME is t, test the default for new frames.
558 If FRAME is nil or omitted, test the selected frame."
559 (let ((default (internal-get-face 'default frame
)))
560 (setq face
(internal-get-face face frame
))
561 (not (and (or (equal (face-foreground default frame
)
562 (face-foreground face frame
))
563 (null (face-foreground face frame
)))
564 (or (equal (face-background default frame
)
565 (face-background face frame
))
566 (null (face-background face frame
)))
567 (or (equal (face-font default frame
) (face-font face frame
))
568 (null (face-font face frame
)))
569 (or (equal (face-stipple default frame
)
570 (face-stipple face frame
))
571 (null (face-stipple face frame
)))
572 (equal (face-underline-p default frame
)
573 (face-underline-p face frame
))
576 (defun face-nontrivial-p (face &optional frame
)
577 "True if face FACE has some non-nil attribute.
578 The optional argument FRAME specifies which frame to test;
579 if FRAME is t, test the default for new frames.
580 If FRAME is nil or omitted, test the selected frame."
581 (setq face
(internal-get-face face frame
))
582 (or (face-foreground face frame
)
583 (face-background face frame
)
584 (face-font face frame
)
585 (face-stipple face frame
)
586 (face-underline-p face frame
)))
589 (defun invert-face (face &optional frame
)
590 "Swap the foreground and background colors of face FACE.
591 If the face doesn't specify both foreground and background, then
592 set its foreground and background to the default background and foreground."
593 (interactive (list (read-face-name "Invert face: ")))
594 (setq face
(internal-get-face face frame
))
595 (let ((fg (face-foreground face frame
))
596 (bg (face-background face frame
)))
599 (set-face-foreground face bg frame
)
600 (set-face-background face fg frame
))
601 (set-face-foreground face
(or (face-background 'default frame
)
602 (cdr (assq 'background-color
(frame-parameters frame
))))
604 (set-face-background face
(or (face-foreground 'default frame
)
605 (cdr (assq 'foreground-color
(frame-parameters frame
))))
610 (defun internal-try-face-font (face font
&optional frame
)
611 "Like set-face-font, but returns nil on failure instead of an error."
613 (set-face-font face font frame
)
616 ;; Manipulating font names.
618 (defconst x-font-regexp nil
)
619 (defconst x-font-regexp-head nil
)
620 (defconst x-font-regexp-weight nil
)
621 (defconst x-font-regexp-slant nil
)
623 (defconst x-font-regexp-weight-subnum
1)
624 (defconst x-font-regexp-slant-subnum
2)
625 (defconst x-font-regexp-swidth-subnum
3)
626 (defconst x-font-regexp-adstyle-subnum
4)
628 ;;; Regexps matching font names in "Host Portable Character Representation."
633 (weight "\\(bold\\|demibold\\|medium\\)") ; 1
634 ; (weight\? "\\(\\*\\|bold\\|demibold\\|medium\\|\\)") ; 1
635 (weight\? "\\([^-]*\\)") ; 1
636 (slant "\\([ior]\\)") ; 2
637 ; (slant\? "\\([ior?*]?\\)") ; 2
638 (slant\? "\\([^-]?\\)") ; 2
639 ; (swidth "\\(\\*\\|normal\\|semicondensed\\|\\)") ; 3
640 (swidth "\\([^-]*\\)") ; 3
641 ; (adstyle "\\(\\*\\|sans\\|\\)") ; 4
642 (adstyle "\\([^-]*\\)") ; 4
644 (pointsize "[0-9][0-9]+")
653 (concat "\\`\\*?[-?*]"
654 foundry - family - weight
\? - slant
\? - swidth - adstyle -
655 pixelsize - pointsize - resx - resy - spacing - avgwidth -
656 registry - encoding
"\\*?\\'"
658 (setq x-font-regexp-head
659 (concat "\\`[-?*]" foundry - family - weight
\? - slant
\?
660 "\\([-*?]\\|\\'\\)"))
661 (setq x-font-regexp-slant
(concat - slant -
))
662 (setq x-font-regexp-weight
(concat - weight -
))
665 (defun x-resolve-font-name (pattern &optional face frame
)
666 "Return a font name matching PATTERN.
667 All wildcards in PATTERN become substantiated.
668 If PATTERN is nil, return the name of the frame's base font, which never
670 Given optional arguments FACE and FRAME, return a font which is
671 also the same size as FACE on FRAME, or fail."
673 (setq face
(face-name face
)))
677 ;; Note that x-list-fonts has code to handle a face with nil as its font.
678 (let ((fonts (x-list-fonts pattern face frame
)))
681 (if (string-match "\\*" pattern
)
682 (if (null (face-font face
))
683 (error "No matching fonts are the same height as the frame default font")
684 (error "No matching fonts are the same height as face `%s'" face
))
685 (if (null (face-font face
))
686 (error "Height of font `%s' doesn't match the frame default font"
688 (error "Height of font `%s' doesn't match face `%s'"
690 (error "No fonts match `%s'" pattern
)))
692 (cdr (assq 'font
(frame-parameters (selected-frame))))))
694 (defun x-frob-font-weight (font which
)
695 (let ((case-fold-search t
))
696 (cond ((string-match x-font-regexp font
)
697 (concat (substring font
0
698 (match-beginning x-font-regexp-weight-subnum
))
700 (substring font
(match-end x-font-regexp-weight-subnum
)
701 (match-beginning x-font-regexp-adstyle-subnum
))
702 ;; Replace the ADD_STYLE_NAME field with *
703 ;; because the info in it may not be the same
704 ;; for related fonts.
706 (substring font
(match-end x-font-regexp-adstyle-subnum
))))
707 ((string-match x-font-regexp-head font
)
708 (concat (substring font
0 (match-beginning 1)) which
709 (substring font
(match-end 1))))
710 ((string-match x-font-regexp-weight font
)
711 (concat (substring font
0 (match-beginning 1)) which
712 (substring font
(match-end 1)))))))
714 (defun x-frob-font-slant (font which
)
715 (let ((case-fold-search t
))
716 (cond ((string-match x-font-regexp font
)
717 (concat (substring font
0
718 (match-beginning x-font-regexp-slant-subnum
))
720 (substring font
(match-end x-font-regexp-slant-subnum
)
721 (match-beginning x-font-regexp-adstyle-subnum
))
722 ;; Replace the ADD_STYLE_NAME field with *
723 ;; because the info in it may not be the same
724 ;; for related fonts.
726 (substring font
(match-end x-font-regexp-adstyle-subnum
))))
727 ((string-match x-font-regexp-head font
)
728 (concat (substring font
0 (match-beginning 2)) which
729 (substring font
(match-end 2))))
730 ((string-match x-font-regexp-slant font
)
731 (concat (substring font
0 (match-beginning 1)) which
732 (substring font
(match-end 1)))))))
734 (defun x-make-font-bold (font)
735 "Given an X font specification, make a bold version of it.
736 If that can't be done, return nil."
737 (x-frob-font-weight font
"bold"))
739 (defun x-make-font-demibold (font)
740 "Given an X font specification, make a demibold version of it.
741 If that can't be done, return nil."
742 (x-frob-font-weight font
"demibold"))
744 (defun x-make-font-unbold (font)
745 "Given an X font specification, make a non-bold version of it.
746 If that can't be done, return nil."
747 (x-frob-font-weight font
"medium"))
749 (defun x-make-font-italic (font)
750 "Given an X font specification, make an italic version of it.
751 If that can't be done, return nil."
752 (x-frob-font-slant font
"i"))
754 (defun x-make-font-oblique (font) ; you say tomayto...
755 "Given an X font specification, make an oblique version of it.
756 If that can't be done, return nil."
757 (x-frob-font-slant font
"o"))
759 (defun x-make-font-unitalic (font)
760 "Given an X font specification, make a non-italic version of it.
761 If that can't be done, return nil."
762 (x-frob-font-slant font
"r"))
764 ;;; non-X-specific interface
766 (defun make-face-bold (face &optional frame noerror
)
767 "Make the font of the given face be bold, if possible.
768 If NOERROR is non-nil, return nil on failure."
769 (interactive (list (read-face-name "Make which face bold: ")))
770 (if (and (eq frame t
) (listp (face-font face t
)))
771 (set-face-font face
(if (memq 'italic
(face-font face t
))
772 '(bold italic
) '(bold))
776 (let ((frames (frame-list)))
777 ;; Make this face bold in global-face-data.
778 (make-face-bold face t noerror
)
779 ;; Make this face bold in each frame.
781 (make-face-bold face
(car frames
) noerror
)
782 (setq frames
(cdr frames
))))
783 (setq face
(internal-get-face face frame
))
784 (setq font
(or (face-font face frame
)
789 (face-font 'default frame
)
790 (cdr (assq 'font
(frame-parameters frame
)))))
791 (or (and font
(make-face-bold-internal face frame font
))
792 ;; We failed to find a bold version of the font.
794 (error "No bold version of %S" font
))))))
796 (defun make-face-bold-internal (face frame font
)
798 (or (and (setq f2
(x-make-font-bold font
))
799 (internal-try-face-font face f2 frame
))
800 (and (setq f2
(x-make-font-demibold font
))
801 (internal-try-face-font face f2 frame
)))))
803 (defun make-face-italic (face &optional frame noerror
)
804 "Make the font of the given face be italic, if possible.
805 If NOERROR is non-nil, return nil on failure."
806 (interactive (list (read-face-name "Make which face italic: ")))
807 (if (and (eq frame t
) (listp (face-font face t
)))
808 (set-face-font face
(if (memq 'bold
(face-font face t
))
809 '(bold italic
) '(italic))
813 (let ((frames (frame-list)))
814 ;; Make this face italic in global-face-data.
815 (make-face-italic face t noerror
)
816 ;; Make this face italic in each frame.
818 (make-face-italic face
(car frames
) noerror
)
819 (setq frames
(cdr frames
))))
820 (setq face
(internal-get-face face frame
))
821 (setq font
(or (face-font face frame
)
826 (face-font 'default frame
)
827 (cdr (assq 'font
(frame-parameters frame
)))))
828 (or (and font
(make-face-italic-internal face frame font
))
829 ;; We failed to find an italic version of the font.
831 (error "No italic version of %S" font
))))))
833 (defun make-face-italic-internal (face frame font
)
835 (or (and (setq f2
(x-make-font-italic font
))
836 (internal-try-face-font face f2 frame
))
837 (and (setq f2
(x-make-font-oblique font
))
838 (internal-try-face-font face f2 frame
)))))
840 (defun make-face-bold-italic (face &optional frame noerror
)
841 "Make the font of the given face be bold and italic, if possible.
842 If NOERROR is non-nil, return nil on failure."
843 (interactive (list (read-face-name "Make which face bold-italic: ")))
844 (if (and (eq frame t
) (listp (face-font face t
)))
845 (set-face-font face
'(bold italic
) t
)
848 (let ((frames (frame-list)))
849 ;; Make this face bold-italic in global-face-data.
850 (make-face-bold-italic face t noerror
)
851 ;; Make this face bold in each frame.
853 (make-face-bold-italic face
(car frames
) noerror
)
854 (setq frames
(cdr frames
))))
855 (setq face
(internal-get-face face frame
))
856 (setq font
(or (face-font face frame
)
861 (face-font 'default frame
)
862 (cdr (assq 'font
(frame-parameters frame
)))))
863 (or (and font
(make-face-bold-italic-internal face frame font
))
864 ;; We failed to find a bold italic version.
866 (error "No bold italic version of %S" font
))))))
868 (defun make-face-bold-italic-internal (face frame font
)
870 (or (and (setq f2
(x-make-font-italic font
))
871 (not (equal font f2
))
872 (setq f3
(x-make-font-bold f2
))
874 (internal-try-face-font face f3 frame
))
875 (and (setq f2
(x-make-font-oblique font
))
876 (not (equal font f2
))
877 (setq f3
(x-make-font-bold f2
))
879 (internal-try-face-font face f3 frame
))
880 (and (setq f2
(x-make-font-italic font
))
881 (not (equal font f2
))
882 (setq f3
(x-make-font-demibold f2
))
884 (internal-try-face-font face f3 frame
))
885 (and (setq f2
(x-make-font-oblique font
))
886 (not (equal font f2
))
887 (setq f3
(x-make-font-demibold f2
))
889 (internal-try-face-font face f3 frame
)))))
891 (defun make-face-unbold (face &optional frame noerror
)
892 "Make the font of the given face be non-bold, if possible.
893 If NOERROR is non-nil, return nil on failure."
894 (interactive (list (read-face-name "Make which face non-bold: ")))
895 (if (and (eq frame t
) (listp (face-font face t
)))
896 (set-face-font face
(if (memq 'italic
(face-font face t
))
901 (let ((frames (frame-list)))
902 ;; Make this face unbold in global-face-data.
903 (make-face-unbold face t noerror
)
904 ;; Make this face unbold in each frame.
906 (make-face-unbold face
(car frames
) noerror
)
907 (setq frames
(cdr frames
))))
908 (setq face
(internal-get-face face frame
))
909 (setq font1
(or (face-font face frame
)
913 (setq font1
(or font1
914 (face-font 'default frame
)
915 (cdr (assq 'font
(frame-parameters frame
)))))
916 (setq font
(and font1
(x-make-font-unbold font1
)))
917 (or (if font
(internal-try-face-font face font frame
))
919 (error "No unbold version of %S" font1
))))))
921 (defun make-face-unitalic (face &optional frame noerror
)
922 "Make the font of the given face be non-italic, if possible.
923 If NOERROR is non-nil, return nil on failure."
924 (interactive (list (read-face-name "Make which face non-italic: ")))
925 (if (and (eq frame t
) (listp (face-font face t
)))
926 (set-face-font face
(if (memq 'bold
(face-font face t
))
931 (let ((frames (frame-list)))
932 ;; Make this face unitalic in global-face-data.
933 (make-face-unitalic face t noerror
)
934 ;; Make this face unitalic in each frame.
936 (make-face-unitalic face
(car frames
) noerror
)
937 (setq frames
(cdr frames
))))
938 (setq face
(internal-get-face face frame
))
939 (setq font1
(or (face-font face frame
)
943 (setq font1
(or font1
944 (face-font 'default frame
)
945 (cdr (assq 'font
(frame-parameters frame
)))))
946 (setq font
(and font1
(x-make-font-unitalic font1
)))
947 (or (if font
(internal-try-face-font face font frame
))
949 (error "No unitalic version of %S" font1
))))))
951 (defvar list-faces-sample-text
952 "abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ"
953 "*Text string to display as the sample text for `list-faces-display'.")
955 ;; The name list-faces would be more consistent, but let's avoid a conflict
956 ;; with Lucid, which uses that name differently.
957 (defun list-faces-display ()
958 "List all faces, using the same sample text in each.
959 The sample text is a string that comes from the variable
960 `list-faces-sample-text'.
962 It is possible to give a particular face name different appearances in
963 different frames. This command shows the appearance in the
966 (let ((faces (sort (face-list) (function string-lessp
)))
968 (frame (selected-frame))
970 (with-output-to-temp-buffer "*Faces*"
972 (set-buffer standard-output
)
973 (setq truncate-lines t
)
975 (setq face
(car faces
))
976 (setq faces
(cdr faces
))
977 (insert (format "%25s " (symbol-name face
)))
979 (insert list-faces-sample-text
)
981 (put-text-property beg
(1- (point)) 'face face
)
982 ;; If the sample text has multiple lines, line up all of them.
988 (goto-char (point-min))))
989 ;; If the *Faces* buffer appears in a different frame,
990 ;; copy all the face definitions from FRAME,
991 ;; so that the display will reflect the frame that was selected.
992 (setq window
(get-buffer-window (get-buffer "*Faces*") t
))
993 (setq disp-frame
(if window
(window-frame window
)
995 (or (eq frame disp-frame
)
996 (let ((faces (face-list)))
998 (copy-face (car faces
) (car faces
) frame disp-frame
)
999 (setq faces
(cdr faces
)))))))
1001 (defun describe-face (face)
1002 "Display the properties of face FACE."
1003 (interactive (list (read-face-name "Describe face: ")))
1004 (with-output-to-temp-buffer "*Help*"
1005 (princ "Properties of face `")
1006 (princ (face-name face
))
1007 (princ "':") (terpri)
1008 (princ "Foreground: ") (princ (face-foreground face
)) (terpri)
1009 (princ "Background: ") (princ (face-background face
)) (terpri)
1010 (princ " Font: ") (princ (face-font face
)) (terpri)
1011 (princ "Underlined: ") (princ (if (face-underline-p face
) "yes" "no")) (terpri)
1012 (princ " Stipple: ") (princ (or (face-stipple face
) "none"))))
1014 ;;; Make the standard faces.
1015 ;;; The C code knows the default and modeline faces as faces 0 and 1,
1016 ;;; so they must be the first two faces made.
1017 (defun face-initialize ()
1018 (make-face 'default
)
1019 (make-face 'modeline
)
1020 (make-face 'highlight
)
1022 ;; These aren't really special in any way, but they're nice to have around.
1026 (make-face 'bold-italic
)
1028 (make-face 'secondary-selection
)
1029 (make-face 'underline
)
1031 (setq region-face
(face-id 'region
))
1033 ;; Specify the global properties of these faces
1034 ;; so they will come out right on new frames.
1036 (make-face-bold 'bold t
)
1037 (make-face-italic 'italic t
)
1038 (make-face-bold-italic 'bold-italic t
)
1040 (set-face-background 'highlight
'("darkseagreen2" "green" t
) t
)
1041 (set-face-background 'region
'("gray" underline
) t
)
1042 (set-face-background 'secondary-selection
'("paleturquoise" "green" t
) t
)
1043 (set-face-background 'modeline
'(t) t
)
1044 (set-face-underline-p 'underline t t
)
1046 ;; Set up the faces of all existing X Window frames
1047 ;; from those global properties, unless already set in a given frame.
1049 (let ((frames (frame-list)))
1051 (if (not (memq (framep (car frames
)) '(t nil
)))
1052 (let ((frame (car frames
))
1053 (rest global-face-data
))
1055 (let ((face (car (car rest
))))
1056 (or (face-differs-from-default-p face
)
1057 (face-fill-in face
(cdr (car rest
)) frame
)))
1058 (setq rest
(cdr rest
)))))
1059 (setq frames
(cdr frames
)))))
1062 ;; Like x-create-frame but also set up the faces.
1064 (defun x-create-frame-with-faces (&optional parameters
)
1065 ;; Read this frame's geometry resource, if it has an explicit name,
1066 ;; and put the specs into PARAMETERS.
1067 (let* ((name (or (cdr (assq 'name parameters
))
1068 (cdr (assq 'name default-frame-alist
))))
1069 (x-resource-name name
)
1070 (res-geometry (if name
(x-get-resource "geometry" "Geometry")))
1074 (setq parsed
(x-parse-geometry res-geometry
))
1075 ;; If the resource specifies a position,
1076 ;; call the position and size "user-specified".
1077 (if (or (assq 'top parsed
) (assq 'left parsed
))
1078 (setq parsed
(cons '(user-position . t
)
1079 (cons '(user-size . t
) parsed
))))
1080 ;; Put the geometry parameters at the end.
1081 ;; Copy default-frame-alist so that they go after it.
1082 (setq parameters
(append parameters
1086 (if (null global-face-data
)
1087 (setq frame
(x-create-frame parameters
))
1088 (let* ((visibility-spec (assq 'visibility parameters
))
1089 (faces (copy-alist global-face-data
))
1092 (setq frame
(x-create-frame (cons '(visibility . nil
) parameters
)))
1095 (set-frame-face-alist frame faces
)
1097 (if (cdr (or (assq 'reverse parameters
)
1098 (assq 'reverse default-frame-alist
)
1099 (let ((resource (x-get-resource "reverseVideo"
1102 (cons nil
(member (downcase resource
)
1103 '("on" "true")))))))
1104 (let* ((params (frame-parameters frame
))
1105 (bg (cdr (assq 'foreground-color params
)))
1106 (fg (cdr (assq 'background-color params
))))
1107 (modify-frame-parameters frame
1108 (list (cons 'foreground-color fg
)
1109 (cons 'background-color bg
)))
1110 (if (equal bg
(cdr (assq 'border-color params
)))
1111 (modify-frame-parameters frame
1112 (list (cons 'border-color fg
))))
1113 (if (equal bg
(cdr (assq 'mouse-color params
)))
1114 (modify-frame-parameters frame
1115 (list (cons 'mouse-color fg
))))
1116 (if (equal bg
(cdr (assq 'cursor-color params
)))
1117 (modify-frame-parameters frame
1118 (list (cons 'cursor-color fg
))))))
1119 ;; Copy the vectors that represent the faces.
1120 ;; Also fill them in from X resources.
1122 (let ((global (cdr (car rest
))))
1123 (setcdr (car rest
) (vector 'face
1124 (face-name (cdr (car rest
)))
1125 (face-id (cdr (car rest
)))
1126 nil nil nil nil nil
))
1127 (face-fill-in (car (car rest
)) global frame
))
1128 (make-face-x-resource-internal (cdr (car rest
)) frame t
)
1129 (setq rest
(cdr rest
)))
1130 (if (null visibility-spec
)
1131 (make-frame-visible frame
)
1132 (modify-frame-parameters frame
(list visibility-spec
)))
1135 (delete-frame frame
)))))
1136 ;; Set up the background-mode frame parameter
1137 ;; so that programs can decide good ways of highlighting
1139 (let ((bg-resource (x-get-resource ".backgroundMode"
1141 (params (frame-parameters frame
))
1144 (cond (bg-resource (intern (downcase bg-resource
)))
1145 ((< (apply '+ (x-color-values
1146 (cdr (assq 'background-color params
))
1148 ;; Just looking at the screen,
1149 ;; colors whose values add up to .6 of the white total
1150 ;; still look dark to me.
1151 (* (apply '+ (x-color-values "white" frame
)) .6))
1154 (modify-frame-parameters frame
1155 (list (cons 'background-mode bg-mode
)
1157 (cond ((x-display-color-p frame
)
1159 ((x-display-grayscale-p frame
)
1164 ;; Update a frame's faces when we change its default font.
1165 (defun frame-update-faces (frame)
1166 (let* ((faces global-face-data
)
1169 (let* ((face (car (car rest
)))
1170 (font (face-font face t
)))
1172 (let ((bold (memq 'bold font
))
1173 (italic (memq 'italic font
)))
1174 ;; Ignore any previous (string-valued) font, it might not even
1175 ;; be the right size anymore.
1176 (set-face-font face nil frame
)
1177 (cond ((and bold italic
)
1178 (make-face-bold-italic face frame t
))
1180 (make-face-bold face frame t
))
1182 (make-face-italic face frame t
)))))
1183 (setq rest
(cdr rest
)))
1186 ;; Update the colors of FACE, after FRAME's own colors have been changed.
1187 ;; This applies only to faces with global color specifications
1188 ;; that are not simple constants.
1189 (defun frame-update-face-colors (frame)
1190 (let ((faces global-face-data
))
1193 (let* ((data (cdr (car faces
)))
1194 (face (car (car faces
)))
1195 (foreground (face-foreground data
))
1196 (background (face-background data
)))
1197 ;; If the global spec is a specific color,
1198 ;; which doesn't depend on the frame's attributes,
1199 ;; we don't need to recalculate it now.
1200 (or (listp foreground
)
1201 (setq foreground nil
))
1202 (or (listp background
)
1203 (setq background nil
))
1204 ;; If we are going to frob this face at all,
1205 ;; reinitialize it first.
1206 (if (or foreground background
)
1207 (progn (set-face-foreground face nil frame
)
1208 (set-face-background face nil frame
)))
1210 (face-try-color-list 'set-face-foreground
1211 face foreground frame
))
1213 (face-try-color-list 'set-face-background
1214 face background frame
)))
1216 (setq faces
(cdr faces
)))))
1218 ;; Fill in the face FACE from frame-independent face data DATA.
1219 ;; DATA should be the non-frame-specific ("global") face vector
1220 ;; for the face. FACE should be a face name or face object.
1221 ;; FRAME is the frame to act on; it must be an actual frame, not nil or t.
1222 (defun face-fill-in (face data frame
)
1224 (let ((foreground (face-foreground data
))
1225 (background (face-background data
))
1226 (font (face-font data
))
1227 (stipple (face-stipple data
)))
1228 (set-face-underline-p face
(face-underline-p data
) frame
)
1230 (face-try-color-list 'set-face-foreground
1231 face foreground frame
))
1233 (face-try-color-list 'set-face-background
1234 face background frame
))
1236 (let ((bold (memq 'bold font
))
1237 (italic (memq 'italic font
)))
1238 (cond ((and bold italic
)
1239 (make-face-bold-italic face frame
))
1241 (make-face-bold face frame
))
1243 (make-face-italic face frame
))))
1245 (set-face-font face font frame
)))
1247 (set-face-stipple face stipple frame
)))
1250 ;; Assuming COLOR is a valid color name,
1251 ;; return t if it can be displayed on FRAME.
1252 (defun face-color-supported-p (frame color background-p
)
1254 (or (x-display-color-p frame
)
1255 ;; A black-and-white display can implement these.
1256 (member color
'("black" "white"))
1257 ;; A black-and-white display can fake gray for background.
1259 (face-color-gray-p color frame
))
1260 ;; A grayscale display can implement colors that are gray (more or less).
1261 (and (x-display-grayscale-p frame
)
1262 (face-color-gray-p color frame
)))))
1264 ;; Use FUNCTION to store a color in FACE on FRAME.
1265 ;; COLORS is either a single color or a list of colors.
1266 ;; If it is a list, try the colors one by one until one of them
1267 ;; succeeds. We signal an error only if all the colors failed.
1268 ;; t as COLORS or as an element of COLORS means to invert the face.
1269 ;; That can't fail, so any subsequent elements after the t are ignored.
1270 (defun face-try-color-list (function face colors frame
)
1271 (if (stringp colors
)
1272 (if (face-color-supported-p frame colors
1273 (eq function
'set-face-background
))
1274 (funcall function face colors frame
))
1276 (invert-face face frame
)
1278 (while (and colors
(not done
))
1279 (if (or (memq (car colors
) '(t underline
))
1280 (face-color-supported-p frame
(car colors
)
1281 (eq function
'set-face-background
)))
1283 ;; If there are more colors to try, catch errors
1284 ;; and set `done' if we succeed.
1287 (cond ((eq (car colors
) t
)
1288 (invert-face face frame
))
1289 ((eq (car colors
) 'underline
)
1290 (set-face-underline-p face t frame
))
1292 (funcall function face
(car colors
) frame
)))
1295 ;; If this is the last color, let the error get out if it fails.
1296 ;; If it succeeds, we will exit anyway after this iteration.
1297 (cond ((eq (car colors
) t
)
1298 (invert-face face frame
))
1299 ((eq (car colors
) 'underline
)
1300 (set-face-underline-p face t frame
))
1302 (funcall function face
(car colors
) frame
)))))
1303 (setq colors
(cdr colors
)))))))
1305 ;; If we are already using x-window frames, initialize faces for them.
1306 (if (or (eq (framep (selected-frame)) 'x
) (eq (framep (selected-frame)) 'win32
))
1311 ;;; faces.el ends here