(iso-transl-char-map): Add elements for mute-acute, etc.
[emacs.git] / lisp / faces.el
blob8c928962e734492b73603a32643912baba9b4fd1
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:
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))
43 ;;;; Functions for manipulating face vectors.
45 ;;; A face vector is a vector of the form:
46 ;;; [face NAME ID FONT FOREGROUND BACKGROUND STIPPLE UNDERLINE]
48 ;;; Type checkers.
49 (defsubst internal-facep (x)
50 (and (vectorp x) (= (length x) 8) (eq (aref x 0) 'face)))
52 (defmacro internal-check-face (face)
53 (` (while (not (internal-facep (, face)))
54 (setq (, face) (signal 'wrong-type-argument (list 'internal-facep (, face)))))))
56 ;;; Accessors.
57 (defun face-name (face)
58 "Return the name of face FACE."
59 (aref (internal-get-face face) 1))
61 (defun face-id (face)
62 "Return the internal ID number of face FACE."
63 (aref (internal-get-face face) 2))
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))
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))
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))
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))
95 (defalias 'face-background-pixmap 'face-stipple)
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))
105 ;;; Mutators.
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))
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))
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)))
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.
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.
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))
150 (defalias 'set-face-background-pixmap 'set-face-stipple)
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))
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)))
198 ;;;; Associating face names (symbols) with their face vectors.
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.")
205 (defun face-list ()
206 "Returns a list of all defined face names."
207 (mapcar 'car global-face-data))
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))
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)))
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))))
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)))
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))))
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)
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)))))
315 (setq face (internal-get-face (face-name face) frame))
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.
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.
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)))))
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))
369 face)
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.
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))
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))))
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.
430 The optional argument FRAME specifies which frame to test;
431 if FRAME is t, test the default for new frames.
432 If FRAME is nil or omitted, test the selected frame."
433 (let ((default (internal-get-face 'default frame)))
434 (setq face (internal-get-face face frame))
435 (not (and (or (equal (face-foreground default frame)
436 (face-foreground face frame))
437 (null (face-foreground face frame)))
438 (or (equal (face-background default frame)
439 (face-background face frame))
440 (null (face-background face frame)))
441 (or (equal (face-font default frame) (face-font face frame))
442 (null (face-font face frame)))
443 (or (equal (face-stipple default frame)
444 (face-stipple face frame))
445 (null (face-stipple face frame)))
446 (equal (face-underline-p default frame)
447 (face-underline-p face frame))
448 ))))
450 (defun face-nontrivial-p (face &optional frame)
451 "True if face FACE has some non-nil attribute.
452 The optional argument FRAME specifies which frame to test;
453 if FRAME is t, test the default for new frames.
454 If FRAME is nil or omitted, test the selected frame."
455 (setq face (internal-get-face face frame))
456 (or (face-foreground face frame)
457 (face-background face frame)
458 (face-font face frame)
459 (face-stipple face frame)
460 (face-underline-p face frame)))
463 (defun invert-face (face &optional frame)
464 "Swap the foreground and background colors of face FACE.
465 If the face doesn't specify both foreground and background, then
466 set its foreground and background to the default background and foreground."
467 (interactive (list (read-face-name "Invert face: ")))
468 (setq face (internal-get-face face frame))
469 (let ((fg (face-foreground face frame))
470 (bg (face-background face frame)))
471 (if (or fg bg)
472 (progn
473 (set-face-foreground face bg frame)
474 (set-face-background face fg frame))
475 (set-face-foreground face (or (face-background 'default frame)
476 (cdr (assq 'background-color (frame-parameters frame))))
477 frame)
478 (set-face-background face (or (face-foreground 'default frame)
479 (cdr (assq 'foreground-color (frame-parameters frame))))
480 frame)))
481 face)
484 (defun internal-try-face-font (face font &optional frame)
485 "Like set-face-font, but returns nil on failure instead of an error."
486 (condition-case ()
487 (set-face-font face font frame)
488 (error nil)))
490 ;; Manipulating font names.
492 (defconst x-font-regexp nil)
493 (defconst x-font-regexp-head nil)
494 (defconst x-font-regexp-weight nil)
495 (defconst x-font-regexp-slant nil)
497 ;;; Regexps matching font names in "Host Portable Character Representation."
499 (let ((- "[-?]")
500 (foundry "[^-]+")
501 (family "[^-]+")
502 (weight "\\(bold\\|demibold\\|medium\\)") ; 1
503 ; (weight\? "\\(\\*\\|bold\\|demibold\\|medium\\|\\)") ; 1
504 (weight\? "\\([^-]*\\)") ; 1
505 (slant "\\([ior]\\)") ; 2
506 ; (slant\? "\\([ior?*]?\\)") ; 2
507 (slant\? "\\([^-]?\\)") ; 2
508 ; (swidth "\\(\\*\\|normal\\|semicondensed\\|\\)") ; 3
509 (swidth "\\([^-]*\\)") ; 3
510 ; (adstyle "\\(\\*\\|sans\\|\\)") ; 4
511 (adstyle "[^-]*") ; 4
512 (pixelsize "[0-9]+")
513 (pointsize "[0-9][0-9]+")
514 (resx "[0-9][0-9]+")
515 (resy "[0-9][0-9]+")
516 (spacing "[cmp?*]")
517 (avgwidth "[0-9]+")
518 (registry "[^-]+")
519 (encoding "[^-]+")
521 (setq x-font-regexp
522 (concat "\\`\\*?[-?*]"
523 foundry - family - weight\? - slant\? - swidth - adstyle -
524 pixelsize - pointsize - resx - resy - spacing - registry -
525 encoding "[-?*]\\*?\\'"
527 (setq x-font-regexp-head
528 (concat "\\`[-?*]" foundry - family - weight\? - slant\?
529 "\\([-*?]\\|\\'\\)"))
530 (setq x-font-regexp-slant (concat - slant -))
531 (setq x-font-regexp-weight (concat - weight -))
532 nil)
534 (defun x-resolve-font-name (pattern &optional face frame)
535 "Return a font name matching PATTERN.
536 All wildcards in PATTERN become substantiated.
537 If PATTERN is nil, return the name of the frame's base font, which never
538 contains wildcards.
539 Given optional arguments FACE and FRAME, return a font which is
540 also the same size as FACE on FRAME, or fail."
541 (or (symbolp face)
542 (setq face (face-name face)))
543 (and (eq frame t)
544 (setq frame nil))
545 (if pattern
546 ;; Note that x-list-fonts has code to handle a face with nil as its font.
547 (let ((fonts (x-list-fonts pattern face frame)))
548 (or fonts
549 (if face
550 (error "No fonts matching pattern are the same size as `%s'"
551 (if (null (face-font face))
552 (cdr (assq 'font (frame-parameters frame)))
553 face))
554 (error "No fonts match `%s'" pattern)))
555 (car fonts))
556 (cdr (assq 'font (frame-parameters (selected-frame))))))
558 (defun x-frob-font-weight (font which)
559 (if (or (string-match x-font-regexp font)
560 (string-match x-font-regexp-head font)
561 (string-match x-font-regexp-weight font))
562 (concat (substring font 0 (match-beginning 1)) which
563 (substring font (match-end 1)))
564 nil))
566 (defun x-frob-font-slant (font which)
567 (cond ((or (string-match x-font-regexp font)
568 (string-match x-font-regexp-head font))
569 (concat (substring font 0 (match-beginning 2)) which
570 (substring font (match-end 2))))
571 ((string-match x-font-regexp-slant font)
572 (concat (substring font 0 (match-beginning 1)) which
573 (substring font (match-end 1))))
574 (t nil)))
577 (defun x-make-font-bold (font)
578 "Given an X font specification, make a bold version of it.
579 If that can't be done, return nil."
580 (x-frob-font-weight font "bold"))
582 (defun x-make-font-demibold (font)
583 "Given an X font specification, make a demibold version of it.
584 If that can't be done, return nil."
585 (x-frob-font-weight font "demibold"))
587 (defun x-make-font-unbold (font)
588 "Given an X font specification, make a non-bold version of it.
589 If that can't be done, return nil."
590 (x-frob-font-weight font "medium"))
592 (defun x-make-font-italic (font)
593 "Given an X font specification, make an italic version of it.
594 If that can't be done, return nil."
595 (x-frob-font-slant font "i"))
597 (defun x-make-font-oblique (font) ; you say tomayto...
598 "Given an X font specification, make an oblique version of it.
599 If that can't be done, return nil."
600 (x-frob-font-slant font "o"))
602 (defun x-make-font-unitalic (font)
603 "Given an X font specification, make a non-italic version of it.
604 If that can't be done, return nil."
605 (x-frob-font-slant font "r"))
607 ;;; non-X-specific interface
609 (defun make-face-bold (face &optional frame noerror)
610 "Make the font of the given face be bold, if possible.
611 If NOERROR is non-nil, return nil on failure."
612 (interactive (list (read-face-name "Make which face bold: ")))
613 (if (and (eq frame t) (listp (face-font face t)))
614 (set-face-font face (if (memq 'italic (face-font face t))
615 '(bold italic) '(bold))
617 (let ((ofont (face-font face frame))
618 font)
619 (if (null frame)
620 (let ((frames (frame-list)))
621 ;; Make this face bold in global-face-data.
622 (make-face-bold face t noerror)
623 ;; Make this face bold in each frame.
624 (while frames
625 (make-face-bold face (car frames) noerror)
626 (setq frames (cdr frames))))
627 (setq face (internal-get-face face frame))
628 (setq font (or (face-font face frame)
629 (face-font face t)))
630 (if (listp font)
631 (setq font nil))
632 (setq font (or font
633 (face-font 'default frame)
634 (cdr (assq 'font (frame-parameters frame)))))
635 (and font (make-face-bold-internal face frame font)))
636 (or (not (equal ofont (face-font face)))
637 (and (not noerror)
638 (error "No bold version of %S" font))))))
640 (defun make-face-bold-internal (face frame font)
641 (let (f2)
642 (or (and (setq f2 (x-make-font-bold font))
643 (internal-try-face-font face f2 frame))
644 (and (setq f2 (x-make-font-demibold font))
645 (internal-try-face-font face f2 frame)))))
647 (defun make-face-italic (face &optional frame noerror)
648 "Make the font of the given face be italic, if possible.
649 If NOERROR is non-nil, return nil on failure."
650 (interactive (list (read-face-name "Make which face italic: ")))
651 (if (and (eq frame t) (listp (face-font face t)))
652 (set-face-font face (if (memq 'bold (face-font face t))
653 '(bold italic) '(italic))
655 (let ((ofont (face-font face frame))
656 font)
657 (if (null frame)
658 (let ((frames (frame-list)))
659 ;; Make this face italic in global-face-data.
660 (make-face-italic face t noerror)
661 ;; Make this face italic in each frame.
662 (while frames
663 (make-face-italic face (car frames) noerror)
664 (setq frames (cdr frames))))
665 (setq face (internal-get-face face frame))
666 (setq font (or (face-font face frame)
667 (face-font face t)))
668 (if (listp font)
669 (setq font nil))
670 (setq font (or font
671 (face-font 'default frame)
672 (cdr (assq 'font (frame-parameters frame)))))
673 (and font (make-face-italic-internal face frame font)))
674 (or (not (equal ofont (face-font face)))
675 (and (not noerror)
676 (error "No italic version of %S" font))))))
678 (defun make-face-italic-internal (face frame font)
679 (let (f2)
680 (or (and (setq f2 (x-make-font-italic font))
681 (internal-try-face-font face f2 frame))
682 (and (setq f2 (x-make-font-oblique font))
683 (internal-try-face-font face f2 frame)))))
685 (defun make-face-bold-italic (face &optional frame noerror)
686 "Make the font of the given face be bold and italic, if possible.
687 If NOERROR is non-nil, return nil on failure."
688 (interactive (list (read-face-name "Make which face bold-italic: ")))
689 (if (and (eq frame t) (listp (face-font face t)))
690 (set-face-font face '(bold italic) t)
691 (let ((ofont (face-font face frame))
692 font)
693 (if (null frame)
694 (let ((frames (frame-list)))
695 ;; Make this face bold-italic in global-face-data.
696 (make-face-bold-italic face t noerror)
697 ;; Make this face bold in each frame.
698 (while frames
699 (make-face-bold-italic face (car frames) noerror)
700 (setq frames (cdr frames))))
701 (setq face (internal-get-face face frame))
702 (setq font (or (face-font face frame)
703 (face-font face t)))
704 (if (listp font)
705 (setq font nil))
706 (setq font (or font
707 (face-font 'default frame)
708 (cdr (assq 'font (frame-parameters frame)))))
709 (and font (make-face-bold-italic-internal face frame font)))
710 (or (not (equal ofont (face-font face)))
711 (and (not noerror)
712 (error "No bold italic version of %S" font))))))
714 (defun make-face-bold-italic-internal (face frame font)
715 (let (f2 f3)
716 (or (and (setq f2 (x-make-font-italic font))
717 (not (equal font f2))
718 (setq f3 (x-make-font-bold f2))
719 (not (equal f2 f3))
720 (internal-try-face-font face f3 frame))
721 (and (setq f2 (x-make-font-oblique font))
722 (not (equal font f2))
723 (setq f3 (x-make-font-bold f2))
724 (not (equal f2 f3))
725 (internal-try-face-font face f3 frame))
726 (and (setq f2 (x-make-font-italic font))
727 (not (equal font f2))
728 (setq f3 (x-make-font-demibold f2))
729 (not (equal f2 f3))
730 (internal-try-face-font face f3 frame))
731 (and (setq f2 (x-make-font-oblique font))
732 (not (equal font f2))
733 (setq f3 (x-make-font-demibold f2))
734 (not (equal f2 f3))
735 (internal-try-face-font face f3 frame)))))
737 (defun make-face-unbold (face &optional frame noerror)
738 "Make the font of the given face be non-bold, if possible.
739 If NOERROR is non-nil, return nil on failure."
740 (interactive (list (read-face-name "Make which face non-bold: ")))
741 (if (and (eq frame t) (listp (face-font face t)))
742 (set-face-font face (if (memq 'italic (face-font face t))
743 '(italic) nil)
745 (let ((ofont (face-font face frame))
746 font font1)
747 (if (null frame)
748 (let ((frames (frame-list)))
749 ;; Make this face unbold in global-face-data.
750 (make-face-unbold face t noerror)
751 ;; Make this face unbold in each frame.
752 (while frames
753 (make-face-unbold face (car frames) noerror)
754 (setq frames (cdr frames))))
755 (setq face (internal-get-face face frame))
756 (setq font1 (or (face-font face frame)
757 (face-font face t)))
758 (if (listp font1)
759 (setq font1 nil))
760 (setq font1 (or font1
761 (face-font 'default frame)
762 (cdr (assq 'font (frame-parameters frame)))))
763 (setq font (and font1 (x-make-font-unbold font1)))
764 (if font (internal-try-face-font face font frame)))
765 (or (not (equal ofont (face-font face)))
766 (and (not noerror)
767 (error "No unbold version of %S" font1))))))
769 (defun make-face-unitalic (face &optional frame noerror)
770 "Make the font of the given face be non-italic, if possible.
771 If NOERROR is non-nil, return nil on failure."
772 (interactive (list (read-face-name "Make which face non-italic: ")))
773 (if (and (eq frame t) (listp (face-font face t)))
774 (set-face-font face (if (memq 'bold (face-font face t))
775 '(bold) nil)
777 (let ((ofont (face-font face frame))
778 font font1)
779 (if (null frame)
780 (let ((frames (frame-list)))
781 ;; Make this face unitalic in global-face-data.
782 (make-face-unitalic face t noerror)
783 ;; Make this face unitalic in each frame.
784 (while frames
785 (make-face-unitalic face (car frames) noerror)
786 (setq frames (cdr frames))))
787 (setq face (internal-get-face face frame))
788 (setq font1 (or (face-font face frame)
789 (face-font face t)))
790 (if (listp font1)
791 (setq font1 nil))
792 (setq font1 (or font1
793 (face-font 'default frame)
794 (cdr (assq 'font (frame-parameters frame)))))
795 (setq font (and font1 (x-make-font-unitalic font1)))
796 (if font (internal-try-face-font face font frame)))
797 (or (not (equal ofont (face-font face)))
798 (and (not noerror)
799 (error "No unitalic version of %S" font1))))))
801 (defvar list-faces-sample-text
802 "abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ"
803 "*Text string to display as the sample text for `list-faces-display'.")
805 ;; The name list-faces would be more consistent, but let's avoid a conflict
806 ;; with Lucid, which uses that name differently.
807 (defun list-faces-display ()
808 "List all faces, using the same sample text in each.
809 The sample text is a string that comes from the variable
810 `list-faces-sample-text'.
812 It is possible to give a particular face name different appearances in
813 different frames. This command shows the appearance in the
814 selected frame."
815 (interactive)
816 (let ((faces (sort (face-list) (function string-lessp)))
817 (face nil)
818 (frame (selected-frame))
819 disp-frame window)
820 (with-output-to-temp-buffer "*Faces*"
821 (save-excursion
822 (set-buffer standard-output)
823 (setq truncate-lines t)
824 (while faces
825 (setq face (car faces))
826 (setq faces (cdr faces))
827 (insert (format "%25s " (symbol-name face)))
828 (let ((beg (point)))
829 (insert list-faces-sample-text)
830 (insert "\n")
831 (put-text-property beg (1- (point)) 'face face)
832 ;; If the sample text has multiple lines, line up all of them.
833 (goto-char beg)
834 (forward-line 1)
835 (while (not (eobp))
836 (insert " ")
837 (forward-line 1))))
838 (goto-char (point-min))))
839 ;; If the *Faces* buffer appears in a different frame,
840 ;; copy all the face definitions from FRAME,
841 ;; so that the display will reflect the frame that was selected.
842 (setq window (get-buffer-window (get-buffer "*Faces*") t))
843 (setq disp-frame (if window (window-frame window)
844 (car (frame-list))))
845 (or (eq frame disp-frame)
846 (let ((faces (face-list)))
847 (while faces
848 (copy-face (car faces) (car faces) frame disp-frame)
849 (setq faces (cdr faces)))))))
851 ;;; Make the standard faces.
852 ;;; The C code knows the default and modeline faces as faces 0 and 1,
853 ;;; so they must be the first two faces made.
854 (defun face-initialize ()
855 (make-face 'default)
856 (make-face 'modeline)
857 (make-face 'highlight)
859 ;; These aren't really special in any way, but they're nice to have around.
861 (make-face 'bold)
862 (make-face 'italic)
863 (make-face 'bold-italic)
864 (make-face 'region)
865 (make-face 'secondary-selection)
866 (make-face 'underline)
868 (setq region-face (face-id 'region))
870 ;; Specify the global properties of these faces
871 ;; so they will come out right on new frames.
873 (make-face-bold 'bold t)
874 (make-face-italic 'italic t)
875 (make-face-bold-italic 'bold-italic t)
877 (set-face-background 'highlight '("darkseagreen2" "green" t) t)
878 (set-face-background 'region '("gray" underline) t)
879 (set-face-background 'secondary-selection '("paleturquoise" "green" t) t)
880 (set-face-background 'modeline '(t) t)
881 (set-face-underline-p 'underline t t)
883 ;; Set up the faces of all existing X Window frames
884 ;; from those global properties, unless already set in a given frame.
886 (let ((frames (frame-list)))
887 (while frames
888 (if (not (memq (framep (car frames)) '(t nil)))
889 (let ((frame (car frames))
890 (rest global-face-data))
891 (while rest
892 (let ((face (car (car rest))))
893 (or (face-differs-from-default-p face)
894 (face-fill-in face (cdr (car rest)) frame)))
895 (setq rest (cdr rest)))))
896 (setq frames (cdr frames)))))
899 ;; Like x-create-frame but also set up the faces.
901 (defun x-create-frame-with-faces (&optional parameters)
902 (if (null global-face-data)
903 (x-create-frame parameters)
904 (let* ((visibility-spec (assq 'visibility parameters))
905 (frame (x-create-frame (cons '(visibility . nil) parameters)))
906 (faces (copy-alist global-face-data))
907 success
908 (rest faces))
909 (unwind-protect
910 (progn
911 (set-frame-face-alist frame faces)
913 (if (cdr (or (assq 'reverse parameters)
914 (assq 'reverse default-frame-alist)
915 (let ((resource (x-get-resource "reverseVideo"
916 "ReverseVideo")))
917 (if resource
918 (cons nil (member (downcase resource)
919 '("on" "true")))))))
920 (let ((params (frame-parameters frame)))
921 (modify-frame-parameters
922 frame
923 (list (cons 'foreground-color (cdr (assq 'background-color params)))
924 (cons 'background-color (cdr (assq 'foreground-color params)))
925 (cons 'mouse-color (cdr (assq 'background-color params)))
926 (cons 'border-color (cdr (assq 'background-color params)))))
927 (modify-frame-parameters
928 frame
929 (list (cons 'cursor-color (cdr (assq 'background-color params)))))))
931 ;; Copy the vectors that represent the faces.
932 ;; Also fill them in from X resources.
933 (while rest
934 (let ((global (cdr (car rest))))
935 (setcdr (car rest) (vector 'face
936 (face-name (cdr (car rest)))
937 (face-id (cdr (car rest)))
938 nil nil nil nil nil))
939 (face-fill-in (car (car rest)) global frame))
940 (make-face-x-resource-internal (cdr (car rest)) frame t)
941 (setq rest (cdr rest)))
942 (if (null visibility-spec)
943 (make-frame-visible frame)
944 (modify-frame-parameters frame (list visibility-spec)))
945 (setq success t)
946 frame)
947 (or success
948 (delete-frame frame))))))
950 ;; Update a frame's faces when we change its default font.
951 (defun frame-update-faces (frame)
952 (let* ((faces global-face-data)
953 (rest faces))
954 (while rest
955 (let* ((face (car (car rest)))
956 (font (face-font face t)))
957 (if (listp font)
958 (let ((bold (memq 'bold font))
959 (italic (memq 'italic font)))
960 ;; Ignore any previous (string-valued) font, it might not even
961 ;; be the right size anymore.
962 (set-face-font face nil frame)
963 (cond ((and bold italic)
964 (make-face-bold-italic face frame t))
965 (bold
966 (make-face-bold face frame t))
967 (italic
968 (make-face-italic face frame t)))))
969 (setq rest (cdr rest)))
970 frame)))
972 ;; Update the colors of FACE, after FRAME's own colors have been changed.
973 ;; This applies only to faces with global color specifications
974 ;; that are not simple constants.
975 (defun frame-update-face-colors (frame)
976 (let ((faces global-face-data))
977 (while faces
978 (condition-case nil
979 (let* ((data (cdr (car faces)))
980 (face (car (car faces)))
981 (foreground (face-foreground data))
982 (background (face-background data)))
983 ;; If the global spec is a specific color,
984 ;; which doesn't depend on the frame's attributes,
985 ;; we don't need to recalculate it now.
986 (or (listp foreground)
987 (setq foreground nil))
988 (or (listp background)
989 (setq background nil))
990 ;; If we are going to frob this face at all,
991 ;; reinitialize it first.
992 (if (or foreground background)
993 (progn (set-face-foreground face nil frame)
994 (set-face-background face nil frame)))
995 (if foreground
996 (face-try-color-list 'set-face-foreground
997 face foreground frame))
998 (if background
999 (face-try-color-list 'set-face-background
1000 face background frame)))
1001 (error nil))
1002 (setq faces (cdr faces)))))
1004 ;; Fill in the face FACE from frame-independent face data DATA.
1005 ;; DATA should be the non-frame-specific ("global") face vector
1006 ;; for the face. FACE should be a face name or face object.
1007 ;; FRAME is the frame to act on; it must be an actual frame, not nil or t.
1008 (defun face-fill-in (face data frame)
1009 (condition-case nil
1010 (let ((foreground (face-foreground data))
1011 (background (face-background data))
1012 (font (face-font data)))
1013 (set-face-underline-p face (face-underline-p data) frame)
1014 (if foreground
1015 (face-try-color-list 'set-face-foreground
1016 face foreground frame))
1017 (if background
1018 (face-try-color-list 'set-face-background
1019 face background frame))
1020 (if (listp font)
1021 (let ((bold (memq 'bold font))
1022 (italic (memq 'italic font)))
1023 (cond ((and bold italic)
1024 (make-face-bold-italic face frame))
1025 (bold
1026 (make-face-bold face frame))
1027 (italic
1028 (make-face-italic face frame))))
1029 (if font
1030 (set-face-font face font frame))))
1031 (error nil)))
1033 ;; Assuming COLOR is a valid color name,
1034 ;; return t if it can be displayed on FRAME.
1035 (defun face-color-supported-p (frame color background-p)
1036 (or (x-display-color-p frame)
1037 ;; A black-and-white display can implement these.
1038 (member color '("black" "white"))
1039 ;; A black-and-white display can fake these for background.
1040 (and background-p
1041 (member color '("gray" "gray1" "gray3")))
1042 ;; A grayscale display can implement colors that are gray (more or less).
1043 (and (x-display-grayscale-p frame)
1044 (let* ((values (x-color-values color frame))
1045 (r (nth 0 values))
1046 (g (nth 1 values))
1047 (b (nth 2 values)))
1048 (and (< (abs (- r g)) (/ (abs (+ r g)) 20))
1049 (< (abs (- g b)) (/ (abs (+ g b)) 20))
1050 (< (abs (- b r)) (/ (abs (+ b r)) 20)))))))
1052 ;; Use FUNCTION to store a color in FACE on FRAME.
1053 ;; COLORS is either a single color or a list of colors.
1054 ;; If it is a list, try the colors one by one until one of them
1055 ;; succeeds. We signal an error only if all the colors failed.
1056 ;; t as COLORS or as an element of COLORS means to invert the face.
1057 ;; That can't fail, so any subsequent elements after the t are ignored.
1058 (defun face-try-color-list (function face colors frame)
1059 (if (stringp colors)
1060 (if (face-color-supported-p frame colors
1061 (eq function 'set-face-background))
1062 (funcall function face colors frame))
1063 (if (eq colors t)
1064 (invert-face face frame)
1065 (let (done)
1066 (while (and colors (not done))
1067 (if (or (memq (car colors) '(t underline))
1068 (face-color-supported-p frame (car colors)
1069 (eq function 'set-face-background)))
1070 (if (cdr colors)
1071 ;; If there are more colors to try, catch errors
1072 ;; and set `done' if we succeed.
1073 (condition-case nil
1074 (progn
1075 (cond ((eq (car colors) t)
1076 (invert-face face frame))
1077 ((eq (car colors) 'underline)
1078 (set-face-underline-p face t frame))
1080 (funcall function face (car colors) frame)))
1081 (setq done t))
1082 (error nil))
1083 ;; If this is the last color, let the error get out if it fails.
1084 ;; If it succeeds, we will exit anyway after this iteration.
1085 (cond ((eq (car colors) t)
1086 (invert-face face frame))
1087 ((eq (car colors) 'underline)
1088 (set-face-underline-p face t frame))
1090 (funcall function face (car colors) frame)))))
1091 (setq colors (cdr colors)))))))
1093 ;; If we are already using x-window frames, initialize faces for them.
1094 (if (eq (framep (selected-frame)) 'x)
1095 (face-initialize))
1097 (provide 'faces)
1099 ;;; faces.el ends here