robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
7945732ed7
master
1" Vim filetype plugin file 2" Language: cobol 3" Maintainer: Ankit Jain <ajatkj@yahoo.co.in> 4" (formerly Tim Pope <vimNOSPAM@tpope.info>) 5" Last Update: By Ankit Jain (add gtk support) on 15.08.2020 6" 2024 Jan 14 by Vim Project (browsefilter) 7" 2026 Jun 27 by Vim Project (add recommended style guard) 8 9" Insert mode mappings: <C-T> <C-D> <Tab> 10" Normal mode mappings: < > << >> [[ ]] [] ][ 11" Visual mode mappings: < > 12 13if exists ( "b:did_ftplugin" ) 14finish 15endif 16let b: did_ftplugin = 1 17 18let s: cpo_save = & cpo 19set cpo & vim 20 21setlocal commentstring = \ \ \ \ \ \ *%s 22setlocal comments = :* 23setlocal fo += croqlt 24if get ( g:, 'cobol_recommended_style' , 25 \get ( g:, 'filetype_recommended_style' , 1 )) 26setlocal expandtab 27setlocal textwidth = 72 28endif 29 30" matchit support 31if exists ( "loaded_matchit" ) 32let s: ordot = '\|\ze\.\%( \@=\|$\)' 33let b: match_ignorecase = 1 34"let b:match_skip = 'getline(".") =~ "^.\\{6\\}[*/C]"' 35let b: match_words = 36 \'\$if\>:$else\>:\$endif\>,' . 37 \'[$-]\@<!\<if\>:\<\%(then\|else\)\>:\<end-if\>' . s: ordot . ',' . 38 \'-\@<!\<perform\s\+\%(\d\+\s\+times\|until\|varying\|with\s\+test\)\>:\<end-perform\>' . s: ordot . ',' . 39 \'-\@<!\<\%(search\|evaluate\)\>:\<\%(when\)\>:\<end-\%(search\|evaluate\)\>' . s: ordot . ',' . 40 \'-\@<!\<\%(add\|compute\|divide\|multiply\|subtract\)\>\%(.*\(\%$\|\%(\n\%(\%(\s*\|.\{6\}\)[*/].*\n\)*\)\=\s*\%(not\s\+\)\=on\s\+size\s\+error\>\)\)\@=:\%(\<not\s\+\)\@<!\<\%(not\s\+\)\=on\s\+size\s\+error\>:\<end-\%(add\|compute\|divide\|multiply\|subtract\)\>' . s: ordot . ',' . 41 \'-\@<!\<\%(string\|unstring\|accept\|display\|call\)\>\%(.*\(\%$\|\%(\n\%(\%(\s*\|.\{6\}\)[*/].*\n\)*\)\=\s*\%(not\s\+\)\=on\s\+\%(overflow\|exception\)\>\)\)\@=:\%(\<not\s\+\)\@<!\<\%(not\s\+\)\=on\s\+\%(overflow\|exception\)\>:\<end-\%(string\|unstring\|accept\|display\|call\)\>' . s: ordot . ',' . 42 \'-\@<!\<\%(delete\|rewrite\|start\|write\|read\)\>\%(.*\(\%$\|\%(\n\%(\%(\s*\|.\{6\}\)[*/].*\n\)*\)\=\s*\%(invalid\s\+key\|at\s\+end\|no\s\+data\|at\s\+end-of-page\)\>\)\)\@=:\%(\<not\s\+\)\@<!\<\%(not\s\+\)\=\%(invalid\s\+key\|at\s\+end\|no\s\+data\|at\s\+end-of-page\)\>:\<end-\%(delete\|rewrite\|start\|write\|read\)\>' . s: ordot 43endif 44 45" add gtk support 46if ( has ( "gui_win32" ) || has ( "gui_gtk" )) && ! exists ( "b:browsefilter" ) 47let b: browsefilter = "COBOL Source Files (*.cbl, *.cob)\t*.cbl;*.cob;*.lib\n" 48if has ( "win32" ) 49let b: browsefilter .= "All Files (*.*)\t*\n" 50else 51let b: browsefilter .= "All Files (*)\t*\n" 52endif 53endif 54 55let b: undo_ftplugin = "setlocal com< cms< fo< et< tw<" . 56 \" | unlet! b:browsefilter b:match_words b:match_ignorecase b:match_skip" 57if ! exists ( "g:no_plugin_maps" ) && ! exists ( "g:no_cobol_maps" ) 58let b: undo_ftplugin = b: undo_ftplugin . 59 \" | sil! exe 'nunmap <buffer> <'" . 60 \" | sil! exe 'nunmap <buffer> >'" . 61 \" | sil! exe 'nunmap <buffer> <<'" . 62 \" | sil! exe 'nunmap <buffer> >>'" . 63 \" | sil! exe 'vunmap <buffer> <'" . 64 \" | sil! exe 'vunmap <buffer> >'" . 65 \" | sil! exe 'iunmap <buffer> <C-D>'" . 66 \" | sil! exe 'iunmap <buffer> <C-T>'" . 67 \" | sil! exe 'iunmap <buffer> <Tab>'" . 68 \" | sil! exe 'nunmap <buffer> <Plug>Traditional'" . 69 \" | sil! exe 'nunmap <buffer> <Plug>Comment'" . 70 \" | sil! exe 'nunmap <buffer> <Plug>DeComment'" . 71 \" | sil! exe 'vunmap <buffer> <Plug>VisualTraditional'" . 72 \" | sil! exe 'vunmap <buffer> <Plug>VisualComment'" . 73 \" | sil! exe 'iunmap <buffer> <Plug>VisualDeComment'" . 74 \" | sil! exe 'unmap <buffer> [['" . 75 \" | sil! exe 'unmap <buffer> ]]'" . 76 \" | sil! exe 'unmap <buffer> []'" . 77 \" | sil! exe 'unmap <buffer> ]['" 78endif 79 80if ! exists ( "g:no_plugin_maps" ) && ! exists ( "g:no_cobol_maps" ) 81if version >= 700 82nnoremap <silent> <buffer> > :set opfunc=< SID > IncreaseFunc< CR > g@ 83nnoremap <silent> <buffer> < :set opfunc=< SID > DecreaseFunc< CR > g@ 84endif 85nnoremap <silent> <buffer> >> :call CobolIndentBlock(1)< CR > 86nnoremap <silent> <buffer> << :call CobolIndentBlock(-1)< CR > 87vnoremap <silent> <buffer> > :call CobolIndentBlock(v:count1)< CR > 88vnoremap <silent> <buffer> < :call CobolIndentBlock(-v:count1)< CR > 89inoremap <silent> <buffer> < C-T > < C-R > =< SID > IncreaseIndent()< CR > <C-R > =< SID > RestoreShiftwidth()< CR > 90inoremap <silent> <buffer> < C-D > < C-R > =< SID > DecreaseIndent()< CR > <C-R > =< SID > RestoreShiftwidth()< CR > 91if ! maparg ( "<Tab>" , "i" ) 92inoremap <silent> <buffer> < Tab > < C-R > =< SID > Tab()< CR > <C-R > =< SID > RestoreShiftwidth()< CR > 93endif 94noremap <silent> <buffer> [[ m':call search('\c^\%(\s*\<Bar>.\{6\}\s\+\)\zs[A-Za-z0-9-]\+\s\+\%(division\<Bar>section\)\s*\.','bW')< CR > 95noremap <silent> <buffer> ]] m':call search('\c^\%(\s*\<Bar>.\{6\}\s\+\)\zs[A-Za-z0-9-]\+\s\+\%(division\<Bar>section\)\.','W')< CR > 96noremap <silent> <buffer> [] m':call< SID > toend('b')< CR > 97noremap <silent> <buffer> ][ m':call< SID > toend('')< CR > 98" For EnhancedCommentify 99noremap <silent> <buffer> < Plug > Traditional :call< SID > Comment('t')< CR > 100noremap <silent> <buffer> < Plug > Comment :call< SID > Comment('c')< CR > 101noremap <silent> <buffer> < Plug > DeComment :call< SID > Comment('u')< CR > 102noremap <silent> <buffer> < Plug > VisualTraditional :'< ,'>call< SID > Comment('t')< CR > 103noremap <silent> <buffer> < Plug > VisualComment :'< ,'>call< SID > Comment('c')< CR > 104noremap <silent> <buffer> < Plug > VisualDeComment :'< ,'>call< SID > Comment('u')< CR > 105endif 106 107let & cpo = s: cpo_save 108unlet s: cpo_save 109 110if exists ( "g:did_cobol_ftplugin_functions" ) 111finish 112endif 113let g: did_cobol_ftplugin_functions = 1 114 115function ! s: repeat ( str , count ) 116let i = 0 117let ret = "" 118while i < a: count 119let ret = ret . a: str 120let i = i + 1 121endwhile 122return ret 123endfunction 124 125function ! s: increase (...) 126let lnum = '.' 127let sw = shiftwidth () 128let i = a: 0 ? a: 1 : indent ( lnum ) 129if i >= 11 130return sw - ( i - 11 ) % sw 131elseif i >= 7 132return 11 - i 133elseif i == 6 134return 1 135else 136return 6 - i 137endif 138endfunction 139 140function ! s: decrease (...) 141let lnum = '.' 142let sw = shiftwidth () 143let i = indent ( a: 0 ? a: 1 : lnum ) 144if i >= 11 + sw 145return 1 + ( i + 12 ) % sw 146elseif i > 11 147return i - 11 148elseif i > 7 149return i - 7 150elseif i == 7 151return 1 152else 153return i 154endif 155endfunction 156 157function ! CobolIndentBlock ( shift ) 158let head = strpart ( getline ( '.' ), 0 , 7 ) 159let tail = strpart ( getline ( '.' ), 7 ) 160let indent = match ( tail , '[^ ]' ) 161let sw = shiftwidth () 162let shift = a: shift 163if shift > 0 164if indent < 4 165let tail = s: repeat ( " " , 4 - indent ). tail 166let shift = shift - 1 167endif 168let tail = s: repeat ( " " , shift * sw ). tail 169let shift = 0 170elseif shift < 0 171if ( indent - 4 ) > - shift * sw 172let tail = strpart ( tail , - shift * sw ) 173elseif ( indent - 4 ) > ( - shift - 1 ) * sw 174let tail = strpart ( tail , indent - 4 ) 175else 176let tail = strpart ( tail , indent ) 177endif 178endif 179call setline ( '.' , head . tail ) 180endfunction 181 182function ! s: IncreaseFunc ( type ) 183 '[, ']call CobolIndentBlock ( 1 ) 184endfunction 185 186function ! s: DecreaseFunc ( type ) 187 '[, ']call CobolIndentBlock ( -1 ) 188endfunction 189 190function ! s: IncreaseIndent () 191let c = "\<C-T>" 192if exists ( "*InsertCtrlTWrapper" ) 193let key = InsertCtrlTWrapper () 194if key != c 195return key 196endif 197endif 198let interval = s: increase () 199let b: cobol_shiftwidth = & shiftwidth 200let & shiftwidth = 1 201let lastchar = strpart ( getline ( '.' ), col ( '.' ) - 2 , 1 ) 202if lastchar == '0' || lastchar == '^' 203return "\<BS>" . lastchar . c 204else 205return s: repeat ( c , interval ) 206endif 207endfunction 208 209function ! s: DecreaseIndent () 210let c = "\<C-D>" 211if exists ( "*InsertCtrlDWrapper" ) 212" I hack Ctrl-D to delete when not at the end of the line. 213let key = InsertCtrlDWrapper () 214if key != c 215return key 216endif 217endif 218let interval = s: decrease () 219let b: cobol_shiftwidth = & shiftwidth 220let & shiftwidth = 1 221return s: repeat ( c , interval ) 222endfunction 223 224function ! s: RestoreShiftwidth () 225if exists ( "b:cobol_shiftwidth" ) 226let & shiftwidth = b: cobol_shiftwidth 227unlet b: cobol_shiftwidth 228endif 229return "" 230endfunction 231 232function ! s: Tab () 233if ( strpart ( getline ( '.' ), 0 , col ( '.' ) - 1 ) =~ '^\s*$' && & sta ) 234return s: IncreaseIndent () 235" &softtabstop < 0: &softtabstop follows &shiftwidth 236elseif ( & sts < 0 || & sts == shiftwidth ()) && & sts != 8 && & et 237return s: repeat ( " " , s: increase ( col ( '.' ) - 1 )) 238else 239return "\<Tab>" 240endif 241endfunction 242 243function ! s: Comment ( arg ) 244" For EnhancedCommentify 245let line = getline ( '.' ) 246if ( line =~ '^.\{6\}[*/C]' || a: arg == 'c' ) && a: arg != 'u' 247let line = substitute ( line , '^.\{6\}\zs.' , ' ' , '' ) 248else 249let line = substitute ( line , '^.\{6\}\zs.' , '*' , '' ) 250endif 251call setline ( '.' , line ) 252endfunction 253 254function ! s: toend ( direction ) 255let ignore = '^\(\s*\|.\{6\}\)\%([*/]\|\s*$\)' 256let keep = line ( '.' ) 257keepjumps + 258while line ( '.' ) < line ( '$' ) && getline ( '.' ) =~ ignore 259keepjumps + 260endwhile 261let res = search ( '\c^\%(\s*\|.\{6\}\s\+\)\zs[A-Za-z0-9-]\+\s\+\%(division\|section\)\s*\.' , a: direction . 'W' ) 262if a: direction != 'b' && ! res 263let res = line ( '$' ) 264keepjumps $ 265elseif res 266keepjumps - 267endif 268if res 269while line ( '.' ) > 1 && getline ( '.' ) =~ ignore 270keepjumps - 271endwhile 272if line ( '.' ) == 1 && getline ( '.' ) =~ ignore 273exe "keepjumps " . keep 274endif 275else 276exe "keepjumps " . keep 277endif 278endfunction