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 | 541f92d | 2015-06-19 13:27:23 +0200 | [diff] [blame] | 4 | " Last Change: Thu Oct 16, 2014 07:07AM |
Bram Moolenaar | db6ea06 | 2014-07-10 22:01:47 +0200 | [diff] [blame] | 5 | |
| 6 | |
| 7 | " Only load this indent file when no other was loaded. |
| 8 | if exists("b:did_indent") |
| 9 | finish |
| 10 | endif |
| 11 | runtime indent/r.vim |
| 12 | let s:RIndent = function(substitute(&indentexpr, "()", "", "")) |
| 13 | let b:did_indent = 1 |
| 14 | |
Bram Moolenaar | db6ea06 | 2014-07-10 22:01:47 +0200 | [diff] [blame] | 15 | setlocal noautoindent |
| 16 | setlocal nocindent |
| 17 | setlocal nosmartindent |
| 18 | setlocal nolisp |
Bram Moolenaar | db6ea06 | 2014-07-10 22:01:47 +0200 | [diff] [blame] | 19 | setlocal indentkeys=0{,0},:,!^F,o,O,e |
| 20 | setlocal indentexpr=GetCorrectRHelpIndent() |
| 21 | |
Bram Moolenaar | 541f92d | 2015-06-19 13:27:23 +0200 | [diff] [blame] | 22 | " Only define the functions once. |
| 23 | if exists("*GetRHelpIndent") |
| 24 | finish |
| 25 | endif |
| 26 | |
Bram Moolenaar | db6ea06 | 2014-07-10 22:01:47 +0200 | [diff] [blame] | 27 | function s:SanitizeRHelpLine(line) |
| 28 | let newline = substitute(a:line, '\\\\', "x", "g") |
| 29 | let newline = substitute(newline, '\\{', "x", "g") |
| 30 | let newline = substitute(newline, '\\}', "x", "g") |
| 31 | let newline = substitute(newline, '\\%', "x", "g") |
| 32 | let newline = substitute(newline, '%.*', "", "") |
| 33 | let newline = substitute(newline, '\s*$', "", "") |
| 34 | return newline |
| 35 | endfunction |
| 36 | |
| 37 | function GetRHelpIndent() |
| 38 | |
| 39 | let clnum = line(".") " current line |
| 40 | if clnum == 1 |
| 41 | return 0 |
| 42 | endif |
| 43 | let cline = getline(clnum) |
| 44 | |
| 45 | if cline =~ '^\s*}\s*$' |
| 46 | let i = clnum |
| 47 | let bb = -1 |
| 48 | while bb != 0 && i > 1 |
| 49 | let i -= 1 |
| 50 | let line = s:SanitizeRHelpLine(getline(i)) |
| 51 | let line2 = substitute(line, "{", "", "g") |
| 52 | let openb = strlen(line) - strlen(line2) |
| 53 | let line3 = substitute(line2, "}", "", "g") |
| 54 | let closeb = strlen(line2) - strlen(line3) |
| 55 | let bb += openb - closeb |
| 56 | endwhile |
| 57 | return indent(i) |
| 58 | endif |
| 59 | |
| 60 | if cline =~ '^\s*#ifdef\>' || cline =~ '^\s*#endif\>' |
| 61 | return 0 |
| 62 | endif |
| 63 | |
| 64 | let lnum = clnum - 1 |
| 65 | let line = getline(lnum) |
| 66 | if line =~ '^\s*#ifdef\>' || line =~ '^\s*#endif\>' |
| 67 | let lnum -= 1 |
| 68 | let line = getline(lnum) |
| 69 | endif |
| 70 | while lnum > 1 && (line =~ '^\s*$' || line =~ '^#ifdef' || line =~ '^#endif') |
| 71 | let lnum -= 1 |
| 72 | let line = getline(lnum) |
| 73 | endwhile |
| 74 | if lnum == 1 |
| 75 | return 0 |
| 76 | endif |
| 77 | let line = s:SanitizeRHelpLine(line) |
| 78 | let line2 = substitute(line, "{", "", "g") |
| 79 | let openb = strlen(line) - strlen(line2) |
| 80 | let line3 = substitute(line2, "}", "", "g") |
| 81 | let closeb = strlen(line2) - strlen(line3) |
| 82 | let bb = openb - closeb |
| 83 | |
| 84 | let ind = indent(lnum) + (bb * &sw) |
| 85 | |
| 86 | if line =~ '^\s*}\s*$' |
| 87 | let ind = indent(lnum) |
| 88 | endif |
| 89 | |
| 90 | if ind < 0 |
| 91 | return 0 |
| 92 | endif |
| 93 | |
| 94 | return ind |
| 95 | endfunction |
| 96 | |
| 97 | function GetCorrectRHelpIndent() |
| 98 | let lastsection = search('^\\[a-z]*{', "bncW") |
| 99 | let secname = getline(lastsection) |
| 100 | if secname =~ '^\\usage{' || secname =~ '^\\examples{' || secname =~ '^\\dontshow{' || secname =~ '^\\dontrun{' || secname =~ '^\\donttest{' || secname =~ '^\\testonly{' || secname =~ '^\\method{.*}{.*}(' |
| 101 | return s:RIndent() |
| 102 | else |
| 103 | return GetRHelpIndent() |
| 104 | endif |
| 105 | endfunction |
| 106 | |
| 107 | " vim: sw=2 |