fortran.vim 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. " Vim indent file
  2. " Language: Fortran 2023 (and Fortran 2018, 2008, 2003, 95, 90, 77, 66)
  3. " Version: (v50) 2023 December 22
  4. " Maintainers: Ajit J. Thakkar <ajit@unb.ca>; <https://ajit.ext.unb.ca/>
  5. " Joshua Hollett <j.hollett@uwinnipeg.ca>
  6. " Usage: For instructions, do :help fortran-indent from Vim
  7. " Credits:
  8. " Version 0.1 was created in September 2000 by Ajit Thakkar.
  9. " Since then, useful suggestions and contributions have been made, in order, by:
  10. " Albert Oliver Serra, Takuya Fujiwara, Philipp Edelmann, Eisuke Kawashima,
  11. " Louis Cochen, and Doug Kearns.
  12. " Only load this indent file when no other was loaded.
  13. if exists("b:did_indent")
  14. finish
  15. endif
  16. let b:did_indent = 1
  17. let s:cposet=&cpoptions
  18. set cpoptions&vim
  19. let b:undo_indent = "setl inde< indk<"
  20. setlocal indentkeys+==~end,=~case,=~if,=~else,=~do,=~where,=~elsewhere,=~select
  21. setlocal indentkeys+==~endif,=~enddo,=~endwhere,=~endselect,=~elseif
  22. setlocal indentkeys+==~interface,=~forall,=~associate,=~block,=~enum,=~critical
  23. setlocal indentkeys+==~endforall,=~endassociate,=~endblock,=~endenum,=~endcritical
  24. if exists("b:fortran_indent_more") || exists("g:fortran_indent_more")
  25. setlocal indentkeys+==~function,=~subroutine,=~module,=~contains,=~program
  26. setlocal indentkeys+==~endfunction,=~endsubroutine,=~endmodule
  27. setlocal indentkeys+==~endprogram
  28. endif
  29. " Determine whether this is a fixed or free format source file
  30. " if this hasn't been done yet using the priority:
  31. " buffer-local value
  32. " > global value
  33. " > file extension as in Intel ifort, gcc (gfortran), NAG, Pathscale, and Cray compilers
  34. if !exists("b:fortran_fixed_source")
  35. if exists("fortran_free_source")
  36. " User guarantees free source form
  37. let b:fortran_fixed_source = 0
  38. elseif exists("fortran_fixed_source")
  39. " User guarantees fixed source form
  40. let b:fortran_fixed_source = 1
  41. elseif expand("%:e") =~? '^f\%(90\|95\|03\|08\)$'
  42. " Free-form file extension defaults as in Intel ifort, gcc(gfortran), NAG, Pathscale, and Cray compilers
  43. let b:fortran_fixed_source = 0
  44. elseif expand("%:e") =~? '^\%(f\|f77\|for\)$'
  45. " Fixed-form file extension defaults
  46. let b:fortran_fixed_source = 1
  47. else
  48. " Modern fortran compilers still allow both fixed and free source form
  49. " Assume fixed source form unless signs of free source form
  50. " are detected in the first five columns of the first s:lmax lines.
  51. " Detection becomes more accurate and time-consuming if more lines
  52. " are checked. Increase the limit below if you keep lots of comments at
  53. " the very top of each file and you have a fast computer.
  54. let s:lmax = 500
  55. if ( s:lmax > line("$") )
  56. let s:lmax = line("$")
  57. endif
  58. let b:fortran_fixed_source = 1
  59. let s:ln=1
  60. while s:ln <= s:lmax
  61. let s:test = strpart(getline(s:ln),0,5)
  62. if s:test !~ '^[Cc*]' && s:test !~ '^ *[!#]' && s:test =~ '[^ 0-9\t]' && s:test !~ '^[ 0-9]*\t'
  63. let b:fortran_fixed_source = 0
  64. break
  65. endif
  66. let s:ln = s:ln + 1
  67. endwhile
  68. endif
  69. endif
  70. " Define the appropriate indent function but only once
  71. if (b:fortran_fixed_source == 1)
  72. setlocal indentexpr=FortranGetFixedIndent()
  73. if exists("*FortranGetFixedIndent")
  74. let &cpoptions = s:cposet
  75. unlet s:cposet
  76. finish
  77. endif
  78. else
  79. setlocal indentexpr=FortranGetFreeIndent()
  80. if exists("*FortranGetFreeIndent")
  81. let &cpoptions = s:cposet
  82. unlet s:cposet
  83. finish
  84. endif
  85. endif
  86. function FortranGetIndent(lnum)
  87. let ind = indent(a:lnum)
  88. let prevline=getline(a:lnum)
  89. " Strip tail comment
  90. let prevstat=substitute(prevline, '!.*$', '', '')
  91. let prev2line=getline(a:lnum-1)
  92. let prev2stat=substitute(prev2line, '!.*$', '', '')
  93. "Indent do loops only if they are all guaranteed to be of do/end do type
  94. if exists("b:fortran_do_enddo") || exists("g:fortran_do_enddo")
  95. if prevstat =~? '^\s*\(\d\+\s\)\=\s*\(\a\w*\s*:\)\=\s*do\>'
  96. let ind = ind + shiftwidth()
  97. endif
  98. if getline(v:lnum) =~? '^\s*\(\d\+\s\)\=\s*end\s*do\>'
  99. let ind = ind - shiftwidth()
  100. endif
  101. endif
  102. "Add a shiftwidth to statements following if, else, else if, case, class,
  103. "where, else where, forall, type, interface and associate statements
  104. if prevstat =~? '^\s*\(case\|class\s\+is\|else\|else\s*if\|else\s*where\)\>'
  105. \ ||prevstat=~? '^\s*\(type\|rank\|interface\|associate\|enum\|critical\)\>'
  106. \ ||prevstat=~? '^\s*change\s\+team\>'
  107. \ ||prevstat=~?'^\s*\(\d\+\s\)\=\s*\(\a\w*\s*:\)\=\s*\(forall\|where\|block\)\>'
  108. \ ||prevstat=~? '^\s*\(\d\+\s\)\=\s*\(\a\w*\s*:\)\=\s*if\>'
  109. let ind = ind + shiftwidth()
  110. " Remove unwanted indent after logical and arithmetic ifs
  111. if prevstat =~? '\<if\>' && prevstat !~? '\<then\>'
  112. let ind = ind - shiftwidth()
  113. endif
  114. " Remove unwanted indent after type( statements
  115. if prevstat =~? '^\s*type\s*('
  116. let ind = ind - shiftwidth()
  117. endif
  118. endif
  119. "Indent program units unless instructed otherwise
  120. if !exists("b:fortran_indent_less") && !exists("g:fortran_indent_less")
  121. let prefix='\(\(pure\|impure\|elemental\|recursive\)\s\+\)\{,2}'
  122. let type='\(\(integer\|real\|double\s\+precision\|complex\|logical'
  123. \.'\|character\|type\|class\)\s*\S*\s\+\)\='
  124. if prevstat =~? '^\s*\(contains\|submodule\|program\)\>'
  125. \ ||prevstat =~? '^\s*'.'module\>\(\s*\procedure\)\@!'
  126. \ ||prevstat =~? '^\s*'.prefix.'subroutine\>'
  127. \ ||prevstat =~? '^\s*'.prefix.type.'function\>'
  128. \ ||prevstat =~? '^\s*'.type.prefix.'function\>'
  129. let ind = ind + shiftwidth()
  130. endif
  131. if getline(v:lnum) =~? '^\s*contains\>'
  132. \ ||getline(v:lnum)=~? '^\s*end\s*'
  133. \ .'\(function\|subroutine\|module\|submodule\|program\)\>'
  134. let ind = ind - shiftwidth()
  135. endif
  136. endif
  137. "Subtract a shiftwidth from else, else if, elsewhere, case, class, end if,
  138. " end where, end select, end forall, end interface, end associate,
  139. " end enum, end type, end block, end team and end type statements
  140. if getline(v:lnum) =~? '^\s*\(\d\+\s\)\=\s*'
  141. \. '\(else\|else\s*if\|else\s*where\|case\|class\|rank\|type\s\+is\|'
  142. \. 'end\s*\(if\|where\|select\|interface\|critical\|team\|'
  143. \. 'type\|forall\|associate\|enum\|block\)\)\>'
  144. let ind = ind - shiftwidth()
  145. " Fix indent for case statement immediately after select
  146. if prevstat =~? '\<select\s*\(case\|type\)\>'
  147. let ind = ind + shiftwidth()
  148. endif
  149. endif
  150. "First continuation line
  151. if prevstat =~ '&\s*$' && prev2stat !~ '&\s*$'
  152. let ind = ind + shiftwidth()
  153. endif
  154. "Line after last continuation line
  155. if prevstat !~ '&\s*$' && prev2stat =~ '&\s*$' && prevstat !~? '\<then\>'
  156. let ind = ind - shiftwidth()
  157. endif
  158. return ind
  159. endfunction
  160. function FortranGetFreeIndent()
  161. "Find the previous non-blank line
  162. let lnum = prevnonblank(v:lnum - 1)
  163. "Use zero indent at the top of the file
  164. if lnum == 0
  165. return 0
  166. endif
  167. let ind=FortranGetIndent(lnum)
  168. return ind
  169. endfunction
  170. function FortranGetFixedIndent()
  171. let currline=getline(v:lnum)
  172. "Don't indent comments, continuation lines and labelled lines
  173. if strpart(currline,0,6) =~ '[^ \t]'
  174. let ind = indent(v:lnum)
  175. return ind
  176. endif
  177. "Find the previous line which is not blank, not a comment,
  178. "not a continuation line, and does not have a label
  179. let lnum = v:lnum - 1
  180. while lnum > 0
  181. let prevline=getline(lnum)
  182. if (prevline =~ "^[C*!]") || (prevline =~ "^\s*$")
  183. \ || (strpart(prevline,5,1) !~ "[ 0]")
  184. " Skip comments, blank lines and continuation lines
  185. let lnum = lnum - 1
  186. else
  187. let test=strpart(prevline,0,5)
  188. if test =~ "[0-9]"
  189. " Skip lines with statement numbers
  190. let lnum = lnum - 1
  191. else
  192. break
  193. endif
  194. endif
  195. endwhile
  196. "First line must begin at column 7
  197. if lnum == 0
  198. return 6
  199. endif
  200. let ind=FortranGetIndent(lnum)
  201. return ind
  202. endfunction
  203. let &cpoptions = s:cposet
  204. unlet s:cposet
  205. " vim:sw=2 tw=130