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