annotate runtime/indent/fortran.vim @ 817:6897668c467f

updated for version 7.0c03
author vimboss
date Wed, 29 Mar 2006 21:06:37 +0000
parents 3fc0f57ecb91
children 23f82b5d2814
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1 " Vim indent file
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2 " Language: Fortran95 (and Fortran90, Fortran77, F and elf90)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
3 " Version: 0.36
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
4 " URL: http://www.unb.ca/chem/ajit/indent/fortran.vim
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
5 " Last Change: 2004 Apr. 05
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
6 " Maintainer: Ajit J. Thakkar <ajit@unb.ca>; <http://www.unb.ca/chem/ajit/>
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
7 " Usage: Do :help fortran-indent from Vim
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
8
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
9 " Only load this indent file when no other was loaded.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
10 if exists("b:did_indent")
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
11 finish
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
12 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
13 let b:did_indent = 1
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
14
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
15 let s:cposet=&cpoptions
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
16 set cpoptions-=C
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
17
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
18 setlocal indentkeys+==~end,=~case,=~if,=~else,=~do,=~where,=~elsewhere,=~select
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
19 setlocal indentkeys+==~endif,=~enddo,=~endwhere,=~endselect
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
20
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
21 " Determine whether this is a fixed or free format source file
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
22 " if this hasn't been done yet
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
23 if !exists("b:fortran_fixed_source")
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
24 if exists("fortran_free_source")
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
25 " User guarantees free source form
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
26 let b:fortran_fixed_source = 0
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
27 elseif exists("fortran_fixed_source")
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
28 " User guarantees fixed source form
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
29 let b:fortran_fixed_source = 1
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
30 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
31 " f90 and f95 allow both fixed and free source form
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
32 " assume fixed source form unless signs of free source form
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
33 " are detected in the first five columns of the first 25 lines
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
34 " Detection becomes more accurate and time-consuming if more lines
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
35 " are checked. Increase the limit below if you keep lots of comments at
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
36 " the very top of each file and you have a fast computer
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
37 let s:lmax = 25
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
38 if ( s:lmax > line("$") )
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
39 let s:lmax = line("$")
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
40 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
41 let b:fortran_fixed_source = 1
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
42 let s:ln=1
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
43 while s:ln <= s:lmax
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
44 let s:test = strpart(getline(s:ln),0,5)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
45 if s:test[0] !~ '[Cc*!#]' && s:test !~ '^ \+[!#]' && s:test =~ '[^ 0-9\t]'
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
46 let b:fortran_fixed_source = 0
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
47 break
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
48 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
49 let s:ln = s:ln + 1
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
50 endwhile
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
51 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
52 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
53
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
54 " Define the appropriate indent function but only once
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
55 if (b:fortran_fixed_source == 1)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
56 setlocal indentexpr=FortranGetFixedIndent()
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
57 if exists("*FortranGetFixedIndent")
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
58 finish
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
59 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
60 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
61 setlocal indentexpr=FortranGetFreeIndent()
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
62 if exists("*FortranGetFreeIndent")
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
63 finish
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
64 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
65 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
66
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
67 function FortranGetIndent(lnum)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
68 let ind = indent(a:lnum)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
69 let prevline=getline(a:lnum)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
70 " Strip tail comment
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
71 let prevstat=substitute(prevline, '!.*$', '', '')
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
72
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
73 "Indent do loops only if they are all guaranteed to be of do/end do type
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
74 if exists("b:fortran_do_enddo") || exists("fortran_do_enddo")
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
75 if prevstat =~? '^\s*\(\d\+\s\)\=\s*\(\a\w*\s*:\)\=\s*do\>'
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
76 let ind = ind + &sw
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
77 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
78 if getline(v:lnum) =~? '^\s*\(\d\+\s\)\=\s*end\s*do\>'
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
79 let ind = ind - &sw
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
80 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
81 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
82
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
83 "Add a shiftwidth to statements following if, else, case,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
84 "where and elsewhere statements
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
85 if prevstat =~? '^\s*\(\d\+\s\)\=\s*\(else\|case\|where\|elsewhere\)\>'
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
86 \ || prevstat =~? '^\s*\(\d\+\s\)\=\s*\(\a\w*\s*:\)\=\s*if\>'
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
87 let ind = ind + &sw
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
88 " Remove unwanted indent after logical and arithmetic ifs
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
89 if prevstat =~? '\<if\>' && prevstat !~? '\<then\>'
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
90 let ind = ind - &sw
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
91 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
92 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
93
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
94 "Subtract a shiftwidth from else, elsewhere, case, end if,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
95 " end where and end select statements
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
96 if getline(v:lnum) =~? '^\s*\(\d\+\s\)\=\s*'
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
97 \. '\(else\|elsewhere\|case\|end\s*\(if\|where\|select\)\)\>'
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
98 let ind = ind - &sw
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
99 " Fix indent for case statement immediately after select
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
100 if prevstat =~? '\<select\>'
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
101 let ind = ind + &sw
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
102 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
103 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
104
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
105 return ind
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
106 endfunction
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
107
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
108 function FortranGetFreeIndent()
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
109 "Find the previous non-blank line
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
110 let lnum = prevnonblank(v:lnum - 1)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
111
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
112 "Use zero indent at the top of the file
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
113 if lnum == 0
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
114 return 0
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
115 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
116
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
117 let ind=FortranGetIndent(lnum)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
118 return ind
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
119 endfunction
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
120
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
121 function FortranGetFixedIndent()
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
122 let currline=getline(v:lnum)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
123 "Don't indent comments, continuation lines and labelled lines
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
124 if strpart(currline,0,6) =~ '[^ \t]'
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
125 let ind = indent(v:lnum)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
126 return ind
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
127 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
128
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
129 "Find the previous line which is not blank, not a comment,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
130 "not a continuation line, and does not have a label
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
131 let lnum = v:lnum - 1
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
132 while lnum > 0
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
133 let prevline=getline(lnum)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
134 if (prevline =~ "^[C*!]") || (prevline =~ "^\s*$")
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
135 \ || (strpart(prevline,5,1) !~ "[ 0]")
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
136 " Skip comments, blank lines and continuation lines
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
137 let lnum = lnum - 1
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
138 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
139 let test=strpart(prevline,0,5)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
140 if test =~ "[0-9]"
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
141 " Skip lines with statement numbers
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
142 let lnum = lnum - 1
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
143 else
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
144 break
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
145 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
146 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
147 endwhile
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
148
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
149 "First line must begin at column 7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
150 if lnum == 0
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
151 return 6
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
152 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
153
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
154 let ind=FortranGetIndent(lnum)
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
155 return ind
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
156 endfunction
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
157
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
158 let &cpoptions=s:cposet
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
159 unlet s:cposet
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
160
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
161 " vim:sw=2 tw=130