robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1" Language: Markdown with chunks of R, Python and other languages 2" Maintainer: This runtime file is looking for a new maintainer. 3" Former Maintainer: Jakson Alves de Aquino <jalvesaq@gmail.com> 4" Former Repository: https://github.com/jalvesaq/R-Vim-runtime 5" Last Change: 2023 Dec 24 07:21AM 6" 2024 Feb 19 by Vim project (announce adoption) 7" 2026 Sep 29 by Vim project (fix typo in code block delimiter pattern) 8" 9" For highlighting pandoc extensions to markdown like citations and TeX and 10" many other advanced features like folding of markdown sections, it is 11" recommended to install the vim-pandoc filetype plugin as well as the 12" vim-pandoc-syntax filetype plugin from https://github.com/vim-pandoc. 13 14 15if exists ( "b:current_syntax" ) 16finish 17endif 18 19let s: cpo_save = & cpo 20set cpo & vim 21 22let g: rmd_include_latex = get ( g:, 'rmd_include_latex' , 1 ) 23if g: rmd_include_latex == 0 || g: rmd_include_latex == 1 24let b: rmd_has_LaTeX = v: false 25elseif g: rmd_include_latex == 2 26let b: rmd_has_LaTeX = v: true 27endif 28 29" Highlight the header of the chunks as R code 30let g: rmd_syn_hl_chunk = get ( g:, 'rmd_syn_hl_chunk' , 0 ) 31 32" Pandoc-syntax has more features, but it is slower. 33" https://github.com/vim-pandoc/vim-pandoc-syntax 34 35" Don't waste time loading syntax that will be discarded: 36let s: save_pandoc_lngs = get ( g:, 'pandoc#syntax#codeblocks#embeds#langs' , []) 37let g: pandoc#syntax#codeblocks#embeds#langs = [] 38 39let g: rmd_dynamic_fenced_languages = get ( g:, 'rmd_dynamic_fenced_languages' , v: true ) 40 41" Step_1: Source pandoc.vim if it is installed: 42runtime syntax/pandoc.vim 43if exists ( "b:current_syntax" ) 44if hlexists ( 'pandocDelimitedCodeBlock' ) 45syn clear pandocDelimitedCodeBlock 46endif 47 48if len ( s: save_pandoc_lngs ) > 0 && ! exists ( 'g:rmd_fenced_languages' ) 49let g: rmd_fenced_languages = deepcopy ( s: save_pandoc_lngs ) 50endif 51 52" Recognize inline R code 53syn region rmdrInline matchgroup = rmdInlineDelim start = "`r "end = "` "contains = @Rmdr containedin = pandocLaTeXRegion , yamlFlowString keepend 54else 55" Step_2: Source markdown.vim if pandoc.vim is not installed 56 57" Configuration if not using pandoc syntax: 58" Add syntax highlighting of YAML header 59let g: rmd_syn_hl_yaml = get ( g:, 'rmd_syn_hl_yaml' , 1 ) 60" Add syntax highlighting of citation keys 61let g: rmd_syn_hl_citations = get ( g:, 'rmd_syn_hl_citations' , 1 ) 62 63" R chunks will not be highlighted by syntax/markdown because their headers 64" follow a non standard pattern: "```{lang" instead of "^```lang". 65" Make a copy of g:markdown_fenced_languages to highlight the chunks later: 66if exists ( 'g:markdown_fenced_languages' ) && ! exists ( 'g:rmd_fenced_languages' ) 67let g: rmd_fenced_languages = deepcopy ( g: markdown_fenced_languages ) 68endif 69 70if exists ( 'g:markdown_fenced_languages' ) && len ( g: markdown_fenced_languages ) > 0 71let s: save_mfl = deepcopy ( g: markdown_fenced_languages ) 72endif 73" Don't waste time loading syntax that will be discarded: 74let g: markdown_fenced_languages = [] 75runtime syntax/markdown.vim 76if exists ( 's:save_mfl' ) > 0 77let g: markdown_fenced_languages = deepcopy ( s: save_mfl ) 78unlet s: save_mfl 79endif 80syn region rmdrInline matchgroup = rmdInlineDelim start = "`r "end = "` "contains = @Rmdr keepend 81 82" Step_2a: Add highlighting for both YAML and citations which are pandoc 83" specific, but also used in Rmd files 84 85" You don't need this if either your markdown/syntax.vim already highlights 86" the YAML header or you are writing standard markdown 87if g: rmd_syn_hl_yaml 88" Basic highlighting of YAML header 89syn match rmdYamlFieldTtl /^\s * \zs\w\%(-\|\w\) * \ze: /contained 90syn match rmdYamlFieldTtl /^\s * -\s * \zs\w\%(-\|\w\) * \ze: /contained 91syn region yamlFlowString matchgroup = yamlFlowStringDelimiter start = '" 'skip = '\\" 'end = '" 'contains = yamlEscape , rmdrInline contained 92syn region yamlFlowString matchgroup = yamlFlowStringDelimiter start = "' "skip = "'' "end = "' "contains = yamlSingleEscape , rmdrInline contained 93syn match yamlEscape contained '\\\%( [ \\"abefnrtv\^0_ NLP\n ] \|x\x\x\|u\x\{4}\|U\x\{8}\) ' 94syn match yamlSingleEscape contained "'' " 95syn match yamlComment /#. * /contained 96" A second colon is a syntax error, unless within a string or following !expr 97syn match yamlColonError /:\s * [ ^'^"^! ] * : /contained 98if & filetype == 'quarto' 99syn region pandocYAMLHeader matchgroup = rmdYamlBlockDelim start = /\%(\%^\|\_^\s * \n\)\@<=\_^-\{3}\ze\n.\+ /end = /^---$ /keepend contains = rmdYamlFieldTtl , yamlFlowString , yamlComment , yamlColonError 100else 101syn region pandocYAMLHeader matchgroup = rmdYamlBlockDelim start = /\%(\%^\|\_^\s * \n\)\@<=\_^-\{3}\ze\n.\+ /end = /^\( [ -. ] \)\1\{2}$ /keepend contains = rmdYamlFieldTtl , yamlFlowString , yamlComment , yamlColonError 102endif 103hi def link rmdYamlBlockDelim Delimiter 104hi def link rmdYamlFieldTtl Identifier 105hi def link yamlFlowString String 106hi def link yamlComment Comment 107hi def link yamlColonError Error 108endif 109 110" Conceal char for manual line break 111if & encoding ==# 'utf-8' 112syn match rmdNewLine '$ 'conceal cchar = ↵ 113endif 114 115" You don't need this if either your markdown/syntax.vim already highlights 116" citations or you are writing standard markdown 117if g: rmd_syn_hl_citations 118" From vim-pandoc-syntax 119" parenthetical citations 120syn match pandocPCite /\^\@<!\[ [ ^\[\] ] \{-}-\{0,1}@ [ [:alnum: ] _] [ [:alnum: ] à-öø-ÿÀ-ÖØ-ß_:.#$%&\-+?<>~\/] * .\{-}\] /contains = pandocEmphasis , pandocStrong , pandocLatex , pandocCiteKey , @Spell , pandocAmpersandEscape display 121" in-text citations with location 122syn match pandocICite /@ [ [:alnum: ] _] [ [:alnum: ] à-öø-ÿÀ-ÖØ-ß_:.#$%&\-+?<>~\/] * \s\[.\{-1,}\] /contains = pandocCiteKey , @Spell display 123" cite keys 124syn match pandocCiteKey /\(-\=@ [ [:alnum: ] _] [ [:alnum: ] à-öø-ÿÀ-ÖØ-ß_:.#$%&\-+?<>~\/] * \) /containedin = pandocPCite , pandocICite contains = @NoSpell display 125syn match pandocCiteAnchor /[ -@ ] /contained containedin = pandocCiteKey display 126syn match pandocCiteLocator /[ \[\] ] /contained containedin = pandocPCite , pandocICite 127hi def link pandocPCite Operator 128hi def link pandocICite Operator 129hi def link pandocCiteKey Label 130hi def link pandocCiteAnchor Operator 131hi def link pandocCiteLocator Operator 132endif 133endif 134 135" Step_3: Highlight code blocks. 136 137syn region rmdCodeBlock matchgroup = rmdCodeDelim start = "^\s * ```\s * {. * }$ "matchgroup = rmdCodeDelim end = "^\s * ```\ze\s * $ "keepend 138syn region rmdCodeBlock matchgroup = rmdCodeDelim start = "^\s * ```.\+$ "matchgroup = rmdCodeDelim end = "^\s * ```\ze\s * $ "keepend 139hi link rmdCodeBlock Special 140 141" Now highlight chunks: 142syn region knitrBodyOptions start = '^#| 'end = '$ 'contained containedin = rComment , pythonComment contains = knitrBodyVar , knitrBodyValue transparent 143syn match knitrBodyValue ': \zs. * \ze$ 'keepend contained containedin = knitrBodyOptions 144syn match knitrBodyVar '| \zs\S\{-}\ze: 'contained containedin = knitrBodyOptions 145 146let g: rmd_fenced_languages = get ( g:, 'rmd_fenced_languages' , [ 'r' ]) 147 148let s: no_syntax_vim = [] 149function s: IncludeLanguage ( lng ) 150if a: lng =~ '=' 151let ftpy = substitute ( a: lng , '.*=' , '' , '' ) 152let lnm = substitute ( a: lng , '=.*' , '' , '' ) 153else 154let ftpy = a: lng 155let lnm = a: lng 156endif 157if index ( s: no_syntax_vim , ftpy ) >= 0 158return 159endif 160if len ( globpath ( & rtp , "syntax/" . ftpy . ".vim" )) 161unlet ! b: current_syntax 162exe 'syn include @Rmd' . lnm . ' syntax/' . ftpy . '.vim' 163let b: current_syntax = "rmd" 164if g: rmd_syn_hl_chunk 165exe 'syn match knitrChunkDelim /```\s*{\s*' . lnm . '/ contained containedin=knitrChunkBrace contains=knitrChunkLabel' 166exe 'syn match knitrChunkLabelDelim /```\s*{\s*' . lnm . ',\=\s*[-[:alnum:]]\{-1,}[,}]/ contained containedin=knitrChunkBrace' 167syn match knitrChunkDelim /}\s * $ /contained containedin = knitrChunkBrace 168exe 'syn match knitrChunkBrace /```\s*{\s*' . lnm . '.*$/ contained containedin=rmd' . lnm . 'Chunk contains=knitrChunkDelim,knitrChunkLabelDelim,@Rmd' . lnm 169exe 'syn region rmd' . lnm . 'Chunk start="^\s*```\s*{\s*=\?' . lnm . '\>.*$" matchgroup=rmdCodeDelim end="^\s*```\ze\s*$" keepend contains=knitrChunkBrace,@Rmd' . lnm 170 171hi link knitrChunkLabel Identifier 172hi link knitrChunkDelim rmdCodeDelim 173hi link knitrChunkLabelDelim rmdCodeDelim 174else 175exe 'syn region rmd' . lnm . 'Chunk matchgroup=rmdCodeDelim start="^\s*```\s*{\s*=\?' . lnm . '\>.*$" matchgroup=rmdCodeDelim end="^\s*```\ze\s*$" keepend contains=@Rmd' . lnm 176endif 177else 178" Avoid the cost of running globpath() whenever the buffer is saved 179let s: no_syntax_vim += [ ftpy ] 180endif 181endfunction 182 183for s: type in g: rmd_fenced_languages 184call s: IncludeLanguage ( s: type ) 185endfor 186unlet ! s: type 187 188let s: LaTeX_included = v: false 189function s: IncludeLaTeX () 190let s: LaTeX_included = v: true 191unlet ! b: current_syntax 192syn include @RmdLaTeX syntax/tex.vim 193" From vim-pandoc-syntax 194syn region rmdLaTeXInlineMath start = /\v\\@<!\$\S@= /end = /\v\\@<!\$\d@! /keepend contains = @RmdLaTeX 195syn match rmdLaTeXCmd /\\ [ [:alpha: ] ]\+\(\({.\{-}}\)\=\(\[.\{-}\]\)\=\) * /contains = @RmdLaTeX 196syn region rmdLaTeX start = '\$\$ 'end = '\$\$ 'keepend contains = @RmdLaTeX 197syn region rmdLaTeX start = /\\begin{\z(.\{-}\)} /end = /\\end{\z1} /keepend contains = @RmdLaTeX 198endfunction 199 200function s: CheckRmdFencedLanguages () 201let alines = getline ( 1 , '$' ) 202call filter ( alines , "v:val =~ '^```{'" ) 203call map ( alines , "substitute(v:val, '^```{', '', '')" ) 204call map ( alines , "substitute(v:val, '\\W.*', '', '')" ) 205for tpy in alines 206if len ( tpy ) == 0 207continue 208endif 209let has_lng = 0 210for lng in g: rmd_fenced_languages 211if tpy == lng 212let has_lng = 1 213continue 214endif 215endfor 216if has_lng == 0 217let g: rmd_fenced_languages += [ tpy ] 218call s: IncludeLanguage ( tpy ) 219endif 220endfor 221 222if hlexists ( 'pandocLaTeXCommand' ) 223return 224endif 225if g: rmd_include_latex 226if ! b: rmd_has_LaTeX && ( search ( '\$\$' , 'wn' ) > 0 || 227 \search ( '\\begin{' , 'wn' ) > 0 ) || 228 \search ( '\\[[:alpha:]]\+' , 'wn' ) || 229 \search ( '\$[^\$]\+\$' , 'wn' ) 230let b: rmd_has_LaTeX = v: true 231endif 232if b: rmd_has_LaTeX && ! s: LaTeX_included 233call s: IncludeLaTeX () 234endif 235endif 236endfunction 237 238if g: rmd_dynamic_fenced_languages 239call s: CheckRmdFencedLanguages () 240augroup RmdSyntax 241autocmd ! 242autocmd BufWritePost <buffer> call s: CheckRmdFencedLanguages () 243augroup END 244endif 245 246" Step_4: Highlight code recognized by pandoc but not defined in pandoc.vim yet: 247syn match pandocDivBegin '^:::\+ {.\{-}} 'contains = pandocHeaderAttr 248syn match pandocDivEnd '^:::\+$ ' 249 250hi def link knitrBodyVar PreProc 251hi def link knitrBodyValue Constant 252hi def link knitrBodyOptions rComment 253hi def link pandocDivBegin Delimiter 254hi def link pandocDivEnd Delimiter 255hi def link rmdInlineDelim Delimiter 256hi def link rmdCodeDelim Delimiter 257 258if len ( s: save_pandoc_lngs ) 259let g: pandoc#syntax#codeblocks#embeds#langs = s: save_pandoc_lngs 260endif 261unlet s: save_pandoc_lngs 262let & cpo = s: cpo_save 263unlet s: cpo_save 264 265syntax iskeyword clear 266 267let b: current_syntax = "rmd" 268 269" vim: ts=8 sw=2