rmd.vim 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. " Only load this indent file when no other was loaded.
  9. if exists("b:did_indent")
  10. finish
  11. endif
  12. runtime indent/r.vim
  13. let s:RIndent = function(substitute(&indentexpr, "()", "", ""))
  14. let b:did_indent = 1
  15. setlocal indentkeys=0{,0},<:>,!^F,o,O,e
  16. setlocal indentexpr=GetRmdIndent()
  17. let b:undo_indent = "setl inde< indk<"
  18. if exists("*GetRmdIndent")
  19. finish
  20. endif
  21. let s:cpo_save = &cpo
  22. set cpo&vim
  23. " Simple Python indentation algorithm
  24. function s:GetPyIndent()
  25. let plnum = prevnonblank(v:lnum - 1)
  26. let pline = getline(plnum)
  27. let cline = getline(v:lnum)
  28. if pline =~ '^s```\s*{\s*python '
  29. return 0
  30. elseif pline =~ ':$'
  31. return indent(plnum) + &shiftwidth
  32. elseif cline =~ 'else:$'
  33. return indent(plnum) - &shiftwidth
  34. endif
  35. return indent(plnum)
  36. endfunction
  37. function s:GetMdIndent()
  38. let pline = getline(v:lnum - 1)
  39. let cline = getline(v:lnum)
  40. if prevnonblank(v:lnum - 1) < v:lnum - 1 || cline =~ '^\s*[-\+\*]\s' || cline =~ '^\s*\d\+\.\s\+'
  41. return indent(v:lnum)
  42. elseif pline =~ '^\s*[-\+\*]\s'
  43. return indent(v:lnum - 1) + 2
  44. elseif pline =~ '^\s*\d\+\.\s\+'
  45. return indent(v:lnum - 1) + 3
  46. elseif pline =~ '^\[\^\S\+\]: '
  47. return indent(v:lnum - 1) + shiftwidth()
  48. endif
  49. return indent(prevnonblank(v:lnum - 1))
  50. endfunction
  51. function s:GetYamlIndent()
  52. let plnum = prevnonblank(v:lnum - 1)
  53. let pline = getline(plnum)
  54. if pline =~ ':\s*$'
  55. return indent(plnum) + shiftwidth()
  56. elseif pline =~ '^\s*- '
  57. return indent(v:lnum) + 2
  58. endif
  59. return indent(plnum)
  60. endfunction
  61. function GetRmdIndent()
  62. if getline(".") =~ '^[ \t]*```{r .*}$' || getline(".") =~ '^[ \t]*```$'
  63. return 0
  64. endif
  65. if search('^[ \t]*```{r', "bncW") > search('^[ \t]*```$', "bncW")
  66. return s:RIndent()
  67. elseif v:lnum > 1 && (search('^---$', "bnW") == 1 &&
  68. \ (search('^---$', "nW") > v:lnum || search('^\.\.\.$', "nW") > v:lnum))
  69. return s:GetYamlIndent()
  70. elseif search('^[ \t]*```{python', "bncW") > search('^[ \t]*```$', "bncW")
  71. return s:GetPyIndent()
  72. else
  73. return s:GetMdIndent()
  74. endif
  75. endfunction
  76. let &cpo = s:cpo_save
  77. unlet s:cpo_save
  78. " vim: sw=2