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