I have lots of LaTeX files, and my AucTeX has been set up to automatically open up all *.tex files in LaTeX-mode. But more recently I've started using ConTeXt, and for those files (which also end in .tex) I'd like to use ConTeXt-mode. Is there any way of embedding something in the file which will tell Emacs to open up the correct mode?

I can start a file with, for example

-*- mode: context; -*-

which works well, but even if it's the very first line in the file, ConTeXt still parses it and writes it to the output, although it comes way before \starttext.

Does anybody have any better suggestions? I suppose the sensible thing would be to move all ConTeXt files into one directory tree, and set up a .dir-locals.el file in that directory, which would tell Emacs to open up all .tex files using ConTeXt mode. But I'd still like to be able to do it on a file-by-file basis.

By the way: are there really no acceptable answers for all the 8 questions you have asked up to now? If there are, please click the accept button on the best answer each (it's the checkmark like symbol below the up/down icons).
–
Paŭlo EbermannMar 19 '11 at 1:17

As an alternative to manually setting the mode file local variable (which is preferred because it does not rely on heuristics), AucTeX automatically recognizes the TeX format! It knows about LaTeX (detected via \documentclass), ConTeXt (detected via \starttext) and Plain TeX. For example, you can do the following:

Create a file abc.tex. AucTeX starts in LaTeX mode by default.

Enter \starttext.

Hit C-c C-n to force reparsing of the buffer.

AucTeX automatically detects that you plan to write a ConTeXt document and switches to ConTeXt mode.

Hit M-x add-file-local-variable RET mode RET RET to add the mode as a file-local variable. Then the file will always trigger ConTeXt mode even if the auto-recognition fails.