1 " Vim filetype plugin file
3 " Author: Tim Pope <vimNOSPAM@tpope.info>
4 " $Id: cobol.vim,v 1.7 2008/07/13 17:21:56 vimboss Exp $
6 " Insert mode mappings: <C-T> <C-D> <Tab>
7 " Normal mode mappings: < > << >> [[ ]] [] ][
8 " Visual mode mappings: < >
10 if exists("b:did_ftplugin")
13 let b:did_ftplugin = 1
18 setlocal commentstring=\ \ \ \ \ \ *%s
25 if exists("loaded_matchit")
26 let s:ordot = '\|\ze\.\%( \@=\|$\)'
27 let b:match_ignorecase=1
28 "let b:match_skip = 'getline(".") =~ "^.\\{6\\}[*/C]"'
30 \ '\$if\>:$else\>:\$endif\>,' .
31 \ '[$-]\@<!\<if\>:\<\%(then\|else\)\>:\<end-if\>'.s:ordot.',' .
32 \ '-\@<!\<perform\s\+\%(\d\+\s\+times\|until\|varying\|with\s\+test\)\>:\<end-perform\>'.s:ordot . ',' .
33 \ '-\@<!\<\%(search\|evaluate\)\>:\<\%(when\)\>:\<end-\%(search\|evaluate\)\>' .s:ordot . ',' .
34 \ '-\@<!\<\%(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 . ',' .
35 \ '-\@<!\<\%(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 . ',' .
36 \ '-\@<!\<\%(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
39 if has("gui_win32") && !exists("b:browsefilter")
40 let b:browsefilter = "COBOL Source Files (*.cbl, *.cob)\t*.cbl;*.cob;*.lib\n".
41 \ "All Files (*.*)\t*.*\n"
44 let b:undo_ftplugin = "setlocal com< cms< fo< et< tw<" .
45 \ " | unlet! b:browsefilter b:match_words b:match_ignorecase b:match_skip"
46 if !exists("g:no_plugin_maps") && !exists("g:no_cobol_maps")
47 let b:undo_ftplugin = b:undo_ftplugin .
48 \ " | sil! exe 'nunmap <buffer> <'" .
49 \ " | sil! exe 'nunmap <buffer> >'" .
50 \ " | sil! exe 'nunmap <buffer> <<'" .
51 \ " | sil! exe 'nunmap <buffer> >>'" .
52 \ " | sil! exe 'vunmap <buffer> <'" .
53 \ " | sil! exe 'vunmap <buffer> >'" .
54 \ " | sil! exe 'iunmap <buffer> <C-D>'" .
55 \ " | sil! exe 'iunmap <buffer> <C-T>'" .
56 \ " | sil! exe 'iunmap <buffer> <Tab>'" .
57 \ " | sil! exe 'nunmap <buffer> <Plug>Traditional'" .
58 \ " | sil! exe 'nunmap <buffer> <Plug>Comment'" .
59 \ " | sil! exe 'nunmap <buffer> <Plug>DeComment'" .
60 \ " | sil! exe 'vunmap <buffer> <Plug>VisualTraditional'" .
61 \ " | sil! exe 'vunmap <buffer> <Plug>VisualComment'" .
62 \ " | sil! exe 'iunmap <buffer> <Plug>VisualDeComment'" .
63 \ " | sil! exe 'unmap <buffer> [['" .
64 \ " | sil! exe 'unmap <buffer> ]]'" .
65 \ " | sil! exe 'unmap <buffer> []'" .
66 \ " | sil! exe 'unmap <buffer> ]['"
69 if !exists("g:no_plugin_maps") && !exists("g:no_cobol_maps")
71 nnoremap <silent> <buffer> > :set opfunc=<SID>IncreaseFunc<CR>g@
72 nnoremap <silent> <buffer> < :set opfunc=<SID>DecreaseFunc<CR>g@
74 nnoremap <silent> <buffer> >> :call CobolIndentBlock(1)<CR>
75 nnoremap <silent> <buffer> << :call CobolIndentBlock(-1)<CR>
76 vnoremap <silent> <buffer> > :call CobolIndentBlock(v:count1)<CR>
77 vnoremap <silent> <buffer> < :call CobolIndentBlock(-v:count1)<CR>
78 inoremap <silent> <buffer> <C-T> <C-R>=<SID>IncreaseIndent()<CR><C-R>=<SID>RestoreShiftwidth()<CR>
79 inoremap <silent> <buffer> <C-D> <C-R>=<SID>DecreaseIndent()<CR><C-R>=<SID>RestoreShiftwidth()<CR>
80 if !maparg("<Tab>","i")
81 inoremap <silent> <buffer> <Tab> <C-R>=<SID>Tab()<CR><C-R>=<SID>RestoreShiftwidth()<CR>
83 noremap <silent> <buffer> [[ m':call search('\c^\%(\s*\<Bar>.\{6\}\s\+\)\zs[A-Za-z0-9-]\+\s\+\%(division\<Bar>section\)\s*\.','bW')<CR>
84 noremap <silent> <buffer> ]] m':call search('\c^\%(\s*\<Bar>.\{6\}\s\+\)\zs[A-Za-z0-9-]\+\s\+\%(division\<Bar>section\)\.','W')<CR>
85 noremap <silent> <buffer> [] m':call <SID>toend('b')<CR>
86 noremap <silent> <buffer> ][ m':call <SID>toend('')<CR>
87 " For EnhancedCommentify
88 noremap <silent> <buffer> <Plug>Traditional :call <SID>Comment('t')<CR>
89 noremap <silent> <buffer> <Plug>Comment :call <SID>Comment('c')<CR>
90 noremap <silent> <buffer> <Plug>DeComment :call <SID>Comment('u')<CR>
91 noremap <silent> <buffer> <Plug>VisualTraditional :'<,'>call <SID>Comment('t')<CR>
92 noremap <silent> <buffer> <Plug>VisualComment :'<,'>call <SID>Comment('c')<CR>
93 noremap <silent> <buffer> <Plug>VisualDeComment :'<,'>call <SID>Comment('u')<CR>
99 if exists("g:did_cobol_ftplugin_functions")
102 let g:did_cobol_ftplugin_functions = 1
104 function! s:repeat(str,count)
108 let ret = ret . a:str
114 function! s:increase(...)
117 let i = a:0 ? a:1 : indent(lnum)
119 return sw - (i - 11) % sw
129 function! s:decrease(...)
132 let i = indent(a:0 ? a:1 : lnum)
134 return 1 + (i + 12) % sw
146 function! CobolIndentBlock(shift)
147 let head = strpart(getline('.'),0,7)
148 let tail = strpart(getline('.'),7)
149 let indent = match(tail,'[^ ]')
154 let tail = s:repeat(" ",4-indent).tail
155 let shift = shift - 1
157 let tail = s:repeat(" ",shift*sw).tail
160 if (indent-4) > -shift * sw
161 let tail = strpart(tail,-shift * sw)
162 elseif (indent-4) > (-shift-1) * sw
163 let tail = strpart(tail,indent - 4)
165 let tail = strpart(tail,indent)
168 call setline('.',head.tail)
171 function! s:IncreaseFunc(type)
172 '[,']call CobolIndentBlock(1)
175 function! s:DecreaseFunc(type)
176 '[,']call CobolIndentBlock(-1)
179 function! s:IncreaseIndent()
181 if exists("*InsertCtrlTWrapper")
182 let key = InsertCtrlTWrapper()
187 let interval = s:increase()
188 let b:cobol_shiftwidth = &shiftwidth
190 let lastchar = strpart(getline('.'),col('.')-2,1)
191 if lastchar == '0' || lastchar == '^'
192 return "\<BS>".lastchar.c
194 return s:repeat(c,interval)
198 function! s:DecreaseIndent()
200 if exists("*InsertCtrlDWrapper")
201 " I hack Ctrl-D to delete when not at the end of the line.
202 let key = InsertCtrlDWrapper()
207 let interval = s:decrease()
208 let b:cobol_shiftwidth = &shiftwidth
210 return s:repeat(c,interval)
213 function! s:RestoreShiftwidth()
214 if exists("b:cobol_shiftwidth")
215 let &shiftwidth=b:cobol_shiftwidth
216 unlet b:cobol_shiftwidth
222 if (strpart(getline('.'),0,col('.')-1) =~ '^\s*$' && &sta)
223 return s:IncreaseIndent()
224 elseif &sts == &sw && &sts != 8 && &et
225 return s:repeat(" ",s:increase(col('.')-1))
231 function! s:Comment(arg)
232 " For EnhancedCommentify
233 let line = getline('.')
234 if (line =~ '^.\{6\}[*/C]' || a:arg == 'c') && a:arg != 'u'
235 let line = substitute(line,'^.\{6\}\zs.',' ','')
237 let line = substitute(line,'^.\{6\}\zs.','*','')
239 call setline('.',line)
242 function! s:toend(direction)
243 let ignore = '^\(\s*\|.\{6\}\)\%([*/]\|\s*$\)'
246 while line('.') < line('$') && getline('.') =~ ignore
249 let res = search('\c^\%(\s*\|.\{6\}\s\+\)\zs[A-Za-z0-9-]\+\s\+\%(division\|section\)\s*\.',a:direction.'W')
250 if a:direction != 'b' && !res
257 while line('.') > 1 && getline('.') =~ ignore
260 if line('.') == 1 && getline('.') =~ ignore
261 exe "keepjumps ".keep
264 exe "keepjumps ".keep