]> 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:

Fix URL syntax regex
[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:IsMkdCode(lnum)
22     let name = synIDattr(synID(a:lnum, 1, 0), 'name')
23     return (name =~ '^mkd\%(Code$\|Snippet\)' || name != '' && name !~ '^\%(mkd\|html\)')
24 endfunction
25
26 function! s:IsLiStart(line)
27     return a:line !~ '^ *\([*-]\)\%( *\1\)\{2}\%( \|\1\)*$' &&
28       \    a:line =~ '^\s*[*+-] \+'
29 endfunction
30
31 function! s:IsHeaderLine(line)
32     return a:line =~ '^\s*#'
33 endfunction
34
35 function! s:IsBlankLine(line)
36     return a:line =~ '^$'
37 endfunction
38
39 function! s:PrevNonBlank(lnum)
40     let i = a:lnum
41     while i > 1 && s:IsBlankLine(getline(i))
42         let i -= 1
43     endwhile
44     return i
45 endfunction
46
47 function GetMarkdownIndent()
48     if v:lnum > 2 && s:IsBlankLine(getline(v:lnum - 1)) && s:IsBlankLine(getline(v:lnum - 2))
49         return 0
50     endif
51     let list_ind = 4
52     " Find a non-blank line above the current line.
53     let lnum = s:PrevNonBlank(v:lnum - 1)
54     " At the start of the file use zero indent.
55     if lnum == 0 | return 0 | endif
56     let ind = indent(lnum)
57     let line = getline(lnum)    " Last line
58     let cline = getline(v:lnum) " Current line
59     if s:IsLiStart(cline)
60         " Current line is the first line of a list item, do not change indent
61         return indent(v:lnum)
62     elseif s:IsHeaderLine(cline) && !s:IsMkdCode(v:lnum)
63         " Current line is the header, do not indent
64         return 0
65     elseif s:IsLiStart(line)
66         if s:IsMkdCode(lnum)
67             return ind
68         else
69             " Last line is the first line of a list item, increase indent
70             return ind + list_ind
71         end
72     else
73         return ind
74     endif
75 endfunction