tags:

views:

112

answers:

5

There's a small nuance that's been bugging me for a while, namely that I frequently type #inclued instead of #include. If it wasn't obvious, I program a lot of C and C++. That typo has wrecked countless builds and consumed time that would have been better spent drinking coffee or surfing stackoverflow. Surely emacs can be helpful and rectify my mistakes as I type (in cc-mode only, of course). But how?

Googling and searching stackoverflow didn't provide any answers.

+5  A: 

You could use this in abbrev-mode: After you entered #inclued, do C-x a i g include RET, and from then on, every time you type #inclued, it will be changed to #include automatically. If you want that abbrev to be local to a mode, use C-x a i l instead of C-x a i g. Also, you can edit your abbrevs with M-x edit-abbrevs.

danlei
That's perfect!
Staffan
A: 

Sounds like flymake is exactly what your after. It runs a compiler in the background, and will hightlight errors, as you type.

Notthinking
I'm sceptical... seems like that would waste a lot of CPU time for little bemefit, and be a nuance in itself since I would have to turn it off whenever I'm on battery power.
Staffan
I have not found it an issue myself, and arnt most compilers capable of reusing large ammounts of object code, resulting in only partial compiles?
Notthinking
A: 

I was going to suggest that this could be a slightly odd application for flyspell, but danlei's answer looks better.

phils
+1  A: 

if you're interested in something a bit different, you could write all your little piece into snippets, using the YAsnippet package, then you could type something like #in, hit TAB, and it will expand into... whatever you want.

Mica
I already use YAsnippet for various things but for includes I find that it's faster just to type it out myself.
Staffan
+2  A: 

A nicier and more global solution than abbrevs (because you can't predict all the typos you'll make) is to use flymake (which comes with emacs distribution).

http://flymake.sourceforge.net/

Flymake checks your source code behind the scene while you're still typing your code into the buffer. It highlights what's wrong with your code (that is : what gcc tells is wrong).

Running gcc in the background does not use more CPU than your antivirus bloatware. Moreover, if you have 2 or more cores, gcc can take advantage of parallelization. It only checks the syntax, not compiling anything.

Jérôme Radix
@Jéôme Radix: Someone already mentioned flymake, and as I said I'm sceptical to this since I work a lot when on battery power. I guess it's something to check out, though.
Staffan