r.vim 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. " Vim indent file
  2. " Language: R
  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 Oct 08 10:45AM
  7. " 2024 Feb 19 by Vim Project (announce adoption)
  8. " Only load this indent file when no other was loaded.
  9. if exists("b:did_indent")
  10. finish
  11. endif
  12. let b:did_indent = 1
  13. setlocal indentkeys=0{,0},:,!^F,o,O,e
  14. setlocal indentexpr=GetRIndent()
  15. setlocal autoindent
  16. let b:undo_indent = "setl inde< indk<"
  17. " Only define the function once.
  18. if exists("*GetRIndent")
  19. finish
  20. endif
  21. let s:cpo_save = &cpo
  22. set cpo&vim
  23. " Options to make the indentation more similar to Emacs/ESS:
  24. let g:r_indent_align_args = get(g:, 'r_indent_align_args', 1)
  25. let g:r_indent_ess_comments = get(g:, 'r_indent_ess_comments', 0)
  26. let g:r_indent_comment_column = get(g:, 'r_indent_comment_column', 40)
  27. let g:r_indent_ess_compatible = get(g:, 'r_indent_ess_compatible', 0)
  28. let g:r_indent_op_pattern = get(g:, 'r_indent_op_pattern',
  29. \ '\(&\||\|+\|-\|\*\|/\|=\|\~\|%\|->\||>\)\s*$')
  30. function s:RDelete_quotes(line)
  31. let i = 0
  32. let j = 0
  33. let line1 = ""
  34. let llen = strlen(a:line)
  35. while i < llen
  36. if a:line[i] == '"'
  37. let i += 1
  38. let line1 = line1 . 's'
  39. while !(a:line[i] == '"' && ((i > 1 && a:line[i-1] == '\' && a:line[i-2] == '\') || a:line[i-1] != '\')) && i < llen
  40. let i += 1
  41. endwhile
  42. if a:line[i] == '"'
  43. let i += 1
  44. endif
  45. elseif a:line[i] == "'"
  46. let i += 1
  47. let line1 = line1 . 's'
  48. while !(a:line[i] == "'" && ((i > 1 && a:line[i-1] == '\' && a:line[i-2] == '\') || a:line[i-1] != '\')) && i < llen
  49. let i += 1
  50. endwhile
  51. if a:line[i] == "'"
  52. let i += 1
  53. endif
  54. elseif a:line[i] == "`"
  55. let i += 1
  56. let line1 = line1 . 's'
  57. while a:line[i] != "`" && i < llen
  58. let i += 1
  59. endwhile
  60. if a:line[i] == "`"
  61. let i += 1
  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. elseif a:line[i] == '('
  91. let nop += 1
  92. endif
  93. endwhile
  94. let line1 = line1 . a:line[i]
  95. endif
  96. let i += 1
  97. endwhile
  98. return line1
  99. endfunction
  100. function s:Get_paren_balance(line, o, c)
  101. let line2 = substitute(a:line, a:o, "", "g")
  102. let openp = strlen(a:line) - strlen(line2)
  103. let line3 = substitute(line2, a:c, "", "g")
  104. let closep = strlen(line2) - strlen(line3)
  105. return openp - closep
  106. endfunction
  107. function s:Get_matching_brace(linenr, o, c, delbrace)
  108. let line = SanitizeRLine(getline(a:linenr))
  109. if a:delbrace == 1
  110. let line = substitute(line, '{$', "", "")
  111. endif
  112. let pb = s:Get_paren_balance(line, a:o, a:c)
  113. let i = a:linenr
  114. while pb != 0 && i > 1
  115. let i -= 1
  116. let pb += s:Get_paren_balance(SanitizeRLine(getline(i)), a:o, a:c)
  117. endwhile
  118. return i
  119. endfunction
  120. " This function is buggy because there 'if's without 'else'
  121. " It must be rewritten relying more on indentation
  122. function s:Get_matching_if(linenr, delif)
  123. let line = SanitizeRLine(getline(a:linenr))
  124. if a:delif
  125. let line = substitute(line, "if", "", "g")
  126. endif
  127. let elsenr = 0
  128. let i = a:linenr
  129. let ifhere = 0
  130. while i > 0
  131. let line2 = substitute(line, '\<else\>', "xxx", "g")
  132. let elsenr += strlen(line) - strlen(line2)
  133. if line =~ '.*\s*if\s*()' || line =~ '.*\s*if\s*()'
  134. let elsenr -= 1
  135. if elsenr == 0
  136. let ifhere = i
  137. break
  138. endif
  139. endif
  140. let i -= 1
  141. let line = SanitizeRLine(getline(i))
  142. endwhile
  143. if ifhere
  144. return ifhere
  145. else
  146. return a:linenr
  147. endif
  148. endfunction
  149. function s:Get_last_paren_idx(line, o, c, pb)
  150. let blc = a:pb
  151. let line = substitute(a:line, '\t', s:curtabstop, "g")
  152. let theidx = -1
  153. let llen = strlen(line)
  154. let idx = 0
  155. while idx < llen
  156. if line[idx] == a:o
  157. let blc -= 1
  158. if blc == 0
  159. let theidx = idx
  160. endif
  161. elseif line[idx] == a:c
  162. let blc += 1
  163. endif
  164. let idx += 1
  165. endwhile
  166. return theidx + 1
  167. endfunction
  168. " Get previous relevant line. Search back until getting a line that isn't
  169. " comment or blank
  170. function s:Get_prev_line(lineno)
  171. let lnum = a:lineno - 1
  172. let data = getline( lnum )
  173. while lnum > 0 && (data =~ '^\s*#' || data =~ '^\s*$')
  174. let lnum = lnum - 1
  175. let data = getline( lnum )
  176. endwhile
  177. return lnum
  178. endfunction
  179. " This function is also used by r-plugin/common_global.vim
  180. " Delete from '#' to the end of the line, unless the '#' is inside a string.
  181. function SanitizeRLine(line)
  182. let newline = s:RDelete_quotes(a:line)
  183. let newline = s:RDelete_parens(newline)
  184. let newline = substitute(newline, '#.*', "", "")
  185. let newline = substitute(newline, '\s*$', "", "")
  186. if &filetype == "rhelp" && newline =~ '^\\method{.*}{.*}(.*'
  187. let newline = substitute(newline, '^\\method{\(.*\)}{.*}', '\1', "")
  188. endif
  189. return newline
  190. endfunction
  191. function GetRIndent()
  192. let clnum = line(".") " current line
  193. let cline = getline(clnum)
  194. if cline =~ '^\s*#'
  195. if g:r_indent_ess_comments == 1
  196. if cline =~ '^\s*###'
  197. return 0
  198. endif
  199. if cline !~ '^\s*##'
  200. return g:r_indent_comment_column
  201. endif
  202. endif
  203. endif
  204. let cline = SanitizeRLine(cline)
  205. if cline =~ '^\s*}'
  206. let indline = s:Get_matching_brace(clnum, '{', '}', 1)
  207. if indline > 0 && indline != clnum
  208. let iline = SanitizeRLine(getline(indline))
  209. if s:Get_paren_balance(iline, "(", ")") == 0 || iline =~ '(\s*{$'
  210. return indent(indline)
  211. else
  212. let indline = s:Get_matching_brace(indline, '(', ')', 1)
  213. return indent(indline)
  214. endif
  215. endif
  216. endif
  217. if cline =~ '^\s*)$'
  218. let indline = s:Get_matching_brace(clnum, '(', ')', 1)
  219. return indent(indline)
  220. endif
  221. " Find the first non blank line above the current line
  222. let lnum = s:Get_prev_line(clnum)
  223. " Hit the start of the file, use zero indent.
  224. if lnum == 0
  225. return 0
  226. endif
  227. let line = SanitizeRLine(getline(lnum))
  228. if &filetype == "rhelp"
  229. if cline =~ '^\\dontshow{' || cline =~ '^\\dontrun{' || cline =~ '^\\donttest{' || cline =~ '^\\testonly{'
  230. return 0
  231. endif
  232. if line =~ '^\\examples{' || line =~ '^\\usage{' || line =~ '^\\dontshow{' || line =~ '^\\dontrun{' || line =~ '^\\donttest{' || line =~ '^\\testonly{'
  233. return 0
  234. endif
  235. endif
  236. if &filetype == "rnoweb" && line =~ "^<<.*>>="
  237. return 0
  238. endif
  239. if cline =~ '^\s*{' && s:Get_paren_balance(cline, '{', '}') > 0
  240. if g:r_indent_ess_compatible && line =~ ')$'
  241. let nlnum = lnum
  242. let nline = line
  243. while s:Get_paren_balance(nline, '(', ')') < 0
  244. let nlnum = s:Get_prev_line(nlnum)
  245. let nline = SanitizeRLine(getline(nlnum)) . nline
  246. endwhile
  247. if nline =~ '^\s*function\s*(' && indent(nlnum) == shiftwidth()
  248. return 0
  249. endif
  250. endif
  251. if s:Get_paren_balance(line, "(", ")") == 0
  252. return indent(lnum)
  253. endif
  254. endif
  255. " line is an incomplete command:
  256. if line =~ '\<\(if\|while\|for\|function\)\s*()$' || line =~ '\<else$' || line =~ '<-$' || line =~ '->$'
  257. return indent(lnum) + shiftwidth()
  258. endif
  259. " Deal with () and []
  260. let pb = s:Get_paren_balance(line, '(', ')')
  261. if line =~ '^\s*{$' || line =~ '(\s*{' || (pb == 0 && (line =~ '{$' || line =~ '(\s*{$'))
  262. return indent(lnum) + shiftwidth()
  263. endif
  264. let s:curtabstop = repeat(' ', &tabstop)
  265. if g:r_indent_align_args == 1
  266. if pb > 0 && line =~ '{$'
  267. return s:Get_last_paren_idx(line, '(', ')', pb) + shiftwidth()
  268. endif
  269. let bb = s:Get_paren_balance(line, '[', ']')
  270. if pb > 0
  271. if &filetype == "rhelp"
  272. let ind = s:Get_last_paren_idx(line, '(', ')', pb)
  273. else
  274. let ind = s:Get_last_paren_idx(getline(lnum), '(', ')', pb)
  275. endif
  276. return ind
  277. endif
  278. if pb < 0 && line =~ '.*[,&|\-\*+<>]$'
  279. if line =~ '.*[\-\*+>]$'
  280. let is_op = v:true
  281. else
  282. let is_op = v:false
  283. endif
  284. let lnum = s:Get_prev_line(lnum)
  285. while pb < 1 && lnum > 0
  286. let line = SanitizeRLine(getline(lnum))
  287. let line = substitute(line, '\t', s:curtabstop, "g")
  288. let ind = strlen(line)
  289. while ind > 0
  290. if line[ind] == ')'
  291. let pb -= 1
  292. elseif line[ind] == '('
  293. let pb += 1
  294. if is_op && pb == 0
  295. return indent(lnum)
  296. endif
  297. endif
  298. if pb == 1
  299. return ind + 1
  300. endif
  301. let ind -= 1
  302. endwhile
  303. let lnum -= 1
  304. endwhile
  305. return 0
  306. endif
  307. if bb > 0
  308. let ind = s:Get_last_paren_idx(getline(lnum), '[', ']', bb)
  309. return ind
  310. endif
  311. endif
  312. let post_block = 0
  313. if line =~ '}$' && s:Get_paren_balance(line, '{', '}') < 0
  314. let lnum = s:Get_matching_brace(lnum, '{', '}', 0)
  315. let line = SanitizeRLine(getline(lnum))
  316. if lnum > 0 && line =~ '^\s*{'
  317. let lnum = s:Get_prev_line(lnum)
  318. let line = SanitizeRLine(getline(lnum))
  319. endif
  320. let pb = s:Get_paren_balance(line, '(', ')')
  321. let post_block = 1
  322. endif
  323. " Indent after operator pattern
  324. let olnum = s:Get_prev_line(lnum)
  325. let oline = getline(olnum)
  326. if olnum > 0
  327. if substitute(line, '#.*', '', '') =~ g:r_indent_op_pattern && s:Get_paren_balance(line, "(", ")") == 0
  328. if substitute(oline, '#.*', '', '') =~ g:r_indent_op_pattern && s:Get_paren_balance(line, "(", ")") == 0
  329. return indent(lnum)
  330. else
  331. return indent(lnum) + shiftwidth()
  332. endif
  333. elseif substitute(oline, '#.*', '', '') =~ g:r_indent_op_pattern && s:Get_paren_balance(line, "(", ")") == 0
  334. return indent(lnum) - shiftwidth()
  335. endif
  336. elseif substitute(line, '#.*', '', '') =~ g:r_indent_op_pattern && s:Get_paren_balance(line, "(", ")") == 0
  337. return indent(lnum) + shiftwidth()
  338. endif
  339. let post_fun = 0
  340. if pb < 0 && line !~ ')\s*[,&|\-\*+<>]$'
  341. let post_fun = 1
  342. while pb < 0 && lnum > 0
  343. let lnum -= 1
  344. let linepiece = SanitizeRLine(getline(lnum))
  345. let pb += s:Get_paren_balance(linepiece, "(", ")")
  346. let line = linepiece . line
  347. endwhile
  348. if line =~ '{$' && post_block == 0
  349. return indent(lnum) + shiftwidth()
  350. endif
  351. " Now we can do some tests again
  352. if cline =~ '^\s*{'
  353. return indent(lnum)
  354. endif
  355. if post_block == 0
  356. let newl = SanitizeRLine(line)
  357. if newl =~ '\<\(if\|while\|for\|function\)\s*()$' || newl =~ '\<else$' || newl =~ '<-$'
  358. return indent(lnum) + shiftwidth()
  359. endif
  360. endif
  361. endif
  362. if cline =~ '^\s*else'
  363. if line =~ '<-\s*if\s*()'
  364. return indent(lnum) + shiftwidth()
  365. elseif line =~ '\<if\s*()'
  366. return indent(lnum)
  367. else
  368. return indent(lnum) - shiftwidth()
  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