]> code.delx.au - gnu-emacs/blob - lisp/faces.el
(read-face-attribute, defined-colors, color-defined-p):
[gnu-emacs] / lisp / faces.el
1 ;;; faces.el --- Lisp faces
2
3 ;; Copyright (C) 1992, 1993, 1994, 1995, 1996, 1998, 1999
4 ;; Free Software Foundation, Inc.
5
6 ;; This file is part of GNU Emacs.
7
8 ;; GNU Emacs is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 2, or (at your option)
11 ;; any later version.
12
13 ;; GNU Emacs is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
17
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs; see the file COPYING. If not, write to the
20 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 ;; Boston, MA 02111-1307, USA.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 (eval-when-compile
28 (require 'custom)
29 (require 'cl))
30
31 (require 'cus-face)
32
33 \f
34 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
35 ;;; Font selection.
36 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
37
38 (defgroup font-selection nil
39 "Influencing face font selection."
40 :group 'faces)
41
42
43 (defcustom face-font-selection-order
44 '(:width :height :weight :slant)
45 "*A list specifying how face font selection chooses fonts.
46 Each of the four symbols `:width', `:height', `:weight', and `:slant'
47 must appear once in the list, and the list must not contain any other
48 elements. Font selection tries to find a best matching font for
49 those face attributes first that appear first in the list. For
50 example, if `:slant' appears before `:height', font selection first
51 tries to find a font with a suitable slant, even if this results in
52 a font height that isn't optimal."
53 :tag "Font selection order."
54 :group 'font-selection
55 :set #'(lambda (symbol value)
56 (set-default symbol value)
57 (internal-set-font-selection-order value)))
58
59
60 (defcustom face-font-family-alternatives
61 '(("courier" "fixed")
62 ("helv" "helvetica" "fixed"))
63 "*Alist of alternative font family names.
64 Each element has the the form (FAMILY ALTERNATIVE1 ALTERNATIVE2 ...).
65 If fonts of family FAMILY can't be loaded, try ALTERNATIVE1, then
66 ALTERNATIVE2 etc."
67 :tag "Alternative font families to try."
68 :group 'font-selection
69 :set #'(lambda (symbol value)
70 (set-default symbol value)
71 (internal-set-alternative-font-family-alist value)))
72
73
74 \f
75 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
76 ;;; Creation, copying.
77 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
78
79
80 (defun face-list ()
81 "Return a list of all defined face names."
82 (mapcar #'car face-new-frame-defaults))
83
84
85 ;;; ### If not frame-local initialize by what X resources?
86
87 (defun make-face (face &optional no-init-from-resources)
88 "Define a new face with name FACE, a symbol.
89 NO-INIT-FROM-RESOURCES non-nil means don't initialize frame-local
90 variants of FACE from X resources. (X resources recognized are found
91 in the global variable `face-x-resources'.) If FACE is already known
92 as a face, leave it unmodified. Value is FACE."
93 (interactive "SMake face: ")
94 (unless (facep face)
95 ;; Make frame-local faces (this also makes the global one).
96 (dolist (frame (frame-list))
97 (internal-make-lisp-face face frame))
98 ;; Add the face to the face menu.
99 (when (fboundp 'facemenu-add-new-face)
100 (facemenu-add-new-face face))
101 ;; Define frame-local faces for all frames from X resources.
102 (unless no-init-from-resources
103 (make-face-x-resource-internal face)))
104 face)
105
106
107 (defun make-empty-face (face)
108 "Define a new, empty face with name FACE.
109 If the face already exists, it is left unmodified. Value is FACE."
110 (interactive "SMake empty face: ")
111 (make-face face 'no-init-from-resources))
112
113
114 (defun copy-face (old-face new-face &optional frame new-frame)
115 "Define a face just like OLD-FACE, with name NEW-FACE.
116
117 If NEW-FACE already exists as a face, it is modified to be like
118 OLD-FACE. If it doesn't already exist, it is created.
119
120 If the optional argument FRAME is given as a frame, NEW-FACE is
121 changed on FRAME only.
122 If FRAME is t, the frame-independent default specification for OLD-FACE
123 is copied to NEW-FACE.
124 If FRAME is nil, copying is done for the frame-independent defaults
125 and for each existing frame.
126
127 If the optional fourth argument NEW-FRAME is given,
128 copy the information from face OLD-FACE on frame FRAME
129 to NEW-FACE on frame NEW-FRAME."
130 (let ((inhibit-quit t))
131 (if (null frame)
132 (progn
133 (dolist (frame (frame-list))
134 (copy-face old-face new-face frame))
135 (copy-face old-face new-face t))
136 (internal-copy-lisp-face old-face new-face frame new-frame))
137 new-face))
138
139
140 \f
141 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
142 ;;; Obsolete functions
143 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
144
145 ;; The functions in this section are defined because Lisp packages use
146 ;; them, despite the prefix `internal-' suggesting that they are
147 ;; private to the face implementation.
148
149 (defun internal-find-face (name &optional frame)
150 "Retrieve the face named NAME.
151 Return nil if there is no such face.
152 If the optional argument FRAME is given, this gets the face NAME for
153 that frame; otherwise, it uses the selected frame.
154 If FRAME is the symbol t, then the global, non-frame face is returned.
155 If NAME is already a face, it is simply returned.
156
157 This function is defined for compatibility with Emacs 20.2. It
158 should not be used anymore."
159 (facep name))
160
161
162 (defun internal-get-face (name &optional frame)
163 "Retrieve the face named NAME; error if there is none.
164 If the optional argument FRAME is given, this gets the face NAME for
165 that frame; otherwise, it uses the selected frame.
166 If FRAME is the symbol t, then the global, non-frame face is returned.
167 If NAME is already a face, it is simply returned.
168
169 This function is defined for compatibility with Emacs 20.2. It
170 should not be used anymore."
171 (or (internal-find-face name frame)
172 (check-face name)))
173
174
175 \f
176 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
177 ;;; Predicates, type checks.
178 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
179
180 (defun facep (face)
181 "Return non-nil if FACE is a face name."
182 (internal-lisp-face-p face))
183
184
185 (defun check-face (face)
186 "Signal an error if FACE doesn't name a face.
187 Value is FACE."
188 (unless (facep face)
189 (error "Not a face: %s" face))
190 face)
191
192
193 ;; The ID returned is not to be confused with the internally used IDs
194 ;; of realized faces. The ID assigned to Lisp faces is used to
195 ;; support faces in display table entries.
196
197 (defun face-id (face &optional frame)
198 "Return the interNal ID of face with name FACE.
199 If optional argument FRAME is nil or omitted, use the selected frame."
200 (check-face face)
201 (get face 'face))
202
203
204 (defun face-equal (face1 face2 &optional frame)
205 "Non-nil if faces FACE1 and FACE2 are equal.
206 Faces are considered equal if all their attributes are equal.
207 If the optional argument FRAME is given, report on face FACE in that frame.
208 If FRAME is t, report on the defaults for face FACE (for new frames).
209 If FRAME is omitted or nil, use the selected frame."
210 (internal-lisp-face-equal-p face1 face2 frame))
211
212
213 (defun face-differs-from-default-p (face &optional frame)
214 "Non-nil if FACE displays differently from the default face.
215 If the optional argument FRAME is given, report on face FACE in that frame.
216 If FRAME is t, report on the defaults for face FACE (for new frames).
217 If FRAME is omitted or nil, use the selected frame.
218 A face is considered to be ``the same'' as the default face if it is
219 actually specified in the same way (equal attributes) or if it is
220 fully-unspecified, and thus inherits the attributes of any face it
221 is displayed on top of."
222 (cond ((eq frame t) (setq frame nil))
223 ((null frame) (setq frame (selected-frame))))
224 (let* ((v1 (internal-lisp-face-p face frame))
225 (n (if v1 (length v1) 0))
226 (v2 (internal-lisp-face-p 'default frame))
227 (i 1))
228 (unless v1
229 (error "Not a face: %S" face))
230 (while (and (< i n)
231 (or (eq 'unspecified (aref v1 i))
232 (equal (aref v1 i) (aref v2 i))))
233 (setq i (1+ i)))
234 (< i n)))
235
236
237 (defun face-nontrivial-p (face &optional frame)
238 "True if face FACE has some non-nil attribute.
239 If the optional argument FRAME is given, report on face FACE in that frame.
240 If FRAME is t, report on the defaults for face FACE (for new frames).
241 If FRAME is omitted or nil, use the selected frame."
242 (not (internal-lisp-face-empty-p face frame)))
243
244
245 \f
246 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
247 ;;; Setting face attributes from X resources.
248 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
249
250 (defcustom face-x-resources
251 '((:family (".attributeFamily" . "Face.AttributeFamily"))
252 (:width (".attributeWidth" . "Face.AttributeWidth"))
253 (:height (".attributeHeight" . "Face.AttributeHeight"))
254 (:weight (".attributeWeight" . "Face.AttributeWeight"))
255 (:slant (".attributeSlant" . "Face.AttributeSlant"))
256 (:foreground (".attributeForeground" . "Face.AttributeForeground"))
257 (:background (".attributeBackground" . "Face.AttributeBackground"))
258 (:overline (".attributeOverline" . "Face.AttributeOverline"))
259 (:strike-through (".attributeStrikeThrough" . "Face.AttributeStrikeThrough"))
260 (:box (".attributeBox" . "Face.AttributeBox"))
261 (:underline (".attributeUnderline" . "Face.AttributeUnderline"))
262 (:inverse-video (".attributeInverse" . "Face.AttributeInverse"))
263 (:stipple
264 (".attributeStipple" . "Face.AttributeStipple")
265 (".attributeBackgroundPixmap" . "Face.AttributeBackgroundPixmap"))
266 (:font (".attributeFont" . "Face.AttributeFont"))
267 (:bold (".attributeBold" . "Face.AttributeBold"))
268 (:italic (".attributeItalic" . "Face.AttributeItalic"))
269 (:font (".attributeFont" . "Face.AttributeFont")))
270 "*List of X resources and classes for face attributes.
271 Each element has the form (ATTRIBUTE ENTRY1 ENTRY2...) where ATTRIBUTE is
272 the name of a face attribute, and each ENTRY is a cons of the form
273 (RESOURCE . CLASS) with RESOURCE being the resource and CLASS being the
274 X resource class for the attribute."
275 :type 'sexp
276 :group 'faces)
277
278
279 (defun set-face-attribute-from-resource (face attribute resource class frame)
280 "Set FACE's ATTRIBUTE from X resource RESOURCE, class CLASS on FRAME.
281 Value is the attribute value specified by the resource, or nil
282 if not present. This function displays a message if the resource
283 specifies an invalid attribute."
284 (let* ((face-name (face-name face))
285 (value (internal-face-x-get-resource (concat face-name resource)
286 class frame)))
287 (when value
288 (condition-case ()
289 (internal-set-lisp-face-attribute-from-resource
290 face attribute (downcase value) frame)
291 (error
292 (message "Face %s, frame %s: invalid attribute %s %s from X resource"
293 face-name frame attribute value))))
294 value))
295
296
297 (defun set-face-attributes-from-resources (face frame)
298 "Set attributes of FACE from X resources for FRAME."
299 (when (memq (framep frame) '(x w32))
300 (dolist (definition face-x-resources)
301 (let ((attribute (car definition)))
302 (dolist (entry (cdr definition))
303 (set-face-attribute-from-resource face attribute (car entry)
304 (cdr entry) frame))))))
305
306
307 (defun make-face-x-resource-internal (face &optional frame)
308 "Fill frame-local FACE on FRAME from X resources.
309 FRAME nil or not specified means do it for all frames."
310 (if (null frame)
311 (dolist (frame (frame-list))
312 (set-face-attributes-from-resources face frame))
313 (set-face-attributes-from-resources face frame)))
314
315
316 \f
317 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
318 ;;; Retrieving face attributes.
319 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
320
321 (defun face-name (face)
322 "Return the name of face FACE."
323 (symbol-name (check-face face)))
324
325
326 (defun face-attribute (face attribute &optional frame)
327 "Return the value of FACE's ATTRIBUTE on FRAME.
328 If the optional argument FRAME is given, report on face FACE in that frame.
329 If FRAME is t, report on the defaults for face FACE (for new frames).
330 If FRAME is omitted or nil, use the selected frame."
331 (internal-get-lisp-face-attribute face attribute frame))
332
333
334 (defun face-foreground (face &optional frame)
335 "Return the foreground color name of FACE, or nil if unspecified.
336 If the optional argument FRAME is given, report on face FACE in that frame.
337 If FRAME is t, report on the defaults for face FACE (for new frames).
338 If FRAME is omitted or nil, use the selected frame."
339 (internal-get-lisp-face-attribute face :foreground frame))
340
341
342 (defun face-background (face &optional frame)
343 "Return the background color name of FACE, or nil if unspecified.
344 If the optional argument FRAME is given, report on face FACE in that frame.
345 If FRAME is t, report on the defaults for face FACE (for new frames).
346 If FRAME is omitted or nil, use the selected frame."
347 (internal-get-lisp-face-attribute face :background frame))
348
349
350 (defun face-stipple (face &optional frame)
351 "Return the stipple pixmap name of FACE, or nil if unspecified.
352 If the optional argument FRAME is given, report on face FACE in that frame.
353 If FRAME is t, report on the defaults for face FACE (for new frames).
354 If FRAME is omitted or nil, use the selected frame."
355 (internal-get-lisp-face-attribute face :stipple frame))
356
357
358 (defalias 'face-background-pixmap 'face-stipple)
359
360
361 (defun face-underline-p (face &optional frame)
362 "Return non-nil if FACE is underlined.
363 If the optional argument FRAME is given, report on face FACE in that frame.
364 If FRAME is t, report on the defaults for face FACE (for new frames).
365 If FRAME is omitted or nil, use the selected frame."
366 (eq (face-attribute face :underline frame) t))
367
368
369 (defun face-inverse-video-p (face &optional frame)
370 "Return non-nil if FACE is in inverse video on FRAME.
371 If the optional argument FRAME is given, report on face FACE in that frame.
372 If FRAME is t, report on the defaults for face FACE (for new frames).
373 If FRAME is omitted or nil, use the selected frame."
374 (eq (face-attribute face :inverse-video frame) t))
375
376
377 (defun face-bold-p (face &optional frame)
378 "Return non-nil if the font of FACE is bold on FRAME.
379 If the optional argument FRAME is given, report on face FACE in that frame.
380 If FRAME is t, report on the defaults for face FACE (for new frames).
381 If FRAME is omitted or nil, use the selected frame.
382 Use `face-attribute' for finer control."
383 (let ((bold (face-attribute face :weight frame)))
384 (memq bold '(semi-bold bold extra-bold ultra-bold))))
385
386
387 (defun face-italic-p (face &optional frame)
388 "Return non-nil if the font of FACE is italic on FRAME.
389 If the optional argument FRAME is given, report on face FACE in that frame.
390 If FRAME is t, report on the defaults for face FACE (for new frames).
391 If FRAME is omitted or nil, use the selected frame.
392 Use `face-attribute' for finer control."
393 (let ((italic (face-attribute face :slant frame)))
394 (memq italic '(italic oblique))))
395
396
397
398
399 \f
400 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
401 ;;; Face documentation.
402 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
403
404 (defun face-documentation (face)
405 "Get the documentation string for FACE."
406 (get face 'face-documentation))
407
408
409 (defun set-face-documentation (face string)
410 "Set the documentation string for FACE to STRING."
411 ;; Perhaps the text should go in DOC.
412 (put face 'face-documentation (purecopy string)))
413
414
415 (defalias 'face-doc-string 'face-documentation)
416 (defalias 'set-face-doc-string 'set-face-documentation)
417
418
419 \f
420 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
421 ;; Setting face attributes.
422 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
423
424
425 (defun set-face-attribute (face frame &rest args)
426 "Set attributes of FACE on FRAME from ARGS.
427
428 FRAME nil means change attributes on all frames. FRAME t means change
429 the default for new frames (this is done automatically each time an
430 attribute is changed on all frames).
431
432 ARGS must come in pairs ATTRIBUTE VALUE. ATTRIBUTE must be a valid
433 face attribute name. All attributes can be set to `unspecified';
434 this fact is not further mentioned below.
435
436 The following attributes are recognized:
437
438 `:family'
439
440 VALUE must be a string specifying the font family, e.g. ``courier'',
441 or a fontset alias name. If a font family is specified, wild-cards `*'
442 and `?' are allowed.
443
444 `:width'
445
446 VALUE specifies the relative proportionate width of the font to use.
447 It must be one of the symbols `ultra-condensed', `extra-condensed',
448 `condensed', `semi-condensed', `normal', `semi-expanded', `expanded',
449 `extra-expanded', or `ultra-expanded'.
450
451 `:height'
452
453 VALUE must be an integer specifying the height of the font to use in
454 1/10 pt.
455
456 `:weight'
457
458 VALUE specifies the weight of the font to use. It must be one of the
459 symbols `ultra-bold', `extra-bold', `bold', `semi-bold', `normal',
460 `semi-light', `light', `extra-light', `ultra-light'.
461
462 `:slant'
463
464 VALUE specifies the slant of the font to use. It must be one of the
465 symbols `italic', `oblique', `normal', `reverse-italic', or
466 `reverse-oblique'.
467
468 `:foreground', `:background'
469
470 VALUE must be a color name, a string.
471
472 `:underline'
473
474 VALUE specifies whether characters in FACE should be underlined. If
475 VALUE is t, underline with foreground color of the face. If VALUE is
476 a string, underline with that color. If VALUE is nil, explicitly
477 don't underline.
478
479 `:overline'
480
481 VALUE specifies whether characters in FACE should be overlined. If
482 VALUE is t, overline with foreground color of the face. If VALUE is a
483 string, overline with that color. If VALUE is nil, explicitly don't
484 overline.
485
486 `:strike-through'
487
488 VALUE specifies whether characters in FACE should be drawn with a line
489 striking through them. If VALUE is t, use the foreground color of the
490 face. If VALUE is a string, strike-through with that color. If VALUE
491 is nil, explicitly don't strike through.
492
493 `:box'
494
495 VALUE specifies whether characters in FACE should have a box drawn
496 around them. If VALUE is nil, explicitly don't draw boxes. If
497 VALUE is t, draw a box with lines of width 1 in the foreground color
498 of the face. If VALUE is a string, the string must be a color name,
499 and the box is drawn in that color with a line width of 1. Otherwise,
500 VALUE must be a property list of the form `(:line-width WIDTH
501 :color COLOR :style STYLE)'. If a keyword/value pair is missing from
502 the property list, a default value will be used for the value, as
503 specified below. WIDTH specifies the width of the lines to draw; it
504 defaults to 1. COLOR is the name of the color to draw in, default is
505 the foreground color of the face for simple boxes, and the background
506 color of the face for 3D boxes. STYLE specifies whether a 3D box
507 should be draw. If STYLE is `released-button', draw a box looking
508 like a released 3D button. If STYLE is `pressed-button' draw a box
509 that appears like a pressed button. If STYLE is nil, the default if
510 the property list doesn't contain a style specification, draw a 2D
511 box.
512
513 `:inverse-video'
514
515 VALUE specifies whether characters in FACE should be displayed in
516 inverse video. VALUE must be one of t or nil.
517
518 `:stipple'
519
520 If VALUE is a string, it must be the name of a file of pixmap data.
521 The directories listed in the `x-bitmap-file-path' variable are
522 searched. Alternatively, VALUE may be a list of the form (WIDTH
523 HEIGHT DATA) where WIDTH and HEIGHT are the size in pixels, and DATA
524 is a string containing the raw bits of the bitmap. VALUE nil means
525 explicitly don't use a stipple pattern.
526
527 For convenience, attributes `:family', `:width', `:height', `:weight',
528 and `:slant' may also be set in one step from an X font name:
529
530 `:font'
531
532 Set font-related face attributes from VALUE. VALUE must be a valid
533 XLFD font name. If it is a font name pattern, the first matching font
534 will be used.
535
536 For compatibility with Emacs 20, keywords `:bold' and `:italic' can
537 be used to specify that a bold or italic font should be used. VALUE
538 must be t or nil in that case. A value of `unspecified' is not allowed."
539 (cond ((null frame)
540 ;; Change face on all frames.
541 (dolist (frame (frame-list))
542 (apply #'set-face-attribute face frame args))
543 ;; Record that as a default for new frames.
544 (apply #'set-face-attribute face t args))
545 (t
546 (while args
547 (internal-set-lisp-face-attribute face (car args)
548 (purecopy (cadr args))
549 frame)
550 (setq args (cdr (cdr args)))))))
551
552
553 (defun make-face-bold (face &optional frame noerror)
554 "Make the font of FACE be bold, if possible.
555 FRAME nil or not specified means change face on all frames.
556 Argument NOERROR is ignored and retained for compatibility.
557 Use `set-face-attribute' for finer control of the font weight."
558 (interactive (list (read-face-name "Make which face bold: ")))
559 (set-face-attribute face frame :weight 'bold))
560
561
562 (defun make-face-unbold (face &optional frame noerror)
563 "Make the font of FACE be non-bold, if possible.
564 FRAME nil or not specified means change face on all frames.
565 Argument NOERROR is ignored and retained for compatibility."
566 (interactive (list (read-face-name "Make which face non-bold: ")))
567 (set-face-attribute face frame :weight 'normal))
568
569
570 (defun make-face-italic (face &optional frame noerror)
571 "Make the font of FACE be italic, if possible.
572 FRAME nil or not specified means change face on all frames.
573 Argument NOERROR is ignored and retained for compatibility.
574 Use `set-face-attribute' for finer control of the font slant."
575 (interactive (list (read-face-name "Make which face italic: ")))
576 (set-face-attribute face frame :slant 'italic))
577
578
579 (defun make-face-unitalic (face &optional frame noerror)
580 "Make the font of FACE be non-italic, if possible.
581 FRAME nil or not specified means change face on all frames."
582 (interactive (list (read-face-name "Make which face non-italic: ")))
583 (set-face-attribute face frame :slant 'normal))
584
585
586 (defun make-face-bold-italic (face &optional frame noerror)
587 "Make the font of FACE be bold and italic, if possible.
588 FRAME nil or not specified means change face on all frames.
589 Argument NOERROR is ignored and retained for compatibility.
590 Use `set-face-attribute' for finer control of font weight and slant."
591 (interactive (list (read-face-name "Make which face bold-italic: ")))
592 (set-face-attribute face frame :weight 'bold :slant 'italic))
593
594
595 (defun set-face-font (face font &optional frame)
596 "Change font-related attributes of FACE to those of FONT (a string).
597 FRAME nil or not specified means change face on all frames.
598 This sets the attributes `:family', `:width', `:height', `:weight',
599 and `:slant'. When called interactively, prompt for the face and font."
600 (interactive (read-face-and-attribute :font))
601 (set-face-attribute face frame :font font))
602
603
604 ;; Implementation note: Emulating gray background colors with a
605 ;; stipple pattern is now part of the face realization process, and is
606 ;; done in C depending on the frame on which the face is realized.
607
608 (defun set-face-background (face color &optional frame)
609 "Change the background color of face FACE to COLOR (a string).
610 FRAME nil or not specified means change face on all frames.
611 When called interactively, prompt for the face and color."
612 (interactive (read-face-and-attribute :background))
613 (set-face-attribute face frame :background color))
614
615
616 (defun set-face-foreground (face color &optional frame)
617 "Change the foreground color of face FACE to COLOR (a string).
618 FRAME nil or not specified means change face on all frames.
619 When called interactively, prompt for the face and color."
620 (interactive (read-face-and-attribute :foreground))
621 (set-face-attribute face frame :foreground color))
622
623
624 (defun set-face-stipple (face stipple &optional frame)
625 "Change the stipple pixmap of face FACE to STIPPLE.
626 FRAME nil or not specified means change face on all frames.
627 STIPPLE. should be a string, the name of a file of pixmap data.
628 The directories listed in the `x-bitmap-file-path' variable are searched.
629
630 Alternatively, STIPPLE may be a list of the form (WIDTH HEIGHT DATA)
631 where WIDTH and HEIGHT are the size in pixels,
632 and DATA is a string, containing the raw bits of the bitmap."
633 (interactive (read-face-and-attribute :stipple))
634 (set-face-attribute face frame :stipple stipple))
635
636
637 (defun set-face-underline (face underline &optional frame)
638 "Specify whether face FACE is underlined.
639 UNDERLINE nil means FACE explicitly doesn't underline.
640 UNDERLINE non-nil means FACE explicitly does underlining
641 with the same of the foreground color.
642 If UNDERLINE is a string, underline with the color named UNDERLINE.
643 FRAME nil or not specified means change face on all frames.
644 Use `set-face-attribute' to ``unspecify'' underlining."
645 (interactive
646 (let ((list (read-face-and-attribute :underline)))
647 (list (car list) (eq (car (cdr list)) t))))
648 (set-face-attribute face frame :underline underline))
649
650
651 (defun set-face-underline-p (face underline-p &optional frame)
652 "Specify whether face FACE is underlined.
653 UNDERLINE-P nil means FACE explicitly doesn't underline.
654 UNDERLINE-P non-nil means FACE explicitly does underlining.
655 FRAME nil or not specified means change face on all frames.
656 Use `set-face-attribute' to ``unspecify'' underlining."
657 (interactive
658 (let ((list (read-face-and-attribute :underline)))
659 (list (car list) (eq (car (cdr list)) t))))
660 (set-face-attribute face frame :underline underline-p))
661
662
663 (defun set-face-inverse-video-p (face inverse-video-p &optional frame)
664 "Specify whether face FACE is in inverse video.
665 INVERSE-VIDEO-P non-nil means FACE displays explicitly in inverse video.
666 INVERSE-VIDEO-P nil means FACE explicitly is not in inverse video.
667 FRAME nil or not specified means change face on all frames.
668 Use `set-face-attribute' to ``unspecify'' the inverse video attribute."
669 (interactive
670 (let ((list (read-face-and-attribute :inverse-video)))
671 (list (car list) (eq (car (cdr list)) t))))
672 (set-face-attribute face frame :inverse-video inverse-video-p))
673
674
675 (defun set-face-bold-p (face bold-p &optional frame)
676 "Specify whether face FACE is bold.
677 BOLD-P non-nil means FACE should explicitly display bold.
678 BOLD-P nil means FACE should explicitly display non-bold.
679 FRAME nil or not specified means change face on all frames.
680 Use `set-face-attribute' or `modify-face' for finer control."
681 (if (null bold-p)
682 (make-face-unbold face frame)
683 (make-face-bold face frame)))
684
685
686 (defun set-face-italic-p (face italic-p &optional frame)
687 "Specify whether face FACE is italic.
688 ITALIC-P non-nil means FACE should explicitly display italic.
689 ITALIC-P nil means FACE should explicitly display non-italic.
690 FRAME nil or not specified means change face on all frames.
691 Use `set-face-attribute' or `modify-face' for finer control."
692 (if (null italic-p)
693 (make-face-unitalic face frame)
694 (make-face-italic face frame)))
695
696
697 (defalias 'set-face-background-pixmap 'set-face-stipple)
698
699
700 (defun invert-face (face &optional frame)
701 "Swap the foreground and background colors of FACE.
702 FRAME nil or not specified means change face on all frames.
703 If FACE specifies neither foreground nor background color,
704 set its foreground and background to the background and foreground
705 of the default face. Value is FACE."
706 (interactive (list (read-face-name "Invert face: ")))
707 (let ((fg (face-attribute face :foreground frame))
708 (bg (face-attribute face :background frame)))
709 (if (or fg bg)
710 (set-face-attribute face frame :foreground bg :background fg)
711 (set-face-attribute face frame
712 :foreground
713 (face-attribute 'default :background frame)
714 :background
715 (face-attribute 'default :foreground frame))))
716 face)
717
718 \f
719 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
720 ;;; Interactively modifying faces.
721 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
722
723 (defun read-face-name (prompt)
724 "Read and return a face symbol, prompting with PROMPT.
725 Value is a symbol naming a known face."
726 (let ((face-list (mapcar #'(lambda (x) (cons (symbol-name x) x))
727 (face-list)))
728 face)
729 (while (equal "" (setq face (completing-read prompt face-list nil t))))
730 (intern face)))
731
732
733 (defun face-valid-attribute-values (attribute &optional frame)
734 "Return valid values for face attribute ATTRIBUTE.
735 The optional argument FRAME is used to determine available fonts
736 and colors. If it is nil or not specified, the selected frame is
737 used. Value is an alist of (NAME . VALUE) if ATTRIBUTE expects a value
738 out of a set of discrete values. Value is `integerp' if ATTRIBUTE expects
739 an integer value."
740 (let (valid)
741 (setq valid
742 (case attribute
743 (:family
744 (if window-system
745 (mapcar #'(lambda (x) (cons (car x) (car x)))
746 (x-font-family-list))
747 ;; Only one font on TTYs.
748 (list (cons "default" "default"))))
749 ((:width :weight :slant :inverse-video)
750 (mapcar #'(lambda (x) (cons (symbol-name x) x))
751 (internal-lisp-face-attribute-values attribute)))
752 ((:underline :overline :strike-through :box)
753 (if window-system
754 (nconc (mapcar #'(lambda (x) (cons (symbol-name x) x))
755 (internal-lisp-face-attribute-values attribute))
756 (mapcar #'(lambda (c) (cons c c))
757 (x-defined-colors frame)))
758 (mapcar #'(lambda (x) (cons (symbol-name x) x))
759 (internal-lisp-face-attribute-values attribute))))
760 ((:foreground :background)
761 (mapcar #'(lambda (c) (cons c c))
762 (defined-colors frame)))
763 ((:height)
764 'integerp)
765 (:stipple
766 (and (memq window-system '(x w32))
767 (mapcar #'list
768 (apply #'nconc (mapcar #'directory-files
769 x-bitmap-file-path)))))
770 (t
771 (error "Internal error"))))
772 (if (listp valid)
773 (nconc (list (cons "unspecified" 'unspecified)) valid)
774 valid)))
775
776
777
778 (defvar face-attribute-name-alist
779 '((:family . "font family")
780 (:width . "character set width")
781 (:height . "height in 1/10 pt")
782 (:weight . "weight")
783 (:slant . "slant")
784 (:underline . "underline")
785 (:overline . "overline")
786 (:strike-through . "strike-through")
787 (:box . "box")
788 (:inverse-video . "inverse-video display")
789 (:foreground . "foreground color")
790 (:background . "background color")
791 (:stipple . "background stipple"))
792 "An alist of descriptive names for face attributes.
793 Each element has the form (ATTRIBUTE-NAME . DESCRIPTION) where
794 ATTRIBUTE-NAME is a face attribute name (a keyword symbol), and
795 DESCRIPTION is a descriptive name for ATTRIBUTE-NAME.")
796
797
798 (defun face-descriptive-attribute-name (attribute)
799 "Return a descriptive name for ATTRIBUTE."
800 (cdr (assq attribute face-attribute-name-alist)))
801
802
803 (defun face-read-string (face default name &optional completion-alist)
804 "Interactively read a face attribute string value.
805 FACE is the face whose attribute is read. DEFAULT is the default
806 value to return if no new value is entered. NAME is a descriptive
807 name of the attribute for prompting. COMPLETION-ALIST is an alist
808 of valid values, if non-nil.
809
810 Entering nothing accepts the default value DEFAULT.
811 Value is the new attribute value."
812 (let* ((completion-ignore-case t)
813 (value (completing-read
814 (if default
815 (format "Set face %s %s (default %s): "
816 face name (downcase (if (symbolp default)
817 (symbol-name default)
818 default)))
819 (format "Set face %s %s: " face name))
820 completion-alist)))
821 (if (equal value "") default value)))
822
823
824 (defun face-read-integer (face default name)
825 "Interactively read an integer face attribute value.
826 FACE is the face whose attribute is read. DEFAULT is the default
827 value to return if no new value is entered. NAME is a descriptive
828 name of the attribute for prompting. Value is the new attribute value."
829 (let ((new-value
830 (face-read-string face
831 (if (memq
832 default
833 '(unspecified unspecified-fg unspecified-bg))
834 default
835 (int-to-string default))
836 name
837 (list (cons "unspecified" 'unspecified)))))
838 (if (memq new-value '(unspecified unspecified-fg unspecified-bg))
839 new-value
840 (string-to-int new-value))))
841
842
843 (defun read-face-attribute (face attribute &optional frame)
844 "Interactively read a new value for FACE's ATTRIBUTE.
845 Optional argument FRAME nil or unspecified means read an attribute value
846 of a global face. Value is the new attribute value."
847 (let* ((old-value (face-attribute face attribute frame))
848 (attribute-name (face-descriptive-attribute-name attribute))
849 (valid (face-valid-attribute-values attribute frame))
850 new-value)
851 ;; Represent complex attribute values as strings by printing them
852 ;; out. Stipple can be a vector; (WIDTH HEIGHT DATA). Box can be
853 ;; a list `(:width WIDTH :color COLOR)' or `(:width WIDTH :shadow
854 ;; SHADOW)'.
855 (when (and (or (eq attribute :stipple)
856 (eq attribute :box))
857 (or (consp old-value)
858 (vectorp old-value)))
859 (setq old-value (prin1-to-string old-value)))
860 (cond ((listp valid)
861 (setq new-value
862 (face-read-string face old-value attribute-name valid))
863 ;; Terminal frames can support colors that don't appear
864 ;; explicitly in VALID, using color approximation code
865 ;; in tty-colors.el.
866 (if (and (memq attribute '(:foreground :background))
867 (not (memq window-system '(x w32 mac)))
868 (not (memq new-value
869 '(unspecified unspecified-fg unspecified-bg))))
870 (setq new-value (car (tty-color-desc new-value frame))))
871 (unless (eq new-value 'unspecified)
872 (setq new-value (cdr (assoc new-value valid)))))
873 ((eq valid 'integerp)
874 (setq new-value (face-read-integer face old-value attribute-name)))
875 (t (error "Internal error")))
876 ;; Convert stipple and box value text we read back to a list or
877 ;; vector if it looks like one. This makes the assumption that a
878 ;; pixmap file name won't start with an open-paren.
879 (when (and (or (eq attribute :stipple)
880 (eq attribute :box))
881 (stringp new-value)
882 (string-match "^[[(]" new-value))
883 (setq new-value (read new-value)))
884 new-value))
885
886
887 (defun read-face-font (face &optional frame)
888 "Read the name of a font for FACE on FRAME.
889 If optional argument FRAME Is nil or omitted, use the selected frame."
890 (let ((completion-ignore-case t))
891 (completing-read "Set font attributes of face %s from font: "
892 face (x-list-fonts "*" nil frame))))
893
894
895 (defun read-all-face-attributes (face &optional frame)
896 "Interactively read all attributes for FACE.
897 If optional argument FRAME Is nil or omitted, use the selected frame.
898 Value is a property list of attribute names and new values."
899 (let (result)
900 (dolist (attribute face-attribute-name-alist result)
901 (setq result (cons (car attribute)
902 (cons (read-face-attribute face (car attribute) frame)
903 result))))))
904
905
906 (defun modify-face (&optional frame)
907 "Modify attributes of faces interactively.
908 If optional argument FRAME is nil or omitted, modify the face used
909 for newly created frame, i.e. the global face."
910 (interactive)
911 (let ((face (read-face-name "Modify face: ")))
912 (apply #'set-face-attribute face frame
913 (read-all-face-attributes face frame))))
914
915
916 (defun read-face-and-attribute (attribute &optional frame)
917 "Read face name and face attribute value.
918 ATTRIBUTE is the attribute whose new value is read.
919 FRAME nil or unspecified means read attribute value of global face.
920 Value is a list (FACE NEW-VALUE) where FACE is the face read
921 (a symbol), and NEW-VALUE is value read."
922 (cond ((eq attribute :font)
923 (let* ((prompt (format "Set font-related attributes of face: "))
924 (face (read-face-name prompt))
925 (font (read-face-font face frame)))
926 (list face font)))
927 (t
928 (let* ((attribute-name (face-descriptive-attribute-name attribute))
929 (prompt (format "Set %s of face: " attribute-name))
930 (face (read-face-name prompt))
931 (new-value (read-face-attribute face attribute frame)))
932 (list face new-value)))))
933
934
935 \f
936 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
937 ;;; Listing faces.
938 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
939
940 (defvar list-faces-sample-text
941 "abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ"
942 "*Text string to display as the sample text for `list-faces-display'.")
943
944
945 ;; The name list-faces would be more consistent, but let's avoid a
946 ;; conflict with Lucid, which uses that name differently.
947
948 (defun list-faces-display ()
949 "List all faces, using the same sample text in each.
950 The sample text is a string that comes from the variable
951 `list-faces-sample-text'."
952 (interactive)
953 (let ((faces (sort (face-list) #'string-lessp))
954 (face nil)
955 (frame (selected-frame))
956 disp-frame window)
957 (with-output-to-temp-buffer "*Faces*"
958 (save-excursion
959 (set-buffer standard-output)
960 (setq truncate-lines t)
961 (while faces
962 (setq face (car faces))
963 (setq faces (cdr faces))
964 (insert (format "%25s " (face-name face)))
965 (let ((beg (point)))
966 (insert list-faces-sample-text)
967 (insert "\n")
968 (put-text-property beg (1- (point)) 'face face)
969 ;; If the sample text has multiple lines, line up all of them.
970 (goto-char beg)
971 (forward-line 1)
972 (while (not (eobp))
973 (insert " ")
974 (forward-line 1))))
975 (goto-char (point-min)))
976 (print-help-return-message))
977 ;; If the *Faces* buffer appears in a different frame,
978 ;; copy all the face definitions from FRAME,
979 ;; so that the display will reflect the frame that was selected.
980 (setq window (get-buffer-window (get-buffer "*Faces*") t))
981 (setq disp-frame (if window (window-frame window)
982 (car (frame-list))))
983 (or (eq frame disp-frame)
984 (let ((faces (face-list)))
985 (while faces
986 (copy-face (car faces) (car faces) frame disp-frame)
987 (setq faces (cdr faces)))))))
988
989
990 (defun describe-face (face &optional frame)
991 "Display the properties of face FACE on FRAME.
992 If the optional argument FRAME is given, report on face FACE in that frame.
993 If FRAME is t, report on the defaults for face FACE (for new frames).
994 If FRAME is omitted or nil, use the selected frame."
995 (interactive (list (read-face-name "Describe face: ")))
996 (let* ((attrs '((:family . "Family")
997 (:width . "Width")
998 (:height . "Height")
999 (:weight . "Weight")
1000 (:slant . "Slant")
1001 (:foreground . "Foreground")
1002 (:background . "Background")
1003 (:underline . "Underline")
1004 (:overline . "Overline")
1005 (:strike-through . "Strike-through")
1006 (:box . "Box")
1007 (:inverse-video . "Inverse")
1008 (:stipple . "Stipple")))
1009 (max-width (apply #'max (mapcar #'(lambda (x) (length (cdr x)))
1010 attrs))))
1011 (with-output-to-temp-buffer "*Help*"
1012 (save-excursion
1013 (set-buffer standard-output)
1014 (dolist (a attrs)
1015 (let ((attr (face-attribute face (car a) frame)))
1016 (insert (make-string (- max-width (length (cdr a))) ?\ )
1017 (cdr a) ": " (format "%s" attr) "\n")))
1018 (insert "\nDocumentation:\n\n"
1019 (or (face-documentation face)
1020 "not documented as a face.")))
1021 (print-help-return-message))))
1022
1023
1024
1025 \f
1026 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1027 ;;; Face specifications (defface).
1028 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1029
1030 ;; Parameter FRAME Is kept for call compatibility to with previous
1031 ;; face implementation.
1032
1033 (defun face-attr-construct (face &optional frame)
1034 "Return a defface-style attribute list for FACE on FRAME.
1035 Value is a property list of pairs ATTRIBUTE VALUE for all specified
1036 face attributes of FACE where ATTRIBUTE is the attribute name and
1037 VALUE is the specified value of that attribute."
1038 (let (result)
1039 (dolist (entry face-attribute-name-alist result)
1040 (let* ((attribute (car entry))
1041 (value (face-attribute face attribute)))
1042 (unless (eq value 'unspecified)
1043 (setq result (nconc (list attribute value) result)))))))
1044
1045
1046 (defun face-spec-set-match-display (display frame)
1047 "Non-nil if DISPLAY matches FRAME.
1048 DISPLAY is part of a spec such as can be used in `defface'.
1049 If FRAME is nil, the current FRAME is used."
1050 (let* ((conjuncts display)
1051 conjunct req options
1052 ;; t means we have succeeded against all the conjuncts in
1053 ;; DISPLAY that have been tested so far.
1054 (match t))
1055 (if (eq conjuncts t)
1056 (setq conjuncts nil))
1057 (while (and conjuncts match)
1058 (setq conjunct (car conjuncts)
1059 conjuncts (cdr conjuncts)
1060 req (car conjunct)
1061 options (cdr conjunct)
1062 match (cond ((eq req 'type)
1063 (or (memq window-system options)
1064 (and (null window-system)
1065 (memq 'tty options))
1066 (and (memq 'motif options)
1067 (featurep 'motif))
1068 (and (memq 'lucid options)
1069 (featurep 'x-toolkit)
1070 (not (featurep 'motif)))
1071 (and (memq 'x-toolkit options)
1072 (featurep 'x-toolkit))))
1073 ((eq req 'class)
1074 (memq (frame-parameter frame 'display-type) options))
1075 ((eq req 'background)
1076 (memq (frame-parameter frame 'background-mode)
1077 options))
1078 (t (error "Unknown req `%S' with options `%S'"
1079 req options)))))
1080 match))
1081
1082
1083 (defun face-spec-choose (spec &optional frame)
1084 "Choose the proper attributes for FRAME, out of SPEC."
1085 (unless frame
1086 (setq frame (selected-frame)))
1087 (let ((tail spec)
1088 result)
1089 (while tail
1090 (let* ((entry (car tail))
1091 (display (nth 0 entry))
1092 (attrs (nth 1 entry)))
1093 (setq tail (cdr tail))
1094 (when (face-spec-set-match-display display frame)
1095 (setq result attrs tail nil))))
1096 result))
1097
1098
1099 (defun face-spec-reset-face (face &optional frame)
1100 "Reset all attributes of FACE on FRAME to unspecified."
1101 (let ((attrs face-attribute-name-alist)
1102 params)
1103 (while attrs
1104 (let ((attr-and-name (car attrs)))
1105 (setq params (cons (car attr-and-name) (cons 'unspecified params))))
1106 (setq attrs (cdr attrs)))
1107 (apply #'set-face-attribute face frame params)))
1108
1109
1110 (defun face-spec-set (face spec &optional frame)
1111 "Set FACE's attributes according to the first matching entry in SPEC.
1112 FRAME is the frame whose frame-local face is set. FRAME nil means
1113 do it on all frames. See `defface' for information about SPEC."
1114 (let ((attrs (face-spec-choose spec frame))
1115 params)
1116 (while attrs
1117 (let ((attribute (car attrs))
1118 (value (car (cdr attrs))))
1119 ;; Support some old-style attribute names and values.
1120 (case attribute
1121 (:bold (setq attribute :weight value (if value 'bold 'normal)))
1122 (:italic (setq attribute :slant value (if value 'italic 'normal))))
1123 (setq params (cons attribute (cons value params))))
1124 (setq attrs (cdr (cdr attrs))))
1125 (face-spec-reset-face face frame)
1126 (apply #'set-face-attribute face frame params)))
1127
1128
1129 (defun face-attr-match-p (face attrs &optional frame)
1130 "Value is non-nil if attributes of FACE match values in plist ATTRS.
1131 Optional parameter FRAME is the frame whose definition of FACE
1132 is used. If nil or omitted, use the selected frame."
1133 (unless frame
1134 (setq frame (selected-frame)))
1135 (let ((list face-attribute-name-alist)
1136 (match t))
1137 (while (and match (not (null list)))
1138 (let* ((attr (car (car list)))
1139 (specified-value (plist-get attrs attr))
1140 (value-now (face-attribute face attr frame)))
1141 (when specified-value
1142 (setq match (equal specified-value value-now)))
1143 (setq list (cdr list))))
1144 match))
1145
1146
1147 (defun face-spec-match-p (face spec &optional frame)
1148 "Return t if FACE, on FRAME, matches what SPEC says it should look like."
1149 (face-attr-match-p face (face-spec-choose spec frame) frame))
1150
1151
1152 \f
1153 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1154 ;;; Frame-type independent color support.
1155 ;;; We keep the old x-* names as aliases for back-compatibility.
1156 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1157
1158 (defun defined-colors (&optional frame)
1159 "Return a list of colors supported for a particular frame.
1160 The argument FRAME specifies which frame to try.
1161 The value may be different for frames on different display types.
1162 If FRAME doesn't support colors, the value is nil."
1163 (if (memq (framep (or frame (selected-frame))) '(x w32))
1164 (xw-defined-colors frame)
1165 (mapcar 'car (tty-color-alist frame))))
1166 (defalias 'x-defined-colors 'defined-colors)
1167
1168 (defun color-defined-p (color &optional frame)
1169 "Return non-nil if color COLOR is supported on frame FRAME.
1170 If FRAME is omitted or nil, use the selected frame.
1171 If COLOR is one of the symbols `unspecified', `unspecified-fg', or
1172 `unspecified-bg', the value is nil."
1173 (if (memq color '(unspecified unspecified-bg unspecified-fg))
1174 nil
1175 (if (memq (framep (or frame (selected-frame))) '(x w32))
1176 (xw-color-defined-p color frame)
1177 (numberp (tty-color-translate color frame)))))
1178 (defalias 'x-color-defined-p 'color-defined-p)
1179
1180 (defun color-values (color &optional frame)
1181 "Return a description of the color named COLOR on frame FRAME.
1182 The value is a list of integer RGB values--\(RED GREEN BLUE\).
1183 These values appear to range from 0 to 65280 or 65535, depending
1184 on the system; white is \(65280 65280 65280\) or \(65535 65535 65535\).
1185 If FRAME is omitted or nil, use the selected frame.
1186 If FRAME cannot display COLOR, the value is nil.
1187 If COLOR is one of the symbols `unspecified', `unspecified-fg', or
1188 `unspecified-bg', the value is nil."
1189 (if (memq color '(unspecified unspecified-fg unspecified-bg))
1190 nil
1191 (if (memq (framep (or frame (selected-frame))) '(x w32))
1192 (xw-color-values color frame)
1193 (tty-color-values color frame))))
1194 (defalias 'x-color-values 'color-values)
1195
1196 (defun display-color-p (&optional display)
1197 "Return t if DISPLAY supports color.
1198 The optional argument DISPLAY specifies which display to ask about.
1199 DISPLAY should be either a frame or a display name (a string).
1200 If omitted or nil, that stands for the selected frame's display."
1201 (if (and (stringp display) (not (fboundp 'x-display-list)))
1202 nil
1203 (if (memq (framep (or display (selected-frame))) '(x w32))
1204 (xw-display-color-p display)
1205 (tty-display-color-p display))))
1206 (defalias 'x-display-color-p 'display-color-p)
1207
1208 \f
1209 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1210 ;;; Background mode.
1211 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1212
1213 (defcustom frame-background-mode nil
1214 "*The brightness of the background.
1215 Set this to the symbol `dark' if your background color is dark, `light' if
1216 your background is light, or nil (default) if you want Emacs to
1217 examine the brightness for you."
1218 :group 'faces
1219 :set #'(lambda (var value)
1220 (set var value)
1221 (mapcar 'frame-set-background-mode (frame-list)))
1222 :initialize 'custom-initialize-changed
1223 :type '(choice (choice-item dark)
1224 (choice-item light)
1225 (choice-item :tag "default" nil)))
1226
1227
1228 (defun frame-set-background-mode (frame)
1229 "Set up the `background-mode' and `display-type' frame parameters for FRAME."
1230 (let* ((bg-resource
1231 (and window-system
1232 (x-get-resource ".backgroundMode" "BackgroundMode")))
1233 (params (frame-parameters frame))
1234 (bg-mode (cond (frame-background-mode)
1235 ((null window-system)
1236 ;; No way to determine this automatically (?).
1237 'dark)
1238 (bg-resource
1239 (intern (downcase bg-resource)))
1240 ((< (apply '+ (x-color-values
1241 (cdr (assq 'background-color
1242 params))
1243 frame))
1244 ;; Just looking at the screen, colors whose
1245 ;; values add up to .6 of the white total
1246 ;; still look dark to me.
1247 (* (apply '+ (x-color-values "white" frame)) .6))
1248 'dark)
1249 (t 'light)))
1250 (display-type (cond ((null window-system)
1251 (if (tty-display-color-p frame) 'color 'mono))
1252 ((x-display-color-p frame)
1253 'color)
1254 ((x-display-grayscale-p frame)
1255 'grayscale)
1256 (t 'mono))))
1257 (modify-frame-parameters frame
1258 (list (cons 'background-mode bg-mode)
1259 (cons 'display-type display-type))))
1260
1261 ;; For all named faces, choose face specs matching the new frame
1262 ;; parameters.
1263 (let ((face-list (face-list)))
1264 (while face-list
1265 (let* ((face (car face-list))
1266 (spec (get face 'face-defface-spec)))
1267 (when spec
1268 (face-spec-set face spec frame))
1269 (setq face-list (cdr face-list))))))
1270
1271
1272
1273 \f
1274 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1275 ;;; Frame creation.
1276 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1277
1278 (defun x-handle-named-frame-geometry (parameters)
1279 "Add geometry parameters for a named frame to parameter list PARAMETERS.
1280 Value is the new parameter list."
1281 (let* ((name (or (cdr (assq 'name parameters))
1282 (cdr (assq 'name default-frame-alist))))
1283 (x-resource-name name)
1284 (res-geometry (if name (x-get-resource "geometry" "Geometry"))))
1285 (when res-geometry
1286 (let ((parsed (x-parse-geometry res-geometry)))
1287 ;; If the resource specifies a position, call the position
1288 ;; and size "user-specified".
1289 (when (or (assq 'top parsed)
1290 (assq 'left parsed))
1291 (setq parsed (append '((user-position . t) (user-size . t)) parsed)))
1292 ;; Put the geometry parameters at the end. Copy
1293 ;; default-frame-alist so that they go after it.
1294 (setq parameters (append parameters default-frame-alist parsed))))
1295 parameters))
1296
1297
1298 (defun x-handle-reverse-video (frame parameters)
1299 "Handle the reverse-video frame parameter and X resource.
1300 `x-create-frame' does not handle this one."
1301 (when (cdr (or (assq 'reverse parameters)
1302 (assq 'reverse default-frame-alist)
1303 (let ((resource (x-get-resource "reverseVideo"
1304 "ReverseVideo")))
1305 (if resource
1306 (cons nil (member (downcase resource)
1307 '("on" "true")))))))
1308 (let* ((params (frame-parameters frame))
1309 (bg (cdr (assq 'foreground-color params)))
1310 (fg (cdr (assq 'background-color params))))
1311 (modify-frame-parameters frame
1312 (list (cons 'foreground-color fg)
1313 (cons 'background-color bg)))
1314 (if (equal bg (cdr (assq 'border-color params)))
1315 (modify-frame-parameters frame
1316 (list (cons 'border-color fg))))
1317 (if (equal bg (cdr (assq 'mouse-color params)))
1318 (modify-frame-parameters frame
1319 (list (cons 'mouse-color fg))))
1320 (if (equal bg (cdr (assq 'cursor-color params)))
1321 (modify-frame-parameters frame
1322 (list (cons 'cursor-color fg)))))))
1323
1324
1325 (defun x-create-frame-with-faces (&optional parameters)
1326 "Create a frame from optional frame parameters PARAMETERS.
1327 Parameters not specified by PARAMETERS are taken from
1328 `default-frame-alist'. If PARAMETERS specify a frame name,
1329 handle X geometry resources for that name. If either PARAMETERS
1330 or `default-frame-alist' contains a `reverse' parameter, or
1331 the X resource ``reverseVideo'' is present, handle that.
1332 Value is the new frame created."
1333 (setq parameters (x-handle-named-frame-geometry parameters))
1334 (let ((visibility-spec (assq 'visibility parameters))
1335 (frame-list (frame-list))
1336 (frame (x-create-frame (cons '(visibility . nil) parameters)))
1337 success)
1338 (unwind-protect
1339 (progn
1340 (x-handle-reverse-video frame parameters)
1341 (frame-set-background-mode frame)
1342 (face-set-after-frame-default frame)
1343 (if (or (null frame-list) (null visibility-spec))
1344 (make-frame-visible frame)
1345 (modify-frame-parameters frame (list visibility-spec)))
1346 (setq success t))
1347 (unless success
1348 (delete-frame frame)))
1349 frame))
1350
1351
1352 (defun face-set-after-frame-default (frame)
1353 "Set frame-local faces of FRAME from face specs and resources.
1354 Initialize colors of certain faces from frame parameters."
1355 (dolist (face (face-list))
1356 (let ((spec (or (get face 'saved-face)
1357 (get face 'face-defface-spec))))
1358 (when spec
1359 (face-spec-set face spec frame))
1360 (internal-merge-in-global-face face frame)
1361 (when (memq window-system '(x w32))
1362 (make-face-x-resource-internal face frame))))
1363
1364 ;; Initialize attributes from frame parameters.
1365 (let ((params '((foreground-color default :foreground)
1366 (background-color default :background)
1367 (border-color border :background)
1368 (cursor-color cursor :background)
1369 (scroll-bar-foreground scroll-bar :foreground)
1370 (scroll-bar-background scroll-bar :background)
1371 (mouse-color mouse :background))))
1372 (while params
1373 (let ((param-name (nth 0 (car params)))
1374 (face (nth 1 (car params)))
1375 (attr (nth 2 (car params)))
1376 value)
1377 (when (setq value (frame-parameter frame param-name))
1378 (set-face-attribute face frame attr value)))
1379 (setq params (cdr params)))))
1380
1381
1382 (defun tty-create-frame-with-faces (&optional parameters)
1383 "Create a frame from optional frame parameters PARAMETERS.
1384 Parameters not specified by PARAMETERS are taken from
1385 `default-frame-alist'. If either PARAMETERS or `default-frame-alist'
1386 contains a `reverse' parameter, handle that. Value is the new frame
1387 created."
1388 (let ((frame (make-terminal-frame parameters))
1389 success)
1390 (unwind-protect
1391 (progn
1392 (frame-set-background-mode frame)
1393 (face-set-after-frame-default frame)
1394 (setq success t))
1395 (unless success
1396 (delete-frame frame)))
1397 frame))
1398
1399
1400 ;; Called from C function init_display to initialize faces of the
1401 ;; dumped terminal frame on startup.
1402
1403 (defun tty-set-up-initial-frame-faces ()
1404 (let ((frame (selected-frame)))
1405 (frame-set-background-mode frame)
1406 (face-set-after-frame-default frame)))
1407
1408
1409
1410 \f
1411 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1412 ;;; Compatiblity with 20.2
1413 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1414
1415 ;; Update a frame's faces when we change its default font.
1416
1417 (defun frame-update-faces (frame)
1418 nil)
1419
1420
1421 ;; Update the colors of FACE, after FRAME's own colors have been
1422 ;; changed.
1423
1424 (defun frame-update-face-colors (frame)
1425 (frame-set-background-mode frame))
1426
1427
1428 \f
1429 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1430 ;;; Standard faces.
1431 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1432
1433 (defgroup basic-faces nil
1434 "The standard faces of Emacs."
1435 :group 'faces)
1436
1437
1438 (defface default
1439 '((t nil))
1440 "Basic default face."
1441 :group 'basic-faces)
1442
1443
1444 (defface mode-line
1445 '((((type x) (class color))
1446 (:box (:line-width 2 :style released-button) :background "grey75"))
1447 (t
1448 (:inverse-video t)))
1449 "Basic mode line face."
1450 :version "21.1"
1451 :group 'basic-faces)
1452
1453 ;; Make `modeline' an alias for `mode-line', for compatibility.
1454 (put 'modeline 'face-alias 'mode-line)
1455
1456 (defface header-line
1457 '((((type x) (class color))
1458 (:box (:line-width 2 :style released-button) :background "grey75"))
1459 (t
1460 (:inverse-video t)))
1461 "Basic header-line face."
1462 :version "21.1"
1463 :group 'basic-faces)
1464
1465
1466 (defface tool-bar
1467 '((((type x) (class color))
1468 (:box (:line-width 1 :style released-button) :background "grey75"))
1469 (((type x) (class mono))
1470 (:box (:line-width 1 :style released-button) :background "grey"))
1471 (t
1472 ()))
1473 "Basic tool-bar face."
1474 :version "21.1"
1475 :group 'basic-faces)
1476
1477
1478 (defface region
1479 '((((type tty) (class color))
1480 (:background "blue" :foreground "white"))
1481 (((type tty) (class mono))
1482 (:inverse-video t))
1483 (((class color) (background dark))
1484 (:background "blue"))
1485 (((class color) (background light))
1486 (:background "lightblue"))
1487 (t (:background "gray")))
1488 "Basic face for highlighting the region."
1489 :group 'basic-faces)
1490
1491
1492 (defface fringe
1493 '((((class color))
1494 (:background "grey95"))
1495 (t
1496 (:background "gray")))
1497 "Basic face for the fringes to the left and right of windows under X."
1498 :version "21.1"
1499 :group 'basic-faces)
1500
1501
1502 (defface scroll-bar '()
1503 "Basic face for the scroll bar colors under X."
1504 :version "21.1"
1505 :group 'basic-faces)
1506
1507
1508 (defface menu
1509 '((((type x-toolkit)) ())
1510 (t (:inverse-video t)))
1511 "Basic menu face."
1512 :version "21.1"
1513 :group 'basic-faces)
1514
1515
1516 (defface border '()
1517 "Basic face for the frame border under X."
1518 :version "21.1"
1519 :group 'basic-faces)
1520
1521
1522 (defface cursor '()
1523 "Basic face for the cursor color under X."
1524 :version "21.1"
1525 :group 'basic-faces)
1526
1527
1528 (defface mouse '()
1529 "Basic face for the mouse color under X."
1530 :version "21.1"
1531 :group 'basic-faces)
1532
1533
1534 (defface bold '((t (:weight bold)))
1535 "Basic bold face."
1536 :group 'basic-faces)
1537
1538
1539 (defface italic '((t (:slant italic)))
1540 "Basic italic font."
1541 :group 'basic-faces)
1542
1543
1544 (defface bold-italic '((t (:weight bold :slant italic)))
1545 "Basic bold-italic face."
1546 :group 'basic-faces)
1547
1548
1549 (defface underline '((t (:underline t)))
1550 "Basic underlined face."
1551 :group 'basic-faces)
1552
1553
1554 (defface highlight
1555 '((((type tty) (class color))
1556 (:background "green"))
1557 (((class color) (background light))
1558 (:background "darkseagreen2"))
1559 (((class color) (background dark))
1560 (:background "darkolivegreen"))
1561 (t (:inverse-video t)))
1562 "Basic face for highlighting."
1563 :group 'basic-faces)
1564
1565
1566 (defface secondary-selection
1567 '((((type tty) (class color))
1568 (:background "cyan"))
1569 (((class color) (background light))
1570 (:background "yellow"))
1571 (((class color) (background dark))
1572 (:background "yellow"))
1573 (t (:inverse-video t)))
1574 "Basic face for displaying the secondary selection."
1575 :group 'basic-faces)
1576
1577
1578 (defface fixed-pitch '((t (:family "courier*")))
1579 "The basic fixed-pitch face."
1580 :group 'basic-faces)
1581
1582
1583 (defface variable-pitch '((t (:family "helv*")))
1584 "The basic variable-pitch face."
1585 :group 'basic-faces)
1586
1587
1588 (defface trailing-whitespace
1589 '((((class color) (background light))
1590 (:background "red"))
1591 (((class color) (background dark))
1592 (:background "red"))
1593 (t (:inverse-video t)))
1594 "Basic face for highlighting trailing whitespace."
1595 :version "21.1"
1596 :group 'basic-faces)
1597
1598
1599 \f
1600 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1601 ;;; Manipulating font names.
1602 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1603
1604 ;; This is here for compatibilty with Emacs 20.2. For example,
1605 ;; international/fontset.el uses these functions to manipulate font
1606 ;; names. The following functions are not used in the face
1607 ;; implementation itself.
1608
1609 (defvar x-font-regexp nil)
1610 (defvar x-font-regexp-head nil)
1611 (defvar x-font-regexp-weight nil)
1612 (defvar x-font-regexp-slant nil)
1613
1614 (defconst x-font-regexp-weight-subnum 1)
1615 (defconst x-font-regexp-slant-subnum 2)
1616 (defconst x-font-regexp-swidth-subnum 3)
1617 (defconst x-font-regexp-adstyle-subnum 4)
1618
1619 ;;; Regexps matching font names in "Host Portable Character Representation."
1620 ;;;
1621 (let ((- "[-?]")
1622 (foundry "[^-]+")
1623 (family "[^-]+")
1624 (weight "\\(bold\\|demibold\\|medium\\)") ; 1
1625 ; (weight\? "\\(\\*\\|bold\\|demibold\\|medium\\|\\)") ; 1
1626 (weight\? "\\([^-]*\\)") ; 1
1627 (slant "\\([ior]\\)") ; 2
1628 ; (slant\? "\\([ior?*]?\\)") ; 2
1629 (slant\? "\\([^-]?\\)") ; 2
1630 ; (swidth "\\(\\*\\|normal\\|semicondensed\\|\\)") ; 3
1631 (swidth "\\([^-]*\\)") ; 3
1632 ; (adstyle "\\(\\*\\|sans\\|\\)") ; 4
1633 (adstyle "\\([^-]*\\)") ; 4
1634 (pixelsize "[0-9]+")
1635 (pointsize "[0-9][0-9]+")
1636 (resx "[0-9][0-9]+")
1637 (resy "[0-9][0-9]+")
1638 (spacing "[cmp?*]")
1639 (avgwidth "[0-9]+")
1640 (registry "[^-]+")
1641 (encoding "[^-]+")
1642 )
1643 (setq x-font-regexp
1644 (concat "\\`\\*?[-?*]"
1645 foundry - family - weight\? - slant\? - swidth - adstyle -
1646 pixelsize - pointsize - resx - resy - spacing - avgwidth -
1647 registry - encoding "\\*?\\'"
1648 ))
1649 (setq x-font-regexp-head
1650 (concat "\\`[-?*]" foundry - family - weight\? - slant\?
1651 "\\([-*?]\\|\\'\\)"))
1652 (setq x-font-regexp-slant (concat - slant -))
1653 (setq x-font-regexp-weight (concat - weight -))
1654 nil)
1655
1656
1657 (defun x-resolve-font-name (pattern &optional face frame)
1658 "Return a font name matching PATTERN.
1659 All wildcards in PATTERN become substantiated.
1660 If PATTERN is nil, return the name of the frame's base font, which never
1661 contains wildcards.
1662 Given optional arguments FACE and FRAME, return a font which is
1663 also the same size as FACE on FRAME, or fail."
1664 (or (symbolp face)
1665 (setq face (face-name face)))
1666 (and (eq frame t)
1667 (setq frame nil))
1668 (if pattern
1669 ;; Note that x-list-fonts has code to handle a face with nil as its font.
1670 (let ((fonts (x-list-fonts pattern face frame 1)))
1671 (or fonts
1672 (if face
1673 (if (string-match "\\*" pattern)
1674 (if (null (face-font face))
1675 (error "No matching fonts are the same height as the frame default font")
1676 (error "No matching fonts are the same height as face `%s'" face))
1677 (if (null (face-font face))
1678 (error "Height of font `%s' doesn't match the frame default font"
1679 pattern)
1680 (error "Height of font `%s' doesn't match face `%s'"
1681 pattern face)))
1682 (error "No fonts match `%s'" pattern)))
1683 (car fonts))
1684 (cdr (assq 'font (frame-parameters (selected-frame))))))
1685
1686
1687 (defun x-frob-font-weight (font which)
1688 (let ((case-fold-search t))
1689 (cond ((string-match x-font-regexp font)
1690 (concat (substring font 0
1691 (match-beginning x-font-regexp-weight-subnum))
1692 which
1693 (substring font (match-end x-font-regexp-weight-subnum)
1694 (match-beginning x-font-regexp-adstyle-subnum))
1695 ;; Replace the ADD_STYLE_NAME field with *
1696 ;; because the info in it may not be the same
1697 ;; for related fonts.
1698 "*"
1699 (substring font (match-end x-font-regexp-adstyle-subnum))))
1700 ((string-match x-font-regexp-head font)
1701 (concat (substring font 0 (match-beginning 1)) which
1702 (substring font (match-end 1))))
1703 ((string-match x-font-regexp-weight font)
1704 (concat (substring font 0 (match-beginning 1)) which
1705 (substring font (match-end 1)))))))
1706
1707
1708 (defun x-frob-font-slant (font which)
1709 (let ((case-fold-search t))
1710 (cond ((string-match x-font-regexp font)
1711 (concat (substring font 0
1712 (match-beginning x-font-regexp-slant-subnum))
1713 which
1714 (substring font (match-end x-font-regexp-slant-subnum)
1715 (match-beginning x-font-regexp-adstyle-subnum))
1716 ;; Replace the ADD_STYLE_NAME field with *
1717 ;; because the info in it may not be the same
1718 ;; for related fonts.
1719 "*"
1720 (substring font (match-end x-font-regexp-adstyle-subnum))))
1721 ((string-match x-font-regexp-head font)
1722 (concat (substring font 0 (match-beginning 2)) which
1723 (substring font (match-end 2))))
1724 ((string-match x-font-regexp-slant font)
1725 (concat (substring font 0 (match-beginning 1)) which
1726 (substring font (match-end 1)))))))
1727
1728
1729 (defun x-make-font-bold (font)
1730 "Given an X font specification, make a bold version of it.
1731 If that can't be done, return nil."
1732 (x-frob-font-weight font "bold"))
1733
1734
1735 (defun x-make-font-demibold (font)
1736 "Given an X font specification, make a demibold version of it.
1737 If that can't be done, return nil."
1738 (x-frob-font-weight font "demibold"))
1739
1740
1741 (defun x-make-font-unbold (font)
1742 "Given an X font specification, make a non-bold version of it.
1743 If that can't be done, return nil."
1744 (x-frob-font-weight font "medium"))
1745
1746
1747 (defun x-make-font-italic (font)
1748 "Given an X font specification, make an italic version of it.
1749 If that can't be done, return nil."
1750 (x-frob-font-slant font "i"))
1751
1752
1753 (defun x-make-font-oblique (font) ; you say tomayto...
1754 "Given an X font specification, make an oblique version of it.
1755 If that can't be done, return nil."
1756 (x-frob-font-slant font "o"))
1757
1758
1759 (defun x-make-font-unitalic (font)
1760 "Given an X font specification, make a non-italic version of it.
1761 If that can't be done, return nil."
1762 (x-frob-font-slant font "r"))
1763
1764
1765 (defun x-make-font-bold-italic (font)
1766 "Given an X font specification, make a bold and italic version of it.
1767 If that can't be done, return nil."
1768 (and (setq font (x-make-font-bold font))
1769 (x-make-font-italic font)))
1770
1771
1772 (provide 'faces)
1773
1774 ;;; end of faces.el