]> code.delx.au - gnu-emacs/blob - lisp/faces.el
Merge from gnus--devo--0
[gnu-emacs] / lisp / faces.el
1 ;;; faces.el --- Lisp faces
2
3 ;; Copyright (C) 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001,
4 ;; 2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5
6 ;; Maintainer: FSF
7 ;; Keywords: internal
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; 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/>.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (eval-when-compile
29 (require 'cl))
30
31 (declare-function xw-defined-colors "term/x-win" (&optional frame))
32
33 (defvar help-xref-stack-item)
34 \f
35 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
36 ;;; Font selection.
37 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
38
39 (defgroup font-selection nil
40 "Influencing face font selection."
41 :group 'faces)
42
43
44 (defcustom face-font-selection-order
45 '(:width :height :weight :slant)
46 "*A list specifying how face font selection chooses fonts.
47 Each of the four symbols `:width', `:height', `:weight', and `:slant'
48 must appear once in the list, and the list must not contain any other
49 elements. Font selection first tries to find a best matching font
50 for those face attributes that appear before in the list. For
51 example, if `:slant' appears before `:height', font selection first
52 tries to find a font with a suitable slant, even if this results in
53 a font height that isn't optimal."
54 :tag "Font selection order"
55 :type '(list symbol symbol symbol symbol)
56 :group 'font-selection
57 :set #'(lambda (symbol value)
58 (set-default symbol value)
59 (internal-set-font-selection-order value)))
60
61
62 ;; This is defined originally in xfaces.c.
63 (defcustom face-font-family-alternatives
64 '(("courier" "fixed")
65 ("helv" "helvetica" "arial" "fixed"))
66 "*Alist of alternative font family names.
67 Each element has the form (FAMILY ALTERNATIVE1 ALTERNATIVE2 ...).
68 If fonts of family FAMILY can't be loaded, try ALTERNATIVE1, then
69 ALTERNATIVE2 etc."
70 :tag "Alternative font families to try"
71 :type '(repeat (repeat string))
72 :group 'font-selection
73 :set #'(lambda (symbol value)
74 (set-default symbol value)
75 (internal-set-alternative-font-family-alist value)))
76
77
78 ;; This is defined originally in xfaces.c.
79 (defcustom face-font-registry-alternatives
80 (if (eq system-type 'windows-nt)
81 '(("iso8859-1" "ms-oemlatin")
82 ("gb2312.1980" "gb2312" "gbk" "gb18030")
83 ("jisx0208.1990" "jisx0208.1983" "jisx0208.1978")
84 ("ksc5601.1989" "ksx1001.1992" "ksc5601.1987")
85 ("muletibetan-2" "muletibetan-0"))
86 '(("gb2312.1980" "gb2312.80&gb8565.88" "gbk" "gb18030")
87 ("jisx0208.1990" "jisx0208.1983" "jisx0208.1978")
88 ("ksc5601.1989" "ksx1001.1992" "ksc5601.1987")
89 ("muletibetan-2" "muletibetan-0")))
90 "*Alist of alternative font registry names.
91 Each element has the form (REGISTRY ALTERNATIVE1 ALTERNATIVE2 ...).
92 If fonts of registry REGISTRY can be loaded, font selection
93 tries to find a best matching font among all fonts of registry
94 REGISTRY, ALTERNATIVE1, ALTERNATIVE2, and etc."
95 :tag "Alternative font registries to try"
96 :type '(repeat (repeat string))
97 :version "21.1"
98 :group 'font-selection
99 :set #'(lambda (symbol value)
100 (set-default symbol value)
101 (internal-set-alternative-font-registry-alist value)))
102
103 \f
104 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
105 ;;; Creation, copying.
106 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
107
108
109 (defun face-list ()
110 "Return a list of all defined face names."
111 (mapcar #'car face-new-frame-defaults))
112
113
114 ;;; ### If not frame-local initialize by what X resources?
115
116 (defun make-face (face &optional no-init-from-resources)
117 "Define a new face with name FACE, a symbol.
118 NO-INIT-FROM-RESOURCES non-nil means don't initialize frame-local
119 variants of FACE from X resources. (X resources recognized are found
120 in the global variable `face-x-resources'.) If FACE is already known
121 as a face, leave it unmodified. Value is FACE."
122 (interactive "SMake face: ")
123 (unless (facep face)
124 ;; Make frame-local faces (this also makes the global one).
125 (dolist (frame (frame-list))
126 (internal-make-lisp-face face frame))
127 ;; Add the face to the face menu.
128 (when (fboundp 'facemenu-add-new-face)
129 (facemenu-add-new-face face))
130 ;; Define frame-local faces for all frames from X resources.
131 (unless no-init-from-resources
132 (make-face-x-resource-internal face)))
133 face)
134
135
136 (defun make-empty-face (face)
137 "Define a new, empty face with name FACE.
138 If the face already exists, it is left unmodified. Value is FACE."
139 (interactive "SMake empty face: ")
140 (make-face face 'no-init-from-resources))
141
142
143 (defun copy-face (old-face new-face &optional frame new-frame)
144 "Define a face just like OLD-FACE, with name NEW-FACE.
145
146 If NEW-FACE already exists as a face, it is modified to be like
147 OLD-FACE. If it doesn't already exist, it is created.
148
149 If the optional argument FRAME is given as a frame, NEW-FACE is
150 changed on FRAME only.
151 If FRAME is t, the frame-independent default specification for OLD-FACE
152 is copied to NEW-FACE.
153 If FRAME is nil, copying is done for the frame-independent defaults
154 and for each existing frame.
155
156 If the optional fourth argument NEW-FRAME is given,
157 copy the information from face OLD-FACE on frame FRAME
158 to NEW-FACE on frame NEW-FRAME. In this case, FRAME may not be nil."
159 (let ((inhibit-quit t))
160 (if (null frame)
161 (progn
162 (when new-frame
163 (error "Copying face %s from all frames to one frame"
164 old-face))
165 (make-empty-face new-face)
166 (dolist (frame (frame-list))
167 (copy-face old-face new-face frame))
168 (copy-face old-face new-face t))
169 (make-empty-face new-face)
170 (internal-copy-lisp-face old-face new-face frame new-frame))
171 new-face))
172
173
174 \f
175 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
176 ;;; Obsolete functions
177 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
178
179 ;; The functions in this section are defined because Lisp packages use
180 ;; them, despite the prefix `internal-' suggesting that they are
181 ;; private to the face implementation.
182
183 (defun internal-find-face (name &optional frame)
184 "Retrieve the face named NAME.
185 Return nil if there is no such face.
186 If NAME is already a face, it is simply returned.
187 The optional argument FRAME is ignored."
188 (facep name))
189 (make-obsolete 'internal-find-face 'facep "21.1")
190
191
192 (defun internal-get-face (name &optional frame)
193 "Retrieve the face named NAME; error if there is none.
194 If NAME is already a face, it is simply returned.
195 The optional argument FRAME is ignored."
196 (or (facep name)
197 (check-face name)))
198 (make-obsolete 'internal-get-face "see `facep' and `check-face'." "21.1")
199
200 \f
201 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
202 ;;; Predicates, type checks.
203 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
204
205 (defun facep (face)
206 "Return non-nil if FACE is a face name; nil otherwise.
207 A face name can be a string or a symbol."
208 (internal-lisp-face-p face))
209
210
211 (defun check-face (face)
212 "Signal an error if FACE doesn't name a face.
213 Value is FACE."
214 (unless (facep face)
215 (error "Not a face: %s" face))
216 face)
217
218
219 ;; The ID returned is not to be confused with the internally used IDs
220 ;; of realized faces. The ID assigned to Lisp faces is used to
221 ;; support faces in display table entries.
222
223 (defun face-id (face &optional frame)
224 "Return the internal ID of face with name FACE.
225 If FACE is a face-alias, return the ID of the target face.
226 The optional argument FRAME is ignored, since the internal face ID
227 of a face name is the same for all frames."
228 (check-face face)
229 (or (get face 'face)
230 (face-id (get face 'face-alias))))
231
232 (defun face-equal (face1 face2 &optional frame)
233 "Non-nil if faces FACE1 and FACE2 are equal.
234 Faces are considered equal if all their attributes are equal.
235 If the optional argument FRAME is given, report on FACE1 and FACE2 in that frame.
236 If FRAME is t, report on the defaults for FACE1 and FACE2 (for new frames).
237 If FRAME is omitted or nil, use the selected frame."
238 (internal-lisp-face-equal-p face1 face2 frame))
239
240
241 (defun face-differs-from-default-p (face &optional frame)
242 "Return non-nil if FACE displays differently from the default face.
243 If the optional argument FRAME is given, report on face FACE in that frame.
244 If FRAME is t, report on the defaults for face FACE (for new frames).
245 If FRAME is omitted or nil, use the selected frame."
246 (let ((attrs
247 (delq :inherit (mapcar 'car face-attribute-name-alist)))
248 (differs nil))
249 (while (and attrs (not differs))
250 (let* ((attr (pop attrs))
251 (attr-val (face-attribute face attr frame t)))
252 (when (and
253 (not (eq attr-val 'unspecified))
254 (display-supports-face-attributes-p (list attr attr-val)
255 frame))
256 (setq differs attr))))
257 differs))
258
259
260 (defun face-nontrivial-p (face &optional frame)
261 "True if face FACE has some non-nil attribute.
262 If the optional argument FRAME is given, report on face FACE in that frame.
263 If FRAME is t, report on the defaults for face FACE (for new frames).
264 If FRAME is omitted or nil, use the selected frame."
265 (not (internal-lisp-face-empty-p face frame)))
266
267
268 \f
269 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
270 ;;; Setting face attributes from X resources.
271 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
272
273 (defcustom face-x-resources
274 '((:family (".attributeFamily" . "Face.AttributeFamily"))
275 (:width (".attributeWidth" . "Face.AttributeWidth"))
276 (:height (".attributeHeight" . "Face.AttributeHeight"))
277 (:weight (".attributeWeight" . "Face.AttributeWeight"))
278 (:slant (".attributeSlant" . "Face.AttributeSlant"))
279 (:foreground (".attributeForeground" . "Face.AttributeForeground"))
280 (:background (".attributeBackground" . "Face.AttributeBackground"))
281 (:overline (".attributeOverline" . "Face.AttributeOverline"))
282 (:strike-through (".attributeStrikeThrough" . "Face.AttributeStrikeThrough"))
283 (:box (".attributeBox" . "Face.AttributeBox"))
284 (:underline (".attributeUnderline" . "Face.AttributeUnderline"))
285 (:inverse-video (".attributeInverse" . "Face.AttributeInverse"))
286 (:stipple
287 (".attributeStipple" . "Face.AttributeStipple")
288 (".attributeBackgroundPixmap" . "Face.AttributeBackgroundPixmap"))
289 (:bold (".attributeBold" . "Face.AttributeBold"))
290 (:italic (".attributeItalic" . "Face.AttributeItalic"))
291 (:font (".attributeFont" . "Face.AttributeFont"))
292 (:inherit (".attributeInherit" . "Face.AttributeInherit")))
293 "*List of X resources and classes for face attributes.
294 Each element has the form (ATTRIBUTE ENTRY1 ENTRY2...) where ATTRIBUTE is
295 the name of a face attribute, and each ENTRY is a cons of the form
296 \(RESOURCE . CLASS) with RESOURCE being the resource and CLASS being the
297 X resource class for the attribute."
298 :type '(repeat (cons symbol (repeat (cons string string))))
299 :group 'faces)
300
301
302 (defun set-face-attribute-from-resource (face attribute resource class frame)
303 "Set FACE's ATTRIBUTE from X resource RESOURCE, class CLASS on FRAME.
304 Value is the attribute value specified by the resource, or nil
305 if not present. This function displays a message if the resource
306 specifies an invalid attribute."
307 (let* ((face-name (face-name face))
308 (value (internal-face-x-get-resource (concat face-name resource)
309 class frame)))
310 (when value
311 (condition-case ()
312 (internal-set-lisp-face-attribute-from-resource
313 face attribute (downcase value) frame)
314 (error
315 (message "Face %s, frame %s: invalid attribute %s %s from X resource"
316 face-name frame attribute value))))
317 value))
318
319
320 (defun set-face-attributes-from-resources (face frame)
321 "Set attributes of FACE from X resources for FRAME."
322 (when (memq (framep frame) '(x w32 mac))
323 (dolist (definition face-x-resources)
324 (let ((attribute (car definition)))
325 (dolist (entry (cdr definition))
326 (set-face-attribute-from-resource face attribute (car entry)
327 (cdr entry) frame))))))
328
329
330 (defun make-face-x-resource-internal (face &optional frame)
331 "Fill frame-local FACE on FRAME from X resources.
332 FRAME nil or not specified means do it for all frames."
333 (if (null frame)
334 (dolist (frame (frame-list))
335 (set-face-attributes-from-resources face frame))
336 (set-face-attributes-from-resources face frame)))
337
338
339 \f
340 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
341 ;;; Retrieving face attributes.
342 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
343
344 (defun face-name (face)
345 "Return the name of face FACE."
346 (symbol-name (check-face face)))
347
348
349 (defun face-all-attributes (face &optional frame)
350 "Return an alist stating the attributes of FACE.
351 Each element of the result has the form (ATTR-NAME . ATTR-VALUE).
352 Normally the value describes the default attributes,
353 but if you specify FRAME, the value describes the attributes
354 of FACE on FRAME."
355 (mapcar (lambda (pair)
356 (let ((attr (car pair)))
357 (cons attr (face-attribute face attr (or frame t)))))
358 face-attribute-name-alist))
359
360 (defun face-attribute (face attribute &optional frame inherit)
361 "Return the value of FACE's ATTRIBUTE on FRAME.
362 If the optional argument FRAME is given, report on face FACE in that frame.
363 If FRAME is t, report on the defaults for face FACE (for new frames).
364 If FRAME is omitted or nil, use the selected frame.
365
366 If INHERIT is nil, only attributes directly defined by FACE are considered,
367 so the return value may be `unspecified', or a relative value.
368 If INHERIT is non-nil, FACE's definition of ATTRIBUTE is merged with the
369 faces specified by its `:inherit' attribute; however the return value
370 may still be `unspecified' or relative.
371 If INHERIT is a face or a list of faces, then the result is further merged
372 with that face (or faces), until it becomes specified and absolute.
373
374 To ensure that the return value is always specified and absolute, use a
375 value of `default' for INHERIT; this will resolve any unspecified or
376 relative values by merging with the `default' face (which is always
377 completely specified)."
378 (let ((value (internal-get-lisp-face-attribute face attribute frame)))
379 (when (and inherit (face-attribute-relative-p attribute value))
380 ;; VALUE is relative, so merge with inherited faces
381 (let ((inh-from (face-attribute face :inherit frame)))
382 (unless (or (null inh-from) (eq inh-from 'unspecified))
383 (condition-case nil
384 (setq value
385 (face-attribute-merged-with attribute value inh-from frame))
386 ;; The `inherit' attribute may point to non existent faces.
387 (error nil)))))
388 (when (and inherit
389 (not (eq inherit t))
390 (face-attribute-relative-p attribute value))
391 ;; We should merge with INHERIT as well
392 (setq value (face-attribute-merged-with attribute value inherit frame)))
393 value))
394
395 (defun face-attribute-merged-with (attribute value faces &optional frame)
396 "Merges ATTRIBUTE, initially VALUE, with faces from FACES until absolute.
397 FACES may be either a single face or a list of faces.
398 \[This is an internal function.]"
399 (cond ((not (face-attribute-relative-p attribute value))
400 value)
401 ((null faces)
402 value)
403 ((consp faces)
404 (face-attribute-merged-with
405 attribute
406 (face-attribute-merged-with attribute value (car faces) frame)
407 (cdr faces)
408 frame))
409 (t
410 (merge-face-attribute attribute
411 value
412 (face-attribute faces attribute frame t)))))
413
414
415 (defmacro face-attribute-specified-or (value &rest body)
416 "Return VALUE, unless it's `unspecified', in which case evaluate BODY and return the result."
417 (let ((temp (make-symbol "value")))
418 `(let ((,temp ,value))
419 (if (not (eq ,temp 'unspecified))
420 ,temp
421 ,@body))))
422
423 (defun face-foreground (face &optional frame inherit)
424 "Return the foreground color name of FACE, or nil if unspecified.
425 If the optional argument FRAME is given, report on face FACE in that frame.
426 If FRAME is t, report on the defaults for face FACE (for new frames).
427 If FRAME is omitted or nil, use the selected frame.
428
429 If INHERIT is nil, only a foreground color directly defined by FACE is
430 considered, so the return value may be nil.
431 If INHERIT is t, and FACE doesn't define a foreground color, then any
432 foreground color that FACE inherits through its `:inherit' attribute
433 is considered as well; however the return value may still be nil.
434 If INHERIT is a face or a list of faces, then it is used to try to
435 resolve an unspecified foreground color.
436
437 To ensure that a valid color is always returned, use a value of
438 `default' for INHERIT; this will resolve any unspecified values by
439 merging with the `default' face (which is always completely specified)."
440 (face-attribute-specified-or (face-attribute face :foreground frame inherit)
441 nil))
442
443 (defun face-background (face &optional frame inherit)
444 "Return the background color name of FACE, or nil if unspecified.
445 If the optional argument FRAME is given, report on face FACE in that frame.
446 If FRAME is t, report on the defaults for face FACE (for new frames).
447 If FRAME is omitted or nil, use the selected frame.
448
449 If INHERIT is nil, only a background color directly defined by FACE is
450 considered, so the return value may be nil.
451 If INHERIT is t, and FACE doesn't define a background color, then any
452 background color that FACE inherits through its `:inherit' attribute
453 is considered as well; however the return value may still be nil.
454 If INHERIT is a face or a list of faces, then it is used to try to
455 resolve an unspecified background color.
456
457 To ensure that a valid color is always returned, use a value of
458 `default' for INHERIT; this will resolve any unspecified values by
459 merging with the `default' face (which is always completely specified)."
460 (face-attribute-specified-or (face-attribute face :background frame inherit)
461 nil))
462
463 (defun face-stipple (face &optional frame inherit)
464 "Return the stipple pixmap name of FACE, or nil if unspecified.
465 If the optional argument FRAME is given, report on face FACE in that frame.
466 If FRAME is t, report on the defaults for face FACE (for new frames).
467 If FRAME is omitted or nil, use the selected frame.
468
469 If INHERIT is nil, only a stipple directly defined by FACE is
470 considered, so the return value may be nil.
471 If INHERIT is t, and FACE doesn't define a stipple, then any stipple
472 that FACE inherits through its `:inherit' attribute is considered as
473 well; however the return value may still be nil.
474 If INHERIT is a face or a list of faces, then it is used to try to
475 resolve an unspecified stipple.
476
477 To ensure that a valid stipple or nil is always returned, use a value of
478 `default' for INHERIT; this will resolve any unspecified values by merging
479 with the `default' face (which is always completely specified)."
480 (face-attribute-specified-or (face-attribute face :stipple frame inherit)
481 nil))
482
483
484 (defalias 'face-background-pixmap 'face-stipple)
485
486
487 (defun face-underline-p (face &optional frame)
488 "Return non-nil if FACE is underlined.
489 If the optional argument FRAME is given, report on face FACE in that frame.
490 If FRAME is t, report on the defaults for face FACE (for new frames).
491 If FRAME is omitted or nil, use the selected frame."
492 (eq (face-attribute face :underline frame) t))
493
494
495 (defun face-inverse-video-p (face &optional frame)
496 "Return non-nil if FACE is in inverse video on FRAME.
497 If the optional argument FRAME is given, report on face FACE in that frame.
498 If FRAME is t, report on the defaults for face FACE (for new frames).
499 If FRAME is omitted or nil, use the selected frame."
500 (eq (face-attribute face :inverse-video frame) t))
501
502
503 (defun face-bold-p (face &optional frame)
504 "Return non-nil if the font of FACE is bold on FRAME.
505 If the optional argument FRAME is given, report on face FACE in that frame.
506 If FRAME is t, report on the defaults for face FACE (for new frames).
507 If FRAME is omitted or nil, use the selected frame.
508 Use `face-attribute' for finer control."
509 (let ((bold (face-attribute face :weight frame)))
510 (memq bold '(semi-bold bold extra-bold ultra-bold))))
511
512
513 (defun face-italic-p (face &optional frame)
514 "Return non-nil if the font of FACE is italic on FRAME.
515 If the optional argument FRAME is given, report on face FACE in that frame.
516 If FRAME is t, report on the defaults for face FACE (for new frames).
517 If FRAME is omitted or nil, use the selected frame.
518 Use `face-attribute' for finer control."
519 (let ((italic (face-attribute face :slant frame)))
520 (memq italic '(italic oblique))))
521
522
523 \f
524 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
525 ;;; Face documentation.
526 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
527
528 (defun face-documentation (face)
529 "Get the documentation string for FACE.
530 If FACE is a face-alias, get the documentation for the target face."
531 (let ((alias (get face 'face-alias))
532 doc)
533 (if alias
534 (progn
535 (setq doc (get alias 'face-documentation))
536 (format "%s is an alias for the face `%s'.%s" face alias
537 (if doc (format "\n%s" doc)
538 "")))
539 (get face 'face-documentation))))
540
541
542 (defun set-face-documentation (face string)
543 "Set the documentation string for FACE to STRING."
544 ;; Perhaps the text should go in DOC.
545 (put face 'face-documentation (purecopy string)))
546
547
548 (defalias 'face-doc-string 'face-documentation)
549 (defalias 'set-face-doc-string 'set-face-documentation)
550
551
552 \f
553 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
554 ;; Setting face attributes.
555 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
556
557
558 (defvar inhibit-face-set-after-frame-default nil
559 "If non-nil, that tells `face-set-after-frame-default' to do nothing.")
560
561 (defun set-face-attribute (face frame &rest args)
562 "Set attributes of FACE on FRAME from ARGS.
563
564 FRAME nil means change attributes on all frames. FRAME t means change
565 the default for new frames (this is done automatically each time an
566 attribute is changed on all frames).
567
568 ARGS must come in pairs ATTRIBUTE VALUE. ATTRIBUTE must be a valid
569 face attribute name. All attributes can be set to `unspecified';
570 this fact is not further mentioned below.
571
572 The following attributes are recognized:
573
574 `:family'
575
576 VALUE must be a string specifying the font family, e.g. ``courier'',
577 or a fontset alias name. If a font family is specified, wild-cards `*'
578 and `?' are allowed.
579
580 `:width'
581
582 VALUE specifies the relative proportionate width of the font to use.
583 It must be one of the symbols `ultra-condensed', `extra-condensed',
584 `condensed', `semi-condensed', `normal', `semi-expanded', `expanded',
585 `extra-expanded', or `ultra-expanded'.
586
587 `:height'
588
589 VALUE must be either an integer specifying the height of the font to use
590 in 1/10 pt, a floating point number specifying the amount by which to
591 scale any underlying face, or a function, which is called with the old
592 height (from the underlying face), and should return the new height.
593
594 `:weight'
595
596 VALUE specifies the weight of the font to use. It must be one of the
597 symbols `ultra-bold', `extra-bold', `bold', `semi-bold', `normal',
598 `semi-light', `light', `extra-light', `ultra-light'.
599
600 `:slant'
601
602 VALUE specifies the slant of the font to use. It must be one of the
603 symbols `italic', `oblique', `normal', `reverse-italic', or
604 `reverse-oblique'.
605
606 `:foreground', `:background'
607
608 VALUE must be a color name, a string.
609
610 `:underline'
611
612 VALUE specifies whether characters in FACE should be underlined. If
613 VALUE is t, underline with foreground color of the face. If VALUE is
614 a string, underline with that color. If VALUE is nil, explicitly
615 don't underline.
616
617 `:overline'
618
619 VALUE specifies whether characters in FACE should be overlined. If
620 VALUE is t, overline with foreground color of the face. If VALUE is a
621 string, overline with that color. If VALUE is nil, explicitly don't
622 overline.
623
624 `:strike-through'
625
626 VALUE specifies whether characters in FACE should be drawn with a line
627 striking through them. If VALUE is t, use the foreground color of the
628 face. If VALUE is a string, strike-through with that color. If VALUE
629 is nil, explicitly don't strike through.
630
631 `:box'
632
633 VALUE specifies whether characters in FACE should have a box drawn
634 around them. If VALUE is nil, explicitly don't draw boxes. If
635 VALUE is t, draw a box with lines of width 1 in the foreground color
636 of the face. If VALUE is a string, the string must be a color name,
637 and the box is drawn in that color with a line width of 1. Otherwise,
638 VALUE must be a property list of the form `(:line-width WIDTH
639 :color COLOR :style STYLE)'. If a keyword/value pair is missing from
640 the property list, a default value will be used for the value, as
641 specified below. WIDTH specifies the width of the lines to draw; it
642 defaults to 1. If WIDTH is negative, the absolute value is the width
643 of the lines, and draw top/bottom lines inside the characters area,
644 not around it. COLOR is the name of the color to draw in, default is
645 the foreground color of the face for simple boxes, and the background
646 color of the face for 3D boxes. STYLE specifies whether a 3D box
647 should be draw. If STYLE is `released-button', draw a box looking
648 like a released 3D button. If STYLE is `pressed-button' draw a box
649 that appears like a pressed button. If STYLE is nil, the default if
650 the property list doesn't contain a style specification, draw a 2D
651 box.
652
653 `:inverse-video'
654
655 VALUE specifies whether characters in FACE should be displayed in
656 inverse video. VALUE must be one of t or nil.
657
658 `:stipple'
659
660 If VALUE is a string, it must be the name of a file of pixmap data.
661 The directories listed in the `x-bitmap-file-path' variable are
662 searched. Alternatively, VALUE may be a list of the form (WIDTH
663 HEIGHT DATA) where WIDTH and HEIGHT are the size in pixels, and DATA
664 is a string containing the raw bits of the bitmap. VALUE nil means
665 explicitly don't use a stipple pattern.
666
667 For convenience, attributes `:family', `:width', `:height', `:weight',
668 and `:slant' may also be set in one step from an X font name:
669
670 `:font'
671
672 Set font-related face attributes from VALUE. VALUE must be a valid
673 XLFD font name. If it is a font name pattern, the first matching font
674 will be used.
675
676 For compatibility with Emacs 20, keywords `:bold' and `:italic' can
677 be used to specify that a bold or italic font should be used. VALUE
678 must be t or nil in that case. A value of `unspecified' is not allowed.
679
680 `:inherit'
681
682 VALUE is the name of a face from which to inherit attributes, or a list
683 of face names. Attributes from inherited faces are merged into the face
684 like an underlying face would be, with higher priority than underlying faces."
685 (let ((where (if (null frame) 0 frame)))
686 (setq args (purecopy args))
687 ;; If we set the new-frame defaults, this face is modified outside Custom.
688 (if (memq where '(0 t))
689 (put (or (get face 'face-alias) face) 'face-modified t))
690 (while args
691 ;; Don't recursively set the attributes from the frame's font param
692 ;; when we update the frame's font param fro the attributes.
693 (let ((inhibit-face-set-after-frame-default t))
694 (internal-set-lisp-face-attribute face (car args)
695 (purecopy (cadr args))
696 where))
697 (setq args (cdr (cdr args))))))
698
699
700 (defun make-face-bold (face &optional frame noerror)
701 "Make the font of FACE be bold, if possible.
702 FRAME nil or not specified means change face on all frames.
703 Argument NOERROR is ignored and retained for compatibility.
704 Use `set-face-attribute' for finer control of the font weight."
705 (interactive (list (read-face-name "Make which face bold")))
706 (set-face-attribute face frame :weight 'bold))
707
708
709 (defun make-face-unbold (face &optional frame noerror)
710 "Make the font of FACE be non-bold, if possible.
711 FRAME nil or not specified means change face on all frames.
712 Argument NOERROR is ignored and retained for compatibility."
713 (interactive (list (read-face-name "Make which face non-bold")))
714 (set-face-attribute face frame :weight 'normal))
715
716
717 (defun make-face-italic (face &optional frame noerror)
718 "Make the font of FACE be italic, if possible.
719 FRAME nil or not specified means change face on all frames.
720 Argument NOERROR is ignored and retained for compatibility.
721 Use `set-face-attribute' for finer control of the font slant."
722 (interactive (list (read-face-name "Make which face italic")))
723 (set-face-attribute face frame :slant 'italic))
724
725
726 (defun make-face-unitalic (face &optional frame noerror)
727 "Make the font of FACE be non-italic, if possible.
728 FRAME nil or not specified means change face on all frames.
729 Argument NOERROR is ignored and retained for compatibility."
730 (interactive (list (read-face-name "Make which face non-italic")))
731 (set-face-attribute face frame :slant 'normal))
732
733
734 (defun make-face-bold-italic (face &optional frame noerror)
735 "Make the font of FACE be bold and italic, if possible.
736 FRAME nil or not specified means change face on all frames.
737 Argument NOERROR is ignored and retained for compatibility.
738 Use `set-face-attribute' for finer control of font weight and slant."
739 (interactive (list (read-face-name "Make which face bold-italic")))
740 (set-face-attribute face frame :weight 'bold :slant 'italic))
741
742
743 (defun set-face-font (face font &optional frame)
744 "Change font-related attributes of FACE to those of FONT (a string).
745 FRAME nil or not specified means change face on all frames.
746 This sets the attributes `:family', `:width', `:height', `:weight',
747 and `:slant'. When called interactively, prompt for the face and font."
748 (interactive (read-face-and-attribute :font))
749 (set-face-attribute face frame :font font))
750
751
752 ;; Implementation note: Emulating gray background colors with a
753 ;; stipple pattern is now part of the face realization process, and is
754 ;; done in C depending on the frame on which the face is realized.
755
756 (defun set-face-background (face color &optional frame)
757 "Change the background color of face FACE to COLOR (a string).
758 FRAME nil or not specified means change face on all frames.
759 COLOR can be a system-defined color name (see `list-colors-display')
760 or a hex spec of the form #RRGGBB.
761 When called interactively, prompts for the face and color."
762 (interactive (read-face-and-attribute :background))
763 (set-face-attribute face frame :background (or color 'unspecified)))
764
765
766 (defun set-face-foreground (face color &optional frame)
767 "Change the foreground color of face FACE to COLOR (a string).
768 FRAME nil or not specified means change face on all frames.
769 COLOR can be a system-defined color name (see `list-colors-display')
770 or a hex spec of the form #RRGGBB.
771 When called interactively, prompts for the face and color."
772 (interactive (read-face-and-attribute :foreground))
773 (set-face-attribute face frame :foreground (or color 'unspecified)))
774
775
776 (defun set-face-stipple (face stipple &optional frame)
777 "Change the stipple pixmap of face FACE to STIPPLE.
778 FRAME nil or not specified means change face on all frames.
779 STIPPLE should be a string, the name of a file of pixmap data.
780 The directories listed in the `x-bitmap-file-path' variable are searched.
781
782 Alternatively, STIPPLE may be a list of the form (WIDTH HEIGHT DATA)
783 where WIDTH and HEIGHT are the size in pixels,
784 and DATA is a string, containing the raw bits of the bitmap."
785 (interactive (read-face-and-attribute :stipple))
786 (set-face-attribute face frame :stipple (or stipple 'unspecified)))
787
788
789 (defun set-face-underline-p (face underline &optional frame)
790 "Specify whether face FACE is underlined.
791 UNDERLINE nil means FACE explicitly doesn't underline.
792 UNDERLINE non-nil means FACE explicitly does underlining
793 with the same of the foreground color.
794 If UNDERLINE is a string, underline with the color named UNDERLINE.
795 FRAME nil or not specified means change face on all frames.
796 Use `set-face-attribute' to ``unspecify'' underlining."
797 (interactive
798 (let ((list (read-face-and-attribute :underline)))
799 (list (car list) (eq (car (cdr list)) t))))
800 (set-face-attribute face frame :underline underline))
801
802 (define-obsolete-function-alias 'set-face-underline
803 'set-face-underline-p "22.1")
804
805
806 (defun set-face-inverse-video-p (face inverse-video-p &optional frame)
807 "Specify whether face FACE is in inverse video.
808 INVERSE-VIDEO-P non-nil means FACE displays explicitly in inverse video.
809 INVERSE-VIDEO-P nil means FACE explicitly is not in inverse video.
810 FRAME nil or not specified means change face on all frames.
811 Use `set-face-attribute' to ``unspecify'' the inverse video attribute."
812 (interactive
813 (let ((list (read-face-and-attribute :inverse-video)))
814 (list (car list) (eq (car (cdr list)) t))))
815 (set-face-attribute face frame :inverse-video inverse-video-p))
816
817
818 (defun set-face-bold-p (face bold-p &optional frame)
819 "Specify whether face FACE is bold.
820 BOLD-P non-nil means FACE should explicitly display bold.
821 BOLD-P nil means FACE should explicitly display non-bold.
822 FRAME nil or not specified means change face on all frames.
823 Use `set-face-attribute' or `modify-face' for finer control."
824 (if (null bold-p)
825 (make-face-unbold face frame)
826 (make-face-bold face frame)))
827
828
829 (defun set-face-italic-p (face italic-p &optional frame)
830 "Specify whether face FACE is italic.
831 ITALIC-P non-nil means FACE should explicitly display italic.
832 ITALIC-P nil means FACE should explicitly display non-italic.
833 FRAME nil or not specified means change face on all frames.
834 Use `set-face-attribute' or `modify-face' for finer control."
835 (if (null italic-p)
836 (make-face-unitalic face frame)
837 (make-face-italic face frame)))
838
839
840 (defalias 'set-face-background-pixmap 'set-face-stipple)
841
842
843 (defun invert-face (face &optional frame)
844 "Swap the foreground and background colors of FACE.
845 If FRAME is omitted or nil, it means change face on all frames.
846 If FACE specifies neither foreground nor background color,
847 set its foreground and background to the background and foreground
848 of the default face. Value is FACE."
849 (interactive (list (read-face-name "Invert face")))
850 (let ((fg (face-attribute face :foreground frame))
851 (bg (face-attribute face :background frame)))
852 (if (not (and (eq fg 'unspecified) (eq bg 'unspecified)))
853 (set-face-attribute face frame :foreground bg :background fg)
854 (set-face-attribute face frame
855 :foreground
856 (face-attribute 'default :background frame)
857 :background
858 (face-attribute 'default :foreground frame))))
859 face)
860
861 \f
862 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
863 ;;; Interactively modifying faces.
864 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
865
866 (defun read-face-name (prompt &optional string-describing-default multiple)
867 "Read a face, defaulting to the face or faces on the char after point.
868 If it has the property `read-face-name', that overrides the `face' property.
869 PROMPT should be a string that describes what the caller will do with the face;
870 it should not end in a space.
871 STRING-DESCRIBING-DEFAULT should describe what default the caller will use if
872 the user just types RET; you can omit it.
873 If MULTIPLE is non-nil, return a list of faces (possibly only one).
874 Otherwise, return a single face."
875 (let ((faceprop (or (get-char-property (point) 'read-face-name)
876 (get-char-property (point) 'face)))
877 (aliasfaces nil)
878 (nonaliasfaces nil)
879 faces)
880 ;; Try to get a face name from the buffer.
881 (if (memq (intern-soft (thing-at-point 'symbol)) (face-list))
882 (setq faces (list (intern-soft (thing-at-point 'symbol)))))
883 ;; Add the named faces that the `face' property uses.
884 (if (and (listp faceprop)
885 ;; Don't treat an attribute spec as a list of faces.
886 (not (keywordp (car faceprop)))
887 (not (memq (car faceprop) '(foreground-color background-color))))
888 (dolist (f faceprop)
889 (if (symbolp f)
890 (push f faces)))
891 (if (symbolp faceprop)
892 (push faceprop faces)))
893 (delete-dups faces)
894
895 ;; Build up the completion tables.
896 (mapatoms (lambda (s)
897 (if (custom-facep s)
898 (if (get s 'face-alias)
899 (push (symbol-name s) aliasfaces)
900 (push (symbol-name s) nonaliasfaces)))))
901
902 ;; If we only want one, and the default is more than one,
903 ;; discard the unwanted ones now.
904 (unless multiple
905 (if faces
906 (setq faces (list (car faces)))))
907 (require 'crm)
908 (let* ((input
909 ;; Read the input.
910 (completing-read-multiple
911 (if (or faces string-describing-default)
912 (format "%s (default %s): " prompt
913 (if faces (mapconcat 'symbol-name faces ",")
914 string-describing-default))
915 (format "%s: " prompt))
916 (completion-table-in-turn nonaliasfaces aliasfaces)
917 nil t nil nil
918 (if faces (mapconcat 'symbol-name faces ","))))
919 ;; Canonicalize the output.
920 (output
921 (cond ((or (equal input "") (equal input '("")))
922 faces)
923 ((stringp input)
924 (mapcar 'intern (split-string input ", *" t)))
925 ((listp input)
926 (mapcar 'intern input))
927 (input))))
928 ;; Return either a list of faces or just one face.
929 (if multiple
930 output
931 (car output)))))
932
933
934 (defun face-valid-attribute-values (attribute &optional frame)
935 "Return valid values for face attribute ATTRIBUTE.
936 The optional argument FRAME is used to determine available fonts
937 and colors. If it is nil or not specified, the selected frame is
938 used. Value is an alist of (NAME . VALUE) if ATTRIBUTE expects a value
939 out of a set of discrete values. Value is `integerp' if ATTRIBUTE expects
940 an integer value."
941 (let ((valid
942 (case attribute
943 (:family
944 (if (window-system frame)
945 (mapcar #'(lambda (x) (cons (car x) (car x)))
946 (font-family-list))
947 ;; Only one font on TTYs.
948 (list (cons "default" "default"))))
949 (:width
950 (mapcar #'(lambda (x) (cons (symbol-name (car x)) (car x)))
951 font-width-table))
952 (:weight
953 (mapcar #'(lambda (x) (cons (symbol-name (car x)) (car x)))
954 font-weight-table))
955 (:slant
956 (mapcar #'(lambda (x) (cons (symbol-name (car x)) (car x)))
957 font-slant-table))
958 (:inverse-video
959 (mapcar #'(lambda (x) (cons (symbol-name x) x))
960 (internal-lisp-face-attribute-values attribute)))
961 ((:underline :overline :strike-through :box)
962 (if (window-system frame)
963 (nconc (mapcar #'(lambda (x) (cons (symbol-name x) x))
964 (internal-lisp-face-attribute-values attribute))
965 (mapcar #'(lambda (c) (cons c c))
966 (defined-colors frame)))
967 (mapcar #'(lambda (x) (cons (symbol-name x) x))
968 (internal-lisp-face-attribute-values attribute))))
969 ((:foreground :background)
970 (mapcar #'(lambda (c) (cons c c))
971 (defined-colors frame)))
972 ((:height)
973 'integerp)
974 (:stipple
975 (and (memq (window-system frame) '(x w32 mac))
976 (mapcar #'list
977 (apply #'nconc
978 (mapcar (lambda (dir)
979 (and (file-readable-p dir)
980 (file-directory-p dir)
981 (directory-files dir)))
982 x-bitmap-file-path)))))
983 (:inherit
984 (cons '("none" . nil)
985 (mapcar #'(lambda (c) (cons (symbol-name c) c))
986 (face-list))))
987 (t
988 (error "Internal error")))))
989 (if (and (listp valid) (not (memq attribute '(:inherit))))
990 (nconc (list (cons "unspecified" 'unspecified)) valid)
991 valid)))
992
993
994 (defvar face-attribute-name-alist
995 '((:family . "font family")
996 (:width . "character set width")
997 (:height . "height in 1/10 pt")
998 (:weight . "weight")
999 (:slant . "slant")
1000 (:underline . "underline")
1001 (:overline . "overline")
1002 (:strike-through . "strike-through")
1003 (:box . "box")
1004 (:inverse-video . "inverse-video display")
1005 (:foreground . "foreground color")
1006 (:background . "background color")
1007 (:stipple . "background stipple")
1008 (:inherit . "inheritance"))
1009 "An alist of descriptive names for face attributes.
1010 Each element has the form (ATTRIBUTE-NAME . DESCRIPTION) where
1011 ATTRIBUTE-NAME is a face attribute name (a keyword symbol), and
1012 DESCRIPTION is a descriptive name for ATTRIBUTE-NAME.")
1013
1014
1015 (defun face-descriptive-attribute-name (attribute)
1016 "Return a descriptive name for ATTRIBUTE."
1017 (cdr (assq attribute face-attribute-name-alist)))
1018
1019
1020 (defun face-read-string (face default name &optional completion-alist)
1021 "Interactively read a face attribute string value.
1022 FACE is the face whose attribute is read. If non-nil, DEFAULT is the
1023 default string to return if no new value is entered. NAME is a
1024 descriptive name of the attribute for prompting. COMPLETION-ALIST is an
1025 alist of valid values, if non-nil.
1026
1027 Entering nothing accepts the default string DEFAULT.
1028 Value is the new attribute value."
1029 ;; Capitalize NAME (we don't use `capitalize' because that capitalizes
1030 ;; each word in a string separately).
1031 (setq name (concat (upcase (substring name 0 1)) (substring name 1)))
1032 (let* ((completion-ignore-case t)
1033 (value (completing-read
1034 (if default
1035 (format "%s for face `%s' (default %s): "
1036 name face default)
1037 (format "%s for face `%s': " name face))
1038 completion-alist nil nil nil nil default)))
1039 (if (equal value "") default value)))
1040
1041
1042 (defun face-read-integer (face default name)
1043 "Interactively read an integer face attribute value.
1044 FACE is the face whose attribute is read. DEFAULT is the default
1045 value to return if no new value is entered. NAME is a descriptive
1046 name of the attribute for prompting. Value is the new attribute value."
1047 (let ((new-value
1048 (face-read-string face
1049 (format "%s" default)
1050 name
1051 (list (cons "unspecified" 'unspecified)))))
1052 (cond ((equal new-value "unspecified")
1053 'unspecified)
1054 ((member new-value '("unspecified-fg" "unspecified-bg"))
1055 new-value)
1056 (t
1057 (string-to-number new-value)))))
1058
1059
1060 (defun read-face-attribute (face attribute &optional frame)
1061 "Interactively read a new value for FACE's ATTRIBUTE.
1062 Optional argument FRAME nil or unspecified means read an attribute value
1063 of a global face. Value is the new attribute value."
1064 (let* ((old-value (face-attribute face attribute frame))
1065 (attribute-name (face-descriptive-attribute-name attribute))
1066 (valid (face-valid-attribute-values attribute frame))
1067 new-value)
1068 ;; Represent complex attribute values as strings by printing them
1069 ;; out. Stipple can be a vector; (WIDTH HEIGHT DATA). Box can be
1070 ;; a list `(:width WIDTH :color COLOR)' or `(:width WIDTH :shadow
1071 ;; SHADOW)'.
1072 (when (and (or (eq attribute :stipple)
1073 (eq attribute :box))
1074 (or (consp old-value)
1075 (vectorp old-value)))
1076 (setq old-value (prin1-to-string old-value)))
1077 (cond ((listp valid)
1078 (let ((default
1079 (or (car (rassoc old-value valid))
1080 (format "%s" old-value))))
1081 (setq new-value
1082 (face-read-string face default attribute-name valid))
1083 (if (equal new-value default)
1084 ;; Nothing changed, so don't bother with all the stuff
1085 ;; below. In particular, this avoids a non-tty color
1086 ;; from being canonicalized for a tty when the user
1087 ;; just uses the default.
1088 (setq new-value old-value)
1089 ;; Terminal frames can support colors that don't appear
1090 ;; explicitly in VALID, using color approximation code
1091 ;; in tty-colors.el.
1092 (when (and (memq attribute '(:foreground :background))
1093 (not (memq (window-system frame) '(x w32 mac)))
1094 (not (member new-value
1095 '("unspecified"
1096 "unspecified-fg" "unspecified-bg"))))
1097 (setq new-value (car (tty-color-desc new-value frame))))
1098 (when (assoc new-value valid)
1099 (setq new-value (cdr (assoc new-value valid)))))))
1100 ((eq valid 'integerp)
1101 (setq new-value (face-read-integer face old-value attribute-name)))
1102 (t (error "Internal error")))
1103 ;; Convert stipple and box value text we read back to a list or
1104 ;; vector if it looks like one. This makes the assumption that a
1105 ;; pixmap file name won't start with an open-paren.
1106 (when (and (or (eq attribute :stipple)
1107 (eq attribute :box))
1108 (stringp new-value)
1109 (string-match "^[[(]" new-value))
1110 (setq new-value (read new-value)))
1111 new-value))
1112
1113
1114 (defun read-face-font (face &optional frame)
1115 "Read the name of a font for FACE on FRAME.
1116 If optional argument FRAME is nil or omitted, use the selected frame."
1117 (let ((completion-ignore-case t))
1118 (completing-read (format "Set font attributes of face `%s' from font: " face)
1119 (append (fontset-list) (x-list-fonts "*" nil frame)))))
1120
1121
1122 (defun read-all-face-attributes (face &optional frame)
1123 "Interactively read all attributes for FACE.
1124 If optional argument FRAME is nil or omitted, use the selected frame.
1125 Value is a property list of attribute names and new values."
1126 (let (result)
1127 (dolist (attribute face-attribute-name-alist result)
1128 (setq result (cons (car attribute)
1129 (cons (read-face-attribute face (car attribute) frame)
1130 result))))))
1131
1132 (defun modify-face (&optional face foreground background stipple
1133 bold-p italic-p underline inverse-p frame)
1134 "Modify attributes of faces interactively.
1135 If optional argument FRAME is nil or omitted, modify the face used
1136 for newly created frame, i.e. the global face.
1137 For non-interactive use, `set-face-attribute' is preferred.
1138 When called from Lisp, if FACE is nil, all arguments but FRAME are ignored
1139 and the face and its settings are obtained by querying the user."
1140 (interactive)
1141 (if face
1142 (set-face-attribute face frame
1143 :foreground (or foreground 'unspecified)
1144 :background (or background 'unspecified)
1145 :stipple stipple
1146 :bold bold-p
1147 :italic italic-p
1148 :underline underline
1149 :inverse-video inverse-p)
1150 (setq face (read-face-name "Modify face"))
1151 (apply #'set-face-attribute face frame
1152 (read-all-face-attributes face frame))))
1153
1154 (defun read-face-and-attribute (attribute &optional frame)
1155 "Read face name and face attribute value.
1156 ATTRIBUTE is the attribute whose new value is read.
1157 FRAME nil or unspecified means read attribute value of global face.
1158 Value is a list (FACE NEW-VALUE) where FACE is the face read
1159 \(a symbol), and NEW-VALUE is value read."
1160 (cond ((eq attribute :font)
1161 (let* ((prompt "Set font-related attributes of face")
1162 (face (read-face-name prompt))
1163 (font (read-face-font face frame)))
1164 (list face font)))
1165 (t
1166 (let* ((attribute-name (face-descriptive-attribute-name attribute))
1167 (prompt (format "Set %s of face" attribute-name))
1168 (face (read-face-name prompt))
1169 (new-value (read-face-attribute face attribute frame)))
1170 (list face new-value)))))
1171
1172
1173 \f
1174 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1175 ;;; Listing faces.
1176 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1177
1178 (defvar list-faces-sample-text
1179 "abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1180 "*Text string to display as the sample text for `list-faces-display'.")
1181
1182
1183 ;; The name list-faces would be more consistent, but let's avoid a
1184 ;; conflict with Lucid, which uses that name differently.
1185
1186 (defvar help-xref-stack)
1187 (defun list-faces-display (&optional regexp)
1188 "List all faces, using the same sample text in each.
1189 The sample text is a string that comes from the variable
1190 `list-faces-sample-text'.
1191
1192 If REGEXP is non-nil, list only those faces with names matching
1193 this regular expression. When called interactively with a prefix
1194 arg, prompt for a regular expression."
1195 (interactive (list (and current-prefix-arg
1196 (read-string "List faces matching regexp: "))))
1197 (let ((all-faces (zerop (length regexp)))
1198 (frame (selected-frame))
1199 (max-length 0)
1200 faces line-format
1201 disp-frame window face-name)
1202 ;; We filter and take the max length in one pass
1203 (setq faces
1204 (delq nil
1205 (mapcar (lambda (f)
1206 (let ((s (symbol-name f)))
1207 (when (or all-faces (string-match regexp s))
1208 (setq max-length (max (length s) max-length))
1209 f)))
1210 (sort (face-list) #'string-lessp))))
1211 (unless faces
1212 (error "No faces matching \"%s\"" regexp))
1213 (setq max-length (1+ max-length)
1214 line-format (format "%%-%ds" max-length))
1215 (with-help-window "*Faces*"
1216 (save-excursion
1217 (set-buffer standard-output)
1218 (setq truncate-lines t)
1219 (insert
1220 (substitute-command-keys
1221 (concat
1222 "Use "
1223 (if (display-mouse-p) "\\[help-follow-mouse] or ")
1224 "\\[help-follow] on a face name to customize it\n"
1225 "or on its sample text for a description of the face.\n\n")))
1226 (setq help-xref-stack nil)
1227 (dolist (face faces)
1228 (setq face-name (symbol-name face))
1229 (insert (format line-format face-name))
1230 ;; Hyperlink to a customization buffer for the face. Using
1231 ;; the help xref mechanism may not be the best way.
1232 (save-excursion
1233 (save-match-data
1234 (search-backward face-name)
1235 (setq help-xref-stack-item `(list-faces-display ,regexp))
1236 (help-xref-button 0 'help-customize-face face)))
1237 (let ((beg (point))
1238 (line-beg (line-beginning-position)))
1239 (insert list-faces-sample-text)
1240 ;; Hyperlink to a help buffer for the face.
1241 (save-excursion
1242 (save-match-data
1243 (search-backward list-faces-sample-text)
1244 (help-xref-button 0 'help-face face)))
1245 (insert "\n")
1246 (put-text-property beg (1- (point)) 'face face)
1247 ;; Make all face commands default to the proper face
1248 ;; anywhere in the line.
1249 (put-text-property line-beg (1- (point)) 'read-face-name face)
1250 ;; If the sample text has multiple lines, line up all of them.
1251 (goto-char beg)
1252 (forward-line 1)
1253 (while (not (eobp))
1254 (insert-char ?\s max-length)
1255 (forward-line 1))))
1256 (goto-char (point-min))))
1257 ;; If the *Faces* buffer appears in a different frame,
1258 ;; copy all the face definitions from FRAME,
1259 ;; so that the display will reflect the frame that was selected.
1260 (setq window (get-buffer-window (get-buffer "*Faces*") t))
1261 (setq disp-frame (if window (window-frame window)
1262 (car (frame-list))))
1263 (or (eq frame disp-frame)
1264 (let ((faces (face-list)))
1265 (while faces
1266 (copy-face (car faces) (car faces) frame disp-frame)
1267 (setq faces (cdr faces)))))))
1268
1269
1270 (defun describe-face (face &optional frame)
1271 "Display the properties of face FACE on FRAME.
1272 Interactively, FACE defaults to the faces of the character after point
1273 and FRAME defaults to the selected frame.
1274
1275 If the optional argument FRAME is given, report on face FACE in that frame.
1276 If FRAME is t, report on the defaults for face FACE (for new frames).
1277 If FRAME is omitted or nil, use the selected frame."
1278 (interactive (list (read-face-name "Describe face" "= `default' face" t)))
1279 (let* ((attrs '((:family . "Family")
1280 (:width . "Width")
1281 (:height . "Height")
1282 (:weight . "Weight")
1283 (:slant . "Slant")
1284 (:foreground . "Foreground")
1285 (:background . "Background")
1286 (:underline . "Underline")
1287 (:overline . "Overline")
1288 (:strike-through . "Strike-through")
1289 (:box . "Box")
1290 (:inverse-video . "Inverse")
1291 (:stipple . "Stipple")
1292 (:font . "Font")
1293 (:fontset . "Fontset")
1294 (:inherit . "Inherit")))
1295 (max-width (apply #'max (mapcar #'(lambda (x) (length (cdr x)))
1296 attrs))))
1297 (help-setup-xref (list #'describe-face face) (interactive-p))
1298 (unless face
1299 (setq face 'default))
1300 (if (not (listp face))
1301 (setq face (list face)))
1302 (with-help-window (help-buffer)
1303 (save-excursion
1304 (set-buffer standard-output)
1305 (dolist (f face)
1306 (if (stringp f) (setq f (intern f)))
1307 (insert "Face: " (symbol-name f))
1308 (if (not (facep f))
1309 (insert " undefined face.\n")
1310 (let ((customize-label "customize this face")
1311 file-name)
1312 (insert (concat " (" (propertize "sample" 'font-lock-face f) ")"))
1313 (princ (concat " (" customize-label ")\n"))
1314 (insert "Documentation: "
1315 (or (face-documentation f)
1316 "Not documented as a face.")
1317 "\n")
1318 (with-current-buffer standard-output
1319 (save-excursion
1320 (re-search-backward
1321 (concat "\\(" customize-label "\\)") nil t)
1322 (help-xref-button 1 'help-customize-face f)))
1323 ;; The next 4 sexps are copied from describe-function-1
1324 ;; and simplified.
1325 (setq file-name (symbol-file f 'defface))
1326 (setq file-name (describe-simplify-lib-file-name file-name))
1327 (when file-name
1328 (princ "Defined in `")
1329 (princ file-name)
1330 (princ "'")
1331 ;; Make a hyperlink to the library.
1332 (save-excursion
1333 (re-search-backward "`\\([^`']+\\)'" nil t)
1334 (help-xref-button 1 'help-face-def f file-name))
1335 (princ ".")
1336 (terpri)
1337 (terpri))
1338 (dolist (a attrs)
1339 (let ((attr (face-attribute f (car a) frame)))
1340 (insert (make-string (- max-width (length (cdr a))) ?\s)
1341 (cdr a) ": " (format "%s" attr))
1342 (if (and (eq (car a) :inherit)
1343 (not (eq attr 'unspecified)))
1344 ;; Make a hyperlink to the parent face.
1345 (save-excursion
1346 (re-search-backward ": \\([^:]+\\)" nil t)
1347 (help-xref-button 1 'help-face attr)))
1348 (insert "\n")))))
1349 (terpri))))))
1350
1351 \f
1352 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1353 ;;; Face specifications (defface).
1354 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1355
1356 ;; Parameter FRAME Is kept for call compatibility to with previous
1357 ;; face implementation.
1358
1359 (defun face-attr-construct (face &optional frame)
1360 "Return a `defface'-style attribute list for FACE on FRAME.
1361 Value is a property list of pairs ATTRIBUTE VALUE for all specified
1362 face attributes of FACE where ATTRIBUTE is the attribute name and
1363 VALUE is the specified value of that attribute."
1364 (let (result)
1365 (dolist (entry face-attribute-name-alist result)
1366 (let* ((attribute (car entry))
1367 (value (face-attribute face attribute)))
1368 (unless (eq value 'unspecified)
1369 (setq result (nconc (list attribute value) result)))))))
1370
1371
1372 (defun face-spec-set-match-display (display frame)
1373 "Non-nil if DISPLAY matches FRAME.
1374 DISPLAY is part of a spec such as can be used in `defface'.
1375 If FRAME is nil, the current FRAME is used."
1376 (let* ((conjuncts display)
1377 conjunct req options
1378 ;; t means we have succeeded against all the conjuncts in
1379 ;; DISPLAY that have been tested so far.
1380 (match t))
1381 (if (eq conjuncts t)
1382 (setq conjuncts nil))
1383 (while (and conjuncts match)
1384 (setq conjunct (car conjuncts)
1385 conjuncts (cdr conjuncts)
1386 req (car conjunct)
1387 options (cdr conjunct)
1388 match (cond ((eq req 'type)
1389 (or (memq (window-system frame) options)
1390 ;; FIXME: This should be revisited to use
1391 ;; display-graphic-p, provided that the
1392 ;; color selection depends on the number
1393 ;; of supported colors, and all defface's
1394 ;; are changed to look at number of colors
1395 ;; instead of (type graphic) etc.
1396 (and (null (window-system frame))
1397 (memq 'tty options))
1398 (and (memq 'motif options)
1399 (featurep 'motif))
1400 (and (memq 'gtk options)
1401 (featurep 'gtk))
1402 (and (memq 'lucid options)
1403 (featurep 'x-toolkit)
1404 (not (featurep 'motif))
1405 (not (featurep 'gtk)))
1406 (and (memq 'x-toolkit options)
1407 (featurep 'x-toolkit))))
1408 ((eq req 'min-colors)
1409 (>= (display-color-cells frame) (car options)))
1410 ((eq req 'class)
1411 (memq (frame-parameter frame 'display-type) options))
1412 ((eq req 'background)
1413 (memq (frame-parameter frame 'background-mode)
1414 options))
1415 ((eq req 'supports)
1416 (display-supports-face-attributes-p options frame))
1417 (t (error "Unknown req `%S' with options `%S'"
1418 req options)))))
1419 match))
1420
1421
1422 (defun face-spec-choose (spec &optional frame)
1423 "Choose the proper attributes for FRAME, out of SPEC.
1424 If SPEC is nil, return nil."
1425 (unless frame
1426 (setq frame (selected-frame)))
1427 (let ((tail spec)
1428 result defaults)
1429 (while tail
1430 (let* ((entry (pop tail))
1431 (display (car entry))
1432 (attrs (cdr entry))
1433 thisval)
1434 ;; Get the attributes as actually specified by this alternative.
1435 (setq thisval
1436 (if (null (cdr attrs)) ;; was (listp (car attrs))
1437 ;; Old-style entry, the attribute list is the
1438 ;; first element.
1439 (car attrs)
1440 attrs))
1441
1442 ;; If the condition is `default', that sets the default
1443 ;; for following conditions.
1444 (if (eq display 'default)
1445 (setq defaults thisval)
1446 ;; Otherwise, if it matches, use it.
1447 (when (face-spec-set-match-display display frame)
1448 (setq result thisval)
1449 (setq tail nil)))))
1450 (if defaults (append result defaults) result)))
1451
1452
1453 (defun face-spec-reset-face (face &optional frame)
1454 "Reset all attributes of FACE on FRAME to unspecified."
1455 (let ((attrs face-attribute-name-alist))
1456 (while attrs
1457 (let ((attr-and-name (car attrs)))
1458 (set-face-attribute face frame (car attr-and-name) 'unspecified))
1459 (setq attrs (cdr attrs)))))
1460
1461
1462 (defun face-spec-set (face spec &optional for-defface)
1463 "Set FACE's face spec, which controls its appearance, to SPEC.
1464 If FOR-DEFFACE is t, set the base spec, the one that `defface'
1465 and Custom set. (In that case, the caller must put it in the
1466 appropriate property, because that depends on the caller.)
1467 If FOR-DEFFACE is nil, set the overriding spec (and store it
1468 in the `face-override-spec' property of FACE).
1469
1470 The appearance of FACE is controlled by the base spec,
1471 by any custom theme specs on top of that, and by the
1472 overriding spec on top of all the rest.
1473
1474 FOR-DEFFACE can also be a frame, in which case we set the
1475 frame-specific attributes of FACE for that frame based on SPEC.
1476 That usage is deprecated.
1477
1478 See `defface' for information about the format and meaning of SPEC."
1479 (if (framep for-defface)
1480 ;; Handle the deprecated case where third arg is a frame.
1481 (face-spec-set-2 face for-defface spec)
1482 (if for-defface
1483 ;; When we reset the face based on its custom spec, then it is
1484 ;; unmodified as far as Custom is concerned.
1485 (put (or (get face 'face-alias) face) 'face-modified nil)
1486 ;; When we change a face based on a spec from outside custom,
1487 ;; record it for future frames.
1488 (put (or (get face 'face-alias) face) 'face-override-spec spec))
1489 ;;; RMS 29 dec 2007: Perhaps this code should be reinstated.
1490 ;;; That depends on whether the overriding spec
1491 ;;; or the default face attributes
1492 ;;; should take priority.
1493 ;;; ;; Clear all the new-frame default attributes for this face.
1494 ;;; ;; face-spec-reset-face won't do it right.
1495 ;;; (let ((facevec (cdr (assq face face-new-frame-defaults))))
1496 ;;; (dotimes (i (length facevec))
1497 ;;; (unless (= i 0)
1498 ;;; (aset facevec i 'unspecified))))
1499 ;; Reset each frame according to the rules implied by all its specs.
1500 (dolist (frame (frame-list))
1501 (face-spec-recalc face frame))))
1502
1503 (defun face-spec-recalc (face frame)
1504 "Reset the face attributes of FACE on FRAME according to its specs.
1505 This applies the defface/custom spec first, then the custom theme specs,
1506 then the override spec."
1507 (face-spec-reset-face face frame)
1508 (let ((face-sym (or (get face 'face-alias) face)))
1509 (face-spec-set-2 face frame
1510 (face-user-default-spec face))
1511 (let ((theme-faces (reverse (get face-sym 'theme-face))))
1512 (dolist (spec theme-faces)
1513 (face-spec-set-2 face frame (cadr spec))))
1514 (face-spec-set-2 face frame (get face-sym 'face-override-spec))))
1515
1516 (defun face-spec-set-2 (face frame spec)
1517 "Set the face attributes of FACE on FRAME according to SPEC."
1518 (let* ((attrs (face-spec-choose spec frame)))
1519 (while attrs
1520 (let ((attribute (car attrs))
1521 (value (car (cdr attrs))))
1522 ;; Support some old-style attribute names and values.
1523 (case attribute
1524 (:bold (setq attribute :weight value (if value 'bold 'normal)))
1525 (:italic (setq attribute :slant value (if value 'italic 'normal)))
1526 ((:foreground :background)
1527 ;; Compatibility with 20.x. Some bogus face specs seem to
1528 ;; exist containing things like `:foreground nil'.
1529 (if (null value) (setq value 'unspecified)))
1530 (t (unless (assq attribute face-x-resources)
1531 (setq attribute nil))))
1532 (when attribute
1533 (set-face-attribute face frame attribute value)))
1534 (setq attrs (cdr (cdr attrs))))))
1535
1536 (defun face-attr-match-p (face attrs &optional frame)
1537 "Return t if attributes of FACE match values in plist ATTRS.
1538 Optional parameter FRAME is the frame whose definition of FACE
1539 is used. If nil or omitted, use the selected frame."
1540 (unless frame
1541 (setq frame (selected-frame)))
1542 (let ((list face-attribute-name-alist)
1543 (match t))
1544 (while (and match (not (null list)))
1545 (let* ((attr (car (car list)))
1546 (specified-value
1547 (if (plist-member attrs attr)
1548 (plist-get attrs attr)
1549 'unspecified))
1550 (value-now (face-attribute face attr frame)))
1551 (setq match (equal specified-value value-now))
1552 (setq list (cdr list))))
1553 match))
1554
1555 (defun face-spec-match-p (face spec &optional frame)
1556 "Return t if FACE, on FRAME, matches what SPEC says it should look like."
1557 (face-attr-match-p face (face-spec-choose spec frame) frame))
1558
1559 (defsubst face-default-spec (face)
1560 "Return the default face-spec for FACE, ignoring any user customization.
1561 If there is no default for FACE, return nil."
1562 (get face 'face-defface-spec))
1563
1564 (defsubst face-user-default-spec (face)
1565 "Return the user's customized face-spec for FACE, or the default if none.
1566 If there is neither a user setting nor a default for FACE, return nil."
1567 (or (get face 'customized-face)
1568 (get face 'saved-face)
1569 (face-default-spec face)))
1570
1571 \f
1572 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1573 ;;; Frame-type independent color support.
1574 ;;; We keep the old x-* names as aliases for back-compatibility.
1575 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1576
1577 (defun defined-colors (&optional frame)
1578 "Return a list of colors supported for a particular frame.
1579 The argument FRAME specifies which frame to try.
1580 The value may be different for frames on different display types.
1581 If FRAME doesn't support colors, the value is nil.
1582 If FRAME is nil, that stands for the selected frame."
1583 (if (memq (framep (or frame (selected-frame))) '(x w32 mac))
1584 (xw-defined-colors frame)
1585 (mapcar 'car (tty-color-alist frame))))
1586 (defalias 'x-defined-colors 'defined-colors)
1587
1588 (defun color-defined-p (color &optional frame)
1589 "Return non-nil if color COLOR is supported on frame FRAME.
1590 If FRAME is omitted or nil, use the selected frame.
1591 If COLOR is the symbol `unspecified' or one of the strings
1592 \"unspecified-fg\" or \"unspecified-bg\", the value is nil."
1593 (if (member color '(unspecified "unspecified-bg" "unspecified-fg"))
1594 nil
1595 (if (member (framep (or frame (selected-frame))) '(x w32 mac))
1596 (xw-color-defined-p color frame)
1597 (numberp (tty-color-translate color frame)))))
1598 (defalias 'x-color-defined-p 'color-defined-p)
1599
1600 (defun color-values (color &optional frame)
1601 "Return a description of the color named COLOR on frame FRAME.
1602 The value is a list of integer RGB values--(RED GREEN BLUE).
1603 These values appear to range from 0 to 65280 or 65535, depending
1604 on the system; white is \(65280 65280 65280\) or \(65535 65535 65535\).
1605 If FRAME is omitted or nil, use the selected frame.
1606 If FRAME cannot display COLOR, the value is nil.
1607 If COLOR is the symbol `unspecified' or one of the strings
1608 \"unspecified-fg\" or \"unspecified-bg\", the value is nil."
1609 (if (member color '(unspecified "unspecified-fg" "unspecified-bg"))
1610 nil
1611 (if (memq (framep (or frame (selected-frame))) '(x w32 mac))
1612 (xw-color-values color frame)
1613 (tty-color-values color frame))))
1614 (defalias 'x-color-values 'color-values)
1615
1616 (defun display-color-p (&optional display)
1617 "Return t if DISPLAY supports color.
1618 The optional argument DISPLAY specifies which display to ask about.
1619 DISPLAY should be either a frame or a display name (a string).
1620 If omitted or nil, that stands for the selected frame's display."
1621 (if (memq (framep-on-display display) '(x w32 mac))
1622 (xw-display-color-p display)
1623 (tty-display-color-p display)))
1624 (defalias 'x-display-color-p 'display-color-p)
1625
1626 (defun display-grayscale-p (&optional display)
1627 "Return non-nil if frames on DISPLAY can display shades of gray."
1628 (let ((frame-type (framep-on-display display)))
1629 (cond
1630 ((memq frame-type '(x w32 mac))
1631 (x-display-grayscale-p display))
1632 (t
1633 (> (tty-color-gray-shades display) 2)))))
1634
1635 (defun read-color (&optional prompt convert-to-RGB-p allow-empty-name-p msg-p)
1636 "Read a color name or RGB hex value: #RRRRGGGGBBBB.
1637 Completion is available for color names, but not for RGB hex strings.
1638 If the user inputs an RGB hex string, it must have the form
1639 #XXXXXXXXXXXX or XXXXXXXXXXXX, where each X is a hex digit. The
1640 number of Xs must be a multiple of 3, with the same number of Xs for
1641 each of red, green, and blue. The order is red, green, blue.
1642
1643 In addition to standard color names and RGB hex values, the following
1644 are available as color candidates. In each case, the corresponding
1645 color is used.
1646
1647 * `foreground at point' - foreground under the cursor
1648 * `background at point' - background under the cursor
1649
1650 Checks input to be sure it represents a valid color. If not, raises
1651 an error (but see exception for empty input with non-nil
1652 ALLOW-EMPTY-NAME-P).
1653
1654 Optional arg PROMPT is the prompt; if nil, uses a default prompt.
1655
1656 Interactively, or with optional arg CONVERT-TO-RGB-P non-nil, converts
1657 an input color name to an RGB hex string. Returns the RGB hex string.
1658
1659 Optional arg ALLOW-EMPTY-NAME-P controls what happens if the user
1660 enters an empty color name (that is, just hits `RET'). If non-nil,
1661 then returns an empty color name, \"\". If nil, then raises an error.
1662 Programs must test for \"\" if ALLOW-EMPTY-NAME-P is non-nil. They
1663 can then perform an appropriate action in case of empty input.
1664
1665 Interactively, or with optional arg MSG-P non-nil, echoes the color in
1666 a message."
1667 (interactive "i\np\ni\np") ; Always convert to RGB interactively.
1668 (let* ((completion-ignore-case t)
1669 (colors (append '("foreground at point" "background at point")
1670 (defined-colors)))
1671 (color (completing-read (or prompt "Color (name or #R+G+B+): ")
1672 colors))
1673 hex-string)
1674 (cond ((string= "foreground at point" color)
1675 (setq color (foreground-color-at-point)))
1676 ((string= "background at point" color)
1677 (setq color (background-color-at-point))))
1678 (unless color
1679 (setq color ""))
1680 (setq hex-string
1681 (string-match "^#?\\([a-fA-F0-9][a-fA-F0-9][a-fA-F0-9]\\)+$" color))
1682 (if (and allow-empty-name-p (string= "" color))
1683 ""
1684 (when (and hex-string (not (eq (aref color 0) ?#)))
1685 (setq color (concat "#" color))) ; No #; add it.
1686 (unless hex-string
1687 (when (or (string= "" color) (not (test-completion color colors)))
1688 (error "No such color: %S" color))
1689 (when convert-to-RGB-p
1690 (let ((components (x-color-values color)))
1691 (unless components (error "No such color: %S" color))
1692 (unless (string-match "^#\\([a-fA-F0-9][a-fA-F0-9][a-fA-F0-9]\\)+$" color)
1693 (setq color (format "#%04X%04X%04X"
1694 (logand 65535 (nth 0 components))
1695 (logand 65535 (nth 1 components))
1696 (logand 65535 (nth 2 components))))))))
1697 (when msg-p (message "Color: `%s'" color))
1698 color)))
1699
1700 ;; Commented out because I decided it is better to include the
1701 ;; duplicates in read-color's completion list.
1702
1703 ;; (defun defined-colors-without-duplicates ()
1704 ;; "Return the list of defined colors, without the no-space versions.
1705 ;; For each color name, we keep the variant that DOES have spaces."
1706 ;; (let ((result (copy-sequence (defined-colors)))
1707 ;; to-be-rejected)
1708 ;; (save-match-data
1709 ;; (dolist (this result)
1710 ;; (if (string-match " " this)
1711 ;; (push (replace-regexp-in-string " " ""
1712 ;; this)
1713 ;; to-be-rejected)))
1714 ;; (dolist (elt to-be-rejected)
1715 ;; (let ((as-found (car (member-ignore-case elt result))))
1716 ;; (setq result (delete as-found result)))))
1717 ;; result))
1718
1719 (defun face-at-point ()
1720 "Return the face of the character after point.
1721 If it has more than one face, return the first one.
1722 Return nil if it has no specified face."
1723 (let* ((faceprop (or (get-char-property (point) 'read-face-name)
1724 (get-char-property (point) 'face)
1725 'default))
1726 (face (cond ((symbolp faceprop) faceprop)
1727 ;; List of faces (don't treat an attribute spec).
1728 ;; Just use the first face.
1729 ((and (consp faceprop) (not (keywordp (car faceprop)))
1730 (not (memq (car faceprop)
1731 '(foreground-color background-color))))
1732 (car faceprop))
1733 (t nil)))) ; Invalid face value.
1734 (if (facep face) face nil)))
1735
1736 (defun foreground-color-at-point ()
1737 "Return the foreground color of the character after point."
1738 ;; `face-at-point' alone is not sufficient. It only gets named faces.
1739 ;; Need also pick up any face properties that are not associated with named faces.
1740 (let ((face (or (face-at-point)
1741 (get-char-property (point) 'read-face-name)
1742 (get-char-property (point) 'face))))
1743 (cond ((and face (symbolp face))
1744 (let ((value (face-foreground face nil 'default)))
1745 (if (member value '("unspecified-fg" "unspecified-bg"))
1746 nil
1747 value)))
1748 ((consp face)
1749 (cond ((memq 'foreground-color face) (cdr (memq 'foreground-color face)))
1750 ((memq ':foreground face) (cadr (memq ':foreground face)))))
1751 (t nil)))) ; Invalid face value.
1752
1753 (defun background-color-at-point ()
1754 "Return the background color of the character after point."
1755 ;; `face-at-point' alone is not sufficient. It only gets named faces.
1756 ;; Need also pick up any face properties that are not associated with named faces.
1757 (let ((face (or (face-at-point)
1758 (get-char-property (point) 'read-face-name)
1759 (get-char-property (point) 'face))))
1760 (cond ((and face (symbolp face))
1761 (let ((value (face-background face nil 'default)))
1762 (if (member value '("unspecified-fg" "unspecified-bg"))
1763 nil
1764 value)))
1765 ((consp face)
1766 (cond ((memq 'background-color face) (cdr (memq 'background-color face)))
1767 ((memq ':background face) (cadr (memq ':background face)))))
1768 (t nil)))) ; Invalid face value.
1769 \f
1770 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1771 ;;; Background mode.
1772 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1773
1774 (defcustom frame-background-mode nil
1775 "*The brightness of the background.
1776 Set this to the symbol `dark' if your background color is dark,
1777 `light' if your background is light, or nil (automatic by default)
1778 if you want Emacs to examine the brightness for you. Don't set this
1779 variable with `setq'; this won't have the expected effect."
1780 :group 'faces
1781 :set #'(lambda (var value)
1782 (set-default var value)
1783 (mapc 'frame-set-background-mode (frame-list)))
1784 :initialize 'custom-initialize-changed
1785 :type '(choice (const dark)
1786 (const light)
1787 (const :tag "automatic" nil)))
1788
1789
1790 (defun frame-set-background-mode (frame)
1791 "Set up display-dependent faces on FRAME.
1792 Display-dependent faces are those which have different definitions
1793 according to the `background-mode' and `display-type' frame parameters."
1794 (let* ((bg-resource
1795 (and (window-system frame)
1796 (x-get-resource "backgroundMode" "BackgroundMode")))
1797 (bg-color (frame-parameter frame 'background-color))
1798 (terminal-bg-mode (terminal-parameter frame 'background-mode))
1799 (tty-type (tty-type frame))
1800 (bg-mode
1801 (cond (frame-background-mode)
1802 (bg-resource
1803 (intern (downcase bg-resource)))
1804 (terminal-bg-mode)
1805 ((and (null (window-system frame))
1806 ;; Unspecified frame background color can only
1807 ;; happen on tty's.
1808 (member bg-color '(nil unspecified "unspecified-bg")))
1809 ;; There is no way to determine the background mode
1810 ;; automatically, so we make a guess based on the
1811 ;; terminal type.
1812 (if (and tty-type
1813 (string-match "^\\(xterm\\|rxvt\\|dtterm\\|eterm\\)"
1814 tty-type))
1815 'light
1816 'dark))
1817 ((equal bg-color "unspecified-fg") ; inverted colors
1818 (if (and tty-type
1819 (string-match "^\\(xterm\\|rxvt\\|dtterm\\|eterm\\)"
1820 tty-type))
1821 'dark
1822 'light))
1823 ((>= (apply '+ (color-values bg-color frame))
1824 ;; Just looking at the screen, colors whose
1825 ;; values add up to .6 of the white total
1826 ;; still look dark to me.
1827 (* (apply '+ (color-values "white" frame)) .6))
1828 'light)
1829 (t 'dark)))
1830 (display-type
1831 (cond ((null (window-system frame))
1832 (if (tty-display-color-p frame) 'color 'mono))
1833 ((display-color-p frame)
1834 'color)
1835 ((x-display-grayscale-p frame)
1836 'grayscale)
1837 (t 'mono)))
1838 (old-bg-mode
1839 (frame-parameter frame 'background-mode))
1840 (old-display-type
1841 (frame-parameter frame 'display-type)))
1842
1843 (unless (and (eq bg-mode old-bg-mode) (eq display-type old-display-type))
1844 (let ((locally-modified-faces nil))
1845 ;; Before modifying the frame parameters, we collect a list of
1846 ;; faces that don't match what their face-spec says they should
1847 ;; look like; we then avoid changing these faces below.
1848 ;; These are the faces whose attributes were modified on FRAME.
1849 ;; We use a negative list on the assumption that most faces will
1850 ;; be unmodified, so we can avoid consing in the common case.
1851 (dolist (face (face-list))
1852 (and (not (get face 'face-override-spec))
1853 (not (face-spec-match-p face
1854 (face-user-default-spec face)
1855 (selected-frame)))
1856 (push face locally-modified-faces)))
1857 ;; Now change to the new frame parameters
1858 (modify-frame-parameters frame
1859 (list (cons 'background-mode bg-mode)
1860 (cons 'display-type display-type)))
1861 ;; For all named faces, choose face specs matching the new frame
1862 ;; parameters, unless they have been locally modified.
1863 (dolist (face (face-list))
1864 (unless (memq face locally-modified-faces)
1865 (face-spec-recalc face frame)))))))
1866
1867 \f
1868 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1869 ;;; Frame creation.
1870 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1871
1872 (defun x-handle-named-frame-geometry (parameters)
1873 "Add geometry parameters for a named frame to parameter list PARAMETERS.
1874 Value is the new parameter list."
1875 (let* ((name (or (cdr (assq 'name parameters))
1876 (cdr (assq 'name default-frame-alist))))
1877 (x-resource-name name)
1878 (res-geometry (if name (x-get-resource "geometry" "Geometry"))))
1879 (when res-geometry
1880 (let ((parsed (x-parse-geometry res-geometry)))
1881 ;; If the resource specifies a position, call the position
1882 ;; and size "user-specified".
1883 (when (or (assq 'top parsed)
1884 (assq 'left parsed))
1885 (setq parsed (append '((user-position . t) (user-size . t)) parsed)))
1886 ;; Put the geometry parameters at the end. Copy
1887 ;; default-frame-alist so that they go after it.
1888 (setq parameters (append parameters default-frame-alist parsed))))
1889 parameters))
1890
1891
1892 (defun x-handle-reverse-video (frame parameters)
1893 "Handle the reverse-video frame parameter and X resource.
1894 `x-create-frame' does not handle this one."
1895 (when (cdr (or (assq 'reverse parameters)
1896 (assq 'reverse default-frame-alist)
1897 (let ((resource (x-get-resource "reverseVideo"
1898 "ReverseVideo")))
1899 (if resource
1900 (cons nil (member (downcase resource)
1901 '("on" "true")))))))
1902 (let* ((params (frame-parameters frame))
1903 (bg (cdr (assq 'foreground-color params)))
1904 (fg (cdr (assq 'background-color params))))
1905 (modify-frame-parameters frame
1906 (list (cons 'foreground-color fg)
1907 (cons 'background-color bg)))
1908 (if (equal bg (cdr (assq 'border-color params)))
1909 (modify-frame-parameters frame
1910 (list (cons 'border-color fg))))
1911 (if (equal bg (cdr (assq 'mouse-color params)))
1912 (modify-frame-parameters frame
1913 (list (cons 'mouse-color fg))))
1914 (if (equal bg (cdr (assq 'cursor-color params)))
1915 (modify-frame-parameters frame
1916 (list (cons 'cursor-color fg)))))))
1917
1918
1919 (defun x-create-frame-with-faces (&optional parameters)
1920 "Create a frame from optional frame parameters PARAMETERS.
1921 Parameters not specified by PARAMETERS are taken from
1922 `default-frame-alist'. If PARAMETERS specify a frame name,
1923 handle X geometry resources for that name. If either PARAMETERS
1924 or `default-frame-alist' contains a `reverse' parameter, or
1925 the X resource ``reverseVideo'' is present, handle that.
1926 Value is the new frame created."
1927 (setq parameters (x-handle-named-frame-geometry parameters))
1928 (let ((visibility-spec (assq 'visibility parameters))
1929 (frame (x-create-frame `((visibility . nil) . ,parameters)))
1930 success)
1931 (unwind-protect
1932 (progn
1933 (x-setup-function-keys frame)
1934 (x-handle-reverse-video frame parameters)
1935 (frame-set-background-mode frame)
1936 (face-set-after-frame-default frame)
1937 ;; Make sure the tool-bar is ready to be enabled. The
1938 ;; `tool-bar-lines' frame parameter will not take effect
1939 ;; without this call.
1940 (tool-bar-setup frame)
1941 (if (null visibility-spec)
1942 (make-frame-visible frame)
1943 (modify-frame-parameters frame (list visibility-spec)))
1944 (setq success t))
1945 (unless success
1946 (delete-frame frame)))
1947 frame))
1948
1949 (defun face-set-after-frame-default (frame)
1950 "Set frame-local faces of FRAME from face specs and resources.
1951 Initialize colors of certain faces from frame parameters."
1952 (unless inhibit-face-set-after-frame-default
1953 (if (face-attribute 'default :font t)
1954 (set-face-attribute 'default frame :font
1955 (face-attribute 'default :font t))
1956 (set-face-attribute 'default frame :family
1957 (face-attribute 'default :family t))
1958 (set-face-attribute 'default frame :height
1959 (face-attribute 'default :height t))
1960 (set-face-attribute 'default frame :slant
1961 (face-attribute 'default :slant t))
1962 (set-face-attribute 'default frame :weight
1963 (face-attribute 'default :weight t))
1964 (set-face-attribute 'default frame :width
1965 (face-attribute 'default :width t))))
1966 ;; Find attributes that should be initialized from frame parameters.
1967 (let ((face-params '((foreground-color default :foreground)
1968 (background-color default :background)
1969 (border-color border :background)
1970 (cursor-color cursor :background)
1971 (scroll-bar-foreground scroll-bar :foreground)
1972 (scroll-bar-background scroll-bar :background)
1973 (mouse-color mouse :background)))
1974 apply-params)
1975 (dolist (param face-params)
1976 (let* ((value (frame-parameter frame (nth 0 param)))
1977 (face (nth 1 param))
1978 (attr (nth 2 param))
1979 (default-value (face-attribute face attr t)))
1980 ;; Compile a list of face attributes to set, but don't set
1981 ;; them yet. The call to make-face-x-resource-internal,
1982 ;; below, can change frame parameters, and the final set of
1983 ;; frame parameters should be the ones acquired at this step.
1984 (if (eq default-value 'unspecified)
1985 ;; The face spec does not specify a new-frame value for
1986 ;; this attribute. Check if the existing frame parameter
1987 ;; specifies it.
1988 (if value
1989 (push (list face frame attr value) apply-params))
1990 ;; The face spec specifies a value for this attribute, to be
1991 ;; applied to the face on all new frames.
1992 (push (list face frame attr default-value) apply-params))))
1993 ;; Initialize faces from face specs and X resources. The
1994 ;; condition-case prevents invalid specs from causing frame
1995 ;; creation to fail.
1996 (dolist (face (delq 'default (face-list)))
1997 (condition-case ()
1998 (progn
1999 (face-spec-recalc face frame)
2000 (if (memq (window-system frame) '(x w32 mac))
2001 (make-face-x-resource-internal face frame))
2002 (internal-merge-in-global-face face frame))
2003 (error nil)))
2004 ;; Apply the attributes specified by frame parameters. This
2005 ;; rewrites parameters changed by make-face-x-resource-internal
2006 (dolist (param apply-params)
2007 (apply 'set-face-attribute param))))
2008
2009 (defun tty-handle-reverse-video (frame parameters)
2010 "Handle the reverse-video frame parameter for terminal frames."
2011 (when (cdr (or (assq 'reverse parameters)
2012 (assq 'reverse default-frame-alist)))
2013 (let* ((params (frame-parameters frame))
2014 (bg (cdr (assq 'foreground-color params)))
2015 (fg (cdr (assq 'background-color params))))
2016 (modify-frame-parameters frame
2017 (list (cons 'foreground-color fg)
2018 (cons 'background-color bg)))
2019 (if (equal bg (cdr (assq 'mouse-color params)))
2020 (modify-frame-parameters frame
2021 (list (cons 'mouse-color fg))))
2022 (if (equal bg (cdr (assq 'cursor-color params)))
2023 (modify-frame-parameters frame
2024 (list (cons 'cursor-color fg)))))))
2025
2026
2027 (defun tty-create-frame-with-faces (&optional parameters)
2028 "Create a frame from optional frame parameters PARAMETERS.
2029 Parameters not specified by PARAMETERS are taken from
2030 `default-frame-alist'. If either PARAMETERS or `default-frame-alist'
2031 contains a `reverse' parameter, handle that. Value is the new frame
2032 created."
2033 (let ((frame (make-terminal-frame parameters))
2034 success)
2035 (unwind-protect
2036 (with-selected-frame frame
2037 (tty-handle-reverse-video frame (frame-parameters frame))
2038
2039 (unless (terminal-parameter frame 'terminal-initted)
2040 (set-terminal-parameter frame 'terminal-initted t)
2041 (set-locale-environment nil frame)
2042 (tty-run-terminal-initialization frame))
2043 (frame-set-background-mode frame)
2044 (face-set-after-frame-default frame)
2045 (setq success t))
2046 (unless success
2047 (delete-frame frame)))
2048 frame))
2049
2050 (defun tty-find-type (pred type)
2051 "Return the longest prefix of TYPE to which PRED returns non-nil.
2052 TYPE should be a tty type name such as \"xterm-16color\".
2053
2054 The function tries only those prefixes that are followed by a
2055 dash or underscore in the original type name, like \"xterm\" in
2056 the above example."
2057 (let (hyphend)
2058 (while (and type
2059 (not (funcall pred type)))
2060 ;; Strip off last hyphen and what follows, then try again
2061 (setq type
2062 (if (setq hyphend (string-match "[-_][^-_]+$" type))
2063 (substring type 0 hyphend)
2064 nil))))
2065 type)
2066
2067 (defun tty-run-terminal-initialization (frame &optional type)
2068 "Run the special initialization code for the terminal type of FRAME.
2069 The optional TYPE parameter may be used to override the autodetected
2070 terminal type to a different value."
2071 (setq type (or type (tty-type frame)))
2072 ;; Load library for our terminal type.
2073 ;; User init file can set term-file-prefix to nil to prevent this.
2074 (with-selected-frame frame
2075 (unless (null term-file-prefix)
2076 (let* (term-init-func)
2077 ;; First, load the terminal initialization file, if it is
2078 ;; available and it hasn't been loaded already.
2079 (tty-find-type #'(lambda (type)
2080 (let ((file (locate-library (concat term-file-prefix type))))
2081 (and file
2082 (or (assoc file load-history)
2083 (load file t t)))))
2084 type)
2085 ;; Next, try to find a matching initialization function, and call it.
2086 (tty-find-type #'(lambda (type)
2087 (fboundp (setq term-init-func
2088 (intern (concat "terminal-init-" type)))))
2089 type)
2090 (when (fboundp term-init-func)
2091 (funcall term-init-func))
2092 (set-terminal-parameter frame 'terminal-initted term-init-func)))))
2093
2094 ;; Called from C function init_display to initialize faces of the
2095 ;; dumped terminal frame on startup.
2096
2097 (defun tty-set-up-initial-frame-faces ()
2098 (let ((frame (selected-frame)))
2099 (frame-set-background-mode frame)
2100 (face-set-after-frame-default frame)))
2101
2102
2103
2104 \f
2105 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2106 ;;; Compatiblity with 20.2
2107 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2108
2109 ;; Update a frame's faces when we change its default font.
2110
2111 (defalias 'frame-update-faces 'ignore "")
2112 (make-obsolete 'frame-update-faces "no longer necessary." "21.1")
2113
2114 ;; Update the colors of FACE, after FRAME's own colors have been
2115 ;; changed.
2116
2117 (define-obsolete-function-alias 'frame-update-face-colors
2118 'frame-set-background-mode "21.1")
2119
2120 \f
2121 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2122 ;;; Standard faces.
2123 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2124
2125 (defgroup basic-faces nil
2126 "The standard faces of Emacs."
2127 :group 'faces)
2128
2129 (defface default
2130 '((t nil)) ; If this were nil, face-defface-spec would not be set.
2131 "Basic default face."
2132 :group 'basic-faces)
2133
2134 (defface bold
2135 '((t :weight bold))
2136 "Basic bold face."
2137 :group 'basic-faces)
2138
2139 (defface italic
2140 '((((supports :slant italic))
2141 :slant italic)
2142 (((supports :underline t))
2143 :underline t)
2144 (t
2145 ;; default to italic, even it doesn't appear to be supported,
2146 ;; because in some cases the display engine will do it's own
2147 ;; workaround (to `dim' on ttys)
2148 :slant italic))
2149 "Basic italic face."
2150 :group 'basic-faces)
2151
2152 (defface bold-italic
2153 '((t :weight bold :slant italic))
2154 "Basic bold-italic face."
2155 :group 'basic-faces)
2156
2157 (defface underline
2158 '((((supports :underline t))
2159 :underline t)
2160 (((supports :weight bold))
2161 :weight bold)
2162 (t :underline t))
2163 "Basic underlined face."
2164 :group 'basic-faces)
2165
2166 (defface fixed-pitch
2167 '((t :family "courier"))
2168 "The basic fixed-pitch face."
2169 :group 'basic-faces)
2170
2171 (defface variable-pitch
2172 '((t :family "helv"))
2173 "The basic variable-pitch face."
2174 :group 'basic-faces)
2175
2176 (defface shadow
2177 '((((class color grayscale) (min-colors 88) (background light))
2178 :foreground "grey50")
2179 (((class color grayscale) (min-colors 88) (background dark))
2180 :foreground "grey70")
2181 (((class color) (min-colors 8) (background light))
2182 :foreground "green")
2183 (((class color) (min-colors 8) (background dark))
2184 :foreground "yellow"))
2185 "Basic face for shadowed text."
2186 :group 'basic-faces
2187 :version "22.1")
2188
2189 (defface link
2190 '((((class color) (min-colors 88) (background light))
2191 :foreground "blue1" :underline t)
2192 (((class color) (background light))
2193 :foreground "blue" :underline t)
2194 (((class color) (min-colors 88) (background dark))
2195 :foreground "cyan1" :underline t)
2196 (((class color) (background dark))
2197 :foreground "cyan" :underline t)
2198 (t :inherit underline))
2199 "Basic face for unvisited links."
2200 :group 'basic-faces
2201 :version "22.1")
2202
2203 (defface link-visited
2204 '((default :inherit link)
2205 (((class color) (background light)) :foreground "magenta4")
2206 (((class color) (background dark)) :foreground "violet"))
2207 "Basic face for visited links."
2208 :group 'basic-faces
2209 :version "22.1")
2210
2211 (defface highlight
2212 '((((class color) (min-colors 88) (background light))
2213 :background "darkseagreen2")
2214 (((class color) (min-colors 88) (background dark))
2215 :background "darkolivegreen")
2216 (((class color) (min-colors 16) (background light))
2217 :background "darkseagreen2")
2218 (((class color) (min-colors 16) (background dark))
2219 :background "darkolivegreen")
2220 (((class color) (min-colors 8))
2221 :background "green" :foreground "black")
2222 (t :inverse-video t))
2223 "Basic face for highlighting."
2224 :group 'basic-faces)
2225
2226 (defface region
2227 '((((class color) (min-colors 88) (background dark))
2228 :background "blue3")
2229 (((class color) (min-colors 88) (background light))
2230 :background "lightgoldenrod2")
2231 (((class color) (min-colors 16) (background dark))
2232 :background "blue3")
2233 (((class color) (min-colors 16) (background light))
2234 :background "lightgoldenrod2")
2235 (((class color) (min-colors 8))
2236 :background "blue" :foreground "white")
2237 (((type tty) (class mono))
2238 :inverse-video t)
2239 (t :background "gray"))
2240 "Basic face for highlighting the region."
2241 :version "21.1"
2242 :group 'basic-faces)
2243
2244 (defface secondary-selection
2245 '((((class color) (min-colors 88) (background light))
2246 :background "yellow1")
2247 (((class color) (min-colors 88) (background dark))
2248 :background "SkyBlue4")
2249 (((class color) (min-colors 16) (background light))
2250 :background "yellow")
2251 (((class color) (min-colors 16) (background dark))
2252 :background "SkyBlue4")
2253 (((class color) (min-colors 8))
2254 :background "cyan" :foreground "black")
2255 (t :inverse-video t))
2256 "Basic face for displaying the secondary selection."
2257 :group 'basic-faces)
2258
2259 (defface trailing-whitespace
2260 '((((class color) (background light))
2261 :background "red1")
2262 (((class color) (background dark))
2263 :background "red1")
2264 (t :inverse-video t))
2265 "Basic face for highlighting trailing whitespace."
2266 :version "21.1"
2267 :group 'whitespace-faces ; like `show-trailing-whitespace'
2268 :group 'basic-faces)
2269
2270 (defface escape-glyph
2271 '((((background dark)) :foreground "cyan")
2272 ;; See the comment in minibuffer-prompt for
2273 ;; the reason not to use blue on MS-DOS.
2274 (((type pc)) :foreground "magenta")
2275 ;; red4 is too dark, but some say blue is too loud.
2276 ;; brown seems to work ok. -- rms.
2277 (t :foreground "brown"))
2278 "Face for characters displayed as sequences using `^' or `\\'."
2279 :group 'basic-faces
2280 :version "22.1")
2281
2282 (defface nobreak-space
2283 '((((class color) (min-colors 88)) :inherit escape-glyph :underline t)
2284 (((class color) (min-colors 8)) :background "magenta")
2285 (t :inverse-video t))
2286 "Face for displaying nobreak space."
2287 :group 'basic-faces
2288 :version "22.1")
2289
2290 (defgroup mode-line-faces nil
2291 "Faces used in the mode line."
2292 :group 'mode-line
2293 :group 'faces
2294 :version "22.1")
2295
2296 (defface mode-line
2297 '((((class color) (min-colors 88))
2298 :box (:line-width -1 :style released-button)
2299 :background "grey75" :foreground "black")
2300 (t
2301 :inverse-video t))
2302 "Basic mode line face for selected window."
2303 :version "21.1"
2304 :group 'mode-line-faces
2305 :group 'basic-faces)
2306
2307 (defface mode-line-inactive
2308 '((default
2309 :inherit mode-line)
2310 (((class color) (min-colors 88) (background light))
2311 :weight light
2312 :box (:line-width -1 :color "grey75" :style nil)
2313 :foreground "grey20" :background "grey90")
2314 (((class color) (min-colors 88) (background dark) )
2315 :weight light
2316 :box (:line-width -1 :color "grey40" :style nil)
2317 :foreground "grey80" :background "grey30"))
2318 "Basic mode line face for non-selected windows."
2319 :version "22.1"
2320 :group 'mode-line-faces
2321 :group 'basic-faces)
2322
2323 (defface mode-line-highlight
2324 '((((class color) (min-colors 88))
2325 :box (:line-width 2 :color "grey40" :style released-button))
2326 (t
2327 :inherit highlight))
2328 "Basic mode line face for highlighting."
2329 :version "22.1"
2330 :group 'mode-line-faces
2331 :group 'basic-faces)
2332
2333 (defface mode-line-emphasis
2334 '((t (:weight bold)))
2335 "Face used to emphasize certain mode line features.
2336 Use the face `mode-line-highlight' for features that can be selected."
2337 :version "23.1"
2338 :group 'mode-line-faces
2339 :group 'basic-faces)
2340
2341 (defface mode-line-buffer-id
2342 '((t (:weight bold)))
2343 "Face used for buffer identification parts of the mode line."
2344 :version "22.1"
2345 :group 'mode-line-faces
2346 :group 'basic-faces)
2347
2348 ;; Make `modeline' an alias for `mode-line', for compatibility.
2349 (put 'modeline 'face-alias 'mode-line)
2350 (put 'modeline-inactive 'face-alias 'mode-line-inactive)
2351 (put 'modeline-highlight 'face-alias 'mode-line-highlight)
2352 (put 'modeline-buffer-id 'face-alias 'mode-line-buffer-id)
2353
2354 (defface header-line
2355 '((default
2356 :inherit mode-line)
2357 (((type tty))
2358 ;; This used to be `:inverse-video t', but that doesn't look very
2359 ;; good when combined with inverse-video mode-lines and multiple
2360 ;; windows. Underlining looks better, and is more consistent with
2361 ;; the window-system face variants, which deemphasize the
2362 ;; header-line in relation to the mode-line face. If a terminal
2363 ;; can't underline, then the header-line will end up without any
2364 ;; highlighting; this may be too confusing in general, although it
2365 ;; happens to look good with the only current use of header-lines,
2366 ;; the info browser. XXX
2367 :inverse-video nil ;Override the value inherited from mode-line.
2368 :underline t)
2369 (((class color grayscale) (background light))
2370 :background "grey90" :foreground "grey20"
2371 :box nil)
2372 (((class color grayscale) (background dark))
2373 :background "grey20" :foreground "grey90"
2374 :box nil)
2375 (((class mono) (background light))
2376 :background "white" :foreground "black"
2377 :inverse-video nil
2378 :box nil
2379 :underline t)
2380 (((class mono) (background dark))
2381 :background "black" :foreground "white"
2382 :inverse-video nil
2383 :box nil
2384 :underline t))
2385 "Basic header-line face."
2386 :version "21.1"
2387 :group 'basic-faces)
2388
2389 (defface vertical-border
2390 '((((type tty)) :inherit mode-line-inactive))
2391 "Face used for vertical window dividers on ttys."
2392 :version "22.1"
2393 :group 'basic-faces)
2394
2395 (defface minibuffer-prompt
2396 '((((background dark)) :foreground "cyan")
2397 ;; Don't use blue because many users of the MS-DOS port customize
2398 ;; their foreground color to be blue.
2399 (((type pc)) :foreground "magenta")
2400 (t :foreground "medium blue"))
2401 "Face for minibuffer prompts.
2402 By default, Emacs automatically adds this face to the value of
2403 `minibuffer-prompt-properties', which is a list of text properties
2404 used to display the prompt text."
2405 :version "22.1"
2406 :group 'basic-faces)
2407
2408 (setq minibuffer-prompt-properties
2409 (append minibuffer-prompt-properties (list 'face 'minibuffer-prompt)))
2410
2411 (defface fringe
2412 '((((class color) (background light))
2413 :background "grey95")
2414 (((class color) (background dark))
2415 :background "grey10")
2416 (t
2417 :background "gray"))
2418 "Basic face for the fringes to the left and right of windows under X."
2419 :version "21.1"
2420 :group 'frames
2421 :group 'basic-faces)
2422
2423 (defface scroll-bar '((t nil))
2424 "Basic face for the scroll bar colors under X."
2425 :version "21.1"
2426 :group 'frames
2427 :group 'basic-faces)
2428
2429 (defface border '((t nil))
2430 "Basic face for the frame border under X."
2431 :version "21.1"
2432 :group 'frames
2433 :group 'basic-faces)
2434
2435 (defface cursor '((t nil))
2436 "Basic face for the cursor color under X.
2437 Note: Other faces cannot inherit from the cursor face."
2438 :version "21.1"
2439 :group 'cursor
2440 :group 'basic-faces)
2441
2442 (put 'cursor 'face-no-inherit t)
2443
2444 (defface mouse '((t nil))
2445 "Basic face for the mouse color under X."
2446 :version "21.1"
2447 :group 'mouse
2448 :group 'basic-faces)
2449
2450 (defface tool-bar
2451 '((default
2452 :box (:line-width 1 :style released-button)
2453 :foreground "black")
2454 (((type x w32 mac) (class color))
2455 :background "grey75")
2456 (((type x) (class mono))
2457 :background "grey"))
2458 "Basic tool-bar face."
2459 :version "21.1"
2460 :group 'basic-faces)
2461
2462 (defface menu
2463 '((((type tty))
2464 :inverse-video t)
2465 (((type x-toolkit))
2466 )
2467 (t
2468 :inverse-video t))
2469 "Basic face for the font and colors of the menu bar and popup menus."
2470 :version "21.1"
2471 :group 'menu
2472 :group 'basic-faces)
2473
2474 \f
2475 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2476 ;;; Manipulating font names.
2477 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2478
2479 ;; This is here for compatibilty with Emacs 20.2. For example,
2480 ;; international/fontset.el uses x-resolve-font-name. The following
2481 ;; functions are not used in the face implementation itself.
2482
2483 (defvar x-font-regexp nil)
2484 (defvar x-font-regexp-head nil)
2485 (defvar x-font-regexp-weight nil)
2486 (defvar x-font-regexp-slant nil)
2487
2488 (defconst x-font-regexp-weight-subnum 1)
2489 (defconst x-font-regexp-slant-subnum 2)
2490 (defconst x-font-regexp-swidth-subnum 3)
2491 (defconst x-font-regexp-adstyle-subnum 4)
2492
2493 ;;; Regexps matching font names in "Host Portable Character Representation."
2494 ;;;
2495 (let ((- "[-?]")
2496 (foundry "[^-]+")
2497 (family "[^-]+")
2498 (weight "\\(bold\\|demibold\\|medium\\)") ; 1
2499 ; (weight\? "\\(\\*\\|bold\\|demibold\\|medium\\|\\)") ; 1
2500 (weight\? "\\([^-]*\\)") ; 1
2501 (slant "\\([ior]\\)") ; 2
2502 ; (slant\? "\\([ior?*]?\\)") ; 2
2503 (slant\? "\\([^-]?\\)") ; 2
2504 ; (swidth "\\(\\*\\|normal\\|semicondensed\\|\\)") ; 3
2505 (swidth "\\([^-]*\\)") ; 3
2506 ; (adstyle "\\(\\*\\|sans\\|\\)") ; 4
2507 (adstyle "\\([^-]*\\)") ; 4
2508 (pixelsize "[0-9]+")
2509 (pointsize "[0-9][0-9]+")
2510 (resx "[0-9][0-9]+")
2511 (resy "[0-9][0-9]+")
2512 (spacing "[cmp?*]")
2513 (avgwidth "[0-9]+")
2514 (registry "[^-]+")
2515 (encoding "[^-]+")
2516 )
2517 (setq x-font-regexp
2518 (concat "\\`\\*?[-?*]"
2519 foundry - family - weight\? - slant\? - swidth - adstyle -
2520 pixelsize - pointsize - resx - resy - spacing - avgwidth -
2521 registry - encoding "\\*?\\'"
2522 ))
2523 (setq x-font-regexp-head
2524 (concat "\\`[-?*]" foundry - family - weight\? - slant\?
2525 "\\([-*?]\\|\\'\\)"))
2526 (setq x-font-regexp-slant (concat - slant -))
2527 (setq x-font-regexp-weight (concat - weight -))
2528 nil)
2529
2530
2531 (defun x-resolve-font-name (pattern &optional face frame)
2532 "Return a font name matching PATTERN.
2533 All wildcards in PATTERN are instantiated.
2534 If PATTERN is nil, return the name of the frame's base font, which never
2535 contains wildcards.
2536 Given optional arguments FACE and FRAME, return a font which is
2537 also the same size as FACE on FRAME, or fail."
2538 (or (symbolp face)
2539 (setq face (face-name face)))
2540 (and (eq frame t)
2541 (setq frame nil))
2542 (if pattern
2543 ;; Note that x-list-fonts has code to handle a face with nil as its font.
2544 (let ((fonts (x-list-fonts pattern face frame 1)))
2545 (or fonts
2546 (if face
2547 (if (string-match "\\*" pattern)
2548 (if (null (face-font face))
2549 (error "No matching fonts are the same height as the frame default font")
2550 (error "No matching fonts are the same height as face `%s'" face))
2551 (if (null (face-font face))
2552 (error "Height of font `%s' doesn't match the frame default font"
2553 pattern)
2554 (error "Height of font `%s' doesn't match face `%s'"
2555 pattern face)))
2556 (error "No fonts match `%s'" pattern)))
2557 (car fonts))
2558 (cdr (assq 'font (frame-parameters (selected-frame))))))
2559
2560
2561 (defun x-frob-font-weight (font which)
2562 (let ((case-fold-search t))
2563 (cond ((string-match x-font-regexp font)
2564 (concat (substring font 0
2565 (match-beginning x-font-regexp-weight-subnum))
2566 which
2567 (substring font (match-end x-font-regexp-weight-subnum)
2568 (match-beginning x-font-regexp-adstyle-subnum))
2569 ;; Replace the ADD_STYLE_NAME field with *
2570 ;; because the info in it may not be the same
2571 ;; for related fonts.
2572 "*"
2573 (substring font (match-end x-font-regexp-adstyle-subnum))))
2574 ((string-match x-font-regexp-head font)
2575 (concat (substring font 0 (match-beginning 1)) which
2576 (substring font (match-end 1))))
2577 ((string-match x-font-regexp-weight font)
2578 (concat (substring font 0 (match-beginning 1)) which
2579 (substring font (match-end 1)))))))
2580 (make-obsolete 'x-frob-font-weight 'make-face-... "21.1")
2581
2582 (defun x-frob-font-slant (font which)
2583 (let ((case-fold-search t))
2584 (cond ((string-match x-font-regexp font)
2585 (concat (substring font 0
2586 (match-beginning x-font-regexp-slant-subnum))
2587 which
2588 (substring font (match-end x-font-regexp-slant-subnum)
2589 (match-beginning x-font-regexp-adstyle-subnum))
2590 ;; Replace the ADD_STYLE_NAME field with *
2591 ;; because the info in it may not be the same
2592 ;; for related fonts.
2593 "*"
2594 (substring font (match-end x-font-regexp-adstyle-subnum))))
2595 ((string-match x-font-regexp-head font)
2596 (concat (substring font 0 (match-beginning 2)) which
2597 (substring font (match-end 2))))
2598 ((string-match x-font-regexp-slant font)
2599 (concat (substring font 0 (match-beginning 1)) which
2600 (substring font (match-end 1)))))))
2601 (make-obsolete 'x-frob-font-slant 'make-face-... "21.1")
2602
2603 ;; These aliases are here so that we don't get warnings about obsolete
2604 ;; functions from the byte compiler.
2605 (defalias 'internal-frob-font-weight 'x-frob-font-weight)
2606 (defalias 'internal-frob-font-slant 'x-frob-font-slant)
2607
2608 (defun x-make-font-bold (font)
2609 "Given an X font specification, make a bold version of it.
2610 If that can't be done, return nil."
2611 (internal-frob-font-weight font "bold"))
2612 (make-obsolete 'x-make-font-bold 'make-face-bold "21.1")
2613
2614 (defun x-make-font-demibold (font)
2615 "Given an X font specification, make a demibold version of it.
2616 If that can't be done, return nil."
2617 (internal-frob-font-weight font "demibold"))
2618 (make-obsolete 'x-make-font-demibold 'make-face-bold "21.1")
2619
2620 (defun x-make-font-unbold (font)
2621 "Given an X font specification, make a non-bold version of it.
2622 If that can't be done, return nil."
2623 (internal-frob-font-weight font "medium"))
2624 (make-obsolete 'x-make-font-unbold 'make-face-unbold "21.1")
2625
2626 (defun x-make-font-italic (font)
2627 "Given an X font specification, make an italic version of it.
2628 If that can't be done, return nil."
2629 (internal-frob-font-slant font "i"))
2630 (make-obsolete 'x-make-font-italic 'make-face-italic "21.1")
2631
2632 (defun x-make-font-oblique (font) ; you say tomayto...
2633 "Given an X font specification, make an oblique version of it.
2634 If that can't be done, return nil."
2635 (internal-frob-font-slant font "o"))
2636 (make-obsolete 'x-make-font-oblique 'make-face-italic "21.1")
2637
2638 (defun x-make-font-unitalic (font)
2639 "Given an X font specification, make a non-italic version of it.
2640 If that can't be done, return nil."
2641 (internal-frob-font-slant font "r"))
2642 (make-obsolete 'x-make-font-unitalic 'make-face-unitalic "21.1")
2643
2644 (defun x-make-font-bold-italic (font)
2645 "Given an X font specification, make a bold and italic version of it.
2646 If that can't be done, return nil."
2647 (and (setq font (internal-frob-font-weight font "bold"))
2648 (internal-frob-font-slant font "i")))
2649 (make-obsolete 'x-make-font-bold-italic 'make-face-bold-italic "21.1")
2650
2651 (provide 'faces)
2652
2653 ;; arch-tag: 19a4759f-2963-445f-b004-425b9aadd7d6
2654 ;;; faces.el ends here