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