cdl.vim 4.3 KB

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