Commit | Line | Data |
---|---|---|
465fceed ER |
1 | ;;; faces.el --- Lisp interface to the c "face" structure |
2 | ||
b578f267 | 3 | ;; Copyright (C) 1992, 1993, 1994, 1995, 1996 Free Software Foundation, Inc. |
465fceed ER |
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 | |
b578f267 EN |
18 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
19 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
20 | ;; Boston, MA 02111-1307, USA. | |
465fceed ER |
21 | |
22 | ;;; Commentary: | |
23 | ||
24 | ;; Mostly derived from Lucid. | |
25 | ||
26 | ;;; Code: | |
27 | ||
39b3b754 RS |
28 | (eval-when-compile |
29 | ;; These used to be defsubsts, now they're subrs. Avoid losing if we're | |
30 | ;; being compiled with an old Emacs that still has defsubrs in it. | |
31 | (put 'face-name 'byte-optimizer nil) | |
32 | (put 'face-id 'byte-optimizer nil) | |
33 | (put 'face-font 'byte-optimizer nil) | |
87b6ab4c | 34 | (put 'face-font-explicit 'byte-optimizer nil) |
39b3b754 RS |
35 | (put 'face-foreground 'byte-optimizer nil) |
36 | (put 'face-background 'byte-optimizer nil) | |
37 | (put 'face-stipple 'byte-optimizer nil) | |
38 | (put 'face-underline-p 'byte-optimizer nil) | |
39 | (put 'set-face-font 'byte-optimizer nil) | |
87b6ab4c | 40 | (put 'set-face-font-auto 'byte-optimizer nil) |
39b3b754 RS |
41 | (put 'set-face-foreground 'byte-optimizer nil) |
42 | (put 'set-face-background 'byte-optimizer nil) | |
ca58b3ec | 43 | (put 'set-face-stipple 'byte-optimizer nil) |
39b3b754 | 44 | (put 'set-face-underline-p 'byte-optimizer nil)) |
bdda3754 JB |
45 | \f |
46 | ;;;; Functions for manipulating face vectors. | |
47 | ||
48 | ;;; A face vector is a vector of the form: | |
d343b6f1 RS |
49 | ;;; [face NAME ID FONT FOREGROUND BACKGROUND STIPPLE |
50 | ;;; UNDERLINE-P INVERSE-VIDEO-P FONT-EXPLICIT-P BOLD-P ITALIC-P] | |
bdda3754 JB |
51 | |
52 | ;;; Type checkers. | |
465fceed | 53 | (defsubst internal-facep (x) |
1bcb155c | 54 | (and (vectorp x) (= (length x) 12) (eq (aref x 0) 'face))) |
465fceed | 55 | |
962a60aa RS |
56 | (defun facep (x) |
57 | "Return t if X is a face name or an internal face vector." | |
58 | (and (or (internal-facep x) | |
59 | (and (symbolp x) (assq x global-face-data))) | |
60 | t)) | |
61 | ||
465fceed | 62 | (defmacro internal-check-face (face) |
962a60aa RS |
63 | (` (or (internal-facep (, face)) |
64 | (signal 'wrong-type-argument (list 'internal-facep (, face)))))) | |
465fceed | 65 | |
bdda3754 | 66 | ;;; Accessors. |
feaad827 | 67 | (defun face-name (face) |
465fceed ER |
68 | "Return the name of face FACE." |
69 | (aref (internal-get-face face) 1)) | |
70 | ||
feaad827 | 71 | (defun face-id (face) |
465fceed ER |
72 | "Return the internal ID number of face FACE." |
73 | (aref (internal-get-face face) 2)) | |
74 | ||
feaad827 | 75 | (defun face-font (face &optional frame) |
465fceed ER |
76 | "Return the font name of face FACE, or nil if it is unspecified. |
77 | If the optional argument FRAME is given, report on face FACE in that frame. | |
f3f31ccf RS |
78 | If FRAME is t, report on the defaults for face FACE (for new frames). |
79 | The font default for a face is either nil, or a list | |
80 | of the form (bold), (italic) or (bold italic). | |
81 | If FRAME is omitted or nil, use the selected frame." | |
465fceed ER |
82 | (aref (internal-get-face face frame) 3)) |
83 | ||
feaad827 | 84 | (defun face-foreground (face &optional frame) |
465fceed ER |
85 | "Return the foreground color name of face FACE, or nil if unspecified. |
86 | If the optional argument FRAME is given, report on face FACE in that frame. | |
f3f31ccf RS |
87 | If FRAME is t, report on the defaults for face FACE (for new frames). |
88 | If FRAME is omitted or nil, use the selected frame." | |
465fceed ER |
89 | (aref (internal-get-face face frame) 4)) |
90 | ||
feaad827 | 91 | (defun face-background (face &optional frame) |
465fceed ER |
92 | "Return the background color name of face FACE, or nil if unspecified. |
93 | If the optional argument FRAME is given, report on face FACE in that frame. | |
f3f31ccf RS |
94 | If FRAME is t, report on the defaults for face FACE (for new frames). |
95 | If FRAME is omitted or nil, use the selected frame." | |
465fceed ER |
96 | (aref (internal-get-face face frame) 5)) |
97 | ||
feaad827 | 98 | (defun face-stipple (face &optional frame) |
72418504 RS |
99 | "Return the stipple pixmap name of face FACE, or nil if unspecified. |
100 | If the optional argument FRAME is given, report on face FACE in that frame. | |
101 | If FRAME is t, report on the defaults for face FACE (for new frames). | |
102 | If FRAME is omitted or nil, use the selected frame." | |
103 | (aref (internal-get-face face frame) 6)) | |
104 | ||
105 | (defalias 'face-background-pixmap 'face-stipple) | |
465fceed | 106 | |
feaad827 | 107 | (defun face-underline-p (face &optional frame) |
465fceed ER |
108 | "Return t if face FACE is underlined. |
109 | If the optional argument FRAME is given, report on face FACE in that frame. | |
f3f31ccf RS |
110 | If FRAME is t, report on the defaults for face FACE (for new frames). |
111 | If FRAME is omitted or nil, use the selected frame." | |
465fceed ER |
112 | (aref (internal-get-face face frame) 7)) |
113 | ||
e8e4cda0 RS |
114 | (defun face-inverse-video-p (face &optional frame) |
115 | "Return t if face FACE is in inverse video. | |
116 | If the optional argument FRAME is given, report on face FACE in that frame. | |
117 | If FRAME is t, report on the defaults for face FACE (for new frames). | |
118 | If FRAME is omitted or nil, use the selected frame." | |
119 | (aref (internal-get-face face frame) 8)) | |
120 | ||
1bcb155c RS |
121 | (defun face-font-explicit (face &optional frame) |
122 | "Return non-nil if this face's font was explicitly specified." | |
123 | (aref (internal-get-face face frame) 9)) | |
124 | ||
d11fba25 RS |
125 | (defun face-bold-p (face &optional frame) |
126 | "Return non-nil if the font of FACE is bold. | |
127 | If the optional argument FRAME is given, report on face FACE in that frame. | |
128 | If FRAME is t, report on the defaults for face FACE (for new frames). | |
d11fba25 | 129 | If FRAME is omitted or nil, use the selected frame." |
1bcb155c | 130 | (aref (internal-get-face face frame) 10)) |
d11fba25 RS |
131 | |
132 | (defun face-italic-p (face &optional frame) | |
133 | "Return non-nil if the font of FACE is italic. | |
134 | If the optional argument FRAME is given, report on face FACE in that frame. | |
135 | If FRAME is t, report on the defaults for face FACE (for new frames). | |
d11fba25 | 136 | If FRAME is omitted or nil, use the selected frame." |
1bcb155c | 137 | (aref (internal-get-face face frame) 11)) |
d11fba25 | 138 | |
02e0989b RS |
139 | (defalias 'face-doc-string 'face-documentation) |
140 | (defun face-documentation (face) | |
d11fba25 RS |
141 | "Get the documentation string for FACE." |
142 | (get face 'face-documentation)) | |
bdda3754 JB |
143 | \f |
144 | ;;; Mutators. | |
465fceed | 145 | |
feaad827 | 146 | (defun set-face-font (face font &optional frame) |
465fceed ER |
147 | "Change the font of face FACE to FONT (a string). |
148 | If the optional FRAME argument is provided, change only | |
87b6ab4c RS |
149 | in that frame; otherwise change each frame." |
150 | (interactive (internal-face-interactive "font")) | |
151 | (if (stringp font) | |
8cd2ac8d | 152 | (setq font (or (resolve-fontset-name font) |
87b6ab4c RS |
153 | (x-resolve-font-name font 'default frame)))) |
154 | (internal-set-face-1 face 'font font 3 frame) | |
155 | ;; Record that this face's font was set explicitly, not automatically, | |
156 | ;; unless we are setting it to nil. | |
157 | (internal-set-face-1 face nil (not (null font)) 9 frame)) | |
158 | ||
159 | (defun set-face-font-auto (face font &optional frame) | |
160 | "Change the font of face FACE to FONT (a string), for an automatic change. | |
161 | An automatic change means that we don't change the \"explicit\" flag; | |
162 | if the font was derived from the frame font before, it is now. | |
163 | If the optional FRAME argument is provided, change only | |
465fceed ER |
164 | in that frame; otherwise change each frame." |
165 | (interactive (internal-face-interactive "font")) | |
3aa3a18c | 166 | (if (stringp font) |
8cd2ac8d | 167 | (setq font (or (resolve-fontset-name font) |
3aa3a18c | 168 | (x-resolve-font-name font 'default frame)))) |
10d89673 | 169 | (internal-set-face-1 face 'font font 3 frame)) |
465fceed | 170 | |
87b6ab4c RS |
171 | (defun set-face-font-explicit (face flag &optional frame) |
172 | "Set the explicit-font flag of face FACE to FLAG. | |
173 | If the optional FRAME argument is provided, change only | |
174 | in that frame; otherwise change each frame." | |
e9e59456 | 175 | (internal-set-face-1 face nil flag 9 frame)) |
87b6ab4c | 176 | |
feaad827 | 177 | (defun set-face-foreground (face color &optional frame) |
465fceed ER |
178 | "Change the foreground color of face FACE to COLOR (a string). |
179 | If the optional FRAME argument is provided, change only | |
180 | in that frame; otherwise change each frame." | |
f60a629a | 181 | (interactive (internal-face-interactive "foreground" 'color)) |
f0138172 | 182 | (internal-set-face-1 face 'foreground color 4 frame)) |
465fceed | 183 | |
ef436392 KH |
184 | (defvar face-default-stipple "gray3" |
185 | "Default stipple pattern used on monochrome displays. | |
186 | This stipple pattern is used on monochrome displays | |
187 | instead of shades of gray for a face background color. | |
188 | See `set-face-stipple' for possible values for this variable.") | |
189 | ||
190 | (defun face-color-gray-p (color &optional frame) | |
191 | "Return t if COLOR is a shade of gray (or white or black). | |
192 | FRAME specifies the frame and thus the display for interpreting COLOR." | |
193 | (let* ((values (x-color-values color frame)) | |
194 | (r (nth 0 values)) | |
195 | (g (nth 1 values)) | |
196 | (b (nth 2 values))) | |
25ae394e RS |
197 | (and values |
198 | (< (abs (- r g)) (/ (max 1 (abs r) (abs g)) 20)) | |
ef436392 KH |
199 | (< (abs (- g b)) (/ (max 1 (abs g) (abs b)) 20)) |
200 | (< (abs (- b r)) (/ (max 1 (abs b) (abs r)) 20))))) | |
201 | ||
feaad827 | 202 | (defun set-face-background (face color &optional frame) |
465fceed ER |
203 | "Change the background color of face FACE to COLOR (a string). |
204 | If the optional FRAME argument is provided, change only | |
205 | in that frame; otherwise change each frame." | |
f60a629a | 206 | (interactive (internal-face-interactive "background" 'color)) |
ee09252a RS |
207 | ;; For a specific frame, use gray stipple instead of gray color |
208 | ;; if the display does not support a gray color. | |
21ef90ee | 209 | (if (and frame (not (eq frame t)) color |
a7acbbe4 | 210 | ;; Check for support for foreground, not for background! |
f1d71b2f RS |
211 | ;; face-color-supported-p is smart enough to know |
212 | ;; that grays are "supported" as background | |
213 | ;; because we are supposed to use stipple for them! | |
214 | (not (face-color-supported-p frame color nil))) | |
ef436392 | 215 | (set-face-stipple face face-default-stipple frame) |
d0672e0d RS |
216 | (if (null frame) |
217 | (let ((frames (frame-list))) | |
218 | (while frames | |
219 | (set-face-background (face-name face) color (car frames)) | |
220 | (setq frames (cdr frames))) | |
221 | (set-face-background face color t) | |
222 | color) | |
223 | (internal-set-face-1 face 'background color 5 frame)))) | |
465fceed | 224 | |
ef436392 | 225 | (defun set-face-stipple (face pixmap &optional frame) |
72418504 RS |
226 | "Change the stipple pixmap of face FACE to PIXMAP. |
227 | PIXMAP should be a string, the name of a file of pixmap data. | |
228 | The directories listed in the `x-bitmap-file-path' variable are searched. | |
465fceed | 229 | |
72418504 RS |
230 | Alternatively, PIXMAP may be a list of the form (WIDTH HEIGHT DATA) |
231 | where WIDTH and HEIGHT are the size in pixels, | |
232 | and DATA is a string, containing the raw bits of the bitmap. | |
465fceed | 233 | |
72418504 RS |
234 | If the optional FRAME argument is provided, change only |
235 | in that frame; otherwise change each frame." | |
3c5ddb48 | 236 | (interactive (internal-face-interactive-stipple "stipple")) |
ef436392 | 237 | (internal-set-face-1 face 'background-pixmap pixmap 6 frame)) |
72418504 RS |
238 | |
239 | (defalias 'set-face-background-pixmap 'set-face-stipple) | |
465fceed | 240 | |
feaad827 | 241 | (defun set-face-underline-p (face underline-p &optional frame) |
465fceed ER |
242 | "Specify whether face FACE is underlined. (Yes if UNDERLINE-P is non-nil.) |
243 | If the optional FRAME argument is provided, change only | |
244 | in that frame; otherwise change each frame." | |
245 | (interactive (internal-face-interactive "underline-p" "underlined")) | |
f0138172 | 246 | (internal-set-face-1 face 'underline underline-p 7 frame)) |
d11fba25 | 247 | |
e8e4cda0 RS |
248 | (defun set-face-inverse-video-p (face inverse-video-p &optional frame) |
249 | "Specify whether face FACE is in inverse video. | |
250 | \(Yes if INVERSE-VIDEO-P is non-nil.) | |
251 | If the optional FRAME argument is provided, change only | |
252 | in that frame; otherwise change each frame." | |
253 | (interactive (internal-face-interactive "inverse-video-p" "inverse-video")) | |
254 | (internal-set-face-1 face 'inverse-video inverse-video-p 8 frame)) | |
255 | ||
d11fba25 RS |
256 | (defun set-face-bold-p (face bold-p &optional frame) |
257 | "Specify whether face FACE is bold. (Yes if BOLD-P is non-nil.) | |
258 | If the optional FRAME argument is provided, change only | |
259 | in that frame; otherwise change each frame." | |
260 | (cond ((eq bold-p nil) (make-face-unbold face frame t)) | |
261 | (t (make-face-bold face frame t)))) | |
262 | ||
263 | (defun set-face-italic-p (face italic-p &optional frame) | |
264 | "Specify whether face FACE is italic. (Yes if ITALIC-P is non-nil.) | |
265 | If the optional FRAME argument is provided, change only | |
266 | in that frame; otherwise change each frame." | |
267 | (cond ((eq italic-p nil) (make-face-unitalic face frame t)) | |
268 | (t (make-face-italic face frame t)))) | |
269 | ||
e42537fe | 270 | (defalias 'set-face-doc-string 'set-face-documentation) |
02e0989b | 271 | (defun set-face-documentation (face string) |
d11fba25 RS |
272 | "Set the documentation string for FACE to STRING." |
273 | (put face 'face-documentation string)) | |
1c0a8710 | 274 | \f |
2b979e14 | 275 | (defun modify-face-read-string (face default name alist) |
6ffb01c4 RS |
276 | (let ((value |
277 | (completing-read | |
278 | (if default | |
279 | (format "Set face %s %s (default %s): " | |
280 | face name (downcase default)) | |
281 | (format "Set face %s %s: " face name)) | |
282 | alist))) | |
283 | (cond ((equal value "none") | |
077fcb28 | 284 | '(nil)) |
6ffb01c4 RS |
285 | ((equal value "") |
286 | default) | |
287 | (t value)))) | |
288 | ||
289 | (defun modify-face (face foreground background stipple | |
d343b6f1 | 290 | bold-p italic-p underline-p &optional inverse-p frame) |
1c0a8710 | 291 | "Change the display attributes for face FACE. |
905cf8f2 SM |
292 | If the optional FRAME argument is provided, change only |
293 | in that frame; otherwise change each frame. | |
294 | ||
295 | FOREGROUND and BACKGROUND should be a colour name string (or list of strings to | |
296 | try) or nil. STIPPLE should be a stipple pattern name string or nil. | |
297 | If nil, means do not change the display attribute corresponding to that arg. | |
077fcb28 | 298 | If (nil), that means clear out the attribute. |
905cf8f2 | 299 | |
d343b6f1 RS |
300 | BOLD-P, ITALIC-P, UNDERLINE-P, and INVERSE-P specify whether |
301 | the face should be set bold, italic, underlined or in inverse-video, | |
302 | respectively. If one of these arguments is neither nil or t, it means do not | |
303 | change the display attribute corresponding to that argument. | |
905cf8f2 SM |
304 | |
305 | If called interactively, prompts for a face name and face attributes." | |
1c0a8710 RS |
306 | (interactive |
307 | (let* ((completion-ignore-case t) | |
905cf8f2 SM |
308 | (face (symbol-name (read-face-name "Modify face: "))) |
309 | (colors (mapcar 'list x-colors)) | |
310 | (stipples (mapcar 'list (apply 'nconc | |
311 | (mapcar 'directory-files | |
312 | x-bitmap-file-path)))) | |
313 | (foreground (modify-face-read-string | |
314 | face (face-foreground (intern face)) | |
315 | "foreground" colors)) | |
316 | (background (modify-face-read-string | |
317 | face (face-background (intern face)) | |
318 | "background" colors)) | |
e5de0238 RS |
319 | ;; If the stipple value is a list (WIDTH HEIGHT DATA), |
320 | ;; represent that as a string by printing it out. | |
321 | (old-stipple-string | |
322 | (if (stringp (face-stipple (intern face))) | |
323 | (face-stipple (intern face)) | |
3c5ddb48 RS |
324 | (if (face-stipple (intern face)) |
325 | (prin1-to-string (face-stipple (intern face)))))) | |
e5de0238 RS |
326 | (new-stipple-string |
327 | (modify-face-read-string | |
328 | face old-stipple-string | |
329 | "stipple" stipples)) | |
330 | ;; Convert the stipple value text we read | |
331 | ;; back to a list if it looks like one. | |
332 | ;; This makes the assumption that a pixmap file name | |
333 | ;; won't start with an open-paren. | |
334 | (stipple | |
3c5ddb48 RS |
335 | (and new-stipple-string |
336 | (if (string-match "^(" new-stipple-string) | |
337 | (read new-stipple-string) | |
338 | new-stipple-string))) | |
339 | (bold-p (y-or-n-p (concat "Should face " face " be bold "))) | |
340 | (italic-p (y-or-n-p (concat "Should face " face " be italic "))) | |
341 | (underline-p (y-or-n-p (concat "Should face " face " be underlined "))) | |
d343b6f1 | 342 | (inverse-p (y-or-n-p (concat "Should face " face " be inverse-video "))) |
905cf8f2 | 343 | (all-frames-p (y-or-n-p (concat "Modify face " face " in all frames ")))) |
1c0a8710 RS |
344 | (message "Face %s: %s" face |
345 | (mapconcat 'identity | |
346 | (delq nil | |
077fcb28 RS |
347 | (list (if (equal foreground '(nil)) |
348 | " no foreground" | |
349 | (and foreground (concat (downcase foreground) " foreground"))) | |
350 | (if (equal background '(nil)) | |
351 | " no background" | |
352 | (and background (concat (downcase background) " background"))) | |
353 | (if (equal stipple '(nil)) | |
354 | " no stipple" | |
355 | (and stipple (concat (downcase new-stipple-string) " stipple"))) | |
1c0a8710 | 356 | (and bold-p "bold") (and italic-p "italic") |
d343b6f1 | 357 | (and inverse-p "inverse") |
1c0a8710 | 358 | (and underline-p "underline"))) ", ")) |
6ffb01c4 | 359 | (list (intern face) foreground background stipple |
d343b6f1 | 360 | bold-p italic-p underline-p inverse-p |
905cf8f2 | 361 | (if all-frames-p nil (selected-frame))))) |
d343b6f1 RS |
362 | ;; Clear this before we install the new foreground and background; |
363 | ;; otherwise, clearing it after would swap them! | |
364 | (when (and (or foreground background) (face-inverse-video-p face)) | |
573d18cb | 365 | (set-face-inverse-video-p face nil frame) |
d343b6f1 RS |
366 | ;; Arrange to restore it after, if we are not setting it now. |
367 | (or (memq inverse-p '(t nil)) | |
368 | (setq inverse-p t))) | |
905cf8f2 SM |
369 | (condition-case nil |
370 | (face-try-color-list 'set-face-foreground face foreground frame) | |
371 | (error nil)) | |
372 | (condition-case nil | |
373 | (face-try-color-list 'set-face-background face background frame) | |
374 | (error nil)) | |
375 | (condition-case nil | |
376 | (set-face-stipple face stipple frame) | |
377 | (error nil)) | |
d343b6f1 RS |
378 | ;; Now that we have the new colors, |
379 | (if (memq inverse-p '(nil t)) | |
380 | (set-face-inverse-video-p face inverse-p frame)) | |
e8e4cda0 RS |
381 | (cond ((eq bold-p nil) |
382 | (if (face-font face frame) | |
383 | (make-face-unbold face frame t))) | |
384 | ((eq bold-p t) | |
385 | (make-face-bold face frame t))) | |
386 | (cond ((eq italic-p nil) | |
387 | (if (face-font face frame) | |
388 | (make-face-unitalic face frame t))) | |
905cf8f2 SM |
389 | ((eq italic-p t) (make-face-italic face frame t))) |
390 | (if (memq underline-p '(nil t)) | |
391 | (set-face-underline-p face underline-p frame)) | |
1c0a8710 | 392 | (and (interactive-p) (redraw-display))) |
bdda3754 JB |
393 | \f |
394 | ;;;; Associating face names (symbols) with their face vectors. | |
395 | ||
66a5c2c6 JB |
396 | (defvar global-face-data nil |
397 | "Internal data for face support functions. Not for external use. | |
398 | This is an alist associating face names with the default values for | |
399 | their parameters. Newly created frames get their data from here.") | |
400 | ||
bdda3754 JB |
401 | (defun face-list () |
402 | "Returns a list of all defined face names." | |
403 | (mapcar 'car global-face-data)) | |
404 | ||
405 | (defun internal-find-face (name &optional frame) | |
406 | "Retrieve the face named NAME. Return nil if there is no such face. | |
407 | If the optional argument FRAME is given, this gets the face NAME for | |
408 | that frame; otherwise, it uses the selected frame. | |
409 | If FRAME is the symbol t, then the global, non-frame face is returned. | |
410 | If NAME is already a face, it is simply returned." | |
411 | (if (and (eq frame t) (not (symbolp name))) | |
412 | (setq name (face-name name))) | |
413 | (if (symbolp name) | |
414 | (cdr (assq name | |
415 | (if (eq frame t) | |
416 | global-face-data | |
417 | (frame-face-alist (or frame (selected-frame)))))) | |
418 | (internal-check-face name) | |
419 | name)) | |
420 | ||
421 | (defun internal-get-face (name &optional frame) | |
422 | "Retrieve the face named NAME; error if there is none. | |
423 | If the optional argument FRAME is given, this gets the face NAME for | |
424 | that frame; otherwise, it uses the selected frame. | |
425 | If FRAME is the symbol t, then the global, non-frame face is returned. | |
426 | If NAME is already a face, it is simply returned." | |
427 | (or (internal-find-face name frame) | |
428 | (internal-check-face name))) | |
429 | ||
430 | ||
431 | (defun internal-set-face-1 (face name value index frame) | |
432 | (let ((inhibit-quit t)) | |
433 | (if (null frame) | |
434 | (let ((frames (frame-list))) | |
435 | (while frames | |
436 | (internal-set-face-1 (face-name face) name value index (car frames)) | |
437 | (setq frames (cdr frames))) | |
438 | (aset (internal-get-face (if (symbolp face) face (face-name face)) t) | |
439 | index value) | |
440 | value) | |
e8e4cda0 RS |
441 | (let ((internal-face (internal-get-face face frame))) |
442 | (or (eq frame t) | |
443 | (if (eq name 'inverse-video) | |
444 | (or (eq value (aref internal-face index)) | |
445 | (invert-face face frame)) | |
87b6ab4c RS |
446 | (and name (fboundp 'set-face-attribute-internal) |
447 | (set-face-attribute-internal (face-id face) | |
448 | name value frame)))) | |
e8e4cda0 | 449 | (aset internal-face index value))))) |
bdda3754 JB |
450 | |
451 | ||
452 | (defun read-face-name (prompt) | |
453 | (let (face) | |
454 | (while (= (length face) 0) | |
455 | (setq face (completing-read prompt | |
456 | (mapcar '(lambda (x) (list (symbol-name x))) | |
457 | (face-list)) | |
458 | nil t))) | |
459 | (intern face))) | |
460 | ||
461 | (defun internal-face-interactive (what &optional bool) | |
462 | (let* ((fn (intern (concat "face-" what))) | |
463 | (prompt (concat "Set " what " of face")) | |
464 | (face (read-face-name (concat prompt ": "))) | |
465 | (default (if (fboundp fn) | |
466 | (or (funcall fn face (selected-frame)) | |
467 | (funcall fn 'default (selected-frame))))) | |
f60a629a RS |
468 | value) |
469 | (setq value | |
470 | (cond ((eq bool 'color) | |
471 | (completing-read (concat prompt " " (symbol-name face) " to: ") | |
472 | (mapcar (function (lambda (color) | |
473 | (cons color color))) | |
474 | x-colors) | |
475 | nil nil nil nil default)) | |
476 | (bool | |
477 | (y-or-n-p (concat "Should face " (symbol-name face) | |
478 | " be " bool "? "))) | |
479 | (t | |
480 | (read-string (concat prompt " " (symbol-name face) " to: ") | |
481 | nil nil default)))) | |
bdda3754 JB |
482 | (list face (if (equal value "") nil value)))) |
483 | ||
3c5ddb48 RS |
484 | (defun internal-face-interactive-stipple (what) |
485 | (let* ((fn (intern (concat "face-" what))) | |
486 | (prompt (concat "Set " what " of face")) | |
487 | (face (read-face-name (concat prompt ": "))) | |
488 | (default (if (fboundp fn) | |
489 | (or (funcall fn face (selected-frame)) | |
490 | (funcall fn 'default (selected-frame))))) | |
491 | ;; If the stipple value is a list (WIDTH HEIGHT DATA), | |
492 | ;; represent that as a string by printing it out. | |
493 | (old-stipple-string | |
494 | (if (stringp (face-stipple face)) | |
495 | (face-stipple face) | |
496 | (if (null (face-stipple face)) | |
497 | nil | |
498 | (prin1-to-string (face-stipple face))))) | |
499 | (new-stipple-string | |
500 | (read-string | |
501 | (concat prompt " " (symbol-name face) " to: ") | |
502 | old-stipple-string)) | |
503 | ;; Convert the stipple value text we read | |
504 | ;; back to a list if it looks like one. | |
505 | ;; This makes the assumption that a pixmap file name | |
506 | ;; won't start with an open-paren. | |
507 | (stipple | |
508 | (if (string-match "^(" new-stipple-string) | |
509 | (read new-stipple-string) | |
510 | new-stipple-string))) | |
511 | (list face (if (equal stipple "") nil stipple)))) | |
465fceed | 512 | |
17b3a11b | 513 | (defun make-face (name &optional no-resources) |
465fceed ER |
514 | "Define a new FACE on all frames. |
515 | You can modify the font, color, etc of this face with the set-face- functions. | |
17b3a11b RS |
516 | If NO-RESOURCES is non-nil, then we ignore X resources |
517 | and always make a face whose attributes are all nil. | |
518 | ||
465fceed | 519 | If the face already exists, it is unmodified." |
19fac299 | 520 | (interactive "SMake face: ") |
465fceed | 521 | (or (internal-find-face name) |
1bcb155c | 522 | (let ((face (make-vector 12 nil))) |
465fceed ER |
523 | (aset face 0 'face) |
524 | (aset face 1 name) | |
525 | (let* ((frames (frame-list)) | |
526 | (inhibit-quit t) | |
527 | (id (internal-next-face-id))) | |
60a719b0 RS |
528 | (if (fboundp 'make-face-internal) |
529 | (make-face-internal id)) | |
465fceed ER |
530 | (aset face 2 id) |
531 | (while frames | |
532 | (set-frame-face-alist (car frames) | |
533 | (cons (cons name (copy-sequence face)) | |
534 | (frame-face-alist (car frames)))) | |
535 | (setq frames (cdr frames))) | |
536 | (setq global-face-data (cons (cons name face) global-face-data))) | |
17b3a11b RS |
537 | ;; When making a face after frames already exist |
538 | (or no-resources | |
539 | (if (memq window-system '(x w32)) | |
540 | (make-face-x-resource-internal face))) | |
541 | ;; Add to menu of faces. | |
33af44e8 BG |
542 | (if (fboundp 'facemenu-add-new-face) |
543 | (facemenu-add-new-face name)) | |
7ee29ed8 RS |
544 | face)) |
545 | name) | |
465fceed | 546 | |
17b3a11b RS |
547 | (defun make-empty-face (face) |
548 | "Define a new FACE on all frames, which initially reflects the defaults. | |
549 | You can modify the font, color, etc of this face with the set-face- functions. | |
550 | If the face already exists, it is unmodified." | |
551 | (interactive "SMake empty face: ") | |
552 | (make-face face t)) | |
553 | ||
465fceed ER |
554 | ;; Fill in a face by default based on X resources, for all existing frames. |
555 | ;; This has to be done when a new face is made. | |
556 | (defun make-face-x-resource-internal (face &optional frame set-anyway) | |
557 | (cond ((null frame) | |
558 | (let ((frames (frame-list))) | |
559 | (while frames | |
b86b9918 | 560 | (if (memq (framep (car frames)) '(x w32)) |
586ab698 RS |
561 | (make-face-x-resource-internal (face-name face) |
562 | (car frames) set-anyway)) | |
465fceed ER |
563 | (setq frames (cdr frames))))) |
564 | (t | |
565 | (setq face (internal-get-face (face-name face) frame)) | |
566 | ;; | |
567 | ;; These are things like "attributeForeground" instead of simply | |
568 | ;; "foreground" because people tend to do things like "*foreground", | |
569 | ;; which would cause all faces to be fully qualified, making faces | |
570 | ;; inherit attributes in a non-useful way. So we've made them slightly | |
571 | ;; less obvious to specify in order to make them work correctly in | |
572 | ;; more random environments. | |
573 | ;; | |
574 | ;; I think these should be called "face.faceForeground" instead of | |
575 | ;; "face.attributeForeground", but they're the way they are for | |
576 | ;; hysterical reasons. | |
577 | ;; | |
578 | (let* ((name (symbol-name (face-name face))) | |
579 | (fn (or (x-get-resource (concat name ".attributeFont") | |
580 | "Face.AttributeFont") | |
581 | (and set-anyway (face-font face)))) | |
582 | (fg (or (x-get-resource (concat name ".attributeForeground") | |
583 | "Face.AttributeForeground") | |
584 | (and set-anyway (face-foreground face)))) | |
585 | (bg (or (x-get-resource (concat name ".attributeBackground") | |
586 | "Face.AttributeBackground") | |
587 | (and set-anyway (face-background face)))) | |
72418504 RS |
588 | (bgp (or (x-get-resource (concat name ".attributeStipple") |
589 | "Face.AttributeStipple") | |
590 | (x-get-resource (concat name ".attributeBackgroundPixmap") | |
591 | "Face.AttributeBackgroundPixmap") | |
592 | (and set-anyway (face-stipple face)))) | |
69718e9d RS |
593 | (ulp (let ((resource (x-get-resource |
594 | (concat name ".attributeUnderline") | |
595 | "Face.AttributeUnderline"))) | |
596 | (if resource | |
597 | (member (downcase resource) '("on" "true")) | |
598 | (and set-anyway (face-underline-p face))))) | |
465fceed ER |
599 | ) |
600 | (if fn | |
601 | (condition-case () | |
19deb21e RS |
602 | (cond ((string= fn "italic") |
603 | (make-face-italic face)) | |
604 | ((string= fn "bold") | |
605 | (make-face-bold face)) | |
606 | ((string= fn "bold-italic") | |
607 | (make-face-bold-italic face)) | |
608 | (t | |
609 | (set-face-font face fn frame))) | |
610 | (error | |
611 | (if (member fn '("italic" "bold" "bold-italic")) | |
612 | (message "no %s version found for face `%s'" fn name) | |
613 | (message "font `%s' not found for face `%s'" fn name))))) | |
465fceed ER |
614 | (if fg |
615 | (condition-case () | |
616 | (set-face-foreground face fg frame) | |
617 | (error (message "color `%s' not allocated for face `%s'" fg name)))) | |
618 | (if bg | |
619 | (condition-case () | |
620 | (set-face-background face bg frame) | |
621 | (error (message "color `%s' not allocated for face `%s'" bg name)))) | |
72418504 RS |
622 | (if bgp |
623 | (condition-case () | |
624 | (set-face-stipple face bgp frame) | |
625 | (error (message "pixmap `%s' not found for face `%s'" bgp name)))) | |
465fceed ER |
626 | (if (or ulp set-anyway) |
627 | (set-face-underline-p face ulp frame)) | |
628 | ))) | |
629 | face) | |
630 | ||
2f2ddd1e RS |
631 | (defun copy-face (old-face new-face &optional frame new-frame) |
632 | "Define a face just like OLD-FACE, with name NEW-FACE. | |
633 | If NEW-FACE already exists as a face, it is modified to be like OLD-FACE. | |
634 | If it doesn't already exist, it is created. | |
635 | ||
636 | If the optional argument FRAME is given as a frame, | |
637 | NEW-FACE is changed on FRAME only. | |
638 | If FRAME is t, the frame-independent default specification for OLD-FACE | |
639 | is copied to NEW-FACE. | |
640 | If FRAME is nil, copying is done for the frame-independent defaults | |
641 | and for each existing frame. | |
710e7005 RS |
642 | If the optional fourth argument NEW-FRAME is given, |
643 | copy the information from face OLD-FACE on frame FRAME | |
2f2ddd1e | 644 | to NEW-FACE on frame NEW-FRAME." |
710e7005 | 645 | (or new-frame (setq new-frame frame)) |
da41135a | 646 | (let ((inhibit-quit t)) |
465fceed ER |
647 | (if (null frame) |
648 | (let ((frames (frame-list))) | |
649 | (while frames | |
2f2ddd1e | 650 | (copy-face old-face new-face (car frames)) |
465fceed | 651 | (setq frames (cdr frames))) |
2f2ddd1e | 652 | (copy-face old-face new-face t)) |
da41135a KH |
653 | (setq old-face (internal-get-face old-face frame)) |
654 | (setq new-face (or (internal-find-face new-face new-frame) | |
655 | (make-face new-face))) | |
52267b56 RS |
656 | (condition-case nil |
657 | ;; A face that has a global symbolic font modifier such as `bold' | |
658 | ;; might legitimately get an error here. | |
659 | ;; Use the frame's default font in that case. | |
660 | (set-face-font new-face (face-font old-face frame) new-frame) | |
661 | (error | |
662 | (set-face-font new-face nil new-frame))) | |
87b6ab4c RS |
663 | (set-face-font-explicit new-face (face-font-explicit old-face frame) |
664 | new-frame) | |
710e7005 RS |
665 | (set-face-foreground new-face (face-foreground old-face frame) new-frame) |
666 | (set-face-background new-face (face-background old-face frame) new-frame) | |
72418504 RS |
667 | (set-face-stipple new-face |
668 | (face-stipple old-face frame) | |
669 | new-frame) | |
465fceed | 670 | (set-face-underline-p new-face (face-underline-p old-face frame) |
710e7005 | 671 | new-frame)) |
465fceed ER |
672 | new-face)) |
673 | ||
674 | (defun face-equal (face1 face2 &optional frame) | |
ade516a1 | 675 | "True if the faces FACE1 and FACE2 display in the same way." |
465fceed ER |
676 | (setq face1 (internal-get-face face1 frame) |
677 | face2 (internal-get-face face2 frame)) | |
678 | (and (equal (face-foreground face1 frame) (face-foreground face2 frame)) | |
679 | (equal (face-background face1 frame) (face-background face2 frame)) | |
680 | (equal (face-font face1 frame) (face-font face2 frame)) | |
ae0249df | 681 | (eq (face-underline-p face1 frame) (face-underline-p face2 frame)) |
72418504 RS |
682 | (equal (face-stipple face1 frame) |
683 | (face-stipple face2 frame)))) | |
465fceed ER |
684 | |
685 | (defun face-differs-from-default-p (face &optional frame) | |
686 | "True if face FACE displays differently from the default face, on FRAME. | |
687 | A face is considered to be ``the same'' as the default face if it is | |
688 | actually specified in the same way (equivalent fonts, etc) or if it is | |
689 | fully unspecified, and thus inherits the attributes of any face it | |
93d6fcef RS |
690 | is displayed on top of. |
691 | ||
692 | The optional argument FRAME specifies which frame to test; | |
693 | if FRAME is t, test the default for new frames. | |
694 | If FRAME is nil or omitted, test the selected frame." | |
465fceed ER |
695 | (let ((default (internal-get-face 'default frame))) |
696 | (setq face (internal-get-face face frame)) | |
697 | (not (and (or (equal (face-foreground default frame) | |
698 | (face-foreground face frame)) | |
699 | (null (face-foreground face frame))) | |
700 | (or (equal (face-background default frame) | |
701 | (face-background face frame)) | |
702 | (null (face-background face frame))) | |
fbd44116 SM |
703 | (or (null (face-font face frame)) |
704 | (equal (face-font face frame) | |
705 | (or (face-font default frame) | |
706 | (downcase | |
707 | (cdr (assq 'font (frame-parameters frame))))))) | |
72418504 RS |
708 | (or (equal (face-stipple default frame) |
709 | (face-stipple face frame)) | |
710 | (null (face-stipple face frame))) | |
465fceed ER |
711 | (equal (face-underline-p default frame) |
712 | (face-underline-p face frame)) | |
713 | )))) | |
714 | ||
93d6fcef RS |
715 | (defun face-nontrivial-p (face &optional frame) |
716 | "True if face FACE has some non-nil attribute. | |
717 | The optional argument FRAME specifies which frame to test; | |
718 | if FRAME is t, test the default for new frames. | |
719 | If FRAME is nil or omitted, test the selected frame." | |
720 | (setq face (internal-get-face face frame)) | |
721 | (or (face-foreground face frame) | |
722 | (face-background face frame) | |
723 | (face-font face frame) | |
724 | (face-stipple face frame) | |
725 | (face-underline-p face frame))) | |
726 | ||
465fceed ER |
727 | |
728 | (defun invert-face (face &optional frame) | |
729 | "Swap the foreground and background colors of face FACE. | |
730 | If the face doesn't specify both foreground and background, then | |
8494bbf1 | 731 | set its foreground and background to the default background and foreground." |
465fceed ER |
732 | (interactive (list (read-face-name "Invert face: "))) |
733 | (setq face (internal-get-face face frame)) | |
734 | (let ((fg (face-foreground face frame)) | |
735 | (bg (face-background face frame))) | |
736 | (if (or fg bg) | |
737 | (progn | |
738 | (set-face-foreground face bg frame) | |
739 | (set-face-background face fg frame)) | |
c5f0b2d4 RS |
740 | (let* ((frame-bg (cdr (assq 'background-color (frame-parameters frame)))) |
741 | (default-bg (or (face-background 'default frame) | |
742 | frame-bg)) | |
743 | (frame-fg (cdr (assq 'foreground-color (frame-parameters frame)))) | |
744 | (default-fg (or (face-foreground 'default frame) | |
745 | frame-fg))) | |
746 | (set-face-foreground face default-bg frame) | |
747 | (set-face-background face default-fg frame)))) | |
465fceed ER |
748 | face) |
749 | ||
750 | ||
751 | (defun internal-try-face-font (face font &optional frame) | |
752 | "Like set-face-font, but returns nil on failure instead of an error." | |
753 | (condition-case () | |
87b6ab4c | 754 | (set-face-font-auto face font frame) |
465fceed | 755 | (error nil))) |
465fceed ER |
756 | \f |
757 | ;; Manipulating font names. | |
758 | ||
d7fa5aa2 RS |
759 | (defvar x-font-regexp nil) |
760 | (defvar x-font-regexp-head nil) | |
761 | (defvar x-font-regexp-weight nil) | |
762 | (defvar x-font-regexp-slant nil) | |
465fceed | 763 | |
021ca129 KH |
764 | (defconst x-font-regexp-weight-subnum 1) |
765 | (defconst x-font-regexp-slant-subnum 2) | |
766 | (defconst x-font-regexp-swidth-subnum 3) | |
767 | (defconst x-font-regexp-adstyle-subnum 4) | |
768 | ||
465fceed ER |
769 | ;;; Regexps matching font names in "Host Portable Character Representation." |
770 | ;;; | |
771 | (let ((- "[-?]") | |
772 | (foundry "[^-]+") | |
773 | (family "[^-]+") | |
774 | (weight "\\(bold\\|demibold\\|medium\\)") ; 1 | |
775 | ; (weight\? "\\(\\*\\|bold\\|demibold\\|medium\\|\\)") ; 1 | |
776 | (weight\? "\\([^-]*\\)") ; 1 | |
777 | (slant "\\([ior]\\)") ; 2 | |
778 | ; (slant\? "\\([ior?*]?\\)") ; 2 | |
779 | (slant\? "\\([^-]?\\)") ; 2 | |
780 | ; (swidth "\\(\\*\\|normal\\|semicondensed\\|\\)") ; 3 | |
781 | (swidth "\\([^-]*\\)") ; 3 | |
782 | ; (adstyle "\\(\\*\\|sans\\|\\)") ; 4 | |
c8787b81 | 783 | (adstyle "\\([^-]*\\)") ; 4 |
465fceed ER |
784 | (pixelsize "[0-9]+") |
785 | (pointsize "[0-9][0-9]+") | |
786 | (resx "[0-9][0-9]+") | |
787 | (resy "[0-9][0-9]+") | |
788 | (spacing "[cmp?*]") | |
789 | (avgwidth "[0-9]+") | |
790 | (registry "[^-]+") | |
791 | (encoding "[^-]+") | |
792 | ) | |
793 | (setq x-font-regexp | |
794 | (concat "\\`\\*?[-?*]" | |
795 | foundry - family - weight\? - slant\? - swidth - adstyle - | |
e20d641f RS |
796 | pixelsize - pointsize - resx - resy - spacing - avgwidth - |
797 | registry - encoding "\\*?\\'" | |
465fceed ER |
798 | )) |
799 | (setq x-font-regexp-head | |
800 | (concat "\\`[-?*]" foundry - family - weight\? - slant\? | |
801 | "\\([-*?]\\|\\'\\)")) | |
802 | (setq x-font-regexp-slant (concat - slant -)) | |
803 | (setq x-font-regexp-weight (concat - weight -)) | |
804 | nil) | |
805 | ||
281dc1c2 JB |
806 | (defun x-resolve-font-name (pattern &optional face frame) |
807 | "Return a font name matching PATTERN. | |
808 | All wildcards in PATTERN become substantiated. | |
10d89673 JB |
809 | If PATTERN is nil, return the name of the frame's base font, which never |
810 | contains wildcards. | |
e17dbede RS |
811 | Given optional arguments FACE and FRAME, return a font which is |
812 | also the same size as FACE on FRAME, or fail." | |
14e6867c RS |
813 | (or (symbolp face) |
814 | (setq face (face-name face))) | |
815 | (and (eq frame t) | |
816 | (setq frame nil)) | |
10d89673 | 817 | (if pattern |
8db93e45 | 818 | ;; Note that x-list-fonts has code to handle a face with nil as its font. |
abd89b80 | 819 | (let ((fonts (x-list-fonts pattern face frame 1))) |
10d89673 JB |
820 | (or fonts |
821 | (if face | |
962a60aa RS |
822 | (if (string-match "\\*" pattern) |
823 | (if (null (face-font face)) | |
824 | (error "No matching fonts are the same height as the frame default font") | |
825 | (error "No matching fonts are the same height as face `%s'" face)) | |
826 | (if (null (face-font face)) | |
827 | (error "Height of font `%s' doesn't match the frame default font" | |
828 | pattern) | |
829 | (error "Height of font `%s' doesn't match face `%s'" | |
830 | pattern face))) | |
7cf6fac1 | 831 | (error "No fonts match `%s'" pattern))) |
10d89673 JB |
832 | (car fonts)) |
833 | (cdr (assq 'font (frame-parameters (selected-frame)))))) | |
281dc1c2 | 834 | |
465fceed | 835 | (defun x-frob-font-weight (font which) |
b7ffee5f SM |
836 | (let ((case-fold-search t)) |
837 | (cond ((string-match x-font-regexp font) | |
838 | (concat (substring font 0 | |
839 | (match-beginning x-font-regexp-weight-subnum)) | |
840 | which | |
841 | (substring font (match-end x-font-regexp-weight-subnum) | |
842 | (match-beginning x-font-regexp-adstyle-subnum)) | |
843 | ;; Replace the ADD_STYLE_NAME field with * | |
844 | ;; because the info in it may not be the same | |
845 | ;; for related fonts. | |
846 | "*" | |
847 | (substring font (match-end x-font-regexp-adstyle-subnum)))) | |
528fbf51 RS |
848 | ((string-match x-font-regexp-head font) |
849 | (concat (substring font 0 (match-beginning 1)) which | |
850 | (substring font (match-end 1)))) | |
851 | ((string-match x-font-regexp-weight font) | |
b7ffee5f SM |
852 | (concat (substring font 0 (match-beginning 1)) which |
853 | (substring font (match-end 1))))))) | |
465fceed ER |
854 | |
855 | (defun x-frob-font-slant (font which) | |
b7ffee5f SM |
856 | (let ((case-fold-search t)) |
857 | (cond ((string-match x-font-regexp font) | |
858 | (concat (substring font 0 | |
859 | (match-beginning x-font-regexp-slant-subnum)) | |
860 | which | |
861 | (substring font (match-end x-font-regexp-slant-subnum) | |
862 | (match-beginning x-font-regexp-adstyle-subnum)) | |
863 | ;; Replace the ADD_STYLE_NAME field with * | |
864 | ;; because the info in it may not be the same | |
865 | ;; for related fonts. | |
866 | "*" | |
867 | (substring font (match-end x-font-regexp-adstyle-subnum)))) | |
528fbf51 RS |
868 | ((string-match x-font-regexp-head font) |
869 | (concat (substring font 0 (match-beginning 2)) which | |
870 | (substring font (match-end 2)))) | |
871 | ((string-match x-font-regexp-slant font) | |
b7ffee5f SM |
872 | (concat (substring font 0 (match-beginning 1)) which |
873 | (substring font (match-end 1))))))) | |
465fceed ER |
874 | |
875 | (defun x-make-font-bold (font) | |
f3f31ccf RS |
876 | "Given an X font specification, make a bold version of it. |
877 | If that can't be done, return nil." | |
465fceed ER |
878 | (x-frob-font-weight font "bold")) |
879 | ||
880 | (defun x-make-font-demibold (font) | |
f3f31ccf RS |
881 | "Given an X font specification, make a demibold version of it. |
882 | If that can't be done, return nil." | |
465fceed ER |
883 | (x-frob-font-weight font "demibold")) |
884 | ||
885 | (defun x-make-font-unbold (font) | |
f3f31ccf RS |
886 | "Given an X font specification, make a non-bold version of it. |
887 | If that can't be done, return nil." | |
465fceed ER |
888 | (x-frob-font-weight font "medium")) |
889 | ||
890 | (defun x-make-font-italic (font) | |
f3f31ccf RS |
891 | "Given an X font specification, make an italic version of it. |
892 | If that can't be done, return nil." | |
465fceed ER |
893 | (x-frob-font-slant font "i")) |
894 | ||
895 | (defun x-make-font-oblique (font) ; you say tomayto... | |
f3f31ccf RS |
896 | "Given an X font specification, make an oblique version of it. |
897 | If that can't be done, return nil." | |
465fceed ER |
898 | (x-frob-font-slant font "o")) |
899 | ||
900 | (defun x-make-font-unitalic (font) | |
f3f31ccf RS |
901 | "Given an X font specification, make a non-italic version of it. |
902 | If that can't be done, return nil." | |
465fceed | 903 | (x-frob-font-slant font "r")) |
b7d7285c KH |
904 | |
905 | (defun x-make-font-bold-italic (font) | |
906 | "Given an X font specification, make a bold and italic version of it. | |
907 | If that can't be done, return nil." | |
908 | (and (setq font (x-make-font-bold font)) | |
909 | (x-make-font-italic font))) | |
465fceed ER |
910 | \f |
911 | ;;; non-X-specific interface | |
912 | ||
bb9a81fc | 913 | (defun make-face-bold (face &optional frame noerror) |
465fceed | 914 | "Make the font of the given face be bold, if possible. |
bb9a81fc | 915 | If NOERROR is non-nil, return nil on failure." |
465fceed | 916 | (interactive (list (read-face-name "Make which face bold: "))) |
1bcb155c RS |
917 | ;; Set the bold-p flag, first of all. |
918 | (internal-set-face-1 face nil t 10 frame) | |
534dbc97 | 919 | (if (and (eq frame t) (listp (face-font face t))) |
f3f31ccf RS |
920 | (set-face-font face (if (memq 'italic (face-font face t)) |
921 | '(bold italic) '(bold)) | |
922 | t) | |
488bedff | 923 | (let (font) |
f3f31ccf RS |
924 | (if (null frame) |
925 | (let ((frames (frame-list))) | |
926 | ;; Make this face bold in global-face-data. | |
927 | (make-face-bold face t noerror) | |
928 | ;; Make this face bold in each frame. | |
929 | (while frames | |
930 | (make-face-bold face (car frames) noerror) | |
931 | (setq frames (cdr frames)))) | |
932 | (setq face (internal-get-face face frame)) | |
933 | (setq font (or (face-font face frame) | |
934 | (face-font face t))) | |
935 | (if (listp font) | |
936 | (setq font nil)) | |
937 | (setq font (or font | |
938 | (face-font 'default frame) | |
939 | (cdr (assq 'font (frame-parameters frame))))) | |
488bedff RS |
940 | (or (and font (make-face-bold-internal face frame font)) |
941 | ;; We failed to find a bold version of the font. | |
942 | noerror | |
943 | (error "No bold version of %S" font)))))) | |
f3f31ccf | 944 | |
4b3203d9 RS |
945 | (defun make-face-bold-internal (face frame font) |
946 | (let (f2) | |
947 | (or (and (setq f2 (x-make-font-bold font)) | |
948 | (internal-try-face-font face f2 frame)) | |
949 | (and (setq f2 (x-make-font-demibold font)) | |
950 | (internal-try-face-font face f2 frame))))) | |
bb9a81fc JB |
951 | |
952 | (defun make-face-italic (face &optional frame noerror) | |
465fceed | 953 | "Make the font of the given face be italic, if possible. |
bb9a81fc | 954 | If NOERROR is non-nil, return nil on failure." |
465fceed | 955 | (interactive (list (read-face-name "Make which face italic: "))) |
1bcb155c RS |
956 | ;; Set the italic-p flag, first of all. |
957 | (internal-set-face-1 face nil t 11 frame) | |
534dbc97 | 958 | (if (and (eq frame t) (listp (face-font face t))) |
f3f31ccf RS |
959 | (set-face-font face (if (memq 'bold (face-font face t)) |
960 | '(bold italic) '(italic)) | |
961 | t) | |
488bedff | 962 | (let (font) |
f3f31ccf RS |
963 | (if (null frame) |
964 | (let ((frames (frame-list))) | |
965 | ;; Make this face italic in global-face-data. | |
966 | (make-face-italic face t noerror) | |
967 | ;; Make this face italic in each frame. | |
968 | (while frames | |
969 | (make-face-italic face (car frames) noerror) | |
970 | (setq frames (cdr frames)))) | |
971 | (setq face (internal-get-face face frame)) | |
972 | (setq font (or (face-font face frame) | |
973 | (face-font face t))) | |
974 | (if (listp font) | |
975 | (setq font nil)) | |
976 | (setq font (or font | |
977 | (face-font 'default frame) | |
978 | (cdr (assq 'font (frame-parameters frame))))) | |
488bedff RS |
979 | (or (and font (make-face-italic-internal face frame font)) |
980 | ;; We failed to find an italic version of the font. | |
981 | noerror | |
982 | (error "No italic version of %S" font)))))) | |
f3f31ccf | 983 | |
4b3203d9 RS |
984 | (defun make-face-italic-internal (face frame font) |
985 | (let (f2) | |
986 | (or (and (setq f2 (x-make-font-italic font)) | |
987 | (internal-try-face-font face f2 frame)) | |
988 | (and (setq f2 (x-make-font-oblique font)) | |
989 | (internal-try-face-font face f2 frame))))) | |
bb9a81fc JB |
990 | |
991 | (defun make-face-bold-italic (face &optional frame noerror) | |
465fceed | 992 | "Make the font of the given face be bold and italic, if possible. |
bb9a81fc | 993 | If NOERROR is non-nil, return nil on failure." |
465fceed | 994 | (interactive (list (read-face-name "Make which face bold-italic: "))) |
1bcb155c RS |
995 | ;; Set the bold-p and italic-p flags, first of all. |
996 | (internal-set-face-1 face nil t 10 frame) | |
997 | (internal-set-face-1 face nil t 11 frame) | |
534dbc97 | 998 | (if (and (eq frame t) (listp (face-font face t))) |
f3f31ccf | 999 | (set-face-font face '(bold italic) t) |
488bedff | 1000 | (let (font) |
f3f31ccf RS |
1001 | (if (null frame) |
1002 | (let ((frames (frame-list))) | |
1003 | ;; Make this face bold-italic in global-face-data. | |
1004 | (make-face-bold-italic face t noerror) | |
1005 | ;; Make this face bold in each frame. | |
1006 | (while frames | |
1007 | (make-face-bold-italic face (car frames) noerror) | |
1008 | (setq frames (cdr frames)))) | |
1009 | (setq face (internal-get-face face frame)) | |
1010 | (setq font (or (face-font face frame) | |
1011 | (face-font face t))) | |
1012 | (if (listp font) | |
1013 | (setq font nil)) | |
1014 | (setq font (or font | |
1015 | (face-font 'default frame) | |
1016 | (cdr (assq 'font (frame-parameters frame))))) | |
488bedff RS |
1017 | (or (and font (make-face-bold-italic-internal face frame font)) |
1018 | ;; We failed to find a bold italic version. | |
1019 | noerror | |
1020 | (error "No bold italic version of %S" font)))))) | |
f3f31ccf | 1021 | |
4b3203d9 | 1022 | (defun make-face-bold-italic-internal (face frame font) |
f3f31ccf RS |
1023 | (let (f2 f3) |
1024 | (or (and (setq f2 (x-make-font-italic font)) | |
1025 | (not (equal font f2)) | |
1026 | (setq f3 (x-make-font-bold f2)) | |
1027 | (not (equal f2 f3)) | |
1028 | (internal-try-face-font face f3 frame)) | |
1029 | (and (setq f2 (x-make-font-oblique font)) | |
1030 | (not (equal font f2)) | |
1031 | (setq f3 (x-make-font-bold f2)) | |
1032 | (not (equal f2 f3)) | |
1033 | (internal-try-face-font face f3 frame)) | |
1034 | (and (setq f2 (x-make-font-italic font)) | |
1035 | (not (equal font f2)) | |
1036 | (setq f3 (x-make-font-demibold f2)) | |
1037 | (not (equal f2 f3)) | |
1038 | (internal-try-face-font face f3 frame)) | |
1039 | (and (setq f2 (x-make-font-oblique font)) | |
1040 | (not (equal font f2)) | |
1041 | (setq f3 (x-make-font-demibold f2)) | |
1042 | (not (equal f2 f3)) | |
1043 | (internal-try-face-font face f3 frame))))) | |
bb9a81fc JB |
1044 | |
1045 | (defun make-face-unbold (face &optional frame noerror) | |
465fceed | 1046 | "Make the font of the given face be non-bold, if possible. |
bb9a81fc | 1047 | If NOERROR is non-nil, return nil on failure." |
465fceed | 1048 | (interactive (list (read-face-name "Make which face non-bold: "))) |
1bcb155c RS |
1049 | ;; Clear the bold-p flag, first of all. |
1050 | (internal-set-face-1 face nil nil 10 frame) | |
534dbc97 | 1051 | (if (and (eq frame t) (listp (face-font face t))) |
f3f31ccf RS |
1052 | (set-face-font face (if (memq 'italic (face-font face t)) |
1053 | '(italic) nil) | |
1054 | t) | |
488bedff | 1055 | (let (font font1) |
f3f31ccf RS |
1056 | (if (null frame) |
1057 | (let ((frames (frame-list))) | |
1058 | ;; Make this face unbold in global-face-data. | |
1059 | (make-face-unbold face t noerror) | |
1060 | ;; Make this face unbold in each frame. | |
1061 | (while frames | |
1062 | (make-face-unbold face (car frames) noerror) | |
1063 | (setq frames (cdr frames)))) | |
1064 | (setq face (internal-get-face face frame)) | |
1065 | (setq font1 (or (face-font face frame) | |
1066 | (face-font face t))) | |
1067 | (if (listp font1) | |
1068 | (setq font1 nil)) | |
1069 | (setq font1 (or font1 | |
1070 | (face-font 'default frame) | |
1071 | (cdr (assq 'font (frame-parameters frame))))) | |
96616c04 | 1072 | (setq font (and font1 (x-make-font-unbold font1))) |
488bedff RS |
1073 | (or (if font (internal-try-face-font face font frame)) |
1074 | noerror | |
1075 | (error "No unbold version of %S" font1)))))) | |
bb9a81fc JB |
1076 | |
1077 | (defun make-face-unitalic (face &optional frame noerror) | |
465fceed | 1078 | "Make the font of the given face be non-italic, if possible. |
bb9a81fc | 1079 | If NOERROR is non-nil, return nil on failure." |
465fceed | 1080 | (interactive (list (read-face-name "Make which face non-italic: "))) |
1bcb155c RS |
1081 | ;; Clear the italic-p flag, first of all. |
1082 | (internal-set-face-1 face nil nil 11 frame) | |
534dbc97 | 1083 | (if (and (eq frame t) (listp (face-font face t))) |
f3f31ccf RS |
1084 | (set-face-font face (if (memq 'bold (face-font face t)) |
1085 | '(bold) nil) | |
1086 | t) | |
488bedff | 1087 | (let (font font1) |
f3f31ccf RS |
1088 | (if (null frame) |
1089 | (let ((frames (frame-list))) | |
1090 | ;; Make this face unitalic in global-face-data. | |
1091 | (make-face-unitalic face t noerror) | |
1092 | ;; Make this face unitalic in each frame. | |
1093 | (while frames | |
1094 | (make-face-unitalic face (car frames) noerror) | |
1095 | (setq frames (cdr frames)))) | |
1096 | (setq face (internal-get-face face frame)) | |
1097 | (setq font1 (or (face-font face frame) | |
1098 | (face-font face t))) | |
1099 | (if (listp font1) | |
1100 | (setq font1 nil)) | |
1101 | (setq font1 (or font1 | |
1102 | (face-font 'default frame) | |
1103 | (cdr (assq 'font (frame-parameters frame))))) | |
96616c04 | 1104 | (setq font (and font1 (x-make-font-unitalic font1))) |
488bedff RS |
1105 | (or (if font (internal-try-face-font face font frame)) |
1106 | noerror | |
1107 | (error "No unitalic version of %S" font1)))))) | |
465fceed | 1108 | \f |
710e7005 RS |
1109 | (defvar list-faces-sample-text |
1110 | "abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
1111 | "*Text string to display as the sample text for `list-faces-display'.") | |
1112 | ||
1113 | ;; The name list-faces would be more consistent, but let's avoid a conflict | |
1114 | ;; with Lucid, which uses that name differently. | |
1115 | (defun list-faces-display () | |
1116 | "List all faces, using the same sample text in each. | |
1117 | The sample text is a string that comes from the variable | |
1118 | `list-faces-sample-text'. | |
1119 | ||
1120 | It is possible to give a particular face name different appearances in | |
1121 | different frames. This command shows the appearance in the | |
1122 | selected frame." | |
1123 | (interactive) | |
1124 | (let ((faces (sort (face-list) (function string-lessp))) | |
1125 | (face nil) | |
1126 | (frame (selected-frame)) | |
6fe3eb77 RS |
1127 | disp-frame window |
1128 | (face-name-max-length | |
1129 | (car (sort (mapcar (function string-width) | |
1130 | (mapcar (function symbol-name) (face-list))) | |
1131 | (function >))))) | |
710e7005 RS |
1132 | (with-output-to-temp-buffer "*Faces*" |
1133 | (save-excursion | |
1134 | (set-buffer standard-output) | |
1135 | (setq truncate-lines t) | |
1136 | (while faces | |
1137 | (setq face (car faces)) | |
1138 | (setq faces (cdr faces)) | |
6fe3eb77 RS |
1139 | (insert (format |
1140 | (format "%%-%ds " | |
1141 | face-name-max-length) | |
1142 | (symbol-name face))) | |
710e7005 RS |
1143 | (let ((beg (point))) |
1144 | (insert list-faces-sample-text) | |
1145 | (insert "\n") | |
23b04eac RS |
1146 | (put-text-property beg (1- (point)) 'face face) |
1147 | ;; If the sample text has multiple lines, line up all of them. | |
1148 | (goto-char beg) | |
1149 | (forward-line 1) | |
1150 | (while (not (eobp)) | |
ed584298 | 1151 | (insert-char ? (1+ face-name-max-length)) |
23b04eac | 1152 | (forward-line 1)))) |
b7ad053c RS |
1153 | (goto-char (point-min))) |
1154 | (print-help-return-message)) | |
710e7005 RS |
1155 | ;; If the *Faces* buffer appears in a different frame, |
1156 | ;; copy all the face definitions from FRAME, | |
1157 | ;; so that the display will reflect the frame that was selected. | |
1158 | (setq window (get-buffer-window (get-buffer "*Faces*") t)) | |
1159 | (setq disp-frame (if window (window-frame window) | |
1160 | (car (frame-list)))) | |
1161 | (or (eq frame disp-frame) | |
1162 | (let ((faces (face-list))) | |
1163 | (while faces | |
1164 | (copy-face (car faces) (car faces) frame disp-frame) | |
1165 | (setq faces (cdr faces))))))) | |
19deb21e RS |
1166 | |
1167 | (defun describe-face (face) | |
1168 | "Display the properties of face FACE." | |
1169 | (interactive (list (read-face-name "Describe face: "))) | |
1170 | (with-output-to-temp-buffer "*Help*" | |
1171 | (princ "Properties of face `") | |
1172 | (princ (face-name face)) | |
1173 | (princ "':") (terpri) | |
1174 | (princ "Foreground: ") (princ (face-foreground face)) (terpri) | |
1175 | (princ "Background: ") (princ (face-background face)) (terpri) | |
1176 | (princ " Font: ") (princ (face-font face)) (terpri) | |
1177 | (princ "Underlined: ") (princ (if (face-underline-p face) "yes" "no")) (terpri) | |
64b26d5c RS |
1178 | (princ " Stipple: ") (princ (or (face-stipple face) "none")) (terpri) |
1179 | (terpri) | |
1180 | (princ "Documentation:") (terpri) | |
02e0989b | 1181 | (let ((doc (face-documentation face))) |
64b26d5c RS |
1182 | (if doc |
1183 | (princ doc) | |
b7ad053c RS |
1184 | (princ "not documented as a face."))) |
1185 | (print-help-return-message))) | |
710e7005 | 1186 | \f |
d11fba25 RS |
1187 | ;;; Setting a face based on a SPEC. |
1188 | ||
95c7d3d3 RS |
1189 | (defun face-attr-match-p (face attrs &optional frame) |
1190 | (or frame (setq frame (selected-frame))) | |
1191 | (and (face-attr-match-1 face frame attrs ':inverse-video | |
1192 | 'face-inverse-video-p) | |
1193 | (if (face-inverse-video-p face frame) | |
1194 | (and | |
1195 | (face-attr-match-1 face frame attrs | |
1196 | ':foreground 'face-background | |
1197 | (cdr (assq 'foreground-color | |
1198 | (frame-parameters frame)))) | |
1199 | (face-attr-match-1 face frame attrs | |
1200 | ':background 'face-foreground | |
1201 | (cdr (assq 'background-color | |
1202 | (frame-parameters frame))))) | |
1203 | (and | |
1204 | (face-attr-match-1 face frame attrs ':foreground 'face-foreground) | |
1205 | (face-attr-match-1 face frame attrs ':background 'face-background))) | |
1206 | (face-attr-match-1 face frame attrs ':stipple 'face-stipple) | |
1207 | (face-attr-match-1 face frame attrs ':bold 'face-bold-p) | |
1208 | (face-attr-match-1 face frame attrs ':italic 'face-italic-p) | |
1209 | (face-attr-match-1 face frame attrs ':underline 'face-underline-p) | |
1210 | )) | |
1211 | ||
1212 | (defun face-attr-match-1 (face frame plist property function | |
1213 | &optional defaultval) | |
1214 | (while (and plist (not (eq (car plist) property))) | |
1215 | (setq plist (cdr (cdr plist)))) | |
1216 | (eq (funcall function face frame) | |
1217 | (if plist | |
1218 | (nth 1 plist) | |
1219 | (or defaultval | |
1220 | (funcall function 'default frame))))) | |
1221 | ||
1222 | (defun face-spec-match-p (face spec &optional frame) | |
1223 | "Return t if FACE, on FRAME, matches what SPEC says it should look like." | |
1224 | (face-attr-match-p face (face-spec-choose spec frame) frame)) | |
1225 | ||
1226 | (defun face-attr-construct (face &optional frame) | |
1227 | "Return a defface-style attribute list for FACE, as it exists on FRAME." | |
1228 | (let (result) | |
1229 | (if (face-inverse-video-p face frame) | |
1230 | (progn | |
1231 | (setq result (cons ':inverse-video (cons t result))) | |
1232 | (or (face-attr-match-1 face frame nil | |
1233 | ':foreground 'face-background | |
1234 | (cdr (assq 'foreground-color | |
1235 | (frame-parameters frame)))) | |
1236 | (setq result (cons ':foreground | |
1237 | (cons (face-foreground face frame) result)))) | |
1238 | (or (face-attr-match-1 face frame nil | |
1239 | ':background 'face-foreground | |
1240 | (cdr (assq 'background-color | |
1241 | (frame-parameters frame)))) | |
1242 | (setq result (cons ':background | |
1243 | (cons (face-background face frame) result))))) | |
1244 | (if (face-foreground face frame) | |
1245 | (setq result (cons ':foreground | |
1246 | (cons (face-foreground face frame) result)))) | |
1247 | (if (face-background face frame) | |
1248 | (setq result (cons ':background | |
1249 | (cons (face-background face frame) result))))) | |
1250 | (if (face-stipple face frame) | |
1251 | (setq result (cons ':stipple | |
1252 | (cons (face-stipple face frame) result)))) | |
1253 | (if (face-bold-p face frame) | |
1254 | (setq result (cons ':bold | |
1255 | (cons (face-bold-p face frame) result)))) | |
1256 | (if (face-italic-p face frame) | |
1257 | (setq result (cons ':italic | |
1258 | (cons (face-italic-p face frame) result)))) | |
1259 | (if (face-underline-p face frame) | |
1260 | (setq result (cons ':underline | |
1261 | (cons (face-underline-p face frame) result)))) | |
1262 | result)) | |
1263 | ||
1264 | ;; Choose the proper attributes for FRAME, out of SPEC. | |
1265 | (defun face-spec-choose (spec &optional frame) | |
1266 | (or frame (setq frame (selected-frame))) | |
1267 | (let ((tail spec) | |
1268 | result) | |
1269 | (while tail | |
d11fba25 RS |
1270 | (let* ((entry (car tail)) |
1271 | (display (nth 0 entry)) | |
1272 | (attrs (nth 1 entry))) | |
1273 | (setq tail (cdr tail)) | |
d11fba25 | 1274 | (when (face-spec-set-match-display display frame) |
95c7d3d3 RS |
1275 | (setq result attrs tail nil)))) |
1276 | result)) | |
1277 | ||
1278 | (defun face-spec-set (face spec &optional frame) | |
1279 | "Set FACE's face attributes according to the first matching entry in SPEC. | |
1280 | If optional FRAME is non-nil, set it for that frame only. | |
1281 | If it is nil, then apply SPEC to each frame individually. | |
1282 | See `defface' for information about SPEC." | |
1283 | (if frame | |
1284 | (let ((attrs (face-spec-choose spec frame))) | |
1285 | (when attrs | |
1286 | ;; If the font was set automatically, clear it out | |
1287 | ;; to allow it to be set it again. | |
1288 | (unless (face-font-explicit face frame) | |
1289 | (set-face-font face nil frame)) | |
d343b6f1 | 1290 | (modify-face face '(nil) '(nil) nil nil nil nil nil frame) |
d11fba25 RS |
1291 | (face-spec-set-1 face frame attrs ':foreground 'set-face-foreground) |
1292 | (face-spec-set-1 face frame attrs ':background 'set-face-background) | |
1293 | (face-spec-set-1 face frame attrs ':stipple 'set-face-stipple) | |
1294 | (face-spec-set-1 face frame attrs ':bold 'set-face-bold-p) | |
1295 | (face-spec-set-1 face frame attrs ':italic 'set-face-italic-p) | |
1296 | (face-spec-set-1 face frame attrs ':underline 'set-face-underline-p) | |
e8e4cda0 | 1297 | (face-spec-set-1 face frame attrs ':inverse-video |
95c7d3d3 RS |
1298 | 'set-face-inverse-video-p))) |
1299 | (let ((frames (frame-list)) | |
1300 | frame) | |
1301 | (while frames | |
1302 | (setq frame (car frames) | |
1303 | frames (cdr frames)) | |
1304 | (face-spec-set face (or (get face 'saved-face) | |
1305 | (get face 'face-defface-spec)) | |
1306 | frame) | |
1307 | (face-spec-set face spec frame))))) | |
d11fba25 RS |
1308 | |
1309 | (defun face-spec-set-1 (face frame plist property function) | |
1310 | (while (and plist (not (eq (car plist) property))) | |
1311 | (setq plist (cdr (cdr plist)))) | |
1312 | (if plist | |
1313 | (funcall function face (nth 1 plist) frame))) | |
1314 | ||
1315 | (defun face-spec-set-match-display (display frame) | |
1316 | "Non-nil iff DISPLAY matches FRAME. | |
1317 | DISPLAY is part of a spec such as can be used in `defface'. | |
1318 | If FRAME is nil, the current FRAME is used." | |
1319 | (let* ((conjuncts display) | |
1320 | conjunct req options | |
1321 | ;; t means we have succeeded against all | |
1322 | ;; the conjunts in DISPLAY that have been tested so far. | |
1323 | (match t)) | |
1324 | (if (eq conjuncts t) | |
1325 | (setq conjuncts nil)) | |
1326 | (while (and conjuncts match) | |
1327 | (setq conjunct (car conjuncts) | |
1328 | conjuncts (cdr conjuncts) | |
1329 | req (car conjunct) | |
1330 | options (cdr conjunct) | |
1331 | match (cond ((eq req 'type) | |
1332 | (memq window-system options)) | |
1333 | ((eq req 'class) | |
1334 | (memq (frame-parameter frame 'display-type) options)) | |
1335 | ((eq req 'background) | |
1336 | (memq (frame-parameter frame 'background-mode) | |
1337 | options)) | |
1338 | (t | |
1339 | (error "Unknown req `%S' with options `%S'" | |
1340 | req options))))) | |
1341 | match)) | |
465fceed | 1342 | \f |
465fceed ER |
1343 | ;; Like x-create-frame but also set up the faces. |
1344 | ||
1345 | (defun x-create-frame-with-faces (&optional parameters) | |
6e4aafdc KH |
1346 | ;; Read this frame's geometry resource, if it has an explicit name, |
1347 | ;; and put the specs into PARAMETERS. | |
1348 | (let* ((name (or (cdr (assq 'name parameters)) | |
313b841c | 1349 | (cdr (assq 'name default-frame-alist)))) |
6e4aafdc | 1350 | (x-resource-name name) |
28ceec49 | 1351 | (res-geometry (if name (x-get-resource "geometry" "Geometry")))) |
6e4aafdc | 1352 | (if res-geometry |
28ceec49 | 1353 | (let ((parsed (x-parse-geometry res-geometry))) |
6e4aafdc KH |
1354 | ;; If the resource specifies a position, |
1355 | ;; call the position and size "user-specified". | |
1356 | (if (or (assq 'top parsed) (assq 'left parsed)) | |
28ceec49 SM |
1357 | (setq parsed (append '((user-position . t) (user-size . t)) |
1358 | parsed))) | |
313b841c KH |
1359 | ;; Put the geometry parameters at the end. |
1360 | ;; Copy default-frame-alist so that they go after it. | |
28ceec49 | 1361 | (setq parameters (append parameters default-frame-alist parsed))))) |
53caf287 KH |
1362 | |
1363 | (if default-enable-multibyte-characters | |
1364 | ;; If an ASCII font is specified in PARAMETERS, we try to create | |
1365 | ;; a fontset from it, and use it for the new frame. | |
1366 | (condition-case nil | |
1367 | (let ((font (cdr (assq 'font parameters)))) | |
1368 | (if (and font | |
1369 | (not (query-fontset font))) | |
1370 | (setq parameters | |
1371 | (cons (cons 'font (create-fontset-from-ascii-font font)) | |
1372 | parameters)))) | |
1373 | (error nil))) | |
1374 | ||
ef436392 KH |
1375 | (let (frame) |
1376 | (if (null global-face-data) | |
d11fba25 RS |
1377 | (progn |
1378 | (setq frame (x-create-frame parameters)) | |
1379 | (frame-set-background-mode frame)) | |
ef436392 | 1380 | (let* ((visibility-spec (assq 'visibility parameters)) |
d11fba25 | 1381 | success faces rest) |
ef436392 KH |
1382 | (setq frame (x-create-frame (cons '(visibility . nil) parameters))) |
1383 | (unwind-protect | |
1384 | (progn | |
d11fba25 RS |
1385 | ;; Copy the face alist, copying the face vectors |
1386 | ;; and emptying out their attributes. | |
1387 | (setq faces | |
1388 | (mapcar '(lambda (elt) | |
1389 | (cons (car elt) | |
1390 | (vector 'face | |
1391 | (face-name (cdr elt)) | |
1392 | (face-id (cdr elt)) | |
1bcb155c RS |
1393 | nil |
1394 | nil nil nil nil | |
1395 | nil nil nil nil))) | |
d11fba25 | 1396 | global-face-data)) |
ef436392 KH |
1397 | (set-frame-face-alist frame faces) |
1398 | ||
d11fba25 RS |
1399 | ;; Handle the reverse-video frame parameter |
1400 | ;; and X resource. x-create-frame does not handle this one. | |
ef436392 KH |
1401 | (if (cdr (or (assq 'reverse parameters) |
1402 | (assq 'reverse default-frame-alist) | |
1403 | (let ((resource (x-get-resource "reverseVideo" | |
1404 | "ReverseVideo"))) | |
1405 | (if resource | |
1406 | (cons nil (member (downcase resource) | |
1407 | '("on" "true"))))))) | |
1408 | (let* ((params (frame-parameters frame)) | |
1409 | (bg (cdr (assq 'foreground-color params))) | |
1410 | (fg (cdr (assq 'background-color params)))) | |
1411 | (modify-frame-parameters frame | |
1412 | (list (cons 'foreground-color fg) | |
1413 | (cons 'background-color bg))) | |
1414 | (if (equal bg (cdr (assq 'border-color params))) | |
1415 | (modify-frame-parameters frame | |
1416 | (list (cons 'border-color fg)))) | |
1417 | (if (equal bg (cdr (assq 'mouse-color params))) | |
1418 | (modify-frame-parameters frame | |
1419 | (list (cons 'mouse-color fg)))) | |
1420 | (if (equal bg (cdr (assq 'cursor-color params))) | |
1421 | (modify-frame-parameters frame | |
1422 | (list (cons 'cursor-color fg)))))) | |
d11fba25 | 1423 | |
f0201260 RS |
1424 | (frame-set-background-mode frame) |
1425 | ||
b1eb5e6a | 1426 | (face-set-after-frame-default frame) |
d11fba25 RS |
1427 | |
1428 | ;; Make the frame visible, if desired. | |
ef436392 KH |
1429 | (if (null visibility-spec) |
1430 | (make-frame-visible frame) | |
1431 | (modify-frame-parameters frame (list visibility-spec))) | |
1432 | (setq success t)) | |
1433 | (or success | |
1434 | (delete-frame frame))))) | |
ef436392 | 1435 | frame)) |
e09c52a8 | 1436 | |
b1eb5e6a RS |
1437 | ;; Update a frame's faces after the frame font changes. |
1438 | ;; This is called from modify-frame-parameters | |
1439 | ;; as well as from elsewhere in this file. | |
1440 | (defun face-set-after-frame-default (frame) | |
1441 | (let ((rest (frame-face-alist frame))) | |
1442 | (while rest | |
1443 | ;; Set up each face, first from the defface information, | |
1444 | ;; then the global face data, and then the X resources. | |
1445 | (let* ((face (car (car rest))) | |
acc9e99d RS |
1446 | (spec (or (get face 'customized-face) |
1447 | (get face 'saved-face) | |
b1eb5e6a RS |
1448 | (get face 'face-defface-spec))) |
1449 | (global (cdr (assq face global-face-data))) | |
1450 | (local (cdr (car rest)))) | |
87b6ab4c | 1451 | (when spec |
b1eb5e6a RS |
1452 | (face-spec-set face spec frame)) |
1453 | (face-fill-in face global frame) | |
1454 | (make-face-x-resource-internal local frame)) | |
1455 | (setq rest (cdr rest))))) | |
1456 | ||
d11fba25 RS |
1457 | (defcustom frame-background-mode nil |
1458 | "*The brightness of the background. | |
1459 | Set this to the symbol dark if your background color is dark, light if | |
1460 | your background is light, or nil (default) if you want Emacs to | |
1461 | examine the brightness for you." | |
1462 | :group 'faces | |
ba7eadda RS |
1463 | :set #'(lambda (var value) |
1464 | (set var value) | |
1465 | (mapcar 'frame-set-background-mode (frame-list))) | |
1466 | :initialize 'custom-initialize-changed | |
d11fba25 RS |
1467 | :type '(choice (choice-item dark) |
1468 | (choice-item light) | |
1469 | (choice-item :tag "default" nil))) | |
1470 | ||
1471 | (defun frame-set-background-mode (frame) | |
1472 | "Set up the `background-mode' and `display-type' frame parameters for FRAME." | |
fabe6087 | 1473 | (unless (eq (framep frame) t) |
49cd149a RS |
1474 | (let ((bg-resource (x-get-resource ".backgroundMode" |
1475 | "BackgroundMode")) | |
1476 | (params (frame-parameters frame)) | |
1477 | (bg-mode)) | |
1478 | (setq bg-mode | |
1479 | (cond (frame-background-mode) | |
1480 | (bg-resource (intern (downcase bg-resource))) | |
1481 | ((< (apply '+ (x-color-values | |
1482 | (cdr (assq 'background-color params)) | |
1483 | frame)) | |
1484 | ;; Just looking at the screen, | |
1485 | ;; colors whose values add up to .6 of the white total | |
1486 | ;; still look dark to me. | |
1487 | (* (apply '+ (x-color-values "white" frame)) .6)) | |
1488 | 'dark) | |
1489 | (t 'light))) | |
1490 | (modify-frame-parameters frame | |
1491 | (list (cons 'background-mode bg-mode) | |
1492 | (cons 'display-type | |
1493 | (cond ((x-display-color-p frame) | |
1494 | 'color) | |
1495 | ((x-display-grayscale-p frame) | |
1496 | 'grayscale) | |
1497 | (t 'mono)))))))) | |
d11fba25 | 1498 | |
9d52c8d3 | 1499 | ;; Update a frame's faces when we change its default font. |
b1eb5e6a | 1500 | (defun frame-update-faces (frame) nil) |
9d52c8d3 | 1501 | |
f16c38ae RS |
1502 | ;; Update the colors of FACE, after FRAME's own colors have been changed. |
1503 | ;; This applies only to faces with global color specifications | |
1504 | ;; that are not simple constants. | |
1505 | (defun frame-update-face-colors (frame) | |
0bea75b9 | 1506 | (frame-set-background-mode frame) |
f16c38ae RS |
1507 | (let ((faces global-face-data)) |
1508 | (while faces | |
1509 | (condition-case nil | |
1510 | (let* ((data (cdr (car faces))) | |
1511 | (face (car (car faces))) | |
1512 | (foreground (face-foreground data)) | |
1513 | (background (face-background data))) | |
1514 | ;; If the global spec is a specific color, | |
1515 | ;; which doesn't depend on the frame's attributes, | |
1516 | ;; we don't need to recalculate it now. | |
1517 | (or (listp foreground) | |
1518 | (setq foreground nil)) | |
1519 | (or (listp background) | |
1520 | (setq background nil)) | |
1521 | ;; If we are going to frob this face at all, | |
1522 | ;; reinitialize it first. | |
1523 | (if (or foreground background) | |
1524 | (progn (set-face-foreground face nil frame) | |
1525 | (set-face-background face nil frame))) | |
1526 | (if foreground | |
1527 | (face-try-color-list 'set-face-foreground | |
1528 | face foreground frame)) | |
1529 | (if background | |
1530 | (face-try-color-list 'set-face-background | |
1531 | face background frame))) | |
1532 | (error nil)) | |
1533 | (setq faces (cdr faces))))) | |
1534 | ||
19ae9866 RS |
1535 | ;; Fill in the face FACE from frame-independent face data DATA. |
1536 | ;; DATA should be the non-frame-specific ("global") face vector | |
1537 | ;; for the face. FACE should be a face name or face object. | |
1538 | ;; FRAME is the frame to act on; it must be an actual frame, not nil or t. | |
1539 | (defun face-fill-in (face data frame) | |
1540 | (condition-case nil | |
1541 | (let ((foreground (face-foreground data)) | |
1542 | (background (face-background data)) | |
ca58b3ec KH |
1543 | (font (face-font data)) |
1544 | (stipple (face-stipple data))) | |
e8e4cda0 RS |
1545 | (if (face-underline-p data) |
1546 | (set-face-underline-p face (face-underline-p data) frame)) | |
19ae9866 RS |
1547 | (if foreground |
1548 | (face-try-color-list 'set-face-foreground | |
1549 | face foreground frame)) | |
1550 | (if background | |
1551 | (face-try-color-list 'set-face-background | |
1552 | face background frame)) | |
1553 | (if (listp font) | |
1554 | (let ((bold (memq 'bold font)) | |
1555 | (italic (memq 'italic font))) | |
1556 | (cond ((and bold italic) | |
1557 | (make-face-bold-italic face frame)) | |
1558 | (bold | |
1559 | (make-face-bold face frame)) | |
1560 | (italic | |
1561 | (make-face-italic face frame)))) | |
1562 | (if font | |
ca58b3ec KH |
1563 | (set-face-font face font frame))) |
1564 | (if stipple | |
1565 | (set-face-stipple face stipple frame))) | |
19ae9866 | 1566 | (error nil))) |
e09c52a8 | 1567 | |
4099a32d RS |
1568 | ;; Assuming COLOR is a valid color name, |
1569 | ;; return t if it can be displayed on FRAME. | |
1570 | (defun face-color-supported-p (frame color background-p) | |
055ff5a6 RS |
1571 | (and window-system |
1572 | (or (x-display-color-p frame) | |
1573 | ;; A black-and-white display can implement these. | |
1574 | (member color '("black" "white")) | |
1575 | ;; A black-and-white display can fake gray for background. | |
1576 | (and background-p | |
1577 | (face-color-gray-p color frame)) | |
1578 | ;; A grayscale display can implement colors that are gray (more or less). | |
1579 | (and (x-display-grayscale-p frame) | |
1580 | (face-color-gray-p color frame))))) | |
4099a32d | 1581 | |
19ae9866 RS |
1582 | ;; Use FUNCTION to store a color in FACE on FRAME. |
1583 | ;; COLORS is either a single color or a list of colors. | |
1584 | ;; If it is a list, try the colors one by one until one of them | |
1585 | ;; succeeds. We signal an error only if all the colors failed. | |
1586 | ;; t as COLORS or as an element of COLORS means to invert the face. | |
1587 | ;; That can't fail, so any subsequent elements after the t are ignored. | |
1588 | (defun face-try-color-list (function face colors frame) | |
1589 | (if (stringp colors) | |
4099a32d RS |
1590 | (if (face-color-supported-p frame colors |
1591 | (eq function 'set-face-background)) | |
1592 | (funcall function face colors frame)) | |
19ae9866 | 1593 | (if (eq colors t) |
e8e4cda0 | 1594 | (set-face-inverse-video-p face t frame) |
19ae9866 RS |
1595 | (let (done) |
1596 | (while (and colors (not done)) | |
077fcb28 | 1597 | (if (or (memq (car colors) '(t underline nil)) |
4099a32d RS |
1598 | (face-color-supported-p frame (car colors) |
1599 | (eq function 'set-face-background))) | |
1600 | (if (cdr colors) | |
1601 | ;; If there are more colors to try, catch errors | |
1602 | ;; and set `done' if we succeed. | |
1603 | (condition-case nil | |
1604 | (progn | |
1605 | (cond ((eq (car colors) t) | |
e8e4cda0 | 1606 | (set-face-inverse-video-p face t frame)) |
4099a32d RS |
1607 | ((eq (car colors) 'underline) |
1608 | (set-face-underline-p face t frame)) | |
1609 | (t | |
1610 | (funcall function face (car colors) frame))) | |
1611 | (setq done t)) | |
1612 | (error nil)) | |
1613 | ;; If this is the last color, let the error get out if it fails. | |
1614 | ;; If it succeeds, we will exit anyway after this iteration. | |
1615 | (cond ((eq (car colors) t) | |
e8e4cda0 | 1616 | (set-face-inverse-video-p face t frame)) |
4099a32d RS |
1617 | ((eq (car colors) 'underline) |
1618 | (set-face-underline-p face t frame)) | |
1619 | (t | |
1620 | (funcall function face (car colors) frame))))) | |
19ae9866 | 1621 | (setq colors (cdr colors))))))) |
e09c52a8 | 1622 | |
55893d25 RS |
1623 | ;;; Make the standard faces. |
1624 | ;;; The C code knows the default and modeline faces as faces 0 and 1, | |
1625 | ;;; so they must be the first two faces made. | |
1626 | (make-face 'default) | |
1627 | (make-face 'modeline) | |
1628 | (make-face 'highlight) | |
1629 | ||
1630 | ;; These aren't really special in any way, but they're nice to have around. | |
1631 | ||
1632 | (make-face 'bold) | |
1633 | (make-face 'italic) | |
1634 | (make-face 'bold-italic) | |
1635 | (make-face 'region) | |
1636 | (make-face 'secondary-selection) | |
1637 | (make-face 'underline) | |
1638 | ||
1639 | (setq region-face (face-id 'region)) | |
1640 | ||
113ab303 KH |
1641 | (defgroup basic-faces nil |
1642 | "The standard faces of Emacs." | |
1643 | :prefix "huh" | |
1644 | :group 'faces) | |
1645 | ||
55893d25 RS |
1646 | ;; Specify how these faces look, and their documentation. |
1647 | (let ((all '((bold "Use bold font." ((t (:bold t)))) | |
1648 | (bold-italic "Use bold italic font." ((t (:bold t :italic t)))) | |
1649 | (italic "Use italic font." ((t (:italic t)))) | |
1650 | (underline "Underline text." ((t (:underline t)))) | |
1651 | (default "Used for text not covered by other faces." ((t nil))) | |
1652 | (highlight "Highlight text in some way." | |
1c1e0fe5 RS |
1653 | ((((class color) (background light)) |
1654 | (:background "darkseagreen2")) | |
1655 | (((class color) (background dark)) | |
1656 | (:background "darkolivegreen")) | |
55893d25 RS |
1657 | (t (:inverse-video t)))) |
1658 | (modeline "Used for displaying the modeline." | |
1659 | ((t (:inverse-video t)))) | |
1660 | (region "Used for displaying the region." | |
1c1e0fe5 RS |
1661 | ((((class color) (background dark)) |
1662 | (:background "blue")) | |
1663 | (t (:background "gray")))) | |
55893d25 RS |
1664 | (secondary-selection |
1665 | "Used for displaying the secondary selection." | |
1c1e0fe5 RS |
1666 | ((((class color) (background light)) |
1667 | (:background "paleturquoise")) | |
1668 | (((class color) (background dark)) | |
1669 | (:background "darkslateblue")) | |
55893d25 RS |
1670 | (t (:inverse-video t)))))) |
1671 | entry symbol doc spec) | |
1672 | (while all | |
1673 | (setq entry (car all) | |
1674 | all (cdr all) | |
1675 | symbol (nth 0 entry) | |
1676 | doc (nth 1 entry) | |
1677 | spec (nth 2 entry)) | |
113ab303 | 1678 | (custom-add-to-group 'basic-faces symbol 'custom-face) |
55893d25 RS |
1679 | (put symbol 'face-documentation doc) |
1680 | (put symbol 'face-defface-spec spec))) | |
465fceed | 1681 | |
f0138172 JB |
1682 | (provide 'faces) |
1683 | ||
465fceed | 1684 | ;;; faces.el ends here |