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