rmd.vim (2432B)
1 " Vim indent file 2 " Language: Rmd 3 " Maintainer: This runtime file is looking for a new maintainer. 4 " Former Maintainer: Jakson Alves de Aquino <jalvesaq@gmail.com> 5 " Former Repository: https://github.com/jalvesaq/R-Vim-runtime 6 " Last Change: 2022 Nov 09 09:44PM 7 " 2024 Feb 19 by Vim Project (announce adoption) 8 9 10 " Only load this indent file when no other was loaded. 11 if exists("b:did_indent") 12 finish 13 endif 14 runtime indent/r.vim 15 let s:RIndent = function(substitute(&indentexpr, "()", "", "")) 16 let b:did_indent = 1 17 18 setlocal indentkeys=0{,0},<:>,!^F,o,O,e 19 setlocal indentexpr=GetRmdIndent() 20 21 let b:undo_indent = "setl inde< indk<" 22 23 if exists("*GetRmdIndent") 24 finish 25 endif 26 27 let s:cpo_save = &cpo 28 set cpo&vim 29 30 " Simple Python indentation algorithm 31 function s:GetPyIndent() 32 let plnum = prevnonblank(v:lnum - 1) 33 let pline = getline(plnum) 34 let cline = getline(v:lnum) 35 if pline =~ '^s```\s*{\s*python ' 36 return 0 37 elseif pline =~ ':$' 38 return indent(plnum) + &shiftwidth 39 elseif cline =~ 'else:$' 40 return indent(plnum) - &shiftwidth 41 endif 42 return indent(plnum) 43 endfunction 44 45 function s:GetMdIndent() 46 let pline = getline(v:lnum - 1) 47 let cline = getline(v:lnum) 48 if prevnonblank(v:lnum - 1) < v:lnum - 1 || cline =~ '^\s*[-\+\*]\s' || cline =~ '^\s*\d\+\.\s\+' 49 return indent(v:lnum) 50 elseif pline =~ '^\s*[-\+\*]\s' 51 return indent(v:lnum - 1) + 2 52 elseif pline =~ '^\s*\d\+\.\s\+' 53 return indent(v:lnum - 1) + 3 54 elseif pline =~ '^\[\^\S\+\]: ' 55 return indent(v:lnum - 1) + shiftwidth() 56 endif 57 return indent(prevnonblank(v:lnum - 1)) 58 endfunction 59 60 function s:GetYamlIndent() 61 let plnum = prevnonblank(v:lnum - 1) 62 let pline = getline(plnum) 63 if pline =~ ':\s*$' 64 return indent(plnum) + shiftwidth() 65 elseif pline =~ '^\s*- ' 66 return indent(v:lnum) + 2 67 endif 68 return indent(plnum) 69 endfunction 70 71 function GetRmdIndent() 72 if getline(".") =~ '^[ \t]*```{r .*}$' || getline(".") =~ '^[ \t]*```$' 73 return 0 74 endif 75 if search('^[ \t]*```{r', "bncW") > search('^[ \t]*```$', "bncW") 76 return s:RIndent() 77 elseif v:lnum > 1 && (search('^---$', "bnW") == 1 && 78 \ (search('^---$', "nW") > v:lnum || search('^\.\.\.$', "nW") > v:lnum)) 79 return s:GetYamlIndent() 80 elseif search('^[ \t]*```{python', "bncW") > search('^[ \t]*```$', "bncW") 81 return s:GetPyIndent() 82 else 83 return s:GetMdIndent() 84 endif 85 endfunction 86 87 let &cpo = s:cpo_save 88 unlet s:cpo_save 89 90 " vim: sw=2