home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / vim60.zip / vim60rt2.zip / vim / vim60 / indent / fortran.vim < prev    next >
Text File  |  2001-09-26  |  4KB  |  147 lines

  1. " Vim indent file
  2. " Language:    Fortran90 (and Fortran95, Fortran77, F and elf90)
  3. " Version:    0.33
  4. " Last Change:    2001 Sep 17
  5. " Maintainer:    Ajit J. Thakkar <ajit@unb.ca>; <http://www.unb.ca/chem/ajit/>
  6. " For the latest version of this file, see <http://www.unb.ca/chem/ajit/vim.htm>
  7.  
  8. " Only load this indent file when no other was loaded.
  9. if exists("b:did_indent")
  10.   finish
  11. endif
  12. let b:did_indent = 1
  13.  
  14. setlocal indentkeys+==~end,=~case,=~if,=~else,=~do,=~where,=~elsewhere,=~select
  15.  
  16. " Determine whether this is a fixed or free format source file
  17. " if this hasn't been done yet
  18. if !exists("b:fortran_fixed_source")
  19.   if exists("fortran_free_source")
  20.     let b:fortran_fixed_source = 0
  21.   else
  22.     " f90 and f95 allow both fixed and free source form
  23.     " assume fixed source form unless signs of free source form
  24.     " are detected in the first five columns of the first 25 lines
  25.     " Detection becomes more accurate and time-consuming if more lines
  26.     " are checked. Increase the limit below if you keep lots of comments at
  27.     " the very top of each file and you have a fast computer
  28.     let s:lmax = 25
  29.     if ( s:lmax > line("$") )
  30.       let s:lmax = line("$")
  31.     endif
  32.     let b:fortran_fixed_source = 1
  33.     let s:ln=1
  34.     while s:ln <= s:lmax
  35.       let s:test = strpart(getline(s:ln),0,5)
  36.       if s:test[0] !~ '[Cc*!#]' && s:test !~ '^ \+[!#]' && s:test =~ '[^ 0-9\t]'
  37.     let b:fortran_fixed_source = 0
  38.     break
  39.       endif
  40.       let s:ln = s:ln + 1
  41.     endwhile
  42.   endif
  43. endif
  44.  
  45. " Define the appropriate indent function but only once
  46. if (b:fortran_fixed_source == 1)
  47.   setlocal indentexpr=FortranGetFixedIndent()
  48.   if exists("*FortranGetFixedIndent")
  49.     finish
  50.   endif
  51. else
  52.   setlocal indentexpr=FortranGetFreeIndent()
  53.   if exists("*FortranGetFreeIndent")
  54.     finish
  55.   endif
  56. endif
  57.  
  58. function FortranGetIndent(lnum)
  59.   let ind = indent(a:lnum)
  60.   let prevline=getline(a:lnum)
  61.   " Strip tail comment
  62.   let prevstat=substitute(prevline, '!.*$', '', '')
  63.  
  64.   "Indent do loops only if they are all guaranteed to be of do/end do type
  65.   if exists("b:fortran_do_enddo") || exists("fortran_do_enddo")
  66.     if prevstat =~? '^\s*\(\d\+\s\)\=\s*\(\a\w*\s*:\)\=\s*do\>'
  67.       let ind = ind + &sw
  68.     endif
  69.     if getline(v:lnum) =~? '^\s*\(\d\+\s\)\=\s*end\s*do\>'
  70.       let ind = ind - &sw
  71.     endif
  72.   endif
  73.  
  74.   "Add a shiftwidth to statements following if, else, case,
  75.   "where and elsewhere statements
  76.   if prevstat =~? '^\s*\(\d\+\s\)\=\s*\(else\|case\|where\|elsewhere\)\>' || prevstat =~? '^\s*\(\d\+\s\)\=\s*\(\a\w*\s*:\)\=\s*if\>'
  77.      let ind = ind + &sw
  78.     " Remove unwanted indent after logical and arithmetic ifs
  79.     if prevstat =~? '\<if\>' && prevstat !~? '\<then\>'
  80.       let ind = ind - &sw
  81.     endif
  82.   endif
  83.  
  84.   "Subtract a shiftwidth from else, elsewhere, case, end if,
  85.   " end where and end select statements
  86.   if getline(v:lnum) =~? '^\s*\(\d\+\s\)\=\s*\(else\|elsewhere\|case\|end\s*\(if\|where\|select\)\)\>'
  87.     let ind = ind - &sw
  88.     " Fix indent for case statement immediately after select
  89.     if prevstat =~? '\<select\>'
  90.       let ind = ind + &sw
  91.     endif
  92.   endif
  93.  
  94.   return ind
  95. endfunction
  96.  
  97. function FortranGetFreeIndent()
  98.   "Find the previous non-blank line
  99.   let lnum = prevnonblank(v:lnum - 1)
  100.  
  101.   "Use zero indent at the top of the file
  102.   if lnum == 0
  103.     return 0
  104.   endif
  105.  
  106.   let ind=FortranGetIndent(lnum)
  107.   return ind
  108. endfunction
  109.  
  110. function FortranGetFixedIndent()
  111.   let currline=getline(v:lnum)
  112.   "Don't indent comments, continuation lines and labelled lines
  113.   if strpart(currline,0,6) =~ '[^ \t]'
  114.     let ind = indent(v:lnum)
  115.     return ind
  116.   endif
  117.  
  118.   "Find the previous line which is not blank, not a comment,
  119.   "not a continuation line, and does not have a label
  120.   let lnum = v:lnum - 1
  121.   while lnum > 0
  122.     let prevline=getline(lnum)
  123.     if (prevline =~ "^[C*!]") || (prevline =~ "^\s*$") || (strpart(prevline,5,1) !~ "[ 0]")
  124.       " Skip comments, blank lines and continuation lines
  125.       let lnum = lnum - 1
  126.     else
  127.       let test=strpart(prevline,0,5)
  128.       if test =~ "[0-9]"
  129.     " Skip lines with statement numbers
  130.     let lnum = lnum - 1
  131.       else
  132.     break
  133.       endif
  134.     endif
  135.   endwhile
  136.  
  137.   "First line must begin at column 7
  138.   if lnum == 0
  139.     return 6
  140.   endif
  141.  
  142.   let ind=FortranGetIndent(lnum)
  143.   return ind
  144. endfunction
  145.  
  146. " vim:sw=2 tw=130
  147.