Using flymake in emacs to validate xml as you type

February 23, 2009 at 11:44 pm Leave a comment

Motivation

  • Seeing mistakes earlier can make them easier to correct
  • Many programs will simply die with little logging if given broken xml files
  • Remembering to run a validator takes effort

Setup

  • Install rxp (this is available under cygwin on windows, and using apt under most linuxes)
  • Create this perl scripts, make it executable and place it on your path [1] call it xmlparse
    #!/usr/bin/perl
    open(HA, "rxp $ARGV[0] 2>&1|");
    while (){
        /^Error/ && chomp;
        print;
    }
  • Add the following code to your .emacs file
  • (defun flymake-xml-init ()
      (let* ((temp-file (flymake-init-create-temp-buffer-copy
    		     'flymake-create-temp-inplace))
    	 (local-file (file-relative-name
    		      temp-file
    		      (file-name-directory buffer-file-name))))
        (list "xmlparse" (list local-file))))
    
    (add-to-list 'flymake-allowed-file-name-masks
    	     '("\\.xml\\'" flymake-xml-init)) 
    
    (add-to-list 'flymake-err-line-patterns
    	     '("\\(.*\\) at line \\([0-9]+\\) char \\([0-9]+\\) of file://\\(.*\\)"
    	      4 2 3 1))
  • Also, ensure that you have the following command somewhere in your .emacs file
     (add-hook 'find-file-hook 'flymake-find-file-hook)

This should then work. You might like to change the face used to show errors with M-x customize-face flymake-errline. Hovering over an error will show you a description of the error

Caveats

The behavior for unclosed tags is not what I want. The error is reported where the parent tag closes – which can be quite some distance from the opening tag. However at least the message is informative.

There is an existing emacs host, nxhtml, which has some of these things built in together with other editing tools – but it looks a little monolithic.

[1] This ensures that each error is written on only one line. Flymake appears to only be able deal with output in this form.

Advertisements

Entry filed under: Uncategorized. Tags: , , , .

Directory Links on Windows Using flymake in emacs to validate html as you type

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


February 2009
M T W T F S S
« Sep   Mar »
 1
2345678
9101112131415
16171819202122
232425262728  

%d bloggers like this: