blob: 7130658f593692817918e0305e4e9f58c43ba30f [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001" Vim indent file
Ajit-Thakkar68630842023-12-05 23:07:27 +01002" Language: Fortran 2008 (and Fortran 2003, 95, 90, and 77)
3" Version: (v49) 2023 December 1
4" Maintainers: Ajit J. Thakkar <ajit@unb.ca>; <https://ajit.ext.unb.ca/>
5" Joshua Hollett <j.hollett@uwinnipeg.ca>
Bram Moolenaar256972a2015-12-29 19:10:25 +01006" Usage: For instructions, do :help fortran-indent from Vim
Bram Moolenaar8a94d872015-01-25 13:02:57 +01007" Credits:
Bram Moolenaar4f4d51a2020-10-11 13:57:40 +02008" Version 0.1 was created in September 2000 by Ajit Thakkar.
9" Since then, useful suggestions and contributions have been made, in order, by:
10" Albert Oliver Serra, Takuya Fujiwara, Philipp Edelmann, Eisuke Kawashima,
Bram Moolenaar2d8ed022022-05-21 13:08:16 +010011" Louis Cochen, and Doug Kearns.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012
13" Only load this indent file when no other was loaded.
14if exists("b:did_indent")
15 finish
16endif
17let b:did_indent = 1
18
Bram Moolenaarb6b046b2011-12-30 13:11:27 +010019let s:cposet=&cpoptions
20set cpoptions&vim
Bram Moolenaar2d8ed022022-05-21 13:08:16 +010021let b:undo_indent = "setl inde< indk<"
Bram Moolenaarb6b046b2011-12-30 13:11:27 +010022
Bram Moolenaar071d4272004-06-13 20:20:40 +000023setlocal indentkeys+==~end,=~case,=~if,=~else,=~do,=~where,=~elsewhere,=~select
Bram Moolenaar251e1912011-06-19 05:09:16 +020024setlocal indentkeys+==~endif,=~enddo,=~endwhere,=~endselect,=~elseif
25setlocal indentkeys+==~type,=~interface,=~forall,=~associate,=~block,=~enum
26setlocal indentkeys+==~endforall,=~endassociate,=~endblock,=~endenum
27if exists("b:fortran_indent_more") || exists("g:fortran_indent_more")
28 setlocal indentkeys+==~function,=~subroutine,=~module,=~contains,=~program
29 setlocal indentkeys+==~endfunction,=~endsubroutine,=~endmodule
30 setlocal indentkeys+==~endprogram
31endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000032
33" Determine whether this is a fixed or free format source file
Bram Moolenaar256972a2015-12-29 19:10:25 +010034" if this hasn't been done yet using the priority:
35" buffer-local value
36" > global value
37" > file extension as in Intel ifort, gcc (gfortran), NAG, Pathscale, and Cray compilers
Bram Moolenaar071d4272004-06-13 20:20:40 +000038if !exists("b:fortran_fixed_source")
39 if exists("fortran_free_source")
40 " User guarantees free source form
41 let b:fortran_fixed_source = 0
42 elseif exists("fortran_fixed_source")
43 " User guarantees fixed source form
44 let b:fortran_fixed_source = 1
Bram Moolenaar2c7f8c52020-04-20 19:52:53 +020045 elseif expand("%:e") =~? '^f\%(90\|95\|03\|08\)$'
Bram Moolenaar256972a2015-12-29 19:10:25 +010046 " Free-form file extension defaults as in Intel ifort, gcc(gfortran), NAG, Pathscale, and Cray compilers
47 let b:fortran_fixed_source = 0
Bram Moolenaar2c7f8c52020-04-20 19:52:53 +020048 elseif expand("%:e") =~? '^\%(f\|f77\|for\)$'
Bram Moolenaar256972a2015-12-29 19:10:25 +010049 " Fixed-form file extension defaults
50 let b:fortran_fixed_source = 1
Bram Moolenaar071d4272004-06-13 20:20:40 +000051 else
Bram Moolenaar256972a2015-12-29 19:10:25 +010052 " Modern fortran still allows both fixed and free source form
53 " Assume fixed source form unless signs of free source form
Bram Moolenaar8a94d872015-01-25 13:02:57 +010054 " are detected in the first five columns of the first s:lmax lines.
Bram Moolenaar256972a2015-12-29 19:10:25 +010055 " Detection becomes more accurate and time-consuming if more lines
Bram Moolenaar071d4272004-06-13 20:20:40 +000056 " are checked. Increase the limit below if you keep lots of comments at
Bram Moolenaar256972a2015-12-29 19:10:25 +010057 " the very top of each file and you have a fast computer.
Bram Moolenaarc88ebf72010-07-22 22:30:23 +020058 let s:lmax = 500
Bram Moolenaar071d4272004-06-13 20:20:40 +000059 if ( s:lmax > line("$") )
60 let s:lmax = line("$")
61 endif
62 let b:fortran_fixed_source = 1
63 let s:ln=1
64 while s:ln <= s:lmax
65 let s:test = strpart(getline(s:ln),0,5)
Bram Moolenaarc88ebf72010-07-22 22:30:23 +020066 if s:test !~ '^[Cc*]' && s:test !~ '^ *[!#]' && s:test =~ '[^ 0-9\t]' && s:test !~ '^[ 0-9]*\t'
Bram Moolenaar071d4272004-06-13 20:20:40 +000067 let b:fortran_fixed_source = 0
68 break
69 endif
70 let s:ln = s:ln + 1
71 endwhile
72 endif
73endif
74
75" Define the appropriate indent function but only once
76if (b:fortran_fixed_source == 1)
77 setlocal indentexpr=FortranGetFixedIndent()
78 if exists("*FortranGetFixedIndent")
Bram Moolenaare0e39172021-01-25 21:14:57 +010079 let &cpoptions = s:cposet
80 unlet s:cposet
Bram Moolenaar071d4272004-06-13 20:20:40 +000081 finish
82 endif
83else
84 setlocal indentexpr=FortranGetFreeIndent()
85 if exists("*FortranGetFreeIndent")
Bram Moolenaare0e39172021-01-25 21:14:57 +010086 let &cpoptions = s:cposet
87 unlet s:cposet
Bram Moolenaar071d4272004-06-13 20:20:40 +000088 finish
89 endif
90endif
91
Bram Moolenaar071d4272004-06-13 20:20:40 +000092function FortranGetIndent(lnum)
93 let ind = indent(a:lnum)
94 let prevline=getline(a:lnum)
95 " Strip tail comment
96 let prevstat=substitute(prevline, '!.*$', '', '')
Bram Moolenaar251e1912011-06-19 05:09:16 +020097 let prev2line=getline(a:lnum-1)
98 let prev2stat=substitute(prev2line, '!.*$', '', '')
Bram Moolenaar071d4272004-06-13 20:20:40 +000099
100 "Indent do loops only if they are all guaranteed to be of do/end do type
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000101 if exists("b:fortran_do_enddo") || exists("g:fortran_do_enddo")
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102 if prevstat =~? '^\s*\(\d\+\s\)\=\s*\(\a\w*\s*:\)\=\s*do\>'
Bram Moolenaar298b4402016-01-28 22:38:53 +0100103 let ind = ind + shiftwidth()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104 endif
105 if getline(v:lnum) =~? '^\s*\(\d\+\s\)\=\s*end\s*do\>'
Bram Moolenaar298b4402016-01-28 22:38:53 +0100106 let ind = ind - shiftwidth()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107 endif
108 endif
109
Bram Moolenaarc0514bf2016-11-17 14:50:09 +0100110 "Add a shiftwidth to statements following if, else, else if, case, class,
Bram Moolenaar251e1912011-06-19 05:09:16 +0200111 "where, else where, forall, type, interface and associate statements
Bram Moolenaarc0514bf2016-11-17 14:50:09 +0100112 if prevstat =~? '^\s*\(case\|class\|else\|else\s*if\|else\s*where\)\>'
Bram Moolenaar251e1912011-06-19 05:09:16 +0200113 \ ||prevstat=~? '^\s*\(type\|interface\|associate\|enum\)\>'
114 \ ||prevstat=~?'^\s*\(\d\+\s\)\=\s*\(\a\w*\s*:\)\=\s*\(forall\|where\|block\)\>'
115 \ ||prevstat=~? '^\s*\(\d\+\s\)\=\s*\(\a\w*\s*:\)\=\s*if\>'
Bram Moolenaar298b4402016-01-28 22:38:53 +0100116 let ind = ind + shiftwidth()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117 " Remove unwanted indent after logical and arithmetic ifs
118 if prevstat =~? '\<if\>' && prevstat !~? '\<then\>'
Bram Moolenaar298b4402016-01-28 22:38:53 +0100119 let ind = ind - shiftwidth()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120 endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000121 " Remove unwanted indent after type( statements
Bram Moolenaar251e1912011-06-19 05:09:16 +0200122 if prevstat =~? '^\s*type\s*('
Bram Moolenaar298b4402016-01-28 22:38:53 +0100123 let ind = ind - shiftwidth()
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000124 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125 endif
126
Bram Moolenaar251e1912011-06-19 05:09:16 +0200127 "Indent program units unless instructed otherwise
128 if !exists("b:fortran_indent_less") && !exists("g:fortran_indent_less")
129 let prefix='\(\(pure\|impure\|elemental\|recursive\)\s\+\)\{,2}'
130 let type='\(\(integer\|real\|double\s\+precision\|complex\|logical'
131 \.'\|character\|type\|class\)\s*\S*\s\+\)\='
Bram Moolenaar2ec618c2016-10-01 14:47:05 +0200132 if prevstat =~? '^\s*\(contains\|submodule\|program\)\>'
133 \ ||prevstat =~? '^\s*'.'module\>\(\s*\procedure\)\@!'
Bram Moolenaar251e1912011-06-19 05:09:16 +0200134 \ ||prevstat =~? '^\s*'.prefix.'subroutine\>'
135 \ ||prevstat =~? '^\s*'.prefix.type.'function\>'
136 \ ||prevstat =~? '^\s*'.type.prefix.'function\>'
Bram Moolenaar298b4402016-01-28 22:38:53 +0100137 let ind = ind + shiftwidth()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138 endif
Bram Moolenaar251e1912011-06-19 05:09:16 +0200139 if getline(v:lnum) =~? '^\s*contains\>'
140 \ ||getline(v:lnum)=~? '^\s*end\s*'
Bram Moolenaar2ec618c2016-10-01 14:47:05 +0200141 \ .'\(function\|subroutine\|module\|submodule\|program\)\>'
Bram Moolenaar298b4402016-01-28 22:38:53 +0100142 let ind = ind - shiftwidth()
Bram Moolenaar251e1912011-06-19 05:09:16 +0200143 endif
144 endif
145
Bram Moolenaarc0514bf2016-11-17 14:50:09 +0100146 "Subtract a shiftwidth from else, else if, elsewhere, case, class, end if,
Bram Moolenaar251e1912011-06-19 05:09:16 +0200147 " end where, end select, end forall, end interface, end associate,
Bram Moolenaare4a3bcf2016-08-26 19:52:37 +0200148 " end enum, end type, end block and end type statements
Bram Moolenaar251e1912011-06-19 05:09:16 +0200149 if getline(v:lnum) =~? '^\s*\(\d\+\s\)\=\s*'
Bram Moolenaarc0514bf2016-11-17 14:50:09 +0100150 \. '\(else\|else\s*if\|else\s*where\|case\|class\|'
Bram Moolenaar251e1912011-06-19 05:09:16 +0200151 \. 'end\s*\(if\|where\|select\|interface\|'
Bram Moolenaar8a94d872015-01-25 13:02:57 +0100152 \. 'type\|forall\|associate\|enum\|block\)\)\>'
Bram Moolenaar298b4402016-01-28 22:38:53 +0100153 let ind = ind - shiftwidth()
Bram Moolenaar251e1912011-06-19 05:09:16 +0200154 " Fix indent for case statement immediately after select
Bram Moolenaar4f4d51a2020-10-11 13:57:40 +0200155 if prevstat =~? '\<select\s*\(case\|type\)\>'
Bram Moolenaar298b4402016-01-28 22:38:53 +0100156 let ind = ind + shiftwidth()
Bram Moolenaar251e1912011-06-19 05:09:16 +0200157 endif
158 endif
159
160 "First continuation line
161 if prevstat =~ '&\s*$' && prev2stat !~ '&\s*$'
Bram Moolenaar298b4402016-01-28 22:38:53 +0100162 let ind = ind + shiftwidth()
Bram Moolenaar251e1912011-06-19 05:09:16 +0200163 endif
164 "Line after last continuation line
Bram Moolenaar8a94d872015-01-25 13:02:57 +0100165 if prevstat !~ '&\s*$' && prev2stat =~ '&\s*$' && prevstat !~? '\<then\>'
Bram Moolenaar298b4402016-01-28 22:38:53 +0100166 let ind = ind - shiftwidth()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167 endif
168
169 return ind
170endfunction
171
172function FortranGetFreeIndent()
173 "Find the previous non-blank line
174 let lnum = prevnonblank(v:lnum - 1)
175
176 "Use zero indent at the top of the file
177 if lnum == 0
178 return 0
179 endif
180
181 let ind=FortranGetIndent(lnum)
182 return ind
183endfunction
184
185function FortranGetFixedIndent()
186 let currline=getline(v:lnum)
187 "Don't indent comments, continuation lines and labelled lines
188 if strpart(currline,0,6) =~ '[^ \t]'
189 let ind = indent(v:lnum)
190 return ind
191 endif
192
193 "Find the previous line which is not blank, not a comment,
194 "not a continuation line, and does not have a label
195 let lnum = v:lnum - 1
196 while lnum > 0
197 let prevline=getline(lnum)
198 if (prevline =~ "^[C*!]") || (prevline =~ "^\s*$")
199 \ || (strpart(prevline,5,1) !~ "[ 0]")
200 " Skip comments, blank lines and continuation lines
201 let lnum = lnum - 1
202 else
203 let test=strpart(prevline,0,5)
204 if test =~ "[0-9]"
205 " Skip lines with statement numbers
206 let lnum = lnum - 1
207 else
208 break
209 endif
210 endif
211 endwhile
212
213 "First line must begin at column 7
214 if lnum == 0
215 return 6
216 endif
217
218 let ind=FortranGetIndent(lnum)
219 return ind
220endfunction
221
Bram Moolenaare0e39172021-01-25 21:14:57 +0100222let &cpoptions = s:cposet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223unlet s:cposet
224
225" vim:sw=2 tw=130