blob: 77b4ebf1efba3f711c45aaf892f3c51944adcb25 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001" Vim indent file
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002" Language: Autoconf configure.{ac,in} file
3" Maintainer: Nikolai Weibull <now@bitwi.se>
Bram Moolenaar57657d82006-04-21 22:12:41 +00004" Latest Revision: 2006-04-19
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005" TODO: how about nested [()]'s in one line
6" what's wrong with '\\\@!'?
Bram Moolenaar071d4272004-06-13 20:20:40 +00007
8" Only load this indent file when no other was loaded.
9if exists("b:did_indent")
10 finish
11endif
12
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000013runtime! indent/sh.vim " will set b:did_indent
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15setlocal indentexpr=GetConfigIndent()
16setlocal indentkeys=!^F,o,O,=then,=do,=else,=elif,=esac,=fi,=fin,=fil,=done
17
18" Only define the function once.
19if exists("*GetConfigIndent")
20 finish
21endif
22
23" get the offset (indent) of the end of the match of 'regexp' in 'line'
24function s:GetOffsetOf(line, regexp)
25 let end = matchend(a:line, a:regexp)
26 let width = 0
27 let i = 0
28 while i < end
29 if a:line[i] != "\t"
30 let width = width + 1
31 else
32 let width = width + &ts - (width % &ts)
33 endif
34 let i = i + 1
35 endwhile
36 return width
37endfunction
38
39function GetConfigIndent()
40 " Find a non-blank line above the current line.
41 let lnum = prevnonblank(v:lnum - 1)
42
43 " Hit the start of the file, use zero indent.
44 if lnum == 0
45 return 0
46 endif
47
48 " where to put this
49 let ind = GetShIndent()
50 let line = getline(lnum)
51
52 " if previous line has unmatched, unescaped opening parentheses,
53 " indent to its position. TODO: not failsafe if multiple ('s
54 if line =~ '\\\@<!([^)]*$'
55 let ind = s:GetOffsetOf(line, '\\\@!(')
56 endif
57
58 " if previous line has unmatched opening bracket,
59 " indent to its position. TODO: same as above
60 if line =~ '\[[^]]*$'
61 let ind = s:GetOffsetOf(line, '\[')
62 endif
63
64 " if previous line had an unmatched closing parantheses,
65 " indent to the matching opening parantheses
Bram Moolenaar899dddf2006-03-26 21:06:50 +000066 if line =~ '[^(]\+\\\@<!)$'
Bram Moolenaar071d4272004-06-13 20:20:40 +000067 call search(')', 'bW')
68 let lnum = searchpair('\\\@<!(', '', ')', 'bWn')
69 let ind = indent(lnum)
70 endif
71
72 " if previous line had an unmatched closing bracket,
73 " indent to the matching opening bracket
Bram Moolenaar899dddf2006-03-26 21:06:50 +000074 if line =~ '[^[]\+]$'
Bram Moolenaar071d4272004-06-13 20:20:40 +000075 call search(']', 'bW')
76 let lnum = searchpair('\[', '', ']', 'bWn')
77 let ind = indent(lnum)
78 endif
79
80 return ind
81endfunction