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