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