1" Vim indent file 2" Language: Fortran 2008 (and older: Fortran 2003, 95, 90, and 77) 3" Version: 0.42 4" Last Change: 2015 Nov. 30 5" Maintainer: Ajit J. Thakkar <[email protected]>; <http://www2.unb.ca/~ajit/> 6" Usage: For instructions, do :help fortran-indent from Vim 7" Credits: 8" Useful suggestions were made by: Albert Oliver Serra. 9 10" Only load this indent file when no other was loaded. 11if exists("b:did_indent") 12 finish 13endif 14let b:did_indent = 1 15 16let s:cposet=&cpoptions 17set cpoptions&vim 18 19setlocal indentkeys+==~end,=~case,=~if,=~else,=~do,=~where,=~elsewhere,=~select 20setlocal indentkeys+==~endif,=~enddo,=~endwhere,=~endselect,=~elseif 21setlocal indentkeys+==~type,=~interface,=~forall,=~associate,=~block,=~enum 22setlocal indentkeys+==~endforall,=~endassociate,=~endblock,=~endenum 23if 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 27endif 28 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 34if !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 still allows 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 69endif 70 71" Define the appropriate indent function but only once 72if (b:fortran_fixed_source == 1) 73 setlocal indentexpr=FortranGetFixedIndent() 74 if exists("*FortranGetFixedIndent") 75 finish 76 endif 77else 78 setlocal indentexpr=FortranGetFreeIndent() 79 if exists("*FortranGetFreeIndent") 80 finish 81 endif 82endif 83 84function FortranGetIndent(lnum) 85 let ind = indent(a:lnum) 86 let prevline=getline(a:lnum) 87 " Strip tail comment 88 let prevstat=substitute(prevline, '!.*$', '', '') 89 let prev2line=getline(a:lnum-1) 90 let prev2stat=substitute(prev2line, '!.*$', '', '') 91 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 + &sw 96 endif 97 if getline(v:lnum) =~? '^\s*\(\d\+\s\)\=\s*end\s*do\>' 98 let ind = ind - &sw 99 endif 100 endif 101 102 "Add a shiftwidth to statements following if, else, else if, case, 103 "where, else where, forall, type, interface and associate statements 104 if prevstat =~? '^\s*\(case\|else\|else\s*if\|else\s*where\)\>' 105 \ ||prevstat=~? '^\s*\(type\|interface\|associate\|enum\)\>' 106 \ ||prevstat=~?'^\s*\(\d\+\s\)\=\s*\(\a\w*\s*:\)\=\s*\(forall\|where\|block\)\>' 107 \ ||prevstat=~? '^\s*\(\d\+\s\)\=\s*\(\a\w*\s*:\)\=\s*if\>' 108 let ind = ind + &sw 109 " Remove unwanted indent after logical and arithmetic ifs 110 if prevstat =~? '\<if\>' && prevstat !~? '\<then\>' 111 let ind = ind - &sw 112 endif 113 " Remove unwanted indent after type( statements 114 if prevstat =~? '^\s*type\s*(' 115 let ind = ind - &sw 116 endif 117 endif 118 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*\(module\|contains\|program\)\>' 125 \ ||prevstat =~? '^\s*'.prefix.'subroutine\>' 126 \ ||prevstat =~? '^\s*'.prefix.type.'function\>' 127 \ ||prevstat =~? '^\s*'.type.prefix.'function\>' 128 let ind = ind + &sw 129 endif 130 if getline(v:lnum) =~? '^\s*contains\>' 131 \ ||getline(v:lnum)=~? '^\s*end\s*' 132 \ .'\(function\|subroutine\|module\|program\)\>' 133 let ind = ind - &sw 134 endif 135 endif 136 137 "Subtract a shiftwidth from else, else if, elsewhere, case, end if, 138 " end where, end select, end forall, end interface, end associate, 139 " end enum, and end type statements 140 if getline(v:lnum) =~? '^\s*\(\d\+\s\)\=\s*' 141 \. '\(else\|else\s*if\|else\s*where\|case\|' 142 \. 'end\s*\(if\|where\|select\|interface\|' 143 \. 'type\|forall\|associate\|enum\|block\)\)\>' 144 let ind = ind - &sw 145 " Fix indent for case statement immediately after select 146 if prevstat =~? '\<select\s\+\(case\|type\)\>' 147 let ind = ind + &sw 148 endif 149 endif 150 151 "First continuation line 152 if prevstat =~ '&\s*$' && prev2stat !~ '&\s*$' 153 let ind = ind + &sw 154 endif 155 if prevstat =~ '&\s*$' && prevstat =~ '\<else\s*if\>' 156 let ind = ind - &sw 157 endif 158 "Line after last continuation line 159 if prevstat !~ '&\s*$' && prev2stat =~ '&\s*$' && prevstat !~? '\<then\>' 160 let ind = ind - &sw 161 endif 162 163 return ind 164endfunction 165 166function FortranGetFreeIndent() 167 "Find the previous non-blank line 168 let lnum = prevnonblank(v:lnum - 1) 169 170 "Use zero indent at the top of the file 171 if lnum == 0 172 return 0 173 endif 174 175 let ind=FortranGetIndent(lnum) 176 return ind 177endfunction 178 179function FortranGetFixedIndent() 180 let currline=getline(v:lnum) 181 "Don't indent comments, continuation lines and labelled lines 182 if strpart(currline,0,6) =~ '[^ \t]' 183 let ind = indent(v:lnum) 184 return ind 185 endif 186 187 "Find the previous line which is not blank, not a comment, 188 "not a continuation line, and does not have a label 189 let lnum = v:lnum - 1 190 while lnum > 0 191 let prevline=getline(lnum) 192 if (prevline =~ "^[C*!]") || (prevline =~ "^\s*$") 193 \ || (strpart(prevline,5,1) !~ "[ 0]") 194 " Skip comments, blank lines and continuation lines 195 let lnum = lnum - 1 196 else 197 let test=strpart(prevline,0,5) 198 if test =~ "[0-9]" 199 " Skip lines with statement numbers 200 let lnum = lnum - 1 201 else 202 break 203 endif 204 endif 205 endwhile 206 207 "First line must begin at column 7 208 if lnum == 0 209 return 6 210 endif 211 212 let ind=FortranGetIndent(lnum) 213 return ind 214endfunction 215 216let &cpoptions=s:cposet 217unlet s:cposet 218 219" vim:sw=2 tw=130 220