changeset 33368:033f22d4a52a

runtime(rmd) Update ftplugin and syntax files (#13193) Commit: https://github.com/vim/vim/commit/34745942390383ec626b168e9837d284622c7bbe Author: Jakson Alves de Aquino <jalvesaq@gmail.com> Date: Wed Sep 27 13:56:02 2023 -0300 runtime(rmd) Update ftplugin and syntax files (https://github.com/vim/vim/issues/13193) ftplugin/rmd.vim: - Set 'commentstring' dynamically according to code region. syntax/rmd.vim: - Include syntax highlighting of fenced languages dynamically. - Add conceal char for line break. Signed-off-by: Christian Brabandt <cb@256bit.org>
author Christian Brabandt <cb@256bit.org>
date Wed, 27 Sep 2023 19:00:12 +0200
parents 128c0d304a1a
children cba0c0fab5c1
files runtime/ftplugin/rmd.vim runtime/syntax/rmd.vim
diffstat 2 files changed, 84 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/runtime/ftplugin/rmd.vim
+++ b/runtime/ftplugin/rmd.vim
@@ -2,7 +2,7 @@
 " Language: R Markdown file
 " Maintainer: Jakson Alves de Aquino <jalvesaq@gmail.com>
 " Homepage: https://github.com/jalvesaq/R-Vim-runtime
-" Last Change:	Mon Feb 27, 2023  07:15PM
+" Last Change:	Mon May 29, 2023  06:31AM
 " Original work by Alex Zvoleff (adjusted from R help for rmd by Michel Kuhlmann)
 
 " Only do this when not yet done for this buffer
@@ -32,12 +32,18 @@ function FormatRmd()
   return 1
 endfunction
 
+let s:last_line = 0
 function SetRmdCommentStr()
-    if (search("^[ \t]*```[ ]*{r", "bncW") > search("^[ \t]*```$", "bncW")) || ((search('^---$', 'Wn') || search('^\.\.\.$', 'Wn')) && search('^---$', 'bnW'))
-        set commentstring=#\ %s
-    else
-        set commentstring=<!--\ %s\ -->
-    endif
+  if line('.') == s:last_line
+    return
+  endif
+  let s:last_line = line('.')
+
+  if (search("^[ \t]*```[ ]*{r", "bncW") > search("^[ \t]*```$", "bncW")) || ((search('^---$', 'Wn') || search('^\.\.\.$', 'Wn')) && search('^---$', 'bnW'))
+    set commentstring=#\ %s
+  else
+    set commentstring=<!--\ %s\ -->
+  endif
 endfunction
 
 " If you do not want both 'comments' and 'commentstring' dynamically defined,
--- a/runtime/syntax/rmd.vim
+++ b/runtime/syntax/rmd.vim
@@ -1,7 +1,7 @@
 " 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: Fri Feb 24, 2023  08:28AM
+" 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
@@ -26,6 +26,8 @@ let g:rmd_syn_hl_chunk = get(g:, 'rmd_sy
 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")
@@ -95,6 +97,11 @@ else
     hi def link yamlColonError Error
   endif
 
+  " Conceal char for manual line break
+  if &encoding ==# 'utf-8'
+    syn match rmdNewLine '  $' conceal cchar=↵
+  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
@@ -127,32 +134,78 @@ syn match knitrBodyValue ': \zs.*\ze$' k
 syn match knitrBodyVar '| \zs\S\{-}\ze:' contained containedin=knitrBodyOptions
 
 let g:rmd_fenced_languages = get(g:, 'rmd_fenced_languages', ['r'])
-for s:type in g:rmd_fenced_languages
-  if s:type =~ '='
-    let s:ft = substitute(s:type, '.*=', '', '')
-    let s:nm = substitute(s:type, '=.*', '', '')
+
+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
+  if index(s:no_syntax_vim, ftpy) >= 0
+    return
   endif
-  unlet! b:current_syntax
-  exe 'syn include @Rmd'.s:nm.' syntax/'.s:ft.'.vim'
-  if g:rmd_syn_hl_chunk
-    exe 'syn match knitrChunkDelim /```\s*{\s*'.s:nm.'/ contained containedin=knitrChunkBrace contains=knitrChunkLabel'
-    exe 'syn match knitrChunkLabelDelim /```\s*{\s*'.s:nm.',\=\s*[-[:alnum:]]\{-1,}[,}]/ contained containedin=knitrChunkBrace'
-    syn match knitrChunkDelim /}\s*$/ contained containedin=knitrChunkBrace
-    exe 'syn match knitrChunkBrace /```\s*{\s*'.s:nm.'.*$/ contained containedin=rmd'.s:nm.'Chunk contains=knitrChunkDelim,knitrChunkLabelDelim,@Rmd'.s:nm
-    exe 'syn region rmd'.s:nm.'Chunk start="^\s*```\s*{\s*=\?'.s:nm.'\>.*$" matchgroup=rmdCodeDelim end="^\s*```\ze\s*$" keepend contains=knitrChunkBrace,@Rmd'.s:nm
+  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
+      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
 
+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 '^:::\+$'