Runtime files update
[MacVim.git] / runtime / indent / ada.vim
blob95dabf91f319cb6d8574edab727e0eb90a97eab5
1 "------------------------------------------------------------------------------
2 "  Description: Vim Ada indent file
3 "     Language: Ada (2005)
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>
8 "      $Author: vimboss $
9 "        $Date: 2007/05/05 17:25:37 $
10 "      Version: 4.2
11 "    $Revision: 1.4 $
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 "------------------------------------------------------------------------------
19 " ToDo:
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
27    finish
28 endif
30 let b:did_indent = 1
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")
38    finish
39 endif
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\>\)'
43 else
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\>\)'
45 endif
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, '', '' )
62    while lnum > 1
63       if a:stop_at != ''  &&  line =~ '^\s*' . a:stop_at  &&  indent(lnum) < a:prev_indent
64          return a:prev_indent
65       elseif line =~ '^\s*' . a:blockstart
66          let ind = indent(lnum)
67          if ind < a:prev_indent
68             return ind
69          endif
70       endif
72       let lnum = prevnonblank(lnum - 1)
73       " Get previous non-blank/non-comment-only line
74       while 1
75          let line = substitute( getline(lnum), ada#Comment, '', '' )
76          if line !~ '^\s*$' && line !~ '^\s*#'
77             break
78          endif
79          let lnum = prevnonblank(lnum - 1)
80          if lnum <= 0
81             return a:prev_indent
82          endif
83       endwhile
84    endwhile
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)
100    let ends = 0
101    while lnum > 1
102       if getline(lnum) =~ '^\s*' . a:blockstart
103          let ind = indent(lnum)
104          if ends <= 0
105             if ind < a:prev_indent
106                return ind
107             endif
108          else
109             let ends = ends - 1
110          endif
111       elseif getline(lnum) =~ '^\s*' . a:blockend
112          let ends = ends + 1
113       endif
115       let lnum = prevnonblank(lnum - 1)
116       " Get previous non-blank/non-comment-only line
117       while 1
118          let line = getline(lnum)
119          let line = substitute( line, ada#Comment, '', '' )
120          if line !~ '^\s*$'
121             break
122          endif
123          let lnum = prevnonblank(lnum - 1)
124          if lnum <= 0
125             return a:prev_indent
126          endif
127       endwhile
128    endwhile
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
141    while lnum > 0
142       let prev_lnum = lnum
143       let lnum = prevnonblank(lnum - 1)
144       " Get previous non-blank/non-comment-only line
145       while 1
146          let line = substitute( getline(lnum), ada#Comment, '', '' )
147          if line !~ '^\s*$' && line !~ '^\s*#'
148             break
149          endif
150          let lnum = prevnonblank(lnum - 1)
151          if lnum <= 0
152             return a:current_indent
153          endif
154       endwhile
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
159       endif
160       if line !~ '[.=(]\s*$'
161          let ind = indent(prev_lnum)
162          if ind < a:current_indent
163             return ind
164          endif
165       endif
166    endwhile
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)
180    let package_line = 0
182    " Get previous non-blank/non-comment-only/non-cpp line
183    while 1
184       let line = substitute( getline(lnum), g:ada#Comment, '', '' )
185       if line !~ '^\s*$' && line !~ '^\s*#'
186          break
187       endif
188       let lnum = prevnonblank(lnum - 1)
189       if lnum <= 0
190          return ind
191       endif
192    endwhile
194    " Get default indent (from prev. line)
195    let ind = indent(lnum)
196    let initind = ind
198    " Now check what's on the previous line
199    if line =~ s:AdaBlockStart  ||  line =~ '(\s*$'
200       " Check for false matches to AdaBlockStart
201       let false_match = 0
202       if line =~ '^\s*\(procedure\|function\|package\)\>.*\<is\s*new\>'
203          " Generic instantiation
204          let false_match = 1
205       elseif line =~ ')\s*;\s*$'  ||  line =~ '^\([^(]*([^)]*)\)*[^(]*;\s*$'
206          " forward declaration
207          let false_match = 1
208       endif
209       " Move indent in
210       if ! false_match
211          let ind = ind + &sw
212       endif
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
222       exe lnum
223       exe 'normal! $F)%'
224       if getline('.') =~ '^\s*('
225          " Dire layout - use previous indent (could check for ada#Comment here)
226          let ind = indent( prevnonblank( line('.')-1 ) )
227       else
228          let ind = indent('.')
229       endif
230       exe v:lnum
231    elseif line =~ '[.=(]\s*$'
232       " A statement continuation - move in one
233       let ind = ind + &sw
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 )
240    endif
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)
248    if line =~ '^\s*#'
249       " Start of line for ada-pp
250       let ind = 0
251    elseif continuation && line =~ '^\s*('
252       " Don't do this if we've already indented due to the previous line
253       if ind == initind
254          let ind = ind + &sw
255       endif
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\>'
269       " End of loops
270       let ind = s:EndBlockIndent( ind, lnum, '\(\(while\|for\)\>.*\)\?\<loop\>', 'end\>\s*\<loop\>' )
271    elseif line =~ '^\s*end\>\s*\<record\>'
272       " End of records
273       let ind = s:EndBlockIndent( ind, lnum, '\(type\>.*\)\=\<record\>', 'end\>\s*\<record\>' )
274    elseif line =~ '^\s*end\>\s*\<procedure\>'
275       " End of procedures
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\>', '' )
287    endif
289    return ind
290 endfunction GetAdaIndent
292 finish " 1}}}
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