cobol.vim (9867B)
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 13 if exists("b:did_ftplugin") 14 finish 15 endif 16 let b:did_ftplugin = 1 17 18 let s:cpo_save = &cpo 19 set cpo&vim 20 21 setlocal commentstring=\ \ \ \ \ \ *%s 22 setlocal comments=:* 23 setlocal fo+=croqlt 24 if get(g:, 'cobol_recommended_style', 25 \ get(g:, 'filetype_recommended_style', 1)) 26 setlocal expandtab 27 setlocal textwidth=72 28 endif 29 30 " matchit support 31 if exists("loaded_matchit") 32 let s:ordot = '\|\ze\.\%( \@=\|$\)' 33 let b:match_ignorecase=1 34 "let b:match_skip = 'getline(".") =~ "^.\\{6\\}[*/C]"' 35 let 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 43 endif 44 45 " add gtk support 46 if (has("gui_win32") || has("gui_gtk")) && !exists("b:browsefilter") 47 let b:browsefilter = "COBOL Source Files (*.cbl, *.cob)\t*.cbl;*.cob;*.lib\n" 48 if has("win32") 49 let b:browsefilter .= "All Files (*.*)\t*\n" 50 else 51 let b:browsefilter .= "All Files (*)\t*\n" 52 endif 53 endif 54 55 let b:undo_ftplugin = "setlocal com< cms< fo< et< tw<" . 56 \ " | unlet! b:browsefilter b:match_words b:match_ignorecase b:match_skip" 57 if !exists("g:no_plugin_maps") && !exists("g:no_cobol_maps") 58 let 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> ]['" 78 endif 79 80 if !exists("g:no_plugin_maps") && !exists("g:no_cobol_maps") 81 if version >= 700 82 nnoremap <silent> <buffer> > :set opfunc=<SID>IncreaseFunc<CR>g@ 83 nnoremap <silent> <buffer> < :set opfunc=<SID>DecreaseFunc<CR>g@ 84 endif 85 nnoremap <silent> <buffer> >> :call CobolIndentBlock(1)<CR> 86 nnoremap <silent> <buffer> << :call CobolIndentBlock(-1)<CR> 87 vnoremap <silent> <buffer> > :call CobolIndentBlock(v:count1)<CR> 88 vnoremap <silent> <buffer> < :call CobolIndentBlock(-v:count1)<CR> 89 inoremap <silent> <buffer> <C-T> <C-R>=<SID>IncreaseIndent()<CR><C-R>=<SID>RestoreShiftwidth()<CR> 90 inoremap <silent> <buffer> <C-D> <C-R>=<SID>DecreaseIndent()<CR><C-R>=<SID>RestoreShiftwidth()<CR> 91 if !maparg("<Tab>","i") 92 inoremap <silent> <buffer> <Tab> <C-R>=<SID>Tab()<CR><C-R>=<SID>RestoreShiftwidth()<CR> 93 endif 94 noremap <silent> <buffer> [[ m':call search('\c^\%(\s*\<Bar>.\{6\}\s\+\)\zs[A-Za-z0-9-]\+\s\+\%(division\<Bar>section\)\s*\.','bW')<CR> 95 noremap <silent> <buffer> ]] m':call search('\c^\%(\s*\<Bar>.\{6\}\s\+\)\zs[A-Za-z0-9-]\+\s\+\%(division\<Bar>section\)\.','W')<CR> 96 noremap <silent> <buffer> [] m':call <SID>toend('b')<CR> 97 noremap <silent> <buffer> ][ m':call <SID>toend('')<CR> 98 " For EnhancedCommentify 99 noremap <silent> <buffer> <Plug>Traditional :call <SID>Comment('t')<CR> 100 noremap <silent> <buffer> <Plug>Comment :call <SID>Comment('c')<CR> 101 noremap <silent> <buffer> <Plug>DeComment :call <SID>Comment('u')<CR> 102 noremap <silent> <buffer> <Plug>VisualTraditional :'<,'>call <SID>Comment('t')<CR> 103 noremap <silent> <buffer> <Plug>VisualComment :'<,'>call <SID>Comment('c')<CR> 104 noremap <silent> <buffer> <Plug>VisualDeComment :'<,'>call <SID>Comment('u')<CR> 105 endif 106 107 let &cpo = s:cpo_save 108 unlet s:cpo_save 109 110 if exists("g:did_cobol_ftplugin_functions") 111 finish 112 endif 113 let g:did_cobol_ftplugin_functions = 1 114 115 function! s:repeat(str,count) 116 let i = 0 117 let ret = "" 118 while i < a:count 119 let ret = ret . a:str 120 let i = i + 1 121 endwhile 122 return ret 123 endfunction 124 125 function! s:increase(...) 126 let lnum = '.' 127 let sw = shiftwidth() 128 let i = a:0 ? a:1 : indent(lnum) 129 if i >= 11 130 return sw - (i - 11) % sw 131 elseif i >= 7 132 return 11-i 133 elseif i == 6 134 return 1 135 else 136 return 6-i 137 endif 138 endfunction 139 140 function! s:decrease(...) 141 let lnum = '.' 142 let sw = shiftwidth() 143 let i = indent(a:0 ? a:1 : lnum) 144 if i >= 11 + sw 145 return 1 + (i + 12) % sw 146 elseif i > 11 147 return i-11 148 elseif i > 7 149 return i-7 150 elseif i == 7 151 return 1 152 else 153 return i 154 endif 155 endfunction 156 157 function! CobolIndentBlock(shift) 158 let head = strpart(getline('.'),0,7) 159 let tail = strpart(getline('.'),7) 160 let indent = match(tail,'[^ ]') 161 let sw = shiftwidth() 162 let shift = a:shift 163 if shift > 0 164 if indent < 4 165 let tail = s:repeat(" ",4-indent).tail 166 let shift = shift - 1 167 endif 168 let tail = s:repeat(" ",shift*sw).tail 169 let shift = 0 170 elseif shift < 0 171 if (indent-4) > -shift * sw 172 let tail = strpart(tail,-shift * sw) 173 elseif (indent-4) > (-shift-1) * sw 174 let tail = strpart(tail,indent - 4) 175 else 176 let tail = strpart(tail,indent) 177 endif 178 endif 179 call setline('.',head.tail) 180 endfunction 181 182 function! s:IncreaseFunc(type) 183 '[,']call CobolIndentBlock(1) 184 endfunction 185 186 function! s:DecreaseFunc(type) 187 '[,']call CobolIndentBlock(-1) 188 endfunction 189 190 function! s:IncreaseIndent() 191 let c = "\<C-T>" 192 if exists("*InsertCtrlTWrapper") 193 let key = InsertCtrlTWrapper() 194 if key != c 195 return key 196 endif 197 endif 198 let interval = s:increase() 199 let b:cobol_shiftwidth = &shiftwidth 200 let &shiftwidth = 1 201 let lastchar = strpart(getline('.'),col('.')-2,1) 202 if lastchar == '0' || lastchar == '^' 203 return "\<BS>".lastchar.c 204 else 205 return s:repeat(c,interval) 206 endif 207 endfunction 208 209 function! s:DecreaseIndent() 210 let c = "\<C-D>" 211 if exists("*InsertCtrlDWrapper") 212 " I hack Ctrl-D to delete when not at the end of the line. 213 let key = InsertCtrlDWrapper() 214 if key != c 215 return key 216 endif 217 endif 218 let interval = s:decrease() 219 let b:cobol_shiftwidth = &shiftwidth 220 let &shiftwidth = 1 221 return s:repeat(c,interval) 222 endfunction 223 224 function! s:RestoreShiftwidth() 225 if exists("b:cobol_shiftwidth") 226 let &shiftwidth=b:cobol_shiftwidth 227 unlet b:cobol_shiftwidth 228 endif 229 return "" 230 endfunction 231 232 function! s:Tab() 233 if (strpart(getline('.'),0,col('.')-1) =~ '^\s*$' && &sta) 234 return s:IncreaseIndent() 235 " &softtabstop < 0: &softtabstop follows &shiftwidth 236 elseif (&sts < 0 || &sts == shiftwidth()) && &sts != 8 && &et 237 return s:repeat(" ",s:increase(col('.')-1)) 238 else 239 return "\<Tab>" 240 endif 241 endfunction 242 243 function! s:Comment(arg) 244 " For EnhancedCommentify 245 let line = getline('.') 246 if (line =~ '^.\{6\}[*/C]' || a:arg == 'c') && a:arg != 'u' 247 let line = substitute(line,'^.\{6\}\zs.',' ','') 248 else 249 let line = substitute(line,'^.\{6\}\zs.','*','') 250 endif 251 call setline('.',line) 252 endfunction 253 254 function! s:toend(direction) 255 let ignore = '^\(\s*\|.\{6\}\)\%([*/]\|\s*$\)' 256 let keep = line('.') 257 keepjumps + 258 while line('.') < line('$') && getline('.') =~ ignore 259 keepjumps + 260 endwhile 261 let res = search('\c^\%(\s*\|.\{6\}\s\+\)\zs[A-Za-z0-9-]\+\s\+\%(division\|section\)\s*\.',a:direction.'W') 262 if a:direction != 'b' && !res 263 let res = line('$') 264 keepjumps $ 265 elseif res 266 keepjumps - 267 endif 268 if res 269 while line('.') > 1 && getline('.') =~ ignore 270 keepjumps - 271 endwhile 272 if line('.') == 1 && getline('.') =~ ignore 273 exe "keepjumps ".keep 274 endif 275 else 276 exe "keepjumps ".keep 277 endif 278 endfunction