Bram Moolenaar | db6ea06 | 2014-07-10 22:01:47 +0200 | [diff] [blame] | 1 | " Vim indent file |
| 2 | " Language: R Documentation (Help), *.Rd |
Christian Brabandt | f9ca139 | 2024-02-19 20:37:11 +0100 | [diff] [blame] | 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: 2023 Feb 27 07:01PM |
| 7 | " 2024 Feb 19 by Vim Project (announce adoption) |
Bram Moolenaar | db6ea06 | 2014-07-10 22:01:47 +0200 | [diff] [blame] | 8 | |
| 9 | |
| 10 | " Only load this indent file when no other was loaded. |
| 11 | if exists("b:did_indent") |
| 12 | finish |
| 13 | endif |
| 14 | runtime indent/r.vim |
| 15 | let s:RIndent = function(substitute(&indentexpr, "()", "", "")) |
| 16 | let b:did_indent = 1 |
| 17 | |
Bram Moolenaar | db6ea06 | 2014-07-10 22:01:47 +0200 | [diff] [blame] | 18 | setlocal noautoindent |
| 19 | setlocal nocindent |
| 20 | setlocal nosmartindent |
| 21 | setlocal nolisp |
Bram Moolenaar | db6ea06 | 2014-07-10 22:01:47 +0200 | [diff] [blame] | 22 | setlocal indentkeys=0{,0},:,!^F,o,O,e |
| 23 | setlocal indentexpr=GetCorrectRHelpIndent() |
| 24 | |
Bram Moolenaar | 71badf9 | 2023-04-22 22:40:14 +0100 | [diff] [blame] | 25 | let b:undo_indent = "setl ai< cin< inde< indk< lisp< si<" |
Bram Moolenaar | dd60c36 | 2023-02-27 15:49:53 +0000 | [diff] [blame] | 26 | |
Bram Moolenaar | 541f92d | 2015-06-19 13:27:23 +0200 | [diff] [blame] | 27 | " Only define the functions once. |
| 28 | if exists("*GetRHelpIndent") |
| 29 | finish |
| 30 | endif |
| 31 | |
Bram Moolenaar | db6ea06 | 2014-07-10 22:01:47 +0200 | [diff] [blame] | 32 | function 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 |
| 40 | endfunction |
| 41 | |
| 42 | function 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 Moolenaar | cd5c8f8 | 2017-04-09 20:11:58 +0200 | [diff] [blame] | 89 | let ind = indent(lnum) + (bb * shiftwidth()) |
Bram Moolenaar | db6ea06 | 2014-07-10 22:01:47 +0200 | [diff] [blame] | 90 | |
| 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 |
| 100 | endfunction |
| 101 | |
| 102 | function 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 |
| 110 | endfunction |
| 111 | |
| 112 | " vim: sw=2 |