faces.el 72.3 KB
Newer Older
Gerd Moellmann's avatar
Gerd Moellmann committed
1
;;; faces.el --- Lisp faces
Eric S. Raymond's avatar
Eric S. Raymond committed
2

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

;; 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
19 20 21
;; 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
22 23 24 25 26

;;; Commentary:

;;; Code:

27
(eval-when-compile
28 29 30
  (require 'cl)
  ;; Warning suppression -- can't require x-win in batch:
  (autoload 'xw-defined-colors "x-win"))
Gerd Moellmann's avatar
Gerd Moellmann committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54

(require 'cus-face)


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Font selection.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defgroup font-selection nil
  "Influencing face font selection."
  :group 'faces)


(defcustom face-font-selection-order
  '(:width :height :weight :slant)
  "*A list specifying how face font selection chooses fonts.
Each of the four symbols `:width', `:height', `:weight', and `:slant'
must appear once in the list, and the list must not contain any other
elements.  Font selection tries to find a best matching font for
those face attributes first that appear first in the list.  For
example, if `:slant' appears before `:height', font selection first
tries to find a font with a suitable slant, even if this results in
a font height that isn't optimal."
  :tag "Font selection order."
Gerd Moellmann's avatar
Gerd Moellmann committed
55
  :type '(list symbol symbol symbol symbol)
Gerd Moellmann's avatar
Gerd Moellmann committed
56 57 58 59 60
  :group 'font-selection
  :set #'(lambda (symbol value)
	   (set-default symbol value)
	   (internal-set-font-selection-order value)))

61 62

;; This is defined originally in xfaces.c.
Gerd Moellmann's avatar
Gerd Moellmann committed
63 64
(defcustom face-font-family-alternatives
  '(("courier" "fixed")
65
    ("helv" "helvetica" "arial" "fixed"))
Gerd Moellmann's avatar
Gerd Moellmann committed
66 67 68 69 70
  "*Alist of alternative font family names.
Each element has the the form (FAMILY ALTERNATIVE1 ALTERNATIVE2 ...).
If fonts of family FAMILY can't be loaded, try ALTERNATIVE1, then
ALTERNATIVE2 etc."
  :tag "Alternative font families to try."
Gerd Moellmann's avatar
Gerd Moellmann committed
71
  :type '(repeat (repeat string))
Gerd Moellmann's avatar
Gerd Moellmann committed
72 73 74 75 76 77
  :group 'font-selection
  :set #'(lambda (symbol value)
	   (set-default symbol value)
	   (internal-set-alternative-font-family-alist value)))


78 79
;; This is defined originally in xfaces.c.
(defcustom face-font-registry-alternatives
80 81 82 83
  '(("gb2312.1980" "gb2312.80&gb8565.88" "gbk*")
    ("jisx0208.1990" "jisx0208.1983" "jisx0208.1978")
    ("ksc5601.1989" "ksx1001.1992" "ksc5601.1987")
    ("muletibetan-2" "muletibetan-0"))
84 85
  "*Alist of alternative font registry names.
Each element has the the form (REGISTRY ALTERNATIVE1 ALTERNATIVE2 ...).
86 87 88
If fonts of registry REGISTRY can be loaded, font selection
tries to find a best matching font among all fonts of registry
REGISTRY, ALTERNATIVE1, ALTERNATIVE2, and etc."
89 90
  :tag "Alternative font registries to try."
  :type '(repeat (repeat string))
91
  :version "21.1"
92 93 94 95 96 97
  :group 'font-selection
  :set #'(lambda (symbol value)
	   (set-default symbol value)
	   (internal-set-alternative-font-registry-alist value)))


98

Gerd Moellmann's avatar
Gerd Moellmann committed
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Creation, copying.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


(defun face-list ()
  "Return a list of all defined face names."
  (mapcar #'car face-new-frame-defaults))


;;; ### If not frame-local initialize by what X resources?

(defun make-face (face &optional no-init-from-resources)
  "Define a new face with name FACE, a symbol.
NO-INIT-FROM-RESOURCES non-nil means don't initialize frame-local
variants of FACE from X resources.  (X resources recognized are found
in the global variable `face-x-resources'.)  If FACE is already known
as a face, leave it unmodified.  Value is FACE."
  (interactive "SMake face: ")
  (unless (facep face)
    ;; Make frame-local faces (this also makes the global one).
    (dolist (frame (frame-list))
      (internal-make-lisp-face face frame))
    ;; Add the face to the face menu.
    (when (fboundp 'facemenu-add-new-face)
      (facemenu-add-new-face face))
    ;; Define frame-local faces for all frames from X resources.
    (unless no-init-from-resources
      (make-face-x-resource-internal face)))
  face)


(defun make-empty-face (face)
  "Define a new, empty face with name FACE.
If the face already exists, it is left unmodified.  Value is FACE."
  (interactive "SMake empty face: ")
  (make-face face 'no-init-from-resources))


(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.

If the optional fourth argument NEW-FRAME is given,
copy the information from face OLD-FACE on frame FRAME
to NEW-FACE on frame NEW-FRAME."
  (let ((inhibit-quit t))
    (if (null frame)
	(progn
	  (dolist (frame (frame-list))
	    (copy-face old-face new-face frame))
	  (copy-face old-face new-face t))
      (internal-copy-lisp-face old-face new-face frame new-frame))
    new-face))



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Obsolete functions
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; The functions in this section are defined because Lisp packages use
;; them, despite the prefix `internal-' suggesting that they are
Dave Love's avatar
Dave Love committed
171
;; private to the face implementation.
Gerd Moellmann's avatar
Gerd Moellmann committed
172 173 174 175 176 177 178 179 180 181 182 183

(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.

This function is defined for compatibility with Emacs 20.2.  It
should not be used anymore."
  (facep name))
184
(make-obsolete 'internal-find-face 'facep "21.1")
Gerd Moellmann's avatar
Gerd Moellmann committed
185 186 187 188 189 190 191 192 193 194 195 196 197


(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.

This function is defined for compatibility with Emacs 20.2.  It
should not be used anymore."
  (or (internal-find-face name frame)
      (check-face name)))
198
(make-obsolete 'internal-get-face "See `facep' and `check-face'." "21.1")
Gerd Moellmann's avatar
Gerd Moellmann committed
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Predicates, type checks.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defun facep (face)
  "Return non-nil if FACE is a face name."
  (internal-lisp-face-p face))


(defun check-face (face)
  "Signal an error if FACE doesn't name a face.
Value is FACE."
  (unless (facep face)
    (error "Not a face: %s" face))
  face)


;; The ID returned is not to be confused with the internally used IDs
;; of realized faces.  The ID assigned to Lisp faces is used to
;; support faces in display table entries.

(defun face-id (face &optional frame)
  "Return the interNal ID of face with name FACE.
If optional argument FRAME is nil or omitted, use the selected frame."
  (check-face face)
  (get face 'face))


(defun face-equal (face1 face2 &optional frame)
  "Non-nil if faces FACE1 and FACE2 are equal.
Faces are considered equal if all their attributes are equal.
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."
  (internal-lisp-face-equal-p face1 face2 frame))


(defun face-differs-from-default-p (face &optional frame)
  "Non-nil if FACE displays differently from the default face.
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.
A face is considered to be ``the same'' as the default face if it is
actually specified in the same way (equal attributes) or if it is
fully-unspecified, and thus inherits the attributes of any face it
is displayed on top of."
247 248 249 250 251 252 253 254 255 256 257 258 259
  (cond ((eq frame t) (setq frame nil))
	((null frame) (setq frame (selected-frame))))
  (let* ((v1 (internal-lisp-face-p face frame))
	 (n (if v1 (length v1) 0))
	 (v2 (internal-lisp-face-p 'default frame))
	 (i 1))
    (unless v1
      (error "Not a face: %S" face))
    (while (and (< i n)
		(or (eq 'unspecified (aref v1 i))
		    (equal (aref v1 i) (aref v2 i))))
      (setq i (1+ i)))
    (< i n)))
Gerd Moellmann's avatar
Gerd Moellmann committed
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292


(defun face-nontrivial-p (face &optional frame)
  "True if face FACE has some non-nil attribute.
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."
  (not (internal-lisp-face-empty-p face frame)))



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Setting face attributes from X resources.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defcustom face-x-resources
  '((:family (".attributeFamily" . "Face.AttributeFamily"))
    (:width (".attributeWidth" . "Face.AttributeWidth"))
    (:height (".attributeHeight" . "Face.AttributeHeight"))
    (:weight (".attributeWeight" . "Face.AttributeWeight"))
    (:slant (".attributeSlant" . "Face.AttributeSlant"))
    (:foreground (".attributeForeground" . "Face.AttributeForeground"))
    (:background (".attributeBackground" . "Face.AttributeBackground"))
    (:overline (".attributeOverline" . "Face.AttributeOverline"))
    (:strike-through (".attributeStrikeThrough" . "Face.AttributeStrikeThrough"))
    (:box (".attributeBox" . "Face.AttributeBox"))
    (:underline (".attributeUnderline" . "Face.AttributeUnderline"))
    (:inverse-video (".attributeInverse" . "Face.AttributeInverse"))
    (:stipple
     (".attributeStipple" . "Face.AttributeStipple")
     (".attributeBackgroundPixmap" . "Face.AttributeBackgroundPixmap"))
    (:bold (".attributeBold" . "Face.AttributeBold"))
    (:italic (".attributeItalic" . "Face.AttributeItalic"))
293 294
    (:font (".attributeFont" . "Face.AttributeFont"))
    (:inherit (".attributeInherit" . "Face.AttributeInherit")))
Gerd Moellmann's avatar
Gerd Moellmann committed
295 296 297 298 299
  "*List of X resources and classes for face attributes.
Each element has the form (ATTRIBUTE ENTRY1 ENTRY2...) where ATTRIBUTE is
the name of a face attribute, and each ENTRY is a cons of the form
(RESOURCE . CLASS) with RESOURCE being the resource and CLASS being the
X resource class for the attribute."
300
  :type '(repeat (cons symbol (repeat (cons string string))))
Gerd Moellmann's avatar
Gerd Moellmann committed
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
  :group 'faces)


(defun set-face-attribute-from-resource (face attribute resource class frame)
  "Set FACE's ATTRIBUTE from X resource RESOURCE, class CLASS on FRAME.
Value is the attribute value specified by the resource, or nil
if not present.  This function displays a message if the resource
specifies an invalid attribute."
  (let* ((face-name (face-name face))
	 (value (internal-face-x-get-resource (concat face-name resource)
					      class frame)))
    (when value
      (condition-case ()
	  (internal-set-lisp-face-attribute-from-resource
	   face attribute (downcase value) frame)
	(error
	 (message "Face %s, frame %s: invalid attribute %s %s from X resource"
		  face-name frame attribute value))))
    value))


(defun set-face-attributes-from-resources (face frame)
  "Set attributes of FACE from X resources for FRAME."
324
  (when (memq (framep frame) '(x w32 mac))
Gerd Moellmann's avatar
Gerd Moellmann committed
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
    (dolist (definition face-x-resources)
      (let ((attribute (car definition)))
	(dolist (entry (cdr definition))
	  (set-face-attribute-from-resource face attribute (car entry)
					    (cdr entry) frame))))))
    
  
(defun make-face-x-resource-internal (face &optional frame)
  "Fill frame-local FACE on FRAME from X resources.
FRAME nil or not specified means do it for all frames."
  (if (null frame)
      (dolist (frame (frame-list))
	(set-face-attributes-from-resources face frame))
    (set-face-attributes-from-resources face frame)))



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Retrieving face attributes.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

346
(defun face-name (face)
Eric S. Raymond's avatar
Eric S. Raymond committed
347
  "Return the name of face FACE."
Gerd Moellmann's avatar
Gerd Moellmann committed
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
  (symbol-name (check-face face)))


(defun face-attribute (face attribute &optional frame)
  "Return the value of FACE's ATTRIBUTE on FRAME.
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."
  (internal-get-lisp-face-attribute face attribute frame))


(defun face-foreground (face &optional frame)
  "Return the foreground color name of 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."
364 365 366 367
  (let ((value (internal-get-lisp-face-attribute face :foreground frame)))
    (if (eq value 'unspecified)
	nil 
      value)))
Gerd Moellmann's avatar
Gerd Moellmann committed
368 369 370 371 372 373 374


(defun face-background (face &optional frame)
  "Return the background color name of 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."
375 376 377 378
  (let ((value (internal-get-lisp-face-attribute face :background frame)))
    (if (eq value 'unspecified)
	nil
      value)))
Gerd Moellmann's avatar
Gerd Moellmann committed
379 380 381 382 383 384 385


(defun face-stipple (face &optional frame)
 "Return the stipple pixmap name of 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."
386 387 388 389
  (let ((value (internal-get-lisp-face-attribute face :stipple frame)))
    (if (eq value 'unspecified)
	nil
      value)))
Gerd Moellmann's avatar
Gerd Moellmann committed
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417


(defalias 'face-background-pixmap 'face-stipple)


(defun face-underline-p (face &optional frame)
 "Return non-nil if FACE is underlined.
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."
 (eq (face-attribute face :underline frame) t))


(defun face-inverse-video-p (face &optional frame)
 "Return non-nil if FACE is in inverse video on FRAME.
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."
 (eq (face-attribute face :inverse-video frame) t))


(defun face-bold-p (face &optional frame)
  "Return non-nil if the font of FACE is bold on FRAME.
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.
Use `face-attribute' for finer control."
  (let ((bold (face-attribute face :weight frame)))
418
    (memq bold '(semi-bold bold extra-bold ultra-bold))))
Gerd Moellmann's avatar
Gerd Moellmann committed
419 420 421 422 423 424 425 426 427


(defun face-italic-p (face &optional frame)
  "Return non-nil if the font of FACE is italic on FRAME.
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.
Use `face-attribute' for finer control."
  (let ((italic (face-attribute face :slant frame)))
428
    (memq italic '(italic oblique))))
Gerd Moellmann's avatar
Gerd Moellmann committed
429 430 431 432 433 434 435 436 437 438 439 440 441 442
    


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Face documentation.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defun face-documentation (face)
  "Get the documentation string for FACE."
  (get face 'face-documentation))


(defun set-face-documentation (face string)
  "Set the documentation string for FACE to STRING."
443
  ;; Perhaps the text should go in DOC.
444
  (put face 'face-documentation (purecopy string)))
Gerd Moellmann's avatar
Gerd Moellmann committed
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484


(defalias 'face-doc-string 'face-documentation)
(defalias 'set-face-doc-string 'set-face-documentation)



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Setting face attributes.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


(defun set-face-attribute (face frame &rest args)
  "Set attributes of FACE on FRAME from ARGS.

FRAME nil means change attributes on all frames.  FRAME t means change
the default for new frames (this is done automatically each time an
attribute is changed on all frames).

ARGS must come in pairs ATTRIBUTE VALUE.  ATTRIBUTE must be a valid
face attribute name.  All attributes can be set to `unspecified';
this fact is not further mentioned below.

The following attributes are recognized:

`:family'

VALUE must be a string specifying the font family, e.g. ``courier'',
or a fontset alias name.  If a font family is specified, wild-cards `*'
and `?' are allowed.

`:width'

VALUE specifies the relative proportionate width of the font to use.
It must be one of the symbols `ultra-condensed', `extra-condensed',
`condensed', `semi-condensed', `normal', `semi-expanded', `expanded',
`extra-expanded', or `ultra-expanded'.

`:height'

Miles Bader's avatar
Miles Bader committed
485 486 487 488
VALUE must be either an integer specifying the height of the font to use
in 1/10 pt, a floating point number specifying the amount by which to
scale any underlying face, or a function, which is called with the old
height (from the underlying face), and should return the new height.
Gerd Moellmann's avatar
Gerd Moellmann committed
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537

`:weight'

VALUE specifies the weight of the font to use.  It must be one of the
symbols `ultra-bold', `extra-bold', `bold', `semi-bold', `normal',
`semi-light', `light', `extra-light', `ultra-light'.

`:slant'

VALUE specifies the slant of the font to use.  It must be one of the
symbols `italic', `oblique', `normal', `reverse-italic', or
`reverse-oblique'.

`:foreground', `:background'

VALUE must be a color name, a string.

`:underline'

VALUE specifies whether characters in FACE should be underlined.  If
VALUE is t, underline with foreground color of the face.  If VALUE is
a string, underline with that color.  If VALUE is nil, explicitly
don't underline.

`:overline'

VALUE specifies whether characters in FACE should be overlined.  If
VALUE is t, overline with foreground color of the face.  If VALUE is a
string, overline with that color.  If VALUE is nil, explicitly don't
overline.

`:strike-through'

VALUE specifies whether characters in FACE should be drawn with a line
striking through them.  If VALUE is t, use the foreground color of the
face.  If VALUE is a string, strike-through with that color.  If VALUE
is nil, explicitly don't strike through.

`:box'

VALUE specifies whether characters in FACE should have a box drawn
around them.  If VALUE is nil, explicitly don't draw boxes.  If
VALUE is t, draw a box with lines of width 1 in the foreground color
of the face.  If VALUE is a string, the string must be a color name,
and the box is drawn in that color with a line width of 1.  Otherwise,
VALUE must be a property list of the form `(:line-width WIDTH
:color COLOR :style STYLE)'.  If a keyword/value pair is missing from
the property list, a default value will be used for the value, as
specified below.  WIDTH specifies the width of the lines to draw; it
538 539 540
defaults to 1.  If WIDTH is negative, the absolute value is the width
of the lines, and draw top/bottom lines inside the characters area,
not around it.  COLOR is the name of the color to draw in, default is
Gerd Moellmann's avatar
Gerd Moellmann committed
541 542 543 544 545 546 547 548 549 550 551
the foreground color of the face for simple boxes, and the background
color of the face for 3D boxes.  STYLE specifies whether a 3D box
should be draw.  If STYLE is `released-button', draw a box looking
like a released 3D button.  If STYLE is `pressed-button' draw a box
that appears like a pressed button.  If STYLE is nil, the default if
the property list doesn't contain a style specification, draw a 2D
box.

`:inverse-video'

VALUE specifies whether characters in FACE should be displayed in
Dave Love's avatar
Dave Love committed
552
inverse video.  VALUE must be one of t or nil.
Gerd Moellmann's avatar
Gerd Moellmann committed
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573

`:stipple'

If VALUE is a string, it must be the name of a file of pixmap data.
The directories listed in the `x-bitmap-file-path' variable are
searched.  Alternatively, VALUE 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.  VALUE nil means
explicitly don't use a stipple pattern.

For convenience, attributes `:family', `:width', `:height', `:weight',
and `:slant' may also be set in one step from an X font name:

`:font'

Set font-related face attributes from VALUE.  VALUE must be a valid
XLFD font name.  If it is a font name pattern, the first matching font
will be used.

For compatibility with Emacs 20, keywords `:bold' and `:italic' can
be used to specify that a bold or italic font should be used.  VALUE
Miles Bader's avatar
Miles Bader committed
574 575 576 577 578 579 580
must be t or nil in that case.  A value of `unspecified' is not allowed.

`:inherit'

VALUE is the name of a face from which to inherit attributes, or a list
of face names.  Attributes from inherited faces are merged into the face
like an underlying face would be, with higher priority than underlying faces."
581 582 583 584 585 586 587
  (let ((where (if (null frame) 0 frame)))
    (setq args (purecopy args))
    (while args
      (internal-set-lisp-face-attribute face (car args)
					(purecopy (cadr args))
					where)
      (setq args (cdr (cdr args))))))
Gerd Moellmann's avatar
Gerd Moellmann committed
588 589


590
(defun make-face-bold (face &optional frame noerror)
Gerd Moellmann's avatar
Gerd Moellmann committed
591 592
  "Make the font of FACE be bold, if possible.
FRAME nil or not specified means change face on all frames.
593
Argument NOERROR is ignored and retained for compatibility.
Gerd Moellmann's avatar
Gerd Moellmann committed
594
Use `set-face-attribute' for finer control of the font weight."
595
  (interactive (list (read-face-name "Make which face bold ")))
Gerd Moellmann's avatar
Gerd Moellmann committed
596 597 598
  (set-face-attribute face frame :weight 'bold))


599
(defun make-face-unbold (face &optional frame noerror)
Gerd Moellmann's avatar
Gerd Moellmann committed
600
  "Make the font of FACE be non-bold, if possible.
601 602
FRAME nil or not specified means change face on all frames.
Argument NOERROR is ignored and retained for compatibility."
603
  (interactive (list (read-face-name "Make which face non-bold ")))
Gerd Moellmann's avatar
Gerd Moellmann committed
604 605 606
  (set-face-attribute face frame :weight 'normal))

  
607
(defun make-face-italic (face &optional frame noerror)
Gerd Moellmann's avatar
Gerd Moellmann committed
608 609
  "Make the font of FACE be italic, if possible.
FRAME nil or not specified means change face on all frames.
610
Argument NOERROR is ignored and retained for compatibility.
Gerd Moellmann's avatar
Gerd Moellmann committed
611
Use `set-face-attribute' for finer control of the font slant."
612
  (interactive (list (read-face-name "Make which face italic ")))
Gerd Moellmann's avatar
Gerd Moellmann committed
613 614 615
  (set-face-attribute face frame :slant 'italic))


616
(defun make-face-unitalic (face &optional frame noerror)
Gerd Moellmann's avatar
Gerd Moellmann committed
617
  "Make the font of FACE be non-italic, if possible.
Dave Love's avatar
Dave Love committed
618 619
FRAME nil or not specified means change face on all frames.
Argument NOERROR is ignored and retained for compatibility."
620
  (interactive (list (read-face-name "Make which face non-italic ")))
Gerd Moellmann's avatar
Gerd Moellmann committed
621 622 623
  (set-face-attribute face frame :slant 'normal))

  
624
(defun make-face-bold-italic (face &optional frame noerror)
Gerd Moellmann's avatar
Gerd Moellmann committed
625 626
  "Make the font of FACE be bold and italic, if possible.
FRAME nil or not specified means change face on all frames.
627
Argument NOERROR is ignored and retained for compatibility.
Gerd Moellmann's avatar
Gerd Moellmann committed
628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
Use `set-face-attribute' for finer control of font weight and slant."
  (interactive (list (read-face-name "Make which face bold-italic: ")))
  (set-face-attribute face frame :weight 'bold :slant 'italic))


(defun set-face-font (face font &optional frame)
  "Change font-related attributes of FACE to those of FONT (a string).
FRAME nil or not specified means change face on all frames.
This sets the attributes `:family', `:width', `:height', `:weight',
and `:slant'.  When called interactively, prompt for the face and font."
  (interactive (read-face-and-attribute :font))
  (set-face-attribute face frame :font font))


;; Implementation note: Emulating gray background colors with a
;; stipple pattern is now part of the face realization process, and is
;; done in C depending on the frame on which the face is realized.

(defun set-face-background (face color &optional frame)
  "Change the background color of face FACE to COLOR (a string).
FRAME nil or not specified means change face on all frames.
When called interactively, prompt for the face and color."
  (interactive (read-face-and-attribute :background))
651
  (set-face-attribute face frame :background (or color 'unspecified)))
Gerd Moellmann's avatar
Gerd Moellmann committed
652 653 654 655 656 657 658


(defun set-face-foreground (face color &optional frame)
  "Change the foreground color of face FACE to COLOR (a string).
FRAME nil or not specified means change face on all frames.
When called interactively, prompt for the face and color."
  (interactive (read-face-and-attribute :foreground))
659
  (set-face-attribute face frame :foreground (or color 'unspecified)))
Gerd Moellmann's avatar
Gerd Moellmann committed
660 661 662 663 664


(defun set-face-stipple (face stipple &optional frame)
  "Change the stipple pixmap of face FACE to STIPPLE.
FRAME nil or not specified means change face on all frames.
Dave Love's avatar
Dave Love committed
665
STIPPLE should be a string, the name of a file of pixmap data.
Gerd Moellmann's avatar
Gerd Moellmann committed
666 667 668 669 670 671
The directories listed in the `x-bitmap-file-path' variable are searched.

Alternatively, STIPPLE 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."
  (interactive (read-face-and-attribute :stipple))
672
  (set-face-attribute face frame :stipple (or stipple 'unspecified)))
Gerd Moellmann's avatar
Gerd Moellmann committed
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743


(defun set-face-underline (face underline &optional frame)
  "Specify whether face FACE is underlined.
UNDERLINE nil means FACE explicitly doesn't underline.
UNDERLINE non-nil means FACE explicitly does underlining
with the same of the foreground color.
If UNDERLINE is a string, underline with the color named UNDERLINE.
FRAME nil or not specified means change face on all frames.
Use `set-face-attribute' to ``unspecify'' underlining."
  (interactive
   (let ((list (read-face-and-attribute :underline)))
     (list (car list) (eq (car (cdr list)) t))))
  (set-face-attribute face frame :underline underline))


(defun set-face-underline-p (face underline-p &optional frame)
  "Specify whether face FACE is underlined.
UNDERLINE-P nil means FACE explicitly doesn't underline.
UNDERLINE-P non-nil means FACE explicitly does underlining.
FRAME nil or not specified means change face on all frames.
Use `set-face-attribute' to ``unspecify'' underlining."
  (interactive
   (let ((list (read-face-and-attribute :underline)))
     (list (car list) (eq (car (cdr list)) t))))
  (set-face-attribute face frame :underline underline-p))


(defun set-face-inverse-video-p (face inverse-video-p &optional frame)
  "Specify whether face FACE is in inverse video.
INVERSE-VIDEO-P non-nil means FACE displays explicitly in inverse video.
INVERSE-VIDEO-P nil means FACE explicitly is not in inverse video.
FRAME nil or not specified means change face on all frames.
Use `set-face-attribute' to ``unspecify'' the inverse video attribute."
  (interactive
   (let ((list (read-face-and-attribute :inverse-video)))
     (list (car list) (eq (car (cdr list)) t))))
  (set-face-attribute face frame :inverse-video inverse-video-p))


(defun set-face-bold-p (face bold-p &optional frame)
  "Specify whether face FACE is bold.
BOLD-P non-nil means FACE should explicitly display bold.
BOLD-P nil means FACE should explicitly display non-bold.
FRAME nil or not specified means change face on all frames.
Use `set-face-attribute' or `modify-face' for finer control."
  (if (null bold-p)
      (make-face-unbold face frame)
    (make-face-bold face frame)))


(defun set-face-italic-p (face italic-p &optional frame)
  "Specify whether face FACE is italic.
ITALIC-P non-nil means FACE should explicitly display italic.
ITALIC-P nil means FACE should explicitly display non-italic.
FRAME nil or not specified means change face on all frames.
Use `set-face-attribute' or `modify-face' for finer control."
  (if (null italic-p)
      (make-face-unitalic face frame)
    (make-face-italic face frame)))


(defalias 'set-face-background-pixmap 'set-face-stipple)


(defun invert-face (face &optional frame)
  "Swap the foreground and background colors of FACE.
FRAME nil or not specified means change face on all frames.
If FACE specifies neither foreground nor background color,
set its foreground and background to the background and foreground
of the default face.  Value is FACE."
744
  (interactive (list (read-face-name "Invert face ")))
Gerd Moellmann's avatar
Gerd Moellmann committed
745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
  (let ((fg (face-attribute face :foreground frame))
	(bg (face-attribute face :background frame)))
    (if (or fg bg)
	(set-face-attribute face frame :foreground bg :background fg)
      (set-face-attribute face frame
			  :foreground
			  (face-attribute 'default :background frame)
			  :background
			  (face-attribute 'default :foreground frame))))
  face)


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Interactively modifying faces.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defun read-face-name (prompt)
  "Read and return a face symbol, prompting with PROMPT.
Value is a symbol naming a known face."
  (let ((face-list (mapcar #'(lambda (x) (cons (symbol-name x) x))
			   (face-list)))
766
	(def (thing-at-point 'symbol))
Gerd Moellmann's avatar
Gerd Moellmann committed
767
	face)
768
    (cond ((assoc def face-list)
Miles Bader's avatar
Miles Bader committed
769
	   (setq prompt (concat prompt " (default " def "): ")))
770 771 772 773
	  (t (setq def nil)
	     (setq prompt (concat prompt ": "))))
    (while (equal "" (setq face (completing-read
				 prompt face-list nil t nil nil def))))
Gerd Moellmann's avatar
Gerd Moellmann committed
774 775 776 777 778 779 780 781 782 783
    (intern face)))


(defun face-valid-attribute-values (attribute &optional frame)
  "Return valid values for face attribute ATTRIBUTE.
The optional argument FRAME is used to determine available fonts
and colors.  If it is nil or not specified, the selected frame is
used.  Value is an alist of (NAME . VALUE) if ATTRIBUTE expects a value
out of a set of discrete values.  Value is `integerp' if ATTRIBUTE expects
an integer value."
784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
  (let (valid)
    (setq valid
	  (case attribute
	    (:family
	     (if window-system
		 (mapcar #'(lambda (x) (cons (car x) (car x)))
			 (x-font-family-list))
	       ;; Only one font on TTYs.
	       (list (cons "default" "default"))))
	    ((:width :weight :slant :inverse-video)
	     (mapcar #'(lambda (x) (cons (symbol-name x) x))
		     (internal-lisp-face-attribute-values attribute)))
	    ((:underline :overline :strike-through :box)
	     (if window-system
		 (nconc (mapcar #'(lambda (x) (cons (symbol-name x) x))
				(internal-lisp-face-attribute-values attribute))
			(mapcar #'(lambda (c) (cons c c))
				(x-defined-colors frame)))
	       (mapcar #'(lambda (x) (cons (symbol-name x) x))
		       (internal-lisp-face-attribute-values attribute))))
	    ((:foreground :background)
	     (mapcar #'(lambda (c) (cons c c))
806
		     (defined-colors frame)))
807 808 809
	    ((:height)
	     'integerp)
	    (:stipple
810
	     (and (memq window-system '(x w32 mac))
811
		  (mapcar #'list
812 813 814 815 816 817
			  (apply #'nconc
				 (mapcar (lambda (dir)
					   (and (file-readable-p dir)
						(file-directory-p dir)
						(directory-files dir)))
					 x-bitmap-file-path)))))
Miles Bader's avatar
Miles Bader committed
818 819 820 821
	    (:inherit
	     (cons '("none" . nil)
		   (mapcar #'(lambda (c) (cons (symbol-name c) c))
			   (face-list))))
822 823
	    (t
	     (error "Internal error"))))
Miles Bader's avatar
Miles Bader committed
824
    (if (and (listp valid) (not (memq attribute '(:inherit))))
825 826 827
	(nconc (list (cons "unspecified" 'unspecified)) valid)
      valid)))
	       
Gerd Moellmann's avatar
Gerd Moellmann committed
828 829 830 831 832 833 834 835 836 837 838 839 840 841 842


(defvar face-attribute-name-alist
  '((:family . "font family")
    (:width . "character set width")
    (:height . "height in 1/10 pt")
    (:weight . "weight")
    (:slant . "slant")
    (:underline . "underline")
    (:overline . "overline")
    (:strike-through . "strike-through")
    (:box . "box")
    (:inverse-video . "inverse-video display")
    (:foreground . "foreground color")
    (:background . "background color")
Miles Bader's avatar
Miles Bader committed
843 844
    (:stipple . "background stipple")
    (:inherit . "inheritance"))
Gerd Moellmann's avatar
Gerd Moellmann committed
845 846 847 848 849 850 851 852 853 854 855 856 857
  "An alist of descriptive names for face attributes.
Each element has the form (ATTRIBUTE-NAME . DESCRIPTION) where
ATTRIBUTE-NAME is a face attribute name (a keyword symbol), and
DESCRIPTION is a descriptive name for ATTRIBUTE-NAME.")


(defun face-descriptive-attribute-name (attribute)
  "Return a descriptive name for ATTRIBUTE."
  (cdr (assq attribute face-attribute-name-alist)))


(defun face-read-string (face default name &optional completion-alist)
  "Interactively read a face attribute string value.
Miles Bader's avatar
Miles Bader committed
858 859 860 861
FACE is the face whose attribute is read.  If non-nil, DEFAULT is the
default string to return if no new value is entered.  NAME is a
descriptive name of the attribute for prompting.  COMPLETION-ALIST is an
alist of valid values, if non-nil.
Gerd Moellmann's avatar
Gerd Moellmann committed
862

Miles Bader's avatar
Miles Bader committed
863
Entering nothing accepts the default string DEFAULT.
Gerd Moellmann's avatar
Gerd Moellmann committed
864
Value is the new attribute value."
Miles Bader's avatar
Miles Bader committed
865 866 867
  ;; Capitalize NAME (we don't use `capitalize' because that capitalizes
  ;; each word in a string separately).
  (setq name (concat (upcase (substring name 0 1)) (substring name 1)))
Gerd Moellmann's avatar
Gerd Moellmann committed
868 869 870
  (let* ((completion-ignore-case t)
	 (value (completing-read
		 (if default
Miles Bader's avatar
Miles Bader committed
871 872 873
		     (format "%s for face `%s' (default %s): "
			     name face default)
		   (format "%s for face `%s': " name face))
Gerd Moellmann's avatar
Gerd Moellmann committed
874
		 completion-alist)))
875
    (if (equal value "") default value)))
Gerd Moellmann's avatar
Gerd Moellmann committed
876 877 878 879 880 881 882


(defun face-read-integer (face default name)
  "Interactively read an integer face attribute value.
FACE is the face whose attribute is read.  DEFAULT is the default
value to return if no new value is entered.  NAME is a descriptive
name of the attribute for prompting.  Value is the new attribute value."
883 884
  (let ((new-value
	 (face-read-string face
Miles Bader's avatar
Miles Bader committed
885
			   (format "%s" default)
886 887
			   name
			   (list (cons "unspecified" 'unspecified)))))
Miles Bader's avatar
Miles Bader committed
888 889 890 891 892 893
    (cond ((equal new-value "unspecified")
	   'unspecified)
	  ((member new-value '("unspecified-fg" "unspecified-bg"))
	   new-value)
	  (t
	   (string-to-int new-value)))))
Gerd Moellmann's avatar
Gerd Moellmann committed
894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913


(defun read-face-attribute (face attribute &optional frame)
  "Interactively read a new value for FACE's ATTRIBUTE.
Optional argument FRAME nil or unspecified means read an attribute value
of a global face.  Value is the new attribute value."
  (let* ((old-value (face-attribute face attribute frame))
	 (attribute-name (face-descriptive-attribute-name attribute))
	 (valid (face-valid-attribute-values attribute frame))
	 new-value)
    ;; Represent complex attribute values as strings by printing them
    ;; out.  Stipple can be a vector; (WIDTH HEIGHT DATA).  Box can be
    ;; a list `(:width WIDTH :color COLOR)' or `(:width WIDTH :shadow
    ;; SHADOW)'.
    (when (and (or (eq attribute :stipple)
		   (eq attribute :box))
	       (or (consp old-value)
		   (vectorp old-value)))
      (setq old-value (prin1-to-string old-value)))
    (cond ((listp valid)
Miles Bader's avatar
Miles Bader committed
914 915 916 917 918 919 920 921 922 923 924 925 926 927
	   (let ((default
		   (or (car (rassoc old-value valid))
		       (format "%s" old-value))))
	     (setq new-value
		   (face-read-string face default attribute-name valid))
	     (if (equal new-value default)
		 ;; Nothing changed, so don't bother with all the stuff
		 ;; below.  In particular, this avoids a non-tty color
		 ;; from being canonicalized for a tty when the user
		 ;; just uses the default.
		 (setq new-value old-value)
	       ;; Terminal frames can support colors that don't appear
	       ;; explicitly in VALID, using color approximation code
	       ;; in tty-colors.el.
928 929 930 931 932
	       (when (and (memq attribute '(:foreground :background))
			  (not (memq window-system '(x w32 mac)))
			  (not (member new-value
				       '("unspecified"
					 "unspecified-fg" "unspecified-bg"))))
Miles Bader's avatar
WS  
Miles Bader committed
933
		 (setq new-value (car (tty-color-desc new-value frame))))
934 935
	       (when (assoc new-value valid)
		 (setq new-value (cdr (assoc new-value valid)))))))
Gerd Moellmann's avatar
Gerd Moellmann committed
936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953
	  ((eq valid 'integerp)
	   (setq new-value (face-read-integer face old-value attribute-name)))
	  (t (error "Internal error")))
    ;; Convert stipple and box value text we read back to a list or
    ;; vector if it looks like one.  This makes the assumption that a
    ;; pixmap file name won't start with an open-paren.
    (when (and (or (eq attribute :stipple)
		   (eq attribute :box))
	       (stringp new-value)
	       (string-match "^[[(]" new-value))
      (setq new-value (read new-value)))
    new-value))


(defun read-face-font (face &optional frame)
  "Read the name of a font for FACE on FRAME.
If optional argument FRAME Is nil or omitted, use the selected frame."
  (let ((completion-ignore-case t))
954
    (completing-read (format "Set font attributes of face `%s' from font: " face)
955
		     (mapcar 'list (x-list-fonts "*" nil frame)))))
Gerd Moellmann's avatar
Gerd Moellmann committed
956 957 958 959 960 961 962 963 964 965 966 967 968


(defun read-all-face-attributes (face &optional frame)
  "Interactively read all attributes for FACE.
If optional argument FRAME Is nil or omitted, use the selected frame.
Value is a property list of attribute names and new values."
  (let (result)
    (dolist (attribute face-attribute-name-alist result)
      (setq result (cons (car attribute)
			 (cons (read-face-attribute face (car attribute) frame)
			       result))))))

    
969 970
(defun modify-face (&optional face foreground background stipple
			      bold-p italic-p underline-p inverse-p frame)
Gerd Moellmann's avatar
Gerd Moellmann committed
971 972
  "Modify attributes of faces interactively.
If optional argument FRAME is nil or omitted, modify the face used
973 974 975 976
for newly created frame, i.e. the global face.
For non-interactive use, `set-face-attribute' is preferred.
When called from elisp, if FACE is nil, all arguments but FRAME are ignored
and the face and its settings are obtained by querying the user."
Gerd Moellmann's avatar
Gerd Moellmann committed
977
  (interactive)
978 979 980 981 982 983 984 985 986 987
  (if face
      (set-face-attribute face frame
			  :foreground (or foreground 'unspecified)
			  :background (or background 'unspecified)
			  :stipple stipple
			  :bold bold-p
			  :italic italic-p
			  :underline underline-p
			  :inverse-video inverse-p)
    (setq face (read-face-name "Modify face"))
Gerd Moellmann's avatar
Gerd Moellmann committed
988 989 990 991 992 993 994 995
    (apply #'set-face-attribute face frame
	   (read-all-face-attributes face frame))))

(defun read-face-and-attribute (attribute &optional frame)
  "Read face name and face attribute value.
ATTRIBUTE is the attribute whose new value is read.
FRAME nil or unspecified means read attribute value of global face.
Value is a list (FACE NEW-VALUE) where FACE is the face read
996
\(a symbol), and NEW-VALUE is value read."
Gerd Moellmann's avatar
Gerd Moellmann committed
997
  (cond ((eq attribute :font)
998
	 (let* ((prompt "Set font-related attributes of face")
Gerd Moellmann's avatar
Gerd Moellmann committed
999 1000 1001 1002 1003
		(face (read-face-name prompt))
		(font (read-face-font face frame)))
	   (list face font)))
	(t
	 (let* ((attribute-name (face-descriptive-attribute-name attribute))
Miles Bader's avatar
Miles Bader committed
1004
		(prompt (format "Set %s of face" attribute-name))
Gerd Moellmann's avatar
Gerd Moellmann committed
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
		(face (read-face-name prompt))
		(new-value (read-face-attribute face attribute frame)))
	   (list face new-value)))))



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Listing faces.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(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'."
  (interactive)
  (let ((faces (sort (face-list) #'string-lessp))
	(face nil)
	(frame (selected-frame))
1031
	disp-frame window face-name)
Gerd Moellmann's avatar
Gerd Moellmann committed
1032 1033 1034 1035
    (with-output-to-temp-buffer "*Faces*"
      (save-excursion
	(set-buffer standard-output)
	(setq truncate-lines t)
1036 1037 1038 1039
	(insert
	 (substitute-command-keys
	  (concat
	   "Use "
1040
	   (if (display-mouse-p) "\\[help-follow-mouse] or ")
1041
	   "\\[help-follow] on a face name to customize it\n"
1042 1043
	   "or on its sample text for a decription of the face.\n\n")))
	(setq help-xref-stack nil)
Gerd Moellmann's avatar
Gerd Moellmann committed
1044 1045 1046
	(while faces
	  (setq face (car faces))
	  (setq faces (cdr faces))
1047 1048 1049 1050 1051 1052 1053
	  (setq face-name (symbol-name face))
	  (insert (format "%25s " face-name))
	  ;; Hyperlink to a customization buffer for the face.  Using
	  ;; the help xref mechanism may not be the best way.
	  (save-excursion
	    (save-match-data
	      (search-backward face-name)
Dave Love's avatar
Dave Love committed
1054 1055 1056 1057 1058
	      (help-xref-button 0 (lambda (f)
				    (if help-xref-stack
					(pop help-xref-stack))
				    (customize-face f))
				face-name
1059
				"mouse-2: customize this face")))
Gerd Moellmann's avatar
Gerd Moellmann committed
1060 1061
	  (let ((beg (point)))
	    (insert list-faces-sample-text)
1062 1063 1064 1065
	    ;; Hyperlink to a help buffer for the face.
	    (save-excursion
	      (save-match-data
		(search-backward list-faces-sample-text)
1066 1067
		(help-xref-button 0 #'describe-face face
				  "mouse-2: describe this face")))
Gerd Moellmann's avatar
Gerd Moellmann committed
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
	    (insert "\n")
	    (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))))
	(goto-char (point-min)))
      (print-help-return-message))
    ;; 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)))))))


(defun describe-face (face &optional frame)
  "Display the properties of face FACE on FRAME.
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."
Dave Love's avatar
Dave Love committed
1096
  (interactive (list (read-face-name "Describe face")))
Gerd Moellmann's avatar
Gerd Moellmann committed
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
  (let* ((attrs '((:family . "Family")
		  (:width . "Width")
		  (:height . "Height")
		  (:weight . "Weight")
		  (:slant . "Slant")
		  (:foreground . "Foreground")
		  (:background . "Background")
		  (:underline . "Underline")
		  (:overline . "Overline")
		  (:strike-through . "Strike-through")
		  (:box . "Box")
		  (:inverse-video . "Inverse")
1109
		  (:stipple . "Stipple")
1110 1111
		  (:font . "Font or fontset")
		  (:inherit . "Inherit")))
Gerd Moellmann's avatar
Gerd Moellmann committed
1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
	(max-width (apply #'max (mapcar #'(lambda (x) (length (cdr x)))
					attrs))))
    (with-output-to-temp-buffer "*Help*"
      (save-excursion
	(set-buffer standard-output)
	(dolist (a attrs)
	  (let ((attr (face-attribute face (car a) frame)))
	    (insert (make-string (- max-width (length (cdr a))) ?\ )
		    (cdr a) ": " (format "%s" attr) "\n")))
	(insert "\nDocumentation:\n\n"
		(or (face-documentation face)
Dave Love's avatar
Dave Love committed
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137
		    "not documented as a face."))
	(let ((customize-label "customize"))
	  (terpri)
	  (terpri)
	  (princ (concat "You can " customize-label " this face."))
	  (with-current-buffer "*Help*"
	    (save-excursion
	      (re-search-backward
	       (concat "\\(" customize-label "\\)") nil t)
	      (help-xref-button 1 #'customize-face face
				"mouse-2, RET: customize face")))))
      (print-help-return-message)
      (with-current-buffer "*Help*"
	(help-setup-xref (list #'describe-face face) (interactive-p))
	(buffer-string)))))
Gerd Moellmann's avatar
Gerd Moellmann committed
1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Face specifications (defface).
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Parameter FRAME Is kept for call compatibility to with previous
;; face implementation.

(defun face-attr-construct (face &optional frame)
  "Return a defface-style attribute list for FACE on FRAME.
Value is a property list of pairs ATTRIBUTE VALUE for all specified
face attributes of FACE where ATTRIBUTE is the attribute name and
VALUE is the specified value of that attribute."
  (let (result)
    (dolist (entry face-attribute-name-alist result)
      (let* ((attribute (car entry))
	     (value (face-attribute face attribute)))
	(unless (eq value 'unspecified)
	  (setq result (nconc (list attribute value) result)))))))
    

(defun face-spec-set-match-display (display frame)
  "Non-nil if DISPLAY matches FRAME.
DISPLAY is part of a spec such as can be used in `defface'.
If FRAME is nil, the current FRAME is used."
  (let* ((conjuncts display)
	 conjunct req options
	 ;; t means we have succeeded against all the conjuncts in
	 ;; DISPLAY that have been tested so far.
	 (match t))
    (if (eq conjuncts t)
	(setq conjuncts nil))
    (while (and conjuncts match)
      (setq conjunct (car conjuncts)
	    conjuncts (cdr conjuncts)
	    req (car conjunct)
	    options (cdr conjunct)
	    match (cond ((eq req 'type)
			 (or (memq window-system options)
1177 1178 1179 1180 1181 1182 1183 1184
			     ;; FIXME: This should be revisited to use
			     ;; display-graphic-p, provided that the
			     ;; color selection depends on the number
			     ;; of supported colors, and all defface's
			     ;; are changed to look at number of colors
			     ;; instead of (type graphic) etc.
			     (and (null window-system)
				  (memq 'tty options))
1185 1186 1187 1188 1189 1190 1191
			     (and (memq 'motif options)
				  (featurep 'motif))
			     (and (memq 'lucid options)
				  (featurep 'x-toolkit)
				  (not (featurep 'motif)))
			     (and (memq 'x-toolkit options)
				  (featurep 'x-toolkit))))
Gerd Moellmann's avatar
Gerd Moellmann committed
1192 1193 1194 1195 1196
			((eq req 'class)
			 (memq (frame-parameter frame 'display-type) options))
			((eq req 'background)
			 (memq (frame-parameter frame 'background-mode)
			       options))
Dave Love's avatar
Dave Love committed
1197
			(t (error "Unknown req `%S' with options `%S'"
Gerd Moellmann's avatar
Gerd Moellmann committed
1198 1199 1200 1201 1202
				  req options)))))
    match))


(defun face-spec-choose (spec &optional frame)
1203 1204
  "Choose the proper attributes for FRAME, out of SPEC.
If SPEC is nil, return nil."
Gerd Moellmann's avatar
Gerd Moellmann committed
1205 1206 1207 1208 1209
  (unless frame
    (setq frame (selected-frame)))
  (let ((tail spec)
	result)
    (while tail
1210 1211 1212
      (let* ((entry (pop tail))
	     (display (car entry))
	     (attrs (cdr entry)))
Gerd Moellmann's avatar
Gerd Moellmann committed
1213
	(when (face-spec-set-match-display display frame)
1214 1215 1216 1217 1218 1219
	  (setq result (if (listp (car attrs))
			   ;; Old-style entry, the attribute list is the
			   ;; first element.
			   (car attrs)
			 attrs)
		tail nil))))
Gerd Moellmann's avatar
Gerd Moellmann committed
1220 1221 1222 1223 1224
    result))


(defun face-spec-reset-face (face &optional frame)
  "Reset all attributes of FACE on FRAME to unspecified."
1225
  (let ((attrs face-attribute-name-alist))
Gerd Moellmann's avatar
Gerd Moellmann committed
1226 1227
    (while attrs
      (let ((attr-and-name (car attrs)))
1228 1229
	(set-face-attribute face frame (car attr-and-name) 'unspecified))
      (setq attrs (cdr attrs)))))
Gerd Moellmann's avatar
Gerd Moellmann committed
1230 1231 1232 1233 1234


(defun face-spec-set (face spec &optional frame)
  "Set FACE's attributes according to the first matching entry in SPEC.
FRAME is the frame whose frame-local face is set.  FRAME nil means
1235 1236
do it on all frames.  See `defface' for information about SPEC.
If SPEC is nil, do nothing."
1237
  (let ((attrs (face-spec-choose spec frame)))
1238 1239
    (when attrs
      (face-spec-reset-face face frame))
Gerd Moellmann's avatar
Gerd Moellmann committed
1240 1241 1242 1243 1244 1245
    (while attrs
      (let ((attribute (car attrs))
	    (value (car (cdr attrs))))
	;; Support some old-style attribute names and values.
	(case attribute
	  (:bold (setq attribute :weight value (if value 'bold 'normal)))
1246
	  (:italic (setq attribute :slant value (if value 'italic 'normal)))
1247 1248 1249 1250
	  ((:foreground :background)
	   ;; Compatibility with 20.x.  Some bogus face specs seem to 
	   ;; exist containing things like `:foreground nil'.
	   (if (null value) (setq value 'unspecified)))
1251 1252 1253
	  (t (unless (assq attribute face-x-resources)
	       (setq attribute nil))))
	(when attribute
1254 1255
	  (set-face-attribute face frame attribute value)))
      (setq attrs (cdr (cdr attrs))))))
Gerd Moellmann's avatar
Gerd Moellmann committed
1256 1257 1258


(defun face-attr-match-p (face attrs &optional frame)
Miles Bader's avatar
Miles Bader committed
1259
  "Return t if attributes of FACE match values in plist ATTRS.
Gerd Moellmann's avatar
Gerd Moellmann committed
1260 1261 1262 1263 1264 1265 1266 1267
Optional parameter FRAME is the frame whose definition of FACE
is used.  If nil or omitted, use the selected frame."
  (unless frame
    (setq frame (selected-frame)))
  (let ((list face-attribute-name-alist)
	(match t))
    (while (and match (not (null list)))
      (let* ((attr (car (car list)))
Miles Bader's avatar
Miles Bader committed
1268 1269 1270 1271
	     (specified-value
	      (if (plist-member attrs attr)
		  (plist-get attrs attr)
		'unspecified))
Gerd Moellmann's avatar
Gerd Moellmann committed
1272
	     (value-now (face-attribute face attr frame)))
Miles Bader's avatar
Miles Bader committed
1273
	(setq match (equal specified-value value-now))
Gerd Moellmann's avatar
Gerd Moellmann committed
1274 1275 1276 1277 1278 1279 1280
	(setq list (cdr list))))
    match))

(defun face-spec-match-p (face spec &optional frame)
  "Return t if FACE, on FRAME, matches what SPEC says it should look like."
  (face-attr-match-p face (face-spec-choose spec frame) frame))

1281
(defsubst face-default-spec (face)
1282 1283 1284
  "Return the default face-spec for FACE, ignoring any user customization.
If there is no default for FACE, return nil."
  (get face 'face-defface-spec))
Gerd Moellmann's avatar
Gerd Moellmann committed
1285

1286 1287
(defsubst face-user-default-spec (face)
  "Return the user's customized face-spec for FACE, or the default if none.
1288
If there is neither a user setting nor a default for FACE, return nil."
1289 1290 1291
  (or (get face 'saved-face)
      (face-default-spec face)))

1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Frame-type independent color support.
;;; We keep the old x-* names as aliases for back-compatibility.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defun defined-colors (&optional frame)
  "Return a list of colors supported for a particular frame.
The argument FRAME specifies which frame to try.
The value may be different for frames on different display types.
If FRAME doesn't support colors, the value is nil."
1303
  (if (memq (framep (or frame (selected-frame))) '(x w32 mac))
1304
      (xw-defined-colors frame)
1305
    (mapcar 'car (tty-color-alist frame))))
1306 1307 1308 1309 1310
(defalias 'x-defined-colors 'defined-colors)

(defun color-defined-p (color &optional frame)
  "Return non-nil if color COLOR is supported on frame FRAME.
If FRAME is omitted or nil, use the selected frame.
1311 1312
If COLOR is the symbol `unspecified' or one of the strings
\"unspecified-fg\" or \"unspecified-bg\", the value is nil."
1313
  (if (member color '(unspecified "unspecified-bg" "unspecified-fg"))
1314
      nil
1315
    (if (member (framep (or frame (selected-frame))) '(x w32 mac))
1316
	(xw-color-defined-p color frame)
1317
      (numberp (tty-color-translate color frame)))))
1318 1319 1320 1321 1322
(defalias 'x-color-defined-p 'color-defined-p)

(defun color-values (color &optional frame)
  "Return a description of the color named COLOR on frame FRAME.
The value is a list of integer RGB values--\(RED GREEN BLUE\).
Gerd Moellmann's avatar
Gerd Moellmann committed
1323
These values appear to range from 0 65535; white is \(65535 65535 65535\).
1324 1325
If FRAME is omitted or nil, use the selected frame.
If FRAME cannot display COLOR, the value is nil.
1326 1327
If COLOR is the symbol `unspecified' or one of the strings
\"unspecified-fg\" or \"unspecified-bg\", the value is nil."
1328
  (if (member color '(unspecified "unspecified-fg" "unspecified-bg"))
1329
      nil
1330
    (if (memq (framep (or frame (selected-frame))) '(x w32 mac))
1331 1332 1333 1334 1335 1336 1337 1338 1339
	(xw-color-values color frame)
      (tty-color-values color frame))))
(defalias 'x-color-values 'color-values)

(defun display-color-p (&optional display)
  "Return t if DISPLAY supports color.
The optional argument DISPLAY specifies which display to ask about.
DISPLAY should be either a frame or a display name (a string).
If omitted or nil, that stands for the selected frame's display."
1340
  (if (memq (framep-on-display display) '(x w32 mac))
1341 1342
      (xw-display-color-p display)
    (tty-display-color-p display)))
1343 1344
(defalias 'x-display-color-p 'display-color-p)

1345 1346 1347 1348 1349 1350 1351 1352 1353
(defun display-grayscale-p (&optional display)
  "Return non-nil if frames on DISPLAY can display shades of gray."
  (let ((frame-type (framep-on-display display)))
    (cond
     ((memq frame-type '(x w32 mac))
      (x-display-grayscale-p display))
     (t
      (> (tty-color-gray-shades display) 2)))))

Gerd Moellmann's avatar
Gerd Moellmann committed
1354 1355 1356 1357 1358 1359 1360 1361 1362

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Background mode.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defcustom frame-background-mode nil
  "*The brightness of the background.
Set this to the symbol `dark' if your background color is dark, `light' if
your background is light, or nil (default) if you want Emacs to
1363 1364
examine the brightness for you.  Don't set this variable with `setq';
this won't have the expected effect."
Gerd Moellmann's avatar
Gerd Moellmann committed
1365 1366
  :group 'faces
  :set #'(lambda (var value)
1367
	   (set-default var value)
1368
	   (mapc 'frame-set-background-mode (frame-list)))
Gerd Moellmann's avatar
Gerd Moellmann committed
1369
  :initialize 'custom-initialize-changed
Dave Love's avatar
Dave Love committed
1370
  :type '(choice (choice-item dark)
Gerd Moellmann's avatar
Gerd Moellmann committed
1371 1372 1373 1374 1375
		 (choice-item light)
		 (choice-item :tag "default" nil)))


(defun frame-set-background-mode (frame)