]> code.delx.au - gnu-emacs/blob - lisp/faces.el
83af2d40f948d1c42a49e1c2ac715f0285aed471
[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 (put face 'face-documentation string))
412
413
414 (defalias 'face-doc-string 'face-documentation)
415 (defalias 'set-face-doc-string 'set-face-documentation)
416
417
418 \f
419 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
420 ;; Setting face attributes.
421 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
422
423
424 (defun set-face-attribute (face frame &rest args)
425 "Set attributes of FACE on FRAME from ARGS.
426
427 FRAME nil means change attributes on all frames. FRAME t means change
428 the default for new frames (this is done automatically each time an
429 attribute is changed on all frames).
430
431 ARGS must come in pairs ATTRIBUTE VALUE. ATTRIBUTE must be a valid
432 face attribute name. All attributes can be set to `unspecified';
433 this fact is not further mentioned below.
434
435 The following attributes are recognized:
436
437 `:family'
438
439 VALUE must be a string specifying the font family, e.g. ``courier'',
440 or a fontset alias name. If a font family is specified, wild-cards `*'
441 and `?' are allowed.
442
443 `:width'
444
445 VALUE specifies the relative proportionate width of the font to use.
446 It must be one of the symbols `ultra-condensed', `extra-condensed',
447 `condensed', `semi-condensed', `normal', `semi-expanded', `expanded',
448 `extra-expanded', or `ultra-expanded'.
449
450 `:height'
451
452 VALUE must be an integer specifying the height of the font to use in
453 1/10 pt.
454
455 `:weight'
456
457 VALUE specifies the weight of the font to use. It must be one of the
458 symbols `ultra-bold', `extra-bold', `bold', `semi-bold', `normal',
459 `semi-light', `light', `extra-light', `ultra-light'.
460
461 `:slant'
462
463 VALUE specifies the slant of the font to use. It must be one of the
464 symbols `italic', `oblique', `normal', `reverse-italic', or
465 `reverse-oblique'.
466
467 `:foreground', `:background'
468
469 VALUE must be a color name, a string.
470
471 `:underline'
472
473 VALUE specifies whether characters in FACE should be underlined. If
474 VALUE is t, underline with foreground color of the face. If VALUE is
475 a string, underline with that color. If VALUE is nil, explicitly
476 don't underline.
477
478 `:overline'
479
480 VALUE specifies whether characters in FACE should be overlined. If
481 VALUE is t, overline with foreground color of the face. If VALUE is a
482 string, overline with that color. If VALUE is nil, explicitly don't
483 overline.
484
485 `:strike-through'
486
487 VALUE specifies whether characters in FACE should be drawn with a line
488 striking through them. If VALUE is t, use the foreground color of the
489 face. If VALUE is a string, strike-through with that color. If VALUE
490 is nil, explicitly don't strike through.
491
492 `:box'
493
494 VALUE specifies whether characters in FACE should have a box drawn
495 around them. If VALUE is nil, explicitly don't draw boxes. If
496 VALUE is t, draw a box with lines of width 1 in the foreground color
497 of the face. If VALUE is a string, the string must be a color name,
498 and the box is drawn in that color with a line width of 1. Otherwise,
499 VALUE must be a property list of the form `(:line-width WIDTH
500 :color COLOR :style STYLE)'. If a keyword/value pair is missing from
501 the property list, a default value will be used for the value, as
502 specified below. WIDTH specifies the width of the lines to draw; it
503 defaults to 1. COLOR is the name of the color to draw in, default is
504 the foreground color of the face for simple boxes, and the background
505 color of the face for 3D boxes. STYLE specifies whether a 3D box
506 should be draw. If STYLE is `released-button', draw a box looking
507 like a released 3D button. If STYLE is `pressed-button' draw a box
508 that appears like a pressed button. If STYLE is nil, the default if
509 the property list doesn't contain a style specification, draw a 2D
510 box.
511
512 `:inverse-video'
513
514 VALUE specifies whether characters in FACE should be displayed in
515 inverse video. VALUE must be one of t or nil.
516
517 `:stipple'
518
519 If VALUE is a string, it must be the name of a file of pixmap data.
520 The directories listed in the `x-bitmap-file-path' variable are
521 searched. Alternatively, VALUE may be a list of the form (WIDTH
522 HEIGHT DATA) where WIDTH and HEIGHT are the size in pixels, and DATA
523 is a string containing the raw bits of the bitmap. VALUE nil means
524 explicitly don't use a stipple pattern.
525
526 For convenience, attributes `:family', `:width', `:height', `:weight',
527 and `:slant' may also be set in one step from an X font name:
528
529 `:font'
530
531 Set font-related face attributes from VALUE. VALUE must be a valid
532 XLFD font name. If it is a font name pattern, the first matching font
533 will be used.
534
535 For compatibility with Emacs 20, keywords `:bold' and `:italic' can
536 be used to specify that a bold or italic font should be used. VALUE
537 must be t or nil in that case. A value of `unspecified' is not allowed."
538 (cond ((null frame)
539 ;; Change face on all frames.
540 (dolist (frame (frame-list))
541 (apply #'set-face-attribute face frame args))
542 ;; Record that as a default for new frames.
543 (apply #'set-face-attribute face t args))
544 (t
545 (while args
546 (internal-set-lisp-face-attribute face (car args)
547 (car (cdr args)) frame)
548 (setq args (cdr (cdr args)))))))
549
550
551 (defun make-face-bold (face &optional frame)
552 "Make the font of FACE be bold, if possible.
553 FRAME nil or not specified means change face on all frames.
554 Use `set-face-attribute' for finer control of the font weight."
555 (interactive (list (read-face-name "Make which face bold: ")))
556 (set-face-attribute face frame :weight 'bold))
557
558
559 (defun make-face-unbold (face &optional frame)
560 "Make the font of FACE be non-bold, if possible.
561 FRAME nil or not specified means change face on all frames."
562 (interactive (list (read-face-name "Make which face non-bold: ")))
563 (set-face-attribute face frame :weight 'normal))
564
565
566 (defun make-face-italic (face &optional frame)
567 "Make the font of FACE be italic, if possible.
568 FRAME nil or not specified means change face on all frames.
569 Use `set-face-attribute' for finer control of the font slant."
570 (interactive (list (read-face-name "Make which face italic: ")))
571 (set-face-attribute face frame :slant 'italic))
572
573
574 (defun make-face-unitalic (face &optional frame)
575 "Make the font of FACE be non-italic, if possible.
576 FRAME nil or not specified means change face on all frames."
577 (interactive (list (read-face-name "Make which face non-italic: ")))
578 (set-face-attribute face frame :slant 'normal))
579
580
581 (defun make-face-bold-italic (face &optional frame)
582 "Make the font of FACE be bold and italic, if possible.
583 FRAME nil or not specified means change face on all frames.
584 Use `set-face-attribute' for finer control of font weight and slant."
585 (interactive (list (read-face-name "Make which face bold-italic: ")))
586 (set-face-attribute face frame :weight 'bold :slant 'italic))
587
588
589 (defun set-face-font (face font &optional frame)
590 "Change font-related attributes of FACE to those of FONT (a string).
591 FRAME nil or not specified means change face on all frames.
592 This sets the attributes `:family', `:width', `:height', `:weight',
593 and `:slant'. When called interactively, prompt for the face and font."
594 (interactive (read-face-and-attribute :font))
595 (set-face-attribute face frame :font font))
596
597
598 ;; Implementation note: Emulating gray background colors with a
599 ;; stipple pattern is now part of the face realization process, and is
600 ;; done in C depending on the frame on which the face is realized.
601
602 (defun set-face-background (face color &optional frame)
603 "Change the background color of face FACE to COLOR (a string).
604 FRAME nil or not specified means change face on all frames.
605 When called interactively, prompt for the face and color."
606 (interactive (read-face-and-attribute :background))
607 (set-face-attribute face frame :background color))
608
609
610 (defun set-face-foreground (face color &optional frame)
611 "Change the foreground color of face FACE to COLOR (a string).
612 FRAME nil or not specified means change face on all frames.
613 When called interactively, prompt for the face and color."
614 (interactive (read-face-and-attribute :foreground))
615 (set-face-attribute face frame :foreground color))
616
617
618 (defun set-face-stipple (face stipple &optional frame)
619 "Change the stipple pixmap of face FACE to STIPPLE.
620 FRAME nil or not specified means change face on all frames.
621 STIPPLE. should be a string, the name of a file of pixmap data.
622 The directories listed in the `x-bitmap-file-path' variable are searched.
623
624 Alternatively, STIPPLE may be a list of the form (WIDTH HEIGHT DATA)
625 where WIDTH and HEIGHT are the size in pixels,
626 and DATA is a string, containing the raw bits of the bitmap."
627 (interactive (read-face-and-attribute :stipple))
628 (set-face-attribute face frame :stipple stipple))
629
630
631 (defun set-face-underline (face underline &optional frame)
632 "Specify whether face FACE is underlined.
633 UNDERLINE nil means FACE explicitly doesn't underline.
634 UNDERLINE non-nil means FACE explicitly does underlining
635 with the same of the foreground color.
636 If UNDERLINE is a string, underline with the color named UNDERLINE.
637 FRAME nil or not specified means change face on all frames.
638 Use `set-face-attribute' to ``unspecify'' underlining."
639 (interactive
640 (let ((list (read-face-and-attribute :underline)))
641 (list (car list) (eq (car (cdr list)) t))))
642 (set-face-attribute face frame :underline underline))
643
644
645 (defun set-face-underline-p (face underline-p &optional frame)
646 "Specify whether face FACE is underlined.
647 UNDERLINE-P nil means FACE explicitly doesn't underline.
648 UNDERLINE-P non-nil means FACE explicitly does underlining.
649 FRAME nil or not specified means change face on all frames.
650 Use `set-face-attribute' to ``unspecify'' underlining."
651 (interactive
652 (let ((list (read-face-and-attribute :underline)))
653 (list (car list) (eq (car (cdr list)) t))))
654 (set-face-attribute face frame :underline underline-p))
655
656
657 (defun set-face-inverse-video-p (face inverse-video-p &optional frame)
658 "Specify whether face FACE is in inverse video.
659 INVERSE-VIDEO-P non-nil means FACE displays explicitly in inverse video.
660 INVERSE-VIDEO-P nil means FACE explicitly is not in inverse video.
661 FRAME nil or not specified means change face on all frames.
662 Use `set-face-attribute' to ``unspecify'' the inverse video attribute."
663 (interactive
664 (let ((list (read-face-and-attribute :inverse-video)))
665 (list (car list) (eq (car (cdr list)) t))))
666 (set-face-attribute face frame :inverse-video inverse-video-p))
667
668
669 (defun set-face-bold-p (face bold-p &optional frame)
670 "Specify whether face FACE is bold.
671 BOLD-P non-nil means FACE should explicitly display bold.
672 BOLD-P nil means FACE should explicitly display non-bold.
673 FRAME nil or not specified means change face on all frames.
674 Use `set-face-attribute' or `modify-face' for finer control."
675 (if (null bold-p)
676 (make-face-unbold face frame)
677 (make-face-bold face frame)))
678
679
680 (defun set-face-italic-p (face italic-p &optional frame)
681 "Specify whether face FACE is italic.
682 ITALIC-P non-nil means FACE should explicitly display italic.
683 ITALIC-P nil means FACE should explicitly display non-italic.
684 FRAME nil or not specified means change face on all frames.
685 Use `set-face-attribute' or `modify-face' for finer control."
686 (if (null italic-p)
687 (make-face-unitalic face frame)
688 (make-face-italic face frame)))
689
690
691 (defalias 'set-face-background-pixmap 'set-face-stipple)
692
693
694 (defun invert-face (face &optional frame)
695 "Swap the foreground and background colors of FACE.
696 FRAME nil or not specified means change face on all frames.
697 If FACE specifies neither foreground nor background color,
698 set its foreground and background to the background and foreground
699 of the default face. Value is FACE."
700 (interactive (list (read-face-name "Invert face: ")))
701 (let ((fg (face-attribute face :foreground frame))
702 (bg (face-attribute face :background frame)))
703 (if (or fg bg)
704 (set-face-attribute face frame :foreground bg :background fg)
705 (set-face-attribute face frame
706 :foreground
707 (face-attribute 'default :background frame)
708 :background
709 (face-attribute 'default :foreground frame))))
710 face)
711
712 \f
713 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
714 ;;; Interactively modifying faces.
715 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
716
717 (defun read-face-name (prompt)
718 "Read and return a face symbol, prompting with PROMPT.
719 Value is a symbol naming a known face."
720 (let ((face-list (mapcar #'(lambda (x) (cons (symbol-name x) x))
721 (face-list)))
722 face)
723 (while (equal "" (setq face (completing-read prompt face-list nil t))))
724 (intern face)))
725
726
727 (defun face-valid-attribute-values (attribute &optional frame)
728 "Return valid values for face attribute ATTRIBUTE.
729 The optional argument FRAME is used to determine available fonts
730 and colors. If it is nil or not specified, the selected frame is
731 used. Value is an alist of (NAME . VALUE) if ATTRIBUTE expects a value
732 out of a set of discrete values. Value is `integerp' if ATTRIBUTE expects
733 an integer value."
734 (let (valid)
735 (setq valid
736 (case attribute
737 (:family
738 (if window-system
739 (mapcar #'(lambda (x) (cons (car x) (car x)))
740 (x-font-family-list))
741 ;; Only one font on TTYs.
742 (list (cons "default" "default"))))
743 ((:width :weight :slant :inverse-video)
744 (mapcar #'(lambda (x) (cons (symbol-name x) x))
745 (internal-lisp-face-attribute-values attribute)))
746 ((:underline :overline :strike-through :box)
747 (if window-system
748 (nconc (mapcar #'(lambda (x) (cons (symbol-name x) x))
749 (internal-lisp-face-attribute-values attribute))
750 (mapcar #'(lambda (c) (cons c c))
751 (x-defined-colors frame)))
752 (mapcar #'(lambda (x) (cons (symbol-name x) x))
753 (internal-lisp-face-attribute-values attribute))))
754 ((:foreground :background)
755 (mapcar #'(lambda (c) (cons c c))
756 (or (and window-system (x-defined-colors frame))
757 (tty-defined-colors))))
758 ((:height)
759 'integerp)
760 (:stipple
761 (and (memq window-system '(x w32))
762 (mapcar #'list
763 (apply #'nconc (mapcar #'directory-files
764 x-bitmap-file-path)))))
765 (t
766 (error "Internal error"))))
767 (if (listp valid)
768 (nconc (list (cons "unspecified" 'unspecified)) valid)
769 valid)))
770
771
772
773 (defvar face-attribute-name-alist
774 '((:family . "font family")
775 (:width . "character set width")
776 (:height . "height in 1/10 pt")
777 (:weight . "weight")
778 (:slant . "slant")
779 (:underline . "underline")
780 (:overline . "overline")
781 (:strike-through . "strike-through")
782 (:box . "box")
783 (:inverse-video . "inverse-video display")
784 (:foreground . "foreground color")
785 (:background . "background color")
786 (:stipple . "background stipple"))
787 "An alist of descriptive names for face attributes.
788 Each element has the form (ATTRIBUTE-NAME . DESCRIPTION) where
789 ATTRIBUTE-NAME is a face attribute name (a keyword symbol), and
790 DESCRIPTION is a descriptive name for ATTRIBUTE-NAME.")
791
792
793 (defun face-descriptive-attribute-name (attribute)
794 "Return a descriptive name for ATTRIBUTE."
795 (cdr (assq attribute face-attribute-name-alist)))
796
797
798 (defun face-read-string (face default name &optional completion-alist)
799 "Interactively read a face attribute string value.
800 FACE is the face whose attribute is read. DEFAULT is the default
801 value to return if no new value is entered. NAME is a descriptive
802 name of the attribute for prompting. COMPLETION-ALIST is an alist
803 of valid values, if non-nil.
804
805 Entering nothing accepts the default value DEFAULT.
806 Value is the new attribute value."
807 (let* ((completion-ignore-case t)
808 (value (completing-read
809 (if default
810 (format "Set face %s %s (default %s): "
811 face name (downcase (if (symbolp default)
812 (symbol-name default)
813 default)))
814 (format "Set face %s %s: " face name))
815 completion-alist)))
816 (if (equal value "") default value)))
817
818
819 (defun face-read-integer (face default name)
820 "Interactively read an integer face attribute value.
821 FACE is the face whose attribute is read. DEFAULT is the default
822 value to return if no new value is entered. NAME is a descriptive
823 name of the attribute for prompting. Value is the new attribute value."
824 (let ((new-value
825 (face-read-string face
826 (if (eq default 'unspecified)
827 'unspecified
828 (int-to-string default))
829 name
830 (list (cons "unspecified" 'unspecified)))))
831 (if (eq new-value 'unspecified)
832 new-value
833 (string-to-int new-value))))
834
835
836 (defun read-face-attribute (face attribute &optional frame)
837 "Interactively read a new value for FACE's ATTRIBUTE.
838 Optional argument FRAME nil or unspecified means read an attribute value
839 of a global face. Value is the new attribute value."
840 (let* ((old-value (face-attribute face attribute frame))
841 (attribute-name (face-descriptive-attribute-name attribute))
842 (valid (face-valid-attribute-values attribute frame))
843 new-value)
844 ;; Represent complex attribute values as strings by printing them
845 ;; out. Stipple can be a vector; (WIDTH HEIGHT DATA). Box can be
846 ;; a list `(:width WIDTH :color COLOR)' or `(:width WIDTH :shadow
847 ;; SHADOW)'.
848 (when (and (or (eq attribute :stipple)
849 (eq attribute :box))
850 (or (consp old-value)
851 (vectorp old-value)))
852 (setq old-value (prin1-to-string old-value)))
853 (cond ((listp valid)
854 (setq new-value
855 (face-read-string face old-value attribute-name valid))
856 (unless (eq new-value 'unspecified)
857 (setq new-value (cdr (assoc new-value valid)))))
858 ((eq valid 'integerp)
859 (setq new-value (face-read-integer face old-value attribute-name)))
860 (t (error "Internal error")))
861 ;; Convert stipple and box value text we read back to a list or
862 ;; vector if it looks like one. This makes the assumption that a
863 ;; pixmap file name won't start with an open-paren.
864 (when (and (or (eq attribute :stipple)
865 (eq attribute :box))
866 (stringp new-value)
867 (string-match "^[[(]" new-value))
868 (setq new-value (read new-value)))
869 new-value))
870
871
872 (defun read-face-font (face &optional frame)
873 "Read the name of a font for FACE on FRAME.
874 If optional argument FRAME Is nil or omitted, use the selected frame."
875 (let ((completion-ignore-case t))
876 (completing-read "Set font attributes of face %s from font: "
877 face (x-list-fonts "*" nil frame))))
878
879
880 (defun read-all-face-attributes (face &optional frame)
881 "Interactively read all attributes for FACE.
882 If optional argument FRAME Is nil or omitted, use the selected frame.
883 Value is a property list of attribute names and new values."
884 (let (result)
885 (dolist (attribute face-attribute-name-alist result)
886 (setq result (cons (car attribute)
887 (cons (read-face-attribute face (car attribute) frame)
888 result))))))
889
890
891 (defun modify-face (&optional frame)
892 "Modify attributes of faces interactively.
893 If optional argument FRAME is nil or omitted, modify the face used
894 for newly created frame, i.e. the global face."
895 (interactive)
896 (let ((face (read-face-name "Modify face: ")))
897 (apply #'set-face-attribute face frame
898 (read-all-face-attributes face frame))))
899
900
901 (defun read-face-and-attribute (attribute &optional frame)
902 "Read face name and face attribute value.
903 ATTRIBUTE is the attribute whose new value is read.
904 FRAME nil or unspecified means read attribute value of global face.
905 Value is a list (FACE NEW-VALUE) where FACE is the face read
906 (a symbol), and NEW-VALUE is value read."
907 (cond ((eq attribute :font)
908 (let* ((prompt (format "Set font-related attributes of face: "))
909 (face (read-face-name prompt))
910 (font (read-face-font face frame)))
911 (list face font)))
912 (t
913 (let* ((attribute-name (face-descriptive-attribute-name attribute))
914 (prompt (format "Set %s of face: " attribute-name))
915 (face (read-face-name prompt))
916 (new-value (read-face-attribute face attribute frame)))
917 (list face new-value)))))
918
919
920 \f
921 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
922 ;;; Listing faces.
923 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
924
925 (defvar list-faces-sample-text
926 "abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ"
927 "*Text string to display as the sample text for `list-faces-display'.")
928
929
930 ;; The name list-faces would be more consistent, but let's avoid a
931 ;; conflict with Lucid, which uses that name differently.
932
933 (defun list-faces-display ()
934 "List all faces, using the same sample text in each.
935 The sample text is a string that comes from the variable
936 `list-faces-sample-text'."
937 (interactive)
938 (let ((faces (sort (face-list) #'string-lessp))
939 (face nil)
940 (frame (selected-frame))
941 disp-frame window)
942 (with-output-to-temp-buffer "*Faces*"
943 (save-excursion
944 (set-buffer standard-output)
945 (setq truncate-lines t)
946 (while faces
947 (setq face (car faces))
948 (setq faces (cdr faces))
949 (insert (format "%25s " (face-name face)))
950 (let ((beg (point)))
951 (insert list-faces-sample-text)
952 (insert "\n")
953 (put-text-property beg (1- (point)) 'face face)
954 ;; If the sample text has multiple lines, line up all of them.
955 (goto-char beg)
956 (forward-line 1)
957 (while (not (eobp))
958 (insert " ")
959 (forward-line 1))))
960 (goto-char (point-min)))
961 (print-help-return-message))
962 ;; If the *Faces* buffer appears in a different frame,
963 ;; copy all the face definitions from FRAME,
964 ;; so that the display will reflect the frame that was selected.
965 (setq window (get-buffer-window (get-buffer "*Faces*") t))
966 (setq disp-frame (if window (window-frame window)
967 (car (frame-list))))
968 (or (eq frame disp-frame)
969 (let ((faces (face-list)))
970 (while faces
971 (copy-face (car faces) (car faces) frame disp-frame)
972 (setq faces (cdr faces)))))))
973
974
975 (defun describe-face (face &optional frame)
976 "Display the properties of face FACE on FRAME.
977 If the optional argument FRAME is given, report on face FACE in that frame.
978 If FRAME is t, report on the defaults for face FACE (for new frames).
979 If FRAME is omitted or nil, use the selected frame."
980 (interactive (list (read-face-name "Describe face: ")))
981 (let* ((attrs '((:family . "Family")
982 (:width . "Width")
983 (:height . "Height")
984 (:weight . "Weight")
985 (:slant . "Slant")
986 (:foreground . "Foreground")
987 (:background . "Background")
988 (:underline . "Underline")
989 (:overline . "Overline")
990 (:strike-through . "Strike-through")
991 (:box . "Box")
992 (:inverse-video . "Inverse")
993 (:stipple . "Stipple")))
994 (max-width (apply #'max (mapcar #'(lambda (x) (length (cdr x)))
995 attrs))))
996 (with-output-to-temp-buffer "*Help*"
997 (save-excursion
998 (set-buffer standard-output)
999 (dolist (a attrs)
1000 (let ((attr (face-attribute face (car a) frame)))
1001 (insert (make-string (- max-width (length (cdr a))) ?\ )
1002 (cdr a) ": " (format "%s" attr) "\n")))
1003 (insert "\nDocumentation:\n\n"
1004 (or (face-documentation face)
1005 "not documented as a face.")))
1006 (print-help-return-message))))
1007
1008
1009
1010 \f
1011 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1012 ;;; Face specifications (defface).
1013 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1014
1015 ;; Parameter FRAME Is kept for call compatibility to with previous
1016 ;; face implementation.
1017
1018 (defun face-attr-construct (face &optional frame)
1019 "Return a defface-style attribute list for FACE on FRAME.
1020 Value is a property list of pairs ATTRIBUTE VALUE for all specified
1021 face attributes of FACE where ATTRIBUTE is the attribute name and
1022 VALUE is the specified value of that attribute."
1023 (let (result)
1024 (dolist (entry face-attribute-name-alist result)
1025 (let* ((attribute (car entry))
1026 (value (face-attribute face attribute)))
1027 (unless (eq value 'unspecified)
1028 (setq result (nconc (list attribute value) result)))))))
1029
1030
1031 (defun face-spec-set-match-display (display frame)
1032 "Non-nil if DISPLAY matches FRAME.
1033 DISPLAY is part of a spec such as can be used in `defface'.
1034 If FRAME is nil, the current FRAME is used."
1035 (let* ((conjuncts display)
1036 conjunct req options
1037 ;; t means we have succeeded against all the conjuncts in
1038 ;; DISPLAY that have been tested so far.
1039 (match t))
1040 (if (eq conjuncts t)
1041 (setq conjuncts nil))
1042 (while (and conjuncts match)
1043 (setq conjunct (car conjuncts)
1044 conjuncts (cdr conjuncts)
1045 req (car conjunct)
1046 options (cdr conjunct)
1047 match (cond ((eq req 'type)
1048 (or (memq window-system options)
1049 (and (null window-system)
1050 (memq 'tty options))
1051 (and (memq 'motif options)
1052 (featurep 'motif))
1053 (and (memq 'lucid options)
1054 (featurep 'x-toolkit)
1055 (not (featurep 'motif)))
1056 (and (memq 'x-toolkit options)
1057 (featurep 'x-toolkit))))
1058 ((eq req 'class)
1059 (memq (frame-parameter frame 'display-type) options))
1060 ((eq req 'background)
1061 (memq (frame-parameter frame 'background-mode)
1062 options))
1063 (t (error "Unknown req `%S' with options `%S'"
1064 req options)))))
1065 match))
1066
1067
1068 (defun face-spec-choose (spec &optional frame)
1069 "Choose the proper attributes for FRAME, out of SPEC."
1070 (unless frame
1071 (setq frame (selected-frame)))
1072 (let ((tail spec)
1073 result)
1074 (while tail
1075 (let* ((entry (car tail))
1076 (display (nth 0 entry))
1077 (attrs (nth 1 entry)))
1078 (setq tail (cdr tail))
1079 (when (face-spec-set-match-display display frame)
1080 (setq result attrs tail nil))))
1081 result))
1082
1083
1084 (defun face-spec-reset-face (face &optional frame)
1085 "Reset all attributes of FACE on FRAME to unspecified."
1086 (let ((attrs face-attribute-name-alist)
1087 params)
1088 (while attrs
1089 (let ((attr-and-name (car attrs)))
1090 (setq params (cons (car attr-and-name) (cons 'unspecified params))))
1091 (setq attrs (cdr attrs)))
1092 (apply #'set-face-attribute face frame params)))
1093
1094
1095 (defun face-spec-set (face spec &optional frame)
1096 "Set FACE's attributes according to the first matching entry in SPEC.
1097 FRAME is the frame whose frame-local face is set. FRAME nil means
1098 do it on all frames. See `defface' for information about SPEC."
1099 (let ((attrs (face-spec-choose spec frame))
1100 params)
1101 (while attrs
1102 (let ((attribute (car attrs))
1103 (value (car (cdr attrs))))
1104 ;; Support some old-style attribute names and values.
1105 (case attribute
1106 (:bold (setq attribute :weight value (if value 'bold 'normal)))
1107 (:italic (setq attribute :slant value (if value 'italic 'normal))))
1108 (setq params (cons attribute (cons value params))))
1109 (setq attrs (cdr (cdr attrs))))
1110 (face-spec-reset-face face frame)
1111 (apply #'set-face-attribute face frame params)))
1112
1113
1114 (defun face-attr-match-p (face attrs &optional frame)
1115 "Value is non-nil if attributes of FACE match values in plist ATTRS.
1116 Optional parameter FRAME is the frame whose definition of FACE
1117 is used. If nil or omitted, use the selected frame."
1118 (unless frame
1119 (setq frame (selected-frame)))
1120 (let ((list face-attribute-name-alist)
1121 (match t))
1122 (while (and match (not (null list)))
1123 (let* ((attr (car (car list)))
1124 (specified-value (plist-get attrs attr))
1125 (value-now (face-attribute face attr frame)))
1126 (when specified-value
1127 (setq match (equal specified-value value-now)))
1128 (setq list (cdr list))))
1129 match))
1130
1131
1132 (defun face-spec-match-p (face spec &optional frame)
1133 "Return t if FACE, on FRAME, matches what SPEC says it should look like."
1134 (face-attr-match-p face (face-spec-choose spec frame) frame))
1135
1136
1137 \f
1138 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1139 ;;; Background mode.
1140 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1141
1142 (defcustom frame-background-mode nil
1143 "*The brightness of the background.
1144 Set this to the symbol `dark' if your background color is dark, `light' if
1145 your background is light, or nil (default) if you want Emacs to
1146 examine the brightness for you."
1147 :group 'faces
1148 :set #'(lambda (var value)
1149 (set var value)
1150 (mapcar 'frame-set-background-mode (frame-list)))
1151 :initialize 'custom-initialize-changed
1152 :type '(choice (choice-item dark)
1153 (choice-item light)
1154 (choice-item :tag "default" nil)))
1155
1156
1157 (defun frame-set-background-mode (frame)
1158 "Set up the `background-mode' and `display-type' frame parameters for FRAME."
1159 (let* ((bg-resource
1160 (and window-system
1161 (x-get-resource ".backgroundMode" "BackgroundMode")))
1162 (params (frame-parameters frame))
1163 (bg-mode (cond (frame-background-mode)
1164 ((null window-system)
1165 ;; No way to determine this automatically (?).
1166 'dark)
1167 (bg-resource
1168 (intern (downcase bg-resource)))
1169 ((< (apply '+ (x-color-values
1170 (cdr (assq 'background-color
1171 params))
1172 frame))
1173 ;; Just looking at the screen, colors whose
1174 ;; values add up to .6 of the white total
1175 ;; still look dark to me.
1176 (* (apply '+ (x-color-values "white" frame)) .6))
1177 'dark)
1178 (t 'light)))
1179 (display-type (cond ((null window-system)
1180 (if (tty-display-color-p) 'color 'mono))
1181 ((x-display-color-p frame)
1182 'color)
1183 ((x-display-grayscale-p frame)
1184 'grayscale)
1185 (t 'mono))))
1186 (modify-frame-parameters frame
1187 (list (cons 'background-mode bg-mode)
1188 (cons 'display-type display-type))))
1189
1190 ;; For all named faces, choose face specs matching the new frame
1191 ;; parameters.
1192 (let ((face-list (face-list)))
1193 (while face-list
1194 (let* ((face (car face-list))
1195 (spec (get face 'face-defface-spec)))
1196 (when spec
1197 (face-spec-set face spec frame))
1198 (setq face-list (cdr face-list))))))
1199
1200
1201
1202 \f
1203 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1204 ;;; Frame creation.
1205 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1206
1207 (defun x-handle-named-frame-geometry (parameters)
1208 "Add geometry parameters for a named frame to parameter list PARAMETERS.
1209 Value is the new parameter list."
1210 (let* ((name (or (cdr (assq 'name parameters))
1211 (cdr (assq 'name default-frame-alist))))
1212 (x-resource-name name)
1213 (res-geometry (if name (x-get-resource "geometry" "Geometry"))))
1214 (when res-geometry
1215 (let ((parsed (x-parse-geometry res-geometry)))
1216 ;; If the resource specifies a position, call the position
1217 ;; and size "user-specified".
1218 (when (or (assq 'top parsed)
1219 (assq 'left parsed))
1220 (setq parsed (append '((user-position . t) (user-size . t)) parsed)))
1221 ;; Put the geometry parameters at the end. Copy
1222 ;; default-frame-alist so that they go after it.
1223 (setq parameters (append parameters default-frame-alist parsed))))
1224 parameters))
1225
1226
1227 (defun x-handle-reverse-video (frame parameters)
1228 "Handle the reverse-video frame parameter and X resource.
1229 `x-create-frame' does not handle this one."
1230 (when (cdr (or (assq 'reverse parameters)
1231 (assq 'reverse default-frame-alist)
1232 (let ((resource (x-get-resource "reverseVideo"
1233 "ReverseVideo")))
1234 (if resource
1235 (cons nil (member (downcase resource)
1236 '("on" "true")))))))
1237 (let* ((params (frame-parameters frame))
1238 (bg (cdr (assq 'foreground-color params)))
1239 (fg (cdr (assq 'background-color params))))
1240 (modify-frame-parameters frame
1241 (list (cons 'foreground-color fg)
1242 (cons 'background-color bg)))
1243 (if (equal bg (cdr (assq 'border-color params)))
1244 (modify-frame-parameters frame
1245 (list (cons 'border-color fg))))
1246 (if (equal bg (cdr (assq 'mouse-color params)))
1247 (modify-frame-parameters frame
1248 (list (cons 'mouse-color fg))))
1249 (if (equal bg (cdr (assq 'cursor-color params)))
1250 (modify-frame-parameters frame
1251 (list (cons 'cursor-color fg)))))))
1252
1253
1254 (defun x-create-frame-with-faces (&optional parameters)
1255 "Create a frame from optional frame parameters PARAMETERS.
1256 Parameters not specified by PARAMETERS are taken from
1257 `default-frame-alist'. If PARAMETERS specify a frame name,
1258 handle X geometry resources for that name. If either PARAMETERS
1259 or `default-frame-alist' contains a `reverse' parameter, or
1260 the X resource ``reverseVideo'' is present, handle that.
1261 Value is the new frame created."
1262 (setq parameters (x-handle-named-frame-geometry parameters))
1263 (let ((visibility-spec (assq 'visibility parameters))
1264 (frame-list (frame-list))
1265 (frame (x-create-frame (cons '(visibility . nil) parameters)))
1266 success)
1267 (unwind-protect
1268 (progn
1269 (x-handle-reverse-video frame parameters)
1270 (frame-set-background-mode frame)
1271 (face-set-after-frame-default frame)
1272 (if (or (null frame-list) (null visibility-spec))
1273 (make-frame-visible frame)
1274 (modify-frame-parameters frame (list visibility-spec)))
1275 (setq success t))
1276 (unless success
1277 (delete-frame frame)))
1278 frame))
1279
1280
1281 (defun face-set-after-frame-default (frame)
1282 "Set frame-local faces of FRAME from face specs and resources.
1283 Initialize colors of certain faces from frame parameters."
1284 (dolist (face (face-list))
1285 (let ((spec (or (get face 'saved-face)
1286 (get face 'face-defface-spec))))
1287 (when spec
1288 (face-spec-set face spec frame))
1289 (internal-merge-in-global-face face frame)
1290 (when (memq window-system '(x w32))
1291 (make-face-x-resource-internal face frame))))
1292
1293 ;; Initialize attributes from frame parameters.
1294 (let ((params '((foreground-color default :foreground)
1295 (background-color default :background)
1296 (border-color border :background)
1297 (cursor-color cursor :background)
1298 (scroll-bar-foreground scroll-bar :foreground)
1299 (scroll-bar-background scroll-bar :background)
1300 (mouse-color mouse :background))))
1301 (while params
1302 (let ((param-name (nth 0 (car params)))
1303 (face (nth 1 (car params)))
1304 (attr (nth 2 (car params)))
1305 value)
1306 (when (setq value (frame-parameter frame param-name))
1307 (set-face-attribute face frame attr value)))
1308 (setq params (cdr params)))))
1309
1310
1311 (defun tty-create-frame-with-faces (&optional parameters)
1312 "Create a frame from optional frame parameters PARAMETERS.
1313 Parameters not specified by PARAMETERS are taken from
1314 `default-frame-alist'. If either PARAMETERS or `default-frame-alist'
1315 contains a `reverse' parameter, handle that. Value is the new frame
1316 created."
1317 (let ((frame (make-terminal-frame parameters))
1318 success)
1319 (unwind-protect
1320 (progn
1321 (frame-set-background-mode frame)
1322 (face-set-after-frame-default frame)
1323 (setq success t))
1324 (unless success
1325 (delete-frame frame)))
1326 frame))
1327
1328
1329 ;; Called from C function init_display to initialize faces of the
1330 ;; dumped terminal frame on startup.
1331
1332 (defun tty-set-up-initial-frame-faces ()
1333 (let ((frame (selected-frame)))
1334 (frame-set-background-mode frame)
1335 (face-set-after-frame-default frame)))
1336
1337
1338
1339 \f
1340 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1341 ;;; Compatiblity with 20.2
1342 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1343
1344 ;; Update a frame's faces when we change its default font.
1345
1346 (defun frame-update-faces (frame)
1347 nil)
1348
1349
1350 ;; Update the colors of FACE, after FRAME's own colors have been
1351 ;; changed.
1352
1353 (defun frame-update-face-colors (frame)
1354 (frame-set-background-mode frame))
1355
1356
1357 \f
1358 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1359 ;;; Standard faces.
1360 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1361
1362 (defgroup basic-faces nil
1363 "The standard faces of Emacs."
1364 :group 'faces)
1365
1366
1367 (defface default
1368 '((t nil))
1369 "Basic default face."
1370 :group 'basic-faces)
1371
1372
1373 (defface mode-line
1374 '((((type x) (class color))
1375 (:box (:line-width 2 :style released-button) :background "grey75"))
1376 (t
1377 (:inverse-video t)))
1378 "Basic mode line face."
1379 :version "21.1"
1380 :group 'basic-faces)
1381
1382 ;; Make `modeline' an alias for `mode-line', for compatibility.
1383 (put 'modeline 'face-alias 'mode-line)
1384
1385 (defface header-line
1386 '((((type x) (class color))
1387 (:box (:line-width 2 :style released-button) :background "grey75"))
1388 (t
1389 (:inverse-video t)))
1390 "Basic header-line face."
1391 :version "21.1"
1392 :group 'basic-faces)
1393
1394
1395 (defface tool-bar
1396 '((((type x) (class color))
1397 (:box (:line-width 1 :style released-button) :background "grey75"))
1398 (((type x) (class mono))
1399 (:box (:line-width 1 :style released-button) :background "grey"))
1400 (t
1401 ()))
1402 "Basic tool-bar face."
1403 :version "21.1"
1404 :group 'basic-faces)
1405
1406
1407 (defface region
1408 '((((type tty) (class color))
1409 (:background "blue" :foreground "white"))
1410 (((type tty) (class mono))
1411 (:inverse-video t))
1412 (((class color) (background dark))
1413 (:background "blue"))
1414 (((class color) (background light))
1415 (:background "lightblue"))
1416 (t (:background "gray")))
1417 "Basic face for highlighting the region."
1418 :group 'basic-faces)
1419
1420
1421 (defface fringe
1422 '((((class color))
1423 (:background "grey95"))
1424 (t
1425 (:background "gray")))
1426 "Basic face for the fringes to the left and right of windows under X."
1427 :version "21.1"
1428 :group 'basic-faces)
1429
1430
1431 (defface scroll-bar '()
1432 "Basic face for the scroll bar colors under X."
1433 :version "21.1"
1434 :group 'basic-faces)
1435
1436
1437 (defface menu
1438 '((((type x-toolkit)) ())
1439 (t (:inverse-video t)))
1440 "Basic menu face."
1441 :version "21.1"
1442 :group 'basic-faces)
1443
1444
1445 (defface border '()
1446 "Basic face for the frame border under X."
1447 :version "21.1"
1448 :group 'basic-faces)
1449
1450
1451 (defface cursor '()
1452 "Basic face for the cursor color under X."
1453 :version "21.1"
1454 :group 'basic-faces)
1455
1456
1457 (defface mouse '()
1458 "Basic face for the mouse color under X."
1459 :version "21.1"
1460 :group 'basic-faces)
1461
1462
1463 (defface bold '((t (:weight bold)))
1464 "Basic bold face."
1465 :group 'basic-faces)
1466
1467
1468 (defface italic '((t (:slant italic)))
1469 "Basic italic font."
1470 :group 'basic-faces)
1471
1472
1473 (defface bold-italic '((t (:weight bold :slant italic)))
1474 "Basic bold-italic face."
1475 :group 'basic-faces)
1476
1477
1478 (defface underline '((t (:underline t)))
1479 "Basic underlined face."
1480 :group 'basic-faces)
1481
1482
1483 (defface highlight
1484 '((((type tty) (class color))
1485 (:background "green"))
1486 (((class color) (background light))
1487 (:background "darkseagreen2"))
1488 (((class color) (background dark))
1489 (:background "darkolivegreen"))
1490 (t (:inverse-video t)))
1491 "Basic face for highlighting."
1492 :group 'basic-faces)
1493
1494
1495 (defface secondary-selection
1496 '((((type tty) (class color))
1497 (:background "cyan"))
1498 (((class color) (background light))
1499 (:background "paleturquoise"))
1500 (((class color) (background dark))
1501 (:background "darkslateblue"))
1502 (t (:inverse-video t)))
1503 "Basic face for displaying the secondary selection."
1504 :group 'basic-faces)
1505
1506
1507 (defface fixed-pitch '((t (:family "courier*")))
1508 "The basic fixed-pitch face."
1509 :group 'basic-faces)
1510
1511
1512 (defface variable-pitch '((t (:family "helv*")))
1513 "The basic variable-pitch face."
1514 :group 'basic-faces)
1515
1516
1517 (defface trailing-whitespace
1518 '((((class color) (background light))
1519 (:background "red"))
1520 (((class color) (background dark))
1521 (:background "red"))
1522 (t (:inverse-video t)))
1523 "Basic face for highlighting trailing whitespace."
1524 :version "21.1"
1525 :group 'basic-faces)
1526
1527
1528 \f
1529 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1530 ;;; Manipulating font names.
1531 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1532
1533 ;; This is here for compatibilty with Emacs 20.2. For example,
1534 ;; international/fontset.el uses these functions to manipulate font
1535 ;; names. The following functions are not used in the face
1536 ;; implementation itself.
1537
1538 (defvar x-font-regexp nil)
1539 (defvar x-font-regexp-head nil)
1540 (defvar x-font-regexp-weight nil)
1541 (defvar x-font-regexp-slant nil)
1542
1543 (defconst x-font-regexp-weight-subnum 1)
1544 (defconst x-font-regexp-slant-subnum 2)
1545 (defconst x-font-regexp-swidth-subnum 3)
1546 (defconst x-font-regexp-adstyle-subnum 4)
1547
1548 ;;; Regexps matching font names in "Host Portable Character Representation."
1549 ;;;
1550 (let ((- "[-?]")
1551 (foundry "[^-]+")
1552 (family "[^-]+")
1553 (weight "\\(bold\\|demibold\\|medium\\)") ; 1
1554 ; (weight\? "\\(\\*\\|bold\\|demibold\\|medium\\|\\)") ; 1
1555 (weight\? "\\([^-]*\\)") ; 1
1556 (slant "\\([ior]\\)") ; 2
1557 ; (slant\? "\\([ior?*]?\\)") ; 2
1558 (slant\? "\\([^-]?\\)") ; 2
1559 ; (swidth "\\(\\*\\|normal\\|semicondensed\\|\\)") ; 3
1560 (swidth "\\([^-]*\\)") ; 3
1561 ; (adstyle "\\(\\*\\|sans\\|\\)") ; 4
1562 (adstyle "\\([^-]*\\)") ; 4
1563 (pixelsize "[0-9]+")
1564 (pointsize "[0-9][0-9]+")
1565 (resx "[0-9][0-9]+")
1566 (resy "[0-9][0-9]+")
1567 (spacing "[cmp?*]")
1568 (avgwidth "[0-9]+")
1569 (registry "[^-]+")
1570 (encoding "[^-]+")
1571 )
1572 (setq x-font-regexp
1573 (concat "\\`\\*?[-?*]"
1574 foundry - family - weight\? - slant\? - swidth - adstyle -
1575 pixelsize - pointsize - resx - resy - spacing - avgwidth -
1576 registry - encoding "\\*?\\'"
1577 ))
1578 (setq x-font-regexp-head
1579 (concat "\\`[-?*]" foundry - family - weight\? - slant\?
1580 "\\([-*?]\\|\\'\\)"))
1581 (setq x-font-regexp-slant (concat - slant -))
1582 (setq x-font-regexp-weight (concat - weight -))
1583 nil)
1584
1585
1586 (defun x-resolve-font-name (pattern &optional face frame)
1587 "Return a font name matching PATTERN.
1588 All wildcards in PATTERN become substantiated.
1589 If PATTERN is nil, return the name of the frame's base font, which never
1590 contains wildcards.
1591 Given optional arguments FACE and FRAME, return a font which is
1592 also the same size as FACE on FRAME, or fail."
1593 (or (symbolp face)
1594 (setq face (face-name face)))
1595 (and (eq frame t)
1596 (setq frame nil))
1597 (if pattern
1598 ;; Note that x-list-fonts has code to handle a face with nil as its font.
1599 (let ((fonts (x-list-fonts pattern face frame 1)))
1600 (or fonts
1601 (if face
1602 (if (string-match "\\*" pattern)
1603 (if (null (face-font face))
1604 (error "No matching fonts are the same height as the frame default font")
1605 (error "No matching fonts are the same height as face `%s'" face))
1606 (if (null (face-font face))
1607 (error "Height of font `%s' doesn't match the frame default font"
1608 pattern)
1609 (error "Height of font `%s' doesn't match face `%s'"
1610 pattern face)))
1611 (error "No fonts match `%s'" pattern)))
1612 (car fonts))
1613 (cdr (assq 'font (frame-parameters (selected-frame))))))
1614
1615
1616 (defun x-frob-font-weight (font which)
1617 (let ((case-fold-search t))
1618 (cond ((string-match x-font-regexp font)
1619 (concat (substring font 0
1620 (match-beginning x-font-regexp-weight-subnum))
1621 which
1622 (substring font (match-end x-font-regexp-weight-subnum)
1623 (match-beginning x-font-regexp-adstyle-subnum))
1624 ;; Replace the ADD_STYLE_NAME field with *
1625 ;; because the info in it may not be the same
1626 ;; for related fonts.
1627 "*"
1628 (substring font (match-end x-font-regexp-adstyle-subnum))))
1629 ((string-match x-font-regexp-head font)
1630 (concat (substring font 0 (match-beginning 1)) which
1631 (substring font (match-end 1))))
1632 ((string-match x-font-regexp-weight font)
1633 (concat (substring font 0 (match-beginning 1)) which
1634 (substring font (match-end 1)))))))
1635
1636
1637 (defun x-frob-font-slant (font which)
1638 (let ((case-fold-search t))
1639 (cond ((string-match x-font-regexp font)
1640 (concat (substring font 0
1641 (match-beginning x-font-regexp-slant-subnum))
1642 which
1643 (substring font (match-end x-font-regexp-slant-subnum)
1644 (match-beginning x-font-regexp-adstyle-subnum))
1645 ;; Replace the ADD_STYLE_NAME field with *
1646 ;; because the info in it may not be the same
1647 ;; for related fonts.
1648 "*"
1649 (substring font (match-end x-font-regexp-adstyle-subnum))))
1650 ((string-match x-font-regexp-head font)
1651 (concat (substring font 0 (match-beginning 2)) which
1652 (substring font (match-end 2))))
1653 ((string-match x-font-regexp-slant font)
1654 (concat (substring font 0 (match-beginning 1)) which
1655 (substring font (match-end 1)))))))
1656
1657
1658 (defun x-make-font-bold (font)
1659 "Given an X font specification, make a bold version of it.
1660 If that can't be done, return nil."
1661 (x-frob-font-weight font "bold"))
1662
1663
1664 (defun x-make-font-demibold (font)
1665 "Given an X font specification, make a demibold version of it.
1666 If that can't be done, return nil."
1667 (x-frob-font-weight font "demibold"))
1668
1669
1670 (defun x-make-font-unbold (font)
1671 "Given an X font specification, make a non-bold version of it.
1672 If that can't be done, return nil."
1673 (x-frob-font-weight font "medium"))
1674
1675
1676 (defun x-make-font-italic (font)
1677 "Given an X font specification, make an italic version of it.
1678 If that can't be done, return nil."
1679 (x-frob-font-slant font "i"))
1680
1681
1682 (defun x-make-font-oblique (font) ; you say tomayto...
1683 "Given an X font specification, make an oblique version of it.
1684 If that can't be done, return nil."
1685 (x-frob-font-slant font "o"))
1686
1687
1688 (defun x-make-font-unitalic (font)
1689 "Given an X font specification, make a non-italic version of it.
1690 If that can't be done, return nil."
1691 (x-frob-font-slant font "r"))
1692
1693
1694 (defun x-make-font-bold-italic (font)
1695 "Given an X font specification, make a bold and italic version of it.
1696 If that can't be done, return nil."
1697 (and (setq font (x-make-font-bold font))
1698 (x-make-font-italic font)))
1699
1700
1701 (provide 'faces)
1702
1703 ;;; end of faces.el