faces.el 97.7 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-1996, 1998-2012  Free Software Foundation, Inc.
Eric S. Raymond's avatar
Eric S. Raymond committed
4

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

Eric S. Raymond's avatar
Eric S. Raymond committed
9 10
;; This file is part of GNU Emacs.

11
;; GNU Emacs is free software: you can redistribute it and/or modify
Eric S. Raymond's avatar
Eric S. Raymond committed
12
;; it under the terms of the GNU General Public License as published by
13 14
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
Eric S. Raymond's avatar
Eric S. Raymond committed
15 16 17 18 19 20 21

;; 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
22
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
Eric S. Raymond's avatar
Eric S. Raymond committed
23 24 25 26 27

;;; Commentary:

;;; Code:

28 29 30 31 32 33 34 35 36 37
(defcustom term-file-prefix (purecopy "term/")
  "If non-nil, Emacs startup performs terminal-specific initialization.
It does this by: (load (concat term-file-prefix (getenv \"TERM\")))

You may set this variable to nil in your init file if you do not wish
the terminal-initialization file to be loaded."
  :type '(choice (const :tag "No terminal-specific initialization" nil)
		 (string :tag "Name of directory with term files"))
  :group 'terminals)

Glenn Morris's avatar
Glenn Morris committed
38
(declare-function xw-defined-colors "term/common-win" (&optional frame))
Gerd Moellmann's avatar
Gerd Moellmann committed
39

40
(defvar help-xref-stack-item)
41 42 43 44 45 46

(defvar face-name-history nil
  "History list for some commands that read face names.
Maximum length of the history list is determined by the value
of `history-length', which see.")

Gerd Moellmann's avatar
Gerd Moellmann committed
47 48 49 50 51 52 53 54 55 56 57 58

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Font selection.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

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


(defcustom face-font-selection-order
  '(:width :height :weight :slant)
Lute Kamstra's avatar
Lute Kamstra committed
59
  "A list specifying how face font selection chooses fonts.
Gerd Moellmann's avatar
Gerd Moellmann committed
60 61
Each of the four symbols `:width', `:height', `:weight', and `:slant'
must appear once in the list, and the list must not contain any other
62
elements.  Font selection first tries to find a best matching font
63
for those face attributes that appear before in the list.  For
Gerd Moellmann's avatar
Gerd Moellmann committed
64 65 66
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
67
  :tag "Font selection order"
Gerd Moellmann's avatar
Gerd Moellmann committed
68
  :type '(list symbol symbol symbol symbol)
Gerd Moellmann's avatar
Gerd Moellmann committed
69 70 71 72 73
  :group 'font-selection
  :set #'(lambda (symbol value)
	   (set-default symbol value)
	   (internal-set-font-selection-order value)))

74

75
;; In the absence of Fontconfig support, Monospace and Sans Serif are
76 77
;; unavailable, and we fall back on the courier and helv families,
;; which are generally available.
Gerd Moellmann's avatar
Gerd Moellmann committed
78
(defcustom face-font-family-alternatives
79
  (mapcar (lambda (arg) (mapcar 'purecopy arg))
80
  '(("Monospace" "courier" "fixed")
81
    ("courier" "CMU Typewriter Text" "fixed")
82
    ("Sans Serif" "helv" "helvetica" "arial" "fixed")
83
    ("helv" "helvetica" "arial" "fixed")))
Lute Kamstra's avatar
Lute Kamstra committed
84
  "Alist of alternative font family names.
Pavel Janík's avatar
Pavel Janík committed
85
Each element has the form (FAMILY ALTERNATIVE1 ALTERNATIVE2 ...).
Gerd Moellmann's avatar
Gerd Moellmann committed
86 87
If fonts of family FAMILY can't be loaded, try ALTERNATIVE1, then
ALTERNATIVE2 etc."
Andreas Schwab's avatar
Andreas Schwab committed
88
  :tag "Alternative font families to try"
Gerd Moellmann's avatar
Gerd Moellmann committed
89
  :type '(repeat (repeat string))
Gerd Moellmann's avatar
Gerd Moellmann committed
90 91 92 93 94 95
  :group 'font-selection
  :set #'(lambda (symbol value)
	   (set-default symbol value)
	   (internal-set-alternative-font-family-alist value)))


96 97
;; This is defined originally in xfaces.c.
(defcustom face-font-registry-alternatives
98
  (mapcar (lambda (arg) (mapcar 'purecopy arg))
Daniel Colascione's avatar
Daniel Colascione committed
99
  (if (featurep 'w32)
100
      '(("iso8859-1" "ms-oemlatin")
101
	("gb2312.1980" "gb2312" "gbk" "gb18030")
102 103 104
	("jisx0208.1990" "jisx0208.1983" "jisx0208.1978")
	("ksc5601.1989" "ksx1001.1992" "ksc5601.1987")
	("muletibetan-2" "muletibetan-0"))
105
    '(("gb2312.1980" "gb2312.80&gb8565.88" "gbk" "gb18030")
106 107
      ("jisx0208.1990" "jisx0208.1983" "jisx0208.1978")
      ("ksc5601.1989" "ksx1001.1992" "ksc5601.1987")
108
      ("muletibetan-2" "muletibetan-0"))))
Lute Kamstra's avatar
Lute Kamstra committed
109
  "Alist of alternative font registry names.
Pavel Janík's avatar
Pavel Janík committed
110
Each element has the form (REGISTRY ALTERNATIVE1 ALTERNATIVE2 ...).
111 112 113
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
114
  :tag "Alternative font registries to try"
115
  :type '(repeat (repeat string))
116
  :version "21.1"
117 118 119 120 121
  :group 'font-selection
  :set #'(lambda (symbol value)
	   (set-default symbol value)
	   (internal-set-alternative-font-registry-alist value)))

122

Gerd Moellmann's avatar
Gerd Moellmann committed
123 124 125 126 127 128
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Creation, copying.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


(defun face-list ()
129
  "Return a list of all defined faces."
Gerd Moellmann's avatar
Gerd Moellmann committed
130 131 132 133
  (mapcar #'car face-new-frame-defaults))

(defun make-face (face &optional no-init-from-resources)
  "Define a new face with name FACE, a symbol.
134 135 136 137 138
Do not call this directly from Lisp code; use `defface' instead.

If NO-INIT-FROM-RESOURCES is non-nil, don't initialize face
attributes from X resources.  If FACE is already known as a face,
leave it unmodified.  Return FACE."
139 140
  (interactive (list (read-from-minibuffer
		      "Make face: " nil nil t 'face-name-history)))
Gerd Moellmann's avatar
Gerd Moellmann committed
141 142 143 144 145 146 147 148 149 150 151 152 153 154
  (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.
155
Do not call this directly from Lisp code; use `defface' instead."
156 157
  (interactive (list (read-from-minibuffer
		      "Make empty face: " nil nil t 'face-name-history)))
Gerd Moellmann's avatar
Gerd Moellmann committed
158 159 160
  (make-face face 'no-init-from-resources))

(defun copy-face (old-face new-face &optional frame new-frame)
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
  "Define a face named NEW-FACE, which is a copy of OLD-FACE.
This function does not copy face customization data, so NEW-FACE
will not be made customizable.  Most Lisp code should not call
this function; use `defface' with :inherit instead.

If NEW-FACE already exists as a face, modify it to be like
OLD-FACE.  If NEW-FACE doesn't already exist, create it.

If the optional argument FRAME is a frame, change NEW-FACE on
FRAME only.  If FRAME is t, copy the frame-independent default
specification for OLD-FACE to NEW-FACE.  If FRAME is nil, copy
the defaults as well as the faces on 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.  In this case, FRAME must not be nil."
Gerd Moellmann's avatar
Gerd Moellmann committed
177 178 179
  (let ((inhibit-quit t))
    (if (null frame)
	(progn
180 181 182 183
	  (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
184 185 186
	  (dolist (frame (frame-list))
	    (copy-face old-face new-face frame))
	  (copy-face old-face new-face t))
187
      (make-empty-face new-face)
Gerd Moellmann's avatar
Gerd Moellmann committed
188 189 190 191 192 193 194 195 196
      (internal-copy-lisp-face old-face new-face frame new-frame))
    new-face))

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

(defun facep (face)
Richard M. Stallman's avatar
Richard M. Stallman committed
197 198
  "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
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
  (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.

214
(defun face-id (face &optional _frame)
Juanma Barranquero's avatar
Juanma Barranquero committed
215
  "Return the internal ID of face with name FACE.
216
If FACE is a face-alias, return the ID of the target face.
Richard M. Stallman's avatar
Richard M. Stallman committed
217 218
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
219
  (check-face face)
220 221
  (or (get face 'face)
      (face-id (get face 'face-alias))))
Gerd Moellmann's avatar
Gerd Moellmann committed
222 223 224 225

(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.
226 227
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
228 229 230 231 232
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)
233
  "Return non-nil if FACE displays differently from the default face.
Gerd Moellmann's avatar
Gerd Moellmann committed
234 235
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).
236
If FRAME is omitted or nil, use the selected frame."
237
  (let ((attrs
238
	 (delq :inherit (mapcar 'car face-attribute-name-alist)))
239 240 241 242 243 244 245 246 247 248
	(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
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264


(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
265 266 267
  (mapcar
   (lambda (arg)
     ;; FIXME; can we purecopy some of the conses too?
268 269
     (cons (car arg)
	   (cons (purecopy (car (cdr arg))) (purecopy (cdr (cdr arg))))))
Gerd Moellmann's avatar
Gerd Moellmann committed
270
  '((:family (".attributeFamily" . "Face.AttributeFamily"))
271
    (:foundry (".attributeFoundry" . "Face.AttributeFoundry"))
Gerd Moellmann's avatar
Gerd Moellmann committed
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
    (: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"))
288
    (:font (".attributeFont" . "Face.AttributeFont"))
289
    (:inherit (".attributeInherit" . "Face.AttributeInherit"))))
Lute Kamstra's avatar
Lute Kamstra committed
290
  "List of X resources and classes for face attributes.
Gerd Moellmann's avatar
Gerd Moellmann committed
291 292
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
293
\(RESOURCE . CLASS) with RESOURCE being the resource and CLASS being the
Gerd Moellmann's avatar
Gerd Moellmann committed
294
X resource class for the attribute."
295
  :type '(repeat (cons symbol (repeat (cons string string))))
Gerd Moellmann's avatar
Gerd Moellmann committed
296 297 298
  :group 'faces)


299 300 301 302 303 304
(declare-function internal-face-x-get-resource "xfaces.c"
		  (resource class frame))

(declare-function internal-set-lisp-face-attribute-from-resource "xfaces.c"
		  (face attr value &optional frame))

Gerd Moellmann's avatar
Gerd Moellmann committed
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
(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."
325
  (when (memq (framep frame) '(x w32))
Gerd Moellmann's avatar
Gerd Moellmann committed
326 327 328 329 330
    (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
331 332


Gerd Moellmann's avatar
Gerd Moellmann committed
333 334 335 336 337 338 339 340 341 342 343 344 345 346
(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.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

347
(defun face-name (face)
Eric S. Raymond's avatar
Eric S. Raymond committed
348
  "Return the name of face FACE."
Gerd Moellmann's avatar
Gerd Moellmann committed
349 350 351
  (symbol-name (check-face face)))


352 353 354
(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).
355
If FRAME is omitted or nil the value describes the default attributes,
356 357
but if you specify FRAME, the value describes the attributes
of FACE on FRAME."
358 359 360
  (mapcar (lambda (pair)
	    (let ((attr (car pair)))
	      (cons attr (face-attribute face attr (or frame t)))))
361 362
  	  face-attribute-name-alist))

363
(defun face-attribute (face attribute &optional frame inherit)
Gerd Moellmann's avatar
Gerd Moellmann committed
364 365 366
  "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).
367
If FRAME is omitted or nil, use the selected frame.
Gerd Moellmann's avatar
Gerd Moellmann committed
368

369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
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))
386 387 388 389 390
          (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)))))
391 392 393
    (when (and inherit
	       (not (eq inherit t))
	       (face-attribute-relative-p attribute value))
394
      ;; We should merge with INHERIT as well
395 396
      (setq value (face-attribute-merged-with attribute value inherit frame)))
    value))
Gerd Moellmann's avatar
Gerd Moellmann committed
397

398 399 400
(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.
401
\[This is an internal function.]"
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
  (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
427 428 429
  "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).
430
If FRAME is omitted or nil, use the selected frame.
Gerd Moellmann's avatar
Gerd Moellmann committed
431

432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
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
447 448 449
  "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).
450
If FRAME is omitted or nil, use the selected frame.
Gerd Moellmann's avatar
Gerd Moellmann committed
451

452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
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
467 468 469
 "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).
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
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
485 486 487 488 489


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


490 491 492 493 494
;; FIXME all of these -p functions ignore inheritance (cf face-stipple).
;; Ie, a face that inherits from an underlined face but does not
;; specify :underline will return nil.
;; So these functions don't actually tell you anything about how the
;; face will _appear_.  So not very useful IMO.
Gerd Moellmann's avatar
Gerd Moellmann committed
495
(defun face-underline-p (face &optional frame)
496
 "Return non-nil if FACE specifies a non-nil underlining.
Gerd Moellmann's avatar
Gerd Moellmann committed
497 498 499
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."
500
 (face-attribute-specified-or (face-attribute face :underline frame) nil))
Gerd Moellmann's avatar
Gerd Moellmann committed
501 502 503


(defun face-inverse-video-p (face &optional frame)
504
 "Return non-nil if FACE specifies a non-nil inverse-video.
Gerd Moellmann's avatar
Gerd Moellmann committed
505 506 507 508 509 510 511 512 513 514 515 516 517
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)))
518
    (memq bold '(semi-bold bold extra-bold ultra-bold))))
Gerd Moellmann's avatar
Gerd Moellmann committed
519 520 521 522 523 524 525 526 527


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

Gerd Moellmann's avatar
Gerd Moellmann committed
530 531 532 533 534 535 536

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

(defun face-documentation (face)
537 538 539 540 541 542 543
  "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))
544
	  (format "%s is an alias for the face `%s'.%s" face alias
545 546 547
                  (if doc (format "\n%s" doc)
                    "")))
      (get face 'face-documentation))))
Gerd Moellmann's avatar
Gerd Moellmann committed
548 549 550 551


(defun set-face-documentation (face string)
  "Set the documentation string for FACE to STRING."
552
  ;; Perhaps the text should go in DOC.
553
  (put face 'face-documentation (purecopy string)))
Gerd Moellmann's avatar
Gerd Moellmann committed
554 555 556 557 558 559 560 561 562 563 564 565 566 567


(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.
568 569
This function overrides the face attributes specified by FACE's
face spec.  It is mostly intended for internal use only.
Gerd Moellmann's avatar
Gerd Moellmann committed
570

571 572 573
If FRAME is nil, set the attributes for all existing frames, as
well as the default for new frames.  If FRAME is t, change the
default for new frames only.
Gerd Moellmann's avatar
Gerd Moellmann committed
574

575 576 577
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.
Gerd Moellmann's avatar
Gerd Moellmann committed
578 579 580 581 582

The following attributes are recognized:

`:family'

583 584
VALUE must be a string specifying the font family
\(e.g. \"Monospace\") or a fontset.
Gerd Moellmann's avatar
Gerd Moellmann committed
585

586 587 588 589 590 591
`:foundry'

VALUE must be a string specifying the font foundry,
e.g. ``adobe''.  If a font foundry is specified, wild-cards `*'
and `?' are allowed.

Gerd Moellmann's avatar
Gerd Moellmann committed
592 593 594 595 596 597 598 599 600
`: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'

601 602 603
VALUE specifies the relative or absolute height of the font.  An
absolute height is an integer, and specifies font height in units
of 1/10 pt.  A relative height is either a floating point number,
604
which specifies a scaling factor for the underlying face height;
605 606 607
or a function that takes a single argument (the underlying face
height) and returns the new height.  Note that for the `default'
face, you must specify an absolute height (since there is nothing
608
for it to be relative to).
Gerd Moellmann's avatar
Gerd Moellmann committed
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627

`: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'

628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
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.

Otherwise, VALUE must be a property list of the form:

`(:color COLOR :style STYLE)'.

COLOR can be a either a color name string or `foreground-color'.
STYLE can be either `line' or `wave'.
If a keyword/value pair is missing from the property list, a
default value will be used for the value.
The default value of COLOR is the foreground color of the face.
The default value of STYLE is `line'.
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 662 663 664 665 666 667 668

`: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
669 670 671
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
672 673 674 675 676 677 678 679 680 681 682
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
683
inverse video.  VALUE must be one of t or nil.
Gerd Moellmann's avatar
Gerd Moellmann committed
684 685 686 687 688 689 690 691 692 693

`: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.

694 695 696
For convenience, attributes `:family', `:foundry', `:width',
`:height', `:weight', and `:slant' may also be set in one step
from an X font name:
Gerd Moellmann's avatar
Gerd Moellmann committed
697 698 699

`:font'

700 701 702 703
Set font-related face attributes from VALUE.  VALUE must be a
valid font name or font object.  Setting this attribute will also
set the `:family', `:foundry', `:width', `:height', `:weight',
and `:slant' attributes.
Miles Bader's avatar
Miles Bader committed
704 705 706

`:inherit'

707 708 709 710 711 712 713 714 715 716 717 718 719
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.

For backward compatibility, the keywords `:bold' and `:italic'
can be used to specify weight and slant respectively.  This usage
is considered obsolete.  For these two keywords, the VALUE must
be either t or nil.  A value of t for `:bold' is equivalent to
setting `:weight' to `bold', and a value of t for `:italic' is
equivalent to setting `:slant' to `italic'.  But if `:weight' is
specified in the face spec, `:bold' is ignored, and if `:slant'
is specified, `:italic' is ignored."
720 721 722 723
  (setq args (purecopy args))
  (let ((where (if (null frame) 0 frame))
	(spec args)
	family foundry)
724 725
    ;; If we set the new-frame defaults, this face is modified outside Custom.
    (if (memq where '(0 t))
726
	(put (or (get face 'face-alias) face) 'face-modified t))
727 728 729 730 731 732 733 734 735 736 737 738 739
    ;; If family and/or foundry are specified, set it first.  Certain
    ;; face attributes, e.g. :weight semi-condensed, are not supported
    ;; in every font.  See bug#1127.
    (while spec
      (cond ((eq (car spec) :family)
	     (setq family (cadr spec)))
	    ((eq (car spec) :foundry)
	     (setq foundry (cadr spec))))
      (setq spec (cddr spec)))
    (when (or family foundry)
      (when (and (stringp family)
		 (string-match "\\([^-]*\\)-\\([^-]*\\)" family))
	(unless foundry
740 741
	  (setq foundry (match-string 1 family)))
	(setq family (match-string 2 family)))
742
      (when (or (stringp family) (eq family 'unspecified))
743 744
	(internal-set-lisp-face-attribute face :family (purecopy family)
					  where))
745
      (when (or (stringp foundry) (eq foundry 'unspecified))
746 747
	(internal-set-lisp-face-attribute face :foundry (purecopy foundry)
					  where)))
748
    (while args
749
      (unless (memq (car args) '(:family :foundry))
750 751 752
	(internal-set-lisp-face-attribute face (car args)
					  (purecopy (cadr args))
					  where))
753
      (setq args (cddr args)))))
Gerd Moellmann's avatar
Gerd Moellmann committed
754

755
(defun make-face-bold (face &optional frame _noerror)
Gerd Moellmann's avatar
Gerd Moellmann committed
756 757
  "Make the font of FACE be bold, if possible.
FRAME nil or not specified means change face on all frames.
758
Argument NOERROR is ignored and retained for compatibility.
Gerd Moellmann's avatar
Gerd Moellmann committed
759
Use `set-face-attribute' for finer control of the font weight."
Eli Zaretskii's avatar
Eli Zaretskii committed
760
  (interactive (list (read-face-name "Make which face bold")))
Gerd Moellmann's avatar
Gerd Moellmann committed
761 762 763
  (set-face-attribute face frame :weight 'bold))


764
(defun make-face-unbold (face &optional frame _noerror)
Gerd Moellmann's avatar
Gerd Moellmann committed
765
  "Make the font of FACE be non-bold, if possible.
766 767
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
768
  (interactive (list (read-face-name "Make which face non-bold")))
Gerd Moellmann's avatar
Gerd Moellmann committed
769 770
  (set-face-attribute face frame :weight 'normal))

Sam Steingold's avatar
Sam Steingold committed
771

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


781
(defun make-face-unitalic (face &optional frame _noerror)
Gerd Moellmann's avatar
Gerd Moellmann committed
782
  "Make the font of FACE be non-italic, if possible.
Dave Love's avatar
Dave Love committed
783 784
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
785
  (interactive (list (read-face-name "Make which face non-italic")))
Gerd Moellmann's avatar
Gerd Moellmann committed
786 787
  (set-face-attribute face frame :slant 'normal))

Sam Steingold's avatar
Sam Steingold committed
788

789
(defun make-face-bold-italic (face &optional frame _noerror)
Gerd Moellmann's avatar
Gerd Moellmann committed
790 791
  "Make the font of FACE be bold and italic, if possible.
FRAME nil or not specified means change face on all frames.
792
Argument NOERROR is ignored and retained for compatibility.
Gerd Moellmann's avatar
Gerd Moellmann committed
793
Use `set-face-attribute' for finer control of font weight and slant."
Eli Zaretskii's avatar
Eli Zaretskii committed
794
  (interactive (list (read-face-name "Make which face bold-italic")))
Gerd Moellmann's avatar
Gerd Moellmann committed
795 796 797 798 799 800
  (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.
801 802 803
This sets the attributes `:family', `:foundry', `:width',
`:height', `:weight', and `:slant'.  When called interactively,
prompt for the face and font."
Gerd Moellmann's avatar
Gerd Moellmann committed
804 805 806 807 808 809 810 811 812 813 814
  (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.
815 816 817
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
818
  (interactive (read-face-and-attribute :background))
819
  (set-face-attribute face frame :background (or color 'unspecified)))
Gerd Moellmann's avatar
Gerd Moellmann committed
820 821 822 823 824


(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.
825 826 827
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
828
  (interactive (read-face-and-attribute :foreground))
829
  (set-face-attribute face frame :foreground (or color 'unspecified)))
Gerd Moellmann's avatar
Gerd Moellmann committed
830 831 832 833 834


(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
835
STIPPLE should be a string, the name of a file of pixmap data.
Gerd Moellmann's avatar
Gerd Moellmann committed
836 837 838 839 840 841
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))
842
  (set-face-attribute face frame :stipple (or stipple 'unspecified)))
Gerd Moellmann's avatar
Gerd Moellmann committed
843 844


845
(defun set-face-underline (face underline &optional frame)
Gerd Moellmann's avatar
Gerd Moellmann committed
846 847
  "Specify whether face FACE is underlined.
UNDERLINE nil means FACE explicitly doesn't underline.
848 849 850 851 852 853 854 855
UNDERLINE t means FACE underlines with its foreground color.
If UNDERLINE is a string, underline with that color.

UNDERLINE may also be a list of the form (:color COLOR :style STYLE),
where COLOR is a string or `foreground-color', and STYLE is either
`line' or `wave'.  :color may be omitted, which means to use the
foreground color.  :style may be omitted, which means to use a line.

Gerd Moellmann's avatar
Gerd Moellmann committed
856 857
FRAME nil or not specified means change face on all frames.
Use `set-face-attribute' to ``unspecify'' underlining."
858
  (interactive (read-face-and-attribute :underline))
859
  (set-face-attribute face frame :underline underline))
Gerd Moellmann's avatar
Gerd Moellmann committed
860

861 862
(define-obsolete-function-alias 'set-face-underline-p
                                'set-face-underline "24.3")
863

Gerd Moellmann's avatar
Gerd Moellmann committed
864

865
(defun set-face-inverse-video (face inverse-video-p &optional frame)
Gerd Moellmann's avatar
Gerd Moellmann committed
866 867 868 869 870 871 872
  "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)))
873
     (list (car list) (if (cadr list) t))))
Gerd Moellmann's avatar
Gerd Moellmann committed
874 875
  (set-face-attribute face frame :inverse-video inverse-video-p))

876 877
(define-obsolete-function-alias 'set-face-inverse-video-p
                                'set-face-inverse-video "24.4")
Gerd Moellmann's avatar
Gerd Moellmann committed
878

879
(defun set-face-bold (face bold-p &optional frame)
Gerd Moellmann's avatar
Gerd Moellmann committed
880 881 882 883 884 885 886 887 888
  "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)))

889
(define-obsolete-function-alias 'set-face-bold-p 'set-face-bold "24.4")
Gerd Moellmann's avatar
Gerd Moellmann committed
890

891 892

(defun set-face-italic (face italic-p &optional frame)
Gerd Moellmann's avatar
Gerd Moellmann committed
893 894 895 896 897 898 899 900 901
  "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)))

902 903
(define-obsolete-function-alias 'set-face-italic-p 'set-face-italic "24.4")

Gerd Moellmann's avatar
Gerd Moellmann committed
904 905 906 907 908 909

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


(defun invert-face (face &optional frame)
  "Swap the foreground and background colors of FACE.
910
If FRAME is omitted or nil, it means change face on all frames.
Gerd Moellmann's avatar
Gerd Moellmann committed
911 912 913
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
914
  (interactive (list (read-face-name "Invert face")))
Gerd Moellmann's avatar
Gerd Moellmann committed
915 916
  (let ((fg (face-attribute face :foreground frame))
	(bg (face-attribute face :background frame)))
917
    (if (not (and (eq fg 'unspecified) (eq bg 'unspecified)))
Gerd Moellmann's avatar
Gerd Moellmann committed
918 919 920 921 922 923 924 925 926 927 928 929 930
	(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.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

931
(defun read-face-name (prompt &optional default multiple)
932
  "Read a face, defaulting to the face or faces on the char after point.
933
If it has the property `read-face-name', that overrides the `face' property.
934 935
PROMPT should be a string that describes what the caller will do with the face;
it should not end in a space.
936 937 938
The optional argument DEFAULT provides the value to display in the
minibuffer prompt that is returned if the user just types RET
unless DEFAULT is a string (in which case nil is returned).
939 940 941 942
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)))
943 944
        (aliasfaces nil)
        (nonaliasfaces nil)
945
	faces)
946 947 948 949
    ;; 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.
950 951 952 953
    (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))))
954 955 956 957
	(dolist (f faceprop)
	  (if (symbolp f)
	      (push f faces)))
      (if (symbolp faceprop)
958
	  (push faceprop faces)))
959
    (delete-dups faces)
960

961 962 963 964 965 966 967
    ;; 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)))))

968 969 970 971 972
    ;; 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)))))
973
    (require 'crm)
974 975
    (let* ((input
	    ;; Read the input.
976
	    (completing-read-multiple
977 978
	     (if (or faces default)
		 (format "%s (default `%s'): " prompt
979
			 (if faces (mapconcat 'symbol-name faces ",")
980
			   default))
981
	       (format "%s: " prompt))
982
	     (completion-table-in-turn nonaliasfaces aliasfaces)
983
	     nil t nil 'face-name-history
984
	     (if faces (mapconcat 'symbol-name faces ","))))
985 986
	   ;; Canonicalize the output.
	   (output
987
	    (cond ((or (equal input "") (equal input '("")))
988
		   (or faces (unless (stringp default) default)))
989 990 991 992 993
		  ((stringp input)
		   (mapcar 'intern (split-string input ", *" t)))
		  ((listp input)
		   (mapcar 'intern input))
		  (input))))
994 995 996 997 998
      ;; Return either a list of faces or just one face.
      (if multiple
	  output
	(car output)))))

999 1000
;; Not defined without X, but behind window-system test.
(defvar x-bitmap-file-path)
1001

Gerd Moellmann's avatar
Gerd Moellmann committed
1002 1003 1004
(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
Juanma Barranquero's avatar
Juanma Barranquero committed
1005 1006 1007
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
Gerd Moellmann's avatar
Gerd Moellmann committed
1008
an integer value."
Sam Steingold's avatar
Sam Steingold committed
1009
  (let ((valid
Stefan Monnier's avatar
Stefan Monnier committed
1010 1011
         (pcase attribute
           (`:family
1012
            (if (window-system frame)
1013
                (mapcar (lambda (x) (cons x x))
1014
                        (font-family-list))
Sam Steingold's avatar
Sam Steingold committed
1015 1016
	      ;; Only one font on TTYs.
	      (list (cons "default" "default"))))
Stefan Monnier's avatar
Stefan Monnier committed
1017
           (`:foundry
1018
	    (list nil))
Stefan Monnier's avatar
Stefan Monnier committed
1019
	   (`:width
1020
	    (mapcar #'(lambda (x) (cons (symbol-name (aref x 1)) (aref x 1)))
1021
		    font-width-table))
Stefan Monnier's avatar
Stefan Monnier committed
1022
           (`:weight
1023
	    (mapcar #'(lambda (x) (cons (symbol-name (aref x 1)) (aref x 1)))
1024
		    font-weight-table))
Stefan Monnier's avatar
Stefan Monnier committed
1025
	   (`:slant
1026
	    (mapcar #'(lambda (x) (cons (symbol-name (aref x 1)) (aref x 1)))
1027
		    font-slant-table))
Stefan Monnier's avatar
Stefan Monnier committed
1028
	   (`:inverse-video
1029 1030
	    (mapcar #'(lambda (x) (cons (symbol-name x) x))
		    (internal-lisp-face-attribute-values attribute)))
Stefan Monnier's avatar
Stefan Monnier committed
1031
           ((or `:underline `:overline `:strike-through `:box)
1032
            (if (window-system frame)
Sam Steingold's avatar
Sam Steingold committed
1033 1034 1035
                (nconc (mapcar #'(lambda (x) (cons (symbol-name x) x))
                               (internal-lisp-face-attribute-values attribute))
                       (mapcar #'(lambda (c) (cons c c))
1036
                               (defined-colors frame)))
Sam Steingold's avatar
Sam Steingold committed
1037 1038
	      (mapcar #'(lambda (x) (cons (symbol-name x) x))
		      (internal-lisp-face-attribute-values attribute))))
Stefan Monnier's avatar
Stefan Monnier committed
1039
           ((or `:foreground `:background)
Sam Steingold's avatar
Sam Steingold committed
1040 1041
            (mapcar #'(lambda (c) (cons c c))
                    (defined-colors frame)))
Stefan Monnier's avatar
Stefan Monnier committed
1042
           (`:height
Sam Steingold's avatar
Sam Steingold committed
1043
            'integerp)
Stefan Monnier's avatar
Stefan Monnier committed
1044
           (`:stipple
1045
            (and (memq (window-system frame) '(x ns)) ; No stipple on w32
Sam Steingold's avatar
Sam Steingold committed
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)))))
Stefan Monnier's avatar
Stefan Monnier committed
1053
           (`:inherit