blob: 97820ae148706f4b6c8bf0e45b96bb86470867f6 [file] [log] [blame]
Bram Moolenaardb6ea062014-07-10 22:01:47 +02001" Vim indent file
2" Language: R Documentation (Help), *.Rd
Christian Brabandtf9ca1392024-02-19 20:37:11 +01003" 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: 2023 Feb 27 07:01PM
7" 2024 Feb 19 by Vim Project (announce adoption)
Bram Moolenaardb6ea062014-07-10 22:01:47 +02008
9
10" Only load this indent file when no other was loaded.
11if exists("b:did_indent")
12 finish
13endif
14runtime indent/r.vim
15let s:RIndent = function(substitute(&indentexpr, "()", "", ""))
16let b:did_indent = 1
17
Bram Moolenaardb6ea062014-07-10 22:01:47 +020018setlocal noautoindent
19setlocal nocindent
20setlocal nosmartindent
21setlocal nolisp
Bram Moolenaardb6ea062014-07-10 22:01:47 +020022setlocal indentkeys=0{,0},:,!^F,o,O,e
23setlocal indentexpr=GetCorrectRHelpIndent()
24
Bram Moolenaar71badf92023-04-22 22:40:14 +010025let b:undo_indent = "setl ai< cin< inde< indk< lisp< si<"
Bram Moolenaardd60c362023-02-27 15:49:53 +000026
Bram Moolenaar541f92d2015-06-19 13:27:23 +020027" Only define the functions once.
28if exists("*GetRHelpIndent")
29 finish
30endif
31
Bram Moolenaardb6ea062014-07-10 22:01:47 +020032function s:SanitizeRHelpLine(line)
33 let newline = substitute(a:line, '\\\\', "x", "g")
34 let newline = substitute(newline, '\\{', "x", "g")
35 let newline = substitute(newline, '\\}', "x", "g")
36 let newline = substitute(newline, '\\%', "x", "g")
37 let newline = substitute(newline, '%.*', "", "")
38 let newline = substitute(newline, '\s*$', "", "")
39 return newline
40endfunction
41
42function GetRHelpIndent()
43
44 let clnum = line(".") " current line
45 if clnum == 1
46 return 0
47 endif
48 let cline = getline(clnum)
49
50 if cline =~ '^\s*}\s*$'
51 let i = clnum
52 let bb = -1
53 while bb != 0 && i > 1
54 let i -= 1
55 let line = s:SanitizeRHelpLine(getline(i))
56 let line2 = substitute(line, "{", "", "g")
57 let openb = strlen(line) - strlen(line2)
58 let line3 = substitute(line2, "}", "", "g")
59 let closeb = strlen(line2) - strlen(line3)
60 let bb += openb - closeb
61 endwhile
62 return indent(i)
63 endif
64
65 if cline =~ '^\s*#ifdef\>' || cline =~ '^\s*#endif\>'
66 return 0
67 endif
68
69 let lnum = clnum - 1
70 let line = getline(lnum)
71 if line =~ '^\s*#ifdef\>' || line =~ '^\s*#endif\>'
72 let lnum -= 1
73 let line = getline(lnum)
74 endif
75 while lnum > 1 && (line =~ '^\s*$' || line =~ '^#ifdef' || line =~ '^#endif')
76 let lnum -= 1
77 let line = getline(lnum)
78 endwhile
79 if lnum == 1
80 return 0
81 endif
82 let line = s:SanitizeRHelpLine(line)
83 let line2 = substitute(line, "{", "", "g")
84 let openb = strlen(line) - strlen(line2)
85 let line3 = substitute(line2, "}", "", "g")
86 let closeb = strlen(line2) - strlen(line3)
87 let bb = openb - closeb
88
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +020089 let ind = indent(lnum) + (bb * shiftwidth())
Bram Moolenaardb6ea062014-07-10 22:01:47 +020090
91 if line =~ '^\s*}\s*$'
92 let ind = indent(lnum)
93 endif
94
95 if ind < 0
96 return 0
97 endif
98
99 return ind
100endfunction
101
102function GetCorrectRHelpIndent()
103 let lastsection = search('^\\[a-z]*{', "bncW")
104 let secname = getline(lastsection)
105 if secname =~ '^\\usage{' || secname =~ '^\\examples{' || secname =~ '^\\dontshow{' || secname =~ '^\\dontrun{' || secname =~ '^\\donttest{' || secname =~ '^\\testonly{' || secname =~ '^\\method{.*}{.*}('
106 return s:RIndent()
107 else
108 return GetRHelpIndent()
109 endif
110endfunction
111
112" vim: sw=2