112

Is there any modification of an existing tool or new tool that will syntax-highlight (colour) blocks of R code in a knitr markdown document when opened in Emacs?

The knitr chunks I am looking at are triple-backtick blocks [```] that contain R code inside.

EDIT: things that I tried so far:

- Ubuntu 12.04 with Emacs 23

emacs --version
GNU Emacs 23.3.1

1. Polymode

https://github.com/vitoshka/polymode/issues/3

I am trying to get polymode to syntax-highlight my Rmd files, but it's complaining about a 'color' load file:

cd ~/.emacs.d
git clone https://github.com/vitoshka/polymode.git

In my .emacs file:

(add-to-list 'load-path "/home/avilella/.emacs.d/polymode/")
(add-to-list 'load-path "/home/avilella/.emacs.d/polymode/modes/")

;; Require any polymode bundles that you are interested in:

(require 'poly-R)
(require 'poly-markdown)

eval-buffer:

Cannot open load file: color

- CentOS with Emacs 24:

emacs --version
GNU Emacs 24.2.1

1. Polymode installed correctly

Open a Rmd file, M-x polymode-minor-mode, no change to the syntax highlighting.


emacs --version
GNU Emacs 24.3.1

1. Polymode installed correctly

Open a Rmd file, M-x poly-markdown+r-mode, some of the syntax highlighting starts to appear, but I need to manually modify the test in each triple-tick block of code for it to fully show the syntax.

Enter image description here

Enter image description here

The second image is just after typing a Return before the knitr block.

Josh Davis
  • 26,422
  • 5
  • 47
  • 66
719016
  • 8,262
  • 17
  • 72
  • 140
  • 10
    i'd love to know the answers. There are persistent rumours that the ESS guys will get around to this, but I haven't seen it yet ... – Ben Bolker May 15 '13 at 14:17
  • 3
    I have something at home that basically works. It is based on mumamo-mode and markdown-mode. I had to make a couple changes to mumamo-mode to get it to work though, explained here: https://bugs.launchpad.net/nxhtml/+bug/626039/comments/5 . I'll follow-up here with an answer after I get home and remember what I did. – Erik Iverson May 15 '13 at 19:15
  • 4
    What about Vitalie's `polymode`: https://github.com/vitoshka/polymode – daroczig May 15 '13 at 19:25
  • 3
    @daroczig -- Interesting. Any idea what he means by "No literate programming backends", in the README's **Warning!** section? – Josh O'Brien May 15 '13 at 19:52
  • 1
    @JoshO'Brien: not sure but just check out the [last image](https://github.com/vitoshka/polymode/blob/master/img/Rmd.png) there :) I've read about `polymode` a few weeks ago on the [ESS list](https://stat.ethz.ch/pipermail/ess-help/2013-March/008755.html) where you might get more information on the topic. – daroczig May 15 '13 at 20:13
  • 4
    @JoshO'Brien On Vitalie's ess-help announcement, he clarifies what he meant: "Rnw is just a polymode. What I mean by literate programming backend is a tool that can process/convert the file into something else. Like knitr or org-mode. I will hook these days ess-swv-weave and ess-swv-PDF in R related polymode maps. That should be quite ok for time being as knitr is smart enough to recognize the stuff it is dealing with." – Erik Iverson May 15 '13 at 20:44
  • 1
    @daroczig Awesome, thanks for pointing polymode out. I knew unsubscribing from ess-help was not a smart choice! :) – Erik Iverson May 15 '13 at 20:46
  • 1
    @ErikIverson -- Thanks. Yes, I see that [Stephen Eglen was also confused by that warning](https://stat.ethz.ch/pipermail/ess-help/2013-March/008764.html). – Josh O'Brien May 15 '13 at 21:41
  • @OP I was able to replicate the problem on Ubuntu 13.04 but when I tried on my equivalent set-up on Windows the highlighting works just fine with M-x poly-markdown+r-mode – mcheema Jul 03 '13 at 19:14
  • @OP are you using the ess from ftp.debian.org ess_13.05-3_all.deb? This is the main difference between my set up on Ubuntu and on windows. The windows ESS comes with Vincent Goulet's distribution of emacs and ess. Maybe the Ubuntu version is slightly older build. – mcheema Jul 03 '13 at 19:24
  • 1
    Org-mode has a very similar feature: it identifies code blocks inside an org file and colors them perfectly. If you delve into their code you might be able to isolate and port the code responsible for this. Beware tough, org mode's source is usually a gigantic labyrinth of code. – Malabarba Jul 03 '13 at 21:28
  • 1
    I think you should upgrade to emacs24, for which there is a ppa on ubuntu. If you really want this I guess I could write it for you if you ask nicely. – nic ferrier Aug 05 '13 at 23:15
  • @nicferrier: I use emacs 24 at work and would like to be able to have knitr markdown fully highlighted. What is the best option now? – 719016 Aug 08 '13 at 07:50
  • @200508519211022689616937: My answer should apply for the first two of your instances. For the last please, say what you read in Emacs Mode Line on the (major minor) side, before and after hitting return. – antonio Dec 20 '14 at 00:41

2 Answers2

3

This might help: http://sjp.co.nz/posts/emacs-ess-knitr/

Knitr markdown in Emacs through ESS

priyolahiri
  • 588
  • 1
  • 3
  • 10
  • Sorry but this reference is wrong. Currently Emacs-ESS has no *highlighting* for Rmd documents or even simple markdown (and by the way your linked article doesn't claim so). As for the former you need polymode and for the latter markdown.el. Besides, if all you want is to just switch the ESS engine from Sweave to knitr (without highlighting!), there is no need to go through all the complication of this article, just add `(setq ess-swv-processor 'knitr)` in you init file. – antonio Dec 20 '14 at 00:20
2

Your problem

First of all, you say you have GNU Emacs 23.3.1, but in polymode readme.md, it reads:

Tested with Emacs 24.3.1 and 24.4.5.

As for your error: "Cannot open load file: color", in polymode.el, there is the line:

(require 'color)

this package is in Emacs 24, but it might well miss in your version.

Solution

  1. Upgrade to a recent (therefore supported) version of Emacs.

  2. Extract the polymode.zip in a directory where you keep Emacs material, e.g.:

    ~\conf\emacs
    

and change the resulting polymode-master dir to polymode

  1. Add this in your init file (and if you used different names above, change names below accordingly):

    ;; Just an Emacs personal dir containing polymode packages etc.
    (setq MY-EMACS  "~/conf/emacs") 
    
    (defun my-emacs  (subfolder)
    "Get path to personal dir + subfolder"
    (concat (expand-file-name MY-EMACS) "/" subfolder))
    
    ;; ESS Markdown
    ;; -------------
    (defun rmd-mode ()
      "ESS Markdown mode for rmd files"
      (interactive)
      (setq load-path 
        (append (list (my-emacs "polymode/") 
                  (my-emacs "polymode/modes/"))
            load-path))
      (require 'poly-R)
      (require 'poly-markdown)
      (poly-markdown+r-mode))
    
    ;; Wrap line in markdown. Comment if you don't dislike words cut in the middle
    (add-hook 'markdown-mode-hook (lambda () (visual-line-mode 1)))
    
    ;; Let you use markdown buffer easily
    (setq ess-nuke-trailing-whitespace-p nil)  
    
  2. Meta+rmd when the Rmd-file is open or set the mode in <!-- Local Variables: -->
    ... enjoy a dramatic change in your productivity.

PS
Do not overlook last elisp line. It will allow to save markdown properly. Otherwise, when you reopen your doc, you'll have unpleasant surprises.

A note to Windows users

Skip this if you are a Linux guy, but, despite the question mention Ubuntu, my answer applies perfectly to Windows Emacs too. Just with respect to:

  • Step 2) You will extract polymode.zip in:

    %USERPROFILE%\conf\emacs
    

    As you guess, the environment variable %USERPROFILE% contains the path to your user profile directory. If you are not sure what it is, execute set USERPROFILE in the CLI prompt.
    As a Windows user you might be used to store things in My Documents. In that case your dir will be:

    %USERPROFILE%\Documents\conf\emacs
    
  • Step 3) If in Step 2) you used the path %USERPROFILE%\Documents\conf\emacs, change accordingly the first code line from:

    (setq MY-EMACS  "~/conf/emacs") 
    

    to (note the slashes):

    (setq MY-EMACS  "~/Documents/conf/emacs") 
    

    In both cases (with and without "My Documents"), you might prefer the more Windows style:

    (setq MY-EMACS (substitute-in-file-name "$USERPROFILE/Documents/conf/emacs")) 
    
antonio
  • 9,285
  • 10
  • 59
  • 113