summaryrefslogtreecommitdiff
path: root/runtime/syntax/rmd.vim
blob: 05435354ad05f773ee1957cea7889b21bc49de62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
" markdown Text with R statements
" Language: markdown with R code chunks
" Homepage: https://github.com/jalvesaq/R-Vim-runtime
" Last Change: Sat Jan 28, 2017  10:06PM
"
" CONFIGURATION:
"   To highlight chunk headers as R code, put in your vimrc (e.g. .config/nvim/init.vim):
"   let rmd_syn_hl_chunk = 1
"
"   For highlighting pandoc extensions to markdown like citations and TeX and
"   many other advanced features like folding of markdown sections, it is
"   recommended to install the vim-pandoc filetype plugin as well as the
"   vim-pandoc-syntax filetype plugin from https://github.com/vim-pandoc.
"
" TODO:
"   - Provide highlighting for rmarkdown parameters in yaml header

if exists("b:current_syntax")
  finish
endif

" load all of pandoc info, e.g. from
" https://github.com/vim-pandoc/vim-pandoc-syntax
runtime syntax/pandoc.vim
if exists("b:current_syntax")
  let rmdIsPandoc = 1
  unlet b:current_syntax
else
  let rmdIsPandoc = 0
  runtime syntax/markdown.vim
  if exists("b:current_syntax")
    unlet b:current_syntax
  endif

  " load all of the yaml syntax highlighting rules into @yaml
  syntax include @yaml syntax/yaml.vim
  if exists("b:current_syntax")
    unlet b:current_syntax
  endif

  " highlight yaml block commonly used for front matter
  syntax region rmdYamlBlock matchgroup=rmdYamlBlockDelim start="^---" matchgroup=rmdYamlBlockDelim end="^---" contains=@yaml keepend fold
endif

if !exists("g:rmd_syn_langs")
  let g:rmd_syn_langs = ["r"]
else
  let s:hasr = 0
  for s:lng in g:rmd_syn_langs
    if s:lng == "r"
      let s:hasr = 1
    endif
  endfor
  if s:hasr == 0
    let g:rmd_syn_langs += ["r"]
  endif
endif

for s:lng in g:rmd_syn_langs
  exe 'syntax include @' . toupper(s:lng) . ' syntax/'. s:lng . '.vim'
  if exists("b:current_syntax")
    unlet b:current_syntax
  endif
  exe 'syntax region rmd' . toupper(s:lng) . 'Chunk start="^[ \t]*``` *{\(' . s:lng . '\|r.*engine\s*=\s*["' . "']" . s:lng . "['" . '"]\).*}$" end="^[ \t]*```$" contains=@' . toupper(s:lng) . ',rmd' . toupper(s:lng) . 'ChunkDelim keepend fold'

  if exists("g:rmd_syn_hl_chunk") && s:lng == "r"
    " highlight R code inside chunk header
    syntax match rmdRChunkDelim "^[ \t]*```{r" contained
    syntax match rmdRChunkDelim "}$" contained
  else
    exe 'syntax match rmd' . toupper(s:lng) . 'ChunkDelim "^[ \t]*```{\(' . s:lng . '\|r.*engine\s*=\s*["' . "']" . s:lng . "['" . '"]\).*}$" contained'
  endif
  exe 'syntax match rmd' . toupper(s:lng) . 'ChunkDelim "^[ \t]*```$" contained'
endfor


" also match and syntax highlight in-line R code
syntax region rmdrInline matchgroup=rmdInlineDelim start="`r "  end="`" contains=@R containedin=pandocLaTeXRegion,yamlFlowString keepend
" I was not able to highlight rmdrInline inside a pandocLaTeXCommand, although
" highlighting works within pandocLaTeXRegion and yamlFlowString. 
syntax cluster texMathZoneGroup add=rmdrInline

" match slidify special marker
syntax match rmdSlidifySpecial "\*\*\*"


if rmdIsPandoc == 0
  syn match rmdBlockQuote /^\s*>.*\n\(.*\n\@<!\n\)*/ skipnl
  " LaTeX
  syntax include @LaTeX syntax/tex.vim
  if exists("b:current_syntax")
    unlet b:current_syntax
  endif
  " Inline
  syntax match rmdLaTeXInlDelim "\$"
  syntax match rmdLaTeXInlDelim "\\\$"
  syn region texMathZoneX	matchgroup=Delimiter start="\$" skip="\\\\\|\\\$"	matchgroup=Delimiter end="\$" end="%stopzone\>"	contains=@texMathZoneGroup
  " Region
  syntax match rmdLaTeXRegDelim "\$\$" contained
  syntax match rmdLaTeXRegDelim "\$\$latex$" contained
  syntax match rmdLaTeXSt "\\[a-zA-Z]\+"
  syntax region rmdLaTeXRegion start="^\$\$" skip="\\\$" end="\$\$$" contains=@LaTeX,rmdLaTeXRegDelim keepend
  syntax region rmdLaTeXRegion2 start="^\\\[" end="\\\]" contains=@LaTeX,rmdLaTeXRegDelim keepend
  hi def link rmdBlockQuote Comment
  hi def link rmdLaTeXSt Statement
  hi def link rmdLaTeXInlDelim Special
  hi def link rmdLaTeXRegDelim Special
endif

for s:lng in g:rmd_syn_langs
  exe 'syn sync match rmd' . toupper(s:lng) . 'SyncChunk grouphere rmd' . toupper(s:lng) . 'Chunk /^[ \t]*``` *{\(' . s:lng . '\|r.*engine\s*=\s*["' . "']" . s:lng . "['" . '"]\)/'
endfor

hi def link rmdYamlBlockDelim Delim
for s:lng in g:rmd_syn_langs
  exe 'hi def link rmd' . toupper(s:lng) . 'ChunkDelim Special'
endfor
hi def link rmdInlineDelim Special
hi def link rmdSlidifySpecial Special

let b:current_syntax = "rmd"

" vim: ts=8 sw=2