Mercurial > vim
annotate runtime/indent/fortran.vim @ 32215:4b6c544c41f6
Added tag v9.0.1438 for changeset a4dafbd8b7d5d49a172a71e5553fa20837456a09
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Sun, 02 Apr 2023 21:30:07 +0200 |
parents | 57c9377b9c62 |
children | cf2ae76cbbf7 |
rev | line source |
---|---|
7 | 1 " Vim indent file |
7384
aea5ebf352c4
commit https://github.com/vim/vim/commit/256972a9849b5d575b62a6a71be5b6934b5b0e8b
Christian Brabandt <cb@256bit.org>
parents:
6530
diff
changeset
|
2 " Language: Fortran 2008 (and older: Fortran 2003, 95, 90, and 77) |
28933 | 3 " Version: (v49) 2022 May 14 |
4 " Maintainer: Ajit J. Thakkar <thakkar.ajit@gmail.com>; <http://www2.unb.ca/~ajit/> | |
7384
aea5ebf352c4
commit https://github.com/vim/vim/commit/256972a9849b5d575b62a6a71be5b6934b5b0e8b
Christian Brabandt <cb@256bit.org>
parents:
6530
diff
changeset
|
5 " Usage: For instructions, do :help fortran-indent from Vim |
6530 | 6 " Credits: |
22565 | 7 " 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, | |
28933 | 10 " Louis Cochen, and Doug Kearns. |
7 | 11 |
12 " Only load this indent file when no other was loaded. | |
13 if exists("b:did_indent") | |
14 finish | |
15 endif | |
16 let b:did_indent = 1 | |
17 | |
3237 | 18 let s:cposet=&cpoptions |
19 set cpoptions&vim | |
28933 | 20 let b:undo_indent = "setl inde< indk<" |
3237 | 21 |
7 | 22 setlocal indentkeys+==~end,=~case,=~if,=~else,=~do,=~where,=~elsewhere,=~select |
2908 | 23 setlocal indentkeys+==~endif,=~enddo,=~endwhere,=~endselect,=~elseif |
24 setlocal indentkeys+==~type,=~interface,=~forall,=~associate,=~block,=~enum | |
25 setlocal indentkeys+==~endforall,=~endassociate,=~endblock,=~endenum | |
26 if 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 | |
30 endif | |
7 | 31 |
32 " Determine whether this is a fixed or free format source file | |
7384
aea5ebf352c4
commit https://github.com/vim/vim/commit/256972a9849b5d575b62a6a71be5b6934b5b0e8b
Christian Brabandt <cb@256bit.org>
parents:
6530
diff
changeset
|
33 " if this hasn't been done yet using the priority: |
aea5ebf352c4
commit https://github.com/vim/vim/commit/256972a9849b5d575b62a6a71be5b6934b5b0e8b
Christian Brabandt <cb@256bit.org>
parents:
6530
diff
changeset
|
34 " buffer-local value |
aea5ebf352c4
commit https://github.com/vim/vim/commit/256972a9849b5d575b62a6a71be5b6934b5b0e8b
Christian Brabandt <cb@256bit.org>
parents:
6530
diff
changeset
|
35 " > global value |
aea5ebf352c4
commit https://github.com/vim/vim/commit/256972a9849b5d575b62a6a71be5b6934b5b0e8b
Christian Brabandt <cb@256bit.org>
parents:
6530
diff
changeset
|
36 " > file extension as in Intel ifort, gcc (gfortran), NAG, Pathscale, and Cray compilers |
7 | 37 if !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 | |
20115 | 44 elseif expand("%:e") =~? '^f\%(90\|95\|03\|08\)$' |
7384
aea5ebf352c4
commit https://github.com/vim/vim/commit/256972a9849b5d575b62a6a71be5b6934b5b0e8b
Christian Brabandt <cb@256bit.org>
parents:
6530
diff
changeset
|
45 " Free-form file extension defaults as in Intel ifort, gcc(gfortran), NAG, Pathscale, and Cray compilers |
aea5ebf352c4
commit https://github.com/vim/vim/commit/256972a9849b5d575b62a6a71be5b6934b5b0e8b
Christian Brabandt <cb@256bit.org>
parents:
6530
diff
changeset
|
46 let b:fortran_fixed_source = 0 |
20115 | 47 elseif expand("%:e") =~? '^\%(f\|f77\|for\)$' |
7384
aea5ebf352c4
commit https://github.com/vim/vim/commit/256972a9849b5d575b62a6a71be5b6934b5b0e8b
Christian Brabandt <cb@256bit.org>
parents:
6530
diff
changeset
|
48 " Fixed-form file extension defaults |
aea5ebf352c4
commit https://github.com/vim/vim/commit/256972a9849b5d575b62a6a71be5b6934b5b0e8b
Christian Brabandt <cb@256bit.org>
parents:
6530
diff
changeset
|
49 let b:fortran_fixed_source = 1 |
7 | 50 else |
7384
aea5ebf352c4
commit https://github.com/vim/vim/commit/256972a9849b5d575b62a6a71be5b6934b5b0e8b
Christian Brabandt <cb@256bit.org>
parents:
6530
diff
changeset
|
51 " Modern fortran still allows both fixed and free source form |
aea5ebf352c4
commit https://github.com/vim/vim/commit/256972a9849b5d575b62a6a71be5b6934b5b0e8b
Christian Brabandt <cb@256bit.org>
parents:
6530
diff
changeset
|
52 " Assume fixed source form unless signs of free source form |
6530 | 53 " are detected in the first five columns of the first s:lmax lines. |
7384
aea5ebf352c4
commit https://github.com/vim/vim/commit/256972a9849b5d575b62a6a71be5b6934b5b0e8b
Christian Brabandt <cb@256bit.org>
parents:
6530
diff
changeset
|
54 " Detection becomes more accurate and time-consuming if more lines |
7 | 55 " are checked. Increase the limit below if you keep lots of comments at |
7384
aea5ebf352c4
commit https://github.com/vim/vim/commit/256972a9849b5d575b62a6a71be5b6934b5b0e8b
Christian Brabandt <cb@256bit.org>
parents:
6530
diff
changeset
|
56 " the very top of each file and you have a fast computer. |
2377
878562053ba3
Update Fortran indent and syntax file. (Ajit Thakkar)
Bram Moolenaar <bram@vim.org>
parents:
1125
diff
changeset
|
57 let s:lmax = 500 |
7 | 58 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) | |
2377
878562053ba3
Update Fortran indent and syntax file. (Ajit Thakkar)
Bram Moolenaar <bram@vim.org>
parents:
1125
diff
changeset
|
65 if s:test !~ '^[Cc*]' && s:test !~ '^ *[!#]' && s:test =~ '[^ 0-9\t]' && s:test !~ '^[ 0-9]*\t' |
7 | 66 let b:fortran_fixed_source = 0 |
67 break | |
68 endif | |
69 let s:ln = s:ln + 1 | |
70 endwhile | |
71 endif | |
72 endif | |
73 | |
74 " Define the appropriate indent function but only once | |
75 if (b:fortran_fixed_source == 1) | |
76 setlocal indentexpr=FortranGetFixedIndent() | |
77 if exists("*FortranGetFixedIndent") | |
23737 | 78 let &cpoptions = s:cposet |
79 unlet s:cposet | |
7 | 80 finish |
81 endif | |
82 else | |
83 setlocal indentexpr=FortranGetFreeIndent() | |
84 if exists("*FortranGetFreeIndent") | |
23737 | 85 let &cpoptions = s:cposet |
86 unlet s:cposet | |
7 | 87 finish |
88 endif | |
89 endif | |
90 | |
91 function FortranGetIndent(lnum) | |
92 let ind = indent(a:lnum) | |
93 let prevline=getline(a:lnum) | |
94 " Strip tail comment | |
95 let prevstat=substitute(prevline, '!.*$', '', '') | |
2908 | 96 let prev2line=getline(a:lnum-1) |
97 let prev2stat=substitute(prev2line, '!.*$', '', '') | |
7 | 98 |
99 "Indent do loops only if they are all guaranteed to be of do/end do type | |
1125 | 100 if exists("b:fortran_do_enddo") || exists("g:fortran_do_enddo") |
7 | 101 if prevstat =~? '^\s*\(\d\+\s\)\=\s*\(\a\w*\s*:\)\=\s*do\>' |
7790
ca19726d5e83
commit https://github.com/vim/vim/commit/298b440930ecece38d6ea0505a3e582dc817e79b
Christian Brabandt <cb@256bit.org>
parents:
7384
diff
changeset
|
102 let ind = ind + shiftwidth() |
7 | 103 endif |
104 if getline(v:lnum) =~? '^\s*\(\d\+\s\)\=\s*end\s*do\>' | |
7790
ca19726d5e83
commit https://github.com/vim/vim/commit/298b440930ecece38d6ea0505a3e582dc817e79b
Christian Brabandt <cb@256bit.org>
parents:
7384
diff
changeset
|
105 let ind = ind - shiftwidth() |
7 | 106 endif |
107 endif | |
108 | |
10385
368468ef35cf
commit https://github.com/vim/vim/commit/c0514bf4777a1d55f5785b3887c5686fd0bbe870
Christian Brabandt <cb@256bit.org>
parents:
10244
diff
changeset
|
109 "Add a shiftwidth to statements following if, else, else if, case, class, |
2908 | 110 "where, else where, forall, type, interface and associate statements |
10385
368468ef35cf
commit https://github.com/vim/vim/commit/c0514bf4777a1d55f5785b3887c5686fd0bbe870
Christian Brabandt <cb@256bit.org>
parents:
10244
diff
changeset
|
111 if prevstat =~? '^\s*\(case\|class\|else\|else\s*if\|else\s*where\)\>' |
2908 | 112 \ ||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\>' | |
7790
ca19726d5e83
commit https://github.com/vim/vim/commit/298b440930ecece38d6ea0505a3e582dc817e79b
Christian Brabandt <cb@256bit.org>
parents:
7384
diff
changeset
|
115 let ind = ind + shiftwidth() |
7 | 116 " Remove unwanted indent after logical and arithmetic ifs |
117 if prevstat =~? '\<if\>' && prevstat !~? '\<then\>' | |
7790
ca19726d5e83
commit https://github.com/vim/vim/commit/298b440930ecece38d6ea0505a3e582dc817e79b
Christian Brabandt <cb@256bit.org>
parents:
7384
diff
changeset
|
118 let ind = ind - shiftwidth() |
7 | 119 endif |
840 | 120 " Remove unwanted indent after type( statements |
2908 | 121 if prevstat =~? '^\s*type\s*(' |
7790
ca19726d5e83
commit https://github.com/vim/vim/commit/298b440930ecece38d6ea0505a3e582dc817e79b
Christian Brabandt <cb@256bit.org>
parents:
7384
diff
changeset
|
122 let ind = ind - shiftwidth() |
840 | 123 endif |
7 | 124 endif |
125 | |
2908 | 126 "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\+\)\=' | |
10244
876fbdd84e52
commit https://github.com/vim/vim/commit/2ec618c9feac4573b154510236ad8121c77d0eca
Christian Brabandt <cb@256bit.org>
parents:
10048
diff
changeset
|
131 if prevstat =~? '^\s*\(contains\|submodule\|program\)\>' |
876fbdd84e52
commit https://github.com/vim/vim/commit/2ec618c9feac4573b154510236ad8121c77d0eca
Christian Brabandt <cb@256bit.org>
parents:
10048
diff
changeset
|
132 \ ||prevstat =~? '^\s*'.'module\>\(\s*\procedure\)\@!' |
2908 | 133 \ ||prevstat =~? '^\s*'.prefix.'subroutine\>' |
134 \ ||prevstat =~? '^\s*'.prefix.type.'function\>' | |
135 \ ||prevstat =~? '^\s*'.type.prefix.'function\>' | |
7790
ca19726d5e83
commit https://github.com/vim/vim/commit/298b440930ecece38d6ea0505a3e582dc817e79b
Christian Brabandt <cb@256bit.org>
parents:
7384
diff
changeset
|
136 let ind = ind + shiftwidth() |
2908 | 137 endif |
138 if getline(v:lnum) =~? '^\s*contains\>' | |
139 \ ||getline(v:lnum)=~? '^\s*end\s*' | |
10244
876fbdd84e52
commit https://github.com/vim/vim/commit/2ec618c9feac4573b154510236ad8121c77d0eca
Christian Brabandt <cb@256bit.org>
parents:
10048
diff
changeset
|
140 \ .'\(function\|subroutine\|module\|submodule\|program\)\>' |
7790
ca19726d5e83
commit https://github.com/vim/vim/commit/298b440930ecece38d6ea0505a3e582dc817e79b
Christian Brabandt <cb@256bit.org>
parents:
7384
diff
changeset
|
141 let ind = ind - shiftwidth() |
2908 | 142 endif |
143 endif | |
144 | |
10385
368468ef35cf
commit https://github.com/vim/vim/commit/c0514bf4777a1d55f5785b3887c5686fd0bbe870
Christian Brabandt <cb@256bit.org>
parents:
10244
diff
changeset
|
145 "Subtract a shiftwidth from else, else if, elsewhere, case, class, end if, |
2908 | 146 " end where, end select, end forall, end interface, end associate, |
9975
03fa8a51e9dc
commit https://github.com/vim/vim/commit/e4a3bcf28d92d0bde9ca227ccb40d401038185e5
Christian Brabandt <cb@256bit.org>
parents:
7790
diff
changeset
|
147 " end enum, end type, end block and end type statements |
7 | 148 if getline(v:lnum) =~? '^\s*\(\d\+\s\)\=\s*' |
10385
368468ef35cf
commit https://github.com/vim/vim/commit/c0514bf4777a1d55f5785b3887c5686fd0bbe870
Christian Brabandt <cb@256bit.org>
parents:
10244
diff
changeset
|
149 \. '\(else\|else\s*if\|else\s*where\|case\|class\|' |
2908 | 150 \. 'end\s*\(if\|where\|select\|interface\|' |
6530 | 151 \. 'type\|forall\|associate\|enum\|block\)\)\>' |
7790
ca19726d5e83
commit https://github.com/vim/vim/commit/298b440930ecece38d6ea0505a3e582dc817e79b
Christian Brabandt <cb@256bit.org>
parents:
7384
diff
changeset
|
152 let ind = ind - shiftwidth() |
7 | 153 " Fix indent for case statement immediately after select |
22565 | 154 if prevstat =~? '\<select\s*\(case\|type\)\>' |
7790
ca19726d5e83
commit https://github.com/vim/vim/commit/298b440930ecece38d6ea0505a3e582dc817e79b
Christian Brabandt <cb@256bit.org>
parents:
7384
diff
changeset
|
155 let ind = ind + shiftwidth() |
7 | 156 endif |
157 endif | |
158 | |
2908 | 159 "First continuation line |
160 if prevstat =~ '&\s*$' && prev2stat !~ '&\s*$' | |
7790
ca19726d5e83
commit https://github.com/vim/vim/commit/298b440930ecece38d6ea0505a3e582dc817e79b
Christian Brabandt <cb@256bit.org>
parents:
7384
diff
changeset
|
161 let ind = ind + shiftwidth() |
2908 | 162 endif |
163 "Line after last continuation line | |
6530 | 164 if prevstat !~ '&\s*$' && prev2stat =~ '&\s*$' && prevstat !~? '\<then\>' |
7790
ca19726d5e83
commit https://github.com/vim/vim/commit/298b440930ecece38d6ea0505a3e582dc817e79b
Christian Brabandt <cb@256bit.org>
parents:
7384
diff
changeset
|
165 let ind = ind - shiftwidth() |
2908 | 166 endif |
167 | |
7 | 168 return ind |
169 endfunction | |
170 | |
171 function 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 | |
182 endfunction | |
183 | |
184 function 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 | |
219 endfunction | |
220 | |
23737 | 221 let &cpoptions = s:cposet |
7 | 222 unlet s:cposet |
223 | |
224 " vim:sw=2 tw=130 |