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