From: Hiroshi Shirosaki Date: Fri, 19 Feb 2016 01:45:26 +0000 (+0900) Subject: Add 'g:vim_markdown_fenced_languages' option X-Git-Url: https://git.madduck.net/etc/vim.git/commitdiff_plain/04e060dc062ee981f5c9bcc8f3b700f803da285f?ds=inline Add 'g:vim_markdown_fenced_languages' option Users can set filetype map in `.vimrc` for fenced code block syntax highlighting. Based on the patch by @citizenmatt. Fix #250 --- diff --git a/README.md b/README.md index 014c54e..c265d5b 100644 --- a/README.md +++ b/README.md @@ -139,6 +139,23 @@ To disable conceal regardless of `conceallevel` setting, add the following to yo let g:vim_markdown_conceal = 0 ``` +### Fenced code block languages + +You can use filetype name as fenced code block languages for syntax highlighting. +If you want to use different name from filetype, you can add it in your '.vimrc' like so: + +```vim +let g:vim_markdown_fenced_languages = ['csharp=cs'] +``` + +This will cause the following to be highlighted using the `cs` filetype syntax. + + ```csharp + ... + ``` + +Default is `['c++=cpp', 'viml=vim', 'bash=sh', 'ini=dosini']`. + ### Syntax extensions The following options control which syntax extensions will be turned on. They are off by default. diff --git a/doc/vim-markdown.txt b/doc/vim-markdown.txt index d1922b7..6e486de 100644 --- a/doc/vim-markdown.txt +++ b/doc/vim-markdown.txt @@ -14,7 +14,8 @@ Contents ~ 6. Text emphasis restriction to single-lines |vim-markdown-text-emphasis-restriction-to-single-lines| 7. Syntax Concealing |vim-markdown-syntax-concealing| - 8. Syntax extensions |vim-markdown-syntax-extensions| + 8. Fenced code block languages |vim-markdown-fenced-code-block-languages| + 9. Syntax extensions |vim-markdown-syntax-extensions| 1. LaTeX math |vim-markdown-latex-math| 2. YAML Front Matter |vim-markdown-yaml-front-matter| 3. TOML Front Matter |vim-markdown-toml-front-matter| @@ -151,6 +152,24 @@ your '.vimrc': > let g:vim_markdown_conceal = 0 < +------------------------------------------------------------------------------- + *vim-markdown-fenced-code-block-languages* +Fenced code block languages ~ + +You can use filetype name as fenced code block languages for syntax +highlighting. If you want to use different name from filetype, you can add it +in your '.vimrc' like so: +> + let g:vim_markdown_fenced_languages = ['csharp=cs'] +< +This will cause the following to be highlighted using the 'cs' filetype syntax. +> + ```csharp + ... + ``` +< +Default is "['c++=cpp', 'viml=vim', 'bash=sh', 'ini=dosini']". + ------------------------------------------------------------------------------- *vim-markdown-syntax-extensions* Syntax extensions ~ diff --git a/ftplugin/markdown.vim b/ftplugin/markdown.vim index c7832d7..e189ec4 100644 --- a/ftplugin/markdown.vim +++ b/ftplugin/markdown.vim @@ -614,12 +614,21 @@ command! -buffer Tocv call s:Toc('vertical') command! -buffer Toct call s:Toc('tab') " Heavily based on vim-notes - http://peterodding.com/code/vim/notes/ -let s:filetype_dict = { - \ 'c++': 'cpp', - \ 'viml': 'vim', - \ 'bash': 'sh', - \ 'ini': 'dosini' -\ } +if exists('g:vim_markdown_fenced_languages') + let s:filetype_dict = {} + for s:filetype in g:vim_markdown_fenced_languages + let key = matchstr(s:filetype, "[^=]*") + let val = matchstr(s:filetype, "[^=]*$") + let s:filetype_dict[key] = val + endfor +else + let s:filetype_dict = { + \ 'c++': 'cpp', + \ 'viml': 'vim', + \ 'bash': 'sh', + \ 'ini': 'dosini' + \ } +endif function! s:MarkdownHighlightSources(force) " Syntax highlight source code embedded in notes. diff --git a/test/syntax.vader b/test/syntax.vader index d2dbb13..98548a0 100644 --- a/test/syntax.vader +++ b/test/syntax.vader @@ -424,6 +424,19 @@ Execute (fenced code block syntax with alias language specifier): AssertEqual SyntaxOf('g:a'), 'vimVar' AssertEqual SyntaxOf('g:b'), 'vimVar' +Given markdown; +```csharp +var s = "foo"; +``` + +Execute (fenced code block languages setting): + let g:vim_markdown_fenced_languages = ['csharp=cs'] + source ../ftplugin/markdown.vim + let b:func = Markdown_GetFunc('vim-markdown/ftplugin/markdown.vim', 'MarkdownRefreshSyntax') + call b:func(0) + AssertEqual SyntaxOf('foo'), 'csString' + unlet g:vim_markdown_fenced_languages + Given markdown; ```a+b- code