d96fe11ba83d9b23d1049f3b33d600dafdad7b39
[chise/xemacs-chise.git.1] / lisp / x-font-menu.el
1 ;; x-font-menu.el --- Managing menus of X fonts.
2
3 ;; Copyright (C) 1994 Free Software Foundation, Inc.
4 ;; Copyright (C) 1995 Tinker Systems and INS Engineering Corp.
5 ;; Copyright (C) 1997 Sun Microsystems
6
7 ;; Author: Jamie Zawinski <jwz@netscape.com>
8 ;; Restructured by: Jonathan Stigelman <Stig@hackvan.com>
9 ;; Mule-ized by: Martin Buchholz
10
11 ;; This file is part of XEmacs.
12
13 ;; XEmacs is free software; you can redistribute it and/or modify it
14 ;; under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; any later version.
17
18 ;; XEmacs is distributed in the hope that it will be useful, but
19 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21 ;; General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with XEmacs; see the file COPYING.  If not, write to the 
25 ;; Free Software Foundation, 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
27
28 ;;; Commentary:
29 ;;;
30 ;;; Creates three menus, "Font", "Size", and "Weight", and puts them on the
31 ;;; "Options" menu.  The contents of these menus are the superset of those
32 ;;; properties available on any fonts, but only the intersection of the three
33 ;;; sets is selectable at one time.
34 ;;;
35 ;;; Known Problems:
36 ;;; ===============
37 ;;; Items on the Font menu are selectable if and only if that font exists in
38 ;;; the same size and weight as the current font.  This means that some fonts
39 ;;; are simply not reachable from some other fonts - if only one font comes
40 ;;; in only one point size (like "Nil", which comes only in 2), you will never
41 ;;; be able to select it.  It would be better if the items on the Fonts menu
42 ;;; were always selectable, and selecting them would set the size to be the
43 ;;; closest size to the current font's size.
44 ;;;
45 ;;; This attempts to change all other faces in an analagous way to the change
46 ;;; that was made to the default face; if it can't, it will skip over the face.
47 ;;; However, this could leave incongruous font sizes around, which may cause
48 ;;; some nonreversibility problems if further changes are made.  Perhaps it
49 ;;; should remember the initial fonts of all faces, and derive all subsequent
50 ;;; fonts from that initial state.
51 ;;;
52 ;;; xfontsel(1) is a lot more flexible (but probably harder to understand).
53 ;;;
54 ;;; The code to construct menus from all of the x11 fonts available from the
55 ;;; server is autoloaded and executed the very first time that one of the Font
56 ;;; menus is selected on each device.  That is, if XEmacs has frames on two
57 ;;; different devices, then separate font menu information will be maintained
58 ;;; for each X display.  If the font path changes after emacs has already
59 ;;; asked the X server on a particular display for its list of fonts, this
60 ;;; won't notice.  Also, the first time that a font menu is posted on each
61 ;;; display will entail a lengthy delay, but that's better than slowing down
62 ;;; XEmacs startup.  At any time (i.e.: after a font-path change or
63 ;;; immediately after device creation), you can call
64 ;;; `reset-device-font-menus' to rebuild the menus from all currently
65 ;;; available fonts.
66 ;;;
67 ;;; There is knowledge here about the regexp match numbers in
68 ;;; `x-font-regexp' and `x-font-regexp-foundry-and-family' defined in
69 ;;; x-faces.el.
70 ;;;
71 ;;; There are at least three kinds of fonts under X11r5:
72 ;;;
73 ;;; - bitmap fonts, which can be assumed to look as good as possible;
74 ;;; - bitmap fonts which have been (or can be) automatically scaled to
75 ;;;   a new size, and which almost always look awful;
76 ;;; - and true outline fonts, which should look ok at any size, but in
77 ;;;   practice (on at least some systems) look awful at any size, and
78 ;;;   even in theory are unlikely ever to look as good as non-scaled
79 ;;;   bitmap fonts.
80 ;;;
81 ;;; It would be nice to get this code to look for non-scaled bitmap fonts
82 ;;; first, then outline fonts, then scaled bitmap fonts as a last resort.
83 ;;; But it's not clear to me how to tell them apart based on their truenames
84 ;;; and/or the result of XListFonts().  I welcome any and all explanations
85 ;;; of the subtleties involved...
86 ;;;
87 ;;;
88 ;;; If You Think You'Re Seeing A Bug:
89 ;;; =================================
90 ;;; When reporting problems, send the following information:
91 ;;;
92 ;;; - Exactly what behavior you're seeing;
93 ;;; - The output of the `xlsfonts' program;
94 ;;; - The value of the variable `device-fonts-cache';
95 ;;; - The values of the following expressions, both before and after
96 ;;;   making a selection from any of the fonts-related menus:
97 ;;;     (face-font 'default)
98 ;;;     (font-truename   (face-font 'default))
99 ;;;     (font-properties (face-font 'default))
100 ;;; - The values of the following variables after making a selection:
101 ;;;     font-menu-preferred-resolution
102 ;;;     font-menu-registry-encoding
103 ;;;
104 ;;; There is a common misconception that "*-courier-medium-r-*-11-*", also
105 ;;; known as "-adobe-courier-medium-r-normal--11-80-100-100-m-60-iso8859-1",
106 ;;; is an 11-point font.  It is not -- it is an 11-pixel font at 100dpi,
107 ;;; which is an 8-point font (the number after -11- is the size in tenths
108 ;;; of points).  So if you expect to be seeing an "11" entry in the "Size"
109 ;;; menu and are not, this may be why.
110 ;;;
111 ;;; In the real world (aka Solaris), one has to deal with fonts that
112 ;;; appear to be medium-i but are really light-r, and fonts that
113 ;;; resolve to different resolutions depending on the charset:
114 ;;;
115 ;;; (font-instance-truename
116 ;;;  (make-font-instance "-*-mincho-medium-i-normal-*-*-*-*-*-*-*-jisx0201*-*"))
117 ;;; ==>
118 ;;; "-morisawa-ryumin light kl-light-r-normal--10-100-72-72-m-50-jisx0201.1976-0"
119 ;;;
120 ;;; (list-fonts "-dt-interface user-medium-r-normal-s*-*-*-*-*-*-*-*-*")
121 ;;; ==>
122 ;;; ("-dt-interface user-medium-r-normal-s sans-12-120-72-72-m-70-iso8859-1"
123 ;;;  "-dt-interface user-medium-r-normal-s-14-120-75-75-m-120-jisx0208.1983-0"
124 ;;;  "-dt-interface user-medium-r-normal-s-14-120-75-75-m-60-jisx0201.1976-0")
125
126 ;;; Code:
127
128 ;; #### - implement these...
129 ;;
130 ;;; (defvar font-menu-ignore-proportional-fonts nil
131 ;;;   "*If non-nil, then the font menu will only show fixed-width fonts.")
132
133 (defgroup font-menu ()
134   "Settings for the font menu"
135   :group 'x)
136
137 ;;;###autoload
138 (defcustom font-menu-ignore-scaled-fonts t
139   "*If non-nil, then the font menu will try to show only bitmap fonts."
140   :type 'boolean
141   :group 'font-menu)
142
143 ;;;###autoload
144 (defcustom font-menu-this-frame-only-p nil
145   "*If non-nil, then changing the default font from the font menu will only
146 affect one frame instead of all frames."
147   :type 'boolean
148   :group 'font-menu)
149
150 (defcustom font-menu-max-items 25
151   "*Maximum number of items in the font menu
152 If number of entries in a menu is larger than this value, split menu
153 into submenus of nearly equal length.  If nil, never split menu into
154 submenus."
155   :group 'font-menu
156   :type '(choice (const :tag "no submenus" nil)
157                  (integer)))
158
159 (defcustom font-menu-submenu-name-format "%-12.12s ... %.12s"
160   "*Format specification of the submenu name.
161 Used by `font-menu-split-long-menu' if the number of entries in a menu is
162 larger than `font-menu-menu-max-items'.
163 This string should contain one %s for the name of the first entry and
164 one %s for the name of the last entry in the submenu.
165 If the value is a function, it should return the submenu name.  The
166 function is be called with two arguments, the names of the first and
167 the last entry in the menu."
168   :group 'font-menu
169   :type '(choice (string :tag "Format string")
170                  (function)))
171
172
173 ;; only call XListFonts (and parse) once per device.
174 ;; ( (device . [parsed-list-fonts family-menu size-menu weight-menu]) ...)
175 (defvar device-fonts-cache nil)
176
177 (defvar font-menu-registry-encoding nil
178   "Registry and encoding to use with font menu fonts.")
179
180 (defvar font-menu-preferred-resolution "*-*"
181   "Preferred horizontal and vertical font menu resolution (e.g. \"75-75\").")
182
183 (defvar fonts-menu-junk-families
184   (purecopy
185    (mapconcat
186     #'identity
187     '("cursor" "glyph" "symbol" ; Obvious losers.
188       "\\`Ax...\\'"             ; FrameMaker fonts - there are just way too
189                                 ;  many of these, and there is a different
190                                 ;  font family for each font face!  Losers.
191                                 ;  "Axcor" -> "Applix Courier Roman",
192                                 ;  "Axcob" -> "Applix Courier Bold", etc.
193       )
194     "\\|"))
195   "A regexp matching font families which are uninteresting (e.g. cursor fonts).")
196
197 (eval-when-compile
198   (defsubst device-fonts-cache ()
199     (or (cdr (assq (selected-device) device-fonts-cache))
200         (reset-device-font-menus (selected-device)))))
201
202 (defun hack-font-truename (fn)
203   "Filter the output of `font-instance-truename' to deal with Japanese fontsets."
204   (if (string-match "," (font-instance-truename fn))
205       (let ((fpnt (nth 8 (split-string (font-instance-name fn) "-")))
206             (flist (split-string (font-instance-truename fn) ","))
207             ret)
208         (while flist
209           (if (string-equal fpnt (nth 8 (split-string (car flist) "-")))
210               (progn (setq ret (car flist)) (setq flist nil))
211             (setq flist (cdr flist))
212             ))
213         ret)
214     (font-instance-truename fn)))
215
216 ;;;###autoload
217 (fset 'install-font-menus 'reset-device-font-menus)
218 (make-obsolete 'install-font-menus 'reset-device-font-menus)
219
220 (defvar x-font-regexp-ascii nil
221   "This is used to filter out font families that can't display ASCII text.
222 It must be set at run-time.")
223
224 (defun vassoc (key valist)
225   "Search VALIST for a vector whose first element is equal to KEY.
226 See also `assoc'."
227   ;; by Stig@hackvan.com
228   (let (el)
229     (catch 'done
230       (while (setq el (pop valist))
231         (and (equal key (aref el 0))
232              (throw 'done el))))))
233
234 ;;;###autoload
235 (defun reset-device-font-menus (&optional device debug)
236   "Generates the `Font', `Size', and `Weight' submenus for the Options menu.
237 This is run the first time that a font-menu is needed for each device.
238 If you don't like the lazy invocation of this function, you can add it to
239 `create-device-hook' and that will make the font menus respond more quickly
240 when they are selected for the first time.  If you add fonts to your system, 
241 or if you change your font path, you can call this to re-initialize the menus."
242   ;; by Stig@hackvan.com
243   ;; #### - this should implement a `menus-only' option, which would
244   ;; recalculate the menus from the cache w/o having to do list-fonts again.
245   (message "Getting list of fonts from server... ")
246   (if (or noninteractive
247           (not (or device (setq device (selected-device))))
248           (not (eq (device-type device) 'x)))
249       nil
250     (unless x-font-regexp-ascii
251       (setq x-font-regexp-ascii (if (featurep 'mule)
252                                     (charset-registry 'ascii)
253                                   "iso8859-1")))
254     (setq font-menu-registry-encoding
255           (if (featurep 'mule) "*-*" "iso8859-1"))
256     (let ((case-fold-search t)
257           family size weight entry monospaced-p
258           dev-cache cache families sizes weights)
259       (dolist (name (cond ((null debug) ; debugging kludge
260                            (list-fonts "*-*-*-*-*-*-*-*-*-*-*-*-*-*" device))
261                           ((stringp debug) (split-string debug "\n"))
262                           (t debug)))
263         (when (and (string-match x-font-regexp-ascii name)
264                    (string-match x-font-regexp name))
265           (setq weight (capitalize (match-string 1 name))
266                 size   (string-to-int (match-string 6 name)))
267           (or (string-match x-font-regexp-foundry-and-family name)
268               (error "internal error"))
269           (setq family (capitalize (match-string 1 name)))
270           (or (string-match x-font-regexp-spacing name)
271               (error "internal error"))
272           (setq monospaced-p (string= "m" (match-string 1 name)))
273           (unless (string-match fonts-menu-junk-families family)
274             (setq entry (or (vassoc family cache)
275                             (car (setq cache
276                                        (cons (vector family nil nil t)
277                                              cache)))))
278             (or (member family families) (push family families))
279             (or (member weight weights)  (push weight weights))
280             (or (member size   sizes)    (push size   sizes))
281             (or (member weight (aref entry 1)) (push weight (aref entry 1)))
282             (or (member size   (aref entry 2)) (push size   (aref entry 2)))
283             (aset entry 3 (and (aref entry 3) monospaced-p)))))
284       ;;
285       ;; Hack scalable fonts.
286       ;; Some fonts come only in scalable versions (the only size is 0)
287       ;; and some fonts come in both scalable and non-scalable versions
288       ;; (one size is 0).  If there are any scalable fonts at all, make
289       ;; sure that the union of all point sizes contains at least some
290       ;; common sizes - it's possible that some sensible sizes might end
291       ;; up not getting mentioned explicitly.
292       ;;
293       (if (member 0 sizes)
294           (let ((common '(60 80 100 120 140 160 180 240)))
295             (while common
296               (or;;(member (car common) sizes)   ; not enough slack
297                (let ((rest sizes)
298                      (done nil))
299                  (while (and (not done) rest)
300                    (if (and (> (car common) (- (car rest) 5))
301                             (< (car common) (+ (car rest) 5)))
302                        (setq done t))
303                    (setq rest (cdr rest)))
304                  done)
305                (setq sizes (cons (car common) sizes)))
306               (setq common (cdr common)))
307             (setq sizes (delq 0 sizes))))
308
309       (setq families (sort families 'string-lessp)
310             weights  (sort weights 'string-lessp)
311             sizes    (sort sizes '<))
312
313       (dolist (entry cache)
314           (aset entry 1 (sort (aref entry 1) 'string-lessp))
315           (aset entry 2 (sort (aref entry 2) '<)))
316
317       (message "Getting list of fonts from server... done.")
318
319       (setq dev-cache (assq device device-fonts-cache))
320       (or dev-cache
321           (setq dev-cache (car (push (list device) device-fonts-cache))))
322       (setcdr
323        dev-cache
324        (vector
325         cache
326         (mapcar (lambda (x)
327                   (vector x
328                           (list 'font-menu-set-font x nil nil)
329                           ':style 'radio ':active nil ':selected nil))
330                 families)
331         (mapcar (lambda (x)
332                   (vector (if (/= 0 (% x 10))
333                               ;; works with no LISP_FLOAT_TYPE
334                               (concat (int-to-string (/ x 10)) "."
335                                       (int-to-string (% x 10)))
336                             (int-to-string (/ x 10)))
337                           (list 'font-menu-set-font nil nil x)
338                           ':style 'radio ':active nil ':selected nil))
339                 sizes)
340         (mapcar (lambda (x)
341                   (vector x
342                           (list 'font-menu-set-font nil x nil)
343                           ':style 'radio ':active nil ':selected nil))
344                 weights)))
345       (cdr dev-cache))))
346
347 ;; Extract font information from a face.  We examine both the
348 ;; user-specified font name and the canonical (`true') font name.
349 ;; These can appear to have totally different properties.
350 ;; For examples, see the prolog above.
351
352 ;; We use the user-specified one if possible, else use the truename.
353 ;; If the user didn't specify one (with "-dt-*-*", for example)
354 ;; get the truename and use the possibly suboptimal data from that.
355 (defun* font-menu-font-data (face dcache)
356   (let* ((case-fold-search t)
357          (domain (if font-menu-this-frame-only-p
358                                   (selected-frame)
359                                 (selected-device)))
360          (name (font-instance-name (face-font-instance face domain)))
361          (truename (font-instance-truename
362                     (face-font-instance face domain
363                                         (if (featurep 'mule) 'ascii))))
364          family size weight entry slant)
365     (when (string-match x-font-regexp-foundry-and-family name)
366       (setq family (capitalize (match-string 1 name)))
367       (setq entry (vassoc family (aref dcache 0))))
368     (when (and (null entry)
369                (string-match x-font-regexp-foundry-and-family truename))
370       (setq family (capitalize (match-string 1 truename)))
371       (setq entry  (vassoc family (aref dcache 0))))
372     (when (null entry)
373       (return-from font-menu-font-data (make-vector 5 nil)))
374     
375     (when (string-match x-font-regexp name)
376       (setq weight (capitalize    (match-string 1 name)))
377       (setq size   (string-to-int (match-string 6 name))))
378       
379     (when (string-match x-font-regexp truename)
380       (when (not (member weight (aref entry 1)))
381         (setq weight (capitalize (match-string 1 truename))))
382       (when (not (member size   (aref entry 2)))
383         (setq size (string-to-int (match-string 6 truename))))
384       (setq slant (capitalize (match-string 2 truename))))
385       
386     (vector entry family size weight slant)))
387
388 (defun font-menu-split-long-menu (menu)
389   "Split MENU according to `font-menu-max-items'."
390   (let ((len (length menu)))
391     (if (or (null font-menu-max-items)
392             (null (featurep 'lisp-float-type))
393             (<= len font-menu-max-items))
394         menu
395       ;; Submenu is max 2 entries longer than menu, never shorter, number of
396       ;; entries in submenus differ by at most one (with longer submenus first)
397       (let* ((outer (floor (sqrt len)))
398              (inner (/ len outer))
399              (rest (% len outer))
400              (result nil))
401         (setq menu (reverse menu))
402         (while menu
403           (let ((in inner)
404                 (sub nil)
405                 (to (car menu)))
406             (while (> in 0)
407               (setq in   (1- in)
408                     sub  (cons (car menu) sub)
409                     menu (cdr menu)))
410             (setq result
411                   (cons (cons (if (stringp font-menu-submenu-name-format)
412                                   (format font-menu-submenu-name-format
413                                           (aref (car sub) 0) (aref to 0))
414                                 (funcall font-menu-submenu-name-format
415                                          (aref (car sub) 0) (aref to 0)))
416                               sub)
417                         result)
418                   rest  (1+ rest))
419             (if (= rest outer) (setq inner (1+ inner)))))
420         result))))
421
422 ;;;###autoload
423 (defun font-menu-family-constructor (ignored)
424   (catch 'menu
425     (unless (eq 'x (device-type (selected-device)))
426       (throw 'menu '(["Cannot parse current font" ding nil])))
427     (let* ((dcache (device-fonts-cache))
428            (font-data (font-menu-font-data 'default dcache))
429            (entry  (aref font-data 0))
430            (family (aref font-data 1))
431            (size   (aref font-data 2))
432            (weight (aref font-data 3))
433            f)
434       (unless family
435         (throw 'menu '(["Cannot parse current font" ding nil])))
436       ;; Items on the Font menu are enabled iff that font exists in
437       ;; the same size and weight as the current font (scalable fonts
438       ;; exist in every size).  Only the current font is marked as
439       ;; selected.
440       (font-menu-split-long-menu
441        (mapcar
442         (lambda (item)
443           (setq f (aref item 0)
444                 entry (vassoc f (aref dcache 0)))
445           ;; The user can no longer easily control the weight using the menu
446           ;; Note it is silly anyway as it could very well be that the font
447           ;; has no common size+weight combinations with the default font.
448 ;;        (if (and (member weight (aref entry 1))
449 ;;                 (or (member size (aref entry 2))
450 ;;                     (and (not font-menu-ignore-scaled-fonts)
451 ;;                          (member 0 (aref entry 2)))))
452 ;;            (enable-menu-item item)
453 ;;          (disable-menu-item item))
454           (if (and font-menu-ignore-scaled-fonts (member 0 (aref entry 2)))
455               (disable-menu-item item)
456             (enable-menu-item item))      
457           (if (string-equal family f)
458               (select-toggle-menu-item item)
459             (deselect-toggle-menu-item item))
460           item)
461         (aref dcache 1))))))
462
463 ;;;###autoload
464 (defun font-menu-size-constructor (ignored)
465   (catch 'menu
466     (unless (eq 'x (device-type (selected-device)))
467       (throw 'menu '(["Cannot parse current font" ding nil])))
468     (let* ((dcache (device-fonts-cache))
469            (font-data (font-menu-font-data 'default dcache))
470            (entry  (aref font-data 0))
471            (family (aref font-data 1))
472            (size   (aref font-data 2))
473            ;;(weight (aref font-data 3))
474            s)
475       (unless family
476         (throw 'menu '(["Cannot parse current font" ding nil])))
477       ;; Items on the Size menu are enabled iff current font has
478       ;; that size.  Only the size of the current font is selected.
479       ;; (If the current font comes in size 0, it is scalable, and
480       ;; thus has every size.)
481       (mapcar
482        (lambda (item)
483          (setq s (nth 3 (aref item 1)))
484          (if (or (member s (aref entry 2))
485                  (and (not font-menu-ignore-scaled-fonts)
486                       (member 0 (aref entry 2))))
487              (enable-menu-item item)
488            (disable-menu-item item))
489          (if (eq size s)
490              (select-toggle-menu-item item)
491            (deselect-toggle-menu-item item))
492          item)
493        (aref dcache 2)))))
494
495 ;;;###autoload
496 (defun font-menu-weight-constructor (ignored)
497   (catch 'menu
498     (unless (eq 'x (device-type (selected-device)))
499       (throw 'menu '(["Cannot parse current font" ding nil])))
500     (let* ((dcache (device-fonts-cache))
501            (font-data (font-menu-font-data 'default dcache))
502            (entry  (aref font-data 0))
503            (family (aref font-data 1))
504            ;;(size   (aref font-data 2))
505            (weight (aref font-data 3))
506            w)
507       (unless family
508         (throw 'menu '(["Cannot parse current font" ding nil])))
509       ;; Items on the Weight menu are enabled iff current font
510       ;; has that weight.  Only the weight of the current font
511       ;; is selected.
512       (mapcar
513        (lambda (item)
514          (setq w (aref item 0))
515          (if (member w (aref entry 1))
516              (enable-menu-item item)
517            (disable-menu-item item))
518          (if (string-equal weight w)
519              (select-toggle-menu-item item)
520            (deselect-toggle-menu-item item))
521          item)
522        (aref dcache 3)))))
523
524 \f
525 ;;; Changing font sizes
526
527 (defun font-menu-set-font (family weight size)
528   ;; This is what gets run when an item is selected from any of the three
529   ;; fonts menus.  It needs to be rather clever.
530   ;; (size is measured in 10ths of points.)
531   (let* ((dcache (device-fonts-cache))
532          (font-data (font-menu-font-data 'default dcache))
533          (from-family (aref font-data 1))
534          (from-size   (aref font-data 2))
535          (from-weight (aref font-data 3))
536          (from-slant  (aref font-data 4))
537          new-default-face-font
538          new-props)
539     (unless from-family
540       (signal 'error '("couldn't parse font name for default face")))
541     (when weight
542       (signal 'error '("Setting weight currently not supported")))
543     (setq new-default-face-font
544           (font-menu-load-font (or family from-family)
545                                (or weight from-weight)
546                                (or size   from-size)
547                                from-slant
548                                font-menu-preferred-resolution))
549     (dolist (face (delq 'default (face-list)))
550       (when (face-font-instance face)
551         (message "Changing font of `%s'..." face)
552         (condition-case c
553             (font-menu-change-face face
554                                    from-family from-weight from-size
555                                    family      weight      size)
556           (error
557            (display-error c nil)
558            (sit-for 1)))))
559     ;; Set the default face's font after hacking the other faces, so that
560     ;; the frame size doesn't change until we are all done.
561
562     ;; If we need to be frame local we do the changes ourselves.
563     (if font-menu-this-frame-only-p
564     ;;; WMP - we need to honor font-menu-this-frame-only-p here!
565         (set-face-font 'default new-default-face-font
566                        (and font-menu-this-frame-only-p (selected-frame)))
567       ;; OK Let Customize do it.
568       (when (and family (not (equal family from-family)))
569         (setq new-props (append (list :family family) new-props)))
570       (when (and size (not (equal size from-size)))
571         (setq new-props (append
572            (list :size (concat (int-to-string (/ size 10)) "pt")) new-props)))
573       (custom-set-face-update-spec 'default '((type x)) new-props)
574       (message "Font %s" (face-font-name 'default)))))
575
576
577 (defun font-menu-change-face (face
578                               from-family from-weight from-size
579                               to-family   to-weight   to-size)
580   (or (symbolp face) (signal 'wrong-type-argument (list 'symbolp face)))
581   (let* ((dcache (device-fonts-cache))
582          (font-data (font-menu-font-data face dcache))
583          (face-family (aref font-data 1))
584          (face-size   (aref font-data 2))
585          (face-weight (aref font-data 3))
586          (face-slant  (aref font-data 4)))
587
588     (or face-family
589         (signal 'error (list "couldn't parse font name for face" face)))
590
591     ;; If this face matches the old default face in the attribute we
592     ;; are changing, then change it to the new attribute along that
593     ;; dimension.  Also, the face must have its own global attribute.
594     ;; If its value is inherited, we don't touch it.  If any of this
595     ;; is not true, we leave it alone.
596     (when (and (face-font face 'global)
597                (cond 
598                 (to-family (string-equal face-family from-family))
599                 (to-weight (string-equal face-weight from-weight))
600                 (to-size   (=            face-size   from-size))))
601       (set-face-font face
602                      (font-menu-load-font (or to-family face-family)
603                                           (or to-weight face-weight)
604                                           (or to-size   face-size)
605                                           face-slant
606                                           font-menu-preferred-resolution)
607                      (and font-menu-this-frame-only-p
608                           (selected-frame))))))
609
610 (defun font-menu-load-font (family weight size slant resolution)
611   "Try to load a font with the requested properties.
612 The weight, slant and resolution are only hints."
613   (when (integerp size) (setq size (int-to-string size)))
614   (let (font)
615     (catch 'got-font
616       (dolist (weight (list weight "*"))
617         (dolist (slant
618                  (cond ((string-equal slant "O") '("O" "I" "*"))
619                        ((string-equal slant "I") '("I" "O" "*"))
620                        ((string-equal slant "*") '("*"))
621                        (t (list slant "*"))))
622           (dolist (resolution
623                    (if (string-equal resolution "*-*")
624                        (list resolution)
625                      (list resolution "*-*")))
626             (when (setq font
627                         (make-font-instance
628                          (concat  "-*-" family "-" weight "-" slant "-*-*-*-"
629                                   size "-" resolution "-*-*-"
630                                   font-menu-registry-encoding)
631                          nil t))
632               (throw 'got-font font))))))))
633
634 (defun flush-device-fonts-cache (device)
635   ;; by Stig@hackvan.com
636   (let ((elt (assq device device-fonts-cache)))
637     (and elt
638          (setq device-fonts-cache (delq elt device-fonts-cache)))))
639
640 (add-hook 'delete-device-hook 'flush-device-fonts-cache)
641
642 (provide 'x-font-menu)
643
644 ;;; x-font-menu.el ends here