Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1 | " Vim syntax file |
| 2 | " Language: XML |
| 3 | " Maintainer: Johannes Zellner <johannes@zellner.org> |
| 4 | " Author and previous maintainer: |
| 5 | " Paul Siegmann <pauls@euronet.nl> |
| 6 | " Last Change: Fri, 04 Jun 2004 10:41:54 CEST |
| 7 | " Filenames: *.xml |
| 8 | " $Id$ |
| 9 | |
| 10 | " CONFIGURATION: |
| 11 | " syntax folding can be turned on by |
| 12 | " |
| 13 | " let g:xml_syntax_folding = 1 |
| 14 | " |
| 15 | " before the syntax file gets loaded (e.g. in ~/.vimrc). |
| 16 | " This might slow down syntax highlighting significantly, |
| 17 | " especially for large files. |
| 18 | " |
| 19 | " CREDITS: |
| 20 | " The original version was derived by Paul Siegmann from |
| 21 | " Claudio Fleiner's html.vim. |
| 22 | " |
| 23 | " REFERENCES: |
| 24 | " [1] http://www.w3.org/TR/2000/REC-xml-20001006 |
| 25 | " [2] http://www.w3.org/XML/1998/06/xmlspec-report-19980910.htm |
| 26 | " |
| 27 | " as <hirauchi@kiwi.ne.jp> pointed out according to reference [1] |
| 28 | " |
| 29 | " 2.3 Common Syntactic Constructs |
| 30 | " [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' | CombiningChar | Extender |
| 31 | " [5] Name ::= (Letter | '_' | ':') (NameChar)* |
| 32 | " |
| 33 | " NOTE: |
| 34 | " 1) empty tag delimiters "/>" inside attribute values (strings) |
| 35 | " confuse syntax highlighting. |
| 36 | " 2) for large files, folding can be pretty slow, especially when |
| 37 | " loading a file the first time and viewoptions contains 'folds' |
| 38 | " so that folds of previous sessions are applied. |
| 39 | " Don't use 'foldmethod=syntax' in this case. |
| 40 | |
| 41 | |
| 42 | " Quit when a syntax file was already loaded |
| 43 | if exists("b:current_syntax") |
| 44 | finish |
| 45 | endif |
| 46 | |
| 47 | let s:xml_cpo_save = &cpo |
| 48 | set cpo&vim |
| 49 | |
| 50 | syn case match |
| 51 | |
| 52 | " mark illegal characters |
| 53 | syn match xmlError "[<&]" |
| 54 | |
| 55 | " strings (inside tags) aka VALUES |
| 56 | " |
| 57 | " EXAMPLE: |
| 58 | " |
| 59 | " <tag foo.attribute = "value"> |
| 60 | " ^^^^^^^ |
| 61 | syn region xmlString contained start=+"+ skip=+\\\\\|\\"+ end=+"+ contains=xmlEntity display |
| 62 | syn region xmlString contained start=+'+ skip=+\\\\\|\\'+ end=+'+ contains=xmlEntity display |
| 63 | |
| 64 | |
| 65 | " punctuation (within attributes) e.g. <tag xml:foo.attribute ...> |
| 66 | " ^ ^ |
| 67 | " syn match xmlAttribPunct +[-:._]+ contained display |
| 68 | syn match xmlAttribPunct +[:.]+ contained display |
| 69 | |
| 70 | " no highlighting for xmlEqual (xmlEqual has no highlighting group) |
| 71 | syn match xmlEqual +=+ display |
| 72 | |
| 73 | |
| 74 | " attribute, everything before the '=' |
| 75 | " |
| 76 | " PROVIDES: @xmlAttribHook |
| 77 | " |
| 78 | " EXAMPLE: |
| 79 | " |
| 80 | " <tag foo.attribute = "value"> |
| 81 | " ^^^^^^^^^^^^^ |
| 82 | " |
| 83 | syn match xmlAttrib |
| 84 | \ +[-'"<]\@<!\<[a-zA-Z:_][-.0-9a-zA-Z0-9:_]*\>\(['">]\@!\|$\)+ |
| 85 | \ contained |
| 86 | \ contains=xmlAttribPunct,@xmlAttribHook |
| 87 | \ display |
| 88 | |
| 89 | |
| 90 | " namespace spec |
| 91 | " |
| 92 | " PROVIDES: @xmlNamespaceHook |
| 93 | " |
| 94 | " EXAMPLE: |
| 95 | " |
| 96 | " <xsl:for-each select = "lola"> |
| 97 | " ^^^ |
| 98 | " |
| 99 | if exists("g:xml_namespace_transparent") |
| 100 | syn match xmlNamespace |
| 101 | \ +\(<\|</\)\@<=[^ /!?<>"':]\+[:]\@=+ |
| 102 | \ contained |
| 103 | \ contains=@xmlNamespaceHook |
| 104 | \ transparent |
| 105 | \ display |
| 106 | else |
| 107 | syn match xmlNamespace |
| 108 | \ +\(<\|</\)\@<=[^ /!?<>"':]\+[:]\@=+ |
| 109 | \ contained |
| 110 | \ contains=@xmlNamespaceHook |
| 111 | \ display |
| 112 | endif |
| 113 | |
| 114 | |
| 115 | " tag name |
| 116 | " |
| 117 | " PROVIDES: @xmlTagHook |
| 118 | " |
| 119 | " EXAMPLE: |
| 120 | " |
| 121 | " <tag foo.attribute = "value"> |
| 122 | " ^^^ |
| 123 | " |
| 124 | syn match xmlTagName |
| 125 | \ +[<]\@<=[^ /!?<>"']\++ |
| 126 | \ contained |
| 127 | \ contains=xmlNamespace,xmlAttribPunct,@xmlTagHook |
| 128 | \ display |
| 129 | |
| 130 | |
| 131 | if exists('g:xml_syntax_folding') |
| 132 | |
| 133 | " start tag |
| 134 | " use matchgroup=xmlTag to skip over the leading '<' |
| 135 | " |
| 136 | " PROVIDES: @xmlStartTagHook |
| 137 | " |
| 138 | " EXAMPLE: |
| 139 | " |
| 140 | " <tag id="whoops"> |
| 141 | " s^^^^^^^^^^^^^^^e |
| 142 | " |
| 143 | syn region xmlTag |
| 144 | \ matchgroup=xmlTag start=+<[^ /!?<>"']\@=+ |
| 145 | \ matchgroup=xmlTag end=+>+ |
| 146 | \ contained |
| 147 | \ contains=xmlError,xmlTagName,xmlAttrib,xmlEqual,xmlString,@xmlStartTagHook |
| 148 | |
| 149 | |
| 150 | " highlight the end tag |
| 151 | " |
| 152 | " PROVIDES: @xmlTagHook |
| 153 | " (should we provide a separate @xmlEndTagHook ?) |
| 154 | " |
| 155 | " EXAMPLE: |
| 156 | " |
| 157 | " </tag> |
| 158 | " ^^^^^^ |
| 159 | " |
| 160 | syn match xmlEndTag |
| 161 | \ +</[^ /!?<>"']\+>+ |
| 162 | \ contained |
| 163 | \ contains=xmlNamespace,xmlAttribPunct,@xmlTagHook |
| 164 | |
| 165 | |
| 166 | " tag elements with syntax-folding. |
| 167 | " NOTE: NO HIGHLIGHTING -- highlighting is done by contained elements |
| 168 | " |
| 169 | " PROVIDES: @xmlRegionHook |
| 170 | " |
| 171 | " EXAMPLE: |
| 172 | " |
| 173 | " <tag id="whoops"> |
| 174 | " <!-- comment --> |
| 175 | " <another.tag></another.tag> |
| 176 | " <empty.tag/> |
| 177 | " some data |
| 178 | " </tag> |
| 179 | " |
| 180 | syn region xmlRegion |
| 181 | \ start=+<\z([^ /!?<>"']\+\)+ |
| 182 | \ skip=+<!--\_.\{-}-->+ |
| 183 | \ end=+</\z1\_\s\{-}>+ |
| 184 | \ matchgroup=xmlEndTag end=+/>+ |
| 185 | \ fold |
| 186 | \ contains=xmlTag,xmlEndTag,xmlCdata,xmlRegion,xmlComment,xmlEntity,xmlProcessing,@xmlRegionHook |
| 187 | \ keepend |
| 188 | \ extend |
| 189 | |
| 190 | else |
| 191 | |
| 192 | " no syntax folding: |
| 193 | " - contained attribute removed |
| 194 | " - xmlRegion not defined |
| 195 | " |
| 196 | syn region xmlTag |
| 197 | \ matchgroup=xmlTag start=+<[^ /!?<>"']\@=+ |
| 198 | \ matchgroup=xmlTag end=+>+ |
| 199 | \ contains=xmlError,xmlTagName,xmlAttrib,xmlEqual,xmlString,@xmlStartTagHook |
| 200 | |
| 201 | syn match xmlEndTag |
| 202 | \ +</[^ /!?<>"']\+>+ |
| 203 | \ contains=xmlNamespace,xmlAttribPunct,@xmlTagHook |
| 204 | |
| 205 | endif |
| 206 | |
| 207 | |
| 208 | " &entities; compare with dtd |
| 209 | syn match xmlEntity "&[^; \t]*;" contains=xmlEntityPunct |
| 210 | syn match xmlEntityPunct contained "[&.;]" |
| 211 | |
| 212 | if exists('g:xml_syntax_folding') |
| 213 | |
| 214 | " The real comments (this implements the comments as defined by xml, |
| 215 | " but not all xml pages actually conform to it. Errors are flagged. |
| 216 | syn region xmlComment |
| 217 | \ start=+<!+ |
| 218 | \ end=+>+ |
| 219 | \ contains=xmlCommentPart,xmlCommentError |
| 220 | \ extend |
| 221 | \ fold |
| 222 | |
| 223 | else |
| 224 | |
| 225 | " no syntax folding: |
| 226 | " - fold attribute removed |
| 227 | " |
| 228 | syn region xmlComment |
| 229 | \ start=+<!+ |
| 230 | \ end=+>+ |
| 231 | \ contains=xmlCommentPart,xmlCommentError |
| 232 | \ extend |
| 233 | |
| 234 | endif |
| 235 | |
| 236 | syn keyword xmlTodo contained TODO FIXME XXX display |
| 237 | syn match xmlCommentError contained "[^><!]" |
| 238 | syn region xmlCommentPart |
| 239 | \ start=+--+ |
| 240 | \ end=+--+ |
| 241 | \ contained |
| 242 | \ contains=xmlTodo,@xmlCommentHook |
| 243 | |
| 244 | |
| 245 | " CData sections |
| 246 | " |
| 247 | " PROVIDES: @xmlCdataHook |
| 248 | " |
| 249 | syn region xmlCdata |
| 250 | \ start=+<!\[CDATA\[+ |
| 251 | \ end=+]]>+ |
| 252 | \ contains=xmlCdataStart,xmlCdataEnd,@xmlCdataHook |
| 253 | \ keepend |
| 254 | \ extend |
| 255 | |
| 256 | " using the following line instead leads to corrupt folding at CDATA regions |
| 257 | " syn match xmlCdata +<!\[CDATA\[\_.\{-}]]>+ contains=xmlCdataStart,xmlCdataEnd,@xmlCdataHook |
| 258 | syn match xmlCdataStart +<!\[CDATA\[+ contained contains=xmlCdataCdata |
| 259 | syn keyword xmlCdataCdata CDATA contained |
| 260 | syn match xmlCdataEnd +]]>+ contained |
| 261 | |
| 262 | |
| 263 | " Processing instructions |
| 264 | " This allows "?>" inside strings -- good idea? |
| 265 | syn region xmlProcessing matchgroup=xmlProcessingDelim start="<?" end="?>" contains=xmlAttrib,xmlEqual,xmlString |
| 266 | |
| 267 | |
| 268 | if exists('g:xml_syntax_folding') |
| 269 | |
| 270 | " DTD -- we use dtd.vim here |
| 271 | syn region xmlDocType matchgroup=xmlDocTypeDecl |
| 272 | \ start="<!DOCTYPE"he=s+2,rs=s+2 end=">" |
| 273 | \ fold |
| 274 | \ contains=xmlDocTypeKeyword,xmlInlineDTD,xmlString |
| 275 | else |
| 276 | |
| 277 | " no syntax folding: |
| 278 | " - fold attribute removed |
| 279 | " |
| 280 | syn region xmlDocType matchgroup=xmlDocTypeDecl |
| 281 | \ start="<!DOCTYPE"he=s+2,rs=s+2 end=">" |
| 282 | \ contains=xmlDocTypeKeyword,xmlInlineDTD,xmlString |
| 283 | |
| 284 | endif |
| 285 | |
| 286 | syn keyword xmlDocTypeKeyword contained DOCTYPE PUBLIC SYSTEM |
| 287 | syn region xmlInlineDTD contained matchgroup=xmlDocTypeDecl start="\[" end="]" contains=@xmlDTD |
| 288 | syn include @xmlDTD <sfile>:p:h/dtd.vim |
| 289 | unlet b:current_syntax |
| 290 | |
| 291 | |
| 292 | " synchronizing |
| 293 | " TODO !!! to be improved !!! |
| 294 | |
| 295 | syn sync match xmlSyncDT grouphere xmlDocType +\_.\(<!DOCTYPE\)\@=+ |
| 296 | " syn sync match xmlSyncDT groupthere NONE +]>+ |
| 297 | |
| 298 | if exists('g:xml_syntax_folding') |
| 299 | syn sync match xmlSync grouphere xmlRegion +\_.\(<[^ /!?<>"']\+\)\@=+ |
| 300 | " syn sync match xmlSync grouphere xmlRegion "<[^ /!?<>"']*>" |
| 301 | syn sync match xmlSync groupthere xmlRegion +</[^ /!?<>"']\+>+ |
| 302 | endif |
| 303 | |
| 304 | syn sync minlines=100 |
| 305 | |
| 306 | |
| 307 | " The default highlighting. |
| 308 | hi def link xmlTodo Todo |
| 309 | hi def link xmlTag Function |
| 310 | hi def link xmlTagName Function |
| 311 | hi def link xmlEndTag Identifier |
| 312 | if !exists("g:xml_namespace_transparent") |
| 313 | hi def link xmlNamespace Tag |
| 314 | endif |
| 315 | hi def link xmlEntity Statement |
| 316 | hi def link xmlEntityPunct Type |
| 317 | |
| 318 | hi def link xmlAttribPunct Comment |
| 319 | hi def link xmlAttrib Type |
| 320 | |
| 321 | hi def link xmlString String |
| 322 | hi def link xmlComment Comment |
| 323 | hi def link xmlCommentPart Comment |
| 324 | hi def link xmlCommentError Error |
| 325 | hi def link xmlError Error |
| 326 | |
| 327 | hi def link xmlProcessingDelim Comment |
| 328 | hi def link xmlProcessing Type |
| 329 | |
| 330 | hi def link xmlCdata String |
| 331 | hi def link xmlCdataCdata Statement |
| 332 | hi def link xmlCdataStart Type |
| 333 | hi def link xmlCdataEnd Type |
| 334 | |
| 335 | hi def link xmlDocTypeDecl Function |
| 336 | hi def link xmlDocTypeKeyword Statement |
| 337 | hi def link xmlInlineDTD Function |
| 338 | |
| 339 | let b:current_syntax = "xml" |
| 340 | |
| 341 | let &cpo = s:xml_cpo_save |
| 342 | unlet s:xml_cpo_save |
| 343 | |
| 344 | " vim: ts=8 |