Commit | Line | Data |
---|---|---|
465fceed ER |
1 | ;;; faces.el --- Lisp interface to the c "face" structure |
2 | ||
3 | ;; Copyright (C) 1992, 1993 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 | (defsubst internal-facep (x) | |
28 | (and (vectorp x) (= (length x) 8) (eq (aref x 0) 'face))) | |
29 | ||
30 | (defmacro internal-check-face (face) | |
31 | (` (while (not (internal-facep (, face))) | |
32 | (setq (, face) (signal 'wrong-type-argument (list 'internal-facep (, face))))))) | |
33 | ||
34 | ||
35 | (defvar global-face-data nil "do not use this") | |
36 | ||
37 | (defun face-list () | |
38 | "Returns a list of all defined face names." | |
39 | (mapcar 'car global-face-data)) | |
40 | ||
41 | (defun internal-find-face (name &optional frame) | |
42 | "Retrieve the face named NAME. Return nil if there is no such face. | |
43 | If the optional argument FRAME is given, this gets the face NAME for | |
44 | that frame; otherwise, it uses the selected frame. | |
45 | If FRAME is the symbol t, then the global, non-frame face is returned. | |
46 | If NAME is already a face, it is simply returned." | |
47 | (if (and (eq frame t) (not (symbolp name))) | |
48 | (setq name (face-name name))) | |
49 | (if (symbolp name) | |
50 | (cdr (assq name | |
51 | (if (eq frame t) | |
52 | global-face-data | |
53 | (frame-face-alist (or frame (selected-frame)))))) | |
54 | (internal-check-face name) | |
55 | name)) | |
56 | ||
57 | (defun internal-get-face (name &optional frame) | |
58 | "Retrieve the face named NAME; error if there is none. | |
59 | If the optional argument FRAME is given, this gets the face NAME for | |
60 | that frame; otherwise, it uses the selected frame. | |
61 | If FRAME is the symbol t, then the global, non-frame face is returned. | |
62 | If NAME is already a face, it is simply returned." | |
63 | (or (internal-find-face name frame) | |
64 | (internal-check-face name))) | |
65 | ||
66 | (defsubst face-name (face) | |
67 | "Return the name of face FACE." | |
68 | (aref (internal-get-face face) 1)) | |
69 | ||
70 | (defsubst face-id (face) | |
71 | "Return the internal ID number of face FACE." | |
72 | (aref (internal-get-face face) 2)) | |
73 | ||
74 | (defsubst face-font (face &optional frame) | |
75 | "Return the font name of face FACE, or nil if it is unspecified. | |
76 | If the optional argument FRAME is given, report on face FACE in that frame. | |
77 | Otherwise report on the defaults for face FACE (for new frames)." | |
78 | (aref (internal-get-face face frame) 3)) | |
79 | ||
80 | (defsubst face-foreground (face &optional frame) | |
81 | "Return the foreground color name of face FACE, or nil if unspecified. | |
82 | If the optional argument FRAME is given, report on face FACE in that frame. | |
83 | Otherwise report on the defaults for face FACE (for new frames)." | |
84 | (aref (internal-get-face face frame) 4)) | |
85 | ||
86 | (defsubst face-background (face &optional frame) | |
87 | "Return the background color name of face FACE, or nil if unspecified. | |
88 | If the optional argument FRAME is given, report on face FACE in that frame. | |
89 | Otherwise report on the defaults for face FACE (for new frames)." | |
90 | (aref (internal-get-face face frame) 5)) | |
91 | ||
92 | (defsubst face-background-pixmap (face &optional frame) | |
93 | "Return the background pixmap name of face FACE, or nil if unspecified. | |
94 | If the optional argument FRAME is given, report on face FACE in that frame. | |
95 | Otherwise report on the defaults for face FACE (for new frames)." | |
96 | (aref (internal-get-face face frame) 6)) | |
97 | ||
98 | (defsubst face-underline-p (face &optional frame) | |
99 | "Return t if face FACE is underlined. | |
100 | If the optional argument FRAME is given, report on face FACE in that frame. | |
101 | Otherwise report on the defaults for face FACE (for new frames)." | |
102 | (aref (internal-get-face face frame) 7)) | |
103 | ||
104 | ||
f0138172 | 105 | (defun internal-set-face-1 (face name value index frame) |
465fceed ER |
106 | (let ((inhibit-quit t)) |
107 | (if (null frame) | |
108 | (let ((frames (frame-list))) | |
109 | (while frames | |
f0138172 | 110 | (internal-set-face-1 (face-name face) name value index (car frames)) |
465fceed ER |
111 | (setq frames (cdr frames))) |
112 | (aset (internal-get-face (if (symbolp face) face (face-name face)) t) | |
113 | index value) | |
114 | value) | |
115 | (or (eq frame t) | |
116 | (set-face-attribute-internal (face-id face) name value frame)) | |
117 | (aset (internal-get-face face frame) index value)))) | |
118 | ||
119 | ||
120 | (defun read-face-name (prompt) | |
121 | (let (face) | |
122 | (while (= (length face) 0) | |
123 | (setq face (completing-read prompt | |
124 | (mapcar '(lambda (x) (list (symbol-name x))) | |
bb9a81fc | 125 | (face-list)) |
465fceed ER |
126 | nil t))) |
127 | (intern face))) | |
128 | ||
129 | (defun internal-face-interactive (what &optional bool) | |
130 | (let* ((fn (intern (concat "face-" what))) | |
131 | (prompt (concat "Set " what " of face")) | |
132 | (face (read-face-name (concat prompt ": "))) | |
133 | (default (if (fboundp fn) | |
134 | (or (funcall fn face (selected-frame)) | |
135 | (funcall fn 'default (selected-frame))))) | |
136 | (value (if bool | |
137 | (y-or-n-p (concat "Should face " (symbol-name face) | |
138 | " be " bool "? ")) | |
139 | (read-string (concat prompt " " (symbol-name face) " to: ") | |
140 | default)))) | |
141 | (list face (if (equal value "") nil value)))) | |
142 | ||
143 | ||
144 | (defsubst set-face-font (face font &optional frame) | |
145 | "Change the font of face FACE to FONT (a string). | |
146 | If the optional FRAME argument is provided, change only | |
147 | in that frame; otherwise change each frame." | |
148 | (interactive (internal-face-interactive "font")) | |
f0138172 | 149 | (internal-set-face-1 face 'font font 3 frame)) |
465fceed ER |
150 | |
151 | (defsubst set-face-foreground (face color &optional frame) | |
152 | "Change the foreground color of face FACE to COLOR (a string). | |
153 | If the optional FRAME argument is provided, change only | |
154 | in that frame; otherwise change each frame." | |
155 | (interactive (internal-face-interactive "foreground")) | |
f0138172 | 156 | (internal-set-face-1 face 'foreground color 4 frame)) |
465fceed ER |
157 | |
158 | (defsubst set-face-background (face color &optional frame) | |
159 | "Change the background color of face FACE to COLOR (a string). | |
160 | If the optional FRAME argument is provided, change only | |
161 | in that frame; otherwise change each frame." | |
162 | (interactive (internal-face-interactive "background")) | |
f0138172 | 163 | (internal-set-face-1 face 'background color 5 frame)) |
465fceed ER |
164 | |
165 | (defsubst set-face-background-pixmap (face name &optional frame) | |
166 | "Change the background pixmap of face FACE to PIXMAP. | |
167 | PIXMAP should be a string, the name of a file of pixmap data. | |
168 | The directories listed in the `x-bitmap-file-path' variable are searched. | |
169 | ||
170 | Alternatively, PIXMAP may be a list of the form (WIDTH HEIGHT DATA) | |
171 | where WIDTH and HEIGHT are the size in pixels, | |
172 | and DATA is a string, containing the raw bits of the bitmap. | |
173 | ||
174 | If the optional FRAME argument is provided, change only | |
175 | in that frame; otherwise change each frame." | |
176 | (interactive (internal-face-interactive "background-pixmap")) | |
f0138172 | 177 | (internal-set-face-1 face 'background-pixmap name 6 frame)) |
465fceed ER |
178 | |
179 | (defsubst set-face-underline-p (face underline-p &optional frame) | |
180 | "Specify whether face FACE is underlined. (Yes if UNDERLINE-P is non-nil.) | |
181 | If the optional FRAME argument is provided, change only | |
182 | in that frame; otherwise change each frame." | |
183 | (interactive (internal-face-interactive "underline-p" "underlined")) | |
f0138172 | 184 | (internal-set-face-1 face 'underline underline-p 7 frame)) |
465fceed ER |
185 | |
186 | ||
187 | (defun make-face (name) | |
188 | "Define a new FACE on all frames. | |
189 | You can modify the font, color, etc of this face with the set-face- functions. | |
190 | If the face already exists, it is unmodified." | |
191 | (or (internal-find-face name) | |
192 | (let ((face (make-vector 8 nil))) | |
193 | (aset face 0 'face) | |
194 | (aset face 1 name) | |
195 | (let* ((frames (frame-list)) | |
196 | (inhibit-quit t) | |
197 | (id (internal-next-face-id))) | |
198 | (make-face-internal id) | |
199 | (aset face 2 id) | |
200 | (while frames | |
201 | (set-frame-face-alist (car frames) | |
202 | (cons (cons name (copy-sequence face)) | |
203 | (frame-face-alist (car frames)))) | |
204 | (setq frames (cdr frames))) | |
205 | (setq global-face-data (cons (cons name face) global-face-data))) | |
206 | ;; when making a face after frames already exist | |
207 | (if (eq window-system 'x) | |
208 | (make-face-x-resource-internal face)) | |
209 | face))) | |
210 | ||
211 | ;; Fill in a face by default based on X resources, for all existing frames. | |
212 | ;; This has to be done when a new face is made. | |
213 | (defun make-face-x-resource-internal (face &optional frame set-anyway) | |
214 | (cond ((null frame) | |
215 | (let ((frames (frame-list))) | |
216 | (while frames | |
217 | (make-face-x-resource-internal (face-name face) | |
218 | (car frames) set-anyway) | |
219 | (setq frames (cdr frames))))) | |
220 | (t | |
221 | (setq face (internal-get-face (face-name face) frame)) | |
222 | ;; | |
223 | ;; These are things like "attributeForeground" instead of simply | |
224 | ;; "foreground" because people tend to do things like "*foreground", | |
225 | ;; which would cause all faces to be fully qualified, making faces | |
226 | ;; inherit attributes in a non-useful way. So we've made them slightly | |
227 | ;; less obvious to specify in order to make them work correctly in | |
228 | ;; more random environments. | |
229 | ;; | |
230 | ;; I think these should be called "face.faceForeground" instead of | |
231 | ;; "face.attributeForeground", but they're the way they are for | |
232 | ;; hysterical reasons. | |
233 | ;; | |
234 | (let* ((name (symbol-name (face-name face))) | |
235 | (fn (or (x-get-resource (concat name ".attributeFont") | |
236 | "Face.AttributeFont") | |
237 | (and set-anyway (face-font face)))) | |
238 | (fg (or (x-get-resource (concat name ".attributeForeground") | |
239 | "Face.AttributeForeground") | |
240 | (and set-anyway (face-foreground face)))) | |
241 | (bg (or (x-get-resource (concat name ".attributeBackground") | |
242 | "Face.AttributeBackground") | |
243 | (and set-anyway (face-background face)))) | |
244 | ;; (bgp (or (x-get-resource (concat name ".attributeBackgroundPixmap") | |
245 | ;; "Face.AttributeBackgroundPixmap") | |
246 | ;; (and set-anyway (face-background-pixmap face)))) | |
247 | (ulp (or (x-get-resource (concat name ".attributeUnderline") | |
248 | "Face.AttributeUnderline") | |
249 | (and set-anyway (face-underline-p face)))) | |
250 | ) | |
251 | (if fn | |
252 | (condition-case () | |
253 | (set-face-font face fn frame) | |
254 | (error (message "font `%s' not found for face `%s'" fn name)))) | |
255 | (if fg | |
256 | (condition-case () | |
257 | (set-face-foreground face fg frame) | |
258 | (error (message "color `%s' not allocated for face `%s'" fg name)))) | |
259 | (if bg | |
260 | (condition-case () | |
261 | (set-face-background face bg frame) | |
262 | (error (message "color `%s' not allocated for face `%s'" bg name)))) | |
263 | ;; (if bgp | |
264 | ;; (condition-case () | |
265 | ;; (set-face-background-pixmap face bgp frame) | |
266 | ;; (error (message "pixmap `%s' not found for face `%s'" bgp name)))) | |
267 | (if (or ulp set-anyway) | |
268 | (set-face-underline-p face ulp frame)) | |
269 | ))) | |
270 | face) | |
271 | ||
272 | (defun copy-face (old-face new-name &optional frame) | |
273 | "Define a face just like OLD-FACE, with name NEW-NAME. | |
274 | If NEW-NAME already exists as a face, it is modified to be like OLD-FACE. | |
275 | If the optional argument FRAME is given, this applies only to that frame. | |
276 | Otherwise it applies to each frame separately." | |
277 | (setq old-face (internal-get-face old-face frame)) | |
278 | (let* ((inhibit-quit t) | |
279 | (new-face (or (internal-find-face new-name frame) | |
280 | (make-face new-name)))) | |
281 | (if (null frame) | |
282 | (let ((frames (frame-list))) | |
283 | (while frames | |
284 | (copy-face old-face new-name (car frames)) | |
285 | (setq frames (cdr frames))) | |
286 | (copy-face old-face new-name t)) | |
287 | (set-face-font new-face (face-font old-face frame) frame) | |
288 | (set-face-foreground new-face (face-foreground old-face frame) frame) | |
289 | (set-face-background new-face (face-background old-face frame) frame) | |
290 | (set-face-background-pixmap | |
291 | new-face (face-background-pixmap old-face frame) frame) | |
292 | (set-face-underline-p new-face (face-underline-p old-face frame) | |
293 | frame)) | |
294 | new-face)) | |
295 | ||
296 | (defun face-equal (face1 face2 &optional frame) | |
297 | "True if the faces FACE1 and FACE2 display in the the same way." | |
298 | (setq face1 (internal-get-face face1 frame) | |
299 | face2 (internal-get-face face2 frame)) | |
300 | (and (equal (face-foreground face1 frame) (face-foreground face2 frame)) | |
301 | (equal (face-background face1 frame) (face-background face2 frame)) | |
302 | (equal (face-font face1 frame) (face-font face2 frame)) | |
303 | (equal (face-background-pixmap face1 frame) | |
304 | (face-background-pixmap face2 frame)))) | |
305 | ||
306 | (defun face-differs-from-default-p (face &optional frame) | |
307 | "True if face FACE displays differently from the default face, on FRAME. | |
308 | A face is considered to be ``the same'' as the default face if it is | |
309 | actually specified in the same way (equivalent fonts, etc) or if it is | |
310 | fully unspecified, and thus inherits the attributes of any face it | |
311 | is displayed on top of." | |
312 | (let ((default (internal-get-face 'default frame))) | |
313 | (setq face (internal-get-face face frame)) | |
314 | (not (and (or (equal (face-foreground default frame) | |
315 | (face-foreground face frame)) | |
316 | (null (face-foreground face frame))) | |
317 | (or (equal (face-background default frame) | |
318 | (face-background face frame)) | |
319 | (null (face-background face frame))) | |
320 | (or (equal (face-font default frame) (face-font face frame)) | |
321 | (null (face-font face frame))) | |
322 | (or (equal (face-background-pixmap default frame) | |
323 | (face-background-pixmap face frame)) | |
324 | (null (face-background-pixmap face frame))) | |
325 | (equal (face-underline-p default frame) | |
326 | (face-underline-p face frame)) | |
327 | )))) | |
328 | ||
329 | ||
330 | (defun invert-face (face &optional frame) | |
331 | "Swap the foreground and background colors of face FACE. | |
332 | If the face doesn't specify both foreground and background, then | |
333 | its foreground and background are set to the background and | |
334 | foreground of the default face." | |
335 | (interactive (list (read-face-name "Invert face: "))) | |
336 | (setq face (internal-get-face face frame)) | |
337 | (let ((fg (face-foreground face frame)) | |
338 | (bg (face-background face frame))) | |
339 | (if (or fg bg) | |
340 | (progn | |
341 | (set-face-foreground face bg frame) | |
342 | (set-face-background face fg frame)) | |
343 | (set-face-foreground face (face-background 'default frame) frame) | |
344 | (set-face-background face (face-foreground 'default frame) frame))) | |
345 | face) | |
346 | ||
347 | ||
348 | (defun internal-try-face-font (face font &optional frame) | |
349 | "Like set-face-font, but returns nil on failure instead of an error." | |
350 | (condition-case () | |
351 | (set-face-font face font frame) | |
352 | (error nil))) | |
353 | ||
354 | ||
355 | (defun set-default-font (font) | |
356 | "Sets the font used for normal text and the modeline to FONT in all frames. | |
357 | For finer-grained control, use set-face-font." | |
358 | (interactive (list (read-string "Set default font: " | |
359 | (face-font 'default (selected-frame))))) | |
360 | (set-face-font 'default font) | |
361 | (set-face-font 'modeline font)) | |
362 | \f | |
363 | ;; Manipulating font names. | |
364 | ||
365 | (defconst x-font-regexp nil) | |
366 | (defconst x-font-regexp-head nil) | |
367 | (defconst x-font-regexp-weight nil) | |
368 | (defconst x-font-regexp-slant nil) | |
369 | ||
370 | ;;; Regexps matching font names in "Host Portable Character Representation." | |
371 | ;;; | |
372 | (let ((- "[-?]") | |
373 | (foundry "[^-]+") | |
374 | (family "[^-]+") | |
375 | (weight "\\(bold\\|demibold\\|medium\\)") ; 1 | |
376 | ; (weight\? "\\(\\*\\|bold\\|demibold\\|medium\\|\\)") ; 1 | |
377 | (weight\? "\\([^-]*\\)") ; 1 | |
378 | (slant "\\([ior]\\)") ; 2 | |
379 | ; (slant\? "\\([ior?*]?\\)") ; 2 | |
380 | (slant\? "\\([^-]?\\)") ; 2 | |
381 | ; (swidth "\\(\\*\\|normal\\|semicondensed\\|\\)") ; 3 | |
382 | (swidth "\\([^-]*\\)") ; 3 | |
383 | ; (adstyle "\\(\\*\\|sans\\|\\)") ; 4 | |
384 | (adstyle "[^-]*") ; 4 | |
385 | (pixelsize "[0-9]+") | |
386 | (pointsize "[0-9][0-9]+") | |
387 | (resx "[0-9][0-9]+") | |
388 | (resy "[0-9][0-9]+") | |
389 | (spacing "[cmp?*]") | |
390 | (avgwidth "[0-9]+") | |
391 | (registry "[^-]+") | |
392 | (encoding "[^-]+") | |
393 | ) | |
394 | (setq x-font-regexp | |
395 | (concat "\\`\\*?[-?*]" | |
396 | foundry - family - weight\? - slant\? - swidth - adstyle - | |
397 | pixelsize - pointsize - resx - resy - spacing - registry - | |
398 | encoding "[-?*]\\*?\\'" | |
399 | )) | |
400 | (setq x-font-regexp-head | |
401 | (concat "\\`[-?*]" foundry - family - weight\? - slant\? | |
402 | "\\([-*?]\\|\\'\\)")) | |
403 | (setq x-font-regexp-slant (concat - slant -)) | |
404 | (setq x-font-regexp-weight (concat - weight -)) | |
405 | nil) | |
406 | ||
407 | (defun x-frob-font-weight (font which) | |
408 | (if (or (string-match x-font-regexp font) | |
409 | (string-match x-font-regexp-head font) | |
410 | (string-match x-font-regexp-weight font)) | |
411 | (concat (substring font 0 (match-beginning 1)) which | |
412 | (substring font (match-end 1))) | |
413 | nil)) | |
414 | ||
415 | (defun x-frob-font-slant (font which) | |
416 | (cond ((or (string-match x-font-regexp font) | |
417 | (string-match x-font-regexp-head font)) | |
418 | (concat (substring font 0 (match-beginning 2)) which | |
419 | (substring font (match-end 2)))) | |
420 | ((string-match x-font-regexp-slant font) | |
421 | (concat (substring font 0 (match-beginning 1)) which | |
422 | (substring font (match-end 1)))) | |
423 | (t nil))) | |
424 | ||
425 | ||
426 | (defun x-make-font-bold (font) | |
427 | "Given an X font specification, this attempts to make a `bold' version | |
428 | of it. If it fails, it returns nil." | |
429 | (x-frob-font-weight font "bold")) | |
430 | ||
431 | (defun x-make-font-demibold (font) | |
432 | "Given an X font specification, this attempts to make a `demibold' version | |
433 | of it. If it fails, it returns nil." | |
434 | (x-frob-font-weight font "demibold")) | |
435 | ||
436 | (defun x-make-font-unbold (font) | |
437 | "Given an X font specification, this attempts to make a non-bold version | |
438 | of it. If it fails, it returns nil." | |
439 | (x-frob-font-weight font "medium")) | |
440 | ||
441 | (defun x-make-font-italic (font) | |
442 | "Given an X font specification, this attempts to make an `italic' version | |
443 | of it. If it fails, it returns nil." | |
444 | (x-frob-font-slant font "i")) | |
445 | ||
446 | (defun x-make-font-oblique (font) ; you say tomayto... | |
447 | "Given an X font specification, this attempts to make an `italic' version | |
448 | of it. If it fails, it returns nil." | |
449 | (x-frob-font-slant font "o")) | |
450 | ||
451 | (defun x-make-font-unitalic (font) | |
452 | "Given an X font specification, this attempts to make a non-italic version | |
453 | of it. If it fails, it returns nil." | |
454 | (x-frob-font-slant font "r")) | |
455 | ||
456 | \f | |
457 | ;;; non-X-specific interface | |
458 | ||
bb9a81fc | 459 | (defun make-face-bold (face &optional frame noerror) |
465fceed | 460 | "Make the font of the given face be bold, if possible. |
bb9a81fc | 461 | If NOERROR is non-nil, return nil on failure." |
465fceed | 462 | (interactive (list (read-face-name "Make which face bold: "))) |
bb9a81fc JB |
463 | (let ((ofont (face-font face frame)) |
464 | font f2) | |
465fceed ER |
465 | (if (null frame) |
466 | (let ((frames (frame-list))) | |
467 | (while frames | |
468 | (make-face-bold face (car frames)) | |
469 | (setq frames (cdr frames)))) | |
470 | (setq face (internal-get-face face frame)) | |
bb9a81fc JB |
471 | (setq font (or (face-font face frame) |
472 | (face-font face t) | |
473 | (face-font 'default frame) | |
474 | (cdr (assq 'font (frame-parameters frame))))) | |
475 | (or (and (setq f2 (x-make-font-bold font)) | |
476 | (internal-try-face-font face f2)) | |
477 | (and (setq f2 (x-make-font-demibold font)) | |
478 | (internal-try-face-font face f2)))) | |
479 | (or (not (equal ofont (face-font face))) | |
480 | (and (not noerror) | |
481 | (error "No %s version of %S" face ofont))))) | |
482 | ||
483 | (defun make-face-italic (face &optional frame noerror) | |
465fceed | 484 | "Make the font of the given face be italic, if possible. |
bb9a81fc | 485 | If NOERROR is non-nil, return nil on failure." |
465fceed | 486 | (interactive (list (read-face-name "Make which face italic: "))) |
bb9a81fc JB |
487 | (let ((ofont (face-font face frame)) |
488 | font f2) | |
465fceed ER |
489 | (if (null frame) |
490 | (let ((frames (frame-list))) | |
491 | (while frames | |
492 | (make-face-italic face (car frames)) | |
493 | (setq frames (cdr frames)))) | |
494 | (setq face (internal-get-face face frame)) | |
bb9a81fc JB |
495 | (setq font (or (face-font face frame) |
496 | (face-font face t) | |
497 | (face-font 'default frame) | |
498 | (cdr (assq 'font (frame-parameters frame))))) | |
499 | (or (and (setq f2 (x-make-font-italic font)) | |
500 | (internal-try-face-font face f2)) | |
501 | (and (setq f2 (x-make-font-oblique font)) | |
502 | (internal-try-face-font face f2)))) | |
503 | (or (not (equal ofont (face-font face))) | |
504 | (and (not noerror) | |
505 | (error "No %s version of %S" face ofont))))) | |
506 | ||
507 | (defun make-face-bold-italic (face &optional frame noerror) | |
465fceed | 508 | "Make the font of the given face be bold and italic, if possible. |
bb9a81fc | 509 | If NOERROR is non-nil, return nil on failure." |
465fceed | 510 | (interactive (list (read-face-name "Make which face bold-italic: "))) |
bb9a81fc JB |
511 | (let ((ofont (face-font face frame)) |
512 | font f2 f3) | |
465fceed ER |
513 | (if (null frame) |
514 | (let ((frames (frame-list))) | |
515 | (while frames | |
516 | (make-face-bold-italic face (car frames)) | |
517 | (setq frames (cdr frames)))) | |
518 | (setq face (internal-get-face face frame)) | |
bb9a81fc | 519 | (setq font (or (face-font face frame) |
465fceed | 520 | (face-font face t) |
bb9a81fc JB |
521 | (face-font 'default frame) |
522 | (cdr (assq 'font (frame-parameters frame))))) | |
523 | (or (and (setq f2 (x-make-font-italic font)) | |
524 | (not (equal font f2)) | |
525 | (setq f3 (x-make-font-bold f2)) | |
526 | (not (equal f2 f3)) | |
527 | (internal-try-face-font face f3)) | |
528 | (and (setq f2 (x-make-font-oblique font)) | |
529 | (not (equal font f2)) | |
530 | (setq f3 (x-make-font-bold f2)) | |
531 | (not (equal f2 f3)) | |
532 | (internal-try-face-font face f3)) | |
533 | (and (setq f2 (x-make-font-italic font)) | |
534 | (not (equal font f2)) | |
535 | (setq f3 (x-make-font-demibold f2)) | |
536 | (not (equal f2 f3)) | |
537 | (internal-try-face-font face f3)) | |
538 | (and (setq f2 (x-make-font-oblique font)) | |
539 | (not (equal font f2)) | |
540 | (setq f3 (x-make-font-demibold f2)) | |
541 | (not (equal f2 f3)) | |
542 | (internal-try-face-font face f3)))) | |
543 | (or (not (equal ofont (face-font face))) | |
544 | (and (not noerror) | |
545 | (error "No %s version of %S" face ofont))))) | |
546 | ||
547 | (defun make-face-unbold (face &optional frame noerror) | |
465fceed | 548 | "Make the font of the given face be non-bold, if possible. |
bb9a81fc | 549 | If NOERROR is non-nil, return nil on failure." |
465fceed | 550 | (interactive (list (read-face-name "Make which face non-bold: "))) |
bb9a81fc JB |
551 | (let ((ofont (face-font face frame)) |
552 | font font1) | |
465fceed ER |
553 | (if (null frame) |
554 | (let ((frames (frame-list))) | |
555 | (while frames | |
556 | (make-face-unbold face (car frames)) | |
557 | (setq frames (cdr frames)))) | |
558 | (setq face (internal-get-face face frame)) | |
bb9a81fc JB |
559 | (setq font1 (or (face-font face frame) |
560 | (face-font face t) | |
561 | (face-font 'default frame) | |
562 | (cdr (assq 'font (frame-parameters frame))))) | |
563 | (setq font (x-make-font-unbold font1)) | |
564 | (if font (internal-try-face-font face font))) | |
565 | (or (not (equal ofont (face-font face))) | |
566 | (and (not noerror) | |
567 | (error "No %s version of %S" face ofont))))) | |
568 | ||
569 | (defun make-face-unitalic (face &optional frame noerror) | |
465fceed | 570 | "Make the font of the given face be non-italic, if possible. |
bb9a81fc | 571 | If NOERROR is non-nil, return nil on failure." |
465fceed | 572 | (interactive (list (read-face-name "Make which face non-italic: "))) |
bb9a81fc JB |
573 | (let ((ofont (face-font face frame)) |
574 | font font1) | |
465fceed ER |
575 | (if (null frame) |
576 | (let ((frames (frame-list))) | |
577 | (while frames | |
578 | (make-face-unitalic face (car frames)) | |
579 | (setq frames (cdr frames)))) | |
580 | (setq face (internal-get-face face frame)) | |
bb9a81fc JB |
581 | (setq font1 (or (face-font face frame) |
582 | (face-font face t) | |
583 | (face-font 'default frame) | |
584 | (cdr (assq 'font (frame-parameters frame))))) | |
585 | (setq font (x-make-font-unitalic font1)) | |
586 | (if font (internal-try-face-font face font))) | |
587 | (or (not (equal ofont (face-font face))) | |
588 | (and (not noerror) | |
589 | (error "No %s version of %S" face ofont))))) | |
465fceed ER |
590 | \f |
591 | ;;; Make the builtin faces; the C code knows these as faces 0, 1, and 2, | |
592 | ;;; respectively, so they must be the first three faces made. | |
593 | ||
594 | (if (internal-find-face 'default) | |
595 | nil | |
596 | (make-face 'default) | |
597 | (make-face 'modeline) | |
598 | (make-face 'highlight) | |
599 | ;; | |
600 | ;; These aren't really special in any way, but they're nice to have around. | |
601 | ;; The X-specific code is clever at them. | |
602 | ;; | |
603 | (make-face 'bold) | |
604 | (make-face 'italic) | |
605 | (make-face 'bold-italic) | |
606 | (make-face 'primary-selection) | |
607 | (make-face 'secondary-selection)) | |
608 | \f | |
609 | ;;; This really belongs in setting a frame's own font. | |
610 | ;;; ;; | |
611 | ;;; ;; No font specified in the resource database; try to cope. | |
612 | ;;; ;; | |
613 | ;;; (internal-try-face-font default "-*-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-*" | |
614 | ;;; frame) | |
615 | ;;; (internal-try-face-font default "-*-courier-*-r-*-*-*-120-*-*-*-*-iso8859-*" | |
616 | ;;; frame) | |
617 | ;;; (internal-try-face-font default "-*-*-medium-r-*-*-*-120-*-*-m-*-iso8859-*" frame) | |
618 | ;;; (internal-try-face-font default "-*-*-medium-r-*-*-*-120-*-*-c-*-iso8859-*" frame) | |
619 | ;;; (internal-try-face-font default "-*-*-*-r-*-*-*-120-*-*-m-*-iso8859-*" frame) | |
620 | ;;; (internal-try-face-font default "-*-*-*-r-*-*-*-120-*-*-c-*-iso8859-*" frame) | |
621 | ;;; (internal-try-face-font default "-*-*-*-r-*-*-*-120-*-*-*-*-iso8859-*" frame) | |
622 | ||
623 | ||
624 | ;;; This is called from make-screen-initial-faces to make sure that the | |
625 | ;;; "default" and "modeline" faces for this screen have enough attributes | |
626 | ;;; specified for emacs to be able to display anything on it. This had | |
627 | ;;; better not signal an error. | |
628 | ;;; | |
629 | (defun x-initialize-frame-faces (frame) | |
630 | (or (face-differs-from-default-p 'bold frame) | |
bb9a81fc | 631 | (make-face-bold 'bold frame t) |
465fceed | 632 | ;; if default font is bold, then make the `bold' face be unbold. |
bb9a81fc | 633 | (make-face-unbold 'bold frame t) |
465fceed | 634 | ;; otherwise the luser specified one of the bogus font names |
bb9a81fc | 635 | (internal-x-complain-about-font 'bold frame) |
465fceed ER |
636 | ) |
637 | ||
638 | (or (face-differs-from-default-p 'italic frame) | |
bb9a81fc | 639 | (make-face-italic 'italic frame t) |
465fceed | 640 | (progn |
bb9a81fc JB |
641 | (make-face-bold 'italic frame t) |
642 | (internal-x-complain-about-font 'italic frame)) | |
465fceed ER |
643 | ) |
644 | ||
645 | (or (face-differs-from-default-p 'bold-italic frame) | |
bb9a81fc | 646 | (make-face-bold-italic 'bold-italic frame t) |
465fceed | 647 | ;; if we couldn't get a bold-italic version, try just bold. |
bb9a81fc | 648 | (make-face-bold 'bold-italic frame t) |
465fceed ER |
649 | ;; if we couldn't get bold or bold-italic, then that's probably because |
650 | ;; the default font is bold, so make the `bold-italic' face be unbold. | |
bb9a81fc JB |
651 | (and (make-face-unbold 'bold-italic frame t) |
652 | (make-face-italic 'bold-italic frame t)) | |
465fceed ER |
653 | ;; if that didn't work, try italic (can this ever happen? what the hell.) |
654 | (progn | |
bb9a81fc | 655 | (make-face-italic 'bold-italic frame t) |
465fceed | 656 | ;; then bitch and moan. |
bb9a81fc | 657 | (internal-x-complain-about-font 'bold-italic frame)) |
465fceed ER |
658 | ) |
659 | ||
660 | (or (face-differs-from-default-p 'highlight frame) | |
661 | (condition-case () | |
662 | (if (x-display-color-p) | |
663 | (condition-case () | |
664 | (set-face-background 'highlight "darkseagreen2" frame) | |
665 | (error (set-face-background 'highlight "green" frame))) | |
666 | (set-face-background-pixmap 'highlight "gray1" frame) | |
667 | ) | |
668 | (error (invert-face 'highlight frame)))) | |
669 | ||
670 | (or (face-differs-from-default-p 'primary-selection frame) | |
671 | (condition-case () | |
672 | (if (x-display-color-p) | |
673 | (set-face-background 'primary-selection "gray" frame) | |
674 | (set-face-background-pixmap 'primary-selection "gray3" frame) | |
675 | ) | |
676 | (error (invert-face 'primary-selection frame)))) | |
677 | ||
678 | (or (face-differs-from-default-p 'secondary-selection frame) | |
679 | (condition-case () | |
680 | (if (x-display-color-p) | |
681 | (condition-case () | |
682 | ;; some older X servers don't have this one. | |
683 | (set-face-background 'secondary-selection "paleturquoise" | |
684 | frame) | |
685 | (error | |
686 | (set-face-background 'secondary-selection "green" frame))) | |
687 | (set-face-background-pixmap 'secondary-selection "gray1" frame) | |
688 | ) | |
689 | (error (invert-face 'secondary-selection frame)))) | |
bb9a81fc | 690 | ) |
465fceed | 691 | |
bb9a81fc JB |
692 | (defun internal-x-complain-about-font (face frame) |
693 | (message "No %s version of %S" | |
465fceed | 694 | face |
bb9a81fc JB |
695 | (or (face-font face frame) |
696 | (face-font face t) | |
697 | (face-font 'default frame) | |
698 | (cdr (assq 'font (frame-parameters frame))))) | |
465fceed ER |
699 | (sit-for 1)) |
700 | \f | |
701 | ;; Like x-create-frame but also set up the faces. | |
702 | ||
703 | (defun x-create-frame-with-faces (&optional parameters) | |
704 | (let* ((frame (x-create-frame parameters)) | |
705 | (faces (copy-alist global-face-data)) | |
706 | (rest faces) | |
707 | default modeline) | |
708 | (set-frame-face-alist frame faces) | |
709 | ||
710 | ;; Copy the vectors that represent the faces. | |
711 | ;; Also fill them in from X resources. | |
712 | (while rest | |
713 | (setcdr (car rest) (copy-sequence (cdr (car rest)))) | |
bb9a81fc | 714 | (make-face-x-resource-internal (cdr (car rest)) frame t) |
465fceed ER |
715 | (setq rest (cdr rest))) |
716 | ||
717 | (setq default (internal-get-face 'default frame) | |
718 | modeline (internal-get-face 'modeline frame)) | |
719 | ||
720 | (x-initialize-frame-faces frame) | |
721 | ||
bb9a81fc JB |
722 | ;;; ;; Make sure the modeline face is fully qualified. |
723 | ;;; (if (and (not (face-font modeline frame)) (face-font default frame)) | |
724 | ;;; (set-face-font modeline (face-font default frame) frame)) | |
725 | ;;; (if (and (not (face-background modeline frame)) | |
726 | ;;; (face-background default frame)) | |
727 | ;;; (set-face-background modeline (face-background default frame) frame)) | |
728 | ;;; (if (and (not (face-foreground modeline frame)) | |
729 | ;;; (face-foreground default frame)) | |
730 | ;;; (set-face-foreground modeline (face-foreground default frame) frame)) | |
465fceed ER |
731 | frame)) |
732 | ||
465fceed ER |
733 | ;; Set up the faces of all existing frames. |
734 | (let ((frames (frame-list))) | |
735 | (while frames | |
f0138172 JB |
736 | (if (eq (framep (car frames)) 'x) |
737 | (x-initialize-frame-faces (car frames))) | |
465fceed ER |
738 | (setq frames (cdr frames)))) |
739 | ||
f0138172 JB |
740 | (provide 'faces) |
741 | ||
465fceed | 742 | ;;; faces.el ends here |