]> git.madduck.net Git - etc/vim.git/blob - indent/markdown.vim

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Improve performance of folding
[etc/vim.git] / indent / markdown.vim
1 if exists("b:did_indent") | finish | endif
2 let b:did_indent = 1
3
4 setlocal indentexpr=GetMarkdownIndent()
5 setlocal nolisp
6 setlocal autoindent
7
8 " Automatically insert bullets
9 setlocal formatoptions+=r
10 " Do not automatically insert bullets when auto-wrapping with text-width
11 setlocal formatoptions-=c
12 " Accept various markers as bullets
13 setlocal comments=b:*,b:+,b:-
14
15 " Automatically continue blockquote on line break
16 setlocal comments+=b:>
17
18 " Only define the function once
19 if exists("*GetMarkdownIndent") | finish | endif
20
21 function! s:is_mkdCode(lnum)
22     return synIDattr(synID(a:lnum, 1, 0), 'name') == 'mkdCode'
23 endfunction
24
25 function! s:is_li_start(line)
26     return a:line !~ '^ *\([*-]\)\%( *\1\)\{2}\%( \|\1\)*$' &&
27       \    a:line =~ '^\s*[*+-] \+'
28 endfunction
29
30 function! s:is_blank_line(line)
31     return a:line =~ '^$'
32 endfunction
33
34 function! s:prevnonblank(lnum)
35     let i = a:lnum
36     while i > 1 && s:is_blank_line(getline(i))
37         let i -= 1
38     endwhile
39     return i
40 endfunction
41
42 function GetMarkdownIndent()
43     let list_ind = 4
44     " Find a non-blank line above the current line.
45     let lnum = prevnonblank(v:lnum - 1)
46     " At the start of the file use zero indent.
47     if lnum == 0 | return 0 | endif
48     let ind = indent(lnum)
49     let line = getline(lnum)    " Last line
50     let cline = getline(v:lnum) " Current line
51     if s:is_li_start(cline) 
52         " Current line is the first line of a list item, do not change indent
53         return indent(v:lnum)
54     elseif s:is_li_start(line)
55         if s:is_mkdCode(lnum)
56             return ind
57         else
58             " Last line is the first line of a list item, increase indent
59             return ind + list_ind
60         end
61     else
62         return ind
63     endif
64 endfunction