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: Wed Apr 21, 2021 09:55AM 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" Highlight the header of the chunks as R code 17let g:rmd_syn_hl_chunk = get(g:, 'rmd_syn_hl_chunk', 0) 18 19" Pandoc-syntax has more features, but it is slower. 20" https://github.com/vim-pandoc/vim-pandoc-syntax 21let g:pandoc#syntax#codeblocks#embeds#langs = get(g:, 'pandoc#syntax#codeblocks#embeds#langs', ['r']) 22runtime syntax/pandoc.vim 23if exists("b:current_syntax") 24 " Recognize inline R code 25 syn region rmdrInline matchgroup=rmdInlineDelim start="`r " end="`" contains=@R containedin=pandocLaTeXRegion,yamlFlowString keepend 26 hi def link rmdInlineDelim Delimiter 27 28 " Fix recognition of language chunks (code adapted from pandoc, 2021-03-28) 29 " Knitr requires braces in the block's header 30 for s:lng in g:pandoc#syntax#codeblocks#embeds#langs 31 let s:nm = matchstr(s:lng, '^[^=]*') 32 exe 'syn clear pandocDelimitedCodeBlock_'.s:nm 33 exe 'syn clear pandocDelimitedCodeBlockinBlockQuote_'.s:nm 34 if g:rmd_syn_hl_chunk 35 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=@R' 36 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,@'.toupper(s:nm) 37 else 38 exe 'syn region rmd'.s:nm.'Chunk matchgroup=rmdCodeDelim start="^\s*```\s*{\s*'.s:nm.'\>.*$" matchgroup=rmdCodeDelim end="^\s*```\ze\s*$" keepend contains=@'.toupper(s:nm) 39 endif 40 endfor 41 unlet s:lng 42 unlet s:nm 43 hi def link rmdInlineDelim Delimiter 44 hi def link rmdCodeDelim Delimiter 45 let b:current_syntax = "rmd" 46 finish 47endif 48 49" Configuration if not using pandoc syntax: 50" Add syntax highlighting of YAML header 51let g:rmd_syn_hl_yaml = get(g:, 'rmd_syn_hl_yaml', 1) 52" Add syntax highlighting of citation keys 53let g:rmd_syn_hl_citations = get(g:, 'rmd_syn_hl_citations', 1) 54 55let s:cpo_save = &cpo 56set cpo&vim 57 58" R chunks will not be highlighted by syntax/markdown because their headers 59" follow a non standard pattern: "```{lang" instead of "^```lang". 60" Make a copy of g:markdown_fenced_languages to highlight the chunks later: 61if exists('g:markdown_fenced_languages') 62 if !exists('g:rmd_fenced_languages') 63 let g:rmd_fenced_languages = deepcopy(g:markdown_fenced_languages) 64 let g:markdown_fenced_languages = [] 65 endif 66else 67 let g:rmd_fenced_languages = ['r'] 68endif 69 70runtime syntax/markdown.vim 71 72" Now highlight chunks: 73for s:type in g:rmd_fenced_languages 74 if s:type =~ '=' 75 let s:ft = substitute(s:type, '.*=', '', '') 76 let s:nm = substitute(s:type, '=.*', '', '') 77 else 78 let s:ft = s:type 79 let s:nm = s:type 80 endif 81 unlet! b:current_syntax 82 exe 'syn include @Rmd'.s:nm.' syntax/'.s:ft.'.vim' 83 if g:rmd_syn_hl_chunk 84 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=@Rmdr' 85 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 86 else 87 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 88 endif 89endfor 90unlet! s:type 91 92" Recognize inline R code 93syn region rmdrInline matchgroup=rmdInlineDelim start="`r " end="`" contains=@Rmdr keepend 94 95hi def link rmdInlineDelim Delimiter 96hi def link rmdCodeDelim Delimiter 97 98" You don't need this if either your markdown/syntax.vim already highlights 99" the YAML header or you are writing standard markdown 100if g:rmd_syn_hl_yaml 101 " Minimum highlighting of yaml header 102 syn match rmdYamlFieldTtl /^\s*\zs\w*\ze:/ contained 103 syn match rmdYamlFieldTtl /^\s*-\s*\zs\w*\ze:/ contained 104 syn region yamlFlowString matchgroup=yamlFlowStringDelimiter start='"' skip='\\"' end='"' contains=yamlEscape,rmdrInline contained 105 syn region yamlFlowString matchgroup=yamlFlowStringDelimiter start="'" skip="''" end="'" contains=yamlSingleEscape,rmdrInline contained 106 syn match yamlEscape contained '\\\%([\\"abefnrtv\^0_ NLP\n]\|x\x\x\|u\x\{4}\|U\x\{8}\)' 107 syn match yamlSingleEscape contained "''" 108 syn region pandocYAMLHeader matchgroup=rmdYamlBlockDelim start=/\%(\%^\|\_^\s*\n\)\@<=\_^-\{3}\ze\n.\+/ end=/^\([-.]\)\1\{2}$/ keepend contains=rmdYamlFieldTtl,yamlFlowString 109 hi def link rmdYamlBlockDelim Delimiter 110 hi def link rmdYamlFieldTtl Identifier 111 hi def link yamlFlowString String 112endif 113 114" You don't need this if either your markdown/syntax.vim already highlights 115" citations or you are writing standard markdown 116if g:rmd_syn_hl_citations 117 " From vim-pandoc-syntax 118 " parenthetical citations 119 syn match pandocPCite /\^\@<!\[[^\[\]]\{-}-\{0,1}@[[:alnum:]_][[:alnum:]à-öø-ÿÀ-ÖØ-ß_:.#$%&\-+?<>~\/]*.\{-}\]/ contains=pandocEmphasis,pandocStrong,pandocLatex,pandocCiteKey,@Spell,pandocAmpersandEscape display 120 " in-text citations with location 121 syn match pandocICite /@[[:alnum:]_][[:alnum:]à-öø-ÿÀ-ÖØ-ß_:.#$%&\-+?<>~\/]*\s\[.\{-1,}\]/ contains=pandocCiteKey,@Spell display 122 " cite keys 123 syn match pandocCiteKey /\(-\=@[[:alnum:]_][[:alnum:]à-öø-ÿÀ-ÖØ-ß_:.#$%&\-+?<>~\/]*\)/ containedin=pandocPCite,pandocICite contains=@NoSpell display 124 syn match pandocCiteAnchor /[-@]/ contained containedin=pandocCiteKey display 125 syn match pandocCiteLocator /[\[\]]/ contained containedin=pandocPCite,pandocICite 126 hi def link pandocPCite Operator 127 hi def link pandocICite Operator 128 hi def link pandocCiteKey Label 129 hi def link pandocCiteAnchor Operator 130 hi def link pandocCiteLocator Operator 131endif 132 133let b:current_syntax = "rmd" 134 135let &cpo = s:cpo_save 136unlet s:cpo_save 137 138" vim: ts=8 sw=2 139