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