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.

Interesting minor modes discovered in the Emacs guided tour

I was looking for a good introduction to Emacs for some friends and stumbled upon the truly excellent Guided Tour of Emacs on gnu.org.  And unsurprisingly it contained a few useful minor modes of which I had never heard.

icomplete-mode

icomplete-mode shows completions in the minibuffer as you type.  If you’re too impatient to hit tab, then this is the minor mode for you.

iswitchb-mode

This global minor mode solves an inconvenience that had always bothered me.  Typically, to see a list of buffers without resorting to the mouse, one has to C-x b TAB to see the list of buffers, or C-x C-b and then toggle over to the other window (Ctl-o) to use dired to select a buffer. Too many keystrokes.

With iswitchb-mode turned on, C-x b shows a list of avaialble buffers in the mini buffer, narrowing them down as you type.  A much easier way to jump between dozens (hundreds?) of open files.

Update: Looks like iswitchb-mode has been replaced with the far superior ido-mode as of Emacs 22.

Emacs Japanese Input

If you ever find yourself stuck relying on the IME built-into Emacs (rather than the IME native to your OS), here are the essential key strokes you’ll need to know.

  • C-\:  toggle input mode (ime on or off)
  • C-h:  shows conversion options if used when in conversion mode
  • SPC: transform input to kanji, or show the next conversion candidate
  • DEL:  abort conversion
  • C-n: show the next conversion candidate
  • C-p: show the previous conversion candidate
  • C-o: lengthen conversion bunsetsu
  • C-i, TAB: shorten conversion bunsetsu
  • Shift-k:  toggle between hiragana and katakana
  • qq:  toggle between alphabet and kanji modes
  • qz:  turn on zenkaku alphabet mode
  • qh:  turn off zenkaku alphabet mode

Also useful:

  • C-x RET l:  set the buffer language environment
  • C-x RET f: select the encoding
  • C-x RET C-\:  select the input method

Remote PHP Debugging with Emacs

It’s an Emacs weekend here at blog.arithm.

I was recently asked if it was possible to do remote PHP debugging with Emacs. We’re talking about Emacs, so the answer is:  Of course!  How to do it:

  1. Grab Tohru Fujinaka’s GEBEN library.  It’s an aging alpha release, but works fairly well.
  2. Make sure you also have CEDET installed.  I’m using the 1.0 pre4 release.
  3. Drop debugclient somewhere in your path.  (Here Emacs is running on Windows, so I just threw it into the C:\WINDOWS\ directory.)  Make sure the executable is named “debugclient” and not “debugclient-0.9.0” or whatever.
  4. If you’re using the standard xdebug port 9,000 (recommended), make sure that port is open on your firewall.  For most folks these days that means opening up the default OS firewall as well as forwarding a port from the router.  See the exceptional portforward.com for more information on router port forwarding specifics.

Now, assuming you already have xdebug plugged into PHP, modify php.ini or an .htaccess file with the following settings:

  • xdebug.remote_enable = true
  • xdebug.remote_handler = dbgp
  • xdebug.remote_host = your_client_ip

For security reasons it’s wiser to do this via .htaccess on directories that permit only authenticated access.

To debug, ask Emacs to listen for connections by going into GEBEN Mode with Meta-x geben. You should see “xdebug started.” in the status row at the bottom of the Emacs window.

Run your PHP script with the xdebug switch on, eg.

http://www.example.com/script.php?XDEBUG_SESSION_START

If everything is setup correctly, Emacs will load the source of script.php (be wary of security!) and allow you to step through the code.

Available commands are:

spc step into/step over
i   step into
o   step over
r   step out
b   set a breakpoint at a line
u   unset a breakpoint at a line
g   run
q   stop

Some gotchas: I’ve noticed the GEBEN expects the first keystroke to be “space”; often it will freeze otherwise.  GEBEN may also run into problems on certain session-related PHP commands.

Happy debugging!

Set Emacs’ HOME directory in Vista

Rather than allow Emacs to assume HOME is c:\ (which will cause Vista to winge every time you want to modify your .emacs file), try the following:

setx HOME "%USERPROFILE%"

This will make home something like c:\Users\username.

Now you can keep your .emacs in a far more rational place and not have to wake up the Vista security troll whenever you want to do some tweaking.

Undo in Emacs

There are a variety of ways to “undo” something in Emacs.  Unfortunately some of the keystrokes work in some environments, while others do not.  Here’s a quick rundown:

  1. Meta-x undo — Will undo only the most recent changes.  Cannot be used multiple times.
  2. Ctrl-x u — Undo.  Consecutive repetitions will undo earlier and earlier changes.
  3. Ctrl-_ or Ctrl-/ — Same as Ctrl-x u, however does not work in some envrionments.
  4. Ctrl-Shift-Minus — Same as Ctrl-x u, however does not work in some environments.

For whatever reason I can never remember the Ctrl-Shift-Minus sequence when I find myself in an environment where Ctrl-_ and Ctrl-/ don’t work.

Keep in mind that Emacs undo is a bit different from traditional undo:  Undo actions themselves are recorded as actions.  This can be confusing.

In some environments, Ctrl-x Ctrl-_ will reverse the order of undo.  I avoid this out of fear that it will cause the universe to implode.

See the Emacs info node for more good undo lore, such as how to use “selective undo” which restricts undo actions to changes performed in the current active region.  (Very useful.)