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