blob: fc1d9e068b79ae8830c48cd17be693bc15f6b6c3 [file] [log] [blame]
Bram Moolenaar7a329912010-05-21 12:05:36 +02001" Vim filetype plugin
2" Language: Markdown
3" Maintainer: Tim Pope <vimNOSPAM@tpope.org>
Bram Moolenaarc08ee742019-12-05 22:47:25 +01004" Last Change: 2019 Dec 05
Bram Moolenaar7a329912010-05-21 12:05:36 +02005
6if exists("b:did_ftplugin")
7 finish
8endif
9
10runtime! ftplugin/html.vim ftplugin/html_*.vim ftplugin/html/*.vim
Bram Moolenaar7a329912010-05-21 12:05:36 +020011
Bram Moolenaarc08ee742019-12-05 22:47:25 +010012setlocal comments=fb:*,fb:-,fb:+,n:> commentstring=<!--%s-->
Bram Moolenaar543b7ef2013-06-01 14:50:56 +020013setlocal formatoptions+=tcqln formatoptions-=r formatoptions-=o
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020014setlocal formatlistpat=^\\s*\\d\\+\\.\\s\\+\\\|^[-*+]\\s\\+\\\|^\\[^\\ze[^\\]]\\+\\]:
Bram Moolenaar7a329912010-05-21 12:05:36 +020015
Bram Moolenaarf1568ec2011-12-14 21:17:39 +010016if 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
Bram Moolenaar7a329912010-05-21 12:05:36 +020021
Bram Moolenaarc08ee742019-12-05 22:47:25 +010022function! s:NotCodeBlock(lnum) abort
23 return synIDattr(synID(v:lnum, 1, 1), 'name') !=# 'markdownCode'
24endfunction
25
26function! MarkdownFold() abort
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020027 let line = getline(v:lnum)
28
Bram Moolenaarc08ee742019-12-05 22:47:25 +010029 if line =~# '^#\+ ' && s:NotCodeBlock(v:lnum)
30 return ">" . match(line, ' ')
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020031 endif
32
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020033 let nextline = getline(v:lnum + 1)
Bram Moolenaarc08ee742019-12-05 22:47:25 +010034 if (line =~ '^.\+$') && (nextline =~ '^=\+$') && s:NotCodeBlock(v:lnum + 1)
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020035 return ">1"
36 endif
37
Bram Moolenaarc08ee742019-12-05 22:47:25 +010038 if (line =~ '^.\+$') && (nextline =~ '^-\+$') && s:NotCodeBlock(v:lnum + 1)
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020039 return ">2"
40 endif
41
42 return "="
43endfunction
44
Bram Moolenaarc08ee742019-12-05 22:47:25 +010045function! 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
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020067if has("folding") && exists("g:markdown_folding")
68 setlocal foldexpr=MarkdownFold()
69 setlocal foldmethod=expr
Bram Moolenaarc08ee742019-12-05 22:47:25 +010070 setlocal foldtext=MarkdownFoldText()
71 let b:undo_ftplugin .= " foldexpr< foldmethod< foldtext<"
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020072endif
73
Bram Moolenaar7a329912010-05-21 12:05:36 +020074" vim:set sw=2: