Mercurial > vim
annotate runtime/indent/fortran.vim @ 10091:9e5e3392f9b6
Added tag v7.4.2316 for changeset 68303019acc994c41e623b6be3235f7342f2be67
author | Christian Brabandt <cb@256bit.org> |
---|---|
date | Sat, 03 Sep 2016 19:00:06 +0200 |
parents | 43efa4f5a8ea |
children | 876fbdd84e52 |
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) |
9975
03fa8a51e9dc
commit https://github.com/vim/vim/commit/e4a3bcf28d92d0bde9ca227ccb40d401038185e5
Christian Brabandt <cb@256bit.org>
parents:
7790
diff
changeset
|
3 " Version: 0.45 |
10048
43efa4f5a8ea
commit https://github.com/vim/vim/commit/89bcfda6834aba724d12554a34b9ed49f5789fd5
Christian Brabandt <cb@256bit.org>
parents:
9975
diff
changeset
|
4 " Last Change: 2016 Aug. 29 |
6530 | 5 " 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
|
6 " Usage: For instructions, do :help fortran-indent from Vim |
6530 | 7 " Credits: |
7790
ca19726d5e83
commit https://github.com/vim/vim/commit/298b440930ecece38d6ea0505a3e582dc817e79b
Christian Brabandt <cb@256bit.org>
parents:
7384
diff
changeset
|
8 " Useful suggestions were made by: Albert Oliver Serra and Takuya Fujiwara. |
7 | 9 |
10 " Only load this indent file when no other was loaded. | |
11 if exists("b:did_indent") | |
12 finish | |
13 endif | |
14 let b:did_indent = 1 | |
15 | |
3237 | 16 let s:cposet=&cpoptions |
17 set cpoptions&vim | |
18 | |
7 | 19 setlocal indentkeys+==~end,=~case,=~if,=~else,=~do,=~where,=~elsewhere,=~select |
2908 | 20 setlocal indentkeys+==~endif,=~enddo,=~endwhere,=~endselect,=~elseif |
21 setlocal indentkeys+==~type,=~interface,=~forall,=~associate,=~block,=~enum | |
22 setlocal indentkeys+==~endforall,=~endassociate,=~endblock,=~endenum | |
23 if exists("b:fortran_indent_more") || exists("g:fortran_indent_more") | |
24 setlocal indentkeys+==~function,=~subroutine,=~module,=~contains,=~program | |
25 setlocal indentkeys+==~endfunction,=~endsubroutine,=~endmodule | |
26 setlocal indentkeys+==~endprogram | |
27 endif | |
7 | 28 |
29 " 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
|
30 " 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
|
31 " buffer-local value |
aea5ebf352c4
commit https://github.com/vim/vim/commit/256972a9849b5d575b62a6a71be5b6934b5b0e8b
Christian Brabandt <cb@256bit.org>
parents:
6530
diff
changeset
|
32 " > global value |
aea5ebf352c4
commit https://github.com/vim/vim/commit/256972a9849b5d575b62a6a71be5b6934b5b0e8b
Christian Brabandt <cb@256bit.org>
parents:
6530
diff
changeset
|
33 " > file extension as in Intel ifort, gcc (gfortran), NAG, Pathscale, and Cray compilers |
7 | 34 if !exists("b:fortran_fixed_source") |
35 if exists("fortran_free_source") | |
36 " User guarantees free source form | |
37 let b:fortran_fixed_source = 0 | |
38 elseif exists("fortran_fixed_source") | |
39 " User guarantees fixed source form | |
40 let b:fortran_fixed_source = 1 | |
7384
aea5ebf352c4
commit https://github.com/vim/vim/commit/256972a9849b5d575b62a6a71be5b6934b5b0e8b
Christian Brabandt <cb@256bit.org>
parents:
6530
diff
changeset
|
41 elseif expand("%:e") ==? "f\<90\|95\|03\|08\>" |
aea5ebf352c4
commit https://github.com/vim/vim/commit/256972a9849b5d575b62a6a71be5b6934b5b0e8b
Christian Brabandt <cb@256bit.org>
parents:
6530
diff
changeset
|
42 " 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
|
43 let b:fortran_fixed_source = 0 |
aea5ebf352c4
commit https://github.com/vim/vim/commit/256972a9849b5d575b62a6a71be5b6934b5b0e8b
Christian Brabandt <cb@256bit.org>
parents:
6530
diff
changeset
|
44 elseif expand("%:e") ==? "f\|f77\|for" |
aea5ebf352c4
commit https://github.com/vim/vim/commit/256972a9849b5d575b62a6a71be5b6934b5b0e8b
Christian Brabandt <cb@256bit.org>
parents:
6530
diff
changeset
|
45 " Fixed-form file extension defaults |
aea5ebf352c4
commit https://github.com/vim/vim/commit/256972a9849b5d575b62a6a71be5b6934b5b0e8b
Christian Brabandt <cb@256bit.org>
parents:
6530
diff
changeset
|
46 let b:fortran_fixed_source = 1 |
7 | 47 else |
7384
aea5ebf352c4
commit https://github.com/vim/vim/commit/256972a9849b5d575b62a6a71be5b6934b5b0e8b
Christian Brabandt <cb@256bit.org>
parents:
6530
diff
changeset
|
48 " 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
|
49 " Assume fixed source form unless signs of free source form |
6530 | 50 " 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
|
51 " Detection becomes more accurate and time-consuming if more lines |
7 | 52 " 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
|
53 " 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
|
54 let s:lmax = 500 |
7 | 55 if ( s:lmax > line("$") ) |
56 let s:lmax = line("$") | |
57 endif | |
58 let b:fortran_fixed_source = 1 | |
59 let s:ln=1 | |
60 while s:ln <= s:lmax | |
61 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
|
62 if s:test !~ '^[Cc*]' && s:test !~ '^ *[!#]' && s:test =~ '[^ 0-9\t]' && s:test !~ '^[ 0-9]*\t' |
7 | 63 let b:fortran_fixed_source = 0 |
64 break | |
65 endif | |
66 let s:ln = s:ln + 1 | |
67 endwhile | |
68 endif | |
69 endif | |
70 | |
71 " Define the appropriate indent function but only once | |
72 if (b:fortran_fixed_source == 1) | |
73 setlocal indentexpr=FortranGetFixedIndent() | |
74 if exists("*FortranGetFixedIndent") | |
75 finish | |
76 endif | |
77 else | |
78 setlocal indentexpr=FortranGetFreeIndent() | |
79 if exists("*FortranGetFreeIndent") | |
80 finish | |
81 endif | |
82 endif | |
83 | |
84 function FortranGetIndent(lnum) | |
85 let ind = indent(a:lnum) | |
86 let prevline=getline(a:lnum) | |
87 " Strip tail comment | |
88 let prevstat=substitute(prevline, '!.*$', '', '') | |
2908 | 89 let prev2line=getline(a:lnum-1) |
90 let prev2stat=substitute(prev2line, '!.*$', '', '') | |
7 | 91 |
92 "Indent do loops only if they are all guaranteed to be of do/end do type | |
1125 | 93 if exists("b:fortran_do_enddo") || exists("g:fortran_do_enddo") |
7 | 94 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
|
95 let ind = ind + shiftwidth() |
7 | 96 endif |
97 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
|
98 let ind = ind - shiftwidth() |
7 | 99 endif |
100 endif | |
101 | |
2908 | 102 "Add a shiftwidth to statements following if, else, else if, case, |
103 "where, else where, forall, type, interface and associate statements | |
104 if prevstat =~? '^\s*\(case\|else\|else\s*if\|else\s*where\)\>' | |
105 \ ||prevstat=~? '^\s*\(type\|interface\|associate\|enum\)\>' | |
106 \ ||prevstat=~?'^\s*\(\d\+\s\)\=\s*\(\a\w*\s*:\)\=\s*\(forall\|where\|block\)\>' | |
107 \ ||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
|
108 let ind = ind + shiftwidth() |
7 | 109 " Remove unwanted indent after logical and arithmetic ifs |
110 if prevstat =~? '\<if\>' && prevstat !~? '\<then\>' | |
7790
ca19726d5e83
commit https://github.com/vim/vim/commit/298b440930ecece38d6ea0505a3e582dc817e79b
Christian Brabandt <cb@256bit.org>
parents:
7384
diff
changeset
|
111 let ind = ind - shiftwidth() |
7 | 112 endif |
840 | 113 " Remove unwanted indent after type( statements |
2908 | 114 if prevstat =~? '^\s*type\s*(' |
7790
ca19726d5e83
commit https://github.com/vim/vim/commit/298b440930ecece38d6ea0505a3e582dc817e79b
Christian Brabandt <cb@256bit.org>
parents:
7384
diff
changeset
|
115 let ind = ind - shiftwidth() |
840 | 116 endif |
7 | 117 endif |
118 | |
2908 | 119 "Indent program units unless instructed otherwise |
120 if !exists("b:fortran_indent_less") && !exists("g:fortran_indent_less") | |
121 let prefix='\(\(pure\|impure\|elemental\|recursive\)\s\+\)\{,2}' | |
122 let type='\(\(integer\|real\|double\s\+precision\|complex\|logical' | |
123 \.'\|character\|type\|class\)\s*\S*\s\+\)\=' | |
9975
03fa8a51e9dc
commit https://github.com/vim/vim/commit/e4a3bcf28d92d0bde9ca227ccb40d401038185e5
Christian Brabandt <cb@256bit.org>
parents:
7790
diff
changeset
|
124 if prevstat =~? '^\s*\(module\|contains\/submodule\|program\)\>' |
2908 | 125 \ ||prevstat =~? '^\s*'.prefix.'subroutine\>' |
126 \ ||prevstat =~? '^\s*'.prefix.type.'function\>' | |
127 \ ||prevstat =~? '^\s*'.type.prefix.'function\>' | |
7790
ca19726d5e83
commit https://github.com/vim/vim/commit/298b440930ecece38d6ea0505a3e582dc817e79b
Christian Brabandt <cb@256bit.org>
parents:
7384
diff
changeset
|
128 let ind = ind + shiftwidth() |
2908 | 129 endif |
130 if getline(v:lnum) =~? '^\s*contains\>' | |
131 \ ||getline(v:lnum)=~? '^\s*end\s*' | |
9975
03fa8a51e9dc
commit https://github.com/vim/vim/commit/e4a3bcf28d92d0bde9ca227ccb40d401038185e5
Christian Brabandt <cb@256bit.org>
parents:
7790
diff
changeset
|
132 \ .'\(function\|subroutine\|module\/submodule\|program\)\>' |
7790
ca19726d5e83
commit https://github.com/vim/vim/commit/298b440930ecece38d6ea0505a3e582dc817e79b
Christian Brabandt <cb@256bit.org>
parents:
7384
diff
changeset
|
133 let ind = ind - shiftwidth() |
2908 | 134 endif |
135 endif | |
136 | |
137 "Subtract a shiftwidth from else, else if, elsewhere, case, end if, | |
138 " 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
|
139 " end enum, end type, end block and end type statements |
7 | 140 if getline(v:lnum) =~? '^\s*\(\d\+\s\)\=\s*' |
2908 | 141 \. '\(else\|else\s*if\|else\s*where\|case\|' |
142 \. 'end\s*\(if\|where\|select\|interface\|' | |
6530 | 143 \. 'type\|forall\|associate\|enum\|block\)\)\>' |
7790
ca19726d5e83
commit https://github.com/vim/vim/commit/298b440930ecece38d6ea0505a3e582dc817e79b
Christian Brabandt <cb@256bit.org>
parents:
7384
diff
changeset
|
144 let ind = ind - shiftwidth() |
7 | 145 " Fix indent for case statement immediately after select |
2908 | 146 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
|
147 let ind = ind + shiftwidth() |
7 | 148 endif |
149 endif | |
150 | |
2908 | 151 "First continuation line |
152 if prevstat =~ '&\s*$' && prev2stat !~ '&\s*$' | |
7790
ca19726d5e83
commit https://github.com/vim/vim/commit/298b440930ecece38d6ea0505a3e582dc817e79b
Christian Brabandt <cb@256bit.org>
parents:
7384
diff
changeset
|
153 let ind = ind + shiftwidth() |
2908 | 154 endif |
155 "Line after last continuation line | |
6530 | 156 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
|
157 let ind = ind - shiftwidth() |
2908 | 158 endif |
159 | |
7 | 160 return ind |
161 endfunction | |
162 | |
163 function FortranGetFreeIndent() | |
164 "Find the previous non-blank line | |
165 let lnum = prevnonblank(v:lnum - 1) | |
166 | |
167 "Use zero indent at the top of the file | |
168 if lnum == 0 | |
169 return 0 | |
170 endif | |
171 | |
172 let ind=FortranGetIndent(lnum) | |
173 return ind | |
174 endfunction | |
175 | |
176 function FortranGetFixedIndent() | |
177 let currline=getline(v:lnum) | |
178 "Don't indent comments, continuation lines and labelled lines | |
179 if strpart(currline,0,6) =~ '[^ \t]' | |
180 let ind = indent(v:lnum) | |
181 return ind | |
182 endif | |
183 | |
184 "Find the previous line which is not blank, not a comment, | |
185 "not a continuation line, and does not have a label | |
186 let lnum = v:lnum - 1 | |
187 while lnum > 0 | |
188 let prevline=getline(lnum) | |
189 if (prevline =~ "^[C*!]") || (prevline =~ "^\s*$") | |
190 \ || (strpart(prevline,5,1) !~ "[ 0]") | |
191 " Skip comments, blank lines and continuation lines | |
192 let lnum = lnum - 1 | |
193 else | |
194 let test=strpart(prevline,0,5) | |
195 if test =~ "[0-9]" | |
196 " Skip lines with statement numbers | |
197 let lnum = lnum - 1 | |
198 else | |
199 break | |
200 endif | |
201 endif | |
202 endwhile | |
203 | |
204 "First line must begin at column 7 | |
205 if lnum == 0 | |
206 return 6 | |
207 endif | |
208 | |
209 let ind=FortranGetIndent(lnum) | |
210 return ind | |
211 endfunction | |
212 | |
213 let &cpoptions=s:cposet | |
214 unlet s:cposet | |
215 | |
216 " vim:sw=2 tw=130 |