blob: e76c64b671b06b1f633c3d960532b8e9262010ef [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001" Vim indent file
2" Language: Fortran95 (and Fortran90, Fortran77, F and elf90)
Bram Moolenaarb6b046b2011-12-30 13:11:27 +01003" Version: 0.40
4" Last Change: 2011 Dec. 28
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 Moolenaarb6b046b2011-12-30 13:11:27 +010014let s:cposet=&cpoptions
15set cpoptions&vim
16
Bram Moolenaar071d4272004-06-13 20:20:40 +000017setlocal indentkeys+==~end,=~case,=~if,=~else,=~do,=~where,=~elsewhere,=~select
Bram Moolenaar251e1912011-06-19 05:09:16 +020018setlocal indentkeys+==~endif,=~enddo,=~endwhere,=~endselect,=~elseif
19setlocal indentkeys+==~type,=~interface,=~forall,=~associate,=~block,=~enum
20setlocal indentkeys+==~endforall,=~endassociate,=~endblock,=~endenum
21if exists("b:fortran_indent_more") || exists("g:fortran_indent_more")
22 setlocal indentkeys+==~function,=~subroutine,=~module,=~contains,=~program
23 setlocal indentkeys+==~endfunction,=~endsubroutine,=~endmodule
24 setlocal indentkeys+==~endprogram
25endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026
27" Determine whether this is a fixed or free format source file
28" if this hasn't been done yet
29if !exists("b:fortran_fixed_source")
30 if exists("fortran_free_source")
31 " User guarantees free source form
32 let b:fortran_fixed_source = 0
33 elseif exists("fortran_fixed_source")
34 " User guarantees fixed source form
35 let b:fortran_fixed_source = 1
36 else
37 " f90 and f95 allow both fixed and free source form
38 " assume fixed source form unless signs of free source form
Bram Moolenaar910f66f2006-04-05 20:41:53 +000039 " are detected in the first five columns of the first 250 lines
Bram Moolenaar071d4272004-06-13 20:20:40 +000040 " Detection becomes more accurate and time-consuming if more lines
41 " are checked. Increase the limit below if you keep lots of comments at
42 " the very top of each file and you have a fast computer
Bram Moolenaarc88ebf72010-07-22 22:30:23 +020043 let s:lmax = 500
Bram Moolenaar071d4272004-06-13 20:20:40 +000044 if ( s:lmax > line("$") )
45 let s:lmax = line("$")
46 endif
47 let b:fortran_fixed_source = 1
48 let s:ln=1
49 while s:ln <= s:lmax
50 let s:test = strpart(getline(s:ln),0,5)
Bram Moolenaarc88ebf72010-07-22 22:30:23 +020051 if s:test !~ '^[Cc*]' && s:test !~ '^ *[!#]' && s:test =~ '[^ 0-9\t]' && s:test !~ '^[ 0-9]*\t'
Bram Moolenaar071d4272004-06-13 20:20:40 +000052 let b:fortran_fixed_source = 0
53 break
54 endif
55 let s:ln = s:ln + 1
56 endwhile
57 endif
58endif
59
60" Define the appropriate indent function but only once
61if (b:fortran_fixed_source == 1)
62 setlocal indentexpr=FortranGetFixedIndent()
63 if exists("*FortranGetFixedIndent")
64 finish
65 endif
66else
67 setlocal indentexpr=FortranGetFreeIndent()
68 if exists("*FortranGetFreeIndent")
69 finish
70 endif
71endif
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