diff options
author | Josh Rahm <joshuarahm@gmail.com> | 2023-11-29 22:39:54 +0000 |
---|---|---|
committer | Josh Rahm <joshuarahm@gmail.com> | 2023-11-29 22:39:54 +0000 |
commit | 21cb7d04c387e4198ca8098a884c78b56ffcf4c2 (patch) | |
tree | 84fe5690df1551f0bb2bdfe1a13aacd29ebc1de7 /runtime/syntax/rmd.vim | |
parent | d9c904f85a23a496df4eb6be42aa43f007b22d50 (diff) | |
parent | 4a8bf24ac690004aedf5540fa440e788459e5e34 (diff) | |
download | rneovim-colorcolchar.tar.gz rneovim-colorcolchar.tar.bz2 rneovim-colorcolchar.zip |
Merge remote-tracking branch 'upstream/master' into colorcolcharcolorcolchar
Diffstat (limited to 'runtime/syntax/rmd.vim')
-rw-r--r-- | runtime/syntax/rmd.vim | 278 |
1 files changed, 185 insertions, 93 deletions
diff --git a/runtime/syntax/rmd.vim b/runtime/syntax/rmd.vim index cccd4110f5..93343dd729 100644 --- a/runtime/syntax/rmd.vim +++ b/runtime/syntax/rmd.vim @@ -1,7 +1,7 @@ -" markdown Text with R statements -" Language: markdown with R code chunks +" Language: Markdown with chunks of R, Python and other languages +" Maintainer: Jakson Aquino <jalvesaq@gmail.com> " Homepage: https://github.com/jalvesaq/R-Vim-runtime -" Last Change: Wed Apr 21, 2021 09:55AM +" Last Change: Wed May 17, 2023 06:34AM " " For highlighting pandoc extensions to markdown like citations and TeX and " many other advanced features like folding of markdown sections, it is @@ -13,126 +13,218 @@ if exists("b:current_syntax") finish endif +let s:cpo_save = &cpo +set cpo&vim + " Highlight the header of the chunks as R code let g:rmd_syn_hl_chunk = get(g:, 'rmd_syn_hl_chunk', 0) " Pandoc-syntax has more features, but it is slower. " https://github.com/vim-pandoc/vim-pandoc-syntax -let g:pandoc#syntax#codeblocks#embeds#langs = get(g:, 'pandoc#syntax#codeblocks#embeds#langs', ['r']) + +" Don't waste time loading syntax that will be discarded: +let s:save_pandoc_lngs = get(g:, 'pandoc#syntax#codeblocks#embeds#langs', []) +let g:pandoc#syntax#codeblocks#embeds#langs = [] + +let g:rmd_dynamic_fenced_languages = get(g:, 'rmd_dynamic_fenced_languages', v:true) + +" Step_1: Source pandoc.vim if it is installed: runtime syntax/pandoc.vim if exists("b:current_syntax") + if hlexists('pandocDelimitedCodeBlock') + syn clear pandocDelimitedCodeBlock + endif + + if len(s:save_pandoc_lngs) > 0 && !exists('g:rmd_fenced_languages') + let g:rmd_fenced_languages = deepcopy(s:save_pandoc_lngs) + endif + " Recognize inline R code - syn region rmdrInline matchgroup=rmdInlineDelim start="`r " end="`" contains=@R containedin=pandocLaTeXRegion,yamlFlowString keepend - hi def link rmdInlineDelim Delimiter - - " Fix recognition of language chunks (code adapted from pandoc, 2021-03-28) - " Knitr requires braces in the block's header - for s:lng in g:pandoc#syntax#codeblocks#embeds#langs - let s:nm = matchstr(s:lng, '^[^=]*') - exe 'syn clear pandocDelimitedCodeBlock_'.s:nm - exe 'syn clear pandocDelimitedCodeBlockinBlockQuote_'.s:nm - if g:rmd_syn_hl_chunk - 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' - 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) + syn region rmdrInline matchgroup=rmdInlineDelim start="`r " end="`" contains=@Rmdr containedin=pandocLaTeXRegion,yamlFlowString keepend +else + " Step_2: Source markdown.vim if pandoc.vim is not installed + + " Configuration if not using pandoc syntax: + " Add syntax highlighting of YAML header + let g:rmd_syn_hl_yaml = get(g:, 'rmd_syn_hl_yaml', 1) + " Add syntax highlighting of citation keys + let g:rmd_syn_hl_citations = get(g:, 'rmd_syn_hl_citations', 1) + + " R chunks will not be highlighted by syntax/markdown because their headers + " follow a non standard pattern: "```{lang" instead of "^```lang". + " Make a copy of g:markdown_fenced_languages to highlight the chunks later: + if exists('g:markdown_fenced_languages') && !exists('g:rmd_fenced_languages') + let g:rmd_fenced_languages = deepcopy(g:markdown_fenced_languages) + endif + + if exists('g:markdown_fenced_languages') && len(g:markdown_fenced_languages) > 0 + let s:save_mfl = deepcopy(g:markdown_fenced_languages) + endif + " Don't waste time loading syntax that will be discarded: + let g:markdown_fenced_languages = [] + runtime syntax/markdown.vim + if exists('s:save_mfl') > 0 + let g:markdown_fenced_languages = deepcopy(s:save_mfl) + unlet s:save_mfl + endif + syn region rmdrInline matchgroup=rmdInlineDelim start="`r " end="`" contains=@Rmdr keepend + + " Step_2a: Add highlighting for both YAML and citations which are pandoc + " specific, but also used in Rmd files + + " You don't need this if either your markdown/syntax.vim already highlights + " the YAML header or you are writing standard markdown + if g:rmd_syn_hl_yaml + " Basic highlighting of YAML header + syn match rmdYamlFieldTtl /^\s*\zs\w\%(-\|\w\)*\ze:/ contained + syn match rmdYamlFieldTtl /^\s*-\s*\zs\w\%(-\|\w\)*\ze:/ contained + syn region yamlFlowString matchgroup=yamlFlowStringDelimiter start='"' skip='\\"' end='"' contains=yamlEscape,rmdrInline contained + syn region yamlFlowString matchgroup=yamlFlowStringDelimiter start="'" skip="''" end="'" contains=yamlSingleEscape,rmdrInline contained + syn match yamlEscape contained '\\\%([\\"abefnrtv\^0_ NLP\n]\|x\x\x\|u\x\{4}\|U\x\{8}\)' + syn match yamlSingleEscape contained "''" + syn match yamlComment /#.*/ contained + " A second colon is a syntax error, unless within a string or following !expr + syn match yamlColonError /:\s*[^'^"^!]*:/ contained + if &filetype == 'quarto' + syn region pandocYAMLHeader matchgroup=rmdYamlBlockDelim start=/\%(\%^\|\_^\s*\n\)\@<=\_^-\{3}\ze\n.\+/ end=/^---$/ keepend contains=rmdYamlFieldTtl,yamlFlowString,yamlComment,yamlColonError else - 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) + syn region pandocYAMLHeader matchgroup=rmdYamlBlockDelim start=/\%(\%^\|\_^\s*\n\)\@<=\_^-\{3}\ze\n.\+/ end=/^\([-.]\)\1\{2}$/ keepend contains=rmdYamlFieldTtl,yamlFlowString,yamlComment,yamlColonError endif - endfor - unlet s:lng - unlet s:nm - hi def link rmdInlineDelim Delimiter - hi def link rmdCodeDelim Delimiter - let b:current_syntax = "rmd" - finish -endif - -" Configuration if not using pandoc syntax: -" Add syntax highlighting of YAML header -let g:rmd_syn_hl_yaml = get(g:, 'rmd_syn_hl_yaml', 1) -" Add syntax highlighting of citation keys -let g:rmd_syn_hl_citations = get(g:, 'rmd_syn_hl_citations', 1) + hi def link rmdYamlBlockDelim Delimiter + hi def link rmdYamlFieldTtl Identifier + hi def link yamlFlowString String + hi def link yamlComment Comment + hi def link yamlColonError Error + endif -let s:cpo_save = &cpo -set cpo&vim + " Conceal char for manual line break + if &encoding ==# 'utf-8' + syn match rmdNewLine ' $' conceal cchar=↵ + endif -" R chunks will not be highlighted by syntax/markdown because their headers -" follow a non standard pattern: "```{lang" instead of "^```lang". -" Make a copy of g:markdown_fenced_languages to highlight the chunks later: -if exists('g:markdown_fenced_languages') - if !exists('g:rmd_fenced_languages') - let g:rmd_fenced_languages = deepcopy(g:markdown_fenced_languages) - let g:markdown_fenced_languages = [] + " You don't need this if either your markdown/syntax.vim already highlights + " citations or you are writing standard markdown + if g:rmd_syn_hl_citations + " From vim-pandoc-syntax + " parenthetical citations + syn match pandocPCite /\^\@<!\[[^\[\]]\{-}-\{0,1}@[[:alnum:]_][[:alnum:]à-öø-ÿÀ-ÖØ-ß_:.#$%&\-+?<>~\/]*.\{-}\]/ contains=pandocEmphasis,pandocStrong,pandocLatex,pandocCiteKey,@Spell,pandocAmpersandEscape display + " in-text citations with location + syn match pandocICite /@[[:alnum:]_][[:alnum:]à-öø-ÿÀ-ÖØ-ß_:.#$%&\-+?<>~\/]*\s\[.\{-1,}\]/ contains=pandocCiteKey,@Spell display + " cite keys + syn match pandocCiteKey /\(-\=@[[:alnum:]_][[:alnum:]à-öø-ÿÀ-ÖØ-ß_:.#$%&\-+?<>~\/]*\)/ containedin=pandocPCite,pandocICite contains=@NoSpell display + syn match pandocCiteAnchor /[-@]/ contained containedin=pandocCiteKey display + syn match pandocCiteLocator /[\[\]]/ contained containedin=pandocPCite,pandocICite + hi def link pandocPCite Operator + hi def link pandocICite Operator + hi def link pandocCiteKey Label + hi def link pandocCiteAnchor Operator + hi def link pandocCiteLocator Operator endif -else - let g:rmd_fenced_languages = ['r'] endif -runtime syntax/markdown.vim +" Step_3: Highlight code blocks. + +syn region rmdCodeBlock matchgroup=rmdCodeDelim start="^\s*```\s*{.*}$" matchgroup=rmdCodeDelim end="^\s*```\ze\s*$" keepend +syn region rmdCodeBlock matchgroup=rmdCodeDelim start="^\s*```.+$" matchgroup=rmdCodeDelim end="^```$" keepend +hi link rmdCodeBlock Special " Now highlight chunks: -for s:type in g:rmd_fenced_languages - if s:type =~ '=' - let s:ft = substitute(s:type, '.*=', '', '') - let s:nm = substitute(s:type, '=.*', '', '') +syn region knitrBodyOptions start='^#| ' end='$' contained containedin=rComment,pythonComment contains=knitrBodyVar,knitrBodyValue transparent +syn match knitrBodyValue ': \zs.*\ze$' keepend contained containedin=knitrBodyOptions +syn match knitrBodyVar '| \zs\S\{-}\ze:' contained containedin=knitrBodyOptions + +let g:rmd_fenced_languages = get(g:, 'rmd_fenced_languages', ['r']) + +let s:no_syntax_vim = [] +function IncludeLanguage(lng) + if a:lng =~ '=' + let ftpy = substitute(a:lng, '.*=', '', '') + let lnm = substitute(a:lng, '=.*', '', '') else - let s:ft = s:type - let s:nm = s:type + let ftpy = a:lng + let lnm = a:lng endif - unlet! b:current_syntax - exe 'syn include @Rmd'.s:nm.' syntax/'.s:ft.'.vim' - if g:rmd_syn_hl_chunk - 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' - 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 + if index(s:no_syntax_vim, ftpy) >= 0 + return + endif + if len(globpath(&rtp, "syntax/" . ftpy . ".vim")) + unlet! b:current_syntax + exe 'syn include @Rmd'.lnm.' syntax/'.ftpy.'.vim' + let b:current_syntax = "rmd" + if g:rmd_syn_hl_chunk + exe 'syn match knitrChunkDelim /```\s*{\s*'.lnm.'/ contained containedin=knitrChunkBrace contains=knitrChunkLabel' + exe 'syn match knitrChunkLabelDelim /```\s*{\s*'.lnm.',\=\s*[-[:alnum:]]\{-1,}[,}]/ contained containedin=knitrChunkBrace' + syn match knitrChunkDelim /}\s*$/ contained containedin=knitrChunkBrace + exe 'syn match knitrChunkBrace /```\s*{\s*'.lnm.'.*$/ contained containedin=rmd'.lnm.'Chunk contains=knitrChunkDelim,knitrChunkLabelDelim,@Rmd'.lnm + exe 'syn region rmd'.lnm.'Chunk start="^\s*```\s*{\s*=\?'.lnm.'\>.*$" matchgroup=rmdCodeDelim end="^\s*```\ze\s*$" keepend contains=knitrChunkBrace,@Rmd'.lnm + + hi link knitrChunkLabel Identifier + hi link knitrChunkDelim rmdCodeDelim + hi link knitrChunkLabelDelim rmdCodeDelim + else + exe 'syn region rmd'.lnm.'Chunk matchgroup=rmdCodeDelim start="^\s*```\s*{\s*=\?'.lnm.'\>.*$" matchgroup=rmdCodeDelim end="^\s*```\ze\s*$" keepend contains=@Rmd'.lnm + endif else - 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 + " Avoid the cost of running globpath() whenever the buffer is saved + let s:no_syntax_vim += [ftpy] endif +endfunction + +for s:type in g:rmd_fenced_languages + call IncludeLanguage(s:type) endfor unlet! s:type -" Recognize inline R code -syn region rmdrInline matchgroup=rmdInlineDelim start="`r " end="`" contains=@Rmdr keepend +function CheckRmdFencedLanguages() + let alines = getline(1, '$') + call filter(alines, "v:val =~ '^```{'") + call map(alines, "substitute(v:val, '^```{', '', '')") + call map(alines, "substitute(v:val, '\\W.*', '', '')") + for tpy in alines + if len(tpy) == 0 + continue + endif + let has_lng = 0 + for lng in g:rmd_fenced_languages + if tpy == lng + let has_lng = 1 + continue + endif + endfor + if has_lng == 0 + let g:rmd_fenced_languages += [tpy] + call IncludeLanguage(tpy) + endif + endfor +endfunction + +if g:rmd_dynamic_fenced_languages + call CheckRmdFencedLanguages() + augroup RmdSyntax + autocmd! + autocmd BufWritePost <buffer> call CheckRmdFencedLanguages() + augroup END +endif + +" Step_4: Highlight code recognized by pandoc but not defined in pandoc.vim yet: +syn match pandocDivBegin '^:::\+ {.\{-}}' contains=pandocHeaderAttr +syn match pandocDivEnd '^:::\+$' +hi def link knitrBodyVar PreProc +hi def link knitrBodyValue Constant +hi def link knitrBodyOptions rComment +hi def link pandocDivBegin Delimiter +hi def link pandocDivEnd Delimiter hi def link rmdInlineDelim Delimiter hi def link rmdCodeDelim Delimiter -" You don't need this if either your markdown/syntax.vim already highlights -" the YAML header or you are writing standard markdown -if g:rmd_syn_hl_yaml - " Minimum highlighting of yaml header - syn match rmdYamlFieldTtl /^\s*\zs\w*\ze:/ contained - syn match rmdYamlFieldTtl /^\s*-\s*\zs\w*\ze:/ contained - syn region yamlFlowString matchgroup=yamlFlowStringDelimiter start='"' skip='\\"' end='"' contains=yamlEscape,rmdrInline contained - syn region yamlFlowString matchgroup=yamlFlowStringDelimiter start="'" skip="''" end="'" contains=yamlSingleEscape,rmdrInline contained - syn match yamlEscape contained '\\\%([\\"abefnrtv\^0_ NLP\n]\|x\x\x\|u\x\{4}\|U\x\{8}\)' - syn match yamlSingleEscape contained "''" - syn region pandocYAMLHeader matchgroup=rmdYamlBlockDelim start=/\%(\%^\|\_^\s*\n\)\@<=\_^-\{3}\ze\n.\+/ end=/^\([-.]\)\1\{2}$/ keepend contains=rmdYamlFieldTtl,yamlFlowString - hi def link rmdYamlBlockDelim Delimiter - hi def link rmdYamlFieldTtl Identifier - hi def link yamlFlowString String -endif - -" You don't need this if either your markdown/syntax.vim already highlights -" citations or you are writing standard markdown -if g:rmd_syn_hl_citations - " From vim-pandoc-syntax - " parenthetical citations - syn match pandocPCite /\^\@<!\[[^\[\]]\{-}-\{0,1}@[[:alnum:]_][[:alnum:]à-öø-ÿÀ-ÖØ-ß_:.#$%&\-+?<>~\/]*.\{-}\]/ contains=pandocEmphasis,pandocStrong,pandocLatex,pandocCiteKey,@Spell,pandocAmpersandEscape display - " in-text citations with location - syn match pandocICite /@[[:alnum:]_][[:alnum:]à-öø-ÿÀ-ÖØ-ß_:.#$%&\-+?<>~\/]*\s\[.\{-1,}\]/ contains=pandocCiteKey,@Spell display - " cite keys - syn match pandocCiteKey /\(-\=@[[:alnum:]_][[:alnum:]à-öø-ÿÀ-ÖØ-ß_:.#$%&\-+?<>~\/]*\)/ containedin=pandocPCite,pandocICite contains=@NoSpell display - syn match pandocCiteAnchor /[-@]/ contained containedin=pandocCiteKey display - syn match pandocCiteLocator /[\[\]]/ contained containedin=pandocPCite,pandocICite - hi def link pandocPCite Operator - hi def link pandocICite Operator - hi def link pandocCiteKey Label - hi def link pandocCiteAnchor Operator - hi def link pandocCiteLocator Operator +if len(s:save_pandoc_lngs) + let g:pandoc#syntax#codeblocks#embeds#langs = s:save_pandoc_lngs endif - -let b:current_syntax = "rmd" - +unlet s:save_pandoc_lngs let &cpo = s:cpo_save unlet s:cpo_save +let b:current_syntax = "rmd" + " vim: ts=8 sw=2 |