multi-mode

Sometimes it is desirable to use several different »major modes« in one buffer. Such a case is met when comments in formatted text are contained in a file dedicated to a programming language. Here it can turn out useful to use one mode for the program code and another for the comments.

I have used this to edit Prolog code. The code itself is treated in prolog-mode and the comments are treated in LaTeX-mode.

It is desirable to use different modes instead of overloading one mode. A lot of work has already been invested in the single modes which does not need to be duplicated.

The multi-mode »minor mode« provides a means to achieve such a feature.

The modes are described by starting sequences. I am assuming that certain tokens – the transition strings – denote the places where the switch between the modes has to be performed. To determine the mode for the current point of the buffer it is sufficient to find the previous transition strings before this point.

The desired mode is defined in a list of pairs or triples. They consist of a transition string and a mode. Optionally a function symbol can be present which is invoked to activate the requested mode. if no transition string matches.

Download

Here you can download the sources and documentation.

Code for the mode and documentation
multi-mode-el, 17 kb.