EMACS folding/unfolding R code
Asked Answered
T

2

16

Is there an Emacs minor-mode (or piece of elisp code) that lets you selectively hide/show environments while in Sweave (Sweave = R + LaTeX)?

For instance, I would like to move to the beginning of a code block (<<>>), hit a keystroke, and have the contents of the environment hidden from view.

Is this possible? I just tried hs-minor-mode, allout-mode, and outline-minor-mode, but most of them don't recognize R environments.

I also tryed org-mode that works great for folding/unfolding but, do not support the LaTeX highlighting code for expression like: \cite{}; \ref{}; \ce{} ...

Best Riccardo

--EDIT--

I tried for some day to use emacs-folding-mode but, because I work on a very long code (more than 2000 rows), folding mode "goes crazy" and for example misunderstand the mining of some special character (i.e. $), that has very different use both in R than LaTeX. I think the problem is intrinsic to Sweave, because in the same buffer I have R code and LaTeX code together.

So, now I'm testing emacs outline minor mode. But when I move through R from LaTeX (and vice versa) all the outlined part were unfolded despite I write in my .emacs:

(defun turn-on-outline-minor-mode ()
 (outline-minor-mode 1))
  (add-hook 'ess-mode-hook 'turn-on-outline-minor-mode)
  (add-hook 'LaTeX-mode-hook 'turn-on-outline-minor-mode)
  (add-hook 'latex-mode-hook 'turn-on-outline-minor-mode)
(setq outline-minor-mode-prefix "\C-c\C-o")

Do you have any suggestions??

Regards

--EDIT 2--

It seems to work:

(load "folding" 'nomessage 'noerror)
(folding-mode-add-find-file-hook)
(add-hook 'LaTeX-mode-hook 'folding-mode) 
(add-hook 'ess-mode-hook 'folding-mode) 
(folding-add-to-marks-list 'ess-mode "#{{{ " "#}}}" " ")

I don't know if is right that, when you leave the chunk, it is automatically unfolded.

Thorsten answered 19/2, 2012 at 22:57 Comment(2)
it's the implementation of Rnw mode which gives you the trouble. I've also tried hideshow. But because Rnw resets the mode in the chunks you leave/enter, everything is getting reset. There are some talks in ESS to rewrite the Rnw, based on a better noweb.el.Platform
Hi, I partially solved the problem using the folding mode. I re-re-edited the post with mine solution.Thorsten
I
1

There's a generic folding mode here: http://www.emacswiki.org/emacs/FoldingMode

Inion answered 19/2, 2012 at 23:9 Comment(11)
What's the "some trouble" you're having?Inion
Hi, I tried th emacs folding mode and I have some trouble. I add to my .emacs this:(load "folding" 'nomessage 'noerror) (folding-mode-add-find-file-hook) (folding-add-to-marks-list 'Rnw-mode "#{{{" "#}}}" nil t) but when I open a .Rnw file emacs do not detect the folding marks; moreover, if I foldify a region (C-c @ C-x), emacs use the folding character of LaTeX instead of "#{{{ #}}}". Any suggestion??Thorsten
If the issue is that the folds aren't using the comment syntax of the language you are using, try following the instructions for Ruby, PHP and Prolog that are here: emacswiki.org/emacs/FoldingMode#toc4 and just modify for R.Inion
It's what I've done with this code (folding-add-to-marks-list 'Rnw-mode "#{{{" "#}}}" nil t) but emacs do not detect the folding marks.Thorsten
And Rnw-mode is a real emacs mode?Inion
No, and this is the problem...I think. In the .emacs file I define Rnw-mode:(defun Rnw-mode () (require 'ess-noweb) (noweb-mode) (if (fboundp 'R-mode) (setq noweb-default-code-mode 'R-mode))). To invoke the R chunk within the .Rnw file I type <<>> my R code @. Thorsten
let us continue this discussion in chatThorsten
For anyone who had the same problem the right sintax is: (add-hook 'LaTeX-mode-hook 'folding-mode).Thorsten
@Thorsten - note that I put on a bounty; if you have something that works, please provide it as an answer so that I can get this working.Reliance
Read the --EDIT 2-- of my question. This is the solution I used and it works good for me.Thorsten
You have to provide your answer as an "answer", for the bounty to be granted.Hypocrisy
M
1

I have had very good results with the hideshow hs-minor-mode, these are the lines I basically use in my ~/.emacs.d/init.el:

(add-hook 'ess-mode-hook 'hs-minor-mode)

(eval-after-load 'hideshow
 '(progn
   (global-set-key (kbd "C-+") 'hs-toggle-hiding)))
Mulderig answered 29/5, 2017 at 14:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.