Mercurial > vim
annotate runtime/indent/fortran.vim @ 26346:8be6413a8e27 v8.2.3704
patch 8.2.3704: Vim9: cannot use a list declaration in a :def function
Commit: https://github.com/vim/vim/commit/ab36e6ae7b87b0295fb19270e4339a734875c6b1
Author: Bram Moolenaar <Bram@vim.org>
Date: Tue Nov 30 16:14:49 2021 +0000
patch 8.2.3704: Vim9: cannot use a list declaration in a :def function
Problem: Vim9: cannot use a list declaration in a :def function.
Solution: Make it work.
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Tue, 30 Nov 2021 17:15:03 +0100 |
parents | 34b4eb3a8458 |
children | 57c9377b9c62 |
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) |
22565 | 3 " Version: (v48) 2020 October 07 |
6530 | 4 " Maintainer: Ajit J. Thakkar <ajit@unb.ca>; <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, | |
10 " and Louis Cochen. | |
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 | |
20 | |
7 | 21 setlocal indentkeys+==~end,=~case,=~if,=~else,=~do,=~where,=~elsewhere,=~select |
2908 | 22 setlocal indentkeys+==~endif,=~enddo,=~endwhere,=~endselect,=~elseif |
23 setlocal indentkeys+==~type,=~interface,=~forall,=~associate,=~block,=~enum | |
24 setlocal indentkeys+==~endforall,=~endassociate,=~endblock,=~endenum | |
25 if 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 | |
29 endif | |
7 | 30 |
31 " 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
|
32 " 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
|
33 " buffer-local value |
aea5ebf352c4
commit https://github.com/vim/vim/commit/256972a9849b5d575b62a6a71be5b6934b5b0e8b
Christian Brabandt <cb@256bit.org>
parents:
6530
diff
changeset
|
34 " > global value |
aea5ebf352c4
commit https://github.com/vim/vim/commit/256972a9849b5d575b62a6a71be5b6934b5b0e8b
Christian Brabandt <cb@256bit.org>
parents:
6530
diff
changeset
|
35 " > file extension as in Intel ifort, gcc (gfortran), NAG, Pathscale, and Cray compilers |
7 | 36 if !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 | |
20115 | 43 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
|
44 " 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
|
45 let b:fortran_fixed_source = 0 |
20115 | 46 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
|
47 " Fixed-form file extension defaults |
aea5ebf352c4
commit https://github.com/vim/vim/commit/256972a9849b5d575b62a6a71be5b6934b5b0e8b
Christian Brabandt <cb@256bit.org>
parents:
6530
diff
changeset
|
48 let b:fortran_fixed_source = 1 |
7 | 49 else |
7384
aea5ebf352c4
commit https://github.com/vim/vim/commit/256972a9849b5d575b62a6a71be5b6934b5b0e8b
Christian Brabandt <cb@256bit.org>
parents:
6530
diff
changeset
|
50 " 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
|
51 " Assume fixed source form unless signs of free source form |
6530 | 52 " 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
|
53 " Detection becomes more accurate and time-consuming if more lines |
7 | 54 " 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
|
55 " 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
|
56 let s:lmax = 500 |
7 | 57 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) | |
2377
878562053ba3
Update Fortran indent and syntax file. (Ajit Thakkar)
Bram Moolenaar <bram@vim.org>
parents:
1125
diff
changeset
|
64 if s:test !~ '^[Cc*]' && s:test !~ '^ *[!#]' && s:test =~ '[^ 0-9\t]' && s:test !~ '^[ 0-9]*\t' |
7 | 65 let b:fortran_fixed_source = 0 |
66 break | |
67 endif | |
68 let s:ln = s:ln + 1 | |
69 endwhile | |
70 endif | |
71 endif | |
72 | |
73 " Define the appropriate indent function but only once | |
74 if (b:fortran_fixed_source == 1) | |
75 setlocal indentexpr=FortranGetFixedIndent() | |
76 if exists("*FortranGetFixedIndent") | |
23737 | 77 let &cpoptions = s:cposet |
78 unlet s:cposet | |
7 | 79 finish |
80 endif | |
81 else | |
82 setlocal indentexpr=FortranGetFreeIndent() | |
83 if exists("*FortranGetFreeIndent") | |
23737 | 84 let &cpoptions = s:cposet |
85 unlet s:cposet | |
7 | 86 finish |
87 endif | |
88 endif | |
89 | |
90 function FortranGetIndent(lnum) | |
91 let ind = indent(a:lnum) | |
92 let prevline=getline(a:lnum) | |
93 " Strip tail comment | |
94 let prevstat=substitute(prevline, '!.*$', '', '') | |
2908 | 95 let prev2line=getline(a:lnum-1) |
96 let prev2stat=substitute(prev2line, '!.*$', '', '') | |
7 | 97 |
98 "Indent do loops only if they are all guaranteed to be of do/end do type | |
1125 | 99 if exists("b:fortran_do_enddo") || exists("g:fortran_do_enddo") |
7 | 100 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
|
101 let ind = ind + shiftwidth() |
7 | 102 endif |
103 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
|
104 let ind = ind - shiftwidth() |
7 | 105 endif |
106 endif | |
107 | |
10385
368468ef35cf
commit https://github.com/vim/vim/commit/c0514bf4777a1d55f5785b3887c5686fd0bbe870
Christian Brabandt <cb@256bit.org>
parents:
10244
diff
changeset
|
108 "Add a shiftwidth to statements following if, else, else if, case, class, |
2908 | 109 "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
|
110 if prevstat =~? '^\s*\(case\|class\|else\|else\s*if\|else\s*where\)\>' |
2908 | 111 \ ||prevstat=~? '^\s*\(type\|interface\|associate\|enum\)\>' |
112 \ ||prevstat=~?'^\s*\(\d\+\s\)\=\s*\(\a\w*\s*:\)\=\s*\(forall\|where\|block\)\>' | |
113 \ ||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
|
114 let ind = ind + shiftwidth() |
7 | 115 " Remove unwanted indent after logical and arithmetic ifs |
116 if prevstat =~? '\<if\>' && prevstat !~? '\<then\>' | |
7790
ca19726d5e83
commit https://github.com/vim/vim/commit/298b440930ecece38d6ea0505a3e582dc817e79b
Christian Brabandt <cb@256bit.org>
parents:
7384
diff
changeset
|
117 let ind = ind - shiftwidth() |
7 | 118 endif |
840 | 119 " Remove unwanted indent after type( statements |
2908 | 120 if prevstat =~? '^\s*type\s*(' |
7790
ca19726d5e83
commit https://github.com/vim/vim/commit/298b440930ecece38d6ea0505a3e582dc817e79b
Christian Brabandt <cb@256bit.org>
parents:
7384
diff
changeset
|
121 let ind = ind - shiftwidth() |
840 | 122 endif |
7 | 123 endif |
124 | |
2908 | 125 "Indent program units unless instructed otherwise |
126 if !exists("b:fortran_indent_less") && !exists("g:fortran_indent_less") | |
127 let prefix='\(\(pure\|impure\|elemental\|recursive\)\s\+\)\{,2}' | |
128 let type='\(\(integer\|real\|double\s\+precision\|complex\|logical' | |
129 \.'\|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
|
130 if prevstat =~? '^\s*\(contains\|submodule\|program\)\>' |
876fbdd84e52
commit https://github.com/vim/vim/commit/2ec618c9feac4573b154510236ad8121c77d0eca
Christian Brabandt <cb@256bit.org>
parents:
10048
diff
changeset
|
131 \ ||prevstat =~? '^\s*'.'module\>\(\s*\procedure\)\@!' |
2908 | 132 \ ||prevstat =~? '^\s*'.prefix.'subroutine\>' |
133 \ ||prevstat =~? '^\s*'.prefix.type.'function\>' | |
134 \ ||prevstat =~? '^\s*'.type.prefix.'function\>' | |
7790
ca19726d5e83
commit https://github.com/vim/vim/commit/298b440930ecece38d6ea0505a3e582dc817e79b
Christian Brabandt <cb@256bit.org>
parents:
7384
diff
changeset
|
135 let ind = ind + shiftwidth() |
2908 | 136 endif |
137 if getline(v:lnum) =~? '^\s*contains\>' | |
138 \ ||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
|
139 \ .'\(function\|subroutine\|module\|submodule\|program\)\>' |
7790
ca19726d5e83
commit https://github.com/vim/vim/commit/298b440930ecece38d6ea0505a3e582dc817e79b
Christian Brabandt <cb@256bit.org>
parents:
7384
diff
changeset
|
140 let ind = ind - shiftwidth() |
2908 | 141 endif |
142 endif | |
143 | |
10385
368468ef35cf
commit https://github.com/vim/vim/commit/c0514bf4777a1d55f5785b3887c5686fd0bbe870
Christian Brabandt <cb@256bit.org>
parents:
10244
diff
changeset
|
144 "Subtract a shiftwidth from else, else if, elsewhere, case, class, end if, |
2908 | 145 " 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
|
146 " end enum, end type, end block and end type statements |
7 | 147 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
|
148 \. '\(else\|else\s*if\|else\s*where\|case\|class\|' |
2908 | 149 \. 'end\s*\(if\|where\|select\|interface\|' |
6530 | 150 \. 'type\|forall\|associate\|enum\|block\)\)\>' |
7790
ca19726d5e83
commit https://github.com/vim/vim/commit/298b440930ecece38d6ea0505a3e582dc817e79b
Christian Brabandt <cb@256bit.org>
parents:
7384
diff
changeset
|
151 let ind = ind - shiftwidth() |
7 | 152 " Fix indent for case statement immediately after select |
22565 | 153 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
|
154 let ind = ind + shiftwidth() |
7 | 155 endif |
156 endif | |
157 | |
2908 | 158 "First continuation line |
159 if prevstat =~ '&\s*$' && prev2stat !~ '&\s*$' | |
7790
ca19726d5e83
commit https://github.com/vim/vim/commit/298b440930ecece38d6ea0505a3e582dc817e79b
Christian Brabandt <cb@256bit.org>
parents:
7384
diff
changeset
|
160 let ind = ind + shiftwidth() |
2908 | 161 endif |
162 "Line after last continuation line | |
6530 | 163 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
|
164 let ind = ind - shiftwidth() |
2908 | 165 endif |
166 | |
7 | 167 return ind |
168 endfunction | |
169 | |
170 function FortranGetFreeIndent() | |
171 "Find the previous non-blank line | |
172 let lnum = prevnonblank(v:lnum - 1) | |
173 | |
174 "Use zero indent at the top of the file | |
175 if lnum == 0 | |
176 return 0 | |
177 endif | |
178 | |
179 let ind=FortranGetIndent(lnum) | |
180 return ind | |
181 endfunction | |
182 | |
183 function FortranGetFixedIndent() | |
184 let currline=getline(v:lnum) | |
185 "Don't indent comments, continuation lines and labelled lines | |
186 if strpart(currline,0,6) =~ '[^ \t]' | |
187 let ind = indent(v:lnum) | |
188 return ind | |
189 endif | |
190 | |
191 "Find the previous line which is not blank, not a comment, | |
192 "not a continuation line, and does not have a label | |
193 let lnum = v:lnum - 1 | |
194 while lnum > 0 | |
195 let prevline=getline(lnum) | |
196 if (prevline =~ "^[C*!]") || (prevline =~ "^\s*$") | |
197 \ || (strpart(prevline,5,1) !~ "[ 0]") | |
198 " Skip comments, blank lines and continuation lines | |
199 let lnum = lnum - 1 | |
200 else | |
201 let test=strpart(prevline,0,5) | |
202 if test =~ "[0-9]" | |
203 " Skip lines with statement numbers | |
204 let lnum = lnum - 1 | |
205 else | |
206 break | |
207 endif | |
208 endif | |
209 endwhile | |
210 | |
211 "First line must begin at column 7 | |
212 if lnum == 0 | |
213 return 6 | |
214 endif | |
215 | |
216 let ind=FortranGetIndent(lnum) | |
217 return ind | |
218 endfunction | |
219 | |
23737 | 220 let &cpoptions = s:cposet |
7 | 221 unlet s:cposet |
222 | |
223 " vim:sw=2 tw=130 |