robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
d3068d34cc
master
1" Vim indent file 2" Language: occam 3" Maintainer: Mario Schweigler <ms44@kent.ac.uk> (Invalid email address) 4" Doug Kearns <dougkearns@gmail.com> 5" Last Change: 2022 Apr 06 6 7" Only load this indent file when no other was loaded. 8if exists ( "b:did_indent" ) 9finish 10endif 11let b: did_indent = 1 12 13"{{{ Settings 14" Set the occam indent function 15setlocal indentexpr = GetOccamIndent () 16" Indent after new line and after initial colon 17setlocal indentkeys = o,O,0=: 18"}}} 19 20let b: undo_indent = "setl inde< indk<" 21 22" Only define the function once 23if exists ( "*GetOccamIndent" ) 24finish 25endif 26let s: keepcpo = & cpo 27set cpo & vim 28 29"{{{ Indent definitions 30" Define carriage return indent 31let s: FirstLevelIndent = '^\C\s*\(IF\|ALT\|PRI\s\+ALT\|PAR\|SEQ\|PRI\s\+PAR\|WHILE\|VALOF\|CLAIM\|FORKING\)\>\|\(--.*\)\@<!\(\<PROC\>\|??\|\<CASE\>\s*\(--.*\)\=\_$\)' 32let s: FirstLevelNonColonEndIndent = '^\C\s*PROTOCOL\>\|\(--.*\)\@<!\<\(\(CHAN\|DATA\)\s\+TYPE\|FUNCTION\)\>' 33let s: SecondLevelIndent = '^\C\s*\(IF\|ALT\|PRI\s\+ALT\)\>\|\(--.*\)\@<!?\s*\<CASE\>\s*\(--.*\)\=\_$' 34let s: SecondLevelNonColonEndIndent = '\(--.*\)\@<!\<\(CHAN\|DATA\)\s\+TYPE\>' 35 36" Define colon indent 37let s: ColonIndent = '\(--.*\)\@<!\<PROC\>' 38let s: ColonNonColonEndIndent = '^\C\s*PROTOCOL\>\|\(--.*\)\@<!\<\(\(CHAN\|DATA\)\s\+TYPE\|FUNCTION\)\>' 39 40let s: ColonEnd = '\(--.*\)\@<!:\s*\(--.*\)\=$' 41let s: ColonStart = '^\s*:\s*\(--.*\)\=$' 42 43" Define comment 44let s: CommentLine = '^\s*--' 45"}}} 46 47"{{{ function GetOccamIndent() 48" Auxiliary function to get the correct indent for a line of occam code 49function GetOccamIndent () 50 51" Ensure magic is on 52let save_magic = & magic 53setlocal magic 54 55" Get reference line number 56let linenum = prevnonblank ( v: lnum - 1 ) 57while linenum > 0 && getline ( linenum ) =~ s: CommentLine 58let linenum = prevnonblank ( linenum - 1 ) 59endwhile 60 61" Get current indent 62let curindent = indent ( linenum ) 63 64" Get current line 65let line = getline ( linenum ) 66 67" Get previous line number 68let prevlinenum = prevnonblank ( linenum - 1 ) 69while prevlinenum > 0 && getline ( prevlinenum ) =~ s: CommentLine 70let prevlinenum = prevnonblank ( prevlinenum - 1 ) 71endwhile 72 73" Get previous line 74let prevline = getline ( prevlinenum ) 75 76" Colon indent 77if getline ( v: lnum ) =~ s: ColonStart 78 79let found = 0 80 81while found < 1 82 83if line =~ s: ColonStart 84let found = found - 1 85elseif line =~ s: ColonIndent || ( line =~ s: ColonNonColonEndIndent && line !~ s: ColonEnd ) 86let found = found + 1 87endif 88 89if found < 1 90let linenum = prevnonblank ( linenum - 1 ) 91if linenum > 0 92let line = getline ( linenum ) 93else 94let found = 1 95endif 96endif 97 98endwhile 99 100if linenum > 0 101let curindent = indent ( linenum ) 102else 103let colonline = getline ( v: lnum ) 104let tabstr = '' 105while strlen ( tabstr ) < & tabstop 106let tabstr = ' ' . tabstr 107endwhile 108let colonline = substitute ( colonline , '\t' , tabstr , 'g' ) 109let curindent = match ( colonline , ':' ) 110endif 111 112" Restore magic 113if ! save_magic |setlocal nomagic |endif 114 115return curindent 116endif 117 118if getline ( v: lnum ) =~ '^\s*:' 119let colonline = getline ( v: lnum ) 120let tabstr = '' 121while strlen ( tabstr ) < & tabstop 122let tabstr = ' ' . tabstr 123endwhile 124let colonline = substitute ( colonline , '\t' , tabstr , 'g' ) 125let curindent = match ( colonline , ':' ) 126 127" Restore magic 128if ! save_magic |setlocal nomagic |endif 129 130return curindent 131endif 132 133" Carriage return indenat 134if line =~ s: FirstLevelIndent || ( line =~ s: FirstLevelNonColonEndIndent && line !~ s: ColonEnd ) 135 \|| ( line !~ s: ColonStart && ( prevline =~ s: SecondLevelIndent 136 \|| ( prevline =~ s: SecondLevelNonColonEndIndent && prevline !~ s: ColonEnd ))) 137let curindent = curindent + shiftwidth () 138 139" Restore magic 140if ! save_magic |setlocal nomagic |endif 141 142return curindent 143endif 144 145" Commented line 146if getline ( prevnonblank ( v: lnum - 1 )) =~ s: CommentLine 147 148" Restore magic 149if ! save_magic |setlocal nomagic |endif 150 151return indent ( prevnonblank ( v: lnum - 1 )) 152endif 153 154" Look for previous second level IF / ALT / PRI ALT 155let found = 0 156 157while ! found 158 159if indent ( prevlinenum ) == curindent - shiftwidth () 160let found = 1 161endif 162 163if ! found 164let prevlinenum = prevnonblank ( prevlinenum - 1 ) 165while prevlinenum > 0 && getline ( prevlinenum ) =~ s: CommentLine 166let prevlinenum = prevnonblank ( prevlinenum - 1 ) 167endwhile 168if prevlinenum == 0 169let found = 1 170endif 171endif 172 173endwhile 174 175if prevlinenum > 0 176if getline ( prevlinenum ) =~ s: SecondLevelIndent 177let curindent = curindent + shiftwidth () 178endif 179endif 180 181" Restore magic 182if ! save_magic |setlocal nomagic |endif 183 184return curindent 185 186endfunction 187"}}} 188 189let & cpo = s: keepcpo 190unlet s: keepcpo