(byte-recompile-directory):
[bpt/emacs.git] / lisp / faces.el
1 ;;; faces.el --- Lisp interface to the c "face" structure
2
3 ;; Copyright (C) 1992, 1993, 1994, 1995, 1996 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 the
19 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 ;; Boston, MA 02111-1307, USA.
21
22 ;;; Commentary:
23
24 ;; Mostly derived from Lucid.
25
26 ;;; Code:
27
28 (eval-when-compile
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-font-explicit 'byte-optimizer nil)
35 (put 'face-foreground 'byte-optimizer nil)
36 (put 'face-background 'byte-optimizer nil)
37 (put 'face-stipple 'byte-optimizer nil)
38 (put 'face-underline-p 'byte-optimizer nil)
39 (put 'set-face-font 'byte-optimizer nil)
40 (put 'set-face-font-auto 'byte-optimizer nil)
41 (put 'set-face-foreground 'byte-optimizer nil)
42 (put 'set-face-background 'byte-optimizer nil)
43 (put 'set-face-stipple 'byte-optimizer nil)
44 (put 'set-face-underline-p 'byte-optimizer nil))
45 \f
46 ;;;; Functions for manipulating face vectors.
47
48 ;;; A face vector is a vector of the form:
49 ;;; [face NAME ID FONT FOREGROUND BACKGROUND STIPPLE
50 ;;; UNDERLINE-P INVERSE-VIDEO-P FONT-EXPLICIT-P BOLD-P ITALIC-P]
51
52 ;;; Type checkers.
53 (defsubst internal-facep (x)
54 (and (vectorp x) (= (length x) 12) (eq (aref x 0) 'face)))
55
56 (defun facep (x)
57 "Return t if X is a face name or an internal face vector."
58 (and (or (internal-facep x)
59 (and (symbolp x) (assq x global-face-data)))
60 t))
61
62 (defmacro internal-check-face (face)
63 (` (or (internal-facep (, face))
64 (signal 'wrong-type-argument (list 'internal-facep (, face))))))
65
66 ;;; Accessors.
67 (defun face-name (face)
68 "Return the name of face FACE."
69 (aref (internal-get-face face) 1))
70
71 (defun face-id (face)
72 "Return the internal ID number of face FACE."
73 (aref (internal-get-face face) 2))
74
75 (defun face-font (face &optional frame)
76 "Return the font name of face FACE, or nil if it is unspecified.
77 If the optional argument FRAME is given, report on face FACE in that frame.
78 If FRAME is t, report on the defaults for face FACE (for new frames).
79 The font default for a face is either nil, or a list
80 of the form (bold), (italic) or (bold italic).
81 If FRAME is omitted or nil, use the selected frame."
82 (aref (internal-get-face face frame) 3))
83
84 (defun face-foreground (face &optional frame)
85 "Return the foreground color name of face FACE, or nil if unspecified.
86 If the optional argument FRAME is given, report on face FACE in that frame.
87 If FRAME is t, report on the defaults for face FACE (for new frames).
88 If FRAME is omitted or nil, use the selected frame."
89 (aref (internal-get-face face frame) 4))
90
91 (defun face-background (face &optional frame)
92 "Return the background color name of face FACE, or nil if unspecified.
93 If the optional argument FRAME is given, report on face FACE in that frame.
94 If FRAME is t, report on the defaults for face FACE (for new frames).
95 If FRAME is omitted or nil, use the selected frame."
96 (aref (internal-get-face face frame) 5))
97
98 (defun face-stipple (face &optional frame)
99 "Return the stipple pixmap name of face FACE, or nil if unspecified.
100 If the optional argument FRAME is given, report on face FACE in that frame.
101 If FRAME is t, report on the defaults for face FACE (for new frames).
102 If FRAME is omitted or nil, use the selected frame."
103 (aref (internal-get-face face frame) 6))
104
105 (defalias 'face-background-pixmap 'face-stipple)
106
107 (defun face-underline-p (face &optional frame)
108 "Return t if face FACE is underlined.
109 If the optional argument FRAME is given, report on face FACE in that frame.
110 If FRAME is t, report on the defaults for face FACE (for new frames).
111 If FRAME is omitted or nil, use the selected frame."
112 (aref (internal-get-face face frame) 7))
113
114 (defun face-inverse-video-p (face &optional frame)
115 "Return t if face FACE is in inverse video.
116 If the optional argument FRAME is given, report on face FACE in that frame.
117 If FRAME is t, report on the defaults for face FACE (for new frames).
118 If FRAME is omitted or nil, use the selected frame."
119 (aref (internal-get-face face frame) 8))
120
121 (defun face-font-explicit (face &optional frame)
122 "Return non-nil if this face's font was explicitly specified."
123 (aref (internal-get-face face frame) 9))
124
125 (defun face-bold-p (face &optional frame)
126 "Return non-nil if the font of FACE is bold.
127 If the optional argument FRAME is given, report on face FACE in that frame.
128 If FRAME is t, report on the defaults for face FACE (for new frames).
129 If FRAME is omitted or nil, use the selected frame."
130 (aref (internal-get-face face frame) 10))
131
132 (defun face-italic-p (face &optional frame)
133 "Return non-nil if the font of FACE is italic.
134 If the optional argument FRAME is given, report on face FACE in that frame.
135 If FRAME is t, report on the defaults for face FACE (for new frames).
136 If FRAME is omitted or nil, use the selected frame."
137 (aref (internal-get-face face frame) 11))
138
139 (defalias 'face-doc-string 'face-documentation)
140 (defun face-documentation (face)
141 "Get the documentation string for FACE."
142 (get face 'face-documentation))
143 \f
144 ;;; Mutators.
145
146 (defun set-face-font (face font &optional frame)
147 "Change the font of face FACE to FONT (a string).
148 If the optional FRAME argument is provided, change only
149 in that frame; otherwise change each frame."
150 (interactive (internal-face-interactive "font"))
151 (if (stringp font)
152 (setq font (or (and (not (eq window-system 'w32))
153 (resolve-fontset-name font))
154 (x-resolve-font-name font 'default frame))))
155 (internal-set-face-1 face 'font font 3 frame)
156 ;; Record that this face's font was set explicitly, not automatically,
157 ;; unless we are setting it to nil.
158 (internal-set-face-1 face nil (not (null font)) 9 frame))
159
160 (defun set-face-font-auto (face font &optional frame)
161 "Change the font of face FACE to FONT (a string), for an automatic change.
162 An automatic change means that we don't change the \"explicit\" flag;
163 if the font was derived from the frame font before, it is now.
164 If the optional FRAME argument is provided, change only
165 in that frame; otherwise change each frame."
166 (interactive (internal-face-interactive "font"))
167 (if (stringp font)
168 (setq font (or (and (not (eq window-system 'w32))
169 (resolve-fontset-name font))
170 (x-resolve-font-name font 'default frame))))
171 (internal-set-face-1 face 'font font 3 frame))
172
173 (defun set-face-font-explicit (face flag &optional frame)
174 "Set the explicit-font flag of face FACE to FLAG.
175 If the optional FRAME argument is provided, change only
176 in that frame; otherwise change each frame."
177 (internal-set-face-1 face nil flag 9 frame))
178
179 (defun set-face-foreground (face color &optional frame)
180 "Change the foreground color of face FACE to COLOR (a string).
181 If the optional FRAME argument is provided, change only
182 in that frame; otherwise change each frame."
183 (interactive (internal-face-interactive "foreground" 'color))
184 (internal-set-face-1 face 'foreground color 4 frame))
185
186 (defvar face-default-stipple "gray3"
187 "Default stipple pattern used on monochrome displays.
188 This stipple pattern is used on monochrome displays
189 instead of shades of gray for a face background color.
190 See `set-face-stipple' for possible values for this variable.")
191
192 (defun face-color-gray-p (color &optional frame)
193 "Return t if COLOR is a shade of gray (or white or black).
194 FRAME specifies the frame and thus the display for interpreting COLOR."
195 (let* ((values (x-color-values color frame))
196 (r (nth 0 values))
197 (g (nth 1 values))
198 (b (nth 2 values)))
199 (and values
200 (< (abs (- r g)) (/ (max 1 (abs r) (abs g)) 20))
201 (< (abs (- g b)) (/ (max 1 (abs g) (abs b)) 20))
202 (< (abs (- b r)) (/ (max 1 (abs b) (abs r)) 20)))))
203
204 (defun set-face-background (face color &optional frame)
205 "Change the background color of face FACE to COLOR (a string).
206 If the optional FRAME argument is provided, change only
207 in that frame; otherwise change each frame."
208 (interactive (internal-face-interactive "background" 'color))
209 ;; For a specific frame, use gray stipple instead of gray color
210 ;; if the display does not support a gray color.
211 (if (and frame (not (eq frame t)) color
212 ;; Check for support for foreground, not for background!
213 ;; face-color-supported-p is smart enough to know
214 ;; that grays are "supported" as background
215 ;; because we are supposed to use stipple for them!
216 (not (face-color-supported-p frame color nil)))
217 (set-face-stipple face face-default-stipple frame)
218 (if (null frame)
219 (let ((frames (frame-list)))
220 (while frames
221 (set-face-background (face-name face) color (car frames))
222 (setq frames (cdr frames)))
223 (set-face-background face color t)
224 color)
225 (internal-set-face-1 face 'background color 5 frame))))
226
227 (defun set-face-stipple (face pixmap &optional frame)
228 "Change the stipple pixmap of face FACE to PIXMAP.
229 PIXMAP should be a string, the name of a file of pixmap data.
230 The directories listed in the `x-bitmap-file-path' variable are searched.
231
232 Alternatively, PIXMAP may be a list of the form (WIDTH HEIGHT DATA)
233 where WIDTH and HEIGHT are the size in pixels,
234 and DATA is a string, containing the raw bits of the bitmap.
235
236 If the optional FRAME argument is provided, change only
237 in that frame; otherwise change each frame."
238 (interactive (internal-face-interactive-stipple "stipple"))
239 (internal-set-face-1 face 'background-pixmap pixmap 6 frame))
240
241 (defalias 'set-face-background-pixmap 'set-face-stipple)
242
243 (defun set-face-underline-p (face underline-p &optional frame)
244 "Specify whether face FACE is underlined. (Yes if UNDERLINE-P is non-nil.)
245 If the optional FRAME argument is provided, change only
246 in that frame; otherwise change each frame."
247 (interactive (internal-face-interactive "underline-p" "underlined"))
248 (internal-set-face-1 face 'underline underline-p 7 frame))
249
250 (defun set-face-inverse-video-p (face inverse-video-p &optional frame)
251 "Specify whether face FACE is in inverse video.
252 \(Yes if INVERSE-VIDEO-P is non-nil.)
253 If the optional FRAME argument is provided, change only
254 in that frame; otherwise change each frame."
255 (interactive (internal-face-interactive "inverse-video-p" "inverse-video"))
256 (internal-set-face-1 face 'inverse-video inverse-video-p 8 frame))
257
258 (defun set-face-bold-p (face bold-p &optional frame)
259 "Specify whether face FACE is bold. (Yes if BOLD-P is non-nil.)
260 If the optional FRAME argument is provided, change only
261 in that frame; otherwise change each frame."
262 (cond ((eq bold-p nil) (make-face-unbold face frame t))
263 (t (make-face-bold face frame t))))
264
265 (defun set-face-italic-p (face italic-p &optional frame)
266 "Specify whether face FACE is italic. (Yes if ITALIC-P is non-nil.)
267 If the optional FRAME argument is provided, change only
268 in that frame; otherwise change each frame."
269 (cond ((eq italic-p nil) (make-face-unitalic face frame t))
270 (t (make-face-italic face frame t))))
271
272 (defalias 'set-face-doc-string 'set-face-documentation)
273 (defun set-face-documentation (face string)
274 "Set the documentation string for FACE to STRING."
275 (put face 'face-documentation string))
276 \f
277 (defun modify-face-read-string (face default name alist)
278 (let ((value
279 (completing-read
280 (if default
281 (format "Set face %s %s (default %s): "
282 face name (downcase default))
283 (format "Set face %s %s: " face name))
284 alist)))
285 (cond ((equal value "none")
286 nil)
287 ((equal value "")
288 default)
289 (t value))))
290
291 (defun modify-face (face foreground background stipple
292 bold-p italic-p underline-p &optional inverse-p frame)
293 "Change the display attributes for face FACE.
294 If the optional FRAME argument is provided, change only
295 in that frame; otherwise change each frame.
296
297 FOREGROUND and BACKGROUND should be a colour name string (or list of strings to
298 try) or nil. STIPPLE should be a stipple pattern name string or nil.
299 If nil, means do not change the display attribute corresponding to that arg.
300
301 BOLD-P, ITALIC-P, UNDERLINE-P, and INVERSE-P specify whether
302 the face should be set bold, italic, underlined or in inverse-video,
303 respectively. If one of these arguments is neither nil or t, it means do not
304 change the display attribute corresponding to that argument.
305
306 If called interactively, prompts for a face name and face attributes."
307 (interactive
308 (let* ((completion-ignore-case t)
309 (face (symbol-name (read-face-name "Modify face: ")))
310 (colors (mapcar 'list x-colors))
311 (stipples (mapcar 'list (apply 'nconc
312 (mapcar 'directory-files
313 x-bitmap-file-path))))
314 (foreground (modify-face-read-string
315 face (face-foreground (intern face))
316 "foreground" colors))
317 (background (modify-face-read-string
318 face (face-background (intern face))
319 "background" colors))
320 ;; If the stipple value is a list (WIDTH HEIGHT DATA),
321 ;; represent that as a string by printing it out.
322 (old-stipple-string
323 (if (stringp (face-stipple (intern face)))
324 (face-stipple (intern face))
325 (if (face-stipple (intern face))
326 (prin1-to-string (face-stipple (intern face))))))
327 (new-stipple-string
328 (modify-face-read-string
329 face old-stipple-string
330 "stipple" stipples))
331 ;; Convert the stipple value text we read
332 ;; back to a list if it looks like one.
333 ;; This makes the assumption that a pixmap file name
334 ;; won't start with an open-paren.
335 (stipple
336 (and new-stipple-string
337 (if (string-match "^(" new-stipple-string)
338 (read new-stipple-string)
339 new-stipple-string)))
340 (bold-p (y-or-n-p (concat "Should face " face " be bold ")))
341 (italic-p (y-or-n-p (concat "Should face " face " be italic ")))
342 (underline-p (y-or-n-p (concat "Should face " face " be underlined ")))
343 (inverse-p (y-or-n-p (concat "Should face " face " be inverse-video ")))
344 (all-frames-p (y-or-n-p (concat "Modify face " face " in all frames "))))
345 (message "Face %s: %s" face
346 (mapconcat 'identity
347 (delq nil
348 (list (and foreground (concat (downcase foreground) " foreground"))
349 (and background (concat (downcase background) " background"))
350 (and stipple (concat (downcase new-stipple-string) " stipple"))
351 (and bold-p "bold") (and italic-p "italic")
352 (and inverse-p "inverse")
353 (and underline-p "underline"))) ", "))
354 (list (intern face) foreground background stipple
355 bold-p italic-p underline-p inverse-p
356 (if all-frames-p nil (selected-frame)))))
357 ;; Clear this before we install the new foreground and background;
358 ;; otherwise, clearing it after would swap them!
359 (when (and (or foreground background) (face-inverse-video-p face))
360 (set-face-inverse-video-p face nil frame)
361 ;; Arrange to restore it after, if we are not setting it now.
362 (or (memq inverse-p '(t nil))
363 (setq inverse-p t)))
364 (condition-case nil
365 (face-try-color-list 'set-face-foreground face foreground frame)
366 (error nil))
367 (condition-case nil
368 (face-try-color-list 'set-face-background face background frame)
369 (error nil))
370 (condition-case nil
371 (set-face-stipple face stipple frame)
372 (error nil))
373 ;; Now that we have the new colors,
374 (if (memq inverse-p '(nil t))
375 (set-face-inverse-video-p face inverse-p frame))
376 (cond ((eq bold-p nil)
377 (if (face-font face frame)
378 (make-face-unbold face frame t)))
379 ((eq bold-p t)
380 (make-face-bold face frame t)))
381 (cond ((eq italic-p nil)
382 (if (face-font face frame)
383 (make-face-unitalic face frame t)))
384 ((eq italic-p t) (make-face-italic face frame t)))
385 (if (memq underline-p '(nil t))
386 (set-face-underline-p face underline-p frame))
387 (and (interactive-p) (redraw-display)))
388 \f
389 ;;;; Associating face names (symbols) with their face vectors.
390
391 (defvar global-face-data nil
392 "Internal data for face support functions. Not for external use.
393 This is an alist associating face names with the default values for
394 their parameters. Newly created frames get their data from here.")
395
396 (defun face-list ()
397 "Returns a list of all defined face names."
398 (mapcar 'car global-face-data))
399
400 (defun internal-find-face (name &optional frame)
401 "Retrieve the face named NAME. Return nil if there is no such face.
402 If the optional argument FRAME is given, this gets the face NAME for
403 that frame; otherwise, it uses the selected frame.
404 If FRAME is the symbol t, then the global, non-frame face is returned.
405 If NAME is already a face, it is simply returned."
406 (if (and (eq frame t) (not (symbolp name)))
407 (setq name (face-name name)))
408 (if (symbolp name)
409 (cdr (assq name
410 (if (eq frame t)
411 global-face-data
412 (frame-face-alist (or frame (selected-frame))))))
413 (internal-check-face name)
414 name))
415
416 (defun internal-get-face (name &optional frame)
417 "Retrieve the face named NAME; error if there is none.
418 If the optional argument FRAME is given, this gets the face NAME for
419 that frame; otherwise, it uses the selected frame.
420 If FRAME is the symbol t, then the global, non-frame face is returned.
421 If NAME is already a face, it is simply returned."
422 (or (internal-find-face name frame)
423 (internal-check-face name)))
424
425
426 (defun internal-set-face-1 (face name value index frame)
427 (let ((inhibit-quit t))
428 (if (null frame)
429 (let ((frames (frame-list)))
430 (while frames
431 (internal-set-face-1 (face-name face) name value index (car frames))
432 (setq frames (cdr frames)))
433 (aset (internal-get-face (if (symbolp face) face (face-name face)) t)
434 index value)
435 value)
436 (let ((internal-face (internal-get-face face frame)))
437 (or (eq frame t)
438 (if (eq name 'inverse-video)
439 (or (eq value (aref internal-face index))
440 (invert-face face frame))
441 (and name (fboundp 'set-face-attribute-internal)
442 (set-face-attribute-internal (face-id face)
443 name value frame))))
444 (aset internal-face index value)))))
445
446
447 (defun read-face-name (prompt)
448 (let (face)
449 (while (= (length face) 0)
450 (setq face (completing-read prompt
451 (mapcar '(lambda (x) (list (symbol-name x)))
452 (face-list))
453 nil t)))
454 (intern face)))
455
456 (defun internal-face-interactive (what &optional bool)
457 (let* ((fn (intern (concat "face-" what)))
458 (prompt (concat "Set " what " of face"))
459 (face (read-face-name (concat prompt ": ")))
460 (default (if (fboundp fn)
461 (or (funcall fn face (selected-frame))
462 (funcall fn 'default (selected-frame)))))
463 value)
464 (setq value
465 (cond ((eq bool 'color)
466 (completing-read (concat prompt " " (symbol-name face) " to: ")
467 (mapcar (function (lambda (color)
468 (cons color color)))
469 x-colors)
470 nil nil nil nil default))
471 (bool
472 (y-or-n-p (concat "Should face " (symbol-name face)
473 " be " bool "? ")))
474 (t
475 (read-string (concat prompt " " (symbol-name face) " to: ")
476 nil nil default))))
477 (list face (if (equal value "") nil value))))
478
479 (defun internal-face-interactive-stipple (what)
480 (let* ((fn (intern (concat "face-" what)))
481 (prompt (concat "Set " what " of face"))
482 (face (read-face-name (concat prompt ": ")))
483 (default (if (fboundp fn)
484 (or (funcall fn face (selected-frame))
485 (funcall fn 'default (selected-frame)))))
486 ;; If the stipple value is a list (WIDTH HEIGHT DATA),
487 ;; represent that as a string by printing it out.
488 (old-stipple-string
489 (if (stringp (face-stipple face))
490 (face-stipple face)
491 (if (null (face-stipple face))
492 nil
493 (prin1-to-string (face-stipple face)))))
494 (new-stipple-string
495 (read-string
496 (concat prompt " " (symbol-name face) " to: ")
497 old-stipple-string))
498 ;; Convert the stipple value text we read
499 ;; back to a list if it looks like one.
500 ;; This makes the assumption that a pixmap file name
501 ;; won't start with an open-paren.
502 (stipple
503 (if (string-match "^(" new-stipple-string)
504 (read new-stipple-string)
505 new-stipple-string)))
506 (list face (if (equal stipple "") nil stipple))))
507
508 (defun make-face (name &optional no-resources)
509 "Define a new FACE on all frames.
510 You can modify the font, color, etc of this face with the set-face- functions.
511 If NO-RESOURCES is non-nil, then we ignore X resources
512 and always make a face whose attributes are all nil.
513
514 If the face already exists, it is unmodified."
515 (interactive "SMake face: ")
516 (or (internal-find-face name)
517 (let ((face (make-vector 12 nil)))
518 (aset face 0 'face)
519 (aset face 1 name)
520 (let* ((frames (frame-list))
521 (inhibit-quit t)
522 (id (internal-next-face-id)))
523 (if (fboundp 'make-face-internal)
524 (make-face-internal id))
525 (aset face 2 id)
526 (while frames
527 (set-frame-face-alist (car frames)
528 (cons (cons name (copy-sequence face))
529 (frame-face-alist (car frames))))
530 (setq frames (cdr frames)))
531 (setq global-face-data (cons (cons name face) global-face-data)))
532 ;; When making a face after frames already exist
533 (or no-resources
534 (if (memq window-system '(x w32))
535 (make-face-x-resource-internal face)))
536 ;; Add to menu of faces.
537 (if (fboundp 'facemenu-add-new-face)
538 (facemenu-add-new-face name))
539 face))
540 name)
541
542 (defun make-empty-face (face)
543 "Define a new FACE on all frames, which initially reflects the defaults.
544 You can modify the font, color, etc of this face with the set-face- functions.
545 If the face already exists, it is unmodified."
546 (interactive "SMake empty face: ")
547 (make-face face t))
548
549 ;; Fill in a face by default based on X resources, for all existing frames.
550 ;; This has to be done when a new face is made.
551 (defun make-face-x-resource-internal (face &optional frame set-anyway)
552 (cond ((null frame)
553 (let ((frames (frame-list)))
554 (while frames
555 (if (memq (framep (car frames)) '(x w32))
556 (make-face-x-resource-internal (face-name face)
557 (car frames) set-anyway))
558 (setq frames (cdr frames)))))
559 (t
560 (setq face (internal-get-face (face-name face) frame))
561 ;;
562 ;; These are things like "attributeForeground" instead of simply
563 ;; "foreground" because people tend to do things like "*foreground",
564 ;; which would cause all faces to be fully qualified, making faces
565 ;; inherit attributes in a non-useful way. So we've made them slightly
566 ;; less obvious to specify in order to make them work correctly in
567 ;; more random environments.
568 ;;
569 ;; I think these should be called "face.faceForeground" instead of
570 ;; "face.attributeForeground", but they're the way they are for
571 ;; hysterical reasons.
572 ;;
573 (let* ((name (symbol-name (face-name face)))
574 (fn (or (x-get-resource (concat name ".attributeFont")
575 "Face.AttributeFont")
576 (and set-anyway (face-font face))))
577 (fg (or (x-get-resource (concat name ".attributeForeground")
578 "Face.AttributeForeground")
579 (and set-anyway (face-foreground face))))
580 (bg (or (x-get-resource (concat name ".attributeBackground")
581 "Face.AttributeBackground")
582 (and set-anyway (face-background face))))
583 (bgp (or (x-get-resource (concat name ".attributeStipple")
584 "Face.AttributeStipple")
585 (x-get-resource (concat name ".attributeBackgroundPixmap")
586 "Face.AttributeBackgroundPixmap")
587 (and set-anyway (face-stipple face))))
588 (ulp (let ((resource (x-get-resource
589 (concat name ".attributeUnderline")
590 "Face.AttributeUnderline")))
591 (if resource
592 (member (downcase resource) '("on" "true"))
593 (and set-anyway (face-underline-p face)))))
594 )
595 (if fn
596 (condition-case ()
597 (cond ((string= fn "italic")
598 (make-face-italic face))
599 ((string= fn "bold")
600 (make-face-bold face))
601 ((string= fn "bold-italic")
602 (make-face-bold-italic face))
603 (t
604 (set-face-font face fn frame)))
605 (error
606 (if (member fn '("italic" "bold" "bold-italic"))
607 (message "no %s version found for face `%s'" fn name)
608 (message "font `%s' not found for face `%s'" fn name)))))
609 (if fg
610 (condition-case ()
611 (set-face-foreground face fg frame)
612 (error (message "color `%s' not allocated for face `%s'" fg name))))
613 (if bg
614 (condition-case ()
615 (set-face-background face bg frame)
616 (error (message "color `%s' not allocated for face `%s'" bg name))))
617 (if bgp
618 (condition-case ()
619 (set-face-stipple face bgp frame)
620 (error (message "pixmap `%s' not found for face `%s'" bgp name))))
621 (if (or ulp set-anyway)
622 (set-face-underline-p face ulp frame))
623 )))
624 face)
625
626 (defun copy-face (old-face new-face &optional frame new-frame)
627 "Define a face just like OLD-FACE, with name NEW-FACE.
628 If NEW-FACE already exists as a face, it is modified to be like OLD-FACE.
629 If it doesn't already exist, it is created.
630
631 If the optional argument FRAME is given as a frame,
632 NEW-FACE is changed on FRAME only.
633 If FRAME is t, the frame-independent default specification for OLD-FACE
634 is copied to NEW-FACE.
635 If FRAME is nil, copying is done for the frame-independent defaults
636 and for each existing frame.
637 If the optional fourth argument NEW-FRAME is given,
638 copy the information from face OLD-FACE on frame FRAME
639 to NEW-FACE on frame NEW-FRAME."
640 (or new-frame (setq new-frame frame))
641 (let ((inhibit-quit t))
642 (if (null frame)
643 (let ((frames (frame-list)))
644 (while frames
645 (copy-face old-face new-face (car frames))
646 (setq frames (cdr frames)))
647 (copy-face old-face new-face t))
648 (setq old-face (internal-get-face old-face frame))
649 (setq new-face (or (internal-find-face new-face new-frame)
650 (make-face new-face)))
651 (condition-case nil
652 ;; A face that has a global symbolic font modifier such as `bold'
653 ;; might legitimately get an error here.
654 ;; Use the frame's default font in that case.
655 (set-face-font new-face (face-font old-face frame) new-frame)
656 (error
657 (set-face-font new-face nil new-frame)))
658 (set-face-font-explicit new-face (face-font-explicit old-face frame)
659 new-frame)
660 (set-face-foreground new-face (face-foreground old-face frame) new-frame)
661 (set-face-background new-face (face-background old-face frame) new-frame)
662 (set-face-stipple new-face
663 (face-stipple old-face frame)
664 new-frame)
665 (set-face-underline-p new-face (face-underline-p old-face frame)
666 new-frame))
667 new-face))
668
669 (defun face-equal (face1 face2 &optional frame)
670 "True if the faces FACE1 and FACE2 display in the same way."
671 (setq face1 (internal-get-face face1 frame)
672 face2 (internal-get-face face2 frame))
673 (and (equal (face-foreground face1 frame) (face-foreground face2 frame))
674 (equal (face-background face1 frame) (face-background face2 frame))
675 (equal (face-font face1 frame) (face-font face2 frame))
676 (eq (face-underline-p face1 frame) (face-underline-p face2 frame))
677 (equal (face-stipple face1 frame)
678 (face-stipple face2 frame))))
679
680 (defun face-differs-from-default-p (face &optional frame)
681 "True if face FACE displays differently from the default face, on FRAME.
682 A face is considered to be ``the same'' as the default face if it is
683 actually specified in the same way (equivalent fonts, etc) or if it is
684 fully unspecified, and thus inherits the attributes of any face it
685 is displayed on top of.
686
687 The optional argument FRAME specifies which frame to test;
688 if FRAME is t, test the default for new frames.
689 If FRAME is nil or omitted, test the selected frame."
690 (let ((default (internal-get-face 'default frame)))
691 (setq face (internal-get-face face frame))
692 (not (and (or (equal (face-foreground default frame)
693 (face-foreground face frame))
694 (null (face-foreground face frame)))
695 (or (equal (face-background default frame)
696 (face-background face frame))
697 (null (face-background face frame)))
698 (or (null (face-font face frame))
699 (equal (face-font face frame)
700 (or (face-font default frame)
701 (downcase
702 (cdr (assq 'font (frame-parameters frame)))))))
703 (or (equal (face-stipple default frame)
704 (face-stipple face frame))
705 (null (face-stipple face frame)))
706 (equal (face-underline-p default frame)
707 (face-underline-p face frame))
708 ))))
709
710 (defun face-nontrivial-p (face &optional frame)
711 "True if face FACE has some non-nil attribute.
712 The optional argument FRAME specifies which frame to test;
713 if FRAME is t, test the default for new frames.
714 If FRAME is nil or omitted, test the selected frame."
715 (setq face (internal-get-face face frame))
716 (or (face-foreground face frame)
717 (face-background face frame)
718 (face-font face frame)
719 (face-stipple face frame)
720 (face-underline-p face frame)))
721
722
723 (defun invert-face (face &optional frame)
724 "Swap the foreground and background colors of face FACE.
725 If the face doesn't specify both foreground and background, then
726 set its foreground and background to the default background and foreground."
727 (interactive (list (read-face-name "Invert face: ")))
728 (setq face (internal-get-face face frame))
729 (let ((fg (face-foreground face frame))
730 (bg (face-background face frame)))
731 (if (or fg bg)
732 (progn
733 (set-face-foreground face bg frame)
734 (set-face-background face fg frame))
735 (let* ((frame-bg (cdr (assq 'background-color (frame-parameters frame))))
736 (default-bg (or (face-background 'default frame)
737 frame-bg))
738 (frame-fg (cdr (assq 'foreground-color (frame-parameters frame))))
739 (default-fg (or (face-foreground 'default frame)
740 frame-fg)))
741 (set-face-foreground face default-bg frame)
742 (set-face-background face default-fg frame))))
743 face)
744
745
746 (defun internal-try-face-font (face font &optional frame)
747 "Like set-face-font, but returns nil on failure instead of an error."
748 (condition-case ()
749 (set-face-font-auto face font frame)
750 (error nil)))
751 \f
752 ;; Manipulating font names.
753
754 (defvar x-font-regexp nil)
755 (defvar x-font-regexp-head nil)
756 (defvar x-font-regexp-weight nil)
757 (defvar x-font-regexp-slant nil)
758
759 (defconst x-font-regexp-weight-subnum 1)
760 (defconst x-font-regexp-slant-subnum 2)
761 (defconst x-font-regexp-swidth-subnum 3)
762 (defconst x-font-regexp-adstyle-subnum 4)
763
764 ;;; Regexps matching font names in "Host Portable Character Representation."
765 ;;;
766 (let ((- "[-?]")
767 (foundry "[^-]+")
768 (family "[^-]+")
769 (weight "\\(bold\\|demibold\\|medium\\)") ; 1
770 ; (weight\? "\\(\\*\\|bold\\|demibold\\|medium\\|\\)") ; 1
771 (weight\? "\\([^-]*\\)") ; 1
772 (slant "\\([ior]\\)") ; 2
773 ; (slant\? "\\([ior?*]?\\)") ; 2
774 (slant\? "\\([^-]?\\)") ; 2
775 ; (swidth "\\(\\*\\|normal\\|semicondensed\\|\\)") ; 3
776 (swidth "\\([^-]*\\)") ; 3
777 ; (adstyle "\\(\\*\\|sans\\|\\)") ; 4
778 (adstyle "\\([^-]*\\)") ; 4
779 (pixelsize "[0-9]+")
780 (pointsize "[0-9][0-9]+")
781 (resx "[0-9][0-9]+")
782 (resy "[0-9][0-9]+")
783 (spacing "[cmp?*]")
784 (avgwidth "[0-9]+")
785 (registry "[^-]+")
786 (encoding "[^-]+")
787 )
788 (setq x-font-regexp
789 (concat "\\`\\*?[-?*]"
790 foundry - family - weight\? - slant\? - swidth - adstyle -
791 pixelsize - pointsize - resx - resy - spacing - avgwidth -
792 registry - encoding "\\*?\\'"
793 ))
794 (setq x-font-regexp-head
795 (concat "\\`[-?*]" foundry - family - weight\? - slant\?
796 "\\([-*?]\\|\\'\\)"))
797 (setq x-font-regexp-slant (concat - slant -))
798 (setq x-font-regexp-weight (concat - weight -))
799 nil)
800
801 (defun x-resolve-font-name (pattern &optional face frame)
802 "Return a font name matching PATTERN.
803 All wildcards in PATTERN become substantiated.
804 If PATTERN is nil, return the name of the frame's base font, which never
805 contains wildcards.
806 Given optional arguments FACE and FRAME, return a font which is
807 also the same size as FACE on FRAME, or fail."
808 (or (symbolp face)
809 (setq face (face-name face)))
810 (and (eq frame t)
811 (setq frame nil))
812 (if pattern
813 ;; Note that x-list-fonts has code to handle a face with nil as its font.
814 (let ((fonts (x-list-fonts pattern face frame 1)))
815 (or fonts
816 (if face
817 (if (string-match "\\*" pattern)
818 (if (null (face-font face))
819 (error "No matching fonts are the same height as the frame default font")
820 (error "No matching fonts are the same height as face `%s'" face))
821 (if (null (face-font face))
822 (error "Height of font `%s' doesn't match the frame default font"
823 pattern)
824 (error "Height of font `%s' doesn't match face `%s'"
825 pattern face)))
826 (error "No fonts match `%s'" pattern)))
827 (car fonts))
828 (cdr (assq 'font (frame-parameters (selected-frame))))))
829
830 (defun x-frob-font-weight (font which)
831 (let ((case-fold-search t))
832 (cond ((string-match x-font-regexp font)
833 (concat (substring font 0
834 (match-beginning x-font-regexp-weight-subnum))
835 which
836 (substring font (match-end x-font-regexp-weight-subnum)
837 (match-beginning x-font-regexp-adstyle-subnum))
838 ;; Replace the ADD_STYLE_NAME field with *
839 ;; because the info in it may not be the same
840 ;; for related fonts.
841 "*"
842 (substring font (match-end x-font-regexp-adstyle-subnum))))
843 ((string-match x-font-regexp-head font)
844 (concat (substring font 0 (match-beginning 1)) which
845 (substring font (match-end 1))))
846 ((string-match x-font-regexp-weight font)
847 (concat (substring font 0 (match-beginning 1)) which
848 (substring font (match-end 1)))))))
849
850 (defun x-frob-font-slant (font which)
851 (let ((case-fold-search t))
852 (cond ((string-match x-font-regexp font)
853 (concat (substring font 0
854 (match-beginning x-font-regexp-slant-subnum))
855 which
856 (substring font (match-end x-font-regexp-slant-subnum)
857 (match-beginning x-font-regexp-adstyle-subnum))
858 ;; Replace the ADD_STYLE_NAME field with *
859 ;; because the info in it may not be the same
860 ;; for related fonts.
861 "*"
862 (substring font (match-end x-font-regexp-adstyle-subnum))))
863 ((string-match x-font-regexp-head font)
864 (concat (substring font 0 (match-beginning 2)) which
865 (substring font (match-end 2))))
866 ((string-match x-font-regexp-slant font)
867 (concat (substring font 0 (match-beginning 1)) which
868 (substring font (match-end 1)))))))
869
870 (defun x-make-font-bold (font)
871 "Given an X font specification, make a bold version of it.
872 If that can't be done, return nil."
873 (x-frob-font-weight font "bold"))
874
875 (defun x-make-font-demibold (font)
876 "Given an X font specification, make a demibold version of it.
877 If that can't be done, return nil."
878 (x-frob-font-weight font "demibold"))
879
880 (defun x-make-font-unbold (font)
881 "Given an X font specification, make a non-bold version of it.
882 If that can't be done, return nil."
883 (x-frob-font-weight font "medium"))
884
885 (defun x-make-font-italic (font)
886 "Given an X font specification, make an italic version of it.
887 If that can't be done, return nil."
888 (x-frob-font-slant font "i"))
889
890 (defun x-make-font-oblique (font) ; you say tomayto...
891 "Given an X font specification, make an oblique version of it.
892 If that can't be done, return nil."
893 (x-frob-font-slant font "o"))
894
895 (defun x-make-font-unitalic (font)
896 "Given an X font specification, make a non-italic version of it.
897 If that can't be done, return nil."
898 (x-frob-font-slant font "r"))
899
900 (defun x-make-font-bold-italic (font)
901 "Given an X font specification, make a bold and italic version of it.
902 If that can't be done, return nil."
903 (and (setq font (x-make-font-bold font))
904 (x-make-font-italic font)))
905 \f
906 ;;; non-X-specific interface
907
908 (defun make-face-bold (face &optional frame noerror)
909 "Make the font of the given face be bold, if possible.
910 If NOERROR is non-nil, return nil on failure."
911 (interactive (list (read-face-name "Make which face bold: ")))
912 ;; Set the bold-p flag, first of all.
913 (internal-set-face-1 face nil t 10 frame)
914 (if (and (eq frame t) (listp (face-font face t)))
915 (set-face-font face (if (memq 'italic (face-font face t))
916 '(bold italic) '(bold))
917 t)
918 (let (font)
919 (if (null frame)
920 (let ((frames (frame-list)))
921 ;; Make this face bold in global-face-data.
922 (make-face-bold face t noerror)
923 ;; Make this face bold in each frame.
924 (while frames
925 (make-face-bold face (car frames) noerror)
926 (setq frames (cdr frames))))
927 (setq face (internal-get-face face frame))
928 (setq font (or (face-font face frame)
929 (face-font face t)))
930 (if (listp font)
931 (setq font nil))
932 (setq font (or font
933 (face-font 'default frame)
934 (cdr (assq 'font (frame-parameters frame)))))
935 (or (and font (make-face-bold-internal face frame font))
936 ;; We failed to find a bold version of the font.
937 noerror
938 (error "No bold version of %S" font))))))
939
940 (defun make-face-bold-internal (face frame font)
941 (let (f2)
942 (or (and (setq f2 (x-make-font-bold font))
943 (internal-try-face-font face f2 frame))
944 (and (setq f2 (x-make-font-demibold font))
945 (internal-try-face-font face f2 frame)))))
946
947 (defun make-face-italic (face &optional frame noerror)
948 "Make the font of the given face be italic, if possible.
949 If NOERROR is non-nil, return nil on failure."
950 (interactive (list (read-face-name "Make which face italic: ")))
951 ;; Set the italic-p flag, first of all.
952 (internal-set-face-1 face nil t 11 frame)
953 (if (and (eq frame t) (listp (face-font face t)))
954 (set-face-font face (if (memq 'bold (face-font face t))
955 '(bold italic) '(italic))
956 t)
957 (let (font)
958 (if (null frame)
959 (let ((frames (frame-list)))
960 ;; Make this face italic in global-face-data.
961 (make-face-italic face t noerror)
962 ;; Make this face italic in each frame.
963 (while frames
964 (make-face-italic face (car frames) noerror)
965 (setq frames (cdr frames))))
966 (setq face (internal-get-face face frame))
967 (setq font (or (face-font face frame)
968 (face-font face t)))
969 (if (listp font)
970 (setq font nil))
971 (setq font (or font
972 (face-font 'default frame)
973 (cdr (assq 'font (frame-parameters frame)))))
974 (or (and font (make-face-italic-internal face frame font))
975 ;; We failed to find an italic version of the font.
976 noerror
977 (error "No italic version of %S" font))))))
978
979 (defun make-face-italic-internal (face frame font)
980 (let (f2)
981 (or (and (setq f2 (x-make-font-italic font))
982 (internal-try-face-font face f2 frame))
983 (and (setq f2 (x-make-font-oblique font))
984 (internal-try-face-font face f2 frame)))))
985
986 (defun make-face-bold-italic (face &optional frame noerror)
987 "Make the font of the given face be bold and italic, if possible.
988 If NOERROR is non-nil, return nil on failure."
989 (interactive (list (read-face-name "Make which face bold-italic: ")))
990 ;; Set the bold-p and italic-p flags, first of all.
991 (internal-set-face-1 face nil t 10 frame)
992 (internal-set-face-1 face nil t 11 frame)
993 (if (and (eq frame t) (listp (face-font face t)))
994 (set-face-font face '(bold italic) t)
995 (let (font)
996 (if (null frame)
997 (let ((frames (frame-list)))
998 ;; Make this face bold-italic in global-face-data.
999 (make-face-bold-italic face t noerror)
1000 ;; Make this face bold in each frame.
1001 (while frames
1002 (make-face-bold-italic face (car frames) noerror)
1003 (setq frames (cdr frames))))
1004 (setq face (internal-get-face face frame))
1005 (setq font (or (face-font face frame)
1006 (face-font face t)))
1007 (if (listp font)
1008 (setq font nil))
1009 (setq font (or font
1010 (face-font 'default frame)
1011 (cdr (assq 'font (frame-parameters frame)))))
1012 (or (and font (make-face-bold-italic-internal face frame font))
1013 ;; We failed to find a bold italic version.
1014 noerror
1015 (error "No bold italic version of %S" font))))))
1016
1017 (defun make-face-bold-italic-internal (face frame font)
1018 (let (f2 f3)
1019 (or (and (setq f2 (x-make-font-italic font))
1020 (not (equal font f2))
1021 (setq f3 (x-make-font-bold f2))
1022 (not (equal f2 f3))
1023 (internal-try-face-font face f3 frame))
1024 (and (setq f2 (x-make-font-oblique font))
1025 (not (equal font f2))
1026 (setq f3 (x-make-font-bold f2))
1027 (not (equal f2 f3))
1028 (internal-try-face-font face f3 frame))
1029 (and (setq f2 (x-make-font-italic font))
1030 (not (equal font f2))
1031 (setq f3 (x-make-font-demibold f2))
1032 (not (equal f2 f3))
1033 (internal-try-face-font face f3 frame))
1034 (and (setq f2 (x-make-font-oblique font))
1035 (not (equal font f2))
1036 (setq f3 (x-make-font-demibold f2))
1037 (not (equal f2 f3))
1038 (internal-try-face-font face f3 frame)))))
1039
1040 (defun make-face-unbold (face &optional frame noerror)
1041 "Make the font of the given face be non-bold, if possible.
1042 If NOERROR is non-nil, return nil on failure."
1043 (interactive (list (read-face-name "Make which face non-bold: ")))
1044 ;; Clear the bold-p flag, first of all.
1045 (internal-set-face-1 face nil nil 10 frame)
1046 (if (and (eq frame t) (listp (face-font face t)))
1047 (set-face-font face (if (memq 'italic (face-font face t))
1048 '(italic) nil)
1049 t)
1050 (let (font font1)
1051 (if (null frame)
1052 (let ((frames (frame-list)))
1053 ;; Make this face unbold in global-face-data.
1054 (make-face-unbold face t noerror)
1055 ;; Make this face unbold in each frame.
1056 (while frames
1057 (make-face-unbold face (car frames) noerror)
1058 (setq frames (cdr frames))))
1059 (setq face (internal-get-face face frame))
1060 (setq font1 (or (face-font face frame)
1061 (face-font face t)))
1062 (if (listp font1)
1063 (setq font1 nil))
1064 (setq font1 (or font1
1065 (face-font 'default frame)
1066 (cdr (assq 'font (frame-parameters frame)))))
1067 (setq font (and font1 (x-make-font-unbold font1)))
1068 (or (if font (internal-try-face-font face font frame))
1069 noerror
1070 (error "No unbold version of %S" font1))))))
1071
1072 (defun make-face-unitalic (face &optional frame noerror)
1073 "Make the font of the given face be non-italic, if possible.
1074 If NOERROR is non-nil, return nil on failure."
1075 (interactive (list (read-face-name "Make which face non-italic: ")))
1076 ;; Clear the italic-p flag, first of all.
1077 (internal-set-face-1 face nil nil 11 frame)
1078 (if (and (eq frame t) (listp (face-font face t)))
1079 (set-face-font face (if (memq 'bold (face-font face t))
1080 '(bold) nil)
1081 t)
1082 (let (font font1)
1083 (if (null frame)
1084 (let ((frames (frame-list)))
1085 ;; Make this face unitalic in global-face-data.
1086 (make-face-unitalic face t noerror)
1087 ;; Make this face unitalic in each frame.
1088 (while frames
1089 (make-face-unitalic face (car frames) noerror)
1090 (setq frames (cdr frames))))
1091 (setq face (internal-get-face face frame))
1092 (setq font1 (or (face-font face frame)
1093 (face-font face t)))
1094 (if (listp font1)
1095 (setq font1 nil))
1096 (setq font1 (or font1
1097 (face-font 'default frame)
1098 (cdr (assq 'font (frame-parameters frame)))))
1099 (setq font (and font1 (x-make-font-unitalic font1)))
1100 (or (if font (internal-try-face-font face font frame))
1101 noerror
1102 (error "No unitalic version of %S" font1))))))
1103 \f
1104 (defvar list-faces-sample-text
1105 "abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1106 "*Text string to display as the sample text for `list-faces-display'.")
1107
1108 ;; The name list-faces would be more consistent, but let's avoid a conflict
1109 ;; with Lucid, which uses that name differently.
1110 (defun list-faces-display ()
1111 "List all faces, using the same sample text in each.
1112 The sample text is a string that comes from the variable
1113 `list-faces-sample-text'.
1114
1115 It is possible to give a particular face name different appearances in
1116 different frames. This command shows the appearance in the
1117 selected frame."
1118 (interactive)
1119 (let ((faces (sort (face-list) (function string-lessp)))
1120 (face nil)
1121 (frame (selected-frame))
1122 disp-frame window)
1123 (with-output-to-temp-buffer "*Faces*"
1124 (save-excursion
1125 (set-buffer standard-output)
1126 (setq truncate-lines t)
1127 (while faces
1128 (setq face (car faces))
1129 (setq faces (cdr faces))
1130 (insert (format "%25s " (symbol-name face)))
1131 (let ((beg (point)))
1132 (insert list-faces-sample-text)
1133 (insert "\n")
1134 (put-text-property beg (1- (point)) 'face face)
1135 ;; If the sample text has multiple lines, line up all of them.
1136 (goto-char beg)
1137 (forward-line 1)
1138 (while (not (eobp))
1139 (insert " ")
1140 (forward-line 1))))
1141 (goto-char (point-min)))
1142 (print-help-return-message))
1143 ;; If the *Faces* buffer appears in a different frame,
1144 ;; copy all the face definitions from FRAME,
1145 ;; so that the display will reflect the frame that was selected.
1146 (setq window (get-buffer-window (get-buffer "*Faces*") t))
1147 (setq disp-frame (if window (window-frame window)
1148 (car (frame-list))))
1149 (or (eq frame disp-frame)
1150 (let ((faces (face-list)))
1151 (while faces
1152 (copy-face (car faces) (car faces) frame disp-frame)
1153 (setq faces (cdr faces)))))))
1154
1155 (defun describe-face (face)
1156 "Display the properties of face FACE."
1157 (interactive (list (read-face-name "Describe face: ")))
1158 (with-output-to-temp-buffer "*Help*"
1159 (princ "Properties of face `")
1160 (princ (face-name face))
1161 (princ "':") (terpri)
1162 (princ "Foreground: ") (princ (face-foreground face)) (terpri)
1163 (princ "Background: ") (princ (face-background face)) (terpri)
1164 (princ " Font: ") (princ (face-font face)) (terpri)
1165 (princ "Underlined: ") (princ (if (face-underline-p face) "yes" "no")) (terpri)
1166 (princ " Stipple: ") (princ (or (face-stipple face) "none")) (terpri)
1167 (terpri)
1168 (princ "Documentation:") (terpri)
1169 (let ((doc (face-documentation face)))
1170 (if doc
1171 (princ doc)
1172 (princ "not documented as a face.")))
1173 (print-help-return-message)))
1174 \f
1175 ;;; Setting a face based on a SPEC.
1176
1177 (defun face-attr-match-p (face attrs &optional frame)
1178 (or frame (setq frame (selected-frame)))
1179 (and (face-attr-match-1 face frame attrs ':inverse-video
1180 'face-inverse-video-p)
1181 (if (face-inverse-video-p face frame)
1182 (and
1183 (face-attr-match-1 face frame attrs
1184 ':foreground 'face-background
1185 (cdr (assq 'foreground-color
1186 (frame-parameters frame))))
1187 (face-attr-match-1 face frame attrs
1188 ':background 'face-foreground
1189 (cdr (assq 'background-color
1190 (frame-parameters frame)))))
1191 (and
1192 (face-attr-match-1 face frame attrs ':foreground 'face-foreground)
1193 (face-attr-match-1 face frame attrs ':background 'face-background)))
1194 (face-attr-match-1 face frame attrs ':stipple 'face-stipple)
1195 (face-attr-match-1 face frame attrs ':bold 'face-bold-p)
1196 (face-attr-match-1 face frame attrs ':italic 'face-italic-p)
1197 (face-attr-match-1 face frame attrs ':underline 'face-underline-p)
1198 ))
1199
1200 (defun face-attr-match-1 (face frame plist property function
1201 &optional defaultval)
1202 (while (and plist (not (eq (car plist) property)))
1203 (setq plist (cdr (cdr plist))))
1204 (eq (funcall function face frame)
1205 (if plist
1206 (nth 1 plist)
1207 (or defaultval
1208 (funcall function 'default frame)))))
1209
1210 (defun face-spec-match-p (face spec &optional frame)
1211 "Return t if FACE, on FRAME, matches what SPEC says it should look like."
1212 (face-attr-match-p face (face-spec-choose spec frame) frame))
1213
1214 (defun face-attr-construct (face &optional frame)
1215 "Return a defface-style attribute list for FACE, as it exists on FRAME."
1216 (let (result)
1217 (if (face-inverse-video-p face frame)
1218 (progn
1219 (setq result (cons ':inverse-video (cons t result)))
1220 (or (face-attr-match-1 face frame nil
1221 ':foreground 'face-background
1222 (cdr (assq 'foreground-color
1223 (frame-parameters frame))))
1224 (setq result (cons ':foreground
1225 (cons (face-foreground face frame) result))))
1226 (or (face-attr-match-1 face frame nil
1227 ':background 'face-foreground
1228 (cdr (assq 'background-color
1229 (frame-parameters frame))))
1230 (setq result (cons ':background
1231 (cons (face-background face frame) result)))))
1232 (if (face-foreground face frame)
1233 (setq result (cons ':foreground
1234 (cons (face-foreground face frame) result))))
1235 (if (face-background face frame)
1236 (setq result (cons ':background
1237 (cons (face-background face frame) result)))))
1238 (if (face-stipple face frame)
1239 (setq result (cons ':stipple
1240 (cons (face-stipple face frame) result))))
1241 (if (face-bold-p face frame)
1242 (setq result (cons ':bold
1243 (cons (face-bold-p face frame) result))))
1244 (if (face-italic-p face frame)
1245 (setq result (cons ':italic
1246 (cons (face-italic-p face frame) result))))
1247 (if (face-underline-p face frame)
1248 (setq result (cons ':underline
1249 (cons (face-underline-p face frame) result))))
1250 result))
1251
1252 ;; Choose the proper attributes for FRAME, out of SPEC.
1253 (defun face-spec-choose (spec &optional frame)
1254 (or frame (setq frame (selected-frame)))
1255 (let ((tail spec)
1256 result)
1257 (while tail
1258 (let* ((entry (car tail))
1259 (display (nth 0 entry))
1260 (attrs (nth 1 entry)))
1261 (setq tail (cdr tail))
1262 (when (face-spec-set-match-display display frame)
1263 (setq result attrs tail nil))))
1264 result))
1265
1266 (defun face-spec-set (face spec &optional frame)
1267 "Set FACE's face attributes according to the first matching entry in SPEC.
1268 If optional FRAME is non-nil, set it for that frame only.
1269 If it is nil, then apply SPEC to each frame individually.
1270 See `defface' for information about SPEC."
1271 (if frame
1272 (let ((attrs (face-spec-choose spec frame)))
1273 (when attrs
1274 ;; If the font was set automatically, clear it out
1275 ;; to allow it to be set it again.
1276 (unless (face-font-explicit face frame)
1277 (set-face-font face nil frame))
1278 (modify-face face '(nil) '(nil) nil nil nil nil nil frame)
1279 (face-spec-set-1 face frame attrs ':foreground 'set-face-foreground)
1280 (face-spec-set-1 face frame attrs ':background 'set-face-background)
1281 (face-spec-set-1 face frame attrs ':stipple 'set-face-stipple)
1282 (face-spec-set-1 face frame attrs ':bold 'set-face-bold-p)
1283 (face-spec-set-1 face frame attrs ':italic 'set-face-italic-p)
1284 (face-spec-set-1 face frame attrs ':underline 'set-face-underline-p)
1285 (face-spec-set-1 face frame attrs ':inverse-video
1286 'set-face-inverse-video-p)))
1287 (let ((frames (frame-list))
1288 frame)
1289 (while frames
1290 (setq frame (car frames)
1291 frames (cdr frames))
1292 (face-spec-set face (or (get face 'saved-face)
1293 (get face 'face-defface-spec))
1294 frame)
1295 (face-spec-set face spec frame)))))
1296
1297 (defun face-spec-set-1 (face frame plist property function)
1298 (while (and plist (not (eq (car plist) property)))
1299 (setq plist (cdr (cdr plist))))
1300 (if plist
1301 (funcall function face (nth 1 plist) frame)))
1302
1303 (defun face-spec-set-match-display (display frame)
1304 "Non-nil iff DISPLAY matches FRAME.
1305 DISPLAY is part of a spec such as can be used in `defface'.
1306 If FRAME is nil, the current FRAME is used."
1307 (let* ((conjuncts display)
1308 conjunct req options
1309 ;; t means we have succeeded against all
1310 ;; the conjunts in DISPLAY that have been tested so far.
1311 (match t))
1312 (if (eq conjuncts t)
1313 (setq conjuncts nil))
1314 (while (and conjuncts match)
1315 (setq conjunct (car conjuncts)
1316 conjuncts (cdr conjuncts)
1317 req (car conjunct)
1318 options (cdr conjunct)
1319 match (cond ((eq req 'type)
1320 (memq window-system options))
1321 ((eq req 'class)
1322 (memq (frame-parameter frame 'display-type) options))
1323 ((eq req 'background)
1324 (memq (frame-parameter frame 'background-mode)
1325 options))
1326 (t
1327 (error "Unknown req `%S' with options `%S'"
1328 req options)))))
1329 match))
1330 \f
1331 ;; Like x-create-frame but also set up the faces.
1332
1333 (defun x-create-frame-with-faces (&optional parameters)
1334 ;; Read this frame's geometry resource, if it has an explicit name,
1335 ;; and put the specs into PARAMETERS.
1336 (let* ((name (or (cdr (assq 'name parameters))
1337 (cdr (assq 'name default-frame-alist))))
1338 (x-resource-name name)
1339 (res-geometry (if name (x-get-resource "geometry" "Geometry"))))
1340 (if res-geometry
1341 (let ((parsed (x-parse-geometry res-geometry)))
1342 ;; If the resource specifies a position,
1343 ;; call the position and size "user-specified".
1344 (if (or (assq 'top parsed) (assq 'left parsed))
1345 (setq parsed (append '((user-position . t) (user-size . t))
1346 parsed)))
1347 ;; Put the geometry parameters at the end.
1348 ;; Copy default-frame-alist so that they go after it.
1349 (setq parameters (append parameters default-frame-alist parsed)))))
1350
1351 (if default-enable-multibyte-characters
1352 ;; If an ASCII font is specified in PARAMETERS, we try to create
1353 ;; a fontset from it, and use it for the new frame.
1354 (condition-case nil
1355 (let ((font (cdr (assq 'font parameters))))
1356 (if (and font
1357 (not (query-fontset font)))
1358 (setq parameters
1359 (cons (cons 'font (create-fontset-from-ascii-font font))
1360 parameters))))
1361 (error nil)))
1362
1363 (let (frame)
1364 (if (null global-face-data)
1365 (progn
1366 (setq frame (x-create-frame parameters))
1367 (frame-set-background-mode frame))
1368 (let* ((visibility-spec (assq 'visibility parameters))
1369 success faces rest)
1370 (setq frame (x-create-frame (cons '(visibility . nil) parameters)))
1371 (unwind-protect
1372 (progn
1373 ;; Copy the face alist, copying the face vectors
1374 ;; and emptying out their attributes.
1375 (setq faces
1376 (mapcar '(lambda (elt)
1377 (cons (car elt)
1378 (vector 'face
1379 (face-name (cdr elt))
1380 (face-id (cdr elt))
1381 nil
1382 nil nil nil nil
1383 nil nil nil nil)))
1384 global-face-data))
1385 (set-frame-face-alist frame faces)
1386
1387 ;; Handle the reverse-video frame parameter
1388 ;; and X resource. x-create-frame does not handle this one.
1389 (if (cdr (or (assq 'reverse parameters)
1390 (assq 'reverse default-frame-alist)
1391 (let ((resource (x-get-resource "reverseVideo"
1392 "ReverseVideo")))
1393 (if resource
1394 (cons nil (member (downcase resource)
1395 '("on" "true")))))))
1396 (let* ((params (frame-parameters frame))
1397 (bg (cdr (assq 'foreground-color params)))
1398 (fg (cdr (assq 'background-color params))))
1399 (modify-frame-parameters frame
1400 (list (cons 'foreground-color fg)
1401 (cons 'background-color bg)))
1402 (if (equal bg (cdr (assq 'border-color params)))
1403 (modify-frame-parameters frame
1404 (list (cons 'border-color fg))))
1405 (if (equal bg (cdr (assq 'mouse-color params)))
1406 (modify-frame-parameters frame
1407 (list (cons 'mouse-color fg))))
1408 (if (equal bg (cdr (assq 'cursor-color params)))
1409 (modify-frame-parameters frame
1410 (list (cons 'cursor-color fg))))))
1411
1412 (frame-set-background-mode frame)
1413
1414 (face-set-after-frame-default frame)
1415
1416 ;; Make the frame visible, if desired.
1417 (if (null visibility-spec)
1418 (make-frame-visible frame)
1419 (modify-frame-parameters frame (list visibility-spec)))
1420 (setq success t))
1421 (or success
1422 (delete-frame frame)))))
1423 frame))
1424
1425 ;; Update a frame's faces after the frame font changes.
1426 ;; This is called from modify-frame-parameters
1427 ;; as well as from elsewhere in this file.
1428 (defun face-set-after-frame-default (frame)
1429 (let ((rest (frame-face-alist frame)))
1430 (while rest
1431 ;; Set up each face, first from the defface information,
1432 ;; then the global face data, and then the X resources.
1433 (let* ((face (car (car rest)))
1434 (spec (or (get face 'saved-face)
1435 (get face 'face-defface-spec)))
1436 (global (cdr (assq face global-face-data)))
1437 (local (cdr (car rest))))
1438 (when spec
1439 (face-spec-set face spec frame))
1440 (face-fill-in face global frame)
1441 (make-face-x-resource-internal local frame))
1442 (setq rest (cdr rest)))))
1443
1444 (defcustom frame-background-mode nil
1445 "*The brightness of the background.
1446 Set this to the symbol dark if your background color is dark, light if
1447 your background is light, or nil (default) if you want Emacs to
1448 examine the brightness for you."
1449 :group 'faces
1450 :set #'(lambda (var value)
1451 (set var value)
1452 (mapcar 'frame-set-background-mode (frame-list)))
1453 :initialize 'custom-initialize-changed
1454 :type '(choice (choice-item dark)
1455 (choice-item light)
1456 (choice-item :tag "default" nil)))
1457
1458 (defun frame-set-background-mode (frame)
1459 "Set up the `background-mode' and `display-type' frame parameters for FRAME."
1460 (unless (memq (framep frame) '(t pc))
1461 (let ((bg-resource (x-get-resource ".backgroundMode"
1462 "BackgroundMode"))
1463 (params (frame-parameters frame))
1464 (bg-mode))
1465 (setq bg-mode
1466 (cond (frame-background-mode)
1467 (bg-resource (intern (downcase bg-resource)))
1468 ((< (apply '+ (x-color-values
1469 (cdr (assq 'background-color params))
1470 frame))
1471 ;; Just looking at the screen,
1472 ;; colors whose values add up to .6 of the white total
1473 ;; still look dark to me.
1474 (* (apply '+ (x-color-values "white" frame)) .6))
1475 'dark)
1476 (t 'light)))
1477 (modify-frame-parameters frame
1478 (list (cons 'background-mode bg-mode)
1479 (cons 'display-type
1480 (cond ((x-display-color-p frame)
1481 'color)
1482 ((x-display-grayscale-p frame)
1483 'grayscale)
1484 (t 'mono))))))))
1485
1486 ;; Update a frame's faces when we change its default font.
1487 (defun frame-update-faces (frame) nil)
1488
1489 ;; Update the colors of FACE, after FRAME's own colors have been changed.
1490 ;; This applies only to faces with global color specifications
1491 ;; that are not simple constants.
1492 (defun frame-update-face-colors (frame)
1493 (frame-set-background-mode frame)
1494 (let ((faces global-face-data))
1495 (while faces
1496 (condition-case nil
1497 (let* ((data (cdr (car faces)))
1498 (face (car (car faces)))
1499 (foreground (face-foreground data))
1500 (background (face-background data)))
1501 ;; If the global spec is a specific color,
1502 ;; which doesn't depend on the frame's attributes,
1503 ;; we don't need to recalculate it now.
1504 (or (listp foreground)
1505 (setq foreground nil))
1506 (or (listp background)
1507 (setq background nil))
1508 ;; If we are going to frob this face at all,
1509 ;; reinitialize it first.
1510 (if (or foreground background)
1511 (progn (set-face-foreground face nil frame)
1512 (set-face-background face nil frame)))
1513 (if foreground
1514 (face-try-color-list 'set-face-foreground
1515 face foreground frame))
1516 (if background
1517 (face-try-color-list 'set-face-background
1518 face background frame)))
1519 (error nil))
1520 (setq faces (cdr faces)))))
1521
1522 ;; Fill in the face FACE from frame-independent face data DATA.
1523 ;; DATA should be the non-frame-specific ("global") face vector
1524 ;; for the face. FACE should be a face name or face object.
1525 ;; FRAME is the frame to act on; it must be an actual frame, not nil or t.
1526 (defun face-fill-in (face data frame)
1527 (condition-case nil
1528 (let ((foreground (face-foreground data))
1529 (background (face-background data))
1530 (font (face-font data))
1531 (stipple (face-stipple data)))
1532 (if (face-underline-p data)
1533 (set-face-underline-p face (face-underline-p data) frame))
1534 (if foreground
1535 (face-try-color-list 'set-face-foreground
1536 face foreground frame))
1537 (if background
1538 (face-try-color-list 'set-face-background
1539 face background frame))
1540 (if (listp font)
1541 (let ((bold (memq 'bold font))
1542 (italic (memq 'italic font)))
1543 (cond ((and bold italic)
1544 (make-face-bold-italic face frame))
1545 (bold
1546 (make-face-bold face frame))
1547 (italic
1548 (make-face-italic face frame))))
1549 (if font
1550 (set-face-font face font frame)))
1551 (if stipple
1552 (set-face-stipple face stipple frame)))
1553 (error nil)))
1554
1555 ;; Assuming COLOR is a valid color name,
1556 ;; return t if it can be displayed on FRAME.
1557 (defun face-color-supported-p (frame color background-p)
1558 (and window-system
1559 (or (x-display-color-p frame)
1560 ;; A black-and-white display can implement these.
1561 (member color '("black" "white"))
1562 ;; A black-and-white display can fake gray for background.
1563 (and background-p
1564 (face-color-gray-p color frame))
1565 ;; A grayscale display can implement colors that are gray (more or less).
1566 (and (x-display-grayscale-p frame)
1567 (face-color-gray-p color frame)))))
1568
1569 ;; Use FUNCTION to store a color in FACE on FRAME.
1570 ;; COLORS is either a single color or a list of colors.
1571 ;; If it is a list, try the colors one by one until one of them
1572 ;; succeeds. We signal an error only if all the colors failed.
1573 ;; t as COLORS or as an element of COLORS means to invert the face.
1574 ;; That can't fail, so any subsequent elements after the t are ignored.
1575 (defun face-try-color-list (function face colors frame)
1576 (if (stringp colors)
1577 (if (face-color-supported-p frame colors
1578 (eq function 'set-face-background))
1579 (funcall function face colors frame))
1580 (if (eq colors t)
1581 (set-face-inverse-video-p face t frame)
1582 (let (done)
1583 (while (and colors (not done))
1584 (if (or (memq (car colors) '(t underline))
1585 (face-color-supported-p frame (car colors)
1586 (eq function 'set-face-background)))
1587 (if (cdr colors)
1588 ;; If there are more colors to try, catch errors
1589 ;; and set `done' if we succeed.
1590 (condition-case nil
1591 (progn
1592 (cond ((eq (car colors) t)
1593 (set-face-inverse-video-p face t frame))
1594 ((eq (car colors) 'underline)
1595 (set-face-underline-p face t frame))
1596 (t
1597 (funcall function face (car colors) frame)))
1598 (setq done t))
1599 (error nil))
1600 ;; If this is the last color, let the error get out if it fails.
1601 ;; If it succeeds, we will exit anyway after this iteration.
1602 (cond ((eq (car colors) t)
1603 (set-face-inverse-video-p face t frame))
1604 ((eq (car colors) 'underline)
1605 (set-face-underline-p face t frame))
1606 (t
1607 (funcall function face (car colors) frame)))))
1608 (setq colors (cdr colors)))))))
1609
1610 ;;; Make the standard faces.
1611 ;;; The C code knows the default and modeline faces as faces 0 and 1,
1612 ;;; so they must be the first two faces made.
1613 (make-face 'default)
1614 (make-face 'modeline)
1615 (make-face 'highlight)
1616
1617 ;; These aren't really special in any way, but they're nice to have around.
1618
1619 (make-face 'bold)
1620 (make-face 'italic)
1621 (make-face 'bold-italic)
1622 (make-face 'region)
1623 (make-face 'secondary-selection)
1624 (make-face 'underline)
1625
1626 (setq region-face (face-id 'region))
1627
1628 (defgroup basic-faces nil
1629 "The standard faces of Emacs."
1630 :prefix "huh"
1631 :group 'faces)
1632
1633 ;; Specify how these faces look, and their documentation.
1634 (let ((all '((bold "Use bold font." ((t (:bold t))))
1635 (bold-italic "Use bold italic font." ((t (:bold t :italic t))))
1636 (italic "Use italic font." ((t (:italic t))))
1637 (underline "Underline text." ((t (:underline t))))
1638 (default "Used for text not covered by other faces." ((t nil)))
1639 (highlight "Highlight text in some way."
1640 ((((class color) (background light))
1641 (:background "darkseagreen2"))
1642 (((class color) (background dark))
1643 (:background "darkolivegreen"))
1644 (t (:inverse-video t))))
1645 (modeline "Used for displaying the modeline."
1646 ((t (:inverse-video t))))
1647 (region "Used for displaying the region."
1648 ((((class color) (background dark))
1649 (:background "blue"))
1650 (t (:background "gray"))))
1651 (secondary-selection
1652 "Used for displaying the secondary selection."
1653 ((((class color) (background light))
1654 (:background "paleturquoise"))
1655 (((class color) (background dark))
1656 (:background "darkslateblue"))
1657 (t (:inverse-video t))))))
1658 entry symbol doc spec)
1659 (while all
1660 (setq entry (car all)
1661 all (cdr all)
1662 symbol (nth 0 entry)
1663 doc (nth 1 entry)
1664 spec (nth 2 entry))
1665 (custom-add-to-group 'basic-faces symbol 'custom-face)
1666 (put symbol 'face-documentation doc)
1667 (put symbol 'face-defface-spec spec)))
1668
1669 (provide 'faces)
1670
1671 ;;; faces.el ends here