remacsr-markdownorg-modeknitr-spin

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


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.


Solution

  • 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 , ' )