blob: 2b963f139d3cbb72a33d8fbe883143489a9bf668 [file] [log] [blame]
Bram Moolenaar7a329912010-05-21 12:05:36 +02001" Vim filetype plugin
Bram Moolenaar3c053a12022-10-16 13:11:12 +01002" Language: Markdown
3" Maintainer: Tim Pope <https://github.com/tpope/vim-markdown>
4" Last Change: 2022 Oct 13
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 Moolenaar3c053a12022-10-16 13:11:12 +010012let s:keepcpo= &cpo
13set cpo&vim
14
Bram Moolenaarc08ee742019-12-05 22:47:25 +010015setlocal comments=fb:*,fb:-,fb:+,n:> commentstring=<!--%s-->
Bram Moolenaar543b7ef2013-06-01 14:50:56 +020016setlocal formatoptions+=tcqln formatoptions-=r formatoptions-=o
Bram Moolenaar3c053a12022-10-16 13:11:12 +010017setlocal formatlistpat=^\\s*\\d\\+\\.\\s\\+\\\|^\\s*[-*+]\\s\\+\\\|^\\[^\\ze[^\\]]\\+\\]:\\&^.\\{4\\}
Bram Moolenaar7a329912010-05-21 12:05:36 +020018
Bram Moolenaarf1568ec2011-12-14 21:17:39 +010019if exists('b:undo_ftplugin')
Bram Moolenaar3c053a12022-10-16 13:11:12 +010020 let b:undo_ftplugin .= "|setl cms< com< fo< flp< et< ts< sts< sw<"
Bram Moolenaarf1568ec2011-12-14 21:17:39 +010021else
Bram Moolenaar3c053a12022-10-16 13:11:12 +010022 let b:undo_ftplugin = "setl cms< com< fo< flp< et< ts< sts< sw<"
23endif
24
25if get(g:, 'markdown_recommended_style', 1)
26 setlocal expandtab tabstop=4 softtabstop=4 shiftwidth=4
27endif
28
29if !exists("g:no_plugin_maps") && !exists("g:no_markdown_maps")
30 nnoremap <silent><buffer> [[ :<C-U>call search('\%(^#\{1,5\}\s\+\S\\|^\S.*\n^[=-]\+$\)', "bsW")<CR>
31 nnoremap <silent><buffer> ]] :<C-U>call search('\%(^#\{1,5\}\s\+\S\\|^\S.*\n^[=-]\+$\)', "sW")<CR>
32 xnoremap <silent><buffer> [[ :<C-U>exe "normal! gv"<Bar>call search('\%(^#\{1,5\}\s\+\S\\|^\S.*\n^[=-]\+$\)', "bsW")<CR>
33 xnoremap <silent><buffer> ]] :<C-U>exe "normal! gv"<Bar>call search('\%(^#\{1,5\}\s\+\S\\|^\S.*\n^[=-]\+$\)', "sW")<CR>
34 let b:undo_ftplugin .= '|sil! nunmap <buffer> [[|sil! nunmap <buffer> ]]|sil! xunmap <buffer> [[|sil! xunmap <buffer> ]]'
Bram Moolenaarf1568ec2011-12-14 21:17:39 +010035endif
Bram Moolenaar7a329912010-05-21 12:05:36 +020036
Bram Moolenaarc08ee742019-12-05 22:47:25 +010037function! s:NotCodeBlock(lnum) abort
Bram Moolenaar3c053a12022-10-16 13:11:12 +010038 return synIDattr(synID(a:lnum, 1, 1), 'name') !=# 'markdownCode'
Bram Moolenaarc08ee742019-12-05 22:47:25 +010039endfunction
40
41function! MarkdownFold() abort
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020042 let line = getline(v:lnum)
43
Bram Moolenaarc08ee742019-12-05 22:47:25 +010044 if line =~# '^#\+ ' && s:NotCodeBlock(v:lnum)
45 return ">" . match(line, ' ')
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020046 endif
47
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020048 let nextline = getline(v:lnum + 1)
Bram Moolenaarc08ee742019-12-05 22:47:25 +010049 if (line =~ '^.\+$') && (nextline =~ '^=\+$') && s:NotCodeBlock(v:lnum + 1)
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020050 return ">1"
51 endif
52
Bram Moolenaarc08ee742019-12-05 22:47:25 +010053 if (line =~ '^.\+$') && (nextline =~ '^-\+$') && s:NotCodeBlock(v:lnum + 1)
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020054 return ">2"
55 endif
56
57 return "="
58endfunction
59
Bram Moolenaarc08ee742019-12-05 22:47:25 +010060function! s:HashIndent(lnum) abort
61 let hash_header = matchstr(getline(a:lnum), '^#\{1,6}')
62 if len(hash_header)
63 return hash_header
64 else
65 let nextline = getline(a:lnum + 1)
66 if nextline =~# '^=\+\s*$'
67 return '#'
68 elseif nextline =~# '^-\+\s*$'
69 return '##'
70 endif
71 endif
72endfunction
73
74function! MarkdownFoldText() abort
75 let hash_indent = s:HashIndent(v:foldstart)
76 let title = substitute(getline(v:foldstart), '^#\+\s*', '', '')
77 let foldsize = (v:foldend - v:foldstart + 1)
78 let linecount = '['.foldsize.' lines]'
79 return hash_indent.' '.title.' '.linecount
80endfunction
81
Bram Moolenaar3c053a12022-10-16 13:11:12 +010082if has("folding") && get(g:, "markdown_folding", 0)
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020083 setlocal foldexpr=MarkdownFold()
84 setlocal foldmethod=expr
Bram Moolenaarc08ee742019-12-05 22:47:25 +010085 setlocal foldtext=MarkdownFoldText()
Bram Moolenaar3c053a12022-10-16 13:11:12 +010086 let b:undo_ftplugin .= "|setl foldexpr< foldmethod< foldtext<"
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020087endif
88
Bram Moolenaar3c053a12022-10-16 13:11:12 +010089let &cpo = s:keepcpo
90unlet s:keepcpo
91
Bram Moolenaar7a329912010-05-21 12:05:36 +020092" vim:set sw=2: