CTRLP
[my-vim-dotfolder.git] / indent / haskell.vim
blobdc7c649d8124d8c976260a44d98aaf0d48c96469
1 " Vim indent file
2 " Language:     Haskell
3 " Author:       motemen <motemen@gmail.com>
4 " Version:      0.1
5 " Last Change:  2007-07-25
7 " Modify g:haskell_indent_if and g:haskell_indent_case to
8 " change indentation for `if'(default 3) and `case'(default 5).
9 " Example (in .vimrc):
10 " > let g:haskell_indent_if = 2
12 if exists('b:did_indent')
13     finish
14 endif
16 let b:did_indent = 1
18 if !exists('g:haskell_indent_if')
19     " if bool
20     " >>>then ...
21     " >>>else ...
22     let g:haskell_indent_if = 3
23 endif
25 if !exists('g:haskell_indent_case')
26     " case xs of
27     " >>>>>[] -> ...
28     " >>>>>(y:ys) -> ...
29     let g:haskell_indent_case = 5
30 endif
32 setlocal indentexpr=GetHaskellIndent()
33 setlocal indentkeys=!^F,o,O
35 function! GetHaskellIndent()
36     let line = substitute(getline(getpos('.')[1] - 1), '\t', repeat(' ', &tabstop), 'g')
38     if line =~ '[!#$%&*+./<=>?@\\^|~-]$\|\<do$'
39         return match(line, '\s*where \zs\|\S') + &shiftwidth
40     endif
42     if line =~ '{$'
43         return match(line, '\s*where \zs\|\S') + &shiftwidth
44     endif
46     if line =~ '^\(instance\|class\).*\&.*where$'
47         return &shiftwidth
48     endif
50     if line =~ ')$'
51         let pos = getpos('.')
52         normal k$
53         let paren_end   = getpos('.')
54         normal %
55         let paren_begin = getpos('.')
56         call setpos('.', pos)
57         if paren_begin[1] != paren_end[1]
58             return paren_begin[2] - 1
59         endif
60     endif
62     if line !~ '\<else\>'
63         let s = match(line, '\<if\>.*\&.*\zs\<then\>')
64         if s > 0
65             return s
66         endif
68         let s = match(line, '\<if\>')
69         if s > 0
70             return s + g:haskell_indent_if
71         endif
72     endif
74     let s = match(line, '\<do\s\+\zs[^{]\|\<where\s\+\zs\w\|\<let\s\+\zs\S\|^\s*\zs|\s')
75     if s > 0
76         return s
77     endif
79     let s = match(line, '\<case\>')
80     if s > 0
81         return s + g:haskell_indent_case
82     endif
84     return match(line, '\S')
85 endfunction