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