Skip to content
Snippets Groups Projects
Commit 033ed536 authored by Kevin Lyda's avatar Kevin Lyda :speech_balloon:
Browse files

Clean up; add Unnumber.

Have the python functions loaded in a better way.  Make less
clashy names.  Make the : commands work.
parent d484bf4f
Branches
No related tags found
No related merge requests found
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2017 Kevin Lyda <kevin@phrye.com>
#
# Distributed under terms of the GPL license.
"""
COBOL utilities.
"""
import vim
import math
def cobol_Renumber():
if vim.current.buffer.options['filetype'] == b'cobol':
gap = {}
last_line_no = 0
lines = vim.current.buffer
for i in range(len(lines)):
if len(lines[i]) == 0 or lines[i][:6] == ' ':
if not gap:
gap['i'] = i
gap['last_line_no'] = last_line_no
else:
try:
last_line_no = int(lines[i][:6])
except ValueError:
print('Malformed line - has non-numbers in columns 1-6')
return
if gap:
delta = math.floor((last_line_no - gap['last_line_no']) /
(i - gap['i'] + 1))
if delta > 1:
new_line_no = gap['last_line_no'] + delta
for j in range(gap['i'], i):
lines[j] = ('%06d' % new_line_no) + lines[j][6:]
new_line_no += delta
gap = {}
if gap:
delta = 100
new_line_no = gap['last_line_no'] + delta
for j in range(gap['i'], len(lines)):
lines[j] = ('%06d' % new_line_no) + lines[j][6:]
new_line_no += delta
else:
print('Not a COBOL file. This is a %s file' %
vim.current.buffer.options['filetype'].decode('UTF-8'))
def cobol_Unnumber():
if vim.current.buffer.options['filetype'] == b'cobol':
lines = vim.current.buffer
for i in range(len(lines)):
if lines[i][:6].isdigit():
lines[i] = ' ' + lines[i][6:]
else:
print('Not a COBOL file. This is a %s file' %
vim.current.buffer.options['filetype'].decode('UTF-8'))
......@@ -17,15 +17,20 @@ endif
let s:path = fnamemodify(resolve(expand('<sfile>:p')), ':h')
function! LoadCobolPython()
!exists('g:cobol_py_loaded')
if !exists('g:cobol_py_loaded')
exec 'py3file ' . s:path . '/cobol.py'
let g:cobol_py_loaded = 1
endif
endfunction
function! Renumber()
exec 'py3file ' . s:path . '/renumber.py'
py3 cobol_Renumber()
endfunction
" call LoadCobolPython()
function! Unnumber()
py3 cobol_Unnumber()
endfunction
call LoadCobolPython()
command! -nargs=0 Renumber call Renumber()
command! -nargs=0 Unnumber call Unnumber()
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2017 Kevin Lyda <kevin@phrye.com>
#
# Distributed under terms of the GPL license.
"""
COBOL utilities.
"""
import vim
import math
if vim.current.buffer.options['filetype'] == b'cobol':
gap = {}
last_line_no = 0
lines = vim.current.buffer
for i in range(len(lines)):
if len(lines[i]) == 0 or lines[i][:6] == ' ':
if not gap:
gap['i'] = i
gap['last_line_no'] = last_line_no
else:
try:
last_line_no = int(lines[i][:6])
except ValueError:
raise ValueError('Malformed line - has non-numbers in columns 1-6')
if gap:
delta = math.floor((last_line_no - gap['last_line_no']) /
(i - gap['i'] + 1))
if delta > 1:
new_line_no = gap['last_line_no'] + delta
for j in range(gap['i'], i):
lines[j] = ('%06d' % new_line_no) + lines[j][6:]
new_line_no += delta
gap = {}
if gap:
delta = 100
new_line_no = gap['last_line_no'] + delta
for j in range(gap['i'], len(lines)):
lines[j] = ('%06d' % new_line_no) + lines[j][6:]
new_line_no += delta
else:
print('Not a COBOL file. This is a %s file' %
vim.current.buffer.options['filetype'].decode('UTF-8'))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment