blob: a26389024d17bfe25eea4e6f158666e313cc0062 [file] [log] [blame]
Bram Moolenaardb6ea062014-07-10 22:01:47 +02001" markdown Text with R statements
2" Language: markdown with R code chunks
Bram Moolenaar77cdfd12016-03-12 12:57:59 +01003" Homepage: https://github.com/jalvesaq/R-Vim-runtime
Bram Moolenaarfc65cab2018-08-28 22:58:02 +02004" Last Change: Sat Aug 25, 2018 03:44PM
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +02005"
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.
Bram Moolenaarfc65cab2018-08-28 22:58:02 +020010
Bram Moolenaardb6ea062014-07-10 22:01:47 +020011
Bram Moolenaar77cdfd12016-03-12 12:57:59 +010012if exists("b:current_syntax")
Bram Moolenaardb6ea062014-07-10 22:01:47 +020013 finish
14endif
15
Bram Moolenaarfc65cab2018-08-28 22:58:02 +020016" 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.
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +020025" https://github.com/vim-pandoc/vim-pandoc-syntax
Bram Moolenaarfc65cab2018-08-28 22:58:02 +020026let g:pandoc#syntax#codeblocks#embeds#langs = get(g:, 'pandoc#syntax#codeblocks#embeds#langs', ['r'])
Bram Moolenaardb6ea062014-07-10 22:01:47 +020027runtime syntax/pandoc.vim
28if exists("b:current_syntax")
Bram Moolenaarfc65cab2018-08-28 22:58:02 +020029 " 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
Bram Moolenaardb6ea062014-07-10 22:01:47 +020035endif
36
Bram Moolenaarfc65cab2018-08-28 22:58:02 +020037let 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 = []
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +020047 endif
Bram Moolenaarfc65cab2018-08-28 22:58:02 +020048else
49 let g:rmd_fenced_languages = ['r']
Bram Moolenaardb6ea062014-07-10 22:01:47 +020050endif
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +020051
Bram Moolenaarfc65cab2018-08-28 22:58:02 +020052runtime syntax/markdown.vim
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +020053
Bram Moolenaarfc65cab2018-08-28 22:58:02 +020054" 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, '.*=', '')
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +020059 else
Bram Moolenaarfc65cab2018-08-28 22:58:02 +020060 let s:lng = s:type
61 let s:nm = s:type
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +020062 endif
Bram Moolenaarfc65cab2018-08-28 22:58:02 +020063 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'
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +020072endfor
Bram Moolenaarfc65cab2018-08-28 22:58:02 +020073unlet! s:type
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +020074
Bram Moolenaarfc65cab2018-08-28 22:58:02 +020075hi def link rmdInlineDelim Delimiter
76hi def link rmdCodeDelim Delimiter
Bram Moolenaardb6ea062014-07-10 22:01:47 +020077
Bram Moolenaarfc65cab2018-08-28 22:58:02 +020078" 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
Bram Moolenaardb6ea062014-07-10 22:01:47 +020092endif
93
Bram Moolenaarfc65cab2018-08-28 22:58:02 +020094" 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
Bram Moolenaardb6ea062014-07-10 22:01:47 +0200112
113let b:current_syntax = "rmd"
114
Bram Moolenaarfc65cab2018-08-28 22:58:02 +0200115let &cpo = s:cpo_save
116unlet s:cpo_save
117
Bram Moolenaardb6ea062014-07-10 22:01:47 +0200118" vim: ts=8 sw=2