1 "------------------------------------------------------------------------------
2 " Description: Vim Ada indent file
4 " $Id: ada.vim,v 1.4 2007/05/05 17:25:37 vimboss Exp $
5 " Copyright: Copyright (C) 2006 Martin Krischik
6 " Maintainer: Martin Krischik
7 " Neil Bird <neil@fnxweb.com>
9 " $Date: 2007/05/05 17:25:37 $
12 " $HeadURL: https://svn.sourceforge.net/svnroot/gnuada/trunk/tools/vim/indent/ada.vim $
13 " History: 24.05.2006 MK Unified Headers
14 " 16.07.2006 MK Ada-Mode as vim-ball
15 " 15.10.2006 MK Bram's suggestion for runtime integration
16 " 05.11.2006 MK Bram suggested to save on spaces
17 " Help Page: ft-vim-indent
18 "------------------------------------------------------------------------------
20 " Verify handling of multi-line exprs. and recovery upon the final ';'.
21 " Correctly find comments given '"' and "" ==> " syntax.
22 " Combine the two large block-indent functions into one?
23 "------------------------------------------------------------------------------
25 " Only load this indent file when no other was loaded.
26 if exists("b:did_indent") || version < 700
32 setlocal indentexpr=GetAdaIndent()
33 setlocal indentkeys-=0{,0}
34 setlocal indentkeys+=0=~then,0=~end,0=~elsif,0=~when,0=~exception,0=~begin,0=~is,0=~record
36 " Only define the functions once.
37 if exists("*GetAdaIndent")
41 if exists("g:ada_with_gnat_project_files")
42 let s:AdaBlockStart = '^\s*\(if\>\|while\>\|else\>\|elsif\>\|loop\>\|for\>.*\<\(loop\|use\)\>\|declare\>\|begin\>\|type\>.*\<is\>[^;]*$\|\(type\>.*\)\=\<record\>\|procedure\>\|function\>\|accept\>\|do\>\|task\>\|package\>\|project\>\|then\>\|when\>\|is\>\)'
44 let s:AdaBlockStart = '^\s*\(if\>\|while\>\|else\>\|elsif\>\|loop\>\|for\>.*\<\(loop\|use\)\>\|declare\>\|begin\>\|type\>.*\<is\>[^;]*$\|\(type\>.*\)\=\<record\>\|procedure\>\|function\>\|accept\>\|do\>\|task\>\|package\>\|then\>\|when\>\|is\>\)'
47 " Section: s:MainBlockIndent {{{1
49 " Try to find indent of the block we're in
50 " prev_indent = the previous line's indent
51 " prev_lnum = previous line (to start looking on)
52 " blockstart = expr. that indicates a possible start of this block
53 " stop_at = if non-null, if a matching line is found, gives up!
54 " No recursive previous block analysis: simply look for a valid line
55 " with a lesser or equal indent than we currently (on prev_lnum) have.
56 " This shouldn't work as well as it appears to with lines that are currently
57 " nowhere near the correct indent (e.g., start of line)!
58 " Seems to work OK as it 'starts' with the indent of the /previous/ line.
59 function s:MainBlockIndent (prev_indent, prev_lnum, blockstart, stop_at)
60 let lnum = a:prev_lnum
61 let line = substitute( getline(lnum), ada#Comment, '', '' )
63 if a:stop_at != '' && line =~ '^\s*' . a:stop_at && indent(lnum) < a:prev_indent
65 elseif line =~ '^\s*' . a:blockstart
66 let ind = indent(lnum)
67 if ind < a:prev_indent
72 let lnum = prevnonblank(lnum - 1)
73 " Get previous non-blank/non-comment-only line
75 let line = substitute( getline(lnum), ada#Comment, '', '' )
76 if line !~ '^\s*$' && line !~ '^\s*#'
79 let lnum = prevnonblank(lnum - 1)
85 " Fallback - just move back one
86 return a:prev_indent - &sw
87 endfunction MainBlockIndent
89 " Section: s:EndBlockIndent {{{1
91 " Try to find indent of the block we're in (and about to complete),
92 " including handling of nested blocks. Works on the 'end' of a block.
93 " prev_indent = the previous line's indent
94 " prev_lnum = previous line (to start looking on)
95 " blockstart = expr. that indicates a possible start of this block
96 " blockend = expr. that indicates a possible end of this block
97 function s:EndBlockIndent( prev_indent, prev_lnum, blockstart, blockend )
98 let lnum = a:prev_lnum
99 let line = getline(lnum)
102 if getline(lnum) =~ '^\s*' . a:blockstart
103 let ind = indent(lnum)
105 if ind < a:prev_indent
111 elseif getline(lnum) =~ '^\s*' . a:blockend
115 let lnum = prevnonblank(lnum - 1)
116 " Get previous non-blank/non-comment-only line
118 let line = getline(lnum)
119 let line = substitute( line, ada#Comment, '', '' )
123 let lnum = prevnonblank(lnum - 1)
129 " Fallback - just move back one
130 return a:prev_indent - &sw
131 endfunction EndBlockIndent
133 " Section: s:StatementIndent {{{1
135 " Return indent of previous statement-start
136 " (after we've indented due to multi-line statements).
137 " This time, we start searching on the line *before* the one given (which is
138 " the end of a statement - we want the previous beginning).
139 function s:StatementIndent( current_indent, prev_lnum )
140 let lnum = a:prev_lnum
143 let lnum = prevnonblank(lnum - 1)
144 " Get previous non-blank/non-comment-only line
146 let line = substitute( getline(lnum), ada#Comment, '', '' )
147 if line !~ '^\s*$' && line !~ '^\s*#'
150 let lnum = prevnonblank(lnum - 1)
152 return a:current_indent
155 " Leave indent alone if our ';' line is part of a ';'-delineated
156 " aggregate (e.g., procedure args.) or first line after a block start.
157 if line =~ s:AdaBlockStart || line =~ '(\s*$'
158 return a:current_indent
160 if line !~ '[.=(]\s*$'
161 let ind = indent(prev_lnum)
162 if ind < a:current_indent
167 " Fallback - just use current one
168 return a:current_indent
169 endfunction StatementIndent
172 " Section: GetAdaIndent {{{1
174 " Find correct indent of a new line based upon what went before
176 function GetAdaIndent()
177 " Find a non-blank line above the current line.
178 let lnum = prevnonblank(v:lnum - 1)
179 let ind = indent(lnum)
182 " Get previous non-blank/non-comment-only/non-cpp line
184 let line = substitute( getline(lnum), g:ada#Comment, '', '' )
185 if line !~ '^\s*$' && line !~ '^\s*#'
188 let lnum = prevnonblank(lnum - 1)
194 " Get default indent (from prev. line)
195 let ind = indent(lnum)
198 " Now check what's on the previous line
199 if line =~ s:AdaBlockStart || line =~ '(\s*$'
200 " Check for false matches to AdaBlockStart
202 if line =~ '^\s*\(procedure\|function\|package\)\>.*\<is\s*new\>'
203 " Generic instantiation
205 elseif line =~ ')\s*;\s*$' || line =~ '^\([^(]*([^)]*)\)*[^(]*;\s*$'
206 " forward declaration
213 elseif line =~ '^\s*\(case\|exception\)\>'
214 " Move indent in twice (next 'when' will move back)
215 let ind = ind + 2 * &sw
216 elseif line =~ '^\s*end\s*record\>'
217 " Move indent back to tallying 'type' preceeding the 'record'.
218 " Allow indent to be equal to 'end record's.
219 let ind = s:MainBlockIndent( ind+&sw, lnum, 'type\>', '' )
220 elseif line =~ '\(^\s*new\>.*\)\@<!)\s*[;,]\s*$'
221 " Revert to indent of line that started this parenthesis pair
224 if getline('.') =~ '^\s*('
225 " Dire layout - use previous indent (could check for ada#Comment here)
226 let ind = indent( prevnonblank( line('.')-1 ) )
228 let ind = indent('.')
231 elseif line =~ '[.=(]\s*$'
232 " A statement continuation - move in one
234 elseif line =~ '^\s*new\>'
235 " Multiple line generic instantiation ('package blah is\nnew thingy')
236 let ind = s:StatementIndent( ind - &sw, lnum )
237 elseif line =~ ';\s*$'
238 " Statement end (but not 'end' ) - try to find current statement-start indent
239 let ind = s:StatementIndent( ind, lnum )
242 " Check for potential argument list on next line
243 let continuation = (line =~ '[A-Za-z0-9_]\s*$')
246 " Check current line; search for simplistic matching start-of-block
247 let line = getline(v:lnum)
249 " Start of line for ada-pp
251 elseif continuation && line =~ '^\s*('
252 " Don't do this if we've already indented due to the previous line
256 elseif line =~ '^\s*\(begin\|is\)\>'
257 let ind = s:MainBlockIndent( ind, lnum, '\(procedure\|function\|declare\|package\|task\)\>', 'begin\>' )
258 elseif line =~ '^\s*record\>'
259 let ind = s:MainBlockIndent( ind, lnum, 'type\>\|for\>.*\<use\>', '' ) + &sw
260 elseif line =~ '^\s*\(else\|elsif\)\>'
261 let ind = s:MainBlockIndent( ind, lnum, 'if\>', '' )
262 elseif line =~ '^\s*when\>'
263 " Align 'when' one /in/ from matching block start
264 let ind = s:MainBlockIndent( ind, lnum, '\(case\|exception\)\>', '' ) + &sw
265 elseif line =~ '^\s*end\>\s*\<if\>'
266 " End of if statements
267 let ind = s:EndBlockIndent( ind, lnum, 'if\>', 'end\>\s*\<if\>' )
268 elseif line =~ '^\s*end\>\s*\<loop\>'
270 let ind = s:EndBlockIndent( ind, lnum, '\(\(while\|for\)\>.*\)\?\<loop\>', 'end\>\s*\<loop\>' )
271 elseif line =~ '^\s*end\>\s*\<record\>'
273 let ind = s:EndBlockIndent( ind, lnum, '\(type\>.*\)\=\<record\>', 'end\>\s*\<record\>' )
274 elseif line =~ '^\s*end\>\s*\<procedure\>'
276 let ind = s:EndBlockIndent( ind, lnum, 'procedure\>.*\<is\>', 'end\>\s*\<procedure\>' )
277 elseif line =~ '^\s*end\>\s*\<case\>'
278 " End of case statement
279 let ind = s:EndBlockIndent( ind, lnum, 'case\>.*\<is\>', 'end\>\s*\<case\>' )
280 elseif line =~ '^\s*end\>'
281 " General case for end
282 let ind = s:MainBlockIndent( ind, lnum, '\(if\|while\|for\|loop\|accept\|begin\|record\|case\|exception\|package\)\>', '' )
283 elseif line =~ '^\s*exception\>'
284 let ind = s:MainBlockIndent( ind, lnum, 'begin\>', '' )
285 elseif line =~ '^\s*then\>'
286 let ind = s:MainBlockIndent( ind, lnum, 'if\>', '' )
290 endfunction GetAdaIndent
294 "------------------------------------------------------------------------------
295 " Copyright (C) 2006 Martin Krischik
297 " Vim is Charityware - see ":help license" or uganda.txt for licence details.
298 "------------------------------------------------------------------------------
299 " vim: textwidth=78 wrap tabstop=8 shiftwidth=3 softtabstop=3 noexpandtab
300 " vim: foldmethod=marker