How to edit a markdown code block using org-edit-special in emacs

632 views Asked by At

I am would like to edit my R chunks from RMarkdown files the way org-edit-special does. I found generic-edit-special from jonathan leech-pepin that does a similar thing for js, css and ruby in html files. I figured I could tweak it as suggested by the author to make it work for my case but I was not able to make it work even in its original form.

I was able to run the function ges/org-edit-special from a html file with a js script block but nothing happened (no error and no new buffer). I used the Internal Script example from here as html file for this test.

Here is the init.el I made for testing:

(require 'package)
(add-to-list 'package-archives '("melpa" . "http://melpa.org/packages/"))
(setq package-list
    '(js2))

; activate all the packages
(package-initialize)
(load "~/.emacs.d/lisp/generic-edit-special")

; Org setup (telling org-mode to edit javascript with js2)
(push (cons "javascript" 'js2) org-src-lang-modes)
;; For html-mode

(require 'generic-edit-special)
(eval-after-load "sgml-mode" '(define-key sgml-mode-map [(control c) ?'] 'ges/org-edit-special))

I am pretty new to emacs and use spacemacs usually so I guess it is just me not being able to configure things correctly but I am clearly lost right now.

I also know about polymode for editing RMarkdown files but do not like to use it. I prefer the "external code buffer" approach. And I cannot use pure org-mode either as I have to collaborate with people not using Emacs.

2

There are 2 answers

0
Gallarus On BEST ANSWER

I just discovered the markdown-edit-code-block function from markdown-mode that does exactly what I want.

I set the major mode for .Rmd files to be markdown-mode and executing it inside an R chunk opens an indirect buffer with ess-r-mode as major mode. (In doom emacs, the default keybinding is , ' )

5
Rorschach On

Like you, I prefer the "external code buffer" but often need to share .Rmd or other formats. Luckily, both knitr and rmarkdown support markup in your R code buffer. See here for a (dated) intro to "spinning" your R code.

Anyway, the format is basically just markdown after ##' comments (double hash with quote) with some yaml header information as in Rmd files, eg. the following can be run as pure R code,

##' ---
##' title: "Foo"
##' output:
##'   html_document:
##'     toc: TRUE
##' author: Me
##' ---

##- r setup, include=FALSE -------------------------------------------------
library(ggplot2)
knitr::opts_chunk$set(echo = TRUE)
## /* end r setup */

##' # A header
##' a code block
##- blk1 -------------------------------------------------------------------
dat <- data.frame(x=sample(10, 10), y=runif(10))
## /* end blk1 */

##' # Another section
##' A code block w/ image
##- img,  fig.width=9, fig.height=4 ----------------------------------------
plot(y ~ x, data=dat, type='l')
## /* end sem */

##' # Next section
##' etc.

and converted into an Rmd with knitr::spin("<filename.R>", knit=FALSE), or rendered to HTML (as specified above) with rmarkdown::render("<file.R>").

Personally, I would simplify the file generation with a Makefile (make sure those are tabs), eg. to both render and create an Rmd,

foo.html: foo.R
    rscript -e "knitr::spin(\"$^\", knit=FALSE); \
    rmarkdown::render(\"$^\", \"all\")"

Alternatively, the commands could be used from Emacs to compile, etc.