views:

135

answers:

2

In Emacs, I'm working with a file that is a hybrid of two languages.

Question 1: Is there a simple way to write a major mode file that combines two major modes?

Details:

  • The language is called "brew" (not the "BREW" of "Binary Runtime Environment for Wireless").

  • brew is made up of the languages R and Latex, whose modes are R-mode and latex-mode.

  • The R code appears between the tags <% and %>. Everything else is Latex.

  • How can I write a brew-mode.el file? (Or is one already available?)

One idea, which I got from this posting, is to use Latex mode, and treat the code of the form <% ... %> as a comment.

Question 2: How do you change the .emacs file or the latex.el file to have Latex mode treat code of the form <% ... %> as a comment?

+4  A: 

A number of folks have written solutions enabling you to use multiple major modes at once. See the Emacs Wiki for Multiple Modes. I personally have no experience with them and cannot recommend one over another.

Trey Jackson
I've worked with MuMaMo in nxhtml and it works pretty well most of the time.
Bozhidar Batsov
@Trey: thank you for the link. I changed the two-mode-mode.el file there, and got it to work. But in Latex mode (not R mode), the minibuffer was constantly printing messages. Possibly Emacs was in an infinite loop. I used Aquamacs (Emacs) 1.9 on Mac OS 10.6.3. Your experience with two-mode-mode might differ. @Bozhidar: Thank you for the information.
Winston C. Yang
A: 

To answer your second question you need to use the font-lock-add-keyword function; give it a major mode to add keywords too, and a association list of regex / face:

(font-lock-add-keywords 'latex-mode
   '(("\<\%.*\%\>" . font-lock-comment-face)))

More details here on emacswiki: http://www.emacswiki.org/emacs/AddKeywords

snim2