RSS

How to preview fixed width (mono spaced) fonts in an editable Emacs buffer?

09 May

When using Emacs, I don’t spend time thinking about fonts most of the time. Like the majority, I pick my favorite fixed width, mono space font and get on with it. Every now and then I can hear about some cool new font for reading lots of software source code and technical writing, and I might give it a try, but that’s the end of it.

But sometimes, you just want to have an overview and see everything summed up in a single place, preferably an Emacs buffer so you can also play with it and hack it. Of course, your GNU/Linux, macOS, or MS Windows will happily show you all the available fonts, and let you filter out fixed width ones suitable for programming. Emacs itself can also do something very similar. But as I said, why not have something according to your taste?

With a bit of Emacs Lisp, it seems not that difficult, at least on GNU/Linux:


;; See the following for more details
;; https://emacs.stackexchange.com/a/50215/8887
;; and also see the following on a recent GNU/Linux or similar system:
;; /usr/share/doc/fontconfig/fontconfig-user.html
;; for the explanation of spacing=100
;; also see the following UNIX StackExchange answer:
;; https://unix.stackexchange.com/a/363368/13105
(defun compare-monospace-font-families ()
"Display a list of all monospace font faces. Tested on GNU/Linux."
(interactive)
(pop-to-buffer "*Monospace Fonts*")
(erase-buffer)
(dolist (font-name (seq-filter (lambda (font)
(when-let ((info (font-info font)))
(string-match-p "spacing=100" (aref info 1))))
(font-family-list)))
(insert
(propertize
(concat "1 l; 0 O o [ < = > ] " font-name ")\n")
'font-lock-face `((:family
,(format "%s" (font-get (font-spec :name font-name) :family))))))))

The result of running compare-monospace-font-families can be seen in the following screenshot:

Emacs fixed width fonts

Emacs fixed width fonts

Unfortunately, trying to do something similar for MS Windows is not that straightforward. The closest I could come is the following, and it lists some fonts that are not fixed width:


;; see also:
;; https://gist.github.com/emres/917ffc60f4fe209a6a1bad6b3abd4fa1
(defun font-is-mono-p (font-name)
(string-match "-mono-" font-name))
(defun compare-monospace-fonts ()
"Display a list of all monospace font faces. Tested on MS Windows 10."
(interactive)
(pop-to-buffer "*Monospace Fonts*")
(erase-buffer)
(dolist (font-name (x-list-fonts "*"))
(if (and (font-is-mono-p font-name)
(ignore-errors (font-spec :name font-name)))
(progn
(newline)
(insert
(propertize (concat "1 l; 0 O o [ < = > ] " font-name ")\n")
'font-lock-face `((:family ,(format "%s" (font-get (font-spec :name font-name) :family)))
(:font (font-spec :name font-name)))))))))

I hope you found this information useful. If you want to learn more about Emacs and fonts, feel free to visit the following web pages, too:

 

 
1 Comment

Posted by on May 9, 2019 in Emacs, General

 

Tags: ,

One response to “How to preview fixed width (mono spaced) fonts in an editable Emacs buffer?

Leave a comment