Listing installed TrueType fonts in Emacs

In this new fangled age of TrueType support in Emacs it’s really useful to know the names of the Monospace fonts installed on your system in the format that Emacs understands. If you are on a Linux/UNIX system running Xorg with fontconfig installed then add this function to your .emacs file

(defun list-monospace-truetype-fonts ()
  "Provide a list of available monospaced truetype fonts on this
   machine"
  (interactive)
  (set-buffer (get-buffer-create "Monospaced Fonts"))
  (call-process-shell-command "fc-list :spacing=mono:scalable=true family | sort" nil t)
  (setq buffer-read-only t)
  (switch-to-buffer-other-window (current-buffer)))

You can set these fonts in a number of ways, currently I am getting the most joy using this form:

(defun safe-set-initial-font (fontstring)
  "Set the default frame font"
  (interactive "s")
  (setq initial-frame-alist
 `((font . ,fontstring)
   (background-color . ,(face-background 'default))
   (foreground-color . ,(face-foreground 'default))
   (horizontal-scroll-bars . nil)
   (vertical-scroll-bars . nil)
   (menu-bar-lines . 0)
   (height . 40)
   (width . 80)
   (cursor-color . "red")
   (mouse-color . "green")))
  (setq default-frame-alist (copy-alist initial-frame-alist)))

That’s pretty nasty, but it gets round a bug where opening a new frame results in a different font being used.