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.

Riccardo
  • 1,885
  • 3
  • 15
  • 22
  • 1
    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](http://www.loveshack.ukfsn.org/emacs/noweb.el). – VitoshKa Mar 09 '12 at 13:28
  • Hi, I partially solved the problem using the folding mode. I re-re-edited the post with mine solution. – Riccardo Mar 16 '12 at 10:21

2 Answers2

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)))
quazgar
  • 4,304
  • 2
  • 29
  • 41
1

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

snim2
  • 4,004
  • 27
  • 44
  • What's the "some trouble" you're having? – snim2 Feb 20 '12 at 11:37
  • 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?? – Riccardo Feb 20 '12 at 11:39
  • 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: http://www.emacswiki.org/emacs/FoldingMode#toc4 and just modify for R. – snim2 Feb 20 '12 at 12:57
  • 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. – Riccardo Feb 20 '12 at 13:29
  • And `Rnw-mode` is a real emacs mode? – snim2 Feb 20 '12 at 13:37
  • 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 @`. ` – Riccardo Feb 20 '12 at 13:40
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/7962/discussion-between-riccardo-and-snim2) – Riccardo Feb 20 '12 at 14:24
  • 3
    For anyone who had the same problem the right sintax is: (add-hook 'LaTeX-mode-hook 'folding-mode). – Riccardo Feb 20 '12 at 16:22
  • @Riccardo - 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. – Abe Jan 15 '13 at 19:21
  • Read the --EDIT 2-- of my question. This is the solution I used and it works good for me. – Riccardo Jan 15 '13 at 21:40
  • You have to provide your answer as an "answer", for the bounty to be granted. – Dmitry Jan 19 '13 at 23:35