rmd.vim 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 Mar 28, 2021 08:05PM
  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. " Simple Python indentation algorithm
  21. function s:GetPyIndent()
  22. let plnum = prevnonblank(v:lnum - 1)
  23. let pline = getline(plnum)
  24. let cline = getline(v:lnum)
  25. if pline =~ '^s```\s*{\s*python '
  26. return 0
  27. elseif pline =~ ':$'
  28. return indent(plnum) + &shiftwidth
  29. elseif cline =~ 'else:$'
  30. return indent(plnum) - &shiftwidth
  31. endif
  32. return indent(plnum)
  33. endfunction
  34. function s:GetMdIndent()
  35. let pline = getline(v:lnum - 1)
  36. let cline = getline(v:lnum)
  37. if prevnonblank(v:lnum - 1) < v:lnum - 1 || cline =~ '^\s*[-\+\*]\s' || cline =~ '^\s*\d\+\.\s\+'
  38. return indent(v:lnum)
  39. elseif pline =~ '^\s*[-\+\*]\s'
  40. return indent(v:lnum - 1) + 2
  41. elseif pline =~ '^\s*\d\+\.\s\+'
  42. return indent(v:lnum - 1) + 3
  43. endif
  44. return indent(prevnonblank(v:lnum - 1))
  45. endfunction
  46. function s:GetYamlIndent()
  47. let plnum = prevnonblank(v:lnum - 1)
  48. let pline = getline(plnum)
  49. if pline =~ ':\s*$'
  50. return indent(plnum) + shiftwidth()
  51. elseif pline =~ '^\s*- '
  52. return indent(v:lnum) + 2
  53. endif
  54. return indent(plnum)
  55. endfunction
  56. function GetRmdIndent()
  57. if getline(".") =~ '^[ \t]*```{r .*}$' || getline(".") =~ '^[ \t]*```$'
  58. return 0
  59. endif
  60. if search('^[ \t]*```{r', "bncW") > search('^[ \t]*```$', "bncW")
  61. return s:RIndent()
  62. elseif v:lnum > 1 && (search('^---$', "bnW") == 1 &&
  63. \ (search('^---$', "nW") > v:lnum || search('^\.\.\.$', "nW") > v:lnum))
  64. return s:GetYamlIndent()
  65. elseif search('^[ \t]*```{python', "bncW") > search('^[ \t]*```$', "bncW")
  66. return s:GetPyIndent()
  67. else
  68. return s:GetMdIndent()
  69. endif
  70. endfunction
  71. let &cpo = s:cpo_save
  72. unlet s:cpo_save
  73. " vim: sw=2