Skip to content

Instantly share code, notes, and snippets.

@haxney
Last active February 19, 2023 18:45
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save haxney/3055728 to your computer and use it in GitHub Desktop.
Save haxney/3055728 to your computer and use it in GitHub Desktop.
List monospace fonts in Emacs
;; Display all the monospace fonts available to Emacs in a dedicated buffer
(defun font-is-mono-p (font-family)
;; with-selected-window
(let ((wind (selected-window))
m-width l-width)
(with-temp-buffer
(set-window-buffer (selected-window) (current-buffer))
(text-scale-set 4)
(insert (propertize "l l l l l" 'face `((:family ,font-family))))
(goto-char (line-end-position))
(setq l-width (car (posn-x-y (posn-at-point))))
(newline)
(forward-line)
(insert (propertize "m m m m m" 'face `((:family ,font-family) italic)))
(goto-char (line-end-position))
(setq m-width (car (posn-x-y (posn-at-point))))
(eq l-width m-width))))
(defun compare-monospace-fonts ()
"Display a list of all monospace font faces."
(interactive)
(pop-to-buffer "*Monospace Fonts*")
(erase-buffer)
(dolist (font-family (font-family-list))
(when (font-is-mono-p font-family)
(let ((str font-family))
(newline)
(insert
(propertize (concat "The quick brown fox jumps over the lazy dog 1 l; 0 O o ("
font-family ")\n") 'face `((:family ,font-family)))
(propertize (concat "The quick brown fox jumps over the lazy dog 1 l; 0 O o ("
font-family ")\n") 'face `((:family ,font-family) italic)))))))
@wiedzmin
Copy link

s/with-current-buffer "asdf"/with-current-buffer "Monospace Fonts"

@ryanpeach
Copy link

Actually:
%s/with-current-buffer "asdf"/with-current-buffer "*Monospace Fonts*"/g

@kanreki
Copy link

kanreki commented Nov 15, 2022

curse you, Markdown! ;-)

@haxney
Copy link
Author

haxney commented Nov 20, 2022

Fixed! Only 11 years later. The with-current-buffer should really be with-temp-buffer. The buffer only exists to test the width of characters, so there's no need to keep it around.

@kanreki
Copy link

kanreki commented Nov 20, 2022

That makes a lot more sense!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment