blob: 4ff0700fd22b99b1891298688ec417ea13cb023f [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001" Vim indent file
2" Language: Fortran95 (and Fortran90, Fortran77, F and elf90)
Bram Moolenaarc88ebf72010-07-22 22:30:23 +02003" Version: 0.38
4" Last Change: 2010 July 21
Bram Moolenaar071d4272004-06-13 20:20:40 +00005" Maintainer: Ajit J. Thakkar <ajit@unb.ca>; <http://www.unb.ca/chem/ajit/>
6" Usage: Do :help fortran-indent from Vim
7
8" Only load this indent file when no other was loaded.
9if exists("b:did_indent")
10 finish
11endif
12let b:did_indent = 1
13
Bram Moolenaar071d4272004-06-13 20:20:40 +000014setlocal indentkeys+==~end,=~case,=~if,=~else,=~do,=~where,=~elsewhere,=~select
15setlocal indentkeys+==~endif,=~enddo,=~endwhere,=~endselect
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016setlocal indentkeys+==~type,=~interface
Bram Moolenaar071d4272004-06-13 20:20:40 +000017
18" Determine whether this is a fixed or free format source file
19" if this hasn't been done yet
20if !exists("b:fortran_fixed_source")
21 if exists("fortran_free_source")
22 " User guarantees free source form
23 let b:fortran_fixed_source = 0
24 elseif exists("fortran_fixed_source")
25 " User guarantees fixed source form
26 let b:fortran_fixed_source = 1
27 else
28 " f90 and f95 allow both fixed and free source form
29 " assume fixed source form unless signs of free source form
Bram Moolenaar910f66f2006-04-05 20:41:53 +000030 " are detected in the first five columns of the first 250 lines
Bram Moolenaar071d4272004-06-13 20:20:40 +000031 " Detection becomes more accurate and time-consuming if more lines
32 " are checked. Increase the limit below if you keep lots of comments at
33 " the very top of each file and you have a fast computer
Bram Moolenaarc88ebf72010-07-22 22:30:23 +020034 let s:lmax = 500
Bram Moolenaar071d4272004-06-13 20:20:40 +000035 if ( s:lmax > line("$") )
36 let s:lmax = line("$")
37 endif
38 let b:fortran_fixed_source = 1
39 let s:ln=1
40 while s:ln <= s:lmax
41 let s:test = strpart(getline(s:ln),0,5)
Bram Moolenaarc88ebf72010-07-22 22:30:23 +020042 if s:test !~ '^[Cc*]' && s:test !~ '^ *[!#]' && s:test =~ '[^ 0-9\t]' && s:test !~ '^[ 0-9]*\t'
Bram Moolenaar071d4272004-06-13 20:20:40 +000043 let b:fortran_fixed_source = 0
44 break
45 endif
46 let s:ln = s:ln + 1
47 endwhile
48 endif
49endif
50
51" Define the appropriate indent function but only once
52if (b:fortran_fixed_source == 1)
53 setlocal indentexpr=FortranGetFixedIndent()
54 if exists("*FortranGetFixedIndent")
55 finish
56 endif
57else
58 setlocal indentexpr=FortranGetFreeIndent()
59 if exists("*FortranGetFreeIndent")
60 finish
61 endif
62endif
63
Bram Moolenaar910f66f2006-04-05 20:41:53 +000064let s:cposet=&cpoptions
65set cpoptions-=C
66
Bram Moolenaar071d4272004-06-13 20:20:40 +000067function FortranGetIndent(lnum)
68 let ind = indent(a:lnum)
69 let prevline=getline(a:lnum)
70 " Strip tail comment
71 let prevstat=substitute(prevline, '!.*$', '', '')
72
73 "Indent do loops only if they are all guaranteed to be of do/end do type
Bram Moolenaarc81e5e72007-05-05 18:24:42 +000074 if exists("b:fortran_do_enddo") || exists("g:fortran_do_enddo")
Bram Moolenaar071d4272004-06-13 20:20:40 +000075 if prevstat =~? '^\s*\(\d\+\s\)\=\s*\(\a\w*\s*:\)\=\s*do\>'
76 let ind = ind + &sw
77 endif
78 if getline(v:lnum) =~? '^\s*\(\d\+\s\)\=\s*end\s*do\>'
79 let ind = ind - &sw
80 endif
81 endif
82
83 "Add a shiftwidth to statements following if, else, case,
Bram Moolenaareb3593b2006-04-22 22:33:57 +000084 "where, elsewhere, type and interface statements
Bram Moolenaar071d4272004-06-13 20:20:40 +000085 if prevstat =~? '^\s*\(\d\+\s\)\=\s*\(else\|case\|where\|elsewhere\)\>'
Bram Moolenaareb3593b2006-04-22 22:33:57 +000086 \ ||prevstat =~? '^\s*\(\d\+\s\)\=\s*\(type\|interface\)\>'
Bram Moolenaar071d4272004-06-13 20:20:40 +000087 \ || prevstat =~? '^\s*\(\d\+\s\)\=\s*\(\a\w*\s*:\)\=\s*if\>'
88 let ind = ind + &sw
89 " Remove unwanted indent after logical and arithmetic ifs
90 if prevstat =~? '\<if\>' && prevstat !~? '\<then\>'
91 let ind = ind - &sw
92 endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +000093 " Remove unwanted indent after type( statements
94 if prevstat =~? '\<type\s*('
95 let ind = ind - &sw
96 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000097 endif
98
99 "Subtract a shiftwidth from else, elsewhere, case, end if,
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000100 " end where, end select, end interface and end type statements
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101 if getline(v:lnum) =~? '^\s*\(\d\+\s\)\=\s*'
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000102 \. '\(else\|elsewhere\|case\|end\s*\(if\|where\|select\|interface\|type\)\)\>'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103 let ind = ind - &sw
104 " Fix indent for case statement immediately after select
105 if prevstat =~? '\<select\>'
106 let ind = ind + &sw
107 endif
108 endif
109
110 return ind
111endfunction
112
113function FortranGetFreeIndent()
114 "Find the previous non-blank line
115 let lnum = prevnonblank(v:lnum - 1)
116
117 "Use zero indent at the top of the file
118 if lnum == 0
119 return 0
120 endif
121
122 let ind=FortranGetIndent(lnum)
123 return ind
124endfunction
125
126function FortranGetFixedIndent()
127 let currline=getline(v:lnum)
128 "Don't indent comments, continuation lines and labelled lines
129 if strpart(currline,0,6) =~ '[^ \t]'
130 let ind = indent(v:lnum)
131 return ind
132 endif
133
134 "Find the previous line which is not blank, not a comment,
135 "not a continuation line, and does not have a label
136 let lnum = v:lnum - 1
137 while lnum > 0
138 let prevline=getline(lnum)
139 if (prevline =~ "^[C*!]") || (prevline =~ "^\s*$")
140 \ || (strpart(prevline,5,1) !~ "[ 0]")
141 " Skip comments, blank lines and continuation lines
142 let lnum = lnum - 1
143 else
144 let test=strpart(prevline,0,5)
145 if test =~ "[0-9]"
146 " Skip lines with statement numbers
147 let lnum = lnum - 1
148 else
149 break
150 endif
151 endif
152 endwhile
153
154 "First line must begin at column 7
155 if lnum == 0
156 return 6
157 endif
158
159 let ind=FortranGetIndent(lnum)
160 return ind
161endfunction
162
163let &cpoptions=s:cposet
164unlet s:cposet
165
166" vim:sw=2 tw=130