July 31, 2008

Bulk view

Emacs and PHP: On-the-fly syntax checking with Flymake

The dreaded white screen of nothingness usually means that I’ve misplaced a quotation mark or brace somewhere in my PHP code. On-the-fly syntax checking in Eclipse helped me find those errors quickly because I could scan the right scrollbar for areas marked with red. I knew that shifting to Emacs wouldn’t automatically cure me of the propensity to mismatch my parentheses. If I could get on-the-fly syntax checking working in Emacs, I’d save myself a lot of time.

It took me a while to sort out various configuration problems. Most were due to the fact that PHP didn’t report parse errors with my original configuration, even though I had uncommented the line after “Show only errors”. As it turned out, the error_reporting option in php.ini needs to include E_PARSE in order for php to report parsing error details. Here’s the value I’m currently using in /etc/php.ini:


Flymake is the package responsible for on-the-fly syntax checking in Emacs. Out of the box, it supports C, C++, XML, XHTML, Perl, Java, TeX, and IDL. To load Flymake and add support for PHP, add the following to your ~/.emacs:

(require 'flymake)

(defun flymake-php-init ()
  "Use php to check the syntax of the current file."
  (let* ((temp (flymake-init-create-temp-buffer-copy 'flymake-create-temp-inplace))
	 (local (file-relative-name temp (file-name-directory buffer-file-name))))
    (list "php" (list "-f" local "-l"))))

(add-to-list 'flymake-err-line-patterns 
  '("\\(Parse\\|Fatal\\) error: +\\(.*?\\) in \\(.*?\\) on line \\([0-9]+\\)$" 3 4 nil 2))

(add-to-list 'flymake-allowed-file-name-masks '("\\.php$" flymake-php-init))

;; Drupal-type extensions
(add-to-list 'flymake-allowed-file-name-masks '("\\.module$" flymake-php-init))
(add-to-list 'flymake-allowed-file-name-masks '("\\.install$" flymake-php-init))
(add-to-list 'flymake-allowed-file-name-masks '("\\.inc$" flymake-php-init))
(add-to-list 'flymake-allowed-file-name-masks '("\\.engine$" flymake-php-init))

(add-hook 'php-mode-hook (lambda () (flymake-mode 1)))
(define-key php-mode-map '[M-S-up] 'flymake-goto-prev-error)
(define-key php-mode-map '[M-S-down] 'flymake-goto-next-error)

Evaluate that code, open one of your PHP files, and intentionally break it. The syntax error should be highlighted. To change the highlighting, move your point to the error and type M-x customize-face. Accept the default (flymake-errline), then customize it as desired. Don’t forget to save your customizations for future sessions.

If your syntax errors are not highlighted and you get a message like this:

Configuration error occured while running (php -f test_flymake.php -l). flymake will be switched OFF

double-check your /etc/php.ini to make sure that E_PARSE is included as one of the options for error_reporting. You can check the output by running php -f yourfile.php -l. You should see the line number of the parse error. Make sure that this matches the regular expression added to flymake-err-line-patterns in your ~/.emacs. If your PHP returns a slightly different message, modify your flymake-err-line-patterns accordingly.

Flymake can save you a lot of programmer frustration for the cost of a little CPU time. Use it to check for errors before you save files or commit them to your source code control repository, and you and other developers will be much happier.

(UPDATE: Fixed typo in flymake-php-init)

Kaizen: Moving time around

I recently (re)discovered that writing is much easier and more enjoyable in the early morning when I’m fresh and focused than late at night when I’m thinking more about what I’m doing at work. To take advantage of this, I’ve been slowly moving my waking time earlier and earlier. Yesterday, I went to bed at 9 after tidying up and preparing for the next day. Today, I got up at 5:15. It was fifteen minutes later than I’d set the alarm clock for, but I realized that the dream I was dreaming wasn’t all that interesting compared to what I might learn if I started writing.

Moving tasks to the night before can support this early-morning writing by freeing up more time. The more I can do the night before, the less I need to do the morning after – and it pays off even when the exchange isn’t 1:1. For example, moving the half-hour I used to spend cooking steel-cut oats for breakfast from morning to evening means that I spend just a few minutes heating up a breakfast with much more texture than instant oatmeal. I packed my lunch last night, so I just need to grab it from the fridge and head out the door. I checked my purse for my keys and badge, too. Doing these little things the night before helps me streamline my morning routine.

What else can I do to free up time? I might try watering the plants in the late afternoon or try outlining in the evenings. OpenLoops has good tips for making the most of these early morning hours, and I’m sure I’ll discover more along the way.

I’d also like to look into freeing up weekend time. I used to save laundry loads and library runs for the weekend, but if I can use my weekday evenings to take care of these things, then that frees up a larger block of uninterrupted time.