blob: f68c09076395ff03e031020008deea1423f64f12 [file] [log] [blame]
Bram Moolenaardb6ea062014-07-10 22:01:47 +02001" Vim indent file
2" Language: R Documentation (Help), *.Rd
3" Author: Jakson Alves de Aquino <jalvesaq@gmail.com>
Bram Moolenaar77cdfd12016-03-12 12:57:59 +01004" Homepage: https://github.com/jalvesaq/R-Vim-runtime
Bram Moolenaardd60c362023-02-27 15:49:53 +00005" Last Change: Feb 25, 2023
Bram Moolenaardb6ea062014-07-10 22:01:47 +02006
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
Bram Moolenaardb6ea062014-07-10 22:01:47 +020016setlocal noautoindent
17setlocal nocindent
18setlocal nosmartindent
19setlocal nolisp
Bram Moolenaardb6ea062014-07-10 22:01:47 +020020setlocal indentkeys=0{,0},:,!^F,o,O,e
21setlocal indentexpr=GetCorrectRHelpIndent()
22
Bram Moolenaardd60c362023-02-27 15:49:53 +000023let b:undo_indent = "setl ai< cin< inde< indk< <lisp <si"
24
Bram Moolenaar541f92d2015-06-19 13:27:23 +020025" Only define the functions once.
26if exists("*GetRHelpIndent")
27 finish
28endif
29
Bram Moolenaardb6ea062014-07-10 22:01:47 +020030function s:SanitizeRHelpLine(line)
31 let newline = substitute(a:line, '\\\\', "x", "g")
32 let newline = substitute(newline, '\\{', "x", "g")
33 let newline = substitute(newline, '\\}', "x", "g")
34 let newline = substitute(newline, '\\%', "x", "g")
35 let newline = substitute(newline, '%.*', "", "")
36 let newline = substitute(newline, '\s*$', "", "")
37 return newline
38endfunction
39
40function GetRHelpIndent()
41
42 let clnum = line(".") " current line
43 if clnum == 1
44 return 0
45 endif
46 let cline = getline(clnum)
47
48 if cline =~ '^\s*}\s*$'
49 let i = clnum
50 let bb = -1
51 while bb != 0 && i > 1
52 let i -= 1
53 let line = s:SanitizeRHelpLine(getline(i))
54 let line2 = substitute(line, "{", "", "g")
55 let openb = strlen(line) - strlen(line2)
56 let line3 = substitute(line2, "}", "", "g")
57 let closeb = strlen(line2) - strlen(line3)
58 let bb += openb - closeb
59 endwhile
60 return indent(i)
61 endif
62
63 if cline =~ '^\s*#ifdef\>' || cline =~ '^\s*#endif\>'
64 return 0
65 endif
66
67 let lnum = clnum - 1
68 let line = getline(lnum)
69 if line =~ '^\s*#ifdef\>' || line =~ '^\s*#endif\>'
70 let lnum -= 1
71 let line = getline(lnum)
72 endif
73 while lnum > 1 && (line =~ '^\s*$' || line =~ '^#ifdef' || line =~ '^#endif')
74 let lnum -= 1
75 let line = getline(lnum)
76 endwhile
77 if lnum == 1
78 return 0
79 endif
80 let line = s:SanitizeRHelpLine(line)
81 let line2 = substitute(line, "{", "", "g")
82 let openb = strlen(line) - strlen(line2)
83 let line3 = substitute(line2, "}", "", "g")
84 let closeb = strlen(line2) - strlen(line3)
85 let bb = openb - closeb
86
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +020087 let ind = indent(lnum) + (bb * shiftwidth())
Bram Moolenaardb6ea062014-07-10 22:01:47 +020088
89 if line =~ '^\s*}\s*$'
90 let ind = indent(lnum)
91 endif
92
93 if ind < 0
94 return 0
95 endif
96
97 return ind
98endfunction
99
100function GetCorrectRHelpIndent()
101 let lastsection = search('^\\[a-z]*{', "bncW")
102 let secname = getline(lastsection)
103 if secname =~ '^\\usage{' || secname =~ '^\\examples{' || secname =~ '^\\dontshow{' || secname =~ '^\\dontrun{' || secname =~ '^\\donttest{' || secname =~ '^\\testonly{' || secname =~ '^\\method{.*}{.*}('
104 return s:RIndent()
105 else
106 return GetRHelpIndent()
107 endif
108endfunction
109
110" vim: sw=2