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