blob: d4ee5ac24231b7db50466da11c0d35cdd1c44b58 [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>
Tim Pope244f0162023-12-28 12:50:18 -05004" Last Change: 2023 Dec 28
Riley Bruins0a083062024-06-03 20:40:45 +02005" 2024 May 24 by Riley Bruins <ribru17@gmail.com> ('commentstring')
Bram Moolenaar7a329912010-05-21 12:05:36 +02006
7if exists("b:did_ftplugin")
8 finish
9endif
10
11runtime! ftplugin/html.vim ftplugin/html_*.vim ftplugin/html/*.vim
Bram Moolenaar7a329912010-05-21 12:05:36 +020012
Bram Moolenaar3c053a12022-10-16 13:11:12 +010013let s:keepcpo= &cpo
14set cpo&vim
15
Riley Bruins0a083062024-06-03 20:40:45 +020016setlocal comments=fb:*,fb:-,fb:+,n:> commentstring=<!--\ %s\ -->
Bram Moolenaar543b7ef2013-06-01 14:50:56 +020017setlocal formatoptions+=tcqln formatoptions-=r formatoptions-=o
Bram Moolenaar3c053a12022-10-16 13:11:12 +010018setlocal formatlistpat=^\\s*\\d\\+\\.\\s\\+\\\|^\\s*[-*+]\\s\\+\\\|^\\[^\\ze[^\\]]\\+\\]:\\&^.\\{4\\}
Bram Moolenaar7a329912010-05-21 12:05:36 +020019
Bram Moolenaarf1568ec2011-12-14 21:17:39 +010020if exists('b:undo_ftplugin')
Bram Moolenaar3c053a12022-10-16 13:11:12 +010021 let b:undo_ftplugin .= "|setl cms< com< fo< flp< et< ts< sts< sw<"
Bram Moolenaarf1568ec2011-12-14 21:17:39 +010022else
Bram Moolenaar3c053a12022-10-16 13:11:12 +010023 let b:undo_ftplugin = "setl cms< com< fo< flp< et< ts< sts< sw<"
24endif
25
26if get(g:, 'markdown_recommended_style', 1)
27 setlocal expandtab tabstop=4 softtabstop=4 shiftwidth=4
28endif
29
30if !exists("g:no_plugin_maps") && !exists("g:no_markdown_maps")
31 nnoremap <silent><buffer> [[ :<C-U>call search('\%(^#\{1,5\}\s\+\S\\|^\S.*\n^[=-]\+$\)', "bsW")<CR>
32 nnoremap <silent><buffer> ]] :<C-U>call search('\%(^#\{1,5\}\s\+\S\\|^\S.*\n^[=-]\+$\)', "sW")<CR>
33 xnoremap <silent><buffer> [[ :<C-U>exe "normal! gv"<Bar>call search('\%(^#\{1,5\}\s\+\S\\|^\S.*\n^[=-]\+$\)', "bsW")<CR>
34 xnoremap <silent><buffer> ]] :<C-U>exe "normal! gv"<Bar>call search('\%(^#\{1,5\}\s\+\S\\|^\S.*\n^[=-]\+$\)', "sW")<CR>
35 let b:undo_ftplugin .= '|sil! nunmap <buffer> [[|sil! nunmap <buffer> ]]|sil! xunmap <buffer> [[|sil! xunmap <buffer> ]]'
Bram Moolenaarf1568ec2011-12-14 21:17:39 +010036endif
Bram Moolenaar7a329912010-05-21 12:05:36 +020037
Bram Moolenaarc08ee742019-12-05 22:47:25 +010038function! s:NotCodeBlock(lnum) abort
Tim Pope244f0162023-12-28 12:50:18 -050039 return synIDattr(synID(a:lnum, 1, 1), 'name') !=# 'markdownCodeBlock'
Bram Moolenaarc08ee742019-12-05 22:47:25 +010040endfunction
41
42function! MarkdownFold() abort
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020043 let line = getline(v:lnum)
44
Bram Moolenaarc08ee742019-12-05 22:47:25 +010045 if line =~# '^#\+ ' && s:NotCodeBlock(v:lnum)
46 return ">" . match(line, ' ')
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020047 endif
48
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020049 let nextline = getline(v:lnum + 1)
Bram Moolenaarc08ee742019-12-05 22:47:25 +010050 if (line =~ '^.\+$') && (nextline =~ '^=\+$') && s:NotCodeBlock(v:lnum + 1)
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020051 return ">1"
52 endif
53
Bram Moolenaarc08ee742019-12-05 22:47:25 +010054 if (line =~ '^.\+$') && (nextline =~ '^-\+$') && s:NotCodeBlock(v:lnum + 1)
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020055 return ">2"
56 endif
57
58 return "="
59endfunction
60
Bram Moolenaarc08ee742019-12-05 22:47:25 +010061function! s:HashIndent(lnum) abort
62 let hash_header = matchstr(getline(a:lnum), '^#\{1,6}')
63 if len(hash_header)
64 return hash_header
65 else
66 let nextline = getline(a:lnum + 1)
67 if nextline =~# '^=\+\s*$'
68 return '#'
69 elseif nextline =~# '^-\+\s*$'
70 return '##'
71 endif
72 endif
73endfunction
74
75function! MarkdownFoldText() abort
76 let hash_indent = s:HashIndent(v:foldstart)
77 let title = substitute(getline(v:foldstart), '^#\+\s*', '', '')
78 let foldsize = (v:foldend - v:foldstart + 1)
79 let linecount = '['.foldsize.' lines]'
80 return hash_indent.' '.title.' '.linecount
81endfunction
82
Bram Moolenaar3c053a12022-10-16 13:11:12 +010083if has("folding") && get(g:, "markdown_folding", 0)
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020084 setlocal foldexpr=MarkdownFold()
85 setlocal foldmethod=expr
Bram Moolenaarc08ee742019-12-05 22:47:25 +010086 setlocal foldtext=MarkdownFoldText()
Bram Moolenaar3c053a12022-10-16 13:11:12 +010087 let b:undo_ftplugin .= "|setl foldexpr< foldmethod< foldtext<"
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020088endif
89
Bram Moolenaar3c053a12022-10-16 13:11:12 +010090let &cpo = s:keepcpo
91unlet s:keepcpo
92
Bram Moolenaar7a329912010-05-21 12:05:36 +020093" vim:set sw=2: