blob: cccd4110f560aabcfb400c309ef1973af7151202 [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 Moolenaar11e3c5b2021-04-21 18:09:37 +02004" Last Change: Wed Apr 21, 2021 09:55AM
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 Moolenaar11e3c5b2021-04-21 18:09:37 +020016" Highlight the header of the chunks as R code
17let g:rmd_syn_hl_chunk = get(g:, 'rmd_syn_hl_chunk', 0)
Bram Moolenaarfc65cab2018-08-28 22:58:02 +020018
19" Pandoc-syntax has more features, but it is slower.
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +020020" https://github.com/vim-pandoc/vim-pandoc-syntax
Bram Moolenaarfc65cab2018-08-28 22:58:02 +020021let g:pandoc#syntax#codeblocks#embeds#langs = get(g:, 'pandoc#syntax#codeblocks#embeds#langs', ['r'])
Bram Moolenaardb6ea062014-07-10 22:01:47 +020022runtime syntax/pandoc.vim
23if exists("b:current_syntax")
Bram Moolenaar11e3c5b2021-04-21 18:09:37 +020024 " Recognize inline R code
Bram Moolenaarfc65cab2018-08-28 22:58:02 +020025 syn region rmdrInline matchgroup=rmdInlineDelim start="`r " end="`" contains=@R containedin=pandocLaTeXRegion,yamlFlowString keepend
26 hi def link rmdInlineDelim Delimiter
Bram Moolenaar11e3c5b2021-04-21 18:09:37 +020027
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
Bram Moolenaarfc65cab2018-08-28 22:58:02 +020045 let b:current_syntax = "rmd"
46 finish
Bram Moolenaardb6ea062014-07-10 22:01:47 +020047endif
48
Bram Moolenaar11e3c5b2021-04-21 18:09:37 +020049" 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
Bram Moolenaarfc65cab2018-08-28 22:58:02 +020055let 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 = []
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +020065 endif
Bram Moolenaarfc65cab2018-08-28 22:58:02 +020066else
67 let g:rmd_fenced_languages = ['r']
Bram Moolenaardb6ea062014-07-10 22:01:47 +020068endif
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +020069
Bram Moolenaarfc65cab2018-08-28 22:58:02 +020070runtime syntax/markdown.vim
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +020071
Bram Moolenaarfc65cab2018-08-28 22:58:02 +020072" Now highlight chunks:
73for s:type in g:rmd_fenced_languages
74 if s:type =~ '='
Bram Moolenaara6c27c42019-05-09 19:16:22 +020075 let s:ft = substitute(s:type, '.*=', '', '')
76 let s:nm = substitute(s:type, '=.*', '', '')
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +020077 else
Bram Moolenaara6c27c42019-05-09 19:16:22 +020078 let s:ft = s:type
Bram Moolenaarfc65cab2018-08-28 22:58:02 +020079 let s:nm = s:type
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +020080 endif
Bram Moolenaarfc65cab2018-08-28 22:58:02 +020081 unlet! b:current_syntax
Bram Moolenaara6c27c42019-05-09 19:16:22 +020082 exe 'syn include @Rmd'.s:nm.' syntax/'.s:ft.'.vim'
Bram Moolenaarfc65cab2018-08-28 22:58:02 +020083 if g:rmd_syn_hl_chunk
Bram Moolenaar11e3c5b2021-04-21 18:09:37 +020084 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'
Bram Moolenaarfc65cab2018-08-28 22:58:02 +020085 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
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +020089endfor
Bram Moolenaarfc65cab2018-08-28 22:58:02 +020090unlet! s:type
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +020091
Bram Moolenaar11e3c5b2021-04-21 18:09:37 +020092" Recognize inline R code
93syn region rmdrInline matchgroup=rmdInlineDelim start="`r " end="`" contains=@Rmdr keepend
94
Bram Moolenaarfc65cab2018-08-28 22:58:02 +020095hi def link rmdInlineDelim Delimiter
96hi def link rmdCodeDelim Delimiter
Bram Moolenaardb6ea062014-07-10 22:01:47 +020097
Bram Moolenaarfc65cab2018-08-28 22:58:02 +020098" 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
Bram Moolenaardb6ea062014-07-10 22:01:47 +0200112endif
113
Bram Moolenaarfc65cab2018-08-28 22:58:02 +0200114" 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
Bram Moolenaardb6ea062014-07-10 22:01:47 +0200132
133let b:current_syntax = "rmd"
134
Bram Moolenaarfc65cab2018-08-28 22:58:02 +0200135let &cpo = s:cpo_save
136unlet s:cpo_save
137
Bram Moolenaardb6ea062014-07-10 22:01:47 +0200138" vim: ts=8 sw=2