Bram Moolenaar | db6ea06 | 2014-07-10 22:01:47 +0200 | [diff] [blame] | 1 | " Vim indent file |
| 2 | " Language: R Documentation (Help), *.Rd |
| 3 | " Author: Jakson Alves de Aquino <jalvesaq@gmail.com> |
Bram Moolenaar | 77cdfd1 | 2016-03-12 12:57:59 +0100 | [diff] [blame] | 4 | " Homepage: https://github.com/jalvesaq/R-Vim-runtime |
Bram Moolenaar | 71badf9 | 2023-04-22 22:40:14 +0100 | [diff] [blame] | 5 | " Last Change: Mon Feb 27, 2023 07:01PM |
Bram Moolenaar | db6ea06 | 2014-07-10 22:01:47 +0200 | [diff] [blame] | 6 | |
| 7 | |
| 8 | " Only load this indent file when no other was loaded. |
| 9 | if exists("b:did_indent") |
| 10 | finish |
| 11 | endif |
| 12 | runtime indent/r.vim |
| 13 | let s:RIndent = function(substitute(&indentexpr, "()", "", "")) |
| 14 | let b:did_indent = 1 |
| 15 | |
Bram Moolenaar | db6ea06 | 2014-07-10 22:01:47 +0200 | [diff] [blame] | 16 | setlocal noautoindent |
| 17 | setlocal nocindent |
| 18 | setlocal nosmartindent |
| 19 | setlocal nolisp |
Bram Moolenaar | db6ea06 | 2014-07-10 22:01:47 +0200 | [diff] [blame] | 20 | setlocal indentkeys=0{,0},:,!^F,o,O,e |
| 21 | setlocal indentexpr=GetCorrectRHelpIndent() |
| 22 | |
Bram Moolenaar | 71badf9 | 2023-04-22 22:40:14 +0100 | [diff] [blame] | 23 | let b:undo_indent = "setl ai< cin< inde< indk< lisp< si<" |
Bram Moolenaar | dd60c36 | 2023-02-27 15:49:53 +0000 | [diff] [blame] | 24 | |
Bram Moolenaar | 541f92d | 2015-06-19 13:27:23 +0200 | [diff] [blame] | 25 | " Only define the functions once. |
| 26 | if exists("*GetRHelpIndent") |
| 27 | finish |
| 28 | endif |
| 29 | |
Bram Moolenaar | db6ea06 | 2014-07-10 22:01:47 +0200 | [diff] [blame] | 30 | function 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 |
| 38 | endfunction |
| 39 | |
| 40 | function 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 Moolenaar | cd5c8f8 | 2017-04-09 20:11:58 +0200 | [diff] [blame] | 87 | let ind = indent(lnum) + (bb * shiftwidth()) |
Bram Moolenaar | db6ea06 | 2014-07-10 22:01:47 +0200 | [diff] [blame] | 88 | |
| 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 |
| 98 | endfunction |
| 99 | |
| 100 | function 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 |
| 108 | endfunction |
| 109 | |
| 110 | " vim: sw=2 |