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