robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
3222f0ad00
master
1" Vim indent file 2" Language: Erlang (http://www.erlang.org) 3" Author: Csaba Hoch <csaba.hoch@gmail.com> 4" Contributors: Edwin Fine <efine145_nospam01 at usa dot net> 5" Pawel 'kTT' Salata <rockplayer.pl@gmail.com> 6" Ricardo Catalinas Jiménez <jimenezrick@gmail.com> 7" Last Update: 2022-Sep-06 8" License: Vim license 9" URL: https://github.com/vim-erlang/vim-erlang-runtime 10 11" Note About Usage: 12" This indentation script works best with the Erlang syntax file created by 13" Kreąimir Marľić (Kresimir Marzic) and maintained by Csaba Hoch. 14 15" Notes About Implementation: 16" 17" - LTI = Line to indent. 18" - The index of the first line is 1, but the index of the first column is 0. 19 20 21" Initialization {{{1 22" ============== 23 24" Only load this indent file when no other was loaded 25" Vim 7 or later is needed 26if exists ( "b:did_indent" ) || version < 700 27finish 28else 29let b: did_indent = 1 30endif 31 32setlocal indentexpr = ErlangIndent () 33setlocal indentkeys += 0=end,0=of,0=catch,0=after,0=else,0=when,0=),0=],0=},0=>> 34 35let b: undo_indent = "setl inde< indk<" 36 37" Only define the functions once 38if exists ( "*ErlangIndent" ) 39finish 40endif 41 42let s: cpo_save = & cpo 43set cpo & vim 44 45" Logging library {{{1 46" =============== 47 48" Purpose: 49" Logs the given string using the ErlangIndentLog function if it exists. 50" Parameters: 51" s: string 52function ! s: Log ( s ) 53if exists ( "*ErlangIndentLog" ) 54call ErlangIndentLog ( a: s ) 55endif 56endfunction 57 58" Line tokenizer library {{{1 59" ====================== 60 61" Indtokens are "indentation tokens". See their exact format in the 62" documentation of the s:GetTokensFromLine function. 63 64" Purpose: 65" Calculate the new virtual column after the given segment of a line. 66" Parameters: 67" line: string 68" first_index: integer -- the index of the first character of the segment 69" last_index: integer -- the index of the last character of the segment 70" vcol: integer -- the virtual column of the first character of the token 71" tabstop: integer -- the value of the 'tabstop' option to be used 72" Returns: 73" vcol: integer 74" Example: 75" " index: 0 12 34567 76" " vcol: 0 45 89 77" s:CalcVCol("\t'\tx', b", 1, 4, 4) -> 10 78function ! s: CalcVCol ( line , first_index , last_index , vcol , tabstop ) 79 80" We copy the relevant segment of the line, otherwise if the line were 81" e.g. `"\t", term` then the else branch below would consume the `", term` 82" part at once. 83let line = a: line [ a: first_index : a: last_index ] 84 85let i = 0 86let last_index = a: last_index - a: first_index 87let vcol = a: vcol 88 89while 0 <= i && i <= last_index 90 91if line [ i ] ==# "\t" 92" Example (when tabstop == 4): 93" 94" vcol + tab -> next_vcol 95" 0 + tab -> 4 96" 1 + tab -> 4 97" 2 + tab -> 4 98" 3 + tab -> 4 99" 4 + tab -> 8 100" 101" next_i - i == the number of tabs 102let next_i = matchend ( line , '\t*' , i + 1 ) 103let vcol = ( vcol / a: tabstop + ( next_i - i )) * a: tabstop 104call s: Log ( 'new vcol after tab: ' . vcol ) 105else 106let next_i = matchend ( line , '[^\t]*' , i + 1 ) 107let vcol += next_i - i 108call s: Log ( 'new vcol after other: ' . vcol ) 109endif 110let i = next_i 111endwhile 112 113return vcol 114endfunction 115 116" Purpose: 117" Go through the whole line and return the tokens in the line. 118" Parameters: 119" line: string -- the line to be examined 120" string_continuation: bool 121" atom_continuation: bool 122" Returns: 123" indtokens = [indtoken] 124" indtoken = [token, vcol, col] 125" token = string (examples: 'begin', '<quoted_atom>', '}') 126" vcol = integer (the virtual column of the first character of the token; 127" counting starts from 0) 128" col = integer (counting starts from 0) 129function ! s: GetTokensFromLine ( line , string_continuation , atom_continuation , 130 \tabstop ) 131 132let linelen = strlen ( a: line ) " The length of the line 133let i = 0 " The index of the current character in the line 134let vcol = 0 " The virtual column of the current character 135let indtokens = [] 136 137if a: string_continuation 138let i = matchend ( a: line , '^\%([^"\\]\|\\.\)*"' , 0 ) 139if i ==# -1 140call s: Log ( ' Whole line is string continuation -> ignore' ) 141return [] 142else 143let vcol = s: CalcVCol ( a: line , 0 , i - 1 , 0 , a: tabstop ) 144call add ( indtokens , [ '<string_end>' , vcol , i ]) 145endif 146elseif a: atom_continuation 147let i = matchend ( a: line , "^\\%([^'\\\\]\\|\\\\.\\)*'" , 0 ) 148if i ==# -1 149call s: Log ( ' Whole line is quoted atom continuation -> ignore' ) 150return [] 151else 152let vcol = s: CalcVCol ( a: line , 0 , i - 1 , 0 , a: tabstop ) 153call add ( indtokens , [ '<quoted_atom_end>' , vcol , i ]) 154endif 155endif 156 157while 0 <= i && i < linelen 158 159let next_vcol = '' 160 161" Spaces 162if a: line [ i ] ==# ' ' 163let next_i = matchend ( a: line , ' *' , i + 1 ) 164 165" Tabs 166elseif a: line [ i ] ==# "\t" 167let next_i = matchend ( a: line , '\t*' , i + 1 ) 168 169" See example in s:CalcVCol 170let next_vcol = ( vcol / a: tabstop + ( next_i - i )) * a: tabstop 171 172" Comment 173elseif a: line [ i ] ==# '%' 174let next_i = linelen 175 176" String token: "..." 177elseif a: line [ i ] ==# '"' 178let next_i = matchend ( a: line , '\%([^"\\]\|\\.\)*"' , i + 1 ) 179if next_i ==# -1 180call add ( indtokens , [ '<string_start>' , vcol , i ]) 181else 182let next_vcol = s: CalcVCol ( a: line , i , next_i - 1 , vcol , a: tabstop ) 183call add ( indtokens , [ '<string>' , vcol , i ]) 184endif 185 186" Quoted atom token: '...' 187elseif a: line [ i ] ==# "'" 188let next_i = matchend ( a: line , "\\%([^'\\\\]\\|\\\\.\\)*'" , i + 1 ) 189if next_i ==# -1 190call add ( indtokens , [ '<quoted_atom_start>' , vcol , i ]) 191else 192let next_vcol = s: CalcVCol ( a: line , i , next_i - 1 , vcol , a: tabstop ) 193call add ( indtokens , [ '<quoted_atom>' , vcol , i ]) 194endif 195 196" Keyword or atom or variable token or number 197elseif a: line [ i ] =~# '[a-zA-Z_@0-9]' 198let next_i = matchend ( a: line , 199 \'[[:alnum:]_@:]*\%(\s*#\s*[[:alnum:]_@:]*\)\=' , 200 \i + 1 ) 201call add ( indtokens , [ a: line [( i ):( next_i - 1 )], vcol , i ]) 202 203" Character token: $<char> (as in: $a) 204elseif a: line [ i ] ==# '$' 205call add ( indtokens , [ '$.' , vcol , i ]) 206let next_i = i + 2 207 208" Dot token: . 209elseif a: line [ i ] ==# '.' 210 211let next_i = i + 1 212 213if i + 1 ==# linelen || a: line [ i + 1 ] =~# '[[:blank:]%]' 214" End of clause token: . (as in: f() -> ok.) 215call add ( indtokens , [ '<end_of_clause>' , vcol , i ]) 216 217else 218" Possibilities: 219" - Dot token in float: . (as in: 3.14) 220" - Dot token in record: . (as in: #myrec.myfield) 221call add ( indtokens , [ '.' , vcol , i ]) 222endif 223 224" Equal sign 225elseif a: line [ i ] ==# '=' 226" This is handled separately so that "=<<" will be parsed as 227" ['=', '<<'] instead of ['=<', '<']. Although Erlang parses it 228" currently in the latter way, that may be fixed some day. 229call add ( indtokens , [ a: line [ i ], vcol , i ]) 230let next_i = i + 1 231 232" Three-character tokens 233elseif i + 1 < linelen && 234 \index ([ '=:=' , '=/=' ], a: line [ i : i + 1 ]) != -1 235call add ( indtokens , [ a: line [ i : i + 1 ], vcol , i ]) 236let next_i = i + 2 237 238" Two-character tokens 239elseif i + 1 < linelen && 240 \index ([ '->' , '<<' , '>>' , '||' , '==' , '/=' , '=<' , '>=' , '?=' , '++' , 241 \'--' , '::' ], 242 \a: line [ i : i + 1 ]) != -1 243call add ( indtokens , [ a: line [ i : i + 1 ], vcol , i ]) 244let next_i = i + 2 245 246" Other character: , ; < > ( ) [ ] { } # + - * / : ? = ! | 247else 248call add ( indtokens , [ a: line [ i ], vcol , i ]) 249let next_i = i + 1 250 251endif 252 253if next_vcol ==# '' 254let vcol += next_i - i 255else 256let vcol = next_vcol 257endif 258 259let i = next_i 260 261endwhile 262 263return indtokens 264 265endfunction 266 267" TODO: doc, handle "not found" case 268function ! s: GetIndtokenAtCol ( indtokens , col ) 269let i = 0 270while i < len ( a: indtokens ) 271if a: indtokens [ i ][ 2 ] ==# a: col 272return [ 1 , i ] 273elseif a: indtokens [ i ][ 2 ] > a: col 274return [ 0 , s: IndentError ( 'No token at col ' . a: col . ', ' . 275 \'indtokens = ' . string ( a: indtokens ), 276 \'' , '' )] 277endif 278let i += 1 279endwhile 280return [ 0 , s: IndentError ( 'No token at col ' . a: col . ', ' . 281 \'indtokens = ' . string ( a: indtokens ), 282 \'' , '' )] 283endfunction 284 285" Stack library {{{1 286" ============= 287 288" Purpose: 289" Push a token onto the parser's stack. 290" Parameters: 291" stack: [token] 292" token: string 293function ! s: Push ( stack , token ) 294call s: Log ( ' Stack Push: "' . a: token . '" into ' . string ( a: stack )) 295call insert ( a: stack , a: token ) 296endfunction 297 298" Purpose: 299" Pop a token from the parser's stack. 300" Parameters: 301" stack: [token] 302" token: string 303" Returns: 304" token: string -- the removed element 305function ! s: Pop ( stack ) 306let head = remove ( a: stack , 0 ) 307call s: Log ( ' Stack Pop: "' . head . '" from ' . string ( a: stack )) 308return head 309endfunction 310 311" Library for accessing and storing tokenized lines {{{1 312" ================================================= 313 314" The Erlang token cache: an `lnum -> indtokens` dictionary that stores the 315" tokenized lines. 316let s: all_tokens = {} 317let s: file_name = '' 318let s: last_changedtick = -1 319 320" Purpose: 321" Clear the Erlang token cache if we have a different file or the file has 322" been changed since the last indentation. 323function ! s: ClearTokenCacheIfNeeded () 324let file_name = expand ( '%:p' ) 325if file_name != s: file_name || 326 \b: changedtick != s: last_changedtick 327let s: file_name = file_name 328let s: last_changedtick = b: changedtick 329let s: all_tokens = {} 330endif 331endfunction 332 333" Purpose: 334" Return the tokens of line `lnum`, if that line is not empty. If it is 335" empty, find the first non-empty line in the given `direction` and return 336" the tokens of that line. 337" Parameters: 338" lnum: integer 339" direction: 'up' | 'down' 340" Returns: 341" result: [] -- the result is an empty list if we hit the beginning or end 342" of the file 343" | [lnum, indtokens] 344" lnum: integer -- the index of the non-empty line that was found and 345" tokenized 346" indtokens: [indtoken] -- the tokens of line `lnum` 347function ! s: TokenizeLine ( lnum , direction ) 348 349call s: Log ( 'Tokenizing starts from line ' . a: lnum ) 350if a: direction ==# 'up' 351let lnum = prevnonblank ( a: lnum ) 352else " a:direction ==# 'down' 353let lnum = nextnonblank ( a: lnum ) 354endif 355 356" We hit the beginning or end of the file 357if lnum ==# 0 358let indtokens = [] 359call s: Log ( ' We hit the beginning or end of the file.' ) 360 361" The line has already been parsed 362elseif has_key ( s: all_tokens , lnum ) 363let indtokens = s: all_tokens [ lnum ] 364call s: Log ( 'Cached line ' . lnum . ': ' . getline ( lnum )) 365call s: Log ( " Tokens in the line:\n - " . join ( indtokens , "\n - " )) 366 367" The line should be parsed now 368else 369 370" Parse the line 371let line = getline ( lnum ) 372let string_continuation = s: IsLineStringContinuation ( lnum ) 373let atom_continuation = s: IsLineAtomContinuation ( lnum ) 374let indtokens = s: GetTokensFromLine ( line , string_continuation , 375 \atom_continuation , & tabstop ) 376let s: all_tokens [ lnum ] = indtokens 377call s: Log ( 'Tokenizing line ' . lnum . ': ' . line ) 378call s: Log ( " Tokens in the line:\n - " . join ( indtokens , "\n - " )) 379 380endif 381 382return [ lnum , indtokens ] 383endfunction 384 385" Purpose: 386" As a helper function for PrevIndToken and NextIndToken, the FindIndToken 387" function finds the first line with at least one token in the given 388" direction. 389" Parameters: 390" lnum: integer 391" direction: 'up' | 'down' 392" Returns: 393" result: [[], 0, 0] 394" -- the result is an empty list if we hit the beginning or end of 395" the file 396" | [indtoken, lnum, i] 397" -- the content, lnum and token index of the next (or previous) 398" indtoken 399function ! s: FindIndToken ( lnum , dir ) 400let lnum = a: lnum 401while 1 402let lnum += ( a: dir ==# 'up' ? -1 : 1 ) 403let [ lnum , indtokens ] = s: TokenizeLine ( lnum , a: dir ) 404if lnum ==# 0 405" We hit the beginning or end of the file 406return [[], 0 , 0 ] 407elseif ! empty ( indtokens ) 408" We found a non-empty line. If we were moving up, we return the last 409" token of this line. Otherwise we return the first token if this line. 410let i = ( a: dir ==# 'up' ? len ( indtokens ) - 1 : 0 ) 411return [ indtokens [ i ], lnum , i ] 412endif 413endwhile 414endfunction 415 416" Purpose: 417" Find the token that directly precedes the given token. 418" Parameters: 419" lnum: integer -- the line of the given token 420" i: the index of the given token within line `lnum` 421" Returns: 422" result = [] -- the result is an empty list if the given token is the first 423" token of the file 424" | indtoken 425function ! s: PrevIndToken ( lnum , i ) 426call s: Log ( ' PrevIndToken called: lnum=' . a: lnum . ', i =' . a: i ) 427 428" If the current line has a previous token, return that 429if a: i > 0 430return [ s: all_tokens [ a: lnum ][ a: i - 1 ], a: lnum , a: i - 1 ] 431else 432return s: FindIndToken ( a: lnum , 'up' ) 433endif 434endfunction 435 436" Purpose: 437" Find the token that directly succeeds the given token. 438" Parameters: 439" lnum: integer -- the line of the given token 440" i: the index of the given token within line `lnum` 441" Returns: 442" result = [] -- the result is an empty list if the given token is the last 443" token of the file 444" | indtoken 445function ! s: NextIndToken ( lnum , i ) 446call s: Log ( ' NextIndToken called: lnum=' . a: lnum . ', i =' . a: i ) 447 448" If the current line has a next token, return that 449if len ( s: all_tokens [ a: lnum ]) > a: i + 1 450return [ s: all_tokens [ a: lnum ][ a: i + 1 ], a: lnum , a: i + 1 ] 451else 452return s: FindIndToken ( a: lnum , 'down' ) 453endif 454endfunction 455 456" ErlangCalcIndent helper functions {{{1 457" ================================= 458 459" Purpose: 460" This function is called when the parser encounters a syntax error. 461" 462" If we encounter a syntax error, we return 463" g:erlang_unexpected_token_indent, which is -1 by default. This means that 464" the indentation of the LTI will not be changed. 465" Parameter: 466" msg: string 467" token: string 468" stack: [token] 469" Returns: 470" indent: integer 471function ! s: IndentError ( msg , token , stack ) 472call s: Log ( 'Indent error: ' . a: msg . ' -> return' ) 473call s: Log ( ' Token = ' . a: token . ', ' . 474 \' stack = ' . string ( a: stack )) 475return g: erlang_unexpected_token_indent 476endfunction 477 478" Purpose: 479" This function is called when the parser encounters an unexpected token, 480" and the parser will return the number given back by UnexpectedToken. 481" 482" If we encounter an unexpected token, we return 483" g:erlang_unexpected_token_indent, which is -1 by default. This means that 484" the indentation of the LTI will not be changed. 485" Parameter: 486" token: string 487" stack: [token] 488" Returns: 489" indent: integer 490function ! s: UnexpectedToken ( token , stack ) 491call s: Log ( ' Unexpected token ' . a: token . ', stack = ' . 492 \string ( a: stack ) . ' -> return' ) 493return g: erlang_unexpected_token_indent 494endfunction 495 496if ! exists ( 'g:erlang_unexpected_token_indent' ) 497let g: erlang_unexpected_token_indent = -1 498endif 499 500" Purpose: 501" Return whether the given line starts with a string continuation. 502" Parameter: 503" lnum: integer 504" Returns: 505" result: bool 506" Example: 507" f() -> % IsLineStringContinuation = false 508" "This is a % IsLineStringContinuation = false 509" multiline % IsLineStringContinuation = true 510" string". % IsLineStringContinuation = true 511function ! s: IsLineStringContinuation ( lnum ) 512if has ( 'syntax_items' ) 513return synIDattr ( synID ( a: lnum , 1 , 0 ), 'name' ) =~# '^erlangString' 514else 515return 0 516endif 517endfunction 518 519" Purpose: 520" Return whether the given line starts with an atom continuation. 521" Parameter: 522" lnum: integer 523" Returns: 524" result: bool 525" Example: 526" 'function with % IsLineAtomContinuation = true, but should be false 527" weird name'() -> % IsLineAtomContinuation = true 528" ok. % IsLineAtomContinuation = false 529function ! s: IsLineAtomContinuation ( lnum ) 530if has ( 'syntax_items' ) 531let syn_name = synIDattr ( synID ( a: lnum , 1 , 0 ), 'name' ) 532return syn_name =~# '^erlangQuotedAtom' || 533 \syn_name =~# '^erlangQuotedRecord' 534else 535return 0 536endif 537endfunction 538 539" Purpose: 540" Return whether the 'catch' token (which should be the `i`th token in line 541" `lnum`) is standalone or part of a try-catch block, based on the preceding 542" token. 543" Parameters: 544" lnum: integer 545" i: integer 546" Return: 547" is_standalone: bool 548function ! s: IsCatchStandalone ( lnum , i ) 549call s: Log ( ' IsCatchStandalone called: lnum=' . a: lnum . ', i=' . a: i ) 550let [ prev_indtoken , _ , _ ] = s: PrevIndToken ( a: lnum , a: i ) 551 552" If we hit the beginning of the file, it is not a catch in a try block 553if prev_indtoken == [] 554return 1 555endif 556 557let prev_token = prev_indtoken [ 0 ] 558 559if prev_token =~# '^[A-Z_@0-9]' 560let is_standalone = 0 561elseif prev_token =~# '[a-z]' 562if index ([ 'after' , 'and' , 'andalso' , 'band' , 'begin' , 'bnot' , 'bor' , 'bsl' , 563 \'bsr' , 'bxor' , 'case' , 'catch' , 'div' , 'maybe' , 'not' , 'or' , 564 \'orelse' , 'rem' , 'try' , 'xor' ], prev_token ) != -1 565" If catch is after these keywords, it is standalone 566let is_standalone = 1 567else 568" If catch is after another keyword (e.g. 'end') or an atom, it is 569" part of try-catch. 570" 571" Keywords: 572" - may precede 'catch': end 573" - may not precede 'catch': else fun if of receive when 574" - unused: cond let query 575let is_standalone = 0 576endif 577elseif index ([ ')' , ']' , '}' , '<string>' , '<string_end>' , '<quoted_atom>' , 578 \'<quoted_atom_end>' , '$.' ], prev_token ) != -1 579let is_standalone = 0 580else 581" This 'else' branch includes the following tokens: 582" -> == /= =< < >= > ?= =:= =/= + - * / ++ -- :: < > ; ( [ { ? = ! . | 583let is_standalone = 1 584endif 585 586call s: Log ( ' "catch" preceded by "' . prev_token . '" -> catch ' . 587 \( is_standalone ? 'is standalone' : 'belongs to try-catch' )) 588return is_standalone 589 590endfunction 591 592" Purpose: 593" This function is called when a begin-type element ('begin', 'case', 594" '[', '<<', etc.) is found. It asks the caller to return if the stack 595" if already empty. 596" Parameters: 597" stack: [token] 598" token: string 599" curr_vcol: integer 600" stored_vcol: integer 601" sw: integer -- number of spaces to be used after the begin element as 602" indentation 603" Returns: 604" result: [should_return, indent] 605" should_return: bool -- if true, the caller should return `indent` to Vim 606" indent -- integer 607function ! s: BeginElementFoundIfEmpty ( stack , token , curr_vcol , stored_vcol , sw ) 608if empty ( a: stack ) 609if a: stored_vcol ==# -1 610call s: Log ( ' "' . a: token . '" directly precedes LTI -> return' ) 611return [ 1 , a: curr_vcol + a: sw ] 612else 613call s: Log ( ' "' . a: token . 614 \'" token (whose expression includes LTI) found -> return' ) 615return [ 1 , a: stored_vcol ] 616endif 617else 618return [ 0 , 0 ] 619endif 620endfunction 621 622" Purpose: 623" This function is called when a begin-type element ('begin', 'case', '[', 624" '<<', etc.) is found, and in some cases when 'after' and 'when' is found. 625" It asks the caller to return if the stack is already empty. 626" Parameters: 627" stack: [token] 628" token: string 629" curr_vcol: integer 630" stored_vcol: integer 631" end_token: end token that belongs to the begin element found (e.g. if the 632" begin element is 'begin', the end token is 'end') 633" sw: integer -- number of spaces to be used after the begin element as 634" indentation 635" Returns: 636" result: [should_return, indent] 637" should_return: bool -- if true, the caller should return `indent` to Vim 638" indent -- integer 639function ! s: BeginElementFound ( stack , token , curr_vcol , stored_vcol , end_token , sw ) 640 641" Return 'return' if the stack is empty 642let [ ret , res ] = s: BeginElementFoundIfEmpty ( a: stack , a: token , a: curr_vcol , 643 \a: stored_vcol , a: sw ) 644if ret |return [ ret , res ] |endif 645 646if a: stack [ 0 ] ==# a: end_token 647call s: Log ( ' "' . a: token . '" pops "' . a: end_token . '"' ) 648call s: Pop ( a: stack ) 649if ! empty ( a: stack ) && a: stack [ 0 ] ==# 'align_to_begin_element' 650call s: Pop ( a: stack ) 651if empty ( a: stack ) 652return [ 1 , a: curr_vcol ] 653else 654return [ 1 , s: UnexpectedToken ( a: token , a: stack )] 655endif 656else 657return [ 0 , 0 ] 658endif 659else 660return [ 1 , s: UnexpectedToken ( a: token , a: stack )] 661endif 662endfunction 663 664" Purpose: 665" This function is called when we hit the beginning of a file or an 666" end-of-clause token -- i.e. when we found the beginning of the current 667" clause. 668" 669" If the stack contains an '->' or 'when', this means that we can return 670" now, since we were looking for the beginning of the clause. 671" Parameters: 672" stack: [token] 673" token: string 674" stored_vcol: integer 675" lnum: the line number of the "end of clause" mark (or 0 if we hit the 676" beginning of the file) 677" i: the index of the "end of clause" token within its own line 678" Returns: 679" result: [should_return, indent] 680" should_return: bool -- if true, the caller should return `indent` to Vim 681" indent -- integer 682function ! s: BeginningOfClauseFound ( stack , token , stored_vcol , lnum , i ) 683if ! empty ( a: stack ) && a: stack [ 0 ] ==# 'when' 684call s: Log ( ' BeginningOfClauseFound: "when" found in stack' ) 685call s: Pop ( a: stack ) 686if empty ( a: stack ) 687call s: Log ( ' Stack is ["when"], so LTI is in a guard -> return' ) 688return [ 1 , a: stored_vcol + shiftwidth () + 2 ] 689else 690return [ 1 , s: UnexpectedToken ( a: token , a: stack )] 691endif 692elseif ! empty ( a: stack ) && a: stack [ 0 ] ==# '->' 693call s: Log ( ' BeginningOfClauseFound: "->" found in stack' ) 694call s: Pop ( a: stack ) 695if empty ( a: stack ) 696call s: Log ( ' Stack is ["->"], so LTI is in function body -> return' ) 697return [ 1 , a: stored_vcol + shiftwidth ()] 698elseif a: stack [ 0 ] ==# ';' 699call s: Pop ( a: stack ) 700 701if ! empty ( a: stack ) 702return [ 1 , s: UnexpectedToken ( a: token , a: stack )] 703endif 704 705if a: lnum ==# 0 706" Set lnum and i to be NextIndToken-friendly 707let lnum = 1 708let i = -1 709else 710let lnum = a: lnum 711let i = a: i 712endif 713 714" Are we after a "-spec func() ...;" clause? 715let [ next1_indtoken , next1_lnum , next1_i ] = s: NextIndToken ( lnum , i ) 716if ! empty ( next1_indtoken ) && next1_indtoken [ 0 ] =~# '-' 717let [ next2_indtoken , next2_lnum , next2_i ] = 718 \s: NextIndToken ( next1_lnum , next1_i ) 719if ! empty ( next2_indtoken ) && next2_indtoken [ 0 ] =~# 'spec' 720let [ next3_indtoken , next3_lnum , next3_i ] = 721 \s: NextIndToken ( next2_lnum , next2_i ) 722if ! empty ( next3_indtoken ) 723let [ next4_indtoken , next4_lnum , next4_i ] = 724 \s: NextIndToken ( next3_lnum , next3_i ) 725if ! empty ( next4_indtoken ) 726" Yes, we are. 727call s: Log ( ' Stack is ["->", ";"], so LTI is in a "-spec" ' . 728 \'attribute -> return' ) 729return [ 1 , next4_indtoken [ 1 ]] 730endif 731endif 732endif 733endif 734 735call s: Log ( ' Stack is ["->", ";"], so LTI is in a function head ' . 736 \'-> return' ) 737return [ 1 , a: stored_vcol ] 738 739else 740return [ 1 , s: UnexpectedToken ( a: token , a: stack )] 741endif 742else 743return [ 0 , 0 ] 744endif 745endfunction 746 747let g: erlang_indent_searchpair_timeout = 2000 748 749" TODO 750function ! s: SearchPair ( lnum , curr_col , start , middle , end ) 751call cursor ( a: lnum , a: curr_col + 1 ) 752let [ lnum_new , col1_new ] = 753 \searchpairpos ( a: start , a: middle , a: end , 'bW' , 754 \'synIDattr(synID(line("."), col("."), 0), "name") ' . 755 \'=~? "string\\|quotedatom\\|todo\\|comment\\|' . 756 \'erlangmodifier"' , 757 \0 , g: erlang_indent_searchpair_timeout ) 758return [ lnum_new , col1_new - 1 ] 759endfunction 760 761function ! s: SearchEndPair ( lnum , curr_col ) 762return s: SearchPair ( 763 \a: lnum , a: curr_col , 764 \'\C\<\%(case\|try\|begin\|receive\|if\|maybe\)\>\|' . 765 \'\<fun\>\%(\s\|\n\|%.*$\|[A-Z_@][a-zA-Z_@]*\)*(' , 766 \'' , 767 \'\<end\>' ) 768endfunction 769 770" ErlangCalcIndent {{{1 771" ================ 772 773" Purpose: 774" Calculate the indentation of the given line. 775" Parameters: 776" lnum: integer -- index of the line for which the indentation should be 777" calculated 778" stack: [token] -- initial stack 779" Return: 780" indent: integer -- if -1, that means "don't change the indentation"; 781" otherwise it means "indent the line with `indent` 782" number of spaces or equivalent tabs" 783function ! s: ErlangCalcIndent ( lnum , stack ) 784let res = s: ErlangCalcIndent2 ( a: lnum , a: stack ) 785call s: Log ( "ErlangCalcIndent returned: " . res ) 786return res 787endfunction 788 789function ! s: ErlangCalcIndent2 ( lnum , stack ) 790 791let lnum = a: lnum 792let stored_vcol = -1 " Virtual column of the first character of the token that 793" we currently think we might align to. 794let mode = 'normal' 795let stack = a: stack 796let semicolon_abscol = '' 797 798" Walk through the lines of the buffer backwards (starting from the 799" previous line) until we can decide how to indent the current line. 800while 1 801 802let [ lnum , indtokens ] = s: TokenizeLine ( lnum , 'up' ) 803 804" Hit the start of the file 805if lnum ==# 0 806let [ ret , res ] = s: BeginningOfClauseFound ( stack , 'beginning_of_file' , 807 \stored_vcol , 0 , 0 ) 808if ret |return res |endif 809 810return 0 811endif 812 813let i = len ( indtokens ) - 1 814let last_token_of_line = 1 815 816while i >= 0 817 818let [ token , curr_vcol , curr_col ] = indtokens [ i ] 819call s: Log ( ' Analyzing the following token: ' . string ( indtokens [ i ])) 820 821if len ( stack ) > 256 " TODO: magic number 822return s: IndentError ( 'Stack too long' , token , stack ) 823endif 824 825if token ==# '<end_of_clause>' 826let [ ret , res ] = s: BeginningOfClauseFound ( stack , token , stored_vcol , 827 \lnum , i ) 828if ret |return res |endif 829 830if stored_vcol ==# -1 831call s: Log ( ' End of clause directly precedes LTI -> return' ) 832return 0 833else 834call s: Log ( ' End of clause (but not end of line) -> return' ) 835return stored_vcol 836endif 837 838elseif stack == [ 'prev_term_plus' ] 839if token =~# '[a-zA-Z_@#]' || 840 \token ==# '<string>' || token ==# '<string_start>' || 841 \token ==# '<quoted_atom>' || token ==# '<quoted_atom_start>' 842call s: Log ( ' previous token found: curr_vcol + plus = ' . 843 \curr_vcol . " + " . plus ) 844return curr_vcol + plus 845endif 846 847elseif token ==# 'begin' 848let [ ret , res ] = s: BeginElementFound ( stack , token , curr_vcol , 849 \stored_vcol , 'end' , shiftwidth ()) 850if ret |return res |endif 851 852" case EXPR of BRANCHES end 853" if BRANCHES end 854" try EXPR catch BRANCHES end 855" try EXPR after BODY end 856" try EXPR catch BRANCHES after BODY end 857" try EXPR of BRANCHES catch BRANCHES end 858" try EXPR of BRANCHES after BODY end 859" try EXPR of BRANCHES catch BRANCHES after BODY end 860" receive BRANCHES end 861" receive BRANCHES after BRANCHES end 862" maybe EXPR end 863" maybe EXPR else BRANCHES end 864 865" This branch is not Emacs-compatible 866elseif ( index ([ 'of' , 'receive' , 'after' , 'if' , 'else' ], token ) != -1 || 867 \( token ==# 'catch' && ! s: IsCatchStandalone ( lnum , i ))) && 868 \! last_token_of_line && 869 \( empty ( stack ) || stack ==# [ 'when' ] || stack ==# [ '->' ] || 870 \stack ==# [ '->' , ';' ]) 871 872" If we are after of/receive/etc, but these are not the last 873" tokens of the line, we want to indent like this: 874" 875" % stack == [] 876" receive stored_vcol, 877" LTI 878" 879" % stack == ['->', ';'] 880" receive stored_vcol -> 881" B; 882" LTI 883" 884" % stack == ['->'] 885" receive stored_vcol -> 886" LTI 887" 888" % stack == ['when'] 889" receive stored_vcol when 890" LTI 891 892" stack = [] => LTI is a condition 893" stack = ['->'] => LTI is a branch 894" stack = ['->', ';'] => LTI is a condition 895" stack = ['when'] => LTI is a guard 896if empty ( stack ) || stack == [ '->' , ';' ] 897call s: Log ( ' LTI is in a condition after ' . 898 \'"of/receive/after/if/else/catch" -> return' ) 899return stored_vcol 900elseif stack == [ '->' ] 901call s: Log ( ' LTI is in a branch after ' . 902 \'"of/receive/after/if/else/catch" -> return' ) 903return stored_vcol + shiftwidth () 904elseif stack == [ 'when' ] 905call s: Log ( ' LTI is in a guard after ' . 906 \'"of/receive/after/if/else/catch" -> return' ) 907return stored_vcol + shiftwidth () 908else 909return s: UnexpectedToken ( token , stack ) 910endif 911 912elseif index ([ 'case' , 'if' , 'try' , 'receive' , 'maybe' ], token ) != -1 913 914" stack = [] => LTI is a condition 915" stack = ['->'] => LTI is a branch 916" stack = ['->', ';'] => LTI is a condition 917" stack = ['when'] => LTI is in a guard 918if empty ( stack ) 919" pass 920elseif ( token ==# 'case' && stack [ 0 ] ==# 'of' ) || 921 \( token ==# 'if' ) || 922 \( token ==# 'maybe' && stack [ 0 ] ==# 'else' ) || 923 \( token ==# 'try' && ( stack [ 0 ] ==# 'of' || 924 \stack [ 0 ] ==# 'catch' || 925 \stack [ 0 ] ==# 'after' )) || 926 \( token ==# 'receive' ) 927 928" From the indentation point of view, the keyword 929" (of/catch/after/else/end) before the LTI is what counts, so 930" when we reached these tokens, and the stack already had 931" a catch/after/else/end, we didn't modify it. 932" 933" This way when we reach case/try/receive/maybe (i.e. now), 934" there is at most one of/catch/after/else/end token in the 935" stack. 936if token ==# 'case' || token ==# 'try' || 937 \( token ==# 'receive' && stack [ 0 ] ==# 'after' ) || 938 \( token ==# 'maybe' && stack [ 0 ] ==# 'else' ) 939call s: Pop ( stack ) 940endif 941 942if empty ( stack ) 943call s: Log ( ' LTI is in a condition; matching ' . 944 \'"case/if/try/receive/maybe" found' ) 945let stored_vcol = curr_vcol + shiftwidth () 946elseif stack [ 0 ] ==# 'align_to_begin_element' 947call s: Pop ( stack ) 948let stored_vcol = curr_vcol 949elseif len ( stack ) > 1 && stack [ 0 ] ==# '->' && stack [ 1 ] ==# ';' 950call s: Log ( ' LTI is in a condition; matching ' . 951 \'"case/if/try/receive/maybe" found' ) 952call s: Pop ( stack ) 953call s: Pop ( stack ) 954let stored_vcol = curr_vcol + shiftwidth () 955elseif stack [ 0 ] ==# '->' 956call s: Log ( ' LTI is in a branch; matching ' . 957 \'"case/if/try/receive/maybe" found' ) 958call s: Pop ( stack ) 959let stored_vcol = curr_vcol + 2 * shiftwidth () 960elseif stack [ 0 ] ==# 'when' 961call s: Log ( ' LTI is in a guard; matching ' . 962 \'"case/if/try/receive/maybe" found' ) 963call s: Pop ( stack ) 964let stored_vcol = curr_vcol + 2 * shiftwidth () + 2 965endif 966 967endif 968 969let [ ret , res ] = s: BeginElementFound ( stack , token , curr_vcol , 970 \stored_vcol , 'end' , shiftwidth ()) 971if ret |return res |endif 972 973elseif token ==# 'fun' 974let [ next_indtoken , next_lnum , next_i ] = s: NextIndToken ( lnum , i ) 975call s: Log ( ' Next indtoken = ' . string ( next_indtoken )) 976 977if ! empty ( next_indtoken ) && next_indtoken [ 0 ] =~# '^[A-Z_@]' 978" The "fun" is followed by a variable, so we might have a named fun: 979" "fun Fun() -> ok end". Thus we take the next token to decide 980" whether this is a function definition ("fun()") or just a function 981" reference ("fun Mod:Fun"). 982let [ next_indtoken , _ , _ ] = s: NextIndToken ( next_lnum , next_i ) 983call s: Log ( ' Next indtoken = ' . string ( next_indtoken )) 984endif 985 986if ! empty ( next_indtoken ) && next_indtoken [ 0 ] ==# '(' 987" We have an anonymous function definition 988" (e.g. "fun () -> ok end") 989 990" stack = [] => LTI is a condition 991" stack = ['->'] => LTI is a branch 992" stack = ['->', ';'] => LTI is a condition 993" stack = ['when'] => LTI is in a guard 994if empty ( stack ) 995call s: Log ( ' LTI is in a condition; matching "fun" found' ) 996let stored_vcol = curr_vcol + shiftwidth () 997elseif len ( stack ) > 1 && stack [ 0 ] ==# '->' && stack [ 1 ] ==# ';' 998call s: Log ( ' LTI is in a condition; matching "fun" found' ) 999call s: Pop ( stack ) 1000call s: Pop ( stack ) 1001elseif stack [ 0 ] ==# '->' 1002call s: Log ( ' LTI is in a branch; matching "fun" found' ) 1003call s: Pop ( stack ) 1004let stored_vcol = curr_vcol + 2 * shiftwidth () 1005elseif stack [ 0 ] ==# 'when' 1006call s: Log ( ' LTI is in a guard; matching "fun" found' ) 1007call s: Pop ( stack ) 1008let stored_vcol = curr_vcol + 2 * shiftwidth () + 2 1009endif 1010 1011let [ ret , res ] = s: BeginElementFound ( stack , token , curr_vcol , 1012 \stored_vcol , 'end' , shiftwidth ()) 1013if ret |return res |endif 1014else 1015" Pass: we have a function reference (e.g. "fun f/0") 1016endif 1017 1018elseif token ==# '[' 1019" Emacs compatibility 1020let [ ret , res ] = s: BeginElementFound ( stack , token , curr_vcol , 1021 \stored_vcol , ']' , 1 ) 1022if ret |return res |endif 1023 1024elseif token ==# '<<' 1025" Emacs compatibility 1026let [ ret , res ] = s: BeginElementFound ( stack , token , curr_vcol , 1027 \stored_vcol , '>>' , 2 ) 1028if ret |return res |endif 1029 1030elseif token ==# '(' || token ==# '{' 1031 1032let end_token = ( token ==# '(' ? ')' : 1033 \token ==# '{' ? '}' : 'error' ) 1034 1035if empty ( stack ) 1036" We found the opening paren whose block contains the LTI. 1037let mode = 'inside' 1038elseif stack [ 0 ] ==# end_token 1039call s: Log ( ' "' . token . '" pops "' . end_token . '"' ) 1040call s: Pop ( stack ) 1041 1042if ! empty ( stack ) && stack [ 0 ] ==# 'align_to_begin_element' 1043" We found the opening paren whose closing paren 1044" starts LTI 1045let mode = 'align_to_begin_element' 1046else 1047" We found the opening pair for a closing paren that 1048" was already in the stack. 1049let mode = 'outside' 1050endif 1051else 1052return s: UnexpectedToken ( token , stack ) 1053endif 1054 1055if mode ==# 'inside' || mode ==# 'align_to_begin_element' 1056 1057if last_token_of_line && i != 0 1058" Examples: {{{ 1059" 1060" mode == 'inside': 1061" 1062" my_func( 1063" LTI 1064" 1065" [Variable, { 1066" LTI 1067" 1068" mode == 'align_to_begin_element': 1069" 1070" my_func( 1071" Params 1072" ) % LTI 1073" 1074" [Variable, { 1075" Terms 1076" } % LTI 1077" }}} 1078let stack = [ 'prev_term_plus' ] 1079let plus = ( mode ==# 'inside' ? 2 : 1 ) 1080call s: Log ( ' "' . token . 1081 \'" token found at end of line -> find previous token' ) 1082elseif mode ==# 'align_to_begin_element' 1083" Examples: {{{ 1084" 1085" mode == 'align_to_begin_element' && !last_token_of_line 1086" 1087" my_func(stored_vcol 1088" ) % LTI 1089" 1090" [Variable, {stored_vcol 1091" } % LTI 1092" 1093" mode == 'align_to_begin_element' && i == 0 1094" 1095" ( 1096" stored_vcol 1097" ) % LTI 1098" 1099" { 1100" stored_vcol 1101" } % LTI 1102" }}} 1103call s: Log ( ' "' . token . '" token (whose closing token ' . 1104 \'starts LTI) found -> return' ) 1105return curr_vcol 1106elseif stored_vcol ==# -1 1107" Examples: {{{ 1108" 1109" mode == 'inside' && stored_vcol == -1 && !last_token_of_line 1110" 1111" my_func( 1112" LTI 1113" [Variable, { 1114" LTI 1115" 1116" mode == 'inside' && stored_vcol == -1 && i == 0 1117" 1118" ( 1119" LTI 1120" 1121" { 1122" LTI 1123" }}} 1124call s: Log ( ' "' . token . 1125 \'" token (which directly precedes LTI) found -> return' ) 1126return curr_vcol + 1 1127else 1128" Examples: {{{ 1129" 1130" mode == 'inside' && stored_vcol != -1 && !last_token_of_line 1131" 1132" my_func(stored_vcol, 1133" LTI 1134" 1135" [Variable, {stored_vcol, 1136" LTI 1137" 1138" mode == 'inside' && stored_vcol != -1 && i == 0 1139" 1140" (stored_vcol, 1141" LTI 1142" 1143" {stored_vcol, 1144" LTI 1145" }}} 1146call s: Log ( ' "' . token . 1147 \'" token (whose block contains LTI) found -> return' ) 1148return stored_vcol 1149endif 1150endif 1151 1152elseif index ([ 'end' , ')' , ']' , '}' , '>>' ], token ) != -1 1153 1154" If we can be sure that there is synchronization in the Erlang 1155" syntax, we use searchpair to make the script quicker. Otherwise we 1156" just push the token onto the stack and keep parsing. 1157 1158" No synchronization -> no searchpair optimization 1159if ! exists ( 'b:erlang_syntax_synced' ) 1160call s: Push ( stack , token ) 1161 1162" We don't have searchpair optimization for '>>' 1163elseif token ==# '>>' 1164call s: Push ( stack , token ) 1165 1166elseif token ==# 'end' 1167let [ lnum_new , col_new ] = s: SearchEndPair ( lnum , curr_col ) 1168 1169if lnum_new ==# 0 1170return s: IndentError ( 'Matching token for "end" not found' , 1171 \token , stack ) 1172else 1173if lnum_new != lnum 1174call s: Log ( ' Tokenize for "end" <<<<' ) 1175let [ lnum , indtokens ] = s: TokenizeLine ( lnum_new , 'up' ) 1176call s: Log ( ' >>>> Tokenize for "end"' ) 1177endif 1178 1179let [ success , i ] = s: GetIndtokenAtCol ( indtokens , col_new ) 1180if ! success |return i |endif 1181let [ token , curr_vcol , curr_col ] = indtokens [ i ] 1182call s: Log ( ' Match for "end" in line ' . lnum_new . ': ' . 1183 \string ( indtokens [ i ])) 1184endif 1185 1186else " token is one of the following: ')', ']', '}' 1187 1188call s: Push ( stack , token ) 1189 1190" We have to escape '[', because this string will be interpreted as a 1191" regexp 1192let open_paren = ( token ==# ')' ? '(' : 1193 \token ==# ']' ? '\[' : 1194 \'{' ) 1195 1196let [ lnum_new , col_new ] = s: SearchPair ( lnum , curr_col , 1197 \open_paren , '' , token ) 1198 1199if lnum_new ==# 0 1200return s: IndentError ( 'Matching token not found' , 1201 \token , stack ) 1202else 1203if lnum_new != lnum 1204call s: Log ( ' Tokenize the opening paren <<<<' ) 1205let [ lnum , indtokens ] = s: TokenizeLine ( lnum_new , 'up' ) 1206call s: Log ( ' >>>>' ) 1207endif 1208 1209let [ success , i ] = s: GetIndtokenAtCol ( indtokens , col_new ) 1210if ! success |return i |endif 1211let [ token , curr_vcol , curr_col ] = indtokens [ i ] 1212call s: Log ( ' Match in line ' . lnum_new . ': ' . 1213 \string ( indtokens [ i ])) 1214 1215" Go back to the beginning of the loop and handle the opening paren 1216continue 1217endif 1218endif 1219 1220elseif token ==# ';' 1221 1222if empty ( stack ) 1223call s: Push ( stack , ';' ) 1224elseif index ([ ';' , '->' , 'when' , 'end' , 'after' , 'catch' , 'else' ], 1225 \stack [ 0 ]) != -1 1226" Pass: 1227" 1228" - If the stack top is another ';', then one ';' is 1229" enough. 1230" - If the stack top is an '->' or a 'when', then we 1231" should keep that, because they signify the type of the 1232" LTI (branch, condition or guard). 1233" - From the indentation point of view, the keyword 1234" (of/catch/after/else/end) before the LTI is what counts, so 1235" if the stack already has a catch/after/else/end, we don't 1236" modify it. This way when we reach case/try/receive/maybe, 1237" there will be at most one of/catch/after/else/end token in 1238" the stack. 1239else 1240return s: UnexpectedToken ( token , stack ) 1241endif 1242 1243elseif token ==# '->' 1244 1245if empty ( stack ) && ! last_token_of_line 1246call s: Log ( ' LTI is in expression after arrow -> return' ) 1247return stored_vcol 1248elseif empty ( stack ) || stack [ 0 ] ==# ';' || stack [ 0 ] ==# 'end' 1249" stack = [';'] -> LTI is either a branch or in a guard 1250" stack = ['->'] -> LTI is a condition 1251" stack = ['->', ';'] -> LTI is a branch 1252call s: Push ( stack , '->' ) 1253elseif index ([ '->' , 'when' , 'end' , 'after' , 'catch' , 'else' ], 1254 \stack [ 0 ]) != -1 1255" Pass: 1256" 1257" - If the stack top is another '->', then one '->' is 1258" enough. 1259" - If the stack top is a 'when', then we should keep 1260" that, because this signifies that LTI is a in a guard. 1261" - From the indentation point of view, the keyword 1262" (of/catch/after/else/end) before the LTI is what counts, so 1263" if the stack already has a catch/after/else/end, we don't 1264" modify it. This way when we reach case/try/receive/maybe, 1265" there will be at most one of/catch/after/else/end token in 1266" the stack. 1267else 1268return s: UnexpectedToken ( token , stack ) 1269endif 1270 1271elseif token ==# 'when' 1272 1273" Pop all ';' from the top of the stack 1274while ! empty ( stack ) && stack [ 0 ] ==# ';' 1275call s: Pop ( stack ) 1276endwhile 1277 1278if empty ( stack ) 1279if semicolon_abscol != '' 1280let stored_vcol = semicolon_abscol 1281endif 1282if ! last_token_of_line 1283" Example: 1284" when A, 1285" LTI 1286let [ ret , res ] = s: BeginElementFoundIfEmpty ( stack , token , curr_vcol , 1287 \stored_vcol , shiftwidth ()) 1288if ret |return res |endif 1289else 1290" Example: 1291" when 1292" LTI 1293call s: Push ( stack , token ) 1294endif 1295elseif index ([ '->' , 'when' , 'end' , 'after' , 'catch' , 'else' ], 1296 \stack [ 0 ]) != -1 1297" Pass: 1298" - If the stack top is another 'when', then one 'when' is 1299" enough. 1300" - If the stack top is an '->' or a 'when', then we 1301" should keep that, because they signify the type of the 1302" LTI (branch, condition or guard). 1303" - From the indentation point of view, the keyword 1304" (of/catch/after/else/end) before the LTI is what counts, so 1305" if the stack already has a catch/after/else/end, we don't 1306" modify it. This way when we reach case/try/receive/maybe, 1307" there will be at most one of/catch/after/else/end token in 1308" the stack. 1309else 1310return s: UnexpectedToken ( token , stack ) 1311endif 1312 1313elseif token ==# 'of' || token ==# 'after' || token ==# 'else' || 1314 \( token ==# 'catch' && ! s: IsCatchStandalone ( lnum , i )) 1315 1316if token ==# 'after' || token ==# 'else' 1317" If LTI is between an after/else and the corresponding 'end', then 1318" let's return because calculating the indentation based on 1319" after/else is enough. 1320" 1321" Example: 1322" receive A after 1323" LTI 1324" maybe A else 1325" LTI 1326" 1327" Note about Emacs compatibility {{{ 1328" 1329" It would be fine to indent the examples above the following way: 1330" 1331" receive A after 1332" LTI 1333" maybe A else 1334" LTI 1335" 1336" We intend it the way above because that is how Emacs does it. 1337" Also, this is a bit faster. 1338" 1339" We are still not 100% Emacs compatible because of placing the 1340" 'end' after the indented blocks. 1341" 1342" Emacs example: 1343" 1344" receive A after 1345" LTI 1346" end, 1347" maybe A else 1348" LTI 1349" end % Yes, it's here (in OTP 25.0, might change 1350" % later) 1351" 1352" vim-erlang example: 1353" 1354" receive A after 1355" LTI 1356" end, 1357" maybe A else 1358" LTI 1359" end 1360" }}} 1361let [ ret , res ] = s: BeginElementFoundIfEmpty ( stack , token , curr_vcol , 1362 \stored_vcol , shiftwidth ()) 1363if ret |return res |endif 1364endif 1365 1366if empty ( stack ) || stack [ 0 ] ==# '->' || stack [ 0 ] ==# 'when' 1367call s: Push ( stack , token ) 1368elseif stack [ 0 ] ==# 'catch' || stack [ 0 ] ==# 'after' || 1369 \stack [ 0 ] ==# 'else' || stack [ 0 ] ==# 'end' 1370" Pass: From the indentation point of view, the keyword 1371" (of/catch/after/end) before the LTI is what counts, so 1372" if the stack already has a catch/after/end, we don't 1373" modify it. This way when we reach case/try/receive, 1374" there will be at most one of/catch/after/end token in 1375" the stack. 1376else 1377return s: UnexpectedToken ( token , stack ) 1378endif 1379 1380elseif token ==# '||' && empty ( stack ) && ! last_token_of_line 1381 1382call s: Log ( ' LTI is in expression after "||" -> return' ) 1383return stored_vcol 1384 1385else 1386call s: Log ( ' Misc token, stack unchanged = ' . string ( stack )) 1387 1388endif 1389 1390if empty ( stack ) || stack [ 0 ] ==# '->' || stack [ 0 ] ==# 'when' 1391let stored_vcol = curr_vcol 1392let semicolon_abscol = '' 1393call s: Log ( ' Misc token when the stack is empty or has "->" ' . 1394 \'-> setting stored_vcol to ' . stored_vcol ) 1395elseif stack [ 0 ] ==# ';' 1396let semicolon_abscol = curr_vcol 1397call s: Log ( ' Setting semicolon-stored_vcol to ' . stored_vcol ) 1398endif 1399 1400let i -= 1 1401call s: Log ( ' Token processed. stored_vcol=' . stored_vcol ) 1402 1403let last_token_of_line = 0 1404 1405endwhile " iteration on tokens in a line 1406 1407call s: Log ( ' Line analyzed. stored_vcol=' . stored_vcol ) 1408 1409if empty ( stack ) && stored_vcol != -1 && 1410 \( ! empty ( indtokens ) && indtokens [ 0 ][ 0 ] != '<string_end>' && 1411 \indtokens [ 0 ][ 0 ] != '<quoted_atom_end>' ) 1412call s: Log ( ' Empty stack at the beginning of the line -> return' ) 1413return stored_vcol 1414endif 1415 1416let lnum -= 1 1417 1418endwhile " iteration on lines 1419 1420endfunction 1421 1422" ErlangIndent function {{{1 1423" ===================== 1424 1425function ! ErlangIndent () 1426 1427call s: ClearTokenCacheIfNeeded () 1428 1429let currline = getline ( v: lnum ) 1430call s: Log ( 'Indenting line ' . v: lnum . ': ' . currline ) 1431 1432if s: IsLineStringContinuation ( v: lnum ) || s: IsLineAtomContinuation ( v: lnum ) 1433call s: Log ( 'String or atom continuation found -> ' . 1434 \'leaving indentation unchanged' ) 1435return -1 1436endif 1437 1438" If the line starts with the comment, and so is the previous non-blank line 1439if currline =~# '^\s*%' 1440let lnum = prevnonblank ( v: lnum - 1 ) 1441if lnum ==# 0 1442call s: Log ( 'First non-empty line of the file -> return 0.' ) 1443return 0 1444else 1445let ml = matchlist ( getline ( lnum ), '^\(\s*\)%' ) 1446" If the previous line also starts with a comment, then return the same 1447" indentation that line has. Otherwise exit from this special "if" and 1448" don't care that the current line is a comment. 1449if ! empty ( ml ) 1450let new_col = s: CalcVCol ( ml [ 1 ], 0 , len ( ml [ 1 ]) - 1 , 0 , & tabstop ) 1451call s: Log ( 'Comment line after another comment line -> ' . 1452 \'use same indent: ' . new_col ) 1453return new_col 1454endif 1455endif 1456endif 1457 1458let ml = matchlist ( currline , 1459 \'^\(\s*\)\(\%(end\|of\|catch\|after\|else\)\>\|[)\]}]\|>>\)' ) 1460 1461" If the line has a special beginning, but not a standalone catch 1462if ! empty ( ml ) && ! ( ml [ 2 ] ==# 'catch' && s: IsCatchStandalone ( v: lnum , 0 )) 1463 1464let curr_col = len ( ml [ 1 ]) 1465 1466" If we can be sure that there is synchronization in the Erlang 1467" syntax, we use searchpair to make the script quicker. 1468if ml [ 2 ] ==# 'end' && exists ( 'b:erlang_syntax_synced' ) 1469 1470let [ lnum , col ] = s: SearchEndPair ( v: lnum , curr_col ) 1471 1472if lnum ==# 0 1473return s: IndentError ( 'Matching token for "end" not found' , 1474 \'end' , []) 1475else 1476call s: Log ( ' Tokenize for "end" <<<<' ) 1477let [ lnum , indtokens ] = s: TokenizeLine ( lnum , 'up' ) 1478call s: Log ( ' >>>> Tokenize for "end"' ) 1479 1480let [ success , i ] = s: GetIndtokenAtCol ( indtokens , col ) 1481if ! success |return i |endif 1482let [ token , curr_vcol , curr_col ] = indtokens [ i ] 1483call s: Log ( ' Match for "end" in line ' . lnum . ': ' . 1484 \string ( indtokens [ i ])) 1485return curr_vcol 1486endif 1487 1488else 1489 1490call s: Log ( " Line type = 'end'" ) 1491let new_col = s: ErlangCalcIndent ( v: lnum - 1 , 1492 \[ ml [ 2 ], 'align_to_begin_element' ]) 1493endif 1494else 1495call s: Log ( " Line type = 'normal'" ) 1496 1497let new_col = s: ErlangCalcIndent ( v: lnum - 1 , []) 1498if currline =~# '^\s*when\>' 1499let new_col += 2 1500endif 1501endif 1502 1503if new_col < -1 1504call s: Log ( 'WARNING: returning new_col == ' . new_col ) 1505return g: erlang_unexpected_token_indent 1506endif 1507 1508return new_col 1509 1510endfunction 1511 1512" ErlangShowTokensInLine functions {{{1 1513" ================================ 1514 1515" These functions are useful during development. 1516 1517function ! ErlangShowTokensInLine ( line ) 1518echo "Line: " . a: line 1519let indtokens = s: GetTokensFromLine ( a: line , 0 , 0 , & tabstop ) 1520echo "Tokens:" 1521for it in indtokens 1522echo it 1523endfor 1524endfunction 1525 1526function ! ErlangShowTokensInCurrentLine () 1527return ErlangShowTokensInLine ( getline ( '.' )) 1528endfunction 1529 1530" Cleanup {{{1 1531" ======= 1532 1533let & cpo = s: cpo_save 1534unlet s: cpo_save 1535 1536" vim: sw=2 et fdm=marker