monospace font in tables and source code blocks in org-mode, proportional font in other parts

Emacs term popular term use
variable pitch font proportional font usual text
fixed pitch font monospace font source code & ascii art

1 Enabling proportional font for text-mode buffers

(add-hook 'text-mode-hook 'variable-pitch-mode)

With that, every major mode, including org-mode, inheriting from text-mode will display buffers in proportional font.

2 Using monospace font for tables and code blocks, while still using variable-pitch-mode in org mode buffers

(defun my-adjoin-to-list-or-symbol (element list-or-symbol)
  (let ((list (if (not (listp list-or-symbol))
                  (list list-or-symbol)
                list-or-symbol)))
    (require 'cl-lib)
    (cl-adjoin element list)))

(eval-after-load "org"
  '(mapc
    (lambda (face)
      (set-face-attribute
       face nil
       :inherit
       (my-adjoin-to-list-or-symbol
        'fixed-pitch
        (face-attribute face :inherit))))
    (list 'org-code 'org-block 'org-table 'org-block-background)))

Now org buffers text should be displayed in proportional font, except for tables and source blocks which are displayed in monospace font.

If you see glitches in tables and source blocks, or if you find the text not anti-aliased, apply this bug fix for fixed-pitch

3 How does the code work?

It adds the face fixed-pitch to the inherit attribute of the faces: org-code, org-block, org-table, and org-block-background.

How does one find out that those four faces are the faces to target? This command

M-x describe-face

lists all faces active at point.

I did not check the other three, but when I checked org-code face, its inherit attribute was the symbol shadow and shadow is yet another face name. The above code replaces that attribute with the list:

(fixed-pitch shadow)

so that the combined effect of fixed-pitch and shadow applies to org-code face.

Does this break anything? Yes, I found that when I export as html an org file which contains a source block whose contents is itself an org buffer which contains a source block, Emacs reports error and says that (fixed-pitch shadow) is not a symbol. Probably related to htmlize.

4 Info buffers

Why not enable variable-pitch-mode on Info buffers as well, now that we know how to make parts of text to be in monospace font.

;;; display Info mode buffers in proportional font
(add-hook 'Info-mode-hook 'variable-pitch-mode)

;;; but code examples in monospace font
(defvar my-rx-info-code (rx bol "     " (* not-newline) eol))
(add-hook 'Info-mode-hook 'my-Info-font-lock)
(defun my-Info-font-lock ()
  (interactive)
  (require 'org)
  (font-lock-add-keywords
   nil
   `((,my-rx-info-code
      .
      ;; let's just use org-block
      (quote org-block)
      ))))

;;; following doesn't work. don't know why
;; (font-lock-add-keywords
;;  'Info-mode
;;  `((,my-rx-info-code
;;     .
;;     (quote org-block)
;;     )))

The regexp my-rx-info-code matches lines starting with five spaces. It intends to match emacs lisp code examples, but there are lots of false positives as well.

5 Markdown mode buffers

To display source code blocks or pre blocks in monospace:

(eval-after-load "markdown-mode"
  '(mapc
    (lambda (face)
      (set-face-attribute
       face nil
       :inherit
       (my-adjoin-to-list-or-symbol
        'fixed-pitch
        (face-attribute face :inherit))))
    (list 'markdown-pre-face 'markdown-inline-code-face)))
This entry was posted in Emacs and tagged , . Bookmark the permalink.

2 Responses to monospace font in tables and source code blocks in org-mode, proportional font in other parts

  1. Heinrich says:

    Wow, that is a great addition! Maybe ‘org-date could also be added in eval-after-load “org”, as it might also occur in tables and ruin the formatting.

  2. Pingback: how to make rainbow-delimiters-mode work with org-mode export or htmlize | Yoo Box

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s