comparison lisp/x-font-menu.el @ 414:da8ed4261e83 r21-2-15

Import from CVS: tag r21-2-15
author cvs
date Mon, 13 Aug 2007 11:21:38 +0200
parents 697ef44129c6
children
comparison
equal deleted inserted replaced
413:901169e5ca31 414:da8ed4261e83
5 ;; Copyright (C) 1997 Sun Microsystems 5 ;; Copyright (C) 1997 Sun Microsystems
6 6
7 ;; Author: Jamie Zawinski <jwz@netscape.com> 7 ;; Author: Jamie Zawinski <jwz@netscape.com>
8 ;; Restructured by: Jonathan Stigelman <Stig@hackvan.com> 8 ;; Restructured by: Jonathan Stigelman <Stig@hackvan.com>
9 ;; Mule-ized by: Martin Buchholz 9 ;; Mule-ized by: Martin Buchholz
10 ;; More restructuring for MS-Windows by Andy Piper <andy@xemacs.org>
10 11
11 ;; This file is part of XEmacs. 12 ;; This file is part of XEmacs.
12 13
13 ;; XEmacs is free software; you can redistribute it and/or modify it 14 ;; 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 ;; under the terms of the GNU General Public License as published by
22 23
23 ;; You should have received a copy of the GNU General Public License 24 ;; 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 ;; along with XEmacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, 59 Temple Place - Suite 330, 26 ;; Free Software Foundation, 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA. 27 ;; 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: 28 ;;; Code:
127 29
128 ;; #### - implement these... 30 ;; #### - implement these...
129 ;; 31 ;;
130 ;;; (defvar font-menu-ignore-proportional-fonts nil 32 ;;; (defvar font-menu-ignore-proportional-fonts nil
131 ;;; "*If non-nil, then the font menu will only show fixed-width fonts.") 33 ;;; "*If non-nil, then the font menu will only show fixed-width fonts.")
132 34
133 (defgroup font-menu () 35 (require 'font-menu)
134 "Settings for the font menu" 36
135 :group 'x) 37 (defvar x-font-menu-registry-encoding nil
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.") 38 "Registry and encoding to use with font menu fonts.")
179 39
180 (defvar font-menu-preferred-resolution "*-*" 40 (defvar x-fonts-menu-junk-families
181 "Preferred horizontal and vertical font menu resolution (e.g. \"75-75\").")
182
183 (defvar fonts-menu-junk-families
184 (purecopy 41 (purecopy
185 (mapconcat 42 (mapconcat
186 #'identity 43 #'identity
187 '("cursor" "glyph" "symbol" ; Obvious losers. 44 '("cursor" "glyph" "symbol" ; Obvious losers.
188 "\\`Ax...\\'" ; FrameMaker fonts - there are just way too 45 "\\`Ax...\\'" ; FrameMaker fonts - there are just way too
191 ; "Axcor" -> "Applix Courier Roman", 48 ; "Axcor" -> "Applix Courier Roman",
192 ; "Axcob" -> "Applix Courier Bold", etc. 49 ; "Axcob" -> "Applix Courier Bold", etc.
193 ) 50 )
194 "\\|")) 51 "\\|"))
195 "A regexp matching font families which are uninteresting (e.g. cursor fonts).") 52 "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 53
202 (defun hack-font-truename (fn) 54 (defun hack-font-truename (fn)
203 "Filter the output of `font-instance-truename' to deal with Japanese fontsets." 55 "Filter the output of `font-instance-truename' to deal with Japanese fontsets."
204 (if (string-match "," (font-instance-truename fn)) 56 (if (string-match "," (font-instance-truename fn))
205 (let ((fpnt (nth 8 (split-string (font-instance-name fn) "-"))) 57 (let ((fpnt (nth 8 (split-string (font-instance-name fn) "-")))
211 (setq flist (cdr flist)) 63 (setq flist (cdr flist))
212 )) 64 ))
213 ret) 65 ret)
214 (font-instance-truename fn))) 66 (font-instance-truename fn)))
215 67
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 68 (defvar x-font-regexp-ascii nil
221 "This is used to filter out font families that can't display ASCII text. 69 "This is used to filter out font families that can't display ASCII text.
222 It must be set at run-time.") 70 It must be set at run-time.")
223 71
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 72 ;;;###autoload
235 (defun reset-device-font-menus (&optional device debug) 73 (defun x-reset-device-font-menus (device &optional debug)
236 "Generates the `Font', `Size', and `Weight' submenus for the Options menu. 74 "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. 75 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 76 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 77 `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, 78 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." 79 or if you change your font path, you can call this to re-initialize the menus."
242 ;; by Stig@hackvan.com 80 ;; by Stig@hackvan.com
243 ;; #### - this should implement a `menus-only' option, which would 81 ;; #### - this should implement a `menus-only' option, which would
244 ;; recalculate the menus from the cache w/o having to do list-fonts again. 82 ;; recalculate the menus from the cache w/o having to do list-fonts again.
245 (message "Getting list of fonts from server... ") 83 (unless x-font-regexp-ascii
246 (if (or noninteractive 84 (setq x-font-regexp-ascii (if (featurep 'mule)
247 (not (or device (setq device (selected-device)))) 85 (charset-registry 'ascii)
248 (not (eq (device-type device) 'x))) 86 "iso8859-1")))
249 nil 87 (setq x-font-menu-registry-encoding
250 (unless x-font-regexp-ascii 88 (if (featurep 'mule) "*-*" "iso8859-1"))
251 (setq x-font-regexp-ascii (if (featurep 'mule) 89 (let ((case-fold-search t)
252 (charset-registry 'ascii) 90 family size weight entry monospaced-p
253 "iso8859-1"))) 91 dev-cache cache families sizes weights)
254 (setq font-menu-registry-encoding 92 (dolist (name (cond ((null debug) ; debugging kludge
255 (if (featurep 'mule) "*-*" "iso8859-1")) 93 (list-fonts "*-*-*-*-*-*-*-*-*-*-*-*-*-*" device))
256 (let ((case-fold-search t) 94 ((stringp debug) (split-string debug "\n"))
257 family size weight entry monospaced-p 95 (t debug)))
258 dev-cache cache families sizes weights) 96 (when (and (string-match x-font-regexp-ascii name)
259 (dolist (name (cond ((null debug) ; debugging kludge 97 (string-match x-font-regexp name))
260 (list-fonts "*-*-*-*-*-*-*-*-*-*-*-*-*-*" device)) 98 (setq weight (capitalize (match-string 1 name))
261 ((stringp debug) (split-string debug "\n")) 99 size (string-to-int (match-string 6 name)))
262 (t debug))) 100 (or (string-match x-font-regexp-foundry-and-family name)
263 (when (and (string-match x-font-regexp-ascii name) 101 (error "internal error"))
264 (string-match x-font-regexp name)) 102 (setq family (capitalize (match-string 1 name)))
265 (setq weight (capitalize (match-string 1 name)) 103 (or (string-match x-font-regexp-spacing name)
266 size (string-to-int (match-string 6 name))) 104 (error "internal error"))
267 (or (string-match x-font-regexp-foundry-and-family name) 105 (setq monospaced-p (string= "m" (match-string 1 name)))
268 (error "internal error")) 106 (unless (string-match x-fonts-menu-junk-families family)
269 (setq family (capitalize (match-string 1 name))) 107 (setq entry (or (vassoc family cache)
270 (or (string-match x-font-regexp-spacing name) 108 (car (setq cache
271 (error "internal error")) 109 (cons (vector family nil nil t)
272 (setq monospaced-p (string= "m" (match-string 1 name))) 110 cache)))))
273 (unless (string-match fonts-menu-junk-families family) 111 (or (member family families) (push family families))
274 (setq entry (or (vassoc family cache) 112 (or (member weight weights) (push weight weights))
275 (car (setq cache 113 (or (member size sizes) (push size sizes))
276 (cons (vector family nil nil t) 114 (or (member weight (aref entry 1)) (push weight (aref entry 1)))
277 cache))))) 115 (or (member size (aref entry 2)) (push size (aref entry 2)))
278 (or (member family families) (push family families)) 116 (aset entry 3 (and (aref entry 3) monospaced-p)))))
279 (or (member weight weights) (push weight weights)) 117 ;;
280 (or (member size sizes) (push size sizes)) 118 ;; Hack scalable fonts.
281 (or (member weight (aref entry 1)) (push weight (aref entry 1))) 119 ;; Some fonts come only in scalable versions (the only size is 0)
282 (or (member size (aref entry 2)) (push size (aref entry 2))) 120 ;; and some fonts come in both scalable and non-scalable versions
283 (aset entry 3 (and (aref entry 3) monospaced-p))))) 121 ;; (one size is 0). If there are any scalable fonts at all, make
284 ;; 122 ;; sure that the union of all point sizes contains at least some
285 ;; Hack scalable fonts. 123 ;; common sizes - it's possible that some sensible sizes might end
286 ;; Some fonts come only in scalable versions (the only size is 0) 124 ;; up not getting mentioned explicitly.
287 ;; and some fonts come in both scalable and non-scalable versions 125 ;;
288 ;; (one size is 0). If there are any scalable fonts at all, make 126 (if (member 0 sizes)
289 ;; sure that the union of all point sizes contains at least some 127 (let ((common '(60 80 100 120 140 160 180 240)))
290 ;; common sizes - it's possible that some sensible sizes might end 128 (while common
291 ;; up not getting mentioned explicitly. 129 (or;;(member (car common) sizes) ; not enough slack
292 ;; 130 (let ((rest sizes)
293 (if (member 0 sizes) 131 (done nil))
294 (let ((common '(60 80 100 120 140 160 180 240))) 132 (while (and (not done) rest)
295 (while common 133 (if (and (> (car common) (- (car rest) 5))
296 (or;;(member (car common) sizes) ; not enough slack 134 (< (car common) (+ (car rest) 5)))
297 (let ((rest sizes) 135 (setq done t))
298 (done nil)) 136 (setq rest (cdr rest)))
299 (while (and (not done) rest) 137 done)
300 (if (and (> (car common) (- (car rest) 5)) 138 (setq sizes (cons (car common) sizes)))
301 (< (car common) (+ (car rest) 5))) 139 (setq common (cdr common)))
302 (setq done t)) 140 (setq sizes (delq 0 sizes))))
303 (setq rest (cdr rest))) 141
304 done) 142 (setq families (sort families 'string-lessp)
305 (setq sizes (cons (car common) sizes))) 143 weights (sort weights 'string-lessp)
306 (setq common (cdr common))) 144 sizes (sort sizes '<))
307 (setq sizes (delq 0 sizes)))) 145
308 146 (dolist (entry cache)
309 (setq families (sort families 'string-lessp) 147 (aset entry 1 (sort (aref entry 1) 'string-lessp))
310 weights (sort weights 'string-lessp) 148 (aset entry 2 (sort (aref entry 2) '<)))
311 sizes (sort sizes '<)) 149
312 150 (setq dev-cache (assq device device-fonts-cache))
313 (dolist (entry cache) 151 (or dev-cache
314 (aset entry 1 (sort (aref entry 1) 'string-lessp)) 152 (setq dev-cache (car (push (list device) device-fonts-cache))))
315 (aset entry 2 (sort (aref entry 2) '<))) 153 (setcdr
316 154 dev-cache
317 (message "Getting list of fonts from server... done.") 155 (vector
318 156 cache
319 (setq dev-cache (assq device device-fonts-cache)) 157 (mapcar (lambda (x)
320 (or dev-cache 158 (vector x
321 (setq dev-cache (car (push (list device) device-fonts-cache)))) 159 (list 'font-menu-set-font x nil nil)
322 (setcdr 160 ':style 'radio ':active nil ':selected nil))
323 dev-cache 161 families)
324 (vector 162 (mapcar (lambda (x)
325 cache 163 (vector (if (/= 0 (% x 10))
326 (mapcar (lambda (x) 164 ;; works with no LISP_FLOAT_TYPE
327 (vector x 165 (concat (int-to-string (/ x 10)) "."
328 (list 'font-menu-set-font x nil nil) 166 (int-to-string (% x 10)))
329 ':style 'radio ':active nil ':selected nil)) 167 (int-to-string (/ x 10)))
330 families) 168 (list 'font-menu-set-font nil nil x)
331 (mapcar (lambda (x) 169 ':style 'radio ':active nil ':selected nil))
332 (vector (if (/= 0 (% x 10)) 170 sizes)
333 ;; works with no LISP_FLOAT_TYPE 171 (mapcar (lambda (x)
334 (concat (int-to-string (/ x 10)) "." 172 (vector x
335 (int-to-string (% x 10))) 173 (list 'font-menu-set-font nil x nil)
336 (int-to-string (/ x 10))) 174 ':style 'radio ':active nil ':selected nil))
337 (list 'font-menu-set-font nil nil x) 175 weights)))
338 ':style 'radio ':active nil ':selected nil)) 176 (cdr dev-cache)))
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 177
347 ;; Extract font information from a face. We examine both the 178 ;; Extract font information from a face. We examine both the
348 ;; user-specified font name and the canonical (`true') font name. 179 ;; user-specified font name and the canonical (`true') font name.
349 ;; These can appear to have totally different properties. 180 ;; These can appear to have totally different properties.
350 ;; For examples, see the prolog above. 181 ;; For examples, see the prolog above.
351 182
352 ;; We use the user-specified one if possible, else use the truename. 183 ;; We use the user-specified one if possible, else use the truename.
353 ;; If the user didn't specify one (with "-dt-*-*", for example) 184 ;; If the user didn't specify one (with "-dt-*-*", for example)
354 ;; get the truename and use the possibly suboptimal data from that. 185 ;; get the truename and use the possibly suboptimal data from that.
355 (defun* font-menu-font-data (face dcache) 186 ;;;###autoload
187 (defun* x-font-menu-font-data (face dcache)
356 (let* ((case-fold-search t) 188 (let* ((case-fold-search t)
357 (domain (if font-menu-this-frame-only-p 189 (domain (if font-menu-this-frame-only-p
358 (selected-frame) 190 (selected-frame)
359 (selected-device))) 191 (selected-device)))
360 (name (font-instance-name (face-font-instance face domain))) 192 (name (font-instance-name (face-font-instance face domain)))
368 (when (and (null entry) 200 (when (and (null entry)
369 (string-match x-font-regexp-foundry-and-family truename)) 201 (string-match x-font-regexp-foundry-and-family truename))
370 (setq family (capitalize (match-string 1 truename))) 202 (setq family (capitalize (match-string 1 truename)))
371 (setq entry (vassoc family (aref dcache 0)))) 203 (setq entry (vassoc family (aref dcache 0))))
372 (when (null entry) 204 (when (null entry)
373 (return-from font-menu-font-data (make-vector 5 nil))) 205 (return-from x-font-menu-font-data (make-vector 5 nil)))
374 206
375 (when (string-match x-font-regexp name) 207 (when (string-match x-font-regexp name)
376 (setq weight (capitalize (match-string 1 name))) 208 (setq weight (capitalize (match-string 1 name)))
377 (setq size (string-to-int (match-string 6 name)))) 209 (setq size (string-to-int (match-string 6 name))))
378 210
383 (setq size (string-to-int (match-string 6 truename)))) 215 (setq size (string-to-int (match-string 6 truename))))
384 (setq slant (capitalize (match-string 2 truename)))) 216 (setq slant (capitalize (match-string 2 truename))))
385 217
386 (vector entry family size weight slant))) 218 (vector entry family size weight slant)))
387 219
388 (defun font-menu-split-long-menu (menu) 220 (defun x-font-menu-load-font (family weight size slant resolution)
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
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. 221 "Try to load a font with the requested properties.
612 The weight, slant and resolution are only hints." 222 The weight, slant and resolution are only hints."
613 (when (integerp size) (setq size (int-to-string size))) 223 (when (integerp size) (setq size (int-to-string size)))
614 (let (font) 224 (let (font)
615 (catch 'got-font 225 (catch 'got-font
625 (list resolution "*-*"))) 235 (list resolution "*-*")))
626 (when (setq font 236 (when (setq font
627 (make-font-instance 237 (make-font-instance
628 (concat "-*-" family "-" weight "-" slant "-*-*-*-" 238 (concat "-*-" family "-" weight "-" slant "-*-*-*-"
629 size "-" resolution "-*-*-" 239 size "-" resolution "-*-*-"
630 font-menu-registry-encoding) 240 x-font-menu-registry-encoding)
631 nil t)) 241 nil t))
632 (throw 'got-font font)))))))) 242 (throw 'got-font font))))))))
633 243
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) 244 (provide 'x-font-menu)
643 245
644 ;;; x-font-menu.el ends here 246 ;;; x-font-menu.el ends here