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