3 " Maintainer: Neil Carter <n.carter@swansea.ac.uk>
5 " Last Change: 2005 Jul 05
8 if exists("b:did_indent")
13 setlocal indentexpr=GetPascalIndent(v:lnum)
15 setlocal indentkeys+==end;,==const,==type,==var,==begin,==repeat,==until,==for
16 setlocal indentkeys+==program,==function,==procedure,==object,==private
17 setlocal indentkeys+==record,==if,==else,==case
19 if exists("*GetPascalIndent")
24 function! s:GetPrevNonCommentLineNum( line_num )
26 " Skip lines starting with a comment
27 let SKIP_LINES = '^\s*\(\((\*\)\|\(\*\ \)\|\(\*)\)\|{\|}\)'
29 let nline = a:line_num
31 let nline = prevnonblank(nline-1)
32 if getline(nline) !~? SKIP_LINES
41 function! GetPascalIndent( line_num )
42 " Line 0 always goes at column 0
47 let this_codeline = getline( a:line_num )
49 " If in the middle of a three-part comment
50 if this_codeline =~ '^\s*\*'
51 return indent( a:line_num )
54 let prev_codeline_num = s:GetPrevNonCommentLineNum( a:line_num )
55 let prev_codeline = getline( prev_codeline_num )
56 let indnt = indent( prev_codeline_num )
58 " Compiler directives should always go in column zero.
59 if this_codeline =~ '^\s*{\(\$IFDEF\|\$ELSE\|\$ENDIF\)'
63 " These items have nothing before or after (not even a comment), and
64 " go on column 0. Make sure that the ^\s* is followed by \( to make
65 " ORs work properly, and not include the start of line (this must
67 " The bracketed expression with the underline is a routine
68 " separator. This is one case where we do indent comment lines.
69 if this_codeline =~ '^\s*\((\*\ _\+\ \*)\|\<\(const\|var\)\>\)$'
73 " These items may have text after them, and go on column 0 (in most
74 " cases). The problem is that "function" and "procedure" keywords
75 " should be indented if within a class declaration.
76 if this_codeline =~ '^\s*\<\(program\|type\|uses\|procedure\|function\)\>'
81 " If the begin does not come after "if", "for", or "else", then it
83 if this_codeline =~ '^\s*begin\>' && prev_codeline !~ '^\s*\<\(if\|for\|else\)\>'
87 " These keywords are indented once only.
88 if this_codeline =~ '^\s*\<\(private\)\>'
92 " If the PREVIOUS LINE contained these items, the current line is
93 " always indented once.
94 if prev_codeline =~ '^\s*\<\(type\|uses\)\>'
98 " These keywords are indented once only. Possibly surrounded by
100 if this_codeline =~ '^.\+\<\(object\|record\)\>'
104 " If the previous line was indenting...
105 if prev_codeline =~ '^\s*\<\(for\|if\|case\|else\|end\ else\)\>'
107 let indnt = indnt + &shiftwidth
108 " BUT... if this is the start of a multistatement block then we
109 " need to align the begin with the previous line.
110 if this_codeline =~ '^\s*begin\>'
111 return indnt - &shiftwidth
114 " We also need to keep the indentation level constant if the
115 " whole if-then statement was on one line.
116 if prev_codeline =~ '\<then\>.\+'
117 let indnt = indnt - &shiftwidth
121 " PREVIOUS-LINE BEGIN
122 " If the previous line was an indenting keyword then indent once...
123 if prev_codeline =~ '^\s*\<\(const\|var\|begin\|repeat\|private\)\>'
124 " But only if this is another var in a list.
125 if this_codeline !~ '^\s*var\>'
126 return indnt + &shiftwidth
130 " PREVIOUS-LINE BEGIN
131 " Indent code after a case statement begin
132 if prev_codeline =~ '\:\ begin\>'
133 return indnt + &shiftwidth
136 " These words may have text before them on the line (hence the .*)
137 " but are followed by nothing. Always indent once only.
138 if prev_codeline =~ '^\(.*\|\s*\)\<\(object\|record\)\>$'
139 return indnt + &shiftwidth
142 " If we just closed a bracket that started on a previous line, then
143 " unindent. But don't return yet -- we need to check for further
144 " unindentation (for end/until/else)
145 if prev_codeline =~ '^[^(]*[^*])'
146 let indnt = indnt - &shiftwidth
149 " At the end of a block, we have to unindent both the current line
150 " (the "end" for instance) and the newly-created line.
151 if this_codeline =~ '^\s*\<\(end\|until\|else\)\>'
152 return indnt - &shiftwidth
155 " If we have opened a bracket and it continues over one line,
158 " RE = an opening bracket followed by any amount of anything other
159 " than a closing bracket and then the end-of-line.
161 " If we didn't include the end of line, this RE would match even
162 " closed brackets, since it would match everything up to the closing
165 " This test isn't clever enough to handle brackets inside strings or
167 if prev_codeline =~ '([^*]\=[^)]*$'
168 return indnt + &shiftwidth