faces.el 103 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,
Glenn Morris's avatar
Glenn Morris committed
4
;;   2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
Eric S. Raymond's avatar
Eric S. Raymond committed
5

6
;; Maintainer: FSF
Pavel Janík's avatar
Pavel Janík committed
7
;; Keywords: internal
8

Eric S. Raymond's avatar
Eric S. Raymond committed
9 10 11 12
;; 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
13
;; the Free Software Foundation; either version 3, or (at your option)
Eric S. Raymond's avatar
Eric S. Raymond committed
14 15 16 17 18 19 20 21
;; 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
22
;; along with GNU Emacs; see the file COPYING.  If not, write to the
Lute Kamstra's avatar
Lute Kamstra committed
23 24
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
Eric S. Raymond's avatar
Eric S. Raymond committed
25 26 27 28 29

;;; Commentary:

;;; Code:

30
(eval-when-compile
31 32 33
  (require 'cl))

(declare-function xw-defined-colors "term/x-win" (&optional frame))
Gerd Moellmann's avatar
Gerd Moellmann committed
34

35
(defvar help-xref-stack-item)
Gerd Moellmann's avatar
Gerd Moellmann committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; 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
51
elements.  Font selection first tries to find a best matching font
52
for those face attributes that appear before in the list.  For
Gerd Moellmann's avatar
Gerd Moellmann committed
53 54 55
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."
Andreas Schwab's avatar
Andreas Schwab committed
56
  :tag "Font selection order"
Gerd Moellmann's avatar
Gerd Moellmann committed
57
  :type '(list symbol symbol symbol symbol)
Gerd Moellmann's avatar
Gerd Moellmann committed
58 59 60 61 62
  :group 'font-selection
  :set #'(lambda (symbol value)
	   (set-default symbol value)
	   (internal-set-font-selection-order value)))

63 64

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


80 81
;; This is defined originally in xfaces.c.
(defcustom face-font-registry-alternatives
82
  (if (eq system-type 'windows-nt)
83
      '(("iso8859-1" "ms-oemlatin")
84
	("gb2312.1980" "gb2312" "gbk" "gb18030")
85 86 87
	("jisx0208.1990" "jisx0208.1983" "jisx0208.1978")
	("ksc5601.1989" "ksx1001.1992" "ksc5601.1987")
	("muletibetan-2" "muletibetan-0"))
88
    '(("gb2312.1980" "gb2312.80&gb8565.88" "gbk" "gb18030")
89 90 91
      ("jisx0208.1990" "jisx0208.1983" "jisx0208.1978")
      ("ksc5601.1989" "ksx1001.1992" "ksc5601.1987")
      ("muletibetan-2" "muletibetan-0")))
92
  "*Alist of alternative font registry names.
Pavel Janík's avatar
Pavel Janík committed
93
Each element has the form (REGISTRY ALTERNATIVE1 ALTERNATIVE2 ...).
94 95 96
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."
Andreas Schwab's avatar
Andreas Schwab committed
97
  :tag "Alternative font registries to try"
98
  :type '(repeat (repeat string))
99
  :version "21.1"
100 101 102 103 104 105
  :group 'font-selection
  :set #'(lambda (symbol value)
	   (set-default symbol value)
	   (internal-set-alternative-font-registry-alist value)))


106
(defcustom font-weight-table
107 108
  (if (eq system-type 'windows-nt)
      '((thin . 100)
109
        (ultralight . 199) (ultra-light . 200) (extra-light . 201)
110
        (light . 300)
111
        (semilight . 330) (semi-light . 331)
112
        (book . 350)
113
        (normal . 400) (regular . 401)
114
        (medium . 500)
115
        (semibold . 599) (semi-bold . 600) (demibold . 601) (demi . 602)
116
        (bold . 700)
117 118 119
        (extrabold . 799) (extra-bold . 800)
        (ultrabold . 801) (ultra-bold . 802)
        (black . 900) (heavy . 901))
120
    '((thin . 0)
121
      (ultralight . 39) (ultra-light . 40) (extra-light . 41)
122
      (light . 50)
123
      (semilight . 65) (semi-light . 66)
124
      (book . 75)
125 126
      (medium . 99) (regular . 100) (normal . 101)
      (semibold . 179) (semi-bold . 180) (demibold . 181) (demi . 182)
127
      (bold . 200)
128 129 130
      (extrabold . 204) (extra-bold . 205)
      (ultrabold . 206) (ultra-bold . 207)
      (black . 210) (heavy . 211)))
131 132 133 134 135 136 137 138 139 140 141 142 143
  "*Alist of font weight symbols vs the corresponding numeric values."
  :tag "Font weight table"
  :version "23.1"
  :group 'font-selection
  :type '(repeat (cons symbol integer))
  :set #'(lambda (symbol value)
	   (set-default symbol value)
	   (if (fboundp 'internal-set-font-style-table)
	       (internal-set-font-style-table :weight value))))

(defcustom font-slant-table
  '((ro . 0)
    (ri . 10)
144
    (r . 98) (roman . 99) (normal . 100)
145 146
    (i . 199) (italic . 200) (ot . 201)
    (o . 210) (oblique . 211))
147 148 149 150 151 152 153 154 155 156 157
  "*Alist of font slant symbols vs the corresponding numeric values."
  :tag "Font slant table"
  :version "23.1"
  :group 'font-selection
  :type '(repeat (cons symbol integer))
  :set #'(lambda (symbol value)
	   (set-default symbol value)
	   (if (fboundp 'internal-set-font-style-table)
	       (internal-set-font-style-table :slant value))))

(defcustom font-swidth-table
158 159 160 161 162 163
  '((ultracondensed . 50) (ultra-condensed . 51)
    (extracondensed . 63) (extra-condensed . 64)
    (narrow . 74) (compressed . 75) (condensed . 76)
    (semi-condensed . 87) (semicondensed . 88)
    (medium . 99) (normal . 100) (regular . 101)
    (semiexpanded . 112) (semi-expanded . 113) (demiexpanded . 114)
164
    (expanded . 125)
165 166
    (extraexpanded . 150) (extra-expanded . 151)
    (ultraexpanded . 199) (ultra-expanded . 200) (wide . 201))
167 168 169 170 171 172 173 174 175
  "*Alist of font swidth symbols vs the corresponding numeric values."
  :tag "Font swidth table"
  :version "23.1"
  :group 'font-selection
  :type '(repeat (cons symbol integer))
  :set #'(lambda (symbol value)
	   (set-default symbol value)
	   (if (fboundp 'internal-set-font-style-table)
	       (internal-set-font-style-table :width value))))
176

Gerd Moellmann's avatar
Gerd Moellmann committed
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; 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.

Juanma Barranquero's avatar
Juanma Barranquero committed
222
If the optional argument FRAME is given as a frame, NEW-FACE is
Gerd Moellmann's avatar
Gerd Moellmann committed
223 224 225 226 227 228 229 230
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
231
to NEW-FACE on frame NEW-FRAME.  In this case, FRAME may not be nil."
Gerd Moellmann's avatar
Gerd Moellmann committed
232 233 234
  (let ((inhibit-quit t))
    (if (null frame)
	(progn
235 236 237 238
	  (when new-frame
	    (error "Copying face %s from all frames to one frame"
		   old-face))
	  (make-empty-face new-face)
Gerd Moellmann's avatar
Gerd Moellmann committed
239 240 241
	  (dolist (frame (frame-list))
	    (copy-face old-face new-face frame))
	  (copy-face old-face new-face t))
242
      (make-empty-face new-face)
Gerd Moellmann's avatar
Gerd Moellmann committed
243 244 245 246 247 248 249 250 251 252 253
      (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
254
;; private to the face implementation.
Gerd Moellmann's avatar
Gerd Moellmann committed
255 256 257 258

(defun internal-find-face (name &optional frame)
  "Retrieve the face named NAME.
Return nil if there is no such face.
259 260
If NAME is already a face, it is simply returned.
The optional argument FRAME is ignored."
Gerd Moellmann's avatar
Gerd Moellmann committed
261
  (facep name))
262
(make-obsolete 'internal-find-face 'facep "21.1")
Gerd Moellmann's avatar
Gerd Moellmann committed
263 264 265 266


(defun internal-get-face (name &optional frame)
  "Retrieve the face named NAME; error if there is none.
267 268
If NAME is already a face, it is simply returned.
The optional argument FRAME is ignored."
269
  (or (facep name)
Gerd Moellmann's avatar
Gerd Moellmann committed
270
      (check-face name)))
271
(make-obsolete 'internal-get-face "see `facep' and `check-face'." "21.1")
Gerd Moellmann's avatar
Gerd Moellmann committed
272 273 274 275 276 277 278

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

(defun facep (face)
Richard M. Stallman's avatar
Richard M. Stallman committed
279 280
  "Return non-nil if FACE is a face name; nil otherwise.
A face name can be a string or a symbol."
Gerd Moellmann's avatar
Gerd Moellmann committed
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
  (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)
Juanma Barranquero's avatar
Juanma Barranquero committed
297
  "Return the internal ID of face with name FACE.
298
If FACE is a face-alias, return the ID of the target face.
Richard M. Stallman's avatar
Richard M. Stallman committed
299 300
The optional argument FRAME is ignored, since the internal face ID
of a face name is the same for all frames."
Gerd Moellmann's avatar
Gerd Moellmann committed
301
  (check-face face)
302 303
  (or (get face 'face)
      (face-id (get face 'face-alias))))
Gerd Moellmann's avatar
Gerd Moellmann committed
304 305 306 307

(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.
308 309
If the optional argument FRAME is given, report on FACE1 and FACE2 in that frame.
If FRAME is t, report on the defaults for FACE1 and FACE2 (for new frames).
Gerd Moellmann's avatar
Gerd Moellmann committed
310 311 312 313 314
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)
315
  "Return non-nil if FACE displays differently from the default face.
Gerd Moellmann's avatar
Gerd Moellmann committed
316 317
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).
318
If FRAME is omitted or nil, use the selected frame."
319
  (let ((attrs
320
	 (delq :inherit (mapcar 'car face-attribute-name-alist)))
321 322 323 324 325 326 327 328 329 330
	(differs nil))
    (while (and attrs (not differs))
      (let* ((attr (pop attrs))
	     (attr-val (face-attribute face attr frame t)))
	(when (and
	       (not (eq attr-val 'unspecified))
	       (display-supports-face-attributes-p (list attr attr-val)
						   frame))
	  (setq differs attr))))
    differs))
Gerd Moellmann's avatar
Gerd Moellmann committed
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 362 363


(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"))
364 365
    (:font (".attributeFont" . "Face.AttributeFont"))
    (:inherit (".attributeInherit" . "Face.AttributeInherit")))
Gerd Moellmann's avatar
Gerd Moellmann committed
366 367 368
  "*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
369
\(RESOURCE . CLASS) with RESOURCE being the resource and CLASS being the
Gerd Moellmann's avatar
Gerd Moellmann committed
370
X resource class for the attribute."
371
  :type '(repeat (cons symbol (repeat (cons string string))))
Gerd Moellmann's avatar
Gerd Moellmann committed
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
  :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."
395
  (when (memq (framep frame) '(x w32 mac))
Gerd Moellmann's avatar
Gerd Moellmann committed
396 397 398 399 400
    (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))))))
Sam Steingold's avatar
Sam Steingold committed
401 402


Gerd Moellmann's avatar
Gerd Moellmann committed
403 404 405 406 407 408 409 410 411 412 413 414 415 416
(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.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

417
(defun face-name (face)
Eric S. Raymond's avatar
Eric S. Raymond committed
418
  "Return the name of face FACE."
Gerd Moellmann's avatar
Gerd Moellmann committed
419 420 421
  (symbol-name (check-face face)))


422 423 424 425 426 427
(defun face-all-attributes (face &optional frame)
  "Return an alist stating the attributes of FACE.
Each element of the result has the form (ATTR-NAME . ATTR-VALUE).
Normally the value describes the default attributes,
but if you specify FRAME, the value describes the attributes
of FACE on FRAME."
428 429 430
  (mapcar (lambda (pair)
	    (let ((attr (car pair)))
	      (cons attr (face-attribute face attr (or frame t)))))
431 432
  	  face-attribute-name-alist))

433
(defun face-attribute (face attribute &optional frame inherit)
Gerd Moellmann's avatar
Gerd Moellmann committed
434 435 436
  "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).
437
If FRAME is omitted or nil, use the selected frame.
Gerd Moellmann's avatar
Gerd Moellmann committed
438

439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
If INHERIT is nil, only attributes directly defined by FACE are considered,
  so the return value may be `unspecified', or a relative value.
If INHERIT is non-nil, FACE's definition of ATTRIBUTE is merged with the
  faces specified by its `:inherit' attribute; however the return value
  may still be `unspecified' or relative.
If INHERIT is a face or a list of faces, then the result is further merged
  with that face (or faces), until it becomes specified and absolute.

To ensure that the return value is always specified and absolute, use a
value of `default' for INHERIT; this will resolve any unspecified or
relative values by merging with the `default' face (which is always
completely specified)."
  (let ((value (internal-get-lisp-face-attribute face attribute frame)))
    (when (and inherit (face-attribute-relative-p attribute value))
      ;; VALUE is relative, so merge with inherited faces
      (let ((inh-from (face-attribute face :inherit frame)))
	(unless (or (null inh-from) (eq inh-from 'unspecified))
456 457 458 459 460
          (condition-case nil
              (setq value
                    (face-attribute-merged-with attribute value inh-from frame))
            ;; The `inherit' attribute may point to non existent faces.
            (error nil)))))
461 462 463
    (when (and inherit
	       (not (eq inherit t))
	       (face-attribute-relative-p attribute value))
464
      ;; We should merge with INHERIT as well
465 466
      (setq value (face-attribute-merged-with attribute value inherit frame)))
    value))
Gerd Moellmann's avatar
Gerd Moellmann committed
467

468 469 470
(defun face-attribute-merged-with (attribute value faces &optional frame)
  "Merges ATTRIBUTE, initially VALUE, with faces from FACES until absolute.
FACES may be either a single face or a list of faces.
471
\[This is an internal function.]"
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
  (cond ((not (face-attribute-relative-p attribute value))
	 value)
	((null faces)
	 value)
	((consp faces)
	 (face-attribute-merged-with
	  attribute
	  (face-attribute-merged-with attribute value (car faces) frame)
	  (cdr faces)
	  frame))
	(t
	 (merge-face-attribute attribute
			       value
			       (face-attribute faces attribute frame t)))))


(defmacro face-attribute-specified-or (value &rest body)
  "Return VALUE, unless it's `unspecified', in which case evaluate BODY and return the result."
  (let ((temp (make-symbol "value")))
    `(let ((,temp ,value))
       (if (not (eq ,temp 'unspecified))
	   ,temp
	 ,@body))))

(defun face-foreground (face &optional frame inherit)
Gerd Moellmann's avatar
Gerd Moellmann committed
497 498 499
  "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).
500
If FRAME is omitted or nil, use the selected frame.
Gerd Moellmann's avatar
Gerd Moellmann committed
501

502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
If INHERIT is nil, only a foreground color directly defined by FACE is
  considered, so the return value may be nil.
If INHERIT is t, and FACE doesn't define a foreground color, then any
  foreground color that FACE inherits through its `:inherit' attribute
  is considered as well; however the return value may still be nil.
If INHERIT is a face or a list of faces, then it is used to try to
  resolve an unspecified foreground color.

To ensure that a valid color is always returned, use a value of
`default' for INHERIT; this will resolve any unspecified values by
merging with the `default' face (which is always completely specified)."
  (face-attribute-specified-or (face-attribute face :foreground frame inherit)
			       nil))

(defun face-background (face &optional frame inherit)
Gerd Moellmann's avatar
Gerd Moellmann committed
517 518 519
  "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).
520
If FRAME is omitted or nil, use the selected frame.
Gerd Moellmann's avatar
Gerd Moellmann committed
521

522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
If INHERIT is nil, only a background color directly defined by FACE is
  considered, so the return value may be nil.
If INHERIT is t, and FACE doesn't define a background color, then any
  background color that FACE inherits through its `:inherit' attribute
  is considered as well; however the return value may still be nil.
If INHERIT is a face or a list of faces, then it is used to try to
  resolve an unspecified background color.

To ensure that a valid color is always returned, use a value of
`default' for INHERIT; this will resolve any unspecified values by
merging with the `default' face (which is always completely specified)."
  (face-attribute-specified-or (face-attribute face :background frame inherit)
			       nil))

(defun face-stipple (face &optional frame inherit)
Gerd Moellmann's avatar
Gerd Moellmann committed
537 538 539
 "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).
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
If FRAME is omitted or nil, use the selected frame.

If INHERIT is nil, only a stipple directly defined by FACE is
  considered, so the return value may be nil.
If INHERIT is t, and FACE doesn't define a stipple, then any stipple
  that FACE inherits through its `:inherit' attribute is considered as
  well; however the return value may still be nil.
If INHERIT is a face or a list of faces, then it is used to try to
  resolve an unspecified stipple.

To ensure that a valid stipple or nil is always returned, use a value of
`default' for INHERIT; this will resolve any unspecified values by merging
with the `default' face (which is always completely specified)."
  (face-attribute-specified-or (face-attribute face :stipple frame inherit)
			       nil))
Gerd Moellmann's avatar
Gerd Moellmann committed
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582


(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)))
583
    (memq bold '(semi-bold bold extra-bold ultra-bold))))
Gerd Moellmann's avatar
Gerd Moellmann committed
584 585 586 587 588 589 590 591 592


(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)))
593
    (memq italic '(italic oblique))))
Sam Steingold's avatar
Sam Steingold committed
594

Gerd Moellmann's avatar
Gerd Moellmann committed
595 596 597 598 599 600 601

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

(defun face-documentation (face)
602 603 604 605 606 607 608 609 610 611 612
  "Get the documentation string for FACE.
If FACE is a face-alias, get the documentation for the target face."
  (let ((alias (get face 'face-alias))
        doc)
    (if alias
        (progn
          (setq doc (get alias 'face-documentation))
          (format "%s is an alias for the face `%s'.%s" face alias
                  (if doc (format "\n%s" doc)
                    "")))
      (get face 'face-documentation))))
Gerd Moellmann's avatar
Gerd Moellmann committed
613 614 615 616


(defun set-face-documentation (face string)
  "Set the documentation string for FACE to STRING."
617
  ;; Perhaps the text should go in DOC.
618
  (put face 'face-documentation (purecopy string)))
Gerd Moellmann's avatar
Gerd Moellmann committed
619 620 621 622 623 624 625 626 627 628 629 630


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


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


631 632 633
(defvar inhibit-face-set-after-frame-default nil
  "If non-nil, that tells `face-set-after-frame-default' to do nothing.")

Gerd Moellmann's avatar
Gerd Moellmann committed
634 635 636 637 638 639 640 641
(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
642
face attribute name. All attributes can be set to `unspecified';
Gerd Moellmann's avatar
Gerd Moellmann committed
643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
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
662 663 664 665
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
666 667 668 669 670 671 672 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

`: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
715 716 717
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
718 719 720 721 722 723 724 725 726 727 728
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
729
inverse video.  VALUE must be one of t or nil.
Gerd Moellmann's avatar
Gerd Moellmann committed
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750

`: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
751 752 753 754 755 756 757
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."
758 759
  (let ((where (if (null frame) 0 frame)))
    (setq args (purecopy args))
760 761
    ;; If we set the new-frame defaults, this face is modified outside Custom.
    (if (memq where '(0 t))
762
	(put (or (get face 'face-alias) face) 'face-modified t))
763
    (while args
764 765 766 767 768 769
      ;; Don't recursively set the attributes from the frame's font param
      ;; when we update the frame's font param fro the attributes.
      (let ((inhibit-face-set-after-frame-default t))
	(internal-set-lisp-face-attribute face (car args)
					  (purecopy (cadr args))
					  where))
770
      (setq args (cdr (cdr args))))))
Gerd Moellmann's avatar
Gerd Moellmann committed
771 772


773
(defun make-face-bold (face &optional frame noerror)
Gerd Moellmann's avatar
Gerd Moellmann committed
774 775
  "Make the font of FACE be bold, if possible.
FRAME nil or not specified means change face on all frames.
776
Argument NOERROR is ignored and retained for compatibility.
Gerd Moellmann's avatar
Gerd Moellmann committed
777
Use `set-face-attribute' for finer control of the font weight."
Eli Zaretskii's avatar
Eli Zaretskii committed
778
  (interactive (list (read-face-name "Make which face bold")))
Gerd Moellmann's avatar
Gerd Moellmann committed
779 780 781
  (set-face-attribute face frame :weight 'bold))


782
(defun make-face-unbold (face &optional frame noerror)
Gerd Moellmann's avatar
Gerd Moellmann committed
783
  "Make the font of FACE be non-bold, if possible.
784 785
FRAME nil or not specified means change face on all frames.
Argument NOERROR is ignored and retained for compatibility."
Eli Zaretskii's avatar
Eli Zaretskii committed
786
  (interactive (list (read-face-name "Make which face non-bold")))
Gerd Moellmann's avatar
Gerd Moellmann committed
787 788
  (set-face-attribute face frame :weight 'normal))

Sam Steingold's avatar
Sam Steingold committed
789

790
(defun make-face-italic (face &optional frame noerror)
Gerd Moellmann's avatar
Gerd Moellmann committed
791 792
  "Make the font of FACE be italic, if possible.
FRAME nil or not specified means change face on all frames.
793
Argument NOERROR is ignored and retained for compatibility.
Gerd Moellmann's avatar
Gerd Moellmann committed
794
Use `set-face-attribute' for finer control of the font slant."
Eli Zaretskii's avatar
Eli Zaretskii committed
795
  (interactive (list (read-face-name "Make which face italic")))
Gerd Moellmann's avatar
Gerd Moellmann committed
796 797 798
  (set-face-attribute face frame :slant 'italic))


799
(defun make-face-unitalic (face &optional frame noerror)
Gerd Moellmann's avatar
Gerd Moellmann committed
800
  "Make the font of FACE be non-italic, if possible.
Dave Love's avatar
Dave Love committed
801 802
FRAME nil or not specified means change face on all frames.
Argument NOERROR is ignored and retained for compatibility."
Eli Zaretskii's avatar
Eli Zaretskii committed
803
  (interactive (list (read-face-name "Make which face non-italic")))
Gerd Moellmann's avatar
Gerd Moellmann committed
804 805
  (set-face-attribute face frame :slant 'normal))

Sam Steingold's avatar
Sam Steingold committed
806

807
(defun make-face-bold-italic (face &optional frame noerror)
Gerd Moellmann's avatar
Gerd Moellmann committed
808 809
  "Make the font of FACE be bold and italic, if possible.
FRAME nil or not specified means change face on all frames.
810
Argument NOERROR is ignored and retained for compatibility.
Gerd Moellmann's avatar
Gerd Moellmann committed
811
Use `set-face-attribute' for finer control of font weight and slant."
Eli Zaretskii's avatar
Eli Zaretskii committed
812
  (interactive (list (read-face-name "Make which face bold-italic")))
Gerd Moellmann's avatar
Gerd Moellmann committed
813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831
  (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.
832 833 834
COLOR can be a system-defined color name (see `list-colors-display')
or a hex spec of the form #RRGGBB.
When called interactively, prompts for the face and color."
Gerd Moellmann's avatar
Gerd Moellmann committed
835
  (interactive (read-face-and-attribute :background))
836
  (set-face-attribute face frame :background (or color 'unspecified)))
Gerd Moellmann's avatar
Gerd Moellmann committed
837 838 839 840 841


(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.
842 843 844
COLOR can be a system-defined color name (see `list-colors-display')
or a hex spec of the form #RRGGBB.
When called interactively, prompts for the face and color."
Gerd Moellmann's avatar
Gerd Moellmann committed
845
  (interactive (read-face-and-attribute :foreground))
846
  (set-face-attribute face frame :foreground (or color 'unspecified)))
Gerd Moellmann's avatar
Gerd Moellmann committed
847 848 849 850 851


(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
852
STIPPLE should be a string, the name of a file of pixmap data.
Gerd Moellmann's avatar
Gerd Moellmann committed
853 854 855 856 857 858
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))
859
  (set-face-attribute face frame :stipple (or stipple 'unspecified)))
Gerd Moellmann's avatar
Gerd Moellmann committed
860 861


862
(defun set-face-underline-p (face underline &optional frame)
Gerd Moellmann's avatar
Gerd Moellmann committed
863 864 865 866 867 868 869 870 871 872
  "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))))
873
  (set-face-attribute face frame :underline underline))
Gerd Moellmann's avatar
Gerd Moellmann committed
874

875 876 877
(define-obsolete-function-alias 'set-face-underline
                                'set-face-underline-p "22.1")

Gerd Moellmann's avatar
Gerd Moellmann committed
878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917

(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.
918
If FRAME is omitted or nil, it means change face on all frames.
Gerd Moellmann's avatar
Gerd Moellmann committed
919 920 921
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."
Eli Zaretskii's avatar
Eli Zaretskii committed
922
  (interactive (list (read-face-name "Invert face")))
Gerd Moellmann's avatar
Gerd Moellmann committed
923 924
  (let ((fg (face-attribute face :foreground frame))
	(bg (face-attribute face :background frame)))
925
    (if (not (and (eq fg 'unspecified) (eq bg 'unspecified)))
Gerd Moellmann's avatar
Gerd Moellmann committed
926 927 928 929 930 931 932 933 934 935 936 937 938
	(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.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

939 940
(defun read-face-name (prompt &optional string-describing-default multiple)
  "Read a face, defaulting to the face or faces on the char after point.
941
If it has the property `read-face-name', that overrides the `face' property.
942 943 944 945
PROMPT should be a string that describes what the caller will do with the face;
it should not end in a space.
STRING-DESCRIBING-DEFAULT should describe what default the caller will use if
the user just types RET; you can omit it.
946 947 948 949
If MULTIPLE is non-nil, return a list of faces (possibly only one).
Otherwise, return a single face."
  (let ((faceprop (or (get-char-property (point) 'read-face-name)
		      (get-char-property (point) 'face)))
950 951
        (aliasfaces nil)
        (nonaliasfaces nil)
952
	faces)
953 954 955 956
    ;; Try to get a face name from the buffer.
    (if (memq (intern-soft (thing-at-point 'symbol)) (face-list))
	(setq faces (list (intern-soft (thing-at-point 'symbol)))))
    ;; Add the named faces that the `face' property uses.
957 958 959 960
    (if (and (listp faceprop)
	     ;; Don't treat an attribute spec as a list of faces.
	     (not (keywordp (car faceprop)))
	     (not (memq (car faceprop) '(foreground-color background-color))))
961 962 963 964
	(dolist (f faceprop)
	  (if (symbolp f)
	      (push f faces)))
      (if (symbolp faceprop)
965
	  (push faceprop faces)))
966
    (delete-dups faces)
967

968 969 970 971 972 973 974
    ;; Build up the completion tables.
    (mapatoms (lambda (s)
                (if (custom-facep s)
                    (if (get s 'face-alias)
                        (push (symbol-name s) aliasfaces)
                      (push (symbol-name s) nonaliasfaces)))))

975 976 977 978 979
    ;; If we only want one, and the default is more than one,
    ;; discard the unwanted ones now.
    (unless multiple
      (if faces
	  (setq faces (list (car faces)))))
980
    (require 'crm)
981 982
    (let* ((input
	    ;; Read the input.
983
	    (completing-read-multiple
984 985
	     (if (or faces string-describing-default)
		 (format "%s (default %s): " prompt
986
			 (if faces (mapconcat 'symbol-name faces ",")
987
			   string-describing-default))
988
	       (format "%s: " prompt))
989
	     (completion-table-in-turn nonaliasfaces aliasfaces)
990 991
	     nil t nil nil
	     (if faces (mapconcat 'symbol-name faces ","))))
992 993
	   ;; Canonicalize the output.
	   (output
994 995 996 997 998 999 1000
	    (cond ((or (equal input "") (equal input '("")))
		   faces)
		  ((stringp input)
		   (mapcar 'intern (split-string input ", *" t)))
		  ((listp input)
		   (mapcar 'intern input))
		  (input))))
1001 1002 1003 1004 1005 1006
      ;; Return either a list of faces or just one face.
      (if multiple
	  output
	(car output)))))


Gerd Moellmann's avatar
Gerd Moellmann committed
1007 1008 1009 1010 1011 1012 1013
(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."
Sam Steingold's avatar
Sam Steingold committed
1014 1015 1016
  (let ((valid
         (case attribute
           (:family
1017
            (if (window-system frame)
Sam Steingold's avatar
Sam Steingold committed
1018 1019 1020 1021 1022 1023 1024 1025
                (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)
1026
            (if (window-system frame)
Sam Steingold's avatar
Sam Steingold committed
1027 1028 1029
                (nconc (mapcar #'(lambda (x) (cons (symbol-name x) x))
                               (internal-lisp-face-attribute-values attribute))
                       (mapcar #'(lambda (c) (cons c c))
1030
                               (defined-colors frame)))
Sam Steingold's avatar
Sam Steingold committed
1031 1032 1033 1034 1035 1036 1037 1038
	      (mapcar #'(lambda (x) (cons (symbol-name x) x))
		      (internal-lisp-face-attribute-values attribute))))
           ((:foreground :background)
            (mapcar #'(lambda (c) (cons c c))
                    (defined-colors frame)))
           ((:height)
            'integerp)
           (:stipple
1039
            (and (memq (window-system frame) '(x w32 mac))
Sam Steingold's avatar
Sam Steingold committed
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052
                 (mapcar #'list
                         (apply #'nconc
                                (mapcar (lambda (dir)
                                          (and (file-readable-p dir)
                                               (file-directory-p dir)
                                               (directory-files dir)))
                                        x-bitmap-file-path)))))
           (:inherit
            (cons '("none" . nil)
                  (mapcar #'(lambda (c) (cons (symbol-name c) c))
                          (face-list))))
           (t
            (error "Internal error")))))
Miles Bader's avatar
Miles Bader committed
1053
    (if (and (listp valid) (not (memq attribute '(:inherit))))
1054 1055
	(nconc (list (cons "unspecified" 'unspecified)) valid)
      valid)))
Gerd Moellmann's avatar
Gerd Moellmann committed
1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070


(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
1071 1072
    (:stipple . "background stipple")
    (:inherit . "inheritance"))
Gerd Moellmann's avatar
Gerd Moellmann committed
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085
  "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
1086 1087 1088 1089
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
1090

Miles Bader's avatar
Miles Bader committed
1091
Entering nothing accepts the default string DEFAULT.
Gerd Moellmann's avatar
Gerd Moellmann committed
1092
Value is the new attribute value."
Miles Bader's avatar
Miles Bader committed
1093 1094 1095
  ;; 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
1096 1097 1098
  (let* ((completion-ignore-case t)
	 (value (completing-read
		 (if default
Miles Bader's avatar
Miles Bader committed
1099 1100 1101
		     (format "%s for face `%s' (default %s): "
			     name face default)
		   (format "%s for face `%s': " name face))
1102
		 completion-alist nil nil nil nil default)))
1103
    (if (equal value "") default value)))
Gerd Moellmann's avatar
Gerd Moellmann committed
1104 1105 1106 1107 1108 1109 1110


(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."
1111 1112
  (let ((new-value
	 (face-read-string face
Miles Bader's avatar
Miles Bader committed
1113
			   (format "%s" default)
1114 1115
			   name
			   (list (cons "unspecified" 'unspecified)))))
Miles Bader's avatar
Miles Bader committed
1116 1117 1118 1119 1120
    (cond ((equal new-value "unspecified")
	   'unspecified)
	  ((member new-value '("unspecified-fg" "unspecified-bg"))
	   new-value)
	  (t
1121
	   (string-to-number new-value)))))
Gerd Moellmann's avatar
Gerd Moellmann committed
1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141


(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
1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155
	   (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.
1156
	       (when (and (memq attribute '(:foreground :background))
1157
			  (not (memq (window-system frame) '(x w32 mac)))
1158 1159 1160
			  (not (member new-value
				       '("unspecified"
					 "unspecified-fg" "unspecified-bg"))))
Miles Bader's avatar
WS  
Miles Bader committed
1161
		 (setq new-value (car (tty-color-desc new-value frame))))
1162 1163
	       (when (assoc new-value valid)
		 (setq new-value (cdr (assoc new-value valid)))))))
Gerd Moellmann's avatar
Gerd Moellmann committed
1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179
	  ((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.
1180
If optional argument FRAME is nil or omitted, use the selected frame."
Gerd Moellmann's avatar
Gerd Moellmann committed
1181
  (let ((completion-ignore-case t))
1182
    (completing-read (format "Set font attributes of face `%s' from font: " face)