faces.el 48.8 KB
Newer Older
Eric S. Raymond's avatar
Eric S. Raymond committed
1 2
;;; faces.el --- Lisp interface to the c "face" structure

Erik Naggum's avatar
Erik Naggum committed
3
;; Copyright (C) 1992, 1993, 1994, 1995, 1996 Free Software Foundation, Inc.
Eric S. Raymond's avatar
Eric S. Raymond committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17

;; This file is part of GNU Emacs.

;; GNU Emacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
Erik Naggum's avatar
Erik Naggum committed
18 19 20
;; along with GNU Emacs; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
Eric S. Raymond's avatar
Eric S. Raymond committed
21 22 23 24 25 26 27

;;; Commentary:

;; Mostly derived from Lucid.

;;; Code:

28 29 30 31 32 33 34 35 36 37 38 39 40
(eval-when-compile
 ;; These used to be defsubsts, now they're subrs.  Avoid losing if we're
 ;; being compiled with an old Emacs that still has defsubrs in it.
 (put 'face-name 'byte-optimizer nil)
 (put 'face-id 'byte-optimizer nil)
 (put 'face-font 'byte-optimizer nil)
 (put 'face-foreground 'byte-optimizer nil)
 (put 'face-background 'byte-optimizer nil)
 (put 'face-stipple 'byte-optimizer nil)
 (put 'face-underline-p 'byte-optimizer nil)
 (put 'set-face-font 'byte-optimizer nil)
 (put 'set-face-foreground 'byte-optimizer nil)
 (put 'set-face-background 'byte-optimizer nil)
41
 (put 'set-face-stipple 'byte-optimizer nil)
42
 (put 'set-face-underline-p 'byte-optimizer nil))
43 44 45 46

;;;; Functions for manipulating face vectors.

;;; A face vector is a vector of the form:
47
;;;    [face NAME ID FONT FOREGROUND BACKGROUND STIPPLE UNDERLINE]
48 49

;;; Type checkers.
Eric S. Raymond's avatar
Eric S. Raymond committed
50 51 52
(defsubst internal-facep (x)
  (and (vectorp x) (= (length x) 8) (eq (aref x 0) 'face)))

Richard M. Stallman's avatar
Richard M. Stallman committed
53 54 55 56 57 58
(defun facep (x)
  "Return t if X is a face name or an internal face vector."
  (and (or (internal-facep x)
	   (and (symbolp x) (assq x global-face-data)))
       t))
      
Eric S. Raymond's avatar
Eric S. Raymond committed
59
(defmacro internal-check-face (face)
Richard M. Stallman's avatar
Richard M. Stallman committed
60 61
  (` (or (internal-facep (, face))
	 (signal 'wrong-type-argument (list 'internal-facep (, face))))))
Eric S. Raymond's avatar
Eric S. Raymond committed
62

63
;;; Accessors.
64
(defun face-name (face)
Eric S. Raymond's avatar
Eric S. Raymond committed
65 66 67
  "Return the name of face FACE."
  (aref (internal-get-face face) 1))

68
(defun face-id (face)
Eric S. Raymond's avatar
Eric S. Raymond committed
69 70 71
  "Return the internal ID number of face FACE."
  (aref (internal-get-face face) 2))

72
(defun face-font (face &optional frame)
Eric S. Raymond's avatar
Eric S. Raymond committed
73 74
  "Return the font name of face FACE, or nil if it is unspecified.
If the optional argument FRAME is given, report on face FACE in that frame.
75 76 77 78
If FRAME is t, report on the defaults for face FACE (for new frames).
  The font default for a face is either nil, or a list
  of the form (bold), (italic) or (bold italic).
If FRAME is omitted or nil, use the selected frame."
Eric S. Raymond's avatar
Eric S. Raymond committed
79 80
  (aref (internal-get-face face frame) 3))

81
(defun face-foreground (face &optional frame)
Eric S. Raymond's avatar
Eric S. Raymond committed
82 83
  "Return the foreground color name of face FACE, or nil if unspecified.
If the optional argument FRAME is given, report on face FACE in that frame.
84 85
If FRAME is t, report on the defaults for face FACE (for new frames).
If FRAME is omitted or nil, use the selected frame."
Eric S. Raymond's avatar
Eric S. Raymond committed
86 87
  (aref (internal-get-face face frame) 4))

88
(defun face-background (face &optional frame)
Eric S. Raymond's avatar
Eric S. Raymond committed
89 90
  "Return the background color name of face FACE, or nil if unspecified.
If the optional argument FRAME is given, report on face FACE in that frame.
91 92
If FRAME is t, report on the defaults for face FACE (for new frames).
If FRAME is omitted or nil, use the selected frame."
Eric S. Raymond's avatar
Eric S. Raymond committed
93 94
  (aref (internal-get-face face frame) 5))

95
(defun face-stipple (face &optional frame)
96 97 98 99 100 101 102
 "Return the stipple pixmap name of face FACE, or nil if unspecified.
If the optional argument FRAME is given, report on face FACE in that frame.
If FRAME is t, report on the defaults for face FACE (for new frames).
If FRAME is omitted or nil, use the selected frame."
 (aref (internal-get-face face frame) 6))

(defalias 'face-background-pixmap 'face-stipple)
Eric S. Raymond's avatar
Eric S. Raymond committed
103

104
(defun face-underline-p (face &optional frame)
Eric S. Raymond's avatar
Eric S. Raymond committed
105 106
 "Return t if face FACE is underlined.
If the optional argument FRAME is given, report on face FACE in that frame.
107 108
If FRAME is t, report on the defaults for face FACE (for new frames).
If FRAME is omitted or nil, use the selected frame."
Eric S. Raymond's avatar
Eric S. Raymond committed
109 110
 (aref (internal-get-face face frame) 7))

111 112

;;; Mutators.
Eric S. Raymond's avatar
Eric S. Raymond committed
113

114
(defun set-face-font (face font &optional frame)
Eric S. Raymond's avatar
Eric S. Raymond committed
115 116 117 118
  "Change the font of face FACE to FONT (a string).
If the optional FRAME argument is provided, change only
in that frame; otherwise change each frame."
  (interactive (internal-face-interactive "font"))
119
  (if (stringp font) (setq font (x-resolve-font-name font 'default frame)))
120
  (internal-set-face-1 face 'font font 3 frame))
Eric S. Raymond's avatar
Eric S. Raymond committed
121

122
(defun set-face-foreground (face color &optional frame)
Eric S. Raymond's avatar
Eric S. Raymond committed
123 124 125 126
  "Change the foreground color of face FACE to COLOR (a string).
If the optional FRAME argument is provided, change only
in that frame; otherwise change each frame."
  (interactive (internal-face-interactive "foreground"))
127
  (internal-set-face-1 face 'foreground color 4 frame))
Eric S. Raymond's avatar
Eric S. Raymond committed
128

129 130 131 132 133 134 135 136 137 138 139 140 141
(defvar face-default-stipple "gray3" 
  "Default stipple pattern used on monochrome displays.
This stipple pattern is used on monochrome displays
instead of shades of gray for a face background color.
See `set-face-stipple' for possible values for this variable.")

(defun face-color-gray-p (color &optional frame)
  "Return t if COLOR is a shade of gray (or white or black).
FRAME specifies the frame and thus the display for interpreting COLOR."
  (let* ((values (x-color-values color frame))
	 (r (nth 0 values))
	 (g (nth 1 values))
	 (b (nth 2 values)))
142 143
    (and values
	 (< (abs (- r g)) (/ (max 1 (abs r) (abs g)) 20))
144 145 146
	 (< (abs (- g b)) (/ (max 1 (abs g) (abs b)) 20))
	 (< (abs (- b r)) (/ (max 1 (abs b) (abs r)) 20)))))

147
(defun set-face-background (face color &optional frame)
Eric S. Raymond's avatar
Eric S. Raymond committed
148 149 150 151
  "Change the background color of face FACE to COLOR (a string).
If the optional FRAME argument is provided, change only
in that frame; otherwise change each frame."
  (interactive (internal-face-interactive "background"))
152 153
  ;; For a specific frame, use gray stipple instead of gray color
  ;; if the display does not support a gray color.
154
  (if (and frame (not (eq frame t)) color
Karl Heuer's avatar
Karl Heuer committed
155
	   ;; Check for support for foreground, not for background!
156 157 158 159
	   ;; face-color-supported-p is smart enough to know
	   ;; that grays are "supported" as background
	   ;; because we are supposed to use stipple for them!
	   (not (face-color-supported-p frame color nil)))
160
      (set-face-stipple face face-default-stipple frame)
161 162 163 164 165 166 167 168
    (if (null frame)
	(let ((frames (frame-list)))
	  (while frames
	    (set-face-background (face-name face) color (car frames))
	    (setq frames (cdr frames)))
	  (set-face-background face color t)
	  color)
      (internal-set-face-1 face 'background color 5 frame))))
Eric S. Raymond's avatar
Eric S. Raymond committed
169

170
(defun set-face-stipple (face pixmap &optional frame)
171 172 173
  "Change the stipple pixmap of face FACE to PIXMAP.
PIXMAP should be a string, the name of a file of pixmap data.
The directories listed in the `x-bitmap-file-path' variable are searched.
Eric S. Raymond's avatar
Eric S. Raymond committed
174

175 176 177
Alternatively, PIXMAP may be a list of the form (WIDTH HEIGHT DATA)
where WIDTH and HEIGHT are the size in pixels,
and DATA is a string, containing the raw bits of the bitmap.  
Eric S. Raymond's avatar
Eric S. Raymond committed
178

179 180
If the optional FRAME argument is provided, change only
in that frame; otherwise change each frame."
181
  (interactive (internal-face-interactive-stipple "stipple"))
182
  (internal-set-face-1 face 'background-pixmap pixmap 6 frame))
183 184

(defalias 'set-face-background-pixmap 'set-face-stipple)
Eric S. Raymond's avatar
Eric S. Raymond committed
185

186
(defun set-face-underline-p (face underline-p &optional frame)
Eric S. Raymond's avatar
Eric S. Raymond committed
187 188 189 190
  "Specify whether face FACE is underlined.  (Yes if UNDERLINE-P is non-nil.)
If the optional FRAME argument is provided, change only
in that frame; otherwise change each frame."
  (interactive (internal-face-interactive "underline-p" "underlined"))
191
  (internal-set-face-1 face 'underline underline-p 7 frame))
192

Richard M. Stallman's avatar
Richard M. Stallman committed
193
(defun modify-face-read-string (face default name alist)
194 195 196 197 198 199 200 201 202 203 204 205 206 207
  (let ((value
	 (completing-read
	  (if default
	      (format "Set face %s %s (default %s): "
		      face name (downcase default))
	    (format "Set face %s %s: " face name))
	  alist)))
    (cond ((equal value "none")
	   nil)
	  ((equal value "")
	   default)
	  (t value))))

(defun modify-face (face foreground background stipple
Simon Marshall's avatar
Simon Marshall committed
208
		    bold-p italic-p underline-p &optional frame)
209
  "Change the display attributes for face FACE.
Simon Marshall's avatar
Simon Marshall committed
210 211 212 213 214 215 216
If the optional FRAME argument is provided, change only
in that frame; otherwise change each frame.

FOREGROUND and BACKGROUND should be a colour name string (or list of strings to
try) or nil.  STIPPLE should be a stipple pattern name string or nil.
If nil, means do not change the display attribute corresponding to that arg.

217
BOLD-P, ITALIC-P, and UNDERLINE-P specify whether the face should be set bold,
Simon Marshall's avatar
Simon Marshall committed
218 219 220 221
in italic, and underlined, respectively.  If neither nil or t, means do not
change the display attribute corresponding to that arg.

If called interactively, prompts for a face name and face attributes."
222 223
  (interactive
   (let* ((completion-ignore-case t)
Simon Marshall's avatar
Simon Marshall committed
224 225 226 227 228 229 230 231 232 233 234
	  (face		(symbol-name (read-face-name "Modify face: ")))
	  (colors	(mapcar 'list x-colors))
	  (stipples	(mapcar 'list (apply 'nconc
					    (mapcar 'directory-files
						    x-bitmap-file-path))))
	  (foreground	(modify-face-read-string
			 face (face-foreground (intern face))
			 "foreground" colors))
	  (background	(modify-face-read-string
			 face (face-background (intern face))
			 "background" colors))
235 236 237 238 239
	  ;; If the stipple value is a list (WIDTH HEIGHT DATA),
	  ;; represent that as a string by printing it out.
	  (old-stipple-string
	   (if (stringp (face-stipple (intern face)))
	       (face-stipple (intern face))
240 241
	     (if (face-stipple (intern face))
		 (prin1-to-string (face-stipple (intern face))))))
242 243 244 245 246 247 248 249 250
	  (new-stipple-string
	   (modify-face-read-string
	    face old-stipple-string
	    "stipple" stipples))
	  ;; Convert the stipple value text we read
	  ;; back to a list if it looks like one.
	  ;; This makes the assumption that a pixmap file name
	  ;; won't start with an open-paren.
	  (stipple
251 252 253 254 255 256 257
	   (and new-stipple-string
		(if (string-match "^(" new-stipple-string)
		    (read new-stipple-string)
		  new-stipple-string)))
	  (bold-p	(y-or-n-p (concat "Should face " face " be bold ")))
	  (italic-p	(y-or-n-p (concat "Should face " face " be italic ")))
	  (underline-p	(y-or-n-p (concat "Should face " face " be underlined ")))
Simon Marshall's avatar
Simon Marshall committed
258
	  (all-frames-p	(y-or-n-p (concat "Modify face " face " in all frames "))))
259 260 261 262 263
     (message "Face %s: %s" face
      (mapconcat 'identity
       (delq nil
	(list (and foreground (concat (downcase foreground) " foreground"))
	      (and background (concat (downcase background) " background"))
264
	      (and stipple (concat (downcase new-stipple-string) " stipple"))
265 266
	      (and bold-p "bold") (and italic-p "italic")
	      (and underline-p "underline"))) ", "))
267
     (list (intern face) foreground background stipple
Simon Marshall's avatar
Simon Marshall committed
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
	   bold-p italic-p underline-p
	   (if all-frames-p nil (selected-frame)))))
  (condition-case nil
      (face-try-color-list 'set-face-foreground face foreground frame)
    (error nil))
  (condition-case nil
      (face-try-color-list 'set-face-background face background frame)
    (error nil))
  (condition-case nil
      (set-face-stipple face stipple frame)
    (error nil))
  (cond ((eq bold-p nil) (make-face-unbold face frame t))
	((eq bold-p t) (make-face-bold face frame t)))
  (cond ((eq italic-p nil) (make-face-unitalic face frame t))
	((eq italic-p t) (make-face-italic face frame t)))
  (if (memq underline-p '(nil t))
      (set-face-underline-p face underline-p frame))
285
  (and (interactive-p) (redraw-display)))
286 287 288

;;;; Associating face names (symbols) with their face vectors.

289 290 291 292 293
(defvar global-face-data nil
  "Internal data for face support functions.  Not for external use.
This is an alist associating face names with the default values for
their parameters.  Newly created frames get their data from here.")

294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
(defun face-list ()
  "Returns a list of all defined face names."
  (mapcar 'car global-face-data))

(defun internal-find-face (name &optional frame)
  "Retrieve the face named NAME.  Return nil if there is no such face.
If the optional argument FRAME is given, this gets the face NAME for
that frame; otherwise, it uses the selected frame.
If FRAME is the symbol t, then the global, non-frame face is returned.
If NAME is already a face, it is simply returned."
  (if (and (eq frame t) (not (symbolp name)))
      (setq name (face-name name)))
  (if (symbolp name)
      (cdr (assq name
		 (if (eq frame t)
		     global-face-data
		   (frame-face-alist (or frame (selected-frame))))))
    (internal-check-face name)
    name))

(defun internal-get-face (name &optional frame)
  "Retrieve the face named NAME; error if there is none.
If the optional argument FRAME is given, this gets the face NAME for
that frame; otherwise, it uses the selected frame.
If FRAME is the symbol t, then the global, non-frame face is returned.
If NAME is already a face, it is simply returned."
  (or (internal-find-face name frame)
      (internal-check-face name)))


(defun internal-set-face-1 (face name value index frame)
  (let ((inhibit-quit t))
    (if (null frame)
	(let ((frames (frame-list)))
	  (while frames
	    (internal-set-face-1 (face-name face) name value index (car frames))
	    (setq frames (cdr frames)))
	  (aset (internal-get-face (if (symbolp face) face (face-name face)) t)
		index value)
	  value)
      (or (eq frame t)
	  (set-face-attribute-internal (face-id face) name value frame))
      (aset (internal-get-face face frame) index value))))


(defun read-face-name (prompt)
  (let (face)
    (while (= (length face) 0)
      (setq face (completing-read prompt
				  (mapcar '(lambda (x) (list (symbol-name x)))
					  (face-list))
				  nil t)))
    (intern face)))

(defun internal-face-interactive (what &optional bool)
  (let* ((fn (intern (concat "face-" what)))
	 (prompt (concat "Set " what " of face"))
	 (face (read-face-name (concat prompt ": ")))
	 (default (if (fboundp fn)
		      (or (funcall fn face (selected-frame))
			  (funcall fn 'default (selected-frame)))))
	 (value (if bool
		    (y-or-n-p (concat "Should face " (symbol-name face)
				      " be " bool "? "))
		  (read-string (concat prompt " " (symbol-name face) " to: ")
			       default))))
    (list face (if (equal value "") nil value))))

362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
(defun internal-face-interactive-stipple (what)
  (let* ((fn (intern (concat "face-" what)))
	 (prompt (concat "Set " what " of face"))
	 (face (read-face-name (concat prompt ": ")))
	 (default (if (fboundp fn)
		      (or (funcall fn face (selected-frame))
			  (funcall fn 'default (selected-frame)))))
	 ;; If the stipple value is a list (WIDTH HEIGHT DATA),
	 ;; represent that as a string by printing it out.
	 (old-stipple-string
	  (if (stringp (face-stipple face))
	      (face-stipple face)
	    (if (null (face-stipple face))
		nil
	      (prin1-to-string (face-stipple face)))))
	 (new-stipple-string
	  (read-string
	   (concat prompt " " (symbol-name face) " to: ")
	   old-stipple-string))
	 ;; Convert the stipple value text we read
	 ;; back to a list if it looks like one.
	 ;; This makes the assumption that a pixmap file name
	 ;; won't start with an open-paren.
	 (stipple
	  (if (string-match "^(" new-stipple-string)
	      (read new-stipple-string)
	    new-stipple-string)))
    (list face (if (equal stipple "") nil stipple))))
Eric S. Raymond's avatar
Eric S. Raymond committed
390 391 392 393 394

(defun make-face (name)
  "Define a new FACE on all frames.  
You can modify the font, color, etc of this face with the set-face- functions.
If the face already exists, it is unmodified."
395
  (interactive "SMake face: ")
Eric S. Raymond's avatar
Eric S. Raymond committed
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
  (or (internal-find-face name)
      (let ((face (make-vector 8 nil)))
	(aset face 0 'face)
	(aset face 1 name)
	(let* ((frames (frame-list))
	       (inhibit-quit t)
	       (id (internal-next-face-id)))
	  (make-face-internal id)
	  (aset face 2 id)
	  (while frames
	    (set-frame-face-alist (car frames)
				  (cons (cons name (copy-sequence face))
					(frame-face-alist (car frames))))
	    (setq frames (cdr frames)))
	  (setq global-face-data (cons (cons name face) global-face-data)))
	;; when making a face after frames already exist
412
	(if (or (eq window-system 'x) (eq window-system 'win32))
Eric S. Raymond's avatar
Eric S. Raymond committed
413
	    (make-face-x-resource-internal face))
414 415 416
	;; add to menu
	(if (fboundp 'facemenu-add-new-face)
	    (facemenu-add-new-face name))
417 418
	face))
  name)
Eric S. Raymond's avatar
Eric S. Raymond committed
419 420 421 422 423 424 425

;; Fill in a face by default based on X resources, for all existing frames.
;; This has to be done when a new face is made.
(defun make-face-x-resource-internal (face &optional frame set-anyway)
  (cond ((null frame)
	 (let ((frames (frame-list)))
	   (while frames
426
	     (if (or (eq (framep (car frames)) 'x) (eq (framep (car frames)) 'win32))
427 428
		 (make-face-x-resource-internal (face-name face)
						(car frames) set-anyway))
Eric S. Raymond's avatar
Eric S. Raymond committed
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
	     (setq frames (cdr frames)))))
	(t
	 (setq face (internal-get-face (face-name face) frame))
	 ;;
	 ;; These are things like "attributeForeground" instead of simply
	 ;; "foreground" because people tend to do things like "*foreground",
	 ;; which would cause all faces to be fully qualified, making faces
	 ;; inherit attributes in a non-useful way.  So we've made them slightly
	 ;; less obvious to specify in order to make them work correctly in
	 ;; more random environments.
	 ;;
	 ;; I think these should be called "face.faceForeground" instead of
	 ;; "face.attributeForeground", but they're the way they are for
	 ;; hysterical reasons.
	 ;; 
	 (let* ((name (symbol-name (face-name face)))
		(fn  (or (x-get-resource (concat name ".attributeFont")
					 "Face.AttributeFont")
			 (and set-anyway (face-font face))))
		(fg  (or (x-get-resource (concat name ".attributeForeground")
					 "Face.AttributeForeground")
			 (and set-anyway (face-foreground face))))
		(bg  (or (x-get-resource (concat name ".attributeBackground")
					 "Face.AttributeBackground")
			 (and set-anyway (face-background face))))
454 455 456 457 458
		(bgp (or (x-get-resource (concat name ".attributeStipple")
					 "Face.AttributeStipple")
			 (x-get-resource (concat name ".attributeBackgroundPixmap")
					 "Face.AttributeBackgroundPixmap")
			 (and set-anyway (face-stipple face))))
459 460 461 462 463 464
		(ulp (let ((resource (x-get-resource
				      (concat name ".attributeUnderline")
				      "Face.AttributeUnderline")))
		       (if resource
			   (member (downcase resource) '("on" "true"))
			 (and set-anyway (face-underline-p face)))))
Eric S. Raymond's avatar
Eric S. Raymond committed
465 466 467
		)
	   (if fn
	       (condition-case ()
468 469 470 471 472 473 474 475 476 477 478 479
		   (cond ((string= fn "italic")
			  (make-face-italic face))
			 ((string= fn "bold")
			  (make-face-bold face))
			 ((string= fn "bold-italic")
			  (make-face-bold-italic face))
			 (t
			  (set-face-font face fn frame)))
		 (error
		  (if (member fn '("italic" "bold" "bold-italic"))
		      (message "no %s version found for face `%s'" fn name)
		    (message "font `%s' not found for face `%s'" fn name)))))
Eric S. Raymond's avatar
Eric S. Raymond committed
480 481 482 483 484 485 486 487
	   (if fg
	       (condition-case ()
		   (set-face-foreground face fg frame)
		 (error (message "color `%s' not allocated for face `%s'" fg name))))
	   (if bg
	       (condition-case ()
		   (set-face-background face bg frame)
		 (error (message "color `%s' not allocated for face `%s'" bg name))))
488 489 490 491
	   (if bgp
	       (condition-case ()
		   (set-face-stipple face bgp frame)
		 (error (message "pixmap `%s' not found for face `%s'" bgp name))))
Eric S. Raymond's avatar
Eric S. Raymond committed
492 493 494 495 496
	   (if (or ulp set-anyway)
	       (set-face-underline-p face ulp frame))
	   )))
  face)

Richard M. Stallman's avatar
Richard M. Stallman committed
497 498 499 500 501 502 503 504 505 506 507
(defun copy-face (old-face new-face &optional frame new-frame)
  "Define a face just like OLD-FACE, with name NEW-FACE.
If NEW-FACE already exists as a face, it is modified to be like OLD-FACE.
If it doesn't already exist, it is created.

If the optional argument FRAME is given as a frame,
NEW-FACE is changed on FRAME only.
If FRAME is t, the frame-independent default specification for OLD-FACE
is copied to NEW-FACE.
If FRAME is nil, copying is done for the frame-independent defaults
and for each existing frame.
508 509
If the optional fourth argument NEW-FRAME is given, 
copy the information from face OLD-FACE on frame FRAME
Richard M. Stallman's avatar
Richard M. Stallman committed
510
to NEW-FACE on frame NEW-FRAME."
511
  (or new-frame (setq new-frame frame))
512
  (let ((inhibit-quit t))
Eric S. Raymond's avatar
Eric S. Raymond committed
513 514 515
    (if (null frame)
	(let ((frames (frame-list)))
	  (while frames
Richard M. Stallman's avatar
Richard M. Stallman committed
516
	    (copy-face old-face new-face (car frames))
Eric S. Raymond's avatar
Eric S. Raymond committed
517
	    (setq frames (cdr frames)))
Richard M. Stallman's avatar
Richard M. Stallman committed
518
	  (copy-face old-face new-face t))
519 520 521
      (setq old-face (internal-get-face old-face frame))
      (setq new-face (or (internal-find-face new-face new-frame)
			 (make-face new-face)))
522 523 524 525 526 527 528
      (condition-case nil
	  ;; A face that has a global symbolic font modifier such as `bold'
	  ;; might legitimately get an error here.
	  ;; Use the frame's default font in that case.
	  (set-face-font new-face (face-font old-face frame) new-frame)
	(error
	 (set-face-font new-face nil new-frame)))
529 530
      (set-face-foreground new-face (face-foreground old-face frame) new-frame)
      (set-face-background new-face (face-background old-face frame) new-frame)
531 532 533
      (set-face-stipple new-face
			(face-stipple old-face frame)
			new-frame)
Eric S. Raymond's avatar
Eric S. Raymond committed
534
      (set-face-underline-p new-face (face-underline-p old-face frame)
535
			    new-frame))
Eric S. Raymond's avatar
Eric S. Raymond committed
536 537 538
    new-face))

(defun face-equal (face1 face2 &optional frame)
539
  "True if the faces FACE1 and FACE2 display in the same way."
Eric S. Raymond's avatar
Eric S. Raymond committed
540 541 542 543 544
  (setq face1 (internal-get-face face1 frame)
	face2 (internal-get-face face2 frame))
  (and (equal (face-foreground face1 frame) (face-foreground face2 frame))
       (equal (face-background face1 frame) (face-background face2 frame))
       (equal (face-font face1 frame) (face-font face2 frame))
545
       (eq (face-underline-p face1 frame) (face-underline-p face2 frame))
546 547
       (equal (face-stipple face1 frame)
	      (face-stipple face2 frame))))
Eric S. Raymond's avatar
Eric S. Raymond committed
548 549 550 551 552 553

(defun face-differs-from-default-p (face &optional frame)
  "True if face FACE displays differently from the default face, on FRAME.
A face is considered to be ``the same'' as the default face if it is 
actually specified in the same way (equivalent fonts, etc) or if it is 
fully unspecified, and thus inherits the attributes of any face it 
554 555 556 557 558
is displayed on top of.

The optional argument FRAME specifies which frame to test;
if FRAME is t, test the default for new frames.
If FRAME is nil or omitted, test the selected frame."
Eric S. Raymond's avatar
Eric S. Raymond committed
559 560 561 562 563 564 565 566 567 568
  (let ((default (internal-get-face 'default frame)))
    (setq face (internal-get-face face frame))
    (not (and (or (equal (face-foreground default frame)
			 (face-foreground face frame))
		  (null (face-foreground face frame)))
	      (or (equal (face-background default frame)
			 (face-background face frame))
		  (null (face-background face frame)))
	      (or (equal (face-font default frame) (face-font face frame))
		  (null (face-font face frame)))
569 570 571
	      (or (equal (face-stipple default frame)
			 (face-stipple face frame))
		  (null (face-stipple face frame)))
Eric S. Raymond's avatar
Eric S. Raymond committed
572 573 574 575
	      (equal (face-underline-p default frame)
		     (face-underline-p face frame))
	      ))))

576 577 578 579 580 581 582 583 584 585 586 587
(defun face-nontrivial-p (face &optional frame)
  "True if face FACE has some non-nil attribute.
The optional argument FRAME specifies which frame to test;
if FRAME is t, test the default for new frames.
If FRAME is nil or omitted, test the selected frame."
  (setq face (internal-get-face face frame))
  (or (face-foreground face frame)
      (face-background face frame)
      (face-font face frame)
      (face-stipple face frame)
      (face-underline-p face frame)))

Eric S. Raymond's avatar
Eric S. Raymond committed
588 589 590 591

(defun invert-face (face &optional frame)
  "Swap the foreground and background colors of face FACE.
If the face doesn't specify both foreground and background, then
592
set its foreground and background to the default background and foreground."
Eric S. Raymond's avatar
Eric S. Raymond committed
593 594 595 596 597 598 599 600
  (interactive (list (read-face-name "Invert face: ")))
  (setq face (internal-get-face face frame))
  (let ((fg (face-foreground face frame))
	(bg (face-background face frame)))
    (if (or fg bg)
	(progn
	  (set-face-foreground face bg frame)
	  (set-face-background face fg frame))
601 602 603 604 605 606
      (set-face-foreground face (or (face-background 'default frame)
				    (cdr (assq 'background-color (frame-parameters frame))))
			   frame)
      (set-face-background face (or (face-foreground 'default frame)
				    (cdr (assq 'foreground-color (frame-parameters frame))))
			   frame)))
Eric S. Raymond's avatar
Eric S. Raymond committed
607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622
  face)


(defun internal-try-face-font (face font &optional frame)
  "Like set-face-font, but returns nil on failure instead of an error."
  (condition-case ()
      (set-face-font face font frame)
    (error nil)))

;; Manipulating font names.

(defconst x-font-regexp nil)
(defconst x-font-regexp-head nil)
(defconst x-font-regexp-weight nil)
(defconst x-font-regexp-slant nil)

623 624 625 626 627
(defconst x-font-regexp-weight-subnum 1)
(defconst x-font-regexp-slant-subnum 2)
(defconst x-font-regexp-swidth-subnum 3)
(defconst x-font-regexp-adstyle-subnum 4)

Eric S. Raymond's avatar
Eric S. Raymond committed
628 629 630 631 632 633 634 635 636 637 638 639 640 641
;;; Regexps matching font names in "Host Portable Character Representation."
;;;
(let ((- 		"[-?]")
      (foundry		"[^-]+")
      (family 		"[^-]+")
      (weight		"\\(bold\\|demibold\\|medium\\)")		; 1
;     (weight\?		"\\(\\*\\|bold\\|demibold\\|medium\\|\\)")	; 1
      (weight\?		"\\([^-]*\\)")					; 1
      (slant		"\\([ior]\\)")					; 2
;     (slant\?		"\\([ior?*]?\\)")				; 2
      (slant\?		"\\([^-]?\\)")					; 2
;     (swidth		"\\(\\*\\|normal\\|semicondensed\\|\\)")	; 3
      (swidth		"\\([^-]*\\)")					; 3
;     (adstyle		"\\(\\*\\|sans\\|\\)")				; 4
642
      (adstyle		"\\([^-]*\\)")					; 4
Eric S. Raymond's avatar
Eric S. Raymond committed
643 644 645 646 647 648 649 650 651 652 653 654
      (pixelsize	"[0-9]+")
      (pointsize	"[0-9][0-9]+")
      (resx		"[0-9][0-9]+")
      (resy		"[0-9][0-9]+")
      (spacing		"[cmp?*]")
      (avgwidth		"[0-9]+")
      (registry		"[^-]+")
      (encoding		"[^-]+")
      )
  (setq x-font-regexp
	(concat "\\`\\*?[-?*]"
		foundry - family - weight\? - slant\? - swidth - adstyle -
655 656
		pixelsize - pointsize - resx - resy - spacing - avgwidth -
		registry - encoding "\\*?\\'"
Eric S. Raymond's avatar
Eric S. Raymond committed
657 658 659 660 661 662 663 664
		))
  (setq x-font-regexp-head
	(concat "\\`[-?*]" foundry - family - weight\? - slant\?
		"\\([-*?]\\|\\'\\)"))
  (setq x-font-regexp-slant (concat - slant -))
  (setq x-font-regexp-weight (concat - weight -))
  nil)	    

665 666 667
(defun x-resolve-font-name (pattern &optional face frame)
  "Return a font name matching PATTERN.
All wildcards in PATTERN become substantiated.
668 669
If PATTERN is nil, return the name of the frame's base font, which never
contains wildcards.
670 671
Given optional arguments FACE and FRAME, return a font which is
also the same size as FACE on FRAME, or fail."
672 673 674 675
  (or (symbolp face)
      (setq face (face-name face)))
  (and (eq frame t)
       (setq frame nil))
676
  (if pattern
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 1)))
679 680
	(or fonts
	    (if face
Richard M. Stallman's avatar
Richard M. Stallman committed
681 682 683 684 685 686 687 688 689
		(if (string-match "\\*" pattern)
		    (if (null (face-font face))
			(error "No matching fonts are the same height as the frame default font")
		      (error "No matching fonts are the same height as face `%s'" face))
		  (if (null (face-font face))
		      (error "Height of font `%s' doesn't match the frame default font"
			     pattern)
		    (error "Height of font `%s' doesn't match face `%s'"
			   pattern face)))
690
	      (error "No fonts match `%s'" pattern)))
691 692
	(car fonts))
    (cdr (assq 'font (frame-parameters (selected-frame))))))
693

Eric S. Raymond's avatar
Eric S. Raymond committed
694
(defun x-frob-font-weight (font which)
695 696 697 698 699 700 701 702 703 704 705 706
  (let ((case-fold-search t))
    (cond ((string-match x-font-regexp font)
	   (concat (substring font 0
			      (match-beginning x-font-regexp-weight-subnum))
		   which
		   (substring font (match-end x-font-regexp-weight-subnum)
			      (match-beginning x-font-regexp-adstyle-subnum))
		   ;; Replace the ADD_STYLE_NAME field with *
		   ;; because the info in it may not be the same
		   ;; for related fonts.
		   "*"
		   (substring font (match-end x-font-regexp-adstyle-subnum))))
707 708 709 710
	  ((string-match x-font-regexp-head font)
	   (concat (substring font 0 (match-beginning 1)) which
		   (substring font (match-end 1))))
	  ((string-match x-font-regexp-weight font)
711 712
	   (concat (substring font 0 (match-beginning 1)) which
		   (substring font (match-end 1)))))))
Eric S. Raymond's avatar
Eric S. Raymond committed
713 714

(defun x-frob-font-slant (font which)
715 716 717 718 719 720 721 722 723 724 725 726
  (let ((case-fold-search t))
    (cond ((string-match x-font-regexp font)
	   (concat (substring font 0
			      (match-beginning x-font-regexp-slant-subnum))
		   which
		   (substring font (match-end x-font-regexp-slant-subnum)
			      (match-beginning x-font-regexp-adstyle-subnum))
		   ;; Replace the ADD_STYLE_NAME field with *
		   ;; because the info in it may not be the same
		   ;; for related fonts.
		   "*"
		   (substring font (match-end x-font-regexp-adstyle-subnum))))
727 728 729 730
	  ((string-match x-font-regexp-head font)
	   (concat (substring font 0 (match-beginning 2)) which
		   (substring font (match-end 2))))
	  ((string-match x-font-regexp-slant font)
731 732
	   (concat (substring font 0 (match-beginning 1)) which
		   (substring font (match-end 1)))))))
Eric S. Raymond's avatar
Eric S. Raymond committed
733 734

(defun x-make-font-bold (font)
735 736
  "Given an X font specification, make a bold version of it.
If that can't be done, return nil."
Eric S. Raymond's avatar
Eric S. Raymond committed
737 738 739
  (x-frob-font-weight font "bold"))

(defun x-make-font-demibold (font)
740 741
  "Given an X font specification, make a demibold version of it.
If that can't be done, return nil."
Eric S. Raymond's avatar
Eric S. Raymond committed
742 743 744
  (x-frob-font-weight font "demibold"))

(defun x-make-font-unbold (font)
745 746
  "Given an X font specification, make a non-bold version of it.
If that can't be done, return nil."
Eric S. Raymond's avatar
Eric S. Raymond committed
747 748 749
  (x-frob-font-weight font "medium"))

(defun x-make-font-italic (font)
750 751
  "Given an X font specification, make an italic version of it.
If that can't be done, return nil."
Eric S. Raymond's avatar
Eric S. Raymond committed
752 753 754
  (x-frob-font-slant font "i"))

(defun x-make-font-oblique (font) ; you say tomayto...
755 756
  "Given an X font specification, make an oblique version of it.
If that can't be done, return nil."
Eric S. Raymond's avatar
Eric S. Raymond committed
757 758 759
  (x-frob-font-slant font "o"))

(defun x-make-font-unitalic (font)
760 761
  "Given an X font specification, make a non-italic version of it.
If that can't be done, return nil."
Eric S. Raymond's avatar
Eric S. Raymond committed
762 763 764 765
  (x-frob-font-slant font "r"))

;;; non-X-specific interface

766
(defun make-face-bold (face &optional frame noerror)
Eric S. Raymond's avatar
Eric S. Raymond committed
767
  "Make the font of the given face be bold, if possible.  
768
If NOERROR is non-nil, return nil on failure."
Eric S. Raymond's avatar
Eric S. Raymond committed
769
  (interactive (list (read-face-name "Make which face bold: ")))
770
  (if (and (eq frame t) (listp (face-font face t)))
771 772 773
      (set-face-font face (if (memq 'italic (face-font face t))
			      '(bold italic) '(bold))
		     t)
774
    (let (font)
775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
      (if (null frame)
	  (let ((frames (frame-list)))
	    ;; Make this face bold in global-face-data.
	    (make-face-bold face t noerror)
	    ;; Make this face bold in each frame.
	    (while frames
	      (make-face-bold face (car frames) noerror)
	      (setq frames (cdr frames))))
	(setq face (internal-get-face face frame))
	(setq font (or (face-font face frame)
		       (face-font face t)))
	(if (listp font)
	    (setq font nil))
	(setq font (or font
		       (face-font 'default frame)
		       (cdr (assq 'font (frame-parameters frame)))))
791 792 793 794
	(or (and font (make-face-bold-internal face frame font))
	    ;; We failed to find a bold version of the font.
	    noerror
	    (error "No bold version of %S" font))))))
795

796 797 798 799 800 801
(defun make-face-bold-internal (face frame font)
  (let (f2)
    (or (and (setq f2 (x-make-font-bold font))
	     (internal-try-face-font face f2 frame))
	(and (setq f2 (x-make-font-demibold font))
	     (internal-try-face-font face f2 frame)))))
802 803

(defun make-face-italic (face &optional frame noerror)
Eric S. Raymond's avatar
Eric S. Raymond committed
804
  "Make the font of the given face be italic, if possible.  
805
If NOERROR is non-nil, return nil on failure."
Eric S. Raymond's avatar
Eric S. Raymond committed
806
  (interactive (list (read-face-name "Make which face italic: ")))
807
  (if (and (eq frame t) (listp (face-font face t)))
808 809 810
      (set-face-font face (if (memq 'bold (face-font face t))
			      '(bold italic) '(italic))
		     t)
811
    (let (font)
812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827
      (if (null frame)
	  (let ((frames (frame-list)))
	    ;; Make this face italic in global-face-data.
	    (make-face-italic face t noerror)
	    ;; Make this face italic in each frame.
	    (while frames
	      (make-face-italic face (car frames) noerror)
	      (setq frames (cdr frames))))
	(setq face (internal-get-face face frame))
	(setq font (or (face-font face frame)
		       (face-font face t)))
	(if (listp font)
	    (setq font nil))
	(setq font (or font
		       (face-font 'default frame)
		       (cdr (assq 'font (frame-parameters frame)))))
828 829 830 831
	(or (and font (make-face-italic-internal face frame font))
	    ;; We failed to find an italic version of the font.
	    noerror
	    (error "No italic version of %S" font))))))
832

833 834 835 836 837 838
(defun make-face-italic-internal (face frame font)
  (let (f2)
    (or (and (setq f2 (x-make-font-italic font))
	     (internal-try-face-font face f2 frame))
	(and (setq f2 (x-make-font-oblique font))
	     (internal-try-face-font face f2 frame)))))
839 840

(defun make-face-bold-italic (face &optional frame noerror)
Eric S. Raymond's avatar
Eric S. Raymond committed
841
  "Make the font of the given face be bold and italic, if possible.  
842
If NOERROR is non-nil, return nil on failure."
Eric S. Raymond's avatar
Eric S. Raymond committed
843
  (interactive (list (read-face-name "Make which face bold-italic: ")))
844
  (if (and (eq frame t) (listp (face-font face t)))
845
      (set-face-font face '(bold italic) t)
846
    (let (font)
847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862
      (if (null frame)
	  (let ((frames (frame-list)))
	    ;; Make this face bold-italic in global-face-data.
	    (make-face-bold-italic face t noerror)
	    ;; Make this face bold in each frame.
	    (while frames
	      (make-face-bold-italic face (car frames) noerror)
	      (setq frames (cdr frames))))
	(setq face (internal-get-face face frame))
	(setq font (or (face-font face frame)
		       (face-font face t)))
	(if (listp font)
	    (setq font nil))
	(setq font (or font
		       (face-font 'default frame)
		       (cdr (assq 'font (frame-parameters frame)))))
863 864 865 866
	(or (and font (make-face-bold-italic-internal face frame font))
	    ;; We failed to find a bold italic version.
	    noerror
	    (error "No bold italic version of %S" font))))))
867

868
(defun make-face-bold-italic-internal (face frame font)
869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889
  (let (f2 f3)
    (or (and (setq f2 (x-make-font-italic font))
	     (not (equal font f2))
	     (setq f3 (x-make-font-bold f2))
	     (not (equal f2 f3))
	     (internal-try-face-font face f3 frame))
	(and (setq f2 (x-make-font-oblique font))
	     (not (equal font f2))
	     (setq f3 (x-make-font-bold f2))
	     (not (equal f2 f3))
	     (internal-try-face-font face f3 frame))
	(and (setq f2 (x-make-font-italic font))
	     (not (equal font f2))
	     (setq f3 (x-make-font-demibold f2))
	     (not (equal f2 f3))
	     (internal-try-face-font face f3 frame))
	(and (setq f2 (x-make-font-oblique font))
	     (not (equal font f2))
	     (setq f3 (x-make-font-demibold f2))
	     (not (equal f2 f3))
	     (internal-try-face-font face f3 frame)))))
890 891

(defun make-face-unbold (face &optional frame noerror)
Eric S. Raymond's avatar
Eric S. Raymond committed
892
  "Make the font of the given face be non-bold, if possible.  
893
If NOERROR is non-nil, return nil on failure."
Eric S. Raymond's avatar
Eric S. Raymond committed
894
  (interactive (list (read-face-name "Make which face non-bold: ")))
895
  (if (and (eq frame t) (listp (face-font face t)))
896 897 898
      (set-face-font face (if (memq 'italic (face-font face t))
			      '(italic) nil)
		     t)
899
    (let (font font1)
900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915
      (if (null frame)
	  (let ((frames (frame-list)))
	    ;; Make this face unbold in global-face-data.
	    (make-face-unbold face t noerror)
	    ;; Make this face unbold in each frame.
	    (while frames
	      (make-face-unbold face (car frames) noerror)
	      (setq frames (cdr frames))))
	(setq face (internal-get-face face frame))
	(setq font1 (or (face-font face frame)
			(face-font face t)))
	(if (listp font1)
	    (setq font1 nil))
	(setq font1 (or font1
			(face-font 'default frame)
			(cdr (assq 'font (frame-parameters frame)))))
916
	(setq font (and font1 (x-make-font-unbold font1)))
917 918 919
	(or (if font (internal-try-face-font face font frame))
	    noerror
	    (error "No unbold version of %S" font1))))))
920 921

(defun make-face-unitalic (face &optional frame noerror)
Eric S. Raymond's avatar
Eric S. Raymond committed
922
  "Make the font of the given face be non-italic, if possible.  
923
If NOERROR is non-nil, return nil on failure."
Eric S. Raymond's avatar
Eric S. Raymond committed
924
  (interactive (list (read-face-name "Make which face non-italic: ")))
925
  (if (and (eq frame t) (listp (face-font face t)))
926 927 928
      (set-face-font face (if (memq 'bold (face-font face t))
			      '(bold) nil)
		     t)
929
    (let (font font1)
930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945
      (if (null frame)
	  (let ((frames (frame-list)))
	    ;; Make this face unitalic in global-face-data.
	    (make-face-unitalic face t noerror)
	    ;; Make this face unitalic in each frame.
	    (while frames
	      (make-face-unitalic face (car frames) noerror)
	      (setq frames (cdr frames))))
	(setq face (internal-get-face face frame))
	(setq font1 (or (face-font face frame)
			(face-font face t)))
	(if (listp font1)
	    (setq font1 nil))
	(setq font1 (or font1
			(face-font 'default frame)
			(cdr (assq 'font (frame-parameters frame)))))
946
	(setq font (and font1 (x-make-font-unitalic font1)))
947 948 949
	(or (if font (internal-try-face-font face font frame))
	    noerror
	    (error "No unitalic version of %S" font1))))))
Eric S. Raymond's avatar
Eric S. Raymond committed
950

951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980
(defvar list-faces-sample-text
  "abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  "*Text string to display as the sample text for `list-faces-display'.")

;; The name list-faces would be more consistent, but let's avoid a conflict
;; with Lucid, which uses that name differently.
(defun list-faces-display ()
  "List all faces, using the same sample text in each.
The sample text is a string that comes from the variable
`list-faces-sample-text'.

It is possible to give a particular face name different appearances in
different frames.  This command shows the appearance in the
selected frame."
  (interactive)
  (let ((faces (sort (face-list) (function string-lessp)))
	(face nil)
	(frame (selected-frame))
	disp-frame window)
    (with-output-to-temp-buffer "*Faces*"
      (save-excursion
	(set-buffer standard-output)
	(setq truncate-lines t)
	(while faces
	  (setq face (car faces))
	  (setq faces (cdr faces))
	  (insert (format "%25s " (symbol-name face)))
	  (let ((beg (point)))
	    (insert list-faces-sample-text)
	    (insert "\n")
981 982 983 984 985 986 987
	    (put-text-property beg (1- (point)) 'face face)
	    ;; If the sample text has multiple lines, line up all of them.
	    (goto-char beg)
	    (forward-line 1)
	    (while (not (eobp))
	      (insert "                          ")
	      (forward-line 1))))
988 989 990 991 992 993 994 995 996 997 998 999
	(goto-char (point-min))))
    ;; If the *Faces* buffer appears in a different frame,
    ;; copy all the face definitions from FRAME,
    ;; so that the display will reflect the frame that was selected.
    (setq window (get-buffer-window (get-buffer "*Faces*") t))
    (setq disp-frame (if window (window-frame window)
		       (car (frame-list))))
    (or (eq frame disp-frame)
	(let ((faces (face-list)))
	  (while faces
	    (copy-face (car faces) (car faces) frame disp-frame)
	    (setq faces (cdr faces)))))))
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012

(defun describe-face (face)
  "Display the properties of face FACE."
  (interactive (list (read-face-name "Describe face: ")))
  (with-output-to-temp-buffer "*Help*"
    (princ "Properties of face `")
    (princ (face-name face))
    (princ "':") (terpri)
    (princ "Foreground: ") (princ (face-foreground face)) (terpri)
    (princ "Background: ") (princ (face-background face)) (terpri)
    (princ "      Font: ") (princ (face-font face)) (terpri)
    (princ "Underlined: ") (princ (if (face-underline-p face) "yes" "no")) (terpri)
    (princ "   Stipple: ") (princ (or (face-stipple face) "none"))))
1013

1014 1015 1016
;;; Make the standard faces.
;;; The C code knows the default and modeline faces as faces 0 and 1,
;;; so they must be the first two faces made.
1017
(defun face-initialize ()
Eric S. Raymond's avatar
Eric S. Raymond committed
1018
  (make-face 'default)
1019
  (make-face 'modeline)
Eric S. Raymond's avatar
Eric S. Raymond committed
1020
  (make-face 'highlight)
1021

Eric S. Raymond's avatar
Eric S. Raymond committed
1022
  ;; These aren't really special in any way, but they're nice to have around.
1023

Eric S. Raymond's avatar
Eric S. Raymond committed
1024 1025 1026
  (make-face 'bold)
  (make-face 'italic)
  (make-face 'bold-italic)
1027
  (make-face 'region)
1028
  (make-face 'secondary-selection)
1029
  (make-face 'underline)
1030

1031
  (setq region-face (face-id 'region))
1032

1033 1034 1035 1036 1037 1038 1039 1040
  ;; Specify the global properties of these faces
  ;; so they will come out right on new frames.

  (make-face-bold 'bold t)
  (make-face-italic 'italic t)
  (make-face-bold-italic 'bold-italic t)

  (set-face-background 'highlight '("darkseagreen2" "green" t) t)
1041
  (set-face-background 'region '("gray" underline) t)
1042 1043 1044 1045 1046 1047 1048
  (set-face-background 'secondary-selection '("paleturquoise" "green" t) t)
  (set-face-background 'modeline '(t) t)
  (set-face-underline-p 'underline t t)

  ;; Set up the faces of all existing X Window frames
  ;; from those global properties, unless already set in a given frame.

1049 1050
  (let ((frames (frame-list)))
    (while frames
1051
      (if (not (memq (framep (car frames)) '(t nil)))
1052 1053 1054 1055 1056 1057 1058
	  (let ((frame (car frames))
		(rest global-face-data))
	    (while rest
	      (let ((face (car (car rest))))
		(or (face-differs-from-default-p face)
		    (face-fill-in face (cdr (car rest)) frame)))
	      (setq rest (cdr rest)))))
1059 1060
      (setq frames (cdr frames)))))

Eric S. Raymond's avatar
Eric S. Raymond committed
1061 1062 1063 1064

;; Like x-create-frame but also set up the faces.

(defun x-create-frame-with-faces (&optional parameters)
1065 1066 1067
  ;; Read this frame's geometry resource, if it has an explicit name,
  ;; and put the specs into PARAMETERS.
  (let* ((name (or (cdr (assq 'name parameters))
1068
		   (cdr (assq 'name default-frame-alist))))
1069
	 (x-resource-name name)
1070
	 (res-geometry (if name (x-get-resource "geometry" "Geometry")))
1071 1072 1073 1074 1075 1076 1077 1078 1079
	 parsed)
    (if res-geometry
	(progn
	  (setq parsed (x-parse-geometry res-geometry))
	  ;; If the resource specifies a position,
	  ;; call the position and size "user-specified".
	  (if (or (assq 'top parsed) (assq 'left parsed))
	      (setq parsed (cons '(user-position . t)
				 (cons '(user-size . t) parsed))))
1080 1081 1082 1083 1084
	  ;; Put the geometry parameters at the end.
	  ;; Copy default-frame-alist so that they go after it.
	  (setq parameters (append parameters
				   default-frame-alist
				   parsed)))))
1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
  (let (frame)
    (if (null global-face-data)
	(setq frame (x-create-frame parameters))
      (let* ((visibility-spec (assq 'visibility parameters))
	     (faces (copy-alist global-face-data))
	     success
	     (rest faces))
	(setq frame (x-create-frame (cons '(visibility . nil) parameters)))
	(unwind-protect
	    (progn
	      (set-frame-face-alist frame faces)

	      (if (cdr (or (assq 'reverse parameters)
			   (assq 'reverse default-frame-alist)
			   (let ((resource (x-get-resource "reverseVideo"
							   "ReverseVideo")))
			     (if resource
				 (cons nil (member (downcase resource)
						   '("on" "true")))))))
		  (let* ((params (frame-parameters frame))
			 (bg (cdr (assq 'foreground-color params)))
			 (fg (cdr (assq 'background-color params))))
		    (modify-frame-parameters frame
					     (list (cons 'foreground-color fg)
						   (cons 'background-color bg)))
		    (if (equal bg (cdr (assq 'border-color params)))
			(modify-frame-parameters frame
						 (list (cons 'border-color fg))))
		    (if (equal bg (cdr (assq 'mouse-color params)))
			(modify-frame-parameters frame
						 (list (cons 'mouse-color fg))))
		    (if (equal bg (cdr (assq 'cursor-color params)))
			(modify-frame-parameters frame
						 (list (cons 'cursor-color fg))))))
	      ;; Copy the vectors that represent the faces.
	      ;; Also fill them in from X resources.
	      (while rest
		(let ((global (cdr (car rest))))
		  (setcdr (car rest) (vector 'face
					     (face-name (cdr (car rest)))
					     (face-id (cdr (car rest)))
					     nil nil nil nil nil))
		  (face-fill-in (car (car rest)) global frame))
		(make-face-x-resource-internal (cdr (car rest)) frame t)
		(setq rest (cdr rest)))
	      (if (null visibility-spec)
		  (make-frame-visible frame)
		(modify-frame-parameters frame (list visibility-spec)))
	      (setq success t))
	  (or success
	      (delete-frame frame)))))
    ;; Set up the background-mode frame parameter
    ;; so that programs can decide good ways of highlighting
    ;; on this frame.
    (let ((bg-resource (x-get-resource ".backgroundMode"
				       "BackgroundMode"))
1141
	  (params (frame-parameters frame))
1142 1143 1144 1145
	  (bg-mode))
      (setq bg-mode
	    (cond (bg-resource (intern (downcase bg-resource)))
		  ((< (apply '+ (x-color-values
1146 1147
				 (cdr (assq 'background-color params))
				 frame))
1148 1149 1150 1151
		      ;; Just looking at the screen,
		      ;; colors whose values add up to .6 of the white total
		      ;; still look dark to me.
		      (* (apply '+ (x-color-values "white" frame)) .6))