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