]> git.madduck.net Git - etc/vim.git/blob - plugin/mkd.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:

Support indented code fences inside lists
[etc/vim.git] / plugin / mkd.vim
1 " Heavily based on vim-notes - http://peterodding.com/code/vim/notes/
2 function! s:Markdown_highlight_sources()
3     " Syntax highlight source code embedded in notes.
4     " Look for code blocks in the current file
5     let filetypes = {}
6     for line in getline(1, '$')
7         let ft = matchstr(line, '```\zs\w*\>')
8         if !empty(ft) && ft !~ '^\d*$' | let filetypes[ft] = 1 | endif
9     endfor
10     if !exists('b:mkd_known_filetypes')
11         let b:mkd_known_filetypes = {}
12     endif
13     if b:mkd_known_filetypes == filetypes || empty(filetypes)
14         return
15     endif
16
17     " Now we're ready to actually highlight the code blocks.
18     let startgroup = 'mkdCodeStart'
19     let endgroup = 'mkdCodeEnd'
20     for ft in keys(filetypes)
21         if !has_key(b:mkd_known_filetypes, ft)
22
23             let group = 'mkdSnippet' . toupper(ft)
24             let include = s:syntax_include(ft)
25             let command = 'syntax region %s matchgroup=%s start="^\s*```%s$" matchgroup=%s end="\s*```$" keepend contains=%s%s'
26             execute printf(command, group, startgroup, ft, endgroup, include, has('conceal') ? ' concealends' : '')
27             execute printf('syntax cluster mkdNonListItem add=%s', group)
28
29             let b:mkd_known_filetypes[ft] = 1
30         endif
31     endfor
32 endfunction
33
34 function! s:syntax_include(filetype)
35     " Include the syntax highlighting of another {filetype}.
36     let grouplistname = '@' . toupper(a:filetype)
37     " Unset the name of the current syntax while including the other syntax
38     " because some syntax scripts do nothing when "b:current_syntax" is set
39     if exists('b:current_syntax')
40         let syntax_save = b:current_syntax
41         unlet b:current_syntax
42     endif
43     try
44         execute 'syntax include' grouplistname 'syntax/' . a:filetype . '.vim'
45         execute 'syntax include' grouplistname 'after/syntax/' . a:filetype . '.vim'
46     catch /E484/
47         " Ignore missing scripts
48     endtry
49     " Restore the name of the current syntax
50     if exists('syntax_save')
51         let b:current_syntax = syntax_save
52     elseif exists('b:current_syntax')
53         unlet b:current_syntax
54     endif
55     return grouplistname
56 endfunction
57
58
59 function! s:Markdown_refresh_syntax()
60     if &filetype == 'mkd' && line('$') > 1
61         call s:Markdown_highlight_sources()
62     endif
63 endfunction
64
65 augroup Mkd
66     autocmd!
67     au BufReadPost * call s:Markdown_refresh_syntax()
68     au BufReadPost,BufWritePost * call s:Markdown_refresh_syntax()
69     au InsertEnter,InsertLeave * call s:Markdown_refresh_syntax()
70     au CursorHold,CursorHoldI * call s:Markdown_refresh_syntax()
71 augroup END