blob: df55569f89a708fe1ebf479dc3dfe68a51953616 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001" Vim indent file
Bram Moolenaar256972a2015-12-29 19:10:25 +01002" Language: Fortran 2008 (and older: Fortran 2003, 95, 90, and 77)
Bram Moolenaarc0514bf2016-11-17 14:50:09 +01003" Version: 47
Bram Moolenaar2c7f8c52020-04-20 19:52:53 +02004" Last Change: 2020 Apr 20
5" Patched By: Eisuke Kawashima
Bram Moolenaar8a94d872015-01-25 13:02:57 +01006" Maintainer: Ajit J. Thakkar <ajit@unb.ca>; <http://www2.unb.ca/~ajit/>
Bram Moolenaar256972a2015-12-29 19:10:25 +01007" Usage: For instructions, do :help fortran-indent from Vim
Bram Moolenaar8a94d872015-01-25 13:02:57 +01008" Credits:
Bram Moolenaar2ec618c2016-10-01 14:47:05 +02009" Useful suggestions were made, in chronological order, by:
10" Albert Oliver Serra, Takuya Fujiwara and Philipp Edelmann.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011
12" Only load this indent file when no other was loaded.
13if exists("b:did_indent")
14 finish
15endif
16let b:did_indent = 1
17
Bram Moolenaarb6b046b2011-12-30 13:11:27 +010018let s:cposet=&cpoptions
19set cpoptions&vim
20
Bram Moolenaar071d4272004-06-13 20:20:40 +000021setlocal indentkeys+==~end,=~case,=~if,=~else,=~do,=~where,=~elsewhere,=~select
Bram Moolenaar251e1912011-06-19 05:09:16 +020022setlocal indentkeys+==~endif,=~enddo,=~endwhere,=~endselect,=~elseif
23setlocal indentkeys+==~type,=~interface,=~forall,=~associate,=~block,=~enum
24setlocal indentkeys+==~endforall,=~endassociate,=~endblock,=~endenum
25if exists("b:fortran_indent_more") || exists("g:fortran_indent_more")
26 setlocal indentkeys+==~function,=~subroutine,=~module,=~contains,=~program
27 setlocal indentkeys+==~endfunction,=~endsubroutine,=~endmodule
28 setlocal indentkeys+==~endprogram
29endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000030
31" Determine whether this is a fixed or free format source file
Bram Moolenaar256972a2015-12-29 19:10:25 +010032" if this hasn't been done yet using the priority:
33" buffer-local value
34" > global value
35" > file extension as in Intel ifort, gcc (gfortran), NAG, Pathscale, and Cray compilers
Bram Moolenaar071d4272004-06-13 20:20:40 +000036if !exists("b:fortran_fixed_source")
37 if exists("fortran_free_source")
38 " User guarantees free source form
39 let b:fortran_fixed_source = 0
40 elseif exists("fortran_fixed_source")
41 " User guarantees fixed source form
42 let b:fortran_fixed_source = 1
Bram Moolenaar2c7f8c52020-04-20 19:52:53 +020043 elseif expand("%:e") =~? '^f\%(90\|95\|03\|08\)$'
Bram Moolenaar256972a2015-12-29 19:10:25 +010044 " Free-form file extension defaults as in Intel ifort, gcc(gfortran), NAG, Pathscale, and Cray compilers
45 let b:fortran_fixed_source = 0
Bram Moolenaar2c7f8c52020-04-20 19:52:53 +020046 elseif expand("%:e") =~? '^\%(f\|f77\|for\)$'
Bram Moolenaar256972a2015-12-29 19:10:25 +010047 " Fixed-form file extension defaults
48 let b:fortran_fixed_source = 1
Bram Moolenaar071d4272004-06-13 20:20:40 +000049 else
Bram Moolenaar256972a2015-12-29 19:10:25 +010050 " Modern fortran still allows both fixed and free source form
51 " Assume fixed source form unless signs of free source form
Bram Moolenaar8a94d872015-01-25 13:02:57 +010052 " are detected in the first five columns of the first s:lmax lines.
Bram Moolenaar256972a2015-12-29 19:10:25 +010053 " Detection becomes more accurate and time-consuming if more lines
Bram Moolenaar071d4272004-06-13 20:20:40 +000054 " are checked. Increase the limit below if you keep lots of comments at
Bram Moolenaar256972a2015-12-29 19:10:25 +010055 " the very top of each file and you have a fast computer.
Bram Moolenaarc88ebf72010-07-22 22:30:23 +020056 let s:lmax = 500
Bram Moolenaar071d4272004-06-13 20:20:40 +000057 if ( s:lmax > line("$") )
58 let s:lmax = line("$")
59 endif
60 let b:fortran_fixed_source = 1
61 let s:ln=1
62 while s:ln <= s:lmax
63 let s:test = strpart(getline(s:ln),0,5)
Bram Moolenaarc88ebf72010-07-22 22:30:23 +020064 if s:test !~ '^[Cc*]' && s:test !~ '^ *[!#]' && s:test =~ '[^ 0-9\t]' && s:test !~ '^[ 0-9]*\t'
Bram Moolenaar071d4272004-06-13 20:20:40 +000065 let b:fortran_fixed_source = 0
66 break
67 endif
68 let s:ln = s:ln + 1
69 endwhile
70 endif
71endif
72
73" Define the appropriate indent function but only once
74if (b:fortran_fixed_source == 1)
75 setlocal indentexpr=FortranGetFixedIndent()
76 if exists("*FortranGetFixedIndent")
77 finish
78 endif
79else
80 setlocal indentexpr=FortranGetFreeIndent()
81 if exists("*FortranGetFreeIndent")
82 finish
83 endif
84endif
85
Bram Moolenaar071d4272004-06-13 20:20:40 +000086function FortranGetIndent(lnum)
87 let ind = indent(a:lnum)
88 let prevline=getline(a:lnum)
89 " Strip tail comment
90 let prevstat=substitute(prevline, '!.*$', '', '')
Bram Moolenaar251e1912011-06-19 05:09:16 +020091 let prev2line=getline(a:lnum-1)
92 let prev2stat=substitute(prev2line, '!.*$', '', '')
Bram Moolenaar071d4272004-06-13 20:20:40 +000093
94 "Indent do loops only if they are all guaranteed to be of do/end do type
Bram Moolenaarc81e5e72007-05-05 18:24:42 +000095 if exists("b:fortran_do_enddo") || exists("g:fortran_do_enddo")
Bram Moolenaar071d4272004-06-13 20:20:40 +000096 if prevstat =~? '^\s*\(\d\+\s\)\=\s*\(\a\w*\s*:\)\=\s*do\>'
Bram Moolenaar298b4402016-01-28 22:38:53 +010097 let ind = ind + shiftwidth()
Bram Moolenaar071d4272004-06-13 20:20:40 +000098 endif
99 if getline(v:lnum) =~? '^\s*\(\d\+\s\)\=\s*end\s*do\>'
Bram Moolenaar298b4402016-01-28 22:38:53 +0100100 let ind = ind - shiftwidth()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101 endif
102 endif
103
Bram Moolenaarc0514bf2016-11-17 14:50:09 +0100104 "Add a shiftwidth to statements following if, else, else if, case, class,
Bram Moolenaar251e1912011-06-19 05:09:16 +0200105 "where, else where, forall, type, interface and associate statements
Bram Moolenaarc0514bf2016-11-17 14:50:09 +0100106 if prevstat =~? '^\s*\(case\|class\|else\|else\s*if\|else\s*where\)\>'
Bram Moolenaar251e1912011-06-19 05:09:16 +0200107 \ ||prevstat=~? '^\s*\(type\|interface\|associate\|enum\)\>'
108 \ ||prevstat=~?'^\s*\(\d\+\s\)\=\s*\(\a\w*\s*:\)\=\s*\(forall\|where\|block\)\>'
109 \ ||prevstat=~? '^\s*\(\d\+\s\)\=\s*\(\a\w*\s*:\)\=\s*if\>'
Bram Moolenaar298b4402016-01-28 22:38:53 +0100110 let ind = ind + shiftwidth()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111 " Remove unwanted indent after logical and arithmetic ifs
112 if prevstat =~? '\<if\>' && prevstat !~? '\<then\>'
Bram Moolenaar298b4402016-01-28 22:38:53 +0100113 let ind = ind - shiftwidth()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114 endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000115 " Remove unwanted indent after type( statements
Bram Moolenaar251e1912011-06-19 05:09:16 +0200116 if prevstat =~? '^\s*type\s*('
Bram Moolenaar298b4402016-01-28 22:38:53 +0100117 let ind = ind - shiftwidth()
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000118 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119 endif
120
Bram Moolenaar251e1912011-06-19 05:09:16 +0200121 "Indent program units unless instructed otherwise
122 if !exists("b:fortran_indent_less") && !exists("g:fortran_indent_less")
123 let prefix='\(\(pure\|impure\|elemental\|recursive\)\s\+\)\{,2}'
124 let type='\(\(integer\|real\|double\s\+precision\|complex\|logical'
125 \.'\|character\|type\|class\)\s*\S*\s\+\)\='
Bram Moolenaar2ec618c2016-10-01 14:47:05 +0200126 if prevstat =~? '^\s*\(contains\|submodule\|program\)\>'
127 \ ||prevstat =~? '^\s*'.'module\>\(\s*\procedure\)\@!'
Bram Moolenaar251e1912011-06-19 05:09:16 +0200128 \ ||prevstat =~? '^\s*'.prefix.'subroutine\>'
129 \ ||prevstat =~? '^\s*'.prefix.type.'function\>'
130 \ ||prevstat =~? '^\s*'.type.prefix.'function\>'
Bram Moolenaar298b4402016-01-28 22:38:53 +0100131 let ind = ind + shiftwidth()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132 endif
Bram Moolenaar251e1912011-06-19 05:09:16 +0200133 if getline(v:lnum) =~? '^\s*contains\>'
134 \ ||getline(v:lnum)=~? '^\s*end\s*'
Bram Moolenaar2ec618c2016-10-01 14:47:05 +0200135 \ .'\(function\|subroutine\|module\|submodule\|program\)\>'
Bram Moolenaar298b4402016-01-28 22:38:53 +0100136 let ind = ind - shiftwidth()
Bram Moolenaar251e1912011-06-19 05:09:16 +0200137 endif
138 endif
139
Bram Moolenaarc0514bf2016-11-17 14:50:09 +0100140 "Subtract a shiftwidth from else, else if, elsewhere, case, class, end if,
Bram Moolenaar251e1912011-06-19 05:09:16 +0200141 " end where, end select, end forall, end interface, end associate,
Bram Moolenaare4a3bcf2016-08-26 19:52:37 +0200142 " end enum, end type, end block and end type statements
Bram Moolenaar251e1912011-06-19 05:09:16 +0200143 if getline(v:lnum) =~? '^\s*\(\d\+\s\)\=\s*'
Bram Moolenaarc0514bf2016-11-17 14:50:09 +0100144 \. '\(else\|else\s*if\|else\s*where\|case\|class\|'
Bram Moolenaar251e1912011-06-19 05:09:16 +0200145 \. 'end\s*\(if\|where\|select\|interface\|'
Bram Moolenaar8a94d872015-01-25 13:02:57 +0100146 \. 'type\|forall\|associate\|enum\|block\)\)\>'
Bram Moolenaar298b4402016-01-28 22:38:53 +0100147 let ind = ind - shiftwidth()
Bram Moolenaar251e1912011-06-19 05:09:16 +0200148 " Fix indent for case statement immediately after select
149 if prevstat =~? '\<select\s\+\(case\|type\)\>'
Bram Moolenaar298b4402016-01-28 22:38:53 +0100150 let ind = ind + shiftwidth()
Bram Moolenaar251e1912011-06-19 05:09:16 +0200151 endif
152 endif
153
154 "First continuation line
155 if prevstat =~ '&\s*$' && prev2stat !~ '&\s*$'
Bram Moolenaar298b4402016-01-28 22:38:53 +0100156 let ind = ind + shiftwidth()
Bram Moolenaar251e1912011-06-19 05:09:16 +0200157 endif
158 "Line after last continuation line
Bram Moolenaar8a94d872015-01-25 13:02:57 +0100159 if prevstat !~ '&\s*$' && prev2stat =~ '&\s*$' && prevstat !~? '\<then\>'
Bram Moolenaar298b4402016-01-28 22:38:53 +0100160 let ind = ind - shiftwidth()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161 endif
162
163 return ind
164endfunction
165
166function FortranGetFreeIndent()
167 "Find the previous non-blank line
168 let lnum = prevnonblank(v:lnum - 1)
169
170 "Use zero indent at the top of the file
171 if lnum == 0
172 return 0
173 endif
174
175 let ind=FortranGetIndent(lnum)
176 return ind
177endfunction
178
179function FortranGetFixedIndent()
180 let currline=getline(v:lnum)
181 "Don't indent comments, continuation lines and labelled lines
182 if strpart(currline,0,6) =~ '[^ \t]'
183 let ind = indent(v:lnum)
184 return ind
185 endif
186
187 "Find the previous line which is not blank, not a comment,
188 "not a continuation line, and does not have a label
189 let lnum = v:lnum - 1
190 while lnum > 0
191 let prevline=getline(lnum)
192 if (prevline =~ "^[C*!]") || (prevline =~ "^\s*$")
193 \ || (strpart(prevline,5,1) !~ "[ 0]")
194 " Skip comments, blank lines and continuation lines
195 let lnum = lnum - 1
196 else
197 let test=strpart(prevline,0,5)
198 if test =~ "[0-9]"
199 " Skip lines with statement numbers
200 let lnum = lnum - 1
201 else
202 break
203 endif
204 endif
205 endwhile
206
207 "First line must begin at column 7
208 if lnum == 0
209 return 6
210 endif
211
212 let ind=FortranGetIndent(lnum)
213 return ind
214endfunction
215
216let &cpoptions=s:cposet
217unlet s:cposet
218
219" vim:sw=2 tw=130