Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
fortran.vim227 linesDownload Raw Back to indent
1" Vim indent file2" Language:	Fortran 2023 (and Fortran 2018, 2008, 2003, 95, 90, 77, 66)3" Version:	(v50) 2023 December 224" 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 Vim7" 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")15  finish16endif17let b:did_indent = 118 19let s:cposet=&cpoptions20set cpoptions&vim21let b:undo_indent = "setl inde< indk<"22 23setlocal indentkeys+==~end,=~case,=~if,=~else,=~do,=~where,=~elsewhere,=~select24setlocal indentkeys+==~endif,=~enddo,=~endwhere,=~endselect,=~elseif25setlocal indentkeys+==~interface,=~forall,=~associate,=~block,=~enum,=~critical26setlocal indentkeys+==~endforall,=~endassociate,=~endblock,=~endenum,=~endcritical27if exists("b:fortran_indent_more") || exists("g:fortran_indent_more")28  setlocal indentkeys+==~function,=~subroutine,=~module,=~contains,=~program29  setlocal indentkeys+==~endfunction,=~endsubroutine,=~endmodule30  setlocal indentkeys+==~endprogram31endif32 33" Determine whether this is a fixed or free format source file34" if this hasn't been done yet using the priority:35" buffer-local value36" > global value37" > file extension as in Intel ifort, gcc (gfortran), NAG, Pathscale, and Cray compilers38if !exists("b:fortran_fixed_source")39  if exists("fortran_free_source")40    " User guarantees free source form41    let b:fortran_fixed_source = 042  elseif exists("fortran_fixed_source")43    " User guarantees fixed source form44    let b:fortran_fixed_source = 145  elseif expand("%:e") =~? '^f\%(90\|95\|03\|08\)$'46    " Free-form file extension defaults as in Intel ifort, gcc(gfortran), NAG, Pathscale, and Cray compilers47    let b:fortran_fixed_source = 048  elseif expand("%:e") =~? '^\%(f\|f77\|for\)$'49    " Fixed-form file extension defaults50    let b:fortran_fixed_source = 151  else52    " Modern fortran compilers still allow both fixed and free source form53    " Assume fixed source form unless signs of free source form54    " are detected in the first five columns of the first s:lmax lines.55    " Detection becomes more accurate and time-consuming if more lines56    " are checked. Increase the limit below if you keep lots of comments at57    " the very top of each file and you have a fast computer.58    let s:lmax = 50059    if ( s:lmax > line("$") )60      let s:lmax = line("$")61    endif62    let b:fortran_fixed_source = 163    let s:ln=164    while s:ln <= s:lmax65      let s:test = strpart(getline(s:ln),0,5)66      if s:test !~ '^[Cc*]' && s:test !~ '^ *[!#]' && s:test =~ '[^ 0-9\t]' && s:test !~ '^[ 0-9]*\t'67	let b:fortran_fixed_source = 068	break69      endif70      let s:ln = s:ln + 171    endwhile72  endif73endif74 75" Define the appropriate indent function but only once76if (b:fortran_fixed_source == 1)77  setlocal indentexpr=FortranGetFixedIndent()78  if exists("*FortranGetFixedIndent")79    let &cpoptions = s:cposet80    unlet s:cposet81    finish82  endif83else84  setlocal indentexpr=FortranGetFreeIndent()85  if exists("*FortranGetFreeIndent")86    let &cpoptions = s:cposet87    unlet s:cposet88    finish89  endif90endif91 92function FortranGetIndent(lnum)93  let ind = indent(a:lnum)94  let prevline=getline(a:lnum)95  " Strip tail comment96  let prevstat=substitute(prevline, '!.*$', '', '')97  let prev2line=getline(a:lnum-1)98  let prev2stat=substitute(prev2line, '!.*$', '', '')99 100  "Indent do loops only if they are all guaranteed to be of do/end do type101  if exists("b:fortran_do_enddo") || exists("g:fortran_do_enddo")102    if prevstat =~? '^\s*\(\d\+\s\)\=\s*\(\a\w*\s*:\)\=\s*do\>'103      let ind = ind + shiftwidth()104    endif105    if getline(v:lnum) =~? '^\s*\(\d\+\s\)\=\s*end\s*do\>'106      let ind = ind - shiftwidth()107    endif108  endif109 110  "Add a shiftwidth to statements following if, else, else if, case, class,111  "where, else where, forall, type, interface and associate statements112  if 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\>'117     let ind = ind + shiftwidth()118    " Remove unwanted indent after logical and arithmetic ifs119    if prevstat =~? '\<if\>' && prevstat !~? '\<then\>'120      let ind = ind - shiftwidth()121    endif122    " Remove unwanted indent after type( statements123    if prevstat =~? '^\s*type\s*('124      let ind = ind - shiftwidth()125    endif126  endif127 128  "Indent program units unless instructed otherwise129  if !exists("b:fortran_indent_less") && !exists("g:fortran_indent_less")130    let prefix='\(\(pure\|impure\|elemental\|recursive\)\s\+\)\{,2}'131    let type='\(\(integer\|real\|double\s\+precision\|complex\|logical'132          \.'\|character\|type\|class\)\s*\S*\s\+\)\='133    if 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\>'138      let ind = ind + shiftwidth()139    endif140    if getline(v:lnum) =~? '^\s*contains\>'141          \ ||getline(v:lnum)=~? '^\s*end\s*'142          \ .'\(function\|subroutine\|module\|submodule\|program\)\>'143      let ind = ind - shiftwidth()144    endif145  endif146 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 statements150  if 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\)\)\>'154    let ind = ind - shiftwidth()155    " Fix indent for case statement immediately after select156    if prevstat =~? '\<select\s*\(case\|type\)\>'157      let ind = ind + shiftwidth()158    endif159  endif160 161  "First continuation line162  if prevstat =~ '&\s*$' && prev2stat !~ '&\s*$'163    let ind = ind + shiftwidth()164  endif165  "Line after last continuation line166  if prevstat !~ '&\s*$' && prev2stat =~ '&\s*$' && prevstat !~? '\<then\>'167    let ind = ind - shiftwidth()168  endif169 170  return ind171endfunction172 173function FortranGetFreeIndent()174  "Find the previous non-blank line175  let lnum = prevnonblank(v:lnum - 1)176 177  "Use zero indent at the top of the file178  if lnum == 0179    return 0180  endif181 182  let ind=FortranGetIndent(lnum)183  return ind184endfunction185 186function FortranGetFixedIndent()187  let currline=getline(v:lnum)188  "Don't indent comments, continuation lines and labelled lines189  if strpart(currline,0,6) =~ '[^ \t]'190    let ind = indent(v:lnum)191    return ind192  endif193 194  "Find the previous line which is not blank, not a comment,195  "not a continuation line, and does not have a label196  let lnum = v:lnum - 1197  while lnum > 0198    let prevline=getline(lnum)199    if (prevline =~ "^[C*!]") || (prevline =~ "^\s*$")200	\ || (strpart(prevline,5,1) !~ "[ 0]")201      " Skip comments, blank lines and continuation lines202      let lnum = lnum - 1203    else204      let test=strpart(prevline,0,5)205      if test =~ "[0-9]"206	" Skip lines with statement numbers207	let lnum = lnum - 1208      else209	break210      endif211    endif212  endwhile213 214  "First line must begin at column 7215  if lnum == 0216    return 6217  endif218 219  let ind=FortranGetIndent(lnum)220  return ind221endfunction222 223let &cpoptions = s:cposet224unlet s:cposet225 226" vim:sw=2 tw=130227 
codekingpro/portable-devtools · Team Ai