fortran.vim 7.5 KB

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