rmd.vim 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. " Vim indent file
  2. " Language: Rmd
  3. " Author: Jakson Alves de Aquino <jalvesaq@gmail.com>
  4. " Homepage: https://github.com/jalvesaq/R-Vim-runtime
  5. " Last Change: Sun Aug 19, 2018 09:14PM
  6. " Only load this indent file when no other was loaded.
  7. if exists("b:did_indent")
  8. finish
  9. endif
  10. runtime indent/r.vim
  11. let s:RIndent = function(substitute(&indentexpr, "()", "", ""))
  12. let b:did_indent = 1
  13. setlocal indentkeys=0{,0},:,!^F,o,O,e
  14. setlocal indentexpr=GetRmdIndent()
  15. if exists("*GetRmdIndent")
  16. finish
  17. endif
  18. let s:cpo_save = &cpo
  19. set cpo&vim
  20. function s:GetMdIndent()
  21. let pline = getline(v:lnum - 1)
  22. let cline = getline(v:lnum)
  23. if prevnonblank(v:lnum - 1) < v:lnum - 1 || cline =~ '^\s*[-\+\*]\s' || cline =~ '^\s*\d\+\.\s\+'
  24. return indent(v:lnum)
  25. elseif pline =~ '^\s*[-\+\*]\s'
  26. return indent(v:lnum - 1) + 2
  27. elseif pline =~ '^\s*\d\+\.\s\+'
  28. return indent(v:lnum - 1) + 3
  29. endif
  30. return indent(prevnonblank(v:lnum - 1))
  31. endfunction
  32. function s:GetYamlIndent()
  33. let pline = getline(v:lnum - 1)
  34. if pline =~ ':\s*$'
  35. return indent(v:lnum) + &sw
  36. elseif pline =~ '^\s*- '
  37. return indent(v:lnum) + 2
  38. endif
  39. return indent(prevnonblank(v:lnum - 1))
  40. endfunction
  41. function GetRmdIndent()
  42. if getline(".") =~ '^[ \t]*```{r .*}$' || getline(".") =~ '^[ \t]*```$'
  43. return 0
  44. endif
  45. if search('^[ \t]*```{r', "bncW") > search('^[ \t]*```$', "bncW")
  46. return s:RIndent()
  47. elseif v:lnum > 1 && search('^---$', "bnW") == 1 &&
  48. \ (search('^---$', "nW") > v:lnum || search('^...$', "nW") > v:lnum)
  49. return s:GetYamlIndent()
  50. else
  51. return s:GetMdIndent()
  52. endif
  53. endfunction
  54. let &cpo = s:cpo_save
  55. unlet s:cpo_save
  56. " vim: sw=2