August 30, 2011

Realtime syntax checking with emacs

_ Note: this post is a web cache recover of a November 2010 post that got lost with the blog crash. I updated it to add C/C++ autocompletion. _

One of the nice features of fat IDEs is that you get real time syntax checking. Some languages make it easy, some not.

For example Eclipse has access to the compiler as a service inside the IDE, and it checks the code as you type, even suggesting fixes.

I started to research what could be done on the emacs side to get a better experience, as when I am coding, I am usually thinking at the same time I write, and this means I make more syntax errors than the average guy.

So I found flymake, which is included by default in the emacs package. Copy pasting some snippets from the emacs wiki I got really nice real time checking for ruby:

Going forward with python was easy. A flymake extension that uses pyflakes (a python checking program) was already available.

It was with C/C++ where things started to get more complicated. Flymake has support for those hardcoded so that make with a special target is invoked. I wanted to check the current file only. Also gcc syntax errors are not that good.

So I started to modify the extensions I had seen to use clang (and clang++) from the LLVM project. Once it worked, I got nice error messages on C/C++ files:

I was so happy to have this working on C++ that I needed more to challenge my new acquired mastery. So I decided to try it with ycp. Not that I code with it that often, but I have colleagues who do. After adapting the extensions, here is the result:

You can find all the required files on my emacs setup repository. Follow custom.el which goes to custom-$lang.el to site-lisp/flymake-$lang.el.

Update 20.11.2010:

Thanks to this post I managed to get also Rails .erb templates working. Those are more tricky as they can’t be parsed directly by ruby, but they have to go first through erb -x and then through ruby -c. I ported the script to the style of loading (init and load functions) I was already using.

Update 30.08.2011:

Thanks to this autocomplete extension, I was able to setup autocompletion using clang for C/C++. Here is how it looks like:

Next! ruby autocompletion.