9

I use variable-pitch-mode with org buffers. Normally I set org-block to fixed-pitch and the code between #+BEGIN_SRC and #+END_SRC are properly rendered as fixed width fonts, also with proper syntax highlighting. However starting from last week, for some reason, although the code block font still had proper syntax highlighting, the face became the same as the variable-pitch-face as normal org-mode texts. I update my packages periodically and I think an update in org-mode caused the issue but I'm not sure what happened.


Update:

Turns out this is actually related to org-block-background face, which has been removed in org version 8.3.1 in commit f8b42e8, thus the bug. Not sure if there's a solution yet. https://stackoverflow.com/questions/26290924/fontify-r-code-blocks-in-org-mode-8 https://lists.gnu.org/archive/html/emacs-orgmode/2015-08/msg00510.html

I'm currently just locking my org-mode version to the older one.


Update 2:

The newest org-mode release, version 9.0.0, apparently lets src-blocks inherit org-block face, while the org-block-background face becomes obsolete. So now just by setting org-block face you should be able to have fixed-width fonts for all codes.

7
  • Fixed in 81af689d0f214c98c9bd45f482f18329c5e84ca1.
    – rasmus
    Jun 9, 2016 at 14:23
  • @rasmus Thanks for the patch. However, this seems to work in an #BEGIN_EXAMPLE block, but doesn't work in #BEGIN_SRC language_name? Once the language should have its own syntax highlighting, the font reverts to variable-pitch in my case.
    – xji
    Jun 11, 2016 at 9:44
  • You need to use the master version -- what will eventually be Org-9.
    – rasmus
    Jun 11, 2016 at 12:29
  • @rasmus Just to confirm, in this version the face between #BEGIN_SRC lang_name and #END_SRC should be something other than default, right? Currently when I run describe-face inside of the block, I get default, which is the same font as normal text of orgmode. While between #BEGIN_EXAMPLE and #END_EXAMPLE it's org-block and for ~text~ it's org-code, for =text= it's org-verbatim, and thus those texts are displayed as fixed-pitch correctly.
    – xji
    Sep 10, 2016 at 7:41
  • @rasmus Since it's still default face beween #BEGIN_SRC lang_name and #END_SRC, it seems impossible to set it to fixed-pitch while keeping the normal content variable-pitch. I'm not sure I've correctly installed the master branch though.
    – xji
    Sep 10, 2016 at 7:42

2 Answers 2

7

Turns out this is actually related to org-block-background face, which has been removed in org version 8.3.1 in commit f8b42e8, thus the bug. The rationale seems to be

  1. it causes a bug with ps export
  2. it's inefficient

Maybe in the future there will be an alternative, but not yet.

https://stackoverflow.com/questions/26290924/fontify-r-code-blocks-in-org-mode-8

https://lists.gnu.org/archive/html/emacs-orgmode/2015-08/msg00510.html

Currently it seems the only way to get the old behavior back is to manually reverse the changes introduced in commit f8b42e8. You can see the commit here:

http://orgmode.org/cgit.cgi/org-mode.git/commit/?id=f8b42e8

diff --git a/lisp/org-faces.el b/lisp/org-faces.el
index e693dab..83453e8 100644
--- a/lisp/org-faces.el
+++ b/lisp/org-faces.el
@@ -537,9 +537,6 @@ follows a #+DATE:, #+AUTHOR: or #+EMAIL: keyword."
   :group 'org-faces
   :version "22.1")

-(defface org-block-background '((t ()))
-  "Face used for the source block background.")
-
 (org-copy-face 'org-meta-line 'org-block-begin-line
   "Face used for the line delimiting the begin of source blocks.")

diff --git a/lisp/org.el b/lisp/org.el
index a153151..7e30061 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -5930,15 +5930,7 @@ by a #."
          (cond
           ((and lang (not (string= lang "")) org-src-fontify-natively)
        (org-src-font-lock-fontify-block lang block-start block-end)
-       ;; remove old background overlays
-       (mapc (lambda (ov)
-           (if (eq (overlay-get ov 'face) 'org-block-background)
-               (delete-overlay ov)))
-             (overlays-at (/ (+ beg1 block-end) 2)))
-       ;; add a background overlay
-       (setq ovl (make-overlay beg1 block-end))
-                (overlay-put ovl 'face 'org-block-background)
-                (overlay-put ovl 'evaporate t)) ; make it go away when empty
+       (add-text-properties beg1 block-end '(src-block t)))
           (quoting
        (add-text-properties beg1 (min (point-max) (1+ end1))
                     '(face org-block))) ; end of source block
@@ -21828,9 +21820,7 @@ and end of string."
 When INSIDE is non-nil, don't consider we are within a src block
 when point is at #+BEGIN_SRC or #+END_SRC."
   (let ((case-fold-search t) ov)
-    (or (and (setq ov (overlays-at (point)))
-        (memq 'org-block-background
-          (overlay-properties (car ov))))
+    (or (and (eq (get-char-property (point) 'src-block) t))
    (and (not inside)
         (save-match-data
           (save-excursion
2
  • Thanks for digging that out -- that's a shame as I'd not really seen any of the problems myself. Aug 20, 2015 at 6:32
  • @stuarthickinbottom Yeah I guess that change only occurred after my org-mode updated to 8.3.1
    – xji
    Aug 20, 2015 at 7:57
2

I'm not sure when it changed, but I'm seeing it too.

Using C-u C-x = within such a block I can see it actually has the face org-block-background now, rather than org-block. Customising that face to inherit from fixed-pitch works for me.

Tested as follows:

(set-face-attribute 'org-block-background nil :inherit 'fixed-pitch)

If you want to preserve any existing inheritance on that face you could do the following (adapted from the original here: https://stackoverflow.com/questions/3758139/variable-pitch-for-org-mode-fixed-pitch-for-tables/16819449#16819449):

(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)))

(set-face-attribute 'org-block-background nil :inherit
            (my-adjoin-to-list-or-symbol
             'fixed-pitch
             (face-attribute 'org-block-background :inherit)))
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.