(Redirected from EmacsTidy/Comment/2005-09-22)

Combine HTML Tidy with Emacs

I write quite a bit of HTML in emacs, and I often have to clean up html that others have written. The best tool for this is HTML tidy.

Ken Bowen pointed me towards a bit of code which I reworked in to this emacs function, tidy-buffer():

 ; Function to run Tidy HTML parser on buffer
 ; NOTE: this requires external Tidy program
 (defun tidy-buffer ()
  "Run Tidy HTML parser on current buffer."
  (if (get-buffer "tidy-errs") (kill-buffer "tidy-errs"))
  (shell-command-on-region (point-min) (point-max)
    "tidy -f /tmp/tidy-errs -q -i -wrap 72 -c" t)
  (find-file-other-window "/tmp/tidy-errs")
  (other-window 1)
  (delete-file "/tmp/tidy-errs")
  (message "buffer tidy'ed")
 (global-set-key (kbd "C-x t") 'tidy-buffer)

This function basically runs the current buffer through tidy (obviously you have to have that installed first), and then pops up a new buffer with the errors from that process. I bind it to C-xt.

This function could be improved my making it work like make does, that is selecting a particular error in the error buffer could move you to that location in your html file.

This will only work in XEmacs right now, someone should write the small fix to the call the shell-command-on-region to make it work in GNU Emacs as well.

Just stick this in to your .emacs to try it out.

Actually, this tidy.el looks much more complete than mine. It is like 20 times longer, though.

update 8/29/02:

Thanks to Ted Weatherly for supplying the fix for the 'buffer has changed on disk' problem this function used to have.


Comment on EmacsTidy:

Exactly what I was looking for! I like that its short and simple too!




Our Founder
ToolboxClick to hide/show