cdl.vim 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. " Description: Comshare Dimension Definition Language (CDL)
  2. " Author: Raul Segura Acevedo <raulseguraaceved@netscape.net>
  3. " Last Change: Fri Nov 30 13:35:48 2001 CST
  4. if exists("b:did_indent")
  5. "finish
  6. endif
  7. let b:did_indent = 1
  8. setlocal indentexpr=CdlGetIndent(v:lnum)
  9. setlocal indentkeys&
  10. setlocal indentkeys+==~else,=~endif,=~then,;,),=
  11. " Only define the function once.
  12. if exists("*CdlGetIndent")
  13. "finish
  14. endif
  15. " find out if an "...=..." expresion is an assignment (or a conditional)
  16. " it scans 'line' first, and then the previos lines
  17. fun! CdlAsignment(lnum, line)
  18. let f = -1
  19. let lnum = a:lnum
  20. let line = a:line
  21. while lnum > 0 && f == -1
  22. " line without members [a] of [b]:[c]...
  23. let inicio = 0
  24. while 1
  25. " keywords that help to decide
  26. let inicio = matchend(line, '\c\<\(expr\|\a*if\|and\|or\|not\|else\|then\|memberis\|\k\+of\)\>\|[<>;]', inicio)
  27. if inicio < 0
  28. break
  29. endif
  30. " it's formula if there's a ';', 'elsE', 'theN', 'enDif' or 'expr'
  31. " conditional if there's a '<', '>', 'elseif', 'if', 'and', 'or', 'not',
  32. " 'memberis', 'childrenof' and other \k\+of funcions
  33. let f = line[inicio-1] =~? '[en;]' || strpart(line, inicio-4, 4) =~? 'ndif\|expr'
  34. endw
  35. let lnum = prevnonblank(lnum-1)
  36. let line = substitute(getline(lnum), '\c\(\[[^]]*]\(\s*of\s*\|:\)*\)\+', ' ', 'g')
  37. endw
  38. " if we hit the start of the file then f = -1, return 1 (formula)
  39. return f != 0
  40. endf
  41. fun! CdlGetIndent(lnum)
  42. let thisline = getline(a:lnum)
  43. if match(thisline, '^\s*\(\k\+\|\[[^]]*]\)\s*\(,\|;\s*$\)') >= 0
  44. " it's an attributes line
  45. return shiftwidth()
  46. elseif match(thisline, '^\c\s*\([{}]\|\/[*/]\|dimension\|schedule\|group\|hierarchy\|class\)') >= 0
  47. " it's a header or '{' or '}' or a comment
  48. return 0
  49. end
  50. let lnum = prevnonblank(a:lnum-1)
  51. " Hit the start of the file, use zero indent.
  52. if lnum == 0
  53. return 0
  54. endif
  55. " PREVIOUS LINE
  56. let ind = indent(lnum)
  57. let line = getline(lnum)
  58. let f = -1 " wether a '=' is a conditional or a asignment, -1 means we don't know yet
  59. " one 'closing' element at the beginning of the line has already reduced the
  60. " indent, but 'else', 'elseif' & 'then' increment it for the next line
  61. " '=' at the beginning has already de right indent (increased for asignments)
  62. let inicio = matchend(line, '^\c\s*\(else\a*\|then\|endif\|/[*/]\|[);={]\)')
  63. if inicio > 0
  64. let c = line[inicio-1]
  65. " ')' and '=' don't change indent and are useless to set 'f'
  66. if c == '{'
  67. return shiftwidth()
  68. elseif c != ')' && c != '='
  69. let f = 1 " all but 'elseif' are followed by a formula
  70. if c ==? 'n' || c ==? 'e' " 'then', 'else'
  71. let ind = ind + shiftwidth()
  72. elseif strpart(line, inicio-6, 6) ==? 'elseif' " elseif, set f to conditional
  73. let ind = ind + shiftwidth()
  74. let f = 0
  75. end
  76. end
  77. end
  78. " remove members [a] of [b]:[c]... (inicio remainds valid)
  79. let line = substitute(line, '\c\(\[[^]]*]\(\s*of\s*\|:\)*\)\+', ' ', 'g')
  80. while 1
  81. " search for the next interesting element
  82. let inicio=matchend(line, '\c\<if\|endif\|[()=;]', inicio)
  83. if inicio < 0
  84. break
  85. end
  86. let c = line[inicio-1]
  87. " 'expr(...)' containing the formula
  88. if strpart(line, inicio-5, 5) ==? 'expr('
  89. let ind = 0
  90. let f = 1
  91. elseif c == ')' || c== ';' || strpart(line, inicio-5, 5) ==? 'endif'
  92. let ind = ind - shiftwidth()
  93. elseif c == '(' || c ==? 'f' " '(' or 'if'
  94. let ind = ind + shiftwidth()
  95. else " c == '='
  96. " if it is an asignment increase indent
  97. if f == -1 " we don't know yet, find out
  98. let f = CdlAsignment(lnum, strpart(line, 0, inicio))
  99. end
  100. if f == 1 " formula increase it
  101. let ind = ind + shiftwidth()
  102. end
  103. end
  104. endw
  105. " CURRENT LINE, if it starts with a closing element, decrease indent
  106. " or if it starts with '=' (asignment), increase indent
  107. if match(thisline, '^\c\s*\(else\|then\|endif\|[);]\)') >= 0
  108. let ind = ind - shiftwidth()
  109. elseif match(thisline, '^\s*=') >= 0
  110. if f == -1 " we don't know yet if is an asignment, find out
  111. let f = CdlAsignment(lnum, "")
  112. end
  113. if f == 1 " formula increase it
  114. let ind = ind + shiftwidth()
  115. end
  116. end
  117. return ind
  118. endfun