robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

robinmerge: 1dc9728daa1d8a2702

master
10.7 KiB269 linesraw
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")
16  finish
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
24  let b:rmd_has_LaTeX = v:false
25elseif g:rmd_include_latex == 2
26  let 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")
44  if hlexists('pandocDelimitedCodeBlock')
45    syn clear pandocDelimitedCodeBlock
46  endif
47
48  if len(s:save_pandoc_lngs) > 0 && !exists('g:rmd_fenced_languages')
49    let g:rmd_fenced_languages = deepcopy(s:save_pandoc_lngs)
50  endif
51
52  " Recognize inline R code
53  syn 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
59  let g:rmd_syn_hl_yaml = get(g:, 'rmd_syn_hl_yaml', 1)
60  " Add syntax highlighting of citation keys
61  let 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:
66  if exists('g:markdown_fenced_languages') && !exists('g:rmd_fenced_languages')
67    let g:rmd_fenced_languages = deepcopy(g:markdown_fenced_languages)
68  endif
69
70  if exists('g:markdown_fenced_languages') && len(g:markdown_fenced_languages) > 0
71    let s:save_mfl = deepcopy(g:markdown_fenced_languages)
72  endif
73  " Don't waste time loading syntax that will be discarded:
74  let g:markdown_fenced_languages = []
75  runtime syntax/markdown.vim
76  if exists('s:save_mfl') > 0
77    let g:markdown_fenced_languages = deepcopy(s:save_mfl)
78    unlet s:save_mfl
79  endif
80  syn 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
87  if g:rmd_syn_hl_yaml
88    " Basic highlighting of YAML header
89    syn match rmdYamlFieldTtl /^\s*\zs\w\%(-\|\w\)*\ze:/ contained
90    syn match rmdYamlFieldTtl /^\s*-\s*\zs\w\%(-\|\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 match yamlComment /#.*/ contained
96    " A second colon is a syntax error, unless within a string or following !expr
97    syn match yamlColonError /:\s*[^'^"^!]*:/ contained
98    if &filetype == 'quarto'
99      syn region pandocYAMLHeader matchgroup=rmdYamlBlockDelim start=/\%(\%^\|\_^\s*\n\)\@<=\_^-\{3}\ze\n.\+/ end=/^---$/ keepend contains=rmdYamlFieldTtl,yamlFlowString,yamlComment,yamlColonError
100    else
101      syn region pandocYAMLHeader matchgroup=rmdYamlBlockDelim start=/\%(\%^\|\_^\s*\n\)\@<=\_^-\{3}\ze\n.\+/ end=/^\([-.]\)\1\{2}$/ keepend contains=rmdYamlFieldTtl,yamlFlowString,yamlComment,yamlColonError
102    endif
103    hi def link rmdYamlBlockDelim Delimiter
104    hi def link rmdYamlFieldTtl Identifier
105    hi def link yamlFlowString String
106    hi def link yamlComment Comment
107    hi def link yamlColonError Error
108  endif
109
110  " Conceal char for manual line break
111  if &encoding ==# 'utf-8'
112    syn match rmdNewLine '  $' conceal cchar=↵
113  endif
114
115  " You don't need this if either your markdown/syntax.vim already highlights
116  " citations or you are writing standard markdown
117  if g:rmd_syn_hl_citations
118    " From vim-pandoc-syntax
119    " parenthetical citations
120    syn match pandocPCite /\^\@<!\[[^\[\]]\{-}-\{0,1}@[[:alnum:]_][[:alnum:]à-öø-ÿÀ-ÖØ-ß_:.#$%&\-+?<>~\/]*.\{-}\]/ contains=pandocEmphasis,pandocStrong,pandocLatex,pandocCiteKey,@Spell,pandocAmpersandEscape display
121    " in-text citations with location
122    syn match pandocICite /@[[:alnum:]_][[:alnum:]à-öø-ÿÀ-ÖØ-ß_:.#$%&\-+?<>~\/]*\s\[.\{-1,}\]/ contains=pandocCiteKey,@Spell display
123    " cite keys
124    syn match pandocCiteKey /\(-\=@[[:alnum:]_][[:alnum:]à-öø-ÿÀ-ÖØ-ß_:.#$%&\-+?<>~\/]*\)/ containedin=pandocPCite,pandocICite contains=@NoSpell display
125    syn match pandocCiteAnchor /[-@]/ contained containedin=pandocCiteKey display
126    syn match pandocCiteLocator /[\[\]]/ contained containedin=pandocPCite,pandocICite
127    hi def link pandocPCite Operator
128    hi def link pandocICite Operator
129    hi def link pandocCiteKey Label
130    hi def link pandocCiteAnchor Operator
131    hi def link pandocCiteLocator Operator
132  endif
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)
150  if a:lng =~ '='
151    let ftpy = substitute(a:lng, '.*=', '', '')
152    let lnm = substitute(a:lng, '=.*', '', '')
153  else
154    let ftpy = a:lng
155    let lnm  = a:lng
156  endif
157  if index(s:no_syntax_vim, ftpy) >= 0
158    return
159  endif
160  if len(globpath(&rtp, "syntax/" . ftpy . ".vim"))
161    unlet! b:current_syntax
162    exe 'syn include @Rmd'.lnm.' syntax/'.ftpy.'.vim'
163    let b:current_syntax = "rmd"
164    if g:rmd_syn_hl_chunk
165      exe 'syn match knitrChunkDelim /```\s*{\s*'.lnm.'/ contained containedin=knitrChunkBrace contains=knitrChunkLabel'
166      exe 'syn match knitrChunkLabelDelim /```\s*{\s*'.lnm.',\=\s*[-[:alnum:]]\{-1,}[,}]/ contained containedin=knitrChunkBrace'
167      syn match knitrChunkDelim /}\s*$/ contained containedin=knitrChunkBrace
168      exe 'syn match knitrChunkBrace /```\s*{\s*'.lnm.'.*$/ contained containedin=rmd'.lnm.'Chunk contains=knitrChunkDelim,knitrChunkLabelDelim,@Rmd'.lnm
169      exe 'syn region rmd'.lnm.'Chunk start="^\s*```\s*{\s*=\?'.lnm.'\>.*$" matchgroup=rmdCodeDelim end="^\s*```\ze\s*$" keepend contains=knitrChunkBrace,@Rmd'.lnm
170
171      hi link knitrChunkLabel Identifier
172      hi link knitrChunkDelim rmdCodeDelim
173      hi link knitrChunkLabelDelim rmdCodeDelim
174    else
175      exe 'syn region rmd'.lnm.'Chunk matchgroup=rmdCodeDelim start="^\s*```\s*{\s*=\?'.lnm.'\>.*$" matchgroup=rmdCodeDelim end="^\s*```\ze\s*$" keepend contains=@Rmd'.lnm
176    endif
177  else
178    " Avoid the cost of running globpath() whenever the buffer is saved
179    let s:no_syntax_vim += [ftpy]
180  endif
181endfunction
182
183for s:type in g:rmd_fenced_languages
184  call s:IncludeLanguage(s:type)
185endfor
186unlet! s:type
187
188let s:LaTeX_included = v:false
189function s:IncludeLaTeX()
190  let s:LaTeX_included = v:true
191  unlet! b:current_syntax
192  syn include @RmdLaTeX syntax/tex.vim
193  " From vim-pandoc-syntax
194  syn region rmdLaTeXInlineMath start=/\v\\@<!\$\S@=/ end=/\v\\@<!\$\d@!/ keepend contains=@RmdLaTeX
195  syn match rmdLaTeXCmd /\\[[:alpha:]]\+\(\({.\{-}}\)\=\(\[.\{-}\]\)\=\)*/ contains=@RmdLaTeX
196  syn region rmdLaTeX start='\$\$'  end='\$\$' keepend contains=@RmdLaTeX
197  syn region rmdLaTeX start=/\\begin{\z(.\{-}\)}/ end=/\\end{\z1}/ keepend contains=@RmdLaTeX
198endfunction
199
200function s:CheckRmdFencedLanguages()
201  let alines = getline(1, '$')
202  call filter(alines, "v:val =~ '^```{'")
203  call map(alines, "substitute(v:val, '^```{', '', '')")
204  call map(alines, "substitute(v:val, '\\W.*', '', '')")
205  for tpy in alines
206    if len(tpy) == 0
207      continue
208    endif
209    let has_lng = 0
210    for lng in g:rmd_fenced_languages
211      if tpy == lng
212        let has_lng = 1
213        continue
214      endif
215    endfor
216    if has_lng == 0
217      let g:rmd_fenced_languages += [tpy]
218      call s:IncludeLanguage(tpy)
219    endif
220  endfor
221
222  if hlexists('pandocLaTeXCommand')
223    return
224  endif
225  if g:rmd_include_latex
226    if !b:rmd_has_LaTeX && (search('\$\$', 'wn') > 0 ||
227          \ search('\\begin{', 'wn') > 0) ||
228          \ search('\\[[:alpha:]]\+', 'wn') ||
229          \ search('\$[^\$]\+\$', 'wn')
230      let b:rmd_has_LaTeX = v:true
231    endif
232    if b:rmd_has_LaTeX && !s:LaTeX_included
233      call s:IncludeLaTeX()
234    endif
235  endif
236endfunction
237
238if g:rmd_dynamic_fenced_languages
239  call s:CheckRmdFencedLanguages()
240  augroup RmdSyntax
241    autocmd!
242    autocmd BufWritePost <buffer> call s:CheckRmdFencedLanguages()
243  augroup 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)
259  let 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