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