1" markdown Text with R statements 2" Language: markdown with R code chunks 3" Homepage: https://github.com/jalvesaq/R-Vim-runtime 4" Last Change: Sat Aug 25, 2018 03:44PM 5" 6" For highlighting pandoc extensions to markdown like citations and TeX and 7" many other advanced features like folding of markdown sections, it is 8" recommended to install the vim-pandoc filetype plugin as well as the 9" vim-pandoc-syntax filetype plugin from https://github.com/vim-pandoc. 10 11 12if exists("b:current_syntax") 13 finish 14endif 15 16" Configuration if not using pandoc syntax: 17" Add syntax highlighting of YAML header 18let g:rmd_syn_hl_yaml = get(g:, 'rmd_syn_hl_yaml', 1) 19" Add syntax highlighting of citation keys 20let g:rmd_syn_hl_citations = get(g:, 'rmd_syn_hl_citations', 1) 21" Highlight the header of the chunk of R code 22let g:rmd_syn_hl_chunk = get(g:, 'g:rmd_syn_hl_chunk', 0) 23 24" Pandoc-syntax has more features, but it is slower. 25" https://github.com/vim-pandoc/vim-pandoc-syntax 26let g:pandoc#syntax#codeblocks#embeds#langs = get(g:, 'pandoc#syntax#codeblocks#embeds#langs', ['r']) 27runtime syntax/pandoc.vim 28if exists("b:current_syntax") 29 " Fix recognition of R code 30 syn region pandocDelimitedCodeBlock_r start=/^```{r\>.*}$/ end=/^```$/ contained containedin=pandocDelimitedCodeBlock contains=@R 31 syn region rmdrInline matchgroup=rmdInlineDelim start="`r " end="`" contains=@R containedin=pandocLaTeXRegion,yamlFlowString keepend 32 hi def link rmdInlineDelim Delimiter 33 let b:current_syntax = "rmd" 34 finish 35endif 36 37let s:cpo_save = &cpo 38set cpo&vim 39 40" R chunks will not be highlighted by syntax/markdown because their headers 41" follow a non standard pattern: "```{lang" instead of "^```lang". 42" Make a copy of g:markdown_fenced_languages to highlight the chunks later: 43if exists('g:markdown_fenced_languages') 44 if !exists('g:rmd_fenced_languages') 45 let g:rmd_fenced_languages = deepcopy(g:markdown_fenced_languages) 46 let g:markdown_fenced_languages = [] 47 endif 48else 49 let g:rmd_fenced_languages = ['r'] 50endif 51 52runtime syntax/markdown.vim 53 54" Now highlight chunks: 55for s:type in g:rmd_fenced_languages 56 if s:type =~ '=' 57 let s:lng = substitute(s:type, '=.*', '') 58 let s:nm = substitute(s:type, '.*=', '') 59 else 60 let s:lng = s:type 61 let s:nm = s:type 62 endif 63 unlet! b:current_syntax 64 exe 'syn include @Rmd'.s:nm.' syntax/'.s:lng.'.vim' 65 if g:rmd_syn_hl_chunk 66 exe 'syn region rmd'.s:nm.'ChunkDelim matchgroup=rmdCodeDelim start="^\s*```\s*{\s*'.s:nm.'\>" matchgroup=rmdCodeDelim end="}$" keepend containedin=rmd'.s:nm.'Chunk contains=@Rmd'.s:nm 67 exe 'syn region rmd'.s:nm.'Chunk start="^\s*```\s*{\s*'.s:nm.'\>.*$" matchgroup=rmdCodeDelim end="^\s*```\ze\s*$" keepend contains=rmd'.s:nm.'ChunkDelim,@Rmd'.s:nm 68 else 69 exe 'syn region rmd'.s:nm.'Chunk matchgroup=rmdCodeDelim start="^\s*```\s*{\s*'.s:nm.'\>.*$" matchgroup=rmdCodeDelim end="^\s*```\ze\s*$" keepend contains=@Rmd'.s:nm 70 endif 71 exe 'syn region rmd'.s:nm.'Inline matchgroup=rmdInlineDelim start="`'.s:nm.' " end="`" contains=@Rmd'.s:nm.' keepend' 72endfor 73unlet! s:type 74 75hi def link rmdInlineDelim Delimiter 76hi def link rmdCodeDelim Delimiter 77 78" You don't need this if either your markdown/syntax.vim already highlights 79" the YAML header or you are writing standard markdown 80if g:rmd_syn_hl_yaml 81 " Minimum highlighting of yaml header 82 syn match rmdYamlFieldTtl /^\s*\zs\w*\ze:/ contained 83 syn match rmdYamlFieldTtl /^\s*-\s*\zs\w*\ze:/ contained 84 syn region yamlFlowString matchgroup=yamlFlowStringDelimiter start='"' skip='\\"' end='"' contains=yamlEscape,rmdrInline contained 85 syn region yamlFlowString matchgroup=yamlFlowStringDelimiter start="'" skip="''" end="'" contains=yamlSingleEscape,rmdrInline contained 86 syn match yamlEscape contained '\\\%([\\"abefnrtv\^0_ NLP\n]\|x\x\x\|u\x\{4}\|U\x\{8}\)' 87 syn match yamlSingleEscape contained "''" 88 syn region pandocYAMLHeader matchgroup=rmdYamlBlockDelim start=/\%(\%^\|\_^\s*\n\)\@<=\_^-\{3}\ze\n.\+/ end=/^\([-.]\)\1\{2}$/ keepend contains=rmdYamlFieldTtl,yamlFlowString 89 hi def link rmdYamlBlockDelim Delimiter 90 hi def link rmdYamlFieldTtl Identifier 91 hi def link yamlFlowString String 92endif 93 94" You don't need this if either your markdown/syntax.vim already highlights 95" citations or you are writing standard markdown 96if g:rmd_syn_hl_citations 97 " From vim-pandoc-syntax 98 " parenthetical citations 99 syn match pandocPCite /\^\@<!\[[^\[\]]\{-}-\{0,1}@[[:alnum:]_][[:alnum:]à-öø-ÿÀ-ÖØ-ß_:.#$%&\-+?<>~\/]*.\{-}\]/ contains=pandocEmphasis,pandocStrong,pandocLatex,pandocCiteKey,@Spell,pandocAmpersandEscape display 100 " in-text citations with location 101 syn match pandocICite /@[[:alnum:]_][[:alnum:]à-öø-ÿÀ-ÖØ-ß_:.#$%&\-+?<>~\/]*\s\[.\{-1,}\]/ contains=pandocCiteKey,@Spell display 102 " cite keys 103 syn match pandocCiteKey /\(-\=@[[:alnum:]_][[:alnum:]à-öø-ÿÀ-ÖØ-ß_:.#$%&\-+?<>~\/]*\)/ containedin=pandocPCite,pandocICite contains=@NoSpell display 104 syn match pandocCiteAnchor /[-@]/ contained containedin=pandocCiteKey display 105 syn match pandocCiteLocator /[\[\]]/ contained containedin=pandocPCite,pandocICite 106 hi def link pandocPCite Operator 107 hi def link pandocICite Operator 108 hi def link pandocCiteKey Label 109 hi def link pandocCiteAnchor Operator 110 hi def link pandocCiteLocator Operator 111endif 112 113let b:current_syntax = "rmd" 114 115let &cpo = s:cpo_save 116unlet s:cpo_save 117 118" vim: ts=8 sw=2 119