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