blob: a651c09c6be85277b8910d7389f1c4e64cdaba92 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001" Vim indent file
2" Language: Fortran95 (and Fortran90, Fortran77, F and elf90)
Bram Moolenaar251e1912011-06-19 05:09:16 +02003" Version: 0.39
4" Last Change: 2011 May 30
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
Bram Moolenaar251e1912011-06-19 05:09:16 +020015setlocal indentkeys+==~endif,=~enddo,=~endwhere,=~endselect,=~elseif
16setlocal indentkeys+==~type,=~interface,=~forall,=~associate,=~block,=~enum
17setlocal indentkeys+==~endforall,=~endassociate,=~endblock,=~endenum
18if exists("b:fortran_indent_more") || exists("g:fortran_indent_more")
19 setlocal indentkeys+==~function,=~subroutine,=~module,=~contains,=~program
20 setlocal indentkeys+==~endfunction,=~endsubroutine,=~endmodule
21 setlocal indentkeys+==~endprogram
22endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023
24" Determine whether this is a fixed or free format source file
25" if this hasn't been done yet
26if !exists("b:fortran_fixed_source")
27 if exists("fortran_free_source")
28 " User guarantees free source form
29 let b:fortran_fixed_source = 0
30 elseif exists("fortran_fixed_source")
31 " User guarantees fixed source form
32 let b:fortran_fixed_source = 1
33 else
34 " f90 and f95 allow both fixed and free source form
35 " assume fixed source form unless signs of free source form
Bram Moolenaar910f66f2006-04-05 20:41:53 +000036 " are detected in the first five columns of the first 250 lines
Bram Moolenaar071d4272004-06-13 20:20:40 +000037 " Detection becomes more accurate and time-consuming if more lines
38 " are checked. Increase the limit below if you keep lots of comments at
39 " the very top of each file and you have a fast computer
Bram Moolenaarc88ebf72010-07-22 22:30:23 +020040 let s:lmax = 500
Bram Moolenaar071d4272004-06-13 20:20:40 +000041 if ( s:lmax > line("$") )
42 let s:lmax = line("$")
43 endif
44 let b:fortran_fixed_source = 1
45 let s:ln=1
46 while s:ln <= s:lmax
47 let s:test = strpart(getline(s:ln),0,5)
Bram Moolenaarc88ebf72010-07-22 22:30:23 +020048 if s:test !~ '^[Cc*]' && s:test !~ '^ *[!#]' && s:test =~ '[^ 0-9\t]' && s:test !~ '^[ 0-9]*\t'
Bram Moolenaar071d4272004-06-13 20:20:40 +000049 let b:fortran_fixed_source = 0
50 break
51 endif
52 let s:ln = s:ln + 1
53 endwhile
54 endif
55endif
56
57" Define the appropriate indent function but only once
58if (b:fortran_fixed_source == 1)
59 setlocal indentexpr=FortranGetFixedIndent()
60 if exists("*FortranGetFixedIndent")
61 finish
62 endif
63else
64 setlocal indentexpr=FortranGetFreeIndent()
65 if exists("*FortranGetFreeIndent")
66 finish
67 endif
68endif
69
Bram Moolenaar910f66f2006-04-05 20:41:53 +000070let s:cposet=&cpoptions
71set cpoptions-=C
72
Bram Moolenaar071d4272004-06-13 20:20:40 +000073function FortranGetIndent(lnum)
74 let ind = indent(a:lnum)
75 let prevline=getline(a:lnum)
76 " Strip tail comment
77 let prevstat=substitute(prevline, '!.*$', '', '')
Bram Moolenaar251e1912011-06-19 05:09:16 +020078 let prev2line=getline(a:lnum-1)
79 let prev2stat=substitute(prev2line, '!.*$', '', '')
Bram Moolenaar071d4272004-06-13 20:20:40 +000080
81 "Indent do loops only if they are all guaranteed to be of do/end do type
Bram Moolenaarc81e5e72007-05-05 18:24:42 +000082 if exists("b:fortran_do_enddo") || exists("g:fortran_do_enddo")
Bram Moolenaar071d4272004-06-13 20:20:40 +000083 if prevstat =~? '^\s*\(\d\+\s\)\=\s*\(\a\w*\s*:\)\=\s*do\>'
84 let ind = ind + &sw
85 endif
86 if getline(v:lnum) =~? '^\s*\(\d\+\s\)\=\s*end\s*do\>'
87 let ind = ind - &sw
88 endif
89 endif
90
Bram Moolenaar251e1912011-06-19 05:09:16 +020091 "Add a shiftwidth to statements following if, else, else if, case,
92 "where, else where, forall, type, interface and associate statements
93 if prevstat =~? '^\s*\(case\|else\|else\s*if\|else\s*where\)\>'
94 \ ||prevstat=~? '^\s*\(type\|interface\|associate\|enum\)\>'
95 \ ||prevstat=~?'^\s*\(\d\+\s\)\=\s*\(\a\w*\s*:\)\=\s*\(forall\|where\|block\)\>'
96 \ ||prevstat=~? '^\s*\(\d\+\s\)\=\s*\(\a\w*\s*:\)\=\s*if\>'
Bram Moolenaar071d4272004-06-13 20:20:40 +000097 let ind = ind + &sw
98 " Remove unwanted indent after logical and arithmetic ifs
99 if prevstat =~? '\<if\>' && prevstat !~? '\<then\>'
100 let ind = ind - &sw
101 endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000102 " Remove unwanted indent after type( statements
Bram Moolenaar251e1912011-06-19 05:09:16 +0200103 if prevstat =~? '^\s*type\s*('
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000104 let ind = ind - &sw
105 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106 endif
107
Bram Moolenaar251e1912011-06-19 05:09:16 +0200108 "Indent program units unless instructed otherwise
109 if !exists("b:fortran_indent_less") && !exists("g:fortran_indent_less")
110 let prefix='\(\(pure\|impure\|elemental\|recursive\)\s\+\)\{,2}'
111 let type='\(\(integer\|real\|double\s\+precision\|complex\|logical'
112 \.'\|character\|type\|class\)\s*\S*\s\+\)\='
113 if prevstat =~? '^\s*\(module\|contains\|program\)\>'
114 \ ||prevstat =~? '^\s*'.prefix.'subroutine\>'
115 \ ||prevstat =~? '^\s*'.prefix.type.'function\>'
116 \ ||prevstat =~? '^\s*'.type.prefix.'function\>'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117 let ind = ind + &sw
118 endif
Bram Moolenaar251e1912011-06-19 05:09:16 +0200119 if getline(v:lnum) =~? '^\s*contains\>'
120 \ ||getline(v:lnum)=~? '^\s*end\s*'
121 \ .'\(function\|subroutine\|module\|program\)\>'
122 let ind = ind - &sw
123 endif
124 endif
125
126 "Subtract a shiftwidth from else, else if, elsewhere, case, end if,
127 " end where, end select, end forall, end interface, end associate,
128 " end enum, and end type statements
129 if getline(v:lnum) =~? '^\s*\(\d\+\s\)\=\s*'
130 \. '\(else\|else\s*if\|else\s*where\|case\|'
131 \. 'end\s*\(if\|where\|select\|interface\|'
132 \. 'type\|forall\|associate\|enum\)\)\>'
133 let ind = ind - &sw
134 " Fix indent for case statement immediately after select
135 if prevstat =~? '\<select\s\+\(case\|type\)\>'
136 let ind = ind + &sw
137 endif
138 endif
139
140 "First continuation line
141 if prevstat =~ '&\s*$' && prev2stat !~ '&\s*$'
142 let ind = ind + &sw
143 endif
144 "Line after last continuation line
145 if prevstat !~ '&\s*$' && prev2stat =~ '&\s*$'
146 let ind = ind - &sw
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147 endif
148
149 return ind
150endfunction
151
152function FortranGetFreeIndent()
153 "Find the previous non-blank line
154 let lnum = prevnonblank(v:lnum - 1)
155
156 "Use zero indent at the top of the file
157 if lnum == 0
158 return 0
159 endif
160
161 let ind=FortranGetIndent(lnum)
162 return ind
163endfunction
164
165function FortranGetFixedIndent()
166 let currline=getline(v:lnum)
167 "Don't indent comments, continuation lines and labelled lines
168 if strpart(currline,0,6) =~ '[^ \t]'
169 let ind = indent(v:lnum)
170 return ind
171 endif
172
173 "Find the previous line which is not blank, not a comment,
174 "not a continuation line, and does not have a label
175 let lnum = v:lnum - 1
176 while lnum > 0
177 let prevline=getline(lnum)
178 if (prevline =~ "^[C*!]") || (prevline =~ "^\s*$")
179 \ || (strpart(prevline,5,1) !~ "[ 0]")
180 " Skip comments, blank lines and continuation lines
181 let lnum = lnum - 1
182 else
183 let test=strpart(prevline,0,5)
184 if test =~ "[0-9]"
185 " Skip lines with statement numbers
186 let lnum = lnum - 1
187 else
188 break
189 endif
190 endif
191 endwhile
192
193 "First line must begin at column 7
194 if lnum == 0
195 return 6
196 endif
197
198 let ind=FortranGetIndent(lnum)
199 return ind
200endfunction
201
202let &cpoptions=s:cposet
203unlet s:cposet
204
205" vim:sw=2 tw=130