import all my dotfiles, awesome included.
[~madcoder/dotfiles.git] / vim / indent / mail.vim
1 " Vim indent file
2
3 if exists("b:did_indent")
4   finish
5 endif
6
7 let b:did_indent = 1
8
9 if !exists("g:mail_itemize_chars")
10   let g:mail_itemize_chars='o*+-'
11 endif
12
13 let s:item_cap_re='^ *\(['.g:mail_itemize_chars.']\) .*$'
14 let s:item_re='^ *['.g:mail_itemize_chars.'] '
15 let s:void_re='^\([>}].*\| *\)$'
16
17 setlocal nocindent
18 setlocal comments=n:},n:>
19 setlocal nolisp
20 setlocal nosmartindent
21 setlocal noautoindent
22 setlocal indentexpr=GetMailIndent()
23 setlocal indentkeys=!^F,*<return>,o,O,<space>
24
25 " Only define the function once.
26 if exists("*GetMailIndent")
27   finish
28 endif
29
30 function! s:GetItemIndent(lnum)
31   let lnum = a:lnum
32   let char = substitute(getline(lnum), s:item_cap_re, '\1', '')
33   let maxind = 100
34   let lastind = -1
35
36   while lnum >= 1
37     let lnum = lnum - 1
38     let line = getline(lnum)
39     let ind = indent(lnum)
40
41     if line =~ s:void_re
42       return lastind < 0 ? 2 : lastind
43     end
44
45     if ind < maxind
46       let maxind = ind
47     
48       if line =~ s:item_re
49         if lastind < 0
50           let lastind = ind + 4
51         end
52
53         if char == substitute(line, s:item_cap_re, '\1', '')
54           return ind
55         end
56       end
57    
58     end
59   endwhile
60 endfunction
61
62 function GetMailIndent()
63   if v:lnum <= 1
64     return 0
65   end
66
67   let ind2 = indent(v:lnum - 2)
68   let ind1 = indent(v:lnum - 1)
69   let pre2 = getline(v:lnum - 2)
70   let pre1 = getline(v:lnum - 1)
71   let cur  = getline(v:lnum)
72
73   if cur =~ s:void_re || pre1 == '-- ' || cur =~ '-- '
74     return 0
75   endif
76
77   let ind = ind1
78
79   if pre1 =~ s:item_re
80     let ind = ind + 2
81   elseif pre2 =~ s:void_re || (ind2 <= 2 && pre2 =~ '[^a-zA-Z0-9]$')
82     let ind = ind - 2
83   end
84
85   let len = strlen(pre1) + strlen( substitute(cur, '^ *\([^ ]*\).*$', '\1', '') )
86   if pre1 =~ '[^a-zA-Z0-9]$' && len + 1 < &tw
87     if ind1 <= 2 && pre1 !~ s:item_re
88       let ind = 2
89     else
90       let ind = 0
91     end
92   end
93
94   if cur =~ s:item_re
95     let ind = s:GetItemIndent(v:lnum)
96   end
97
98   return ind
99 endfunction
100
101 " vim: set sts=2 sw=2: