blob: 2c83f26b58eb679857b860c37b705ff04194e978 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001" Vim indent file
Bram Moolenaar8a94d872015-01-25 13:02:57 +01002" Language: Fortran 2008 (and earlier versions: 2003, 95, 90, and 77)
3" Version: 0.41
4" Last Change: 2015 Jan. 15
5" Maintainer: Ajit J. Thakkar <ajit@unb.ca>; <http://www2.unb.ca/~ajit/>
Bram Moolenaar071d4272004-06-13 20:20:40 +00006" Usage: Do :help fortran-indent from Vim
Bram Moolenaar8a94d872015-01-25 13:02:57 +01007" Credits:
8" Useful suggestions were made by: Albert Oliver Serra.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009
10" Only load this indent file when no other was loaded.
11if exists("b:did_indent")
12 finish
13endif
14let b:did_indent = 1
15
Bram Moolenaarb6b046b2011-12-30 13:11:27 +010016let s:cposet=&cpoptions
17set cpoptions&vim
18
Bram Moolenaar071d4272004-06-13 20:20:40 +000019setlocal indentkeys+==~end,=~case,=~if,=~else,=~do,=~where,=~elsewhere,=~select
Bram Moolenaar251e1912011-06-19 05:09:16 +020020setlocal indentkeys+==~endif,=~enddo,=~endwhere,=~endselect,=~elseif
21setlocal indentkeys+==~type,=~interface,=~forall,=~associate,=~block,=~enum
22setlocal indentkeys+==~endforall,=~endassociate,=~endblock,=~endenum
23if exists("b:fortran_indent_more") || exists("g:fortran_indent_more")
24 setlocal indentkeys+==~function,=~subroutine,=~module,=~contains,=~program
25 setlocal indentkeys+==~endfunction,=~endsubroutine,=~endmodule
26 setlocal indentkeys+==~endprogram
27endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000028
29" Determine whether this is a fixed or free format source file
30" if this hasn't been done yet
31if !exists("b:fortran_fixed_source")
32 if exists("fortran_free_source")
33 " User guarantees free source form
34 let b:fortran_fixed_source = 0
35 elseif exists("fortran_fixed_source")
36 " User guarantees fixed source form
37 let b:fortran_fixed_source = 1
38 else
39 " f90 and f95 allow both fixed and free source form
40 " assume fixed source form unless signs of free source form
Bram Moolenaar8a94d872015-01-25 13:02:57 +010041 " are detected in the first five columns of the first s:lmax lines.
42 " Detection becomes more accurate and more time-consuming if more lines
Bram Moolenaar071d4272004-06-13 20:20:40 +000043 " are checked. Increase the limit below if you keep lots of comments at
44 " the very top of each file and you have a fast computer
Bram Moolenaarc88ebf72010-07-22 22:30:23 +020045 let s:lmax = 500
Bram Moolenaar071d4272004-06-13 20:20:40 +000046 if ( s:lmax > line("$") )
47 let s:lmax = line("$")
48 endif
49 let b:fortran_fixed_source = 1
50 let s:ln=1
51 while s:ln <= s:lmax
52 let s:test = strpart(getline(s:ln),0,5)
Bram Moolenaarc88ebf72010-07-22 22:30:23 +020053 if s:test !~ '^[Cc*]' && s:test !~ '^ *[!#]' && s:test =~ '[^ 0-9\t]' && s:test !~ '^[ 0-9]*\t'
Bram Moolenaar071d4272004-06-13 20:20:40 +000054 let b:fortran_fixed_source = 0
55 break
56 endif
57 let s:ln = s:ln + 1
58 endwhile
59 endif
60endif
61
62" Define the appropriate indent function but only once
63if (b:fortran_fixed_source == 1)
64 setlocal indentexpr=FortranGetFixedIndent()
65 if exists("*FortranGetFixedIndent")
66 finish
67 endif
68else
69 setlocal indentexpr=FortranGetFreeIndent()
70 if exists("*FortranGetFreeIndent")
71 finish
72 endif
73endif
74
Bram Moolenaar071d4272004-06-13 20:20:40 +000075function FortranGetIndent(lnum)
76 let ind = indent(a:lnum)
77 let prevline=getline(a:lnum)
78 " Strip tail comment
79 let prevstat=substitute(prevline, '!.*$', '', '')
Bram Moolenaar251e1912011-06-19 05:09:16 +020080 let prev2line=getline(a:lnum-1)
81 let prev2stat=substitute(prev2line, '!.*$', '', '')
Bram Moolenaar071d4272004-06-13 20:20:40 +000082
83 "Indent do loops only if they are all guaranteed to be of do/end do type
Bram Moolenaarc81e5e72007-05-05 18:24:42 +000084 if exists("b:fortran_do_enddo") || exists("g:fortran_do_enddo")
Bram Moolenaar071d4272004-06-13 20:20:40 +000085 if prevstat =~? '^\s*\(\d\+\s\)\=\s*\(\a\w*\s*:\)\=\s*do\>'
86 let ind = ind + &sw
87 endif
88 if getline(v:lnum) =~? '^\s*\(\d\+\s\)\=\s*end\s*do\>'
89 let ind = ind - &sw
90 endif
91 endif
92
Bram Moolenaar251e1912011-06-19 05:09:16 +020093 "Add a shiftwidth to statements following if, else, else if, case,
94 "where, else where, forall, type, interface and associate statements
95 if prevstat =~? '^\s*\(case\|else\|else\s*if\|else\s*where\)\>'
96 \ ||prevstat=~? '^\s*\(type\|interface\|associate\|enum\)\>'
97 \ ||prevstat=~?'^\s*\(\d\+\s\)\=\s*\(\a\w*\s*:\)\=\s*\(forall\|where\|block\)\>'
98 \ ||prevstat=~? '^\s*\(\d\+\s\)\=\s*\(\a\w*\s*:\)\=\s*if\>'
Bram Moolenaar071d4272004-06-13 20:20:40 +000099 let ind = ind + &sw
100 " Remove unwanted indent after logical and arithmetic ifs
101 if prevstat =~? '\<if\>' && prevstat !~? '\<then\>'
102 let ind = ind - &sw
103 endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000104 " Remove unwanted indent after type( statements
Bram Moolenaar251e1912011-06-19 05:09:16 +0200105 if prevstat =~? '^\s*type\s*('
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000106 let ind = ind - &sw
107 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108 endif
109
Bram Moolenaar251e1912011-06-19 05:09:16 +0200110 "Indent program units unless instructed otherwise
111 if !exists("b:fortran_indent_less") && !exists("g:fortran_indent_less")
112 let prefix='\(\(pure\|impure\|elemental\|recursive\)\s\+\)\{,2}'
113 let type='\(\(integer\|real\|double\s\+precision\|complex\|logical'
114 \.'\|character\|type\|class\)\s*\S*\s\+\)\='
115 if prevstat =~? '^\s*\(module\|contains\|program\)\>'
116 \ ||prevstat =~? '^\s*'.prefix.'subroutine\>'
117 \ ||prevstat =~? '^\s*'.prefix.type.'function\>'
118 \ ||prevstat =~? '^\s*'.type.prefix.'function\>'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119 let ind = ind + &sw
120 endif
Bram Moolenaar251e1912011-06-19 05:09:16 +0200121 if getline(v:lnum) =~? '^\s*contains\>'
122 \ ||getline(v:lnum)=~? '^\s*end\s*'
123 \ .'\(function\|subroutine\|module\|program\)\>'
124 let ind = ind - &sw
125 endif
126 endif
127
128 "Subtract a shiftwidth from else, else if, elsewhere, case, end if,
129 " end where, end select, end forall, end interface, end associate,
130 " end enum, and end type statements
131 if getline(v:lnum) =~? '^\s*\(\d\+\s\)\=\s*'
132 \. '\(else\|else\s*if\|else\s*where\|case\|'
133 \. 'end\s*\(if\|where\|select\|interface\|'
Bram Moolenaar8a94d872015-01-25 13:02:57 +0100134 \. 'type\|forall\|associate\|enum\|block\)\)\>'
Bram Moolenaar251e1912011-06-19 05:09:16 +0200135 let ind = ind - &sw
136 " Fix indent for case statement immediately after select
137 if prevstat =~? '\<select\s\+\(case\|type\)\>'
138 let ind = ind + &sw
139 endif
140 endif
141
142 "First continuation line
143 if prevstat =~ '&\s*$' && prev2stat !~ '&\s*$'
144 let ind = ind + &sw
145 endif
Bram Moolenaar8a94d872015-01-25 13:02:57 +0100146 if prevstat =~ '&\s*$' && prevstat =~ '\<else\s*if\>'
147 let ind = ind - &sw
148 endif
Bram Moolenaar251e1912011-06-19 05:09:16 +0200149 "Line after last continuation line
Bram Moolenaar8a94d872015-01-25 13:02:57 +0100150 if prevstat !~ '&\s*$' && prev2stat =~ '&\s*$' && prevstat !~? '\<then\>'
Bram Moolenaar251e1912011-06-19 05:09:16 +0200151 let ind = ind - &sw
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152 endif
153
154 return ind
155endfunction
156
157function FortranGetFreeIndent()
158 "Find the previous non-blank line
159 let lnum = prevnonblank(v:lnum - 1)
160
161 "Use zero indent at the top of the file
162 if lnum == 0
163 return 0
164 endif
165
166 let ind=FortranGetIndent(lnum)
167 return ind
168endfunction
169
170function FortranGetFixedIndent()
171 let currline=getline(v:lnum)
172 "Don't indent comments, continuation lines and labelled lines
173 if strpart(currline,0,6) =~ '[^ \t]'
174 let ind = indent(v:lnum)
175 return ind
176 endif
177
178 "Find the previous line which is not blank, not a comment,
179 "not a continuation line, and does not have a label
180 let lnum = v:lnum - 1
181 while lnum > 0
182 let prevline=getline(lnum)
183 if (prevline =~ "^[C*!]") || (prevline =~ "^\s*$")
184 \ || (strpart(prevline,5,1) !~ "[ 0]")
185 " Skip comments, blank lines and continuation lines
186 let lnum = lnum - 1
187 else
188 let test=strpart(prevline,0,5)
189 if test =~ "[0-9]"
190 " Skip lines with statement numbers
191 let lnum = lnum - 1
192 else
193 break
194 endif
195 endif
196 endwhile
197
198 "First line must begin at column 7
199 if lnum == 0
200 return 6
201 endif
202
203 let ind=FortranGetIndent(lnum)
204 return ind
205endfunction
206
207let &cpoptions=s:cposet
208unlet s:cposet
209
210" vim:sw=2 tw=130