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