]> code.delx.au - gnu-emacs/blob - lisp/faces.el
(frame-update-face-colors): New function.
[gnu-emacs] / lisp / faces.el
1 ;;; faces.el --- Lisp interface to the c "face" structure
2
3 ;; Copyright (C) 1992, 1993, 1994 Free Software Foundation, Inc.
4
5 ;; This file is part of GNU Emacs.
6
7 ;; GNU Emacs is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 2, or (at your option)
10 ;; any later version.
11
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
16
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING. If not, write to
19 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
20
21 ;;; Commentary:
22
23 ;; Mostly derived from Lucid.
24
25 ;;; Code:
26
27 (eval-when-compile
28 ;; These used to be defsubsts, now they're subrs. Avoid losing if we're
29 ;; being compiled with an old Emacs that still has defsubrs in it.
30 (put 'face-name 'byte-optimizer nil)
31 (put 'face-id 'byte-optimizer nil)
32 (put 'face-font 'byte-optimizer nil)
33 (put 'face-foreground 'byte-optimizer nil)
34 (put 'face-background 'byte-optimizer nil)
35 (put 'face-stipple 'byte-optimizer nil)
36 (put 'face-underline-p 'byte-optimizer nil)
37 (put 'set-face-font 'byte-optimizer nil)
38 (put 'set-face-foreground 'byte-optimizer nil)
39 (put 'set-face-background 'byte-optimizer nil)
40 (put 'set-stipple 'byte-optimizer nil)
41 (put 'set-face-underline-p 'byte-optimizer nil))
42 \f
43 ;;;; Functions for manipulating face vectors.
44
45 ;;; A face vector is a vector of the form:
46 ;;; [face NAME ID FONT FOREGROUND BACKGROUND STIPPLE UNDERLINE]
47
48 ;;; Type checkers.
49 (defsubst internal-facep (x)
50 (and (vectorp x) (= (length x) 8) (eq (aref x 0) 'face)))
51
52 (defmacro internal-check-face (face)
53 (` (while (not (internal-facep (, face)))
54 (setq (, face) (signal 'wrong-type-argument (list 'internal-facep (, face)))))))
55
56 ;;; Accessors.
57 (defun face-name (face)
58 "Return the name of face FACE."
59 (aref (internal-get-face face) 1))
60
61 (defun face-id (face)
62 "Return the internal ID number of face FACE."
63 (aref (internal-get-face face) 2))
64
65 (defun face-font (face &optional frame)
66 "Return the font name of face FACE, or nil if it is unspecified.
67 If the optional argument FRAME is given, report on face FACE in that frame.
68 If FRAME is t, report on the defaults for face FACE (for new frames).
69 The font default for a face is either nil, or a list
70 of the form (bold), (italic) or (bold italic).
71 If FRAME is omitted or nil, use the selected frame."
72 (aref (internal-get-face face frame) 3))
73
74 (defun face-foreground (face &optional frame)
75 "Return the foreground color name of face FACE, or nil if unspecified.
76 If the optional argument FRAME is given, report on face FACE in that frame.
77 If FRAME is t, report on the defaults for face FACE (for new frames).
78 If FRAME is omitted or nil, use the selected frame."
79 (aref (internal-get-face face frame) 4))
80
81 (defun face-background (face &optional frame)
82 "Return the background color name of face FACE, or nil if unspecified.
83 If the optional argument FRAME is given, report on face FACE in that frame.
84 If FRAME is t, report on the defaults for face FACE (for new frames).
85 If FRAME is omitted or nil, use the selected frame."
86 (aref (internal-get-face face frame) 5))
87
88 (defun face-stipple (face &optional frame)
89 "Return the stipple pixmap name of face FACE, or nil if unspecified.
90 If the optional argument FRAME is given, report on face FACE in that frame.
91 If FRAME is t, report on the defaults for face FACE (for new frames).
92 If FRAME is omitted or nil, use the selected frame."
93 (aref (internal-get-face face frame) 6))
94
95 (defalias 'face-background-pixmap 'face-stipple)
96
97 (defun face-underline-p (face &optional frame)
98 "Return t if face FACE is underlined.
99 If the optional argument FRAME is given, report on face FACE in that frame.
100 If FRAME is t, report on the defaults for face FACE (for new frames).
101 If FRAME is omitted or nil, use the selected frame."
102 (aref (internal-get-face face frame) 7))
103
104 \f
105 ;;; Mutators.
106
107 (defun set-face-font (face font &optional frame)
108 "Change the font of face FACE to FONT (a string).
109 If the optional FRAME argument is provided, change only
110 in that frame; otherwise change each frame."
111 (interactive (internal-face-interactive "font"))
112 (if (stringp font) (setq font (x-resolve-font-name font 'default frame)))
113 (internal-set-face-1 face 'font font 3 frame))
114
115 (defun set-face-foreground (face color &optional frame)
116 "Change the foreground color of face FACE to COLOR (a string).
117 If the optional FRAME argument is provided, change only
118 in that frame; otherwise change each frame."
119 (interactive (internal-face-interactive "foreground"))
120 (internal-set-face-1 face 'foreground color 4 frame))
121
122 (defun set-face-background (face color &optional frame)
123 "Change the background color of face FACE to COLOR (a string).
124 If the optional FRAME argument is provided, change only
125 in that frame; otherwise change each frame."
126 (interactive (internal-face-interactive "background"))
127 ;; For a specific frame, use gray stipple instead of gray color
128 ;; if the display does not support a gray color.
129 (if (and frame (not (eq frame t))
130 (member color '("gray" "gray1" "gray3"))
131 (not (x-display-color-p frame))
132 (not (x-display-grayscale-p frame)))
133 (set-face-stipple face color frame)
134 (internal-set-face-1 face 'background color 5 frame)))
135
136 (defun set-face-stipple (face name &optional frame)
137 "Change the stipple pixmap of face FACE to PIXMAP.
138 PIXMAP should be a string, the name of a file of pixmap data.
139 The directories listed in the `x-bitmap-file-path' variable are searched.
140
141 Alternatively, PIXMAP may be a list of the form (WIDTH HEIGHT DATA)
142 where WIDTH and HEIGHT are the size in pixels,
143 and DATA is a string, containing the raw bits of the bitmap.
144
145 If the optional FRAME argument is provided, change only
146 in that frame; otherwise change each frame."
147 (interactive (internal-face-interactive "stipple"))
148 (internal-set-face-1 face 'background-pixmap name 6 frame))
149
150 (defalias 'set-face-background-pixmap 'set-face-stipple)
151
152 (defun set-face-underline-p (face underline-p &optional frame)
153 "Specify whether face FACE is underlined. (Yes if UNDERLINE-P is non-nil.)
154 If the optional FRAME argument is provided, change only
155 in that frame; otherwise change each frame."
156 (interactive (internal-face-interactive "underline-p" "underlined"))
157 (internal-set-face-1 face 'underline underline-p 7 frame))
158 \f
159 (defun modify-face (face foreground background bold-p italic-p underline-p)
160 "Change the display attributes for face FACE.
161 FOREGROUND and BACKGROUND should be color strings. (Default color if nil.)
162 BOLD-P, ITALIC-P, and UNDERLINE-P specify whether the face should be set bold,
163 in italic, and underlined, respectively. (Yes if non-nil.)
164 If called interactively, prompts for a face and face attributes."
165 (interactive
166 (let* ((completion-ignore-case t)
167 (face (symbol-name (read-face-name "Face: ")))
168 (foreground (completing-read
169 (format "Face %s set foreground (default %s): " face
170 (downcase (or (face-foreground (intern face))
171 "foreground")))
172 (mapcar 'list (x-defined-colors))))
173 (background (completing-read
174 (format "Face %s set background (default %s): " face
175 (downcase (or (face-background (intern face))
176 "background")))
177 (mapcar 'list (x-defined-colors))))
178 (bold-p (y-or-n-p (concat "Face " face ": set bold ")))
179 (italic-p (y-or-n-p (concat "Face " face ": set italic ")))
180 (underline-p (y-or-n-p (concat "Face " face ": set underline "))))
181 (if (string-equal background "") (setq background nil))
182 (if (string-equal foreground "") (setq foreground nil))
183 (message "Face %s: %s" face
184 (mapconcat 'identity
185 (delq nil
186 (list (and foreground (concat (downcase foreground) " foreground"))
187 (and background (concat (downcase background) " background"))
188 (and bold-p "bold") (and italic-p "italic")
189 (and underline-p "underline"))) ", "))
190 (list (intern face) foreground background bold-p italic-p underline-p)))
191 (condition-case nil (set-face-foreground face foreground) (error nil))
192 (condition-case nil (set-face-background face background) (error nil))
193 (funcall (if bold-p 'make-face-bold 'make-face-unbold) face nil t)
194 (funcall (if italic-p 'make-face-italic 'make-face-unitalic) face nil t)
195 (set-face-underline-p face underline-p)
196 (and (interactive-p) (redraw-display)))
197 \f
198 ;;;; Associating face names (symbols) with their face vectors.
199
200 (defvar global-face-data nil
201 "Internal data for face support functions. Not for external use.
202 This is an alist associating face names with the default values for
203 their parameters. Newly created frames get their data from here.")
204
205 (defun face-list ()
206 "Returns a list of all defined face names."
207 (mapcar 'car global-face-data))
208
209 (defun internal-find-face (name &optional frame)
210 "Retrieve the face named NAME. Return nil if there is no such face.
211 If the optional argument FRAME is given, this gets the face NAME for
212 that frame; otherwise, it uses the selected frame.
213 If FRAME is the symbol t, then the global, non-frame face is returned.
214 If NAME is already a face, it is simply returned."
215 (if (and (eq frame t) (not (symbolp name)))
216 (setq name (face-name name)))
217 (if (symbolp name)
218 (cdr (assq name
219 (if (eq frame t)
220 global-face-data
221 (frame-face-alist (or frame (selected-frame))))))
222 (internal-check-face name)
223 name))
224
225 (defun internal-get-face (name &optional frame)
226 "Retrieve the face named NAME; error if there is none.
227 If the optional argument FRAME is given, this gets the face NAME for
228 that frame; otherwise, it uses the selected frame.
229 If FRAME is the symbol t, then the global, non-frame face is returned.
230 If NAME is already a face, it is simply returned."
231 (or (internal-find-face name frame)
232 (internal-check-face name)))
233
234
235 (defun internal-set-face-1 (face name value index frame)
236 (let ((inhibit-quit t))
237 (if (null frame)
238 (let ((frames (frame-list)))
239 (while frames
240 (internal-set-face-1 (face-name face) name value index (car frames))
241 (setq frames (cdr frames)))
242 (aset (internal-get-face (if (symbolp face) face (face-name face)) t)
243 index value)
244 value)
245 (or (eq frame t)
246 (set-face-attribute-internal (face-id face) name value frame))
247 (aset (internal-get-face face frame) index value))))
248
249
250 (defun read-face-name (prompt)
251 (let (face)
252 (while (= (length face) 0)
253 (setq face (completing-read prompt
254 (mapcar '(lambda (x) (list (symbol-name x)))
255 (face-list))
256 nil t)))
257 (intern face)))
258
259 (defun internal-face-interactive (what &optional bool)
260 (let* ((fn (intern (concat "face-" what)))
261 (prompt (concat "Set " what " of face"))
262 (face (read-face-name (concat prompt ": ")))
263 (default (if (fboundp fn)
264 (or (funcall fn face (selected-frame))
265 (funcall fn 'default (selected-frame)))))
266 (value (if bool
267 (y-or-n-p (concat "Should face " (symbol-name face)
268 " be " bool "? "))
269 (read-string (concat prompt " " (symbol-name face) " to: ")
270 default))))
271 (list face (if (equal value "") nil value))))
272
273
274
275 (defun make-face (name)
276 "Define a new FACE on all frames.
277 You can modify the font, color, etc of this face with the set-face- functions.
278 If the face already exists, it is unmodified."
279 (interactive "SMake face: ")
280 (or (internal-find-face name)
281 (let ((face (make-vector 8 nil)))
282 (aset face 0 'face)
283 (aset face 1 name)
284 (let* ((frames (frame-list))
285 (inhibit-quit t)
286 (id (internal-next-face-id)))
287 (make-face-internal id)
288 (aset face 2 id)
289 (while frames
290 (set-frame-face-alist (car frames)
291 (cons (cons name (copy-sequence face))
292 (frame-face-alist (car frames))))
293 (setq frames (cdr frames)))
294 (setq global-face-data (cons (cons name face) global-face-data)))
295 ;; when making a face after frames already exist
296 (if (eq window-system 'x)
297 (make-face-x-resource-internal face))
298 ;; add to menu
299 (if (fboundp 'facemenu-add-new-face)
300 (facemenu-add-new-face name))
301 face))
302 name)
303
304 ;; Fill in a face by default based on X resources, for all existing frames.
305 ;; This has to be done when a new face is made.
306 (defun make-face-x-resource-internal (face &optional frame set-anyway)
307 (cond ((null frame)
308 (let ((frames (frame-list)))
309 (while frames
310 (if (eq (framep (car frames)) 'x)
311 (make-face-x-resource-internal (face-name face)
312 (car frames) set-anyway))
313 (setq frames (cdr frames)))))
314 (t
315 (setq face (internal-get-face (face-name face) frame))
316 ;;
317 ;; These are things like "attributeForeground" instead of simply
318 ;; "foreground" because people tend to do things like "*foreground",
319 ;; which would cause all faces to be fully qualified, making faces
320 ;; inherit attributes in a non-useful way. So we've made them slightly
321 ;; less obvious to specify in order to make them work correctly in
322 ;; more random environments.
323 ;;
324 ;; I think these should be called "face.faceForeground" instead of
325 ;; "face.attributeForeground", but they're the way they are for
326 ;; hysterical reasons.
327 ;;
328 (let* ((name (symbol-name (face-name face)))
329 (fn (or (x-get-resource (concat name ".attributeFont")
330 "Face.AttributeFont")
331 (and set-anyway (face-font face))))
332 (fg (or (x-get-resource (concat name ".attributeForeground")
333 "Face.AttributeForeground")
334 (and set-anyway (face-foreground face))))
335 (bg (or (x-get-resource (concat name ".attributeBackground")
336 "Face.AttributeBackground")
337 (and set-anyway (face-background face))))
338 (bgp (or (x-get-resource (concat name ".attributeStipple")
339 "Face.AttributeStipple")
340 (x-get-resource (concat name ".attributeBackgroundPixmap")
341 "Face.AttributeBackgroundPixmap")
342 (and set-anyway (face-stipple face))))
343 (ulp (let ((resource (x-get-resource
344 (concat name ".attributeUnderline")
345 "Face.AttributeUnderline")))
346 (if resource
347 (member (downcase resource) '("on" "true"))
348 (and set-anyway (face-underline-p face)))))
349 )
350 (if fn
351 (condition-case ()
352 (set-face-font face fn frame)
353 (error (message "font `%s' not found for face `%s'" fn name))))
354 (if fg
355 (condition-case ()
356 (set-face-foreground face fg frame)
357 (error (message "color `%s' not allocated for face `%s'" fg name))))
358 (if bg
359 (condition-case ()
360 (set-face-background face bg frame)
361 (error (message "color `%s' not allocated for face `%s'" bg name))))
362 (if bgp
363 (condition-case ()
364 (set-face-stipple face bgp frame)
365 (error (message "pixmap `%s' not found for face `%s'" bgp name))))
366 (if (or ulp set-anyway)
367 (set-face-underline-p face ulp frame))
368 )))
369 face)
370
371 (defun copy-face (old-face new-face &optional frame new-frame)
372 "Define a face just like OLD-FACE, with name NEW-FACE.
373 If NEW-FACE already exists as a face, it is modified to be like OLD-FACE.
374 If it doesn't already exist, it is created.
375
376 If the optional argument FRAME is given as a frame,
377 NEW-FACE is changed on FRAME only.
378 If FRAME is t, the frame-independent default specification for OLD-FACE
379 is copied to NEW-FACE.
380 If FRAME is nil, copying is done for the frame-independent defaults
381 and for each existing frame.
382 If the optional fourth argument NEW-FRAME is given,
383 copy the information from face OLD-FACE on frame FRAME
384 to NEW-FACE on frame NEW-FRAME."
385 (or new-frame (setq new-frame frame))
386 (let ((inhibit-quit t))
387 (if (null frame)
388 (let ((frames (frame-list)))
389 (while frames
390 (copy-face old-face new-face (car frames))
391 (setq frames (cdr frames)))
392 (copy-face old-face new-face t))
393 (setq old-face (internal-get-face old-face frame))
394 (setq new-face (or (internal-find-face new-face new-frame)
395 (make-face new-face)))
396 (condition-case nil
397 ;; A face that has a global symbolic font modifier such as `bold'
398 ;; might legitimately get an error here.
399 ;; Use the frame's default font in that case.
400 (set-face-font new-face (face-font old-face frame) new-frame)
401 (error
402 (set-face-font new-face nil new-frame)))
403 (set-face-foreground new-face (face-foreground old-face frame) new-frame)
404 (set-face-background new-face (face-background old-face frame) new-frame)
405 (set-face-stipple new-face
406 (face-stipple old-face frame)
407 new-frame)
408 (set-face-underline-p new-face (face-underline-p old-face frame)
409 new-frame))
410 new-face))
411
412 (defun face-equal (face1 face2 &optional frame)
413 "True if the faces FACE1 and FACE2 display in the same way."
414 (setq face1 (internal-get-face face1 frame)
415 face2 (internal-get-face face2 frame))
416 (and (equal (face-foreground face1 frame) (face-foreground face2 frame))
417 (equal (face-background face1 frame) (face-background face2 frame))
418 (equal (face-font face1 frame) (face-font face2 frame))
419 (eq (face-underline-p face1 frame) (face-underline-p face2 frame))
420 (equal (face-stipple face1 frame)
421 (face-stipple face2 frame))))
422
423 (defun face-differs-from-default-p (face &optional frame)
424 "True if face FACE displays differently from the default face, on FRAME.
425 A face is considered to be ``the same'' as the default face if it is
426 actually specified in the same way (equivalent fonts, etc) or if it is
427 fully unspecified, and thus inherits the attributes of any face it
428 is displayed on top of."
429 (let ((default (internal-get-face 'default frame)))
430 (setq face (internal-get-face face frame))
431 (not (and (or (equal (face-foreground default frame)
432 (face-foreground face frame))
433 (null (face-foreground face frame)))
434 (or (equal (face-background default frame)
435 (face-background face frame))
436 (null (face-background face frame)))
437 (or (equal (face-font default frame) (face-font face frame))
438 (null (face-font face frame)))
439 (or (equal (face-stipple default frame)
440 (face-stipple face frame))
441 (null (face-stipple face frame)))
442 (equal (face-underline-p default frame)
443 (face-underline-p face frame))
444 ))))
445
446
447 (defun invert-face (face &optional frame)
448 "Swap the foreground and background colors of face FACE.
449 If the face doesn't specify both foreground and background, then
450 set its foreground and background to the default background and foreground."
451 (interactive (list (read-face-name "Invert face: ")))
452 (setq face (internal-get-face face frame))
453 (let ((fg (face-foreground face frame))
454 (bg (face-background face frame)))
455 (if (or fg bg)
456 (progn
457 (set-face-foreground face bg frame)
458 (set-face-background face fg frame))
459 (set-face-foreground face (or (face-background 'default frame)
460 (cdr (assq 'background-color (frame-parameters frame))))
461 frame)
462 (set-face-background face (or (face-foreground 'default frame)
463 (cdr (assq 'foreground-color (frame-parameters frame))))
464 frame)))
465 face)
466
467
468 (defun internal-try-face-font (face font &optional frame)
469 "Like set-face-font, but returns nil on failure instead of an error."
470 (condition-case ()
471 (set-face-font face font frame)
472 (error nil)))
473 \f
474 ;; Manipulating font names.
475
476 (defconst x-font-regexp nil)
477 (defconst x-font-regexp-head nil)
478 (defconst x-font-regexp-weight nil)
479 (defconst x-font-regexp-slant nil)
480
481 ;;; Regexps matching font names in "Host Portable Character Representation."
482 ;;;
483 (let ((- "[-?]")
484 (foundry "[^-]+")
485 (family "[^-]+")
486 (weight "\\(bold\\|demibold\\|medium\\)") ; 1
487 ; (weight\? "\\(\\*\\|bold\\|demibold\\|medium\\|\\)") ; 1
488 (weight\? "\\([^-]*\\)") ; 1
489 (slant "\\([ior]\\)") ; 2
490 ; (slant\? "\\([ior?*]?\\)") ; 2
491 (slant\? "\\([^-]?\\)") ; 2
492 ; (swidth "\\(\\*\\|normal\\|semicondensed\\|\\)") ; 3
493 (swidth "\\([^-]*\\)") ; 3
494 ; (adstyle "\\(\\*\\|sans\\|\\)") ; 4
495 (adstyle "[^-]*") ; 4
496 (pixelsize "[0-9]+")
497 (pointsize "[0-9][0-9]+")
498 (resx "[0-9][0-9]+")
499 (resy "[0-9][0-9]+")
500 (spacing "[cmp?*]")
501 (avgwidth "[0-9]+")
502 (registry "[^-]+")
503 (encoding "[^-]+")
504 )
505 (setq x-font-regexp
506 (concat "\\`\\*?[-?*]"
507 foundry - family - weight\? - slant\? - swidth - adstyle -
508 pixelsize - pointsize - resx - resy - spacing - registry -
509 encoding "[-?*]\\*?\\'"
510 ))
511 (setq x-font-regexp-head
512 (concat "\\`[-?*]" foundry - family - weight\? - slant\?
513 "\\([-*?]\\|\\'\\)"))
514 (setq x-font-regexp-slant (concat - slant -))
515 (setq x-font-regexp-weight (concat - weight -))
516 nil)
517
518 (defun x-resolve-font-name (pattern &optional face frame)
519 "Return a font name matching PATTERN.
520 All wildcards in PATTERN become substantiated.
521 If PATTERN is nil, return the name of the frame's base font, which never
522 contains wildcards.
523 Given optional arguments FACE and FRAME, return a font which is
524 also the same size as FACE on FRAME, or fail."
525 (or (symbolp face)
526 (setq face (face-name face)))
527 (and (eq frame t)
528 (setq frame nil))
529 (if pattern
530 ;; Note that x-list-fonts has code to handle a face with nil as its font.
531 (let ((fonts (x-list-fonts pattern face frame)))
532 (or fonts
533 (if face
534 (error "No fonts matching pattern are the same size as `%s'"
535 (if (null (face-font face))
536 (cdr (assq 'font (frame-parameters frame)))
537 face))
538 (error "No fonts match `%s'" pattern)))
539 (car fonts))
540 (cdr (assq 'font (frame-parameters (selected-frame))))))
541
542 (defun x-frob-font-weight (font which)
543 (if (or (string-match x-font-regexp font)
544 (string-match x-font-regexp-head font)
545 (string-match x-font-regexp-weight font))
546 (concat (substring font 0 (match-beginning 1)) which
547 (substring font (match-end 1)))
548 nil))
549
550 (defun x-frob-font-slant (font which)
551 (cond ((or (string-match x-font-regexp font)
552 (string-match x-font-regexp-head font))
553 (concat (substring font 0 (match-beginning 2)) which
554 (substring font (match-end 2))))
555 ((string-match x-font-regexp-slant font)
556 (concat (substring font 0 (match-beginning 1)) which
557 (substring font (match-end 1))))
558 (t nil)))
559
560
561 (defun x-make-font-bold (font)
562 "Given an X font specification, make a bold version of it.
563 If that can't be done, return nil."
564 (x-frob-font-weight font "bold"))
565
566 (defun x-make-font-demibold (font)
567 "Given an X font specification, make a demibold version of it.
568 If that can't be done, return nil."
569 (x-frob-font-weight font "demibold"))
570
571 (defun x-make-font-unbold (font)
572 "Given an X font specification, make a non-bold version of it.
573 If that can't be done, return nil."
574 (x-frob-font-weight font "medium"))
575
576 (defun x-make-font-italic (font)
577 "Given an X font specification, make an italic version of it.
578 If that can't be done, return nil."
579 (x-frob-font-slant font "i"))
580
581 (defun x-make-font-oblique (font) ; you say tomayto...
582 "Given an X font specification, make an oblique version of it.
583 If that can't be done, return nil."
584 (x-frob-font-slant font "o"))
585
586 (defun x-make-font-unitalic (font)
587 "Given an X font specification, make a non-italic version of it.
588 If that can't be done, return nil."
589 (x-frob-font-slant font "r"))
590 \f
591 ;;; non-X-specific interface
592
593 (defun make-face-bold (face &optional frame noerror)
594 "Make the font of the given face be bold, if possible.
595 If NOERROR is non-nil, return nil on failure."
596 (interactive (list (read-face-name "Make which face bold: ")))
597 (if (and (eq frame t) (listp (face-font face t)))
598 (set-face-font face (if (memq 'italic (face-font face t))
599 '(bold italic) '(bold))
600 t)
601 (let ((ofont (face-font face frame))
602 font)
603 (if (null frame)
604 (let ((frames (frame-list)))
605 ;; Make this face bold in global-face-data.
606 (make-face-bold face t noerror)
607 ;; Make this face bold in each frame.
608 (while frames
609 (make-face-bold face (car frames) noerror)
610 (setq frames (cdr frames))))
611 (setq face (internal-get-face face frame))
612 (setq font (or (face-font face frame)
613 (face-font face t)))
614 (if (listp font)
615 (setq font nil))
616 (setq font (or font
617 (face-font 'default frame)
618 (cdr (assq 'font (frame-parameters frame)))))
619 (and font (make-face-bold-internal face frame font)))
620 (or (not (equal ofont (face-font face)))
621 (and (not noerror)
622 (error "No bold version of %S" font))))))
623
624 (defun make-face-bold-internal (face frame font)
625 (let (f2)
626 (or (and (setq f2 (x-make-font-bold font))
627 (internal-try-face-font face f2 frame))
628 (and (setq f2 (x-make-font-demibold font))
629 (internal-try-face-font face f2 frame)))))
630
631 (defun make-face-italic (face &optional frame noerror)
632 "Make the font of the given face be italic, if possible.
633 If NOERROR is non-nil, return nil on failure."
634 (interactive (list (read-face-name "Make which face italic: ")))
635 (if (and (eq frame t) (listp (face-font face t)))
636 (set-face-font face (if (memq 'bold (face-font face t))
637 '(bold italic) '(italic))
638 t)
639 (let ((ofont (face-font face frame))
640 font)
641 (if (null frame)
642 (let ((frames (frame-list)))
643 ;; Make this face italic in global-face-data.
644 (make-face-italic face t noerror)
645 ;; Make this face italic in each frame.
646 (while frames
647 (make-face-italic face (car frames) noerror)
648 (setq frames (cdr frames))))
649 (setq face (internal-get-face face frame))
650 (setq font (or (face-font face frame)
651 (face-font face t)))
652 (if (listp font)
653 (setq font nil))
654 (setq font (or font
655 (face-font 'default frame)
656 (cdr (assq 'font (frame-parameters frame)))))
657 (and font (make-face-italic-internal face frame font)))
658 (or (not (equal ofont (face-font face)))
659 (and (not noerror)
660 (error "No italic version of %S" font))))))
661
662 (defun make-face-italic-internal (face frame font)
663 (let (f2)
664 (or (and (setq f2 (x-make-font-italic font))
665 (internal-try-face-font face f2 frame))
666 (and (setq f2 (x-make-font-oblique font))
667 (internal-try-face-font face f2 frame)))))
668
669 (defun make-face-bold-italic (face &optional frame noerror)
670 "Make the font of the given face be bold and italic, if possible.
671 If NOERROR is non-nil, return nil on failure."
672 (interactive (list (read-face-name "Make which face bold-italic: ")))
673 (if (and (eq frame t) (listp (face-font face t)))
674 (set-face-font face '(bold italic) t)
675 (let ((ofont (face-font face frame))
676 font)
677 (if (null frame)
678 (let ((frames (frame-list)))
679 ;; Make this face bold-italic in global-face-data.
680 (make-face-bold-italic face t noerror)
681 ;; Make this face bold in each frame.
682 (while frames
683 (make-face-bold-italic face (car frames) noerror)
684 (setq frames (cdr frames))))
685 (setq face (internal-get-face face frame))
686 (setq font (or (face-font face frame)
687 (face-font face t)))
688 (if (listp font)
689 (setq font nil))
690 (setq font (or font
691 (face-font 'default frame)
692 (cdr (assq 'font (frame-parameters frame)))))
693 (and font (make-face-bold-italic-internal face frame font)))
694 (or (not (equal ofont (face-font face)))
695 (and (not noerror)
696 (error "No bold italic version of %S" font))))))
697
698 (defun make-face-bold-italic-internal (face frame font)
699 (let (f2 f3)
700 (or (and (setq f2 (x-make-font-italic font))
701 (not (equal font f2))
702 (setq f3 (x-make-font-bold f2))
703 (not (equal f2 f3))
704 (internal-try-face-font face f3 frame))
705 (and (setq f2 (x-make-font-oblique font))
706 (not (equal font f2))
707 (setq f3 (x-make-font-bold f2))
708 (not (equal f2 f3))
709 (internal-try-face-font face f3 frame))
710 (and (setq f2 (x-make-font-italic font))
711 (not (equal font f2))
712 (setq f3 (x-make-font-demibold f2))
713 (not (equal f2 f3))
714 (internal-try-face-font face f3 frame))
715 (and (setq f2 (x-make-font-oblique font))
716 (not (equal font f2))
717 (setq f3 (x-make-font-demibold f2))
718 (not (equal f2 f3))
719 (internal-try-face-font face f3 frame)))))
720
721 (defun make-face-unbold (face &optional frame noerror)
722 "Make the font of the given face be non-bold, if possible.
723 If NOERROR is non-nil, return nil on failure."
724 (interactive (list (read-face-name "Make which face non-bold: ")))
725 (if (and (eq frame t) (listp (face-font face t)))
726 (set-face-font face (if (memq 'italic (face-font face t))
727 '(italic) nil)
728 t)
729 (let ((ofont (face-font face frame))
730 font font1)
731 (if (null frame)
732 (let ((frames (frame-list)))
733 ;; Make this face unbold in global-face-data.
734 (make-face-unbold face t noerror)
735 ;; Make this face unbold in each frame.
736 (while frames
737 (make-face-unbold face (car frames) noerror)
738 (setq frames (cdr frames))))
739 (setq face (internal-get-face face frame))
740 (setq font1 (or (face-font face frame)
741 (face-font face t)))
742 (if (listp font1)
743 (setq font1 nil))
744 (setq font1 (or font1
745 (face-font 'default frame)
746 (cdr (assq 'font (frame-parameters frame)))))
747 (setq font (and font1 (x-make-font-unbold font1)))
748 (if font (internal-try-face-font face font frame)))
749 (or (not (equal ofont (face-font face)))
750 (and (not noerror)
751 (error "No unbold version of %S" font1))))))
752
753 (defun make-face-unitalic (face &optional frame noerror)
754 "Make the font of the given face be non-italic, if possible.
755 If NOERROR is non-nil, return nil on failure."
756 (interactive (list (read-face-name "Make which face non-italic: ")))
757 (if (and (eq frame t) (listp (face-font face t)))
758 (set-face-font face (if (memq 'bold (face-font face t))
759 '(bold) nil)
760 t)
761 (let ((ofont (face-font face frame))
762 font font1)
763 (if (null frame)
764 (let ((frames (frame-list)))
765 ;; Make this face unitalic in global-face-data.
766 (make-face-unitalic face t noerror)
767 ;; Make this face unitalic in each frame.
768 (while frames
769 (make-face-unitalic face (car frames) noerror)
770 (setq frames (cdr frames))))
771 (setq face (internal-get-face face frame))
772 (setq font1 (or (face-font face frame)
773 (face-font face t)))
774 (if (listp font1)
775 (setq font1 nil))
776 (setq font1 (or font1
777 (face-font 'default frame)
778 (cdr (assq 'font (frame-parameters frame)))))
779 (setq font (and font1 (x-make-font-unitalic font1)))
780 (if font (internal-try-face-font face font frame)))
781 (or (not (equal ofont (face-font face)))
782 (and (not noerror)
783 (error "No unitalic version of %S" font1))))))
784 \f
785 (defvar list-faces-sample-text
786 "abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ"
787 "*Text string to display as the sample text for `list-faces-display'.")
788
789 ;; The name list-faces would be more consistent, but let's avoid a conflict
790 ;; with Lucid, which uses that name differently.
791 (defun list-faces-display ()
792 "List all faces, using the same sample text in each.
793 The sample text is a string that comes from the variable
794 `list-faces-sample-text'.
795
796 It is possible to give a particular face name different appearances in
797 different frames. This command shows the appearance in the
798 selected frame."
799 (interactive)
800 (let ((faces (sort (face-list) (function string-lessp)))
801 (face nil)
802 (frame (selected-frame))
803 disp-frame window)
804 (with-output-to-temp-buffer "*Faces*"
805 (save-excursion
806 (set-buffer standard-output)
807 (setq truncate-lines t)
808 (while faces
809 (setq face (car faces))
810 (setq faces (cdr faces))
811 (insert (format "%25s " (symbol-name face)))
812 (let ((beg (point)))
813 (insert list-faces-sample-text)
814 (insert "\n")
815 (put-text-property beg (1- (point)) 'face face)
816 ;; If the sample text has multiple lines, line up all of them.
817 (goto-char beg)
818 (forward-line 1)
819 (while (not (eobp))
820 (insert " ")
821 (forward-line 1))))
822 (goto-char (point-min))))
823 ;; If the *Faces* buffer appears in a different frame,
824 ;; copy all the face definitions from FRAME,
825 ;; so that the display will reflect the frame that was selected.
826 (setq window (get-buffer-window (get-buffer "*Faces*") t))
827 (setq disp-frame (if window (window-frame window)
828 (car (frame-list))))
829 (or (eq frame disp-frame)
830 (let ((faces (face-list)))
831 (while faces
832 (copy-face (car faces) (car faces) frame disp-frame)
833 (setq faces (cdr faces)))))))
834 \f
835 ;;; Make the standard faces.
836 ;;; The C code knows the default and modeline faces as faces 0 and 1,
837 ;;; so they must be the first two faces made.
838 (defun face-initialize ()
839 (make-face 'default)
840 (make-face 'modeline)
841 (make-face 'highlight)
842
843 ;; These aren't really special in any way, but they're nice to have around.
844
845 (make-face 'bold)
846 (make-face 'italic)
847 (make-face 'bold-italic)
848 (make-face 'region)
849 (make-face 'secondary-selection)
850 (make-face 'underline)
851
852 (setq region-face (face-id 'region))
853
854 ;; Specify the global properties of these faces
855 ;; so they will come out right on new frames.
856
857 (make-face-bold 'bold t)
858 (make-face-italic 'italic t)
859 (make-face-bold-italic 'bold-italic t)
860
861 (set-face-background 'highlight '("darkseagreen2" "green" t) t)
862 (set-face-background 'region '("gray" underline) t)
863 (set-face-background 'secondary-selection '("paleturquoise" "green" t) t)
864 (set-face-background 'modeline '(t) t)
865 (set-face-underline-p 'underline t t)
866
867 ;; Set up the faces of all existing X Window frames
868 ;; from those global properties, unless already set in a given frame.
869
870 (let ((frames (frame-list)))
871 (while frames
872 (if (not (memq (framep (car frames)) '(t nil)))
873 (let ((frame (car frames))
874 (rest global-face-data))
875 (while rest
876 (let ((face (car (car rest))))
877 (or (face-differs-from-default-p face)
878 (face-fill-in face (cdr (car rest)) frame)))
879 (setq rest (cdr rest)))))
880 (setq frames (cdr frames)))))
881
882 \f
883 ;; Like x-create-frame but also set up the faces.
884
885 (defun x-create-frame-with-faces (&optional parameters)
886 (if (null global-face-data)
887 (x-create-frame parameters)
888 (let* ((visibility-spec (assq 'visibility parameters))
889 (frame (x-create-frame (cons '(visibility . nil) parameters)))
890 (faces (copy-alist global-face-data))
891 success
892 (rest faces))
893 (unwind-protect
894 (progn
895 (set-frame-face-alist frame faces)
896
897 (if (cdr (or (assq 'reverse parameters)
898 (assq 'reverse default-frame-alist)
899 (let ((resource (x-get-resource "reverseVideo"
900 "ReverseVideo")))
901 (if resource
902 (cons nil (member (downcase resource)
903 '("on" "true")))))))
904 (let ((params (frame-parameters frame)))
905 (modify-frame-parameters
906 frame
907 (list (cons 'foreground-color (cdr (assq 'background-color params)))
908 (cons 'background-color (cdr (assq 'foreground-color params)))
909 (cons 'mouse-color (cdr (assq 'background-color params)))
910 (cons 'border-color (cdr (assq 'background-color params)))))
911 (modify-frame-parameters
912 frame
913 (list (cons 'cursor-color (cdr (assq 'background-color params)))))))
914
915 ;; Copy the vectors that represent the faces.
916 ;; Also fill them in from X resources.
917 (while rest
918 (let ((global (cdr (car rest))))
919 (setcdr (car rest) (vector 'face
920 (face-name (cdr (car rest)))
921 (face-id (cdr (car rest)))
922 nil nil nil nil nil))
923 (face-fill-in (car (car rest)) global frame))
924 (make-face-x-resource-internal (cdr (car rest)) frame t)
925 (setq rest (cdr rest)))
926 (if (null visibility-spec)
927 (make-frame-visible frame)
928 (modify-frame-parameters frame (list visibility-spec)))
929 (setq success t)
930 frame)
931 (or success
932 (delete-frame frame))))))
933
934 ;; Update a frame's faces when we change its default font.
935 (defun frame-update-faces (frame)
936 (let* ((faces global-face-data)
937 (rest faces))
938 (while rest
939 (let* ((face (car (car rest)))
940 (font (face-font face t)))
941 (if (listp font)
942 (let ((bold (memq 'bold font))
943 (italic (memq 'italic font)))
944 ;; Ignore any previous (string-valued) font, it might not even
945 ;; be the right size anymore.
946 (set-face-font face nil frame)
947 (cond ((and bold italic)
948 (make-face-bold-italic face frame t))
949 (bold
950 (make-face-bold face frame t))
951 (italic
952 (make-face-italic face frame t)))))
953 (setq rest (cdr rest)))
954 frame)))
955
956 ;; Update the colors of FACE, after FRAME's own colors have been changed.
957 ;; This applies only to faces with global color specifications
958 ;; that are not simple constants.
959 (defun frame-update-face-colors (frame)
960 (let ((faces global-face-data))
961 (while faces
962 (condition-case nil
963 (let* ((data (cdr (car faces)))
964 (face (car (car faces)))
965 (foreground (face-foreground data))
966 (background (face-background data)))
967 ;; If the global spec is a specific color,
968 ;; which doesn't depend on the frame's attributes,
969 ;; we don't need to recalculate it now.
970 (or (listp foreground)
971 (setq foreground nil))
972 (or (listp background)
973 (setq background nil))
974 ;; If we are going to frob this face at all,
975 ;; reinitialize it first.
976 (if (or foreground background)
977 (progn (set-face-foreground face nil frame)
978 (set-face-background face nil frame)))
979 (if foreground
980 (face-try-color-list 'set-face-foreground
981 face foreground frame))
982 (if background
983 (face-try-color-list 'set-face-background
984 face background frame)))
985 (error nil))
986 (setq faces (cdr faces)))))
987
988 ;; Fill in the face FACE from frame-independent face data DATA.
989 ;; DATA should be the non-frame-specific ("global") face vector
990 ;; for the face. FACE should be a face name or face object.
991 ;; FRAME is the frame to act on; it must be an actual frame, not nil or t.
992 (defun face-fill-in (face data frame)
993 (condition-case nil
994 (let ((foreground (face-foreground data))
995 (background (face-background data))
996 (font (face-font data)))
997 (set-face-underline-p face (face-underline-p data) frame)
998 (if foreground
999 (face-try-color-list 'set-face-foreground
1000 face foreground frame))
1001 (if background
1002 (face-try-color-list 'set-face-background
1003 face background frame))
1004 (if (listp font)
1005 (let ((bold (memq 'bold font))
1006 (italic (memq 'italic font)))
1007 (cond ((and bold italic)
1008 (make-face-bold-italic face frame))
1009 (bold
1010 (make-face-bold face frame))
1011 (italic
1012 (make-face-italic face frame))))
1013 (if font
1014 (set-face-font face font frame))))
1015 (error nil)))
1016
1017 ;; Assuming COLOR is a valid color name,
1018 ;; return t if it can be displayed on FRAME.
1019 (defun face-color-supported-p (frame color background-p)
1020 (or (x-display-color-p frame)
1021 ;; A black-and-white display can implement these.
1022 (member color '("black" "white"))
1023 ;; A black-and-white display can fake these for background.
1024 (and background-p
1025 (member color '("gray" "gray1" "gray3")))
1026 ;; A grayscale display can implement colors that are gray (more or less).
1027 (and (x-display-grayscale-p frame)
1028 (let* ((values (x-color-values color frame))
1029 (r (nth 0 values))
1030 (g (nth 1 values))
1031 (b (nth 2 values)))
1032 (and (< (abs (- r g)) (/ (abs (+ r g)) 20))
1033 (< (abs (- g b)) (/ (abs (+ g b)) 20))
1034 (< (abs (- b r)) (/ (abs (+ b r)) 20)))))))
1035
1036 ;; Use FUNCTION to store a color in FACE on FRAME.
1037 ;; COLORS is either a single color or a list of colors.
1038 ;; If it is a list, try the colors one by one until one of them
1039 ;; succeeds. We signal an error only if all the colors failed.
1040 ;; t as COLORS or as an element of COLORS means to invert the face.
1041 ;; That can't fail, so any subsequent elements after the t are ignored.
1042 (defun face-try-color-list (function face colors frame)
1043 (if (stringp colors)
1044 (if (face-color-supported-p frame colors
1045 (eq function 'set-face-background))
1046 (funcall function face colors frame))
1047 (if (eq colors t)
1048 (invert-face face frame)
1049 (let (done)
1050 (while (and colors (not done))
1051 (if (or (eq (car colors) t)
1052 (face-color-supported-p frame (car colors)
1053 (eq function 'set-face-background)))
1054 (if (cdr colors)
1055 ;; If there are more colors to try, catch errors
1056 ;; and set `done' if we succeed.
1057 (condition-case nil
1058 (progn
1059 (cond ((eq (car colors) t)
1060 (invert-face face frame))
1061 ((eq (car colors) 'underline)
1062 (set-face-underline-p face t frame))
1063 (t
1064 (funcall function face (car colors) frame)))
1065 (setq done t))
1066 (error nil))
1067 ;; If this is the last color, let the error get out if it fails.
1068 ;; If it succeeds, we will exit anyway after this iteration.
1069 (cond ((eq (car colors) t)
1070 (invert-face face frame))
1071 ((eq (car colors) 'underline)
1072 (set-face-underline-p face t frame))
1073 (t
1074 (funcall function face (car colors) frame)))))
1075 (setq colors (cdr colors)))))))
1076
1077 ;; If we are already using x-window frames, initialize faces for them.
1078 (if (eq (framep (selected-frame)) 'x)
1079 (face-initialize))
1080
1081 (provide 'faces)
1082
1083 ;;; faces.el ends here