summaryrefslogtreecommitdiffstats
path: root/runtime/indent/rhelp.vim
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 08:50:31 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 08:50:31 +0000
commitaed8ce9da277f5ecffe968b324f242c41c3b752a (patch)
treed2e538394cb7a8a7c42a4aac6ccf1a8e3256999b /runtime/indent/rhelp.vim
parentInitial commit. (diff)
downloadvim-aed8ce9da277f5ecffe968b324f242c41c3b752a.tar.xz
vim-aed8ce9da277f5ecffe968b324f242c41c3b752a.zip
Adding upstream version 2:9.0.1378.upstream/2%9.0.1378upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'runtime/indent/rhelp.vim')
-rw-r--r--runtime/indent/rhelp.vim110
1 files changed, 110 insertions, 0 deletions
diff --git a/runtime/indent/rhelp.vim b/runtime/indent/rhelp.vim
new file mode 100644
index 0000000..f68c090
--- /dev/null
+++ b/runtime/indent/rhelp.vim
@@ -0,0 +1,110 @@
+" Vim indent file
+" Language: R Documentation (Help), *.Rd
+" Author: Jakson Alves de Aquino <jalvesaq@gmail.com>
+" Homepage: https://github.com/jalvesaq/R-Vim-runtime
+" Last Change: Feb 25, 2023
+
+
+" Only load this indent file when no other was loaded.
+if exists("b:did_indent")
+ finish
+endif
+runtime indent/r.vim
+let s:RIndent = function(substitute(&indentexpr, "()", "", ""))
+let b:did_indent = 1
+
+setlocal noautoindent
+setlocal nocindent
+setlocal nosmartindent
+setlocal nolisp
+setlocal indentkeys=0{,0},:,!^F,o,O,e
+setlocal indentexpr=GetCorrectRHelpIndent()
+
+let b:undo_indent = "setl ai< cin< inde< indk< <lisp <si"
+
+" Only define the functions once.
+if exists("*GetRHelpIndent")
+ finish
+endif
+
+function s:SanitizeRHelpLine(line)
+ let newline = substitute(a:line, '\\\\', "x", "g")
+ let newline = substitute(newline, '\\{', "x", "g")
+ let newline = substitute(newline, '\\}', "x", "g")
+ let newline = substitute(newline, '\\%', "x", "g")
+ let newline = substitute(newline, '%.*', "", "")
+ let newline = substitute(newline, '\s*$', "", "")
+ return newline
+endfunction
+
+function GetRHelpIndent()
+
+ let clnum = line(".") " current line
+ if clnum == 1
+ return 0
+ endif
+ let cline = getline(clnum)
+
+ if cline =~ '^\s*}\s*$'
+ let i = clnum
+ let bb = -1
+ while bb != 0 && i > 1
+ let i -= 1
+ let line = s:SanitizeRHelpLine(getline(i))
+ let line2 = substitute(line, "{", "", "g")
+ let openb = strlen(line) - strlen(line2)
+ let line3 = substitute(line2, "}", "", "g")
+ let closeb = strlen(line2) - strlen(line3)
+ let bb += openb - closeb
+ endwhile
+ return indent(i)
+ endif
+
+ if cline =~ '^\s*#ifdef\>' || cline =~ '^\s*#endif\>'
+ return 0
+ endif
+
+ let lnum = clnum - 1
+ let line = getline(lnum)
+ if line =~ '^\s*#ifdef\>' || line =~ '^\s*#endif\>'
+ let lnum -= 1
+ let line = getline(lnum)
+ endif
+ while lnum > 1 && (line =~ '^\s*$' || line =~ '^#ifdef' || line =~ '^#endif')
+ let lnum -= 1
+ let line = getline(lnum)
+ endwhile
+ if lnum == 1
+ return 0
+ endif
+ let line = s:SanitizeRHelpLine(line)
+ let line2 = substitute(line, "{", "", "g")
+ let openb = strlen(line) - strlen(line2)
+ let line3 = substitute(line2, "}", "", "g")
+ let closeb = strlen(line2) - strlen(line3)
+ let bb = openb - closeb
+
+ let ind = indent(lnum) + (bb * shiftwidth())
+
+ if line =~ '^\s*}\s*$'
+ let ind = indent(lnum)
+ endif
+
+ if ind < 0
+ return 0
+ endif
+
+ return ind
+endfunction
+
+function GetCorrectRHelpIndent()
+ let lastsection = search('^\\[a-z]*{', "bncW")
+ let secname = getline(lastsection)
+ if secname =~ '^\\usage{' || secname =~ '^\\examples{' || secname =~ '^\\dontshow{' || secname =~ '^\\dontrun{' || secname =~ '^\\donttest{' || secname =~ '^\\testonly{' || secname =~ '^\\method{.*}{.*}('
+ return s:RIndent()
+ else
+ return GetRHelpIndent()
+ endif
+endfunction
+
+" vim: sw=2