rmd.vim 6.1 KB

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