[ESS] Editing Rpad and other html/R files

Stephen Eglen S.J.Eglen at damtp.cam.ac.uk
Fri Dec 22 10:56:42 CET 2006


Tom,
thanks for this valuable contribution -- I'd not seen rpad before.

Your use of multiple modes in one file is interesting too; over the
vacation I plan to take a look at the various options for Emacs to
switch modes in one file -- this is currently a hot topic for Sweave
documents.


Best wishes, Stephen

Tom Short writes:
 > Rpad files are html files with R interspersed for making webapps (www.rpad.org).
 > It is possible to edit Rpad files with ESS's R-mode by adding the following to
 > your .emacs file:
 > 
 > (add-to-list 'auto-mode-alist '("\\.[Rr]pad\\'" . R-mode))
 > 
 > That works fine, but it's nice to be able to have emacs adjust it's syntax
 > highlighting depending on if you are editing the R part or the html part. That
 > is possible with two-mode-mode, available at (despite its name, two-mode-mode
 > can support multiple modes):
 > 
 > http://www.hst.aau.dk/net/Report_writing/Emacs_and_editors/Config_Emacs/two-mode-mode.el
 > 
 > Once that is installed, you can add the following to your .emacs file:
 > 
 > (require 'two-mode-mode)
 > (require 'css-mode)
 > (defun Rpad-mode ()
 >   "Treat the current buffer as an Rpad file (HTML with embedded R)."
 >   (interactive)
 >   (setq default-mode (list "HTML" 'html-mode))
 >   (setq second-modes (list
 >                       (list "R" "<pre dojoType=\"Rpad\"" "</pre>" 'R-mode)
 >                       (list "Rtextarea" "<textarea dojoType=\"Rpad\""
 > "</textarea>" 'R-mode)
 >                       ))
 >   (two-mode-mode))
 > (add-to-list 'auto-mode-alist '("\\.[Rr]pad$" . Rpad-mode))
 > 
 > Here's a more advanced version that adds javascript, css, and php into the mix: 
 > 
 > (require 'two-mode-mode)
 > (require 'css-mode)
 > (defun Rpad-mode ()
 >   "Treat the current buffer as an Rpad file (HTML with embedded R)."
 >   (interactive)
 >   (setq default-mode (list "HTML" 'html-mode))
 >   (setq second-modes (list
 >                       (list "R" "<pre dojoType=\"Rpad\"" "</pre>" 'R-mode)
 >                       (list "Rtextarea" "<textarea dojoType=\"Rpad\""
 > "</textarea>" 'R-mode)
 >                       (list "Javascript" "<script" "</script>"
 > 'javascript-generic-mode)
 >                       (list "css" "<style>" "</style>" 'css-mode)
 >                       (list "php" "<?php" "?>" 'c++-mode)
 >                       ))
 >   (two-mode-mode))
 > (add-to-list 'auto-mode-alist '("\\.[Rr]pad$" . Rpad-mode))
 > 
 > Overall, the experience works relatively well. Occasionally, emacs gets mixed up
 > by the code in another section (especially by apostrophes). 
 > 
 > It is possible to adapt this approach to other mixed R/html filetypes, including
 > Sweave/html (in R2HTML) and R.rsp files. The key is adapting the regex strings
 > for finding the start and end of the code block.
 > http://www.emacswiki.org/cgi-bin/wiki/MultipleModes also has info on alternatives.
 > 
 > - Tom
 > 
 > Tom Short
 > EPRI Solutions, Inc.
 > 
 > ______________________________________________
 > ESS-help at stat.math.ethz.ch mailing list
 > https://stat.ethz.ch/mailman/listinfo/ess-help




More information about the ESS-help mailing list