pascal.vim 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. " Vim indent file
  2. " Language: Pascal
  3. " Maintainer: Neil Carter <n.carter@swansea.ac.uk>
  4. " Created: 2004 Jul 13
  5. " Last Change: 2017 Jun 13
  6. "
  7. " This is version 2.0, a complete rewrite.
  8. "
  9. " For further documentation, see http://psy.swansea.ac.uk/staff/carter/vim/
  10. if exists("b:did_indent")
  11. finish
  12. endif
  13. let b:did_indent = 1
  14. setlocal indentexpr=GetPascalIndent(v:lnum)
  15. setlocal indentkeys&
  16. setlocal indentkeys+==end;,==const,==type,==var,==begin,==repeat,==until,==for
  17. setlocal indentkeys+==program,==function,==procedure,==object,==private
  18. setlocal indentkeys+==record,==if,==else,==case
  19. if exists("*GetPascalIndent")
  20. finish
  21. endif
  22. function! s:GetPrevNonCommentLineNum( line_num )
  23. " Skip lines starting with a comment
  24. let SKIP_LINES = '^\s*\(\((\*\)\|\(\*\ \)\|\(\*)\)\|{\|}\)'
  25. let nline = a:line_num
  26. while nline > 0
  27. let nline = prevnonblank(nline-1)
  28. if getline(nline) !~? SKIP_LINES
  29. break
  30. endif
  31. endwhile
  32. return nline
  33. endfunction
  34. function! s:PurifyCode( line_num )
  35. " Strip any trailing comments and whitespace
  36. let pureline = 'TODO'
  37. return pureline
  38. endfunction
  39. function! GetPascalIndent( line_num )
  40. " Line 0 always goes at column 0
  41. if a:line_num == 0
  42. return 0
  43. endif
  44. let this_codeline = getline( a:line_num )
  45. " SAME INDENT
  46. " Middle of a three-part comment
  47. if this_codeline =~ '^\s*\*'
  48. return indent( a:line_num - 1)
  49. endif
  50. " COLUMN 1 ALWAYS
  51. " Last line of the program
  52. if this_codeline =~ '^\s*end\.'
  53. return 0
  54. endif
  55. " Compiler directives, allowing "(*" and "{"
  56. "if this_codeline =~ '^\s*\({\|(\*\)$\(IFDEF\|IFNDEF\|ELSE\|ENDIF\)'
  57. if this_codeline =~ '^\s*\({\|(\*\)\$'
  58. return 0
  59. endif
  60. " section headers
  61. if this_codeline =~ '^\s*\(program\|procedure\|function\|type\)\>'
  62. return 0
  63. endif
  64. " Subroutine separators, lines ending with "const" or "var"
  65. if this_codeline =~ '^\s*\((\*\ _\+\ \*)\|\(const\|var\)\)$'
  66. return 0
  67. endif
  68. " OTHERWISE, WE NEED TO LOOK FURTHER BACK...
  69. let prev_codeline_num = s:GetPrevNonCommentLineNum( a:line_num )
  70. let prev_codeline = getline( prev_codeline_num )
  71. let indnt = indent( prev_codeline_num )
  72. " INCREASE INDENT
  73. " If the PREVIOUS LINE ended in these items, always indent
  74. if prev_codeline =~ '\<\(type\|const\|var\)$'
  75. return indnt + shiftwidth()
  76. endif
  77. if prev_codeline =~ '\<repeat$'
  78. if this_codeline !~ '^\s*until\>'
  79. return indnt + shiftwidth()
  80. else
  81. return indnt
  82. endif
  83. endif
  84. if prev_codeline =~ '\<\(begin\|record\)$'
  85. if this_codeline !~ '^\s*end\>'
  86. return indnt + shiftwidth()
  87. else
  88. return indnt
  89. endif
  90. endif
  91. " If the PREVIOUS LINE ended with these items, indent if not
  92. " followed by "begin"
  93. if prev_codeline =~ '\<\(\|else\|then\|do\)$' || prev_codeline =~ ':$'
  94. if this_codeline !~ '^\s*begin\>'
  95. return indnt + shiftwidth()
  96. else
  97. " If it does start with "begin" then keep the same indent
  98. "return indnt + shiftwidth()
  99. return indnt
  100. endif
  101. endif
  102. " Inside a parameter list (i.e. a "(" without a ")"). ???? Considers
  103. " only the line before the current one. TODO: Get it working for
  104. " parameter lists longer than two lines.
  105. if prev_codeline =~ '([^)]\+$'
  106. return indnt + shiftwidth()
  107. endif
  108. " DECREASE INDENT
  109. " Lines starting with "else", but not following line ending with
  110. " "end".
  111. if this_codeline =~ '^\s*else\>' && prev_codeline !~ '\<end$'
  112. return indnt - shiftwidth()
  113. endif
  114. " Lines after a single-statement branch/loop.
  115. " Two lines before ended in "then", "else", or "do"
  116. " Previous line didn't end in "begin"
  117. let prev2_codeline_num = s:GetPrevNonCommentLineNum( prev_codeline_num )
  118. let prev2_codeline = getline( prev2_codeline_num )
  119. if prev2_codeline =~ '\<\(then\|else\|do\)$' && prev_codeline !~ '\<begin$'
  120. " If the next code line after a single statement branch/loop
  121. " starts with "end", "except" or "finally", we need an
  122. " additional unindentation.
  123. if this_codeline =~ '^\s*\(end;\|except\|finally\|\)$'
  124. " Note that we don't return from here.
  125. return indnt - 2 * shiftwidth()
  126. endif
  127. return indnt - shiftwidth()
  128. endif
  129. " Lines starting with "until" or "end". This rule must be overridden
  130. " by the one for "end" after a single-statement branch/loop. In
  131. " other words that rule should come before this one.
  132. if this_codeline =~ '^\s*\(end\|until\)\>'
  133. return indnt - shiftwidth()
  134. endif
  135. " MISCELLANEOUS THINGS TO CATCH
  136. " Most "begin"s will have been handled by now. Any remaining
  137. " "begin"s on their own line should go in column 1.
  138. if this_codeline =~ '^\s*begin$'
  139. return 0
  140. endif
  141. " ____________________________________________________________________
  142. " Object/Borland Pascal/Delphi Extensions
  143. "
  144. " Note that extended-pascal is handled here, unless it is simpler to
  145. " handle them in the standard-pascal section above.
  146. " COLUMN 1 ALWAYS
  147. " section headers at start of line.
  148. if this_codeline =~ '^\s*\(interface\|implementation\|uses\|unit\)\>'
  149. return 0
  150. endif
  151. " INDENT ONCE
  152. " If the PREVIOUS LINE ended in these items, always indent.
  153. if prev_codeline =~ '^\s*\(unit\|uses\|try\|except\|finally\|private\|protected\|public\|published\)$'
  154. return indnt + shiftwidth()
  155. endif
  156. " ???? Indent "procedure" and "functions" if they appear within an
  157. " class/object definition. But that means overriding standard-pascal
  158. " rule where these words always go in column 1.
  159. " UNINDENT ONCE
  160. if this_codeline =~ '^\s*\(except\|finally\)$'
  161. return indnt - shiftwidth()
  162. endif
  163. if this_codeline =~ '^\s*\(private\|protected\|public\|published\)$'
  164. return indnt - shiftwidth()
  165. endif
  166. " ____________________________________________________________________
  167. " If nothing changed, return same indent.
  168. return indnt
  169. endfunction