cobol.vim 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. " Vim indent file
  2. " Language: cobol
  3. " Maintainer: Ankit Jain <ajatkj@yahoo.co.in>
  4. " (formerly Tim Pope <vimNOSPAM@tpope.info>)
  5. " $Id: cobol.vim,v 1.1 2007/05/05 18:08:19 vimboss Exp $
  6. " Last Update: By Ankit Jain on 22.03.2019
  7. " Ankit Jain 22.03.2019 Changes & fixes:
  8. " Allow chars in 1st 6 columns
  9. " #C22032019
  10. if exists("b:did_indent")
  11. finish
  12. endif
  13. let b:did_indent = 1
  14. setlocal expandtab
  15. setlocal indentexpr=GetCobolIndent(v:lnum)
  16. setlocal indentkeys&
  17. setlocal indentkeys+=0<*>,0/,0$,0=01,=~division,=~section,0=~end,0=~then,0=~else,0=~when,*<Return>,.
  18. " Only define the function once.
  19. if exists("*GetCobolIndent")
  20. finish
  21. endif
  22. let s:skip = 'getline(".") =~ "^.\\{6\\}[*/$-]\\|\"[^\"]*\""'
  23. function! s:prevgood(lnum)
  24. " Find a non-blank line above the current line.
  25. " Skip over comments.
  26. let lnum = a:lnum
  27. while lnum > 0
  28. let lnum = prevnonblank(lnum - 1)
  29. let line = getline(lnum)
  30. if line !~? '^\s*[*/$-]' && line !~? '^.\{6\}[*/$CD-]'
  31. break
  32. endif
  33. endwhile
  34. return lnum
  35. endfunction
  36. function! s:stripped(lnum)
  37. return substitute(strpart(getline(a:lnum),0,72),'^\s*','','')
  38. endfunction
  39. function! s:optionalblock(lnum,ind,blocks,clauses)
  40. let ind = a:ind
  41. let clauses = '\c\<\%(\<NOT\s\+\)\@<!\%(NOT\s\+\)\=\%('.a:clauses.'\)'
  42. let begin = '\c-\@<!\<\%('.a:blocks.'\)\>'
  43. let beginfull = begin.'\ze.*\%(\n\%(\s*\%([*/$-].*\)\=\n\)*\)\=\s*\%('.clauses.'\)'
  44. let end = '\c\<end-\%('.a:blocks.'\)\>\|\%(\.\%( \|$\)\)\@='
  45. let cline = s:stripped(a:lnum)
  46. let line = s:stripped(s:prevgood(a:lnum))
  47. if cline =~? clauses "&& line !~? '^search\>'
  48. call cursor(a:lnum,1)
  49. let lastclause = searchpair(beginfull,clauses,end,'bWr',s:skip)
  50. if getline(lastclause) =~? clauses && s:stripped(lastclause) !~? '^'.begin
  51. let ind = indent(lastclause)
  52. elseif lastclause > 0
  53. let ind = indent(lastclause) + shiftwidth()
  54. "let ind = ind + shiftwidth()
  55. endif
  56. elseif line =~? clauses && cline !~? end
  57. let ind = ind + shiftwidth()
  58. endif
  59. return ind
  60. endfunction
  61. function! GetCobolIndent(lnum) abort
  62. let minshft = 6
  63. let ashft = minshft + 1
  64. let bshft = ashft + 4
  65. " (Obsolete) numbered lines
  66. " #C22032019: Columns 1-6 could have alphabets as well as numbers
  67. "if getline(a:lnum) =~? '^\s*\d\{6\}\%($\|[ */$CD-]\)'
  68. if getline(a:lnum) =~? '^\s*[a-zA-Z0-9]\{6\}\%($\|[ */$CD-]\)'
  69. return 0
  70. endif
  71. let cline = s:stripped(a:lnum)
  72. " Comments, etc. must start in the 7th column
  73. if cline =~? '^[*/$-]'
  74. return minshft
  75. elseif cline =~# '^[CD]' && indent(a:lnum) == minshft
  76. return minshft
  77. endif
  78. " Divisions, sections, and file descriptions start in area A
  79. if cline =~? '\<\(DIVISION\|SECTION\)\%($\|\.\)' || cline =~? '^[FS]D\>'
  80. return ashft
  81. endif
  82. " Fields
  83. if cline =~? '^0*\(1\|77\)\>'
  84. return ashft
  85. endif
  86. if cline =~? '^\d\+\>'
  87. let cnum = matchstr(cline,'^\d\+\>')
  88. let default = 0
  89. let step = -1
  90. while step < 2
  91. let lnum = a:lnum
  92. while lnum > 0 && lnum < line('$') && lnum > a:lnum - 500 && lnum < a:lnum + 500
  93. let lnum = step > 0 ? nextnonblank(lnum + step) : prevnonblank(lnum + step)
  94. let line = getline(lnum)
  95. let lindent = indent(lnum)
  96. if line =~? '^\s*\d\+\>'
  97. let num = matchstr(line,'^\s*\zs\d\+\>')
  98. if 0+cnum == num
  99. return lindent
  100. elseif 0+cnum > num && default < lindent + shiftwidth()
  101. let default = lindent + shiftwidth()
  102. endif
  103. elseif lindent < bshft && lindent >= ashft
  104. break
  105. endif
  106. endwhile
  107. let step = step + 2
  108. endwhile
  109. return default ? default : bshft
  110. endif
  111. let lnum = s:prevgood(a:lnum)
  112. " Hit the start of the file, use "zero" indent.
  113. if lnum == 0
  114. return ashft
  115. endif
  116. " Initial spaces are ignored
  117. let line = s:stripped(lnum)
  118. let ind = indent(lnum)
  119. " Paragraphs. There may be some false positives.
  120. if cline =~? '^\(\a[A-Z0-9-]*[A-Z0-9]\|\d[A-Z0-9-]*\a\)\.' "\s*$'
  121. if cline !~? '^EXIT\s*\.' && line =~? '\.\s*$'
  122. return ashft
  123. endif
  124. endif
  125. " Paragraphs in the identification division.
  126. "if cline =~? '^\(PROGRAM-ID\|AUTHOR\|INSTALLATION\|' .
  127. "\ 'DATE-WRITTEN\|DATE-COMPILED\|SECURITY\)\>'
  128. "return ashft
  129. "endif
  130. if line =~? '\.$'
  131. " XXX
  132. return bshft
  133. endif
  134. if line =~? '^PERFORM\>'
  135. let perfline = substitute(line, '\c^PERFORM\s*', "", "")
  136. if perfline =~? '^\%(\k\+\s\+TIMES\)\=\s*$'
  137. let ind = ind + shiftwidth()
  138. elseif perfline =~? '^\%(WITH\s\+TEST\|VARYING\|UNTIL\)\>.*[^.]$'
  139. let ind = ind + shiftwidth()
  140. endif
  141. endif
  142. if line =~? '^\%(IF\|THEN\|ELSE\|READ\|EVALUATE\|SEARCH\|SELECT\)\>'
  143. let ind = ind + shiftwidth()
  144. endif
  145. let ind = s:optionalblock(a:lnum,ind,'ADD\|COMPUTE\|DIVIDE\|MULTIPLY\|SUBTRACT','ON\s\+SIZE\s\+ERROR')
  146. let ind = s:optionalblock(a:lnum,ind,'STRING\|UNSTRING\|ACCEPT\|DISPLAY\|CALL','ON\s\+OVERFLOW\|ON\s\+EXCEPTION')
  147. if cline !~? '^AT\s\+END\>' || line !~? '^SEARCH\>'
  148. let ind = s:optionalblock(a:lnum,ind,'DELETE\|REWRITE\|START\|WRITE\|READ','INVALID\s\+KEY\|AT\s\+END\|NO\s\+DATA\|AT\s\+END-OF-PAGE')
  149. endif
  150. if cline =~? '^WHEN\>'
  151. call cursor(a:lnum,1)
  152. " We also search for READ so that contained AT ENDs are skipped
  153. let lastclause = searchpair('\c-\@<!\<\%(SEARCH\|EVALUATE\|READ\)\>','\c\<\%(WHEN\|AT\s\+END\)\>','\c\<END-\%(SEARCH\|EVALUATE\|READ\)\>','bW',s:skip)
  154. let g:foo = s:stripped(lastclause)
  155. if s:stripped(lastclause) =~? '\c\<\%(WHEN\|AT\s\+END\)\>'
  156. "&& s:stripped(lastclause) !~? '^\%(SEARCH\|EVALUATE\|READ\)\>'
  157. let ind = indent(lastclause)
  158. elseif lastclause > 0
  159. let ind = indent(lastclause) + shiftwidth()
  160. endif
  161. elseif line =~? '^WHEN\>'
  162. let ind = ind + shiftwidth()
  163. endif
  164. "I'm not sure why I had this
  165. "if line =~? '^ELSE\>-\@!' && line !~? '\.$'
  166. "let ind = indent(s:prevgood(lnum))
  167. "endif
  168. if cline =~? '^\(END\)\>-\@!'
  169. " On lines with just END, 'guess' a simple shift left
  170. let ind = ind - shiftwidth()
  171. elseif cline =~? '^\(END-IF\|THEN\|ELSE\)\>-\@!'
  172. call cursor(a:lnum,indent(a:lnum))
  173. let match = searchpair('\c-\@<!\<IF\>','\c-\@<!\%(THEN\|ELSE\)\>','\c-\@<!\<END-IF\>\zs','bnW',s:skip)
  174. if match > 0
  175. let ind = indent(match)
  176. endif
  177. elseif cline =~? '^END-[A-Z]'
  178. let beginword = matchstr(cline,'\c\<END-\zs[A-Z0-9-]\+')
  179. let endword = 'END-'.beginword
  180. let first = 0
  181. let suffix = '.*\%(\n\%(\%(\s*\|.\{6\}\)[*/].*\n\)*\)\=\s*'
  182. if beginword =~? '^\%(ADD\|COMPUTE\|DIVIDE\|MULTIPLY\|SUBTRACT\)$'
  183. let beginword = beginword . suffix . '\<\%(NOT\s\+\)\=ON\s\+SIZE\s\+ERROR'
  184. let g:beginword = beginword
  185. let first = 1
  186. elseif beginword =~? '^\%(STRING\|UNSTRING\)$'
  187. let beginword = beginword . suffix . '\<\%(NOT\s\+\)\=ON\s\+OVERFLOW'
  188. let first = 1
  189. elseif beginword =~? '^\%(ACCEPT\|DISPLAY\)$'
  190. let beginword = beginword . suffix . '\<\%(NOT\s\+\)\=ON\s\+EXCEPTION'
  191. let first = 1
  192. elseif beginword ==? 'CALL'
  193. let beginword = beginword . suffix . '\<\%(NOT\s\+\)\=ON\s\+\%(EXCEPTION\|OVERFLOW\)'
  194. let first = 1
  195. elseif beginword =~? '^\%(DELETE\|REWRITE\|START\|READ\|WRITE\)$'
  196. let first = 1
  197. let beginword = beginword . suffix . '\<\%(NOT\s\+\)\=\(INVALID\s\+KEY'
  198. if beginword =~? '^READ'
  199. let first = 0
  200. let beginword = beginword . '\|AT\s\+END\|NO\s\+DATA'
  201. elseif beginword =~? '^WRITE'
  202. let beginword = beginword . '\|AT\s\+END-OF-PAGE'
  203. endif
  204. let beginword = beginword . '\)'
  205. endif
  206. call cursor(a:lnum,indent(a:lnum))
  207. let match = searchpair('\c-\@<!\<'.beginword.'\>','','\c\<'.endword.'\>\zs','bnW'.(first? 'r' : ''),s:skip)
  208. if match > 0
  209. let ind = indent(match)
  210. elseif cline =~? '^\(END-\(READ\|EVALUATE\|SEARCH\|PERFORM\)\)\>'
  211. let ind = ind - shiftwidth()
  212. endif
  213. endif
  214. return ind < bshft ? bshft : ind
  215. endfunction