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