occam.vim 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. " Only load this indent file when no other was loaded.
  7. if exists("b:did_indent")
  8. finish
  9. endif
  10. let b:did_indent = 1
  11. "{{{ Settings
  12. " Set the occam indent function
  13. setlocal indentexpr=GetOccamIndent()
  14. " Indent after new line and after initial colon
  15. setlocal indentkeys=o,O,0=:
  16. "}}}
  17. let b:undo_indent = "setl inde< indk<"
  18. " Only define the function once
  19. if exists("*GetOccamIndent")
  20. finish
  21. endif
  22. let s:keepcpo= &cpo
  23. set cpo&vim
  24. "{{{ Indent definitions
  25. " Define carriage return indent
  26. let s:FirstLevelIndent = '^\C\s*\(IF\|ALT\|PRI\s\+ALT\|PAR\|SEQ\|PRI\s\+PAR\|WHILE\|VALOF\|CLAIM\|FORKING\)\>\|\(--.*\)\@<!\(\<PROC\>\|??\|\<CASE\>\s*\(--.*\)\=\_$\)'
  27. let s:FirstLevelNonColonEndIndent = '^\C\s*PROTOCOL\>\|\(--.*\)\@<!\<\(\(CHAN\|DATA\)\s\+TYPE\|FUNCTION\)\>'
  28. let s:SecondLevelIndent = '^\C\s*\(IF\|ALT\|PRI\s\+ALT\)\>\|\(--.*\)\@<!?\s*\<CASE\>\s*\(--.*\)\=\_$'
  29. let s:SecondLevelNonColonEndIndent = '\(--.*\)\@<!\<\(CHAN\|DATA\)\s\+TYPE\>'
  30. " Define colon indent
  31. let s:ColonIndent = '\(--.*\)\@<!\<PROC\>'
  32. let s:ColonNonColonEndIndent = '^\C\s*PROTOCOL\>\|\(--.*\)\@<!\<\(\(CHAN\|DATA\)\s\+TYPE\|FUNCTION\)\>'
  33. let s:ColonEnd = '\(--.*\)\@<!:\s*\(--.*\)\=$'
  34. let s:ColonStart = '^\s*:\s*\(--.*\)\=$'
  35. " Define comment
  36. let s:CommentLine = '^\s*--'
  37. "}}}
  38. "{{{ function GetOccamIndent()
  39. " Auxiliary function to get the correct indent for a line of occam code
  40. function GetOccamIndent()
  41. " Ensure magic is on
  42. let save_magic = &magic
  43. setlocal magic
  44. " Get reference line number
  45. let linenum = prevnonblank(v:lnum - 1)
  46. while linenum > 0 && getline(linenum) =~ s:CommentLine
  47. let linenum = prevnonblank(linenum - 1)
  48. endwhile
  49. " Get current indent
  50. let curindent = indent(linenum)
  51. " Get current line
  52. let line = getline(linenum)
  53. " Get previous line number
  54. let prevlinenum = prevnonblank(linenum - 1)
  55. while prevlinenum > 0 && getline(prevlinenum) =~ s:CommentLine
  56. let prevlinenum = prevnonblank(prevlinenum - 1)
  57. endwhile
  58. " Get previous line
  59. let prevline = getline(prevlinenum)
  60. " Colon indent
  61. if getline(v:lnum) =~ s:ColonStart
  62. let found = 0
  63. while found < 1
  64. if line =~ s:ColonStart
  65. let found = found - 1
  66. elseif line =~ s:ColonIndent || (line =~ s:ColonNonColonEndIndent && line !~ s:ColonEnd)
  67. let found = found + 1
  68. endif
  69. if found < 1
  70. let linenum = prevnonblank(linenum - 1)
  71. if linenum > 0
  72. let line = getline(linenum)
  73. else
  74. let found = 1
  75. endif
  76. endif
  77. endwhile
  78. if linenum > 0
  79. let curindent = indent(linenum)
  80. else
  81. let colonline = getline(v:lnum)
  82. let tabstr = ''
  83. while strlen(tabstr) < &tabstop
  84. let tabstr = ' ' . tabstr
  85. endwhile
  86. let colonline = substitute(colonline, '\t', tabstr, 'g')
  87. let curindent = match(colonline, ':')
  88. endif
  89. " Restore magic
  90. if !save_magic|setlocal nomagic|endif
  91. return curindent
  92. endif
  93. if getline(v:lnum) =~ '^\s*:'
  94. let colonline = getline(v:lnum)
  95. let tabstr = ''
  96. while strlen(tabstr) < &tabstop
  97. let tabstr = ' ' . tabstr
  98. endwhile
  99. let colonline = substitute(colonline, '\t', tabstr, 'g')
  100. let curindent = match(colonline, ':')
  101. " Restore magic
  102. if !save_magic|setlocal nomagic|endif
  103. return curindent
  104. endif
  105. " Carriage return indenat
  106. if line =~ s:FirstLevelIndent || (line =~ s:FirstLevelNonColonEndIndent && line !~ s:ColonEnd)
  107. \ || (line !~ s:ColonStart && (prevline =~ s:SecondLevelIndent
  108. \ || (prevline =~ s:SecondLevelNonColonEndIndent && prevline !~ s:ColonEnd)))
  109. let curindent = curindent + shiftwidth()
  110. " Restore magic
  111. if !save_magic|setlocal nomagic|endif
  112. return curindent
  113. endif
  114. " Commented line
  115. if getline(prevnonblank(v:lnum - 1)) =~ s:CommentLine
  116. " Restore magic
  117. if !save_magic|setlocal nomagic|endif
  118. return indent(prevnonblank(v:lnum - 1))
  119. endif
  120. " Look for previous second level IF / ALT / PRI ALT
  121. let found = 0
  122. while !found
  123. if indent(prevlinenum) == curindent - shiftwidth()
  124. let found = 1
  125. endif
  126. if !found
  127. let prevlinenum = prevnonblank(prevlinenum - 1)
  128. while prevlinenum > 0 && getline(prevlinenum) =~ s:CommentLine
  129. let prevlinenum = prevnonblank(prevlinenum - 1)
  130. endwhile
  131. if prevlinenum == 0
  132. let found = 1
  133. endif
  134. endif
  135. endwhile
  136. if prevlinenum > 0
  137. if getline(prevlinenum) =~ s:SecondLevelIndent
  138. let curindent = curindent + shiftwidth()
  139. endif
  140. endif
  141. " Restore magic
  142. if !save_magic|setlocal nomagic|endif
  143. return curindent
  144. endfunction
  145. "}}}
  146. let &cpo = s:keepcpo
  147. unlet s:keepcpo