2
votes

My GNU Emacs configuration is shared between multiple computers (including fixed installations for FreeBSD, Ubuntu, and Windows 7 and a portable installation with NT Emacs and Cygwin on a USB drive). I want to define a list of fonts that Emacs will try, in order, until an installed font is found (since I don't always have root access where I can install fonts). Usually, set-fontset-font and friends would work; however, when Emacs is used as a daemon (emacs --daemon) the normal fontsets aren't created until a frame is opened. Since this all needs to run as part of my .emacs, that's too late.

(Just in case the fontset creation problem is something specific to FreeBSD's Emacs port or my configuration, this is the output of make showconfig.)

1

1 Answers

3
votes

This question got me most of the way there; the proposed answer works as long as you only need one font. To have multiple fallback fonts, you need to define a fontset and tell Emacs to use that fontset for frames. First, define the list of fonts you want to use:

;; Fill in your list of fonts here
(setq my-fonts '("-xos4-terminus-medium-*-*-*-16-*-*-*-*-*-iso10646-1" ...))

Then, put those fonts into the standard fontset:

;; NOTE: only works if you don't use --daemon
(dolist (font (reverse my-fonts))
  (set-fontset-font "fontset-standard" 'unicode font nil 'prepend))
(add-to-list 'default-frame-alist '(font . "fontset-standard"))

This will work fine as long as you never start Emacs as a daemon; if you do, you're told

error: Fontset `fontset-standard' does not exist

To fix that, we need to create the standard fontset before adding fonts to it. Emacs defines standard-fontset-spec to be the value the standard fontset is initialized with; we can do it ourselves by calling create-fontset-from-fontset-spec first:

(create-fontset-from-fontset-spec standard-fontset-spec) ;to make --daemon work
(dolist (font (reverse my-fonts))
  (set-fontset-font "fontset-standard" 'unicode font nil 'prepend))
(add-to-list 'default-frame-alist '(font . "fontset-standard"))