1" Vim filetype plugin
2" Language:		Markdown
3" Maintainer:		Tim Pope <[email protected]>
4" Last Change:		2019 Dec 05
5
6if exists("b:did_ftplugin")
7  finish
8endif
9
10runtime! ftplugin/html.vim ftplugin/html_*.vim ftplugin/html/*.vim
11
12setlocal comments=fb:*,fb:-,fb:+,n:> commentstring=<!--%s-->
13setlocal formatoptions+=tcqln formatoptions-=r formatoptions-=o
14setlocal formatlistpat=^\\s*\\d\\+\\.\\s\\+\\\|^[-*+]\\s\\+\\\|^\\[^\\ze[^\\]]\\+\\]:
15
16if exists('b:undo_ftplugin')
17  let b:undo_ftplugin .= "|setl cms< com< fo< flp<"
18else
19  let b:undo_ftplugin = "setl cms< com< fo< flp<"
20endif
21
22function! s:NotCodeBlock(lnum) abort
23  return synIDattr(synID(v:lnum, 1, 1), 'name') !=# 'markdownCode'
24endfunction
25
26function! MarkdownFold() abort
27  let line = getline(v:lnum)
28
29  if line =~# '^#\+ ' && s:NotCodeBlock(v:lnum)
30    return ">" . match(line, ' ')
31  endif
32
33  let nextline = getline(v:lnum + 1)
34  if (line =~ '^.\+$') && (nextline =~ '^=\+$') && s:NotCodeBlock(v:lnum + 1)
35    return ">1"
36  endif
37
38  if (line =~ '^.\+$') && (nextline =~ '^-\+$') && s:NotCodeBlock(v:lnum + 1)
39    return ">2"
40  endif
41
42  return "="
43endfunction
44
45function! s:HashIndent(lnum) abort
46  let hash_header = matchstr(getline(a:lnum), '^#\{1,6}')
47  if len(hash_header)
48    return hash_header
49  else
50    let nextline = getline(a:lnum + 1)
51    if nextline =~# '^=\+\s*$'
52      return '#'
53    elseif nextline =~# '^-\+\s*$'
54      return '##'
55    endif
56  endif
57endfunction
58
59function! MarkdownFoldText() abort
60  let hash_indent = s:HashIndent(v:foldstart)
61  let title = substitute(getline(v:foldstart), '^#\+\s*', '', '')
62  let foldsize = (v:foldend - v:foldstart + 1)
63  let linecount = '['.foldsize.' lines]'
64  return hash_indent.' '.title.' '.linecount
65endfunction
66
67if has("folding") && exists("g:markdown_folding")
68  setlocal foldexpr=MarkdownFold()
69  setlocal foldmethod=expr
70  setlocal foldtext=MarkdownFoldText()
71  let b:undo_ftplugin .= " foldexpr< foldmethod< foldtext<"
72endif
73
74" vim:set sw=2:
75