views:

54

answers:

2

Emacs has poor handling of auto-indentation in Flex and Bison. In fact, it seems to have no support for flex mode. So, how does an emacs user cope with these? I like VIm but I would prefer not to switch because I am much faster and more comfortable in Emacs.

I had a third party elisp module for Bison a few months ago but when its indentation broke, it would never get fixed. In short, it was a bad hack.

Or is there a way I can turn off auto indentation for .l and .y files (so pressing would do one indent)? How would I also change this elisp setting for just emacs?

A nice and concise guide for elisp would be very helpful too. I wouldn't mind spending a few days to write my own flex and bison modes if I had the right documentation.

+1  A: 

Emacs chooses the major mode mainly based on the file name extension. .l is a contended extension: some people use it for lex, others for lisp (and there are a few other rarer uses). Emacs associates .l with lisp, and .lex with lex (for which it uses C mode).

If the .l files you work with are more often lex than lisp, you can change what .l files are associated with the following line in your .emacs:

(add-to-list 'auto-mode-alist '("\\.l\\'" . c-mode))

You can also declare inside a file what mode you want Emacs to use when it opens the file. Put the following snippet on the first line of the file (typically in a comment):

-*-mode: c-mode-*-

This is a more general feature, offering other syntaxes and other possibilities; see “File Variables” in the Emacs manual for more information.


If you would like to get started with Emacs Lisp, read the Emacs Lisp intro (which may be included in your Emacs or OS distribution). Once you've played around with the basics of the language a bit, you can turn to the chapter on modes in the Emacs Lisp reference manual.

Gilles
I'll try this on my Emacs setup at home... I'm not entirely sure, but I thought Flex/Lex had issues with C mode (especially in the rules section). For example, if the first character of a line is not whitespace, Flex interprets it to be a pattern to match. With C mode, if I wanted to put some code corresponding to a rule, I would be unable to have it off to the right unless I force an indent, which is tedious. Any simple solutions or ideas for what to do here?
Kizaru
@PlaZmaZ: I don't think you can solve this without writing a bit of code. A lex or yacc mode would be derived from C mode (look up “Derived Modes“ in the Emacs Lisp manual). CC mode has a complex indentation engine, which I've never looked into; I think the “Advanced Customizations” section will interest you. For syntax coloring, look up “Font Lock mode”.
Gilles
+1  A: 

Additional tip: You might decide that what you want is Emacs' generic behavior -- what it uses when it doesn't have any special mode for a file format. That's called Fundamental mode in emacs lingo: so you can request it on the fly with M-x fundamental-mode, or put -*- mode: fundamental -*- on the first line of the file, or customize auto-mode-alist like so:

(add-to-list 'auto-mode-alist '("\\.l\\'" . fundamental-mode))

Another thing to try might be indented-text-mode (probably with auto-fill disabled).

Zack