robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
0c2a7aa5f7
master
1" Vim indent file 2" Language: Pascal 3" Maintainer: Neil Carter <n.carter@swansea.ac.uk> 4" Created: 2004 Jul 13 5" Last Change: 2021 Sep 22 6" 7" For further documentation, see https://psy.swansea.ac.uk/staff/carter/vim/ 8 9 10if exists ( "b:did_indent" ) 11finish 12endif 13let b: did_indent = 1 14 15setlocal indentexpr = GetPascalIndent (v:lnum) 16setlocal indentkeys & 17setlocal indentkeys += =end;,==const,==type,==var,==begin,==repeat,==until,==for 18setlocal indentkeys += =program,==function,==procedure,==object,==private 19setlocal indentkeys += =record,==if,==else,==case 20 21let b: undo_indent = 'setlocal indentexpr< indentkeys<' 22 23if exists ( "*GetPascalIndent" ) 24finish 25endif 26 27 28" ________________________________________________________________ 29function ! s: GetPrevNonCommentLineNum ( line_num ) 30 31" Skip lines starting with a comment 32let SKIP_LINES = '^\s*\(\((\*\)\|\(\*\ \)\|\(\*)\)\|{\|}\)' 33 34let nline = a: line_num 35while nline > 0 36let nline = prevnonblank ( nline - 1 ) 37if getline ( nline ) !~? SKIP_LINES 38break 39endif 40endwhile 41 42return nline 43endfunction 44 45 46" ________________________________________________________________ 47function ! s: PurifyCode ( line_num ) 48" Strip any trailing comments and whitespace 49let pureline = 'TODO' 50return pureline 51endfunction 52 53 54" ________________________________________________________________ 55function ! GetPascalIndent ( line_num ) 56 57" Line 0 always goes at column 0 58if a: line_num == 0 59return 0 60endif 61 62let this_codeline = getline ( a: line_num ) 63 64 65" SAME INDENT 66 67" Middle of a three-part comment 68if this_codeline =~ '^\s*\*' 69return indent ( a: line_num - 1 ) 70endif 71 72 73" COLUMN 1 ALWAYS 74 75" Last line of the program 76if this_codeline =~ '^\s*end\.' 77return 0 78endif 79 80" Compiler directives, allowing "(*" and "{" 81"if this_codeline =~ '^\s*\({\|(\*\)$\(IFDEF\|IFNDEF\|ELSE\|ENDIF\)' 82if this_codeline =~ '^\s*\({\|(\*\)\$' 83return 0 84endif 85 86" section headers 87if this_codeline =~ '^\s*\(program\|procedure\|function\|type\)\>' 88return 0 89endif 90 91" Subroutine separators, lines ending with "const" or "var" 92if this_codeline =~ '^\s*\((\*\ _\+\ \*)\|\(const\|var\)\)$' 93return 0 94endif 95 96 97" OTHERWISE, WE NEED TO LOOK FURTHER BACK... 98 99let prev_codeline_num = s: GetPrevNonCommentLineNum ( a: line_num ) 100let prev_codeline = getline ( prev_codeline_num ) 101let indnt = indent ( prev_codeline_num ) 102 103 104" INCREASE INDENT 105 106" If the PREVIOUS LINE ended in these items, always indent 107if prev_codeline =~ '\<\(type\|const\|var\)$' 108return indnt + shiftwidth () 109endif 110 111if prev_codeline =~ '\<repeat$' 112if this_codeline !~ '^\s*until\>' 113return indnt + shiftwidth () 114else 115return indnt 116endif 117endif 118 119if prev_codeline =~ '\<\(begin\|record\)$' 120if this_codeline !~ '^\s*end\>' 121return indnt + shiftwidth () 122else 123return indnt 124endif 125endif 126 127" If the PREVIOUS LINE ended with these items, indent if not 128" followed by "begin" 129if prev_codeline =~ '\<\(\|else\|then\|do\)$' || prev_codeline =~ ':$' 130if this_codeline !~ '^\s*begin\>' 131return indnt + shiftwidth () 132else 133" If it does start with "begin" then keep the same indent 134"return indnt + shiftwidth() 135return indnt 136endif 137endif 138 139" Inside a parameter list (i.e. a "(" without a ")"). ???? Considers 140" only the line before the current one. TODO: Get it working for 141" parameter lists longer than two lines. 142if prev_codeline =~ '([^)]\+$' 143return indnt + shiftwidth () 144endif 145 146 147" DECREASE INDENT 148 149" Lines starting with "else", but not following line ending with 150" "end". 151if this_codeline =~ '^\s*else\>' && prev_codeline !~ '\<end$' 152return indnt - shiftwidth () 153endif 154 155" Lines after a single-statement branch/loop. 156" Two lines before ended in "then", "else", or "do" 157" Previous line didn't end in "begin" 158let prev2_codeline_num = s: GetPrevNonCommentLineNum ( prev_codeline_num ) 159let prev2_codeline = getline ( prev2_codeline_num ) 160if prev2_codeline =~ '\<\(then\|else\|do\)$' && prev_codeline !~ '\<begin$' 161" If the next code line after a single statement branch/loop 162" starts with "end", "except" or "finally", we need an 163" additional unindentation. 164if this_codeline =~ '^\s*\(end;\|except\|finally\|\)$' 165" Note that we don't return from here. 166return indnt - 2 * shiftwidth () 167endif 168return indnt - shiftwidth () 169endif 170 171" Lines starting with "until" or "end". This rule must be overridden 172" by the one for "end" after a single-statement branch/loop. In 173" other words that rule should come before this one. 174if this_codeline =~ '^\s*\(end\|until\)\>' 175return indnt - shiftwidth () 176endif 177 178 179" MISCELLANEOUS THINGS TO CATCH 180 181" Most "begin"s will have been handled by now. Any remaining 182" "begin"s on their own line should go in column 1. 183if this_codeline =~ '^\s*begin$' 184return 0 185endif 186 187 188" ________________________________________________________________ 189" Object/Borland Pascal/Delphi Extensions 190" 191" Note that extended-pascal is handled here, unless it is simpler to 192" handle them in the standard-pascal section above. 193 194 195" COLUMN 1 ALWAYS 196 197" section headers at start of line. 198if this_codeline =~ '^\s*\(interface\|implementation\|uses\|unit\)\>' 199return 0 200endif 201 202 203" INDENT ONCE 204 205" If the PREVIOUS LINE ended in these items, always indent. 206if prev_codeline =~ '^\s*\(unit\|uses\|try\|except\|finally\|private\|protected\|public\|published\)$' 207return indnt + shiftwidth () 208endif 209 210" ???? Indent "procedure" and "functions" if they appear within an 211" class/object definition. But that means overriding standard-pascal 212" rule where these words always go in column 1. 213 214 215" UNINDENT ONCE 216 217if this_codeline =~ '^\s*\(except\|finally\)$' 218return indnt - shiftwidth () 219endif 220 221if this_codeline =~ '^\s*\(private\|protected\|public\|published\)$' 222return indnt - shiftwidth () 223endif 224 225 226" If nothing changed, return same indent. 227return indnt 228endfunction 229