views:

180

answers:

1

Vim's syntax highlighting for XML/XSL is great, except it turns off all syntax highlighting in CDATA regions. Is there a way to turn on syntax highlighting on in CDATA regions?

At work, we have a lot of XSL code embedded within other XML documents. It would be great if I could get all of the goodness of XML editing for the embedded XSL code as well without having to temporarily remove the CDATA tags, or copy the CDATA content into a temporary file.

Example:

<root>
  <with type="xsl"><![CDATA[
      <xsl:template match="/">
          <!-- XSL content here -->
      </xsl:template>
   ]]>
  </with>
</root>

The XSL is always contained within , so there is something to match against.

We also sometimes embed Javascript inside CDATA regions as well. It would be nice to turn on Javascript syntax highlighting for those regions.

A: 

You would need to modify the syntax highlighting script that comes with vim (it's in /usr/share/vim/vim72/syntax/xml.vim on my system). However, I'm pretty sure this is a bad idea - you will be operating under the 'illusion' that the content of the CDATA section is actually XML and it's definitely not - as it's in a CDATA section it's just text.

In order to handle embedded javascript (which seems less risky to me) you will also need to create a modified syntax highlighter. A good place to start looking for information on how to do this would be the XHTML syntax highlighter for vim - that supports embedded languages

Nic Gibson
Looks like you can augment a syntax file by placing your modifications in a `~/.vim/after/filetype.vim` file. So I could alter the syntax highlighting rules for xml by creating `~/.vim/after/xml.vim`. I'm just not sure what to put in the file...
Jim Hurne
Ah, I'd forgotten that file (I've moved from mostly vim to mostly GUI editors these days). This is probably a question you should ask on the vim mailing lists I suspect
Nic Gibson