r.vim 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. " Vim indent file
  2. " Language: R
  3. " Author: Jakson Alves de Aquino <jalvesaq@gmail.com>
  4. " Homepage: https://github.com/jalvesaq/R-Vim-runtime
  5. " Last Change: Sun Aug 19, 2018 09:13PM
  6. " Only load this indent file when no other was loaded.
  7. if exists("b:did_indent")
  8. finish
  9. endif
  10. let b:did_indent = 1
  11. setlocal indentkeys=0{,0},:,!^F,o,O,e
  12. setlocal indentexpr=GetRIndent()
  13. " Only define the function once.
  14. if exists("*GetRIndent")
  15. finish
  16. endif
  17. let s:cpo_save = &cpo
  18. set cpo&vim
  19. " Options to make the indentation more similar to Emacs/ESS:
  20. let g:r_indent_align_args = get(g:, 'r_indent_align_args', 1)
  21. let g:r_indent_ess_comments = get(g:, 'r_indent_ess_comments', 0)
  22. let g:r_indent_comment_column = get(g:, 'r_indent_comment_column', 40)
  23. let g:r_indent_ess_compatible = get(g:, 'r_indent_ess_compatible', 0)
  24. let g:r_indent_op_pattern = get(g:, 'r_indent_op_pattern',
  25. \ '\(&\||\|+\|-\|\*\|/\|=\|\~\|%\|->\)\s*$')
  26. function s:RDelete_quotes(line)
  27. let i = 0
  28. let j = 0
  29. let line1 = ""
  30. let llen = strlen(a:line)
  31. while i < llen
  32. if a:line[i] == '"'
  33. let i += 1
  34. let line1 = line1 . 's'
  35. while !(a:line[i] == '"' && ((i > 1 && a:line[i-1] == '\' && a:line[i-2] == '\') || a:line[i-1] != '\')) && i < llen
  36. let i += 1
  37. endwhile
  38. if a:line[i] == '"'
  39. let i += 1
  40. endif
  41. else
  42. if a:line[i] == "'"
  43. let i += 1
  44. let line1 = line1 . 's'
  45. while !(a:line[i] == "'" && ((i > 1 && a:line[i-1] == '\' && a:line[i-2] == '\') || a:line[i-1] != '\')) && i < llen
  46. let i += 1
  47. endwhile
  48. if a:line[i] == "'"
  49. let i += 1
  50. endif
  51. else
  52. if a:line[i] == "`"
  53. let i += 1
  54. let line1 = line1 . 's'
  55. while a:line[i] != "`" && i < llen
  56. let i += 1
  57. endwhile
  58. if a:line[i] == "`"
  59. let i += 1
  60. endif
  61. endif
  62. endif
  63. endif
  64. if i == llen
  65. break
  66. endif
  67. let line1 = line1 . a:line[i]
  68. let j += 1
  69. let i += 1
  70. endwhile
  71. return line1
  72. endfunction
  73. " Convert foo(bar()) int foo()
  74. function s:RDelete_parens(line)
  75. if s:Get_paren_balance(a:line, "(", ")") != 0
  76. return a:line
  77. endif
  78. let i = 0
  79. let j = 0
  80. let line1 = ""
  81. let llen = strlen(a:line)
  82. while i < llen
  83. let line1 = line1 . a:line[i]
  84. if a:line[i] == '('
  85. let nop = 1
  86. while nop > 0 && i < llen
  87. let i += 1
  88. if a:line[i] == ')'
  89. let nop -= 1
  90. else
  91. if a:line[i] == '('
  92. let nop += 1
  93. endif
  94. endif
  95. endwhile
  96. let line1 = line1 . a:line[i]
  97. endif
  98. let i += 1
  99. endwhile
  100. return line1
  101. endfunction
  102. function! s:Get_paren_balance(line, o, c)
  103. let line2 = substitute(a:line, a:o, "", "g")
  104. let openp = strlen(a:line) - strlen(line2)
  105. let line3 = substitute(line2, a:c, "", "g")
  106. let closep = strlen(line2) - strlen(line3)
  107. return openp - closep
  108. endfunction
  109. function! s:Get_matching_brace(linenr, o, c, delbrace)
  110. let line = SanitizeRLine(getline(a:linenr))
  111. if a:delbrace == 1
  112. let line = substitute(line, '{$', "", "")
  113. endif
  114. let pb = s:Get_paren_balance(line, a:o, a:c)
  115. let i = a:linenr
  116. while pb != 0 && i > 1
  117. let i -= 1
  118. let pb += s:Get_paren_balance(SanitizeRLine(getline(i)), a:o, a:c)
  119. endwhile
  120. return i
  121. endfunction
  122. " This function is buggy because there 'if's without 'else'
  123. " It must be rewritten relying more on indentation
  124. function! s:Get_matching_if(linenr, delif)
  125. let line = SanitizeRLine(getline(a:linenr))
  126. if a:delif
  127. let line = substitute(line, "if", "", "g")
  128. endif
  129. let elsenr = 0
  130. let i = a:linenr
  131. let ifhere = 0
  132. while i > 0
  133. let line2 = substitute(line, '\<else\>', "xxx", "g")
  134. let elsenr += strlen(line) - strlen(line2)
  135. if line =~ '.*\s*if\s*()' || line =~ '.*\s*if\s*()'
  136. let elsenr -= 1
  137. if elsenr == 0
  138. let ifhere = i
  139. break
  140. endif
  141. endif
  142. let i -= 1
  143. let line = SanitizeRLine(getline(i))
  144. endwhile
  145. if ifhere
  146. return ifhere
  147. else
  148. return a:linenr
  149. endif
  150. endfunction
  151. function! s:Get_last_paren_idx(line, o, c, pb)
  152. let blc = a:pb
  153. let line = substitute(a:line, '\t', s:curtabstop, "g")
  154. let theidx = -1
  155. let llen = strlen(line)
  156. let idx = 0
  157. while idx < llen
  158. if line[idx] == a:o
  159. let blc -= 1
  160. if blc == 0
  161. let theidx = idx
  162. endif
  163. else
  164. if line[idx] == a:c
  165. let blc += 1
  166. endif
  167. endif
  168. let idx += 1
  169. endwhile
  170. return theidx + 1
  171. endfunction
  172. " Get previous relevant line. Search back until getting a line that isn't
  173. " comment or blank
  174. function s:Get_prev_line(lineno)
  175. let lnum = a:lineno - 1
  176. let data = getline( lnum )
  177. while lnum > 0 && (data =~ '^\s*#' || data =~ '^\s*$')
  178. let lnum = lnum - 1
  179. let data = getline( lnum )
  180. endwhile
  181. return lnum
  182. endfunction
  183. " This function is also used by r-plugin/common_global.vim
  184. " Delete from '#' to the end of the line, unless the '#' is inside a string.
  185. function SanitizeRLine(line)
  186. let newline = s:RDelete_quotes(a:line)
  187. let newline = s:RDelete_parens(newline)
  188. let newline = substitute(newline, '#.*', "", "")
  189. let newline = substitute(newline, '\s*$', "", "")
  190. if &filetype == "rhelp" && newline =~ '^\\method{.*}{.*}(.*'
  191. let newline = substitute(newline, '^\\method{\(.*\)}{.*}', '\1', "")
  192. endif
  193. return newline
  194. endfunction
  195. function GetRIndent()
  196. let clnum = line(".") " current line
  197. let cline = getline(clnum)
  198. if cline =~ '^\s*#'
  199. if g:r_indent_ess_comments == 1
  200. if cline =~ '^\s*###'
  201. return 0
  202. endif
  203. if cline !~ '^\s*##'
  204. return g:r_indent_comment_column
  205. endif
  206. endif
  207. endif
  208. let cline = SanitizeRLine(cline)
  209. if cline =~ '^\s*}'
  210. let indline = s:Get_matching_brace(clnum, '{', '}', 1)
  211. if indline > 0 && indline != clnum
  212. let iline = SanitizeRLine(getline(indline))
  213. if s:Get_paren_balance(iline, "(", ")") == 0 || iline =~ '(\s*{$'
  214. return indent(indline)
  215. else
  216. let indline = s:Get_matching_brace(indline, '(', ')', 1)
  217. return indent(indline)
  218. endif
  219. endif
  220. endif
  221. if cline =~ '^\s*)$'
  222. let indline = s:Get_matching_brace(clnum, '(', ')', 1)
  223. return indent(indline)
  224. endif
  225. " Find the first non blank line above the current line
  226. let lnum = s:Get_prev_line(clnum)
  227. " Hit the start of the file, use zero indent.
  228. if lnum == 0
  229. return 0
  230. endif
  231. let line = SanitizeRLine(getline(lnum))
  232. if &filetype == "rhelp"
  233. if cline =~ '^\\dontshow{' || cline =~ '^\\dontrun{' || cline =~ '^\\donttest{' || cline =~ '^\\testonly{'
  234. return 0
  235. endif
  236. if line =~ '^\\examples{' || line =~ '^\\usage{' || line =~ '^\\dontshow{' || line =~ '^\\dontrun{' || line =~ '^\\donttest{' || line =~ '^\\testonly{'
  237. return 0
  238. endif
  239. endif
  240. if &filetype == "rnoweb" && line =~ "^<<.*>>="
  241. return 0
  242. endif
  243. if cline =~ '^\s*{' && s:Get_paren_balance(cline, '{', '}') > 0
  244. if g:r_indent_ess_compatible && line =~ ')$'
  245. let nlnum = lnum
  246. let nline = line
  247. while s:Get_paren_balance(nline, '(', ')') < 0
  248. let nlnum = s:Get_prev_line(nlnum)
  249. let nline = SanitizeRLine(getline(nlnum)) . nline
  250. endwhile
  251. if nline =~ '^\s*function\s*(' && indent(nlnum) == shiftwidth()
  252. return 0
  253. endif
  254. endif
  255. if s:Get_paren_balance(line, "(", ")") == 0
  256. return indent(lnum)
  257. endif
  258. endif
  259. " line is an incomplete command:
  260. if line =~ '\<\(if\|while\|for\|function\)\s*()$' || line =~ '\<else$' || line =~ '<-$' || line =~ '->$'
  261. return indent(lnum) + shiftwidth()
  262. endif
  263. " Deal with () and []
  264. let pb = s:Get_paren_balance(line, '(', ')')
  265. if line =~ '^\s*{$' || line =~ '(\s*{' || (pb == 0 && (line =~ '{$' || line =~ '(\s*{$'))
  266. return indent(lnum) + shiftwidth()
  267. endif
  268. let s:curtabstop = repeat(' ', &tabstop)
  269. if g:r_indent_align_args == 1
  270. if pb > 0 && line =~ '{$'
  271. return s:Get_last_paren_idx(line, '(', ')', pb) + shiftwidth()
  272. endif
  273. let bb = s:Get_paren_balance(line, '[', ']')
  274. if pb > 0
  275. if &filetype == "rhelp"
  276. let ind = s:Get_last_paren_idx(line, '(', ')', pb)
  277. else
  278. let ind = s:Get_last_paren_idx(getline(lnum), '(', ')', pb)
  279. endif
  280. return ind
  281. endif
  282. if pb < 0 && line =~ '.*[,&|\-\*+<>]$'
  283. let lnum = s:Get_prev_line(lnum)
  284. while pb < 1 && lnum > 0
  285. let line = SanitizeRLine(getline(lnum))
  286. let line = substitute(line, '\t', s:curtabstop, "g")
  287. let ind = strlen(line)
  288. while ind > 0
  289. if line[ind] == ')'
  290. let pb -= 1
  291. else
  292. if line[ind] == '('
  293. let pb += 1
  294. endif
  295. endif
  296. if pb == 1
  297. return ind + 1
  298. endif
  299. let ind -= 1
  300. endwhile
  301. let lnum -= 1
  302. endwhile
  303. return 0
  304. endif
  305. if bb > 0
  306. let ind = s:Get_last_paren_idx(getline(lnum), '[', ']', bb)
  307. return ind
  308. endif
  309. endif
  310. let post_block = 0
  311. if line =~ '}$' && s:Get_paren_balance(line, '{', '}') < 0
  312. let lnum = s:Get_matching_brace(lnum, '{', '}', 0)
  313. let line = SanitizeRLine(getline(lnum))
  314. if lnum > 0 && line =~ '^\s*{'
  315. let lnum = s:Get_prev_line(lnum)
  316. let line = SanitizeRLine(getline(lnum))
  317. endif
  318. let pb = s:Get_paren_balance(line, '(', ')')
  319. let post_block = 1
  320. endif
  321. " Indent after operator pattern
  322. let olnum = s:Get_prev_line(lnum)
  323. let oline = getline(olnum)
  324. if olnum > 0
  325. if line =~ g:r_indent_op_pattern && s:Get_paren_balance(line, "(", ")") == 0
  326. if oline =~ g:r_indent_op_pattern && s:Get_paren_balance(line, "(", ")") == 0
  327. return indent(lnum)
  328. else
  329. return indent(lnum) + shiftwidth()
  330. endif
  331. else
  332. if oline =~ g:r_indent_op_pattern && s:Get_paren_balance(line, "(", ")") == 0
  333. return indent(lnum) - shiftwidth()
  334. endif
  335. endif
  336. endif
  337. let post_fun = 0
  338. if pb < 0 && line !~ ')\s*[,&|\-\*+<>]$'
  339. let post_fun = 1
  340. while pb < 0 && lnum > 0
  341. let lnum -= 1
  342. let linepiece = SanitizeRLine(getline(lnum))
  343. let pb += s:Get_paren_balance(linepiece, "(", ")")
  344. let line = linepiece . line
  345. endwhile
  346. if line =~ '{$' && post_block == 0
  347. return indent(lnum) + shiftwidth()
  348. endif
  349. " Now we can do some tests again
  350. if cline =~ '^\s*{'
  351. return indent(lnum)
  352. endif
  353. if post_block == 0
  354. let newl = SanitizeRLine(line)
  355. if newl =~ '\<\(if\|while\|for\|function\)\s*()$' || newl =~ '\<else$' || newl =~ '<-$'
  356. return indent(lnum) + shiftwidth()
  357. endif
  358. endif
  359. endif
  360. if cline =~ '^\s*else'
  361. if line =~ '<-\s*if\s*()'
  362. return indent(lnum) + shiftwidth()
  363. else
  364. if line =~ '\<if\s*()'
  365. return indent(lnum)
  366. else
  367. return indent(lnum) - shiftwidth()
  368. endif
  369. endif
  370. endif
  371. let bb = s:Get_paren_balance(line, '[', ']')
  372. if bb < 0 && line =~ '.*]'
  373. while bb < 0 && lnum > 0
  374. let lnum -= 1
  375. let linepiece = SanitizeRLine(getline(lnum))
  376. let bb += s:Get_paren_balance(linepiece, "[", "]")
  377. let line = linepiece . line
  378. endwhile
  379. let line = s:RDelete_parens(line)
  380. endif
  381. let plnum = s:Get_prev_line(lnum)
  382. let ppost_else = 0
  383. if plnum > 0
  384. let pline = SanitizeRLine(getline(plnum))
  385. let ppost_block = 0
  386. if pline =~ '}$'
  387. let ppost_block = 1
  388. let plnum = s:Get_matching_brace(plnum, '{', '}', 0)
  389. let pline = SanitizeRLine(getline(plnum))
  390. if pline =~ '^\s*{$' && plnum > 0
  391. let plnum = s:Get_prev_line(plnum)
  392. let pline = SanitizeRLine(getline(plnum))
  393. endif
  394. endif
  395. if pline =~ 'else$'
  396. let ppost_else = 1
  397. let plnum = s:Get_matching_if(plnum, 0)
  398. let pline = SanitizeRLine(getline(plnum))
  399. endif
  400. if pline =~ '^\s*else\s*if\s*('
  401. let pplnum = s:Get_prev_line(plnum)
  402. let ppline = SanitizeRLine(getline(pplnum))
  403. while ppline =~ '^\s*else\s*if\s*(' || ppline =~ '^\s*if\s*()\s*\S$'
  404. let plnum = pplnum
  405. let pline = ppline
  406. let pplnum = s:Get_prev_line(plnum)
  407. let ppline = SanitizeRLine(getline(pplnum))
  408. endwhile
  409. while ppline =~ '\<\(if\|while\|for\|function\)\s*()$' || ppline =~ '\<else$' || ppline =~ '<-$'
  410. let plnum = pplnum
  411. let pline = ppline
  412. let pplnum = s:Get_prev_line(plnum)
  413. let ppline = SanitizeRLine(getline(pplnum))
  414. endwhile
  415. endif
  416. let ppb = s:Get_paren_balance(pline, '(', ')')
  417. if ppb < 0 && (pline =~ ')\s*{$' || pline =~ ')$')
  418. while ppb < 0 && plnum > 0
  419. let plnum -= 1
  420. let linepiece = SanitizeRLine(getline(plnum))
  421. let ppb += s:Get_paren_balance(linepiece, "(", ")")
  422. let pline = linepiece . pline
  423. endwhile
  424. let pline = s:RDelete_parens(pline)
  425. endif
  426. endif
  427. let ind = indent(lnum)
  428. if g:r_indent_align_args == 0 && pb != 0
  429. let ind += pb * shiftwidth()
  430. return ind
  431. endif
  432. if g:r_indent_align_args == 0 && bb != 0
  433. let ind += bb * shiftwidth()
  434. return ind
  435. endif
  436. if plnum > 0
  437. let pind = indent(plnum)
  438. else
  439. let pind = 0
  440. endif
  441. if ind == pind || (ind == (pind + shiftwidth()) && pline =~ '{$' && ppost_else == 0)
  442. return ind
  443. endif
  444. let pline = getline(plnum)
  445. let pbb = s:Get_paren_balance(pline, '[', ']')
  446. while pind < ind && plnum > 0 && ppb == 0 && pbb == 0
  447. let ind = pind
  448. let plnum = s:Get_prev_line(plnum)
  449. let pline = getline(plnum)
  450. let ppb = s:Get_paren_balance(pline, '(', ')')
  451. let pbb = s:Get_paren_balance(pline, '[', ']')
  452. while pline =~ '^\s*else'
  453. let plnum = s:Get_matching_if(plnum, 1)
  454. let pline = getline(plnum)
  455. let ppb = s:Get_paren_balance(pline, '(', ')')
  456. let pbb = s:Get_paren_balance(pline, '[', ']')
  457. endwhile
  458. let pind = indent(plnum)
  459. if ind == (pind + shiftwidth()) && pline =~ '{$'
  460. return ind
  461. endif
  462. endwhile
  463. return ind
  464. endfunction
  465. let &cpo = s:cpo_save
  466. unlet s:cpo_save
  467. " vim: sw=2