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