robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
e651ae5864
master
1" Vim indent file 2" Language: DTD (Document Type Definition for XML) 3" Maintainer: Doug Kearns <dougkearns@gmail.com> 4" Previous Maintainer: Nikolai Weibull <now@bitwi.se> 5" Last Change: 24 Sep 2021 6 7" Only load this indent file when no other was loaded. 8if exists ( "b:did_indent" ) 9finish 10endif 11let b: did_indent = 1 12 13setlocal indentexpr = GetDTDIndent () 14setlocal indentkeys = !^F,o,O,> 15setlocal nosmartindent 16 17let b: undo_indent = "setl inde< indk< si<" 18 19if exists ( "*GetDTDIndent" ) 20finish 21endif 22 23let s: cpo_save = & cpo 24set cpo & vim 25 26" TODO: Needs to be adjusted to stop at [, <, and ]. 27let s: token_pattern = '^[^[:space:]]\+' 28 29function s: lex1 ( input , start , ...) 30let pattern = a: 0 > 0 ? a: 1 : s: token_pattern 31let start = matchend ( a: input , '^\_s*' , a: start ) 32if start == -1 33return [ "" , a: start ] 34endif 35let end = matchend ( a: input , pattern , start ) 36if end == -1 37return [ "" , a: start ] 38endif 39let token = strpart ( a: input , start , end - start ) 40return [ token , end ] 41endfunction 42 43function s: lex ( input , start , ...) 44let pattern = a: 0 > 0 ? a: 1 : s: token_pattern 45let info = s: lex1 ( a: input , a: start , pattern ) 46while info [ 0 ] == '--' 47let info = s: lex1 ( a: input , info [ 1 ], pattern ) 48while info [ 0 ] != "" && info [ 0 ] != '--' 49let info = s: lex1 ( a: input , info [ 1 ], pattern ) 50endwhile 51if info [ 0 ] == "" 52return info 53endif 54let info = s: lex1 ( a: input , info [ 1 ], pattern ) 55endwhile 56return info 57endfunction 58 59function s: indent_to_innermost_parentheses ( line , end ) 60let token = '(' 61let end = a: end 62let parentheses = [ end - 1 ] 63while token != "" 64let [ token , end ] = s: lex ( a: line , end , '^\%([(),|]\|[A-Za-z0-9_-]\+\|#P\=CDATA\|%[A-Za-z0-9_-]\+;\)[?*+]\=' ) 65if token [ 0 ] == '(' 66call add ( parentheses , end - 1 ) 67elseif token [ 0 ] == ')' 68if len ( parentheses ) == 1 69return [ -1 , end ] 70endif 71call remove ( parentheses , -1 ) 72endif 73endwhile 74return [ parentheses [ -1 ] - strridx ( a: line , "\n" , parentheses [ -1 ]), end ] 75endfunction 76 77" TODO: Line and end could be script global (think OO members). 78function GetDTDIndent () 79if v: lnum == 1 80return 0 81endif 82 83" Begin by searching back for a <! that isn’t inside a comment. 84" From here, depending on what follows immediately after, parse to 85" where we’re at to determine what to do. 86if search ( '<!' , 'bceW' ) == 0 87return indent ( v: lnum - 1 ) 88endif 89let lnum = line ( '.' ) 90let col = col ( '.' ) 91let indent = indent ( '.' ) 92let line = lnum == v: lnum ? getline ( lnum ) : join ( getline ( lnum , v: lnum - 1 ), "\n" ) 93 94let [ declaration , end ] = s: lex1 ( line , col ) 95if declaration == "" 96return indent + shiftwidth () 97elseif declaration == '--' 98" We’re looking at a comment. Now, simply determine if the comment is 99" terminated or not. If it isn’t, let Vim take care of that using 100" 'comments' and 'autoindent'. Otherwise, indent to the first lines level. 101while declaration != "" 102let [ declaration , end ] = s: lex ( line , end ) 103if declaration == "-->" 104return indent 105endif 106endwhile 107return -1 108elseif declaration == 'ELEMENT' 109" Check for element name. If none exists, indent one level. 110let [ name , end ] = s: lex ( line , end ) 111if name == "" 112return indent + shiftwidth () 113endif 114 115" Check for token following element name. This can be a specification of 116" whether the start or end tag may be omitted. If nothing is found, indent 117" one level. 118let [ token , end ] = s: lex ( line , end , '^\%([-O(]\|ANY\|EMPTY\)' ) 119let n = 0 120while token =~ '[-O]' && n < 2 121let [ token , end ] = s: lex ( line , end , '^\%([-O(]\|ANY\|EMPTY\)' ) 122let n += 1 123endwhile 124if token == "" 125return indent + shiftwidth () 126endif 127 128" Next comes the content model. If the token we’ve found isn’t a 129" parenthesis it must be either ANY, EMPTY or some random junk. Either 130" way, we’re done indenting this element, so set it to that of the first 131" line so that the terminating “>” winds up having the same indentation. 132if token != '(' 133return indent 134endif 135 136" Now go through the content model. We need to keep track of the nesting 137" of parentheses. As soon as we hit 0 we’re done. If that happens we must 138" have a complete content model. Thus set indentation to be the same as that 139" of the first line so that the terminating “>” winds up having the same 140" indentation. Otherwise, we’ll indent to the innermost parentheses not yet 141" matched. 142let [ indent_of_innermost , end ] = s: indent_to_innermost_parentheses ( line , end ) 143if indent_of_innermost != -1 144return indent_of_innermost 145endif 146 147" Finally, look for any additions and/or exceptions to the content model. 148" This is defined by a “+” or “-” followed by another content model 149" declaration. 150" TODO: Can the “-” be separated by whitespace from the “(”? 151let seen = { '+(' : 0 , '-(' : 0 } 152while 1 153let [ additions_exceptions , end ] = s: lex ( line , end , '^[+-](' ) 154if additions_exceptions != '+(' && additions_exceptions != '-(' 155let [ token , end ] = s: lex ( line , end ) 156if token == '>' 157return indent 158endif 159" TODO: Should use s:lex here on getline(v:lnum) and check for >. 160return getline ( v: lnum ) =~ '^\s*>' || count ( values ( seen ), 0 ) == 0 ? indent : ( indent + shiftwidth ()) 161endif 162 163" If we’ve seen an addition or exception already and this is of the same 164" kind, the user is writing a broken DTD. Time to bail. 165if seen [ additions_exceptions ] 166return indent 167endif 168let seen [ additions_exceptions ] = 1 169 170let [ indent_of_innermost , end ] = s: indent_to_innermost_parentheses ( line , end ) 171if indent_of_innermost != -1 172return indent_of_innermost 173endif 174endwhile 175elseif declaration == 'ATTLIST' 176" Check for element name. If none exists, indent one level. 177let [ name , end ] = s: lex ( line , end ) 178if name == "" 179return indent + shiftwidth () 180endif 181 182" Check for any number of attributes. 183while 1 184" Check for attribute name. If none exists, indent one level, unless the 185" current line is a lone “>”, in which case we indent to the same level 186" as the first line. Otherwise, if the attribute name is “>”, we have 187" actually hit the end of the attribute list, in which case we indent to 188" the same level as the first line. 189let [ name , end ] = s: lex ( line , end ) 190if name == "" 191" TODO: Should use s:lex here on getline(v:lnum) and check for >. 192return getline ( v: lnum ) =~ '^\s*>' ? indent : ( indent + shiftwidth ()) 193elseif name == ">" 194return indent 195endif 196 197" Check for attribute value declaration. If none exists, indent two 198" levels. Otherwise, if it’s an enumerated value, check for nested 199" parentheses and indent to the innermost one if we don’t reach the end 200" of the listc. Otherwise, just continue with looking for the default 201" attribute value. 202" TODO: Do validation of keywords 203" (CDATA|NMTOKEN|NMTOKENS|ID|IDREF|IDREFS|ENTITY|ENTITIES)? 204let [ value , end ] = s: lex ( line , end , '^\%((\|[^[:space:]]\+\)' ) 205if value == "" 206return indent + shiftwidth () * 2 207elseif value == 'NOTATION' 208" If this is a enumerated value based on notations, read another token 209" for the actual value. If it doesn’t exist, indent three levels. 210" TODO: If validating according to above, value must be equal to '('. 211let [ value , end ] = s: lex ( line , end , '^\%((\|[^[:space:]]\+\)' ) 212if value == "" 213return indent + shiftwidth () * 3 214endif 215endif 216 217if value == '(' 218let [ indent_of_innermost , end ] = s: indent_to_innermost_parentheses ( line , end ) 219if indent_of_innermost != -1 220return indent_of_innermost 221endif 222endif 223 224" Finally look for the attribute’s default value. If non exists, indent 225" two levels. 226let [ default , end ] = s: lex ( line , end , '^\%("\_[^"]*"\|#\(REQUIRED\|IMPLIED\|FIXED\)\)' ) 227if default == "" 228return indent + shiftwidth () * 2 229elseif default == '#FIXED' 230" We need to look for the fixed value. If non exists, indent three 231" levels. 232let [ default , end ] = s: lex ( line , end , '^"\_[^"]*"' ) 233if default == "" 234return indent + shiftwidth () * 3 235endif 236endif 237endwhile 238elseif declaration == 'ENTITY' 239" Check for entity name. If none exists, indent one level. Otherwise, if 240" the name actually turns out to be a percent sign, “%”, this is a 241" parameter entity. Read another token to determine the entity name and, 242" again, if none exists, indent one level. 243let [ name , end ] = s: lex ( line , end ) 244if name == "" 245return indent + shiftwidth () 246elseif name == '%' 247let [ name , end ] = s: lex ( line , end ) 248if name == "" 249return indent + shiftwidth () 250endif 251endif 252 253" Now check for the entity value. If none exists, indent one level. If it 254" does exist, indent to same level as first line, as we’re now done with 255" this entity. 256" 257" The entity value can be a string in single or double quotes (no escapes 258" to worry about, as entities are used instead). However, it can also be 259" that this is an external unparsed entity. In that case we have to look 260" further for (possibly) a public ID and an URI followed by the NDATA 261" keyword and the actual notation name. For the public ID and URI, indent 262" two levels, if they don’t exist. If the NDATA keyword doesn’t exist, 263" indent one level. Otherwise, if the actual notation name doesn’t exist, 264" indent two level. If it does, indent to same level as first line, as 265" we’re now done with this entity. 266let [ value , end ] = s: lex ( line , end ) 267if value == "" 268return indent + shiftwidth () 269elseif value == 'SYSTEM' || value == 'PUBLIC' 270let [ quoted_string , end ] = s: lex ( line , end , '\%("[^"]\+"\|''[^'']\+''\)' ) 271if quoted_string == "" 272return indent + shiftwidth () * 2 273endif 274 275if value == 'PUBLIC' 276let [ quoted_string , end ] = s: lex ( line , end , '\%("[^"]\+"\|''[^'']\+''\)' ) 277if quoted_string == "" 278return indent + shiftwidth () * 2 279endif 280endif 281 282let [ ndata , end ] = s: lex ( line , end ) 283if ndata == "" 284return indent + shiftwidth () 285endif 286 287let [ name , end ] = s: lex ( line , end ) 288return name == "" ? ( indent + shiftwidth () * 2 ) : indent 289else 290return indent 291endif 292elseif declaration == 'NOTATION' 293" Check for notation name. If none exists, indent one level. 294let [ name , end ] = s: lex ( line , end ) 295if name == "" 296return indent + shiftwidth () 297endif 298 299" Now check for the external ID. If none exists, indent one level. 300let [ id , end ] = s: lex ( line , end ) 301if id == "" 302return indent + shiftwidth () 303elseif id == 'SYSTEM' || id == 'PUBLIC' 304let [ quoted_string , end ] = s: lex ( line , end , '\%("[^"]\+"\|''[^'']\+''\)' ) 305if quoted_string == "" 306return indent + shiftwidth () * 2 307endif 308 309if id == 'PUBLIC' 310let [ quoted_string , end ] = s: lex ( line , end , '\%("[^"]\+"\|''[^'']\+''\|>\)' ) 311if quoted_string == "" 312" TODO: Should use s:lex here on getline(v:lnum) and check for >. 313return getline ( v: lnum ) =~ '^\s*>' ? indent : ( indent + shiftwidth () * 2 ) 314elseif quoted_string == '>' 315return indent 316endif 317endif 318endif 319 320return indent 321endif 322 323" TODO: Processing directives could be indented I suppose. But perhaps it’s 324" just as well to let the user decide how to indent them (perhaps extending 325" this function to include proper support for whatever processing directive 326" language they want to use). 327 328" Conditional sections are simply passed along to let Vim decide what to do 329" (and hence the user). 330return -1 331endfunction 332 333let & cpo = s: cpo_save 334unlet s: cpo_save