]> 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:

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