blob: 9623014818def64fa3bfb7224633d62f273f51ce [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 Moolenaar2d8ed022022-05-21 13:08:16 +01003" Version: (v49) 2022 May 14
4" Maintainer: Ajit J. Thakkar <thakkar.ajit@gmail.com>; <http://www2.unb.ca/~ajit/>
Bram Moolenaar256972a2015-12-29 19:10:25 +01005" Usage: For instructions, do :help fortran-indent from Vim
Bram Moolenaar8a94d872015-01-25 13:02:57 +01006" Credits:
Bram Moolenaar4f4d51a2020-10-11 13:57:40 +02007" Version 0.1 was created in September 2000 by Ajit Thakkar.
8" Since then, useful suggestions and contributions have been made, in order, by:
9" Albert Oliver Serra, Takuya Fujiwara, Philipp Edelmann, Eisuke Kawashima,
Bram Moolenaar2d8ed022022-05-21 13:08:16 +010010" Louis Cochen, and Doug Kearns.
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
Bram Moolenaar2d8ed022022-05-21 13:08:16 +010020let b:undo_indent = "setl inde< indk<"
Bram Moolenaarb6b046b2011-12-30 13:11:27 +010021
Bram Moolenaar071d4272004-06-13 20:20:40 +000022setlocal indentkeys+==~end,=~case,=~if,=~else,=~do,=~where,=~elsewhere,=~select
Bram Moolenaar251e1912011-06-19 05:09:16 +020023setlocal indentkeys+==~endif,=~enddo,=~endwhere,=~endselect,=~elseif
24setlocal indentkeys+==~type,=~interface,=~forall,=~associate,=~block,=~enum
25setlocal indentkeys+==~endforall,=~endassociate,=~endblock,=~endenum
26if exists("b:fortran_indent_more") || exists("g:fortran_indent_more")
27 setlocal indentkeys+==~function,=~subroutine,=~module,=~contains,=~program
28 setlocal indentkeys+==~endfunction,=~endsubroutine,=~endmodule
29 setlocal indentkeys+==~endprogram
30endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000031
32" Determine whether this is a fixed or free format source file
Bram Moolenaar256972a2015-12-29 19:10:25 +010033" if this hasn't been done yet using the priority:
34" buffer-local value
35" > global value
36" > file extension as in Intel ifort, gcc (gfortran), NAG, Pathscale, and Cray compilers
Bram Moolenaar071d4272004-06-13 20:20:40 +000037if !exists("b:fortran_fixed_source")
38 if exists("fortran_free_source")
39 " User guarantees free source form
40 let b:fortran_fixed_source = 0
41 elseif exists("fortran_fixed_source")
42 " User guarantees fixed source form
43 let b:fortran_fixed_source = 1
Bram Moolenaar2c7f8c52020-04-20 19:52:53 +020044 elseif expand("%:e") =~? '^f\%(90\|95\|03\|08\)$'
Bram Moolenaar256972a2015-12-29 19:10:25 +010045 " Free-form file extension defaults as in Intel ifort, gcc(gfortran), NAG, Pathscale, and Cray compilers
46 let b:fortran_fixed_source = 0
Bram Moolenaar2c7f8c52020-04-20 19:52:53 +020047 elseif expand("%:e") =~? '^\%(f\|f77\|for\)$'
Bram Moolenaar256972a2015-12-29 19:10:25 +010048 " Fixed-form file extension defaults
49 let b:fortran_fixed_source = 1
Bram Moolenaar071d4272004-06-13 20:20:40 +000050 else
Bram Moolenaar256972a2015-12-29 19:10:25 +010051 " Modern fortran still allows both fixed and free source form
52 " Assume fixed source form unless signs of free source form
Bram Moolenaar8a94d872015-01-25 13:02:57 +010053 " are detected in the first five columns of the first s:lmax lines.
Bram Moolenaar256972a2015-12-29 19:10:25 +010054 " Detection becomes more accurate and time-consuming if more lines
Bram Moolenaar071d4272004-06-13 20:20:40 +000055 " are checked. Increase the limit below if you keep lots of comments at
Bram Moolenaar256972a2015-12-29 19:10:25 +010056 " the very top of each file and you have a fast computer.
Bram Moolenaarc88ebf72010-07-22 22:30:23 +020057 let s:lmax = 500
Bram Moolenaar071d4272004-06-13 20:20:40 +000058 if ( s:lmax > line("$") )
59 let s:lmax = line("$")
60 endif
61 let b:fortran_fixed_source = 1
62 let s:ln=1
63 while s:ln <= s:lmax
64 let s:test = strpart(getline(s:ln),0,5)
Bram Moolenaarc88ebf72010-07-22 22:30:23 +020065 if s:test !~ '^[Cc*]' && s:test !~ '^ *[!#]' && s:test =~ '[^ 0-9\t]' && s:test !~ '^[ 0-9]*\t'
Bram Moolenaar071d4272004-06-13 20:20:40 +000066 let b:fortran_fixed_source = 0
67 break
68 endif
69 let s:ln = s:ln + 1
70 endwhile
71 endif
72endif
73
74" Define the appropriate indent function but only once
75if (b:fortran_fixed_source == 1)
76 setlocal indentexpr=FortranGetFixedIndent()
77 if exists("*FortranGetFixedIndent")
Bram Moolenaare0e39172021-01-25 21:14:57 +010078 let &cpoptions = s:cposet
79 unlet s:cposet
Bram Moolenaar071d4272004-06-13 20:20:40 +000080 finish
81 endif
82else
83 setlocal indentexpr=FortranGetFreeIndent()
84 if exists("*FortranGetFreeIndent")
Bram Moolenaare0e39172021-01-25 21:14:57 +010085 let &cpoptions = s:cposet
86 unlet s:cposet
Bram Moolenaar071d4272004-06-13 20:20:40 +000087 finish
88 endif
89endif
90
Bram Moolenaar071d4272004-06-13 20:20:40 +000091function FortranGetIndent(lnum)
92 let ind = indent(a:lnum)
93 let prevline=getline(a:lnum)
94 " Strip tail comment
95 let prevstat=substitute(prevline, '!.*$', '', '')
Bram Moolenaar251e1912011-06-19 05:09:16 +020096 let prev2line=getline(a:lnum-1)
97 let prev2stat=substitute(prev2line, '!.*$', '', '')
Bram Moolenaar071d4272004-06-13 20:20:40 +000098
99 "Indent do loops only if they are all guaranteed to be of do/end do type
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000100 if exists("b:fortran_do_enddo") || exists("g:fortran_do_enddo")
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101 if prevstat =~? '^\s*\(\d\+\s\)\=\s*\(\a\w*\s*:\)\=\s*do\>'
Bram Moolenaar298b4402016-01-28 22:38:53 +0100102 let ind = ind + shiftwidth()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103 endif
104 if getline(v:lnum) =~? '^\s*\(\d\+\s\)\=\s*end\s*do\>'
Bram Moolenaar298b4402016-01-28 22:38:53 +0100105 let ind = ind - shiftwidth()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106 endif
107 endif
108
Bram Moolenaarc0514bf2016-11-17 14:50:09 +0100109 "Add a shiftwidth to statements following if, else, else if, case, class,
Bram Moolenaar251e1912011-06-19 05:09:16 +0200110 "where, else where, forall, type, interface and associate statements
Bram Moolenaarc0514bf2016-11-17 14:50:09 +0100111 if prevstat =~? '^\s*\(case\|class\|else\|else\s*if\|else\s*where\)\>'
Bram Moolenaar251e1912011-06-19 05:09:16 +0200112 \ ||prevstat=~? '^\s*\(type\|interface\|associate\|enum\)\>'
113 \ ||prevstat=~?'^\s*\(\d\+\s\)\=\s*\(\a\w*\s*:\)\=\s*\(forall\|where\|block\)\>'
114 \ ||prevstat=~? '^\s*\(\d\+\s\)\=\s*\(\a\w*\s*:\)\=\s*if\>'
Bram Moolenaar298b4402016-01-28 22:38:53 +0100115 let ind = ind + shiftwidth()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116 " Remove unwanted indent after logical and arithmetic ifs
117 if prevstat =~? '\<if\>' && prevstat !~? '\<then\>'
Bram Moolenaar298b4402016-01-28 22:38:53 +0100118 let ind = ind - shiftwidth()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119 endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000120 " Remove unwanted indent after type( statements
Bram Moolenaar251e1912011-06-19 05:09:16 +0200121 if prevstat =~? '^\s*type\s*('
Bram Moolenaar298b4402016-01-28 22:38:53 +0100122 let ind = ind - shiftwidth()
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000123 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124 endif
125
Bram Moolenaar251e1912011-06-19 05:09:16 +0200126 "Indent program units unless instructed otherwise
127 if !exists("b:fortran_indent_less") && !exists("g:fortran_indent_less")
128 let prefix='\(\(pure\|impure\|elemental\|recursive\)\s\+\)\{,2}'
129 let type='\(\(integer\|real\|double\s\+precision\|complex\|logical'
130 \.'\|character\|type\|class\)\s*\S*\s\+\)\='
Bram Moolenaar2ec618c2016-10-01 14:47:05 +0200131 if prevstat =~? '^\s*\(contains\|submodule\|program\)\>'
132 \ ||prevstat =~? '^\s*'.'module\>\(\s*\procedure\)\@!'
Bram Moolenaar251e1912011-06-19 05:09:16 +0200133 \ ||prevstat =~? '^\s*'.prefix.'subroutine\>'
134 \ ||prevstat =~? '^\s*'.prefix.type.'function\>'
135 \ ||prevstat =~? '^\s*'.type.prefix.'function\>'
Bram Moolenaar298b4402016-01-28 22:38:53 +0100136 let ind = ind + shiftwidth()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137 endif
Bram Moolenaar251e1912011-06-19 05:09:16 +0200138 if getline(v:lnum) =~? '^\s*contains\>'
139 \ ||getline(v:lnum)=~? '^\s*end\s*'
Bram Moolenaar2ec618c2016-10-01 14:47:05 +0200140 \ .'\(function\|subroutine\|module\|submodule\|program\)\>'
Bram Moolenaar298b4402016-01-28 22:38:53 +0100141 let ind = ind - shiftwidth()
Bram Moolenaar251e1912011-06-19 05:09:16 +0200142 endif
143 endif
144
Bram Moolenaarc0514bf2016-11-17 14:50:09 +0100145 "Subtract a shiftwidth from else, else if, elsewhere, case, class, end if,
Bram Moolenaar251e1912011-06-19 05:09:16 +0200146 " end where, end select, end forall, end interface, end associate,
Bram Moolenaare4a3bcf2016-08-26 19:52:37 +0200147 " end enum, end type, end block and end type statements
Bram Moolenaar251e1912011-06-19 05:09:16 +0200148 if getline(v:lnum) =~? '^\s*\(\d\+\s\)\=\s*'
Bram Moolenaarc0514bf2016-11-17 14:50:09 +0100149 \. '\(else\|else\s*if\|else\s*where\|case\|class\|'
Bram Moolenaar251e1912011-06-19 05:09:16 +0200150 \. 'end\s*\(if\|where\|select\|interface\|'
Bram Moolenaar8a94d872015-01-25 13:02:57 +0100151 \. 'type\|forall\|associate\|enum\|block\)\)\>'
Bram Moolenaar298b4402016-01-28 22:38:53 +0100152 let ind = ind - shiftwidth()
Bram Moolenaar251e1912011-06-19 05:09:16 +0200153 " Fix indent for case statement immediately after select
Bram Moolenaar4f4d51a2020-10-11 13:57:40 +0200154 if prevstat =~? '\<select\s*\(case\|type\)\>'
Bram Moolenaar298b4402016-01-28 22:38:53 +0100155 let ind = ind + shiftwidth()
Bram Moolenaar251e1912011-06-19 05:09:16 +0200156 endif
157 endif
158
159 "First continuation line
160 if prevstat =~ '&\s*$' && prev2stat !~ '&\s*$'
Bram Moolenaar298b4402016-01-28 22:38:53 +0100161 let ind = ind + shiftwidth()
Bram Moolenaar251e1912011-06-19 05:09:16 +0200162 endif
163 "Line after last continuation line
Bram Moolenaar8a94d872015-01-25 13:02:57 +0100164 if prevstat !~ '&\s*$' && prev2stat =~ '&\s*$' && prevstat !~? '\<then\>'
Bram Moolenaar298b4402016-01-28 22:38:53 +0100165 let ind = ind - shiftwidth()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166 endif
167
168 return ind
169endfunction
170
171function FortranGetFreeIndent()
172 "Find the previous non-blank line
173 let lnum = prevnonblank(v:lnum - 1)
174
175 "Use zero indent at the top of the file
176 if lnum == 0
177 return 0
178 endif
179
180 let ind=FortranGetIndent(lnum)
181 return ind
182endfunction
183
184function FortranGetFixedIndent()
185 let currline=getline(v:lnum)
186 "Don't indent comments, continuation lines and labelled lines
187 if strpart(currline,0,6) =~ '[^ \t]'
188 let ind = indent(v:lnum)
189 return ind
190 endif
191
192 "Find the previous line which is not blank, not a comment,
193 "not a continuation line, and does not have a label
194 let lnum = v:lnum - 1
195 while lnum > 0
196 let prevline=getline(lnum)
197 if (prevline =~ "^[C*!]") || (prevline =~ "^\s*$")
198 \ || (strpart(prevline,5,1) !~ "[ 0]")
199 " Skip comments, blank lines and continuation lines
200 let lnum = lnum - 1
201 else
202 let test=strpart(prevline,0,5)
203 if test =~ "[0-9]"
204 " Skip lines with statement numbers
205 let lnum = lnum - 1
206 else
207 break
208 endif
209 endif
210 endwhile
211
212 "First line must begin at column 7
213 if lnum == 0
214 return 6
215 endif
216
217 let ind=FortranGetIndent(lnum)
218 return ind
219endfunction
220
Bram Moolenaare0e39172021-01-25 21:14:57 +0100221let &cpoptions = s:cposet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222unlet s:cposet
223
224" vim:sw=2 tw=130