Save _flymake files in a temporary directory

This has been bugging me for awhile. Recent versions of Emacs save _flymake files “inplace”, meaning in the same directory as the source file. Problem is, Flymake has a tendency to crash, leaving behind the corpses of various _flymake files, which in turn cause version control annoyances, automated build issues, unusual problems with caching in web frameworks, etc.

There’s an obscure FlymakeRuby entry on the EmacsWiki with a suggested fix, repeated here:

Add this to lisp/progmodes/flymake.el in your Emacs distro, and then call it from the init method corresponding to your target language. PHP, for example:

Note that 'flymake-create-temp-inplace has been replaced with 'flymake-create-temp-intemp. Be sure to remove or recompile the corresponding flymake.elc file.

Now _flymake junk will be saved to where you have defined temporary-file-directory in .emacs. In my case:

(setq temporary-file-directory "~/.emacs.d/tmp/")

Yay. Much less annoying.

Getting Flymake to work with Emacs nXhtml

If you develop in PHP on Emacs, then you’ve probably got a hook setup to run flymake-mode on PHP files. And if so, then you’ve also probably noticed that this doesn’t work with nXhtml, which will often return an error like “mumamo can’t find the file” (or some such), causing flymake-mode to disable itself.

The culprit is likely to be a bind like this:

(add-hook 'php-mode-hook (lambda () (flymake-mode t)))

which says, “turn on flymake-mode when php-mode is started”. (Sacha Chua has a post that recommends using this from couple of years back.) The problem, as far as I can tell, is that nXhtml tries to process the fleetingly available _flymake file rather than the original PHP script whenever it toggles into php-mode.

A better hook to use is the find-file-hook, like this:

(add-hook 'find-file-hook 'flymake-mode)

With this, Flymake will properly validate PHP chunks in nXhtml mode, as well as any other files that Flymake is smart enough to process.

Defeating the flymake configuration error in Emacs php-mode

Getting the following message when trying to use Flymake with PHP?:

“Flymake: Configuration error occurred while running. Flymake will be switch OFF”

This threw me, though it probably shouldn’t have. If you’re seeing it as well, double check two settings in your php.ini file:

1. The error_reporting setting needs to include E_PARSE. I personally like to use error_reporting = E_ALL | E_STRICT. This shows everything that the PHP compiler thinks you are doing wrong.

2. Also double check your php.ini file for display_errors = On. I almost always forget about this when setting up on a new box because I tend to override php.ini with .htaccess values.

Finally, triple check your command line php settings with a quick $ php -i, which dumps the content of phpinfo() to the command line.