Adam
Adam

Reputation: 2245

Fontify R code blocks in Org-mode 8

I am trying to change the background color of R code blocks in Org-mode 8. In Org-mode 7, I was able to use:

(defface org-block-background
   '((t (:background "#dadada")))
   "Face used for the source block background.")

But the org-block-background variable seems to have disappeared in version 8...?

I tried:

(defface org-block
   '((t (:background "#dadada")))
   "Face used for the source block background.")

which works for:

#+BEGIN_SRC
#+END_SRC

and

#+BEGIN_latex
#+END_latex

But for some reason, the background color disappears, the moment I specify a language, e.g...

#+BEGIN_SRC R
#+END_SRC

I am working on a mac, running Emacs 24.3 and have upgraded org-mode to v8, using:

cd ~/.emacs.d/lisp
git clone git://orgmode.org/org-mode.git
cd org-mode
make autoloads
make
make doc

Here is the config from my init.el file:

;;;----- Startup ----------------------------;

;;; Add src directory to path
(add-to-list 'load-path "~/.emacs.d/lisp/")

;;;----- Org-Mode ---------------------------;

;;; Add upgraded org-mode to load path
(add-to-list 'load-path "~/.emacs.d/lisp/org-mode/lisp")
(add-to-list 'load-path "~/.emacs.d/lisp/org-mode/contrib/lisp" t)

;;; fontify code in code blocks
(setq org-src-fontify-natively t)

(defface org-block-begin-line
  '((t (:foreground "#666666" :background "#dadada")))
  "Face used for the line delimiting the begin of source blocks.")

(defface org-block
  '((t (:background "#dadada")))
  "Face used for the source block background.")

(defface org-block-end-line
  '((t (:foreground "#666666" :background "#dadada")))
  "Face used for the line delimiting the end of source blocks.")

(require 'org)

;;;----- ESS/R ------------------------------;

(add-to-list 'load-path "~/.emacs.d/lisp/ess/lisp/")
(load "ess-site")

;;;------ Babel ------------------------------;

;;; Support R
(org-babel-do-load-languages
  'org-babel-load-languages
  '((R . t)
    (latex . t)))

;;;----- Look & feel ----------------------------;

;;; Set default theme
(add-to-list 'custom-theme-load-path "~/.emacs.d/themes")
(load-theme 'solarized-light t)

Any ideas?

Thanks!

Upvotes: 4

Views: 1660

Answers (3)

xji
xji

Reputation: 8237

Turns out org-block-background face 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.

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

Upvotes: 3

Joe Corneli
Joe Corneli

Reputation: 662

FYI, this will not work for current versions of Org Mode (from the Git master branch), after this commit http://orgmode.org/cgit.cgi/org-mode.git/commit/?id=f8b42e8 - at least until something changes. This is announced here (July, 2014) and continues to be in effect for Org Mode users, e.g. see here (April, 2015). Anyone dedicated to restoring the old behavior locally might be able to do it from the commit's diff - I haven't tried. Future versions of Org Mode may restore the feature, possibly going about it in a different way. For now, this is about all you can get:

enter image description here

(The first SRC block above gets a background from my setting for org-block.)

Upvotes: 5

Adam
Adam

Reputation: 2245

I figured it out. Turns out I had cloned an old branch of org-mode which was missing the org-block-background variable! Deleted my org-mode folder and reinstalled using:

cd ~/.emacs.d/lisp
git clone https://github.com/Konubinix/org-mode.git
cd org-mode
make autoloads
make
make doc

Then revised my init.el to read:

;;;----- Org-Mode ---------------------------;


;;; Add upgraded org-mode to load path
(add-to-list 'load-path "~/.emacs.d/lisp/org-mode/lisp")
(add-to-list 'load-path "~/.emacs.d/lisp/org-mode/contrib/lisp" t)

;;; fontify code in code blocks
(setq org-src-fontify-natively t)

(defface org-block-begin-line
  '((t (:foreground "#666666" :background "#dadada")))
  "Face used for the line delimiting the begin of source blocks.")

(defface org-block
  '((t (:background "#dadada")))
  "Face used for the source block background.")

(defface org-block-background
  '((t (:background "#dadada")))
  "Face used for the source block background.")

(defface org-block-end-line
  '((t (:foreground "#666666" :background "#dadada")))
  "Face used for the line delimiting the end of source blocks.")

(require 'org)

And presto!

Upvotes: 3

Related Questions