I used to use delete-trailing-whitespace in my local-file-write-hooks for any source file I edited, but I had to disable it a while back. Many files containing legacy source were extremely messy in this regard, and it made making simple fixes harder to track, since there was a large amount of noise in the diff from the whitespace changes.
But I just solved this. Here at Digg, we document all new code with phpDocumentor. Leveraging this, I wrote a new function to examine the header of the source file and strip whitespace if I have an @author tag.
(defun maybe-delete-trailing-whitespace ()
"Delete trailing whitespace if I am the author of this file."
(interactive)
(and (delete-trailing-whitespacep) (delete-trailing-whitespace)))
(defun delete-trailing-whitespacep ()
"Should we delete trailing whitespace when saving this file?"
(save-excursion
(goto-char (point-min))
(next-line 25)
(let ((pos (point)))
(goto-char (point-min))
(re-search-forward (concat "@author +" user-full-name) pos t))))
(defun progmodes-write-hooks ()
"Hooks which run on file write for programming modes"
(prog1 nil
(set-buffer-file-coding-system 'utf-8-unix)
(untabify-buffer)
(copyright-update)
(maybe-delete-trailing-whitespace)))
(defun progmodes-hooks ()
"Hooks for all programming modes"
(add-hook 'local-write-file-hooks 'progmodes-write-hooks))
(add-hook 'php-mode-hook 'progmodes-hooks)
(add-hook 'js2-mode-hook 'progmodes-hooks)
;; Repeat for any other mode for editing source
It works a treat, happily stripping trailing whitespace if I’m listed as an author in the header, and leaving it unmolested otherwise. I’m not entirely satisfied with delete-trailing-whitespacep, which feels a little graceless. Maybe someone knows a better way to do it?
Discussion