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