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