3/12/2007

Pretty Print XML With Emacs

There seems to be no way in Emacs's nxml-mode to pretty format a block of xml code. However, here is one.


(defun bf-pretty-print-xml-region (begin end)
  "Pretty format XML markup in region. You need to have nxml-mode
http://www.emacswiki.org/cgi-bin/wiki/NxmlMode installed to do
this.  The function inserts linebreaks to separate tags that have
nothing but whitespace between them.  It then indents the markup
by using nxml's indentation rules."
  (interactive "r")
  (save-excursion
      (nxml-mode)
      (goto-char begin)
      (while (search-forward-regexp "\>[ \\t]*\<" nil t) 
        (backward-char) (insert "\n"))
      (indent-region begin end))
    (message "Ah, much better!"))

Update: I was wondering why I had to write such an obviously useful function by myself. Turns out that there is an interface to HTML Tidy, which, among many other ways to manipulate XML documents, also does pretty printing. Anyway, if you don't want to install tidy, the simple function above will work just fine.

2 comments:

nagi said...

nice job.
I'm using your function with (delete-trailing-whitespace) function after (indent region begin end) .
regards

calculateonlinedotorg said...

I love this piece of software !!! Before I used xmllint (from cygwin) but this is easier to use.

One note however: CDATA blocks are not properly indented. But again this might be a feature of the indentation routine of nxml rather than a problem in the "Pretty print XML" code. For xml with CDATA sections xmllint might indent better.