faces.el 111 KB
Newer Older
Mark Oteiza's avatar
Mark Oteiza committed
1
;;; faces.el --- Lisp faces -*- lexical-binding: t -*-
Eric S. Raymond's avatar
Eric S. Raymond committed
2

Paul Eggert's avatar
Paul Eggert committed
3
;; Copyright (C) 1992-1996, 1998-2017 Free Software Foundation, Inc.
Eric S. Raymond's avatar
Eric S. Raymond committed
4

5
;; Maintainer: emacs-devel@gnu.org
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)

38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
(defcustom term-file-aliases
  '(("apollo" . "vt100")
    ("vt102" . "vt100")
    ("vt125" . "vt100")
    ("vt201" . "vt200")
    ("vt220" . "vt200")
    ("vt240" . "vt200")
    ("vt300" . "vt200")
    ("vt320" . "vt200")
    ("vt400" . "vt200")
    ("vt420" . "vt200")
    )
  "Alist of terminal type aliases.
Entries are of the form (TYPE . ALIAS), where both elements are strings.
This means to treat a terminal of type TYPE as if it were of type ALIAS."
  :type '(alist :key-type (string :tag "Terminal")
		:value-type (string :tag "Alias"))
  :group 'terminals
Stefan Monnier's avatar
Stefan Monnier committed
56
  :version "25.1")
57

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

60
(defvar help-xref-stack-item)
61 62 63 64 65 66

(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
67 68 69 70 71 72 73 74 75 76 77 78

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; 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
79
  "A list specifying how face font selection chooses fonts.
Gerd Moellmann's avatar
Gerd Moellmann committed
80 81
Each of the four symbols `:width', `:height', `:weight', and `:slant'
must appear once in the list, and the list must not contain any other
82
elements.  Font selection first tries to find a best matching font
83
for those face attributes that appear before in the list.  For
Gerd Moellmann's avatar
Gerd Moellmann committed
84 85 86
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
87
  :tag "Font selection order"
Gerd Moellmann's avatar
Gerd Moellmann committed
88
  :type '(list symbol symbol symbol symbol)
Gerd Moellmann's avatar
Gerd Moellmann committed
89 90 91 92 93
  :group 'font-selection
  :set #'(lambda (symbol value)
	   (set-default symbol value)
	   (internal-set-font-selection-order value)))

94

95
;; In the absence of Fontconfig support, Monospace and Sans Serif are
96 97
;; unavailable, and we fall back on the courier and helv families,
;; which are generally available.
Gerd Moellmann's avatar
Gerd Moellmann committed
98
(defcustom face-font-family-alternatives
99
  (mapcar (lambda (arg) (mapcar 'purecopy arg))
100
  '(("Monospace" "courier" "fixed")
101 102 103 104 105 106 107 108 109 110

    ;; Monospace Serif is an Emacs invention, intended to work around
    ;; portability problems when using Courier.  It should work well
    ;; when combined with Monospaced and with other standard fonts.
    ("Monospace Serif"

     ;; This looks good on GNU/Linux.
     "Courier 10 Pitch"
     ;; This looks good on MS-Windows and OS X.
     "Consolas"
111
     ;; This looks good on macOS.  "Courier" looks good too, but is
112 113 114 115 116 117 118 119 120 121 122 123
     ;; jagged on GNU/Linux and so is listed later as "courier".
     "Courier Std"
     ;; Although these are anti-aliased, they are a bit faint compared
     ;; to the above.
     "FreeMono" "Nimbus Mono L"
     ;; These are aliased and look jagged.
     "courier" "fixed"
     ;; Omit Courier New, as it is the default MS-Windows font and so
     ;; would look no different, and is pretty faint on other platforms.
     )

    ;; This is present for backward compatibility.
124
    ("courier" "CMU Typewriter Text" "fixed")
125

126
    ("Sans Serif" "helv" "helvetica" "arial" "fixed")
127
    ("helv" "helvetica" "arial" "fixed")))
Lute Kamstra's avatar
Lute Kamstra committed
128
  "Alist of alternative font family names.
Pavel Janík's avatar
Pavel Janík committed
129
Each element has the form (FAMILY ALTERNATIVE1 ALTERNATIVE2 ...).
Gerd Moellmann's avatar
Gerd Moellmann committed
130 131
If fonts of family FAMILY can't be loaded, try ALTERNATIVE1, then
ALTERNATIVE2 etc."
Andreas Schwab's avatar
Andreas Schwab committed
132
  :tag "Alternative font families to try"
Gerd Moellmann's avatar
Gerd Moellmann committed
133
  :type '(repeat (repeat string))
Gerd Moellmann's avatar
Gerd Moellmann committed
134 135 136 137 138 139
  :group 'font-selection
  :set #'(lambda (symbol value)
	   (set-default symbol value)
	   (internal-set-alternative-font-family-alist value)))


140 141
;; This is defined originally in xfaces.c.
(defcustom face-font-registry-alternatives
142
  (mapcar (lambda (arg) (mapcar 'purecopy arg))
Daniel Colascione's avatar
Daniel Colascione committed
143
  (if (featurep 'w32)
144
      '(("iso8859-1" "ms-oemlatin")
145
	("gb2312.1980" "gb2312" "gbk" "gb18030")
146 147 148
	("jisx0208.1990" "jisx0208.1983" "jisx0208.1978")
	("ksc5601.1989" "ksx1001.1992" "ksc5601.1987")
	("muletibetan-2" "muletibetan-0"))
149
    '(("gb2312.1980" "gb2312.80&gb8565.88" "gbk" "gb18030")
150 151
      ("jisx0208.1990" "jisx0208.1983" "jisx0208.1978")
      ("ksc5601.1989" "ksx1001.1992" "ksc5601.1987")
152
      ("muletibetan-2" "muletibetan-0"))))
Lute Kamstra's avatar
Lute Kamstra committed
153
  "Alist of alternative font registry names.
Pavel Janík's avatar
Pavel Janík committed
154
Each element has the form (REGISTRY ALTERNATIVE1 ALTERNATIVE2 ...).
155 156 157
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
158
  :tag "Alternative font registries to try"
159
  :type '(repeat (repeat string))
160
  :version "21.1"
161 162 163 164 165
  :group 'font-selection
  :set #'(lambda (symbol value)
	   (set-default symbol value)
	   (internal-set-alternative-font-registry-alist value)))

166

Gerd Moellmann's avatar
Gerd Moellmann committed
167 168 169 170 171 172
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Creation, copying.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


(defun face-list ()
173
  "Return a list of all defined faces."
Gerd Moellmann's avatar
Gerd Moellmann committed
174 175
  (mapcar #'car face-new-frame-defaults))

176
(defun make-face (face)
Gerd Moellmann's avatar
Gerd Moellmann committed
177
  "Define a new face with name FACE, a symbol.
178 179
Do not call this directly from Lisp code; use `defface' instead.

180
If FACE is already known as a face, leave it unmodified.  Return FACE."
181 182
  (interactive (list (read-from-minibuffer
		      "Make face: " nil nil t 'face-name-history)))
Gerd Moellmann's avatar
Gerd Moellmann committed
183 184 185 186 187 188 189 190
  (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.
191
    (make-face-x-resource-internal face))
Gerd Moellmann's avatar
Gerd Moellmann committed
192 193 194 195
  face)

(defun make-empty-face (face)
  "Define a new, empty face with name FACE.
196
Do not call this directly from Lisp code; use `defface' instead."
197 198
  (interactive (list (read-from-minibuffer
		      "Make empty face: " nil nil t 'face-name-history)))
Eli Zaretskii's avatar
Eli Zaretskii committed
199
  (make-face face))
Gerd Moellmann's avatar
Gerd Moellmann committed
200 201

(defun copy-face (old-face new-face &optional frame new-frame)
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
  "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
218 219 220
  (let ((inhibit-quit t))
    (if (null frame)
	(progn
221 222 223 224
	  (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
225 226 227
	  (dolist (frame (frame-list))
	    (copy-face old-face new-face frame))
	  (copy-face old-face new-face t))
228
      (make-empty-face new-face)
Gerd Moellmann's avatar
Gerd Moellmann committed
229 230 231 232 233 234 235 236 237
      (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
238 239
  "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
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
  (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.

255
(defun face-id (face &optional _frame)
Juanma Barranquero's avatar
Juanma Barranquero committed
256
  "Return the internal ID of face with name FACE.
257
If FACE is a face-alias, return the ID of the target face.
Richard M. Stallman's avatar
Richard M. Stallman committed
258 259
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
260
  (check-face face)
261 262
  (or (get face 'face)
      (face-id (get face 'face-alias))))
Gerd Moellmann's avatar
Gerd Moellmann committed
263 264 265 266

(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.
267 268
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
269 270 271 272 273
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)
274
  "Return non-nil if FACE displays differently from the default face.
Gerd Moellmann's avatar
Gerd Moellmann committed
275 276
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).
277
If FRAME is omitted or nil, use the selected frame."
278
  (let ((attrs
279
	 (delq :inherit (mapcar 'car face-attribute-name-alist)))
280 281 282 283 284 285 286 287 288 289
	(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
290 291 292 293 294 295 296 297 298 299


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


300 301 302
(defun face-list-p (face-or-list)
  "True if FACE-OR-LIST is a list of faces.
Return nil if FACE-OR-LIST is a non-nil atom, or a cons cell whose car
303
is either `foreground-color', `background-color', or a keyword."
304 305 306 307 308 309 310
  ;; The logic of merge_face_ref (xfaces.c) is recreated here.
  (and (listp face-or-list)
       (not (memq (car face-or-list)
		  '(foreground-color background-color)))
       (not (keywordp (car face-or-list)))))


Gerd Moellmann's avatar
Gerd Moellmann committed
311 312 313 314 315 316

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Setting face attributes from X resources.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defcustom face-x-resources
317 318 319
  (mapcar
   (lambda (arg)
     ;; FIXME; can we purecopy some of the conses too?
320 321
     (cons (car arg)
	   (cons (purecopy (car (cdr arg))) (purecopy (cdr (cdr arg))))))
Gerd Moellmann's avatar
Gerd Moellmann committed
322
  '((:family (".attributeFamily" . "Face.AttributeFamily"))
323
    (:foundry (".attributeFoundry" . "Face.AttributeFoundry"))
Gerd Moellmann's avatar
Gerd Moellmann committed
324 325 326 327 328
    (:width (".attributeWidth" . "Face.AttributeWidth"))
    (:height (".attributeHeight" . "Face.AttributeHeight"))
    (:weight (".attributeWeight" . "Face.AttributeWeight"))
    (:slant (".attributeSlant" . "Face.AttributeSlant"))
    (:foreground (".attributeForeground" . "Face.AttributeForeground"))
Jan Djärv's avatar
Jan Djärv committed
329 330
    (:distant-foreground
     (".attributeDistantForeground" . "Face.AttributeDistantForeground"))
Gerd Moellmann's avatar
Gerd Moellmann committed
331 332 333 334 335 336 337 338 339 340 341
    (: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"))
342
    (:font (".attributeFont" . "Face.AttributeFont"))
343
    (:inherit (".attributeInherit" . "Face.AttributeInherit"))))
Lute Kamstra's avatar
Lute Kamstra committed
344
  "List of X resources and classes for face attributes.
Gerd Moellmann's avatar
Gerd Moellmann committed
345 346
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
347
\(RESOURCE . CLASS) with RESOURCE being the resource and CLASS being the
Gerd Moellmann's avatar
Gerd Moellmann committed
348
X resource class for the attribute."
349
  :type '(repeat (cons symbol (repeat (cons string string))))
Gerd Moellmann's avatar
Gerd Moellmann committed
350 351 352
  :group 'faces)


353
(declare-function internal-face-x-get-resource "xfaces.c"
Glenn Morris's avatar
Glenn Morris committed
354
		  (resource class &optional frame))
355 356 357 358

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

Gerd Moellmann's avatar
Gerd Moellmann committed
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
(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."
379
  (when (memq (framep frame) '(x w32))
Gerd Moellmann's avatar
Gerd Moellmann committed
380 381 382 383 384
    (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
385 386


Gerd Moellmann's avatar
Gerd Moellmann committed
387 388
(defun make-face-x-resource-internal (face &optional frame)
  "Fill frame-local FACE on FRAME from X resources.
389 390 391 392 393 394 395 396 397 398
FRAME nil or not specified means do it for all frames.

If `inhibit-x-resources' is non-nil, this function does nothing."
  (unless inhibit-x-resources
    (dolist (frame (if (null frame) (frame-list) (list frame)))
      ;; `x-create-frame' already took care of correctly handling
      ;; the reverse video case-- do _not_ touch the default face
      (unless (and (eq face 'default)
		   (frame-parameter frame 'reverse))
        (set-face-attributes-from-resources face frame)))))
Gerd Moellmann's avatar
Gerd Moellmann committed
399 400 401 402 403 404 405


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

406
(defun face-name (face)
Eric S. Raymond's avatar
Eric S. Raymond committed
407
  "Return the name of face FACE."
408
  (symbol-name (check-face face)))
Gerd Moellmann's avatar
Gerd Moellmann committed
409 410


411 412 413
(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).
414
If FRAME is omitted or nil the value describes the default attributes,
415 416
but if you specify FRAME, the value describes the attributes
of FACE on FRAME."
417 418 419
  (mapcar (lambda (pair)
	    (let ((attr (car pair)))
	      (cons attr (face-attribute face attr (or frame t)))))
420 421
  	  face-attribute-name-alist))

422
(defun face-attribute (face attribute &optional frame inherit)
Gerd Moellmann's avatar
Gerd Moellmann committed
423 424 425
  "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).
426
If FRAME is omitted or nil, use the selected frame.
Gerd Moellmann's avatar
Gerd Moellmann committed
427

428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
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))
445 446 447 448 449
          (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)))))
450 451 452
    (when (and inherit
	       (not (eq inherit t))
	       (face-attribute-relative-p attribute value))
453
      ;; We should merge with INHERIT as well
454 455
      (setq value (face-attribute-merged-with attribute value inherit frame)))
    value))
Gerd Moellmann's avatar
Gerd Moellmann committed
456

457 458 459
(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.
460
[This is an internal function.]"
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
  (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
486 487 488
  "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).
489
If FRAME is omitted or nil, use the selected frame.
Gerd Moellmann's avatar
Gerd Moellmann committed
490

491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
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
506 507 508
  "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).
509
If FRAME is omitted or nil, use the selected frame.
Gerd Moellmann's avatar
Gerd Moellmann committed
510

511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
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
526 527 528
 "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).
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
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
544 545 546 547 548


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


549
(defun face-underline-p (face &optional frame inherit)
550
 "Return non-nil if FACE specifies a non-nil underlining.
Gerd Moellmann's avatar
Gerd Moellmann committed
551 552
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).
553 554 555 556
If FRAME is omitted or nil, use the selected frame.
Optional argument INHERIT is passed to `face-attribute'."
 (face-attribute-specified-or
  (face-attribute face :underline frame inherit) nil))
Gerd Moellmann's avatar
Gerd Moellmann committed
557 558


559
(defun face-inverse-video-p (face &optional frame inherit)
560
 "Return non-nil if FACE specifies a non-nil inverse-video.
Gerd Moellmann's avatar
Gerd Moellmann committed
561 562
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).
563 564 565
If FRAME is omitted or nil, use the selected frame.
Optional argument INHERIT is passed to `face-attribute'."
 (eq (face-attribute face :inverse-video frame inherit) t))
Gerd Moellmann's avatar
Gerd Moellmann committed
566 567


568
(defun face-bold-p (face &optional frame inherit)
Gerd Moellmann's avatar
Gerd Moellmann committed
569 570 571 572
  "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.
573
Optional argument INHERIT is passed to `face-attribute'.
Gerd Moellmann's avatar
Gerd Moellmann committed
574
Use `face-attribute' for finer control."
575
  (let ((bold (face-attribute face :weight frame inherit)))
576
    (memq bold '(semi-bold bold extra-bold ultra-bold))))
Gerd Moellmann's avatar
Gerd Moellmann committed
577 578


579
(defun face-italic-p (face &optional frame inherit)
Gerd Moellmann's avatar
Gerd Moellmann committed
580 581 582 583
  "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.
584
Optional argument INHERIT is passed to `face-attribute'.
Gerd Moellmann's avatar
Gerd Moellmann committed
585
Use `face-attribute' for finer control."
586
  (let ((italic (face-attribute face :slant frame inherit)))
587
    (memq italic '(italic oblique))))
Sam Steingold's avatar
Sam Steingold committed
588

Gerd Moellmann's avatar
Gerd Moellmann committed
589 590 591 592 593 594 595

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

(defun face-documentation (face)
596 597
  "Get the documentation string for FACE.
If FACE is a face-alias, get the documentation for the target face."
598
  (let ((alias (get face 'face-alias)))
599
    (if alias
600
        (let ((doc (get alias 'face-documentation)))
601
	  (format "%s is an alias for the face `%s'.%s" face alias
602 603 604
                  (if doc (format "\n%s" doc)
                    "")))
      (get face 'face-documentation))))
Gerd Moellmann's avatar
Gerd Moellmann committed
605 606 607 608


(defun set-face-documentation (face string)
  "Set the documentation string for FACE to STRING."
609
  ;; Perhaps the text should go in DOC.
610
  (put face 'face-documentation (purecopy string)))
Gerd Moellmann's avatar
Gerd Moellmann committed
611 612 613 614 615 616 617 618 619 620 621 622 623 624


(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.
625 626
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
627

628 629 630
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
631

632 633 634
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
635 636 637 638 639

The following attributes are recognized:

`:family'

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

643 644 645
`:foundry'

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

Gerd Moellmann's avatar
Gerd Moellmann committed
649 650 651 652 653 654 655 656 657
`: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'

658 659 660
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,
661
which specifies a scaling factor for the underlying face height;
662 663 664
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
665
for it to be relative to).
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

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

685 686 687 688 689 690 691 692 693 694 695 696 697 698 699
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
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725

`: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
726 727 728
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
729 730 731 732 733 734 735 736 737 738 739
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
740
inverse video.  VALUE must be one of t or nil.
Gerd Moellmann's avatar
Gerd Moellmann committed
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.

751 752 753
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
754 755 756

`:font'

757 758 759 760
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
761 762 763

`:inherit'

764 765 766 767 768 769 770 771 772 773 774 775 776
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."
777 778 779
  (setq args (purecopy args))
  (let ((where (if (null frame) 0 frame))
	(spec args)
780
	family foundry orig-family orig-foundry)
781 782
    ;; If we set the new-frame defaults, this face is modified outside Custom.
    (if (memq where '(0 t))
783
	(put (or (get face 'face-alias) face) 'face-modified t))
784 785 786 787 788 789 790 791 792 793 794 795
    ;; 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))
796 797
        (setq orig-foundry foundry
              orig-family family)
798
	(unless foundry
799
	  (setq foundry (match-string 1 family)))
800 801 802 803 804 805
	(setq family (match-string 2 family))
        ;; Reject bogus "families" that are all-digits -- those are some
        ;; weird font names, like Foobar-12, that end in a number.
        (when (string-match "\\`[0-9]*\\'" family)
          (setq family orig-family)
          (setq foundry orig-foundry)))
806
      (when (or (stringp family) (eq family 'unspecified))
807 808
	(internal-set-lisp-face-attribute face :family (purecopy family)
					  where))
809
      (when (or (stringp foundry) (eq foundry 'unspecified))
810 811
	(internal-set-lisp-face-attribute face :foundry (purecopy foundry)
					  where)))
812
    (while args
813
      (unless (memq (car args) '(:family :foundry))
814 815 816
	(internal-set-lisp-face-attribute face (car args)
					  (purecopy (cadr args))
					  where))
817
      (setq args (cddr args)))))
Gerd Moellmann's avatar
Gerd Moellmann committed
818

819
(defun make-face-bold (face &optional frame _noerror)
Gerd Moellmann's avatar
Gerd Moellmann committed
820 821
  "Make the font of FACE be bold, if possible.
FRAME nil or not specified means change face on all frames.
822
Argument NOERROR is ignored and retained for compatibility.
Gerd Moellmann's avatar
Gerd Moellmann committed
823
Use `set-face-attribute' for finer control of the font weight."
824 825
  (interactive (list (read-face-name "Make which face bold"
                                     (face-at-point t))))
Gerd Moellmann's avatar
Gerd Moellmann committed
826 827 828
  (set-face-attribute face frame :weight 'bold))


829
(defun make-face-unbold (face &optional frame _noerror)
Gerd Moellmann's avatar
Gerd Moellmann committed
830
  "Make the font of FACE be non-bold, if possible.
831 832
FRAME nil or not specified means change face on all frames.
Argument NOERROR is ignored and retained for compatibility."
833 834
  (interactive (list (read-face-name "Make which face non-bold"
                                     (face-at-point t))))
Gerd Moellmann's avatar
Gerd Moellmann committed
835 836
  (set-face-attribute face frame :weight 'normal))

Sam Steingold's avatar
Sam Steingold committed
837

838
(defun make-face-italic (face &optional frame _noerror)
Gerd Moellmann's avatar
Gerd Moellmann committed
839 840
  "Make the font of FACE be italic, if possible.
FRAME nil or not specified means change face on all frames.
841
Argument NOERROR is ignored and retained for compatibility.
Gerd Moellmann's avatar
Gerd Moellmann committed
842
Use `set-face-attribute' for finer control of the font slant."
843 844
  (interactive (list (read-face-name "Make which face italic"
                                     (face-at-point t))))
Gerd Moellmann's avatar
Gerd Moellmann committed
845 846 847
  (set-face-attribute face frame :slant 'italic))


848
(defun make-face-unitalic (face &optional frame _noerror)
Gerd Moellmann's avatar
Gerd Moellmann committed
849
  "Make the font of FACE be non-italic, if possible.
Dave Love's avatar
Dave Love committed
850 851
FRAME nil or not specified means change face on all frames.
Argument NOERROR is ignored and retained for compatibility."
852 853
  (interactive (list (read-face-name "Make which face non-italic"
                                     (face-at-point t))))
Gerd Moellmann's avatar
Gerd Moellmann committed
854 855
  (set-face-attribute face frame :slant 'normal))

Sam Steingold's avatar
Sam Steingold committed
856

857
(defun make-face-bold-italic (face &optional frame _noerror)
Gerd Moellmann's avatar
Gerd Moellmann committed
858 859
  "Make the font of FACE be bold and italic, if possible.
FRAME nil or not specified means change face on all frames.
860
Argument NOERROR is ignored and retained for compatibility.
Gerd Moellmann's avatar
Gerd Moellmann committed
861
Use `set-face-attribute' for finer control of font weight and slant."
862 863
  (interactive (list (read-face-name "Make which face bold-italic"
                                     (face-at-point t))))
Gerd Moellmann's avatar
Gerd Moellmann committed
864 865 866 867 868 869
  (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.
870 871 872
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
873 874 875 876 877 878 879 880 881 882 883
  (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.
884 885 886
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
887
  (interactive (read-face-and-attribute :background))
888
  (set-face-attribute face frame :background (or color 'unspecified)))
Gerd Moellmann's avatar
Gerd Moellmann committed
889 890 891 892 893


(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.
894 895 896
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
897
  (interactive (read-face-and-attribute :foreground))
898
  (set-face-attribute face frame :foreground (or color 'unspecified)))
Gerd Moellmann's avatar
Gerd Moellmann committed
899 900 901 902 903


(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
904
STIPPLE should be a string, the name of a file of pixmap data.
Gerd Moellmann's avatar
Gerd Moellmann committed
905 906 907 908 909 910
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))
911
  (set-face-attribute face frame :stipple (or stipple 'unspecified)))
Gerd Moellmann's avatar
Gerd Moellmann committed
912 913


914
(defun set-face-underline (face underline &optional frame)
Gerd Moellmann's avatar
Gerd Moellmann committed
915 916
  "Specify whether face FACE is underlined.
UNDERLINE nil means FACE explicitly doesn't underline.
917 918 919 920 921 922 923 924
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
925
FRAME nil or not specified means change face on all frames.
926
Use `set-face-attribute' to \"unspecify\" underlining."
927
  (interactive (read-face-and-attribute :underline))
928
  (set-face-attribute face frame :underline underline))
Gerd Moellmann's avatar
Gerd Moellmann committed
929

930 931
(define-obsolete-function-alias 'set-face-underline-p
                                'set-face-underline "24.3")
932

Gerd Moellmann's avatar
Gerd Moellmann committed
933

934
(defun set-face-inverse-video (face inverse-video-p &optional frame)
Gerd Moellmann's avatar
Gerd Moellmann committed
935 936 937 938
  "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.
939
Use `set-face-attribute' to \"unspecify\" the inverse video attribute."
Gerd Moellmann's avatar
Gerd Moellmann committed
940 941
  (interactive
   (let ((list (read-face-and-attribute :inverse-video)))
942
     (list (car list) (if (cadr list) t))))
Gerd Moellmann's avatar
Gerd Moellmann committed
943 944
  (set-face-attribute face frame :inverse-video inverse-video-p))

945 946
(define-obsolete-function-alias 'set-face-inverse-video-p
                                'set-face-inverse-video "24.4")
Gerd Moellmann's avatar
Gerd Moellmann committed
947

948
(defun set-face-bold (face bold-p &optional frame)
Gerd Moellmann's avatar
Gerd Moellmann committed
949 950 951 952 953 954 955 956 957
  "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)))

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

960 961

(defun set-face-italic (face italic-p &optional frame)
Gerd Moellmann's avatar
Gerd Moellmann committed
962 963 964 965 966 967 968 969 970
  "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)))

971 972
(define-obsolete-function-alias 'set-face-italic-p 'set-face-italic "24.4")

Gerd Moellmann's avatar
Gerd Moellmann committed
973 974 975 976 977 978

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


(defun invert-face (face &optional frame)
  "Swap the foreground and background colors of FACE.
979
If FRAME is omitted or nil, it means change face on all frames.
Gerd Moellmann's avatar
Gerd Moellmann committed
980 981 982
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."
983
  (interactive (list (read-face-name "Invert face" (face-at-point t))))
Gerd Moellmann's avatar
Gerd Moellmann committed
984 985
  (let ((fg (face-attribute face :foreground frame))
	(bg (face-attribute face :background frame)))
986
    (if (not (and (eq fg 'unspecified) (eq bg 'unspecified)))
Gerd Moellmann's avatar
Gerd Moellmann committed
987 988 989 990 991 992 993 994 995 996 997 998 999
	(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.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

1000 1001
(defvar crm-separator) ; from crm.el

1002
(defun read-face-name (prompt &optional default multiple)
1003 1004
  "Read one or more face names, prompting with PROMPT.
PROMPT should not end in a space or a colon.
1005

1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
If DEFAULT is non-nil, it should be a face (a symbol) or a face
name (a string).  It can also be a list of faces or face names.

If MULTIPLE is non-nil, the return value from this function is a
list of faces.  Otherwise a single face is returned.

If the user enter the empty string at the prompt, DEFAULT is
returned after a possible transformation according to MULTIPLE.
That is, if DEFAULT is a list and MULTIPLE is nil, the first
element of DEFAULT is returned.  If DEFAULT isn't a list, but
MULTIPLE is non-nil, a one-element list containing DEFAULT is
returned.  Otherwise, DEFAULT is returned verbatim."
  (unless (listp default)
    (setq default (list default)))
  (when default
    (setq default
          (if multiple
              (mapconcat (lambda (f) (if (symbolp f) (symbol-name f) f))
                         default ", ")
            ;; If we only want one, and the default is more than one,
            ;; discard the unwanted ones.
            (setq default (car default))
            (if (symbolp default)
                (symbol-name default)
              default))))
1031 1032 1033 1034 1035
  (when (and default (not multiple))
    (require 'crm)
    ;; For compatibility with `completing-read-multiple' use `crm-separator'
    ;; to define DEFAULT if MULTIPLE is nil.
    (setq default (car (split-string default crm-separator t))))
1036

1037 1038 1039 1040
  ;; Older versions of `read-face-name' did not append ": " to the
  ;; prompt, so there are third party libraries that have that in the
  ;; prompt.  If so, remove it.
  (setq prompt (replace-regexp-in-string ": ?\\'" "" prompt))
1041
  (let ((prompt (if default
1042
                    (format-message "%s (default `%s'): " prompt default)
1043 1044
                  (format "%s: " prompt)))
        aliasfaces nonaliasfaces faces)
1045
    ;; Build up the completion tables.
1046
    (mapatoms (lambda (s)
1047
                (if (facep s)
1048 1049 1050
                    (if (get s 'face-alias)
                        (push (symbol-name s) aliasfaces)
                      (push (symbol-name s) nonaliasfaces)))))
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
    (if multiple
        (progn
          (dolist (face (completing-read-multiple
                         prompt
                         (completion-table-in-turn nonaliasfaces aliasfaces)
                         nil t nil 'face-name-history default))
            ;; Ignore elements that are not faces
            ;; (for example, because DEFAULT was "all faces")
            (if (facep face) (push (intern face) faces)))
          (nreverse faces))
      (let ((face (completing-read
                   prompt
1063
                   (completion-table-in-turn nonaliasfaces aliasfaces)
1064 1065
                   nil t nil 'face-name-history default)))
        (if (facep face) (intern face))))))